|
@@ -39,14 +39,12 @@
|
|
|
#include <wolfcrypt/src/misc.c>
|
|
|
#endif
|
|
|
|
|
|
-
|
|
|
-/*
|
|
|
- generate an ed25519 key pair.
|
|
|
- returns 0 on success
|
|
|
+/* generate an ed25519 key pair.
|
|
|
+ * returns 0 on success
|
|
|
*/
|
|
|
int wc_ed25519_make_key(RNG* rng, int keySz, ed25519_key* key)
|
|
|
{
|
|
|
- byte az[64];
|
|
|
+ byte az[ED25519_PRV_KEY_SIZE];
|
|
|
int ret;
|
|
|
ge_p3 A;
|
|
|
|
|
@@ -57,16 +55,25 @@ int wc_ed25519_make_key(RNG* rng, int keySz, ed25519_key* key)
|
|
|
if (keySz != ED25519_KEY_SIZE)
|
|
|
return BAD_FUNC_ARG;
|
|
|
|
|
|
- ret = 0;
|
|
|
- ret |= wc_RNG_GenerateBlock(rng, key->k, 32);
|
|
|
- ret |= wc_Sha512Hash(key->k, 32, az);
|
|
|
- az[0] &= 248;
|
|
|
- az[31] &= 63;
|
|
|
+ ret = wc_RNG_GenerateBlock(rng, key->k, ED25519_KEY_SIZE);
|
|
|
+ if (ret != 0)
|
|
|
+ return ret;
|
|
|
+ ret = wc_Sha512Hash(key->k, ED25519_KEY_SIZE, az);
|
|
|
+ if (ret != 0) {
|
|
|
+ ForceZero(key->k, ED25519_KEY_SIZE);
|
|
|
+ return ret;
|
|
|
+ }
|
|
|
+
|
|
|
+ /* apply clamp */
|
|
|
+ az[0] &= 248;
|
|
|
+ az[31] &= 63; /* same than az[31] &= 127 because of az[31] |= 64 */
|
|
|
az[31] |= 64;
|
|
|
|
|
|
ge_scalarmult_base(&A, az);
|
|
|
ge_p3_tobytes(key->p, &A);
|
|
|
- XMEMMOVE(key->k + 32, key->p, 32);
|
|
|
+
|
|
|
+ /* put public key after private key, on the same buffer */
|
|
|
+ XMEMMOVE(key->k + ED25519_KEY_SIZE, key->p, ED25519_PUB_KEY_SIZE);
|
|
|
|
|
|
return ret;
|
|
|
}
|
|
@@ -76,43 +83,56 @@ int wc_ed25519_make_key(RNG* rng, int keySz, ed25519_key* key)
|
|
|
in contains the message to sign
|
|
|
inlen is the length of the message to sign
|
|
|
out is the buffer to write the signature
|
|
|
- outlen [in/out] input size of out buf
|
|
|
- output gets set as the final length of out
|
|
|
+ outLen [in/out] input size of out buf
|
|
|
+ output gets set as the final length of out
|
|
|
key is the ed25519 key to use when signing
|
|
|
return 0 on success
|
|
|
*/
|
|
|
int wc_ed25519_sign_msg(const byte* in, word32 inlen, byte* out,
|
|
|
- word32 *outlen, ed25519_key* key)
|
|
|
+ word32 *outLen, ed25519_key* key)
|
|
|
{
|
|
|
ge_p3 R;
|
|
|
byte nonce[SHA512_DIGEST_SIZE];
|
|
|
byte hram[SHA512_DIGEST_SIZE];
|
|
|
- byte az[64];
|
|
|
- word32 sigSz;
|
|
|
+ byte az[ED25519_PRV_KEY_SIZE];
|
|
|
Sha512 sha;
|
|
|
- int ret = 0;
|
|
|
+ int ret;
|
|
|
|
|
|
/* sanity check on arguments */
|
|
|
- if (in == NULL || out == NULL || outlen == NULL || key == NULL)
|
|
|
+ if (in == NULL || out == NULL || outLen == NULL || key == NULL)
|
|
|
return BAD_FUNC_ARG;
|
|
|
|
|
|
/* check and set up out length */
|
|
|
- ret = 0;
|
|
|
- sigSz = wc_ed25519_sig_size(key);
|
|
|
- if (*outlen < sigSz)
|
|
|
- return BAD_FUNC_ARG;
|
|
|
- *outlen = sigSz;
|
|
|
+ if (*outLen < ED25519_SIG_SIZE) {
|
|
|
+ *outLen = ED25519_SIG_SIZE;
|
|
|
+ return BUFFER_E;
|
|
|
+ }
|
|
|
+ *outLen = ED25519_SIG_SIZE;
|
|
|
|
|
|
/* step 1: create nonce to use where nonce is r in
|
|
|
r = H(h_b, ... ,h_2b-1,M) */
|
|
|
- ret |= wc_Sha512Hash(key->k,32,az);
|
|
|
+ ret = wc_Sha512Hash(key->k, ED25519_KEY_SIZE, az);
|
|
|
+ if (ret != 0)
|
|
|
+ return ret;
|
|
|
+
|
|
|
+ /* apply clamp */
|
|
|
az[0] &= 248;
|
|
|
- az[31] &= 63;
|
|
|
+ az[31] &= 63; /* same than az[31] &= 127 because of az[31] |= 64 */
|
|
|
az[31] |= 64;
|
|
|
- ret |= wc_InitSha512(&sha);
|
|
|
- ret |= wc_Sha512Update(&sha, az + 32, 32);
|
|
|
- ret |= wc_Sha512Update(&sha, in, inlen);
|
|
|
- ret |= wc_Sha512Final(&sha, nonce);
|
|
|
+
|
|
|
+ ret = wc_InitSha512(&sha);
|
|
|
+ if (ret != 0)
|
|
|
+ return ret;
|
|
|
+ ret = wc_Sha512Update(&sha, az + ED25519_KEY_SIZE, ED25519_KEY_SIZE);
|
|
|
+ if (ret != 0)
|
|
|
+ return ret;
|
|
|
+ ret = wc_Sha512Update(&sha, in, inlen);
|
|
|
+ if (ret != 0)
|
|
|
+ return ret;
|
|
|
+ ret = wc_Sha512Final(&sha, nonce);
|
|
|
+ if (ret != 0)
|
|
|
+ return ret;
|
|
|
+
|
|
|
sc_reduce(nonce);
|
|
|
|
|
|
/* step 2: computing R = rB where rB is the scalar multiplication of
|
|
@@ -122,13 +142,24 @@ int wc_ed25519_sign_msg(const byte* in, word32 inlen, byte* out,
|
|
|
|
|
|
/* step 3: hash R + public key + message getting H(R,A,M) then
|
|
|
creating S = (r + H(R,A,M)a) mod l */
|
|
|
- ret |= wc_InitSha512(&sha);
|
|
|
- ret |= wc_Sha512Update(&sha, out, 32);
|
|
|
- ret |= wc_Sha512Update(&sha, key->p, 32);
|
|
|
- ret |= wc_Sha512Update(&sha, in, inlen);
|
|
|
- ret |= wc_Sha512Final(&sha, hram);
|
|
|
+ ret = wc_InitSha512(&sha);
|
|
|
+ if (ret != 0)
|
|
|
+ return ret;
|
|
|
+ ret = wc_Sha512Update(&sha, out, ED25519_SIG_SIZE/2);
|
|
|
+ if (ret != 0)
|
|
|
+ return ret;
|
|
|
+ ret = wc_Sha512Update(&sha, key->p, ED25519_PUB_KEY_SIZE);
|
|
|
+ if (ret != 0)
|
|
|
+ return ret;
|
|
|
+ ret = wc_Sha512Update(&sha, in, inlen);
|
|
|
+ if (ret != 0)
|
|
|
+ return ret;
|
|
|
+ ret = wc_Sha512Final(&sha, hram);
|
|
|
+ if (ret != 0)
|
|
|
+ return ret;
|
|
|
+
|
|
|
sc_reduce(hram);
|
|
|
- sc_muladd(out + 32, hram, az, nonce);
|
|
|
+ sc_muladd(out + (ED25519_SIG_SIZE/2), hram, az, nonce);
|
|
|
|
|
|
return ret;
|
|
|
}
|
|
@@ -144,11 +175,10 @@ int wc_ed25519_sign_msg(const byte* in, word32 inlen, byte* out,
|
|
|
int wc_ed25519_verify_msg(byte* sig, word32 siglen, const byte* msg,
|
|
|
word32 msglen, int* stat, ed25519_key* key)
|
|
|
{
|
|
|
- byte rcheck[32];
|
|
|
+ byte rcheck[ED25519_KEY_SIZE];
|
|
|
byte h[SHA512_DIGEST_SIZE];
|
|
|
ge_p3 A;
|
|
|
ge_p2 R;
|
|
|
- word32 sigSz;
|
|
|
int ret;
|
|
|
Sha512 sha;
|
|
|
|
|
@@ -156,14 +186,11 @@ int wc_ed25519_verify_msg(byte* sig, word32 siglen, const byte* msg,
|
|
|
if (sig == NULL || msg == NULL || stat == NULL || key == NULL)
|
|
|
return BAD_FUNC_ARG;
|
|
|
|
|
|
- ret = 0;
|
|
|
+ /* set verification failed by default */
|
|
|
*stat = 0;
|
|
|
- sigSz = wc_ed25519_size(key);
|
|
|
|
|
|
/* check on basics needed to verify signature */
|
|
|
- if (siglen < sigSz)
|
|
|
- return BAD_FUNC_ARG;
|
|
|
- if (sig[63] & 224)
|
|
|
+ if (siglen < ED25519_SIG_SIZE || (sig[ED25519_SIG_SIZE-1] & 224))
|
|
|
return BAD_FUNC_ARG;
|
|
|
|
|
|
/* uncompress A (public key), test if valid, and negate it */
|
|
@@ -171,24 +198,41 @@ int wc_ed25519_verify_msg(byte* sig, word32 siglen, const byte* msg,
|
|
|
return BAD_FUNC_ARG;
|
|
|
|
|
|
/* find H(R,A,M) and store it as h */
|
|
|
- ret |= wc_InitSha512(&sha);
|
|
|
- ret |= wc_Sha512Update(&sha, sig, 32);
|
|
|
- ret |= wc_Sha512Update(&sha, key->p, 32);
|
|
|
- ret |= wc_Sha512Update(&sha, msg, msglen);
|
|
|
- ret |= wc_Sha512Final(&sha, h);
|
|
|
+ ret = wc_InitSha512(&sha);
|
|
|
+ if (ret != 0)
|
|
|
+ return ret;
|
|
|
+ ret = wc_Sha512Update(&sha, sig, ED25519_SIG_SIZE/2);
|
|
|
+ if (ret != 0)
|
|
|
+ return ret;
|
|
|
+ ret = wc_Sha512Update(&sha, key->p, ED25519_PUB_KEY_SIZE);
|
|
|
+ if (ret != 0)
|
|
|
+ return ret;
|
|
|
+ ret = wc_Sha512Update(&sha, msg, msglen);
|
|
|
+ if (ret != 0)
|
|
|
+ return ret;
|
|
|
+ ret = wc_Sha512Final(&sha, h);
|
|
|
+ if (ret != 0)
|
|
|
+ return ret;
|
|
|
+
|
|
|
sc_reduce(h);
|
|
|
|
|
|
/*
|
|
|
Uses a fast single-signature verification SB = R + H(R,A,M)A becomes
|
|
|
SB - H(R,A,M)A saving decompression of R
|
|
|
*/
|
|
|
- ret |= ge_double_scalarmult_vartime(&R, h, &A, sig + 32);
|
|
|
+ ret = ge_double_scalarmult_vartime(&R, h, &A, sig + (ED25519_SIG_SIZE/2));
|
|
|
+ if (ret != 0)
|
|
|
+ return ret;
|
|
|
+
|
|
|
ge_tobytes(rcheck, &R);
|
|
|
|
|
|
/* comparison of R created to R in sig */
|
|
|
- ret |= ConstantCompare(rcheck, sig, 32);
|
|
|
+ ret = ConstantCompare(rcheck, sig, ED25519_SIG_SIZE/2);
|
|
|
+ if (ret != 0)
|
|
|
+ return ret;
|
|
|
|
|
|
- *stat = (ret == 0)? 1: 0;
|
|
|
+ /* set the verification status */
|
|
|
+ *stat = 1;
|
|
|
|
|
|
return ret;
|
|
|
}
|
|
@@ -223,19 +267,17 @@ void wc_ed25519_free(ed25519_key* key)
|
|
|
*/
|
|
|
int wc_ed25519_export_public(ed25519_key* key, byte* out, word32* outLen)
|
|
|
{
|
|
|
- word32 keySz;
|
|
|
-
|
|
|
/* sanity check on arguments */
|
|
|
if (key == NULL || out == NULL || outLen == NULL)
|
|
|
return BAD_FUNC_ARG;
|
|
|
|
|
|
- keySz = wc_ed25519_size(key);
|
|
|
- if (*outLen < keySz) {
|
|
|
- *outLen = keySz;
|
|
|
+ if (*outLen < ED25519_PUB_KEY_SIZE) {
|
|
|
+ *outLen = ED25519_PUB_KEY_SIZE;
|
|
|
return BUFFER_E;
|
|
|
}
|
|
|
- *outLen = keySz;
|
|
|
- XMEMCPY(out, key->p, keySz);
|
|
|
+
|
|
|
+ *outLen = ED25519_PUB_KEY_SIZE;
|
|
|
+ XMEMCPY(out, key->p, ED25519_PUB_KEY_SIZE);
|
|
|
|
|
|
return 0;
|
|
|
}
|
|
@@ -249,37 +291,35 @@ int wc_ed25519_export_public(ed25519_key* key, byte* out, word32* outLen)
|
|
|
*/
|
|
|
int wc_ed25519_import_public(const byte* in, word32 inLen, ed25519_key* key)
|
|
|
{
|
|
|
- word32 keySz;
|
|
|
int ret;
|
|
|
|
|
|
/* sanity check on arguments */
|
|
|
if (in == NULL || key == NULL)
|
|
|
return BAD_FUNC_ARG;
|
|
|
|
|
|
- keySz = wc_ed25519_size(key);
|
|
|
-
|
|
|
- if (inLen < keySz)
|
|
|
+ if (inLen < ED25519_PUB_KEY_SIZE)
|
|
|
return BAD_FUNC_ARG;
|
|
|
|
|
|
/* compressed prefix according to draft
|
|
|
http://www.ietf.org/id/draft-koch-eddsa-for-openpgp-02.txt */
|
|
|
- if (in[0] == 0x40) {
|
|
|
+ if (in[0] == 0x40 && inLen > ED25519_PUB_KEY_SIZE) {
|
|
|
/* key is stored in compressed format so just copy in */
|
|
|
- XMEMCPY(key->p, (in + 1), keySz);
|
|
|
+ XMEMCPY(key->p, (in + 1), ED25519_PUB_KEY_SIZE);
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
/* importing uncompressed public key */
|
|
|
- if (in[0] == 0x04) {
|
|
|
+ if (in[0] == 0x04 && inLen > 2*ED25519_PUB_KEY_SIZE) {
|
|
|
/* pass in (x,y) and store compressed key */
|
|
|
- ret = ge_compress_key(key->p, (in+1), (in+1+keySz), keySz);
|
|
|
+ ret = ge_compress_key(key->p, in+1,
|
|
|
+ in+1+ED25519_PUB_KEY_SIZE, ED25519_PUB_KEY_SIZE);
|
|
|
return ret;
|
|
|
}
|
|
|
|
|
|
/* if not specified compressed or uncompressed check key size
|
|
|
if key size is equal to compressed key size copy in key */
|
|
|
- if (inLen == keySz) {
|
|
|
- XMEMCPY(key->p, in, keySz);
|
|
|
+ if (inLen == ED25519_PUB_KEY_SIZE) {
|
|
|
+ XMEMCPY(key->p, in, ED25519_PUB_KEY_SIZE);
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
@@ -294,77 +334,129 @@ int wc_ed25519_import_public(const byte* in, word32 inLen, ed25519_key* key)
|
|
|
int wc_ed25519_import_private_key(const byte* priv, word32 privSz,
|
|
|
const byte* pub, word32 pubSz, ed25519_key* key)
|
|
|
{
|
|
|
- word32 keySz;
|
|
|
int ret;
|
|
|
|
|
|
/* sanity check on arguments */
|
|
|
if (priv == NULL || pub == NULL || key == NULL)
|
|
|
return BAD_FUNC_ARG;
|
|
|
|
|
|
- keySz = wc_ed25519_size(key);
|
|
|
-
|
|
|
/* key size check */
|
|
|
- if (privSz < keySz || pubSz < keySz)
|
|
|
+ if (privSz < ED25519_KEY_SIZE || pubSz < ED25519_PUB_KEY_SIZE)
|
|
|
return BAD_FUNC_ARG;
|
|
|
|
|
|
- XMEMCPY(key->k, priv, keySz);
|
|
|
+ /* import public key */
|
|
|
ret = wc_ed25519_import_public(pub, pubSz, key);
|
|
|
- XMEMCPY((key->k + keySz), key->p, keySz);
|
|
|
+ if (ret != 0)
|
|
|
+ return ret;
|
|
|
+
|
|
|
+ /* make the private key (priv + pub) */
|
|
|
+ XMEMCPY(key->k, priv, ED25519_KEY_SIZE);
|
|
|
+ XMEMCPY(key->k + ED25519_KEY_SIZE, key->p, ED25519_PUB_KEY_SIZE);
|
|
|
|
|
|
return ret;
|
|
|
}
|
|
|
|
|
|
|
|
|
/*
|
|
|
- outLen should contain the size of out buffer when input. outLen is than set
|
|
|
- to the final output length.
|
|
|
- returns 0 on success
|
|
|
+ export private key only (secret part so 32 bytes)
|
|
|
+ outLen should contain the size of out buffer when input. outLen is than set
|
|
|
+ to the final output length.
|
|
|
+ returns 0 on success
|
|
|
*/
|
|
|
int wc_ed25519_export_private_only(ed25519_key* key, byte* out, word32* outLen)
|
|
|
{
|
|
|
- word32 keySz;
|
|
|
+ /* sanity checks on arguments */
|
|
|
+ if (key == NULL || out == NULL || outLen == NULL)
|
|
|
+ return BAD_FUNC_ARG;
|
|
|
+
|
|
|
+ if (*outLen < ED25519_KEY_SIZE) {
|
|
|
+ *outLen = ED25519_KEY_SIZE;
|
|
|
+ return BUFFER_E;
|
|
|
+ }
|
|
|
+
|
|
|
+ *outLen = ED25519_KEY_SIZE;
|
|
|
+ XMEMCPY(out, key->k, ED25519_KEY_SIZE);
|
|
|
+
|
|
|
+ return 0;
|
|
|
+}
|
|
|
|
|
|
+/*
|
|
|
+ export private key, including public part
|
|
|
+ outLen should contain the size of out buffer when input. outLen is than set
|
|
|
+ to the final output length.
|
|
|
+ returns 0 on success
|
|
|
+ */
|
|
|
+int wc_ed25519_export_private(ed25519_key* key, byte* out, word32* outLen)
|
|
|
+{
|
|
|
/* sanity checks on arguments */
|
|
|
if (key == NULL || out == NULL || outLen == NULL)
|
|
|
return BAD_FUNC_ARG;
|
|
|
|
|
|
- keySz = wc_ed25519_size(key);
|
|
|
- if (*outLen < keySz) {
|
|
|
- *outLen = keySz;
|
|
|
+ if (*outLen < ED25519_PRV_KEY_SIZE) {
|
|
|
+ *outLen = ED25519_PRV_KEY_SIZE;
|
|
|
return BUFFER_E;
|
|
|
}
|
|
|
- *outLen = keySz;
|
|
|
- XMEMCPY(out, key->k, keySz);
|
|
|
+
|
|
|
+ *outLen = ED25519_PRV_KEY_SIZE;
|
|
|
+ XMEMCPY(out, key->k, ED25519_PRV_KEY_SIZE);
|
|
|
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
+/* export full private key and public key
|
|
|
+ return 0 on success
|
|
|
+ */
|
|
|
+int wc_ed25519_export_key(ed25519_key* key,
|
|
|
+ byte* priv, word32 *privSz,
|
|
|
+ byte* pub, word32 *pubSz)
|
|
|
+{
|
|
|
+ int ret;
|
|
|
+
|
|
|
+ /* export 'full' private part */
|
|
|
+ ret = wc_ed25519_export_private(key, priv, privSz);
|
|
|
+ if (ret != 0)
|
|
|
+ return ret;
|
|
|
+
|
|
|
+ /* export public part */
|
|
|
+ ret = wc_ed25519_export_public(key, pub, pubSz);
|
|
|
+
|
|
|
+ return ret;
|
|
|
+}
|
|
|
|
|
|
-/* is the compressed key size in bytes */
|
|
|
+/* returns the private key size (secret only) in bytes */
|
|
|
int wc_ed25519_size(ed25519_key* key)
|
|
|
{
|
|
|
- word32 keySz;
|
|
|
-
|
|
|
if (key == NULL)
|
|
|
return BAD_FUNC_ARG;
|
|
|
|
|
|
- keySz = ED25519_KEY_SIZE;
|
|
|
+ return ED25519_KEY_SIZE;
|
|
|
+}
|
|
|
+
|
|
|
+/* returns the private key size (secret + public) in bytes */
|
|
|
+int wc_ed25519_priv_size(ed25519_key* key)
|
|
|
+{
|
|
|
+ if (key == NULL)
|
|
|
+ return BAD_FUNC_ARG;
|
|
|
|
|
|
- return keySz;
|
|
|
+ return ED25519_PRV_KEY_SIZE;
|
|
|
}
|
|
|
|
|
|
+/* returns the compressed key size in bytes (public key) */
|
|
|
+int wc_ed25519_pub_size(ed25519_key* key)
|
|
|
+{
|
|
|
+ if (key == NULL)
|
|
|
+ return BAD_FUNC_ARG;
|
|
|
+
|
|
|
+ return ED25519_PUB_KEY_SIZE;
|
|
|
+}
|
|
|
|
|
|
/* returns the size of signature in bytes */
|
|
|
int wc_ed25519_sig_size(ed25519_key* key)
|
|
|
{
|
|
|
- word32 sigSz;
|
|
|
-
|
|
|
if (key == NULL)
|
|
|
return BAD_FUNC_ARG;
|
|
|
|
|
|
- sigSz = ED25519_SIG_SIZE;
|
|
|
-
|
|
|
- return sigSz;
|
|
|
+ return ED25519_SIG_SIZE;
|
|
|
}
|
|
|
|
|
|
#endif /* HAVE_ED25519 */
|