[CRYPTO] api: Fixed crypto_tfm context alignment
[firefly-linux-kernel-4.4.55.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/slab.h>
25 #include <linux/string.h>
26 #include <linux/uaccess.h>
27
28 /*
29  * Algorithm masks and types.
30  */
31 #define CRYPTO_ALG_TYPE_MASK            0x000000ff
32 #define CRYPTO_ALG_TYPE_CIPHER          0x00000001
33 #define CRYPTO_ALG_TYPE_DIGEST          0x00000002
34 #define CRYPTO_ALG_TYPE_COMPRESS        0x00000004
35
36 /*
37  * Transform masks and values (for crt_flags).
38  */
39 #define CRYPTO_TFM_MODE_MASK            0x000000ff
40 #define CRYPTO_TFM_REQ_MASK             0x000fff00
41 #define CRYPTO_TFM_RES_MASK             0xfff00000
42
43 #define CRYPTO_TFM_MODE_ECB             0x00000001
44 #define CRYPTO_TFM_MODE_CBC             0x00000002
45 #define CRYPTO_TFM_MODE_CFB             0x00000004
46 #define CRYPTO_TFM_MODE_CTR             0x00000008
47
48 #define CRYPTO_TFM_REQ_WEAK_KEY         0x00000100
49 #define CRYPTO_TFM_REQ_MAY_SLEEP        0x00000200
50 #define CRYPTO_TFM_RES_WEAK_KEY         0x00100000
51 #define CRYPTO_TFM_RES_BAD_KEY_LEN      0x00200000
52 #define CRYPTO_TFM_RES_BAD_KEY_SCHED    0x00400000
53 #define CRYPTO_TFM_RES_BAD_BLOCK_LEN    0x00800000
54 #define CRYPTO_TFM_RES_BAD_FLAGS        0x01000000
55
56 /*
57  * Miscellaneous stuff.
58  */
59 #define CRYPTO_UNSPEC                   0
60 #define CRYPTO_MAX_ALG_NAME             64
61
62 #define CRYPTO_DIR_ENCRYPT              1
63 #define CRYPTO_DIR_DECRYPT              0
64
65 /*
66  * The macro CRYPTO_MINALIGN_ATTR (along with the void * type in the actual
67  * declaration) is used to ensure that the crypto_tfm context structure is
68  * aligned correctly for the given architecture so that there are no alignment
69  * faults for C data types.  In particular, this is required on platforms such
70  * as arm where pointers are 32-bit aligned but there are data types such as
71  * u64 which require 64-bit alignment.
72  */
73 #if defined(ARCH_KMALLOC_MINALIGN)
74 #define CRYPTO_MINALIGN ARCH_KMALLOC_MINALIGN
75 #elif defined(ARCH_SLAB_MINALIGN)
76 #define CRYPTO_MINALIGN ARCH_SLAB_MINALIGN
77 #endif
78
79 #ifdef CRYPTO_MINALIGN
80 #define CRYPTO_MINALIGN_ATTR __attribute__ ((__aligned__(CRYPTO_MINALIGN)))
81 #else
82 #define CRYPTO_MINALIGN_ATTR
83 #endif
84
85 struct scatterlist;
86 struct crypto_tfm;
87
88 struct cipher_desc {
89         struct crypto_tfm *tfm;
90         void (*crfn)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
91         unsigned int (*prfn)(const struct cipher_desc *desc, u8 *dst,
92                              const u8 *src, unsigned int nbytes);
93         void *info;
94 };
95
96 /*
97  * Algorithms: modular crypto algorithm implementations, managed
98  * via crypto_register_alg() and crypto_unregister_alg().
99  */
100 struct cipher_alg {
101         unsigned int cia_min_keysize;
102         unsigned int cia_max_keysize;
103         int (*cia_setkey)(struct crypto_tfm *tfm, const u8 *key,
104                           unsigned int keylen, u32 *flags);
105         void (*cia_encrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
106         void (*cia_decrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
107
108         unsigned int (*cia_encrypt_ecb)(const struct cipher_desc *desc,
109                                         u8 *dst, const u8 *src,
110                                         unsigned int nbytes);
111         unsigned int (*cia_decrypt_ecb)(const struct cipher_desc *desc,
112                                         u8 *dst, const u8 *src,
113                                         unsigned int nbytes);
114         unsigned int (*cia_encrypt_cbc)(const struct cipher_desc *desc,
115                                         u8 *dst, const u8 *src,
116                                         unsigned int nbytes);
117         unsigned int (*cia_decrypt_cbc)(const struct cipher_desc *desc,
118                                         u8 *dst, const u8 *src,
119                                         unsigned int nbytes);
120 };
121
122 struct digest_alg {
123         unsigned int dia_digestsize;
124         void (*dia_init)(struct crypto_tfm *tfm);
125         void (*dia_update)(struct crypto_tfm *tfm, const u8 *data,
126                            unsigned int len);
127         void (*dia_final)(struct crypto_tfm *tfm, u8 *out);
128         int (*dia_setkey)(struct crypto_tfm *tfm, const u8 *key,
129                           unsigned int keylen, u32 *flags);
130 };
131
132 struct compress_alg {
133         int (*coa_compress)(struct crypto_tfm *tfm, const u8 *src,
134                             unsigned int slen, u8 *dst, unsigned int *dlen);
135         int (*coa_decompress)(struct crypto_tfm *tfm, const u8 *src,
136                               unsigned int slen, u8 *dst, unsigned int *dlen);
137 };
138
139 #define cra_cipher      cra_u.cipher
140 #define cra_digest      cra_u.digest
141 #define cra_compress    cra_u.compress
142
143 struct crypto_alg {
144         struct list_head cra_list;
145         u32 cra_flags;
146         unsigned int cra_blocksize;
147         unsigned int cra_ctxsize;
148         unsigned int cra_alignmask;
149
150         int cra_priority;
151
152         char cra_name[CRYPTO_MAX_ALG_NAME];
153         char cra_driver_name[CRYPTO_MAX_ALG_NAME];
154
155         union {
156                 struct cipher_alg cipher;
157                 struct digest_alg digest;
158                 struct compress_alg compress;
159         } cra_u;
160
161         int (*cra_init)(struct crypto_tfm *tfm);
162         void (*cra_exit)(struct crypto_tfm *tfm);
163         
164         struct module *cra_module;
165 };
166
167 /*
168  * Algorithm registration interface.
169  */
170 int crypto_register_alg(struct crypto_alg *alg);
171 int crypto_unregister_alg(struct crypto_alg *alg);
172
173 /*
174  * Algorithm query interface.
175  */
176 #ifdef CONFIG_CRYPTO
177 int crypto_alg_available(const char *name, u32 flags);
178 #else
179 static inline int crypto_alg_available(const char *name, u32 flags)
180 {
181         return 0;
182 }
183 #endif
184
185 /*
186  * Transforms: user-instantiated objects which encapsulate algorithms
187  * and core processing logic.  Managed via crypto_alloc_tfm() and
188  * crypto_free_tfm(), as well as the various helpers below.
189  */
190
191 struct cipher_tfm {
192         void *cit_iv;
193         unsigned int cit_ivsize;
194         u32 cit_mode;
195         int (*cit_setkey)(struct crypto_tfm *tfm,
196                           const u8 *key, unsigned int keylen);
197         int (*cit_encrypt)(struct crypto_tfm *tfm,
198                            struct scatterlist *dst,
199                            struct scatterlist *src,
200                            unsigned int nbytes);
201         int (*cit_encrypt_iv)(struct crypto_tfm *tfm,
202                               struct scatterlist *dst,
203                               struct scatterlist *src,
204                               unsigned int nbytes, u8 *iv);
205         int (*cit_decrypt)(struct crypto_tfm *tfm,
206                            struct scatterlist *dst,
207                            struct scatterlist *src,
208                            unsigned int nbytes);
209         int (*cit_decrypt_iv)(struct crypto_tfm *tfm,
210                            struct scatterlist *dst,
211                            struct scatterlist *src,
212                            unsigned int nbytes, u8 *iv);
213         void (*cit_xor_block)(u8 *dst, const u8 *src);
214 };
215
216 struct digest_tfm {
217         void (*dit_init)(struct crypto_tfm *tfm);
218         void (*dit_update)(struct crypto_tfm *tfm,
219                            struct scatterlist *sg, unsigned int nsg);
220         void (*dit_final)(struct crypto_tfm *tfm, u8 *out);
221         void (*dit_digest)(struct crypto_tfm *tfm, struct scatterlist *sg,
222                            unsigned int nsg, u8 *out);
223         int (*dit_setkey)(struct crypto_tfm *tfm,
224                           const u8 *key, unsigned int keylen);
225 #ifdef CONFIG_CRYPTO_HMAC
226         void *dit_hmac_block;
227 #endif
228 };
229
230 struct compress_tfm {
231         int (*cot_compress)(struct crypto_tfm *tfm,
232                             const u8 *src, unsigned int slen,
233                             u8 *dst, unsigned int *dlen);
234         int (*cot_decompress)(struct crypto_tfm *tfm,
235                               const u8 *src, unsigned int slen,
236                               u8 *dst, unsigned int *dlen);
237 };
238
239 #define crt_cipher      crt_u.cipher
240 #define crt_digest      crt_u.digest
241 #define crt_compress    crt_u.compress
242
243 struct crypto_tfm {
244
245         u32 crt_flags;
246         
247         union {
248                 struct cipher_tfm cipher;
249                 struct digest_tfm digest;
250                 struct compress_tfm compress;
251         } crt_u;
252         
253         struct crypto_alg *__crt_alg;
254
255         void *__crt_ctx[] CRYPTO_MINALIGN_ATTR;
256 };
257
258 /* 
259  * Transform user interface.
260  */
261  
262 /*
263  * crypto_alloc_tfm() will first attempt to locate an already loaded algorithm.
264  * If that fails and the kernel supports dynamically loadable modules, it
265  * will then attempt to load a module of the same name or alias.  A refcount
266  * is grabbed on the algorithm which is then associated with the new transform.
267  *
268  * crypto_free_tfm() frees up the transform and any associated resources,
269  * then drops the refcount on the associated algorithm.
270  */
271 struct crypto_tfm *crypto_alloc_tfm(const char *alg_name, u32 tfm_flags);
272 void crypto_free_tfm(struct crypto_tfm *tfm);
273
274 /*
275  * Transform helpers which query the underlying algorithm.
276  */
277 static inline const char *crypto_tfm_alg_name(struct crypto_tfm *tfm)
278 {
279         return tfm->__crt_alg->cra_name;
280 }
281
282 static inline const char *crypto_tfm_alg_modname(struct crypto_tfm *tfm)
283 {
284         return module_name(tfm->__crt_alg->cra_module);
285 }
286
287 static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm)
288 {
289         return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK;
290 }
291
292 static inline unsigned int crypto_tfm_alg_min_keysize(struct crypto_tfm *tfm)
293 {
294         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
295         return tfm->__crt_alg->cra_cipher.cia_min_keysize;
296 }
297
298 static inline unsigned int crypto_tfm_alg_max_keysize(struct crypto_tfm *tfm)
299 {
300         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
301         return tfm->__crt_alg->cra_cipher.cia_max_keysize;
302 }
303
304 static inline unsigned int crypto_tfm_alg_ivsize(struct crypto_tfm *tfm)
305 {
306         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
307         return tfm->crt_cipher.cit_ivsize;
308 }
309
310 static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm)
311 {
312         return tfm->__crt_alg->cra_blocksize;
313 }
314
315 static inline unsigned int crypto_tfm_alg_digestsize(struct crypto_tfm *tfm)
316 {
317         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
318         return tfm->__crt_alg->cra_digest.dia_digestsize;
319 }
320
321 static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm *tfm)
322 {
323         return tfm->__crt_alg->cra_alignmask;
324 }
325
326 static inline void *crypto_tfm_ctx(struct crypto_tfm *tfm)
327 {
328         return tfm->__crt_ctx;
329 }
330
331 static inline unsigned int crypto_tfm_ctx_alignment(void)
332 {
333         struct crypto_tfm *tfm;
334         return __alignof__(tfm->__crt_ctx);
335 }
336
337 /*
338  * API wrappers.
339  */
340 static inline void crypto_digest_init(struct crypto_tfm *tfm)
341 {
342         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
343         tfm->crt_digest.dit_init(tfm);
344 }
345
346 static inline void crypto_digest_update(struct crypto_tfm *tfm,
347                                         struct scatterlist *sg,
348                                         unsigned int nsg)
349 {
350         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
351         tfm->crt_digest.dit_update(tfm, sg, nsg);
352 }
353
354 static inline void crypto_digest_final(struct crypto_tfm *tfm, u8 *out)
355 {
356         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
357         tfm->crt_digest.dit_final(tfm, out);
358 }
359
360 static inline void crypto_digest_digest(struct crypto_tfm *tfm,
361                                         struct scatterlist *sg,
362                                         unsigned int nsg, u8 *out)
363 {
364         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
365         tfm->crt_digest.dit_digest(tfm, sg, nsg, out);
366 }
367
368 static inline int crypto_digest_setkey(struct crypto_tfm *tfm,
369                                        const u8 *key, unsigned int keylen)
370 {
371         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
372         if (tfm->crt_digest.dit_setkey == NULL)
373                 return -ENOSYS;
374         return tfm->crt_digest.dit_setkey(tfm, key, keylen);
375 }
376
377 static inline int crypto_cipher_setkey(struct crypto_tfm *tfm,
378                                        const u8 *key, unsigned int keylen)
379 {
380         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
381         return tfm->crt_cipher.cit_setkey(tfm, key, keylen);
382 }
383
384 static inline int crypto_cipher_encrypt(struct crypto_tfm *tfm,
385                                         struct scatterlist *dst,
386                                         struct scatterlist *src,
387                                         unsigned int nbytes)
388 {
389         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
390         return tfm->crt_cipher.cit_encrypt(tfm, dst, src, nbytes);
391 }                                        
392
393 static inline int crypto_cipher_encrypt_iv(struct crypto_tfm *tfm,
394                                            struct scatterlist *dst,
395                                            struct scatterlist *src,
396                                            unsigned int nbytes, u8 *iv)
397 {
398         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
399         BUG_ON(tfm->crt_cipher.cit_mode == CRYPTO_TFM_MODE_ECB);
400         return tfm->crt_cipher.cit_encrypt_iv(tfm, dst, src, nbytes, iv);
401 }                                        
402
403 static inline int crypto_cipher_decrypt(struct crypto_tfm *tfm,
404                                         struct scatterlist *dst,
405                                         struct scatterlist *src,
406                                         unsigned int nbytes)
407 {
408         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
409         return tfm->crt_cipher.cit_decrypt(tfm, dst, src, nbytes);
410 }
411
412 static inline int crypto_cipher_decrypt_iv(struct crypto_tfm *tfm,
413                                            struct scatterlist *dst,
414                                            struct scatterlist *src,
415                                            unsigned int nbytes, u8 *iv)
416 {
417         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
418         BUG_ON(tfm->crt_cipher.cit_mode == CRYPTO_TFM_MODE_ECB);
419         return tfm->crt_cipher.cit_decrypt_iv(tfm, dst, src, nbytes, iv);
420 }
421
422 static inline void crypto_cipher_set_iv(struct crypto_tfm *tfm,
423                                         const u8 *src, unsigned int len)
424 {
425         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
426         memcpy(tfm->crt_cipher.cit_iv, src, len);
427 }
428
429 static inline void crypto_cipher_get_iv(struct crypto_tfm *tfm,
430                                         u8 *dst, unsigned int len)
431 {
432         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
433         memcpy(dst, tfm->crt_cipher.cit_iv, len);
434 }
435
436 static inline int crypto_comp_compress(struct crypto_tfm *tfm,
437                                        const u8 *src, unsigned int slen,
438                                        u8 *dst, unsigned int *dlen)
439 {
440         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_COMPRESS);
441         return tfm->crt_compress.cot_compress(tfm, src, slen, dst, dlen);
442 }
443
444 static inline int crypto_comp_decompress(struct crypto_tfm *tfm,
445                                          const u8 *src, unsigned int slen,
446                                          u8 *dst, unsigned int *dlen)
447 {
448         BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_COMPRESS);
449         return tfm->crt_compress.cot_decompress(tfm, src, slen, dst, dlen);
450 }
451
452 /*
453  * HMAC support.
454  */
455 #ifdef CONFIG_CRYPTO_HMAC
456 void crypto_hmac_init(struct crypto_tfm *tfm, u8 *key, unsigned int *keylen);
457 void crypto_hmac_update(struct crypto_tfm *tfm,
458                         struct scatterlist *sg, unsigned int nsg);
459 void crypto_hmac_final(struct crypto_tfm *tfm, u8 *key,
460                        unsigned int *keylen, u8 *out);
461 void crypto_hmac(struct crypto_tfm *tfm, u8 *key, unsigned int *keylen,
462                  struct scatterlist *sg, unsigned int nsg, u8 *out);
463 #endif  /* CONFIG_CRYPTO_HMAC */
464
465 #endif  /* _LINUX_CRYPTO_H */
466