aead.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * AEAD: Authenticated Encryption with Associated Data
  4. *
  5. * Copyright (c) 2007-2015 Herbert Xu <herbert@gondor.apana.org.au>
  6. */
  7. #ifndef _CRYPTO_INTERNAL_AEAD_H
  8. #define _CRYPTO_INTERNAL_AEAD_H
  9. #include <crypto/aead.h>
  10. #include <crypto/algapi.h>
  11. #include <linux/stddef.h>
  12. #include <linux/types.h>
  13. struct rtattr;
  14. struct aead_instance {
  15. void (*free)(struct aead_instance *inst);
  16. union {
  17. struct {
  18. char head[offsetof(struct aead_alg, base)];
  19. struct crypto_instance base;
  20. } s;
  21. struct aead_alg alg;
  22. };
  23. };
  24. struct crypto_aead_spawn {
  25. struct crypto_spawn base;
  26. };
  27. struct aead_queue {
  28. struct crypto_queue base;
  29. };
  30. static inline void *crypto_aead_ctx(struct crypto_aead *tfm)
  31. {
  32. return crypto_tfm_ctx(&tfm->base);
  33. }
  34. static inline void *crypto_aead_ctx_dma(struct crypto_aead *tfm)
  35. {
  36. return crypto_tfm_ctx_dma(&tfm->base);
  37. }
  38. static inline struct crypto_instance *aead_crypto_instance(
  39. struct aead_instance *inst)
  40. {
  41. return container_of(&inst->alg.base, struct crypto_instance, alg);
  42. }
  43. static inline struct aead_instance *aead_instance(struct crypto_instance *inst)
  44. {
  45. return container_of(&inst->alg, struct aead_instance, alg.base);
  46. }
  47. static inline struct aead_instance *aead_alg_instance(struct crypto_aead *aead)
  48. {
  49. return aead_instance(crypto_tfm_alg_instance(&aead->base));
  50. }
  51. static inline void *aead_instance_ctx(struct aead_instance *inst)
  52. {
  53. return crypto_instance_ctx(aead_crypto_instance(inst));
  54. }
  55. static inline void *aead_request_ctx(struct aead_request *req)
  56. {
  57. return req->__ctx;
  58. }
  59. static inline void *aead_request_ctx_dma(struct aead_request *req)
  60. {
  61. unsigned int align = crypto_dma_align();
  62. if (align <= crypto_tfm_ctx_alignment())
  63. align = 1;
  64. return PTR_ALIGN(aead_request_ctx(req), align);
  65. }
  66. static inline void aead_request_complete(struct aead_request *req, int err)
  67. {
  68. crypto_request_complete(&req->base, err);
  69. }
  70. static inline u32 aead_request_flags(struct aead_request *req)
  71. {
  72. return req->base.flags;
  73. }
  74. static inline struct aead_request *aead_request_cast(
  75. struct crypto_async_request *req)
  76. {
  77. return container_of(req, struct aead_request, base);
  78. }
  79. int crypto_grab_aead(struct crypto_aead_spawn *spawn,
  80. struct crypto_instance *inst,
  81. const char *name, u32 type, u32 mask);
  82. static inline void crypto_drop_aead(struct crypto_aead_spawn *spawn)
  83. {
  84. crypto_drop_spawn(&spawn->base);
  85. }
  86. static inline struct aead_alg *crypto_spawn_aead_alg(
  87. struct crypto_aead_spawn *spawn)
  88. {
  89. return container_of(spawn->base.alg, struct aead_alg, base);
  90. }
  91. static inline struct crypto_aead *crypto_spawn_aead(
  92. struct crypto_aead_spawn *spawn)
  93. {
  94. return crypto_spawn_tfm2(&spawn->base);
  95. }
  96. static inline void crypto_aead_set_reqsize(struct crypto_aead *aead,
  97. unsigned int reqsize)
  98. {
  99. aead->reqsize = reqsize;
  100. }
  101. static inline void crypto_aead_set_reqsize_dma(struct crypto_aead *aead,
  102. unsigned int reqsize)
  103. {
  104. reqsize += crypto_dma_align() & ~(crypto_tfm_ctx_alignment() - 1);
  105. aead->reqsize = reqsize;
  106. }
  107. static inline void aead_init_queue(struct aead_queue *queue,
  108. unsigned int max_qlen)
  109. {
  110. crypto_init_queue(&queue->base, max_qlen);
  111. }
  112. static inline unsigned int crypto_aead_alg_chunksize(struct aead_alg *alg)
  113. {
  114. return alg->chunksize;
  115. }
  116. /**
  117. * crypto_aead_chunksize() - obtain chunk size
  118. * @tfm: cipher handle
  119. *
  120. * The block size is set to one for ciphers such as CCM. However,
  121. * you still need to provide incremental updates in multiples of
  122. * the underlying block size as the IV does not have sub-block
  123. * granularity. This is known in this API as the chunk size.
  124. *
  125. * Return: chunk size in bytes
  126. */
  127. static inline unsigned int crypto_aead_chunksize(struct crypto_aead *tfm)
  128. {
  129. return crypto_aead_alg_chunksize(crypto_aead_alg(tfm));
  130. }
  131. int crypto_register_aead(struct aead_alg *alg);
  132. void crypto_unregister_aead(struct aead_alg *alg);
  133. int crypto_register_aeads(struct aead_alg *algs, int count);
  134. void crypto_unregister_aeads(struct aead_alg *algs, int count);
  135. int aead_register_instance(struct crypto_template *tmpl,
  136. struct aead_instance *inst);
  137. #endif /* _CRYPTO_INTERNAL_AEAD_H */