Skip to content

Commit

Permalink
Update ZUC
Browse files Browse the repository at this point in the history
  • Loading branch information
guanzhi committed Apr 29, 2024
1 parent 5cf6f26 commit b351803
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 16 deletions.
6 changes: 3 additions & 3 deletions include/gmssl/zuc.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2023 The GmSSL Project. All Rights Reserved.
* Copyright 2014-2024 The GmSSL Project. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the License); you may
* not use this file except in compliance with the License.
Expand Down Expand Up @@ -79,8 +79,8 @@ ZUC_UINT32 zuc_eia_generate_mac(const ZUC_UINT32 *data, size_t nbits,
typedef ZUC_STATE ZUC256_STATE;

void zuc256_init(ZUC256_STATE *state, const uint8_t key[ZUC256_KEY_SIZE], const uint8_t iv[ZUC256_IV_SIZE]);
#define zuc256_generate_keystream(state,nwords,words) zuc_generate_keystream(state,nwords,words)
#define zuc256_generate_keyword(state) zuc_generate_keyword(state)
void zuc256_generate_keystream(ZUC_STATE *state, size_t nwords, ZUC_UINT32 *words);
ZUC_UINT32 zuc256_generate_keyword(ZUC_STATE *state);


typedef struct ZUC256_MAC_CTX_st {
Expand Down
11 changes: 9 additions & 2 deletions src/zuc.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
* Copyright 2014-2024 The GmSSL Project. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the License); you may
* not use this file except in compliance with the License.
Expand Down Expand Up @@ -224,7 +224,6 @@ void zuc_generate_keystream(ZUC_STATE *state, size_t nwords, uint32_t *keystream
//keystream[i] = X3 ^ F(X0, X1, X2);
keystream[i] = X3 ^ ((X0 ^ R1) + R2);


W1 = R1 + X1;
W2 = R2 ^ X2;
U = L1((W1 << 16) | (W2 >> 16));
Expand Down Expand Up @@ -575,6 +574,14 @@ void zuc256_init(ZUC_STATE *key, const uint8_t K[32],
zuc256_set_mac_key(key, K, IV, 0);
}

uint32_t zuc256_generate_keyword(ZUC_STATE *state) {
return zuc_generate_keyword(state);
}

void zuc256_generate_keystream(ZUC_STATE *state, size_t nwords, uint32_t *keystream) {
zuc_generate_keystream(state, nwords, keystream);
}

void zuc256_mac_init(ZUC256_MAC_CTX *ctx, const uint8_t key[32],
const uint8_t iv[23], int macbits)
{
Expand Down
18 changes: 7 additions & 11 deletions tests/zuctest.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
* Copyright 2014-2024 The GmSSL Project. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the License); you may
* not use this file except in compliance with the License.
Expand Down Expand Up @@ -467,7 +467,7 @@ static int test_zuc256_mac(void)
return 1;
}

static int test_zuc_generate_keystream_speed(void)
static int speed_zuc_generate_keystream(void)
{
ZUC_STATE zuc_state;
uint8_t key[16];
Expand All @@ -478,8 +478,6 @@ static int test_zuc_generate_keystream_speed(void)
int i;

zuc_init(&zuc_state, key, iv);

// warm up
for (i = 0; i < 4096; i++) {
zuc_generate_keystream(&zuc_state, 1024, buf);
}
Expand All @@ -491,12 +489,12 @@ static int test_zuc_generate_keystream_speed(void)
end = clock();

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

return 1;
}

static int test_zuc_encrypt_speed(void)
static int speed_zuc_encrypt(void)
{
ZUC_STATE zuc_state;
uint8_t key[16];
Expand All @@ -508,8 +506,6 @@ static int test_zuc_encrypt_speed(void)
int i;

zuc_init(&zuc_state, key, iv);

// warm up
for (i = 0; i < 4096; i++) {
zuc_encrypt(&zuc_state, buf, 4096, buf);
}
Expand All @@ -521,7 +517,7 @@ static int test_zuc_encrypt_speed(void)
end = clock();

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

return 1;
}
Expand All @@ -534,8 +530,8 @@ int main(void)
if (test_zuc256() != 1) goto err;
if (test_zuc256_mac() != 1) goto err;
#if ENABLE_TEST_SPEED
if (test_zuc_generate_keystream_speed() != 1) goto err;
if (test_zuc_encrypt_speed() != 1) goto err;
if (speed_zuc_generate_keystream() != 1) goto err;
if (speed_zuc_encrypt() != 1) goto err;
#endif
printf("%s all tests passed\n", __FILE__);
return 0;
Expand Down

0 comments on commit b351803

Please sign in to comment.