update zuc

with zuc_spec test passed
This commit is contained in:
Zhi Guan
2017-12-26 22:57:54 +08:00
parent d8e57c5432
commit b920f5af10
4 changed files with 42 additions and 18 deletions

View File

@@ -53,18 +53,13 @@
#include <stdlib.h>
#include "zuc_spec.h"
typedef struct {
uint32_t lfsr_s[16];
uint32_t f_r[2];
uint32_t brc_x[4];
uint32_t S[16];
uint32_t R1;
uint32_t R2;
} zuc_key_t;
static unsigned char S0[256] = {
static const unsigned char S0[256] = {
0x3e,0x72,0x5b,0x47,0xca,0xe0,0x00,0x33,0x04,0xd1,0x54,0x98,0x09,0xb9,0x6d,0xcb,
0x7b,0x1b,0xf9,0x32,0xaf,0x9d,0x6a,0xa5,0xb8,0x2d,0xfc,0x1d,0x08,0x53,0x03,0x90,
0x4d,0x4e,0x84,0x99,0xe4,0xce,0xd9,0x91,0xdd,0xb6,0x85,0x48,0x8b,0x29,0x6e,0xac,
@@ -83,7 +78,7 @@ static unsigned char S0[256] = {
0x8d,0x27,0x1a,0xdb,0x81,0xb3,0xa0,0xf4,0x45,0x7a,0x19,0xdf,0xee,0x78,0x34,0x60,
};
static unsigned char S1[256] = {
static const unsigned char S1[256] = {
0x55,0xc2,0x63,0x71,0x3b,0xc8,0x47,0x86,0x9f,0x3c,0xda,0x5b,0x29,0xaa,0xfd,0x77,
0x8c,0xc5,0x94,0x0c,0xa6,0x1a,0x13,0x00,0xe3,0xa8,0x16,0x72,0x40,0xf9,0xf8,0x42,
0x44,0x26,0x68,0x96,0x81,0xd9,0x45,0x3e,0x10,0x76,0xc6,0xa7,0x8b,0x39,0x43,0xe1,
@@ -102,21 +97,20 @@ static unsigned char S1[256] = {
0x64,0xbe,0x85,0x9b,0x2f,0x59,0x8a,0xd7,0xb0,0x25,0xac,0xaf,0x12,0x03,0xe2,0xf2,
};
uint32_t EK_d[16] = {
static uint32_t const EK_d[16] = {
0x44D7,0x26BC,0x626B,0x135E,0x5789,0x35E2,0x7135,0x09AF,
0x4D78,0x2F13,0x6BC4,0x1AF1,0x5E26,0x3C4D,0x789A,0x47AC,
};
inline uint32_t zuc_madd(uint32_t a, uint32_t b)
{
u32 c = a + b;
uint32_t c = a + b;
return (c & 0x7FFFFFFF) + (c >> 31);
}
/* LFSR with initialization mode */
#define MulByPow2(x, k) ((((x) << k) | ((x) >> (31 - k))) & 0x7FFFFFFF)
#define MulByPow2(x, k) ((((x) << (k)) | ((x) >> (31 - (k)))) & 0x7FFFFFFF)
void zuc_lfsr_init(zuc_key_t *key, uint32_t u)
{
@@ -195,6 +189,12 @@ void zuc_bit_reorganization(zuc_key_t *key)
key->brc_x[3] = ((key->lfsr_s[2] & 0xFFFF) << 16) | (key->lfsr_s[0] >> 15);
}
#define ZUC_BIT_REORG(x,x0,x1,x2,x3) \
x0 = ((s[15] & 0x7FFF8000) << 1) | (s[14] & 0xFFFF); \
x1 = ((s[11] & 0xFFFF) << 16) | (s[9] >> 15); \
x2 = ((s[7] & 0xFFFF) << 16) | (s[5] >> 15); \
x3 = ((s[2] & 0xFFFF) << 16) | (s[0] >> 15)
#define ROT32(a, k) (((a) << k) | ((a) >> (32 - k)))
#define L1(x) \
@@ -299,5 +299,3 @@ void ZUC(const unsigned char *key, const unsigned char *iv, uint32_t *keystream,
zuc_key_init(&zuc, key, iv);
zuc_generate_keystream(&zuc, keystream, num);
}

View File

@@ -50,6 +50,7 @@
/* code from ZUC 3GPP Specifications, version 1.6
*/
#include <stdio.h>
#include <stdlib.h>
#include "zuc_spec.h"
@@ -345,7 +346,6 @@ void EEA3(u8* CK, u32 COUNT, u32 BEARER, u32 DIRECTION, u32 LENGTH, u32* M, u32*
free(z);
}
u32 GET_WORD(u32 * DATA, u32 i)
{
u32 WORD, ti;
@@ -405,3 +405,15 @@ void EIA3(u8* IK, u32 COUNT, u32 DIRECTION, u32 BEARER, u32 LENGTH, u32* M, u32*
free(z);
}
int main(int argc, char **argv)
{
unsigned char key[16] = {0};
unsigned char iv[16] = {0};
u32 z[3];
Initialization(key, iv);
GenerateKeystream(z, 3);
printf("%08x, %08x, %08x\n", z[0], z[1], z[2]);
return 0;
}

View File

@@ -60,6 +60,8 @@ typedef unsigned int u32;
extern "C" {
#endif
void Initialization(u8* k, u8* iv);
void GenerateKeystream(u32* pKeystream, int KeystreamLen);
void ZUC(u8* k, u8* iv, u32* ks, int len);
void EEA3(u8* CK, u32 COUNT, u32 BEARER, u32 DIRECTION, u32 LENGTH, u32* M, u32* C);
void EIA3(u8* IK, u32 COUNT, u32 DIRECTION, u32 BEARER, u32 LENGTH, u32* M, u32* MAC);
@@ -68,4 +70,3 @@ void EIA3(u8* IK, u32 COUNT, u32 DIRECTION, u32 BEARER, u32 LENGTH, u32* M, u32*
}
#endif
#endif

View File

@@ -108,6 +108,19 @@ int main(int argc, char **argv)
{
int err = 0;
unsigned char key[][] = {
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
{0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff},
{0x3d,0x4c,0x4b,0xe9,0x6a,0x82,0xfd,0xae,0xb5,0x8f,0x64,0x1d,0xb1,0x7b,0x45,0x5b},
};
unsigned char iv[][] = {
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
{0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff},
{0x84,0x31,0x9a,0xa8,0xde,0x69,0x15,0xca,0x1f,0x6b,0xda,0x6b,0xfb,0xd8,0xc7,0x66},
};
uint32_t z0[] = {0x286dafe5,0x668b56df,0x3ead461d};
uint32_t z1[] = {0x27bedc74,0x0657cfa0,0x14f1c272};
uint32_t z2[] = {0x018082da,0x7096398b,0x3279c419};
return err;
}