spu.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright 2016 Broadcom
  4. */
  5. #include <linux/kernel.h>
  6. #include <linux/string.h>
  7. #include "util.h"
  8. #include "spu.h"
  9. #include "spum.h"
  10. #include "cipher.h"
  11. char *hash_alg_name[] = { "None", "md5", "sha1", "sha224", "sha256", "aes",
  12. "sha384", "sha512", "sha3_224", "sha3_256", "sha3_384", "sha3_512" };
  13. char *aead_alg_name[] = { "ccm(aes)", "gcm(aes)", "authenc" };
  14. /* Assumes SPU-M messages are in big endian */
  15. void spum_dump_msg_hdr(u8 *buf, unsigned int buf_len)
  16. {
  17. u8 *ptr = buf;
  18. struct SPUHEADER *spuh = (struct SPUHEADER *)buf;
  19. unsigned int hash_key_len = 0;
  20. unsigned int hash_state_len = 0;
  21. unsigned int cipher_key_len = 0;
  22. unsigned int iv_len;
  23. u32 pflags;
  24. u32 cflags;
  25. u32 ecf;
  26. u32 cipher_alg;
  27. u32 cipher_mode;
  28. u32 cipher_type;
  29. u32 hash_alg;
  30. u32 hash_mode;
  31. u32 hash_type;
  32. u32 sctx_size; /* SCTX length in words */
  33. u32 sctx_pl_len; /* SCTX payload length in bytes */
  34. packet_log("\n");
  35. packet_log("SPU Message header %p len: %u\n", buf, buf_len);
  36. /* ========== Decode MH ========== */
  37. packet_log(" MH 0x%08x\n", be32_to_cpup((__be32 *)ptr));
  38. if (spuh->mh.flags & MH_SCTX_PRES)
  39. packet_log(" SCTX present\n");
  40. if (spuh->mh.flags & MH_BDESC_PRES)
  41. packet_log(" BDESC present\n");
  42. if (spuh->mh.flags & MH_MFM_PRES)
  43. packet_log(" MFM present\n");
  44. if (spuh->mh.flags & MH_BD_PRES)
  45. packet_log(" BD present\n");
  46. if (spuh->mh.flags & MH_HASH_PRES)
  47. packet_log(" HASH present\n");
  48. if (spuh->mh.flags & MH_SUPDT_PRES)
  49. packet_log(" SUPDT present\n");
  50. packet_log(" Opcode 0x%02x\n", spuh->mh.op_code);
  51. ptr += sizeof(spuh->mh) + sizeof(spuh->emh); /* skip emh. unused */
  52. /* ========== Decode SCTX ========== */
  53. if (spuh->mh.flags & MH_SCTX_PRES) {
  54. pflags = be32_to_cpu(spuh->sa.proto_flags);
  55. packet_log(" SCTX[0] 0x%08x\n", pflags);
  56. sctx_size = pflags & SCTX_SIZE;
  57. packet_log(" Size %u words\n", sctx_size);
  58. cflags = be32_to_cpu(spuh->sa.cipher_flags);
  59. packet_log(" SCTX[1] 0x%08x\n", cflags);
  60. packet_log(" Inbound:%lu (1:decrypt/vrfy 0:encrypt/auth)\n",
  61. (cflags & CIPHER_INBOUND) >> CIPHER_INBOUND_SHIFT);
  62. packet_log(" Order:%lu (1:AuthFirst 0:EncFirst)\n",
  63. (cflags & CIPHER_ORDER) >> CIPHER_ORDER_SHIFT);
  64. packet_log(" ICV_IS_512:%lx\n",
  65. (cflags & ICV_IS_512) >> ICV_IS_512_SHIFT);
  66. cipher_alg = (cflags & CIPHER_ALG) >> CIPHER_ALG_SHIFT;
  67. cipher_mode = (cflags & CIPHER_MODE) >> CIPHER_MODE_SHIFT;
  68. cipher_type = (cflags & CIPHER_TYPE) >> CIPHER_TYPE_SHIFT;
  69. packet_log(" Crypto Alg:%u Mode:%u Type:%u\n",
  70. cipher_alg, cipher_mode, cipher_type);
  71. hash_alg = (cflags & HASH_ALG) >> HASH_ALG_SHIFT;
  72. hash_mode = (cflags & HASH_MODE) >> HASH_MODE_SHIFT;
  73. hash_type = (cflags & HASH_TYPE) >> HASH_TYPE_SHIFT;
  74. packet_log(" Hash Alg:%x Mode:%x Type:%x\n",
  75. hash_alg, hash_mode, hash_type);
  76. packet_log(" UPDT_Offset:%u\n", cflags & UPDT_OFST);
  77. ecf = be32_to_cpu(spuh->sa.ecf);
  78. packet_log(" SCTX[2] 0x%08x\n", ecf);
  79. packet_log(" WriteICV:%lu CheckICV:%lu ICV_SIZE:%u ",
  80. (ecf & INSERT_ICV) >> INSERT_ICV_SHIFT,
  81. (ecf & CHECK_ICV) >> CHECK_ICV_SHIFT,
  82. (ecf & ICV_SIZE) >> ICV_SIZE_SHIFT);
  83. packet_log("BD_SUPPRESS:%lu\n",
  84. (ecf & BD_SUPPRESS) >> BD_SUPPRESS_SHIFT);
  85. packet_log(" SCTX_IV:%lu ExplicitIV:%lu GenIV:%lu ",
  86. (ecf & SCTX_IV) >> SCTX_IV_SHIFT,
  87. (ecf & EXPLICIT_IV) >> EXPLICIT_IV_SHIFT,
  88. (ecf & GEN_IV) >> GEN_IV_SHIFT);
  89. packet_log("IV_OV_OFST:%lu EXP_IV_SIZE:%u\n",
  90. (ecf & IV_OFFSET) >> IV_OFFSET_SHIFT,
  91. ecf & EXP_IV_SIZE);
  92. ptr += sizeof(struct SCTX);
  93. if (hash_alg && hash_mode) {
  94. char *name = "NONE";
  95. switch (hash_alg) {
  96. case HASH_ALG_MD5:
  97. hash_key_len = 16;
  98. name = "MD5";
  99. break;
  100. case HASH_ALG_SHA1:
  101. hash_key_len = 20;
  102. name = "SHA1";
  103. break;
  104. case HASH_ALG_SHA224:
  105. hash_key_len = 28;
  106. name = "SHA224";
  107. break;
  108. case HASH_ALG_SHA256:
  109. hash_key_len = 32;
  110. name = "SHA256";
  111. break;
  112. case HASH_ALG_SHA384:
  113. hash_key_len = 48;
  114. name = "SHA384";
  115. break;
  116. case HASH_ALG_SHA512:
  117. hash_key_len = 64;
  118. name = "SHA512";
  119. break;
  120. case HASH_ALG_AES:
  121. hash_key_len = 0;
  122. name = "AES";
  123. break;
  124. case HASH_ALG_NONE:
  125. break;
  126. }
  127. packet_log(" Auth Key Type:%s Length:%u Bytes\n",
  128. name, hash_key_len);
  129. packet_dump(" KEY: ", ptr, hash_key_len);
  130. ptr += hash_key_len;
  131. } else if ((hash_alg == HASH_ALG_AES) &&
  132. (hash_mode == HASH_MODE_XCBC)) {
  133. char *name = "NONE";
  134. switch (cipher_type) {
  135. case CIPHER_TYPE_AES128:
  136. hash_key_len = 16;
  137. name = "AES128-XCBC";
  138. break;
  139. case CIPHER_TYPE_AES192:
  140. hash_key_len = 24;
  141. name = "AES192-XCBC";
  142. break;
  143. case CIPHER_TYPE_AES256:
  144. hash_key_len = 32;
  145. name = "AES256-XCBC";
  146. break;
  147. }
  148. packet_log(" Auth Key Type:%s Length:%u Bytes\n",
  149. name, hash_key_len);
  150. packet_dump(" KEY: ", ptr, hash_key_len);
  151. ptr += hash_key_len;
  152. }
  153. if (hash_alg && (hash_mode == HASH_MODE_NONE) &&
  154. (hash_type == HASH_TYPE_UPDT)) {
  155. char *name = "NONE";
  156. switch (hash_alg) {
  157. case HASH_ALG_MD5:
  158. hash_state_len = 16;
  159. name = "MD5";
  160. break;
  161. case HASH_ALG_SHA1:
  162. hash_state_len = 20;
  163. name = "SHA1";
  164. break;
  165. case HASH_ALG_SHA224:
  166. hash_state_len = 32;
  167. name = "SHA224";
  168. break;
  169. case HASH_ALG_SHA256:
  170. hash_state_len = 32;
  171. name = "SHA256";
  172. break;
  173. case HASH_ALG_SHA384:
  174. hash_state_len = 48;
  175. name = "SHA384";
  176. break;
  177. case HASH_ALG_SHA512:
  178. hash_state_len = 64;
  179. name = "SHA512";
  180. break;
  181. case HASH_ALG_AES:
  182. hash_state_len = 0;
  183. name = "AES";
  184. break;
  185. case HASH_ALG_NONE:
  186. break;
  187. }
  188. packet_log(" Auth State Type:%s Length:%u Bytes\n",
  189. name, hash_state_len);
  190. packet_dump(" State: ", ptr, hash_state_len);
  191. ptr += hash_state_len;
  192. }
  193. if (cipher_alg) {
  194. char *name = "NONE";
  195. switch (cipher_alg) {
  196. case CIPHER_ALG_DES:
  197. cipher_key_len = 8;
  198. name = "DES";
  199. break;
  200. case CIPHER_ALG_3DES:
  201. cipher_key_len = 24;
  202. name = "3DES";
  203. break;
  204. case CIPHER_ALG_AES:
  205. switch (cipher_type) {
  206. case CIPHER_TYPE_AES128:
  207. cipher_key_len = 16;
  208. name = "AES128";
  209. break;
  210. case CIPHER_TYPE_AES192:
  211. cipher_key_len = 24;
  212. name = "AES192";
  213. break;
  214. case CIPHER_TYPE_AES256:
  215. cipher_key_len = 32;
  216. name = "AES256";
  217. break;
  218. }
  219. break;
  220. case CIPHER_ALG_NONE:
  221. break;
  222. }
  223. packet_log(" Cipher Key Type:%s Length:%u Bytes\n",
  224. name, cipher_key_len);
  225. /* XTS has two keys */
  226. if (cipher_mode == CIPHER_MODE_XTS) {
  227. packet_dump(" KEY2: ", ptr, cipher_key_len);
  228. ptr += cipher_key_len;
  229. packet_dump(" KEY1: ", ptr, cipher_key_len);
  230. ptr += cipher_key_len;
  231. cipher_key_len *= 2;
  232. } else {
  233. packet_dump(" KEY: ", ptr, cipher_key_len);
  234. ptr += cipher_key_len;
  235. }
  236. if (ecf & SCTX_IV) {
  237. sctx_pl_len = sctx_size * sizeof(u32) -
  238. sizeof(struct SCTX);
  239. iv_len = sctx_pl_len -
  240. (hash_key_len + hash_state_len +
  241. cipher_key_len);
  242. packet_log(" IV Length:%u Bytes\n", iv_len);
  243. packet_dump(" IV: ", ptr, iv_len);
  244. ptr += iv_len;
  245. }
  246. }
  247. }
  248. /* ========== Decode BDESC ========== */
  249. if (spuh->mh.flags & MH_BDESC_PRES) {
  250. struct BDESC_HEADER *bdesc = (struct BDESC_HEADER *)ptr;
  251. packet_log(" BDESC[0] 0x%08x\n", be32_to_cpup((__be32 *)ptr));
  252. packet_log(" OffsetMAC:%u LengthMAC:%u\n",
  253. be16_to_cpu(bdesc->offset_mac),
  254. be16_to_cpu(bdesc->length_mac));
  255. ptr += sizeof(u32);
  256. packet_log(" BDESC[1] 0x%08x\n", be32_to_cpup((__be32 *)ptr));
  257. packet_log(" OffsetCrypto:%u LengthCrypto:%u\n",
  258. be16_to_cpu(bdesc->offset_crypto),
  259. be16_to_cpu(bdesc->length_crypto));
  260. ptr += sizeof(u32);
  261. packet_log(" BDESC[2] 0x%08x\n", be32_to_cpup((__be32 *)ptr));
  262. packet_log(" OffsetICV:%u OffsetIV:%u\n",
  263. be16_to_cpu(bdesc->offset_icv),
  264. be16_to_cpu(bdesc->offset_iv));
  265. ptr += sizeof(u32);
  266. }
  267. /* ========== Decode BD ========== */
  268. if (spuh->mh.flags & MH_BD_PRES) {
  269. struct BD_HEADER *bd = (struct BD_HEADER *)ptr;
  270. packet_log(" BD[0] 0x%08x\n", be32_to_cpup((__be32 *)ptr));
  271. packet_log(" Size:%ubytes PrevLength:%u\n",
  272. be16_to_cpu(bd->size), be16_to_cpu(bd->prev_length));
  273. ptr += 4;
  274. }
  275. /* Double check sanity */
  276. if (buf + buf_len != ptr) {
  277. packet_log(" Packet parsed incorrectly. ");
  278. packet_log("buf:%p buf_len:%u buf+buf_len:%p ptr:%p\n",
  279. buf, buf_len, buf + buf_len, ptr);
  280. }
  281. packet_log("\n");
  282. }
  283. /**
  284. * spum_ns2_ctx_max_payload() - Determine the max length of the payload for a
  285. * SPU message for a given cipher and hash alg context.
  286. * @cipher_alg: The cipher algorithm
  287. * @cipher_mode: The cipher mode
  288. * @blocksize: The size of a block of data for this algo
  289. *
  290. * The max payload must be a multiple of the blocksize so that if a request is
  291. * too large to fit in a single SPU message, the request can be broken into
  292. * max_payload sized chunks. Each chunk must be a multiple of blocksize.
  293. *
  294. * Return: Max payload length in bytes
  295. */
  296. u32 spum_ns2_ctx_max_payload(enum spu_cipher_alg cipher_alg,
  297. enum spu_cipher_mode cipher_mode,
  298. unsigned int blocksize)
  299. {
  300. u32 max_payload = SPUM_NS2_MAX_PAYLOAD;
  301. u32 excess;
  302. /* In XTS on SPU-M, we'll need to insert tweak before input data */
  303. if (cipher_mode == CIPHER_MODE_XTS)
  304. max_payload -= SPU_XTS_TWEAK_SIZE;
  305. excess = max_payload % blocksize;
  306. return max_payload - excess;
  307. }
  308. /**
  309. * spum_nsp_ctx_max_payload() - Determine the max length of the payload for a
  310. * SPU message for a given cipher and hash alg context.
  311. * @cipher_alg: The cipher algorithm
  312. * @cipher_mode: The cipher mode
  313. * @blocksize: The size of a block of data for this algo
  314. *
  315. * The max payload must be a multiple of the blocksize so that if a request is
  316. * too large to fit in a single SPU message, the request can be broken into
  317. * max_payload sized chunks. Each chunk must be a multiple of blocksize.
  318. *
  319. * Return: Max payload length in bytes
  320. */
  321. u32 spum_nsp_ctx_max_payload(enum spu_cipher_alg cipher_alg,
  322. enum spu_cipher_mode cipher_mode,
  323. unsigned int blocksize)
  324. {
  325. u32 max_payload = SPUM_NSP_MAX_PAYLOAD;
  326. u32 excess;
  327. /* In XTS on SPU-M, we'll need to insert tweak before input data */
  328. if (cipher_mode == CIPHER_MODE_XTS)
  329. max_payload -= SPU_XTS_TWEAK_SIZE;
  330. excess = max_payload % blocksize;
  331. return max_payload - excess;
  332. }
  333. /** spum_payload_length() - Given a SPU-M message header, extract the payload
  334. * length.
  335. * @spu_hdr: Start of SPU header
  336. *
  337. * Assumes just MH, EMH, BD (no SCTX, BDESC. Works for response frames.
  338. *
  339. * Return: payload length in bytes
  340. */
  341. u32 spum_payload_length(u8 *spu_hdr)
  342. {
  343. struct BD_HEADER *bd;
  344. u32 pl_len;
  345. /* Find BD header. skip MH, EMH */
  346. bd = (struct BD_HEADER *)(spu_hdr + 8);
  347. pl_len = be16_to_cpu(bd->size);
  348. return pl_len;
  349. }
  350. /**
  351. * spum_response_hdr_len() - Given the length of the hash key and encryption
  352. * key, determine the expected length of a SPU response header.
  353. * @auth_key_len: authentication key length (bytes)
  354. * @enc_key_len: encryption key length (bytes)
  355. * @is_hash: true if response message is for a hash operation
  356. *
  357. * Return: length of SPU response header (bytes)
  358. */
  359. u16 spum_response_hdr_len(u16 auth_key_len, u16 enc_key_len, bool is_hash)
  360. {
  361. if (is_hash)
  362. return SPU_HASH_RESP_HDR_LEN;
  363. else
  364. return SPU_RESP_HDR_LEN;
  365. }
  366. /**
  367. * spum_hash_pad_len() - Calculate the length of hash padding required to extend
  368. * data to a full block size.
  369. * @hash_alg: hash algorithm
  370. * @hash_mode: hash mode
  371. * @chunksize: length of data, in bytes
  372. * @hash_block_size: size of a block of data for hash algorithm
  373. *
  374. * Reserve space for 1 byte (0x80) start of pad and the total length as u64
  375. *
  376. * Return: length of hash pad in bytes
  377. */
  378. u16 spum_hash_pad_len(enum hash_alg hash_alg, enum hash_mode hash_mode,
  379. u32 chunksize, u16 hash_block_size)
  380. {
  381. unsigned int length_len;
  382. unsigned int used_space_last_block;
  383. int hash_pad_len;
  384. /* AES-XCBC hash requires just padding to next block boundary */
  385. if ((hash_alg == HASH_ALG_AES) && (hash_mode == HASH_MODE_XCBC)) {
  386. used_space_last_block = chunksize % hash_block_size;
  387. hash_pad_len = hash_block_size - used_space_last_block;
  388. if (hash_pad_len >= hash_block_size)
  389. hash_pad_len -= hash_block_size;
  390. return hash_pad_len;
  391. }
  392. used_space_last_block = chunksize % hash_block_size + 1;
  393. if ((hash_alg == HASH_ALG_SHA384) || (hash_alg == HASH_ALG_SHA512))
  394. length_len = 2 * sizeof(u64);
  395. else
  396. length_len = sizeof(u64);
  397. used_space_last_block += length_len;
  398. hash_pad_len = hash_block_size - used_space_last_block;
  399. if (hash_pad_len < 0)
  400. hash_pad_len += hash_block_size;
  401. hash_pad_len += 1 + length_len;
  402. return hash_pad_len;
  403. }
  404. /**
  405. * spum_gcm_ccm_pad_len() - Determine the required length of GCM or CCM padding.
  406. * @cipher_mode: Algo type
  407. * @data_size: Length of plaintext (bytes)
  408. *
  409. * Return: Length of padding, in bytes
  410. */
  411. u32 spum_gcm_ccm_pad_len(enum spu_cipher_mode cipher_mode,
  412. unsigned int data_size)
  413. {
  414. u32 pad_len = 0;
  415. u32 m1 = SPU_GCM_CCM_ALIGN - 1;
  416. if ((cipher_mode == CIPHER_MODE_GCM) ||
  417. (cipher_mode == CIPHER_MODE_CCM))
  418. pad_len = ((data_size + m1) & ~m1) - data_size;
  419. return pad_len;
  420. }
  421. /**
  422. * spum_assoc_resp_len() - Determine the size of the receive buffer required to
  423. * catch associated data.
  424. * @cipher_mode: cipher mode
  425. * @assoc_len: length of associated data (bytes)
  426. * @iv_len: length of IV (bytes)
  427. * @is_encrypt: true if encrypting. false if decrypting.
  428. *
  429. * Return: length of associated data in response message (bytes)
  430. */
  431. u32 spum_assoc_resp_len(enum spu_cipher_mode cipher_mode,
  432. unsigned int assoc_len, unsigned int iv_len,
  433. bool is_encrypt)
  434. {
  435. u32 buflen = 0;
  436. u32 pad;
  437. if (assoc_len)
  438. buflen = assoc_len;
  439. if (cipher_mode == CIPHER_MODE_GCM) {
  440. /* AAD needs to be padded in responses too */
  441. pad = spum_gcm_ccm_pad_len(cipher_mode, buflen);
  442. buflen += pad;
  443. }
  444. if (cipher_mode == CIPHER_MODE_CCM) {
  445. /*
  446. * AAD needs to be padded in responses too
  447. * for CCM, len + 2 needs to be 128-bit aligned.
  448. */
  449. pad = spum_gcm_ccm_pad_len(cipher_mode, buflen + 2);
  450. buflen += pad;
  451. }
  452. return buflen;
  453. }
  454. /**
  455. * spum_aead_ivlen() - Calculate the length of the AEAD IV to be included
  456. * in a SPU request after the AAD and before the payload.
  457. * @cipher_mode: cipher mode
  458. * @iv_len: initialization vector length in bytes
  459. *
  460. * In Linux ~4.2 and later, the assoc_data sg includes the IV. So no need
  461. * to include the IV as a separate field in the SPU request msg.
  462. *
  463. * Return: Length of AEAD IV in bytes
  464. */
  465. u8 spum_aead_ivlen(enum spu_cipher_mode cipher_mode, u16 iv_len)
  466. {
  467. return 0;
  468. }
  469. /**
  470. * spum_hash_type() - Determine the type of hash operation.
  471. * @src_sent: The number of bytes in the current request that have already
  472. * been sent to the SPU to be hashed.
  473. *
  474. * We do not use HASH_TYPE_FULL for requests that fit in a single SPU message.
  475. * Using FULL causes failures (such as when the string to be hashed is empty).
  476. * For similar reasons, we never use HASH_TYPE_FIN. Instead, submit messages
  477. * as INIT or UPDT and do the hash padding in sw.
  478. */
  479. enum hash_type spum_hash_type(u32 src_sent)
  480. {
  481. return src_sent ? HASH_TYPE_UPDT : HASH_TYPE_INIT;
  482. }
  483. /**
  484. * spum_digest_size() - Determine the size of a hash digest to expect the SPU to
  485. * return.
  486. * @alg_digest_size: Number of bytes in the final digest for the given algo
  487. * @alg: The hash algorithm
  488. * @htype: Type of hash operation (init, update, full, etc)
  489. *
  490. * When doing incremental hashing for an algorithm with a truncated hash
  491. * (e.g., SHA224), the SPU returns the full digest so that it can be fed back as
  492. * a partial result for the next chunk.
  493. */
  494. u32 spum_digest_size(u32 alg_digest_size, enum hash_alg alg,
  495. enum hash_type htype)
  496. {
  497. u32 digestsize = alg_digest_size;
  498. /* SPU returns complete digest when doing incremental hash and truncated
  499. * hash algo.
  500. */
  501. if ((htype == HASH_TYPE_INIT) || (htype == HASH_TYPE_UPDT)) {
  502. if (alg == HASH_ALG_SHA224)
  503. digestsize = SHA256_DIGEST_SIZE;
  504. else if (alg == HASH_ALG_SHA384)
  505. digestsize = SHA512_DIGEST_SIZE;
  506. }
  507. return digestsize;
  508. }
  509. /**
  510. * spum_create_request() - Build a SPU request message header, up to and
  511. * including the BD header. Construct the message starting at spu_hdr. Caller
  512. * should allocate this buffer in DMA-able memory at least SPU_HEADER_ALLOC_LEN
  513. * bytes long.
  514. * @spu_hdr: Start of buffer where SPU request header is to be written
  515. * @req_opts: SPU request message options
  516. * @cipher_parms: Parameters related to cipher algorithm
  517. * @hash_parms: Parameters related to hash algorithm
  518. * @aead_parms: Parameters related to AEAD operation
  519. * @data_size: Length of data to be encrypted or authenticated. If AEAD, does
  520. * not include length of AAD.
  521. *
  522. * Return: the length of the SPU header in bytes. 0 if an error occurs.
  523. */
  524. u32 spum_create_request(u8 *spu_hdr,
  525. struct spu_request_opts *req_opts,
  526. struct spu_cipher_parms *cipher_parms,
  527. struct spu_hash_parms *hash_parms,
  528. struct spu_aead_parms *aead_parms,
  529. unsigned int data_size)
  530. {
  531. struct SPUHEADER *spuh;
  532. struct BDESC_HEADER *bdesc;
  533. struct BD_HEADER *bd;
  534. u8 *ptr;
  535. u32 protocol_bits = 0;
  536. u32 cipher_bits = 0;
  537. u32 ecf_bits = 0;
  538. u8 sctx_words = 0;
  539. unsigned int buf_len = 0;
  540. /* size of the cipher payload */
  541. unsigned int cipher_len = hash_parms->prebuf_len + data_size +
  542. hash_parms->pad_len;
  543. /* offset of prebuf or data from end of BD header */
  544. unsigned int cipher_offset = aead_parms->assoc_size +
  545. aead_parms->iv_len + aead_parms->aad_pad_len;
  546. /* total size of the DB data (without STAT word padding) */
  547. unsigned int real_db_size = spu_real_db_size(aead_parms->assoc_size,
  548. aead_parms->iv_len,
  549. hash_parms->prebuf_len,
  550. data_size,
  551. aead_parms->aad_pad_len,
  552. aead_parms->data_pad_len,
  553. hash_parms->pad_len);
  554. unsigned int auth_offset = 0;
  555. unsigned int offset_iv = 0;
  556. /* size/offset of the auth payload */
  557. unsigned int auth_len;
  558. auth_len = real_db_size;
  559. if (req_opts->is_aead && req_opts->is_inbound)
  560. cipher_len -= hash_parms->digestsize;
  561. if (req_opts->is_aead && req_opts->is_inbound)
  562. auth_len -= hash_parms->digestsize;
  563. if ((hash_parms->alg == HASH_ALG_AES) &&
  564. (hash_parms->mode == HASH_MODE_XCBC)) {
  565. auth_len -= hash_parms->pad_len;
  566. cipher_len -= hash_parms->pad_len;
  567. }
  568. flow_log("%s()\n", __func__);
  569. flow_log(" in:%u authFirst:%u\n",
  570. req_opts->is_inbound, req_opts->auth_first);
  571. flow_log(" %s. cipher alg:%u mode:%u type %u\n",
  572. spu_alg_name(cipher_parms->alg, cipher_parms->mode),
  573. cipher_parms->alg, cipher_parms->mode, cipher_parms->type);
  574. flow_log(" key: %d\n", cipher_parms->key_len);
  575. flow_dump(" key: ", cipher_parms->key_buf, cipher_parms->key_len);
  576. flow_log(" iv: %d\n", cipher_parms->iv_len);
  577. flow_dump(" iv: ", cipher_parms->iv_buf, cipher_parms->iv_len);
  578. flow_log(" auth alg:%u mode:%u type %u\n",
  579. hash_parms->alg, hash_parms->mode, hash_parms->type);
  580. flow_log(" digestsize: %u\n", hash_parms->digestsize);
  581. flow_log(" authkey: %d\n", hash_parms->key_len);
  582. flow_dump(" authkey: ", hash_parms->key_buf, hash_parms->key_len);
  583. flow_log(" assoc_size:%u\n", aead_parms->assoc_size);
  584. flow_log(" prebuf_len:%u\n", hash_parms->prebuf_len);
  585. flow_log(" data_size:%u\n", data_size);
  586. flow_log(" hash_pad_len:%u\n", hash_parms->pad_len);
  587. flow_log(" real_db_size:%u\n", real_db_size);
  588. flow_log(" auth_offset:%u auth_len:%u cipher_offset:%u cipher_len:%u\n",
  589. auth_offset, auth_len, cipher_offset, cipher_len);
  590. flow_log(" aead_iv: %u\n", aead_parms->iv_len);
  591. /* starting out: zero the header (plus some) */
  592. ptr = spu_hdr;
  593. memset(ptr, 0, sizeof(struct SPUHEADER));
  594. /* format master header word */
  595. /* Do not set the next bit even though the datasheet says to */
  596. spuh = (struct SPUHEADER *)ptr;
  597. ptr += sizeof(struct SPUHEADER);
  598. buf_len += sizeof(struct SPUHEADER);
  599. spuh->mh.op_code = SPU_CRYPTO_OPERATION_GENERIC;
  600. spuh->mh.flags |= (MH_SCTX_PRES | MH_BDESC_PRES | MH_BD_PRES);
  601. /* Format sctx word 0 (protocol_bits) */
  602. sctx_words = 3; /* size in words */
  603. /* Format sctx word 1 (cipher_bits) */
  604. if (req_opts->is_inbound)
  605. cipher_bits |= CIPHER_INBOUND;
  606. if (req_opts->auth_first)
  607. cipher_bits |= CIPHER_ORDER;
  608. /* Set the crypto parameters in the cipher.flags */
  609. cipher_bits |= cipher_parms->alg << CIPHER_ALG_SHIFT;
  610. cipher_bits |= cipher_parms->mode << CIPHER_MODE_SHIFT;
  611. cipher_bits |= cipher_parms->type << CIPHER_TYPE_SHIFT;
  612. /* Set the auth parameters in the cipher.flags */
  613. cipher_bits |= hash_parms->alg << HASH_ALG_SHIFT;
  614. cipher_bits |= hash_parms->mode << HASH_MODE_SHIFT;
  615. cipher_bits |= hash_parms->type << HASH_TYPE_SHIFT;
  616. /*
  617. * Format sctx extensions if required, and update main fields if
  618. * required)
  619. */
  620. if (hash_parms->alg) {
  621. /* Write the authentication key material if present */
  622. if (hash_parms->key_len) {
  623. memcpy(ptr, hash_parms->key_buf, hash_parms->key_len);
  624. ptr += hash_parms->key_len;
  625. buf_len += hash_parms->key_len;
  626. sctx_words += hash_parms->key_len / 4;
  627. }
  628. if ((cipher_parms->mode == CIPHER_MODE_GCM) ||
  629. (cipher_parms->mode == CIPHER_MODE_CCM))
  630. /* unpadded length */
  631. offset_iv = aead_parms->assoc_size;
  632. /* if GCM/CCM we need to write ICV into the payload */
  633. if (!req_opts->is_inbound) {
  634. if ((cipher_parms->mode == CIPHER_MODE_GCM) ||
  635. (cipher_parms->mode == CIPHER_MODE_CCM))
  636. ecf_bits |= 1 << INSERT_ICV_SHIFT;
  637. } else {
  638. ecf_bits |= CHECK_ICV;
  639. }
  640. /* Inform the SPU of the ICV size (in words) */
  641. if (hash_parms->digestsize == 64)
  642. cipher_bits |= ICV_IS_512;
  643. else
  644. ecf_bits |=
  645. (hash_parms->digestsize / 4) << ICV_SIZE_SHIFT;
  646. }
  647. if (req_opts->bd_suppress)
  648. ecf_bits |= BD_SUPPRESS;
  649. /* copy the encryption keys in the SAD entry */
  650. if (cipher_parms->alg) {
  651. if (cipher_parms->key_len) {
  652. memcpy(ptr, cipher_parms->key_buf,
  653. cipher_parms->key_len);
  654. ptr += cipher_parms->key_len;
  655. buf_len += cipher_parms->key_len;
  656. sctx_words += cipher_parms->key_len / 4;
  657. }
  658. /*
  659. * if encrypting then set IV size, use SCTX IV unless no IV
  660. * given here
  661. */
  662. if (cipher_parms->iv_buf && cipher_parms->iv_len) {
  663. /* Use SCTX IV */
  664. ecf_bits |= SCTX_IV;
  665. /* cipher iv provided so put it in here */
  666. memcpy(ptr, cipher_parms->iv_buf, cipher_parms->iv_len);
  667. ptr += cipher_parms->iv_len;
  668. buf_len += cipher_parms->iv_len;
  669. sctx_words += cipher_parms->iv_len / 4;
  670. }
  671. }
  672. /*
  673. * RFC4543 (GMAC/ESP) requires data to be sent as part of AAD
  674. * so we need to override the BDESC parameters.
  675. */
  676. if (req_opts->is_rfc4543) {
  677. if (req_opts->is_inbound)
  678. data_size -= hash_parms->digestsize;
  679. offset_iv = aead_parms->assoc_size + data_size;
  680. cipher_len = 0;
  681. cipher_offset = offset_iv;
  682. auth_len = cipher_offset + aead_parms->data_pad_len;
  683. }
  684. /* write in the total sctx length now that we know it */
  685. protocol_bits |= sctx_words;
  686. /* Endian adjust the SCTX */
  687. spuh->sa.proto_flags = cpu_to_be32(protocol_bits);
  688. spuh->sa.cipher_flags = cpu_to_be32(cipher_bits);
  689. spuh->sa.ecf = cpu_to_be32(ecf_bits);
  690. /* === create the BDESC section === */
  691. bdesc = (struct BDESC_HEADER *)ptr;
  692. bdesc->offset_mac = cpu_to_be16(auth_offset);
  693. bdesc->length_mac = cpu_to_be16(auth_len);
  694. bdesc->offset_crypto = cpu_to_be16(cipher_offset);
  695. bdesc->length_crypto = cpu_to_be16(cipher_len);
  696. /*
  697. * CCM in SPU-M requires that ICV not be in same 32-bit word as data or
  698. * padding. So account for padding as necessary.
  699. */
  700. if (cipher_parms->mode == CIPHER_MODE_CCM)
  701. auth_len += spum_wordalign_padlen(auth_len);
  702. bdesc->offset_icv = cpu_to_be16(auth_len);
  703. bdesc->offset_iv = cpu_to_be16(offset_iv);
  704. ptr += sizeof(struct BDESC_HEADER);
  705. buf_len += sizeof(struct BDESC_HEADER);
  706. /* === no MFM section === */
  707. /* === create the BD section === */
  708. /* add the BD header */
  709. bd = (struct BD_HEADER *)ptr;
  710. bd->size = cpu_to_be16(real_db_size);
  711. bd->prev_length = 0;
  712. ptr += sizeof(struct BD_HEADER);
  713. buf_len += sizeof(struct BD_HEADER);
  714. packet_dump(" SPU request header: ", spu_hdr, buf_len);
  715. return buf_len;
  716. }
  717. /**
  718. * spum_cipher_req_init() - Build a SPU request message header, up to and
  719. * including the BD header.
  720. * @spu_hdr: Start of SPU request header (MH)
  721. * @cipher_parms: Parameters that describe the cipher request
  722. *
  723. * Construct the message starting at spu_hdr. Caller should allocate this buffer
  724. * in DMA-able memory at least SPU_HEADER_ALLOC_LEN bytes long.
  725. *
  726. * Return: the length of the SPU header in bytes. 0 if an error occurs.
  727. */
  728. u16 spum_cipher_req_init(u8 *spu_hdr, struct spu_cipher_parms *cipher_parms)
  729. {
  730. struct SPUHEADER *spuh;
  731. u32 protocol_bits = 0;
  732. u32 cipher_bits = 0;
  733. u32 ecf_bits = 0;
  734. u8 sctx_words = 0;
  735. u8 *ptr = spu_hdr;
  736. flow_log("%s()\n", __func__);
  737. flow_log(" cipher alg:%u mode:%u type %u\n", cipher_parms->alg,
  738. cipher_parms->mode, cipher_parms->type);
  739. flow_log(" cipher_iv_len: %u\n", cipher_parms->iv_len);
  740. flow_log(" key: %d\n", cipher_parms->key_len);
  741. flow_dump(" key: ", cipher_parms->key_buf, cipher_parms->key_len);
  742. /* starting out: zero the header (plus some) */
  743. memset(spu_hdr, 0, sizeof(struct SPUHEADER));
  744. ptr += sizeof(struct SPUHEADER);
  745. /* format master header word */
  746. /* Do not set the next bit even though the datasheet says to */
  747. spuh = (struct SPUHEADER *)spu_hdr;
  748. spuh->mh.op_code = SPU_CRYPTO_OPERATION_GENERIC;
  749. spuh->mh.flags |= (MH_SCTX_PRES | MH_BDESC_PRES | MH_BD_PRES);
  750. /* Format sctx word 0 (protocol_bits) */
  751. sctx_words = 3; /* size in words */
  752. /* copy the encryption keys in the SAD entry */
  753. if (cipher_parms->alg) {
  754. if (cipher_parms->key_len) {
  755. ptr += cipher_parms->key_len;
  756. sctx_words += cipher_parms->key_len / 4;
  757. }
  758. /*
  759. * if encrypting then set IV size, use SCTX IV unless no IV
  760. * given here
  761. */
  762. if (cipher_parms->iv_len) {
  763. /* Use SCTX IV */
  764. ecf_bits |= SCTX_IV;
  765. ptr += cipher_parms->iv_len;
  766. sctx_words += cipher_parms->iv_len / 4;
  767. }
  768. }
  769. /* Set the crypto parameters in the cipher.flags */
  770. cipher_bits |= cipher_parms->alg << CIPHER_ALG_SHIFT;
  771. cipher_bits |= cipher_parms->mode << CIPHER_MODE_SHIFT;
  772. cipher_bits |= cipher_parms->type << CIPHER_TYPE_SHIFT;
  773. /* copy the encryption keys in the SAD entry */
  774. if (cipher_parms->alg && cipher_parms->key_len)
  775. memcpy(spuh + 1, cipher_parms->key_buf, cipher_parms->key_len);
  776. /* write in the total sctx length now that we know it */
  777. protocol_bits |= sctx_words;
  778. /* Endian adjust the SCTX */
  779. spuh->sa.proto_flags = cpu_to_be32(protocol_bits);
  780. /* Endian adjust the SCTX */
  781. spuh->sa.cipher_flags = cpu_to_be32(cipher_bits);
  782. spuh->sa.ecf = cpu_to_be32(ecf_bits);
  783. packet_dump(" SPU request header: ", spu_hdr,
  784. sizeof(struct SPUHEADER));
  785. return sizeof(struct SPUHEADER) + cipher_parms->key_len +
  786. cipher_parms->iv_len + sizeof(struct BDESC_HEADER) +
  787. sizeof(struct BD_HEADER);
  788. }
  789. /**
  790. * spum_cipher_req_finish() - Finish building a SPU request message header for a
  791. * block cipher request. Assumes much of the header was already filled in at
  792. * setkey() time in spu_cipher_req_init().
  793. * @spu_hdr: Start of the request message header (MH field)
  794. * @spu_req_hdr_len: Length in bytes of the SPU request header
  795. * @is_inbound: 0 encrypt, 1 decrypt
  796. * @cipher_parms: Parameters describing cipher operation to be performed
  797. * @data_size: Length of the data in the BD field
  798. *
  799. * Assumes much of the header was already filled in at setkey() time in
  800. * spum_cipher_req_init().
  801. * spum_cipher_req_init() fills in the encryption key.
  802. */
  803. void spum_cipher_req_finish(u8 *spu_hdr,
  804. u16 spu_req_hdr_len,
  805. unsigned int is_inbound,
  806. struct spu_cipher_parms *cipher_parms,
  807. unsigned int data_size)
  808. {
  809. struct SPUHEADER *spuh;
  810. struct BDESC_HEADER *bdesc;
  811. struct BD_HEADER *bd;
  812. u8 *bdesc_ptr = spu_hdr + spu_req_hdr_len -
  813. (sizeof(struct BD_HEADER) + sizeof(struct BDESC_HEADER));
  814. u32 cipher_bits;
  815. flow_log("%s()\n", __func__);
  816. flow_log(" in: %u\n", is_inbound);
  817. flow_log(" cipher alg: %u, cipher_type: %u\n", cipher_parms->alg,
  818. cipher_parms->type);
  819. /*
  820. * In XTS mode, API puts "i" parameter (block tweak) in IV. For
  821. * SPU-M, should be in start of the BD; tx_sg_create() copies it there.
  822. * IV in SPU msg for SPU-M should be 0, since that's the "j" parameter
  823. * (block ctr within larger data unit) - given we can send entire disk
  824. * block (<= 4KB) in 1 SPU msg, don't need to use this parameter.
  825. */
  826. if (cipher_parms->mode == CIPHER_MODE_XTS)
  827. memset(cipher_parms->iv_buf, 0, cipher_parms->iv_len);
  828. flow_log(" iv len: %d\n", cipher_parms->iv_len);
  829. flow_dump(" iv: ", cipher_parms->iv_buf, cipher_parms->iv_len);
  830. flow_log(" data_size: %u\n", data_size);
  831. /* format master header word */
  832. /* Do not set the next bit even though the datasheet says to */
  833. spuh = (struct SPUHEADER *)spu_hdr;
  834. /* cipher_bits was initialized at setkey time */
  835. cipher_bits = be32_to_cpu(spuh->sa.cipher_flags);
  836. /* Format sctx word 1 (cipher_bits) */
  837. if (is_inbound)
  838. cipher_bits |= CIPHER_INBOUND;
  839. else
  840. cipher_bits &= ~CIPHER_INBOUND;
  841. if (cipher_parms->alg && cipher_parms->iv_buf && cipher_parms->iv_len)
  842. /* cipher iv provided so put it in here */
  843. memcpy(bdesc_ptr - cipher_parms->iv_len, cipher_parms->iv_buf,
  844. cipher_parms->iv_len);
  845. spuh->sa.cipher_flags = cpu_to_be32(cipher_bits);
  846. /* === create the BDESC section === */
  847. bdesc = (struct BDESC_HEADER *)bdesc_ptr;
  848. bdesc->offset_mac = 0;
  849. bdesc->length_mac = 0;
  850. bdesc->offset_crypto = 0;
  851. /* XTS mode, data_size needs to include tweak parameter */
  852. if (cipher_parms->mode == CIPHER_MODE_XTS)
  853. bdesc->length_crypto = cpu_to_be16(data_size +
  854. SPU_XTS_TWEAK_SIZE);
  855. else
  856. bdesc->length_crypto = cpu_to_be16(data_size);
  857. bdesc->offset_icv = 0;
  858. bdesc->offset_iv = 0;
  859. /* === no MFM section === */
  860. /* === create the BD section === */
  861. /* add the BD header */
  862. bd = (struct BD_HEADER *)(bdesc_ptr + sizeof(struct BDESC_HEADER));
  863. bd->size = cpu_to_be16(data_size);
  864. /* XTS mode, data_size needs to include tweak parameter */
  865. if (cipher_parms->mode == CIPHER_MODE_XTS)
  866. bd->size = cpu_to_be16(data_size + SPU_XTS_TWEAK_SIZE);
  867. else
  868. bd->size = cpu_to_be16(data_size);
  869. bd->prev_length = 0;
  870. packet_dump(" SPU request header: ", spu_hdr, spu_req_hdr_len);
  871. }
  872. /**
  873. * spum_request_pad() - Create pad bytes at the end of the data.
  874. * @pad_start: Start of buffer where pad bytes are to be written
  875. * @gcm_ccm_padding: length of GCM/CCM padding, in bytes
  876. * @hash_pad_len: Number of bytes of padding extend data to full block
  877. * @auth_alg: authentication algorithm
  878. * @auth_mode: authentication mode
  879. * @total_sent: length inserted at end of hash pad
  880. * @status_padding: Number of bytes of padding to align STATUS word
  881. *
  882. * There may be three forms of pad:
  883. * 1. GCM/CCM pad - for GCM/CCM mode ciphers, pad to 16-byte alignment
  884. * 2. hash pad - pad to a block length, with 0x80 data terminator and
  885. * size at the end
  886. * 3. STAT pad - to ensure the STAT field is 4-byte aligned
  887. */
  888. void spum_request_pad(u8 *pad_start,
  889. u32 gcm_ccm_padding,
  890. u32 hash_pad_len,
  891. enum hash_alg auth_alg,
  892. enum hash_mode auth_mode,
  893. unsigned int total_sent, u32 status_padding)
  894. {
  895. u8 *ptr = pad_start;
  896. /* fix data alignent for GCM/CCM */
  897. if (gcm_ccm_padding > 0) {
  898. flow_log(" GCM: padding to 16 byte alignment: %u bytes\n",
  899. gcm_ccm_padding);
  900. memset(ptr, 0, gcm_ccm_padding);
  901. ptr += gcm_ccm_padding;
  902. }
  903. if (hash_pad_len > 0) {
  904. /* clear the padding section */
  905. memset(ptr, 0, hash_pad_len);
  906. if ((auth_alg == HASH_ALG_AES) &&
  907. (auth_mode == HASH_MODE_XCBC)) {
  908. /* AES/XCBC just requires padding to be 0s */
  909. ptr += hash_pad_len;
  910. } else {
  911. /* terminate the data */
  912. *ptr = 0x80;
  913. ptr += (hash_pad_len - sizeof(u64));
  914. /* add the size at the end as required per alg */
  915. if (auth_alg == HASH_ALG_MD5)
  916. *(__le64 *)ptr = cpu_to_le64(total_sent * 8ull);
  917. else /* SHA1, SHA2-224, SHA2-256 */
  918. *(__be64 *)ptr = cpu_to_be64(total_sent * 8ull);
  919. ptr += sizeof(u64);
  920. }
  921. }
  922. /* pad to a 4byte alignment for STAT */
  923. if (status_padding > 0) {
  924. flow_log(" STAT: padding to 4 byte alignment: %u bytes\n",
  925. status_padding);
  926. memset(ptr, 0, status_padding);
  927. ptr += status_padding;
  928. }
  929. }
  930. /**
  931. * spum_xts_tweak_in_payload() - Indicate that SPUM DOES place the XTS tweak
  932. * field in the packet payload (rather than using IV)
  933. *
  934. * Return: 1
  935. */
  936. u8 spum_xts_tweak_in_payload(void)
  937. {
  938. return 1;
  939. }
  940. /**
  941. * spum_tx_status_len() - Return the length of the STATUS field in a SPU
  942. * response message.
  943. *
  944. * Return: Length of STATUS field in bytes.
  945. */
  946. u8 spum_tx_status_len(void)
  947. {
  948. return SPU_TX_STATUS_LEN;
  949. }
  950. /**
  951. * spum_rx_status_len() - Return the length of the STATUS field in a SPU
  952. * response message.
  953. *
  954. * Return: Length of STATUS field in bytes.
  955. */
  956. u8 spum_rx_status_len(void)
  957. {
  958. return SPU_RX_STATUS_LEN;
  959. }
  960. /**
  961. * spum_status_process() - Process the status from a SPU response message.
  962. * @statp: start of STATUS word
  963. * Return:
  964. * 0 - if status is good and response should be processed
  965. * !0 - status indicates an error and response is invalid
  966. */
  967. int spum_status_process(u8 *statp)
  968. {
  969. u32 status;
  970. status = __be32_to_cpu(*(__be32 *)statp);
  971. flow_log("SPU response STATUS %#08x\n", status);
  972. if (status & SPU_STATUS_ERROR_FLAG) {
  973. pr_err("%s() Warning: Error result from SPU: %#08x\n",
  974. __func__, status);
  975. if (status & SPU_STATUS_INVALID_ICV)
  976. return SPU_INVALID_ICV;
  977. return -EBADMSG;
  978. }
  979. return 0;
  980. }
  981. /**
  982. * spum_ccm_update_iv() - Update the IV as per the requirements for CCM mode.
  983. *
  984. * @digestsize: Digest size of this request
  985. * @cipher_parms: (pointer to) cipher parmaeters, includes IV buf & IV len
  986. * @assoclen: Length of AAD data
  987. * @chunksize: length of input data to be sent in this req
  988. * @is_encrypt: true if this is an output/encrypt operation
  989. * @is_esp: true if this is an ESP / RFC4309 operation
  990. *
  991. */
  992. void spum_ccm_update_iv(unsigned int digestsize,
  993. struct spu_cipher_parms *cipher_parms,
  994. unsigned int assoclen,
  995. unsigned int chunksize,
  996. bool is_encrypt,
  997. bool is_esp)
  998. {
  999. u8 L; /* L from CCM algorithm, length of plaintext data */
  1000. u8 mprime; /* M' from CCM algo, (M - 2) / 2, where M=authsize */
  1001. u8 adata;
  1002. if (cipher_parms->iv_len != CCM_AES_IV_SIZE) {
  1003. pr_err("%s(): Invalid IV len %d for CCM mode, should be %d\n",
  1004. __func__, cipher_parms->iv_len, CCM_AES_IV_SIZE);
  1005. return;
  1006. }
  1007. /*
  1008. * IV needs to be formatted as follows:
  1009. *
  1010. * | Byte 0 | Bytes 1 - N | Bytes (N+1) - 15 |
  1011. * | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | Bits 7 - 0 | Bits 7 - 0 |
  1012. * | 0 |Ad?|(M - 2) / 2| L - 1 | Nonce | Plaintext Length |
  1013. *
  1014. * Ad? = 1 if AAD present, 0 if not present
  1015. * M = size of auth field, 8, 12, or 16 bytes (SPU-M) -or-
  1016. * 4, 6, 8, 10, 12, 14, 16 bytes (SPU2)
  1017. * L = Size of Plaintext Length field; Nonce size = 15 - L
  1018. *
  1019. * It appears that the crypto API already expects the L-1 portion
  1020. * to be set in the first byte of the IV, which implicitly determines
  1021. * the nonce size, and also fills in the nonce. But the other bits
  1022. * in byte 0 as well as the plaintext length need to be filled in.
  1023. *
  1024. * In rfc4309/esp mode, L is not already in the supplied IV and
  1025. * we need to fill it in, as well as move the IV data to be after
  1026. * the salt
  1027. */
  1028. if (is_esp) {
  1029. L = CCM_ESP_L_VALUE; /* RFC4309 has fixed L */
  1030. } else {
  1031. /* L' = plaintext length - 1 so Plaintext length is L' + 1 */
  1032. L = ((cipher_parms->iv_buf[0] & CCM_B0_L_PRIME) >>
  1033. CCM_B0_L_PRIME_SHIFT) + 1;
  1034. }
  1035. mprime = (digestsize - 2) >> 1; /* M' = (M - 2) / 2 */
  1036. adata = (assoclen > 0); /* adata = 1 if any associated data */
  1037. cipher_parms->iv_buf[0] = (adata << CCM_B0_ADATA_SHIFT) |
  1038. (mprime << CCM_B0_M_PRIME_SHIFT) |
  1039. ((L - 1) << CCM_B0_L_PRIME_SHIFT);
  1040. /* Nonce is already filled in by crypto API, and is 15 - L bytes */
  1041. /* Don't include digest in plaintext size when decrypting */
  1042. if (!is_encrypt)
  1043. chunksize -= digestsize;
  1044. /* Fill in length of plaintext, formatted to be L bytes long */
  1045. format_value_ccm(chunksize, &cipher_parms->iv_buf[15 - L + 1], L);
  1046. }
  1047. /**
  1048. * spum_wordalign_padlen() - Given the length of a data field, determine the
  1049. * padding required to align the data following this field on a 4-byte boundary.
  1050. * @data_size: length of data field in bytes
  1051. *
  1052. * Return: length of status field padding, in bytes
  1053. */
  1054. u32 spum_wordalign_padlen(u32 data_size)
  1055. {
  1056. return ((data_size + 3) & ~3) - data_size;
  1057. }