hash.h 8.8 KB

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