cc_aead.c 77 KB

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