mirror of
https://github.com/guanzhi/GmSSL.git
synced 2026-05-07 00:46:17 +08:00
Update SM4 CBC API
This commit is contained in:
@@ -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_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);
|
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);
|
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],
|
int sm4_cbc_padding_encrypt(const SM4_KEY *key, const uint8_t iv[SM4_BLOCK_SIZE],
|
||||||
|
|||||||
@@ -12,7 +12,8 @@
|
|||||||
#include <gmssl/mem.h>
|
#include <gmssl/mem.h>
|
||||||
#include <gmssl/error.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)
|
const uint8_t *in, size_t nblocks, uint8_t *out)
|
||||||
{
|
{
|
||||||
while (nblocks--) {
|
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)
|
const uint8_t *in, size_t nblocks, uint8_t *out)
|
||||||
{
|
{
|
||||||
while (nblocks >= 8) {
|
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);
|
memset(block + rem, padding, padding);
|
||||||
if (inlen/16) {
|
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;
|
out += inlen - rem;
|
||||||
iv = out - 16;
|
iv = out - 16;
|
||||||
}
|
}
|
||||||
sm4_cbc_encrypt(key, iv, block, 1, out);
|
sm4_cbc_encrypt_blocks(key, iv, block, 1, out);
|
||||||
*outlen = inlen - rem + 16;
|
*outlen = inlen - rem + 16;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@@ -89,10 +90,10 @@ int sm4_cbc_padding_decrypt(const SM4_KEY *key, const uint8_t iv[16],
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
if (inlen > 16) {
|
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;
|
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];
|
padding = block[15];
|
||||||
if (padding < 1 || padding > 16) {
|
if (padding < 1 || padding > 16) {
|
||||||
@@ -135,7 +136,7 @@ int sm4_cbc_encrypt_update(SM4_CBC_CTX *ctx,
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
memcpy(ctx->block + ctx->block_nbytes, in, left);
|
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);
|
memcpy(ctx->iv, out, SM4_BLOCK_SIZE);
|
||||||
in += left;
|
in += left;
|
||||||
inlen -= left;
|
inlen -= left;
|
||||||
@@ -145,7 +146,7 @@ int sm4_cbc_encrypt_update(SM4_CBC_CTX *ctx,
|
|||||||
if (inlen >= SM4_BLOCK_SIZE) {
|
if (inlen >= SM4_BLOCK_SIZE) {
|
||||||
nblocks = inlen / SM4_BLOCK_SIZE;
|
nblocks = inlen / SM4_BLOCK_SIZE;
|
||||||
len = nblocks * 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);
|
memcpy(ctx->iv, out + len - SM4_BLOCK_SIZE, SM4_BLOCK_SIZE);
|
||||||
in += len;
|
in += len;
|
||||||
inlen -= len;
|
inlen -= len;
|
||||||
@@ -201,7 +202,7 @@ int sm4_cbc_decrypt_update(SM4_CBC_CTX *ctx,
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
memcpy(ctx->block + ctx->block_nbytes, in, left);
|
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);
|
memcpy(ctx->iv, ctx->block, SM4_BLOCK_SIZE);
|
||||||
in += left;
|
in += left;
|
||||||
inlen -= left;
|
inlen -= left;
|
||||||
@@ -211,7 +212,7 @@ int sm4_cbc_decrypt_update(SM4_CBC_CTX *ctx,
|
|||||||
if (inlen > SM4_BLOCK_SIZE) {
|
if (inlen > SM4_BLOCK_SIZE) {
|
||||||
nblocks = (inlen-1) / SM4_BLOCK_SIZE;
|
nblocks = (inlen-1) / SM4_BLOCK_SIZE;
|
||||||
len = nblocks * 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);
|
memcpy(ctx->iv, in + len - SM4_BLOCK_SIZE, SM4_BLOCK_SIZE);
|
||||||
in += len;
|
in += len;
|
||||||
inlen -= len;
|
inlen -= len;
|
||||||
|
|||||||
@@ -312,11 +312,11 @@ int tls_cbc_encrypt(const SM3_HMAC_CTX *inited_hmac_ctx, const SM4_KEY *enc_key,
|
|||||||
out += 16;
|
out += 16;
|
||||||
|
|
||||||
if (inlen >= 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;
|
out += inlen - rem;
|
||||||
iv = out - 16;
|
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);
|
*outlen = 16 + inlen - rem + sizeof(last_blocks);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@@ -349,7 +349,7 @@ int tls_cbc_decrypt(const SM3_HMAC_CTX *inited_hmac_ctx, const SM4_KEY *dec_key,
|
|||||||
in += 16;
|
in += 16;
|
||||||
inlen -= 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_len = out[inlen - 1];
|
||||||
padding = out + inlen - padding_len - 1;
|
padding = out + inlen - padding_len - 1;
|
||||||
|
|||||||
@@ -28,9 +28,9 @@ static int test_sm4_cbc(void)
|
|||||||
uint8_t buf3[32] = {0};
|
uint8_t buf3[32] = {0};
|
||||||
|
|
||||||
sm4_set_encrypt_key(&sm4_key, key);
|
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_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) {
|
if (memcmp(buf1, buf3, sizeof(buf3)) != 0) {
|
||||||
fprintf(stderr, "%s %d: error\n", __FILE__, __LINE__);
|
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_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) {
|
if (memcmp(encrypted, ciphertext, ciphertext_len) != 0) {
|
||||||
error_print();
|
error_print();
|
||||||
|
|||||||
Reference in New Issue
Block a user