Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix incrementing function of gcm mode #1610

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/aead.c
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,15 @@ static void ctr_incr(uint8_t a[16])
}
}

static void gcm_incr(uint8_t a[16])
{
int i;
for (i = 15; i >= 12; i--) {
a[i]++;
if (a[i]) break;
}
}

int sm4_gcm_encrypt_init(SM4_GCM_CTX *ctx,
const uint8_t *key, size_t keylen, const uint8_t *iv, size_t ivlen,
const uint8_t *aad, size_t aadlen, size_t taglen)
Expand Down Expand Up @@ -396,7 +405,7 @@ int sm4_gcm_encrypt_init(SM4_GCM_CTX *ctx,

sm4_encrypt(&ctx->enc_ctx.sm4_key, Y, ctx->Y);

ctr_incr(Y);
gcm_incr(Y);
memcpy(ctx->enc_ctx.ctr, Y, 16);

gmssl_secure_clear(H, sizeof(H));
Expand Down
13 changes: 11 additions & 2 deletions src/aes_modes.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,15 @@ static void ctr_incr(uint8_t a[16])
}
}

static void gcm_incr(uint8_t a[16])
{
int i;
for (i = 15; i >= 12; i--) {
a[i]++;
if (a[i]) break;
}
}

void aes_ctr_encrypt(const AES_KEY *key, uint8_t ctr[16], const uint8_t *in, size_t inlen, uint8_t *out)
{
uint8_t block[16];
Expand Down Expand Up @@ -152,7 +161,7 @@ int aes_gcm_encrypt(const AES_KEY *key, const uint8_t *iv, size_t ivlen,
while (left) {
uint8_t block[16];
size_t len = left < 16 ? left : 16;
ctr_incr(Y);
gcm_incr(Y);
aes_encrypt(key, Y, block);
gmssl_memxor(pout, pin, block, len);
pin += len;
Expand Down Expand Up @@ -197,7 +206,7 @@ int aes_gcm_decrypt(const AES_KEY *key, const uint8_t *iv, size_t ivlen,
while (left) {
uint8_t block[16];
size_t len = left < 16 ? left : 16;
ctr_incr(Y);
gcm_incr(Y);
aes_encrypt(key, Y, block);
gmssl_memxor(pout, pin, block, len);
pin += len;
Expand Down
13 changes: 11 additions & 2 deletions src/sm4_modes.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,15 @@ static void ctr_incr(uint8_t a[16])
}
}

static void gcm_incr(uint8_t a[16])
{
int i;
for (i = 15; i >= 12; i--) {
a[i]++;
if (a[i]) break;
}
}

#ifndef ENABLE_SM4_AESNI_AVX
void sm4_ctr_encrypt(const SM4_KEY *key, uint8_t ctr[16], const uint8_t *in, size_t inlen, uint8_t *out)
{
Expand Down Expand Up @@ -150,7 +159,7 @@ int sm4_gcm_encrypt(const SM4_KEY *key, const uint8_t *iv, size_t ivlen,
while (left) {
uint8_t block[16];
size_t len = left < 16 ? left : 16;
ctr_incr(Y);
gcm_incr(Y);
sm4_encrypt(key, Y, block);
gmssl_memxor(pout, pin, block, len);
pin += len;
Expand Down Expand Up @@ -195,7 +204,7 @@ int sm4_gcm_decrypt(const SM4_KEY *key, const uint8_t *iv, size_t ivlen,
while (left) {
uint8_t block[16];
size_t len = left < 16 ? left : 16;
ctr_incr(Y);
gcm_incr(Y);
sm4_encrypt(key, Y, block);
gmssl_memxor(pout, pin, block, len);
pin += len;
Expand Down