shash.c 14 KB

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