shash.c 14 KB

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