spu2.c 39 KB

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