cc_aead.c 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712
  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->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. unsigned int idx = *seq_size;
  951. /* Hash associated data */
  952. if (req->assoclen > 0)
  953. cc_set_assoc_desc(req, DIN_HASH, desc, &idx);
  954. /* Hash IV */
  955. *seq_size = idx;
  956. }
  957. static void cc_proc_scheme_desc(struct aead_request *req,
  958. struct cc_hw_desc desc[],
  959. unsigned int *seq_size)
  960. {
  961. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  962. struct cc_aead_ctx *ctx = crypto_aead_ctx(tfm);
  963. struct cc_aead_handle *aead_handle = ctx->drvdata->aead_handle;
  964. unsigned int hash_mode = (ctx->auth_mode == DRV_HASH_SHA1) ?
  965. DRV_HASH_HW_SHA1 : DRV_HASH_HW_SHA256;
  966. unsigned int digest_size = (ctx->auth_mode == DRV_HASH_SHA1) ?
  967. CC_SHA1_DIGEST_SIZE : CC_SHA256_DIGEST_SIZE;
  968. unsigned int idx = *seq_size;
  969. hw_desc_init(&desc[idx]);
  970. set_cipher_mode(&desc[idx], hash_mode);
  971. set_dout_sram(&desc[idx], aead_handle->sram_workspace_addr,
  972. ctx->drvdata->hash_len_sz);
  973. set_flow_mode(&desc[idx], S_HASH_to_DOUT);
  974. set_setup_mode(&desc[idx], SETUP_WRITE_STATE1);
  975. set_cipher_do(&desc[idx], DO_PAD);
  976. idx++;
  977. /* Get final ICV result */
  978. hw_desc_init(&desc[idx]);
  979. set_dout_sram(&desc[idx], aead_handle->sram_workspace_addr,
  980. digest_size);
  981. set_flow_mode(&desc[idx], S_HASH_to_DOUT);
  982. set_setup_mode(&desc[idx], SETUP_WRITE_STATE0);
  983. set_cipher_config0(&desc[idx], HASH_DIGEST_RESULT_LITTLE_ENDIAN);
  984. set_cipher_mode(&desc[idx], hash_mode);
  985. idx++;
  986. /* Loading hash opad xor key state */
  987. hw_desc_init(&desc[idx]);
  988. set_cipher_mode(&desc[idx], hash_mode);
  989. set_din_type(&desc[idx], DMA_DLLI,
  990. (ctx->auth_state.hmac.ipad_opad_dma_addr + digest_size),
  991. digest_size, NS_BIT);
  992. set_flow_mode(&desc[idx], S_DIN_to_HASH);
  993. set_setup_mode(&desc[idx], SETUP_LOAD_STATE0);
  994. idx++;
  995. /* Load init. digest len (64 bytes) */
  996. hw_desc_init(&desc[idx]);
  997. set_cipher_mode(&desc[idx], hash_mode);
  998. set_din_sram(&desc[idx], cc_digest_len_addr(ctx->drvdata, hash_mode),
  999. ctx->drvdata->hash_len_sz);
  1000. set_cipher_config1(&desc[idx], HASH_PADDING_ENABLED);
  1001. set_flow_mode(&desc[idx], S_DIN_to_HASH);
  1002. set_setup_mode(&desc[idx], SETUP_LOAD_KEY0);
  1003. idx++;
  1004. /* Perform HASH update */
  1005. hw_desc_init(&desc[idx]);
  1006. set_din_sram(&desc[idx], aead_handle->sram_workspace_addr,
  1007. digest_size);
  1008. set_flow_mode(&desc[idx], DIN_HASH);
  1009. idx++;
  1010. *seq_size = idx;
  1011. }
  1012. static void cc_mlli_to_sram(struct aead_request *req,
  1013. struct cc_hw_desc desc[], unsigned int *seq_size)
  1014. {
  1015. struct aead_req_ctx *req_ctx = aead_request_ctx(req);
  1016. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  1017. struct cc_aead_ctx *ctx = crypto_aead_ctx(tfm);
  1018. struct device *dev = drvdata_to_dev(ctx->drvdata);
  1019. if (req_ctx->assoc_buff_type == CC_DMA_BUF_MLLI ||
  1020. req_ctx->data_buff_type == CC_DMA_BUF_MLLI ||
  1021. !req_ctx->is_single_pass) {
  1022. dev_dbg(dev, "Copy-to-sram: mlli_dma=%08x, mlli_size=%u\n",
  1023. (unsigned int)ctx->drvdata->mlli_sram_addr,
  1024. req_ctx->mlli_params.mlli_len);
  1025. /* Copy MLLI table host-to-sram */
  1026. hw_desc_init(&desc[*seq_size]);
  1027. set_din_type(&desc[*seq_size], DMA_DLLI,
  1028. req_ctx->mlli_params.mlli_dma_addr,
  1029. req_ctx->mlli_params.mlli_len, NS_BIT);
  1030. set_dout_sram(&desc[*seq_size],
  1031. ctx->drvdata->mlli_sram_addr,
  1032. req_ctx->mlli_params.mlli_len);
  1033. set_flow_mode(&desc[*seq_size], BYPASS);
  1034. (*seq_size)++;
  1035. }
  1036. }
  1037. static enum cc_flow_mode cc_get_data_flow(enum drv_crypto_direction direct,
  1038. enum cc_flow_mode setup_flow_mode,
  1039. bool is_single_pass)
  1040. {
  1041. enum cc_flow_mode data_flow_mode;
  1042. if (direct == DRV_CRYPTO_DIRECTION_ENCRYPT) {
  1043. if (setup_flow_mode == S_DIN_to_AES)
  1044. data_flow_mode = is_single_pass ?
  1045. AES_to_HASH_and_DOUT : DIN_AES_DOUT;
  1046. else
  1047. data_flow_mode = is_single_pass ?
  1048. DES_to_HASH_and_DOUT : DIN_DES_DOUT;
  1049. } else { /* Decrypt */
  1050. if (setup_flow_mode == S_DIN_to_AES)
  1051. data_flow_mode = is_single_pass ?
  1052. AES_and_HASH : DIN_AES_DOUT;
  1053. else
  1054. data_flow_mode = is_single_pass ?
  1055. DES_and_HASH : DIN_DES_DOUT;
  1056. }
  1057. return data_flow_mode;
  1058. }
  1059. static void cc_hmac_authenc(struct aead_request *req, struct cc_hw_desc desc[],
  1060. unsigned int *seq_size)
  1061. {
  1062. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  1063. struct cc_aead_ctx *ctx = crypto_aead_ctx(tfm);
  1064. struct aead_req_ctx *req_ctx = aead_request_ctx(req);
  1065. int direct = req_ctx->gen_ctx.op_type;
  1066. unsigned int data_flow_mode =
  1067. cc_get_data_flow(direct, ctx->flow_mode,
  1068. req_ctx->is_single_pass);
  1069. if (req_ctx->is_single_pass) {
  1070. /**
  1071. * Single-pass flow
  1072. */
  1073. cc_set_hmac_desc(req, desc, seq_size);
  1074. cc_set_cipher_desc(req, desc, seq_size);
  1075. cc_proc_header_desc(req, desc, seq_size);
  1076. cc_proc_cipher_desc(req, data_flow_mode, desc, seq_size);
  1077. cc_proc_scheme_desc(req, desc, seq_size);
  1078. cc_proc_digest_desc(req, desc, seq_size);
  1079. return;
  1080. }
  1081. /**
  1082. * Double-pass flow
  1083. * Fallback for unsupported single-pass modes,
  1084. * i.e. using assoc. data of non-word-multiple
  1085. */
  1086. if (direct == DRV_CRYPTO_DIRECTION_ENCRYPT) {
  1087. /* encrypt first.. */
  1088. cc_proc_cipher(req, desc, seq_size, data_flow_mode);
  1089. /* authenc after..*/
  1090. cc_set_hmac_desc(req, desc, seq_size);
  1091. cc_proc_authen_desc(req, DIN_HASH, desc, seq_size, direct);
  1092. cc_proc_scheme_desc(req, desc, seq_size);
  1093. cc_proc_digest_desc(req, desc, seq_size);
  1094. } else { /*DECRYPT*/
  1095. /* authenc first..*/
  1096. cc_set_hmac_desc(req, desc, seq_size);
  1097. cc_proc_authen_desc(req, DIN_HASH, desc, seq_size, direct);
  1098. cc_proc_scheme_desc(req, desc, seq_size);
  1099. /* decrypt after.. */
  1100. cc_proc_cipher(req, desc, seq_size, data_flow_mode);
  1101. /* read the digest result with setting the completion bit
  1102. * must be after the cipher operation
  1103. */
  1104. cc_proc_digest_desc(req, desc, seq_size);
  1105. }
  1106. }
  1107. static void
  1108. cc_xcbc_authenc(struct aead_request *req, struct cc_hw_desc desc[],
  1109. unsigned int *seq_size)
  1110. {
  1111. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  1112. struct cc_aead_ctx *ctx = crypto_aead_ctx(tfm);
  1113. struct aead_req_ctx *req_ctx = aead_request_ctx(req);
  1114. int direct = req_ctx->gen_ctx.op_type;
  1115. unsigned int data_flow_mode =
  1116. cc_get_data_flow(direct, ctx->flow_mode,
  1117. req_ctx->is_single_pass);
  1118. if (req_ctx->is_single_pass) {
  1119. /**
  1120. * Single-pass flow
  1121. */
  1122. cc_set_xcbc_desc(req, desc, seq_size);
  1123. cc_set_cipher_desc(req, desc, seq_size);
  1124. cc_proc_header_desc(req, desc, seq_size);
  1125. cc_proc_cipher_desc(req, data_flow_mode, desc, seq_size);
  1126. cc_proc_digest_desc(req, desc, seq_size);
  1127. return;
  1128. }
  1129. /**
  1130. * Double-pass flow
  1131. * Fallback for unsupported single-pass modes,
  1132. * i.e. using assoc. data of non-word-multiple
  1133. */
  1134. if (direct == DRV_CRYPTO_DIRECTION_ENCRYPT) {
  1135. /* encrypt first.. */
  1136. cc_proc_cipher(req, desc, seq_size, data_flow_mode);
  1137. /* authenc after.. */
  1138. cc_set_xcbc_desc(req, desc, seq_size);
  1139. cc_proc_authen_desc(req, DIN_HASH, desc, seq_size, direct);
  1140. cc_proc_digest_desc(req, desc, seq_size);
  1141. } else { /*DECRYPT*/
  1142. /* authenc first.. */
  1143. cc_set_xcbc_desc(req, desc, seq_size);
  1144. cc_proc_authen_desc(req, DIN_HASH, desc, seq_size, direct);
  1145. /* decrypt after..*/
  1146. cc_proc_cipher(req, desc, seq_size, data_flow_mode);
  1147. /* read the digest result with setting the completion bit
  1148. * must be after the cipher operation
  1149. */
  1150. cc_proc_digest_desc(req, desc, seq_size);
  1151. }
  1152. }
  1153. static int validate_data_size(struct cc_aead_ctx *ctx,
  1154. enum drv_crypto_direction direct,
  1155. struct aead_request *req)
  1156. {
  1157. struct aead_req_ctx *areq_ctx = aead_request_ctx(req);
  1158. struct device *dev = drvdata_to_dev(ctx->drvdata);
  1159. unsigned int assoclen = req->assoclen;
  1160. unsigned int cipherlen = (direct == DRV_CRYPTO_DIRECTION_DECRYPT) ?
  1161. (req->cryptlen - ctx->authsize) : req->cryptlen;
  1162. if (direct == DRV_CRYPTO_DIRECTION_DECRYPT &&
  1163. req->cryptlen < ctx->authsize)
  1164. goto data_size_err;
  1165. areq_ctx->is_single_pass = true; /*defaulted to fast flow*/
  1166. switch (ctx->flow_mode) {
  1167. case S_DIN_to_AES:
  1168. if (ctx->cipher_mode == DRV_CIPHER_CBC &&
  1169. !IS_ALIGNED(cipherlen, AES_BLOCK_SIZE))
  1170. goto data_size_err;
  1171. if (ctx->cipher_mode == DRV_CIPHER_CCM)
  1172. break;
  1173. if (ctx->cipher_mode == DRV_CIPHER_GCTR) {
  1174. if (areq_ctx->plaintext_authenticate_only)
  1175. areq_ctx->is_single_pass = false;
  1176. break;
  1177. }
  1178. if (!IS_ALIGNED(assoclen, sizeof(u32)))
  1179. areq_ctx->is_single_pass = false;
  1180. if (ctx->cipher_mode == DRV_CIPHER_CTR &&
  1181. !IS_ALIGNED(cipherlen, sizeof(u32)))
  1182. areq_ctx->is_single_pass = false;
  1183. break;
  1184. case S_DIN_to_DES:
  1185. if (!IS_ALIGNED(cipherlen, DES_BLOCK_SIZE))
  1186. goto data_size_err;
  1187. if (!IS_ALIGNED(assoclen, DES_BLOCK_SIZE))
  1188. areq_ctx->is_single_pass = false;
  1189. break;
  1190. default:
  1191. dev_err(dev, "Unexpected flow mode (%d)\n", ctx->flow_mode);
  1192. goto data_size_err;
  1193. }
  1194. return 0;
  1195. data_size_err:
  1196. return -EINVAL;
  1197. }
  1198. static unsigned int format_ccm_a0(u8 *pa0_buff, u32 header_size)
  1199. {
  1200. unsigned int len = 0;
  1201. if (header_size == 0)
  1202. return 0;
  1203. if (header_size < ((1UL << 16) - (1UL << 8))) {
  1204. len = 2;
  1205. pa0_buff[0] = (header_size >> 8) & 0xFF;
  1206. pa0_buff[1] = header_size & 0xFF;
  1207. } else {
  1208. len = 6;
  1209. pa0_buff[0] = 0xFF;
  1210. pa0_buff[1] = 0xFE;
  1211. pa0_buff[2] = (header_size >> 24) & 0xFF;
  1212. pa0_buff[3] = (header_size >> 16) & 0xFF;
  1213. pa0_buff[4] = (header_size >> 8) & 0xFF;
  1214. pa0_buff[5] = header_size & 0xFF;
  1215. }
  1216. return len;
  1217. }
  1218. static int set_msg_len(u8 *block, unsigned int msglen, unsigned int csize)
  1219. {
  1220. __be32 data;
  1221. memset(block, 0, csize);
  1222. block += csize;
  1223. if (csize >= 4)
  1224. csize = 4;
  1225. else if (msglen > (1 << (8 * csize)))
  1226. return -EOVERFLOW;
  1227. data = cpu_to_be32(msglen);
  1228. memcpy(block - csize, (u8 *)&data + 4 - csize, csize);
  1229. return 0;
  1230. }
  1231. static int cc_ccm(struct aead_request *req, struct cc_hw_desc desc[],
  1232. unsigned int *seq_size)
  1233. {
  1234. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  1235. struct cc_aead_ctx *ctx = crypto_aead_ctx(tfm);
  1236. struct aead_req_ctx *req_ctx = aead_request_ctx(req);
  1237. unsigned int idx = *seq_size;
  1238. unsigned int cipher_flow_mode;
  1239. dma_addr_t mac_result;
  1240. if (req_ctx->gen_ctx.op_type == DRV_CRYPTO_DIRECTION_DECRYPT) {
  1241. cipher_flow_mode = AES_to_HASH_and_DOUT;
  1242. mac_result = req_ctx->mac_buf_dma_addr;
  1243. } else { /* Encrypt */
  1244. cipher_flow_mode = AES_and_HASH;
  1245. mac_result = req_ctx->icv_dma_addr;
  1246. }
  1247. /* load key */
  1248. hw_desc_init(&desc[idx]);
  1249. set_cipher_mode(&desc[idx], DRV_CIPHER_CTR);
  1250. set_din_type(&desc[idx], DMA_DLLI, ctx->enckey_dma_addr,
  1251. ((ctx->enc_keylen == 24) ? CC_AES_KEY_SIZE_MAX :
  1252. ctx->enc_keylen), NS_BIT);
  1253. set_key_size_aes(&desc[idx], ctx->enc_keylen);
  1254. set_setup_mode(&desc[idx], SETUP_LOAD_KEY0);
  1255. set_cipher_config0(&desc[idx], DESC_DIRECTION_ENCRYPT_ENCRYPT);
  1256. set_flow_mode(&desc[idx], S_DIN_to_AES);
  1257. idx++;
  1258. /* load ctr state */
  1259. hw_desc_init(&desc[idx]);
  1260. set_cipher_mode(&desc[idx], DRV_CIPHER_CTR);
  1261. set_key_size_aes(&desc[idx], ctx->enc_keylen);
  1262. set_din_type(&desc[idx], DMA_DLLI,
  1263. req_ctx->gen_ctx.iv_dma_addr, AES_BLOCK_SIZE, NS_BIT);
  1264. set_cipher_config0(&desc[idx], DESC_DIRECTION_ENCRYPT_ENCRYPT);
  1265. set_setup_mode(&desc[idx], SETUP_LOAD_STATE1);
  1266. set_flow_mode(&desc[idx], S_DIN_to_AES);
  1267. idx++;
  1268. /* load MAC key */
  1269. hw_desc_init(&desc[idx]);
  1270. set_cipher_mode(&desc[idx], DRV_CIPHER_CBC_MAC);
  1271. set_din_type(&desc[idx], DMA_DLLI, ctx->enckey_dma_addr,
  1272. ((ctx->enc_keylen == 24) ? CC_AES_KEY_SIZE_MAX :
  1273. ctx->enc_keylen), NS_BIT);
  1274. set_key_size_aes(&desc[idx], ctx->enc_keylen);
  1275. set_setup_mode(&desc[idx], SETUP_LOAD_KEY0);
  1276. set_cipher_config0(&desc[idx], DESC_DIRECTION_ENCRYPT_ENCRYPT);
  1277. set_flow_mode(&desc[idx], S_DIN_to_HASH);
  1278. set_aes_not_hash_mode(&desc[idx]);
  1279. idx++;
  1280. /* load MAC state */
  1281. hw_desc_init(&desc[idx]);
  1282. set_cipher_mode(&desc[idx], DRV_CIPHER_CBC_MAC);
  1283. set_key_size_aes(&desc[idx], ctx->enc_keylen);
  1284. set_din_type(&desc[idx], DMA_DLLI, req_ctx->mac_buf_dma_addr,
  1285. AES_BLOCK_SIZE, NS_BIT);
  1286. set_cipher_config0(&desc[idx], DESC_DIRECTION_ENCRYPT_ENCRYPT);
  1287. set_setup_mode(&desc[idx], SETUP_LOAD_STATE0);
  1288. set_flow_mode(&desc[idx], S_DIN_to_HASH);
  1289. set_aes_not_hash_mode(&desc[idx]);
  1290. idx++;
  1291. /* process assoc data */
  1292. if (req->assoclen > 0) {
  1293. cc_set_assoc_desc(req, DIN_HASH, desc, &idx);
  1294. } else {
  1295. hw_desc_init(&desc[idx]);
  1296. set_din_type(&desc[idx], DMA_DLLI,
  1297. sg_dma_address(&req_ctx->ccm_adata_sg),
  1298. AES_BLOCK_SIZE + req_ctx->ccm_hdr_size, NS_BIT);
  1299. set_flow_mode(&desc[idx], DIN_HASH);
  1300. idx++;
  1301. }
  1302. /* process the cipher */
  1303. if (req_ctx->cryptlen)
  1304. cc_proc_cipher_desc(req, cipher_flow_mode, desc, &idx);
  1305. /* Read temporal MAC */
  1306. hw_desc_init(&desc[idx]);
  1307. set_cipher_mode(&desc[idx], DRV_CIPHER_CBC_MAC);
  1308. set_dout_dlli(&desc[idx], req_ctx->mac_buf_dma_addr, ctx->authsize,
  1309. NS_BIT, 0);
  1310. set_setup_mode(&desc[idx], SETUP_WRITE_STATE0);
  1311. set_cipher_config0(&desc[idx], HASH_DIGEST_RESULT_LITTLE_ENDIAN);
  1312. set_flow_mode(&desc[idx], S_HASH_to_DOUT);
  1313. set_aes_not_hash_mode(&desc[idx]);
  1314. idx++;
  1315. /* load AES-CTR state (for last MAC calculation)*/
  1316. hw_desc_init(&desc[idx]);
  1317. set_cipher_mode(&desc[idx], DRV_CIPHER_CTR);
  1318. set_cipher_config0(&desc[idx], DRV_CRYPTO_DIRECTION_ENCRYPT);
  1319. set_din_type(&desc[idx], DMA_DLLI, req_ctx->ccm_iv0_dma_addr,
  1320. AES_BLOCK_SIZE, NS_BIT);
  1321. set_key_size_aes(&desc[idx], ctx->enc_keylen);
  1322. set_setup_mode(&desc[idx], SETUP_LOAD_STATE1);
  1323. set_flow_mode(&desc[idx], S_DIN_to_AES);
  1324. idx++;
  1325. hw_desc_init(&desc[idx]);
  1326. set_din_no_dma(&desc[idx], 0, 0xfffff0);
  1327. set_dout_no_dma(&desc[idx], 0, 0, 1);
  1328. idx++;
  1329. /* encrypt the "T" value and store MAC in mac_state */
  1330. hw_desc_init(&desc[idx]);
  1331. set_din_type(&desc[idx], DMA_DLLI, req_ctx->mac_buf_dma_addr,
  1332. ctx->authsize, NS_BIT);
  1333. set_dout_dlli(&desc[idx], mac_result, ctx->authsize, NS_BIT, 1);
  1334. set_queue_last_ind(ctx->drvdata, &desc[idx]);
  1335. set_flow_mode(&desc[idx], DIN_AES_DOUT);
  1336. idx++;
  1337. *seq_size = idx;
  1338. return 0;
  1339. }
  1340. static int config_ccm_adata(struct aead_request *req)
  1341. {
  1342. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  1343. struct cc_aead_ctx *ctx = crypto_aead_ctx(tfm);
  1344. struct device *dev = drvdata_to_dev(ctx->drvdata);
  1345. struct aead_req_ctx *req_ctx = aead_request_ctx(req);
  1346. //unsigned int size_of_a = 0, rem_a_size = 0;
  1347. unsigned int lp = req->iv[0];
  1348. /* Note: The code assume that req->iv[0] already contains the value
  1349. * of L' of RFC3610
  1350. */
  1351. unsigned int l = lp + 1; /* This is L' of RFC 3610. */
  1352. unsigned int m = ctx->authsize; /* This is M' of RFC 3610. */
  1353. u8 *b0 = req_ctx->ccm_config + CCM_B0_OFFSET;
  1354. u8 *a0 = req_ctx->ccm_config + CCM_A0_OFFSET;
  1355. u8 *ctr_count_0 = req_ctx->ccm_config + CCM_CTR_COUNT_0_OFFSET;
  1356. unsigned int cryptlen = (req_ctx->gen_ctx.op_type ==
  1357. DRV_CRYPTO_DIRECTION_ENCRYPT) ?
  1358. req->cryptlen :
  1359. (req->cryptlen - ctx->authsize);
  1360. int rc;
  1361. memset(req_ctx->mac_buf, 0, AES_BLOCK_SIZE);
  1362. memset(req_ctx->ccm_config, 0, AES_BLOCK_SIZE * 3);
  1363. /* taken from crypto/ccm.c */
  1364. /* 2 <= L <= 8, so 1 <= L' <= 7. */
  1365. if (l < 2 || l > 8) {
  1366. dev_err(dev, "illegal iv value %X\n", req->iv[0]);
  1367. return -EINVAL;
  1368. }
  1369. memcpy(b0, req->iv, AES_BLOCK_SIZE);
  1370. /* format control info per RFC 3610 and
  1371. * NIST Special Publication 800-38C
  1372. */
  1373. *b0 |= (8 * ((m - 2) / 2));
  1374. if (req->assoclen > 0)
  1375. *b0 |= 64; /* Enable bit 6 if Adata exists. */
  1376. rc = set_msg_len(b0 + 16 - l, cryptlen, l); /* Write L'. */
  1377. if (rc) {
  1378. dev_err(dev, "message len overflow detected");
  1379. return rc;
  1380. }
  1381. /* END of "taken from crypto/ccm.c" */
  1382. /* l(a) - size of associated data. */
  1383. req_ctx->ccm_hdr_size = format_ccm_a0(a0, req->assoclen);
  1384. memset(req->iv + 15 - req->iv[0], 0, req->iv[0] + 1);
  1385. req->iv[15] = 1;
  1386. memcpy(ctr_count_0, req->iv, AES_BLOCK_SIZE);
  1387. ctr_count_0[15] = 0;
  1388. return 0;
  1389. }
  1390. static void cc_proc_rfc4309_ccm(struct aead_request *req)
  1391. {
  1392. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  1393. struct cc_aead_ctx *ctx = crypto_aead_ctx(tfm);
  1394. struct aead_req_ctx *areq_ctx = aead_request_ctx(req);
  1395. /* L' */
  1396. memset(areq_ctx->ctr_iv, 0, AES_BLOCK_SIZE);
  1397. /* For RFC 4309, always use 4 bytes for message length
  1398. * (at most 2^32-1 bytes).
  1399. */
  1400. areq_ctx->ctr_iv[0] = 3;
  1401. /* In RFC 4309 there is an 11-bytes nonce+IV part,
  1402. * that we build here.
  1403. */
  1404. memcpy(areq_ctx->ctr_iv + CCM_BLOCK_NONCE_OFFSET, ctx->ctr_nonce,
  1405. CCM_BLOCK_NONCE_SIZE);
  1406. memcpy(areq_ctx->ctr_iv + CCM_BLOCK_IV_OFFSET, req->iv,
  1407. CCM_BLOCK_IV_SIZE);
  1408. req->iv = areq_ctx->ctr_iv;
  1409. req->assoclen -= CCM_BLOCK_IV_SIZE;
  1410. }
  1411. static void cc_set_ghash_desc(struct aead_request *req,
  1412. struct cc_hw_desc desc[], unsigned int *seq_size)
  1413. {
  1414. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  1415. struct cc_aead_ctx *ctx = crypto_aead_ctx(tfm);
  1416. struct aead_req_ctx *req_ctx = aead_request_ctx(req);
  1417. unsigned int idx = *seq_size;
  1418. /* load key to AES*/
  1419. hw_desc_init(&desc[idx]);
  1420. set_cipher_mode(&desc[idx], DRV_CIPHER_ECB);
  1421. set_cipher_config0(&desc[idx], DRV_CRYPTO_DIRECTION_ENCRYPT);
  1422. set_din_type(&desc[idx], DMA_DLLI, ctx->enckey_dma_addr,
  1423. ctx->enc_keylen, NS_BIT);
  1424. set_key_size_aes(&desc[idx], ctx->enc_keylen);
  1425. set_setup_mode(&desc[idx], SETUP_LOAD_KEY0);
  1426. set_flow_mode(&desc[idx], S_DIN_to_AES);
  1427. idx++;
  1428. /* process one zero block to generate hkey */
  1429. hw_desc_init(&desc[idx]);
  1430. set_din_const(&desc[idx], 0x0, AES_BLOCK_SIZE);
  1431. set_dout_dlli(&desc[idx], req_ctx->hkey_dma_addr, AES_BLOCK_SIZE,
  1432. NS_BIT, 0);
  1433. set_flow_mode(&desc[idx], DIN_AES_DOUT);
  1434. idx++;
  1435. /* Memory Barrier */
  1436. hw_desc_init(&desc[idx]);
  1437. set_din_no_dma(&desc[idx], 0, 0xfffff0);
  1438. set_dout_no_dma(&desc[idx], 0, 0, 1);
  1439. idx++;
  1440. /* Load GHASH subkey */
  1441. hw_desc_init(&desc[idx]);
  1442. set_din_type(&desc[idx], DMA_DLLI, req_ctx->hkey_dma_addr,
  1443. AES_BLOCK_SIZE, NS_BIT);
  1444. set_dout_no_dma(&desc[idx], 0, 0, 1);
  1445. set_flow_mode(&desc[idx], S_DIN_to_HASH);
  1446. set_aes_not_hash_mode(&desc[idx]);
  1447. set_cipher_mode(&desc[idx], DRV_HASH_HW_GHASH);
  1448. set_cipher_config1(&desc[idx], HASH_PADDING_ENABLED);
  1449. set_setup_mode(&desc[idx], SETUP_LOAD_KEY0);
  1450. idx++;
  1451. /* Configure Hash Engine to work with GHASH.
  1452. * Since it was not possible to extend HASH submodes to add GHASH,
  1453. * The following command is necessary in order to
  1454. * select GHASH (according to HW designers)
  1455. */
  1456. hw_desc_init(&desc[idx]);
  1457. set_din_no_dma(&desc[idx], 0, 0xfffff0);
  1458. set_dout_no_dma(&desc[idx], 0, 0, 1);
  1459. set_flow_mode(&desc[idx], S_DIN_to_HASH);
  1460. set_aes_not_hash_mode(&desc[idx]);
  1461. set_cipher_mode(&desc[idx], DRV_HASH_HW_GHASH);
  1462. set_cipher_do(&desc[idx], 1); //1=AES_SK RKEK
  1463. set_cipher_config0(&desc[idx], DRV_CRYPTO_DIRECTION_ENCRYPT);
  1464. set_cipher_config1(&desc[idx], HASH_PADDING_ENABLED);
  1465. set_setup_mode(&desc[idx], SETUP_LOAD_KEY0);
  1466. idx++;
  1467. /* Load GHASH initial STATE (which is 0). (for any hash there is an
  1468. * initial state)
  1469. */
  1470. hw_desc_init(&desc[idx]);
  1471. set_din_const(&desc[idx], 0x0, AES_BLOCK_SIZE);
  1472. set_dout_no_dma(&desc[idx], 0, 0, 1);
  1473. set_flow_mode(&desc[idx], S_DIN_to_HASH);
  1474. set_aes_not_hash_mode(&desc[idx]);
  1475. set_cipher_mode(&desc[idx], DRV_HASH_HW_GHASH);
  1476. set_cipher_config1(&desc[idx], HASH_PADDING_ENABLED);
  1477. set_setup_mode(&desc[idx], SETUP_LOAD_STATE0);
  1478. idx++;
  1479. *seq_size = idx;
  1480. }
  1481. static void cc_set_gctr_desc(struct aead_request *req, struct cc_hw_desc desc[],
  1482. unsigned int *seq_size)
  1483. {
  1484. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  1485. struct cc_aead_ctx *ctx = crypto_aead_ctx(tfm);
  1486. struct aead_req_ctx *req_ctx = aead_request_ctx(req);
  1487. unsigned int idx = *seq_size;
  1488. /* load key to AES*/
  1489. hw_desc_init(&desc[idx]);
  1490. set_cipher_mode(&desc[idx], DRV_CIPHER_GCTR);
  1491. set_cipher_config0(&desc[idx], DRV_CRYPTO_DIRECTION_ENCRYPT);
  1492. set_din_type(&desc[idx], DMA_DLLI, ctx->enckey_dma_addr,
  1493. ctx->enc_keylen, NS_BIT);
  1494. set_key_size_aes(&desc[idx], ctx->enc_keylen);
  1495. set_setup_mode(&desc[idx], SETUP_LOAD_KEY0);
  1496. set_flow_mode(&desc[idx], S_DIN_to_AES);
  1497. idx++;
  1498. if (req_ctx->cryptlen && !req_ctx->plaintext_authenticate_only) {
  1499. /* load AES/CTR initial CTR value inc by 2*/
  1500. hw_desc_init(&desc[idx]);
  1501. set_cipher_mode(&desc[idx], DRV_CIPHER_GCTR);
  1502. set_key_size_aes(&desc[idx], ctx->enc_keylen);
  1503. set_din_type(&desc[idx], DMA_DLLI,
  1504. req_ctx->gcm_iv_inc2_dma_addr, AES_BLOCK_SIZE,
  1505. NS_BIT);
  1506. set_cipher_config0(&desc[idx], DRV_CRYPTO_DIRECTION_ENCRYPT);
  1507. set_setup_mode(&desc[idx], SETUP_LOAD_STATE1);
  1508. set_flow_mode(&desc[idx], S_DIN_to_AES);
  1509. idx++;
  1510. }
  1511. *seq_size = idx;
  1512. }
  1513. static void cc_proc_gcm_result(struct aead_request *req,
  1514. struct cc_hw_desc desc[],
  1515. unsigned int *seq_size)
  1516. {
  1517. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  1518. struct cc_aead_ctx *ctx = crypto_aead_ctx(tfm);
  1519. struct aead_req_ctx *req_ctx = aead_request_ctx(req);
  1520. dma_addr_t mac_result;
  1521. unsigned int idx = *seq_size;
  1522. if (req_ctx->gen_ctx.op_type == DRV_CRYPTO_DIRECTION_DECRYPT) {
  1523. mac_result = req_ctx->mac_buf_dma_addr;
  1524. } else { /* Encrypt */
  1525. mac_result = req_ctx->icv_dma_addr;
  1526. }
  1527. /* process(ghash) gcm_block_len */
  1528. hw_desc_init(&desc[idx]);
  1529. set_din_type(&desc[idx], DMA_DLLI, req_ctx->gcm_block_len_dma_addr,
  1530. AES_BLOCK_SIZE, NS_BIT);
  1531. set_flow_mode(&desc[idx], DIN_HASH);
  1532. idx++;
  1533. /* Store GHASH state after GHASH(Associated Data + Cipher +LenBlock) */
  1534. hw_desc_init(&desc[idx]);
  1535. set_cipher_mode(&desc[idx], DRV_HASH_HW_GHASH);
  1536. set_din_no_dma(&desc[idx], 0, 0xfffff0);
  1537. set_dout_dlli(&desc[idx], req_ctx->mac_buf_dma_addr, AES_BLOCK_SIZE,
  1538. NS_BIT, 0);
  1539. set_setup_mode(&desc[idx], SETUP_WRITE_STATE0);
  1540. set_flow_mode(&desc[idx], S_HASH_to_DOUT);
  1541. set_aes_not_hash_mode(&desc[idx]);
  1542. idx++;
  1543. /* load AES/CTR initial CTR value inc by 1*/
  1544. hw_desc_init(&desc[idx]);
  1545. set_cipher_mode(&desc[idx], DRV_CIPHER_GCTR);
  1546. set_key_size_aes(&desc[idx], ctx->enc_keylen);
  1547. set_din_type(&desc[idx], DMA_DLLI, req_ctx->gcm_iv_inc1_dma_addr,
  1548. AES_BLOCK_SIZE, NS_BIT);
  1549. set_cipher_config0(&desc[idx], DRV_CRYPTO_DIRECTION_ENCRYPT);
  1550. set_setup_mode(&desc[idx], SETUP_LOAD_STATE1);
  1551. set_flow_mode(&desc[idx], S_DIN_to_AES);
  1552. idx++;
  1553. /* Memory Barrier */
  1554. hw_desc_init(&desc[idx]);
  1555. set_din_no_dma(&desc[idx], 0, 0xfffff0);
  1556. set_dout_no_dma(&desc[idx], 0, 0, 1);
  1557. idx++;
  1558. /* process GCTR on stored GHASH and store MAC in mac_state*/
  1559. hw_desc_init(&desc[idx]);
  1560. set_cipher_mode(&desc[idx], DRV_CIPHER_GCTR);
  1561. set_din_type(&desc[idx], DMA_DLLI, req_ctx->mac_buf_dma_addr,
  1562. AES_BLOCK_SIZE, NS_BIT);
  1563. set_dout_dlli(&desc[idx], mac_result, ctx->authsize, NS_BIT, 1);
  1564. set_queue_last_ind(ctx->drvdata, &desc[idx]);
  1565. set_flow_mode(&desc[idx], DIN_AES_DOUT);
  1566. idx++;
  1567. *seq_size = idx;
  1568. }
  1569. static int cc_gcm(struct aead_request *req, struct cc_hw_desc desc[],
  1570. unsigned int *seq_size)
  1571. {
  1572. struct aead_req_ctx *req_ctx = aead_request_ctx(req);
  1573. unsigned int cipher_flow_mode;
  1574. if (req_ctx->gen_ctx.op_type == DRV_CRYPTO_DIRECTION_DECRYPT) {
  1575. cipher_flow_mode = AES_and_HASH;
  1576. } else { /* Encrypt */
  1577. cipher_flow_mode = AES_to_HASH_and_DOUT;
  1578. }
  1579. //in RFC4543 no data to encrypt. just copy data from src to dest.
  1580. if (req_ctx->plaintext_authenticate_only) {
  1581. cc_proc_cipher_desc(req, BYPASS, desc, seq_size);
  1582. cc_set_ghash_desc(req, desc, seq_size);
  1583. /* process(ghash) assoc data */
  1584. cc_set_assoc_desc(req, DIN_HASH, desc, seq_size);
  1585. cc_set_gctr_desc(req, desc, seq_size);
  1586. cc_proc_gcm_result(req, desc, seq_size);
  1587. return 0;
  1588. }
  1589. // for gcm and rfc4106.
  1590. cc_set_ghash_desc(req, desc, seq_size);
  1591. /* process(ghash) assoc data */
  1592. if (req->assoclen > 0)
  1593. cc_set_assoc_desc(req, DIN_HASH, desc, seq_size);
  1594. cc_set_gctr_desc(req, desc, seq_size);
  1595. /* process(gctr+ghash) */
  1596. if (req_ctx->cryptlen)
  1597. cc_proc_cipher_desc(req, cipher_flow_mode, desc, seq_size);
  1598. cc_proc_gcm_result(req, desc, seq_size);
  1599. return 0;
  1600. }
  1601. static int config_gcm_context(struct aead_request *req)
  1602. {
  1603. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  1604. struct cc_aead_ctx *ctx = crypto_aead_ctx(tfm);
  1605. struct aead_req_ctx *req_ctx = aead_request_ctx(req);
  1606. struct device *dev = drvdata_to_dev(ctx->drvdata);
  1607. unsigned int cryptlen = (req_ctx->gen_ctx.op_type ==
  1608. DRV_CRYPTO_DIRECTION_ENCRYPT) ?
  1609. req->cryptlen :
  1610. (req->cryptlen - ctx->authsize);
  1611. __be32 counter = cpu_to_be32(2);
  1612. dev_dbg(dev, "%s() cryptlen = %d, req->assoclen = %d ctx->authsize = %d\n",
  1613. __func__, cryptlen, req->assoclen, ctx->authsize);
  1614. memset(req_ctx->hkey, 0, AES_BLOCK_SIZE);
  1615. memset(req_ctx->mac_buf, 0, AES_BLOCK_SIZE);
  1616. memcpy(req->iv + 12, &counter, 4);
  1617. memcpy(req_ctx->gcm_iv_inc2, req->iv, 16);
  1618. counter = cpu_to_be32(1);
  1619. memcpy(req->iv + 12, &counter, 4);
  1620. memcpy(req_ctx->gcm_iv_inc1, req->iv, 16);
  1621. if (!req_ctx->plaintext_authenticate_only) {
  1622. __be64 temp64;
  1623. temp64 = cpu_to_be64(req->assoclen * 8);
  1624. memcpy(&req_ctx->gcm_len_block.len_a, &temp64, sizeof(temp64));
  1625. temp64 = cpu_to_be64(cryptlen * 8);
  1626. memcpy(&req_ctx->gcm_len_block.len_c, &temp64, 8);
  1627. } else {
  1628. /* rfc4543=> all data(AAD,IV,Plain) are considered additional
  1629. * data that is nothing is encrypted.
  1630. */
  1631. __be64 temp64;
  1632. temp64 = cpu_to_be64((req->assoclen + GCM_BLOCK_RFC4_IV_SIZE +
  1633. cryptlen) * 8);
  1634. memcpy(&req_ctx->gcm_len_block.len_a, &temp64, sizeof(temp64));
  1635. temp64 = 0;
  1636. memcpy(&req_ctx->gcm_len_block.len_c, &temp64, 8);
  1637. }
  1638. return 0;
  1639. }
  1640. static void cc_proc_rfc4_gcm(struct aead_request *req)
  1641. {
  1642. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  1643. struct cc_aead_ctx *ctx = crypto_aead_ctx(tfm);
  1644. struct aead_req_ctx *areq_ctx = aead_request_ctx(req);
  1645. memcpy(areq_ctx->ctr_iv + GCM_BLOCK_RFC4_NONCE_OFFSET,
  1646. ctx->ctr_nonce, GCM_BLOCK_RFC4_NONCE_SIZE);
  1647. memcpy(areq_ctx->ctr_iv + GCM_BLOCK_RFC4_IV_OFFSET, req->iv,
  1648. GCM_BLOCK_RFC4_IV_SIZE);
  1649. req->iv = areq_ctx->ctr_iv;
  1650. req->assoclen -= GCM_BLOCK_RFC4_IV_SIZE;
  1651. }
  1652. static int cc_proc_aead(struct aead_request *req,
  1653. enum drv_crypto_direction direct)
  1654. {
  1655. int rc = 0;
  1656. int seq_len = 0;
  1657. struct cc_hw_desc desc[MAX_AEAD_PROCESS_SEQ];
  1658. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  1659. struct cc_aead_ctx *ctx = crypto_aead_ctx(tfm);
  1660. struct aead_req_ctx *areq_ctx = aead_request_ctx(req);
  1661. struct device *dev = drvdata_to_dev(ctx->drvdata);
  1662. struct cc_crypto_req cc_req = {};
  1663. dev_dbg(dev, "%s context=%p req=%p iv=%p src=%p src_ofs=%d dst=%p dst_ofs=%d cryptolen=%d\n",
  1664. ((direct == DRV_CRYPTO_DIRECTION_ENCRYPT) ? "Enc" : "Dec"),
  1665. ctx, req, req->iv, sg_virt(req->src), req->src->offset,
  1666. sg_virt(req->dst), req->dst->offset, req->cryptlen);
  1667. /* STAT_PHASE_0: Init and sanity checks */
  1668. /* Check data length according to mode */
  1669. if (validate_data_size(ctx, direct, req)) {
  1670. dev_err(dev, "Unsupported crypt/assoc len %d/%d.\n",
  1671. req->cryptlen, req->assoclen);
  1672. crypto_aead_set_flags(tfm, CRYPTO_TFM_RES_BAD_BLOCK_LEN);
  1673. return -EINVAL;
  1674. }
  1675. /* Setup request structure */
  1676. cc_req.user_cb = (void *)cc_aead_complete;
  1677. cc_req.user_arg = (void *)req;
  1678. /* Setup request context */
  1679. areq_ctx->gen_ctx.op_type = direct;
  1680. areq_ctx->req_authsize = ctx->authsize;
  1681. areq_ctx->cipher_mode = ctx->cipher_mode;
  1682. /* STAT_PHASE_1: Map buffers */
  1683. if (ctx->cipher_mode == DRV_CIPHER_CTR) {
  1684. /* Build CTR IV - Copy nonce from last 4 bytes in
  1685. * CTR key to first 4 bytes in CTR IV
  1686. */
  1687. memcpy(areq_ctx->ctr_iv, ctx->ctr_nonce,
  1688. CTR_RFC3686_NONCE_SIZE);
  1689. if (!areq_ctx->backup_giv) /*User none-generated IV*/
  1690. memcpy(areq_ctx->ctr_iv + CTR_RFC3686_NONCE_SIZE,
  1691. req->iv, CTR_RFC3686_IV_SIZE);
  1692. /* Initialize counter portion of counter block */
  1693. *(__be32 *)(areq_ctx->ctr_iv + CTR_RFC3686_NONCE_SIZE +
  1694. CTR_RFC3686_IV_SIZE) = cpu_to_be32(1);
  1695. /* Replace with counter iv */
  1696. req->iv = areq_ctx->ctr_iv;
  1697. areq_ctx->hw_iv_size = CTR_RFC3686_BLOCK_SIZE;
  1698. } else if ((ctx->cipher_mode == DRV_CIPHER_CCM) ||
  1699. (ctx->cipher_mode == DRV_CIPHER_GCTR)) {
  1700. areq_ctx->hw_iv_size = AES_BLOCK_SIZE;
  1701. if (areq_ctx->ctr_iv != req->iv) {
  1702. memcpy(areq_ctx->ctr_iv, req->iv,
  1703. crypto_aead_ivsize(tfm));
  1704. req->iv = areq_ctx->ctr_iv;
  1705. }
  1706. } else {
  1707. areq_ctx->hw_iv_size = crypto_aead_ivsize(tfm);
  1708. }
  1709. if (ctx->cipher_mode == DRV_CIPHER_CCM) {
  1710. rc = config_ccm_adata(req);
  1711. if (rc) {
  1712. dev_dbg(dev, "config_ccm_adata() returned with a failure %d!",
  1713. rc);
  1714. goto exit;
  1715. }
  1716. } else {
  1717. areq_ctx->ccm_hdr_size = ccm_header_size_null;
  1718. }
  1719. if (ctx->cipher_mode == DRV_CIPHER_GCTR) {
  1720. rc = config_gcm_context(req);
  1721. if (rc) {
  1722. dev_dbg(dev, "config_gcm_context() returned with a failure %d!",
  1723. rc);
  1724. goto exit;
  1725. }
  1726. }
  1727. rc = cc_map_aead_request(ctx->drvdata, req);
  1728. if (rc) {
  1729. dev_err(dev, "map_request() failed\n");
  1730. goto exit;
  1731. }
  1732. /* do we need to generate IV? */
  1733. if (areq_ctx->backup_giv) {
  1734. /* set the DMA mapped IV address*/
  1735. if (ctx->cipher_mode == DRV_CIPHER_CTR) {
  1736. cc_req.ivgen_dma_addr[0] =
  1737. areq_ctx->gen_ctx.iv_dma_addr +
  1738. CTR_RFC3686_NONCE_SIZE;
  1739. cc_req.ivgen_dma_addr_len = 1;
  1740. } else if (ctx->cipher_mode == DRV_CIPHER_CCM) {
  1741. /* In ccm, the IV needs to exist both inside B0 and
  1742. * inside the counter.It is also copied to iv_dma_addr
  1743. * for other reasons (like returning it to the user).
  1744. * So, using 3 (identical) IV outputs.
  1745. */
  1746. cc_req.ivgen_dma_addr[0] =
  1747. areq_ctx->gen_ctx.iv_dma_addr +
  1748. CCM_BLOCK_IV_OFFSET;
  1749. cc_req.ivgen_dma_addr[1] =
  1750. sg_dma_address(&areq_ctx->ccm_adata_sg) +
  1751. CCM_B0_OFFSET + CCM_BLOCK_IV_OFFSET;
  1752. cc_req.ivgen_dma_addr[2] =
  1753. sg_dma_address(&areq_ctx->ccm_adata_sg) +
  1754. CCM_CTR_COUNT_0_OFFSET + CCM_BLOCK_IV_OFFSET;
  1755. cc_req.ivgen_dma_addr_len = 3;
  1756. } else {
  1757. cc_req.ivgen_dma_addr[0] =
  1758. areq_ctx->gen_ctx.iv_dma_addr;
  1759. cc_req.ivgen_dma_addr_len = 1;
  1760. }
  1761. /* set the IV size (8/16 B long)*/
  1762. cc_req.ivgen_size = crypto_aead_ivsize(tfm);
  1763. }
  1764. /* STAT_PHASE_2: Create sequence */
  1765. /* Load MLLI tables to SRAM if necessary */
  1766. cc_mlli_to_sram(req, desc, &seq_len);
  1767. /*TODO: move seq len by reference */
  1768. switch (ctx->auth_mode) {
  1769. case DRV_HASH_SHA1:
  1770. case DRV_HASH_SHA256:
  1771. cc_hmac_authenc(req, desc, &seq_len);
  1772. break;
  1773. case DRV_HASH_XCBC_MAC:
  1774. cc_xcbc_authenc(req, desc, &seq_len);
  1775. break;
  1776. case DRV_HASH_NULL:
  1777. if (ctx->cipher_mode == DRV_CIPHER_CCM)
  1778. cc_ccm(req, desc, &seq_len);
  1779. if (ctx->cipher_mode == DRV_CIPHER_GCTR)
  1780. cc_gcm(req, desc, &seq_len);
  1781. break;
  1782. default:
  1783. dev_err(dev, "Unsupported authenc (%d)\n", ctx->auth_mode);
  1784. cc_unmap_aead_request(dev, req);
  1785. rc = -ENOTSUPP;
  1786. goto exit;
  1787. }
  1788. /* STAT_PHASE_3: Lock HW and push sequence */
  1789. rc = cc_send_request(ctx->drvdata, &cc_req, desc, seq_len, &req->base);
  1790. if (rc != -EINPROGRESS && rc != -EBUSY) {
  1791. dev_err(dev, "send_request() failed (rc=%d)\n", rc);
  1792. cc_unmap_aead_request(dev, req);
  1793. }
  1794. exit:
  1795. return rc;
  1796. }
  1797. static int cc_aead_encrypt(struct aead_request *req)
  1798. {
  1799. struct aead_req_ctx *areq_ctx = aead_request_ctx(req);
  1800. int rc;
  1801. /* No generated IV required */
  1802. areq_ctx->backup_iv = req->iv;
  1803. areq_ctx->backup_giv = NULL;
  1804. areq_ctx->is_gcm4543 = false;
  1805. areq_ctx->plaintext_authenticate_only = false;
  1806. rc = cc_proc_aead(req, DRV_CRYPTO_DIRECTION_ENCRYPT);
  1807. if (rc != -EINPROGRESS && rc != -EBUSY)
  1808. req->iv = areq_ctx->backup_iv;
  1809. return rc;
  1810. }
  1811. static int cc_rfc4309_ccm_encrypt(struct aead_request *req)
  1812. {
  1813. /* Very similar to cc_aead_encrypt() above. */
  1814. struct aead_req_ctx *areq_ctx = aead_request_ctx(req);
  1815. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  1816. struct cc_aead_ctx *ctx = crypto_aead_ctx(tfm);
  1817. struct device *dev = drvdata_to_dev(ctx->drvdata);
  1818. int rc = -EINVAL;
  1819. if (!valid_assoclen(req)) {
  1820. dev_err(dev, "invalid Assoclen:%u\n", req->assoclen);
  1821. goto out;
  1822. }
  1823. /* No generated IV required */
  1824. areq_ctx->backup_iv = req->iv;
  1825. areq_ctx->backup_giv = NULL;
  1826. areq_ctx->is_gcm4543 = true;
  1827. cc_proc_rfc4309_ccm(req);
  1828. rc = cc_proc_aead(req, DRV_CRYPTO_DIRECTION_ENCRYPT);
  1829. if (rc != -EINPROGRESS && rc != -EBUSY)
  1830. req->iv = areq_ctx->backup_iv;
  1831. out:
  1832. return rc;
  1833. }
  1834. static int cc_aead_decrypt(struct aead_request *req)
  1835. {
  1836. struct aead_req_ctx *areq_ctx = aead_request_ctx(req);
  1837. int rc;
  1838. /* No generated IV required */
  1839. areq_ctx->backup_iv = req->iv;
  1840. areq_ctx->backup_giv = NULL;
  1841. areq_ctx->is_gcm4543 = false;
  1842. areq_ctx->plaintext_authenticate_only = false;
  1843. rc = cc_proc_aead(req, DRV_CRYPTO_DIRECTION_DECRYPT);
  1844. if (rc != -EINPROGRESS && rc != -EBUSY)
  1845. req->iv = areq_ctx->backup_iv;
  1846. return rc;
  1847. }
  1848. static int cc_rfc4309_ccm_decrypt(struct aead_request *req)
  1849. {
  1850. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  1851. struct cc_aead_ctx *ctx = crypto_aead_ctx(tfm);
  1852. struct device *dev = drvdata_to_dev(ctx->drvdata);
  1853. struct aead_req_ctx *areq_ctx = aead_request_ctx(req);
  1854. int rc = -EINVAL;
  1855. if (!valid_assoclen(req)) {
  1856. dev_err(dev, "invalid Assoclen:%u\n", req->assoclen);
  1857. goto out;
  1858. }
  1859. /* No generated IV required */
  1860. areq_ctx->backup_iv = req->iv;
  1861. areq_ctx->backup_giv = NULL;
  1862. areq_ctx->is_gcm4543 = true;
  1863. cc_proc_rfc4309_ccm(req);
  1864. rc = cc_proc_aead(req, DRV_CRYPTO_DIRECTION_DECRYPT);
  1865. if (rc != -EINPROGRESS && rc != -EBUSY)
  1866. req->iv = areq_ctx->backup_iv;
  1867. out:
  1868. return rc;
  1869. }
  1870. static int cc_rfc4106_gcm_setkey(struct crypto_aead *tfm, const u8 *key,
  1871. unsigned int keylen)
  1872. {
  1873. struct cc_aead_ctx *ctx = crypto_aead_ctx(tfm);
  1874. struct device *dev = drvdata_to_dev(ctx->drvdata);
  1875. dev_dbg(dev, "%s() keylen %d, key %p\n", __func__, keylen, key);
  1876. if (keylen < 4)
  1877. return -EINVAL;
  1878. keylen -= 4;
  1879. memcpy(ctx->ctr_nonce, key + keylen, 4);
  1880. return cc_aead_setkey(tfm, key, keylen);
  1881. }
  1882. static int cc_rfc4543_gcm_setkey(struct crypto_aead *tfm, const u8 *key,
  1883. unsigned int keylen)
  1884. {
  1885. struct cc_aead_ctx *ctx = crypto_aead_ctx(tfm);
  1886. struct device *dev = drvdata_to_dev(ctx->drvdata);
  1887. dev_dbg(dev, "%s() keylen %d, key %p\n", __func__, keylen, key);
  1888. if (keylen < 4)
  1889. return -EINVAL;
  1890. keylen -= 4;
  1891. memcpy(ctx->ctr_nonce, key + keylen, 4);
  1892. return cc_aead_setkey(tfm, key, keylen);
  1893. }
  1894. static int cc_gcm_setauthsize(struct crypto_aead *authenc,
  1895. unsigned int authsize)
  1896. {
  1897. switch (authsize) {
  1898. case 4:
  1899. case 8:
  1900. case 12:
  1901. case 13:
  1902. case 14:
  1903. case 15:
  1904. case 16:
  1905. break;
  1906. default:
  1907. return -EINVAL;
  1908. }
  1909. return cc_aead_setauthsize(authenc, authsize);
  1910. }
  1911. static int cc_rfc4106_gcm_setauthsize(struct crypto_aead *authenc,
  1912. unsigned int authsize)
  1913. {
  1914. struct cc_aead_ctx *ctx = crypto_aead_ctx(authenc);
  1915. struct device *dev = drvdata_to_dev(ctx->drvdata);
  1916. dev_dbg(dev, "authsize %d\n", authsize);
  1917. switch (authsize) {
  1918. case 8:
  1919. case 12:
  1920. case 16:
  1921. break;
  1922. default:
  1923. return -EINVAL;
  1924. }
  1925. return cc_aead_setauthsize(authenc, authsize);
  1926. }
  1927. static int cc_rfc4543_gcm_setauthsize(struct crypto_aead *authenc,
  1928. unsigned int authsize)
  1929. {
  1930. struct cc_aead_ctx *ctx = crypto_aead_ctx(authenc);
  1931. struct device *dev = drvdata_to_dev(ctx->drvdata);
  1932. dev_dbg(dev, "authsize %d\n", authsize);
  1933. if (authsize != 16)
  1934. return -EINVAL;
  1935. return cc_aead_setauthsize(authenc, authsize);
  1936. }
  1937. static int cc_rfc4106_gcm_encrypt(struct aead_request *req)
  1938. {
  1939. /* Very similar to cc_aead_encrypt() above. */
  1940. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  1941. struct cc_aead_ctx *ctx = crypto_aead_ctx(tfm);
  1942. struct device *dev = drvdata_to_dev(ctx->drvdata);
  1943. struct aead_req_ctx *areq_ctx = aead_request_ctx(req);
  1944. int rc = -EINVAL;
  1945. if (!valid_assoclen(req)) {
  1946. dev_err(dev, "invalid Assoclen:%u\n", req->assoclen);
  1947. goto out;
  1948. }
  1949. /* No generated IV required */
  1950. areq_ctx->backup_iv = req->iv;
  1951. areq_ctx->backup_giv = NULL;
  1952. areq_ctx->plaintext_authenticate_only = false;
  1953. cc_proc_rfc4_gcm(req);
  1954. areq_ctx->is_gcm4543 = true;
  1955. rc = cc_proc_aead(req, DRV_CRYPTO_DIRECTION_ENCRYPT);
  1956. if (rc != -EINPROGRESS && rc != -EBUSY)
  1957. req->iv = areq_ctx->backup_iv;
  1958. out:
  1959. return rc;
  1960. }
  1961. static int cc_rfc4543_gcm_encrypt(struct aead_request *req)
  1962. {
  1963. /* Very similar to cc_aead_encrypt() above. */
  1964. struct aead_req_ctx *areq_ctx = aead_request_ctx(req);
  1965. int rc;
  1966. //plaintext is not encryped with rfc4543
  1967. areq_ctx->plaintext_authenticate_only = true;
  1968. /* No generated IV required */
  1969. areq_ctx->backup_iv = req->iv;
  1970. areq_ctx->backup_giv = NULL;
  1971. cc_proc_rfc4_gcm(req);
  1972. areq_ctx->is_gcm4543 = true;
  1973. rc = cc_proc_aead(req, DRV_CRYPTO_DIRECTION_ENCRYPT);
  1974. if (rc != -EINPROGRESS && rc != -EBUSY)
  1975. req->iv = areq_ctx->backup_iv;
  1976. return rc;
  1977. }
  1978. static int cc_rfc4106_gcm_decrypt(struct aead_request *req)
  1979. {
  1980. /* Very similar to cc_aead_decrypt() above. */
  1981. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  1982. struct cc_aead_ctx *ctx = crypto_aead_ctx(tfm);
  1983. struct device *dev = drvdata_to_dev(ctx->drvdata);
  1984. struct aead_req_ctx *areq_ctx = aead_request_ctx(req);
  1985. int rc = -EINVAL;
  1986. if (!valid_assoclen(req)) {
  1987. dev_err(dev, "invalid Assoclen:%u\n", req->assoclen);
  1988. goto out;
  1989. }
  1990. /* No generated IV required */
  1991. areq_ctx->backup_iv = req->iv;
  1992. areq_ctx->backup_giv = NULL;
  1993. areq_ctx->plaintext_authenticate_only = false;
  1994. cc_proc_rfc4_gcm(req);
  1995. areq_ctx->is_gcm4543 = true;
  1996. rc = cc_proc_aead(req, DRV_CRYPTO_DIRECTION_DECRYPT);
  1997. if (rc != -EINPROGRESS && rc != -EBUSY)
  1998. req->iv = areq_ctx->backup_iv;
  1999. out:
  2000. return rc;
  2001. }
  2002. static int cc_rfc4543_gcm_decrypt(struct aead_request *req)
  2003. {
  2004. /* Very similar to cc_aead_decrypt() above. */
  2005. struct aead_req_ctx *areq_ctx = aead_request_ctx(req);
  2006. int rc;
  2007. //plaintext is not decryped with rfc4543
  2008. areq_ctx->plaintext_authenticate_only = true;
  2009. /* No generated IV required */
  2010. areq_ctx->backup_iv = req->iv;
  2011. areq_ctx->backup_giv = NULL;
  2012. cc_proc_rfc4_gcm(req);
  2013. areq_ctx->is_gcm4543 = true;
  2014. rc = cc_proc_aead(req, DRV_CRYPTO_DIRECTION_DECRYPT);
  2015. if (rc != -EINPROGRESS && rc != -EBUSY)
  2016. req->iv = areq_ctx->backup_iv;
  2017. return rc;
  2018. }
  2019. /* aead alg */
  2020. static struct cc_alg_template aead_algs[] = {
  2021. {
  2022. .name = "authenc(hmac(sha1),cbc(aes))",
  2023. .driver_name = "authenc-hmac-sha1-cbc-aes-ccree",
  2024. .blocksize = AES_BLOCK_SIZE,
  2025. .template_aead = {
  2026. .setkey = cc_aead_setkey,
  2027. .setauthsize = cc_aead_setauthsize,
  2028. .encrypt = cc_aead_encrypt,
  2029. .decrypt = cc_aead_decrypt,
  2030. .init = cc_aead_init,
  2031. .exit = cc_aead_exit,
  2032. .ivsize = AES_BLOCK_SIZE,
  2033. .maxauthsize = SHA1_DIGEST_SIZE,
  2034. },
  2035. .cipher_mode = DRV_CIPHER_CBC,
  2036. .flow_mode = S_DIN_to_AES,
  2037. .auth_mode = DRV_HASH_SHA1,
  2038. .min_hw_rev = CC_HW_REV_630,
  2039. },
  2040. {
  2041. .name = "authenc(hmac(sha1),cbc(des3_ede))",
  2042. .driver_name = "authenc-hmac-sha1-cbc-des3-ccree",
  2043. .blocksize = DES3_EDE_BLOCK_SIZE,
  2044. .template_aead = {
  2045. .setkey = cc_aead_setkey,
  2046. .setauthsize = cc_aead_setauthsize,
  2047. .encrypt = cc_aead_encrypt,
  2048. .decrypt = cc_aead_decrypt,
  2049. .init = cc_aead_init,
  2050. .exit = cc_aead_exit,
  2051. .ivsize = DES3_EDE_BLOCK_SIZE,
  2052. .maxauthsize = SHA1_DIGEST_SIZE,
  2053. },
  2054. .cipher_mode = DRV_CIPHER_CBC,
  2055. .flow_mode = S_DIN_to_DES,
  2056. .auth_mode = DRV_HASH_SHA1,
  2057. .min_hw_rev = CC_HW_REV_630,
  2058. },
  2059. {
  2060. .name = "authenc(hmac(sha256),cbc(aes))",
  2061. .driver_name = "authenc-hmac-sha256-cbc-aes-ccree",
  2062. .blocksize = AES_BLOCK_SIZE,
  2063. .template_aead = {
  2064. .setkey = cc_aead_setkey,
  2065. .setauthsize = cc_aead_setauthsize,
  2066. .encrypt = cc_aead_encrypt,
  2067. .decrypt = cc_aead_decrypt,
  2068. .init = cc_aead_init,
  2069. .exit = cc_aead_exit,
  2070. .ivsize = AES_BLOCK_SIZE,
  2071. .maxauthsize = SHA256_DIGEST_SIZE,
  2072. },
  2073. .cipher_mode = DRV_CIPHER_CBC,
  2074. .flow_mode = S_DIN_to_AES,
  2075. .auth_mode = DRV_HASH_SHA256,
  2076. .min_hw_rev = CC_HW_REV_630,
  2077. },
  2078. {
  2079. .name = "authenc(hmac(sha256),cbc(des3_ede))",
  2080. .driver_name = "authenc-hmac-sha256-cbc-des3-ccree",
  2081. .blocksize = DES3_EDE_BLOCK_SIZE,
  2082. .template_aead = {
  2083. .setkey = cc_aead_setkey,
  2084. .setauthsize = cc_aead_setauthsize,
  2085. .encrypt = cc_aead_encrypt,
  2086. .decrypt = cc_aead_decrypt,
  2087. .init = cc_aead_init,
  2088. .exit = cc_aead_exit,
  2089. .ivsize = DES3_EDE_BLOCK_SIZE,
  2090. .maxauthsize = SHA256_DIGEST_SIZE,
  2091. },
  2092. .cipher_mode = DRV_CIPHER_CBC,
  2093. .flow_mode = S_DIN_to_DES,
  2094. .auth_mode = DRV_HASH_SHA256,
  2095. .min_hw_rev = CC_HW_REV_630,
  2096. },
  2097. {
  2098. .name = "authenc(xcbc(aes),cbc(aes))",
  2099. .driver_name = "authenc-xcbc-aes-cbc-aes-ccree",
  2100. .blocksize = AES_BLOCK_SIZE,
  2101. .template_aead = {
  2102. .setkey = cc_aead_setkey,
  2103. .setauthsize = cc_aead_setauthsize,
  2104. .encrypt = cc_aead_encrypt,
  2105. .decrypt = cc_aead_decrypt,
  2106. .init = cc_aead_init,
  2107. .exit = cc_aead_exit,
  2108. .ivsize = AES_BLOCK_SIZE,
  2109. .maxauthsize = AES_BLOCK_SIZE,
  2110. },
  2111. .cipher_mode = DRV_CIPHER_CBC,
  2112. .flow_mode = S_DIN_to_AES,
  2113. .auth_mode = DRV_HASH_XCBC_MAC,
  2114. .min_hw_rev = CC_HW_REV_630,
  2115. },
  2116. {
  2117. .name = "authenc(hmac(sha1),rfc3686(ctr(aes)))",
  2118. .driver_name = "authenc-hmac-sha1-rfc3686-ctr-aes-ccree",
  2119. .blocksize = 1,
  2120. .template_aead = {
  2121. .setkey = cc_aead_setkey,
  2122. .setauthsize = cc_aead_setauthsize,
  2123. .encrypt = cc_aead_encrypt,
  2124. .decrypt = cc_aead_decrypt,
  2125. .init = cc_aead_init,
  2126. .exit = cc_aead_exit,
  2127. .ivsize = CTR_RFC3686_IV_SIZE,
  2128. .maxauthsize = SHA1_DIGEST_SIZE,
  2129. },
  2130. .cipher_mode = DRV_CIPHER_CTR,
  2131. .flow_mode = S_DIN_to_AES,
  2132. .auth_mode = DRV_HASH_SHA1,
  2133. .min_hw_rev = CC_HW_REV_630,
  2134. },
  2135. {
  2136. .name = "authenc(hmac(sha256),rfc3686(ctr(aes)))",
  2137. .driver_name = "authenc-hmac-sha256-rfc3686-ctr-aes-ccree",
  2138. .blocksize = 1,
  2139. .template_aead = {
  2140. .setkey = cc_aead_setkey,
  2141. .setauthsize = cc_aead_setauthsize,
  2142. .encrypt = cc_aead_encrypt,
  2143. .decrypt = cc_aead_decrypt,
  2144. .init = cc_aead_init,
  2145. .exit = cc_aead_exit,
  2146. .ivsize = CTR_RFC3686_IV_SIZE,
  2147. .maxauthsize = SHA256_DIGEST_SIZE,
  2148. },
  2149. .cipher_mode = DRV_CIPHER_CTR,
  2150. .flow_mode = S_DIN_to_AES,
  2151. .auth_mode = DRV_HASH_SHA256,
  2152. .min_hw_rev = CC_HW_REV_630,
  2153. },
  2154. {
  2155. .name = "authenc(xcbc(aes),rfc3686(ctr(aes)))",
  2156. .driver_name = "authenc-xcbc-aes-rfc3686-ctr-aes-ccree",
  2157. .blocksize = 1,
  2158. .template_aead = {
  2159. .setkey = cc_aead_setkey,
  2160. .setauthsize = cc_aead_setauthsize,
  2161. .encrypt = cc_aead_encrypt,
  2162. .decrypt = cc_aead_decrypt,
  2163. .init = cc_aead_init,
  2164. .exit = cc_aead_exit,
  2165. .ivsize = CTR_RFC3686_IV_SIZE,
  2166. .maxauthsize = AES_BLOCK_SIZE,
  2167. },
  2168. .cipher_mode = DRV_CIPHER_CTR,
  2169. .flow_mode = S_DIN_to_AES,
  2170. .auth_mode = DRV_HASH_XCBC_MAC,
  2171. .min_hw_rev = CC_HW_REV_630,
  2172. },
  2173. {
  2174. .name = "ccm(aes)",
  2175. .driver_name = "ccm-aes-ccree",
  2176. .blocksize = 1,
  2177. .template_aead = {
  2178. .setkey = cc_aead_setkey,
  2179. .setauthsize = cc_ccm_setauthsize,
  2180. .encrypt = cc_aead_encrypt,
  2181. .decrypt = cc_aead_decrypt,
  2182. .init = cc_aead_init,
  2183. .exit = cc_aead_exit,
  2184. .ivsize = AES_BLOCK_SIZE,
  2185. .maxauthsize = AES_BLOCK_SIZE,
  2186. },
  2187. .cipher_mode = DRV_CIPHER_CCM,
  2188. .flow_mode = S_DIN_to_AES,
  2189. .auth_mode = DRV_HASH_NULL,
  2190. .min_hw_rev = CC_HW_REV_630,
  2191. },
  2192. {
  2193. .name = "rfc4309(ccm(aes))",
  2194. .driver_name = "rfc4309-ccm-aes-ccree",
  2195. .blocksize = 1,
  2196. .template_aead = {
  2197. .setkey = cc_rfc4309_ccm_setkey,
  2198. .setauthsize = cc_rfc4309_ccm_setauthsize,
  2199. .encrypt = cc_rfc4309_ccm_encrypt,
  2200. .decrypt = cc_rfc4309_ccm_decrypt,
  2201. .init = cc_aead_init,
  2202. .exit = cc_aead_exit,
  2203. .ivsize = CCM_BLOCK_IV_SIZE,
  2204. .maxauthsize = AES_BLOCK_SIZE,
  2205. },
  2206. .cipher_mode = DRV_CIPHER_CCM,
  2207. .flow_mode = S_DIN_to_AES,
  2208. .auth_mode = DRV_HASH_NULL,
  2209. .min_hw_rev = CC_HW_REV_630,
  2210. },
  2211. {
  2212. .name = "gcm(aes)",
  2213. .driver_name = "gcm-aes-ccree",
  2214. .blocksize = 1,
  2215. .template_aead = {
  2216. .setkey = cc_aead_setkey,
  2217. .setauthsize = cc_gcm_setauthsize,
  2218. .encrypt = cc_aead_encrypt,
  2219. .decrypt = cc_aead_decrypt,
  2220. .init = cc_aead_init,
  2221. .exit = cc_aead_exit,
  2222. .ivsize = 12,
  2223. .maxauthsize = AES_BLOCK_SIZE,
  2224. },
  2225. .cipher_mode = DRV_CIPHER_GCTR,
  2226. .flow_mode = S_DIN_to_AES,
  2227. .auth_mode = DRV_HASH_NULL,
  2228. .min_hw_rev = CC_HW_REV_630,
  2229. },
  2230. {
  2231. .name = "rfc4106(gcm(aes))",
  2232. .driver_name = "rfc4106-gcm-aes-ccree",
  2233. .blocksize = 1,
  2234. .template_aead = {
  2235. .setkey = cc_rfc4106_gcm_setkey,
  2236. .setauthsize = cc_rfc4106_gcm_setauthsize,
  2237. .encrypt = cc_rfc4106_gcm_encrypt,
  2238. .decrypt = cc_rfc4106_gcm_decrypt,
  2239. .init = cc_aead_init,
  2240. .exit = cc_aead_exit,
  2241. .ivsize = GCM_BLOCK_RFC4_IV_SIZE,
  2242. .maxauthsize = AES_BLOCK_SIZE,
  2243. },
  2244. .cipher_mode = DRV_CIPHER_GCTR,
  2245. .flow_mode = S_DIN_to_AES,
  2246. .auth_mode = DRV_HASH_NULL,
  2247. .min_hw_rev = CC_HW_REV_630,
  2248. },
  2249. {
  2250. .name = "rfc4543(gcm(aes))",
  2251. .driver_name = "rfc4543-gcm-aes-ccree",
  2252. .blocksize = 1,
  2253. .template_aead = {
  2254. .setkey = cc_rfc4543_gcm_setkey,
  2255. .setauthsize = cc_rfc4543_gcm_setauthsize,
  2256. .encrypt = cc_rfc4543_gcm_encrypt,
  2257. .decrypt = cc_rfc4543_gcm_decrypt,
  2258. .init = cc_aead_init,
  2259. .exit = cc_aead_exit,
  2260. .ivsize = GCM_BLOCK_RFC4_IV_SIZE,
  2261. .maxauthsize = AES_BLOCK_SIZE,
  2262. },
  2263. .cipher_mode = DRV_CIPHER_GCTR,
  2264. .flow_mode = S_DIN_to_AES,
  2265. .auth_mode = DRV_HASH_NULL,
  2266. .min_hw_rev = CC_HW_REV_630,
  2267. },
  2268. };
  2269. static struct cc_crypto_alg *cc_create_aead_alg(struct cc_alg_template *tmpl,
  2270. struct device *dev)
  2271. {
  2272. struct cc_crypto_alg *t_alg;
  2273. struct aead_alg *alg;
  2274. t_alg = kzalloc(sizeof(*t_alg), GFP_KERNEL);
  2275. if (!t_alg)
  2276. return ERR_PTR(-ENOMEM);
  2277. alg = &tmpl->template_aead;
  2278. snprintf(alg->base.cra_name, CRYPTO_MAX_ALG_NAME, "%s", tmpl->name);
  2279. snprintf(alg->base.cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
  2280. tmpl->driver_name);
  2281. alg->base.cra_module = THIS_MODULE;
  2282. alg->base.cra_priority = CC_CRA_PRIO;
  2283. alg->base.cra_ctxsize = sizeof(struct cc_aead_ctx);
  2284. alg->base.cra_flags = CRYPTO_ALG_ASYNC | CRYPTO_ALG_KERN_DRIVER_ONLY;
  2285. alg->init = cc_aead_init;
  2286. alg->exit = cc_aead_exit;
  2287. t_alg->aead_alg = *alg;
  2288. t_alg->cipher_mode = tmpl->cipher_mode;
  2289. t_alg->flow_mode = tmpl->flow_mode;
  2290. t_alg->auth_mode = tmpl->auth_mode;
  2291. return t_alg;
  2292. }
  2293. int cc_aead_free(struct cc_drvdata *drvdata)
  2294. {
  2295. struct cc_crypto_alg *t_alg, *n;
  2296. struct cc_aead_handle *aead_handle =
  2297. (struct cc_aead_handle *)drvdata->aead_handle;
  2298. if (aead_handle) {
  2299. /* Remove registered algs */
  2300. list_for_each_entry_safe(t_alg, n, &aead_handle->aead_list,
  2301. entry) {
  2302. crypto_unregister_aead(&t_alg->aead_alg);
  2303. list_del(&t_alg->entry);
  2304. kfree(t_alg);
  2305. }
  2306. kfree(aead_handle);
  2307. drvdata->aead_handle = NULL;
  2308. }
  2309. return 0;
  2310. }
  2311. int cc_aead_alloc(struct cc_drvdata *drvdata)
  2312. {
  2313. struct cc_aead_handle *aead_handle;
  2314. struct cc_crypto_alg *t_alg;
  2315. int rc = -ENOMEM;
  2316. int alg;
  2317. struct device *dev = drvdata_to_dev(drvdata);
  2318. aead_handle = kmalloc(sizeof(*aead_handle), GFP_KERNEL);
  2319. if (!aead_handle) {
  2320. rc = -ENOMEM;
  2321. goto fail0;
  2322. }
  2323. INIT_LIST_HEAD(&aead_handle->aead_list);
  2324. drvdata->aead_handle = aead_handle;
  2325. aead_handle->sram_workspace_addr = cc_sram_alloc(drvdata,
  2326. MAX_HMAC_DIGEST_SIZE);
  2327. if (aead_handle->sram_workspace_addr == NULL_SRAM_ADDR) {
  2328. dev_err(dev, "SRAM pool exhausted\n");
  2329. rc = -ENOMEM;
  2330. goto fail1;
  2331. }
  2332. /* Linux crypto */
  2333. for (alg = 0; alg < ARRAY_SIZE(aead_algs); alg++) {
  2334. if (aead_algs[alg].min_hw_rev > drvdata->hw_rev)
  2335. continue;
  2336. t_alg = cc_create_aead_alg(&aead_algs[alg], dev);
  2337. if (IS_ERR(t_alg)) {
  2338. rc = PTR_ERR(t_alg);
  2339. dev_err(dev, "%s alg allocation failed\n",
  2340. aead_algs[alg].driver_name);
  2341. goto fail1;
  2342. }
  2343. t_alg->drvdata = drvdata;
  2344. rc = crypto_register_aead(&t_alg->aead_alg);
  2345. if (rc) {
  2346. dev_err(dev, "%s alg registration failed\n",
  2347. t_alg->aead_alg.base.cra_driver_name);
  2348. goto fail2;
  2349. } else {
  2350. list_add_tail(&t_alg->entry, &aead_handle->aead_list);
  2351. dev_dbg(dev, "Registered %s\n",
  2352. t_alg->aead_alg.base.cra_driver_name);
  2353. }
  2354. }
  2355. return 0;
  2356. fail2:
  2357. kfree(t_alg);
  2358. fail1:
  2359. cc_aead_free(drvdata);
  2360. fail0:
  2361. return rc;
  2362. }