Skip to content

Commit f9c127a

Browse files
committed
Fix aead set random IV
1 parent 1dc4d0e commit f9c127a

File tree

5 files changed

+236
-7
lines changed

5 files changed

+236
-7
lines changed

scripts/test-wp-cs.sh

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -284,9 +284,9 @@ openssl version -a || true
284284
if [ "${AM_BWRAPPED-}" != "yes" ]; then
285285
# Perform the build only if not in the bubble
286286
printf "Cleaning up previous builds\n"
287-
${SCRIPT_DIR}/build-wolfprovider.sh --clean --distclean
287+
${SCRIPT_DIR}/build-wolfprovider.sh --clean --distclean || exit 1
288288
printf "Building wolfProvider\n"
289-
${SCRIPT_DIR}/build-wolfprovider.sh
289+
${SCRIPT_DIR}/build-wolfprovider.sh || exit 1
290290

291291
printf "OPENSSL_BIN: $OPENSSL_BIN\n"
292292
$OPENSSL_BIN version -a || true
@@ -321,4 +321,3 @@ else
321321
printf "$FAIL tests failed.\n"
322322
exit 1
323323
fi
324-

src/wp_aes_aead.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -666,8 +666,8 @@ static int wp_aead_set_ctx_params(wp_AeadCtx* ctx, const OSSL_PARAM params[])
666666
ok = wp_aead_set_param_tls1_iv_fixed(ctx, params);
667667
}
668668
else if (ok && (ctx->mode == EVP_CIPH_GCM_MODE) &&
669-
(XMEMCMP(params->key, OSSL_CIPHER_PARAM_AEAD_TLS1_IV_FIXED,
670-
sizeof(OSSL_CIPHER_PARAM_AEAD_TLS1_IV_FIXED)) == 0)) {
669+
(XMEMCMP(params->key, OSSL_CIPHER_PARAM_AEAD_TLS1_SET_IV_INV,
670+
sizeof(OSSL_CIPHER_PARAM_AEAD_TLS1_SET_IV_INV)) == 0)) {
671671
ok = wp_aead_set_param_tls1_iv_rand(ctx, params);
672672
}
673673

@@ -925,7 +925,12 @@ static int wp_aesgcm_set_rand_iv(wp_AeadCtx *ctx, unsigned char *in,
925925
XMEMCPY(ctx->origIv, ctx->iv, ctx->ivLen);
926926
#endif
927927
XMEMCPY(ctx->iv + ctx->ivLen - inLen, in, inLen);
928+
#ifdef WOLFSSL_AESGCM_STREAM
929+
/* Stream update initializes AES-GCM when IV state is buffered. */
930+
ctx->ivState = IV_STATE_BUFFERED;
931+
#else
928932
ctx->ivState = IV_STATE_COPIED;
933+
#endif
929934
}
930935

931936
WOLFPROV_LEAVE(WP_LOG_COMP_AES, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__), ok);

test/test_aestag.c

Lines changed: 223 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -669,6 +669,215 @@ static int test_aes_tag_dec_ossh_multi(const EVP_CIPHER *cipher,
669669
return err;
670670
}
671671

