[CRYPTO] Add support for low-level multi-block operations
[linux-2.6] / include / linux / crypto.h
1 /*
2  * Scatterlist Cryptographic API.
3  *
4  * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
5  * Copyright (c) 2002 David S. Miller (davem@redhat.com)
6  *
7  * Portions derived from Cryptoapi, by Alexander Kjeldaas <astor@fast.no>
8  * and Nettle, by Niels Möller.
9  * 
10  * This program is free software; you can redistribute it and/or modify it
11  * under the terms of the GNU General Public License as published by the Free
12  * Software Foundation; either version 2 of the License, or (at your option) 
13  * any later version.
14  *
15  */
16 #ifndef _LINUX_CRYPTO_H
17 #define _LINUX_CRYPTO_H
18
19 #include <linux/config.h>
20 #include <linux/module.h>
21 #include <linux/kernel.h>
22 #include <linux/types.h>
23 #include <linux/list.h>
24 #include <linux/string.h>
25 #include <asm/page.h>
26
27 /*
28  * Algorithm masks and types.
29  */
30 #define CRYPTO_ALG_TYPE_MASK            0x000000ff
31 #define CRYPTO_ALG_TYPE_CIPHER          0x00000001
32 #define CRYPTO_ALG_TYPE_DIGEST          0x00000002
33 #define CRYPTO_ALG_TYPE_COMPRESS        0x00000004
34
35 /*
36  * Transform masks and values (for crt_flags).
37  */
38 #define CRYPTO_TFM_MODE_MASK            0x000000ff
39 #define CRYPTO_TFM_REQ_MASK             0x000fff00
40 #define CRYPTO_TFM_RES_MASK             0xfff00000
41
42 #define CRYPTO_TFM_MODE_ECB             0x00000001
43 #define CRYPTO_TFM_MODE_CBC             0x00000002
44 #define CRYPTO_TFM_MODE_CFB             0x00000004
45 #define CRYPTO_TFM_MODE_CTR             0x00000008
46
47 #define CRYPTO_TFM_REQ_WEAK_KEY         0x00000100
48 #define CRYPTO_TFM_RES_WEAK_KEY         0x00100000
49 #define CRYPTO_TFM_RES_BAD_KEY_LEN      0x00200000
50 #define CRYPTO_TFM_RES_BAD_KEY_SCHED    0x00400000
51 #define CRYPTO_TFM_RES_BAD_BLOCK_LEN    0x00800000
52 #define CRYPTO_TFM_RES_BAD_FLAGS        0x01000000
53
54 /*
55  * Miscellaneous stuff.
56  */
57 #define CRYPTO_UNSPEC                   0
58 #define CRYPTO_MAX_ALG_NAME             64
59
60 #define CRYPTO_DIR_ENCRYPT              1
61 #define CRYPTO_DIR_DECRYPT              0
62
63 struct scatterlist;
64 struct crypto_tfm;
65
66 struct cipher_desc {
67         struct crypto_tfm *tfm;
68         void (*crfn)(void *ctx, u8 *dst, const u8 *src);
69         unsigned int (*prfn)(const struct cipher_desc *desc, u8 *dst,
70                              const u8 *src, unsigned int nbytes);
71         void *info;
72 };
73
74 /*
75  * Algorithms: modular crypto algorithm implementations, managed
76  * via crypto_register_alg() and crypto_unregister_alg().
77  */
78 struct cipher_alg {
79         unsigned int cia_min_keysize;
80         unsigned int cia_max_keysize;
81         int (*cia_setkey)(void *ctx, const u8 *key,
82                           unsigned int keylen, u32 *flags);
83         void (*cia_encrypt)(void *ctx, u8 *dst, const u8 *src);
84         void (*cia_decrypt)(void *ctx, u8 *dst, const u8 *src);
85
86         unsigned int (*cia_encrypt_ecb)(const struct cipher_desc *desc,
87                                         u8 *dst, const u8 *src,
88                                         unsigned int nbytes);
89         unsigned int (*cia_decrypt_ecb)(const struct cipher_desc *desc,
90                                         u8 *dst, const u8 *src,
91                                         unsigned int nbytes);
92         unsigned int (*cia_encrypt_cbc)(const struct cipher_desc *desc,
93                                         u8 *dst, const u8 *src,
94                                         unsigned int nbytes);
95         unsigned int (*cia_decrypt_cbc)(const struct cipher_desc *desc,
96                                         u8 *dst, const u8 *src,
97                                         unsigned int nbytes);
98 };
99
100 struct digest_alg {
101         unsigned int dia_digestsize;
102         void (*dia_init)(void *ctx);
103         void (*dia_update)(void *ctx, const u8 *data, unsigned int len);
104         void (*dia_final)(void *ctx, u8 *out);
105         int (*dia_setkey)(void *ctx, const u8 *key,
106                           unsigned int keylen, u32 *flags);
107 };
108
109 struct compress_alg {
110         int (*coa_init)(void *ctx);
111         void (*coa_exit)(void *ctx);
112         int (*coa_compress)(void *ctx, const u8 *src, unsigned int slen,
113                             u8 *dst, unsigned int *dlen);
114         int (*coa_decompress)(void *ctx, const u8 *src, unsigned int slen,
115                               u8 *dst, unsigned int *dlen);
116 };
117
118 #define cra_cipher      cra_u.cipher
119 #define cra_digest      cra_u.digest
120 #define cra_compress    cra_u.compress
121
122 struct crypto_alg {
123         struct list_head cra_list;
124         u32 cra_flags;
125         unsigned int cra_blocksize;
126         unsigned int cra_ctxsize;
127         const char cra_name[CRYPTO_MAX_ALG_NAME];
128
129         union {
130                 struct cipher_alg cipher;
131                 struct digest_alg digest;
132                 struct compress_alg compress;
133         } cra_u;
134         
135         struct module *cra_module;
136 };
137
138 /*
139  * Algorithm registration interface.
140  */
141 int crypto_register_alg(struct crypto_alg *alg);
142 int crypto_unregister_alg(struct crypto_alg *alg);
143
144 /*
145  * Algorithm query interface.
146  */
147 #ifdef CONFIG_CRYPTO
148 int crypto_alg_available(const char *name, u32 flags);
149 #else
150 static inline int crypto_alg_available(const char *name, u32 flags)
151 {
152         return 0;
153 }
154 #endif
155
156 /*
157  * Transforms: user-instantiated objects which encapsulate algorithms
158  * and core processing logic.  Managed via crypto_alloc_tfm() and
159  * crypto_free_tfm(), as well as the various helpers below.
160  */
161
162 struct cipher_tfm {
163         void *cit_iv;
164         unsigned int cit_ivsize;
165         u32 cit_mode;
166         int (*cit_setkey)(struct crypto_tfm *tfm,
167                           const u8 *key, unsigned int keylen);
168         int (*cit_encrypt)(struct crypto_tfm *tfm,
169                            struct scatterlist *dst,
170                            struct scatterlist *src,
171                            unsigned int nbytes);
172         int (*cit_encrypt_iv)(struct crypto_tfm *tfm,
173                               struct scatterlist *dst,
174                               struct scatterlist *src,
175                               unsigned int nbytes, u8 *iv);
176         int (*cit_decrypt)(struct crypto_tfm *tfm,
177                            struct scatterlist *dst,
178                            struct scatterlist *src,
179                            unsigned int nbytes);
180         int (*cit_decrypt_iv)(struct crypto_tfm *tfm,
181                            struct scatterlist *dst,
182                            struct scatterlist *src,
183                            unsigned int nbytes, u8 *iv);
184         void (*cit_xor_block)(u8 *dst, const u8 *src);
185 };
186
187 struct digest_tfm {
188         void (*dit_init)(struct crypto_tfm *tfm);
189         void (*dit_update)(struct crypto_tfm *tfm,
190                            struct scatterlist *sg, unsigned int nsg);
191         void (*dit_final)(struct crypto_tfm *tfm, u8 *out);
192         void (*dit_digest)(struct crypto_tfm *tfm, struct scatterlist *sg,
193                            unsigned int nsg, u8 *out);
194         int (*dit_setkey)(struct crypto_tfm *tfm,
195                           const u8 *key, unsigned int keylen);
196 #ifdef CONFIG_CRYPTO_HMAC
197         void *dit_hmac_block;
198 #endif
199 };
200
201 struct compress_tfm {
202         int (*cot_compress)(struct crypto_tfm *tfm,
203                             const u8 *src, unsigned int slen,
204                             u8 *dst, unsigned int *dlen);
205         int (*cot_decompress)(struct crypto_tfm *tfm,
206                               const u8 *src, unsigned int slen,
207                               u8 *dst, unsigned int *dlen);
208 };
209
210 #define crt_cipher      crt_u.cipher
211 #define crt_digest      crt_u.digest
212 #define crt_compress    crt_u.compress
213
214 struct crypto_tfm {
215
216         u32 crt_flags;
217         
218         union {
219                 struct cipher_tfm cipher;
220                 struct digest_tfm digest;
221                 struct compress_tfm compress;
222         } crt_u;
223         
224         struct crypto_alg *__crt_alg;
225 };
226
227 /* 
228  * Transform user interface.
229  */
230  
231 /*
232  * crypto_alloc_tfm() will first attempt to locate an already loaded algorithm.
233  * If that fails and the kernel supports dynamically loadable modules, it
234  * will then attempt to load a module of the same name or alias.  A refcount
235  * is grabbed on the algorithm which is then associated with the new transform.
236  *
237  * crypto_free_tfm() frees up the transform and any associated resources,
238  * then drops the refcount on the associated algorithm.
239  */
240 struct crypto_tfm *crypto_alloc_tfm(const char *alg_name, u32 tfm_flags);
241 void crypto_free_tfm(struct crypto_tfm *tfm);
242
243 /*
244  * Transform helpers which query the underlying algorithm.
245  */
246 static inline const char *crypto_tfm_alg_name(struct crypto_tfm *tfm)
247 {
248         return tfm->__crt_alg->cra_name;
249 }
250
251 static inline const char *crypto_tfm_alg_modname(struct crypto_tfm *tfm)
252 {
253         return module_name(tfm->__crt_alg->cra_module);
254 }
255
256 static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm)
257 {
258         return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK;
259 }
260
261 static inline unsigned int crypto_tfm_alg_min_keysize(struct crypto_tfm *tfm)
262 {
263         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
264         return tfm->__crt_alg->cra_cipher.cia_min_keysize;
265 }
266
267 static inline unsigned int crypto_tfm_alg_max_keysize(struct crypto_tfm *tfm)
268 {
269         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
270         return tfm->__crt_alg->cra_cipher.cia_max_keysize;
271 }
272
273 static inline unsigned int crypto_tfm_alg_ivsize(struct crypto_tfm *tfm)
274 {
275         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
276         return tfm->crt_cipher.cit_ivsize;
277 }
278
279 static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm)
280 {
281         return tfm->__crt_alg->cra_blocksize;
282 }
283
284 static inline unsigned int crypto_tfm_alg_digestsize(struct crypto_tfm *tfm)
285 {
286         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
287         return tfm->__crt_alg->cra_digest.dia_digestsize;
288 }
289
290 static inline void *crypto_tfm_ctx(struct crypto_tfm *tfm)
291 {
292         return (void *)&tfm[1];
293 }
294
295 /*
296  * API wrappers.
297  */
298 static inline void crypto_digest_init(struct crypto_tfm *tfm)
299 {
300         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
301         tfm->crt_digest.dit_init(tfm);
302 }
303
304 static inline void crypto_digest_update(struct crypto_tfm *tfm,
305                                         struct scatterlist *sg,
306                                         unsigned int nsg)
307 {
308         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
309         tfm->crt_digest.dit_update(tfm, sg, nsg);
310 }
311
312 static inline void crypto_digest_final(struct crypto_tfm *tfm, u8 *out)
313 {
314         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
315         tfm->crt_digest.dit_final(tfm, out);
316 }
317
318 static inline void crypto_digest_digest(struct crypto_tfm *tfm,
319                                         struct scatterlist *sg,
320                                         unsigned int nsg, u8 *out)
321 {
322         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
323         tfm->crt_digest.dit_digest(tfm, sg, nsg, out);
324 }
325
326 static inline int crypto_digest_setkey(struct crypto_tfm *tfm,
327                                        const u8 *key, unsigned int keylen)
328 {
329         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
330         if (tfm->crt_digest.dit_setkey == NULL)
331                 return -ENOSYS;
332         return tfm->crt_digest.dit_setkey(tfm, key, keylen);
333 }
334
335 static inline int crypto_cipher_setkey(struct crypto_tfm *tfm,
336                                        const u8 *key, unsigned int keylen)
337 {
338         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
339         return tfm->crt_cipher.cit_setkey(tfm, key, keylen);
340 }
341
342 static inline int crypto_cipher_encrypt(struct crypto_tfm *tfm,
343                                         struct scatterlist *dst,
344                                         struct scatterlist *src,
345                                         unsigned int nbytes)
346 {
347         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
348         return tfm->crt_cipher.cit_encrypt(tfm, dst, src, nbytes);
349 }                                        
350
351 static inline int crypto_cipher_encrypt_iv(struct crypto_tfm *tfm,
352                                            struct scatterlist *dst,
353                                            struct scatterlist *src,
354                                            unsigned int nbytes, u8 *iv)
355 {
356         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
357         BUG_ON(tfm->crt_cipher.cit_mode == CRYPTO_TFM_MODE_ECB);
358         return tfm->crt_cipher.cit_encrypt_iv(tfm, dst, src, nbytes, iv);
359 }                                        
360
361 static inline int crypto_cipher_decrypt(struct crypto_tfm *tfm,
362                                         struct scatterlist *dst,
363                                         struct scatterlist *src,
364                                         unsigned int nbytes)
365 {
366         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
367         return tfm->crt_cipher.cit_decrypt(tfm, dst, src, nbytes);
368 }
369
370 static inline int crypto_cipher_decrypt_iv(struct crypto_tfm *tfm,
371                                            struct scatterlist *dst,
372                                            struct scatterlist *src,
373                                            unsigned int nbytes, u8 *iv)
374 {
375         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
376         BUG_ON(tfm->crt_cipher.cit_mode == CRYPTO_TFM_MODE_ECB);
377         return tfm->crt_cipher.cit_decrypt_iv(tfm, dst, src, nbytes, iv);
378 }
379
380 static inline void crypto_cipher_set_iv(struct crypto_tfm *tfm,
381                                         const u8 *src, unsigned int len)
382 {
383         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
384         memcpy(tfm->crt_cipher.cit_iv, src, len);
385 }
386
387 static inline void crypto_cipher_get_iv(struct crypto_tfm *tfm,
388                                         u8 *dst, unsigned int len)
389 {
390         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
391         memcpy(dst, tfm->crt_cipher.cit_iv, len);
392 }
393
394 static inline int crypto_comp_compress(struct crypto_tfm *tfm,
395                                        const u8 *src, unsigned int slen,
396                                        u8 *dst, unsigned int *dlen)
397 {
398         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_COMPRESS);
399         return tfm->crt_compress.cot_compress(tfm, src, slen, dst, dlen);
400 }
401
402 static inline int crypto_comp_decompress(struct crypto_tfm *tfm,
403                                          const u8 *src, unsigned int slen,
404                                          u8 *dst, unsigned int *dlen)
405 {
406         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_COMPRESS);
407         return tfm->crt_compress.cot_decompress(tfm, src, slen, dst, dlen);
408 }
409
410 /*
411  * HMAC support.
412  */
413 #ifdef CONFIG_CRYPTO_HMAC
414 void crypto_hmac_init(struct crypto_tfm *tfm, u8 *key, unsigned int *keylen);
415 void crypto_hmac_update(struct crypto_tfm *tfm,
416                         struct scatterlist *sg, unsigned int nsg);
417 void crypto_hmac_final(struct crypto_tfm *tfm, u8 *key,
418                        unsigned int *keylen, u8 *out);
419 void crypto_hmac(struct crypto_tfm *tfm, u8 *key, unsigned int *keylen,
420                  struct scatterlist *sg, unsigned int nsg, u8 *out);
421 #endif  /* CONFIG_CRYPTO_HMAC */
422
423 #endif  /* _LINUX_CRYPTO_H */
424