algapi.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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);
  19. int (*init)(struct crypto_tfm *tfm);
  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);
  76. struct crypto_alg *crypto_get_attr_alg(void *param, unsigned int len,
  77. u32 type, u32 mask);
  78. struct crypto_instance *crypto_alloc_instance(const char *name,
  79. struct crypto_alg *alg);
  80. int blkcipher_walk_done(struct blkcipher_desc *desc,
  81. struct blkcipher_walk *walk, int err);
  82. int blkcipher_walk_virt(struct blkcipher_desc *desc,
  83. struct blkcipher_walk *walk);
  84. int blkcipher_walk_phys(struct blkcipher_desc *desc,
  85. struct blkcipher_walk *walk);
  86. static inline void *crypto_tfm_ctx_aligned(struct crypto_tfm *tfm)
  87. {
  88. unsigned long addr = (unsigned long)crypto_tfm_ctx(tfm);
  89. unsigned long align = crypto_tfm_alg_alignmask(tfm);
  90. if (align <= crypto_tfm_ctx_alignment())
  91. align = 1;
  92. return (void *)ALIGN(addr, align);
  93. }
  94. static inline void *crypto_instance_ctx(struct crypto_instance *inst)
  95. {
  96. return inst->__ctx;
  97. }
  98. static inline void *crypto_blkcipher_ctx(struct crypto_blkcipher *tfm)
  99. {
  100. return crypto_tfm_ctx(&tfm->base);
  101. }
  102. static inline void *crypto_blkcipher_ctx_aligned(struct crypto_blkcipher *tfm)
  103. {
  104. return crypto_tfm_ctx_aligned(&tfm->base);
  105. }
  106. static inline struct cipher_alg *crypto_cipher_alg(struct crypto_cipher *tfm)
  107. {
  108. return &crypto_cipher_tfm(tfm)->__crt_alg->cra_cipher;
  109. }
  110. static inline void *crypto_hash_ctx_aligned(struct crypto_hash *tfm)
  111. {
  112. return crypto_tfm_ctx_aligned(&tfm->base);
  113. }
  114. static inline void blkcipher_walk_init(struct blkcipher_walk *walk,
  115. struct scatterlist *dst,
  116. struct scatterlist *src,
  117. unsigned int nbytes)
  118. {
  119. walk->in.sg = src;
  120. walk->out.sg = dst;
  121. walk->total = nbytes;
  122. }
  123. #endif /* _CRYPTO_ALGAPI_H */