hash.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /*
  2. * Hash: Hash algorithms under the crypto API
  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. #ifndef _CRYPTO_HASH_H
  13. #define _CRYPTO_HASH_H
  14. #include <linux/crypto.h>
  15. struct shash_desc {
  16. struct crypto_shash *tfm;
  17. u32 flags;
  18. void *__ctx[] CRYPTO_MINALIGN_ATTR;
  19. };
  20. struct shash_alg {
  21. int (*init)(struct shash_desc *desc);
  22. int (*update)(struct shash_desc *desc, const u8 *data,
  23. unsigned int len);
  24. int (*final)(struct shash_desc *desc, u8 *out);
  25. int (*finup)(struct shash_desc *desc, const u8 *data,
  26. unsigned int len, u8 *out);
  27. int (*digest)(struct shash_desc *desc, const u8 *data,
  28. unsigned int len, u8 *out);
  29. int (*export)(struct shash_desc *desc, void *out);
  30. int (*import)(struct shash_desc *desc, const void *in);
  31. int (*setkey)(struct crypto_shash *tfm, const u8 *key,
  32. unsigned int keylen);
  33. unsigned int descsize;
  34. unsigned int digestsize;
  35. unsigned int statesize;
  36. struct crypto_alg base;
  37. };
  38. struct crypto_ahash {
  39. struct crypto_tfm base;
  40. };
  41. struct crypto_shash {
  42. struct crypto_tfm base;
  43. };
  44. static inline struct crypto_ahash *__crypto_ahash_cast(struct crypto_tfm *tfm)
  45. {
  46. return (struct crypto_ahash *)tfm;
  47. }
  48. static inline struct crypto_ahash *crypto_alloc_ahash(const char *alg_name,
  49. u32 type, u32 mask)
  50. {
  51. type &= ~CRYPTO_ALG_TYPE_MASK;
  52. mask &= ~CRYPTO_ALG_TYPE_MASK;
  53. type |= CRYPTO_ALG_TYPE_AHASH;
  54. mask |= CRYPTO_ALG_TYPE_AHASH_MASK;
  55. return __crypto_ahash_cast(crypto_alloc_base(alg_name, type, mask));
  56. }
  57. static inline struct crypto_tfm *crypto_ahash_tfm(struct crypto_ahash *tfm)
  58. {
  59. return &tfm->base;
  60. }
  61. static inline void crypto_free_ahash(struct crypto_ahash *tfm)
  62. {
  63. crypto_free_tfm(crypto_ahash_tfm(tfm));
  64. }
  65. static inline unsigned int crypto_ahash_alignmask(
  66. struct crypto_ahash *tfm)
  67. {
  68. return crypto_tfm_alg_alignmask(crypto_ahash_tfm(tfm));
  69. }
  70. static inline struct ahash_tfm *crypto_ahash_crt(struct crypto_ahash *tfm)
  71. {
  72. return &crypto_ahash_tfm(tfm)->crt_ahash;
  73. }
  74. static inline unsigned int crypto_ahash_digestsize(struct crypto_ahash *tfm)
  75. {
  76. return crypto_ahash_crt(tfm)->digestsize;
  77. }
  78. static inline u32 crypto_ahash_get_flags(struct crypto_ahash *tfm)
  79. {
  80. return crypto_tfm_get_flags(crypto_ahash_tfm(tfm));
  81. }
  82. static inline void crypto_ahash_set_flags(struct crypto_ahash *tfm, u32 flags)
  83. {
  84. crypto_tfm_set_flags(crypto_ahash_tfm(tfm), flags);
  85. }
  86. static inline void crypto_ahash_clear_flags(struct crypto_ahash *tfm, u32 flags)
  87. {
  88. crypto_tfm_clear_flags(crypto_ahash_tfm(tfm), flags);
  89. }
  90. static inline struct crypto_ahash *crypto_ahash_reqtfm(
  91. struct ahash_request *req)
  92. {
  93. return __crypto_ahash_cast(req->base.tfm);
  94. }
  95. static inline unsigned int crypto_ahash_reqsize(struct crypto_ahash *tfm)
  96. {
  97. return crypto_ahash_crt(tfm)->reqsize;
  98. }
  99. static inline void *ahash_request_ctx(struct ahash_request *req)
  100. {
  101. return req->__ctx;
  102. }
  103. static inline int crypto_ahash_setkey(struct crypto_ahash *tfm,
  104. const u8 *key, unsigned int keylen)
  105. {
  106. struct ahash_tfm *crt = crypto_ahash_crt(tfm);
  107. return crt->setkey(tfm, key, keylen);
  108. }
  109. static inline int crypto_ahash_digest(struct ahash_request *req)
  110. {
  111. struct ahash_tfm *crt = crypto_ahash_crt(crypto_ahash_reqtfm(req));
  112. return crt->digest(req);
  113. }
  114. static inline void crypto_ahash_export(struct ahash_request *req, u8 *out)
  115. {
  116. memcpy(out, ahash_request_ctx(req),
  117. crypto_ahash_reqsize(crypto_ahash_reqtfm(req)));
  118. }
  119. int crypto_ahash_import(struct ahash_request *req, const u8 *in);
  120. static inline int crypto_ahash_init(struct ahash_request *req)
  121. {
  122. struct ahash_tfm *crt = crypto_ahash_crt(crypto_ahash_reqtfm(req));
  123. return crt->init(req);
  124. }
  125. static inline int crypto_ahash_update(struct ahash_request *req)
  126. {
  127. struct ahash_tfm *crt = crypto_ahash_crt(crypto_ahash_reqtfm(req));
  128. return crt->update(req);
  129. }
  130. static inline int crypto_ahash_final(struct ahash_request *req)
  131. {
  132. struct ahash_tfm *crt = crypto_ahash_crt(crypto_ahash_reqtfm(req));
  133. return crt->final(req);
  134. }
  135. static inline void ahash_request_set_tfm(struct ahash_request *req,
  136. struct crypto_ahash *tfm)
  137. {
  138. req->base.tfm = crypto_ahash_tfm(tfm);
  139. }
  140. static inline struct ahash_request *ahash_request_alloc(
  141. struct crypto_ahash *tfm, gfp_t gfp)
  142. {
  143. struct ahash_request *req;
  144. req = kmalloc(sizeof(struct ahash_request) +
  145. crypto_ahash_reqsize(tfm), gfp);
  146. if (likely(req))
  147. ahash_request_set_tfm(req, tfm);
  148. return req;
  149. }
  150. static inline void ahash_request_free(struct ahash_request *req)
  151. {
  152. kfree(req);
  153. }
  154. static inline struct ahash_request *ahash_request_cast(
  155. struct crypto_async_request *req)
  156. {
  157. return container_of(req, struct ahash_request, base);
  158. }
  159. static inline void ahash_request_set_callback(struct ahash_request *req,
  160. u32 flags,
  161. crypto_completion_t complete,
  162. void *data)
  163. {
  164. req->base.complete = complete;
  165. req->base.data = data;
  166. req->base.flags = flags;
  167. }
  168. static inline void ahash_request_set_crypt(struct ahash_request *req,
  169. struct scatterlist *src, u8 *result,
  170. unsigned int nbytes)
  171. {
  172. req->src = src;
  173. req->nbytes = nbytes;
  174. req->result = result;
  175. }
  176. struct crypto_shash *crypto_alloc_shash(const char *alg_name, u32 type,
  177. u32 mask);
  178. static inline struct crypto_tfm *crypto_shash_tfm(struct crypto_shash *tfm)
  179. {
  180. return &tfm->base;
  181. }
  182. static inline void crypto_free_shash(struct crypto_shash *tfm)
  183. {
  184. crypto_destroy_tfm(tfm, crypto_shash_tfm(tfm));
  185. }
  186. static inline unsigned int crypto_shash_alignmask(
  187. struct crypto_shash *tfm)
  188. {
  189. return crypto_tfm_alg_alignmask(crypto_shash_tfm(tfm));
  190. }
  191. static inline unsigned int crypto_shash_blocksize(struct crypto_shash *tfm)
  192. {
  193. return crypto_tfm_alg_blocksize(crypto_shash_tfm(tfm));
  194. }
  195. static inline struct shash_alg *__crypto_shash_alg(struct crypto_alg *alg)
  196. {
  197. return container_of(alg, struct shash_alg, base);
  198. }
  199. static inline struct shash_alg *crypto_shash_alg(struct crypto_shash *tfm)
  200. {
  201. return __crypto_shash_alg(crypto_shash_tfm(tfm)->__crt_alg);
  202. }
  203. static inline unsigned int crypto_shash_digestsize(struct crypto_shash *tfm)
  204. {
  205. return crypto_shash_alg(tfm)->digestsize;
  206. }
  207. static inline unsigned int crypto_shash_statesize(struct crypto_shash *tfm)
  208. {
  209. return crypto_shash_alg(tfm)->statesize;
  210. }
  211. static inline u32 crypto_shash_get_flags(struct crypto_shash *tfm)
  212. {
  213. return crypto_tfm_get_flags(crypto_shash_tfm(tfm));
  214. }
  215. static inline void crypto_shash_set_flags(struct crypto_shash *tfm, u32 flags)
  216. {
  217. crypto_tfm_set_flags(crypto_shash_tfm(tfm), flags);
  218. }
  219. static inline void crypto_shash_clear_flags(struct crypto_shash *tfm, u32 flags)
  220. {
  221. crypto_tfm_clear_flags(crypto_shash_tfm(tfm), flags);
  222. }
  223. static inline unsigned int crypto_shash_descsize(struct crypto_shash *tfm)
  224. {
  225. return crypto_shash_alg(tfm)->descsize;
  226. }
  227. static inline void *shash_desc_ctx(struct shash_desc *desc)
  228. {
  229. return desc->__ctx;
  230. }
  231. int crypto_shash_setkey(struct crypto_shash *tfm, const u8 *key,
  232. unsigned int keylen);
  233. int crypto_shash_digest(struct shash_desc *desc, const u8 *data,
  234. unsigned int len, u8 *out);
  235. static inline int crypto_shash_export(struct shash_desc *desc, void *out)
  236. {
  237. return crypto_shash_alg(desc->tfm)->export(desc, out);
  238. }
  239. static inline int crypto_shash_import(struct shash_desc *desc, const void *in)
  240. {
  241. return crypto_shash_alg(desc->tfm)->import(desc, in);
  242. }
  243. static inline int crypto_shash_init(struct shash_desc *desc)
  244. {
  245. return crypto_shash_alg(desc->tfm)->init(desc);
  246. }
  247. int crypto_shash_update(struct shash_desc *desc, const u8 *data,
  248. unsigned int len);
  249. int crypto_shash_final(struct shash_desc *desc, u8 *out);
  250. int crypto_shash_finup(struct shash_desc *desc, const u8 *data,
  251. unsigned int len, u8 *out);
  252. #endif /* _CRYPTO_HASH_H */