shash.c 15 KB

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