blob: 5a5466d518e8e74c85232f92245e0ad08cc63df3 [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
Herbert Xu5cde0af2006-08-22 00:07:53 +100035#define CRYPTO_ALG_TYPE_BLKCIPHER 0x00000003
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#define CRYPTO_ALG_TYPE_COMPRESS 0x00000004
37
Herbert Xu28259822006-08-06 21:23:26 +100038#define CRYPTO_ALG_LARVAL 0x00000010
Herbert Xu6bfd4802006-09-21 11:39:29 +100039#define CRYPTO_ALG_DEAD 0x00000020
40#define CRYPTO_ALG_DYING 0x00000040
Herbert Xuf3f632d2006-08-06 23:12:59 +100041#define CRYPTO_ALG_ASYNC 0x00000080
Herbert Xu28259822006-08-06 21:23:26 +100042
Linus Torvalds1da177e2005-04-16 15:20:36 -070043/*
44 * Transform masks and values (for crt_flags).
45 */
46#define CRYPTO_TFM_MODE_MASK 0x000000ff
47#define CRYPTO_TFM_REQ_MASK 0x000fff00
48#define CRYPTO_TFM_RES_MASK 0xfff00000
49
50#define CRYPTO_TFM_MODE_ECB 0x00000001
51#define CRYPTO_TFM_MODE_CBC 0x00000002
52#define CRYPTO_TFM_MODE_CFB 0x00000004
53#define CRYPTO_TFM_MODE_CTR 0x00000008
54
55#define CRYPTO_TFM_REQ_WEAK_KEY 0x00000100
Herbert Xu64baf3c2005-09-01 17:43:05 -070056#define CRYPTO_TFM_REQ_MAY_SLEEP 0x00000200
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#define CRYPTO_TFM_RES_WEAK_KEY 0x00100000
58#define CRYPTO_TFM_RES_BAD_KEY_LEN 0x00200000
59#define CRYPTO_TFM_RES_BAD_KEY_SCHED 0x00400000
60#define CRYPTO_TFM_RES_BAD_BLOCK_LEN 0x00800000
61#define CRYPTO_TFM_RES_BAD_FLAGS 0x01000000
62
63/*
64 * Miscellaneous stuff.
65 */
66#define CRYPTO_UNSPEC 0
67#define CRYPTO_MAX_ALG_NAME 64
68
69#define CRYPTO_DIR_ENCRYPT 1
70#define CRYPTO_DIR_DECRYPT 0
71
Herbert Xu79911102006-08-21 21:03:52 +100072/*
73 * The macro CRYPTO_MINALIGN_ATTR (along with the void * type in the actual
74 * declaration) is used to ensure that the crypto_tfm context structure is
75 * aligned correctly for the given architecture so that there are no alignment
76 * faults for C data types. In particular, this is required on platforms such
77 * as arm where pointers are 32-bit aligned but there are data types such as
78 * u64 which require 64-bit alignment.
79 */
80#if defined(ARCH_KMALLOC_MINALIGN)
81#define CRYPTO_MINALIGN ARCH_KMALLOC_MINALIGN
82#elif defined(ARCH_SLAB_MINALIGN)
83#define CRYPTO_MINALIGN ARCH_SLAB_MINALIGN
84#endif
85
86#ifdef CRYPTO_MINALIGN
87#define CRYPTO_MINALIGN_ATTR __attribute__ ((__aligned__(CRYPTO_MINALIGN)))
88#else
89#define CRYPTO_MINALIGN_ATTR
90#endif
91
Linus Torvalds1da177e2005-04-16 15:20:36 -070092struct scatterlist;
Herbert Xu5cde0af2006-08-22 00:07:53 +100093struct crypto_blkcipher;
Herbert Xu40725182005-07-06 13:51:52 -070094struct crypto_tfm;
Herbert Xue853c3c2006-08-22 00:06:54 +100095struct crypto_type;
Herbert Xu40725182005-07-06 13:51:52 -070096
Herbert Xu5cde0af2006-08-22 00:07:53 +100097struct blkcipher_desc {
98 struct crypto_blkcipher *tfm;
99 void *info;
100 u32 flags;
101};
102
Herbert Xu40725182005-07-06 13:51:52 -0700103struct cipher_desc {
104 struct crypto_tfm *tfm;
Herbert Xu6c2bb982006-05-16 22:09:29 +1000105 void (*crfn)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Herbert Xu40725182005-07-06 13:51:52 -0700106 unsigned int (*prfn)(const struct cipher_desc *desc, u8 *dst,
107 const u8 *src, unsigned int nbytes);
108 void *info;
109};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
111/*
112 * Algorithms: modular crypto algorithm implementations, managed
113 * via crypto_register_alg() and crypto_unregister_alg().
114 */
Herbert Xu5cde0af2006-08-22 00:07:53 +1000115struct blkcipher_alg {
116 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
117 unsigned int keylen);
118 int (*encrypt)(struct blkcipher_desc *desc,
119 struct scatterlist *dst, struct scatterlist *src,
120 unsigned int nbytes);
121 int (*decrypt)(struct blkcipher_desc *desc,
122 struct scatterlist *dst, struct scatterlist *src,
123 unsigned int nbytes);
124
125 unsigned int min_keysize;
126 unsigned int max_keysize;
127 unsigned int ivsize;
128};
129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130struct cipher_alg {
131 unsigned int cia_min_keysize;
132 unsigned int cia_max_keysize;
Herbert Xu6c2bb982006-05-16 22:09:29 +1000133 int (*cia_setkey)(struct crypto_tfm *tfm, const u8 *key,
Herbert Xu560c06a2006-08-13 14:16:39 +1000134 unsigned int keylen);
Herbert Xu6c2bb982006-05-16 22:09:29 +1000135 void (*cia_encrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
136 void (*cia_decrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Herbert Xu40725182005-07-06 13:51:52 -0700137
138 unsigned int (*cia_encrypt_ecb)(const struct cipher_desc *desc,
139 u8 *dst, const u8 *src,
140 unsigned int nbytes);
141 unsigned int (*cia_decrypt_ecb)(const struct cipher_desc *desc,
142 u8 *dst, const u8 *src,
143 unsigned int nbytes);
144 unsigned int (*cia_encrypt_cbc)(const struct cipher_desc *desc,
145 u8 *dst, const u8 *src,
146 unsigned int nbytes);
147 unsigned int (*cia_decrypt_cbc)(const struct cipher_desc *desc,
148 u8 *dst, const u8 *src,
149 unsigned int nbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150};
151
152struct digest_alg {
153 unsigned int dia_digestsize;
Herbert Xu6c2bb982006-05-16 22:09:29 +1000154 void (*dia_init)(struct crypto_tfm *tfm);
155 void (*dia_update)(struct crypto_tfm *tfm, const u8 *data,
156 unsigned int len);
157 void (*dia_final)(struct crypto_tfm *tfm, u8 *out);
158 int (*dia_setkey)(struct crypto_tfm *tfm, const u8 *key,
Herbert Xu560c06a2006-08-13 14:16:39 +1000159 unsigned int keylen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160};
161
162struct compress_alg {
Herbert Xu6c2bb982006-05-16 22:09:29 +1000163 int (*coa_compress)(struct crypto_tfm *tfm, const u8 *src,
164 unsigned int slen, u8 *dst, unsigned int *dlen);
165 int (*coa_decompress)(struct crypto_tfm *tfm, const u8 *src,
166 unsigned int slen, u8 *dst, unsigned int *dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167};
168
Herbert Xu5cde0af2006-08-22 00:07:53 +1000169#define cra_blkcipher cra_u.blkcipher
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170#define cra_cipher cra_u.cipher
171#define cra_digest cra_u.digest
172#define cra_compress cra_u.compress
173
174struct crypto_alg {
175 struct list_head cra_list;
Herbert Xu6bfd4802006-09-21 11:39:29 +1000176 struct list_head cra_users;
177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 u32 cra_flags;
179 unsigned int cra_blocksize;
180 unsigned int cra_ctxsize;
Herbert Xu95477372005-07-06 13:52:09 -0700181 unsigned int cra_alignmask;
Herbert Xu5cb14542005-11-05 16:58:14 +1100182
183 int cra_priority;
Herbert Xu6521f302006-08-06 20:28:44 +1000184 atomic_t cra_refcnt;
Herbert Xu5cb14542005-11-05 16:58:14 +1100185
Herbert Xud913ea02006-05-21 08:45:26 +1000186 char cra_name[CRYPTO_MAX_ALG_NAME];
187 char cra_driver_name[CRYPTO_MAX_ALG_NAME];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
Herbert Xue853c3c2006-08-22 00:06:54 +1000189 const struct crypto_type *cra_type;
190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 union {
Herbert Xu5cde0af2006-08-22 00:07:53 +1000192 struct blkcipher_alg blkcipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 struct cipher_alg cipher;
194 struct digest_alg digest;
195 struct compress_alg compress;
196 } cra_u;
Herbert Xuc7fc0592006-05-24 13:02:26 +1000197
198 int (*cra_init)(struct crypto_tfm *tfm);
199 void (*cra_exit)(struct crypto_tfm *tfm);
Herbert Xu6521f302006-08-06 20:28:44 +1000200 void (*cra_destroy)(struct crypto_alg *alg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202 struct module *cra_module;
203};
204
205/*
206 * Algorithm registration interface.
207 */
208int crypto_register_alg(struct crypto_alg *alg);
209int crypto_unregister_alg(struct crypto_alg *alg);
210
211/*
212 * Algorithm query interface.
213 */
214#ifdef CONFIG_CRYPTO
215int crypto_alg_available(const char *name, u32 flags);
216#else
217static inline int crypto_alg_available(const char *name, u32 flags)
218{
219 return 0;
220}
221#endif
222
223/*
224 * Transforms: user-instantiated objects which encapsulate algorithms
Herbert Xu6d7d6842006-07-30 11:53:01 +1000225 * and core processing logic. Managed via crypto_alloc_*() and
226 * crypto_free_*(), as well as the various helpers below.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Herbert Xu5cde0af2006-08-22 00:07:53 +1000229struct blkcipher_tfm {
230 void *iv;
231 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
232 unsigned int keylen);
233 int (*encrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
234 struct scatterlist *src, unsigned int nbytes);
235 int (*decrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
236 struct scatterlist *src, unsigned int nbytes);
237};
238
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239struct cipher_tfm {
240 void *cit_iv;
241 unsigned int cit_ivsize;
242 u32 cit_mode;
243 int (*cit_setkey)(struct crypto_tfm *tfm,
244 const u8 *key, unsigned int keylen);
245 int (*cit_encrypt)(struct crypto_tfm *tfm,
246 struct scatterlist *dst,
247 struct scatterlist *src,
248 unsigned int nbytes);
249 int (*cit_encrypt_iv)(struct crypto_tfm *tfm,
250 struct scatterlist *dst,
251 struct scatterlist *src,
252 unsigned int nbytes, u8 *iv);
253 int (*cit_decrypt)(struct crypto_tfm *tfm,
254 struct scatterlist *dst,
255 struct scatterlist *src,
256 unsigned int nbytes);
257 int (*cit_decrypt_iv)(struct crypto_tfm *tfm,
258 struct scatterlist *dst,
259 struct scatterlist *src,
260 unsigned int nbytes, u8 *iv);
261 void (*cit_xor_block)(u8 *dst, const u8 *src);
Herbert Xuf28776a2006-08-13 20:58:18 +1000262 void (*cit_encrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
263 void (*cit_decrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264};
265
266struct digest_tfm {
267 void (*dit_init)(struct crypto_tfm *tfm);
268 void (*dit_update)(struct crypto_tfm *tfm,
269 struct scatterlist *sg, unsigned int nsg);
270 void (*dit_final)(struct crypto_tfm *tfm, u8 *out);
271 void (*dit_digest)(struct crypto_tfm *tfm, struct scatterlist *sg,
272 unsigned int nsg, u8 *out);
273 int (*dit_setkey)(struct crypto_tfm *tfm,
274 const u8 *key, unsigned int keylen);
275#ifdef CONFIG_CRYPTO_HMAC
276 void *dit_hmac_block;
277#endif
278};
279
280struct compress_tfm {
281 int (*cot_compress)(struct crypto_tfm *tfm,
282 const u8 *src, unsigned int slen,
283 u8 *dst, unsigned int *dlen);
284 int (*cot_decompress)(struct crypto_tfm *tfm,
285 const u8 *src, unsigned int slen,
286 u8 *dst, unsigned int *dlen);
287};
288
Herbert Xu5cde0af2006-08-22 00:07:53 +1000289#define crt_blkcipher crt_u.blkcipher
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290#define crt_cipher crt_u.cipher
291#define crt_digest crt_u.digest
292#define crt_compress crt_u.compress
293
294struct crypto_tfm {
295
296 u32 crt_flags;
297
298 union {
Herbert Xu5cde0af2006-08-22 00:07:53 +1000299 struct blkcipher_tfm blkcipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 struct cipher_tfm cipher;
301 struct digest_tfm digest;
302 struct compress_tfm compress;
303 } crt_u;
304
305 struct crypto_alg *__crt_alg;
Herbert Xuf10b7892006-01-25 22:34:01 +1100306
Herbert Xu79911102006-08-21 21:03:52 +1000307 void *__crt_ctx[] CRYPTO_MINALIGN_ATTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308};
309
Herbert Xuf28776a2006-08-13 20:58:18 +1000310#define crypto_cipher crypto_tfm
311
Herbert Xu5cde0af2006-08-22 00:07:53 +1000312struct crypto_blkcipher {
313 struct crypto_tfm base;
314};
315
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000316enum {
317 CRYPTOA_UNSPEC,
318 CRYPTOA_ALG,
319};
320
321struct crypto_attr_alg {
322 char name[CRYPTO_MAX_ALG_NAME];
323};
324
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325/*
326 * Transform user interface.
327 */
328
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329struct crypto_tfm *crypto_alloc_tfm(const char *alg_name, u32 tfm_flags);
Herbert Xu6d7d6842006-07-30 11:53:01 +1000330struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331void crypto_free_tfm(struct crypto_tfm *tfm);
332
333/*
334 * Transform helpers which query the underlying algorithm.
335 */
336static inline const char *crypto_tfm_alg_name(struct crypto_tfm *tfm)
337{
338 return tfm->__crt_alg->cra_name;
339}
340
Michal Ludvigb14cdd62006-07-09 09:02:24 +1000341static inline const char *crypto_tfm_alg_driver_name(struct crypto_tfm *tfm)
342{
343 return tfm->__crt_alg->cra_driver_name;
344}
345
346static inline int crypto_tfm_alg_priority(struct crypto_tfm *tfm)
347{
348 return tfm->__crt_alg->cra_priority;
349}
350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351static inline const char *crypto_tfm_alg_modname(struct crypto_tfm *tfm)
352{
353 return module_name(tfm->__crt_alg->cra_module);
354}
355
356static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm)
357{
358 return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK;
359}
360
361static inline unsigned int crypto_tfm_alg_min_keysize(struct crypto_tfm *tfm)
362{
363 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
364 return tfm->__crt_alg->cra_cipher.cia_min_keysize;
365}
366
367static inline unsigned int crypto_tfm_alg_max_keysize(struct crypto_tfm *tfm)
368{
369 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
370 return tfm->__crt_alg->cra_cipher.cia_max_keysize;
371}
372
373static inline unsigned int crypto_tfm_alg_ivsize(struct crypto_tfm *tfm)
374{
375 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
376 return tfm->crt_cipher.cit_ivsize;
377}
378
379static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm)
380{
381 return tfm->__crt_alg->cra_blocksize;
382}
383
384static inline unsigned int crypto_tfm_alg_digestsize(struct crypto_tfm *tfm)
385{
386 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
387 return tfm->__crt_alg->cra_digest.dia_digestsize;
388}
389
Herbert Xufbdae9f2005-07-06 13:53:29 -0700390static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm *tfm)
391{
392 return tfm->__crt_alg->cra_alignmask;
393}
394
Herbert Xuf28776a2006-08-13 20:58:18 +1000395static inline u32 crypto_tfm_get_flags(struct crypto_tfm *tfm)
396{
397 return tfm->crt_flags;
398}
399
400static inline void crypto_tfm_set_flags(struct crypto_tfm *tfm, u32 flags)
401{
402 tfm->crt_flags |= flags;
403}
404
405static inline void crypto_tfm_clear_flags(struct crypto_tfm *tfm, u32 flags)
406{
407 tfm->crt_flags &= ~flags;
408}
409
Herbert Xu40725182005-07-06 13:51:52 -0700410static inline void *crypto_tfm_ctx(struct crypto_tfm *tfm)
411{
Herbert Xuf10b7892006-01-25 22:34:01 +1100412 return tfm->__crt_ctx;
413}
414
415static inline unsigned int crypto_tfm_ctx_alignment(void)
416{
417 struct crypto_tfm *tfm;
418 return __alignof__(tfm->__crt_ctx);
Herbert Xu40725182005-07-06 13:51:52 -0700419}
420
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421/*
422 * API wrappers.
423 */
Herbert Xu5cde0af2006-08-22 00:07:53 +1000424static inline struct crypto_blkcipher *__crypto_blkcipher_cast(
425 struct crypto_tfm *tfm)
426{
427 return (struct crypto_blkcipher *)tfm;
428}
429
430static inline struct crypto_blkcipher *crypto_blkcipher_cast(
431 struct crypto_tfm *tfm)
432{
433 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_BLKCIPHER);
434 return __crypto_blkcipher_cast(tfm);
435}
436
437static inline struct crypto_blkcipher *crypto_alloc_blkcipher(
438 const char *alg_name, u32 type, u32 mask)
439{
440 type &= ~CRYPTO_ALG_TYPE_MASK;
441 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
442 mask |= CRYPTO_ALG_TYPE_MASK;
443
444 return __crypto_blkcipher_cast(crypto_alloc_base(alg_name, type, mask));
445}
446
447static inline struct crypto_tfm *crypto_blkcipher_tfm(
448 struct crypto_blkcipher *tfm)
449{
450 return &tfm->base;
451}
452
453static inline void crypto_free_blkcipher(struct crypto_blkcipher *tfm)
454{
455 crypto_free_tfm(crypto_blkcipher_tfm(tfm));
456}
457
458static inline const char *crypto_blkcipher_name(struct crypto_blkcipher *tfm)
459{
460 return crypto_tfm_alg_name(crypto_blkcipher_tfm(tfm));
461}
462
463static inline struct blkcipher_tfm *crypto_blkcipher_crt(
464 struct crypto_blkcipher *tfm)
465{
466 return &crypto_blkcipher_tfm(tfm)->crt_blkcipher;
467}
468
469static inline struct blkcipher_alg *crypto_blkcipher_alg(
470 struct crypto_blkcipher *tfm)
471{
472 return &crypto_blkcipher_tfm(tfm)->__crt_alg->cra_blkcipher;
473}
474
475static inline unsigned int crypto_blkcipher_ivsize(struct crypto_blkcipher *tfm)
476{
477 return crypto_blkcipher_alg(tfm)->ivsize;
478}
479
480static inline unsigned int crypto_blkcipher_blocksize(
481 struct crypto_blkcipher *tfm)
482{
483 return crypto_tfm_alg_blocksize(crypto_blkcipher_tfm(tfm));
484}
485
486static inline unsigned int crypto_blkcipher_alignmask(
487 struct crypto_blkcipher *tfm)
488{
489 return crypto_tfm_alg_alignmask(crypto_blkcipher_tfm(tfm));
490}
491
492static inline u32 crypto_blkcipher_get_flags(struct crypto_blkcipher *tfm)
493{
494 return crypto_tfm_get_flags(crypto_blkcipher_tfm(tfm));
495}
496
497static inline void crypto_blkcipher_set_flags(struct crypto_blkcipher *tfm,
498 u32 flags)
499{
500 crypto_tfm_set_flags(crypto_blkcipher_tfm(tfm), flags);
501}
502
503static inline void crypto_blkcipher_clear_flags(struct crypto_blkcipher *tfm,
504 u32 flags)
505{
506 crypto_tfm_clear_flags(crypto_blkcipher_tfm(tfm), flags);
507}
508
509static inline int crypto_blkcipher_setkey(struct crypto_blkcipher *tfm,
510 const u8 *key, unsigned int keylen)
511{
512 return crypto_blkcipher_crt(tfm)->setkey(crypto_blkcipher_tfm(tfm),
513 key, keylen);
514}
515
516static inline int crypto_blkcipher_encrypt(struct blkcipher_desc *desc,
517 struct scatterlist *dst,
518 struct scatterlist *src,
519 unsigned int nbytes)
520{
521 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
522 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
523}
524
525static inline int crypto_blkcipher_encrypt_iv(struct blkcipher_desc *desc,
526 struct scatterlist *dst,
527 struct scatterlist *src,
528 unsigned int nbytes)
529{
530 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
531}
532
533static inline int crypto_blkcipher_decrypt(struct blkcipher_desc *desc,
534 struct scatterlist *dst,
535 struct scatterlist *src,
536 unsigned int nbytes)
537{
538 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
539 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
540}
541
542static inline int crypto_blkcipher_decrypt_iv(struct blkcipher_desc *desc,
543 struct scatterlist *dst,
544 struct scatterlist *src,
545 unsigned int nbytes)
546{
547 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
548}
549
550static inline void crypto_blkcipher_set_iv(struct crypto_blkcipher *tfm,
551 const u8 *src, unsigned int len)
552{
553 memcpy(crypto_blkcipher_crt(tfm)->iv, src, len);
554}
555
556static inline void crypto_blkcipher_get_iv(struct crypto_blkcipher *tfm,
557 u8 *dst, unsigned int len)
558{
559 memcpy(dst, crypto_blkcipher_crt(tfm)->iv, len);
560}
561
Herbert Xuf28776a2006-08-13 20:58:18 +1000562static inline struct crypto_cipher *__crypto_cipher_cast(struct crypto_tfm *tfm)
563{
564 return (struct crypto_cipher *)tfm;
565}
566
567static inline struct crypto_cipher *crypto_cipher_cast(struct crypto_tfm *tfm)
568{
569 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
570 return __crypto_cipher_cast(tfm);
571}
572
573static inline struct crypto_cipher *crypto_alloc_cipher(const char *alg_name,
574 u32 type, u32 mask)
575{
576 type &= ~CRYPTO_ALG_TYPE_MASK;
577 type |= CRYPTO_ALG_TYPE_CIPHER;
578 mask |= CRYPTO_ALG_TYPE_MASK;
579
580 return __crypto_cipher_cast(crypto_alloc_base(alg_name, type, mask));
581}
582
583static inline struct crypto_tfm *crypto_cipher_tfm(struct crypto_cipher *tfm)
584{
585 return tfm;
586}
587
588static inline void crypto_free_cipher(struct crypto_cipher *tfm)
589{
590 crypto_free_tfm(crypto_cipher_tfm(tfm));
591}
592
593static inline struct cipher_tfm *crypto_cipher_crt(struct crypto_cipher *tfm)
594{
595 return &crypto_cipher_tfm(tfm)->crt_cipher;
596}
597
598static inline unsigned int crypto_cipher_blocksize(struct crypto_cipher *tfm)
599{
600 return crypto_tfm_alg_blocksize(crypto_cipher_tfm(tfm));
601}
602
603static inline unsigned int crypto_cipher_alignmask(struct crypto_cipher *tfm)
604{
605 return crypto_tfm_alg_alignmask(crypto_cipher_tfm(tfm));
606}
607
608static inline u32 crypto_cipher_get_flags(struct crypto_cipher *tfm)
609{
610 return crypto_tfm_get_flags(crypto_cipher_tfm(tfm));
611}
612
613static inline void crypto_cipher_set_flags(struct crypto_cipher *tfm,
614 u32 flags)
615{
616 crypto_tfm_set_flags(crypto_cipher_tfm(tfm), flags);
617}
618
619static inline void crypto_cipher_clear_flags(struct crypto_cipher *tfm,
620 u32 flags)
621{
622 crypto_tfm_clear_flags(crypto_cipher_tfm(tfm), flags);
623}
624
625static inline void crypto_cipher_encrypt_one(struct crypto_cipher *tfm,
626 u8 *dst, const u8 *src)
627{
628 crypto_cipher_crt(tfm)->cit_encrypt_one(crypto_cipher_tfm(tfm),
629 dst, src);
630}
631
632static inline void crypto_cipher_decrypt_one(struct crypto_cipher *tfm,
633 u8 *dst, const u8 *src)
634{
635 crypto_cipher_crt(tfm)->cit_decrypt_one(crypto_cipher_tfm(tfm),
636 dst, src);
637}
638
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639static inline void crypto_digest_init(struct crypto_tfm *tfm)
640{
641 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
642 tfm->crt_digest.dit_init(tfm);
643}
644
645static inline void crypto_digest_update(struct crypto_tfm *tfm,
646 struct scatterlist *sg,
647 unsigned int nsg)
648{
649 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
650 tfm->crt_digest.dit_update(tfm, sg, nsg);
651}
652
653static inline void crypto_digest_final(struct crypto_tfm *tfm, u8 *out)
654{
655 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
656 tfm->crt_digest.dit_final(tfm, out);
657}
658
659static inline void crypto_digest_digest(struct crypto_tfm *tfm,
660 struct scatterlist *sg,
661 unsigned int nsg, u8 *out)
662{
663 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
664 tfm->crt_digest.dit_digest(tfm, sg, nsg, out);
665}
666
667static inline int crypto_digest_setkey(struct crypto_tfm *tfm,
668 const u8 *key, unsigned int keylen)
669{
670 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 return tfm->crt_digest.dit_setkey(tfm, key, keylen);
672}
673
674static inline int crypto_cipher_setkey(struct crypto_tfm *tfm,
675 const u8 *key, unsigned int keylen)
676{
677 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
678 return tfm->crt_cipher.cit_setkey(tfm, key, keylen);
679}
680
681static inline int crypto_cipher_encrypt(struct crypto_tfm *tfm,
682 struct scatterlist *dst,
683 struct scatterlist *src,
684 unsigned int nbytes)
685{
686 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
687 return tfm->crt_cipher.cit_encrypt(tfm, dst, src, nbytes);
688}
689
690static inline int crypto_cipher_encrypt_iv(struct crypto_tfm *tfm,
691 struct scatterlist *dst,
692 struct scatterlist *src,
693 unsigned int nbytes, u8 *iv)
694{
695 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 return tfm->crt_cipher.cit_encrypt_iv(tfm, dst, src, nbytes, iv);
697}
698
699static inline int crypto_cipher_decrypt(struct crypto_tfm *tfm,
700 struct scatterlist *dst,
701 struct scatterlist *src,
702 unsigned int nbytes)
703{
704 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
705 return tfm->crt_cipher.cit_decrypt(tfm, dst, src, nbytes);
706}
707
708static inline int crypto_cipher_decrypt_iv(struct crypto_tfm *tfm,
709 struct scatterlist *dst,
710 struct scatterlist *src,
711 unsigned int nbytes, u8 *iv)
712{
713 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 return tfm->crt_cipher.cit_decrypt_iv(tfm, dst, src, nbytes, iv);
715}
716
717static inline void crypto_cipher_set_iv(struct crypto_tfm *tfm,
718 const u8 *src, unsigned int len)
719{
720 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
721 memcpy(tfm->crt_cipher.cit_iv, src, len);
722}
723
724static inline void crypto_cipher_get_iv(struct crypto_tfm *tfm,
725 u8 *dst, unsigned int len)
726{
727 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
728 memcpy(dst, tfm->crt_cipher.cit_iv, len);
729}
730
731static inline int crypto_comp_compress(struct crypto_tfm *tfm,
732 const u8 *src, unsigned int slen,
733 u8 *dst, unsigned int *dlen)
734{
735 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_COMPRESS);
736 return tfm->crt_compress.cot_compress(tfm, src, slen, dst, dlen);
737}
738
739static inline int crypto_comp_decompress(struct crypto_tfm *tfm,
740 const u8 *src, unsigned int slen,
741 u8 *dst, unsigned int *dlen)
742{
743 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_COMPRESS);
744 return tfm->crt_compress.cot_decompress(tfm, src, slen, dst, dlen);
745}
746
747/*
748 * HMAC support.
749 */
750#ifdef CONFIG_CRYPTO_HMAC
751void crypto_hmac_init(struct crypto_tfm *tfm, u8 *key, unsigned int *keylen);
752void crypto_hmac_update(struct crypto_tfm *tfm,
753 struct scatterlist *sg, unsigned int nsg);
754void crypto_hmac_final(struct crypto_tfm *tfm, u8 *key,
755 unsigned int *keylen, u8 *out);
756void crypto_hmac(struct crypto_tfm *tfm, u8 *key, unsigned int *keylen,
757 struct scatterlist *sg, unsigned int nsg, u8 *out);
758#endif /* CONFIG_CRYPTO_HMAC */
759
760#endif /* _LINUX_CRYPTO_H */
761