blob: d0c190b4d02fb439b1884c3130459af9047ad0ab [file] [log] [blame]
Herbert Xucce9e062006-08-21 21:08:13 +10001/*
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 Xu4cc77202006-08-06 21:16:34 +100017struct module;
Herbert Xuebc610e2007-01-01 18:37:02 +110018struct rtattr;
Herbert Xue853c3c2006-08-22 00:06:54 +100019struct seq_file;
20
21struct crypto_type {
Herbert Xu27d2a332007-01-24 20:50:26 +110022 unsigned int (*ctxsize)(struct crypto_alg *alg, u32 type, u32 mask);
23 int (*init)(struct crypto_tfm *tfm, u32 type, u32 mask);
Herbert Xue853c3c2006-08-22 00:06:54 +100024 void (*exit)(struct crypto_tfm *tfm);
25 void (*show)(struct seq_file *m, struct crypto_alg *alg);
26};
Herbert Xu4cc77202006-08-06 21:16:34 +100027
28struct crypto_instance {
29 struct crypto_alg alg;
30
31 struct crypto_template *tmpl;
32 struct hlist_node list;
33
34 void *__ctx[] CRYPTO_MINALIGN_ATTR;
35};
36
37struct crypto_template {
38 struct list_head list;
39 struct hlist_head instances;
40 struct module *module;
41
Herbert Xuebc610e2007-01-01 18:37:02 +110042 struct crypto_instance *(*alloc)(struct rtattr **tb);
Herbert Xu4cc77202006-08-06 21:16:34 +100043 void (*free)(struct crypto_instance *inst);
44
45 char name[CRYPTO_MAX_ALG_NAME];
46};
47
Herbert Xu6bfd4802006-09-21 11:39:29 +100048struct crypto_spawn {
49 struct list_head list;
50 struct crypto_alg *alg;
51 struct crypto_instance *inst;
52};
53
Herbert Xu5c640972006-08-12 21:56:17 +100054struct scatter_walk {
55 struct scatterlist *sg;
56 unsigned int offset;
57};
58
Herbert Xu5cde0af2006-08-22 00:07:53 +100059struct blkcipher_walk {
60 union {
61 struct {
62 struct page *page;
63 unsigned long offset;
64 } phys;
65
66 struct {
67 u8 *page;
68 u8 *addr;
69 } virt;
70 } src, dst;
71
72 struct scatter_walk in;
73 unsigned int nbytes;
74
75 struct scatter_walk out;
76 unsigned int total;
77
78 void *page;
79 u8 *buffer;
80 u8 *iv;
81
82 int flags;
83};
84
85extern const struct crypto_type crypto_blkcipher_type;
Herbert Xu055bcee2006-08-19 22:24:23 +100086extern const struct crypto_type crypto_hash_type;
Herbert Xu5cde0af2006-08-22 00:07:53 +100087
Herbert Xudb131ef2006-09-21 11:44:08 +100088void crypto_mod_put(struct crypto_alg *alg);
89
Herbert Xu4cc77202006-08-06 21:16:34 +100090int crypto_register_template(struct crypto_template *tmpl);
91void crypto_unregister_template(struct crypto_template *tmpl);
92struct crypto_template *crypto_lookup_template(const char *name);
93
Herbert Xu6bfd4802006-09-21 11:39:29 +100094int crypto_init_spawn(struct crypto_spawn *spawn, struct crypto_alg *alg,
95 struct crypto_instance *inst);
96void crypto_drop_spawn(struct crypto_spawn *spawn);
Herbert Xu2e306ee2006-12-17 10:05:58 +110097struct crypto_tfm *crypto_spawn_tfm(struct crypto_spawn *spawn, u32 type,
98 u32 mask);
Herbert Xu6bfd4802006-09-21 11:39:29 +100099
Herbert Xuebc610e2007-01-01 18:37:02 +1100100struct crypto_attr_type *crypto_get_attr_type(struct rtattr **tb);
101int crypto_check_attr_type(struct rtattr **tb, u32 type);
102struct crypto_alg *crypto_get_attr_alg(struct rtattr **tb, u32 type, u32 mask);
Herbert Xu7fed0bf2006-08-06 23:10:45 +1000103struct crypto_instance *crypto_alloc_instance(const char *name,
104 struct crypto_alg *alg);
105
Herbert Xu5cde0af2006-08-22 00:07:53 +1000106int blkcipher_walk_done(struct blkcipher_desc *desc,
107 struct blkcipher_walk *walk, int err);
108int blkcipher_walk_virt(struct blkcipher_desc *desc,
109 struct blkcipher_walk *walk);
110int blkcipher_walk_phys(struct blkcipher_desc *desc,
111 struct blkcipher_walk *walk);
112
113static inline void *crypto_tfm_ctx_aligned(struct crypto_tfm *tfm)
114{
115 unsigned long addr = (unsigned long)crypto_tfm_ctx(tfm);
116 unsigned long align = crypto_tfm_alg_alignmask(tfm);
117
118 if (align <= crypto_tfm_ctx_alignment())
119 align = 1;
120 return (void *)ALIGN(addr, align);
121}
122
Herbert Xu4cc77202006-08-06 21:16:34 +1000123static inline void *crypto_instance_ctx(struct crypto_instance *inst)
124{
125 return inst->__ctx;
126}
127
Herbert Xu5cde0af2006-08-22 00:07:53 +1000128static inline void *crypto_blkcipher_ctx(struct crypto_blkcipher *tfm)
129{
130 return crypto_tfm_ctx(&tfm->base);
131}
132
133static inline void *crypto_blkcipher_ctx_aligned(struct crypto_blkcipher *tfm)
134{
135 return crypto_tfm_ctx_aligned(&tfm->base);
136}
137
Herbert Xu2e306ee2006-12-17 10:05:58 +1100138static inline struct crypto_cipher *crypto_spawn_cipher(
139 struct crypto_spawn *spawn)
140{
141 u32 type = CRYPTO_ALG_TYPE_CIPHER;
142 u32 mask = CRYPTO_ALG_TYPE_MASK;
143
144 return __crypto_cipher_cast(crypto_spawn_tfm(spawn, type, mask));
145}
146
Herbert Xuf28776a2006-08-13 20:58:18 +1000147static inline struct cipher_alg *crypto_cipher_alg(struct crypto_cipher *tfm)
148{
149 return &crypto_cipher_tfm(tfm)->__crt_alg->cra_cipher;
150}
151
Herbert Xu2e306ee2006-12-17 10:05:58 +1100152static inline struct crypto_hash *crypto_spawn_hash(struct crypto_spawn *spawn)
153{
154 u32 type = CRYPTO_ALG_TYPE_HASH;
155 u32 mask = CRYPTO_ALG_TYPE_HASH_MASK;
156
157 return __crypto_hash_cast(crypto_spawn_tfm(spawn, type, mask));
158}
159
Herbert Xu055bcee2006-08-19 22:24:23 +1000160static inline void *crypto_hash_ctx_aligned(struct crypto_hash *tfm)
161{
162 return crypto_tfm_ctx_aligned(&tfm->base);
163}
164
Herbert Xu5cde0af2006-08-22 00:07:53 +1000165static inline void blkcipher_walk_init(struct blkcipher_walk *walk,
166 struct scatterlist *dst,
167 struct scatterlist *src,
168 unsigned int nbytes)
169{
170 walk->in.sg = src;
171 walk->out.sg = dst;
172 walk->total = nbytes;
173}
174
Herbert Xucce9e062006-08-21 21:08:13 +1000175#endif /* _CRYPTO_ALGAPI_H */
176