]> git.ipfire.org Git - thirdparty/strongswan.git/commitdiff
openssl: Add utility function that creates an EC_GROUP object from ECC parameters
authorTobias Brunner <tobias@strongswan.org>
Tue, 10 Sep 2013 15:35:30 +0000 (17:35 +0200)
committerTobias Brunner <tobias@strongswan.org>
Mon, 16 Sep 2013 13:05:54 +0000 (15:05 +0200)
src/libstrongswan/plugins/openssl/Makefile.am
src/libstrongswan/plugins/openssl/openssl_ec_diffie_hellman.c
src/libstrongswan/plugins/openssl/openssl_ec_private_key.c
src/libstrongswan/plugins/openssl/openssl_ec_util.c [new file with mode: 0644]
src/libstrongswan/plugins/openssl/openssl_ec_util.h [new file with mode: 0644]
src/libstrongswan/plugins/openssl/openssl_util.h

index cbfd69b71ff7818a2440c06751f495b465bc7653..f3c217b11371ca6a743a307ba23c63b63c1ad954 100644 (file)
@@ -23,6 +23,7 @@ libstrongswan_openssl_la_SOURCES = \
        openssl_ec_diffie_hellman.c openssl_ec_diffie_hellman.h \
        openssl_ec_private_key.c openssl_ec_private_key.h \
        openssl_ec_public_key.c openssl_ec_public_key.h \
+       openssl_ec_util.c openssl_ec_util.h \
        openssl_x509.c openssl_x509.h \
        openssl_crl.c openssl_crl.h \
        openssl_pkcs7.c openssl_pkcs7.h \
index d846278c821a5eaf33e4bb1979387be8a48b74ee..67ac4634dc5b8d9ed1658bfde8e4ba468d4f7a48 100644 (file)
@@ -21,6 +21,7 @@
 #include <openssl/objects.h>
 
 #include "openssl_ec_diffie_hellman.h"
+#include "openssl_ec_util.h"
 #include "openssl_util.h"
 
 #include <utils/debug.h>
index 12f264267efc3eecc014a987cfcb14734011b7fb..e65372802d61402875aed692c531089fa75b8a90 100644 (file)
@@ -20,6 +20,7 @@
 
 #include "openssl_ec_private_key.h"
 #include "openssl_ec_public_key.h"
+#include "openssl_ec_util.h"
 #include "openssl_util.h"
 
 #include <utils/debug.h>
diff --git a/src/libstrongswan/plugins/openssl/openssl_ec_util.c b/src/libstrongswan/plugins/openssl/openssl_ec_util.c
new file mode 100644 (file)
index 0000000..d340a59
--- /dev/null
@@ -0,0 +1,89 @@
+/*
+ * Copyright (C) 2013 Tobias Brunner
+ * Hochschule fuer Technik Rapperswil
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2 of the License, or (at your
+ * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * for more details.
+ */
+
+#include <openssl/opensslconf.h>
+
+#ifndef OPENSSL_NO_EC
+
+#include "openssl_ec_util.h"
+
+#include <openssl/bn.h>
+
+/*
+ * See header
+ */
+EC_GROUP *openssl_ec_group_for_curve(ec_curve_t curve)
+{
+       ec_params_t *params;
+       BIGNUM *p, *a, *b, *x, *y, *q;
+       const BIGNUM *h;
+       EC_POINT *G = NULL;
+       EC_GROUP *group = NULL, *result = NULL;
+       BN_CTX *ctx = NULL;
+
+       params = ec_get_params(curve);
+       if (!params)
+       {
+               return NULL;
+       }
+       ctx = BN_CTX_new();
+       p = BN_bin2bn(params->p.ptr, params->p.len, NULL);
+       a = BN_bin2bn(params->a.ptr, params->a.len, NULL);
+       b = BN_bin2bn(params->b.ptr, params->b.len, NULL);
+       x = BN_bin2bn(params->x.ptr, params->x.len, NULL);
+       y = BN_bin2bn(params->y.ptr, params->y.len, NULL);
+       q = BN_bin2bn(params->q.ptr, params->q.len, NULL);
+       /* all supported groups currently have a cofactor of 1 */
+       h = BN_value_one();
+       if (!ctx || !p || !a || !b || !x || !y || !q)
+       {
+               goto failed;
+       }
+       group = EC_GROUP_new_curve_GFp(p, a, b, ctx);
+       if (!group)
+       {
+               goto failed;
+       }
+       G = EC_POINT_new(group);
+       if (!G || !EC_POINT_set_affine_coordinates_GFp(group, G, x, y, ctx))
+       {
+               goto failed;
+       }
+       if (!EC_GROUP_set_generator(group, G, q, h))
+       {
+               goto failed;
+       }
+       result = group;
+
+failed:
+       if (!result && group)
+       {
+               EC_GROUP_free(group);
+       }
+       if (G)
+       {
+               EC_POINT_free(G);
+       }
+       BN_CTX_free(ctx);
+       BN_free(p);
+       BN_free(a);
+       BN_free(b);
+       BN_free(x);
+       BN_free(y);
+       BN_free(q);
+       return result;
+}
+
+#endif /* OPENSSL_NO_EC */
diff --git a/src/libstrongswan/plugins/openssl/openssl_ec_util.h b/src/libstrongswan/plugins/openssl/openssl_ec_util.h
new file mode 100644 (file)
index 0000000..2ff5c72
--- /dev/null
@@ -0,0 +1,40 @@
+/*
+ * Copyright (C) 2013 Tobias Brunner
+ * Hochschule fuer Technik Rapperswil
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2 of the License, or (at your
+ * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * for more details.
+ */
+
+/**
+ * @defgroup openssl_ec_util openssl_ec_util
+ * @{ @ingroup openssl_p
+ */
+
+#ifndef OPENSSL_EC_UTIL_H_
+#define OPENSSL_EC_UTIL_H_
+
+#include <openssl/ec.h>
+#include <crypto/ec_params.h>
+
+/**
+ * Returns the length in bytes of a field element
+ */
+#define EC_FIELD_ELEMENT_LEN(group) ((EC_GROUP_get_degree(group) + 7) / 8)
+
+/**
+ * Create an EC_GROUP object for the given curve.
+ *
+ * @param curve                curve
+ * @return                     allocated EC_GROUP object or NULL
+ */
+EC_GROUP *openssl_ec_group_for_curve(ec_curve_t curve);
+
+#endif /** OPENSSL_EC_UTIL_H_ @}*/
index ce2a9e10912468f18b0c095e32c9935ee525ce68..1ea373012dcfee3586268966aaaed8702ab75810 100644 (file)
 #include <openssl/bn.h>
 #include <openssl/asn1.h>
 
-/**
- * Returns the length in bytes of a field element
- */
-#define EC_FIELD_ELEMENT_LEN(group) ((EC_GROUP_get_degree(group) + 7) / 8)
-
 /**
  * Creates a hash of a given type of a chunk of data.
  *