hash.h 7.4 KB

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