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