1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17#include <linux/kernel.h>
18#include <linux/string.h>
19
20#include "util.h"
21#include "spu.h"
22#include "spum.h"
23#include "cipher.h"
24
25
26char *tag_to_hash_idx[] = { "none", "md5", "sha1", "sha224", "sha256" };
27
28char *hash_alg_name[] = { "None", "md5", "sha1", "sha224", "sha256", "aes",
29 "sha384", "sha512", "sha3_224", "sha3_256", "sha3_384", "sha3_512" };
30
31char *aead_alg_name[] = { "ccm(aes)", "gcm(aes)", "authenc" };
32
33
34void spum_dump_msg_hdr(u8 *buf, unsigned int buf_len)
35{
36 u8 *ptr = buf;
37 struct SPUHEADER *spuh = (struct SPUHEADER *)buf;
38 unsigned int hash_key_len = 0;
39 unsigned int hash_state_len = 0;
40 unsigned int cipher_key_len = 0;
41 unsigned int iv_len;
42 u32 pflags;
43 u32 cflags;
44 u32 ecf;
45 u32 cipher_alg;
46 u32 cipher_mode;
47 u32 cipher_type;
48 u32 hash_alg;
49 u32 hash_mode;
50 u32 hash_type;
51 u32 sctx_size;
52 u32 sctx_pl_len;
53
54 packet_log("\n");
55 packet_log("SPU Message header %p len: %u\n", buf, buf_len);
56
57
58 packet_log(" MH 0x%08x\n", be32_to_cpu(*((u32 *)ptr)));
59 if (spuh->mh.flags & MH_SCTX_PRES)
60 packet_log(" SCTX present\n");
61 if (spuh->mh.flags & MH_BDESC_PRES)
62 packet_log(" BDESC present\n");
63 if (spuh->mh.flags & MH_MFM_PRES)
64 packet_log(" MFM present\n");
65 if (spuh->mh.flags & MH_BD_PRES)
66 packet_log(" BD present\n");
67 if (spuh->mh.flags & MH_HASH_PRES)
68 packet_log(" HASH present\n");
69 if (spuh->mh.flags & MH_SUPDT_PRES)
70 packet_log(" SUPDT present\n");
71 packet_log(" Opcode 0x%02x\n", spuh->mh.op_code);
72
73 ptr += sizeof(spuh->mh) + sizeof(spuh->emh);
74
75
76 if (spuh->mh.flags & MH_SCTX_PRES) {
77 pflags = be32_to_cpu(spuh->sa.proto_flags);
78 packet_log(" SCTX[0] 0x%08x\n", pflags);
79 sctx_size = pflags & SCTX_SIZE;
80 packet_log(" Size %u words\n", sctx_size);
81
82 cflags = be32_to_cpu(spuh->sa.cipher_flags);
83 packet_log(" SCTX[1] 0x%08x\n", cflags);
84 packet_log(" Inbound:%lu (1:decrypt/vrfy 0:encrypt/auth)\n",
85 (cflags & CIPHER_INBOUND) >> CIPHER_INBOUND_SHIFT);
86 packet_log(" Order:%lu (1:AuthFirst 0:EncFirst)\n",
87 (cflags & CIPHER_ORDER) >> CIPHER_ORDER_SHIFT);
88 packet_log(" ICV_IS_512:%lx\n",
89 (cflags & ICV_IS_512) >> ICV_IS_512_SHIFT);
90 cipher_alg = (cflags & CIPHER_ALG) >> CIPHER_ALG_SHIFT;
91 cipher_mode = (cflags & CIPHER_MODE) >> CIPHER_MODE_SHIFT;
92 cipher_type = (cflags & CIPHER_TYPE) >> CIPHER_TYPE_SHIFT;
93 packet_log(" Crypto Alg:%u Mode:%u Type:%u\n",
94 cipher_alg, cipher_mode, cipher_type);
95 hash_alg = (cflags & HASH_ALG) >> HASH_ALG_SHIFT;
96 hash_mode = (cflags & HASH_MODE) >> HASH_MODE_SHIFT;
97 hash_type = (cflags & HASH_TYPE) >> HASH_TYPE_SHIFT;
98 packet_log(" Hash Alg:%x Mode:%x Type:%x\n",
99 hash_alg, hash_mode, hash_type);
100 packet_log(" UPDT_Offset:%u\n", cflags & UPDT_OFST);
101
102 ecf = be32_to_cpu(spuh->sa.ecf);
103 packet_log(" SCTX[2] 0x%08x\n", ecf);
104 packet_log(" WriteICV:%lu CheckICV:%lu ICV_SIZE:%u ",
105 (ecf & INSERT_ICV) >> INSERT_ICV_SHIFT,
106 (ecf & CHECK_ICV) >> CHECK_ICV_SHIFT,
107 (ecf & ICV_SIZE) >> ICV_SIZE_SHIFT);
108 packet_log("BD_SUPPRESS:%lu\n",
109 (ecf & BD_SUPPRESS) >> BD_SUPPRESS_SHIFT);
110 packet_log(" SCTX_IV:%lu ExplicitIV:%lu GenIV:%lu ",
111 (ecf & SCTX_IV) >> SCTX_IV_SHIFT,
112 (ecf & EXPLICIT_IV) >> EXPLICIT_IV_SHIFT,
113 (ecf & GEN_IV) >> GEN_IV_SHIFT);
114 packet_log("IV_OV_OFST:%lu EXP_IV_SIZE:%u\n",
115 (ecf & IV_OFFSET) >> IV_OFFSET_SHIFT,
116 ecf & EXP_IV_SIZE);
117
118 ptr += sizeof(struct SCTX);
119
120 if (hash_alg && hash_mode) {
121 char *name = "NONE";
122
123 switch (hash_alg) {
124 case HASH_ALG_MD5:
125 hash_key_len = 16;
126 name = "MD5";
127 break;
128 case HASH_ALG_SHA1:
129 hash_key_len = 20;
130 name = "SHA1";
131 break;
132 case HASH_ALG_SHA224:
133 hash_key_len = 28;
134 name = "SHA224";
135 break;
136 case HASH_ALG_SHA256:
137 hash_key_len = 32;
138 name = "SHA256";
139 break;
140 case HASH_ALG_SHA384:
141 hash_key_len = 48;
142 name = "SHA384";
143 break;
144 case HASH_ALG_SHA512:
145 hash_key_len = 64;
146 name = "SHA512";
147 break;
148 case HASH_ALG_AES:
149 hash_key_len = 0;
150 name = "AES";
151 break;
152 case HASH_ALG_NONE:
153 break;
154 }
155
156 packet_log(" Auth Key Type:%s Length:%u Bytes\n",
157 name, hash_key_len);
158 packet_dump(" KEY: ", ptr, hash_key_len);
159 ptr += hash_key_len;
160 } else if ((hash_alg == HASH_ALG_AES) &&
161 (hash_mode == HASH_MODE_XCBC)) {
162 char *name = "NONE";
163
164 switch (cipher_type) {
165 case CIPHER_TYPE_AES128:
166 hash_key_len = 16;
167 name = "AES128-XCBC";
168 break;
169 case CIPHER_TYPE_AES192:
170 hash_key_len = 24;
171 name = "AES192-XCBC";
172 break;
173 case CIPHER_TYPE_AES256:
174 hash_key_len = 32;
175 name = "AES256-XCBC";
176 break;
177 }
178 packet_log(" Auth Key Type:%s Length:%u Bytes\n",
179 name, hash_key_len);
180 packet_dump(" KEY: ", ptr, hash_key_len);
181 ptr += hash_key_len;
182 }
183
184 if (hash_alg && (hash_mode == HASH_MODE_NONE) &&
185 (hash_type == HASH_TYPE_UPDT)) {
186 char *name = "NONE";
187
188 switch (hash_alg) {
189 case HASH_ALG_MD5:
190 hash_state_len = 16;
191 name = "MD5";
192 break;
193 case HASH_ALG_SHA1:
194 hash_state_len = 20;
195 name = "SHA1";
196 break;
197 case HASH_ALG_SHA224:
198 hash_state_len = 32;
199 name = "SHA224";
200 break;
201 case HASH_ALG_SHA256:
202 hash_state_len = 32;
203 name = "SHA256";
204 break;
205 case HASH_ALG_SHA384:
206 hash_state_len = 48;
207 name = "SHA384";
208 break;
209 case HASH_ALG_SHA512:
210 hash_state_len = 64;
211 name = "SHA512";
212 break;
213 case HASH_ALG_AES:
214 hash_state_len = 0;
215 name = "AES";
216 break;
217 case HASH_ALG_NONE:
218 break;
219 }
220
221 packet_log(" Auth State Type:%s Length:%u Bytes\n",
222 name, hash_state_len);
223 packet_dump(" State: ", ptr, hash_state_len);
224 ptr += hash_state_len;
225 }
226
227 if (cipher_alg) {
228 char *name = "NONE";
229
230 switch (cipher_alg) {
231 case CIPHER_ALG_DES:
232 cipher_key_len = 8;
233 name = "DES";
234 break;
235 case CIPHER_ALG_3DES:
236 cipher_key_len = 24;
237 name = "3DES";
238 break;
239 case CIPHER_ALG_RC4:
240 cipher_key_len = 260;
241 name = "ARC4";
242 break;
243 case CIPHER_ALG_AES:
244 switch (cipher_type) {
245 case CIPHER_TYPE_AES128:
246 cipher_key_len = 16;
247 name = "AES128";
248 break;
249 case CIPHER_TYPE_AES192:
250 cipher_key_len = 24;
251 name = "AES192";
252 break;
253 case CIPHER_TYPE_AES256:
254 cipher_key_len = 32;
255 name = "AES256";
256 break;
257 }
258 break;
259 case CIPHER_ALG_NONE:
260 break;
261 }
262
263 packet_log(" Cipher Key Type:%s Length:%u Bytes\n",
264 name, cipher_key_len);
265
266
267 if (cipher_mode == CIPHER_MODE_XTS) {
268 packet_dump(" KEY2: ", ptr, cipher_key_len);
269 ptr += cipher_key_len;
270 packet_dump(" KEY1: ", ptr, cipher_key_len);
271 ptr += cipher_key_len;
272
273 cipher_key_len *= 2;
274 } else {
275 packet_dump(" KEY: ", ptr, cipher_key_len);
276 ptr += cipher_key_len;
277 }
278
279 if (ecf & SCTX_IV) {
280 sctx_pl_len = sctx_size * sizeof(u32) -
281 sizeof(struct SCTX);
282 iv_len = sctx_pl_len -
283 (hash_key_len + hash_state_len +
284 cipher_key_len);
285 packet_log(" IV Length:%u Bytes\n", iv_len);
286 packet_dump(" IV: ", ptr, iv_len);
287 ptr += iv_len;
288 }
289 }
290 }
291
292
293 if (spuh->mh.flags & MH_BDESC_PRES) {
294#ifdef DEBUG
295 struct BDESC_HEADER *bdesc = (struct BDESC_HEADER *)ptr;
296#endif
297 packet_log(" BDESC[0] 0x%08x\n", be32_to_cpu(*((u32 *)ptr)));
298 packet_log(" OffsetMAC:%u LengthMAC:%u\n",
299 be16_to_cpu(bdesc->offset_mac),
300 be16_to_cpu(bdesc->length_mac));
301 ptr += sizeof(u32);
302
303 packet_log(" BDESC[1] 0x%08x\n", be32_to_cpu(*((u32 *)ptr)));
304 packet_log(" OffsetCrypto:%u LengthCrypto:%u\n",
305 be16_to_cpu(bdesc->offset_crypto),
306 be16_to_cpu(bdesc->length_crypto));
307 ptr += sizeof(u32);
308
309 packet_log(" BDESC[2] 0x%08x\n", be32_to_cpu(*((u32 *)ptr)));
310 packet_log(" OffsetICV:%u OffsetIV:%u\n",
311 be16_to_cpu(bdesc->offset_icv),
312 be16_to_cpu(bdesc->offset_iv));
313 ptr += sizeof(u32);
314 }
315
316
317 if (spuh->mh.flags & MH_BD_PRES) {
318#ifdef DEBUG
319 struct BD_HEADER *bd = (struct BD_HEADER *)ptr;
320#endif
321 packet_log(" BD[0] 0x%08x\n", be32_to_cpu(*((u32 *)ptr)));
322 packet_log(" Size:%ubytes PrevLength:%u\n",
323 be16_to_cpu(bd->size), be16_to_cpu(bd->prev_length));
324 ptr += 4;
325 }
326
327
328 if (buf + buf_len != ptr) {
329 packet_log(" Packet parsed incorrectly. ");
330 packet_log("buf:%p buf_len:%u buf+buf_len:%p ptr:%p\n",
331 buf, buf_len, buf + buf_len, ptr);
332 }
333
334 packet_log("\n");
335}
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350u32 spum_ns2_ctx_max_payload(enum spu_cipher_alg cipher_alg,
351 enum spu_cipher_mode cipher_mode,
352 unsigned int blocksize)
353{
354 u32 max_payload = SPUM_NS2_MAX_PAYLOAD;
355 u32 excess;
356
357
358 if (cipher_mode == CIPHER_MODE_XTS)
359 max_payload -= SPU_XTS_TWEAK_SIZE;
360
361 excess = max_payload % blocksize;
362
363 return max_payload - excess;
364}
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379u32 spum_nsp_ctx_max_payload(enum spu_cipher_alg cipher_alg,
380 enum spu_cipher_mode cipher_mode,
381 unsigned int blocksize)
382{
383 u32 max_payload = SPUM_NSP_MAX_PAYLOAD;
384 u32 excess;
385
386
387 if (cipher_mode == CIPHER_MODE_XTS)
388 max_payload -= SPU_XTS_TWEAK_SIZE;
389
390 excess = max_payload % blocksize;
391
392 return max_payload - excess;
393}
394
395
396
397
398
399
400
401
402
403u32 spum_payload_length(u8 *spu_hdr)
404{
405 struct BD_HEADER *bd;
406 u32 pl_len;
407
408
409 bd = (struct BD_HEADER *)(spu_hdr + 8);
410 pl_len = be16_to_cpu(bd->size);
411
412 return pl_len;
413}
414
415
416
417
418
419
420
421
422
423
424u16 spum_response_hdr_len(u16 auth_key_len, u16 enc_key_len, bool is_hash)
425{
426 if (is_hash)
427 return SPU_HASH_RESP_HDR_LEN;
428 else
429 return SPU_RESP_HDR_LEN;
430}
431
432
433
434
435
436
437
438
439
440
441
442
443
444u16 spum_hash_pad_len(enum hash_alg hash_alg, enum hash_mode hash_mode,
445 u32 chunksize, u16 hash_block_size)
446{
447 unsigned int length_len;
448 unsigned int used_space_last_block;
449 int hash_pad_len;
450
451
452 if ((hash_alg == HASH_ALG_AES) && (hash_mode == HASH_MODE_XCBC)) {
453 used_space_last_block = chunksize % hash_block_size;
454 hash_pad_len = hash_block_size - used_space_last_block;
455 if (hash_pad_len >= hash_block_size)
456 hash_pad_len -= hash_block_size;
457 return hash_pad_len;
458 }
459
460 used_space_last_block = chunksize % hash_block_size + 1;
461 if ((hash_alg == HASH_ALG_SHA384) || (hash_alg == HASH_ALG_SHA512))
462 length_len = 2 * sizeof(u64);
463 else
464 length_len = sizeof(u64);
465
466 used_space_last_block += length_len;
467 hash_pad_len = hash_block_size - used_space_last_block;
468 if (hash_pad_len < 0)
469 hash_pad_len += hash_block_size;
470
471 hash_pad_len += 1 + length_len;
472 return hash_pad_len;
473}
474
475
476
477
478
479
480
481
482u32 spum_gcm_ccm_pad_len(enum spu_cipher_mode cipher_mode,
483 unsigned int data_size)
484{
485 u32 pad_len = 0;
486 u32 m1 = SPU_GCM_CCM_ALIGN - 1;
487
488 if ((cipher_mode == CIPHER_MODE_GCM) ||
489 (cipher_mode == CIPHER_MODE_CCM))
490 pad_len = ((data_size + m1) & ~m1) - data_size;
491
492 return pad_len;
493}
494
495
496
497
498
499
500
501
502
503
504
505u32 spum_assoc_resp_len(enum spu_cipher_mode cipher_mode,
506 unsigned int assoc_len, unsigned int iv_len,
507 bool is_encrypt)
508{
509 u32 buflen = 0;
510 u32 pad;
511
512 if (assoc_len)
513 buflen = assoc_len;
514
515 if (cipher_mode == CIPHER_MODE_GCM) {
516
517 pad = spum_gcm_ccm_pad_len(cipher_mode, buflen);
518 buflen += pad;
519 }
520 if (cipher_mode == CIPHER_MODE_CCM) {
521
522
523
524
525 pad = spum_gcm_ccm_pad_len(cipher_mode, buflen + 2);
526 buflen += pad;
527 }
528
529 return buflen;
530}
531
532
533
534
535
536
537
538
539
540
541
542
543u8 spum_aead_ivlen(enum spu_cipher_mode cipher_mode, u16 iv_len)
544{
545 return 0;
546}
547
548
549
550
551
552
553
554
555
556
557
558enum hash_type spum_hash_type(u32 src_sent)
559{
560 return src_sent ? HASH_TYPE_UPDT : HASH_TYPE_INIT;
561}
562
563
564
565
566
567
568
569
570
571
572
573
574u32 spum_digest_size(u32 alg_digest_size, enum hash_alg alg,
575 enum hash_type htype)
576{
577 u32 digestsize = alg_digest_size;
578
579
580
581
582 if ((htype == HASH_TYPE_INIT) || (htype == HASH_TYPE_UPDT)) {
583 if (alg == HASH_ALG_SHA224)
584 digestsize = SHA256_DIGEST_SIZE;
585 else if (alg == HASH_ALG_SHA384)
586 digestsize = SHA512_DIGEST_SIZE;
587 }
588 return digestsize;
589}
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606u32 spum_create_request(u8 *spu_hdr,
607 struct spu_request_opts *req_opts,
608 struct spu_cipher_parms *cipher_parms,
609 struct spu_hash_parms *hash_parms,
610 struct spu_aead_parms *aead_parms,
611 unsigned int data_size)
612{
613 struct SPUHEADER *spuh;
614 struct BDESC_HEADER *bdesc;
615 struct BD_HEADER *bd;
616
617 u8 *ptr;
618 u32 protocol_bits = 0;
619 u32 cipher_bits = 0;
620 u32 ecf_bits = 0;
621 u8 sctx_words = 0;
622 unsigned int buf_len = 0;
623
624
625 unsigned int cipher_len = hash_parms->prebuf_len + data_size +
626 hash_parms->pad_len;
627
628
629 unsigned int cipher_offset = aead_parms->assoc_size +
630 aead_parms->iv_len + aead_parms->aad_pad_len;
631
632
633 unsigned int real_db_size = spu_real_db_size(aead_parms->assoc_size,
634 aead_parms->iv_len,
635 hash_parms->prebuf_len,
636 data_size,
637 aead_parms->aad_pad_len,
638 aead_parms->data_pad_len,
639 hash_parms->pad_len);
640
641 unsigned int auth_offset = 0;
642 unsigned int offset_iv = 0;
643
644
645 unsigned int auth_len;
646
647 auth_len = real_db_size;
648
649 if (req_opts->is_aead && req_opts->is_inbound)
650 cipher_len -= hash_parms->digestsize;
651
652 if (req_opts->is_aead && req_opts->is_inbound)
653 auth_len -= hash_parms->digestsize;
654
655 if ((hash_parms->alg == HASH_ALG_AES) &&
656 (hash_parms->mode == HASH_MODE_XCBC)) {
657 auth_len -= hash_parms->pad_len;
658 cipher_len -= hash_parms->pad_len;
659 }
660
661 flow_log("%s()\n", __func__);
662 flow_log(" in:%u authFirst:%u\n",
663 req_opts->is_inbound, req_opts->auth_first);
664 flow_log(" %s. cipher alg:%u mode:%u type %u\n",
665 spu_alg_name(cipher_parms->alg, cipher_parms->mode),
666 cipher_parms->alg, cipher_parms->mode, cipher_parms->type);
667 flow_log(" key: %d\n", cipher_parms->key_len);
668 flow_dump(" key: ", cipher_parms->key_buf, cipher_parms->key_len);
669 flow_log(" iv: %d\n", cipher_parms->iv_len);
670 flow_dump(" iv: ", cipher_parms->iv_buf, cipher_parms->iv_len);
671 flow_log(" auth alg:%u mode:%u type %u\n",
672 hash_parms->alg, hash_parms->mode, hash_parms->type);
673 flow_log(" digestsize: %u\n", hash_parms->digestsize);
674 flow_log(" authkey: %d\n", hash_parms->key_len);
675 flow_dump(" authkey: ", hash_parms->key_buf, hash_parms->key_len);
676 flow_log(" assoc_size:%u\n", aead_parms->assoc_size);
677 flow_log(" prebuf_len:%u\n", hash_parms->prebuf_len);
678 flow_log(" data_size:%u\n", data_size);
679 flow_log(" hash_pad_len:%u\n", hash_parms->pad_len);
680 flow_log(" real_db_size:%u\n", real_db_size);
681 flow_log(" auth_offset:%u auth_len:%u cipher_offset:%u cipher_len:%u\n",
682 auth_offset, auth_len, cipher_offset, cipher_len);
683 flow_log(" aead_iv: %u\n", aead_parms->iv_len);
684
685
686 ptr = spu_hdr;
687 memset(ptr, 0, sizeof(struct SPUHEADER));
688
689
690
691 spuh = (struct SPUHEADER *)ptr;
692 ptr += sizeof(struct SPUHEADER);
693 buf_len += sizeof(struct SPUHEADER);
694
695 spuh->mh.op_code = SPU_CRYPTO_OPERATION_GENERIC;
696 spuh->mh.flags |= (MH_SCTX_PRES | MH_BDESC_PRES | MH_BD_PRES);
697
698
699 sctx_words = 3;
700
701
702 if (req_opts->is_inbound)
703 cipher_bits |= CIPHER_INBOUND;
704 if (req_opts->auth_first)
705 cipher_bits |= CIPHER_ORDER;
706
707
708 cipher_bits |= cipher_parms->alg << CIPHER_ALG_SHIFT;
709 cipher_bits |= cipher_parms->mode << CIPHER_MODE_SHIFT;
710 cipher_bits |= cipher_parms->type << CIPHER_TYPE_SHIFT;
711
712
713 cipher_bits |= hash_parms->alg << HASH_ALG_SHIFT;
714 cipher_bits |= hash_parms->mode << HASH_MODE_SHIFT;
715 cipher_bits |= hash_parms->type << HASH_TYPE_SHIFT;
716
717
718
719
720
721 if (hash_parms->alg) {
722
723 if (hash_parms->key_len) {
724 memcpy(ptr, hash_parms->key_buf, hash_parms->key_len);
725 ptr += hash_parms->key_len;
726 buf_len += hash_parms->key_len;
727 sctx_words += hash_parms->key_len / 4;
728 }
729
730 if ((cipher_parms->mode == CIPHER_MODE_GCM) ||
731 (cipher_parms->mode == CIPHER_MODE_CCM))
732
733 offset_iv = aead_parms->assoc_size;
734
735
736 if (!req_opts->is_inbound) {
737 if ((cipher_parms->mode == CIPHER_MODE_GCM) ||
738 (cipher_parms->mode == CIPHER_MODE_CCM))
739 ecf_bits |= 1 << INSERT_ICV_SHIFT;
740 } else {
741 ecf_bits |= CHECK_ICV;
742 }
743
744
745 if (hash_parms->digestsize == 64)
746 cipher_bits |= ICV_IS_512;
747 else
748 ecf_bits |=
749 (hash_parms->digestsize / 4) << ICV_SIZE_SHIFT;
750 }
751
752 if (req_opts->bd_suppress)
753 ecf_bits |= BD_SUPPRESS;
754
755
756 if (cipher_parms->alg) {
757 if (cipher_parms->key_len) {
758 memcpy(ptr, cipher_parms->key_buf,
759 cipher_parms->key_len);
760 ptr += cipher_parms->key_len;
761 buf_len += cipher_parms->key_len;
762 sctx_words += cipher_parms->key_len / 4;
763 }
764
765
766
767
768
769 if (cipher_parms->iv_buf && cipher_parms->iv_len) {
770
771 ecf_bits |= SCTX_IV;
772
773
774 memcpy(ptr, cipher_parms->iv_buf, cipher_parms->iv_len);
775
776 ptr += cipher_parms->iv_len;
777 buf_len += cipher_parms->iv_len;
778 sctx_words += cipher_parms->iv_len / 4;
779 }
780 }
781
782
783
784
785
786 if (req_opts->is_rfc4543) {
787 if (req_opts->is_inbound)
788 data_size -= hash_parms->digestsize;
789 offset_iv = aead_parms->assoc_size + data_size;
790 cipher_len = 0;
791 cipher_offset = offset_iv;
792 auth_len = cipher_offset + aead_parms->data_pad_len;
793 }
794
795
796 protocol_bits |= sctx_words;
797
798
799 spuh->sa.proto_flags = cpu_to_be32(protocol_bits);
800 spuh->sa.cipher_flags = cpu_to_be32(cipher_bits);
801 spuh->sa.ecf = cpu_to_be32(ecf_bits);
802
803
804 bdesc = (struct BDESC_HEADER *)ptr;
805
806 bdesc->offset_mac = cpu_to_be16(auth_offset);
807 bdesc->length_mac = cpu_to_be16(auth_len);
808 bdesc->offset_crypto = cpu_to_be16(cipher_offset);
809 bdesc->length_crypto = cpu_to_be16(cipher_len);
810
811
812
813
814
815 if (cipher_parms->mode == CIPHER_MODE_CCM)
816 auth_len += spum_wordalign_padlen(auth_len);
817
818 bdesc->offset_icv = cpu_to_be16(auth_len);
819 bdesc->offset_iv = cpu_to_be16(offset_iv);
820
821 ptr += sizeof(struct BDESC_HEADER);
822 buf_len += sizeof(struct BDESC_HEADER);
823
824
825
826
827
828
829 bd = (struct BD_HEADER *)ptr;
830 bd->size = cpu_to_be16(real_db_size);
831 bd->prev_length = 0;
832
833 ptr += sizeof(struct BD_HEADER);
834 buf_len += sizeof(struct BD_HEADER);
835
836 packet_dump(" SPU request header: ", spu_hdr, buf_len);
837
838 return buf_len;
839}
840
841
842
843
844
845
846
847
848
849
850
851
852u16 spum_cipher_req_init(u8 *spu_hdr, struct spu_cipher_parms *cipher_parms)
853{
854 struct SPUHEADER *spuh;
855 u32 protocol_bits = 0;
856 u32 cipher_bits = 0;
857 u32 ecf_bits = 0;
858 u8 sctx_words = 0;
859 u8 *ptr = spu_hdr;
860
861 flow_log("%s()\n", __func__);
862 flow_log(" cipher alg:%u mode:%u type %u\n", cipher_parms->alg,
863 cipher_parms->mode, cipher_parms->type);
864 flow_log(" cipher_iv_len: %u\n", cipher_parms->iv_len);
865 flow_log(" key: %d\n", cipher_parms->key_len);
866 flow_dump(" key: ", cipher_parms->key_buf, cipher_parms->key_len);
867
868
869 memset(spu_hdr, 0, sizeof(struct SPUHEADER));
870 ptr += sizeof(struct SPUHEADER);
871
872
873
874 spuh = (struct SPUHEADER *)spu_hdr;
875
876 spuh->mh.op_code = SPU_CRYPTO_OPERATION_GENERIC;
877 spuh->mh.flags |= (MH_SCTX_PRES | MH_BDESC_PRES | MH_BD_PRES);
878
879
880 sctx_words = 3;
881
882
883 if (cipher_parms->alg) {
884 if (cipher_parms->key_len) {
885 ptr += cipher_parms->key_len;
886 sctx_words += cipher_parms->key_len / 4;
887 }
888
889
890
891
892
893 if (cipher_parms->iv_len) {
894
895 ecf_bits |= SCTX_IV;
896 ptr += cipher_parms->iv_len;
897 sctx_words += cipher_parms->iv_len / 4;
898 }
899 }
900
901
902 cipher_bits |= cipher_parms->alg << CIPHER_ALG_SHIFT;
903 cipher_bits |= cipher_parms->mode << CIPHER_MODE_SHIFT;
904 cipher_bits |= cipher_parms->type << CIPHER_TYPE_SHIFT;
905
906
907 if (cipher_parms->alg && cipher_parms->key_len)
908 memcpy(spuh + 1, cipher_parms->key_buf, cipher_parms->key_len);
909
910
911 protocol_bits |= sctx_words;
912
913
914 spuh->sa.proto_flags = cpu_to_be32(protocol_bits);
915
916
917 spuh->sa.cipher_flags = cpu_to_be32(cipher_bits);
918 spuh->sa.ecf = cpu_to_be32(ecf_bits);
919
920 packet_dump(" SPU request header: ", spu_hdr,
921 sizeof(struct SPUHEADER));
922
923 return sizeof(struct SPUHEADER) + cipher_parms->key_len +
924 cipher_parms->iv_len + sizeof(struct BDESC_HEADER) +
925 sizeof(struct BD_HEADER);
926}
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946void spum_cipher_req_finish(u8 *spu_hdr,
947 u16 spu_req_hdr_len,
948 unsigned int is_inbound,
949 struct spu_cipher_parms *cipher_parms,
950 bool update_key,
951 unsigned int data_size)
952{
953 struct SPUHEADER *spuh;
954 struct BDESC_HEADER *bdesc;
955 struct BD_HEADER *bd;
956 u8 *bdesc_ptr = spu_hdr + spu_req_hdr_len -
957 (sizeof(struct BD_HEADER) + sizeof(struct BDESC_HEADER));
958
959 u32 cipher_bits;
960
961 flow_log("%s()\n", __func__);
962 flow_log(" in: %u\n", is_inbound);
963 flow_log(" cipher alg: %u, cipher_type: %u\n", cipher_parms->alg,
964 cipher_parms->type);
965 if (update_key) {
966 flow_log(" cipher key len: %u\n", cipher_parms->key_len);
967 flow_dump(" key: ", cipher_parms->key_buf,
968 cipher_parms->key_len);
969 }
970
971
972
973
974
975
976
977
978 if (cipher_parms->mode == CIPHER_MODE_XTS)
979 memset(cipher_parms->iv_buf, 0, cipher_parms->iv_len);
980
981 flow_log(" iv len: %d\n", cipher_parms->iv_len);
982 flow_dump(" iv: ", cipher_parms->iv_buf, cipher_parms->iv_len);
983 flow_log(" data_size: %u\n", data_size);
984
985
986
987 spuh = (struct SPUHEADER *)spu_hdr;
988
989
990 cipher_bits = be32_to_cpu(spuh->sa.cipher_flags);
991
992
993 if (is_inbound)
994 cipher_bits |= CIPHER_INBOUND;
995 else
996 cipher_bits &= ~CIPHER_INBOUND;
997
998
999 if (update_key) {
1000 spuh->sa.cipher_flags |=
1001 cipher_parms->type << CIPHER_TYPE_SHIFT;
1002 memcpy(spuh + 1, cipher_parms->key_buf, cipher_parms->key_len);
1003 }
1004
1005 if (cipher_parms->alg && cipher_parms->iv_buf && cipher_parms->iv_len)
1006
1007 memcpy(bdesc_ptr - cipher_parms->iv_len, cipher_parms->iv_buf,
1008 cipher_parms->iv_len);
1009
1010 spuh->sa.cipher_flags = cpu_to_be32(cipher_bits);
1011
1012
1013 bdesc = (struct BDESC_HEADER *)bdesc_ptr;
1014 bdesc->offset_mac = 0;
1015 bdesc->length_mac = 0;
1016 bdesc->offset_crypto = 0;
1017
1018
1019 if (cipher_parms->mode == CIPHER_MODE_XTS)
1020 bdesc->length_crypto = cpu_to_be16(data_size +
1021 SPU_XTS_TWEAK_SIZE);
1022 else
1023 bdesc->length_crypto = cpu_to_be16(data_size);
1024
1025 bdesc->offset_icv = 0;
1026 bdesc->offset_iv = 0;
1027
1028
1029
1030
1031
1032 bd = (struct BD_HEADER *)(bdesc_ptr + sizeof(struct BDESC_HEADER));
1033 bd->size = cpu_to_be16(data_size);
1034
1035
1036 if (cipher_parms->mode == CIPHER_MODE_XTS)
1037 bd->size = cpu_to_be16(data_size + SPU_XTS_TWEAK_SIZE);
1038 else
1039 bd->size = cpu_to_be16(data_size);
1040
1041 bd->prev_length = 0;
1042
1043 packet_dump(" SPU request header: ", spu_hdr, spu_req_hdr_len);
1044}
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062void spum_request_pad(u8 *pad_start,
1063 u32 gcm_ccm_padding,
1064 u32 hash_pad_len,
1065 enum hash_alg auth_alg,
1066 enum hash_mode auth_mode,
1067 unsigned int total_sent, u32 status_padding)
1068{
1069 u8 *ptr = pad_start;
1070
1071
1072 if (gcm_ccm_padding > 0) {
1073 flow_log(" GCM: padding to 16 byte alignment: %u bytes\n",
1074 gcm_ccm_padding);
1075 memset(ptr, 0, gcm_ccm_padding);
1076 ptr += gcm_ccm_padding;
1077 }
1078
1079 if (hash_pad_len > 0) {
1080
1081 memset(ptr, 0, hash_pad_len);
1082
1083 if ((auth_alg == HASH_ALG_AES) &&
1084 (auth_mode == HASH_MODE_XCBC)) {
1085
1086 ptr += hash_pad_len;
1087 } else {
1088
1089 *ptr = 0x80;
1090 ptr += (hash_pad_len - sizeof(u64));
1091
1092
1093 if (auth_alg == HASH_ALG_MD5)
1094 *(u64 *)ptr = cpu_to_le64((u64)total_sent * 8);
1095 else
1096 *(u64 *)ptr = cpu_to_be64((u64)total_sent * 8);
1097 ptr += sizeof(u64);
1098 }
1099 }
1100
1101
1102 if (status_padding > 0) {
1103 flow_log(" STAT: padding to 4 byte alignment: %u bytes\n",
1104 status_padding);
1105
1106 memset(ptr, 0, status_padding);
1107 ptr += status_padding;
1108 }
1109}
1110
1111
1112
1113
1114
1115
1116
1117u8 spum_xts_tweak_in_payload(void)
1118{
1119 return 1;
1120}
1121
1122
1123
1124
1125
1126
1127
1128u8 spum_tx_status_len(void)
1129{
1130 return SPU_TX_STATUS_LEN;
1131}
1132
1133
1134
1135
1136
1137
1138
1139u8 spum_rx_status_len(void)
1140{
1141 return SPU_RX_STATUS_LEN;
1142}
1143
1144
1145
1146
1147
1148
1149
1150
1151int spum_status_process(u8 *statp)
1152{
1153 u32 status;
1154
1155 status = __be32_to_cpu(*(__be32 *)statp);
1156 flow_log("SPU response STATUS %#08x\n", status);
1157 if (status & SPU_STATUS_ERROR_FLAG) {
1158 pr_err("%s() Warning: Error result from SPU: %#08x\n",
1159 __func__, status);
1160 if (status & SPU_STATUS_INVALID_ICV)
1161 return SPU_INVALID_ICV;
1162 return -EBADMSG;
1163 }
1164 return 0;
1165}
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178void spum_ccm_update_iv(unsigned int digestsize,
1179 struct spu_cipher_parms *cipher_parms,
1180 unsigned int assoclen,
1181 unsigned int chunksize,
1182 bool is_encrypt,
1183 bool is_esp)
1184{
1185 u8 L;
1186 u8 mprime;
1187 u8 adata;
1188
1189 if (cipher_parms->iv_len != CCM_AES_IV_SIZE) {
1190 pr_err("%s(): Invalid IV len %d for CCM mode, should be %d\n",
1191 __func__, cipher_parms->iv_len, CCM_AES_IV_SIZE);
1192 return;
1193 }
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216 if (is_esp) {
1217 L = CCM_ESP_L_VALUE;
1218 } else {
1219
1220 L = ((cipher_parms->iv_buf[0] & CCM_B0_L_PRIME) >>
1221 CCM_B0_L_PRIME_SHIFT) + 1;
1222 }
1223
1224 mprime = (digestsize - 2) >> 1;
1225 adata = (assoclen > 0);
1226
1227 cipher_parms->iv_buf[0] = (adata << CCM_B0_ADATA_SHIFT) |
1228 (mprime << CCM_B0_M_PRIME_SHIFT) |
1229 ((L - 1) << CCM_B0_L_PRIME_SHIFT);
1230
1231
1232
1233
1234 if (!is_encrypt)
1235 chunksize -= digestsize;
1236
1237
1238 format_value_ccm(chunksize, &cipher_parms->iv_buf[15 - L + 1], L);
1239}
1240
1241
1242
1243
1244
1245
1246
1247
1248u32 spum_wordalign_padlen(u32 data_size)
1249{
1250 return ((data_size + 3) & ~3) - data_size;
1251}
1252