cc_aead.c 78 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (C) 2012-2018 ARM Limited or its affiliates. */
  3. #include <linux/kernel.h>
  4. #include <linux/module.h>
  5. #include <crypto/algapi.h>
  6. #include <crypto/internal/aead.h>
  7. #include <crypto/authenc.h>
  8. #include <crypto/des.h>
  9. #include <linux/rtnetlink.h>
  10. #include "cc_driver.h"
  11. #include "cc_buffer_mgr.h"
  12. #include "cc_aead.h"
  13. #include "cc_request_mgr.h"
  14. #include "cc_hash.h"
  15. #include "cc_sram_mgr.h"
  16. #define template_aead template_u.aead
  17. #define MAX_AEAD_SETKEY_SEQ 12
  18. #define MAX_AEAD_PROCESS_SEQ 23
  19. #define MAX_HMAC_DIGEST_SIZE (SHA256_DIGEST_SIZE)
  20. #define MAX_HMAC_BLOCK_SIZE (SHA256_BLOCK_SIZE)
  21. #define AES_CCM_RFC4309_NONCE_SIZE 3
  22. #define MAX_NONCE_SIZE CTR_RFC3686_NONCE_SIZE
  23. /* Value of each ICV_CMP byte (of 8) in case of success */
  24. #define ICV_VERIF_OK 0x01
  25. struct cc_aead_handle {
  26. cc_sram_addr_t sram_workspace_addr;
  27. struct list_head aead_list;
  28. };
  29. struct cc_hmac_s {
  30. u8 *padded_authkey;
  31. u8 *ipad_opad; /* IPAD, OPAD*/
  32. dma_addr_t padded_authkey_dma_addr;
  33. dma_addr_t ipad_opad_dma_addr;
  34. };
  35. struct cc_xcbc_s {
  36. u8 *xcbc_keys; /* K1,K2,K3 */
  37. dma_addr_t xcbc_keys_dma_addr;
  38. };
  39. struct cc_aead_ctx {
  40. struct cc_drvdata *drvdata;
  41. u8 ctr_nonce[MAX_NONCE_SIZE]; /* used for ctr3686 iv and aes ccm */
  42. u8 *enckey;
  43. dma_addr_t enckey_dma_addr;
  44. union {
  45. struct cc_hmac_s hmac;
  46. struct cc_xcbc_s xcbc;
  47. } auth_state;
  48. unsigned int enc_keylen;
  49. unsigned int auth_keylen;
  50. unsigned int authsize; /* Actual (reduced?) size of the MAC/ICv */
  51. enum drv_cipher_mode cipher_mode;
  52. enum cc_flow_mode flow_mode;
  53. enum drv_hash_mode auth_mode;
  54. };
  55. static inline bool valid_assoclen(struct aead_request *req)
  56. {
  57. return ((req->assoclen == 16) || (req->assoclen == 20));
  58. }
  59. static void cc_aead_exit(struct crypto_aead *tfm)
  60. {
  61. struct cc_aead_ctx *ctx = crypto_aead_ctx(tfm);
  62. struct device *dev = drvdata_to_dev(ctx->drvdata);
  63. dev_dbg(dev, "Clearing context @%p for %s\n", crypto_aead_ctx(tfm),
  64. crypto_tfm_alg_name(&tfm->base));
  65. /* Unmap enckey buffer */
  66. if (ctx->enckey) {
  67. dma_free_coherent(dev, AES_MAX_KEY_SIZE, ctx->enckey,
  68. ctx->enckey_dma_addr);
  69. dev_dbg(dev, "Freed enckey DMA buffer enckey_dma_addr=%pad\n",
  70. &ctx->enckey_dma_addr);
  71. ctx->enckey_dma_addr = 0;
  72. ctx->enckey = NULL;
  73. }
  74. if (ctx->auth_mode == DRV_HASH_XCBC_MAC) { /* XCBC authetication */
  75. struct cc_xcbc_s *xcbc = &ctx->auth_state.xcbc;
  76. if (xcbc->xcbc_keys) {
  77. dma_free_coherent(dev, CC_AES_128_BIT_KEY_SIZE * 3,
  78. xcbc->xcbc_keys,
  79. xcbc->xcbc_keys_dma_addr);
  80. }
  81. dev_dbg(dev, "Freed xcbc_keys DMA buffer xcbc_keys_dma_addr=%pad\n",
  82. &xcbc->xcbc_keys_dma_addr);
  83. xcbc->xcbc_keys_dma_addr = 0;
  84. xcbc->xcbc_keys = NULL;
  85. } else if (ctx->auth_mode != DRV_HASH_NULL) { /* HMAC auth. */
  86. struct cc_hmac_s *hmac = &ctx->auth_state.hmac;
  87. if (hmac->ipad_opad) {
  88. dma_free_coherent(dev, 2 * MAX_HMAC_DIGEST_SIZE,
  89. hmac->ipad_opad,
  90. hmac->ipad_opad_dma_addr);
  91. dev_dbg(dev, "Freed ipad_opad DMA buffer ipad_opad_dma_addr=%pad\n",
  92. &hmac->ipad_opad_dma_addr);
  93. hmac->ipad_opad_dma_addr = 0;
  94. hmac->ipad_opad = NULL;
  95. }
  96. if (hmac->padded_authkey) {
  97. dma_free_coherent(dev, MAX_HMAC_BLOCK_SIZE,
  98. hmac->padded_authkey,
  99. hmac->padded_authkey_dma_addr);
  100. dev_dbg(dev, "Freed padded_authkey DMA buffer padded_authkey_dma_addr=%pad\n",
  101. &hmac->padded_authkey_dma_addr);
  102. hmac->padded_authkey_dma_addr = 0;
  103. hmac->padded_authkey = NULL;
  104. }
  105. }
  106. }
  107. static int cc_aead_init(struct crypto_aead *tfm)
  108. {
  109. struct aead_alg *alg = crypto_aead_alg(tfm);
  110. struct cc_aead_ctx *ctx = crypto_aead_ctx(tfm);
  111. struct cc_crypto_alg *cc_alg =
  112. container_of(alg, struct cc_crypto_alg, aead_alg);
  113. struct device *dev = drvdata_to_dev(cc_alg->drvdata);
  114. dev_dbg(dev, "Initializing context @%p for %s\n", ctx,
  115. crypto_tfm_alg_name(&tfm->base));
  116. /* Initialize modes in instance */
  117. ctx->cipher_mode = cc_alg->cipher_mode;
  118. ctx->flow_mode = cc_alg->flow_mode;
  119. ctx->auth_mode = cc_alg->auth_mode;
  120. ctx->drvdata = cc_alg->drvdata;
  121. crypto_aead_set_reqsize(tfm, sizeof(struct aead_req_ctx));
  122. /* Allocate key buffer, cache line aligned */
  123. ctx->enckey = dma_alloc_coherent(dev, AES_MAX_KEY_SIZE,
  124. &ctx->enckey_dma_addr, GFP_KERNEL);
  125. if (!ctx->enckey) {
  126. dev_err(dev, "Failed allocating key buffer\n");
  127. goto init_failed;
  128. }
  129. dev_dbg(dev, "Allocated enckey buffer in context ctx->enckey=@%p\n",
  130. ctx->enckey);
  131. /* Set default authlen value */
  132. if (ctx->auth_mode == DRV_HASH_XCBC_MAC) { /* XCBC authetication */
  133. struct cc_xcbc_s *xcbc = &ctx->auth_state.xcbc;
  134. const unsigned int key_size = CC_AES_128_BIT_KEY_SIZE * 3;
  135. /* Allocate dma-coherent buffer for XCBC's K1+K2+K3 */
  136. /* (and temporary for user key - up to 256b) */
  137. xcbc->xcbc_keys = dma_alloc_coherent(dev, key_size,
  138. &xcbc->xcbc_keys_dma_addr,
  139. GFP_KERNEL);
  140. if (!xcbc->xcbc_keys) {
  141. dev_err(dev, "Failed allocating buffer for XCBC keys\n");
  142. goto init_failed;
  143. }
  144. } else if (ctx->auth_mode != DRV_HASH_NULL) { /* HMAC authentication */
  145. struct cc_hmac_s *hmac = &ctx->auth_state.hmac;
  146. const unsigned int digest_size = 2 * MAX_HMAC_DIGEST_SIZE;
  147. dma_addr_t *pkey_dma = &hmac->padded_authkey_dma_addr;
  148. /* Allocate dma-coherent buffer for IPAD + OPAD */
  149. hmac->ipad_opad = dma_alloc_coherent(dev, digest_size,
  150. &hmac->ipad_opad_dma_addr,
  151. GFP_KERNEL);
  152. if (!hmac->ipad_opad) {
  153. dev_err(dev, "Failed allocating IPAD/OPAD buffer\n");
  154. goto init_failed;
  155. }
  156. dev_dbg(dev, "Allocated authkey buffer in context ctx->authkey=@%p\n",
  157. hmac->ipad_opad);
  158. hmac->padded_authkey = dma_alloc_coherent(dev,
  159. MAX_HMAC_BLOCK_SIZE,
  160. pkey_dma,
  161. GFP_KERNEL);
  162. if (!hmac->padded_authkey) {
  163. dev_err(dev, "failed to allocate padded_authkey\n");
  164. goto init_failed;
  165. }
  166. } else {
  167. ctx->auth_state.hmac.ipad_opad = NULL;
  168. ctx->auth_state.hmac.padded_authkey = NULL;
  169. }
  170. return 0;
  171. init_failed:
  172. cc_aead_exit(tfm);
  173. return -ENOMEM;
  174. }
  175. static void cc_aead_complete(struct device *dev, void *cc_req, int err)
  176. {
  177. struct aead_request *areq = (struct aead_request *)cc_req;
  178. struct aead_req_ctx *areq_ctx = aead_request_ctx(areq);
  179. struct crypto_aead *tfm = crypto_aead_reqtfm(cc_req);
  180. struct cc_aead_ctx *ctx = crypto_aead_ctx(tfm);
  181. cc_unmap_aead_request(dev, areq);
  182. /* Restore ordinary iv pointer */
  183. areq->iv = areq_ctx->backup_iv;
  184. if (err)
  185. goto done;
  186. if (areq_ctx->gen_ctx.op_type == DRV_CRYPTO_DIRECTION_DECRYPT) {
  187. if (memcmp(areq_ctx->mac_buf, areq_ctx->icv_virt_addr,
  188. ctx->authsize) != 0) {
  189. dev_dbg(dev, "Payload authentication failure, (auth-size=%d, cipher=%d)\n",
  190. ctx->authsize, ctx->cipher_mode);
  191. /* In case of payload authentication failure, MUST NOT
  192. * revealed the decrypted message --> zero its memory.
  193. */
  194. cc_zero_sgl(areq->dst, areq->cryptlen);
  195. err = -EBADMSG;
  196. }
  197. } else { /*ENCRYPT*/
  198. if (areq_ctx->is_icv_fragmented) {
  199. u32 skip = areq->cryptlen + areq_ctx->dst_offset;
  200. cc_copy_sg_portion(dev, areq_ctx->mac_buf,
  201. areq_ctx->dst_sgl, skip,
  202. (skip + ctx->authsize),
  203. CC_SG_FROM_BUF);
  204. }
  205. /* If an IV was generated, copy it back to the user provided
  206. * buffer.
  207. */
  208. if (areq_ctx->backup_giv) {
  209. if (ctx->cipher_mode == DRV_CIPHER_CTR)
  210. memcpy(areq_ctx->backup_giv, areq_ctx->ctr_iv +
  211. CTR_RFC3686_NONCE_SIZE,
  212. CTR_RFC3686_IV_SIZE);
  213. else if (ctx->cipher_mode == DRV_CIPHER_CCM)
  214. memcpy(areq_ctx->backup_giv, areq_ctx->ctr_iv +
  215. CCM_BLOCK_IV_OFFSET, CCM_BLOCK_IV_SIZE);
  216. }
  217. }
  218. done:
  219. aead_request_complete(areq, err);
  220. }
  221. static unsigned int xcbc_setkey(struct cc_hw_desc *desc,
  222. struct cc_aead_ctx *ctx)
  223. {
  224. /* Load the AES key */
  225. hw_desc_init(&desc[0]);
  226. /* We are using for the source/user key the same buffer
  227. * as for the output keys, * because after this key loading it
  228. * is not needed anymore
  229. */
  230. set_din_type(&desc[0], DMA_DLLI,
  231. ctx->auth_state.xcbc.xcbc_keys_dma_addr, ctx->auth_keylen,
  232. NS_BIT);
  233. set_cipher_mode(&desc[0], DRV_CIPHER_ECB);
  234. set_cipher_config0(&desc[0], DRV_CRYPTO_DIRECTION_ENCRYPT);
  235. set_key_size_aes(&desc[0], ctx->auth_keylen);
  236. set_flow_mode(&desc[0], S_DIN_to_AES);
  237. set_setup_mode(&desc[0], SETUP_LOAD_KEY0);
  238. hw_desc_init(&desc[1]);
  239. set_din_const(&desc[1], 0x01010101, CC_AES_128_BIT_KEY_SIZE);
  240. set_flow_mode(&desc[1], DIN_AES_DOUT);
  241. set_dout_dlli(&desc[1], ctx->auth_state.xcbc.xcbc_keys_dma_addr,
  242. AES_KEYSIZE_128, NS_BIT, 0);
  243. hw_desc_init(&desc[2]);
  244. set_din_const(&desc[2], 0x02020202, CC_AES_128_BIT_KEY_SIZE);
  245. set_flow_mode(&desc[2], DIN_AES_DOUT);
  246. set_dout_dlli(&desc[2], (ctx->auth_state.xcbc.xcbc_keys_dma_addr
  247. + AES_KEYSIZE_128),
  248. AES_KEYSIZE_128, NS_BIT, 0);
  249. hw_desc_init(&desc[3]);
  250. set_din_const(&desc[3], 0x03030303, CC_AES_128_BIT_KEY_SIZE);
  251. set_flow_mode(&desc[3], DIN_AES_DOUT);
  252. set_dout_dlli(&desc[3], (ctx->auth_state.xcbc.xcbc_keys_dma_addr
  253. + 2 * AES_KEYSIZE_128),
  254. AES_KEYSIZE_128, NS_BIT, 0);
  255. return 4;
  256. }
  257. static int hmac_setkey(struct cc_hw_desc *desc, struct cc_aead_ctx *ctx)
  258. {
  259. unsigned int hmac_pad_const[2] = { HMAC_IPAD_CONST, HMAC_OPAD_CONST };
  260. unsigned int digest_ofs = 0;
  261. unsigned int hash_mode = (ctx->auth_mode == DRV_HASH_SHA1) ?
  262. DRV_HASH_HW_SHA1 : DRV_HASH_HW_SHA256;
  263. unsigned int digest_size = (ctx->auth_mode == DRV_HASH_SHA1) ?
  264. CC_SHA1_DIGEST_SIZE : CC_SHA256_DIGEST_SIZE;
  265. struct cc_hmac_s *hmac = &ctx->auth_state.hmac;
  266. unsigned int idx = 0;
  267. int i;
  268. /* calc derived HMAC key */
  269. for (i = 0; i < 2; i++) {
  270. /* Load hash initial state */
  271. hw_desc_init(&desc[idx]);
  272. set_cipher_mode(&desc[idx], hash_mode);
  273. set_din_sram(&desc[idx],
  274. cc_larval_digest_addr(ctx->drvdata,
  275. ctx->auth_mode),
  276. digest_size);
  277. set_flow_mode(&desc[idx], S_DIN_to_HASH);
  278. set_setup_mode(&desc[idx], SETUP_LOAD_STATE0);
  279. idx++;
  280. /* Load the hash current length*/
  281. hw_desc_init(&desc[idx]);
  282. set_cipher_mode(&desc[idx], hash_mode);
  283. set_din_const(&desc[idx], 0, ctx->drvdata->hash_len_sz);
  284. set_flow_mode(&desc[idx], S_DIN_to_HASH);
  285. set_setup_mode(&desc[idx], SETUP_LOAD_KEY0);
  286. idx++;
  287. /* Prepare ipad key */
  288. hw_desc_init(&desc[idx]);
  289. set_xor_val(&desc[idx], hmac_pad_const[i]);
  290. set_cipher_mode(&desc[idx], hash_mode);
  291. set_flow_mode(&desc[idx], S_DIN_to_HASH);
  292. set_setup_mode(&desc[idx], SETUP_LOAD_STATE1);
  293. idx++;
  294. /* Perform HASH update */
  295. hw_desc_init(&desc[idx]);
  296. set_din_type(&desc[idx], DMA_DLLI,
  297. hmac->padded_authkey_dma_addr,
  298. SHA256_BLOCK_SIZE, NS_BIT);
  299. set_cipher_mode(&desc[idx], hash_mode);
  300. set_xor_active(&desc[idx]);
  301. set_flow_mode(&desc[idx], DIN_HASH);
  302. idx++;
  303. /* Get the digset */
  304. hw_desc_init(&desc[idx]);
  305. set_cipher_mode(&desc[idx], hash_mode);
  306. set_dout_dlli(&desc[idx],
  307. (hmac->ipad_opad_dma_addr + digest_ofs),
  308. digest_size, NS_BIT, 0);
  309. set_flow_mode(&desc[idx], S_HASH_to_DOUT);
  310. set_setup_mode(&desc[idx], SETUP_WRITE_STATE0);
  311. set_cipher_config1(&desc[idx], HASH_PADDING_DISABLED);
  312. idx++;
  313. digest_ofs += digest_size;
  314. }
  315. return idx;
  316. }
  317. static int validate_keys_sizes(struct cc_aead_ctx *ctx)
  318. {
  319. struct device *dev = drvdata_to_dev(ctx->drvdata);
  320. dev_dbg(dev, "enc_keylen=%u authkeylen=%u\n",
  321. ctx->enc_keylen, ctx->auth_keylen);
  322. switch (ctx->auth_mode) {
  323. case DRV_HASH_SHA1:
  324. case DRV_HASH_SHA256:
  325. break;
  326. case DRV_HASH_XCBC_MAC:
  327. if (ctx->auth_keylen != AES_KEYSIZE_128 &&
  328. ctx->auth_keylen != AES_KEYSIZE_192 &&
  329. ctx->auth_keylen != AES_KEYSIZE_256)
  330. return -ENOTSUPP;
  331. break;
  332. case DRV_HASH_NULL: /* Not authenc (e.g., CCM) - no auth_key) */
  333. if (ctx->auth_keylen > 0)
  334. return -EINVAL;
  335. break;
  336. default:
  337. dev_err(dev, "Invalid auth_mode=%d\n", ctx->auth_mode);
  338. return -EINVAL;
  339. }
  340. /* Check cipher key size */
  341. if (ctx->flow_mode == S_DIN_to_DES) {
  342. if (ctx->enc_keylen != DES3_EDE_KEY_SIZE) {
  343. dev_err(dev, "Invalid cipher(3DES) key size: %u\n",
  344. ctx->enc_keylen);
  345. return -EINVAL;
  346. }
  347. } else { /* Default assumed to be AES ciphers */
  348. if (ctx->enc_keylen != AES_KEYSIZE_128 &&
  349. ctx->enc_keylen != AES_KEYSIZE_192 &&
  350. ctx->enc_keylen != AES_KEYSIZE_256) {
  351. dev_err(dev, "Invalid cipher(AES) key size: %u\n",
  352. ctx->enc_keylen);
  353. return -EINVAL;
  354. }
  355. }
  356. return 0; /* All tests of keys sizes passed */
  357. }
  358. /* This function prepers the user key so it can pass to the hmac processing
  359. * (copy to intenral buffer or hash in case of key longer than block
  360. */
  361. static int cc_get_plain_hmac_key(struct crypto_aead *tfm, const u8 *authkey,
  362. unsigned int keylen)
  363. {
  364. dma_addr_t key_dma_addr = 0;
  365. struct cc_aead_ctx *ctx = crypto_aead_ctx(tfm);
  366. struct device *dev = drvdata_to_dev(ctx->drvdata);
  367. u32 larval_addr = cc_larval_digest_addr(ctx->drvdata, ctx->auth_mode);
  368. struct cc_crypto_req cc_req = {};
  369. unsigned int blocksize;
  370. unsigned int digestsize;
  371. unsigned int hashmode;
  372. unsigned int idx = 0;
  373. int rc = 0;
  374. u8 *key = NULL;
  375. struct cc_hw_desc desc[MAX_AEAD_SETKEY_SEQ];
  376. dma_addr_t padded_authkey_dma_addr =
  377. ctx->auth_state.hmac.padded_authkey_dma_addr;
  378. switch (ctx->auth_mode) { /* auth_key required and >0 */
  379. case DRV_HASH_SHA1:
  380. blocksize = SHA1_BLOCK_SIZE;
  381. digestsize = SHA1_DIGEST_SIZE;
  382. hashmode = DRV_HASH_HW_SHA1;
  383. break;
  384. case DRV_HASH_SHA256:
  385. default:
  386. blocksize = SHA256_BLOCK_SIZE;
  387. digestsize = SHA256_DIGEST_SIZE;
  388. hashmode = DRV_HASH_HW_SHA256;
  389. }
  390. if (keylen != 0) {
  391. key = kmemdup(authkey, keylen, GFP_KERNEL);
  392. if (!key)
  393. return -ENOMEM;
  394. key_dma_addr = dma_map_single(dev, (void *)key, keylen,
  395. DMA_TO_DEVICE);
  396. if (dma_mapping_error(dev, key_dma_addr)) {
  397. dev_err(dev, "Mapping key va=0x%p len=%u for DMA failed\n",
  398. key, keylen);
  399. kzfree(key);
  400. return -ENOMEM;
  401. }
  402. if (keylen > blocksize) {
  403. /* Load hash initial state */
  404. hw_desc_init(&desc[idx]);
  405. set_cipher_mode(&desc[idx], hashmode);
  406. set_din_sram(&desc[idx], larval_addr, digestsize);
  407. set_flow_mode(&desc[idx], S_DIN_to_HASH);
  408. set_setup_mode(&desc[idx], SETUP_LOAD_STATE0);
  409. idx++;
  410. /* Load the hash current length*/
  411. hw_desc_init(&desc[idx]);
  412. set_cipher_mode(&desc[idx], hashmode);
  413. set_din_const(&desc[idx], 0, ctx->drvdata->hash_len_sz);
  414. set_cipher_config1(&desc[idx], HASH_PADDING_ENABLED);
  415. set_flow_mode(&desc[idx], S_DIN_to_HASH);
  416. set_setup_mode(&desc[idx], SETUP_LOAD_KEY0);
  417. idx++;
  418. hw_desc_init(&desc[idx]);
  419. set_din_type(&desc[idx], DMA_DLLI,
  420. key_dma_addr, keylen, NS_BIT);
  421. set_flow_mode(&desc[idx], DIN_HASH);
  422. idx++;
  423. /* Get hashed key */
  424. hw_desc_init(&desc[idx]);
  425. set_cipher_mode(&desc[idx], hashmode);
  426. set_dout_dlli(&desc[idx], padded_authkey_dma_addr,
  427. digestsize, NS_BIT, 0);
  428. set_flow_mode(&desc[idx], S_HASH_to_DOUT);
  429. set_setup_mode(&desc[idx], SETUP_WRITE_STATE0);
  430. set_cipher_config1(&desc[idx], HASH_PADDING_DISABLED);
  431. set_cipher_config0(&desc[idx],
  432. HASH_DIGEST_RESULT_LITTLE_ENDIAN);
  433. idx++;
  434. hw_desc_init(&desc[idx]);
  435. set_din_const(&desc[idx], 0, (blocksize - digestsize));
  436. set_flow_mode(&desc[idx], BYPASS);
  437. set_dout_dlli(&desc[idx], (padded_authkey_dma_addr +
  438. digestsize), (blocksize - digestsize),
  439. NS_BIT, 0);
  440. idx++;
  441. } else {
  442. hw_desc_init(&desc[idx]);
  443. set_din_type(&desc[idx], DMA_DLLI, key_dma_addr,
  444. keylen, NS_BIT);
  445. set_flow_mode(&desc[idx], BYPASS);
  446. set_dout_dlli(&desc[idx], padded_authkey_dma_addr,
  447. keylen, NS_BIT, 0);
  448. idx++;
  449. if ((blocksize - keylen) != 0) {
  450. hw_desc_init(&desc[idx]);
  451. set_din_const(&desc[idx], 0,
  452. (blocksize - keylen));
  453. set_flow_mode(&desc[idx], BYPASS);
  454. set_dout_dlli(&desc[idx],
  455. (padded_authkey_dma_addr +
  456. keylen),
  457. (blocksize - keylen), NS_BIT, 0);
  458. idx++;
  459. }
  460. }
  461. } else {
  462. hw_desc_init(&desc[idx]);
  463. set_din_const(&desc[idx], 0, (blocksize - keylen));
  464. set_flow_mode(&desc[idx], BYPASS);
  465. set_dout_dlli(&desc[idx], padded_authkey_dma_addr,
  466. blocksize, NS_BIT, 0);
  467. idx++;
  468. }
  469. rc = cc_send_sync_request(ctx->drvdata, &cc_req, desc, idx);
  470. if (rc)
  471. dev_err(dev, "send_request() failed (rc=%d)\n", rc);
  472. if (key_dma_addr)
  473. dma_unmap_single(dev, key_dma_addr, keylen, DMA_TO_DEVICE);
  474. kzfree(key);
  475. return rc;
  476. }
  477. static int cc_aead_setkey(struct crypto_aead *tfm, const u8 *key,
  478. unsigned int keylen)
  479. {
  480. struct cc_aead_ctx *ctx = crypto_aead_ctx(tfm);
  481. struct cc_crypto_req cc_req = {};
  482. struct cc_hw_desc desc[MAX_AEAD_SETKEY_SEQ];
  483. unsigned int seq_len = 0;
  484. struct device *dev = drvdata_to_dev(ctx->drvdata);
  485. const u8 *enckey, *authkey;
  486. int rc;
  487. dev_dbg(dev, "Setting key in context @%p for %s. key=%p keylen=%u\n",
  488. ctx, crypto_tfm_alg_name(crypto_aead_tfm(tfm)), key, keylen);
  489. /* STAT_PHASE_0: Init and sanity checks */
  490. if (ctx->auth_mode != DRV_HASH_NULL) { /* authenc() alg. */
  491. struct crypto_authenc_keys keys;
  492. rc = crypto_authenc_extractkeys(&keys, key, keylen);
  493. if (rc)
  494. goto badkey;
  495. enckey = keys.enckey;
  496. authkey = keys.authkey;
  497. ctx->enc_keylen = keys.enckeylen;
  498. ctx->auth_keylen = keys.authkeylen;
  499. if (ctx->cipher_mode == DRV_CIPHER_CTR) {
  500. /* the nonce is stored in bytes at end of key */
  501. rc = -EINVAL;
  502. if (ctx->enc_keylen <
  503. (AES_MIN_KEY_SIZE + CTR_RFC3686_NONCE_SIZE))
  504. goto badkey;
  505. /* Copy nonce from last 4 bytes in CTR key to
  506. * first 4 bytes in CTR IV
  507. */
  508. memcpy(ctx->ctr_nonce, enckey + ctx->enc_keylen -
  509. CTR_RFC3686_NONCE_SIZE, CTR_RFC3686_NONCE_SIZE);
  510. /* Set CTR key size */
  511. ctx->enc_keylen -= CTR_RFC3686_NONCE_SIZE;
  512. }
  513. } else { /* non-authenc - has just one key */
  514. enckey = key;
  515. authkey = NULL;
  516. ctx->enc_keylen = keylen;
  517. ctx->auth_keylen = 0;
  518. }
  519. rc = validate_keys_sizes(ctx);
  520. if (rc)
  521. goto badkey;
  522. /* STAT_PHASE_1: Copy key to ctx */
  523. /* Get key material */
  524. memcpy(ctx->enckey, enckey, ctx->enc_keylen);
  525. if (ctx->enc_keylen == 24)
  526. memset(ctx->enckey + 24, 0, CC_AES_KEY_SIZE_MAX - 24);
  527. if (ctx->auth_mode == DRV_HASH_XCBC_MAC) {
  528. memcpy(ctx->auth_state.xcbc.xcbc_keys, authkey,
  529. ctx->auth_keylen);
  530. } else if (ctx->auth_mode != DRV_HASH_NULL) { /* HMAC */
  531. rc = cc_get_plain_hmac_key(tfm, authkey, ctx->auth_keylen);
  532. if (rc)
  533. goto badkey;
  534. }
  535. /* STAT_PHASE_2: Create sequence */
  536. switch (ctx->auth_mode) {
  537. case DRV_HASH_SHA1:
  538. case DRV_HASH_SHA256:
  539. seq_len = hmac_setkey(desc, ctx);
  540. break;
  541. case DRV_HASH_XCBC_MAC:
  542. seq_len = xcbc_setkey(desc, ctx);
  543. break;
  544. case DRV_HASH_NULL: /* non-authenc modes, e.g., CCM */
  545. break; /* No auth. key setup */
  546. default:
  547. dev_err(dev, "Unsupported authenc (%d)\n", ctx->auth_mode);
  548. rc = -ENOTSUPP;
  549. goto badkey;
  550. }
  551. /* STAT_PHASE_3: Submit sequence to HW */
  552. if (seq_len > 0) { /* For CCM there is no sequence to setup the key */
  553. rc = cc_send_sync_request(ctx->drvdata, &cc_req, desc, seq_len);
  554. if (rc) {
  555. dev_err(dev, "send_request() failed (rc=%d)\n", rc);
  556. goto setkey_error;
  557. }
  558. }
  559. /* Update STAT_PHASE_3 */
  560. return rc;
  561. badkey:
  562. crypto_aead_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
  563. setkey_error:
  564. return rc;
  565. }
  566. static int cc_rfc4309_ccm_setkey(struct crypto_aead *tfm, const u8 *key,
  567. unsigned int keylen)
  568. {
  569. struct cc_aead_ctx *ctx = crypto_aead_ctx(tfm);
  570. if (keylen < 3)
  571. return -EINVAL;
  572. keylen -= 3;
  573. memcpy(ctx->ctr_nonce, key + keylen, 3);
  574. return cc_aead_setkey(tfm, key, keylen);
  575. }
  576. static int cc_aead_setauthsize(struct crypto_aead *authenc,
  577. unsigned int authsize)
  578. {
  579. struct cc_aead_ctx *ctx = crypto_aead_ctx(authenc);
  580. struct device *dev = drvdata_to_dev(ctx->drvdata);
  581. /* Unsupported auth. sizes */
  582. if (authsize == 0 ||
  583. authsize > crypto_aead_maxauthsize(authenc)) {
  584. return -ENOTSUPP;
  585. }
  586. ctx->authsize = authsize;
  587. dev_dbg(dev, "authlen=%d\n", ctx->authsize);
  588. return 0;
  589. }
  590. static int cc_rfc4309_ccm_setauthsize(struct crypto_aead *authenc,
  591. unsigned int authsize)
  592. {
  593. switch (authsize) {
  594. case 8:
  595. case 12:
  596. case 16:
  597. break;
  598. default:
  599. return -EINVAL;
  600. }
  601. return cc_aead_setauthsize(authenc, authsize);
  602. }
  603. static int cc_ccm_setauthsize(struct crypto_aead *authenc,
  604. unsigned int authsize)
  605. {
  606. switch (authsize) {
  607. case 4:
  608. case 6:
  609. case 8:
  610. case 10:
  611. case 12:
  612. case 14:
  613. case 16:
  614. break;
  615. default:
  616. return -EINVAL;
  617. }
  618. return cc_aead_setauthsize(authenc, authsize);
  619. }
  620. static void cc_set_assoc_desc(struct aead_request *areq, unsigned int flow_mode,
  621. struct cc_hw_desc desc[], unsigned int *seq_size)
  622. {
  623. struct crypto_aead *tfm = crypto_aead_reqtfm(areq);
  624. struct cc_aead_ctx *ctx = crypto_aead_ctx(tfm);
  625. struct aead_req_ctx *areq_ctx = aead_request_ctx(areq);
  626. enum cc_req_dma_buf_type assoc_dma_type = areq_ctx->assoc_buff_type;
  627. unsigned int idx = *seq_size;
  628. struct device *dev = drvdata_to_dev(ctx->drvdata);
  629. switch (assoc_dma_type) {
  630. case CC_DMA_BUF_DLLI:
  631. dev_dbg(dev, "ASSOC buffer type DLLI\n");
  632. hw_desc_init(&desc[idx]);
  633. set_din_type(&desc[idx], DMA_DLLI, sg_dma_address(areq->src),
  634. areq_ctx->assoclen, NS_BIT);
  635. set_flow_mode(&desc[idx], flow_mode);
  636. if (ctx->auth_mode == DRV_HASH_XCBC_MAC &&
  637. areq_ctx->cryptlen > 0)
  638. set_din_not_last_indication(&desc[idx]);
  639. break;
  640. case CC_DMA_BUF_MLLI:
  641. dev_dbg(dev, "ASSOC buffer type MLLI\n");
  642. hw_desc_init(&desc[idx]);
  643. set_din_type(&desc[idx], DMA_MLLI, areq_ctx->assoc.sram_addr,
  644. areq_ctx->assoc.mlli_nents, NS_BIT);
  645. set_flow_mode(&desc[idx], flow_mode);
  646. if (ctx->auth_mode == DRV_HASH_XCBC_MAC &&
  647. areq_ctx->cryptlen > 0)
  648. set_din_not_last_indication(&desc[idx]);
  649. break;
  650. case CC_DMA_BUF_NULL:
  651. default:
  652. dev_err(dev, "Invalid ASSOC buffer type\n");
  653. }
  654. *seq_size = (++idx);
  655. }
  656. static void cc_proc_authen_desc(struct aead_request *areq,
  657. unsigned int flow_mode,
  658. struct cc_hw_desc desc[],
  659. unsigned int *seq_size, int direct)
  660. {
  661. struct aead_req_ctx *areq_ctx = aead_request_ctx(areq);
  662. enum cc_req_dma_buf_type data_dma_type = areq_ctx->data_buff_type;
  663. unsigned int idx = *seq_size;
  664. struct crypto_aead *tfm = crypto_aead_reqtfm(areq);
  665. struct cc_aead_ctx *ctx = crypto_aead_ctx(tfm);
  666. struct device *dev = drvdata_to_dev(ctx->drvdata);
  667. switch (data_dma_type) {
  668. case CC_DMA_BUF_DLLI:
  669. {
  670. struct scatterlist *cipher =
  671. (direct == DRV_CRYPTO_DIRECTION_ENCRYPT) ?
  672. areq_ctx->dst_sgl : areq_ctx->src_sgl;
  673. unsigned int offset =
  674. (direct == DRV_CRYPTO_DIRECTION_ENCRYPT) ?
  675. areq_ctx->dst_offset : areq_ctx->src_offset;
  676. dev_dbg(dev, "AUTHENC: SRC/DST buffer type DLLI\n");
  677. hw_desc_init(&desc[idx]);
  678. set_din_type(&desc[idx], DMA_DLLI,
  679. (sg_dma_address(cipher) + offset),
  680. areq_ctx->cryptlen, NS_BIT);
  681. set_flow_mode(&desc[idx], flow_mode);
  682. break;
  683. }
  684. case CC_DMA_BUF_MLLI:
  685. {
  686. /* DOUBLE-PASS flow (as default)
  687. * assoc. + iv + data -compact in one table
  688. * if assoclen is ZERO only IV perform
  689. */
  690. cc_sram_addr_t mlli_addr = areq_ctx->assoc.sram_addr;
  691. u32 mlli_nents = areq_ctx->assoc.mlli_nents;
  692. if (areq_ctx->is_single_pass) {
  693. if (direct == DRV_CRYPTO_DIRECTION_ENCRYPT) {
  694. mlli_addr = areq_ctx->dst.sram_addr;
  695. mlli_nents = areq_ctx->dst.mlli_nents;
  696. } else {
  697. mlli_addr = areq_ctx->src.sram_addr;
  698. mlli_nents = areq_ctx->src.mlli_nents;
  699. }
  700. }
  701. dev_dbg(dev, "AUTHENC: SRC/DST buffer type MLLI\n");
  702. hw_desc_init(&desc[idx]);
  703. set_din_type(&desc[idx], DMA_MLLI, mlli_addr, mlli_nents,
  704. NS_BIT);
  705. set_flow_mode(&desc[idx], flow_mode);
  706. break;
  707. }
  708. case CC_DMA_BUF_NULL:
  709. default:
  710. dev_err(dev, "AUTHENC: Invalid SRC/DST buffer type\n");
  711. }
  712. *seq_size = (++idx);
  713. }
  714. static void cc_proc_cipher_desc(struct aead_request *areq,
  715. unsigned int flow_mode,
  716. struct cc_hw_desc desc[],
  717. unsigned int *seq_size)
  718. {
  719. unsigned int idx = *seq_size;
  720. struct aead_req_ctx *areq_ctx = aead_request_ctx(areq);
  721. enum cc_req_dma_buf_type data_dma_type = areq_ctx->data_buff_type;
  722. struct crypto_aead *tfm = crypto_aead_reqtfm(areq);
  723. struct cc_aead_ctx *ctx = crypto_aead_ctx(tfm);
  724. struct device *dev = drvdata_to_dev(ctx->drvdata);
  725. if (areq_ctx->cryptlen == 0)
  726. return; /*null processing*/
  727. switch (data_dma_type) {
  728. case CC_DMA_BUF_DLLI:
  729. dev_dbg(dev, "CIPHER: SRC/DST buffer type DLLI\n");
  730. hw_desc_init(&desc[idx]);
  731. set_din_type(&desc[idx], DMA_DLLI,
  732. (sg_dma_address(areq_ctx->src_sgl) +
  733. areq_ctx->src_offset), areq_ctx->cryptlen,
  734. NS_BIT);
  735. set_dout_dlli(&desc[idx],
  736. (sg_dma_address(areq_ctx->dst_sgl) +
  737. areq_ctx->dst_offset),
  738. areq_ctx->cryptlen, NS_BIT, 0);
  739. set_flow_mode(&desc[idx], flow_mode);
  740. break;
  741. case CC_DMA_BUF_MLLI:
  742. dev_dbg(dev, "CIPHER: SRC/DST buffer type MLLI\n");
  743. hw_desc_init(&desc[idx]);
  744. set_din_type(&desc[idx], DMA_MLLI, areq_ctx->src.sram_addr,
  745. areq_ctx->src.mlli_nents, NS_BIT);
  746. set_dout_mlli(&desc[idx], areq_ctx->dst.sram_addr,
  747. areq_ctx->dst.mlli_nents, NS_BIT, 0);
  748. set_flow_mode(&desc[idx], flow_mode);
  749. break;
  750. case CC_DMA_BUF_NULL:
  751. default:
  752. dev_err(dev, "CIPHER: Invalid SRC/DST buffer type\n");
  753. }
  754. *seq_size = (++idx);
  755. }
  756. static void cc_proc_digest_desc(struct aead_request *req,
  757. struct cc_hw_desc desc[],
  758. unsigned int *seq_size)
  759. {
  760. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  761. struct cc_aead_ctx *ctx = crypto_aead_ctx(tfm);
  762. struct aead_req_ctx *req_ctx = aead_request_ctx(req);
  763. unsigned int idx = *seq_size;
  764. unsigned int hash_mode = (ctx->auth_mode == DRV_HASH_SHA1) ?
  765. DRV_HASH_HW_SHA1 : DRV_HASH_HW_SHA256;
  766. int direct = req_ctx->gen_ctx.op_type;
  767. /* Get final ICV result */
  768. if (direct == DRV_CRYPTO_DIRECTION_ENCRYPT) {
  769. hw_desc_init(&desc[idx]);
  770. set_flow_mode(&desc[idx], S_HASH_to_DOUT);
  771. set_setup_mode(&desc[idx], SETUP_WRITE_STATE0);
  772. set_dout_dlli(&desc[idx], req_ctx->icv_dma_addr, ctx->authsize,
  773. NS_BIT, 1);
  774. set_queue_last_ind(ctx->drvdata, &desc[idx]);
  775. if (ctx->auth_mode == DRV_HASH_XCBC_MAC) {
  776. set_aes_not_hash_mode(&desc[idx]);
  777. set_cipher_mode(&desc[idx], DRV_CIPHER_XCBC_MAC);
  778. } else {
  779. set_cipher_config0(&desc[idx],
  780. HASH_DIGEST_RESULT_LITTLE_ENDIAN);
  781. set_cipher_mode(&desc[idx], hash_mode);
  782. }
  783. } else { /*Decrypt*/
  784. /* Get ICV out from hardware */
  785. hw_desc_init(&desc[idx]);
  786. set_setup_mode(&desc[idx], SETUP_WRITE_STATE0);
  787. set_flow_mode(&desc[idx], S_HASH_to_DOUT);
  788. set_dout_dlli(&desc[idx], req_ctx->mac_buf_dma_addr,
  789. ctx->authsize, NS_BIT, 1);
  790. set_queue_last_ind(ctx->drvdata, &desc[idx]);
  791. set_cipher_config0(&desc[idx],
  792. HASH_DIGEST_RESULT_LITTLE_ENDIAN);
  793. set_cipher_config1(&desc[idx], HASH_PADDING_DISABLED);
  794. if (ctx->auth_mode == DRV_HASH_XCBC_MAC) {
  795. set_cipher_mode(&desc[idx], DRV_CIPHER_XCBC_MAC);
  796. set_aes_not_hash_mode(&desc[idx]);
  797. } else {
  798. set_cipher_mode(&desc[idx], hash_mode);
  799. }
  800. }
  801. *seq_size = (++idx);
  802. }
  803. static void cc_set_cipher_desc(struct aead_request *req,
  804. struct cc_hw_desc desc[],
  805. unsigned int *seq_size)
  806. {
  807. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  808. struct cc_aead_ctx *ctx = crypto_aead_ctx(tfm);
  809. struct aead_req_ctx *req_ctx = aead_request_ctx(req);
  810. unsigned int hw_iv_size = req_ctx->hw_iv_size;
  811. unsigned int idx = *seq_size;
  812. int direct = req_ctx->gen_ctx.op_type;
  813. /* Setup cipher state */
  814. hw_desc_init(&desc[idx]);
  815. set_cipher_config0(&desc[idx], direct);
  816. set_flow_mode(&desc[idx], ctx->flow_mode);
  817. set_din_type(&desc[idx], DMA_DLLI, req_ctx->gen_ctx.iv_dma_addr,
  818. hw_iv_size, NS_BIT);
  819. if (ctx->cipher_mode == DRV_CIPHER_CTR)
  820. set_setup_mode(&desc[idx], SETUP_LOAD_STATE1);
  821. else
  822. set_setup_mode(&desc[idx], SETUP_LOAD_STATE0);
  823. set_cipher_mode(&desc[idx], ctx->cipher_mode);
  824. idx++;
  825. /* Setup enc. key */
  826. hw_desc_init(&desc[idx]);
  827. set_cipher_config0(&desc[idx], direct);
  828. set_setup_mode(&desc[idx], SETUP_LOAD_KEY0);
  829. set_flow_mode(&desc[idx], ctx->flow_mode);
  830. if (ctx->flow_mode == S_DIN_to_AES) {
  831. set_din_type(&desc[idx], DMA_DLLI, ctx->enckey_dma_addr,
  832. ((ctx->enc_keylen == 24) ? CC_AES_KEY_SIZE_MAX :
  833. ctx->enc_keylen), NS_BIT);
  834. set_key_size_aes(&desc[idx], ctx->enc_keylen);
  835. } else {
  836. set_din_type(&desc[idx], DMA_DLLI, ctx->enckey_dma_addr,
  837. ctx->enc_keylen, NS_BIT);
  838. set_key_size_des(&desc[idx], ctx->enc_keylen);
  839. }
  840. set_cipher_mode(&desc[idx], ctx->cipher_mode);
  841. idx++;
  842. *seq_size = idx;
  843. }
  844. static void cc_proc_cipher(struct aead_request *req, struct cc_hw_desc desc[],
  845. unsigned int *seq_size, unsigned int data_flow_mode)
  846. {
  847. struct aead_req_ctx *req_ctx = aead_request_ctx(req);
  848. int direct = req_ctx->gen_ctx.op_type;
  849. unsigned int idx = *seq_size;
  850. if (req_ctx->cryptlen == 0)
  851. return; /*null processing*/
  852. cc_set_cipher_desc(req, desc, &idx);
  853. cc_proc_cipher_desc(req, data_flow_mode, desc, &idx);
  854. if (direct == DRV_CRYPTO_DIRECTION_ENCRYPT) {
  855. /* We must wait for DMA to write all cipher */
  856. hw_desc_init(&desc[idx]);
  857. set_din_no_dma(&desc[idx], 0, 0xfffff0);
  858. set_dout_no_dma(&desc[idx], 0, 0, 1);
  859. idx++;
  860. }
  861. *seq_size = idx;
  862. }
  863. static void cc_set_hmac_desc(struct aead_request *req, struct cc_hw_desc desc[],
  864. unsigned int *seq_size)
  865. {
  866. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  867. struct cc_aead_ctx *ctx = crypto_aead_ctx(tfm);
  868. unsigned int hash_mode = (ctx->auth_mode == DRV_HASH_SHA1) ?
  869. DRV_HASH_HW_SHA1 : DRV_HASH_HW_SHA256;
  870. unsigned int digest_size = (ctx->auth_mode == DRV_HASH_SHA1) ?
  871. CC_SHA1_DIGEST_SIZE : CC_SHA256_DIGEST_SIZE;
  872. unsigned int idx = *seq_size;
  873. /* Loading hash ipad xor key state */
  874. hw_desc_init(&desc[idx]);
  875. set_cipher_mode(&desc[idx], hash_mode);
  876. set_din_type(&desc[idx], DMA_DLLI,
  877. ctx->auth_state.hmac.ipad_opad_dma_addr, digest_size,
  878. NS_BIT);
  879. set_flow_mode(&desc[idx], S_DIN_to_HASH);
  880. set_setup_mode(&desc[idx], SETUP_LOAD_STATE0);
  881. idx++;
  882. /* Load init. digest len (64 bytes) */
  883. hw_desc_init(&desc[idx]);
  884. set_cipher_mode(&desc[idx], hash_mode);
  885. set_din_sram(&desc[idx], cc_digest_len_addr(ctx->drvdata, hash_mode),
  886. ctx->drvdata->hash_len_sz);
  887. set_flow_mode(&desc[idx], S_DIN_to_HASH);
  888. set_setup_mode(&desc[idx], SETUP_LOAD_KEY0);
  889. idx++;
  890. *seq_size = idx;
  891. }
  892. static void cc_set_xcbc_desc(struct aead_request *req, struct cc_hw_desc desc[],
  893. unsigned int *seq_size)
  894. {
  895. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  896. struct cc_aead_ctx *ctx = crypto_aead_ctx(tfm);
  897. unsigned int idx = *seq_size;
  898. /* Loading MAC state */
  899. hw_desc_init(&desc[idx]);
  900. set_din_const(&desc[idx], 0, CC_AES_BLOCK_SIZE);
  901. set_setup_mode(&desc[idx], SETUP_LOAD_STATE0);
  902. set_cipher_mode(&desc[idx], DRV_CIPHER_XCBC_MAC);
  903. set_cipher_config0(&desc[idx], DESC_DIRECTION_ENCRYPT_ENCRYPT);
  904. set_key_size_aes(&desc[idx], CC_AES_128_BIT_KEY_SIZE);
  905. set_flow_mode(&desc[idx], S_DIN_to_HASH);
  906. set_aes_not_hash_mode(&desc[idx]);
  907. idx++;
  908. /* Setup XCBC MAC K1 */
  909. hw_desc_init(&desc[idx]);
  910. set_din_type(&desc[idx], DMA_DLLI,
  911. ctx->auth_state.xcbc.xcbc_keys_dma_addr,
  912. AES_KEYSIZE_128, NS_BIT);
  913. set_setup_mode(&desc[idx], SETUP_LOAD_KEY0);
  914. set_cipher_mode(&desc[idx], DRV_CIPHER_XCBC_MAC);
  915. set_cipher_config0(&desc[idx], DESC_DIRECTION_ENCRYPT_ENCRYPT);
  916. set_key_size_aes(&desc[idx], CC_AES_128_BIT_KEY_SIZE);
  917. set_flow_mode(&desc[idx], S_DIN_to_HASH);
  918. set_aes_not_hash_mode(&desc[idx]);
  919. idx++;
  920. /* Setup XCBC MAC K2 */
  921. hw_desc_init(&desc[idx]);
  922. set_din_type(&desc[idx], DMA_DLLI,
  923. (ctx->auth_state.xcbc.xcbc_keys_dma_addr +
  924. AES_KEYSIZE_128), AES_KEYSIZE_128, NS_BIT);
  925. set_setup_mode(&desc[idx], SETUP_LOAD_STATE1);
  926. set_cipher_mode(&desc[idx], DRV_CIPHER_XCBC_MAC);
  927. set_cipher_config0(&desc[idx], DESC_DIRECTION_ENCRYPT_ENCRYPT);
  928. set_key_size_aes(&desc[idx], CC_AES_128_BIT_KEY_SIZE);
  929. set_flow_mode(&desc[idx], S_DIN_to_HASH);
  930. set_aes_not_hash_mode(&desc[idx]);
  931. idx++;
  932. /* Setup XCBC MAC K3 */
  933. hw_desc_init(&desc[idx]);
  934. set_din_type(&desc[idx], DMA_DLLI,
  935. (ctx->auth_state.xcbc.xcbc_keys_dma_addr +
  936. 2 * AES_KEYSIZE_128), AES_KEYSIZE_128, NS_BIT);
  937. set_setup_mode(&desc[idx], SETUP_LOAD_STATE2);
  938. set_cipher_mode(&desc[idx], DRV_CIPHER_XCBC_MAC);
  939. set_cipher_config0(&desc[idx], DESC_DIRECTION_ENCRYPT_ENCRYPT);
  940. set_key_size_aes(&desc[idx], CC_AES_128_BIT_KEY_SIZE);
  941. set_flow_mode(&desc[idx], S_DIN_to_HASH);
  942. set_aes_not_hash_mode(&desc[idx]);
  943. idx++;
  944. *seq_size = idx;
  945. }
  946. static void cc_proc_header_desc(struct aead_request *req,
  947. struct cc_hw_desc desc[],
  948. unsigned int *seq_size)
  949. {
  950. struct aead_req_ctx *areq_ctx = aead_request_ctx(req);
  951. unsigned int idx = *seq_size;
  952. /* Hash associated data */
  953. if (areq_ctx->assoclen > 0)
  954. cc_set_assoc_desc(req, DIN_HASH, desc, &idx);
  955. /* Hash IV */
  956. *seq_size = idx;
  957. }
  958. static void cc_proc_scheme_desc(struct aead_request *req,
  959. struct cc_hw_desc desc[],
  960. unsigned int *seq_size)
  961. {
  962. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  963. struct cc_aead_ctx *ctx = crypto_aead_ctx(tfm);
  964. struct cc_aead_handle *aead_handle = ctx->drvdata->aead_handle;
  965. unsigned int hash_mode = (ctx->auth_mode == DRV_HASH_SHA1) ?
  966. DRV_HASH_HW_SHA1 : DRV_HASH_HW_SHA256;
  967. unsigned int digest_size = (ctx->auth_mode == DRV_HASH_SHA1) ?
  968. CC_SHA1_DIGEST_SIZE : CC_SHA256_DIGEST_SIZE;
  969. unsigned int idx = *seq_size;
  970. hw_desc_init(&desc[idx]);
  971. set_cipher_mode(&desc[idx], hash_mode);
  972. set_dout_sram(&desc[idx], aead_handle->sram_workspace_addr,
  973. ctx->drvdata->hash_len_sz);
  974. set_flow_mode(&desc[idx], S_HASH_to_DOUT);
  975. set_setup_mode(&desc[idx], SETUP_WRITE_STATE1);
  976. set_cipher_do(&desc[idx], DO_PAD);
  977. idx++;
  978. /* Get final ICV result */
  979. hw_desc_init(&desc[idx]);
  980. set_dout_sram(&desc[idx], aead_handle->sram_workspace_addr,
  981. digest_size);
  982. set_flow_mode(&desc[idx], S_HASH_to_DOUT);
  983. set_setup_mode(&desc[idx], SETUP_WRITE_STATE0);
  984. set_cipher_config0(&desc[idx], HASH_DIGEST_RESULT_LITTLE_ENDIAN);
  985. set_cipher_mode(&desc[idx], hash_mode);
  986. idx++;
  987. /* Loading hash opad xor key state */
  988. hw_desc_init(&desc[idx]);
  989. set_cipher_mode(&desc[idx], hash_mode);
  990. set_din_type(&desc[idx], DMA_DLLI,
  991. (ctx->auth_state.hmac.ipad_opad_dma_addr + digest_size),
  992. digest_size, NS_BIT);
  993. set_flow_mode(&desc[idx], S_DIN_to_HASH);
  994. set_setup_mode(&desc[idx], SETUP_LOAD_STATE0);
  995. idx++;
  996. /* Load init. digest len (64 bytes) */
  997. hw_desc_init(&desc[idx]);
  998. set_cipher_mode(&desc[idx], hash_mode);
  999. set_din_sram(&desc[idx], cc_digest_len_addr(ctx->drvdata, hash_mode),
  1000. ctx->drvdata->hash_len_sz);
  1001. set_cipher_config1(&desc[idx], HASH_PADDING_ENABLED);
  1002. set_flow_mode(&desc[idx], S_DIN_to_HASH);
  1003. set_setup_mode(&desc[idx], SETUP_LOAD_KEY0);
  1004. idx++;
  1005. /* Perform HASH update */
  1006. hw_desc_init(&desc[idx]);
  1007. set_din_sram(&desc[idx], aead_handle->sram_workspace_addr,
  1008. digest_size);
  1009. set_flow_mode(&desc[idx], DIN_HASH);
  1010. idx++;
  1011. *seq_size = idx;
  1012. }
  1013. static void cc_mlli_to_sram(struct aead_request *req,
  1014. struct cc_hw_desc desc[], unsigned int *seq_size)
  1015. {
  1016. struct aead_req_ctx *req_ctx = aead_request_ctx(req);
  1017. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  1018. struct cc_aead_ctx *ctx = crypto_aead_ctx(tfm);
  1019. struct device *dev = drvdata_to_dev(ctx->drvdata);
  1020. if (req_ctx->assoc_buff_type == CC_DMA_BUF_MLLI ||
  1021. req_ctx->data_buff_type == CC_DMA_BUF_MLLI ||
  1022. !req_ctx->is_single_pass) {
  1023. dev_dbg(dev, "Copy-to-sram: mlli_dma=%08x, mlli_size=%u\n",
  1024. (unsigned int)ctx->drvdata->mlli_sram_addr,
  1025. req_ctx->mlli_params.mlli_len);
  1026. /* Copy MLLI table host-to-sram */
  1027. hw_desc_init(&desc[*seq_size]);
  1028. set_din_type(&desc[*seq_size], DMA_DLLI,
  1029. req_ctx->mlli_params.mlli_dma_addr,
  1030. req_ctx->mlli_params.mlli_len, NS_BIT);
  1031. set_dout_sram(&desc[*seq_size],
  1032. ctx->drvdata->mlli_sram_addr,
  1033. req_ctx->mlli_params.mlli_len);
  1034. set_flow_mode(&desc[*seq_size], BYPASS);
  1035. (*seq_size)++;
  1036. }
  1037. }
  1038. static enum cc_flow_mode cc_get_data_flow(enum drv_crypto_direction direct,
  1039. enum cc_flow_mode setup_flow_mode,
  1040. bool is_single_pass)
  1041. {
  1042. enum cc_flow_mode data_flow_mode;
  1043. if (direct == DRV_CRYPTO_DIRECTION_ENCRYPT) {
  1044. if (setup_flow_mode == S_DIN_to_AES)
  1045. data_flow_mode = is_single_pass ?
  1046. AES_to_HASH_and_DOUT : DIN_AES_DOUT;
  1047. else
  1048. data_flow_mode = is_single_pass ?
  1049. DES_to_HASH_and_DOUT : DIN_DES_DOUT;
  1050. } else { /* Decrypt */
  1051. if (setup_flow_mode == S_DIN_to_AES)
  1052. data_flow_mode = is_single_pass ?
  1053. AES_and_HASH : DIN_AES_DOUT;
  1054. else
  1055. data_flow_mode = is_single_pass ?
  1056. DES_and_HASH : DIN_DES_DOUT;
  1057. }
  1058. return data_flow_mode;
  1059. }
  1060. static void cc_hmac_authenc(struct aead_request *req, struct cc_hw_desc desc[],
  1061. unsigned int *seq_size)
  1062. {
  1063. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  1064. struct cc_aead_ctx *ctx = crypto_aead_ctx(tfm);
  1065. struct aead_req_ctx *req_ctx = aead_request_ctx(req);
  1066. int direct = req_ctx->gen_ctx.op_type;
  1067. unsigned int data_flow_mode =
  1068. cc_get_data_flow(direct, ctx->flow_mode,
  1069. req_ctx->is_single_pass);
  1070. if (req_ctx->is_single_pass) {
  1071. /**
  1072. * Single-pass flow
  1073. */
  1074. cc_set_hmac_desc(req, desc, seq_size);
  1075. cc_set_cipher_desc(req, desc, seq_size);
  1076. cc_proc_header_desc(req, desc, seq_size);
  1077. cc_proc_cipher_desc(req, data_flow_mode, desc, seq_size);
  1078. cc_proc_scheme_desc(req, desc, seq_size);
  1079. cc_proc_digest_desc(req, desc, seq_size);
  1080. return;
  1081. }
  1082. /**
  1083. * Double-pass flow
  1084. * Fallback for unsupported single-pass modes,
  1085. * i.e. using assoc. data of non-word-multiple
  1086. */
  1087. if (direct == DRV_CRYPTO_DIRECTION_ENCRYPT) {
  1088. /* encrypt first.. */
  1089. cc_proc_cipher(req, desc, seq_size, data_flow_mode);
  1090. /* authenc after..*/
  1091. cc_set_hmac_desc(req, desc, seq_size);
  1092. cc_proc_authen_desc(req, DIN_HASH, desc, seq_size, direct);
  1093. cc_proc_scheme_desc(req, desc, seq_size);
  1094. cc_proc_digest_desc(req, desc, seq_size);
  1095. } else { /*DECRYPT*/
  1096. /* authenc first..*/
  1097. cc_set_hmac_desc(req, desc, seq_size);
  1098. cc_proc_authen_desc(req, DIN_HASH, desc, seq_size, direct);
  1099. cc_proc_scheme_desc(req, desc, seq_size);
  1100. /* decrypt after.. */
  1101. cc_proc_cipher(req, desc, seq_size, data_flow_mode);
  1102. /* read the digest result with setting the completion bit
  1103. * must be after the cipher operation
  1104. */
  1105. cc_proc_digest_desc(req, desc, seq_size);
  1106. }
  1107. }
  1108. static void
  1109. cc_xcbc_authenc(struct aead_request *req, struct cc_hw_desc desc[],
  1110. unsigned int *seq_size)
  1111. {
  1112. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  1113. struct cc_aead_ctx *ctx = crypto_aead_ctx(tfm);
  1114. struct aead_req_ctx *req_ctx = aead_request_ctx(req);
  1115. int direct = req_ctx->gen_ctx.op_type;
  1116. unsigned int data_flow_mode =
  1117. cc_get_data_flow(direct, ctx->flow_mode,
  1118. req_ctx->is_single_pass);
  1119. if (req_ctx->is_single_pass) {
  1120. /**
  1121. * Single-pass flow
  1122. */
  1123. cc_set_xcbc_desc(req, desc, seq_size);
  1124. cc_set_cipher_desc(req, desc, seq_size);
  1125. cc_proc_header_desc(req, desc, seq_size);
  1126. cc_proc_cipher_desc(req, data_flow_mode, desc, seq_size);
  1127. cc_proc_digest_desc(req, desc, seq_size);
  1128. return;
  1129. }
  1130. /**
  1131. * Double-pass flow
  1132. * Fallback for unsupported single-pass modes,
  1133. * i.e. using assoc. data of non-word-multiple
  1134. */
  1135. if (direct == DRV_CRYPTO_DIRECTION_ENCRYPT) {
  1136. /* encrypt first.. */
  1137. cc_proc_cipher(req, desc, seq_size, data_flow_mode);
  1138. /* authenc after.. */
  1139. cc_set_xcbc_desc(req, desc, seq_size);
  1140. cc_proc_authen_desc(req, DIN_HASH, desc, seq_size, direct);
  1141. cc_proc_digest_desc(req, desc, seq_size);
  1142. } else { /*DECRYPT*/
  1143. /* authenc first.. */
  1144. cc_set_xcbc_desc(req, desc, seq_size);
  1145. cc_proc_authen_desc(req, DIN_HASH, desc, seq_size, direct);
  1146. /* decrypt after..*/
  1147. cc_proc_cipher(req, desc, seq_size, data_flow_mode);
  1148. /* read the digest result with setting the completion bit
  1149. * must be after the cipher operation
  1150. */
  1151. cc_proc_digest_desc(req, desc, seq_size);
  1152. }
  1153. }
  1154. static int validate_data_size(struct cc_aead_ctx *ctx,
  1155. enum drv_crypto_direction direct,
  1156. struct aead_request *req)
  1157. {
  1158. struct aead_req_ctx *areq_ctx = aead_request_ctx(req);
  1159. struct device *dev = drvdata_to_dev(ctx->drvdata);
  1160. unsigned int assoclen = areq_ctx->assoclen;
  1161. unsigned int cipherlen = (direct == DRV_CRYPTO_DIRECTION_DECRYPT) ?
  1162. (req->cryptlen - ctx->authsize) : req->cryptlen;
  1163. if (direct == DRV_CRYPTO_DIRECTION_DECRYPT &&
  1164. req->cryptlen < ctx->authsize)
  1165. goto data_size_err;
  1166. areq_ctx->is_single_pass = true; /*defaulted to fast flow*/
  1167. switch (ctx->flow_mode) {
  1168. case S_DIN_to_AES:
  1169. if (ctx->cipher_mode == DRV_CIPHER_CBC &&
  1170. !IS_ALIGNED(cipherlen, AES_BLOCK_SIZE))
  1171. goto data_size_err;
  1172. if (ctx->cipher_mode == DRV_CIPHER_CCM)
  1173. break;
  1174. if (ctx->cipher_mode == DRV_CIPHER_GCTR) {
  1175. if (areq_ctx->plaintext_authenticate_only)
  1176. areq_ctx->is_single_pass = false;
  1177. break;
  1178. }
  1179. if (!IS_ALIGNED(assoclen, sizeof(u32)))
  1180. areq_ctx->is_single_pass = false;
  1181. if (ctx->cipher_mode == DRV_CIPHER_CTR &&
  1182. !IS_ALIGNED(cipherlen, sizeof(u32)))
  1183. areq_ctx->is_single_pass = false;
  1184. break;
  1185. case S_DIN_to_DES:
  1186. if (!IS_ALIGNED(cipherlen, DES_BLOCK_SIZE))
  1187. goto data_size_err;
  1188. if (!IS_ALIGNED(assoclen, DES_BLOCK_SIZE))
  1189. areq_ctx->is_single_pass = false;
  1190. break;
  1191. default:
  1192. dev_err(dev, "Unexpected flow mode (%d)\n", ctx->flow_mode);
  1193. goto data_size_err;
  1194. }
  1195. return 0;
  1196. data_size_err:
  1197. return -EINVAL;
  1198. }
  1199. static unsigned int format_ccm_a0(u8 *pa0_buff, u32 header_size)
  1200. {
  1201. unsigned int len = 0;
  1202. if (header_size == 0)
  1203. return 0;
  1204. if (header_size < ((1UL << 16) - (1UL << 8))) {
  1205. len = 2;
  1206. pa0_buff[0] = (header_size >> 8) & 0xFF;
  1207. pa0_buff[1] = header_size & 0xFF;
  1208. } else {
  1209. len = 6;
  1210. pa0_buff[0] = 0xFF;
  1211. pa0_buff[1] = 0xFE;
  1212. pa0_buff[2] = (header_size >> 24) & 0xFF;
  1213. pa0_buff[3] = (header_size >> 16) & 0xFF;
  1214. pa0_buff[4] = (header_size >> 8) & 0xFF;
  1215. pa0_buff[5] = header_size & 0xFF;
  1216. }
  1217. return len;
  1218. }
  1219. static int set_msg_len(u8 *block, unsigned int msglen, unsigned int csize)
  1220. {
  1221. __be32 data;
  1222. memset(block, 0, csize);
  1223. block += csize;
  1224. if (csize >= 4)
  1225. csize = 4;
  1226. else if (msglen > (1 << (8 * csize)))
  1227. return -EOVERFLOW;
  1228. data = cpu_to_be32(msglen);
  1229. memcpy(block - csize, (u8 *)&data + 4 - csize, csize);
  1230. return 0;
  1231. }
  1232. static int cc_ccm(struct aead_request *req, struct cc_hw_desc desc[],
  1233. unsigned int *seq_size)
  1234. {
  1235. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  1236. struct cc_aead_ctx *ctx = crypto_aead_ctx(tfm);
  1237. struct aead_req_ctx *req_ctx = aead_request_ctx(req);
  1238. unsigned int idx = *seq_size;
  1239. unsigned int cipher_flow_mode;
  1240. dma_addr_t mac_result;
  1241. if (req_ctx->gen_ctx.op_type == DRV_CRYPTO_DIRECTION_DECRYPT) {
  1242. cipher_flow_mode = AES_to_HASH_and_DOUT;
  1243. mac_result = req_ctx->mac_buf_dma_addr;
  1244. } else { /* Encrypt */
  1245. cipher_flow_mode = AES_and_HASH;
  1246. mac_result = req_ctx->icv_dma_addr;
  1247. }
  1248. /* load key */
  1249. hw_desc_init(&desc[idx]);
  1250. set_cipher_mode(&desc[idx], DRV_CIPHER_CTR);
  1251. set_din_type(&desc[idx], DMA_DLLI, ctx->enckey_dma_addr,
  1252. ((ctx->enc_keylen == 24) ? CC_AES_KEY_SIZE_MAX :
  1253. ctx->enc_keylen), NS_BIT);
  1254. set_key_size_aes(&desc[idx], ctx->enc_keylen);
  1255. set_setup_mode(&desc[idx], SETUP_LOAD_KEY0);
  1256. set_cipher_config0(&desc[idx], DESC_DIRECTION_ENCRYPT_ENCRYPT);
  1257. set_flow_mode(&desc[idx], S_DIN_to_AES);
  1258. idx++;
  1259. /* load ctr state */
  1260. hw_desc_init(&desc[idx]);
  1261. set_cipher_mode(&desc[idx], DRV_CIPHER_CTR);
  1262. set_key_size_aes(&desc[idx], ctx->enc_keylen);
  1263. set_din_type(&desc[idx], DMA_DLLI,
  1264. req_ctx->gen_ctx.iv_dma_addr, AES_BLOCK_SIZE, NS_BIT);
  1265. set_cipher_config0(&desc[idx], DESC_DIRECTION_ENCRYPT_ENCRYPT);
  1266. set_setup_mode(&desc[idx], SETUP_LOAD_STATE1);
  1267. set_flow_mode(&desc[idx], S_DIN_to_AES);
  1268. idx++;
  1269. /* load MAC key */
  1270. hw_desc_init(&desc[idx]);
  1271. set_cipher_mode(&desc[idx], DRV_CIPHER_CBC_MAC);
  1272. set_din_type(&desc[idx], DMA_DLLI, ctx->enckey_dma_addr,
  1273. ((ctx->enc_keylen == 24) ? CC_AES_KEY_SIZE_MAX :
  1274. ctx->enc_keylen), NS_BIT);
  1275. set_key_size_aes(&desc[idx], ctx->enc_keylen);
  1276. set_setup_mode(&desc[idx], SETUP_LOAD_KEY0);
  1277. set_cipher_config0(&desc[idx], DESC_DIRECTION_ENCRYPT_ENCRYPT);
  1278. set_flow_mode(&desc[idx], S_DIN_to_HASH);
  1279. set_aes_not_hash_mode(&desc[idx]);
  1280. idx++;
  1281. /* load MAC state */
  1282. hw_desc_init(&desc[idx]);
  1283. set_cipher_mode(&desc[idx], DRV_CIPHER_CBC_MAC);
  1284. set_key_size_aes(&desc[idx], ctx->enc_keylen);
  1285. set_din_type(&desc[idx], DMA_DLLI, req_ctx->mac_buf_dma_addr,
  1286. AES_BLOCK_SIZE, NS_BIT);
  1287. set_cipher_config0(&desc[idx], DESC_DIRECTION_ENCRYPT_ENCRYPT);
  1288. set_setup_mode(&desc[idx], SETUP_LOAD_STATE0);
  1289. set_flow_mode(&desc[idx], S_DIN_to_HASH);
  1290. set_aes_not_hash_mode(&desc[idx]);
  1291. idx++;
  1292. /* process assoc data */
  1293. if (req_ctx->assoclen > 0) {
  1294. cc_set_assoc_desc(req, DIN_HASH, desc, &idx);
  1295. } else {
  1296. hw_desc_init(&desc[idx]);
  1297. set_din_type(&desc[idx], DMA_DLLI,
  1298. sg_dma_address(&req_ctx->ccm_adata_sg),
  1299. AES_BLOCK_SIZE + req_ctx->ccm_hdr_size, NS_BIT);
  1300. set_flow_mode(&desc[idx], DIN_HASH);
  1301. idx++;
  1302. }
  1303. /* process the cipher */
  1304. if (req_ctx->cryptlen)
  1305. cc_proc_cipher_desc(req, cipher_flow_mode, desc, &idx);
  1306. /* Read temporal MAC */
  1307. hw_desc_init(&desc[idx]);
  1308. set_cipher_mode(&desc[idx], DRV_CIPHER_CBC_MAC);
  1309. set_dout_dlli(&desc[idx], req_ctx->mac_buf_dma_addr, ctx->authsize,
  1310. NS_BIT, 0);
  1311. set_setup_mode(&desc[idx], SETUP_WRITE_STATE0);
  1312. set_cipher_config0(&desc[idx], HASH_DIGEST_RESULT_LITTLE_ENDIAN);
  1313. set_flow_mode(&desc[idx], S_HASH_to_DOUT);
  1314. set_aes_not_hash_mode(&desc[idx]);
  1315. idx++;
  1316. /* load AES-CTR state (for last MAC calculation)*/
  1317. hw_desc_init(&desc[idx]);
  1318. set_cipher_mode(&desc[idx], DRV_CIPHER_CTR);
  1319. set_cipher_config0(&desc[idx], DRV_CRYPTO_DIRECTION_ENCRYPT);
  1320. set_din_type(&desc[idx], DMA_DLLI, req_ctx->ccm_iv0_dma_addr,
  1321. AES_BLOCK_SIZE, NS_BIT);
  1322. set_key_size_aes(&desc[idx], ctx->enc_keylen);
  1323. set_setup_mode(&desc[idx], SETUP_LOAD_STATE1);
  1324. set_flow_mode(&desc[idx], S_DIN_to_AES);
  1325. idx++;
  1326. hw_desc_init(&desc[idx]);
  1327. set_din_no_dma(&desc[idx], 0, 0xfffff0);
  1328. set_dout_no_dma(&desc[idx], 0, 0, 1);
  1329. idx++;
  1330. /* encrypt the "T" value and store MAC in mac_state */
  1331. hw_desc_init(&desc[idx]);
  1332. set_din_type(&desc[idx], DMA_DLLI, req_ctx->mac_buf_dma_addr,
  1333. ctx->authsize, NS_BIT);
  1334. set_dout_dlli(&desc[idx], mac_result, ctx->authsize, NS_BIT, 1);
  1335. set_queue_last_ind(ctx->drvdata, &desc[idx]);
  1336. set_flow_mode(&desc[idx], DIN_AES_DOUT);
  1337. idx++;
  1338. *seq_size = idx;
  1339. return 0;
  1340. }
  1341. static int config_ccm_adata(struct aead_request *req)
  1342. {
  1343. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  1344. struct cc_aead_ctx *ctx = crypto_aead_ctx(tfm);
  1345. struct device *dev = drvdata_to_dev(ctx->drvdata);
  1346. struct aead_req_ctx *req_ctx = aead_request_ctx(req);
  1347. //unsigned int size_of_a = 0, rem_a_size = 0;
  1348. unsigned int lp = req->iv[0];
  1349. /* Note: The code assume that req->iv[0] already contains the value
  1350. * of L' of RFC3610
  1351. */
  1352. unsigned int l = lp + 1; /* This is L' of RFC 3610. */
  1353. unsigned int m = ctx->authsize; /* This is M' of RFC 3610. */
  1354. u8 *b0 = req_ctx->ccm_config + CCM_B0_OFFSET;
  1355. u8 *a0 = req_ctx->ccm_config + CCM_A0_OFFSET;
  1356. u8 *ctr_count_0 = req_ctx->ccm_config + CCM_CTR_COUNT_0_OFFSET;
  1357. unsigned int cryptlen = (req_ctx->gen_ctx.op_type ==
  1358. DRV_CRYPTO_DIRECTION_ENCRYPT) ?
  1359. req->cryptlen :
  1360. (req->cryptlen - ctx->authsize);
  1361. int rc;
  1362. memset(req_ctx->mac_buf, 0, AES_BLOCK_SIZE);
  1363. memset(req_ctx->ccm_config, 0, AES_BLOCK_SIZE * 3);
  1364. /* taken from crypto/ccm.c */
  1365. /* 2 <= L <= 8, so 1 <= L' <= 7. */
  1366. if (l < 2 || l > 8) {
  1367. dev_err(dev, "illegal iv value %X\n", req->iv[0]);
  1368. return -EINVAL;
  1369. }
  1370. memcpy(b0, req->iv, AES_BLOCK_SIZE);
  1371. /* format control info per RFC 3610 and
  1372. * NIST Special Publication 800-38C
  1373. */
  1374. *b0 |= (8 * ((m - 2) / 2));
  1375. if (req_ctx->assoclen > 0)
  1376. *b0 |= 64; /* Enable bit 6 if Adata exists. */
  1377. rc = set_msg_len(b0 + 16 - l, cryptlen, l); /* Write L'. */
  1378. if (rc) {
  1379. dev_err(dev, "message len overflow detected");
  1380. return rc;
  1381. }
  1382. /* END of "taken from crypto/ccm.c" */
  1383. /* l(a) - size of associated data. */
  1384. req_ctx->ccm_hdr_size = format_ccm_a0(a0, req_ctx->assoclen);
  1385. memset(req->iv + 15 - req->iv[0], 0, req->iv[0] + 1);
  1386. req->iv[15] = 1;
  1387. memcpy(ctr_count_0, req->iv, AES_BLOCK_SIZE);
  1388. ctr_count_0[15] = 0;
  1389. return 0;
  1390. }
  1391. static void cc_proc_rfc4309_ccm(struct aead_request *req)
  1392. {
  1393. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  1394. struct cc_aead_ctx *ctx = crypto_aead_ctx(tfm);
  1395. struct aead_req_ctx *areq_ctx = aead_request_ctx(req);
  1396. /* L' */
  1397. memset(areq_ctx->ctr_iv, 0, AES_BLOCK_SIZE);
  1398. /* For RFC 4309, always use 4 bytes for message length
  1399. * (at most 2^32-1 bytes).
  1400. */
  1401. areq_ctx->ctr_iv[0] = 3;
  1402. /* In RFC 4309 there is an 11-bytes nonce+IV part,
  1403. * that we build here.
  1404. */
  1405. memcpy(areq_ctx->ctr_iv + CCM_BLOCK_NONCE_OFFSET, ctx->ctr_nonce,
  1406. CCM_BLOCK_NONCE_SIZE);
  1407. memcpy(areq_ctx->ctr_iv + CCM_BLOCK_IV_OFFSET, req->iv,
  1408. CCM_BLOCK_IV_SIZE);
  1409. req->iv = areq_ctx->ctr_iv;
  1410. areq_ctx->assoclen -= CCM_BLOCK_IV_SIZE;
  1411. }
  1412. static void cc_set_ghash_desc(struct aead_request *req,
  1413. struct cc_hw_desc desc[], unsigned int *seq_size)
  1414. {
  1415. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  1416. struct cc_aead_ctx *ctx = crypto_aead_ctx(tfm);
  1417. struct aead_req_ctx *req_ctx = aead_request_ctx(req);
  1418. unsigned int idx = *seq_size;
  1419. /* load key to AES*/
  1420. hw_desc_init(&desc[idx]);
  1421. set_cipher_mode(&desc[idx], DRV_CIPHER_ECB);
  1422. set_cipher_config0(&desc[idx], DRV_CRYPTO_DIRECTION_ENCRYPT);
  1423. set_din_type(&desc[idx], DMA_DLLI, ctx->enckey_dma_addr,
  1424. ctx->enc_keylen, NS_BIT);
  1425. set_key_size_aes(&desc[idx], ctx->enc_keylen);
  1426. set_setup_mode(&desc[idx], SETUP_LOAD_KEY0);
  1427. set_flow_mode(&desc[idx], S_DIN_to_AES);
  1428. idx++;
  1429. /* process one zero block to generate hkey */
  1430. hw_desc_init(&desc[idx]);
  1431. set_din_const(&desc[idx], 0x0, AES_BLOCK_SIZE);
  1432. set_dout_dlli(&desc[idx], req_ctx->hkey_dma_addr, AES_BLOCK_SIZE,
  1433. NS_BIT, 0);
  1434. set_flow_mode(&desc[idx], DIN_AES_DOUT);
  1435. idx++;
  1436. /* Memory Barrier */
  1437. hw_desc_init(&desc[idx]);
  1438. set_din_no_dma(&desc[idx], 0, 0xfffff0);
  1439. set_dout_no_dma(&desc[idx], 0, 0, 1);
  1440. idx++;
  1441. /* Load GHASH subkey */
  1442. hw_desc_init(&desc[idx]);
  1443. set_din_type(&desc[idx], DMA_DLLI, req_ctx->hkey_dma_addr,
  1444. AES_BLOCK_SIZE, NS_BIT);
  1445. set_dout_no_dma(&desc[idx], 0, 0, 1);
  1446. set_flow_mode(&desc[idx], S_DIN_to_HASH);
  1447. set_aes_not_hash_mode(&desc[idx]);
  1448. set_cipher_mode(&desc[idx], DRV_HASH_HW_GHASH);
  1449. set_cipher_config1(&desc[idx], HASH_PADDING_ENABLED);
  1450. set_setup_mode(&desc[idx], SETUP_LOAD_KEY0);
  1451. idx++;
  1452. /* Configure Hash Engine to work with GHASH.
  1453. * Since it was not possible to extend HASH submodes to add GHASH,
  1454. * The following command is necessary in order to
  1455. * select GHASH (according to HW designers)
  1456. */
  1457. hw_desc_init(&desc[idx]);
  1458. set_din_no_dma(&desc[idx], 0, 0xfffff0);
  1459. set_dout_no_dma(&desc[idx], 0, 0, 1);
  1460. set_flow_mode(&desc[idx], S_DIN_to_HASH);
  1461. set_aes_not_hash_mode(&desc[idx]);
  1462. set_cipher_mode(&desc[idx], DRV_HASH_HW_GHASH);
  1463. set_cipher_do(&desc[idx], 1); //1=AES_SK RKEK
  1464. set_cipher_config0(&desc[idx], DRV_CRYPTO_DIRECTION_ENCRYPT);
  1465. set_cipher_config1(&desc[idx], HASH_PADDING_ENABLED);
  1466. set_setup_mode(&desc[idx], SETUP_LOAD_KEY0);
  1467. idx++;
  1468. /* Load GHASH initial STATE (which is 0). (for any hash there is an
  1469. * initial state)
  1470. */
  1471. hw_desc_init(&desc[idx]);
  1472. set_din_const(&desc[idx], 0x0, AES_BLOCK_SIZE);
  1473. set_dout_no_dma(&desc[idx], 0, 0, 1);
  1474. set_flow_mode(&desc[idx], S_DIN_to_HASH);
  1475. set_aes_not_hash_mode(&desc[idx]);
  1476. set_cipher_mode(&desc[idx], DRV_HASH_HW_GHASH);
  1477. set_cipher_config1(&desc[idx], HASH_PADDING_ENABLED);
  1478. set_setup_mode(&desc[idx], SETUP_LOAD_STATE0);
  1479. idx++;
  1480. *seq_size = idx;
  1481. }
  1482. static void cc_set_gctr_desc(struct aead_request *req, struct cc_hw_desc desc[],
  1483. unsigned int *seq_size)
  1484. {
  1485. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  1486. struct cc_aead_ctx *ctx = crypto_aead_ctx(tfm);
  1487. struct aead_req_ctx *req_ctx = aead_request_ctx(req);
  1488. unsigned int idx = *seq_size;
  1489. /* load key to AES*/
  1490. hw_desc_init(&desc[idx]);
  1491. set_cipher_mode(&desc[idx], DRV_CIPHER_GCTR);
  1492. set_cipher_config0(&desc[idx], DRV_CRYPTO_DIRECTION_ENCRYPT);
  1493. set_din_type(&desc[idx], DMA_DLLI, ctx->enckey_dma_addr,
  1494. ctx->enc_keylen, NS_BIT);
  1495. set_key_size_aes(&desc[idx], ctx->enc_keylen);
  1496. set_setup_mode(&desc[idx], SETUP_LOAD_KEY0);
  1497. set_flow_mode(&desc[idx], S_DIN_to_AES);
  1498. idx++;
  1499. if (req_ctx->cryptlen && !req_ctx->plaintext_authenticate_only) {
  1500. /* load AES/CTR initial CTR value inc by 2*/
  1501. hw_desc_init(&desc[idx]);
  1502. set_cipher_mode(&desc[idx], DRV_CIPHER_GCTR);
  1503. set_key_size_aes(&desc[idx], ctx->enc_keylen);
  1504. set_din_type(&desc[idx], DMA_DLLI,
  1505. req_ctx->gcm_iv_inc2_dma_addr, AES_BLOCK_SIZE,
  1506. NS_BIT);
  1507. set_cipher_config0(&desc[idx], DRV_CRYPTO_DIRECTION_ENCRYPT);
  1508. set_setup_mode(&desc[idx], SETUP_LOAD_STATE1);
  1509. set_flow_mode(&desc[idx], S_DIN_to_AES);
  1510. idx++;
  1511. }
  1512. *seq_size = idx;
  1513. }
  1514. static void cc_proc_gcm_result(struct aead_request *req,
  1515. struct cc_hw_desc desc[],
  1516. unsigned int *seq_size)
  1517. {
  1518. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  1519. struct cc_aead_ctx *ctx = crypto_aead_ctx(tfm);
  1520. struct aead_req_ctx *req_ctx = aead_request_ctx(req);
  1521. dma_addr_t mac_result;
  1522. unsigned int idx = *seq_size;
  1523. if (req_ctx->gen_ctx.op_type == DRV_CRYPTO_DIRECTION_DECRYPT) {
  1524. mac_result = req_ctx->mac_buf_dma_addr;
  1525. } else { /* Encrypt */
  1526. mac_result = req_ctx->icv_dma_addr;
  1527. }
  1528. /* process(ghash) gcm_block_len */
  1529. hw_desc_init(&desc[idx]);
  1530. set_din_type(&desc[idx], DMA_DLLI, req_ctx->gcm_block_len_dma_addr,
  1531. AES_BLOCK_SIZE, NS_BIT);
  1532. set_flow_mode(&desc[idx], DIN_HASH);
  1533. idx++;
  1534. /* Store GHASH state after GHASH(Associated Data + Cipher +LenBlock) */
  1535. hw_desc_init(&desc[idx]);
  1536. set_cipher_mode(&desc[idx], DRV_HASH_HW_GHASH);
  1537. set_din_no_dma(&desc[idx], 0, 0xfffff0);
  1538. set_dout_dlli(&desc[idx], req_ctx->mac_buf_dma_addr, AES_BLOCK_SIZE,
  1539. NS_BIT, 0);
  1540. set_setup_mode(&desc[idx], SETUP_WRITE_STATE0);
  1541. set_flow_mode(&desc[idx], S_HASH_to_DOUT);
  1542. set_aes_not_hash_mode(&desc[idx]);
  1543. idx++;
  1544. /* load AES/CTR initial CTR value inc by 1*/
  1545. hw_desc_init(&desc[idx]);
  1546. set_cipher_mode(&desc[idx], DRV_CIPHER_GCTR);
  1547. set_key_size_aes(&desc[idx], ctx->enc_keylen);
  1548. set_din_type(&desc[idx], DMA_DLLI, req_ctx->gcm_iv_inc1_dma_addr,
  1549. AES_BLOCK_SIZE, NS_BIT);
  1550. set_cipher_config0(&desc[idx], DRV_CRYPTO_DIRECTION_ENCRYPT);
  1551. set_setup_mode(&desc[idx], SETUP_LOAD_STATE1);
  1552. set_flow_mode(&desc[idx], S_DIN_to_AES);
  1553. idx++;
  1554. /* Memory Barrier */
  1555. hw_desc_init(&desc[idx]);
  1556. set_din_no_dma(&desc[idx], 0, 0xfffff0);
  1557. set_dout_no_dma(&desc[idx], 0, 0, 1);
  1558. idx++;
  1559. /* process GCTR on stored GHASH and store MAC in mac_state*/
  1560. hw_desc_init(&desc[idx]);
  1561. set_cipher_mode(&desc[idx], DRV_CIPHER_GCTR);
  1562. set_din_type(&desc[idx], DMA_DLLI, req_ctx->mac_buf_dma_addr,
  1563. AES_BLOCK_SIZE, NS_BIT);
  1564. set_dout_dlli(&desc[idx], mac_result, ctx->authsize, NS_BIT, 1);
  1565. set_queue_last_ind(ctx->drvdata, &desc[idx]);
  1566. set_flow_mode(&desc[idx], DIN_AES_DOUT);
  1567. idx++;
  1568. *seq_size = idx;
  1569. }
  1570. static int cc_gcm(struct aead_request *req, struct cc_hw_desc desc[],
  1571. unsigned int *seq_size)
  1572. {
  1573. struct aead_req_ctx *req_ctx = aead_request_ctx(req);
  1574. unsigned int cipher_flow_mode;
  1575. if (req_ctx->gen_ctx.op_type == DRV_CRYPTO_DIRECTION_DECRYPT) {
  1576. cipher_flow_mode = AES_and_HASH;
  1577. } else { /* Encrypt */
  1578. cipher_flow_mode = AES_to_HASH_and_DOUT;
  1579. }
  1580. //in RFC4543 no data to encrypt. just copy data from src to dest.
  1581. if (req_ctx->plaintext_authenticate_only) {
  1582. cc_proc_cipher_desc(req, BYPASS, desc, seq_size);
  1583. cc_set_ghash_desc(req, desc, seq_size);
  1584. /* process(ghash) assoc data */
  1585. cc_set_assoc_desc(req, DIN_HASH, desc, seq_size);
  1586. cc_set_gctr_desc(req, desc, seq_size);
  1587. cc_proc_gcm_result(req, desc, seq_size);
  1588. return 0;
  1589. }
  1590. // for gcm and rfc4106.
  1591. cc_set_ghash_desc(req, desc, seq_size);
  1592. /* process(ghash) assoc data */
  1593. if (req_ctx->assoclen > 0)
  1594. cc_set_assoc_desc(req, DIN_HASH, desc, seq_size);
  1595. cc_set_gctr_desc(req, desc, seq_size);
  1596. /* process(gctr+ghash) */
  1597. if (req_ctx->cryptlen)
  1598. cc_proc_cipher_desc(req, cipher_flow_mode, desc, seq_size);
  1599. cc_proc_gcm_result(req, desc, seq_size);
  1600. return 0;
  1601. }
  1602. static int config_gcm_context(struct aead_request *req)
  1603. {
  1604. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  1605. struct cc_aead_ctx *ctx = crypto_aead_ctx(tfm);
  1606. struct aead_req_ctx *req_ctx = aead_request_ctx(req);
  1607. struct device *dev = drvdata_to_dev(ctx->drvdata);
  1608. unsigned int cryptlen = (req_ctx->gen_ctx.op_type ==
  1609. DRV_CRYPTO_DIRECTION_ENCRYPT) ?
  1610. req->cryptlen :
  1611. (req->cryptlen - ctx->authsize);
  1612. __be32 counter = cpu_to_be32(2);
  1613. dev_dbg(dev, "%s() cryptlen = %d, req_ctx->assoclen = %d ctx->authsize = %d\n",
  1614. __func__, cryptlen, req_ctx->assoclen, ctx->authsize);
  1615. memset(req_ctx->hkey, 0, AES_BLOCK_SIZE);
  1616. memset(req_ctx->mac_buf, 0, AES_BLOCK_SIZE);
  1617. memcpy(req->iv + 12, &counter, 4);
  1618. memcpy(req_ctx->gcm_iv_inc2, req->iv, 16);
  1619. counter = cpu_to_be32(1);
  1620. memcpy(req->iv + 12, &counter, 4);
  1621. memcpy(req_ctx->gcm_iv_inc1, req->iv, 16);
  1622. if (!req_ctx->plaintext_authenticate_only) {
  1623. __be64 temp64;
  1624. temp64 = cpu_to_be64(req_ctx->assoclen * 8);
  1625. memcpy(&req_ctx->gcm_len_block.len_a, &temp64, sizeof(temp64));
  1626. temp64 = cpu_to_be64(cryptlen * 8);
  1627. memcpy(&req_ctx->gcm_len_block.len_c, &temp64, 8);
  1628. } else {
  1629. /* rfc4543=> all data(AAD,IV,Plain) are considered additional
  1630. * data that is nothing is encrypted.
  1631. */
  1632. __be64 temp64;
  1633. temp64 = cpu_to_be64((req_ctx->assoclen +
  1634. GCM_BLOCK_RFC4_IV_SIZE + cryptlen) * 8);
  1635. memcpy(&req_ctx->gcm_len_block.len_a, &temp64, sizeof(temp64));
  1636. temp64 = 0;
  1637. memcpy(&req_ctx->gcm_len_block.len_c, &temp64, 8);
  1638. }
  1639. return 0;
  1640. }
  1641. static void cc_proc_rfc4_gcm(struct aead_request *req)
  1642. {
  1643. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  1644. struct cc_aead_ctx *ctx = crypto_aead_ctx(tfm);
  1645. struct aead_req_ctx *areq_ctx = aead_request_ctx(req);
  1646. memcpy(areq_ctx->ctr_iv + GCM_BLOCK_RFC4_NONCE_OFFSET,
  1647. ctx->ctr_nonce, GCM_BLOCK_RFC4_NONCE_SIZE);
  1648. memcpy(areq_ctx->ctr_iv + GCM_BLOCK_RFC4_IV_OFFSET, req->iv,
  1649. GCM_BLOCK_RFC4_IV_SIZE);
  1650. req->iv = areq_ctx->ctr_iv;
  1651. areq_ctx->assoclen -= GCM_BLOCK_RFC4_IV_SIZE;
  1652. }
  1653. static int cc_proc_aead(struct aead_request *req,
  1654. enum drv_crypto_direction direct)
  1655. {
  1656. int rc = 0;
  1657. int seq_len = 0;
  1658. struct cc_hw_desc desc[MAX_AEAD_PROCESS_SEQ];
  1659. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  1660. struct cc_aead_ctx *ctx = crypto_aead_ctx(tfm);
  1661. struct aead_req_ctx *areq_ctx = aead_request_ctx(req);
  1662. struct device *dev = drvdata_to_dev(ctx->drvdata);
  1663. struct cc_crypto_req cc_req = {};
  1664. dev_dbg(dev, "%s context=%p req=%p iv=%p src=%p src_ofs=%d dst=%p dst_ofs=%d cryptolen=%d\n",
  1665. ((direct == DRV_CRYPTO_DIRECTION_ENCRYPT) ? "Enc" : "Dec"),
  1666. ctx, req, req->iv, sg_virt(req->src), req->src->offset,
  1667. sg_virt(req->dst), req->dst->offset, req->cryptlen);
  1668. /* STAT_PHASE_0: Init and sanity checks */
  1669. /* Check data length according to mode */
  1670. if (validate_data_size(ctx, direct, req)) {
  1671. dev_err(dev, "Unsupported crypt/assoc len %d/%d.\n",
  1672. req->cryptlen, areq_ctx->assoclen);
  1673. crypto_aead_set_flags(tfm, CRYPTO_TFM_RES_BAD_BLOCK_LEN);
  1674. return -EINVAL;
  1675. }
  1676. /* Setup request structure */
  1677. cc_req.user_cb = (void *)cc_aead_complete;
  1678. cc_req.user_arg = (void *)req;
  1679. /* Setup request context */
  1680. areq_ctx->gen_ctx.op_type = direct;
  1681. areq_ctx->req_authsize = ctx->authsize;
  1682. areq_ctx->cipher_mode = ctx->cipher_mode;
  1683. /* STAT_PHASE_1: Map buffers */
  1684. if (ctx->cipher_mode == DRV_CIPHER_CTR) {
  1685. /* Build CTR IV - Copy nonce from last 4 bytes in
  1686. * CTR key to first 4 bytes in CTR IV
  1687. */
  1688. memcpy(areq_ctx->ctr_iv, ctx->ctr_nonce,
  1689. CTR_RFC3686_NONCE_SIZE);
  1690. if (!areq_ctx->backup_giv) /*User none-generated IV*/
  1691. memcpy(areq_ctx->ctr_iv + CTR_RFC3686_NONCE_SIZE,
  1692. req->iv, CTR_RFC3686_IV_SIZE);
  1693. /* Initialize counter portion of counter block */
  1694. *(__be32 *)(areq_ctx->ctr_iv + CTR_RFC3686_NONCE_SIZE +
  1695. CTR_RFC3686_IV_SIZE) = cpu_to_be32(1);
  1696. /* Replace with counter iv */
  1697. req->iv = areq_ctx->ctr_iv;
  1698. areq_ctx->hw_iv_size = CTR_RFC3686_BLOCK_SIZE;
  1699. } else if ((ctx->cipher_mode == DRV_CIPHER_CCM) ||
  1700. (ctx->cipher_mode == DRV_CIPHER_GCTR)) {
  1701. areq_ctx->hw_iv_size = AES_BLOCK_SIZE;
  1702. if (areq_ctx->ctr_iv != req->iv) {
  1703. memcpy(areq_ctx->ctr_iv, req->iv,
  1704. crypto_aead_ivsize(tfm));
  1705. req->iv = areq_ctx->ctr_iv;
  1706. }
  1707. } else {
  1708. areq_ctx->hw_iv_size = crypto_aead_ivsize(tfm);
  1709. }
  1710. if (ctx->cipher_mode == DRV_CIPHER_CCM) {
  1711. rc = config_ccm_adata(req);
  1712. if (rc) {
  1713. dev_dbg(dev, "config_ccm_adata() returned with a failure %d!",
  1714. rc);
  1715. goto exit;
  1716. }
  1717. } else {
  1718. areq_ctx->ccm_hdr_size = ccm_header_size_null;
  1719. }
  1720. if (ctx->cipher_mode == DRV_CIPHER_GCTR) {
  1721. rc = config_gcm_context(req);
  1722. if (rc) {
  1723. dev_dbg(dev, "config_gcm_context() returned with a failure %d!",
  1724. rc);
  1725. goto exit;
  1726. }
  1727. }
  1728. rc = cc_map_aead_request(ctx->drvdata, req);
  1729. if (rc) {
  1730. dev_err(dev, "map_request() failed\n");
  1731. goto exit;
  1732. }
  1733. /* do we need to generate IV? */
  1734. if (areq_ctx->backup_giv) {
  1735. /* set the DMA mapped IV address*/
  1736. if (ctx->cipher_mode == DRV_CIPHER_CTR) {
  1737. cc_req.ivgen_dma_addr[0] =
  1738. areq_ctx->gen_ctx.iv_dma_addr +
  1739. CTR_RFC3686_NONCE_SIZE;
  1740. cc_req.ivgen_dma_addr_len = 1;
  1741. } else if (ctx->cipher_mode == DRV_CIPHER_CCM) {
  1742. /* In ccm, the IV needs to exist both inside B0 and
  1743. * inside the counter.It is also copied to iv_dma_addr
  1744. * for other reasons (like returning it to the user).
  1745. * So, using 3 (identical) IV outputs.
  1746. */
  1747. cc_req.ivgen_dma_addr[0] =
  1748. areq_ctx->gen_ctx.iv_dma_addr +
  1749. CCM_BLOCK_IV_OFFSET;
  1750. cc_req.ivgen_dma_addr[1] =
  1751. sg_dma_address(&areq_ctx->ccm_adata_sg) +
  1752. CCM_B0_OFFSET + CCM_BLOCK_IV_OFFSET;
  1753. cc_req.ivgen_dma_addr[2] =
  1754. sg_dma_address(&areq_ctx->ccm_adata_sg) +
  1755. CCM_CTR_COUNT_0_OFFSET + CCM_BLOCK_IV_OFFSET;
  1756. cc_req.ivgen_dma_addr_len = 3;
  1757. } else {
  1758. cc_req.ivgen_dma_addr[0] =
  1759. areq_ctx->gen_ctx.iv_dma_addr;
  1760. cc_req.ivgen_dma_addr_len = 1;
  1761. }
  1762. /* set the IV size (8/16 B long)*/
  1763. cc_req.ivgen_size = crypto_aead_ivsize(tfm);
  1764. }
  1765. /* STAT_PHASE_2: Create sequence */
  1766. /* Load MLLI tables to SRAM if necessary */
  1767. cc_mlli_to_sram(req, desc, &seq_len);
  1768. /*TODO: move seq len by reference */
  1769. switch (ctx->auth_mode) {
  1770. case DRV_HASH_SHA1:
  1771. case DRV_HASH_SHA256:
  1772. cc_hmac_authenc(req, desc, &seq_len);
  1773. break;
  1774. case DRV_HASH_XCBC_MAC:
  1775. cc_xcbc_authenc(req, desc, &seq_len);
  1776. break;
  1777. case DRV_HASH_NULL:
  1778. if (ctx->cipher_mode == DRV_CIPHER_CCM)
  1779. cc_ccm(req, desc, &seq_len);
  1780. if (ctx->cipher_mode == DRV_CIPHER_GCTR)
  1781. cc_gcm(req, desc, &seq_len);
  1782. break;
  1783. default:
  1784. dev_err(dev, "Unsupported authenc (%d)\n", ctx->auth_mode);
  1785. cc_unmap_aead_request(dev, req);
  1786. rc = -ENOTSUPP;
  1787. goto exit;
  1788. }
  1789. /* STAT_PHASE_3: Lock HW and push sequence */
  1790. rc = cc_send_request(ctx->drvdata, &cc_req, desc, seq_len, &req->base);
  1791. if (rc != -EINPROGRESS && rc != -EBUSY) {
  1792. dev_err(dev, "send_request() failed (rc=%d)\n", rc);
  1793. cc_unmap_aead_request(dev, req);
  1794. }
  1795. exit:
  1796. return rc;
  1797. }
  1798. static int cc_aead_encrypt(struct aead_request *req)
  1799. {
  1800. struct aead_req_ctx *areq_ctx = aead_request_ctx(req);
  1801. int rc;
  1802. memset(areq_ctx, 0, sizeof(*areq_ctx));
  1803. /* No generated IV required */
  1804. areq_ctx->backup_iv = req->iv;
  1805. areq_ctx->assoclen = req->assoclen;
  1806. areq_ctx->backup_giv = NULL;
  1807. areq_ctx->is_gcm4543 = false;
  1808. areq_ctx->plaintext_authenticate_only = false;
  1809. rc = cc_proc_aead(req, DRV_CRYPTO_DIRECTION_ENCRYPT);
  1810. if (rc != -EINPROGRESS && rc != -EBUSY)
  1811. req->iv = areq_ctx->backup_iv;
  1812. return rc;
  1813. }
  1814. static int cc_rfc4309_ccm_encrypt(struct aead_request *req)
  1815. {
  1816. /* Very similar to cc_aead_encrypt() above. */
  1817. struct aead_req_ctx *areq_ctx = aead_request_ctx(req);
  1818. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  1819. struct cc_aead_ctx *ctx = crypto_aead_ctx(tfm);
  1820. struct device *dev = drvdata_to_dev(ctx->drvdata);
  1821. int rc = -EINVAL;
  1822. if (!valid_assoclen(req)) {
  1823. dev_err(dev, "invalid Assoclen:%u\n", req->assoclen);
  1824. goto out;
  1825. }
  1826. memset(areq_ctx, 0, sizeof(*areq_ctx));
  1827. /* No generated IV required */
  1828. areq_ctx->backup_iv = req->iv;
  1829. areq_ctx->assoclen = req->assoclen;
  1830. areq_ctx->backup_giv = NULL;
  1831. areq_ctx->is_gcm4543 = true;
  1832. cc_proc_rfc4309_ccm(req);
  1833. rc = cc_proc_aead(req, DRV_CRYPTO_DIRECTION_ENCRYPT);
  1834. if (rc != -EINPROGRESS && rc != -EBUSY)
  1835. req->iv = areq_ctx->backup_iv;
  1836. out:
  1837. return rc;
  1838. }
  1839. static int cc_aead_decrypt(struct aead_request *req)
  1840. {
  1841. struct aead_req_ctx *areq_ctx = aead_request_ctx(req);
  1842. int rc;
  1843. memset(areq_ctx, 0, sizeof(*areq_ctx));
  1844. /* No generated IV required */
  1845. areq_ctx->backup_iv = req->iv;
  1846. areq_ctx->assoclen = req->assoclen;
  1847. areq_ctx->backup_giv = NULL;
  1848. areq_ctx->is_gcm4543 = false;
  1849. areq_ctx->plaintext_authenticate_only = false;
  1850. rc = cc_proc_aead(req, DRV_CRYPTO_DIRECTION_DECRYPT);
  1851. if (rc != -EINPROGRESS && rc != -EBUSY)
  1852. req->iv = areq_ctx->backup_iv;
  1853. return rc;
  1854. }
  1855. static int cc_rfc4309_ccm_decrypt(struct aead_request *req)
  1856. {
  1857. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  1858. struct cc_aead_ctx *ctx = crypto_aead_ctx(tfm);
  1859. struct device *dev = drvdata_to_dev(ctx->drvdata);
  1860. struct aead_req_ctx *areq_ctx = aead_request_ctx(req);
  1861. int rc = -EINVAL;
  1862. if (!valid_assoclen(req)) {
  1863. dev_err(dev, "invalid Assoclen:%u\n", req->assoclen);
  1864. goto out;
  1865. }
  1866. memset(areq_ctx, 0, sizeof(*areq_ctx));
  1867. /* No generated IV required */
  1868. areq_ctx->backup_iv = req->iv;
  1869. areq_ctx->assoclen = req->assoclen;
  1870. areq_ctx->backup_giv = NULL;
  1871. areq_ctx->is_gcm4543 = true;
  1872. cc_proc_rfc4309_ccm(req);
  1873. rc = cc_proc_aead(req, DRV_CRYPTO_DIRECTION_DECRYPT);
  1874. if (rc != -EINPROGRESS && rc != -EBUSY)
  1875. req->iv = areq_ctx->backup_iv;
  1876. out:
  1877. return rc;
  1878. }
  1879. static int cc_rfc4106_gcm_setkey(struct crypto_aead *tfm, const u8 *key,
  1880. unsigned int keylen)
  1881. {
  1882. struct cc_aead_ctx *ctx = crypto_aead_ctx(tfm);
  1883. struct device *dev = drvdata_to_dev(ctx->drvdata);
  1884. dev_dbg(dev, "%s() keylen %d, key %p\n", __func__, keylen, key);
  1885. if (keylen < 4)
  1886. return -EINVAL;
  1887. keylen -= 4;
  1888. memcpy(ctx->ctr_nonce, key + keylen, 4);
  1889. return cc_aead_setkey(tfm, key, keylen);
  1890. }
  1891. static int cc_rfc4543_gcm_setkey(struct crypto_aead *tfm, const u8 *key,
  1892. unsigned int keylen)
  1893. {
  1894. struct cc_aead_ctx *ctx = crypto_aead_ctx(tfm);
  1895. struct device *dev = drvdata_to_dev(ctx->drvdata);
  1896. dev_dbg(dev, "%s() keylen %d, key %p\n", __func__, keylen, key);
  1897. if (keylen < 4)
  1898. return -EINVAL;
  1899. keylen -= 4;
  1900. memcpy(ctx->ctr_nonce, key + keylen, 4);
  1901. return cc_aead_setkey(tfm, key, keylen);
  1902. }
  1903. static int cc_gcm_setauthsize(struct crypto_aead *authenc,
  1904. unsigned int authsize)
  1905. {
  1906. switch (authsize) {
  1907. case 4:
  1908. case 8:
  1909. case 12:
  1910. case 13:
  1911. case 14:
  1912. case 15:
  1913. case 16:
  1914. break;
  1915. default:
  1916. return -EINVAL;
  1917. }
  1918. return cc_aead_setauthsize(authenc, authsize);
  1919. }
  1920. static int cc_rfc4106_gcm_setauthsize(struct crypto_aead *authenc,
  1921. unsigned int authsize)
  1922. {
  1923. struct cc_aead_ctx *ctx = crypto_aead_ctx(authenc);
  1924. struct device *dev = drvdata_to_dev(ctx->drvdata);
  1925. dev_dbg(dev, "authsize %d\n", authsize);
  1926. switch (authsize) {
  1927. case 8:
  1928. case 12:
  1929. case 16:
  1930. break;
  1931. default:
  1932. return -EINVAL;
  1933. }
  1934. return cc_aead_setauthsize(authenc, authsize);
  1935. }
  1936. static int cc_rfc4543_gcm_setauthsize(struct crypto_aead *authenc,
  1937. unsigned int authsize)
  1938. {
  1939. struct cc_aead_ctx *ctx = crypto_aead_ctx(authenc);
  1940. struct device *dev = drvdata_to_dev(ctx->drvdata);
  1941. dev_dbg(dev, "authsize %d\n", authsize);
  1942. if (authsize != 16)
  1943. return -EINVAL;
  1944. return cc_aead_setauthsize(authenc, authsize);
  1945. }
  1946. static int cc_rfc4106_gcm_encrypt(struct aead_request *req)
  1947. {
  1948. /* Very similar to cc_aead_encrypt() above. */
  1949. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  1950. struct cc_aead_ctx *ctx = crypto_aead_ctx(tfm);
  1951. struct device *dev = drvdata_to_dev(ctx->drvdata);
  1952. struct aead_req_ctx *areq_ctx = aead_request_ctx(req);
  1953. int rc = -EINVAL;
  1954. if (!valid_assoclen(req)) {
  1955. dev_err(dev, "invalid Assoclen:%u\n", req->assoclen);
  1956. goto out;
  1957. }
  1958. memset(areq_ctx, 0, sizeof(*areq_ctx));
  1959. /* No generated IV required */
  1960. areq_ctx->backup_iv = req->iv;
  1961. areq_ctx->assoclen = req->assoclen;
  1962. areq_ctx->backup_giv = NULL;
  1963. areq_ctx->plaintext_authenticate_only = false;
  1964. cc_proc_rfc4_gcm(req);
  1965. areq_ctx->is_gcm4543 = true;
  1966. rc = cc_proc_aead(req, DRV_CRYPTO_DIRECTION_ENCRYPT);
  1967. if (rc != -EINPROGRESS && rc != -EBUSY)
  1968. req->iv = areq_ctx->backup_iv;
  1969. out:
  1970. return rc;
  1971. }
  1972. static int cc_rfc4543_gcm_encrypt(struct aead_request *req)
  1973. {
  1974. /* Very similar to cc_aead_encrypt() above. */
  1975. struct aead_req_ctx *areq_ctx = aead_request_ctx(req);
  1976. int rc;
  1977. memset(areq_ctx, 0, sizeof(*areq_ctx));
  1978. //plaintext is not encryped with rfc4543
  1979. areq_ctx->plaintext_authenticate_only = true;
  1980. /* No generated IV required */
  1981. areq_ctx->backup_iv = req->iv;
  1982. areq_ctx->assoclen = req->assoclen;
  1983. areq_ctx->backup_giv = NULL;
  1984. cc_proc_rfc4_gcm(req);
  1985. areq_ctx->is_gcm4543 = true;
  1986. rc = cc_proc_aead(req, DRV_CRYPTO_DIRECTION_ENCRYPT);
  1987. if (rc != -EINPROGRESS && rc != -EBUSY)
  1988. req->iv = areq_ctx->backup_iv;
  1989. return rc;
  1990. }
  1991. static int cc_rfc4106_gcm_decrypt(struct aead_request *req)
  1992. {
  1993. /* Very similar to cc_aead_decrypt() above. */
  1994. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  1995. struct cc_aead_ctx *ctx = crypto_aead_ctx(tfm);
  1996. struct device *dev = drvdata_to_dev(ctx->drvdata);
  1997. struct aead_req_ctx *areq_ctx = aead_request_ctx(req);
  1998. int rc = -EINVAL;
  1999. if (!valid_assoclen(req)) {
  2000. dev_err(dev, "invalid Assoclen:%u\n", req->assoclen);
  2001. goto out;
  2002. }
  2003. memset(areq_ctx, 0, sizeof(*areq_ctx));
  2004. /* No generated IV required */
  2005. areq_ctx->backup_iv = req->iv;
  2006. areq_ctx->assoclen = req->assoclen;
  2007. areq_ctx->backup_giv = NULL;
  2008. areq_ctx->plaintext_authenticate_only = false;
  2009. cc_proc_rfc4_gcm(req);
  2010. areq_ctx->is_gcm4543 = true;
  2011. rc = cc_proc_aead(req, DRV_CRYPTO_DIRECTION_DECRYPT);
  2012. if (rc != -EINPROGRESS && rc != -EBUSY)
  2013. req->iv = areq_ctx->backup_iv;
  2014. out:
  2015. return rc;
  2016. }
  2017. static int cc_rfc4543_gcm_decrypt(struct aead_request *req)
  2018. {
  2019. /* Very similar to cc_aead_decrypt() above. */
  2020. struct aead_req_ctx *areq_ctx = aead_request_ctx(req);
  2021. int rc;
  2022. memset(areq_ctx, 0, sizeof(*areq_ctx));
  2023. //plaintext is not decryped with rfc4543
  2024. areq_ctx->plaintext_authenticate_only = true;
  2025. /* No generated IV required */
  2026. areq_ctx->backup_iv = req->iv;
  2027. areq_ctx->assoclen = req->assoclen;
  2028. areq_ctx->backup_giv = NULL;
  2029. cc_proc_rfc4_gcm(req);
  2030. areq_ctx->is_gcm4543 = true;
  2031. rc = cc_proc_aead(req, DRV_CRYPTO_DIRECTION_DECRYPT);
  2032. if (rc != -EINPROGRESS && rc != -EBUSY)
  2033. req->iv = areq_ctx->backup_iv;
  2034. return rc;
  2035. }
  2036. /* aead alg */
  2037. static struct cc_alg_template aead_algs[] = {
  2038. {
  2039. .name = "authenc(hmac(sha1),cbc(aes))",
  2040. .driver_name = "authenc-hmac-sha1-cbc-aes-ccree",
  2041. .blocksize = AES_BLOCK_SIZE,
  2042. .template_aead = {
  2043. .setkey = cc_aead_setkey,
  2044. .setauthsize = cc_aead_setauthsize,
  2045. .encrypt = cc_aead_encrypt,
  2046. .decrypt = cc_aead_decrypt,
  2047. .init = cc_aead_init,
  2048. .exit = cc_aead_exit,
  2049. .ivsize = AES_BLOCK_SIZE,
  2050. .maxauthsize = SHA1_DIGEST_SIZE,
  2051. },
  2052. .cipher_mode = DRV_CIPHER_CBC,
  2053. .flow_mode = S_DIN_to_AES,
  2054. .auth_mode = DRV_HASH_SHA1,
  2055. .min_hw_rev = CC_HW_REV_630,
  2056. },
  2057. {
  2058. .name = "authenc(hmac(sha1),cbc(des3_ede))",
  2059. .driver_name = "authenc-hmac-sha1-cbc-des3-ccree",
  2060. .blocksize = DES3_EDE_BLOCK_SIZE,
  2061. .template_aead = {
  2062. .setkey = cc_aead_setkey,
  2063. .setauthsize = cc_aead_setauthsize,
  2064. .encrypt = cc_aead_encrypt,
  2065. .decrypt = cc_aead_decrypt,
  2066. .init = cc_aead_init,
  2067. .exit = cc_aead_exit,
  2068. .ivsize = DES3_EDE_BLOCK_SIZE,
  2069. .maxauthsize = SHA1_DIGEST_SIZE,
  2070. },
  2071. .cipher_mode = DRV_CIPHER_CBC,
  2072. .flow_mode = S_DIN_to_DES,
  2073. .auth_mode = DRV_HASH_SHA1,
  2074. .min_hw_rev = CC_HW_REV_630,
  2075. },
  2076. {
  2077. .name = "authenc(hmac(sha256),cbc(aes))",
  2078. .driver_name = "authenc-hmac-sha256-cbc-aes-ccree",
  2079. .blocksize = AES_BLOCK_SIZE,
  2080. .template_aead = {
  2081. .setkey = cc_aead_setkey,
  2082. .setauthsize = cc_aead_setauthsize,
  2083. .encrypt = cc_aead_encrypt,
  2084. .decrypt = cc_aead_decrypt,
  2085. .init = cc_aead_init,
  2086. .exit = cc_aead_exit,
  2087. .ivsize = AES_BLOCK_SIZE,
  2088. .maxauthsize = SHA256_DIGEST_SIZE,
  2089. },
  2090. .cipher_mode = DRV_CIPHER_CBC,
  2091. .flow_mode = S_DIN_to_AES,
  2092. .auth_mode = DRV_HASH_SHA256,
  2093. .min_hw_rev = CC_HW_REV_630,
  2094. },
  2095. {
  2096. .name = "authenc(hmac(sha256),cbc(des3_ede))",
  2097. .driver_name = "authenc-hmac-sha256-cbc-des3-ccree",
  2098. .blocksize = DES3_EDE_BLOCK_SIZE,
  2099. .template_aead = {
  2100. .setkey = cc_aead_setkey,
  2101. .setauthsize = cc_aead_setauthsize,
  2102. .encrypt = cc_aead_encrypt,
  2103. .decrypt = cc_aead_decrypt,
  2104. .init = cc_aead_init,
  2105. .exit = cc_aead_exit,
  2106. .ivsize = DES3_EDE_BLOCK_SIZE,
  2107. .maxauthsize = SHA256_DIGEST_SIZE,
  2108. },
  2109. .cipher_mode = DRV_CIPHER_CBC,
  2110. .flow_mode = S_DIN_to_DES,
  2111. .auth_mode = DRV_HASH_SHA256,
  2112. .min_hw_rev = CC_HW_REV_630,
  2113. },
  2114. {
  2115. .name = "authenc(xcbc(aes),cbc(aes))",
  2116. .driver_name = "authenc-xcbc-aes-cbc-aes-ccree",
  2117. .blocksize = AES_BLOCK_SIZE,
  2118. .template_aead = {
  2119. .setkey = cc_aead_setkey,
  2120. .setauthsize = cc_aead_setauthsize,
  2121. .encrypt = cc_aead_encrypt,
  2122. .decrypt = cc_aead_decrypt,
  2123. .init = cc_aead_init,
  2124. .exit = cc_aead_exit,
  2125. .ivsize = AES_BLOCK_SIZE,
  2126. .maxauthsize = AES_BLOCK_SIZE,
  2127. },
  2128. .cipher_mode = DRV_CIPHER_CBC,
  2129. .flow_mode = S_DIN_to_AES,
  2130. .auth_mode = DRV_HASH_XCBC_MAC,
  2131. .min_hw_rev = CC_HW_REV_630,
  2132. },
  2133. {
  2134. .name = "authenc(hmac(sha1),rfc3686(ctr(aes)))",
  2135. .driver_name = "authenc-hmac-sha1-rfc3686-ctr-aes-ccree",
  2136. .blocksize = 1,
  2137. .template_aead = {
  2138. .setkey = cc_aead_setkey,
  2139. .setauthsize = cc_aead_setauthsize,
  2140. .encrypt = cc_aead_encrypt,
  2141. .decrypt = cc_aead_decrypt,
  2142. .init = cc_aead_init,
  2143. .exit = cc_aead_exit,
  2144. .ivsize = CTR_RFC3686_IV_SIZE,
  2145. .maxauthsize = SHA1_DIGEST_SIZE,
  2146. },
  2147. .cipher_mode = DRV_CIPHER_CTR,
  2148. .flow_mode = S_DIN_to_AES,
  2149. .auth_mode = DRV_HASH_SHA1,
  2150. .min_hw_rev = CC_HW_REV_630,
  2151. },
  2152. {
  2153. .name = "authenc(hmac(sha256),rfc3686(ctr(aes)))",
  2154. .driver_name = "authenc-hmac-sha256-rfc3686-ctr-aes-ccree",
  2155. .blocksize = 1,
  2156. .template_aead = {
  2157. .setkey = cc_aead_setkey,
  2158. .setauthsize = cc_aead_setauthsize,
  2159. .encrypt = cc_aead_encrypt,
  2160. .decrypt = cc_aead_decrypt,
  2161. .init = cc_aead_init,
  2162. .exit = cc_aead_exit,
  2163. .ivsize = CTR_RFC3686_IV_SIZE,
  2164. .maxauthsize = SHA256_DIGEST_SIZE,
  2165. },
  2166. .cipher_mode = DRV_CIPHER_CTR,
  2167. .flow_mode = S_DIN_to_AES,
  2168. .auth_mode = DRV_HASH_SHA256,
  2169. .min_hw_rev = CC_HW_REV_630,
  2170. },
  2171. {
  2172. .name = "authenc(xcbc(aes),rfc3686(ctr(aes)))",
  2173. .driver_name = "authenc-xcbc-aes-rfc3686-ctr-aes-ccree",
  2174. .blocksize = 1,
  2175. .template_aead = {
  2176. .setkey = cc_aead_setkey,
  2177. .setauthsize = cc_aead_setauthsize,
  2178. .encrypt = cc_aead_encrypt,
  2179. .decrypt = cc_aead_decrypt,
  2180. .init = cc_aead_init,
  2181. .exit = cc_aead_exit,
  2182. .ivsize = CTR_RFC3686_IV_SIZE,
  2183. .maxauthsize = AES_BLOCK_SIZE,
  2184. },
  2185. .cipher_mode = DRV_CIPHER_CTR,
  2186. .flow_mode = S_DIN_to_AES,
  2187. .auth_mode = DRV_HASH_XCBC_MAC,
  2188. .min_hw_rev = CC_HW_REV_630,
  2189. },
  2190. {
  2191. .name = "ccm(aes)",
  2192. .driver_name = "ccm-aes-ccree",
  2193. .blocksize = 1,
  2194. .template_aead = {
  2195. .setkey = cc_aead_setkey,
  2196. .setauthsize = cc_ccm_setauthsize,
  2197. .encrypt = cc_aead_encrypt,
  2198. .decrypt = cc_aead_decrypt,
  2199. .init = cc_aead_init,
  2200. .exit = cc_aead_exit,
  2201. .ivsize = AES_BLOCK_SIZE,
  2202. .maxauthsize = AES_BLOCK_SIZE,
  2203. },
  2204. .cipher_mode = DRV_CIPHER_CCM,
  2205. .flow_mode = S_DIN_to_AES,
  2206. .auth_mode = DRV_HASH_NULL,
  2207. .min_hw_rev = CC_HW_REV_630,
  2208. },
  2209. {
  2210. .name = "rfc4309(ccm(aes))",
  2211. .driver_name = "rfc4309-ccm-aes-ccree",
  2212. .blocksize = 1,
  2213. .template_aead = {
  2214. .setkey = cc_rfc4309_ccm_setkey,
  2215. .setauthsize = cc_rfc4309_ccm_setauthsize,
  2216. .encrypt = cc_rfc4309_ccm_encrypt,
  2217. .decrypt = cc_rfc4309_ccm_decrypt,
  2218. .init = cc_aead_init,
  2219. .exit = cc_aead_exit,
  2220. .ivsize = CCM_BLOCK_IV_SIZE,
  2221. .maxauthsize = AES_BLOCK_SIZE,
  2222. },
  2223. .cipher_mode = DRV_CIPHER_CCM,
  2224. .flow_mode = S_DIN_to_AES,
  2225. .auth_mode = DRV_HASH_NULL,
  2226. .min_hw_rev = CC_HW_REV_630,
  2227. },
  2228. {
  2229. .name = "gcm(aes)",
  2230. .driver_name = "gcm-aes-ccree",
  2231. .blocksize = 1,
  2232. .template_aead = {
  2233. .setkey = cc_aead_setkey,
  2234. .setauthsize = cc_gcm_setauthsize,
  2235. .encrypt = cc_aead_encrypt,
  2236. .decrypt = cc_aead_decrypt,
  2237. .init = cc_aead_init,
  2238. .exit = cc_aead_exit,
  2239. .ivsize = 12,
  2240. .maxauthsize = AES_BLOCK_SIZE,
  2241. },
  2242. .cipher_mode = DRV_CIPHER_GCTR,
  2243. .flow_mode = S_DIN_to_AES,
  2244. .auth_mode = DRV_HASH_NULL,
  2245. .min_hw_rev = CC_HW_REV_630,
  2246. },
  2247. {
  2248. .name = "rfc4106(gcm(aes))",
  2249. .driver_name = "rfc4106-gcm-aes-ccree",
  2250. .blocksize = 1,
  2251. .template_aead = {
  2252. .setkey = cc_rfc4106_gcm_setkey,
  2253. .setauthsize = cc_rfc4106_gcm_setauthsize,
  2254. .encrypt = cc_rfc4106_gcm_encrypt,
  2255. .decrypt = cc_rfc4106_gcm_decrypt,
  2256. .init = cc_aead_init,
  2257. .exit = cc_aead_exit,
  2258. .ivsize = GCM_BLOCK_RFC4_IV_SIZE,
  2259. .maxauthsize = AES_BLOCK_SIZE,
  2260. },
  2261. .cipher_mode = DRV_CIPHER_GCTR,
  2262. .flow_mode = S_DIN_to_AES,
  2263. .auth_mode = DRV_HASH_NULL,
  2264. .min_hw_rev = CC_HW_REV_630,
  2265. },
  2266. {
  2267. .name = "rfc4543(gcm(aes))",
  2268. .driver_name = "rfc4543-gcm-aes-ccree",
  2269. .blocksize = 1,
  2270. .template_aead = {
  2271. .setkey = cc_rfc4543_gcm_setkey,
  2272. .setauthsize = cc_rfc4543_gcm_setauthsize,
  2273. .encrypt = cc_rfc4543_gcm_encrypt,
  2274. .decrypt = cc_rfc4543_gcm_decrypt,
  2275. .init = cc_aead_init,
  2276. .exit = cc_aead_exit,
  2277. .ivsize = GCM_BLOCK_RFC4_IV_SIZE,
  2278. .maxauthsize = AES_BLOCK_SIZE,
  2279. },
  2280. .cipher_mode = DRV_CIPHER_GCTR,
  2281. .flow_mode = S_DIN_to_AES,
  2282. .auth_mode = DRV_HASH_NULL,
  2283. .min_hw_rev = CC_HW_REV_630,
  2284. },
  2285. };
  2286. static struct cc_crypto_alg *cc_create_aead_alg(struct cc_alg_template *tmpl,
  2287. struct device *dev)
  2288. {
  2289. struct cc_crypto_alg *t_alg;
  2290. struct aead_alg *alg;
  2291. t_alg = kzalloc(sizeof(*t_alg), GFP_KERNEL);
  2292. if (!t_alg)
  2293. return ERR_PTR(-ENOMEM);
  2294. alg = &tmpl->template_aead;
  2295. snprintf(alg->base.cra_name, CRYPTO_MAX_ALG_NAME, "%s", tmpl->name);
  2296. snprintf(alg->base.cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
  2297. tmpl->driver_name);
  2298. alg->base.cra_module = THIS_MODULE;
  2299. alg->base.cra_priority = CC_CRA_PRIO;
  2300. alg->base.cra_ctxsize = sizeof(struct cc_aead_ctx);
  2301. alg->base.cra_flags = CRYPTO_ALG_ASYNC | CRYPTO_ALG_KERN_DRIVER_ONLY;
  2302. alg->init = cc_aead_init;
  2303. alg->exit = cc_aead_exit;
  2304. t_alg->aead_alg = *alg;
  2305. t_alg->cipher_mode = tmpl->cipher_mode;
  2306. t_alg->flow_mode = tmpl->flow_mode;
  2307. t_alg->auth_mode = tmpl->auth_mode;
  2308. return t_alg;
  2309. }
  2310. int cc_aead_free(struct cc_drvdata *drvdata)
  2311. {
  2312. struct cc_crypto_alg *t_alg, *n;
  2313. struct cc_aead_handle *aead_handle =
  2314. (struct cc_aead_handle *)drvdata->aead_handle;
  2315. if (aead_handle) {
  2316. /* Remove registered algs */
  2317. list_for_each_entry_safe(t_alg, n, &aead_handle->aead_list,
  2318. entry) {
  2319. crypto_unregister_aead(&t_alg->aead_alg);
  2320. list_del(&t_alg->entry);
  2321. kfree(t_alg);
  2322. }
  2323. kfree(aead_handle);
  2324. drvdata->aead_handle = NULL;
  2325. }
  2326. return 0;
  2327. }
  2328. int cc_aead_alloc(struct cc_drvdata *drvdata)
  2329. {
  2330. struct cc_aead_handle *aead_handle;
  2331. struct cc_crypto_alg *t_alg;
  2332. int rc = -ENOMEM;
  2333. int alg;
  2334. struct device *dev = drvdata_to_dev(drvdata);
  2335. aead_handle = kmalloc(sizeof(*aead_handle), GFP_KERNEL);
  2336. if (!aead_handle) {
  2337. rc = -ENOMEM;
  2338. goto fail0;
  2339. }
  2340. INIT_LIST_HEAD(&aead_handle->aead_list);
  2341. drvdata->aead_handle = aead_handle;
  2342. aead_handle->sram_workspace_addr = cc_sram_alloc(drvdata,
  2343. MAX_HMAC_DIGEST_SIZE);
  2344. if (aead_handle->sram_workspace_addr == NULL_SRAM_ADDR) {
  2345. dev_err(dev, "SRAM pool exhausted\n");
  2346. rc = -ENOMEM;
  2347. goto fail1;
  2348. }
  2349. /* Linux crypto */
  2350. for (alg = 0; alg < ARRAY_SIZE(aead_algs); alg++) {
  2351. if (aead_algs[alg].min_hw_rev > drvdata->hw_rev)
  2352. continue;
  2353. t_alg = cc_create_aead_alg(&aead_algs[alg], dev);
  2354. if (IS_ERR(t_alg)) {
  2355. rc = PTR_ERR(t_alg);
  2356. dev_err(dev, "%s alg allocation failed\n",
  2357. aead_algs[alg].driver_name);
  2358. goto fail1;
  2359. }
  2360. t_alg->drvdata = drvdata;
  2361. rc = crypto_register_aead(&t_alg->aead_alg);
  2362. if (rc) {
  2363. dev_err(dev, "%s alg registration failed\n",
  2364. t_alg->aead_alg.base.cra_driver_name);
  2365. goto fail2;
  2366. } else {
  2367. list_add_tail(&t_alg->entry, &aead_handle->aead_list);
  2368. dev_dbg(dev, "Registered %s\n",
  2369. t_alg->aead_alg.base.cra_driver_name);
  2370. }
  2371. }
  2372. return 0;
  2373. fail2:
  2374. kfree(t_alg);
  2375. fail1:
  2376. cc_aead_free(drvdata);
  2377. fail0:
  2378. return rc;
  2379. }