blob: ac9d49beecd38247d8e7dfdc63240d7e57b840c3 [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)
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
63struct scatterlist;
Herbert Xu40725182005-07-06 13:51:52 -070064struct crypto_tfm;
65
66struct 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};
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74/*
75 * Algorithms: modular crypto algorithm implementations, managed
76 * via crypto_register_alg() and crypto_unregister_alg().
77 */
78struct 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);
Herbert Xu40725182005-07-06 13:51:52 -070085
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098};
99
100struct 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
109struct 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
122struct crypto_alg {
123 struct list_head cra_list;
124 u32 cra_flags;
125 unsigned int cra_blocksize;
126 unsigned int cra_ctxsize;
Herbert Xu95477372005-07-06 13:52:09 -0700127 unsigned int cra_alignmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 const char cra_name[CRYPTO_MAX_ALG_NAME];
129
130 union {
131 struct cipher_alg cipher;
132 struct digest_alg digest;
133 struct compress_alg compress;
134 } cra_u;
135
136 struct module *cra_module;
137};
138
139/*
140 * Algorithm registration interface.
141 */
142int crypto_register_alg(struct crypto_alg *alg);
143int crypto_unregister_alg(struct crypto_alg *alg);
144
145/*
146 * Algorithm query interface.
147 */
148#ifdef CONFIG_CRYPTO
149int crypto_alg_available(const char *name, u32 flags);
150#else
151static inline int crypto_alg_available(const char *name, u32 flags)
152{
153 return 0;
154}
155#endif
156
157/*
158 * Transforms: user-instantiated objects which encapsulate algorithms
159 * and core processing logic. Managed via crypto_alloc_tfm() and
160 * crypto_free_tfm(), as well as the various helpers below.
161 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
163struct cipher_tfm {
164 void *cit_iv;
165 unsigned int cit_ivsize;
166 u32 cit_mode;
167 int (*cit_setkey)(struct crypto_tfm *tfm,
168 const u8 *key, unsigned int keylen);
169 int (*cit_encrypt)(struct crypto_tfm *tfm,
170 struct scatterlist *dst,
171 struct scatterlist *src,
172 unsigned int nbytes);
173 int (*cit_encrypt_iv)(struct crypto_tfm *tfm,
174 struct scatterlist *dst,
175 struct scatterlist *src,
176 unsigned int nbytes, u8 *iv);
177 int (*cit_decrypt)(struct crypto_tfm *tfm,
178 struct scatterlist *dst,
179 struct scatterlist *src,
180 unsigned int nbytes);
181 int (*cit_decrypt_iv)(struct crypto_tfm *tfm,
182 struct scatterlist *dst,
183 struct scatterlist *src,
184 unsigned int nbytes, u8 *iv);
185 void (*cit_xor_block)(u8 *dst, const u8 *src);
186};
187
188struct digest_tfm {
189 void (*dit_init)(struct crypto_tfm *tfm);
190 void (*dit_update)(struct crypto_tfm *tfm,
191 struct scatterlist *sg, unsigned int nsg);
192 void (*dit_final)(struct crypto_tfm *tfm, u8 *out);
193 void (*dit_digest)(struct crypto_tfm *tfm, struct scatterlist *sg,
194 unsigned int nsg, u8 *out);
195 int (*dit_setkey)(struct crypto_tfm *tfm,
196 const u8 *key, unsigned int keylen);
197#ifdef CONFIG_CRYPTO_HMAC
198 void *dit_hmac_block;
199#endif
200};
201
202struct compress_tfm {
203 int (*cot_compress)(struct crypto_tfm *tfm,
204 const u8 *src, unsigned int slen,
205 u8 *dst, unsigned int *dlen);
206 int (*cot_decompress)(struct crypto_tfm *tfm,
207 const u8 *src, unsigned int slen,
208 u8 *dst, unsigned int *dlen);
209};
210
211#define crt_cipher crt_u.cipher
212#define crt_digest crt_u.digest
213#define crt_compress crt_u.compress
214
215struct crypto_tfm {
216
217 u32 crt_flags;
218
219 union {
220 struct cipher_tfm cipher;
221 struct digest_tfm digest;
222 struct compress_tfm compress;
223 } crt_u;
224
225 struct crypto_alg *__crt_alg;
226};
227
228/*
229 * Transform user interface.
230 */
231
232/*
233 * crypto_alloc_tfm() will first attempt to locate an already loaded algorithm.
234 * If that fails and the kernel supports dynamically loadable modules, it
235 * will then attempt to load a module of the same name or alias. A refcount
236 * is grabbed on the algorithm which is then associated with the new transform.
237 *
238 * crypto_free_tfm() frees up the transform and any associated resources,
239 * then drops the refcount on the associated algorithm.
240 */
241struct crypto_tfm *crypto_alloc_tfm(const char *alg_name, u32 tfm_flags);
242void crypto_free_tfm(struct crypto_tfm *tfm);
243
244/*
245 * Transform helpers which query the underlying algorithm.
246 */
247static inline const char *crypto_tfm_alg_name(struct crypto_tfm *tfm)
248{
249 return tfm->__crt_alg->cra_name;
250}
251
252static inline const char *crypto_tfm_alg_modname(struct crypto_tfm *tfm)
253{
254 return module_name(tfm->__crt_alg->cra_module);
255}
256
257static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm)
258{
259 return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK;
260}
261
262static inline unsigned int crypto_tfm_alg_min_keysize(struct crypto_tfm *tfm)
263{
264 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
265 return tfm->__crt_alg->cra_cipher.cia_min_keysize;
266}
267
268static inline unsigned int crypto_tfm_alg_max_keysize(struct crypto_tfm *tfm)
269{
270 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
271 return tfm->__crt_alg->cra_cipher.cia_max_keysize;
272}
273
274static inline unsigned int crypto_tfm_alg_ivsize(struct crypto_tfm *tfm)
275{
276 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
277 return tfm->crt_cipher.cit_ivsize;
278}
279
280static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm)
281{
282 return tfm->__crt_alg->cra_blocksize;
283}
284
285static inline unsigned int crypto_tfm_alg_digestsize(struct crypto_tfm *tfm)
286{
287 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
288 return tfm->__crt_alg->cra_digest.dia_digestsize;
289}
290
Herbert Xu40725182005-07-06 13:51:52 -0700291static inline void *crypto_tfm_ctx(struct crypto_tfm *tfm)
292{
293 return (void *)&tfm[1];
294}
295
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296/*
297 * API wrappers.
298 */
299static inline void crypto_digest_init(struct crypto_tfm *tfm)
300{
301 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
302 tfm->crt_digest.dit_init(tfm);
303}
304
305static inline void crypto_digest_update(struct crypto_tfm *tfm,
306 struct scatterlist *sg,
307 unsigned int nsg)
308{
309 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
310 tfm->crt_digest.dit_update(tfm, sg, nsg);
311}
312
313static inline void crypto_digest_final(struct crypto_tfm *tfm, u8 *out)
314{
315 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
316 tfm->crt_digest.dit_final(tfm, out);
317}
318
319static inline void crypto_digest_digest(struct crypto_tfm *tfm,
320 struct scatterlist *sg,
321 unsigned int nsg, u8 *out)
322{
323 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
324 tfm->crt_digest.dit_digest(tfm, sg, nsg, out);
325}
326
327static inline int crypto_digest_setkey(struct crypto_tfm *tfm,
328 const u8 *key, unsigned int keylen)
329{
330 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
331 if (tfm->crt_digest.dit_setkey == NULL)
332 return -ENOSYS;
333 return tfm->crt_digest.dit_setkey(tfm, key, keylen);
334}
335
336static inline int crypto_cipher_setkey(struct crypto_tfm *tfm,
337 const u8 *key, unsigned int keylen)
338{
339 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
340 return tfm->crt_cipher.cit_setkey(tfm, key, keylen);
341}
342
343static inline int crypto_cipher_encrypt(struct crypto_tfm *tfm,
344 struct scatterlist *dst,
345 struct scatterlist *src,
346 unsigned int nbytes)
347{
348 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
349 return tfm->crt_cipher.cit_encrypt(tfm, dst, src, nbytes);
350}
351
352static inline int crypto_cipher_encrypt_iv(struct crypto_tfm *tfm,
353 struct scatterlist *dst,
354 struct scatterlist *src,
355 unsigned int nbytes, u8 *iv)
356{
357 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
358 BUG_ON(tfm->crt_cipher.cit_mode == CRYPTO_TFM_MODE_ECB);
359 return tfm->crt_cipher.cit_encrypt_iv(tfm, dst, src, nbytes, iv);
360}
361
362static inline int crypto_cipher_decrypt(struct crypto_tfm *tfm,
363 struct scatterlist *dst,
364 struct scatterlist *src,
365 unsigned int nbytes)
366{
367 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
368 return tfm->crt_cipher.cit_decrypt(tfm, dst, src, nbytes);
369}
370
371static inline int crypto_cipher_decrypt_iv(struct crypto_tfm *tfm,
372 struct scatterlist *dst,
373 struct scatterlist *src,
374 unsigned int nbytes, u8 *iv)
375{
376 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
377 BUG_ON(tfm->crt_cipher.cit_mode == CRYPTO_TFM_MODE_ECB);
378 return tfm->crt_cipher.cit_decrypt_iv(tfm, dst, src, nbytes, iv);
379}
380
381static inline void crypto_cipher_set_iv(struct crypto_tfm *tfm,
382 const u8 *src, unsigned int len)
383{
384 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
385 memcpy(tfm->crt_cipher.cit_iv, src, len);
386}
387
388static inline void crypto_cipher_get_iv(struct crypto_tfm *tfm,
389 u8 *dst, unsigned int len)
390{
391 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
392 memcpy(dst, tfm->crt_cipher.cit_iv, len);
393}
394
395static inline int crypto_comp_compress(struct crypto_tfm *tfm,
396 const u8 *src, unsigned int slen,
397 u8 *dst, unsigned int *dlen)
398{
399 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_COMPRESS);
400 return tfm->crt_compress.cot_compress(tfm, src, slen, dst, dlen);
401}
402
403static inline int crypto_comp_decompress(struct crypto_tfm *tfm,
404 const u8 *src, unsigned int slen,
405 u8 *dst, unsigned int *dlen)
406{
407 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_COMPRESS);
408 return tfm->crt_compress.cot_decompress(tfm, src, slen, dst, dlen);
409}
410
411/*
412 * HMAC support.
413 */
414#ifdef CONFIG_CRYPTO_HMAC
415void crypto_hmac_init(struct crypto_tfm *tfm, u8 *key, unsigned int *keylen);
416void crypto_hmac_update(struct crypto_tfm *tfm,
417 struct scatterlist *sg, unsigned int nsg);
418void crypto_hmac_final(struct crypto_tfm *tfm, u8 *key,
419 unsigned int *keylen, u8 *out);
420void crypto_hmac(struct crypto_tfm *tfm, u8 *key, unsigned int *keylen,
421 struct scatterlist *sg, unsigned int nsg, u8 *out);
422#endif /* CONFIG_CRYPTO_HMAC */
423
424#endif /* _LINUX_CRYPTO_H */
425