hash.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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_hash_walk_first_compat(struct hash_desc *hdesc,
  45. struct crypto_hash_walk *walk,
  46. struct scatterlist *sg, unsigned int len);
  47. static inline int crypto_hash_walk_last(struct crypto_hash_walk *walk)
  48. {
  49. return !(walk->entrylen | walk->total);
  50. }
  51. int crypto_register_ahash(struct ahash_alg *alg);
  52. int crypto_unregister_ahash(struct ahash_alg *alg);
  53. int ahash_register_instance(struct crypto_template *tmpl,
  54. struct ahash_instance *inst);
  55. void ahash_free_instance(struct crypto_instance *inst);
  56. int crypto_init_ahash_spawn(struct crypto_ahash_spawn *spawn,
  57. struct hash_alg_common *alg,
  58. struct crypto_instance *inst);
  59. static inline void crypto_drop_ahash(struct crypto_ahash_spawn *spawn)
  60. {
  61. crypto_drop_spawn(&spawn->base);
  62. }
  63. struct hash_alg_common *ahash_attr_alg(struct rtattr *rta, u32 type, u32 mask);
  64. int crypto_register_shash(struct shash_alg *alg);
  65. int crypto_unregister_shash(struct shash_alg *alg);
  66. int shash_register_instance(struct crypto_template *tmpl,
  67. struct shash_instance *inst);
  68. void shash_free_instance(struct crypto_instance *inst);
  69. int crypto_init_shash_spawn(struct crypto_shash_spawn *spawn,
  70. struct shash_alg *alg,
  71. struct crypto_instance *inst);
  72. static inline void crypto_drop_shash(struct crypto_shash_spawn *spawn)
  73. {
  74. crypto_drop_spawn(&spawn->base);
  75. }
  76. struct shash_alg *shash_attr_alg(struct rtattr *rta, u32 type, u32 mask);
  77. int shash_ahash_update(struct ahash_request *req, struct shash_desc *desc);
  78. int shash_ahash_finup(struct ahash_request *req, struct shash_desc *desc);
  79. int shash_ahash_digest(struct ahash_request *req, struct shash_desc *desc);
  80. int crypto_init_shash_ops_async(struct crypto_tfm *tfm);
  81. static inline void *crypto_ahash_ctx(struct crypto_ahash *tfm)
  82. {
  83. return crypto_tfm_ctx(crypto_ahash_tfm(tfm));
  84. }
  85. static inline struct ahash_alg *__crypto_ahash_alg(struct crypto_alg *alg)
  86. {
  87. return container_of(__crypto_hash_alg_common(alg), struct ahash_alg,
  88. halg);
  89. }
  90. static inline void crypto_ahash_set_reqsize(struct crypto_ahash *tfm,
  91. unsigned int reqsize)
  92. {
  93. tfm->reqsize = reqsize;
  94. }
  95. static inline struct crypto_instance *ahash_crypto_instance(
  96. struct ahash_instance *inst)
  97. {
  98. return container_of(&inst->alg.halg.base, struct crypto_instance, alg);
  99. }
  100. static inline struct ahash_instance *ahash_instance(
  101. struct crypto_instance *inst)
  102. {
  103. return container_of(&inst->alg, struct ahash_instance, alg.halg.base);
  104. }
  105. static inline void *ahash_instance_ctx(struct ahash_instance *inst)
  106. {
  107. return crypto_instance_ctx(ahash_crypto_instance(inst));
  108. }
  109. static inline unsigned int ahash_instance_headroom(void)
  110. {
  111. return sizeof(struct ahash_alg) - sizeof(struct crypto_alg);
  112. }
  113. static inline struct ahash_instance *ahash_alloc_instance(
  114. const char *name, struct crypto_alg *alg)
  115. {
  116. return crypto_alloc_instance2(name, alg, ahash_instance_headroom());
  117. }
  118. static inline struct crypto_ahash *crypto_spawn_ahash(
  119. struct crypto_ahash_spawn *spawn)
  120. {
  121. return crypto_spawn_tfm2(&spawn->base);
  122. }
  123. static inline int ahash_enqueue_request(struct crypto_queue *queue,
  124. struct ahash_request *request)
  125. {
  126. return crypto_enqueue_request(queue, &request->base);
  127. }
  128. static inline struct ahash_request *ahash_dequeue_request(
  129. struct crypto_queue *queue)
  130. {
  131. return ahash_request_cast(crypto_dequeue_request(queue));
  132. }
  133. static inline int ahash_tfm_in_queue(struct crypto_queue *queue,
  134. struct crypto_ahash *tfm)
  135. {
  136. return crypto_tfm_in_queue(queue, crypto_ahash_tfm(tfm));
  137. }
  138. static inline void *crypto_shash_ctx(struct crypto_shash *tfm)
  139. {
  140. return crypto_tfm_ctx(&tfm->base);
  141. }
  142. static inline struct crypto_instance *shash_crypto_instance(
  143. struct shash_instance *inst)
  144. {
  145. return container_of(&inst->alg.base, struct crypto_instance, alg);
  146. }
  147. static inline struct shash_instance *shash_instance(
  148. struct crypto_instance *inst)
  149. {
  150. return container_of(__crypto_shash_alg(&inst->alg),
  151. struct shash_instance, alg);
  152. }
  153. static inline void *shash_instance_ctx(struct shash_instance *inst)
  154. {
  155. return crypto_instance_ctx(shash_crypto_instance(inst));
  156. }
  157. static inline struct shash_instance *shash_alloc_instance(
  158. const char *name, struct crypto_alg *alg)
  159. {
  160. return crypto_alloc_instance2(name, alg,
  161. sizeof(struct shash_alg) - sizeof(*alg));
  162. }
  163. static inline struct crypto_shash *crypto_spawn_shash(
  164. struct crypto_shash_spawn *spawn)
  165. {
  166. return crypto_spawn_tfm2(&spawn->base);
  167. }
  168. static inline void *crypto_shash_ctx_aligned(struct crypto_shash *tfm)
  169. {
  170. return crypto_tfm_ctx_aligned(&tfm->base);
  171. }
  172. static inline struct crypto_shash *__crypto_shash_cast(struct crypto_tfm *tfm)
  173. {
  174. return container_of(tfm, struct crypto_shash, base);
  175. }
  176. #endif /* _CRYPTO_INTERNAL_HASH_H */