blob: fa7afa9b9f4f14910f13cbfb6fdc09180520ec9f [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>
John Anthony Kazos Jr18735dd2007-10-19 23:07:36 +02009 * and Nettle, by Niels Möller.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
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
Herbert Xu332f88402007-11-15 22:36:07 +080036#define CRYPTO_ALG_TYPE_ABLKCIPHER 0x00000005
Herbert Xu61da88e2007-12-17 21:51:27 +080037#define CRYPTO_ALG_TYPE_GIVCIPHER 0x00000006
Herbert Xu332f88402007-11-15 22:36:07 +080038#define CRYPTO_ALG_TYPE_COMPRESS 0x00000008
39#define CRYPTO_ALG_TYPE_AEAD 0x00000009
Herbert Xu055bcee2006-08-19 22:24:23 +100040
41#define CRYPTO_ALG_TYPE_HASH_MASK 0x0000000e
Herbert Xu332f88402007-11-15 22:36:07 +080042#define CRYPTO_ALG_TYPE_BLKCIPHER_MASK 0x0000000c
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Herbert Xu28259822006-08-06 21:23:26 +100044#define CRYPTO_ALG_LARVAL 0x00000010
Herbert Xu6bfd4802006-09-21 11:39:29 +100045#define CRYPTO_ALG_DEAD 0x00000020
46#define CRYPTO_ALG_DYING 0x00000040
Herbert Xuf3f632d2006-08-06 23:12:59 +100047#define CRYPTO_ALG_ASYNC 0x00000080
Herbert Xu28259822006-08-06 21:23:26 +100048
Linus Torvalds1da177e2005-04-16 15:20:36 -070049/*
Herbert Xu60104392006-08-26 18:34:10 +100050 * Set this bit if and only if the algorithm requires another algorithm of
51 * the same type to handle corner cases.
52 */
53#define CRYPTO_ALG_NEED_FALLBACK 0x00000100
54
55/*
Herbert Xuecfc4322007-12-05 21:08:36 +110056 * This bit is set for symmetric key ciphers that have already been wrapped
57 * with a generic IV generator to prevent them from being wrapped again.
58 */
59#define CRYPTO_ALG_GENIV 0x00000200
60
61/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 * Transform masks and values (for crt_flags).
63 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#define CRYPTO_TFM_REQ_MASK 0x000fff00
65#define CRYPTO_TFM_RES_MASK 0xfff00000
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#define CRYPTO_TFM_REQ_WEAK_KEY 0x00000100
Herbert Xu64baf3c2005-09-01 17:43:05 -070068#define CRYPTO_TFM_REQ_MAY_SLEEP 0x00000200
Herbert Xu32e39832007-03-24 14:35:34 +110069#define CRYPTO_TFM_REQ_MAY_BACKLOG 0x00000400
Linus Torvalds1da177e2005-04-16 15:20:36 -070070#define CRYPTO_TFM_RES_WEAK_KEY 0x00100000
71#define CRYPTO_TFM_RES_BAD_KEY_LEN 0x00200000
72#define CRYPTO_TFM_RES_BAD_KEY_SCHED 0x00400000
73#define CRYPTO_TFM_RES_BAD_BLOCK_LEN 0x00800000
74#define CRYPTO_TFM_RES_BAD_FLAGS 0x01000000
75
76/*
77 * Miscellaneous stuff.
78 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070079#define CRYPTO_MAX_ALG_NAME 64
80
Herbert Xu79911102006-08-21 21:03:52 +100081/*
82 * The macro CRYPTO_MINALIGN_ATTR (along with the void * type in the actual
83 * declaration) is used to ensure that the crypto_tfm context structure is
84 * aligned correctly for the given architecture so that there are no alignment
85 * faults for C data types. In particular, this is required on platforms such
86 * as arm where pointers are 32-bit aligned but there are data types such as
87 * u64 which require 64-bit alignment.
88 */
89#if defined(ARCH_KMALLOC_MINALIGN)
90#define CRYPTO_MINALIGN ARCH_KMALLOC_MINALIGN
91#elif defined(ARCH_SLAB_MINALIGN)
92#define CRYPTO_MINALIGN ARCH_SLAB_MINALIGN
93#endif
94
95#ifdef CRYPTO_MINALIGN
96#define CRYPTO_MINALIGN_ATTR __attribute__ ((__aligned__(CRYPTO_MINALIGN)))
97#else
98#define CRYPTO_MINALIGN_ATTR
99#endif
100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101struct scatterlist;
Herbert Xu32e39832007-03-24 14:35:34 +1100102struct crypto_ablkcipher;
103struct crypto_async_request;
Herbert Xu1ae97822007-08-30 15:36:14 +0800104struct crypto_aead;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000105struct crypto_blkcipher;
Herbert Xu055bcee2006-08-19 22:24:23 +1000106struct crypto_hash;
Herbert Xu40725182005-07-06 13:51:52 -0700107struct crypto_tfm;
Herbert Xue853c3c2006-08-22 00:06:54 +1000108struct crypto_type;
Herbert Xu61da88e2007-12-17 21:51:27 +0800109struct skcipher_givcrypt_request;
Herbert Xu40725182005-07-06 13:51:52 -0700110
Herbert Xu32e39832007-03-24 14:35:34 +1100111typedef void (*crypto_completion_t)(struct crypto_async_request *req, int err);
112
113struct crypto_async_request {
114 struct list_head list;
115 crypto_completion_t complete;
116 void *data;
117 struct crypto_tfm *tfm;
118
119 u32 flags;
120};
121
122struct ablkcipher_request {
123 struct crypto_async_request base;
124
125 unsigned int nbytes;
126
127 void *info;
128
129 struct scatterlist *src;
130 struct scatterlist *dst;
131
132 void *__ctx[] CRYPTO_MINALIGN_ATTR;
133};
134
Herbert Xu1ae97822007-08-30 15:36:14 +0800135/**
136 * struct aead_request - AEAD request
137 * @base: Common attributes for async crypto requests
138 * @assoclen: Length in bytes of associated data for authentication
139 * @cryptlen: Length of data to be encrypted or decrypted
140 * @iv: Initialisation vector
141 * @assoc: Associated data
142 * @src: Source data
143 * @dst: Destination data
144 * @__ctx: Start of private context data
145 */
146struct aead_request {
147 struct crypto_async_request base;
148
149 unsigned int assoclen;
150 unsigned int cryptlen;
151
152 u8 *iv;
153
154 struct scatterlist *assoc;
155 struct scatterlist *src;
156 struct scatterlist *dst;
157
158 void *__ctx[] CRYPTO_MINALIGN_ATTR;
159};
160
Herbert Xu5cde0af2006-08-22 00:07:53 +1000161struct blkcipher_desc {
162 struct crypto_blkcipher *tfm;
163 void *info;
164 u32 flags;
165};
166
Herbert Xu40725182005-07-06 13:51:52 -0700167struct cipher_desc {
168 struct crypto_tfm *tfm;
Herbert Xu6c2bb982006-05-16 22:09:29 +1000169 void (*crfn)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Herbert Xu40725182005-07-06 13:51:52 -0700170 unsigned int (*prfn)(const struct cipher_desc *desc, u8 *dst,
171 const u8 *src, unsigned int nbytes);
172 void *info;
173};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
Herbert Xu055bcee2006-08-19 22:24:23 +1000175struct hash_desc {
176 struct crypto_hash *tfm;
177 u32 flags;
178};
179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180/*
181 * Algorithms: modular crypto algorithm implementations, managed
182 * via crypto_register_alg() and crypto_unregister_alg().
183 */
Herbert Xub5b7f082007-04-16 20:48:54 +1000184struct ablkcipher_alg {
185 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
186 unsigned int keylen);
187 int (*encrypt)(struct ablkcipher_request *req);
188 int (*decrypt)(struct ablkcipher_request *req);
Herbert Xu61da88e2007-12-17 21:51:27 +0800189 int (*givencrypt)(struct skcipher_givcrypt_request *req);
190 int (*givdecrypt)(struct skcipher_givcrypt_request *req);
Herbert Xub5b7f082007-04-16 20:48:54 +1000191
Herbert Xu23508e12007-11-27 21:33:24 +0800192 const char *geniv;
193
Herbert Xub5b7f082007-04-16 20:48:54 +1000194 unsigned int min_keysize;
195 unsigned int max_keysize;
196 unsigned int ivsize;
197};
198
Herbert Xu1ae97822007-08-30 15:36:14 +0800199struct aead_alg {
200 int (*setkey)(struct crypto_aead *tfm, const u8 *key,
201 unsigned int keylen);
Herbert Xu7ba683a2007-12-02 18:49:21 +1100202 int (*setauthsize)(struct crypto_aead *tfm, unsigned int authsize);
Herbert Xu1ae97822007-08-30 15:36:14 +0800203 int (*encrypt)(struct aead_request *req);
204 int (*decrypt)(struct aead_request *req);
205
206 unsigned int ivsize;
Herbert Xu7ba683a2007-12-02 18:49:21 +1100207 unsigned int maxauthsize;
Herbert Xu1ae97822007-08-30 15:36:14 +0800208};
209
Herbert Xu5cde0af2006-08-22 00:07:53 +1000210struct blkcipher_alg {
211 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
212 unsigned int keylen);
213 int (*encrypt)(struct blkcipher_desc *desc,
214 struct scatterlist *dst, struct scatterlist *src,
215 unsigned int nbytes);
216 int (*decrypt)(struct blkcipher_desc *desc,
217 struct scatterlist *dst, struct scatterlist *src,
218 unsigned int nbytes);
219
Herbert Xu23508e12007-11-27 21:33:24 +0800220 const char *geniv;
221
Herbert Xu5cde0af2006-08-22 00:07:53 +1000222 unsigned int min_keysize;
223 unsigned int max_keysize;
224 unsigned int ivsize;
225};
226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227struct cipher_alg {
228 unsigned int cia_min_keysize;
229 unsigned int cia_max_keysize;
Herbert Xu6c2bb982006-05-16 22:09:29 +1000230 int (*cia_setkey)(struct crypto_tfm *tfm, const u8 *key,
Herbert Xu560c06a2006-08-13 14:16:39 +1000231 unsigned int keylen);
Herbert Xu6c2bb982006-05-16 22:09:29 +1000232 void (*cia_encrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
233 void (*cia_decrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234};
235
236struct digest_alg {
237 unsigned int dia_digestsize;
Herbert Xu6c2bb982006-05-16 22:09:29 +1000238 void (*dia_init)(struct crypto_tfm *tfm);
239 void (*dia_update)(struct crypto_tfm *tfm, const u8 *data,
240 unsigned int len);
241 void (*dia_final)(struct crypto_tfm *tfm, u8 *out);
242 int (*dia_setkey)(struct crypto_tfm *tfm, const u8 *key,
Herbert Xu560c06a2006-08-13 14:16:39 +1000243 unsigned int keylen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244};
245
Herbert Xu055bcee2006-08-19 22:24:23 +1000246struct hash_alg {
247 int (*init)(struct hash_desc *desc);
248 int (*update)(struct hash_desc *desc, struct scatterlist *sg,
249 unsigned int nbytes);
250 int (*final)(struct hash_desc *desc, u8 *out);
251 int (*digest)(struct hash_desc *desc, struct scatterlist *sg,
252 unsigned int nbytes, u8 *out);
253 int (*setkey)(struct crypto_hash *tfm, const u8 *key,
254 unsigned int keylen);
255
256 unsigned int digestsize;
257};
258
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259struct compress_alg {
Herbert Xu6c2bb982006-05-16 22:09:29 +1000260 int (*coa_compress)(struct crypto_tfm *tfm, const u8 *src,
261 unsigned int slen, u8 *dst, unsigned int *dlen);
262 int (*coa_decompress)(struct crypto_tfm *tfm, const u8 *src,
263 unsigned int slen, u8 *dst, unsigned int *dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264};
265
Herbert Xub5b7f082007-04-16 20:48:54 +1000266#define cra_ablkcipher cra_u.ablkcipher
Herbert Xu1ae97822007-08-30 15:36:14 +0800267#define cra_aead cra_u.aead
Herbert Xu5cde0af2006-08-22 00:07:53 +1000268#define cra_blkcipher cra_u.blkcipher
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269#define cra_cipher cra_u.cipher
270#define cra_digest cra_u.digest
Herbert Xu055bcee2006-08-19 22:24:23 +1000271#define cra_hash cra_u.hash
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272#define cra_compress cra_u.compress
273
274struct crypto_alg {
275 struct list_head cra_list;
Herbert Xu6bfd4802006-09-21 11:39:29 +1000276 struct list_head cra_users;
277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 u32 cra_flags;
279 unsigned int cra_blocksize;
280 unsigned int cra_ctxsize;
Herbert Xu95477372005-07-06 13:52:09 -0700281 unsigned int cra_alignmask;
Herbert Xu5cb14542005-11-05 16:58:14 +1100282
283 int cra_priority;
Herbert Xu6521f302006-08-06 20:28:44 +1000284 atomic_t cra_refcnt;
Herbert Xu5cb14542005-11-05 16:58:14 +1100285
Herbert Xud913ea02006-05-21 08:45:26 +1000286 char cra_name[CRYPTO_MAX_ALG_NAME];
287 char cra_driver_name[CRYPTO_MAX_ALG_NAME];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
Herbert Xue853c3c2006-08-22 00:06:54 +1000289 const struct crypto_type *cra_type;
290
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 union {
Herbert Xub5b7f082007-04-16 20:48:54 +1000292 struct ablkcipher_alg ablkcipher;
Herbert Xu1ae97822007-08-30 15:36:14 +0800293 struct aead_alg aead;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000294 struct blkcipher_alg blkcipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 struct cipher_alg cipher;
296 struct digest_alg digest;
Herbert Xu055bcee2006-08-19 22:24:23 +1000297 struct hash_alg hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 struct compress_alg compress;
299 } cra_u;
Herbert Xuc7fc0592006-05-24 13:02:26 +1000300
301 int (*cra_init)(struct crypto_tfm *tfm);
302 void (*cra_exit)(struct crypto_tfm *tfm);
Herbert Xu6521f302006-08-06 20:28:44 +1000303 void (*cra_destroy)(struct crypto_alg *alg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
305 struct module *cra_module;
306};
307
308/*
309 * Algorithm registration interface.
310 */
311int crypto_register_alg(struct crypto_alg *alg);
312int crypto_unregister_alg(struct crypto_alg *alg);
313
314/*
315 * Algorithm query interface.
316 */
317#ifdef CONFIG_CRYPTO
Herbert Xufce32d72006-08-26 17:35:45 +1000318int crypto_has_alg(const char *name, u32 type, u32 mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319#else
Herbert Xufce32d72006-08-26 17:35:45 +1000320static inline int crypto_has_alg(const char *name, u32 type, u32 mask)
321{
322 return 0;
323}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324#endif
325
326/*
327 * Transforms: user-instantiated objects which encapsulate algorithms
Herbert Xu6d7d6842006-07-30 11:53:01 +1000328 * and core processing logic. Managed via crypto_alloc_*() and
329 * crypto_free_*(), as well as the various helpers below.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
Herbert Xu32e39832007-03-24 14:35:34 +1100332struct ablkcipher_tfm {
333 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
334 unsigned int keylen);
335 int (*encrypt)(struct ablkcipher_request *req);
336 int (*decrypt)(struct ablkcipher_request *req);
Herbert Xu61da88e2007-12-17 21:51:27 +0800337 int (*givencrypt)(struct skcipher_givcrypt_request *req);
338 int (*givdecrypt)(struct skcipher_givcrypt_request *req);
339
Herbert Xuecfc4322007-12-05 21:08:36 +1100340 struct crypto_ablkcipher *base;
341
Herbert Xu32e39832007-03-24 14:35:34 +1100342 unsigned int ivsize;
343 unsigned int reqsize;
344};
345
Herbert Xu1ae97822007-08-30 15:36:14 +0800346struct aead_tfm {
347 int (*setkey)(struct crypto_aead *tfm, const u8 *key,
348 unsigned int keylen);
349 int (*encrypt)(struct aead_request *req);
350 int (*decrypt)(struct aead_request *req);
351 unsigned int ivsize;
352 unsigned int authsize;
353 unsigned int reqsize;
354};
355
Herbert Xu5cde0af2006-08-22 00:07:53 +1000356struct blkcipher_tfm {
357 void *iv;
358 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
359 unsigned int keylen);
360 int (*encrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
361 struct scatterlist *src, unsigned int nbytes);
362 int (*decrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
363 struct scatterlist *src, unsigned int nbytes);
364};
365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366struct cipher_tfm {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 int (*cit_setkey)(struct crypto_tfm *tfm,
368 const u8 *key, unsigned int keylen);
Herbert Xuf28776a2006-08-13 20:58:18 +1000369 void (*cit_encrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
370 void (*cit_decrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371};
372
Herbert Xu055bcee2006-08-19 22:24:23 +1000373struct hash_tfm {
374 int (*init)(struct hash_desc *desc);
375 int (*update)(struct hash_desc *desc,
376 struct scatterlist *sg, unsigned int nsg);
377 int (*final)(struct hash_desc *desc, u8 *out);
378 int (*digest)(struct hash_desc *desc, struct scatterlist *sg,
379 unsigned int nsg, u8 *out);
380 int (*setkey)(struct crypto_hash *tfm, const u8 *key,
381 unsigned int keylen);
Herbert Xu055bcee2006-08-19 22:24:23 +1000382 unsigned int digestsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383};
384
385struct compress_tfm {
386 int (*cot_compress)(struct crypto_tfm *tfm,
387 const u8 *src, unsigned int slen,
388 u8 *dst, unsigned int *dlen);
389 int (*cot_decompress)(struct crypto_tfm *tfm,
390 const u8 *src, unsigned int slen,
391 u8 *dst, unsigned int *dlen);
392};
393
Herbert Xu32e39832007-03-24 14:35:34 +1100394#define crt_ablkcipher crt_u.ablkcipher
Herbert Xu1ae97822007-08-30 15:36:14 +0800395#define crt_aead crt_u.aead
Herbert Xu5cde0af2006-08-22 00:07:53 +1000396#define crt_blkcipher crt_u.blkcipher
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397#define crt_cipher crt_u.cipher
Herbert Xu055bcee2006-08-19 22:24:23 +1000398#define crt_hash crt_u.hash
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399#define crt_compress crt_u.compress
400
401struct crypto_tfm {
402
403 u32 crt_flags;
404
405 union {
Herbert Xu32e39832007-03-24 14:35:34 +1100406 struct ablkcipher_tfm ablkcipher;
Herbert Xu1ae97822007-08-30 15:36:14 +0800407 struct aead_tfm aead;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000408 struct blkcipher_tfm blkcipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 struct cipher_tfm cipher;
Herbert Xu055bcee2006-08-19 22:24:23 +1000410 struct hash_tfm hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 struct compress_tfm compress;
412 } crt_u;
413
414 struct crypto_alg *__crt_alg;
Herbert Xuf10b7892006-01-25 22:34:01 +1100415
Herbert Xu79911102006-08-21 21:03:52 +1000416 void *__crt_ctx[] CRYPTO_MINALIGN_ATTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417};
418
Herbert Xu32e39832007-03-24 14:35:34 +1100419struct crypto_ablkcipher {
420 struct crypto_tfm base;
421};
422
Herbert Xu1ae97822007-08-30 15:36:14 +0800423struct crypto_aead {
424 struct crypto_tfm base;
425};
426
Herbert Xu5cde0af2006-08-22 00:07:53 +1000427struct crypto_blkcipher {
428 struct crypto_tfm base;
429};
430
Herbert Xu78a1fe42006-12-24 10:02:00 +1100431struct crypto_cipher {
432 struct crypto_tfm base;
433};
434
435struct crypto_comp {
436 struct crypto_tfm base;
437};
438
Herbert Xu055bcee2006-08-19 22:24:23 +1000439struct crypto_hash {
440 struct crypto_tfm base;
441};
442
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000443enum {
444 CRYPTOA_UNSPEC,
445 CRYPTOA_ALG,
Herbert Xuebc610e2007-01-01 18:37:02 +1100446 CRYPTOA_TYPE,
Herbert Xu39e1ee012007-08-29 19:27:26 +0800447 CRYPTOA_U32,
Herbert Xuebc610e2007-01-01 18:37:02 +1100448 __CRYPTOA_MAX,
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000449};
450
Herbert Xuebc610e2007-01-01 18:37:02 +1100451#define CRYPTOA_MAX (__CRYPTOA_MAX - 1)
452
Herbert Xu39e1ee012007-08-29 19:27:26 +0800453/* Maximum number of (rtattr) parameters for each template. */
454#define CRYPTO_MAX_ATTRS 32
455
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000456struct crypto_attr_alg {
457 char name[CRYPTO_MAX_ALG_NAME];
458};
459
Herbert Xuebc610e2007-01-01 18:37:02 +1100460struct crypto_attr_type {
461 u32 type;
462 u32 mask;
463};
464
Herbert Xu39e1ee012007-08-29 19:27:26 +0800465struct crypto_attr_u32 {
466 u32 num;
467};
468
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469/*
470 * Transform user interface.
471 */
472
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473struct crypto_tfm *crypto_alloc_tfm(const char *alg_name, u32 tfm_flags);
Herbert Xu6d7d6842006-07-30 11:53:01 +1000474struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475void crypto_free_tfm(struct crypto_tfm *tfm);
476
477/*
478 * Transform helpers which query the underlying algorithm.
479 */
480static inline const char *crypto_tfm_alg_name(struct crypto_tfm *tfm)
481{
482 return tfm->__crt_alg->cra_name;
483}
484
Michal Ludvigb14cdd62006-07-09 09:02:24 +1000485static inline const char *crypto_tfm_alg_driver_name(struct crypto_tfm *tfm)
486{
487 return tfm->__crt_alg->cra_driver_name;
488}
489
490static inline int crypto_tfm_alg_priority(struct crypto_tfm *tfm)
491{
492 return tfm->__crt_alg->cra_priority;
493}
494
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495static inline const char *crypto_tfm_alg_modname(struct crypto_tfm *tfm)
496{
497 return module_name(tfm->__crt_alg->cra_module);
498}
499
500static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm)
501{
502 return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK;
503}
504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm)
506{
507 return tfm->__crt_alg->cra_blocksize;
508}
509
Herbert Xufbdae9f2005-07-06 13:53:29 -0700510static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm *tfm)
511{
512 return tfm->__crt_alg->cra_alignmask;
513}
514
Herbert Xuf28776a2006-08-13 20:58:18 +1000515static inline u32 crypto_tfm_get_flags(struct crypto_tfm *tfm)
516{
517 return tfm->crt_flags;
518}
519
520static inline void crypto_tfm_set_flags(struct crypto_tfm *tfm, u32 flags)
521{
522 tfm->crt_flags |= flags;
523}
524
525static inline void crypto_tfm_clear_flags(struct crypto_tfm *tfm, u32 flags)
526{
527 tfm->crt_flags &= ~flags;
528}
529
Herbert Xu40725182005-07-06 13:51:52 -0700530static inline void *crypto_tfm_ctx(struct crypto_tfm *tfm)
531{
Herbert Xuf10b7892006-01-25 22:34:01 +1100532 return tfm->__crt_ctx;
533}
534
535static inline unsigned int crypto_tfm_ctx_alignment(void)
536{
537 struct crypto_tfm *tfm;
538 return __alignof__(tfm->__crt_ctx);
Herbert Xu40725182005-07-06 13:51:52 -0700539}
540
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541/*
542 * API wrappers.
543 */
Herbert Xu32e39832007-03-24 14:35:34 +1100544static inline struct crypto_ablkcipher *__crypto_ablkcipher_cast(
545 struct crypto_tfm *tfm)
546{
547 return (struct crypto_ablkcipher *)tfm;
548}
549
Herbert Xu378f4f52007-12-17 20:07:31 +0800550static inline u32 crypto_skcipher_type(u32 type)
551{
Herbert Xuecfc4322007-12-05 21:08:36 +1100552 type &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
Herbert Xu378f4f52007-12-17 20:07:31 +0800553 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
554 return type;
555}
556
557static inline u32 crypto_skcipher_mask(u32 mask)
558{
Herbert Xuecfc4322007-12-05 21:08:36 +1100559 mask &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
Herbert Xu378f4f52007-12-17 20:07:31 +0800560 mask |= CRYPTO_ALG_TYPE_BLKCIPHER_MASK;
561 return mask;
562}
563
Herbert Xu32e39832007-03-24 14:35:34 +1100564static inline struct crypto_ablkcipher *crypto_alloc_ablkcipher(
565 const char *alg_name, u32 type, u32 mask)
566{
Herbert Xu32e39832007-03-24 14:35:34 +1100567 return __crypto_ablkcipher_cast(
Herbert Xu378f4f52007-12-17 20:07:31 +0800568 crypto_alloc_base(alg_name, crypto_skcipher_type(type),
569 crypto_skcipher_mask(mask)));
Herbert Xu32e39832007-03-24 14:35:34 +1100570}
571
572static inline struct crypto_tfm *crypto_ablkcipher_tfm(
573 struct crypto_ablkcipher *tfm)
574{
575 return &tfm->base;
576}
577
578static inline void crypto_free_ablkcipher(struct crypto_ablkcipher *tfm)
579{
580 crypto_free_tfm(crypto_ablkcipher_tfm(tfm));
581}
582
583static inline int crypto_has_ablkcipher(const char *alg_name, u32 type,
584 u32 mask)
585{
Herbert Xu378f4f52007-12-17 20:07:31 +0800586 return crypto_has_alg(alg_name, crypto_skcipher_type(type),
587 crypto_skcipher_mask(mask));
Herbert Xu32e39832007-03-24 14:35:34 +1100588}
589
590static inline struct ablkcipher_tfm *crypto_ablkcipher_crt(
591 struct crypto_ablkcipher *tfm)
592{
593 return &crypto_ablkcipher_tfm(tfm)->crt_ablkcipher;
594}
595
596static inline unsigned int crypto_ablkcipher_ivsize(
597 struct crypto_ablkcipher *tfm)
598{
599 return crypto_ablkcipher_crt(tfm)->ivsize;
600}
601
602static inline unsigned int crypto_ablkcipher_blocksize(
603 struct crypto_ablkcipher *tfm)
604{
605 return crypto_tfm_alg_blocksize(crypto_ablkcipher_tfm(tfm));
606}
607
608static inline unsigned int crypto_ablkcipher_alignmask(
609 struct crypto_ablkcipher *tfm)
610{
611 return crypto_tfm_alg_alignmask(crypto_ablkcipher_tfm(tfm));
612}
613
614static inline u32 crypto_ablkcipher_get_flags(struct crypto_ablkcipher *tfm)
615{
616 return crypto_tfm_get_flags(crypto_ablkcipher_tfm(tfm));
617}
618
619static inline void crypto_ablkcipher_set_flags(struct crypto_ablkcipher *tfm,
620 u32 flags)
621{
622 crypto_tfm_set_flags(crypto_ablkcipher_tfm(tfm), flags);
623}
624
625static inline void crypto_ablkcipher_clear_flags(struct crypto_ablkcipher *tfm,
626 u32 flags)
627{
628 crypto_tfm_clear_flags(crypto_ablkcipher_tfm(tfm), flags);
629}
630
631static inline int crypto_ablkcipher_setkey(struct crypto_ablkcipher *tfm,
632 const u8 *key, unsigned int keylen)
633{
Herbert Xuecfc4322007-12-05 21:08:36 +1100634 struct ablkcipher_tfm *crt = crypto_ablkcipher_crt(tfm);
635
636 return crt->setkey(crt->base, key, keylen);
Herbert Xu32e39832007-03-24 14:35:34 +1100637}
638
639static inline struct crypto_ablkcipher *crypto_ablkcipher_reqtfm(
640 struct ablkcipher_request *req)
641{
642 return __crypto_ablkcipher_cast(req->base.tfm);
643}
644
645static inline int crypto_ablkcipher_encrypt(struct ablkcipher_request *req)
646{
647 struct ablkcipher_tfm *crt =
648 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
649 return crt->encrypt(req);
650}
651
652static inline int crypto_ablkcipher_decrypt(struct ablkcipher_request *req)
653{
654 struct ablkcipher_tfm *crt =
655 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
656 return crt->decrypt(req);
657}
658
Herbert Xub16c3a22007-08-29 19:02:04 +0800659static inline unsigned int crypto_ablkcipher_reqsize(
660 struct crypto_ablkcipher *tfm)
Herbert Xu32e39832007-03-24 14:35:34 +1100661{
662 return crypto_ablkcipher_crt(tfm)->reqsize;
663}
664
Herbert Xue196d622007-04-14 16:09:14 +1000665static inline void ablkcipher_request_set_tfm(
666 struct ablkcipher_request *req, struct crypto_ablkcipher *tfm)
667{
Herbert Xuecfc4322007-12-05 21:08:36 +1100668 req->base.tfm = crypto_ablkcipher_tfm(crypto_ablkcipher_crt(tfm)->base);
Herbert Xue196d622007-04-14 16:09:14 +1000669}
670
Herbert Xub5b7f082007-04-16 20:48:54 +1000671static inline struct ablkcipher_request *ablkcipher_request_cast(
672 struct crypto_async_request *req)
673{
674 return container_of(req, struct ablkcipher_request, base);
675}
676
Herbert Xu32e39832007-03-24 14:35:34 +1100677static inline struct ablkcipher_request *ablkcipher_request_alloc(
678 struct crypto_ablkcipher *tfm, gfp_t gfp)
679{
680 struct ablkcipher_request *req;
681
682 req = kmalloc(sizeof(struct ablkcipher_request) +
683 crypto_ablkcipher_reqsize(tfm), gfp);
684
685 if (likely(req))
Herbert Xue196d622007-04-14 16:09:14 +1000686 ablkcipher_request_set_tfm(req, tfm);
Herbert Xu32e39832007-03-24 14:35:34 +1100687
688 return req;
689}
690
691static inline void ablkcipher_request_free(struct ablkcipher_request *req)
692{
693 kfree(req);
694}
695
696static inline void ablkcipher_request_set_callback(
697 struct ablkcipher_request *req,
698 u32 flags, crypto_completion_t complete, void *data)
699{
700 req->base.complete = complete;
701 req->base.data = data;
702 req->base.flags = flags;
703}
704
705static inline void ablkcipher_request_set_crypt(
706 struct ablkcipher_request *req,
707 struct scatterlist *src, struct scatterlist *dst,
708 unsigned int nbytes, void *iv)
709{
710 req->src = src;
711 req->dst = dst;
712 req->nbytes = nbytes;
713 req->info = iv;
714}
715
Herbert Xu1ae97822007-08-30 15:36:14 +0800716static inline struct crypto_aead *__crypto_aead_cast(struct crypto_tfm *tfm)
717{
718 return (struct crypto_aead *)tfm;
719}
720
721static inline struct crypto_aead *crypto_alloc_aead(const char *alg_name,
722 u32 type, u32 mask)
723{
724 type &= ~CRYPTO_ALG_TYPE_MASK;
725 type |= CRYPTO_ALG_TYPE_AEAD;
726 mask |= CRYPTO_ALG_TYPE_MASK;
727
728 return __crypto_aead_cast(crypto_alloc_base(alg_name, type, mask));
729}
730
731static inline struct crypto_tfm *crypto_aead_tfm(struct crypto_aead *tfm)
732{
733 return &tfm->base;
734}
735
736static inline void crypto_free_aead(struct crypto_aead *tfm)
737{
738 crypto_free_tfm(crypto_aead_tfm(tfm));
739}
740
741static inline struct aead_tfm *crypto_aead_crt(struct crypto_aead *tfm)
742{
743 return &crypto_aead_tfm(tfm)->crt_aead;
744}
745
746static inline unsigned int crypto_aead_ivsize(struct crypto_aead *tfm)
747{
748 return crypto_aead_crt(tfm)->ivsize;
749}
750
751static inline unsigned int crypto_aead_authsize(struct crypto_aead *tfm)
752{
753 return crypto_aead_crt(tfm)->authsize;
754}
755
756static inline unsigned int crypto_aead_blocksize(struct crypto_aead *tfm)
757{
758 return crypto_tfm_alg_blocksize(crypto_aead_tfm(tfm));
759}
760
761static inline unsigned int crypto_aead_alignmask(struct crypto_aead *tfm)
762{
763 return crypto_tfm_alg_alignmask(crypto_aead_tfm(tfm));
764}
765
766static inline u32 crypto_aead_get_flags(struct crypto_aead *tfm)
767{
768 return crypto_tfm_get_flags(crypto_aead_tfm(tfm));
769}
770
771static inline void crypto_aead_set_flags(struct crypto_aead *tfm, u32 flags)
772{
773 crypto_tfm_set_flags(crypto_aead_tfm(tfm), flags);
774}
775
776static inline void crypto_aead_clear_flags(struct crypto_aead *tfm, u32 flags)
777{
778 crypto_tfm_clear_flags(crypto_aead_tfm(tfm), flags);
779}
780
781static inline int crypto_aead_setkey(struct crypto_aead *tfm, const u8 *key,
782 unsigned int keylen)
783{
784 return crypto_aead_crt(tfm)->setkey(tfm, key, keylen);
785}
786
Herbert Xu7ba683a2007-12-02 18:49:21 +1100787int crypto_aead_setauthsize(struct crypto_aead *tfm, unsigned int authsize);
788
Herbert Xu1ae97822007-08-30 15:36:14 +0800789static inline struct crypto_aead *crypto_aead_reqtfm(struct aead_request *req)
790{
791 return __crypto_aead_cast(req->base.tfm);
792}
793
794static inline int crypto_aead_encrypt(struct aead_request *req)
795{
796 return crypto_aead_crt(crypto_aead_reqtfm(req))->encrypt(req);
797}
798
799static inline int crypto_aead_decrypt(struct aead_request *req)
800{
801 return crypto_aead_crt(crypto_aead_reqtfm(req))->decrypt(req);
802}
803
Herbert Xub16c3a22007-08-29 19:02:04 +0800804static inline unsigned int crypto_aead_reqsize(struct crypto_aead *tfm)
Herbert Xu1ae97822007-08-30 15:36:14 +0800805{
806 return crypto_aead_crt(tfm)->reqsize;
807}
808
809static inline void aead_request_set_tfm(struct aead_request *req,
810 struct crypto_aead *tfm)
811{
812 req->base.tfm = crypto_aead_tfm(tfm);
813}
814
815static inline struct aead_request *aead_request_alloc(struct crypto_aead *tfm,
816 gfp_t gfp)
817{
818 struct aead_request *req;
819
820 req = kmalloc(sizeof(*req) + crypto_aead_reqsize(tfm), gfp);
821
822 if (likely(req))
823 aead_request_set_tfm(req, tfm);
824
825 return req;
826}
827
828static inline void aead_request_free(struct aead_request *req)
829{
830 kfree(req);
831}
832
833static inline void aead_request_set_callback(struct aead_request *req,
834 u32 flags,
835 crypto_completion_t complete,
836 void *data)
837{
838 req->base.complete = complete;
839 req->base.data = data;
840 req->base.flags = flags;
841}
842
843static inline void aead_request_set_crypt(struct aead_request *req,
844 struct scatterlist *src,
845 struct scatterlist *dst,
846 unsigned int cryptlen, u8 *iv)
847{
848 req->src = src;
849 req->dst = dst;
850 req->cryptlen = cryptlen;
851 req->iv = iv;
852}
853
854static inline void aead_request_set_assoc(struct aead_request *req,
855 struct scatterlist *assoc,
856 unsigned int assoclen)
857{
858 req->assoc = assoc;
859 req->assoclen = assoclen;
860}
861
Herbert Xu5cde0af2006-08-22 00:07:53 +1000862static inline struct crypto_blkcipher *__crypto_blkcipher_cast(
863 struct crypto_tfm *tfm)
864{
865 return (struct crypto_blkcipher *)tfm;
866}
867
868static inline struct crypto_blkcipher *crypto_blkcipher_cast(
869 struct crypto_tfm *tfm)
870{
871 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_BLKCIPHER);
872 return __crypto_blkcipher_cast(tfm);
873}
874
875static inline struct crypto_blkcipher *crypto_alloc_blkcipher(
876 const char *alg_name, u32 type, u32 mask)
877{
Herbert Xu332f88402007-11-15 22:36:07 +0800878 type &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000879 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
Herbert Xu332f88402007-11-15 22:36:07 +0800880 mask |= CRYPTO_ALG_TYPE_MASK;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000881
882 return __crypto_blkcipher_cast(crypto_alloc_base(alg_name, type, mask));
883}
884
885static inline struct crypto_tfm *crypto_blkcipher_tfm(
886 struct crypto_blkcipher *tfm)
887{
888 return &tfm->base;
889}
890
891static inline void crypto_free_blkcipher(struct crypto_blkcipher *tfm)
892{
893 crypto_free_tfm(crypto_blkcipher_tfm(tfm));
894}
895
Herbert Xufce32d72006-08-26 17:35:45 +1000896static inline int crypto_has_blkcipher(const char *alg_name, u32 type, u32 mask)
897{
Herbert Xu332f88402007-11-15 22:36:07 +0800898 type &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xufce32d72006-08-26 17:35:45 +1000899 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
Herbert Xu332f88402007-11-15 22:36:07 +0800900 mask |= CRYPTO_ALG_TYPE_MASK;
Herbert Xufce32d72006-08-26 17:35:45 +1000901
902 return crypto_has_alg(alg_name, type, mask);
903}
904
Herbert Xu5cde0af2006-08-22 00:07:53 +1000905static inline const char *crypto_blkcipher_name(struct crypto_blkcipher *tfm)
906{
907 return crypto_tfm_alg_name(crypto_blkcipher_tfm(tfm));
908}
909
910static inline struct blkcipher_tfm *crypto_blkcipher_crt(
911 struct crypto_blkcipher *tfm)
912{
913 return &crypto_blkcipher_tfm(tfm)->crt_blkcipher;
914}
915
916static inline struct blkcipher_alg *crypto_blkcipher_alg(
917 struct crypto_blkcipher *tfm)
918{
919 return &crypto_blkcipher_tfm(tfm)->__crt_alg->cra_blkcipher;
920}
921
922static inline unsigned int crypto_blkcipher_ivsize(struct crypto_blkcipher *tfm)
923{
924 return crypto_blkcipher_alg(tfm)->ivsize;
925}
926
927static inline unsigned int crypto_blkcipher_blocksize(
928 struct crypto_blkcipher *tfm)
929{
930 return crypto_tfm_alg_blocksize(crypto_blkcipher_tfm(tfm));
931}
932
933static inline unsigned int crypto_blkcipher_alignmask(
934 struct crypto_blkcipher *tfm)
935{
936 return crypto_tfm_alg_alignmask(crypto_blkcipher_tfm(tfm));
937}
938
939static inline u32 crypto_blkcipher_get_flags(struct crypto_blkcipher *tfm)
940{
941 return crypto_tfm_get_flags(crypto_blkcipher_tfm(tfm));
942}
943
944static inline void crypto_blkcipher_set_flags(struct crypto_blkcipher *tfm,
945 u32 flags)
946{
947 crypto_tfm_set_flags(crypto_blkcipher_tfm(tfm), flags);
948}
949
950static inline void crypto_blkcipher_clear_flags(struct crypto_blkcipher *tfm,
951 u32 flags)
952{
953 crypto_tfm_clear_flags(crypto_blkcipher_tfm(tfm), flags);
954}
955
956static inline int crypto_blkcipher_setkey(struct crypto_blkcipher *tfm,
957 const u8 *key, unsigned int keylen)
958{
959 return crypto_blkcipher_crt(tfm)->setkey(crypto_blkcipher_tfm(tfm),
960 key, keylen);
961}
962
963static inline int crypto_blkcipher_encrypt(struct blkcipher_desc *desc,
964 struct scatterlist *dst,
965 struct scatterlist *src,
966 unsigned int nbytes)
967{
968 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
969 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
970}
971
972static inline int crypto_blkcipher_encrypt_iv(struct blkcipher_desc *desc,
973 struct scatterlist *dst,
974 struct scatterlist *src,
975 unsigned int nbytes)
976{
977 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
978}
979
980static inline int crypto_blkcipher_decrypt(struct blkcipher_desc *desc,
981 struct scatterlist *dst,
982 struct scatterlist *src,
983 unsigned int nbytes)
984{
985 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
986 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
987}
988
989static inline int crypto_blkcipher_decrypt_iv(struct blkcipher_desc *desc,
990 struct scatterlist *dst,
991 struct scatterlist *src,
992 unsigned int nbytes)
993{
994 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
995}
996
997static inline void crypto_blkcipher_set_iv(struct crypto_blkcipher *tfm,
998 const u8 *src, unsigned int len)
999{
1000 memcpy(crypto_blkcipher_crt(tfm)->iv, src, len);
1001}
1002
1003static inline void crypto_blkcipher_get_iv(struct crypto_blkcipher *tfm,
1004 u8 *dst, unsigned int len)
1005{
1006 memcpy(dst, crypto_blkcipher_crt(tfm)->iv, len);
1007}
1008
Herbert Xuf28776a2006-08-13 20:58:18 +10001009static inline struct crypto_cipher *__crypto_cipher_cast(struct crypto_tfm *tfm)
1010{
1011 return (struct crypto_cipher *)tfm;
1012}
1013
1014static inline struct crypto_cipher *crypto_cipher_cast(struct crypto_tfm *tfm)
1015{
1016 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
1017 return __crypto_cipher_cast(tfm);
1018}
1019
1020static inline struct crypto_cipher *crypto_alloc_cipher(const char *alg_name,
1021 u32 type, u32 mask)
1022{
1023 type &= ~CRYPTO_ALG_TYPE_MASK;
1024 type |= CRYPTO_ALG_TYPE_CIPHER;
1025 mask |= CRYPTO_ALG_TYPE_MASK;
1026
1027 return __crypto_cipher_cast(crypto_alloc_base(alg_name, type, mask));
1028}
1029
1030static inline struct crypto_tfm *crypto_cipher_tfm(struct crypto_cipher *tfm)
1031{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001032 return &tfm->base;
Herbert Xuf28776a2006-08-13 20:58:18 +10001033}
1034
1035static inline void crypto_free_cipher(struct crypto_cipher *tfm)
1036{
1037 crypto_free_tfm(crypto_cipher_tfm(tfm));
1038}
1039
Herbert Xufce32d72006-08-26 17:35:45 +10001040static inline int crypto_has_cipher(const char *alg_name, u32 type, u32 mask)
1041{
1042 type &= ~CRYPTO_ALG_TYPE_MASK;
1043 type |= CRYPTO_ALG_TYPE_CIPHER;
1044 mask |= CRYPTO_ALG_TYPE_MASK;
1045
1046 return crypto_has_alg(alg_name, type, mask);
1047}
1048
Herbert Xuf28776a2006-08-13 20:58:18 +10001049static inline struct cipher_tfm *crypto_cipher_crt(struct crypto_cipher *tfm)
1050{
1051 return &crypto_cipher_tfm(tfm)->crt_cipher;
1052}
1053
1054static inline unsigned int crypto_cipher_blocksize(struct crypto_cipher *tfm)
1055{
1056 return crypto_tfm_alg_blocksize(crypto_cipher_tfm(tfm));
1057}
1058
1059static inline unsigned int crypto_cipher_alignmask(struct crypto_cipher *tfm)
1060{
1061 return crypto_tfm_alg_alignmask(crypto_cipher_tfm(tfm));
1062}
1063
1064static inline u32 crypto_cipher_get_flags(struct crypto_cipher *tfm)
1065{
1066 return crypto_tfm_get_flags(crypto_cipher_tfm(tfm));
1067}
1068
1069static inline void crypto_cipher_set_flags(struct crypto_cipher *tfm,
1070 u32 flags)
1071{
1072 crypto_tfm_set_flags(crypto_cipher_tfm(tfm), flags);
1073}
1074
1075static inline void crypto_cipher_clear_flags(struct crypto_cipher *tfm,
1076 u32 flags)
1077{
1078 crypto_tfm_clear_flags(crypto_cipher_tfm(tfm), flags);
1079}
1080
Herbert Xu7226bc872006-08-21 21:40:49 +10001081static inline int crypto_cipher_setkey(struct crypto_cipher *tfm,
1082 const u8 *key, unsigned int keylen)
1083{
1084 return crypto_cipher_crt(tfm)->cit_setkey(crypto_cipher_tfm(tfm),
1085 key, keylen);
1086}
1087
Herbert Xuf28776a2006-08-13 20:58:18 +10001088static inline void crypto_cipher_encrypt_one(struct crypto_cipher *tfm,
1089 u8 *dst, const u8 *src)
1090{
1091 crypto_cipher_crt(tfm)->cit_encrypt_one(crypto_cipher_tfm(tfm),
1092 dst, src);
1093}
1094
1095static inline void crypto_cipher_decrypt_one(struct crypto_cipher *tfm,
1096 u8 *dst, const u8 *src)
1097{
1098 crypto_cipher_crt(tfm)->cit_decrypt_one(crypto_cipher_tfm(tfm),
1099 dst, src);
1100}
1101
Herbert Xu055bcee2006-08-19 22:24:23 +10001102static inline struct crypto_hash *__crypto_hash_cast(struct crypto_tfm *tfm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103{
Herbert Xu055bcee2006-08-19 22:24:23 +10001104 return (struct crypto_hash *)tfm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105}
1106
Herbert Xu055bcee2006-08-19 22:24:23 +10001107static inline struct crypto_hash *crypto_hash_cast(struct crypto_tfm *tfm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108{
Herbert Xu055bcee2006-08-19 22:24:23 +10001109 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_HASH) &
1110 CRYPTO_ALG_TYPE_HASH_MASK);
1111 return __crypto_hash_cast(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112}
1113
Herbert Xu055bcee2006-08-19 22:24:23 +10001114static inline struct crypto_hash *crypto_alloc_hash(const char *alg_name,
1115 u32 type, u32 mask)
1116{
1117 type &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xu551a09a2007-12-01 21:47:07 +11001118 mask &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xu055bcee2006-08-19 22:24:23 +10001119 type |= CRYPTO_ALG_TYPE_HASH;
1120 mask |= CRYPTO_ALG_TYPE_HASH_MASK;
1121
1122 return __crypto_hash_cast(crypto_alloc_base(alg_name, type, mask));
1123}
1124
1125static inline struct crypto_tfm *crypto_hash_tfm(struct crypto_hash *tfm)
1126{
1127 return &tfm->base;
1128}
1129
1130static inline void crypto_free_hash(struct crypto_hash *tfm)
1131{
1132 crypto_free_tfm(crypto_hash_tfm(tfm));
1133}
1134
Herbert Xufce32d72006-08-26 17:35:45 +10001135static inline int crypto_has_hash(const char *alg_name, u32 type, u32 mask)
1136{
1137 type &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xu551a09a2007-12-01 21:47:07 +11001138 mask &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xufce32d72006-08-26 17:35:45 +10001139 type |= CRYPTO_ALG_TYPE_HASH;
1140 mask |= CRYPTO_ALG_TYPE_HASH_MASK;
1141
1142 return crypto_has_alg(alg_name, type, mask);
1143}
1144
Herbert Xu055bcee2006-08-19 22:24:23 +10001145static inline struct hash_tfm *crypto_hash_crt(struct crypto_hash *tfm)
1146{
1147 return &crypto_hash_tfm(tfm)->crt_hash;
1148}
1149
1150static inline unsigned int crypto_hash_blocksize(struct crypto_hash *tfm)
1151{
1152 return crypto_tfm_alg_blocksize(crypto_hash_tfm(tfm));
1153}
1154
1155static inline unsigned int crypto_hash_alignmask(struct crypto_hash *tfm)
1156{
1157 return crypto_tfm_alg_alignmask(crypto_hash_tfm(tfm));
1158}
1159
1160static inline unsigned int crypto_hash_digestsize(struct crypto_hash *tfm)
1161{
1162 return crypto_hash_crt(tfm)->digestsize;
1163}
1164
1165static inline u32 crypto_hash_get_flags(struct crypto_hash *tfm)
1166{
1167 return crypto_tfm_get_flags(crypto_hash_tfm(tfm));
1168}
1169
1170static inline void crypto_hash_set_flags(struct crypto_hash *tfm, u32 flags)
1171{
1172 crypto_tfm_set_flags(crypto_hash_tfm(tfm), flags);
1173}
1174
1175static inline void crypto_hash_clear_flags(struct crypto_hash *tfm, u32 flags)
1176{
1177 crypto_tfm_clear_flags(crypto_hash_tfm(tfm), flags);
1178}
1179
1180static inline int crypto_hash_init(struct hash_desc *desc)
1181{
1182 return crypto_hash_crt(desc->tfm)->init(desc);
1183}
1184
1185static inline int crypto_hash_update(struct hash_desc *desc,
1186 struct scatterlist *sg,
1187 unsigned int nbytes)
1188{
1189 return crypto_hash_crt(desc->tfm)->update(desc, sg, nbytes);
1190}
1191
1192static inline int crypto_hash_final(struct hash_desc *desc, u8 *out)
1193{
1194 return crypto_hash_crt(desc->tfm)->final(desc, out);
1195}
1196
1197static inline int crypto_hash_digest(struct hash_desc *desc,
1198 struct scatterlist *sg,
1199 unsigned int nbytes, u8 *out)
1200{
1201 return crypto_hash_crt(desc->tfm)->digest(desc, sg, nbytes, out);
1202}
1203
1204static inline int crypto_hash_setkey(struct crypto_hash *hash,
1205 const u8 *key, unsigned int keylen)
1206{
1207 return crypto_hash_crt(hash)->setkey(hash, key, keylen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208}
1209
Herbert Xufce32d72006-08-26 17:35:45 +10001210static inline struct crypto_comp *__crypto_comp_cast(struct crypto_tfm *tfm)
1211{
1212 return (struct crypto_comp *)tfm;
1213}
1214
1215static inline struct crypto_comp *crypto_comp_cast(struct crypto_tfm *tfm)
1216{
1217 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_COMPRESS) &
1218 CRYPTO_ALG_TYPE_MASK);
1219 return __crypto_comp_cast(tfm);
1220}
1221
1222static inline struct crypto_comp *crypto_alloc_comp(const char *alg_name,
1223 u32 type, u32 mask)
1224{
1225 type &= ~CRYPTO_ALG_TYPE_MASK;
1226 type |= CRYPTO_ALG_TYPE_COMPRESS;
1227 mask |= CRYPTO_ALG_TYPE_MASK;
1228
1229 return __crypto_comp_cast(crypto_alloc_base(alg_name, type, mask));
1230}
1231
1232static inline struct crypto_tfm *crypto_comp_tfm(struct crypto_comp *tfm)
1233{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001234 return &tfm->base;
Herbert Xufce32d72006-08-26 17:35:45 +10001235}
1236
1237static inline void crypto_free_comp(struct crypto_comp *tfm)
1238{
1239 crypto_free_tfm(crypto_comp_tfm(tfm));
1240}
1241
1242static inline int crypto_has_comp(const char *alg_name, u32 type, u32 mask)
1243{
1244 type &= ~CRYPTO_ALG_TYPE_MASK;
1245 type |= CRYPTO_ALG_TYPE_COMPRESS;
1246 mask |= CRYPTO_ALG_TYPE_MASK;
1247
1248 return crypto_has_alg(alg_name, type, mask);
1249}
1250
Herbert Xue4d5b792006-08-26 18:12:40 +10001251static inline const char *crypto_comp_name(struct crypto_comp *tfm)
1252{
1253 return crypto_tfm_alg_name(crypto_comp_tfm(tfm));
1254}
1255
Herbert Xufce32d72006-08-26 17:35:45 +10001256static inline struct compress_tfm *crypto_comp_crt(struct crypto_comp *tfm)
1257{
1258 return &crypto_comp_tfm(tfm)->crt_compress;
1259}
1260
1261static inline int crypto_comp_compress(struct crypto_comp *tfm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 const u8 *src, unsigned int slen,
1263 u8 *dst, unsigned int *dlen)
1264{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001265 return crypto_comp_crt(tfm)->cot_compress(crypto_comp_tfm(tfm),
1266 src, slen, dst, dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267}
1268
Herbert Xufce32d72006-08-26 17:35:45 +10001269static inline int crypto_comp_decompress(struct crypto_comp *tfm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 const u8 *src, unsigned int slen,
1271 u8 *dst, unsigned int *dlen)
1272{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001273 return crypto_comp_crt(tfm)->cot_decompress(crypto_comp_tfm(tfm),
1274 src, slen, dst, dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275}
1276
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277#endif /* _LINUX_CRYPTO_H */
1278