sha512_mb.c 28 KB

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