cc_hash.c 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370
  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/hash.h>
  7. #include <crypto/md5.h>
  8. #include <crypto/sm3.h>
  9. #include <crypto/internal/hash.h>
  10. #include "cc_driver.h"
  11. #include "cc_request_mgr.h"
  12. #include "cc_buffer_mgr.h"
  13. #include "cc_hash.h"
  14. #include "cc_sram_mgr.h"
  15. #define CC_MAX_HASH_SEQ_LEN 12
  16. #define CC_MAX_OPAD_KEYS_SIZE CC_MAX_HASH_BLCK_SIZE
  17. #define CC_SM3_HASH_LEN_SIZE 8
  18. struct cc_hash_handle {
  19. cc_sram_addr_t digest_len_sram_addr; /* const value in SRAM*/
  20. cc_sram_addr_t larval_digest_sram_addr; /* const value in SRAM */
  21. struct list_head hash_list;
  22. };
  23. static const u32 cc_digest_len_init[] = {
  24. 0x00000040, 0x00000000, 0x00000000, 0x00000000 };
  25. static const u32 cc_md5_init[] = {
  26. SHA1_H3, SHA1_H2, SHA1_H1, SHA1_H0 };
  27. static const u32 cc_sha1_init[] = {
  28. SHA1_H4, SHA1_H3, SHA1_H2, SHA1_H1, SHA1_H0 };
  29. static const u32 cc_sha224_init[] = {
  30. SHA224_H7, SHA224_H6, SHA224_H5, SHA224_H4,
  31. SHA224_H3, SHA224_H2, SHA224_H1, SHA224_H0 };
  32. static const u32 cc_sha256_init[] = {
  33. SHA256_H7, SHA256_H6, SHA256_H5, SHA256_H4,
  34. SHA256_H3, SHA256_H2, SHA256_H1, SHA256_H0 };
  35. static const u32 cc_digest_len_sha512_init[] = {
  36. 0x00000080, 0x00000000, 0x00000000, 0x00000000 };
  37. static u64 cc_sha384_init[] = {
  38. SHA384_H7, SHA384_H6, SHA384_H5, SHA384_H4,
  39. SHA384_H3, SHA384_H2, SHA384_H1, SHA384_H0 };
  40. static u64 cc_sha512_init[] = {
  41. SHA512_H7, SHA512_H6, SHA512_H5, SHA512_H4,
  42. SHA512_H3, SHA512_H2, SHA512_H1, SHA512_H0 };
  43. static const u32 cc_sm3_init[] = {
  44. SM3_IVH, SM3_IVG, SM3_IVF, SM3_IVE,
  45. SM3_IVD, SM3_IVC, SM3_IVB, SM3_IVA };
  46. static void cc_setup_xcbc(struct ahash_request *areq, struct cc_hw_desc desc[],
  47. unsigned int *seq_size);
  48. static void cc_setup_cmac(struct ahash_request *areq, struct cc_hw_desc desc[],
  49. unsigned int *seq_size);
  50. static const void *cc_larval_digest(struct device *dev, u32 mode);
  51. struct cc_hash_alg {
  52. struct list_head entry;
  53. int hash_mode;
  54. int hw_mode;
  55. int inter_digestsize;
  56. struct cc_drvdata *drvdata;
  57. struct ahash_alg ahash_alg;
  58. };
  59. struct hash_key_req_ctx {
  60. u32 keylen;
  61. dma_addr_t key_dma_addr;
  62. u8 *key;
  63. };
  64. /* hash per-session context */
  65. struct cc_hash_ctx {
  66. struct cc_drvdata *drvdata;
  67. /* holds the origin digest; the digest after "setkey" if HMAC,*
  68. * the initial digest if HASH.
  69. */
  70. u8 digest_buff[CC_MAX_HASH_DIGEST_SIZE] ____cacheline_aligned;
  71. u8 opad_tmp_keys_buff[CC_MAX_OPAD_KEYS_SIZE] ____cacheline_aligned;
  72. dma_addr_t opad_tmp_keys_dma_addr ____cacheline_aligned;
  73. dma_addr_t digest_buff_dma_addr;
  74. /* use for hmac with key large then mode block size */
  75. struct hash_key_req_ctx key_params;
  76. int hash_mode;
  77. int hw_mode;
  78. int inter_digestsize;
  79. unsigned int hash_len;
  80. struct completion setkey_comp;
  81. bool is_hmac;
  82. };
  83. static void cc_set_desc(struct ahash_req_ctx *areq_ctx, struct cc_hash_ctx *ctx,
  84. unsigned int flow_mode, struct cc_hw_desc desc[],
  85. bool is_not_last_data, unsigned int *seq_size);
  86. static void cc_set_endianity(u32 mode, struct cc_hw_desc *desc)
  87. {
  88. if (mode == DRV_HASH_MD5 || mode == DRV_HASH_SHA384 ||
  89. mode == DRV_HASH_SHA512) {
  90. set_bytes_swap(desc, 1);
  91. } else {
  92. set_cipher_config0(desc, HASH_DIGEST_RESULT_LITTLE_ENDIAN);
  93. }
  94. }
  95. static int cc_map_result(struct device *dev, struct ahash_req_ctx *state,
  96. unsigned int digestsize)
  97. {
  98. state->digest_result_dma_addr =
  99. dma_map_single(dev, state->digest_result_buff,
  100. digestsize, DMA_BIDIRECTIONAL);
  101. if (dma_mapping_error(dev, state->digest_result_dma_addr)) {
  102. dev_err(dev, "Mapping digest result buffer %u B for DMA failed\n",
  103. digestsize);
  104. return -ENOMEM;
  105. }
  106. dev_dbg(dev, "Mapped digest result buffer %u B at va=%pK to dma=%pad\n",
  107. digestsize, state->digest_result_buff,
  108. &state->digest_result_dma_addr);
  109. return 0;
  110. }
  111. static void cc_init_req(struct device *dev, struct ahash_req_ctx *state,
  112. struct cc_hash_ctx *ctx)
  113. {
  114. bool is_hmac = ctx->is_hmac;
  115. memset(state, 0, sizeof(*state));
  116. if (is_hmac) {
  117. if (ctx->hw_mode != DRV_CIPHER_XCBC_MAC &&
  118. ctx->hw_mode != DRV_CIPHER_CMAC) {
  119. dma_sync_single_for_cpu(dev, ctx->digest_buff_dma_addr,
  120. ctx->inter_digestsize,
  121. DMA_BIDIRECTIONAL);
  122. memcpy(state->digest_buff, ctx->digest_buff,
  123. ctx->inter_digestsize);
  124. if (ctx->hash_mode == DRV_HASH_SHA512 ||
  125. ctx->hash_mode == DRV_HASH_SHA384)
  126. memcpy(state->digest_bytes_len,
  127. cc_digest_len_sha512_init,
  128. ctx->hash_len);
  129. else
  130. memcpy(state->digest_bytes_len,
  131. cc_digest_len_init,
  132. ctx->hash_len);
  133. }
  134. if (ctx->hash_mode != DRV_HASH_NULL) {
  135. dma_sync_single_for_cpu(dev,
  136. ctx->opad_tmp_keys_dma_addr,
  137. ctx->inter_digestsize,
  138. DMA_BIDIRECTIONAL);
  139. memcpy(state->opad_digest_buff,
  140. ctx->opad_tmp_keys_buff, ctx->inter_digestsize);
  141. }
  142. } else { /*hash*/
  143. /* Copy the initial digests if hash flow. */
  144. const void *larval = cc_larval_digest(dev, ctx->hash_mode);
  145. memcpy(state->digest_buff, larval, ctx->inter_digestsize);
  146. }
  147. }
  148. static int cc_map_req(struct device *dev, struct ahash_req_ctx *state,
  149. struct cc_hash_ctx *ctx)
  150. {
  151. bool is_hmac = ctx->is_hmac;
  152. state->digest_buff_dma_addr =
  153. dma_map_single(dev, state->digest_buff,
  154. ctx->inter_digestsize, DMA_BIDIRECTIONAL);
  155. if (dma_mapping_error(dev, state->digest_buff_dma_addr)) {
  156. dev_err(dev, "Mapping digest len %d B at va=%pK for DMA failed\n",
  157. ctx->inter_digestsize, state->digest_buff);
  158. return -EINVAL;
  159. }
  160. dev_dbg(dev, "Mapped digest %d B at va=%pK to dma=%pad\n",
  161. ctx->inter_digestsize, state->digest_buff,
  162. &state->digest_buff_dma_addr);
  163. if (ctx->hw_mode != DRV_CIPHER_XCBC_MAC) {
  164. state->digest_bytes_len_dma_addr =
  165. dma_map_single(dev, state->digest_bytes_len,
  166. HASH_MAX_LEN_SIZE, DMA_BIDIRECTIONAL);
  167. if (dma_mapping_error(dev, state->digest_bytes_len_dma_addr)) {
  168. dev_err(dev, "Mapping digest len %u B at va=%pK for DMA failed\n",
  169. HASH_MAX_LEN_SIZE, state->digest_bytes_len);
  170. goto unmap_digest_buf;
  171. }
  172. dev_dbg(dev, "Mapped digest len %u B at va=%pK to dma=%pad\n",
  173. HASH_MAX_LEN_SIZE, state->digest_bytes_len,
  174. &state->digest_bytes_len_dma_addr);
  175. }
  176. if (is_hmac && ctx->hash_mode != DRV_HASH_NULL) {
  177. state->opad_digest_dma_addr =
  178. dma_map_single(dev, state->opad_digest_buff,
  179. ctx->inter_digestsize,
  180. DMA_BIDIRECTIONAL);
  181. if (dma_mapping_error(dev, state->opad_digest_dma_addr)) {
  182. dev_err(dev, "Mapping opad digest %d B at va=%pK for DMA failed\n",
  183. ctx->inter_digestsize,
  184. state->opad_digest_buff);
  185. goto unmap_digest_len;
  186. }
  187. dev_dbg(dev, "Mapped opad digest %d B at va=%pK to dma=%pad\n",
  188. ctx->inter_digestsize, state->opad_digest_buff,
  189. &state->opad_digest_dma_addr);
  190. }
  191. return 0;
  192. unmap_digest_len:
  193. if (state->digest_bytes_len_dma_addr) {
  194. dma_unmap_single(dev, state->digest_bytes_len_dma_addr,
  195. HASH_MAX_LEN_SIZE, DMA_BIDIRECTIONAL);
  196. state->digest_bytes_len_dma_addr = 0;
  197. }
  198. unmap_digest_buf:
  199. if (state->digest_buff_dma_addr) {
  200. dma_unmap_single(dev, state->digest_buff_dma_addr,
  201. ctx->inter_digestsize, DMA_BIDIRECTIONAL);
  202. state->digest_buff_dma_addr = 0;
  203. }
  204. return -EINVAL;
  205. }
  206. static void cc_unmap_req(struct device *dev, struct ahash_req_ctx *state,
  207. struct cc_hash_ctx *ctx)
  208. {
  209. if (state->digest_buff_dma_addr) {
  210. dma_unmap_single(dev, state->digest_buff_dma_addr,
  211. ctx->inter_digestsize, DMA_BIDIRECTIONAL);
  212. dev_dbg(dev, "Unmapped digest-buffer: digest_buff_dma_addr=%pad\n",
  213. &state->digest_buff_dma_addr);
  214. state->digest_buff_dma_addr = 0;
  215. }
  216. if (state->digest_bytes_len_dma_addr) {
  217. dma_unmap_single(dev, state->digest_bytes_len_dma_addr,
  218. HASH_MAX_LEN_SIZE, DMA_BIDIRECTIONAL);
  219. dev_dbg(dev, "Unmapped digest-bytes-len buffer: digest_bytes_len_dma_addr=%pad\n",
  220. &state->digest_bytes_len_dma_addr);
  221. state->digest_bytes_len_dma_addr = 0;
  222. }
  223. if (state->opad_digest_dma_addr) {
  224. dma_unmap_single(dev, state->opad_digest_dma_addr,
  225. ctx->inter_digestsize, DMA_BIDIRECTIONAL);
  226. dev_dbg(dev, "Unmapped opad-digest: opad_digest_dma_addr=%pad\n",
  227. &state->opad_digest_dma_addr);
  228. state->opad_digest_dma_addr = 0;
  229. }
  230. }
  231. static void cc_unmap_result(struct device *dev, struct ahash_req_ctx *state,
  232. unsigned int digestsize, u8 *result)
  233. {
  234. if (state->digest_result_dma_addr) {
  235. dma_unmap_single(dev, state->digest_result_dma_addr, digestsize,
  236. DMA_BIDIRECTIONAL);
  237. dev_dbg(dev, "unmpa digest result buffer va (%pK) pa (%pad) len %u\n",
  238. state->digest_result_buff,
  239. &state->digest_result_dma_addr, digestsize);
  240. memcpy(result, state->digest_result_buff, digestsize);
  241. }
  242. state->digest_result_dma_addr = 0;
  243. }
  244. static void cc_update_complete(struct device *dev, void *cc_req, int err)
  245. {
  246. struct ahash_request *req = (struct ahash_request *)cc_req;
  247. struct ahash_req_ctx *state = ahash_request_ctx(req);
  248. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  249. struct cc_hash_ctx *ctx = crypto_ahash_ctx(tfm);
  250. dev_dbg(dev, "req=%pK\n", req);
  251. if (err != -EINPROGRESS) {
  252. /* Not a BACKLOG notification */
  253. cc_unmap_hash_request(dev, state, req->src, false);
  254. cc_unmap_req(dev, state, ctx);
  255. }
  256. ahash_request_complete(req, err);
  257. }
  258. static void cc_digest_complete(struct device *dev, void *cc_req, int err)
  259. {
  260. struct ahash_request *req = (struct ahash_request *)cc_req;
  261. struct ahash_req_ctx *state = ahash_request_ctx(req);
  262. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  263. struct cc_hash_ctx *ctx = crypto_ahash_ctx(tfm);
  264. u32 digestsize = crypto_ahash_digestsize(tfm);
  265. dev_dbg(dev, "req=%pK\n", req);
  266. if (err != -EINPROGRESS) {
  267. /* Not a BACKLOG notification */
  268. cc_unmap_hash_request(dev, state, req->src, false);
  269. cc_unmap_result(dev, state, digestsize, req->result);
  270. cc_unmap_req(dev, state, ctx);
  271. }
  272. ahash_request_complete(req, err);
  273. }
  274. static void cc_hash_complete(struct device *dev, void *cc_req, int err)
  275. {
  276. struct ahash_request *req = (struct ahash_request *)cc_req;
  277. struct ahash_req_ctx *state = ahash_request_ctx(req);
  278. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  279. struct cc_hash_ctx *ctx = crypto_ahash_ctx(tfm);
  280. u32 digestsize = crypto_ahash_digestsize(tfm);
  281. dev_dbg(dev, "req=%pK\n", req);
  282. if (err != -EINPROGRESS) {
  283. /* Not a BACKLOG notification */
  284. cc_unmap_hash_request(dev, state, req->src, false);
  285. cc_unmap_result(dev, state, digestsize, req->result);
  286. cc_unmap_req(dev, state, ctx);
  287. }
  288. ahash_request_complete(req, err);
  289. }
  290. static int cc_fin_result(struct cc_hw_desc *desc, struct ahash_request *req,
  291. int idx)
  292. {
  293. struct ahash_req_ctx *state = ahash_request_ctx(req);
  294. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  295. struct cc_hash_ctx *ctx = crypto_ahash_ctx(tfm);
  296. u32 digestsize = crypto_ahash_digestsize(tfm);
  297. /* Get final MAC result */
  298. hw_desc_init(&desc[idx]);
  299. set_hash_cipher_mode(&desc[idx], ctx->hw_mode, ctx->hash_mode);
  300. /* TODO */
  301. set_dout_dlli(&desc[idx], state->digest_result_dma_addr, digestsize,
  302. NS_BIT, 1);
  303. set_queue_last_ind(ctx->drvdata, &desc[idx]);
  304. set_flow_mode(&desc[idx], S_HASH_to_DOUT);
  305. set_setup_mode(&desc[idx], SETUP_WRITE_STATE0);
  306. set_cipher_config1(&desc[idx], HASH_PADDING_DISABLED);
  307. cc_set_endianity(ctx->hash_mode, &desc[idx]);
  308. idx++;
  309. return idx;
  310. }
  311. static int cc_fin_hmac(struct cc_hw_desc *desc, struct ahash_request *req,
  312. int idx)
  313. {
  314. struct ahash_req_ctx *state = ahash_request_ctx(req);
  315. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  316. struct cc_hash_ctx *ctx = crypto_ahash_ctx(tfm);
  317. u32 digestsize = crypto_ahash_digestsize(tfm);
  318. /* store the hash digest result in the context */
  319. hw_desc_init(&desc[idx]);
  320. set_cipher_mode(&desc[idx], ctx->hw_mode);
  321. set_dout_dlli(&desc[idx], state->digest_buff_dma_addr, digestsize,
  322. NS_BIT, 0);
  323. set_flow_mode(&desc[idx], S_HASH_to_DOUT);
  324. cc_set_endianity(ctx->hash_mode, &desc[idx]);
  325. set_setup_mode(&desc[idx], SETUP_WRITE_STATE0);
  326. idx++;
  327. /* Loading hash opad xor key state */
  328. hw_desc_init(&desc[idx]);
  329. set_cipher_mode(&desc[idx], ctx->hw_mode);
  330. set_din_type(&desc[idx], DMA_DLLI, state->opad_digest_dma_addr,
  331. ctx->inter_digestsize, NS_BIT);
  332. set_flow_mode(&desc[idx], S_DIN_to_HASH);
  333. set_setup_mode(&desc[idx], SETUP_LOAD_STATE0);
  334. idx++;
  335. /* Load the hash current length */
  336. hw_desc_init(&desc[idx]);
  337. set_cipher_mode(&desc[idx], ctx->hw_mode);
  338. set_din_sram(&desc[idx],
  339. cc_digest_len_addr(ctx->drvdata, ctx->hash_mode),
  340. ctx->hash_len);
  341. set_cipher_config1(&desc[idx], HASH_PADDING_ENABLED);
  342. set_flow_mode(&desc[idx], S_DIN_to_HASH);
  343. set_setup_mode(&desc[idx], SETUP_LOAD_KEY0);
  344. idx++;
  345. /* Memory Barrier: wait for IPAD/OPAD axi write to complete */
  346. hw_desc_init(&desc[idx]);
  347. set_din_no_dma(&desc[idx], 0, 0xfffff0);
  348. set_dout_no_dma(&desc[idx], 0, 0, 1);
  349. idx++;
  350. /* Perform HASH update */
  351. hw_desc_init(&desc[idx]);
  352. set_din_type(&desc[idx], DMA_DLLI, state->digest_buff_dma_addr,
  353. digestsize, NS_BIT);
  354. set_flow_mode(&desc[idx], DIN_HASH);
  355. idx++;
  356. return idx;
  357. }
  358. static int cc_hash_digest(struct ahash_request *req)
  359. {
  360. struct ahash_req_ctx *state = ahash_request_ctx(req);
  361. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  362. struct cc_hash_ctx *ctx = crypto_ahash_ctx(tfm);
  363. u32 digestsize = crypto_ahash_digestsize(tfm);
  364. struct scatterlist *src = req->src;
  365. unsigned int nbytes = req->nbytes;
  366. u8 *result = req->result;
  367. struct device *dev = drvdata_to_dev(ctx->drvdata);
  368. bool is_hmac = ctx->is_hmac;
  369. struct cc_crypto_req cc_req = {};
  370. struct cc_hw_desc desc[CC_MAX_HASH_SEQ_LEN];
  371. cc_sram_addr_t larval_digest_addr =
  372. cc_larval_digest_addr(ctx->drvdata, ctx->hash_mode);
  373. int idx = 0;
  374. int rc = 0;
  375. gfp_t flags = cc_gfp_flags(&req->base);
  376. dev_dbg(dev, "===== %s-digest (%d) ====\n", is_hmac ? "hmac" : "hash",
  377. nbytes);
  378. cc_init_req(dev, state, ctx);
  379. if (cc_map_req(dev, state, ctx)) {
  380. dev_err(dev, "map_ahash_source() failed\n");
  381. return -ENOMEM;
  382. }
  383. if (cc_map_result(dev, state, digestsize)) {
  384. dev_err(dev, "map_ahash_digest() failed\n");
  385. cc_unmap_req(dev, state, ctx);
  386. return -ENOMEM;
  387. }
  388. if (cc_map_hash_request_final(ctx->drvdata, state, src, nbytes, 1,
  389. flags)) {
  390. dev_err(dev, "map_ahash_request_final() failed\n");
  391. cc_unmap_result(dev, state, digestsize, result);
  392. cc_unmap_req(dev, state, ctx);
  393. return -ENOMEM;
  394. }
  395. /* Setup request structure */
  396. cc_req.user_cb = cc_digest_complete;
  397. cc_req.user_arg = req;
  398. /* If HMAC then load hash IPAD xor key, if HASH then load initial
  399. * digest
  400. */
  401. hw_desc_init(&desc[idx]);
  402. set_hash_cipher_mode(&desc[idx], ctx->hw_mode, ctx->hash_mode);
  403. if (is_hmac) {
  404. set_din_type(&desc[idx], DMA_DLLI, state->digest_buff_dma_addr,
  405. ctx->inter_digestsize, NS_BIT);
  406. } else {
  407. set_din_sram(&desc[idx], larval_digest_addr,
  408. ctx->inter_digestsize);
  409. }
  410. set_flow_mode(&desc[idx], S_DIN_to_HASH);
  411. set_setup_mode(&desc[idx], SETUP_LOAD_STATE0);
  412. idx++;
  413. /* Load the hash current length */
  414. hw_desc_init(&desc[idx]);
  415. set_hash_cipher_mode(&desc[idx], ctx->hw_mode, ctx->hash_mode);
  416. if (is_hmac) {
  417. set_din_type(&desc[idx], DMA_DLLI,
  418. state->digest_bytes_len_dma_addr,
  419. ctx->hash_len, NS_BIT);
  420. } else {
  421. set_din_const(&desc[idx], 0, ctx->hash_len);
  422. if (nbytes)
  423. set_cipher_config1(&desc[idx], HASH_PADDING_ENABLED);
  424. else
  425. set_cipher_do(&desc[idx], DO_PAD);
  426. }
  427. set_flow_mode(&desc[idx], S_DIN_to_HASH);
  428. set_setup_mode(&desc[idx], SETUP_LOAD_KEY0);
  429. idx++;
  430. cc_set_desc(state, ctx, DIN_HASH, desc, false, &idx);
  431. if (is_hmac) {
  432. /* HW last hash block padding (aka. "DO_PAD") */
  433. hw_desc_init(&desc[idx]);
  434. set_cipher_mode(&desc[idx], ctx->hw_mode);
  435. set_dout_dlli(&desc[idx], state->digest_buff_dma_addr,
  436. ctx->hash_len, NS_BIT, 0);
  437. set_flow_mode(&desc[idx], S_HASH_to_DOUT);
  438. set_setup_mode(&desc[idx], SETUP_WRITE_STATE1);
  439. set_cipher_do(&desc[idx], DO_PAD);
  440. idx++;
  441. idx = cc_fin_hmac(desc, req, idx);
  442. }
  443. idx = cc_fin_result(desc, req, idx);
  444. rc = cc_send_request(ctx->drvdata, &cc_req, desc, idx, &req->base);
  445. if (rc != -EINPROGRESS && rc != -EBUSY) {
  446. dev_err(dev, "send_request() failed (rc=%d)\n", rc);
  447. cc_unmap_hash_request(dev, state, src, true);
  448. cc_unmap_result(dev, state, digestsize, result);
  449. cc_unmap_req(dev, state, ctx);
  450. }
  451. return rc;
  452. }
  453. static int cc_restore_hash(struct cc_hw_desc *desc, struct cc_hash_ctx *ctx,
  454. struct ahash_req_ctx *state, unsigned int idx)
  455. {
  456. /* Restore hash digest */
  457. hw_desc_init(&desc[idx]);
  458. set_hash_cipher_mode(&desc[idx], ctx->hw_mode, ctx->hash_mode);
  459. set_din_type(&desc[idx], DMA_DLLI, state->digest_buff_dma_addr,
  460. ctx->inter_digestsize, NS_BIT);
  461. set_flow_mode(&desc[idx], S_DIN_to_HASH);
  462. set_setup_mode(&desc[idx], SETUP_LOAD_STATE0);
  463. idx++;
  464. /* Restore hash current length */
  465. hw_desc_init(&desc[idx]);
  466. set_hash_cipher_mode(&desc[idx], ctx->hw_mode, ctx->hash_mode);
  467. set_cipher_config1(&desc[idx], HASH_PADDING_DISABLED);
  468. set_din_type(&desc[idx], DMA_DLLI, state->digest_bytes_len_dma_addr,
  469. ctx->hash_len, NS_BIT);
  470. set_flow_mode(&desc[idx], S_DIN_to_HASH);
  471. set_setup_mode(&desc[idx], SETUP_LOAD_KEY0);
  472. idx++;
  473. cc_set_desc(state, ctx, DIN_HASH, desc, false, &idx);
  474. return idx;
  475. }
  476. static int cc_hash_update(struct ahash_request *req)
  477. {
  478. struct ahash_req_ctx *state = ahash_request_ctx(req);
  479. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  480. struct cc_hash_ctx *ctx = crypto_ahash_ctx(tfm);
  481. unsigned int block_size = crypto_tfm_alg_blocksize(&tfm->base);
  482. struct scatterlist *src = req->src;
  483. unsigned int nbytes = req->nbytes;
  484. struct device *dev = drvdata_to_dev(ctx->drvdata);
  485. struct cc_crypto_req cc_req = {};
  486. struct cc_hw_desc desc[CC_MAX_HASH_SEQ_LEN];
  487. u32 idx = 0;
  488. int rc;
  489. gfp_t flags = cc_gfp_flags(&req->base);
  490. dev_dbg(dev, "===== %s-update (%d) ====\n", ctx->is_hmac ?
  491. "hmac" : "hash", nbytes);
  492. if (nbytes == 0) {
  493. /* no real updates required */
  494. return 0;
  495. }
  496. rc = cc_map_hash_request_update(ctx->drvdata, state, src, nbytes,
  497. block_size, flags);
  498. if (rc) {
  499. if (rc == 1) {
  500. dev_dbg(dev, " data size not require HW update %x\n",
  501. nbytes);
  502. /* No hardware updates are required */
  503. return 0;
  504. }
  505. dev_err(dev, "map_ahash_request_update() failed\n");
  506. return -ENOMEM;
  507. }
  508. if (cc_map_req(dev, state, ctx)) {
  509. dev_err(dev, "map_ahash_source() failed\n");
  510. cc_unmap_hash_request(dev, state, src, true);
  511. return -EINVAL;
  512. }
  513. /* Setup request structure */
  514. cc_req.user_cb = cc_update_complete;
  515. cc_req.user_arg = req;
  516. idx = cc_restore_hash(desc, ctx, state, idx);
  517. /* store the hash digest result in context */
  518. hw_desc_init(&desc[idx]);
  519. set_hash_cipher_mode(&desc[idx], ctx->hw_mode, ctx->hash_mode);
  520. set_dout_dlli(&desc[idx], state->digest_buff_dma_addr,
  521. ctx->inter_digestsize, NS_BIT, 0);
  522. set_flow_mode(&desc[idx], S_HASH_to_DOUT);
  523. set_setup_mode(&desc[idx], SETUP_WRITE_STATE0);
  524. idx++;
  525. /* store current hash length in context */
  526. hw_desc_init(&desc[idx]);
  527. set_hash_cipher_mode(&desc[idx], ctx->hw_mode, ctx->hash_mode);
  528. set_dout_dlli(&desc[idx], state->digest_bytes_len_dma_addr,
  529. ctx->hash_len, NS_BIT, 1);
  530. set_queue_last_ind(ctx->drvdata, &desc[idx]);
  531. set_flow_mode(&desc[idx], S_HASH_to_DOUT);
  532. set_setup_mode(&desc[idx], SETUP_WRITE_STATE1);
  533. idx++;
  534. rc = cc_send_request(ctx->drvdata, &cc_req, desc, idx, &req->base);
  535. if (rc != -EINPROGRESS && rc != -EBUSY) {
  536. dev_err(dev, "send_request() failed (rc=%d)\n", rc);
  537. cc_unmap_hash_request(dev, state, src, true);
  538. cc_unmap_req(dev, state, ctx);
  539. }
  540. return rc;
  541. }
  542. static int cc_do_finup(struct ahash_request *req, bool update)
  543. {
  544. struct ahash_req_ctx *state = ahash_request_ctx(req);
  545. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  546. struct cc_hash_ctx *ctx = crypto_ahash_ctx(tfm);
  547. u32 digestsize = crypto_ahash_digestsize(tfm);
  548. struct scatterlist *src = req->src;
  549. unsigned int nbytes = req->nbytes;
  550. u8 *result = req->result;
  551. struct device *dev = drvdata_to_dev(ctx->drvdata);
  552. bool is_hmac = ctx->is_hmac;
  553. struct cc_crypto_req cc_req = {};
  554. struct cc_hw_desc desc[CC_MAX_HASH_SEQ_LEN];
  555. unsigned int idx = 0;
  556. int rc;
  557. gfp_t flags = cc_gfp_flags(&req->base);
  558. dev_dbg(dev, "===== %s-%s (%d) ====\n", is_hmac ? "hmac" : "hash",
  559. update ? "finup" : "final", nbytes);
  560. if (cc_map_req(dev, state, ctx)) {
  561. dev_err(dev, "map_ahash_source() failed\n");
  562. return -EINVAL;
  563. }
  564. if (cc_map_hash_request_final(ctx->drvdata, state, src, nbytes, update,
  565. flags)) {
  566. dev_err(dev, "map_ahash_request_final() failed\n");
  567. cc_unmap_req(dev, state, ctx);
  568. return -ENOMEM;
  569. }
  570. if (cc_map_result(dev, state, digestsize)) {
  571. dev_err(dev, "map_ahash_digest() failed\n");
  572. cc_unmap_hash_request(dev, state, src, true);
  573. cc_unmap_req(dev, state, ctx);
  574. return -ENOMEM;
  575. }
  576. /* Setup request structure */
  577. cc_req.user_cb = cc_hash_complete;
  578. cc_req.user_arg = req;
  579. idx = cc_restore_hash(desc, ctx, state, idx);
  580. /* Pad the hash */
  581. hw_desc_init(&desc[idx]);
  582. set_cipher_do(&desc[idx], DO_PAD);
  583. set_hash_cipher_mode(&desc[idx], ctx->hw_mode, ctx->hash_mode);
  584. set_dout_dlli(&desc[idx], state->digest_bytes_len_dma_addr,
  585. ctx->hash_len, NS_BIT, 0);
  586. set_setup_mode(&desc[idx], SETUP_WRITE_STATE1);
  587. set_flow_mode(&desc[idx], S_HASH_to_DOUT);
  588. idx++;
  589. if (is_hmac)
  590. idx = cc_fin_hmac(desc, req, idx);
  591. idx = cc_fin_result(desc, req, idx);
  592. rc = cc_send_request(ctx->drvdata, &cc_req, desc, idx, &req->base);
  593. if (rc != -EINPROGRESS && rc != -EBUSY) {
  594. dev_err(dev, "send_request() failed (rc=%d)\n", rc);
  595. cc_unmap_hash_request(dev, state, src, true);
  596. cc_unmap_result(dev, state, digestsize, result);
  597. cc_unmap_req(dev, state, ctx);
  598. }
  599. return rc;
  600. }
  601. static int cc_hash_finup(struct ahash_request *req)
  602. {
  603. return cc_do_finup(req, true);
  604. }
  605. static int cc_hash_final(struct ahash_request *req)
  606. {
  607. return cc_do_finup(req, false);
  608. }
  609. static int cc_hash_init(struct ahash_request *req)
  610. {
  611. struct ahash_req_ctx *state = ahash_request_ctx(req);
  612. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  613. struct cc_hash_ctx *ctx = crypto_ahash_ctx(tfm);
  614. struct device *dev = drvdata_to_dev(ctx->drvdata);
  615. dev_dbg(dev, "===== init (%d) ====\n", req->nbytes);
  616. cc_init_req(dev, state, ctx);
  617. return 0;
  618. }
  619. static int cc_hash_setkey(struct crypto_ahash *ahash, const u8 *key,
  620. unsigned int keylen)
  621. {
  622. unsigned int hmac_pad_const[2] = { HMAC_IPAD_CONST, HMAC_OPAD_CONST };
  623. struct cc_crypto_req cc_req = {};
  624. struct cc_hash_ctx *ctx = NULL;
  625. int blocksize = 0;
  626. int digestsize = 0;
  627. int i, idx = 0, rc = 0;
  628. struct cc_hw_desc desc[CC_MAX_HASH_SEQ_LEN];
  629. cc_sram_addr_t larval_addr;
  630. struct device *dev;
  631. ctx = crypto_ahash_ctx(ahash);
  632. dev = drvdata_to_dev(ctx->drvdata);
  633. dev_dbg(dev, "start keylen: %d", keylen);
  634. blocksize = crypto_tfm_alg_blocksize(&ahash->base);
  635. digestsize = crypto_ahash_digestsize(ahash);
  636. larval_addr = cc_larval_digest_addr(ctx->drvdata, ctx->hash_mode);
  637. /* The keylen value distinguishes HASH in case keylen is ZERO bytes,
  638. * any NON-ZERO value utilizes HMAC flow
  639. */
  640. ctx->key_params.keylen = keylen;
  641. ctx->key_params.key_dma_addr = 0;
  642. ctx->is_hmac = true;
  643. ctx->key_params.key = NULL;
  644. if (keylen) {
  645. ctx->key_params.key = kmemdup(key, keylen, GFP_KERNEL);
  646. if (!ctx->key_params.key)
  647. return -ENOMEM;
  648. ctx->key_params.key_dma_addr =
  649. dma_map_single(dev, (void *)ctx->key_params.key, keylen,
  650. DMA_TO_DEVICE);
  651. if (dma_mapping_error(dev, ctx->key_params.key_dma_addr)) {
  652. dev_err(dev, "Mapping key va=0x%p len=%u for DMA failed\n",
  653. ctx->key_params.key, keylen);
  654. kzfree(ctx->key_params.key);
  655. return -ENOMEM;
  656. }
  657. dev_dbg(dev, "mapping key-buffer: key_dma_addr=%pad keylen=%u\n",
  658. &ctx->key_params.key_dma_addr, ctx->key_params.keylen);
  659. if (keylen > blocksize) {
  660. /* Load hash initial state */
  661. hw_desc_init(&desc[idx]);
  662. set_cipher_mode(&desc[idx], ctx->hw_mode);
  663. set_din_sram(&desc[idx], larval_addr,
  664. ctx->inter_digestsize);
  665. set_flow_mode(&desc[idx], S_DIN_to_HASH);
  666. set_setup_mode(&desc[idx], SETUP_LOAD_STATE0);
  667. idx++;
  668. /* Load the hash current length*/
  669. hw_desc_init(&desc[idx]);
  670. set_cipher_mode(&desc[idx], ctx->hw_mode);
  671. set_din_const(&desc[idx], 0, ctx->hash_len);
  672. set_cipher_config1(&desc[idx], HASH_PADDING_ENABLED);
  673. set_flow_mode(&desc[idx], S_DIN_to_HASH);
  674. set_setup_mode(&desc[idx], SETUP_LOAD_KEY0);
  675. idx++;
  676. hw_desc_init(&desc[idx]);
  677. set_din_type(&desc[idx], DMA_DLLI,
  678. ctx->key_params.key_dma_addr, keylen,
  679. NS_BIT);
  680. set_flow_mode(&desc[idx], DIN_HASH);
  681. idx++;
  682. /* Get hashed key */
  683. hw_desc_init(&desc[idx]);
  684. set_cipher_mode(&desc[idx], ctx->hw_mode);
  685. set_dout_dlli(&desc[idx], ctx->opad_tmp_keys_dma_addr,
  686. digestsize, NS_BIT, 0);
  687. set_flow_mode(&desc[idx], S_HASH_to_DOUT);
  688. set_setup_mode(&desc[idx], SETUP_WRITE_STATE0);
  689. set_cipher_config1(&desc[idx], HASH_PADDING_DISABLED);
  690. cc_set_endianity(ctx->hash_mode, &desc[idx]);
  691. idx++;
  692. hw_desc_init(&desc[idx]);
  693. set_din_const(&desc[idx], 0, (blocksize - digestsize));
  694. set_flow_mode(&desc[idx], BYPASS);
  695. set_dout_dlli(&desc[idx],
  696. (ctx->opad_tmp_keys_dma_addr +
  697. digestsize),
  698. (blocksize - digestsize), NS_BIT, 0);
  699. idx++;
  700. } else {
  701. hw_desc_init(&desc[idx]);
  702. set_din_type(&desc[idx], DMA_DLLI,
  703. ctx->key_params.key_dma_addr, keylen,
  704. NS_BIT);
  705. set_flow_mode(&desc[idx], BYPASS);
  706. set_dout_dlli(&desc[idx], ctx->opad_tmp_keys_dma_addr,
  707. keylen, NS_BIT, 0);
  708. idx++;
  709. if ((blocksize - keylen)) {
  710. hw_desc_init(&desc[idx]);
  711. set_din_const(&desc[idx], 0,
  712. (blocksize - keylen));
  713. set_flow_mode(&desc[idx], BYPASS);
  714. set_dout_dlli(&desc[idx],
  715. (ctx->opad_tmp_keys_dma_addr +
  716. keylen), (blocksize - keylen),
  717. NS_BIT, 0);
  718. idx++;
  719. }
  720. }
  721. } else {
  722. hw_desc_init(&desc[idx]);
  723. set_din_const(&desc[idx], 0, blocksize);
  724. set_flow_mode(&desc[idx], BYPASS);
  725. set_dout_dlli(&desc[idx], (ctx->opad_tmp_keys_dma_addr),
  726. blocksize, NS_BIT, 0);
  727. idx++;
  728. }
  729. rc = cc_send_sync_request(ctx->drvdata, &cc_req, desc, idx);
  730. if (rc) {
  731. dev_err(dev, "send_request() failed (rc=%d)\n", rc);
  732. goto out;
  733. }
  734. /* calc derived HMAC key */
  735. for (idx = 0, i = 0; i < 2; i++) {
  736. /* Load hash initial state */
  737. hw_desc_init(&desc[idx]);
  738. set_cipher_mode(&desc[idx], ctx->hw_mode);
  739. set_din_sram(&desc[idx], larval_addr, ctx->inter_digestsize);
  740. set_flow_mode(&desc[idx], S_DIN_to_HASH);
  741. set_setup_mode(&desc[idx], SETUP_LOAD_STATE0);
  742. idx++;
  743. /* Load the hash current length*/
  744. hw_desc_init(&desc[idx]);
  745. set_cipher_mode(&desc[idx], ctx->hw_mode);
  746. set_din_const(&desc[idx], 0, ctx->hash_len);
  747. set_flow_mode(&desc[idx], S_DIN_to_HASH);
  748. set_setup_mode(&desc[idx], SETUP_LOAD_KEY0);
  749. idx++;
  750. /* Prepare ipad key */
  751. hw_desc_init(&desc[idx]);
  752. set_xor_val(&desc[idx], hmac_pad_const[i]);
  753. set_cipher_mode(&desc[idx], ctx->hw_mode);
  754. set_flow_mode(&desc[idx], S_DIN_to_HASH);
  755. set_setup_mode(&desc[idx], SETUP_LOAD_STATE1);
  756. idx++;
  757. /* Perform HASH update */
  758. hw_desc_init(&desc[idx]);
  759. set_din_type(&desc[idx], DMA_DLLI, ctx->opad_tmp_keys_dma_addr,
  760. blocksize, NS_BIT);
  761. set_cipher_mode(&desc[idx], ctx->hw_mode);
  762. set_xor_active(&desc[idx]);
  763. set_flow_mode(&desc[idx], DIN_HASH);
  764. idx++;
  765. /* Get the IPAD/OPAD xor key (Note, IPAD is the initial digest
  766. * of the first HASH "update" state)
  767. */
  768. hw_desc_init(&desc[idx]);
  769. set_cipher_mode(&desc[idx], ctx->hw_mode);
  770. if (i > 0) /* Not first iteration */
  771. set_dout_dlli(&desc[idx], ctx->opad_tmp_keys_dma_addr,
  772. ctx->inter_digestsize, NS_BIT, 0);
  773. else /* First iteration */
  774. set_dout_dlli(&desc[idx], ctx->digest_buff_dma_addr,
  775. ctx->inter_digestsize, NS_BIT, 0);
  776. set_flow_mode(&desc[idx], S_HASH_to_DOUT);
  777. set_setup_mode(&desc[idx], SETUP_WRITE_STATE0);
  778. idx++;
  779. }
  780. rc = cc_send_sync_request(ctx->drvdata, &cc_req, desc, idx);
  781. out:
  782. if (rc)
  783. crypto_ahash_set_flags(ahash, CRYPTO_TFM_RES_BAD_KEY_LEN);
  784. if (ctx->key_params.key_dma_addr) {
  785. dma_unmap_single(dev, ctx->key_params.key_dma_addr,
  786. ctx->key_params.keylen, DMA_TO_DEVICE);
  787. dev_dbg(dev, "Unmapped key-buffer: key_dma_addr=%pad keylen=%u\n",
  788. &ctx->key_params.key_dma_addr, ctx->key_params.keylen);
  789. }
  790. kzfree(ctx->key_params.key);
  791. return rc;
  792. }
  793. static int cc_xcbc_setkey(struct crypto_ahash *ahash,
  794. const u8 *key, unsigned int keylen)
  795. {
  796. struct cc_crypto_req cc_req = {};
  797. struct cc_hash_ctx *ctx = crypto_ahash_ctx(ahash);
  798. struct device *dev = drvdata_to_dev(ctx->drvdata);
  799. int rc = 0;
  800. unsigned int idx = 0;
  801. struct cc_hw_desc desc[CC_MAX_HASH_SEQ_LEN];
  802. dev_dbg(dev, "===== setkey (%d) ====\n", keylen);
  803. switch (keylen) {
  804. case AES_KEYSIZE_128:
  805. case AES_KEYSIZE_192:
  806. case AES_KEYSIZE_256:
  807. break;
  808. default:
  809. return -EINVAL;
  810. }
  811. ctx->key_params.keylen = keylen;
  812. ctx->key_params.key = kmemdup(key, keylen, GFP_KERNEL);
  813. if (!ctx->key_params.key)
  814. return -ENOMEM;
  815. ctx->key_params.key_dma_addr =
  816. dma_map_single(dev, ctx->key_params.key, keylen, DMA_TO_DEVICE);
  817. if (dma_mapping_error(dev, ctx->key_params.key_dma_addr)) {
  818. dev_err(dev, "Mapping key va=0x%p len=%u for DMA failed\n",
  819. key, keylen);
  820. kzfree(ctx->key_params.key);
  821. return -ENOMEM;
  822. }
  823. dev_dbg(dev, "mapping key-buffer: key_dma_addr=%pad keylen=%u\n",
  824. &ctx->key_params.key_dma_addr, ctx->key_params.keylen);
  825. ctx->is_hmac = true;
  826. /* 1. Load the AES key */
  827. hw_desc_init(&desc[idx]);
  828. set_din_type(&desc[idx], DMA_DLLI, ctx->key_params.key_dma_addr,
  829. keylen, NS_BIT);
  830. set_cipher_mode(&desc[idx], DRV_CIPHER_ECB);
  831. set_cipher_config0(&desc[idx], DRV_CRYPTO_DIRECTION_ENCRYPT);
  832. set_key_size_aes(&desc[idx], keylen);
  833. set_flow_mode(&desc[idx], S_DIN_to_AES);
  834. set_setup_mode(&desc[idx], SETUP_LOAD_KEY0);
  835. idx++;
  836. hw_desc_init(&desc[idx]);
  837. set_din_const(&desc[idx], 0x01010101, CC_AES_128_BIT_KEY_SIZE);
  838. set_flow_mode(&desc[idx], DIN_AES_DOUT);
  839. set_dout_dlli(&desc[idx],
  840. (ctx->opad_tmp_keys_dma_addr + XCBC_MAC_K1_OFFSET),
  841. CC_AES_128_BIT_KEY_SIZE, NS_BIT, 0);
  842. idx++;
  843. hw_desc_init(&desc[idx]);
  844. set_din_const(&desc[idx], 0x02020202, CC_AES_128_BIT_KEY_SIZE);
  845. set_flow_mode(&desc[idx], DIN_AES_DOUT);
  846. set_dout_dlli(&desc[idx],
  847. (ctx->opad_tmp_keys_dma_addr + XCBC_MAC_K2_OFFSET),
  848. CC_AES_128_BIT_KEY_SIZE, NS_BIT, 0);
  849. idx++;
  850. hw_desc_init(&desc[idx]);
  851. set_din_const(&desc[idx], 0x03030303, CC_AES_128_BIT_KEY_SIZE);
  852. set_flow_mode(&desc[idx], DIN_AES_DOUT);
  853. set_dout_dlli(&desc[idx],
  854. (ctx->opad_tmp_keys_dma_addr + XCBC_MAC_K3_OFFSET),
  855. CC_AES_128_BIT_KEY_SIZE, NS_BIT, 0);
  856. idx++;
  857. rc = cc_send_sync_request(ctx->drvdata, &cc_req, desc, idx);
  858. if (rc)
  859. crypto_ahash_set_flags(ahash, CRYPTO_TFM_RES_BAD_KEY_LEN);
  860. dma_unmap_single(dev, ctx->key_params.key_dma_addr,
  861. ctx->key_params.keylen, DMA_TO_DEVICE);
  862. dev_dbg(dev, "Unmapped key-buffer: key_dma_addr=%pad keylen=%u\n",
  863. &ctx->key_params.key_dma_addr, ctx->key_params.keylen);
  864. kzfree(ctx->key_params.key);
  865. return rc;
  866. }
  867. static int cc_cmac_setkey(struct crypto_ahash *ahash,
  868. const u8 *key, unsigned int keylen)
  869. {
  870. struct cc_hash_ctx *ctx = crypto_ahash_ctx(ahash);
  871. struct device *dev = drvdata_to_dev(ctx->drvdata);
  872. dev_dbg(dev, "===== setkey (%d) ====\n", keylen);
  873. ctx->is_hmac = true;
  874. switch (keylen) {
  875. case AES_KEYSIZE_128:
  876. case AES_KEYSIZE_192:
  877. case AES_KEYSIZE_256:
  878. break;
  879. default:
  880. return -EINVAL;
  881. }
  882. ctx->key_params.keylen = keylen;
  883. /* STAT_PHASE_1: Copy key to ctx */
  884. dma_sync_single_for_cpu(dev, ctx->opad_tmp_keys_dma_addr,
  885. keylen, DMA_TO_DEVICE);
  886. memcpy(ctx->opad_tmp_keys_buff, key, keylen);
  887. if (keylen == 24) {
  888. memset(ctx->opad_tmp_keys_buff + 24, 0,
  889. CC_AES_KEY_SIZE_MAX - 24);
  890. }
  891. dma_sync_single_for_device(dev, ctx->opad_tmp_keys_dma_addr,
  892. keylen, DMA_TO_DEVICE);
  893. ctx->key_params.keylen = keylen;
  894. return 0;
  895. }
  896. static void cc_free_ctx(struct cc_hash_ctx *ctx)
  897. {
  898. struct device *dev = drvdata_to_dev(ctx->drvdata);
  899. if (ctx->digest_buff_dma_addr) {
  900. dma_unmap_single(dev, ctx->digest_buff_dma_addr,
  901. sizeof(ctx->digest_buff), DMA_BIDIRECTIONAL);
  902. dev_dbg(dev, "Unmapped digest-buffer: digest_buff_dma_addr=%pad\n",
  903. &ctx->digest_buff_dma_addr);
  904. ctx->digest_buff_dma_addr = 0;
  905. }
  906. if (ctx->opad_tmp_keys_dma_addr) {
  907. dma_unmap_single(dev, ctx->opad_tmp_keys_dma_addr,
  908. sizeof(ctx->opad_tmp_keys_buff),
  909. DMA_BIDIRECTIONAL);
  910. dev_dbg(dev, "Unmapped opad-digest: opad_tmp_keys_dma_addr=%pad\n",
  911. &ctx->opad_tmp_keys_dma_addr);
  912. ctx->opad_tmp_keys_dma_addr = 0;
  913. }
  914. ctx->key_params.keylen = 0;
  915. }
  916. static int cc_alloc_ctx(struct cc_hash_ctx *ctx)
  917. {
  918. struct device *dev = drvdata_to_dev(ctx->drvdata);
  919. ctx->key_params.keylen = 0;
  920. ctx->digest_buff_dma_addr =
  921. dma_map_single(dev, (void *)ctx->digest_buff,
  922. sizeof(ctx->digest_buff), DMA_BIDIRECTIONAL);
  923. if (dma_mapping_error(dev, ctx->digest_buff_dma_addr)) {
  924. dev_err(dev, "Mapping digest len %zu B at va=%pK for DMA failed\n",
  925. sizeof(ctx->digest_buff), ctx->digest_buff);
  926. goto fail;
  927. }
  928. dev_dbg(dev, "Mapped digest %zu B at va=%pK to dma=%pad\n",
  929. sizeof(ctx->digest_buff), ctx->digest_buff,
  930. &ctx->digest_buff_dma_addr);
  931. ctx->opad_tmp_keys_dma_addr =
  932. dma_map_single(dev, (void *)ctx->opad_tmp_keys_buff,
  933. sizeof(ctx->opad_tmp_keys_buff),
  934. DMA_BIDIRECTIONAL);
  935. if (dma_mapping_error(dev, ctx->opad_tmp_keys_dma_addr)) {
  936. dev_err(dev, "Mapping opad digest %zu B at va=%pK for DMA failed\n",
  937. sizeof(ctx->opad_tmp_keys_buff),
  938. ctx->opad_tmp_keys_buff);
  939. goto fail;
  940. }
  941. dev_dbg(dev, "Mapped opad_tmp_keys %zu B at va=%pK to dma=%pad\n",
  942. sizeof(ctx->opad_tmp_keys_buff), ctx->opad_tmp_keys_buff,
  943. &ctx->opad_tmp_keys_dma_addr);
  944. ctx->is_hmac = false;
  945. return 0;
  946. fail:
  947. cc_free_ctx(ctx);
  948. return -ENOMEM;
  949. }
  950. static int cc_get_hash_len(struct crypto_tfm *tfm)
  951. {
  952. struct cc_hash_ctx *ctx = crypto_tfm_ctx(tfm);
  953. if (ctx->hash_mode == DRV_HASH_SM3)
  954. return CC_SM3_HASH_LEN_SIZE;
  955. else
  956. return cc_get_default_hash_len(ctx->drvdata);
  957. }
  958. static int cc_cra_init(struct crypto_tfm *tfm)
  959. {
  960. struct cc_hash_ctx *ctx = crypto_tfm_ctx(tfm);
  961. struct hash_alg_common *hash_alg_common =
  962. container_of(tfm->__crt_alg, struct hash_alg_common, base);
  963. struct ahash_alg *ahash_alg =
  964. container_of(hash_alg_common, struct ahash_alg, halg);
  965. struct cc_hash_alg *cc_alg =
  966. container_of(ahash_alg, struct cc_hash_alg, ahash_alg);
  967. crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
  968. sizeof(struct ahash_req_ctx));
  969. ctx->hash_mode = cc_alg->hash_mode;
  970. ctx->hw_mode = cc_alg->hw_mode;
  971. ctx->inter_digestsize = cc_alg->inter_digestsize;
  972. ctx->drvdata = cc_alg->drvdata;
  973. ctx->hash_len = cc_get_hash_len(tfm);
  974. return cc_alloc_ctx(ctx);
  975. }
  976. static void cc_cra_exit(struct crypto_tfm *tfm)
  977. {
  978. struct cc_hash_ctx *ctx = crypto_tfm_ctx(tfm);
  979. struct device *dev = drvdata_to_dev(ctx->drvdata);
  980. dev_dbg(dev, "cc_cra_exit");
  981. cc_free_ctx(ctx);
  982. }
  983. static int cc_mac_update(struct ahash_request *req)
  984. {
  985. struct ahash_req_ctx *state = ahash_request_ctx(req);
  986. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  987. struct cc_hash_ctx *ctx = crypto_ahash_ctx(tfm);
  988. struct device *dev = drvdata_to_dev(ctx->drvdata);
  989. unsigned int block_size = crypto_tfm_alg_blocksize(&tfm->base);
  990. struct cc_crypto_req cc_req = {};
  991. struct cc_hw_desc desc[CC_MAX_HASH_SEQ_LEN];
  992. int rc;
  993. u32 idx = 0;
  994. gfp_t flags = cc_gfp_flags(&req->base);
  995. if (req->nbytes == 0) {
  996. /* no real updates required */
  997. return 0;
  998. }
  999. state->xcbc_count++;
  1000. rc = cc_map_hash_request_update(ctx->drvdata, state, req->src,
  1001. req->nbytes, block_size, flags);
  1002. if (rc) {
  1003. if (rc == 1) {
  1004. dev_dbg(dev, " data size not require HW update %x\n",
  1005. req->nbytes);
  1006. /* No hardware updates are required */
  1007. return 0;
  1008. }
  1009. dev_err(dev, "map_ahash_request_update() failed\n");
  1010. return -ENOMEM;
  1011. }
  1012. if (cc_map_req(dev, state, ctx)) {
  1013. dev_err(dev, "map_ahash_source() failed\n");
  1014. return -EINVAL;
  1015. }
  1016. if (ctx->hw_mode == DRV_CIPHER_XCBC_MAC)
  1017. cc_setup_xcbc(req, desc, &idx);
  1018. else
  1019. cc_setup_cmac(req, desc, &idx);
  1020. cc_set_desc(state, ctx, DIN_AES_DOUT, desc, true, &idx);
  1021. /* store the hash digest result in context */
  1022. hw_desc_init(&desc[idx]);
  1023. set_cipher_mode(&desc[idx], ctx->hw_mode);
  1024. set_dout_dlli(&desc[idx], state->digest_buff_dma_addr,
  1025. ctx->inter_digestsize, NS_BIT, 1);
  1026. set_queue_last_ind(ctx->drvdata, &desc[idx]);
  1027. set_flow_mode(&desc[idx], S_AES_to_DOUT);
  1028. set_setup_mode(&desc[idx], SETUP_WRITE_STATE0);
  1029. idx++;
  1030. /* Setup request structure */
  1031. cc_req.user_cb = (void *)cc_update_complete;
  1032. cc_req.user_arg = (void *)req;
  1033. rc = cc_send_request(ctx->drvdata, &cc_req, desc, idx, &req->base);
  1034. if (rc != -EINPROGRESS && rc != -EBUSY) {
  1035. dev_err(dev, "send_request() failed (rc=%d)\n", rc);
  1036. cc_unmap_hash_request(dev, state, req->src, true);
  1037. cc_unmap_req(dev, state, ctx);
  1038. }
  1039. return rc;
  1040. }
  1041. static int cc_mac_final(struct ahash_request *req)
  1042. {
  1043. struct ahash_req_ctx *state = ahash_request_ctx(req);
  1044. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  1045. struct cc_hash_ctx *ctx = crypto_ahash_ctx(tfm);
  1046. struct device *dev = drvdata_to_dev(ctx->drvdata);
  1047. struct cc_crypto_req cc_req = {};
  1048. struct cc_hw_desc desc[CC_MAX_HASH_SEQ_LEN];
  1049. int idx = 0;
  1050. int rc = 0;
  1051. u32 key_size, key_len;
  1052. u32 digestsize = crypto_ahash_digestsize(tfm);
  1053. gfp_t flags = cc_gfp_flags(&req->base);
  1054. u32 rem_cnt = *cc_hash_buf_cnt(state);
  1055. if (ctx->hw_mode == DRV_CIPHER_XCBC_MAC) {
  1056. key_size = CC_AES_128_BIT_KEY_SIZE;
  1057. key_len = CC_AES_128_BIT_KEY_SIZE;
  1058. } else {
  1059. key_size = (ctx->key_params.keylen == 24) ? AES_MAX_KEY_SIZE :
  1060. ctx->key_params.keylen;
  1061. key_len = ctx->key_params.keylen;
  1062. }
  1063. dev_dbg(dev, "===== final xcbc reminder (%d) ====\n", rem_cnt);
  1064. if (cc_map_req(dev, state, ctx)) {
  1065. dev_err(dev, "map_ahash_source() failed\n");
  1066. return -EINVAL;
  1067. }
  1068. if (cc_map_hash_request_final(ctx->drvdata, state, req->src,
  1069. req->nbytes, 0, flags)) {
  1070. dev_err(dev, "map_ahash_request_final() failed\n");
  1071. cc_unmap_req(dev, state, ctx);
  1072. return -ENOMEM;
  1073. }
  1074. if (cc_map_result(dev, state, digestsize)) {
  1075. dev_err(dev, "map_ahash_digest() failed\n");
  1076. cc_unmap_hash_request(dev, state, req->src, true);
  1077. cc_unmap_req(dev, state, ctx);
  1078. return -ENOMEM;
  1079. }
  1080. /* Setup request structure */
  1081. cc_req.user_cb = (void *)cc_hash_complete;
  1082. cc_req.user_arg = (void *)req;
  1083. if (state->xcbc_count && rem_cnt == 0) {
  1084. /* Load key for ECB decryption */
  1085. hw_desc_init(&desc[idx]);
  1086. set_cipher_mode(&desc[idx], DRV_CIPHER_ECB);
  1087. set_cipher_config0(&desc[idx], DRV_CRYPTO_DIRECTION_DECRYPT);
  1088. set_din_type(&desc[idx], DMA_DLLI,
  1089. (ctx->opad_tmp_keys_dma_addr + XCBC_MAC_K1_OFFSET),
  1090. key_size, NS_BIT);
  1091. set_key_size_aes(&desc[idx], key_len);
  1092. set_flow_mode(&desc[idx], S_DIN_to_AES);
  1093. set_setup_mode(&desc[idx], SETUP_LOAD_KEY0);
  1094. idx++;
  1095. /* Initiate decryption of block state to previous
  1096. * block_state-XOR-M[n]
  1097. */
  1098. hw_desc_init(&desc[idx]);
  1099. set_din_type(&desc[idx], DMA_DLLI, state->digest_buff_dma_addr,
  1100. CC_AES_BLOCK_SIZE, NS_BIT);
  1101. set_dout_dlli(&desc[idx], state->digest_buff_dma_addr,
  1102. CC_AES_BLOCK_SIZE, NS_BIT, 0);
  1103. set_flow_mode(&desc[idx], DIN_AES_DOUT);
  1104. idx++;
  1105. /* Memory Barrier: wait for axi write to complete */
  1106. hw_desc_init(&desc[idx]);
  1107. set_din_no_dma(&desc[idx], 0, 0xfffff0);
  1108. set_dout_no_dma(&desc[idx], 0, 0, 1);
  1109. idx++;
  1110. }
  1111. if (ctx->hw_mode == DRV_CIPHER_XCBC_MAC)
  1112. cc_setup_xcbc(req, desc, &idx);
  1113. else
  1114. cc_setup_cmac(req, desc, &idx);
  1115. if (state->xcbc_count == 0) {
  1116. hw_desc_init(&desc[idx]);
  1117. set_cipher_mode(&desc[idx], ctx->hw_mode);
  1118. set_key_size_aes(&desc[idx], key_len);
  1119. set_cmac_size0_mode(&desc[idx]);
  1120. set_flow_mode(&desc[idx], S_DIN_to_AES);
  1121. idx++;
  1122. } else if (rem_cnt > 0) {
  1123. cc_set_desc(state, ctx, DIN_AES_DOUT, desc, false, &idx);
  1124. } else {
  1125. hw_desc_init(&desc[idx]);
  1126. set_din_const(&desc[idx], 0x00, CC_AES_BLOCK_SIZE);
  1127. set_flow_mode(&desc[idx], DIN_AES_DOUT);
  1128. idx++;
  1129. }
  1130. /* Get final MAC result */
  1131. hw_desc_init(&desc[idx]);
  1132. /* TODO */
  1133. set_dout_dlli(&desc[idx], state->digest_result_dma_addr,
  1134. digestsize, NS_BIT, 1);
  1135. set_queue_last_ind(ctx->drvdata, &desc[idx]);
  1136. set_flow_mode(&desc[idx], S_AES_to_DOUT);
  1137. set_setup_mode(&desc[idx], SETUP_WRITE_STATE0);
  1138. set_cipher_mode(&desc[idx], ctx->hw_mode);
  1139. idx++;
  1140. rc = cc_send_request(ctx->drvdata, &cc_req, desc, idx, &req->base);
  1141. if (rc != -EINPROGRESS && rc != -EBUSY) {
  1142. dev_err(dev, "send_request() failed (rc=%d)\n", rc);
  1143. cc_unmap_hash_request(dev, state, req->src, true);
  1144. cc_unmap_result(dev, state, digestsize, req->result);
  1145. cc_unmap_req(dev, state, ctx);
  1146. }
  1147. return rc;
  1148. }
  1149. static int cc_mac_finup(struct ahash_request *req)
  1150. {
  1151. struct ahash_req_ctx *state = ahash_request_ctx(req);
  1152. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  1153. struct cc_hash_ctx *ctx = crypto_ahash_ctx(tfm);
  1154. struct device *dev = drvdata_to_dev(ctx->drvdata);
  1155. struct cc_crypto_req cc_req = {};
  1156. struct cc_hw_desc desc[CC_MAX_HASH_SEQ_LEN];
  1157. int idx = 0;
  1158. int rc = 0;
  1159. u32 key_len = 0;
  1160. u32 digestsize = crypto_ahash_digestsize(tfm);
  1161. gfp_t flags = cc_gfp_flags(&req->base);
  1162. dev_dbg(dev, "===== finup xcbc(%d) ====\n", req->nbytes);
  1163. if (state->xcbc_count > 0 && req->nbytes == 0) {
  1164. dev_dbg(dev, "No data to update. Call to fdx_mac_final\n");
  1165. return cc_mac_final(req);
  1166. }
  1167. if (cc_map_req(dev, state, ctx)) {
  1168. dev_err(dev, "map_ahash_source() failed\n");
  1169. return -EINVAL;
  1170. }
  1171. if (cc_map_hash_request_final(ctx->drvdata, state, req->src,
  1172. req->nbytes, 1, flags)) {
  1173. dev_err(dev, "map_ahash_request_final() failed\n");
  1174. cc_unmap_req(dev, state, ctx);
  1175. return -ENOMEM;
  1176. }
  1177. if (cc_map_result(dev, state, digestsize)) {
  1178. dev_err(dev, "map_ahash_digest() failed\n");
  1179. cc_unmap_hash_request(dev, state, req->src, true);
  1180. cc_unmap_req(dev, state, ctx);
  1181. return -ENOMEM;
  1182. }
  1183. /* Setup request structure */
  1184. cc_req.user_cb = (void *)cc_hash_complete;
  1185. cc_req.user_arg = (void *)req;
  1186. if (ctx->hw_mode == DRV_CIPHER_XCBC_MAC) {
  1187. key_len = CC_AES_128_BIT_KEY_SIZE;
  1188. cc_setup_xcbc(req, desc, &idx);
  1189. } else {
  1190. key_len = ctx->key_params.keylen;
  1191. cc_setup_cmac(req, desc, &idx);
  1192. }
  1193. if (req->nbytes == 0) {
  1194. hw_desc_init(&desc[idx]);
  1195. set_cipher_mode(&desc[idx], ctx->hw_mode);
  1196. set_key_size_aes(&desc[idx], key_len);
  1197. set_cmac_size0_mode(&desc[idx]);
  1198. set_flow_mode(&desc[idx], S_DIN_to_AES);
  1199. idx++;
  1200. } else {
  1201. cc_set_desc(state, ctx, DIN_AES_DOUT, desc, false, &idx);
  1202. }
  1203. /* Get final MAC result */
  1204. hw_desc_init(&desc[idx]);
  1205. /* TODO */
  1206. set_dout_dlli(&desc[idx], state->digest_result_dma_addr,
  1207. digestsize, NS_BIT, 1);
  1208. set_queue_last_ind(ctx->drvdata, &desc[idx]);
  1209. set_flow_mode(&desc[idx], S_AES_to_DOUT);
  1210. set_setup_mode(&desc[idx], SETUP_WRITE_STATE0);
  1211. set_cipher_mode(&desc[idx], ctx->hw_mode);
  1212. idx++;
  1213. rc = cc_send_request(ctx->drvdata, &cc_req, desc, idx, &req->base);
  1214. if (rc != -EINPROGRESS && rc != -EBUSY) {
  1215. dev_err(dev, "send_request() failed (rc=%d)\n", rc);
  1216. cc_unmap_hash_request(dev, state, req->src, true);
  1217. cc_unmap_result(dev, state, digestsize, req->result);
  1218. cc_unmap_req(dev, state, ctx);
  1219. }
  1220. return rc;
  1221. }
  1222. static int cc_mac_digest(struct ahash_request *req)
  1223. {
  1224. struct ahash_req_ctx *state = ahash_request_ctx(req);
  1225. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  1226. struct cc_hash_ctx *ctx = crypto_ahash_ctx(tfm);
  1227. struct device *dev = drvdata_to_dev(ctx->drvdata);
  1228. u32 digestsize = crypto_ahash_digestsize(tfm);
  1229. struct cc_crypto_req cc_req = {};
  1230. struct cc_hw_desc desc[CC_MAX_HASH_SEQ_LEN];
  1231. u32 key_len;
  1232. unsigned int idx = 0;
  1233. int rc;
  1234. gfp_t flags = cc_gfp_flags(&req->base);
  1235. dev_dbg(dev, "===== -digest mac (%d) ====\n", req->nbytes);
  1236. cc_init_req(dev, state, ctx);
  1237. if (cc_map_req(dev, state, ctx)) {
  1238. dev_err(dev, "map_ahash_source() failed\n");
  1239. return -ENOMEM;
  1240. }
  1241. if (cc_map_result(dev, state, digestsize)) {
  1242. dev_err(dev, "map_ahash_digest() failed\n");
  1243. cc_unmap_req(dev, state, ctx);
  1244. return -ENOMEM;
  1245. }
  1246. if (cc_map_hash_request_final(ctx->drvdata, state, req->src,
  1247. req->nbytes, 1, flags)) {
  1248. dev_err(dev, "map_ahash_request_final() failed\n");
  1249. cc_unmap_req(dev, state, ctx);
  1250. return -ENOMEM;
  1251. }
  1252. /* Setup request structure */
  1253. cc_req.user_cb = (void *)cc_digest_complete;
  1254. cc_req.user_arg = (void *)req;
  1255. if (ctx->hw_mode == DRV_CIPHER_XCBC_MAC) {
  1256. key_len = CC_AES_128_BIT_KEY_SIZE;
  1257. cc_setup_xcbc(req, desc, &idx);
  1258. } else {
  1259. key_len = ctx->key_params.keylen;
  1260. cc_setup_cmac(req, desc, &idx);
  1261. }
  1262. if (req->nbytes == 0) {
  1263. hw_desc_init(&desc[idx]);
  1264. set_cipher_mode(&desc[idx], ctx->hw_mode);
  1265. set_key_size_aes(&desc[idx], key_len);
  1266. set_cmac_size0_mode(&desc[idx]);
  1267. set_flow_mode(&desc[idx], S_DIN_to_AES);
  1268. idx++;
  1269. } else {
  1270. cc_set_desc(state, ctx, DIN_AES_DOUT, desc, false, &idx);
  1271. }
  1272. /* Get final MAC result */
  1273. hw_desc_init(&desc[idx]);
  1274. set_dout_dlli(&desc[idx], state->digest_result_dma_addr,
  1275. CC_AES_BLOCK_SIZE, NS_BIT, 1);
  1276. set_queue_last_ind(ctx->drvdata, &desc[idx]);
  1277. set_flow_mode(&desc[idx], S_AES_to_DOUT);
  1278. set_setup_mode(&desc[idx], SETUP_WRITE_STATE0);
  1279. set_cipher_config0(&desc[idx], DESC_DIRECTION_ENCRYPT_ENCRYPT);
  1280. set_cipher_mode(&desc[idx], ctx->hw_mode);
  1281. idx++;
  1282. rc = cc_send_request(ctx->drvdata, &cc_req, desc, idx, &req->base);
  1283. if (rc != -EINPROGRESS && rc != -EBUSY) {
  1284. dev_err(dev, "send_request() failed (rc=%d)\n", rc);
  1285. cc_unmap_hash_request(dev, state, req->src, true);
  1286. cc_unmap_result(dev, state, digestsize, req->result);
  1287. cc_unmap_req(dev, state, ctx);
  1288. }
  1289. return rc;
  1290. }
  1291. static int cc_hash_export(struct ahash_request *req, void *out)
  1292. {
  1293. struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
  1294. struct cc_hash_ctx *ctx = crypto_ahash_ctx(ahash);
  1295. struct ahash_req_ctx *state = ahash_request_ctx(req);
  1296. u8 *curr_buff = cc_hash_buf(state);
  1297. u32 curr_buff_cnt = *cc_hash_buf_cnt(state);
  1298. const u32 tmp = CC_EXPORT_MAGIC;
  1299. memcpy(out, &tmp, sizeof(u32));
  1300. out += sizeof(u32);
  1301. memcpy(out, state->digest_buff, ctx->inter_digestsize);
  1302. out += ctx->inter_digestsize;
  1303. memcpy(out, state->digest_bytes_len, ctx->hash_len);
  1304. out += ctx->hash_len;
  1305. memcpy(out, &curr_buff_cnt, sizeof(u32));
  1306. out += sizeof(u32);
  1307. memcpy(out, curr_buff, curr_buff_cnt);
  1308. return 0;
  1309. }
  1310. static int cc_hash_import(struct ahash_request *req, const void *in)
  1311. {
  1312. struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
  1313. struct cc_hash_ctx *ctx = crypto_ahash_ctx(ahash);
  1314. struct device *dev = drvdata_to_dev(ctx->drvdata);
  1315. struct ahash_req_ctx *state = ahash_request_ctx(req);
  1316. u32 tmp;
  1317. memcpy(&tmp, in, sizeof(u32));
  1318. if (tmp != CC_EXPORT_MAGIC)
  1319. return -EINVAL;
  1320. in += sizeof(u32);
  1321. cc_init_req(dev, state, ctx);
  1322. memcpy(state->digest_buff, in, ctx->inter_digestsize);
  1323. in += ctx->inter_digestsize;
  1324. memcpy(state->digest_bytes_len, in, ctx->hash_len);
  1325. in += ctx->hash_len;
  1326. /* Sanity check the data as much as possible */
  1327. memcpy(&tmp, in, sizeof(u32));
  1328. if (tmp > CC_MAX_HASH_BLCK_SIZE)
  1329. return -EINVAL;
  1330. in += sizeof(u32);
  1331. state->buf_cnt[0] = tmp;
  1332. memcpy(state->buffers[0], in, tmp);
  1333. return 0;
  1334. }
  1335. struct cc_hash_template {
  1336. char name[CRYPTO_MAX_ALG_NAME];
  1337. char driver_name[CRYPTO_MAX_ALG_NAME];
  1338. char mac_name[CRYPTO_MAX_ALG_NAME];
  1339. char mac_driver_name[CRYPTO_MAX_ALG_NAME];
  1340. unsigned int blocksize;
  1341. bool is_mac;
  1342. bool synchronize;
  1343. struct ahash_alg template_ahash;
  1344. int hash_mode;
  1345. int hw_mode;
  1346. int inter_digestsize;
  1347. struct cc_drvdata *drvdata;
  1348. u32 min_hw_rev;
  1349. enum cc_std_body std_body;
  1350. };
  1351. #define CC_STATE_SIZE(_x) \
  1352. ((_x) + HASH_MAX_LEN_SIZE + CC_MAX_HASH_BLCK_SIZE + (2 * sizeof(u32)))
  1353. /* hash descriptors */
  1354. static struct cc_hash_template driver_hash[] = {
  1355. //Asynchronize hash template
  1356. {
  1357. .name = "sha1",
  1358. .driver_name = "sha1-ccree",
  1359. .mac_name = "hmac(sha1)",
  1360. .mac_driver_name = "hmac-sha1-ccree",
  1361. .blocksize = SHA1_BLOCK_SIZE,
  1362. .is_mac = true,
  1363. .synchronize = false,
  1364. .template_ahash = {
  1365. .init = cc_hash_init,
  1366. .update = cc_hash_update,
  1367. .final = cc_hash_final,
  1368. .finup = cc_hash_finup,
  1369. .digest = cc_hash_digest,
  1370. .export = cc_hash_export,
  1371. .import = cc_hash_import,
  1372. .setkey = cc_hash_setkey,
  1373. .halg = {
  1374. .digestsize = SHA1_DIGEST_SIZE,
  1375. .statesize = CC_STATE_SIZE(SHA1_DIGEST_SIZE),
  1376. },
  1377. },
  1378. .hash_mode = DRV_HASH_SHA1,
  1379. .hw_mode = DRV_HASH_HW_SHA1,
  1380. .inter_digestsize = SHA1_DIGEST_SIZE,
  1381. .min_hw_rev = CC_HW_REV_630,
  1382. .std_body = CC_STD_NIST,
  1383. },
  1384. {
  1385. .name = "sha256",
  1386. .driver_name = "sha256-ccree",
  1387. .mac_name = "hmac(sha256)",
  1388. .mac_driver_name = "hmac-sha256-ccree",
  1389. .blocksize = SHA256_BLOCK_SIZE,
  1390. .is_mac = true,
  1391. .template_ahash = {
  1392. .init = cc_hash_init,
  1393. .update = cc_hash_update,
  1394. .final = cc_hash_final,
  1395. .finup = cc_hash_finup,
  1396. .digest = cc_hash_digest,
  1397. .export = cc_hash_export,
  1398. .import = cc_hash_import,
  1399. .setkey = cc_hash_setkey,
  1400. .halg = {
  1401. .digestsize = SHA256_DIGEST_SIZE,
  1402. .statesize = CC_STATE_SIZE(SHA256_DIGEST_SIZE)
  1403. },
  1404. },
  1405. .hash_mode = DRV_HASH_SHA256,
  1406. .hw_mode = DRV_HASH_HW_SHA256,
  1407. .inter_digestsize = SHA256_DIGEST_SIZE,
  1408. .min_hw_rev = CC_HW_REV_630,
  1409. .std_body = CC_STD_NIST,
  1410. },
  1411. {
  1412. .name = "sha224",
  1413. .driver_name = "sha224-ccree",
  1414. .mac_name = "hmac(sha224)",
  1415. .mac_driver_name = "hmac-sha224-ccree",
  1416. .blocksize = SHA224_BLOCK_SIZE,
  1417. .is_mac = true,
  1418. .template_ahash = {
  1419. .init = cc_hash_init,
  1420. .update = cc_hash_update,
  1421. .final = cc_hash_final,
  1422. .finup = cc_hash_finup,
  1423. .digest = cc_hash_digest,
  1424. .export = cc_hash_export,
  1425. .import = cc_hash_import,
  1426. .setkey = cc_hash_setkey,
  1427. .halg = {
  1428. .digestsize = SHA224_DIGEST_SIZE,
  1429. .statesize = CC_STATE_SIZE(SHA256_DIGEST_SIZE),
  1430. },
  1431. },
  1432. .hash_mode = DRV_HASH_SHA224,
  1433. .hw_mode = DRV_HASH_HW_SHA256,
  1434. .inter_digestsize = SHA256_DIGEST_SIZE,
  1435. .min_hw_rev = CC_HW_REV_630,
  1436. .std_body = CC_STD_NIST,
  1437. },
  1438. {
  1439. .name = "sha384",
  1440. .driver_name = "sha384-ccree",
  1441. .mac_name = "hmac(sha384)",
  1442. .mac_driver_name = "hmac-sha384-ccree",
  1443. .blocksize = SHA384_BLOCK_SIZE,
  1444. .is_mac = true,
  1445. .template_ahash = {
  1446. .init = cc_hash_init,
  1447. .update = cc_hash_update,
  1448. .final = cc_hash_final,
  1449. .finup = cc_hash_finup,
  1450. .digest = cc_hash_digest,
  1451. .export = cc_hash_export,
  1452. .import = cc_hash_import,
  1453. .setkey = cc_hash_setkey,
  1454. .halg = {
  1455. .digestsize = SHA384_DIGEST_SIZE,
  1456. .statesize = CC_STATE_SIZE(SHA512_DIGEST_SIZE),
  1457. },
  1458. },
  1459. .hash_mode = DRV_HASH_SHA384,
  1460. .hw_mode = DRV_HASH_HW_SHA512,
  1461. .inter_digestsize = SHA512_DIGEST_SIZE,
  1462. .min_hw_rev = CC_HW_REV_712,
  1463. .std_body = CC_STD_NIST,
  1464. },
  1465. {
  1466. .name = "sha512",
  1467. .driver_name = "sha512-ccree",
  1468. .mac_name = "hmac(sha512)",
  1469. .mac_driver_name = "hmac-sha512-ccree",
  1470. .blocksize = SHA512_BLOCK_SIZE,
  1471. .is_mac = true,
  1472. .template_ahash = {
  1473. .init = cc_hash_init,
  1474. .update = cc_hash_update,
  1475. .final = cc_hash_final,
  1476. .finup = cc_hash_finup,
  1477. .digest = cc_hash_digest,
  1478. .export = cc_hash_export,
  1479. .import = cc_hash_import,
  1480. .setkey = cc_hash_setkey,
  1481. .halg = {
  1482. .digestsize = SHA512_DIGEST_SIZE,
  1483. .statesize = CC_STATE_SIZE(SHA512_DIGEST_SIZE),
  1484. },
  1485. },
  1486. .hash_mode = DRV_HASH_SHA512,
  1487. .hw_mode = DRV_HASH_HW_SHA512,
  1488. .inter_digestsize = SHA512_DIGEST_SIZE,
  1489. .min_hw_rev = CC_HW_REV_712,
  1490. .std_body = CC_STD_NIST,
  1491. },
  1492. {
  1493. .name = "md5",
  1494. .driver_name = "md5-ccree",
  1495. .mac_name = "hmac(md5)",
  1496. .mac_driver_name = "hmac-md5-ccree",
  1497. .blocksize = MD5_HMAC_BLOCK_SIZE,
  1498. .is_mac = true,
  1499. .template_ahash = {
  1500. .init = cc_hash_init,
  1501. .update = cc_hash_update,
  1502. .final = cc_hash_final,
  1503. .finup = cc_hash_finup,
  1504. .digest = cc_hash_digest,
  1505. .export = cc_hash_export,
  1506. .import = cc_hash_import,
  1507. .setkey = cc_hash_setkey,
  1508. .halg = {
  1509. .digestsize = MD5_DIGEST_SIZE,
  1510. .statesize = CC_STATE_SIZE(MD5_DIGEST_SIZE),
  1511. },
  1512. },
  1513. .hash_mode = DRV_HASH_MD5,
  1514. .hw_mode = DRV_HASH_HW_MD5,
  1515. .inter_digestsize = MD5_DIGEST_SIZE,
  1516. .min_hw_rev = CC_HW_REV_630,
  1517. .std_body = CC_STD_NIST,
  1518. },
  1519. {
  1520. .name = "sm3",
  1521. .driver_name = "sm3-ccree",
  1522. .blocksize = SM3_BLOCK_SIZE,
  1523. .is_mac = false,
  1524. .template_ahash = {
  1525. .init = cc_hash_init,
  1526. .update = cc_hash_update,
  1527. .final = cc_hash_final,
  1528. .finup = cc_hash_finup,
  1529. .digest = cc_hash_digest,
  1530. .export = cc_hash_export,
  1531. .import = cc_hash_import,
  1532. .setkey = cc_hash_setkey,
  1533. .halg = {
  1534. .digestsize = SM3_DIGEST_SIZE,
  1535. .statesize = CC_STATE_SIZE(SM3_DIGEST_SIZE),
  1536. },
  1537. },
  1538. .hash_mode = DRV_HASH_SM3,
  1539. .hw_mode = DRV_HASH_HW_SM3,
  1540. .inter_digestsize = SM3_DIGEST_SIZE,
  1541. .min_hw_rev = CC_HW_REV_713,
  1542. .std_body = CC_STD_OSCCA,
  1543. },
  1544. {
  1545. .mac_name = "xcbc(aes)",
  1546. .mac_driver_name = "xcbc-aes-ccree",
  1547. .blocksize = AES_BLOCK_SIZE,
  1548. .is_mac = true,
  1549. .template_ahash = {
  1550. .init = cc_hash_init,
  1551. .update = cc_mac_update,
  1552. .final = cc_mac_final,
  1553. .finup = cc_mac_finup,
  1554. .digest = cc_mac_digest,
  1555. .setkey = cc_xcbc_setkey,
  1556. .export = cc_hash_export,
  1557. .import = cc_hash_import,
  1558. .halg = {
  1559. .digestsize = AES_BLOCK_SIZE,
  1560. .statesize = CC_STATE_SIZE(AES_BLOCK_SIZE),
  1561. },
  1562. },
  1563. .hash_mode = DRV_HASH_NULL,
  1564. .hw_mode = DRV_CIPHER_XCBC_MAC,
  1565. .inter_digestsize = AES_BLOCK_SIZE,
  1566. .min_hw_rev = CC_HW_REV_630,
  1567. .std_body = CC_STD_NIST,
  1568. },
  1569. {
  1570. .mac_name = "cmac(aes)",
  1571. .mac_driver_name = "cmac-aes-ccree",
  1572. .blocksize = AES_BLOCK_SIZE,
  1573. .is_mac = true,
  1574. .template_ahash = {
  1575. .init = cc_hash_init,
  1576. .update = cc_mac_update,
  1577. .final = cc_mac_final,
  1578. .finup = cc_mac_finup,
  1579. .digest = cc_mac_digest,
  1580. .setkey = cc_cmac_setkey,
  1581. .export = cc_hash_export,
  1582. .import = cc_hash_import,
  1583. .halg = {
  1584. .digestsize = AES_BLOCK_SIZE,
  1585. .statesize = CC_STATE_SIZE(AES_BLOCK_SIZE),
  1586. },
  1587. },
  1588. .hash_mode = DRV_HASH_NULL,
  1589. .hw_mode = DRV_CIPHER_CMAC,
  1590. .inter_digestsize = AES_BLOCK_SIZE,
  1591. .min_hw_rev = CC_HW_REV_630,
  1592. .std_body = CC_STD_NIST,
  1593. },
  1594. };
  1595. static struct cc_hash_alg *cc_alloc_hash_alg(struct cc_hash_template *template,
  1596. struct device *dev, bool keyed)
  1597. {
  1598. struct cc_hash_alg *t_crypto_alg;
  1599. struct crypto_alg *alg;
  1600. struct ahash_alg *halg;
  1601. t_crypto_alg = kzalloc(sizeof(*t_crypto_alg), GFP_KERNEL);
  1602. if (!t_crypto_alg)
  1603. return ERR_PTR(-ENOMEM);
  1604. t_crypto_alg->ahash_alg = template->template_ahash;
  1605. halg = &t_crypto_alg->ahash_alg;
  1606. alg = &halg->halg.base;
  1607. if (keyed) {
  1608. snprintf(alg->cra_name, CRYPTO_MAX_ALG_NAME, "%s",
  1609. template->mac_name);
  1610. snprintf(alg->cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
  1611. template->mac_driver_name);
  1612. } else {
  1613. halg->setkey = NULL;
  1614. snprintf(alg->cra_name, CRYPTO_MAX_ALG_NAME, "%s",
  1615. template->name);
  1616. snprintf(alg->cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
  1617. template->driver_name);
  1618. }
  1619. alg->cra_module = THIS_MODULE;
  1620. alg->cra_ctxsize = sizeof(struct cc_hash_ctx);
  1621. alg->cra_priority = CC_CRA_PRIO;
  1622. alg->cra_blocksize = template->blocksize;
  1623. alg->cra_alignmask = 0;
  1624. alg->cra_exit = cc_cra_exit;
  1625. alg->cra_init = cc_cra_init;
  1626. alg->cra_flags = CRYPTO_ALG_ASYNC | CRYPTO_ALG_KERN_DRIVER_ONLY;
  1627. t_crypto_alg->hash_mode = template->hash_mode;
  1628. t_crypto_alg->hw_mode = template->hw_mode;
  1629. t_crypto_alg->inter_digestsize = template->inter_digestsize;
  1630. return t_crypto_alg;
  1631. }
  1632. int cc_init_hash_sram(struct cc_drvdata *drvdata)
  1633. {
  1634. struct cc_hash_handle *hash_handle = drvdata->hash_handle;
  1635. cc_sram_addr_t sram_buff_ofs = hash_handle->digest_len_sram_addr;
  1636. unsigned int larval_seq_len = 0;
  1637. struct cc_hw_desc larval_seq[CC_DIGEST_SIZE_MAX / sizeof(u32)];
  1638. bool large_sha_supported = (drvdata->hw_rev >= CC_HW_REV_712);
  1639. bool sm3_supported = (drvdata->hw_rev >= CC_HW_REV_713);
  1640. int rc = 0;
  1641. /* Copy-to-sram digest-len */
  1642. cc_set_sram_desc(cc_digest_len_init, sram_buff_ofs,
  1643. ARRAY_SIZE(cc_digest_len_init), larval_seq,
  1644. &larval_seq_len);
  1645. rc = send_request_init(drvdata, larval_seq, larval_seq_len);
  1646. if (rc)
  1647. goto init_digest_const_err;
  1648. sram_buff_ofs += sizeof(cc_digest_len_init);
  1649. larval_seq_len = 0;
  1650. if (large_sha_supported) {
  1651. /* Copy-to-sram digest-len for sha384/512 */
  1652. cc_set_sram_desc(cc_digest_len_sha512_init, sram_buff_ofs,
  1653. ARRAY_SIZE(cc_digest_len_sha512_init),
  1654. larval_seq, &larval_seq_len);
  1655. rc = send_request_init(drvdata, larval_seq, larval_seq_len);
  1656. if (rc)
  1657. goto init_digest_const_err;
  1658. sram_buff_ofs += sizeof(cc_digest_len_sha512_init);
  1659. larval_seq_len = 0;
  1660. }
  1661. /* The initial digests offset */
  1662. hash_handle->larval_digest_sram_addr = sram_buff_ofs;
  1663. /* Copy-to-sram initial SHA* digests */
  1664. cc_set_sram_desc(cc_md5_init, sram_buff_ofs, ARRAY_SIZE(cc_md5_init),
  1665. larval_seq, &larval_seq_len);
  1666. rc = send_request_init(drvdata, larval_seq, larval_seq_len);
  1667. if (rc)
  1668. goto init_digest_const_err;
  1669. sram_buff_ofs += sizeof(cc_md5_init);
  1670. larval_seq_len = 0;
  1671. cc_set_sram_desc(cc_sha1_init, sram_buff_ofs,
  1672. ARRAY_SIZE(cc_sha1_init), larval_seq,
  1673. &larval_seq_len);
  1674. rc = send_request_init(drvdata, larval_seq, larval_seq_len);
  1675. if (rc)
  1676. goto init_digest_const_err;
  1677. sram_buff_ofs += sizeof(cc_sha1_init);
  1678. larval_seq_len = 0;
  1679. cc_set_sram_desc(cc_sha224_init, sram_buff_ofs,
  1680. ARRAY_SIZE(cc_sha224_init), larval_seq,
  1681. &larval_seq_len);
  1682. rc = send_request_init(drvdata, larval_seq, larval_seq_len);
  1683. if (rc)
  1684. goto init_digest_const_err;
  1685. sram_buff_ofs += sizeof(cc_sha224_init);
  1686. larval_seq_len = 0;
  1687. cc_set_sram_desc(cc_sha256_init, sram_buff_ofs,
  1688. ARRAY_SIZE(cc_sha256_init), larval_seq,
  1689. &larval_seq_len);
  1690. rc = send_request_init(drvdata, larval_seq, larval_seq_len);
  1691. if (rc)
  1692. goto init_digest_const_err;
  1693. sram_buff_ofs += sizeof(cc_sha256_init);
  1694. larval_seq_len = 0;
  1695. if (sm3_supported) {
  1696. cc_set_sram_desc(cc_sm3_init, sram_buff_ofs,
  1697. ARRAY_SIZE(cc_sm3_init), larval_seq,
  1698. &larval_seq_len);
  1699. rc = send_request_init(drvdata, larval_seq, larval_seq_len);
  1700. if (rc)
  1701. goto init_digest_const_err;
  1702. sram_buff_ofs += sizeof(cc_sm3_init);
  1703. larval_seq_len = 0;
  1704. }
  1705. if (large_sha_supported) {
  1706. cc_set_sram_desc((u32 *)cc_sha384_init, sram_buff_ofs,
  1707. (ARRAY_SIZE(cc_sha384_init) * 2), larval_seq,
  1708. &larval_seq_len);
  1709. rc = send_request_init(drvdata, larval_seq, larval_seq_len);
  1710. if (rc)
  1711. goto init_digest_const_err;
  1712. sram_buff_ofs += sizeof(cc_sha384_init);
  1713. larval_seq_len = 0;
  1714. cc_set_sram_desc((u32 *)cc_sha512_init, sram_buff_ofs,
  1715. (ARRAY_SIZE(cc_sha512_init) * 2), larval_seq,
  1716. &larval_seq_len);
  1717. rc = send_request_init(drvdata, larval_seq, larval_seq_len);
  1718. if (rc)
  1719. goto init_digest_const_err;
  1720. }
  1721. init_digest_const_err:
  1722. return rc;
  1723. }
  1724. static void __init cc_swap_dwords(u32 *buf, unsigned long size)
  1725. {
  1726. int i;
  1727. u32 tmp;
  1728. for (i = 0; i < size; i += 2) {
  1729. tmp = buf[i];
  1730. buf[i] = buf[i + 1];
  1731. buf[i + 1] = tmp;
  1732. }
  1733. }
  1734. /*
  1735. * Due to the way the HW works we need to swap every
  1736. * double word in the SHA384 and SHA512 larval hashes
  1737. */
  1738. void __init cc_hash_global_init(void)
  1739. {
  1740. cc_swap_dwords((u32 *)&cc_sha384_init, (ARRAY_SIZE(cc_sha384_init) * 2));
  1741. cc_swap_dwords((u32 *)&cc_sha512_init, (ARRAY_SIZE(cc_sha512_init) * 2));
  1742. }
  1743. int cc_hash_alloc(struct cc_drvdata *drvdata)
  1744. {
  1745. struct cc_hash_handle *hash_handle;
  1746. cc_sram_addr_t sram_buff;
  1747. u32 sram_size_to_alloc;
  1748. struct device *dev = drvdata_to_dev(drvdata);
  1749. int rc = 0;
  1750. int alg;
  1751. hash_handle = kzalloc(sizeof(*hash_handle), GFP_KERNEL);
  1752. if (!hash_handle)
  1753. return -ENOMEM;
  1754. INIT_LIST_HEAD(&hash_handle->hash_list);
  1755. drvdata->hash_handle = hash_handle;
  1756. sram_size_to_alloc = sizeof(cc_digest_len_init) +
  1757. sizeof(cc_md5_init) +
  1758. sizeof(cc_sha1_init) +
  1759. sizeof(cc_sha224_init) +
  1760. sizeof(cc_sha256_init);
  1761. if (drvdata->hw_rev >= CC_HW_REV_713)
  1762. sram_size_to_alloc += sizeof(cc_sm3_init);
  1763. if (drvdata->hw_rev >= CC_HW_REV_712)
  1764. sram_size_to_alloc += sizeof(cc_digest_len_sha512_init) +
  1765. sizeof(cc_sha384_init) + sizeof(cc_sha512_init);
  1766. sram_buff = cc_sram_alloc(drvdata, sram_size_to_alloc);
  1767. if (sram_buff == NULL_SRAM_ADDR) {
  1768. dev_err(dev, "SRAM pool exhausted\n");
  1769. rc = -ENOMEM;
  1770. goto fail;
  1771. }
  1772. /* The initial digest-len offset */
  1773. hash_handle->digest_len_sram_addr = sram_buff;
  1774. /*must be set before the alg registration as it is being used there*/
  1775. rc = cc_init_hash_sram(drvdata);
  1776. if (rc) {
  1777. dev_err(dev, "Init digest CONST failed (rc=%d)\n", rc);
  1778. goto fail;
  1779. }
  1780. /* ahash registration */
  1781. for (alg = 0; alg < ARRAY_SIZE(driver_hash); alg++) {
  1782. struct cc_hash_alg *t_alg;
  1783. int hw_mode = driver_hash[alg].hw_mode;
  1784. /* Check that the HW revision and variants are suitable */
  1785. if ((driver_hash[alg].min_hw_rev > drvdata->hw_rev) ||
  1786. !(drvdata->std_bodies & driver_hash[alg].std_body))
  1787. continue;
  1788. if (driver_hash[alg].is_mac) {
  1789. /* register hmac version */
  1790. t_alg = cc_alloc_hash_alg(&driver_hash[alg], dev, true);
  1791. if (IS_ERR(t_alg)) {
  1792. rc = PTR_ERR(t_alg);
  1793. dev_err(dev, "%s alg allocation failed\n",
  1794. driver_hash[alg].driver_name);
  1795. goto fail;
  1796. }
  1797. t_alg->drvdata = drvdata;
  1798. rc = crypto_register_ahash(&t_alg->ahash_alg);
  1799. if (rc) {
  1800. dev_err(dev, "%s alg registration failed\n",
  1801. driver_hash[alg].driver_name);
  1802. kfree(t_alg);
  1803. goto fail;
  1804. } else {
  1805. list_add_tail(&t_alg->entry,
  1806. &hash_handle->hash_list);
  1807. }
  1808. }
  1809. if (hw_mode == DRV_CIPHER_XCBC_MAC ||
  1810. hw_mode == DRV_CIPHER_CMAC)
  1811. continue;
  1812. /* register hash version */
  1813. t_alg = cc_alloc_hash_alg(&driver_hash[alg], dev, false);
  1814. if (IS_ERR(t_alg)) {
  1815. rc = PTR_ERR(t_alg);
  1816. dev_err(dev, "%s alg allocation failed\n",
  1817. driver_hash[alg].driver_name);
  1818. goto fail;
  1819. }
  1820. t_alg->drvdata = drvdata;
  1821. rc = crypto_register_ahash(&t_alg->ahash_alg);
  1822. if (rc) {
  1823. dev_err(dev, "%s alg registration failed\n",
  1824. driver_hash[alg].driver_name);
  1825. kfree(t_alg);
  1826. goto fail;
  1827. } else {
  1828. list_add_tail(&t_alg->entry, &hash_handle->hash_list);
  1829. }
  1830. }
  1831. return 0;
  1832. fail:
  1833. kfree(drvdata->hash_handle);
  1834. drvdata->hash_handle = NULL;
  1835. return rc;
  1836. }
  1837. int cc_hash_free(struct cc_drvdata *drvdata)
  1838. {
  1839. struct cc_hash_alg *t_hash_alg, *hash_n;
  1840. struct cc_hash_handle *hash_handle = drvdata->hash_handle;
  1841. if (hash_handle) {
  1842. list_for_each_entry_safe(t_hash_alg, hash_n,
  1843. &hash_handle->hash_list, entry) {
  1844. crypto_unregister_ahash(&t_hash_alg->ahash_alg);
  1845. list_del(&t_hash_alg->entry);
  1846. kfree(t_hash_alg);
  1847. }
  1848. kfree(hash_handle);
  1849. drvdata->hash_handle = NULL;
  1850. }
  1851. return 0;
  1852. }
  1853. static void cc_setup_xcbc(struct ahash_request *areq, struct cc_hw_desc desc[],
  1854. unsigned int *seq_size)
  1855. {
  1856. unsigned int idx = *seq_size;
  1857. struct ahash_req_ctx *state = ahash_request_ctx(areq);
  1858. struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
  1859. struct cc_hash_ctx *ctx = crypto_ahash_ctx(tfm);
  1860. /* Setup XCBC MAC K1 */
  1861. hw_desc_init(&desc[idx]);
  1862. set_din_type(&desc[idx], DMA_DLLI, (ctx->opad_tmp_keys_dma_addr +
  1863. XCBC_MAC_K1_OFFSET),
  1864. CC_AES_128_BIT_KEY_SIZE, NS_BIT);
  1865. set_setup_mode(&desc[idx], SETUP_LOAD_KEY0);
  1866. set_hash_cipher_mode(&desc[idx], DRV_CIPHER_XCBC_MAC, ctx->hash_mode);
  1867. set_cipher_config0(&desc[idx], DESC_DIRECTION_ENCRYPT_ENCRYPT);
  1868. set_key_size_aes(&desc[idx], CC_AES_128_BIT_KEY_SIZE);
  1869. set_flow_mode(&desc[idx], S_DIN_to_AES);
  1870. idx++;
  1871. /* Setup XCBC MAC K2 */
  1872. hw_desc_init(&desc[idx]);
  1873. set_din_type(&desc[idx], DMA_DLLI,
  1874. (ctx->opad_tmp_keys_dma_addr + XCBC_MAC_K2_OFFSET),
  1875. CC_AES_128_BIT_KEY_SIZE, NS_BIT);
  1876. set_setup_mode(&desc[idx], SETUP_LOAD_STATE1);
  1877. set_cipher_mode(&desc[idx], DRV_CIPHER_XCBC_MAC);
  1878. set_cipher_config0(&desc[idx], DESC_DIRECTION_ENCRYPT_ENCRYPT);
  1879. set_key_size_aes(&desc[idx], CC_AES_128_BIT_KEY_SIZE);
  1880. set_flow_mode(&desc[idx], S_DIN_to_AES);
  1881. idx++;
  1882. /* Setup XCBC MAC K3 */
  1883. hw_desc_init(&desc[idx]);
  1884. set_din_type(&desc[idx], DMA_DLLI,
  1885. (ctx->opad_tmp_keys_dma_addr + XCBC_MAC_K3_OFFSET),
  1886. CC_AES_128_BIT_KEY_SIZE, NS_BIT);
  1887. set_setup_mode(&desc[idx], SETUP_LOAD_STATE2);
  1888. set_cipher_mode(&desc[idx], DRV_CIPHER_XCBC_MAC);
  1889. set_cipher_config0(&desc[idx], DESC_DIRECTION_ENCRYPT_ENCRYPT);
  1890. set_key_size_aes(&desc[idx], CC_AES_128_BIT_KEY_SIZE);
  1891. set_flow_mode(&desc[idx], S_DIN_to_AES);
  1892. idx++;
  1893. /* Loading MAC state */
  1894. hw_desc_init(&desc[idx]);
  1895. set_din_type(&desc[idx], DMA_DLLI, state->digest_buff_dma_addr,
  1896. CC_AES_BLOCK_SIZE, NS_BIT);
  1897. set_setup_mode(&desc[idx], SETUP_LOAD_STATE0);
  1898. set_cipher_mode(&desc[idx], DRV_CIPHER_XCBC_MAC);
  1899. set_cipher_config0(&desc[idx], DESC_DIRECTION_ENCRYPT_ENCRYPT);
  1900. set_key_size_aes(&desc[idx], CC_AES_128_BIT_KEY_SIZE);
  1901. set_flow_mode(&desc[idx], S_DIN_to_AES);
  1902. idx++;
  1903. *seq_size = idx;
  1904. }
  1905. static void cc_setup_cmac(struct ahash_request *areq, struct cc_hw_desc desc[],
  1906. unsigned int *seq_size)
  1907. {
  1908. unsigned int idx = *seq_size;
  1909. struct ahash_req_ctx *state = ahash_request_ctx(areq);
  1910. struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
  1911. struct cc_hash_ctx *ctx = crypto_ahash_ctx(tfm);
  1912. /* Setup CMAC Key */
  1913. hw_desc_init(&desc[idx]);
  1914. set_din_type(&desc[idx], DMA_DLLI, ctx->opad_tmp_keys_dma_addr,
  1915. ((ctx->key_params.keylen == 24) ? AES_MAX_KEY_SIZE :
  1916. ctx->key_params.keylen), NS_BIT);
  1917. set_setup_mode(&desc[idx], SETUP_LOAD_KEY0);
  1918. set_cipher_mode(&desc[idx], DRV_CIPHER_CMAC);
  1919. set_cipher_config0(&desc[idx], DESC_DIRECTION_ENCRYPT_ENCRYPT);
  1920. set_key_size_aes(&desc[idx], ctx->key_params.keylen);
  1921. set_flow_mode(&desc[idx], S_DIN_to_AES);
  1922. idx++;
  1923. /* Load MAC state */
  1924. hw_desc_init(&desc[idx]);
  1925. set_din_type(&desc[idx], DMA_DLLI, state->digest_buff_dma_addr,
  1926. CC_AES_BLOCK_SIZE, NS_BIT);
  1927. set_setup_mode(&desc[idx], SETUP_LOAD_STATE0);
  1928. set_cipher_mode(&desc[idx], DRV_CIPHER_CMAC);
  1929. set_cipher_config0(&desc[idx], DESC_DIRECTION_ENCRYPT_ENCRYPT);
  1930. set_key_size_aes(&desc[idx], ctx->key_params.keylen);
  1931. set_flow_mode(&desc[idx], S_DIN_to_AES);
  1932. idx++;
  1933. *seq_size = idx;
  1934. }
  1935. static void cc_set_desc(struct ahash_req_ctx *areq_ctx,
  1936. struct cc_hash_ctx *ctx, unsigned int flow_mode,
  1937. struct cc_hw_desc desc[], bool is_not_last_data,
  1938. unsigned int *seq_size)
  1939. {
  1940. unsigned int idx = *seq_size;
  1941. struct device *dev = drvdata_to_dev(ctx->drvdata);
  1942. if (areq_ctx->data_dma_buf_type == CC_DMA_BUF_DLLI) {
  1943. hw_desc_init(&desc[idx]);
  1944. set_din_type(&desc[idx], DMA_DLLI,
  1945. sg_dma_address(areq_ctx->curr_sg),
  1946. areq_ctx->curr_sg->length, NS_BIT);
  1947. set_flow_mode(&desc[idx], flow_mode);
  1948. idx++;
  1949. } else {
  1950. if (areq_ctx->data_dma_buf_type == CC_DMA_BUF_NULL) {
  1951. dev_dbg(dev, " NULL mode\n");
  1952. /* nothing to build */
  1953. return;
  1954. }
  1955. /* bypass */
  1956. hw_desc_init(&desc[idx]);
  1957. set_din_type(&desc[idx], DMA_DLLI,
  1958. areq_ctx->mlli_params.mlli_dma_addr,
  1959. areq_ctx->mlli_params.mlli_len, NS_BIT);
  1960. set_dout_sram(&desc[idx], ctx->drvdata->mlli_sram_addr,
  1961. areq_ctx->mlli_params.mlli_len);
  1962. set_flow_mode(&desc[idx], BYPASS);
  1963. idx++;
  1964. /* process */
  1965. hw_desc_init(&desc[idx]);
  1966. set_din_type(&desc[idx], DMA_MLLI,
  1967. ctx->drvdata->mlli_sram_addr,
  1968. areq_ctx->mlli_nents, NS_BIT);
  1969. set_flow_mode(&desc[idx], flow_mode);
  1970. idx++;
  1971. }
  1972. if (is_not_last_data)
  1973. set_din_not_last_indication(&desc[(idx - 1)]);
  1974. /* return updated desc sequence size */
  1975. *seq_size = idx;
  1976. }
  1977. static const void *cc_larval_digest(struct device *dev, u32 mode)
  1978. {
  1979. switch (mode) {
  1980. case DRV_HASH_MD5:
  1981. return cc_md5_init;
  1982. case DRV_HASH_SHA1:
  1983. return cc_sha1_init;
  1984. case DRV_HASH_SHA224:
  1985. return cc_sha224_init;
  1986. case DRV_HASH_SHA256:
  1987. return cc_sha256_init;
  1988. case DRV_HASH_SHA384:
  1989. return cc_sha384_init;
  1990. case DRV_HASH_SHA512:
  1991. return cc_sha512_init;
  1992. case DRV_HASH_SM3:
  1993. return cc_sm3_init;
  1994. default:
  1995. dev_err(dev, "Invalid hash mode (%d)\n", mode);
  1996. return cc_md5_init;
  1997. }
  1998. }
  1999. /*!
  2000. * Gets the address of the initial digest in SRAM
  2001. * according to the given hash mode
  2002. *
  2003. * \param drvdata
  2004. * \param mode The Hash mode. Supported modes: MD5/SHA1/SHA224/SHA256
  2005. *
  2006. * \return u32 The address of the initial digest in SRAM
  2007. */
  2008. cc_sram_addr_t cc_larval_digest_addr(void *drvdata, u32 mode)
  2009. {
  2010. struct cc_drvdata *_drvdata = (struct cc_drvdata *)drvdata;
  2011. struct cc_hash_handle *hash_handle = _drvdata->hash_handle;
  2012. struct device *dev = drvdata_to_dev(_drvdata);
  2013. bool sm3_supported = (_drvdata->hw_rev >= CC_HW_REV_713);
  2014. cc_sram_addr_t addr;
  2015. switch (mode) {
  2016. case DRV_HASH_NULL:
  2017. break; /*Ignore*/
  2018. case DRV_HASH_MD5:
  2019. return (hash_handle->larval_digest_sram_addr);
  2020. case DRV_HASH_SHA1:
  2021. return (hash_handle->larval_digest_sram_addr +
  2022. sizeof(cc_md5_init));
  2023. case DRV_HASH_SHA224:
  2024. return (hash_handle->larval_digest_sram_addr +
  2025. sizeof(cc_md5_init) +
  2026. sizeof(cc_sha1_init));
  2027. case DRV_HASH_SHA256:
  2028. return (hash_handle->larval_digest_sram_addr +
  2029. sizeof(cc_md5_init) +
  2030. sizeof(cc_sha1_init) +
  2031. sizeof(cc_sha224_init));
  2032. case DRV_HASH_SM3:
  2033. return (hash_handle->larval_digest_sram_addr +
  2034. sizeof(cc_md5_init) +
  2035. sizeof(cc_sha1_init) +
  2036. sizeof(cc_sha224_init) +
  2037. sizeof(cc_sha256_init));
  2038. case DRV_HASH_SHA384:
  2039. addr = (hash_handle->larval_digest_sram_addr +
  2040. sizeof(cc_md5_init) +
  2041. sizeof(cc_sha1_init) +
  2042. sizeof(cc_sha224_init) +
  2043. sizeof(cc_sha256_init));
  2044. if (sm3_supported)
  2045. addr += sizeof(cc_sm3_init);
  2046. return addr;
  2047. case DRV_HASH_SHA512:
  2048. addr = (hash_handle->larval_digest_sram_addr +
  2049. sizeof(cc_md5_init) +
  2050. sizeof(cc_sha1_init) +
  2051. sizeof(cc_sha224_init) +
  2052. sizeof(cc_sha256_init) +
  2053. sizeof(cc_sha384_init));
  2054. if (sm3_supported)
  2055. addr += sizeof(cc_sm3_init);
  2056. return addr;
  2057. default:
  2058. dev_err(dev, "Invalid hash mode (%d)\n", mode);
  2059. }
  2060. /*This is valid wrong value to avoid kernel crash*/
  2061. return hash_handle->larval_digest_sram_addr;
  2062. }
  2063. cc_sram_addr_t
  2064. cc_digest_len_addr(void *drvdata, u32 mode)
  2065. {
  2066. struct cc_drvdata *_drvdata = (struct cc_drvdata *)drvdata;
  2067. struct cc_hash_handle *hash_handle = _drvdata->hash_handle;
  2068. cc_sram_addr_t digest_len_addr = hash_handle->digest_len_sram_addr;
  2069. switch (mode) {
  2070. case DRV_HASH_SHA1:
  2071. case DRV_HASH_SHA224:
  2072. case DRV_HASH_SHA256:
  2073. case DRV_HASH_MD5:
  2074. return digest_len_addr;
  2075. #if (CC_DEV_SHA_MAX > 256)
  2076. case DRV_HASH_SHA384:
  2077. case DRV_HASH_SHA512:
  2078. return digest_len_addr + sizeof(cc_digest_len_init);
  2079. #endif
  2080. default:
  2081. return digest_len_addr; /*to avoid kernel crash*/
  2082. }
  2083. }