[CRYPTO] cipher: Added block ciphers for CBC/ECB
[safe/jmp/linux-2.6] / include / crypto / algapi.h
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
17 struct module;
18 struct seq_file;
19
20 struct crypto_type {
21         unsigned int (*ctxsize)(struct crypto_alg *alg);
22         int (*init)(struct crypto_tfm *tfm);
23         void (*exit)(struct crypto_tfm *tfm);
24         void (*show)(struct seq_file *m, struct crypto_alg *alg);
25 };
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
47 struct crypto_spawn {
48         struct list_head list;
49         struct crypto_alg *alg;
50         struct crypto_instance *inst;
51 };
52
53 struct scatter_walk {
54         struct scatterlist *sg;
55         unsigned int offset;
56 };
57
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;
85
86 void crypto_mod_put(struct crypto_alg *alg);
87
88 int crypto_register_template(struct crypto_template *tmpl);
89 void crypto_unregister_template(struct crypto_template *tmpl);
90 struct crypto_template *crypto_lookup_template(const char *name);
91
92 int crypto_init_spawn(struct crypto_spawn *spawn, struct crypto_alg *alg,
93                       struct crypto_instance *inst);
94 void crypto_drop_spawn(struct crypto_spawn *spawn);
95 struct crypto_tfm *crypto_spawn_tfm(struct crypto_spawn *spawn);
96
97 struct crypto_alg *crypto_get_attr_alg(void *param, unsigned int len,
98                                        u32 type, u32 mask);
99 struct crypto_instance *crypto_alloc_instance(const char *name,
100                                               struct crypto_alg *alg);
101
102 int blkcipher_walk_done(struct blkcipher_desc *desc,
103                         struct blkcipher_walk *walk, int err);
104 int blkcipher_walk_virt(struct blkcipher_desc *desc,
105                         struct blkcipher_walk *walk);
106 int blkcipher_walk_phys(struct blkcipher_desc *desc,
107                         struct blkcipher_walk *walk);
108
109 static inline void *crypto_tfm_ctx_aligned(struct crypto_tfm *tfm)
110 {
111         unsigned long addr = (unsigned long)crypto_tfm_ctx(tfm);
112         unsigned long align = crypto_tfm_alg_alignmask(tfm);
113
114         if (align <= crypto_tfm_ctx_alignment())
115                 align = 1;
116         return (void *)ALIGN(addr, align);
117 }
118
119 static inline void *crypto_instance_ctx(struct crypto_instance *inst)
120 {
121         return inst->__ctx;
122 }
123
124 static inline void *crypto_blkcipher_ctx(struct crypto_blkcipher *tfm)
125 {
126         return crypto_tfm_ctx(&tfm->base);
127 }
128
129 static inline void *crypto_blkcipher_ctx_aligned(struct crypto_blkcipher *tfm)
130 {
131         return crypto_tfm_ctx_aligned(&tfm->base);
132 }
133
134 static inline struct cipher_alg *crypto_cipher_alg(struct crypto_cipher *tfm)
135 {
136         return &crypto_cipher_tfm(tfm)->__crt_alg->cra_cipher;
137 }
138
139 static inline void blkcipher_walk_init(struct blkcipher_walk *walk,
140                                        struct scatterlist *dst,
141                                        struct scatterlist *src,
142                                        unsigned int nbytes)
143 {
144         walk->in.sg = src;
145         walk->out.sg = dst;
146         walk->total = nbytes;
147 }
148
149 #endif  /* _CRYPTO_ALGAPI_H */
150