safexcel_hash.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2017 Marvell
  4. *
  5. * Antoine Tenart <antoine.tenart@free-electrons.com>
  6. */
  7. #include <crypto/hmac.h>
  8. #include <crypto/md5.h>
  9. #include <crypto/sha.h>
  10. #include <linux/device.h>
  11. #include <linux/dma-mapping.h>
  12. #include <linux/dmapool.h>
  13. #include "safexcel.h"
  14. struct safexcel_ahash_ctx {
  15. struct safexcel_context base;
  16. struct safexcel_crypto_priv *priv;
  17. u32 alg;
  18. u32 ipad[SHA512_DIGEST_SIZE / sizeof(u32)];
  19. u32 opad[SHA512_DIGEST_SIZE / sizeof(u32)];
  20. };
  21. struct safexcel_ahash_req {
  22. bool last_req;
  23. bool finish;
  24. bool hmac;
  25. bool needs_inv;
  26. int nents;
  27. dma_addr_t result_dma;
  28. u32 digest;
  29. u8 state_sz; /* expected sate size, only set once */
  30. u32 state[SHA512_DIGEST_SIZE / sizeof(u32)] __aligned(sizeof(u32));
  31. u64 len[2];
  32. u64 processed[2];
  33. u8 cache[SHA512_BLOCK_SIZE] __aligned(sizeof(u32));
  34. dma_addr_t cache_dma;
  35. unsigned int cache_sz;
  36. u8 cache_next[SHA512_BLOCK_SIZE] __aligned(sizeof(u32));
  37. };
  38. static inline u64 safexcel_queued_len(struct safexcel_ahash_req *req)
  39. {
  40. if (req->len[1] > req->processed[1])
  41. return 0xffffffff - (req->len[0] - req->processed[0]);
  42. return req->len[0] - req->processed[0];
  43. }
  44. static void safexcel_hash_token(struct safexcel_command_desc *cdesc,
  45. u32 input_length, u32 result_length)
  46. {
  47. struct safexcel_token *token =
  48. (struct safexcel_token *)cdesc->control_data.token;
  49. token[0].opcode = EIP197_TOKEN_OPCODE_DIRECTION;
  50. token[0].packet_length = input_length;
  51. token[0].stat = EIP197_TOKEN_STAT_LAST_HASH;
  52. token[0].instructions = EIP197_TOKEN_INS_TYPE_HASH;
  53. token[1].opcode = EIP197_TOKEN_OPCODE_INSERT;
  54. token[1].packet_length = result_length;
  55. token[1].stat = EIP197_TOKEN_STAT_LAST_HASH |
  56. EIP197_TOKEN_STAT_LAST_PACKET;
  57. token[1].instructions = EIP197_TOKEN_INS_TYPE_OUTPUT |
  58. EIP197_TOKEN_INS_INSERT_HASH_DIGEST;
  59. }
  60. static void safexcel_context_control(struct safexcel_ahash_ctx *ctx,
  61. struct safexcel_ahash_req *req,
  62. struct safexcel_command_desc *cdesc,
  63. unsigned int digestsize)
  64. {
  65. struct safexcel_crypto_priv *priv = ctx->priv;
  66. int i;
  67. cdesc->control_data.control0 |= CONTEXT_CONTROL_TYPE_HASH_OUT;
  68. cdesc->control_data.control0 |= ctx->alg;
  69. cdesc->control_data.control0 |= req->digest;
  70. if (req->digest == CONTEXT_CONTROL_DIGEST_PRECOMPUTED) {
  71. if (req->processed[0] || req->processed[1]) {
  72. if (ctx->alg == CONTEXT_CONTROL_CRYPTO_ALG_MD5)
  73. cdesc->control_data.control0 |= CONTEXT_CONTROL_SIZE(5);
  74. else if (ctx->alg == CONTEXT_CONTROL_CRYPTO_ALG_SHA1)
  75. cdesc->control_data.control0 |= CONTEXT_CONTROL_SIZE(6);
  76. else if (ctx->alg == CONTEXT_CONTROL_CRYPTO_ALG_SHA224 ||
  77. ctx->alg == CONTEXT_CONTROL_CRYPTO_ALG_SHA256)
  78. cdesc->control_data.control0 |= CONTEXT_CONTROL_SIZE(9);
  79. else if (ctx->alg == CONTEXT_CONTROL_CRYPTO_ALG_SHA384 ||
  80. ctx->alg == CONTEXT_CONTROL_CRYPTO_ALG_SHA512)
  81. cdesc->control_data.control0 |= CONTEXT_CONTROL_SIZE(17);
  82. cdesc->control_data.control1 |= CONTEXT_CONTROL_DIGEST_CNT;
  83. } else {
  84. cdesc->control_data.control0 |= CONTEXT_CONTROL_RESTART_HASH;
  85. }
  86. if (!req->finish)
  87. cdesc->control_data.control0 |= CONTEXT_CONTROL_NO_FINISH_HASH;
  88. /*
  89. * Copy the input digest if needed, and setup the context
  90. * fields. Do this now as we need it to setup the first command
  91. * descriptor.
  92. */
  93. if (req->processed[0] || req->processed[1]) {
  94. for (i = 0; i < digestsize / sizeof(u32); i++)
  95. ctx->base.ctxr->data[i] = cpu_to_le32(req->state[i]);
  96. if (req->finish) {
  97. u64 count = req->processed[0] / EIP197_COUNTER_BLOCK_SIZE;
  98. count += ((0xffffffff / EIP197_COUNTER_BLOCK_SIZE) *
  99. req->processed[1]);
  100. /* This is a haredware limitation, as the
  101. * counter must fit into an u32. This represents
  102. * a farily big amount of input data, so we
  103. * shouldn't see this.
  104. */
  105. if (unlikely(count & 0xffff0000)) {
  106. dev_warn(priv->dev,
  107. "Input data is too big\n");
  108. return;
  109. }
  110. ctx->base.ctxr->data[i] = cpu_to_le32(count);
  111. }
  112. }
  113. } else if (req->digest == CONTEXT_CONTROL_DIGEST_HMAC) {
  114. cdesc->control_data.control0 |= CONTEXT_CONTROL_SIZE(2 * req->state_sz / sizeof(u32));
  115. memcpy(ctx->base.ctxr->data, ctx->ipad, req->state_sz);
  116. memcpy(ctx->base.ctxr->data + req->state_sz / sizeof(u32),
  117. ctx->opad, req->state_sz);
  118. }
  119. }
  120. static int safexcel_handle_req_result(struct safexcel_crypto_priv *priv, int ring,
  121. struct crypto_async_request *async,
  122. bool *should_complete, int *ret)
  123. {
  124. struct safexcel_result_desc *rdesc;
  125. struct ahash_request *areq = ahash_request_cast(async);
  126. struct crypto_ahash *ahash = crypto_ahash_reqtfm(areq);
  127. struct safexcel_ahash_req *sreq = ahash_request_ctx(areq);
  128. u64 cache_len;
  129. *ret = 0;
  130. rdesc = safexcel_ring_next_rptr(priv, &priv->ring[ring].rdr);
  131. if (IS_ERR(rdesc)) {
  132. dev_err(priv->dev,
  133. "hash: result: could not retrieve the result descriptor\n");
  134. *ret = PTR_ERR(rdesc);
  135. } else {
  136. *ret = safexcel_rdesc_check_errors(priv, rdesc);
  137. }
  138. safexcel_complete(priv, ring);
  139. if (sreq->nents) {
  140. dma_unmap_sg(priv->dev, areq->src, sreq->nents, DMA_TO_DEVICE);
  141. sreq->nents = 0;
  142. }
  143. if (sreq->result_dma) {
  144. dma_unmap_single(priv->dev, sreq->result_dma, sreq->state_sz,
  145. DMA_FROM_DEVICE);
  146. sreq->result_dma = 0;
  147. }
  148. if (sreq->cache_dma) {
  149. dma_unmap_single(priv->dev, sreq->cache_dma, sreq->cache_sz,
  150. DMA_TO_DEVICE);
  151. sreq->cache_dma = 0;
  152. }
  153. if (sreq->finish)
  154. memcpy(areq->result, sreq->state,
  155. crypto_ahash_digestsize(ahash));
  156. cache_len = safexcel_queued_len(sreq);
  157. if (cache_len)
  158. memcpy(sreq->cache, sreq->cache_next, cache_len);
  159. *should_complete = true;
  160. return 1;
  161. }
  162. static int safexcel_ahash_send_req(struct crypto_async_request *async, int ring,
  163. int *commands, int *results)
  164. {
  165. struct ahash_request *areq = ahash_request_cast(async);
  166. struct crypto_ahash *ahash = crypto_ahash_reqtfm(areq);
  167. struct safexcel_ahash_req *req = ahash_request_ctx(areq);
  168. struct safexcel_ahash_ctx *ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(areq));
  169. struct safexcel_crypto_priv *priv = ctx->priv;
  170. struct safexcel_command_desc *cdesc, *first_cdesc = NULL;
  171. struct safexcel_result_desc *rdesc;
  172. struct scatterlist *sg;
  173. int i, extra, n_cdesc = 0, ret = 0;
  174. u64 queued, len, cache_len;
  175. queued = len = safexcel_queued_len(req);
  176. if (queued <= crypto_ahash_blocksize(ahash))
  177. cache_len = queued;
  178. else
  179. cache_len = queued - areq->nbytes;
  180. if (!req->last_req) {
  181. /* If this is not the last request and the queued data does not
  182. * fit into full blocks, cache it for the next send() call.
  183. */
  184. extra = queued & (crypto_ahash_blocksize(ahash) - 1);
  185. if (!extra)
  186. /* If this is not the last request and the queued data
  187. * is a multiple of a block, cache the last one for now.
  188. */
  189. extra = crypto_ahash_blocksize(ahash);
  190. if (extra) {
  191. sg_pcopy_to_buffer(areq->src, sg_nents(areq->src),
  192. req->cache_next, extra,
  193. areq->nbytes - extra);
  194. queued -= extra;
  195. len -= extra;
  196. if (!queued) {
  197. *commands = 0;
  198. *results = 0;
  199. return 0;
  200. }
  201. }
  202. }
  203. /* Add a command descriptor for the cached data, if any */
  204. if (cache_len) {
  205. req->cache_dma = dma_map_single(priv->dev, req->cache,
  206. cache_len, DMA_TO_DEVICE);
  207. if (dma_mapping_error(priv->dev, req->cache_dma))
  208. return -EINVAL;
  209. req->cache_sz = cache_len;
  210. first_cdesc = safexcel_add_cdesc(priv, ring, 1,
  211. (cache_len == len),
  212. req->cache_dma, cache_len, len,
  213. ctx->base.ctxr_dma);
  214. if (IS_ERR(first_cdesc)) {
  215. ret = PTR_ERR(first_cdesc);
  216. goto unmap_cache;
  217. }
  218. n_cdesc++;
  219. queued -= cache_len;
  220. if (!queued)
  221. goto send_command;
  222. }
  223. /* Now handle the current ahash request buffer(s) */
  224. req->nents = dma_map_sg(priv->dev, areq->src,
  225. sg_nents_for_len(areq->src, areq->nbytes),
  226. DMA_TO_DEVICE);
  227. if (!req->nents) {
  228. ret = -ENOMEM;
  229. goto cdesc_rollback;
  230. }
  231. for_each_sg(areq->src, sg, req->nents, i) {
  232. int sglen = sg_dma_len(sg);
  233. /* Do not overflow the request */
  234. if (queued < sglen)
  235. sglen = queued;
  236. cdesc = safexcel_add_cdesc(priv, ring, !n_cdesc,
  237. !(queued - sglen), sg_dma_address(sg),
  238. sglen, len, ctx->base.ctxr_dma);
  239. if (IS_ERR(cdesc)) {
  240. ret = PTR_ERR(cdesc);
  241. goto unmap_sg;
  242. }
  243. n_cdesc++;
  244. if (n_cdesc == 1)
  245. first_cdesc = cdesc;
  246. queued -= sglen;
  247. if (!queued)
  248. break;
  249. }
  250. send_command:
  251. /* Setup the context options */
  252. safexcel_context_control(ctx, req, first_cdesc, req->state_sz);
  253. /* Add the token */
  254. safexcel_hash_token(first_cdesc, len, req->state_sz);
  255. req->result_dma = dma_map_single(priv->dev, req->state, req->state_sz,
  256. DMA_FROM_DEVICE);
  257. if (dma_mapping_error(priv->dev, req->result_dma)) {
  258. ret = -EINVAL;
  259. goto unmap_sg;
  260. }
  261. /* Add a result descriptor */
  262. rdesc = safexcel_add_rdesc(priv, ring, 1, 1, req->result_dma,
  263. req->state_sz);
  264. if (IS_ERR(rdesc)) {
  265. ret = PTR_ERR(rdesc);
  266. goto unmap_result;
  267. }
  268. safexcel_rdr_req_set(priv, ring, rdesc, &areq->base);
  269. req->processed[0] += len;
  270. if (req->processed[0] < len)
  271. req->processed[1]++;
  272. *commands = n_cdesc;
  273. *results = 1;
  274. return 0;
  275. unmap_result:
  276. dma_unmap_single(priv->dev, req->result_dma, req->state_sz,
  277. DMA_FROM_DEVICE);
  278. unmap_sg:
  279. dma_unmap_sg(priv->dev, areq->src, req->nents, DMA_TO_DEVICE);
  280. cdesc_rollback:
  281. for (i = 0; i < n_cdesc; i++)
  282. safexcel_ring_rollback_wptr(priv, &priv->ring[ring].cdr);
  283. unmap_cache:
  284. if (req->cache_dma) {
  285. dma_unmap_single(priv->dev, req->cache_dma, req->cache_sz,
  286. DMA_TO_DEVICE);
  287. req->cache_sz = 0;
  288. }
  289. return ret;
  290. }
  291. static inline bool safexcel_ahash_needs_inv_get(struct ahash_request *areq)
  292. {
  293. struct safexcel_ahash_ctx *ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(areq));
  294. struct safexcel_ahash_req *req = ahash_request_ctx(areq);
  295. unsigned int state_w_sz = req->state_sz / sizeof(u32);
  296. u64 processed;
  297. int i;
  298. processed = req->processed[0] / EIP197_COUNTER_BLOCK_SIZE;
  299. processed += (0xffffffff / EIP197_COUNTER_BLOCK_SIZE) * req->processed[1];
  300. for (i = 0; i < state_w_sz; i++)
  301. if (ctx->base.ctxr->data[i] != cpu_to_le32(req->state[i]))
  302. return true;
  303. if (ctx->base.ctxr->data[state_w_sz] != cpu_to_le32(processed))
  304. return true;
  305. return false;
  306. }
  307. static int safexcel_handle_inv_result(struct safexcel_crypto_priv *priv,
  308. int ring,
  309. struct crypto_async_request *async,
  310. bool *should_complete, int *ret)
  311. {
  312. struct safexcel_result_desc *rdesc;
  313. struct ahash_request *areq = ahash_request_cast(async);
  314. struct crypto_ahash *ahash = crypto_ahash_reqtfm(areq);
  315. struct safexcel_ahash_ctx *ctx = crypto_ahash_ctx(ahash);
  316. int enq_ret;
  317. *ret = 0;
  318. rdesc = safexcel_ring_next_rptr(priv, &priv->ring[ring].rdr);
  319. if (IS_ERR(rdesc)) {
  320. dev_err(priv->dev,
  321. "hash: invalidate: could not retrieve the result descriptor\n");
  322. *ret = PTR_ERR(rdesc);
  323. } else {
  324. *ret = safexcel_rdesc_check_errors(priv, rdesc);
  325. }
  326. safexcel_complete(priv, ring);
  327. if (ctx->base.exit_inv) {
  328. dma_pool_free(priv->context_pool, ctx->base.ctxr,
  329. ctx->base.ctxr_dma);
  330. *should_complete = true;
  331. return 1;
  332. }
  333. ring = safexcel_select_ring(priv);
  334. ctx->base.ring = ring;
  335. spin_lock_bh(&priv->ring[ring].queue_lock);
  336. enq_ret = crypto_enqueue_request(&priv->ring[ring].queue, async);
  337. spin_unlock_bh(&priv->ring[ring].queue_lock);
  338. if (enq_ret != -EINPROGRESS)
  339. *ret = enq_ret;
  340. queue_work(priv->ring[ring].workqueue,
  341. &priv->ring[ring].work_data.work);
  342. *should_complete = false;
  343. return 1;
  344. }
  345. static int safexcel_handle_result(struct safexcel_crypto_priv *priv, int ring,
  346. struct crypto_async_request *async,
  347. bool *should_complete, int *ret)
  348. {
  349. struct ahash_request *areq = ahash_request_cast(async);
  350. struct safexcel_ahash_req *req = ahash_request_ctx(areq);
  351. int err;
  352. BUG_ON(!(priv->flags & EIP197_TRC_CACHE) && req->needs_inv);
  353. if (req->needs_inv) {
  354. req->needs_inv = false;
  355. err = safexcel_handle_inv_result(priv, ring, async,
  356. should_complete, ret);
  357. } else {
  358. err = safexcel_handle_req_result(priv, ring, async,
  359. should_complete, ret);
  360. }
  361. return err;
  362. }
  363. static int safexcel_ahash_send_inv(struct crypto_async_request *async,
  364. int ring, int *commands, int *results)
  365. {
  366. struct ahash_request *areq = ahash_request_cast(async);
  367. struct safexcel_ahash_ctx *ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(areq));
  368. int ret;
  369. ret = safexcel_invalidate_cache(async, ctx->priv,
  370. ctx->base.ctxr_dma, ring);
  371. if (unlikely(ret))
  372. return ret;
  373. *commands = 1;
  374. *results = 1;
  375. return 0;
  376. }
  377. static int safexcel_ahash_send(struct crypto_async_request *async,
  378. int ring, int *commands, int *results)
  379. {
  380. struct ahash_request *areq = ahash_request_cast(async);
  381. struct safexcel_ahash_req *req = ahash_request_ctx(areq);
  382. int ret;
  383. if (req->needs_inv)
  384. ret = safexcel_ahash_send_inv(async, ring, commands, results);
  385. else
  386. ret = safexcel_ahash_send_req(async, ring, commands, results);
  387. return ret;
  388. }
  389. static int safexcel_ahash_exit_inv(struct crypto_tfm *tfm)
  390. {
  391. struct safexcel_ahash_ctx *ctx = crypto_tfm_ctx(tfm);
  392. struct safexcel_crypto_priv *priv = ctx->priv;
  393. EIP197_REQUEST_ON_STACK(req, ahash, EIP197_AHASH_REQ_SIZE);
  394. struct safexcel_ahash_req *rctx = ahash_request_ctx(req);
  395. struct safexcel_inv_result result = {};
  396. int ring = ctx->base.ring;
  397. memset(req, 0, sizeof(struct ahash_request));
  398. /* create invalidation request */
  399. init_completion(&result.completion);
  400. ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
  401. safexcel_inv_complete, &result);
  402. ahash_request_set_tfm(req, __crypto_ahash_cast(tfm));
  403. ctx = crypto_tfm_ctx(req->base.tfm);
  404. ctx->base.exit_inv = true;
  405. rctx->needs_inv = true;
  406. spin_lock_bh(&priv->ring[ring].queue_lock);
  407. crypto_enqueue_request(&priv->ring[ring].queue, &req->base);
  408. spin_unlock_bh(&priv->ring[ring].queue_lock);
  409. queue_work(priv->ring[ring].workqueue,
  410. &priv->ring[ring].work_data.work);
  411. wait_for_completion(&result.completion);
  412. if (result.error) {
  413. dev_warn(priv->dev, "hash: completion error (%d)\n",
  414. result.error);
  415. return result.error;
  416. }
  417. return 0;
  418. }
  419. /* safexcel_ahash_cache: cache data until at least one request can be sent to
  420. * the engine, aka. when there is at least 1 block size in the pipe.
  421. */
  422. static int safexcel_ahash_cache(struct ahash_request *areq)
  423. {
  424. struct safexcel_ahash_req *req = ahash_request_ctx(areq);
  425. struct crypto_ahash *ahash = crypto_ahash_reqtfm(areq);
  426. u64 queued, cache_len;
  427. /* queued: everything accepted by the driver which will be handled by
  428. * the next send() calls.
  429. * tot sz handled by update() - tot sz handled by send()
  430. */
  431. queued = safexcel_queued_len(req);
  432. /* cache_len: everything accepted by the driver but not sent yet,
  433. * tot sz handled by update() - last req sz - tot sz handled by send()
  434. */
  435. cache_len = queued - areq->nbytes;
  436. /*
  437. * In case there isn't enough bytes to proceed (less than a
  438. * block size), cache the data until we have enough.
  439. */
  440. if (cache_len + areq->nbytes <= crypto_ahash_blocksize(ahash)) {
  441. sg_pcopy_to_buffer(areq->src, sg_nents(areq->src),
  442. req->cache + cache_len,
  443. areq->nbytes, 0);
  444. return areq->nbytes;
  445. }
  446. /* We couldn't cache all the data */
  447. return -E2BIG;
  448. }
  449. static int safexcel_ahash_enqueue(struct ahash_request *areq)
  450. {
  451. struct safexcel_ahash_ctx *ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(areq));
  452. struct safexcel_ahash_req *req = ahash_request_ctx(areq);
  453. struct safexcel_crypto_priv *priv = ctx->priv;
  454. int ret, ring;
  455. req->needs_inv = false;
  456. if (ctx->base.ctxr) {
  457. if (priv->flags & EIP197_TRC_CACHE && !ctx->base.needs_inv &&
  458. (req->processed[0] || req->processed[1]) &&
  459. req->digest == CONTEXT_CONTROL_DIGEST_PRECOMPUTED)
  460. /* We're still setting needs_inv here, even though it is
  461. * cleared right away, because the needs_inv flag can be
  462. * set in other functions and we want to keep the same
  463. * logic.
  464. */
  465. ctx->base.needs_inv = safexcel_ahash_needs_inv_get(areq);
  466. if (ctx->base.needs_inv) {
  467. ctx->base.needs_inv = false;
  468. req->needs_inv = true;
  469. }
  470. } else {
  471. ctx->base.ring = safexcel_select_ring(priv);
  472. ctx->base.ctxr = dma_pool_zalloc(priv->context_pool,
  473. EIP197_GFP_FLAGS(areq->base),
  474. &ctx->base.ctxr_dma);
  475. if (!ctx->base.ctxr)
  476. return -ENOMEM;
  477. }
  478. ring = ctx->base.ring;
  479. spin_lock_bh(&priv->ring[ring].queue_lock);
  480. ret = crypto_enqueue_request(&priv->ring[ring].queue, &areq->base);
  481. spin_unlock_bh(&priv->ring[ring].queue_lock);
  482. queue_work(priv->ring[ring].workqueue,
  483. &priv->ring[ring].work_data.work);
  484. return ret;
  485. }
  486. static int safexcel_ahash_update(struct ahash_request *areq)
  487. {
  488. struct safexcel_ahash_req *req = ahash_request_ctx(areq);
  489. struct crypto_ahash *ahash = crypto_ahash_reqtfm(areq);
  490. /* If the request is 0 length, do nothing */
  491. if (!areq->nbytes)
  492. return 0;
  493. req->len[0] += areq->nbytes;
  494. if (req->len[0] < areq->nbytes)
  495. req->len[1]++;
  496. safexcel_ahash_cache(areq);
  497. /*
  498. * We're not doing partial updates when performing an hmac request.
  499. * Everything will be handled by the final() call.
  500. */
  501. if (req->digest == CONTEXT_CONTROL_DIGEST_HMAC)
  502. return 0;
  503. if (req->hmac)
  504. return safexcel_ahash_enqueue(areq);
  505. if (!req->last_req &&
  506. safexcel_queued_len(req) > crypto_ahash_blocksize(ahash))
  507. return safexcel_ahash_enqueue(areq);
  508. return 0;
  509. }
  510. static int safexcel_ahash_final(struct ahash_request *areq)
  511. {
  512. struct safexcel_ahash_req *req = ahash_request_ctx(areq);
  513. struct safexcel_ahash_ctx *ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(areq));
  514. req->last_req = true;
  515. req->finish = true;
  516. /* If we have an overall 0 length request */
  517. if (!req->len[0] && !req->len[1] && !areq->nbytes) {
  518. if (ctx->alg == CONTEXT_CONTROL_CRYPTO_ALG_MD5)
  519. memcpy(areq->result, md5_zero_message_hash,
  520. MD5_DIGEST_SIZE);
  521. else if (ctx->alg == CONTEXT_CONTROL_CRYPTO_ALG_SHA1)
  522. memcpy(areq->result, sha1_zero_message_hash,
  523. SHA1_DIGEST_SIZE);
  524. else if (ctx->alg == CONTEXT_CONTROL_CRYPTO_ALG_SHA224)
  525. memcpy(areq->result, sha224_zero_message_hash,
  526. SHA224_DIGEST_SIZE);
  527. else if (ctx->alg == CONTEXT_CONTROL_CRYPTO_ALG_SHA256)
  528. memcpy(areq->result, sha256_zero_message_hash,
  529. SHA256_DIGEST_SIZE);
  530. else if (ctx->alg == CONTEXT_CONTROL_CRYPTO_ALG_SHA384)
  531. memcpy(areq->result, sha384_zero_message_hash,
  532. SHA384_DIGEST_SIZE);
  533. else if (ctx->alg == CONTEXT_CONTROL_CRYPTO_ALG_SHA512)
  534. memcpy(areq->result, sha512_zero_message_hash,
  535. SHA512_DIGEST_SIZE);
  536. return 0;
  537. }
  538. return safexcel_ahash_enqueue(areq);
  539. }
  540. static int safexcel_ahash_finup(struct ahash_request *areq)
  541. {
  542. struct safexcel_ahash_req *req = ahash_request_ctx(areq);
  543. req->last_req = true;
  544. req->finish = true;
  545. safexcel_ahash_update(areq);
  546. return safexcel_ahash_final(areq);
  547. }
  548. static int safexcel_ahash_export(struct ahash_request *areq, void *out)
  549. {
  550. struct crypto_ahash *ahash = crypto_ahash_reqtfm(areq);
  551. struct safexcel_ahash_req *req = ahash_request_ctx(areq);
  552. struct safexcel_ahash_export_state *export = out;
  553. export->len[0] = req->len[0];
  554. export->len[1] = req->len[1];
  555. export->processed[0] = req->processed[0];
  556. export->processed[1] = req->processed[1];
  557. export->digest = req->digest;
  558. memcpy(export->state, req->state, req->state_sz);
  559. memcpy(export->cache, req->cache, crypto_ahash_blocksize(ahash));
  560. return 0;
  561. }
  562. static int safexcel_ahash_import(struct ahash_request *areq, const void *in)
  563. {
  564. struct crypto_ahash *ahash = crypto_ahash_reqtfm(areq);
  565. struct safexcel_ahash_req *req = ahash_request_ctx(areq);
  566. const struct safexcel_ahash_export_state *export = in;
  567. int ret;
  568. ret = crypto_ahash_init(areq);
  569. if (ret)
  570. return ret;
  571. req->len[0] = export->len[0];
  572. req->len[1] = export->len[1];
  573. req->processed[0] = export->processed[0];
  574. req->processed[1] = export->processed[1];
  575. req->digest = export->digest;
  576. memcpy(req->cache, export->cache, crypto_ahash_blocksize(ahash));
  577. memcpy(req->state, export->state, req->state_sz);
  578. return 0;
  579. }
  580. static int safexcel_ahash_cra_init(struct crypto_tfm *tfm)
  581. {
  582. struct safexcel_ahash_ctx *ctx = crypto_tfm_ctx(tfm);
  583. struct safexcel_alg_template *tmpl =
  584. container_of(__crypto_ahash_alg(tfm->__crt_alg),
  585. struct safexcel_alg_template, alg.ahash);
  586. ctx->priv = tmpl->priv;
  587. ctx->base.send = safexcel_ahash_send;
  588. ctx->base.handle_result = safexcel_handle_result;
  589. crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
  590. sizeof(struct safexcel_ahash_req));
  591. return 0;
  592. }
  593. static int safexcel_sha1_init(struct ahash_request *areq)
  594. {
  595. struct safexcel_ahash_ctx *ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(areq));
  596. struct safexcel_ahash_req *req = ahash_request_ctx(areq);
  597. memset(req, 0, sizeof(*req));
  598. req->state[0] = SHA1_H0;
  599. req->state[1] = SHA1_H1;
  600. req->state[2] = SHA1_H2;
  601. req->state[3] = SHA1_H3;
  602. req->state[4] = SHA1_H4;
  603. ctx->alg = CONTEXT_CONTROL_CRYPTO_ALG_SHA1;
  604. req->digest = CONTEXT_CONTROL_DIGEST_PRECOMPUTED;
  605. req->state_sz = SHA1_DIGEST_SIZE;
  606. return 0;
  607. }
  608. static int safexcel_sha1_digest(struct ahash_request *areq)
  609. {
  610. int ret = safexcel_sha1_init(areq);
  611. if (ret)
  612. return ret;
  613. return safexcel_ahash_finup(areq);
  614. }
  615. static void safexcel_ahash_cra_exit(struct crypto_tfm *tfm)
  616. {
  617. struct safexcel_ahash_ctx *ctx = crypto_tfm_ctx(tfm);
  618. struct safexcel_crypto_priv *priv = ctx->priv;
  619. int ret;
  620. /* context not allocated, skip invalidation */
  621. if (!ctx->base.ctxr)
  622. return;
  623. if (priv->flags & EIP197_TRC_CACHE) {
  624. ret = safexcel_ahash_exit_inv(tfm);
  625. if (ret)
  626. dev_warn(priv->dev, "hash: invalidation error %d\n", ret);
  627. } else {
  628. dma_pool_free(priv->context_pool, ctx->base.ctxr,
  629. ctx->base.ctxr_dma);
  630. }
  631. }
  632. struct safexcel_alg_template safexcel_alg_sha1 = {
  633. .type = SAFEXCEL_ALG_TYPE_AHASH,
  634. .engines = EIP97IES | EIP197B | EIP197D,
  635. .alg.ahash = {
  636. .init = safexcel_sha1_init,
  637. .update = safexcel_ahash_update,
  638. .final = safexcel_ahash_final,
  639. .finup = safexcel_ahash_finup,
  640. .digest = safexcel_sha1_digest,
  641. .export = safexcel_ahash_export,
  642. .import = safexcel_ahash_import,
  643. .halg = {
  644. .digestsize = SHA1_DIGEST_SIZE,
  645. .statesize = sizeof(struct safexcel_ahash_export_state),
  646. .base = {
  647. .cra_name = "sha1",
  648. .cra_driver_name = "safexcel-sha1",
  649. .cra_priority = 300,
  650. .cra_flags = CRYPTO_ALG_ASYNC |
  651. CRYPTO_ALG_KERN_DRIVER_ONLY,
  652. .cra_blocksize = SHA1_BLOCK_SIZE,
  653. .cra_ctxsize = sizeof(struct safexcel_ahash_ctx),
  654. .cra_init = safexcel_ahash_cra_init,
  655. .cra_exit = safexcel_ahash_cra_exit,
  656. .cra_module = THIS_MODULE,
  657. },
  658. },
  659. },
  660. };
  661. static int safexcel_hmac_sha1_init(struct ahash_request *areq)
  662. {
  663. struct safexcel_ahash_req *req = ahash_request_ctx(areq);
  664. safexcel_sha1_init(areq);
  665. req->digest = CONTEXT_CONTROL_DIGEST_HMAC;
  666. return 0;
  667. }
  668. static int safexcel_hmac_sha1_digest(struct ahash_request *areq)
  669. {
  670. int ret = safexcel_hmac_sha1_init(areq);
  671. if (ret)
  672. return ret;
  673. return safexcel_ahash_finup(areq);
  674. }
  675. struct safexcel_ahash_result {
  676. struct completion completion;
  677. int error;
  678. };
  679. static void safexcel_ahash_complete(struct crypto_async_request *req, int error)
  680. {
  681. struct safexcel_ahash_result *result = req->data;
  682. if (error == -EINPROGRESS)
  683. return;
  684. result->error = error;
  685. complete(&result->completion);
  686. }
  687. static int safexcel_hmac_init_pad(struct ahash_request *areq,
  688. unsigned int blocksize, const u8 *key,
  689. unsigned int keylen, u8 *ipad, u8 *opad)
  690. {
  691. struct safexcel_ahash_result result;
  692. struct scatterlist sg;
  693. int ret, i;
  694. u8 *keydup;
  695. if (keylen <= blocksize) {
  696. memcpy(ipad, key, keylen);
  697. } else {
  698. keydup = kmemdup(key, keylen, GFP_KERNEL);
  699. if (!keydup)
  700. return -ENOMEM;
  701. ahash_request_set_callback(areq, CRYPTO_TFM_REQ_MAY_BACKLOG,
  702. safexcel_ahash_complete, &result);
  703. sg_init_one(&sg, keydup, keylen);
  704. ahash_request_set_crypt(areq, &sg, ipad, keylen);
  705. init_completion(&result.completion);
  706. ret = crypto_ahash_digest(areq);
  707. if (ret == -EINPROGRESS || ret == -EBUSY) {
  708. wait_for_completion_interruptible(&result.completion);
  709. ret = result.error;
  710. }
  711. /* Avoid leaking */
  712. memzero_explicit(keydup, keylen);
  713. kfree(keydup);
  714. if (ret)
  715. return ret;
  716. keylen = crypto_ahash_digestsize(crypto_ahash_reqtfm(areq));
  717. }
  718. memset(ipad + keylen, 0, blocksize - keylen);
  719. memcpy(opad, ipad, blocksize);
  720. for (i = 0; i < blocksize; i++) {
  721. ipad[i] ^= HMAC_IPAD_VALUE;
  722. opad[i] ^= HMAC_OPAD_VALUE;
  723. }
  724. return 0;
  725. }
  726. static int safexcel_hmac_init_iv(struct ahash_request *areq,
  727. unsigned int blocksize, u8 *pad, void *state)
  728. {
  729. struct safexcel_ahash_result result;
  730. struct safexcel_ahash_req *req;
  731. struct scatterlist sg;
  732. int ret;
  733. ahash_request_set_callback(areq, CRYPTO_TFM_REQ_MAY_BACKLOG,
  734. safexcel_ahash_complete, &result);
  735. sg_init_one(&sg, pad, blocksize);
  736. ahash_request_set_crypt(areq, &sg, pad, blocksize);
  737. init_completion(&result.completion);
  738. ret = crypto_ahash_init(areq);
  739. if (ret)
  740. return ret;
  741. req = ahash_request_ctx(areq);
  742. req->hmac = true;
  743. req->last_req = true;
  744. ret = crypto_ahash_update(areq);
  745. if (ret && ret != -EINPROGRESS && ret != -EBUSY)
  746. return ret;
  747. wait_for_completion_interruptible(&result.completion);
  748. if (result.error)
  749. return result.error;
  750. return crypto_ahash_export(areq, state);
  751. }
  752. int safexcel_hmac_setkey(const char *alg, const u8 *key, unsigned int keylen,
  753. void *istate, void *ostate)
  754. {
  755. struct ahash_request *areq;
  756. struct crypto_ahash *tfm;
  757. unsigned int blocksize;
  758. u8 *ipad, *opad;
  759. int ret;
  760. tfm = crypto_alloc_ahash(alg, 0, 0);
  761. if (IS_ERR(tfm))
  762. return PTR_ERR(tfm);
  763. areq = ahash_request_alloc(tfm, GFP_KERNEL);
  764. if (!areq) {
  765. ret = -ENOMEM;
  766. goto free_ahash;
  767. }
  768. crypto_ahash_clear_flags(tfm, ~0);
  769. blocksize = crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
  770. ipad = kcalloc(2, blocksize, GFP_KERNEL);
  771. if (!ipad) {
  772. ret = -ENOMEM;
  773. goto free_request;
  774. }
  775. opad = ipad + blocksize;
  776. ret = safexcel_hmac_init_pad(areq, blocksize, key, keylen, ipad, opad);
  777. if (ret)
  778. goto free_ipad;
  779. ret = safexcel_hmac_init_iv(areq, blocksize, ipad, istate);
  780. if (ret)
  781. goto free_ipad;
  782. ret = safexcel_hmac_init_iv(areq, blocksize, opad, ostate);
  783. free_ipad:
  784. kfree(ipad);
  785. free_request:
  786. ahash_request_free(areq);
  787. free_ahash:
  788. crypto_free_ahash(tfm);
  789. return ret;
  790. }
  791. static int safexcel_hmac_alg_setkey(struct crypto_ahash *tfm, const u8 *key,
  792. unsigned int keylen, const char *alg,
  793. unsigned int state_sz)
  794. {
  795. struct safexcel_ahash_ctx *ctx = crypto_tfm_ctx(crypto_ahash_tfm(tfm));
  796. struct safexcel_crypto_priv *priv = ctx->priv;
  797. struct safexcel_ahash_export_state istate, ostate;
  798. int ret, i;
  799. ret = safexcel_hmac_setkey(alg, key, keylen, &istate, &ostate);
  800. if (ret)
  801. return ret;
  802. if (priv->flags & EIP197_TRC_CACHE && ctx->base.ctxr) {
  803. for (i = 0; i < state_sz / sizeof(u32); i++) {
  804. if (ctx->ipad[i] != le32_to_cpu(istate.state[i]) ||
  805. ctx->opad[i] != le32_to_cpu(ostate.state[i])) {
  806. ctx->base.needs_inv = true;
  807. break;
  808. }
  809. }
  810. }
  811. memcpy(ctx->ipad, &istate.state, state_sz);
  812. memcpy(ctx->opad, &ostate.state, state_sz);
  813. return 0;
  814. }
  815. static int safexcel_hmac_sha1_setkey(struct crypto_ahash *tfm, const u8 *key,
  816. unsigned int keylen)
  817. {
  818. return safexcel_hmac_alg_setkey(tfm, key, keylen, "safexcel-sha1",
  819. SHA1_DIGEST_SIZE);
  820. }
  821. struct safexcel_alg_template safexcel_alg_hmac_sha1 = {
  822. .type = SAFEXCEL_ALG_TYPE_AHASH,
  823. .engines = EIP97IES | EIP197B | EIP197D,
  824. .alg.ahash = {
  825. .init = safexcel_hmac_sha1_init,
  826. .update = safexcel_ahash_update,
  827. .final = safexcel_ahash_final,
  828. .finup = safexcel_ahash_finup,
  829. .digest = safexcel_hmac_sha1_digest,
  830. .setkey = safexcel_hmac_sha1_setkey,
  831. .export = safexcel_ahash_export,
  832. .import = safexcel_ahash_import,
  833. .halg = {
  834. .digestsize = SHA1_DIGEST_SIZE,
  835. .statesize = sizeof(struct safexcel_ahash_export_state),
  836. .base = {
  837. .cra_name = "hmac(sha1)",
  838. .cra_driver_name = "safexcel-hmac-sha1",
  839. .cra_priority = 300,
  840. .cra_flags = CRYPTO_ALG_ASYNC |
  841. CRYPTO_ALG_KERN_DRIVER_ONLY,
  842. .cra_blocksize = SHA1_BLOCK_SIZE,
  843. .cra_ctxsize = sizeof(struct safexcel_ahash_ctx),
  844. .cra_init = safexcel_ahash_cra_init,
  845. .cra_exit = safexcel_ahash_cra_exit,
  846. .cra_module = THIS_MODULE,
  847. },
  848. },
  849. },
  850. };
  851. static int safexcel_sha256_init(struct ahash_request *areq)
  852. {
  853. struct safexcel_ahash_ctx *ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(areq));
  854. struct safexcel_ahash_req *req = ahash_request_ctx(areq);
  855. memset(req, 0, sizeof(*req));
  856. req->state[0] = SHA256_H0;
  857. req->state[1] = SHA256_H1;
  858. req->state[2] = SHA256_H2;
  859. req->state[3] = SHA256_H3;
  860. req->state[4] = SHA256_H4;
  861. req->state[5] = SHA256_H5;
  862. req->state[6] = SHA256_H6;
  863. req->state[7] = SHA256_H7;
  864. ctx->alg = CONTEXT_CONTROL_CRYPTO_ALG_SHA256;
  865. req->digest = CONTEXT_CONTROL_DIGEST_PRECOMPUTED;
  866. req->state_sz = SHA256_DIGEST_SIZE;
  867. return 0;
  868. }
  869. static int safexcel_sha256_digest(struct ahash_request *areq)
  870. {
  871. int ret = safexcel_sha256_init(areq);
  872. if (ret)
  873. return ret;
  874. return safexcel_ahash_finup(areq);
  875. }
  876. struct safexcel_alg_template safexcel_alg_sha256 = {
  877. .type = SAFEXCEL_ALG_TYPE_AHASH,
  878. .engines = EIP97IES | EIP197B | EIP197D,
  879. .alg.ahash = {
  880. .init = safexcel_sha256_init,
  881. .update = safexcel_ahash_update,
  882. .final = safexcel_ahash_final,
  883. .finup = safexcel_ahash_finup,
  884. .digest = safexcel_sha256_digest,
  885. .export = safexcel_ahash_export,
  886. .import = safexcel_ahash_import,
  887. .halg = {
  888. .digestsize = SHA256_DIGEST_SIZE,
  889. .statesize = sizeof(struct safexcel_ahash_export_state),
  890. .base = {
  891. .cra_name = "sha256",
  892. .cra_driver_name = "safexcel-sha256",
  893. .cra_priority = 300,
  894. .cra_flags = CRYPTO_ALG_ASYNC |
  895. CRYPTO_ALG_KERN_DRIVER_ONLY,
  896. .cra_blocksize = SHA256_BLOCK_SIZE,
  897. .cra_ctxsize = sizeof(struct safexcel_ahash_ctx),
  898. .cra_init = safexcel_ahash_cra_init,
  899. .cra_exit = safexcel_ahash_cra_exit,
  900. .cra_module = THIS_MODULE,
  901. },
  902. },
  903. },
  904. };
  905. static int safexcel_sha224_init(struct ahash_request *areq)
  906. {
  907. struct safexcel_ahash_ctx *ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(areq));
  908. struct safexcel_ahash_req *req = ahash_request_ctx(areq);
  909. memset(req, 0, sizeof(*req));
  910. req->state[0] = SHA224_H0;
  911. req->state[1] = SHA224_H1;
  912. req->state[2] = SHA224_H2;
  913. req->state[3] = SHA224_H3;
  914. req->state[4] = SHA224_H4;
  915. req->state[5] = SHA224_H5;
  916. req->state[6] = SHA224_H6;
  917. req->state[7] = SHA224_H7;
  918. ctx->alg = CONTEXT_CONTROL_CRYPTO_ALG_SHA224;
  919. req->digest = CONTEXT_CONTROL_DIGEST_PRECOMPUTED;
  920. req->state_sz = SHA256_DIGEST_SIZE;
  921. return 0;
  922. }
  923. static int safexcel_sha224_digest(struct ahash_request *areq)
  924. {
  925. int ret = safexcel_sha224_init(areq);
  926. if (ret)
  927. return ret;
  928. return safexcel_ahash_finup(areq);
  929. }
  930. struct safexcel_alg_template safexcel_alg_sha224 = {
  931. .type = SAFEXCEL_ALG_TYPE_AHASH,
  932. .engines = EIP97IES | EIP197B | EIP197D,
  933. .alg.ahash = {
  934. .init = safexcel_sha224_init,
  935. .update = safexcel_ahash_update,
  936. .final = safexcel_ahash_final,
  937. .finup = safexcel_ahash_finup,
  938. .digest = safexcel_sha224_digest,
  939. .export = safexcel_ahash_export,
  940. .import = safexcel_ahash_import,
  941. .halg = {
  942. .digestsize = SHA224_DIGEST_SIZE,
  943. .statesize = sizeof(struct safexcel_ahash_export_state),
  944. .base = {
  945. .cra_name = "sha224",
  946. .cra_driver_name = "safexcel-sha224",
  947. .cra_priority = 300,
  948. .cra_flags = CRYPTO_ALG_ASYNC |
  949. CRYPTO_ALG_KERN_DRIVER_ONLY,
  950. .cra_blocksize = SHA224_BLOCK_SIZE,
  951. .cra_ctxsize = sizeof(struct safexcel_ahash_ctx),
  952. .cra_init = safexcel_ahash_cra_init,
  953. .cra_exit = safexcel_ahash_cra_exit,
  954. .cra_module = THIS_MODULE,
  955. },
  956. },
  957. },
  958. };
  959. static int safexcel_hmac_sha224_setkey(struct crypto_ahash *tfm, const u8 *key,
  960. unsigned int keylen)
  961. {
  962. return safexcel_hmac_alg_setkey(tfm, key, keylen, "safexcel-sha224",
  963. SHA256_DIGEST_SIZE);
  964. }
  965. static int safexcel_hmac_sha224_init(struct ahash_request *areq)
  966. {
  967. struct safexcel_ahash_req *req = ahash_request_ctx(areq);
  968. safexcel_sha224_init(areq);
  969. req->digest = CONTEXT_CONTROL_DIGEST_HMAC;
  970. return 0;
  971. }
  972. static int safexcel_hmac_sha224_digest(struct ahash_request *areq)
  973. {
  974. int ret = safexcel_hmac_sha224_init(areq);
  975. if (ret)
  976. return ret;
  977. return safexcel_ahash_finup(areq);
  978. }
  979. struct safexcel_alg_template safexcel_alg_hmac_sha224 = {
  980. .type = SAFEXCEL_ALG_TYPE_AHASH,
  981. .engines = EIP97IES | EIP197B | EIP197D,
  982. .alg.ahash = {
  983. .init = safexcel_hmac_sha224_init,
  984. .update = safexcel_ahash_update,
  985. .final = safexcel_ahash_final,
  986. .finup = safexcel_ahash_finup,
  987. .digest = safexcel_hmac_sha224_digest,
  988. .setkey = safexcel_hmac_sha224_setkey,
  989. .export = safexcel_ahash_export,
  990. .import = safexcel_ahash_import,
  991. .halg = {
  992. .digestsize = SHA224_DIGEST_SIZE,
  993. .statesize = sizeof(struct safexcel_ahash_export_state),
  994. .base = {
  995. .cra_name = "hmac(sha224)",
  996. .cra_driver_name = "safexcel-hmac-sha224",
  997. .cra_priority = 300,
  998. .cra_flags = CRYPTO_ALG_ASYNC |
  999. CRYPTO_ALG_KERN_DRIVER_ONLY,
  1000. .cra_blocksize = SHA224_BLOCK_SIZE,
  1001. .cra_ctxsize = sizeof(struct safexcel_ahash_ctx),
  1002. .cra_init = safexcel_ahash_cra_init,
  1003. .cra_exit = safexcel_ahash_cra_exit,
  1004. .cra_module = THIS_MODULE,
  1005. },
  1006. },
  1007. },
  1008. };
  1009. static int safexcel_hmac_sha256_setkey(struct crypto_ahash *tfm, const u8 *key,
  1010. unsigned int keylen)
  1011. {
  1012. return safexcel_hmac_alg_setkey(tfm, key, keylen, "safexcel-sha256",
  1013. SHA256_DIGEST_SIZE);
  1014. }
  1015. static int safexcel_hmac_sha256_init(struct ahash_request *areq)
  1016. {
  1017. struct safexcel_ahash_req *req = ahash_request_ctx(areq);
  1018. safexcel_sha256_init(areq);
  1019. req->digest = CONTEXT_CONTROL_DIGEST_HMAC;
  1020. return 0;
  1021. }
  1022. static int safexcel_hmac_sha256_digest(struct ahash_request *areq)
  1023. {
  1024. int ret = safexcel_hmac_sha256_init(areq);
  1025. if (ret)
  1026. return ret;
  1027. return safexcel_ahash_finup(areq);
  1028. }
  1029. struct safexcel_alg_template safexcel_alg_hmac_sha256 = {
  1030. .type = SAFEXCEL_ALG_TYPE_AHASH,
  1031. .engines = EIP97IES | EIP197B | EIP197D,
  1032. .alg.ahash = {
  1033. .init = safexcel_hmac_sha256_init,
  1034. .update = safexcel_ahash_update,
  1035. .final = safexcel_ahash_final,
  1036. .finup = safexcel_ahash_finup,
  1037. .digest = safexcel_hmac_sha256_digest,
  1038. .setkey = safexcel_hmac_sha256_setkey,
  1039. .export = safexcel_ahash_export,
  1040. .import = safexcel_ahash_import,
  1041. .halg = {
  1042. .digestsize = SHA256_DIGEST_SIZE,
  1043. .statesize = sizeof(struct safexcel_ahash_export_state),
  1044. .base = {
  1045. .cra_name = "hmac(sha256)",
  1046. .cra_driver_name = "safexcel-hmac-sha256",
  1047. .cra_priority = 300,
  1048. .cra_flags = CRYPTO_ALG_ASYNC |
  1049. CRYPTO_ALG_KERN_DRIVER_ONLY,
  1050. .cra_blocksize = SHA256_BLOCK_SIZE,
  1051. .cra_ctxsize = sizeof(struct safexcel_ahash_ctx),
  1052. .cra_init = safexcel_ahash_cra_init,
  1053. .cra_exit = safexcel_ahash_cra_exit,
  1054. .cra_module = THIS_MODULE,
  1055. },
  1056. },
  1057. },
  1058. };
  1059. static int safexcel_sha512_init(struct ahash_request *areq)
  1060. {
  1061. struct safexcel_ahash_ctx *ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(areq));
  1062. struct safexcel_ahash_req *req = ahash_request_ctx(areq);
  1063. memset(req, 0, sizeof(*req));
  1064. req->state[0] = lower_32_bits(SHA512_H0);
  1065. req->state[1] = upper_32_bits(SHA512_H0);
  1066. req->state[2] = lower_32_bits(SHA512_H1);
  1067. req->state[3] = upper_32_bits(SHA512_H1);
  1068. req->state[4] = lower_32_bits(SHA512_H2);
  1069. req->state[5] = upper_32_bits(SHA512_H2);
  1070. req->state[6] = lower_32_bits(SHA512_H3);
  1071. req->state[7] = upper_32_bits(SHA512_H3);
  1072. req->state[8] = lower_32_bits(SHA512_H4);
  1073. req->state[9] = upper_32_bits(SHA512_H4);
  1074. req->state[10] = lower_32_bits(SHA512_H5);
  1075. req->state[11] = upper_32_bits(SHA512_H5);
  1076. req->state[12] = lower_32_bits(SHA512_H6);
  1077. req->state[13] = upper_32_bits(SHA512_H6);
  1078. req->state[14] = lower_32_bits(SHA512_H7);
  1079. req->state[15] = upper_32_bits(SHA512_H7);
  1080. ctx->alg = CONTEXT_CONTROL_CRYPTO_ALG_SHA512;
  1081. req->digest = CONTEXT_CONTROL_DIGEST_PRECOMPUTED;
  1082. req->state_sz = SHA512_DIGEST_SIZE;
  1083. return 0;
  1084. }
  1085. static int safexcel_sha512_digest(struct ahash_request *areq)
  1086. {
  1087. int ret = safexcel_sha512_init(areq);
  1088. if (ret)
  1089. return ret;
  1090. return safexcel_ahash_finup(areq);
  1091. }
  1092. struct safexcel_alg_template safexcel_alg_sha512 = {
  1093. .type = SAFEXCEL_ALG_TYPE_AHASH,
  1094. .engines = EIP97IES | EIP197B | EIP197D,
  1095. .alg.ahash = {
  1096. .init = safexcel_sha512_init,
  1097. .update = safexcel_ahash_update,
  1098. .final = safexcel_ahash_final,
  1099. .finup = safexcel_ahash_finup,
  1100. .digest = safexcel_sha512_digest,
  1101. .export = safexcel_ahash_export,
  1102. .import = safexcel_ahash_import,
  1103. .halg = {
  1104. .digestsize = SHA512_DIGEST_SIZE,
  1105. .statesize = sizeof(struct safexcel_ahash_export_state),
  1106. .base = {
  1107. .cra_name = "sha512",
  1108. .cra_driver_name = "safexcel-sha512",
  1109. .cra_priority = 300,
  1110. .cra_flags = CRYPTO_ALG_ASYNC |
  1111. CRYPTO_ALG_KERN_DRIVER_ONLY,
  1112. .cra_blocksize = SHA512_BLOCK_SIZE,
  1113. .cra_ctxsize = sizeof(struct safexcel_ahash_ctx),
  1114. .cra_init = safexcel_ahash_cra_init,
  1115. .cra_exit = safexcel_ahash_cra_exit,
  1116. .cra_module = THIS_MODULE,
  1117. },
  1118. },
  1119. },
  1120. };
  1121. static int safexcel_sha384_init(struct ahash_request *areq)
  1122. {
  1123. struct safexcel_ahash_ctx *ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(areq));
  1124. struct safexcel_ahash_req *req = ahash_request_ctx(areq);
  1125. memset(req, 0, sizeof(*req));
  1126. req->state[0] = lower_32_bits(SHA384_H0);
  1127. req->state[1] = upper_32_bits(SHA384_H0);
  1128. req->state[2] = lower_32_bits(SHA384_H1);
  1129. req->state[3] = upper_32_bits(SHA384_H1);
  1130. req->state[4] = lower_32_bits(SHA384_H2);
  1131. req->state[5] = upper_32_bits(SHA384_H2);
  1132. req->state[6] = lower_32_bits(SHA384_H3);
  1133. req->state[7] = upper_32_bits(SHA384_H3);
  1134. req->state[8] = lower_32_bits(SHA384_H4);
  1135. req->state[9] = upper_32_bits(SHA384_H4);
  1136. req->state[10] = lower_32_bits(SHA384_H5);
  1137. req->state[11] = upper_32_bits(SHA384_H5);
  1138. req->state[12] = lower_32_bits(SHA384_H6);
  1139. req->state[13] = upper_32_bits(SHA384_H6);
  1140. req->state[14] = lower_32_bits(SHA384_H7);
  1141. req->state[15] = upper_32_bits(SHA384_H7);
  1142. ctx->alg = CONTEXT_CONTROL_CRYPTO_ALG_SHA384;
  1143. req->digest = CONTEXT_CONTROL_DIGEST_PRECOMPUTED;
  1144. req->state_sz = SHA512_DIGEST_SIZE;
  1145. return 0;
  1146. }
  1147. static int safexcel_sha384_digest(struct ahash_request *areq)
  1148. {
  1149. int ret = safexcel_sha384_init(areq);
  1150. if (ret)
  1151. return ret;
  1152. return safexcel_ahash_finup(areq);
  1153. }
  1154. struct safexcel_alg_template safexcel_alg_sha384 = {
  1155. .type = SAFEXCEL_ALG_TYPE_AHASH,
  1156. .engines = EIP97IES | EIP197B | EIP197D,
  1157. .alg.ahash = {
  1158. .init = safexcel_sha384_init,
  1159. .update = safexcel_ahash_update,
  1160. .final = safexcel_ahash_final,
  1161. .finup = safexcel_ahash_finup,
  1162. .digest = safexcel_sha384_digest,
  1163. .export = safexcel_ahash_export,
  1164. .import = safexcel_ahash_import,
  1165. .halg = {
  1166. .digestsize = SHA384_DIGEST_SIZE,
  1167. .statesize = sizeof(struct safexcel_ahash_export_state),
  1168. .base = {
  1169. .cra_name = "sha384",
  1170. .cra_driver_name = "safexcel-sha384",
  1171. .cra_priority = 300,
  1172. .cra_flags = CRYPTO_ALG_ASYNC |
  1173. CRYPTO_ALG_KERN_DRIVER_ONLY,
  1174. .cra_blocksize = SHA384_BLOCK_SIZE,
  1175. .cra_ctxsize = sizeof(struct safexcel_ahash_ctx),
  1176. .cra_init = safexcel_ahash_cra_init,
  1177. .cra_exit = safexcel_ahash_cra_exit,
  1178. .cra_module = THIS_MODULE,
  1179. },
  1180. },
  1181. },
  1182. };
  1183. static int safexcel_hmac_sha512_setkey(struct crypto_ahash *tfm, const u8 *key,
  1184. unsigned int keylen)
  1185. {
  1186. return safexcel_hmac_alg_setkey(tfm, key, keylen, "safexcel-sha512",
  1187. SHA512_DIGEST_SIZE);
  1188. }
  1189. static int safexcel_hmac_sha512_init(struct ahash_request *areq)
  1190. {
  1191. struct safexcel_ahash_req *req = ahash_request_ctx(areq);
  1192. safexcel_sha512_init(areq);
  1193. req->digest = CONTEXT_CONTROL_DIGEST_HMAC;
  1194. return 0;
  1195. }
  1196. static int safexcel_hmac_sha512_digest(struct ahash_request *areq)
  1197. {
  1198. int ret = safexcel_hmac_sha512_init(areq);
  1199. if (ret)
  1200. return ret;
  1201. return safexcel_ahash_finup(areq);
  1202. }
  1203. struct safexcel_alg_template safexcel_alg_hmac_sha512 = {
  1204. .type = SAFEXCEL_ALG_TYPE_AHASH,
  1205. .engines = EIP97IES | EIP197B | EIP197D,
  1206. .alg.ahash = {
  1207. .init = safexcel_hmac_sha512_init,
  1208. .update = safexcel_ahash_update,
  1209. .final = safexcel_ahash_final,
  1210. .finup = safexcel_ahash_finup,
  1211. .digest = safexcel_hmac_sha512_digest,
  1212. .setkey = safexcel_hmac_sha512_setkey,
  1213. .export = safexcel_ahash_export,
  1214. .import = safexcel_ahash_import,
  1215. .halg = {
  1216. .digestsize = SHA512_DIGEST_SIZE,
  1217. .statesize = sizeof(struct safexcel_ahash_export_state),
  1218. .base = {
  1219. .cra_name = "hmac(sha512)",
  1220. .cra_driver_name = "safexcel-hmac-sha512",
  1221. .cra_priority = 300,
  1222. .cra_flags = CRYPTO_ALG_ASYNC |
  1223. CRYPTO_ALG_KERN_DRIVER_ONLY,
  1224. .cra_blocksize = SHA512_BLOCK_SIZE,
  1225. .cra_ctxsize = sizeof(struct safexcel_ahash_ctx),
  1226. .cra_init = safexcel_ahash_cra_init,
  1227. .cra_exit = safexcel_ahash_cra_exit,
  1228. .cra_module = THIS_MODULE,
  1229. },
  1230. },
  1231. },
  1232. };
  1233. static int safexcel_hmac_sha384_setkey(struct crypto_ahash *tfm, const u8 *key,
  1234. unsigned int keylen)
  1235. {
  1236. return safexcel_hmac_alg_setkey(tfm, key, keylen, "safexcel-sha384",
  1237. SHA512_DIGEST_SIZE);
  1238. }
  1239. static int safexcel_hmac_sha384_init(struct ahash_request *areq)
  1240. {
  1241. struct safexcel_ahash_req *req = ahash_request_ctx(areq);
  1242. safexcel_sha384_init(areq);
  1243. req->digest = CONTEXT_CONTROL_DIGEST_HMAC;
  1244. return 0;
  1245. }
  1246. static int safexcel_hmac_sha384_digest(struct ahash_request *areq)
  1247. {
  1248. int ret = safexcel_hmac_sha384_init(areq);
  1249. if (ret)
  1250. return ret;
  1251. return safexcel_ahash_finup(areq);
  1252. }
  1253. struct safexcel_alg_template safexcel_alg_hmac_sha384 = {
  1254. .type = SAFEXCEL_ALG_TYPE_AHASH,
  1255. .engines = EIP97IES | EIP197B | EIP197D,
  1256. .alg.ahash = {
  1257. .init = safexcel_hmac_sha384_init,
  1258. .update = safexcel_ahash_update,
  1259. .final = safexcel_ahash_final,
  1260. .finup = safexcel_ahash_finup,
  1261. .digest = safexcel_hmac_sha384_digest,
  1262. .setkey = safexcel_hmac_sha384_setkey,
  1263. .export = safexcel_ahash_export,
  1264. .import = safexcel_ahash_import,
  1265. .halg = {
  1266. .digestsize = SHA384_DIGEST_SIZE,
  1267. .statesize = sizeof(struct safexcel_ahash_export_state),
  1268. .base = {
  1269. .cra_name = "hmac(sha384)",
  1270. .cra_driver_name = "safexcel-hmac-sha384",
  1271. .cra_priority = 300,
  1272. .cra_flags = CRYPTO_ALG_ASYNC |
  1273. CRYPTO_ALG_KERN_DRIVER_ONLY,
  1274. .cra_blocksize = SHA384_BLOCK_SIZE,
  1275. .cra_ctxsize = sizeof(struct safexcel_ahash_ctx),
  1276. .cra_init = safexcel_ahash_cra_init,
  1277. .cra_exit = safexcel_ahash_cra_exit,
  1278. .cra_module = THIS_MODULE,
  1279. },
  1280. },
  1281. },
  1282. };
  1283. static int safexcel_md5_init(struct ahash_request *areq)
  1284. {
  1285. struct safexcel_ahash_ctx *ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(areq));
  1286. struct safexcel_ahash_req *req = ahash_request_ctx(areq);
  1287. memset(req, 0, sizeof(*req));
  1288. req->state[0] = MD5_H0;
  1289. req->state[1] = MD5_H1;
  1290. req->state[2] = MD5_H2;
  1291. req->state[3] = MD5_H3;
  1292. ctx->alg = CONTEXT_CONTROL_CRYPTO_ALG_MD5;
  1293. req->digest = CONTEXT_CONTROL_DIGEST_PRECOMPUTED;
  1294. req->state_sz = MD5_DIGEST_SIZE;
  1295. return 0;
  1296. }
  1297. static int safexcel_md5_digest(struct ahash_request *areq)
  1298. {
  1299. int ret = safexcel_md5_init(areq);
  1300. if (ret)
  1301. return ret;
  1302. return safexcel_ahash_finup(areq);
  1303. }
  1304. struct safexcel_alg_template safexcel_alg_md5 = {
  1305. .type = SAFEXCEL_ALG_TYPE_AHASH,
  1306. .engines = EIP97IES | EIP197B | EIP197D,
  1307. .alg.ahash = {
  1308. .init = safexcel_md5_init,
  1309. .update = safexcel_ahash_update,
  1310. .final = safexcel_ahash_final,
  1311. .finup = safexcel_ahash_finup,
  1312. .digest = safexcel_md5_digest,
  1313. .export = safexcel_ahash_export,
  1314. .import = safexcel_ahash_import,
  1315. .halg = {
  1316. .digestsize = MD5_DIGEST_SIZE,
  1317. .statesize = sizeof(struct safexcel_ahash_export_state),
  1318. .base = {
  1319. .cra_name = "md5",
  1320. .cra_driver_name = "safexcel-md5",
  1321. .cra_priority = 300,
  1322. .cra_flags = CRYPTO_ALG_ASYNC |
  1323. CRYPTO_ALG_KERN_DRIVER_ONLY,
  1324. .cra_blocksize = MD5_HMAC_BLOCK_SIZE,
  1325. .cra_ctxsize = sizeof(struct safexcel_ahash_ctx),
  1326. .cra_init = safexcel_ahash_cra_init,
  1327. .cra_exit = safexcel_ahash_cra_exit,
  1328. .cra_module = THIS_MODULE,
  1329. },
  1330. },
  1331. },
  1332. };
  1333. static int safexcel_hmac_md5_init(struct ahash_request *areq)
  1334. {
  1335. struct safexcel_ahash_req *req = ahash_request_ctx(areq);
  1336. safexcel_md5_init(areq);
  1337. req->digest = CONTEXT_CONTROL_DIGEST_HMAC;
  1338. return 0;
  1339. }
  1340. static int safexcel_hmac_md5_setkey(struct crypto_ahash *tfm, const u8 *key,
  1341. unsigned int keylen)
  1342. {
  1343. return safexcel_hmac_alg_setkey(tfm, key, keylen, "safexcel-md5",
  1344. MD5_DIGEST_SIZE);
  1345. }
  1346. static int safexcel_hmac_md5_digest(struct ahash_request *areq)
  1347. {
  1348. int ret = safexcel_hmac_md5_init(areq);
  1349. if (ret)
  1350. return ret;
  1351. return safexcel_ahash_finup(areq);
  1352. }
  1353. struct safexcel_alg_template safexcel_alg_hmac_md5 = {
  1354. .type = SAFEXCEL_ALG_TYPE_AHASH,
  1355. .engines = EIP97IES | EIP197B | EIP197D,
  1356. .alg.ahash = {
  1357. .init = safexcel_hmac_md5_init,
  1358. .update = safexcel_ahash_update,
  1359. .final = safexcel_ahash_final,
  1360. .finup = safexcel_ahash_finup,
  1361. .digest = safexcel_hmac_md5_digest,
  1362. .setkey = safexcel_hmac_md5_setkey,
  1363. .export = safexcel_ahash_export,
  1364. .import = safexcel_ahash_import,
  1365. .halg = {
  1366. .digestsize = MD5_DIGEST_SIZE,
  1367. .statesize = sizeof(struct safexcel_ahash_export_state),
  1368. .base = {
  1369. .cra_name = "hmac(md5)",
  1370. .cra_driver_name = "safexcel-hmac-md5",
  1371. .cra_priority = 300,
  1372. .cra_flags = CRYPTO_ALG_ASYNC |
  1373. CRYPTO_ALG_KERN_DRIVER_ONLY,
  1374. .cra_blocksize = MD5_HMAC_BLOCK_SIZE,
  1375. .cra_ctxsize = sizeof(struct safexcel_ahash_ctx),
  1376. .cra_init = safexcel_ahash_cra_init,
  1377. .cra_exit = safexcel_ahash_cra_exit,
  1378. .cra_module = THIS_MODULE,
  1379. },
  1380. },
  1381. },
  1382. };