shash.c 15 KB

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