shash.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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. static const struct crypto_type crypto_shash_type;
  20. static inline struct crypto_shash *__crypto_shash_cast(struct crypto_tfm *tfm)
  21. {
  22. return container_of(tfm, struct crypto_shash, base);
  23. }
  24. #include "internal.h"
  25. static int shash_setkey_unaligned(struct crypto_shash *tfm, const u8 *key,
  26. unsigned int keylen)
  27. {
  28. struct shash_alg *shash = crypto_shash_alg(tfm);
  29. unsigned long alignmask = crypto_shash_alignmask(tfm);
  30. unsigned long absize;
  31. u8 *buffer, *alignbuffer;
  32. int err;
  33. absize = keylen + (alignmask & ~(CRYPTO_MINALIGN - 1));
  34. buffer = kmalloc(absize, GFP_KERNEL);
  35. if (!buffer)
  36. return -ENOMEM;
  37. alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
  38. memcpy(alignbuffer, key, keylen);
  39. err = shash->setkey(tfm, alignbuffer, keylen);
  40. memset(alignbuffer, 0, keylen);
  41. kfree(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 (!shash->setkey)
  50. return -ENOSYS;
  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. return len + (mask & ~(__alignof__(u8 __attribute__ ((aligned))) - 1));
  60. }
  61. static int shash_update_unaligned(struct shash_desc *desc, const u8 *data,
  62. unsigned int len)
  63. {
  64. struct crypto_shash *tfm = desc->tfm;
  65. struct shash_alg *shash = crypto_shash_alg(tfm);
  66. unsigned long alignmask = crypto_shash_alignmask(tfm);
  67. unsigned int unaligned_len = alignmask + 1 -
  68. ((unsigned long)data & alignmask);
  69. u8 buf[shash_align_buffer_size(unaligned_len, alignmask)]
  70. __attribute__ ((aligned));
  71. memcpy(buf, data, unaligned_len);
  72. return shash->update(desc, buf, unaligned_len) ?:
  73. shash->update(desc, data + unaligned_len, len - unaligned_len);
  74. }
  75. int crypto_shash_update(struct shash_desc *desc, const u8 *data,
  76. unsigned int len)
  77. {
  78. struct crypto_shash *tfm = desc->tfm;
  79. struct shash_alg *shash = crypto_shash_alg(tfm);
  80. unsigned long alignmask = crypto_shash_alignmask(tfm);
  81. if ((unsigned long)data & alignmask)
  82. return shash_update_unaligned(desc, data, len);
  83. return shash->update(desc, data, len);
  84. }
  85. EXPORT_SYMBOL_GPL(crypto_shash_update);
  86. static int shash_final_unaligned(struct shash_desc *desc, u8 *out)
  87. {
  88. struct crypto_shash *tfm = desc->tfm;
  89. unsigned long alignmask = crypto_shash_alignmask(tfm);
  90. struct shash_alg *shash = crypto_shash_alg(tfm);
  91. unsigned int ds = crypto_shash_digestsize(tfm);
  92. u8 buf[shash_align_buffer_size(ds, alignmask)]
  93. __attribute__ ((aligned));
  94. int err;
  95. err = shash->final(desc, buf);
  96. memcpy(out, buf, ds);
  97. return err;
  98. }
  99. int crypto_shash_final(struct shash_desc *desc, u8 *out)
  100. {
  101. struct crypto_shash *tfm = desc->tfm;
  102. struct shash_alg *shash = crypto_shash_alg(tfm);
  103. unsigned long alignmask = crypto_shash_alignmask(tfm);
  104. if ((unsigned long)out & alignmask)
  105. return shash_final_unaligned(desc, out);
  106. return shash->final(desc, out);
  107. }
  108. EXPORT_SYMBOL_GPL(crypto_shash_final);
  109. static int shash_finup_unaligned(struct shash_desc *desc, const u8 *data,
  110. unsigned int len, u8 *out)
  111. {
  112. return crypto_shash_update(desc, data, len) ?:
  113. crypto_shash_final(desc, out);
  114. }
  115. int crypto_shash_finup(struct shash_desc *desc, const u8 *data,
  116. unsigned int len, u8 *out)
  117. {
  118. struct crypto_shash *tfm = desc->tfm;
  119. struct shash_alg *shash = crypto_shash_alg(tfm);
  120. unsigned long alignmask = crypto_shash_alignmask(tfm);
  121. if (((unsigned long)data | (unsigned long)out) & alignmask ||
  122. !shash->finup)
  123. return shash_finup_unaligned(desc, data, len, out);
  124. return shash->finup(desc, data, len, out);
  125. }
  126. EXPORT_SYMBOL_GPL(crypto_shash_finup);
  127. static int shash_digest_unaligned(struct shash_desc *desc, const u8 *data,
  128. unsigned int len, u8 *out)
  129. {
  130. return crypto_shash_init(desc) ?:
  131. crypto_shash_update(desc, data, len) ?:
  132. crypto_shash_final(desc, out);
  133. }
  134. int crypto_shash_digest(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. !shash->digest)
  142. return shash_digest_unaligned(desc, data, len, out);
  143. return shash->digest(desc, data, len, out);
  144. }
  145. EXPORT_SYMBOL_GPL(crypto_shash_digest);
  146. int crypto_shash_import(struct shash_desc *desc, const u8 *in)
  147. {
  148. struct crypto_shash *tfm = desc->tfm;
  149. struct shash_alg *alg = crypto_shash_alg(tfm);
  150. memcpy(shash_desc_ctx(desc), in, crypto_shash_descsize(tfm));
  151. if (alg->reinit)
  152. alg->reinit(desc);
  153. return 0;
  154. }
  155. EXPORT_SYMBOL_GPL(crypto_shash_import);
  156. static int shash_async_setkey(struct crypto_ahash *tfm, const u8 *key,
  157. unsigned int keylen)
  158. {
  159. struct crypto_shash **ctx = crypto_ahash_ctx(tfm);
  160. return crypto_shash_setkey(*ctx, key, keylen);
  161. }
  162. static int shash_async_init(struct ahash_request *req)
  163. {
  164. struct crypto_shash **ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(req));
  165. struct shash_desc *desc = ahash_request_ctx(req);
  166. desc->tfm = *ctx;
  167. desc->flags = req->base.flags;
  168. return crypto_shash_init(desc);
  169. }
  170. static int shash_async_update(struct ahash_request *req)
  171. {
  172. struct shash_desc *desc = ahash_request_ctx(req);
  173. struct crypto_hash_walk walk;
  174. int nbytes;
  175. for (nbytes = crypto_hash_walk_first(req, &walk); nbytes > 0;
  176. nbytes = crypto_hash_walk_done(&walk, nbytes))
  177. nbytes = crypto_shash_update(desc, walk.data, nbytes);
  178. return nbytes;
  179. }
  180. static int shash_async_final(struct ahash_request *req)
  181. {
  182. return crypto_shash_final(ahash_request_ctx(req), req->result);
  183. }
  184. static int shash_async_digest(struct ahash_request *req)
  185. {
  186. struct scatterlist *sg = req->src;
  187. unsigned int offset = sg->offset;
  188. unsigned int nbytes = req->nbytes;
  189. int err;
  190. if (nbytes < min(sg->length, ((unsigned int)(PAGE_SIZE)) - offset)) {
  191. struct crypto_shash **ctx =
  192. crypto_ahash_ctx(crypto_ahash_reqtfm(req));
  193. struct shash_desc *desc = ahash_request_ctx(req);
  194. void *data;
  195. desc->tfm = *ctx;
  196. desc->flags = req->base.flags;
  197. data = crypto_kmap(sg_page(sg), 0);
  198. err = crypto_shash_digest(desc, data + offset, nbytes,
  199. req->result);
  200. crypto_kunmap(data, 0);
  201. crypto_yield(desc->flags);
  202. goto out;
  203. }
  204. err = shash_async_init(req);
  205. if (err)
  206. goto out;
  207. err = shash_async_update(req);
  208. if (err)
  209. goto out;
  210. err = shash_async_final(req);
  211. out:
  212. return err;
  213. }
  214. static void crypto_exit_shash_ops_async(struct crypto_tfm *tfm)
  215. {
  216. struct crypto_shash **ctx = crypto_tfm_ctx(tfm);
  217. crypto_free_shash(*ctx);
  218. }
  219. static int crypto_init_shash_ops_async(struct crypto_tfm *tfm)
  220. {
  221. struct crypto_alg *calg = tfm->__crt_alg;
  222. struct shash_alg *alg = __crypto_shash_alg(calg);
  223. struct ahash_tfm *crt = &tfm->crt_ahash;
  224. struct crypto_shash **ctx = crypto_tfm_ctx(tfm);
  225. struct crypto_shash *shash;
  226. if (!crypto_mod_get(calg))
  227. return -EAGAIN;
  228. shash = __crypto_shash_cast(crypto_create_tfm(
  229. calg, &crypto_shash_type));
  230. if (IS_ERR(shash)) {
  231. crypto_mod_put(calg);
  232. return PTR_ERR(shash);
  233. }
  234. *ctx = shash;
  235. tfm->exit = crypto_exit_shash_ops_async;
  236. crt->init = shash_async_init;
  237. crt->update = shash_async_update;
  238. crt->final = shash_async_final;
  239. crt->digest = shash_async_digest;
  240. crt->setkey = shash_async_setkey;
  241. crt->digestsize = alg->digestsize;
  242. crt->reqsize = sizeof(struct shash_desc) + crypto_shash_descsize(shash);
  243. return 0;
  244. }
  245. static int shash_compat_setkey(struct crypto_hash *tfm, const u8 *key,
  246. unsigned int keylen)
  247. {
  248. struct shash_desc *desc = crypto_hash_ctx(tfm);
  249. return crypto_shash_setkey(desc->tfm, key, keylen);
  250. }
  251. static int shash_compat_init(struct hash_desc *hdesc)
  252. {
  253. struct shash_desc *desc = crypto_hash_ctx(hdesc->tfm);
  254. desc->flags = hdesc->flags;
  255. return crypto_shash_init(desc);
  256. }
  257. static int shash_compat_update(struct hash_desc *hdesc, struct scatterlist *sg,
  258. unsigned int len)
  259. {
  260. struct shash_desc *desc = crypto_hash_ctx(hdesc->tfm);
  261. struct crypto_hash_walk walk;
  262. int nbytes;
  263. for (nbytes = crypto_hash_walk_first_compat(hdesc, &walk, sg, len);
  264. nbytes > 0; nbytes = crypto_hash_walk_done(&walk, nbytes))
  265. nbytes = crypto_shash_update(desc, walk.data, nbytes);
  266. return nbytes;
  267. }
  268. static int shash_compat_final(struct hash_desc *hdesc, u8 *out)
  269. {
  270. return crypto_shash_final(crypto_hash_ctx(hdesc->tfm), out);
  271. }
  272. static int shash_compat_digest(struct hash_desc *hdesc, struct scatterlist *sg,
  273. unsigned int nbytes, u8 *out)
  274. {
  275. unsigned int offset = sg->offset;
  276. int err;
  277. if (nbytes < min(sg->length, ((unsigned int)(PAGE_SIZE)) - offset)) {
  278. struct shash_desc *desc = crypto_hash_ctx(hdesc->tfm);
  279. void *data;
  280. desc->flags = hdesc->flags;
  281. data = crypto_kmap(sg_page(sg), 0);
  282. err = crypto_shash_digest(desc, data + offset, nbytes, out);
  283. crypto_kunmap(data, 0);
  284. crypto_yield(desc->flags);
  285. goto out;
  286. }
  287. err = shash_compat_init(hdesc);
  288. if (err)
  289. goto out;
  290. err = shash_compat_update(hdesc, sg, nbytes);
  291. if (err)
  292. goto out;
  293. err = shash_compat_final(hdesc, out);
  294. out:
  295. return err;
  296. }
  297. static void crypto_exit_shash_ops_compat(struct crypto_tfm *tfm)
  298. {
  299. struct shash_desc *desc= crypto_tfm_ctx(tfm);
  300. crypto_free_shash(desc->tfm);
  301. }
  302. static int crypto_init_shash_ops_compat(struct crypto_tfm *tfm)
  303. {
  304. struct hash_tfm *crt = &tfm->crt_hash;
  305. struct crypto_alg *calg = tfm->__crt_alg;
  306. struct shash_alg *alg = __crypto_shash_alg(calg);
  307. struct shash_desc *desc = crypto_tfm_ctx(tfm);
  308. struct crypto_shash *shash;
  309. shash = __crypto_shash_cast(crypto_create_tfm(
  310. calg, &crypto_shash_type));
  311. if (IS_ERR(shash))
  312. return PTR_ERR(shash);
  313. desc->tfm = shash;
  314. tfm->exit = crypto_exit_shash_ops_compat;
  315. crt->init = shash_compat_init;
  316. crt->update = shash_compat_update;
  317. crt->final = shash_compat_final;
  318. crt->digest = shash_compat_digest;
  319. crt->setkey = shash_compat_setkey;
  320. crt->digestsize = alg->digestsize;
  321. return 0;
  322. }
  323. static int crypto_init_shash_ops(struct crypto_tfm *tfm, u32 type, u32 mask)
  324. {
  325. switch (mask & CRYPTO_ALG_TYPE_MASK) {
  326. case CRYPTO_ALG_TYPE_HASH_MASK:
  327. return crypto_init_shash_ops_compat(tfm);
  328. case CRYPTO_ALG_TYPE_AHASH_MASK:
  329. return crypto_init_shash_ops_async(tfm);
  330. }
  331. return -EINVAL;
  332. }
  333. static unsigned int crypto_shash_ctxsize(struct crypto_alg *alg, u32 type,
  334. u32 mask)
  335. {
  336. struct shash_alg *salg = __crypto_shash_alg(alg);
  337. switch (mask & CRYPTO_ALG_TYPE_MASK) {
  338. case CRYPTO_ALG_TYPE_HASH_MASK:
  339. return sizeof(struct shash_desc) + salg->descsize;
  340. case CRYPTO_ALG_TYPE_AHASH_MASK:
  341. return sizeof(struct crypto_shash *);
  342. }
  343. return 0;
  344. }
  345. static int crypto_shash_init_tfm(struct crypto_tfm *tfm,
  346. const struct crypto_type *frontend)
  347. {
  348. if (frontend->type != CRYPTO_ALG_TYPE_SHASH)
  349. return -EINVAL;
  350. return 0;
  351. }
  352. static unsigned int crypto_shash_extsize(struct crypto_alg *alg,
  353. const struct crypto_type *frontend)
  354. {
  355. return alg->cra_ctxsize;
  356. }
  357. static void crypto_shash_show(struct seq_file *m, struct crypto_alg *alg)
  358. __attribute__ ((unused));
  359. static void crypto_shash_show(struct seq_file *m, struct crypto_alg *alg)
  360. {
  361. struct shash_alg *salg = __crypto_shash_alg(alg);
  362. seq_printf(m, "type : shash\n");
  363. seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
  364. seq_printf(m, "digestsize : %u\n", salg->digestsize);
  365. seq_printf(m, "descsize : %u\n", salg->descsize);
  366. }
  367. static const struct crypto_type crypto_shash_type = {
  368. .ctxsize = crypto_shash_ctxsize,
  369. .extsize = crypto_shash_extsize,
  370. .init = crypto_init_shash_ops,
  371. .init_tfm = crypto_shash_init_tfm,
  372. #ifdef CONFIG_PROC_FS
  373. .show = crypto_shash_show,
  374. #endif
  375. .maskclear = ~CRYPTO_ALG_TYPE_MASK,
  376. .maskset = CRYPTO_ALG_TYPE_MASK,
  377. .type = CRYPTO_ALG_TYPE_SHASH,
  378. .tfmsize = offsetof(struct crypto_shash, base),
  379. };
  380. struct crypto_shash *crypto_alloc_shash(const char *alg_name, u32 type,
  381. u32 mask)
  382. {
  383. return __crypto_shash_cast(
  384. crypto_alloc_tfm(alg_name, &crypto_shash_type, type, mask));
  385. }
  386. EXPORT_SYMBOL_GPL(crypto_alloc_shash);
  387. int crypto_register_shash(struct shash_alg *alg)
  388. {
  389. struct crypto_alg *base = &alg->base;
  390. if (alg->digestsize > PAGE_SIZE / 8 ||
  391. alg->descsize > PAGE_SIZE / 8)
  392. return -EINVAL;
  393. base->cra_type = &crypto_shash_type;
  394. base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK;
  395. base->cra_flags |= CRYPTO_ALG_TYPE_SHASH;
  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. MODULE_LICENSE("GPL");
  405. MODULE_DESCRIPTION("Synchronous cryptographic hash type");