Browse Source

use shared {ed,x}25519_export() implementation

Steffen Jaeckel 6 years ago
parent
commit
0392867678

+ 3 - 1
src/headers/tomcrypt_private.h

@@ -320,7 +320,9 @@ int ec25519_import_pkcs8(const unsigned char *in, unsigned long inlen,
                        const void *pwd, unsigned long pwdlen,
                        enum ltc_oid_id id, sk_to_pk fp,
                        curve25519_key *key);
-
+int ec25519_export(       unsigned char *out, unsigned long *outlen,
+                                    int  which,
+                   const curve25519_key *key);
 #endif /* LTC_CURVE25519 */
 
 #ifdef LTC_DER

+ 100 - 0
src/pk/ec25519/ec25519_export.c

@@ -0,0 +1,100 @@
+/* LibTomCrypt, modular cryptographic library -- Tom St Denis
+ *
+ * LibTomCrypt is a library that provides various cryptographic
+ * algorithms in a highly modular and flexible manner.
+ *
+ * The library is free for all purposes without any express
+ * guarantee it works.
+ */
+#include "tomcrypt_private.h"
+
+/**
+  @file ed25519_export.c
+  Export a Ed25519 key to a binary packet, Steffen Jaeckel
+*/
+
+#ifdef LTC_CURVE25519
+
+/**
+   Generic export of a Curve/Ed25519 key to a binary packet
+   @param out    [out] The destination for the key
+   @param outlen [in/out] The max size and resulting size of the Ed25519 key
+   @param type   Which type of key (PK_PRIVATE, PK_PUBLIC|PK_STD or PK_PUBLIC)
+   @param key    The key you wish to export
+   @return CRYPT_OK if successful
+*/
+int ec25519_export(       unsigned char *out, unsigned long *outlen,
+                                    int  which,
+                   const curve25519_key *key)
+{
+   int err, std;
+   const char* OID;
+   unsigned long oid[16], oidlen;
+   ltc_asn1_list alg_id[1];
+   unsigned char private_key[34];
+   unsigned long version, private_key_len = sizeof(private_key);
+
+   LTC_ARGCHK(out       != NULL);
+   LTC_ARGCHK(outlen    != NULL);
+   LTC_ARGCHK(key       != NULL);
+
+   std = which & PK_STD;
+   which &= ~PK_STD;
+
+   if (which == PK_PRIVATE) {
+      if(key->type != PK_PRIVATE) return CRYPT_PK_INVALID_TYPE;
+
+      if (std == PK_STD) {
+         if ((err = pk_get_oid(key->algo, &OID)) != CRYPT_OK) {
+            return err;
+         }
+         oidlen = sizeof(oid)/sizeof(oid[0]);
+         if ((err = pk_oid_str_to_num(OID, oid, &oidlen)) != CRYPT_OK) {
+            return err;
+         }
+
+         LTC_SET_ASN1(alg_id, 0, LTC_ASN1_OBJECT_IDENTIFIER, oid, oidlen);
+
+         /* encode private key as PKCS#8 */
+         if ((err = der_encode_octet_string(key->priv, 32uL, private_key, &private_key_len)) != CRYPT_OK) {
+            return err;
+         }
+
+         version = 0;
+         err = der_encode_sequence_multi(out, outlen,
+                                   LTC_ASN1_SHORT_INTEGER,            1uL, &version,
+                                   LTC_ASN1_SEQUENCE,                 1uL, alg_id,
+                                   LTC_ASN1_OCTET_STRING, private_key_len, private_key,
+                                   LTC_ASN1_EOL,                      0uL, NULL);
+      } else {
+         if (*outlen < sizeof(key->priv)) {
+            err = CRYPT_BUFFER_OVERFLOW;
+         } else {
+            XMEMCPY(out, key->priv, sizeof(key->priv));
+            err = CRYPT_OK;
+         }
+         *outlen = sizeof(key->priv);
+      }
+   } else {
+      if (std == PK_STD) {
+         /* encode public key as SubjectPublicKeyInfo */
+         err = x509_encode_subject_public_key_info(out, outlen, key->algo, key->pub, 32uL, LTC_ASN1_EOL, NULL, 0);
+      } else {
+         if (*outlen < sizeof(key->pub)) {
+            err = CRYPT_BUFFER_OVERFLOW;
+         } else {
+            XMEMCPY(out, key->pub, sizeof(key->pub));
+            err = CRYPT_OK;
+         }
+         *outlen = sizeof(key->pub);
+      }
+   }
+
+   return err;
+}
+
+#endif
+
+/* ref:         $Format:%D$ */
+/* git commit:  $Format:%H$ */
+/* commit time: $Format:%ai$ */

+ 2 - 53
src/pk/ed25519/ed25519_export.c

