0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 #include <linux/kernel.h>
0013 #include <linux/string.h>
0014
0015 #include "util.h"
0016 #include "spu.h"
0017 #include "spu2.h"
0018
0019 #define SPU2_TX_STATUS_LEN 0
0020
0021
0022
0023
0024
0025 #define SPU2_RX_STATUS_LEN 2
0026
0027 enum spu2_proto_sel {
0028 SPU2_PROTO_RESV = 0,
0029 SPU2_MACSEC_SECTAG8_ECB = 1,
0030 SPU2_MACSEC_SECTAG8_SCB = 2,
0031 SPU2_MACSEC_SECTAG16 = 3,
0032 SPU2_MACSEC_SECTAG16_8_XPN = 4,
0033 SPU2_IPSEC = 5,
0034 SPU2_IPSEC_ESN = 6,
0035 SPU2_TLS_CIPHER = 7,
0036 SPU2_TLS_AEAD = 8,
0037 SPU2_DTLS_CIPHER = 9,
0038 SPU2_DTLS_AEAD = 10
0039 };
0040
0041 static char *spu2_cipher_type_names[] = { "None", "AES128", "AES192", "AES256",
0042 "DES", "3DES"
0043 };
0044
0045 static char *spu2_cipher_mode_names[] = { "ECB", "CBC", "CTR", "CFB", "OFB",
0046 "XTS", "CCM", "GCM"
0047 };
0048
0049 static char *spu2_hash_type_names[] = { "None", "AES128", "AES192", "AES256",
0050 "Reserved", "Reserved", "MD5", "SHA1", "SHA224", "SHA256", "SHA384",
0051 "SHA512", "SHA512/224", "SHA512/256", "SHA3-224", "SHA3-256",
0052 "SHA3-384", "SHA3-512"
0053 };
0054
0055 static char *spu2_hash_mode_names[] = { "CMAC", "CBC-MAC", "XCBC-MAC", "HMAC",
0056 "Rabin", "CCM", "GCM", "Reserved"
0057 };
0058
0059 static char *spu2_ciph_type_name(enum spu2_cipher_type cipher_type)
0060 {
0061 if (cipher_type >= SPU2_CIPHER_TYPE_LAST)
0062 return "Reserved";
0063 return spu2_cipher_type_names[cipher_type];
0064 }
0065
0066 static char *spu2_ciph_mode_name(enum spu2_cipher_mode cipher_mode)
0067 {
0068 if (cipher_mode >= SPU2_CIPHER_MODE_LAST)
0069 return "Reserved";
0070 return spu2_cipher_mode_names[cipher_mode];
0071 }
0072
0073 static char *spu2_hash_type_name(enum spu2_hash_type hash_type)
0074 {
0075 if (hash_type >= SPU2_HASH_TYPE_LAST)
0076 return "Reserved";
0077 return spu2_hash_type_names[hash_type];
0078 }
0079
0080 static char *spu2_hash_mode_name(enum spu2_hash_mode hash_mode)
0081 {
0082 if (hash_mode >= SPU2_HASH_MODE_LAST)
0083 return "Reserved";
0084 return spu2_hash_mode_names[hash_mode];
0085 }
0086
0087
0088
0089
0090
0091 static int spu2_cipher_mode_xlate(enum spu_cipher_mode cipher_mode,
0092 enum spu2_cipher_mode *spu2_mode)
0093 {
0094 switch (cipher_mode) {
0095 case CIPHER_MODE_ECB:
0096 *spu2_mode = SPU2_CIPHER_MODE_ECB;
0097 break;
0098 case CIPHER_MODE_CBC:
0099 *spu2_mode = SPU2_CIPHER_MODE_CBC;
0100 break;
0101 case CIPHER_MODE_OFB:
0102 *spu2_mode = SPU2_CIPHER_MODE_OFB;
0103 break;
0104 case CIPHER_MODE_CFB:
0105 *spu2_mode = SPU2_CIPHER_MODE_CFB;
0106 break;
0107 case CIPHER_MODE_CTR:
0108 *spu2_mode = SPU2_CIPHER_MODE_CTR;
0109 break;
0110 case CIPHER_MODE_CCM:
0111 *spu2_mode = SPU2_CIPHER_MODE_CCM;
0112 break;
0113 case CIPHER_MODE_GCM:
0114 *spu2_mode = SPU2_CIPHER_MODE_GCM;
0115 break;
0116 case CIPHER_MODE_XTS:
0117 *spu2_mode = SPU2_CIPHER_MODE_XTS;
0118 break;
0119 default:
0120 return -EINVAL;
0121 }
0122 return 0;
0123 }
0124
0125
0126
0127
0128
0129
0130
0131
0132
0133
0134
0135
0136 static int spu2_cipher_xlate(enum spu_cipher_alg cipher_alg,
0137 enum spu_cipher_mode cipher_mode,
0138 enum spu_cipher_type cipher_type,
0139 enum spu2_cipher_type *spu2_type,
0140 enum spu2_cipher_mode *spu2_mode)
0141 {
0142 int err;
0143
0144 err = spu2_cipher_mode_xlate(cipher_mode, spu2_mode);
0145 if (err) {
0146 flow_log("Invalid cipher mode %d\n", cipher_mode);
0147 return err;
0148 }
0149
0150 switch (cipher_alg) {
0151 case CIPHER_ALG_NONE:
0152 *spu2_type = SPU2_CIPHER_TYPE_NONE;
0153 break;
0154 case CIPHER_ALG_RC4:
0155
0156 err = -EINVAL;
0157 *spu2_type = SPU2_CIPHER_TYPE_NONE;
0158 break;
0159 case CIPHER_ALG_DES:
0160 *spu2_type = SPU2_CIPHER_TYPE_DES;
0161 break;
0162 case CIPHER_ALG_3DES:
0163 *spu2_type = SPU2_CIPHER_TYPE_3DES;
0164 break;
0165 case CIPHER_ALG_AES:
0166 switch (cipher_type) {
0167 case CIPHER_TYPE_AES128:
0168 *spu2_type = SPU2_CIPHER_TYPE_AES128;
0169 break;
0170 case CIPHER_TYPE_AES192:
0171 *spu2_type = SPU2_CIPHER_TYPE_AES192;
0172 break;
0173 case CIPHER_TYPE_AES256:
0174 *spu2_type = SPU2_CIPHER_TYPE_AES256;
0175 break;
0176 default:
0177 err = -EINVAL;
0178 }
0179 break;
0180 case CIPHER_ALG_LAST:
0181 default:
0182 err = -EINVAL;
0183 break;
0184 }
0185
0186 if (err)
0187 flow_log("Invalid cipher alg %d or type %d\n",
0188 cipher_alg, cipher_type);
0189 return err;
0190 }
0191
0192
0193
0194
0195
0196 static int spu2_hash_mode_xlate(enum hash_mode hash_mode,
0197 enum spu2_hash_mode *spu2_mode)
0198 {
0199 switch (hash_mode) {
0200 case HASH_MODE_XCBC:
0201 *spu2_mode = SPU2_HASH_MODE_XCBC_MAC;
0202 break;
0203 case HASH_MODE_CMAC:
0204 *spu2_mode = SPU2_HASH_MODE_CMAC;
0205 break;
0206 case HASH_MODE_HMAC:
0207 *spu2_mode = SPU2_HASH_MODE_HMAC;
0208 break;
0209 case HASH_MODE_CCM:
0210 *spu2_mode = SPU2_HASH_MODE_CCM;
0211 break;
0212 case HASH_MODE_GCM:
0213 *spu2_mode = SPU2_HASH_MODE_GCM;
0214 break;
0215 default:
0216 return -EINVAL;
0217 }
0218 return 0;
0219 }
0220
0221
0222
0223
0224
0225
0226
0227
0228
0229
0230
0231
0232
0233 static int
0234 spu2_hash_xlate(enum hash_alg hash_alg, enum hash_mode hash_mode,
0235 enum hash_type hash_type, enum spu_cipher_type ciph_type,
0236 enum spu2_hash_type *spu2_type, enum spu2_hash_mode *spu2_mode)
0237 {
0238 int err;
0239
0240 err = spu2_hash_mode_xlate(hash_mode, spu2_mode);
0241 if (err) {
0242 flow_log("Invalid hash mode %d\n", hash_mode);
0243 return err;
0244 }
0245
0246 switch (hash_alg) {
0247 case HASH_ALG_NONE:
0248 *spu2_type = SPU2_HASH_TYPE_NONE;
0249 break;
0250 case HASH_ALG_MD5:
0251 *spu2_type = SPU2_HASH_TYPE_MD5;
0252 break;
0253 case HASH_ALG_SHA1:
0254 *spu2_type = SPU2_HASH_TYPE_SHA1;
0255 break;
0256 case HASH_ALG_SHA224:
0257 *spu2_type = SPU2_HASH_TYPE_SHA224;
0258 break;
0259 case HASH_ALG_SHA256:
0260 *spu2_type = SPU2_HASH_TYPE_SHA256;
0261 break;
0262 case HASH_ALG_SHA384:
0263 *spu2_type = SPU2_HASH_TYPE_SHA384;
0264 break;
0265 case HASH_ALG_SHA512:
0266 *spu2_type = SPU2_HASH_TYPE_SHA512;
0267 break;
0268 case HASH_ALG_AES:
0269 switch (ciph_type) {
0270 case CIPHER_TYPE_AES128:
0271 *spu2_type = SPU2_HASH_TYPE_AES128;
0272 break;
0273 case CIPHER_TYPE_AES192:
0274 *spu2_type = SPU2_HASH_TYPE_AES192;
0275 break;
0276 case CIPHER_TYPE_AES256:
0277 *spu2_type = SPU2_HASH_TYPE_AES256;
0278 break;
0279 default:
0280 err = -EINVAL;
0281 }
0282 break;
0283 case HASH_ALG_SHA3_224:
0284 *spu2_type = SPU2_HASH_TYPE_SHA3_224;
0285 break;
0286 case HASH_ALG_SHA3_256:
0287 *spu2_type = SPU2_HASH_TYPE_SHA3_256;
0288 break;
0289 case HASH_ALG_SHA3_384:
0290 *spu2_type = SPU2_HASH_TYPE_SHA3_384;
0291 break;
0292 case HASH_ALG_SHA3_512:
0293 *spu2_type = SPU2_HASH_TYPE_SHA3_512;
0294 break;
0295 case HASH_ALG_LAST:
0296 default:
0297 err = -EINVAL;
0298 break;
0299 }
0300
0301 if (err)
0302 flow_log("Invalid hash alg %d or type %d\n",
0303 hash_alg, hash_type);
0304 return err;
0305 }
0306
0307
0308 static void spu2_dump_fmd_ctrl0(u64 ctrl0)
0309 {
0310 enum spu2_cipher_type ciph_type;
0311 enum spu2_cipher_mode ciph_mode;
0312 enum spu2_hash_type hash_type;
0313 enum spu2_hash_mode hash_mode;
0314 char *ciph_name;
0315 char *ciph_mode_name;
0316 char *hash_name;
0317 char *hash_mode_name;
0318 u8 cfb;
0319 u8 proto;
0320
0321 packet_log(" FMD CTRL0 %#16llx\n", ctrl0);
0322 if (ctrl0 & SPU2_CIPH_ENCRYPT_EN)
0323 packet_log(" encrypt\n");
0324 else
0325 packet_log(" decrypt\n");
0326
0327 ciph_type = (ctrl0 & SPU2_CIPH_TYPE) >> SPU2_CIPH_TYPE_SHIFT;
0328 ciph_name = spu2_ciph_type_name(ciph_type);
0329 packet_log(" Cipher type: %s\n", ciph_name);
0330
0331 if (ciph_type != SPU2_CIPHER_TYPE_NONE) {
0332 ciph_mode = (ctrl0 & SPU2_CIPH_MODE) >> SPU2_CIPH_MODE_SHIFT;
0333 ciph_mode_name = spu2_ciph_mode_name(ciph_mode);
0334 packet_log(" Cipher mode: %s\n", ciph_mode_name);
0335 }
0336
0337 cfb = (ctrl0 & SPU2_CFB_MASK) >> SPU2_CFB_MASK_SHIFT;
0338 packet_log(" CFB %#x\n", cfb);
0339
0340 proto = (ctrl0 & SPU2_PROTO_SEL) >> SPU2_PROTO_SEL_SHIFT;
0341 packet_log(" protocol %#x\n", proto);
0342
0343 if (ctrl0 & SPU2_HASH_FIRST)
0344 packet_log(" hash first\n");
0345 else
0346 packet_log(" cipher first\n");
0347
0348 if (ctrl0 & SPU2_CHK_TAG)
0349 packet_log(" check tag\n");
0350
0351 hash_type = (ctrl0 & SPU2_HASH_TYPE) >> SPU2_HASH_TYPE_SHIFT;
0352 hash_name = spu2_hash_type_name(hash_type);
0353 packet_log(" Hash type: %s\n", hash_name);
0354
0355 if (hash_type != SPU2_HASH_TYPE_NONE) {
0356 hash_mode = (ctrl0 & SPU2_HASH_MODE) >> SPU2_HASH_MODE_SHIFT;
0357 hash_mode_name = spu2_hash_mode_name(hash_mode);
0358 packet_log(" Hash mode: %s\n", hash_mode_name);
0359 }
0360
0361 if (ctrl0 & SPU2_CIPH_PAD_EN) {
0362 packet_log(" Cipher pad: %#2llx\n",
0363 (ctrl0 & SPU2_CIPH_PAD) >> SPU2_CIPH_PAD_SHIFT);
0364 }
0365 }
0366
0367
0368 static void spu2_dump_fmd_ctrl1(u64 ctrl1)
0369 {
0370 u8 hash_key_len;
0371 u8 ciph_key_len;
0372 u8 ret_iv_len;
0373 u8 iv_offset;
0374 u8 iv_len;
0375 u8 hash_tag_len;
0376 u8 ret_md;
0377
0378 packet_log(" FMD CTRL1 %#16llx\n", ctrl1);
0379 if (ctrl1 & SPU2_TAG_LOC)
0380 packet_log(" Tag after payload\n");
0381
0382 packet_log(" Msg includes ");
0383 if (ctrl1 & SPU2_HAS_FR_DATA)
0384 packet_log("FD ");
0385 if (ctrl1 & SPU2_HAS_AAD1)
0386 packet_log("AAD1 ");
0387 if (ctrl1 & SPU2_HAS_NAAD)
0388 packet_log("NAAD ");
0389 if (ctrl1 & SPU2_HAS_AAD2)
0390 packet_log("AAD2 ");
0391 if (ctrl1 & SPU2_HAS_ESN)
0392 packet_log("ESN ");
0393 packet_log("\n");
0394
0395 hash_key_len = (ctrl1 & SPU2_HASH_KEY_LEN) >> SPU2_HASH_KEY_LEN_SHIFT;
0396 packet_log(" Hash key len %u\n", hash_key_len);
0397
0398 ciph_key_len = (ctrl1 & SPU2_CIPH_KEY_LEN) >> SPU2_CIPH_KEY_LEN_SHIFT;
0399 packet_log(" Cipher key len %u\n", ciph_key_len);
0400
0401 if (ctrl1 & SPU2_GENIV)
0402 packet_log(" Generate IV\n");
0403
0404 if (ctrl1 & SPU2_HASH_IV)
0405 packet_log(" IV included in hash\n");
0406
0407 if (ctrl1 & SPU2_RET_IV)
0408 packet_log(" Return IV in output before payload\n");
0409
0410 ret_iv_len = (ctrl1 & SPU2_RET_IV_LEN) >> SPU2_RET_IV_LEN_SHIFT;
0411 packet_log(" Length of returned IV %u bytes\n",
0412 ret_iv_len ? ret_iv_len : 16);
0413
0414 iv_offset = (ctrl1 & SPU2_IV_OFFSET) >> SPU2_IV_OFFSET_SHIFT;
0415 packet_log(" IV offset %u\n", iv_offset);
0416
0417 iv_len = (ctrl1 & SPU2_IV_LEN) >> SPU2_IV_LEN_SHIFT;
0418 packet_log(" Input IV len %u bytes\n", iv_len);
0419
0420 hash_tag_len = (ctrl1 & SPU2_HASH_TAG_LEN) >> SPU2_HASH_TAG_LEN_SHIFT;
0421 packet_log(" Hash tag length %u bytes\n", hash_tag_len);
0422
0423 packet_log(" Return ");
0424 ret_md = (ctrl1 & SPU2_RETURN_MD) >> SPU2_RETURN_MD_SHIFT;
0425 if (ret_md)
0426 packet_log("FMD ");
0427 if (ret_md == SPU2_RET_FMD_OMD)
0428 packet_log("OMD ");
0429 else if (ret_md == SPU2_RET_FMD_OMD_IV)
0430 packet_log("OMD IV ");
0431 if (ctrl1 & SPU2_RETURN_FD)
0432 packet_log("FD ");
0433 if (ctrl1 & SPU2_RETURN_AAD1)
0434 packet_log("AAD1 ");
0435 if (ctrl1 & SPU2_RETURN_NAAD)
0436 packet_log("NAAD ");
0437 if (ctrl1 & SPU2_RETURN_AAD2)
0438 packet_log("AAD2 ");
0439 if (ctrl1 & SPU2_RETURN_PAY)
0440 packet_log("Payload");
0441 packet_log("\n");
0442 }
0443
0444
0445 static void spu2_dump_fmd_ctrl2(u64 ctrl2)
0446 {
0447 packet_log(" FMD CTRL2 %#16llx\n", ctrl2);
0448
0449 packet_log(" AAD1 offset %llu length %llu bytes\n",
0450 ctrl2 & SPU2_AAD1_OFFSET,
0451 (ctrl2 & SPU2_AAD1_LEN) >> SPU2_AAD1_LEN_SHIFT);
0452 packet_log(" AAD2 offset %llu\n",
0453 (ctrl2 & SPU2_AAD2_OFFSET) >> SPU2_AAD2_OFFSET_SHIFT);
0454 packet_log(" Payload offset %llu\n",
0455 (ctrl2 & SPU2_PL_OFFSET) >> SPU2_PL_OFFSET_SHIFT);
0456 }
0457
0458
0459 static void spu2_dump_fmd_ctrl3(u64 ctrl3)
0460 {
0461 packet_log(" FMD CTRL3 %#16llx\n", ctrl3);
0462
0463 packet_log(" Payload length %llu bytes\n", ctrl3 & SPU2_PL_LEN);
0464 packet_log(" TLS length %llu bytes\n",
0465 (ctrl3 & SPU2_TLS_LEN) >> SPU2_TLS_LEN_SHIFT);
0466 }
0467
0468 static void spu2_dump_fmd(struct SPU2_FMD *fmd)
0469 {
0470 spu2_dump_fmd_ctrl0(le64_to_cpu(fmd->ctrl0));
0471 spu2_dump_fmd_ctrl1(le64_to_cpu(fmd->ctrl1));
0472 spu2_dump_fmd_ctrl2(le64_to_cpu(fmd->ctrl2));
0473 spu2_dump_fmd_ctrl3(le64_to_cpu(fmd->ctrl3));
0474 }
0475
0476 static void spu2_dump_omd(u8 *omd, u16 hash_key_len, u16 ciph_key_len,
0477 u16 hash_iv_len, u16 ciph_iv_len)
0478 {
0479 u8 *ptr = omd;
0480
0481 packet_log(" OMD:\n");
0482
0483 if (hash_key_len) {
0484 packet_log(" Hash Key Length %u bytes\n", hash_key_len);
0485 packet_dump(" KEY: ", ptr, hash_key_len);
0486 ptr += hash_key_len;
0487 }
0488
0489 if (ciph_key_len) {
0490 packet_log(" Cipher Key Length %u bytes\n", ciph_key_len);
0491 packet_dump(" KEY: ", ptr, ciph_key_len);
0492 ptr += ciph_key_len;
0493 }
0494
0495 if (hash_iv_len) {
0496 packet_log(" Hash IV Length %u bytes\n", hash_iv_len);
0497 packet_dump(" hash IV: ", ptr, hash_iv_len);
0498 ptr += ciph_key_len;
0499 }
0500
0501 if (ciph_iv_len) {
0502 packet_log(" Cipher IV Length %u bytes\n", ciph_iv_len);
0503 packet_dump(" cipher IV: ", ptr, ciph_iv_len);
0504 }
0505 }
0506
0507
0508 void spu2_dump_msg_hdr(u8 *buf, unsigned int buf_len)
0509 {
0510 struct SPU2_FMD *fmd = (struct SPU2_FMD *)buf;
0511 u8 *omd;
0512 u64 ctrl1;
0513 u16 hash_key_len;
0514 u16 ciph_key_len;
0515 u16 hash_iv_len;
0516 u16 ciph_iv_len;
0517 u16 omd_len;
0518
0519 packet_log("\n");
0520 packet_log("SPU2 message header %p len: %u\n", buf, buf_len);
0521
0522 spu2_dump_fmd(fmd);
0523 omd = (u8 *)(fmd + 1);
0524
0525 ctrl1 = le64_to_cpu(fmd->ctrl1);
0526 hash_key_len = (ctrl1 & SPU2_HASH_KEY_LEN) >> SPU2_HASH_KEY_LEN_SHIFT;
0527 ciph_key_len = (ctrl1 & SPU2_CIPH_KEY_LEN) >> SPU2_CIPH_KEY_LEN_SHIFT;
0528 hash_iv_len = 0;
0529 ciph_iv_len = (ctrl1 & SPU2_IV_LEN) >> SPU2_IV_LEN_SHIFT;
0530 spu2_dump_omd(omd, hash_key_len, ciph_key_len, hash_iv_len,
0531 ciph_iv_len);
0532
0533
0534 omd_len = hash_key_len + ciph_key_len + hash_iv_len + ciph_iv_len;
0535 if (FMD_SIZE + omd_len != buf_len) {
0536 packet_log
0537 (" Packet parsed incorrectly. buf_len %u, sum of MD %zu\n",
0538 buf_len, FMD_SIZE + omd_len);
0539 }
0540 packet_log("\n");
0541 }
0542
0543
0544
0545
0546
0547
0548
0549
0550
0551
0552
0553
0554 static int spu2_fmd_init(struct SPU2_FMD *fmd,
0555 enum spu2_cipher_type spu2_type,
0556 enum spu2_cipher_mode spu2_mode,
0557 u32 cipher_key_len, u32 cipher_iv_len)
0558 {
0559 u64 ctrl0;
0560 u64 ctrl1;
0561 u64 ctrl2;
0562 u64 ctrl3;
0563 u32 aad1_offset;
0564 u32 aad2_offset;
0565 u16 aad1_len = 0;
0566 u64 payload_offset;
0567
0568 ctrl0 = (spu2_type << SPU2_CIPH_TYPE_SHIFT) |
0569 (spu2_mode << SPU2_CIPH_MODE_SHIFT);
0570
0571 ctrl1 = (cipher_key_len << SPU2_CIPH_KEY_LEN_SHIFT) |
0572 ((u64)cipher_iv_len << SPU2_IV_LEN_SHIFT) |
0573 ((u64)SPU2_RET_FMD_ONLY << SPU2_RETURN_MD_SHIFT) | SPU2_RETURN_PAY;
0574
0575
0576
0577
0578
0579 aad1_offset = 0;
0580 aad2_offset = aad1_offset;
0581 payload_offset = 0;
0582 ctrl2 = aad1_offset |
0583 (aad1_len << SPU2_AAD1_LEN_SHIFT) |
0584 (aad2_offset << SPU2_AAD2_OFFSET_SHIFT) |
0585 (payload_offset << SPU2_PL_OFFSET_SHIFT);
0586
0587 ctrl3 = 0;
0588
0589 fmd->ctrl0 = cpu_to_le64(ctrl0);
0590 fmd->ctrl1 = cpu_to_le64(ctrl1);
0591 fmd->ctrl2 = cpu_to_le64(ctrl2);
0592 fmd->ctrl3 = cpu_to_le64(ctrl3);
0593
0594 return 0;
0595 }
0596
0597
0598
0599
0600
0601
0602
0603
0604
0605
0606
0607
0608
0609 static void spu2_fmd_ctrl0_write(struct SPU2_FMD *fmd,
0610 bool is_inbound, bool auth_first,
0611 enum spu2_proto_sel protocol,
0612 enum spu2_cipher_type cipher_type,
0613 enum spu2_cipher_mode cipher_mode,
0614 enum spu2_hash_type auth_type,
0615 enum spu2_hash_mode auth_mode)
0616 {
0617 u64 ctrl0 = 0;
0618
0619 if ((cipher_type != SPU2_CIPHER_TYPE_NONE) && !is_inbound)
0620 ctrl0 |= SPU2_CIPH_ENCRYPT_EN;
0621
0622 ctrl0 |= ((u64)cipher_type << SPU2_CIPH_TYPE_SHIFT) |
0623 ((u64)cipher_mode << SPU2_CIPH_MODE_SHIFT);
0624
0625 if (protocol)
0626 ctrl0 |= (u64)protocol << SPU2_PROTO_SEL_SHIFT;
0627
0628 if (auth_first)
0629 ctrl0 |= SPU2_HASH_FIRST;
0630
0631 if (is_inbound && (auth_type != SPU2_HASH_TYPE_NONE))
0632 ctrl0 |= SPU2_CHK_TAG;
0633
0634 ctrl0 |= (((u64)auth_type << SPU2_HASH_TYPE_SHIFT) |
0635 ((u64)auth_mode << SPU2_HASH_MODE_SHIFT));
0636
0637 fmd->ctrl0 = cpu_to_le64(ctrl0);
0638 }
0639
0640
0641
0642
0643
0644
0645
0646
0647
0648
0649
0650
0651
0652
0653
0654
0655
0656
0657
0658
0659
0660
0661 static void spu2_fmd_ctrl1_write(struct SPU2_FMD *fmd, bool is_inbound,
0662 u64 assoc_size,
0663 u64 auth_key_len, u64 cipher_key_len,
0664 bool gen_iv, bool hash_iv, bool return_iv,
0665 u64 ret_iv_len, u64 ret_iv_offset,
0666 u64 cipher_iv_len, u64 digest_size,
0667 bool return_payload, bool return_md)
0668 {
0669 u64 ctrl1 = 0;
0670
0671 if (is_inbound && digest_size)
0672 ctrl1 |= SPU2_TAG_LOC;
0673
0674 if (assoc_size) {
0675 ctrl1 |= SPU2_HAS_AAD2;
0676 ctrl1 |= SPU2_RETURN_AAD2;
0677 }
0678
0679 if (auth_key_len)
0680 ctrl1 |= ((auth_key_len << SPU2_HASH_KEY_LEN_SHIFT) &
0681 SPU2_HASH_KEY_LEN);
0682
0683 if (cipher_key_len)
0684 ctrl1 |= ((cipher_key_len << SPU2_CIPH_KEY_LEN_SHIFT) &
0685 SPU2_CIPH_KEY_LEN);
0686
0687 if (gen_iv)
0688 ctrl1 |= SPU2_GENIV;
0689
0690 if (hash_iv)
0691 ctrl1 |= SPU2_HASH_IV;
0692
0693 if (return_iv) {
0694 ctrl1 |= SPU2_RET_IV;
0695 ctrl1 |= ret_iv_len << SPU2_RET_IV_LEN_SHIFT;
0696 ctrl1 |= ret_iv_offset << SPU2_IV_OFFSET_SHIFT;
0697 }
0698
0699 ctrl1 |= ((cipher_iv_len << SPU2_IV_LEN_SHIFT) & SPU2_IV_LEN);
0700
0701 if (digest_size)
0702 ctrl1 |= ((digest_size << SPU2_HASH_TAG_LEN_SHIFT) &
0703 SPU2_HASH_TAG_LEN);
0704
0705
0706
0707
0708 if (return_md)
0709 ctrl1 |= ((u64)SPU2_RET_FMD_ONLY << SPU2_RETURN_MD_SHIFT);
0710 else
0711 ctrl1 |= ((u64)SPU2_RET_NO_MD << SPU2_RETURN_MD_SHIFT);
0712
0713
0714
0715 if (return_payload)
0716 ctrl1 |= SPU2_RETURN_PAY;
0717
0718 fmd->ctrl1 = cpu_to_le64(ctrl1);
0719 }
0720
0721
0722
0723
0724
0725
0726
0727
0728
0729
0730
0731
0732 static void spu2_fmd_ctrl2_write(struct SPU2_FMD *fmd, u64 cipher_offset,
0733 u64 auth_key_len, u64 auth_iv_len,
0734 u64 cipher_key_len, u64 cipher_iv_len)
0735 {
0736 u64 ctrl2;
0737 u64 aad1_offset;
0738 u64 aad2_offset;
0739 u16 aad1_len = 0;
0740 u64 payload_offset;
0741
0742
0743 aad1_offset = 0;
0744
0745 aad2_offset = aad1_offset;
0746 payload_offset = cipher_offset;
0747 ctrl2 = aad1_offset |
0748 (aad1_len << SPU2_AAD1_LEN_SHIFT) |
0749 (aad2_offset << SPU2_AAD2_OFFSET_SHIFT) |
0750 (payload_offset << SPU2_PL_OFFSET_SHIFT);
0751
0752 fmd->ctrl2 = cpu_to_le64(ctrl2);
0753 }
0754
0755
0756
0757
0758
0759
0760 static void spu2_fmd_ctrl3_write(struct SPU2_FMD *fmd, u64 payload_len)
0761 {
0762 u64 ctrl3;
0763
0764 ctrl3 = payload_len & SPU2_PL_LEN;
0765
0766 fmd->ctrl3 = cpu_to_le64(ctrl3);
0767 }
0768
0769
0770
0771
0772
0773
0774
0775
0776
0777
0778
0779
0780
0781
0782
0783 u32 spu2_ctx_max_payload(enum spu_cipher_alg cipher_alg,
0784 enum spu_cipher_mode cipher_mode,
0785 unsigned int blocksize)
0786 {
0787 if ((cipher_alg == CIPHER_ALG_AES) &&
0788 (cipher_mode == CIPHER_MODE_CCM)) {
0789 u32 excess = SPU2_MAX_PAYLOAD % blocksize;
0790
0791 return SPU2_MAX_PAYLOAD - excess;
0792 } else {
0793 return SPU_MAX_PAYLOAD_INF;
0794 }
0795 }
0796
0797
0798
0799
0800
0801
0802
0803
0804 u32 spu2_payload_length(u8 *spu_hdr)
0805 {
0806 struct SPU2_FMD *fmd = (struct SPU2_FMD *)spu_hdr;
0807 u32 pl_len;
0808 u64 ctrl3;
0809
0810 ctrl3 = le64_to_cpu(fmd->ctrl3);
0811 pl_len = ctrl3 & SPU2_PL_LEN;
0812
0813 return pl_len;
0814 }
0815
0816
0817
0818
0819
0820
0821
0822
0823
0824
0825
0826
0827 u16 spu2_response_hdr_len(u16 auth_key_len, u16 enc_key_len, bool is_hash)
0828 {
0829 return FMD_SIZE;
0830 }
0831
0832
0833
0834
0835
0836
0837
0838
0839
0840
0841
0842
0843
0844 u16 spu2_hash_pad_len(enum hash_alg hash_alg, enum hash_mode hash_mode,
0845 u32 chunksize, u16 hash_block_size)
0846 {
0847 return 0;
0848 }
0849
0850
0851
0852
0853
0854
0855
0856
0857
0858 u32 spu2_gcm_ccm_pad_len(enum spu_cipher_mode cipher_mode,
0859 unsigned int data_size)
0860 {
0861 return 0;
0862 }
0863
0864
0865
0866
0867
0868
0869
0870
0871
0872
0873
0874 u32 spu2_assoc_resp_len(enum spu_cipher_mode cipher_mode,
0875 unsigned int assoc_len, unsigned int iv_len,
0876 bool is_encrypt)
0877 {
0878 u32 resp_len = assoc_len;
0879
0880 if (is_encrypt)
0881
0882 resp_len += iv_len;
0883 return resp_len;
0884 }
0885
0886
0887
0888
0889
0890
0891
0892
0893
0894
0895
0896
0897 u8 spu2_aead_ivlen(enum spu_cipher_mode cipher_mode, u16 iv_len)
0898 {
0899 return 0;
0900 }
0901
0902
0903
0904
0905
0906
0907
0908
0909 enum hash_type spu2_hash_type(u32 src_sent)
0910 {
0911 return HASH_TYPE_FULL;
0912 }
0913
0914
0915
0916
0917
0918
0919
0920
0921
0922 u32 spu2_digest_size(u32 alg_digest_size, enum hash_alg alg,
0923 enum hash_type htype)
0924 {
0925 return alg_digest_size;
0926 }
0927
0928
0929
0930
0931
0932
0933
0934
0935
0936
0937
0938
0939
0940
0941
0942
0943
0944 u32 spu2_create_request(u8 *spu_hdr,
0945 struct spu_request_opts *req_opts,
0946 struct spu_cipher_parms *cipher_parms,
0947 struct spu_hash_parms *hash_parms,
0948 struct spu_aead_parms *aead_parms,
0949 unsigned int data_size)
0950 {
0951 struct SPU2_FMD *fmd;
0952 u8 *ptr;
0953 unsigned int buf_len;
0954 int err;
0955 enum spu2_cipher_type spu2_ciph_type = SPU2_CIPHER_TYPE_NONE;
0956 enum spu2_cipher_mode spu2_ciph_mode;
0957 enum spu2_hash_type spu2_auth_type = SPU2_HASH_TYPE_NONE;
0958 enum spu2_hash_mode spu2_auth_mode;
0959 bool return_md = true;
0960 enum spu2_proto_sel proto = SPU2_PROTO_RESV;
0961
0962
0963 unsigned int payload_len =
0964 hash_parms->prebuf_len + data_size + hash_parms->pad_len -
0965 ((req_opts->is_aead && req_opts->is_inbound) ?
0966 hash_parms->digestsize : 0);
0967
0968
0969 unsigned int cipher_offset = aead_parms->assoc_size +
0970 aead_parms->aad_pad_len + aead_parms->iv_len;
0971
0972
0973 unsigned int real_db_size = spu_real_db_size(aead_parms->assoc_size,
0974 aead_parms->iv_len,
0975 hash_parms->prebuf_len,
0976 data_size,
0977 aead_parms->aad_pad_len,
0978 aead_parms->data_pad_len,
0979 hash_parms->pad_len);
0980 unsigned int assoc_size = aead_parms->assoc_size;
0981
0982 if (req_opts->is_aead &&
0983 (cipher_parms->alg == CIPHER_ALG_AES) &&
0984 (cipher_parms->mode == CIPHER_MODE_GCM))
0985
0986
0987
0988
0989 req_opts->auth_first = req_opts->is_inbound;
0990
0991
0992 if (req_opts->is_aead &&
0993 (cipher_parms->alg == CIPHER_ALG_AES) &&
0994 (cipher_parms->mode == CIPHER_MODE_CCM))
0995 req_opts->auth_first = !req_opts->is_inbound;
0996
0997 flow_log("%s()\n", __func__);
0998 flow_log(" in:%u authFirst:%u\n",
0999 req_opts->is_inbound, req_opts->auth_first);
1000 flow_log(" cipher alg:%u mode:%u type %u\n", cipher_parms->alg,
1001 cipher_parms->mode, cipher_parms->type);
1002 flow_log(" is_esp: %s\n", req_opts->is_esp ? "yes" : "no");
1003 flow_log(" key: %d\n", cipher_parms->key_len);
1004 flow_dump(" key: ", cipher_parms->key_buf, cipher_parms->key_len);
1005 flow_log(" iv: %d\n", cipher_parms->iv_len);
1006 flow_dump(" iv: ", cipher_parms->iv_buf, cipher_parms->iv_len);
1007 flow_log(" auth alg:%u mode:%u type %u\n",
1008 hash_parms->alg, hash_parms->mode, hash_parms->type);
1009 flow_log(" digestsize: %u\n", hash_parms->digestsize);
1010 flow_log(" authkey: %d\n", hash_parms->key_len);
1011 flow_dump(" authkey: ", hash_parms->key_buf, hash_parms->key_len);
1012 flow_log(" assoc_size:%u\n", assoc_size);
1013 flow_log(" prebuf_len:%u\n", hash_parms->prebuf_len);
1014 flow_log(" data_size:%u\n", data_size);
1015 flow_log(" hash_pad_len:%u\n", hash_parms->pad_len);
1016 flow_log(" real_db_size:%u\n", real_db_size);
1017 flow_log(" cipher_offset:%u payload_len:%u\n",
1018 cipher_offset, payload_len);
1019 flow_log(" aead_iv: %u\n", aead_parms->iv_len);
1020
1021
1022 err = spu2_cipher_xlate(cipher_parms->alg, cipher_parms->mode,
1023 cipher_parms->type,
1024 &spu2_ciph_type, &spu2_ciph_mode);
1025
1026
1027
1028
1029
1030
1031
1032 if ((req_opts->is_rfc4543) ||
1033 ((spu2_ciph_mode == SPU2_CIPHER_MODE_GCM) &&
1034 (payload_len == 0))) {
1035
1036 spu2_ciph_type = SPU2_CIPHER_TYPE_NONE;
1037 hash_parms->key_len = cipher_parms->key_len;
1038 memcpy(hash_parms->key_buf, cipher_parms->key_buf,
1039 cipher_parms->key_len);
1040 cipher_parms->key_len = 0;
1041
1042 if (req_opts->is_rfc4543)
1043 payload_len += assoc_size;
1044 else
1045 payload_len = assoc_size;
1046 cipher_offset = 0;
1047 assoc_size = 0;
1048 }
1049
1050 if (err)
1051 return 0;
1052
1053 flow_log("spu2 cipher type %s, cipher mode %s\n",
1054 spu2_ciph_type_name(spu2_ciph_type),
1055 spu2_ciph_mode_name(spu2_ciph_mode));
1056
1057 err = spu2_hash_xlate(hash_parms->alg, hash_parms->mode,
1058 hash_parms->type,
1059 cipher_parms->type,
1060 &spu2_auth_type, &spu2_auth_mode);
1061 if (err)
1062 return 0;
1063
1064 flow_log("spu2 hash type %s, hash mode %s\n",
1065 spu2_hash_type_name(spu2_auth_type),
1066 spu2_hash_mode_name(spu2_auth_mode));
1067
1068 fmd = (struct SPU2_FMD *)spu_hdr;
1069
1070 spu2_fmd_ctrl0_write(fmd, req_opts->is_inbound, req_opts->auth_first,
1071 proto, spu2_ciph_type, spu2_ciph_mode,
1072 spu2_auth_type, spu2_auth_mode);
1073
1074 spu2_fmd_ctrl1_write(fmd, req_opts->is_inbound, assoc_size,
1075 hash_parms->key_len, cipher_parms->key_len,
1076 false, false,
1077 aead_parms->return_iv, aead_parms->ret_iv_len,
1078 aead_parms->ret_iv_off,
1079 cipher_parms->iv_len, hash_parms->digestsize,
1080 !req_opts->bd_suppress, return_md);
1081
1082 spu2_fmd_ctrl2_write(fmd, cipher_offset, hash_parms->key_len, 0,
1083 cipher_parms->key_len, cipher_parms->iv_len);
1084
1085 spu2_fmd_ctrl3_write(fmd, payload_len);
1086
1087 ptr = (u8 *)(fmd + 1);
1088 buf_len = sizeof(struct SPU2_FMD);
1089
1090
1091 if (hash_parms->key_len) {
1092 memcpy(ptr, hash_parms->key_buf, hash_parms->key_len);
1093 ptr += hash_parms->key_len;
1094 buf_len += hash_parms->key_len;
1095 }
1096 if (cipher_parms->key_len) {
1097 memcpy(ptr, cipher_parms->key_buf, cipher_parms->key_len);
1098 ptr += cipher_parms->key_len;
1099 buf_len += cipher_parms->key_len;
1100 }
1101 if (cipher_parms->iv_len) {
1102 memcpy(ptr, cipher_parms->iv_buf, cipher_parms->iv_len);
1103 ptr += cipher_parms->iv_len;
1104 buf_len += cipher_parms->iv_len;
1105 }
1106
1107 packet_dump(" SPU request header: ", spu_hdr, buf_len);
1108
1109 return buf_len;
1110 }
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126 u16 spu2_cipher_req_init(u8 *spu_hdr, struct spu_cipher_parms *cipher_parms)
1127 {
1128 struct SPU2_FMD *fmd;
1129 u8 *omd;
1130 enum spu2_cipher_type spu2_type = SPU2_CIPHER_TYPE_NONE;
1131 enum spu2_cipher_mode spu2_mode;
1132 int err;
1133
1134 flow_log("%s()\n", __func__);
1135 flow_log(" cipher alg:%u mode:%u type %u\n", cipher_parms->alg,
1136 cipher_parms->mode, cipher_parms->type);
1137 flow_log(" cipher_iv_len: %u\n", cipher_parms->iv_len);
1138 flow_log(" key: %d\n", cipher_parms->key_len);
1139 flow_dump(" key: ", cipher_parms->key_buf, cipher_parms->key_len);
1140
1141
1142 err = spu2_cipher_xlate(cipher_parms->alg, cipher_parms->mode,
1143 cipher_parms->type, &spu2_type, &spu2_mode);
1144 if (err)
1145 return 0;
1146
1147 flow_log("spu2 cipher type %s, cipher mode %s\n",
1148 spu2_ciph_type_name(spu2_type),
1149 spu2_ciph_mode_name(spu2_mode));
1150
1151
1152 fmd = (struct SPU2_FMD *)spu_hdr;
1153 err = spu2_fmd_init(fmd, spu2_type, spu2_mode, cipher_parms->key_len,
1154 cipher_parms->iv_len);
1155 if (err)
1156 return 0;
1157
1158
1159 omd = (u8 *)(fmd + 1);
1160 if (cipher_parms->key_buf && cipher_parms->key_len)
1161 memcpy(omd, cipher_parms->key_buf, cipher_parms->key_len);
1162
1163 packet_dump(" SPU request header: ", spu_hdr,
1164 FMD_SIZE + cipher_parms->key_len + cipher_parms->iv_len);
1165
1166 return FMD_SIZE + cipher_parms->key_len + cipher_parms->iv_len;
1167 }
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182 void spu2_cipher_req_finish(u8 *spu_hdr,
1183 u16 spu_req_hdr_len,
1184 unsigned int is_inbound,
1185 struct spu_cipher_parms *cipher_parms,
1186 unsigned int data_size)
1187 {
1188 struct SPU2_FMD *fmd;
1189 u8 *omd;
1190 u64 ctrl0;
1191 u64 ctrl3;
1192
1193 flow_log("%s()\n", __func__);
1194 flow_log(" in: %u\n", is_inbound);
1195 flow_log(" cipher alg: %u, cipher_type: %u\n", cipher_parms->alg,
1196 cipher_parms->type);
1197 flow_log(" iv len: %d\n", cipher_parms->iv_len);
1198 flow_dump(" iv: ", cipher_parms->iv_buf, cipher_parms->iv_len);
1199 flow_log(" data_size: %u\n", data_size);
1200
1201 fmd = (struct SPU2_FMD *)spu_hdr;
1202 omd = (u8 *)(fmd + 1);
1203
1204
1205
1206
1207
1208 ctrl0 = le64_to_cpu(fmd->ctrl0);
1209 if (is_inbound)
1210 ctrl0 &= ~SPU2_CIPH_ENCRYPT_EN;
1211 else
1212 ctrl0 |= SPU2_CIPH_ENCRYPT_EN;
1213 fmd->ctrl0 = cpu_to_le64(ctrl0);
1214
1215 if (cipher_parms->alg && cipher_parms->iv_buf && cipher_parms->iv_len) {
1216
1217 memcpy(omd + cipher_parms->key_len, cipher_parms->iv_buf,
1218 cipher_parms->iv_len);
1219 }
1220
1221 ctrl3 = le64_to_cpu(fmd->ctrl3);
1222 data_size &= SPU2_PL_LEN;
1223 ctrl3 |= data_size;
1224 fmd->ctrl3 = cpu_to_le64(ctrl3);
1225
1226 packet_dump(" SPU request header: ", spu_hdr, spu_req_hdr_len);
1227 }
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245 void spu2_request_pad(u8 *pad_start, u32 gcm_padding, u32 hash_pad_len,
1246 enum hash_alg auth_alg, enum hash_mode auth_mode,
1247 unsigned int total_sent, u32 status_padding)
1248 {
1249 u8 *ptr = pad_start;
1250
1251
1252 if (gcm_padding > 0) {
1253 flow_log(" GCM: padding to 16 byte alignment: %u bytes\n",
1254 gcm_padding);
1255 memset(ptr, 0, gcm_padding);
1256 ptr += gcm_padding;
1257 }
1258
1259 if (hash_pad_len > 0) {
1260
1261 memset(ptr, 0, hash_pad_len);
1262
1263
1264 *ptr = 0x80;
1265 ptr += (hash_pad_len - sizeof(u64));
1266
1267
1268 if (auth_alg == HASH_ALG_MD5)
1269 *(__le64 *)ptr = cpu_to_le64(total_sent * 8ull);
1270 else
1271 *(__be64 *)ptr = cpu_to_be64(total_sent * 8ull);
1272 ptr += sizeof(u64);
1273 }
1274
1275
1276 if (status_padding > 0) {
1277 flow_log(" STAT: padding to 4 byte alignment: %u bytes\n",
1278 status_padding);
1279
1280 memset(ptr, 0, status_padding);
1281 ptr += status_padding;
1282 }
1283 }
1284
1285
1286
1287
1288
1289
1290
1291 u8 spu2_xts_tweak_in_payload(void)
1292 {
1293 return 0;
1294 }
1295
1296
1297
1298
1299
1300
1301
1302 u8 spu2_tx_status_len(void)
1303 {
1304 return SPU2_TX_STATUS_LEN;
1305 }
1306
1307
1308
1309
1310
1311
1312
1313 u8 spu2_rx_status_len(void)
1314 {
1315 return SPU2_RX_STATUS_LEN;
1316 }
1317
1318
1319
1320
1321
1322
1323
1324
1325 int spu2_status_process(u8 *statp)
1326 {
1327
1328 u16 status = le16_to_cpu(*(__le16 *)statp);
1329
1330 if (status == 0)
1331 return 0;
1332
1333 flow_log("rx status is %#x\n", status);
1334 if (status == SPU2_INVALID_ICV)
1335 return SPU_INVALID_ICV;
1336
1337 return -EBADMSG;
1338 }
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351 void spu2_ccm_update_iv(unsigned int digestsize,
1352 struct spu_cipher_parms *cipher_parms,
1353 unsigned int assoclen, unsigned int chunksize,
1354 bool is_encrypt, bool is_esp)
1355 {
1356 int L;
1357
1358
1359
1360
1361
1362
1363 if (is_esp)
1364 L = CCM_ESP_L_VALUE;
1365 else
1366 L = ((cipher_parms->iv_buf[0] & CCM_B0_L_PRIME) >>
1367 CCM_B0_L_PRIME_SHIFT) + 1;
1368
1369
1370 cipher_parms->iv_len -= (1 + L);
1371 memmove(cipher_parms->iv_buf, &cipher_parms->iv_buf[1],
1372 cipher_parms->iv_len);
1373 }
1374
1375
1376
1377
1378
1379
1380
1381 u32 spu2_wordalign_padlen(u32 data_size)
1382 {
1383 return 0;
1384 }