mirror of
https://github.com/guanzhi/GmSSL.git
synced 2026-05-07 00:46:17 +08:00
Update ZUC
This commit is contained in:
@@ -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
|
* Licensed under the Apache License, Version 2.0 (the License); you may
|
||||||
* not use this file except in compliance with the License.
|
* not use this file except in compliance with the License.
|
||||||
@@ -79,8 +79,8 @@ ZUC_UINT32 zuc_eia_generate_mac(const ZUC_UINT32 *data, size_t nbits,
|
|||||||
typedef ZUC_STATE ZUC256_STATE;
|
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]);
|
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)
|
void zuc256_generate_keystream(ZUC_STATE *state, size_t nwords, ZUC_UINT32 *words);
|
||||||
#define zuc256_generate_keyword(state) zuc_generate_keyword(state)
|
ZUC_UINT32 zuc256_generate_keyword(ZUC_STATE *state);
|
||||||
|
|
||||||
|
|
||||||
typedef struct ZUC256_MAC_CTX_st {
|
typedef struct ZUC256_MAC_CTX_st {
|
||||||
|
|||||||
11
src/zuc.c
11
src/zuc.c
@@ -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
|
* Licensed under the Apache License, Version 2.0 (the License); you may
|
||||||
* not use this file except in compliance with the License.
|
* not use this file except in compliance with the License.
|
||||||
@@ -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 ^ F(X0, X1, X2);
|
||||||
keystream[i] = X3 ^ ((X0 ^ R1) + R2);
|
keystream[i] = X3 ^ ((X0 ^ R1) + R2);
|
||||||
|
|
||||||
|
|
||||||
W1 = R1 + X1;
|
W1 = R1 + X1;
|
||||||
W2 = R2 ^ X2;
|
W2 = R2 ^ X2;
|
||||||
U = L1((W1 << 16) | (W2 >> 16));
|
U = L1((W1 << 16) | (W2 >> 16));
|
||||||
@@ -575,6 +574,14 @@ void zuc256_init(ZUC_STATE *key, const uint8_t K[32],
|
|||||||
zuc256_set_mac_key(key, K, IV, 0);
|
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],
|
void zuc256_mac_init(ZUC256_MAC_CTX *ctx, const uint8_t key[32],
|
||||||
const uint8_t iv[23], int macbits)
|
const uint8_t iv[23], int macbits)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -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
|
* Licensed under the Apache License, Version 2.0 (the License); you may
|
||||||
* not use this file except in compliance with the License.
|
* not use this file except in compliance with the License.
|
||||||
@@ -467,7 +467,7 @@ static int test_zuc256_mac(void)
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int test_zuc_generate_keystream_speed(void)
|
static int speed_zuc_generate_keystream(void)
|
||||||
{
|
{
|
||||||
ZUC_STATE zuc_state;
|
ZUC_STATE zuc_state;
|
||||||
uint8_t key[16];
|
uint8_t key[16];
|
||||||
@@ -478,8 +478,6 @@ static int test_zuc_generate_keystream_speed(void)
|
|||||||
int i;
|
int i;
|
||||||
|
|
||||||
zuc_init(&zuc_state, key, iv);
|
zuc_init(&zuc_state, key, iv);
|
||||||
|
|
||||||
// warm up
|
|
||||||
for (i = 0; i < 4096; i++) {
|
for (i = 0; i < 4096; i++) {
|
||||||
zuc_generate_keystream(&zuc_state, 1024, buf);
|
zuc_generate_keystream(&zuc_state, 1024, buf);
|
||||||
}
|
}
|
||||||
@@ -491,12 +489,12 @@ static int test_zuc_generate_keystream_speed(void)
|
|||||||
end = clock();
|
end = clock();
|
||||||
|
|
||||||
seconds = (double)(end - begin)/CLOCKS_PER_SEC;
|
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;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int test_zuc_encrypt_speed(void)
|
static int speed_zuc_encrypt(void)
|
||||||
{
|
{
|
||||||
ZUC_STATE zuc_state;
|
ZUC_STATE zuc_state;
|
||||||
uint8_t key[16];
|
uint8_t key[16];
|
||||||
@@ -508,8 +506,6 @@ static int test_zuc_encrypt_speed(void)
|
|||||||
int i;
|
int i;
|
||||||
|
|
||||||
zuc_init(&zuc_state, key, iv);
|
zuc_init(&zuc_state, key, iv);
|
||||||
|
|
||||||
// warm up
|
|
||||||
for (i = 0; i < 4096; i++) {
|
for (i = 0; i < 4096; i++) {
|
||||||
zuc_encrypt(&zuc_state, buf, 4096, buf);
|
zuc_encrypt(&zuc_state, buf, 4096, buf);
|
||||||
}
|
}
|
||||||
@@ -521,7 +517,7 @@ static int test_zuc_encrypt_speed(void)
|
|||||||
end = clock();
|
end = clock();
|
||||||
|
|
||||||
seconds = (double)(end - begin)/CLOCKS_PER_SEC;
|
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;
|
return 1;
|
||||||
}
|
}
|
||||||
@@ -534,8 +530,8 @@ int main(void)
|
|||||||
if (test_zuc256() != 1) goto err;
|
if (test_zuc256() != 1) goto err;
|
||||||
if (test_zuc256_mac() != 1) goto err;
|
if (test_zuc256_mac() != 1) goto err;
|
||||||
#if ENABLE_TEST_SPEED
|
#if ENABLE_TEST_SPEED
|
||||||
if (test_zuc_generate_keystream_speed() != 1) goto err;
|
if (speed_zuc_generate_keystream() != 1) goto err;
|
||||||
if (test_zuc_encrypt_speed() != 1) goto err;
|
if (speed_zuc_encrypt() != 1) goto err;
|
||||||
#endif
|
#endif
|
||||||
printf("%s all tests passed\n", __FILE__);
|
printf("%s all tests passed\n", __FILE__);
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
Reference in New Issue
Block a user