Update aead.c

Fix bug in GCM tmp buffer size
This commit is contained in:
Zhi Guan
2023-03-13 15:47:01 +08:00
parent b6dc8383c7
commit a3aa4e3104

View File

@@ -123,7 +123,7 @@ int sm4_cbc_sm3_hmac_decrypt_update(SM4_CBC_SM3_HMAC_CTX *ctx, const uint8_t *in
} }
if (inlen <= SM3_HMAC_SIZE) { if (inlen <= SM3_HMAC_SIZE) {
uint8_t tmp[32]; uint8_t tmp[SM3_HMAC_SIZE];
sm3_hmac_update(&ctx->mac_ctx, ctx->mac, inlen); sm3_hmac_update(&ctx->mac_ctx, ctx->mac, inlen);
if (sm4_cbc_decrypt_update(&ctx->enc_ctx, ctx->mac, inlen, out, outlen) != 1) { if (sm4_cbc_decrypt_update(&ctx->enc_ctx, ctx->mac, inlen, out, outlen) != 1) {
error_print(); error_print();
@@ -132,7 +132,7 @@ int sm4_cbc_sm3_hmac_decrypt_update(SM4_CBC_SM3_HMAC_CTX *ctx, const uint8_t *in
len = SM3_HMAC_SIZE - inlen; len = SM3_HMAC_SIZE - inlen;
memcpy(tmp, ctx->mac + inlen, len); memcpy(tmp, ctx->mac + inlen, len);
memcpy(tmp + len, in, inlen); memcpy(tmp + len, in, inlen);
memcpy(ctx->mac, tmp, 32); memcpy(ctx->mac, tmp, SM3_HMAC_SIZE);
} else { } else {
sm3_hmac_update(&ctx->mac_ctx, ctx->mac, SM3_HMAC_SIZE); sm3_hmac_update(&ctx->mac_ctx, ctx->mac, SM3_HMAC_SIZE);
if (sm4_cbc_decrypt_update(&ctx->enc_ctx, ctx->mac, SM3_HMAC_SIZE, out, outlen) != 1) { if (sm4_cbc_decrypt_update(&ctx->enc_ctx, ctx->mac, SM3_HMAC_SIZE, out, outlen) != 1) {
@@ -285,7 +285,7 @@ int sm4_ctr_sm3_hmac_decrypt_update(SM4_CTR_SM3_HMAC_CTX *ctx, const uint8_t *in
} }
if (inlen <= SM3_HMAC_SIZE) { if (inlen <= SM3_HMAC_SIZE) {
uint8_t tmp[32]; uint8_t tmp[SM3_HMAC_SIZE];
sm3_hmac_update(&ctx->mac_ctx, ctx->mac, inlen); sm3_hmac_update(&ctx->mac_ctx, ctx->mac, inlen);
if (sm4_ctr_decrypt_update(&ctx->enc_ctx, ctx->mac, inlen, out, outlen) != 1) { if (sm4_ctr_decrypt_update(&ctx->enc_ctx, ctx->mac, inlen, out, outlen) != 1) {
error_print(); error_print();
@@ -294,7 +294,7 @@ int sm4_ctr_sm3_hmac_decrypt_update(SM4_CTR_SM3_HMAC_CTX *ctx, const uint8_t *in
len = SM3_HMAC_SIZE - inlen; len = SM3_HMAC_SIZE - inlen;
memcpy(tmp, ctx->mac + inlen, len); memcpy(tmp, ctx->mac + inlen, len);
memcpy(tmp + len, in, inlen); memcpy(tmp + len, in, inlen);
memcpy(ctx->mac, tmp, 32); memcpy(ctx->mac, tmp, SM3_HMAC_SIZE);
} else { } else {
sm3_hmac_update(&ctx->mac_ctx, ctx->mac, SM3_HMAC_SIZE); sm3_hmac_update(&ctx->mac_ctx, ctx->mac, SM3_HMAC_SIZE);
if (sm4_ctr_decrypt_update(&ctx->enc_ctx, ctx->mac, SM3_HMAC_SIZE, out, outlen) != 1) { if (sm4_ctr_decrypt_update(&ctx->enc_ctx, ctx->mac, SM3_HMAC_SIZE, out, outlen) != 1) {
@@ -475,7 +475,7 @@ int sm4_gcm_decrypt_update(SM4_GCM_CTX *ctx, const uint8_t *in, size_t inlen, ui
} }
if (inlen <= ctx->taglen) { if (inlen <= ctx->taglen) {
uint8_t tmp[32]; uint8_t tmp[GHASH_SIZE];
ghash_update(&ctx->mac_ctx, ctx->mac, inlen); ghash_update(&ctx->mac_ctx, ctx->mac, inlen);
if (sm4_ctr_decrypt_update(&ctx->enc_ctx, ctx->mac, inlen, out, outlen) != 1) { if (sm4_ctr_decrypt_update(&ctx->enc_ctx, ctx->mac, inlen, out, outlen) != 1) {
error_print(); error_print();
@@ -484,7 +484,7 @@ int sm4_gcm_decrypt_update(SM4_GCM_CTX *ctx, const uint8_t *in, size_t inlen, ui
len = ctx->taglen - inlen; len = ctx->taglen - inlen;
memcpy(tmp, ctx->mac + inlen, len); memcpy(tmp, ctx->mac + inlen, len);
memcpy(tmp + len, in, inlen); memcpy(tmp + len, in, inlen);
memcpy(ctx->mac, tmp, 32); memcpy(ctx->mac, tmp, GHASH_SIZE);
} else { } else {
ghash_update(&ctx->mac_ctx, ctx->mac, ctx->taglen); ghash_update(&ctx->mac_ctx, ctx->mac, ctx->taglen);
if (sm4_ctr_decrypt_update(&ctx->enc_ctx, ctx->mac, ctx->taglen, out, outlen) != 1) { if (sm4_ctr_decrypt_update(&ctx->enc_ctx, ctx->mac, ctx->taglen, out, outlen) != 1) {