hash.h 8.8 KB

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