shash.c 19 KB

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