hash.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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. /* Set this bit to handle partial blocks in the API. */
  12. #define CRYPTO_AHASH_ALG_BLOCK_ONLY 0x01000000
  13. /* Set this bit if final requires at least one byte. */
  14. #define CRYPTO_AHASH_ALG_FINAL_NONZERO 0x02000000
  15. /* Set this bit if finup can deal with multiple blocks. */
  16. #define CRYPTO_AHASH_ALG_FINUP_MAX 0x04000000
  17. /* This bit is set by the Crypto API if export_core is not supported. */
  18. #define CRYPTO_AHASH_ALG_NO_EXPORT_CORE 0x08000000
  19. #define HASH_FBREQ_ON_STACK(name, req) \
  20. char __##name##_req[sizeof(struct ahash_request) + \
  21. MAX_SYNC_HASH_REQSIZE] CRYPTO_MINALIGN_ATTR; \
  22. struct ahash_request *name = ahash_fbreq_on_stack_init( \
  23. __##name##_req, (req))
  24. struct ahash_request;
  25. struct scatterlist;
  26. struct crypto_hash_walk {
  27. const char *data;
  28. unsigned int offset;
  29. unsigned int flags;
  30. struct page *pg;
  31. unsigned int entrylen;
  32. unsigned int total;
  33. struct scatterlist *sg;
  34. };
  35. struct ahash_instance {
  36. void (*free)(struct ahash_instance *inst);
  37. union {
  38. struct {
  39. char head[offsetof(struct ahash_alg, halg.base)];
  40. struct crypto_instance base;
  41. } s;
  42. struct ahash_alg alg;
  43. };
  44. };
  45. struct shash_instance {
  46. void (*free)(struct shash_instance *inst);
  47. union {
  48. struct {
  49. char head[offsetof(struct shash_alg, base)];
  50. struct crypto_instance base;
  51. } s;
  52. struct shash_alg alg;
  53. };
  54. };
  55. struct crypto_ahash_spawn {
  56. struct crypto_spawn base;
  57. };
  58. struct crypto_shash_spawn {
  59. struct crypto_spawn base;
  60. };
  61. int crypto_hash_walk_done(struct crypto_hash_walk *walk, int err);
  62. int crypto_hash_walk_first(struct ahash_request *req,
  63. struct crypto_hash_walk *walk);
  64. static inline int crypto_hash_walk_last(struct crypto_hash_walk *walk)
  65. {
  66. return !(walk->entrylen | walk->total);
  67. }
  68. int crypto_register_ahash(struct ahash_alg *alg);
  69. void crypto_unregister_ahash(struct ahash_alg *alg);
  70. int crypto_register_ahashes(struct ahash_alg *algs, int count);
  71. void crypto_unregister_ahashes(struct ahash_alg *algs, int count);
  72. int ahash_register_instance(struct crypto_template *tmpl,
  73. struct ahash_instance *inst);
  74. void ahash_free_singlespawn_instance(struct ahash_instance *inst);
  75. int shash_no_setkey(struct crypto_shash *tfm, const u8 *key,
  76. unsigned int keylen);
  77. static inline bool crypto_shash_alg_has_setkey(struct shash_alg *alg)
  78. {
  79. return alg->setkey != shash_no_setkey;
  80. }
  81. bool crypto_hash_alg_has_setkey(struct hash_alg_common *halg);
  82. static inline bool crypto_shash_alg_needs_key(struct shash_alg *alg)
  83. {
  84. return crypto_shash_alg_has_setkey(alg) &&
  85. !(alg->base.cra_flags & CRYPTO_ALG_OPTIONAL_KEY);
  86. }
  87. static inline bool crypto_hash_alg_needs_key(struct hash_alg_common *alg)
  88. {
  89. return crypto_hash_alg_has_setkey(alg) &&
  90. !(alg->base.cra_flags & CRYPTO_ALG_OPTIONAL_KEY);
  91. }
  92. static inline bool crypto_hash_no_export_core(struct crypto_ahash *tfm)
  93. {
  94. return crypto_hash_alg_common(tfm)->base.cra_flags &
  95. CRYPTO_AHASH_ALG_NO_EXPORT_CORE;
  96. }
  97. int crypto_grab_ahash(struct crypto_ahash_spawn *spawn,
  98. struct crypto_instance *inst,
  99. const char *name, u32 type, u32 mask);
  100. static inline void crypto_drop_ahash(struct crypto_ahash_spawn *spawn)
  101. {
  102. crypto_drop_spawn(&spawn->base);
  103. }
  104. static inline struct hash_alg_common *crypto_spawn_ahash_alg(
  105. struct crypto_ahash_spawn *spawn)
  106. {
  107. return __crypto_hash_alg_common(spawn->base.alg);
  108. }
  109. int crypto_register_shash(struct shash_alg *alg);
  110. void crypto_unregister_shash(struct shash_alg *alg);
  111. int crypto_register_shashes(struct shash_alg *algs, int count);
  112. void crypto_unregister_shashes(struct shash_alg *algs, int count);
  113. int shash_register_instance(struct crypto_template *tmpl,
  114. struct shash_instance *inst);
  115. void shash_free_singlespawn_instance(struct shash_instance *inst);
  116. int crypto_grab_shash(struct crypto_shash_spawn *spawn,
  117. struct crypto_instance *inst,
  118. const char *name, u32 type, u32 mask);
  119. static inline void crypto_drop_shash(struct crypto_shash_spawn *spawn)
  120. {
  121. crypto_drop_spawn(&spawn->base);
  122. }
  123. static inline struct shash_alg *crypto_spawn_shash_alg(
  124. struct crypto_shash_spawn *spawn)
  125. {
  126. return __crypto_shash_alg(spawn->base.alg);
  127. }
  128. int shash_ahash_update(struct ahash_request *req, struct shash_desc *desc);
  129. int shash_ahash_finup(struct ahash_request *req, struct shash_desc *desc);
  130. int shash_ahash_digest(struct ahash_request *req, struct shash_desc *desc);
  131. static inline void *crypto_ahash_ctx(struct crypto_ahash *tfm)
  132. {
  133. return crypto_tfm_ctx(crypto_ahash_tfm(tfm));
  134. }
  135. static inline void *crypto_ahash_ctx_dma(struct crypto_ahash *tfm)
  136. {
  137. return crypto_tfm_ctx_dma(crypto_ahash_tfm(tfm));
  138. }
  139. static inline struct ahash_alg *__crypto_ahash_alg(struct crypto_alg *alg)
  140. {
  141. return container_of(__crypto_hash_alg_common(alg), struct ahash_alg,
  142. halg);
  143. }
  144. static inline struct ahash_alg *crypto_ahash_alg(struct crypto_ahash *hash)
  145. {
  146. return container_of(crypto_hash_alg_common(hash), struct ahash_alg,
  147. halg);
  148. }
  149. static inline void crypto_ahash_set_statesize(struct crypto_ahash *tfm,
  150. unsigned int size)
  151. {
  152. tfm->statesize = size;
  153. }
  154. static inline void crypto_ahash_set_reqsize(struct crypto_ahash *tfm,
  155. unsigned int reqsize)
  156. {
  157. tfm->reqsize = reqsize;
  158. }
  159. static inline bool crypto_ahash_tested(struct crypto_ahash *tfm)
  160. {
  161. struct crypto_tfm *tfm_base = crypto_ahash_tfm(tfm);
  162. return tfm_base->__crt_alg->cra_flags & CRYPTO_ALG_TESTED;
  163. }
  164. static inline void crypto_ahash_set_reqsize_dma(struct crypto_ahash *ahash,
  165. unsigned int reqsize)
  166. {
  167. reqsize += crypto_dma_align() & ~(crypto_tfm_ctx_alignment() - 1);
  168. ahash->reqsize = reqsize;
  169. }
  170. static inline struct crypto_instance *ahash_crypto_instance(
  171. struct ahash_instance *inst)
  172. {
  173. return &inst->s.base;
  174. }
  175. static inline struct ahash_instance *ahash_instance(
  176. struct crypto_instance *inst)
  177. {
  178. return container_of(inst, struct ahash_instance, s.base);
  179. }
  180. static inline struct ahash_instance *ahash_alg_instance(
  181. struct crypto_ahash *ahash)
  182. {
  183. return ahash_instance(crypto_tfm_alg_instance(&ahash->base));
  184. }
  185. static inline void *ahash_instance_ctx(struct ahash_instance *inst)
  186. {
  187. return crypto_instance_ctx(ahash_crypto_instance(inst));
  188. }
  189. static inline void *ahash_request_ctx_dma(struct ahash_request *req)
  190. {
  191. unsigned int align = crypto_dma_align();
  192. if (align <= crypto_tfm_ctx_alignment())
  193. align = 1;
  194. return PTR_ALIGN(ahash_request_ctx(req), align);
  195. }
  196. static inline void ahash_request_complete(struct ahash_request *req, int err)
  197. {
  198. crypto_request_complete(&req->base, err);
  199. }
  200. static inline u32 ahash_request_flags(struct ahash_request *req)
  201. {
  202. return crypto_request_flags(&req->base) & ~CRYPTO_AHASH_REQ_PRIVATE;
  203. }
  204. static inline struct crypto_ahash *crypto_spawn_ahash(
  205. struct crypto_ahash_spawn *spawn)
  206. {
  207. return crypto_spawn_tfm2(&spawn->base);
  208. }
  209. static inline int ahash_enqueue_request(struct crypto_queue *queue,
  210. struct ahash_request *request)
  211. {
  212. return crypto_enqueue_request(queue, &request->base);
  213. }
  214. static inline struct ahash_request *ahash_dequeue_request(
  215. struct crypto_queue *queue)
  216. {
  217. return ahash_request_cast(crypto_dequeue_request(queue));
  218. }
  219. static inline void *crypto_shash_ctx(struct crypto_shash *tfm)
  220. {
  221. return crypto_tfm_ctx(&tfm->base);
  222. }
  223. static inline struct crypto_instance *shash_crypto_instance(
  224. struct shash_instance *inst)
  225. {
  226. return &inst->s.base;
  227. }
  228. static inline struct shash_instance *shash_instance(
  229. struct crypto_instance *inst)
  230. {
  231. return container_of(inst, struct shash_instance, s.base);
  232. }
  233. static inline struct shash_instance *shash_alg_instance(
  234. struct crypto_shash *shash)
  235. {
  236. return shash_instance(crypto_tfm_alg_instance(&shash->base));
  237. }
  238. static inline void *shash_instance_ctx(struct shash_instance *inst)
  239. {
  240. return crypto_instance_ctx(shash_crypto_instance(inst));
  241. }
  242. static inline struct crypto_shash *crypto_spawn_shash(
  243. struct crypto_shash_spawn *spawn)
  244. {
  245. return crypto_spawn_tfm2(&spawn->base);
  246. }
  247. static inline struct crypto_shash *__crypto_shash_cast(struct crypto_tfm *tfm)
  248. {
  249. return container_of(tfm, struct crypto_shash, base);
  250. }
  251. static inline bool ahash_request_isvirt(struct ahash_request *req)
  252. {
  253. return req->base.flags & CRYPTO_AHASH_REQ_VIRT;
  254. }
  255. static inline bool crypto_ahash_req_virt(struct crypto_ahash *tfm)
  256. {
  257. return crypto_tfm_req_virt(&tfm->base);
  258. }
  259. static inline struct crypto_ahash *crypto_ahash_fb(struct crypto_ahash *tfm)
  260. {
  261. return __crypto_ahash_cast(crypto_ahash_tfm(tfm)->fb);
  262. }
  263. static inline struct ahash_request *ahash_fbreq_on_stack_init(
  264. char *buf, struct ahash_request *old)
  265. {
  266. struct crypto_ahash *tfm = crypto_ahash_reqtfm(old);
  267. struct ahash_request *req = (void *)buf;
  268. crypto_stack_request_init(&req->base,
  269. crypto_ahash_tfm(crypto_ahash_fb(tfm)));
  270. ahash_request_set_callback(req, ahash_request_flags(old), NULL, NULL);
  271. req->base.flags &= ~CRYPTO_AHASH_REQ_PRIVATE;
  272. req->base.flags |= old->base.flags & CRYPTO_AHASH_REQ_PRIVATE;
  273. req->src = old->src;
  274. req->result = old->result;
  275. req->nbytes = old->nbytes;
  276. return req;
  277. }
  278. /* Return the state size without partial block for block-only algorithms. */
  279. static inline unsigned int crypto_shash_coresize(struct crypto_shash *tfm)
  280. {
  281. return crypto_shash_statesize(tfm) - crypto_shash_blocksize(tfm) - 1;
  282. }
  283. /* This can only be used if the request was never cloned. */
  284. #define HASH_REQUEST_ZERO(name) \
  285. memzero_explicit(__##name##_req, sizeof(__##name##_req))
  286. /**
  287. * crypto_ahash_export_core() - extract core state for message digest
  288. * @req: reference to the ahash_request handle whose state is exported
  289. * @out: output buffer of sufficient size that can hold the hash state
  290. *
  291. * Export the hash state without the partial block buffer.
  292. *
  293. * Context: Softirq or process context.
  294. * Return: 0 if the export creation was successful; < 0 if an error occurred
  295. */
  296. int crypto_ahash_export_core(struct ahash_request *req, void *out);
  297. /**
  298. * crypto_ahash_import_core() - import core state
  299. * @req: reference to ahash_request handle the state is imported into
  300. * @in: buffer holding the state
  301. *
  302. * Import the hash state without the partial block buffer.
  303. *
  304. * Context: Softirq or process context.
  305. * Return: 0 if the import was successful; < 0 if an error occurred
  306. */
  307. int crypto_ahash_import_core(struct ahash_request *req, const void *in);
  308. /**
  309. * crypto_shash_export_core() - extract core state for message digest
  310. * @desc: reference to the operational state handle whose state is exported
  311. * @out: output buffer of sufficient size that can hold the hash state
  312. *
  313. * Export the hash state without the partial block buffer.
  314. *
  315. * Context: Softirq or process context.
  316. * Return: 0 if the export creation was successful; < 0 if an error occurred
  317. */
  318. int crypto_shash_export_core(struct shash_desc *desc, void *out);
  319. /**
  320. * crypto_shash_import_core() - import core state
  321. * @desc: reference to the operational state handle the state imported into
  322. * @in: buffer holding the state
  323. *
  324. * Import the hash state without the partial block buffer.
  325. *
  326. * Context: Softirq or process context.
  327. * Return: 0 if the import was successful; < 0 if an error occurred
  328. */
  329. int crypto_shash_import_core(struct shash_desc *desc, const void *in);
  330. #endif /* _CRYPTO_INTERNAL_HASH_H */