Skip to content

Commit

Permalink
evp_libctx_test.c: adapt for cipher eNULL-HMAC-SHA{256,384}
Browse files Browse the repository at this point in the history
  • Loading branch information
rajeev-0 committed Dec 6, 2023
1 parent e1abc7e commit 311d8a1
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions test/evp_libctx_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@ static int test_cipher_reinit(int test_id)
int out1_len = 0, out2_len = 0, out3_len = 0;
EVP_CIPHER *cipher = NULL;
EVP_CIPHER_CTX *ctx = NULL;
OSSL_LIB_CTX *prev_libctx = NULL;
unsigned char out1[256];
unsigned char out2[256];
unsigned char out3[256];
Expand All @@ -334,7 +335,11 @@ static int test_cipher_reinit(int test_id)
0x03, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
};
unsigned char iv[16] = {
unsigned char iv[48] = {
0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08,
0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00,
0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08,
0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00,
0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08,
0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00
};
Expand Down Expand Up @@ -364,6 +369,11 @@ static int test_cipher_reinit(int test_id)
/* DES3-WRAP uses random every update - so it will give a different value */
diff = EVP_CIPHER_is_a(cipher, "DES3-WRAP");

/* eNULL-HMAC- use implicit fetching of digest algo */
if (EVP_CIPHER_is_a(cipher, "eNULL-HMAC-SHA256")
|| EVP_CIPHER_is_a(cipher, "eNULL-HMAC-SHA384"))
prev_libctx = OSSL_LIB_CTX_set0_default(libctx);

if (!TEST_true(EVP_EncryptInit_ex(ctx, cipher, NULL, key, iv))
|| !TEST_true(EVP_EncryptUpdate(ctx, out1, &out1_len, in, sizeof(in)))
|| !TEST_true(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv))
Expand All @@ -389,6 +399,9 @@ static int test_cipher_reinit(int test_id)
}
ret = 1;
err:
/* restore the old libctx */
if (prev_libctx != NULL)
OSSL_LIB_CTX_set0_default(prev_libctx);
EVP_CIPHER_free(cipher);
EVP_CIPHER_CTX_free(ctx);
return ret;
Expand All @@ -406,6 +419,7 @@ static int test_cipher_reinit_partialupdate(int test_id)
int out1_len = 0, out2_len = 0, out3_len = 0;
EVP_CIPHER *cipher = NULL;
EVP_CIPHER_CTX *ctx = NULL;
OSSL_LIB_CTX *prev_libctx = NULL;
unsigned char out1[256];
unsigned char out2[256];
unsigned char out3[256];
Expand All @@ -425,7 +439,11 @@ static int test_cipher_reinit_partialupdate(int test_id)
0x03, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
};
static const unsigned char iv[16] = {
static const unsigned char iv[48] = {
0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08,
0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00,
0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08,
0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00,
0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08,
0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00
};
Expand All @@ -438,6 +456,11 @@ static int test_cipher_reinit_partialupdate(int test_id)
if (!TEST_ptr(cipher = EVP_CIPHER_fetch(libctx, name, NULL)))
goto err;

/* eNULL-HMAC- use implicit fetching of digest algo */
if (EVP_CIPHER_is_a(cipher, "eNULL-HMAC-SHA256")
|| EVP_CIPHER_is_a(cipher, "eNULL-HMAC-SHA384"))
prev_libctx = OSSL_LIB_CTX_set0_default(libctx);

in_len = EVP_CIPHER_get_block_size(cipher) / 2;

/* skip any ciphers that don't allow partial updates */
Expand Down Expand Up @@ -469,6 +492,9 @@ static int test_cipher_reinit_partialupdate(int test_id)
}
ret = 1;
err:
/* restore the old libctx */
if (prev_libctx != NULL)
OSSL_LIB_CTX_set0_default(prev_libctx);
EVP_CIPHER_free(cipher);
EVP_CIPHER_CTX_free(ctx);
return ret;
Expand Down

0 comments on commit 311d8a1

Please sign in to comment.