1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26#ifndef _SPU_H
27#define _SPU_H
28
29#include <linux/types.h>
30#include <linux/scatterlist.h>
31#include <crypto/sha.h>
32
33enum spu_cipher_alg {
34 CIPHER_ALG_NONE = 0x0,
35 CIPHER_ALG_RC4 = 0x1,
36 CIPHER_ALG_DES = 0x2,
37 CIPHER_ALG_3DES = 0x3,
38 CIPHER_ALG_AES = 0x4,
39 CIPHER_ALG_LAST = 0x5
40};
41
42enum spu_cipher_mode {
43 CIPHER_MODE_NONE = 0x0,
44 CIPHER_MODE_ECB = 0x0,
45 CIPHER_MODE_CBC = 0x1,
46 CIPHER_MODE_OFB = 0x2,
47 CIPHER_MODE_CFB = 0x3,
48 CIPHER_MODE_CTR = 0x4,
49 CIPHER_MODE_CCM = 0x5,
50 CIPHER_MODE_GCM = 0x6,
51 CIPHER_MODE_XTS = 0x7,
52 CIPHER_MODE_LAST = 0x8
53};
54
55enum spu_cipher_type {
56 CIPHER_TYPE_NONE = 0x0,
57 CIPHER_TYPE_DES = 0x0,
58 CIPHER_TYPE_3DES = 0x0,
59 CIPHER_TYPE_INIT = 0x0,
60 CIPHER_TYPE_AES128 = 0x0,
61 CIPHER_TYPE_AES192 = 0x1,
62 CIPHER_TYPE_UPDT = 0x1,
63 CIPHER_TYPE_AES256 = 0x2,
64};
65
66enum hash_alg {
67 HASH_ALG_NONE = 0x0,
68 HASH_ALG_MD5 = 0x1,
69 HASH_ALG_SHA1 = 0x2,
70 HASH_ALG_SHA224 = 0x3,
71 HASH_ALG_SHA256 = 0x4,
72 HASH_ALG_AES = 0x5,
73 HASH_ALG_SHA384 = 0x6,
74 HASH_ALG_SHA512 = 0x7,
75
76 HASH_ALG_SHA3_224 = 0x8,
77 HASH_ALG_SHA3_256 = 0x9,
78 HASH_ALG_SHA3_384 = 0xa,
79 HASH_ALG_SHA3_512 = 0xb,
80 HASH_ALG_LAST
81};
82
83enum hash_mode {
84 HASH_MODE_NONE = 0x0,
85 HASH_MODE_HASH = 0x0,
86 HASH_MODE_XCBC = 0x0,
87 HASH_MODE_CMAC = 0x1,
88 HASH_MODE_CTXT = 0x1,
89 HASH_MODE_HMAC = 0x2,
90 HASH_MODE_RABIN = 0x4,
91 HASH_MODE_FHMAC = 0x6,
92 HASH_MODE_CCM = 0x5,
93 HASH_MODE_GCM = 0x6,
94};
95
96enum hash_type {
97 HASH_TYPE_NONE = 0x0,
98 HASH_TYPE_FULL = 0x0,
99 HASH_TYPE_INIT = 0x1,
100 HASH_TYPE_UPDT = 0x2,
101 HASH_TYPE_FIN = 0x3,
102 HASH_TYPE_AES128 = 0x0,
103 HASH_TYPE_AES192 = 0x1,
104 HASH_TYPE_AES256 = 0x2
105};
106
107enum aead_type {
108 AES_CCM,
109 AES_GCM,
110 AUTHENC,
111 AEAD_TYPE_LAST
112};
113
114extern char *hash_alg_name[HASH_ALG_LAST];
115extern char *aead_alg_name[AEAD_TYPE_LAST];
116
117struct spu_request_opts {
118 bool is_inbound;
119 bool auth_first;
120 bool is_aead;
121 bool is_esp;
122 bool bd_suppress;
123 bool is_rfc4543;
124};
125
126struct spu_cipher_parms {
127 enum spu_cipher_alg alg;
128 enum spu_cipher_mode mode;
129 enum spu_cipher_type type;
130 u8 *key_buf;
131 u16 key_len;
132
133 u8 *iv_buf;
134 u16 iv_len;
135};
136
137struct spu_hash_parms {
138 enum hash_alg alg;
139 enum hash_mode mode;
140 enum hash_type type;
141 u8 digestsize;
142 u8 *key_buf;
143 u16 key_len;
144 u16 prebuf_len;
145
146 int pad_len;
147};
148
149struct spu_aead_parms {
150 u32 assoc_size;
151 u16 iv_len;
152 u8 aad_pad_len;
153 u8 data_pad_len;
154 bool return_iv;
155 u32 ret_iv_len;
156 u32 ret_iv_off;
157};
158
159
160
161#define SPU_RX_STATUS_LEN 4
162
163
164#define SPU_STAT_PAD_MAX 4
165
166
167#define SPU_PAD_LEN_MAX (SPU_GCM_CCM_ALIGN + MAX_HASH_BLOCK_SIZE + \
168 SPU_STAT_PAD_MAX)
169
170
171#define SPU_GCM_CCM_ALIGN 16
172
173
174#define SPU_SUPDT_LEN 260
175
176
177
178
179#define SPU_INVALID_ICV 1
180
181
182#define SPU_MAX_PAYLOAD_INF 0xFFFFFFFF
183
184
185#define SPU_XTS_TWEAK_SIZE 16
186
187
188#define CCM_B0_ADATA 0x40
189#define CCM_B0_ADATA_SHIFT 6
190#define CCM_B0_M_PRIME 0x38
191#define CCM_B0_M_PRIME_SHIFT 3
192#define CCM_B0_L_PRIME 0x07
193#define CCM_B0_L_PRIME_SHIFT 0
194#define CCM_ESP_L_VALUE 4
195
196
197
198
199
200
201
202
203
204static __always_inline bool spu_req_incl_icv(enum spu_cipher_mode cipher_mode,
205 bool is_encrypt)
206{
207 if ((cipher_mode == CIPHER_MODE_GCM) && !is_encrypt)
208 return true;
209 if ((cipher_mode == CIPHER_MODE_CCM) && !is_encrypt)
210 return true;
211
212 return false;
213}
214
215static __always_inline u32 spu_real_db_size(u32 assoc_size,
216 u32 aead_iv_buf_len,
217 u32 prebuf_len,
218 u32 data_size,
219 u32 aad_pad_len,
220 u32 gcm_pad_len,
221 u32 hash_pad_len)
222{
223 return assoc_size + aead_iv_buf_len + prebuf_len + data_size +
224 aad_pad_len + gcm_pad_len + hash_pad_len;
225}
226
227
228
229void spum_dump_msg_hdr(u8 *buf, unsigned int buf_len);
230
231u32 spum_ns2_ctx_max_payload(enum spu_cipher_alg cipher_alg,
232 enum spu_cipher_mode cipher_mode,
233 unsigned int blocksize);
234u32 spum_nsp_ctx_max_payload(enum spu_cipher_alg cipher_alg,
235 enum spu_cipher_mode cipher_mode,
236 unsigned int blocksize);
237u32 spum_payload_length(u8 *spu_hdr);
238u16 spum_response_hdr_len(u16 auth_key_len, u16 enc_key_len, bool is_hash);
239u16 spum_hash_pad_len(enum hash_alg hash_alg, enum hash_mode hash_mode,
240 u32 chunksize, u16 hash_block_size);
241u32 spum_gcm_ccm_pad_len(enum spu_cipher_mode cipher_mode,
242 unsigned int data_size);
243u32 spum_assoc_resp_len(enum spu_cipher_mode cipher_mode,
244 unsigned int assoc_len, unsigned int iv_len,
245 bool is_encrypt);
246u8 spum_aead_ivlen(enum spu_cipher_mode cipher_mode, u16 iv_len);
247bool spu_req_incl_icv(enum spu_cipher_mode cipher_mode, bool is_encrypt);
248enum hash_type spum_hash_type(u32 src_sent);
249u32 spum_digest_size(u32 alg_digest_size, enum hash_alg alg,
250 enum hash_type htype);
251
252u32 spum_create_request(u8 *spu_hdr,
253 struct spu_request_opts *req_opts,
254 struct spu_cipher_parms *cipher_parms,
255 struct spu_hash_parms *hash_parms,
256 struct spu_aead_parms *aead_parms,
257 unsigned int data_size);
258
259u16 spum_cipher_req_init(u8 *spu_hdr, struct spu_cipher_parms *cipher_parms);
260
261void spum_cipher_req_finish(u8 *spu_hdr,
262 u16 spu_req_hdr_len,
263 unsigned int is_inbound,
264 struct spu_cipher_parms *cipher_parms,
265 bool update_key,
266 unsigned int data_size);
267
268void spum_request_pad(u8 *pad_start,
269 u32 gcm_padding,
270 u32 hash_pad_len,
271 enum hash_alg auth_alg,
272 enum hash_mode auth_mode,
273 unsigned int total_sent, u32 status_padding);
274
275u8 spum_xts_tweak_in_payload(void);
276u8 spum_tx_status_len(void);
277u8 spum_rx_status_len(void);
278int spum_status_process(u8 *statp);
279
280void spum_ccm_update_iv(unsigned int digestsize,
281 struct spu_cipher_parms *cipher_parms,
282 unsigned int assoclen,
283 unsigned int chunksize,
284 bool is_encrypt,
285 bool is_esp);
286u32 spum_wordalign_padlen(u32 data_size);
287#endif
288