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