From ff590780536063897d73725c5b5c8fae7affd6be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mois=C3=A9s=20Guimar=C3=A3es?= Date: Mon, 7 Jul 2014 22:30:22 -0300 Subject: [PATCH 1/7] remove stack reduction macros from hash functions --- ctaocrypt/src/md2.c | 15 ++++++++++++--- ctaocrypt/src/md5.c | 15 ++++++++++++--- ctaocrypt/src/sha.c | 15 ++++++++++++--- ctaocrypt/src/sha256.c | 15 ++++++++++++--- ctaocrypt/src/sha512.c | 32 +++++++++++++++++++++++++------- 5 files changed, 73 insertions(+), 19 deletions(-) diff --git a/ctaocrypt/src/md2.c b/ctaocrypt/src/md2.c index 30a1ec5f7..e129cf73c 100644 --- a/ctaocrypt/src/md2.c +++ b/ctaocrypt/src/md2.c @@ -132,16 +132,25 @@ void Md2Final(Md2* md2, byte* hash) int Md2Hash(const byte* data, word32 len, byte* hash) { - DECLARE_VAR(Md2, md2); +#ifdef CYASSL_SMALL_STACK + Md2* md2; +#else + Md2 md2[1]; +#endif - if (!CREATE_VAR(Md2, md2)) +#ifdef CYASSL_SMALL_STACK + md2 = (Md2*)XMALLOC(sizeof(Md2), NULL, DYNAMIC_TYPE_TMP_BUFFER); + if (md2 == NULL) return MEMORY_E; +#endif InitMd2(md2); Md2Update(md2, data, len); Md2Final(md2, hash); - DESTROY_VAR(md2); +#ifdef CYASSL_SMALL_STACK + XFREE(md2, NULL, DYNAMIC_TYPE_TMP_BUFFER); +#endif return 0; } diff --git a/ctaocrypt/src/md5.c b/ctaocrypt/src/md5.c index 1bf23f88e..4a375391d 100644 --- a/ctaocrypt/src/md5.c +++ b/ctaocrypt/src/md5.c @@ -365,16 +365,25 @@ void Md5Final(Md5* md5, byte* hash) int Md5Hash(const byte* data, word32 len, byte* hash) { - DECLARE_VAR(Md5, md5); +#ifdef CYASSL_SMALL_STACK + Md5* md5; +#else + Md5 md5[1]; +#endif - if (!CREATE_VAR(Md5, md5)) +#ifdef CYASSL_SMALL_STACK + md5 = (Md5*)XMALLOC(sizeof(Md5), NULL, DYNAMIC_TYPE_TMP_BUFFER); + if (md5 == NULL) return MEMORY_E; +#endif InitMd5(md5); Md5Update(md5, data, len); Md5Final(md5, hash); - DESTROY_VAR(md5); +#ifdef CYASSL_SMALL_STACK + XFREE(md5, NULL, DYNAMIC_TYPE_TMP_BUFFER); +#endif return 0; } diff --git a/ctaocrypt/src/sha.c b/ctaocrypt/src/sha.c index 9e9651433..7501312d5 100644 --- a/ctaocrypt/src/sha.c +++ b/ctaocrypt/src/sha.c @@ -399,10 +399,17 @@ int ShaFinal(Sha* sha, byte* hash) int ShaHash(const byte* data, word32 len, byte* hash) { int ret = 0; - DECLARE_VAR(Sha, sha); +#ifdef CYASSL_SMALL_STACK + Sha* sha; +#else + Sha sha[1]; +#endif - if (!CREATE_VAR(Sha, sha)) +#ifdef CYASSL_SMALL_STACK + sha = (Sha*)XMALLOC(sizeof(Sha), NULL, DYNAMIC_TYPE_TMP_BUFFER); + if (sha == NULL) return MEMORY_E; +#endif if ((ret = InitSha(sha)) != 0) { CYASSL_MSG("InitSha failed"); @@ -412,7 +419,9 @@ int ShaHash(const byte* data, word32 len, byte* hash) ShaFinal(sha, hash); } - DESTROY_VAR(sha); +#ifdef CYASSL_SMALL_STACK + XFREE(sha, NULL, DYNAMIC_TYPE_TMP_BUFFER); +#endif return ret; } diff --git a/ctaocrypt/src/sha256.c b/ctaocrypt/src/sha256.c index 5aed4b325..a90fb19b5 100644 --- a/ctaocrypt/src/sha256.c +++ b/ctaocrypt/src/sha256.c @@ -288,10 +288,17 @@ int Sha256Final(Sha256* sha256, byte* hash) int Sha256Hash(const byte* data, word32 len, byte* hash) { int ret = 0; - DECLARE_VAR(Sha256, sha256); +#ifdef CYASSL_SMALL_STACK + Sha256* sha256; +#else + Sha256 sha256[1]; +#endif - if (!CREATE_VAR(Sha256, sha256)) +#ifdef CYASSL_SMALL_STACK + sha256 = (Sha256*)XMALLOC(sizeof(Sha256), NULL, DYNAMIC_TYPE_TMP_BUFFER); + if (sha256 == NULL) return MEMORY_E; +#endif if ((ret = InitSha256(sha256)) != 0) { CYASSL_MSG("InitSha256 failed"); @@ -303,7 +310,9 @@ int Sha256Hash(const byte* data, word32 len, byte* hash) CYASSL_MSG("Sha256Final failed"); } - DESTROY_VAR(sha256); +#ifdef CYASSL_SMALL_STACK + XFREE(sha256, NULL, DYNAMIC_TYPE_TMP_BUFFER); +#endif return ret; } diff --git a/ctaocrypt/src/sha512.c b/ctaocrypt/src/sha512.c index df3ddcda5..40086949b 100644 --- a/ctaocrypt/src/sha512.c +++ b/ctaocrypt/src/sha512.c @@ -301,10 +301,17 @@ int Sha512Final(Sha512* sha512, byte* hash) int Sha512Hash(const byte* data, word32 len, byte* hash) { int ret = 0; - DECLARE_VAR(Sha512, sha512); - - if (!CREATE_VAR(Sha512, sha512)) +#ifdef CYASSL_SMALL_STACK + Sha512* sha512; +#else + Sha512 sha512[1]; +#endif + +#ifdef CYASSL_SMALL_STACK + sha512 = (Sha512*)XMALLOC(sizeof(Sha512), NULL, DYNAMIC_TYPE_TMP_BUFFER); + if (sha512 == NULL) return MEMORY_E; +#endif if ((ret = InitSha512(sha512)) != 0) { CYASSL_MSG("InitSha512 failed"); @@ -316,7 +323,9 @@ int Sha512Hash(const byte* data, word32 len, byte* hash) CYASSL_MSG("Sha512Final failed"); } - DESTROY_VAR(sha512); +#ifdef CYASSL_SMALL_STACK + XFREE(sha512, NULL, DYNAMIC_TYPE_TMP_BUFFER); +#endif return ret; } @@ -499,10 +508,17 @@ int Sha384Final(Sha384* sha384, byte* hash) int Sha384Hash(const byte* data, word32 len, byte* hash) { int ret = 0; - DECLARE_VAR(Sha384, sha384); +#ifdef CYASSL_SMALL_STACK + Sha384* sha384; +#else + Sha384 sha384[1]; +#endif - if (!CREATE_VAR(Sha384, sha384)) +#ifdef CYASSL_SMALL_STACK + sha384 = (Sha384*)XMALLOC(sizeof(Sha384), NULL, DYNAMIC_TYPE_TMP_BUFFER); + if (sha384 == NULL) return MEMORY_E; +#endif if ((ret = InitSha384(sha384)) != 0) { CYASSL_MSG("InitSha384 failed"); @@ -514,7 +530,9 @@ int Sha384Hash(const byte* data, word32 len, byte* hash) CYASSL_MSG("Sha384Final failed"); } - DESTROY_VAR(sha384); +#ifdef CYASSL_SMALL_STACK + XFREE(sha384, NULL, DYNAMIC_TYPE_TMP_BUFFER); +#endif return ret; } From f8cf3bf853e372293ca818646c930e1e2defd334 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mois=C3=A9s=20Guimar=C3=A3es?= Date: Mon, 7 Jul 2014 22:56:13 -0300 Subject: [PATCH 2/7] remove stack reduction macros --- ctaocrypt/src/asn.c | 252 +++++++++++++++++++++++++++--------- cyassl/ctaocrypt/settings.h | 19 --- 2 files changed, 191 insertions(+), 80 deletions(-) diff --git a/ctaocrypt/src/asn.c b/ctaocrypt/src/asn.c index 2b83c90f4..685557bdc 100644 --- a/ctaocrypt/src/asn.c +++ b/ctaocrypt/src/asn.c @@ -2846,10 +2846,17 @@ static int ConfirmSignature(const byte* buf, word32 bufSz, void* heap) { int typeH = 0, digestSz = 0, ret = 0; - DECLARE_ARRAY(byte, digest, MAX_DIGEST_SIZE); +#ifdef CYASSL_SMALL_STACK + byte* digest; +#else + byte digest[MAX_DIGEST_SIZE]; +#endif - if (!CREATE_ARRAY(byte, digest, MAX_DIGEST_SIZE)) +#ifdef CYASSL_SMALL_STACK + digest = (byte*)XMALLOC(MAX_DIGEST_SIZE, NULL, DYNAMIC_TYPE_TMP_BUFFER); + if (digest == NULL) return 0; /* not confirmed */ +#endif (void)key; (void)keySz; @@ -2916,7 +2923,9 @@ static int ConfirmSignature(const byte* buf, word32 bufSz, } if (typeH == 0) { - DESTROY_ARRAY(digest); +#ifdef CYASSL_SMALL_STACK + XFREE(digest, NULL, DYNAMIC_TYPE_TMP_BUFFER); +#endif return 0; /* not confirmed */ } @@ -2927,30 +2936,55 @@ static int ConfirmSignature(const byte* buf, word32 bufSz, word32 idx = 0; int encodedSigSz, verifySz; byte* out; - DECLARE_VAR(RsaKey, pubKey); - DECLARE_ARRAY(byte, plain, MAX_ENCODED_SIG_SZ); - DECLARE_ARRAY(byte, encodedSig, MAX_ENCODED_SIG_SZ); +#ifdef CYASSL_SMALL_STACK + RsaKey* pubKey; + byte* plain; + byte* encodedSig; +#else + RsaKey pubKey[1]; + byte plain[MAX_ENCODED_SIG_SZ]; + byte encodedSig[MAX_ENCODED_SIG_SZ]; +#endif + +#ifdef CYASSL_SMALL_STACK + pubKey = (RsaKey*)XMALLOC(sizeof(RsaKey), NULL, + DYNAMIC_TYPE_TMP_BUFFER); + plain = (byte*)XMALLOC(MAX_ENCODED_SIG_SZ, NULL, + DYNAMIC_TYPE_TMP_BUFFER); + encodedSig = (byte*)XMALLOC(MAX_ENCODED_SIG_SZ, NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (pubKey == NULL || plain == NULL || encodedSig == NULL) { + CYASSL_MSG("Failed to allocate memory at ConfirmSignature"); + + if (pubKey) + XFREE(pubKey, NULL, DYNAMIC_TYPE_TMP_BUFFER); + if (plain) + XFREE(plain, NULL, DYNAMIC_TYPE_TMP_BUFFER); + if (encodedSig) + XFREE(encodedSig, NULL, DYNAMIC_TYPE_TMP_BUFFER); + + break; /* not confirmed */ + } +#endif + if (sigSz > MAX_ENCODED_SIG_SZ) { CYASSL_MSG("Verify Signautre is too big"); } - else if (!CREATE_VAR(RsaKey, pubKey)) { - CYASSL_MSG("Failed to allocate pubKey"); - } else if (InitRsaKey(pubKey, heap) != 0) { CYASSL_MSG("InitRsaKey failed"); } else if (RsaPublicKeyDecode(key, &idx, pubKey, keySz) < 0) { CYASSL_MSG("ASN Key decode error RSA"); } - else if (CREATE_ARRAY(byte, plain, MAX_ENCODED_SIG_SZ)) { + else { XMEMCPY(plain, sig, sigSz); if ((verifySz = RsaSSL_VerifyInline(plain, sigSz, &out, pubKey)) < 0) { CYASSL_MSG("Rsa SSL verify error"); } - else if (CREATE_ARRAY(byte, encodedSig, MAX_ENCODED_SIG_SZ)) { + else { /* make sure we're right justified */ encodedSigSz = EncodeSignature(encodedSig, digest, digestSz, typeH); @@ -2986,16 +3020,17 @@ static int ConfirmSignature(const byte* buf, word32 bufSz, } #endif /* CYASSL_DEBUG_ENCODING */ - DESTROY_ARRAY(encodedSig); } - DESTROY_ARRAY(plain); } - if (pubKey) { - FreeRsaKey(pubKey); - DESTROY_VAR(pubKey); - } + FreeRsaKey(pubKey); + +#ifdef CYASSL_SMALL_STACK + XFREE(pubKey, NULL, DYNAMIC_TYPE_TMP_BUFFER); + XFREE(plain, NULL, DYNAMIC_TYPE_TMP_BUFFER); + XFREE(encodedSig, NULL, DYNAMIC_TYPE_TMP_BUFFER); +#endif } #endif /* NO_RSA */ @@ -3003,12 +3038,22 @@ static int ConfirmSignature(const byte* buf, word32 bufSz, case ECDSAk: { int verify = 0; - DECLARE_VAR(ecc_key, pubKey); +#ifdef CYASSL_SMALL_STACK + ecc_key* pubKey; +#else + ecc_key pubKey[1]; +#endif - if (!CREATE_VAR(ecc_key, pubKey)) { +#ifdef CYASSL_SMALL_STACK + pubKey = (ecc_key*)XMALLOC(sizeof(ecc_key), NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (pubKey == NULL) { CYASSL_MSG("Failed to allocate pubKey"); + break; /* not confirmed */ } - else if (ecc_import_x963(key, keySz, pubKey) < 0) { +#endif + + if (ecc_import_x963(key, keySz, pubKey) < 0) { CYASSL_MSG("ASN Key import error ECC"); } else if (ecc_verify_hash(sig, sigSz, digest, digestSz, &verify, @@ -3022,7 +3067,9 @@ static int ConfirmSignature(const byte* buf, word32 bufSz, if (pubKey) { ecc_free(pubKey); - DESTROY_VAR(pubKey); +#ifdef CYASSL_SMALL_STACK + XFREE(pubKey, NULL, DYNAMIC_TYPE_TMP_BUFFER); +#endif } } #endif /* HAVE_ECC */ @@ -3030,7 +3077,10 @@ static int ConfirmSignature(const byte* buf, word32 bufSz, CYASSL_MSG("Verify Key type unknown"); } - DESTROY_ARRAY(digest); +#ifdef CYASSL_SMALL_STACK + XFREE(digest, NULL, DYNAMIC_TYPE_TMP_BUFFER); +#endif + return ret; } @@ -5507,7 +5557,11 @@ static int MakeSignature(const byte* buffer, int sz, byte* sig, int sigSz, { int encSigSz, digestSz, typeH = 0, ret = 0; byte digest[SHA256_DIGEST_SIZE]; /* max size */ - DECLARE_ARRAY(byte, encSig, MAX_ENCODED_DIG_SZ + MAX_ALGO_SZ + MAX_SEQ_SZ); +#ifdef CYASSL_SMALL_STACK + byte* encSig; +#else + byte encSig[MAX_ENCODED_DIG_SZ + MAX_ALGO_SZ + MAX_SEQ_SZ]; +#endif (void)digest; (void)digestSz; @@ -5558,31 +5612,36 @@ static int MakeSignature(const byte* buffer, int sz, byte* sig, int sigSz, if (ret != 0) return ret; - if (!CREATE_ARRAY(byte, encSig, MAX_ENCODED_DIG_SZ + - MAX_ALGO_SZ + MAX_SEQ_SZ)) { +#ifdef CYASSL_SMALL_STACK + encSig = (byte*)XMALLOC(MAX_ENCODED_DIG_SZ + MAX_ALGO_SZ + MAX_SEQ_SZ, + NULL, DYNAMIC_TYPE_TMP_BUFFER); + if (encSig == NULL) return MEMORY_E; - } +#endif + + ret = ALGO_ID_E; + #ifndef NO_RSA - else if (rsaKey) { + if (rsaKey) { /* signature */ encSigSz = EncodeSignature(encSig, digest, digestSz, typeH); ret = RsaSSL_Sign(encSig, encSigSz, sig, sigSz, rsaKey, rng); } #endif + #ifdef HAVE_ECC - else if (eccKey) { + if (!rsaKey && eccKey) { word32 outSz = sigSz; ret = ecc_sign_hash(digest, digestSz, sig, &outSz, rng, eccKey); if (ret == 0) ret = outSz; } -#endif /* HAVE_ECC */ - else { - ret = ALGO_ID_E; - } +#endif - DESTROY_ARRAY(encSig); +#ifdef CYASSL_SMALL_STACK + XFREE(encSig, NULL, DYNAMIC_TYPE_TMP_BUFFER); +#endif return ret; } @@ -5622,12 +5681,19 @@ static int MakeAnyCert(Cert* cert, byte* derBuffer, word32 derSz, const byte* ntruKey, word16 ntruSz) { int ret; - DECLARE_VAR(DerCert, der); +#ifdef CYASSL_SMALL_STACK + DerCert* der; +#else + DerCert der[1]; +#endif cert->keyType = eccKey ? ECC_KEY : (rsaKey ? RSA_KEY : NTRU_KEY); - if (!CREATE_VAR(DerCert, der)) +#ifdef CYASSL_SMALL_STACK + der = (DerCert*)XMALLOC(sizeof(DerCert), NULL, DYNAMIC_TYPE_TMP_BUFFER); + if (der == NULL) return MEMORY_E; +#endif ret = EncodeCert(cert, der, rsaKey, eccKey, rng, ntruKey, ntruSz); @@ -5638,7 +5704,9 @@ static int MakeAnyCert(Cert* cert, byte* derBuffer, word32 derSz, ret = cert->bodySz = WriteCertBody(der, derBuffer); } - DESTROY_VAR(der); +#ifdef CYASSL_SMALL_STACK + XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER); +#endif return ret; } @@ -5837,12 +5905,19 @@ int MakeCertReq(Cert* cert, byte* derBuffer, word32 derSz, RsaKey* rsaKey, ecc_key* eccKey) { int ret; - DECLARE_VAR(DerCert, der); +#ifdef CYASSL_SMALL_STACK + DerCert* der; +#else + DerCert der[1]; +#endif cert->keyType = eccKey ? ECC_KEY : RSA_KEY; - if (!CREATE_VAR(DerCert, der)) +#ifdef CYASSL_SMALL_STACK + der = (DerCert*)XMALLOC(sizeof(DerCert), NULL, DYNAMIC_TYPE_TMP_BUFFER); + if (der == NULL) return MEMORY_E; +#endif ret = EncodeCertReq(cert, der, rsaKey, eccKey); @@ -5853,7 +5928,9 @@ int MakeCertReq(Cert* cert, byte* derBuffer, word32 derSz, ret = cert->bodySz = WriteCertReqBody(der, derBuffer); } - DESTROY_VAR(der); +#ifdef CYASSL_SMALL_STACK + XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER); +#endif return ret; } @@ -5865,13 +5942,20 @@ int SignCert(int requestSz, int sType, byte* buffer, word32 buffSz, RsaKey* rsaKey, ecc_key* eccKey, RNG* rng) { int sigSz; - DECLARE_ARRAY(byte, sig, MAX_ENCODED_SIG_SZ); +#ifdef CYASSL_SMALL_STACK + byte* sig; +#else + byte sig[MAX_ENCODED_SIG_SZ]; +#endif if (requestSz < 0) return requestSz; - if (!CREATE_ARRAY(byte, sig, MAX_ENCODED_SIG_SZ)) +#ifdef CYASSL_SMALL_STACK + sig = (byte*)XMALLOC(MAX_ENCODED_SIG_SZ, NULL, DYNAMIC_TYPE_TMP_BUFFER); + if (sig == NULL) return MEMORY_E; +#endif sigSz = MakeSignature(buffer, requestSz, sig, MAX_ENCODED_SIG_SZ, rsaKey, eccKey, rng, sType); @@ -5883,7 +5967,9 @@ int SignCert(int requestSz, int sType, byte* buffer, word32 buffSz, sigSz = AddSignature(buffer, requestSz, sig, sigSz, sType); } - DESTROY_ARRAY(sig); +#ifdef CYASSL_SMALL_STACK + XFREE(sig, NULL, DYNAMIC_TYPE_TMP_BUFFER); +#endif return sigSz; } @@ -5906,13 +5992,21 @@ int MakeSelfCert(Cert* cert, byte* buffer, word32 buffSz, RsaKey* key, RNG* rng) static int SetAltNamesFromCert(Cert* cert, const byte* der, int derSz) { int ret; - DECLARE_VAR(DecodedCert, decoded); +#ifdef CYASSL_SMALL_STACK + DecodedCert* decoded; +#else + DecodedCert decoded[1]; +#endif if (derSz < 0) return derSz; - if (!CREATE_VAR(DecodedCert, decoded)) +#ifdef CYASSL_SMALL_STACK + decoded = (DecodedCert*)XMALLOC(sizeof(DecodedCert), NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (decoded == NULL) return MEMORY_E; +#endif InitDecodedCert(decoded, (byte*)der, derSz, 0); ret = ParseCertRelative(decoded, CA_TYPE, NO_VERIFY, 0); @@ -5981,7 +6075,9 @@ static int SetAltNamesFromCert(Cert* cert, const byte* der, int derSz) } FreeDecodedCert(decoded); - DESTROY_VAR(decoded); +#ifdef CYASSL_SMALL_STACK + XFREE(decoded, NULL, DYNAMIC_TYPE_TMP_BUFFER); +#endif return ret < 0 ? ret : 0; } @@ -5991,14 +6087,22 @@ static int SetAltNamesFromCert(Cert* cert, const byte* der, int derSz) static int SetDatesFromCert(Cert* cert, const byte* der, int derSz) { int ret; - DECLARE_VAR(DecodedCert, decoded); +#ifdef CYASSL_SMALL_STACK + DecodedCert* decoded; +#else + DecodedCert decoded[1]; +#endif CYASSL_ENTER("SetDatesFromCert"); if (derSz < 0) return derSz; - if (!CREATE_VAR(DecodedCert, decoded)) +#ifdef CYASSL_SMALL_STACK + decoded = (DecodedCert*)XMALLOC(sizeof(DecodedCert), NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (decoded == NULL) return MEMORY_E; +#endif InitDecodedCert(decoded, (byte*)der, derSz, 0); ret = ParseCertRelative(decoded, CA_TYPE, NO_VERIFY, 0); @@ -6024,7 +6128,10 @@ static int SetDatesFromCert(Cert* cert, const byte* der, int derSz) } FreeDecodedCert(decoded); - DESTROY_VAR(decoded); + +#ifdef CYASSL_SMALL_STACK + XFREE(decoded, NULL, DYNAMIC_TYPE_TMP_BUFFER); +#endif return ret < 0 ? ret : 0; } @@ -6037,13 +6144,21 @@ static int SetDatesFromCert(Cert* cert, const byte* der, int derSz) static int SetNameFromCert(CertName* cn, const byte* der, int derSz) { int ret, sz; - DECLARE_VAR(DecodedCert, decoded); +#ifdef CYASSL_SMALL_STACK + DecodedCert* decoded; +#else + DecodedCert decoded[1]; +#endif if (derSz < 0) return derSz; - if (!CREATE_VAR(DecodedCert, decoded)) +#ifdef CYASSL_SMALL_STACK + decoded = (DecodedCert*)XMALLOC(sizeof(DecodedCert), NULL, + DYNAMIC_TYPE_TMP_BUFFER); + if (decoded == NULL) return MEMORY_E; +#endif InitDecodedCert(decoded, (byte*)der, derSz, 0); ret = ParseCertRelative(decoded, CA_TYPE, NO_VERIFY, 0); @@ -6110,7 +6225,10 @@ static int SetNameFromCert(CertName* cn, const byte* der, int derSz) } FreeDecodedCert(decoded); - DESTROY_VAR(decoded); + +#ifdef CYASSL_SMALL_STACK + XFREE(decoded, NULL, DYNAMIC_TYPE_TMP_BUFFER); +#endif return ret < 0 ? ret : 0; } @@ -6297,9 +6415,13 @@ int EccPrivateKeyDecode(const byte* input, word32* inOutIdx, ecc_key* key, int privSz, pubSz; byte b; int ret = 0; - DECLARE_ARRAY(byte, priv, ECC_MAXSIZE); - DECLARE_ARRAY(byte, pub, ECC_MAXSIZE * 2 + 1); /* public key has two parts - plus header */ +#ifdef CYASSL_SMALL_STACK + byte* priv; + byte* pub; +#else + byte priv[ECC_MAXSIZE]; + byte pub[ECC_MAXSIZE * 2 + 1]; /* public key has two parts plus header */ +#endif if (input == NULL || inOutIdx == NULL || key == NULL || inSz == 0) return BAD_FUNC_ARG; @@ -6320,10 +6442,19 @@ int EccPrivateKeyDecode(const byte* input, word32* inOutIdx, ecc_key* key, if (GetLength(input, inOutIdx, &length, inSz) < 0) return ASN_PARSE_E; - /* priv key */ - if (!CREATE_ARRAY(byte, priv, ECC_MAXSIZE)) +#ifdef CYASSL_SMALL_STACK + priv = (byte*)XMALLOC(ECC_MAXSIZE, NULL, DYNAMIC_TYPE_TMP_BUFFER); + if (priv == NULL) return MEMORY_E; + + pub = (byte*)XMALLOC(ECC_MAXSIZE * 2 + 1, NULL, DYNAMIC_TYPE_TMP_BUFFER); + if (pub == NULL) { + XFREE(priv, NULL, DYNAMIC_TYPE_TMP_BUFFER); + return MEMORY_E; + } +#endif + /* priv key */ privSz = length; XMEMCPY(priv, &input[*inOutIdx], privSz); *inOutIdx += length; @@ -6386,8 +6517,6 @@ int EccPrivateKeyDecode(const byte* input, word32* inOutIdx, ecc_key* key, if (b != 0x00) { ret = ASN_EXPECT_0_E; } - else if (!CREATE_ARRAY(byte, pub, ECC_MAXSIZE * 2 + 1)) - ret = MEMORY_E; else { /* pub key */ pubSz = length - 1; /* null prefix */ @@ -6396,14 +6525,15 @@ int EccPrivateKeyDecode(const byte* input, word32* inOutIdx, ecc_key* key, *inOutIdx += length; ret = ecc_import_private_key(priv, privSz, pub, pubSz, key); - - DESTROY_ARRAY(pub); } } } } - DESTROY_ARRAY(priv); +#ifdef CYASSL_SMALL_STACK + XFREE(priv, NULL, DYNAMIC_TYPE_TMP_BUFFER); + XFREE(pub, NULL, DYNAMIC_TYPE_TMP_BUFFER); +#endif return ret; } diff --git a/cyassl/ctaocrypt/settings.h b/cyassl/ctaocrypt/settings.h index 5ec46a994..57bb8290e 100644 --- a/cyassl/ctaocrypt/settings.h +++ b/cyassl/ctaocrypt/settings.h @@ -254,25 +254,6 @@ #define XREALLOC yaXREALLOC #endif -#ifdef CYASSL_SMALL_STACK - #define DECLARE_ARRAY(type, var, size) \ - type* var = NULL - - #define CREATE_ARRAY(type, var, size) \ - (var = (type*)XMALLOC(sizeof(type) * size, NULL, \ - DYNAMIC_TYPE_TMP_BUFFER)) - - #define DESTROY_ARRAY(var) \ - XFREE(var, NULL, DYNAMIC_TYPE_TMP_BUFFER) -#else - #define DECLARE_ARRAY(type, var, size) type var[size] - #define CREATE_ARRAY(type, var, size) 1 - #define DESTROY_ARRAY(var) -#endif - -#define DECLARE_VAR(type, var) DECLARE_ARRAY(type, var, 1) -#define CREATE_VAR(type, var) CREATE_ARRAY(type, var, 1) -#define DESTROY_VAR(var) DESTROY_ARRAY(var) #ifdef FREERTOS #ifndef NO_WRITEV From d6f5f57452853c53ad8505f55349d9df0ac8c946 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mois=C3=A9s=20Guimar=C3=A3es?= Date: Tue, 8 Jul 2014 13:03:12 -0300 Subject: [PATCH 3/7] remove unnecessary check on pubKey --- ctaocrypt/src/asn.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/ctaocrypt/src/asn.c b/ctaocrypt/src/asn.c index 685557bdc..4bc5f9267 100644 --- a/ctaocrypt/src/asn.c +++ b/ctaocrypt/src/asn.c @@ -3064,13 +3064,11 @@ static int ConfirmSignature(const byte* buf, word32 bufSz, CYASSL_MSG("ECC Verify didn't match"); } else ret = 1; /* match */ - - if (pubKey) { - ecc_free(pubKey); + + ecc_free(pubKey); #ifdef CYASSL_SMALL_STACK - XFREE(pubKey, NULL, DYNAMIC_TYPE_TMP_BUFFER); + XFREE(pubKey, NULL, DYNAMIC_TYPE_TMP_BUFFER); #endif - } } #endif /* HAVE_ECC */ default: From 9ffc44a01f5029306c398612143a9075c93f205f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mois=C3=A9s=20Guimar=C3=A3es?= Date: Tue, 8 Jul 2014 13:41:42 -0300 Subject: [PATCH 4/7] ecc_free should be called only upon ecc_import_x963 success. --- ctaocrypt/src/asn.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/ctaocrypt/src/asn.c b/ctaocrypt/src/asn.c index 4bc5f9267..e7b4bd275 100644 --- a/ctaocrypt/src/asn.c +++ b/ctaocrypt/src/asn.c @@ -3056,16 +3056,18 @@ static int ConfirmSignature(const byte* buf, word32 bufSz, if (ecc_import_x963(key, keySz, pubKey) < 0) { CYASSL_MSG("ASN Key import error ECC"); } - else if (ecc_verify_hash(sig, sigSz, digest, digestSz, &verify, + else { + if (ecc_verify_hash(sig, sigSz, digest, digestSz, &verify, pubKey) != 0) { - CYASSL_MSG("ECC verify hash error"); - } - else if (1 != verify) { - CYASSL_MSG("ECC Verify didn't match"); - } else - ret = 1; /* match */ + CYASSL_MSG("ECC verify hash error"); + } + else if (1 != verify) { + CYASSL_MSG("ECC Verify didn't match"); + } else + ret = 1; /* match */ - ecc_free(pubKey); + ecc_free(pubKey); + } #ifdef CYASSL_SMALL_STACK XFREE(pubKey, NULL, DYNAMIC_TYPE_TMP_BUFFER); #endif From 1f6dcd94baf0f8410e4f95f1eaf0e1fec06925ec Mon Sep 17 00:00:00 2001 From: Shane Israel Date: Wed, 9 Jul 2014 16:10:10 -0600 Subject: [PATCH 5/7] Fixed NTRU param issue in asn.c and added an NTRU keygen benchmark --- ctaocrypt/benchmark/benchmark.c | 79 +++++++++++++++++++++++++++++++++ ctaocrypt/src/asn.c | 5 ++- 2 files changed, 82 insertions(+), 2 deletions(-) diff --git a/ctaocrypt/benchmark/benchmark.c b/ctaocrypt/benchmark/benchmark.c index 22e6450bf..4e4f6ee91 100644 --- a/ctaocrypt/benchmark/benchmark.c +++ b/ctaocrypt/benchmark/benchmark.c @@ -51,6 +51,9 @@ #include "cavium_common.h" #include "cavium_ioctl.h" #endif +#ifdef HAVE_NTRU + #include "ntru_crypto.h" +#endif #if defined(CYASSL_MDK_ARM) extern FILE * CyaSSL_fopen(const char *fname, const char *mode) ; @@ -101,6 +104,9 @@ void bench_dh(void); void bench_eccKeyGen(void); void bench_eccKeyAgree(void); #endif +#ifdef HAVE_NTRU +void bench_ntruKeyGen(void); +#endif double current_time(int); @@ -220,6 +226,9 @@ int benchmark_test(void *args) #if defined(CYASSL_KEY_GEN) && !defined(NO_RSA) bench_rsaKeyGen(); + #ifdef HAVE_NTRU + bench_ntruKeyGen(); + #endif #endif #ifdef HAVE_ECC @@ -1025,6 +1034,76 @@ void bench_rsaKeyGen(void) " iterations\n", milliEach, genTimes); } #endif /* CYASSL_KEY_GEN */ +#ifdef HAVE_NTRU +byte GetEntropy(ENTROPY_CMD cmd, byte* out); + +byte GetEntropy(ENTROPY_CMD cmd, byte* out) +{ + if (cmd == INIT) + return (InitRng(&rng) == 0) ? 1 : 0; + + if (out == NULL) + return 0; + + if (cmd == GET_BYTE_OF_ENTROPY) + return (RNG_GenerateBlock(&rng, out, 1) == 0) ? 1 : 0; + + if (cmd == GET_NUM_BYTES_PER_BYTE_OF_ENTROPY) { + *out = 1; + return 1; + } + + return 0; +} +void bench_ntruKeyGen(void) +{ + double start, total, each, milliEach; + int i; + + byte public_key[5951]; /* 2048 key equivalent to rsa */ + word16 public_key_len; + byte private_key[5951]; + word16 private_key_len; + + DRBG_HANDLE drbg; + static uint8_t const pers_str[] = { + 'C', 'y', 'a', 'S', 'S', 'L', ' ', 't', 'e', 's', 't' + }; + + word32 rc = ntru_crypto_drbg_instantiate(112, pers_str, sizeof(pers_str), GetEntropy, &drbg); + + if(rc != DRBG_OK) { + printf("NTRU drbg instantiate failed\n"); + return; + } + + start = current_time(1); + + for(i = 0; i < genTimes; i++) { + ntru_crypto_ntru_encrypt_keygen(drbg, NTRU_EES401EP2, + &public_key_len, NULL, &private_key_len, NULL); + ntru_crypto_ntru_encrypt_keygen(drbg, NTRU_EES401EP2, + &public_key_len, public_key, &private_key_len, private_key); + } + + total = current_time(0) - start; + + rc = ntru_crypto_drbg_uninstantiate(drbg); + + if (rc != NTRU_OK) { + printf("NTRU drbg uninstantiate failed\n"); + return; + } + + each = total / genTimes; + milliEach = each * 1000; + + printf("\n"); + printf("NTRU 2048 key generation %6.3f milliseconds, avg over %d" + " iterations\n", milliEach, genTimes); + +} +#endif #ifdef HAVE_ECC void bench_eccKeyGen(void) diff --git a/ctaocrypt/src/asn.c b/ctaocrypt/src/asn.c index e7b4bd275..6cd211722 100644 --- a/ctaocrypt/src/asn.c +++ b/ctaocrypt/src/asn.c @@ -1667,9 +1667,10 @@ static int GetKey(DecodedCert* cert) #else byte keyBlob[MAX_NTRU_KEY_SZ]; #endif + uint32_t remaining = (uint32_t)cert->maxIdx - cert->srcIdx; rc = ntru_crypto_ntru_encrypt_subjectPublicKeyInfo2PublicKey(key, - &keyLen, NULL, &next); + &keyLen, NULL, &next, &remaining); if (rc != NTRU_OK) return ASN_NTRU_KEY_E; @@ -1684,7 +1685,7 @@ static int GetKey(DecodedCert* cert) #endif rc = ntru_crypto_ntru_encrypt_subjectPublicKeyInfo2PublicKey(key, - &keyLen, keyBlob, &next); + &keyLen, keyBlob, &next, &remaining); if (rc != NTRU_OK) { #ifdef CYASSL_SMALL_STACK XFREE(keyBlob, NULL, DYNAMIC_TYPE_TMP_BUFFER); From 8462ed06535e255c8731f94c809c6847059f30b9 Mon Sep 17 00:00:00 2001 From: toddouska Date: Wed, 9 Jul 2014 15:47:37 -0700 Subject: [PATCH 6/7] fix issue #94, keygen w/ normal math --- ctaocrypt/src/integer.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ctaocrypt/src/integer.c b/ctaocrypt/src/integer.c index 56598f451..b39a36f9f 100644 --- a/ctaocrypt/src/integer.c +++ b/ctaocrypt/src/integer.c @@ -3765,7 +3765,7 @@ int mp_sqrmod (mp_int * a, mp_int * b, mp_int * c) #endif -#if defined(HAVE_ECC) || !defined(NO_PWDBASED) || defined(CYASSL_SNIFFER) || defined(CYASSL_HAVE_WOLFSCEP) +#if defined(HAVE_ECC) || !defined(NO_PWDBASED) || defined(CYASSL_SNIFFER) || defined(CYASSL_HAVE_WOLFSCEP) || defined(CYASSL_KEY_GEN) /* single digit addition */ int mp_add_d (mp_int* a, mp_digit b, mp_int* c) From 1c7eb610178030f34891e64b2944ff534b03c40e Mon Sep 17 00:00:00 2001 From: toddouska Date: Wed, 9 Jul 2014 16:18:55 -0700 Subject: [PATCH 7/7] ntru keygen is indepedent of additional cyassl keygen, use cyassl types, correct ntru benchmark output --- ctaocrypt/benchmark/benchmark.c | 17 ++++++++--------- ctaocrypt/src/asn.c | 4 +--- 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/ctaocrypt/benchmark/benchmark.c b/ctaocrypt/benchmark/benchmark.c index 4e4f6ee91..604782480 100644 --- a/ctaocrypt/benchmark/benchmark.c +++ b/ctaocrypt/benchmark/benchmark.c @@ -226,9 +226,10 @@ int benchmark_test(void *args) #if defined(CYASSL_KEY_GEN) && !defined(NO_RSA) bench_rsaKeyGen(); - #ifdef HAVE_NTRU - bench_ntruKeyGen(); - #endif +#endif + +#ifdef HAVE_NTRU + bench_ntruKeyGen(); #endif #ifdef HAVE_ECC @@ -1063,7 +1064,7 @@ void bench_ntruKeyGen(void) byte public_key[5951]; /* 2048 key equivalent to rsa */ word16 public_key_len; byte private_key[5951]; - word16 private_key_len; + word16 private_key_len = sizeof(private_key); DRBG_HANDLE drbg; static uint8_t const pers_str[] = { @@ -1080,10 +1081,8 @@ void bench_ntruKeyGen(void) start = current_time(1); for(i = 0; i < genTimes; i++) { - ntru_crypto_ntru_encrypt_keygen(drbg, NTRU_EES401EP2, - &public_key_len, NULL, &private_key_len, NULL); - ntru_crypto_ntru_encrypt_keygen(drbg, NTRU_EES401EP2, - &public_key_len, public_key, &private_key_len, private_key); + ntru_crypto_ntru_encrypt_keygen(drbg, NTRU_EES401EP2, &public_key_len, + public_key, &private_key_len, private_key); } total = current_time(0) - start; @@ -1099,7 +1098,7 @@ void bench_ntruKeyGen(void) milliEach = each * 1000; printf("\n"); - printf("NTRU 2048 key generation %6.3f milliseconds, avg over %d" + printf("NTRU 112 key generation %6.3f milliseconds, avg over %d" " iterations\n", milliEach, genTimes); } diff --git a/ctaocrypt/src/asn.c b/ctaocrypt/src/asn.c index 6cd211722..6d2d962e1 100644 --- a/ctaocrypt/src/asn.c +++ b/ctaocrypt/src/asn.c @@ -1662,16 +1662,14 @@ static int GetKey(DecodedCert* cert) byte* next = (byte*)key; word16 keyLen; word32 rc; + word32 remaining = cert->maxIdx - cert->srcIdx; #ifdef CYASSL_SMALL_STACK byte* keyBlob = NULL; #else byte keyBlob[MAX_NTRU_KEY_SZ]; #endif - uint32_t remaining = (uint32_t)cert->maxIdx - cert->srcIdx; - rc = ntru_crypto_ntru_encrypt_subjectPublicKeyInfo2PublicKey(key, &keyLen, NULL, &next, &remaining); - if (rc != NTRU_OK) return ASN_NTRU_KEY_E; if (keyLen > MAX_NTRU_KEY_SZ)