hash.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415
  1. /*
  2. * Hash algorithms supported by the CESA: MD5, SHA1 and SHA256.
  3. *
  4. * Author: Boris Brezillon <boris.brezillon@free-electrons.com>
  5. * Author: Arnaud Ebalard <arno@natisbad.org>
  6. *
  7. * This work is based on an initial version written by
  8. * Sebastian Andrzej Siewior < sebastian at breakpoint dot cc >
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License version 2 as published
  12. * by the Free Software Foundation.
  13. */
  14. #include <crypto/md5.h>
  15. #include <crypto/sha.h>
  16. #include "cesa.h"
  17. struct mv_cesa_ahash_dma_iter {
  18. struct mv_cesa_dma_iter base;
  19. struct mv_cesa_sg_dma_iter src;
  20. };
  21. static inline void
  22. mv_cesa_ahash_req_iter_init(struct mv_cesa_ahash_dma_iter *iter,
  23. struct ahash_request *req)
  24. {
  25. struct mv_cesa_ahash_req *creq = ahash_request_ctx(req);
  26. unsigned int len = req->nbytes + creq->cache_ptr;
  27. if (!creq->last_req)
  28. len &= ~CESA_HASH_BLOCK_SIZE_MSK;
  29. mv_cesa_req_dma_iter_init(&iter->base, len);
  30. mv_cesa_sg_dma_iter_init(&iter->src, req->src, DMA_TO_DEVICE);
  31. iter->src.op_offset = creq->cache_ptr;
  32. }
  33. static inline bool
  34. mv_cesa_ahash_req_iter_next_op(struct mv_cesa_ahash_dma_iter *iter)
  35. {
  36. iter->src.op_offset = 0;
  37. return mv_cesa_req_dma_iter_next_op(&iter->base);
  38. }
  39. static inline int
  40. mv_cesa_ahash_dma_alloc_cache(struct mv_cesa_ahash_dma_req *req, gfp_t flags)
  41. {
  42. req->cache = dma_pool_alloc(cesa_dev->dma->cache_pool, flags,
  43. &req->cache_dma);
  44. if (!req->cache)
  45. return -ENOMEM;
  46. return 0;
  47. }
  48. static inline void
  49. mv_cesa_ahash_dma_free_cache(struct mv_cesa_ahash_dma_req *req)
  50. {
  51. if (!req->cache)
  52. return;
  53. dma_pool_free(cesa_dev->dma->cache_pool, req->cache,
  54. req->cache_dma);
  55. }
  56. static int mv_cesa_ahash_dma_alloc_padding(struct mv_cesa_ahash_dma_req *req,
  57. gfp_t flags)
  58. {
  59. if (req->padding)
  60. return 0;
  61. req->padding = dma_pool_alloc(cesa_dev->dma->padding_pool, flags,
  62. &req->padding_dma);
  63. if (!req->padding)
  64. return -ENOMEM;
  65. return 0;
  66. }
  67. static void mv_cesa_ahash_dma_free_padding(struct mv_cesa_ahash_dma_req *req)
  68. {
  69. if (!req->padding)
  70. return;
  71. dma_pool_free(cesa_dev->dma->padding_pool, req->padding,
  72. req->padding_dma);
  73. req->padding = NULL;
  74. }
  75. static inline void mv_cesa_ahash_dma_last_cleanup(struct ahash_request *req)
  76. {
  77. struct mv_cesa_ahash_req *creq = ahash_request_ctx(req);
  78. mv_cesa_ahash_dma_free_padding(&creq->req.dma);
  79. }
  80. static inline void mv_cesa_ahash_dma_cleanup(struct ahash_request *req)
  81. {
  82. struct mv_cesa_ahash_req *creq = ahash_request_ctx(req);
  83. dma_unmap_sg(cesa_dev->dev, req->src, creq->src_nents, DMA_TO_DEVICE);
  84. mv_cesa_ahash_dma_free_cache(&creq->req.dma);
  85. mv_cesa_dma_cleanup(&creq->base);
  86. }
  87. static inline void mv_cesa_ahash_cleanup(struct ahash_request *req)
  88. {
  89. struct mv_cesa_ahash_req *creq = ahash_request_ctx(req);
  90. if (mv_cesa_req_get_type(&creq->base) == CESA_DMA_REQ)
  91. mv_cesa_ahash_dma_cleanup(req);
  92. }
  93. static void mv_cesa_ahash_last_cleanup(struct ahash_request *req)
  94. {
  95. struct mv_cesa_ahash_req *creq = ahash_request_ctx(req);
  96. if (mv_cesa_req_get_type(&creq->base) == CESA_DMA_REQ)
  97. mv_cesa_ahash_dma_last_cleanup(req);
  98. }
  99. static int mv_cesa_ahash_pad_len(struct mv_cesa_ahash_req *creq)
  100. {
  101. unsigned int index, padlen;
  102. index = creq->len & CESA_HASH_BLOCK_SIZE_MSK;
  103. padlen = (index < 56) ? (56 - index) : (64 + 56 - index);
  104. return padlen;
  105. }
  106. static int mv_cesa_ahash_pad_req(struct mv_cesa_ahash_req *creq, u8 *buf)
  107. {
  108. unsigned int index, padlen;
  109. buf[0] = 0x80;
  110. /* Pad out to 56 mod 64 */
  111. index = creq->len & CESA_HASH_BLOCK_SIZE_MSK;
  112. padlen = mv_cesa_ahash_pad_len(creq);
  113. memset(buf + 1, 0, padlen - 1);
  114. if (creq->algo_le) {
  115. __le64 bits = cpu_to_le64(creq->len << 3);
  116. memcpy(buf + padlen, &bits, sizeof(bits));
  117. } else {
  118. __be64 bits = cpu_to_be64(creq->len << 3);
  119. memcpy(buf + padlen, &bits, sizeof(bits));
  120. }
  121. return padlen + 8;
  122. }
  123. static void mv_cesa_ahash_std_step(struct ahash_request *req)
  124. {
  125. struct mv_cesa_ahash_req *creq = ahash_request_ctx(req);
  126. struct mv_cesa_ahash_std_req *sreq = &creq->req.std;
  127. struct mv_cesa_engine *engine = creq->base.engine;
  128. struct mv_cesa_op_ctx *op;
  129. unsigned int new_cache_ptr = 0;
  130. u32 frag_mode;
  131. size_t len;
  132. unsigned int digsize;
  133. int i;
  134. mv_cesa_adjust_op(engine, &creq->op_tmpl);
  135. memcpy_toio(engine->sram, &creq->op_tmpl, sizeof(creq->op_tmpl));
  136. if (!sreq->offset) {
  137. digsize = crypto_ahash_digestsize(crypto_ahash_reqtfm(req));
  138. for (i = 0; i < digsize / 4; i++)
  139. writel_relaxed(creq->state[i], engine->regs + CESA_IVDIG(i));
  140. }
  141. if (creq->cache_ptr)
  142. memcpy_toio(engine->sram + CESA_SA_DATA_SRAM_OFFSET,
  143. creq->cache, creq->cache_ptr);
  144. len = min_t(size_t, req->nbytes + creq->cache_ptr - sreq->offset,
  145. CESA_SA_SRAM_PAYLOAD_SIZE);
  146. if (!creq->last_req) {
  147. new_cache_ptr = len & CESA_HASH_BLOCK_SIZE_MSK;
  148. len &= ~CESA_HASH_BLOCK_SIZE_MSK;
  149. }
  150. if (len - creq->cache_ptr)
  151. sreq->offset += sg_pcopy_to_buffer(req->src, creq->src_nents,
  152. engine->sram +
  153. CESA_SA_DATA_SRAM_OFFSET +
  154. creq->cache_ptr,
  155. len - creq->cache_ptr,
  156. sreq->offset);
  157. op = &creq->op_tmpl;
  158. frag_mode = mv_cesa_get_op_cfg(op) & CESA_SA_DESC_CFG_FRAG_MSK;
  159. if (creq->last_req && sreq->offset == req->nbytes &&
  160. creq->len <= CESA_SA_DESC_MAC_SRC_TOTAL_LEN_MAX) {
  161. if (frag_mode == CESA_SA_DESC_CFG_FIRST_FRAG)
  162. frag_mode = CESA_SA_DESC_CFG_NOT_FRAG;
  163. else if (frag_mode == CESA_SA_DESC_CFG_MID_FRAG)
  164. frag_mode = CESA_SA_DESC_CFG_LAST_FRAG;
  165. }
  166. if (frag_mode == CESA_SA_DESC_CFG_NOT_FRAG ||
  167. frag_mode == CESA_SA_DESC_CFG_LAST_FRAG) {
  168. if (len &&
  169. creq->len <= CESA_SA_DESC_MAC_SRC_TOTAL_LEN_MAX) {
  170. mv_cesa_set_mac_op_total_len(op, creq->len);
  171. } else {
  172. int trailerlen = mv_cesa_ahash_pad_len(creq) + 8;
  173. if (len + trailerlen > CESA_SA_SRAM_PAYLOAD_SIZE) {
  174. len &= CESA_HASH_BLOCK_SIZE_MSK;
  175. new_cache_ptr = 64 - trailerlen;
  176. memcpy_fromio(creq->cache,
  177. engine->sram +
  178. CESA_SA_DATA_SRAM_OFFSET + len,
  179. new_cache_ptr);
  180. } else {
  181. len += mv_cesa_ahash_pad_req(creq,
  182. engine->sram + len +
  183. CESA_SA_DATA_SRAM_OFFSET);
  184. }
  185. if (frag_mode == CESA_SA_DESC_CFG_LAST_FRAG)
  186. frag_mode = CESA_SA_DESC_CFG_MID_FRAG;
  187. else
  188. frag_mode = CESA_SA_DESC_CFG_FIRST_FRAG;
  189. }
  190. }
  191. mv_cesa_set_mac_op_frag_len(op, len);
  192. mv_cesa_update_op_cfg(op, frag_mode, CESA_SA_DESC_CFG_FRAG_MSK);
  193. /* FIXME: only update enc_len field */
  194. memcpy_toio(engine->sram, op, sizeof(*op));
  195. if (frag_mode == CESA_SA_DESC_CFG_FIRST_FRAG)
  196. mv_cesa_update_op_cfg(op, CESA_SA_DESC_CFG_MID_FRAG,
  197. CESA_SA_DESC_CFG_FRAG_MSK);
  198. creq->cache_ptr = new_cache_ptr;
  199. mv_cesa_set_int_mask(engine, CESA_SA_INT_ACCEL0_DONE);
  200. writel_relaxed(CESA_SA_CFG_PARA_DIS, engine->regs + CESA_SA_CFG);
  201. BUG_ON(readl(engine->regs + CESA_SA_CMD) &
  202. CESA_SA_CMD_EN_CESA_SA_ACCL0);
  203. writel(CESA_SA_CMD_EN_CESA_SA_ACCL0, engine->regs + CESA_SA_CMD);
  204. }
  205. static int mv_cesa_ahash_std_process(struct ahash_request *req, u32 status)
  206. {
  207. struct mv_cesa_ahash_req *creq = ahash_request_ctx(req);
  208. struct mv_cesa_ahash_std_req *sreq = &creq->req.std;
  209. if (sreq->offset < (req->nbytes - creq->cache_ptr))
  210. return -EINPROGRESS;
  211. return 0;
  212. }
  213. static inline void mv_cesa_ahash_dma_prepare(struct ahash_request *req)
  214. {
  215. struct mv_cesa_ahash_req *creq = ahash_request_ctx(req);
  216. struct mv_cesa_req *basereq = &creq->base;
  217. mv_cesa_dma_prepare(basereq, basereq->engine);
  218. }
  219. static void mv_cesa_ahash_std_prepare(struct ahash_request *req)
  220. {
  221. struct mv_cesa_ahash_req *creq = ahash_request_ctx(req);
  222. struct mv_cesa_ahash_std_req *sreq = &creq->req.std;
  223. sreq->offset = 0;
  224. }
  225. static void mv_cesa_ahash_dma_step(struct ahash_request *req)
  226. {
  227. struct mv_cesa_ahash_req *creq = ahash_request_ctx(req);
  228. struct mv_cesa_req *base = &creq->base;
  229. /* We must explicitly set the digest state. */
  230. if (base->chain.first->flags & CESA_TDMA_SET_STATE) {
  231. struct mv_cesa_engine *engine = base->engine;
  232. int i;
  233. /* Set the hash state in the IVDIG regs. */
  234. for (i = 0; i < ARRAY_SIZE(creq->state); i++)
  235. writel_relaxed(creq->state[i], engine->regs +
  236. CESA_IVDIG(i));
  237. }
  238. mv_cesa_dma_step(base);
  239. }
  240. static void mv_cesa_ahash_step(struct crypto_async_request *req)
  241. {
  242. struct ahash_request *ahashreq = ahash_request_cast(req);
  243. struct mv_cesa_ahash_req *creq = ahash_request_ctx(ahashreq);
  244. if (mv_cesa_req_get_type(&creq->base) == CESA_DMA_REQ)
  245. mv_cesa_ahash_dma_step(ahashreq);
  246. else
  247. mv_cesa_ahash_std_step(ahashreq);
  248. }
  249. static int mv_cesa_ahash_process(struct crypto_async_request *req, u32 status)
  250. {
  251. struct ahash_request *ahashreq = ahash_request_cast(req);
  252. struct mv_cesa_ahash_req *creq = ahash_request_ctx(ahashreq);
  253. if (mv_cesa_req_get_type(&creq->base) == CESA_DMA_REQ)
  254. return mv_cesa_dma_process(&creq->base, status);
  255. return mv_cesa_ahash_std_process(ahashreq, status);
  256. }
  257. static void mv_cesa_ahash_complete(struct crypto_async_request *req)
  258. {
  259. struct ahash_request *ahashreq = ahash_request_cast(req);
  260. struct mv_cesa_ahash_req *creq = ahash_request_ctx(ahashreq);
  261. struct mv_cesa_engine *engine = creq->base.engine;
  262. unsigned int digsize;
  263. int i;
  264. digsize = crypto_ahash_digestsize(crypto_ahash_reqtfm(ahashreq));
  265. for (i = 0; i < digsize / 4; i++)
  266. creq->state[i] = readl_relaxed(engine->regs + CESA_IVDIG(i));
  267. if (creq->last_req) {
  268. /*
  269. * Hardware's MD5 digest is in little endian format, but
  270. * SHA in big endian format
  271. */
  272. if (creq->algo_le) {
  273. __le32 *result = (void *)ahashreq->result;
  274. for (i = 0; i < digsize / 4; i++)
  275. result[i] = cpu_to_le32(creq->state[i]);
  276. } else {
  277. __be32 *result = (void *)ahashreq->result;
  278. for (i = 0; i < digsize / 4; i++)
  279. result[i] = cpu_to_be32(creq->state[i]);
  280. }
  281. }
  282. atomic_sub(ahashreq->nbytes, &engine->load);
  283. }
  284. static void mv_cesa_ahash_prepare(struct crypto_async_request *req,
  285. struct mv_cesa_engine *engine)
  286. {
  287. struct ahash_request *ahashreq = ahash_request_cast(req);
  288. struct mv_cesa_ahash_req *creq = ahash_request_ctx(ahashreq);
  289. creq->base.engine = engine;
  290. if (mv_cesa_req_get_type(&creq->base) == CESA_DMA_REQ)
  291. mv_cesa_ahash_dma_prepare(ahashreq);
  292. else
  293. mv_cesa_ahash_std_prepare(ahashreq);
  294. }
  295. static void mv_cesa_ahash_req_cleanup(struct crypto_async_request *req)
  296. {
  297. struct ahash_request *ahashreq = ahash_request_cast(req);
  298. struct mv_cesa_ahash_req *creq = ahash_request_ctx(ahashreq);
  299. if (creq->last_req)
  300. mv_cesa_ahash_last_cleanup(ahashreq);
  301. mv_cesa_ahash_cleanup(ahashreq);
  302. if (creq->cache_ptr)
  303. sg_pcopy_to_buffer(ahashreq->src, creq->src_nents,
  304. creq->cache,
  305. creq->cache_ptr,
  306. ahashreq->nbytes - creq->cache_ptr);
  307. }
  308. static const struct mv_cesa_req_ops mv_cesa_ahash_req_ops = {
  309. .step = mv_cesa_ahash_step,
  310. .process = mv_cesa_ahash_process,
  311. .cleanup = mv_cesa_ahash_req_cleanup,
  312. .complete = mv_cesa_ahash_complete,
  313. };
  314. static void mv_cesa_ahash_init(struct ahash_request *req,
  315. struct mv_cesa_op_ctx *tmpl, bool algo_le)
  316. {
  317. struct mv_cesa_ahash_req *creq = ahash_request_ctx(req);
  318. memset(creq, 0, sizeof(*creq));
  319. mv_cesa_update_op_cfg(tmpl,
  320. CESA_SA_DESC_CFG_OP_MAC_ONLY |
  321. CESA_SA_DESC_CFG_FIRST_FRAG,
  322. CESA_SA_DESC_CFG_OP_MSK |
  323. CESA_SA_DESC_CFG_FRAG_MSK);
  324. mv_cesa_set_mac_op_total_len(tmpl, 0);
  325. mv_cesa_set_mac_op_frag_len(tmpl, 0);
  326. creq->op_tmpl = *tmpl;
  327. creq->len = 0;
  328. creq->algo_le = algo_le;
  329. }
  330. static inline int mv_cesa_ahash_cra_init(struct crypto_tfm *tfm)
  331. {
  332. struct mv_cesa_hash_ctx *ctx = crypto_tfm_ctx(tfm);
  333. ctx->base.ops = &mv_cesa_ahash_req_ops;
  334. crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
  335. sizeof(struct mv_cesa_ahash_req));
  336. return 0;
  337. }
  338. static bool mv_cesa_ahash_cache_req(struct ahash_request *req)
  339. {
  340. struct mv_cesa_ahash_req *creq = ahash_request_ctx(req);
  341. bool cached = false;
  342. if (creq->cache_ptr + req->nbytes < CESA_MAX_HASH_BLOCK_SIZE && !creq->last_req) {
  343. cached = true;
  344. if (!req->nbytes)
  345. return cached;
  346. sg_pcopy_to_buffer(req->src, creq->src_nents,
  347. creq->cache + creq->cache_ptr,
  348. req->nbytes, 0);
  349. creq->cache_ptr += req->nbytes;
  350. }
  351. return cached;
  352. }
  353. static struct mv_cesa_op_ctx *
  354. mv_cesa_dma_add_frag(struct mv_cesa_tdma_chain *chain,
  355. struct mv_cesa_op_ctx *tmpl, unsigned int frag_len,
  356. gfp_t flags)
  357. {
  358. struct mv_cesa_op_ctx *op;
  359. int ret;
  360. op = mv_cesa_dma_add_op(chain, tmpl, false, flags);
  361. if (IS_ERR(op))
  362. return op;
  363. /* Set the operation block fragment length. */
  364. mv_cesa_set_mac_op_frag_len(op, frag_len);
  365. /* Append dummy desc to launch operation */
  366. ret = mv_cesa_dma_add_dummy_launch(chain, flags);
  367. if (ret)
  368. return ERR_PTR(ret);
  369. if (mv_cesa_mac_op_is_first_frag(tmpl))
  370. mv_cesa_update_op_cfg(tmpl,
  371. CESA_SA_DESC_CFG_MID_FRAG,
  372. CESA_SA_DESC_CFG_FRAG_MSK);
  373. return op;
  374. }
  375. static int
  376. mv_cesa_ahash_dma_add_cache(struct mv_cesa_tdma_chain *chain,
  377. struct mv_cesa_ahash_req *creq,
  378. gfp_t flags)
  379. {
  380. struct mv_cesa_ahash_dma_req *ahashdreq = &creq->req.dma;
  381. int ret;
  382. if (!creq->cache_ptr)
  383. return 0;
  384. ret = mv_cesa_ahash_dma_alloc_cache(ahashdreq, flags);
  385. if (ret)
  386. return ret;
  387. memcpy(ahashdreq->cache, creq->cache, creq->cache_ptr);
  388. return mv_cesa_dma_add_data_transfer(chain,
  389. CESA_SA_DATA_SRAM_OFFSET,
  390. ahashdreq->cache_dma,
  391. creq->cache_ptr,
  392. CESA_TDMA_DST_IN_SRAM,
  393. flags);
  394. }
  395. static struct mv_cesa_op_ctx *
  396. mv_cesa_ahash_dma_last_req(struct mv_cesa_tdma_chain *chain,
  397. struct mv_cesa_ahash_dma_iter *dma_iter,
  398. struct mv_cesa_ahash_req *creq,
  399. unsigned int frag_len, gfp_t flags)
  400. {
  401. struct mv_cesa_ahash_dma_req *ahashdreq = &creq->req.dma;
  402. unsigned int len, trailerlen, padoff = 0;
  403. struct mv_cesa_op_ctx *op;
  404. int ret;
  405. /*
  406. * If the transfer is smaller than our maximum length, and we have
  407. * some data outstanding, we can ask the engine to finish the hash.
  408. */
  409. if (creq->len <= CESA_SA_DESC_MAC_SRC_TOTAL_LEN_MAX && frag_len) {
  410. op = mv_cesa_dma_add_frag(chain, &creq->op_tmpl, frag_len,
  411. flags);
  412. if (IS_ERR(op))
  413. return op;
  414. mv_cesa_set_mac_op_total_len(op, creq->len);
  415. mv_cesa_update_op_cfg(op, mv_cesa_mac_op_is_first_frag(op) ?
  416. CESA_SA_DESC_CFG_NOT_FRAG :
  417. CESA_SA_DESC_CFG_LAST_FRAG,
  418. CESA_SA_DESC_CFG_FRAG_MSK);
  419. return op;
  420. }
  421. /*
  422. * The request is longer than the engine can handle, or we have
  423. * no data outstanding. Manually generate the padding, adding it
  424. * as a "mid" fragment.
  425. */
  426. ret = mv_cesa_ahash_dma_alloc_padding(ahashdreq, flags);
  427. if (ret)
  428. return ERR_PTR(ret);
  429. trailerlen = mv_cesa_ahash_pad_req(creq, ahashdreq->padding);
  430. len = min(CESA_SA_SRAM_PAYLOAD_SIZE - frag_len, trailerlen);
  431. if (len) {
  432. ret = mv_cesa_dma_add_data_transfer(chain,
  433. CESA_SA_DATA_SRAM_OFFSET +
  434. frag_len,
  435. ahashdreq->padding_dma,
  436. len, CESA_TDMA_DST_IN_SRAM,
  437. flags);
  438. if (ret)
  439. return ERR_PTR(ret);
  440. op = mv_cesa_dma_add_frag(chain, &creq->op_tmpl, frag_len + len,
  441. flags);
  442. if (IS_ERR(op))
  443. return op;
  444. if (len == trailerlen)
  445. return op;
  446. padoff += len;
  447. }
  448. ret = mv_cesa_dma_add_data_transfer(chain,
  449. CESA_SA_DATA_SRAM_OFFSET,
  450. ahashdreq->padding_dma +
  451. padoff,
  452. trailerlen - padoff,
  453. CESA_TDMA_DST_IN_SRAM,
  454. flags);
  455. if (ret)
  456. return ERR_PTR(ret);
  457. return mv_cesa_dma_add_frag(chain, &creq->op_tmpl, trailerlen - padoff,
  458. flags);
  459. }
  460. static int mv_cesa_ahash_dma_req_init(struct ahash_request *req)
  461. {
  462. struct mv_cesa_ahash_req *creq = ahash_request_ctx(req);
  463. gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
  464. GFP_KERNEL : GFP_ATOMIC;
  465. struct mv_cesa_req *basereq = &creq->base;
  466. struct mv_cesa_ahash_dma_iter iter;
  467. struct mv_cesa_op_ctx *op = NULL;
  468. unsigned int frag_len;
  469. bool set_state = false;
  470. int ret;
  471. basereq->chain.first = NULL;
  472. basereq->chain.last = NULL;
  473. if (!mv_cesa_mac_op_is_first_frag(&creq->op_tmpl))
  474. set_state = true;
  475. if (creq->src_nents) {
  476. ret = dma_map_sg(cesa_dev->dev, req->src, creq->src_nents,
  477. DMA_TO_DEVICE);
  478. if (!ret) {
  479. ret = -ENOMEM;
  480. goto err;
  481. }
  482. }
  483. mv_cesa_tdma_desc_iter_init(&basereq->chain);
  484. mv_cesa_ahash_req_iter_init(&iter, req);
  485. /*
  486. * Add the cache (left-over data from a previous block) first.
  487. * This will never overflow the SRAM size.
  488. */
  489. ret = mv_cesa_ahash_dma_add_cache(&basereq->chain, creq, flags);
  490. if (ret)
  491. goto err_free_tdma;
  492. if (iter.src.sg) {
  493. /*
  494. * Add all the new data, inserting an operation block and
  495. * launch command between each full SRAM block-worth of
  496. * data. We intentionally do not add the final op block.
  497. */
  498. while (true) {
  499. ret = mv_cesa_dma_add_op_transfers(&basereq->chain,
  500. &iter.base,
  501. &iter.src, flags);
  502. if (ret)
  503. goto err_free_tdma;
  504. frag_len = iter.base.op_len;
  505. if (!mv_cesa_ahash_req_iter_next_op(&iter))
  506. break;
  507. op = mv_cesa_dma_add_frag(&basereq->chain, &creq->op_tmpl,
  508. frag_len, flags);
  509. if (IS_ERR(op)) {
  510. ret = PTR_ERR(op);
  511. goto err_free_tdma;
  512. }
  513. }
  514. } else {
  515. /* Account for the data that was in the cache. */
  516. frag_len = iter.base.op_len;
  517. }
  518. /*
  519. * At this point, frag_len indicates whether we have any data
  520. * outstanding which needs an operation. Queue up the final
  521. * operation, which depends whether this is the final request.
  522. */
  523. if (creq->last_req)
  524. op = mv_cesa_ahash_dma_last_req(&basereq->chain, &iter, creq,
  525. frag_len, flags);
  526. else if (frag_len)
  527. op = mv_cesa_dma_add_frag(&basereq->chain, &creq->op_tmpl,
  528. frag_len, flags);
  529. if (IS_ERR(op)) {
  530. ret = PTR_ERR(op);
  531. goto err_free_tdma;
  532. }
  533. if (op) {
  534. /* Add dummy desc to wait for crypto operation end */
  535. ret = mv_cesa_dma_add_dummy_end(&basereq->chain, flags);
  536. if (ret)
  537. goto err_free_tdma;
  538. }
  539. if (!creq->last_req)
  540. creq->cache_ptr = req->nbytes + creq->cache_ptr -
  541. iter.base.len;
  542. else
  543. creq->cache_ptr = 0;
  544. basereq->chain.last->flags |= (CESA_TDMA_END_OF_REQ |
  545. CESA_TDMA_BREAK_CHAIN);
  546. if (set_state) {
  547. /*
  548. * Put the CESA_TDMA_SET_STATE flag on the first tdma desc to
  549. * let the step logic know that the IVDIG registers should be
  550. * explicitly set before launching a TDMA chain.
  551. */
  552. basereq->chain.first->flags |= CESA_TDMA_SET_STATE;
  553. }
  554. return 0;
  555. err_free_tdma:
  556. mv_cesa_dma_cleanup(basereq);
  557. dma_unmap_sg(cesa_dev->dev, req->src, creq->src_nents, DMA_TO_DEVICE);
  558. err:
  559. mv_cesa_ahash_last_cleanup(req);
  560. return ret;
  561. }
  562. static int mv_cesa_ahash_req_init(struct ahash_request *req, bool *cached)
  563. {
  564. struct mv_cesa_ahash_req *creq = ahash_request_ctx(req);
  565. creq->src_nents = sg_nents_for_len(req->src, req->nbytes);
  566. if (creq->src_nents < 0) {
  567. dev_err(cesa_dev->dev, "Invalid number of src SG");
  568. return creq->src_nents;
  569. }
  570. *cached = mv_cesa_ahash_cache_req(req);
  571. if (*cached)
  572. return 0;
  573. if (cesa_dev->caps->has_tdma)
  574. return mv_cesa_ahash_dma_req_init(req);
  575. else
  576. return 0;
  577. }
  578. static int mv_cesa_ahash_queue_req(struct ahash_request *req)
  579. {
  580. struct mv_cesa_ahash_req *creq = ahash_request_ctx(req);
  581. struct mv_cesa_engine *engine;
  582. bool cached = false;
  583. int ret;
  584. ret = mv_cesa_ahash_req_init(req, &cached);
  585. if (ret)
  586. return ret;
  587. if (cached)
  588. return 0;
  589. engine = mv_cesa_select_engine(req->nbytes);
  590. mv_cesa_ahash_prepare(&req->base, engine);
  591. ret = mv_cesa_queue_req(&req->base, &creq->base);
  592. if (mv_cesa_req_needs_cleanup(&req->base, ret))
  593. mv_cesa_ahash_cleanup(req);
  594. return ret;
  595. }
  596. static int mv_cesa_ahash_update(struct ahash_request *req)
  597. {
  598. struct mv_cesa_ahash_req *creq = ahash_request_ctx(req);
  599. creq->len += req->nbytes;
  600. return mv_cesa_ahash_queue_req(req);
  601. }
  602. static int mv_cesa_ahash_final(struct ahash_request *req)
  603. {
  604. struct mv_cesa_ahash_req *creq = ahash_request_ctx(req);
  605. struct mv_cesa_op_ctx *tmpl = &creq->op_tmpl;
  606. mv_cesa_set_mac_op_total_len(tmpl, creq->len);
  607. creq->last_req = true;
  608. req->nbytes = 0;
  609. return mv_cesa_ahash_queue_req(req);
  610. }
  611. static int mv_cesa_ahash_finup(struct ahash_request *req)
  612. {
  613. struct mv_cesa_ahash_req *creq = ahash_request_ctx(req);
  614. struct mv_cesa_op_ctx *tmpl = &creq->op_tmpl;
  615. creq->len += req->nbytes;
  616. mv_cesa_set_mac_op_total_len(tmpl, creq->len);
  617. creq->last_req = true;
  618. return mv_cesa_ahash_queue_req(req);
  619. }
  620. static int mv_cesa_ahash_export(struct ahash_request *req, void *hash,
  621. u64 *len, void *cache)
  622. {
  623. struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
  624. struct mv_cesa_ahash_req *creq = ahash_request_ctx(req);
  625. unsigned int digsize = crypto_ahash_digestsize(ahash);
  626. unsigned int blocksize;
  627. blocksize = crypto_ahash_blocksize(ahash);
  628. *len = creq->len;
  629. memcpy(hash, creq->state, digsize);
  630. memset(cache, 0, blocksize);
  631. memcpy(cache, creq->cache, creq->cache_ptr);
  632. return 0;
  633. }
  634. static int mv_cesa_ahash_import(struct ahash_request *req, const void *hash,
  635. u64 len, const void *cache)
  636. {
  637. struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
  638. struct mv_cesa_ahash_req *creq = ahash_request_ctx(req);
  639. unsigned int digsize = crypto_ahash_digestsize(ahash);
  640. unsigned int blocksize;
  641. unsigned int cache_ptr;
  642. int ret;
  643. ret = crypto_ahash_init(req);
  644. if (ret)
  645. return ret;
  646. blocksize = crypto_ahash_blocksize(ahash);
  647. if (len >= blocksize)
  648. mv_cesa_update_op_cfg(&creq->op_tmpl,
  649. CESA_SA_DESC_CFG_MID_FRAG,
  650. CESA_SA_DESC_CFG_FRAG_MSK);
  651. creq->len = len;
  652. memcpy(creq->state, hash, digsize);
  653. creq->cache_ptr = 0;
  654. cache_ptr = do_div(len, blocksize);
  655. if (!cache_ptr)
  656. return 0;
  657. memcpy(creq->cache, cache, cache_ptr);
  658. creq->cache_ptr = cache_ptr;
  659. return 0;
  660. }
  661. static int mv_cesa_md5_init(struct ahash_request *req)
  662. {
  663. struct mv_cesa_ahash_req *creq = ahash_request_ctx(req);
  664. struct mv_cesa_op_ctx tmpl = { };
  665. mv_cesa_set_op_cfg(&tmpl, CESA_SA_DESC_CFG_MACM_MD5);
  666. mv_cesa_ahash_init(req, &tmpl, true);
  667. creq->state[0] = MD5_H0;
  668. creq->state[1] = MD5_H1;
  669. creq->state[2] = MD5_H2;
  670. creq->state[3] = MD5_H3;
  671. return 0;
  672. }
  673. static int mv_cesa_md5_export(struct ahash_request *req, void *out)
  674. {
  675. struct md5_state *out_state = out;
  676. return mv_cesa_ahash_export(req, out_state->hash,
  677. &out_state->byte_count, out_state->block);
  678. }
  679. static int mv_cesa_md5_import(struct ahash_request *req, const void *in)
  680. {
  681. const struct md5_state *in_state = in;
  682. return mv_cesa_ahash_import(req, in_state->hash, in_state->byte_count,
  683. in_state->block);
  684. }
  685. static int mv_cesa_md5_digest(struct ahash_request *req)
  686. {
  687. int ret;
  688. ret = mv_cesa_md5_init(req);
  689. if (ret)
  690. return ret;
  691. return mv_cesa_ahash_finup(req);
  692. }
  693. struct ahash_alg mv_md5_alg = {
  694. .init = mv_cesa_md5_init,
  695. .update = mv_cesa_ahash_update,
  696. .final = mv_cesa_ahash_final,
  697. .finup = mv_cesa_ahash_finup,
  698. .digest = mv_cesa_md5_digest,
  699. .export = mv_cesa_md5_export,
  700. .import = mv_cesa_md5_import,
  701. .halg = {
  702. .digestsize = MD5_DIGEST_SIZE,
  703. .statesize = sizeof(struct md5_state),
  704. .base = {
  705. .cra_name = "md5",
  706. .cra_driver_name = "mv-md5",
  707. .cra_priority = 300,
  708. .cra_flags = CRYPTO_ALG_ASYNC |
  709. CRYPTO_ALG_KERN_DRIVER_ONLY,
  710. .cra_blocksize = MD5_HMAC_BLOCK_SIZE,
  711. .cra_ctxsize = sizeof(struct mv_cesa_hash_ctx),
  712. .cra_init = mv_cesa_ahash_cra_init,
  713. .cra_module = THIS_MODULE,
  714. }
  715. }
  716. };
  717. static int mv_cesa_sha1_init(struct ahash_request *req)
  718. {
  719. struct mv_cesa_ahash_req *creq = ahash_request_ctx(req);
  720. struct mv_cesa_op_ctx tmpl = { };
  721. mv_cesa_set_op_cfg(&tmpl, CESA_SA_DESC_CFG_MACM_SHA1);
  722. mv_cesa_ahash_init(req, &tmpl, false);
  723. creq->state[0] = SHA1_H0;
  724. creq->state[1] = SHA1_H1;
  725. creq->state[2] = SHA1_H2;
  726. creq->state[3] = SHA1_H3;
  727. creq->state[4] = SHA1_H4;
  728. return 0;
  729. }
  730. static int mv_cesa_sha1_export(struct ahash_request *req, void *out)
  731. {
  732. struct sha1_state *out_state = out;
  733. return mv_cesa_ahash_export(req, out_state->state, &out_state->count,
  734. out_state->buffer);
  735. }
  736. static int mv_cesa_sha1_import(struct ahash_request *req, const void *in)
  737. {
  738. const struct sha1_state *in_state = in;
  739. return mv_cesa_ahash_import(req, in_state->state, in_state->count,
  740. in_state->buffer);
  741. }
  742. static int mv_cesa_sha1_digest(struct ahash_request *req)
  743. {
  744. int ret;
  745. ret = mv_cesa_sha1_init(req);
  746. if (ret)
  747. return ret;
  748. return mv_cesa_ahash_finup(req);
  749. }
  750. struct ahash_alg mv_sha1_alg = {
  751. .init = mv_cesa_sha1_init,
  752. .update = mv_cesa_ahash_update,
  753. .final = mv_cesa_ahash_final,
  754. .finup = mv_cesa_ahash_finup,
  755. .digest = mv_cesa_sha1_digest,
  756. .export = mv_cesa_sha1_export,
  757. .import = mv_cesa_sha1_import,
  758. .halg = {
  759. .digestsize = SHA1_DIGEST_SIZE,
  760. .statesize = sizeof(struct sha1_state),
  761. .base = {
  762. .cra_name = "sha1",
  763. .cra_driver_name = "mv-sha1",
  764. .cra_priority = 300,
  765. .cra_flags = CRYPTO_ALG_ASYNC |
  766. CRYPTO_ALG_KERN_DRIVER_ONLY,
  767. .cra_blocksize = SHA1_BLOCK_SIZE,
  768. .cra_ctxsize = sizeof(struct mv_cesa_hash_ctx),
  769. .cra_init = mv_cesa_ahash_cra_init,
  770. .cra_module = THIS_MODULE,
  771. }
  772. }
  773. };
  774. static int mv_cesa_sha256_init(struct ahash_request *req)
  775. {
  776. struct mv_cesa_ahash_req *creq = ahash_request_ctx(req);
  777. struct mv_cesa_op_ctx tmpl = { };
  778. mv_cesa_set_op_cfg(&tmpl, CESA_SA_DESC_CFG_MACM_SHA256);
  779. mv_cesa_ahash_init(req, &tmpl, false);
  780. creq->state[0] = SHA256_H0;
  781. creq->state[1] = SHA256_H1;
  782. creq->state[2] = SHA256_H2;
  783. creq->state[3] = SHA256_H3;
  784. creq->state[4] = SHA256_H4;
  785. creq->state[5] = SHA256_H5;
  786. creq->state[6] = SHA256_H6;
  787. creq->state[7] = SHA256_H7;
  788. return 0;
  789. }
  790. static int mv_cesa_sha256_digest(struct ahash_request *req)
  791. {
  792. int ret;
  793. ret = mv_cesa_sha256_init(req);
  794. if (ret)
  795. return ret;
  796. return mv_cesa_ahash_finup(req);
  797. }
  798. static int mv_cesa_sha256_export(struct ahash_request *req, void *out)
  799. {
  800. struct sha256_state *out_state = out;
  801. return mv_cesa_ahash_export(req, out_state->state, &out_state->count,
  802. out_state->buf);
  803. }
  804. static int mv_cesa_sha256_import(struct ahash_request *req, const void *in)
  805. {
  806. const struct sha256_state *in_state = in;
  807. return mv_cesa_ahash_import(req, in_state->state, in_state->count,
  808. in_state->buf);
  809. }
  810. struct ahash_alg mv_sha256_alg = {
  811. .init = mv_cesa_sha256_init,
  812. .update = mv_cesa_ahash_update,
  813. .final = mv_cesa_ahash_final,
  814. .finup = mv_cesa_ahash_finup,
  815. .digest = mv_cesa_sha256_digest,
  816. .export = mv_cesa_sha256_export,
  817. .import = mv_cesa_sha256_import,
  818. .halg = {
  819. .digestsize = SHA256_DIGEST_SIZE,
  820. .statesize = sizeof(struct sha256_state),
  821. .base = {
  822. .cra_name = "sha256",
  823. .cra_driver_name = "mv-sha256",
  824. .cra_priority = 300,
  825. .cra_flags = CRYPTO_ALG_ASYNC |
  826. CRYPTO_ALG_KERN_DRIVER_ONLY,
  827. .cra_blocksize = SHA256_BLOCK_SIZE,
  828. .cra_ctxsize = sizeof(struct mv_cesa_hash_ctx),
  829. .cra_init = mv_cesa_ahash_cra_init,
  830. .cra_module = THIS_MODULE,
  831. }
  832. }
  833. };
  834. struct mv_cesa_ahash_result {
  835. struct completion completion;
  836. int error;
  837. };
  838. static void mv_cesa_hmac_ahash_complete(struct crypto_async_request *req,
  839. int error)
  840. {
  841. struct mv_cesa_ahash_result *result = req->data;
  842. if (error == -EINPROGRESS)
  843. return;
  844. result->error = error;
  845. complete(&result->completion);
  846. }
  847. static int mv_cesa_ahmac_iv_state_init(struct ahash_request *req, u8 *pad,
  848. void *state, unsigned int blocksize)
  849. {
  850. struct mv_cesa_ahash_result result;
  851. struct scatterlist sg;
  852. int ret;
  853. ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
  854. mv_cesa_hmac_ahash_complete, &result);
  855. sg_init_one(&sg, pad, blocksize);
  856. ahash_request_set_crypt(req, &sg, pad, blocksize);
  857. init_completion(&result.completion);
  858. ret = crypto_ahash_init(req);
  859. if (ret)
  860. return ret;
  861. ret = crypto_ahash_update(req);
  862. if (ret && ret != -EINPROGRESS)
  863. return ret;
  864. wait_for_completion_interruptible(&result.completion);
  865. if (result.error)
  866. return result.error;
  867. ret = crypto_ahash_export(req, state);
  868. if (ret)
  869. return ret;
  870. return 0;
  871. }
  872. static int mv_cesa_ahmac_pad_init(struct ahash_request *req,
  873. const u8 *key, unsigned int keylen,
  874. u8 *ipad, u8 *opad,
  875. unsigned int blocksize)
  876. {
  877. struct mv_cesa_ahash_result result;
  878. struct scatterlist sg;
  879. int ret;
  880. int i;
  881. if (keylen <= blocksize) {
  882. memcpy(ipad, key, keylen);
  883. } else {
  884. u8 *keydup = kmemdup(key, keylen, GFP_KERNEL);
  885. if (!keydup)
  886. return -ENOMEM;
  887. ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
  888. mv_cesa_hmac_ahash_complete,
  889. &result);
  890. sg_init_one(&sg, keydup, keylen);
  891. ahash_request_set_crypt(req, &sg, ipad, keylen);
  892. init_completion(&result.completion);
  893. ret = crypto_ahash_digest(req);
  894. if (ret == -EINPROGRESS) {
  895. wait_for_completion_interruptible(&result.completion);
  896. ret = result.error;
  897. }
  898. /* Set the memory region to 0 to avoid any leak. */
  899. memset(keydup, 0, keylen);
  900. kfree(keydup);
  901. if (ret)
  902. return ret;
  903. keylen = crypto_ahash_digestsize(crypto_ahash_reqtfm(req));
  904. }
  905. memset(ipad + keylen, 0, blocksize - keylen);
  906. memcpy(opad, ipad, blocksize);
  907. for (i = 0; i < blocksize; i++) {
  908. ipad[i] ^= 0x36;
  909. opad[i] ^= 0x5c;
  910. }
  911. return 0;
  912. }
  913. static int mv_cesa_ahmac_setkey(const char *hash_alg_name,
  914. const u8 *key, unsigned int keylen,
  915. void *istate, void *ostate)
  916. {
  917. struct ahash_request *req;
  918. struct crypto_ahash *tfm;
  919. unsigned int blocksize;
  920. u8 *ipad = NULL;
  921. u8 *opad;
  922. int ret;
  923. tfm = crypto_alloc_ahash(hash_alg_name, CRYPTO_ALG_TYPE_AHASH,
  924. CRYPTO_ALG_TYPE_AHASH_MASK);
  925. if (IS_ERR(tfm))
  926. return PTR_ERR(tfm);
  927. req = ahash_request_alloc(tfm, GFP_KERNEL);
  928. if (!req) {
  929. ret = -ENOMEM;
  930. goto free_ahash;
  931. }
  932. crypto_ahash_clear_flags(tfm, ~0);
  933. blocksize = crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
  934. ipad = kzalloc(2 * blocksize, GFP_KERNEL);
  935. if (!ipad) {
  936. ret = -ENOMEM;
  937. goto free_req;
  938. }
  939. opad = ipad + blocksize;
  940. ret = mv_cesa_ahmac_pad_init(req, key, keylen, ipad, opad, blocksize);
  941. if (ret)
  942. goto free_ipad;
  943. ret = mv_cesa_ahmac_iv_state_init(req, ipad, istate, blocksize);
  944. if (ret)
  945. goto free_ipad;
  946. ret = mv_cesa_ahmac_iv_state_init(req, opad, ostate, blocksize);
  947. free_ipad:
  948. kfree(ipad);
  949. free_req:
  950. ahash_request_free(req);
  951. free_ahash:
  952. crypto_free_ahash(tfm);
  953. return ret;
  954. }
  955. static int mv_cesa_ahmac_cra_init(struct crypto_tfm *tfm)
  956. {
  957. struct mv_cesa_hmac_ctx *ctx = crypto_tfm_ctx(tfm);
  958. ctx->base.ops = &mv_cesa_ahash_req_ops;
  959. crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
  960. sizeof(struct mv_cesa_ahash_req));
  961. return 0;
  962. }
  963. static int mv_cesa_ahmac_md5_init(struct ahash_request *req)
  964. {
  965. struct mv_cesa_hmac_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  966. struct mv_cesa_op_ctx tmpl = { };
  967. mv_cesa_set_op_cfg(&tmpl, CESA_SA_DESC_CFG_MACM_HMAC_MD5);
  968. memcpy(tmpl.ctx.hash.iv, ctx->iv, sizeof(ctx->iv));
  969. mv_cesa_ahash_init(req, &tmpl, true);
  970. return 0;
  971. }
  972. static int mv_cesa_ahmac_md5_setkey(struct crypto_ahash *tfm, const u8 *key,
  973. unsigned int keylen)
  974. {
  975. struct mv_cesa_hmac_ctx *ctx = crypto_tfm_ctx(crypto_ahash_tfm(tfm));
  976. struct md5_state istate, ostate;
  977. int ret, i;
  978. ret = mv_cesa_ahmac_setkey("mv-md5", key, keylen, &istate, &ostate);
  979. if (ret)
  980. return ret;
  981. for (i = 0; i < ARRAY_SIZE(istate.hash); i++)
  982. ctx->iv[i] = be32_to_cpu(istate.hash[i]);
  983. for (i = 0; i < ARRAY_SIZE(ostate.hash); i++)
  984. ctx->iv[i + 8] = be32_to_cpu(ostate.hash[i]);
  985. return 0;
  986. }
  987. static int mv_cesa_ahmac_md5_digest(struct ahash_request *req)
  988. {
  989. int ret;
  990. ret = mv_cesa_ahmac_md5_init(req);
  991. if (ret)
  992. return ret;
  993. return mv_cesa_ahash_finup(req);
  994. }
  995. struct ahash_alg mv_ahmac_md5_alg = {
  996. .init = mv_cesa_ahmac_md5_init,
  997. .update = mv_cesa_ahash_update,
  998. .final = mv_cesa_ahash_final,
  999. .finup = mv_cesa_ahash_finup,
  1000. .digest = mv_cesa_ahmac_md5_digest,
  1001. .setkey = mv_cesa_ahmac_md5_setkey,
  1002. .export = mv_cesa_md5_export,
  1003. .import = mv_cesa_md5_import,
  1004. .halg = {
  1005. .digestsize = MD5_DIGEST_SIZE,
  1006. .statesize = sizeof(struct md5_state),
  1007. .base = {
  1008. .cra_name = "hmac(md5)",
  1009. .cra_driver_name = "mv-hmac-md5",
  1010. .cra_priority = 300,
  1011. .cra_flags = CRYPTO_ALG_ASYNC |
  1012. CRYPTO_ALG_KERN_DRIVER_ONLY,
  1013. .cra_blocksize = MD5_HMAC_BLOCK_SIZE,
  1014. .cra_ctxsize = sizeof(struct mv_cesa_hmac_ctx),
  1015. .cra_init = mv_cesa_ahmac_cra_init,
  1016. .cra_module = THIS_MODULE,
  1017. }
  1018. }
  1019. };
  1020. static int mv_cesa_ahmac_sha1_init(struct ahash_request *req)
  1021. {
  1022. struct mv_cesa_hmac_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  1023. struct mv_cesa_op_ctx tmpl = { };
  1024. mv_cesa_set_op_cfg(&tmpl, CESA_SA_DESC_CFG_MACM_HMAC_SHA1);
  1025. memcpy(tmpl.ctx.hash.iv, ctx->iv, sizeof(ctx->iv));
  1026. mv_cesa_ahash_init(req, &tmpl, false);
  1027. return 0;
  1028. }
  1029. static int mv_cesa_ahmac_sha1_setkey(struct crypto_ahash *tfm, const u8 *key,
  1030. unsigned int keylen)
  1031. {
  1032. struct mv_cesa_hmac_ctx *ctx = crypto_tfm_ctx(crypto_ahash_tfm(tfm));
  1033. struct sha1_state istate, ostate;
  1034. int ret, i;
  1035. ret = mv_cesa_ahmac_setkey("mv-sha1", key, keylen, &istate, &ostate);
  1036. if (ret)
  1037. return ret;
  1038. for (i = 0; i < ARRAY_SIZE(istate.state); i++)
  1039. ctx->iv[i] = be32_to_cpu(istate.state[i]);
  1040. for (i = 0; i < ARRAY_SIZE(ostate.state); i++)
  1041. ctx->iv[i + 8] = be32_to_cpu(ostate.state[i]);
  1042. return 0;
  1043. }
  1044. static int mv_cesa_ahmac_sha1_digest(struct ahash_request *req)
  1045. {
  1046. int ret;
  1047. ret = mv_cesa_ahmac_sha1_init(req);
  1048. if (ret)
  1049. return ret;
  1050. return mv_cesa_ahash_finup(req);
  1051. }
  1052. struct ahash_alg mv_ahmac_sha1_alg = {
  1053. .init = mv_cesa_ahmac_sha1_init,
  1054. .update = mv_cesa_ahash_update,
  1055. .final = mv_cesa_ahash_final,
  1056. .finup = mv_cesa_ahash_finup,
  1057. .digest = mv_cesa_ahmac_sha1_digest,
  1058. .setkey = mv_cesa_ahmac_sha1_setkey,
  1059. .export = mv_cesa_sha1_export,
  1060. .import = mv_cesa_sha1_import,
  1061. .halg = {
  1062. .digestsize = SHA1_DIGEST_SIZE,
  1063. .statesize = sizeof(struct sha1_state),
  1064. .base = {
  1065. .cra_name = "hmac(sha1)",
  1066. .cra_driver_name = "mv-hmac-sha1",
  1067. .cra_priority = 300,
  1068. .cra_flags = CRYPTO_ALG_ASYNC |
  1069. CRYPTO_ALG_KERN_DRIVER_ONLY,
  1070. .cra_blocksize = SHA1_BLOCK_SIZE,
  1071. .cra_ctxsize = sizeof(struct mv_cesa_hmac_ctx),
  1072. .cra_init = mv_cesa_ahmac_cra_init,
  1073. .cra_module = THIS_MODULE,
  1074. }
  1075. }
  1076. };
  1077. static int mv_cesa_ahmac_sha256_setkey(struct crypto_ahash *tfm, const u8 *key,
  1078. unsigned int keylen)
  1079. {
  1080. struct mv_cesa_hmac_ctx *ctx = crypto_tfm_ctx(crypto_ahash_tfm(tfm));
  1081. struct sha256_state istate, ostate;
  1082. int ret, i;
  1083. ret = mv_cesa_ahmac_setkey("mv-sha256", key, keylen, &istate, &ostate);
  1084. if (ret)
  1085. return ret;
  1086. for (i = 0; i < ARRAY_SIZE(istate.state); i++)
  1087. ctx->iv[i] = be32_to_cpu(istate.state[i]);
  1088. for (i = 0; i < ARRAY_SIZE(ostate.state); i++)
  1089. ctx->iv[i + 8] = be32_to_cpu(ostate.state[i]);
  1090. return 0;
  1091. }
  1092. static int mv_cesa_ahmac_sha256_init(struct ahash_request *req)
  1093. {
  1094. struct mv_cesa_hmac_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  1095. struct mv_cesa_op_ctx tmpl = { };
  1096. mv_cesa_set_op_cfg(&tmpl, CESA_SA_DESC_CFG_MACM_HMAC_SHA256);
  1097. memcpy(tmpl.ctx.hash.iv, ctx->iv, sizeof(ctx->iv));
  1098. mv_cesa_ahash_init(req, &tmpl, false);
  1099. return 0;
  1100. }
  1101. static int mv_cesa_ahmac_sha256_digest(struct ahash_request *req)
  1102. {
  1103. int ret;
  1104. ret = mv_cesa_ahmac_sha256_init(req);
  1105. if (ret)
  1106. return ret;
  1107. return mv_cesa_ahash_finup(req);
  1108. }
  1109. struct ahash_alg mv_ahmac_sha256_alg = {
  1110. .init = mv_cesa_ahmac_sha256_init,
  1111. .update = mv_cesa_ahash_update,
  1112. .final = mv_cesa_ahash_final,
  1113. .finup = mv_cesa_ahash_finup,
  1114. .digest = mv_cesa_ahmac_sha256_digest,
  1115. .setkey = mv_cesa_ahmac_sha256_setkey,
  1116. .export = mv_cesa_sha256_export,
  1117. .import = mv_cesa_sha256_import,
  1118. .halg = {
  1119. .digestsize = SHA256_DIGEST_SIZE,
  1120. .statesize = sizeof(struct sha256_state),
  1121. .base = {
  1122. .cra_name = "hmac(sha256)",
  1123. .cra_driver_name = "mv-hmac-sha256",
  1124. .cra_priority = 300,
  1125. .cra_flags = CRYPTO_ALG_ASYNC |
  1126. CRYPTO_ALG_KERN_DRIVER_ONLY,
  1127. .cra_blocksize = SHA256_BLOCK_SIZE,
  1128. .cra_ctxsize = sizeof(struct mv_cesa_hmac_ctx),
  1129. .cra_init = mv_cesa_ahmac_cra_init,
  1130. .cra_module = THIS_MODULE,
  1131. }
  1132. }
  1133. };