aead.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * AEAD: Authenticated Encryption with Associated Data
  3. *
  4. * Copyright (c) 2007 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. extern const struct crypto_type crypto_aead_type;
  36. extern const struct crypto_type crypto_nivaead_type;
  37. static inline void *crypto_aead_ctx(struct crypto_aead *tfm)
  38. {
  39. return crypto_tfm_ctx(&tfm->base);
  40. }
  41. static inline struct crypto_instance *crypto_aead_alg_instance(
  42. struct crypto_aead *aead)
  43. {
  44. return crypto_tfm_alg_instance(&aead->base);
  45. }
  46. static inline struct crypto_instance *aead_crypto_instance(
  47. struct aead_instance *inst)
  48. {
  49. return container_of(&inst->alg.base, struct crypto_instance, alg);
  50. }
  51. static inline struct aead_instance *aead_instance(struct crypto_instance *inst)
  52. {
  53. return container_of(&inst->alg, struct aead_instance, alg.base);
  54. }
  55. static inline struct aead_instance *aead_alg_instance(struct crypto_aead *aead)
  56. {
  57. return aead_instance(crypto_aead_alg_instance(aead));
  58. }
  59. static inline void *aead_instance_ctx(struct aead_instance *inst)
  60. {
  61. return crypto_instance_ctx(aead_crypto_instance(inst));
  62. }
  63. static inline void *aead_request_ctx(struct aead_request *req)
  64. {
  65. return req->__ctx;
  66. }
  67. static inline void aead_request_complete(struct aead_request *req, int err)
  68. {
  69. req->base.complete(&req->base, err);
  70. }
  71. static inline u32 aead_request_flags(struct aead_request *req)
  72. {
  73. return req->base.flags;
  74. }
  75. static inline void crypto_set_aead_spawn(
  76. struct crypto_aead_spawn *spawn, struct crypto_instance *inst)
  77. {
  78. crypto_set_spawn(&spawn->base, inst);
  79. }
  80. struct crypto_alg *crypto_lookup_aead(const char *name, u32 type, u32 mask);
  81. int crypto_grab_aead(struct crypto_aead_spawn *spawn, const char *name,
  82. u32 type, u32 mask);
  83. static inline void crypto_drop_aead(struct crypto_aead_spawn *spawn)
  84. {
  85. crypto_drop_spawn(&spawn->base);
  86. }
  87. static inline struct crypto_alg *crypto_aead_spawn_alg(
  88. struct crypto_aead_spawn *spawn)
  89. {
  90. return spawn->base.alg;
  91. }
  92. static inline struct aead_alg *crypto_spawn_aead_alg(
  93. struct crypto_aead_spawn *spawn)
  94. {
  95. return container_of(spawn->base.alg, struct aead_alg, base);
  96. }
  97. static inline struct crypto_aead *crypto_spawn_aead(
  98. struct crypto_aead_spawn *spawn)
  99. {
  100. return crypto_spawn_tfm2(&spawn->base);
  101. }
  102. static inline struct crypto_aead *aead_geniv_base(struct crypto_aead *geniv)
  103. {
  104. return geniv->child;
  105. }
  106. static inline void *aead_givcrypt_reqctx(struct aead_givcrypt_request *req)
  107. {
  108. return aead_request_ctx(&req->areq);
  109. }
  110. static inline void aead_givcrypt_complete(struct aead_givcrypt_request *req,
  111. int err)
  112. {
  113. aead_request_complete(&req->areq, err);
  114. }
  115. static inline void crypto_aead_set_reqsize(struct crypto_aead *aead,
  116. unsigned int reqsize)
  117. {
  118. crypto_aead_crt(aead)->reqsize = reqsize;
  119. }
  120. static inline unsigned int crypto_aead_alg_maxauthsize(struct aead_alg *alg)
  121. {
  122. return alg->base.cra_aead.encrypt ? alg->base.cra_aead.maxauthsize :
  123. alg->maxauthsize;
  124. }
  125. static inline unsigned int crypto_aead_maxauthsize(struct crypto_aead *aead)
  126. {
  127. return crypto_aead_alg_maxauthsize(crypto_aead_alg(aead));
  128. }
  129. static inline void aead_init_queue(struct aead_queue *queue,
  130. unsigned int max_qlen)
  131. {
  132. crypto_init_queue(&queue->base, max_qlen);
  133. }
  134. static inline int aead_enqueue_request(struct aead_queue *queue,
  135. struct aead_request *request)
  136. {
  137. return crypto_enqueue_request(&queue->base, &request->base);
  138. }
  139. static inline struct aead_request *aead_dequeue_request(
  140. struct aead_queue *queue)
  141. {
  142. struct crypto_async_request *req;
  143. req = crypto_dequeue_request(&queue->base);
  144. return req ? container_of(req, struct aead_request, base) : NULL;
  145. }
  146. static inline struct aead_request *aead_get_backlog(struct aead_queue *queue)
  147. {
  148. struct crypto_async_request *req;
  149. req = crypto_get_backlog(&queue->base);
  150. return req ? container_of(req, struct aead_request, base) : NULL;
  151. }
  152. int crypto_register_aead(struct aead_alg *alg);
  153. void crypto_unregister_aead(struct aead_alg *alg);
  154. int crypto_register_aeads(struct aead_alg *algs, int count);
  155. void crypto_unregister_aeads(struct aead_alg *algs, int count);
  156. int aead_register_instance(struct crypto_template *tmpl,
  157. struct aead_instance *inst);
  158. #endif /* _CRYPTO_INTERNAL_AEAD_H */