akcipher.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * Public Key Encryption
  4. *
  5. * Copyright (c) 2015, Intel Corporation
  6. * Authors: Tadeusz Struk <tadeusz.struk@intel.com>
  7. */
  8. #ifndef _CRYPTO_AKCIPHER_INT_H
  9. #define _CRYPTO_AKCIPHER_INT_H
  10. #include <crypto/akcipher.h>
  11. #include <crypto/algapi.h>
  12. struct akcipher_instance {
  13. void (*free)(struct akcipher_instance *inst);
  14. union {
  15. struct {
  16. char head[offsetof(struct akcipher_alg, base)];
  17. struct crypto_instance base;
  18. } s;
  19. struct akcipher_alg alg;
  20. };
  21. };
  22. struct crypto_akcipher_spawn {
  23. struct crypto_spawn base;
  24. };
  25. /*
  26. * Transform internal helpers.
  27. */
  28. static inline void *akcipher_request_ctx(struct akcipher_request *req)
  29. {
  30. return req->__ctx;
  31. }
  32. static inline void *akcipher_request_ctx_dma(struct akcipher_request *req)
  33. {
  34. unsigned int align = crypto_dma_align();
  35. if (align <= crypto_tfm_ctx_alignment())
  36. align = 1;
  37. return PTR_ALIGN(akcipher_request_ctx(req), align);
  38. }
  39. static inline void akcipher_set_reqsize(struct crypto_akcipher *akcipher,
  40. unsigned int reqsize)
  41. {
  42. akcipher->reqsize = reqsize;
  43. }
  44. static inline void akcipher_set_reqsize_dma(struct crypto_akcipher *akcipher,
  45. unsigned int reqsize)
  46. {
  47. reqsize += crypto_dma_align() & ~(crypto_tfm_ctx_alignment() - 1);
  48. akcipher->reqsize = reqsize;
  49. }
  50. static inline void *akcipher_tfm_ctx(struct crypto_akcipher *tfm)
  51. {
  52. return crypto_tfm_ctx(&tfm->base);
  53. }
  54. static inline void *akcipher_tfm_ctx_dma(struct crypto_akcipher *tfm)
  55. {
  56. return crypto_tfm_ctx_dma(&tfm->base);
  57. }
  58. static inline void akcipher_request_complete(struct akcipher_request *req,
  59. int err)
  60. {
  61. crypto_request_complete(&req->base, err);
  62. }
  63. static inline const char *akcipher_alg_name(struct crypto_akcipher *tfm)
  64. {
  65. return crypto_akcipher_tfm(tfm)->__crt_alg->cra_name;
  66. }
  67. static inline struct crypto_instance *akcipher_crypto_instance(
  68. struct akcipher_instance *inst)
  69. {
  70. return container_of(&inst->alg.base, struct crypto_instance, alg);
  71. }
  72. static inline struct akcipher_instance *akcipher_instance(
  73. struct crypto_instance *inst)
  74. {
  75. return container_of(&inst->alg, struct akcipher_instance, alg.base);
  76. }
  77. static inline struct akcipher_instance *akcipher_alg_instance(
  78. struct crypto_akcipher *akcipher)
  79. {
  80. return akcipher_instance(crypto_tfm_alg_instance(&akcipher->base));
  81. }
  82. static inline void *akcipher_instance_ctx(struct akcipher_instance *inst)
  83. {
  84. return crypto_instance_ctx(akcipher_crypto_instance(inst));
  85. }
  86. int crypto_grab_akcipher(struct crypto_akcipher_spawn *spawn,
  87. struct crypto_instance *inst,
  88. const char *name, u32 type, u32 mask);
  89. static inline struct crypto_akcipher *crypto_spawn_akcipher(
  90. struct crypto_akcipher_spawn *spawn)
  91. {
  92. return crypto_spawn_tfm2(&spawn->base);
  93. }
  94. static inline void crypto_drop_akcipher(struct crypto_akcipher_spawn *spawn)
  95. {
  96. crypto_drop_spawn(&spawn->base);
  97. }
  98. static inline struct akcipher_alg *crypto_spawn_akcipher_alg(
  99. struct crypto_akcipher_spawn *spawn)
  100. {
  101. return container_of(spawn->base.alg, struct akcipher_alg, base);
  102. }
  103. /**
  104. * crypto_register_akcipher() -- Register public key algorithm
  105. *
  106. * Function registers an implementation of a public key cipher algorithm
  107. *
  108. * @alg: algorithm definition
  109. *
  110. * Return: zero on success; error code in case of error
  111. */
  112. int crypto_register_akcipher(struct akcipher_alg *alg);
  113. /**
  114. * crypto_unregister_akcipher() -- Unregister public key algorithm
  115. *
  116. * Function unregisters an implementation of a public key cipher algorithm
  117. *
  118. * @alg: algorithm definition
  119. */
  120. void crypto_unregister_akcipher(struct akcipher_alg *alg);
  121. /**
  122. * akcipher_register_instance() -- Unregister public key template instance
  123. *
  124. * Function registers an implementation of an asymmetric key algorithm
  125. * created from a template
  126. *
  127. * @tmpl: the template from which the algorithm was created
  128. * @inst: the template instance
  129. */
  130. int akcipher_register_instance(struct crypto_template *tmpl,
  131. struct akcipher_instance *inst);
  132. #endif