hash.h 6.7 KB

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