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