diff --git a/certs/test/cert-ext-ia.cfg b/certs/test/cert-ext-ia.cfg new file mode 100644 index 000000000..8721916b3 --- /dev/null +++ b/certs/test/cert-ext-ia.cfg @@ -0,0 +1,18 @@ +[ req ] +distinguished_name = req_distinguished_name +prompt = no +x509_extensions = v3_ca + +[ req_distinguished_name ] +C = AU +ST = Queensland +L = Brisbane +O = wolfSSL Inc +OU = Engineering +CN = www.wolfssl.com +emailAddress = support@www.wolfsssl.com + +[ v3_ca ] +inhibitAnyPolicy = critical,1 +nsComment = "Testing inhibit any" + diff --git a/certs/test/cert-ext-ia.der b/certs/test/cert-ext-ia.der new file mode 100644 index 000000000..73ea7c0a8 Binary files /dev/null and b/certs/test/cert-ext-ia.der differ diff --git a/certs/test/cert-ext-nc.cfg b/certs/test/cert-ext-nc.cfg new file mode 100644 index 000000000..b27f3f4fe --- /dev/null +++ b/certs/test/cert-ext-nc.cfg @@ -0,0 +1,18 @@ +[ req ] +distinguished_name = req_distinguished_name +prompt = no +x509_extensions = v3_ca + +[ req_distinguished_name ] +C = AU +ST = Queensland +L = Brisbane +O = wolfSSL Inc +OU = Engineering +CN = www.wolfssl.com +emailAddress = support@www.wolfsssl.com + +[ v3_ca ] +nameConstraints = critical,permitted;email:.wolfssl.com +nsComment = "Testing name constraints" + diff --git a/certs/test/cert-ext-nc.der b/certs/test/cert-ext-nc.der new file mode 100644 index 000000000..ff944476d Binary files /dev/null and b/certs/test/cert-ext-nc.der differ diff --git a/certs/test/cert-ext-ns.der b/certs/test/cert-ext-ns.der new file mode 100644 index 000000000..10cc65520 Binary files /dev/null and b/certs/test/cert-ext-ns.der differ diff --git a/certs/test/gen-ext-certs.sh b/certs/test/gen-ext-certs.sh new file mode 100644 index 000000000..20b61e9c9 --- /dev/null +++ b/certs/test/gen-ext-certs.sh @@ -0,0 +1,69 @@ +#!/bin/sh + +TMP="/tmp/`basename $0`" + +gen_cert() { + openssl req -x509 -keyform DER -key certs/server-key.der \ + -outform DER -out $OUT -config $CONFIG \ + >$TMP 2>&1 + + if [ "$?" = "0" -a -f $OUT ]; then + echo "Created: $OUT" + else + cat $TMP + echo "Failed: $OUT" + fi + + rm $TMP +} + +OUT=certs/test/cert-ext-nc.der +KEYFILE=certs/test/cert-ext-nc-key.der +CONFIG=certs/test/cert-ext-nc.cfg +tee >$CONFIG <$CONFIG <md5); + ret = 0; #endif break; case WC_HASH_TYPE_SHA: #ifndef NO_SHA ret = wc_InitSha(&hash->sha); - if (ret != 0) - return ret; #endif break; case WC_HASH_TYPE_SHA224: #ifdef WOLFSSL_SHA224 ret = wc_InitSha224(&hash->sha224); - if (ret != 0) - return ret; #endif break; case WC_HASH_TYPE_SHA256: #ifndef NO_SHA256 ret = wc_InitSha256(&hash->sha256); - if (ret != 0) - return ret; #endif break; case WC_HASH_TYPE_SHA384: #ifdef WOLFSSL_SHA384 ret = wc_InitSha384(&hash->sha384); - if (ret != 0) - return ret; #endif break; case WC_HASH_TYPE_SHA512: #ifdef WOLFSSL_SHA512 ret = wc_InitSha512(&hash->sha512); - if (ret != 0) - return ret; #endif break; @@ -280,7 +271,7 @@ int wc_HashInit(wc_HashAlg* hash, enum wc_HashType type) case WC_HASH_TYPE_MD4: case WC_HASH_TYPE_NONE: default: - return BAD_FUNC_ARG; + ret = BAD_FUNC_ARG; }; return ret; @@ -298,6 +289,7 @@ int wc_HashUpdate(wc_HashAlg* hash, enum wc_HashType type, const byte* data, case WC_HASH_TYPE_MD5: #ifndef NO_MD5 wc_Md5Update(&hash->md5, data, dataSz); + ret = 0; #endif break; case WC_HASH_TYPE_SHA: @@ -310,29 +302,21 @@ int wc_HashUpdate(wc_HashAlg* hash, enum wc_HashType type, const byte* data, case WC_HASH_TYPE_SHA224: #ifdef WOLFSSL_SHA224 ret = wc_Sha224Update(&hash->sha224, data, dataSz); - if (ret != 0) - return ret; #endif break; case WC_HASH_TYPE_SHA256: #ifndef NO_SHA256 ret = wc_Sha256Update(&hash->sha256, data, dataSz); - if (ret != 0) - return ret; #endif break; case WC_HASH_TYPE_SHA384: #ifdef WOLFSSL_SHA384 ret = wc_Sha384Update(&hash->sha384, data, dataSz); - if (ret != 0) - return ret; #endif break; case WC_HASH_TYPE_SHA512: #ifdef WOLFSSL_SHA512 ret = wc_Sha512Update(&hash->sha512, data, dataSz); - if (ret != 0) - return ret; #endif break; @@ -342,7 +326,7 @@ int wc_HashUpdate(wc_HashAlg* hash, enum wc_HashType type, const byte* data, case WC_HASH_TYPE_MD4: case WC_HASH_TYPE_NONE: default: - return BAD_FUNC_ARG; + ret = BAD_FUNC_ARG; }; return ret; @@ -359,41 +343,32 @@ int wc_HashFinal(wc_HashAlg* hash, enum wc_HashType type, byte* out) case WC_HASH_TYPE_MD5: #ifndef NO_MD5 wc_Md5Final(&hash->md5, out); + ret = 0; #endif break; case WC_HASH_TYPE_SHA: #ifndef NO_SHA ret = wc_ShaFinal(&hash->sha, out); - if (ret != 0) - return ret; #endif break; case WC_HASH_TYPE_SHA224: #ifdef WOLFSSL_SHA224 ret = wc_Sha224Final(&hash->sha224, out); - if (ret != 0) - return ret; #endif break; case WC_HASH_TYPE_SHA256: #ifndef NO_SHA256 ret = wc_Sha256Final(&hash->sha256, out); - if (ret != 0) - return ret; #endif break; case WC_HASH_TYPE_SHA384: #ifdef WOLFSSL_SHA384 ret = wc_Sha384Final(&hash->sha384, out); - if (ret != 0) - return ret; #endif break; case WC_HASH_TYPE_SHA512: #ifdef WOLFSSL_SHA512 ret = wc_Sha512Final(&hash->sha512, out); - if (ret != 0) - return ret; #endif break; @@ -403,10 +378,10 @@ int wc_HashFinal(wc_HashAlg* hash, enum wc_HashType type, byte* out) case WC_HASH_TYPE_MD4: case WC_HASH_TYPE_NONE: default: - return BAD_FUNC_ARG; + ret = BAD_FUNC_ARG; }; - return 0; + return ret; } diff --git a/wolfcrypt/src/tfm.c b/wolfcrypt/src/tfm.c index f39728546..7fba9c64c 100644 --- a/wolfcrypt/src/tfm.c +++ b/wolfcrypt/src/tfm.c @@ -2043,26 +2043,26 @@ int fp_leading_bit(fp_int *a) void fp_lshd(fp_int *a, int x) { - int y; + int y; - /* move up and truncate as required */ - y = MIN(a->used + x - 1, (int)(FP_SIZE-1)); + /* move up and truncate as required */ + y = MIN(a->used + x - 1, (int)(FP_SIZE-1)); - /* store new size */ - a->used = y + 1; + /* store new size */ + a->used = y + 1; - /* move digits */ - for (; y >= x; y--) { - a->dp[y] = a->dp[y-x]; - } + /* move digits */ + for (; y >= x; y--) { + a->dp[y] = a->dp[y-x]; + } - /* zero lower digits */ - for (; y >= 0; y--) { - a->dp[y] = 0; - } + /* zero lower digits */ + for (; y >= 0; y--) { + a->dp[y] = 0; + } - /* clamp digits */ - fp_clamp(a); + /* clamp digits */ + fp_clamp(a); } @@ -2095,6 +2095,9 @@ void fp_rshb(fp_int *c, int x) /* set the carry to the carry bits of the current word found above */ r = rr; } + + /* clamp digits */ + fp_clamp(c); } diff --git a/wolfcrypt/src/wc_port.c b/wolfcrypt/src/wc_port.c index 23f002491..aa1dac6f8 100644 --- a/wolfcrypt/src/wc_port.c +++ b/wolfcrypt/src/wc_port.c @@ -264,7 +264,7 @@ wolfSSL_Mutex* wc_InitAndAllocMutex() { wolfSSL_Mutex* m = (wolfSSL_Mutex*) XMALLOC(sizeof(wolfSSL_Mutex), NULL, DYNAMIC_TYPE_MUTEX); - if(m && wc_InitMutex(m)) + if(m && wc_InitMutex(m) == 0) return m; XFREE(m, NULL, DYNAMIC_TYPE_MUTEX); m = NULL; diff --git a/wolfcrypt/test/test.c b/wolfcrypt/test/test.c index 2e0cebb5a..49be41814 100644 --- a/wolfcrypt/test/test.c +++ b/wolfcrypt/test/test.c @@ -39,6 +39,9 @@ #define HEAP_HINT NULL #endif /* WOLFSSL_STATIC_MEMORY */ +#include +#include + #ifdef WOLFSSL_TEST_CERT #include #else @@ -59,9 +62,11 @@ #endif #include +#include #include #include #include +#include #include #include #include @@ -173,6 +178,9 @@ typedef struct testVector { size_t outLen; } testVector; +int error_test(void); +int base64_test(void); +int asn_test(void); int md2_test(void); int md5_test(void); int md4_test(void); @@ -181,6 +189,7 @@ int sha224_test(void); int sha256_test(void); int sha512_test(void); int sha384_test(void); +int hash_test(void); int hmac_md5_test(void); int hmac_sha_test(void); int hmac_sha224_test(void); @@ -245,6 +254,9 @@ int scrypt_test(void); int pkcs7signed_test(void); int pkcs7encrypted_test(void); #endif +#if !defined(NO_ASN_TIME) && defined(WOLFSSL_TEST_CERT) +int cert_test(void); +#endif #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_TEST_CERT) int certext_test(void); #endif @@ -254,6 +266,14 @@ int idea_test(void); #ifdef WOLFSSL_STATIC_MEMORY int memory_test(void); #endif +#ifdef HAVE_VALGRIND +int mp_test(void); +#endif +int logging_test(void); +int mutex_test(void); +#ifdef USE_WOLFSSL_MEMORY +int memcb_test(void); +#endif #if defined(DEBUG_WOLFSSL) && !defined(HAVE_VALGRIND) && !defined(OPENSSL_EXTRA) int wolfSSL_Debugging_ON(void); @@ -356,6 +376,25 @@ int wolfcrypt_test(void* args) (void)devId; #endif /* WOLFSSL_ASYNC_CRYPT */ + if ( (ret = error_test()) != 0) + return err_sys("error test failed!\n", ret); + else + printf( "error test passed!\n"); + +#ifndef NO_CODING + if ( (ret = base64_test()) != 0) + return err_sys("base64 test failed!\n", ret); + else + printf( "base64 test passed!\n"); +#endif + +#ifndef NO_ASN + if ( (ret = asn_test()) != 0) + return err_sys("base64 test failed!\n", ret); + else + printf( "base64 test passed!\n"); +#endif + #ifndef NO_MD5 if ( (ret = md5_test()) != 0) return err_sys("MD5 test failed!\n", ret); @@ -412,6 +451,11 @@ int wolfcrypt_test(void* args) printf( "SHA-512 test passed!\n"); #endif + if ( (ret = hash_test()) != 0) + return err_sys("Hash test failed!\n", ret); + else + printf( "Hash test passed!\n"); + #ifdef WOLFSSL_RIPEMD if ( (ret = ripemd_test()) != 0) return err_sys("RIPEMD test failed!\n", ret); @@ -616,6 +660,13 @@ int wolfcrypt_test(void* args) printf( "RSA test passed!\n"); #endif +#if !defined(NO_ASN_TIME) && defined(WOLFSSL_TEST_CERT) + if ( (ret = cert_test()) != 0) + return err_sys("CERT test failed!\n", ret); + else + printf( "CERT test passed!\n"); +#endif + #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_TEST_CERT) if ( (ret = certext_test()) != 0) return err_sys("CERT EXT test failed!\n", ret); @@ -728,6 +779,32 @@ int wolfcrypt_test(void* args) printf( "PKCS7encrypted test passed!\n"); #endif +#ifdef HAVE_VALGRIND + if ( (ret = mp_test()) != 0) + return err_sys("mp test failed!\n", ret); + else + printf( "mp test passed!\n"); +#endif + +#ifdef HAVE_VALGRIND + if ( (ret = logging_test()) != 0) + return err_sys("logging test failed!\n", ret); + else + printf( "logging test passed!\n"); +#endif + + if ( (ret = mutex_test()) != 0) + return err_sys("mutex test failed!\n", ret); + else + printf( "mutex test passed!\n"); + +#ifdef USE_WOLFSSL_MEMORY + if ( (ret = memcb_test()) != 0) + return err_sys("memcb test failed!\n", ret); + else + printf( "memcb test passed!\n"); +#endif + #if defined(USE_WOLFSSL_MEMORY) && defined(WOLFSSL_TRACK_MEMORY) ShowMemoryTracker(); #endif @@ -775,6 +852,190 @@ int wolfcrypt_test(void* args) #endif /* NO_MAIN_DRIVER */ +int error_test() +{ + const char* errStr; + char out[WOLFSSL_MAX_ERROR_SZ]; + const char* unknownStr = wc_GetErrorString(0); + +#ifdef NO_ERROR_STRINGS + /* Ensure a valid error code's string matches an invalid code's. + * The string is that error strings are not available. + */ + errStr = wc_GetErrorString(OPEN_RAN_E); + wc_ErrorString(OPEN_RAN_E, out); + if (XSTRNCMP(errStr, unknownStr, XSTRLEN(unknownStr)) != 0) + return -10; + if (XSTRNCMP(out, unknownStr, XSTRLEN(unknownStr)) != 0) + return -11; +#else + int i; + int j = 0; + /* Values that are not or no longer error codes. */ + int missing[] = { -122, -123, -124, -127, -128, -129, + -161, -162, -163, -164, -165, -166, -167, -168, -169, + -178, -179, -233, + 0 }; + + /* Check that all errors have a string and it's the same through the two + * APIs. Check that the values that are not errors map to the unknown + * string. + */ + for (i = OPEN_RAN_E; i >= ECC_CDH_KAT_FIPS_E; i--) { + errStr = wc_GetErrorString(i); + wc_ErrorString(i, out); + + if (i != missing[j]) { + if (XSTRNCMP(errStr, unknownStr, XSTRLEN(unknownStr)) == 0) + return -10; + if (XSTRNCMP(out, unknownStr, XSTRLEN(unknownStr)) == 0) + return -11; + if (XSTRNCMP(errStr, out, XSTRLEN(errStr)) != 0) + return -12; + } + else { + j++; + if (XSTRNCMP(errStr, unknownStr, XSTRLEN(unknownStr)) != 0) + return -13; + if (XSTRNCMP(out, unknownStr, XSTRLEN(unknownStr)) != 0) + return -14; + } + } + + /* Check if the next possible value has been given a string. */ + errStr = wc_GetErrorString(i); + wc_ErrorString(i, out); + if (XSTRNCMP(errStr, unknownStr, XSTRLEN(unknownStr)) != 0) + return -15; + if (XSTRNCMP(out, unknownStr, XSTRLEN(unknownStr)) != 0) + return -16; +#endif + + return 0; +} + +#ifndef NO_CODING +int base64_test() +{ + int ret; + const byte good[] = "A+Gd\0\0\0"; + const byte goodEnd[] = "A+Gd \r\n"; + byte out[128]; + word32 outLen; + byte data[3]; + word32 dataLen; + byte longData[79] = { 0 }; + const byte symbols[] = "+/A="; + const byte badSmall[] = "AAA Gdj="; + const byte badLarge[] = "AAA~Gdj="; + const byte badEOL[] = "A+Gd "; + int i; + + /* Good Base64 encodings. */ + outLen = sizeof(out); + ret = Base64_Decode(good, sizeof(good), out, &outLen); + if (ret != 0) + return -20; + outLen = sizeof(out); + ret = Base64_Decode(goodEnd, sizeof(goodEnd), out, &outLen); + if (ret != 0) + return -21; + + /* Bad parameters. */ + outLen = 1; + ret = Base64_Decode(good, sizeof(good), out, &outLen); + if (ret != BAD_FUNC_ARG) + return -22; + + outLen = sizeof(out); + ret = Base64_Decode(badEOL, sizeof(badEOL), out, &outLen); + if (ret != ASN_INPUT_E) + return -23; + /* Bad character at each offset 0-3. */ + for (i = 0; i < 4; i++) { + outLen = sizeof(out); + ret = Base64_Decode(badSmall + i, 4, out, &outLen); + if (ret != ASN_INPUT_E) + return -24 - i; + ret = Base64_Decode(badLarge + i, 4, out, &outLen); + if (ret != ASN_INPUT_E) + return -28 - i; + } + + /* Decode and encode all symbols - non-alphanumeric. */ + dataLen = sizeof(data); + ret = Base64_Decode(symbols, sizeof(symbols), data, &dataLen); + if (ret != 0) + return -40; + outLen = sizeof(out); + ret = Base64_Encode(data, dataLen, NULL, &outLen); + if (ret != LENGTH_ONLY_E) + return -41; + outLen = sizeof(out); + ret = Base64_Encode(data, dataLen, out, &outLen); + if (ret != 0) + return -42; + outLen = 7; + ret = Base64_EncodeEsc(data, dataLen, out, &outLen); + if (ret != BUFFER_E) + return -43; + outLen = sizeof(out); + ret = Base64_EncodeEsc(data, dataLen, NULL, &outLen); + if (ret != LENGTH_ONLY_E) + return -44; + outLen = sizeof(out); + ret = Base64_EncodeEsc(data, dataLen, out, &outLen); + if (ret != 0) + return -45; + outLen = sizeof(out); + ret = Base64_Encode_NoNl(data, dataLen, out, &outLen); + if (ret != 0) + return -46; + + /* Data that results in an encoding longer than one line. */ + outLen = sizeof(out); + dataLen = sizeof(longData); + ret = Base64_Encode(longData, dataLen, out, &outLen); + if (ret != 0) + return -47; + outLen = sizeof(out); + ret = Base64_EncodeEsc(longData, dataLen, out, &outLen); + if (ret != 0) + return -48; + outLen = sizeof(out); + ret = Base64_Encode_NoNl(longData, dataLen, out, &outLen); + if (ret != 0) + return -49; + + return 0; +} +#endif + +#ifndef NO_ASN +int asn_test() +{ +#ifndef NO_ASN_TIME + { + time_t now; + + /* Parameter Validation tests. */ + if (wc_GetTime(NULL, sizeof(now)) != BAD_FUNC_ARG) + return -100; + if (wc_GetTime(&now, 0) != BUFFER_E) + return -101; + + now = 0; + if (wc_GetTime(&now, sizeof(now)) != 0) + return -102; + if (now == 0) + return -103; + } +#endif + + return 0; +} +#endif + #ifdef WOLFSSL_MD2 int md2_test() { @@ -855,6 +1116,7 @@ int md2_test() int md5_test(void) { Md5 md5; + Md5 partialMd5; byte hash[MD5_DIGEST_SIZE]; testVector a, b, c, d, e; @@ -909,6 +1171,26 @@ int md5_test(void) return -5 - i; } + /* Position restoration and getting the hash doesn't invalidate state. */ + wc_InitMd5(&md5); + wc_InitMd5(&partialMd5); + wc_Md5Update(&partialMd5, (byte*)a.input, 1); + wc_Md5RestorePos(&md5, &partialMd5); + wc_Md5GetHash(&partialMd5, hash); + wc_Md5Update(&partialMd5, (byte*)a.input + 1, (word32)a.inLen - 1); + wc_Md5Update(&md5, (byte*)a.input + 1, (word32)a.inLen - 1); + wc_Md5Final(&partialMd5, hash); + if (XMEMCMP(hash, a.output, a.outLen) != 0) + return -10; + XMEMSET(hash, 0, a.outLen); + wc_Md5Final(&md5, hash); + if (XMEMCMP(hash, a.output, a.outLen) != 0) + return -11; + if (wc_Md5Hash((byte*)a.input, (word32)a.inLen, hash) != 0) + return -12; + if (XMEMCMP(hash, a.output, a.outLen) != 0) + return -13; + return 0; } #endif /* NO_MD5 */ @@ -997,6 +1279,7 @@ int md4_test(void) int sha_test(void) { Sha sha; + Sha partialSha; byte hash[SHA_DIGEST_SIZE]; testVector a, b, c, d; @@ -1048,6 +1331,43 @@ int sha_test(void) return -10 - i; } + /* Position restoration and getting the hash doesn't invalidate state. */ + ret = wc_InitSha(&sha); + if (ret != 0) + return -20; + ret = wc_InitSha(&partialSha); + if (ret != 0) + return -21; + ret = wc_ShaUpdate(&partialSha, (byte*)a.input, 1); + if (ret != 0) + return -22; + wc_ShaRestorePos(&sha, &partialSha); + ret = wc_ShaGetHash(&partialSha, hash); + if (ret != 0) + return -23; + ret = wc_ShaUpdate(&partialSha, (byte*)a.input + 1, (word32)a.inLen - 1); + if (ret != 0) + return -24; + ret = wc_ShaUpdate(&sha, (byte*)a.input + 1, (word32)a.inLen - 1); + if (ret != 0) + return -25; + ret = wc_ShaFinal(&partialSha, hash); + if (ret != 0) + return -26; + if (XMEMCMP(hash, a.output, a.outLen) != 0) + return -27; + XMEMSET(hash, 0, a.outLen); + ret = wc_ShaFinal(&sha, hash); + if (ret != 0) + return -28; + if (XMEMCMP(hash, a.output, a.outLen) != 0) + return -29; + ret = wc_ShaHash((byte*)a.input, (word32)a.inLen, hash); + if (ret != 0) + return -30; + if (XMEMCMP(hash, a.output, a.outLen) != 0) + return -31; + return 0; } @@ -1225,6 +1545,30 @@ int sha224_test(void) return -10 - i; } + /* Getting the hash doesn't invalidate state. */ + ret = wc_InitSha224(&sha); + if (ret != 0) + return -20; + ret = wc_Sha224Update(&sha, (byte*)a.input, 1); + if (ret != 0) + return -21; + ret = wc_Sha224GetHash(&sha, hash); + if (ret != 0) + return -22; + ret = wc_Sha224Update(&sha, (byte*)a.input + 1, a.inLen - 1); + if (ret != 0) + return -23; + ret = wc_Sha224Final(&sha, hash); + if (ret != 0) + return -24; + if (XMEMCMP(hash, a.output, a.outLen) != 0) + return -25; + ret = wc_Sha224Hash((byte*)a.input, a.inLen, hash); + if (ret != 0) + return -26; + if (XMEMCMP(hash, a.output, a.outLen) != 0) + return -27; + return 0; } #endif @@ -1234,6 +1578,7 @@ int sha224_test(void) int sha256_test(void) { Sha256 sha; + Sha256 partialSha; byte hash[SHA256_DIGEST_SIZE]; testVector a, b; @@ -1274,6 +1619,44 @@ int sha256_test(void) return -10 - i; } + /* Position restoration and getting the hash doesn't invalidate state. */ + ret = wc_InitSha256(&sha); + if (ret != 0) + return -20; + ret = wc_InitSha256(&partialSha); + if (ret != 0) + return -21; + ret = wc_Sha256Update(&partialSha, (byte*)a.input, 1); + if (ret != 0) + return -22; + wc_Sha256RestorePos(&sha, &partialSha); + ret = wc_Sha256GetHash(&partialSha, hash); + if (ret != 0) + return -23; + ret = wc_Sha256Update(&partialSha, (byte*)a.input + 1, (word32)a.inLen - 1); + if (ret != 0) + return -24; + ret = wc_Sha256Update(&sha, (byte*)a.input + 1, (word32)a.inLen - 1); + if (ret != 0) + return -25; + ret = wc_Sha256Final(&partialSha, hash); + if (ret != 0) + return -26; + if (XMEMCMP(hash, a.output, a.outLen) != 0) + return -27; + XMEMSET(hash, 0, a.outLen); + ret = wc_Sha256Final(&sha, hash); + if (ret != 0) + return -28; + if (XMEMCMP(hash, a.output, a.outLen) != 0) + return -29; + XMEMSET(hash, 0, a.outLen); + ret = wc_Sha256Hash((byte*)a.input, (word32)a.inLen, hash); + if (ret != 0) + return -30; + if (XMEMCMP(hash, a.output, a.outLen) != 0) + return -31; + return 0; } #endif @@ -1329,6 +1712,30 @@ int sha512_test(void) return -10 - i; } + /* Getting the hash doesn't invalidate state. */ + ret = wc_InitSha512(&sha); + if (ret != 0) + return -20; + ret = wc_Sha512Update(&sha, (byte*)a.input, 1); + if (ret != 0) + return -21; + ret = wc_Sha512GetHash(&sha, hash); + if (ret != 0) + return -22; + ret = wc_Sha512Update(&sha, (byte*)a.input + 1, (word32)a.inLen - 1); + if (ret != 0) + return -23; + ret = wc_Sha512Final(&sha, hash); + if (ret != 0) + return -24; + if (XMEMCMP(hash, a.output, a.outLen) != 0) + return -15; + ret = wc_Sha512Hash((byte*)a.input, (word32)a.inLen, hash); + if (ret != 0) + return -26; + if (XMEMCMP(hash, a.output, a.outLen) != 0) + return -27; + return 0; } #endif @@ -1382,10 +1789,196 @@ int sha384_test(void) return -10 - i; } + /* Getting the hash doesn't invalidate state. */ + ret = wc_InitSha384(&sha); + if (ret != 0) + return -20; + ret = wc_Sha384Update(&sha, (byte*)a.input, 1); + if (ret != 0) + return -21; + ret = wc_Sha384GetHash(&sha, hash); + if (ret != 0) + return -22; + ret = wc_Sha384Update(&sha, (byte*)a.input + 1, a.inLen - 1); + if (ret != 0) + return -23; + ret = wc_Sha384Final(&sha, hash); + if (ret != 0) + return -24; + if (XMEMCMP(hash, a.output, a.outLen) != 0) + return -25; + XMEMSET(hash, 0, a.outLen); + ret = wc_Sha384Hash((byte*)a.input, a.inLen, hash); + if (ret != 0) + return -26; + if (XMEMCMP(hash, a.output, a.outLen) != 0) + return -27; + return 0; } #endif /* WOLFSSL_SHA384 */ +int hash_test(void) +{ + wc_HashAlg hash; + int ret, exp_ret; + int i, j; + byte data[] = "0123456789abcdef0123456789abcdef012345"; + byte out[MAX_DIGEST_SIZE]; + enum wc_HashType typesGood[] = { WC_HASH_TYPE_MD5, WC_HASH_TYPE_SHA, + WC_HASH_TYPE_SHA224, WC_HASH_TYPE_SHA384, + WC_HASH_TYPE_SHA512, WC_HASH_TYPE_SHA256 }; + enum wc_HashType typesNoImpl[] = { +#ifdef NO_MD5 + WC_HASH_TYPE_MD5, +#endif +#ifdef NO_SHA + WC_HASH_TYPE_SHA, +#endif +#ifndef WOLFSSL_SHA224 + WC_HASH_TYPE_SHA224, +#endif +#ifdef NO_SHA256 + WC_HASH_TYPE_SHA256, +#endif +#ifndef WOLFSSL_SHA384 + WC_HASH_TYPE_SHA384, +#endif +#ifndef WOLFSSL_SHA512 + WC_HASH_TYPE_SHA512, +#endif + WC_HASH_TYPE_NONE + }; + enum wc_HashType typesBad[] = { WC_HASH_TYPE_NONE, WC_HASH_TYPE_MD5_SHA, + WC_HASH_TYPE_MD2, WC_HASH_TYPE_MD4 }; + + /* Parameter Validation testing. */ + ret = wc_HashInit(NULL, WC_HASH_TYPE_SHA256); + if (ret != BAD_FUNC_ARG) + return -4100; + ret = wc_HashUpdate(NULL, WC_HASH_TYPE_SHA256, NULL, sizeof(data)); + if (ret != BAD_FUNC_ARG) + return -4101; + ret = wc_HashUpdate(&hash, WC_HASH_TYPE_SHA256, NULL, sizeof(data)); + if (ret != BAD_FUNC_ARG) + return -4102; + ret = wc_HashUpdate(NULL, WC_HASH_TYPE_SHA256, data, sizeof(data)); + if (ret != BAD_FUNC_ARG) + return -4103; + ret = wc_HashFinal(NULL, WC_HASH_TYPE_SHA256, NULL); + if (ret != BAD_FUNC_ARG) + return -4104; + ret = wc_HashFinal(&hash, WC_HASH_TYPE_SHA256, NULL); + if (ret != BAD_FUNC_ARG) + return -4105; + ret = wc_HashFinal(NULL, WC_HASH_TYPE_SHA256, out); + if (ret != BAD_FUNC_ARG) + return -4106; + + /* Try invalid hash algorithms. */ + for (i = 0; i < (int)(sizeof(typesBad)/sizeof(*typesBad)); i++) { + ret = wc_HashInit(&hash, typesBad[i]); + if (ret != BAD_FUNC_ARG) + return -4110 - i; + ret = wc_HashUpdate(&hash, typesBad[i], data, sizeof(data)); + if (ret != BAD_FUNC_ARG) + return -4120 - i; + ret = wc_HashFinal(&hash, typesBad[i], data); + if (ret != BAD_FUNC_ARG) + return -4130 - i; + } + + /* Try valid hash algorithms. */ + for (i = 0, j = 0; i < (int)(sizeof(typesGood)/sizeof(*typesGood)); i++) { + exp_ret = 0; + if (typesGood[i] == typesNoImpl[j]) { + /* Recognized but no implementation compiled in. */ + exp_ret = HASH_TYPE_E; + j++; + } + ret = wc_HashInit(&hash, typesGood[i]); + if (ret != exp_ret) + return -4140 - i; + ret = wc_HashUpdate(&hash, typesGood[i], data, sizeof(data)); + if (ret != exp_ret) + return -4150 - i; + ret = wc_HashFinal(&hash, typesGood[i], data); + if (ret != exp_ret) + return -4160 - i; + ret = wc_HashGetOID(typesGood[i]); + if (ret == BAD_FUNC_ARG || + (exp_ret == 0 && ret == HASH_TYPE_E) || + (exp_ret != 0 && ret != HASH_TYPE_E)) { + return -4170 - i; + } + } + + ret = wc_HashGetOID(WC_HASH_TYPE_MD2); +#ifdef WOLFSSL_MD2 + if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG) + return -4180; +#else + if (ret != HASH_TYPE_E) + return -4180; +#endif + ret = wc_HashGetOID(WC_HASH_TYPE_MD5_SHA); +#ifndef NO_MD5 + if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG) + return -4181; +#else + if (ret != HASH_TYPE_E) + return -4181; +#endif + ret = wc_HashGetOID(WC_HASH_TYPE_MD4); + if (ret != BAD_FUNC_ARG) + return -4182; + ret = wc_HashGetOID(WC_HASH_TYPE_NONE); + if (ret != BAD_FUNC_ARG) + return -4183; + +#ifndef NO_ASN +#ifdef WOLFSSL_MD2 + ret = wc_GetCTC_HashOID(MD2); + if (ret == 0) + return -4190; +#endif +#ifndef NO_MD5 + ret = wc_GetCTC_HashOID(MD5); + if (ret == 0) + return -4191; +#endif +#ifndef NO_SHA + ret = wc_GetCTC_HashOID(SHA); + if (ret == 0) + return -4192; +#endif +#ifdef WOLFSSL_SHA224 + ret = wc_GetCTC_HashOID(SHA224); + if (ret == 0) + return -4193; +#endif +#ifndef NO_SHA256 + ret = wc_GetCTC_HashOID(SHA256); + if (ret == 0) + return -4194; +#endif +#ifdef WOLFSSL_SHA384 + ret = wc_GetCTC_HashOID(SHA384); + if (ret == 0) + return -4195; +#endif +#ifdef WOLFSSL_SHA512 + ret = wc_GetCTC_HashOID(SHA512); + if (ret == 0) + return -4196; +#endif + ret = wc_GetCTC_HashOID(-1); + if (ret != 0) + return -4197; +#endif + + return 0; +} #if !defined(NO_HMAC) && !defined(NO_MD5) int hmac_md5_test(void) @@ -1463,6 +2056,11 @@ int hmac_md5_test(void) #endif } +#ifndef HAVE_FIPS + if (wc_HmacSizeByType(MD5) != MD5_DIGEST_SIZE) + return -4018; +#endif + return 0; } #endif /* NO_HMAC && NO_MD5 */ @@ -1540,6 +2138,11 @@ int hmac_sha_test(void) #endif } +#ifndef HAVE_FIPS + if (wc_HmacSizeByType(SHA) != SHA_DIGEST_SIZE) + return -4021; +#endif + return 0; } #endif @@ -1618,6 +2221,11 @@ int hmac_sha224_test(void) #endif } +#ifndef HAVE_FIPS + if (wc_HmacSizeByType(SHA224) != SHA224_DIGEST_SIZE) + return -4024; +#endif + return 0; } #endif @@ -1699,6 +2307,15 @@ int hmac_sha256_test(void) #endif } +#ifndef HAVE_FIPS + if (wc_HmacSizeByType(SHA256) != SHA256_DIGEST_SIZE) + return -4024; + if (wc_HmacSizeByType(20) != BAD_FUNC_ARG) + return -4025; +#endif + if (wolfSSL_GetHmacMaxSize() != MAX_DIGEST_SIZE) + return -4026; + return 0; } #endif @@ -1785,6 +2402,11 @@ int hmac_blake2b_test(void) #endif } +#ifndef HAVE_FIPS + if (wc_HmacSizeByType(BLAKE2B_ID) != BLAKE2B_OUTBYTES) + return -4027; +#endif + return 0; } #endif @@ -1862,6 +2484,11 @@ int hmac_sha384_test(void) return -20 - i; } +#ifndef HAVE_FIPS + if (wc_HmacSizeByType(SHA384) != SHA384_DIGEST_SIZE) + return -4030; +#endif + return 0; } #endif @@ -1942,6 +2569,11 @@ int hmac_sha512_test(void) return -20 - i; } +#ifndef HAVE_FIPS + if (wc_HmacSizeByType(SHA512) != SHA512_DIGEST_SIZE) + return -4033; +#endif + return 0; } #endif @@ -2653,6 +3285,59 @@ int chacha20_poly1305_aead_test(void) XMEMSET(generatedAuthTag, 0, sizeof(generatedAuthTag)); XMEMSET(generatedPlaintext, 0, sizeof(generatedPlaintext)); + /* Parameter Validation testing */ + /* Encrypt */ + err = wc_ChaCha20Poly1305_Encrypt(NULL, iv1, aad1, sizeof(aad1), plaintext1, + sizeof(plaintext1), generatedCiphertext, generatedAuthTag); + if (err != BAD_FUNC_ARG) + return -1050; + err = wc_ChaCha20Poly1305_Encrypt(key1, NULL, aad1, sizeof(aad1), + plaintext1, sizeof(plaintext1), generatedCiphertext, + generatedAuthTag); + if (err != BAD_FUNC_ARG) + return -1051; + err = wc_ChaCha20Poly1305_Encrypt(key1, iv1, aad1, sizeof(aad1), NULL, + sizeof(plaintext1), generatedCiphertext, generatedAuthTag); + if (err != BAD_FUNC_ARG) + return -1052; + err = wc_ChaCha20Poly1305_Encrypt(key1, iv1, aad1, sizeof(aad1), plaintext1, + sizeof(plaintext1), NULL, generatedAuthTag); + if (err != BAD_FUNC_ARG) + return -1053; + err = wc_ChaCha20Poly1305_Encrypt(key1, iv1, aad1, sizeof(aad1), plaintext1, + sizeof(plaintext1), generatedCiphertext, NULL); + if (err != BAD_FUNC_ARG) + return -1054; + err = wc_ChaCha20Poly1305_Encrypt(key1, iv1, aad1, sizeof(aad1), plaintext1, + 0, generatedCiphertext, generatedAuthTag); + if (err != BAD_FUNC_ARG) + return -1055; + /* Decrypt */ + err = wc_ChaCha20Poly1305_Decrypt(NULL, iv2, aad2, sizeof(aad2), cipher2, + sizeof(cipher2), authTag2, generatedPlaintext); + if (err != BAD_FUNC_ARG) + return -1056; + err = wc_ChaCha20Poly1305_Decrypt(key2, NULL, aad2, sizeof(aad2), cipher2, + sizeof(cipher2), authTag2, generatedPlaintext); + if (err != BAD_FUNC_ARG) + return -1057; + err = wc_ChaCha20Poly1305_Decrypt(key2, iv2, aad2, sizeof(aad2), NULL, + sizeof(cipher2), authTag2, generatedPlaintext); + if (err != BAD_FUNC_ARG) + return -1058; + err = wc_ChaCha20Poly1305_Decrypt(key2, iv2, aad2, sizeof(aad2), cipher2, + sizeof(cipher2), NULL, generatedPlaintext); + if (err != BAD_FUNC_ARG) + return -1059; + err = wc_ChaCha20Poly1305_Decrypt(key2, iv2, aad2, sizeof(aad2), cipher2, + sizeof(cipher2), authTag2, NULL); + if (err != BAD_FUNC_ARG) + return -1060; + err = wc_ChaCha20Poly1305_Decrypt(key2, iv2, aad2, sizeof(aad2), cipher2, + 0, authTag2, generatedPlaintext); + if (err != BAD_FUNC_ARG) + return -1061; + /* Test #1 */ err = wc_ChaCha20Poly1305_Encrypt(key1, iv1, @@ -2875,6 +3560,132 @@ int des3_test(void) #ifndef NO_AES +static int aes_key_size_test(void) +{ + int ret; + Aes aes; + byte key16[] = { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, + 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66 }; + byte key24[] = { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, + 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37 }; + byte key32[] = { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, + 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, + 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66 }; + byte iv[] = "1234567890abcdef"; +#ifndef HAVE_FIPS + word32 keySize; +#endif + +#ifdef WC_INITAES_H + ret = wc_InitAes_h(NULL, NULL); + if (ret != BAD_FUNC_ARG) + return -1100; + ret = wc_InitAes_h(&aes, NULL); + if (ret != 0) + return -1100; +#endif + +#ifndef HAVE_FIPS + /* Parameter Validation testing. */ + ret = wc_AesGetKeySize(NULL, NULL); + if (ret != BAD_FUNC_ARG) + return -1100; + ret = wc_AesGetKeySize(&aes, NULL); + if (ret != BAD_FUNC_ARG) + return -1101; + ret = wc_AesGetKeySize(NULL, &keySize); + if (ret != BAD_FUNC_ARG) + return -1102; + /* Crashes in FIPS */ + ret = wc_AesSetKey(NULL, key16, sizeof(key16), iv, AES_ENCRYPTION); + if (ret != BAD_FUNC_ARG) + return -1103; +#endif + /* NULL IV indicates to use all zeros IV. */ + ret = wc_AesSetKey(&aes, key16, sizeof(key16), NULL, AES_ENCRYPTION); + if (ret != 0) + return -1104; + ret = wc_AesSetKey(&aes, key32, sizeof(key32) - 1, iv, AES_ENCRYPTION); + if (ret != BAD_FUNC_ARG) + return -1111; +#ifndef HAVE_FIPS + /* Force invalid rounds */ + aes.rounds = 16; + ret = wc_AesGetKeySize(&aes, &keySize); + if (ret != BAD_FUNC_ARG) + return -1112; +#endif + + ret = wc_AesSetKey(&aes, key16, sizeof(key16), iv, AES_ENCRYPTION); + if (ret != 0) + return -1105; +#ifndef HAVE_FIPS + ret = wc_AesGetKeySize(&aes, &keySize); + if (ret != 0 || keySize != sizeof(key16)) + return -1106; +#endif + + ret = wc_AesSetKey(&aes, key24, sizeof(key24), iv, AES_ENCRYPTION); + if (ret != 0) + return -1107; +#ifndef HAVE_FIPS + ret = wc_AesGetKeySize(&aes, &keySize); + if (ret != 0 || keySize != sizeof(key24)) + return -1108; +#endif + + ret = wc_AesSetKey(&aes, key32, sizeof(key32), iv, AES_ENCRYPTION); + if (ret != 0) + return -1109; +#ifndef HAVE_FIPS + ret = wc_AesGetKeySize(&aes, &keySize); + if (ret != 0 || keySize != sizeof(key32)) + return -1110; +#endif + + return 0; +} + +#if defined(HAVE_AES_CBC) +static int aes_cbc_test(void) +{ + byte cipher[AES_BLOCK_SIZE]; + byte plain[AES_BLOCK_SIZE]; + int ret; + const byte msg[] = { /* "Now is the time for all " w/o trailing 0 */ + 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74, + 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20, + 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20 + }; + byte key[] = "0123456789abcdef "; /* align */ + byte iv[] = "1234567890abcdef "; /* align */ + + /* Parameter Validation testing. */ + ret = wc_AesCbcEncryptWithKey(cipher, msg, AES_BLOCK_SIZE, key, 17, NULL); + if (ret != BAD_FUNC_ARG) + return -1120; + ret = wc_AesCbcDecryptWithKey(plain, cipher, AES_BLOCK_SIZE, key, 17, NULL); + if (ret != BAD_FUNC_ARG) + return -1121; + + ret = wc_AesCbcEncryptWithKey(cipher, msg, AES_BLOCK_SIZE, key, + AES_BLOCK_SIZE, iv); + if (ret != 0) + return -1130; + ret = wc_AesCbcDecryptWithKey(plain, cipher, AES_BLOCK_SIZE, key, + AES_BLOCK_SIZE, iv); + if (ret != 0) + return -1131; + + if (XMEMCMP(plain, msg, AES_BLOCK_SIZE) != 0) + return -1132; + + return 0; +} +#endif + int aes_test(void) { #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_COUNTER) @@ -3231,6 +4042,16 @@ int aes_test(void) } #endif /* WOLFSSL_AES_DIRECT */ + ret = aes_key_size_test(); + if (ret != 0) + return ret; + +#if defined(HAVE_AES_CBC) + ret = aes_cbc_test(); + if (ret != 0) + return ret; +#endif + return ret; } @@ -4202,7 +5023,12 @@ static int random_rng_test(void) ret = -38; goto exit; } - ret = 0; + + ret = wc_RNG_GenerateByte(&rng, block); + if (ret != 0) { + ret = -41; + goto exit; + } exit: /* Make sure and free RNG */ @@ -4518,6 +5344,65 @@ byte GetEntropy(ENTROPY_CMD cmd, byte* out) #endif #endif +#if !defined(NO_ASN_TIME) && defined(WOLFSSL_TEST_CERT) +int cert_test(void) +{ + DecodedCert cert; + byte* tmp; + size_t bytes; + FILE *file; + int ret; + + tmp = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); + if (tmp == NULL) + return -200; + + /* Certificate with Name Constraints extension. */ +#ifdef FREESCALE_MQX + file = fopen(".\\certs\\test\\cert-ext-nc.der", "rb"); +#else + file = fopen("./certs/test/cert-ext-nc.der", "rb"); +#endif + if (!file) { + ret = -201; + goto done; + } + bytes = fread(tmp, 1, FOURK_BUF, file); + fclose(file); + InitDecodedCert(&cert, tmp, (word32)bytes, 0); + ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL); + if (ret != 0) { + ret = -202; + goto done; + } + FreeDecodedCert(&cert); + + /* Certificate with Inhibit Any Policy extension. */ +#ifdef FREESCALE_MQX + file = fopen(".\\certs\\test\\cert-ext-ia.der", "rb"); +#else + file = fopen("./certs/test/cert-ext-ia.der", "rb"); +#endif + if (!file) { + ret = -201; + goto done; + } + bytes = fread(tmp, 1, FOURK_BUF, file); + fclose(file); + InitDecodedCert(&cert, tmp, (word32)bytes, 0); + ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL); + if (ret != 0) { + ret = -204; + goto done; + } + +done: + FreeDecodedCert(&cert); + XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); + + return ret; +} +#endif #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_TEST_CERT) int certext_test(void) @@ -4713,6 +5598,447 @@ int certext_test(void) } #endif /* WOLFSSL_CERT_EXT && WOLFSSL_TEST_CERT */ +static int rsa_flatten_test(RsaKey* key) +{ + int ret; + byte e[256]; + byte n[256]; + word32 eSz = sizeof(e); + word32 nSz = sizeof(n); + + /* Parameter Validation testing. */ + ret = wc_RsaFlattenPublicKey(NULL, e, &eSz, n, &nSz); +#ifdef HAVE_USER_RSA + /* Implementation using IPP Libraries returns: + * -101 = USER_CRYPTO_ERROR + */ + if (ret == 0) +#else + if (ret != BAD_FUNC_ARG) +#endif + return -480; + ret = wc_RsaFlattenPublicKey(key, NULL, &eSz, n, &nSz); +#ifdef HAVE_USER_RSA + /* Implementation using IPP Libraries returns: + * -101 = USER_CRYPTO_ERROR + */ + if (ret == 0) +#else + if (ret != BAD_FUNC_ARG) +#endif + return -481; + ret = wc_RsaFlattenPublicKey(key, e, NULL, n, &nSz); +#ifdef HAVE_USER_RSA + /* Implementation using IPP Libraries returns: + * -101 = USER_CRYPTO_ERROR + */ + if (ret == 0) +#else + if (ret != BAD_FUNC_ARG) +#endif + return -482; + ret = wc_RsaFlattenPublicKey(key, e, &eSz, NULL, &nSz); +#ifdef HAVE_USER_RSA + /* Implementation using IPP Libraries returns: + * -101 = USER_CRYPTO_ERROR + */ + if (ret == 0) +#else + if (ret != BAD_FUNC_ARG) +#endif + return -483; + ret = wc_RsaFlattenPublicKey(key, e, &eSz, n, NULL); +#ifdef HAVE_USER_RSA + /* Implementation using IPP Libraries returns: + * -101 = USER_CRYPTO_ERROR + */ + if (ret == 0) +#else + if (ret != BAD_FUNC_ARG) +#endif + return -484; + ret = wc_RsaFlattenPublicKey(key, e, &eSz, n, &nSz); + if (ret != 0) + return -485; + eSz = 0; + ret = wc_RsaFlattenPublicKey(key, e, &eSz, n, &nSz); +#ifdef HAVE_USER_RSA + /* Implementation using IPP Libraries returns: + * -101 = USER_CRYPTO_ERROR + */ + if (ret == 0) +#elif defined(HAVE_FIPS) + if (ret != 0) +#else + if (ret != RSA_BUFFER_E) +#endif + return -486; + eSz = sizeof(e); + nSz = 0; + ret = wc_RsaFlattenPublicKey(key, e, &eSz, n, &nSz); +#ifdef HAVE_USER_RSA + /* Implementation using IPP Libraries returns: + * -101 = USER_CRYPTO_ERROR + */ + if (ret == 0) +#else + if (ret != RSA_BUFFER_E) +#endif + return -487; + + return 0; +} + +static int rsa_sig_test(RsaKey* key, word32 keyLen, int modLen, WC_RNG* rng) +{ + int ret; + word32 sigSz; + byte in[] = "Everyone gets Friday off."; + word32 inLen = (word32)XSTRLEN((char*)in); + byte out[256]; + + /* Parameter Validation testing. */ + ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_NONE, key, keyLen); + if (ret != BAD_FUNC_ARG) + return -490; + ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_RSA, key, 0); + if (ret != BAD_FUNC_ARG) + return -491; + + sigSz = modLen; + ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, NULL, + inLen, out, &sigSz, key, keyLen, rng); + if (ret != BAD_FUNC_ARG) + return -492; + ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in, + 0, out, &sigSz, key, keyLen, rng); + if (ret != BAD_FUNC_ARG) + return -493; + ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in, + inLen, NULL, &sigSz, key, keyLen, rng); + if (ret != BAD_FUNC_ARG) + return -494; + ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in, + inLen, out, NULL, key, keyLen, rng); + if (ret != BAD_FUNC_ARG) + return -495; + ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in, + inLen, out, &sigSz, NULL, keyLen, rng); + if (ret != BAD_FUNC_ARG) + return -496; + ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in, + inLen, out, &sigSz, key, 0, rng); + if (ret != BAD_FUNC_ARG) + return -497; + ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in, + inLen, out, &sigSz, key, keyLen, NULL); +#ifdef HAVE_USER_RSA + /* Implementation using IPP Libraries returns: + * -101 = USER_CRYPTO_ERROR + */ + if (ret == 0) +#elif defined(HAVE_FIPS) + /* FIPS140 implementation doesn't do blinding. */ + if (ret != 0) +#else + if (ret != MISSING_RNG_E) +#endif + return -498; + sigSz = 0; + ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in, + inLen, out, &sigSz, key, keyLen, rng); + if (ret != BAD_FUNC_ARG) + return -499; + + ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, NULL, + inLen, out, modLen, key, keyLen); + if (ret != BAD_FUNC_ARG) + return -500; + ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in, + 0, out, modLen, key, keyLen); + if (ret != BAD_FUNC_ARG) + return -501; + ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in, + inLen, NULL, modLen, key, keyLen); + if (ret != BAD_FUNC_ARG) + return -502; + ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in, + inLen, out, 0, key, keyLen); + if (ret != BAD_FUNC_ARG) + return -503; + ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in, + inLen, out, modLen, NULL, keyLen); + if (ret != BAD_FUNC_ARG) + return -504; + ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in, + inLen, out, modLen, key, 0); + if (ret != BAD_FUNC_ARG) + return -505; + +#ifndef HAVE_ECC + ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_ECC, key, keyLen); + if (ret != SIG_TYPE_E) + return -506; +#endif + + /* Use APIs. */ + ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_RSA, key, keyLen); + if (ret != modLen) + return -507; + ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_RSA_W_ENC, key, keyLen); + if (ret != modLen) + return -508; + + sigSz = ret; + ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in, + inLen, out, &sigSz, key, keyLen, rng); + if (ret != 0) + return -509; + + ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in, + inLen, out, modLen, key, keyLen); + if (ret != 0) + return -510; + + sigSz = sizeof(out); + ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA_W_ENC, + in, inLen, out, &sigSz, key, keyLen, rng); + if (ret != 0) + return -511; + + ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA_W_ENC, + in, inLen, out, modLen, key, keyLen); + if (ret != 0) + return -512; + + /* Wrong signature type. */ + ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in, + inLen, out, modLen, key, keyLen); + if (ret == 0) + return -513; + + return 0; +} + +#ifndef HAVE_USER_RSA +static int rsa_decode_test(void) +{ + int ret; + word32 inSz; + word32 inOutIdx; + RsaKey keyPub; + const byte n[2] = { 0x00, 0x23 }; + const byte e[2] = { 0x00, 0x03 }; + const byte good[] = { 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 }; + const byte goodAlgId[] = { 0x30, 0x0f, 0x30, 0x0d, 0x06, 0x00, + 0x03, 0x09, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 }; + const byte goodBitStrNoZero[] = { 0x30, 0x0e, 0x30, 0x0c, 0x06, 0x00, + 0x03, 0x08, 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 }; + const byte goodAlgIdNull[] = { 0x30, 0x11, 0x30, 0x0f, 0x06, 0x00, + 0x05, 0x00, 0x03, 0x09, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23, + 0x02, 0x1, 0x03 }; + const byte badAlgIdNull[] = { 0x30, 0x12, 0x30, 0x10, 0x06, 0x00, + 0x05, 0x01, 0x00, 0x03, 0x09, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23, + 0x02, 0x1, 0x03 }; + const byte badNotBitString[] = { 0x30, 0x0f, 0x30, 0x0d, 0x06, 0x00, + 0x04, 0x09, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 }; + const byte badBitStringLen[] = { 0x30, 0x0f, 0x30, 0x0d, 0x06, 0x00, + 0x03, 0x0a, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 }; + const byte badNoSeq[] = { 0x30, 0x0d, 0x30, 0x0b, 0x06, 0x00, 0x03, 0x07, + 0x00, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 }; + const byte badNoObj[] = { + 0x30, 0x0f, 0x30, 0x0d, 0x05, 0x00, 0x03, 0x09, 0x00, 0x30, 0x06, + 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 }; + const byte badIntN[] = { 0x30, 0x06, 0x02, 0x05, 0x23, 0x02, 0x1, 0x03 }; + const byte badNotIntE[] = { 0x30, 0x06, 0x02, 0x01, 0x23, 0x04, 0x1, 0x03 }; + const byte badLength[] = { 0x30, 0x04, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 }; + + ret = wc_InitRsaKey(&keyPub, NULL); + if (ret != 0) + return -520; + + /* Parameter Validation testing. */ + ret = wc_RsaPublicKeyDecodeRaw(NULL, sizeof(n), e, sizeof(e), &keyPub); + if (ret != BAD_FUNC_ARG) { + ret = -521; + goto done; + } + ret = wc_RsaPublicKeyDecodeRaw(n, sizeof(n), NULL, sizeof(e), &keyPub); + if (ret != BAD_FUNC_ARG) { + ret = -522; + goto done; + } + ret = wc_RsaPublicKeyDecodeRaw(n, sizeof(n), e, sizeof(e), NULL); + if (ret != BAD_FUNC_ARG) { + ret = -523; + goto done; + } + /* TODO: probably should fail when length is -1! */ + ret = wc_RsaPublicKeyDecodeRaw(n, -1, e, sizeof(e), &keyPub); + if (ret != 0) { + ret = -525; + goto done; + } + ret = wc_RsaPublicKeyDecodeRaw(n, sizeof(n), e, -1, &keyPub); + if (ret != 0) { + ret = -526; + goto done; + } + + /* Use API. */ + ret = wc_RsaPublicKeyDecodeRaw(n, sizeof(n), e, sizeof(e), &keyPub); + if (ret != 0) { + ret = -527; + goto done; + } + + /* Parameter Validation testing. */ + inSz = sizeof(good); + ret = wc_RsaPublicKeyDecode(NULL, &inOutIdx, &keyPub, inSz); + if (ret != BAD_FUNC_ARG) { + ret = -531; + goto done; + } + ret = wc_RsaPublicKeyDecode(good, NULL, &keyPub, inSz); + if (ret != BAD_FUNC_ARG) { + ret = -532; + goto done; + } + ret = wc_RsaPublicKeyDecode(good, &inOutIdx, NULL, inSz); + if (ret != BAD_FUNC_ARG) { + ret = -533; + goto done; + } + + /* Use good data and offest to bad data. */ + inOutIdx = 2; + inSz = sizeof(good) - inOutIdx; + ret = wc_RsaPublicKeyDecode(good, &inOutIdx, &keyPub, inSz); + if (ret != ASN_PARSE_E) { + ret = -540; + goto done; + } + inOutIdx = 2; + inSz = sizeof(goodAlgId) - inOutIdx; + ret = wc_RsaPublicKeyDecode(goodAlgId, &inOutIdx, &keyPub, inSz); + if (ret != ASN_PARSE_E) { + ret = -541; + goto done; + } + /* Try different bad data. */ + inSz = sizeof(badAlgIdNull); + inOutIdx = 0; + ret = wc_RsaPublicKeyDecode(badAlgIdNull, &inOutIdx, &keyPub, inSz); + if (ret != ASN_EXPECT_0_E) { + ret = -542; + goto done; + } + inSz = sizeof(badNotBitString); + inOutIdx = 0; + ret = wc_RsaPublicKeyDecode(badNotBitString, &inOutIdx, &keyPub, inSz); + if (ret != ASN_BITSTR_E) { + ret = -543; + goto done; + } + inSz = sizeof(badBitStringLen); + inOutIdx = 0; + ret = wc_RsaPublicKeyDecode(badBitStringLen, &inOutIdx, &keyPub, inSz); + if (ret != ASN_PARSE_E) { + ret = -544; + goto done; + } + inSz = sizeof(badNoSeq); + inOutIdx = 0; + ret = wc_RsaPublicKeyDecode(badNoSeq, &inOutIdx, &keyPub, inSz); + if (ret != ASN_PARSE_E) { + ret = -545; + goto done; + } + inSz = sizeof(badNoObj); + inOutIdx = 0; + ret = wc_RsaPublicKeyDecode(badNoObj, &inOutIdx, &keyPub, inSz); + if (ret != ASN_PARSE_E) { + ret = -546; + goto done; + } + inSz = sizeof(badIntN); + inOutIdx = 0; + ret = wc_RsaPublicKeyDecode(badIntN, &inOutIdx, &keyPub, inSz); + if (ret != ASN_RSA_KEY_E) { + ret = -547; + goto done; + } + inSz = sizeof(badNotIntE); + inOutIdx = 0; + ret = wc_RsaPublicKeyDecode(badNotIntE, &inOutIdx, &keyPub, inSz); + if (ret != ASN_RSA_KEY_E) { + ret = -548; + goto done; + } + /* TODO: Shouldn't pass as the sequence length is too small. */ + inSz = sizeof(badLength); + inOutIdx = 0; + ret = wc_RsaPublicKeyDecode(badLength, &inOutIdx, &keyPub, inSz); + if (ret != 0) { + ret = -549; + goto done; + } + /* TODO: Shouldn't ignore object id's data. */ + + /* Valid data cases. */ + inSz = sizeof(good); + inOutIdx = 0; + ret = wc_RsaPublicKeyDecode(good, &inOutIdx, &keyPub, inSz); + if (ret != 0) { + ret = -550; + goto done; + } + if (inOutIdx != inSz) { + ret = -551; + goto done; + } + + inSz = sizeof(goodAlgId); + inOutIdx = 0; + ret = wc_RsaPublicKeyDecode(goodAlgId, &inOutIdx, &keyPub, inSz); + if (ret != 0) { + ret = -552; + goto done; + } + if (inOutIdx != inSz) { + ret = -553; + goto done; + } + + inSz = sizeof(goodAlgIdNull); + inOutIdx = 0; + ret = wc_RsaPublicKeyDecode(goodAlgIdNull, &inOutIdx, &keyPub, inSz); + if (ret != 0) { + ret = -554; + goto done; + } + if (inOutIdx != inSz) { + ret = -555; + goto done; + } + + inSz = sizeof(goodBitStrNoZero); + inOutIdx = 0; + ret = wc_RsaPublicKeyDecode(goodBitStrNoZero, &inOutIdx, &keyPub, inSz); + if (ret != 0) { + ret = -556; + goto done; + } + if (inOutIdx != inSz) { + ret = -557; + goto done; + } + +done: + wc_FreeRsaKey(&keyPub); + return ret; +} +#endif int rsa_test(void) { @@ -4729,6 +6055,7 @@ int rsa_test(void) word32 inLen = (word32)XSTRLEN((char*)in); byte out[256]; byte plain[256]; + byte* res; #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) FILE *file, *file2; #endif @@ -4736,6 +6063,12 @@ int rsa_test(void) DecodedCert cert; #endif +#ifndef HAVE_USER_RSA + ret = rsa_decode_test(); + if (ret != 0) + return ret; +#endif + tmp = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); if (tmp == NULL) return -40; @@ -4776,6 +6109,10 @@ int rsa_test(void) return -42; } + ret = rsa_sig_test(&key, sizeof(RsaKey), wc_RsaEncryptSize(&key), &rng); + if (ret != 0) + return ret; + do { #if defined(WOLFSSL_ASYNC_CRYPT) ret = wc_RsaAsyncWait(ret, &key); @@ -4823,6 +6160,20 @@ int rsa_test(void) wc_FreeRng(&rng); return -45; } + do { +#if defined(WOLFSSL_ASYNC_CRYPT) + ret = wc_RsaAsyncWait(ret, key); +#endif + if (ret >= 0) { + ret = wc_RsaPrivateDecryptInline(out, idx, &res, &key); + } + } while (ret == WC_PENDING_E); + if (ret < 0) + return -473; + if (ret != (int)inLen) + return -473; + if (XMEMCMP(res, in, inLen)) + return -474; do { #if defined(WOLFSSL_ASYNC_CRYPT) @@ -4944,6 +6295,22 @@ int rsa_test(void) return -245; } + do { +#if defined(WOLFSSL_ASYNC_CRYPT) + ret = wc_RsaAsyncWait(ret, key); +#endif + if (ret >= 0) { + ret = wc_RsaPrivateDecryptInline_ex(out, idx, &res, &key, + WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0); + } + } while (ret == WC_PENDING_E); + if (ret < 0) + return -473; + if (ret != (int)inLen) + return -474; + if (XMEMCMP(res, in, inLen)) + return -475; + /* check fails if not using the same optional label */ XMEMSET(plain, 0, sizeof(plain)); do { @@ -5129,18 +6496,22 @@ int rsa_test(void) } while (ret == WC_PENDING_E); if (ret < 0) { XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); - wc_FreeRng(&rng); + wc_FreeRng(&rng); return -444; } if (XMEMCMP(plain, in, inLen)) { XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); - wc_FreeRng(&rng); + wc_FreeRng(&rng); return -445; } #endif /* !HAVE_FAST_RSA && !HAVE_FIPS */ #endif /* WC_NO_RSA_OAEP */ + ret = rsa_flatten_test(&key); + if (ret != 0) + return ret; + #if defined(WOLFSSL_MDK_ARM) #define sizeof(s) XSTRLEN((char *)(s)) #endif @@ -5155,7 +6526,7 @@ int rsa_test(void) file2 = fopen(clientCert, "rb"); if (!file2) { XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); - wc_FreeRng(&rng); + wc_FreeRng(&rng); return -49; } @@ -5197,7 +6568,7 @@ int rsa_test(void) err_sys("can't open ./certs/client-keyPub.der, " "Please run from wolfSSL home dir", -40); XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); - wc_FreeRng(&rng); + wc_FreeRng(&rng); return -50; } @@ -5208,7 +6579,7 @@ int rsa_test(void) ret = wc_InitRsaKey(&keypub, HEAP_HINT); if (ret != 0) { XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); - wc_FreeRng(&rng); + wc_FreeRng(&rng); return -51; } idx = 0; @@ -5217,7 +6588,7 @@ int rsa_test(void) if (ret != 0) { XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); wc_FreeRsaKey(&keypub); - wc_FreeRng(&rng); + wc_FreeRng(&rng); return -52; } #endif /* WOLFSSL_CERT_EXT */ @@ -5236,13 +6607,13 @@ int rsa_test(void) ret = wc_InitRsaKey(&genKey, HEAP_HINT); if (ret != 0) { XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); - wc_FreeRng(&rng); + wc_FreeRng(&rng); return -300; } ret = wc_MakeRsaKey(&genKey, 1024, 65537, &rng); if (ret != 0) { XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); - wc_FreeRng(&rng); + wc_FreeRng(&rng); return -301; } @@ -5250,7 +6621,7 @@ int rsa_test(void) if (der == NULL) { XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); wc_FreeRsaKey(&genKey); - wc_FreeRng(&rng); + wc_FreeRng(&rng); return -307; } pem = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); @@ -5258,7 +6629,7 @@ int rsa_test(void) XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); wc_FreeRsaKey(&genKey); - wc_FreeRng(&rng); + wc_FreeRng(&rng); return -308; } @@ -5267,7 +6638,7 @@ int rsa_test(void) XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); - wc_FreeRng(&rng); + wc_FreeRng(&rng); return -302; } @@ -5281,7 +6652,7 @@ int rsa_test(void) XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); wc_FreeRsaKey(&genKey); - wc_FreeRng(&rng); + wc_FreeRng(&rng); return -303; } ret = (int)fwrite(der, 1, derSz, keyFile); @@ -5291,7 +6662,7 @@ int rsa_test(void) XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); wc_FreeRsaKey(&genKey); - wc_FreeRng(&rng); + wc_FreeRng(&rng); return -313; } @@ -5301,7 +6672,7 @@ int rsa_test(void) XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); wc_FreeRsaKey(&genKey); - wc_FreeRng(&rng); + wc_FreeRng(&rng); return -304; } @@ -5315,7 +6686,7 @@ int rsa_test(void) XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); wc_FreeRsaKey(&genKey); - wc_FreeRng(&rng); + wc_FreeRng(&rng); return -305; } ret = (int)fwrite(pem, 1, pemSz, pemFile); @@ -5325,7 +6696,7 @@ int rsa_test(void) XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); wc_FreeRsaKey(&genKey); - wc_FreeRng(&rng); + wc_FreeRng(&rng); return -314; } @@ -5335,7 +6706,7 @@ int rsa_test(void) XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); wc_FreeRsaKey(&genKey); - wc_FreeRng(&rng); + wc_FreeRng(&rng); return -3060; } idx = 0; @@ -5346,7 +6717,7 @@ int rsa_test(void) XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); wc_FreeRsaKey(&derIn); wc_FreeRsaKey(&genKey); - wc_FreeRng(&rng); + wc_FreeRng(&rng); return -306; } @@ -5375,14 +6746,14 @@ int rsa_test(void) DYNAMIC_TYPE_TMP_BUFFER); if (derCert == NULL) { XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); - wc_FreeRng(&rng); + wc_FreeRng(&rng); return -309; } pem = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,DYNAMIC_TYPE_TMP_BUFFER); if (pem == NULL) { XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(derCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); - wc_FreeRng(&rng); + wc_FreeRng(&rng); return -310; } @@ -5411,7 +6782,7 @@ int rsa_test(void) XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(derCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); - wc_FreeRng(&rng); + wc_FreeRng(&rng); return -398; } @@ -5420,7 +6791,7 @@ int rsa_test(void) XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(derCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); - wc_FreeRng(&rng); + wc_FreeRng(&rng); return -399; } @@ -5429,7 +6800,7 @@ int rsa_test(void) XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(derCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); - wc_FreeRng(&rng); + wc_FreeRng(&rng); return -400; } #endif /* WOLFSSL_CERT_EXT */ @@ -5439,7 +6810,7 @@ int rsa_test(void) XFREE(derCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); - wc_FreeRng(&rng); + wc_FreeRng(&rng); return -401; } @@ -5450,7 +6821,7 @@ int rsa_test(void) XFREE(derCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); - wc_FreeRng(&rng); + wc_FreeRng(&rng); return -402; } FreeDecodedCert(&decode); @@ -5465,7 +6836,7 @@ int rsa_test(void) XFREE(derCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); - wc_FreeRng(&rng); + wc_FreeRng(&rng); return -403; } ret = (int)fwrite(derCert, 1, certSz, derFile); @@ -5474,7 +6845,7 @@ int rsa_test(void) XFREE(derCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); - wc_FreeRng(&rng); + wc_FreeRng(&rng); return -414; } @@ -5483,7 +6854,7 @@ int rsa_test(void) XFREE(derCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); - wc_FreeRng(&rng); + wc_FreeRng(&rng); return -404; } @@ -5496,7 +6867,7 @@ int rsa_test(void) XFREE(derCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); - wc_FreeRng(&rng); + wc_FreeRng(&rng); return -405; } ret = (int)fwrite(pem, 1, pemSz, pemFile); @@ -5505,7 +6876,7 @@ int rsa_test(void) XFREE(derCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); - wc_FreeRng(&rng); + wc_FreeRng(&rng); return -406; } XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); @@ -5532,14 +6903,14 @@ int rsa_test(void) DYNAMIC_TYPE_TMP_BUFFER); if (derCert == NULL) { XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); - wc_FreeRng(&rng); + wc_FreeRng(&rng); return -311; } pem = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,DYNAMIC_TYPE_TMP_BUFFER); if (pem == NULL) { XFREE(derCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); - wc_FreeRng(&rng); + wc_FreeRng(&rng); return -312; } @@ -5549,7 +6920,7 @@ int rsa_test(void) XFREE(derCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); - wc_FreeRng(&rng); + wc_FreeRng(&rng); return -412; } @@ -5561,7 +6932,7 @@ int rsa_test(void) XFREE(derCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); - wc_FreeRng(&rng); + wc_FreeRng(&rng); return -411; } ret = wc_RsaPrivateKeyDecode(tmp, &idx3, &caKey, (word32)bytes3); @@ -5570,7 +6941,7 @@ int rsa_test(void) XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); wc_FreeRsaKey(&caKey); - wc_FreeRng(&rng); + wc_FreeRng(&rng); return -413; } @@ -5599,7 +6970,7 @@ int rsa_test(void) XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(derCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); - wc_FreeRng(&rng); + wc_FreeRng(&rng); return -398; } @@ -5608,7 +6979,7 @@ int rsa_test(void) XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(derCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); - wc_FreeRng(&rng); + wc_FreeRng(&rng); return -399; } @@ -5617,7 +6988,7 @@ int rsa_test(void) XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(derCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); - wc_FreeRng(&rng); + wc_FreeRng(&rng); return -400; } #endif /* WOLFSSL_CERT_EXT */ @@ -5628,7 +6999,7 @@ int rsa_test(void) XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); wc_FreeRsaKey(&caKey); - wc_FreeRng(&rng); + wc_FreeRng(&rng); return -405; } @@ -5638,7 +7009,7 @@ int rsa_test(void) XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); wc_FreeRsaKey(&caKey); - wc_FreeRng(&rng); + wc_FreeRng(&rng); return -407; } @@ -5649,7 +7020,7 @@ int rsa_test(void) XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); wc_FreeRsaKey(&caKey); - wc_FreeRng(&rng); + wc_FreeRng(&rng); return -408; } @@ -5661,7 +7032,7 @@ int rsa_test(void) XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); wc_FreeRsaKey(&caKey); - wc_FreeRng(&rng); + wc_FreeRng(&rng); return -409; } FreeDecodedCert(&decode); @@ -5677,7 +7048,7 @@ int rsa_test(void) XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); wc_FreeRsaKey(&caKey); - wc_FreeRng(&rng); + wc_FreeRng(&rng); return -410; } ret = (int)fwrite(derCert, 1, certSz, derFile); @@ -5687,7 +7058,7 @@ int rsa_test(void) XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); wc_FreeRsaKey(&caKey); - wc_FreeRng(&rng); + wc_FreeRng(&rng); return -416; } @@ -5697,7 +7068,7 @@ int rsa_test(void) XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); wc_FreeRsaKey(&caKey); - wc_FreeRng(&rng); + wc_FreeRng(&rng); return -411; } @@ -5711,7 +7082,7 @@ int rsa_test(void) XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); wc_FreeRsaKey(&caKey); - wc_FreeRng(&rng); + wc_FreeRng(&rng); return -412; } ret = (int)fwrite(pem, 1, pemSz, pemFile); @@ -5721,7 +7092,7 @@ int rsa_test(void) XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); fclose(pemFile); wc_FreeRsaKey(&caKey); - wc_FreeRng(&rng); + wc_FreeRng(&rng); return -415; } fclose(pemFile); @@ -5754,14 +7125,14 @@ int rsa_test(void) DYNAMIC_TYPE_TMP_BUFFER); if (derCert == NULL) { XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); - wc_FreeRng(&rng); + wc_FreeRng(&rng); return -5311; } pem = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,DYNAMIC_TYPE_TMP_BUFFER); if (pem == NULL) { XFREE(derCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); - wc_FreeRng(&rng); + wc_FreeRng(&rng); return -5312; } @@ -5771,7 +7142,7 @@ int rsa_test(void) XFREE(derCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); - wc_FreeRng(&rng); + wc_FreeRng(&rng); return -5412; } @@ -5784,7 +7155,7 @@ int rsa_test(void) XFREE(derCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); - wc_FreeRng(&rng); + wc_FreeRng(&rng); return -5413; } @@ -5813,7 +7184,7 @@ int rsa_test(void) XFREE(derCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); - wc_FreeRng(&rng); + wc_FreeRng(&rng); return -5500; } @@ -5825,7 +7196,7 @@ int rsa_test(void) XFREE(derCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); - wc_FreeRng(&rng); + wc_FreeRng(&rng); return -5501; } @@ -5836,7 +7207,7 @@ int rsa_test(void) XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); wc_ecc_free(&caKeyPub); - wc_FreeRng(&rng); + wc_FreeRng(&rng); return -5502; } @@ -5846,7 +7217,7 @@ int rsa_test(void) XFREE(derCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); wc_ecc_free(&caKeyPub); - wc_FreeRng(&rng); + wc_FreeRng(&rng); return -5503; } @@ -5856,7 +7227,7 @@ int rsa_test(void) XFREE(derCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); wc_ecc_free(&caKeyPub); - wc_FreeRng(&rng); + wc_FreeRng(&rng); return -5504; } wc_ecc_free(&caKeyPub); @@ -5866,7 +7237,7 @@ int rsa_test(void) XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(derCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); - wc_FreeRng(&rng); + wc_FreeRng(&rng); return -5505; } #endif /* WOLFSSL_CERT_EXT */ @@ -5877,7 +7248,7 @@ int rsa_test(void) XFREE(derCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); wc_ecc_free(&caKey); XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); - wc_FreeRng(&rng); + wc_FreeRng(&rng); return -5405; } @@ -5887,7 +7258,7 @@ int rsa_test(void) XFREE(derCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); wc_ecc_free(&caKey); XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); - wc_FreeRng(&rng); + wc_FreeRng(&rng); return -5407; } @@ -5898,7 +7269,7 @@ int rsa_test(void) XFREE(derCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); wc_ecc_free(&caKey); XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); - wc_FreeRng(&rng); + wc_FreeRng(&rng); return -5408; } @@ -5910,7 +7281,7 @@ int rsa_test(void) XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(derCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); wc_ecc_free(&caKey); - wc_FreeRng(&rng); + wc_FreeRng(&rng); return -5409; } FreeDecodedCert(&decode); @@ -5926,7 +7297,7 @@ int rsa_test(void) XFREE(derCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); wc_ecc_free(&caKey); XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); - wc_FreeRng(&rng); + wc_FreeRng(&rng); return -5410; } ret = (int)fwrite(derCert, 1, certSz, derFile); @@ -5936,7 +7307,7 @@ int rsa_test(void) XFREE(derCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); wc_ecc_free(&caKey); XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); - wc_FreeRng(&rng); + wc_FreeRng(&rng); return -5414; } @@ -5946,7 +7317,7 @@ int rsa_test(void) XFREE(derCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); wc_ecc_free(&caKey); XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); - wc_FreeRng(&rng); + wc_FreeRng(&rng); return -5411; } @@ -5960,7 +7331,7 @@ int rsa_test(void) XFREE(derCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); wc_ecc_free(&caKey); XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); - wc_FreeRng(&rng); + wc_FreeRng(&rng); return -5412; } ret = (int)fwrite(pem, 1, pemSz, pemFile); @@ -5969,7 +7340,7 @@ int rsa_test(void) XFREE(derCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); wc_ecc_free(&caKey); XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); - wc_FreeRng(&rng); + wc_FreeRng(&rng); return -5415; } @@ -5999,14 +7370,14 @@ int rsa_test(void) DYNAMIC_TYPE_TMP_BUFFER); if (derCert == NULL) { XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); - wc_FreeRng(&rng); + wc_FreeRng(&rng); return -311; } pem = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,DYNAMIC_TYPE_TMP_BUFFER); if (pem == NULL) { XFREE(derCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); - wc_FreeRng(&rng); + wc_FreeRng(&rng); return -312; } @@ -6024,7 +7395,7 @@ int rsa_test(void) XFREE(derCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); - wc_FreeRng(&rng); + wc_FreeRng(&rng); return -448; } @@ -6035,7 +7406,7 @@ int rsa_test(void) XFREE(derCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); - wc_FreeRng(&rng); + wc_FreeRng(&rng); return -449; } @@ -6046,7 +7417,7 @@ int rsa_test(void) XFREE(derCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); - wc_FreeRng(&rng); + wc_FreeRng(&rng); return -450; } @@ -6056,7 +7427,7 @@ int rsa_test(void) XFREE(derCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); - wc_FreeRng(&rng); + wc_FreeRng(&rng); return -451; } @@ -6066,7 +7437,7 @@ int rsa_test(void) XFREE(derCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); - wc_FreeRng(&rng); + wc_FreeRng(&rng); return -452; } @@ -6078,7 +7449,7 @@ int rsa_test(void) XFREE(derCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); - wc_FreeRng(&rng); + wc_FreeRng(&rng); return -453; } ret = wc_RsaPrivateKeyDecode(tmp, &idx3, &caKey, (word32)bytes); @@ -6086,7 +7457,7 @@ int rsa_test(void) XFREE(derCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); - wc_FreeRng(&rng); + wc_FreeRng(&rng); return -454; } @@ -6109,7 +7480,7 @@ int rsa_test(void) XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(derCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); - wc_FreeRng(&rng); + wc_FreeRng(&rng); return -496; } @@ -6118,7 +7489,7 @@ int rsa_test(void) XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(derCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); - wc_FreeRng(&rng); + wc_FreeRng(&rng); return -495; } @@ -6128,7 +7499,7 @@ int rsa_test(void) XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(derCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); - wc_FreeRng(&rng); + wc_FreeRng(&rng); return -494; } #endif /* WOLFSSL_CERT_EXT */ @@ -6139,7 +7510,7 @@ int rsa_test(void) XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); wc_FreeRsaKey(&caKey); XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); - wc_FreeRng(&rng); + wc_FreeRng(&rng); return -455; } @@ -6150,7 +7521,7 @@ int rsa_test(void) XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); wc_FreeRsaKey(&caKey); XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); - wc_FreeRng(&rng); + wc_FreeRng(&rng); return -456; } @@ -6161,7 +7532,7 @@ int rsa_test(void) XFREE(derCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); - wc_FreeRng(&rng); + wc_FreeRng(&rng); return -457; } @@ -6173,7 +7544,7 @@ int rsa_test(void) XFREE(derCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); - wc_FreeRng(&rng); + wc_FreeRng(&rng); return -458; } FreeDecodedCert(&decode); @@ -6183,7 +7554,7 @@ int rsa_test(void) XFREE(derCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); - wc_FreeRng(&rng); + wc_FreeRng(&rng); return -459; } ret = (int)fwrite(derCert, 1, certSz, derFile); @@ -6192,7 +7563,7 @@ int rsa_test(void) XFREE(derCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); - wc_FreeRng(&rng); + wc_FreeRng(&rng); return -473; } @@ -6201,7 +7572,7 @@ int rsa_test(void) XFREE(derCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); - wc_FreeRng(&rng); + wc_FreeRng(&rng); return -460; } @@ -6210,7 +7581,7 @@ int rsa_test(void) XFREE(derCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); - wc_FreeRng(&rng); + wc_FreeRng(&rng); return -461; } ret = (int)fwrite(pem, 1, pemSz, pemFile); @@ -6219,7 +7590,7 @@ int rsa_test(void) XFREE(derCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); - wc_FreeRng(&rng); + wc_FreeRng(&rng); return -474; } @@ -6228,7 +7599,7 @@ int rsa_test(void) XFREE(derCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); - wc_FreeRng(&rng); + wc_FreeRng(&rng); return -462; } ret = (int)fwrite(private_key, 1, private_key_len, ntruPrivFile); @@ -6237,7 +7608,7 @@ int rsa_test(void) XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(derCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); - wc_FreeRng(&rng); + wc_FreeRng(&rng); return -475; } XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); @@ -6256,14 +7627,14 @@ int rsa_test(void) der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,DYNAMIC_TYPE_TMP_BUFFER); if (der == NULL) { XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); - wc_FreeRng(&rng); + wc_FreeRng(&rng); return -463; } pem = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,DYNAMIC_TYPE_TMP_BUFFER); if (pem == NULL) { XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); - wc_FreeRng(&rng); + wc_FreeRng(&rng); return -464; } @@ -6287,7 +7658,7 @@ int rsa_test(void) XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); - wc_FreeRng(&rng); + wc_FreeRng(&rng); return -496; } @@ -6297,7 +7668,7 @@ int rsa_test(void) XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); - wc_FreeRng(&rng); + wc_FreeRng(&rng); return -494; } #endif /* WOLFSSL_CERT_EXT */ @@ -6307,7 +7678,7 @@ int rsa_test(void) XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); - wc_FreeRng(&rng); + wc_FreeRng(&rng); return -465; } @@ -6317,7 +7688,7 @@ int rsa_test(void) XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); - wc_FreeRng(&rng); + wc_FreeRng(&rng); return -466; } @@ -6326,7 +7697,7 @@ int rsa_test(void) XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); - wc_FreeRng(&rng); + wc_FreeRng(&rng); return -467; } @@ -6339,7 +7710,7 @@ int rsa_test(void) XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); - wc_FreeRng(&rng); + wc_FreeRng(&rng); return -468; } @@ -6349,7 +7720,7 @@ int rsa_test(void) XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); - wc_FreeRng(&rng); + wc_FreeRng(&rng); return -471; } @@ -6362,7 +7733,7 @@ int rsa_test(void) XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); - wc_FreeRng(&rng); + wc_FreeRng(&rng); return -469; } ret = (int)fwrite(pem, 1, pemSz, reqFile); @@ -6371,7 +7742,7 @@ int rsa_test(void) XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); - wc_FreeRng(&rng); + wc_FreeRng(&rng); return -470; } @@ -6409,6 +7780,48 @@ int rsa_test(void) #endif #endif +static int dh_generate_test(WC_RNG *rng) +{ + int ret; + DhKey smallKey; + byte p[2] = { 0, 5 }; + byte g[2] = { 0, 2 }; + byte priv[2]; + word32 privSz = sizeof(priv); + byte pub[2]; + word32 pubSz = sizeof(pub); + + wc_InitDhKey(&smallKey); + + /* Parameter Validation testing. */ + ret = wc_DhSetKey(NULL, p, sizeof(p), g, sizeof(g)); + if (ret != BAD_FUNC_ARG) + return -100; + ret = wc_DhSetKey(&smallKey, NULL, sizeof(p), g, sizeof(g)); + if (ret != BAD_FUNC_ARG) + return -100; + ret = wc_DhSetKey(&smallKey, p, 0, g, sizeof(g)); + if (ret != BAD_FUNC_ARG) + return -100; + ret = wc_DhSetKey(&smallKey, p, sizeof(p), NULL, sizeof(g)); + if (ret != BAD_FUNC_ARG) + return -100; + ret = wc_DhSetKey(&smallKey, p, sizeof(p), g, 0); + if (ret != BAD_FUNC_ARG) + return -100; + ret = wc_DhSetKey(&smallKey, p, sizeof(p), g, sizeof(g)); + if (ret != 0) + return -101; + + /* Use API. */ + ret = wc_DhGenerateKeyPair(&smallKey, rng, priv, &privSz, pub, &pubSz); + wc_FreeDhKey(&smallKey); + if (ret != 0) + return -102; + + return 0; +} + int dh_test(void) { int ret; @@ -6490,6 +7903,10 @@ int dh_test(void) if (XMEMCMP(agree, agree2, agreeSz)) return -56; + ret = dh_generate_test(&rng); + if (ret != 0) + return -57; + wc_FreeDhKey(&key); wc_FreeDhKey(&key2); wc_FreeRng(&rng); @@ -7456,7 +8873,7 @@ int openssl_test(void) return -3407; total += outlen; if(total != 32) - return 3408; + return -3408; total = 0; EVP_CIPHER_CTX_init(&de); @@ -7488,7 +8905,7 @@ int openssl_test(void) total += outlen; if(total != 18) - return 3427; + return -3427; if (XMEMCMP(plain, cbcPlain, 18)) return -3428; @@ -8425,7 +9842,7 @@ static int ecc_test_curve_size(WC_RNG* rng, int keySize, int testVerifyCount, #ifdef HAVE_ECC_KEY_EXPORT x = sizeof(exportBuf); - ret = wc_ecc_export_x963(&userA, exportBuf, &x); + ret = wc_ecc_export_x963_ex(&userA, exportBuf, &x, 0); if (ret != 0) goto done; @@ -8564,11 +9981,533 @@ static int ecc_test_curve(WC_RNG* rng, int keySize) return 0; } +#if !defined(WOLFSSL_ATECC508A) && defined(HAVE_ECC_KEY_IMPORT) && \ + defined(HAVE_ECC_KEY_EXPORT) +static int ecc_point_test() +{ + int ret; + ecc_point* point; + ecc_point* point2; + word32 outLen; + byte out[65]; + byte der[] = { 0x04, /* = Uncompressed */ + 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, + 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, + 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, + 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, + 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, + 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, + 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, + 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 }; +#ifdef HAVE_COMP_KEY + byte derComp0[] = { 0x02, /* = Compressed, y even */ + 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, + 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, + 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, + 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 }; + byte derComp1[] = { 0x03, /* = Compressed, y odd */ + 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, + 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, + 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, + 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 }; +#endif + byte altDer[] = { 0x04, /* = Uncompressed */ + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 }; + + outLen = sizeof(out); + point = wc_ecc_new_point(); + if (point == NULL) + return -1035; + point2 = wc_ecc_new_point(); + if (point2 == NULL) { + wc_ecc_del_point(point); + return -1036; + } + + /* Parameter Validation testing. */ + wc_ecc_del_point(NULL); + ret = wc_ecc_import_point_der(NULL, sizeof(der), 6, point); + if (ret != ECC_BAD_ARG_E) { + ret = -1037; + goto done; + } + ret = wc_ecc_import_point_der(der, sizeof(der), -1, point); + if (ret != ECC_BAD_ARG_E) { + ret = -1038; + goto done; + } + ret = wc_ecc_import_point_der(der, sizeof(der), 6, NULL); + if (ret != ECC_BAD_ARG_E) { + ret = -1039; + goto done; + } + ret = wc_ecc_export_point_der(-1, point, out, &outLen); + if (ret != ECC_BAD_ARG_E) { + ret = -1040; + goto done; + } + ret = wc_ecc_export_point_der(6, NULL, out, &outLen); + if (ret != ECC_BAD_ARG_E) { + ret = -1041; + goto done; + } + ret = wc_ecc_export_point_der(6, point, NULL, &outLen); + if (ret != LENGTH_ONLY_E || outLen != sizeof(out)) { + ret = -1043; + goto done; + } + ret = wc_ecc_export_point_der(6, point, out, NULL); + if (ret != ECC_BAD_ARG_E) { + ret = -1043; + goto done; + } + outLen = 0; + ret = wc_ecc_export_point_der(6, point, out, &outLen); + if (ret != BUFFER_E) { + ret = -1044; + goto done; + } + ret = wc_ecc_copy_point(NULL, NULL); + if (ret != ECC_BAD_ARG_E) { + ret = -1045; + goto done; + } + ret = wc_ecc_copy_point(NULL, point2); + if (ret != ECC_BAD_ARG_E) { + ret = -1046; + goto done; + } + ret = wc_ecc_copy_point(point, NULL); + if (ret != ECC_BAD_ARG_E) { + ret = -1047; + goto done; + } + ret = wc_ecc_cmp_point(NULL, NULL); + if (ret != BAD_FUNC_ARG) { + ret = -1048; + goto done; + } + ret = wc_ecc_cmp_point(NULL, point2); + if (ret != BAD_FUNC_ARG) { + ret = -1049; + goto done; + } + ret = wc_ecc_cmp_point(point, NULL); + if (ret != BAD_FUNC_ARG) { + ret = -1050; + goto done; + } + + /* Use API. */ + ret = wc_ecc_import_point_der(der, sizeof(der), 6, point); + if (ret != 0) { + ret = -1051; + goto done; + } + + outLen = sizeof(out); + ret = wc_ecc_export_point_der(6, point, out, &outLen); + if (ret != 0) { + ret = -1052; + goto done; + } + if (outLen != sizeof(der)) { + ret = -1053; + goto done; + } + if (XMEMCMP(out, der, outLen) != 0) { + ret = -1054; + goto done; + } + + ret = wc_ecc_copy_point(point2, point); + if (ret != MP_OKAY) { + ret = -1055; + goto done; + } + ret = wc_ecc_cmp_point(point2, point); + if (ret != MP_EQ) { + ret = -1056; + goto done; + } + + ret = wc_ecc_import_point_der(altDer, sizeof(altDer), 6, point2); + if (ret != 0) { + ret = -1057; + goto done; + } + ret = wc_ecc_cmp_point(point2, point); + if (ret != MP_GT) { + ret = -1058; + goto done; + } + +#ifdef HAVE_COMP_KEY + /* TODO: Doesn't work. */ + ret = wc_ecc_import_point_der(derComp0, sizeof(der), 6, point); + if (ret != 0) { + ret = -1059; + goto done; + } + + ret = wc_ecc_import_point_der(derComp1, sizeof(der), 6, point); + if (ret != 0) { + ret = -1060; + goto done; + } +#endif + +done: + wc_ecc_del_point(point2); + wc_ecc_del_point(point); + + return ret; +} +#endif /* !WOLFSSL_ATECC508A && HAVE_ECC_KEY_IMPORT && HAVE_ECC_KEY_EXPORT */ + +#ifndef NO_SIG_WRAPPER +static int ecc_sig_test(WC_RNG* rng, ecc_key* key) +{ + int ret; + word32 sigSz; + int size; + byte out[65]; + byte in[] = "Everyone gets Friday off."; + word32 inLen = (word32)XSTRLEN((char*)in); + + size = wc_ecc_sig_size(key); + + ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_ECC, key, sizeof(*key)); + if (ret != size) + return -1030; + + sigSz = ret; + ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_ECC, in, + inLen, out, &sigSz, key, sizeof(*key), rng); + if (ret != 0) + return -1031; + + ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_ECC, in, + inLen, out, sigSz, key, sizeof(*key)); + if (ret != 0) + return -1032; + + return 0; +} +#endif + +#if defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT) +static int ecc_exp_imp_test(ecc_key* key) +{ + int ret; + ecc_key keyImp; + byte priv[32]; + word32 privLen; + byte pub[65]; + word32 pubLen; + const char qx[] = "01020304050607080102030405060708" + "01020304050607080102030405060708"; + const char qy[] = "01020304050607080102030405060708" + "01020304050607080102030405060708"; + const char d[] = "01020304050607080102030405060708"; + + wc_ecc_init(&keyImp); + + privLen = sizeof(priv); + ret = wc_ecc_export_private_only(key, priv, &privLen); + if (ret != 0) { + ret = -1070; + goto done; + } + pubLen = sizeof(pub); + ret = wc_ecc_export_point_der(key->idx, &key->pubkey, pub, &pubLen); + if (ret != 0) { + ret = -1071; + goto done; + } + + ret = wc_ecc_import_private_key(priv, privLen, pub, pubLen, &keyImp); + if (ret != 0) { + ret = -1072; + goto done; + } + + ret = wc_ecc_import_raw_ex(&keyImp, qx, qy, d, ECC_SECP256R1); + if (ret != 0) { + ret = -1073; + goto done; + } + +done: + wc_ecc_free(&keyImp); + return ret; +} +#endif /* HAVE_ECC_KEY_IMPORT && HAVE_ECC_KEY_EXPORT */ + +#ifdef HAVE_ECC_KEY_IMPORT +static int ecc_mulmod_test(ecc_key* key1) +{ + int ret; + ecc_key key2; + ecc_key key3; + + wc_ecc_init(&key2); + wc_ecc_init(&key3); + + /* TODO: Use test data. */ + /* Need base point (Gx,Gy) and parameter A - load them as the public and + * private key in key2. + */ + ret = wc_ecc_import_raw_ex(&key2, key1->dp->Gx, key1->dp->Gy, key1->dp->Af, + ECC_SECP256R1); + if (ret != 0) + goto done; + + /* Need a point (Gx,Gy) and prime - load them as the public and private key + * in key3. + */ + ret = wc_ecc_import_raw_ex(&key3, key1->dp->Gx, key1->dp->Gy, + key1->dp->prime, ECC_SECP256R1); + if (ret != 0) + goto done; + + ret = wc_ecc_mulmod(&key1->k, &key2.pubkey, &key3.pubkey, &key2.k, &key3.k, + 1); + if (ret != 0) { + ret = -1080; + goto done; + } + +done: + wc_ecc_free(&key3); + wc_ecc_free(&key2); + return ret; +} +#endif + +#ifndef WOLFSSL_ATECC508A +static int ecc_ssh_test(ecc_key* key) +{ + int ret; + byte out[128]; + word32 outLen = sizeof(out); + + /* Parameter Validation testing. */ + ret = wc_ecc_shared_secret_ssh(NULL, &key->pubkey, out, &outLen); + if (ret != BAD_FUNC_ARG) + return -1090; + ret = wc_ecc_shared_secret_ssh(key, NULL, out, &outLen); + if (ret != BAD_FUNC_ARG) + return -1091; + ret = wc_ecc_shared_secret_ssh(key, &key->pubkey, NULL, &outLen); + if (ret != BAD_FUNC_ARG) + return -1092; + ret = wc_ecc_shared_secret_ssh(key, &key->pubkey, out, NULL); + if (ret != BAD_FUNC_ARG) + return -1093; + + /* Use API. */ + ret = wc_ecc_shared_secret_ssh(key, &key->pubkey, out, &outLen); + if (ret != 0) + return -1094; + return 0; +} +#endif + +static int ecc_def_curve_test(WC_RNG *rng) +{ + int ret; + ecc_key key; + + wc_ecc_init(&key); + + ret = wc_ecc_make_key(rng, 32, &key); + if (ret != 0) { + ret = -1030; + goto done; + } + +#ifndef NO_SIG_WRAPPER + ret = ecc_sig_test(rng, &key); + if (ret < 0) + goto done; +#endif +#if defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT) + ret = ecc_exp_imp_test(&key); + if (ret < 0) + goto done; +#endif +#ifdef HAVE_ECC_KEY_IMPORT + ret = ecc_mulmod_test(&key); + if (ret < 0) + goto done; +#endif +#ifndef WOLFSSL_ATECC508A + ret = ecc_ssh_test(&key); + if (ret < 0) + goto done; +#endif +done: + wc_ecc_free(&key); + return ret; +} + +#ifdef WOLFSSL_CERT_EXT +static int ecc_decode_test(void) +{ + int ret; + word32 inSz; + word32 inOutIdx; + ecc_key key; + const byte good[] = { 0x30, 0x0d, 0x30, 0x0b, 0x06, 0x00, 0x06, 0x01, 0x01, + 0x03, 0x04, 0x00, 0x04, 0x01, 0x01 }; + const byte badNoObjId[] = { 0x30, 0x08, 0x30, 0x06, 0x03, 0x04, + 0x00, 0x04, 0x01, 0x01 }; + const byte badOneObjId[] = { 0x30, 0x0a, 0x30, 0x08, 0x06, 0x00, 0x03, 0x04, + 0x00, 0x04, 0x01, 0x01 }; + const byte badObjId1Len[] = { 0x30, 0x0c, 0x30, 0x0a, 0x06, 0x09, + 0x06, 0x00, 0x03, 0x04, 0x00, 0x04, 0x01, 0x01 }; + const byte badObj2d1Len[] = { 0x30, 0x0c, 0x30, 0x0a, 0x06, 0x00, + 0x06, 0x07, 0x03, 0x04, 0x00, 0x04, 0x01, 0x01 }; + const byte badNotBitStr[] = { 0x30, 0x0d, 0x30, 0x0b, 0x06, 0x00, + 0x06, 0x01, 0x01, 0x04, 0x04, 0x00, 0x04, 0x01, 0x01 }; + const byte badBitStrLen[] = { 0x30, 0x0d, 0x30, 0x0b, 0x06, 0x00, + 0x06, 0x01, 0x01, 0x03, 0x05, 0x00, 0x04, 0x01, 0x01 }; + const byte badNoBitStrZero[] = { 0x30, 0x0c, 0x30, 0x0a, 0x06, 0x00, + 0x06, 0x01, 0x01, 0x03, 0x03, 0x04, 0x01, 0x01 }; + const byte badPoint[] = { 0x30, 0x0b, 0x30, 0x09, 0x06, 0x00, 0x06, 0x01, + 0x01, 0x03, 0x03, 0x00, 0x04, 0x01 }; + + XMEMSET(&key, 0, sizeof(key)); + wc_ecc_init(&key); + + inSz = sizeof(good); + ret = wc_EccPublicKeyDecode(NULL, &inOutIdx, &key, inSz); + if (ret != BAD_FUNC_ARG) { + ret = -1100; + goto done; + } + ret = wc_EccPublicKeyDecode(good, NULL, &key, inSz); + if (ret != BAD_FUNC_ARG) { + ret = -1101; + goto done; + } + ret = wc_EccPublicKeyDecode(good, &inOutIdx, NULL, inSz); + if (ret != BAD_FUNC_ARG) { + ret = -1102; + goto done; + } + ret = wc_EccPublicKeyDecode(good, &inOutIdx, &key, 0); + if (ret != BAD_FUNC_ARG) { + ret = -1103; + goto done; + } + + /* Change offset to produce bad input data. */ + inOutIdx = 2; + inSz = sizeof(good) - inOutIdx; + ret = wc_EccPublicKeyDecode(good, &inOutIdx, &key, inSz); + if (ret != ASN_PARSE_E) { + ret = -1104; + goto done; + } + inOutIdx = 4; + inSz = sizeof(good) - inOutIdx; + ret = wc_EccPublicKeyDecode(good, &inOutIdx, &key, inSz); + if (ret != ASN_PARSE_E) { + ret = -1105; + goto done; + } + /* Bad data. */ + inSz = sizeof(badNoObjId); + inOutIdx = 0; + ret = wc_EccPublicKeyDecode(badNoObjId, &inOutIdx, &key, inSz); + if (ret != ASN_OBJECT_ID_E) { + ret = -1106; + goto done; + } + inSz = sizeof(badOneObjId); + inOutIdx = 0; + ret = wc_EccPublicKeyDecode(badOneObjId, &inOutIdx, &key, inSz); + if (ret != ASN_OBJECT_ID_E) { + ret = -1107; + goto done; + } + inSz = sizeof(badObjId1Len); + inOutIdx = 0; + ret = wc_EccPublicKeyDecode(badObjId1Len, &inOutIdx, &key, inSz); + if (ret != ASN_PARSE_E) { + ret = -1108; + goto done; + } + inSz = sizeof(badObj2d1Len); + inOutIdx = 0; + ret = wc_EccPublicKeyDecode(badObj2d1Len, &inOutIdx, &key, inSz); + if (ret != ASN_PARSE_E) { + ret = -1109; + goto done; + } + inSz = sizeof(badNotBitStr); + inOutIdx = 0; + ret = wc_EccPublicKeyDecode(badNotBitStr, &inOutIdx, &key, inSz); + if (ret != ASN_BITSTR_E) { + ret = -1110; + goto done; + } + inSz = sizeof(badBitStrLen); + inOutIdx = 0; + ret = wc_EccPublicKeyDecode(badBitStrLen, &inOutIdx, &key, inSz); + if (ret != ASN_PARSE_E) { + ret = -1111; + goto done; + } + inSz = sizeof(badNoBitStrZero); + inOutIdx = 0; + ret = wc_EccPublicKeyDecode(badNoBitStrZero, &inOutIdx, &key, inSz); + if (ret != ASN_EXPECT_0_E) { + ret = -1112; + goto done; + } + inSz = sizeof(badPoint); + inOutIdx = 0; + ret = wc_EccPublicKeyDecode(badPoint, &inOutIdx, &key, inSz); + if (ret != ASN_ECC_KEY_E) { + ret = -1113; + goto done; + } + + inSz = sizeof(good); + inOutIdx = 0; + ret = wc_EccPublicKeyDecode(good, &inOutIdx, &key, inSz); + if (ret != 0) { + ret = -1120; + goto done; + } + +done: + wc_ecc_free(&key); + return ret; +} +#endif /* WOLFSSL_CERT_EXT */ + int ecc_test(void) { int ret; WC_RNG rng; +#ifdef WOLFSSL_CERT_EXT + ret = ecc_decode_test(); + if (ret < 0) + return ret; +#endif + ret = wc_InitRng(&rng); if (ret != 0) return -1001; @@ -8614,6 +10553,17 @@ int ecc_test(void) if (ret < 0) { goto done; } +#if !defined(WOLFSSL_ATECC508A) && defined(HAVE_ECC_KEY_IMPORT) && \ + defined(HAVE_ECC_KEY_EXPORT) + ret = ecc_point_test(); + if (ret < 0) { + goto done; + } +#endif + ret = ecc_def_curve_test(&rng); + if (ret < 0) { + goto done; + } #endif /* !NO_ECC256 */ #if defined(HAVE_ECC320) || defined(HAVE_ALL_CURVES) ret = ecc_test_curve(&rng, 40); @@ -8878,6 +10828,7 @@ int ecc_test_buffers() { if (XMEMCMP(plain, in, ret)) return -48; +#ifdef WOLFSSL_CERT_EXT idx = 0; bytes = sizeof_ecc_clikeypub_der_256; @@ -8886,6 +10837,7 @@ int ecc_test_buffers() { (word32) bytes); if (ret != 0) return -52; +#endif return 0; } @@ -10503,6 +12455,318 @@ int pkcs7signed_test(void) #endif /* HAVE_PKCS7 */ +#ifdef HAVE_VALGRIND +/* Need a static build to have access to symbols. */ + +/* Maximum number of bytes in a number to test. */ +#define MP_MAX_TEST_BYTE_LEN 16 + +#if defined(HAVE_ECC) || defined(WOLFSSL_KEY_GEN) +static int randNum(mp_int* n, int len, WC_RNG* rng, void* heap) +{ + byte d[MP_MAX_TEST_BYTE_LEN]; + int ret; + + (void)heap; + + do { + ret = wc_RNG_GenerateBlock(rng, d, len); + if (ret != 0) + return ret; + ret = mp_read_unsigned_bin(n, d, len); + if (ret != 0) + return ret; + } while (mp_iszero(n)); + + return 0; +} +#endif + +int mp_test() +{ + WC_RNG rng; + int ret; +#if defined(HAVE_ECC) || defined(WOLFSSL_KEY_GEN) + int i, j, k; +#endif + mp_int a, b, r1, r2, p; + mp_digit d; + + ret = mp_init_multi(&a, &b, &r1, &r2, NULL, NULL); + if (ret != 0) + return -10000; + + mp_init_copy(&p, &a); + + ret = wc_InitRng(&rng); + if (ret != 0) + goto done; + +#if defined(HAVE_ECC) || defined(WOLFSSL_KEY_GEN) + mp_set_int(&a, 0); + if (a.used != 0 || a.dp[0] != 0) + return -10001; + + for (j = 1; j <= MP_MAX_TEST_BYTE_LEN; j++) { + for (i = 0; i < 4 * j; i++) { + /* New values to use. */ + ret = randNum(&p, j, &rng, NULL); + if (ret != 0) + return -11000; + ret = randNum(&a, j, &rng, NULL); + if (ret != 0) + return -11001; + ret = randNum(&b, j, &rng, NULL); + if (ret != 0) + return -11002; + ret = wc_RNG_GenerateBlock(&rng, (byte*)&d, sizeof(d)); + if (ret != 0) + return -11003; + + /* Ensure sqrmod produce same result as mulmod. */ + ret = mp_sqrmod(&a, &p, &r1); + if (ret != 0) + return -11005; + ret = mp_mulmod(&a, &a, &p, &r2); + if (ret != 0) + return -11006; + if (mp_cmp(&r1, &r2) != 0) + return -11007; + + /* Ensure add with mod produce same result as sub with mod. */ + ret = mp_addmod(&a, &b, &p, &r1); + if (ret != 0) + return -11010; + b.sign ^= 1; + ret = mp_submod(&a, &b, &p, &r2); + if (ret != 0) + return -11011; + if (mp_cmp(&r1, &r2) != 0) + return -11012; + + /* Ensure add digit produce same result as sub digit. */ + ret = mp_add_d(&a, d, &r1); + if (ret != 0) + return -11015; + ret = mp_sub_d(&r1, d, &r2); + if (ret != 0) + return -11016; + if (mp_cmp(&a, &r2) != 0) + return -11017; + + /* Invert - if p is even it will use the slow impl. + * - if p and a are even it will fail. + */ + ret = mp_invmod(&a, &p, &r1); + if (ret != 0 && ret != FP_VAL) + return -11019; + ret = 0; + + /* Shift up and down number all bits in a digit. */ + for (k = 0; k < DIGIT_BIT; k++) { + mp_mul_2d(&a, k, &r1); + mp_div_2d(&r1, k, &r2, &p); + if (mp_cmp(&a, &r2) != 0) + return -11020; + if (!mp_iszero(&p)) + return -11021; + mp_rshb(&r1, k); + if (mp_cmp(&a, &r1) != 0) + return -11022; + } + } + } + + /* Check that setting a digit works. */ + mp_set_int(&a, d); + if (a.used != 1 || a.dp[0] != d) + return -11025; + + /* Check setting a bit and testing a bit works. */ + for (i = 0; i < MP_MAX_TEST_BYTE_LEN * 8; i++) { + mp_zero(&a); + mp_set_bit(&a, i); + if (!mp_is_bit_set(&a, i)) + return -11030; + } +#endif + +done: + mp_clear(&p); + mp_clear(&r2); + mp_clear(&r1); + mp_clear(&a); + wc_FreeRng(&rng); + return ret; +} +#endif + +#ifdef HAVE_VALGRIND +/* Need a static build to have access to symbols. */ + +#ifndef WOLFSSL_SSL_H +/* APIs hiding in ssl.h */ +extern int wolfSSL_Debugging_ON(void); +extern void wolfSSL_Debugging_OFF(void); +#endif + +#ifdef DEBUG_WOLFSSL +static int log_cnt = 0; +static void my_Logging_cb(const int logLevel, const char *const logMessage) +{ + (void)logLevel; + (void)logMessage; + log_cnt++; +} +#endif + +int logging_test() +{ +#ifdef DEBUG_WOLFSSL + const char* msg = "Testing, testing. 1, 2, 3, 4 ..."; + byte a[8] = { 1, 2, 3, 4, 5, 6, 7, 8 }; + byte b[256]; + size_t i; + + for (i = 0; i < sizeof(b); i++) + b[i] = i; + + if (wolfSSL_Debugging_ON() != 0) + return -12000; + if (wolfSSL_SetLoggingCb(NULL) != BAD_FUNC_ARG) + return -12002; + + WOLFSSL_MSG(msg); + WOLFSSL_BUFFER(a, sizeof(a)); + WOLFSSL_BUFFER(b, sizeof(b)); + WOLFSSL_BUFFER(NULL, 0); + + wolfSSL_Debugging_OFF(); + + WOLFSSL_MSG(msg); + WOLFSSL_BUFFER(b, sizeof(b)); + + if (wolfSSL_SetLoggingCb(my_Logging_cb) != 0) + return -12003; + + wolfSSL_Debugging_OFF(); + + WOLFSSL_MSG(msg); + WOLFSSL_BUFFER(b, sizeof(b)); + + if (log_cnt != 0) + return -12005; + if (wolfSSL_Debugging_ON() != 0) + return -12006; + + WOLFSSL_MSG(msg); + WOLFSSL_BUFFER(b, sizeof(b)); + + /* One call for each line of output. */ + if (log_cnt != 17) + return -12007; +#else + if (wolfSSL_Debugging_ON() != NOT_COMPILED_IN) + return -12000; + wolfSSL_Debugging_OFF(); + if (wolfSSL_SetLoggingCb(NULL) != NOT_COMPILED_IN) + return -12001; +#endif + return 0; +} +#endif + +int mutex_test() +{ +#ifdef WOLFSSL_PTHREADS + wolfSSL_Mutex m; +#endif + wolfSSL_Mutex *mm = wc_InitAndAllocMutex(); + if (mm == NULL) + return -12020; + wc_FreeMutex(mm); + XFREE(mm, NULL, DYNAMIC_TYPE_MUTEX); + +#ifdef WOLFSSL_PTHREADS + if (wc_InitMutex(&m) != 0) + return -12021; + if (wc_LockMutex(&m) != 0) + return -12022; + if (wc_FreeMutex(&m) != BAD_MUTEX_E) + return -12023; + if (wc_UnLockMutex(&m) != 0) + return -12024; + if (wc_FreeMutex(&m) != 0) + return -12025; + if (wc_LockMutex(&m) != BAD_MUTEX_E) + return -12026; + if (wc_UnLockMutex(&m) != BAD_MUTEX_E) + return -12027; +#endif + + return 0; +} + +#ifdef USE_WOLFSSL_MEMORY +static int malloc_cnt = 0; +static int realloc_cnt = 0; +static int free_cnt = 0; + +static void *my_Malloc_cb(size_t size) +{ + malloc_cnt++; + return malloc(size); +} +static void my_Free_cb(void *ptr) +{ + free_cnt++; + free(ptr); +} +static void *my_Realloc_cb(void *ptr, size_t size) +{ + realloc_cnt++; + return realloc(ptr, size); +} + +int memcb_test() +{ + byte* b = NULL; + + b = (byte*)XREALLOC(b, 1024, NULL, DYNAMIC_TYPE_TMP_BUFFER); + XFREE(b, NULL, DYNAMIC_TYPE_TMP_BUFFER); + b = NULL; + + /* Parameter Validation testing. */ + if (wolfSSL_SetAllocators(NULL, (wolfSSL_Free_cb)&my_Free_cb, + (wolfSSL_Realloc_cb)&my_Realloc_cb) != BAD_FUNC_ARG) + return -12100; + if (wolfSSL_SetAllocators((wolfSSL_Malloc_cb)&my_Malloc_cb, NULL, + (wolfSSL_Realloc_cb)&my_Realloc_cb) != BAD_FUNC_ARG) + return -12101; + if (wolfSSL_SetAllocators((wolfSSL_Malloc_cb)&my_Malloc_cb, + (wolfSSL_Free_cb)&my_Free_cb, NULL) != BAD_FUNC_ARG) + return -12102; + + /* Use API. */ + if (wolfSSL_SetAllocators((wolfSSL_Malloc_cb)&my_Malloc_cb, + (wolfSSL_Free_cb)&my_Free_cb, (wolfSSL_Realloc_cb)my_Realloc_cb) + != 0) + return -12100; + + b = (byte*)XMALLOC(1024, NULL, DYNAMIC_TYPE_TMP_BUFFER); + b = (byte*)XREALLOC(b, 1024, NULL, DYNAMIC_TYPE_TMP_BUFFER); + XFREE(b, NULL, DYNAMIC_TYPE_TMP_BUFFER); + +#ifndef WOLFSSL_STATIC_MEMORY + if (malloc_cnt != 1 || free_cnt != 1 || realloc_cnt != 1) +#else + if (malloc_cnt != 0 || free_cnt != 0 || realloc_cnt != 0) +#endif + return -12110; + return 0; +} +#endif + #undef ERROR_OUT #else diff --git a/wolfcrypt/user-crypto/src/rsa.c b/wolfcrypt/user-crypto/src/rsa.c index fdee5f5a2..2533a94a8 100644 --- a/wolfcrypt/user-crypto/src/rsa.c +++ b/wolfcrypt/user-crypto/src/rsa.c @@ -1950,7 +1950,7 @@ int wc_RsaFlattenPublicKey(RsaKey* key, byte* e, word32* eSz, byte* n, if (key == NULL || e == NULL || eSz == NULL || n == NULL || nSz == NULL) return USER_CRYPTO_ERROR; - bytSz = sizeof(byte); + bytSz = sizeof(byte) * 8; ret = ippsExtGet_BN(NULL, &sz, NULL, key->e); if (ret != ippStsNoErr) return USER_CRYPTO_ERROR;