sha1_mb.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935
  1. /*
  2. * Multi buffer SHA1 algorithm Glue Code
  3. *
  4. * This file is provided under a dual BSD/GPLv2 license. When using or
  5. * redistributing this file, you may do so under either license.
  6. *
  7. * GPL LICENSE SUMMARY
  8. *
  9. * Copyright(c) 2014 Intel Corporation.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of version 2 of the GNU General Public License as
  13. * published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful, but
  16. * WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * Contact Information:
  21. * Tim Chen <tim.c.chen@linux.intel.com>
  22. *
  23. * BSD LICENSE
  24. *
  25. * Copyright(c) 2014 Intel Corporation.
  26. *
  27. * Redistribution and use in source and binary forms, with or without
  28. * modification, are permitted provided that the following conditions
  29. * are met:
  30. *
  31. * * Redistributions of source code must retain the above copyright
  32. * notice, this list of conditions and the following disclaimer.
  33. * * Redistributions in binary form must reproduce the above copyright
  34. * notice, this list of conditions and the following disclaimer in
  35. * the documentation and/or other materials provided with the
  36. * distribution.
  37. * * Neither the name of Intel Corporation nor the names of its
  38. * contributors may be used to endorse or promote products derived
  39. * from this software without specific prior written permission.
  40. *
  41. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  42. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  43. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  44. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  45. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  46. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  47. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  48. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  49. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  50. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  51. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  52. */
  53. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  54. #include <crypto/internal/hash.h>
  55. #include <linux/init.h>
  56. #include <linux/module.h>
  57. #include <linux/mm.h>
  58. #include <linux/cryptohash.h>
  59. #include <linux/types.h>
  60. #include <linux/list.h>
  61. #include <crypto/scatterwalk.h>
  62. #include <crypto/sha.h>
  63. #include <crypto/mcryptd.h>
  64. #include <crypto/crypto_wq.h>
  65. #include <asm/byteorder.h>
  66. #include <linux/hardirq.h>
  67. #include <asm/fpu/api.h>
  68. #include "sha_mb_ctx.h"
  69. #define FLUSH_INTERVAL 1000 /* in usec */
  70. static struct mcryptd_alg_state sha1_mb_alg_state;
  71. struct sha1_mb_ctx {
  72. struct mcryptd_ahash *mcryptd_tfm;
  73. };
  74. static inline struct mcryptd_hash_request_ctx *cast_hash_to_mcryptd_ctx(struct sha1_hash_ctx *hash_ctx)
  75. {
  76. struct shash_desc *desc;
  77. desc = container_of((void *) hash_ctx, struct shash_desc, __ctx);
  78. return container_of(desc, struct mcryptd_hash_request_ctx, desc);
  79. }
  80. static inline struct ahash_request *cast_mcryptd_ctx_to_req(struct mcryptd_hash_request_ctx *ctx)
  81. {
  82. return container_of((void *) ctx, struct ahash_request, __ctx);
  83. }
  84. static void req_ctx_init(struct mcryptd_hash_request_ctx *rctx,
  85. struct shash_desc *desc)
  86. {
  87. rctx->flag = HASH_UPDATE;
  88. }
  89. static asmlinkage void (*sha1_job_mgr_init)(struct sha1_mb_mgr *state);
  90. static asmlinkage struct job_sha1* (*sha1_job_mgr_submit)(struct sha1_mb_mgr *state,
  91. struct job_sha1 *job);
  92. static asmlinkage struct job_sha1* (*sha1_job_mgr_flush)(struct sha1_mb_mgr *state);
  93. static asmlinkage struct job_sha1* (*sha1_job_mgr_get_comp_job)(struct sha1_mb_mgr *state);
  94. inline void sha1_init_digest(uint32_t *digest)
  95. {
  96. static const uint32_t initial_digest[SHA1_DIGEST_LENGTH] = {SHA1_H0,
  97. SHA1_H1, SHA1_H2, SHA1_H3, SHA1_H4 };
  98. memcpy(digest, initial_digest, sizeof(initial_digest));
  99. }
  100. inline uint32_t sha1_pad(uint8_t padblock[SHA1_BLOCK_SIZE * 2],
  101. uint32_t total_len)
  102. {
  103. uint32_t i = total_len & (SHA1_BLOCK_SIZE - 1);
  104. memset(&padblock[i], 0, SHA1_BLOCK_SIZE);
  105. padblock[i] = 0x80;
  106. i += ((SHA1_BLOCK_SIZE - 1) &
  107. (0 - (total_len + SHA1_PADLENGTHFIELD_SIZE + 1)))
  108. + 1 + SHA1_PADLENGTHFIELD_SIZE;
  109. #if SHA1_PADLENGTHFIELD_SIZE == 16
  110. *((uint64_t *) &padblock[i - 16]) = 0;
  111. #endif
  112. *((uint64_t *) &padblock[i - 8]) = cpu_to_be64(total_len << 3);
  113. /* Number of extra blocks to hash */
  114. return i >> SHA1_LOG2_BLOCK_SIZE;
  115. }
  116. static struct sha1_hash_ctx *sha1_ctx_mgr_resubmit(struct sha1_ctx_mgr *mgr, struct sha1_hash_ctx *ctx)
  117. {
  118. while (ctx) {
  119. if (ctx->status & HASH_CTX_STS_COMPLETE) {
  120. /* Clear PROCESSING bit */
  121. ctx->status = HASH_CTX_STS_COMPLETE;
  122. return ctx;
  123. }
  124. /*
  125. * If the extra blocks are empty, begin hashing what remains
  126. * in the user's buffer.
  127. */
  128. if (ctx->partial_block_buffer_length == 0 &&
  129. ctx->incoming_buffer_length) {
  130. const void *buffer = ctx->incoming_buffer;
  131. uint32_t len = ctx->incoming_buffer_length;
  132. uint32_t copy_len;
  133. /*
  134. * Only entire blocks can be hashed.
  135. * Copy remainder to extra blocks buffer.
  136. */
  137. copy_len = len & (SHA1_BLOCK_SIZE-1);
  138. if (copy_len) {
  139. len -= copy_len;
  140. memcpy(ctx->partial_block_buffer,
  141. ((const char *) buffer + len),
  142. copy_len);
  143. ctx->partial_block_buffer_length = copy_len;
  144. }
  145. ctx->incoming_buffer_length = 0;
  146. /* len should be a multiple of the block size now */
  147. assert((len % SHA1_BLOCK_SIZE) == 0);
  148. /* Set len to the number of blocks to be hashed */
  149. len >>= SHA1_LOG2_BLOCK_SIZE;
  150. if (len) {
  151. ctx->job.buffer = (uint8_t *) buffer;
  152. ctx->job.len = len;
  153. ctx = (struct sha1_hash_ctx *) sha1_job_mgr_submit(&mgr->mgr,
  154. &ctx->job);
  155. continue;
  156. }
  157. }
  158. /*
  159. * If the extra blocks are not empty, then we are
  160. * either on the last block(s) or we need more
  161. * user input before continuing.
  162. */
  163. if (ctx->status & HASH_CTX_STS_LAST) {
  164. uint8_t *buf = ctx->partial_block_buffer;
  165. uint32_t n_extra_blocks = sha1_pad(buf, ctx->total_length);
  166. ctx->status = (HASH_CTX_STS_PROCESSING |
  167. HASH_CTX_STS_COMPLETE);
  168. ctx->job.buffer = buf;
  169. ctx->job.len = (uint32_t) n_extra_blocks;
  170. ctx = (struct sha1_hash_ctx *) sha1_job_mgr_submit(&mgr->mgr, &ctx->job);
  171. continue;
  172. }
  173. ctx->status = HASH_CTX_STS_IDLE;
  174. return ctx;
  175. }
  176. return NULL;
  177. }
  178. static struct sha1_hash_ctx *sha1_ctx_mgr_get_comp_ctx(struct sha1_ctx_mgr *mgr)
  179. {
  180. /*
  181. * If get_comp_job returns NULL, there are no jobs complete.
  182. * If get_comp_job returns a job, verify that it is safe to return to the user.
  183. * If it is not ready, resubmit the job to finish processing.
  184. * If sha1_ctx_mgr_resubmit returned a job, it is ready to be returned.
  185. * Otherwise, all jobs currently being managed by the hash_ctx_mgr still need processing.
  186. */
  187. struct sha1_hash_ctx *ctx;
  188. ctx = (struct sha1_hash_ctx *) sha1_job_mgr_get_comp_job(&mgr->mgr);
  189. return sha1_ctx_mgr_resubmit(mgr, ctx);
  190. }
  191. static void sha1_ctx_mgr_init(struct sha1_ctx_mgr *mgr)
  192. {
  193. sha1_job_mgr_init(&mgr->mgr);
  194. }
  195. static struct sha1_hash_ctx *sha1_ctx_mgr_submit(struct sha1_ctx_mgr *mgr,
  196. struct sha1_hash_ctx *ctx,
  197. const void *buffer,
  198. uint32_t len,
  199. int flags)
  200. {
  201. if (flags & (~HASH_ENTIRE)) {
  202. /* User should not pass anything other than FIRST, UPDATE, or LAST */
  203. ctx->error = HASH_CTX_ERROR_INVALID_FLAGS;
  204. return ctx;
  205. }
  206. if (ctx->status & HASH_CTX_STS_PROCESSING) {
  207. /* Cannot submit to a currently processing job. */
  208. ctx->error = HASH_CTX_ERROR_ALREADY_PROCESSING;
  209. return ctx;
  210. }
  211. if ((ctx->status & HASH_CTX_STS_COMPLETE) && !(flags & HASH_FIRST)) {
  212. /* Cannot update a finished job. */
  213. ctx->error = HASH_CTX_ERROR_ALREADY_COMPLETED;
  214. return ctx;
  215. }
  216. if (flags & HASH_FIRST) {
  217. /* Init digest */
  218. sha1_init_digest(ctx->job.result_digest);
  219. /* Reset byte counter */
  220. ctx->total_length = 0;
  221. /* Clear extra blocks */
  222. ctx->partial_block_buffer_length = 0;
  223. }
  224. /* If we made it here, there were no errors during this call to submit */
  225. ctx->error = HASH_CTX_ERROR_NONE;
  226. /* Store buffer ptr info from user */
  227. ctx->incoming_buffer = buffer;
  228. ctx->incoming_buffer_length = len;
  229. /* Store the user's request flags and mark this ctx as currently being processed. */
  230. ctx->status = (flags & HASH_LAST) ?
  231. (HASH_CTX_STS_PROCESSING | HASH_CTX_STS_LAST) :
  232. HASH_CTX_STS_PROCESSING;
  233. /* Advance byte counter */
  234. ctx->total_length += len;
  235. /*
  236. * If there is anything currently buffered in the extra blocks,
  237. * append to it until it contains a whole block.
  238. * Or if the user's buffer contains less than a whole block,
  239. * append as much as possible to the extra block.
  240. */
  241. if ((ctx->partial_block_buffer_length) | (len < SHA1_BLOCK_SIZE)) {
  242. /* Compute how many bytes to copy from user buffer into extra block */
  243. uint32_t copy_len = SHA1_BLOCK_SIZE - ctx->partial_block_buffer_length;
  244. if (len < copy_len)
  245. copy_len = len;
  246. if (copy_len) {
  247. /* Copy and update relevant pointers and counters */
  248. memcpy(&ctx->partial_block_buffer[ctx->partial_block_buffer_length],
  249. buffer, copy_len);
  250. ctx->partial_block_buffer_length += copy_len;
  251. ctx->incoming_buffer = (const void *)((const char *)buffer + copy_len);
  252. ctx->incoming_buffer_length = len - copy_len;
  253. }
  254. /* The extra block should never contain more than 1 block here */
  255. assert(ctx->partial_block_buffer_length <= SHA1_BLOCK_SIZE);
  256. /* If the extra block buffer contains exactly 1 block, it can be hashed. */
  257. if (ctx->partial_block_buffer_length >= SHA1_BLOCK_SIZE) {
  258. ctx->partial_block_buffer_length = 0;
  259. ctx->job.buffer = ctx->partial_block_buffer;
  260. ctx->job.len = 1;
  261. ctx = (struct sha1_hash_ctx *) sha1_job_mgr_submit(&mgr->mgr, &ctx->job);
  262. }
  263. }
  264. return sha1_ctx_mgr_resubmit(mgr, ctx);
  265. }
  266. static struct sha1_hash_ctx *sha1_ctx_mgr_flush(struct sha1_ctx_mgr *mgr)
  267. {
  268. struct sha1_hash_ctx *ctx;
  269. while (1) {
  270. ctx = (struct sha1_hash_ctx *) sha1_job_mgr_flush(&mgr->mgr);
  271. /* If flush returned 0, there are no more jobs in flight. */
  272. if (!ctx)
  273. return NULL;
  274. /*
  275. * If flush returned a job, resubmit the job to finish processing.
  276. */
  277. ctx = sha1_ctx_mgr_resubmit(mgr, ctx);
  278. /*
  279. * If sha1_ctx_mgr_resubmit returned a job, it is ready to be returned.
  280. * Otherwise, all jobs currently being managed by the sha1_ctx_mgr
  281. * still need processing. Loop.
  282. */
  283. if (ctx)
  284. return ctx;
  285. }
  286. }
  287. static int sha1_mb_init(struct shash_desc *desc)
  288. {
  289. struct sha1_hash_ctx *sctx = shash_desc_ctx(desc);
  290. hash_ctx_init(sctx);
  291. sctx->job.result_digest[0] = SHA1_H0;
  292. sctx->job.result_digest[1] = SHA1_H1;
  293. sctx->job.result_digest[2] = SHA1_H2;
  294. sctx->job.result_digest[3] = SHA1_H3;
  295. sctx->job.result_digest[4] = SHA1_H4;
  296. sctx->total_length = 0;
  297. sctx->partial_block_buffer_length = 0;
  298. sctx->status = HASH_CTX_STS_IDLE;
  299. return 0;
  300. }
  301. static int sha1_mb_set_results(struct mcryptd_hash_request_ctx *rctx)
  302. {
  303. int i;
  304. struct sha1_hash_ctx *sctx = shash_desc_ctx(&rctx->desc);
  305. __be32 *dst = (__be32 *) rctx->out;
  306. for (i = 0; i < 5; ++i)
  307. dst[i] = cpu_to_be32(sctx->job.result_digest[i]);
  308. return 0;
  309. }
  310. static int sha_finish_walk(struct mcryptd_hash_request_ctx **ret_rctx,
  311. struct mcryptd_alg_cstate *cstate, bool flush)
  312. {
  313. int flag = HASH_UPDATE;
  314. int nbytes, err = 0;
  315. struct mcryptd_hash_request_ctx *rctx = *ret_rctx;
  316. struct sha1_hash_ctx *sha_ctx;
  317. /* more work ? */
  318. while (!(rctx->flag & HASH_DONE)) {
  319. nbytes = crypto_ahash_walk_done(&rctx->walk, 0);
  320. if (nbytes < 0) {
  321. err = nbytes;
  322. goto out;
  323. }
  324. /* check if the walk is done */
  325. if (crypto_ahash_walk_last(&rctx->walk)) {
  326. rctx->flag |= HASH_DONE;
  327. if (rctx->flag & HASH_FINAL)
  328. flag |= HASH_LAST;
  329. }
  330. sha_ctx = (struct sha1_hash_ctx *) shash_desc_ctx(&rctx->desc);
  331. kernel_fpu_begin();
  332. sha_ctx = sha1_ctx_mgr_submit(cstate->mgr, sha_ctx, rctx->walk.data, nbytes, flag);
  333. if (!sha_ctx) {
  334. if (flush)
  335. sha_ctx = sha1_ctx_mgr_flush(cstate->mgr);
  336. }
  337. kernel_fpu_end();
  338. if (sha_ctx)
  339. rctx = cast_hash_to_mcryptd_ctx(sha_ctx);
  340. else {
  341. rctx = NULL;
  342. goto out;
  343. }
  344. }
  345. /* copy the results */
  346. if (rctx->flag & HASH_FINAL)
  347. sha1_mb_set_results(rctx);
  348. out:
  349. *ret_rctx = rctx;
  350. return err;
  351. }
  352. static int sha_complete_job(struct mcryptd_hash_request_ctx *rctx,
  353. struct mcryptd_alg_cstate *cstate,
  354. int err)
  355. {
  356. struct ahash_request *req = cast_mcryptd_ctx_to_req(rctx);
  357. struct sha1_hash_ctx *sha_ctx;
  358. struct mcryptd_hash_request_ctx *req_ctx;
  359. int ret;
  360. /* remove from work list */
  361. spin_lock(&cstate->work_lock);
  362. list_del(&rctx->waiter);
  363. spin_unlock(&cstate->work_lock);
  364. if (irqs_disabled())
  365. rctx->complete(&req->base, err);
  366. else {
  367. local_bh_disable();
  368. rctx->complete(&req->base, err);
  369. local_bh_enable();
  370. }
  371. /* check to see if there are other jobs that are done */
  372. sha_ctx = sha1_ctx_mgr_get_comp_ctx(cstate->mgr);
  373. while (sha_ctx) {
  374. req_ctx = cast_hash_to_mcryptd_ctx(sha_ctx);
  375. ret = sha_finish_walk(&req_ctx, cstate, false);
  376. if (req_ctx) {
  377. spin_lock(&cstate->work_lock);
  378. list_del(&req_ctx->waiter);
  379. spin_unlock(&cstate->work_lock);
  380. req = cast_mcryptd_ctx_to_req(req_ctx);
  381. if (irqs_disabled())
  382. rctx->complete(&req->base, ret);
  383. else {
  384. local_bh_disable();
  385. rctx->complete(&req->base, ret);
  386. local_bh_enable();
  387. }
  388. }
  389. sha_ctx = sha1_ctx_mgr_get_comp_ctx(cstate->mgr);
  390. }
  391. return 0;
  392. }
  393. static void sha1_mb_add_list(struct mcryptd_hash_request_ctx *rctx,
  394. struct mcryptd_alg_cstate *cstate)
  395. {
  396. unsigned long next_flush;
  397. unsigned long delay = usecs_to_jiffies(FLUSH_INTERVAL);
  398. /* initialize tag */
  399. rctx->tag.arrival = jiffies; /* tag the arrival time */
  400. rctx->tag.seq_num = cstate->next_seq_num++;
  401. next_flush = rctx->tag.arrival + delay;
  402. rctx->tag.expire = next_flush;
  403. spin_lock(&cstate->work_lock);
  404. list_add_tail(&rctx->waiter, &cstate->work_list);
  405. spin_unlock(&cstate->work_lock);
  406. mcryptd_arm_flusher(cstate, delay);
  407. }
  408. static int sha1_mb_update(struct shash_desc *desc, const u8 *data,
  409. unsigned int len)
  410. {
  411. struct mcryptd_hash_request_ctx *rctx =
  412. container_of(desc, struct mcryptd_hash_request_ctx, desc);
  413. struct mcryptd_alg_cstate *cstate =
  414. this_cpu_ptr(sha1_mb_alg_state.alg_cstate);
  415. struct ahash_request *req = cast_mcryptd_ctx_to_req(rctx);
  416. struct sha1_hash_ctx *sha_ctx;
  417. int ret = 0, nbytes;
  418. /* sanity check */
  419. if (rctx->tag.cpu != smp_processor_id()) {
  420. pr_err("mcryptd error: cpu clash\n");
  421. goto done;
  422. }
  423. /* need to init context */
  424. req_ctx_init(rctx, desc);
  425. nbytes = crypto_ahash_walk_first(req, &rctx->walk);
  426. if (nbytes < 0) {
  427. ret = nbytes;
  428. goto done;
  429. }
  430. if (crypto_ahash_walk_last(&rctx->walk))
  431. rctx->flag |= HASH_DONE;
  432. /* submit */
  433. sha_ctx = (struct sha1_hash_ctx *) shash_desc_ctx(desc);
  434. sha1_mb_add_list(rctx, cstate);
  435. kernel_fpu_begin();
  436. sha_ctx = sha1_ctx_mgr_submit(cstate->mgr, sha_ctx, rctx->walk.data, nbytes, HASH_UPDATE);
  437. kernel_fpu_end();
  438. /* check if anything is returned */
  439. if (!sha_ctx)
  440. return -EINPROGRESS;
  441. if (sha_ctx->error) {
  442. ret = sha_ctx->error;
  443. rctx = cast_hash_to_mcryptd_ctx(sha_ctx);
  444. goto done;
  445. }
  446. rctx = cast_hash_to_mcryptd_ctx(sha_ctx);
  447. ret = sha_finish_walk(&rctx, cstate, false);
  448. if (!rctx)
  449. return -EINPROGRESS;
  450. done:
  451. sha_complete_job(rctx, cstate, ret);
  452. return ret;
  453. }
  454. static int sha1_mb_finup(struct shash_desc *desc, const u8 *data,
  455. unsigned int len, u8 *out)
  456. {
  457. struct mcryptd_hash_request_ctx *rctx =
  458. container_of(desc, struct mcryptd_hash_request_ctx, desc);
  459. struct mcryptd_alg_cstate *cstate =
  460. this_cpu_ptr(sha1_mb_alg_state.alg_cstate);
  461. struct ahash_request *req = cast_mcryptd_ctx_to_req(rctx);
  462. struct sha1_hash_ctx *sha_ctx;
  463. int ret = 0, flag = HASH_UPDATE, nbytes;
  464. /* sanity check */
  465. if (rctx->tag.cpu != smp_processor_id()) {
  466. pr_err("mcryptd error: cpu clash\n");
  467. goto done;
  468. }
  469. /* need to init context */
  470. req_ctx_init(rctx, desc);
  471. nbytes = crypto_ahash_walk_first(req, &rctx->walk);
  472. if (nbytes < 0) {
  473. ret = nbytes;
  474. goto done;
  475. }
  476. if (crypto_ahash_walk_last(&rctx->walk)) {
  477. rctx->flag |= HASH_DONE;
  478. flag = HASH_LAST;
  479. }
  480. rctx->out = out;
  481. /* submit */
  482. rctx->flag |= HASH_FINAL;
  483. sha_ctx = (struct sha1_hash_ctx *) shash_desc_ctx(desc);
  484. sha1_mb_add_list(rctx, cstate);
  485. kernel_fpu_begin();
  486. sha_ctx = sha1_ctx_mgr_submit(cstate->mgr, sha_ctx, rctx->walk.data, nbytes, flag);
  487. kernel_fpu_end();
  488. /* check if anything is returned */
  489. if (!sha_ctx)
  490. return -EINPROGRESS;
  491. if (sha_ctx->error) {
  492. ret = sha_ctx->error;
  493. goto done;
  494. }
  495. rctx = cast_hash_to_mcryptd_ctx(sha_ctx);
  496. ret = sha_finish_walk(&rctx, cstate, false);
  497. if (!rctx)
  498. return -EINPROGRESS;
  499. done:
  500. sha_complete_job(rctx, cstate, ret);
  501. return ret;
  502. }
  503. static int sha1_mb_final(struct shash_desc *desc, u8 *out)
  504. {
  505. struct mcryptd_hash_request_ctx *rctx =
  506. container_of(desc, struct mcryptd_hash_request_ctx, desc);
  507. struct mcryptd_alg_cstate *cstate =
  508. this_cpu_ptr(sha1_mb_alg_state.alg_cstate);
  509. struct sha1_hash_ctx *sha_ctx;
  510. int ret = 0;
  511. u8 data;
  512. /* sanity check */
  513. if (rctx->tag.cpu != smp_processor_id()) {
  514. pr_err("mcryptd error: cpu clash\n");
  515. goto done;
  516. }
  517. /* need to init context */
  518. req_ctx_init(rctx, desc);
  519. rctx->out = out;
  520. rctx->flag |= HASH_DONE | HASH_FINAL;
  521. sha_ctx = (struct sha1_hash_ctx *) shash_desc_ctx(desc);
  522. /* flag HASH_FINAL and 0 data size */
  523. sha1_mb_add_list(rctx, cstate);
  524. kernel_fpu_begin();
  525. sha_ctx = sha1_ctx_mgr_submit(cstate->mgr, sha_ctx, &data, 0, HASH_LAST);
  526. kernel_fpu_end();
  527. /* check if anything is returned */
  528. if (!sha_ctx)
  529. return -EINPROGRESS;
  530. if (sha_ctx->error) {
  531. ret = sha_ctx->error;
  532. rctx = cast_hash_to_mcryptd_ctx(sha_ctx);
  533. goto done;
  534. }
  535. rctx = cast_hash_to_mcryptd_ctx(sha_ctx);
  536. ret = sha_finish_walk(&rctx, cstate, false);
  537. if (!rctx)
  538. return -EINPROGRESS;
  539. done:
  540. sha_complete_job(rctx, cstate, ret);
  541. return ret;
  542. }
  543. static int sha1_mb_export(struct shash_desc *desc, void *out)
  544. {
  545. struct sha1_hash_ctx *sctx = shash_desc_ctx(desc);
  546. memcpy(out, sctx, sizeof(*sctx));
  547. return 0;
  548. }
  549. static int sha1_mb_import(struct shash_desc *desc, const void *in)
  550. {
  551. struct sha1_hash_ctx *sctx = shash_desc_ctx(desc);
  552. memcpy(sctx, in, sizeof(*sctx));
  553. return 0;
  554. }
  555. static struct shash_alg sha1_mb_shash_alg = {
  556. .digestsize = SHA1_DIGEST_SIZE,
  557. .init = sha1_mb_init,
  558. .update = sha1_mb_update,
  559. .final = sha1_mb_final,
  560. .finup = sha1_mb_finup,
  561. .export = sha1_mb_export,
  562. .import = sha1_mb_import,
  563. .descsize = sizeof(struct sha1_hash_ctx),
  564. .statesize = sizeof(struct sha1_hash_ctx),
  565. .base = {
  566. .cra_name = "__sha1-mb",
  567. .cra_driver_name = "__intel_sha1-mb",
  568. .cra_priority = 100,
  569. /*
  570. * use ASYNC flag as some buffers in multi-buffer
  571. * algo may not have completed before hashing thread sleep
  572. */
  573. .cra_flags = CRYPTO_ALG_TYPE_SHASH | CRYPTO_ALG_ASYNC |
  574. CRYPTO_ALG_INTERNAL,
  575. .cra_blocksize = SHA1_BLOCK_SIZE,
  576. .cra_module = THIS_MODULE,
  577. .cra_list = LIST_HEAD_INIT(sha1_mb_shash_alg.base.cra_list),
  578. }
  579. };
  580. static int sha1_mb_async_init(struct ahash_request *req)
  581. {
  582. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  583. struct sha1_mb_ctx *ctx = crypto_ahash_ctx(tfm);
  584. struct ahash_request *mcryptd_req = ahash_request_ctx(req);
  585. struct mcryptd_ahash *mcryptd_tfm = ctx->mcryptd_tfm;
  586. memcpy(mcryptd_req, req, sizeof(*req));
  587. ahash_request_set_tfm(mcryptd_req, &mcryptd_tfm->base);
  588. return crypto_ahash_init(mcryptd_req);
  589. }
  590. static int sha1_mb_async_update(struct ahash_request *req)
  591. {
  592. struct ahash_request *mcryptd_req = ahash_request_ctx(req);
  593. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  594. struct sha1_mb_ctx *ctx = crypto_ahash_ctx(tfm);
  595. struct mcryptd_ahash *mcryptd_tfm = ctx->mcryptd_tfm;
  596. memcpy(mcryptd_req, req, sizeof(*req));
  597. ahash_request_set_tfm(mcryptd_req, &mcryptd_tfm->base);
  598. return crypto_ahash_update(mcryptd_req);
  599. }
  600. static int sha1_mb_async_finup(struct ahash_request *req)
  601. {
  602. struct ahash_request *mcryptd_req = ahash_request_ctx(req);
  603. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  604. struct sha1_mb_ctx *ctx = crypto_ahash_ctx(tfm);
  605. struct mcryptd_ahash *mcryptd_tfm = ctx->mcryptd_tfm;
  606. memcpy(mcryptd_req, req, sizeof(*req));
  607. ahash_request_set_tfm(mcryptd_req, &mcryptd_tfm->base);
  608. return crypto_ahash_finup(mcryptd_req);
  609. }
  610. static int sha1_mb_async_final(struct ahash_request *req)
  611. {
  612. struct ahash_request *mcryptd_req = ahash_request_ctx(req);
  613. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  614. struct sha1_mb_ctx *ctx = crypto_ahash_ctx(tfm);
  615. struct mcryptd_ahash *mcryptd_tfm = ctx->mcryptd_tfm;
  616. memcpy(mcryptd_req, req, sizeof(*req));
  617. ahash_request_set_tfm(mcryptd_req, &mcryptd_tfm->base);
  618. return crypto_ahash_final(mcryptd_req);
  619. }
  620. static int sha1_mb_async_digest(struct ahash_request *req)
  621. {
  622. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  623. struct sha1_mb_ctx *ctx = crypto_ahash_ctx(tfm);
  624. struct ahash_request *mcryptd_req = ahash_request_ctx(req);
  625. struct mcryptd_ahash *mcryptd_tfm = ctx->mcryptd_tfm;
  626. memcpy(mcryptd_req, req, sizeof(*req));
  627. ahash_request_set_tfm(mcryptd_req, &mcryptd_tfm->base);
  628. return crypto_ahash_digest(mcryptd_req);
  629. }
  630. static int sha1_mb_async_init_tfm(struct crypto_tfm *tfm)
  631. {
  632. struct mcryptd_ahash *mcryptd_tfm;
  633. struct sha1_mb_ctx *ctx = crypto_tfm_ctx(tfm);
  634. struct mcryptd_hash_ctx *mctx;
  635. mcryptd_tfm = mcryptd_alloc_ahash("__intel_sha1-mb",
  636. CRYPTO_ALG_INTERNAL,
  637. CRYPTO_ALG_INTERNAL);
  638. if (IS_ERR(mcryptd_tfm))
  639. return PTR_ERR(mcryptd_tfm);
  640. mctx = crypto_ahash_ctx(&mcryptd_tfm->base);
  641. mctx->alg_state = &sha1_mb_alg_state;
  642. ctx->mcryptd_tfm = mcryptd_tfm;
  643. crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
  644. sizeof(struct ahash_request) +
  645. crypto_ahash_reqsize(&mcryptd_tfm->base));
  646. return 0;
  647. }
  648. static void sha1_mb_async_exit_tfm(struct crypto_tfm *tfm)
  649. {
  650. struct sha1_mb_ctx *ctx = crypto_tfm_ctx(tfm);
  651. mcryptd_free_ahash(ctx->mcryptd_tfm);
  652. }
  653. static struct ahash_alg sha1_mb_async_alg = {
  654. .init = sha1_mb_async_init,
  655. .update = sha1_mb_async_update,
  656. .final = sha1_mb_async_final,
  657. .finup = sha1_mb_async_finup,
  658. .digest = sha1_mb_async_digest,
  659. .halg = {
  660. .digestsize = SHA1_DIGEST_SIZE,
  661. .base = {
  662. .cra_name = "sha1",
  663. .cra_driver_name = "sha1_mb",
  664. .cra_priority = 200,
  665. .cra_flags = CRYPTO_ALG_TYPE_AHASH | CRYPTO_ALG_ASYNC,
  666. .cra_blocksize = SHA1_BLOCK_SIZE,
  667. .cra_type = &crypto_ahash_type,
  668. .cra_module = THIS_MODULE,
  669. .cra_list = LIST_HEAD_INIT(sha1_mb_async_alg.halg.base.cra_list),
  670. .cra_init = sha1_mb_async_init_tfm,
  671. .cra_exit = sha1_mb_async_exit_tfm,
  672. .cra_ctxsize = sizeof(struct sha1_mb_ctx),
  673. .cra_alignmask = 0,
  674. },
  675. },
  676. };
  677. static unsigned long sha1_mb_flusher(struct mcryptd_alg_cstate *cstate)
  678. {
  679. struct mcryptd_hash_request_ctx *rctx;
  680. unsigned long cur_time;
  681. unsigned long next_flush = 0;
  682. struct sha1_hash_ctx *sha_ctx;
  683. cur_time = jiffies;
  684. while (!list_empty(&cstate->work_list)) {
  685. rctx = list_entry(cstate->work_list.next,
  686. struct mcryptd_hash_request_ctx, waiter);
  687. if (time_before(cur_time, rctx->tag.expire))
  688. break;
  689. kernel_fpu_begin();
  690. sha_ctx = (struct sha1_hash_ctx *) sha1_ctx_mgr_flush(cstate->mgr);
  691. kernel_fpu_end();
  692. if (!sha_ctx) {
  693. pr_err("sha1_mb error: nothing got flushed for non-empty list\n");
  694. break;
  695. }
  696. rctx = cast_hash_to_mcryptd_ctx(sha_ctx);
  697. sha_finish_walk(&rctx, cstate, true);
  698. sha_complete_job(rctx, cstate, 0);
  699. }
  700. if (!list_empty(&cstate->work_list)) {
  701. rctx = list_entry(cstate->work_list.next,
  702. struct mcryptd_hash_request_ctx, waiter);
  703. /* get the hash context and then flush time */
  704. next_flush = rctx->tag.expire;
  705. mcryptd_arm_flusher(cstate, get_delay(next_flush));
  706. }
  707. return next_flush;
  708. }
  709. static int __init sha1_mb_mod_init(void)
  710. {
  711. int cpu;
  712. int err;
  713. struct mcryptd_alg_cstate *cpu_state;
  714. /* check for dependent cpu features */
  715. if (!boot_cpu_has(X86_FEATURE_AVX2) ||
  716. !boot_cpu_has(X86_FEATURE_BMI2))
  717. return -ENODEV;
  718. /* initialize multibuffer structures */
  719. sha1_mb_alg_state.alg_cstate = alloc_percpu(struct mcryptd_alg_cstate);
  720. sha1_job_mgr_init = sha1_mb_mgr_init_avx2;
  721. sha1_job_mgr_submit = sha1_mb_mgr_submit_avx2;
  722. sha1_job_mgr_flush = sha1_mb_mgr_flush_avx2;
  723. sha1_job_mgr_get_comp_job = sha1_mb_mgr_get_comp_job_avx2;
  724. if (!sha1_mb_alg_state.alg_cstate)
  725. return -ENOMEM;
  726. for_each_possible_cpu(cpu) {
  727. cpu_state = per_cpu_ptr(sha1_mb_alg_state.alg_cstate, cpu);
  728. cpu_state->next_flush = 0;
  729. cpu_state->next_seq_num = 0;
  730. cpu_state->flusher_engaged = false;
  731. INIT_DELAYED_WORK(&cpu_state->flush, mcryptd_flusher);
  732. cpu_state->cpu = cpu;
  733. cpu_state->alg_state = &sha1_mb_alg_state;
  734. cpu_state->mgr = kzalloc(sizeof(struct sha1_ctx_mgr),
  735. GFP_KERNEL);
  736. if (!cpu_state->mgr)
  737. goto err2;
  738. sha1_ctx_mgr_init(cpu_state->mgr);
  739. INIT_LIST_HEAD(&cpu_state->work_list);
  740. spin_lock_init(&cpu_state->work_lock);
  741. }
  742. sha1_mb_alg_state.flusher = &sha1_mb_flusher;
  743. err = crypto_register_shash(&sha1_mb_shash_alg);
  744. if (err)
  745. goto err2;
  746. err = crypto_register_ahash(&sha1_mb_async_alg);
  747. if (err)
  748. goto err1;
  749. return 0;
  750. err1:
  751. crypto_unregister_shash(&sha1_mb_shash_alg);
  752. err2:
  753. for_each_possible_cpu(cpu) {
  754. cpu_state = per_cpu_ptr(sha1_mb_alg_state.alg_cstate, cpu);
  755. kfree(cpu_state->mgr);
  756. }
  757. free_percpu(sha1_mb_alg_state.alg_cstate);
  758. return -ENODEV;
  759. }
  760. static void __exit sha1_mb_mod_fini(void)
  761. {
  762. int cpu;
  763. struct mcryptd_alg_cstate *cpu_state;
  764. crypto_unregister_ahash(&sha1_mb_async_alg);
  765. crypto_unregister_shash(&sha1_mb_shash_alg);
  766. for_each_possible_cpu(cpu) {
  767. cpu_state = per_cpu_ptr(sha1_mb_alg_state.alg_cstate, cpu);
  768. kfree(cpu_state->mgr);
  769. }
  770. free_percpu(sha1_mb_alg_state.alg_cstate);
  771. }
  772. module_init(sha1_mb_mod_init);
  773. module_exit(sha1_mb_mod_fini);
  774. MODULE_LICENSE("GPL");
  775. MODULE_DESCRIPTION("SHA1 Secure Hash Algorithm, multi buffer accelerated");
  776. MODULE_ALIAS_CRYPTO("sha1");