hash.c 36 KB

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