672+
static int test_aes_tag_enc_ossh_iv_params(const EVP_CIPHER *cipher,
673+
unsigned char *key, unsigned char *iv, int ivFixedSetArg,
674+
unsigned char *aad, unsigned char *msg, int len, unsigned char *enc,
675+
unsigned char *tag, unsigned char *ivInv, size_t ivInvLen)
676+
{
677+
int err;
678+
EVP_CIPHER_CTX *encCtx;
679+
unsigned int tagLen = 16;
680+
681+
err = (encCtx = EVP_CIPHER_CTX_new()) == NULL;
682+
if (err == 0) {
683+
err = EVP_CipherInit(encCtx, cipher, NULL, iv, 1) != 1;
684+
}
685+
if (err == 0) {
686+
err = EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_SET_IV_FIXED,
687+
ivFixedSetArg, iv) != 1;
688+
}
689+
if (err == 0) {
690+
err = EVP_CipherInit(encCtx, NULL, key, NULL, -1) != 1;
691+
}
692+
if (err == 0) {
693+
err = EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_IV_GEN, (int)ivInvLen,
694+
ivInv) != 1;
695+
}
696+
if (err == 0) {
697+
err = EVP_Cipher(encCtx, NULL, aad, (int)strlen((char *)aad)) <= 0;
698+
}
699+
if (err == 0) {
700+
err = EVP_Cipher(encCtx, enc, msg, len) != len;
701+
}
702+
if (err == 0) {
703+
err = EVP_Cipher(encCtx, NULL, NULL, 0) < 0;
704+
}
705+
if (err == 0) {
706+
err = EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_GET_TAG, tagLen,
707+
tag) != 1;
708+
}
709+
710+
EVP_CIPHER_CTX_free(encCtx);
711+
return err;
712+
}
713+
714+
static int test_aes_tag_dec_ossh_set_iv_inv(const EVP_CIPHER *cipher,
715+
unsigned char *key, unsigned char *iv, int ivFixedSetArg,
716+
unsigned char *aad, unsigned char *msg, int len, unsigned char *enc,
717+
unsigned char *tag, unsigned char *dec, unsigned char *ivInv,
718+
size_t ivInvLen)
719+
{
720+
int err;
721+
EVP_CIPHER_CTX *decCtx;
722+
unsigned int tagLen = 16;
723+
724+
err = (decCtx = EVP_CIPHER_CTX_new()) == NULL;
725+
if (err == 0) {
726+
err = EVP_CipherInit(decCtx, cipher, NULL, iv, 0) != 1;
727+
}
728+
if (err == 0) {
729+
err = EVP_CIPHER_CTX_ctrl(decCtx, EVP_CTRL_GCM_SET_IV_FIXED,
730+
ivFixedSetArg, iv) != 1;
731+
}
732+
if (err == 0) {
733+
err = EVP_CipherInit(decCtx, NULL, key, NULL, -1) != 1;
734+
}
735+
if (err == 0) {
736+
err = EVP_CIPHER_CTX_ctrl(decCtx, EVP_CTRL_GCM_SET_IV_INV,
737+
(int)ivInvLen, ivInv) != 1;
738+
}
739+
if (err == 0) {
740+
err = EVP_CIPHER_CTX_ctrl(decCtx, EVP_CTRL_GCM_SET_TAG, tagLen,
741+
tag) != 1;
742+
}
743+
if (err == 0) {
744+
err = EVP_Cipher(decCtx, NULL, aad, (int)strlen((char *)aad)) <= 0;
745+
}
746+
if (err == 0) {
747+
err = EVP_Cipher(decCtx, dec, enc, len) != len;
748+
}
749+
if (err == 0) {
750+
err = EVP_Cipher(decCtx, NULL, NULL, 0) < 0;
751+
}
752+
if ((err == 0) && (dec != NULL) && (msg != NULL) && (memcmp(dec, msg,
753+
len) != 0)) {
754+
err = 1;
755+
}
756+
757+
EVP_CIPHER_CTX_free(decCtx);
758+
return err;
759+
}
760+
761+
static int test_aes_tag_set_iv_inv(void *data, const char *cipher,
762+
int keyLen)
763+
{
764+
int err = 0;
765+
unsigned char msg[] = "Test pattern";
766+
unsigned char key[32];
767+
unsigned char iv[12];
768+
unsigned char aad[] = "AAD";
769+
unsigned char enc[sizeof(msg)];
770+
unsigned char tag[AES_BLOCK_SIZE];
771+
unsigned char dec[sizeof(msg)];
772+
unsigned char ivInv[EVP_GCM_TLS_EXPLICIT_IV_LEN];
773+
EVP_CIPHER* ocipher;
774+
EVP_CIPHER* wcipher;
775+
776+
(void)data;
777+
778+
ocipher = EVP_CIPHER_fetch(osslLibCtx, cipher, "");
779+
wcipher = EVP_CIPHER_fetch(wpLibCtx, cipher, "");
780+
781+
if (RAND_bytes(key, keyLen) == 0) {
782+
err = 1;
783+
}
784+
if ((err == 0) && (RAND_bytes(iv, sizeof(iv)) == 0)) {
785+
err = 1;
786+
}
787+
788+
if (err == 0) {
789+
PRINT_MSG("Encrypt with OpenSSL (SET_IV_INV)");
790+
err = test_aes_tag_enc_ossh_iv_params(ocipher, key, iv, -1, aad, msg,
791+
sizeof(msg), enc, tag, ivInv,
792+
sizeof(ivInv));
793+
}
794+
if (err == 0) {
795+
PRINT_MSG("Decrypt with wolfprovider (SET_IV_INV)");
796+
err = test_aes_tag_dec_ossh_set_iv_inv(wcipher, key, iv, -1, aad, msg,
797+
sizeof(msg), enc, tag, dec,
798+
ivInv, sizeof(ivInv));
799+
}
800+
if (err == 0) {
801+
PRINT_MSG("Encrypt with wolfprovider (SET_IV_INV)");
802+
err = test_aes_tag_enc_ossh_iv_params(wcipher, key, iv, -1, aad, msg,
803+
sizeof(msg), enc, tag, ivInv,
804+
sizeof(ivInv));
805+
}
806+
if (err == 0) {
807+
PRINT_MSG("Decrypt with OpenSSL (SET_IV_INV)");
808+
err = test_aes_tag_dec_ossh_set_iv_inv(ocipher, key, iv, -1, aad, msg,
809+
sizeof(msg), enc, tag, dec,
810+
ivInv, sizeof(ivInv));
811+
}
812+
813+
EVP_CIPHER_free(wcipher);
814+
EVP_CIPHER_free(ocipher);
815+
816+
return err;
817+
}
818+
819+
static int test_aes_tag_set_iv_fixed(void *data, const char *cipher,
820+
int keyLen)
821+
{
822+
int err = 0;
823+
unsigned char msg[] = "Test pattern";
824+
unsigned char key[32];
825+
unsigned char iv[12];
826+
unsigned char aad[] = "AAD";
827+
unsigned char enc[sizeof(msg)];
828+
unsigned char tag[AES_BLOCK_SIZE];
829+
unsigned char dec[sizeof(msg)];
830+
unsigned char ivInv[EVP_GCM_TLS_EXPLICIT_IV_LEN];
831+
EVP_CIPHER* ocipher;
832+
EVP_CIPHER* wcipher;
833+
834+
(void)data;
835+
836+
ocipher = EVP_CIPHER_fetch(osslLibCtx, cipher, "");
837+
wcipher = EVP_CIPHER_fetch(wpLibCtx, cipher, "");
838+
839+
if (RAND_bytes(key, keyLen) == 0) {
840+
err = 1;
841+
}
842+
if ((err == 0) && (RAND_bytes(iv, sizeof(iv)) == 0)) {
843+
err = 1;
844+
}
845+
846+
if (err == 0) {
847+
PRINT_MSG("Encrypt with OpenSSL (TLS1_IV_FIXED)");
848+
err = test_aes_tag_enc_ossh_iv_params(ocipher, key, iv,
849+
EVP_GCM_TLS_FIXED_IV_LEN, aad,
850+
msg, sizeof(msg), enc, tag,
851+
ivInv, sizeof(ivInv));
852+
}
853+
if (err == 0) {
854+
PRINT_MSG("Decrypt with wolfprovider (TLS1_IV_FIXED)");
855+
err = test_aes_tag_dec_ossh_set_iv_inv(wcipher, key, iv,
856+
EVP_GCM_TLS_FIXED_IV_LEN, aad,
857+
msg, sizeof(msg), enc, tag, dec,
858+
ivInv, sizeof(ivInv));
859+
}
860+
if (err == 0) {
861+
PRINT_MSG("Encrypt with wolfprovider (TLS1_IV_FIXED)");
862+
err = test_aes_tag_enc_ossh_iv_params(wcipher, key, iv,
863+
EVP_GCM_TLS_FIXED_IV_LEN, aad,
864+
msg, sizeof(msg), enc, tag,
865+
ivInv, sizeof(ivInv));
866+
}
867+
if (err == 0) {
868+
PRINT_MSG("Decrypt with OpenSSL (TLS1_IV_FIXED)");
869+
err = test_aes_tag_dec_ossh_set_iv_inv(ocipher, key, iv,
870+
EVP_GCM_TLS_FIXED_IV_LEN, aad,
871+
msg, sizeof(msg), enc, tag, dec,
872+
ivInv, sizeof(ivInv));
873+
}
874+
875+
EVP_CIPHER_free(wcipher);
876+
EVP_CIPHER_free(ocipher);
877+
878+
return err;
879+
}
880+
672881
static int test_aes_tag_fixed(void *data, const char *cipher,
673882
int keyLen, int ivFixedLen, int ivLen)
674883
{
@@ -1042,6 +1251,20 @@ int test_aes128_gcm_tls(void *data)
10421251
EVP_GCM_TLS_FIXED_IV_LEN, 0);
10431252
}
10441253

