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 <asm/i387.h>
  67. #include <asm/xcr.h>
  68. #include <asm/xsave.h>
  69. #include <linux/hardirq.h>
  70. #include <asm/fpu-internal.h>
  71. #include "sha_mb_ctx.h"
  72. #define FLUSH_INTERVAL 1000 /* in usec */
  73. static struct mcryptd_alg_state sha1_mb_alg_state;
  74. struct sha1_mb_ctx {
  75. struct mcryptd_ahash *mcryptd_tfm;
  76. };
  77. static inline struct mcryptd_hash_request_ctx *cast_hash_to_mcryptd_ctx(struct sha1_hash_ctx *hash_ctx)
  78. {
  79. struct shash_desc *desc;
  80. desc = container_of((void *) hash_ctx, struct shash_desc, __ctx);
  81. return container_of(desc, struct mcryptd_hash_request_ctx, desc);
  82. }
  83. static inline struct ahash_request *cast_mcryptd_ctx_to_req(struct mcryptd_hash_request_ctx *ctx)
  84. {
  85. return container_of((void *) ctx, struct ahash_request, __ctx);
  86. }
  87. static void req_ctx_init(struct mcryptd_hash_request_ctx *rctx,
  88. struct shash_desc *desc)
  89. {
  90. rctx->flag = HASH_UPDATE;
  91. }
  92. static asmlinkage void (*sha1_job_mgr_init)(struct sha1_mb_mgr *state);
  93. static asmlinkage struct job_sha1* (*sha1_job_mgr_submit)(struct sha1_mb_mgr *state,
  94. struct job_sha1 *job);
  95. static asmlinkage struct job_sha1* (*sha1_job_mgr_flush)(struct sha1_mb_mgr *state);
  96. static asmlinkage struct job_sha1* (*sha1_job_mgr_get_comp_job)(struct sha1_mb_mgr *state);
  97. inline void sha1_init_digest(uint32_t *digest)
  98. {
  99. static const uint32_t initial_digest[SHA1_DIGEST_LENGTH] = {SHA1_H0,
  100. SHA1_H1, SHA1_H2, SHA1_H3, SHA1_H4 };
  101. memcpy(digest, initial_digest, sizeof(initial_digest));
  102. }
  103. inline uint32_t sha1_pad(uint8_t padblock[SHA1_BLOCK_SIZE * 2],
  104. uint32_t total_len)
  105. {
  106. uint32_t i = total_len & (SHA1_BLOCK_SIZE - 1);
  107. memset(&padblock[i], 0, SHA1_BLOCK_SIZE);
  108. padblock[i] = 0x80;
  109. i += ((SHA1_BLOCK_SIZE - 1) &
  110. (0 - (total_len + SHA1_PADLENGTHFIELD_SIZE + 1)))
  111. + 1 + SHA1_PADLENGTHFIELD_SIZE;
  112. #if SHA1_PADLENGTHFIELD_SIZE == 16
  113. *((uint64_t *) &padblock[i - 16]) = 0;
  114. #endif
  115. *((uint64_t *) &padblock[i - 8]) = cpu_to_be64(total_len << 3);
  116. /* Number of extra blocks to hash */
  117. return i >> SHA1_LOG2_BLOCK_SIZE;
  118. }
  119. static struct sha1_hash_ctx *sha1_ctx_mgr_resubmit(struct sha1_ctx_mgr *mgr, struct sha1_hash_ctx *ctx)
  120. {
  121. while (ctx) {
  122. if (ctx->status & HASH_CTX_STS_COMPLETE) {
  123. /* Clear PROCESSING bit */
  124. ctx->status = HASH_CTX_STS_COMPLETE;
  125. return ctx;
  126. }
  127. /*
  128. * If the extra blocks are empty, begin hashing what remains
  129. * in the user's buffer.
  130. */
  131. if (ctx->partial_block_buffer_length == 0 &&
  132. ctx->incoming_buffer_length) {
  133. const void *buffer = ctx->incoming_buffer;
  134. uint32_t len = ctx->incoming_buffer_length;
  135. uint32_t copy_len;
  136. /*
  137. * Only entire blocks can be hashed.
  138. * Copy remainder to extra blocks buffer.
  139. */
  140. copy_len = len & (SHA1_BLOCK_SIZE-1);
  141. if (copy_len) {
  142. len -= copy_len;
  143. memcpy(ctx->partial_block_buffer,
  144. ((const char *) buffer + len),
  145. copy_len);
  146. ctx->partial_block_buffer_length = copy_len;
  147. }
  148. ctx->incoming_buffer_length = 0;
  149. /* len should be a multiple of the block size now */
  150. assert((len % SHA1_BLOCK_SIZE) == 0);
  151. /* Set len to the number of blocks to be hashed */
  152. len >>= SHA1_LOG2_BLOCK_SIZE;
  153. if (len) {
  154. ctx->job.buffer = (uint8_t *) buffer;
  155. ctx->job.len = len;
  156. ctx = (struct sha1_hash_ctx *) sha1_job_mgr_submit(&mgr->mgr,
  157. &ctx->job);
  158. continue;
  159. }
  160. }
  161. /*
  162. * If the extra blocks are not empty, then we are
  163. * either on the last block(s) or we need more
  164. * user input before continuing.
  165. */
  166. if (ctx->status & HASH_CTX_STS_LAST) {
  167. uint8_t *buf = ctx->partial_block_buffer;
  168. uint32_t n_extra_blocks = sha1_pad(buf, ctx->total_length);
  169. ctx->status = (HASH_CTX_STS_PROCESSING |
  170. HASH_CTX_STS_COMPLETE);
  171. ctx->job.buffer = buf;
  172. ctx->job.len = (uint32_t) n_extra_blocks;
  173. ctx = (struct sha1_hash_ctx *) sha1_job_mgr_submit(&mgr->mgr, &ctx->job);
  174. continue;
  175. }
  176. if (ctx)
  177. ctx->status = HASH_CTX_STS_IDLE;
  178. return ctx;
  179. }
  180. return NULL;
  181. }
  182. static struct sha1_hash_ctx *sha1_ctx_mgr_get_comp_ctx(struct sha1_ctx_mgr *mgr)
  183. {
  184. /*
  185. * If get_comp_job returns NULL, there are no jobs complete.
  186. * If get_comp_job returns a job, verify that it is safe to return to the user.
  187. * If it is not ready, resubmit the job to finish processing.
  188. * If sha1_ctx_mgr_resubmit returned a job, it is ready to be returned.
  189. * Otherwise, all jobs currently being managed by the hash_ctx_mgr still need processing.
  190. */
  191. struct sha1_hash_ctx *ctx;
  192. ctx = (struct sha1_hash_ctx *) sha1_job_mgr_get_comp_job(&mgr->mgr);
  193. return sha1_ctx_mgr_resubmit(mgr, ctx);
  194. }
  195. static void sha1_ctx_mgr_init(struct sha1_ctx_mgr *mgr)
  196. {
  197. sha1_job_mgr_init(&mgr->mgr);
  198. }
  199. static struct sha1_hash_ctx *sha1_ctx_mgr_submit(struct sha1_ctx_mgr *mgr,
  200. struct sha1_hash_ctx *ctx,
  201. const void *buffer,
  202. uint32_t len,
  203. int flags)
  204. {
  205. if (flags & (~HASH_ENTIRE)) {
  206. /* User should not pass anything other than FIRST, UPDATE, or LAST */
  207. ctx->error = HASH_CTX_ERROR_INVALID_FLAGS;
  208. return ctx;
  209. }
  210. if (ctx->status & HASH_CTX_STS_PROCESSING) {
  211. /* Cannot submit to a currently processing job. */
  212. ctx->error = HASH_CTX_ERROR_ALREADY_PROCESSING;
  213. return ctx;
  214. }
  215. if ((ctx->status & HASH_CTX_STS_COMPLETE) && !(flags & HASH_FIRST)) {
  216. /* Cannot update a finished job. */
  217. ctx->error = HASH_CTX_ERROR_ALREADY_COMPLETED;
  218. return ctx;
  219. }
  220. if (flags & HASH_FIRST) {
  221. /* Init digest */
  222. sha1_init_digest(ctx->job.result_digest);
  223. /* Reset byte counter */
  224. ctx->total_length = 0;
  225. /* Clear extra blocks */
  226. ctx->partial_block_buffer_length = 0;
  227. }
  228. /* If we made it here, there were no errors during this call to submit */
  229. ctx->error = HASH_CTX_ERROR_NONE;
  230. /* Store buffer ptr info from user */
  231. ctx->incoming_buffer = buffer;
  232. ctx->incoming_buffer_length = len;
  233. /* Store the user's request flags and mark this ctx as currently being processed. */
  234. ctx->status = (flags & HASH_LAST) ?
  235. (HASH_CTX_STS_PROCESSING | HASH_CTX_STS_LAST) :
  236. HASH_CTX_STS_PROCESSING;
  237. /* Advance byte counter */
  238. ctx->total_length += len;
  239. /*
  240. * If there is anything currently buffered in the extra blocks,
  241. * append to it until it contains a whole block.
  242. * Or if the user's buffer contains less than a whole block,
  243. * append as much as possible to the extra block.
  244. */
  245. if ((ctx->partial_block_buffer_length) | (len < SHA1_BLOCK_SIZE)) {
  246. /* Compute how many bytes to copy from user buffer into extra block */
  247. uint32_t copy_len = SHA1_BLOCK_SIZE - ctx->partial_block_buffer_length;
  248. if (len < copy_len)
  249. copy_len = len;
  250. if (copy_len) {
  251. /* Copy and update relevant pointers and counters */
  252. memcpy(&ctx->partial_block_buffer[ctx->partial_block_buffer_length],
  253. buffer, copy_len);
  254. ctx->partial_block_buffer_length += copy_len;
  255. ctx->incoming_buffer = (const void *)((const char *)buffer + copy_len);
  256. ctx->incoming_buffer_length = len - copy_len;
  257. }
  258. /* The extra block should never contain more than 1 block here */
  259. assert(ctx->partial_block_buffer_length <= SHA1_BLOCK_SIZE);
  260. /* If the extra block buffer contains exactly 1 block, it can be hashed. */
  261. if (ctx->partial_block_buffer_length >= SHA1_BLOCK_SIZE) {
  262. ctx->partial_block_buffer_length = 0;
  263. ctx->job.buffer = ctx->partial_block_buffer;
  264. ctx->job.len = 1;
  265. ctx = (struct sha1_hash_ctx *) sha1_job_mgr_submit(&mgr->mgr, &ctx->job);
  266. }
  267. }
  268. return sha1_ctx_mgr_resubmit(mgr, ctx);
  269. }
  270. static struct sha1_hash_ctx *sha1_ctx_mgr_flush(struct sha1_ctx_mgr *mgr)
  271. {
  272. struct sha1_hash_ctx *ctx;
  273. while (1) {
  274. ctx = (struct sha1_hash_ctx *) sha1_job_mgr_flush(&mgr->mgr);
  275. /* If flush returned 0, there are no more jobs in flight. */
  276. if (!ctx)
  277. return NULL;
  278. /*
  279. * If flush returned a job, resubmit the job to finish processing.
  280. */
  281. ctx = sha1_ctx_mgr_resubmit(mgr, ctx);
  282. /*
  283. * If sha1_ctx_mgr_resubmit returned a job, it is ready to be returned.
  284. * Otherwise, all jobs currently being managed by the sha1_ctx_mgr
  285. * still need processing. Loop.
  286. */
  287. if (ctx)
  288. return ctx;
  289. }
  290. }
  291. static int sha1_mb_init(struct shash_desc *desc)
  292. {
  293. struct sha1_hash_ctx *sctx = shash_desc_ctx(desc);
  294. hash_ctx_init(sctx);
  295. sctx->job.result_digest[0] = SHA1_H0;
  296. sctx->job.result_digest[1] = SHA1_H1;
  297. sctx->job.result_digest[2] = SHA1_H2;
  298. sctx->job.result_digest[3] = SHA1_H3;
  299. sctx->job.result_digest[4] = SHA1_H4;
  300. sctx->total_length = 0;
  301. sctx->partial_block_buffer_length = 0;
  302. sctx->status = HASH_CTX_STS_IDLE;
  303. return 0;
  304. }
  305. static int sha1_mb_set_results(struct mcryptd_hash_request_ctx *rctx)
  306. {
  307. int i;
  308. struct sha1_hash_ctx *sctx = shash_desc_ctx(&rctx->desc);
  309. __be32 *dst = (__be32 *) rctx->out;
  310. for (i = 0; i < 5; ++i)
  311. dst[i] = cpu_to_be32(sctx->job.result_digest[i]);
  312. return 0;
  313. }
  314. static int sha_finish_walk(struct mcryptd_hash_request_ctx **ret_rctx,
  315. struct mcryptd_alg_cstate *cstate, bool flush)
  316. {
  317. int flag = HASH_UPDATE;
  318. int nbytes, err = 0;
  319. struct mcryptd_hash_request_ctx *rctx = *ret_rctx;
  320. struct sha1_hash_ctx *sha_ctx;
  321. /* more work ? */
  322. while (!(rctx->flag & HASH_DONE)) {
  323. nbytes = crypto_ahash_walk_done(&rctx->walk, 0);
  324. if (nbytes < 0) {
  325. err = nbytes;
  326. goto out;
  327. }
  328. /* check if the walk is done */
  329. if (crypto_ahash_walk_last(&rctx->walk)) {
  330. rctx->flag |= HASH_DONE;
  331. if (rctx->flag & HASH_FINAL)
  332. flag |= HASH_LAST;
  333. }
  334. sha_ctx = (struct sha1_hash_ctx *) shash_desc_ctx(&rctx->desc);
  335. kernel_fpu_begin();
  336. sha_ctx = sha1_ctx_mgr_submit(cstate->mgr, sha_ctx, rctx->walk.data, nbytes, flag);
  337. if (!sha_ctx) {
  338. if (flush)
  339. sha_ctx = sha1_ctx_mgr_flush(cstate->mgr);
  340. }
  341. kernel_fpu_end();
  342. if (sha_ctx)
  343. rctx = cast_hash_to_mcryptd_ctx(sha_ctx);
  344. else {
  345. rctx = NULL;
  346. goto out;
  347. }
  348. }
  349. /* copy the results */
  350. if (rctx->flag & HASH_FINAL)
  351. sha1_mb_set_results(rctx);
  352. out:
  353. *ret_rctx = rctx;
  354. return err;
  355. }
  356. static int sha_complete_job(struct mcryptd_hash_request_ctx *rctx,
  357. struct mcryptd_alg_cstate *cstate,
  358. int err)
  359. {
  360. struct ahash_request *req = cast_mcryptd_ctx_to_req(rctx);
  361. struct sha1_hash_ctx *sha_ctx;
  362. struct mcryptd_hash_request_ctx *req_ctx;
  363. int ret;
  364. /* remove from work list */
  365. spin_lock(&cstate->work_lock);
  366. list_del(&rctx->waiter);
  367. spin_unlock(&cstate->work_lock);
  368. if (irqs_disabled())
  369. rctx->complete(&req->base, err);
  370. else {
  371. local_bh_disable();
  372. rctx->complete(&req->base, err);
  373. local_bh_enable();
  374. }
  375. /* check to see if there are other jobs that are done */
  376. sha_ctx = sha1_ctx_mgr_get_comp_ctx(cstate->mgr);
  377. while (sha_ctx) {
  378. req_ctx = cast_hash_to_mcryptd_ctx(sha_ctx);
  379. ret = sha_finish_walk(&req_ctx, cstate, false);
  380. if (req_ctx) {
  381. spin_lock(&cstate->work_lock);
  382. list_del(&req_ctx->waiter);
  383. spin_unlock(&cstate->work_lock);
  384. req = cast_mcryptd_ctx_to_req(req_ctx);
  385. if (irqs_disabled())
  386. rctx->complete(&req->base, ret);
  387. else {
  388. local_bh_disable();
  389. rctx->complete(&req->base, ret);
  390. local_bh_enable();
  391. }
  392. }
  393. sha_ctx = sha1_ctx_mgr_get_comp_ctx(cstate->mgr);
  394. }
  395. return 0;
  396. }
  397. static void sha1_mb_add_list(struct mcryptd_hash_request_ctx *rctx,
  398. struct mcryptd_alg_cstate *cstate)
  399. {
  400. unsigned long next_flush;
  401. unsigned long delay = usecs_to_jiffies(FLUSH_INTERVAL);
  402. /* initialize tag */
  403. rctx->tag.arrival = jiffies; /* tag the arrival time */
  404. rctx->tag.seq_num = cstate->next_seq_num++;
  405. next_flush = rctx->tag.arrival + delay;
  406. rctx->tag.expire = next_flush;
  407. spin_lock(&cstate->work_lock);
  408. list_add_tail(&rctx->waiter, &cstate->work_list);
  409. spin_unlock(&cstate->work_lock);
  410. mcryptd_arm_flusher(cstate, delay);
  411. }
  412. static int sha1_mb_update(struct shash_desc *desc, const u8 *data,
  413. unsigned int len)
  414. {
  415. struct mcryptd_hash_request_ctx *rctx =
  416. container_of(desc, struct mcryptd_hash_request_ctx, desc);
  417. struct mcryptd_alg_cstate *cstate =
  418. this_cpu_ptr(sha1_mb_alg_state.alg_cstate);
  419. struct ahash_request *req = cast_mcryptd_ctx_to_req(rctx);
  420. struct sha1_hash_ctx *sha_ctx;
  421. int ret = 0, nbytes;
  422. /* sanity check */
  423. if (rctx->tag.cpu != smp_processor_id()) {
  424. pr_err("mcryptd error: cpu clash\n");
  425. goto done;
  426. }
  427. /* need to init context */
  428. req_ctx_init(rctx, desc);
  429. nbytes = crypto_ahash_walk_first(req, &rctx->walk);
  430. if (nbytes < 0) {
  431. ret = nbytes;
  432. goto done;
  433. }
  434. if (crypto_ahash_walk_last(&rctx->walk))
  435. rctx->flag |= HASH_DONE;
  436. /* submit */
  437. sha_ctx = (struct sha1_hash_ctx *) shash_desc_ctx(desc);
  438. sha1_mb_add_list(rctx, cstate);
  439. kernel_fpu_begin();
  440. sha_ctx = sha1_ctx_mgr_submit(cstate->mgr, sha_ctx, rctx->walk.data, nbytes, HASH_UPDATE);
  441. kernel_fpu_end();
  442. /* check if anything is returned */
  443. if (!sha_ctx)
  444. return -EINPROGRESS;
  445. if (sha_ctx->error) {
  446. ret = sha_ctx->error;
  447. rctx = cast_hash_to_mcryptd_ctx(sha_ctx);
  448. goto done;
  449. }
  450. rctx = cast_hash_to_mcryptd_ctx(sha_ctx);
  451. ret = sha_finish_walk(&rctx, cstate, false);
  452. if (!rctx)
  453. return -EINPROGRESS;
  454. done:
  455. sha_complete_job(rctx, cstate, ret);
  456. return ret;
  457. }
  458. static int sha1_mb_finup(struct shash_desc *desc, const u8 *data,
  459. unsigned int len, u8 *out)
  460. {
  461. struct mcryptd_hash_request_ctx *rctx =
  462. container_of(desc, struct mcryptd_hash_request_ctx, desc);
  463. struct mcryptd_alg_cstate *cstate =
  464. this_cpu_ptr(sha1_mb_alg_state.alg_cstate);
  465. struct ahash_request *req = cast_mcryptd_ctx_to_req(rctx);
  466. struct sha1_hash_ctx *sha_ctx;
  467. int ret = 0, flag = HASH_UPDATE, nbytes;
  468. /* sanity check */
  469. if (rctx->tag.cpu != smp_processor_id()) {
  470. pr_err("mcryptd error: cpu clash\n");
  471. goto done;
  472. }
  473. /* need to init context */
  474. req_ctx_init(rctx, desc);
  475. nbytes = crypto_ahash_walk_first(req, &rctx->walk);
  476. if (nbytes < 0) {
  477. ret = nbytes;
  478. goto done;
  479. }
  480. if (crypto_ahash_walk_last(&rctx->walk)) {
  481. rctx->flag |= HASH_DONE;
  482. flag = HASH_LAST;
  483. }
  484. rctx->out = out;
  485. /* submit */
  486. rctx->flag |= HASH_FINAL;
  487. sha_ctx = (struct sha1_hash_ctx *) shash_desc_ctx(desc);
  488. sha1_mb_add_list(rctx, cstate);
  489. kernel_fpu_begin();
  490. sha_ctx = sha1_ctx_mgr_submit(cstate->mgr, sha_ctx, rctx->walk.data, nbytes, flag);
  491. kernel_fpu_end();
  492. /* check if anything is returned */
  493. if (!sha_ctx)
  494. return -EINPROGRESS;
  495. if (sha_ctx->error) {
  496. ret = sha_ctx->error;
  497. goto done;
  498. }
  499. rctx = cast_hash_to_mcryptd_ctx(sha_ctx);
  500. ret = sha_finish_walk(&rctx, cstate, false);
  501. if (!rctx)
  502. return -EINPROGRESS;
  503. done:
  504. sha_complete_job(rctx, cstate, ret);
  505. return ret;
  506. }
  507. static int sha1_mb_final(struct shash_desc *desc, u8 *out)
  508. {
  509. struct mcryptd_hash_request_ctx *rctx =
  510. container_of(desc, struct mcryptd_hash_request_ctx, desc);
  511. struct mcryptd_alg_cstate *cstate =
  512. this_cpu_ptr(sha1_mb_alg_state.alg_cstate);
  513. struct sha1_hash_ctx *sha_ctx;
  514. int ret = 0;
  515. u8 data;
  516. /* sanity check */
  517. if (rctx->tag.cpu != smp_processor_id()) {
  518. pr_err("mcryptd error: cpu clash\n");
  519. goto done;
  520. }
  521. /* need to init context */
  522. req_ctx_init(rctx, desc);
  523. rctx->out = out;
  524. rctx->flag |= HASH_DONE | HASH_FINAL;
  525. sha_ctx = (struct sha1_hash_ctx *) shash_desc_ctx(desc);
  526. /* flag HASH_FINAL and 0 data size */
  527. sha1_mb_add_list(rctx, cstate);
  528. kernel_fpu_begin();
  529. sha_ctx = sha1_ctx_mgr_submit(cstate->mgr, sha_ctx, &data, 0, HASH_LAST);
  530. kernel_fpu_end();
  531. /* check if anything is returned */
  532. if (!sha_ctx)
  533. return -EINPROGRESS;
  534. if (sha_ctx->error) {
  535. ret = sha_ctx->error;
  536. rctx = cast_hash_to_mcryptd_ctx(sha_ctx);
  537. goto done;
  538. }
  539. rctx = cast_hash_to_mcryptd_ctx(sha_ctx);
  540. ret = sha_finish_walk(&rctx, cstate, false);
  541. if (!rctx)
  542. return -EINPROGRESS;
  543. done:
  544. sha_complete_job(rctx, cstate, ret);
  545. return ret;
  546. }
  547. static int sha1_mb_export(struct shash_desc *desc, void *out)
  548. {
  549. struct sha1_hash_ctx *sctx = shash_desc_ctx(desc);
  550. memcpy(out, sctx, sizeof(*sctx));
  551. return 0;
  552. }
  553. static int sha1_mb_import(struct shash_desc *desc, const void *in)
  554. {
  555. struct sha1_hash_ctx *sctx = shash_desc_ctx(desc);
  556. memcpy(sctx, in, sizeof(*sctx));
  557. return 0;
  558. }
  559. static struct shash_alg sha1_mb_shash_alg = {
  560. .digestsize = SHA1_DIGEST_SIZE,
  561. .init = sha1_mb_init,
  562. .update = sha1_mb_update,
  563. .final = sha1_mb_final,
  564. .finup = sha1_mb_finup,
  565. .export = sha1_mb_export,
  566. .import = sha1_mb_import,
  567. .descsize = sizeof(struct sha1_hash_ctx),
  568. .statesize = sizeof(struct sha1_hash_ctx),
  569. .base = {
  570. .cra_name = "__sha1-mb",
  571. .cra_driver_name = "__intel_sha1-mb",
  572. .cra_priority = 100,
  573. /*
  574. * use ASYNC flag as some buffers in multi-buffer
  575. * algo may not have completed before hashing thread sleep
  576. */
  577. .cra_flags = CRYPTO_ALG_TYPE_SHASH | CRYPTO_ALG_ASYNC,
  578. .cra_blocksize = SHA1_BLOCK_SIZE,
  579. .cra_module = THIS_MODULE,
  580. .cra_list = LIST_HEAD_INIT(sha1_mb_shash_alg.base.cra_list),
  581. }
  582. };
  583. static int sha1_mb_async_init(struct ahash_request *req)
  584. {
  585. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  586. struct sha1_mb_ctx *ctx = crypto_ahash_ctx(tfm);
  587. struct ahash_request *mcryptd_req = ahash_request_ctx(req);
  588. struct mcryptd_ahash *mcryptd_tfm = ctx->mcryptd_tfm;
  589. memcpy(mcryptd_req, req, sizeof(*req));
  590. ahash_request_set_tfm(mcryptd_req, &mcryptd_tfm->base);
  591. return crypto_ahash_init(mcryptd_req);
  592. }
  593. static int sha1_mb_async_update(struct ahash_request *req)
  594. {
  595. struct ahash_request *mcryptd_req = ahash_request_ctx(req);
  596. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  597. struct sha1_mb_ctx *ctx = crypto_ahash_ctx(tfm);
  598. struct mcryptd_ahash *mcryptd_tfm = ctx->mcryptd_tfm;
  599. memcpy(mcryptd_req, req, sizeof(*req));
  600. ahash_request_set_tfm(mcryptd_req, &mcryptd_tfm->base);
  601. return crypto_ahash_update(mcryptd_req);
  602. }
  603. static int sha1_mb_async_finup(struct ahash_request *req)
  604. {
  605. struct ahash_request *mcryptd_req = ahash_request_ctx(req);
  606. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  607. struct sha1_mb_ctx *ctx = crypto_ahash_ctx(tfm);
  608. struct mcryptd_ahash *mcryptd_tfm = ctx->mcryptd_tfm;
  609. memcpy(mcryptd_req, req, sizeof(*req));
  610. ahash_request_set_tfm(mcryptd_req, &mcryptd_tfm->base);
  611. return crypto_ahash_finup(mcryptd_req);
  612. }
  613. static int sha1_mb_async_final(struct ahash_request *req)
  614. {
  615. struct ahash_request *mcryptd_req = ahash_request_ctx(req);
  616. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  617. struct sha1_mb_ctx *ctx = crypto_ahash_ctx(tfm);
  618. struct mcryptd_ahash *mcryptd_tfm = ctx->mcryptd_tfm;
  619. memcpy(mcryptd_req, req, sizeof(*req));
  620. ahash_request_set_tfm(mcryptd_req, &mcryptd_tfm->base);
  621. return crypto_ahash_final(mcryptd_req);
  622. }
  623. static int sha1_mb_async_digest(struct ahash_request *req)
  624. {
  625. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  626. struct sha1_mb_ctx *ctx = crypto_ahash_ctx(tfm);
  627. struct ahash_request *mcryptd_req = ahash_request_ctx(req);
  628. struct mcryptd_ahash *mcryptd_tfm = ctx->mcryptd_tfm;
  629. memcpy(mcryptd_req, req, sizeof(*req));
  630. ahash_request_set_tfm(mcryptd_req, &mcryptd_tfm->base);
  631. return crypto_ahash_digest(mcryptd_req);
  632. }
  633. static int sha1_mb_async_init_tfm(struct crypto_tfm *tfm)
  634. {
  635. struct mcryptd_ahash *mcryptd_tfm;
  636. struct sha1_mb_ctx *ctx = crypto_tfm_ctx(tfm);
  637. struct mcryptd_hash_ctx *mctx;
  638. mcryptd_tfm = mcryptd_alloc_ahash("__intel_sha1-mb", 0, 0);
  639. if (IS_ERR(mcryptd_tfm))
  640. return PTR_ERR(mcryptd_tfm);
  641. mctx = crypto_ahash_ctx(&mcryptd_tfm->base);
  642. mctx->alg_state = &sha1_mb_alg_state;
  643. ctx->mcryptd_tfm = mcryptd_tfm;
  644. crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
  645. sizeof(struct ahash_request) +
  646. crypto_ahash_reqsize(&mcryptd_tfm->base));
  647. return 0;
  648. }
  649. static void sha1_mb_async_exit_tfm(struct crypto_tfm *tfm)
  650. {
  651. struct sha1_mb_ctx *ctx = crypto_tfm_ctx(tfm);
  652. mcryptd_free_ahash(ctx->mcryptd_tfm);
  653. }
  654. static struct ahash_alg sha1_mb_async_alg = {
  655. .init = sha1_mb_async_init,
  656. .update = sha1_mb_async_update,
  657. .final = sha1_mb_async_final,
  658. .finup = sha1_mb_async_finup,
  659. .digest = sha1_mb_async_digest,
  660. .halg = {
  661. .digestsize = SHA1_DIGEST_SIZE,
  662. .base = {
  663. .cra_name = "sha1",
  664. .cra_driver_name = "sha1_mb",
  665. .cra_priority = 200,
  666. .cra_flags = CRYPTO_ALG_TYPE_AHASH | CRYPTO_ALG_ASYNC,
  667. .cra_blocksize = SHA1_BLOCK_SIZE,
  668. .cra_type = &crypto_ahash_type,
  669. .cra_module = THIS_MODULE,
  670. .cra_list = LIST_HEAD_INIT(sha1_mb_async_alg.halg.base.cra_list),
  671. .cra_init = sha1_mb_async_init_tfm,
  672. .cra_exit = sha1_mb_async_exit_tfm,
  673. .cra_ctxsize = sizeof(struct sha1_mb_ctx),
  674. .cra_alignmask = 0,
  675. },
  676. },
  677. };
  678. static unsigned long sha1_mb_flusher(struct mcryptd_alg_cstate *cstate)
  679. {
  680. struct mcryptd_hash_request_ctx *rctx;
  681. unsigned long cur_time;
  682. unsigned long next_flush = 0;
  683. struct sha1_hash_ctx *sha_ctx;
  684. cur_time = jiffies;
  685. while (!list_empty(&cstate->work_list)) {
  686. rctx = list_entry(cstate->work_list.next,
  687. struct mcryptd_hash_request_ctx, waiter);
  688. if time_before(cur_time, rctx->tag.expire)
  689. break;
  690. kernel_fpu_begin();
  691. sha_ctx = (struct sha1_hash_ctx *) sha1_ctx_mgr_flush(cstate->mgr);
  692. kernel_fpu_end();
  693. if (!sha_ctx) {
  694. pr_err("sha1_mb error: nothing got flushed for non-empty list\n");
  695. break;
  696. }
  697. rctx = cast_hash_to_mcryptd_ctx(sha_ctx);
  698. sha_finish_walk(&rctx, cstate, true);
  699. sha_complete_job(rctx, cstate, 0);
  700. }
  701. if (!list_empty(&cstate->work_list)) {
  702. rctx = list_entry(cstate->work_list.next,
  703. struct mcryptd_hash_request_ctx, waiter);
  704. /* get the hash context and then flush time */
  705. next_flush = rctx->tag.expire;
  706. mcryptd_arm_flusher(cstate, get_delay(next_flush));
  707. }
  708. return next_flush;
  709. }
  710. static int __init sha1_mb_mod_init(void)
  711. {
  712. int cpu;
  713. int err;
  714. struct mcryptd_alg_cstate *cpu_state;
  715. /* check for dependent cpu features */
  716. if (!boot_cpu_has(X86_FEATURE_AVX2) ||
  717. !boot_cpu_has(X86_FEATURE_BMI2))
  718. return -ENODEV;
  719. /* initialize multibuffer structures */
  720. sha1_mb_alg_state.alg_cstate = alloc_percpu(struct mcryptd_alg_cstate);
  721. sha1_job_mgr_init = sha1_mb_mgr_init_avx2;
  722. sha1_job_mgr_submit = sha1_mb_mgr_submit_avx2;
  723. sha1_job_mgr_flush = sha1_mb_mgr_flush_avx2;
  724. sha1_job_mgr_get_comp_job = sha1_mb_mgr_get_comp_job_avx2;
  725. if (!sha1_mb_alg_state.alg_cstate)
  726. return -ENOMEM;
  727. for_each_possible_cpu(cpu) {
  728. cpu_state = per_cpu_ptr(sha1_mb_alg_state.alg_cstate, cpu);
  729. cpu_state->next_flush = 0;
  730. cpu_state->next_seq_num = 0;
  731. cpu_state->flusher_engaged = false;
  732. INIT_DELAYED_WORK(&cpu_state->flush, mcryptd_flusher);
  733. cpu_state->cpu = cpu;
  734. cpu_state->alg_state = &sha1_mb_alg_state;
  735. cpu_state->mgr = (struct sha1_ctx_mgr *) kzalloc(sizeof(struct sha1_ctx_mgr), 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");