spu2.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401
  1. /*
  2. * Copyright 2016 Broadcom
  3. *
  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").
  7. *
  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.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * version 2 (GPLv2) along with this source code.
  15. */
  16. /*
  17. * This file works with the SPU2 version of the SPU. SPU2 has different message
  18. * formats than the previous version of the SPU. All SPU message format
  19. * differences should be hidden in the spux.c,h files.
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/string.h>
  23. #include "util.h"
  24. #include "spu.h"
  25. #include "spu2.h"
  26. #define SPU2_TX_STATUS_LEN 0 /* SPU2 has no STATUS in input packet */
  27. /*
  28. * Controlled by pkt_stat_cnt field in CRYPTO_SS_SPU0_CORE_SPU2_CONTROL0
  29. * register. Defaults to 2.
  30. */
  31. #define SPU2_RX_STATUS_LEN 2
  32. enum spu2_proto_sel {
  33. SPU2_PROTO_RESV = 0,
  34. SPU2_MACSEC_SECTAG8_ECB = 1,
  35. SPU2_MACSEC_SECTAG8_SCB = 2,
  36. SPU2_MACSEC_SECTAG16 = 3,
  37. SPU2_MACSEC_SECTAG16_8_XPN = 4,
  38. SPU2_IPSEC = 5,
  39. SPU2_IPSEC_ESN = 6,
  40. SPU2_TLS_CIPHER = 7,
  41. SPU2_TLS_AEAD = 8,
  42. SPU2_DTLS_CIPHER = 9,
  43. SPU2_DTLS_AEAD = 10
  44. };
  45. char *spu2_cipher_type_names[] = { "None", "AES128", "AES192", "AES256",
  46. "DES", "3DES"
  47. };
  48. char *spu2_cipher_mode_names[] = { "ECB", "CBC", "CTR", "CFB", "OFB", "XTS",
  49. "CCM", "GCM"
  50. };
  51. char *spu2_hash_type_names[] = { "None", "AES128", "AES192", "AES256",
  52. "Reserved", "Reserved", "MD5", "SHA1", "SHA224", "SHA256", "SHA384",
  53. "SHA512", "SHA512/224", "SHA512/256", "SHA3-224", "SHA3-256",
  54. "SHA3-384", "SHA3-512"
  55. };
  56. char *spu2_hash_mode_names[] = { "CMAC", "CBC-MAC", "XCBC-MAC", "HMAC",
  57. "Rabin", "CCM", "GCM", "Reserved"
  58. };
  59. static char *spu2_ciph_type_name(enum spu2_cipher_type cipher_type)
  60. {
  61. if (cipher_type >= SPU2_CIPHER_TYPE_LAST)
  62. return "Reserved";
  63. return spu2_cipher_type_names[cipher_type];
  64. }
  65. static char *spu2_ciph_mode_name(enum spu2_cipher_mode cipher_mode)
  66. {
  67. if (cipher_mode >= SPU2_CIPHER_MODE_LAST)
  68. return "Reserved";
  69. return spu2_cipher_mode_names[cipher_mode];
  70. }
  71. static char *spu2_hash_type_name(enum spu2_hash_type hash_type)
  72. {
  73. if (hash_type >= SPU2_HASH_TYPE_LAST)
  74. return "Reserved";
  75. return spu2_hash_type_names[hash_type];
  76. }
  77. static char *spu2_hash_mode_name(enum spu2_hash_mode hash_mode)
  78. {
  79. if (hash_mode >= SPU2_HASH_MODE_LAST)
  80. return "Reserved";
  81. return spu2_hash_mode_names[hash_mode];
  82. }
  83. /*
  84. * Convert from a software cipher mode value to the corresponding value
  85. * for SPU2.
  86. */
  87. static int spu2_cipher_mode_xlate(enum spu_cipher_mode cipher_mode,
  88. enum spu2_cipher_mode *spu2_mode)
  89. {
  90. switch (cipher_mode) {
  91. case CIPHER_MODE_ECB:
  92. *spu2_mode = SPU2_CIPHER_MODE_ECB;
  93. break;
  94. case CIPHER_MODE_CBC:
  95. *spu2_mode = SPU2_CIPHER_MODE_CBC;
  96. break;
  97. case CIPHER_MODE_OFB:
  98. *spu2_mode = SPU2_CIPHER_MODE_OFB;
  99. break;
  100. case CIPHER_MODE_CFB:
  101. *spu2_mode = SPU2_CIPHER_MODE_CFB;
  102. break;
  103. case CIPHER_MODE_CTR:
  104. *spu2_mode = SPU2_CIPHER_MODE_CTR;
  105. break;
  106. case CIPHER_MODE_CCM:
  107. *spu2_mode = SPU2_CIPHER_MODE_CCM;
  108. break;
  109. case CIPHER_MODE_GCM:
  110. *spu2_mode = SPU2_CIPHER_MODE_GCM;
  111. break;
  112. case CIPHER_MODE_XTS:
  113. *spu2_mode = SPU2_CIPHER_MODE_XTS;
  114. break;
  115. default:
  116. return -EINVAL;
  117. }
  118. return 0;
  119. }
  120. /**
  121. * spu2_cipher_xlate() - Convert a cipher {alg/mode/type} triple to a SPU2
  122. * cipher type and mode.
  123. * @cipher_alg: [in] cipher algorithm value from software enumeration
  124. * @cipher_mode: [in] cipher mode value from software enumeration
  125. * @cipher_type: [in] cipher type value from software enumeration
  126. * @spu2_type: [out] cipher type value used by spu2 hardware
  127. * @spu2_mode: [out] cipher mode value used by spu2 hardware
  128. *
  129. * Return: 0 if successful
  130. */
  131. static int spu2_cipher_xlate(enum spu_cipher_alg cipher_alg,
  132. enum spu_cipher_mode cipher_mode,
  133. enum spu_cipher_type cipher_type,
  134. enum spu2_cipher_type *spu2_type,
  135. enum spu2_cipher_mode *spu2_mode)
  136. {
  137. int err;
  138. err = spu2_cipher_mode_xlate(cipher_mode, spu2_mode);
  139. if (err) {
  140. flow_log("Invalid cipher mode %d\n", cipher_mode);
  141. return err;
  142. }
  143. switch (cipher_alg) {
  144. case CIPHER_ALG_NONE:
  145. *spu2_type = SPU2_CIPHER_TYPE_NONE;
  146. break;
  147. case CIPHER_ALG_RC4:
  148. /* SPU2 does not support RC4 */
  149. err = -EINVAL;
  150. *spu2_type = SPU2_CIPHER_TYPE_NONE;
  151. break;
  152. case CIPHER_ALG_DES:
  153. *spu2_type = SPU2_CIPHER_TYPE_DES;
  154. break;
  155. case CIPHER_ALG_3DES:
  156. *spu2_type = SPU2_CIPHER_TYPE_3DES;
  157. break;
  158. case CIPHER_ALG_AES:
  159. switch (cipher_type) {
  160. case CIPHER_TYPE_AES128:
  161. *spu2_type = SPU2_CIPHER_TYPE_AES128;
  162. break;
  163. case CIPHER_TYPE_AES192:
  164. *spu2_type = SPU2_CIPHER_TYPE_AES192;
  165. break;
  166. case CIPHER_TYPE_AES256:
  167. *spu2_type = SPU2_CIPHER_TYPE_AES256;
  168. break;
  169. default:
  170. err = -EINVAL;
  171. }
  172. break;
  173. case CIPHER_ALG_LAST:
  174. default:
  175. err = -EINVAL;
  176. break;
  177. }
  178. if (err)
  179. flow_log("Invalid cipher alg %d or type %d\n",
  180. cipher_alg, cipher_type);
  181. return err;
  182. }
  183. /*
  184. * Convert from a software hash mode value to the corresponding value
  185. * for SPU2. Note that HASH_MODE_NONE and HASH_MODE_XCBC have the same value.
  186. */
  187. static int spu2_hash_mode_xlate(enum hash_mode hash_mode,
  188. enum spu2_hash_mode *spu2_mode)
  189. {
  190. switch (hash_mode) {
  191. case HASH_MODE_XCBC:
  192. *spu2_mode = SPU2_HASH_MODE_XCBC_MAC;
  193. break;
  194. case HASH_MODE_CMAC:
  195. *spu2_mode = SPU2_HASH_MODE_CMAC;
  196. break;
  197. case HASH_MODE_HMAC:
  198. *spu2_mode = SPU2_HASH_MODE_HMAC;
  199. break;
  200. case HASH_MODE_CCM:
  201. *spu2_mode = SPU2_HASH_MODE_CCM;
  202. break;
  203. case HASH_MODE_GCM:
  204. *spu2_mode = SPU2_HASH_MODE_GCM;
  205. break;
  206. default:
  207. return -EINVAL;
  208. }
  209. return 0;
  210. }
  211. /**
  212. * spu2_hash_xlate() - Convert a hash {alg/mode/type} triple to a SPU2 hash type
  213. * and mode.
  214. * @hash_alg: [in] hash algorithm value from software enumeration
  215. * @hash_mode: [in] hash mode value from software enumeration
  216. * @hash_type: [in] hash type value from software enumeration
  217. * @ciph_type: [in] cipher type value from software enumeration
  218. * @spu2_type: [out] hash type value used by SPU2 hardware
  219. * @spu2_mode: [out] hash mode value used by SPU2 hardware
  220. *
  221. * Return: 0 if successful
  222. */
  223. static int
  224. spu2_hash_xlate(enum hash_alg hash_alg, enum hash_mode hash_mode,
  225. enum hash_type hash_type, enum spu_cipher_type ciph_type,
  226. enum spu2_hash_type *spu2_type, enum spu2_hash_mode *spu2_mode)
  227. {
  228. int err;
  229. err = spu2_hash_mode_xlate(hash_mode, spu2_mode);
  230. if (err) {
  231. flow_log("Invalid hash mode %d\n", hash_mode);
  232. return err;
  233. }
  234. switch (hash_alg) {
  235. case HASH_ALG_NONE:
  236. *spu2_type = SPU2_HASH_TYPE_NONE;
  237. break;
  238. case HASH_ALG_MD5:
  239. *spu2_type = SPU2_HASH_TYPE_MD5;
  240. break;
  241. case HASH_ALG_SHA1:
  242. *spu2_type = SPU2_HASH_TYPE_SHA1;
  243. break;
  244. case HASH_ALG_SHA224:
  245. *spu2_type = SPU2_HASH_TYPE_SHA224;
  246. break;
  247. case HASH_ALG_SHA256:
  248. *spu2_type = SPU2_HASH_TYPE_SHA256;
  249. break;
  250. case HASH_ALG_SHA384:
  251. *spu2_type = SPU2_HASH_TYPE_SHA384;
  252. break;
  253. case HASH_ALG_SHA512:
  254. *spu2_type = SPU2_HASH_TYPE_SHA512;
  255. break;
  256. case HASH_ALG_AES:
  257. switch (ciph_type) {
  258. case CIPHER_TYPE_AES128:
  259. *spu2_type = SPU2_HASH_TYPE_AES128;
  260. break;
  261. case CIPHER_TYPE_AES192:
  262. *spu2_type = SPU2_HASH_TYPE_AES192;
  263. break;
  264. case CIPHER_TYPE_AES256:
  265. *spu2_type = SPU2_HASH_TYPE_AES256;
  266. break;
  267. default:
  268. err = -EINVAL;
  269. }
  270. break;
  271. case HASH_ALG_SHA3_224:
  272. *spu2_type = SPU2_HASH_TYPE_SHA3_224;
  273. break;
  274. case HASH_ALG_SHA3_256:
  275. *spu2_type = SPU2_HASH_TYPE_SHA3_256;
  276. break;
  277. case HASH_ALG_SHA3_384:
  278. *spu2_type = SPU2_HASH_TYPE_SHA3_384;
  279. break;
  280. case HASH_ALG_SHA3_512:
  281. *spu2_type = SPU2_HASH_TYPE_SHA3_512;
  282. case HASH_ALG_LAST:
  283. default:
  284. err = -EINVAL;
  285. break;
  286. }
  287. if (err)
  288. flow_log("Invalid hash alg %d or type %d\n",
  289. hash_alg, hash_type);
  290. return err;
  291. }
  292. /* Dump FMD ctrl0. The ctrl0 input is in host byte order */
  293. static void spu2_dump_fmd_ctrl0(u64 ctrl0)
  294. {
  295. enum spu2_cipher_type ciph_type;
  296. enum spu2_cipher_mode ciph_mode;
  297. enum spu2_hash_type hash_type;
  298. enum spu2_hash_mode hash_mode;
  299. char *ciph_name;
  300. char *ciph_mode_name;
  301. char *hash_name;
  302. char *hash_mode_name;
  303. u8 cfb;
  304. u8 proto;
  305. packet_log(" FMD CTRL0 %#16llx\n", ctrl0);
  306. if (ctrl0 & SPU2_CIPH_ENCRYPT_EN)
  307. packet_log(" encrypt\n");
  308. else
  309. packet_log(" decrypt\n");
  310. ciph_type = (ctrl0 & SPU2_CIPH_TYPE) >> SPU2_CIPH_TYPE_SHIFT;
  311. ciph_name = spu2_ciph_type_name(ciph_type);
  312. packet_log(" Cipher type: %s\n", ciph_name);
  313. if (ciph_type != SPU2_CIPHER_TYPE_NONE) {
  314. ciph_mode = (ctrl0 & SPU2_CIPH_MODE) >> SPU2_CIPH_MODE_SHIFT;
  315. ciph_mode_name = spu2_ciph_mode_name(ciph_mode);
  316. packet_log(" Cipher mode: %s\n", ciph_mode_name);
  317. }
  318. cfb = (ctrl0 & SPU2_CFB_MASK) >> SPU2_CFB_MASK_SHIFT;
  319. packet_log(" CFB %#x\n", cfb);
  320. proto = (ctrl0 & SPU2_PROTO_SEL) >> SPU2_PROTO_SEL_SHIFT;
  321. packet_log(" protocol %#x\n", proto);
  322. if (ctrl0 & SPU2_HASH_FIRST)
  323. packet_log(" hash first\n");
  324. else
  325. packet_log(" cipher first\n");
  326. if (ctrl0 & SPU2_CHK_TAG)
  327. packet_log(" check tag\n");
  328. hash_type = (ctrl0 & SPU2_HASH_TYPE) >> SPU2_HASH_TYPE_SHIFT;
  329. hash_name = spu2_hash_type_name(hash_type);
  330. packet_log(" Hash type: %s\n", hash_name);
  331. if (hash_type != SPU2_HASH_TYPE_NONE) {
  332. hash_mode = (ctrl0 & SPU2_HASH_MODE) >> SPU2_HASH_MODE_SHIFT;
  333. hash_mode_name = spu2_hash_mode_name(hash_mode);
  334. packet_log(" Hash mode: %s\n", hash_mode_name);
  335. }
  336. if (ctrl0 & SPU2_CIPH_PAD_EN) {
  337. packet_log(" Cipher pad: %#2llx\n",
  338. (ctrl0 & SPU2_CIPH_PAD) >> SPU2_CIPH_PAD_SHIFT);
  339. }
  340. }
  341. /* Dump FMD ctrl1. The ctrl1 input is in host byte order */
  342. static void spu2_dump_fmd_ctrl1(u64 ctrl1)
  343. {
  344. u8 hash_key_len;
  345. u8 ciph_key_len;
  346. u8 ret_iv_len;
  347. u8 iv_offset;
  348. u8 iv_len;
  349. u8 hash_tag_len;
  350. u8 ret_md;
  351. packet_log(" FMD CTRL1 %#16llx\n", ctrl1);
  352. if (ctrl1 & SPU2_TAG_LOC)
  353. packet_log(" Tag after payload\n");
  354. packet_log(" Msg includes ");
  355. if (ctrl1 & SPU2_HAS_FR_DATA)
  356. packet_log("FD ");
  357. if (ctrl1 & SPU2_HAS_AAD1)
  358. packet_log("AAD1 ");
  359. if (ctrl1 & SPU2_HAS_NAAD)
  360. packet_log("NAAD ");
  361. if (ctrl1 & SPU2_HAS_AAD2)
  362. packet_log("AAD2 ");
  363. if (ctrl1 & SPU2_HAS_ESN)
  364. packet_log("ESN ");
  365. packet_log("\n");
  366. hash_key_len = (ctrl1 & SPU2_HASH_KEY_LEN) >> SPU2_HASH_KEY_LEN_SHIFT;
  367. packet_log(" Hash key len %u\n", hash_key_len);
  368. ciph_key_len = (ctrl1 & SPU2_CIPH_KEY_LEN) >> SPU2_CIPH_KEY_LEN_SHIFT;
  369. packet_log(" Cipher key len %u\n", ciph_key_len);
  370. if (ctrl1 & SPU2_GENIV)
  371. packet_log(" Generate IV\n");
  372. if (ctrl1 & SPU2_HASH_IV)
  373. packet_log(" IV included in hash\n");
  374. if (ctrl1 & SPU2_RET_IV)
  375. packet_log(" Return IV in output before payload\n");
  376. ret_iv_len = (ctrl1 & SPU2_RET_IV_LEN) >> SPU2_RET_IV_LEN_SHIFT;
  377. packet_log(" Length of returned IV %u bytes\n",
  378. ret_iv_len ? ret_iv_len : 16);
  379. iv_offset = (ctrl1 & SPU2_IV_OFFSET) >> SPU2_IV_OFFSET_SHIFT;
  380. packet_log(" IV offset %u\n", iv_offset);
  381. iv_len = (ctrl1 & SPU2_IV_LEN) >> SPU2_IV_LEN_SHIFT;
  382. packet_log(" Input IV len %u bytes\n", iv_len);
  383. hash_tag_len = (ctrl1 & SPU2_HASH_TAG_LEN) >> SPU2_HASH_TAG_LEN_SHIFT;
  384. packet_log(" Hash tag length %u bytes\n", hash_tag_len);
  385. packet_log(" Return ");
  386. ret_md = (ctrl1 & SPU2_RETURN_MD) >> SPU2_RETURN_MD_SHIFT;
  387. if (ret_md)
  388. packet_log("FMD ");
  389. if (ret_md == SPU2_RET_FMD_OMD)
  390. packet_log("OMD ");
  391. else if (ret_md == SPU2_RET_FMD_OMD_IV)
  392. packet_log("OMD IV ");
  393. if (ctrl1 & SPU2_RETURN_FD)
  394. packet_log("FD ");
  395. if (ctrl1 & SPU2_RETURN_AAD1)
  396. packet_log("AAD1 ");
  397. if (ctrl1 & SPU2_RETURN_NAAD)
  398. packet_log("NAAD ");
  399. if (ctrl1 & SPU2_RETURN_AAD2)
  400. packet_log("AAD2 ");
  401. if (ctrl1 & SPU2_RETURN_PAY)
  402. packet_log("Payload");
  403. packet_log("\n");
  404. }
  405. /* Dump FMD ctrl2. The ctrl2 input is in host byte order */
  406. static void spu2_dump_fmd_ctrl2(u64 ctrl2)
  407. {
  408. packet_log(" FMD CTRL2 %#16llx\n", ctrl2);
  409. packet_log(" AAD1 offset %llu length %llu bytes\n",
  410. ctrl2 & SPU2_AAD1_OFFSET,
  411. (ctrl2 & SPU2_AAD1_LEN) >> SPU2_AAD1_LEN_SHIFT);
  412. packet_log(" AAD2 offset %llu\n",
  413. (ctrl2 & SPU2_AAD2_OFFSET) >> SPU2_AAD2_OFFSET_SHIFT);
  414. packet_log(" Payload offset %llu\n",
  415. (ctrl2 & SPU2_PL_OFFSET) >> SPU2_PL_OFFSET_SHIFT);
  416. }
  417. /* Dump FMD ctrl3. The ctrl3 input is in host byte order */
  418. static void spu2_dump_fmd_ctrl3(u64 ctrl3)
  419. {
  420. packet_log(" FMD CTRL3 %#16llx\n", ctrl3);
  421. packet_log(" Payload length %llu bytes\n", ctrl3 & SPU2_PL_LEN);
  422. packet_log(" TLS length %llu bytes\n",
  423. (ctrl3 & SPU2_TLS_LEN) >> SPU2_TLS_LEN_SHIFT);
  424. }
  425. static void spu2_dump_fmd(struct SPU2_FMD *fmd)
  426. {
  427. spu2_dump_fmd_ctrl0(le64_to_cpu(fmd->ctrl0));
  428. spu2_dump_fmd_ctrl1(le64_to_cpu(fmd->ctrl1));
  429. spu2_dump_fmd_ctrl2(le64_to_cpu(fmd->ctrl2));
  430. spu2_dump_fmd_ctrl3(le64_to_cpu(fmd->ctrl3));
  431. }
  432. static void spu2_dump_omd(u8 *omd, u16 hash_key_len, u16 ciph_key_len,
  433. u16 hash_iv_len, u16 ciph_iv_len)
  434. {
  435. u8 *ptr = omd;
  436. packet_log(" OMD:\n");
  437. if (hash_key_len) {
  438. packet_log(" Hash Key Length %u bytes\n", hash_key_len);
  439. packet_dump(" KEY: ", ptr, hash_key_len);
  440. ptr += hash_key_len;
  441. }
  442. if (ciph_key_len) {
  443. packet_log(" Cipher Key Length %u bytes\n", ciph_key_len);
  444. packet_dump(" KEY: ", ptr, ciph_key_len);
  445. ptr += ciph_key_len;
  446. }
  447. if (hash_iv_len) {
  448. packet_log(" Hash IV Length %u bytes\n", hash_iv_len);
  449. packet_dump(" hash IV: ", ptr, hash_iv_len);
  450. ptr += ciph_key_len;
  451. }
  452. if (ciph_iv_len) {
  453. packet_log(" Cipher IV Length %u bytes\n", ciph_iv_len);
  454. packet_dump(" cipher IV: ", ptr, ciph_iv_len);
  455. }
  456. }
  457. /* Dump a SPU2 header for debug */
  458. void spu2_dump_msg_hdr(u8 *buf, unsigned int buf_len)
  459. {
  460. struct SPU2_FMD *fmd = (struct SPU2_FMD *)buf;
  461. u8 *omd;
  462. u64 ctrl1;
  463. u16 hash_key_len;
  464. u16 ciph_key_len;
  465. u16 hash_iv_len;
  466. u16 ciph_iv_len;
  467. u16 omd_len;
  468. packet_log("\n");
  469. packet_log("SPU2 message header %p len: %u\n", buf, buf_len);
  470. spu2_dump_fmd(fmd);
  471. omd = (u8 *)(fmd + 1);
  472. ctrl1 = le64_to_cpu(fmd->ctrl1);
  473. hash_key_len = (ctrl1 & SPU2_HASH_KEY_LEN) >> SPU2_HASH_KEY_LEN_SHIFT;
  474. ciph_key_len = (ctrl1 & SPU2_CIPH_KEY_LEN) >> SPU2_CIPH_KEY_LEN_SHIFT;
  475. hash_iv_len = 0;
  476. ciph_iv_len = (ctrl1 & SPU2_IV_LEN) >> SPU2_IV_LEN_SHIFT;
  477. spu2_dump_omd(omd, hash_key_len, ciph_key_len, hash_iv_len,
  478. ciph_iv_len);
  479. /* Double check sanity */
  480. omd_len = hash_key_len + ciph_key_len + hash_iv_len + ciph_iv_len;
  481. if (FMD_SIZE + omd_len != buf_len) {
  482. packet_log
  483. (" Packet parsed incorrectly. buf_len %u, sum of MD %zu\n",
  484. buf_len, FMD_SIZE + omd_len);
  485. }
  486. packet_log("\n");
  487. }
  488. /**
  489. * spu2_fmd_init() - At setkey time, initialize the fixed meta data for
  490. * subsequent ablkcipher requests for this context.
  491. * @spu2_cipher_type: Cipher algorithm
  492. * @spu2_mode: Cipher mode
  493. * @cipher_key_len: Length of cipher key, in bytes
  494. * @cipher_iv_len: Length of cipher initialization vector, in bytes
  495. *
  496. * Return: 0 (success)
  497. */
  498. static int spu2_fmd_init(struct SPU2_FMD *fmd,
  499. enum spu2_cipher_type spu2_type,
  500. enum spu2_cipher_mode spu2_mode,
  501. u32 cipher_key_len, u32 cipher_iv_len)
  502. {
  503. u64 ctrl0;
  504. u64 ctrl1;
  505. u64 ctrl2;
  506. u64 ctrl3;
  507. u32 aad1_offset;
  508. u32 aad2_offset;
  509. u16 aad1_len = 0;
  510. u64 payload_offset;
  511. ctrl0 = (spu2_type << SPU2_CIPH_TYPE_SHIFT) |
  512. (spu2_mode << SPU2_CIPH_MODE_SHIFT);
  513. ctrl1 = (cipher_key_len << SPU2_CIPH_KEY_LEN_SHIFT) |
  514. ((u64)cipher_iv_len << SPU2_IV_LEN_SHIFT) |
  515. ((u64)SPU2_RET_FMD_ONLY << SPU2_RETURN_MD_SHIFT) | SPU2_RETURN_PAY;
  516. /*
  517. * AAD1 offset is from start of FD. FD length is always 0 for this
  518. * driver. So AAD1_offset is always 0.
  519. */
  520. aad1_offset = 0;
  521. aad2_offset = aad1_offset;
  522. payload_offset = 0;
  523. ctrl2 = aad1_offset |
  524. (aad1_len << SPU2_AAD1_LEN_SHIFT) |
  525. (aad2_offset << SPU2_AAD2_OFFSET_SHIFT) |
  526. (payload_offset << SPU2_PL_OFFSET_SHIFT);
  527. ctrl3 = 0;
  528. fmd->ctrl0 = cpu_to_le64(ctrl0);
  529. fmd->ctrl1 = cpu_to_le64(ctrl1);
  530. fmd->ctrl2 = cpu_to_le64(ctrl2);
  531. fmd->ctrl3 = cpu_to_le64(ctrl3);
  532. return 0;
  533. }
  534. /**
  535. * spu2_fmd_ctrl0_write() - Write ctrl0 field in fixed metadata (FMD) field of
  536. * SPU request packet.
  537. * @fmd: Start of FMD field to be written
  538. * @is_inbound: true if decrypting. false if encrypting.
  539. * @authFirst: true if alg authenticates before encrypting
  540. * @protocol: protocol selector
  541. * @cipher_type: cipher algorithm
  542. * @cipher_mode: cipher mode
  543. * @auth_type: authentication type
  544. * @auth_mode: authentication mode
  545. */
  546. static void spu2_fmd_ctrl0_write(struct SPU2_FMD *fmd,
  547. bool is_inbound, bool auth_first,
  548. enum spu2_proto_sel protocol,
  549. enum spu2_cipher_type cipher_type,
  550. enum spu2_cipher_mode cipher_mode,
  551. enum spu2_hash_type auth_type,
  552. enum spu2_hash_mode auth_mode)
  553. {
  554. u64 ctrl0 = 0;
  555. if ((cipher_type != SPU2_CIPHER_TYPE_NONE) && !is_inbound)
  556. ctrl0 |= SPU2_CIPH_ENCRYPT_EN;
  557. ctrl0 |= ((u64)cipher_type << SPU2_CIPH_TYPE_SHIFT) |
  558. ((u64)cipher_mode << SPU2_CIPH_MODE_SHIFT);
  559. if (protocol)
  560. ctrl0 |= (u64)protocol << SPU2_PROTO_SEL_SHIFT;
  561. if (auth_first)
  562. ctrl0 |= SPU2_HASH_FIRST;
  563. if (is_inbound && (auth_type != SPU2_HASH_TYPE_NONE))
  564. ctrl0 |= SPU2_CHK_TAG;
  565. ctrl0 |= (((u64)auth_type << SPU2_HASH_TYPE_SHIFT) |
  566. ((u64)auth_mode << SPU2_HASH_MODE_SHIFT));
  567. fmd->ctrl0 = cpu_to_le64(ctrl0);
  568. }
  569. /**
  570. * spu2_fmd_ctrl1_write() - Write ctrl1 field in fixed metadata (FMD) field of
  571. * SPU request packet.
  572. * @fmd: Start of FMD field to be written
  573. * @assoc_size: Length of additional associated data, in bytes
  574. * @auth_key_len: Length of authentication key, in bytes
  575. * @cipher_key_len: Length of cipher key, in bytes
  576. * @gen_iv: If true, hw generates IV and returns in response
  577. * @hash_iv: IV participates in hash. Used for IPSEC and TLS.
  578. * @return_iv: Return IV in output packet before payload
  579. * @ret_iv_len: Length of IV returned from SPU, in bytes
  580. * @ret_iv_offset: Offset into full IV of start of returned IV
  581. * @cipher_iv_len: Length of input cipher IV, in bytes
  582. * @digest_size: Length of digest (aka, hash tag or ICV), in bytes
  583. * @return_payload: Return payload in SPU response
  584. * @return_md : return metadata in SPU response
  585. *
  586. * Packet can have AAD2 w/o AAD1. For algorithms currently supported,
  587. * associated data goes in AAD2.
  588. */
  589. static void spu2_fmd_ctrl1_write(struct SPU2_FMD *fmd, bool is_inbound,
  590. u64 assoc_size,
  591. u64 auth_key_len, u64 cipher_key_len,
  592. bool gen_iv, bool hash_iv, bool return_iv,
  593. u64 ret_iv_len, u64 ret_iv_offset,
  594. u64 cipher_iv_len, u64 digest_size,
  595. bool return_payload, bool return_md)
  596. {
  597. u64 ctrl1 = 0;
  598. if (is_inbound && digest_size)
  599. ctrl1 |= SPU2_TAG_LOC;
  600. if (assoc_size) {
  601. ctrl1 |= SPU2_HAS_AAD2;
  602. ctrl1 |= SPU2_RETURN_AAD2; /* need aad2 for gcm aes esp */
  603. }
  604. if (auth_key_len)
  605. ctrl1 |= ((auth_key_len << SPU2_HASH_KEY_LEN_SHIFT) &
  606. SPU2_HASH_KEY_LEN);
  607. if (cipher_key_len)
  608. ctrl1 |= ((cipher_key_len << SPU2_CIPH_KEY_LEN_SHIFT) &
  609. SPU2_CIPH_KEY_LEN);
  610. if (gen_iv)
  611. ctrl1 |= SPU2_GENIV;
  612. if (hash_iv)
  613. ctrl1 |= SPU2_HASH_IV;
  614. if (return_iv) {
  615. ctrl1 |= SPU2_RET_IV;
  616. ctrl1 |= ret_iv_len << SPU2_RET_IV_LEN_SHIFT;
  617. ctrl1 |= ret_iv_offset << SPU2_IV_OFFSET_SHIFT;
  618. }
  619. ctrl1 |= ((cipher_iv_len << SPU2_IV_LEN_SHIFT) & SPU2_IV_LEN);
  620. if (digest_size)
  621. ctrl1 |= ((digest_size << SPU2_HASH_TAG_LEN_SHIFT) &
  622. SPU2_HASH_TAG_LEN);
  623. /* Let's ask for the output pkt to include FMD, but don't need to
  624. * get keys and IVs back in OMD.
  625. */
  626. if (return_md)
  627. ctrl1 |= ((u64)SPU2_RET_FMD_ONLY << SPU2_RETURN_MD_SHIFT);
  628. else
  629. ctrl1 |= ((u64)SPU2_RET_NO_MD << SPU2_RETURN_MD_SHIFT);
  630. /* Crypto API does not get assoc data back. So no need for AAD2. */
  631. if (return_payload)
  632. ctrl1 |= SPU2_RETURN_PAY;
  633. fmd->ctrl1 = cpu_to_le64(ctrl1);
  634. }
  635. /**
  636. * spu2_fmd_ctrl2_write() - Set the ctrl2 field in the fixed metadata field of
  637. * SPU2 header.
  638. * @fmd: Start of FMD field to be written
  639. * @cipher_offset: Number of bytes from Start of Packet (end of FD field) where
  640. * data to be encrypted or decrypted begins
  641. * @auth_key_len: Length of authentication key, in bytes
  642. * @auth_iv_len: Length of authentication initialization vector, in bytes
  643. * @cipher_key_len: Length of cipher key, in bytes
  644. * @cipher_iv_len: Length of cipher IV, in bytes
  645. */
  646. static void spu2_fmd_ctrl2_write(struct SPU2_FMD *fmd, u64 cipher_offset,
  647. u64 auth_key_len, u64 auth_iv_len,
  648. u64 cipher_key_len, u64 cipher_iv_len)
  649. {
  650. u64 ctrl2;
  651. u64 aad1_offset;
  652. u64 aad2_offset;
  653. u16 aad1_len = 0;
  654. u64 payload_offset;
  655. /* AAD1 offset is from start of FD. FD length always 0. */
  656. aad1_offset = 0;
  657. aad2_offset = aad1_offset;
  658. payload_offset = cipher_offset;
  659. ctrl2 = aad1_offset |
  660. (aad1_len << SPU2_AAD1_LEN_SHIFT) |
  661. (aad2_offset << SPU2_AAD2_OFFSET_SHIFT) |
  662. (payload_offset << SPU2_PL_OFFSET_SHIFT);
  663. fmd->ctrl2 = cpu_to_le64(ctrl2);
  664. }
  665. /**
  666. * spu2_fmd_ctrl3_write() - Set the ctrl3 field in FMD
  667. * @fmd: Fixed meta data. First field in SPU2 msg header.
  668. * @payload_len: Length of payload, in bytes
  669. */
  670. static void spu2_fmd_ctrl3_write(struct SPU2_FMD *fmd, u64 payload_len)
  671. {
  672. u64 ctrl3;
  673. ctrl3 = payload_len & SPU2_PL_LEN;
  674. fmd->ctrl3 = cpu_to_le64(ctrl3);
  675. }
  676. /**
  677. * spu2_ctx_max_payload() - Determine the maximum length of the payload for a
  678. * SPU message for a given cipher and hash alg context.
  679. * @cipher_alg: The cipher algorithm
  680. * @cipher_mode: The cipher mode
  681. * @blocksize: The size of a block of data for this algo
  682. *
  683. * For SPU2, the hardware generally ignores the PayloadLen field in ctrl3 of
  684. * FMD and just keeps computing until it receives a DMA descriptor with the EOF
  685. * flag set. So we consider the max payload to be infinite. AES CCM is an
  686. * exception.
  687. *
  688. * Return: Max payload length in bytes
  689. */
  690. u32 spu2_ctx_max_payload(enum spu_cipher_alg cipher_alg,
  691. enum spu_cipher_mode cipher_mode,
  692. unsigned int blocksize)
  693. {
  694. if ((cipher_alg == CIPHER_ALG_AES) &&
  695. (cipher_mode == CIPHER_MODE_CCM)) {
  696. u32 excess = SPU2_MAX_PAYLOAD % blocksize;
  697. return SPU2_MAX_PAYLOAD - excess;
  698. } else {
  699. return SPU_MAX_PAYLOAD_INF;
  700. }
  701. }
  702. /**
  703. * spu_payload_length() - Given a SPU2 message header, extract the payload
  704. * length.
  705. * @spu_hdr: Start of SPU message header (FMD)
  706. *
  707. * Return: payload length, in bytes
  708. */
  709. u32 spu2_payload_length(u8 *spu_hdr)
  710. {
  711. struct SPU2_FMD *fmd = (struct SPU2_FMD *)spu_hdr;
  712. u32 pl_len;
  713. u64 ctrl3;
  714. ctrl3 = le64_to_cpu(fmd->ctrl3);
  715. pl_len = ctrl3 & SPU2_PL_LEN;
  716. return pl_len;
  717. }
  718. /**
  719. * spu_response_hdr_len() - Determine the expected length of a SPU response
  720. * header.
  721. * @auth_key_len: Length of authentication key, in bytes
  722. * @enc_key_len: Length of encryption key, in bytes
  723. *
  724. * For SPU2, includes just FMD. OMD is never requested.
  725. *
  726. * Return: Length of FMD, in bytes
  727. */
  728. u16 spu2_response_hdr_len(u16 auth_key_len, u16 enc_key_len, bool is_hash)
  729. {
  730. return FMD_SIZE;
  731. }
  732. /**
  733. * spu_hash_pad_len() - Calculate the length of hash padding required to extend
  734. * data to a full block size.
  735. * @hash_alg: hash algorithm
  736. * @hash_mode: hash mode
  737. * @chunksize: length of data, in bytes
  738. * @hash_block_size: size of a hash block, in bytes
  739. *
  740. * SPU2 hardware does all hash padding
  741. *
  742. * Return: length of hash pad in bytes
  743. */
  744. u16 spu2_hash_pad_len(enum hash_alg hash_alg, enum hash_mode hash_mode,
  745. u32 chunksize, u16 hash_block_size)
  746. {
  747. return 0;
  748. }
  749. /**
  750. * spu2_gcm_ccm_padlen() - Determine the length of GCM/CCM padding for either
  751. * the AAD field or the data.
  752. *
  753. * Return: 0. Unlike SPU-M, SPU2 hardware does any GCM/CCM padding required.
  754. */
  755. u32 spu2_gcm_ccm_pad_len(enum spu_cipher_mode cipher_mode,
  756. unsigned int data_size)
  757. {
  758. return 0;
  759. }
  760. /**
  761. * spu_assoc_resp_len() - Determine the size of the AAD2 buffer needed to catch
  762. * associated data in a SPU2 output packet.
  763. * @cipher_mode: cipher mode
  764. * @assoc_len: length of additional associated data, in bytes
  765. * @iv_len: length of initialization vector, in bytes
  766. * @is_encrypt: true if encrypting. false if decrypt.
  767. *
  768. * Return: Length of buffer to catch associated data in response
  769. */
  770. u32 spu2_assoc_resp_len(enum spu_cipher_mode cipher_mode,
  771. unsigned int assoc_len, unsigned int iv_len,
  772. bool is_encrypt)
  773. {
  774. u32 resp_len = assoc_len;
  775. if (is_encrypt)
  776. /* gcm aes esp has to write 8-byte IV in response */
  777. resp_len += iv_len;
  778. return resp_len;
  779. }
  780. /*
  781. * spu_aead_ivlen() - Calculate the length of the AEAD IV to be included
  782. * in a SPU request after the AAD and before the payload.
  783. * @cipher_mode: cipher mode
  784. * @iv_ctr_len: initialization vector length in bytes
  785. *
  786. * For SPU2, AEAD IV is included in OMD and does not need to be repeated
  787. * prior to the payload.
  788. *
  789. * Return: Length of AEAD IV in bytes
  790. */
  791. u8 spu2_aead_ivlen(enum spu_cipher_mode cipher_mode, u16 iv_len)
  792. {
  793. return 0;
  794. }
  795. /**
  796. * spu2_hash_type() - Determine the type of hash operation.
  797. * @src_sent: The number of bytes in the current request that have already
  798. * been sent to the SPU to be hashed.
  799. *
  800. * SPU2 always does a FULL hash operation
  801. */
  802. enum hash_type spu2_hash_type(u32 src_sent)
  803. {
  804. return HASH_TYPE_FULL;
  805. }
  806. /**
  807. * spu2_digest_size() - Determine the size of a hash digest to expect the SPU to
  808. * return.
  809. * alg_digest_size: Number of bytes in the final digest for the given algo
  810. * alg: The hash algorithm
  811. * htype: Type of hash operation (init, update, full, etc)
  812. *
  813. */
  814. u32 spu2_digest_size(u32 alg_digest_size, enum hash_alg alg,
  815. enum hash_type htype)
  816. {
  817. return alg_digest_size;
  818. }
  819. /**
  820. * spu_create_request() - Build a SPU2 request message header, includint FMD and
  821. * OMD.
  822. * @spu_hdr: Start of buffer where SPU request header is to be written
  823. * @req_opts: SPU request message options
  824. * @cipher_parms: Parameters related to cipher algorithm
  825. * @hash_parms: Parameters related to hash algorithm
  826. * @aead_parms: Parameters related to AEAD operation
  827. * @data_size: Length of data to be encrypted or authenticated. If AEAD, does
  828. * not include length of AAD.
  829. *
  830. * Construct the message starting at spu_hdr. Caller should allocate this buffer
  831. * in DMA-able memory at least SPU_HEADER_ALLOC_LEN bytes long.
  832. *
  833. * Return: the length of the SPU header in bytes. 0 if an error occurs.
  834. */
  835. u32 spu2_create_request(u8 *spu_hdr,
  836. struct spu_request_opts *req_opts,
  837. struct spu_cipher_parms *cipher_parms,
  838. struct spu_hash_parms *hash_parms,
  839. struct spu_aead_parms *aead_parms,
  840. unsigned int data_size)
  841. {
  842. struct SPU2_FMD *fmd;
  843. u8 *ptr;
  844. unsigned int buf_len;
  845. int err;
  846. enum spu2_cipher_type spu2_ciph_type = SPU2_CIPHER_TYPE_NONE;
  847. enum spu2_cipher_mode spu2_ciph_mode;
  848. enum spu2_hash_type spu2_auth_type = SPU2_HASH_TYPE_NONE;
  849. enum spu2_hash_mode spu2_auth_mode;
  850. bool return_md = true;
  851. enum spu2_proto_sel proto = SPU2_PROTO_RESV;
  852. /* size of the payload */
  853. unsigned int payload_len =
  854. hash_parms->prebuf_len + data_size + hash_parms->pad_len -
  855. ((req_opts->is_aead && req_opts->is_inbound) ?
  856. hash_parms->digestsize : 0);
  857. /* offset of prebuf or data from start of AAD2 */
  858. unsigned int cipher_offset = aead_parms->assoc_size +
  859. aead_parms->aad_pad_len + aead_parms->iv_len;
  860. #ifdef DEBUG
  861. /* total size of the data following OMD (without STAT word padding) */
  862. unsigned int real_db_size = spu_real_db_size(aead_parms->assoc_size,
  863. aead_parms->iv_len,
  864. hash_parms->prebuf_len,
  865. data_size,
  866. aead_parms->aad_pad_len,
  867. aead_parms->data_pad_len,
  868. hash_parms->pad_len);
  869. #endif
  870. unsigned int assoc_size = aead_parms->assoc_size;
  871. if (req_opts->is_aead &&
  872. (cipher_parms->alg == CIPHER_ALG_AES) &&
  873. (cipher_parms->mode == CIPHER_MODE_GCM))
  874. /*
  875. * On SPU 2, aes gcm cipher first on encrypt, auth first on
  876. * decrypt
  877. */
  878. req_opts->auth_first = req_opts->is_inbound;
  879. /* and do opposite for ccm (auth 1st on encrypt) */
  880. if (req_opts->is_aead &&
  881. (cipher_parms->alg == CIPHER_ALG_AES) &&
  882. (cipher_parms->mode == CIPHER_MODE_CCM))
  883. req_opts->auth_first = !req_opts->is_inbound;
  884. flow_log("%s()\n", __func__);
  885. flow_log(" in:%u authFirst:%u\n",
  886. req_opts->is_inbound, req_opts->auth_first);
  887. flow_log(" cipher alg:%u mode:%u type %u\n", cipher_parms->alg,
  888. cipher_parms->mode, cipher_parms->type);
  889. flow_log(" is_esp: %s\n", req_opts->is_esp ? "yes" : "no");
  890. flow_log(" key: %d\n", cipher_parms->key_len);
  891. flow_dump(" key: ", cipher_parms->key_buf, cipher_parms->key_len);
  892. flow_log(" iv: %d\n", cipher_parms->iv_len);
  893. flow_dump(" iv: ", cipher_parms->iv_buf, cipher_parms->iv_len);
  894. flow_log(" auth alg:%u mode:%u type %u\n",
  895. hash_parms->alg, hash_parms->mode, hash_parms->type);
  896. flow_log(" digestsize: %u\n", hash_parms->digestsize);
  897. flow_log(" authkey: %d\n", hash_parms->key_len);
  898. flow_dump(" authkey: ", hash_parms->key_buf, hash_parms->key_len);
  899. flow_log(" assoc_size:%u\n", assoc_size);
  900. flow_log(" prebuf_len:%u\n", hash_parms->prebuf_len);
  901. flow_log(" data_size:%u\n", data_size);
  902. flow_log(" hash_pad_len:%u\n", hash_parms->pad_len);
  903. flow_log(" real_db_size:%u\n", real_db_size);
  904. flow_log(" cipher_offset:%u payload_len:%u\n",
  905. cipher_offset, payload_len);
  906. flow_log(" aead_iv: %u\n", aead_parms->iv_len);
  907. /* Convert to spu2 values for cipher alg, hash alg */
  908. err = spu2_cipher_xlate(cipher_parms->alg, cipher_parms->mode,
  909. cipher_parms->type,
  910. &spu2_ciph_type, &spu2_ciph_mode);
  911. /* If we are doing GCM hashing only - either via rfc4543 transform
  912. * or because we happen to do GCM with AAD only and no payload - we
  913. * need to configure hardware to use hash key rather than cipher key
  914. * and put data into payload. This is because unlike SPU-M, running
  915. * GCM cipher with 0 size payload is not permitted.
  916. */
  917. if ((req_opts->is_rfc4543) ||
  918. ((spu2_ciph_mode == SPU2_CIPHER_MODE_GCM) &&
  919. (payload_len == 0))) {
  920. /* Use hashing (only) and set up hash key */
  921. spu2_ciph_type = SPU2_CIPHER_TYPE_NONE;
  922. hash_parms->key_len = cipher_parms->key_len;
  923. memcpy(hash_parms->key_buf, cipher_parms->key_buf,
  924. cipher_parms->key_len);
  925. cipher_parms->key_len = 0;
  926. if (req_opts->is_rfc4543)
  927. payload_len += assoc_size;
  928. else
  929. payload_len = assoc_size;
  930. cipher_offset = 0;
  931. assoc_size = 0;
  932. }
  933. if (err)
  934. return 0;
  935. flow_log("spu2 cipher type %s, cipher mode %s\n",
  936. spu2_ciph_type_name(spu2_ciph_type),
  937. spu2_ciph_mode_name(spu2_ciph_mode));
  938. err = spu2_hash_xlate(hash_parms->alg, hash_parms->mode,
  939. hash_parms->type,
  940. cipher_parms->type,
  941. &spu2_auth_type, &spu2_auth_mode);
  942. if (err)
  943. return 0;
  944. flow_log("spu2 hash type %s, hash mode %s\n",
  945. spu2_hash_type_name(spu2_auth_type),
  946. spu2_hash_mode_name(spu2_auth_mode));
  947. fmd = (struct SPU2_FMD *)spu_hdr;
  948. spu2_fmd_ctrl0_write(fmd, req_opts->is_inbound, req_opts->auth_first,
  949. proto, spu2_ciph_type, spu2_ciph_mode,
  950. spu2_auth_type, spu2_auth_mode);
  951. spu2_fmd_ctrl1_write(fmd, req_opts->is_inbound, assoc_size,
  952. hash_parms->key_len, cipher_parms->key_len,
  953. false, false,
  954. aead_parms->return_iv, aead_parms->ret_iv_len,
  955. aead_parms->ret_iv_off,
  956. cipher_parms->iv_len, hash_parms->digestsize,
  957. !req_opts->bd_suppress, return_md);
  958. spu2_fmd_ctrl2_write(fmd, cipher_offset, hash_parms->key_len, 0,
  959. cipher_parms->key_len, cipher_parms->iv_len);
  960. spu2_fmd_ctrl3_write(fmd, payload_len);
  961. ptr = (u8 *)(fmd + 1);
  962. buf_len = sizeof(struct SPU2_FMD);
  963. /* Write OMD */
  964. if (hash_parms->key_len) {
  965. memcpy(ptr, hash_parms->key_buf, hash_parms->key_len);
  966. ptr += hash_parms->key_len;
  967. buf_len += hash_parms->key_len;
  968. }
  969. if (cipher_parms->key_len) {
  970. memcpy(ptr, cipher_parms->key_buf, cipher_parms->key_len);
  971. ptr += cipher_parms->key_len;
  972. buf_len += cipher_parms->key_len;
  973. }
  974. if (cipher_parms->iv_len) {
  975. memcpy(ptr, cipher_parms->iv_buf, cipher_parms->iv_len);
  976. ptr += cipher_parms->iv_len;
  977. buf_len += cipher_parms->iv_len;
  978. }
  979. packet_dump(" SPU request header: ", spu_hdr, buf_len);
  980. return buf_len;
  981. }
  982. /**
  983. * spu_cipher_req_init() - Build an ablkcipher SPU2 request message header,
  984. * including FMD and OMD.
  985. * @spu_hdr: Location of start of SPU request (FMD field)
  986. * @cipher_parms: Parameters describing cipher request
  987. *
  988. * Called at setkey time to initialize a msg header that can be reused for all
  989. * subsequent ablkcipher requests. Construct the message starting at spu_hdr.
  990. * Caller should allocate this buffer in DMA-able memory at least
  991. * SPU_HEADER_ALLOC_LEN bytes long.
  992. *
  993. * Return: the total length of the SPU header (FMD and OMD) in bytes. 0 if an
  994. * error occurs.
  995. */
  996. u16 spu2_cipher_req_init(u8 *spu_hdr, struct spu_cipher_parms *cipher_parms)
  997. {
  998. struct SPU2_FMD *fmd;
  999. u8 *omd;
  1000. enum spu2_cipher_type spu2_type = SPU2_CIPHER_TYPE_NONE;
  1001. enum spu2_cipher_mode spu2_mode;
  1002. int err;
  1003. flow_log("%s()\n", __func__);
  1004. flow_log(" cipher alg:%u mode:%u type %u\n", cipher_parms->alg,
  1005. cipher_parms->mode, cipher_parms->type);
  1006. flow_log(" cipher_iv_len: %u\n", cipher_parms->iv_len);
  1007. flow_log(" key: %d\n", cipher_parms->key_len);
  1008. flow_dump(" key: ", cipher_parms->key_buf, cipher_parms->key_len);
  1009. /* Convert to spu2 values */
  1010. err = spu2_cipher_xlate(cipher_parms->alg, cipher_parms->mode,
  1011. cipher_parms->type, &spu2_type, &spu2_mode);
  1012. if (err)
  1013. return 0;
  1014. flow_log("spu2 cipher type %s, cipher mode %s\n",
  1015. spu2_ciph_type_name(spu2_type),
  1016. spu2_ciph_mode_name(spu2_mode));
  1017. /* Construct the FMD header */
  1018. fmd = (struct SPU2_FMD *)spu_hdr;
  1019. err = spu2_fmd_init(fmd, spu2_type, spu2_mode, cipher_parms->key_len,
  1020. cipher_parms->iv_len);
  1021. if (err)
  1022. return 0;
  1023. /* Write cipher key to OMD */
  1024. omd = (u8 *)(fmd + 1);
  1025. if (cipher_parms->key_buf && cipher_parms->key_len)
  1026. memcpy(omd, cipher_parms->key_buf, cipher_parms->key_len);
  1027. packet_dump(" SPU request header: ", spu_hdr,
  1028. FMD_SIZE + cipher_parms->key_len + cipher_parms->iv_len);
  1029. return FMD_SIZE + cipher_parms->key_len + cipher_parms->iv_len;
  1030. }
  1031. /**
  1032. * spu_cipher_req_finish() - Finish building a SPU request message header for a
  1033. * block cipher request.
  1034. * @spu_hdr: Start of the request message header (MH field)
  1035. * @spu_req_hdr_len: Length in bytes of the SPU request header
  1036. * @isInbound: 0 encrypt, 1 decrypt
  1037. * @cipher_parms: Parameters describing cipher operation to be performed
  1038. * @update_key: If true, rewrite the cipher key in SCTX
  1039. * @data_size: Length of the data in the BD field
  1040. *
  1041. * Assumes much of the header was already filled in at setkey() time in
  1042. * spu_cipher_req_init().
  1043. * spu_cipher_req_init() fills in the encryption key. For RC4, when submitting a
  1044. * request for a non-first chunk, we use the 260-byte SUPDT field from the
  1045. * previous response as the key. update_key is true for this case. Unused in all
  1046. * other cases.
  1047. */
  1048. void spu2_cipher_req_finish(u8 *spu_hdr,
  1049. u16 spu_req_hdr_len,
  1050. unsigned int is_inbound,
  1051. struct spu_cipher_parms *cipher_parms,
  1052. bool update_key,
  1053. unsigned int data_size)
  1054. {
  1055. struct SPU2_FMD *fmd;
  1056. u8 *omd; /* start of optional metadata */
  1057. u64 ctrl0;
  1058. u64 ctrl3;
  1059. flow_log("%s()\n", __func__);
  1060. flow_log(" in: %u\n", is_inbound);
  1061. flow_log(" cipher alg: %u, cipher_type: %u\n", cipher_parms->alg,
  1062. cipher_parms->type);
  1063. if (update_key) {
  1064. flow_log(" cipher key len: %u\n", cipher_parms->key_len);
  1065. flow_dump(" key: ", cipher_parms->key_buf,
  1066. cipher_parms->key_len);
  1067. }
  1068. flow_log(" iv len: %d\n", cipher_parms->iv_len);
  1069. flow_dump(" iv: ", cipher_parms->iv_buf, cipher_parms->iv_len);
  1070. flow_log(" data_size: %u\n", data_size);
  1071. fmd = (struct SPU2_FMD *)spu_hdr;
  1072. omd = (u8 *)(fmd + 1);
  1073. /*
  1074. * FMD ctrl0 was initialized at setkey time. update it to indicate
  1075. * whether we are encrypting or decrypting.
  1076. */
  1077. ctrl0 = le64_to_cpu(fmd->ctrl0);
  1078. if (is_inbound)
  1079. ctrl0 &= ~SPU2_CIPH_ENCRYPT_EN; /* decrypt */
  1080. else
  1081. ctrl0 |= SPU2_CIPH_ENCRYPT_EN; /* encrypt */
  1082. fmd->ctrl0 = cpu_to_le64(ctrl0);
  1083. if (cipher_parms->alg && cipher_parms->iv_buf && cipher_parms->iv_len) {
  1084. /* cipher iv provided so put it in here */
  1085. memcpy(omd + cipher_parms->key_len, cipher_parms->iv_buf,
  1086. cipher_parms->iv_len);
  1087. }
  1088. ctrl3 = le64_to_cpu(fmd->ctrl3);
  1089. data_size &= SPU2_PL_LEN;
  1090. ctrl3 |= data_size;
  1091. fmd->ctrl3 = cpu_to_le64(ctrl3);
  1092. packet_dump(" SPU request header: ", spu_hdr, spu_req_hdr_len);
  1093. }
  1094. /**
  1095. * spu_request_pad() - Create pad bytes at the end of the data.
  1096. * @pad_start: Start of buffer where pad bytes are to be written
  1097. * @gcm_padding: Length of GCM padding, in bytes
  1098. * @hash_pad_len: Number of bytes of padding extend data to full block
  1099. * @auth_alg: Authentication algorithm
  1100. * @auth_mode: Authentication mode
  1101. * @total_sent: Length inserted at end of hash pad
  1102. * @status_padding: Number of bytes of padding to align STATUS word
  1103. *
  1104. * There may be three forms of pad:
  1105. * 1. GCM pad - for GCM mode ciphers, pad to 16-byte alignment
  1106. * 2. hash pad - pad to a block length, with 0x80 data terminator and
  1107. * size at the end
  1108. * 3. STAT pad - to ensure the STAT field is 4-byte aligned
  1109. */
  1110. void spu2_request_pad(u8 *pad_start, u32 gcm_padding, u32 hash_pad_len,
  1111. enum hash_alg auth_alg, enum hash_mode auth_mode,
  1112. unsigned int total_sent, u32 status_padding)
  1113. {
  1114. u8 *ptr = pad_start;
  1115. /* fix data alignent for GCM */
  1116. if (gcm_padding > 0) {
  1117. flow_log(" GCM: padding to 16 byte alignment: %u bytes\n",
  1118. gcm_padding);
  1119. memset(ptr, 0, gcm_padding);
  1120. ptr += gcm_padding;
  1121. }
  1122. if (hash_pad_len > 0) {
  1123. /* clear the padding section */
  1124. memset(ptr, 0, hash_pad_len);
  1125. /* terminate the data */
  1126. *ptr = 0x80;
  1127. ptr += (hash_pad_len - sizeof(u64));
  1128. /* add the size at the end as required per alg */
  1129. if (auth_alg == HASH_ALG_MD5)
  1130. *(u64 *)ptr = cpu_to_le64((u64)total_sent * 8);
  1131. else /* SHA1, SHA2-224, SHA2-256 */
  1132. *(u64 *)ptr = cpu_to_be64((u64)total_sent * 8);
  1133. ptr += sizeof(u64);
  1134. }
  1135. /* pad to a 4byte alignment for STAT */
  1136. if (status_padding > 0) {
  1137. flow_log(" STAT: padding to 4 byte alignment: %u bytes\n",
  1138. status_padding);
  1139. memset(ptr, 0, status_padding);
  1140. ptr += status_padding;
  1141. }
  1142. }
  1143. /**
  1144. * spu2_xts_tweak_in_payload() - Indicate that SPU2 does NOT place the XTS
  1145. * tweak field in the packet payload (it uses IV instead)
  1146. *
  1147. * Return: 0
  1148. */
  1149. u8 spu2_xts_tweak_in_payload(void)
  1150. {
  1151. return 0;
  1152. }
  1153. /**
  1154. * spu2_tx_status_len() - Return the length of the STATUS field in a SPU
  1155. * response message.
  1156. *
  1157. * Return: Length of STATUS field in bytes.
  1158. */
  1159. u8 spu2_tx_status_len(void)
  1160. {
  1161. return SPU2_TX_STATUS_LEN;
  1162. }
  1163. /**
  1164. * spu2_rx_status_len() - Return the length of the STATUS field in a SPU
  1165. * response message.
  1166. *
  1167. * Return: Length of STATUS field in bytes.
  1168. */
  1169. u8 spu2_rx_status_len(void)
  1170. {
  1171. return SPU2_RX_STATUS_LEN;
  1172. }
  1173. /**
  1174. * spu_status_process() - Process the status from a SPU response message.
  1175. * @statp: start of STATUS word
  1176. *
  1177. * Return: 0 - if status is good and response should be processed
  1178. * !0 - status indicates an error and response is invalid
  1179. */
  1180. int spu2_status_process(u8 *statp)
  1181. {
  1182. /* SPU2 status is 2 bytes by default - SPU_RX_STATUS_LEN */
  1183. u16 status = le16_to_cpu(*(__le16 *)statp);
  1184. if (status == 0)
  1185. return 0;
  1186. flow_log("rx status is %#x\n", status);
  1187. if (status == SPU2_INVALID_ICV)
  1188. return SPU_INVALID_ICV;
  1189. return -EBADMSG;
  1190. }
  1191. /**
  1192. * spu2_ccm_update_iv() - Update the IV as per the requirements for CCM mode.
  1193. *
  1194. * @digestsize: Digest size of this request
  1195. * @cipher_parms: (pointer to) cipher parmaeters, includes IV buf & IV len
  1196. * @assoclen: Length of AAD data
  1197. * @chunksize: length of input data to be sent in this req
  1198. * @is_encrypt: true if this is an output/encrypt operation
  1199. * @is_esp: true if this is an ESP / RFC4309 operation
  1200. *
  1201. */
  1202. void spu2_ccm_update_iv(unsigned int digestsize,
  1203. struct spu_cipher_parms *cipher_parms,
  1204. unsigned int assoclen, unsigned int chunksize,
  1205. bool is_encrypt, bool is_esp)
  1206. {
  1207. int L; /* size of length field, in bytes */
  1208. /*
  1209. * In RFC4309 mode, L is fixed at 4 bytes; otherwise, IV from
  1210. * testmgr contains (L-1) in bottom 3 bits of first byte,
  1211. * per RFC 3610.
  1212. */
  1213. if (is_esp)
  1214. L = CCM_ESP_L_VALUE;
  1215. else
  1216. L = ((cipher_parms->iv_buf[0] & CCM_B0_L_PRIME) >>
  1217. CCM_B0_L_PRIME_SHIFT) + 1;
  1218. /* SPU2 doesn't want these length bytes nor the first byte... */
  1219. cipher_parms->iv_len -= (1 + L);
  1220. memmove(cipher_parms->iv_buf, &cipher_parms->iv_buf[1],
  1221. cipher_parms->iv_len);
  1222. }
  1223. /**
  1224. * spu2_wordalign_padlen() - SPU2 does not require padding.
  1225. * @data_size: length of data field in bytes
  1226. *
  1227. * Return: length of status field padding, in bytes (always 0 on SPU2)
  1228. */
  1229. u32 spu2_wordalign_padlen(u32 data_size)
  1230. {
  1231. return 0;
  1232. }