caamalg.c 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249
  1. /*
  2. * caam - Freescale FSL CAAM support for crypto API
  3. *
  4. * Copyright 2008-2011 Freescale Semiconductor, Inc.
  5. *
  6. * Based on talitos crypto API driver.
  7. *
  8. * relationship of job descriptors to shared descriptors (SteveC Dec 10 2008):
  9. *
  10. * --------------- ---------------
  11. * | JobDesc #1 |-------------------->| ShareDesc |
  12. * | *(packet 1) | | (PDB) |
  13. * --------------- |------------->| (hashKey) |
  14. * . | | (cipherKey) |
  15. * . | |-------->| (operation) |
  16. * --------------- | | ---------------
  17. * | JobDesc #2 |------| |
  18. * | *(packet 2) | |
  19. * --------------- |
  20. * . |
  21. * . |
  22. * --------------- |
  23. * | JobDesc #3 |------------
  24. * | *(packet 3) |
  25. * ---------------
  26. *
  27. * The SharedDesc never changes for a connection unless rekeyed, but
  28. * each packet will likely be in a different place. So all we need
  29. * to know to process the packet is where the input is, where the
  30. * output goes, and what context we want to process with. Context is
  31. * in the SharedDesc, packet references in the JobDesc.
  32. *
  33. * So, a job desc looks like:
  34. *
  35. * ---------------------
  36. * | Header |
  37. * | ShareDesc Pointer |
  38. * | SEQ_OUT_PTR |
  39. * | (output buffer) |
  40. * | (output length) |
  41. * | SEQ_IN_PTR |
  42. * | (input buffer) |
  43. * | (input length) |
  44. * ---------------------
  45. */
  46. #include "compat.h"
  47. #include "regs.h"
  48. #include "intern.h"
  49. #include "desc_constr.h"
  50. #include "jr.h"
  51. #include "error.h"
  52. #include "sg_sw_sec4.h"
  53. #include "key_gen.h"
  54. /*
  55. * crypto alg
  56. */
  57. #define CAAM_CRA_PRIORITY 3000
  58. /* max key is sum of AES_MAX_KEY_SIZE, max split key size */
  59. #define CAAM_MAX_KEY_SIZE (AES_MAX_KEY_SIZE + \
  60. SHA512_DIGEST_SIZE * 2)
  61. /* max IV is max of AES_BLOCK_SIZE, DES3_EDE_BLOCK_SIZE */
  62. #define CAAM_MAX_IV_LENGTH 16
  63. /* length of descriptors text */
  64. #define DESC_AEAD_BASE (4 * CAAM_CMD_SZ)
  65. #define DESC_AEAD_ENC_LEN (DESC_AEAD_BASE + 16 * CAAM_CMD_SZ)
  66. #define DESC_AEAD_DEC_LEN (DESC_AEAD_BASE + 21 * CAAM_CMD_SZ)
  67. #define DESC_AEAD_GIVENC_LEN (DESC_AEAD_ENC_LEN + 7 * CAAM_CMD_SZ)
  68. #define DESC_ABLKCIPHER_BASE (3 * CAAM_CMD_SZ)
  69. #define DESC_ABLKCIPHER_ENC_LEN (DESC_ABLKCIPHER_BASE + \
  70. 20 * CAAM_CMD_SZ)
  71. #define DESC_ABLKCIPHER_DEC_LEN (DESC_ABLKCIPHER_BASE + \
  72. 15 * CAAM_CMD_SZ)
  73. #define DESC_MAX_USED_BYTES (DESC_AEAD_GIVENC_LEN + \
  74. CAAM_MAX_KEY_SIZE)
  75. #define DESC_MAX_USED_LEN (DESC_MAX_USED_BYTES / CAAM_CMD_SZ)
  76. #ifdef DEBUG
  77. /* for print_hex_dumps with line references */
  78. #define debug(format, arg...) printk(format, arg)
  79. #else
  80. #define debug(format, arg...)
  81. #endif
  82. /* Set DK bit in class 1 operation if shared */
  83. static inline void append_dec_op1(u32 *desc, u32 type)
  84. {
  85. u32 *jump_cmd, *uncond_jump_cmd;
  86. jump_cmd = append_jump(desc, JUMP_TEST_ALL | JUMP_COND_SHRD);
  87. append_operation(desc, type | OP_ALG_AS_INITFINAL |
  88. OP_ALG_DECRYPT);
  89. uncond_jump_cmd = append_jump(desc, JUMP_TEST_ALL);
  90. set_jump_tgt_here(desc, jump_cmd);
  91. append_operation(desc, type | OP_ALG_AS_INITFINAL |
  92. OP_ALG_DECRYPT | OP_ALG_AAI_DK);
  93. set_jump_tgt_here(desc, uncond_jump_cmd);
  94. }
  95. /*
  96. * Wait for completion of class 1 key loading before allowing
  97. * error propagation
  98. */
  99. static inline void append_dec_shr_done(u32 *desc)
  100. {
  101. u32 *jump_cmd;
  102. jump_cmd = append_jump(desc, JUMP_CLASS_CLASS1 | JUMP_TEST_ALL);
  103. set_jump_tgt_here(desc, jump_cmd);
  104. append_cmd(desc, SET_OK_NO_PROP_ERRORS | CMD_LOAD);
  105. }
  106. /*
  107. * For aead functions, read payload and write payload,
  108. * both of which are specified in req->src and req->dst
  109. */
  110. static inline void aead_append_src_dst(u32 *desc, u32 msg_type)
  111. {
  112. append_seq_fifo_load(desc, 0, FIFOLD_CLASS_BOTH |
  113. KEY_VLF | msg_type | FIFOLD_TYPE_LASTBOTH);
  114. append_seq_fifo_store(desc, 0, FIFOST_TYPE_MESSAGE_DATA | KEY_VLF);
  115. }
  116. /*
  117. * For aead encrypt and decrypt, read iv for both classes
  118. */
  119. static inline void aead_append_ld_iv(u32 *desc, int ivsize)
  120. {
  121. append_cmd(desc, CMD_SEQ_LOAD | LDST_SRCDST_BYTE_CONTEXT |
  122. LDST_CLASS_1_CCB | ivsize);
  123. append_move(desc, MOVE_SRC_CLASS1CTX | MOVE_DEST_CLASS2INFIFO | ivsize);
  124. }
  125. /*
  126. * For ablkcipher encrypt and decrypt, read from req->src and
  127. * write to req->dst
  128. */
  129. static inline void ablkcipher_append_src_dst(u32 *desc)
  130. {
  131. append_math_add(desc, VARSEQOUTLEN, SEQINLEN, REG0, CAAM_CMD_SZ);
  132. append_math_add(desc, VARSEQINLEN, SEQINLEN, REG0, CAAM_CMD_SZ);
  133. append_seq_fifo_load(desc, 0, FIFOLD_CLASS_CLASS1 |
  134. KEY_VLF | FIFOLD_TYPE_MSG | FIFOLD_TYPE_LAST1);
  135. append_seq_fifo_store(desc, 0, FIFOST_TYPE_MESSAGE_DATA | KEY_VLF);
  136. }
  137. /*
  138. * If all data, including src (with assoc and iv) or dst (with iv only) are
  139. * contiguous
  140. */
  141. #define GIV_SRC_CONTIG 1
  142. #define GIV_DST_CONTIG (1 << 1)
  143. /*
  144. * per-session context
  145. */
  146. struct caam_ctx {
  147. struct device *jrdev;
  148. u32 sh_desc_enc[DESC_MAX_USED_LEN];
  149. u32 sh_desc_dec[DESC_MAX_USED_LEN];
  150. u32 sh_desc_givenc[DESC_MAX_USED_LEN];
  151. dma_addr_t sh_desc_enc_dma;
  152. dma_addr_t sh_desc_dec_dma;
  153. dma_addr_t sh_desc_givenc_dma;
  154. u32 class1_alg_type;
  155. u32 class2_alg_type;
  156. u32 alg_op;
  157. u8 key[CAAM_MAX_KEY_SIZE];
  158. dma_addr_t key_dma;
  159. unsigned int enckeylen;
  160. unsigned int split_key_len;
  161. unsigned int split_key_pad_len;
  162. unsigned int authsize;
  163. };
  164. static void append_key_aead(u32 *desc, struct caam_ctx *ctx,
  165. int keys_fit_inline)
  166. {
  167. if (keys_fit_inline) {
  168. append_key_as_imm(desc, ctx->key, ctx->split_key_pad_len,
  169. ctx->split_key_len, CLASS_2 |
  170. KEY_DEST_MDHA_SPLIT | KEY_ENC);
  171. append_key_as_imm(desc, (void *)ctx->key +
  172. ctx->split_key_pad_len, ctx->enckeylen,
  173. ctx->enckeylen, CLASS_1 | KEY_DEST_CLASS_REG);
  174. } else {
  175. append_key(desc, ctx->key_dma, ctx->split_key_len, CLASS_2 |
  176. KEY_DEST_MDHA_SPLIT | KEY_ENC);
  177. append_key(desc, ctx->key_dma + ctx->split_key_pad_len,
  178. ctx->enckeylen, CLASS_1 | KEY_DEST_CLASS_REG);
  179. }
  180. }
  181. static void init_sh_desc_key_aead(u32 *desc, struct caam_ctx *ctx,
  182. int keys_fit_inline)
  183. {
  184. u32 *key_jump_cmd;
  185. init_sh_desc(desc, HDR_SHARE_SERIAL);
  186. /* Skip if already shared */
  187. key_jump_cmd = append_jump(desc, JUMP_JSL | JUMP_TEST_ALL |
  188. JUMP_COND_SHRD);
  189. append_key_aead(desc, ctx, keys_fit_inline);
  190. set_jump_tgt_here(desc, key_jump_cmd);
  191. /* Propagate errors from shared to job descriptor */
  192. append_cmd(desc, SET_OK_NO_PROP_ERRORS | CMD_LOAD);
  193. }
  194. static int aead_set_sh_desc(struct crypto_aead *aead)
  195. {
  196. struct aead_tfm *tfm = &aead->base.crt_aead;
  197. struct caam_ctx *ctx = crypto_aead_ctx(aead);
  198. struct device *jrdev = ctx->jrdev;
  199. bool keys_fit_inline = false;
  200. u32 *key_jump_cmd, *jump_cmd;
  201. u32 geniv, moveiv;
  202. u32 *desc;
  203. if (!ctx->enckeylen || !ctx->authsize)
  204. return 0;
  205. /*
  206. * Job Descriptor and Shared Descriptors
  207. * must all fit into the 64-word Descriptor h/w Buffer
  208. */
  209. if (DESC_AEAD_ENC_LEN + DESC_JOB_IO_LEN +
  210. ctx->split_key_pad_len + ctx->enckeylen <=
  211. CAAM_DESC_BYTES_MAX)
  212. keys_fit_inline = true;
  213. /* aead_encrypt shared descriptor */
  214. desc = ctx->sh_desc_enc;
  215. init_sh_desc_key_aead(desc, ctx, keys_fit_inline);
  216. /* Class 2 operation */
  217. append_operation(desc, ctx->class2_alg_type |
  218. OP_ALG_AS_INITFINAL | OP_ALG_ENCRYPT);
  219. /* cryptlen = seqoutlen - authsize */
  220. append_math_sub_imm_u32(desc, REG3, SEQOUTLEN, IMM, ctx->authsize);
  221. /* assoclen + cryptlen = seqinlen - ivsize */
  222. append_math_sub_imm_u32(desc, REG2, SEQINLEN, IMM, tfm->ivsize);
  223. /* assoclen + cryptlen = (assoclen + cryptlen) - cryptlen */
  224. append_math_sub(desc, VARSEQINLEN, REG2, REG3, CAAM_CMD_SZ);
  225. /* read assoc before reading payload */
  226. append_seq_fifo_load(desc, 0, FIFOLD_CLASS_CLASS2 | FIFOLD_TYPE_MSG |
  227. KEY_VLF);
  228. aead_append_ld_iv(desc, tfm->ivsize);
  229. /* Class 1 operation */
  230. append_operation(desc, ctx->class1_alg_type |
  231. OP_ALG_AS_INITFINAL | OP_ALG_ENCRYPT);
  232. /* Read and write cryptlen bytes */
  233. append_math_add(desc, VARSEQINLEN, ZERO, REG3, CAAM_CMD_SZ);
  234. append_math_add(desc, VARSEQOUTLEN, ZERO, REG3, CAAM_CMD_SZ);
  235. aead_append_src_dst(desc, FIFOLD_TYPE_MSG1OUT2);
  236. /* Write ICV */
  237. append_seq_store(desc, ctx->authsize, LDST_CLASS_2_CCB |
  238. LDST_SRCDST_BYTE_CONTEXT);
  239. ctx->sh_desc_enc_dma = dma_map_single(jrdev, desc,
  240. desc_bytes(desc),
  241. DMA_TO_DEVICE);
  242. if (dma_mapping_error(jrdev, ctx->sh_desc_enc_dma)) {
  243. dev_err(jrdev, "unable to map shared descriptor\n");
  244. return -ENOMEM;
  245. }
  246. #ifdef DEBUG
  247. print_hex_dump(KERN_ERR, "aead enc shdesc@"__stringify(__LINE__)": ",
  248. DUMP_PREFIX_ADDRESS, 16, 4, desc,
  249. desc_bytes(desc), 1);
  250. #endif
  251. /*
  252. * Job Descriptor and Shared Descriptors
  253. * must all fit into the 64-word Descriptor h/w Buffer
  254. */
  255. if (DESC_AEAD_DEC_LEN + DESC_JOB_IO_LEN +
  256. ctx->split_key_pad_len + ctx->enckeylen <=
  257. CAAM_DESC_BYTES_MAX)
  258. keys_fit_inline = true;
  259. desc = ctx->sh_desc_dec;
  260. /* aead_decrypt shared descriptor */
  261. init_sh_desc(desc, HDR_SHARE_SERIAL);
  262. /* Skip if already shared */
  263. key_jump_cmd = append_jump(desc, JUMP_JSL | JUMP_TEST_ALL |
  264. JUMP_COND_SHRD);
  265. append_key_aead(desc, ctx, keys_fit_inline);
  266. /* Only propagate error immediately if shared */
  267. jump_cmd = append_jump(desc, JUMP_TEST_ALL);
  268. set_jump_tgt_here(desc, key_jump_cmd);
  269. append_cmd(desc, SET_OK_NO_PROP_ERRORS | CMD_LOAD);
  270. set_jump_tgt_here(desc, jump_cmd);
  271. /* Class 2 operation */
  272. append_operation(desc, ctx->class2_alg_type |
  273. OP_ALG_AS_INITFINAL | OP_ALG_DECRYPT | OP_ALG_ICV_ON);
  274. /* assoclen + cryptlen = seqinlen - ivsize */
  275. append_math_sub_imm_u32(desc, REG3, SEQINLEN, IMM,
  276. ctx->authsize + tfm->ivsize)
  277. /* assoclen = (assoclen + cryptlen) - cryptlen */
  278. append_math_sub(desc, REG2, SEQOUTLEN, REG0, CAAM_CMD_SZ);
  279. append_math_sub(desc, VARSEQINLEN, REG3, REG2, CAAM_CMD_SZ);
  280. /* read assoc before reading payload */
  281. append_seq_fifo_load(desc, 0, FIFOLD_CLASS_CLASS2 | FIFOLD_TYPE_MSG |
  282. KEY_VLF);
  283. aead_append_ld_iv(desc, tfm->ivsize);
  284. append_dec_op1(desc, ctx->class1_alg_type);
  285. /* Read and write cryptlen bytes */
  286. append_math_add(desc, VARSEQINLEN, ZERO, REG2, CAAM_CMD_SZ);
  287. append_math_add(desc, VARSEQOUTLEN, ZERO, REG2, CAAM_CMD_SZ);
  288. aead_append_src_dst(desc, FIFOLD_TYPE_MSG);
  289. /* Load ICV */
  290. append_seq_fifo_load(desc, ctx->authsize, FIFOLD_CLASS_CLASS2 |
  291. FIFOLD_TYPE_LAST2 | FIFOLD_TYPE_ICV);
  292. append_dec_shr_done(desc);
  293. ctx->sh_desc_dec_dma = dma_map_single(jrdev, desc,
  294. desc_bytes(desc),
  295. DMA_TO_DEVICE);
  296. if (dma_mapping_error(jrdev, ctx->sh_desc_dec_dma)) {
  297. dev_err(jrdev, "unable to map shared descriptor\n");
  298. return -ENOMEM;
  299. }
  300. #ifdef DEBUG
  301. print_hex_dump(KERN_ERR, "aead dec shdesc@"__stringify(__LINE__)": ",
  302. DUMP_PREFIX_ADDRESS, 16, 4, desc,
  303. desc_bytes(desc), 1);
  304. #endif
  305. /*
  306. * Job Descriptor and Shared Descriptors
  307. * must all fit into the 64-word Descriptor h/w Buffer
  308. */
  309. if (DESC_AEAD_GIVENC_LEN + DESC_JOB_IO_LEN +
  310. ctx->split_key_pad_len + ctx->enckeylen <=
  311. CAAM_DESC_BYTES_MAX)
  312. keys_fit_inline = true;
  313. /* aead_givencrypt shared descriptor */
  314. desc = ctx->sh_desc_givenc;
  315. init_sh_desc_key_aead(desc, ctx, keys_fit_inline);
  316. /* Generate IV */
  317. geniv = NFIFOENTRY_STYPE_PAD | NFIFOENTRY_DEST_DECO |
  318. NFIFOENTRY_DTYPE_MSG | NFIFOENTRY_LC1 |
  319. NFIFOENTRY_PTYPE_RND | (tfm->ivsize << NFIFOENTRY_DLEN_SHIFT);
  320. append_load_imm_u32(desc, geniv, LDST_CLASS_IND_CCB |
  321. LDST_SRCDST_WORD_INFO_FIFO | LDST_IMM);
  322. append_cmd(desc, CMD_LOAD | DISABLE_AUTO_INFO_FIFO);
  323. append_move(desc, MOVE_SRC_INFIFO |
  324. MOVE_DEST_CLASS1CTX | (tfm->ivsize << MOVE_LEN_SHIFT));
  325. append_cmd(desc, CMD_LOAD | ENABLE_AUTO_INFO_FIFO);
  326. /* Copy IV to class 1 context */
  327. append_move(desc, MOVE_SRC_CLASS1CTX |
  328. MOVE_DEST_OUTFIFO | (tfm->ivsize << MOVE_LEN_SHIFT));
  329. /* Return to encryption */
  330. append_operation(desc, ctx->class2_alg_type |
  331. OP_ALG_AS_INITFINAL | OP_ALG_ENCRYPT);
  332. /* ivsize + cryptlen = seqoutlen - authsize */
  333. append_math_sub_imm_u32(desc, REG3, SEQOUTLEN, IMM, ctx->authsize);
  334. /* assoclen = seqinlen - (ivsize + cryptlen) */
  335. append_math_sub(desc, VARSEQINLEN, SEQINLEN, REG3, CAAM_CMD_SZ);
  336. /* read assoc before reading payload */
  337. append_seq_fifo_load(desc, 0, FIFOLD_CLASS_CLASS2 | FIFOLD_TYPE_MSG |
  338. KEY_VLF);
  339. /* Copy iv from class 1 ctx to class 2 fifo*/
  340. moveiv = NFIFOENTRY_STYPE_OFIFO | NFIFOENTRY_DEST_CLASS2 |
  341. NFIFOENTRY_DTYPE_MSG | (tfm->ivsize << NFIFOENTRY_DLEN_SHIFT);
  342. append_load_imm_u32(desc, moveiv, LDST_CLASS_IND_CCB |
  343. LDST_SRCDST_WORD_INFO_FIFO | LDST_IMM);
  344. append_load_imm_u32(desc, tfm->ivsize, LDST_CLASS_2_CCB |
  345. LDST_SRCDST_WORD_DATASZ_REG | LDST_IMM);
  346. /* Class 1 operation */
  347. append_operation(desc, ctx->class1_alg_type |
  348. OP_ALG_AS_INITFINAL | OP_ALG_ENCRYPT);
  349. /* Will write ivsize + cryptlen */
  350. append_math_add(desc, VARSEQOUTLEN, SEQINLEN, REG0, CAAM_CMD_SZ);
  351. /* Not need to reload iv */
  352. append_seq_fifo_load(desc, tfm->ivsize,
  353. FIFOLD_CLASS_SKIP);
  354. /* Will read cryptlen */
  355. append_math_add(desc, VARSEQINLEN, SEQINLEN, REG0, CAAM_CMD_SZ);
  356. aead_append_src_dst(desc, FIFOLD_TYPE_MSG1OUT2);
  357. /* Write ICV */
  358. append_seq_store(desc, ctx->authsize, LDST_CLASS_2_CCB |
  359. LDST_SRCDST_BYTE_CONTEXT);
  360. ctx->sh_desc_givenc_dma = dma_map_single(jrdev, desc,
  361. desc_bytes(desc),
  362. DMA_TO_DEVICE);
  363. if (dma_mapping_error(jrdev, ctx->sh_desc_givenc_dma)) {
  364. dev_err(jrdev, "unable to map shared descriptor\n");
  365. return -ENOMEM;
  366. }
  367. #ifdef DEBUG
  368. print_hex_dump(KERN_ERR, "aead givenc shdesc@"__stringify(__LINE__)": ",
  369. DUMP_PREFIX_ADDRESS, 16, 4, desc,
  370. desc_bytes(desc), 1);
  371. #endif
  372. return 0;
  373. }
  374. static int aead_setauthsize(struct crypto_aead *authenc,
  375. unsigned int authsize)
  376. {
  377. struct caam_ctx *ctx = crypto_aead_ctx(authenc);
  378. ctx->authsize = authsize;
  379. aead_set_sh_desc(authenc);
  380. return 0;
  381. }
  382. static u32 gen_split_aead_key(struct caam_ctx *ctx, const u8 *key_in,
  383. u32 authkeylen)
  384. {
  385. return gen_split_key(ctx->jrdev, ctx->key, ctx->split_key_len,
  386. ctx->split_key_pad_len, key_in, authkeylen,
  387. ctx->alg_op);
  388. }
  389. static int aead_setkey(struct crypto_aead *aead,
  390. const u8 *key, unsigned int keylen)
  391. {
  392. /* Sizes for MDHA pads (*not* keys): MD5, SHA1, 224, 256, 384, 512 */
  393. static const u8 mdpadlen[] = { 16, 20, 32, 32, 64, 64 };
  394. struct caam_ctx *ctx = crypto_aead_ctx(aead);
  395. struct device *jrdev = ctx->jrdev;
  396. struct rtattr *rta = (void *)key;
  397. struct crypto_authenc_key_param *param;
  398. unsigned int authkeylen;
  399. unsigned int enckeylen;
  400. int ret = 0;
  401. param = RTA_DATA(rta);
  402. enckeylen = be32_to_cpu(param->enckeylen);
  403. key += RTA_ALIGN(rta->rta_len);
  404. keylen -= RTA_ALIGN(rta->rta_len);
  405. if (keylen < enckeylen)
  406. goto badkey;
  407. authkeylen = keylen - enckeylen;
  408. if (keylen > CAAM_MAX_KEY_SIZE)
  409. goto badkey;
  410. /* Pick class 2 key length from algorithm submask */
  411. ctx->split_key_len = mdpadlen[(ctx->alg_op & OP_ALG_ALGSEL_SUBMASK) >>
  412. OP_ALG_ALGSEL_SHIFT] * 2;
  413. ctx->split_key_pad_len = ALIGN(ctx->split_key_len, 16);
  414. #ifdef DEBUG
  415. printk(KERN_ERR "keylen %d enckeylen %d authkeylen %d\n",
  416. keylen, enckeylen, authkeylen);
  417. printk(KERN_ERR "split_key_len %d split_key_pad_len %d\n",
  418. ctx->split_key_len, ctx->split_key_pad_len);
  419. print_hex_dump(KERN_ERR, "key in @"__stringify(__LINE__)": ",
  420. DUMP_PREFIX_ADDRESS, 16, 4, key, keylen, 1);
  421. #endif
  422. ret = gen_split_aead_key(ctx, key, authkeylen);
  423. if (ret) {
  424. goto badkey;
  425. }
  426. /* postpend encryption key to auth split key */
  427. memcpy(ctx->key + ctx->split_key_pad_len, key + authkeylen, enckeylen);
  428. ctx->key_dma = dma_map_single(jrdev, ctx->key, ctx->split_key_pad_len +
  429. enckeylen, DMA_TO_DEVICE);
  430. if (dma_mapping_error(jrdev, ctx->key_dma)) {
  431. dev_err(jrdev, "unable to map key i/o memory\n");
  432. return -ENOMEM;
  433. }
  434. #ifdef DEBUG
  435. print_hex_dump(KERN_ERR, "ctx.key@"__stringify(__LINE__)": ",
  436. DUMP_PREFIX_ADDRESS, 16, 4, ctx->key,
  437. ctx->split_key_pad_len + enckeylen, 1);
  438. #endif
  439. ctx->enckeylen = enckeylen;
  440. ret = aead_set_sh_desc(aead);
  441. if (ret) {
  442. dma_unmap_single(jrdev, ctx->key_dma, ctx->split_key_pad_len +
  443. enckeylen, DMA_TO_DEVICE);
  444. }
  445. return ret;
  446. badkey:
  447. crypto_aead_set_flags(aead, CRYPTO_TFM_RES_BAD_KEY_LEN);
  448. return -EINVAL;
  449. }
  450. static int ablkcipher_setkey(struct crypto_ablkcipher *ablkcipher,
  451. const u8 *key, unsigned int keylen)
  452. {
  453. struct caam_ctx *ctx = crypto_ablkcipher_ctx(ablkcipher);
  454. struct ablkcipher_tfm *tfm = &ablkcipher->base.crt_ablkcipher;
  455. struct device *jrdev = ctx->jrdev;
  456. int ret = 0;
  457. u32 *key_jump_cmd, *jump_cmd;
  458. u32 *desc;
  459. #ifdef DEBUG
  460. print_hex_dump(KERN_ERR, "key in @"__stringify(__LINE__)": ",
  461. DUMP_PREFIX_ADDRESS, 16, 4, key, keylen, 1);
  462. #endif
  463. memcpy(ctx->key, key, keylen);
  464. ctx->key_dma = dma_map_single(jrdev, ctx->key, keylen,
  465. DMA_TO_DEVICE);
  466. if (dma_mapping_error(jrdev, ctx->key_dma)) {
  467. dev_err(jrdev, "unable to map key i/o memory\n");
  468. return -ENOMEM;
  469. }
  470. ctx->enckeylen = keylen;
  471. /* ablkcipher_encrypt shared descriptor */
  472. desc = ctx->sh_desc_enc;
  473. init_sh_desc(desc, HDR_SHARE_SERIAL);
  474. /* Skip if already shared */
  475. key_jump_cmd = append_jump(desc, JUMP_JSL | JUMP_TEST_ALL |
  476. JUMP_COND_SHRD);
  477. /* Load class1 key only */
  478. append_key_as_imm(desc, (void *)ctx->key, ctx->enckeylen,
  479. ctx->enckeylen, CLASS_1 |
  480. KEY_DEST_CLASS_REG);
  481. set_jump_tgt_here(desc, key_jump_cmd);
  482. /* Propagate errors from shared to job descriptor */
  483. append_cmd(desc, SET_OK_NO_PROP_ERRORS | CMD_LOAD);
  484. /* Load iv */
  485. append_cmd(desc, CMD_SEQ_LOAD | LDST_SRCDST_BYTE_CONTEXT |
  486. LDST_CLASS_1_CCB | tfm->ivsize);
  487. /* Load operation */
  488. append_operation(desc, ctx->class1_alg_type |
  489. OP_ALG_AS_INITFINAL | OP_ALG_ENCRYPT);
  490. /* Perform operation */
  491. ablkcipher_append_src_dst(desc);
  492. ctx->sh_desc_enc_dma = dma_map_single(jrdev, desc,
  493. desc_bytes(desc),
  494. DMA_TO_DEVICE);
  495. if (dma_mapping_error(jrdev, ctx->sh_desc_enc_dma)) {
  496. dev_err(jrdev, "unable to map shared descriptor\n");
  497. return -ENOMEM;
  498. }
  499. #ifdef DEBUG
  500. print_hex_dump(KERN_ERR,
  501. "ablkcipher enc shdesc@"__stringify(__LINE__)": ",
  502. DUMP_PREFIX_ADDRESS, 16, 4, desc,
  503. desc_bytes(desc), 1);
  504. #endif
  505. /* ablkcipher_decrypt shared descriptor */
  506. desc = ctx->sh_desc_dec;
  507. init_sh_desc(desc, HDR_SHARE_SERIAL);
  508. /* Skip if already shared */
  509. key_jump_cmd = append_jump(desc, JUMP_JSL | JUMP_TEST_ALL |
  510. JUMP_COND_SHRD);
  511. /* Load class1 key only */
  512. append_key_as_imm(desc, (void *)ctx->key, ctx->enckeylen,
  513. ctx->enckeylen, CLASS_1 |
  514. KEY_DEST_CLASS_REG);
  515. /* For aead, only propagate error immediately if shared */
  516. jump_cmd = append_jump(desc, JUMP_TEST_ALL);
  517. set_jump_tgt_here(desc, key_jump_cmd);
  518. append_cmd(desc, SET_OK_NO_PROP_ERRORS | CMD_LOAD);
  519. set_jump_tgt_here(desc, jump_cmd);
  520. /* load IV */
  521. append_cmd(desc, CMD_SEQ_LOAD | LDST_SRCDST_BYTE_CONTEXT |
  522. LDST_CLASS_1_CCB | tfm->ivsize);
  523. /* Choose operation */
  524. append_dec_op1(desc, ctx->class1_alg_type);
  525. /* Perform operation */
  526. ablkcipher_append_src_dst(desc);
  527. /* Wait for key to load before allowing propagating error */
  528. append_dec_shr_done(desc);
  529. ctx->sh_desc_dec_dma = dma_map_single(jrdev, desc,
  530. desc_bytes(desc),
  531. DMA_TO_DEVICE);
  532. if (dma_mapping_error(jrdev, ctx->sh_desc_enc_dma)) {
  533. dev_err(jrdev, "unable to map shared descriptor\n");
  534. return -ENOMEM;
  535. }
  536. #ifdef DEBUG
  537. print_hex_dump(KERN_ERR,
  538. "ablkcipher dec shdesc@"__stringify(__LINE__)": ",
  539. DUMP_PREFIX_ADDRESS, 16, 4, desc,
  540. desc_bytes(desc), 1);
  541. #endif
  542. return ret;
  543. }
  544. /*
  545. * aead_edesc - s/w-extended aead descriptor
  546. * @assoc_nents: number of segments in associated data (SPI+Seq) scatterlist
  547. * @assoc_chained: if source is chained
  548. * @src_nents: number of segments in input scatterlist
  549. * @src_chained: if source is chained
  550. * @dst_nents: number of segments in output scatterlist
  551. * @dst_chained: if destination is chained
  552. * @iv_dma: dma address of iv for checking continuity and link table
  553. * @desc: h/w descriptor (variable length; must not exceed MAX_CAAM_DESCSIZE)
  554. * @sec4_sg_bytes: length of dma mapped sec4_sg space
  555. * @sec4_sg_dma: bus physical mapped address of h/w link table
  556. * @hw_desc: the h/w job descriptor followed by any referenced link tables
  557. */
  558. struct aead_edesc {
  559. int assoc_nents;
  560. bool assoc_chained;
  561. int src_nents;
  562. bool src_chained;
  563. int dst_nents;
  564. bool dst_chained;
  565. dma_addr_t iv_dma;
  566. int sec4_sg_bytes;
  567. dma_addr_t sec4_sg_dma;
  568. struct sec4_sg_entry *sec4_sg;
  569. u32 hw_desc[0];
  570. };
  571. /*
  572. * ablkcipher_edesc - s/w-extended ablkcipher descriptor
  573. * @src_nents: number of segments in input scatterlist
  574. * @src_chained: if source is chained
  575. * @dst_nents: number of segments in output scatterlist
  576. * @dst_chained: if destination is chained
  577. * @iv_dma: dma address of iv for checking continuity and link table
  578. * @desc: h/w descriptor (variable length; must not exceed MAX_CAAM_DESCSIZE)
  579. * @sec4_sg_bytes: length of dma mapped sec4_sg space
  580. * @sec4_sg_dma: bus physical mapped address of h/w link table
  581. * @hw_desc: the h/w job descriptor followed by any referenced link tables
  582. */
  583. struct ablkcipher_edesc {
  584. int src_nents;
  585. bool src_chained;
  586. int dst_nents;
  587. bool dst_chained;
  588. dma_addr_t iv_dma;
  589. int sec4_sg_bytes;
  590. dma_addr_t sec4_sg_dma;
  591. struct sec4_sg_entry *sec4_sg;
  592. u32 hw_desc[0];
  593. };
  594. static void caam_unmap(struct device *dev, struct scatterlist *src,
  595. struct scatterlist *dst, int src_nents,
  596. bool src_chained, int dst_nents, bool dst_chained,
  597. dma_addr_t iv_dma, int ivsize, dma_addr_t sec4_sg_dma,
  598. int sec4_sg_bytes)
  599. {
  600. if (dst != src) {
  601. dma_unmap_sg_chained(dev, src, src_nents ? : 1, DMA_TO_DEVICE,
  602. src_chained);
  603. dma_unmap_sg_chained(dev, dst, dst_nents ? : 1, DMA_FROM_DEVICE,
  604. dst_chained);
  605. } else {
  606. dma_unmap_sg_chained(dev, src, src_nents ? : 1,
  607. DMA_BIDIRECTIONAL, src_chained);
  608. }
  609. if (iv_dma)
  610. dma_unmap_single(dev, iv_dma, ivsize, DMA_TO_DEVICE);
  611. if (sec4_sg_bytes)
  612. dma_unmap_single(dev, sec4_sg_dma, sec4_sg_bytes,
  613. DMA_TO_DEVICE);
  614. }
  615. static void aead_unmap(struct device *dev,
  616. struct aead_edesc *edesc,
  617. struct aead_request *req)
  618. {
  619. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  620. int ivsize = crypto_aead_ivsize(aead);
  621. dma_unmap_sg_chained(dev, req->assoc, edesc->assoc_nents,
  622. DMA_TO_DEVICE, edesc->assoc_chained);
  623. caam_unmap(dev, req->src, req->dst,
  624. edesc->src_nents, edesc->src_chained, edesc->dst_nents,
  625. edesc->dst_chained, edesc->iv_dma, ivsize,
  626. edesc->sec4_sg_dma, edesc->sec4_sg_bytes);
  627. }
  628. static void ablkcipher_unmap(struct device *dev,
  629. struct ablkcipher_edesc *edesc,
  630. struct ablkcipher_request *req)
  631. {
  632. struct crypto_ablkcipher *ablkcipher = crypto_ablkcipher_reqtfm(req);
  633. int ivsize = crypto_ablkcipher_ivsize(ablkcipher);
  634. caam_unmap(dev, req->src, req->dst,
  635. edesc->src_nents, edesc->src_chained, edesc->dst_nents,
  636. edesc->dst_chained, edesc->iv_dma, ivsize,
  637. edesc->sec4_sg_dma, edesc->sec4_sg_bytes);
  638. }
  639. static void aead_encrypt_done(struct device *jrdev, u32 *desc, u32 err,
  640. void *context)
  641. {
  642. struct aead_request *req = context;
  643. struct aead_edesc *edesc;
  644. #ifdef DEBUG
  645. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  646. struct caam_ctx *ctx = crypto_aead_ctx(aead);
  647. int ivsize = crypto_aead_ivsize(aead);
  648. dev_err(jrdev, "%s %d: err 0x%x\n", __func__, __LINE__, err);
  649. #endif
  650. edesc = (struct aead_edesc *)((char *)desc -
  651. offsetof(struct aead_edesc, hw_desc));
  652. if (err) {
  653. char tmp[CAAM_ERROR_STR_MAX];
  654. dev_err(jrdev, "%08x: %s\n", err, caam_jr_strstatus(tmp, err));
  655. }
  656. aead_unmap(jrdev, edesc, req);
  657. #ifdef DEBUG
  658. print_hex_dump(KERN_ERR, "assoc @"__stringify(__LINE__)": ",
  659. DUMP_PREFIX_ADDRESS, 16, 4, sg_virt(req->assoc),
  660. req->assoclen , 1);
  661. print_hex_dump(KERN_ERR, "dstiv @"__stringify(__LINE__)": ",
  662. DUMP_PREFIX_ADDRESS, 16, 4, sg_virt(req->src) - ivsize,
  663. edesc->src_nents ? 100 : ivsize, 1);
  664. print_hex_dump(KERN_ERR, "dst @"__stringify(__LINE__)": ",
  665. DUMP_PREFIX_ADDRESS, 16, 4, sg_virt(req->src),
  666. edesc->src_nents ? 100 : req->cryptlen +
  667. ctx->authsize + 4, 1);
  668. #endif
  669. kfree(edesc);
  670. aead_request_complete(req, err);
  671. }
  672. static void aead_decrypt_done(struct device *jrdev, u32 *desc, u32 err,
  673. void *context)
  674. {
  675. struct aead_request *req = context;
  676. struct aead_edesc *edesc;
  677. #ifdef DEBUG
  678. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  679. struct caam_ctx *ctx = crypto_aead_ctx(aead);
  680. int ivsize = crypto_aead_ivsize(aead);
  681. dev_err(jrdev, "%s %d: err 0x%x\n", __func__, __LINE__, err);
  682. #endif
  683. edesc = (struct aead_edesc *)((char *)desc -
  684. offsetof(struct aead_edesc, hw_desc));
  685. #ifdef DEBUG
  686. print_hex_dump(KERN_ERR, "dstiv @"__stringify(__LINE__)": ",
  687. DUMP_PREFIX_ADDRESS, 16, 4, req->iv,
  688. ivsize, 1);
  689. print_hex_dump(KERN_ERR, "dst @"__stringify(__LINE__)": ",
  690. DUMP_PREFIX_ADDRESS, 16, 4, sg_virt(req->dst),
  691. req->cryptlen, 1);
  692. #endif
  693. if (err) {
  694. char tmp[CAAM_ERROR_STR_MAX];
  695. dev_err(jrdev, "%08x: %s\n", err, caam_jr_strstatus(tmp, err));
  696. }
  697. aead_unmap(jrdev, edesc, req);
  698. /*
  699. * verify hw auth check passed else return -EBADMSG
  700. */
  701. if ((err & JRSTA_CCBERR_ERRID_MASK) == JRSTA_CCBERR_ERRID_ICVCHK)
  702. err = -EBADMSG;
  703. #ifdef DEBUG
  704. print_hex_dump(KERN_ERR, "iphdrout@"__stringify(__LINE__)": ",
  705. DUMP_PREFIX_ADDRESS, 16, 4,
  706. ((char *)sg_virt(req->assoc) - sizeof(struct iphdr)),
  707. sizeof(struct iphdr) + req->assoclen +
  708. ((req->cryptlen > 1500) ? 1500 : req->cryptlen) +
  709. ctx->authsize + 36, 1);
  710. if (!err && edesc->sec4_sg_bytes) {
  711. struct scatterlist *sg = sg_last(req->src, edesc->src_nents);
  712. print_hex_dump(KERN_ERR, "sglastout@"__stringify(__LINE__)": ",
  713. DUMP_PREFIX_ADDRESS, 16, 4, sg_virt(sg),
  714. sg->length + ctx->authsize + 16, 1);
  715. }
  716. #endif
  717. kfree(edesc);
  718. aead_request_complete(req, err);
  719. }
  720. static void ablkcipher_encrypt_done(struct device *jrdev, u32 *desc, u32 err,
  721. void *context)
  722. {
  723. struct ablkcipher_request *req = context;
  724. struct ablkcipher_edesc *edesc;
  725. #ifdef DEBUG
  726. struct crypto_ablkcipher *ablkcipher = crypto_ablkcipher_reqtfm(req);
  727. int ivsize = crypto_ablkcipher_ivsize(ablkcipher);
  728. dev_err(jrdev, "%s %d: err 0x%x\n", __func__, __LINE__, err);
  729. #endif
  730. edesc = (struct ablkcipher_edesc *)((char *)desc -
  731. offsetof(struct ablkcipher_edesc, hw_desc));
  732. if (err) {
  733. char tmp[CAAM_ERROR_STR_MAX];
  734. dev_err(jrdev, "%08x: %s\n", err, caam_jr_strstatus(tmp, err));
  735. }
  736. #ifdef DEBUG
  737. print_hex_dump(KERN_ERR, "dstiv @"__stringify(__LINE__)": ",
  738. DUMP_PREFIX_ADDRESS, 16, 4, req->info,
  739. edesc->src_nents > 1 ? 100 : ivsize, 1);
  740. print_hex_dump(KERN_ERR, "dst @"__stringify(__LINE__)": ",
  741. DUMP_PREFIX_ADDRESS, 16, 4, sg_virt(req->src),
  742. edesc->dst_nents > 1 ? 100 : req->nbytes, 1);
  743. #endif
  744. ablkcipher_unmap(jrdev, edesc, req);
  745. kfree(edesc);
  746. ablkcipher_request_complete(req, err);
  747. }
  748. static void ablkcipher_decrypt_done(struct device *jrdev, u32 *desc, u32 err,
  749. void *context)
  750. {
  751. struct ablkcipher_request *req = context;
  752. struct ablkcipher_edesc *edesc;
  753. #ifdef DEBUG
  754. struct crypto_ablkcipher *ablkcipher = crypto_ablkcipher_reqtfm(req);
  755. int ivsize = crypto_ablkcipher_ivsize(ablkcipher);
  756. dev_err(jrdev, "%s %d: err 0x%x\n", __func__, __LINE__, err);
  757. #endif
  758. edesc = (struct ablkcipher_edesc *)((char *)desc -
  759. offsetof(struct ablkcipher_edesc, hw_desc));
  760. if (err) {
  761. char tmp[CAAM_ERROR_STR_MAX];
  762. dev_err(jrdev, "%08x: %s\n", err, caam_jr_strstatus(tmp, err));
  763. }
  764. #ifdef DEBUG
  765. print_hex_dump(KERN_ERR, "dstiv @"__stringify(__LINE__)": ",
  766. DUMP_PREFIX_ADDRESS, 16, 4, req->info,
  767. ivsize, 1);
  768. print_hex_dump(KERN_ERR, "dst @"__stringify(__LINE__)": ",
  769. DUMP_PREFIX_ADDRESS, 16, 4, sg_virt(req->src),
  770. edesc->dst_nents > 1 ? 100 : req->nbytes, 1);
  771. #endif
  772. ablkcipher_unmap(jrdev, edesc, req);
  773. kfree(edesc);
  774. ablkcipher_request_complete(req, err);
  775. }
  776. /*
  777. * Fill in aead job descriptor
  778. */
  779. static void init_aead_job(u32 *sh_desc, dma_addr_t ptr,
  780. struct aead_edesc *edesc,
  781. struct aead_request *req,
  782. bool all_contig, bool encrypt)
  783. {
  784. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  785. struct caam_ctx *ctx = crypto_aead_ctx(aead);
  786. int ivsize = crypto_aead_ivsize(aead);
  787. int authsize = ctx->authsize;
  788. u32 *desc = edesc->hw_desc;
  789. u32 out_options = 0, in_options;
  790. dma_addr_t dst_dma, src_dma;
  791. int len, sec4_sg_index = 0;
  792. #ifdef DEBUG
  793. debug("assoclen %d cryptlen %d authsize %d\n",
  794. req->assoclen, req->cryptlen, authsize);
  795. print_hex_dump(KERN_ERR, "assoc @"__stringify(__LINE__)": ",
  796. DUMP_PREFIX_ADDRESS, 16, 4, sg_virt(req->assoc),
  797. req->assoclen , 1);
  798. print_hex_dump(KERN_ERR, "presciv@"__stringify(__LINE__)": ",
  799. DUMP_PREFIX_ADDRESS, 16, 4, req->iv,
  800. edesc->src_nents ? 100 : ivsize, 1);
  801. print_hex_dump(KERN_ERR, "src @"__stringify(__LINE__)": ",
  802. DUMP_PREFIX_ADDRESS, 16, 4, sg_virt(req->src),
  803. edesc->src_nents ? 100 : req->cryptlen, 1);
  804. print_hex_dump(KERN_ERR, "shrdesc@"__stringify(__LINE__)": ",
  805. DUMP_PREFIX_ADDRESS, 16, 4, sh_desc,
  806. desc_bytes(sh_desc), 1);
  807. #endif
  808. len = desc_len(sh_desc);
  809. init_job_desc_shared(desc, ptr, len, HDR_SHARE_DEFER | HDR_REVERSE);
  810. if (all_contig) {
  811. src_dma = sg_dma_address(req->assoc);
  812. in_options = 0;
  813. } else {
  814. src_dma = edesc->sec4_sg_dma;
  815. sec4_sg_index += (edesc->assoc_nents ? : 1) + 1 +
  816. (edesc->src_nents ? : 1);
  817. in_options = LDST_SGF;
  818. }
  819. if (encrypt)
  820. append_seq_in_ptr(desc, src_dma, req->assoclen + ivsize +
  821. req->cryptlen - authsize, in_options);
  822. else
  823. append_seq_in_ptr(desc, src_dma, req->assoclen + ivsize +
  824. req->cryptlen, in_options);
  825. if (likely(req->src == req->dst)) {
  826. if (all_contig) {
  827. dst_dma = sg_dma_address(req->src);
  828. } else {
  829. dst_dma = src_dma + sizeof(struct sec4_sg_entry) *
  830. ((edesc->assoc_nents ? : 1) + 1);
  831. out_options = LDST_SGF;
  832. }
  833. } else {
  834. if (!edesc->dst_nents) {
  835. dst_dma = sg_dma_address(req->dst);
  836. } else {
  837. dst_dma = edesc->sec4_sg_dma +
  838. sec4_sg_index *
  839. sizeof(struct sec4_sg_entry);
  840. out_options = LDST_SGF;
  841. }
  842. }
  843. if (encrypt)
  844. append_seq_out_ptr(desc, dst_dma, req->cryptlen, out_options);
  845. else
  846. append_seq_out_ptr(desc, dst_dma, req->cryptlen - authsize,
  847. out_options);
  848. }
  849. /*
  850. * Fill in aead givencrypt job descriptor
  851. */
  852. static void init_aead_giv_job(u32 *sh_desc, dma_addr_t ptr,
  853. struct aead_edesc *edesc,
  854. struct aead_request *req,
  855. int contig)
  856. {
  857. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  858. struct caam_ctx *ctx = crypto_aead_ctx(aead);
  859. int ivsize = crypto_aead_ivsize(aead);
  860. int authsize = ctx->authsize;
  861. u32 *desc = edesc->hw_desc;
  862. u32 out_options = 0, in_options;
  863. dma_addr_t dst_dma, src_dma;
  864. int len, sec4_sg_index = 0;
  865. #ifdef DEBUG
  866. debug("assoclen %d cryptlen %d authsize %d\n",
  867. req->assoclen, req->cryptlen, authsize);
  868. print_hex_dump(KERN_ERR, "assoc @"__stringify(__LINE__)": ",
  869. DUMP_PREFIX_ADDRESS, 16, 4, sg_virt(req->assoc),
  870. req->assoclen , 1);
  871. print_hex_dump(KERN_ERR, "presciv@"__stringify(__LINE__)": ",
  872. DUMP_PREFIX_ADDRESS, 16, 4, req->iv, ivsize, 1);
  873. print_hex_dump(KERN_ERR, "src @"__stringify(__LINE__)": ",
  874. DUMP_PREFIX_ADDRESS, 16, 4, sg_virt(req->src),
  875. edesc->src_nents > 1 ? 100 : req->cryptlen, 1);
  876. print_hex_dump(KERN_ERR, "shrdesc@"__stringify(__LINE__)": ",
  877. DUMP_PREFIX_ADDRESS, 16, 4, sh_desc,
  878. desc_bytes(sh_desc), 1);
  879. #endif
  880. len = desc_len(sh_desc);
  881. init_job_desc_shared(desc, ptr, len, HDR_SHARE_DEFER | HDR_REVERSE);
  882. if (contig & GIV_SRC_CONTIG) {
  883. src_dma = sg_dma_address(req->assoc);
  884. in_options = 0;
  885. } else {
  886. src_dma = edesc->sec4_sg_dma;
  887. sec4_sg_index += edesc->assoc_nents + 1 + edesc->src_nents;
  888. in_options = LDST_SGF;
  889. }
  890. append_seq_in_ptr(desc, src_dma, req->assoclen + ivsize +
  891. req->cryptlen - authsize, in_options);
  892. if (contig & GIV_DST_CONTIG) {
  893. dst_dma = edesc->iv_dma;
  894. } else {
  895. if (likely(req->src == req->dst)) {
  896. dst_dma = src_dma + sizeof(struct sec4_sg_entry) *
  897. edesc->assoc_nents;
  898. out_options = LDST_SGF;
  899. } else {
  900. dst_dma = edesc->sec4_sg_dma +
  901. sec4_sg_index *
  902. sizeof(struct sec4_sg_entry);
  903. out_options = LDST_SGF;
  904. }
  905. }
  906. append_seq_out_ptr(desc, dst_dma, ivsize + req->cryptlen, out_options);
  907. }
  908. /*
  909. * Fill in ablkcipher job descriptor
  910. */
  911. static void init_ablkcipher_job(u32 *sh_desc, dma_addr_t ptr,
  912. struct ablkcipher_edesc *edesc,
  913. struct ablkcipher_request *req,
  914. bool iv_contig)
  915. {
  916. struct crypto_ablkcipher *ablkcipher = crypto_ablkcipher_reqtfm(req);
  917. int ivsize = crypto_ablkcipher_ivsize(ablkcipher);
  918. u32 *desc = edesc->hw_desc;
  919. u32 out_options = 0, in_options;
  920. dma_addr_t dst_dma, src_dma;
  921. int len, sec4_sg_index = 0;
  922. #ifdef DEBUG
  923. print_hex_dump(KERN_ERR, "presciv@"__stringify(__LINE__)": ",
  924. DUMP_PREFIX_ADDRESS, 16, 4, req->info,
  925. ivsize, 1);
  926. print_hex_dump(KERN_ERR, "src @"__stringify(__LINE__)": ",
  927. DUMP_PREFIX_ADDRESS, 16, 4, sg_virt(req->src),
  928. edesc->src_nents ? 100 : req->nbytes, 1);
  929. #endif
  930. len = desc_len(sh_desc);
  931. init_job_desc_shared(desc, ptr, len, HDR_SHARE_DEFER | HDR_REVERSE);
  932. if (iv_contig) {
  933. src_dma = edesc->iv_dma;
  934. in_options = 0;
  935. } else {
  936. src_dma = edesc->sec4_sg_dma;
  937. sec4_sg_index += (iv_contig ? 0 : 1) + edesc->src_nents;
  938. in_options = LDST_SGF;
  939. }
  940. append_seq_in_ptr(desc, src_dma, req->nbytes + ivsize, in_options);
  941. if (likely(req->src == req->dst)) {
  942. if (!edesc->src_nents && iv_contig) {
  943. dst_dma = sg_dma_address(req->src);
  944. } else {
  945. dst_dma = edesc->sec4_sg_dma +
  946. sizeof(struct sec4_sg_entry);
  947. out_options = LDST_SGF;
  948. }
  949. } else {
  950. if (!edesc->dst_nents) {
  951. dst_dma = sg_dma_address(req->dst);
  952. } else {
  953. dst_dma = edesc->sec4_sg_dma +
  954. sec4_sg_index * sizeof(struct sec4_sg_entry);
  955. out_options = LDST_SGF;
  956. }
  957. }
  958. append_seq_out_ptr(desc, dst_dma, req->nbytes, out_options);
  959. }
  960. /*
  961. * allocate and map the aead extended descriptor
  962. */
  963. static struct aead_edesc *aead_edesc_alloc(struct aead_request *req,
  964. int desc_bytes, bool *all_contig_ptr)
  965. {
  966. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  967. struct caam_ctx *ctx = crypto_aead_ctx(aead);
  968. struct device *jrdev = ctx->jrdev;
  969. gfp_t flags = (req->base.flags & (CRYPTO_TFM_REQ_MAY_BACKLOG |
  970. CRYPTO_TFM_REQ_MAY_SLEEP)) ? GFP_KERNEL : GFP_ATOMIC;
  971. int assoc_nents, src_nents, dst_nents = 0;
  972. struct aead_edesc *edesc;
  973. dma_addr_t iv_dma = 0;
  974. int sgc;
  975. bool all_contig = true;
  976. bool assoc_chained = false, src_chained = false, dst_chained = false;
  977. int ivsize = crypto_aead_ivsize(aead);
  978. int sec4_sg_index, sec4_sg_len = 0, sec4_sg_bytes;
  979. assoc_nents = sg_count(req->assoc, req->assoclen, &assoc_chained);
  980. src_nents = sg_count(req->src, req->cryptlen, &src_chained);
  981. if (unlikely(req->dst != req->src))
  982. dst_nents = sg_count(req->dst, req->cryptlen, &dst_chained);
  983. sgc = dma_map_sg_chained(jrdev, req->assoc, assoc_nents ? : 1,
  984. DMA_TO_DEVICE, assoc_chained);
  985. if (likely(req->src == req->dst)) {
  986. sgc = dma_map_sg_chained(jrdev, req->src, src_nents ? : 1,
  987. DMA_BIDIRECTIONAL, src_chained);
  988. } else {
  989. sgc = dma_map_sg_chained(jrdev, req->src, src_nents ? : 1,
  990. DMA_TO_DEVICE, src_chained);
  991. sgc = dma_map_sg_chained(jrdev, req->dst, dst_nents ? : 1,
  992. DMA_FROM_DEVICE, dst_chained);
  993. }
  994. /* Check if data are contiguous */
  995. iv_dma = dma_map_single(jrdev, req->iv, ivsize, DMA_TO_DEVICE);
  996. if (assoc_nents || sg_dma_address(req->assoc) + req->assoclen !=
  997. iv_dma || src_nents || iv_dma + ivsize !=
  998. sg_dma_address(req->src)) {
  999. all_contig = false;
  1000. assoc_nents = assoc_nents ? : 1;
  1001. src_nents = src_nents ? : 1;
  1002. sec4_sg_len = assoc_nents + 1 + src_nents;
  1003. }
  1004. sec4_sg_len += dst_nents;
  1005. sec4_sg_bytes = sec4_sg_len * sizeof(struct sec4_sg_entry);
  1006. /* allocate space for base edesc and hw desc commands, link tables */
  1007. edesc = kmalloc(sizeof(struct aead_edesc) + desc_bytes +
  1008. sec4_sg_bytes, GFP_DMA | flags);
  1009. if (!edesc) {
  1010. dev_err(jrdev, "could not allocate extended descriptor\n");
  1011. return ERR_PTR(-ENOMEM);
  1012. }
  1013. edesc->assoc_nents = assoc_nents;
  1014. edesc->assoc_chained = assoc_chained;
  1015. edesc->src_nents = src_nents;
  1016. edesc->src_chained = src_chained;
  1017. edesc->dst_nents = dst_nents;
  1018. edesc->dst_chained = dst_chained;
  1019. edesc->iv_dma = iv_dma;
  1020. edesc->sec4_sg_bytes = sec4_sg_bytes;
  1021. edesc->sec4_sg = (void *)edesc + sizeof(struct aead_edesc) +
  1022. desc_bytes;
  1023. edesc->sec4_sg_dma = dma_map_single(jrdev, edesc->sec4_sg,
  1024. sec4_sg_bytes, DMA_TO_DEVICE);
  1025. *all_contig_ptr = all_contig;
  1026. sec4_sg_index = 0;
  1027. if (!all_contig) {
  1028. sg_to_sec4_sg(req->assoc,
  1029. (assoc_nents ? : 1),
  1030. edesc->sec4_sg +
  1031. sec4_sg_index, 0);
  1032. sec4_sg_index += assoc_nents ? : 1;
  1033. dma_to_sec4_sg_one(edesc->sec4_sg + sec4_sg_index,
  1034. iv_dma, ivsize, 0);
  1035. sec4_sg_index += 1;
  1036. sg_to_sec4_sg_last(req->src,
  1037. (src_nents ? : 1),
  1038. edesc->sec4_sg +
  1039. sec4_sg_index, 0);
  1040. sec4_sg_index += src_nents ? : 1;
  1041. }
  1042. if (dst_nents) {
  1043. sg_to_sec4_sg_last(req->dst, dst_nents,
  1044. edesc->sec4_sg + sec4_sg_index, 0);
  1045. }
  1046. return edesc;
  1047. }
  1048. static int aead_encrypt(struct aead_request *req)
  1049. {
  1050. struct aead_edesc *edesc;
  1051. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  1052. struct caam_ctx *ctx = crypto_aead_ctx(aead);
  1053. struct device *jrdev = ctx->jrdev;
  1054. bool all_contig;
  1055. u32 *desc;
  1056. int ret = 0;
  1057. req->cryptlen += ctx->authsize;
  1058. /* allocate extended descriptor */
  1059. edesc = aead_edesc_alloc(req, DESC_JOB_IO_LEN *
  1060. CAAM_CMD_SZ, &all_contig);
  1061. if (IS_ERR(edesc))
  1062. return PTR_ERR(edesc);
  1063. /* Create and submit job descriptor */
  1064. init_aead_job(ctx->sh_desc_enc, ctx->sh_desc_enc_dma, edesc, req,
  1065. all_contig, true);
  1066. #ifdef DEBUG
  1067. print_hex_dump(KERN_ERR, "aead jobdesc@"__stringify(__LINE__)": ",
  1068. DUMP_PREFIX_ADDRESS, 16, 4, edesc->hw_desc,
  1069. desc_bytes(edesc->hw_desc), 1);
  1070. #endif
  1071. desc = edesc->hw_desc;
  1072. ret = caam_jr_enqueue(jrdev, desc, aead_encrypt_done, req);
  1073. if (!ret) {
  1074. ret = -EINPROGRESS;
  1075. } else {
  1076. aead_unmap(jrdev, edesc, req);
  1077. kfree(edesc);
  1078. }
  1079. return ret;
  1080. }
  1081. static int aead_decrypt(struct aead_request *req)
  1082. {
  1083. struct aead_edesc *edesc;
  1084. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  1085. struct caam_ctx *ctx = crypto_aead_ctx(aead);
  1086. struct device *jrdev = ctx->jrdev;
  1087. bool all_contig;
  1088. u32 *desc;
  1089. int ret = 0;
  1090. /* allocate extended descriptor */
  1091. edesc = aead_edesc_alloc(req, DESC_JOB_IO_LEN *
  1092. CAAM_CMD_SZ, &all_contig);
  1093. if (IS_ERR(edesc))
  1094. return PTR_ERR(edesc);
  1095. #ifdef DEBUG
  1096. print_hex_dump(KERN_ERR, "dec src@"__stringify(__LINE__)": ",
  1097. DUMP_PREFIX_ADDRESS, 16, 4, sg_virt(req->src),
  1098. req->cryptlen, 1);
  1099. #endif
  1100. /* Create and submit job descriptor*/
  1101. init_aead_job(ctx->sh_desc_dec,
  1102. ctx->sh_desc_dec_dma, edesc, req, all_contig, false);
  1103. #ifdef DEBUG
  1104. print_hex_dump(KERN_ERR, "aead jobdesc@"__stringify(__LINE__)": ",
  1105. DUMP_PREFIX_ADDRESS, 16, 4, edesc->hw_desc,
  1106. desc_bytes(edesc->hw_desc), 1);
  1107. #endif
  1108. desc = edesc->hw_desc;
  1109. ret = caam_jr_enqueue(jrdev, desc, aead_decrypt_done, req);
  1110. if (!ret) {
  1111. ret = -EINPROGRESS;
  1112. } else {
  1113. aead_unmap(jrdev, edesc, req);
  1114. kfree(edesc);
  1115. }
  1116. return ret;
  1117. }
  1118. /*
  1119. * allocate and map the aead extended descriptor for aead givencrypt
  1120. */
  1121. static struct aead_edesc *aead_giv_edesc_alloc(struct aead_givcrypt_request
  1122. *greq, int desc_bytes,
  1123. u32 *contig_ptr)
  1124. {
  1125. struct aead_request *req = &greq->areq;
  1126. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  1127. struct caam_ctx *ctx = crypto_aead_ctx(aead);
  1128. struct device *jrdev = ctx->jrdev;
  1129. gfp_t flags = (req->base.flags & (CRYPTO_TFM_REQ_MAY_BACKLOG |
  1130. CRYPTO_TFM_REQ_MAY_SLEEP)) ? GFP_KERNEL : GFP_ATOMIC;
  1131. int assoc_nents, src_nents, dst_nents = 0;
  1132. struct aead_edesc *edesc;
  1133. dma_addr_t iv_dma = 0;
  1134. int sgc;
  1135. u32 contig = GIV_SRC_CONTIG | GIV_DST_CONTIG;
  1136. int ivsize = crypto_aead_ivsize(aead);
  1137. bool assoc_chained = false, src_chained = false, dst_chained = false;
  1138. int sec4_sg_index, sec4_sg_len = 0, sec4_sg_bytes;
  1139. assoc_nents = sg_count(req->assoc, req->assoclen, &assoc_chained);
  1140. src_nents = sg_count(req->src, req->cryptlen, &src_chained);
  1141. if (unlikely(req->dst != req->src))
  1142. dst_nents = sg_count(req->dst, req->cryptlen, &dst_chained);
  1143. sgc = dma_map_sg_chained(jrdev, req->assoc, assoc_nents ? : 1,
  1144. DMA_TO_DEVICE, assoc_chained);
  1145. if (likely(req->src == req->dst)) {
  1146. sgc = dma_map_sg_chained(jrdev, req->src, src_nents ? : 1,
  1147. DMA_BIDIRECTIONAL, src_chained);
  1148. } else {
  1149. sgc = dma_map_sg_chained(jrdev, req->src, src_nents ? : 1,
  1150. DMA_TO_DEVICE, src_chained);
  1151. sgc = dma_map_sg_chained(jrdev, req->dst, dst_nents ? : 1,
  1152. DMA_FROM_DEVICE, dst_chained);
  1153. }
  1154. /* Check if data are contiguous */
  1155. iv_dma = dma_map_single(jrdev, greq->giv, ivsize, DMA_TO_DEVICE);
  1156. if (assoc_nents || sg_dma_address(req->assoc) + req->assoclen !=
  1157. iv_dma || src_nents || iv_dma + ivsize != sg_dma_address(req->src))
  1158. contig &= ~GIV_SRC_CONTIG;
  1159. if (dst_nents || iv_dma + ivsize != sg_dma_address(req->dst))
  1160. contig &= ~GIV_DST_CONTIG;
  1161. if (unlikely(req->src != req->dst)) {
  1162. dst_nents = dst_nents ? : 1;
  1163. sec4_sg_len += 1;
  1164. }
  1165. if (!(contig & GIV_SRC_CONTIG)) {
  1166. assoc_nents = assoc_nents ? : 1;
  1167. src_nents = src_nents ? : 1;
  1168. sec4_sg_len += assoc_nents + 1 + src_nents;
  1169. if (likely(req->src == req->dst))
  1170. contig &= ~GIV_DST_CONTIG;
  1171. }
  1172. sec4_sg_len += dst_nents;
  1173. sec4_sg_bytes = sec4_sg_len * sizeof(struct sec4_sg_entry);
  1174. /* allocate space for base edesc and hw desc commands, link tables */
  1175. edesc = kmalloc(sizeof(struct aead_edesc) + desc_bytes +
  1176. sec4_sg_bytes, GFP_DMA | flags);
  1177. if (!edesc) {
  1178. dev_err(jrdev, "could not allocate extended descriptor\n");
  1179. return ERR_PTR(-ENOMEM);
  1180. }
  1181. edesc->assoc_nents = assoc_nents;
  1182. edesc->assoc_chained = assoc_chained;
  1183. edesc->src_nents = src_nents;
  1184. edesc->src_chained = src_chained;
  1185. edesc->dst_nents = dst_nents;
  1186. edesc->dst_chained = dst_chained;
  1187. edesc->iv_dma = iv_dma;
  1188. edesc->sec4_sg_bytes = sec4_sg_bytes;
  1189. edesc->sec4_sg = (void *)edesc + sizeof(struct aead_edesc) +
  1190. desc_bytes;
  1191. edesc->sec4_sg_dma = dma_map_single(jrdev, edesc->sec4_sg,
  1192. sec4_sg_bytes, DMA_TO_DEVICE);
  1193. *contig_ptr = contig;
  1194. sec4_sg_index = 0;
  1195. if (!(contig & GIV_SRC_CONTIG)) {
  1196. sg_to_sec4_sg(req->assoc, assoc_nents,
  1197. edesc->sec4_sg +
  1198. sec4_sg_index, 0);
  1199. sec4_sg_index += assoc_nents;
  1200. dma_to_sec4_sg_one(edesc->sec4_sg + sec4_sg_index,
  1201. iv_dma, ivsize, 0);
  1202. sec4_sg_index += 1;
  1203. sg_to_sec4_sg_last(req->src, src_nents,
  1204. edesc->sec4_sg +
  1205. sec4_sg_index, 0);
  1206. sec4_sg_index += src_nents;
  1207. }
  1208. if (unlikely(req->src != req->dst && !(contig & GIV_DST_CONTIG))) {
  1209. dma_to_sec4_sg_one(edesc->sec4_sg + sec4_sg_index,
  1210. iv_dma, ivsize, 0);
  1211. sec4_sg_index += 1;
  1212. sg_to_sec4_sg_last(req->dst, dst_nents,
  1213. edesc->sec4_sg + sec4_sg_index, 0);
  1214. }
  1215. return edesc;
  1216. }
  1217. static int aead_givencrypt(struct aead_givcrypt_request *areq)
  1218. {
  1219. struct aead_request *req = &areq->areq;
  1220. struct aead_edesc *edesc;
  1221. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  1222. struct caam_ctx *ctx = crypto_aead_ctx(aead);
  1223. struct device *jrdev = ctx->jrdev;
  1224. u32 contig;
  1225. u32 *desc;
  1226. int ret = 0;
  1227. req->cryptlen += ctx->authsize;
  1228. /* allocate extended descriptor */
  1229. edesc = aead_giv_edesc_alloc(areq, DESC_JOB_IO_LEN *
  1230. CAAM_CMD_SZ, &contig);
  1231. if (IS_ERR(edesc))
  1232. return PTR_ERR(edesc);
  1233. #ifdef DEBUG
  1234. print_hex_dump(KERN_ERR, "giv src@"__stringify(__LINE__)": ",
  1235. DUMP_PREFIX_ADDRESS, 16, 4, sg_virt(req->src),
  1236. req->cryptlen, 1);
  1237. #endif
  1238. /* Create and submit job descriptor*/
  1239. init_aead_giv_job(ctx->sh_desc_givenc,
  1240. ctx->sh_desc_givenc_dma, edesc, req, contig);
  1241. #ifdef DEBUG
  1242. print_hex_dump(KERN_ERR, "aead jobdesc@"__stringify(__LINE__)": ",
  1243. DUMP_PREFIX_ADDRESS, 16, 4, edesc->hw_desc,
  1244. desc_bytes(edesc->hw_desc), 1);
  1245. #endif
  1246. desc = edesc->hw_desc;
  1247. ret = caam_jr_enqueue(jrdev, desc, aead_encrypt_done, req);
  1248. if (!ret) {
  1249. ret = -EINPROGRESS;
  1250. } else {
  1251. aead_unmap(jrdev, edesc, req);
  1252. kfree(edesc);
  1253. }
  1254. return ret;
  1255. }
  1256. /*
  1257. * allocate and map the ablkcipher extended descriptor for ablkcipher
  1258. */
  1259. static struct ablkcipher_edesc *ablkcipher_edesc_alloc(struct ablkcipher_request
  1260. *req, int desc_bytes,
  1261. bool *iv_contig_out)
  1262. {
  1263. struct crypto_ablkcipher *ablkcipher = crypto_ablkcipher_reqtfm(req);
  1264. struct caam_ctx *ctx = crypto_ablkcipher_ctx(ablkcipher);
  1265. struct device *jrdev = ctx->jrdev;
  1266. gfp_t flags = (req->base.flags & (CRYPTO_TFM_REQ_MAY_BACKLOG |
  1267. CRYPTO_TFM_REQ_MAY_SLEEP)) ?
  1268. GFP_KERNEL : GFP_ATOMIC;
  1269. int src_nents, dst_nents = 0, sec4_sg_bytes;
  1270. struct ablkcipher_edesc *edesc;
  1271. dma_addr_t iv_dma = 0;
  1272. bool iv_contig = false;
  1273. int sgc;
  1274. int ivsize = crypto_ablkcipher_ivsize(ablkcipher);
  1275. bool src_chained = false, dst_chained = false;
  1276. int sec4_sg_index;
  1277. src_nents = sg_count(req->src, req->nbytes, &src_chained);
  1278. if (req->dst != req->src)
  1279. dst_nents = sg_count(req->dst, req->nbytes, &dst_chained);
  1280. if (likely(req->src == req->dst)) {
  1281. sgc = dma_map_sg_chained(jrdev, req->src, src_nents ? : 1,
  1282. DMA_BIDIRECTIONAL, src_chained);
  1283. } else {
  1284. sgc = dma_map_sg_chained(jrdev, req->src, src_nents ? : 1,
  1285. DMA_TO_DEVICE, src_chained);
  1286. sgc = dma_map_sg_chained(jrdev, req->dst, dst_nents ? : 1,
  1287. DMA_FROM_DEVICE, dst_chained);
  1288. }
  1289. /*
  1290. * Check if iv can be contiguous with source and destination.
  1291. * If so, include it. If not, create scatterlist.
  1292. */
  1293. iv_dma = dma_map_single(jrdev, req->info, ivsize, DMA_TO_DEVICE);
  1294. if (!src_nents && iv_dma + ivsize == sg_dma_address(req->src))
  1295. iv_contig = true;
  1296. else
  1297. src_nents = src_nents ? : 1;
  1298. sec4_sg_bytes = ((iv_contig ? 0 : 1) + src_nents + dst_nents) *
  1299. sizeof(struct sec4_sg_entry);
  1300. /* allocate space for base edesc and hw desc commands, link tables */
  1301. edesc = kmalloc(sizeof(struct ablkcipher_edesc) + desc_bytes +
  1302. sec4_sg_bytes, GFP_DMA | flags);
  1303. if (!edesc) {
  1304. dev_err(jrdev, "could not allocate extended descriptor\n");
  1305. return ERR_PTR(-ENOMEM);
  1306. }
  1307. edesc->src_nents = src_nents;
  1308. edesc->src_chained = src_chained;
  1309. edesc->dst_nents = dst_nents;
  1310. edesc->dst_chained = dst_chained;
  1311. edesc->sec4_sg_bytes = sec4_sg_bytes;
  1312. edesc->sec4_sg = (void *)edesc + sizeof(struct ablkcipher_edesc) +
  1313. desc_bytes;
  1314. sec4_sg_index = 0;
  1315. if (!iv_contig) {
  1316. dma_to_sec4_sg_one(edesc->sec4_sg, iv_dma, ivsize, 0);
  1317. sg_to_sec4_sg_last(req->src, src_nents,
  1318. edesc->sec4_sg + 1, 0);
  1319. sec4_sg_index += 1 + src_nents;
  1320. }
  1321. if (dst_nents) {
  1322. sg_to_sec4_sg_last(req->dst, dst_nents,
  1323. edesc->sec4_sg + sec4_sg_index, 0);
  1324. }
  1325. edesc->sec4_sg_dma = dma_map_single(jrdev, edesc->sec4_sg,
  1326. sec4_sg_bytes, DMA_TO_DEVICE);
  1327. edesc->iv_dma = iv_dma;
  1328. #ifdef DEBUG
  1329. print_hex_dump(KERN_ERR, "ablkcipher sec4_sg@"__stringify(__LINE__)": ",
  1330. DUMP_PREFIX_ADDRESS, 16, 4, edesc->sec4_sg,
  1331. sec4_sg_bytes, 1);
  1332. #endif
  1333. *iv_contig_out = iv_contig;
  1334. return edesc;
  1335. }
  1336. static int ablkcipher_encrypt(struct ablkcipher_request *req)
  1337. {
  1338. struct ablkcipher_edesc *edesc;
  1339. struct crypto_ablkcipher *ablkcipher = crypto_ablkcipher_reqtfm(req);
  1340. struct caam_ctx *ctx = crypto_ablkcipher_ctx(ablkcipher);
  1341. struct device *jrdev = ctx->jrdev;
  1342. bool iv_contig;
  1343. u32 *desc;
  1344. int ret = 0;
  1345. /* allocate extended descriptor */
  1346. edesc = ablkcipher_edesc_alloc(req, DESC_JOB_IO_LEN *
  1347. CAAM_CMD_SZ, &iv_contig);
  1348. if (IS_ERR(edesc))
  1349. return PTR_ERR(edesc);
  1350. /* Create and submit job descriptor*/
  1351. init_ablkcipher_job(ctx->sh_desc_enc,
  1352. ctx->sh_desc_enc_dma, edesc, req, iv_contig);
  1353. #ifdef DEBUG
  1354. print_hex_dump(KERN_ERR, "ablkcipher jobdesc@"__stringify(__LINE__)": ",
  1355. DUMP_PREFIX_ADDRESS, 16, 4, edesc->hw_desc,
  1356. desc_bytes(edesc->hw_desc), 1);
  1357. #endif
  1358. desc = edesc->hw_desc;
  1359. ret = caam_jr_enqueue(jrdev, desc, ablkcipher_encrypt_done, req);
  1360. if (!ret) {
  1361. ret = -EINPROGRESS;
  1362. } else {
  1363. ablkcipher_unmap(jrdev, edesc, req);
  1364. kfree(edesc);
  1365. }
  1366. return ret;
  1367. }
  1368. static int ablkcipher_decrypt(struct ablkcipher_request *req)
  1369. {
  1370. struct ablkcipher_edesc *edesc;
  1371. struct crypto_ablkcipher *ablkcipher = crypto_ablkcipher_reqtfm(req);
  1372. struct caam_ctx *ctx = crypto_ablkcipher_ctx(ablkcipher);
  1373. struct device *jrdev = ctx->jrdev;
  1374. bool iv_contig;
  1375. u32 *desc;
  1376. int ret = 0;
  1377. /* allocate extended descriptor */
  1378. edesc = ablkcipher_edesc_alloc(req, DESC_JOB_IO_LEN *
  1379. CAAM_CMD_SZ, &iv_contig);
  1380. if (IS_ERR(edesc))
  1381. return PTR_ERR(edesc);
  1382. /* Create and submit job descriptor*/
  1383. init_ablkcipher_job(ctx->sh_desc_dec,
  1384. ctx->sh_desc_dec_dma, edesc, req, iv_contig);
  1385. desc = edesc->hw_desc;
  1386. #ifdef DEBUG
  1387. print_hex_dump(KERN_ERR, "ablkcipher jobdesc@"__stringify(__LINE__)": ",
  1388. DUMP_PREFIX_ADDRESS, 16, 4, edesc->hw_desc,
  1389. desc_bytes(edesc->hw_desc), 1);
  1390. #endif
  1391. ret = caam_jr_enqueue(jrdev, desc, ablkcipher_decrypt_done, req);
  1392. if (!ret) {
  1393. ret = -EINPROGRESS;
  1394. } else {
  1395. ablkcipher_unmap(jrdev, edesc, req);
  1396. kfree(edesc);
  1397. }
  1398. return ret;
  1399. }
  1400. #define template_aead template_u.aead
  1401. #define template_ablkcipher template_u.ablkcipher
  1402. struct caam_alg_template {
  1403. char name[CRYPTO_MAX_ALG_NAME];
  1404. char driver_name[CRYPTO_MAX_ALG_NAME];
  1405. unsigned int blocksize;
  1406. u32 type;
  1407. union {
  1408. struct ablkcipher_alg ablkcipher;
  1409. struct aead_alg aead;
  1410. struct blkcipher_alg blkcipher;
  1411. struct cipher_alg cipher;
  1412. struct compress_alg compress;
  1413. struct rng_alg rng;
  1414. } template_u;
  1415. u32 class1_alg_type;
  1416. u32 class2_alg_type;
  1417. u32 alg_op;
  1418. };
  1419. static struct caam_alg_template driver_algs[] = {
  1420. /* single-pass ipsec_esp descriptor */
  1421. {
  1422. .name = "authenc(hmac(md5),cbc(aes))",
  1423. .driver_name = "authenc-hmac-md5-cbc-aes-caam",
  1424. .blocksize = AES_BLOCK_SIZE,
  1425. .type = CRYPTO_ALG_TYPE_AEAD,
  1426. .template_aead = {
  1427. .setkey = aead_setkey,
  1428. .setauthsize = aead_setauthsize,
  1429. .encrypt = aead_encrypt,
  1430. .decrypt = aead_decrypt,
  1431. .givencrypt = aead_givencrypt,
  1432. .geniv = "<built-in>",
  1433. .ivsize = AES_BLOCK_SIZE,
  1434. .maxauthsize = MD5_DIGEST_SIZE,
  1435. },
  1436. .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
  1437. .class2_alg_type = OP_ALG_ALGSEL_MD5 | OP_ALG_AAI_HMAC_PRECOMP,
  1438. .alg_op = OP_ALG_ALGSEL_MD5 | OP_ALG_AAI_HMAC,
  1439. },
  1440. {
  1441. .name = "authenc(hmac(sha1),cbc(aes))",
  1442. .driver_name = "authenc-hmac-sha1-cbc-aes-caam",
  1443. .blocksize = AES_BLOCK_SIZE,
  1444. .type = CRYPTO_ALG_TYPE_AEAD,
  1445. .template_aead = {
  1446. .setkey = aead_setkey,
  1447. .setauthsize = aead_setauthsize,
  1448. .encrypt = aead_encrypt,
  1449. .decrypt = aead_decrypt,
  1450. .givencrypt = aead_givencrypt,
  1451. .geniv = "<built-in>",
  1452. .ivsize = AES_BLOCK_SIZE,
  1453. .maxauthsize = SHA1_DIGEST_SIZE,
  1454. },
  1455. .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
  1456. .class2_alg_type = OP_ALG_ALGSEL_SHA1 | OP_ALG_AAI_HMAC_PRECOMP,
  1457. .alg_op = OP_ALG_ALGSEL_SHA1 | OP_ALG_AAI_HMAC,
  1458. },
  1459. {
  1460. .name = "authenc(hmac(sha224),cbc(aes))",
  1461. .driver_name = "authenc-hmac-sha224-cbc-aes-caam",
  1462. .blocksize = AES_BLOCK_SIZE,
  1463. .type = CRYPTO_ALG_TYPE_AEAD,
  1464. .template_aead = {
  1465. .setkey = aead_setkey,
  1466. .setauthsize = aead_setauthsize,
  1467. .encrypt = aead_encrypt,
  1468. .decrypt = aead_decrypt,
  1469. .givencrypt = aead_givencrypt,
  1470. .geniv = "<built-in>",
  1471. .ivsize = AES_BLOCK_SIZE,
  1472. .maxauthsize = SHA224_DIGEST_SIZE,
  1473. },
  1474. .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
  1475. .class2_alg_type = OP_ALG_ALGSEL_SHA224 |
  1476. OP_ALG_AAI_HMAC_PRECOMP,
  1477. .alg_op = OP_ALG_ALGSEL_SHA224 | OP_ALG_AAI_HMAC,
  1478. },
  1479. {
  1480. .name = "authenc(hmac(sha256),cbc(aes))",
  1481. .driver_name = "authenc-hmac-sha256-cbc-aes-caam",
  1482. .blocksize = AES_BLOCK_SIZE,
  1483. .type = CRYPTO_ALG_TYPE_AEAD,
  1484. .template_aead = {
  1485. .setkey = aead_setkey,
  1486. .setauthsize = aead_setauthsize,
  1487. .encrypt = aead_encrypt,
  1488. .decrypt = aead_decrypt,
  1489. .givencrypt = aead_givencrypt,
  1490. .geniv = "<built-in>",
  1491. .ivsize = AES_BLOCK_SIZE,
  1492. .maxauthsize = SHA256_DIGEST_SIZE,
  1493. },
  1494. .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
  1495. .class2_alg_type = OP_ALG_ALGSEL_SHA256 |
  1496. OP_ALG_AAI_HMAC_PRECOMP,
  1497. .alg_op = OP_ALG_ALGSEL_SHA256 | OP_ALG_AAI_HMAC,
  1498. },
  1499. {
  1500. .name = "authenc(hmac(sha384),cbc(aes))",
  1501. .driver_name = "authenc-hmac-sha384-cbc-aes-caam",
  1502. .blocksize = AES_BLOCK_SIZE,
  1503. .type = CRYPTO_ALG_TYPE_AEAD,
  1504. .template_aead = {
  1505. .setkey = aead_setkey,
  1506. .setauthsize = aead_setauthsize,
  1507. .encrypt = aead_encrypt,
  1508. .decrypt = aead_decrypt,
  1509. .givencrypt = aead_givencrypt,
  1510. .geniv = "<built-in>",
  1511. .ivsize = AES_BLOCK_SIZE,
  1512. .maxauthsize = SHA384_DIGEST_SIZE,
  1513. },
  1514. .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
  1515. .class2_alg_type = OP_ALG_ALGSEL_SHA384 |
  1516. OP_ALG_AAI_HMAC_PRECOMP,
  1517. .alg_op = OP_ALG_ALGSEL_SHA384 | OP_ALG_AAI_HMAC,
  1518. },
  1519. {
  1520. .name = "authenc(hmac(sha512),cbc(aes))",
  1521. .driver_name = "authenc-hmac-sha512-cbc-aes-caam",
  1522. .blocksize = AES_BLOCK_SIZE,
  1523. .type = CRYPTO_ALG_TYPE_AEAD,
  1524. .template_aead = {
  1525. .setkey = aead_setkey,
  1526. .setauthsize = aead_setauthsize,
  1527. .encrypt = aead_encrypt,
  1528. .decrypt = aead_decrypt,
  1529. .givencrypt = aead_givencrypt,
  1530. .geniv = "<built-in>",
  1531. .ivsize = AES_BLOCK_SIZE,
  1532. .maxauthsize = SHA512_DIGEST_SIZE,
  1533. },
  1534. .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
  1535. .class2_alg_type = OP_ALG_ALGSEL_SHA512 |
  1536. OP_ALG_AAI_HMAC_PRECOMP,
  1537. .alg_op = OP_ALG_ALGSEL_SHA512 | OP_ALG_AAI_HMAC,
  1538. },
  1539. {
  1540. .name = "authenc(hmac(md5),cbc(des3_ede))",
  1541. .driver_name = "authenc-hmac-md5-cbc-des3_ede-caam",
  1542. .blocksize = DES3_EDE_BLOCK_SIZE,
  1543. .type = CRYPTO_ALG_TYPE_AEAD,
  1544. .template_aead = {
  1545. .setkey = aead_setkey,
  1546. .setauthsize = aead_setauthsize,
  1547. .encrypt = aead_encrypt,
  1548. .decrypt = aead_decrypt,
  1549. .givencrypt = aead_givencrypt,
  1550. .geniv = "<built-in>",
  1551. .ivsize = DES3_EDE_BLOCK_SIZE,
  1552. .maxauthsize = MD5_DIGEST_SIZE,
  1553. },
  1554. .class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
  1555. .class2_alg_type = OP_ALG_ALGSEL_MD5 | OP_ALG_AAI_HMAC_PRECOMP,
  1556. .alg_op = OP_ALG_ALGSEL_MD5 | OP_ALG_AAI_HMAC,
  1557. },
  1558. {
  1559. .name = "authenc(hmac(sha1),cbc(des3_ede))",
  1560. .driver_name = "authenc-hmac-sha1-cbc-des3_ede-caam",
  1561. .blocksize = DES3_EDE_BLOCK_SIZE,
  1562. .type = CRYPTO_ALG_TYPE_AEAD,
  1563. .template_aead = {
  1564. .setkey = aead_setkey,
  1565. .setauthsize = aead_setauthsize,
  1566. .encrypt = aead_encrypt,
  1567. .decrypt = aead_decrypt,
  1568. .givencrypt = aead_givencrypt,
  1569. .geniv = "<built-in>",
  1570. .ivsize = DES3_EDE_BLOCK_SIZE,
  1571. .maxauthsize = SHA1_DIGEST_SIZE,
  1572. },
  1573. .class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
  1574. .class2_alg_type = OP_ALG_ALGSEL_SHA1 | OP_ALG_AAI_HMAC_PRECOMP,
  1575. .alg_op = OP_ALG_ALGSEL_SHA1 | OP_ALG_AAI_HMAC,
  1576. },
  1577. {
  1578. .name = "authenc(hmac(sha224),cbc(des3_ede))",
  1579. .driver_name = "authenc-hmac-sha224-cbc-des3_ede-caam",
  1580. .blocksize = DES3_EDE_BLOCK_SIZE,
  1581. .type = CRYPTO_ALG_TYPE_AEAD,
  1582. .template_aead = {
  1583. .setkey = aead_setkey,
  1584. .setauthsize = aead_setauthsize,
  1585. .encrypt = aead_encrypt,
  1586. .decrypt = aead_decrypt,
  1587. .givencrypt = aead_givencrypt,
  1588. .geniv = "<built-in>",
  1589. .ivsize = DES3_EDE_BLOCK_SIZE,
  1590. .maxauthsize = SHA224_DIGEST_SIZE,
  1591. },
  1592. .class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
  1593. .class2_alg_type = OP_ALG_ALGSEL_SHA224 |
  1594. OP_ALG_AAI_HMAC_PRECOMP,
  1595. .alg_op = OP_ALG_ALGSEL_SHA224 | OP_ALG_AAI_HMAC,
  1596. },
  1597. {
  1598. .name = "authenc(hmac(sha256),cbc(des3_ede))",
  1599. .driver_name = "authenc-hmac-sha256-cbc-des3_ede-caam",
  1600. .blocksize = DES3_EDE_BLOCK_SIZE,
  1601. .type = CRYPTO_ALG_TYPE_AEAD,
  1602. .template_aead = {
  1603. .setkey = aead_setkey,
  1604. .setauthsize = aead_setauthsize,
  1605. .encrypt = aead_encrypt,
  1606. .decrypt = aead_decrypt,
  1607. .givencrypt = aead_givencrypt,
  1608. .geniv = "<built-in>",
  1609. .ivsize = DES3_EDE_BLOCK_SIZE,
  1610. .maxauthsize = SHA256_DIGEST_SIZE,
  1611. },
  1612. .class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
  1613. .class2_alg_type = OP_ALG_ALGSEL_SHA256 |
  1614. OP_ALG_AAI_HMAC_PRECOMP,
  1615. .alg_op = OP_ALG_ALGSEL_SHA256 | OP_ALG_AAI_HMAC,
  1616. },
  1617. {
  1618. .name = "authenc(hmac(sha384),cbc(des3_ede))",
  1619. .driver_name = "authenc-hmac-sha384-cbc-des3_ede-caam",
  1620. .blocksize = DES3_EDE_BLOCK_SIZE,
  1621. .type = CRYPTO_ALG_TYPE_AEAD,
  1622. .template_aead = {
  1623. .setkey = aead_setkey,
  1624. .setauthsize = aead_setauthsize,
  1625. .encrypt = aead_encrypt,
  1626. .decrypt = aead_decrypt,
  1627. .givencrypt = aead_givencrypt,
  1628. .geniv = "<built-in>",
  1629. .ivsize = DES3_EDE_BLOCK_SIZE,
  1630. .maxauthsize = SHA384_DIGEST_SIZE,
  1631. },
  1632. .class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
  1633. .class2_alg_type = OP_ALG_ALGSEL_SHA384 |
  1634. OP_ALG_AAI_HMAC_PRECOMP,
  1635. .alg_op = OP_ALG_ALGSEL_SHA384 | OP_ALG_AAI_HMAC,
  1636. },
  1637. {
  1638. .name = "authenc(hmac(sha512),cbc(des3_ede))",
  1639. .driver_name = "authenc-hmac-sha512-cbc-des3_ede-caam",
  1640. .blocksize = DES3_EDE_BLOCK_SIZE,
  1641. .type = CRYPTO_ALG_TYPE_AEAD,
  1642. .template_aead = {
  1643. .setkey = aead_setkey,
  1644. .setauthsize = aead_setauthsize,
  1645. .encrypt = aead_encrypt,
  1646. .decrypt = aead_decrypt,
  1647. .givencrypt = aead_givencrypt,
  1648. .geniv = "<built-in>",
  1649. .ivsize = DES3_EDE_BLOCK_SIZE,
  1650. .maxauthsize = SHA512_DIGEST_SIZE,
  1651. },
  1652. .class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
  1653. .class2_alg_type = OP_ALG_ALGSEL_SHA512 |
  1654. OP_ALG_AAI_HMAC_PRECOMP,
  1655. .alg_op = OP_ALG_ALGSEL_SHA512 | OP_ALG_AAI_HMAC,
  1656. },
  1657. {
  1658. .name = "authenc(hmac(md5),cbc(des))",
  1659. .driver_name = "authenc-hmac-md5-cbc-des-caam",
  1660. .blocksize = DES_BLOCK_SIZE,
  1661. .type = CRYPTO_ALG_TYPE_AEAD,
  1662. .template_aead = {
  1663. .setkey = aead_setkey,
  1664. .setauthsize = aead_setauthsize,
  1665. .encrypt = aead_encrypt,
  1666. .decrypt = aead_decrypt,
  1667. .givencrypt = aead_givencrypt,
  1668. .geniv = "<built-in>",
  1669. .ivsize = DES_BLOCK_SIZE,
  1670. .maxauthsize = MD5_DIGEST_SIZE,
  1671. },
  1672. .class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
  1673. .class2_alg_type = OP_ALG_ALGSEL_MD5 | OP_ALG_AAI_HMAC_PRECOMP,
  1674. .alg_op = OP_ALG_ALGSEL_MD5 | OP_ALG_AAI_HMAC,
  1675. },
  1676. {
  1677. .name = "authenc(hmac(sha1),cbc(des))",
  1678. .driver_name = "authenc-hmac-sha1-cbc-des-caam",
  1679. .blocksize = DES_BLOCK_SIZE,
  1680. .type = CRYPTO_ALG_TYPE_AEAD,
  1681. .template_aead = {
  1682. .setkey = aead_setkey,
  1683. .setauthsize = aead_setauthsize,
  1684. .encrypt = aead_encrypt,
  1685. .decrypt = aead_decrypt,
  1686. .givencrypt = aead_givencrypt,
  1687. .geniv = "<built-in>",
  1688. .ivsize = DES_BLOCK_SIZE,
  1689. .maxauthsize = SHA1_DIGEST_SIZE,
  1690. },
  1691. .class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
  1692. .class2_alg_type = OP_ALG_ALGSEL_SHA1 | OP_ALG_AAI_HMAC_PRECOMP,
  1693. .alg_op = OP_ALG_ALGSEL_SHA1 | OP_ALG_AAI_HMAC,
  1694. },
  1695. {
  1696. .name = "authenc(hmac(sha224),cbc(des))",
  1697. .driver_name = "authenc-hmac-sha224-cbc-des-caam",
  1698. .blocksize = DES_BLOCK_SIZE,
  1699. .type = CRYPTO_ALG_TYPE_AEAD,
  1700. .template_aead = {
  1701. .setkey = aead_setkey,
  1702. .setauthsize = aead_setauthsize,
  1703. .encrypt = aead_encrypt,
  1704. .decrypt = aead_decrypt,
  1705. .givencrypt = aead_givencrypt,
  1706. .geniv = "<built-in>",
  1707. .ivsize = DES_BLOCK_SIZE,
  1708. .maxauthsize = SHA224_DIGEST_SIZE,
  1709. },
  1710. .class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
  1711. .class2_alg_type = OP_ALG_ALGSEL_SHA224 |
  1712. OP_ALG_AAI_HMAC_PRECOMP,
  1713. .alg_op = OP_ALG_ALGSEL_SHA224 | OP_ALG_AAI_HMAC,
  1714. },
  1715. {
  1716. .name = "authenc(hmac(sha256),cbc(des))",
  1717. .driver_name = "authenc-hmac-sha256-cbc-des-caam",
  1718. .blocksize = DES_BLOCK_SIZE,
  1719. .type = CRYPTO_ALG_TYPE_AEAD,
  1720. .template_aead = {
  1721. .setkey = aead_setkey,
  1722. .setauthsize = aead_setauthsize,
  1723. .encrypt = aead_encrypt,
  1724. .decrypt = aead_decrypt,
  1725. .givencrypt = aead_givencrypt,
  1726. .geniv = "<built-in>",
  1727. .ivsize = DES_BLOCK_SIZE,
  1728. .maxauthsize = SHA256_DIGEST_SIZE,
  1729. },
  1730. .class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
  1731. .class2_alg_type = OP_ALG_ALGSEL_SHA256 |
  1732. OP_ALG_AAI_HMAC_PRECOMP,
  1733. .alg_op = OP_ALG_ALGSEL_SHA256 | OP_ALG_AAI_HMAC,
  1734. },
  1735. {
  1736. .name = "authenc(hmac(sha384),cbc(des))",
  1737. .driver_name = "authenc-hmac-sha384-cbc-des-caam",
  1738. .blocksize = DES_BLOCK_SIZE,
  1739. .type = CRYPTO_ALG_TYPE_AEAD,
  1740. .template_aead = {
  1741. .setkey = aead_setkey,
  1742. .setauthsize = aead_setauthsize,
  1743. .encrypt = aead_encrypt,
  1744. .decrypt = aead_decrypt,
  1745. .givencrypt = aead_givencrypt,
  1746. .geniv = "<built-in>",
  1747. .ivsize = DES_BLOCK_SIZE,
  1748. .maxauthsize = SHA384_DIGEST_SIZE,
  1749. },
  1750. .class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
  1751. .class2_alg_type = OP_ALG_ALGSEL_SHA384 |
  1752. OP_ALG_AAI_HMAC_PRECOMP,
  1753. .alg_op = OP_ALG_ALGSEL_SHA384 | OP_ALG_AAI_HMAC,
  1754. },
  1755. {
  1756. .name = "authenc(hmac(sha512),cbc(des))",
  1757. .driver_name = "authenc-hmac-sha512-cbc-des-caam",
  1758. .blocksize = DES_BLOCK_SIZE,
  1759. .type = CRYPTO_ALG_TYPE_AEAD,
  1760. .template_aead = {
  1761. .setkey = aead_setkey,
  1762. .setauthsize = aead_setauthsize,
  1763. .encrypt = aead_encrypt,
  1764. .decrypt = aead_decrypt,
  1765. .givencrypt = aead_givencrypt,
  1766. .geniv = "<built-in>",
  1767. .ivsize = DES_BLOCK_SIZE,
  1768. .maxauthsize = SHA512_DIGEST_SIZE,
  1769. },
  1770. .class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
  1771. .class2_alg_type = OP_ALG_ALGSEL_SHA512 |
  1772. OP_ALG_AAI_HMAC_PRECOMP,
  1773. .alg_op = OP_ALG_ALGSEL_SHA512 | OP_ALG_AAI_HMAC,
  1774. },
  1775. /* ablkcipher descriptor */
  1776. {
  1777. .name = "cbc(aes)",
  1778. .driver_name = "cbc-aes-caam",
  1779. .blocksize = AES_BLOCK_SIZE,
  1780. .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
  1781. .template_ablkcipher = {
  1782. .setkey = ablkcipher_setkey,
  1783. .encrypt = ablkcipher_encrypt,
  1784. .decrypt = ablkcipher_decrypt,
  1785. .geniv = "eseqiv",
  1786. .min_keysize = AES_MIN_KEY_SIZE,
  1787. .max_keysize = AES_MAX_KEY_SIZE,
  1788. .ivsize = AES_BLOCK_SIZE,
  1789. },
  1790. .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CBC,
  1791. },
  1792. {
  1793. .name = "cbc(des3_ede)",
  1794. .driver_name = "cbc-3des-caam",
  1795. .blocksize = DES3_EDE_BLOCK_SIZE,
  1796. .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
  1797. .template_ablkcipher = {
  1798. .setkey = ablkcipher_setkey,
  1799. .encrypt = ablkcipher_encrypt,
  1800. .decrypt = ablkcipher_decrypt,
  1801. .geniv = "eseqiv",
  1802. .min_keysize = DES3_EDE_KEY_SIZE,
  1803. .max_keysize = DES3_EDE_KEY_SIZE,
  1804. .ivsize = DES3_EDE_BLOCK_SIZE,
  1805. },
  1806. .class1_alg_type = OP_ALG_ALGSEL_3DES | OP_ALG_AAI_CBC,
  1807. },
  1808. {
  1809. .name = "cbc(des)",
  1810. .driver_name = "cbc-des-caam",
  1811. .blocksize = DES_BLOCK_SIZE,
  1812. .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
  1813. .template_ablkcipher = {
  1814. .setkey = ablkcipher_setkey,
  1815. .encrypt = ablkcipher_encrypt,
  1816. .decrypt = ablkcipher_decrypt,
  1817. .geniv = "eseqiv",
  1818. .min_keysize = DES_KEY_SIZE,
  1819. .max_keysize = DES_KEY_SIZE,
  1820. .ivsize = DES_BLOCK_SIZE,
  1821. },
  1822. .class1_alg_type = OP_ALG_ALGSEL_DES | OP_ALG_AAI_CBC,
  1823. }
  1824. };
  1825. struct caam_crypto_alg {
  1826. struct list_head entry;
  1827. struct device *ctrldev;
  1828. int class1_alg_type;
  1829. int class2_alg_type;
  1830. int alg_op;
  1831. struct crypto_alg crypto_alg;
  1832. };
  1833. static int caam_cra_init(struct crypto_tfm *tfm)
  1834. {
  1835. struct crypto_alg *alg = tfm->__crt_alg;
  1836. struct caam_crypto_alg *caam_alg =
  1837. container_of(alg, struct caam_crypto_alg, crypto_alg);
  1838. struct caam_ctx *ctx = crypto_tfm_ctx(tfm);
  1839. struct caam_drv_private *priv = dev_get_drvdata(caam_alg->ctrldev);
  1840. int tgt_jr = atomic_inc_return(&priv->tfm_count);
  1841. /*
  1842. * distribute tfms across job rings to ensure in-order
  1843. * crypto request processing per tfm
  1844. */
  1845. ctx->jrdev = priv->jrdev[(tgt_jr / 2) % priv->total_jobrs];
  1846. /* copy descriptor header template value */
  1847. ctx->class1_alg_type = OP_TYPE_CLASS1_ALG | caam_alg->class1_alg_type;
  1848. ctx->class2_alg_type = OP_TYPE_CLASS2_ALG | caam_alg->class2_alg_type;
  1849. ctx->alg_op = OP_TYPE_CLASS2_ALG | caam_alg->alg_op;
  1850. return 0;
  1851. }
  1852. static void caam_cra_exit(struct crypto_tfm *tfm)
  1853. {
  1854. struct caam_ctx *ctx = crypto_tfm_ctx(tfm);
  1855. if (ctx->sh_desc_enc_dma &&
  1856. !dma_mapping_error(ctx->jrdev, ctx->sh_desc_enc_dma))
  1857. dma_unmap_single(ctx->jrdev, ctx->sh_desc_enc_dma,
  1858. desc_bytes(ctx->sh_desc_enc), DMA_TO_DEVICE);
  1859. if (ctx->sh_desc_dec_dma &&
  1860. !dma_mapping_error(ctx->jrdev, ctx->sh_desc_dec_dma))
  1861. dma_unmap_single(ctx->jrdev, ctx->sh_desc_dec_dma,
  1862. desc_bytes(ctx->sh_desc_dec), DMA_TO_DEVICE);
  1863. if (ctx->sh_desc_givenc_dma &&
  1864. !dma_mapping_error(ctx->jrdev, ctx->sh_desc_givenc_dma))
  1865. dma_unmap_single(ctx->jrdev, ctx->sh_desc_givenc_dma,
  1866. desc_bytes(ctx->sh_desc_givenc),
  1867. DMA_TO_DEVICE);
  1868. }
  1869. static void __exit caam_algapi_exit(void)
  1870. {
  1871. struct device_node *dev_node;
  1872. struct platform_device *pdev;
  1873. struct device *ctrldev;
  1874. struct caam_drv_private *priv;
  1875. struct caam_crypto_alg *t_alg, *n;
  1876. dev_node = of_find_compatible_node(NULL, NULL, "fsl,sec-v4.0");
  1877. if (!dev_node) {
  1878. dev_node = of_find_compatible_node(NULL, NULL, "fsl,sec4.0");
  1879. if (!dev_node)
  1880. return;
  1881. }
  1882. pdev = of_find_device_by_node(dev_node);
  1883. if (!pdev)
  1884. return;
  1885. ctrldev = &pdev->dev;
  1886. of_node_put(dev_node);
  1887. priv = dev_get_drvdata(ctrldev);
  1888. if (!priv->alg_list.next)
  1889. return;
  1890. list_for_each_entry_safe(t_alg, n, &priv->alg_list, entry) {
  1891. crypto_unregister_alg(&t_alg->crypto_alg);
  1892. list_del(&t_alg->entry);
  1893. kfree(t_alg);
  1894. }
  1895. }
  1896. static struct caam_crypto_alg *caam_alg_alloc(struct device *ctrldev,
  1897. struct caam_alg_template
  1898. *template)
  1899. {
  1900. struct caam_crypto_alg *t_alg;
  1901. struct crypto_alg *alg;
  1902. t_alg = kzalloc(sizeof(struct caam_crypto_alg), GFP_KERNEL);
  1903. if (!t_alg) {
  1904. dev_err(ctrldev, "failed to allocate t_alg\n");
  1905. return ERR_PTR(-ENOMEM);
  1906. }
  1907. alg = &t_alg->crypto_alg;
  1908. snprintf(alg->cra_name, CRYPTO_MAX_ALG_NAME, "%s", template->name);
  1909. snprintf(alg->cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
  1910. template->driver_name);
  1911. alg->cra_module = THIS_MODULE;
  1912. alg->cra_init = caam_cra_init;
  1913. alg->cra_exit = caam_cra_exit;
  1914. alg->cra_priority = CAAM_CRA_PRIORITY;
  1915. alg->cra_blocksize = template->blocksize;
  1916. alg->cra_alignmask = 0;
  1917. alg->cra_ctxsize = sizeof(struct caam_ctx);
  1918. alg->cra_flags = CRYPTO_ALG_ASYNC | CRYPTO_ALG_KERN_DRIVER_ONLY |
  1919. template->type;
  1920. switch (template->type) {
  1921. case CRYPTO_ALG_TYPE_ABLKCIPHER:
  1922. alg->cra_type = &crypto_ablkcipher_type;
  1923. alg->cra_ablkcipher = template->template_ablkcipher;
  1924. break;
  1925. case CRYPTO_ALG_TYPE_AEAD:
  1926. alg->cra_type = &crypto_aead_type;
  1927. alg->cra_aead = template->template_aead;
  1928. break;
  1929. }
  1930. t_alg->class1_alg_type = template->class1_alg_type;
  1931. t_alg->class2_alg_type = template->class2_alg_type;
  1932. t_alg->alg_op = template->alg_op;
  1933. t_alg->ctrldev = ctrldev;
  1934. return t_alg;
  1935. }
  1936. static int __init caam_algapi_init(void)
  1937. {
  1938. struct device_node *dev_node;
  1939. struct platform_device *pdev;
  1940. struct device *ctrldev;
  1941. struct caam_drv_private *priv;
  1942. int i = 0, err = 0;
  1943. dev_node = of_find_compatible_node(NULL, NULL, "fsl,sec-v4.0");
  1944. if (!dev_node) {
  1945. dev_node = of_find_compatible_node(NULL, NULL, "fsl,sec4.0");
  1946. if (!dev_node)
  1947. return -ENODEV;
  1948. }
  1949. pdev = of_find_device_by_node(dev_node);
  1950. if (!pdev)
  1951. return -ENODEV;
  1952. ctrldev = &pdev->dev;
  1953. priv = dev_get_drvdata(ctrldev);
  1954. of_node_put(dev_node);
  1955. INIT_LIST_HEAD(&priv->alg_list);
  1956. atomic_set(&priv->tfm_count, -1);
  1957. /* register crypto algorithms the device supports */
  1958. for (i = 0; i < ARRAY_SIZE(driver_algs); i++) {
  1959. /* TODO: check if h/w supports alg */
  1960. struct caam_crypto_alg *t_alg;
  1961. t_alg = caam_alg_alloc(ctrldev, &driver_algs[i]);
  1962. if (IS_ERR(t_alg)) {
  1963. err = PTR_ERR(t_alg);
  1964. dev_warn(ctrldev, "%s alg allocation failed\n",
  1965. driver_algs[i].driver_name);
  1966. continue;
  1967. }
  1968. err = crypto_register_alg(&t_alg->crypto_alg);
  1969. if (err) {
  1970. dev_warn(ctrldev, "%s alg registration failed\n",
  1971. t_alg->crypto_alg.cra_driver_name);
  1972. kfree(t_alg);
  1973. } else
  1974. list_add_tail(&t_alg->entry, &priv->alg_list);
  1975. }
  1976. if (!list_empty(&priv->alg_list))
  1977. dev_info(ctrldev, "%s algorithms registered in /proc/crypto\n",
  1978. (char *)of_get_property(dev_node, "compatible", NULL));
  1979. return err;
  1980. }
  1981. module_init(caam_algapi_init);
  1982. module_exit(caam_algapi_exit);
  1983. MODULE_LICENSE("GPL");
  1984. MODULE_DESCRIPTION("FSL CAAM support for crypto API");
  1985. MODULE_AUTHOR("Freescale Semiconductor - NMG/STC");