sha256_mb.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031
  1. /*
  2. * Multi buffer SHA256 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) 2016 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. * Megha Dey <megha.dey@linux.intel.com>
  22. *
  23. * BSD LICENSE
  24. *
  25. * Copyright(c) 2016 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 "sha256_mb_ctx.h"
  69. #define FLUSH_INTERVAL 1000 /* in usec */
  70. static struct mcryptd_alg_state sha256_mb_alg_state;
  71. struct sha256_mb_ctx {
  72. struct mcryptd_ahash *mcryptd_tfm;
  73. };
  74. static inline struct mcryptd_hash_request_ctx
  75. *cast_hash_to_mcryptd_ctx(struct sha256_hash_ctx *hash_ctx)
  76. {
  77. struct ahash_request *areq;
  78. areq = container_of((void *) hash_ctx, struct ahash_request, __ctx);
  79. return container_of(areq, struct mcryptd_hash_request_ctx, areq);
  80. }
  81. static inline struct ahash_request
  82. *cast_mcryptd_ctx_to_req(struct mcryptd_hash_request_ctx *ctx)
  83. {
  84. return container_of((void *) ctx, struct ahash_request, __ctx);
  85. }
  86. static void req_ctx_init(struct mcryptd_hash_request_ctx *rctx,
  87. struct ahash_request *areq)
  88. {
  89. rctx->flag = HASH_UPDATE;
  90. }
  91. static asmlinkage void (*sha256_job_mgr_init)(struct sha256_mb_mgr *state);
  92. static asmlinkage struct job_sha256* (*sha256_job_mgr_submit)
  93. (struct sha256_mb_mgr *state, struct job_sha256 *job);
  94. static asmlinkage struct job_sha256* (*sha256_job_mgr_flush)
  95. (struct sha256_mb_mgr *state);
  96. static asmlinkage struct job_sha256* (*sha256_job_mgr_get_comp_job)
  97. (struct sha256_mb_mgr *state);
  98. inline void sha256_init_digest(uint32_t *digest)
  99. {
  100. static const uint32_t initial_digest[SHA256_DIGEST_LENGTH] = {
  101. SHA256_H0, SHA256_H1, SHA256_H2, SHA256_H3,
  102. SHA256_H4, SHA256_H5, SHA256_H6, SHA256_H7};
  103. memcpy(digest, initial_digest, sizeof(initial_digest));
  104. }
  105. inline uint32_t sha256_pad(uint8_t padblock[SHA256_BLOCK_SIZE * 2],
  106. uint32_t total_len)
  107. {
  108. uint32_t i = total_len & (SHA256_BLOCK_SIZE - 1);
  109. memset(&padblock[i], 0, SHA256_BLOCK_SIZE);
  110. padblock[i] = 0x80;
  111. i += ((SHA256_BLOCK_SIZE - 1) &
  112. (0 - (total_len + SHA256_PADLENGTHFIELD_SIZE + 1)))
  113. + 1 + SHA256_PADLENGTHFIELD_SIZE;
  114. #if SHA256_PADLENGTHFIELD_SIZE == 16
  115. *((uint64_t *) &padblock[i - 16]) = 0;
  116. #endif
  117. *((uint64_t *) &padblock[i - 8]) = cpu_to_be64(total_len << 3);
  118. /* Number of extra blocks to hash */
  119. return i >> SHA256_LOG2_BLOCK_SIZE;
  120. }
  121. static struct sha256_hash_ctx
  122. *sha256_ctx_mgr_resubmit(struct sha256_ctx_mgr *mgr,
  123. struct sha256_hash_ctx *ctx)
  124. {
  125. while (ctx) {
  126. if (ctx->status & HASH_CTX_STS_COMPLETE) {
  127. /* Clear PROCESSING bit */
  128. ctx->status = HASH_CTX_STS_COMPLETE;
  129. return ctx;
  130. }
  131. /*
  132. * If the extra blocks are empty, begin hashing what remains
  133. * in the user's buffer.
  134. */
  135. if (ctx->partial_block_buffer_length == 0 &&
  136. ctx->incoming_buffer_length) {
  137. const void *buffer = ctx->incoming_buffer;
  138. uint32_t len = ctx->incoming_buffer_length;
  139. uint32_t copy_len;
  140. /*
  141. * Only entire blocks can be hashed.
  142. * Copy remainder to extra blocks buffer.
  143. */
  144. copy_len = len & (SHA256_BLOCK_SIZE-1);
  145. if (copy_len) {
  146. len -= copy_len;
  147. memcpy(ctx->partial_block_buffer,
  148. ((const char *) buffer + len),
  149. copy_len);
  150. ctx->partial_block_buffer_length = copy_len;
  151. }
  152. ctx->incoming_buffer_length = 0;
  153. /* len should be a multiple of the block size now */
  154. assert((len % SHA256_BLOCK_SIZE) == 0);
  155. /* Set len to the number of blocks to be hashed */
  156. len >>= SHA256_LOG2_BLOCK_SIZE;
  157. if (len) {
  158. ctx->job.buffer = (uint8_t *) buffer;
  159. ctx->job.len = len;
  160. ctx = (struct sha256_hash_ctx *)
  161. sha256_job_mgr_submit(&mgr->mgr, &ctx->job);
  162. continue;
  163. }
  164. }
  165. /*
  166. * If the extra blocks are not empty, then we are
  167. * either on the last block(s) or we need more
  168. * user input before continuing.
  169. */
  170. if (ctx->status & HASH_CTX_STS_LAST) {
  171. uint8_t *buf = ctx->partial_block_buffer;
  172. uint32_t n_extra_blocks =
  173. sha256_pad(buf, ctx->total_length);
  174. ctx->status = (HASH_CTX_STS_PROCESSING |
  175. HASH_CTX_STS_COMPLETE);
  176. ctx->job.buffer = buf;
  177. ctx->job.len = (uint32_t) n_extra_blocks;
  178. ctx = (struct sha256_hash_ctx *)
  179. sha256_job_mgr_submit(&mgr->mgr, &ctx->job);
  180. continue;
  181. }
  182. ctx->status = HASH_CTX_STS_IDLE;
  183. return ctx;
  184. }
  185. return NULL;
  186. }
  187. static struct sha256_hash_ctx
  188. *sha256_ctx_mgr_get_comp_ctx(struct sha256_ctx_mgr *mgr)
  189. {
  190. /*
  191. * If get_comp_job returns NULL, there are no jobs complete.
  192. * If get_comp_job returns a job, verify that it is safe to return to
  193. * the user. If it is not ready, resubmit the job to finish processing.
  194. * If sha256_ctx_mgr_resubmit returned a job, it is ready to be
  195. * returned. Otherwise, all jobs currently being managed by the
  196. * hash_ctx_mgr still need processing.
  197. */
  198. struct sha256_hash_ctx *ctx;
  199. ctx = (struct sha256_hash_ctx *) sha256_job_mgr_get_comp_job(&mgr->mgr);
  200. return sha256_ctx_mgr_resubmit(mgr, ctx);
  201. }
  202. static void sha256_ctx_mgr_init(struct sha256_ctx_mgr *mgr)
  203. {
  204. sha256_job_mgr_init(&mgr->mgr);
  205. }
  206. static struct sha256_hash_ctx *sha256_ctx_mgr_submit(struct sha256_ctx_mgr *mgr,
  207. struct sha256_hash_ctx *ctx,
  208. const void *buffer,
  209. uint32_t len,
  210. int flags)
  211. {
  212. if (flags & (~HASH_ENTIRE)) {
  213. /* User should not pass anything other than FIRST, UPDATE
  214. * or LAST
  215. */
  216. ctx->error = HASH_CTX_ERROR_INVALID_FLAGS;
  217. return ctx;
  218. }
  219. if (ctx->status & HASH_CTX_STS_PROCESSING) {
  220. /* Cannot submit to a currently processing job. */
  221. ctx->error = HASH_CTX_ERROR_ALREADY_PROCESSING;
  222. return ctx;
  223. }
  224. if ((ctx->status & HASH_CTX_STS_COMPLETE) && !(flags & HASH_FIRST)) {
  225. /* Cannot update a finished job. */
  226. ctx->error = HASH_CTX_ERROR_ALREADY_COMPLETED;
  227. return ctx;
  228. }
  229. if (flags & HASH_FIRST) {
  230. /* Init digest */
  231. sha256_init_digest(ctx->job.result_digest);
  232. /* Reset byte counter */
  233. ctx->total_length = 0;
  234. /* Clear extra blocks */
  235. ctx->partial_block_buffer_length = 0;
  236. }
  237. /* If we made it here, there was no error during this call to submit */
  238. ctx->error = HASH_CTX_ERROR_NONE;
  239. /* Store buffer ptr info from user */
  240. ctx->incoming_buffer = buffer;
  241. ctx->incoming_buffer_length = len;
  242. /*
  243. * Store the user's request flags and mark this ctx as currently
  244. * being processed.
  245. */
  246. ctx->status = (flags & HASH_LAST) ?
  247. (HASH_CTX_STS_PROCESSING | HASH_CTX_STS_LAST) :
  248. HASH_CTX_STS_PROCESSING;
  249. /* Advance byte counter */
  250. ctx->total_length += len;
  251. /*
  252. * If there is anything currently buffered in the extra blocks,
  253. * append to it until it contains a whole block.
  254. * Or if the user's buffer contains less than a whole block,
  255. * append as much as possible to the extra block.
  256. */
  257. if (ctx->partial_block_buffer_length || len < SHA256_BLOCK_SIZE) {
  258. /*
  259. * Compute how many bytes to copy from user buffer into
  260. * extra block
  261. */
  262. uint32_t copy_len = SHA256_BLOCK_SIZE -
  263. ctx->partial_block_buffer_length;
  264. if (len < copy_len)
  265. copy_len = len;
  266. if (copy_len) {
  267. /* Copy and update relevant pointers and counters */
  268. memcpy(
  269. &ctx->partial_block_buffer[ctx->partial_block_buffer_length],
  270. buffer, copy_len);
  271. ctx->partial_block_buffer_length += copy_len;
  272. ctx->incoming_buffer = (const void *)
  273. ((const char *)buffer + copy_len);
  274. ctx->incoming_buffer_length = len - copy_len;
  275. }
  276. /* The extra block should never contain more than 1 block */
  277. assert(ctx->partial_block_buffer_length <= SHA256_BLOCK_SIZE);
  278. /*
  279. * If the extra block buffer contains exactly 1 block,
  280. * it can be hashed.
  281. */
  282. if (ctx->partial_block_buffer_length >= SHA256_BLOCK_SIZE) {
  283. ctx->partial_block_buffer_length = 0;
  284. ctx->job.buffer = ctx->partial_block_buffer;
  285. ctx->job.len = 1;
  286. ctx = (struct sha256_hash_ctx *)
  287. sha256_job_mgr_submit(&mgr->mgr, &ctx->job);
  288. }
  289. }
  290. return sha256_ctx_mgr_resubmit(mgr, ctx);
  291. }
  292. static struct sha256_hash_ctx *sha256_ctx_mgr_flush(struct sha256_ctx_mgr *mgr)
  293. {
  294. struct sha256_hash_ctx *ctx;
  295. while (1) {
  296. ctx = (struct sha256_hash_ctx *)
  297. sha256_job_mgr_flush(&mgr->mgr);
  298. /* If flush returned 0, there are no more jobs in flight. */
  299. if (!ctx)
  300. return NULL;
  301. /*
  302. * If flush returned a job, resubmit the job to finish
  303. * processing.
  304. */
  305. ctx = sha256_ctx_mgr_resubmit(mgr, ctx);
  306. /*
  307. * If sha256_ctx_mgr_resubmit returned a job, it is ready to
  308. * be returned. Otherwise, all jobs currently being managed by
  309. * the sha256_ctx_mgr still need processing. Loop.
  310. */
  311. if (ctx)
  312. return ctx;
  313. }
  314. }
  315. static int sha256_mb_init(struct ahash_request *areq)
  316. {
  317. struct sha256_hash_ctx *sctx = ahash_request_ctx(areq);
  318. hash_ctx_init(sctx);
  319. sctx->job.result_digest[0] = SHA256_H0;
  320. sctx->job.result_digest[1] = SHA256_H1;
  321. sctx->job.result_digest[2] = SHA256_H2;
  322. sctx->job.result_digest[3] = SHA256_H3;
  323. sctx->job.result_digest[4] = SHA256_H4;
  324. sctx->job.result_digest[5] = SHA256_H5;
  325. sctx->job.result_digest[6] = SHA256_H6;
  326. sctx->job.result_digest[7] = SHA256_H7;
  327. sctx->total_length = 0;
  328. sctx->partial_block_buffer_length = 0;
  329. sctx->status = HASH_CTX_STS_IDLE;
  330. return 0;
  331. }
  332. static int sha256_mb_set_results(struct mcryptd_hash_request_ctx *rctx)
  333. {
  334. int i;
  335. struct sha256_hash_ctx *sctx = ahash_request_ctx(&rctx->areq);
  336. __be32 *dst = (__be32 *) rctx->out;
  337. for (i = 0; i < 8; ++i)
  338. dst[i] = cpu_to_be32(sctx->job.result_digest[i]);
  339. return 0;
  340. }
  341. static int sha_finish_walk(struct mcryptd_hash_request_ctx **ret_rctx,
  342. struct mcryptd_alg_cstate *cstate, bool flush)
  343. {
  344. int flag = HASH_UPDATE;
  345. int nbytes, err = 0;
  346. struct mcryptd_hash_request_ctx *rctx = *ret_rctx;
  347. struct sha256_hash_ctx *sha_ctx;
  348. /* more work ? */
  349. while (!(rctx->flag & HASH_DONE)) {
  350. nbytes = crypto_ahash_walk_done(&rctx->walk, 0);
  351. if (nbytes < 0) {
  352. err = nbytes;
  353. goto out;
  354. }
  355. /* check if the walk is done */
  356. if (crypto_ahash_walk_last(&rctx->walk)) {
  357. rctx->flag |= HASH_DONE;
  358. if (rctx->flag & HASH_FINAL)
  359. flag |= HASH_LAST;
  360. }
  361. sha_ctx = (struct sha256_hash_ctx *)
  362. ahash_request_ctx(&rctx->areq);
  363. kernel_fpu_begin();
  364. sha_ctx = sha256_ctx_mgr_submit(cstate->mgr, sha_ctx,
  365. rctx->walk.data, nbytes, flag);
  366. if (!sha_ctx) {
  367. if (flush)
  368. sha_ctx = sha256_ctx_mgr_flush(cstate->mgr);
  369. }
  370. kernel_fpu_end();
  371. if (sha_ctx)
  372. rctx = cast_hash_to_mcryptd_ctx(sha_ctx);
  373. else {
  374. rctx = NULL;
  375. goto out;
  376. }
  377. }
  378. /* copy the results */
  379. if (rctx->flag & HASH_FINAL)
  380. sha256_mb_set_results(rctx);
  381. out:
  382. *ret_rctx = rctx;
  383. return err;
  384. }
  385. static int sha_complete_job(struct mcryptd_hash_request_ctx *rctx,
  386. struct mcryptd_alg_cstate *cstate,
  387. int err)
  388. {
  389. struct ahash_request *req = cast_mcryptd_ctx_to_req(rctx);
  390. struct sha256_hash_ctx *sha_ctx;
  391. struct mcryptd_hash_request_ctx *req_ctx;
  392. int ret;
  393. /* remove from work list */
  394. spin_lock(&cstate->work_lock);
  395. list_del(&rctx->waiter);
  396. spin_unlock(&cstate->work_lock);
  397. if (irqs_disabled())
  398. rctx->complete(&req->base, err);
  399. else {
  400. local_bh_disable();
  401. rctx->complete(&req->base, err);
  402. local_bh_enable();
  403. }
  404. /* check to see if there are other jobs that are done */
  405. sha_ctx = sha256_ctx_mgr_get_comp_ctx(cstate->mgr);
  406. while (sha_ctx) {
  407. req_ctx = cast_hash_to_mcryptd_ctx(sha_ctx);
  408. ret = sha_finish_walk(&req_ctx, cstate, false);
  409. if (req_ctx) {
  410. spin_lock(&cstate->work_lock);
  411. list_del(&req_ctx->waiter);
  412. spin_unlock(&cstate->work_lock);
  413. req = cast_mcryptd_ctx_to_req(req_ctx);
  414. if (irqs_disabled())
  415. req_ctx->complete(&req->base, ret);
  416. else {
  417. local_bh_disable();
  418. req_ctx->complete(&req->base, ret);
  419. local_bh_enable();
  420. }
  421. }
  422. sha_ctx = sha256_ctx_mgr_get_comp_ctx(cstate->mgr);
  423. }
  424. return 0;
  425. }
  426. static void sha256_mb_add_list(struct mcryptd_hash_request_ctx *rctx,
  427. struct mcryptd_alg_cstate *cstate)
  428. {
  429. unsigned long next_flush;
  430. unsigned long delay = usecs_to_jiffies(FLUSH_INTERVAL);
  431. /* initialize tag */
  432. rctx->tag.arrival = jiffies; /* tag the arrival time */
  433. rctx->tag.seq_num = cstate->next_seq_num++;
  434. next_flush = rctx->tag.arrival + delay;
  435. rctx->tag.expire = next_flush;
  436. spin_lock(&cstate->work_lock);
  437. list_add_tail(&rctx->waiter, &cstate->work_list);
  438. spin_unlock(&cstate->work_lock);
  439. mcryptd_arm_flusher(cstate, delay);
  440. }
  441. static int sha256_mb_update(struct ahash_request *areq)
  442. {
  443. struct mcryptd_hash_request_ctx *rctx =
  444. container_of(areq, struct mcryptd_hash_request_ctx, areq);
  445. struct mcryptd_alg_cstate *cstate =
  446. this_cpu_ptr(sha256_mb_alg_state.alg_cstate);
  447. struct ahash_request *req = cast_mcryptd_ctx_to_req(rctx);
  448. struct sha256_hash_ctx *sha_ctx;
  449. int ret = 0, nbytes;
  450. /* sanity check */
  451. if (rctx->tag.cpu != smp_processor_id()) {
  452. pr_err("mcryptd error: cpu clash\n");
  453. goto done;
  454. }
  455. /* need to init context */
  456. req_ctx_init(rctx, areq);
  457. nbytes = crypto_ahash_walk_first(req, &rctx->walk);
  458. if (nbytes < 0) {
  459. ret = nbytes;
  460. goto done;
  461. }
  462. if (crypto_ahash_walk_last(&rctx->walk))
  463. rctx->flag |= HASH_DONE;
  464. /* submit */
  465. sha_ctx = (struct sha256_hash_ctx *) ahash_request_ctx(areq);
  466. sha256_mb_add_list(rctx, cstate);
  467. kernel_fpu_begin();
  468. sha_ctx = sha256_ctx_mgr_submit(cstate->mgr, sha_ctx, rctx->walk.data,
  469. nbytes, HASH_UPDATE);
  470. kernel_fpu_end();
  471. /* check if anything is returned */
  472. if (!sha_ctx)
  473. return -EINPROGRESS;
  474. if (sha_ctx->error) {
  475. ret = sha_ctx->error;
  476. rctx = cast_hash_to_mcryptd_ctx(sha_ctx);
  477. goto done;
  478. }
  479. rctx = cast_hash_to_mcryptd_ctx(sha_ctx);
  480. ret = sha_finish_walk(&rctx, cstate, false);
  481. if (!rctx)
  482. return -EINPROGRESS;
  483. done:
  484. sha_complete_job(rctx, cstate, ret);
  485. return ret;
  486. }
  487. static int sha256_mb_finup(struct ahash_request *areq)
  488. {
  489. struct mcryptd_hash_request_ctx *rctx =
  490. container_of(areq, struct mcryptd_hash_request_ctx, areq);
  491. struct mcryptd_alg_cstate *cstate =
  492. this_cpu_ptr(sha256_mb_alg_state.alg_cstate);
  493. struct ahash_request *req = cast_mcryptd_ctx_to_req(rctx);
  494. struct sha256_hash_ctx *sha_ctx;
  495. int ret = 0, flag = HASH_UPDATE, nbytes;
  496. /* sanity check */
  497. if (rctx->tag.cpu != smp_processor_id()) {
  498. pr_err("mcryptd error: cpu clash\n");
  499. goto done;
  500. }
  501. /* need to init context */
  502. req_ctx_init(rctx, areq);
  503. nbytes = crypto_ahash_walk_first(req, &rctx->walk);
  504. if (nbytes < 0) {
  505. ret = nbytes;
  506. goto done;
  507. }
  508. if (crypto_ahash_walk_last(&rctx->walk)) {
  509. rctx->flag |= HASH_DONE;
  510. flag = HASH_LAST;
  511. }
  512. /* submit */
  513. rctx->flag |= HASH_FINAL;
  514. sha_ctx = (struct sha256_hash_ctx *) ahash_request_ctx(areq);
  515. sha256_mb_add_list(rctx, cstate);
  516. kernel_fpu_begin();
  517. sha_ctx = sha256_ctx_mgr_submit(cstate->mgr, sha_ctx, rctx->walk.data,
  518. nbytes, flag);
  519. kernel_fpu_end();
  520. /* check if anything is returned */
  521. if (!sha_ctx)
  522. return -EINPROGRESS;
  523. if (sha_ctx->error) {
  524. ret = sha_ctx->error;
  525. goto done;
  526. }
  527. rctx = cast_hash_to_mcryptd_ctx(sha_ctx);
  528. ret = sha_finish_walk(&rctx, cstate, false);
  529. if (!rctx)
  530. return -EINPROGRESS;
  531. done:
  532. sha_complete_job(rctx, cstate, ret);
  533. return ret;
  534. }
  535. static int sha256_mb_final(struct ahash_request *areq)
  536. {
  537. struct mcryptd_hash_request_ctx *rctx =
  538. container_of(areq, struct mcryptd_hash_request_ctx,
  539. areq);
  540. struct mcryptd_alg_cstate *cstate =
  541. this_cpu_ptr(sha256_mb_alg_state.alg_cstate);
  542. struct sha256_hash_ctx *sha_ctx;
  543. int ret = 0;
  544. u8 data;
  545. /* sanity check */
  546. if (rctx->tag.cpu != smp_processor_id()) {
  547. pr_err("mcryptd error: cpu clash\n");
  548. goto done;
  549. }
  550. /* need to init context */
  551. req_ctx_init(rctx, areq);
  552. rctx->flag |= HASH_DONE | HASH_FINAL;
  553. sha_ctx = (struct sha256_hash_ctx *) ahash_request_ctx(areq);
  554. /* flag HASH_FINAL and 0 data size */
  555. sha256_mb_add_list(rctx, cstate);
  556. kernel_fpu_begin();
  557. sha_ctx = sha256_ctx_mgr_submit(cstate->mgr, sha_ctx, &data, 0,
  558. HASH_LAST);
  559. kernel_fpu_end();
  560. /* check if anything is returned */
  561. if (!sha_ctx)
  562. return -EINPROGRESS;
  563. if (sha_ctx->error) {
  564. ret = sha_ctx->error;
  565. rctx = cast_hash_to_mcryptd_ctx(sha_ctx);
  566. goto done;
  567. }
  568. rctx = cast_hash_to_mcryptd_ctx(sha_ctx);
  569. ret = sha_finish_walk(&rctx, cstate, false);
  570. if (!rctx)
  571. return -EINPROGRESS;
  572. done:
  573. sha_complete_job(rctx, cstate, ret);
  574. return ret;
  575. }
  576. static int sha256_mb_export(struct ahash_request *areq, void *out)
  577. {
  578. struct sha256_hash_ctx *sctx = ahash_request_ctx(areq);
  579. memcpy(out, sctx, sizeof(*sctx));
  580. return 0;
  581. }
  582. static int sha256_mb_import(struct ahash_request *areq, const void *in)
  583. {
  584. struct sha256_hash_ctx *sctx = ahash_request_ctx(areq);
  585. memcpy(sctx, in, sizeof(*sctx));
  586. return 0;
  587. }
  588. static int sha256_mb_async_init_tfm(struct crypto_tfm *tfm)
  589. {
  590. struct mcryptd_ahash *mcryptd_tfm;
  591. struct sha256_mb_ctx *ctx = crypto_tfm_ctx(tfm);
  592. struct mcryptd_hash_ctx *mctx;
  593. mcryptd_tfm = mcryptd_alloc_ahash("__intel_sha256-mb",
  594. CRYPTO_ALG_INTERNAL,
  595. CRYPTO_ALG_INTERNAL);
  596. if (IS_ERR(mcryptd_tfm))
  597. return PTR_ERR(mcryptd_tfm);
  598. mctx = crypto_ahash_ctx(&mcryptd_tfm->base);
  599. mctx->alg_state = &sha256_mb_alg_state;
  600. ctx->mcryptd_tfm = mcryptd_tfm;
  601. crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
  602. sizeof(struct ahash_request) +
  603. crypto_ahash_reqsize(&mcryptd_tfm->base));
  604. return 0;
  605. }
  606. static void sha256_mb_async_exit_tfm(struct crypto_tfm *tfm)
  607. {
  608. struct sha256_mb_ctx *ctx = crypto_tfm_ctx(tfm);
  609. mcryptd_free_ahash(ctx->mcryptd_tfm);
  610. }
  611. static int sha256_mb_areq_init_tfm(struct crypto_tfm *tfm)
  612. {
  613. crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
  614. sizeof(struct ahash_request) +
  615. sizeof(struct sha256_hash_ctx));
  616. return 0;
  617. }
  618. static void sha256_mb_areq_exit_tfm(struct crypto_tfm *tfm)
  619. {
  620. struct sha256_mb_ctx *ctx = crypto_tfm_ctx(tfm);
  621. mcryptd_free_ahash(ctx->mcryptd_tfm);
  622. }
  623. static struct ahash_alg sha256_mb_areq_alg = {
  624. .init = sha256_mb_init,
  625. .update = sha256_mb_update,
  626. .final = sha256_mb_final,
  627. .finup = sha256_mb_finup,
  628. .export = sha256_mb_export,
  629. .import = sha256_mb_import,
  630. .halg = {
  631. .digestsize = SHA256_DIGEST_SIZE,
  632. .statesize = sizeof(struct sha256_hash_ctx),
  633. .base = {
  634. .cra_name = "__sha256-mb",
  635. .cra_driver_name = "__intel_sha256-mb",
  636. .cra_priority = 100,
  637. /*
  638. * use ASYNC flag as some buffers in multi-buffer
  639. * algo may not have completed before hashing thread
  640. * sleep
  641. */
  642. .cra_flags = CRYPTO_ALG_TYPE_AHASH |
  643. CRYPTO_ALG_ASYNC |
  644. CRYPTO_ALG_INTERNAL,
  645. .cra_blocksize = SHA256_BLOCK_SIZE,
  646. .cra_module = THIS_MODULE,
  647. .cra_list = LIST_HEAD_INIT
  648. (sha256_mb_areq_alg.halg.base.cra_list),
  649. .cra_init = sha256_mb_areq_init_tfm,
  650. .cra_exit = sha256_mb_areq_exit_tfm,
  651. .cra_ctxsize = sizeof(struct sha256_hash_ctx),
  652. }
  653. }
  654. };
  655. static int sha256_mb_async_init(struct ahash_request *req)
  656. {
  657. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  658. struct sha256_mb_ctx *ctx = crypto_ahash_ctx(tfm);
  659. struct ahash_request *mcryptd_req = ahash_request_ctx(req);
  660. struct mcryptd_ahash *mcryptd_tfm = ctx->mcryptd_tfm;
  661. memcpy(mcryptd_req, req, sizeof(*req));
  662. ahash_request_set_tfm(mcryptd_req, &mcryptd_tfm->base);
  663. return crypto_ahash_init(mcryptd_req);
  664. }
  665. static int sha256_mb_async_update(struct ahash_request *req)
  666. {
  667. struct ahash_request *mcryptd_req = ahash_request_ctx(req);
  668. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  669. struct sha256_mb_ctx *ctx = crypto_ahash_ctx(tfm);
  670. struct mcryptd_ahash *mcryptd_tfm = ctx->mcryptd_tfm;
  671. memcpy(mcryptd_req, req, sizeof(*req));
  672. ahash_request_set_tfm(mcryptd_req, &mcryptd_tfm->base);
  673. return crypto_ahash_update(mcryptd_req);
  674. }
  675. static int sha256_mb_async_finup(struct ahash_request *req)
  676. {
  677. struct ahash_request *mcryptd_req = ahash_request_ctx(req);
  678. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  679. struct sha256_mb_ctx *ctx = crypto_ahash_ctx(tfm);
  680. struct mcryptd_ahash *mcryptd_tfm = ctx->mcryptd_tfm;
  681. memcpy(mcryptd_req, req, sizeof(*req));
  682. ahash_request_set_tfm(mcryptd_req, &mcryptd_tfm->base);
  683. return crypto_ahash_finup(mcryptd_req);
  684. }
  685. static int sha256_mb_async_final(struct ahash_request *req)
  686. {
  687. struct ahash_request *mcryptd_req = ahash_request_ctx(req);
  688. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  689. struct sha256_mb_ctx *ctx = crypto_ahash_ctx(tfm);
  690. struct mcryptd_ahash *mcryptd_tfm = ctx->mcryptd_tfm;
  691. memcpy(mcryptd_req, req, sizeof(*req));
  692. ahash_request_set_tfm(mcryptd_req, &mcryptd_tfm->base);
  693. return crypto_ahash_final(mcryptd_req);
  694. }
  695. static int sha256_mb_async_digest(struct ahash_request *req)
  696. {
  697. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  698. struct sha256_mb_ctx *ctx = crypto_ahash_ctx(tfm);
  699. struct ahash_request *mcryptd_req = ahash_request_ctx(req);
  700. struct mcryptd_ahash *mcryptd_tfm = ctx->mcryptd_tfm;
  701. memcpy(mcryptd_req, req, sizeof(*req));
  702. ahash_request_set_tfm(mcryptd_req, &mcryptd_tfm->base);
  703. return crypto_ahash_digest(mcryptd_req);
  704. }
  705. static int sha256_mb_async_export(struct ahash_request *req, void *out)
  706. {
  707. struct ahash_request *mcryptd_req = ahash_request_ctx(req);
  708. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  709. struct sha256_mb_ctx *ctx = crypto_ahash_ctx(tfm);
  710. struct mcryptd_ahash *mcryptd_tfm = ctx->mcryptd_tfm;
  711. memcpy(mcryptd_req, req, sizeof(*req));
  712. ahash_request_set_tfm(mcryptd_req, &mcryptd_tfm->base);
  713. return crypto_ahash_export(mcryptd_req, out);
  714. }
  715. static int sha256_mb_async_import(struct ahash_request *req, const void *in)
  716. {
  717. struct ahash_request *mcryptd_req = ahash_request_ctx(req);
  718. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  719. struct sha256_mb_ctx *ctx = crypto_ahash_ctx(tfm);
  720. struct mcryptd_ahash *mcryptd_tfm = ctx->mcryptd_tfm;
  721. struct crypto_ahash *child = mcryptd_ahash_child(mcryptd_tfm);
  722. struct mcryptd_hash_request_ctx *rctx;
  723. struct ahash_request *areq;
  724. memcpy(mcryptd_req, req, sizeof(*req));
  725. ahash_request_set_tfm(mcryptd_req, &mcryptd_tfm->base);
  726. rctx = ahash_request_ctx(mcryptd_req);
  727. areq = &rctx->areq;
  728. ahash_request_set_tfm(areq, child);
  729. ahash_request_set_callback(areq, CRYPTO_TFM_REQ_MAY_SLEEP,
  730. rctx->complete, req);
  731. return crypto_ahash_import(mcryptd_req, in);
  732. }
  733. static struct ahash_alg sha256_mb_async_alg = {
  734. .init = sha256_mb_async_init,
  735. .update = sha256_mb_async_update,
  736. .final = sha256_mb_async_final,
  737. .finup = sha256_mb_async_finup,
  738. .export = sha256_mb_async_export,
  739. .import = sha256_mb_async_import,
  740. .digest = sha256_mb_async_digest,
  741. .halg = {
  742. .digestsize = SHA256_DIGEST_SIZE,
  743. .statesize = sizeof(struct sha256_hash_ctx),
  744. .base = {
  745. .cra_name = "sha256",
  746. .cra_driver_name = "sha256_mb",
  747. .cra_priority = 200,
  748. .cra_flags = CRYPTO_ALG_TYPE_AHASH |
  749. CRYPTO_ALG_ASYNC,
  750. .cra_blocksize = SHA256_BLOCK_SIZE,
  751. .cra_type = &crypto_ahash_type,
  752. .cra_module = THIS_MODULE,
  753. .cra_list = LIST_HEAD_INIT
  754. (sha256_mb_async_alg.halg.base.cra_list),
  755. .cra_init = sha256_mb_async_init_tfm,
  756. .cra_exit = sha256_mb_async_exit_tfm,
  757. .cra_ctxsize = sizeof(struct sha256_mb_ctx),
  758. .cra_alignmask = 0,
  759. },
  760. },
  761. };
  762. static unsigned long sha256_mb_flusher(struct mcryptd_alg_cstate *cstate)
  763. {
  764. struct mcryptd_hash_request_ctx *rctx;
  765. unsigned long cur_time;
  766. unsigned long next_flush = 0;
  767. struct sha256_hash_ctx *sha_ctx;
  768. cur_time = jiffies;
  769. while (!list_empty(&cstate->work_list)) {
  770. rctx = list_entry(cstate->work_list.next,
  771. struct mcryptd_hash_request_ctx, waiter);
  772. if (time_before(cur_time, rctx->tag.expire))
  773. break;
  774. kernel_fpu_begin();
  775. sha_ctx = (struct sha256_hash_ctx *)
  776. sha256_ctx_mgr_flush(cstate->mgr);
  777. kernel_fpu_end();
  778. if (!sha_ctx) {
  779. pr_err("sha256_mb error: nothing got"
  780. " flushed for non-empty list\n");
  781. break;
  782. }
  783. rctx = cast_hash_to_mcryptd_ctx(sha_ctx);
  784. sha_finish_walk(&rctx, cstate, true);
  785. sha_complete_job(rctx, cstate, 0);
  786. }
  787. if (!list_empty(&cstate->work_list)) {
  788. rctx = list_entry(cstate->work_list.next,
  789. struct mcryptd_hash_request_ctx, waiter);
  790. /* get the hash context and then flush time */
  791. next_flush = rctx->tag.expire;
  792. mcryptd_arm_flusher(cstate, get_delay(next_flush));
  793. }
  794. return next_flush;
  795. }
  796. static int __init sha256_mb_mod_init(void)
  797. {
  798. int cpu;
  799. int err;
  800. struct mcryptd_alg_cstate *cpu_state;
  801. /* check for dependent cpu features */
  802. if (!boot_cpu_has(X86_FEATURE_AVX2) ||
  803. !boot_cpu_has(X86_FEATURE_BMI2))
  804. return -ENODEV;
  805. /* initialize multibuffer structures */
  806. sha256_mb_alg_state.alg_cstate = alloc_percpu
  807. (struct mcryptd_alg_cstate);
  808. sha256_job_mgr_init = sha256_mb_mgr_init_avx2;
  809. sha256_job_mgr_submit = sha256_mb_mgr_submit_avx2;
  810. sha256_job_mgr_flush = sha256_mb_mgr_flush_avx2;
  811. sha256_job_mgr_get_comp_job = sha256_mb_mgr_get_comp_job_avx2;
  812. if (!sha256_mb_alg_state.alg_cstate)
  813. return -ENOMEM;
  814. for_each_possible_cpu(cpu) {
  815. cpu_state = per_cpu_ptr(sha256_mb_alg_state.alg_cstate, cpu);
  816. cpu_state->next_flush = 0;
  817. cpu_state->next_seq_num = 0;
  818. cpu_state->flusher_engaged = false;
  819. INIT_DELAYED_WORK(&cpu_state->flush, mcryptd_flusher);
  820. cpu_state->cpu = cpu;
  821. cpu_state->alg_state = &sha256_mb_alg_state;
  822. cpu_state->mgr = kzalloc(sizeof(struct sha256_ctx_mgr),
  823. GFP_KERNEL);
  824. if (!cpu_state->mgr)
  825. goto err2;
  826. sha256_ctx_mgr_init(cpu_state->mgr);
  827. INIT_LIST_HEAD(&cpu_state->work_list);
  828. spin_lock_init(&cpu_state->work_lock);
  829. }
  830. sha256_mb_alg_state.flusher = &sha256_mb_flusher;
  831. err = crypto_register_ahash(&sha256_mb_areq_alg);
  832. if (err)
  833. goto err2;
  834. err = crypto_register_ahash(&sha256_mb_async_alg);
  835. if (err)
  836. goto err1;
  837. return 0;
  838. err1:
  839. crypto_unregister_ahash(&sha256_mb_areq_alg);
  840. err2:
  841. for_each_possible_cpu(cpu) {
  842. cpu_state = per_cpu_ptr(sha256_mb_alg_state.alg_cstate, cpu);
  843. kfree(cpu_state->mgr);
  844. }
  845. free_percpu(sha256_mb_alg_state.alg_cstate);
  846. return -ENODEV;
  847. }
  848. static void __exit sha256_mb_mod_fini(void)
  849. {
  850. int cpu;
  851. struct mcryptd_alg_cstate *cpu_state;
  852. crypto_unregister_ahash(&sha256_mb_async_alg);
  853. crypto_unregister_ahash(&sha256_mb_areq_alg);
  854. for_each_possible_cpu(cpu) {
  855. cpu_state = per_cpu_ptr(sha256_mb_alg_state.alg_cstate, cpu);
  856. kfree(cpu_state->mgr);
  857. }
  858. free_percpu(sha256_mb_alg_state.alg_cstate);
  859. }
  860. module_init(sha256_mb_mod_init);
  861. module_exit(sha256_mb_mod_fini);
  862. MODULE_LICENSE("GPL");
  863. MODULE_DESCRIPTION("SHA256 Secure Hash Algorithm, multi buffer accelerated");
  864. MODULE_ALIAS_CRYPTO("sha256");