mirror of
https://github.com/guanzhi/GmSSL.git
synced 2026-08-09 07:23:56 +08:00
Add 128-EEA3, 128-EIA3, ZUC-MAC, ZUC256, ZUC256-MAC and EVP_zuc256
This commit is contained in:
@@ -1,2 +1,4 @@
|
||||
LIBS=../../libcrypto
|
||||
SOURCE[../../libcrypto]=zuc_core.c
|
||||
SOURCE[../../libcrypto]=zuc_core.c zuc_eea.c zuc_eia.c
|
||||
INCLUDE[zuc_core.o]=../modes
|
||||
INCLUDE[zuc_eia.o]=../modes
|
||||
|
||||
@@ -1,47 +0,0 @@
|
||||
|
||||
ZUC_UINT7 D[16] = {
|
||||
0x22,
|
||||
0x2F,
|
||||
0x24,
|
||||
0x2A,
|
||||
0x6D,
|
||||
0x40,
|
||||
0x40,
|
||||
0x40,
|
||||
0x40,
|
||||
0x40,
|
||||
0x40,
|
||||
0x40,
|
||||
0x52,
|
||||
0x10,
|
||||
0x30
|
||||
};
|
||||
|
||||
void ZUC_set_key(ZUC_KEY *key, const unsigned char *user_key, const unsigned char *iv)
|
||||
{
|
||||
LFSR[0] = MAKEU31(K[0], D[0], K[21], K[16]);
|
||||
LFSR[1] = MAKEU31(K[1], D[1], K[22], K[17]);
|
||||
LFSR[2] = MAKEU31(K[2], D[2], K[23], K[18]);
|
||||
LFSR[3] = MAKEU31(K[3], D[3], K[24], K[19]);
|
||||
LFSR[4] = MAKEU31(K[4], D[4], K[25], K[20]);
|
||||
LFSR[5] = MAKEU31(IV[0], (D[5] | IV[17]), K[5], K[26]);
|
||||
LFSR[6] = MAKEU31(IV[1], (D[6] | IV[18]), K[6], K[27]);
|
||||
LFSR[7] = MAKEU31(IV[10], (D[7] | IV[19]), K[7], IV[2]);
|
||||
LFSR[8] = MAKEU31(K[8], (D[8] | IV[20]), IV[13], IV[11]);
|
||||
LFSR[9] = MAKEU31(K[9], (D[9] | IV[21]), IV[12], IV[4]);
|
||||
LFSR[10] = MAKEU31(IV[5], (D[10] | IV[22]), K[10], K[28]);
|
||||
LFSR[11] = MAKEU31(K[11], (D[11] | IV[23]), IV[6], IV[13]);
|
||||
LFSR[12] = MAKEU31(K[12], (D[12] | IV[24]), IV[7], IV[14]);
|
||||
LFSR[13] = MAKEU31(K[13], D[13], IV[15], IV[8]);
|
||||
LFSR[14] = MAKEU31(K[14], (D[14] | (K[31] >> 4)), IV[16], IV[9]);
|
||||
LFSR[15] = MAKEU31(K[15], (D[15] | (K[31] & 0xF0)), K[30], K[29]);
|
||||
|
||||
R1 = R2 = 0;
|
||||
|
||||
for (i = 0; i < 32; i++) {
|
||||
BitReconstruction3(X0, X1, X2);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/* ====================================================================
|
||||
* Copyright (c) 2015 - 2018 The GmSSL Project. All rights reserved.
|
||||
* Copyright (c) 2015 - 2019 The GmSSL Project. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
@@ -48,7 +48,9 @@
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <openssl/zuc.h>
|
||||
#include "modes_lcl.h"
|
||||
|
||||
static const ZUC_UINT15 KD[16] = {
|
||||
0x44D7,0x26BC,0x626B,0x135E,0x5789,0x35E2,0x7135,0x09AF,
|
||||
@@ -248,12 +250,158 @@ void ZUC_generate_keystream(ZUC_KEY *key, size_t nwords, uint32_t *keystream)
|
||||
key->R2 = R2;
|
||||
}
|
||||
|
||||
#if 0
|
||||
void ZUC_MAC_init(ZUC_MAC_CTX *ctx, const unsigned char key[16], const unsigned char iv[16])
|
||||
{
|
||||
memset(ctx, 0, sizeof(*ctx));
|
||||
ZUC_set_key((ZUC_KEY *)ctx, key, iv);
|
||||
ctx->K0 = ZUC_generate_keyword((ZUC_KEY *)ctx);
|
||||
}
|
||||
|
||||
void ZUC_MAC_update(ZUC_MAC_CTX *ctx, const unsigned char *data, size_t len)
|
||||
{
|
||||
ZUC_UINT32 T = ctx->T;
|
||||
ZUC_UINT32 K0 = ctx->K0;
|
||||
ZUC_UINT32 K1, M;
|
||||
ZUC_UINT31 *LFSR = ctx->LFSR;
|
||||
ZUC_UINT32 R1 = ctx->R1;
|
||||
ZUC_UINT32 R2 = ctx->R2;
|
||||
ZUC_UINT32 X0, X1, X2, X3;
|
||||
ZUC_UINT32 W1, W2, U, V;
|
||||
size_t i;
|
||||
|
||||
if (!data || !len) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (ctx->buflen) {
|
||||
size_t num = sizeof(ctx->buf) - ctx->buflen;
|
||||
if (len < num) {
|
||||
memcpy(ctx->buf + ctx->buflen, data, len);
|
||||
ctx->buflen += len;
|
||||
return;
|
||||
}
|
||||
|
||||
memcpy(ctx->buf + ctx->buflen, data, num);
|
||||
M = GETU32(ctx->buf);
|
||||
ctx->buflen = 0;
|
||||
|
||||
BitReconstruction4(X0, X1, X2, X3);
|
||||
K1 = X3 ^ F(X0, X1, X2);
|
||||
LFSRWithWorkMode();
|
||||
|
||||
for (i = 0; i < 32; i++) {
|
||||
if (M & 0x80000000) {
|
||||
T ^= K0;
|
||||
}
|
||||
M <<= 1;
|
||||
K0 = (K0 << 1) | (K1 >> 31);
|
||||
K1 <<= 1;
|
||||
}
|
||||
|
||||
data += num;
|
||||
len -= num;
|
||||
}
|
||||
|
||||
while (len >= 4) {
|
||||
M = GETU32(data);
|
||||
|
||||
BitReconstruction4(X0, X1, X2, X3);
|
||||
K1 = X3 ^ F(X0, X1, X2);
|
||||
LFSRWithWorkMode();
|
||||
|
||||
for (i = 0; i < 32; i++) {
|
||||
if (M & 0x80000000) {
|
||||
T ^= K0;
|
||||
}
|
||||
M <<= 1;
|
||||
K0 = (K0 << 1) | (K1 >> 31);
|
||||
K1 <<= 1;
|
||||
}
|
||||
|
||||
data += 4;
|
||||
len -= 4;
|
||||
}
|
||||
|
||||
if (len) {
|
||||
memcpy(ctx->buf, data, len);
|
||||
ctx->buflen = len;
|
||||
}
|
||||
ctx->R1 = R1;
|
||||
ctx->R2 = R2;
|
||||
ctx->K0 = K0;
|
||||
ctx->T = T;
|
||||
}
|
||||
|
||||
void ZUC_MAC_final(ZUC_MAC_CTX *ctx, const unsigned char *data, size_t nbits, unsigned char mac[4])
|
||||
{
|
||||
ZUC_UINT32 T = ctx->T;
|
||||
ZUC_UINT32 K0 = ctx->K0;
|
||||
ZUC_UINT32 K1, M;
|
||||
ZUC_UINT31 *LFSR = ctx->LFSR;
|
||||
ZUC_UINT32 R1 = ctx->R1;
|
||||
ZUC_UINT32 R2 = ctx->R2;
|
||||
ZUC_UINT32 X0, X1, X2, X3;
|
||||
ZUC_UINT32 W1, W2, U, V;
|
||||
size_t i;
|
||||
|
||||
|
||||
if (!data)
|
||||
nbits = 0;
|
||||
|
||||
if (nbits >= 8) {
|
||||
ZUC_MAC_update(ctx, data, nbits/8);
|
||||
data += nbits/8;
|
||||
nbits %= 8;
|
||||
}
|
||||
|
||||
T = ctx->T;
|
||||
K0 = ctx->K0;
|
||||
LFSR = ctx->LFSR;
|
||||
R1 = ctx->R1;
|
||||
R2 = ctx->R2;
|
||||
|
||||
|
||||
if (nbits)
|
||||
ctx->buf[ctx->buflen] = *data;
|
||||
|
||||
if (ctx->buflen || nbits) {
|
||||
M = GETU32(ctx->buf);
|
||||
BitReconstruction4(X0, X1, X2, X3);
|
||||
K1 = X3 ^ F(X0, X1, X2);
|
||||
LFSRWithWorkMode();
|
||||
|
||||
for (i = 0; i < ctx->buflen * 8 + nbits; i++) {
|
||||
if (M & 0x80000000) {
|
||||
T ^= K0;
|
||||
}
|
||||
M <<= 1;
|
||||
K0 = (K0 << 1) | (K1 >> 31);
|
||||
K1 <<= 1;
|
||||
}
|
||||
}
|
||||
|
||||
T ^= K0;
|
||||
|
||||
BitReconstruction4(X0, X1, X2, X3);
|
||||
K1 = X3 ^ F(X0, X1, X2);
|
||||
LFSRWithWorkMode();
|
||||
T ^= K1;
|
||||
|
||||
ctx->T = T;
|
||||
PUTU32(mac, T);
|
||||
}
|
||||
|
||||
typedef unsigned char ZUC_UINT7;
|
||||
|
||||
static const ZUC_UINT7 D[16] = {
|
||||
0x22,0x2F,0x24,0x2A,0x6D,0x40,0x40,0x40,
|
||||
0x40,0x40,0x40,0x40,0x40,0x52,0x10,0x30
|
||||
static const ZUC_UINT7 ZUC256_D[][16] = {
|
||||
{0x22,0x2F,0x24,0x2A,0x6D,0x40,0x40,0x40,
|
||||
0x40,0x40,0x40,0x40,0x40,0x52,0x10,0x30},
|
||||
{0x22,0x2F,0x25,0x2A,0x6D,0x40,0x40,0x40,
|
||||
0x40,0x40,0x40,0x40,0x40,0x52,0x10,0x30},
|
||||
{0x23,0x2F,0x24,0x2A,0x6D,0x40,0x40,0x40,
|
||||
0x40,0x40,0x40,0x40,0x40,0x52,0x10,0x30},
|
||||
{0x23,0x2F,0x25,0x2A,0x6D,0x40,0x40,0x40,
|
||||
0x40,0x40,0x40,0x40,0x40,0x52,0x10,0x30},
|
||||
};
|
||||
|
||||
#define ZUC256_MAKEU31(a,b,c,d) \
|
||||
@@ -262,27 +410,40 @@ static const ZUC_UINT7 D[16] = {
|
||||
((uint32_t)(c) << 8) | \
|
||||
(uint32_t)(d))
|
||||
|
||||
void ZUC256_set_key(ZUC_KEY *key, const unsigned char *K, const unsigned char *IV)
|
||||
|
||||
static void zuc256_set_mac_key(ZUC_KEY *key, const unsigned char K[32],
|
||||
const unsigned char IV[23], int macbits)
|
||||
{
|
||||
ZUC_UINT31 *LFSR = key->LFSR;
|
||||
uint32_t R1, R2;
|
||||
uint32_t X0, X1, X2;
|
||||
uint32_t W, W1, W2, U, V;
|
||||
const ZUC_UINT7 *D;
|
||||
int i;
|
||||
|
||||
ZUC_UINT6 IV17 = IV[17] >> 2;
|
||||
ZUC_UINT6 IV18 = ((IV[17] & 0x3) << 4) | (IV[18] >> 4);
|
||||
ZUC_UINT6 IV19 = ((IV[18] & 0xf) << 2) | (IV[19] >> 6);
|
||||
ZUC_UINT6 IV20 = IV[19] & 0x3f;
|
||||
ZUC_UINT6 IV21 = IV[20] >> 2;
|
||||
ZUC_UINT6 IV22 = ((IV[20] & 0x3) << 4) | (IV[21] >> 4);
|
||||
ZUC_UINT6 IV23 = ((IV[21] & 0xf) << 2) | (IV[22] >> 6);
|
||||
ZUC_UINT6 IV24 = IV[22] & 0x3f;
|
||||
|
||||
D = macbits/32 < 3 ? ZUC256_D[macbits/32] : ZUC256_D[3];
|
||||
LFSR[0] = ZUC256_MAKEU31(K[0], D[0], K[21], K[16]);
|
||||
LFSR[1] = ZUC256_MAKEU31(K[1], D[1], K[22], K[17]);
|
||||
LFSR[2] = ZUC256_MAKEU31(K[2], D[2], K[23], K[18]);
|
||||
LFSR[3] = ZUC256_MAKEU31(K[3], D[3], K[24], K[19]);
|
||||
LFSR[4] = ZUC256_MAKEU31(K[4], D[4], K[25], K[20]);
|
||||
LFSR[5] = ZUC256_MAKEU31(IV[0], (D[5] | IV[17]), K[5], K[26]);
|
||||
LFSR[6] = ZUC256_MAKEU31(IV[1], (D[6] | IV[18]), K[6], K[27]);
|
||||
LFSR[7] = ZUC256_MAKEU31(IV[10], (D[7] | IV[19]), K[7], IV[2]);
|
||||
LFSR[8] = ZUC256_MAKEU31(K[8], (D[8] | IV[20]), IV[13], IV[11]);
|
||||
LFSR[9] = ZUC256_MAKEU31(K[9], (D[9] | IV[21]), IV[12], IV[4]);
|
||||
LFSR[10] = ZUC256_MAKEU31(IV[5], (D[10] | IV[22]), K[10], K[28]);
|
||||
LFSR[11] = ZUC256_MAKEU31(K[11], (D[11] | IV[23]), IV[6], IV[13]);
|
||||
LFSR[12] = ZUC256_MAKEU31(K[12], (D[12] | IV[24]), IV[7], IV[14]);
|
||||
LFSR[5] = ZUC256_MAKEU31(IV[0], (D[5] | IV17), K[5], K[26]);
|
||||
LFSR[6] = ZUC256_MAKEU31(IV[1], (D[6] | IV18), K[6], K[27]);
|
||||
LFSR[7] = ZUC256_MAKEU31(IV[10], (D[7] | IV19), K[7], IV[2]);
|
||||
LFSR[8] = ZUC256_MAKEU31(K[8], (D[8] | IV20), IV[13], IV[11]);
|
||||
LFSR[9] = ZUC256_MAKEU31(K[9], (D[9] | IV21), IV[12], IV[4]);
|
||||
LFSR[10] = ZUC256_MAKEU31(IV[5], (D[10] | IV22), K[10], K[28]);
|
||||
LFSR[11] = ZUC256_MAKEU31(K[11], (D[11] | IV23), IV[6], IV[13]);
|
||||
LFSR[12] = ZUC256_MAKEU31(K[12], (D[12] | IV24), IV[7], IV[14]);
|
||||
LFSR[13] = ZUC256_MAKEU31(K[13], D[13], IV[15], IV[8]);
|
||||
LFSR[14] = ZUC256_MAKEU31(K[14], (D[14] | (K[31] >> 4)), IV[16], IV[9]);
|
||||
LFSR[15] = ZUC256_MAKEU31(K[15], (D[15] | (K[31] & 0x0F)), K[30], K[29]);
|
||||
@@ -304,78 +465,138 @@ void ZUC256_set_key(ZUC_KEY *key, const unsigned char *K, const unsigned char *I
|
||||
key->R2 = R2;
|
||||
}
|
||||
|
||||
|
||||
static const ZUC_UINT7 ZUC256_MAC32_D[] = {
|
||||
0x22,0x2F,0x25,0x2A,0x6D,0x40,0x40,0x40,
|
||||
0x40,0x40,0x40,0x40,0x40,0x52,0x10,0x30
|
||||
};
|
||||
|
||||
static const ZUC_UINT7 ZUC256_MAC64_D[] = {
|
||||
0x23,0x2F,0x24,0x2A,0x6D,0x40,0x40,0x40,
|
||||
0x40,0x40,0x40,0x40,0x40,0x52,0x10,0x30,
|
||||
};
|
||||
|
||||
static const ZUC_UINT7 ZUC256_MAC128_D[] = {
|
||||
0x23,0x2F,0x25,0x2A,0x6D,0x40,0x40,0x40,
|
||||
0x40,0x40,0x40,0x40,0x40,0x52,0x10,0x30,
|
||||
};
|
||||
|
||||
int ZUC256_set_mac_key(ZUC_KEY *key, const unsigned char *key,
|
||||
const unsigned char *IV, int macbits)
|
||||
void ZUC256_set_key(ZUC_KEY *key, const unsigned char K[32],
|
||||
const unsigned char IV[23])
|
||||
{
|
||||
const ZUC_UINT7 *K;
|
||||
ZUC_UINT31 *LFSR = key->LFSR;
|
||||
uint32_t R1, R2;
|
||||
uint32_t X0, X1, X2;
|
||||
uint32_t W, W1, W2, U, V;
|
||||
int i;
|
||||
|
||||
switch (macbits) {
|
||||
case 32:
|
||||
D = ZUC256_MAC32_D;
|
||||
break;
|
||||
case 64:
|
||||
D = ZUC256_MAC64_D;
|
||||
break;
|
||||
case 128:
|
||||
D = ZUC256_MAC128_D;
|
||||
break;
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
|
||||
LFSR[0] = MAKEU31(K[0], D[0], K[21], K[16]);
|
||||
LFSR[1] = MAKEU31(K[1], D[1], K[22], K[17]);
|
||||
LFSR[2] = MAKEU31(K[2], D[2], K[23], K[18]);
|
||||
LFSR[3] = MAKEU31(K[3], D[3], K[24], K[19]);
|
||||
LFSR[4] = MAKEU31(K[4], D[4], K[25], K[20]);
|
||||
LFSR[5] = MAKEU31(IV[0], (D[5] | IV[17]), K[5], K[26]);
|
||||
LFSR[6] = MAKEU31(IV[1], (D[6] | IV[18]), K[6], K[27]);
|
||||
LFSR[7] = MAKEU31(IV[10], (D[7] | IV[19]), K[7], IV[2]);
|
||||
LFSR[8] = MAKEU31(K[8], (D[8] | IV[20]), IV[13], IV[11]);
|
||||
LFSR[9] = MAKEU31(K[9], (D[9] | IV[21]), IV[12], IV[4]);
|
||||
LFSR[10] = MAKEU31(IV[5], (D[10] | IV[22]), K[10], K[28]);
|
||||
LFSR[11] = MAKEU31(K[11], (D[11] | IV[23]), IV[6], IV[13]);
|
||||
LFSR[12] = MAKEU31(K[12], (D[12] | IV[24]), IV[7], IV[14]);
|
||||
LFSR[13] = MAKEU31(K[13], D[13], IV[15], IV[8]);
|
||||
LFSR[14] = MAKEU31(K[14], (D[14] | (K[31] >> 4)), IV[16], IV[9]);
|
||||
LFSR[15] = MAKEU31(K[15], (D[15] | (K[31] & 0xF0)), K[30], K[29]);
|
||||
|
||||
R1 = 0;
|
||||
R2 = 0;
|
||||
|
||||
for (i = 0; i < 32; i++) {
|
||||
BitReconstruction3(X0, X1, X2);
|
||||
W = F(X0, X1, X2);
|
||||
LFSRWithInitialisationMode(W >> 1);
|
||||
}
|
||||
|
||||
BitReconstruction2(X1, X2);
|
||||
F_(X1, X2);
|
||||
LFSRWithWorkMode();
|
||||
|
||||
key->R1 = R1;
|
||||
key->R2 = R2;
|
||||
|
||||
zuc256_set_mac_key(key, K, IV, 0);
|
||||
}
|
||||
|
||||
void ZUC256_MAC_init(ZUC256_MAC_CTX *ctx, const unsigned char key[32],
|
||||
const unsigned char iv[23], int macbits)
|
||||
{
|
||||
if (macbits < 32)
|
||||
macbits = 32;
|
||||
else if (macbits > 64)
|
||||
macbits = 128;
|
||||
memset(ctx, 0, sizeof(*ctx));
|
||||
zuc256_set_mac_key((ZUC256_KEY *)ctx, key, iv, macbits);
|
||||
ZUC256_generate_keystream((ZUC256_KEY *)ctx, macbits/32, ctx->T);
|
||||
ZUC256_generate_keystream((ZUC256_KEY *)ctx, macbits/32, ctx->K0);
|
||||
ctx->macbits = (macbits/32) * 32;
|
||||
}
|
||||
|
||||
void ZUC256_MAC_update(ZUC256_MAC_CTX *ctx, const unsigned char *data, size_t len)
|
||||
{
|
||||
ZUC_UINT32 K1, M;
|
||||
size_t n = ctx->macbits / 32;
|
||||
size_t i, j;
|
||||
|
||||
if (!data || !len) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (ctx->buflen) {
|
||||
size_t num = sizeof(ctx->buf) - ctx->buflen;
|
||||
if (len < num) {
|
||||
memcpy(ctx->buf + ctx->buflen, data, len);
|
||||
ctx->buflen += len;
|
||||
return;
|
||||
}
|
||||
|
||||
memcpy(ctx->buf + ctx->buflen, data, num);
|
||||
M = GETU32(ctx->buf);
|
||||
ctx->buflen = 0;
|
||||
|
||||
K1 = ZUC256_generate_keyword((ZUC256_KEY *)ctx);
|
||||
|
||||
for (i = 0; i < 32; i++) {
|
||||
if (M & 0x80000000) {
|
||||
for (j = 0; j < n; j++) {
|
||||
ctx->T[j] ^= ctx->K0[j];
|
||||
}
|
||||
}
|
||||
M <<= 1;
|
||||
for (j = 0; j < n - 1; j++) {
|
||||
ctx->K0[j] = (ctx->K0[j] << 1) | (ctx->K0[j + 1] >> 31);
|
||||
}
|
||||
ctx->K0[j] = (ctx->K0[j] << 1) | (K1 >> 31);
|
||||
K1 <<= 1;
|
||||
}
|
||||
|
||||
data += num;
|
||||
len -= num;
|
||||
}
|
||||
|
||||
while (len >= 4) {
|
||||
M = GETU32(data);
|
||||
K1 = ZUC256_generate_keyword((ZUC256_KEY *)ctx);
|
||||
|
||||
for (i = 0; i < 32; i++) {
|
||||
if (M & 0x80000000) {
|
||||
for (j = 0; j < n; j++) {
|
||||
ctx->T[j] ^= ctx->K0[j];
|
||||
}
|
||||
}
|
||||
M <<= 1;
|
||||
for (j = 0; j < n - 1; j++) {
|
||||
ctx->K0[j] = (ctx->K0[j] << 1) | (ctx->K0[j + 1] >> 31);
|
||||
}
|
||||
ctx->K0[j] = (ctx->K0[j] << 1) | (K1 >> 31);
|
||||
K1 <<= 1;
|
||||
}
|
||||
|
||||
data += 4;
|
||||
len -= 4;
|
||||
}
|
||||
|
||||
if (len) {
|
||||
memcpy(ctx->buf, data, len);
|
||||
ctx->buflen = len;
|
||||
}
|
||||
}
|
||||
|
||||
void ZUC256_MAC_final(ZUC256_MAC_CTX *ctx, const unsigned char *data, size_t nbits, unsigned char *mac)
|
||||
{
|
||||
ZUC_UINT32 K1, M;
|
||||
size_t n = ctx->macbits/32;
|
||||
size_t i, j;
|
||||
|
||||
|
||||
if (!data)
|
||||
nbits = 0;
|
||||
|
||||
if (nbits >= 8) {
|
||||
ZUC256_MAC_update(ctx, data, nbits/8);
|
||||
data += nbits/8;
|
||||
nbits %= 8;
|
||||
}
|
||||
|
||||
if (nbits)
|
||||
ctx->buf[ctx->buflen] = *data;
|
||||
|
||||
if (ctx->buflen || nbits) {
|
||||
M = GETU32(ctx->buf);
|
||||
K1 = ZUC256_generate_keyword((ZUC256_KEY *)ctx);
|
||||
|
||||
|
||||
for (i = 0; i < ctx->buflen * 8 + nbits; i++) {
|
||||
if (M & 0x80000000) {
|
||||
for (j = 0; j < n; j++) {
|
||||
ctx->T[j] ^= ctx->K0[j];
|
||||
}
|
||||
}
|
||||
M <<= 1;
|
||||
for (j = 0; j < n - 1; j++) {
|
||||
ctx->K0[j] = (ctx->K0[j] << 1) | (ctx->K0[j + 1] >> 31);
|
||||
}
|
||||
ctx->K0[j] = (ctx->K0[j] << 1) | (K1 >> 31);
|
||||
K1 <<= 1;
|
||||
}
|
||||
}
|
||||
|
||||
for (j = 0; j < n; j++) {
|
||||
ctx->T[j] ^= ctx->K0[j];
|
||||
PUTU32(mac, ctx->T[j]);
|
||||
mac += 4;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
82
crypto/zuc/zuc_eea.c
Normal file
82
crypto/zuc/zuc_eea.c
Normal file
@@ -0,0 +1,82 @@
|
||||
/* ====================================================================
|
||||
* Copyright (c) 2015 - 2019 The GmSSL Project. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. All advertising materials mentioning features or use of this
|
||||
* software must display the following acknowledgment:
|
||||
* "This product includes software developed by the GmSSL Project.
|
||||
* (http://gmssl.org/)"
|
||||
*
|
||||
* 4. The name "GmSSL Project" must not be used to endorse or promote
|
||||
* products derived from this software without prior written
|
||||
* permission. For written permission, please contact
|
||||
* guanzhi1980@gmail.com.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "GmSSL"
|
||||
* nor may "GmSSL" appear in their names without prior written
|
||||
* permission of the GmSSL Project.
|
||||
*
|
||||
* 6. Redistributions of any form whatsoever must retain the following
|
||||
* acknowledgment:
|
||||
* "This product includes software developed by the GmSSL Project
|
||||
* (http://gmssl.org/)"
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY
|
||||
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
||||
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
* ====================================================================
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <openssl/zuc.h>
|
||||
|
||||
static void ZUC_set_eea_key(ZUC_KEY *key, const unsigned char user_key[16],
|
||||
ZUC_UINT32 count, ZUC_UINT5 bearer, ZUC_BIT direction)
|
||||
{
|
||||
unsigned char iv[16] = {0};
|
||||
iv[0] = iv[8] = count >> 24;
|
||||
iv[1] = iv[9] = count >> 16;
|
||||
iv[2] = iv[10] = count >> 8;
|
||||
iv[3] = iv[11] = count;
|
||||
iv[4] = iv[12] = ((bearer << 1) | (direction & 1)) << 2;
|
||||
ZUC_set_key(key, user_key, iv);
|
||||
}
|
||||
|
||||
void ZUC_eea_encrypt(const ZUC_UINT32 *in, ZUC_UINT32 *out, size_t nbits,
|
||||
const unsigned char key[16], ZUC_UINT32 count, ZUC_UINT5 bearer,
|
||||
ZUC_BIT direction)
|
||||
{
|
||||
ZUC_KEY zuc_key;
|
||||
size_t nwords = (nbits + 31)/32;
|
||||
size_t i;
|
||||
|
||||
ZUC_set_eea_key(&zuc_key, key, count, bearer, direction);
|
||||
ZUC_generate_keystream(&zuc_key, nwords, out);
|
||||
for (i = 0; i < nwords; i++) {
|
||||
out[i] ^= in[i];
|
||||
}
|
||||
|
||||
if (nbits % 32 != 0) {
|
||||
out[nwords - 1] |= (0xffffffff << (32 - (nbits%32)));
|
||||
}
|
||||
}
|
||||
160
crypto/zuc/zuc_eia.c
Normal file
160
crypto/zuc/zuc_eia.c
Normal file
@@ -0,0 +1,160 @@
|
||||
/* ====================================================================
|
||||
* Copyright (c) 2015 - 2019 The GmSSL Project. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. All advertising materials mentioning features or use of this
|
||||
* software must display the following acknowledgment:
|
||||
* "This product includes software developed by the GmSSL Project.
|
||||
* (http://gmssl.org/)"
|
||||
*
|
||||
* 4. The name "GmSSL Project" must not be used to endorse or promote
|
||||
* products derived from this software without prior written
|
||||
* permission. For written permission, please contact
|
||||
* guanzhi1980@gmail.com.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "GmSSL"
|
||||
* nor may "GmSSL" appear in their names without prior written
|
||||
* permission of the GmSSL Project.
|
||||
*
|
||||
* 6. Redistributions of any form whatsoever must retain the following
|
||||
* acknowledgment:
|
||||
* "This product includes software developed by the GmSSL Project
|
||||
* (http://gmssl.org/)"
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY
|
||||
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
||||
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
* ====================================================================
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <openssl/zuc.h>
|
||||
#include <openssl/crypto.h>
|
||||
#include "modes_lcl.h"
|
||||
|
||||
static void zuc_set_eia_iv(unsigned char iv[16], ZUC_UINT32 count, ZUC_UINT5 bearer,
|
||||
ZUC_BIT direction)
|
||||
{
|
||||
memset(iv, 0, 16);
|
||||
iv[0] = count >> 24;
|
||||
iv[1] = iv[9] = count >> 16;
|
||||
iv[2] = iv[10] = count >> 8;
|
||||
iv[3] = iv[11] = count;
|
||||
iv[4] = iv[12] = bearer << 3;
|
||||
iv[8] = iv[0] ^ (direction << 7);
|
||||
iv[14] = (direction << 7);
|
||||
}
|
||||
|
||||
#if 1
|
||||
ZUC_UINT32 ZUC_eia_generate_mac(const ZUC_UINT32 *data, size_t nbits,
|
||||
const unsigned char key[16], ZUC_UINT32 count, ZUC_UINT5 bearer,
|
||||
ZUC_BIT direction)
|
||||
{
|
||||
ZUC_MAC_CTX ctx;
|
||||
unsigned char iv[16];
|
||||
unsigned char mac[4];
|
||||
zuc_set_eia_iv(iv, count, bearer, direction);
|
||||
ZUC_MAC_init(&ctx, key, iv);
|
||||
ZUC_MAC_final(&ctx, (unsigned char *)data, nbits, mac);
|
||||
return GETU32(mac);
|
||||
}
|
||||
#else
|
||||
|
||||
#define ZUC_MAC_BUF_WORDS 64
|
||||
|
||||
#define GET_WORD(p, i) ((i) % 32) \
|
||||
? ((*((ZUC_UINT32 *)(p) + (i)/32) << ((i) % 32)) \
|
||||
| (*((ZUC_UINT32 *)(p) + (i)/32 + 1) >> (32 - ((i) % 32)))) \
|
||||
: *((ZUC_UINT32 *)(p) + (i)/32)
|
||||
|
||||
#define GET_BIT(p, i) \
|
||||
(((*((ZUC_UINT32 *)(p) + (i)/32)) & (1 << (31 - ((i) % 32)))) ? 1 : 0)
|
||||
|
||||
ZUC_UINT32 ZUC_eia_generate_mac(const ZUC_UINT32 *data, size_t nbits,
|
||||
const unsigned char user_key[16], ZUC_UINT32 count, ZUC_UINT5 bearer,
|
||||
ZUC_BIT direction)
|
||||
{
|
||||
ZUC_UINT32 T = 0;
|
||||
ZUC_KEY key;
|
||||
unsigned char iv[16];
|
||||
ZUC_UINT32 buf[ZUC_MAC_BUF_WORDS + 2];
|
||||
size_t nwords = (nbits + 31)/32;
|
||||
size_t i;
|
||||
size_t num = ZUC_MAC_BUF_WORDS;
|
||||
|
||||
|
||||
ZUC_set_eia_iv(iv, count, bearer, direction);
|
||||
ZUC_set_key(&key, user_key, iv);
|
||||
|
||||
if (nwords <= ZUC_MAC_BUF_WORDS) {
|
||||
ZUC_generate_keystream(&key, nwords + 2, buf);
|
||||
for (i = 0; i < nbits; i++) {
|
||||
if (GET_BIT(data, i)) {
|
||||
T ^= GET_WORD(buf, i);
|
||||
}
|
||||
}
|
||||
T ^= GET_WORD(buf, i);
|
||||
T ^= buf[nwords + 1];
|
||||
return T;
|
||||
|
||||
} else {
|
||||
|
||||
ZUC_generate_keystream(&key, ZUC_MAC_BUF_WORDS + 1, buf);
|
||||
for (i = 0; i < ZUC_MAC_BUF_WORDS * 32; i++) {
|
||||
if (GET_BIT(data, i)) {
|
||||
T ^= GET_WORD(buf, i);
|
||||
}
|
||||
}
|
||||
data += ZUC_MAC_BUF_WORDS;
|
||||
nwords -= ZUC_MAC_BUF_WORDS;
|
||||
nbits -= ZUC_MAC_BUF_WORDS * 32;
|
||||
}
|
||||
|
||||
while (nwords > ZUC_MAC_BUF_WORDS) {
|
||||
buf[0] = buf[ZUC_MAC_BUF_WORDS];
|
||||
ZUC_generate_keystream(&key, ZUC_MAC_BUF_WORDS, buf + 1);
|
||||
for (i = 0; i < ZUC_MAC_BUF_WORDS * 32; i ++) {
|
||||
if (GET_BIT(data, i)) {
|
||||
T ^= GET_WORD(buf, i);
|
||||
}
|
||||
}
|
||||
data += num;
|
||||
nwords -= num;
|
||||
nbits -= ZUC_MAC_BUF_WORDS * 32;
|
||||
}
|
||||
|
||||
buf[0] = buf[ZUC_MAC_BUF_WORDS];
|
||||
ZUC_generate_keystream(&key, nwords + 1, buf + 1);
|
||||
for (i = 0; i < nbits; i++) {
|
||||
if (GET_BIT(data, i)) {
|
||||
T ^= GET_WORD(buf, i);
|
||||
}
|
||||
}
|
||||
|
||||
T ^= GET_WORD(buf, i);
|
||||
T ^= buf[nwords + 1];
|
||||
|
||||
return T;
|
||||
}
|
||||
#endif
|
||||
@@ -1,121 +0,0 @@
|
||||
|
||||
|
||||
#include <openssl/zuc.h>
|
||||
|
||||
static const ZUC_UINT7 ZUC256_MAC32_D[] = {
|
||||
0x22,0x2F,0x25,0x2A,0x6D,0x40,0x40,0x40,
|
||||
0x40,0x40,0x40,0x40,0x40,0x52,0x10,0x30
|
||||
};
|
||||
|
||||
static const ZUC_UINT7 ZUC256_MAC64_D[] = {
|
||||
0x23,0x2F,0x24,0x2A,0x6D,0x40,0x40,0x40,
|
||||
0x40,0x40,0x40,0x40,0x40,0x52,0x10,0x30,
|
||||
};
|
||||
|
||||
static const ZUC_UINT7 ZUC256_MAC128_D[] = {
|
||||
0x23,0x2F,0x25,0x2A,0x6D,0x40,0x40,0x40,
|
||||
0x40,0x40,0x40,0x40,0x40,0x52,0x10,0x30,
|
||||
};
|
||||
|
||||
typedef struct {
|
||||
ZUC_KEY zuc;
|
||||
ZUC_MAC_TAG t1;
|
||||
ZUC_MAC_TAG t2;
|
||||
int macbits;
|
||||
} ZUC256_MAC_CTX;
|
||||
|
||||
|
||||
int ZUC_MAC_init(ZUC_MAC *ctx, const unsigned char *key, int bits,
|
||||
const unsigned char *iv, int macbits)
|
||||
{
|
||||
const ZUC_UINT7 *K;
|
||||
ZUC_UINT31 *LFSR = key->LFSR;
|
||||
uint32_t R1, R2;
|
||||
uint32_t X0, X1, X2;
|
||||
uint32_t W, W1, W2, U, V;
|
||||
int i;
|
||||
|
||||
switch (macbits) {
|
||||
case 32:
|
||||
K = KD32;
|
||||
break;
|
||||
case 64:
|
||||
K = KD64;
|
||||
break;
|
||||
case 128:
|
||||
K = KD128;
|
||||
break;
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
|
||||
LFSR[0] = MAKEU31(K[0], D[0], K[21], K[16]);
|
||||
LFSR[1] = MAKEU31(K[1], D[1], K[22], K[17]);
|
||||
LFSR[2] = MAKEU31(K[2], D[2], K[23], K[18]);
|
||||
LFSR[3] = MAKEU31(K[3], D[3], K[24], K[19]);
|
||||
LFSR[4] = MAKEU31(K[4], D[4], K[25], K[20]);
|
||||
LFSR[5] = MAKEU31(IV[0], (D[5] | IV[17]), K[5], K[26]);
|
||||
LFSR[6] = MAKEU31(IV[1], (D[6] | IV[18]), K[6], K[27]);
|
||||
LFSR[7] = MAKEU31(IV[10], (D[7] | IV[19]), K[7], IV[2]);
|
||||
LFSR[8] = MAKEU31(K[8], (D[8] | IV[20]), IV[13], IV[11]);
|
||||
LFSR[9] = MAKEU31(K[9], (D[9] | IV[21]), IV[12], IV[4]);
|
||||
LFSR[10] = MAKEU31(IV[5], (D[10] | IV[22]), K[10], K[28]);
|
||||
LFSR[11] = MAKEU31(K[11], (D[11] | IV[23]), IV[6], IV[13]);
|
||||
LFSR[12] = MAKEU31(K[12], (D[12] | IV[24]), IV[7], IV[14]);
|
||||
LFSR[13] = MAKEU31(K[13], D[13], IV[15], IV[8]);
|
||||
LFSR[14] = MAKEU31(K[14], (D[14] | (K[31] >> 4)), IV[16], IV[9]);
|
||||
LFSR[15] = MAKEU31(K[15], (D[15] | (K[31] & 0xF0)), K[30], K[29]);
|
||||
|
||||
R1 = 0;
|
||||
R2 = 0;
|
||||
|
||||
for (i = 0; i < 32; i++) {
|
||||
BitReconstruction3(X0, X1, X2);
|
||||
W = F(X0, X1, X2);
|
||||
LFSRWithInitialisationMode(W >> 1);
|
||||
}
|
||||
|
||||
BitReconstruction2(X1, X2);
|
||||
F_(X1, X2);
|
||||
LFSRWithWorkMode();
|
||||
|
||||
key->R1 = R1;
|
||||
key->R2 = R2;
|
||||
|
||||
}
|
||||
|
||||
|
||||
#define MAKEU32(i,A,B) (((A) << (i)) | ((B) >> (32 - (i))))
|
||||
#define MASKU8(i,M) (-(((M) >> (7-i)) & 0x01))
|
||||
|
||||
int ZUC256_MAC32(ZUC256_MAC_CTX *ctx, const unsigned char *data, size_t len)
|
||||
{
|
||||
uint32_t T;
|
||||
uint32_t Z;
|
||||
uint32_t *m = data;
|
||||
|
||||
T = ZUC256_generate_keyword(key);
|
||||
Z0 = ZUC256_generate_keyword(key);
|
||||
Z1 = ZUC256_generate_keyword(key);
|
||||
|
||||
for (i = 0; i < len; i++) {
|
||||
|
||||
T ^= MAKEU32(Z0, Z1, (i * 8 + 0) % 32) & MASKU8(data[i], 7);
|
||||
T ^= MAKEU32(Z0, Z1, (i * 8 + 1) % 32) & MASKU8(data[i], 6);
|
||||
T ^= MAKEU32(Z0, Z1, (i * 8 + 2) % 32) & MASKU8(data[i], 5);
|
||||
T ^= MAKEU32(Z0, Z1, (i * 8 + 3) % 32) & MASKU8(data[i], 4);
|
||||
T ^= MAKEU32(Z0, Z1, (i * 8 + 4) % 32) & MASKU8(data[i], 3);
|
||||
T ^= MAKEU32(Z0, Z1, (i * 8 + 5) % 32) & MASKU8(data[i], 2);
|
||||
T ^= MAKEU32(Z0, Z1, (i * 8 + 6) % 32) & MASKU8(data[i], 1);
|
||||
T ^= MAKEU32(Z0, Z1, (i * 8 + 7) % 32) & MASKU8(data[i], 0);
|
||||
|
||||
if (i % 4 == 3) {
|
||||
Z0 = Z1;
|
||||
Z1 = ZUC256_generate_keyword(key);
|
||||
}
|
||||
}
|
||||
|
||||
T ^= MAKEU32(Z0, Z1, (i * 8) % 32);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user