Skip to content

Commit 9c1ac10

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

File tree

6 files changed

+318
-76
lines changed

6 files changed

+318
-76
lines changed

debian/install-wolfprov.sh

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,14 @@ main() {
195195
work_dir=$(mktemp -d)
196196
printf "Working directory: $work_dir\n"
197197
pushd $work_dir 2>&1 > /dev/null
198-
cp -r $REPO_ROOT .
199-
cd $(basename $REPO_ROOT)
198+
repo_name=$(basename "$REPO_ROOT")
199+
if git clone --depth 1 "file://$REPO_ROOT" "$repo_name"; then
200+
:
201+
else
202+
echo "Shallow clone failed, falling back to local clone"
203+
git clone "$REPO_ROOT" "$repo_name"
204+
fi
205+
cd "$repo_name"
200206

201207
wolfprov_build $fips_mode $debug_mode
202208
if [ $no_install -eq 0 ]; then
@@ -218,4 +224,3 @@ main() {
218224

219225
# Run main function with all arguments
220226
main "$@"
221-

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: 81 additions & 68 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);
@@ -997,69 +1002,72 @@ static int wp_aesgcm_tls_iv_set_fixed(wp_AeadCtx* ctx, unsigned char* iv,
9971002
}
9981003

9991004
/**
1000-
* Initialize AES GCM cipher for encryption.
1001-
*
1002-
* Sets the parameters as well as key and IV/nonce.
1005+
* Initialize AES GCM key and IV/nonce state.
10031006
*
10041007
* @param [in, out] ctx AEAD context object.
1005-
* @param [in] key Private key to initialize with. May be NULL.
1008+
* @param [in] key Key to initialize with. May be NULL.
10061009
* @param [in] keyLen Length of key in bytes.
10071010
* @param [in] iv IV/nonce to initialize with. May be NULL.
10081011
* @param [in] ivLen Length of IV/nonce in bytes.
1009-
* @param [in] params Array of parameters and values.
1012+
* @param [in] enc 1 for encryption, 0 for decryption.
10101013
* @return 1 on success.
10111014
* @return 0 on failure.
10121015
*/
1013-
static int wp_aesgcm_einit(wp_AeadCtx* ctx, const unsigned char *key,
1014-
size_t keyLen, const unsigned char *iv, size_t ivLen,
1015-
const OSSL_PARAM params[])
1016+
static int wp_aesgcm_init_key_iv(wp_AeadCtx* ctx, const unsigned char* key,
1017+
size_t keyLen, const unsigned char* iv, size_t ivLen, int enc)
10161018
{
10171019
Aes *aes = &ctx->aes;
10181020
int ok = 1;
1021+
int rc;
10191022

1020-
WOLFPROV_ENTER(WP_LOG_COMP_AES, "wp_aesgcm_einit");
1021-
1022-
if (!wolfssl_prov_is_running()) {
1023-
ok = 0;
1024-
}
1025-
if (ok) {
1026-
WP_CHECK_FIPS_ALGO(WP_CAST_ALGO_AES);
1027-
}
10281023
#ifdef WOLFSSL_AESGCM_STREAM
1029-
if (ok) {
1030-
int rc;
1024+
if (iv != NULL) {
1025+
if (ivLen == 0) {
1026+
ok = 0;
1027+
}
1028+
if (ok) {
1029+
XMEMCPY(ctx->iv, iv, ivLen);
1030+
ctx->ivState = IV_STATE_BUFFERED;
1031+
ctx->ivSet = 0;
1032+
ctx->ivLen = ivLen;
1033+
}
1034+
}
10311035

1032-
if (iv != NULL) {
1033-
if (ivLen == 0) {
1036+
if (ok && (key != NULL)) {
1037+
if ((iv == NULL) || (ivLen == 0)) {
1038+
rc = wc_AesGcmSetKey(aes, key, (word32)keyLen);
1039+
if (rc != 0) {
1040+
WOLFPROV_MSG_DEBUG_RETCODE(WP_LOG_LEVEL_DEBUG,
1041+
"wc_AesGcmSetKey", rc);
10341042
ok = 0;
10351043
}
1036-
if (ok) {
1037-
XMEMCPY(ctx->iv, iv, ivLen);
1038-
ctx->ivState = IV_STATE_BUFFERED;
1039-
ctx->ivSet = 0;
1040-
ctx->ivLen = ivLen;
1041-
}
10421044
}
1043-
if ((ivLen == 0) && (key != NULL)) {
1044-
rc = wc_AesGcmSetKey(aes, key, (word32)keyLen);
1045+
else if (enc) {
1046+
rc = wc_AesGcmEncryptInit(aes, key, (word32)keyLen, iv,
1047+
(word32)ivLen);
10451048
if (rc != 0) {
1046-
WOLFPROV_MSG_DEBUG_RETCODE(WP_LOG_LEVEL_DEBUG, "wc_AesGcmSetKey", rc);
1049+
WOLFPROV_MSG_DEBUG_RETCODE(WP_LOG_LEVEL_DEBUG,
1050+
"wc_AesGcmEncryptInit", rc);
10471051
ok = 0;
10481052
}
10491053
}
1050-
else if (key != NULL) {
1051-
rc = wc_AesGcmEncryptInit(aes, key, (word32)keyLen, iv, (word32)ivLen);
1054+
else {
1055+
rc = wc_AesGcmDecryptInit(aes, key, (word32)keyLen, iv,
1056+
(word32)ivLen);
10521057
if (rc != 0) {
1053-
WOLFPROV_MSG_DEBUG_RETCODE(WP_LOG_LEVEL_DEBUG, "wc_AesGcmEncryptInit", rc);
1058+
WOLFPROV_MSG_DEBUG_RETCODE(WP_LOG_LEVEL_DEBUG,
1059+
"wc_AesGcmDecryptInit", rc);
10541060
ok = 0;
10551061
}
10561062
}
10571063
}
10581064
#else
1059-
if (ok && (key != NULL)) {
1060-
int rc = wc_AesGcmSetKey(aes, key, (word32)keyLen);
1065+
(void)enc;
1066+
if (key != NULL) {
1067+
rc = wc_AesGcmSetKey(aes, key, (word32)keyLen);
10611068
if (rc != 0) {
1062-
WOLFPROV_MSG_DEBUG_RETCODE(WP_LOG_LEVEL_DEBUG, "wc_AesGcmSetKey", rc);
1069+
WOLFPROV_MSG_DEBUG_RETCODE(WP_LOG_LEVEL_DEBUG, "wc_AesGcmSetKey",
1070+
rc);
10631071
ok = 0;
10641072
}
10651073
}
@@ -1074,6 +1082,41 @@ static int wp_aesgcm_einit(wp_AeadCtx* ctx, const unsigned char *key,
10741082
}
10751083
}
10761084
#endif
1085+
1086+
return ok;
1087+
}
1088+
1089+
/**
1090+
* Initialize AES GCM cipher for encryption.
1091+
*
1092+
* Sets the parameters as well as key and IV/nonce.
1093+
*
1094+
* @param [in, out] ctx AEAD context object.
1095+
* @param [in] key Private key to initialize with. May be NULL.
1096+
* @param [in] keyLen Length of key in bytes.
1097+
* @param [in] iv IV/nonce to initialize with. May be NULL.
1098+
* @param [in] ivLen Length of IV/nonce in bytes.
1099+
* @param [in] params Array of parameters and values.
1100+
* @return 1 on success.
1101+
* @return 0 on failure.
1102+
*/
1103+
static int wp_aesgcm_einit(wp_AeadCtx* ctx, const unsigned char *key,
1104+
size_t keyLen, const unsigned char *iv, size_t ivLen,
1105+
const OSSL_PARAM params[])
1106+
{
1107+
int ok = 1;
1108+
1109+
WOLFPROV_ENTER(WP_LOG_COMP_AES, "wp_aesgcm_einit");
1110+
1111+
if (!wolfssl_prov_is_running()) {
1112+
ok = 0;
1113+
}
1114+
if (ok) {
1115+
WP_CHECK_FIPS_ALGO(WP_CAST_ALGO_AES);
1116+
}
1117+
if (ok) {
1118+
ok = wp_aesgcm_init_key_iv(ctx, key, keyLen, iv, ivLen, 1);
1119+
}
10771120
if (ok) {
10781121
ctx->enc = 1;
10791122
ctx->keySet |= (key != NULL);
@@ -1103,7 +1146,6 @@ static int wp_aesgcm_dinit(wp_AeadCtx *ctx, const unsigned char *key,
11031146
size_t keyLen, const unsigned char *iv, size_t ivLen,
11041147
const OSSL_PARAM params[])
11051148
{
1106-
Aes *aes = &ctx->aes;
11071149
int ok = 1;
11081150

11091151
WOLFPROV_ENTER(WP_LOG_COMP_AES, "wp_aesgcm_dinit");
@@ -1114,38 +1156,9 @@ static int wp_aesgcm_dinit(wp_AeadCtx *ctx, const unsigned char *key,
11141156
if (ok) {
11151157
WP_CHECK_FIPS_ALGO(WP_CAST_ALGO_AES);
11161158
}
1117-
#ifdef WOLFSSL_AESGCM_STREAM
1118-
if (ok && key != NULL) {
1119-
int rc = wc_AesGcmDecryptInit(aes, key, (word32)keyLen, iv, (word32)ivLen);
1120-
if (rc != 0) {
1121-
WOLFPROV_MSG_DEBUG_RETCODE(WP_LOG_LEVEL_DEBUG, "wc_AesGcmDecryptInit", rc);
1122-
ok = 0;
1123-
}
1124-
}
11251159
if (ok) {
1126-
XMEMCPY(ctx->iv, iv, ivLen);
1127-
ctx->ivState = IV_STATE_BUFFERED;
1128-
ctx->ivSet = 0;
1129-
}
1130-
#else
1131-
if (ok && (key != NULL)) {
1132-
int rc = wc_AesGcmSetKey(aes, key, (word32)keyLen);
1133-
if (rc != 0) {
1134-
WOLFPROV_MSG_DEBUG_RETCODE(WP_LOG_LEVEL_DEBUG, "wc_AesGcmSetKey", rc);
1135-
ok = 0;
1136-
}
1137-
}
1138-
if (ok && (iv != NULL)) {
1139-
if (ivLen != ctx->ivLen) {
1140-
ok = 0;
1141-
}
1142-
if (ok) {
1143-
XMEMCPY(ctx->iv, iv, ivLen);
1144-
ctx->ivState = IV_STATE_BUFFERED;
1145-
ctx->ivSet = 0;
1146-
}
1160+
ok = wp_aesgcm_init_key_iv(ctx, key, keyLen, iv, ivLen, 0);
11471161
}
1148-
#endif
11491162
if (ok) {
11501163
ctx->enc = 0;
11511164
ctx->keySet |= (key != NULL);

0 commit comments

Comments
 (0)