@@ -27,62 +27,11 @@ int ed25519_export(       unsigned char *out, unsigned long *outlen,
                                     int  which,
                    const curve25519_key *key)
 {
-   int err, std;
-   const char* OID;
-   unsigned long oid[16], oidlen;
-   ltc_asn1_list alg_id[1];
-   unsigned char private_key[34];
-   unsigned long version, private_key_len = sizeof(private_key);
-
-   LTC_ARGCHK(out       != NULL);
-   LTC_ARGCHK(outlen    != NULL);
-   LTC_ARGCHK(key       != NULL);
+   LTC_ARGCHK(key != NULL);
 
    if (key->algo != PKA_ED25519) return CRYPT_PK_INVALID_TYPE;
 
-   std = which & PK_STD;
-   which &= ~PK_STD;
-
-   if (which == PK_PRIVATE) {
-      if(key->type != PK_PRIVATE) return CRYPT_PK_INVALID_TYPE;
-
-      if ((err = pk_get_oid(PKA_ED25519, &OID)) != CRYPT_OK) {
-         return err;
-      }
-      oidlen = sizeof(oid)/sizeof(oid[0]);
-      if ((err = pk_oid_str_to_num(OID, oid, &oidlen)) != CRYPT_OK) {
-         return err;
-      }
-
-      LTC_SET_ASN1(alg_id, 0, LTC_ASN1_OBJECT_IDENTIFIER, oid, oidlen);
-
-      /* encode private key as PKCS#8 */
-      if ((err = der_encode_octet_string(key->priv, 32uL, private_key, &private_key_len)) != CRYPT_OK) {
-         return err;
-      }
-
-      version = 0;
-      err = der_encode_sequence_multi(out, outlen,
-                                LTC_ASN1_SHORT_INTEGER,            1uL, &version,
-                                LTC_ASN1_SEQUENCE,                 1uL, alg_id,
-                                LTC_ASN1_OCTET_STRING, private_key_len, private_key,
-                                LTC_ASN1_EOL,                      0uL, NULL);
-   } else {
-      if (std == PK_STD) {
-         /* encode public key as SubjectPublicKeyInfo */
-         err = x509_encode_subject_public_key_info(out, outlen, PKA_ED25519, key->pub, 32uL, LTC_ASN1_EOL, NULL, 0);
-      } else {
-         if (*outlen < sizeof(key->pub)) {
-            err = CRYPT_BUFFER_OVERFLOW;
-         } else {
-            XMEMCPY(out, key->pub, sizeof(key->pub));
-            err = CRYPT_OK;
-         }
-         *outlen = sizeof(key->pub);
-      }
-   }
-
-   return err;
+   return ec25519_export(out, outlen, which, key);
 }
 
 #endif

+ 2 - 53
src/pk/x25519/x25519_export.c

@@ -27,62 +27,11 @@ int x25519_export(      unsigned char *out, unsigned long *outlen,
                                   int  which,
                   const    curve25519_key *key)
 {
-   int err, std;
-   const char* OID;
-   unsigned long oid[16], oidlen;
-   ltc_asn1_list alg_id[1];
-   unsigned char private_key[34];
-   unsigned long version, private_key_len = sizeof(private_key);
-
-   LTC_ARGCHK(out       != NULL);
-   LTC_ARGCHK(outlen    != NULL);
-   LTC_ARGCHK(key       != NULL);
+   LTC_ARGCHK(key != NULL);
 
    if (key->algo != PKA_X25519) return CRYPT_PK_INVALID_TYPE;
 
-   std = which & PK_STD;
-   which &= ~PK_STD;
-
-   if (which == PK_PRIVATE) {
-      if(key->type != PK_PRIVATE) return CRYPT_PK_INVALID_TYPE;
-
-      if ((err = pk_get_oid(PKA_X25519, &OID)) != CRYPT_OK) {
-         return err;
-      }
-      oidlen = sizeof(oid)/sizeof(oid[0]);
-      if ((err = pk_oid_str_to_num(OID, oid, &oidlen)) != CRYPT_OK) {
-         return err;
-      }
-
-      LTC_SET_ASN1(alg_id, 0, LTC_ASN1_OBJECT_IDENTIFIER, oid, oidlen);
-
-      /* encode private key as PKCS#8 */
-      if ((err = der_encode_octet_string(key->priv, 32uL, private_key, &private_key_len)) != CRYPT_OK) {
-         return err;
-      }
-
-      version = 0;
-      err = der_encode_sequence_multi(out, outlen,
-                                LTC_ASN1_SHORT_INTEGER,            1uL, &version,
-                                LTC_ASN1_SEQUENCE,                 1uL, alg_id,
-                                LTC_ASN1_OCTET_STRING, private_key_len, private_key,
-                                LTC_ASN1_EOL,                      0uL, NULL);
-   } else {
-      if (std == PK_STD) {
-         /* encode public key as SubjectPublicKeyInfo */
-         err = x509_encode_subject_public_key_info(out, outlen, PKA_X25519, key->pub, 32uL, LTC_ASN1_EOL, NULL, 0uL);
-      } else {
-         if (*outlen < sizeof(key->pub)) {
-            err = CRYPT_BUFFER_OVERFLOW;
-         } else {
-            XMEMCPY(out, key->pub, sizeof(key->pub));
-            err = CRYPT_OK;
-         }
-         *outlen = sizeof(key->pub);
-      }
-   }
-
-   return err;
+   return ec25519_export(out, outlen, which, key);
 }
 
 #endif

+ 1 - 1
tests/x25519_test.c

@@ -192,7 +192,7 @@ static int _x25519_compat_test(void)
 
    DO(x25519_make_key(&yarrow_prng, prng_idx, &priv));
 
-   DO(x25519_export(buf, &buflen, PK_PRIVATE, &priv));
+   DO(x25519_export(buf, &buflen, PK_PRIVATE | PK_STD, &priv));
    DO(x25519_import_pkcs8(buf, buflen, NULL, 0, &imported));
    DO(do_compare_testvector(&priv, sizeof(priv), &imported, sizeof(imported), "priv after ex-&import", __LINE__));
    XMEMSET(&imported, 0, sizeof(imported));