algapi.h 3.5 KB

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