safexcel_hash.c 44 KB

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