algapi.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * Cryptographic API for algorithms (i.e., low-level API).
  3. *
  4. * Copyright (c) 2006 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_ALGAPI_H
  13. #define _CRYPTO_ALGAPI_H
  14. #include <linux/crypto.h>
  15. struct module;
  16. struct rtattr;
  17. struct seq_file;
  18. struct crypto_type {
  19. unsigned int (*ctxsize)(struct crypto_alg *alg, u32 type, u32 mask);
  20. int (*init)(struct crypto_tfm *tfm, u32 type, u32 mask);
  21. void (*exit)(struct crypto_tfm *tfm);
  22. void (*show)(struct seq_file *m, struct crypto_alg *alg);
  23. };
  24. struct crypto_instance {
  25. struct crypto_alg alg;
  26. struct crypto_template *tmpl;
  27. struct hlist_node list;
  28. void *__ctx[] CRYPTO_MINALIGN_ATTR;
  29. };
  30. struct crypto_template {
  31. struct list_head list;
  32. struct hlist_head instances;
  33. struct module *module;
  34. struct crypto_instance *(*alloc)(struct rtattr **tb);
  35. void (*free)(struct crypto_instance *inst);
  36. char name[CRYPTO_MAX_ALG_NAME];
  37. };
  38. struct crypto_spawn {
  39. struct list_head list;
  40. struct crypto_alg *alg;
  41. struct crypto_instance *inst;
  42. };
  43. struct scatter_walk {
  44. struct scatterlist *sg;
  45. unsigned int offset;
  46. };
  47. struct blkcipher_walk {
  48. union {
  49. struct {
  50. struct page *page;
  51. unsigned long offset;
  52. } phys;
  53. struct {
  54. u8 *page;
  55. u8 *addr;
  56. } virt;
  57. } src, dst;
  58. struct scatter_walk in;
  59. unsigned int nbytes;
  60. struct scatter_walk out;
  61. unsigned int total;
  62. void *page;
  63. u8 *buffer;
  64. u8 *iv;
  65. int flags;
  66. };
  67. extern const struct crypto_type crypto_blkcipher_type;
  68. extern const struct crypto_type crypto_hash_type;
  69. void crypto_mod_put(struct crypto_alg *alg);
  70. int crypto_register_template(struct crypto_template *tmpl);
  71. void crypto_unregister_template(struct crypto_template *tmpl);
  72. struct crypto_template *crypto_lookup_template(const char *name);
  73. int crypto_init_spawn(struct crypto_spawn *spawn, struct crypto_alg *alg,
  74. struct crypto_instance *inst);
  75. void crypto_drop_spawn(struct crypto_spawn *spawn);
  76. struct crypto_tfm *crypto_spawn_tfm(struct crypto_spawn *spawn, u32 type,
  77. u32 mask);
  78. struct crypto_attr_type *crypto_get_attr_type(struct rtattr **tb);
  79. int crypto_check_attr_type(struct rtattr **tb, u32 type);
  80. struct crypto_alg *crypto_get_attr_alg(struct rtattr **tb, u32 type, u32 mask);
  81. struct crypto_instance *crypto_alloc_instance(const char *name,
  82. struct crypto_alg *alg);
  83. int blkcipher_walk_done(struct blkcipher_desc *desc,
  84. struct blkcipher_walk *walk, int err);
  85. int blkcipher_walk_virt(struct blkcipher_desc *desc,
  86. struct blkcipher_walk *walk);
  87. int blkcipher_walk_phys(struct blkcipher_desc *desc,
  88. struct blkcipher_walk *walk);
  89. static inline void *crypto_tfm_ctx_aligned(struct crypto_tfm *tfm)
  90. {
  91. unsigned long addr = (unsigned long)crypto_tfm_ctx(tfm);
  92. unsigned long align = crypto_tfm_alg_alignmask(tfm);
  93. if (align <= crypto_tfm_ctx_alignment())
  94. align = 1;
  95. return (void *)ALIGN(addr, align);
  96. }
  97. static inline void *crypto_instance_ctx(struct crypto_instance *inst)
  98. {
  99. return inst->__ctx;
  100. }
  101. static inline void *crypto_blkcipher_ctx(struct crypto_blkcipher *tfm)
  102. {
  103. return crypto_tfm_ctx(&tfm->base);
  104. }
  105. static inline void *crypto_blkcipher_ctx_aligned(struct crypto_blkcipher *tfm)
  106. {
  107. return crypto_tfm_ctx_aligned(&tfm->base);
  108. }
  109. static inline struct crypto_cipher *crypto_spawn_cipher(
  110. struct crypto_spawn *spawn)
  111. {
  112. u32 type = CRYPTO_ALG_TYPE_CIPHER;
  113. u32 mask = CRYPTO_ALG_TYPE_MASK;
  114. return __crypto_cipher_cast(crypto_spawn_tfm(spawn, type, mask));
  115. }
  116. static inline struct cipher_alg *crypto_cipher_alg(struct crypto_cipher *tfm)
  117. {
  118. return &crypto_cipher_tfm(tfm)->__crt_alg->cra_cipher;
  119. }
  120. static inline struct crypto_hash *crypto_spawn_hash(struct crypto_spawn *spawn)
  121. {
  122. u32 type = CRYPTO_ALG_TYPE_HASH;
  123. u32 mask = CRYPTO_ALG_TYPE_HASH_MASK;
  124. return __crypto_hash_cast(crypto_spawn_tfm(spawn, type, mask));
  125. }
  126. static inline void *crypto_hash_ctx_aligned(struct crypto_hash *tfm)
  127. {
  128. return crypto_tfm_ctx_aligned(&tfm->base);
  129. }
  130. static inline void blkcipher_walk_init(struct blkcipher_walk *walk,
  131. struct scatterlist *dst,
  132. struct scatterlist *src,
  133. unsigned int nbytes)
  134. {
  135. walk->in.sg = src;
  136. walk->out.sg = dst;
  137. walk->total = nbytes;
  138. }
  139. #endif /* _CRYPTO_ALGAPI_H */