shash.c 15 KB

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