caamalg.c 66 KB

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