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