2
* Scatterlist Cryptographic API.
4
* Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
5
* Copyright (c) 2002 David S. Miller (davem@redhat.com)
7
* Portions derived from Cryptoapi, by Alexander Kjeldaas <astor@fast.no>
8
* and Nettle, by Niels M鰈ler.
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)
16
#ifndef _LINUX_CRYPTO_H
17
#define _LINUX_CRYPTO_H
19
#include <linux/module.h>
20
#include <linux/kernel.h>
21
#include <linux/types.h>
22
#include <linux/list.h>
23
#include <linux/string.h>
25
#include <asm/errno.h>
27
#ifdef BUILT_IN_CRYPTO
28
#ifdef CONFIG_CRYPTO_HMAC
29
#undef CONFIG_CRYPTO_HMAC
35
#endif /* BUILT_IN_CRYPTO */
37
#define crypto_register_alg crypto_register_alg_rsl
38
#define crypto_unregister_alg crypto_unregister_alg_rsl
39
#define crypto_alloc_tfm crypto_alloc_tfm_rsl
40
#define crypto_free_tfm crypto_free_tfm_rsl
41
#define crypto_alg_available crypto_alg_available_rsl
44
* Algorithm masks and types.
46
#define CRYPTO_ALG_TYPE_MASK 0x000000ff
47
#define CRYPTO_ALG_TYPE_CIPHER 0x00000001
48
#define CRYPTO_ALG_TYPE_DIGEST 0x00000002
49
#define CRYPTO_ALG_TYPE_COMPRESS 0x00000004
52
* Transform masks and values (for crt_flags).
54
#define CRYPTO_TFM_MODE_MASK 0x000000ff
55
#define CRYPTO_TFM_REQ_MASK 0x000fff00
56
#define CRYPTO_TFM_RES_MASK 0xfff00000
58
#define CRYPTO_TFM_MODE_ECB 0x00000001
59
#define CRYPTO_TFM_MODE_CBC 0x00000002
60
#define CRYPTO_TFM_MODE_CFB 0x00000004
61
#define CRYPTO_TFM_MODE_CTR 0x00000008
63
#define CRYPTO_TFM_REQ_WEAK_KEY 0x00000100
64
#define CRYPTO_TFM_RES_WEAK_KEY 0x00100000
65
#define CRYPTO_TFM_RES_BAD_KEY_LEN 0x00200000
66
#define CRYPTO_TFM_RES_BAD_KEY_SCHED 0x00400000
67
#define CRYPTO_TFM_RES_BAD_BLOCK_LEN 0x00800000
68
#define CRYPTO_TFM_RES_BAD_FLAGS 0x01000000
71
* Miscellaneous stuff.
73
#define CRYPTO_UNSPEC 0
74
#define CRYPTO_MAX_ALG_NAME 64
79
* Algorithms: modular crypto algorithm implementations, managed
80
* via crypto_register_alg() and crypto_unregister_alg().
83
unsigned int cia_min_keysize;
84
unsigned int cia_max_keysize;
85
int (*cia_setkey)(void *ctx, const u8 *key,
86
unsigned int keylen, u32 *flags);
87
void (*cia_encrypt)(void *ctx, u8 *dst, const u8 *src);
88
void (*cia_decrypt)(void *ctx, u8 *dst, const u8 *src);
92
unsigned int dia_digestsize;
93
void (*dia_init)(void *ctx);
94
void (*dia_update)(void *ctx, const u8 *data, unsigned int len);
95
void (*dia_final)(void *ctx, u8 *out);
96
int (*dia_setkey)(void *ctx, const u8 *key,
97
unsigned int keylen, u32 *flags);
100
struct compress_alg {
101
int (*coa_init)(void *ctx);
102
void (*coa_exit)(void *ctx);
103
int (*coa_compress)(void *ctx, const u8 *src, unsigned int slen,
104
u8 *dst, unsigned int *dlen);
105
int (*coa_decompress)(void *ctx, const u8 *src, unsigned int slen,
106
u8 *dst, unsigned int *dlen);
109
#define cra_cipher cra_u.cipher
110
#define cra_digest cra_u.digest
111
#define cra_compress cra_u.compress
114
struct list_head cra_list;
116
unsigned int cra_blocksize;
117
unsigned int cra_ctxsize;
118
const char cra_name[CRYPTO_MAX_ALG_NAME];
121
struct cipher_alg cipher;
122
struct digest_alg digest;
123
struct compress_alg compress;
126
struct module *cra_module;
130
* Algorithm registration interface.
132
int crypto_register_alg(struct crypto_alg *alg);
133
int crypto_unregister_alg(struct crypto_alg *alg);
136
* Algorithm query interface.
138
int crypto_alg_available(const char *name, u32 flags);
141
* Transforms: user-instantiated objects which encapsulate algorithms
142
* and core processing logic. Managed via crypto_alloc_tfm() and
143
* crypto_free_tfm(), as well as the various helpers below.
149
unsigned int cit_ivsize;
151
int (*cit_setkey)(struct crypto_tfm *tfm,
152
const u8 *key, unsigned int keylen);
153
int (*cit_encrypt)(struct crypto_tfm *tfm,
154
struct scatterlist *dst,
155
struct scatterlist *src,
156
unsigned int nbytes);
157
int (*cit_encrypt_iv)(struct crypto_tfm *tfm,
158
struct scatterlist *dst,
159
struct scatterlist *src,
160
unsigned int nbytes, u8 *iv);
161
int (*cit_decrypt)(struct crypto_tfm *tfm,
162
struct scatterlist *dst,
163
struct scatterlist *src,
164
unsigned int nbytes);
165
int (*cit_decrypt_iv)(struct crypto_tfm *tfm,
166
struct scatterlist *dst,
167
struct scatterlist *src,
168
unsigned int nbytes, u8 *iv);
169
void (*cit_xor_block)(u8 *dst, const u8 *src);
173
void (*dit_init)(struct crypto_tfm *tfm);
174
void (*dit_update)(struct crypto_tfm *tfm,
175
struct scatterlist *sg, unsigned int nsg);
176
void (*dit_final)(struct crypto_tfm *tfm, u8 *out);
177
void (*dit_digest)(struct crypto_tfm *tfm, struct scatterlist *sg,
178
unsigned int nsg, u8 *out);
179
int (*dit_setkey)(struct crypto_tfm *tfm,
180
const u8 *key, unsigned int keylen);
181
#ifdef CONFIG_CRYPTO_HMAC
182
void *dit_hmac_block;
186
struct compress_tfm {
187
int (*cot_compress)(struct crypto_tfm *tfm,
188
const u8 *src, unsigned int slen,
189
u8 *dst, unsigned int *dlen);
190
int (*cot_decompress)(struct crypto_tfm *tfm,
191
const u8 *src, unsigned int slen,
192
u8 *dst, unsigned int *dlen);
195
#define crt_cipher crt_u.cipher
196
#define crt_digest crt_u.digest
197
#define crt_compress crt_u.compress
204
struct cipher_tfm cipher;
205
struct digest_tfm digest;
206
struct compress_tfm compress;
209
struct crypto_alg *__crt_alg;
213
* Transform user interface.
217
* crypto_alloc_tfm() will first attempt to locate an already loaded algorithm.
218
* If that fails and the kernel supports dynamically loadable modules, it
219
* will then attempt to load a module of the same name or alias. A refcount
220
* is grabbed on the algorithm which is then associated with the new transform.
222
* crypto_free_tfm() frees up the transform and any associated resources,
223
* then drops the refcount on the associated algorithm.
225
struct crypto_tfm *crypto_alloc_tfm(const char *alg_name, u32 tfm_flags);
226
void crypto_free_tfm(struct crypto_tfm *tfm);
229
* Transform helpers which query the underlying algorithm.
231
static inline const char *crypto_tfm_alg_name(struct crypto_tfm *tfm)
233
return tfm->__crt_alg->cra_name;
236
static inline const char *crypto_tfm_alg_modname(struct crypto_tfm *tfm)
238
struct crypto_alg *alg = tfm->__crt_alg;
241
return alg->cra_module->name;
246
static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm)
248
return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK;
251
static inline unsigned int crypto_tfm_alg_min_keysize(struct crypto_tfm *tfm)
253
BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
254
return tfm->__crt_alg->cra_cipher.cia_min_keysize;
257
static inline unsigned int crypto_tfm_alg_max_keysize(struct crypto_tfm *tfm)
259
BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
260
return tfm->__crt_alg->cra_cipher.cia_max_keysize;
263
static inline unsigned int crypto_tfm_alg_ivsize(struct crypto_tfm *tfm)
265
BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
266
return tfm->crt_cipher.cit_ivsize;
269
static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm)
271
return tfm->__crt_alg->cra_blocksize;
274
static inline unsigned int crypto_tfm_alg_digestsize(struct crypto_tfm *tfm)
276
BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
277
return tfm->__crt_alg->cra_digest.dia_digestsize;
283
static inline void crypto_digest_init(struct crypto_tfm *tfm)
285
BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
286
tfm->crt_digest.dit_init(tfm);
289
static inline void crypto_digest_update(struct crypto_tfm *tfm,
290
struct scatterlist *sg,
293
BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
294
tfm->crt_digest.dit_update(tfm, sg, nsg);
297
static inline void crypto_digest_final(struct crypto_tfm *tfm, u8 *out)
299
BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
300
tfm->crt_digest.dit_final(tfm, out);
303
static inline void crypto_digest_digest(struct crypto_tfm *tfm,
304
struct scatterlist *sg,
305
unsigned int nsg, u8 *out)
307
BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
308
tfm->crt_digest.dit_digest(tfm, sg, nsg, out);
311
static inline int crypto_digest_setkey(struct crypto_tfm *tfm,
312
const u8 *key, unsigned int keylen)
314
BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
315
if (tfm->crt_digest.dit_setkey == NULL)
317
return tfm->crt_digest.dit_setkey(tfm, key, keylen);
320
static inline int crypto_cipher_setkey(struct crypto_tfm *tfm,
321
const u8 *key, unsigned int keylen)
323
BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
324
return tfm->crt_cipher.cit_setkey(tfm, key, keylen);
327
static inline int crypto_cipher_encrypt(struct crypto_tfm *tfm,
328
struct scatterlist *dst,
329
struct scatterlist *src,
332
BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
333
return tfm->crt_cipher.cit_encrypt(tfm, dst, src, nbytes);
336
static inline int crypto_cipher_encrypt_iv(struct crypto_tfm *tfm,
337
struct scatterlist *dst,
338
struct scatterlist *src,
339
unsigned int nbytes, u8 *iv)
341
BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
342
BUG_ON(tfm->crt_cipher.cit_mode == CRYPTO_TFM_MODE_ECB);
343
return tfm->crt_cipher.cit_encrypt_iv(tfm, dst, src, nbytes, iv);
346
static inline int crypto_cipher_decrypt(struct crypto_tfm *tfm,
347
struct scatterlist *dst,
348
struct scatterlist *src,
351
BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
352
return tfm->crt_cipher.cit_decrypt(tfm, dst, src, nbytes);
355
static inline int crypto_cipher_decrypt_iv(struct crypto_tfm *tfm,
356
struct scatterlist *dst,
357
struct scatterlist *src,
358
unsigned int nbytes, u8 *iv)
360
BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
361
BUG_ON(tfm->crt_cipher.cit_mode == CRYPTO_TFM_MODE_ECB);
362
return tfm->crt_cipher.cit_decrypt_iv(tfm, dst, src, nbytes, iv);
365
static inline void crypto_cipher_set_iv(struct crypto_tfm *tfm,
366
const u8 *src, unsigned int len)
368
BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
369
memcpy(tfm->crt_cipher.cit_iv, src, len);
372
static inline void crypto_cipher_get_iv(struct crypto_tfm *tfm,
373
u8 *dst, unsigned int len)
375
BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
376
memcpy(dst, tfm->crt_cipher.cit_iv, len);
379
static inline int crypto_comp_compress(struct crypto_tfm *tfm,
380
const u8 *src, unsigned int slen,
381
u8 *dst, unsigned int *dlen)
383
BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_COMPRESS);
384
return tfm->crt_compress.cot_compress(tfm, src, slen, dst, dlen);
387
static inline int crypto_comp_decompress(struct crypto_tfm *tfm,
388
const u8 *src, unsigned int slen,
389
u8 *dst, unsigned int *dlen)
391
BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_COMPRESS);
392
return tfm->crt_compress.cot_decompress(tfm, src, slen, dst, dlen);
398
#ifdef CONFIG_CRYPTO_HMAC
399
void crypto_hmac_init(struct crypto_tfm *tfm, u8 *key, unsigned int *keylen);
400
void crypto_hmac_update(struct crypto_tfm *tfm,
401
struct scatterlist *sg, unsigned int nsg);
402
void crypto_hmac_final(struct crypto_tfm *tfm, u8 *key,
403
unsigned int *keylen, u8 *out);
404
void crypto_hmac(struct crypto_tfm *tfm, u8 *key, unsigned int *keylen,
405
struct scatterlist *sg, unsigned int nsg, u8 *out);
406
#endif /* CONFIG_CRYPTO_HMAC */
408
#endif /* _LINUX_CRYPTO_H */