Skip to content

Commit

Permalink
Update SM4 API
Browse files Browse the repository at this point in the history
  • Loading branch information
guanzhi committed Apr 27, 2024
1 parent 3f1fdc1 commit bc15f7a
Show file tree
Hide file tree
Showing 8 changed files with 157 additions and 113 deletions.
11 changes: 10 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ include_directories(include)

add_compile_options(-O3)

option(ENABLE_TEST_SPEED "Enable test speed" ON)

option(ENABLE_SM4_TBOX "Enable SM4 merged S-Box implementation" ON)
option(ENABLE_SM4_AARCH64 "Enable SM4 AARCH64 assembly implementation" OFF)
option(ENABLE_SM4_CTR_AESNI_AVX "Enable SM4 CTR AESNI+AVX assembly implementation" OFF)
Expand All @@ -30,7 +32,7 @@ option(ENABLE_GMUL_AARCH64 "Enable GF(2^128) Multiplication AArch64 assembly" OF
set(src
src/version.c
src/debug.c
src/sm4.c
src/sm4_arm_neon.c
src/sm4_cbc.c
src/sm4_ctr.c
src/sm4_gcm.c
Expand Down Expand Up @@ -84,6 +86,7 @@ set(src
src/tls12.c
src/tls13.c
src/file.c
src/file.c
)

set(tools
Expand Down Expand Up @@ -245,6 +248,12 @@ include(CheckSymbolExists)
# when an option has been enabled, `cmake ..` will not refresh the value
# use `cmake .. -DENABLE_XXX=OFF` to disable the option


if (ENABLE_TEST_SPEED)
message(STATUS "ENABLE_TEST_SPEED is ON")
add_definitions(-DENABLE_TEST_SPEED)
endif()

option(ENABLE_SM2_ALGOR_ID_ENCODE_NULL "Enable AlgorithmIdenifier with algorithm sm2sign_with_sm3 encode a NULL object as parameters" OFF)
if (ENABLE_SM2_ALGOR_ID_ENCODE_NULL)
message(STATUS "ENABLE_SM2_ALGOR_ID_ENCODE_NULL is ON")
Expand Down
7 changes: 6 additions & 1 deletion include/gmssl/sm4.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ _gmssl_export int sm4_cbc_decrypt_finish(SM4_CBC_CTX *ctx, uint8_t *out, size_t

void sm4_ctr_encrypt(const SM4_KEY *key, uint8_t ctr[SM4_BLOCK_SIZE],
const uint8_t *in, size_t inlen, uint8_t *out);
void sm4_ctr32_encrypt(const SM4_KEY *key, uint8_t ctr[SM4_BLOCK_SIZE],
const uint8_t *in, size_t inlen, uint8_t *out);

typedef struct {
union {
Expand All @@ -79,6 +81,9 @@ typedef struct {
_gmssl_export int sm4_ctr_encrypt_init(SM4_CTR_CTX *ctx, const uint8_t key[SM4_KEY_SIZE], const uint8_t ctr[SM4_BLOCK_SIZE]);
_gmssl_export int sm4_ctr_encrypt_update(SM4_CTR_CTX *ctx, const uint8_t *in, size_t inlen, uint8_t *out, size_t *outlen);
_gmssl_export int sm4_ctr_encrypt_finish(SM4_CTR_CTX *ctx, uint8_t *out, size_t *outlen);
int sm4_ctr32_encrypt_init(SM4_CTR_CTX *ctx, const uint8_t key[SM4_KEY_SIZE], const uint8_t ctr[SM4_BLOCK_SIZE]);
int sm4_ctr32_encrypt_update(SM4_CTR_CTX *ctx, const uint8_t *in, size_t inlen, uint8_t *out, size_t *outlen);
int sm4_ctr32_encrypt_finish(SM4_CTR_CTX *ctx, uint8_t *out, size_t *outlen);


#define NIST_SP800_GCM_MAX_IV_SIZE (((uint64_t)1 << (64-3)) - 1) // 2305843009213693951
Expand Down Expand Up @@ -134,7 +139,7 @@ _gmssl_export int sm4_gcm_decrypt_finish(SM4_GCM_CTX *ctx,

#ifdef ENABLE_SM4_ECB
// call `sm4_set_decrypt_key` before decrypt
void sm4_ecb_encrypt(const SM4_KEY *key, const uint8_t *in, size_t nblocks, uint8_t *out);
void sm4_ecb_encrypt_blocks(const SM4_KEY *key, const uint8_t *in, size_t nblocks, uint8_t *out);

typedef struct {
SM4_KEY sm4_key;
Expand Down
89 changes: 89 additions & 0 deletions src/sm4_ctr.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,92 @@ int sm4_ctr_encrypt_finish(SM4_CTR_CTX *ctx, uint8_t *out, size_t *outlen)
*outlen = ctx->block_nbytes;
return 1;
}

// inc32() in nist-sp800-38d
static void ctr32_incr(uint8_t a[16])
{
int i;
for (i = 15; i >= 12; i--) {
a[i]++;
if (a[i]) break;
}
}

void sm4_ctr32_encrypt(const SM4_KEY *key, uint8_t ctr[16], const uint8_t *in, size_t inlen, uint8_t *out)
{
uint8_t block[16];
size_t len;

while (inlen) {
len = inlen < 16 ? inlen : 16;
sm4_encrypt(key, ctr, block);
gmssl_memxor(out, in, block, len);
ctr32_incr(ctr);
in += len;
out += len;
inlen -= len;
}
}

int sm4_ctr32_encrypt_init(SM4_CTR_CTX *ctx,
const uint8_t key[SM4_BLOCK_SIZE], const uint8_t ctr[SM4_BLOCK_SIZE])
{
sm4_set_encrypt_key(&ctx->sm4_key, key);
memcpy(ctx->ctr, ctr, SM4_BLOCK_SIZE);
memset(ctx->block, 0, SM4_BLOCK_SIZE);
ctx->block_nbytes = 0;
return 1;
}

int sm4_ctr32_encrypt_update(SM4_CTR_CTX *ctx,
const uint8_t *in, size_t inlen, uint8_t *out, size_t *outlen)
{
size_t left;
size_t nblocks;
size_t len;

if (ctx->block_nbytes >= SM4_BLOCK_SIZE) {
error_print();
return -1;
}
*outlen = 0;
if (ctx->block_nbytes) {
left = SM4_BLOCK_SIZE - ctx->block_nbytes;
if (inlen < left) {
memcpy(ctx->block + ctx->block_nbytes, in, inlen);
ctx->block_nbytes += inlen;
return 1;
}
memcpy(ctx->block + ctx->block_nbytes, in, left);
sm4_ctr32_encrypt(&ctx->sm4_key, ctx->ctr, ctx->block, SM4_BLOCK_SIZE, out);
in += left;
inlen -= left;
out += SM4_BLOCK_SIZE;
*outlen += SM4_BLOCK_SIZE;
}
if (inlen >= SM4_BLOCK_SIZE) {
nblocks = inlen / SM4_BLOCK_SIZE;
len = nblocks * SM4_BLOCK_SIZE;
sm4_ctr32_encrypt(&ctx->sm4_key, ctx->ctr, in, len, out);
in += len;
inlen -= len;
out += len;
*outlen += len;
}
if (inlen) {
memcpy(ctx->block, in, inlen);
}
ctx->block_nbytes = inlen;
return 1;
}

int sm4_ctr32_encrypt_finish(SM4_CTR_CTX *ctx, uint8_t *out, size_t *outlen)
{
if (ctx->block_nbytes >= SM4_BLOCK_SIZE) {
error_print();
return -1;
}
sm4_ctr32_encrypt(&ctx->sm4_key, ctx->ctr, ctx->block, ctx->block_nbytes, out);
*outlen = ctx->block_nbytes;
return 1;
}
6 changes: 3 additions & 3 deletions src/sm4_ecb.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include <gmssl/error.h>


void sm4_ecb_encrypt(const SM4_KEY *key, const uint8_t *in, size_t nblocks, uint8_t *out)
void sm4_ecb_encrypt_blocks(const SM4_KEY *key, const uint8_t *in, size_t nblocks, uint8_t *out)
{
while (nblocks--) {
sm4_encrypt(key, in, out);
Expand Down Expand Up @@ -50,7 +50,7 @@ int sm4_ecb_encrypt_update(SM4_ECB_CTX *ctx,
return 1;
}
memcpy(ctx->block + ctx->block_nbytes, in, left);
sm4_ecb_encrypt(&ctx->sm4_key, ctx->block, 1, out);
sm4_ecb_encrypt_blocks(&ctx->sm4_key, ctx->block, 1, out);
in += left;
inlen -= left;
out += SM4_BLOCK_SIZE;
Expand All @@ -59,7 +59,7 @@ int sm4_ecb_encrypt_update(SM4_ECB_CTX *ctx,
if (inlen >= SM4_BLOCK_SIZE) {
nblocks = inlen / SM4_BLOCK_SIZE;
len = nblocks * SM4_BLOCK_SIZE;
sm4_ecb_encrypt(&ctx->sm4_key, in, nblocks, out);
sm4_ecb_encrypt_blocks(&ctx->sm4_key, in, nblocks, out);
in += len;
inlen -= len;
out += len;
Expand Down
79 changes: 0 additions & 79 deletions src/sm4_gcm.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,6 @@ static void ctr32_incr(uint8_t a[16])
}
}

static void sm4_ctr32_encrypt(const SM4_KEY *key, uint8_t ctr[16], const uint8_t *in, size_t inlen, uint8_t *out)
{
uint8_t block[16];
size_t len;

while (inlen) {
len = inlen < 16 ? inlen : 16;
sm4_encrypt(key, ctr, block);
gmssl_memxor(out, in, block, len);
ctr32_incr(ctr);
in += len;
out += len;
inlen -= len;
}
}

int sm4_gcm_encrypt(const SM4_KEY *key, const uint8_t *iv, size_t ivlen,
const uint8_t *aad, size_t aadlen, const uint8_t *in, size_t inlen,
uint8_t *out, size_t taglen, uint8_t *tag)
Expand Down Expand Up @@ -108,69 +92,6 @@ int sm4_gcm_decrypt(const SM4_KEY *key, const uint8_t *iv, size_t ivlen,
return 1;
}

static int sm4_ctr32_encrypt_init(SM4_CTR_CTX *ctx,
const uint8_t key[SM4_BLOCK_SIZE], const uint8_t ctr[SM4_BLOCK_SIZE])
{
sm4_set_encrypt_key(&ctx->sm4_key, key);
memcpy(ctx->ctr, ctr, SM4_BLOCK_SIZE);
memset(ctx->block, 0, SM4_BLOCK_SIZE);
ctx->block_nbytes = 0;
return 1;
}

static int sm4_ctr32_encrypt_update(SM4_CTR_CTX *ctx,
const uint8_t *in, size_t inlen, uint8_t *out, size_t *outlen)
{
size_t left;
size_t nblocks;
size_t len;

if (ctx->block_nbytes >= SM4_BLOCK_SIZE) {
error_print();
return -1;
}
*outlen = 0;
if (ctx->block_nbytes) {
left = SM4_BLOCK_SIZE - ctx->block_nbytes;
if (inlen < left) {
memcpy(ctx->block + ctx->block_nbytes, in, inlen);
ctx->block_nbytes += inlen;
return 1;
}
memcpy(ctx->block + ctx->block_nbytes, in, left);
sm4_ctr32_encrypt(&ctx->sm4_key, ctx->ctr, ctx->block, SM4_BLOCK_SIZE, out);
in += left;
inlen -= left;
out += SM4_BLOCK_SIZE;
*outlen += SM4_BLOCK_SIZE;
}
if (inlen >= SM4_BLOCK_SIZE) {
nblocks = inlen / SM4_BLOCK_SIZE;
len = nblocks * SM4_BLOCK_SIZE;
sm4_ctr32_encrypt(&ctx->sm4_key, ctx->ctr, in, len, out);
in += len;
inlen -= len;
out += len;
*outlen += len;
}
if (inlen) {
memcpy(ctx->block, in, inlen);
}
ctx->block_nbytes = inlen;
return 1;
}

static int sm4_ctr32_encrypt_finish(SM4_CTR_CTX *ctx, uint8_t *out, size_t *outlen)
{
if (ctx->block_nbytes >= SM4_BLOCK_SIZE) {
error_print();
return -1;
}
sm4_ctr32_encrypt(&ctx->sm4_key, ctx->ctr, ctx->block, ctx->block_nbytes, out);
*outlen = ctx->block_nbytes;
return 1;
}

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
33 changes: 33 additions & 0 deletions tests/sm4_ctrtest.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <time.h>
#include <gmssl/sm4.h>
#include <gmssl/hex.h>
#include <gmssl/rand.h>
Expand Down Expand Up @@ -357,6 +358,37 @@ static int test_sm4_ctr_ctx_multi_updates(void)
return 1;
}

extern void sm4_ctr32_encrypt_4blocks(const SM4_KEY *key, uint8_t iv[16], const uint8_t *in, size_t num_4blocks, uint8_t *out);


static int test_sm4_ctr32_encrypt_blocks_speed(void)
{
SM4_KEY sm4_key;
uint8_t key[16];
uint8_t ctr[16];
uint8_t buf[4096];
clock_t begin, end;
double seconds;
int i;

memset(key, 0, sizeof(key));
memset(ctr, 0, sizeof(ctr));
memset(buf, 0, sizeof(buf));

sm4_set_encrypt_key(&sm4_key, key);

begin = clock();
for (i = 0; i < 4096; i++) {
sm4_ctr32_encrypt_4blocks(&sm4_key, ctr, buf, sizeof(buf)/64, buf);
}
end = clock();

seconds = (double)(end - begin)/CLOCKS_PER_SEC;
printf("%s: %f MiB per second\n", __FUNCTION__, 16/seconds);

return 1;
}

int main(void)
{
if (test_sm4_ctr() != 1) goto err;
Expand All @@ -365,6 +397,7 @@ int main(void)
if (test_sm4_ctr_ctx() != 1) goto err;
if (test_sm4_ctr_ctx_multi_updates() != 1) goto err;

if (test_sm4_ctr32_encrypt_blocks_speed() != 1) goto err;
printf("%s all tests passed\n", __FILE__);
return 0;
err:
Expand Down
8 changes: 4 additions & 4 deletions tests/sm4_ecbtest.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ static int test_sm4_ecb(void)
rand_bytes(plaintext, sizeof(plaintext));

sm4_set_encrypt_key(&sm4_key, key);
sm4_ecb_encrypt(&sm4_key, plaintext, sizeof(plaintext)/16, encrypted);
sm4_ecb_encrypt_blocks(&sm4_key, plaintext, sizeof(plaintext)/16, encrypted);

sm4_set_decrypt_key(&sm4_key, key);
sm4_ecb_encrypt(&sm4_key, encrypted, sizeof(encrypted)/16, decrypted);
sm4_ecb_encrypt_blocks(&sm4_key, encrypted, sizeof(encrypted)/16, decrypted);

if (memcmp(decrypted, plaintext, sizeof(plaintext)) != 0) {
error_print();
Expand Down Expand Up @@ -69,7 +69,7 @@ static int test_sm4_ecb_test_vectors(void)
uint8_t decrypted[sizeof(plaintext)] = {0};

sm4_set_encrypt_key(&sm4_key, key);
sm4_ecb_encrypt(&sm4_key, plaintext, sizeof(plaintext)/16, encrypted);
sm4_ecb_encrypt_blocks(&sm4_key, plaintext, sizeof(plaintext)/16, encrypted);

format_bytes(stderr, 0, 0, "", encrypted, sizeof(encrypted));

Expand All @@ -79,7 +79,7 @@ static int test_sm4_ecb_test_vectors(void)
}

sm4_set_decrypt_key(&sm4_key, key);
sm4_ecb_encrypt(&sm4_key, encrypted, sizeof(encrypted)/16, decrypted);
sm4_ecb_encrypt_blocks(&sm4_key, encrypted, sizeof(encrypted)/16, decrypted);

if (memcmp(decrypted, plaintext, sizeof(plaintext)) != 0) {
error_print();
Expand Down
Loading

0 comments on commit bc15f7a

Please sign in to comment.