ahash.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Asynchronous Cryptographic Hash operations.
  4. *
  5. * This is the implementation of the ahash (asynchronous hash) API. It differs
  6. * from shash (synchronous hash) in that ahash supports asynchronous operations,
  7. * and it hashes data from scatterlists instead of virtually addressed buffers.
  8. *
  9. * The ahash API provides access to both ahash and shash algorithms. The shash
  10. * API only provides access to shash algorithms.
  11. *
  12. * Copyright (c) 2008 Loc Ho <lho@amcc.com>
  13. */
  14. #include <crypto/scatterwalk.h>
  15. #include <linux/cryptouser.h>
  16. #include <linux/err.h>
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/sched.h>
  20. #include <linux/slab.h>
  21. #include <linux/seq_file.h>
  22. #include <linux/string.h>
  23. #include <net/netlink.h>
  24. #include "hash.h"
  25. #define CRYPTO_ALG_TYPE_AHASH_MASK 0x0000000e
  26. /*
  27. * For an ahash tfm that is using an shash algorithm (instead of an ahash
  28. * algorithm), this returns the underlying shash tfm.
  29. */
  30. static inline struct crypto_shash *ahash_to_shash(struct crypto_ahash *tfm)
  31. {
  32. return *(struct crypto_shash **)crypto_ahash_ctx(tfm);
  33. }
  34. static inline struct shash_desc *prepare_shash_desc(struct ahash_request *req,
  35. struct crypto_ahash *tfm)
  36. {
  37. struct shash_desc *desc = ahash_request_ctx(req);
  38. desc->tfm = ahash_to_shash(tfm);
  39. return desc;
  40. }
  41. int shash_ahash_update(struct ahash_request *req, struct shash_desc *desc)
  42. {
  43. struct crypto_hash_walk walk;
  44. int nbytes;
  45. for (nbytes = crypto_hash_walk_first(req, &walk); nbytes > 0;
  46. nbytes = crypto_hash_walk_done(&walk, nbytes))
  47. nbytes = crypto_shash_update(desc, walk.data, nbytes);
  48. return nbytes;
  49. }
  50. EXPORT_SYMBOL_GPL(shash_ahash_update);
  51. int shash_ahash_finup(struct ahash_request *req, struct shash_desc *desc)
  52. {
  53. struct crypto_hash_walk walk;
  54. int nbytes;
  55. nbytes = crypto_hash_walk_first(req, &walk);
  56. if (!nbytes)
  57. return crypto_shash_final(desc, req->result);
  58. do {
  59. nbytes = crypto_hash_walk_last(&walk) ?
  60. crypto_shash_finup(desc, walk.data, nbytes,
  61. req->result) :
  62. crypto_shash_update(desc, walk.data, nbytes);
  63. nbytes = crypto_hash_walk_done(&walk, nbytes);
  64. } while (nbytes > 0);
  65. return nbytes;
  66. }
  67. EXPORT_SYMBOL_GPL(shash_ahash_finup);
  68. int shash_ahash_digest(struct ahash_request *req, struct shash_desc *desc)
  69. {
  70. unsigned int nbytes = req->nbytes;
  71. struct scatterlist *sg;
  72. unsigned int offset;
  73. int err;
  74. if (nbytes &&
  75. (sg = req->src, offset = sg->offset,
  76. nbytes <= min(sg->length, ((unsigned int)(PAGE_SIZE)) - offset))) {
  77. void *data;
  78. data = kmap_local_page(sg_page(sg));
  79. err = crypto_shash_digest(desc, data + offset, nbytes,
  80. req->result);
  81. kunmap_local(data);
  82. } else
  83. err = crypto_shash_init(desc) ?:
  84. shash_ahash_finup(req, desc);
  85. return err;
  86. }
  87. EXPORT_SYMBOL_GPL(shash_ahash_digest);
  88. static void crypto_exit_ahash_using_shash(struct crypto_tfm *tfm)
  89. {
  90. struct crypto_shash **ctx = crypto_tfm_ctx(tfm);
  91. crypto_free_shash(*ctx);
  92. }
  93. static int crypto_init_ahash_using_shash(struct crypto_tfm *tfm)
  94. {
  95. struct crypto_alg *calg = tfm->__crt_alg;
  96. struct crypto_ahash *crt = __crypto_ahash_cast(tfm);
  97. struct crypto_shash **ctx = crypto_tfm_ctx(tfm);
  98. struct crypto_shash *shash;
  99. if (!crypto_mod_get(calg))
  100. return -EAGAIN;
  101. shash = crypto_create_tfm(calg, &crypto_shash_type);
  102. if (IS_ERR(shash)) {
  103. crypto_mod_put(calg);
  104. return PTR_ERR(shash);
  105. }
  106. crt->using_shash = true;
  107. *ctx = shash;
  108. tfm->exit = crypto_exit_ahash_using_shash;
  109. crypto_ahash_set_flags(crt, crypto_shash_get_flags(shash) &
  110. CRYPTO_TFM_NEED_KEY);
  111. crt->reqsize = sizeof(struct shash_desc) + crypto_shash_descsize(shash);
  112. return 0;
  113. }
  114. static int hash_walk_next(struct crypto_hash_walk *walk)
  115. {
  116. unsigned int offset = walk->offset;
  117. unsigned int nbytes = min(walk->entrylen,
  118. ((unsigned int)(PAGE_SIZE)) - offset);
  119. walk->data = kmap_local_page(walk->pg);
  120. walk->data += offset;
  121. walk->entrylen -= nbytes;
  122. return nbytes;
  123. }
  124. static int hash_walk_new_entry(struct crypto_hash_walk *walk)
  125. {
  126. struct scatterlist *sg;
  127. sg = walk->sg;
  128. walk->offset = sg->offset;
  129. walk->pg = sg_page(walk->sg) + (walk->offset >> PAGE_SHIFT);
  130. walk->offset = offset_in_page(walk->offset);
  131. walk->entrylen = sg->length;
  132. if (walk->entrylen > walk->total)
  133. walk->entrylen = walk->total;
  134. walk->total -= walk->entrylen;
  135. return hash_walk_next(walk);
  136. }
  137. int crypto_hash_walk_done(struct crypto_hash_walk *walk, int err)
  138. {
  139. walk->data -= walk->offset;
  140. kunmap_local(walk->data);
  141. crypto_yield(walk->flags);
  142. if (err)
  143. return err;
  144. if (walk->entrylen) {
  145. walk->offset = 0;
  146. walk->pg++;
  147. return hash_walk_next(walk);
  148. }
  149. if (!walk->total)
  150. return 0;
  151. walk->sg = sg_next(walk->sg);
  152. return hash_walk_new_entry(walk);
  153. }
  154. EXPORT_SYMBOL_GPL(crypto_hash_walk_done);
  155. int crypto_hash_walk_first(struct ahash_request *req,
  156. struct crypto_hash_walk *walk)
  157. {
  158. walk->total = req->nbytes;
  159. if (!walk->total) {
  160. walk->entrylen = 0;
  161. return 0;
  162. }
  163. walk->sg = req->src;
  164. walk->flags = req->base.flags;
  165. return hash_walk_new_entry(walk);
  166. }
  167. EXPORT_SYMBOL_GPL(crypto_hash_walk_first);
  168. static int ahash_nosetkey(struct crypto_ahash *tfm, const u8 *key,
  169. unsigned int keylen)
  170. {
  171. return -ENOSYS;
  172. }
  173. static void ahash_set_needkey(struct crypto_ahash *tfm, struct ahash_alg *alg)
  174. {
  175. if (alg->setkey != ahash_nosetkey &&
  176. !(alg->halg.base.cra_flags & CRYPTO_ALG_OPTIONAL_KEY))
  177. crypto_ahash_set_flags(tfm, CRYPTO_TFM_NEED_KEY);
  178. }
  179. int crypto_ahash_setkey(struct crypto_ahash *tfm, const u8 *key,
  180. unsigned int keylen)
  181. {
  182. if (likely(tfm->using_shash)) {
  183. struct crypto_shash *shash = ahash_to_shash(tfm);
  184. int err;
  185. err = crypto_shash_setkey(shash, key, keylen);
  186. if (unlikely(err)) {
  187. crypto_ahash_set_flags(tfm,
  188. crypto_shash_get_flags(shash) &
  189. CRYPTO_TFM_NEED_KEY);
  190. return err;
  191. }
  192. } else {
  193. struct ahash_alg *alg = crypto_ahash_alg(tfm);
  194. int err;
  195. err = alg->setkey(tfm, key, keylen);
  196. if (unlikely(err)) {
  197. ahash_set_needkey(tfm, alg);
  198. return err;
  199. }
  200. }
  201. crypto_ahash_clear_flags(tfm, CRYPTO_TFM_NEED_KEY);
  202. return 0;
  203. }
  204. EXPORT_SYMBOL_GPL(crypto_ahash_setkey);
  205. int crypto_ahash_init(struct ahash_request *req)
  206. {
  207. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  208. if (likely(tfm->using_shash))
  209. return crypto_shash_init(prepare_shash_desc(req, tfm));
  210. if (crypto_ahash_get_flags(tfm) & CRYPTO_TFM_NEED_KEY)
  211. return -ENOKEY;
  212. return crypto_ahash_alg(tfm)->init(req);
  213. }
  214. EXPORT_SYMBOL_GPL(crypto_ahash_init);
  215. static int ahash_save_req(struct ahash_request *req, crypto_completion_t cplt,
  216. bool has_state)
  217. {
  218. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  219. unsigned int ds = crypto_ahash_digestsize(tfm);
  220. struct ahash_request *subreq;
  221. unsigned int subreq_size;
  222. unsigned int reqsize;
  223. u8 *result;
  224. gfp_t gfp;
  225. u32 flags;
  226. subreq_size = sizeof(*subreq);
  227. reqsize = crypto_ahash_reqsize(tfm);
  228. reqsize = ALIGN(reqsize, crypto_tfm_ctx_alignment());
  229. subreq_size += reqsize;
  230. subreq_size += ds;
  231. flags = ahash_request_flags(req);
  232. gfp = (flags & CRYPTO_TFM_REQ_MAY_SLEEP) ? GFP_KERNEL : GFP_ATOMIC;
  233. subreq = kmalloc(subreq_size, gfp);
  234. if (!subreq)
  235. return -ENOMEM;
  236. ahash_request_set_tfm(subreq, tfm);
  237. ahash_request_set_callback(subreq, flags, cplt, req);
  238. result = (u8 *)(subreq + 1) + reqsize;
  239. ahash_request_set_crypt(subreq, req->src, result, req->nbytes);
  240. if (has_state) {
  241. void *state;
  242. state = kmalloc(crypto_ahash_statesize(tfm), gfp);
  243. if (!state) {
  244. kfree(subreq);
  245. return -ENOMEM;
  246. }
  247. crypto_ahash_export(req, state);
  248. crypto_ahash_import(subreq, state);
  249. kfree_sensitive(state);
  250. }
  251. req->priv = subreq;
  252. return 0;
  253. }
  254. static void ahash_restore_req(struct ahash_request *req, int err)
  255. {
  256. struct ahash_request *subreq = req->priv;
  257. if (!err)
  258. memcpy(req->result, subreq->result,
  259. crypto_ahash_digestsize(crypto_ahash_reqtfm(req)));
  260. req->priv = NULL;
  261. kfree_sensitive(subreq);
  262. }
  263. int crypto_ahash_update(struct ahash_request *req)
  264. {
  265. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  266. if (likely(tfm->using_shash))
  267. return shash_ahash_update(req, ahash_request_ctx(req));
  268. return crypto_ahash_alg(tfm)->update(req);
  269. }
  270. EXPORT_SYMBOL_GPL(crypto_ahash_update);
  271. int crypto_ahash_final(struct ahash_request *req)
  272. {
  273. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  274. if (likely(tfm->using_shash))
  275. return crypto_shash_final(ahash_request_ctx(req), req->result);
  276. return crypto_ahash_alg(tfm)->final(req);
  277. }
  278. EXPORT_SYMBOL_GPL(crypto_ahash_final);
  279. int crypto_ahash_finup(struct ahash_request *req)
  280. {
  281. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  282. if (likely(tfm->using_shash))
  283. return shash_ahash_finup(req, ahash_request_ctx(req));
  284. return crypto_ahash_alg(tfm)->finup(req);
  285. }
  286. EXPORT_SYMBOL_GPL(crypto_ahash_finup);
  287. int crypto_ahash_digest(struct ahash_request *req)
  288. {
  289. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  290. if (likely(tfm->using_shash))
  291. return shash_ahash_digest(req, prepare_shash_desc(req, tfm));
  292. if (crypto_ahash_get_flags(tfm) & CRYPTO_TFM_NEED_KEY)
  293. return -ENOKEY;
  294. return crypto_ahash_alg(tfm)->digest(req);
  295. }
  296. EXPORT_SYMBOL_GPL(crypto_ahash_digest);
  297. static void ahash_def_finup_done2(void *data, int err)
  298. {
  299. struct ahash_request *areq = data;
  300. if (err == -EINPROGRESS)
  301. return;
  302. ahash_restore_req(areq, err);
  303. ahash_request_complete(areq, err);
  304. }
  305. static int ahash_def_finup_finish1(struct ahash_request *req, int err)
  306. {
  307. struct ahash_request *subreq = req->priv;
  308. if (err)
  309. goto out;
  310. subreq->base.complete = ahash_def_finup_done2;
  311. err = crypto_ahash_alg(crypto_ahash_reqtfm(req))->final(subreq);
  312. if (err == -EINPROGRESS || err == -EBUSY)
  313. return err;
  314. out:
  315. ahash_restore_req(req, err);
  316. return err;
  317. }
  318. static void ahash_def_finup_done1(void *data, int err)
  319. {
  320. struct ahash_request *areq = data;
  321. struct ahash_request *subreq;
  322. if (err == -EINPROGRESS)
  323. goto out;
  324. subreq = areq->priv;
  325. subreq->base.flags &= CRYPTO_TFM_REQ_MAY_BACKLOG;
  326. err = ahash_def_finup_finish1(areq, err);
  327. if (err == -EINPROGRESS || err == -EBUSY)
  328. return;
  329. out:
  330. ahash_request_complete(areq, err);
  331. }
  332. static int ahash_def_finup(struct ahash_request *req)
  333. {
  334. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  335. int err;
  336. err = ahash_save_req(req, ahash_def_finup_done1, true);
  337. if (err)
  338. return err;
  339. err = crypto_ahash_alg(tfm)->update(req->priv);
  340. if (err == -EINPROGRESS || err == -EBUSY)
  341. return err;
  342. return ahash_def_finup_finish1(req, err);
  343. }
  344. int crypto_ahash_export(struct ahash_request *req, void *out)
  345. {
  346. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  347. if (likely(tfm->using_shash))
  348. return crypto_shash_export(ahash_request_ctx(req), out);
  349. return crypto_ahash_alg(tfm)->export(req, out);
  350. }
  351. EXPORT_SYMBOL_GPL(crypto_ahash_export);
  352. int crypto_ahash_import(struct ahash_request *req, const void *in)
  353. {
  354. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  355. if (likely(tfm->using_shash))
  356. return crypto_shash_import(prepare_shash_desc(req, tfm), in);
  357. if (crypto_ahash_get_flags(tfm) & CRYPTO_TFM_NEED_KEY)
  358. return -ENOKEY;
  359. return crypto_ahash_alg(tfm)->import(req, in);
  360. }
  361. EXPORT_SYMBOL_GPL(crypto_ahash_import);
  362. static void crypto_ahash_exit_tfm(struct crypto_tfm *tfm)
  363. {
  364. struct crypto_ahash *hash = __crypto_ahash_cast(tfm);
  365. struct ahash_alg *alg = crypto_ahash_alg(hash);
  366. alg->exit_tfm(hash);
  367. }
  368. static int crypto_ahash_init_tfm(struct crypto_tfm *tfm)
  369. {
  370. struct crypto_ahash *hash = __crypto_ahash_cast(tfm);
  371. struct ahash_alg *alg = crypto_ahash_alg(hash);
  372. crypto_ahash_set_statesize(hash, alg->halg.statesize);
  373. if (tfm->__crt_alg->cra_type == &crypto_shash_type)
  374. return crypto_init_ahash_using_shash(tfm);
  375. ahash_set_needkey(hash, alg);
  376. if (alg->exit_tfm)
  377. tfm->exit = crypto_ahash_exit_tfm;
  378. return alg->init_tfm ? alg->init_tfm(hash) : 0;
  379. }
  380. static unsigned int crypto_ahash_extsize(struct crypto_alg *alg)
  381. {
  382. if (alg->cra_type == &crypto_shash_type)
  383. return sizeof(struct crypto_shash *);
  384. return crypto_alg_extsize(alg);
  385. }
  386. static void crypto_ahash_free_instance(struct crypto_instance *inst)
  387. {
  388. struct ahash_instance *ahash = ahash_instance(inst);
  389. ahash->free(ahash);
  390. }
  391. static int __maybe_unused crypto_ahash_report(
  392. struct sk_buff *skb, struct crypto_alg *alg)
  393. {
  394. struct crypto_report_hash rhash;
  395. memset(&rhash, 0, sizeof(rhash));
  396. strscpy(rhash.type, "ahash", sizeof(rhash.type));
  397. rhash.blocksize = alg->cra_blocksize;
  398. rhash.digestsize = __crypto_hash_alg_common(alg)->digestsize;
  399. return nla_put(skb, CRYPTOCFGA_REPORT_HASH, sizeof(rhash), &rhash);
  400. }
  401. static void crypto_ahash_show(struct seq_file *m, struct crypto_alg *alg)
  402. __maybe_unused;
  403. static void crypto_ahash_show(struct seq_file *m, struct crypto_alg *alg)
  404. {
  405. seq_printf(m, "type : ahash\n");
  406. seq_printf(m, "async : %s\n", alg->cra_flags & CRYPTO_ALG_ASYNC ?
  407. "yes" : "no");
  408. seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
  409. seq_printf(m, "digestsize : %u\n",
  410. __crypto_hash_alg_common(alg)->digestsize);
  411. }
  412. static const struct crypto_type crypto_ahash_type = {
  413. .extsize = crypto_ahash_extsize,
  414. .init_tfm = crypto_ahash_init_tfm,
  415. .free = crypto_ahash_free_instance,
  416. #ifdef CONFIG_PROC_FS
  417. .show = crypto_ahash_show,
  418. #endif
  419. #if IS_ENABLED(CONFIG_CRYPTO_USER)
  420. .report = crypto_ahash_report,
  421. #endif
  422. .maskclear = ~CRYPTO_ALG_TYPE_MASK,
  423. .maskset = CRYPTO_ALG_TYPE_AHASH_MASK,
  424. .type = CRYPTO_ALG_TYPE_AHASH,
  425. .tfmsize = offsetof(struct crypto_ahash, base),
  426. };
  427. int crypto_grab_ahash(struct crypto_ahash_spawn *spawn,
  428. struct crypto_instance *inst,
  429. const char *name, u32 type, u32 mask)
  430. {
  431. spawn->base.frontend = &crypto_ahash_type;
  432. return crypto_grab_spawn(&spawn->base, inst, name, type, mask);
  433. }
  434. EXPORT_SYMBOL_GPL(crypto_grab_ahash);
  435. struct crypto_ahash *crypto_alloc_ahash(const char *alg_name, u32 type,
  436. u32 mask)
  437. {
  438. return crypto_alloc_tfm(alg_name, &crypto_ahash_type, type, mask);
  439. }
  440. EXPORT_SYMBOL_GPL(crypto_alloc_ahash);
  441. int crypto_has_ahash(const char *alg_name, u32 type, u32 mask)
  442. {
  443. return crypto_type_has_alg(alg_name, &crypto_ahash_type, type, mask);
  444. }
  445. EXPORT_SYMBOL_GPL(crypto_has_ahash);
  446. static bool crypto_hash_alg_has_setkey(struct hash_alg_common *halg)
  447. {
  448. struct crypto_alg *alg = &halg->base;
  449. if (alg->cra_type == &crypto_shash_type)
  450. return crypto_shash_alg_has_setkey(__crypto_shash_alg(alg));
  451. return __crypto_ahash_alg(alg)->setkey != ahash_nosetkey;
  452. }
  453. struct crypto_ahash *crypto_clone_ahash(struct crypto_ahash *hash)
  454. {
  455. struct hash_alg_common *halg = crypto_hash_alg_common(hash);
  456. struct crypto_tfm *tfm = crypto_ahash_tfm(hash);
  457. struct crypto_ahash *nhash;
  458. struct ahash_alg *alg;
  459. int err;
  460. if (!crypto_hash_alg_has_setkey(halg)) {
  461. tfm = crypto_tfm_get(tfm);
  462. if (IS_ERR(tfm))
  463. return ERR_CAST(tfm);
  464. return hash;
  465. }
  466. nhash = crypto_clone_tfm(&crypto_ahash_type, tfm);
  467. if (IS_ERR(nhash))
  468. return nhash;
  469. nhash->reqsize = hash->reqsize;
  470. nhash->statesize = hash->statesize;
  471. if (likely(hash->using_shash)) {
  472. struct crypto_shash **nctx = crypto_ahash_ctx(nhash);
  473. struct crypto_shash *shash;
  474. shash = crypto_clone_shash(ahash_to_shash(hash));
  475. if (IS_ERR(shash)) {
  476. err = PTR_ERR(shash);
  477. goto out_free_nhash;
  478. }
  479. nhash->using_shash = true;
  480. *nctx = shash;
  481. return nhash;
  482. }
  483. err = -ENOSYS;
  484. alg = crypto_ahash_alg(hash);
  485. if (!alg->clone_tfm)
  486. goto out_free_nhash;
  487. err = alg->clone_tfm(nhash, hash);
  488. if (err)
  489. goto out_free_nhash;
  490. return nhash;
  491. out_free_nhash:
  492. crypto_free_ahash(nhash);
  493. return ERR_PTR(err);
  494. }
  495. EXPORT_SYMBOL_GPL(crypto_clone_ahash);
  496. static int ahash_prepare_alg(struct ahash_alg *alg)
  497. {
  498. struct crypto_alg *base = &alg->halg.base;
  499. int err;
  500. if (alg->halg.statesize == 0)
  501. return -EINVAL;
  502. err = hash_prepare_alg(&alg->halg);
  503. if (err)
  504. return err;
  505. base->cra_type = &crypto_ahash_type;
  506. base->cra_flags |= CRYPTO_ALG_TYPE_AHASH;
  507. if (!alg->finup)
  508. alg->finup = ahash_def_finup;
  509. if (!alg->setkey)
  510. alg->setkey = ahash_nosetkey;
  511. return 0;
  512. }
  513. int crypto_register_ahash(struct ahash_alg *alg)
  514. {
  515. struct crypto_alg *base = &alg->halg.base;
  516. int err;
  517. err = ahash_prepare_alg(alg);
  518. if (err)
  519. return err;
  520. return crypto_register_alg(base);
  521. }
  522. EXPORT_SYMBOL_GPL(crypto_register_ahash);
  523. void crypto_unregister_ahash(struct ahash_alg *alg)
  524. {
  525. crypto_unregister_alg(&alg->halg.base);
  526. }
  527. EXPORT_SYMBOL_GPL(crypto_unregister_ahash);
  528. int crypto_register_ahashes(struct ahash_alg *algs, int count)
  529. {
  530. int i, ret;
  531. for (i = 0; i < count; i++) {
  532. ret = crypto_register_ahash(&algs[i]);
  533. if (ret)
  534. goto err;
  535. }
  536. return 0;
  537. err:
  538. for (--i; i >= 0; --i)
  539. crypto_unregister_ahash(&algs[i]);
  540. return ret;
  541. }
  542. EXPORT_SYMBOL_GPL(crypto_register_ahashes);
  543. void crypto_unregister_ahashes(struct ahash_alg *algs, int count)
  544. {
  545. int i;
  546. for (i = count - 1; i >= 0; --i)
  547. crypto_unregister_ahash(&algs[i]);
  548. }
  549. EXPORT_SYMBOL_GPL(crypto_unregister_ahashes);
  550. int ahash_register_instance(struct crypto_template *tmpl,
  551. struct ahash_instance *inst)
  552. {
  553. int err;
  554. if (WARN_ON(!inst->free))
  555. return -EINVAL;
  556. err = ahash_prepare_alg(&inst->alg);
  557. if (err)
  558. return err;
  559. return crypto_register_instance(tmpl, ahash_crypto_instance(inst));
  560. }
  561. EXPORT_SYMBOL_GPL(ahash_register_instance);
  562. MODULE_LICENSE("GPL");
  563. MODULE_DESCRIPTION("Asynchronous cryptographic hash type");