Skip to content

Commit 35fd1e8

Browse files
authored
Merge pull request #145 from haydenroche5/alignment_safety_const_mem
Update alignment safety code to use a constant amount of extra memory for the temp buffer.
2 parents 9c3519e + 7057031 commit 35fd1e8

File tree

2 files changed

+33
-16
lines changed

2 files changed

+33
-16
lines changed

src/we_digest.c

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -834,7 +834,7 @@ static int we_digest_update(EVP_MD_CTX *ctx, const void *data, size_t len)
834834
}
835835
if (ret == 1) {
836836
/* Allocate new, aligned buffer. */
837-
tmp = (byte*)XMALLOC(len - add, NULL, DYNAMIC_TYPE_TMP_BUFFER);
837+
tmp = (byte*)XMALLOC(WC_SHA512_BLOCK_SIZE, NULL, DYNAMIC_TYPE_TMP_BUFFER);
838838
if (tmp == NULL) {
839839
WOLFENGINE_ERROR_FUNC_NULL(WE_LOG_DIGEST, "XMALLOC",
840840
tmp);
@@ -843,13 +843,22 @@ static int we_digest_update(EVP_MD_CTX *ctx, const void *data, size_t len)
843843
}
844844
if (ret == 1) {
845845
/* Copy remaining data from the unaligned buffer to the aligned one
846-
* and update the hash. */
847-
XMEMCPY(tmp, (byte*)data + add, len - add);
848-
rc = wc_HashUpdate(&digest->hash, digest->hashType,
849-
(const byte*)tmp, len - add);
850-
if (rc != 0) {
851-
WOLFENGINE_ERROR_FUNC(WE_LOG_DIGEST, "wc_HashUpdate", rc);
852-
ret = 0;
846+
* and update the hash iteratively, one block's worth of data at a
847+
* time. */
848+
byte* nextData = (byte*)data + add;
849+
for (size_t remaining = len - add; remaining > 0;) {
850+
size_t nextLen = (remaining <= WC_SHA512_BLOCK_SIZE) ?
851+
remaining : WC_SHA512_BLOCK_SIZE;
852+
XMEMCPY(tmp, nextData, nextLen);
853+
rc = wc_HashUpdate(&digest->hash, digest->hashType,
854+
(const byte*)tmp, nextLen);
855+
if (rc != 0) {
856+
WOLFENGINE_ERROR_FUNC(WE_LOG_DIGEST, "wc_HashUpdate", rc);
857+
ret = 0;
858+
break;
859+
}
860+
nextData += nextLen;
861+
remaining -= nextLen;
853862
}
854863
}
855864

src/we_mac.c

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,21 +1041,29 @@ int we_hmac_update(Hmac* hmac, const void *data, size_t dataSz) {
10411041
}
10421042
if (ret == 1) {
10431043
/* Allocate new, aligned buffer. */
1044-
tmp = (byte*)XMALLOC(dataSz - add, NULL, DYNAMIC_TYPE_TMP_BUFFER);
1044+
tmp = (byte*)XMALLOC(WC_SHA512_BLOCK_SIZE, NULL, DYNAMIC_TYPE_TMP_BUFFER);
10451045
if (tmp == NULL) {
10461046
WOLFENGINE_ERROR_FUNC_NULL(WE_LOG_MAC, "XMALLOC", tmp);
10471047
ret = 0;
10481048
}
10491049
}
10501050
if (ret == 1) {
10511051
/* Copy remaining data from the unaligned buffer to the aligned one
1052-
* and update the hash. */
1053-
XMEMCPY(tmp, (byte*)data + add, dataSz - add);
1054-
rc = wc_HmacUpdate(hmac, (const byte*)tmp,
1055-
dataSz - add);
1056-
if (rc != 0) {
1057-
WOLFENGINE_ERROR_FUNC(WE_LOG_MAC, "wc_HmacUpdate", rc);
1058-
ret = 0;
1052+
* and update the hash iteratively, one block's worth of data at a
1053+
* time. */
1054+
byte* nextData = (byte*)data + add;
1055+
for (size_t remaining = dataSz - add; remaining > 0;) {
1056+
size_t nextLen = (remaining <= WC_SHA512_BLOCK_SIZE) ?
1057+
remaining : WC_SHA512_BLOCK_SIZE;
1058+
XMEMCPY(tmp, nextData, nextLen);
1059+
rc = wc_HmacUpdate(hmac, (const byte*)tmp, nextLen);
1060+
if (rc != 0) {
1061+
WOLFENGINE_ERROR_FUNC(WE_LOG_MAC, "wc_HmacUpdate", rc);
1062+
ret = 0;
1063+
break;
1064+
}
1065+
nextData += nextLen;
1066+
remaining -= nextLen;
10591067
}
10601068
}
10611069

0 commit comments

Comments
 (0)