blob: cf91c4c0638bb757c190bd3b72ddcc2da2dff1ca [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/list.h>
Herbert Xu79911102006-08-21 21:03:52 +100024#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/string.h>
Herbert Xu79911102006-08-21 21:03:52 +100026#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
28/*
29 * Algorithm masks and types.
30 */
Herbert Xu28259822006-08-06 21:23:26 +100031#define CRYPTO_ALG_TYPE_MASK 0x0000000f
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#define CRYPTO_ALG_TYPE_CIPHER 0x00000001
33#define CRYPTO_ALG_TYPE_DIGEST 0x00000002
Herbert Xu055bcee2006-08-19 22:24:23 +100034#define CRYPTO_ALG_TYPE_HASH 0x00000003
35#define CRYPTO_ALG_TYPE_BLKCIPHER 0x00000004
36#define CRYPTO_ALG_TYPE_COMPRESS 0x00000005
37
38#define CRYPTO_ALG_TYPE_HASH_MASK 0x0000000e
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Herbert Xu28259822006-08-06 21:23:26 +100040#define CRYPTO_ALG_LARVAL 0x00000010
Herbert Xu6bfd4802006-09-21 11:39:29 +100041#define CRYPTO_ALG_DEAD 0x00000020
42#define CRYPTO_ALG_DYING 0x00000040
Herbert Xuf3f632d2006-08-06 23:12:59 +100043#define CRYPTO_ALG_ASYNC 0x00000080
Herbert Xu28259822006-08-06 21:23:26 +100044
Linus Torvalds1da177e2005-04-16 15:20:36 -070045/*
46 * Transform masks and values (for crt_flags).
47 */
48#define CRYPTO_TFM_MODE_MASK 0x000000ff
49#define CRYPTO_TFM_REQ_MASK 0x000fff00
50#define CRYPTO_TFM_RES_MASK 0xfff00000
51
52#define CRYPTO_TFM_MODE_ECB 0x00000001
53#define CRYPTO_TFM_MODE_CBC 0x00000002
54#define CRYPTO_TFM_MODE_CFB 0x00000004
55#define CRYPTO_TFM_MODE_CTR 0x00000008
56
57#define CRYPTO_TFM_REQ_WEAK_KEY 0x00000100
Herbert Xu64baf3c2005-09-01 17:43:05 -070058#define CRYPTO_TFM_REQ_MAY_SLEEP 0x00000200
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#define CRYPTO_TFM_RES_WEAK_KEY 0x00100000
60#define CRYPTO_TFM_RES_BAD_KEY_LEN 0x00200000
61#define CRYPTO_TFM_RES_BAD_KEY_SCHED 0x00400000
62#define CRYPTO_TFM_RES_BAD_BLOCK_LEN 0x00800000
63#define CRYPTO_TFM_RES_BAD_FLAGS 0x01000000
64
65/*
66 * Miscellaneous stuff.
67 */
68#define CRYPTO_UNSPEC 0
69#define CRYPTO_MAX_ALG_NAME 64
70
71#define CRYPTO_DIR_ENCRYPT 1
72#define CRYPTO_DIR_DECRYPT 0
73
Herbert Xu79911102006-08-21 21:03:52 +100074/*
75 * The macro CRYPTO_MINALIGN_ATTR (along with the void * type in the actual
76 * declaration) is used to ensure that the crypto_tfm context structure is
77 * aligned correctly for the given architecture so that there are no alignment
78 * faults for C data types. In particular, this is required on platforms such
79 * as arm where pointers are 32-bit aligned but there are data types such as
80 * u64 which require 64-bit alignment.
81 */
82#if defined(ARCH_KMALLOC_MINALIGN)
83#define CRYPTO_MINALIGN ARCH_KMALLOC_MINALIGN
84#elif defined(ARCH_SLAB_MINALIGN)
85#define CRYPTO_MINALIGN ARCH_SLAB_MINALIGN
86#endif
87
88#ifdef CRYPTO_MINALIGN
89#define CRYPTO_MINALIGN_ATTR __attribute__ ((__aligned__(CRYPTO_MINALIGN)))
90#else
91#define CRYPTO_MINALIGN_ATTR
92#endif
93
Linus Torvalds1da177e2005-04-16 15:20:36 -070094struct scatterlist;
Herbert Xu5cde0af2006-08-22 00:07:53 +100095struct crypto_blkcipher;
Herbert Xu055bcee2006-08-19 22:24:23 +100096struct crypto_hash;
Herbert Xu40725182005-07-06 13:51:52 -070097struct crypto_tfm;
Herbert Xue853c3c2006-08-22 00:06:54 +100098struct crypto_type;
Herbert Xu40725182005-07-06 13:51:52 -070099
Herbert Xu5cde0af2006-08-22 00:07:53 +1000100struct blkcipher_desc {
101 struct crypto_blkcipher *tfm;
102 void *info;
103 u32 flags;
104};
105
Herbert Xu40725182005-07-06 13:51:52 -0700106struct cipher_desc {
107 struct crypto_tfm *tfm;
Herbert Xu6c2bb982006-05-16 22:09:29 +1000108 void (*crfn)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Herbert Xu40725182005-07-06 13:51:52 -0700109 unsigned int (*prfn)(const struct cipher_desc *desc, u8 *dst,
110 const u8 *src, unsigned int nbytes);
111 void *info;
112};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Herbert Xu055bcee2006-08-19 22:24:23 +1000114struct hash_desc {
115 struct crypto_hash *tfm;
116 u32 flags;
117};
118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119/*
120 * Algorithms: modular crypto algorithm implementations, managed
121 * via crypto_register_alg() and crypto_unregister_alg().
122 */
Herbert Xu5cde0af2006-08-22 00:07:53 +1000123struct blkcipher_alg {
124 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
125 unsigned int keylen);
126 int (*encrypt)(struct blkcipher_desc *desc,
127 struct scatterlist *dst, struct scatterlist *src,
128 unsigned int nbytes);
129 int (*decrypt)(struct blkcipher_desc *desc,
130 struct scatterlist *dst, struct scatterlist *src,
131 unsigned int nbytes);
132
133 unsigned int min_keysize;
134 unsigned int max_keysize;
135 unsigned int ivsize;
136};
137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138struct cipher_alg {
139 unsigned int cia_min_keysize;
140 unsigned int cia_max_keysize;
Herbert Xu6c2bb982006-05-16 22:09:29 +1000141 int (*cia_setkey)(struct crypto_tfm *tfm, const u8 *key,
Herbert Xu560c06a2006-08-13 14:16:39 +1000142 unsigned int keylen);
Herbert Xu6c2bb982006-05-16 22:09:29 +1000143 void (*cia_encrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
144 void (*cia_decrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Herbert Xu40725182005-07-06 13:51:52 -0700145
146 unsigned int (*cia_encrypt_ecb)(const struct cipher_desc *desc,
147 u8 *dst, const u8 *src,
Herbert Xu7226bc872006-08-21 21:40:49 +1000148 unsigned int nbytes) __deprecated;
Herbert Xu40725182005-07-06 13:51:52 -0700149 unsigned int (*cia_decrypt_ecb)(const struct cipher_desc *desc,
150 u8 *dst, const u8 *src,
Herbert Xu7226bc872006-08-21 21:40:49 +1000151 unsigned int nbytes) __deprecated;
Herbert Xu40725182005-07-06 13:51:52 -0700152 unsigned int (*cia_encrypt_cbc)(const struct cipher_desc *desc,
153 u8 *dst, const u8 *src,
Herbert Xu7226bc872006-08-21 21:40:49 +1000154 unsigned int nbytes) __deprecated;
Herbert Xu40725182005-07-06 13:51:52 -0700155 unsigned int (*cia_decrypt_cbc)(const struct cipher_desc *desc,
156 u8 *dst, const u8 *src,
Herbert Xu7226bc872006-08-21 21:40:49 +1000157 unsigned int nbytes) __deprecated;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158};
159
160struct digest_alg {
161 unsigned int dia_digestsize;
Herbert Xu6c2bb982006-05-16 22:09:29 +1000162 void (*dia_init)(struct crypto_tfm *tfm);
163 void (*dia_update)(struct crypto_tfm *tfm, const u8 *data,
164 unsigned int len);
165 void (*dia_final)(struct crypto_tfm *tfm, u8 *out);
166 int (*dia_setkey)(struct crypto_tfm *tfm, const u8 *key,
Herbert Xu560c06a2006-08-13 14:16:39 +1000167 unsigned int keylen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168};
169
Herbert Xu055bcee2006-08-19 22:24:23 +1000170struct hash_alg {
171 int (*init)(struct hash_desc *desc);
172 int (*update)(struct hash_desc *desc, struct scatterlist *sg,
173 unsigned int nbytes);
174 int (*final)(struct hash_desc *desc, u8 *out);
175 int (*digest)(struct hash_desc *desc, struct scatterlist *sg,
176 unsigned int nbytes, u8 *out);
177 int (*setkey)(struct crypto_hash *tfm, const u8 *key,
178 unsigned int keylen);
179
180 unsigned int digestsize;
181};
182
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183struct compress_alg {
Herbert Xu6c2bb982006-05-16 22:09:29 +1000184 int (*coa_compress)(struct crypto_tfm *tfm, const u8 *src,
185 unsigned int slen, u8 *dst, unsigned int *dlen);
186 int (*coa_decompress)(struct crypto_tfm *tfm, const u8 *src,
187 unsigned int slen, u8 *dst, unsigned int *dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188};
189
Herbert Xu5cde0af2006-08-22 00:07:53 +1000190#define cra_blkcipher cra_u.blkcipher
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191#define cra_cipher cra_u.cipher
192#define cra_digest cra_u.digest
Herbert Xu055bcee2006-08-19 22:24:23 +1000193#define cra_hash cra_u.hash
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194#define cra_compress cra_u.compress
195
196struct crypto_alg {
197 struct list_head cra_list;
Herbert Xu6bfd4802006-09-21 11:39:29 +1000198 struct list_head cra_users;
199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 u32 cra_flags;
201 unsigned int cra_blocksize;
202 unsigned int cra_ctxsize;
Herbert Xu95477372005-07-06 13:52:09 -0700203 unsigned int cra_alignmask;
Herbert Xu5cb14542005-11-05 16:58:14 +1100204
205 int cra_priority;
Herbert Xu6521f302006-08-06 20:28:44 +1000206 atomic_t cra_refcnt;
Herbert Xu5cb14542005-11-05 16:58:14 +1100207
Herbert Xud913ea02006-05-21 08:45:26 +1000208 char cra_name[CRYPTO_MAX_ALG_NAME];
209 char cra_driver_name[CRYPTO_MAX_ALG_NAME];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Herbert Xue853c3c2006-08-22 00:06:54 +1000211 const struct crypto_type *cra_type;
212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 union {
Herbert Xu5cde0af2006-08-22 00:07:53 +1000214 struct blkcipher_alg blkcipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 struct cipher_alg cipher;
216 struct digest_alg digest;
Herbert Xu055bcee2006-08-19 22:24:23 +1000217 struct hash_alg hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 struct compress_alg compress;
219 } cra_u;
Herbert Xuc7fc0592006-05-24 13:02:26 +1000220
221 int (*cra_init)(struct crypto_tfm *tfm);
222 void (*cra_exit)(struct crypto_tfm *tfm);
Herbert Xu6521f302006-08-06 20:28:44 +1000223 void (*cra_destroy)(struct crypto_alg *alg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
225 struct module *cra_module;
226};
227
228/*
229 * Algorithm registration interface.
230 */
231int crypto_register_alg(struct crypto_alg *alg);
232int crypto_unregister_alg(struct crypto_alg *alg);
233
234/*
235 * Algorithm query interface.
236 */
237#ifdef CONFIG_CRYPTO
238int crypto_alg_available(const char *name, u32 flags);
Herbert Xufce32d72006-08-26 17:35:45 +1000239int crypto_has_alg(const char *name, u32 type, u32 mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240#else
241static inline int crypto_alg_available(const char *name, u32 flags)
242{
243 return 0;
244}
Herbert Xufce32d72006-08-26 17:35:45 +1000245
246static inline int crypto_has_alg(const char *name, u32 type, u32 mask)
247{
248 return 0;
249}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250#endif
251
252/*
253 * Transforms: user-instantiated objects which encapsulate algorithms
Herbert Xu6d7d6842006-07-30 11:53:01 +1000254 * and core processing logic. Managed via crypto_alloc_*() and
255 * crypto_free_*(), as well as the various helpers below.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Herbert Xu5cde0af2006-08-22 00:07:53 +1000258struct blkcipher_tfm {
259 void *iv;
260 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
261 unsigned int keylen);
262 int (*encrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
263 struct scatterlist *src, unsigned int nbytes);
264 int (*decrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
265 struct scatterlist *src, unsigned int nbytes);
266};
267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268struct cipher_tfm {
269 void *cit_iv;
270 unsigned int cit_ivsize;
271 u32 cit_mode;
272 int (*cit_setkey)(struct crypto_tfm *tfm,
273 const u8 *key, unsigned int keylen);
274 int (*cit_encrypt)(struct crypto_tfm *tfm,
275 struct scatterlist *dst,
276 struct scatterlist *src,
277 unsigned int nbytes);
278 int (*cit_encrypt_iv)(struct crypto_tfm *tfm,
279 struct scatterlist *dst,
280 struct scatterlist *src,
281 unsigned int nbytes, u8 *iv);
282 int (*cit_decrypt)(struct crypto_tfm *tfm,
283 struct scatterlist *dst,
284 struct scatterlist *src,
285 unsigned int nbytes);
286 int (*cit_decrypt_iv)(struct crypto_tfm *tfm,
287 struct scatterlist *dst,
288 struct scatterlist *src,
289 unsigned int nbytes, u8 *iv);
290 void (*cit_xor_block)(u8 *dst, const u8 *src);
Herbert Xuf28776a2006-08-13 20:58:18 +1000291 void (*cit_encrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
292 void (*cit_decrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293};
294
Herbert Xu055bcee2006-08-19 22:24:23 +1000295struct hash_tfm {
296 int (*init)(struct hash_desc *desc);
297 int (*update)(struct hash_desc *desc,
298 struct scatterlist *sg, unsigned int nsg);
299 int (*final)(struct hash_desc *desc, u8 *out);
300 int (*digest)(struct hash_desc *desc, struct scatterlist *sg,
301 unsigned int nsg, u8 *out);
302 int (*setkey)(struct crypto_hash *tfm, const u8 *key,
303 unsigned int keylen);
Herbert Xu055bcee2006-08-19 22:24:23 +1000304 unsigned int digestsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305};
306
307struct compress_tfm {
308 int (*cot_compress)(struct crypto_tfm *tfm,
309 const u8 *src, unsigned int slen,
310 u8 *dst, unsigned int *dlen);
311 int (*cot_decompress)(struct crypto_tfm *tfm,
312 const u8 *src, unsigned int slen,
313 u8 *dst, unsigned int *dlen);
314};
315
Herbert Xu5cde0af2006-08-22 00:07:53 +1000316#define crt_blkcipher crt_u.blkcipher
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317#define crt_cipher crt_u.cipher
Herbert Xu055bcee2006-08-19 22:24:23 +1000318#define crt_hash crt_u.hash
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319#define crt_compress crt_u.compress
320
321struct crypto_tfm {
322
323 u32 crt_flags;
324
325 union {
Herbert Xu5cde0af2006-08-22 00:07:53 +1000326 struct blkcipher_tfm blkcipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 struct cipher_tfm cipher;
Herbert Xu055bcee2006-08-19 22:24:23 +1000328 struct hash_tfm hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 struct compress_tfm compress;
330 } crt_u;
331
332 struct crypto_alg *__crt_alg;
Herbert Xuf10b7892006-01-25 22:34:01 +1100333
Herbert Xu79911102006-08-21 21:03:52 +1000334 void *__crt_ctx[] CRYPTO_MINALIGN_ATTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335};
336
Herbert Xuf28776a2006-08-13 20:58:18 +1000337#define crypto_cipher crypto_tfm
Herbert Xufce32d72006-08-26 17:35:45 +1000338#define crypto_comp crypto_tfm
Herbert Xuf28776a2006-08-13 20:58:18 +1000339
Herbert Xu5cde0af2006-08-22 00:07:53 +1000340struct crypto_blkcipher {
341 struct crypto_tfm base;
342};
343
Herbert Xu055bcee2006-08-19 22:24:23 +1000344struct crypto_hash {
345 struct crypto_tfm base;
346};
347
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000348enum {
349 CRYPTOA_UNSPEC,
350 CRYPTOA_ALG,
351};
352
353struct crypto_attr_alg {
354 char name[CRYPTO_MAX_ALG_NAME];
355};
356
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357/*
358 * Transform user interface.
359 */
360
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361struct crypto_tfm *crypto_alloc_tfm(const char *alg_name, u32 tfm_flags);
Herbert Xu6d7d6842006-07-30 11:53:01 +1000362struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363void crypto_free_tfm(struct crypto_tfm *tfm);
364
365/*
366 * Transform helpers which query the underlying algorithm.
367 */
368static inline const char *crypto_tfm_alg_name(struct crypto_tfm *tfm)
369{
370 return tfm->__crt_alg->cra_name;
371}
372
Michal Ludvigb14cdd62006-07-09 09:02:24 +1000373static inline const char *crypto_tfm_alg_driver_name(struct crypto_tfm *tfm)
374{
375 return tfm->__crt_alg->cra_driver_name;
376}
377
378static inline int crypto_tfm_alg_priority(struct crypto_tfm *tfm)
379{
380 return tfm->__crt_alg->cra_priority;
381}
382
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383static inline const char *crypto_tfm_alg_modname(struct crypto_tfm *tfm)
384{
385 return module_name(tfm->__crt_alg->cra_module);
386}
387
388static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm)
389{
390 return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK;
391}
392
Herbert Xu7226bc872006-08-21 21:40:49 +1000393static unsigned int crypto_tfm_alg_min_keysize(struct crypto_tfm *tfm)
394 __deprecated;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395static inline unsigned int crypto_tfm_alg_min_keysize(struct crypto_tfm *tfm)
396{
397 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
398 return tfm->__crt_alg->cra_cipher.cia_min_keysize;
399}
400
Herbert Xu7226bc872006-08-21 21:40:49 +1000401static unsigned int crypto_tfm_alg_max_keysize(struct crypto_tfm *tfm)
402 __deprecated;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403static inline unsigned int crypto_tfm_alg_max_keysize(struct crypto_tfm *tfm)
404{
405 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
406 return tfm->__crt_alg->cra_cipher.cia_max_keysize;
407}
408
Herbert Xu7226bc872006-08-21 21:40:49 +1000409static unsigned int crypto_tfm_alg_ivsize(struct crypto_tfm *tfm) __deprecated;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410static inline unsigned int crypto_tfm_alg_ivsize(struct crypto_tfm *tfm)
411{
412 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
413 return tfm->crt_cipher.cit_ivsize;
414}
415
416static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm)
417{
418 return tfm->__crt_alg->cra_blocksize;
419}
420
421static inline unsigned int crypto_tfm_alg_digestsize(struct crypto_tfm *tfm)
422{
423 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
424 return tfm->__crt_alg->cra_digest.dia_digestsize;
425}
426
Herbert Xufbdae9f2005-07-06 13:53:29 -0700427static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm *tfm)
428{
429 return tfm->__crt_alg->cra_alignmask;
430}
431
Herbert Xuf28776a2006-08-13 20:58:18 +1000432static inline u32 crypto_tfm_get_flags(struct crypto_tfm *tfm)
433{
434 return tfm->crt_flags;
435}
436
437static inline void crypto_tfm_set_flags(struct crypto_tfm *tfm, u32 flags)
438{
439 tfm->crt_flags |= flags;
440}
441
442static inline void crypto_tfm_clear_flags(struct crypto_tfm *tfm, u32 flags)
443{
444 tfm->crt_flags &= ~flags;
445}
446
Herbert Xu40725182005-07-06 13:51:52 -0700447static inline void *crypto_tfm_ctx(struct crypto_tfm *tfm)
448{
Herbert Xuf10b7892006-01-25 22:34:01 +1100449 return tfm->__crt_ctx;
450}
451
452static inline unsigned int crypto_tfm_ctx_alignment(void)
453{
454 struct crypto_tfm *tfm;
455 return __alignof__(tfm->__crt_ctx);
Herbert Xu40725182005-07-06 13:51:52 -0700456}
457
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458/*
459 * API wrappers.
460 */
Herbert Xu5cde0af2006-08-22 00:07:53 +1000461static inline struct crypto_blkcipher *__crypto_blkcipher_cast(
462 struct crypto_tfm *tfm)
463{
464 return (struct crypto_blkcipher *)tfm;
465}
466
467static inline struct crypto_blkcipher *crypto_blkcipher_cast(
468 struct crypto_tfm *tfm)
469{
470 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_BLKCIPHER);
471 return __crypto_blkcipher_cast(tfm);
472}
473
474static inline struct crypto_blkcipher *crypto_alloc_blkcipher(
475 const char *alg_name, u32 type, u32 mask)
476{
477 type &= ~CRYPTO_ALG_TYPE_MASK;
478 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
479 mask |= CRYPTO_ALG_TYPE_MASK;
480
481 return __crypto_blkcipher_cast(crypto_alloc_base(alg_name, type, mask));
482}
483
484static inline struct crypto_tfm *crypto_blkcipher_tfm(
485 struct crypto_blkcipher *tfm)
486{
487 return &tfm->base;
488}
489
490static inline void crypto_free_blkcipher(struct crypto_blkcipher *tfm)
491{
492 crypto_free_tfm(crypto_blkcipher_tfm(tfm));
493}
494
Herbert Xufce32d72006-08-26 17:35:45 +1000495static inline int crypto_has_blkcipher(const char *alg_name, u32 type, u32 mask)
496{
497 type &= ~CRYPTO_ALG_TYPE_MASK;
498 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
499 mask |= CRYPTO_ALG_TYPE_MASK;
500
501 return crypto_has_alg(alg_name, type, mask);
502}
503
Herbert Xu5cde0af2006-08-22 00:07:53 +1000504static inline const char *crypto_blkcipher_name(struct crypto_blkcipher *tfm)
505{
506 return crypto_tfm_alg_name(crypto_blkcipher_tfm(tfm));
507}
508
509static inline struct blkcipher_tfm *crypto_blkcipher_crt(
510 struct crypto_blkcipher *tfm)
511{
512 return &crypto_blkcipher_tfm(tfm)->crt_blkcipher;
513}
514
515static inline struct blkcipher_alg *crypto_blkcipher_alg(
516 struct crypto_blkcipher *tfm)
517{
518 return &crypto_blkcipher_tfm(tfm)->__crt_alg->cra_blkcipher;
519}
520
521static inline unsigned int crypto_blkcipher_ivsize(struct crypto_blkcipher *tfm)
522{
523 return crypto_blkcipher_alg(tfm)->ivsize;
524}
525
526static inline unsigned int crypto_blkcipher_blocksize(
527 struct crypto_blkcipher *tfm)
528{
529 return crypto_tfm_alg_blocksize(crypto_blkcipher_tfm(tfm));
530}
531
532static inline unsigned int crypto_blkcipher_alignmask(
533 struct crypto_blkcipher *tfm)
534{
535 return crypto_tfm_alg_alignmask(crypto_blkcipher_tfm(tfm));
536}
537
538static inline u32 crypto_blkcipher_get_flags(struct crypto_blkcipher *tfm)
539{
540 return crypto_tfm_get_flags(crypto_blkcipher_tfm(tfm));
541}
542
543static inline void crypto_blkcipher_set_flags(struct crypto_blkcipher *tfm,
544 u32 flags)
545{
546 crypto_tfm_set_flags(crypto_blkcipher_tfm(tfm), flags);
547}
548
549static inline void crypto_blkcipher_clear_flags(struct crypto_blkcipher *tfm,
550 u32 flags)
551{
552 crypto_tfm_clear_flags(crypto_blkcipher_tfm(tfm), flags);
553}
554
555static inline int crypto_blkcipher_setkey(struct crypto_blkcipher *tfm,
556 const u8 *key, unsigned int keylen)
557{
558 return crypto_blkcipher_crt(tfm)->setkey(crypto_blkcipher_tfm(tfm),
559 key, keylen);
560}
561
562static inline int crypto_blkcipher_encrypt(struct blkcipher_desc *desc,
563 struct scatterlist *dst,
564 struct scatterlist *src,
565 unsigned int nbytes)
566{
567 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
568 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
569}
570
571static inline int crypto_blkcipher_encrypt_iv(struct blkcipher_desc *desc,
572 struct scatterlist *dst,
573 struct scatterlist *src,
574 unsigned int nbytes)
575{
576 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
577}
578
579static inline int crypto_blkcipher_decrypt(struct blkcipher_desc *desc,
580 struct scatterlist *dst,
581 struct scatterlist *src,
582 unsigned int nbytes)
583{
584 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
585 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
586}
587
588static inline int crypto_blkcipher_decrypt_iv(struct blkcipher_desc *desc,
589 struct scatterlist *dst,
590 struct scatterlist *src,
591 unsigned int nbytes)
592{
593 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
594}
595
596static inline void crypto_blkcipher_set_iv(struct crypto_blkcipher *tfm,
597 const u8 *src, unsigned int len)
598{
599 memcpy(crypto_blkcipher_crt(tfm)->iv, src, len);
600}
601
602static inline void crypto_blkcipher_get_iv(struct crypto_blkcipher *tfm,
603 u8 *dst, unsigned int len)
604{
605 memcpy(dst, crypto_blkcipher_crt(tfm)->iv, len);
606}
607
Herbert Xuf28776a2006-08-13 20:58:18 +1000608static inline struct crypto_cipher *__crypto_cipher_cast(struct crypto_tfm *tfm)
609{
610 return (struct crypto_cipher *)tfm;
611}
612
613static inline struct crypto_cipher *crypto_cipher_cast(struct crypto_tfm *tfm)
614{
615 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
616 return __crypto_cipher_cast(tfm);
617}
618
619static inline struct crypto_cipher *crypto_alloc_cipher(const char *alg_name,
620 u32 type, u32 mask)
621{
622 type &= ~CRYPTO_ALG_TYPE_MASK;
623 type |= CRYPTO_ALG_TYPE_CIPHER;
624 mask |= CRYPTO_ALG_TYPE_MASK;
625
626 return __crypto_cipher_cast(crypto_alloc_base(alg_name, type, mask));
627}
628
629static inline struct crypto_tfm *crypto_cipher_tfm(struct crypto_cipher *tfm)
630{
631 return tfm;
632}
633
634static inline void crypto_free_cipher(struct crypto_cipher *tfm)
635{
636 crypto_free_tfm(crypto_cipher_tfm(tfm));
637}
638
Herbert Xufce32d72006-08-26 17:35:45 +1000639static inline int crypto_has_cipher(const char *alg_name, u32 type, u32 mask)
640{
641 type &= ~CRYPTO_ALG_TYPE_MASK;
642 type |= CRYPTO_ALG_TYPE_CIPHER;
643 mask |= CRYPTO_ALG_TYPE_MASK;
644
645 return crypto_has_alg(alg_name, type, mask);
646}
647
Herbert Xuf28776a2006-08-13 20:58:18 +1000648static inline struct cipher_tfm *crypto_cipher_crt(struct crypto_cipher *tfm)
649{
650 return &crypto_cipher_tfm(tfm)->crt_cipher;
651}
652
653static inline unsigned int crypto_cipher_blocksize(struct crypto_cipher *tfm)
654{
655 return crypto_tfm_alg_blocksize(crypto_cipher_tfm(tfm));
656}
657
658static inline unsigned int crypto_cipher_alignmask(struct crypto_cipher *tfm)
659{
660 return crypto_tfm_alg_alignmask(crypto_cipher_tfm(tfm));
661}
662
663static inline u32 crypto_cipher_get_flags(struct crypto_cipher *tfm)
664{
665 return crypto_tfm_get_flags(crypto_cipher_tfm(tfm));
666}
667
668static inline void crypto_cipher_set_flags(struct crypto_cipher *tfm,
669 u32 flags)
670{
671 crypto_tfm_set_flags(crypto_cipher_tfm(tfm), flags);
672}
673
674static inline void crypto_cipher_clear_flags(struct crypto_cipher *tfm,
675 u32 flags)
676{
677 crypto_tfm_clear_flags(crypto_cipher_tfm(tfm), flags);
678}
679
Herbert Xu7226bc872006-08-21 21:40:49 +1000680static inline int crypto_cipher_setkey(struct crypto_cipher *tfm,
681 const u8 *key, unsigned int keylen)
682{
683 return crypto_cipher_crt(tfm)->cit_setkey(crypto_cipher_tfm(tfm),
684 key, keylen);
685}
686
Herbert Xuf28776a2006-08-13 20:58:18 +1000687static inline void crypto_cipher_encrypt_one(struct crypto_cipher *tfm,
688 u8 *dst, const u8 *src)
689{
690 crypto_cipher_crt(tfm)->cit_encrypt_one(crypto_cipher_tfm(tfm),
691 dst, src);
692}
693
694static inline void crypto_cipher_decrypt_one(struct crypto_cipher *tfm,
695 u8 *dst, const u8 *src)
696{
697 crypto_cipher_crt(tfm)->cit_decrypt_one(crypto_cipher_tfm(tfm),
698 dst, src);
699}
700
Herbert Xu055bcee2006-08-19 22:24:23 +1000701void crypto_digest_init(struct crypto_tfm *tfm);
702void crypto_digest_update(struct crypto_tfm *tfm,
703 struct scatterlist *sg, unsigned int nsg);
704void crypto_digest_final(struct crypto_tfm *tfm, u8 *out);
705void crypto_digest_digest(struct crypto_tfm *tfm,
706 struct scatterlist *sg, unsigned int nsg, u8 *out);
707
708static inline struct crypto_hash *__crypto_hash_cast(struct crypto_tfm *tfm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709{
Herbert Xu055bcee2006-08-19 22:24:23 +1000710 return (struct crypto_hash *)tfm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711}
712
Herbert Xu055bcee2006-08-19 22:24:23 +1000713static inline struct crypto_hash *crypto_hash_cast(struct crypto_tfm *tfm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714{
Herbert Xu055bcee2006-08-19 22:24:23 +1000715 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_HASH) &
716 CRYPTO_ALG_TYPE_HASH_MASK);
717 return __crypto_hash_cast(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718}
719
720static inline int crypto_digest_setkey(struct crypto_tfm *tfm,
721 const u8 *key, unsigned int keylen)
722{
Herbert Xu055bcee2006-08-19 22:24:23 +1000723 return tfm->crt_hash.setkey(crypto_hash_cast(tfm), key, keylen);
724}
725
726static inline struct crypto_hash *crypto_alloc_hash(const char *alg_name,
727 u32 type, u32 mask)
728{
729 type &= ~CRYPTO_ALG_TYPE_MASK;
730 type |= CRYPTO_ALG_TYPE_HASH;
731 mask |= CRYPTO_ALG_TYPE_HASH_MASK;
732
733 return __crypto_hash_cast(crypto_alloc_base(alg_name, type, mask));
734}
735
736static inline struct crypto_tfm *crypto_hash_tfm(struct crypto_hash *tfm)
737{
738 return &tfm->base;
739}
740
741static inline void crypto_free_hash(struct crypto_hash *tfm)
742{
743 crypto_free_tfm(crypto_hash_tfm(tfm));
744}
745
Herbert Xufce32d72006-08-26 17:35:45 +1000746static inline int crypto_has_hash(const char *alg_name, u32 type, u32 mask)
747{
748 type &= ~CRYPTO_ALG_TYPE_MASK;
749 type |= CRYPTO_ALG_TYPE_HASH;
750 mask |= CRYPTO_ALG_TYPE_HASH_MASK;
751
752 return crypto_has_alg(alg_name, type, mask);
753}
754
Herbert Xu055bcee2006-08-19 22:24:23 +1000755static inline struct hash_tfm *crypto_hash_crt(struct crypto_hash *tfm)
756{
757 return &crypto_hash_tfm(tfm)->crt_hash;
758}
759
760static inline unsigned int crypto_hash_blocksize(struct crypto_hash *tfm)
761{
762 return crypto_tfm_alg_blocksize(crypto_hash_tfm(tfm));
763}
764
765static inline unsigned int crypto_hash_alignmask(struct crypto_hash *tfm)
766{
767 return crypto_tfm_alg_alignmask(crypto_hash_tfm(tfm));
768}
769
770static inline unsigned int crypto_hash_digestsize(struct crypto_hash *tfm)
771{
772 return crypto_hash_crt(tfm)->digestsize;
773}
774
775static inline u32 crypto_hash_get_flags(struct crypto_hash *tfm)
776{
777 return crypto_tfm_get_flags(crypto_hash_tfm(tfm));
778}
779
780static inline void crypto_hash_set_flags(struct crypto_hash *tfm, u32 flags)
781{
782 crypto_tfm_set_flags(crypto_hash_tfm(tfm), flags);
783}
784
785static inline void crypto_hash_clear_flags(struct crypto_hash *tfm, u32 flags)
786{
787 crypto_tfm_clear_flags(crypto_hash_tfm(tfm), flags);
788}
789
790static inline int crypto_hash_init(struct hash_desc *desc)
791{
792 return crypto_hash_crt(desc->tfm)->init(desc);
793}
794
795static inline int crypto_hash_update(struct hash_desc *desc,
796 struct scatterlist *sg,
797 unsigned int nbytes)
798{
799 return crypto_hash_crt(desc->tfm)->update(desc, sg, nbytes);
800}
801
802static inline int crypto_hash_final(struct hash_desc *desc, u8 *out)
803{
804 return crypto_hash_crt(desc->tfm)->final(desc, out);
805}
806
807static inline int crypto_hash_digest(struct hash_desc *desc,
808 struct scatterlist *sg,
809 unsigned int nbytes, u8 *out)
810{
811 return crypto_hash_crt(desc->tfm)->digest(desc, sg, nbytes, out);
812}
813
814static inline int crypto_hash_setkey(struct crypto_hash *hash,
815 const u8 *key, unsigned int keylen)
816{
817 return crypto_hash_crt(hash)->setkey(hash, key, keylen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818}
819
Herbert Xu7226bc872006-08-21 21:40:49 +1000820static int crypto_cipher_encrypt(struct crypto_tfm *tfm,
821 struct scatterlist *dst,
822 struct scatterlist *src,
823 unsigned int nbytes) __deprecated;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824static inline int crypto_cipher_encrypt(struct crypto_tfm *tfm,
825 struct scatterlist *dst,
826 struct scatterlist *src,
827 unsigned int nbytes)
828{
829 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
830 return tfm->crt_cipher.cit_encrypt(tfm, dst, src, nbytes);
831}
832
Herbert Xu7226bc872006-08-21 21:40:49 +1000833static int crypto_cipher_encrypt_iv(struct crypto_tfm *tfm,
834 struct scatterlist *dst,
835 struct scatterlist *src,
836 unsigned int nbytes, u8 *iv) __deprecated;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837static inline int crypto_cipher_encrypt_iv(struct crypto_tfm *tfm,
838 struct scatterlist *dst,
839 struct scatterlist *src,
840 unsigned int nbytes, u8 *iv)
841{
842 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 return tfm->crt_cipher.cit_encrypt_iv(tfm, dst, src, nbytes, iv);
844}
845
Herbert Xu7226bc872006-08-21 21:40:49 +1000846static int crypto_cipher_decrypt(struct crypto_tfm *tfm,
847 struct scatterlist *dst,
848 struct scatterlist *src,
849 unsigned int nbytes) __deprecated;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850static inline int crypto_cipher_decrypt(struct crypto_tfm *tfm,
851 struct scatterlist *dst,
852 struct scatterlist *src,
853 unsigned int nbytes)
854{
855 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
856 return tfm->crt_cipher.cit_decrypt(tfm, dst, src, nbytes);
857}
858
Herbert Xu7226bc872006-08-21 21:40:49 +1000859static int crypto_cipher_decrypt_iv(struct crypto_tfm *tfm,
860 struct scatterlist *dst,
861 struct scatterlist *src,
862 unsigned int nbytes, u8 *iv) __deprecated;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863static inline int crypto_cipher_decrypt_iv(struct crypto_tfm *tfm,
864 struct scatterlist *dst,
865 struct scatterlist *src,
866 unsigned int nbytes, u8 *iv)
867{
868 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 return tfm->crt_cipher.cit_decrypt_iv(tfm, dst, src, nbytes, iv);
870}
871
Herbert Xu7226bc872006-08-21 21:40:49 +1000872static void crypto_cipher_set_iv(struct crypto_tfm *tfm,
873 const u8 *src, unsigned int len) __deprecated;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874static inline void crypto_cipher_set_iv(struct crypto_tfm *tfm,
875 const u8 *src, unsigned int len)
876{
877 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
878 memcpy(tfm->crt_cipher.cit_iv, src, len);
879}
880
Herbert Xu7226bc872006-08-21 21:40:49 +1000881static void crypto_cipher_get_iv(struct crypto_tfm *tfm,
882 u8 *dst, unsigned int len) __deprecated;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883static inline void crypto_cipher_get_iv(struct crypto_tfm *tfm,
884 u8 *dst, unsigned int len)
885{
886 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
887 memcpy(dst, tfm->crt_cipher.cit_iv, len);
888}
889
Herbert Xufce32d72006-08-26 17:35:45 +1000890static inline struct crypto_comp *__crypto_comp_cast(struct crypto_tfm *tfm)
891{
892 return (struct crypto_comp *)tfm;
893}
894
895static inline struct crypto_comp *crypto_comp_cast(struct crypto_tfm *tfm)
896{
897 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_COMPRESS) &
898 CRYPTO_ALG_TYPE_MASK);
899 return __crypto_comp_cast(tfm);
900}
901
902static inline struct crypto_comp *crypto_alloc_comp(const char *alg_name,
903 u32 type, u32 mask)
904{
905 type &= ~CRYPTO_ALG_TYPE_MASK;
906 type |= CRYPTO_ALG_TYPE_COMPRESS;
907 mask |= CRYPTO_ALG_TYPE_MASK;
908
909 return __crypto_comp_cast(crypto_alloc_base(alg_name, type, mask));
910}
911
912static inline struct crypto_tfm *crypto_comp_tfm(struct crypto_comp *tfm)
913{
914 return tfm;
915}
916
917static inline void crypto_free_comp(struct crypto_comp *tfm)
918{
919 crypto_free_tfm(crypto_comp_tfm(tfm));
920}
921
922static inline int crypto_has_comp(const char *alg_name, u32 type, u32 mask)
923{
924 type &= ~CRYPTO_ALG_TYPE_MASK;
925 type |= CRYPTO_ALG_TYPE_COMPRESS;
926 mask |= CRYPTO_ALG_TYPE_MASK;
927
928 return crypto_has_alg(alg_name, type, mask);
929}
930
931static inline struct compress_tfm *crypto_comp_crt(struct crypto_comp *tfm)
932{
933 return &crypto_comp_tfm(tfm)->crt_compress;
934}
935
936static inline int crypto_comp_compress(struct crypto_comp *tfm,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 const u8 *src, unsigned int slen,
938 u8 *dst, unsigned int *dlen)
939{
Herbert Xufce32d72006-08-26 17:35:45 +1000940 return crypto_comp_crt(tfm)->cot_compress(tfm, src, slen, dst, dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941}
942
Herbert Xufce32d72006-08-26 17:35:45 +1000943static inline int crypto_comp_decompress(struct crypto_comp *tfm,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 const u8 *src, unsigned int slen,
945 u8 *dst, unsigned int *dlen)
946{
Herbert Xufce32d72006-08-26 17:35:45 +1000947 return crypto_comp_crt(tfm)->cot_decompress(tfm, src, slen, dst, dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948}
949
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950#endif /* _LINUX_CRYPTO_H */
951