blob: 6847ab0ea30e15854c944b358d87f2bc393ebd67 [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 Xuf3f632d2006-08-06 23:12:59 +100040#define CRYPTO_ALG_ASYNC 0x00000080
Herbert Xu28259822006-08-06 21:23:26 +100041
Linus Torvalds1da177e2005-04-16 15:20:36 -070042/*
43 * Transform masks and values (for crt_flags).
44 */
45#define CRYPTO_TFM_MODE_MASK 0x000000ff
46#define CRYPTO_TFM_REQ_MASK 0x000fff00
47#define CRYPTO_TFM_RES_MASK 0xfff00000
48
49#define CRYPTO_TFM_MODE_ECB 0x00000001
50#define CRYPTO_TFM_MODE_CBC 0x00000002
51#define CRYPTO_TFM_MODE_CFB 0x00000004
52#define CRYPTO_TFM_MODE_CTR 0x00000008
53
54#define CRYPTO_TFM_REQ_WEAK_KEY 0x00000100
Herbert Xu64baf3c2005-09-01 17:43:05 -070055#define CRYPTO_TFM_REQ_MAY_SLEEP 0x00000200
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#define CRYPTO_TFM_RES_WEAK_KEY 0x00100000
57#define CRYPTO_TFM_RES_BAD_KEY_LEN 0x00200000
58#define CRYPTO_TFM_RES_BAD_KEY_SCHED 0x00400000
59#define CRYPTO_TFM_RES_BAD_BLOCK_LEN 0x00800000
60#define CRYPTO_TFM_RES_BAD_FLAGS 0x01000000
61
62/*
63 * Miscellaneous stuff.
64 */
65#define CRYPTO_UNSPEC 0
66#define CRYPTO_MAX_ALG_NAME 64
67
68#define CRYPTO_DIR_ENCRYPT 1
69#define CRYPTO_DIR_DECRYPT 0
70
Herbert Xu79911102006-08-21 21:03:52 +100071/*
72 * The macro CRYPTO_MINALIGN_ATTR (along with the void * type in the actual
73 * declaration) is used to ensure that the crypto_tfm context structure is
74 * aligned correctly for the given architecture so that there are no alignment
75 * faults for C data types. In particular, this is required on platforms such
76 * as arm where pointers are 32-bit aligned but there are data types such as
77 * u64 which require 64-bit alignment.
78 */
79#if defined(ARCH_KMALLOC_MINALIGN)
80#define CRYPTO_MINALIGN ARCH_KMALLOC_MINALIGN
81#elif defined(ARCH_SLAB_MINALIGN)
82#define CRYPTO_MINALIGN ARCH_SLAB_MINALIGN
83#endif
84
85#ifdef CRYPTO_MINALIGN
86#define CRYPTO_MINALIGN_ATTR __attribute__ ((__aligned__(CRYPTO_MINALIGN)))
87#else
88#define CRYPTO_MINALIGN_ATTR
89#endif
90
Linus Torvalds1da177e2005-04-16 15:20:36 -070091struct scatterlist;
Herbert Xu40725182005-07-06 13:51:52 -070092struct crypto_tfm;
93
94struct cipher_desc {
95 struct crypto_tfm *tfm;
Herbert Xu6c2bb982006-05-16 22:09:29 +100096 void (*crfn)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Herbert Xu40725182005-07-06 13:51:52 -070097 unsigned int (*prfn)(const struct cipher_desc *desc, u8 *dst,
98 const u8 *src, unsigned int nbytes);
99 void *info;
100};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
102/*
103 * Algorithms: modular crypto algorithm implementations, managed
104 * via crypto_register_alg() and crypto_unregister_alg().
105 */
106struct cipher_alg {
107 unsigned int cia_min_keysize;
108 unsigned int cia_max_keysize;
Herbert Xu6c2bb982006-05-16 22:09:29 +1000109 int (*cia_setkey)(struct crypto_tfm *tfm, const u8 *key,
Herbert Xu560c06a2006-08-13 14:16:39 +1000110 unsigned int keylen);
Herbert Xu6c2bb982006-05-16 22:09:29 +1000111 void (*cia_encrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
112 void (*cia_decrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Herbert Xu40725182005-07-06 13:51:52 -0700113
114 unsigned int (*cia_encrypt_ecb)(const struct cipher_desc *desc,
115 u8 *dst, const u8 *src,
116 unsigned int nbytes);
117 unsigned int (*cia_decrypt_ecb)(const struct cipher_desc *desc,
118 u8 *dst, const u8 *src,
119 unsigned int nbytes);
120 unsigned int (*cia_encrypt_cbc)(const struct cipher_desc *desc,
121 u8 *dst, const u8 *src,
122 unsigned int nbytes);
123 unsigned int (*cia_decrypt_cbc)(const struct cipher_desc *desc,
124 u8 *dst, const u8 *src,
125 unsigned int nbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126};
127
128struct digest_alg {
129 unsigned int dia_digestsize;
Herbert Xu6c2bb982006-05-16 22:09:29 +1000130 void (*dia_init)(struct crypto_tfm *tfm);
131 void (*dia_update)(struct crypto_tfm *tfm, const u8 *data,
132 unsigned int len);
133 void (*dia_final)(struct crypto_tfm *tfm, u8 *out);
134 int (*dia_setkey)(struct crypto_tfm *tfm, const u8 *key,
Herbert Xu560c06a2006-08-13 14:16:39 +1000135 unsigned int keylen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136};
137
138struct compress_alg {
Herbert Xu6c2bb982006-05-16 22:09:29 +1000139 int (*coa_compress)(struct crypto_tfm *tfm, const u8 *src,
140 unsigned int slen, u8 *dst, unsigned int *dlen);
141 int (*coa_decompress)(struct crypto_tfm *tfm, const u8 *src,
142 unsigned int slen, u8 *dst, unsigned int *dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143};
144
145#define cra_cipher cra_u.cipher
146#define cra_digest cra_u.digest
147#define cra_compress cra_u.compress
148
149struct crypto_alg {
150 struct list_head cra_list;
Herbert Xu6bfd4802006-09-21 11:39:29 +1000151 struct list_head cra_users;
152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 u32 cra_flags;
154 unsigned int cra_blocksize;
155 unsigned int cra_ctxsize;
Herbert Xu95477372005-07-06 13:52:09 -0700156 unsigned int cra_alignmask;
Herbert Xu5cb14542005-11-05 16:58:14 +1100157
158 int cra_priority;
Herbert Xu6521f302006-08-06 20:28:44 +1000159 atomic_t cra_refcnt;
Herbert Xu5cb14542005-11-05 16:58:14 +1100160
Herbert Xud913ea02006-05-21 08:45:26 +1000161 char cra_name[CRYPTO_MAX_ALG_NAME];
162 char cra_driver_name[CRYPTO_MAX_ALG_NAME];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
164 union {
165 struct cipher_alg cipher;
166 struct digest_alg digest;
167 struct compress_alg compress;
168 } cra_u;
Herbert Xuc7fc0592006-05-24 13:02:26 +1000169
170 int (*cra_init)(struct crypto_tfm *tfm);
171 void (*cra_exit)(struct crypto_tfm *tfm);
Herbert Xu6521f302006-08-06 20:28:44 +1000172 void (*cra_destroy)(struct crypto_alg *alg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174 struct module *cra_module;
175};
176
177/*
178 * Algorithm registration interface.
179 */
180int crypto_register_alg(struct crypto_alg *alg);
181int crypto_unregister_alg(struct crypto_alg *alg);
182
183/*
184 * Algorithm query interface.
185 */
186#ifdef CONFIG_CRYPTO
187int crypto_alg_available(const char *name, u32 flags);
188#else
189static inline int crypto_alg_available(const char *name, u32 flags)
190{
191 return 0;
192}
193#endif
194
195/*
196 * Transforms: user-instantiated objects which encapsulate algorithms
Herbert Xu6d7d6842006-07-30 11:53:01 +1000197 * and core processing logic. Managed via crypto_alloc_*() and
198 * crypto_free_*(), as well as the various helpers below.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
201struct cipher_tfm {
202 void *cit_iv;
203 unsigned int cit_ivsize;
204 u32 cit_mode;
205 int (*cit_setkey)(struct crypto_tfm *tfm,
206 const u8 *key, unsigned int keylen);
207 int (*cit_encrypt)(struct crypto_tfm *tfm,
208 struct scatterlist *dst,
209 struct scatterlist *src,
210 unsigned int nbytes);
211 int (*cit_encrypt_iv)(struct crypto_tfm *tfm,
212 struct scatterlist *dst,
213 struct scatterlist *src,
214 unsigned int nbytes, u8 *iv);
215 int (*cit_decrypt)(struct crypto_tfm *tfm,
216 struct scatterlist *dst,
217 struct scatterlist *src,
218 unsigned int nbytes);
219 int (*cit_decrypt_iv)(struct crypto_tfm *tfm,
220 struct scatterlist *dst,
221 struct scatterlist *src,
222 unsigned int nbytes, u8 *iv);
223 void (*cit_xor_block)(u8 *dst, const u8 *src);
224};
225
226struct digest_tfm {
227 void (*dit_init)(struct crypto_tfm *tfm);
228 void (*dit_update)(struct crypto_tfm *tfm,
229 struct scatterlist *sg, unsigned int nsg);
230 void (*dit_final)(struct crypto_tfm *tfm, u8 *out);
231 void (*dit_digest)(struct crypto_tfm *tfm, struct scatterlist *sg,
232 unsigned int nsg, u8 *out);
233 int (*dit_setkey)(struct crypto_tfm *tfm,
234 const u8 *key, unsigned int keylen);
235#ifdef CONFIG_CRYPTO_HMAC
236 void *dit_hmac_block;
237#endif
238};
239
240struct compress_tfm {
241 int (*cot_compress)(struct crypto_tfm *tfm,
242 const u8 *src, unsigned int slen,
243 u8 *dst, unsigned int *dlen);
244 int (*cot_decompress)(struct crypto_tfm *tfm,
245 const u8 *src, unsigned int slen,
246 u8 *dst, unsigned int *dlen);
247};
248
249#define crt_cipher crt_u.cipher
250#define crt_digest crt_u.digest
251#define crt_compress crt_u.compress
252
253struct crypto_tfm {
254
255 u32 crt_flags;
256
257 union {
258 struct cipher_tfm cipher;
259 struct digest_tfm digest;
260 struct compress_tfm compress;
261 } crt_u;
262
263 struct crypto_alg *__crt_alg;
Herbert Xuf10b7892006-01-25 22:34:01 +1100264
Herbert Xu79911102006-08-21 21:03:52 +1000265 void *__crt_ctx[] CRYPTO_MINALIGN_ATTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266};
267
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000268enum {
269 CRYPTOA_UNSPEC,
270 CRYPTOA_ALG,
271};
272
273struct crypto_attr_alg {
274 char name[CRYPTO_MAX_ALG_NAME];
275};
276
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277/*
278 * Transform user interface.
279 */
280
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281struct crypto_tfm *crypto_alloc_tfm(const char *alg_name, u32 tfm_flags);
Herbert Xu6d7d6842006-07-30 11:53:01 +1000282struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283void crypto_free_tfm(struct crypto_tfm *tfm);
284
285/*
286 * Transform helpers which query the underlying algorithm.
287 */
288static inline const char *crypto_tfm_alg_name(struct crypto_tfm *tfm)
289{
290 return tfm->__crt_alg->cra_name;
291}
292
Michal Ludvigb14cdd62006-07-09 09:02:24 +1000293static inline const char *crypto_tfm_alg_driver_name(struct crypto_tfm *tfm)
294{
295 return tfm->__crt_alg->cra_driver_name;
296}
297
298static inline int crypto_tfm_alg_priority(struct crypto_tfm *tfm)
299{
300 return tfm->__crt_alg->cra_priority;
301}
302
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303static inline const char *crypto_tfm_alg_modname(struct crypto_tfm *tfm)
304{
305 return module_name(tfm->__crt_alg->cra_module);
306}
307
308static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm)
309{
310 return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK;
311}
312
313static inline unsigned int crypto_tfm_alg_min_keysize(struct crypto_tfm *tfm)
314{
315 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
316 return tfm->__crt_alg->cra_cipher.cia_min_keysize;
317}
318
319static inline unsigned int crypto_tfm_alg_max_keysize(struct crypto_tfm *tfm)
320{
321 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
322 return tfm->__crt_alg->cra_cipher.cia_max_keysize;
323}
324
325static inline unsigned int crypto_tfm_alg_ivsize(struct crypto_tfm *tfm)
326{
327 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
328 return tfm->crt_cipher.cit_ivsize;
329}
330
331static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm)
332{
333 return tfm->__crt_alg->cra_blocksize;
334}
335
336static inline unsigned int crypto_tfm_alg_digestsize(struct crypto_tfm *tfm)
337{
338 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
339 return tfm->__crt_alg->cra_digest.dia_digestsize;
340}
341
Herbert Xufbdae9f2005-07-06 13:53:29 -0700342static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm *tfm)
343{
344 return tfm->__crt_alg->cra_alignmask;
345}
346
Herbert Xu40725182005-07-06 13:51:52 -0700347static inline void *crypto_tfm_ctx(struct crypto_tfm *tfm)
348{
Herbert Xuf10b7892006-01-25 22:34:01 +1100349 return tfm->__crt_ctx;
350}
351
352static inline unsigned int crypto_tfm_ctx_alignment(void)
353{
354 struct crypto_tfm *tfm;
355 return __alignof__(tfm->__crt_ctx);
Herbert Xu40725182005-07-06 13:51:52 -0700356}
357
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358/*
359 * API wrappers.
360 */
361static inline void crypto_digest_init(struct crypto_tfm *tfm)
362{
363 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
364 tfm->crt_digest.dit_init(tfm);
365}
366
367static inline void crypto_digest_update(struct crypto_tfm *tfm,
368 struct scatterlist *sg,
369 unsigned int nsg)
370{
371 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
372 tfm->crt_digest.dit_update(tfm, sg, nsg);
373}
374
375static inline void crypto_digest_final(struct crypto_tfm *tfm, u8 *out)
376{
377 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
378 tfm->crt_digest.dit_final(tfm, out);
379}
380
381static inline void crypto_digest_digest(struct crypto_tfm *tfm,
382 struct scatterlist *sg,
383 unsigned int nsg, u8 *out)
384{
385 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
386 tfm->crt_digest.dit_digest(tfm, sg, nsg, out);
387}
388
389static inline int crypto_digest_setkey(struct crypto_tfm *tfm,
390 const u8 *key, unsigned int keylen)
391{
392 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 return tfm->crt_digest.dit_setkey(tfm, key, keylen);
394}
395
396static inline int crypto_cipher_setkey(struct crypto_tfm *tfm,
397 const u8 *key, unsigned int keylen)
398{
399 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
400 return tfm->crt_cipher.cit_setkey(tfm, key, keylen);
401}
402
403static inline int crypto_cipher_encrypt(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_encrypt(tfm, dst, src, nbytes);
410}
411
412static inline int crypto_cipher_encrypt_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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 return tfm->crt_cipher.cit_encrypt_iv(tfm, dst, src, nbytes, iv);
419}
420
421static inline int crypto_cipher_decrypt(struct crypto_tfm *tfm,
422 struct scatterlist *dst,
423 struct scatterlist *src,
424 unsigned int nbytes)
425{
426 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
427 return tfm->crt_cipher.cit_decrypt(tfm, dst, src, nbytes);
428}
429
430static inline int crypto_cipher_decrypt_iv(struct crypto_tfm *tfm,
431 struct scatterlist *dst,
432 struct scatterlist *src,
433 unsigned int nbytes, u8 *iv)
434{
435 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 return tfm->crt_cipher.cit_decrypt_iv(tfm, dst, src, nbytes, iv);
437}
438
439static inline void crypto_cipher_set_iv(struct crypto_tfm *tfm,
440 const u8 *src, unsigned int len)
441{
442 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
443 memcpy(tfm->crt_cipher.cit_iv, src, len);
444}
445
446static inline void crypto_cipher_get_iv(struct crypto_tfm *tfm,
447 u8 *dst, unsigned int len)
448{
449 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
450 memcpy(dst, tfm->crt_cipher.cit_iv, len);
451}
452
453static inline int crypto_comp_compress(struct crypto_tfm *tfm,
454 const u8 *src, unsigned int slen,
455 u8 *dst, unsigned int *dlen)
456{
457 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_COMPRESS);
458 return tfm->crt_compress.cot_compress(tfm, src, slen, dst, dlen);
459}
460
461static inline int crypto_comp_decompress(struct crypto_tfm *tfm,
462 const u8 *src, unsigned int slen,
463 u8 *dst, unsigned int *dlen)
464{
465 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_COMPRESS);
466 return tfm->crt_compress.cot_decompress(tfm, src, slen, dst, dlen);
467}
468
469/*
470 * HMAC support.
471 */
472#ifdef CONFIG_CRYPTO_HMAC
473void crypto_hmac_init(struct crypto_tfm *tfm, u8 *key, unsigned int *keylen);
474void crypto_hmac_update(struct crypto_tfm *tfm,
475 struct scatterlist *sg, unsigned int nsg);
476void crypto_hmac_final(struct crypto_tfm *tfm, u8 *key,
477 unsigned int *keylen, u8 *out);
478void crypto_hmac(struct crypto_tfm *tfm, u8 *key, unsigned int *keylen,
479 struct scatterlist *sg, unsigned int nsg, u8 *out);
480#endif /* CONFIG_CRYPTO_HMAC */
481
482#endif /* _LINUX_CRYPTO_H */
483