• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
3  *
4  *  Licensed under the Apache License, Version 2.0 (the License); you may
5  *  not use this file except in compliance with the License.
6  *
7  *  http://www.apache.org/licenses/LICENSE-2.0
8  */
9 
10 
11 #ifndef GMSSL_ZUC_H
12 #define GMSSL_ZUC_H
13 
14 
15 #include <stdlib.h>
16 #include <stdint.h>
17 
18 
19 #ifdef __cplusplus
20 extern "C" {
21 #endif
22 
23 
24 /*
25 ZUC Public API
26 
27 	ZUC_KEY_SIZE
28 	ZUC_IV_SIZE
29 	ZUC_MAC_SIZE
30 
31 	ZUC_CTX
32 	zuc_encrypt_init
33 	zuc_encrypt_update
34 	zuc_encrypt_finish
35 	zuc_decrypt_init
36 	zuc_decrypt_update
37 	zuc_decrypt_finish
38 
39 	ZUC_MAC_CTX
40 	zuc_mac_init
41 	zuc_mac_update
42 	zuc_mac_finish
43 
44 	zuc_eea_encrypt
45 	zuc_eia_generate_mac
46 */
47 
48 
49 # define ZUC_KEY_SIZE	16
50 # define ZUC_IV_SIZE	16
51 # define ZUC_MAC_SIZE	4
52 
53 typedef uint32_t ZUC_BIT;
54 typedef uint32_t ZUC_UINT5;
55 typedef uint8_t  ZUC_UINT6;
56 typedef uint32_t ZUC_UINT15;
57 typedef uint32_t ZUC_UINT31;
58 typedef uint32_t ZUC_UINT32;
59 
60 typedef struct {
61 	ZUC_UINT31 LFSR[16];
62 	ZUC_UINT32 R1;
63 	ZUC_UINT32 R2;
64 } ZUC_STATE;
65 
66 void zuc_init(ZUC_STATE *state, const uint8_t key[ZUC_KEY_SIZE], const uint8_t iv[ZUC_IV_SIZE]);
67 void zuc_generate_keystream(ZUC_STATE *state, size_t nwords, ZUC_UINT32 *words);
68 ZUC_UINT32 zuc_generate_keyword(ZUC_STATE *state);
69 void zuc_encrypt(ZUC_STATE *state, const uint8_t *in, size_t inlen, uint8_t *out);
70 
71 typedef struct ZUC_MAC_CTX_st {
72 	ZUC_UINT31 LFSR[16];
73 	ZUC_UINT32 R1;
74 	ZUC_UINT32 R2;
75 	ZUC_UINT32 T;
76 	ZUC_UINT32 K0;
77 	uint8_t buf[4];
78 	size_t buflen;
79 } ZUC_MAC_CTX;
80 
81 void zuc_mac_init(ZUC_MAC_CTX *ctx, const uint8_t key[ZUC_KEY_SIZE], const uint8_t iv[ZUC_IV_SIZE]);
82 void zuc_mac_update(ZUC_MAC_CTX *ctx, const uint8_t *data, size_t len);
83 void zuc_mac_finish(ZUC_MAC_CTX *ctx, const uint8_t *data, size_t nbits, uint8_t mac[ZUC_MAC_SIZE]);
84 
85 void zuc_eea_encrypt(const ZUC_UINT32 *in, ZUC_UINT32 *out, size_t nbits,
86 	const uint8_t key[ZUC_KEY_SIZE], ZUC_UINT32 count, ZUC_UINT5 bearer,
87 	ZUC_BIT direction);
88 ZUC_UINT32 zuc_eia_generate_mac(const ZUC_UINT32 *data, size_t nbits,
89 	const uint8_t key[ZUC_KEY_SIZE], ZUC_UINT32 count, ZUC_UINT5 bearer,
90 	ZUC_BIT direction);
91 
92 
93 # define ZUC256_KEY_SIZE	32
94 # define ZUC256_IV_SIZE		23
95 # define ZUC256_MAC32_SIZE	4
96 # define ZUC256_MAC64_SIZE	8
97 # define ZUC256_MAC128_SIZE	16
98 # define ZUC256_MIN_MAC_SIZE	ZUC256_MAC32_SIZE
99 # define ZUC256_MAX_MAC_SIZE	ZUC256_MAC128_SIZE
100 
101 typedef ZUC_STATE ZUC256_STATE;
102 
103 void zuc256_init(ZUC256_STATE *state, const uint8_t key[ZUC256_KEY_SIZE], const uint8_t iv[ZUC256_IV_SIZE]);
104 #define zuc256_generate_keystream(state,nwords,words) zuc_generate_keystream(state,nwords,words)
105 #define zuc256_generate_keyword(state) zuc_generate_keyword(state)
106 
107 
108 typedef struct ZUC256_MAC_CTX_st {
109 	ZUC_UINT31 LFSR[16];
110 	ZUC_UINT32 R1;
111 	ZUC_UINT32 R2;
112 	ZUC_UINT32 T[4];
113 	ZUC_UINT32 K0[4];
114 	uint8_t buf[4];
115 	int buflen;
116 	int macbits;
117 } ZUC256_MAC_CTX;
118 
119 void zuc256_mac_init(ZUC256_MAC_CTX *ctx, const uint8_t key[ZUC256_KEY_SIZE],
120 	const uint8_t iv[ZUC256_IV_SIZE], int macbits);
121 void zuc256_mac_update(ZUC256_MAC_CTX *ctx, const uint8_t *data, size_t len);
122 void zuc256_mac_finish(ZUC256_MAC_CTX *ctx, const uint8_t *data, size_t nbits, uint8_t mac[ZUC_MAC_SIZE]);
123 
124 
125 // Public API
126 
127 typedef struct {
128 	ZUC_STATE zuc_state;
129 	uint8_t block[4];
130 	size_t block_nbytes;
131 } ZUC_CTX;
132 
133 int zuc_encrypt_init(ZUC_CTX *ctx, const uint8_t key[ZUC_KEY_SIZE], const uint8_t iv[ZUC_IV_SIZE]);
134 int zuc_encrypt_update(ZUC_CTX *ctx, const uint8_t *in, size_t inlen, uint8_t *out, size_t *outlen);
135 int zuc_encrypt_finish(ZUC_CTX *ctx, uint8_t *out, size_t *outlen);
136 
137 #define zuc_decrypt_init(ctx,key,iv) zuc_encrypt_init(ctx,key,iv)
138 #define zuc_decrypt_update(ctx,in,inlen,out,outlen) zuc_encrypt_update(ctx,in,inlen,out,outlen)
139 #define zuc_decrypt_finish(ctx,out,outlen) zuc_encrypt_finish(ctx,out,outlen)
140 
141 
142 #ifdef __cplusplus
143 }
144 #endif
145 #endif
146