skcipher.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * Symmetric key ciphers.
  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_SKCIPHER_H
  13. #define _CRYPTO_INTERNAL_SKCIPHER_H
  14. #include <crypto/algapi.h>
  15. #include <crypto/skcipher.h>
  16. #include <linux/types.h>
  17. struct rtattr;
  18. struct skcipher_instance {
  19. void (*free)(struct skcipher_instance *inst);
  20. union {
  21. struct {
  22. char head[offsetof(struct skcipher_alg, base)];
  23. struct crypto_instance base;
  24. } s;
  25. struct skcipher_alg alg;
  26. };
  27. };
  28. struct crypto_skcipher_spawn {
  29. struct crypto_spawn base;
  30. };
  31. extern const struct crypto_type crypto_givcipher_type;
  32. static inline struct crypto_instance *skcipher_crypto_instance(
  33. struct skcipher_instance *inst)
  34. {
  35. return &inst->s.base;
  36. }
  37. static inline struct skcipher_instance *skcipher_alg_instance(
  38. struct crypto_skcipher *skcipher)
  39. {
  40. return container_of(crypto_skcipher_alg(skcipher),
  41. struct skcipher_instance, alg);
  42. }
  43. static inline void *skcipher_instance_ctx(struct skcipher_instance *inst)
  44. {
  45. return crypto_instance_ctx(skcipher_crypto_instance(inst));
  46. }
  47. static inline void skcipher_request_complete(struct skcipher_request *req, int err)
  48. {
  49. req->base.complete(&req->base, err);
  50. }
  51. static inline void crypto_set_skcipher_spawn(
  52. struct crypto_skcipher_spawn *spawn, struct crypto_instance *inst)
  53. {
  54. crypto_set_spawn(&spawn->base, inst);
  55. }
  56. int crypto_grab_skcipher(struct crypto_skcipher_spawn *spawn, const char *name,
  57. u32 type, u32 mask);
  58. static inline void crypto_drop_skcipher(struct crypto_skcipher_spawn *spawn)
  59. {
  60. crypto_drop_spawn(&spawn->base);
  61. }
  62. static inline struct skcipher_alg *crypto_skcipher_spawn_alg(
  63. struct crypto_skcipher_spawn *spawn)
  64. {
  65. return container_of(spawn->base.alg, struct skcipher_alg, base);
  66. }
  67. static inline struct skcipher_alg *crypto_spawn_skcipher_alg(
  68. struct crypto_skcipher_spawn *spawn)
  69. {
  70. return crypto_skcipher_spawn_alg(spawn);
  71. }
  72. static inline struct crypto_skcipher *crypto_spawn_skcipher(
  73. struct crypto_skcipher_spawn *spawn)
  74. {
  75. return crypto_spawn_tfm2(&spawn->base);
  76. }
  77. static inline void crypto_skcipher_set_reqsize(
  78. struct crypto_skcipher *skcipher, unsigned int reqsize)
  79. {
  80. skcipher->reqsize = reqsize;
  81. }
  82. int crypto_register_skcipher(struct skcipher_alg *alg);
  83. void crypto_unregister_skcipher(struct skcipher_alg *alg);
  84. int crypto_register_skciphers(struct skcipher_alg *algs, int count);
  85. void crypto_unregister_skciphers(struct skcipher_alg *algs, int count);
  86. int skcipher_register_instance(struct crypto_template *tmpl,
  87. struct skcipher_instance *inst);
  88. static inline void ablkcipher_request_complete(struct ablkcipher_request *req,
  89. int err)
  90. {
  91. req->base.complete(&req->base, err);
  92. }
  93. static inline u32 ablkcipher_request_flags(struct ablkcipher_request *req)
  94. {
  95. return req->base.flags;
  96. }
  97. static inline void *crypto_skcipher_ctx(struct crypto_skcipher *tfm)
  98. {
  99. return crypto_tfm_ctx(&tfm->base);
  100. }
  101. static inline void *skcipher_request_ctx(struct skcipher_request *req)
  102. {
  103. return req->__ctx;
  104. }
  105. static inline u32 skcipher_request_flags(struct skcipher_request *req)
  106. {
  107. return req->base.flags;
  108. }
  109. static inline unsigned int crypto_skcipher_alg_min_keysize(
  110. struct skcipher_alg *alg)
  111. {
  112. if ((alg->base.cra_flags & CRYPTO_ALG_TYPE_MASK) ==
  113. CRYPTO_ALG_TYPE_BLKCIPHER)
  114. return alg->base.cra_blkcipher.min_keysize;
  115. if (alg->base.cra_ablkcipher.encrypt)
  116. return alg->base.cra_ablkcipher.min_keysize;
  117. return alg->min_keysize;
  118. }
  119. static inline unsigned int crypto_skcipher_alg_max_keysize(
  120. struct skcipher_alg *alg)
  121. {
  122. if ((alg->base.cra_flags & CRYPTO_ALG_TYPE_MASK) ==
  123. CRYPTO_ALG_TYPE_BLKCIPHER)
  124. return alg->base.cra_blkcipher.max_keysize;
  125. if (alg->base.cra_ablkcipher.encrypt)
  126. return alg->base.cra_ablkcipher.max_keysize;
  127. return alg->max_keysize;
  128. }
  129. #endif /* _CRYPTO_INTERNAL_SKCIPHER_H */