1
2
3
4
5
6 #ifndef _CIPHER_H
7 #define _CIPHER_H
8
9 #include <linux/atomic.h>
10 #include <linux/mailbox/brcm-message.h>
11 #include <linux/mailbox_client.h>
12 #include <crypto/aes.h>
13 #include <crypto/internal/hash.h>
14 #include <crypto/aead.h>
15 #include <crypto/arc4.h>
16 #include <crypto/gcm.h>
17 #include <crypto/sha.h>
18 #include <crypto/sha3.h>
19
20 #include "spu.h"
21 #include "spum.h"
22 #include "spu2.h"
23
24
25 #define MAX_SPUS 16
26
27 #define ARC4_STATE_SIZE 4
28
29 #define CCM_AES_IV_SIZE 16
30 #define CCM_ESP_IV_SIZE 8
31 #define RFC4543_ICV_SIZE 16
32
33 #define MAX_KEY_SIZE ARC4_MAX_KEY_SIZE
34 #define MAX_IV_SIZE AES_BLOCK_SIZE
35 #define MAX_DIGEST_SIZE SHA3_512_DIGEST_SIZE
36 #define MAX_ASSOC_SIZE 512
37
38
39 #define GCM_ESP_SALT_SIZE 4
40 #define CCM_ESP_SALT_SIZE 3
41 #define MAX_SALT_SIZE GCM_ESP_SALT_SIZE
42 #define GCM_ESP_SALT_OFFSET 0
43 #define CCM_ESP_SALT_OFFSET 1
44
45 #define GCM_ESP_DIGESTSIZE 16
46
47 #define MAX_HASH_BLOCK_SIZE SHA512_BLOCK_SIZE
48
49
50
51
52
53
54 #define HASH_CARRY_MAX MAX_HASH_BLOCK_SIZE
55
56
57 #define SPU_MSG_ALIGN 4
58
59
60 #define SPU_MB_RETRY_MAX 1000
61
62
63 enum op_type {
64 SPU_OP_CIPHER,
65 SPU_OP_HASH,
66 SPU_OP_HMAC,
67 SPU_OP_AEAD,
68 SPU_OP_NUM
69 };
70
71 enum spu_spu_type {
72 SPU_TYPE_SPUM,
73 SPU_TYPE_SPU2,
74 };
75
76
77
78
79
80 enum spu_spu_subtype {
81 SPU_SUBTYPE_SPUM_NS2,
82 SPU_SUBTYPE_SPUM_NSP,
83 SPU_SUBTYPE_SPU2_V1,
84 SPU_SUBTYPE_SPU2_V2
85 };
86
87 struct spu_type_subtype {
88 enum spu_spu_type type;
89 enum spu_spu_subtype subtype;
90 };
91
92 struct cipher_op {
93 enum spu_cipher_alg alg;
94 enum spu_cipher_mode mode;
95 };
96
97 struct auth_op {
98 enum hash_alg alg;
99 enum hash_mode mode;
100 };
101
102 struct iproc_alg_s {
103 u32 type;
104 union {
105 struct crypto_alg crypto;
106 struct ahash_alg hash;
107 struct aead_alg aead;
108 } alg;
109 struct cipher_op cipher_info;
110 struct auth_op auth_info;
111 bool auth_first;
112 bool registered;
113 };
114
115
116
117
118
119 struct spu_msg_buf {
120
121
122
123
124
125
126 u8 bcm_spu_req_hdr[ALIGN(SPU2_HEADER_ALLOC_LEN, SPU_MSG_ALIGN)];
127
128
129 u8 iv_ctr[ALIGN(2 * AES_BLOCK_SIZE, SPU_MSG_ALIGN)];
130
131
132 u8 digest[ALIGN(MAX_DIGEST_SIZE, SPU_MSG_ALIGN)];
133
134
135 u8 spu_req_pad[ALIGN(SPU_PAD_LEN_MAX, SPU_MSG_ALIGN)];
136
137
138 u8 tx_stat[ALIGN(SPU_TX_STATUS_LEN, SPU_MSG_ALIGN)];
139
140
141
142
143 u8 spu_resp_hdr[ALIGN(SPU2_HEADER_ALLOC_LEN, SPU_MSG_ALIGN)];
144
145
146 u8 rx_stat_pad[ALIGN(SPU_STAT_PAD_MAX, SPU_MSG_ALIGN)];
147
148
149 u8 rx_stat[ALIGN(SPU_RX_STATUS_LEN, SPU_MSG_ALIGN)];
150
151 union {
152
153 struct {
154
155
156
157
158 u8 supdt_tweak[ALIGN(SPU_SUPDT_LEN, SPU_MSG_ALIGN)];
159 } c;
160
161
162 struct {
163
164 u8 gcmpad[ALIGN(AES_BLOCK_SIZE, SPU_MSG_ALIGN)];
165
166
167 u8 req_aad_pad[ALIGN(SPU_PAD_LEN_MAX, SPU_MSG_ALIGN)];
168
169
170 u8 resp_aad[ALIGN(MAX_ASSOC_SIZE + MAX_IV_SIZE,
171 SPU_MSG_ALIGN)];
172 } a;
173 };
174 };
175
176 struct iproc_ctx_s {
177 u8 enckey[MAX_KEY_SIZE + ARC4_STATE_SIZE];
178 unsigned int enckeylen;
179
180 u8 authkey[MAX_KEY_SIZE + ARC4_STATE_SIZE];
181 unsigned int authkeylen;
182
183 u8 salt[MAX_SALT_SIZE];
184 unsigned int salt_len;
185 unsigned int salt_offset;
186 u8 iv[MAX_IV_SIZE];
187
188 unsigned int digestsize;
189
190 struct iproc_alg_s *alg;
191 bool is_esp;
192
193 struct cipher_op cipher;
194 enum spu_cipher_type cipher_type;
195
196 struct auth_op auth;
197 bool auth_first;
198
199
200
201
202
203
204
205
206 unsigned int max_payload;
207
208 struct crypto_aead *fallback_cipher;
209
210
211
212 u8 ipad[MAX_HASH_BLOCK_SIZE];
213 u8 opad[MAX_HASH_BLOCK_SIZE];
214
215
216
217
218
219
220
221 u8 bcm_spu_req_hdr[ALIGN(SPU2_HEADER_ALLOC_LEN, SPU_MSG_ALIGN)];
222
223
224 u16 spu_req_hdr_len;
225
226
227 u16 spu_resp_hdr_len;
228
229
230
231
232
233 struct shash_desc *shash;
234
235 bool is_rfc4543;
236 };
237
238
239 struct spu_hash_export_s {
240 unsigned int total_todo;
241 unsigned int total_sent;
242 u8 hash_carry[HASH_CARRY_MAX];
243 unsigned int hash_carry_len;
244 u8 incr_hash[MAX_DIGEST_SIZE];
245 bool is_sw_hmac;
246 };
247
248 struct iproc_reqctx_s {
249
250 struct crypto_async_request *parent;
251
252
253 struct iproc_ctx_s *ctx;
254
255 u8 chan_idx;
256
257
258 unsigned int total_todo;
259 unsigned int total_received;
260 unsigned int total_sent;
261
262
263
264
265
266
267 unsigned int src_sent;
268
269
270
271
272
273
274
275 struct scatterlist *assoc;
276
277
278
279
280
281
282
283 struct scatterlist *src_sg;
284 int src_nents;
285 u32 src_skip;
286
287
288
289
290
291 struct scatterlist *dst_sg;
292 int dst_nents;
293 u32 dst_skip;
294
295
296 struct brcm_message mb_mssg;
297
298 bool bd_suppress;
299
300
301 bool is_encrypt;
302
303
304
305
306
307
308 u8 *iv_ctr;
309
310 unsigned int iv_ctr_len;
311
312
313
314
315
316
317
318
319
320
321
322
323 u8 hash_carry[HASH_CARRY_MAX];
324 unsigned int hash_carry_len;
325 unsigned int is_final;
326
327
328
329
330
331
332
333
334 u8 incr_hash[MAX_DIGEST_SIZE];
335
336
337 bool is_sw_hmac;
338
339
340 struct crypto_tfm *old_tfm;
341 crypto_completion_t old_complete;
342 void *old_data;
343
344 gfp_t gfp;
345
346
347 struct spu_msg_buf msg_buf;
348 };
349
350
351
352
353
354
355
356 struct spu_hw {
357 void (*spu_dump_msg_hdr)(u8 *buf, unsigned int buf_len);
358 u32 (*spu_ctx_max_payload)(enum spu_cipher_alg cipher_alg,
359 enum spu_cipher_mode cipher_mode,
360 unsigned int blocksize);
361 u32 (*spu_payload_length)(u8 *spu_hdr);
362 u16 (*spu_response_hdr_len)(u16 auth_key_len, u16 enc_key_len,
363 bool is_hash);
364 u16 (*spu_hash_pad_len)(enum hash_alg hash_alg,
365 enum hash_mode hash_mode, u32 chunksize,
366 u16 hash_block_size);
367 u32 (*spu_gcm_ccm_pad_len)(enum spu_cipher_mode cipher_mode,
368 unsigned int data_size);
369 u32 (*spu_assoc_resp_len)(enum spu_cipher_mode cipher_mode,
370 unsigned int assoc_len,
371 unsigned int iv_len, bool is_encrypt);
372 u8 (*spu_aead_ivlen)(enum spu_cipher_mode cipher_mode,
373 u16 iv_len);
374 enum hash_type (*spu_hash_type)(u32 src_sent);
375 u32 (*spu_digest_size)(u32 digest_size, enum hash_alg alg,
376 enum hash_type);
377 u32 (*spu_create_request)(u8 *spu_hdr,
378 struct spu_request_opts *req_opts,
379 struct spu_cipher_parms *cipher_parms,
380 struct spu_hash_parms *hash_parms,
381 struct spu_aead_parms *aead_parms,
382 unsigned int data_size);
383 u16 (*spu_cipher_req_init)(u8 *spu_hdr,
384 struct spu_cipher_parms *cipher_parms);
385 void (*spu_cipher_req_finish)(u8 *spu_hdr,
386 u16 spu_req_hdr_len,
387 unsigned int is_inbound,
388 struct spu_cipher_parms *cipher_parms,
389 bool update_key,
390 unsigned int data_size);
391 void (*spu_request_pad)(u8 *pad_start, u32 gcm_padding,
392 u32 hash_pad_len, enum hash_alg auth_alg,
393 enum hash_mode auth_mode,
394 unsigned int total_sent, u32 status_padding);
395 u8 (*spu_xts_tweak_in_payload)(void);
396 u8 (*spu_tx_status_len)(void);
397 u8 (*spu_rx_status_len)(void);
398 int (*spu_status_process)(u8 *statp);
399 void (*spu_ccm_update_iv)(unsigned int digestsize,
400 struct spu_cipher_parms *cipher_parms,
401 unsigned int assoclen, unsigned int chunksize,
402 bool is_encrypt, bool is_esp);
403 u32 (*spu_wordalign_padlen)(u32 data_size);
404
405
406 void __iomem *reg_vbase[MAX_SPUS];
407
408
409 enum spu_spu_type spu_type;
410
411
412 enum spu_spu_subtype spu_subtype;
413
414
415 u32 num_spu;
416
417
418 u32 num_chan;
419 };
420
421 struct device_private {
422 struct platform_device *pdev;
423
424 struct spu_hw spu;
425
426 atomic_t session_count;
427 atomic_t stream_count;
428
429
430 u8 bcm_hdr_len;
431
432
433 atomic_t next_chan;
434
435 struct dentry *debugfs_dir;
436 struct dentry *debugfs_stats;
437
438
439 atomic64_t bytes_in;
440 atomic64_t bytes_out;
441
442
443 atomic_t op_counts[SPU_OP_NUM];
444
445 atomic_t cipher_cnt[CIPHER_ALG_LAST][CIPHER_MODE_LAST];
446 atomic_t hash_cnt[HASH_ALG_LAST];
447 atomic_t hmac_cnt[HASH_ALG_LAST];
448 atomic_t aead_cnt[AEAD_TYPE_LAST];
449
450
451 atomic_t setkey_cnt[SPU_OP_NUM];
452
453
454 atomic_t mb_no_spc;
455
456
457 atomic_t mb_send_fail;
458
459
460 atomic_t bad_icv;
461
462 struct mbox_client mcl;
463
464
465 struct mbox_chan **mbox;
466 };
467
468 extern struct device_private iproc_priv;
469
470 #endif