blob: 81e178fb9ed878ee7d49654e31ec419514322f80 [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 Xu5cb1454b2005-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
Arun Sharma600634972011-07-26 16:09:06 -070020#include <linux/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/list.h>
Paul Gortmaker187f1882011-11-23 20:12:59 -050023#include <linux/bug.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>
Gilad Ben-Yossefada69a12017-10-18 08:00:38 +010027#include <linux/completion.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29/*
Kees Cook5d26a102014-11-20 17:05:53 -080030 * Autoloaded crypto modules should only use a prefixed name to avoid allowing
31 * arbitrary modules to be loaded. Loading from userspace may still need the
32 * unprefixed names, so retains those aliases as well.
33 * This uses __MODULE_INFO directly instead of MODULE_ALIAS because pre-4.3
34 * gcc (e.g. avr32 toolchain) uses __LINE__ for uniqueness, and this macro
35 * expands twice on the same line. Instead, use a separate base name for the
36 * alias.
37 */
38#define MODULE_ALIAS_CRYPTO(name) \
39 __MODULE_INFO(alias, alias_userspace, name); \
40 __MODULE_INFO(alias, alias_crypto, "crypto-" name)
41
42/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 * Algorithm masks and types.
44 */
Herbert Xu28259822006-08-06 21:23:26 +100045#define CRYPTO_ALG_TYPE_MASK 0x0000000f
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#define CRYPTO_ALG_TYPE_CIPHER 0x00000001
Loc Ho004a4032008-05-14 20:41:47 +080047#define CRYPTO_ALG_TYPE_COMPRESS 0x00000002
48#define CRYPTO_ALG_TYPE_AEAD 0x00000003
Herbert Xu055bcee2006-08-19 22:24:23 +100049#define CRYPTO_ALG_TYPE_BLKCIPHER 0x00000004
Herbert Xu332f88402007-11-15 22:36:07 +080050#define CRYPTO_ALG_TYPE_ABLKCIPHER 0x00000005
Herbert Xu4e6c3df2016-07-12 13:17:31 +080051#define CRYPTO_ALG_TYPE_SKCIPHER 0x00000005
Herbert Xu61da88e2007-12-17 21:51:27 +080052#define CRYPTO_ALG_TYPE_GIVCIPHER 0x00000006
Salvatore Benedetto4e5f2c42016-06-22 17:49:13 +010053#define CRYPTO_ALG_TYPE_KPP 0x00000008
Giovanni Cabiddu2ebda742016-10-21 13:19:47 +010054#define CRYPTO_ALG_TYPE_ACOMPRESS 0x0000000a
Giovanni Cabiddu1ab53a72016-10-21 13:19:48 +010055#define CRYPTO_ALG_TYPE_SCOMPRESS 0x0000000b
Neil Horman17f0f4a2008-08-14 22:15:52 +100056#define CRYPTO_ALG_TYPE_RNG 0x0000000c
Tadeusz Struk3c339ab2015-06-16 10:30:55 -070057#define CRYPTO_ALG_TYPE_AKCIPHER 0x0000000d
Giovanni Cabiddu63044c42016-06-02 13:28:55 +010058#define CRYPTO_ALG_TYPE_DIGEST 0x0000000e
59#define CRYPTO_ALG_TYPE_HASH 0x0000000e
60#define CRYPTO_ALG_TYPE_SHASH 0x0000000e
61#define CRYPTO_ALG_TYPE_AHASH 0x0000000f
Herbert Xu055bcee2006-08-19 22:24:23 +100062
63#define CRYPTO_ALG_TYPE_HASH_MASK 0x0000000e
Giovanni Cabiddu63044c42016-06-02 13:28:55 +010064#define CRYPTO_ALG_TYPE_AHASH_MASK 0x0000000e
Herbert Xu332f88402007-11-15 22:36:07 +080065#define CRYPTO_ALG_TYPE_BLKCIPHER_MASK 0x0000000c
Giovanni Cabiddu1ab53a72016-10-21 13:19:48 +010066#define CRYPTO_ALG_TYPE_ACOMPRESS_MASK 0x0000000e
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Herbert Xu28259822006-08-06 21:23:26 +100068#define CRYPTO_ALG_LARVAL 0x00000010
Herbert Xu6bfd4802006-09-21 11:39:29 +100069#define CRYPTO_ALG_DEAD 0x00000020
70#define CRYPTO_ALG_DYING 0x00000040
Herbert Xuf3f632d2006-08-06 23:12:59 +100071#define CRYPTO_ALG_ASYNC 0x00000080
Herbert Xu28259822006-08-06 21:23:26 +100072
Linus Torvalds1da177e2005-04-16 15:20:36 -070073/*
Herbert Xu60104392006-08-26 18:34:10 +100074 * Set this bit if and only if the algorithm requires another algorithm of
75 * the same type to handle corner cases.
76 */
77#define CRYPTO_ALG_NEED_FALLBACK 0x00000100
78
79/*
Herbert Xuecfc4322007-12-05 21:08:36 +110080 * This bit is set for symmetric key ciphers that have already been wrapped
81 * with a generic IV generator to prevent them from being wrapped again.
82 */
83#define CRYPTO_ALG_GENIV 0x00000200
84
85/*
Herbert Xu73d38642008-08-03 21:15:23 +080086 * Set if the algorithm has passed automated run-time testing. Note that
87 * if there is no run-time testing for a given algorithm it is considered
88 * to have passed.
89 */
90
91#define CRYPTO_ALG_TESTED 0x00000400
92
93/*
Baruch Siach864e0982016-11-30 15:16:08 +020094 * Set if the algorithm is an instance that is built from templates.
Steffen Klassert64a947b2011-09-27 07:21:26 +020095 */
96#define CRYPTO_ALG_INSTANCE 0x00000800
97
Nikos Mavrogiannopoulosd912bb72011-11-01 13:39:56 +010098/* Set this bit if the algorithm provided is hardware accelerated but
99 * not available to userspace via instruction set or so.
100 */
101#define CRYPTO_ALG_KERN_DRIVER_ONLY 0x00001000
102
Steffen Klassert64a947b2011-09-27 07:21:26 +0200103/*
Stephan Mueller06ca7f62015-03-30 21:55:52 +0200104 * Mark a cipher as a service implementation only usable by another
105 * cipher and never by a normal user of the kernel crypto API
106 */
107#define CRYPTO_ALG_INTERNAL 0x00002000
108
109/*
Eric Biggersa208fa82018-01-03 11:16:26 -0800110 * Set if the algorithm has a ->setkey() method but can be used without
111 * calling it first, i.e. there is a default key.
112 */
113#define CRYPTO_ALG_OPTIONAL_KEY 0x00004000
114
115/*
Matthew Garrette2861fa2018-06-08 14:57:42 -0700116 * Don't trigger module loading
117 */
118#define CRYPTO_NOLOAD 0x00008000
119
120/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 * Transform masks and values (for crt_flags).
122 */
Eric Biggers9fa68f62018-01-03 11:16:27 -0800123#define CRYPTO_TFM_NEED_KEY 0x00000001
124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125#define CRYPTO_TFM_REQ_MASK 0x000fff00
126#define CRYPTO_TFM_RES_MASK 0xfff00000
127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128#define CRYPTO_TFM_REQ_WEAK_KEY 0x00000100
Herbert Xu64baf3c2005-09-01 17:43:05 -0700129#define CRYPTO_TFM_REQ_MAY_SLEEP 0x00000200
Herbert Xu32e3983f2007-03-24 14:35:34 +1100130#define CRYPTO_TFM_REQ_MAY_BACKLOG 0x00000400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131#define CRYPTO_TFM_RES_WEAK_KEY 0x00100000
132#define CRYPTO_TFM_RES_BAD_KEY_LEN 0x00200000
133#define CRYPTO_TFM_RES_BAD_KEY_SCHED 0x00400000
134#define CRYPTO_TFM_RES_BAD_BLOCK_LEN 0x00800000
135#define CRYPTO_TFM_RES_BAD_FLAGS 0x01000000
136
137/*
138 * Miscellaneous stuff.
139 */
Herbert Xuf437a3f2017-04-06 16:16:11 +0800140#define CRYPTO_MAX_ALG_NAME 128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Herbert Xu79911102006-08-21 21:03:52 +1000142/*
143 * The macro CRYPTO_MINALIGN_ATTR (along with the void * type in the actual
144 * declaration) is used to ensure that the crypto_tfm context structure is
145 * aligned correctly for the given architecture so that there are no alignment
146 * faults for C data types. In particular, this is required on platforms such
147 * as arm where pointers are 32-bit aligned but there are data types such as
148 * u64 which require 64-bit alignment.
149 */
Herbert Xu79911102006-08-21 21:03:52 +1000150#define CRYPTO_MINALIGN ARCH_KMALLOC_MINALIGN
Herbert Xu79911102006-08-21 21:03:52 +1000151
Herbert Xu79911102006-08-21 21:03:52 +1000152#define CRYPTO_MINALIGN_ATTR __attribute__ ((__aligned__(CRYPTO_MINALIGN)))
Herbert Xu79911102006-08-21 21:03:52 +1000153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154struct scatterlist;
Herbert Xu32e3983f2007-03-24 14:35:34 +1100155struct crypto_ablkcipher;
156struct crypto_async_request;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000157struct crypto_blkcipher;
Herbert Xu40725182005-07-06 13:51:52 -0700158struct crypto_tfm;
Herbert Xue853c3c2006-08-22 00:06:54 +1000159struct crypto_type;
Herbert Xu61da88e2007-12-17 21:51:27 +0800160struct skcipher_givcrypt_request;
Herbert Xu40725182005-07-06 13:51:52 -0700161
Herbert Xu32e3983f2007-03-24 14:35:34 +1100162typedef void (*crypto_completion_t)(struct crypto_async_request *req, int err);
163
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100164/**
165 * DOC: Block Cipher Context Data Structures
166 *
167 * These data structures define the operating context for each block cipher
168 * type.
169 */
170
Herbert Xu32e3983f2007-03-24 14:35:34 +1100171struct crypto_async_request {
172 struct list_head list;
173 crypto_completion_t complete;
174 void *data;
175 struct crypto_tfm *tfm;
176
177 u32 flags;
178};
179
180struct ablkcipher_request {
181 struct crypto_async_request base;
182
183 unsigned int nbytes;
184
185 void *info;
186
187 struct scatterlist *src;
188 struct scatterlist *dst;
189
190 void *__ctx[] CRYPTO_MINALIGN_ATTR;
191};
192
Herbert Xu5cde0af2006-08-22 00:07:53 +1000193struct blkcipher_desc {
194 struct crypto_blkcipher *tfm;
195 void *info;
196 u32 flags;
197};
198
Herbert Xu40725182005-07-06 13:51:52 -0700199struct cipher_desc {
200 struct crypto_tfm *tfm;
Herbert Xu6c2bb982006-05-16 22:09:29 +1000201 void (*crfn)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Herbert Xu40725182005-07-06 13:51:52 -0700202 unsigned int (*prfn)(const struct cipher_desc *desc, u8 *dst,
203 const u8 *src, unsigned int nbytes);
204 void *info;
205};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100207/**
208 * DOC: Block Cipher Algorithm Definitions
209 *
210 * These data structures define modular crypto algorithm implementations,
211 * managed via crypto_register_alg() and crypto_unregister_alg().
212 */
213
214/**
215 * struct ablkcipher_alg - asynchronous block cipher definition
216 * @min_keysize: Minimum key size supported by the transformation. This is the
217 * smallest key length supported by this transformation algorithm.
218 * This must be set to one of the pre-defined values as this is
219 * not hardware specific. Possible values for this field can be
220 * found via git grep "_MIN_KEY_SIZE" include/crypto/
221 * @max_keysize: Maximum key size supported by the transformation. This is the
222 * largest key length supported by this transformation algorithm.
223 * This must be set to one of the pre-defined values as this is
224 * not hardware specific. Possible values for this field can be
225 * found via git grep "_MAX_KEY_SIZE" include/crypto/
226 * @setkey: Set key for the transformation. This function is used to either
227 * program a supplied key into the hardware or store the key in the
228 * transformation context for programming it later. Note that this
229 * function does modify the transformation context. This function can
230 * be called multiple times during the existence of the transformation
231 * object, so one must make sure the key is properly reprogrammed into
232 * the hardware. This function is also responsible for checking the key
233 * length for validity. In case a software fallback was put in place in
234 * the @cra_init call, this function might need to use the fallback if
235 * the algorithm doesn't support all of the key sizes.
236 * @encrypt: Encrypt a scatterlist of blocks. This function is used to encrypt
237 * the supplied scatterlist containing the blocks of data. The crypto
238 * API consumer is responsible for aligning the entries of the
239 * scatterlist properly and making sure the chunks are correctly
240 * sized. In case a software fallback was put in place in the
241 * @cra_init call, this function might need to use the fallback if
242 * the algorithm doesn't support all of the key sizes. In case the
243 * key was stored in transformation context, the key might need to be
244 * re-programmed into the hardware in this function. This function
245 * shall not modify the transformation context, as this function may
246 * be called in parallel with the same transformation object.
247 * @decrypt: Decrypt a single block. This is a reverse counterpart to @encrypt
248 * and the conditions are exactly the same.
249 * @givencrypt: Update the IV for encryption. With this function, a cipher
250 * implementation may provide the function on how to update the IV
251 * for encryption.
252 * @givdecrypt: Update the IV for decryption. This is the reverse of
253 * @givencrypt .
254 * @geniv: The transformation implementation may use an "IV generator" provided
255 * by the kernel crypto API. Several use cases have a predefined
256 * approach how IVs are to be updated. For such use cases, the kernel
257 * crypto API provides ready-to-use implementations that can be
258 * referenced with this variable.
259 * @ivsize: IV size applicable for transformation. The consumer must provide an
260 * IV of exactly that size to perform the encrypt or decrypt operation.
261 *
262 * All fields except @givencrypt , @givdecrypt , @geniv and @ivsize are
263 * mandatory and must be filled.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 */
Herbert Xub5b7f082007-04-16 20:48:54 +1000265struct ablkcipher_alg {
266 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
267 unsigned int keylen);
268 int (*encrypt)(struct ablkcipher_request *req);
269 int (*decrypt)(struct ablkcipher_request *req);
Herbert Xu61da88e2007-12-17 21:51:27 +0800270 int (*givencrypt)(struct skcipher_givcrypt_request *req);
271 int (*givdecrypt)(struct skcipher_givcrypt_request *req);
Herbert Xub5b7f082007-04-16 20:48:54 +1000272
Herbert Xu23508e12007-11-27 21:33:24 +0800273 const char *geniv;
274
Herbert Xub5b7f082007-04-16 20:48:54 +1000275 unsigned int min_keysize;
276 unsigned int max_keysize;
277 unsigned int ivsize;
278};
279
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100280/**
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100281 * struct blkcipher_alg - synchronous block cipher definition
282 * @min_keysize: see struct ablkcipher_alg
283 * @max_keysize: see struct ablkcipher_alg
284 * @setkey: see struct ablkcipher_alg
285 * @encrypt: see struct ablkcipher_alg
286 * @decrypt: see struct ablkcipher_alg
287 * @geniv: see struct ablkcipher_alg
288 * @ivsize: see struct ablkcipher_alg
289 *
290 * All fields except @geniv and @ivsize are mandatory and must be filled.
291 */
Herbert Xu5cde0af2006-08-22 00:07:53 +1000292struct blkcipher_alg {
293 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
294 unsigned int keylen);
295 int (*encrypt)(struct blkcipher_desc *desc,
296 struct scatterlist *dst, struct scatterlist *src,
297 unsigned int nbytes);
298 int (*decrypt)(struct blkcipher_desc *desc,
299 struct scatterlist *dst, struct scatterlist *src,
300 unsigned int nbytes);
301
Herbert Xu23508e12007-11-27 21:33:24 +0800302 const char *geniv;
303
Herbert Xu5cde0af2006-08-22 00:07:53 +1000304 unsigned int min_keysize;
305 unsigned int max_keysize;
306 unsigned int ivsize;
307};
308
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100309/**
310 * struct cipher_alg - single-block symmetric ciphers definition
311 * @cia_min_keysize: Minimum key size supported by the transformation. This is
312 * the smallest key length supported by this transformation
313 * algorithm. This must be set to one of the pre-defined
314 * values as this is not hardware specific. Possible values
315 * for this field can be found via git grep "_MIN_KEY_SIZE"
316 * include/crypto/
317 * @cia_max_keysize: Maximum key size supported by the transformation. This is
318 * the largest key length supported by this transformation
319 * algorithm. This must be set to one of the pre-defined values
320 * as this is not hardware specific. Possible values for this
321 * field can be found via git grep "_MAX_KEY_SIZE"
322 * include/crypto/
323 * @cia_setkey: Set key for the transformation. This function is used to either
324 * program a supplied key into the hardware or store the key in the
325 * transformation context for programming it later. Note that this
326 * function does modify the transformation context. This function
327 * can be called multiple times during the existence of the
328 * transformation object, so one must make sure the key is properly
329 * reprogrammed into the hardware. This function is also
330 * responsible for checking the key length for validity.
331 * @cia_encrypt: Encrypt a single block. This function is used to encrypt a
332 * single block of data, which must be @cra_blocksize big. This
333 * always operates on a full @cra_blocksize and it is not possible
334 * to encrypt a block of smaller size. The supplied buffers must
335 * therefore also be at least of @cra_blocksize size. Both the
336 * input and output buffers are always aligned to @cra_alignmask.
337 * In case either of the input or output buffer supplied by user
338 * of the crypto API is not aligned to @cra_alignmask, the crypto
339 * API will re-align the buffers. The re-alignment means that a
340 * new buffer will be allocated, the data will be copied into the
341 * new buffer, then the processing will happen on the new buffer,
342 * then the data will be copied back into the original buffer and
343 * finally the new buffer will be freed. In case a software
344 * fallback was put in place in the @cra_init call, this function
345 * might need to use the fallback if the algorithm doesn't support
346 * all of the key sizes. In case the key was stored in
347 * transformation context, the key might need to be re-programmed
348 * into the hardware in this function. This function shall not
349 * modify the transformation context, as this function may be
350 * called in parallel with the same transformation object.
351 * @cia_decrypt: Decrypt a single block. This is a reverse counterpart to
352 * @cia_encrypt, and the conditions are exactly the same.
353 *
354 * All fields are mandatory and must be filled.
355 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356struct cipher_alg {
357 unsigned int cia_min_keysize;
358 unsigned int cia_max_keysize;
Herbert Xu6c2bb982006-05-16 22:09:29 +1000359 int (*cia_setkey)(struct crypto_tfm *tfm, const u8 *key,
Herbert Xu560c06a2006-08-13 14:16:39 +1000360 unsigned int keylen);
Herbert Xu6c2bb982006-05-16 22:09:29 +1000361 void (*cia_encrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
362 void (*cia_decrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363};
364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365struct compress_alg {
Herbert Xu6c2bb982006-05-16 22:09:29 +1000366 int (*coa_compress)(struct crypto_tfm *tfm, const u8 *src,
367 unsigned int slen, u8 *dst, unsigned int *dlen);
368 int (*coa_decompress)(struct crypto_tfm *tfm, const u8 *src,
369 unsigned int slen, u8 *dst, unsigned int *dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370};
371
Corentin Labbe17c18f92018-11-29 14:42:24 +0000372#ifdef CONFIG_CRYPTO_STATS
373/*
374 * struct crypto_istat_aead - statistics for AEAD algorithm
375 * @encrypt_cnt: number of encrypt requests
376 * @encrypt_tlen: total data size handled by encrypt requests
377 * @decrypt_cnt: number of decrypt requests
378 * @decrypt_tlen: total data size handled by decrypt requests
Corentin Labbe44f13132018-11-29 14:42:25 +0000379 * @err_cnt: number of error for AEAD requests
Corentin Labbe17c18f92018-11-29 14:42:24 +0000380 */
381struct crypto_istat_aead {
382 atomic64_t encrypt_cnt;
383 atomic64_t encrypt_tlen;
384 atomic64_t decrypt_cnt;
385 atomic64_t decrypt_tlen;
Corentin Labbe44f13132018-11-29 14:42:25 +0000386 atomic64_t err_cnt;
Corentin Labbe17c18f92018-11-29 14:42:24 +0000387};
388
389/*
390 * struct crypto_istat_akcipher - statistics for akcipher algorithm
391 * @encrypt_cnt: number of encrypt requests
392 * @encrypt_tlen: total data size handled by encrypt requests
393 * @decrypt_cnt: number of decrypt requests
394 * @decrypt_tlen: total data size handled by decrypt requests
395 * @verify_cnt: number of verify operation
396 * @sign_cnt: number of sign requests
Corentin Labbe44f13132018-11-29 14:42:25 +0000397 * @err_cnt: number of error for akcipher requests
Corentin Labbe17c18f92018-11-29 14:42:24 +0000398 */
399struct crypto_istat_akcipher {
400 atomic64_t encrypt_cnt;
401 atomic64_t encrypt_tlen;
402 atomic64_t decrypt_cnt;
403 atomic64_t decrypt_tlen;
404 atomic64_t verify_cnt;
405 atomic64_t sign_cnt;
Corentin Labbe44f13132018-11-29 14:42:25 +0000406 atomic64_t err_cnt;
Corentin Labbe17c18f92018-11-29 14:42:24 +0000407};
408
409/*
410 * struct crypto_istat_cipher - statistics for cipher algorithm
411 * @encrypt_cnt: number of encrypt requests
412 * @encrypt_tlen: total data size handled by encrypt requests
413 * @decrypt_cnt: number of decrypt requests
414 * @decrypt_tlen: total data size handled by decrypt requests
Corentin Labbe44f13132018-11-29 14:42:25 +0000415 * @err_cnt: number of error for cipher requests
Corentin Labbe17c18f92018-11-29 14:42:24 +0000416 */
417struct crypto_istat_cipher {
418 atomic64_t encrypt_cnt;
419 atomic64_t encrypt_tlen;
420 atomic64_t decrypt_cnt;
421 atomic64_t decrypt_tlen;
Corentin Labbe44f13132018-11-29 14:42:25 +0000422 atomic64_t err_cnt;
Corentin Labbe17c18f92018-11-29 14:42:24 +0000423};
424
425/*
426 * struct crypto_istat_compress - statistics for compress algorithm
427 * @compress_cnt: number of compress requests
428 * @compress_tlen: total data size handled by compress requests
429 * @decompress_cnt: number of decompress requests
430 * @decompress_tlen: total data size handled by decompress requests
Corentin Labbe44f13132018-11-29 14:42:25 +0000431 * @err_cnt: number of error for compress requests
Corentin Labbe17c18f92018-11-29 14:42:24 +0000432 */
433struct crypto_istat_compress {
434 atomic64_t compress_cnt;
435 atomic64_t compress_tlen;
436 atomic64_t decompress_cnt;
437 atomic64_t decompress_tlen;
Corentin Labbe44f13132018-11-29 14:42:25 +0000438 atomic64_t err_cnt;
Corentin Labbe17c18f92018-11-29 14:42:24 +0000439};
440
441/*
442 * struct crypto_istat_hash - statistics for has algorithm
443 * @hash_cnt: number of hash requests
444 * @hash_tlen: total data size hashed
Corentin Labbe44f13132018-11-29 14:42:25 +0000445 * @err_cnt: number of error for hash requests
Corentin Labbe17c18f92018-11-29 14:42:24 +0000446 */
447struct crypto_istat_hash {
448 atomic64_t hash_cnt;
449 atomic64_t hash_tlen;
Corentin Labbe44f13132018-11-29 14:42:25 +0000450 atomic64_t err_cnt;
Corentin Labbe17c18f92018-11-29 14:42:24 +0000451};
452
453/*
454 * struct crypto_istat_kpp - statistics for KPP algorithm
455 * @setsecret_cnt: number of setsecrey operation
456 * @generate_public_key_cnt: number of generate_public_key operation
457 * @compute_shared_secret_cnt: number of compute_shared_secret operation
Corentin Labbe44f13132018-11-29 14:42:25 +0000458 * @err_cnt: number of error for KPP requests
Corentin Labbe17c18f92018-11-29 14:42:24 +0000459 */
460struct crypto_istat_kpp {
461 atomic64_t setsecret_cnt;
462 atomic64_t generate_public_key_cnt;
463 atomic64_t compute_shared_secret_cnt;
Corentin Labbe44f13132018-11-29 14:42:25 +0000464 atomic64_t err_cnt;
Corentin Labbe17c18f92018-11-29 14:42:24 +0000465};
466
467/*
468 * struct crypto_istat_rng: statistics for RNG algorithm
469 * @generate_cnt: number of RNG generate requests
470 * @generate_tlen: total data size of generated data by the RNG
471 * @seed_cnt: number of times the RNG was seeded
Corentin Labbe44f13132018-11-29 14:42:25 +0000472 * @err_cnt: number of error for RNG requests
Corentin Labbe17c18f92018-11-29 14:42:24 +0000473 */
474struct crypto_istat_rng {
475 atomic64_t generate_cnt;
476 atomic64_t generate_tlen;
477 atomic64_t seed_cnt;
Corentin Labbe44f13132018-11-29 14:42:25 +0000478 atomic64_t err_cnt;
Corentin Labbe17c18f92018-11-29 14:42:24 +0000479};
480#endif /* CONFIG_CRYPTO_STATS */
Neil Horman17f0f4a2008-08-14 22:15:52 +1000481
Herbert Xub5b7f082007-04-16 20:48:54 +1000482#define cra_ablkcipher cra_u.ablkcipher
Herbert Xu5cde0af2006-08-22 00:07:53 +1000483#define cra_blkcipher cra_u.blkcipher
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484#define cra_cipher cra_u.cipher
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485#define cra_compress cra_u.compress
486
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100487/**
488 * struct crypto_alg - definition of a cryptograpic cipher algorithm
489 * @cra_flags: Flags describing this transformation. See include/linux/crypto.h
490 * CRYPTO_ALG_* flags for the flags which go in here. Those are
491 * used for fine-tuning the description of the transformation
492 * algorithm.
493 * @cra_blocksize: Minimum block size of this transformation. The size in bytes
494 * of the smallest possible unit which can be transformed with
495 * this algorithm. The users must respect this value.
496 * In case of HASH transformation, it is possible for a smaller
497 * block than @cra_blocksize to be passed to the crypto API for
498 * transformation, in case of any other transformation type, an
499 * error will be returned upon any attempt to transform smaller
500 * than @cra_blocksize chunks.
501 * @cra_ctxsize: Size of the operational context of the transformation. This
502 * value informs the kernel crypto API about the memory size
503 * needed to be allocated for the transformation context.
504 * @cra_alignmask: Alignment mask for the input and output data buffer. The data
505 * buffer containing the input data for the algorithm must be
506 * aligned to this alignment mask. The data buffer for the
507 * output data must be aligned to this alignment mask. Note that
508 * the Crypto API will do the re-alignment in software, but
509 * only under special conditions and there is a performance hit.
510 * The re-alignment happens at these occasions for different
511 * @cra_u types: cipher -- For both input data and output data
512 * buffer; ahash -- For output hash destination buf; shash --
513 * For output hash destination buf.
514 * This is needed on hardware which is flawed by design and
515 * cannot pick data from arbitrary addresses.
516 * @cra_priority: Priority of this transformation implementation. In case
517 * multiple transformations with same @cra_name are available to
518 * the Crypto API, the kernel will use the one with highest
519 * @cra_priority.
520 * @cra_name: Generic name (usable by multiple implementations) of the
521 * transformation algorithm. This is the name of the transformation
522 * itself. This field is used by the kernel when looking up the
523 * providers of particular transformation.
524 * @cra_driver_name: Unique name of the transformation provider. This is the
525 * name of the provider of the transformation. This can be any
526 * arbitrary value, but in the usual case, this contains the
527 * name of the chip or provider and the name of the
528 * transformation algorithm.
529 * @cra_type: Type of the cryptographic transformation. This is a pointer to
530 * struct crypto_type, which implements callbacks common for all
Masanari Iida12f7c142015-06-04 00:01:21 +0900531 * transformation types. There are multiple options:
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100532 * &crypto_blkcipher_type, &crypto_ablkcipher_type,
Herbert Xub0d955b2015-08-14 15:30:41 +0800533 * &crypto_ahash_type, &crypto_rng_type.
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100534 * This field might be empty. In that case, there are no common
535 * callbacks. This is the case for: cipher, compress, shash.
536 * @cra_u: Callbacks implementing the transformation. This is a union of
537 * multiple structures. Depending on the type of transformation selected
538 * by @cra_type and @cra_flags above, the associated structure must be
539 * filled with callbacks. This field might be empty. This is the case
540 * for ahash, shash.
541 * @cra_init: Initialize the cryptographic transformation object. This function
542 * is used to initialize the cryptographic transformation object.
543 * This function is called only once at the instantiation time, right
544 * after the transformation context was allocated. In case the
545 * cryptographic hardware has some special requirements which need to
546 * be handled by software, this function shall check for the precise
547 * requirement of the transformation and put any software fallbacks
548 * in place.
549 * @cra_exit: Deinitialize the cryptographic transformation object. This is a
550 * counterpart to @cra_init, used to remove various changes set in
551 * @cra_init.
Gary R Hook0063ec42018-03-14 17:15:52 -0500552 * @cra_u.ablkcipher: Union member which contains an asynchronous block cipher
553 * definition. See @struct @ablkcipher_alg.
554 * @cra_u.blkcipher: Union member which contains a synchronous block cipher
555 * definition See @struct @blkcipher_alg.
556 * @cra_u.cipher: Union member which contains a single-block symmetric cipher
557 * definition. See @struct @cipher_alg.
558 * @cra_u.compress: Union member which contains a (de)compression algorithm.
559 * See @struct @compress_alg.
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100560 * @cra_module: Owner of this transformation implementation. Set to THIS_MODULE
561 * @cra_list: internally used
562 * @cra_users: internally used
563 * @cra_refcnt: internally used
564 * @cra_destroy: internally used
565 *
Corentin Labbe17c18f92018-11-29 14:42:24 +0000566 * @stats: union of all possible crypto_istat_xxx structures
Corentin Labbebfad6cb2018-12-13 08:36:38 +0000567 * @stats.aead: statistics for AEAD algorithm
568 * @stats.akcipher: statistics for akcipher algorithm
569 * @stats.cipher: statistics for cipher algorithm
570 * @stats.compress: statistics for compress algorithm
571 * @stats.hash: statistics for hash algorithm
572 * @stats.rng: statistics for rng algorithm
573 * @stats.kpp: statistics for KPP algorithm
Corentin Labbecac58182018-09-19 10:10:54 +0000574 *
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100575 * The struct crypto_alg describes a generic Crypto API algorithm and is common
576 * for all of the transformations. Any variable not documented here shall not
577 * be used by a cipher implementation as it is internal to the Crypto API.
578 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579struct crypto_alg {
580 struct list_head cra_list;
Herbert Xu6bfd4802006-09-21 11:39:29 +1000581 struct list_head cra_users;
582
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 u32 cra_flags;
584 unsigned int cra_blocksize;
585 unsigned int cra_ctxsize;
Herbert Xu95477372005-07-06 13:52:09 -0700586 unsigned int cra_alignmask;
Herbert Xu5cb1454b2005-11-05 16:58:14 +1100587
588 int cra_priority;
Eric Biggersce8614a2017-12-29 10:00:46 -0600589 refcount_t cra_refcnt;
Herbert Xu5cb1454b2005-11-05 16:58:14 +1100590
Herbert Xud913ea02006-05-21 08:45:26 +1000591 char cra_name[CRYPTO_MAX_ALG_NAME];
592 char cra_driver_name[CRYPTO_MAX_ALG_NAME];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
Herbert Xue853c3c2006-08-22 00:06:54 +1000594 const struct crypto_type *cra_type;
595
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 union {
Herbert Xub5b7f082007-04-16 20:48:54 +1000597 struct ablkcipher_alg ablkcipher;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000598 struct blkcipher_alg blkcipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 struct cipher_alg cipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 struct compress_alg compress;
601 } cra_u;
Herbert Xuc7fc0592006-05-24 13:02:26 +1000602
603 int (*cra_init)(struct crypto_tfm *tfm);
604 void (*cra_exit)(struct crypto_tfm *tfm);
Herbert Xu6521f302006-08-06 20:28:44 +1000605 void (*cra_destroy)(struct crypto_alg *alg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
607 struct module *cra_module;
Corentin Labbecac58182018-09-19 10:10:54 +0000608
Corentin Labbe2ced2602018-11-29 14:42:16 +0000609#ifdef CONFIG_CRYPTO_STATS
Corentin Labbecac58182018-09-19 10:10:54 +0000610 union {
Corentin Labbe17c18f92018-11-29 14:42:24 +0000611 struct crypto_istat_aead aead;
612 struct crypto_istat_akcipher akcipher;
613 struct crypto_istat_cipher cipher;
614 struct crypto_istat_compress compress;
615 struct crypto_istat_hash hash;
616 struct crypto_istat_rng rng;
617 struct crypto_istat_kpp kpp;
618 } stats;
Corentin Labbe2ced2602018-11-29 14:42:16 +0000619#endif /* CONFIG_CRYPTO_STATS */
Corentin Labbecac58182018-09-19 10:10:54 +0000620
Herbert Xuedf18b92015-06-18 14:00:48 +0800621} CRYPTO_MINALIGN_ATTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Corentin Labbef7d76e02018-11-29 14:42:21 +0000623#ifdef CONFIG_CRYPTO_STATS
Corentin Labbe1f6669b2018-11-29 14:42:26 +0000624void crypto_stats_init(struct crypto_alg *alg);
Corentin Labbef7d76e02018-11-29 14:42:21 +0000625void crypto_stats_get(struct crypto_alg *alg);
626void crypto_stats_ablkcipher_encrypt(unsigned int nbytes, int ret, struct crypto_alg *alg);
627void crypto_stats_ablkcipher_decrypt(unsigned int nbytes, int ret, struct crypto_alg *alg);
628void crypto_stats_aead_encrypt(unsigned int cryptlen, struct crypto_alg *alg, int ret);
629void crypto_stats_aead_decrypt(unsigned int cryptlen, struct crypto_alg *alg, int ret);
630void crypto_stats_ahash_update(unsigned int nbytes, int ret, struct crypto_alg *alg);
631void crypto_stats_ahash_final(unsigned int nbytes, int ret, struct crypto_alg *alg);
632void crypto_stats_akcipher_encrypt(unsigned int src_len, int ret, struct crypto_alg *alg);
633void crypto_stats_akcipher_decrypt(unsigned int src_len, int ret, struct crypto_alg *alg);
634void crypto_stats_akcipher_sign(int ret, struct crypto_alg *alg);
635void crypto_stats_akcipher_verify(int ret, struct crypto_alg *alg);
636void crypto_stats_compress(unsigned int slen, int ret, struct crypto_alg *alg);
637void crypto_stats_decompress(unsigned int slen, int ret, struct crypto_alg *alg);
638void crypto_stats_kpp_set_secret(struct crypto_alg *alg, int ret);
639void crypto_stats_kpp_generate_public_key(struct crypto_alg *alg, int ret);
640void crypto_stats_kpp_compute_shared_secret(struct crypto_alg *alg, int ret);
641void crypto_stats_rng_seed(struct crypto_alg *alg, int ret);
642void crypto_stats_rng_generate(struct crypto_alg *alg, unsigned int dlen, int ret);
643void crypto_stats_skcipher_encrypt(unsigned int cryptlen, int ret, struct crypto_alg *alg);
644void crypto_stats_skcipher_decrypt(unsigned int cryptlen, int ret, struct crypto_alg *alg);
645#else
Corentin Labbe1f6669b2018-11-29 14:42:26 +0000646static inline void crypto_stats_init(struct crypto_alg *alg)
647{}
Corentin Labbef7d76e02018-11-29 14:42:21 +0000648static inline void crypto_stats_get(struct crypto_alg *alg)
649{}
650static inline void crypto_stats_ablkcipher_encrypt(unsigned int nbytes, int ret, struct crypto_alg *alg)
651{}
652static inline void crypto_stats_ablkcipher_decrypt(unsigned int nbytes, int ret, struct crypto_alg *alg)
653{}
654static inline void crypto_stats_aead_encrypt(unsigned int cryptlen, struct crypto_alg *alg, int ret)
655{}
656static inline void crypto_stats_aead_decrypt(unsigned int cryptlen, struct crypto_alg *alg, int ret)
657{}
658static inline void crypto_stats_ahash_update(unsigned int nbytes, int ret, struct crypto_alg *alg)
659{}
660static inline void crypto_stats_ahash_final(unsigned int nbytes, int ret, struct crypto_alg *alg)
661{}
662static inline void crypto_stats_akcipher_encrypt(unsigned int src_len, int ret, struct crypto_alg *alg)
663{}
664static inline void crypto_stats_akcipher_decrypt(unsigned int src_len, int ret, struct crypto_alg *alg)
665{}
666static inline void crypto_stats_akcipher_sign(int ret, struct crypto_alg *alg)
667{}
668static inline void crypto_stats_akcipher_verify(int ret, struct crypto_alg *alg)
669{}
670static inline void crypto_stats_compress(unsigned int slen, int ret, struct crypto_alg *alg)
671{}
672static inline void crypto_stats_decompress(unsigned int slen, int ret, struct crypto_alg *alg)
673{}
674static inline void crypto_stats_kpp_set_secret(struct crypto_alg *alg, int ret)
675{}
676static inline void crypto_stats_kpp_generate_public_key(struct crypto_alg *alg, int ret)
677{}
678static inline void crypto_stats_kpp_compute_shared_secret(struct crypto_alg *alg, int ret)
679{}
680static inline void crypto_stats_rng_seed(struct crypto_alg *alg, int ret)
681{}
682static inline void crypto_stats_rng_generate(struct crypto_alg *alg, unsigned int dlen, int ret)
683{}
684static inline void crypto_stats_skcipher_encrypt(unsigned int cryptlen, int ret, struct crypto_alg *alg)
685{}
686static inline void crypto_stats_skcipher_decrypt(unsigned int cryptlen, int ret, struct crypto_alg *alg)
687{}
688#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689/*
Gilad Ben-Yossefada69a12017-10-18 08:00:38 +0100690 * A helper struct for waiting for completion of async crypto ops
691 */
692struct crypto_wait {
693 struct completion completion;
694 int err;
695};
696
697/*
698 * Macro for declaring a crypto op async wait object on stack
699 */
700#define DECLARE_CRYPTO_WAIT(_wait) \
701 struct crypto_wait _wait = { \
702 COMPLETION_INITIALIZER_ONSTACK((_wait).completion), 0 }
703
704/*
705 * Async ops completion helper functioons
706 */
707void crypto_req_done(struct crypto_async_request *req, int err);
708
709static inline int crypto_wait_req(int err, struct crypto_wait *wait)
710{
711 switch (err) {
712 case -EINPROGRESS:
713 case -EBUSY:
714 wait_for_completion(&wait->completion);
715 reinit_completion(&wait->completion);
716 err = wait->err;
717 break;
718 };
719
720 return err;
721}
722
723static inline void crypto_init_wait(struct crypto_wait *wait)
724{
725 init_completion(&wait->completion);
726}
727
728/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 * Algorithm registration interface.
730 */
731int crypto_register_alg(struct crypto_alg *alg);
732int crypto_unregister_alg(struct crypto_alg *alg);
Mark Brown4b004342012-01-17 23:34:26 +0000733int crypto_register_algs(struct crypto_alg *algs, int count);
734int crypto_unregister_algs(struct crypto_alg *algs, int count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
736/*
737 * Algorithm query interface.
738 */
Herbert Xufce32d72006-08-26 17:35:45 +1000739int crypto_has_alg(const char *name, u32 type, u32 mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
741/*
742 * Transforms: user-instantiated objects which encapsulate algorithms
Herbert Xu6d7d684d2006-07-30 11:53:01 +1000743 * and core processing logic. Managed via crypto_alloc_*() and
744 * crypto_free_*(), as well as the various helpers below.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
Herbert Xu32e3983f2007-03-24 14:35:34 +1100747struct ablkcipher_tfm {
748 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
749 unsigned int keylen);
750 int (*encrypt)(struct ablkcipher_request *req);
751 int (*decrypt)(struct ablkcipher_request *req);
Herbert Xu61da88e2007-12-17 21:51:27 +0800752
Herbert Xuecfc4322007-12-05 21:08:36 +1100753 struct crypto_ablkcipher *base;
754
Herbert Xu32e3983f2007-03-24 14:35:34 +1100755 unsigned int ivsize;
756 unsigned int reqsize;
757};
758
Herbert Xu5cde0af2006-08-22 00:07:53 +1000759struct blkcipher_tfm {
760 void *iv;
761 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
762 unsigned int keylen);
763 int (*encrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
764 struct scatterlist *src, unsigned int nbytes);
765 int (*decrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
766 struct scatterlist *src, unsigned int nbytes);
767};
768
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769struct cipher_tfm {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 int (*cit_setkey)(struct crypto_tfm *tfm,
771 const u8 *key, unsigned int keylen);
Herbert Xuf28776a2006-08-13 20:58:18 +1000772 void (*cit_encrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
773 void (*cit_decrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774};
775
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776struct compress_tfm {
777 int (*cot_compress)(struct crypto_tfm *tfm,
778 const u8 *src, unsigned int slen,
779 u8 *dst, unsigned int *dlen);
780 int (*cot_decompress)(struct crypto_tfm *tfm,
781 const u8 *src, unsigned int slen,
782 u8 *dst, unsigned int *dlen);
783};
784
Herbert Xu32e3983f2007-03-24 14:35:34 +1100785#define crt_ablkcipher crt_u.ablkcipher
Herbert Xu5cde0af2006-08-22 00:07:53 +1000786#define crt_blkcipher crt_u.blkcipher
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787#define crt_cipher crt_u.cipher
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788#define crt_compress crt_u.compress
789
790struct crypto_tfm {
791
792 u32 crt_flags;
793
794 union {
Herbert Xu32e3983f2007-03-24 14:35:34 +1100795 struct ablkcipher_tfm ablkcipher;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000796 struct blkcipher_tfm blkcipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 struct cipher_tfm cipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 struct compress_tfm compress;
799 } crt_u;
Herbert Xu4a779482008-09-13 18:19:03 -0700800
801 void (*exit)(struct crypto_tfm *tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
803 struct crypto_alg *__crt_alg;
Herbert Xuf10b7892006-01-25 22:34:01 +1100804
Herbert Xu79911102006-08-21 21:03:52 +1000805 void *__crt_ctx[] CRYPTO_MINALIGN_ATTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806};
807
Herbert Xu32e3983f2007-03-24 14:35:34 +1100808struct crypto_ablkcipher {
809 struct crypto_tfm base;
810};
811
Herbert Xu5cde0af2006-08-22 00:07:53 +1000812struct crypto_blkcipher {
813 struct crypto_tfm base;
814};
815
Herbert Xu78a1fe42006-12-24 10:02:00 +1100816struct crypto_cipher {
817 struct crypto_tfm base;
818};
819
820struct crypto_comp {
821 struct crypto_tfm base;
822};
823
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000824enum {
825 CRYPTOA_UNSPEC,
826 CRYPTOA_ALG,
Herbert Xuebc610e2007-01-01 18:37:02 +1100827 CRYPTOA_TYPE,
Herbert Xu39e1ee012007-08-29 19:27:26 +0800828 CRYPTOA_U32,
Herbert Xuebc610e2007-01-01 18:37:02 +1100829 __CRYPTOA_MAX,
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000830};
831
Herbert Xuebc610e2007-01-01 18:37:02 +1100832#define CRYPTOA_MAX (__CRYPTOA_MAX - 1)
833
Herbert Xu39e1ee012007-08-29 19:27:26 +0800834/* Maximum number of (rtattr) parameters for each template. */
835#define CRYPTO_MAX_ATTRS 32
836
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000837struct crypto_attr_alg {
838 char name[CRYPTO_MAX_ALG_NAME];
839};
840
Herbert Xuebc610e2007-01-01 18:37:02 +1100841struct crypto_attr_type {
842 u32 type;
843 u32 mask;
844};
845
Herbert Xu39e1ee012007-08-29 19:27:26 +0800846struct crypto_attr_u32 {
847 u32 num;
848};
849
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850/*
851 * Transform user interface.
852 */
853
Herbert Xu6d7d684d2006-07-30 11:53:01 +1000854struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask);
Herbert Xu7b2cd922009-02-05 16:48:24 +1100855void crypto_destroy_tfm(void *mem, struct crypto_tfm *tfm);
856
857static inline void crypto_free_tfm(struct crypto_tfm *tfm)
858{
859 return crypto_destroy_tfm(tfm, tfm);
860}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
Herbert Xuda7f0332008-07-31 17:08:25 +0800862int alg_test(const char *driver, const char *alg, u32 type, u32 mask);
863
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864/*
865 * Transform helpers which query the underlying algorithm.
866 */
867static inline const char *crypto_tfm_alg_name(struct crypto_tfm *tfm)
868{
869 return tfm->__crt_alg->cra_name;
870}
871
Michal Ludvigb14cdd62006-07-09 09:02:24 +1000872static inline const char *crypto_tfm_alg_driver_name(struct crypto_tfm *tfm)
873{
874 return tfm->__crt_alg->cra_driver_name;
875}
876
877static inline int crypto_tfm_alg_priority(struct crypto_tfm *tfm)
878{
879 return tfm->__crt_alg->cra_priority;
880}
881
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm)
883{
884 return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK;
885}
886
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm)
888{
889 return tfm->__crt_alg->cra_blocksize;
890}
891
Herbert Xufbdae9f2005-07-06 13:53:29 -0700892static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm *tfm)
893{
894 return tfm->__crt_alg->cra_alignmask;
895}
896
Herbert Xuf28776a2006-08-13 20:58:18 +1000897static inline u32 crypto_tfm_get_flags(struct crypto_tfm *tfm)
898{
899 return tfm->crt_flags;
900}
901
902static inline void crypto_tfm_set_flags(struct crypto_tfm *tfm, u32 flags)
903{
904 tfm->crt_flags |= flags;
905}
906
907static inline void crypto_tfm_clear_flags(struct crypto_tfm *tfm, u32 flags)
908{
909 tfm->crt_flags &= ~flags;
910}
911
Herbert Xu40725182005-07-06 13:51:52 -0700912static inline void *crypto_tfm_ctx(struct crypto_tfm *tfm)
913{
Herbert Xuf10b7892006-01-25 22:34:01 +1100914 return tfm->__crt_ctx;
915}
916
917static inline unsigned int crypto_tfm_ctx_alignment(void)
918{
919 struct crypto_tfm *tfm;
920 return __alignof__(tfm->__crt_ctx);
Herbert Xu40725182005-07-06 13:51:52 -0700921}
922
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923/*
924 * API wrappers.
925 */
Herbert Xu32e3983f2007-03-24 14:35:34 +1100926static inline struct crypto_ablkcipher *__crypto_ablkcipher_cast(
927 struct crypto_tfm *tfm)
928{
929 return (struct crypto_ablkcipher *)tfm;
930}
931
Herbert Xu378f4f52007-12-17 20:07:31 +0800932static inline u32 crypto_skcipher_type(u32 type)
933{
Herbert Xuecfc4322007-12-05 21:08:36 +1100934 type &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
Herbert Xu378f4f52007-12-17 20:07:31 +0800935 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
936 return type;
937}
938
939static inline u32 crypto_skcipher_mask(u32 mask)
940{
Herbert Xuecfc4322007-12-05 21:08:36 +1100941 mask &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
Herbert Xu378f4f52007-12-17 20:07:31 +0800942 mask |= CRYPTO_ALG_TYPE_BLKCIPHER_MASK;
943 return mask;
944}
945
Stephan Muellerf13ec332014-11-12 05:28:22 +0100946/**
947 * DOC: Asynchronous Block Cipher API
948 *
949 * Asynchronous block cipher API is used with the ciphers of type
950 * CRYPTO_ALG_TYPE_ABLKCIPHER (listed as type "ablkcipher" in /proc/crypto).
951 *
952 * Asynchronous cipher operations imply that the function invocation for a
953 * cipher request returns immediately before the completion of the operation.
954 * The cipher request is scheduled as a separate kernel thread and therefore
955 * load-balanced on the different CPUs via the process scheduler. To allow
956 * the kernel crypto API to inform the caller about the completion of a cipher
957 * request, the caller must provide a callback function. That function is
958 * invoked with the cipher handle when the request completes.
959 *
960 * To support the asynchronous operation, additional information than just the
961 * cipher handle must be supplied to the kernel crypto API. That additional
962 * information is given by filling in the ablkcipher_request data structure.
963 *
964 * For the asynchronous block cipher API, the state is maintained with the tfm
965 * cipher handle. A single tfm can be used across multiple calls and in
966 * parallel. For asynchronous block cipher calls, context data supplied and
967 * only used by the caller can be referenced the request data structure in
968 * addition to the IV used for the cipher request. The maintenance of such
969 * state information would be important for a crypto driver implementer to
970 * have, because when calling the callback function upon completion of the
971 * cipher operation, that callback function may need some information about
972 * which operation just finished if it invoked multiple in parallel. This
973 * state information is unused by the kernel crypto API.
974 */
975
Herbert Xu32e3983f2007-03-24 14:35:34 +1100976static inline struct crypto_tfm *crypto_ablkcipher_tfm(
977 struct crypto_ablkcipher *tfm)
978{
979 return &tfm->base;
980}
981
Stephan Muellerf13ec332014-11-12 05:28:22 +0100982/**
983 * crypto_free_ablkcipher() - zeroize and free cipher handle
984 * @tfm: cipher handle to be freed
985 */
Herbert Xu32e3983f2007-03-24 14:35:34 +1100986static inline void crypto_free_ablkcipher(struct crypto_ablkcipher *tfm)
987{
988 crypto_free_tfm(crypto_ablkcipher_tfm(tfm));
989}
990
Stephan Muellerf13ec332014-11-12 05:28:22 +0100991/**
992 * crypto_has_ablkcipher() - Search for the availability of an ablkcipher.
993 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
994 * ablkcipher
995 * @type: specifies the type of the cipher
996 * @mask: specifies the mask for the cipher
997 *
998 * Return: true when the ablkcipher is known to the kernel crypto API; false
999 * otherwise
1000 */
Herbert Xu32e3983f2007-03-24 14:35:34 +11001001static inline int crypto_has_ablkcipher(const char *alg_name, u32 type,
1002 u32 mask)
1003{
Herbert Xu378f4f52007-12-17 20:07:31 +08001004 return crypto_has_alg(alg_name, crypto_skcipher_type(type),
1005 crypto_skcipher_mask(mask));
Herbert Xu32e3983f2007-03-24 14:35:34 +11001006}
1007
1008static inline struct ablkcipher_tfm *crypto_ablkcipher_crt(
1009 struct crypto_ablkcipher *tfm)
1010{
1011 return &crypto_ablkcipher_tfm(tfm)->crt_ablkcipher;
1012}
1013
Stephan Muellerf13ec332014-11-12 05:28:22 +01001014/**
1015 * crypto_ablkcipher_ivsize() - obtain IV size
1016 * @tfm: cipher handle
1017 *
1018 * The size of the IV for the ablkcipher referenced by the cipher handle is
1019 * returned. This IV size may be zero if the cipher does not need an IV.
1020 *
1021 * Return: IV size in bytes
1022 */
Herbert Xu32e3983f2007-03-24 14:35:34 +11001023static inline unsigned int crypto_ablkcipher_ivsize(
1024 struct crypto_ablkcipher *tfm)
1025{
1026 return crypto_ablkcipher_crt(tfm)->ivsize;
1027}
1028
Stephan Muellerf13ec332014-11-12 05:28:22 +01001029/**
1030 * crypto_ablkcipher_blocksize() - obtain block size of cipher
1031 * @tfm: cipher handle
1032 *
1033 * The block size for the ablkcipher referenced with the cipher handle is
1034 * returned. The caller may use that information to allocate appropriate
1035 * memory for the data returned by the encryption or decryption operation
1036 *
1037 * Return: block size of cipher
1038 */
Herbert Xu32e3983f2007-03-24 14:35:34 +11001039static inline unsigned int crypto_ablkcipher_blocksize(
1040 struct crypto_ablkcipher *tfm)
1041{
1042 return crypto_tfm_alg_blocksize(crypto_ablkcipher_tfm(tfm));
1043}
1044
1045static inline unsigned int crypto_ablkcipher_alignmask(
1046 struct crypto_ablkcipher *tfm)
1047{
1048 return crypto_tfm_alg_alignmask(crypto_ablkcipher_tfm(tfm));
1049}
1050
1051static inline u32 crypto_ablkcipher_get_flags(struct crypto_ablkcipher *tfm)
1052{
1053 return crypto_tfm_get_flags(crypto_ablkcipher_tfm(tfm));
1054}
1055
1056static inline void crypto_ablkcipher_set_flags(struct crypto_ablkcipher *tfm,
1057 u32 flags)
1058{
1059 crypto_tfm_set_flags(crypto_ablkcipher_tfm(tfm), flags);
1060}
1061
1062static inline void crypto_ablkcipher_clear_flags(struct crypto_ablkcipher *tfm,
1063 u32 flags)
1064{
1065 crypto_tfm_clear_flags(crypto_ablkcipher_tfm(tfm), flags);
1066}
1067
Stephan Muellerf13ec332014-11-12 05:28:22 +01001068/**
1069 * crypto_ablkcipher_setkey() - set key for cipher
1070 * @tfm: cipher handle
1071 * @key: buffer holding the key
1072 * @keylen: length of the key in bytes
1073 *
1074 * The caller provided key is set for the ablkcipher referenced by the cipher
1075 * handle.
1076 *
1077 * Note, the key length determines the cipher type. Many block ciphers implement
1078 * different cipher modes depending on the key size, such as AES-128 vs AES-192
1079 * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
1080 * is performed.
1081 *
1082 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
1083 */
Herbert Xu32e3983f2007-03-24 14:35:34 +11001084static inline int crypto_ablkcipher_setkey(struct crypto_ablkcipher *tfm,
1085 const u8 *key, unsigned int keylen)
1086{
Herbert Xuecfc4322007-12-05 21:08:36 +11001087 struct ablkcipher_tfm *crt = crypto_ablkcipher_crt(tfm);
1088
1089 return crt->setkey(crt->base, key, keylen);
Herbert Xu32e3983f2007-03-24 14:35:34 +11001090}
1091
Stephan Muellerf13ec332014-11-12 05:28:22 +01001092/**
1093 * crypto_ablkcipher_reqtfm() - obtain cipher handle from request
1094 * @req: ablkcipher_request out of which the cipher handle is to be obtained
1095 *
1096 * Return the crypto_ablkcipher handle when furnishing an ablkcipher_request
1097 * data structure.
1098 *
1099 * Return: crypto_ablkcipher handle
1100 */
Herbert Xu32e3983f2007-03-24 14:35:34 +11001101static inline struct crypto_ablkcipher *crypto_ablkcipher_reqtfm(
1102 struct ablkcipher_request *req)
1103{
1104 return __crypto_ablkcipher_cast(req->base.tfm);
1105}
1106
Stephan Muellerf13ec332014-11-12 05:28:22 +01001107/**
1108 * crypto_ablkcipher_encrypt() - encrypt plaintext
1109 * @req: reference to the ablkcipher_request handle that holds all information
1110 * needed to perform the cipher operation
1111 *
1112 * Encrypt plaintext data using the ablkcipher_request handle. That data
1113 * structure and how it is filled with data is discussed with the
1114 * ablkcipher_request_* functions.
1115 *
1116 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1117 */
Herbert Xu32e3983f2007-03-24 14:35:34 +11001118static inline int crypto_ablkcipher_encrypt(struct ablkcipher_request *req)
1119{
1120 struct ablkcipher_tfm *crt =
1121 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
Corentin Labbef7d76e02018-11-29 14:42:21 +00001122 struct crypto_alg *alg = crt->base->base.__crt_alg;
1123 unsigned int nbytes = req->nbytes;
Corentin Labbecac58182018-09-19 10:10:54 +00001124 int ret;
1125
Corentin Labbef7d76e02018-11-29 14:42:21 +00001126 crypto_stats_get(alg);
Corentin Labbecac58182018-09-19 10:10:54 +00001127 ret = crt->encrypt(req);
Corentin Labbef7d76e02018-11-29 14:42:21 +00001128 crypto_stats_ablkcipher_encrypt(nbytes, ret, alg);
Corentin Labbecac58182018-09-19 10:10:54 +00001129 return ret;
Herbert Xu32e3983f2007-03-24 14:35:34 +11001130}
1131
Stephan Muellerf13ec332014-11-12 05:28:22 +01001132/**
1133 * crypto_ablkcipher_decrypt() - decrypt ciphertext
1134 * @req: reference to the ablkcipher_request handle that holds all information
1135 * needed to perform the cipher operation
1136 *
1137 * Decrypt ciphertext data using the ablkcipher_request handle. That data
1138 * structure and how it is filled with data is discussed with the
1139 * ablkcipher_request_* functions.
1140 *
1141 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1142 */
Herbert Xu32e3983f2007-03-24 14:35:34 +11001143static inline int crypto_ablkcipher_decrypt(struct ablkcipher_request *req)
1144{
1145 struct ablkcipher_tfm *crt =
1146 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
Corentin Labbef7d76e02018-11-29 14:42:21 +00001147 struct crypto_alg *alg = crt->base->base.__crt_alg;
1148 unsigned int nbytes = req->nbytes;
Corentin Labbecac58182018-09-19 10:10:54 +00001149 int ret;
1150
Corentin Labbef7d76e02018-11-29 14:42:21 +00001151 crypto_stats_get(alg);
Corentin Labbecac58182018-09-19 10:10:54 +00001152 ret = crt->decrypt(req);
Corentin Labbef7d76e02018-11-29 14:42:21 +00001153 crypto_stats_ablkcipher_decrypt(nbytes, ret, alg);
Corentin Labbecac58182018-09-19 10:10:54 +00001154 return ret;
Herbert Xu32e3983f2007-03-24 14:35:34 +11001155}
1156
Stephan Muellerf13ec332014-11-12 05:28:22 +01001157/**
1158 * DOC: Asynchronous Cipher Request Handle
1159 *
1160 * The ablkcipher_request data structure contains all pointers to data
1161 * required for the asynchronous cipher operation. This includes the cipher
1162 * handle (which can be used by multiple ablkcipher_request instances), pointer
1163 * to plaintext and ciphertext, asynchronous callback function, etc. It acts
1164 * as a handle to the ablkcipher_request_* API calls in a similar way as
1165 * ablkcipher handle to the crypto_ablkcipher_* API calls.
1166 */
1167
1168/**
1169 * crypto_ablkcipher_reqsize() - obtain size of the request data structure
1170 * @tfm: cipher handle
1171 *
1172 * Return: number of bytes
1173 */
Herbert Xub16c3a22007-08-29 19:02:04 +08001174static inline unsigned int crypto_ablkcipher_reqsize(
1175 struct crypto_ablkcipher *tfm)
Herbert Xu32e3983f2007-03-24 14:35:34 +11001176{
1177 return crypto_ablkcipher_crt(tfm)->reqsize;
1178}
1179
Stephan Muellerf13ec332014-11-12 05:28:22 +01001180/**
1181 * ablkcipher_request_set_tfm() - update cipher handle reference in request
1182 * @req: request handle to be modified
1183 * @tfm: cipher handle that shall be added to the request handle
1184 *
1185 * Allow the caller to replace the existing ablkcipher handle in the request
1186 * data structure with a different one.
1187 */
Herbert Xue196d622007-04-14 16:09:14 +10001188static inline void ablkcipher_request_set_tfm(
1189 struct ablkcipher_request *req, struct crypto_ablkcipher *tfm)
1190{
Herbert Xuecfc4322007-12-05 21:08:36 +11001191 req->base.tfm = crypto_ablkcipher_tfm(crypto_ablkcipher_crt(tfm)->base);
Herbert Xue196d622007-04-14 16:09:14 +10001192}
1193
Herbert Xub5b7f082007-04-16 20:48:54 +10001194static inline struct ablkcipher_request *ablkcipher_request_cast(
1195 struct crypto_async_request *req)
1196{
1197 return container_of(req, struct ablkcipher_request, base);
1198}
1199
Stephan Muellerf13ec332014-11-12 05:28:22 +01001200/**
1201 * ablkcipher_request_alloc() - allocate request data structure
1202 * @tfm: cipher handle to be registered with the request
1203 * @gfp: memory allocation flag that is handed to kmalloc by the API call.
1204 *
1205 * Allocate the request data structure that must be used with the ablkcipher
1206 * encrypt and decrypt API calls. During the allocation, the provided ablkcipher
1207 * handle is registered in the request data structure.
1208 *
Eric Biggers6eae29e2016-04-02 10:54:56 -05001209 * Return: allocated request handle in case of success, or NULL if out of memory
Stephan Muellerf13ec332014-11-12 05:28:22 +01001210 */
Herbert Xu32e3983f2007-03-24 14:35:34 +11001211static inline struct ablkcipher_request *ablkcipher_request_alloc(
1212 struct crypto_ablkcipher *tfm, gfp_t gfp)
1213{
1214 struct ablkcipher_request *req;
1215
1216 req = kmalloc(sizeof(struct ablkcipher_request) +
1217 crypto_ablkcipher_reqsize(tfm), gfp);
1218
1219 if (likely(req))
Herbert Xue196d622007-04-14 16:09:14 +10001220 ablkcipher_request_set_tfm(req, tfm);
Herbert Xu32e3983f2007-03-24 14:35:34 +11001221
1222 return req;
1223}
1224
Stephan Muellerf13ec332014-11-12 05:28:22 +01001225/**
1226 * ablkcipher_request_free() - zeroize and free request data structure
1227 * @req: request data structure cipher handle to be freed
1228 */
Herbert Xu32e3983f2007-03-24 14:35:34 +11001229static inline void ablkcipher_request_free(struct ablkcipher_request *req)
1230{
Herbert Xuaef73cf2009-07-11 22:22:14 +08001231 kzfree(req);
Herbert Xu32e3983f2007-03-24 14:35:34 +11001232}
1233
Stephan Muellerf13ec332014-11-12 05:28:22 +01001234/**
1235 * ablkcipher_request_set_callback() - set asynchronous callback function
1236 * @req: request handle
1237 * @flags: specify zero or an ORing of the flags
Stephan Mueller0184cfe2016-10-21 04:57:27 +02001238 * CRYPTO_TFM_REQ_MAY_BACKLOG the request queue may back log and
Stephan Muellerf13ec332014-11-12 05:28:22 +01001239 * increase the wait queue beyond the initial maximum size;
1240 * CRYPTO_TFM_REQ_MAY_SLEEP the request processing may sleep
1241 * @compl: callback function pointer to be registered with the request handle
1242 * @data: The data pointer refers to memory that is not used by the kernel
1243 * crypto API, but provided to the callback function for it to use. Here,
1244 * the caller can provide a reference to memory the callback function can
1245 * operate on. As the callback function is invoked asynchronously to the
1246 * related functionality, it may need to access data structures of the
1247 * related functionality which can be referenced using this pointer. The
1248 * callback function can access the memory via the "data" field in the
1249 * crypto_async_request data structure provided to the callback function.
1250 *
1251 * This function allows setting the callback function that is triggered once the
1252 * cipher operation completes.
1253 *
1254 * The callback function is registered with the ablkcipher_request handle and
Stephan Mueller0184cfe2016-10-21 04:57:27 +02001255 * must comply with the following template::
Stephan Muellerf13ec332014-11-12 05:28:22 +01001256 *
1257 * void callback_function(struct crypto_async_request *req, int error)
1258 */
Herbert Xu32e3983f2007-03-24 14:35:34 +11001259static inline void ablkcipher_request_set_callback(
1260 struct ablkcipher_request *req,
Mark Rustad3e3dc252014-07-25 02:53:38 -07001261 u32 flags, crypto_completion_t compl, void *data)
Herbert Xu32e3983f2007-03-24 14:35:34 +11001262{
Mark Rustad3e3dc252014-07-25 02:53:38 -07001263 req->base.complete = compl;
Herbert Xu32e3983f2007-03-24 14:35:34 +11001264 req->base.data = data;
1265 req->base.flags = flags;
1266}
1267
Stephan Muellerf13ec332014-11-12 05:28:22 +01001268/**
1269 * ablkcipher_request_set_crypt() - set data buffers
1270 * @req: request handle
1271 * @src: source scatter / gather list
1272 * @dst: destination scatter / gather list
1273 * @nbytes: number of bytes to process from @src
1274 * @iv: IV for the cipher operation which must comply with the IV size defined
1275 * by crypto_ablkcipher_ivsize
1276 *
1277 * This function allows setting of the source data and destination data
1278 * scatter / gather lists.
1279 *
1280 * For encryption, the source is treated as the plaintext and the
1281 * destination is the ciphertext. For a decryption operation, the use is
Stephan Mueller379dcfb2015-01-19 00:13:39 +01001282 * reversed - the source is the ciphertext and the destination is the plaintext.
Stephan Muellerf13ec332014-11-12 05:28:22 +01001283 */
Herbert Xu32e3983f2007-03-24 14:35:34 +11001284static inline void ablkcipher_request_set_crypt(
1285 struct ablkcipher_request *req,
1286 struct scatterlist *src, struct scatterlist *dst,
1287 unsigned int nbytes, void *iv)
1288{
1289 req->src = src;
1290 req->dst = dst;
1291 req->nbytes = nbytes;
1292 req->info = iv;
1293}
1294
Stephan Muellerfced7b02014-11-12 05:29:00 +01001295/**
Stephan Mueller58284f02014-11-12 05:29:36 +01001296 * DOC: Synchronous Block Cipher API
1297 *
1298 * The synchronous block cipher API is used with the ciphers of type
1299 * CRYPTO_ALG_TYPE_BLKCIPHER (listed as type "blkcipher" in /proc/crypto)
1300 *
1301 * Synchronous calls, have a context in the tfm. But since a single tfm can be
1302 * used in multiple calls and in parallel, this info should not be changeable
1303 * (unless a lock is used). This applies, for example, to the symmetric key.
1304 * However, the IV is changeable, so there is an iv field in blkcipher_tfm
1305 * structure for synchronous blkcipher api. So, its the only state info that can
1306 * be kept for synchronous calls without using a big lock across a tfm.
1307 *
1308 * The block cipher API allows the use of a complete cipher, i.e. a cipher
1309 * consisting of a template (a block chaining mode) and a single block cipher
1310 * primitive (e.g. AES).
1311 *
1312 * The plaintext data buffer and the ciphertext data buffer are pointed to
1313 * by using scatter/gather lists. The cipher operation is performed
1314 * on all segments of the provided scatter/gather lists.
1315 *
1316 * The kernel crypto API supports a cipher operation "in-place" which means that
1317 * the caller may provide the same scatter/gather list for the plaintext and
1318 * cipher text. After the completion of the cipher operation, the plaintext
1319 * data is replaced with the ciphertext data in case of an encryption and vice
1320 * versa for a decryption. The caller must ensure that the scatter/gather lists
1321 * for the output data point to sufficiently large buffers, i.e. multiples of
1322 * the block size of the cipher.
1323 */
1324
Herbert Xu5cde0af2006-08-22 00:07:53 +10001325static inline struct crypto_blkcipher *__crypto_blkcipher_cast(
1326 struct crypto_tfm *tfm)
1327{
1328 return (struct crypto_blkcipher *)tfm;
1329}
1330
1331static inline struct crypto_blkcipher *crypto_blkcipher_cast(
1332 struct crypto_tfm *tfm)
1333{
1334 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_BLKCIPHER);
1335 return __crypto_blkcipher_cast(tfm);
1336}
1337
Stephan Mueller58284f02014-11-12 05:29:36 +01001338/**
1339 * crypto_alloc_blkcipher() - allocate synchronous block cipher handle
1340 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1341 * blkcipher cipher
1342 * @type: specifies the type of the cipher
1343 * @mask: specifies the mask for the cipher
1344 *
1345 * Allocate a cipher handle for a block cipher. The returned struct
1346 * crypto_blkcipher is the cipher handle that is required for any subsequent
1347 * API invocation for that block cipher.
1348 *
1349 * Return: allocated cipher handle in case of success; IS_ERR() is true in case
1350 * of an error, PTR_ERR() returns the error code.
1351 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001352static inline struct crypto_blkcipher *crypto_alloc_blkcipher(
1353 const char *alg_name, u32 type, u32 mask)
1354{
Herbert Xu332f88402007-11-15 22:36:07 +08001355 type &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xu5cde0af2006-08-22 00:07:53 +10001356 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
Herbert Xu332f88402007-11-15 22:36:07 +08001357 mask |= CRYPTO_ALG_TYPE_MASK;
Herbert Xu5cde0af2006-08-22 00:07:53 +10001358
1359 return __crypto_blkcipher_cast(crypto_alloc_base(alg_name, type, mask));
1360}
1361
1362static inline struct crypto_tfm *crypto_blkcipher_tfm(
1363 struct crypto_blkcipher *tfm)
1364{
1365 return &tfm->base;
1366}
1367
Stephan Mueller58284f02014-11-12 05:29:36 +01001368/**
1369 * crypto_free_blkcipher() - zeroize and free the block cipher handle
1370 * @tfm: cipher handle to be freed
1371 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001372static inline void crypto_free_blkcipher(struct crypto_blkcipher *tfm)
1373{
1374 crypto_free_tfm(crypto_blkcipher_tfm(tfm));
1375}
1376
Stephan Mueller58284f02014-11-12 05:29:36 +01001377/**
1378 * crypto_has_blkcipher() - Search for the availability of a block cipher
1379 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1380 * block cipher
1381 * @type: specifies the type of the cipher
1382 * @mask: specifies the mask for the cipher
1383 *
1384 * Return: true when the block cipher is known to the kernel crypto API; false
1385 * otherwise
1386 */
Herbert Xufce32d72006-08-26 17:35:45 +10001387static inline int crypto_has_blkcipher(const char *alg_name, u32 type, u32 mask)
1388{
Herbert Xu332f88402007-11-15 22:36:07 +08001389 type &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xufce32d72006-08-26 17:35:45 +10001390 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
Herbert Xu332f88402007-11-15 22:36:07 +08001391 mask |= CRYPTO_ALG_TYPE_MASK;
Herbert Xufce32d72006-08-26 17:35:45 +10001392
1393 return crypto_has_alg(alg_name, type, mask);
1394}
1395
Stephan Mueller58284f02014-11-12 05:29:36 +01001396/**
1397 * crypto_blkcipher_name() - return the name / cra_name from the cipher handle
1398 * @tfm: cipher handle
1399 *
1400 * Return: The character string holding the name of the cipher
1401 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001402static inline const char *crypto_blkcipher_name(struct crypto_blkcipher *tfm)
1403{
1404 return crypto_tfm_alg_name(crypto_blkcipher_tfm(tfm));
1405}
1406
1407static inline struct blkcipher_tfm *crypto_blkcipher_crt(
1408 struct crypto_blkcipher *tfm)
1409{
1410 return &crypto_blkcipher_tfm(tfm)->crt_blkcipher;
1411}
1412
1413static inline struct blkcipher_alg *crypto_blkcipher_alg(
1414 struct crypto_blkcipher *tfm)
1415{
1416 return &crypto_blkcipher_tfm(tfm)->__crt_alg->cra_blkcipher;
1417}
1418
Stephan Mueller58284f02014-11-12 05:29:36 +01001419/**
1420 * crypto_blkcipher_ivsize() - obtain IV size
1421 * @tfm: cipher handle
1422 *
1423 * The size of the IV for the block cipher referenced by the cipher handle is
1424 * returned. This IV size may be zero if the cipher does not need an IV.
1425 *
1426 * Return: IV size in bytes
1427 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001428static inline unsigned int crypto_blkcipher_ivsize(struct crypto_blkcipher *tfm)
1429{
1430 return crypto_blkcipher_alg(tfm)->ivsize;
1431}
1432
Stephan Mueller58284f02014-11-12 05:29:36 +01001433/**
1434 * crypto_blkcipher_blocksize() - obtain block size of cipher
1435 * @tfm: cipher handle
1436 *
1437 * The block size for the block cipher referenced with the cipher handle is
1438 * returned. The caller may use that information to allocate appropriate
1439 * memory for the data returned by the encryption or decryption operation.
1440 *
1441 * Return: block size of cipher
1442 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001443static inline unsigned int crypto_blkcipher_blocksize(
1444 struct crypto_blkcipher *tfm)
1445{
1446 return crypto_tfm_alg_blocksize(crypto_blkcipher_tfm(tfm));
1447}
1448
1449static inline unsigned int crypto_blkcipher_alignmask(
1450 struct crypto_blkcipher *tfm)
1451{
1452 return crypto_tfm_alg_alignmask(crypto_blkcipher_tfm(tfm));
1453}
1454
1455static inline u32 crypto_blkcipher_get_flags(struct crypto_blkcipher *tfm)
1456{
1457 return crypto_tfm_get_flags(crypto_blkcipher_tfm(tfm));
1458}
1459
1460static inline void crypto_blkcipher_set_flags(struct crypto_blkcipher *tfm,
1461 u32 flags)
1462{
1463 crypto_tfm_set_flags(crypto_blkcipher_tfm(tfm), flags);
1464}
1465
1466static inline void crypto_blkcipher_clear_flags(struct crypto_blkcipher *tfm,
1467 u32 flags)
1468{
1469 crypto_tfm_clear_flags(crypto_blkcipher_tfm(tfm), flags);
1470}
1471
Stephan Mueller58284f02014-11-12 05:29:36 +01001472/**
1473 * crypto_blkcipher_setkey() - set key for cipher
1474 * @tfm: cipher handle
1475 * @key: buffer holding the key
1476 * @keylen: length of the key in bytes
1477 *
1478 * The caller provided key is set for the block cipher referenced by the cipher
1479 * handle.
1480 *
1481 * Note, the key length determines the cipher type. Many block ciphers implement
1482 * different cipher modes depending on the key size, such as AES-128 vs AES-192
1483 * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
1484 * is performed.
1485 *
1486 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
1487 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001488static inline int crypto_blkcipher_setkey(struct crypto_blkcipher *tfm,
1489 const u8 *key, unsigned int keylen)
1490{
1491 return crypto_blkcipher_crt(tfm)->setkey(crypto_blkcipher_tfm(tfm),
1492 key, keylen);
1493}
1494
Stephan Mueller58284f02014-11-12 05:29:36 +01001495/**
1496 * crypto_blkcipher_encrypt() - encrypt plaintext
1497 * @desc: reference to the block cipher handle with meta data
1498 * @dst: scatter/gather list that is filled by the cipher operation with the
1499 * ciphertext
1500 * @src: scatter/gather list that holds the plaintext
1501 * @nbytes: number of bytes of the plaintext to encrypt.
1502 *
1503 * Encrypt plaintext data using the IV set by the caller with a preceding
1504 * call of crypto_blkcipher_set_iv.
1505 *
1506 * The blkcipher_desc data structure must be filled by the caller and can
1507 * reside on the stack. The caller must fill desc as follows: desc.tfm is filled
1508 * with the block cipher handle; desc.flags is filled with either
1509 * CRYPTO_TFM_REQ_MAY_SLEEP or 0.
1510 *
1511 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1512 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001513static inline int crypto_blkcipher_encrypt(struct blkcipher_desc *desc,
1514 struct scatterlist *dst,
1515 struct scatterlist *src,
1516 unsigned int nbytes)
1517{
1518 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
1519 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
1520}
1521
Stephan Mueller58284f02014-11-12 05:29:36 +01001522/**
1523 * crypto_blkcipher_encrypt_iv() - encrypt plaintext with dedicated IV
1524 * @desc: reference to the block cipher handle with meta data
1525 * @dst: scatter/gather list that is filled by the cipher operation with the
1526 * ciphertext
1527 * @src: scatter/gather list that holds the plaintext
1528 * @nbytes: number of bytes of the plaintext to encrypt.
1529 *
1530 * Encrypt plaintext data with the use of an IV that is solely used for this
1531 * cipher operation. Any previously set IV is not used.
1532 *
1533 * The blkcipher_desc data structure must be filled by the caller and can
1534 * reside on the stack. The caller must fill desc as follows: desc.tfm is filled
1535 * with the block cipher handle; desc.info is filled with the IV to be used for
1536 * the current operation; desc.flags is filled with either
1537 * CRYPTO_TFM_REQ_MAY_SLEEP or 0.
1538 *
1539 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1540 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001541static inline int crypto_blkcipher_encrypt_iv(struct blkcipher_desc *desc,
1542 struct scatterlist *dst,
1543 struct scatterlist *src,
1544 unsigned int nbytes)
1545{
1546 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
1547}
1548
Stephan Mueller58284f02014-11-12 05:29:36 +01001549/**
1550 * crypto_blkcipher_decrypt() - decrypt ciphertext
1551 * @desc: reference to the block cipher handle with meta data
1552 * @dst: scatter/gather list that is filled by the cipher operation with the
1553 * plaintext
1554 * @src: scatter/gather list that holds the ciphertext
1555 * @nbytes: number of bytes of the ciphertext to decrypt.
1556 *
1557 * Decrypt ciphertext data using the IV set by the caller with a preceding
1558 * call of crypto_blkcipher_set_iv.
1559 *
1560 * The blkcipher_desc data structure must be filled by the caller as documented
1561 * for the crypto_blkcipher_encrypt call above.
1562 *
1563 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1564 *
1565 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001566static inline int crypto_blkcipher_decrypt(struct blkcipher_desc *desc,
1567 struct scatterlist *dst,
1568 struct scatterlist *src,
1569 unsigned int nbytes)
1570{
1571 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
1572 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
1573}
1574
Stephan Mueller58284f02014-11-12 05:29:36 +01001575/**
1576 * crypto_blkcipher_decrypt_iv() - decrypt ciphertext with dedicated IV
1577 * @desc: reference to the block cipher handle with meta data
1578 * @dst: scatter/gather list that is filled by the cipher operation with the
1579 * plaintext
1580 * @src: scatter/gather list that holds the ciphertext
1581 * @nbytes: number of bytes of the ciphertext to decrypt.
1582 *
1583 * Decrypt ciphertext data with the use of an IV that is solely used for this
1584 * cipher operation. Any previously set IV is not used.
1585 *
1586 * The blkcipher_desc data structure must be filled by the caller as documented
1587 * for the crypto_blkcipher_encrypt_iv call above.
1588 *
1589 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1590 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001591static inline int crypto_blkcipher_decrypt_iv(struct blkcipher_desc *desc,
1592 struct scatterlist *dst,
1593 struct scatterlist *src,
1594 unsigned int nbytes)
1595{
1596 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
1597}
1598
Stephan Mueller58284f02014-11-12 05:29:36 +01001599/**
1600 * crypto_blkcipher_set_iv() - set IV for cipher
1601 * @tfm: cipher handle
1602 * @src: buffer holding the IV
1603 * @len: length of the IV in bytes
1604 *
1605 * The caller provided IV is set for the block cipher referenced by the cipher
1606 * handle.
1607 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001608static inline void crypto_blkcipher_set_iv(struct crypto_blkcipher *tfm,
1609 const u8 *src, unsigned int len)
1610{
1611 memcpy(crypto_blkcipher_crt(tfm)->iv, src, len);
1612}
1613
Stephan Mueller58284f02014-11-12 05:29:36 +01001614/**
1615 * crypto_blkcipher_get_iv() - obtain IV from cipher
1616 * @tfm: cipher handle
1617 * @dst: buffer filled with the IV
1618 * @len: length of the buffer dst
1619 *
1620 * The caller can obtain the IV set for the block cipher referenced by the
1621 * cipher handle and store it into the user-provided buffer. If the buffer
1622 * has an insufficient space, the IV is truncated to fit the buffer.
1623 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001624static inline void crypto_blkcipher_get_iv(struct crypto_blkcipher *tfm,
1625 u8 *dst, unsigned int len)
1626{
1627 memcpy(dst, crypto_blkcipher_crt(tfm)->iv, len);
1628}
1629
Stephan Mueller16e61032014-11-12 05:30:06 +01001630/**
1631 * DOC: Single Block Cipher API
1632 *
1633 * The single block cipher API is used with the ciphers of type
1634 * CRYPTO_ALG_TYPE_CIPHER (listed as type "cipher" in /proc/crypto).
1635 *
1636 * Using the single block cipher API calls, operations with the basic cipher
1637 * primitive can be implemented. These cipher primitives exclude any block
1638 * chaining operations including IV handling.
1639 *
1640 * The purpose of this single block cipher API is to support the implementation
1641 * of templates or other concepts that only need to perform the cipher operation
1642 * on one block at a time. Templates invoke the underlying cipher primitive
1643 * block-wise and process either the input or the output data of these cipher
1644 * operations.
1645 */
1646
Herbert Xuf28776a2006-08-13 20:58:18 +10001647static inline struct crypto_cipher *__crypto_cipher_cast(struct crypto_tfm *tfm)
1648{
1649 return (struct crypto_cipher *)tfm;
1650}
1651
1652static inline struct crypto_cipher *crypto_cipher_cast(struct crypto_tfm *tfm)
1653{
1654 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
1655 return __crypto_cipher_cast(tfm);
1656}
1657
Stephan Mueller16e61032014-11-12 05:30:06 +01001658/**
1659 * crypto_alloc_cipher() - allocate single block cipher handle
1660 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1661 * single block cipher
1662 * @type: specifies the type of the cipher
1663 * @mask: specifies the mask for the cipher
1664 *
1665 * Allocate a cipher handle for a single block cipher. The returned struct
1666 * crypto_cipher is the cipher handle that is required for any subsequent API
1667 * invocation for that single block cipher.
1668 *
1669 * Return: allocated cipher handle in case of success; IS_ERR() is true in case
1670 * of an error, PTR_ERR() returns the error code.
1671 */
Herbert Xuf28776a2006-08-13 20:58:18 +10001672static inline struct crypto_cipher *crypto_alloc_cipher(const char *alg_name,
1673 u32 type, u32 mask)
1674{
1675 type &= ~CRYPTO_ALG_TYPE_MASK;
1676 type |= CRYPTO_ALG_TYPE_CIPHER;
1677 mask |= CRYPTO_ALG_TYPE_MASK;
1678
1679 return __crypto_cipher_cast(crypto_alloc_base(alg_name, type, mask));
1680}
1681
1682static inline struct crypto_tfm *crypto_cipher_tfm(struct crypto_cipher *tfm)
1683{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001684 return &tfm->base;
Herbert Xuf28776a2006-08-13 20:58:18 +10001685}
1686
Stephan Mueller16e61032014-11-12 05:30:06 +01001687/**
1688 * crypto_free_cipher() - zeroize and free the single block cipher handle
1689 * @tfm: cipher handle to be freed
1690 */
Herbert Xuf28776a2006-08-13 20:58:18 +10001691static inline void crypto_free_cipher(struct crypto_cipher *tfm)
1692{
1693 crypto_free_tfm(crypto_cipher_tfm(tfm));
1694}
1695
Stephan Mueller16e61032014-11-12 05:30:06 +01001696/**
1697 * crypto_has_cipher() - Search for the availability of a single block cipher
1698 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1699 * single block cipher
1700 * @type: specifies the type of the cipher
1701 * @mask: specifies the mask for the cipher
1702 *
1703 * Return: true when the single block cipher is known to the kernel crypto API;
1704 * false otherwise
1705 */
Herbert Xufce32d72006-08-26 17:35:45 +10001706static inline int crypto_has_cipher(const char *alg_name, u32 type, u32 mask)
1707{
1708 type &= ~CRYPTO_ALG_TYPE_MASK;
1709 type |= CRYPTO_ALG_TYPE_CIPHER;
1710 mask |= CRYPTO_ALG_TYPE_MASK;
1711
1712 return crypto_has_alg(alg_name, type, mask);
1713}
1714
Herbert Xuf28776a2006-08-13 20:58:18 +10001715static inline struct cipher_tfm *crypto_cipher_crt(struct crypto_cipher *tfm)
1716{
1717 return &crypto_cipher_tfm(tfm)->crt_cipher;
1718}
1719
Stephan Mueller16e61032014-11-12 05:30:06 +01001720/**
1721 * crypto_cipher_blocksize() - obtain block size for cipher
1722 * @tfm: cipher handle
1723 *
1724 * The block size for the single block cipher referenced with the cipher handle
1725 * tfm is returned. The caller may use that information to allocate appropriate
1726 * memory for the data returned by the encryption or decryption operation
1727 *
1728 * Return: block size of cipher
1729 */
Herbert Xuf28776a2006-08-13 20:58:18 +10001730static inline unsigned int crypto_cipher_blocksize(struct crypto_cipher *tfm)
1731{
1732 return crypto_tfm_alg_blocksize(crypto_cipher_tfm(tfm));
1733}
1734
1735static inline unsigned int crypto_cipher_alignmask(struct crypto_cipher *tfm)
1736{
1737 return crypto_tfm_alg_alignmask(crypto_cipher_tfm(tfm));
1738}
1739
1740static inline u32 crypto_cipher_get_flags(struct crypto_cipher *tfm)
1741{
1742 return crypto_tfm_get_flags(crypto_cipher_tfm(tfm));
1743}
1744
1745static inline void crypto_cipher_set_flags(struct crypto_cipher *tfm,
1746 u32 flags)
1747{
1748 crypto_tfm_set_flags(crypto_cipher_tfm(tfm), flags);
1749}
1750
1751static inline void crypto_cipher_clear_flags(struct crypto_cipher *tfm,
1752 u32 flags)
1753{
1754 crypto_tfm_clear_flags(crypto_cipher_tfm(tfm), flags);
1755}
1756
Stephan Mueller16e61032014-11-12 05:30:06 +01001757/**
1758 * crypto_cipher_setkey() - set key for cipher
1759 * @tfm: cipher handle
1760 * @key: buffer holding the key
1761 * @keylen: length of the key in bytes
1762 *
1763 * The caller provided key is set for the single block cipher referenced by the
1764 * cipher handle.
1765 *
1766 * Note, the key length determines the cipher type. Many block ciphers implement
1767 * different cipher modes depending on the key size, such as AES-128 vs AES-192
1768 * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
1769 * is performed.
1770 *
1771 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
1772 */
Herbert Xu7226bc872006-08-21 21:40:49 +10001773static inline int crypto_cipher_setkey(struct crypto_cipher *tfm,
1774 const u8 *key, unsigned int keylen)
1775{
1776 return crypto_cipher_crt(tfm)->cit_setkey(crypto_cipher_tfm(tfm),
1777 key, keylen);
1778}
1779
Stephan Mueller16e61032014-11-12 05:30:06 +01001780/**
1781 * crypto_cipher_encrypt_one() - encrypt one block of plaintext
1782 * @tfm: cipher handle
1783 * @dst: points to the buffer that will be filled with the ciphertext
1784 * @src: buffer holding the plaintext to be encrypted
1785 *
1786 * Invoke the encryption operation of one block. The caller must ensure that
1787 * the plaintext and ciphertext buffers are at least one block in size.
1788 */
Herbert Xuf28776a2006-08-13 20:58:18 +10001789static inline void crypto_cipher_encrypt_one(struct crypto_cipher *tfm,
1790 u8 *dst, const u8 *src)
1791{
1792 crypto_cipher_crt(tfm)->cit_encrypt_one(crypto_cipher_tfm(tfm),
1793 dst, src);
1794}
1795
Stephan Mueller16e61032014-11-12 05:30:06 +01001796/**
1797 * crypto_cipher_decrypt_one() - decrypt one block of ciphertext
1798 * @tfm: cipher handle
1799 * @dst: points to the buffer that will be filled with the plaintext
1800 * @src: buffer holding the ciphertext to be decrypted
1801 *
1802 * Invoke the decryption operation of one block. The caller must ensure that
1803 * the plaintext and ciphertext buffers are at least one block in size.
1804 */
Herbert Xuf28776a2006-08-13 20:58:18 +10001805static inline void crypto_cipher_decrypt_one(struct crypto_cipher *tfm,
1806 u8 *dst, const u8 *src)
1807{
1808 crypto_cipher_crt(tfm)->cit_decrypt_one(crypto_cipher_tfm(tfm),
1809 dst, src);
1810}
1811
Herbert Xufce32d72006-08-26 17:35:45 +10001812static inline struct crypto_comp *__crypto_comp_cast(struct crypto_tfm *tfm)
1813{
1814 return (struct crypto_comp *)tfm;
1815}
1816
1817static inline struct crypto_comp *crypto_comp_cast(struct crypto_tfm *tfm)
1818{
1819 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_COMPRESS) &
1820 CRYPTO_ALG_TYPE_MASK);
1821 return __crypto_comp_cast(tfm);
1822}
1823
1824static inline struct crypto_comp *crypto_alloc_comp(const char *alg_name,
1825 u32 type, u32 mask)
1826{
1827 type &= ~CRYPTO_ALG_TYPE_MASK;
1828 type |= CRYPTO_ALG_TYPE_COMPRESS;
1829 mask |= CRYPTO_ALG_TYPE_MASK;
1830
1831 return __crypto_comp_cast(crypto_alloc_base(alg_name, type, mask));
1832}
1833
1834static inline struct crypto_tfm *crypto_comp_tfm(struct crypto_comp *tfm)
1835{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001836 return &tfm->base;
Herbert Xufce32d72006-08-26 17:35:45 +10001837}
1838
1839static inline void crypto_free_comp(struct crypto_comp *tfm)
1840{
1841 crypto_free_tfm(crypto_comp_tfm(tfm));
1842}
1843
1844static inline int crypto_has_comp(const char *alg_name, u32 type, u32 mask)
1845{
1846 type &= ~CRYPTO_ALG_TYPE_MASK;
1847 type |= CRYPTO_ALG_TYPE_COMPRESS;
1848 mask |= CRYPTO_ALG_TYPE_MASK;
1849
1850 return crypto_has_alg(alg_name, type, mask);
1851}
1852
Herbert Xue4d5b792006-08-26 18:12:40 +10001853static inline const char *crypto_comp_name(struct crypto_comp *tfm)
1854{
1855 return crypto_tfm_alg_name(crypto_comp_tfm(tfm));
1856}
1857
Herbert Xufce32d72006-08-26 17:35:45 +10001858static inline struct compress_tfm *crypto_comp_crt(struct crypto_comp *tfm)
1859{
1860 return &crypto_comp_tfm(tfm)->crt_compress;
1861}
1862
1863static inline int crypto_comp_compress(struct crypto_comp *tfm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 const u8 *src, unsigned int slen,
1865 u8 *dst, unsigned int *dlen)
1866{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001867 return crypto_comp_crt(tfm)->cot_compress(crypto_comp_tfm(tfm),
1868 src, slen, dst, dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869}
1870
Herbert Xufce32d72006-08-26 17:35:45 +10001871static inline int crypto_comp_decompress(struct crypto_comp *tfm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 const u8 *src, unsigned int slen,
1873 u8 *dst, unsigned int *dlen)
1874{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001875 return crypto_comp_crt(tfm)->cot_decompress(crypto_comp_tfm(tfm),
1876 src, slen, dst, dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877}
1878
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879#endif /* _LINUX_CRYPTO_H */
1880