hash.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*
  2. * Hash algorithms.
  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_INTERNAL_HASH_H
  13. #define _CRYPTO_INTERNAL_HASH_H
  14. #include <crypto/algapi.h>
  15. #include <crypto/hash.h>
  16. struct ahash_request;
  17. struct scatterlist;
  18. struct crypto_hash_walk {
  19. char *data;
  20. unsigned int offset;
  21. unsigned int alignmask;
  22. struct page *pg;
  23. unsigned int entrylen;
  24. unsigned int total;
  25. struct scatterlist *sg;
  26. unsigned int flags;
  27. };
  28. struct ahash_instance {
  29. struct ahash_alg alg;
  30. };
  31. struct shash_instance {
  32. struct shash_alg alg;
  33. };
  34. struct crypto_ahash_spawn {
  35. struct crypto_spawn base;
  36. };
  37. struct crypto_shash_spawn {
  38. struct crypto_spawn base;
  39. };
  40. extern const struct crypto_type crypto_ahash_type;
  41. int crypto_hash_walk_done(struct crypto_hash_walk *walk, int err);
  42. int crypto_hash_walk_first(struct ahash_request *req,
  43. struct crypto_hash_walk *walk);
  44. int crypto_ahash_walk_first(struct ahash_request *req,
  45. struct crypto_hash_walk *walk);
  46. static inline int crypto_ahash_walk_done(struct crypto_hash_walk *walk,
  47. int err)
  48. {
  49. return crypto_hash_walk_done(walk, err);
  50. }
  51. static inline int crypto_hash_walk_last(struct crypto_hash_walk *walk)
  52. {
  53. return !(walk->entrylen | walk->total);
  54. }
  55. static inline int crypto_ahash_walk_last(struct crypto_hash_walk *walk)
  56. {
  57. return crypto_hash_walk_last(walk);
  58. }
  59. int crypto_register_ahash(struct ahash_alg *alg);
  60. int crypto_unregister_ahash(struct ahash_alg *alg);
  61. int crypto_register_ahashes(struct ahash_alg *algs, int count);
  62. void crypto_unregister_ahashes(struct ahash_alg *algs, int count);
  63. int ahash_register_instance(struct crypto_template *tmpl,
  64. struct ahash_instance *inst);
  65. void ahash_free_instance(struct crypto_instance *inst);
  66. int shash_no_setkey(struct crypto_shash *tfm, const u8 *key,
  67. unsigned int keylen);
  68. static inline bool crypto_shash_alg_has_setkey(struct shash_alg *alg)
  69. {
  70. return alg->setkey != shash_no_setkey;
  71. }
  72. int crypto_init_ahash_spawn(struct crypto_ahash_spawn *spawn,
  73. struct hash_alg_common *alg,
  74. struct crypto_instance *inst);
  75. static inline void crypto_drop_ahash(struct crypto_ahash_spawn *spawn)
  76. {
  77. crypto_drop_spawn(&spawn->base);
  78. }
  79. struct hash_alg_common *ahash_attr_alg(struct rtattr *rta, u32 type, u32 mask);
  80. int crypto_register_shash(struct shash_alg *alg);
  81. int crypto_unregister_shash(struct shash_alg *alg);
  82. int crypto_register_shashes(struct shash_alg *algs, int count);
  83. int crypto_unregister_shashes(struct shash_alg *algs, int count);
  84. int shash_register_instance(struct crypto_template *tmpl,
  85. struct shash_instance *inst);
  86. void shash_free_instance(struct crypto_instance *inst);
  87. int crypto_init_shash_spawn(struct crypto_shash_spawn *spawn,
  88. struct shash_alg *alg,
  89. struct crypto_instance *inst);
  90. static inline void crypto_drop_shash(struct crypto_shash_spawn *spawn)
  91. {
  92. crypto_drop_spawn(&spawn->base);
  93. }
  94. struct shash_alg *shash_attr_alg(struct rtattr *rta, u32 type, u32 mask);
  95. int shash_ahash_update(struct ahash_request *req, struct shash_desc *desc);
  96. int shash_ahash_finup(struct ahash_request *req, struct shash_desc *desc);
  97. int shash_ahash_digest(struct ahash_request *req, struct shash_desc *desc);
  98. int ahash_mcryptd_update(struct ahash_request *desc);
  99. int ahash_mcryptd_final(struct ahash_request *desc);
  100. int ahash_mcryptd_finup(struct ahash_request *desc);
  101. int ahash_mcryptd_digest(struct ahash_request *desc);
  102. int crypto_init_shash_ops_async(struct crypto_tfm *tfm);
  103. static inline void *crypto_ahash_ctx(struct crypto_ahash *tfm)
  104. {
  105. return crypto_tfm_ctx(crypto_ahash_tfm(tfm));
  106. }
  107. static inline struct ahash_alg *__crypto_ahash_alg(struct crypto_alg *alg)
  108. {
  109. return container_of(__crypto_hash_alg_common(alg), struct ahash_alg,
  110. halg);
  111. }
  112. static inline void crypto_ahash_set_reqsize(struct crypto_ahash *tfm,
  113. unsigned int reqsize)
  114. {
  115. tfm->reqsize = reqsize;
  116. }
  117. static inline struct crypto_instance *ahash_crypto_instance(
  118. struct ahash_instance *inst)
  119. {
  120. return container_of(&inst->alg.halg.base, struct crypto_instance, alg);
  121. }
  122. static inline struct ahash_instance *ahash_instance(
  123. struct crypto_instance *inst)
  124. {
  125. return container_of(&inst->alg, struct ahash_instance, alg.halg.base);
  126. }
  127. static inline void *ahash_instance_ctx(struct ahash_instance *inst)
  128. {
  129. return crypto_instance_ctx(ahash_crypto_instance(inst));
  130. }
  131. static inline unsigned int ahash_instance_headroom(void)
  132. {
  133. return sizeof(struct ahash_alg) - sizeof(struct crypto_alg);
  134. }
  135. static inline struct ahash_instance *ahash_alloc_instance(
  136. const char *name, struct crypto_alg *alg)
  137. {
  138. return crypto_alloc_instance2(name, alg, ahash_instance_headroom());
  139. }
  140. static inline void ahash_request_complete(struct ahash_request *req, int err)
  141. {
  142. req->base.complete(&req->base, err);
  143. }
  144. static inline u32 ahash_request_flags(struct ahash_request *req)
  145. {
  146. return req->base.flags;
  147. }
  148. static inline struct crypto_ahash *crypto_spawn_ahash(
  149. struct crypto_ahash_spawn *spawn)
  150. {
  151. return crypto_spawn_tfm2(&spawn->base);
  152. }
  153. static inline int ahash_enqueue_request(struct crypto_queue *queue,
  154. struct ahash_request *request)
  155. {
  156. return crypto_enqueue_request(queue, &request->base);
  157. }
  158. static inline struct ahash_request *ahash_dequeue_request(
  159. struct crypto_queue *queue)
  160. {
  161. return ahash_request_cast(crypto_dequeue_request(queue));
  162. }
  163. static inline int ahash_tfm_in_queue(struct crypto_queue *queue,
  164. struct crypto_ahash *tfm)
  165. {
  166. return crypto_tfm_in_queue(queue, crypto_ahash_tfm(tfm));
  167. }
  168. static inline void *crypto_shash_ctx(struct crypto_shash *tfm)
  169. {
  170. return crypto_tfm_ctx(&tfm->base);
  171. }
  172. static inline struct crypto_instance *shash_crypto_instance(
  173. struct shash_instance *inst)
  174. {
  175. return container_of(&inst->alg.base, struct crypto_instance, alg);
  176. }
  177. static inline struct shash_instance *shash_instance(
  178. struct crypto_instance *inst)
  179. {
  180. return container_of(__crypto_shash_alg(&inst->alg),
  181. struct shash_instance, alg);
  182. }
  183. static inline void *shash_instance_ctx(struct shash_instance *inst)
  184. {
  185. return crypto_instance_ctx(shash_crypto_instance(inst));
  186. }
  187. static inline struct shash_instance *shash_alloc_instance(
  188. const char *name, struct crypto_alg *alg)
  189. {
  190. return crypto_alloc_instance2(name, alg,
  191. sizeof(struct shash_alg) - sizeof(*alg));
  192. }
  193. static inline struct crypto_shash *crypto_spawn_shash(
  194. struct crypto_shash_spawn *spawn)
  195. {
  196. return crypto_spawn_tfm2(&spawn->base);
  197. }
  198. static inline void *crypto_shash_ctx_aligned(struct crypto_shash *tfm)
  199. {
  200. return crypto_tfm_ctx_aligned(&tfm->base);
  201. }
  202. static inline struct crypto_shash *__crypto_shash_cast(struct crypto_tfm *tfm)
  203. {
  204. return container_of(tfm, struct crypto_shash, base);
  205. }
  206. #endif /* _CRYPTO_INTERNAL_HASH_H */