shash.c 18 KB

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