diff --git a/doc/crypt.tex b/doc/crypt.tex index d57bceec7..ae6cecee5 100644 --- a/doc/crypt.tex +++ b/doc/crypt.tex @@ -5747,24 +5747,19 @@ \subsection{X25519 Key Operations} \end{verbatim} To generate a fresh X25529 key, one can use \textit{x25519\_make\_key} which will create a private\&public key-pair. - -\index{x25519\_set\_key} +\index{x25519\_import} \begin{verbatim} -int x25519_set_key(const unsigned char *k, unsigned long klen, - const unsigned char *u, unsigned long ulen, - curve25519_key *key); +int x25519_import(const unsigned char *in, unsigned long inlen, curve25519_key *key); \end{verbatim} -To import a public or private key in raw format, one can use the function \textit{x25519\_set\_key}. -In case both, the secret part \textit{k} and the public part \textit{u} are given, the operation validates that the given -public part fits to the secret part. +The \textit{x25519\_import} function can be used to import a public key in DER-encoded \textit{SubjectPublicKeyInfo} format. -\index{x25519\_import} +\index{x25519\_import\_raw} \begin{verbatim} -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); \end{verbatim} -The \textit{x25519\_import} function can be used to import a public key in DER-encoded \textit{SubjectPublicKeyInfo} format. +To import a public or private key in raw format, one can use the function \textit{x25519\_import\_raw}. \index{x25519\_import\_x509} \begin{verbatim} @@ -5836,23 +5831,19 @@ \subsection{EdDSA Key Operations} To generate a fresh Ed25529 key, one can use \textit{ed25519\_make\_key} which will create a private\&public key-pair. -\index{ed25519\_set\_key} +\index{ed25519\_import} \begin{verbatim} -int ed25519_set_key(const unsigned char *sk, unsigned long sklen, - const unsigned char *pk, unsigned long pklen, - curve25519_key *key); +int ed25519_import(const unsigned char *in, unsigned long inlen, curve25519_key *key); \end{verbatim} -To import a public or private key in raw format, one can use the function \textit{ed25519\_set\_key}. -In case both, the secret part \textit{sk} and the public part \textit{pk} are given, the operation validates that the given -public part fits to the secret part. +The \textit{ed25519\_import} function can be used to import a public key in DER-encoded \textit{SubjectPublicKeyInfo} format. -\index{ed25519\_import} +\index{ed25519\_import\_raw} \begin{verbatim} -int ed25519_import(const unsigned char *in, unsigned long inlen, curve25519_key *key); +int ed25519_import_raw(const unsigned char *in, unsigned long inlen, int which, curve25519_key *key); \end{verbatim} -The \textit{ed25519\_import} function can be used to import a public key in DER-encoded \textit{SubjectPublicKeyInfo} format. +To import a public or private key in raw format, one can use the function \textit{ed25519\_import\_raw}. \index{ed25519\_import\_x509} \begin{verbatim} diff --git a/libtomcrypt_VS2008.vcproj b/libtomcrypt_VS2008.vcproj index 7e1cf5327..a3588d976 100644 --- a/libtomcrypt_VS2008.vcproj +++ b/libtomcrypt_VS2008.vcproj @@ -2511,15 +2511,15 @@ > priv, in, sizeof(key->priv)); + tweetnacl_crypto_sk_to_pk(key->pub, key->priv); + } else if (which == PK_PUBLIC) { + XMEMCPY(key->pub, in, sizeof(key->pub)); + } else { + return CRYPT_INVALID_ARG; + } + key->algo = PKA_ED25519; + key->type = which; + + return CRYPT_OK; +} + +#endif + +/* ref: $Format:%D$ */ +/* git commit: $Format:%H$ */ +/* commit time: $Format:%ai$ */ diff --git a/src/pk/ed25519/ed25519_set_key.c b/src/pk/ed25519/ed25519_set_key.c deleted file mode 100644 index de68e69d6..000000000 --- a/src/pk/ed25519/ed25519_set_key.c +++ /dev/null @@ -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 ed25519_set_ku.c - Set the parameters of an Ed25519 key, Steffen Jaeckel -*/ - -#ifdef LTC_CURVE25519 - -/** - Set the parameters of an Ed25519 key - - In case sk and pk are given it is validated that pk is really the - corresponding public part of the key pair. - - @param sk The secret key - @param sklen The length of sk - @param pk The public key - @param pklen The length of pk - @param key [out] Destination of the key - @return CRYPT_OK if successful -*/ -int ed25519_set_key(const unsigned char *sk, unsigned long sklen, - const unsigned char *pk, unsigned long pklen, - curve25519_key *key) -{ - LTC_ARGCHK(key != NULL); - - if (sk != NULL) { - LTC_ARGCHK(sklen == 32uL); - XMEMCPY(key->priv, sk, sizeof(key->priv)); - tweetnacl_crypto_sk_to_pk(key->pub, key->priv); - if (pk != NULL) { - LTC_ARGCHK(pklen == 32uL); - if (XMEM_NEQ(pk, key->pub, sizeof(key->pub)) != 0) { - zeromem(key, sizeof(*key)); - return CRYPT_INVALID_ARG; - } - } - key->type = PK_PRIVATE; - } else if (pk != NULL) { - LTC_ARGCHK(pklen == 32uL); - XMEMCPY(key->pub, pk, sizeof(key->pub)); - key->type = PK_PUBLIC; - } else { - return CRYPT_INVALID_ARG; - } - key->algo = PKA_ED25519; - - return CRYPT_OK; -} - -#endif - -/* ref: $Format:%D$ */ -/* git commit: $Format:%H$ */ -/* commit time: $Format:%ai$ */ diff --git a/src/pk/x25519/x25519_import.c b/src/pk/x25519/x25519_import.c index f62b383c8..65185637c 100644 --- a/src/pk/x25519/x25519_import.c +++ b/src/pk/x25519/x25519_import.c @@ -10,13 +10,13 @@ /** @file x25519_import.c - Import a X25519 key from a binary packet, Steffen Jaeckel + Import a X25519 key from a SubjectPublicKeyInfo, Steffen Jaeckel */ #ifdef LTC_CURVE25519 /** - Import a X25519 key from a binary packet + Import a X25519 key @param in The packet to read @param inlen The length of the input packet @param key [out] Where to import the key to @@ -30,16 +30,6 @@ int x25519_import(const unsigned char *in, unsigned long inlen, curve25519_key * LTC_ARGCHK(in != NULL); LTC_ARGCHK(key != NULL); - /* There's only one case where the inlen is equal to the pubkey-size - * and that's a raw pubkey, so let's just do a raw import. - */ - if (inlen == sizeof(key->pub)) { - XMEMCPY(key->pub, in, sizeof(key->pub)); - key->type = PK_PUBLIC; - key->algo = PKA_X25519; - return CRYPT_OK; - } - key_len = sizeof(key->pub); if ((err = x509_decode_subject_public_key_info(in, inlen, PKA_X25519, key->pub, &key_len, LTC_ASN1_EOL, NULL, 0uL)) == CRYPT_OK) { key->type = PK_PUBLIC; diff --git a/src/pk/x25519/x25519_import_raw.c b/src/pk/x25519/x25519_import_raw.c new file mode 100644 index 000000000..01090850b --- /dev/null +++ b/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$ */ diff --git a/src/pk/x25519/x25519_set_key.c b/src/pk/x25519/x25519_set_key.c deleted file mode 100644 index df276e172..000000000 --- a/src/pk/x25519/x25519_set_key.c +++ /dev/null @@ -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$ */ diff --git a/tests/ed25519_test.c b/tests/ed25519_test.c index 12d0ac891..5d469514c 100644 --- a/tests/ed25519_test.c +++ b/tests/ed25519_test.c @@ -201,7 +201,7 @@ static int _rfc_8032_7_1_test(void) DO(base16_decode(rfc_8032_7_1[n].message, XSTRLEN(rfc_8032_7_1[n].message), msg, &mlen)); siglen = sizeof(sig); DO(base16_decode(rfc_8032_7_1[n].signature, XSTRLEN(rfc_8032_7_1[n].signature), sig, &siglen)); - DO(ed25519_set_key(sec, slen, pub, plen, &key)); + DO(ed25519_import_raw(sec, slen, PK_PRIVATE, &key)); buflen = sizeof(buf); DO(ed25519_sign(msg, mlen, buf, &buflen, &key)); DO(do_compare_testvector(buf, buflen, sig, siglen, "Ed25519 RFC8032 7.1 - sign", n)); @@ -214,7 +214,7 @@ static int _rfc_8032_7_1_test(void) DO(base16_decode(rfc_8032_7_1[n].message, XSTRLEN(rfc_8032_7_1[n].message), msg, &mlen)); siglen = sizeof(sig); DO(base16_decode(rfc_8032_7_1[n].signature, XSTRLEN(rfc_8032_7_1[n].signature), sig, &siglen)); - DO(ed25519_set_key(NULL, 0, pub, plen, &key2)); + DO(ed25519_import_raw(pub, plen, PK_PUBLIC, &key2)); DO(ed25519_verify(msg, mlen, sig, siglen, &ret, &key2)); DO(do_compare_testvector(&ret, sizeof(ret), &should, sizeof(should), "Ed25519 RFC8032 7.1 - verify w/ pubkey", n)); diff --git a/tests/x25519_test.c b/tests/x25519_test.c index 703fc3052..7daa849c0 100644 --- a/tests/x25519_test.c +++ b/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_import(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));