hash.h 8.8 KB

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