Skip to content

Commit

Permalink
Update SM4 CBC API
Browse files Browse the repository at this point in the history
  • Loading branch information
guanzhi committed Apr 28, 2024
1 parent 660b4cf commit 6e8a36c
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 18 deletions.
4 changes: 2 additions & 2 deletions include/gmssl/sm4.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ void sm4_encrypt(const SM4_KEY *key, const uint8_t in[SM4_BLOCK_SIZE], uint8_t o
void sm4_encrypt_blocks(const SM4_KEY *key, const uint8_t *in, size_t nblocks, uint8_t *out);


void sm4_cbc_encrypt(const SM4_KEY *key, const uint8_t iv[SM4_BLOCK_SIZE],
void sm4_cbc_encrypt_blocks(const SM4_KEY *key, const uint8_t iv[SM4_BLOCK_SIZE],
const uint8_t *in, size_t nblocks, uint8_t *out);
void sm4_cbc_decrypt(const SM4_KEY *key, const uint8_t iv[SM4_BLOCK_SIZE],
void sm4_cbc_decrypt_blocks(const SM4_KEY *key, const uint8_t iv[SM4_BLOCK_SIZE],
const uint8_t *in, size_t nblocks, uint8_t *out);

int sm4_cbc_padding_encrypt(const SM4_KEY *key, const uint8_t iv[SM4_BLOCK_SIZE],
Expand Down
21 changes: 11 additions & 10 deletions src/sm4_cbc.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
#include <gmssl/mem.h>
#include <gmssl/error.h>

void sm4_cbc_encrypt(const SM4_KEY *key, const uint8_t iv[16],

void sm4_cbc_encrypt_blocks(const SM4_KEY *key, const uint8_t iv[16],
const uint8_t *in, size_t nblocks, uint8_t *out)
{
while (nblocks--) {
Expand All @@ -24,7 +25,7 @@ void sm4_cbc_encrypt(const SM4_KEY *key, const uint8_t iv[16],
}
}

void sm4_cbc_decrypt(const SM4_KEY *key, const uint8_t iv[16],
void sm4_cbc_decrypt_blocks(const SM4_KEY *key, const uint8_t iv[16],
const uint8_t *in, size_t nblocks, uint8_t *out)
{
while (nblocks >= 8) {
Expand Down Expand Up @@ -63,11 +64,11 @@ int sm4_cbc_padding_encrypt(const SM4_KEY *key, const uint8_t iv[16],
}
memset(block + rem, padding, padding);
if (inlen/16) {
sm4_cbc_encrypt(key, iv, in, inlen/16, out);
sm4_cbc_encrypt_blocks(key, iv, in, inlen/16, out);
out += inlen - rem;
iv = out - 16;
}
sm4_cbc_encrypt(key, iv, block, 1, out);
sm4_cbc_encrypt_blocks(key, iv, block, 1, out);
*outlen = inlen - rem + 16;
return 1;
}
Expand All @@ -89,10 +90,10 @@ int sm4_cbc_padding_decrypt(const SM4_KEY *key, const uint8_t iv[16],
return -1;
}
if (inlen > 16) {
sm4_cbc_decrypt(key, iv, in, inlen/16 - 1, out);
sm4_cbc_decrypt_blocks(key, iv, in, inlen/16 - 1, out);
iv = in + inlen - 32;
}
sm4_cbc_decrypt(key, iv, in + inlen - 16, 1, block);
sm4_cbc_decrypt_blocks(key, iv, in + inlen - 16, 1, block);

padding = block[15];
if (padding < 1 || padding > 16) {
Expand Down Expand Up @@ -135,7 +136,7 @@ int sm4_cbc_encrypt_update(SM4_CBC_CTX *ctx,
return 1;
}
memcpy(ctx->block + ctx->block_nbytes, in, left);
sm4_cbc_encrypt(&ctx->sm4_key, ctx->iv, ctx->block, 1, out);
sm4_cbc_encrypt_blocks(&ctx->sm4_key, ctx->iv, ctx->block, 1, out);
memcpy(ctx->iv, out, SM4_BLOCK_SIZE);
in += left;
inlen -= left;
Expand All @@ -145,7 +146,7 @@ int sm4_cbc_encrypt_update(SM4_CBC_CTX *ctx,
if (inlen >= SM4_BLOCK_SIZE) {
nblocks = inlen / SM4_BLOCK_SIZE;
len = nblocks * SM4_BLOCK_SIZE;
sm4_cbc_encrypt(&ctx->sm4_key, ctx->iv, in, nblocks, out);
sm4_cbc_encrypt_blocks(&ctx->sm4_key, ctx->iv, in, nblocks, out);
memcpy(ctx->iv, out + len - SM4_BLOCK_SIZE, SM4_BLOCK_SIZE);
in += len;
inlen -= len;
Expand Down Expand Up @@ -201,7 +202,7 @@ int sm4_cbc_decrypt_update(SM4_CBC_CTX *ctx,
return 1;
}
memcpy(ctx->block + ctx->block_nbytes, in, left);
sm4_cbc_decrypt(&ctx->sm4_key, ctx->iv, ctx->block, 1, out);
sm4_cbc_decrypt_blocks(&ctx->sm4_key, ctx->iv, ctx->block, 1, out);
memcpy(ctx->iv, ctx->block, SM4_BLOCK_SIZE);
in += left;
inlen -= left;
Expand All @@ -211,7 +212,7 @@ int sm4_cbc_decrypt_update(SM4_CBC_CTX *ctx,
if (inlen > SM4_BLOCK_SIZE) {
nblocks = (inlen-1) / SM4_BLOCK_SIZE;
len = nblocks * SM4_BLOCK_SIZE;
sm4_cbc_decrypt(&ctx->sm4_key, ctx->iv, in, nblocks, out);
sm4_cbc_decrypt_blocks(&ctx->sm4_key, ctx->iv, in, nblocks, out);
memcpy(ctx->iv, in + len - SM4_BLOCK_SIZE, SM4_BLOCK_SIZE);
in += len;
inlen -= len;
Expand Down
6 changes: 3 additions & 3 deletions src/tls.c
Original file line number Diff line number Diff line change
Expand Up @@ -312,11 +312,11 @@ int tls_cbc_encrypt(const SM3_HMAC_CTX *inited_hmac_ctx, const SM4_KEY *enc_key,
out += 16;

if (inlen >= 16) {
sm4_cbc_encrypt(enc_key, iv, in, inlen/16, out);
sm4_cbc_encrypt_blocks(enc_key, iv, in, inlen/16, out);
out += inlen - rem;
iv = out - 16;
}
sm4_cbc_encrypt(enc_key, iv, last_blocks, sizeof(last_blocks)/16, out);
sm4_cbc_encrypt_blocks(enc_key, iv, last_blocks, sizeof(last_blocks)/16, out);
*outlen = 16 + inlen - rem + sizeof(last_blocks);
return 1;
}
Expand Down Expand Up @@ -349,7 +349,7 @@ int tls_cbc_decrypt(const SM3_HMAC_CTX *inited_hmac_ctx, const SM4_KEY *dec_key,
in += 16;
inlen -= 16;

sm4_cbc_decrypt(dec_key, iv, in, inlen/16, out);
sm4_cbc_decrypt_blocks(dec_key, iv, in, inlen/16, out);

padding_len = out[inlen - 1];
padding = out + inlen - padding_len - 1;
Expand Down
6 changes: 3 additions & 3 deletions tests/sm4_cbctest.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ static int test_sm4_cbc(void)
uint8_t buf3[32] = {0};

sm4_set_encrypt_key(&sm4_key, key);
sm4_cbc_encrypt(&sm4_key, iv, buf1, 2, buf2);
sm4_cbc_encrypt_blocks(&sm4_key, iv, buf1, 2, buf2);
sm4_set_decrypt_key(&sm4_key, key);
sm4_cbc_decrypt(&sm4_key, iv, buf2, 2, buf3);
sm4_cbc_decrypt_blocks(&sm4_key, iv, buf2, 2, buf3);

if (memcmp(buf1, buf3, sizeof(buf3)) != 0) {
fprintf(stderr, "%s %d: error\n", __FILE__, __LINE__);
Expand Down Expand Up @@ -107,7 +107,7 @@ static int test_sm4_cbc_test_vectors(void)
}

sm4_set_encrypt_key(&sm4_key, key);
sm4_cbc_encrypt(&sm4_key, iv, plaintext, plaintext_len/16, encrypted);
sm4_cbc_encrypt_blocks(&sm4_key, iv, plaintext, plaintext_len/16, encrypted);

if (memcmp(encrypted, ciphertext, ciphertext_len) != 0) {
error_print();
Expand Down

0 comments on commit 6e8a36c

Please sign in to comment.