@@ -13585,6 +13585,7 @@ static int test_wolfSSL_PKCS5(void)
1358513585 const char *passwd = "pass1234";
1358613586#endif
1358713587 const unsigned char *salt = (unsigned char *)"salt1234";
13588+ const unsigned char *salt16 = (unsigned char *)"salt1234salt5678";
1358813589 unsigned char *out = (unsigned char *)XMALLOC(WC_SHA_DIGEST_SIZE, NULL,
1358913590 DYNAMIC_TYPE_TMP_BUFFER);
1359013591 int ret = 0;
@@ -13593,11 +13594,16 @@ static int test_wolfSSL_PKCS5(void)
1359313594 ExpectIntEQ(ret = PKCS5_PBKDF2_HMAC_SHA1(passwd,(int)XSTRLEN(passwd), salt,
1359413595 (int)XSTRLEN((const char *) salt), 10, WC_SHA_DIGEST_SIZE,out),
1359513596 WOLFSSL_SUCCESS);
13597+ /* A 16-byte salt gets no FIPS v7 indicator, so none may be cleared. */
13598+ ExpectIntEQ(PKCS5_PBKDF2_HMAC_SHA1(passwd, (int)XSTRLEN(passwd), salt16,
13599+ 16, 10, WC_SHA_DIGEST_SIZE, out), WOLFSSL_SUCCESS);
1359613600
1359713601#ifdef WOLFSSL_SHA512
1359813602 ExpectIntEQ(ret = PKCS5_PBKDF2_HMAC(passwd,(int)XSTRLEN(passwd), salt,
1359913603 (int)XSTRLEN((const char *) salt), 10, wolfSSL_EVP_sha512(),
1360013604 WC_SHA_DIGEST_SIZE, out), SSL_SUCCESS);
13605+ ExpectIntEQ(PKCS5_PBKDF2_HMAC(passwd, (int)XSTRLEN(passwd), salt16, 16,
13606+ 10, wolfSSL_EVP_sha512(), WC_SHA_DIGEST_SIZE, out), SSL_SUCCESS);
1360113607#endif
1360213608
1360313609 XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
@@ -21893,7 +21899,7 @@ static int test_wolfSSL_PKCS8_d2i(void)
2189321899#if !defined(NO_DES3) && !defined(NO_SHA) && defined(HAVE_PBKDF2)
2189421900 ExpectNotNull(bio = BIO_new(BIO_s_mem()));
2189521901 /* Write Encrypted PKCS#8 PEM to BIO. */
21896- bytes = 1834 ;
21902+ bytes = 1846 ;
2189721903 ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, EVP_des_ede3_cbc(),
2189821904 NULL, 0, PasswordCallBack, (void*)"yassl123"), bytes);
2189921905 ExpectIntEQ(PEM_write_PKCS8PrivateKey(stderr, pkey, EVP_des_ede3_cbc(),
@@ -21976,8 +21982,8 @@ static int test_wolfSSL_PKCS8_d2i(void)
2197621982 bio = NULL;
2197721983#ifdef HAVE_PBKDF2
2197821984 ExpectNotNull(bio = BIO_new(BIO_s_mem()));
21979- /* Write Encrypted PKCS#8 PEM to BIO (test write 0 then 379 ) */
21980- bytes = 379 ;
21985+ /* Write Encrypted PKCS#8 PEM to BIO (test write 0 then 391 ) */
21986+ bytes = 391 ;
2198121987 ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, EVP_aes_256_cbc(),
2198221988 NULL, 0, NoPasswordCallBack, (void*)"yassl123"), 0);
2198321989 ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, EVP_aes_256_cbc(),
@@ -28254,35 +28260,33 @@ static int test_wolfSSL_CRYPTO_memcmp(void)
2825428260 | wolfCrypt ASN
2825528261 *----------------------------------------------------------------------------*/
2825628262
28257- static int test_wc_CreateEncryptedPKCS8Key(void)
28258- {
28259- EXPECT_DECLS;
2826028263#if defined(HAVE_PKCS8) && !defined(NO_PWDBASED) && defined(WOLFSSL_AES_256) \
2826128264 && !defined(NO_AES_CBC) && !defined(NO_RSA) && !defined(NO_SHA) && \
2826228265 !defined(NO_ASN_CRYPT) && defined(HAVE_PBKDF2)
28263- WC_RNG rng;
28266+ /* Encrypt server_key_der_2048 with salt (NULL for the default), decrypt it
28267+ * and compare with the original. */
28268+ static int create_enc_pkcs8_check(WC_RNG* rng, byte* salt, word32 saltSz)
28269+ {
28270+ EXPECT_DECLS;
2826428271 byte* encKey = NULL;
2826528272 word32 encKeySz = 0;
2826628273 word32 decKeySz = 0;
2826728274 const char password[] = "Lorem ipsum dolor sit amet";
2826828275 word32 passwordSz = (word32)XSTRLEN(password);
2826928276 word32 tradIdx = 0;
2827028277
28271- XMEMSET(&rng, 0, sizeof(WC_RNG));
28272- ExpectIntEQ(wc_InitRng(&rng), 0);
28273- PRIVATE_KEY_UNLOCK();
2827428278 /* Call with NULL for out buffer to get necessary length. */
2827528279 ExpectIntEQ(wc_CreateEncryptedPKCS8Key((byte*)server_key_der_2048,
2827628280 sizeof_server_key_der_2048, NULL, &encKeySz, password, (int)passwordSz,
28277- PKCS5, PBES2, AES256CBCb, NULL, 0 , WC_PKCS12_ITT_DEFAULT, & rng, NULL) ,
28278- WC_NO_ERR_TRACE(LENGTH_ONLY_E));
28281+ PKCS5, PBES2, AES256CBCb, salt, saltSz , WC_PKCS12_ITT_DEFAULT, rng,
28282+ NULL), WC_NO_ERR_TRACE(LENGTH_ONLY_E));
2827928283 ExpectNotNull(encKey = (byte*)XMALLOC(encKeySz, HEAP_HINT,
2828028284 DYNAMIC_TYPE_TMP_BUFFER));
2828128285 /* Call with the allocated out buffer. */
2828228286 ExpectIntGT(wc_CreateEncryptedPKCS8Key((byte*)server_key_der_2048,
2828328287 sizeof_server_key_der_2048, encKey, &encKeySz, password, (int)passwordSz,
28284- PKCS5, PBES2, AES256CBCb, NULL, 0 , WC_PKCS12_ITT_DEFAULT, & rng, NULL) ,
28285- 0);
28288+ PKCS5, PBES2, AES256CBCb, salt, saltSz , WC_PKCS12_ITT_DEFAULT, rng,
28289+ NULL), 0);
2828628290 /* Decrypt the encrypted PKCS8 key we just made. */
2828728291 ExpectIntGT((decKeySz = (word32)wc_DecryptPKCS8Key(encKey, encKeySz, password,
2828828292 (int)passwordSz)), 0);
@@ -28291,9 +28295,29 @@ static int test_wc_CreateEncryptedPKCS8Key(void)
2829128295 /* Check that the decrypted key matches the key prior to encryption. */
2829228296 ExpectIntEQ(XMEMCMP(encKey + tradIdx, server_key_der_2048,
2829328297 sizeof_server_key_der_2048), 0);
28294- PRIVATE_KEY_LOCK();
2829528298
2829628299 XFREE(encKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
28300+ return EXPECT_RESULT();
28301+ }
28302+ #endif
28303+
28304+ static int test_wc_CreateEncryptedPKCS8Key(void)
28305+ {
28306+ EXPECT_DECLS;
28307+ #if defined(HAVE_PKCS8) && !defined(NO_PWDBASED) && defined(WOLFSSL_AES_256) \
28308+ && !defined(NO_AES_CBC) && !defined(NO_RSA) && !defined(NO_SHA) && \
28309+ !defined(NO_ASN_CRYPT) && defined(HAVE_PBKDF2)
28310+ WC_RNG rng;
28311+ byte salt8[8] = { 1, 2, 3, 4, 5, 6, 7, 8 };
28312+
28313+ XMEMSET(&rng, 0, sizeof(WC_RNG));
28314+ ExpectIntEQ(wc_InitRng(&rng), 0);
28315+ PRIVATE_KEY_UNLOCK();
28316+ EXPECT_TEST(create_enc_pkcs8_check(&rng, NULL, 0));
28317+ /* An 8-byte salt: FIPS v7 flags it, and the key must still round-trip. */
28318+ EXPECT_TEST(create_enc_pkcs8_check(&rng, salt8, sizeof(salt8)));
28319+ PRIVATE_KEY_LOCK();
28320+
2829728321 wc_FreeRng(&rng);
2829828322#endif
2829928323 return EXPECT_RESULT();
0 commit comments