shash.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  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 <linux/compiler.h>
  22. #include "internal.h"
  23. static const struct crypto_type crypto_shash_type;
  24. static int shash_no_setkey(struct crypto_shash *tfm, const u8 *key,
  25. unsigned int keylen)
  26. {
  27. return -ENOSYS;
  28. }
  29. /*
  30. * Check whether an shash algorithm has a setkey function.
  31. *
  32. * For CFI compatibility, this must not be an inline function. This is because
  33. * when CFI is enabled, modules won't get the same address for shash_no_setkey
  34. * (if it were exported, which inlining would require) as the core kernel will.
  35. */
  36. bool crypto_shash_alg_has_setkey(struct shash_alg *alg)
  37. {
  38. return alg->setkey != shash_no_setkey;
  39. }
  40. EXPORT_SYMBOL_GPL(crypto_shash_alg_has_setkey);
  41. static int shash_setkey_unaligned(struct crypto_shash *tfm, const u8 *key,
  42. unsigned int keylen)
  43. {
  44. struct shash_alg *shash = crypto_shash_alg(tfm);
  45. unsigned long alignmask = crypto_shash_alignmask(tfm);
  46. unsigned long absize;
  47. u8 *buffer, *alignbuffer;
  48. int err;
  49. absize = keylen + (alignmask & ~(crypto_tfm_ctx_alignment() - 1));
  50. buffer = kmalloc(absize, GFP_ATOMIC);
  51. if (!buffer)
  52. return -ENOMEM;
  53. alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
  54. memcpy(alignbuffer, key, keylen);
  55. err = shash->setkey(tfm, alignbuffer, keylen);
  56. kzfree(buffer);
  57. return err;
  58. }
  59. static void shash_set_needkey(struct crypto_shash *tfm, struct shash_alg *alg)
  60. {
  61. if (crypto_shash_alg_has_setkey(alg) &&
  62. !(alg->base.cra_flags & CRYPTO_ALG_OPTIONAL_KEY))
  63. crypto_shash_set_flags(tfm, CRYPTO_TFM_NEED_KEY);
  64. }
  65. int crypto_shash_setkey(struct crypto_shash *tfm, const u8 *key,
  66. unsigned int keylen)
  67. {
  68. struct shash_alg *shash = crypto_shash_alg(tfm);
  69. unsigned long alignmask = crypto_shash_alignmask(tfm);
  70. int err;
  71. if ((unsigned long)key & alignmask)
  72. err = shash_setkey_unaligned(tfm, key, keylen);
  73. else
  74. err = shash->setkey(tfm, key, keylen);
  75. if (unlikely(err)) {
  76. shash_set_needkey(tfm, shash);
  77. return err;
  78. }
  79. crypto_shash_clear_flags(tfm, CRYPTO_TFM_NEED_KEY);
  80. return 0;
  81. }
  82. EXPORT_SYMBOL_GPL(crypto_shash_setkey);
  83. static inline unsigned int shash_align_buffer_size(unsigned len,
  84. unsigned long mask)
  85. {
  86. typedef u8 __aligned_largest u8_aligned;
  87. return len + (mask & ~(__alignof__(u8_aligned) - 1));
  88. }
  89. static int shash_update_unaligned(struct shash_desc *desc, const u8 *data,
  90. unsigned int len)
  91. {
  92. struct crypto_shash *tfm = desc->tfm;
  93. struct shash_alg *shash = crypto_shash_alg(tfm);
  94. unsigned long alignmask = crypto_shash_alignmask(tfm);
  95. unsigned int unaligned_len = alignmask + 1 -
  96. ((unsigned long)data & alignmask);
  97. u8 ubuf[shash_align_buffer_size(unaligned_len, alignmask)]
  98. __aligned_largest;
  99. u8 *buf = PTR_ALIGN(&ubuf[0], alignmask + 1);
  100. int err;
  101. if (unaligned_len > len)
  102. unaligned_len = len;
  103. memcpy(buf, data, unaligned_len);
  104. err = shash->update(desc, buf, unaligned_len);
  105. memset(buf, 0, unaligned_len);
  106. return err ?:
  107. shash->update(desc, data + unaligned_len, len - unaligned_len);
  108. }
  109. int crypto_shash_update(struct shash_desc *desc, const u8 *data,
  110. unsigned int len)
  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)data & alignmask)
  116. return shash_update_unaligned(desc, data, len);
  117. return shash->update(desc, data, len);
  118. }
  119. EXPORT_SYMBOL_GPL(crypto_shash_update);
  120. static int shash_final_unaligned(struct shash_desc *desc, u8 *out)
  121. {
  122. struct crypto_shash *tfm = desc->tfm;
  123. unsigned long alignmask = crypto_shash_alignmask(tfm);
  124. struct shash_alg *shash = crypto_shash_alg(tfm);
  125. unsigned int ds = crypto_shash_digestsize(tfm);
  126. u8 ubuf[shash_align_buffer_size(ds, alignmask)]
  127. __aligned_largest;
  128. u8 *buf = PTR_ALIGN(&ubuf[0], alignmask + 1);
  129. int err;
  130. err = shash->final(desc, buf);
  131. if (err)
  132. goto out;
  133. memcpy(out, buf, ds);
  134. out:
  135. memset(buf, 0, ds);
  136. return err;
  137. }
  138. int crypto_shash_final(struct shash_desc *desc, u8 *out)
  139. {
  140. struct crypto_shash *tfm = desc->tfm;
  141. struct shash_alg *shash = crypto_shash_alg(tfm);
  142. unsigned long alignmask = crypto_shash_alignmask(tfm);
  143. if ((unsigned long)out & alignmask)
  144. return shash_final_unaligned(desc, out);
  145. return shash->final(desc, out);
  146. }
  147. EXPORT_SYMBOL_GPL(crypto_shash_final);
  148. static int shash_finup_unaligned(struct shash_desc *desc, const u8 *data,
  149. unsigned int len, u8 *out)
  150. {
  151. return crypto_shash_update(desc, data, len) ?:
  152. crypto_shash_final(desc, out);
  153. }
  154. int crypto_shash_finup(struct shash_desc *desc, const u8 *data,
  155. unsigned int len, u8 *out)
  156. {
  157. struct crypto_shash *tfm = desc->tfm;
  158. struct shash_alg *shash = crypto_shash_alg(tfm);
  159. unsigned long alignmask = crypto_shash_alignmask(tfm);
  160. if (((unsigned long)data | (unsigned long)out) & alignmask)
  161. return shash_finup_unaligned(desc, data, len, out);
  162. return shash->finup(desc, data, len, out);
  163. }
  164. EXPORT_SYMBOL_GPL(crypto_shash_finup);
  165. static int shash_digest_unaligned(struct shash_desc *desc, const u8 *data,
  166. unsigned int len, u8 *out)
  167. {
  168. return crypto_shash_init(desc) ?:
  169. crypto_shash_finup(desc, data, len, out);
  170. }
  171. int crypto_shash_digest(struct shash_desc *desc, const u8 *data,
  172. unsigned int len, u8 *out)
  173. {
  174. struct crypto_shash *tfm = desc->tfm;
  175. struct shash_alg *shash = crypto_shash_alg(tfm);
  176. unsigned long alignmask = crypto_shash_alignmask(tfm);
  177. if (crypto_shash_get_flags(tfm) & CRYPTO_TFM_NEED_KEY)
  178. return -ENOKEY;
  179. if (((unsigned long)data | (unsigned long)out) & alignmask)
  180. return shash_digest_unaligned(desc, data, len, out);
  181. return shash->digest(desc, data, len, out);
  182. }
  183. EXPORT_SYMBOL_GPL(crypto_shash_digest);
  184. static int shash_default_export(struct shash_desc *desc, void *out)
  185. {
  186. memcpy(out, shash_desc_ctx(desc), crypto_shash_descsize(desc->tfm));
  187. return 0;
  188. }
  189. static int shash_default_import(struct shash_desc *desc, const void *in)
  190. {
  191. memcpy(shash_desc_ctx(desc), in, crypto_shash_descsize(desc->tfm));
  192. return 0;
  193. }
  194. static int shash_async_setkey(struct crypto_ahash *tfm, const u8 *key,
  195. unsigned int keylen)
  196. {
  197. struct crypto_shash **ctx = crypto_ahash_ctx(tfm);
  198. return crypto_shash_setkey(*ctx, key, keylen);
  199. }
  200. static int shash_async_init(struct ahash_request *req)
  201. {
  202. struct crypto_shash **ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(req));
  203. struct shash_desc *desc = ahash_request_ctx(req);
  204. desc->tfm = *ctx;
  205. desc->flags = req->base.flags;
  206. return crypto_shash_init(desc);
  207. }
  208. int shash_ahash_update(struct ahash_request *req, struct shash_desc *desc)
  209. {
  210. struct crypto_hash_walk walk;
  211. int nbytes;
  212. for (nbytes = crypto_hash_walk_first(req, &walk); nbytes > 0;
  213. nbytes = crypto_hash_walk_done(&walk, nbytes))
  214. nbytes = crypto_shash_update(desc, walk.data, nbytes);
  215. return nbytes;
  216. }
  217. EXPORT_SYMBOL_GPL(shash_ahash_update);
  218. static int shash_async_update(struct ahash_request *req)
  219. {
  220. return shash_ahash_update(req, ahash_request_ctx(req));
  221. }
  222. static int shash_async_final(struct ahash_request *req)
  223. {
  224. return crypto_shash_final(ahash_request_ctx(req), req->result);
  225. }
  226. int shash_ahash_finup(struct ahash_request *req, struct shash_desc *desc)
  227. {
  228. struct crypto_hash_walk walk;
  229. int nbytes;
  230. nbytes = crypto_hash_walk_first(req, &walk);
  231. if (!nbytes)
  232. return crypto_shash_final(desc, req->result);
  233. do {
  234. nbytes = crypto_hash_walk_last(&walk) ?
  235. crypto_shash_finup(desc, walk.data, nbytes,
  236. req->result) :
  237. crypto_shash_update(desc, walk.data, nbytes);
  238. nbytes = crypto_hash_walk_done(&walk, nbytes);
  239. } while (nbytes > 0);
  240. return nbytes;
  241. }
  242. EXPORT_SYMBOL_GPL(shash_ahash_finup);
  243. static int shash_async_finup(struct ahash_request *req)
  244. {
  245. struct crypto_shash **ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(req));
  246. struct shash_desc *desc = ahash_request_ctx(req);
  247. desc->tfm = *ctx;
  248. desc->flags = req->base.flags;
  249. return shash_ahash_finup(req, desc);
  250. }
  251. int shash_ahash_digest(struct ahash_request *req, struct shash_desc *desc)
  252. {
  253. unsigned int nbytes = req->nbytes;
  254. struct scatterlist *sg;
  255. unsigned int offset;
  256. int err;
  257. if (nbytes &&
  258. (sg = req->src, offset = sg->offset,
  259. nbytes < min(sg->length, ((unsigned int)(PAGE_SIZE)) - offset))) {
  260. void *data;
  261. data = kmap_atomic(sg_page(sg));
  262. err = crypto_shash_digest(desc, data + offset, nbytes,
  263. req->result);
  264. kunmap_atomic(data);
  265. crypto_yield(desc->flags);
  266. } else
  267. err = crypto_shash_init(desc) ?:
  268. shash_ahash_finup(req, desc);
  269. return err;
  270. }
  271. EXPORT_SYMBOL_GPL(shash_ahash_digest);
  272. static int shash_async_digest(struct ahash_request *req)
  273. {
  274. struct crypto_shash **ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(req));
  275. struct shash_desc *desc = ahash_request_ctx(req);
  276. desc->tfm = *ctx;
  277. desc->flags = req->base.flags;
  278. return shash_ahash_digest(req, desc);
  279. }
  280. static int shash_async_export(struct ahash_request *req, void *out)
  281. {
  282. return crypto_shash_export(ahash_request_ctx(req), out);
  283. }
  284. static int shash_async_import(struct ahash_request *req, const void *in)
  285. {
  286. struct crypto_shash **ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(req));
  287. struct shash_desc *desc = ahash_request_ctx(req);
  288. desc->tfm = *ctx;
  289. desc->flags = req->base.flags;
  290. return crypto_shash_import(desc, in);
  291. }
  292. static void crypto_exit_shash_ops_async(struct crypto_tfm *tfm)
  293. {
  294. struct crypto_shash **ctx = crypto_tfm_ctx(tfm);
  295. crypto_free_shash(*ctx);
  296. }
  297. int crypto_init_shash_ops_async(struct crypto_tfm *tfm)
  298. {
  299. struct crypto_alg *calg = tfm->__crt_alg;
  300. struct shash_alg *alg = __crypto_shash_alg(calg);
  301. struct crypto_ahash *crt = __crypto_ahash_cast(tfm);
  302. struct crypto_shash **ctx = crypto_tfm_ctx(tfm);
  303. struct crypto_shash *shash;
  304. if (!crypto_mod_get(calg))
  305. return -EAGAIN;
  306. shash = crypto_create_tfm(calg, &crypto_shash_type);
  307. if (IS_ERR(shash)) {
  308. crypto_mod_put(calg);
  309. return PTR_ERR(shash);
  310. }
  311. *ctx = shash;
  312. tfm->exit = crypto_exit_shash_ops_async;
  313. crt->init = shash_async_init;
  314. crt->update = shash_async_update;
  315. crt->final = shash_async_final;
  316. crt->finup = shash_async_finup;
  317. crt->digest = shash_async_digest;
  318. if (crypto_shash_alg_has_setkey(alg))
  319. crt->setkey = shash_async_setkey;
  320. crypto_ahash_set_flags(crt, crypto_shash_get_flags(shash) &
  321. CRYPTO_TFM_NEED_KEY);
  322. if (alg->export)
  323. crt->export = shash_async_export;
  324. if (alg->import)
  325. crt->import = shash_async_import;
  326. crt->reqsize = sizeof(struct shash_desc) + crypto_shash_descsize(shash);
  327. return 0;
  328. }
  329. static int crypto_shash_init_tfm(struct crypto_tfm *tfm)
  330. {
  331. struct crypto_shash *hash = __crypto_shash_cast(tfm);
  332. struct shash_alg *alg = crypto_shash_alg(hash);
  333. hash->descsize = alg->descsize;
  334. shash_set_needkey(hash, alg);
  335. return 0;
  336. }
  337. #ifdef CONFIG_NET
  338. static int crypto_shash_report(struct sk_buff *skb, struct crypto_alg *alg)
  339. {
  340. struct crypto_report_hash rhash;
  341. struct shash_alg *salg = __crypto_shash_alg(alg);
  342. strncpy(rhash.type, "shash", sizeof(rhash.type));
  343. rhash.blocksize = alg->cra_blocksize;
  344. rhash.digestsize = salg->digestsize;
  345. if (nla_put(skb, CRYPTOCFGA_REPORT_HASH,
  346. sizeof(struct crypto_report_hash), &rhash))
  347. goto nla_put_failure;
  348. return 0;
  349. nla_put_failure:
  350. return -EMSGSIZE;
  351. }
  352. #else
  353. static int crypto_shash_report(struct sk_buff *skb, struct crypto_alg *alg)
  354. {
  355. return -ENOSYS;
  356. }
  357. #endif
  358. static void crypto_shash_show(struct seq_file *m, struct crypto_alg *alg)
  359. __maybe_unused;
  360. static void crypto_shash_show(struct seq_file *m, struct crypto_alg *alg)
  361. {
  362. struct shash_alg *salg = __crypto_shash_alg(alg);
  363. seq_printf(m, "type : shash\n");
  364. seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
  365. seq_printf(m, "digestsize : %u\n", salg->digestsize);
  366. }
  367. static const struct crypto_type crypto_shash_type = {
  368. .extsize = crypto_alg_extsize,
  369. .init_tfm = crypto_shash_init_tfm,
  370. #ifdef CONFIG_PROC_FS
  371. .show = crypto_shash_show,
  372. #endif
  373. .report = crypto_shash_report,
  374. .maskclear = ~CRYPTO_ALG_TYPE_MASK,
  375. .maskset = CRYPTO_ALG_TYPE_MASK,
  376. .type = CRYPTO_ALG_TYPE_SHASH,
  377. .tfmsize = offsetof(struct crypto_shash, base),
  378. };
  379. struct crypto_shash *crypto_alloc_shash(const char *alg_name, u32 type,
  380. u32 mask)
  381. {
  382. return crypto_alloc_tfm(alg_name, &crypto_shash_type, type, mask);
  383. }
  384. EXPORT_SYMBOL_GPL(crypto_alloc_shash);
  385. static int shash_prepare_alg(struct shash_alg *alg)
  386. {
  387. struct crypto_alg *base = &alg->base;
  388. if (alg->digestsize > PAGE_SIZE / 8 ||
  389. alg->descsize > PAGE_SIZE / 8 ||
  390. alg->statesize > PAGE_SIZE / 8)
  391. return -EINVAL;
  392. base->cra_type = &crypto_shash_type;
  393. base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK;
  394. base->cra_flags |= CRYPTO_ALG_TYPE_SHASH;
  395. if (!alg->finup)
  396. alg->finup = shash_finup_unaligned;
  397. if (!alg->digest)
  398. alg->digest = shash_digest_unaligned;
  399. if (!alg->export) {
  400. alg->export = shash_default_export;
  401. alg->import = shash_default_import;
  402. alg->statesize = alg->descsize;
  403. }
  404. if (!alg->setkey)
  405. alg->setkey = shash_no_setkey;
  406. return 0;
  407. }
  408. int crypto_register_shash(struct shash_alg *alg)
  409. {
  410. struct crypto_alg *base = &alg->base;
  411. int err;
  412. err = shash_prepare_alg(alg);
  413. if (err)
  414. return err;
  415. return crypto_register_alg(base);
  416. }
  417. EXPORT_SYMBOL_GPL(crypto_register_shash);
  418. int crypto_unregister_shash(struct shash_alg *alg)
  419. {
  420. return crypto_unregister_alg(&alg->base);
  421. }
  422. EXPORT_SYMBOL_GPL(crypto_unregister_shash);
  423. int crypto_register_shashes(struct shash_alg *algs, int count)
  424. {
  425. int i, ret;
  426. for (i = 0; i < count; i++) {
  427. ret = crypto_register_shash(&algs[i]);
  428. if (ret)
  429. goto err;
  430. }
  431. return 0;
  432. err:
  433. for (--i; i >= 0; --i)
  434. crypto_unregister_shash(&algs[i]);
  435. return ret;
  436. }
  437. EXPORT_SYMBOL_GPL(crypto_register_shashes);
  438. int crypto_unregister_shashes(struct shash_alg *algs, int count)
  439. {
  440. int i, ret;
  441. for (i = count - 1; i >= 0; --i) {
  442. ret = crypto_unregister_shash(&algs[i]);
  443. if (ret)
  444. pr_err("Failed to unregister %s %s: %d\n",
  445. algs[i].base.cra_driver_name,
  446. algs[i].base.cra_name, ret);
  447. }
  448. return 0;
  449. }
  450. EXPORT_SYMBOL_GPL(crypto_unregister_shashes);
  451. int shash_register_instance(struct crypto_template *tmpl,
  452. struct shash_instance *inst)
  453. {
  454. int err;
  455. err = shash_prepare_alg(&inst->alg);
  456. if (err)
  457. return err;
  458. return crypto_register_instance(tmpl, shash_crypto_instance(inst));
  459. }
  460. EXPORT_SYMBOL_GPL(shash_register_instance);
  461. void shash_free_instance(struct crypto_instance *inst)
  462. {
  463. crypto_drop_spawn(crypto_instance_ctx(inst));
  464. kfree(shash_instance(inst));
  465. }
  466. EXPORT_SYMBOL_GPL(shash_free_instance);
  467. int crypto_init_shash_spawn(struct crypto_shash_spawn *spawn,
  468. struct shash_alg *alg,
  469. struct crypto_instance *inst)
  470. {
  471. return crypto_init_spawn2(&spawn->base, &alg->base, inst,
  472. &crypto_shash_type);
  473. }
  474. EXPORT_SYMBOL_GPL(crypto_init_shash_spawn);
  475. struct shash_alg *shash_attr_alg(struct rtattr *rta, u32 type, u32 mask)
  476. {
  477. struct crypto_alg *alg;
  478. alg = crypto_attr_alg2(rta, &crypto_shash_type, type, mask);
  479. return IS_ERR(alg) ? ERR_CAST(alg) :
  480. container_of(alg, struct shash_alg, base);
  481. }
  482. EXPORT_SYMBOL_GPL(shash_attr_alg);
  483. MODULE_LICENSE("GPL");
  484. MODULE_DESCRIPTION("Synchronous cryptographic hash type");