mirror of
https://github.com/guanzhi/GmSSL.git
synced 2026-09-23 13:54:07 +08:00
Add 128-EEA3, 128-EIA3, ZUC-MAC, ZUC256, ZUC256-MAC and EVP_zuc256
This commit is contained in:
@@ -293,5 +293,6 @@ void openssl_add_all_ciphers_int(void)
|
||||
#endif
|
||||
#ifndef OPENSSL_NO_ZUC
|
||||
EVP_add_cipher(EVP_zuc());
|
||||
EVP_add_cipher(EVP_zuc256());
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -109,4 +109,31 @@ const EVP_CIPHER *EVP_zuc(void)
|
||||
{
|
||||
return &zuc_cipher;
|
||||
}
|
||||
|
||||
static int zuc256_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
|
||||
const unsigned char *iv, int enc)
|
||||
{
|
||||
EVP_ZUC_KEY *dctx = EVP_C_DATA(EVP_ZUC_KEY, ctx);
|
||||
ZUC256_set_key(&dctx->ks, key, iv);
|
||||
return 1;
|
||||
}
|
||||
|
||||
const EVP_CIPHER zuc256_cipher = {
|
||||
NID_zuc256,
|
||||
1,
|
||||
ZUC256_KEY_LENGTH,
|
||||
ZUC256_IV_LENGTH,
|
||||
0,
|
||||
zuc256_init_key,
|
||||
zuc_do_cipher,
|
||||
NULL,
|
||||
sizeof(EVP_ZUC_KEY),
|
||||
NULL,NULL,NULL,NULL,
|
||||
};
|
||||
|
||||
const EVP_CIPHER *EVP_zuc256(void)
|
||||
{
|
||||
return &zuc256_cipher;
|
||||
}
|
||||
|
||||
#endif /* OPENSSL_NO_ZUC */
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
*/
|
||||
|
||||
/* Serialized OID's */
|
||||
static const unsigned char so[7915] = {
|
||||
static const unsigned char so[7972] = {
|
||||
0x2A,0x86,0x48,0x86,0xF7,0x0D, /* [ 0] OBJ_rsadsi */
|
||||
0x2A,0x86,0x48,0x86,0xF7,0x0D,0x01, /* [ 6] OBJ_pkcs */
|
||||
0x2A,0x86,0x48,0x86,0xF7,0x0D,0x02,0x02, /* [ 13] OBJ_md2 */
|
||||
@@ -1104,9 +1104,15 @@ static const unsigned char so[7915] = {
|
||||
0x2A,0x81,0x1C,0xCF,0x55,0x01,0x82,0x2F,0x07, /* [ 7885] OBJ_sm9hash2 */
|
||||
0x2A,0x81,0x1C,0xCF,0x55,0x01,0x82,0x2F,0x07,0x01, /* [ 7894] OBJ_sm9hash2_with_sm3 */
|
||||
0x2A,0x81,0x1C,0xCF,0x55,0x01,0x82,0x2F,0x07,0x02, /* [ 7904] OBJ_sm9hash2_with_sha256 */
|
||||
0x2A,0x81,0x1C,0xCF,0x55,0x01,0x86,0x20,0x04, /* [ 7914] OBJ_zuc256 */
|
||||
0x2A,0x81,0x1C,0xCF,0x55,0x01,0x86,0x20,0x03, /* [ 7923] OBJ_zuc_mac */
|
||||
0x2A,0x81,0x1C,0xCF,0x55,0x01,0x86,0x20,0x05, /* [ 7932] OBJ_zuc256_mac */
|
||||
0x2A,0x81,0x1C,0xCF,0x55,0x01,0x86,0x20,0x05,0x01, /* [ 7941] OBJ_zuc256_mac32 */
|
||||
0x2A,0x81,0x1C,0xCF,0x55,0x01,0x86,0x20,0x05,0x02, /* [ 7951] OBJ_zuc256_mac64 */
|
||||
0x2A,0x81,0x1C,0xCF,0x55,0x01,0x86,0x20,0x05,0x03, /* [ 7961] OBJ_zuc256_mac128 */
|
||||
};
|
||||
|
||||
#define NUM_NID 1212
|
||||
#define NUM_NID 1218
|
||||
static const ASN1_OBJECT nid_objs[NUM_NID] = {
|
||||
{"UNDEF", "undefined", NID_undef},
|
||||
{"rsadsi", "RSA Data Security, Inc.", NID_rsadsi, 6, &so[0]},
|
||||
@@ -2320,9 +2326,15 @@ static const ASN1_OBJECT nid_objs[NUM_NID] = {
|
||||
{"sm9hash2", "sm9hash2", NID_sm9hash2, 9, &so[7885]},
|
||||
{"sm9hash2-with-sm3", "sm9hash2-with-sm3", NID_sm9hash2_with_sm3, 10, &so[7894]},
|
||||
{"sm9hash2-with-sha256", "sm9hash2-with-sha256", NID_sm9hash2_with_sha256, 10, &so[7904]},
|
||||
{"ZUC256", "zuc256", NID_zuc256, 9, &so[7914]},
|
||||
{"ZUC-MAC", "zuc-mac", NID_zuc_mac, 9, &so[7923]},
|
||||
{"ZUC256-MAC", "zuc256-mac", NID_zuc256_mac, 9, &so[7932]},
|
||||
{"ZUC256-MAC32", "zuc256-mac32", NID_zuc256_mac32, 10, &so[7941]},
|
||||
{"ZUC256-MAC64", "zuc256-mac64", NID_zuc256_mac64, 10, &so[7951]},
|
||||
{"ZUC256-MAC128", "zuc256-mac128", NID_zuc256_mac128, 10, &so[7961]},
|
||||
};
|
||||
|
||||
#define NUM_SN 1202
|
||||
#define NUM_SN 1208
|
||||
static const unsigned int sn_objs[NUM_SN] = {
|
||||
364, /* "AD_DVCS" */
|
||||
419, /* "AES-128-CBC" */
|
||||
@@ -2613,6 +2625,12 @@ static const unsigned int sn_objs[NUM_SN] = {
|
||||
185, /* "X9cm" */
|
||||
125, /* "ZLIB" */
|
||||
1185, /* "ZUC" */
|
||||
1213, /* "ZUC-MAC" */
|
||||
1212, /* "ZUC256" */
|
||||
1214, /* "ZUC256-MAC" */
|
||||
1217, /* "ZUC256-MAC128" */
|
||||
1215, /* "ZUC256-MAC32" */
|
||||
1216, /* "ZUC256-MAC64" */
|
||||
478, /* "aRecord" */
|
||||
289, /* "aaControls" */
|
||||
287, /* "ac-auditEntity" */
|
||||
@@ -3528,7 +3546,7 @@ static const unsigned int sn_objs[NUM_SN] = {
|
||||
1187, /* "zuc-128eia3" */
|
||||
};
|
||||
|
||||
#define NUM_LN 1202
|
||||
#define NUM_LN 1208
|
||||
static const unsigned int ln_objs[NUM_LN] = {
|
||||
363, /* "AD Time Stamping" */
|
||||
405, /* "ANSI X9.62" */
|
||||
@@ -4732,9 +4750,15 @@ static const unsigned int ln_objs[NUM_LN] = {
|
||||
1185, /* "zuc" */
|
||||
1186, /* "zuc-128eea3" */
|
||||
1187, /* "zuc-128eia3" */
|
||||
1213, /* "zuc-mac" */
|
||||
1212, /* "zuc256" */
|
||||
1214, /* "zuc256-mac" */
|
||||
1217, /* "zuc256-mac128" */
|
||||
1215, /* "zuc256-mac32" */
|
||||
1216, /* "zuc256-mac64" */
|
||||
};
|
||||
|
||||
#define NUM_OBJ 1099
|
||||
#define NUM_OBJ 1105
|
||||
static const unsigned int obj_objs[NUM_OBJ] = {
|
||||
0, /* OBJ_undef 0 */
|
||||
181, /* OBJ_iso 1 */
|
||||
@@ -5483,6 +5507,9 @@ static const unsigned int obj_objs[NUM_OBJ] = {
|
||||
1149, /* OBJ_hmac_sm3 1 2 156 10197 1 401 2 */
|
||||
1186, /* OBJ_zuc_128eea3 1 2 156 10197 1 800 1 */
|
||||
1187, /* OBJ_zuc_128eia3 1 2 156 10197 1 800 2 */
|
||||
1213, /* OBJ_zuc_mac 1 2 156 10197 1 800 3 */
|
||||
1212, /* OBJ_zuc256 1 2 156 10197 1 800 4 */
|
||||
1214, /* OBJ_zuc256_mac 1 2 156 10197 1 800 5 */
|
||||
1192, /* OBJ_wapip192v1 1 2 156 11235 1 1 2 1 */
|
||||
997, /* OBJ_id_tc26_gost_3410_2012_512_paramSetTest 1 2 643 7 1 2 1 2 0 */
|
||||
998, /* OBJ_id_tc26_gost_3410_2012_512_paramSetA 1 2 643 7 1 2 1 2 1 */
|
||||
@@ -5700,6 +5727,9 @@ static const unsigned int obj_objs[NUM_OBJ] = {
|
||||
1175, /* OBJ_sm9bn256v1 1 2 156 10197 1 302 6 1 */
|
||||
1210, /* OBJ_sm9hash2_with_sm3 1 2 156 10197 1 303 7 1 */
|
||||
1211, /* OBJ_sm9hash2_with_sha256 1 2 156 10197 1 303 7 2 */
|
||||
1215, /* OBJ_zuc256_mac32 1 2 156 10197 1 800 5 1 */
|
||||
1216, /* OBJ_zuc256_mac64 1 2 156 10197 1 800 5 2 */
|
||||
1217, /* OBJ_zuc256_mac128 1 2 156 10197 1 800 5 3 */
|
||||
189, /* OBJ_id_smime_mod 1 2 840 113549 1 9 16 0 */
|
||||
190, /* OBJ_id_smime_ct 1 2 840 113549 1 9 16 1 */
|
||||
191, /* OBJ_id_smime_aa 1 2 840 113549 1 9 16 2 */
|
||||
|
||||
@@ -1209,3 +1209,9 @@ paillier 1208
|
||||
sm9hash2 1209
|
||||
sm9hash2_with_sm3 1210
|
||||
sm9hash2_with_sha256 1211
|
||||
zuc256 1212
|
||||
zuc_mac 1213
|
||||
zuc256_mac 1214
|
||||
zuc256_mac32 1215
|
||||
zuc256_mac64 1216
|
||||
zuc256_mac128 1217
|
||||
|
||||
@@ -1650,6 +1650,13 @@ sm9kdf 2 : sm9kdf-with-sha256
|
||||
sm-scheme 800 : ZUC : zuc
|
||||
zuc 1 : zuc-128eea3
|
||||
zuc 2 : zuc-128eia3
|
||||
zuc 3 : ZUC-MAC : zuc-mac
|
||||
zuc 4 : ZUC256 : zuc256
|
||||
zuc 5 : ZUC256-MAC : zuc256-mac
|
||||
zuc256-mac 1 : ZUC256-MAC32 : zuc256-mac32
|
||||
zuc256-mac 2 : ZUC256-MAC64 : zuc256-mac64
|
||||
zuc256-mac 3 : ZUC256-MAC128 : zuc256-mac128
|
||||
|
||||
|
||||
# WAPI (GB 15629.11-2003-XG1-2006)
|
||||
ISO-CN 11235 : bwips
|
||||
|
||||
@@ -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