2 * Copyright 2016 Broadcom
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2, as
6 * published by the Free Software Foundation (the "GPL").
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License version 2 (GPLv2) for more details.
13 * You should have received a copy of the GNU General Public License
14 * version 2 (GPLv2) along with this source code.
17 #include <linux/kernel.h>
18 #include <linux/string.h>
25 char *hash_alg_name[] = { "None", "md5", "sha1", "sha224", "sha256", "aes",
26 "sha384", "sha512", "sha3_224", "sha3_256", "sha3_384", "sha3_512" };
28 char *aead_alg_name[] = { "ccm(aes)", "gcm(aes)", "authenc" };
30 /* Assumes SPU-M messages are in big endian */
31 void spum_dump_msg_hdr(u8 *buf, unsigned int buf_len)
34 struct SPUHEADER *spuh = (struct SPUHEADER *)buf;
35 unsigned int hash_key_len = 0;
36 unsigned int hash_state_len = 0;
37 unsigned int cipher_key_len = 0;
48 u32 sctx_size; /* SCTX length in words */
49 u32 sctx_pl_len; /* SCTX payload length in bytes */
52 packet_log("SPU Message header %p len: %u\n", buf, buf_len);
54 /* ========== Decode MH ========== */
55 packet_log(" MH 0x%08x\n", be32_to_cpu(*((u32 *)ptr)));
56 if (spuh->mh.flags & MH_SCTX_PRES)
57 packet_log(" SCTX present\n");
58 if (spuh->mh.flags & MH_BDESC_PRES)
59 packet_log(" BDESC present\n");
60 if (spuh->mh.flags & MH_MFM_PRES)
61 packet_log(" MFM present\n");
62 if (spuh->mh.flags & MH_BD_PRES)
63 packet_log(" BD present\n");
64 if (spuh->mh.flags & MH_HASH_PRES)
65 packet_log(" HASH present\n");
66 if (spuh->mh.flags & MH_SUPDT_PRES)
67 packet_log(" SUPDT present\n");
68 packet_log(" Opcode 0x%02x\n", spuh->mh.op_code);
70 ptr += sizeof(spuh->mh) + sizeof(spuh->emh); /* skip emh. unused */
72 /* ========== Decode SCTX ========== */
73 if (spuh->mh.flags & MH_SCTX_PRES) {
74 pflags = be32_to_cpu(spuh->sa.proto_flags);
75 packet_log(" SCTX[0] 0x%08x\n", pflags);
76 sctx_size = pflags & SCTX_SIZE;
77 packet_log(" Size %u words\n", sctx_size);
79 cflags = be32_to_cpu(spuh->sa.cipher_flags);
80 packet_log(" SCTX[1] 0x%08x\n", cflags);
81 packet_log(" Inbound:%lu (1:decrypt/vrfy 0:encrypt/auth)\n",
82 (cflags & CIPHER_INBOUND) >> CIPHER_INBOUND_SHIFT);
83 packet_log(" Order:%lu (1:AuthFirst 0:EncFirst)\n",
84 (cflags & CIPHER_ORDER) >> CIPHER_ORDER_SHIFT);
85 packet_log(" ICV_IS_512:%lx\n",
86 (cflags & ICV_IS_512) >> ICV_IS_512_SHIFT);
87 cipher_alg = (cflags & CIPHER_ALG) >> CIPHER_ALG_SHIFT;
88 cipher_mode = (cflags & CIPHER_MODE) >> CIPHER_MODE_SHIFT;
89 cipher_type = (cflags & CIPHER_TYPE) >> CIPHER_TYPE_SHIFT;
90 packet_log(" Crypto Alg:%u Mode:%u Type:%u\n",
91 cipher_alg, cipher_mode, cipher_type);
92 hash_alg = (cflags & HASH_ALG) >> HASH_ALG_SHIFT;
93 hash_mode = (cflags & HASH_MODE) >> HASH_MODE_SHIFT;
94 hash_type = (cflags & HASH_TYPE) >> HASH_TYPE_SHIFT;
95 packet_log(" Hash Alg:%x Mode:%x Type:%x\n",
96 hash_alg, hash_mode, hash_type);
97 packet_log(" UPDT_Offset:%u\n", cflags & UPDT_OFST);
99 ecf = be32_to_cpu(spuh->sa.ecf);
100 packet_log(" SCTX[2] 0x%08x\n", ecf);
101 packet_log(" WriteICV:%lu CheckICV:%lu ICV_SIZE:%u ",
102 (ecf & INSERT_ICV) >> INSERT_ICV_SHIFT,
103 (ecf & CHECK_ICV) >> CHECK_ICV_SHIFT,
104 (ecf & ICV_SIZE) >> ICV_SIZE_SHIFT);
105 packet_log("BD_SUPPRESS:%lu\n",
106 (ecf & BD_SUPPRESS) >> BD_SUPPRESS_SHIFT);
107 packet_log(" SCTX_IV:%lu ExplicitIV:%lu GenIV:%lu ",
108 (ecf & SCTX_IV) >> SCTX_IV_SHIFT,
109 (ecf & EXPLICIT_IV) >> EXPLICIT_IV_SHIFT,
110 (ecf & GEN_IV) >> GEN_IV_SHIFT);
111 packet_log("IV_OV_OFST:%lu EXP_IV_SIZE:%u\n",
112 (ecf & IV_OFFSET) >> IV_OFFSET_SHIFT,
115 ptr += sizeof(struct SCTX);
117 if (hash_alg && hash_mode) {
129 case HASH_ALG_SHA224:
133 case HASH_ALG_SHA256:
137 case HASH_ALG_SHA384:
141 case HASH_ALG_SHA512:
153 packet_log(" Auth Key Type:%s Length:%u Bytes\n",
155 packet_dump(" KEY: ", ptr, hash_key_len);
157 } else if ((hash_alg == HASH_ALG_AES) &&
158 (hash_mode == HASH_MODE_XCBC)) {
161 switch (cipher_type) {
162 case CIPHER_TYPE_AES128:
164 name = "AES128-XCBC";
166 case CIPHER_TYPE_AES192:
168 name = "AES192-XCBC";
170 case CIPHER_TYPE_AES256:
172 name = "AES256-XCBC";
175 packet_log(" Auth Key Type:%s Length:%u Bytes\n",
177 packet_dump(" KEY: ", ptr, hash_key_len);
181 if (hash_alg && (hash_mode == HASH_MODE_NONE) &&
182 (hash_type == HASH_TYPE_UPDT)) {
194 case HASH_ALG_SHA224:
198 case HASH_ALG_SHA256:
202 case HASH_ALG_SHA384:
206 case HASH_ALG_SHA512:
218 packet_log(" Auth State Type:%s Length:%u Bytes\n",
219 name, hash_state_len);
220 packet_dump(" State: ", ptr, hash_state_len);
221 ptr += hash_state_len;
227 switch (cipher_alg) {
232 case CIPHER_ALG_3DES:
237 cipher_key_len = 260;
241 switch (cipher_type) {
242 case CIPHER_TYPE_AES128:
246 case CIPHER_TYPE_AES192:
250 case CIPHER_TYPE_AES256:
256 case CIPHER_ALG_NONE:
260 packet_log(" Cipher Key Type:%s Length:%u Bytes\n",
261 name, cipher_key_len);
263 /* XTS has two keys */
264 if (cipher_mode == CIPHER_MODE_XTS) {
265 packet_dump(" KEY2: ", ptr, cipher_key_len);
266 ptr += cipher_key_len;
267 packet_dump(" KEY1: ", ptr, cipher_key_len);
268 ptr += cipher_key_len;
272 packet_dump(" KEY: ", ptr, cipher_key_len);
273 ptr += cipher_key_len;
277 sctx_pl_len = sctx_size * sizeof(u32) -
279 iv_len = sctx_pl_len -
280 (hash_key_len + hash_state_len +
282 packet_log(" IV Length:%u Bytes\n", iv_len);
283 packet_dump(" IV: ", ptr, iv_len);
289 /* ========== Decode BDESC ========== */
290 if (spuh->mh.flags & MH_BDESC_PRES) {
292 struct BDESC_HEADER *bdesc = (struct BDESC_HEADER *)ptr;
294 packet_log(" BDESC[0] 0x%08x\n", be32_to_cpu(*((u32 *)ptr)));
295 packet_log(" OffsetMAC:%u LengthMAC:%u\n",
296 be16_to_cpu(bdesc->offset_mac),
297 be16_to_cpu(bdesc->length_mac));
300 packet_log(" BDESC[1] 0x%08x\n", be32_to_cpu(*((u32 *)ptr)));
301 packet_log(" OffsetCrypto:%u LengthCrypto:%u\n",
302 be16_to_cpu(bdesc->offset_crypto),
303 be16_to_cpu(bdesc->length_crypto));
306 packet_log(" BDESC[2] 0x%08x\n", be32_to_cpu(*((u32 *)ptr)));
307 packet_log(" OffsetICV:%u OffsetIV:%u\n",
308 be16_to_cpu(bdesc->offset_icv),
309 be16_to_cpu(bdesc->offset_iv));
313 /* ========== Decode BD ========== */
314 if (spuh->mh.flags & MH_BD_PRES) {
316 struct BD_HEADER *bd = (struct BD_HEADER *)ptr;
318 packet_log(" BD[0] 0x%08x\n", be32_to_cpu(*((u32 *)ptr)));
319 packet_log(" Size:%ubytes PrevLength:%u\n",
320 be16_to_cpu(bd->size), be16_to_cpu(bd->prev_length));
324 /* Double check sanity */
325 if (buf + buf_len != ptr) {
326 packet_log(" Packet parsed incorrectly. ");
327 packet_log("buf:%p buf_len:%u buf+buf_len:%p ptr:%p\n",
328 buf, buf_len, buf + buf_len, ptr);
335 * spum_ns2_ctx_max_payload() - Determine the max length of the payload for a
336 * SPU message for a given cipher and hash alg context.
337 * @cipher_alg: The cipher algorithm
338 * @cipher_mode: The cipher mode
339 * @blocksize: The size of a block of data for this algo
341 * The max payload must be a multiple of the blocksize so that if a request is
342 * too large to fit in a single SPU message, the request can be broken into
343 * max_payload sized chunks. Each chunk must be a multiple of blocksize.
345 * Return: Max payload length in bytes
347 u32 spum_ns2_ctx_max_payload(enum spu_cipher_alg cipher_alg,
348 enum spu_cipher_mode cipher_mode,
349 unsigned int blocksize)
351 u32 max_payload = SPUM_NS2_MAX_PAYLOAD;
354 /* In XTS on SPU-M, we'll need to insert tweak before input data */
355 if (cipher_mode == CIPHER_MODE_XTS)
356 max_payload -= SPU_XTS_TWEAK_SIZE;
358 excess = max_payload % blocksize;
360 return max_payload - excess;
364 * spum_nsp_ctx_max_payload() - Determine the max length of the payload for a
365 * SPU message for a given cipher and hash alg context.
366 * @cipher_alg: The cipher algorithm
367 * @cipher_mode: The cipher mode
368 * @blocksize: The size of a block of data for this algo
370 * The max payload must be a multiple of the blocksize so that if a request is
371 * too large to fit in a single SPU message, the request can be broken into
372 * max_payload sized chunks. Each chunk must be a multiple of blocksize.
374 * Return: Max payload length in bytes
376 u32 spum_nsp_ctx_max_payload(enum spu_cipher_alg cipher_alg,
377 enum spu_cipher_mode cipher_mode,
378 unsigned int blocksize)
380 u32 max_payload = SPUM_NSP_MAX_PAYLOAD;
383 /* In XTS on SPU-M, we'll need to insert tweak before input data */
384 if (cipher_mode == CIPHER_MODE_XTS)
385 max_payload -= SPU_XTS_TWEAK_SIZE;
387 excess = max_payload % blocksize;
389 return max_payload - excess;
392 /** spum_payload_length() - Given a SPU-M message header, extract the payload
394 * @spu_hdr: Start of SPU header
396 * Assumes just MH, EMH, BD (no SCTX, BDESC. Works for response frames.
398 * Return: payload length in bytes
400 u32 spum_payload_length(u8 *spu_hdr)
402 struct BD_HEADER *bd;
405 /* Find BD header. skip MH, EMH */
406 bd = (struct BD_HEADER *)(spu_hdr + 8);
407 pl_len = be16_to_cpu(bd->size);
413 * spum_response_hdr_len() - Given the length of the hash key and encryption
414 * key, determine the expected length of a SPU response header.
415 * @auth_key_len: authentication key length (bytes)
416 * @enc_key_len: encryption key length (bytes)
417 * @is_hash: true if response message is for a hash operation
419 * Return: length of SPU response header (bytes)
421 u16 spum_response_hdr_len(u16 auth_key_len, u16 enc_key_len, bool is_hash)
424 return SPU_HASH_RESP_HDR_LEN;
426 return SPU_RESP_HDR_LEN;
430 * spum_hash_pad_len() - Calculate the length of hash padding required to extend
431 * data to a full block size.
432 * @hash_alg: hash algorithm
433 * @hash_mode: hash mode
434 * @chunksize: length of data, in bytes
435 * @hash_block_size: size of a block of data for hash algorithm
437 * Reserve space for 1 byte (0x80) start of pad and the total length as u64
439 * Return: length of hash pad in bytes
441 u16 spum_hash_pad_len(enum hash_alg hash_alg, enum hash_mode hash_mode,
442 u32 chunksize, u16 hash_block_size)
444 unsigned int length_len;
445 unsigned int used_space_last_block;
448 /* AES-XCBC hash requires just padding to next block boundary */
449 if ((hash_alg == HASH_ALG_AES) && (hash_mode == HASH_MODE_XCBC)) {
450 used_space_last_block = chunksize % hash_block_size;
451 hash_pad_len = hash_block_size - used_space_last_block;
452 if (hash_pad_len >= hash_block_size)
453 hash_pad_len -= hash_block_size;
457 used_space_last_block = chunksize % hash_block_size + 1;
458 if ((hash_alg == HASH_ALG_SHA384) || (hash_alg == HASH_ALG_SHA512))
459 length_len = 2 * sizeof(u64);
461 length_len = sizeof(u64);
463 used_space_last_block += length_len;
464 hash_pad_len = hash_block_size - used_space_last_block;
465 if (hash_pad_len < 0)
466 hash_pad_len += hash_block_size;
468 hash_pad_len += 1 + length_len;
473 * spum_gcm_ccm_pad_len() - Determine the required length of GCM or CCM padding.
474 * @cipher_mode: Algo type
475 * @data_size: Length of plaintext (bytes)
477 * @Return: Length of padding, in bytes
479 u32 spum_gcm_ccm_pad_len(enum spu_cipher_mode cipher_mode,
480 unsigned int data_size)
483 u32 m1 = SPU_GCM_CCM_ALIGN - 1;
485 if ((cipher_mode == CIPHER_MODE_GCM) ||
486 (cipher_mode == CIPHER_MODE_CCM))
487 pad_len = ((data_size + m1) & ~m1) - data_size;
493 * spum_assoc_resp_len() - Determine the size of the receive buffer required to
494 * catch associated data.
495 * @cipher_mode: cipher mode
496 * @assoc_len: length of associated data (bytes)
497 * @iv_len: length of IV (bytes)
498 * @is_encrypt: true if encrypting. false if decrypting.
500 * Return: length of associated data in response message (bytes)
502 u32 spum_assoc_resp_len(enum spu_cipher_mode cipher_mode,
503 unsigned int assoc_len, unsigned int iv_len,
512 if (cipher_mode == CIPHER_MODE_GCM) {
513 /* AAD needs to be padded in responses too */
514 pad = spum_gcm_ccm_pad_len(cipher_mode, buflen);
517 if (cipher_mode == CIPHER_MODE_CCM) {
519 * AAD needs to be padded in responses too
520 * for CCM, len + 2 needs to be 128-bit aligned.
522 pad = spum_gcm_ccm_pad_len(cipher_mode, buflen + 2);
530 * spu_aead_ivlen() - Calculate the length of the AEAD IV to be included
531 * in a SPU request after the AAD and before the payload.
532 * @cipher_mode: cipher mode
533 * @iv_ctr_len: initialization vector length in bytes
535 * In Linux ~4.2 and later, the assoc_data sg includes the IV. So no need
536 * to include the IV as a separate field in the SPU request msg.
538 * Return: Length of AEAD IV in bytes
540 u8 spum_aead_ivlen(enum spu_cipher_mode cipher_mode, u16 iv_len)
546 * spum_hash_type() - Determine the type of hash operation.
547 * @src_sent: The number of bytes in the current request that have already
548 * been sent to the SPU to be hashed.
550 * We do not use HASH_TYPE_FULL for requests that fit in a single SPU message.
551 * Using FULL causes failures (such as when the string to be hashed is empty).
552 * For similar reasons, we never use HASH_TYPE_FIN. Instead, submit messages
553 * as INIT or UPDT and do the hash padding in sw.
555 enum hash_type spum_hash_type(u32 src_sent)
557 return src_sent ? HASH_TYPE_UPDT : HASH_TYPE_INIT;
561 * spum_digest_size() - Determine the size of a hash digest to expect the SPU to
563 * alg_digest_size: Number of bytes in the final digest for the given algo
564 * alg: The hash algorithm
565 * htype: Type of hash operation (init, update, full, etc)
567 * When doing incremental hashing for an algorithm with a truncated hash
568 * (e.g., SHA224), the SPU returns the full digest so that it can be fed back as
569 * a partial result for the next chunk.
571 u32 spum_digest_size(u32 alg_digest_size, enum hash_alg alg,
572 enum hash_type htype)
574 u32 digestsize = alg_digest_size;
576 /* SPU returns complete digest when doing incremental hash and truncated
579 if ((htype == HASH_TYPE_INIT) || (htype == HASH_TYPE_UPDT)) {
580 if (alg == HASH_ALG_SHA224)
581 digestsize = SHA256_DIGEST_SIZE;
582 else if (alg == HASH_ALG_SHA384)
583 digestsize = SHA512_DIGEST_SIZE;
589 * spum_create_request() - Build a SPU request message header, up to and
590 * including the BD header. Construct the message starting at spu_hdr. Caller
591 * should allocate this buffer in DMA-able memory at least SPU_HEADER_ALLOC_LEN
593 * @spu_hdr: Start of buffer where SPU request header is to be written
594 * @req_opts: SPU request message options
595 * @cipher_parms: Parameters related to cipher algorithm
596 * @hash_parms: Parameters related to hash algorithm
597 * @aead_parms: Parameters related to AEAD operation
598 * @data_size: Length of data to be encrypted or authenticated. If AEAD, does
599 * not include length of AAD.
601 * Return: the length of the SPU header in bytes. 0 if an error occurs.
603 u32 spum_create_request(u8 *spu_hdr,
604 struct spu_request_opts *req_opts,
605 struct spu_cipher_parms *cipher_parms,
606 struct spu_hash_parms *hash_parms,
607 struct spu_aead_parms *aead_parms,
608 unsigned int data_size)
610 struct SPUHEADER *spuh;
611 struct BDESC_HEADER *bdesc;
612 struct BD_HEADER *bd;
615 u32 protocol_bits = 0;
619 unsigned int buf_len = 0;
621 /* size of the cipher payload */
622 unsigned int cipher_len = hash_parms->prebuf_len + data_size +
625 /* offset of prebuf or data from end of BD header */
626 unsigned int cipher_offset = aead_parms->assoc_size +
627 aead_parms->iv_len + aead_parms->aad_pad_len;
629 /* total size of the DB data (without STAT word padding) */
630 unsigned int real_db_size = spu_real_db_size(aead_parms->assoc_size,
632 hash_parms->prebuf_len,
634 aead_parms->aad_pad_len,
635 aead_parms->data_pad_len,
636 hash_parms->pad_len);
638 unsigned int auth_offset = 0;
639 unsigned int offset_iv = 0;
641 /* size/offset of the auth payload */
642 unsigned int auth_len;
644 auth_len = real_db_size;
646 if (req_opts->is_aead && req_opts->is_inbound)
647 cipher_len -= hash_parms->digestsize;
649 if (req_opts->is_aead && req_opts->is_inbound)
650 auth_len -= hash_parms->digestsize;
652 if ((hash_parms->alg == HASH_ALG_AES) &&
653 (hash_parms->mode == HASH_MODE_XCBC)) {
654 auth_len -= hash_parms->pad_len;
655 cipher_len -= hash_parms->pad_len;
658 flow_log("%s()\n", __func__);
659 flow_log(" in:%u authFirst:%u\n",
660 req_opts->is_inbound, req_opts->auth_first);
661 flow_log(" %s. cipher alg:%u mode:%u type %u\n",
662 spu_alg_name(cipher_parms->alg, cipher_parms->mode),
663 cipher_parms->alg, cipher_parms->mode, cipher_parms->type);
664 flow_log(" key: %d\n", cipher_parms->key_len);
665 flow_dump(" key: ", cipher_parms->key_buf, cipher_parms->key_len);
666 flow_log(" iv: %d\n", cipher_parms->iv_len);
667 flow_dump(" iv: ", cipher_parms->iv_buf, cipher_parms->iv_len);
668 flow_log(" auth alg:%u mode:%u type %u\n",
669 hash_parms->alg, hash_parms->mode, hash_parms->type);
670 flow_log(" digestsize: %u\n", hash_parms->digestsize);
671 flow_log(" authkey: %d\n", hash_parms->key_len);
672 flow_dump(" authkey: ", hash_parms->key_buf, hash_parms->key_len);
673 flow_log(" assoc_size:%u\n", aead_parms->assoc_size);
674 flow_log(" prebuf_len:%u\n", hash_parms->prebuf_len);
675 flow_log(" data_size:%u\n", data_size);
676 flow_log(" hash_pad_len:%u\n", hash_parms->pad_len);
677 flow_log(" real_db_size:%u\n", real_db_size);
678 flow_log(" auth_offset:%u auth_len:%u cipher_offset:%u cipher_len:%u\n",
679 auth_offset, auth_len, cipher_offset, cipher_len);
680 flow_log(" aead_iv: %u\n", aead_parms->iv_len);
682 /* starting out: zero the header (plus some) */
684 memset(ptr, 0, sizeof(struct SPUHEADER));
686 /* format master header word */
687 /* Do not set the next bit even though the datasheet says to */
688 spuh = (struct SPUHEADER *)ptr;
689 ptr += sizeof(struct SPUHEADER);
690 buf_len += sizeof(struct SPUHEADER);
692 spuh->mh.op_code = SPU_CRYPTO_OPERATION_GENERIC;
693 spuh->mh.flags |= (MH_SCTX_PRES | MH_BDESC_PRES | MH_BD_PRES);
695 /* Format sctx word 0 (protocol_bits) */
696 sctx_words = 3; /* size in words */
698 /* Format sctx word 1 (cipher_bits) */
699 if (req_opts->is_inbound)
700 cipher_bits |= CIPHER_INBOUND;
701 if (req_opts->auth_first)
702 cipher_bits |= CIPHER_ORDER;
704 /* Set the crypto parameters in the cipher.flags */
705 cipher_bits |= cipher_parms->alg << CIPHER_ALG_SHIFT;
706 cipher_bits |= cipher_parms->mode << CIPHER_MODE_SHIFT;
707 cipher_bits |= cipher_parms->type << CIPHER_TYPE_SHIFT;
709 /* Set the auth parameters in the cipher.flags */
710 cipher_bits |= hash_parms->alg << HASH_ALG_SHIFT;
711 cipher_bits |= hash_parms->mode << HASH_MODE_SHIFT;
712 cipher_bits |= hash_parms->type << HASH_TYPE_SHIFT;
715 * Format sctx extensions if required, and update main fields if
718 if (hash_parms->alg) {
719 /* Write the authentication key material if present */
720 if (hash_parms->key_len) {
721 memcpy(ptr, hash_parms->key_buf, hash_parms->key_len);
722 ptr += hash_parms->key_len;
723 buf_len += hash_parms->key_len;
724 sctx_words += hash_parms->key_len / 4;
727 if ((cipher_parms->mode == CIPHER_MODE_GCM) ||
728 (cipher_parms->mode == CIPHER_MODE_CCM))
729 /* unpadded length */
730 offset_iv = aead_parms->assoc_size;
732 /* if GCM/CCM we need to write ICV into the payload */
733 if (!req_opts->is_inbound) {
734 if ((cipher_parms->mode == CIPHER_MODE_GCM) ||
735 (cipher_parms->mode == CIPHER_MODE_CCM))
736 ecf_bits |= 1 << INSERT_ICV_SHIFT;
738 ecf_bits |= CHECK_ICV;
741 /* Inform the SPU of the ICV size (in words) */
742 if (hash_parms->digestsize == 64)
743 cipher_bits |= ICV_IS_512;
746 (hash_parms->digestsize / 4) << ICV_SIZE_SHIFT;
749 if (req_opts->bd_suppress)
750 ecf_bits |= BD_SUPPRESS;
752 /* copy the encryption keys in the SAD entry */
753 if (cipher_parms->alg) {
754 if (cipher_parms->key_len) {
755 memcpy(ptr, cipher_parms->key_buf,
756 cipher_parms->key_len);
757 ptr += cipher_parms->key_len;
758 buf_len += cipher_parms->key_len;
759 sctx_words += cipher_parms->key_len / 4;
763 * if encrypting then set IV size, use SCTX IV unless no IV
766 if (cipher_parms->iv_buf && cipher_parms->iv_len) {
770 /* cipher iv provided so put it in here */
771 memcpy(ptr, cipher_parms->iv_buf, cipher_parms->iv_len);
773 ptr += cipher_parms->iv_len;
774 buf_len += cipher_parms->iv_len;
775 sctx_words += cipher_parms->iv_len / 4;
780 * RFC4543 (GMAC/ESP) requires data to be sent as part of AAD
781 * so we need to override the BDESC parameters.
783 if (req_opts->is_rfc4543) {
784 if (req_opts->is_inbound)
785 data_size -= hash_parms->digestsize;
786 offset_iv = aead_parms->assoc_size + data_size;
788 cipher_offset = offset_iv;
789 auth_len = cipher_offset + aead_parms->data_pad_len;
792 /* write in the total sctx length now that we know it */
793 protocol_bits |= sctx_words;
795 /* Endian adjust the SCTX */
796 spuh->sa.proto_flags = cpu_to_be32(protocol_bits);
797 spuh->sa.cipher_flags = cpu_to_be32(cipher_bits);
798 spuh->sa.ecf = cpu_to_be32(ecf_bits);
800 /* === create the BDESC section === */
801 bdesc = (struct BDESC_HEADER *)ptr;
803 bdesc->offset_mac = cpu_to_be16(auth_offset);
804 bdesc->length_mac = cpu_to_be16(auth_len);
805 bdesc->offset_crypto = cpu_to_be16(cipher_offset);
806 bdesc->length_crypto = cpu_to_be16(cipher_len);
809 * CCM in SPU-M requires that ICV not be in same 32-bit word as data or
810 * padding. So account for padding as necessary.
812 if (cipher_parms->mode == CIPHER_MODE_CCM)
813 auth_len += spum_wordalign_padlen(auth_len);
815 bdesc->offset_icv = cpu_to_be16(auth_len);
816 bdesc->offset_iv = cpu_to_be16(offset_iv);
818 ptr += sizeof(struct BDESC_HEADER);
819 buf_len += sizeof(struct BDESC_HEADER);
821 /* === no MFM section === */
823 /* === create the BD section === */
825 /* add the BD header */
826 bd = (struct BD_HEADER *)ptr;
827 bd->size = cpu_to_be16(real_db_size);
830 ptr += sizeof(struct BD_HEADER);
831 buf_len += sizeof(struct BD_HEADER);
833 packet_dump(" SPU request header: ", spu_hdr, buf_len);
839 * spum_cipher_req_init() - Build a SPU request message header, up to and
840 * including the BD header.
841 * @spu_hdr: Start of SPU request header (MH)
842 * @cipher_parms: Parameters that describe the cipher request
844 * Construct the message starting at spu_hdr. Caller should allocate this buffer
845 * in DMA-able memory at least SPU_HEADER_ALLOC_LEN bytes long.
847 * Return: the length of the SPU header in bytes. 0 if an error occurs.
849 u16 spum_cipher_req_init(u8 *spu_hdr, struct spu_cipher_parms *cipher_parms)
851 struct SPUHEADER *spuh;
852 u32 protocol_bits = 0;
858 flow_log("%s()\n", __func__);
859 flow_log(" cipher alg:%u mode:%u type %u\n", cipher_parms->alg,
860 cipher_parms->mode, cipher_parms->type);
861 flow_log(" cipher_iv_len: %u\n", cipher_parms->iv_len);
862 flow_log(" key: %d\n", cipher_parms->key_len);
863 flow_dump(" key: ", cipher_parms->key_buf, cipher_parms->key_len);
865 /* starting out: zero the header (plus some) */
866 memset(spu_hdr, 0, sizeof(struct SPUHEADER));
867 ptr += sizeof(struct SPUHEADER);
869 /* format master header word */
870 /* Do not set the next bit even though the datasheet says to */
871 spuh = (struct SPUHEADER *)spu_hdr;
873 spuh->mh.op_code = SPU_CRYPTO_OPERATION_GENERIC;
874 spuh->mh.flags |= (MH_SCTX_PRES | MH_BDESC_PRES | MH_BD_PRES);
876 /* Format sctx word 0 (protocol_bits) */
877 sctx_words = 3; /* size in words */
879 /* copy the encryption keys in the SAD entry */
880 if (cipher_parms->alg) {
881 if (cipher_parms->key_len) {
882 ptr += cipher_parms->key_len;
883 sctx_words += cipher_parms->key_len / 4;
887 * if encrypting then set IV size, use SCTX IV unless no IV
890 if (cipher_parms->iv_len) {
893 ptr += cipher_parms->iv_len;
894 sctx_words += cipher_parms->iv_len / 4;
898 /* Set the crypto parameters in the cipher.flags */
899 cipher_bits |= cipher_parms->alg << CIPHER_ALG_SHIFT;
900 cipher_bits |= cipher_parms->mode << CIPHER_MODE_SHIFT;
901 cipher_bits |= cipher_parms->type << CIPHER_TYPE_SHIFT;
903 /* copy the encryption keys in the SAD entry */
904 if (cipher_parms->alg && cipher_parms->key_len)
905 memcpy(spuh + 1, cipher_parms->key_buf, cipher_parms->key_len);
907 /* write in the total sctx length now that we know it */
908 protocol_bits |= sctx_words;
910 /* Endian adjust the SCTX */
911 spuh->sa.proto_flags = cpu_to_be32(protocol_bits);
913 /* Endian adjust the SCTX */
914 spuh->sa.cipher_flags = cpu_to_be32(cipher_bits);
915 spuh->sa.ecf = cpu_to_be32(ecf_bits);
917 packet_dump(" SPU request header: ", spu_hdr,
918 sizeof(struct SPUHEADER));
920 return sizeof(struct SPUHEADER) + cipher_parms->key_len +
921 cipher_parms->iv_len + sizeof(struct BDESC_HEADER) +
922 sizeof(struct BD_HEADER);
926 * spum_cipher_req_finish() - Finish building a SPU request message header for a
927 * block cipher request. Assumes much of the header was already filled in at
928 * setkey() time in spu_cipher_req_init().
929 * @spu_hdr: Start of the request message header (MH field)
930 * @spu_req_hdr_len: Length in bytes of the SPU request header
931 * @isInbound: 0 encrypt, 1 decrypt
932 * @cipher_parms: Parameters describing cipher operation to be performed
933 * @update_key: If true, rewrite the cipher key in SCTX
934 * @data_size: Length of the data in the BD field
936 * Assumes much of the header was already filled in at setkey() time in
937 * spum_cipher_req_init().
938 * spum_cipher_req_init() fills in the encryption key. For RC4, when submitting
939 * a request for a non-first chunk, we use the 260-byte SUPDT field from the
940 * previous response as the key. update_key is true for this case. Unused in all
943 void spum_cipher_req_finish(u8 *spu_hdr,
945 unsigned int is_inbound,
946 struct spu_cipher_parms *cipher_parms,
948 unsigned int data_size)
950 struct SPUHEADER *spuh;
951 struct BDESC_HEADER *bdesc;
952 struct BD_HEADER *bd;
953 u8 *bdesc_ptr = spu_hdr + spu_req_hdr_len -
954 (sizeof(struct BD_HEADER) + sizeof(struct BDESC_HEADER));
958 flow_log("%s()\n", __func__);
959 flow_log(" in: %u\n", is_inbound);
960 flow_log(" cipher alg: %u, cipher_type: %u\n", cipher_parms->alg,
963 flow_log(" cipher key len: %u\n", cipher_parms->key_len);
964 flow_dump(" key: ", cipher_parms->key_buf,
965 cipher_parms->key_len);
969 * In XTS mode, API puts "i" parameter (block tweak) in IV. For
970 * SPU-M, should be in start of the BD; tx_sg_create() copies it there.
971 * IV in SPU msg for SPU-M should be 0, since that's the "j" parameter
972 * (block ctr within larger data unit) - given we can send entire disk
973 * block (<= 4KB) in 1 SPU msg, don't need to use this parameter.
975 if (cipher_parms->mode == CIPHER_MODE_XTS)
976 memset(cipher_parms->iv_buf, 0, cipher_parms->iv_len);
978 flow_log(" iv len: %d\n", cipher_parms->iv_len);
979 flow_dump(" iv: ", cipher_parms->iv_buf, cipher_parms->iv_len);
980 flow_log(" data_size: %u\n", data_size);
982 /* format master header word */
983 /* Do not set the next bit even though the datasheet says to */
984 spuh = (struct SPUHEADER *)spu_hdr;
986 /* cipher_bits was initialized at setkey time */
987 cipher_bits = be32_to_cpu(spuh->sa.cipher_flags);
989 /* Format sctx word 1 (cipher_bits) */
991 cipher_bits |= CIPHER_INBOUND;
993 cipher_bits &= ~CIPHER_INBOUND;
995 /* update encryption key for RC4 on non-first chunk */
997 spuh->sa.cipher_flags |=
998 cipher_parms->type << CIPHER_TYPE_SHIFT;
999 memcpy(spuh + 1, cipher_parms->key_buf, cipher_parms->key_len);
1002 if (cipher_parms->alg && cipher_parms->iv_buf && cipher_parms->iv_len)
1003 /* cipher iv provided so put it in here */
1004 memcpy(bdesc_ptr - cipher_parms->iv_len, cipher_parms->iv_buf,
1005 cipher_parms->iv_len);
1007 spuh->sa.cipher_flags = cpu_to_be32(cipher_bits);
1009 /* === create the BDESC section === */
1010 bdesc = (struct BDESC_HEADER *)bdesc_ptr;
1011 bdesc->offset_mac = 0;
1012 bdesc->length_mac = 0;
1013 bdesc->offset_crypto = 0;
1015 /* XTS mode, data_size needs to include tweak parameter */
1016 if (cipher_parms->mode == CIPHER_MODE_XTS)
1017 bdesc->length_crypto = cpu_to_be16(data_size +
1018 SPU_XTS_TWEAK_SIZE);
1020 bdesc->length_crypto = cpu_to_be16(data_size);
1022 bdesc->offset_icv = 0;
1023 bdesc->offset_iv = 0;
1025 /* === no MFM section === */
1027 /* === create the BD section === */
1028 /* add the BD header */
1029 bd = (struct BD_HEADER *)(bdesc_ptr + sizeof(struct BDESC_HEADER));
1030 bd->size = cpu_to_be16(data_size);
1032 /* XTS mode, data_size needs to include tweak parameter */
1033 if (cipher_parms->mode == CIPHER_MODE_XTS)
1034 bd->size = cpu_to_be16(data_size + SPU_XTS_TWEAK_SIZE);
1036 bd->size = cpu_to_be16(data_size);
1038 bd->prev_length = 0;
1040 packet_dump(" SPU request header: ", spu_hdr, spu_req_hdr_len);
1044 * spum_request_pad() - Create pad bytes at the end of the data.
1045 * @pad_start: Start of buffer where pad bytes are to be written
1046 * @gcm_ccm_padding: length of GCM/CCM padding, in bytes
1047 * @hash_pad_len: Number of bytes of padding extend data to full block
1048 * @auth_alg: authentication algorithm
1049 * @auth_mode: authentication mode
1050 * @total_sent: length inserted at end of hash pad
1051 * @status_padding: Number of bytes of padding to align STATUS word
1053 * There may be three forms of pad:
1054 * 1. GCM/CCM pad - for GCM/CCM mode ciphers, pad to 16-byte alignment
1055 * 2. hash pad - pad to a block length, with 0x80 data terminator and
1057 * 3. STAT pad - to ensure the STAT field is 4-byte aligned
1059 void spum_request_pad(u8 *pad_start,
1060 u32 gcm_ccm_padding,
1062 enum hash_alg auth_alg,
1063 enum hash_mode auth_mode,
1064 unsigned int total_sent, u32 status_padding)
1066 u8 *ptr = pad_start;
1068 /* fix data alignent for GCM/CCM */
1069 if (gcm_ccm_padding > 0) {
1070 flow_log(" GCM: padding to 16 byte alignment: %u bytes\n",
1072 memset(ptr, 0, gcm_ccm_padding);
1073 ptr += gcm_ccm_padding;
1076 if (hash_pad_len > 0) {
1077 /* clear the padding section */
1078 memset(ptr, 0, hash_pad_len);
1080 if ((auth_alg == HASH_ALG_AES) &&
1081 (auth_mode == HASH_MODE_XCBC)) {
1082 /* AES/XCBC just requires padding to be 0s */
1083 ptr += hash_pad_len;
1085 /* terminate the data */
1087 ptr += (hash_pad_len - sizeof(u64));
1089 /* add the size at the end as required per alg */
1090 if (auth_alg == HASH_ALG_MD5)
1091 *(u64 *)ptr = cpu_to_le64((u64)total_sent * 8);
1092 else /* SHA1, SHA2-224, SHA2-256 */
1093 *(u64 *)ptr = cpu_to_be64((u64)total_sent * 8);
1098 /* pad to a 4byte alignment for STAT */
1099 if (status_padding > 0) {
1100 flow_log(" STAT: padding to 4 byte alignment: %u bytes\n",
1103 memset(ptr, 0, status_padding);
1104 ptr += status_padding;
1109 * spum_xts_tweak_in_payload() - Indicate that SPUM DOES place the XTS tweak
1110 * field in the packet payload (rather than using IV)
1114 u8 spum_xts_tweak_in_payload(void)
1120 * spum_tx_status_len() - Return the length of the STATUS field in a SPU
1123 * Return: Length of STATUS field in bytes.
1125 u8 spum_tx_status_len(void)
1127 return SPU_TX_STATUS_LEN;
1131 * spum_rx_status_len() - Return the length of the STATUS field in a SPU
1134 * Return: Length of STATUS field in bytes.
1136 u8 spum_rx_status_len(void)
1138 return SPU_RX_STATUS_LEN;
1142 * spum_status_process() - Process the status from a SPU response message.
1143 * @statp: start of STATUS word
1145 * 0 - if status is good and response should be processed
1146 * !0 - status indicates an error and response is invalid
1148 int spum_status_process(u8 *statp)
1152 status = __be32_to_cpu(*(__be32 *)statp);
1153 flow_log("SPU response STATUS %#08x\n", status);
1154 if (status & SPU_STATUS_ERROR_FLAG) {
1155 pr_err("%s() Warning: Error result from SPU: %#08x\n",
1157 if (status & SPU_STATUS_INVALID_ICV)
1158 return SPU_INVALID_ICV;
1165 * spum_ccm_update_iv() - Update the IV as per the requirements for CCM mode.
1167 * @digestsize: Digest size of this request
1168 * @cipher_parms: (pointer to) cipher parmaeters, includes IV buf & IV len
1169 * @assoclen: Length of AAD data
1170 * @chunksize: length of input data to be sent in this req
1171 * @is_encrypt: true if this is an output/encrypt operation
1172 * @is_esp: true if this is an ESP / RFC4309 operation
1175 void spum_ccm_update_iv(unsigned int digestsize,
1176 struct spu_cipher_parms *cipher_parms,
1177 unsigned int assoclen,
1178 unsigned int chunksize,
1182 u8 L; /* L from CCM algorithm, length of plaintext data */
1183 u8 mprime; /* M' from CCM algo, (M - 2) / 2, where M=authsize */
1186 if (cipher_parms->iv_len != CCM_AES_IV_SIZE) {
1187 pr_err("%s(): Invalid IV len %d for CCM mode, should be %d\n",
1188 __func__, cipher_parms->iv_len, CCM_AES_IV_SIZE);
1193 * IV needs to be formatted as follows:
1195 * | Byte 0 | Bytes 1 - N | Bytes (N+1) - 15 |
1196 * | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | Bits 7 - 0 | Bits 7 - 0 |
1197 * | 0 |Ad?|(M - 2) / 2| L - 1 | Nonce | Plaintext Length |
1199 * Ad? = 1 if AAD present, 0 if not present
1200 * M = size of auth field, 8, 12, or 16 bytes (SPU-M) -or-
1201 * 4, 6, 8, 10, 12, 14, 16 bytes (SPU2)
1202 * L = Size of Plaintext Length field; Nonce size = 15 - L
1204 * It appears that the crypto API already expects the L-1 portion
1205 * to be set in the first byte of the IV, which implicitly determines
1206 * the nonce size, and also fills in the nonce. But the other bits
1207 * in byte 0 as well as the plaintext length need to be filled in.
1209 * In rfc4309/esp mode, L is not already in the supplied IV and
1210 * we need to fill it in, as well as move the IV data to be after
1214 L = CCM_ESP_L_VALUE; /* RFC4309 has fixed L */
1216 /* L' = plaintext length - 1 so Plaintext length is L' + 1 */
1217 L = ((cipher_parms->iv_buf[0] & CCM_B0_L_PRIME) >>
1218 CCM_B0_L_PRIME_SHIFT) + 1;
1221 mprime = (digestsize - 2) >> 1; /* M' = (M - 2) / 2 */
1222 adata = (assoclen > 0); /* adata = 1 if any associated data */
1224 cipher_parms->iv_buf[0] = (adata << CCM_B0_ADATA_SHIFT) |
1225 (mprime << CCM_B0_M_PRIME_SHIFT) |
1226 ((L - 1) << CCM_B0_L_PRIME_SHIFT);
1228 /* Nonce is already filled in by crypto API, and is 15 - L bytes */
1230 /* Don't include digest in plaintext size when decrypting */
1232 chunksize -= digestsize;
1234 /* Fill in length of plaintext, formatted to be L bytes long */
1235 format_value_ccm(chunksize, &cipher_parms->iv_buf[15 - L + 1], L);
1239 * spum_wordalign_padlen() - Given the length of a data field, determine the
1240 * padding required to align the data following this field on a 4-byte boundary.
1241 * @data_size: length of data field in bytes
1243 * Return: length of status field padding, in bytes
1245 u32 spum_wordalign_padlen(u32 data_size)
1247 return ((data_size + 3) & ~3) - data_size;