hash.h 6.7 KB

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