safexcel_hash.c 44 KB

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