فهرست منبع

add {ed,x}25519_import_x509()

Steffen Jaeckel 6 سال پیش
والد
کامیت
34196b90b9
3فایلهای تغییر یافته به همراه114 افزوده شده و 1 حذف شده
  1. 4 1
      src/headers/tomcrypt_pk.h
  2. 55 0
      src/pk/ed25519/ed25519_import_x509.c
  3. 55 0
      src/pk/x25519/x25519_import_x509.c

+ 4 - 1
src/headers/tomcrypt_pk.h

@@ -346,7 +346,6 @@ typedef struct {
 } curve25519_key;
 
 
-
 /* Ed25519 Signature API */
 int ed25519_make_key(prng_state *prng, int wprng, curve25519_key *key);
 
@@ -356,6 +355,8 @@ int ed25519_export(       unsigned char *out, unsigned long *outlen,
 
 int ed25519_import(const unsigned char *in, unsigned long inlen, curve25519_key *key);
 
+int ed25519_import_x509(const unsigned char *in, unsigned long inlen, curve25519_key *key);
+
 int ed25519_set_key(const unsigned char *sk, unsigned long sklen,
                     const unsigned char *pk, unsigned long pklen,
                          curve25519_key *key);
@@ -377,6 +378,8 @@ int x25519_export(       unsigned char *out, unsigned long *outlen,
 
 int x25519_import(const unsigned char *in, unsigned long inlen, curve25519_key *key);
 
+int x25519_import_x509(const unsigned char *in, unsigned long inlen, curve25519_key *key);
+
 int x25519_set_ku(const unsigned char *k,  unsigned long klen,
                   const unsigned char *u,  unsigned long ulen,
                        curve25519_key *key);

+ 55 - 0
src/pk/ed25519/ed25519_import_x509.c

@@ -0,0 +1,55 @@
+/* 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_import_x509.c
+  Import a Ed25519 key from a X.509 certificate, Steffen Jaeckel
+*/
+
+#ifdef LTC_CURVE25519
+
+static int _ed25519_decode(const unsigned char *in, unsigned long inlen, curve25519_key *key)
+{
+   if (inlen != sizeof(key->pub)) return CRYPT_PK_INVALID_SIZE;
+   XMEMCPY(key->pub, in, sizeof(key->pub));
+   return CRYPT_OK;
+}
+
+/**
+  Import a Ed25519 public key from a X.509 certificate
+  @param in     The DER encoded X.509 certificate
+  @param inlen  The length of the certificate
+  @param key    [out] Where to import the key to
+  @return CRYPT_OK if successful, on error all allocated memory is freed automatically
+*/
+int ed25519_import_x509(const unsigned char *in, unsigned long inlen, curve25519_key *key)
+{
+   int err = CRYPT_INVALID_ARG;
+
+   LTC_ARGCHK(in  != NULL);
+   LTC_ARGCHK(key != NULL);
+
+   if ((err = x509_decode_public_key_from_certificate(in, inlen,
+                                                      PKA_ED25519,
+                                                      LTC_ASN1_EOL, NULL, NULL,
+                                                      (public_key_decode_cb)_ed25519_decode, key)) != CRYPT_OK) {
+      return err;
+   }
+   key->type = PK_PUBLIC;
+   key->algo = PKA_ED25519;
+
+   return err;
+}
+
+#endif
+
+/* ref:         $Format:%D$ */
+/* git commit:  $Format:%H$ */
+/* commit time: $Format:%ai$ */

+ 55 - 0
src/pk/x25519/x25519_import_x509.c

@@ -0,0 +1,55 @@
+/* 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 x25519_import_x509.c
+  Import a X25519 key from a X.509 certificate, Steffen Jaeckel
+*/
+
+#ifdef LTC_CURVE25519
+
+static int _x25519_decode(const unsigned char *in, unsigned long inlen, curve25519_key *key)
+{
+   if (inlen != sizeof(key->pub)) return CRYPT_PK_INVALID_SIZE;
+   XMEMCPY(key->pub, in, sizeof(key->pub));
+   return CRYPT_OK;
+}
+
+/**
+  Import a X25519 public key from a X.509 certificate
+  @param in     The DER encoded X.509 certificate
+  @param inlen  The length of the certificate
+  @param key    [out] Where to import the key to
+  @return CRYPT_OK if successful, on error all allocated memory is freed automatically
+*/
+int x25519_import_x509(const unsigned char *in, unsigned long inlen, curve25519_key *key)
+{
+   int err = CRYPT_INVALID_ARG;
+
+   LTC_ARGCHK(in  != NULL);
+   LTC_ARGCHK(key != NULL);
+
+   if ((err = x509_decode_public_key_from_certificate(in, inlen,
+                                                      PKA_X25519,
+                                                      LTC_ASN1_EOL, NULL, NULL,
+                                                      (public_key_decode_cb)_x25519_decode, key)) != CRYPT_OK) {
+      return err;
+   }
+   key->type = PK_PUBLIC;
+   key->algo = PKA_X25519;
+
+   return err;
+}
+
+#endif
+
+/* ref:         $Format:%D$ */
+/* git commit:  $Format:%H$ */
+/* commit time: $Format:%ai$ */