Skip to content

Commit

Permalink
Update sm4 tool to support more modes
Browse files Browse the repository at this point in the history
Not finish yet
  • Loading branch information
guanzhi committed Feb 21, 2024
1 parent 326e7de commit 79a6437
Show file tree
Hide file tree
Showing 4 changed files with 525 additions and 26 deletions.
20 changes: 18 additions & 2 deletions include/gmssl/sm4.h
Original file line number Diff line number Diff line change
Expand Up @@ -212,11 +212,27 @@ int sm4_ccm_decrypt(const SM4_KEY *sm4_key, const uint8_t *iv, size_t ivlen,

#ifdef ENABLE_SM4_XTS
// call `sm4_set_encrypt_key` to set both `key1` and `key2`
int sm4_xts_encrypt(const SM4_KEY *key1, const SM4_KEY *key2, size_t tweak,
int sm4_xts_encrypt(const SM4_KEY *key1, const SM4_KEY *key2, const uint8_t tweak[16],
const uint8_t *in, size_t inlen, uint8_t *out);
// call `sm4_set_decrypt_key(key1)` and `sm4_set_encrypt_key(key2)`
int sm4_xts_decrypt(const SM4_KEY *key1, const SM4_KEY *key2, size_t tweak,
int sm4_xts_decrypt(const SM4_KEY *key1, const SM4_KEY *key2, const uint8_t tweak[16],
const uint8_t *in, size_t inlen, uint8_t *out);

typedef struct {
SM4_KEY key1;
SM4_KEY key2;
uint8_t tweak[16];
size_t data_unit_size;
uint8_t *block;
size_t block_nbytes;
} SM4_XTS_CTX;

int sm4_xts_encrypt_init(SM4_XTS_CTX *ctx, const uint8_t key[32], const uint8_t iv[16], size_t data_unit_size);
int sm4_xts_encrypt_update(SM4_XTS_CTX *ctx, const uint8_t *in, size_t inlen, uint8_t *out, size_t *outlen);
int sm4_xts_encrypt_finish(SM4_XTS_CTX *ctx, uint8_t *out, size_t *outlen);
int sm4_xts_decrypt_init(SM4_XTS_CTX *ctx, const uint8_t key[32], const uint8_t iv[16], size_t data_unit_size);
int sm4_xts_decrypt_update(SM4_XTS_CTX *ctx, const uint8_t *in, size_t inlen, uint8_t *out, size_t *outlen);
int sm4_xts_decrypt_finish(SM4_XTS_CTX *ctx, uint8_t *out, size_t *outlen);
#endif


Expand Down
203 changes: 191 additions & 12 deletions src/sm4_xts.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
#include <gmssl/error.h>


int sm4_xts_encrypt(const SM4_KEY *key1, const SM4_KEY *key2, size_t tweak,
int sm4_xts_encrypt(const SM4_KEY *key1, const SM4_KEY *key2, const uint8_t tweak[16],
const uint8_t *in, size_t inlen, uint8_t *out)
{
uint8_t T[16] = {0};
uint8_t T[16];
uint8_t block[16];
size_t nblocks, i;
gf128_t a;
Expand All @@ -28,10 +28,7 @@ int sm4_xts_encrypt(const SM4_KEY *key1, const SM4_KEY *key2, size_t tweak,
}
nblocks = inlen / 16 + 1;

for (i = 0; i < 8; i++) {
T[i] = tweak & 0xff;
tweak >>= 8;
}
memcpy(T, tweak, 16);
sm4_encrypt(key2, T, T);

for (i = 0; i < nblocks - 2; i++) {
Expand Down Expand Up @@ -76,10 +73,10 @@ int sm4_xts_encrypt(const SM4_KEY *key1, const SM4_KEY *key2, size_t tweak,
return 1;
}

int sm4_xts_decrypt(const SM4_KEY *key1, const SM4_KEY *key2, size_t tweak,
int sm4_xts_decrypt(const SM4_KEY *key1, const SM4_KEY *key2, const uint8_t tweak[16],
const uint8_t *in, size_t inlen, uint8_t *out)
{
uint8_t T[16] = {0};
uint8_t T[16];
uint8_t block[16];
size_t nblocks, i;
gf128_t a;
Expand All @@ -90,10 +87,7 @@ int sm4_xts_decrypt(const SM4_KEY *key1, const SM4_KEY *key2, size_t tweak,
}
nblocks = inlen / 16 + 1;

for (i = 0; i < 8; i++) {
T[i] = tweak & 0xff;
tweak >>= 8;
}
memcpy(T, tweak, 16);
sm4_encrypt(key2, T, T);

for (i = 0; i < nblocks - 2; i++) {
Expand Down Expand Up @@ -140,3 +134,188 @@ int sm4_xts_decrypt(const SM4_KEY *key1, const SM4_KEY *key2, size_t tweak,
return 1;
}

static void tweak_incr(uint8_t a[16])
{
int i;
for (i = 0; i < 16; i++) {
a[i]++;
if (a[i]) break;
}
}

int sm4_xts_encrypt_init(SM4_XTS_CTX *ctx, const uint8_t key[32], const uint8_t iv[16], size_t data_unit_size)
{
if (data_unit_size < SM4_BLOCK_SIZE) {
error_print();
return -1;
}
sm4_set_encrypt_key(&ctx->key1, key);
sm4_set_encrypt_key(&ctx->key2, key + 16);
memcpy(ctx->tweak, iv, 16);
ctx->data_unit_size = data_unit_size;
if (!(ctx->block = (uint8_t *)malloc(data_unit_size))) {
error_print();
return -1;
}
ctx->block_nbytes = 0;
return 1;
}

int sm4_xts_encrypt_update(SM4_XTS_CTX *ctx,
const uint8_t *in, size_t inlen, uint8_t *out, size_t *outlen)
{
size_t DATA_UNIT_SIZE = ctx->data_unit_size;

size_t left;
size_t nblocks;
size_t len;

if (ctx->block_nbytes >= DATA_UNIT_SIZE) {
error_print();
return -1;
}
*outlen = 0;
if (ctx->block_nbytes) {
left = DATA_UNIT_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);
if (sm4_xts_encrypt(&ctx->key1, &ctx->key2, ctx->tweak, ctx->block, DATA_UNIT_SIZE, out) != 1) {
error_print();
return -1;
}
tweak_incr(ctx->tweak);
in += left;
inlen -= left;
out += DATA_UNIT_SIZE;
*outlen += DATA_UNIT_SIZE;
}
while (inlen >= DATA_UNIT_SIZE) {
if (sm4_xts_encrypt(&ctx->key1, &ctx->key2, ctx->tweak, in, DATA_UNIT_SIZE, out) != 1) {
error_print();
return -1;
}
tweak_incr(ctx->tweak);
in += DATA_UNIT_SIZE;
inlen -= DATA_UNIT_SIZE;
out += DATA_UNIT_SIZE;
*outlen += DATA_UNIT_SIZE;
}
if (inlen) {
memcpy(ctx->block, in, inlen);
}
ctx->block_nbytes = inlen;
return 1;
}

int sm4_xts_encrypt_finish(SM4_XTS_CTX *ctx, uint8_t *out, size_t *outlen)
{
size_t DATA_UNIT_SIZE = ctx->data_unit_size;
if (ctx->block_nbytes >= DATA_UNIT_SIZE) {
error_print();
return -1;
}
if (ctx->block) {
free(ctx->block);
ctx->block = NULL;
}
if (ctx->block_nbytes) {
error_puts("invalid total input length");
return -1;
}
*outlen = 0;
return 1;
}


int sm4_xts_decrypt_init(SM4_XTS_CTX *ctx, const uint8_t key[32], const uint8_t iv[16], size_t data_unit_size)
{
if (data_unit_size < SM4_BLOCK_SIZE) {
error_print();
return -1;
}
sm4_set_decrypt_key(&ctx->key1, key);
sm4_set_encrypt_key(&ctx->key2, key + 16);
memcpy(ctx->tweak, iv, 16);
ctx->data_unit_size = data_unit_size;
if (!(ctx->block = (uint8_t *)malloc(data_unit_size))) {
error_print();
return -1;
}
ctx->block_nbytes = 0;
return 1;
}

int sm4_xts_decrypt_update(SM4_XTS_CTX *ctx,
const uint8_t *in, size_t inlen, uint8_t *out, size_t *outlen)
{
size_t DATA_UNIT_SIZE = ctx->data_unit_size;

size_t left;
size_t nblocks;
size_t len;

if (ctx->block_nbytes >= DATA_UNIT_SIZE) {
error_print();
return -1;
}
*outlen = 0;
if (ctx->block_nbytes) {
left = DATA_UNIT_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);
if (sm4_xts_decrypt(&ctx->key1, &ctx->key2, ctx->tweak, ctx->block, DATA_UNIT_SIZE, out) != 1) {
error_print();
return -1;
}
tweak_incr(ctx->tweak);
in += left;
inlen -= left;
out += DATA_UNIT_SIZE;
*outlen += DATA_UNIT_SIZE;
}
while (inlen >= DATA_UNIT_SIZE) {
if (sm4_xts_decrypt(&ctx->key1, &ctx->key2, ctx->tweak, in, DATA_UNIT_SIZE, out) != 1) {
error_print();
return -1;
}
tweak_incr(ctx->tweak);
in += DATA_UNIT_SIZE;
inlen -= DATA_UNIT_SIZE;
out += DATA_UNIT_SIZE;
*outlen += DATA_UNIT_SIZE;
}
if (inlen) {
memcpy(ctx->block, in, inlen);
}
ctx->block_nbytes = inlen;
return 1;
}

int sm4_xts_decrypt_finish(SM4_XTS_CTX *ctx, uint8_t *out, size_t *outlen)
{
size_t DATA_UNIT_SIZE = ctx->data_unit_size;
if (ctx->block_nbytes >= DATA_UNIT_SIZE) {
error_print();
return -1;
}
if (ctx->block) {
free(ctx->block);
ctx->block = NULL;
}
if (ctx->block_nbytes) {
error_puts("invalid total input length");
return -1;
}
*outlen = 0;
return 1;
}


3 changes: 2 additions & 1 deletion tests/sm4_xtstest.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ static int test_sm4_xts(void)
uint8_t plaintext[16 * 4];
uint8_t encrypted[sizeof(plaintext)];
uint8_t decrypted[sizeof(plaintext)];
size_t tweak = 0x12345678;
uint8_t tweak[16];
size_t i;

rand_bytes(key, sizeof(key));
rand_bytes(tweak, sizeof(tweak));
rand_bytes(plaintext, sizeof(plaintext));

for (i = 0; i < sizeof(len)/sizeof(len[0]); i++) {
Expand Down
Loading

0 comments on commit 79a6437

Please sign in to comment.