blob: fdecee83878ca5fec5f0260a03e5c1ae0a4b9ef8 [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;
Herbert Xue853c3c2006-08-22 00:06:54 +100093struct crypto_type;
Herbert Xu40725182005-07-06 13:51:52 -070094
95struct cipher_desc {
96 struct crypto_tfm *tfm;
Herbert Xu6c2bb982006-05-16 22:09:29 +100097 void (*crfn)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Herbert Xu40725182005-07-06 13:51:52 -070098 unsigned int (*prfn)(const struct cipher_desc *desc, u8 *dst,
99 const u8 *src, unsigned int nbytes);
100 void *info;
101};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103/*
104 * Algorithms: modular crypto algorithm implementations, managed
105 * via crypto_register_alg() and crypto_unregister_alg().
106 */
107struct cipher_alg {
108 unsigned int cia_min_keysize;
109 unsigned int cia_max_keysize;
Herbert Xu6c2bb982006-05-16 22:09:29 +1000110 int (*cia_setkey)(struct crypto_tfm *tfm, const u8 *key,
Herbert Xu560c06a2006-08-13 14:16:39 +1000111 unsigned int keylen);
Herbert Xu6c2bb982006-05-16 22:09:29 +1000112 void (*cia_encrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
113 void (*cia_decrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Herbert Xu40725182005-07-06 13:51:52 -0700114
115 unsigned int (*cia_encrypt_ecb)(const struct cipher_desc *desc,
116 u8 *dst, const u8 *src,
117 unsigned int nbytes);
118 unsigned int (*cia_decrypt_ecb)(const struct cipher_desc *desc,
119 u8 *dst, const u8 *src,
120 unsigned int nbytes);
121 unsigned int (*cia_encrypt_cbc)(const struct cipher_desc *desc,
122 u8 *dst, const u8 *src,
123 unsigned int nbytes);
124 unsigned int (*cia_decrypt_cbc)(const struct cipher_desc *desc,
125 u8 *dst, const u8 *src,
126 unsigned int nbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127};
128
129struct digest_alg {
130 unsigned int dia_digestsize;
Herbert Xu6c2bb982006-05-16 22:09:29 +1000131 void (*dia_init)(struct crypto_tfm *tfm);
132 void (*dia_update)(struct crypto_tfm *tfm, const u8 *data,
133 unsigned int len);
134 void (*dia_final)(struct crypto_tfm *tfm, u8 *out);
135 int (*dia_setkey)(struct crypto_tfm *tfm, const u8 *key,
Herbert Xu560c06a2006-08-13 14:16:39 +1000136 unsigned int keylen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137};
138
139struct compress_alg {
Herbert Xu6c2bb982006-05-16 22:09:29 +1000140 int (*coa_compress)(struct crypto_tfm *tfm, const u8 *src,
141 unsigned int slen, u8 *dst, unsigned int *dlen);
142 int (*coa_decompress)(struct crypto_tfm *tfm, const u8 *src,
143 unsigned int slen, u8 *dst, unsigned int *dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144};
145
146#define cra_cipher cra_u.cipher
147#define cra_digest cra_u.digest
148#define cra_compress cra_u.compress
149
150struct crypto_alg {
151 struct list_head cra_list;
Herbert Xu6bfd4802006-09-21 11:39:29 +1000152 struct list_head cra_users;
153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 u32 cra_flags;
155 unsigned int cra_blocksize;
156 unsigned int cra_ctxsize;
Herbert Xu95477372005-07-06 13:52:09 -0700157 unsigned int cra_alignmask;
Herbert Xu5cb14542005-11-05 16:58:14 +1100158
159 int cra_priority;
Herbert Xu6521f302006-08-06 20:28:44 +1000160 atomic_t cra_refcnt;
Herbert Xu5cb14542005-11-05 16:58:14 +1100161
Herbert Xud913ea02006-05-21 08:45:26 +1000162 char cra_name[CRYPTO_MAX_ALG_NAME];
163 char cra_driver_name[CRYPTO_MAX_ALG_NAME];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Herbert Xue853c3c2006-08-22 00:06:54 +1000165 const struct crypto_type *cra_type;
166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 union {
168 struct cipher_alg cipher;
169 struct digest_alg digest;
170 struct compress_alg compress;
171 } cra_u;
Herbert Xuc7fc0592006-05-24 13:02:26 +1000172
173 int (*cra_init)(struct crypto_tfm *tfm);
174 void (*cra_exit)(struct crypto_tfm *tfm);
Herbert Xu6521f302006-08-06 20:28:44 +1000175 void (*cra_destroy)(struct crypto_alg *alg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
177 struct module *cra_module;
178};
179
180/*
181 * Algorithm registration interface.
182 */
183int crypto_register_alg(struct crypto_alg *alg);
184int crypto_unregister_alg(struct crypto_alg *alg);
185
186/*
187 * Algorithm query interface.
188 */
189#ifdef CONFIG_CRYPTO
190int crypto_alg_available(const char *name, u32 flags);
191#else
192static inline int crypto_alg_available(const char *name, u32 flags)
193{
194 return 0;
195}
196#endif
197
198/*
199 * Transforms: user-instantiated objects which encapsulate algorithms
Herbert Xu6d7d6842006-07-30 11:53:01 +1000200 * and core processing logic. Managed via crypto_alloc_*() and
201 * crypto_free_*(), as well as the various helpers below.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
204struct cipher_tfm {
205 void *cit_iv;
206 unsigned int cit_ivsize;
207 u32 cit_mode;
208 int (*cit_setkey)(struct crypto_tfm *tfm,
209 const u8 *key, unsigned int keylen);
210 int (*cit_encrypt)(struct crypto_tfm *tfm,
211 struct scatterlist *dst,
212 struct scatterlist *src,
213 unsigned int nbytes);
214 int (*cit_encrypt_iv)(struct crypto_tfm *tfm,
215 struct scatterlist *dst,
216 struct scatterlist *src,
217 unsigned int nbytes, u8 *iv);
218 int (*cit_decrypt)(struct crypto_tfm *tfm,
219 struct scatterlist *dst,
220 struct scatterlist *src,
221 unsigned int nbytes);
222 int (*cit_decrypt_iv)(struct crypto_tfm *tfm,
223 struct scatterlist *dst,
224 struct scatterlist *src,
225 unsigned int nbytes, u8 *iv);
226 void (*cit_xor_block)(u8 *dst, const u8 *src);
Herbert Xuf28776a2006-08-13 20:58:18 +1000227 void (*cit_encrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
228 void (*cit_decrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229};
230
231struct digest_tfm {
232 void (*dit_init)(struct crypto_tfm *tfm);
233 void (*dit_update)(struct crypto_tfm *tfm,
234 struct scatterlist *sg, unsigned int nsg);
235 void (*dit_final)(struct crypto_tfm *tfm, u8 *out);
236 void (*dit_digest)(struct crypto_tfm *tfm, struct scatterlist *sg,
237 unsigned int nsg, u8 *out);
238 int (*dit_setkey)(struct crypto_tfm *tfm,
239 const u8 *key, unsigned int keylen);
240#ifdef CONFIG_CRYPTO_HMAC
241 void *dit_hmac_block;
242#endif
243};
244
245struct compress_tfm {
246 int (*cot_compress)(struct crypto_tfm *tfm,
247 const u8 *src, unsigned int slen,
248 u8 *dst, unsigned int *dlen);
249 int (*cot_decompress)(struct crypto_tfm *tfm,
250 const u8 *src, unsigned int slen,
251 u8 *dst, unsigned int *dlen);
252};
253
254#define crt_cipher crt_u.cipher
255#define crt_digest crt_u.digest
256#define crt_compress crt_u.compress
257
258struct crypto_tfm {
259
260 u32 crt_flags;
261
262 union {
263 struct cipher_tfm cipher;
264 struct digest_tfm digest;
265 struct compress_tfm compress;
266 } crt_u;
267
268 struct crypto_alg *__crt_alg;
Herbert Xuf10b7892006-01-25 22:34:01 +1100269
Herbert Xu79911102006-08-21 21:03:52 +1000270 void *__crt_ctx[] CRYPTO_MINALIGN_ATTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271};
272
Herbert Xuf28776a2006-08-13 20:58:18 +1000273#define crypto_cipher crypto_tfm
274
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000275enum {
276 CRYPTOA_UNSPEC,
277 CRYPTOA_ALG,
278};
279
280struct crypto_attr_alg {
281 char name[CRYPTO_MAX_ALG_NAME];
282};
283
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284/*
285 * Transform user interface.
286 */
287
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288struct crypto_tfm *crypto_alloc_tfm(const char *alg_name, u32 tfm_flags);
Herbert Xu6d7d6842006-07-30 11:53:01 +1000289struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290void crypto_free_tfm(struct crypto_tfm *tfm);
291
292/*
293 * Transform helpers which query the underlying algorithm.
294 */
295static inline const char *crypto_tfm_alg_name(struct crypto_tfm *tfm)
296{
297 return tfm->__crt_alg->cra_name;
298}
299
Michal Ludvigb14cdd62006-07-09 09:02:24 +1000300static inline const char *crypto_tfm_alg_driver_name(struct crypto_tfm *tfm)
301{
302 return tfm->__crt_alg->cra_driver_name;
303}
304
305static inline int crypto_tfm_alg_priority(struct crypto_tfm *tfm)
306{
307 return tfm->__crt_alg->cra_priority;
308}
309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310static inline const char *crypto_tfm_alg_modname(struct crypto_tfm *tfm)
311{
312 return module_name(tfm->__crt_alg->cra_module);
313}
314
315static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm)
316{
317 return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK;
318}
319
320static inline unsigned int crypto_tfm_alg_min_keysize(struct crypto_tfm *tfm)
321{
322 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
323 return tfm->__crt_alg->cra_cipher.cia_min_keysize;
324}
325
326static inline unsigned int crypto_tfm_alg_max_keysize(struct crypto_tfm *tfm)
327{
328 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
329 return tfm->__crt_alg->cra_cipher.cia_max_keysize;
330}
331
332static inline unsigned int crypto_tfm_alg_ivsize(struct crypto_tfm *tfm)
333{
334 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
335 return tfm->crt_cipher.cit_ivsize;
336}
337
338static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm)
339{
340 return tfm->__crt_alg->cra_blocksize;
341}
342
343static inline unsigned int crypto_tfm_alg_digestsize(struct crypto_tfm *tfm)
344{
345 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
346 return tfm->__crt_alg->cra_digest.dia_digestsize;
347}
348
Herbert Xufbdae9f2005-07-06 13:53:29 -0700349static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm *tfm)
350{
351 return tfm->__crt_alg->cra_alignmask;
352}
353
Herbert Xuf28776a2006-08-13 20:58:18 +1000354static inline u32 crypto_tfm_get_flags(struct crypto_tfm *tfm)
355{
356 return tfm->crt_flags;
357}
358
359static inline void crypto_tfm_set_flags(struct crypto_tfm *tfm, u32 flags)
360{
361 tfm->crt_flags |= flags;
362}
363
364static inline void crypto_tfm_clear_flags(struct crypto_tfm *tfm, u32 flags)
365{
366 tfm->crt_flags &= ~flags;
367}
368
Herbert Xu40725182005-07-06 13:51:52 -0700369static inline void *crypto_tfm_ctx(struct crypto_tfm *tfm)
370{
Herbert Xuf10b7892006-01-25 22:34:01 +1100371 return tfm->__crt_ctx;
372}
373
374static inline unsigned int crypto_tfm_ctx_alignment(void)
375{
376 struct crypto_tfm *tfm;
377 return __alignof__(tfm->__crt_ctx);
Herbert Xu40725182005-07-06 13:51:52 -0700378}
379
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380/*
381 * API wrappers.
382 */
Herbert Xuf28776a2006-08-13 20:58:18 +1000383static inline struct crypto_cipher *__crypto_cipher_cast(struct crypto_tfm *tfm)
384{
385 return (struct crypto_cipher *)tfm;
386}
387
388static inline struct crypto_cipher *crypto_cipher_cast(struct crypto_tfm *tfm)
389{
390 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
391 return __crypto_cipher_cast(tfm);
392}
393
394static inline struct crypto_cipher *crypto_alloc_cipher(const char *alg_name,
395 u32 type, u32 mask)
396{
397 type &= ~CRYPTO_ALG_TYPE_MASK;
398 type |= CRYPTO_ALG_TYPE_CIPHER;
399 mask |= CRYPTO_ALG_TYPE_MASK;
400
401 return __crypto_cipher_cast(crypto_alloc_base(alg_name, type, mask));
402}
403
404static inline struct crypto_tfm *crypto_cipher_tfm(struct crypto_cipher *tfm)
405{
406 return tfm;
407}
408
409static inline void crypto_free_cipher(struct crypto_cipher *tfm)
410{
411 crypto_free_tfm(crypto_cipher_tfm(tfm));
412}
413
414static inline struct cipher_tfm *crypto_cipher_crt(struct crypto_cipher *tfm)
415{
416 return &crypto_cipher_tfm(tfm)->crt_cipher;
417}
418
419static inline unsigned int crypto_cipher_blocksize(struct crypto_cipher *tfm)
420{
421 return crypto_tfm_alg_blocksize(crypto_cipher_tfm(tfm));
422}
423
424static inline unsigned int crypto_cipher_alignmask(struct crypto_cipher *tfm)
425{
426 return crypto_tfm_alg_alignmask(crypto_cipher_tfm(tfm));
427}
428
429static inline u32 crypto_cipher_get_flags(struct crypto_cipher *tfm)
430{
431 return crypto_tfm_get_flags(crypto_cipher_tfm(tfm));
432}
433
434static inline void crypto_cipher_set_flags(struct crypto_cipher *tfm,
435 u32 flags)
436{
437 crypto_tfm_set_flags(crypto_cipher_tfm(tfm), flags);
438}
439
440static inline void crypto_cipher_clear_flags(struct crypto_cipher *tfm,
441 u32 flags)
442{
443 crypto_tfm_clear_flags(crypto_cipher_tfm(tfm), flags);
444}
445
446static inline void crypto_cipher_encrypt_one(struct crypto_cipher *tfm,
447 u8 *dst, const u8 *src)
448{
449 crypto_cipher_crt(tfm)->cit_encrypt_one(crypto_cipher_tfm(tfm),
450 dst, src);
451}
452
453static inline void crypto_cipher_decrypt_one(struct crypto_cipher *tfm,
454 u8 *dst, const u8 *src)
455{
456 crypto_cipher_crt(tfm)->cit_decrypt_one(crypto_cipher_tfm(tfm),
457 dst, src);
458}
459
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460static inline void crypto_digest_init(struct crypto_tfm *tfm)
461{
462 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
463 tfm->crt_digest.dit_init(tfm);
464}
465
466static inline void crypto_digest_update(struct crypto_tfm *tfm,
467 struct scatterlist *sg,
468 unsigned int nsg)
469{
470 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
471 tfm->crt_digest.dit_update(tfm, sg, nsg);
472}
473
474static inline void crypto_digest_final(struct crypto_tfm *tfm, u8 *out)
475{
476 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
477 tfm->crt_digest.dit_final(tfm, out);
478}
479
480static inline void crypto_digest_digest(struct crypto_tfm *tfm,
481 struct scatterlist *sg,
482 unsigned int nsg, u8 *out)
483{
484 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
485 tfm->crt_digest.dit_digest(tfm, sg, nsg, out);
486}
487
488static inline int crypto_digest_setkey(struct crypto_tfm *tfm,
489 const u8 *key, unsigned int keylen)
490{
491 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 return tfm->crt_digest.dit_setkey(tfm, key, keylen);
493}
494
495static inline int crypto_cipher_setkey(struct crypto_tfm *tfm,
496 const u8 *key, unsigned int keylen)
497{
498 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
499 return tfm->crt_cipher.cit_setkey(tfm, key, keylen);
500}
501
502static inline int crypto_cipher_encrypt(struct crypto_tfm *tfm,
503 struct scatterlist *dst,
504 struct scatterlist *src,
505 unsigned int nbytes)
506{
507 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
508 return tfm->crt_cipher.cit_encrypt(tfm, dst, src, nbytes);
509}
510
511static inline int crypto_cipher_encrypt_iv(struct crypto_tfm *tfm,
512 struct scatterlist *dst,
513 struct scatterlist *src,
514 unsigned int nbytes, u8 *iv)
515{
516 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 return tfm->crt_cipher.cit_encrypt_iv(tfm, dst, src, nbytes, iv);
518}
519
520static inline int crypto_cipher_decrypt(struct crypto_tfm *tfm,
521 struct scatterlist *dst,
522 struct scatterlist *src,
523 unsigned int nbytes)
524{
525 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
526 return tfm->crt_cipher.cit_decrypt(tfm, dst, src, nbytes);
527}
528
529static inline int crypto_cipher_decrypt_iv(struct crypto_tfm *tfm,
530 struct scatterlist *dst,
531 struct scatterlist *src,
532 unsigned int nbytes, u8 *iv)
533{
534 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 return tfm->crt_cipher.cit_decrypt_iv(tfm, dst, src, nbytes, iv);
536}
537
538static inline void crypto_cipher_set_iv(struct crypto_tfm *tfm,
539 const u8 *src, unsigned int len)
540{
541 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
542 memcpy(tfm->crt_cipher.cit_iv, src, len);
543}
544
545static inline void crypto_cipher_get_iv(struct crypto_tfm *tfm,
546 u8 *dst, unsigned int len)
547{
548 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
549 memcpy(dst, tfm->crt_cipher.cit_iv, len);
550}
551
552static inline int crypto_comp_compress(struct crypto_tfm *tfm,
553 const u8 *src, unsigned int slen,
554 u8 *dst, unsigned int *dlen)
555{
556 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_COMPRESS);
557 return tfm->crt_compress.cot_compress(tfm, src, slen, dst, dlen);
558}
559
560static inline int crypto_comp_decompress(struct crypto_tfm *tfm,
561 const u8 *src, unsigned int slen,
562 u8 *dst, unsigned int *dlen)
563{
564 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_COMPRESS);
565 return tfm->crt_compress.cot_decompress(tfm, src, slen, dst, dlen);
566}
567
568/*
569 * HMAC support.
570 */
571#ifdef CONFIG_CRYPTO_HMAC
572void crypto_hmac_init(struct crypto_tfm *tfm, u8 *key, unsigned int *keylen);
573void crypto_hmac_update(struct crypto_tfm *tfm,
574 struct scatterlist *sg, unsigned int nsg);
575void crypto_hmac_final(struct crypto_tfm *tfm, u8 *key,
576 unsigned int *keylen, u8 *out);
577void crypto_hmac(struct crypto_tfm *tfm, u8 *key, unsigned int *keylen,
578 struct scatterlist *sg, unsigned int nsg, u8 *out);
579#endif /* CONFIG_CRYPTO_HMAC */
580
581#endif /* _LINUX_CRYPTO_H */
582