Browse Source

rename x25519_set_key to x25519_import_raw

Steffen Jaeckel 6 years ago
parent
commit
334876db78
4 changed files with 57 additions and 74 deletions
  1. 1 4
      src/headers/tomcrypt_pk.h
  2. 51 0
      src/pk/x25519/x25519_import_raw.c
  3. 0 65
      src/pk/x25519/x25519_set_key.c
  4. 5 5
      tests/x25519_test.c

+ 1 - 4
src/headers/tomcrypt_pk.h

@@ -371,15 +371,12 @@ int ed25519_verify(const  unsigned char *msg, unsigned long msglen,
 /** X25519 Key-Exchange API */
 int x25519_make_key(prng_state *prng, int wprng, curve25519_key *key);
 
-int x25519_set_key(const unsigned char *k,  unsigned long klen,
-                   const unsigned char *u,  unsigned long ulen,
-                        curve25519_key *key);
-
 int x25519_export(       unsigned char *out, unsigned long *outlen,
                                    int  which,
                   const curve25519_key *key);
 
 int x25519_import(const unsigned char *in, unsigned long inlen, curve25519_key *key);
+int x25519_import_raw(const unsigned char *in, unsigned long inlen, int which, curve25519_key *key);
 int x25519_import_x509(const unsigned char *in, unsigned long inlen, curve25519_key *key);
 int x25519_import_pkcs8(const unsigned char *in, unsigned long inlen,
                                  const void *pwd, unsigned long pwdlen,

+ 51 - 0
src/pk/x25519/x25519_import_raw.c

@@ -0,0 +1,51 @@
+/* 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_raw.c
+  Set the parameters of a X25519 key, Steffen Jaeckel
+*/
+
+#ifdef LTC_CURVE25519
+
+/**
+   Set the parameters of a X25519 key
+
+   @param in       The key
+   @param inlen    The length of the key
+   @param which    Which type of key (PK_PRIVATE or PK_PUBLIC)
+   @param key      [out] Destination of the key
+   @return CRYPT_OK if successful
+*/
+int x25519_import_raw(const unsigned char *in, unsigned long inlen, int which, curve25519_key *key)
+{
+   LTC_ARGCHK(in   != NULL);
+   LTC_ARGCHK(inlen == 32uL);
+   LTC_ARGCHK(key  != NULL);
+
+   if (which == PK_PRIVATE) {
+      XMEMCPY(key->priv, in, sizeof(key->priv));
+      tweetnacl_crypto_scalarmult_base(key->pub, key->priv);
+   } else if (which == PK_PUBLIC) {
+      XMEMCPY(key->pub, in, sizeof(key->pub));
+   } else {
+      return CRYPT_INVALID_ARG;
+   }
+   key->algo = PKA_X25519;
+   key->type = which;
+
+   return CRYPT_OK;
+}
+
+#endif
+
+/* ref:         $Format:%D$ */
+/* git commit:  $Format:%H$ */
+/* commit time: $Format:%ai$ */

+ 0 - 65
src/pk/x25519/x25519_set_key.c

@@ -1,65 +0,0 @@
-/* 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_set_ku.c
-  Set the parameters of a X25519 key, Steffen Jaeckel
-*/
-
-#ifdef LTC_CURVE25519
-
-/**
-   Set the parameters of a X25519 key
-
-   In case k and u are given it is validated that u is really the
-   corresponding public part of the key pair
-
-   @param k        The k value (a.k.a scalar or private part)
-   @param klen     The length of k
-   @param u        The u-coordinate (a.k.a public part)
-   @param ulen     The length of u
-   @param key      [out] Destination of the key
-   @return CRYPT_OK if successful
-*/
-int x25519_set_key(const unsigned char *k, unsigned long klen,
-                   const unsigned char *u, unsigned long ulen,
-                        curve25519_key *key)
-{
-   LTC_ARGCHK(key != NULL);
-
-   if (k != NULL) {
-      LTC_ARGCHK(klen == 32uL);
-      XMEMCPY(key->priv, k, sizeof(key->priv));
-      tweetnacl_crypto_scalarmult_base(key->pub, key->priv);
-      if (u != NULL) {
-         LTC_ARGCHK(ulen == 32uL);
-         if (XMEM_NEQ(u, key->pub, sizeof(key->pub)) != 0) {
-            zeromem(key, sizeof(*key));
-            return CRYPT_INVALID_ARG;
-         }
-      }
-      key->type = PK_PRIVATE;
-   } else if (u != NULL) {
-      LTC_ARGCHK(ulen == 32uL);
-      XMEMCPY(key->pub, u, sizeof(key->pub));
-      key->type = PK_PUBLIC;
-   } else {
-      return CRYPT_INVALID_ARG;
-   }
-   key->algo = PKA_X25519;
-
-   return CRYPT_OK;
-}
-
-#endif
-
-/* ref:         $Format:%D$ */
-/* git commit:  $Format:%H$ */
-/* commit time: $Format:%ai$ */

+ 5 - 5
tests/x25519_test.c

@@ -101,10 +101,10 @@ static int _rfc_7748_6_test(void)
    unsigned char buf[32];
    unsigned long buflen = sizeof(buf);
 
-   DO(x25519_set_key(alice_private, sizeof(alice_private), alice_public, sizeof(alice_public), &alice_priv));
-   DO(x25519_set_key(bob_private, sizeof(bob_private), bob_public, sizeof(bob_public), &bob_priv));
-   DO(x25519_set_key(NULL, 0, alice_public, sizeof(alice_public), &alice_pub));
-   DO(x25519_set_key(NULL, 0, bob_public, sizeof(bob_public), &bob_pub));
+   DO(x25519_import_raw(alice_private, sizeof(alice_private), PK_PRIVATE, &alice_priv));
+   DO(x25519_import_raw(bob_private, sizeof(bob_private), PK_PRIVATE, &bob_priv));
+   DO(x25519_import_raw(alice_public, sizeof(alice_public), PK_PUBLIC, &alice_pub));
+   DO(x25519_import_raw(bob_public, sizeof(bob_public), PK_PUBLIC, &bob_pub));
 
    DO(x25519_shared_secret(&alice_priv, &bob_pub, buf, &buflen));
    DO(compare_testvector(buf, buflen, shared_secret, sizeof(shared_secret), "x25519 - RFC 7748 Ch. 6", 0));
@@ -199,7 +199,7 @@ static int _x25519_compat_test(void)
 
    buflen = sizeof(buf);
    DO(x25519_export(buf, &buflen, PK_PUBLIC, &priv));
-   DO(x25519_set_key(NULL, 0, buf, buflen, &pub));
+   DO(x25519_import_raw(buf, buflen, PK_PUBLIC, &pub));
 
    buflen = sizeof(buf);
    DO(x25519_export(buf, &buflen, PK_PUBLIC | PK_STD, &priv));