shash.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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 ((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 buf[shash_align_buffer_size(unaligned_len, alignmask)]
  68. __attribute__ ((aligned));
  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_update(desc, data, len) ?:
  130. crypto_shash_final(desc, out);
  131. }
  132. int crypto_shash_digest(struct shash_desc *desc, const u8 *data,
  133. unsigned int len, u8 *out)
  134. {
  135. struct crypto_shash *tfm = desc->tfm;
  136. struct shash_alg *shash = crypto_shash_alg(tfm);
  137. unsigned long alignmask = crypto_shash_alignmask(tfm);
  138. if (((unsigned long)data | (unsigned long)out) & alignmask ||
  139. !shash->digest)
  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_async_setkey(struct crypto_ahash *tfm, const u8 *key,
  145. unsigned int keylen)
  146. {
  147. struct crypto_shash **ctx = crypto_ahash_ctx(tfm);
  148. return crypto_shash_setkey(*ctx, key, keylen);
  149. }
  150. static int shash_async_init(struct ahash_request *req)
  151. {
  152. struct crypto_shash **ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(req));
  153. struct shash_desc *desc = ahash_request_ctx(req);
  154. desc->tfm = *ctx;
  155. desc->flags = req->base.flags;
  156. return crypto_shash_init(desc);
  157. }
  158. static int shash_async_update(struct ahash_request *req)
  159. {
  160. struct shash_desc *desc = ahash_request_ctx(req);
  161. struct crypto_hash_walk walk;
  162. int nbytes;
  163. for (nbytes = crypto_hash_walk_first(req, &walk); nbytes > 0;
  164. nbytes = crypto_hash_walk_done(&walk, nbytes))
  165. nbytes = crypto_shash_update(desc, walk.data, nbytes);
  166. return nbytes;
  167. }
  168. static int shash_async_final(struct ahash_request *req)
  169. {
  170. return crypto_shash_final(ahash_request_ctx(req), req->result);
  171. }
  172. static int shash_async_digest(struct ahash_request *req)
  173. {
  174. struct scatterlist *sg = req->src;
  175. unsigned int offset = sg->offset;
  176. unsigned int nbytes = req->nbytes;
  177. int err;
  178. if (nbytes < min(sg->length, ((unsigned int)(PAGE_SIZE)) - offset)) {
  179. struct crypto_shash **ctx =
  180. crypto_ahash_ctx(crypto_ahash_reqtfm(req));
  181. struct shash_desc *desc = ahash_request_ctx(req);
  182. void *data;
  183. desc->tfm = *ctx;
  184. desc->flags = req->base.flags;
  185. data = crypto_kmap(sg_page(sg), 0);
  186. err = crypto_shash_digest(desc, data + offset, nbytes,
  187. req->result);
  188. crypto_kunmap(data, 0);
  189. crypto_yield(desc->flags);
  190. goto out;
  191. }
  192. err = shash_async_init(req);
  193. if (err)
  194. goto out;
  195. err = shash_async_update(req);
  196. if (err)
  197. goto out;
  198. err = shash_async_final(req);
  199. out:
  200. return err;
  201. }
  202. static void crypto_exit_shash_ops_async(struct crypto_tfm *tfm)
  203. {
  204. struct crypto_shash **ctx = crypto_tfm_ctx(tfm);
  205. crypto_free_shash(*ctx);
  206. }
  207. static int crypto_init_shash_ops_async(struct crypto_tfm *tfm)
  208. {
  209. struct crypto_alg *calg = tfm->__crt_alg;
  210. struct shash_alg *alg = __crypto_shash_alg(calg);
  211. struct ahash_tfm *crt = &tfm->crt_ahash;
  212. struct crypto_shash **ctx = crypto_tfm_ctx(tfm);
  213. struct crypto_shash *shash;
  214. if (!crypto_mod_get(calg))
  215. return -EAGAIN;
  216. shash = __crypto_shash_cast(crypto_create_tfm(
  217. calg, &crypto_shash_type));
  218. if (IS_ERR(shash)) {
  219. crypto_mod_put(calg);
  220. return PTR_ERR(shash);
  221. }
  222. *ctx = shash;
  223. tfm->exit = crypto_exit_shash_ops_async;
  224. crt->init = shash_async_init;
  225. crt->update = shash_async_update;
  226. crt->final = shash_async_final;
  227. crt->digest = shash_async_digest;
  228. crt->setkey = shash_async_setkey;
  229. crt->digestsize = alg->digestsize;
  230. crt->reqsize = sizeof(struct shash_desc) + crypto_shash_descsize(shash);
  231. return 0;
  232. }
  233. static int crypto_init_shash_ops(struct crypto_tfm *tfm, u32 type, u32 mask)
  234. {
  235. switch (mask & CRYPTO_ALG_TYPE_MASK) {
  236. case CRYPTO_ALG_TYPE_AHASH_MASK:
  237. return crypto_init_shash_ops_async(tfm);
  238. }
  239. return -EINVAL;
  240. }
  241. static unsigned int crypto_shash_ctxsize(struct crypto_alg *alg, u32 type,
  242. u32 mask)
  243. {
  244. switch (mask & CRYPTO_ALG_TYPE_MASK) {
  245. case CRYPTO_ALG_TYPE_AHASH_MASK:
  246. return sizeof(struct crypto_shash *);
  247. }
  248. return 0;
  249. }
  250. static int crypto_shash_init_tfm(struct crypto_tfm *tfm,
  251. const struct crypto_type *frontend)
  252. {
  253. if (frontend->type != CRYPTO_ALG_TYPE_SHASH)
  254. return -EINVAL;
  255. return 0;
  256. }
  257. static unsigned int crypto_shash_extsize(struct crypto_alg *alg,
  258. const struct crypto_type *frontend)
  259. {
  260. return alg->cra_ctxsize;
  261. }
  262. static void crypto_shash_show(struct seq_file *m, struct crypto_alg *alg)
  263. __attribute__ ((unused));
  264. static void crypto_shash_show(struct seq_file *m, struct crypto_alg *alg)
  265. {
  266. struct shash_alg *salg = __crypto_shash_alg(alg);
  267. seq_printf(m, "type : shash\n");
  268. seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
  269. seq_printf(m, "digestsize : %u\n", salg->digestsize);
  270. seq_printf(m, "descsize : %u\n", salg->descsize);
  271. }
  272. static const struct crypto_type crypto_shash_type = {
  273. .ctxsize = crypto_shash_ctxsize,
  274. .extsize = crypto_shash_extsize,
  275. .init = crypto_init_shash_ops,
  276. .init_tfm = crypto_shash_init_tfm,
  277. #ifdef CONFIG_PROC_FS
  278. .show = crypto_shash_show,
  279. #endif
  280. .maskclear = ~CRYPTO_ALG_TYPE_MASK,
  281. .maskset = CRYPTO_ALG_TYPE_MASK,
  282. .type = CRYPTO_ALG_TYPE_SHASH,
  283. .tfmsize = offsetof(struct crypto_shash, base),
  284. };
  285. struct crypto_shash *crypto_alloc_shash(const char *alg_name, u32 type,
  286. u32 mask)
  287. {
  288. return __crypto_shash_cast(
  289. crypto_alloc_tfm(alg_name, &crypto_shash_type, type, mask));
  290. }
  291. EXPORT_SYMBOL_GPL(crypto_alloc_shash);
  292. int crypto_register_shash(struct shash_alg *alg)
  293. {
  294. struct crypto_alg *base = &alg->base;
  295. if (alg->digestsize > PAGE_SIZE / 8 ||
  296. alg->descsize > PAGE_SIZE / 8)
  297. return -EINVAL;
  298. base->cra_type = &crypto_shash_type;
  299. base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK;
  300. base->cra_flags |= CRYPTO_ALG_TYPE_SHASH;
  301. return crypto_register_alg(base);
  302. }
  303. EXPORT_SYMBOL_GPL(crypto_register_shash);
  304. int crypto_unregister_shash(struct shash_alg *alg)
  305. {
  306. return crypto_unregister_alg(&alg->base);
  307. }
  308. EXPORT_SYMBOL_GPL(crypto_unregister_shash);
  309. MODULE_LICENSE("GPL");
  310. MODULE_DESCRIPTION("Synchronous cryptographic hash type");