forked from wolfSSL/wolfssl
Merge pull request #2613 from tmael/evp_aes_gcm
Set default IV length for EVP aes gcm
This commit is contained in:
20
src/ssl.c
20
src/ssl.c
@ -16351,6 +16351,7 @@ int wolfSSL_EVP_MD_type(const WOLFSSL_EVP_MD *md)
|
|||||||
ctx->keyLen = 16;
|
ctx->keyLen = 16;
|
||||||
ctx->block_size = AES_BLOCK_SIZE;
|
ctx->block_size = AES_BLOCK_SIZE;
|
||||||
ctx->authTagSz = AES_BLOCK_SIZE;
|
ctx->authTagSz = AES_BLOCK_SIZE;
|
||||||
|
ctx->ivSz = 12; /* set default IV length to 96 bits */
|
||||||
|
|
||||||
XMEMSET(ctx->authTag, 0, ctx->authTagSz);
|
XMEMSET(ctx->authTag, 0, ctx->authTagSz);
|
||||||
if (iv)
|
if (iv)
|
||||||
@ -16364,11 +16365,6 @@ int wolfSSL_EVP_MD_type(const WOLFSSL_EVP_MD *md)
|
|||||||
if (ret != 0)
|
if (ret != 0)
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
if (iv && key == NULL) {
|
|
||||||
ret = wc_AesSetIV(&ctx->cipher.aes, iv);
|
|
||||||
if (ret != 0)
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
#endif /* WOLFSSL_AES_128 */
|
#endif /* WOLFSSL_AES_128 */
|
||||||
#ifdef WOLFSSL_AES_192
|
#ifdef WOLFSSL_AES_192
|
||||||
@ -16381,6 +16377,7 @@ int wolfSSL_EVP_MD_type(const WOLFSSL_EVP_MD *md)
|
|||||||
ctx->keyLen = 24;
|
ctx->keyLen = 24;
|
||||||
ctx->block_size = AES_BLOCK_SIZE;
|
ctx->block_size = AES_BLOCK_SIZE;
|
||||||
ctx->authTagSz = AES_BLOCK_SIZE;
|
ctx->authTagSz = AES_BLOCK_SIZE;
|
||||||
|
ctx->ivSz = 12; /* set default IV length to 96 bits */
|
||||||
|
|
||||||
XMEMSET(ctx->authTag, 0, ctx->authTagSz);
|
XMEMSET(ctx->authTag, 0, ctx->authTagSz);
|
||||||
if (iv)
|
if (iv)
|
||||||
@ -16394,11 +16391,6 @@ int wolfSSL_EVP_MD_type(const WOLFSSL_EVP_MD *md)
|
|||||||
if (ret != 0)
|
if (ret != 0)
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
if (iv && key == NULL) {
|
|
||||||
ret = wc_AesSetIV(&ctx->cipher.aes, iv);
|
|
||||||
if (ret != 0)
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
#endif /* WOLFSSL_AES_192 */
|
#endif /* WOLFSSL_AES_192 */
|
||||||
#ifdef WOLFSSL_AES_256
|
#ifdef WOLFSSL_AES_256
|
||||||
@ -16411,6 +16403,7 @@ int wolfSSL_EVP_MD_type(const WOLFSSL_EVP_MD *md)
|
|||||||
ctx->keyLen = 32;
|
ctx->keyLen = 32;
|
||||||
ctx->block_size = AES_BLOCK_SIZE;
|
ctx->block_size = AES_BLOCK_SIZE;
|
||||||
ctx->authTagSz = AES_BLOCK_SIZE;
|
ctx->authTagSz = AES_BLOCK_SIZE;
|
||||||
|
ctx->ivSz = 12; /* set default IV length to 96 bits */
|
||||||
|
|
||||||
XMEMSET(ctx->authTag, 0, ctx->authTagSz);
|
XMEMSET(ctx->authTag, 0, ctx->authTagSz);
|
||||||
if (iv)
|
if (iv)
|
||||||
@ -16426,13 +16419,6 @@ int wolfSSL_EVP_MD_type(const WOLFSSL_EVP_MD *md)
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (iv && key == NULL) {
|
|
||||||
ret = wc_AesSetIV(&ctx->cipher.aes, iv);
|
|
||||||
if (ret != 0){
|
|
||||||
WOLFSSL_MSG("wc_AesSetIV() failed");
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
#endif /* WOLFSSL_AES_256 */
|
#endif /* WOLFSSL_AES_256 */
|
||||||
#endif /* HAVE_AESGCM */
|
#endif /* HAVE_AESGCM */
|
||||||
|
111
tests/api.c
111
tests/api.c
@ -27614,59 +27614,90 @@ static void test_wolfssl_EVP_aes_gcm(void)
|
|||||||
int ciphertxtSz = 0;
|
int ciphertxtSz = 0;
|
||||||
int decryptedtxtSz = 0;
|
int decryptedtxtSz = 0;
|
||||||
int len = 0;
|
int len = 0;
|
||||||
EVP_CIPHER_CTX en;
|
int i = 0;
|
||||||
EVP_CIPHER_CTX de;
|
EVP_CIPHER_CTX en[2];
|
||||||
|
EVP_CIPHER_CTX de[2];
|
||||||
|
|
||||||
printf(testingFmt, "wolfssl_EVP_aes_gcm");
|
printf(testingFmt, "wolfssl_EVP_aes_gcm");
|
||||||
|
|
||||||
EVP_CIPHER_CTX_init(&en);
|
for (i = 0; i < 2; i++) {
|
||||||
|
|
||||||
|
EVP_CIPHER_CTX_init(&en[i]);
|
||||||
|
|
||||||
|
if (i == 0) {
|
||||||
|
/* Default uses 96-bits IV length */
|
||||||
#ifdef WOLFSSL_AES_128
|
#ifdef WOLFSSL_AES_128
|
||||||
AssertIntEQ(1, EVP_EncryptInit_ex(&en, EVP_aes_128_gcm(), NULL, NULL, NULL));
|
AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_gcm(), NULL, key, iv));
|
||||||
#elif defined(WOLFSSL_AES_192)
|
#elif defined(WOLFSSL_AES_192)
|
||||||
AssertIntEQ(1, EVP_EncryptInit_ex(&en, EVP_aes_192_gcm(), NULL, NULL, NULL));
|
AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_gcm(), NULL, key, iv));
|
||||||
#elif defined(WOLFSSL_AES_256)
|
#elif defined(WOLFSSL_AES_256)
|
||||||
AssertIntEQ(1, EVP_EncryptInit_ex(&en, EVP_aes_256_gcm(), NULL, NULL, NULL));
|
AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_gcm(), NULL, key, iv));
|
||||||
#endif
|
#endif
|
||||||
AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
|
}
|
||||||
AssertIntEQ(1, EVP_EncryptInit_ex(&en, NULL, NULL, key, iv));
|
else {
|
||||||
AssertIntEQ(1, EVP_EncryptUpdate(&en, NULL, &len, aad, aadSz));
|
|
||||||
AssertIntEQ(1, EVP_EncryptUpdate(&en, ciphertxt, &len, plaintxt, plaintxtSz));
|
|
||||||
ciphertxtSz = len;
|
|
||||||
AssertIntEQ(1, EVP_EncryptFinal_ex(&en, ciphertxt, &len));
|
|
||||||
ciphertxtSz += len;
|
|
||||||
AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en, EVP_CTRL_GCM_GET_TAG, AES_BLOCK_SIZE, tag));
|
|
||||||
|
|
||||||
EVP_CIPHER_CTX_init(&de);
|
|
||||||
#ifdef WOLFSSL_AES_128
|
#ifdef WOLFSSL_AES_128
|
||||||
AssertIntEQ(1, EVP_EncryptInit_ex(&de, EVP_aes_128_gcm(), NULL, NULL, NULL));
|
AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_gcm(), NULL, NULL, NULL));
|
||||||
#elif defined(WOLFSSL_AES_192)
|
#elif defined(WOLFSSL_AES_192)
|
||||||
AssertIntEQ(1, EVP_EncryptInit_ex(&de, EVP_aes_192_gcm(), NULL, NULL, NULL));
|
AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_gcm(), NULL, NULL, NULL));
|
||||||
#elif defined(WOLFSSL_AES_256)
|
#elif defined(WOLFSSL_AES_256)
|
||||||
AssertIntEQ(1, EVP_EncryptInit_ex(&de, EVP_aes_256_gcm(), NULL, NULL, NULL));
|
AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_gcm(), NULL, NULL, NULL));
|
||||||
#endif
|
#endif
|
||||||
AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
|
/* non-default must to set the IV length first */
|
||||||
AssertIntEQ(1, EVP_EncryptInit_ex(&de, NULL, NULL, key, iv));
|
AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
|
||||||
AssertIntEQ(1, EVP_EncryptUpdate(&de, NULL, &len, aad, aadSz));
|
AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
|
||||||
AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de, EVP_CTRL_GCM_SET_TAG, AES_BLOCK_SIZE, tag));
|
}
|
||||||
AssertIntEQ(1, EVP_DecryptUpdate(&de, decryptedtxt, &len, ciphertxt, ciphertxtSz));
|
AssertIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
|
||||||
decryptedtxtSz = len;
|
AssertIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt, plaintxtSz));
|
||||||
AssertIntGT(EVP_DecryptFinal_ex(&de, decryptedtxt, &len), 0);
|
ciphertxtSz = len;
|
||||||
decryptedtxtSz += len;
|
AssertIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
|
||||||
AssertIntEQ(ciphertxtSz, decryptedtxtSz);
|
ciphertxtSz += len;
|
||||||
AssertIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
|
AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_GET_TAG, AES_BLOCK_SIZE, tag));
|
||||||
|
|
||||||
/* modify tag*/
|
EVP_CIPHER_CTX_init(&de[i]);
|
||||||
tag[AES_BLOCK_SIZE-1]+=0xBB;
|
if (i == 0) {
|
||||||
AssertIntEQ(1, EVP_EncryptUpdate(&de, NULL, &len, aad, aadSz));
|
/* Default uses 96-bits IV length */
|
||||||
AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de, EVP_CTRL_GCM_SET_TAG, AES_BLOCK_SIZE, tag));
|
#ifdef WOLFSSL_AES_128
|
||||||
AssertIntEQ(1, EVP_DecryptUpdate(&de, decryptedtxt, &len, ciphertxt, ciphertxtSz));
|
AssertIntEQ(1, EVP_EncryptInit_ex(&de[i], EVP_aes_128_gcm(), NULL, key, iv));
|
||||||
decryptedtxtSz = len;
|
#elif defined(WOLFSSL_AES_192)
|
||||||
AssertIntGT(EVP_DecryptFinal_ex(&de, decryptedtxt, &len), 0);
|
AssertIntEQ(1, EVP_EncryptInit_ex(&de[i], EVP_aes_192_gcm(), NULL, key, iv));
|
||||||
decryptedtxtSz += len;
|
#elif defined(WOLFSSL_AES_256)
|
||||||
AssertIntEQ(ciphertxtSz, decryptedtxtSz);
|
AssertIntEQ(1, EVP_EncryptInit_ex(&de[i], EVP_aes_256_gcm(), NULL, key, iv));
|
||||||
/* decrypted text should not be equal to plain text*/
|
#endif
|
||||||
AssertIntNE(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
|
}
|
||||||
|
else {
|
||||||
|
#ifdef WOLFSSL_AES_128
|
||||||
|
AssertIntEQ(1, EVP_EncryptInit_ex(&de[i], EVP_aes_128_gcm(), NULL, NULL, NULL));
|
||||||
|
#elif defined(WOLFSSL_AES_192)
|
||||||
|
AssertIntEQ(1, EVP_EncryptInit_ex(&de[i], EVP_aes_192_gcm(), NULL, NULL, NULL));
|
||||||
|
#elif defined(WOLFSSL_AES_256)
|
||||||
|
AssertIntEQ(1, EVP_EncryptInit_ex(&de[i], EVP_aes_256_gcm(), NULL, NULL, NULL));
|
||||||
|
#endif
|
||||||
|
/* non-default must to set the IV length first */
|
||||||
|
AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
|
||||||
|
AssertIntEQ(1, EVP_EncryptInit_ex(&de[i], NULL, NULL, key, iv));
|
||||||
|
|
||||||
|
}
|
||||||
|
AssertIntEQ(1, EVP_EncryptUpdate(&de[i], NULL, &len, aad, aadSz));
|
||||||
|
AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG, AES_BLOCK_SIZE, tag));
|
||||||
|
AssertIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt, ciphertxtSz));
|
||||||
|
decryptedtxtSz = len;
|
||||||
|
AssertIntGT(EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len), 0);
|
||||||
|
decryptedtxtSz += len;
|
||||||
|
AssertIntEQ(ciphertxtSz, decryptedtxtSz);
|
||||||
|
AssertIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
|
||||||
|
|
||||||
|
/* modify tag*/
|
||||||
|
tag[AES_BLOCK_SIZE-1]+=0xBB;
|
||||||
|
AssertIntEQ(1, EVP_EncryptUpdate(&de[i], NULL, &len, aad, aadSz));
|
||||||
|
AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG, AES_BLOCK_SIZE, tag));
|
||||||
|
AssertIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt, ciphertxtSz));
|
||||||
|
decryptedtxtSz = len;
|
||||||
|
AssertIntGT(EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len), 0);
|
||||||
|
decryptedtxtSz += len;
|
||||||
|
AssertIntEQ(ciphertxtSz, decryptedtxtSz);
|
||||||
|
/* decrypted text should not be equal to plain text*/
|
||||||
|
AssertIntNE(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
|
||||||
|
}
|
||||||
printf(resultFmt, passed);
|
printf(resultFmt, passed);
|
||||||
|
|
||||||
#endif /* OPENSSL_EXTRA && !NO_AES && HAVE_AESGCM */
|
#endif /* OPENSSL_EXTRA && !NO_AES && HAVE_AESGCM */
|
||||||
|
Reference in New Issue
Block a user