shash.c 16 KB

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