diff --git a/include/gmssl/sm4.h b/include/gmssl/sm4.h index a55c10f6..bd8f4d2a 100644 --- a/include/gmssl/sm4.h +++ b/include/gmssl/sm4.h @@ -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], diff --git a/src/sm4_cbc.c b/src/sm4_cbc.c index 0abb1eea..7c73ba5f 100644 --- a/src/sm4_cbc.c +++ b/src/sm4_cbc.c @@ -12,7 +12,8 @@ #include #include -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--) { @@ -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) { @@ -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; } @@ -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) { @@ -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; @@ -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; @@ -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; @@ -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; diff --git a/src/tls.c b/src/tls.c index b2d43af1..296ef947 100644 --- a/src/tls.c +++ b/src/tls.c @@ -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; } @@ -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; diff --git a/tests/sm4_cbctest.c b/tests/sm4_cbctest.c index bbacf921..d657671c 100644 --- a/tests/sm4_cbctest.c +++ b/tests/sm4_cbctest.c @@ -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__); @@ -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();