shash.c 14 KB

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