cc_hash.c 65 KB

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