Skip to content

Commit

Permalink
Return max *outlen in SM4 CTX update/finish
Browse files Browse the repository at this point in the history
  • Loading branch information
guanzhi committed May 11, 2024
1 parent cd5cb85 commit 2c125fb
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 12 deletions.
24 changes: 20 additions & 4 deletions src/sm4_cbc.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,14 @@ int sm4_cbc_encrypt_update(SM4_CBC_CTX *ctx,
size_t nblocks;
size_t len;

if (!ctx || !in || !out || !outlen) {
if (!ctx || !in || !outlen) {
error_print();
return -1;
}
if (!out) {
*outlen = 16 * ((inlen + 15)/16);
return 1;
}
if (ctx->block_nbytes >= SM4_BLOCK_SIZE) {
error_print();
return -1;
Expand Down Expand Up @@ -132,10 +136,14 @@ int sm4_cbc_encrypt_update(SM4_CBC_CTX *ctx,

int sm4_cbc_encrypt_finish(SM4_CBC_CTX *ctx, uint8_t *out, size_t *outlen)
{
if (!ctx || !out || !outlen) {
if (!ctx || !outlen) {
error_print();
return -1;
}
if (!out) {
*outlen = SM4_BLOCK_SIZE;
return 1;
}
if (ctx->block_nbytes >= SM4_BLOCK_SIZE) {
error_print();
return -1;
Expand Down Expand Up @@ -166,10 +174,14 @@ int sm4_cbc_decrypt_update(SM4_CBC_CTX *ctx,
{
size_t left, len, nblocks;

if (!ctx || !in || !out || !outlen) {
if (!ctx || !in || !outlen) {
error_print();
return -1;
}
if (!out) {
*outlen = 16 * ((inlen + 15)/16);
return 1;
}
if (ctx->block_nbytes > SM4_BLOCK_SIZE) {
error_print();
return -1;
Expand Down Expand Up @@ -208,10 +220,14 @@ int sm4_cbc_decrypt_update(SM4_CBC_CTX *ctx,

int sm4_cbc_decrypt_finish(SM4_CBC_CTX *ctx, uint8_t *out, size_t *outlen)
{
if (!ctx || !out || !outlen) {
if (!ctx || !outlen) {
error_print();
return -1;
}
if (!out) {
*outlen = SM4_BLOCK_SIZE;
return 1;
}
if (ctx->block_nbytes != SM4_BLOCK_SIZE) {
error_print();
return -1;
Expand Down
41 changes: 40 additions & 1 deletion src/sm4_cfb.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ void sm4_cfb_decrypt(const SM4_KEY *key, size_t sbytes, uint8_t iv[16],
int sm4_cfb_encrypt_init(SM4_CFB_CTX *ctx, size_t sbytes,
const uint8_t key[SM4_BLOCK_SIZE], const uint8_t iv[SM4_BLOCK_SIZE])
{
if (!ctx || !key || !iv) {
error_print();
return -1;
}
if (sbytes < 1 || sbytes > 16) {
error_print();
return -1;
Expand All @@ -85,6 +89,14 @@ int sm4_cfb_encrypt_update(SM4_CFB_CTX *ctx,
size_t nblocks;
size_t len;

if (!ctx || !in || !outlen) {
error_print();
return -1;
}
if (!out) {
*outlen = 16 * ((inlen + 15)/16);
return 1;
}
if (ctx->block_nbytes >= ctx->sbytes) {
error_print();
return -1;
Expand Down Expand Up @@ -122,6 +134,14 @@ int sm4_cfb_encrypt_update(SM4_CFB_CTX *ctx,

int sm4_cfb_encrypt_finish(SM4_CFB_CTX *ctx, uint8_t *out, size_t *outlen)
{
if (!ctx || !outlen) {
error_print();
return -1;
}
if (!out) {
*outlen = SM4_BLOCK_SIZE;
return 1;
}
if (ctx->block_nbytes >= ctx->sbytes) {
error_print();
return -1;
Expand All @@ -134,6 +154,10 @@ int sm4_cfb_encrypt_finish(SM4_CFB_CTX *ctx, uint8_t *out, size_t *outlen)
int sm4_cfb_decrypt_init(SM4_CFB_CTX *ctx, size_t sbytes,
const uint8_t key[SM4_BLOCK_SIZE], const uint8_t iv[SM4_BLOCK_SIZE])
{
if (!ctx || !key || !iv) {
error_print();
return -1;
}
if (sbytes < 1 || sbytes > 16) {
error_print();
return -1;
Expand All @@ -153,6 +177,14 @@ int sm4_cfb_decrypt_update(SM4_CFB_CTX *ctx,
size_t nblocks;
size_t len;

if (!ctx || !in || !outlen) {
error_print();
return -1;
}
if (!out) {
*outlen = 16 * ((inlen + 15)/16);
return 1;
}
if (ctx->block_nbytes >= ctx->sbytes) {
error_print();
return -1;
Expand Down Expand Up @@ -190,6 +222,14 @@ int sm4_cfb_decrypt_update(SM4_CFB_CTX *ctx,

int sm4_cfb_decrypt_finish(SM4_CFB_CTX *ctx, uint8_t *out, size_t *outlen)
{
if (!ctx || !outlen) {
error_print();
return -1;
}
if (!out) {
*outlen = SM4_BLOCK_SIZE;
return 1;
}
if (ctx->block_nbytes >= ctx->sbytes) {
error_print();
return -1;
Expand All @@ -198,4 +238,3 @@ int sm4_cfb_decrypt_finish(SM4_CFB_CTX *ctx, uint8_t *out, size_t *outlen)
*outlen = ctx->block_nbytes;
return 1;
}

28 changes: 26 additions & 2 deletions src/sm4_ctr.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,14 @@ int sm4_ctr_encrypt_update(SM4_CTR_CTX *ctx,
size_t nblocks;
size_t len;

if (!ctx || !in || !out || !outlen) {
if (!ctx || !in || !outlen) {
error_print();
return -1;
}
if (!out) {
*outlen = 16 * ((inlen + 15)/16);
return 1;
}
if (ctx->block_nbytes >= SM4_BLOCK_SIZE) {
error_print();
return -1;
Expand Down Expand Up @@ -111,10 +115,14 @@ int sm4_ctr_encrypt_update(SM4_CTR_CTX *ctx,

int sm4_ctr_encrypt_finish(SM4_CTR_CTX *ctx, uint8_t *out, size_t *outlen)
{
if (!ctx || !out || !outlen) {
if (!ctx || !outlen) {
error_print();
return -1;
}
if (!out) {
*outlen = SM4_BLOCK_SIZE;
return 1;
}
if (ctx->block_nbytes >= SM4_BLOCK_SIZE) {
error_print();
return -1;
Expand Down Expand Up @@ -146,6 +154,14 @@ int sm4_ctr32_encrypt_update(SM4_CTR_CTX *ctx,
size_t nblocks;
size_t len;

if (!ctx || !in || !outlen) {
error_print();
return -1;
}
if (!out) {
*outlen = 16 * ((inlen + 15)/16);
return 1;
}
if (ctx->block_nbytes >= SM4_BLOCK_SIZE) {
error_print();
return -1;
Expand Down Expand Up @@ -183,6 +199,14 @@ int sm4_ctr32_encrypt_update(SM4_CTR_CTX *ctx,

int sm4_ctr32_encrypt_finish(SM4_CTR_CTX *ctx, uint8_t *out, size_t *outlen)
{
if (!ctx || !outlen) {
error_print();
return -1;
}
if (!out) {
*outlen = SM4_BLOCK_SIZE;
return 1;
}
if (ctx->block_nbytes >= SM4_BLOCK_SIZE) {
error_print();
return -1;
Expand Down
10 changes: 9 additions & 1 deletion src/sm4_ecb.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,14 @@ int sm4_ecb_encrypt_update(SM4_ECB_CTX *ctx,
size_t nblocks;
size_t len;

if (!ctx || !in || !out || !outlen) {
if (!ctx || !in || !outlen) {
error_print();
return -1;
}
if (!out) {
*outlen = 16 * ((inlen + 15)/16);
return 1;
}
if (ctx->block_nbytes >= SM4_BLOCK_SIZE) {
error_print();
return -1;
Expand Down Expand Up @@ -77,6 +81,10 @@ int sm4_ecb_encrypt_finish(SM4_ECB_CTX *ctx, uint8_t *out, size_t *outlen)
error_print();
return -1;
}
if (!out) {
*outlen = SM4_BLOCK_SIZE; // anyway, caller should prepare a block buffer to support any length input
return 1;
}
if (ctx->block_nbytes >= SM4_BLOCK_SIZE) {
error_print();
return -1;
Expand Down
24 changes: 20 additions & 4 deletions src/sm4_gcm.c
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,14 @@ int sm4_gcm_encrypt_init(SM4_GCM_CTX *ctx,

int sm4_gcm_encrypt_update(SM4_GCM_CTX *ctx, const uint8_t *in, size_t inlen, uint8_t *out, size_t *outlen)
{
if (!ctx || !in || !out || !outlen) {
if (!ctx || !in || !outlen) {
error_print();
return -1;
}
if (!out) {
*outlen = 16 * ((inlen + 15)/16);
return 1;
}
if (sm4_ctr32_encrypt_update(&ctx->enc_ctx, in, inlen, out, outlen) != 1) {
error_print();
return -1;
Expand All @@ -164,10 +168,14 @@ int sm4_gcm_encrypt_finish(SM4_GCM_CTX *ctx, uint8_t *out, size_t *outlen)
{
uint8_t mac[16];

if (!ctx || !out || !outlen) {
if (!ctx || !outlen) {
error_print();
return -1;
}
if (!out) {
*outlen = SM4_BLOCK_SIZE * 2; // GCM output extra mac tag
return 1;
}
if (sm4_ctr32_encrypt_finish(&ctx->enc_ctx, out, outlen) != 1) {
error_print();
return -1;
Expand All @@ -193,10 +201,14 @@ int sm4_gcm_decrypt_update(SM4_GCM_CTX *ctx, const uint8_t *in, size_t inlen, ui
{
size_t len;

if (!ctx || !in || !out || !outlen) {
if (!ctx || !in || !outlen) {
error_print();
return -1;
}
if (!out) {
*outlen = 16 * ((inlen + 15)/16);
return 1;
}
if (ctx->maclen > ctx->taglen) {
error_print();
return -1;
Expand Down Expand Up @@ -251,10 +263,14 @@ int sm4_gcm_decrypt_finish(SM4_GCM_CTX *ctx, uint8_t *out, size_t *outlen)
{
uint8_t mac[GHASH_SIZE];

if (!ctx || !out || !outlen) {
if (!ctx || !outlen) {
error_print();
return -1;
}
if (!out) {
*outlen = SM4_BLOCK_SIZE;
return 1;
}
if (ctx->maclen != ctx->taglen) {
error_print();
return -1;
Expand Down
20 changes: 20 additions & 0 deletions src/sm4_ofb.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ void sm4_ofb_encrypt(const SM4_KEY *key, uint8_t iv[16], const uint8_t *in, size
int sm4_ofb_encrypt_init(SM4_OFB_CTX *ctx,
const uint8_t key[SM4_BLOCK_SIZE], const uint8_t iv[SM4_BLOCK_SIZE])
{
if (!ctx || !key || !iv) {
error_print();
return -1;
}
sm4_set_encrypt_key(&ctx->sm4_key, key);
memcpy(ctx->iv, iv, SM4_BLOCK_SIZE);
memset(ctx->block, 0, SM4_BLOCK_SIZE);
Expand All @@ -44,6 +48,14 @@ int sm4_ofb_encrypt_update(SM4_OFB_CTX *ctx,
size_t nblocks;
size_t len;

if (!ctx || !in || !outlen) {
error_print();
return -1;
}
if (!out) {
*outlen = 16 * ((inlen + 15)/16);
return 1;
}
if (ctx->block_nbytes >= SM4_BLOCK_SIZE) {
error_print();
return -1;
Expand Down Expand Up @@ -81,6 +93,14 @@ int sm4_ofb_encrypt_update(SM4_OFB_CTX *ctx,

int sm4_ofb_encrypt_finish(SM4_OFB_CTX *ctx, uint8_t *out, size_t *outlen)
{
if (!ctx || !outlen) {
error_print();
return -1;
}
if (!out) {
*outlen = SM4_BLOCK_SIZE;
return 1;
}
if (ctx->block_nbytes >= SM4_BLOCK_SIZE) {
error_print();
return -1;
Expand Down

0 comments on commit 2c125fb

Please sign in to comment.