Herbert Xu | cce9e06 | 2006-08-21 21:08:13 +1000 | [diff] [blame] | 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 | |
| 15 | #include <linux/crypto.h> |
| 16 | |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 17 | struct module; |
Herbert Xu | e853c3c | 2006-08-22 00:06:54 +1000 | [diff] [blame] | 18 | struct seq_file; |
| 19 | |
| 20 | struct crypto_type { |
Herbert Xu | 27d2a33 | 2007-01-24 20:50:26 +1100 | [diff] [blame^] | 21 | unsigned int (*ctxsize)(struct crypto_alg *alg, u32 type, u32 mask); |
| 22 | int (*init)(struct crypto_tfm *tfm, u32 type, u32 mask); |
Herbert Xu | e853c3c | 2006-08-22 00:06:54 +1000 | [diff] [blame] | 23 | void (*exit)(struct crypto_tfm *tfm); |
| 24 | void (*show)(struct seq_file *m, struct crypto_alg *alg); |
| 25 | }; |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 26 | |
| 27 | struct crypto_instance { |
| 28 | struct crypto_alg alg; |
| 29 | |
| 30 | struct crypto_template *tmpl; |
| 31 | struct hlist_node list; |
| 32 | |
| 33 | void *__ctx[] CRYPTO_MINALIGN_ATTR; |
| 34 | }; |
| 35 | |
| 36 | struct crypto_template { |
| 37 | struct list_head list; |
| 38 | struct hlist_head instances; |
| 39 | struct module *module; |
| 40 | |
| 41 | struct crypto_instance *(*alloc)(void *param, unsigned int len); |
| 42 | void (*free)(struct crypto_instance *inst); |
| 43 | |
| 44 | char name[CRYPTO_MAX_ALG_NAME]; |
| 45 | }; |
| 46 | |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 47 | struct crypto_spawn { |
| 48 | struct list_head list; |
| 49 | struct crypto_alg *alg; |
| 50 | struct crypto_instance *inst; |
| 51 | }; |
| 52 | |
Herbert Xu | 5c64097 | 2006-08-12 21:56:17 +1000 | [diff] [blame] | 53 | struct scatter_walk { |
| 54 | struct scatterlist *sg; |
| 55 | unsigned int offset; |
| 56 | }; |
| 57 | |
Herbert Xu | 5cde0af | 2006-08-22 00:07:53 +1000 | [diff] [blame] | 58 | struct blkcipher_walk { |
| 59 | union { |
| 60 | struct { |
| 61 | struct page *page; |
| 62 | unsigned long offset; |
| 63 | } phys; |
| 64 | |
| 65 | struct { |
| 66 | u8 *page; |
| 67 | u8 *addr; |
| 68 | } virt; |
| 69 | } src, dst; |
| 70 | |
| 71 | struct scatter_walk in; |
| 72 | unsigned int nbytes; |
| 73 | |
| 74 | struct scatter_walk out; |
| 75 | unsigned int total; |
| 76 | |
| 77 | void *page; |
| 78 | u8 *buffer; |
| 79 | u8 *iv; |
| 80 | |
| 81 | int flags; |
| 82 | }; |
| 83 | |
| 84 | extern const struct crypto_type crypto_blkcipher_type; |
Herbert Xu | 055bcee | 2006-08-19 22:24:23 +1000 | [diff] [blame] | 85 | extern const struct crypto_type crypto_hash_type; |
Herbert Xu | 5cde0af | 2006-08-22 00:07:53 +1000 | [diff] [blame] | 86 | |
Herbert Xu | db131ef | 2006-09-21 11:44:08 +1000 | [diff] [blame] | 87 | void crypto_mod_put(struct crypto_alg *alg); |
| 88 | |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 89 | int crypto_register_template(struct crypto_template *tmpl); |
| 90 | void crypto_unregister_template(struct crypto_template *tmpl); |
| 91 | struct crypto_template *crypto_lookup_template(const char *name); |
| 92 | |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 93 | int crypto_init_spawn(struct crypto_spawn *spawn, struct crypto_alg *alg, |
| 94 | struct crypto_instance *inst); |
| 95 | void crypto_drop_spawn(struct crypto_spawn *spawn); |
Herbert Xu | 2e306ee | 2006-12-17 10:05:58 +1100 | [diff] [blame] | 96 | struct crypto_tfm *crypto_spawn_tfm(struct crypto_spawn *spawn, u32 type, |
| 97 | u32 mask); |
Herbert Xu | 6bfd480 | 2006-09-21 11:39:29 +1000 | [diff] [blame] | 98 | |
Herbert Xu | 7fed0bf | 2006-08-06 23:10:45 +1000 | [diff] [blame] | 99 | struct crypto_alg *crypto_get_attr_alg(void *param, unsigned int len, |
| 100 | u32 type, u32 mask); |
| 101 | struct crypto_instance *crypto_alloc_instance(const char *name, |
| 102 | struct crypto_alg *alg); |
| 103 | |
Herbert Xu | 5cde0af | 2006-08-22 00:07:53 +1000 | [diff] [blame] | 104 | int blkcipher_walk_done(struct blkcipher_desc *desc, |
| 105 | struct blkcipher_walk *walk, int err); |
| 106 | int blkcipher_walk_virt(struct blkcipher_desc *desc, |
| 107 | struct blkcipher_walk *walk); |
| 108 | int blkcipher_walk_phys(struct blkcipher_desc *desc, |
| 109 | struct blkcipher_walk *walk); |
| 110 | |
| 111 | static inline void *crypto_tfm_ctx_aligned(struct crypto_tfm *tfm) |
| 112 | { |
| 113 | unsigned long addr = (unsigned long)crypto_tfm_ctx(tfm); |
| 114 | unsigned long align = crypto_tfm_alg_alignmask(tfm); |
| 115 | |
| 116 | if (align <= crypto_tfm_ctx_alignment()) |
| 117 | align = 1; |
| 118 | return (void *)ALIGN(addr, align); |
| 119 | } |
| 120 | |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame] | 121 | static inline void *crypto_instance_ctx(struct crypto_instance *inst) |
| 122 | { |
| 123 | return inst->__ctx; |
| 124 | } |
| 125 | |
Herbert Xu | 5cde0af | 2006-08-22 00:07:53 +1000 | [diff] [blame] | 126 | static inline void *crypto_blkcipher_ctx(struct crypto_blkcipher *tfm) |
| 127 | { |
| 128 | return crypto_tfm_ctx(&tfm->base); |
| 129 | } |
| 130 | |
| 131 | static inline void *crypto_blkcipher_ctx_aligned(struct crypto_blkcipher *tfm) |
| 132 | { |
| 133 | return crypto_tfm_ctx_aligned(&tfm->base); |
| 134 | } |
| 135 | |
Herbert Xu | 2e306ee | 2006-12-17 10:05:58 +1100 | [diff] [blame] | 136 | static inline struct crypto_cipher *crypto_spawn_cipher( |
| 137 | struct crypto_spawn *spawn) |
| 138 | { |
| 139 | u32 type = CRYPTO_ALG_TYPE_CIPHER; |
| 140 | u32 mask = CRYPTO_ALG_TYPE_MASK; |
| 141 | |
| 142 | return __crypto_cipher_cast(crypto_spawn_tfm(spawn, type, mask)); |
| 143 | } |
| 144 | |
Herbert Xu | f28776a | 2006-08-13 20:58:18 +1000 | [diff] [blame] | 145 | static inline struct cipher_alg *crypto_cipher_alg(struct crypto_cipher *tfm) |
| 146 | { |
| 147 | return &crypto_cipher_tfm(tfm)->__crt_alg->cra_cipher; |
| 148 | } |
| 149 | |
Herbert Xu | 2e306ee | 2006-12-17 10:05:58 +1100 | [diff] [blame] | 150 | static inline struct crypto_hash *crypto_spawn_hash(struct crypto_spawn *spawn) |
| 151 | { |
| 152 | u32 type = CRYPTO_ALG_TYPE_HASH; |
| 153 | u32 mask = CRYPTO_ALG_TYPE_HASH_MASK; |
| 154 | |
| 155 | return __crypto_hash_cast(crypto_spawn_tfm(spawn, type, mask)); |
| 156 | } |
| 157 | |
Herbert Xu | 055bcee | 2006-08-19 22:24:23 +1000 | [diff] [blame] | 158 | static inline void *crypto_hash_ctx_aligned(struct crypto_hash *tfm) |
| 159 | { |
| 160 | return crypto_tfm_ctx_aligned(&tfm->base); |
| 161 | } |
| 162 | |
Herbert Xu | 5cde0af | 2006-08-22 00:07:53 +1000 | [diff] [blame] | 163 | static inline void blkcipher_walk_init(struct blkcipher_walk *walk, |
| 164 | struct scatterlist *dst, |
| 165 | struct scatterlist *src, |
| 166 | unsigned int nbytes) |
| 167 | { |
| 168 | walk->in.sg = src; |
| 169 | walk->out.sg = dst; |
| 170 | walk->total = nbytes; |
| 171 | } |
| 172 | |
Herbert Xu | cce9e06 | 2006-08-21 21:08:13 +1000 | [diff] [blame] | 173 | #endif /* _CRYPTO_ALGAPI_H */ |
| 174 | |