blob: 053bfab43e8d960a9b596826bed498ce3e1caf69 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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)
Herbert Xu5cb14542005-11-05 16:58:14 +11006 * Copyright (c) 2005 Herbert Xu <herbert@gondor.apana.org.au>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
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
Herbert Xu6521f302006-08-06 20:28:44 +100020#include <asm/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/module.h>
22#include <linux/kernel.h>
23#include <linux/types.h>
24#include <linux/list.h>
Herbert Xu79911102006-08-21 21:03:52 +100025#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/string.h>
Herbert Xu79911102006-08-21 21:03:52 +100027#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29/*
30 * Algorithm masks and types.
31 */
Herbert Xu28259822006-08-06 21:23:26 +100032#define CRYPTO_ALG_TYPE_MASK 0x0000000f
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#define CRYPTO_ALG_TYPE_CIPHER 0x00000001
34#define CRYPTO_ALG_TYPE_DIGEST 0x00000002
35#define CRYPTO_ALG_TYPE_COMPRESS 0x00000004
36
Herbert Xu28259822006-08-06 21:23:26 +100037#define CRYPTO_ALG_LARVAL 0x00000010
Herbert Xu6bfd4802006-09-21 11:39:29 +100038#define CRYPTO_ALG_DEAD 0x00000020
39#define CRYPTO_ALG_DYING 0x00000040
Herbert Xu28259822006-08-06 21:23:26 +100040
Linus Torvalds1da177e2005-04-16 15:20:36 -070041/*
42 * Transform masks and values (for crt_flags).
43 */
44#define CRYPTO_TFM_MODE_MASK 0x000000ff
45#define CRYPTO_TFM_REQ_MASK 0x000fff00
46#define CRYPTO_TFM_RES_MASK 0xfff00000
47
48#define CRYPTO_TFM_MODE_ECB 0x00000001
49#define CRYPTO_TFM_MODE_CBC 0x00000002
50#define CRYPTO_TFM_MODE_CFB 0x00000004
51#define CRYPTO_TFM_MODE_CTR 0x00000008
52
53#define CRYPTO_TFM_REQ_WEAK_KEY 0x00000100
Herbert Xu64baf3c2005-09-01 17:43:05 -070054#define CRYPTO_TFM_REQ_MAY_SLEEP 0x00000200
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#define CRYPTO_TFM_RES_WEAK_KEY 0x00100000
56#define CRYPTO_TFM_RES_BAD_KEY_LEN 0x00200000
57#define CRYPTO_TFM_RES_BAD_KEY_SCHED 0x00400000
58#define CRYPTO_TFM_RES_BAD_BLOCK_LEN 0x00800000
59#define CRYPTO_TFM_RES_BAD_FLAGS 0x01000000
60
61/*
62 * Miscellaneous stuff.
63 */
64#define CRYPTO_UNSPEC 0
65#define CRYPTO_MAX_ALG_NAME 64
66
67#define CRYPTO_DIR_ENCRYPT 1
68#define CRYPTO_DIR_DECRYPT 0
69
Herbert Xu79911102006-08-21 21:03:52 +100070/*
71 * The macro CRYPTO_MINALIGN_ATTR (along with the void * type in the actual
72 * declaration) is used to ensure that the crypto_tfm context structure is
73 * aligned correctly for the given architecture so that there are no alignment
74 * faults for C data types. In particular, this is required on platforms such
75 * as arm where pointers are 32-bit aligned but there are data types such as
76 * u64 which require 64-bit alignment.
77 */
78#if defined(ARCH_KMALLOC_MINALIGN)
79#define CRYPTO_MINALIGN ARCH_KMALLOC_MINALIGN
80#elif defined(ARCH_SLAB_MINALIGN)
81#define CRYPTO_MINALIGN ARCH_SLAB_MINALIGN
82#endif
83
84#ifdef CRYPTO_MINALIGN
85#define CRYPTO_MINALIGN_ATTR __attribute__ ((__aligned__(CRYPTO_MINALIGN)))
86#else
87#define CRYPTO_MINALIGN_ATTR
88#endif
89
Linus Torvalds1da177e2005-04-16 15:20:36 -070090struct scatterlist;
Herbert Xu40725182005-07-06 13:51:52 -070091struct crypto_tfm;
92
93struct cipher_desc {
94 struct crypto_tfm *tfm;
Herbert Xu6c2bb982006-05-16 22:09:29 +100095 void (*crfn)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Herbert Xu40725182005-07-06 13:51:52 -070096 unsigned int (*prfn)(const struct cipher_desc *desc, u8 *dst,
97 const u8 *src, unsigned int nbytes);
98 void *info;
99};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101/*
102 * Algorithms: modular crypto algorithm implementations, managed
103 * via crypto_register_alg() and crypto_unregister_alg().
104 */
105struct cipher_alg {
106 unsigned int cia_min_keysize;
107 unsigned int cia_max_keysize;
Herbert Xu6c2bb982006-05-16 22:09:29 +1000108 int (*cia_setkey)(struct crypto_tfm *tfm, const u8 *key,
Herbert Xu560c06a2006-08-13 14:16:39 +1000109 unsigned int keylen);
Herbert Xu6c2bb982006-05-16 22:09:29 +1000110 void (*cia_encrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
111 void (*cia_decrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Herbert Xu40725182005-07-06 13:51:52 -0700112
113 unsigned int (*cia_encrypt_ecb)(const struct cipher_desc *desc,
114 u8 *dst, const u8 *src,
115 unsigned int nbytes);
116 unsigned int (*cia_decrypt_ecb)(const struct cipher_desc *desc,
117 u8 *dst, const u8 *src,
118 unsigned int nbytes);
119 unsigned int (*cia_encrypt_cbc)(const struct cipher_desc *desc,
120 u8 *dst, const u8 *src,
121 unsigned int nbytes);
122 unsigned int (*cia_decrypt_cbc)(const struct cipher_desc *desc,
123 u8 *dst, const u8 *src,
124 unsigned int nbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125};
126
127struct digest_alg {
128 unsigned int dia_digestsize;
Herbert Xu6c2bb982006-05-16 22:09:29 +1000129 void (*dia_init)(struct crypto_tfm *tfm);
130 void (*dia_update)(struct crypto_tfm *tfm, const u8 *data,
131 unsigned int len);
132 void (*dia_final)(struct crypto_tfm *tfm, u8 *out);
133 int (*dia_setkey)(struct crypto_tfm *tfm, const u8 *key,
Herbert Xu560c06a2006-08-13 14:16:39 +1000134 unsigned int keylen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135};
136
137struct compress_alg {
Herbert Xu6c2bb982006-05-16 22:09:29 +1000138 int (*coa_compress)(struct crypto_tfm *tfm, const u8 *src,
139 unsigned int slen, u8 *dst, unsigned int *dlen);
140 int (*coa_decompress)(struct crypto_tfm *tfm, const u8 *src,
141 unsigned int slen, u8 *dst, unsigned int *dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142};
143
144#define cra_cipher cra_u.cipher
145#define cra_digest cra_u.digest
146#define cra_compress cra_u.compress
147
148struct crypto_alg {
149 struct list_head cra_list;
Herbert Xu6bfd4802006-09-21 11:39:29 +1000150 struct list_head cra_users;
151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 u32 cra_flags;
153 unsigned int cra_blocksize;
154 unsigned int cra_ctxsize;
Herbert Xu95477372005-07-06 13:52:09 -0700155 unsigned int cra_alignmask;
Herbert Xu5cb14542005-11-05 16:58:14 +1100156
157 int cra_priority;
Herbert Xu6521f302006-08-06 20:28:44 +1000158 atomic_t cra_refcnt;
Herbert Xu5cb14542005-11-05 16:58:14 +1100159
Herbert Xud913ea02006-05-21 08:45:26 +1000160 char cra_name[CRYPTO_MAX_ALG_NAME];
161 char cra_driver_name[CRYPTO_MAX_ALG_NAME];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
163 union {
164 struct cipher_alg cipher;
165 struct digest_alg digest;
166 struct compress_alg compress;
167 } cra_u;
Herbert Xuc7fc0592006-05-24 13:02:26 +1000168
169 int (*cra_init)(struct crypto_tfm *tfm);
170 void (*cra_exit)(struct crypto_tfm *tfm);
Herbert Xu6521f302006-08-06 20:28:44 +1000171 void (*cra_destroy)(struct crypto_alg *alg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173 struct module *cra_module;
174};
175
176/*
177 * Algorithm registration interface.
178 */
179int crypto_register_alg(struct crypto_alg *alg);
180int crypto_unregister_alg(struct crypto_alg *alg);
181
182/*
183 * Algorithm query interface.
184 */
185#ifdef CONFIG_CRYPTO
186int crypto_alg_available(const char *name, u32 flags);
187#else
188static inline int crypto_alg_available(const char *name, u32 flags)
189{
190 return 0;
191}
192#endif
193
194/*
195 * Transforms: user-instantiated objects which encapsulate algorithms
196 * and core processing logic. Managed via crypto_alloc_tfm() and
197 * crypto_free_tfm(), as well as the various helpers below.
198 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
200struct cipher_tfm {
201 void *cit_iv;
202 unsigned int cit_ivsize;
203 u32 cit_mode;
204 int (*cit_setkey)(struct crypto_tfm *tfm,
205 const u8 *key, unsigned int keylen);
206 int (*cit_encrypt)(struct crypto_tfm *tfm,
207 struct scatterlist *dst,
208 struct scatterlist *src,
209 unsigned int nbytes);
210 int (*cit_encrypt_iv)(struct crypto_tfm *tfm,
211 struct scatterlist *dst,
212 struct scatterlist *src,
213 unsigned int nbytes, u8 *iv);
214 int (*cit_decrypt)(struct crypto_tfm *tfm,
215 struct scatterlist *dst,
216 struct scatterlist *src,
217 unsigned int nbytes);
218 int (*cit_decrypt_iv)(struct crypto_tfm *tfm,
219 struct scatterlist *dst,
220 struct scatterlist *src,
221 unsigned int nbytes, u8 *iv);
222 void (*cit_xor_block)(u8 *dst, const u8 *src);
223};
224
225struct digest_tfm {
226 void (*dit_init)(struct crypto_tfm *tfm);
227 void (*dit_update)(struct crypto_tfm *tfm,
228 struct scatterlist *sg, unsigned int nsg);
229 void (*dit_final)(struct crypto_tfm *tfm, u8 *out);
230 void (*dit_digest)(struct crypto_tfm *tfm, struct scatterlist *sg,
231 unsigned int nsg, u8 *out);
232 int (*dit_setkey)(struct crypto_tfm *tfm,
233 const u8 *key, unsigned int keylen);
234#ifdef CONFIG_CRYPTO_HMAC
235 void *dit_hmac_block;
236#endif
237};
238
239struct compress_tfm {
240 int (*cot_compress)(struct crypto_tfm *tfm,
241 const u8 *src, unsigned int slen,
242 u8 *dst, unsigned int *dlen);
243 int (*cot_decompress)(struct crypto_tfm *tfm,
244 const u8 *src, unsigned int slen,
245 u8 *dst, unsigned int *dlen);
246};
247
248#define crt_cipher crt_u.cipher
249#define crt_digest crt_u.digest
250#define crt_compress crt_u.compress
251
252struct crypto_tfm {
253
254 u32 crt_flags;
255
256 union {
257 struct cipher_tfm cipher;
258 struct digest_tfm digest;
259 struct compress_tfm compress;
260 } crt_u;
261
262 struct crypto_alg *__crt_alg;
Herbert Xuf10b7892006-01-25 22:34:01 +1100263
Herbert Xu79911102006-08-21 21:03:52 +1000264 void *__crt_ctx[] CRYPTO_MINALIGN_ATTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265};
266
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000267enum {
268 CRYPTOA_UNSPEC,
269 CRYPTOA_ALG,
270};
271
272struct crypto_attr_alg {
273 char name[CRYPTO_MAX_ALG_NAME];
274};
275
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276/*
277 * Transform user interface.
278 */
279
280/*
281 * crypto_alloc_tfm() will first attempt to locate an already loaded algorithm.
282 * If that fails and the kernel supports dynamically loadable modules, it
283 * will then attempt to load a module of the same name or alias. A refcount
284 * is grabbed on the algorithm which is then associated with the new transform.
285 *
286 * crypto_free_tfm() frees up the transform and any associated resources,
287 * then drops the refcount on the associated algorithm.
288 */
289struct crypto_tfm *crypto_alloc_tfm(const char *alg_name, u32 tfm_flags);
290void crypto_free_tfm(struct crypto_tfm *tfm);
291
292/*
293 * Transform helpers which query the underlying algorithm.
294 */
295static inline const char *crypto_tfm_alg_name(struct crypto_tfm *tfm)
296{
297 return tfm->__crt_alg->cra_name;
298}
299
Michal Ludvigb14cdd62006-07-09 09:02:24 +1000300static inline const char *crypto_tfm_alg_driver_name(struct crypto_tfm *tfm)
301{
302 return tfm->__crt_alg->cra_driver_name;
303}
304
305static inline int crypto_tfm_alg_priority(struct crypto_tfm *tfm)
306{
307 return tfm->__crt_alg->cra_priority;
308}
309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310static inline const char *crypto_tfm_alg_modname(struct crypto_tfm *tfm)
311{
312 return module_name(tfm->__crt_alg->cra_module);
313}
314
315static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm)
316{
317 return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK;
318}
319
320static inline unsigned int crypto_tfm_alg_min_keysize(struct crypto_tfm *tfm)
321{
322 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
323 return tfm->__crt_alg->cra_cipher.cia_min_keysize;
324}
325
326static inline unsigned int crypto_tfm_alg_max_keysize(struct crypto_tfm *tfm)
327{
328 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
329 return tfm->__crt_alg->cra_cipher.cia_max_keysize;
330}
331
332static inline unsigned int crypto_tfm_alg_ivsize(struct crypto_tfm *tfm)
333{
334 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
335 return tfm->crt_cipher.cit_ivsize;
336}
337
338static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm)
339{
340 return tfm->__crt_alg->cra_blocksize;
341}
342
343static inline unsigned int crypto_tfm_alg_digestsize(struct crypto_tfm *tfm)
344{
345 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
346 return tfm->__crt_alg->cra_digest.dia_digestsize;
347}
348
Herbert Xufbdae9f2005-07-06 13:53:29 -0700349static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm *tfm)
350{
351 return tfm->__crt_alg->cra_alignmask;
352}
353
Herbert Xu40725182005-07-06 13:51:52 -0700354static inline void *crypto_tfm_ctx(struct crypto_tfm *tfm)
355{
Herbert Xuf10b7892006-01-25 22:34:01 +1100356 return tfm->__crt_ctx;
357}
358
359static inline unsigned int crypto_tfm_ctx_alignment(void)
360{
361 struct crypto_tfm *tfm;
362 return __alignof__(tfm->__crt_ctx);
Herbert Xu40725182005-07-06 13:51:52 -0700363}
364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365/*
366 * API wrappers.
367 */
368static inline void crypto_digest_init(struct crypto_tfm *tfm)
369{
370 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
371 tfm->crt_digest.dit_init(tfm);
372}
373
374static inline void crypto_digest_update(struct crypto_tfm *tfm,
375 struct scatterlist *sg,
376 unsigned int nsg)
377{
378 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
379 tfm->crt_digest.dit_update(tfm, sg, nsg);
380}
381
382static inline void crypto_digest_final(struct crypto_tfm *tfm, u8 *out)
383{
384 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
385 tfm->crt_digest.dit_final(tfm, out);
386}
387
388static inline void crypto_digest_digest(struct crypto_tfm *tfm,
389 struct scatterlist *sg,
390 unsigned int nsg, u8 *out)
391{
392 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
393 tfm->crt_digest.dit_digest(tfm, sg, nsg, out);
394}
395
396static inline int crypto_digest_setkey(struct crypto_tfm *tfm,
397 const u8 *key, unsigned int keylen)
398{
399 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 return tfm->crt_digest.dit_setkey(tfm, key, keylen);
401}
402
403static inline int crypto_cipher_setkey(struct crypto_tfm *tfm,
404 const u8 *key, unsigned int keylen)
405{
406 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
407 return tfm->crt_cipher.cit_setkey(tfm, key, keylen);
408}
409
410static inline int crypto_cipher_encrypt(struct crypto_tfm *tfm,
411 struct scatterlist *dst,
412 struct scatterlist *src,
413 unsigned int nbytes)
414{
415 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
416 return tfm->crt_cipher.cit_encrypt(tfm, dst, src, nbytes);
417}
418
419static inline int crypto_cipher_encrypt_iv(struct crypto_tfm *tfm,
420 struct scatterlist *dst,
421 struct scatterlist *src,
422 unsigned int nbytes, u8 *iv)
423{
424 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
425 BUG_ON(tfm->crt_cipher.cit_mode == CRYPTO_TFM_MODE_ECB);
426 return tfm->crt_cipher.cit_encrypt_iv(tfm, dst, src, nbytes, iv);
427}
428
429static inline int crypto_cipher_decrypt(struct crypto_tfm *tfm,
430 struct scatterlist *dst,
431 struct scatterlist *src,
432 unsigned int nbytes)
433{
434 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
435 return tfm->crt_cipher.cit_decrypt(tfm, dst, src, nbytes);
436}
437
438static inline int crypto_cipher_decrypt_iv(struct crypto_tfm *tfm,
439 struct scatterlist *dst,
440 struct scatterlist *src,
441 unsigned int nbytes, u8 *iv)
442{
443 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
444 BUG_ON(tfm->crt_cipher.cit_mode == CRYPTO_TFM_MODE_ECB);
445 return tfm->crt_cipher.cit_decrypt_iv(tfm, dst, src, nbytes, iv);
446}
447
448static inline void crypto_cipher_set_iv(struct crypto_tfm *tfm,
449 const u8 *src, unsigned int len)
450{
451 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
452 memcpy(tfm->crt_cipher.cit_iv, src, len);
453}
454
455static inline void crypto_cipher_get_iv(struct crypto_tfm *tfm,
456 u8 *dst, unsigned int len)
457{
458 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
459 memcpy(dst, tfm->crt_cipher.cit_iv, len);
460}
461
462static inline int crypto_comp_compress(struct crypto_tfm *tfm,
463 const u8 *src, unsigned int slen,
464 u8 *dst, unsigned int *dlen)
465{
466 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_COMPRESS);
467 return tfm->crt_compress.cot_compress(tfm, src, slen, dst, dlen);
468}
469
470static inline int crypto_comp_decompress(struct crypto_tfm *tfm,
471 const u8 *src, unsigned int slen,
472 u8 *dst, unsigned int *dlen)
473{
474 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_COMPRESS);
475 return tfm->crt_compress.cot_decompress(tfm, src, slen, dst, dlen);
476}
477
478/*
479 * HMAC support.
480 */
481#ifdef CONFIG_CRYPTO_HMAC
482void crypto_hmac_init(struct crypto_tfm *tfm, u8 *key, unsigned int *keylen);
483void crypto_hmac_update(struct crypto_tfm *tfm,
484 struct scatterlist *sg, unsigned int nsg);
485void crypto_hmac_final(struct crypto_tfm *tfm, u8 *key,
486 unsigned int *keylen, u8 *out);
487void crypto_hmac(struct crypto_tfm *tfm, u8 *key, unsigned int *keylen,
488 struct scatterlist *sg, unsigned int nsg, u8 *out);
489#endif /* CONFIG_CRYPTO_HMAC */
490
491#endif /* _LINUX_CRYPTO_H */
492