hash.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * Hash algorithms.
  4. *
  5. * Copyright (c) 2008 Herbert Xu <[email protected]>
  6. */
  7. #ifndef _CRYPTO_INTERNAL_HASH_H
  8. #define _CRYPTO_INTERNAL_HASH_H
  9. #include <crypto/algapi.h>
  10. #include <crypto/hash.h>
  11. struct ahash_request;
  12. struct scatterlist;
  13. struct crypto_hash_walk {
  14. char *data;
  15. unsigned int offset;
  16. unsigned int alignmask;
  17. struct page *pg;
  18. unsigned int entrylen;
  19. unsigned int total;
  20. struct scatterlist *sg;
  21. unsigned int flags;
  22. };
  23. struct ahash_instance {
  24. void (*free)(struct ahash_instance *inst);
  25. union {
  26. struct {
  27. char head[offsetof(struct ahash_alg, halg.base)];
  28. struct crypto_instance base;
  29. } s;
  30. struct ahash_alg alg;
  31. };
  32. };
  33. struct shash_instance {
  34. void (*free)(struct shash_instance *inst);
  35. union {
  36. struct {
  37. char head[offsetof(struct shash_alg, base)];
  38. struct crypto_instance base;
  39. } s;
  40. struct shash_alg alg;
  41. };
  42. };
  43. struct crypto_ahash_spawn {
  44. struct crypto_spawn base;
  45. };
  46. struct crypto_shash_spawn {
  47. struct crypto_spawn base;
  48. };
  49. int crypto_hash_walk_done(struct crypto_hash_walk *walk, int err);
  50. int crypto_hash_walk_first(struct ahash_request *req,
  51. struct crypto_hash_walk *walk);
  52. static inline int crypto_hash_walk_last(struct crypto_hash_walk *walk)
  53. {
  54. return !(walk->entrylen | walk->total);
  55. }
  56. int crypto_register_ahash(struct ahash_alg *alg);
  57. void crypto_unregister_ahash(struct ahash_alg *alg);
  58. int crypto_register_ahashes(struct ahash_alg *algs, int count);
  59. void crypto_unregister_ahashes(struct ahash_alg *algs, int count);
  60. int ahash_register_instance(struct crypto_template *tmpl,
  61. struct ahash_instance *inst);
  62. bool crypto_shash_alg_has_setkey(struct shash_alg *alg);
  63. static inline bool crypto_shash_alg_needs_key(struct shash_alg *alg)
  64. {
  65. return crypto_shash_alg_has_setkey(alg) &&
  66. !(alg->base.cra_flags & CRYPTO_ALG_OPTIONAL_KEY);
  67. }
  68. bool crypto_hash_alg_has_setkey(struct hash_alg_common *halg);
  69. int crypto_grab_ahash(struct crypto_ahash_spawn *spawn,
  70. struct crypto_instance *inst,
  71. const char *name, u32 type, u32 mask);
  72. static inline void crypto_drop_ahash(struct crypto_ahash_spawn *spawn)
  73. {
  74. crypto_drop_spawn(&spawn->base);
  75. }
  76. static inline struct hash_alg_common *crypto_spawn_ahash_alg(
  77. struct crypto_ahash_spawn *spawn)
  78. {
  79. return __crypto_hash_alg_common(spawn->base.alg);
  80. }
  81. int crypto_register_shash(struct shash_alg *alg);
  82. void crypto_unregister_shash(struct shash_alg *alg);
  83. int crypto_register_shashes(struct shash_alg *algs, int count);
  84. void crypto_unregister_shashes(struct shash_alg *algs, int count);
  85. int shash_register_instance(struct crypto_template *tmpl,
  86. struct shash_instance *inst);
  87. void shash_free_singlespawn_instance(struct shash_instance *inst);
  88. int crypto_grab_shash(struct crypto_shash_spawn *spawn,
  89. struct crypto_instance *inst,
  90. const char *name, u32 type, u32 mask);
  91. static inline void crypto_drop_shash(struct crypto_shash_spawn *spawn)
  92. {
  93. crypto_drop_spawn(&spawn->base);
  94. }
  95. static inline struct shash_alg *crypto_spawn_shash_alg(
  96. struct crypto_shash_spawn *spawn)
  97. {
  98. return __crypto_shash_alg(spawn->base.alg);
  99. }
  100. int shash_ahash_update(struct ahash_request *req, struct shash_desc *desc);
  101. int shash_ahash_finup(struct ahash_request *req, struct shash_desc *desc);
  102. int shash_ahash_digest(struct ahash_request *req, struct shash_desc *desc);
  103. int crypto_init_shash_ops_async(struct crypto_tfm *tfm);
  104. static inline void *crypto_ahash_ctx(struct crypto_ahash *tfm)
  105. {
  106. return crypto_tfm_ctx(crypto_ahash_tfm(tfm));
  107. }
  108. static inline struct ahash_alg *__crypto_ahash_alg(struct crypto_alg *alg)
  109. {
  110. return container_of(__crypto_hash_alg_common(alg), struct ahash_alg,
  111. halg);
  112. }
  113. static inline void crypto_ahash_set_reqsize(struct crypto_ahash *tfm,
  114. unsigned int reqsize)
  115. {
  116. tfm->reqsize = reqsize;
  117. }
  118. static inline struct crypto_instance *ahash_crypto_instance(
  119. struct ahash_instance *inst)
  120. {
  121. return &inst->s.base;
  122. }
  123. static inline struct ahash_instance *ahash_instance(
  124. struct crypto_instance *inst)
  125. {
  126. return container_of(inst, struct ahash_instance, s.base);
  127. }
  128. static inline struct ahash_instance *ahash_alg_instance(
  129. struct crypto_ahash *ahash)
  130. {
  131. return ahash_instance(crypto_tfm_alg_instance(&ahash->base));
  132. }
  133. static inline void *ahash_instance_ctx(struct ahash_instance *inst)
  134. {
  135. return crypto_instance_ctx(ahash_crypto_instance(inst));
  136. }
  137. static inline void ahash_request_complete(struct ahash_request *req, int err)
  138. {
  139. req->base.complete(&req->base, err);
  140. }
  141. static inline u32 ahash_request_flags(struct ahash_request *req)
  142. {
  143. return req->base.flags;
  144. }
  145. static inline struct crypto_ahash *crypto_spawn_ahash(
  146. struct crypto_ahash_spawn *spawn)
  147. {
  148. return crypto_spawn_tfm2(&spawn->base);
  149. }
  150. static inline int ahash_enqueue_request(struct crypto_queue *queue,
  151. struct ahash_request *request)
  152. {
  153. return crypto_enqueue_request(queue, &request->base);
  154. }
  155. static inline struct ahash_request *ahash_dequeue_request(
  156. struct crypto_queue *queue)
  157. {
  158. return ahash_request_cast(crypto_dequeue_request(queue));
  159. }
  160. static inline void *crypto_shash_ctx(struct crypto_shash *tfm)
  161. {
  162. return crypto_tfm_ctx(&tfm->base);
  163. }
  164. static inline struct crypto_instance *shash_crypto_instance(
  165. struct shash_instance *inst)
  166. {
  167. return &inst->s.base;
  168. }
  169. static inline struct shash_instance *shash_instance(
  170. struct crypto_instance *inst)
  171. {
  172. return container_of(inst, struct shash_instance, s.base);
  173. }
  174. static inline struct shash_instance *shash_alg_instance(
  175. struct crypto_shash *shash)
  176. {
  177. return shash_instance(crypto_tfm_alg_instance(&shash->base));
  178. }
  179. static inline void *shash_instance_ctx(struct shash_instance *inst)
  180. {
  181. return crypto_instance_ctx(shash_crypto_instance(inst));
  182. }
  183. static inline struct crypto_shash *crypto_spawn_shash(
  184. struct crypto_shash_spawn *spawn)
  185. {
  186. return crypto_spawn_tfm2(&spawn->base);
  187. }
  188. static inline void *crypto_shash_ctx_aligned(struct crypto_shash *tfm)
  189. {
  190. return crypto_tfm_ctx_aligned(&tfm->base);
  191. }
  192. static inline struct crypto_shash *__crypto_shash_cast(struct crypto_tfm *tfm)
  193. {
  194. return container_of(tfm, struct crypto_shash, base);
  195. }
  196. #endif /* _CRYPTO_INTERNAL_HASH_H */