shash.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  1. /*
  2. * Synchronous Cryptographic Hash operations.
  3. *
  4. * Copyright (c) 2008 Herbert Xu <herbert@gondor.apana.org.au>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the Free
  8. * Software Foundation; either version 2 of the License, or (at your option)
  9. * any later version.
  10. *
  11. */
  12. #include <crypto/scatterwalk.h>
  13. #include <crypto/internal/hash.h>
  14. #include <linux/err.h>
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/slab.h>
  18. #include <linux/seq_file.h>
  19. #include <linux/cryptouser.h>
  20. #include <net/netlink.h>
  21. #include "internal.h"
  22. static const struct crypto_type crypto_shash_type;
  23. static int shash_no_setkey(struct crypto_shash *tfm, const u8 *key,
  24. unsigned int keylen)
  25. {
  26. return -ENOSYS;
  27. }
  28. static int shash_setkey_unaligned(struct crypto_shash *tfm, const u8 *key,
  29. unsigned int keylen)
  30. {
  31. struct shash_alg *shash = crypto_shash_alg(tfm);
  32. unsigned long alignmask = crypto_shash_alignmask(tfm);
  33. unsigned long absize;
  34. u8 *buffer, *alignbuffer;
  35. int err;
  36. absize = keylen + (alignmask & ~(crypto_tfm_ctx_alignment() - 1));
  37. buffer = kmalloc(absize, GFP_KERNEL);
  38. if (!buffer)
  39. return -ENOMEM;
  40. alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
  41. memcpy(alignbuffer, key, keylen);
  42. err = shash->setkey(tfm, alignbuffer, keylen);
  43. kzfree(buffer);
  44. return err;
  45. }
  46. int crypto_shash_setkey(struct crypto_shash *tfm, const u8 *key,
  47. unsigned int keylen)
  48. {
  49. struct shash_alg *shash = crypto_shash_alg(tfm);
  50. unsigned long alignmask = crypto_shash_alignmask(tfm);
  51. if ((unsigned long)key & alignmask)
  52. return shash_setkey_unaligned(tfm, key, keylen);
  53. return shash->setkey(tfm, key, keylen);
  54. }
  55. EXPORT_SYMBOL_GPL(crypto_shash_setkey);
  56. static inline unsigned int shash_align_buffer_size(unsigned len,
  57. unsigned long mask)
  58. {
  59. return len + (mask & ~(__alignof__(u8 __attribute__ ((aligned))) - 1));
  60. }
  61. static int shash_update_unaligned(struct shash_desc *desc, const u8 *data,
  62. unsigned int len)
  63. {
  64. struct crypto_shash *tfm = desc->tfm;
  65. struct shash_alg *shash = crypto_shash_alg(tfm);
  66. unsigned long alignmask = crypto_shash_alignmask(tfm);
  67. unsigned int unaligned_len = alignmask + 1 -
  68. ((unsigned long)data & alignmask);
  69. u8 ubuf[shash_align_buffer_size(unaligned_len, alignmask)]
  70. __attribute__ ((aligned));
  71. u8 *buf = PTR_ALIGN(&ubuf[0], alignmask + 1);
  72. int err;
  73. if (unaligned_len > len)
  74. unaligned_len = len;
  75. memcpy(buf, data, unaligned_len);
  76. err = shash->update(desc, buf, unaligned_len);
  77. memset(buf, 0, unaligned_len);
  78. return err ?:
  79. shash->update(desc, data + unaligned_len, len - unaligned_len);
  80. }
  81. int crypto_shash_update(struct shash_desc *desc, const u8 *data,
  82. unsigned int len)
  83. {
  84. struct crypto_shash *tfm = desc->tfm;
  85. struct shash_alg *shash = crypto_shash_alg(tfm);
  86. unsigned long alignmask = crypto_shash_alignmask(tfm);
  87. if ((unsigned long)data & alignmask)
  88. return shash_update_unaligned(desc, data, len);
  89. return shash->update(desc, data, len);
  90. }
  91. EXPORT_SYMBOL_GPL(crypto_shash_update);
  92. static int shash_final_unaligned(struct shash_desc *desc, u8 *out)
  93. {
  94. struct crypto_shash *tfm = desc->tfm;
  95. unsigned long alignmask = crypto_shash_alignmask(tfm);
  96. struct shash_alg *shash = crypto_shash_alg(tfm);
  97. unsigned int ds = crypto_shash_digestsize(tfm);
  98. u8 ubuf[shash_align_buffer_size(ds, alignmask)]
  99. __attribute__ ((aligned));
  100. u8 *buf = PTR_ALIGN(&ubuf[0], alignmask + 1);
  101. int err;
  102. err = shash->final(desc, buf);
  103. if (err)
  104. goto out;
  105. memcpy(out, buf, ds);
  106. out:
  107. memset(buf, 0, ds);
  108. return err;
  109. }
  110. int crypto_shash_final(struct shash_desc *desc, u8 *out)
  111. {
  112. struct crypto_shash *tfm = desc->tfm;
  113. struct shash_alg *shash = crypto_shash_alg(tfm);
  114. unsigned long alignmask = crypto_shash_alignmask(tfm);
  115. if ((unsigned long)out & alignmask)
  116. return shash_final_unaligned(desc, out);
  117. return shash->final(desc, out);
  118. }
  119. EXPORT_SYMBOL_GPL(crypto_shash_final);
  120. static int shash_finup_unaligned(struct shash_desc *desc, const u8 *data,
  121. unsigned int len, u8 *out)
  122. {
  123. return crypto_shash_update(desc, data, len) ?:
  124. crypto_shash_final(desc, out);
  125. }
  126. int crypto_shash_finup(struct shash_desc *desc, const u8 *data,
  127. unsigned int len, u8 *out)
  128. {
  129. struct crypto_shash *tfm = desc->tfm;
  130. struct shash_alg *shash = crypto_shash_alg(tfm);
  131. unsigned long alignmask = crypto_shash_alignmask(tfm);
  132. if (((unsigned long)data | (unsigned long)out) & alignmask)
  133. return shash_finup_unaligned(desc, data, len, out);
  134. return shash->finup(desc, data, len, out);
  135. }
  136. EXPORT_SYMBOL_GPL(crypto_shash_finup);
  137. static int shash_digest_unaligned(struct shash_desc *desc, const u8 *data,
  138. unsigned int len, u8 *out)
  139. {
  140. return crypto_shash_init(desc) ?:
  141. crypto_shash_finup(desc, data, len, out);
  142. }
  143. int crypto_shash_digest(struct shash_desc *desc, const u8 *data,
  144. unsigned int len, u8 *out)
  145. {
  146. struct crypto_shash *tfm = desc->tfm;
  147. struct shash_alg *shash = crypto_shash_alg(tfm);
  148. unsigned long alignmask = crypto_shash_alignmask(tfm);
  149. if (((unsigned long)data | (unsigned long)out) & alignmask)
  150. return shash_digest_unaligned(desc, data, len, out);
  151. return shash->digest(desc, data, len, out);
  152. }
  153. EXPORT_SYMBOL_GPL(crypto_shash_digest);
  154. static int shash_default_export(struct shash_desc *desc, void *out)
  155. {
  156. memcpy(out, shash_desc_ctx(desc), crypto_shash_descsize(desc->tfm));
  157. return 0;
  158. }
  159. static int shash_default_import(struct shash_desc *desc, const void *in)
  160. {
  161. memcpy(shash_desc_ctx(desc), in, crypto_shash_descsize(desc->tfm));
  162. return 0;
  163. }
  164. static int shash_async_setkey(struct crypto_ahash *tfm, const u8 *key,
  165. unsigned int keylen)
  166. {
  167. struct crypto_shash **ctx = crypto_ahash_ctx(tfm);
  168. return crypto_shash_setkey(*ctx, key, keylen);
  169. }
  170. static int shash_async_init(struct ahash_request *req)
  171. {
  172. struct crypto_shash **ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(req));
  173. struct shash_desc *desc = ahash_request_ctx(req);
  174. desc->tfm = *ctx;
  175. desc->flags = req->base.flags;
  176. return crypto_shash_init(desc);
  177. }
  178. int shash_ahash_update(struct ahash_request *req, struct shash_desc *desc)
  179. {
  180. struct crypto_hash_walk walk;
  181. int nbytes;
  182. for (nbytes = crypto_hash_walk_first(req, &walk); nbytes > 0;
  183. nbytes = crypto_hash_walk_done(&walk, nbytes))
  184. nbytes = crypto_shash_update(desc, walk.data, nbytes);
  185. return nbytes;
  186. }
  187. EXPORT_SYMBOL_GPL(shash_ahash_update);
  188. static int shash_async_update(struct ahash_request *req)
  189. {
  190. return shash_ahash_update(req, ahash_request_ctx(req));
  191. }
  192. static int shash_async_final(struct ahash_request *req)
  193. {
  194. return crypto_shash_final(ahash_request_ctx(req), req->result);
  195. }
  196. int shash_ahash_finup(struct ahash_request *req, struct shash_desc *desc)
  197. {
  198. struct crypto_hash_walk walk;
  199. int nbytes;
  200. nbytes = crypto_hash_walk_first(req, &walk);
  201. if (!nbytes)
  202. return crypto_shash_final(desc, req->result);
  203. do {
  204. nbytes = crypto_hash_walk_last(&walk) ?
  205. crypto_shash_finup(desc, walk.data, nbytes,
  206. req->result) :
  207. crypto_shash_update(desc, walk.data, nbytes);
  208. nbytes = crypto_hash_walk_done(&walk, nbytes);
  209. } while (nbytes > 0);
  210. return nbytes;
  211. }
  212. EXPORT_SYMBOL_GPL(shash_ahash_finup);
  213. static int shash_async_finup(struct ahash_request *req)
  214. {
  215. struct crypto_shash **ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(req));
  216. struct shash_desc *desc = ahash_request_ctx(req);
  217. desc->tfm = *ctx;
  218. desc->flags = req->base.flags;
  219. return shash_ahash_finup(req, desc);
  220. }
  221. int shash_ahash_digest(struct ahash_request *req, struct shash_desc *desc)
  222. {
  223. struct scatterlist *sg = req->src;
  224. unsigned int offset = sg->offset;
  225. unsigned int nbytes = req->nbytes;
  226. int err;
  227. if (nbytes < min(sg->length, ((unsigned int)(PAGE_SIZE)) - offset)) {
  228. void *data;
  229. data = kmap_atomic(sg_page(sg));
  230. err = crypto_shash_digest(desc, data + offset, nbytes,
  231. req->result);
  232. kunmap_atomic(data);
  233. crypto_yield(desc->flags);
  234. } else
  235. err = crypto_shash_init(desc) ?:
  236. shash_ahash_finup(req, desc);
  237. return err;
  238. }
  239. EXPORT_SYMBOL_GPL(shash_ahash_digest);
  240. static int shash_async_digest(struct ahash_request *req)
  241. {
  242. struct crypto_shash **ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(req));
  243. struct shash_desc *desc = ahash_request_ctx(req);
  244. desc->tfm = *ctx;
  245. desc->flags = req->base.flags;
  246. return shash_ahash_digest(req, desc);
  247. }
  248. static int shash_async_export(struct ahash_request *req, void *out)
  249. {
  250. return crypto_shash_export(ahash_request_ctx(req), out);
  251. }
  252. static int shash_async_import(struct ahash_request *req, const void *in)
  253. {
  254. struct crypto_shash **ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(req));
  255. struct shash_desc *desc = ahash_request_ctx(req);
  256. desc->tfm = *ctx;
  257. desc->flags = req->base.flags;
  258. return crypto_shash_import(desc, in);
  259. }
  260. static void crypto_exit_shash_ops_async(struct crypto_tfm *tfm)
  261. {
  262. struct crypto_shash **ctx = crypto_tfm_ctx(tfm);
  263. crypto_free_shash(*ctx);
  264. }
  265. int crypto_init_shash_ops_async(struct crypto_tfm *tfm)
  266. {
  267. struct crypto_alg *calg = tfm->__crt_alg;
  268. struct shash_alg *alg = __crypto_shash_alg(calg);
  269. struct crypto_ahash *crt = __crypto_ahash_cast(tfm);
  270. struct crypto_shash **ctx = crypto_tfm_ctx(tfm);
  271. struct crypto_shash *shash;
  272. if (!crypto_mod_get(calg))
  273. return -EAGAIN;
  274. shash = crypto_create_tfm(calg, &crypto_shash_type);
  275. if (IS_ERR(shash)) {
  276. crypto_mod_put(calg);
  277. return PTR_ERR(shash);
  278. }
  279. *ctx = shash;
  280. tfm->exit = crypto_exit_shash_ops_async;
  281. crt->init = shash_async_init;
  282. crt->update = shash_async_update;
  283. crt->final = shash_async_final;
  284. crt->finup = shash_async_finup;
  285. crt->digest = shash_async_digest;
  286. if (alg->setkey)
  287. crt->setkey = shash_async_setkey;
  288. if (alg->export)
  289. crt->export = shash_async_export;
  290. if (alg->import)
  291. crt->import = shash_async_import;
  292. crt->reqsize = sizeof(struct shash_desc) + crypto_shash_descsize(shash);
  293. return 0;
  294. }
  295. static int shash_compat_setkey(struct crypto_hash *tfm, const u8 *key,
  296. unsigned int keylen)
  297. {
  298. struct shash_desc **descp = crypto_hash_ctx(tfm);
  299. struct shash_desc *desc = *descp;
  300. return crypto_shash_setkey(desc->tfm, key, keylen);
  301. }
  302. static int shash_compat_init(struct hash_desc *hdesc)
  303. {
  304. struct shash_desc **descp = crypto_hash_ctx(hdesc->tfm);
  305. struct shash_desc *desc = *descp;
  306. desc->flags = hdesc->flags;
  307. return crypto_shash_init(desc);
  308. }
  309. static int shash_compat_update(struct hash_desc *hdesc, struct scatterlist *sg,
  310. unsigned int len)
  311. {
  312. struct shash_desc **descp = crypto_hash_ctx(hdesc->tfm);
  313. struct shash_desc *desc = *descp;
  314. struct crypto_hash_walk walk;
  315. int nbytes;
  316. for (nbytes = crypto_hash_walk_first_compat(hdesc, &walk, sg, len);
  317. nbytes > 0; nbytes = crypto_hash_walk_done(&walk, nbytes))
  318. nbytes = crypto_shash_update(desc, walk.data, nbytes);
  319. return nbytes;
  320. }
  321. static int shash_compat_final(struct hash_desc *hdesc, u8 *out)
  322. {
  323. struct shash_desc **descp = crypto_hash_ctx(hdesc->tfm);
  324. return crypto_shash_final(*descp, out);
  325. }
  326. static int shash_compat_digest(struct hash_desc *hdesc, struct scatterlist *sg,
  327. unsigned int nbytes, u8 *out)
  328. {
  329. unsigned int offset = sg->offset;
  330. int err;
  331. if (nbytes < min(sg->length, ((unsigned int)(PAGE_SIZE)) - offset)) {
  332. struct shash_desc **descp = crypto_hash_ctx(hdesc->tfm);
  333. struct shash_desc *desc = *descp;
  334. void *data;
  335. desc->flags = hdesc->flags;
  336. data = kmap_atomic(sg_page(sg));
  337. err = crypto_shash_digest(desc, data + offset, nbytes, out);
  338. kunmap_atomic(data);
  339. crypto_yield(desc->flags);
  340. goto out;
  341. }
  342. err = shash_compat_init(hdesc);
  343. if (err)
  344. goto out;
  345. err = shash_compat_update(hdesc, sg, nbytes);
  346. if (err)
  347. goto out;
  348. err = shash_compat_final(hdesc, out);
  349. out:
  350. return err;
  351. }
  352. static void crypto_exit_shash_ops_compat(struct crypto_tfm *tfm)
  353. {
  354. struct shash_desc **descp = crypto_tfm_ctx(tfm);
  355. struct shash_desc *desc = *descp;
  356. crypto_free_shash(desc->tfm);
  357. kzfree(desc);
  358. }
  359. static int crypto_init_shash_ops_compat(struct crypto_tfm *tfm)
  360. {
  361. struct hash_tfm *crt = &tfm->crt_hash;
  362. struct crypto_alg *calg = tfm->__crt_alg;
  363. struct shash_alg *alg = __crypto_shash_alg(calg);
  364. struct shash_desc **descp = crypto_tfm_ctx(tfm);
  365. struct crypto_shash *shash;
  366. struct shash_desc *desc;
  367. if (!crypto_mod_get(calg))
  368. return -EAGAIN;
  369. shash = crypto_create_tfm(calg, &crypto_shash_type);
  370. if (IS_ERR(shash)) {
  371. crypto_mod_put(calg);
  372. return PTR_ERR(shash);
  373. }
  374. desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(shash),
  375. GFP_KERNEL);
  376. if (!desc) {
  377. crypto_free_shash(shash);
  378. return -ENOMEM;
  379. }
  380. *descp = desc;
  381. desc->tfm = shash;
  382. tfm->exit = crypto_exit_shash_ops_compat;
  383. crt->init = shash_compat_init;
  384. crt->update = shash_compat_update;
  385. crt->final = shash_compat_final;
  386. crt->digest = shash_compat_digest;
  387. crt->setkey = shash_compat_setkey;
  388. crt->digestsize = alg->digestsize;
  389. return 0;
  390. }
  391. static int crypto_init_shash_ops(struct crypto_tfm *tfm, u32 type, u32 mask)
  392. {
  393. switch (mask & CRYPTO_ALG_TYPE_MASK) {
  394. case CRYPTO_ALG_TYPE_HASH_MASK:
  395. return crypto_init_shash_ops_compat(tfm);
  396. }
  397. return -EINVAL;
  398. }
  399. static unsigned int crypto_shash_ctxsize(struct crypto_alg *alg, u32 type,
  400. u32 mask)
  401. {
  402. switch (mask & CRYPTO_ALG_TYPE_MASK) {
  403. case CRYPTO_ALG_TYPE_HASH_MASK:
  404. return sizeof(struct shash_desc *);
  405. }
  406. return 0;
  407. }
  408. static int crypto_shash_init_tfm(struct crypto_tfm *tfm)
  409. {
  410. struct crypto_shash *hash = __crypto_shash_cast(tfm);
  411. hash->descsize = crypto_shash_alg(hash)->descsize;
  412. return 0;
  413. }
  414. static unsigned int crypto_shash_extsize(struct crypto_alg *alg)
  415. {
  416. return alg->cra_ctxsize;
  417. }
  418. #ifdef CONFIG_NET
  419. static int crypto_shash_report(struct sk_buff *skb, struct crypto_alg *alg)
  420. {
  421. struct crypto_report_hash rhash;
  422. struct shash_alg *salg = __crypto_shash_alg(alg);
  423. snprintf(rhash.type, CRYPTO_MAX_ALG_NAME, "%s", "shash");
  424. rhash.blocksize = alg->cra_blocksize;
  425. rhash.digestsize = salg->digestsize;
  426. NLA_PUT(skb, CRYPTOCFGA_REPORT_HASH,
  427. sizeof(struct crypto_report_hash), &rhash);
  428. return 0;
  429. nla_put_failure:
  430. return -EMSGSIZE;
  431. }
  432. #else
  433. static int crypto_shash_report(struct sk_buff *skb, struct crypto_alg *alg)
  434. {
  435. return -ENOSYS;
  436. }
  437. #endif
  438. static void crypto_shash_show(struct seq_file *m, struct crypto_alg *alg)
  439. __attribute__ ((unused));
  440. static void crypto_shash_show(struct seq_file *m, struct crypto_alg *alg)
  441. {
  442. struct shash_alg *salg = __crypto_shash_alg(alg);
  443. seq_printf(m, "type : shash\n");
  444. seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
  445. seq_printf(m, "digestsize : %u\n", salg->digestsize);
  446. }
  447. static const struct crypto_type crypto_shash_type = {
  448. .ctxsize = crypto_shash_ctxsize,
  449. .extsize = crypto_shash_extsize,
  450. .init = crypto_init_shash_ops,
  451. .init_tfm = crypto_shash_init_tfm,
  452. #ifdef CONFIG_PROC_FS
  453. .show = crypto_shash_show,
  454. #endif
  455. .report = crypto_shash_report,
  456. .maskclear = ~CRYPTO_ALG_TYPE_MASK,
  457. .maskset = CRYPTO_ALG_TYPE_MASK,
  458. .type = CRYPTO_ALG_TYPE_SHASH,
  459. .tfmsize = offsetof(struct crypto_shash, base),
  460. };
  461. struct crypto_shash *crypto_alloc_shash(const char *alg_name, u32 type,
  462. u32 mask)
  463. {
  464. return crypto_alloc_tfm(alg_name, &crypto_shash_type, type, mask);
  465. }
  466. EXPORT_SYMBOL_GPL(crypto_alloc_shash);
  467. static int shash_prepare_alg(struct shash_alg *alg)
  468. {
  469. struct crypto_alg *base = &alg->base;
  470. if (alg->digestsize > PAGE_SIZE / 8 ||
  471. alg->descsize > PAGE_SIZE / 8 ||
  472. alg->statesize > PAGE_SIZE / 8)
  473. return -EINVAL;
  474. base->cra_type = &crypto_shash_type;
  475. base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK;
  476. base->cra_flags |= CRYPTO_ALG_TYPE_SHASH;
  477. if (!alg->finup)
  478. alg->finup = shash_finup_unaligned;
  479. if (!alg->digest)
  480. alg->digest = shash_digest_unaligned;
  481. if (!alg->export) {
  482. alg->export = shash_default_export;
  483. alg->import = shash_default_import;
  484. alg->statesize = alg->descsize;
  485. }
  486. if (!alg->setkey)
  487. alg->setkey = shash_no_setkey;
  488. return 0;
  489. }
  490. int crypto_register_shash(struct shash_alg *alg)
  491. {
  492. struct crypto_alg *base = &alg->base;
  493. int err;
  494. err = shash_prepare_alg(alg);
  495. if (err)
  496. return err;
  497. return crypto_register_alg(base);
  498. }
  499. EXPORT_SYMBOL_GPL(crypto_register_shash);
  500. int crypto_unregister_shash(struct shash_alg *alg)
  501. {
  502. return crypto_unregister_alg(&alg->base);
  503. }
  504. EXPORT_SYMBOL_GPL(crypto_unregister_shash);
  505. int shash_register_instance(struct crypto_template *tmpl,
  506. struct shash_instance *inst)
  507. {
  508. int err;
  509. err = shash_prepare_alg(&inst->alg);
  510. if (err)
  511. return err;
  512. return crypto_register_instance(tmpl, shash_crypto_instance(inst));
  513. }
  514. EXPORT_SYMBOL_GPL(shash_register_instance);
  515. void shash_free_instance(struct crypto_instance *inst)
  516. {
  517. crypto_drop_spawn(crypto_instance_ctx(inst));
  518. kfree(shash_instance(inst));
  519. }
  520. EXPORT_SYMBOL_GPL(shash_free_instance);
  521. int crypto_init_shash_spawn(struct crypto_shash_spawn *spawn,
  522. struct shash_alg *alg,
  523. struct crypto_instance *inst)
  524. {
  525. return crypto_init_spawn2(&spawn->base, &alg->base, inst,
  526. &crypto_shash_type);
  527. }
  528. EXPORT_SYMBOL_GPL(crypto_init_shash_spawn);
  529. struct shash_alg *shash_attr_alg(struct rtattr *rta, u32 type, u32 mask)
  530. {
  531. struct crypto_alg *alg;
  532. alg = crypto_attr_alg2(rta, &crypto_shash_type, type, mask);
  533. return IS_ERR(alg) ? ERR_CAST(alg) :
  534. container_of(alg, struct shash_alg, base);
  535. }
  536. EXPORT_SYMBOL_GPL(shash_attr_alg);
  537. MODULE_LICENSE("GPL");
  538. MODULE_DESCRIPTION("Synchronous cryptographic hash type");