hash.h 8.8 KB

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