aead.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * AEAD: Authenticated Encryption with Associated Data
  3. *
  4. * Copyright (c) 2007-2015 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_AEAD_H
  13. #define _CRYPTO_INTERNAL_AEAD_H
  14. #include <crypto/aead.h>
  15. #include <crypto/algapi.h>
  16. #include <linux/stddef.h>
  17. #include <linux/types.h>
  18. struct rtattr;
  19. struct aead_instance {
  20. void (*free)(struct aead_instance *inst);
  21. union {
  22. struct {
  23. char head[offsetof(struct aead_alg, base)];
  24. struct crypto_instance base;
  25. } s;
  26. struct aead_alg alg;
  27. };
  28. };
  29. struct crypto_aead_spawn {
  30. struct crypto_spawn base;
  31. };
  32. struct aead_queue {
  33. struct crypto_queue base;
  34. };
  35. static inline void *crypto_aead_ctx(struct crypto_aead *tfm)
  36. {
  37. return crypto_tfm_ctx(&tfm->base);
  38. }
  39. static inline struct crypto_instance *aead_crypto_instance(
  40. struct aead_instance *inst)
  41. {
  42. return container_of(&inst->alg.base, struct crypto_instance, alg);
  43. }
  44. static inline struct aead_instance *aead_instance(struct crypto_instance *inst)
  45. {
  46. return container_of(&inst->alg, struct aead_instance, alg.base);
  47. }
  48. static inline struct aead_instance *aead_alg_instance(struct crypto_aead *aead)
  49. {
  50. return aead_instance(crypto_tfm_alg_instance(&aead->base));
  51. }
  52. static inline void *aead_instance_ctx(struct aead_instance *inst)
  53. {
  54. return crypto_instance_ctx(aead_crypto_instance(inst));
  55. }
  56. static inline void *aead_request_ctx(struct aead_request *req)
  57. {
  58. return req->__ctx;
  59. }
  60. static inline void aead_request_complete(struct aead_request *req, int err)
  61. {
  62. req->base.complete(&req->base, err);
  63. }
  64. static inline u32 aead_request_flags(struct aead_request *req)
  65. {
  66. return req->base.flags;
  67. }
  68. static inline struct aead_request *aead_request_cast(
  69. struct crypto_async_request *req)
  70. {
  71. return container_of(req, struct aead_request, base);
  72. }
  73. static inline void crypto_set_aead_spawn(
  74. struct crypto_aead_spawn *spawn, struct crypto_instance *inst)
  75. {
  76. crypto_set_spawn(&spawn->base, inst);
  77. }
  78. int crypto_grab_aead(struct crypto_aead_spawn *spawn, const char *name,
  79. u32 type, u32 mask);
  80. static inline void crypto_drop_aead(struct crypto_aead_spawn *spawn)
  81. {
  82. crypto_drop_spawn(&spawn->base);
  83. }
  84. static inline struct aead_alg *crypto_spawn_aead_alg(
  85. struct crypto_aead_spawn *spawn)
  86. {
  87. return container_of(spawn->base.alg, struct aead_alg, base);
  88. }
  89. static inline struct crypto_aead *crypto_spawn_aead(
  90. struct crypto_aead_spawn *spawn)
  91. {
  92. return crypto_spawn_tfm2(&spawn->base);
  93. }
  94. static inline void crypto_aead_set_reqsize(struct crypto_aead *aead,
  95. unsigned int reqsize)
  96. {
  97. aead->reqsize = reqsize;
  98. }
  99. static inline unsigned int crypto_aead_alg_maxauthsize(struct aead_alg *alg)
  100. {
  101. return alg->maxauthsize;
  102. }
  103. static inline unsigned int crypto_aead_maxauthsize(struct crypto_aead *aead)
  104. {
  105. return crypto_aead_alg_maxauthsize(crypto_aead_alg(aead));
  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 int aead_enqueue_request(struct aead_queue *queue,
  113. struct aead_request *request)
  114. {
  115. return crypto_enqueue_request(&queue->base, &request->base);
  116. }
  117. static inline struct aead_request *aead_dequeue_request(
  118. struct aead_queue *queue)
  119. {
  120. struct crypto_async_request *req;
  121. req = crypto_dequeue_request(&queue->base);
  122. return req ? container_of(req, struct aead_request, base) : NULL;
  123. }
  124. static inline struct aead_request *aead_get_backlog(struct aead_queue *queue)
  125. {
  126. struct crypto_async_request *req;
  127. req = crypto_get_backlog(&queue->base);
  128. return req ? container_of(req, struct aead_request, base) : NULL;
  129. }
  130. static inline unsigned int crypto_aead_alg_chunksize(struct aead_alg *alg)
  131. {
  132. return alg->chunksize;
  133. }
  134. /**
  135. * crypto_aead_chunksize() - obtain chunk size
  136. * @tfm: cipher handle
  137. *
  138. * The block size is set to one for ciphers such as CCM. However,
  139. * you still need to provide incremental updates in multiples of
  140. * the underlying block size as the IV does not have sub-block
  141. * granularity. This is known in this API as the chunk size.
  142. *
  143. * Return: chunk size in bytes
  144. */
  145. static inline unsigned int crypto_aead_chunksize(struct crypto_aead *tfm)
  146. {
  147. return crypto_aead_alg_chunksize(crypto_aead_alg(tfm));
  148. }
  149. int crypto_register_aead(struct aead_alg *alg);
  150. void crypto_unregister_aead(struct aead_alg *alg);
  151. int crypto_register_aeads(struct aead_alg *algs, int count);
  152. void crypto_unregister_aeads(struct aead_alg *algs, int count);
  153. int aead_register_instance(struct crypto_template *tmpl,
  154. struct aead_instance *inst);
  155. #endif /* _CRYPTO_INTERNAL_AEAD_H */