shash.c 15 KB

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