0001
0002
0003
0004
0005
0006 #include <linux/kernel.h>
0007 #include <linux/string.h>
0008
0009 #include "util.h"
0010 #include "spu.h"
0011 #include "spum.h"
0012 #include "cipher.h"
0013
0014 char *hash_alg_name[] = { "None", "md5", "sha1", "sha224", "sha256", "aes",
0015 "sha384", "sha512", "sha3_224", "sha3_256", "sha3_384", "sha3_512" };
0016
0017 char *aead_alg_name[] = { "ccm(aes)", "gcm(aes)", "authenc" };
0018
0019
0020 void spum_dump_msg_hdr(u8 *buf, unsigned int buf_len)
0021 {
0022 u8 *ptr = buf;
0023 struct SPUHEADER *spuh = (struct SPUHEADER *)buf;
0024 unsigned int hash_key_len = 0;
0025 unsigned int hash_state_len = 0;
0026 unsigned int cipher_key_len = 0;
0027 unsigned int iv_len;
0028 u32 pflags;
0029 u32 cflags;
0030 u32 ecf;
0031 u32 cipher_alg;
0032 u32 cipher_mode;
0033 u32 cipher_type;
0034 u32 hash_alg;
0035 u32 hash_mode;
0036 u32 hash_type;
0037 u32 sctx_size;
0038 u32 sctx_pl_len;
0039
0040 packet_log("\n");
0041 packet_log("SPU Message header %p len: %u\n", buf, buf_len);
0042
0043
0044 packet_log(" MH 0x%08x\n", be32_to_cpup((__be32 *)ptr));
0045 if (spuh->mh.flags & MH_SCTX_PRES)
0046 packet_log(" SCTX present\n");
0047 if (spuh->mh.flags & MH_BDESC_PRES)
0048 packet_log(" BDESC present\n");
0049 if (spuh->mh.flags & MH_MFM_PRES)
0050 packet_log(" MFM present\n");
0051 if (spuh->mh.flags & MH_BD_PRES)
0052 packet_log(" BD present\n");
0053 if (spuh->mh.flags & MH_HASH_PRES)
0054 packet_log(" HASH present\n");
0055 if (spuh->mh.flags & MH_SUPDT_PRES)
0056 packet_log(" SUPDT present\n");
0057 packet_log(" Opcode 0x%02x\n", spuh->mh.op_code);
0058
0059 ptr += sizeof(spuh->mh) + sizeof(spuh->emh);
0060
0061
0062 if (spuh->mh.flags & MH_SCTX_PRES) {
0063 pflags = be32_to_cpu(spuh->sa.proto_flags);
0064 packet_log(" SCTX[0] 0x%08x\n", pflags);
0065 sctx_size = pflags & SCTX_SIZE;
0066 packet_log(" Size %u words\n", sctx_size);
0067
0068 cflags = be32_to_cpu(spuh->sa.cipher_flags);
0069 packet_log(" SCTX[1] 0x%08x\n", cflags);
0070 packet_log(" Inbound:%lu (1:decrypt/vrfy 0:encrypt/auth)\n",
0071 (cflags & CIPHER_INBOUND) >> CIPHER_INBOUND_SHIFT);
0072 packet_log(" Order:%lu (1:AuthFirst 0:EncFirst)\n",
0073 (cflags & CIPHER_ORDER) >> CIPHER_ORDER_SHIFT);
0074 packet_log(" ICV_IS_512:%lx\n",
0075 (cflags & ICV_IS_512) >> ICV_IS_512_SHIFT);
0076 cipher_alg = (cflags & CIPHER_ALG) >> CIPHER_ALG_SHIFT;
0077 cipher_mode = (cflags & CIPHER_MODE) >> CIPHER_MODE_SHIFT;
0078 cipher_type = (cflags & CIPHER_TYPE) >> CIPHER_TYPE_SHIFT;
0079 packet_log(" Crypto Alg:%u Mode:%u Type:%u\n",
0080 cipher_alg, cipher_mode, cipher_type);
0081 hash_alg = (cflags & HASH_ALG) >> HASH_ALG_SHIFT;
0082 hash_mode = (cflags & HASH_MODE) >> HASH_MODE_SHIFT;
0083 hash_type = (cflags & HASH_TYPE) >> HASH_TYPE_SHIFT;
0084 packet_log(" Hash Alg:%x Mode:%x Type:%x\n",
0085 hash_alg, hash_mode, hash_type);
0086 packet_log(" UPDT_Offset:%u\n", cflags & UPDT_OFST);
0087
0088 ecf = be32_to_cpu(spuh->sa.ecf);
0089 packet_log(" SCTX[2] 0x%08x\n", ecf);
0090 packet_log(" WriteICV:%lu CheckICV:%lu ICV_SIZE:%u ",
0091 (ecf & INSERT_ICV) >> INSERT_ICV_SHIFT,
0092 (ecf & CHECK_ICV) >> CHECK_ICV_SHIFT,
0093 (ecf & ICV_SIZE) >> ICV_SIZE_SHIFT);
0094 packet_log("BD_SUPPRESS:%lu\n",
0095 (ecf & BD_SUPPRESS) >> BD_SUPPRESS_SHIFT);
0096 packet_log(" SCTX_IV:%lu ExplicitIV:%lu GenIV:%lu ",
0097 (ecf & SCTX_IV) >> SCTX_IV_SHIFT,
0098 (ecf & EXPLICIT_IV) >> EXPLICIT_IV_SHIFT,
0099 (ecf & GEN_IV) >> GEN_IV_SHIFT);
0100 packet_log("IV_OV_OFST:%lu EXP_IV_SIZE:%u\n",
0101 (ecf & IV_OFFSET) >> IV_OFFSET_SHIFT,
0102 ecf & EXP_IV_SIZE);
0103
0104 ptr += sizeof(struct SCTX);
0105
0106 if (hash_alg && hash_mode) {
0107 char *name = "NONE";
0108
0109 switch (hash_alg) {
0110 case HASH_ALG_MD5:
0111 hash_key_len = 16;
0112 name = "MD5";
0113 break;
0114 case HASH_ALG_SHA1:
0115 hash_key_len = 20;
0116 name = "SHA1";
0117 break;
0118 case HASH_ALG_SHA224:
0119 hash_key_len = 28;
0120 name = "SHA224";
0121 break;
0122 case HASH_ALG_SHA256:
0123 hash_key_len = 32;
0124 name = "SHA256";
0125 break;
0126 case HASH_ALG_SHA384:
0127 hash_key_len = 48;
0128 name = "SHA384";
0129 break;
0130 case HASH_ALG_SHA512:
0131 hash_key_len = 64;
0132 name = "SHA512";
0133 break;
0134 case HASH_ALG_AES:
0135 hash_key_len = 0;
0136 name = "AES";
0137 break;
0138 case HASH_ALG_NONE:
0139 break;
0140 }
0141
0142 packet_log(" Auth Key Type:%s Length:%u Bytes\n",
0143 name, hash_key_len);
0144 packet_dump(" KEY: ", ptr, hash_key_len);
0145 ptr += hash_key_len;
0146 } else if ((hash_alg == HASH_ALG_AES) &&
0147 (hash_mode == HASH_MODE_XCBC)) {
0148 char *name = "NONE";
0149
0150 switch (cipher_type) {
0151 case CIPHER_TYPE_AES128:
0152 hash_key_len = 16;
0153 name = "AES128-XCBC";
0154 break;
0155 case CIPHER_TYPE_AES192:
0156 hash_key_len = 24;
0157 name = "AES192-XCBC";
0158 break;
0159 case CIPHER_TYPE_AES256:
0160 hash_key_len = 32;
0161 name = "AES256-XCBC";
0162 break;
0163 }
0164 packet_log(" Auth Key Type:%s Length:%u Bytes\n",
0165 name, hash_key_len);
0166 packet_dump(" KEY: ", ptr, hash_key_len);
0167 ptr += hash_key_len;
0168 }
0169
0170 if (hash_alg && (hash_mode == HASH_MODE_NONE) &&
0171 (hash_type == HASH_TYPE_UPDT)) {
0172 char *name = "NONE";
0173
0174 switch (hash_alg) {
0175 case HASH_ALG_MD5:
0176 hash_state_len = 16;
0177 name = "MD5";
0178 break;
0179 case HASH_ALG_SHA1:
0180 hash_state_len = 20;
0181 name = "SHA1";
0182 break;
0183 case HASH_ALG_SHA224:
0184 hash_state_len = 32;
0185 name = "SHA224";
0186 break;
0187 case HASH_ALG_SHA256:
0188 hash_state_len = 32;
0189 name = "SHA256";
0190 break;
0191 case HASH_ALG_SHA384:
0192 hash_state_len = 48;
0193 name = "SHA384";
0194 break;
0195 case HASH_ALG_SHA512:
0196 hash_state_len = 64;
0197 name = "SHA512";
0198 break;
0199 case HASH_ALG_AES:
0200 hash_state_len = 0;
0201 name = "AES";
0202 break;
0203 case HASH_ALG_NONE:
0204 break;
0205 }
0206
0207 packet_log(" Auth State Type:%s Length:%u Bytes\n",
0208 name, hash_state_len);
0209 packet_dump(" State: ", ptr, hash_state_len);
0210 ptr += hash_state_len;
0211 }
0212
0213 if (cipher_alg) {
0214 char *name = "NONE";
0215
0216 switch (cipher_alg) {
0217 case CIPHER_ALG_DES:
0218 cipher_key_len = 8;
0219 name = "DES";
0220 break;
0221 case CIPHER_ALG_3DES:
0222 cipher_key_len = 24;
0223 name = "3DES";
0224 break;
0225 case CIPHER_ALG_AES:
0226 switch (cipher_type) {
0227 case CIPHER_TYPE_AES128:
0228 cipher_key_len = 16;
0229 name = "AES128";
0230 break;
0231 case CIPHER_TYPE_AES192:
0232 cipher_key_len = 24;
0233 name = "AES192";
0234 break;
0235 case CIPHER_TYPE_AES256:
0236 cipher_key_len = 32;
0237 name = "AES256";
0238 break;
0239 }
0240 break;
0241 case CIPHER_ALG_NONE:
0242 break;
0243 }
0244
0245 packet_log(" Cipher Key Type:%s Length:%u Bytes\n",
0246 name, cipher_key_len);
0247
0248
0249 if (cipher_mode == CIPHER_MODE_XTS) {
0250 packet_dump(" KEY2: ", ptr, cipher_key_len);
0251 ptr += cipher_key_len;
0252 packet_dump(" KEY1: ", ptr, cipher_key_len);
0253 ptr += cipher_key_len;
0254
0255 cipher_key_len *= 2;
0256 } else {
0257 packet_dump(" KEY: ", ptr, cipher_key_len);
0258 ptr += cipher_key_len;
0259 }
0260
0261 if (ecf & SCTX_IV) {
0262 sctx_pl_len = sctx_size * sizeof(u32) -
0263 sizeof(struct SCTX);
0264 iv_len = sctx_pl_len -
0265 (hash_key_len + hash_state_len +
0266 cipher_key_len);
0267 packet_log(" IV Length:%u Bytes\n", iv_len);
0268 packet_dump(" IV: ", ptr, iv_len);
0269 ptr += iv_len;
0270 }
0271 }
0272 }
0273
0274
0275 if (spuh->mh.flags & MH_BDESC_PRES) {
0276 struct BDESC_HEADER *bdesc = (struct BDESC_HEADER *)ptr;
0277
0278 packet_log(" BDESC[0] 0x%08x\n", be32_to_cpup((__be32 *)ptr));
0279 packet_log(" OffsetMAC:%u LengthMAC:%u\n",
0280 be16_to_cpu(bdesc->offset_mac),
0281 be16_to_cpu(bdesc->length_mac));
0282 ptr += sizeof(u32);
0283
0284 packet_log(" BDESC[1] 0x%08x\n", be32_to_cpup((__be32 *)ptr));
0285 packet_log(" OffsetCrypto:%u LengthCrypto:%u\n",
0286 be16_to_cpu(bdesc->offset_crypto),
0287 be16_to_cpu(bdesc->length_crypto));
0288 ptr += sizeof(u32);
0289
0290 packet_log(" BDESC[2] 0x%08x\n", be32_to_cpup((__be32 *)ptr));
0291 packet_log(" OffsetICV:%u OffsetIV:%u\n",
0292 be16_to_cpu(bdesc->offset_icv),
0293 be16_to_cpu(bdesc->offset_iv));
0294 ptr += sizeof(u32);
0295 }
0296
0297
0298 if (spuh->mh.flags & MH_BD_PRES) {
0299 struct BD_HEADER *bd = (struct BD_HEADER *)ptr;
0300
0301 packet_log(" BD[0] 0x%08x\n", be32_to_cpup((__be32 *)ptr));
0302 packet_log(" Size:%ubytes PrevLength:%u\n",
0303 be16_to_cpu(bd->size), be16_to_cpu(bd->prev_length));
0304 ptr += 4;
0305 }
0306
0307
0308 if (buf + buf_len != ptr) {
0309 packet_log(" Packet parsed incorrectly. ");
0310 packet_log("buf:%p buf_len:%u buf+buf_len:%p ptr:%p\n",
0311 buf, buf_len, buf + buf_len, ptr);
0312 }
0313
0314 packet_log("\n");
0315 }
0316
0317
0318
0319
0320
0321
0322
0323
0324
0325
0326
0327
0328
0329
0330 u32 spum_ns2_ctx_max_payload(enum spu_cipher_alg cipher_alg,
0331 enum spu_cipher_mode cipher_mode,
0332 unsigned int blocksize)
0333 {
0334 u32 max_payload = SPUM_NS2_MAX_PAYLOAD;
0335 u32 excess;
0336
0337
0338 if (cipher_mode == CIPHER_MODE_XTS)
0339 max_payload -= SPU_XTS_TWEAK_SIZE;
0340
0341 excess = max_payload % blocksize;
0342
0343 return max_payload - excess;
0344 }
0345
0346
0347
0348
0349
0350
0351
0352
0353
0354
0355
0356
0357
0358
0359 u32 spum_nsp_ctx_max_payload(enum spu_cipher_alg cipher_alg,
0360 enum spu_cipher_mode cipher_mode,
0361 unsigned int blocksize)
0362 {
0363 u32 max_payload = SPUM_NSP_MAX_PAYLOAD;
0364 u32 excess;
0365
0366
0367 if (cipher_mode == CIPHER_MODE_XTS)
0368 max_payload -= SPU_XTS_TWEAK_SIZE;
0369
0370 excess = max_payload % blocksize;
0371
0372 return max_payload - excess;
0373 }
0374
0375
0376
0377
0378
0379
0380
0381
0382
0383 u32 spum_payload_length(u8 *spu_hdr)
0384 {
0385 struct BD_HEADER *bd;
0386 u32 pl_len;
0387
0388
0389 bd = (struct BD_HEADER *)(spu_hdr + 8);
0390 pl_len = be16_to_cpu(bd->size);
0391
0392 return pl_len;
0393 }
0394
0395
0396
0397
0398
0399
0400
0401
0402
0403
0404 u16 spum_response_hdr_len(u16 auth_key_len, u16 enc_key_len, bool is_hash)
0405 {
0406 if (is_hash)
0407 return SPU_HASH_RESP_HDR_LEN;
0408 else
0409 return SPU_RESP_HDR_LEN;
0410 }
0411
0412
0413
0414
0415
0416
0417
0418
0419
0420
0421
0422
0423
0424 u16 spum_hash_pad_len(enum hash_alg hash_alg, enum hash_mode hash_mode,
0425 u32 chunksize, u16 hash_block_size)
0426 {
0427 unsigned int length_len;
0428 unsigned int used_space_last_block;
0429 int hash_pad_len;
0430
0431
0432 if ((hash_alg == HASH_ALG_AES) && (hash_mode == HASH_MODE_XCBC)) {
0433 used_space_last_block = chunksize % hash_block_size;
0434 hash_pad_len = hash_block_size - used_space_last_block;
0435 if (hash_pad_len >= hash_block_size)
0436 hash_pad_len -= hash_block_size;
0437 return hash_pad_len;
0438 }
0439
0440 used_space_last_block = chunksize % hash_block_size + 1;
0441 if ((hash_alg == HASH_ALG_SHA384) || (hash_alg == HASH_ALG_SHA512))
0442 length_len = 2 * sizeof(u64);
0443 else
0444 length_len = sizeof(u64);
0445
0446 used_space_last_block += length_len;
0447 hash_pad_len = hash_block_size - used_space_last_block;
0448 if (hash_pad_len < 0)
0449 hash_pad_len += hash_block_size;
0450
0451 hash_pad_len += 1 + length_len;
0452 return hash_pad_len;
0453 }
0454
0455
0456
0457
0458
0459
0460
0461
0462 u32 spum_gcm_ccm_pad_len(enum spu_cipher_mode cipher_mode,
0463 unsigned int data_size)
0464 {
0465 u32 pad_len = 0;
0466 u32 m1 = SPU_GCM_CCM_ALIGN - 1;
0467
0468 if ((cipher_mode == CIPHER_MODE_GCM) ||
0469 (cipher_mode == CIPHER_MODE_CCM))
0470 pad_len = ((data_size + m1) & ~m1) - data_size;
0471
0472 return pad_len;
0473 }
0474
0475
0476
0477
0478
0479
0480
0481
0482
0483
0484
0485 u32 spum_assoc_resp_len(enum spu_cipher_mode cipher_mode,
0486 unsigned int assoc_len, unsigned int iv_len,
0487 bool is_encrypt)
0488 {
0489 u32 buflen = 0;
0490 u32 pad;
0491
0492 if (assoc_len)
0493 buflen = assoc_len;
0494
0495 if (cipher_mode == CIPHER_MODE_GCM) {
0496
0497 pad = spum_gcm_ccm_pad_len(cipher_mode, buflen);
0498 buflen += pad;
0499 }
0500 if (cipher_mode == CIPHER_MODE_CCM) {
0501
0502
0503
0504
0505 pad = spum_gcm_ccm_pad_len(cipher_mode, buflen + 2);
0506 buflen += pad;
0507 }
0508
0509 return buflen;
0510 }
0511
0512
0513
0514
0515
0516
0517
0518
0519
0520
0521
0522
0523 u8 spum_aead_ivlen(enum spu_cipher_mode cipher_mode, u16 iv_len)
0524 {
0525 return 0;
0526 }
0527
0528
0529
0530
0531
0532
0533
0534
0535
0536
0537
0538 enum hash_type spum_hash_type(u32 src_sent)
0539 {
0540 return src_sent ? HASH_TYPE_UPDT : HASH_TYPE_INIT;
0541 }
0542
0543
0544
0545
0546
0547
0548
0549
0550
0551
0552
0553
0554 u32 spum_digest_size(u32 alg_digest_size, enum hash_alg alg,
0555 enum hash_type htype)
0556 {
0557 u32 digestsize = alg_digest_size;
0558
0559
0560
0561
0562 if ((htype == HASH_TYPE_INIT) || (htype == HASH_TYPE_UPDT)) {
0563 if (alg == HASH_ALG_SHA224)
0564 digestsize = SHA256_DIGEST_SIZE;
0565 else if (alg == HASH_ALG_SHA384)
0566 digestsize = SHA512_DIGEST_SIZE;
0567 }
0568 return digestsize;
0569 }
0570
0571
0572
0573
0574
0575
0576
0577
0578
0579
0580
0581
0582
0583
0584
0585
0586 u32 spum_create_request(u8 *spu_hdr,
0587 struct spu_request_opts *req_opts,
0588 struct spu_cipher_parms *cipher_parms,
0589 struct spu_hash_parms *hash_parms,
0590 struct spu_aead_parms *aead_parms,
0591 unsigned int data_size)
0592 {
0593 struct SPUHEADER *spuh;
0594 struct BDESC_HEADER *bdesc;
0595 struct BD_HEADER *bd;
0596
0597 u8 *ptr;
0598 u32 protocol_bits = 0;
0599 u32 cipher_bits = 0;
0600 u32 ecf_bits = 0;
0601 u8 sctx_words = 0;
0602 unsigned int buf_len = 0;
0603
0604
0605 unsigned int cipher_len = hash_parms->prebuf_len + data_size +
0606 hash_parms->pad_len;
0607
0608
0609 unsigned int cipher_offset = aead_parms->assoc_size +
0610 aead_parms->iv_len + aead_parms->aad_pad_len;
0611
0612
0613 unsigned int real_db_size = spu_real_db_size(aead_parms->assoc_size,
0614 aead_parms->iv_len,
0615 hash_parms->prebuf_len,
0616 data_size,
0617 aead_parms->aad_pad_len,
0618 aead_parms->data_pad_len,
0619 hash_parms->pad_len);
0620
0621 unsigned int auth_offset = 0;
0622 unsigned int offset_iv = 0;
0623
0624
0625 unsigned int auth_len;
0626
0627 auth_len = real_db_size;
0628
0629 if (req_opts->is_aead && req_opts->is_inbound)
0630 cipher_len -= hash_parms->digestsize;
0631
0632 if (req_opts->is_aead && req_opts->is_inbound)
0633 auth_len -= hash_parms->digestsize;
0634
0635 if ((hash_parms->alg == HASH_ALG_AES) &&
0636 (hash_parms->mode == HASH_MODE_XCBC)) {
0637 auth_len -= hash_parms->pad_len;
0638 cipher_len -= hash_parms->pad_len;
0639 }
0640
0641 flow_log("%s()\n", __func__);
0642 flow_log(" in:%u authFirst:%u\n",
0643 req_opts->is_inbound, req_opts->auth_first);
0644 flow_log(" %s. cipher alg:%u mode:%u type %u\n",
0645 spu_alg_name(cipher_parms->alg, cipher_parms->mode),
0646 cipher_parms->alg, cipher_parms->mode, cipher_parms->type);
0647 flow_log(" key: %d\n", cipher_parms->key_len);
0648 flow_dump(" key: ", cipher_parms->key_buf, cipher_parms->key_len);
0649 flow_log(" iv: %d\n", cipher_parms->iv_len);
0650 flow_dump(" iv: ", cipher_parms->iv_buf, cipher_parms->iv_len);
0651 flow_log(" auth alg:%u mode:%u type %u\n",
0652 hash_parms->alg, hash_parms->mode, hash_parms->type);
0653 flow_log(" digestsize: %u\n", hash_parms->digestsize);
0654 flow_log(" authkey: %d\n", hash_parms->key_len);
0655 flow_dump(" authkey: ", hash_parms->key_buf, hash_parms->key_len);
0656 flow_log(" assoc_size:%u\n", aead_parms->assoc_size);
0657 flow_log(" prebuf_len:%u\n", hash_parms->prebuf_len);
0658 flow_log(" data_size:%u\n", data_size);
0659 flow_log(" hash_pad_len:%u\n", hash_parms->pad_len);
0660 flow_log(" real_db_size:%u\n", real_db_size);
0661 flow_log(" auth_offset:%u auth_len:%u cipher_offset:%u cipher_len:%u\n",
0662 auth_offset, auth_len, cipher_offset, cipher_len);
0663 flow_log(" aead_iv: %u\n", aead_parms->iv_len);
0664
0665
0666 ptr = spu_hdr;
0667 memset(ptr, 0, sizeof(struct SPUHEADER));
0668
0669
0670
0671 spuh = (struct SPUHEADER *)ptr;
0672 ptr += sizeof(struct SPUHEADER);
0673 buf_len += sizeof(struct SPUHEADER);
0674
0675 spuh->mh.op_code = SPU_CRYPTO_OPERATION_GENERIC;
0676 spuh->mh.flags |= (MH_SCTX_PRES | MH_BDESC_PRES | MH_BD_PRES);
0677
0678
0679 sctx_words = 3;
0680
0681
0682 if (req_opts->is_inbound)
0683 cipher_bits |= CIPHER_INBOUND;
0684 if (req_opts->auth_first)
0685 cipher_bits |= CIPHER_ORDER;
0686
0687
0688 cipher_bits |= cipher_parms->alg << CIPHER_ALG_SHIFT;
0689 cipher_bits |= cipher_parms->mode << CIPHER_MODE_SHIFT;
0690 cipher_bits |= cipher_parms->type << CIPHER_TYPE_SHIFT;
0691
0692
0693 cipher_bits |= hash_parms->alg << HASH_ALG_SHIFT;
0694 cipher_bits |= hash_parms->mode << HASH_MODE_SHIFT;
0695 cipher_bits |= hash_parms->type << HASH_TYPE_SHIFT;
0696
0697
0698
0699
0700
0701 if (hash_parms->alg) {
0702
0703 if (hash_parms->key_len) {
0704 memcpy(ptr, hash_parms->key_buf, hash_parms->key_len);
0705 ptr += hash_parms->key_len;
0706 buf_len += hash_parms->key_len;
0707 sctx_words += hash_parms->key_len / 4;
0708 }
0709
0710 if ((cipher_parms->mode == CIPHER_MODE_GCM) ||
0711 (cipher_parms->mode == CIPHER_MODE_CCM))
0712
0713 offset_iv = aead_parms->assoc_size;
0714
0715
0716 if (!req_opts->is_inbound) {
0717 if ((cipher_parms->mode == CIPHER_MODE_GCM) ||
0718 (cipher_parms->mode == CIPHER_MODE_CCM))
0719 ecf_bits |= 1 << INSERT_ICV_SHIFT;
0720 } else {
0721 ecf_bits |= CHECK_ICV;
0722 }
0723
0724
0725 if (hash_parms->digestsize == 64)
0726 cipher_bits |= ICV_IS_512;
0727 else
0728 ecf_bits |=
0729 (hash_parms->digestsize / 4) << ICV_SIZE_SHIFT;
0730 }
0731
0732 if (req_opts->bd_suppress)
0733 ecf_bits |= BD_SUPPRESS;
0734
0735
0736 if (cipher_parms->alg) {
0737 if (cipher_parms->key_len) {
0738 memcpy(ptr, cipher_parms->key_buf,
0739 cipher_parms->key_len);
0740 ptr += cipher_parms->key_len;
0741 buf_len += cipher_parms->key_len;
0742 sctx_words += cipher_parms->key_len / 4;
0743 }
0744
0745
0746
0747
0748
0749 if (cipher_parms->iv_buf && cipher_parms->iv_len) {
0750
0751 ecf_bits |= SCTX_IV;
0752
0753
0754 memcpy(ptr, cipher_parms->iv_buf, cipher_parms->iv_len);
0755
0756 ptr += cipher_parms->iv_len;
0757 buf_len += cipher_parms->iv_len;
0758 sctx_words += cipher_parms->iv_len / 4;
0759 }
0760 }
0761
0762
0763
0764
0765
0766 if (req_opts->is_rfc4543) {
0767 if (req_opts->is_inbound)
0768 data_size -= hash_parms->digestsize;
0769 offset_iv = aead_parms->assoc_size + data_size;
0770 cipher_len = 0;
0771 cipher_offset = offset_iv;
0772 auth_len = cipher_offset + aead_parms->data_pad_len;
0773 }
0774
0775
0776 protocol_bits |= sctx_words;
0777
0778
0779 spuh->sa.proto_flags = cpu_to_be32(protocol_bits);
0780 spuh->sa.cipher_flags = cpu_to_be32(cipher_bits);
0781 spuh->sa.ecf = cpu_to_be32(ecf_bits);
0782
0783
0784 bdesc = (struct BDESC_HEADER *)ptr;
0785
0786 bdesc->offset_mac = cpu_to_be16(auth_offset);
0787 bdesc->length_mac = cpu_to_be16(auth_len);
0788 bdesc->offset_crypto = cpu_to_be16(cipher_offset);
0789 bdesc->length_crypto = cpu_to_be16(cipher_len);
0790
0791
0792
0793
0794
0795 if (cipher_parms->mode == CIPHER_MODE_CCM)
0796 auth_len += spum_wordalign_padlen(auth_len);
0797
0798 bdesc->offset_icv = cpu_to_be16(auth_len);
0799 bdesc->offset_iv = cpu_to_be16(offset_iv);
0800
0801 ptr += sizeof(struct BDESC_HEADER);
0802 buf_len += sizeof(struct BDESC_HEADER);
0803
0804
0805
0806
0807
0808
0809 bd = (struct BD_HEADER *)ptr;
0810 bd->size = cpu_to_be16(real_db_size);
0811 bd->prev_length = 0;
0812
0813 ptr += sizeof(struct BD_HEADER);
0814 buf_len += sizeof(struct BD_HEADER);
0815
0816 packet_dump(" SPU request header: ", spu_hdr, buf_len);
0817
0818 return buf_len;
0819 }
0820
0821
0822
0823
0824
0825
0826
0827
0828
0829
0830
0831
0832 u16 spum_cipher_req_init(u8 *spu_hdr, struct spu_cipher_parms *cipher_parms)
0833 {
0834 struct SPUHEADER *spuh;
0835 u32 protocol_bits = 0;
0836 u32 cipher_bits = 0;
0837 u32 ecf_bits = 0;
0838 u8 sctx_words = 0;
0839 u8 *ptr = spu_hdr;
0840
0841 flow_log("%s()\n", __func__);
0842 flow_log(" cipher alg:%u mode:%u type %u\n", cipher_parms->alg,
0843 cipher_parms->mode, cipher_parms->type);
0844 flow_log(" cipher_iv_len: %u\n", cipher_parms->iv_len);
0845 flow_log(" key: %d\n", cipher_parms->key_len);
0846 flow_dump(" key: ", cipher_parms->key_buf, cipher_parms->key_len);
0847
0848
0849 memset(spu_hdr, 0, sizeof(struct SPUHEADER));
0850 ptr += sizeof(struct SPUHEADER);
0851
0852
0853
0854 spuh = (struct SPUHEADER *)spu_hdr;
0855
0856 spuh->mh.op_code = SPU_CRYPTO_OPERATION_GENERIC;
0857 spuh->mh.flags |= (MH_SCTX_PRES | MH_BDESC_PRES | MH_BD_PRES);
0858
0859
0860 sctx_words = 3;
0861
0862
0863 if (cipher_parms->alg) {
0864 if (cipher_parms->key_len) {
0865 ptr += cipher_parms->key_len;
0866 sctx_words += cipher_parms->key_len / 4;
0867 }
0868
0869
0870
0871
0872
0873 if (cipher_parms->iv_len) {
0874
0875 ecf_bits |= SCTX_IV;
0876 ptr += cipher_parms->iv_len;
0877 sctx_words += cipher_parms->iv_len / 4;
0878 }
0879 }
0880
0881
0882 cipher_bits |= cipher_parms->alg << CIPHER_ALG_SHIFT;
0883 cipher_bits |= cipher_parms->mode << CIPHER_MODE_SHIFT;
0884 cipher_bits |= cipher_parms->type << CIPHER_TYPE_SHIFT;
0885
0886
0887 if (cipher_parms->alg && cipher_parms->key_len)
0888 memcpy(spuh + 1, cipher_parms->key_buf, cipher_parms->key_len);
0889
0890
0891 protocol_bits |= sctx_words;
0892
0893
0894 spuh->sa.proto_flags = cpu_to_be32(protocol_bits);
0895
0896
0897 spuh->sa.cipher_flags = cpu_to_be32(cipher_bits);
0898 spuh->sa.ecf = cpu_to_be32(ecf_bits);
0899
0900 packet_dump(" SPU request header: ", spu_hdr,
0901 sizeof(struct SPUHEADER));
0902
0903 return sizeof(struct SPUHEADER) + cipher_parms->key_len +
0904 cipher_parms->iv_len + sizeof(struct BDESC_HEADER) +
0905 sizeof(struct BD_HEADER);
0906 }
0907
0908
0909
0910
0911
0912
0913
0914
0915
0916
0917
0918
0919
0920
0921
0922 void spum_cipher_req_finish(u8 *spu_hdr,
0923 u16 spu_req_hdr_len,
0924 unsigned int is_inbound,
0925 struct spu_cipher_parms *cipher_parms,
0926 unsigned int data_size)
0927 {
0928 struct SPUHEADER *spuh;
0929 struct BDESC_HEADER *bdesc;
0930 struct BD_HEADER *bd;
0931 u8 *bdesc_ptr = spu_hdr + spu_req_hdr_len -
0932 (sizeof(struct BD_HEADER) + sizeof(struct BDESC_HEADER));
0933
0934 u32 cipher_bits;
0935
0936 flow_log("%s()\n", __func__);
0937 flow_log(" in: %u\n", is_inbound);
0938 flow_log(" cipher alg: %u, cipher_type: %u\n", cipher_parms->alg,
0939 cipher_parms->type);
0940
0941
0942
0943
0944
0945
0946
0947
0948 if (cipher_parms->mode == CIPHER_MODE_XTS)
0949 memset(cipher_parms->iv_buf, 0, cipher_parms->iv_len);
0950
0951 flow_log(" iv len: %d\n", cipher_parms->iv_len);
0952 flow_dump(" iv: ", cipher_parms->iv_buf, cipher_parms->iv_len);
0953 flow_log(" data_size: %u\n", data_size);
0954
0955
0956
0957 spuh = (struct SPUHEADER *)spu_hdr;
0958
0959
0960 cipher_bits = be32_to_cpu(spuh->sa.cipher_flags);
0961
0962
0963 if (is_inbound)
0964 cipher_bits |= CIPHER_INBOUND;
0965 else
0966 cipher_bits &= ~CIPHER_INBOUND;
0967
0968 if (cipher_parms->alg && cipher_parms->iv_buf && cipher_parms->iv_len)
0969
0970 memcpy(bdesc_ptr - cipher_parms->iv_len, cipher_parms->iv_buf,
0971 cipher_parms->iv_len);
0972
0973 spuh->sa.cipher_flags = cpu_to_be32(cipher_bits);
0974
0975
0976 bdesc = (struct BDESC_HEADER *)bdesc_ptr;
0977 bdesc->offset_mac = 0;
0978 bdesc->length_mac = 0;
0979 bdesc->offset_crypto = 0;
0980
0981
0982 if (cipher_parms->mode == CIPHER_MODE_XTS)
0983 bdesc->length_crypto = cpu_to_be16(data_size +
0984 SPU_XTS_TWEAK_SIZE);
0985 else
0986 bdesc->length_crypto = cpu_to_be16(data_size);
0987
0988 bdesc->offset_icv = 0;
0989 bdesc->offset_iv = 0;
0990
0991
0992
0993
0994
0995 bd = (struct BD_HEADER *)(bdesc_ptr + sizeof(struct BDESC_HEADER));
0996 bd->size = cpu_to_be16(data_size);
0997
0998
0999 if (cipher_parms->mode == CIPHER_MODE_XTS)
1000 bd->size = cpu_to_be16(data_size + SPU_XTS_TWEAK_SIZE);
1001 else
1002 bd->size = cpu_to_be16(data_size);
1003
1004 bd->prev_length = 0;
1005
1006 packet_dump(" SPU request header: ", spu_hdr, spu_req_hdr_len);
1007 }
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025 void spum_request_pad(u8 *pad_start,
1026 u32 gcm_ccm_padding,
1027 u32 hash_pad_len,
1028 enum hash_alg auth_alg,
1029 enum hash_mode auth_mode,
1030 unsigned int total_sent, u32 status_padding)
1031 {
1032 u8 *ptr = pad_start;
1033
1034
1035 if (gcm_ccm_padding > 0) {
1036 flow_log(" GCM: padding to 16 byte alignment: %u bytes\n",
1037 gcm_ccm_padding);
1038 memset(ptr, 0, gcm_ccm_padding);
1039 ptr += gcm_ccm_padding;
1040 }
1041
1042 if (hash_pad_len > 0) {
1043
1044 memset(ptr, 0, hash_pad_len);
1045
1046 if ((auth_alg == HASH_ALG_AES) &&
1047 (auth_mode == HASH_MODE_XCBC)) {
1048
1049 ptr += hash_pad_len;
1050 } else {
1051
1052 *ptr = 0x80;
1053 ptr += (hash_pad_len - sizeof(u64));
1054
1055
1056 if (auth_alg == HASH_ALG_MD5)
1057 *(__le64 *)ptr = cpu_to_le64(total_sent * 8ull);
1058 else
1059 *(__be64 *)ptr = cpu_to_be64(total_sent * 8ull);
1060 ptr += sizeof(u64);
1061 }
1062 }
1063
1064
1065 if (status_padding > 0) {
1066 flow_log(" STAT: padding to 4 byte alignment: %u bytes\n",
1067 status_padding);
1068
1069 memset(ptr, 0, status_padding);
1070 ptr += status_padding;
1071 }
1072 }
1073
1074
1075
1076
1077
1078
1079
1080 u8 spum_xts_tweak_in_payload(void)
1081 {
1082 return 1;
1083 }
1084
1085
1086
1087
1088
1089
1090
1091 u8 spum_tx_status_len(void)
1092 {
1093 return SPU_TX_STATUS_LEN;
1094 }
1095
1096
1097
1098
1099
1100
1101
1102 u8 spum_rx_status_len(void)
1103 {
1104 return SPU_RX_STATUS_LEN;
1105 }
1106
1107
1108
1109
1110
1111
1112
1113
1114 int spum_status_process(u8 *statp)
1115 {
1116 u32 status;
1117
1118 status = __be32_to_cpu(*(__be32 *)statp);
1119 flow_log("SPU response STATUS %#08x\n", status);
1120 if (status & SPU_STATUS_ERROR_FLAG) {
1121 pr_err("%s() Warning: Error result from SPU: %#08x\n",
1122 __func__, status);
1123 if (status & SPU_STATUS_INVALID_ICV)
1124 return SPU_INVALID_ICV;
1125 return -EBADMSG;
1126 }
1127 return 0;
1128 }
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141 void spum_ccm_update_iv(unsigned int digestsize,
1142 struct spu_cipher_parms *cipher_parms,
1143 unsigned int assoclen,
1144 unsigned int chunksize,
1145 bool is_encrypt,
1146 bool is_esp)
1147 {
1148 u8 L;
1149 u8 mprime;
1150 u8 adata;
1151
1152 if (cipher_parms->iv_len != CCM_AES_IV_SIZE) {
1153 pr_err("%s(): Invalid IV len %d for CCM mode, should be %d\n",
1154 __func__, cipher_parms->iv_len, CCM_AES_IV_SIZE);
1155 return;
1156 }
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179 if (is_esp) {
1180 L = CCM_ESP_L_VALUE;
1181 } else {
1182
1183 L = ((cipher_parms->iv_buf[0] & CCM_B0_L_PRIME) >>
1184 CCM_B0_L_PRIME_SHIFT) + 1;
1185 }
1186
1187 mprime = (digestsize - 2) >> 1;
1188 adata = (assoclen > 0);
1189
1190 cipher_parms->iv_buf[0] = (adata << CCM_B0_ADATA_SHIFT) |
1191 (mprime << CCM_B0_M_PRIME_SHIFT) |
1192 ((L - 1) << CCM_B0_L_PRIME_SHIFT);
1193
1194
1195
1196
1197 if (!is_encrypt)
1198 chunksize -= digestsize;
1199
1200
1201 format_value_ccm(chunksize, &cipher_parms->iv_buf[15 - L + 1], L);
1202 }
1203
1204
1205
1206
1207
1208
1209
1210
1211 u32 spum_wordalign_padlen(u32 data_size)
1212 {
1213 return ((data_size + 3) & ~3) - data_size;
1214 }