Skip to content

Commit d0c0178

Browse files
committed
Add alignment safety checks to we_aes_cbc_hmac.c
1 parent 52813c4 commit d0c0178

File tree

3 files changed

+89
-63
lines changed

3 files changed

+89
-63
lines changed

include/wolfengine/we_internal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ extern EVP_PKEY_ASN1_METHOD *we_hmac_pkey_asn1_method;
220220

221221
int we_init_hmac_pkey_meth(void);
222222
int we_init_hmac_pkey_asn1_meth(void);
223+
int we_hmac_update(Hmac*, const void*, size_t);
223224

224225
#endif /* WE_HAVE_HMAC */
225226

src/we_aes_cbc_hmac.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,9 @@ static int we_aes_cbc_hmac_enc(we_AesCbcHmac* aes, unsigned char *out,
183183
/* MAC the handshake message/data. */
184184
WOLFENGINE_MSG(WE_LOG_CIPHER, "MAC handshake message/data: len = %d",
185185
pLen - off);
186-
rc = wc_HmacUpdate(&aes->hmac, in + off, pLen - off);
187-
if (rc != 0) {
188-
WOLFENGINE_ERROR_FUNC(WE_LOG_CIPHER, "wc_HmacUpdate", rc);
186+
rc = we_hmac_update(&aes->hmac, in + off, pLen - off);
187+
if (rc != 1) {
188+
WOLFENGINE_ERROR_FUNC(WE_LOG_CIPHER, "we_hmac_update", rc);
189189
ret = -1;
190190
}
191191
}
@@ -308,9 +308,9 @@ static int we_aes_cbc_hmac_dec(we_AesCbcHmac* aes, unsigned char *out,
308308
/* MAC the record header. */
309309
WOLFENGINE_MSG(WE_LOG_CIPHER, "Generate MAC over record header: "
310310
"len = %d", aes->pLen);
311-
rc = wc_HmacUpdate(&aes->hmac, aes->tlsAAD, aes->pLen);
312-
if (rc != 0) {
313-
WOLFENGINE_ERROR_FUNC(WE_LOG_CIPHER, "wc_HmacUpdate", rc);
311+
rc = we_hmac_update(&aes->hmac, aes->tlsAAD, aes->pLen);
312+
if (rc != 1) {
313+
WOLFENGINE_ERROR_FUNC(WE_LOG_CIPHER, "we_hmac_update", rc);
314314
ret = -1;
315315
}
316316
}
@@ -319,9 +319,9 @@ static int we_aes_cbc_hmac_dec(we_AesCbcHmac* aes, unsigned char *out,
319319
/* MAC the message/input. */
320320
WOLFENGINE_MSG(WE_LOG_CIPHER, "Generate MAC over message/input, "
321321
"len = %d", ret);
322-
rc = wc_HmacUpdate(&aes->hmac, out + off, ret);
323-
if (rc != 0) {
324-
WOLFENGINE_ERROR_FUNC(WE_LOG_CIPHER, "wc_HmacUpdate", rc);
322+
rc = we_hmac_update(&aes->hmac, out + off, ret);
323+
if (rc != 1) {
324+
WOLFENGINE_ERROR_FUNC(WE_LOG_CIPHER, "we_hmac_update", rc);
325325
ret = -1;
326326
}
327327
}
@@ -478,10 +478,10 @@ static int we_aes_cbc_hmac_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg,
478478
/* MAC the record header. */
479479
WOLFENGINE_MSG(WE_LOG_CIPHER,
480480
"Updating MAC with record header");
481-
rc = wc_HmacUpdate(&aes->hmac, tls, arg);
482-
if (rc != 0) {
481+
rc = we_hmac_update(&aes->hmac, tls, arg);
482+
if (rc != 1) {
483483
WOLFENGINE_ERROR_FUNC(WE_LOG_CIPHER,
484-
"wc_HmacUpdate", rc);
484+
"we_hmac_update", rc);
485485
ret = -1;
486486
}
487487
}

src/we_mac.c

Lines changed: 76 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -996,66 +996,32 @@ static int we_hmac_pkey_init(EVP_PKEY_CTX *ctx)
996996
return ret;
997997
}
998998

999-
1000999
/**
1001-
* Replacement update function for EVP_MD context.
1000+
* Update the HMAC hmac with dataSz bytes from data. If wolfEngine has been
1001+
* built with WE_ALIGNMENT_SAFETY, this function provides a fix for a potential
1002+
* alignment crash in the wolfCrypt FIPS 140-2 code.
10021003
*
1003-
* @param ctx [in] EVP_MD context being used.
1004+
* @param hmac [in] wolfCrypt HMAC data structure.
10041005
* @param data [in] Data to be passed to HMAC update.
10051006
* @param dataSz [in] Size of data buffer to be passed to HMAC update.
10061007
* @returns 1 on success and 0 on failure.
10071008
*/
1008-
static int we_hmac_pkey_update(EVP_MD_CTX *ctx, const void *data, size_t dataSz)
1009-
{
1010-
int ret = 1, rc = 0;
1011-
we_Mac *mac;
1012-
EVP_PKEY_CTX *pkeyCtx;
1013-
1014-
WOLFENGINE_ENTER(WE_LOG_MAC, "we_hmac_pkey_update");
1015-
WOLFENGINE_MSG_VERBOSE(WE_LOG_MAC, "ARGS [ctx = %p, data = %p, "
1016-
"dataSz = %zu]", ctx, data, dataSz);
1017-
1018-
/* If this function is called with an input buffer length of 0, we need to
1019-
* return success immediately. This is how OpenSSL handles this scenario. */
1020-
if (dataSz == 0) {
1021-
WOLFENGINE_MSG(WE_LOG_MAC, "dataSz == 0, returning success.");
1022-
return 1;
1023-
}
1024-
1025-
/* Validate parameters. */
1026-
if ((ctx == NULL) || (data == NULL)) {
1027-
WOLFENGINE_ERROR_FUNC_NULL(WE_LOG_MAC,
1028-
"we_hmac_pkey_update, ctx: ", ctx);
1029-
WOLFENGINE_ERROR_FUNC_NULL(WE_LOG_MAC,
1030-
"we_hmac_pkey_update, data:", (void*)data);
1031-
ret = 0;
1032-
}
1009+
int we_hmac_update(Hmac* hmac, const void *data, size_t dataSz) {
1010+
int ret = 1;
1011+
int rc;
10331012

1034-
if (ret == 1) {
1035-
/* Get PKEY context from digest context. */
1036-
pkeyCtx = EVP_MD_CTX_pkey_ctx(ctx);
1037-
if (pkeyCtx == NULL) {
1038-
ret = 0;
1039-
}
1040-
}
1041-
if (ret == 1) {
1042-
/* Retrieve the internal MAC object. */
1043-
mac = (we_Mac *)EVP_PKEY_CTX_get_data(pkeyCtx);
1044-
if (mac == NULL) {
1045-
WOLFENGINE_ERROR_FUNC_NULL(WE_LOG_MAC, "EVP_PKEY_CTX_get_data",
1046-
mac);
1047-
ret = 0;
1048-
}
1049-
}
1013+
WOLFENGINE_ENTER(WE_LOG_MAC, "we_hmac_update");
1014+
WOLFENGINE_MSG_VERBOSE(WE_LOG_MAC, "ARGS [hmac = %p, data = %p, "
1015+
"dataSz = %zu]", hmac, data, dataSz);
10501016

10511017
#ifdef WE_ALIGNMENT_SAFETY
10521018
const word32 ALIGNMENT_REQ = 8;
10531019
word32 add = 0;
10541020
word32 internalBuffLen = 0;
10551021
byte* tmp = NULL;
1056-
if (ret == 1 && (mac->state.hmac.macType == WC_HASH_TYPE_SHA384 ||
1057-
mac->state.hmac.macType == WC_HASH_TYPE_SHA512)) {
1058-
internalBuffLen = mac->state.hmac.hash.sha512.buffLen;
1022+
if (hmac->macType == WC_HASH_TYPE_SHA384 ||
1023+
hmac->macType == WC_HASH_TYPE_SHA512) {
1024+
internalBuffLen = hmac->hash.sha512.buffLen;
10591025
add = dataSz > (WC_SHA512_BLOCK_SIZE - internalBuffLen) ?
10601026
(WC_SHA512_BLOCK_SIZE - internalBuffLen) : dataSz;
10611027
}
@@ -1068,7 +1034,7 @@ static int we_hmac_pkey_update(EVP_MD_CTX *ctx, const void *data, size_t dataSz)
10681034
/* Update the hash with "add" bytes of data, which will result in
10691035
* an update with a full WC_SHA512_BLOCK_SIZE number of bytes with no
10701036
* leftovers. */
1071-
rc = wc_HmacUpdate(&mac->state.hmac, (const byte*)data, add);
1037+
rc = wc_HmacUpdate(hmac, (const byte*)data, add);
10721038
if (rc != 0) {
10731039
WOLFENGINE_ERROR_FUNC(WE_LOG_MAC, "wc_HmacUpdate", rc);
10741040
ret = 0;
@@ -1085,7 +1051,7 @@ static int we_hmac_pkey_update(EVP_MD_CTX *ctx, const void *data, size_t dataSz)
10851051
/* Copy remaining data from the unaligned buffer to the aligned one
10861052
* and update the hash. */
10871053
XMEMCPY(tmp, (byte*)data + add, dataSz - add);
1088-
rc = wc_HmacUpdate(&mac->state.hmac, (const byte*)tmp,
1054+
rc = wc_HmacUpdate(hmac, (const byte*)tmp,
10891055
dataSz - add);
10901056
if (rc != 0) {
10911057
WOLFENGINE_ERROR_FUNC(WE_LOG_MAC, "wc_HmacUpdate", rc);
@@ -1099,15 +1065,74 @@ static int we_hmac_pkey_update(EVP_MD_CTX *ctx, const void *data, size_t dataSz)
10991065
}
11001066
else
11011067
#endif
1102-
if (ret == 1) {
1068+
{
11031069
/* Update the wolfCrypt HMAC object with more data. */
1104-
rc = wc_HmacUpdate(&mac->state.hmac, (const byte*)data, (word32)dataSz);
1070+
rc = wc_HmacUpdate(hmac, (const byte*)data, (word32)dataSz);
11051071
if (rc != 0) {
11061072
WOLFENGINE_ERROR_FUNC(WE_LOG_MAC, "wc_HmacUpdate", rc);
11071073
ret = 0;
11081074
}
11091075
}
11101076

1077+
WOLFENGINE_LEAVE(WE_LOG_MAC, "we_hmac_update", ret);
1078+
1079+
return ret;
1080+
}
1081+
1082+
/**
1083+
* Replacement update function for EVP_MD context.
1084+
*
1085+
* @param ctx [in] EVP_MD context being used.
1086+
* @param data [in] Data to be passed to HMAC update.
1087+
* @param dataSz [in] Size of data buffer to be passed to HMAC update.
1088+
* @returns 1 on success and 0 on failure.
1089+
*/
1090+
static int we_hmac_pkey_update(EVP_MD_CTX *ctx, const void *data, size_t dataSz)
1091+
{
1092+
int ret = 1;
1093+
we_Mac *mac;
1094+
EVP_PKEY_CTX *pkeyCtx;
1095+
1096+
WOLFENGINE_ENTER(WE_LOG_MAC, "we_hmac_pkey_update");
1097+
WOLFENGINE_MSG_VERBOSE(WE_LOG_MAC, "ARGS [ctx = %p, data = %p, "
1098+
"dataSz = %zu]", ctx, data, dataSz);
1099+
1100+
/* If this function is called with an input buffer length of 0, we need to
1101+
* return success immediately. This is how OpenSSL handles this scenario. */
1102+
if (dataSz == 0) {
1103+
WOLFENGINE_MSG(WE_LOG_MAC, "dataSz == 0, returning success.");
1104+
return 1;
1105+
}
1106+
1107+
/* Validate parameters. */
1108+
if ((ctx == NULL) || (data == NULL)) {
1109+
WOLFENGINE_ERROR_FUNC_NULL(WE_LOG_MAC,
1110+
"we_hmac_pkey_update, ctx: ", ctx);
1111+
WOLFENGINE_ERROR_FUNC_NULL(WE_LOG_MAC,
1112+
"we_hmac_pkey_update, data:", (void*)data);
1113+
ret = 0;
1114+
}
1115+
1116+
if (ret == 1) {
1117+
/* Get PKEY context from digest context. */
1118+
pkeyCtx = EVP_MD_CTX_pkey_ctx(ctx);
1119+
if (pkeyCtx == NULL) {
1120+
ret = 0;
1121+
}
1122+
}
1123+
if (ret == 1) {
1124+
/* Retrieve the internal MAC object. */
1125+
mac = (we_Mac *)EVP_PKEY_CTX_get_data(pkeyCtx);
1126+
if (mac == NULL) {
1127+
WOLFENGINE_ERROR_FUNC_NULL(WE_LOG_MAC, "EVP_PKEY_CTX_get_data",
1128+
mac);
1129+
ret = 0;
1130+
}
1131+
}
1132+
if (ret == 1) {
1133+
ret = we_hmac_update(&mac->state.hmac, data, dataSz);
1134+
}
1135+
11111136
WOLFENGINE_LEAVE(WE_LOG_MAC, "we_hmac_pkey_update", ret);
11121137

11131138
return ret;

0 commit comments

Comments
 (0)