1254+
/******************************************************************************/
1255+
1256+
int test_aes128_gcm_set_iv_inv(void *data)
1257+
{
1258+
return test_aes_tag_set_iv_inv(data, "AES-128-GCM", 16);
1259+
}
1260+
1261+
/******************************************************************************/
1262+
1263+
int test_aes128_gcm_set_iv_fixed(void *data)
1264+
{
1265+
return test_aes_tag_set_iv_fixed(data, "AES-128-GCM", 16);
1266+
}
1267+
10451268
#endif /* WP_HAVE_AESGCM */
10461269

10471270
/******************************************************************************/
@@ -1092,4 +1315,3 @@ int test_aes128_ccm_tls(void *data)
10921315
}
10931316

10941317
#endif /* WP_HAVE_AESCCM */
1095-

test/unit.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,8 @@ TEST_CASE test_case[] = {
266266
TEST_DECL(test_aes256_gcm, NULL),
267267
TEST_DECL(test_aes128_gcm_fixed, NULL),
268268
TEST_DECL(test_aes128_gcm_tls, NULL),
269+
TEST_DECL(test_aes128_gcm_set_iv_inv, NULL),
270+
TEST_DECL(test_aes128_gcm_set_iv_fixed, NULL),
269271
#endif
270272
#ifdef WP_HAVE_AESCCM
271273
TEST_DECL(test_aes128_ccm, NULL),
@@ -813,4 +815,3 @@ int main(int argc, char* argv[])
813815

814816
return err;
815817
}
816-

test/unit.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,8 @@ int test_aes192_gcm(void *data);
193193
int test_aes256_gcm(void *data);
194194
int test_aes128_gcm_fixed(void *data);
195195
int test_aes128_gcm_tls(void *data);
196+
int test_aes128_gcm_set_iv_inv(void *data);
197+
int test_aes128_gcm_set_iv_fixed(void *data);
196198

197199
#endif /* WP_HAVE_AESGCM */
198200

0 commit comments

Comments
 (0)