algapi.h 4.2 KB

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