blob: 9cf8f3ce0e50af6a5174967d592ee9d1eed3830c [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001/* SPDX-License-Identifier: GPL-2.0-or-later */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * Scatterlist Cryptographic API.
4 *
5 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
6 * Copyright (c) 2002 David S. Miller (davem@redhat.com)
Herbert Xu5cb1454b2005-11-05 16:58:14 +11007 * Copyright (c) 2005 Herbert Xu <herbert@gondor.apana.org.au>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * Portions derived from Cryptoapi, by Alexander Kjeldaas <astor@fast.no>
John Anthony Kazos Jr18735dd2007-10-19 23:07:36 +020010 * and Nettle, by Niels Möller.
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 */
12#ifndef _LINUX_CRYPTO_H
13#define _LINUX_CRYPTO_H
14
Arun Sharma600634972011-07-26 16:09:06 -070015#include <linux/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/list.h>
Paul Gortmaker187f1882011-11-23 20:12:59 -050018#include <linux/bug.h>
Herbert Xu79911102006-08-21 21:03:52 +100019#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/string.h>
Herbert Xu79911102006-08-21 21:03:52 +100021#include <linux/uaccess.h>
Gilad Ben-Yossefada69a12017-10-18 08:00:38 +010022#include <linux/completion.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24/*
Kees Cook5d26a102014-11-20 17:05:53 -080025 * Autoloaded crypto modules should only use a prefixed name to avoid allowing
26 * arbitrary modules to be loaded. Loading from userspace may still need the
27 * unprefixed names, so retains those aliases as well.
28 * This uses __MODULE_INFO directly instead of MODULE_ALIAS because pre-4.3
29 * gcc (e.g. avr32 toolchain) uses __LINE__ for uniqueness, and this macro
30 * expands twice on the same line. Instead, use a separate base name for the
31 * alias.
32 */
33#define MODULE_ALIAS_CRYPTO(name) \
34 __MODULE_INFO(alias, alias_userspace, name); \
35 __MODULE_INFO(alias, alias_crypto, "crypto-" name)
36
37/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 * Algorithm masks and types.
39 */
Herbert Xu28259822006-08-06 21:23:26 +100040#define CRYPTO_ALG_TYPE_MASK 0x0000000f
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#define CRYPTO_ALG_TYPE_CIPHER 0x00000001
Loc Ho004a4032008-05-14 20:41:47 +080042#define CRYPTO_ALG_TYPE_COMPRESS 0x00000002
43#define CRYPTO_ALG_TYPE_AEAD 0x00000003
Herbert Xu055bcee2006-08-19 22:24:23 +100044#define CRYPTO_ALG_TYPE_BLKCIPHER 0x00000004
Herbert Xu332f88402007-11-15 22:36:07 +080045#define CRYPTO_ALG_TYPE_ABLKCIPHER 0x00000005
Herbert Xu4e6c3df2016-07-12 13:17:31 +080046#define CRYPTO_ALG_TYPE_SKCIPHER 0x00000005
Salvatore Benedetto4e5f2c42016-06-22 17:49:13 +010047#define CRYPTO_ALG_TYPE_KPP 0x00000008
Giovanni Cabiddu2ebda742016-10-21 13:19:47 +010048#define CRYPTO_ALG_TYPE_ACOMPRESS 0x0000000a
Giovanni Cabiddu1ab53a72016-10-21 13:19:48 +010049#define CRYPTO_ALG_TYPE_SCOMPRESS 0x0000000b
Neil Horman17f0f4a2008-08-14 22:15:52 +100050#define CRYPTO_ALG_TYPE_RNG 0x0000000c
Tadeusz Struk3c339ab2015-06-16 10:30:55 -070051#define CRYPTO_ALG_TYPE_AKCIPHER 0x0000000d
Giovanni Cabiddu63044c42016-06-02 13:28:55 +010052#define CRYPTO_ALG_TYPE_DIGEST 0x0000000e
53#define CRYPTO_ALG_TYPE_HASH 0x0000000e
54#define CRYPTO_ALG_TYPE_SHASH 0x0000000e
55#define CRYPTO_ALG_TYPE_AHASH 0x0000000f
Herbert Xu055bcee2006-08-19 22:24:23 +100056
57#define CRYPTO_ALG_TYPE_HASH_MASK 0x0000000e
Giovanni Cabiddu63044c42016-06-02 13:28:55 +010058#define CRYPTO_ALG_TYPE_AHASH_MASK 0x0000000e
Herbert Xu332f88402007-11-15 22:36:07 +080059#define CRYPTO_ALG_TYPE_BLKCIPHER_MASK 0x0000000c
Giovanni Cabiddu1ab53a72016-10-21 13:19:48 +010060#define CRYPTO_ALG_TYPE_ACOMPRESS_MASK 0x0000000e
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Herbert Xu28259822006-08-06 21:23:26 +100062#define CRYPTO_ALG_LARVAL 0x00000010
Herbert Xu6bfd4802006-09-21 11:39:29 +100063#define CRYPTO_ALG_DEAD 0x00000020
64#define CRYPTO_ALG_DYING 0x00000040
Herbert Xuf3f632d2006-08-06 23:12:59 +100065#define CRYPTO_ALG_ASYNC 0x00000080
Herbert Xu28259822006-08-06 21:23:26 +100066
Linus Torvalds1da177e2005-04-16 15:20:36 -070067/*
Herbert Xu60104392006-08-26 18:34:10 +100068 * Set this bit if and only if the algorithm requires another algorithm of
69 * the same type to handle corner cases.
70 */
71#define CRYPTO_ALG_NEED_FALLBACK 0x00000100
72
73/*
Herbert Xu73d38642008-08-03 21:15:23 +080074 * Set if the algorithm has passed automated run-time testing. Note that
75 * if there is no run-time testing for a given algorithm it is considered
76 * to have passed.
77 */
78
79#define CRYPTO_ALG_TESTED 0x00000400
80
81/*
Baruch Siach864e0982016-11-30 15:16:08 +020082 * Set if the algorithm is an instance that is built from templates.
Steffen Klassert64a947b2011-09-27 07:21:26 +020083 */
84#define CRYPTO_ALG_INSTANCE 0x00000800
85
Nikos Mavrogiannopoulosd912bb72011-11-01 13:39:56 +010086/* Set this bit if the algorithm provided is hardware accelerated but
87 * not available to userspace via instruction set or so.
88 */
89#define CRYPTO_ALG_KERN_DRIVER_ONLY 0x00001000
90
Steffen Klassert64a947b2011-09-27 07:21:26 +020091/*
Stephan Mueller06ca7f62015-03-30 21:55:52 +020092 * Mark a cipher as a service implementation only usable by another
93 * cipher and never by a normal user of the kernel crypto API
94 */
95#define CRYPTO_ALG_INTERNAL 0x00002000
96
97/*
Eric Biggersa208fa82018-01-03 11:16:26 -080098 * Set if the algorithm has a ->setkey() method but can be used without
99 * calling it first, i.e. there is a default key.
100 */
101#define CRYPTO_ALG_OPTIONAL_KEY 0x00004000
102
103/*
Matthew Garrette2861fa2018-06-08 14:57:42 -0700104 * Don't trigger module loading
105 */
106#define CRYPTO_NOLOAD 0x00008000
107
108/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 * Transform masks and values (for crt_flags).
110 */
Eric Biggers9fa68f62018-01-03 11:16:27 -0800111#define CRYPTO_TFM_NEED_KEY 0x00000001
112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113#define CRYPTO_TFM_REQ_MASK 0x000fff00
114#define CRYPTO_TFM_RES_MASK 0xfff00000
115
Eric Biggers231baec2019-01-18 22:48:00 -0800116#define CRYPTO_TFM_REQ_FORBID_WEAK_KEYS 0x00000100
Herbert Xu64baf3c2005-09-01 17:43:05 -0700117#define CRYPTO_TFM_REQ_MAY_SLEEP 0x00000200
Herbert Xu32e3983f2007-03-24 14:35:34 +1100118#define CRYPTO_TFM_REQ_MAY_BACKLOG 0x00000400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119#define CRYPTO_TFM_RES_WEAK_KEY 0x00100000
120#define CRYPTO_TFM_RES_BAD_KEY_LEN 0x00200000
121#define CRYPTO_TFM_RES_BAD_KEY_SCHED 0x00400000
122#define CRYPTO_TFM_RES_BAD_BLOCK_LEN 0x00800000
123#define CRYPTO_TFM_RES_BAD_FLAGS 0x01000000
124
125/*
126 * Miscellaneous stuff.
127 */
Herbert Xuf437a3f2017-04-06 16:16:11 +0800128#define CRYPTO_MAX_ALG_NAME 128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Herbert Xu79911102006-08-21 21:03:52 +1000130/*
131 * The macro CRYPTO_MINALIGN_ATTR (along with the void * type in the actual
132 * declaration) is used to ensure that the crypto_tfm context structure is
133 * aligned correctly for the given architecture so that there are no alignment
134 * faults for C data types. In particular, this is required on platforms such
135 * as arm where pointers are 32-bit aligned but there are data types such as
136 * u64 which require 64-bit alignment.
137 */
Herbert Xu79911102006-08-21 21:03:52 +1000138#define CRYPTO_MINALIGN ARCH_KMALLOC_MINALIGN
Herbert Xu79911102006-08-21 21:03:52 +1000139
Herbert Xu79911102006-08-21 21:03:52 +1000140#define CRYPTO_MINALIGN_ATTR __attribute__ ((__aligned__(CRYPTO_MINALIGN)))
Herbert Xu79911102006-08-21 21:03:52 +1000141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142struct scatterlist;
Herbert Xu32e3983f2007-03-24 14:35:34 +1100143struct crypto_ablkcipher;
144struct crypto_async_request;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000145struct crypto_blkcipher;
Herbert Xu40725182005-07-06 13:51:52 -0700146struct crypto_tfm;
Herbert Xue853c3c2006-08-22 00:06:54 +1000147struct crypto_type;
Herbert Xu40725182005-07-06 13:51:52 -0700148
Herbert Xu32e3983f2007-03-24 14:35:34 +1100149typedef void (*crypto_completion_t)(struct crypto_async_request *req, int err);
150
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100151/**
152 * DOC: Block Cipher Context Data Structures
153 *
154 * These data structures define the operating context for each block cipher
155 * type.
156 */
157
Herbert Xu32e3983f2007-03-24 14:35:34 +1100158struct crypto_async_request {
159 struct list_head list;
160 crypto_completion_t complete;
161 void *data;
162 struct crypto_tfm *tfm;
163
164 u32 flags;
165};
166
167struct ablkcipher_request {
168 struct crypto_async_request base;
169
170 unsigned int nbytes;
171
172 void *info;
173
174 struct scatterlist *src;
175 struct scatterlist *dst;
176
177 void *__ctx[] CRYPTO_MINALIGN_ATTR;
178};
179
Herbert Xu5cde0af2006-08-22 00:07:53 +1000180struct blkcipher_desc {
181 struct crypto_blkcipher *tfm;
182 void *info;
183 u32 flags;
184};
185
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100186/**
187 * DOC: Block Cipher Algorithm Definitions
188 *
189 * These data structures define modular crypto algorithm implementations,
190 * managed via crypto_register_alg() and crypto_unregister_alg().
191 */
192
193/**
194 * struct ablkcipher_alg - asynchronous block cipher definition
195 * @min_keysize: Minimum key size supported by the transformation. This is the
196 * smallest key length supported by this transformation algorithm.
197 * This must be set to one of the pre-defined values as this is
198 * not hardware specific. Possible values for this field can be
199 * found via git grep "_MIN_KEY_SIZE" include/crypto/
200 * @max_keysize: Maximum key size supported by the transformation. This is the
201 * largest key length supported by this transformation algorithm.
202 * This must be set to one of the pre-defined values as this is
203 * not hardware specific. Possible values for this field can be
204 * found via git grep "_MAX_KEY_SIZE" include/crypto/
205 * @setkey: Set key for the transformation. This function is used to either
206 * program a supplied key into the hardware or store the key in the
207 * transformation context for programming it later. Note that this
208 * function does modify the transformation context. This function can
209 * be called multiple times during the existence of the transformation
210 * object, so one must make sure the key is properly reprogrammed into
211 * the hardware. This function is also responsible for checking the key
212 * length for validity. In case a software fallback was put in place in
213 * the @cra_init call, this function might need to use the fallback if
214 * the algorithm doesn't support all of the key sizes.
215 * @encrypt: Encrypt a scatterlist of blocks. This function is used to encrypt
216 * the supplied scatterlist containing the blocks of data. The crypto
217 * API consumer is responsible for aligning the entries of the
218 * scatterlist properly and making sure the chunks are correctly
219 * sized. In case a software fallback was put in place in the
220 * @cra_init call, this function might need to use the fallback if
221 * the algorithm doesn't support all of the key sizes. In case the
222 * key was stored in transformation context, the key might need to be
223 * re-programmed into the hardware in this function. This function
224 * shall not modify the transformation context, as this function may
225 * be called in parallel with the same transformation object.
226 * @decrypt: Decrypt a single block. This is a reverse counterpart to @encrypt
227 * and the conditions are exactly the same.
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100228 * @ivsize: IV size applicable for transformation. The consumer must provide an
229 * IV of exactly that size to perform the encrypt or decrypt operation.
230 *
Eric Biggersc79b4112018-12-16 15:55:06 -0800231 * All fields except @ivsize are mandatory and must be filled.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 */
Herbert Xub5b7f082007-04-16 20:48:54 +1000233struct ablkcipher_alg {
234 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
235 unsigned int keylen);
236 int (*encrypt)(struct ablkcipher_request *req);
237 int (*decrypt)(struct ablkcipher_request *req);
Herbert Xu23508e12007-11-27 21:33:24 +0800238
Herbert Xub5b7f082007-04-16 20:48:54 +1000239 unsigned int min_keysize;
240 unsigned int max_keysize;
241 unsigned int ivsize;
242};
243
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100244/**
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100245 * struct blkcipher_alg - synchronous block cipher definition
246 * @min_keysize: see struct ablkcipher_alg
247 * @max_keysize: see struct ablkcipher_alg
248 * @setkey: see struct ablkcipher_alg
249 * @encrypt: see struct ablkcipher_alg
250 * @decrypt: see struct ablkcipher_alg
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100251 * @ivsize: see struct ablkcipher_alg
252 *
Eric Biggersc79b4112018-12-16 15:55:06 -0800253 * All fields except @ivsize are mandatory and must be filled.
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100254 */
Herbert Xu5cde0af2006-08-22 00:07:53 +1000255struct blkcipher_alg {
256 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
257 unsigned int keylen);
258 int (*encrypt)(struct blkcipher_desc *desc,
259 struct scatterlist *dst, struct scatterlist *src,
260 unsigned int nbytes);
261 int (*decrypt)(struct blkcipher_desc *desc,
262 struct scatterlist *dst, struct scatterlist *src,
263 unsigned int nbytes);
264
265 unsigned int min_keysize;
266 unsigned int max_keysize;
267 unsigned int ivsize;
268};
269
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100270/**
271 * struct cipher_alg - single-block symmetric ciphers definition
272 * @cia_min_keysize: Minimum key size supported by the transformation. This is
273 * the smallest key length supported by this transformation
274 * algorithm. This must be set to one of the pre-defined
275 * values as this is not hardware specific. Possible values
276 * for this field can be found via git grep "_MIN_KEY_SIZE"
277 * include/crypto/
278 * @cia_max_keysize: Maximum key size supported by the transformation. This is
279 * the largest key length supported by this transformation
280 * algorithm. This must be set to one of the pre-defined values
281 * as this is not hardware specific. Possible values for this
282 * field can be found via git grep "_MAX_KEY_SIZE"
283 * include/crypto/
284 * @cia_setkey: Set key for the transformation. This function is used to either
285 * program a supplied key into the hardware or store the key in the
286 * transformation context for programming it later. Note that this
287 * function does modify the transformation context. This function
288 * can be called multiple times during the existence of the
289 * transformation object, so one must make sure the key is properly
290 * reprogrammed into the hardware. This function is also
291 * responsible for checking the key length for validity.
292 * @cia_encrypt: Encrypt a single block. This function is used to encrypt a
293 * single block of data, which must be @cra_blocksize big. This
294 * always operates on a full @cra_blocksize and it is not possible
295 * to encrypt a block of smaller size. The supplied buffers must
296 * therefore also be at least of @cra_blocksize size. Both the
297 * input and output buffers are always aligned to @cra_alignmask.
298 * In case either of the input or output buffer supplied by user
299 * of the crypto API is not aligned to @cra_alignmask, the crypto
300 * API will re-align the buffers. The re-alignment means that a
301 * new buffer will be allocated, the data will be copied into the
302 * new buffer, then the processing will happen on the new buffer,
303 * then the data will be copied back into the original buffer and
304 * finally the new buffer will be freed. In case a software
305 * fallback was put in place in the @cra_init call, this function
306 * might need to use the fallback if the algorithm doesn't support
307 * all of the key sizes. In case the key was stored in
308 * transformation context, the key might need to be re-programmed
309 * into the hardware in this function. This function shall not
310 * modify the transformation context, as this function may be
311 * called in parallel with the same transformation object.
312 * @cia_decrypt: Decrypt a single block. This is a reverse counterpart to
313 * @cia_encrypt, and the conditions are exactly the same.
314 *
315 * All fields are mandatory and must be filled.
316 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317struct cipher_alg {
318 unsigned int cia_min_keysize;
319 unsigned int cia_max_keysize;
Herbert Xu6c2bb982006-05-16 22:09:29 +1000320 int (*cia_setkey)(struct crypto_tfm *tfm, const u8 *key,
Herbert Xu560c06a2006-08-13 14:16:39 +1000321 unsigned int keylen);
Herbert Xu6c2bb982006-05-16 22:09:29 +1000322 void (*cia_encrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
323 void (*cia_decrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324};
325
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326struct compress_alg {
Herbert Xu6c2bb982006-05-16 22:09:29 +1000327 int (*coa_compress)(struct crypto_tfm *tfm, const u8 *src,
328 unsigned int slen, u8 *dst, unsigned int *dlen);
329 int (*coa_decompress)(struct crypto_tfm *tfm, const u8 *src,
330 unsigned int slen, u8 *dst, unsigned int *dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331};
332
Corentin Labbe17c18f92018-11-29 14:42:24 +0000333#ifdef CONFIG_CRYPTO_STATS
334/*
335 * struct crypto_istat_aead - statistics for AEAD algorithm
336 * @encrypt_cnt: number of encrypt requests
337 * @encrypt_tlen: total data size handled by encrypt requests
338 * @decrypt_cnt: number of decrypt requests
339 * @decrypt_tlen: total data size handled by decrypt requests
Corentin Labbe44f13132018-11-29 14:42:25 +0000340 * @err_cnt: number of error for AEAD requests
Corentin Labbe17c18f92018-11-29 14:42:24 +0000341 */
342struct crypto_istat_aead {
343 atomic64_t encrypt_cnt;
344 atomic64_t encrypt_tlen;
345 atomic64_t decrypt_cnt;
346 atomic64_t decrypt_tlen;
Corentin Labbe44f13132018-11-29 14:42:25 +0000347 atomic64_t err_cnt;
Corentin Labbe17c18f92018-11-29 14:42:24 +0000348};
349
350/*
351 * struct crypto_istat_akcipher - statistics for akcipher algorithm
352 * @encrypt_cnt: number of encrypt requests
353 * @encrypt_tlen: total data size handled by encrypt requests
354 * @decrypt_cnt: number of decrypt requests
355 * @decrypt_tlen: total data size handled by decrypt requests
356 * @verify_cnt: number of verify operation
357 * @sign_cnt: number of sign requests
Corentin Labbe44f13132018-11-29 14:42:25 +0000358 * @err_cnt: number of error for akcipher requests
Corentin Labbe17c18f92018-11-29 14:42:24 +0000359 */
360struct crypto_istat_akcipher {
361 atomic64_t encrypt_cnt;
362 atomic64_t encrypt_tlen;
363 atomic64_t decrypt_cnt;
364 atomic64_t decrypt_tlen;
365 atomic64_t verify_cnt;
366 atomic64_t sign_cnt;
Corentin Labbe44f13132018-11-29 14:42:25 +0000367 atomic64_t err_cnt;
Corentin Labbe17c18f92018-11-29 14:42:24 +0000368};
369
370/*
371 * struct crypto_istat_cipher - statistics for cipher algorithm
372 * @encrypt_cnt: number of encrypt requests
373 * @encrypt_tlen: total data size handled by encrypt requests
374 * @decrypt_cnt: number of decrypt requests
375 * @decrypt_tlen: total data size handled by decrypt requests
Corentin Labbe44f13132018-11-29 14:42:25 +0000376 * @err_cnt: number of error for cipher requests
Corentin Labbe17c18f92018-11-29 14:42:24 +0000377 */
378struct crypto_istat_cipher {
379 atomic64_t encrypt_cnt;
380 atomic64_t encrypt_tlen;
381 atomic64_t decrypt_cnt;
382 atomic64_t decrypt_tlen;
Corentin Labbe44f13132018-11-29 14:42:25 +0000383 atomic64_t err_cnt;
Corentin Labbe17c18f92018-11-29 14:42:24 +0000384};
385
386/*
387 * struct crypto_istat_compress - statistics for compress algorithm
388 * @compress_cnt: number of compress requests
389 * @compress_tlen: total data size handled by compress requests
390 * @decompress_cnt: number of decompress requests
391 * @decompress_tlen: total data size handled by decompress requests
Corentin Labbe44f13132018-11-29 14:42:25 +0000392 * @err_cnt: number of error for compress requests
Corentin Labbe17c18f92018-11-29 14:42:24 +0000393 */
394struct crypto_istat_compress {
395 atomic64_t compress_cnt;
396 atomic64_t compress_tlen;
397 atomic64_t decompress_cnt;
398 atomic64_t decompress_tlen;
Corentin Labbe44f13132018-11-29 14:42:25 +0000399 atomic64_t err_cnt;
Corentin Labbe17c18f92018-11-29 14:42:24 +0000400};
401
402/*
403 * struct crypto_istat_hash - statistics for has algorithm
404 * @hash_cnt: number of hash requests
405 * @hash_tlen: total data size hashed
Corentin Labbe44f13132018-11-29 14:42:25 +0000406 * @err_cnt: number of error for hash requests
Corentin Labbe17c18f92018-11-29 14:42:24 +0000407 */
408struct crypto_istat_hash {
409 atomic64_t hash_cnt;
410 atomic64_t hash_tlen;
Corentin Labbe44f13132018-11-29 14:42:25 +0000411 atomic64_t err_cnt;
Corentin Labbe17c18f92018-11-29 14:42:24 +0000412};
413
414/*
415 * struct crypto_istat_kpp - statistics for KPP algorithm
416 * @setsecret_cnt: number of setsecrey operation
417 * @generate_public_key_cnt: number of generate_public_key operation
418 * @compute_shared_secret_cnt: number of compute_shared_secret operation
Corentin Labbe44f13132018-11-29 14:42:25 +0000419 * @err_cnt: number of error for KPP requests
Corentin Labbe17c18f92018-11-29 14:42:24 +0000420 */
421struct crypto_istat_kpp {
422 atomic64_t setsecret_cnt;
423 atomic64_t generate_public_key_cnt;
424 atomic64_t compute_shared_secret_cnt;
Corentin Labbe44f13132018-11-29 14:42:25 +0000425 atomic64_t err_cnt;
Corentin Labbe17c18f92018-11-29 14:42:24 +0000426};
427
428/*
429 * struct crypto_istat_rng: statistics for RNG algorithm
430 * @generate_cnt: number of RNG generate requests
431 * @generate_tlen: total data size of generated data by the RNG
432 * @seed_cnt: number of times the RNG was seeded
Corentin Labbe44f13132018-11-29 14:42:25 +0000433 * @err_cnt: number of error for RNG requests
Corentin Labbe17c18f92018-11-29 14:42:24 +0000434 */
435struct crypto_istat_rng {
436 atomic64_t generate_cnt;
437 atomic64_t generate_tlen;
438 atomic64_t seed_cnt;
Corentin Labbe44f13132018-11-29 14:42:25 +0000439 atomic64_t err_cnt;
Corentin Labbe17c18f92018-11-29 14:42:24 +0000440};
441#endif /* CONFIG_CRYPTO_STATS */
Neil Horman17f0f4a2008-08-14 22:15:52 +1000442
Herbert Xub5b7f082007-04-16 20:48:54 +1000443#define cra_ablkcipher cra_u.ablkcipher
Herbert Xu5cde0af2006-08-22 00:07:53 +1000444#define cra_blkcipher cra_u.blkcipher
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445#define cra_cipher cra_u.cipher
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446#define cra_compress cra_u.compress
447
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100448/**
449 * struct crypto_alg - definition of a cryptograpic cipher algorithm
450 * @cra_flags: Flags describing this transformation. See include/linux/crypto.h
451 * CRYPTO_ALG_* flags for the flags which go in here. Those are
452 * used for fine-tuning the description of the transformation
453 * algorithm.
454 * @cra_blocksize: Minimum block size of this transformation. The size in bytes
455 * of the smallest possible unit which can be transformed with
456 * this algorithm. The users must respect this value.
457 * In case of HASH transformation, it is possible for a smaller
458 * block than @cra_blocksize to be passed to the crypto API for
459 * transformation, in case of any other transformation type, an
460 * error will be returned upon any attempt to transform smaller
461 * than @cra_blocksize chunks.
462 * @cra_ctxsize: Size of the operational context of the transformation. This
463 * value informs the kernel crypto API about the memory size
464 * needed to be allocated for the transformation context.
465 * @cra_alignmask: Alignment mask for the input and output data buffer. The data
466 * buffer containing the input data for the algorithm must be
467 * aligned to this alignment mask. The data buffer for the
468 * output data must be aligned to this alignment mask. Note that
469 * the Crypto API will do the re-alignment in software, but
470 * only under special conditions and there is a performance hit.
471 * The re-alignment happens at these occasions for different
472 * @cra_u types: cipher -- For both input data and output data
473 * buffer; ahash -- For output hash destination buf; shash --
474 * For output hash destination buf.
475 * This is needed on hardware which is flawed by design and
476 * cannot pick data from arbitrary addresses.
477 * @cra_priority: Priority of this transformation implementation. In case
478 * multiple transformations with same @cra_name are available to
479 * the Crypto API, the kernel will use the one with highest
480 * @cra_priority.
481 * @cra_name: Generic name (usable by multiple implementations) of the
482 * transformation algorithm. This is the name of the transformation
483 * itself. This field is used by the kernel when looking up the
484 * providers of particular transformation.
485 * @cra_driver_name: Unique name of the transformation provider. This is the
486 * name of the provider of the transformation. This can be any
487 * arbitrary value, but in the usual case, this contains the
488 * name of the chip or provider and the name of the
489 * transformation algorithm.
490 * @cra_type: Type of the cryptographic transformation. This is a pointer to
491 * struct crypto_type, which implements callbacks common for all
Masanari Iida12f7c142015-06-04 00:01:21 +0900492 * transformation types. There are multiple options:
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100493 * &crypto_blkcipher_type, &crypto_ablkcipher_type,
Herbert Xub0d955b2015-08-14 15:30:41 +0800494 * &crypto_ahash_type, &crypto_rng_type.
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100495 * This field might be empty. In that case, there are no common
496 * callbacks. This is the case for: cipher, compress, shash.
497 * @cra_u: Callbacks implementing the transformation. This is a union of
498 * multiple structures. Depending on the type of transformation selected
499 * by @cra_type and @cra_flags above, the associated structure must be
500 * filled with callbacks. This field might be empty. This is the case
501 * for ahash, shash.
502 * @cra_init: Initialize the cryptographic transformation object. This function
503 * is used to initialize the cryptographic transformation object.
504 * This function is called only once at the instantiation time, right
505 * after the transformation context was allocated. In case the
506 * cryptographic hardware has some special requirements which need to
507 * be handled by software, this function shall check for the precise
508 * requirement of the transformation and put any software fallbacks
509 * in place.
510 * @cra_exit: Deinitialize the cryptographic transformation object. This is a
511 * counterpart to @cra_init, used to remove various changes set in
512 * @cra_init.
Gary R Hook0063ec42018-03-14 17:15:52 -0500513 * @cra_u.ablkcipher: Union member which contains an asynchronous block cipher
514 * definition. See @struct @ablkcipher_alg.
515 * @cra_u.blkcipher: Union member which contains a synchronous block cipher
516 * definition See @struct @blkcipher_alg.
517 * @cra_u.cipher: Union member which contains a single-block symmetric cipher
518 * definition. See @struct @cipher_alg.
519 * @cra_u.compress: Union member which contains a (de)compression algorithm.
520 * See @struct @compress_alg.
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100521 * @cra_module: Owner of this transformation implementation. Set to THIS_MODULE
522 * @cra_list: internally used
523 * @cra_users: internally used
524 * @cra_refcnt: internally used
525 * @cra_destroy: internally used
526 *
Corentin Labbe17c18f92018-11-29 14:42:24 +0000527 * @stats: union of all possible crypto_istat_xxx structures
Corentin Labbebfad6cb2018-12-13 08:36:38 +0000528 * @stats.aead: statistics for AEAD algorithm
529 * @stats.akcipher: statistics for akcipher algorithm
530 * @stats.cipher: statistics for cipher algorithm
531 * @stats.compress: statistics for compress algorithm
532 * @stats.hash: statistics for hash algorithm
533 * @stats.rng: statistics for rng algorithm
534 * @stats.kpp: statistics for KPP algorithm
Corentin Labbecac58182018-09-19 10:10:54 +0000535 *
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100536 * The struct crypto_alg describes a generic Crypto API algorithm and is common
537 * for all of the transformations. Any variable not documented here shall not
538 * be used by a cipher implementation as it is internal to the Crypto API.
539 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540struct crypto_alg {
541 struct list_head cra_list;
Herbert Xu6bfd4802006-09-21 11:39:29 +1000542 struct list_head cra_users;
543
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 u32 cra_flags;
545 unsigned int cra_blocksize;
546 unsigned int cra_ctxsize;
Herbert Xu95477372005-07-06 13:52:09 -0700547 unsigned int cra_alignmask;
Herbert Xu5cb1454b2005-11-05 16:58:14 +1100548
549 int cra_priority;
Eric Biggersce8614a2017-12-29 10:00:46 -0600550 refcount_t cra_refcnt;
Herbert Xu5cb1454b2005-11-05 16:58:14 +1100551
Herbert Xud913ea02006-05-21 08:45:26 +1000552 char cra_name[CRYPTO_MAX_ALG_NAME];
553 char cra_driver_name[CRYPTO_MAX_ALG_NAME];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
Herbert Xue853c3c2006-08-22 00:06:54 +1000555 const struct crypto_type *cra_type;
556
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 union {
Herbert Xub5b7f082007-04-16 20:48:54 +1000558 struct ablkcipher_alg ablkcipher;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000559 struct blkcipher_alg blkcipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 struct cipher_alg cipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 struct compress_alg compress;
562 } cra_u;
Herbert Xuc7fc0592006-05-24 13:02:26 +1000563
564 int (*cra_init)(struct crypto_tfm *tfm);
565 void (*cra_exit)(struct crypto_tfm *tfm);
Herbert Xu6521f302006-08-06 20:28:44 +1000566 void (*cra_destroy)(struct crypto_alg *alg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
568 struct module *cra_module;
Corentin Labbecac58182018-09-19 10:10:54 +0000569
Corentin Labbe2ced2602018-11-29 14:42:16 +0000570#ifdef CONFIG_CRYPTO_STATS
Corentin Labbecac58182018-09-19 10:10:54 +0000571 union {
Corentin Labbe17c18f92018-11-29 14:42:24 +0000572 struct crypto_istat_aead aead;
573 struct crypto_istat_akcipher akcipher;
574 struct crypto_istat_cipher cipher;
575 struct crypto_istat_compress compress;
576 struct crypto_istat_hash hash;
577 struct crypto_istat_rng rng;
578 struct crypto_istat_kpp kpp;
579 } stats;
Corentin Labbe2ced2602018-11-29 14:42:16 +0000580#endif /* CONFIG_CRYPTO_STATS */
Corentin Labbecac58182018-09-19 10:10:54 +0000581
Herbert Xuedf18b92015-06-18 14:00:48 +0800582} CRYPTO_MINALIGN_ATTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
Corentin Labbef7d76e02018-11-29 14:42:21 +0000584#ifdef CONFIG_CRYPTO_STATS
Corentin Labbe1f6669b2018-11-29 14:42:26 +0000585void crypto_stats_init(struct crypto_alg *alg);
Corentin Labbef7d76e02018-11-29 14:42:21 +0000586void crypto_stats_get(struct crypto_alg *alg);
587void crypto_stats_ablkcipher_encrypt(unsigned int nbytes, int ret, struct crypto_alg *alg);
588void crypto_stats_ablkcipher_decrypt(unsigned int nbytes, int ret, struct crypto_alg *alg);
589void crypto_stats_aead_encrypt(unsigned int cryptlen, struct crypto_alg *alg, int ret);
590void crypto_stats_aead_decrypt(unsigned int cryptlen, struct crypto_alg *alg, int ret);
591void crypto_stats_ahash_update(unsigned int nbytes, int ret, struct crypto_alg *alg);
592void crypto_stats_ahash_final(unsigned int nbytes, int ret, struct crypto_alg *alg);
593void crypto_stats_akcipher_encrypt(unsigned int src_len, int ret, struct crypto_alg *alg);
594void crypto_stats_akcipher_decrypt(unsigned int src_len, int ret, struct crypto_alg *alg);
595void crypto_stats_akcipher_sign(int ret, struct crypto_alg *alg);
596void crypto_stats_akcipher_verify(int ret, struct crypto_alg *alg);
597void crypto_stats_compress(unsigned int slen, int ret, struct crypto_alg *alg);
598void crypto_stats_decompress(unsigned int slen, int ret, struct crypto_alg *alg);
599void crypto_stats_kpp_set_secret(struct crypto_alg *alg, int ret);
600void crypto_stats_kpp_generate_public_key(struct crypto_alg *alg, int ret);
601void crypto_stats_kpp_compute_shared_secret(struct crypto_alg *alg, int ret);
602void crypto_stats_rng_seed(struct crypto_alg *alg, int ret);
603void crypto_stats_rng_generate(struct crypto_alg *alg, unsigned int dlen, int ret);
604void crypto_stats_skcipher_encrypt(unsigned int cryptlen, int ret, struct crypto_alg *alg);
605void crypto_stats_skcipher_decrypt(unsigned int cryptlen, int ret, struct crypto_alg *alg);
606#else
Corentin Labbe1f6669b2018-11-29 14:42:26 +0000607static inline void crypto_stats_init(struct crypto_alg *alg)
608{}
Corentin Labbef7d76e02018-11-29 14:42:21 +0000609static inline void crypto_stats_get(struct crypto_alg *alg)
610{}
611static inline void crypto_stats_ablkcipher_encrypt(unsigned int nbytes, int ret, struct crypto_alg *alg)
612{}
613static inline void crypto_stats_ablkcipher_decrypt(unsigned int nbytes, int ret, struct crypto_alg *alg)
614{}
615static inline void crypto_stats_aead_encrypt(unsigned int cryptlen, struct crypto_alg *alg, int ret)
616{}
617static inline void crypto_stats_aead_decrypt(unsigned int cryptlen, struct crypto_alg *alg, int ret)
618{}
619static inline void crypto_stats_ahash_update(unsigned int nbytes, int ret, struct crypto_alg *alg)
620{}
621static inline void crypto_stats_ahash_final(unsigned int nbytes, int ret, struct crypto_alg *alg)
622{}
623static inline void crypto_stats_akcipher_encrypt(unsigned int src_len, int ret, struct crypto_alg *alg)
624{}
625static inline void crypto_stats_akcipher_decrypt(unsigned int src_len, int ret, struct crypto_alg *alg)
626{}
627static inline void crypto_stats_akcipher_sign(int ret, struct crypto_alg *alg)
628{}
629static inline void crypto_stats_akcipher_verify(int ret, struct crypto_alg *alg)
630{}
631static inline void crypto_stats_compress(unsigned int slen, int ret, struct crypto_alg *alg)
632{}
633static inline void crypto_stats_decompress(unsigned int slen, int ret, struct crypto_alg *alg)
634{}
635static inline void crypto_stats_kpp_set_secret(struct crypto_alg *alg, int ret)
636{}
637static inline void crypto_stats_kpp_generate_public_key(struct crypto_alg *alg, int ret)
638{}
639static inline void crypto_stats_kpp_compute_shared_secret(struct crypto_alg *alg, int ret)
640{}
641static inline void crypto_stats_rng_seed(struct crypto_alg *alg, int ret)
642{}
643static inline void crypto_stats_rng_generate(struct crypto_alg *alg, unsigned int dlen, int ret)
644{}
645static inline void crypto_stats_skcipher_encrypt(unsigned int cryptlen, int ret, struct crypto_alg *alg)
646{}
647static inline void crypto_stats_skcipher_decrypt(unsigned int cryptlen, int ret, struct crypto_alg *alg)
648{}
649#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650/*
Gilad Ben-Yossefada69a12017-10-18 08:00:38 +0100651 * A helper struct for waiting for completion of async crypto ops
652 */
653struct crypto_wait {
654 struct completion completion;
655 int err;
656};
657
658/*
659 * Macro for declaring a crypto op async wait object on stack
660 */
661#define DECLARE_CRYPTO_WAIT(_wait) \
662 struct crypto_wait _wait = { \
663 COMPLETION_INITIALIZER_ONSTACK((_wait).completion), 0 }
664
665/*
666 * Async ops completion helper functioons
667 */
668void crypto_req_done(struct crypto_async_request *req, int err);
669
670static inline int crypto_wait_req(int err, struct crypto_wait *wait)
671{
672 switch (err) {
673 case -EINPROGRESS:
674 case -EBUSY:
675 wait_for_completion(&wait->completion);
676 reinit_completion(&wait->completion);
677 err = wait->err;
678 break;
679 };
680
681 return err;
682}
683
684static inline void crypto_init_wait(struct crypto_wait *wait)
685{
686 init_completion(&wait->completion);
687}
688
689/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 * Algorithm registration interface.
691 */
692int crypto_register_alg(struct crypto_alg *alg);
693int crypto_unregister_alg(struct crypto_alg *alg);
Mark Brown4b004342012-01-17 23:34:26 +0000694int crypto_register_algs(struct crypto_alg *algs, int count);
695int crypto_unregister_algs(struct crypto_alg *algs, int count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
697/*
698 * Algorithm query interface.
699 */
Herbert Xufce32d72006-08-26 17:35:45 +1000700int crypto_has_alg(const char *name, u32 type, u32 mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
702/*
703 * Transforms: user-instantiated objects which encapsulate algorithms
Herbert Xu6d7d684d2006-07-30 11:53:01 +1000704 * and core processing logic. Managed via crypto_alloc_*() and
705 * crypto_free_*(), as well as the various helpers below.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
Herbert Xu32e3983f2007-03-24 14:35:34 +1100708struct ablkcipher_tfm {
709 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
710 unsigned int keylen);
711 int (*encrypt)(struct ablkcipher_request *req);
712 int (*decrypt)(struct ablkcipher_request *req);
Herbert Xu61da88e2007-12-17 21:51:27 +0800713
Herbert Xuecfc4322007-12-05 21:08:36 +1100714 struct crypto_ablkcipher *base;
715
Herbert Xu32e3983f2007-03-24 14:35:34 +1100716 unsigned int ivsize;
717 unsigned int reqsize;
718};
719
Herbert Xu5cde0af2006-08-22 00:07:53 +1000720struct blkcipher_tfm {
721 void *iv;
722 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
723 unsigned int keylen);
724 int (*encrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
725 struct scatterlist *src, unsigned int nbytes);
726 int (*decrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
727 struct scatterlist *src, unsigned int nbytes);
728};
729
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730struct cipher_tfm {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 int (*cit_setkey)(struct crypto_tfm *tfm,
732 const u8 *key, unsigned int keylen);
Herbert Xuf28776a2006-08-13 20:58:18 +1000733 void (*cit_encrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
734 void (*cit_decrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735};
736
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737struct compress_tfm {
738 int (*cot_compress)(struct crypto_tfm *tfm,
739 const u8 *src, unsigned int slen,
740 u8 *dst, unsigned int *dlen);
741 int (*cot_decompress)(struct crypto_tfm *tfm,
742 const u8 *src, unsigned int slen,
743 u8 *dst, unsigned int *dlen);
744};
745
Herbert Xu32e3983f2007-03-24 14:35:34 +1100746#define crt_ablkcipher crt_u.ablkcipher
Herbert Xu5cde0af2006-08-22 00:07:53 +1000747#define crt_blkcipher crt_u.blkcipher
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748#define crt_cipher crt_u.cipher
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749#define crt_compress crt_u.compress
750
751struct crypto_tfm {
752
753 u32 crt_flags;
754
755 union {
Herbert Xu32e3983f2007-03-24 14:35:34 +1100756 struct ablkcipher_tfm ablkcipher;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000757 struct blkcipher_tfm blkcipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 struct cipher_tfm cipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 struct compress_tfm compress;
760 } crt_u;
Herbert Xu4a779482008-09-13 18:19:03 -0700761
762 void (*exit)(struct crypto_tfm *tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
764 struct crypto_alg *__crt_alg;
Herbert Xuf10b7892006-01-25 22:34:01 +1100765
Herbert Xu79911102006-08-21 21:03:52 +1000766 void *__crt_ctx[] CRYPTO_MINALIGN_ATTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767};
768
Herbert Xu32e3983f2007-03-24 14:35:34 +1100769struct crypto_ablkcipher {
770 struct crypto_tfm base;
771};
772
Herbert Xu5cde0af2006-08-22 00:07:53 +1000773struct crypto_blkcipher {
774 struct crypto_tfm base;
775};
776
Herbert Xu78a1fe42006-12-24 10:02:00 +1100777struct crypto_cipher {
778 struct crypto_tfm base;
779};
780
781struct crypto_comp {
782 struct crypto_tfm base;
783};
784
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000785enum {
786 CRYPTOA_UNSPEC,
787 CRYPTOA_ALG,
Herbert Xuebc610e2007-01-01 18:37:02 +1100788 CRYPTOA_TYPE,
Herbert Xu39e1ee012007-08-29 19:27:26 +0800789 CRYPTOA_U32,
Herbert Xuebc610e2007-01-01 18:37:02 +1100790 __CRYPTOA_MAX,
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000791};
792
Herbert Xuebc610e2007-01-01 18:37:02 +1100793#define CRYPTOA_MAX (__CRYPTOA_MAX - 1)
794
Herbert Xu39e1ee012007-08-29 19:27:26 +0800795/* Maximum number of (rtattr) parameters for each template. */
796#define CRYPTO_MAX_ATTRS 32
797
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000798struct crypto_attr_alg {
799 char name[CRYPTO_MAX_ALG_NAME];
800};
801
Herbert Xuebc610e2007-01-01 18:37:02 +1100802struct crypto_attr_type {
803 u32 type;
804 u32 mask;
805};
806
Herbert Xu39e1ee012007-08-29 19:27:26 +0800807struct crypto_attr_u32 {
808 u32 num;
809};
810
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811/*
812 * Transform user interface.
813 */
814
Herbert Xu6d7d684d2006-07-30 11:53:01 +1000815struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask);
Herbert Xu7b2cd922009-02-05 16:48:24 +1100816void crypto_destroy_tfm(void *mem, struct crypto_tfm *tfm);
817
818static inline void crypto_free_tfm(struct crypto_tfm *tfm)
819{
820 return crypto_destroy_tfm(tfm, tfm);
821}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
Herbert Xuda7f0332008-07-31 17:08:25 +0800823int alg_test(const char *driver, const char *alg, u32 type, u32 mask);
824
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825/*
826 * Transform helpers which query the underlying algorithm.
827 */
828static inline const char *crypto_tfm_alg_name(struct crypto_tfm *tfm)
829{
830 return tfm->__crt_alg->cra_name;
831}
832
Michal Ludvigb14cdd62006-07-09 09:02:24 +1000833static inline const char *crypto_tfm_alg_driver_name(struct crypto_tfm *tfm)
834{
835 return tfm->__crt_alg->cra_driver_name;
836}
837
838static inline int crypto_tfm_alg_priority(struct crypto_tfm *tfm)
839{
840 return tfm->__crt_alg->cra_priority;
841}
842
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm)
844{
845 return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK;
846}
847
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm)
849{
850 return tfm->__crt_alg->cra_blocksize;
851}
852
Herbert Xufbdae9f2005-07-06 13:53:29 -0700853static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm *tfm)
854{
855 return tfm->__crt_alg->cra_alignmask;
856}
857
Herbert Xuf28776a2006-08-13 20:58:18 +1000858static inline u32 crypto_tfm_get_flags(struct crypto_tfm *tfm)
859{
860 return tfm->crt_flags;
861}
862
863static inline void crypto_tfm_set_flags(struct crypto_tfm *tfm, u32 flags)
864{
865 tfm->crt_flags |= flags;
866}
867
868static inline void crypto_tfm_clear_flags(struct crypto_tfm *tfm, u32 flags)
869{
870 tfm->crt_flags &= ~flags;
871}
872
Herbert Xu40725182005-07-06 13:51:52 -0700873static inline void *crypto_tfm_ctx(struct crypto_tfm *tfm)
874{
Herbert Xuf10b7892006-01-25 22:34:01 +1100875 return tfm->__crt_ctx;
876}
877
878static inline unsigned int crypto_tfm_ctx_alignment(void)
879{
880 struct crypto_tfm *tfm;
881 return __alignof__(tfm->__crt_ctx);
Herbert Xu40725182005-07-06 13:51:52 -0700882}
883
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884/*
885 * API wrappers.
886 */
Herbert Xu32e3983f2007-03-24 14:35:34 +1100887static inline struct crypto_ablkcipher *__crypto_ablkcipher_cast(
888 struct crypto_tfm *tfm)
889{
890 return (struct crypto_ablkcipher *)tfm;
891}
892
Herbert Xu378f4f52007-12-17 20:07:31 +0800893static inline u32 crypto_skcipher_type(u32 type)
894{
Eric Biggersc79b4112018-12-16 15:55:06 -0800895 type &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xu378f4f52007-12-17 20:07:31 +0800896 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
897 return type;
898}
899
900static inline u32 crypto_skcipher_mask(u32 mask)
901{
Eric Biggersc79b4112018-12-16 15:55:06 -0800902 mask &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xu378f4f52007-12-17 20:07:31 +0800903 mask |= CRYPTO_ALG_TYPE_BLKCIPHER_MASK;
904 return mask;
905}
906
Stephan Muellerf13ec332014-11-12 05:28:22 +0100907/**
908 * DOC: Asynchronous Block Cipher API
909 *
910 * Asynchronous block cipher API is used with the ciphers of type
911 * CRYPTO_ALG_TYPE_ABLKCIPHER (listed as type "ablkcipher" in /proc/crypto).
912 *
913 * Asynchronous cipher operations imply that the function invocation for a
914 * cipher request returns immediately before the completion of the operation.
915 * The cipher request is scheduled as a separate kernel thread and therefore
916 * load-balanced on the different CPUs via the process scheduler. To allow
917 * the kernel crypto API to inform the caller about the completion of a cipher
918 * request, the caller must provide a callback function. That function is
919 * invoked with the cipher handle when the request completes.
920 *
921 * To support the asynchronous operation, additional information than just the
922 * cipher handle must be supplied to the kernel crypto API. That additional
923 * information is given by filling in the ablkcipher_request data structure.
924 *
925 * For the asynchronous block cipher API, the state is maintained with the tfm
926 * cipher handle. A single tfm can be used across multiple calls and in
927 * parallel. For asynchronous block cipher calls, context data supplied and
928 * only used by the caller can be referenced the request data structure in
929 * addition to the IV used for the cipher request. The maintenance of such
930 * state information would be important for a crypto driver implementer to
931 * have, because when calling the callback function upon completion of the
932 * cipher operation, that callback function may need some information about
933 * which operation just finished if it invoked multiple in parallel. This
934 * state information is unused by the kernel crypto API.
935 */
936
Herbert Xu32e3983f2007-03-24 14:35:34 +1100937static inline struct crypto_tfm *crypto_ablkcipher_tfm(
938 struct crypto_ablkcipher *tfm)
939{
940 return &tfm->base;
941}
942
Stephan Muellerf13ec332014-11-12 05:28:22 +0100943/**
944 * crypto_free_ablkcipher() - zeroize and free cipher handle
945 * @tfm: cipher handle to be freed
946 */
Herbert Xu32e3983f2007-03-24 14:35:34 +1100947static inline void crypto_free_ablkcipher(struct crypto_ablkcipher *tfm)
948{
949 crypto_free_tfm(crypto_ablkcipher_tfm(tfm));
950}
951
Stephan Muellerf13ec332014-11-12 05:28:22 +0100952/**
953 * crypto_has_ablkcipher() - Search for the availability of an ablkcipher.
954 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
955 * ablkcipher
956 * @type: specifies the type of the cipher
957 * @mask: specifies the mask for the cipher
958 *
959 * Return: true when the ablkcipher is known to the kernel crypto API; false
960 * otherwise
961 */
Herbert Xu32e3983f2007-03-24 14:35:34 +1100962static inline int crypto_has_ablkcipher(const char *alg_name, u32 type,
963 u32 mask)
964{
Herbert Xu378f4f52007-12-17 20:07:31 +0800965 return crypto_has_alg(alg_name, crypto_skcipher_type(type),
966 crypto_skcipher_mask(mask));
Herbert Xu32e3983f2007-03-24 14:35:34 +1100967}
968
969static inline struct ablkcipher_tfm *crypto_ablkcipher_crt(
970 struct crypto_ablkcipher *tfm)
971{
972 return &crypto_ablkcipher_tfm(tfm)->crt_ablkcipher;
973}
974
Stephan Muellerf13ec332014-11-12 05:28:22 +0100975/**
976 * crypto_ablkcipher_ivsize() - obtain IV size
977 * @tfm: cipher handle
978 *
979 * The size of the IV for the ablkcipher referenced by the cipher handle is
980 * returned. This IV size may be zero if the cipher does not need an IV.
981 *
982 * Return: IV size in bytes
983 */
Herbert Xu32e3983f2007-03-24 14:35:34 +1100984static inline unsigned int crypto_ablkcipher_ivsize(
985 struct crypto_ablkcipher *tfm)
986{
987 return crypto_ablkcipher_crt(tfm)->ivsize;
988}
989
Stephan Muellerf13ec332014-11-12 05:28:22 +0100990/**
991 * crypto_ablkcipher_blocksize() - obtain block size of cipher
992 * @tfm: cipher handle
993 *
994 * The block size for the ablkcipher referenced with the cipher handle is
995 * returned. The caller may use that information to allocate appropriate
996 * memory for the data returned by the encryption or decryption operation
997 *
998 * Return: block size of cipher
999 */
Herbert Xu32e3983f2007-03-24 14:35:34 +11001000static inline unsigned int crypto_ablkcipher_blocksize(
1001 struct crypto_ablkcipher *tfm)
1002{
1003 return crypto_tfm_alg_blocksize(crypto_ablkcipher_tfm(tfm));
1004}
1005
1006static inline unsigned int crypto_ablkcipher_alignmask(
1007 struct crypto_ablkcipher *tfm)
1008{
1009 return crypto_tfm_alg_alignmask(crypto_ablkcipher_tfm(tfm));
1010}
1011
1012static inline u32 crypto_ablkcipher_get_flags(struct crypto_ablkcipher *tfm)
1013{
1014 return crypto_tfm_get_flags(crypto_ablkcipher_tfm(tfm));
1015}
1016
1017static inline void crypto_ablkcipher_set_flags(struct crypto_ablkcipher *tfm,
1018 u32 flags)
1019{
1020 crypto_tfm_set_flags(crypto_ablkcipher_tfm(tfm), flags);
1021}
1022
1023static inline void crypto_ablkcipher_clear_flags(struct crypto_ablkcipher *tfm,
1024 u32 flags)
1025{
1026 crypto_tfm_clear_flags(crypto_ablkcipher_tfm(tfm), flags);
1027}
1028
Stephan Muellerf13ec332014-11-12 05:28:22 +01001029/**
1030 * crypto_ablkcipher_setkey() - set key for cipher
1031 * @tfm: cipher handle
1032 * @key: buffer holding the key
1033 * @keylen: length of the key in bytes
1034 *
1035 * The caller provided key is set for the ablkcipher referenced by the cipher
1036 * handle.
1037 *
1038 * Note, the key length determines the cipher type. Many block ciphers implement
1039 * different cipher modes depending on the key size, such as AES-128 vs AES-192
1040 * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
1041 * is performed.
1042 *
1043 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
1044 */
Herbert Xu32e3983f2007-03-24 14:35:34 +11001045static inline int crypto_ablkcipher_setkey(struct crypto_ablkcipher *tfm,
1046 const u8 *key, unsigned int keylen)
1047{
Herbert Xuecfc4322007-12-05 21:08:36 +11001048 struct ablkcipher_tfm *crt = crypto_ablkcipher_crt(tfm);
1049
1050 return crt->setkey(crt->base, key, keylen);
Herbert Xu32e3983f2007-03-24 14:35:34 +11001051}
1052
Stephan Muellerf13ec332014-11-12 05:28:22 +01001053/**
1054 * crypto_ablkcipher_reqtfm() - obtain cipher handle from request
1055 * @req: ablkcipher_request out of which the cipher handle is to be obtained
1056 *
1057 * Return the crypto_ablkcipher handle when furnishing an ablkcipher_request
1058 * data structure.
1059 *
1060 * Return: crypto_ablkcipher handle
1061 */
Herbert Xu32e3983f2007-03-24 14:35:34 +11001062static inline struct crypto_ablkcipher *crypto_ablkcipher_reqtfm(
1063 struct ablkcipher_request *req)
1064{
1065 return __crypto_ablkcipher_cast(req->base.tfm);
1066}
1067
Stephan Muellerf13ec332014-11-12 05:28:22 +01001068/**
1069 * crypto_ablkcipher_encrypt() - encrypt plaintext
1070 * @req: reference to the ablkcipher_request handle that holds all information
1071 * needed to perform the cipher operation
1072 *
1073 * Encrypt plaintext data using the ablkcipher_request handle. That data
1074 * structure and how it is filled with data is discussed with the
1075 * ablkcipher_request_* functions.
1076 *
1077 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1078 */
Herbert Xu32e3983f2007-03-24 14:35:34 +11001079static inline int crypto_ablkcipher_encrypt(struct ablkcipher_request *req)
1080{
1081 struct ablkcipher_tfm *crt =
1082 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
Corentin Labbef7d76e02018-11-29 14:42:21 +00001083 struct crypto_alg *alg = crt->base->base.__crt_alg;
1084 unsigned int nbytes = req->nbytes;
Corentin Labbecac58182018-09-19 10:10:54 +00001085 int ret;
1086
Corentin Labbef7d76e02018-11-29 14:42:21 +00001087 crypto_stats_get(alg);
Corentin Labbecac58182018-09-19 10:10:54 +00001088 ret = crt->encrypt(req);
Corentin Labbef7d76e02018-11-29 14:42:21 +00001089 crypto_stats_ablkcipher_encrypt(nbytes, ret, alg);
Corentin Labbecac58182018-09-19 10:10:54 +00001090 return ret;
Herbert Xu32e3983f2007-03-24 14:35:34 +11001091}
1092
Stephan Muellerf13ec332014-11-12 05:28:22 +01001093/**
1094 * crypto_ablkcipher_decrypt() - decrypt ciphertext
1095 * @req: reference to the ablkcipher_request handle that holds all information
1096 * needed to perform the cipher operation
1097 *
1098 * Decrypt ciphertext data using the ablkcipher_request handle. That data
1099 * structure and how it is filled with data is discussed with the
1100 * ablkcipher_request_* functions.
1101 *
1102 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1103 */
Herbert Xu32e3983f2007-03-24 14:35:34 +11001104static inline int crypto_ablkcipher_decrypt(struct ablkcipher_request *req)
1105{
1106 struct ablkcipher_tfm *crt =
1107 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
Corentin Labbef7d76e02018-11-29 14:42:21 +00001108 struct crypto_alg *alg = crt->base->base.__crt_alg;
1109 unsigned int nbytes = req->nbytes;
Corentin Labbecac58182018-09-19 10:10:54 +00001110 int ret;
1111
Corentin Labbef7d76e02018-11-29 14:42:21 +00001112 crypto_stats_get(alg);
Corentin Labbecac58182018-09-19 10:10:54 +00001113 ret = crt->decrypt(req);
Corentin Labbef7d76e02018-11-29 14:42:21 +00001114 crypto_stats_ablkcipher_decrypt(nbytes, ret, alg);
Corentin Labbecac58182018-09-19 10:10:54 +00001115 return ret;
Herbert Xu32e3983f2007-03-24 14:35:34 +11001116}
1117
Stephan Muellerf13ec332014-11-12 05:28:22 +01001118/**
1119 * DOC: Asynchronous Cipher Request Handle
1120 *
1121 * The ablkcipher_request data structure contains all pointers to data
1122 * required for the asynchronous cipher operation. This includes the cipher
1123 * handle (which can be used by multiple ablkcipher_request instances), pointer
1124 * to plaintext and ciphertext, asynchronous callback function, etc. It acts
1125 * as a handle to the ablkcipher_request_* API calls in a similar way as
1126 * ablkcipher handle to the crypto_ablkcipher_* API calls.
1127 */
1128
1129/**
1130 * crypto_ablkcipher_reqsize() - obtain size of the request data structure
1131 * @tfm: cipher handle
1132 *
1133 * Return: number of bytes
1134 */
Herbert Xub16c3a22007-08-29 19:02:04 +08001135static inline unsigned int crypto_ablkcipher_reqsize(
1136 struct crypto_ablkcipher *tfm)
Herbert Xu32e3983f2007-03-24 14:35:34 +11001137{
1138 return crypto_ablkcipher_crt(tfm)->reqsize;
1139}
1140
Stephan Muellerf13ec332014-11-12 05:28:22 +01001141/**
1142 * ablkcipher_request_set_tfm() - update cipher handle reference in request
1143 * @req: request handle to be modified
1144 * @tfm: cipher handle that shall be added to the request handle
1145 *
1146 * Allow the caller to replace the existing ablkcipher handle in the request
1147 * data structure with a different one.
1148 */
Herbert Xue196d622007-04-14 16:09:14 +10001149static inline void ablkcipher_request_set_tfm(
1150 struct ablkcipher_request *req, struct crypto_ablkcipher *tfm)
1151{
Herbert Xuecfc4322007-12-05 21:08:36 +11001152 req->base.tfm = crypto_ablkcipher_tfm(crypto_ablkcipher_crt(tfm)->base);
Herbert Xue196d622007-04-14 16:09:14 +10001153}
1154
Herbert Xub5b7f082007-04-16 20:48:54 +10001155static inline struct ablkcipher_request *ablkcipher_request_cast(
1156 struct crypto_async_request *req)
1157{
1158 return container_of(req, struct ablkcipher_request, base);
1159}
1160
Stephan Muellerf13ec332014-11-12 05:28:22 +01001161/**
1162 * ablkcipher_request_alloc() - allocate request data structure
1163 * @tfm: cipher handle to be registered with the request
1164 * @gfp: memory allocation flag that is handed to kmalloc by the API call.
1165 *
1166 * Allocate the request data structure that must be used with the ablkcipher
1167 * encrypt and decrypt API calls. During the allocation, the provided ablkcipher
1168 * handle is registered in the request data structure.
1169 *
Eric Biggers6eae29e2016-04-02 10:54:56 -05001170 * Return: allocated request handle in case of success, or NULL if out of memory
Stephan Muellerf13ec332014-11-12 05:28:22 +01001171 */
Herbert Xu32e3983f2007-03-24 14:35:34 +11001172static inline struct ablkcipher_request *ablkcipher_request_alloc(
1173 struct crypto_ablkcipher *tfm, gfp_t gfp)
1174{
1175 struct ablkcipher_request *req;
1176
1177 req = kmalloc(sizeof(struct ablkcipher_request) +
1178 crypto_ablkcipher_reqsize(tfm), gfp);
1179
1180 if (likely(req))
Herbert Xue196d622007-04-14 16:09:14 +10001181 ablkcipher_request_set_tfm(req, tfm);
Herbert Xu32e3983f2007-03-24 14:35:34 +11001182
1183 return req;
1184}
1185
Stephan Muellerf13ec332014-11-12 05:28:22 +01001186/**
1187 * ablkcipher_request_free() - zeroize and free request data structure
1188 * @req: request data structure cipher handle to be freed
1189 */
Herbert Xu32e3983f2007-03-24 14:35:34 +11001190static inline void ablkcipher_request_free(struct ablkcipher_request *req)
1191{
Herbert Xuaef73cf2009-07-11 22:22:14 +08001192 kzfree(req);
Herbert Xu32e3983f2007-03-24 14:35:34 +11001193}
1194
Stephan Muellerf13ec332014-11-12 05:28:22 +01001195/**
1196 * ablkcipher_request_set_callback() - set asynchronous callback function
1197 * @req: request handle
1198 * @flags: specify zero or an ORing of the flags
Stephan Mueller0184cfe2016-10-21 04:57:27 +02001199 * CRYPTO_TFM_REQ_MAY_BACKLOG the request queue may back log and
Stephan Muellerf13ec332014-11-12 05:28:22 +01001200 * increase the wait queue beyond the initial maximum size;
1201 * CRYPTO_TFM_REQ_MAY_SLEEP the request processing may sleep
1202 * @compl: callback function pointer to be registered with the request handle
1203 * @data: The data pointer refers to memory that is not used by the kernel
1204 * crypto API, but provided to the callback function for it to use. Here,
1205 * the caller can provide a reference to memory the callback function can
1206 * operate on. As the callback function is invoked asynchronously to the
1207 * related functionality, it may need to access data structures of the
1208 * related functionality which can be referenced using this pointer. The
1209 * callback function can access the memory via the "data" field in the
1210 * crypto_async_request data structure provided to the callback function.
1211 *
1212 * This function allows setting the callback function that is triggered once the
1213 * cipher operation completes.
1214 *
1215 * The callback function is registered with the ablkcipher_request handle and
Stephan Mueller0184cfe2016-10-21 04:57:27 +02001216 * must comply with the following template::
Stephan Muellerf13ec332014-11-12 05:28:22 +01001217 *
1218 * void callback_function(struct crypto_async_request *req, int error)
1219 */
Herbert Xu32e3983f2007-03-24 14:35:34 +11001220static inline void ablkcipher_request_set_callback(
1221 struct ablkcipher_request *req,
Mark Rustad3e3dc252014-07-25 02:53:38 -07001222 u32 flags, crypto_completion_t compl, void *data)
Herbert Xu32e3983f2007-03-24 14:35:34 +11001223{
Mark Rustad3e3dc252014-07-25 02:53:38 -07001224 req->base.complete = compl;
Herbert Xu32e3983f2007-03-24 14:35:34 +11001225 req->base.data = data;
1226 req->base.flags = flags;
1227}
1228
Stephan Muellerf13ec332014-11-12 05:28:22 +01001229/**
1230 * ablkcipher_request_set_crypt() - set data buffers
1231 * @req: request handle
1232 * @src: source scatter / gather list
1233 * @dst: destination scatter / gather list
1234 * @nbytes: number of bytes to process from @src
1235 * @iv: IV for the cipher operation which must comply with the IV size defined
1236 * by crypto_ablkcipher_ivsize
1237 *
1238 * This function allows setting of the source data and destination data
1239 * scatter / gather lists.
1240 *
1241 * For encryption, the source is treated as the plaintext and the
1242 * destination is the ciphertext. For a decryption operation, the use is
Stephan Mueller379dcfb2015-01-19 00:13:39 +01001243 * reversed - the source is the ciphertext and the destination is the plaintext.
Stephan Muellerf13ec332014-11-12 05:28:22 +01001244 */
Herbert Xu32e3983f2007-03-24 14:35:34 +11001245static inline void ablkcipher_request_set_crypt(
1246 struct ablkcipher_request *req,
1247 struct scatterlist *src, struct scatterlist *dst,
1248 unsigned int nbytes, void *iv)
1249{
1250 req->src = src;
1251 req->dst = dst;
1252 req->nbytes = nbytes;
1253 req->info = iv;
1254}
1255
Stephan Muellerfced7b02014-11-12 05:29:00 +01001256/**
Stephan Mueller58284f02014-11-12 05:29:36 +01001257 * DOC: Synchronous Block Cipher API
1258 *
1259 * The synchronous block cipher API is used with the ciphers of type
1260 * CRYPTO_ALG_TYPE_BLKCIPHER (listed as type "blkcipher" in /proc/crypto)
1261 *
1262 * Synchronous calls, have a context in the tfm. But since a single tfm can be
1263 * used in multiple calls and in parallel, this info should not be changeable
1264 * (unless a lock is used). This applies, for example, to the symmetric key.
1265 * However, the IV is changeable, so there is an iv field in blkcipher_tfm
1266 * structure for synchronous blkcipher api. So, its the only state info that can
1267 * be kept for synchronous calls without using a big lock across a tfm.
1268 *
1269 * The block cipher API allows the use of a complete cipher, i.e. a cipher
1270 * consisting of a template (a block chaining mode) and a single block cipher
1271 * primitive (e.g. AES).
1272 *
1273 * The plaintext data buffer and the ciphertext data buffer are pointed to
1274 * by using scatter/gather lists. The cipher operation is performed
1275 * on all segments of the provided scatter/gather lists.
1276 *
1277 * The kernel crypto API supports a cipher operation "in-place" which means that
1278 * the caller may provide the same scatter/gather list for the plaintext and
1279 * cipher text. After the completion of the cipher operation, the plaintext
1280 * data is replaced with the ciphertext data in case of an encryption and vice
1281 * versa for a decryption. The caller must ensure that the scatter/gather lists
1282 * for the output data point to sufficiently large buffers, i.e. multiples of
1283 * the block size of the cipher.
1284 */
1285
Herbert Xu5cde0af2006-08-22 00:07:53 +10001286static inline struct crypto_blkcipher *__crypto_blkcipher_cast(
1287 struct crypto_tfm *tfm)
1288{
1289 return (struct crypto_blkcipher *)tfm;
1290}
1291
1292static inline struct crypto_blkcipher *crypto_blkcipher_cast(
1293 struct crypto_tfm *tfm)
1294{
1295 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_BLKCIPHER);
1296 return __crypto_blkcipher_cast(tfm);
1297}
1298
Stephan Mueller58284f02014-11-12 05:29:36 +01001299/**
1300 * crypto_alloc_blkcipher() - allocate synchronous block cipher handle
1301 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1302 * blkcipher cipher
1303 * @type: specifies the type of the cipher
1304 * @mask: specifies the mask for the cipher
1305 *
1306 * Allocate a cipher handle for a block cipher. The returned struct
1307 * crypto_blkcipher is the cipher handle that is required for any subsequent
1308 * API invocation for that block cipher.
1309 *
1310 * Return: allocated cipher handle in case of success; IS_ERR() is true in case
1311 * of an error, PTR_ERR() returns the error code.
1312 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001313static inline struct crypto_blkcipher *crypto_alloc_blkcipher(
1314 const char *alg_name, u32 type, u32 mask)
1315{
Herbert Xu332f88402007-11-15 22:36:07 +08001316 type &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xu5cde0af2006-08-22 00:07:53 +10001317 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
Herbert Xu332f88402007-11-15 22:36:07 +08001318 mask |= CRYPTO_ALG_TYPE_MASK;
Herbert Xu5cde0af2006-08-22 00:07:53 +10001319
1320 return __crypto_blkcipher_cast(crypto_alloc_base(alg_name, type, mask));
1321}
1322
1323static inline struct crypto_tfm *crypto_blkcipher_tfm(
1324 struct crypto_blkcipher *tfm)
1325{
1326 return &tfm->base;
1327}
1328
Stephan Mueller58284f02014-11-12 05:29:36 +01001329/**
1330 * crypto_free_blkcipher() - zeroize and free the block cipher handle
1331 * @tfm: cipher handle to be freed
1332 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001333static inline void crypto_free_blkcipher(struct crypto_blkcipher *tfm)
1334{
1335 crypto_free_tfm(crypto_blkcipher_tfm(tfm));
1336}
1337
Stephan Mueller58284f02014-11-12 05:29:36 +01001338/**
1339 * crypto_has_blkcipher() - Search for the availability of a block cipher
1340 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1341 * block cipher
1342 * @type: specifies the type of the cipher
1343 * @mask: specifies the mask for the cipher
1344 *
1345 * Return: true when the block cipher is known to the kernel crypto API; false
1346 * otherwise
1347 */
Herbert Xufce32d72006-08-26 17:35:45 +10001348static inline int crypto_has_blkcipher(const char *alg_name, u32 type, u32 mask)
1349{
Herbert Xu332f88402007-11-15 22:36:07 +08001350 type &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xufce32d72006-08-26 17:35:45 +10001351 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
Herbert Xu332f88402007-11-15 22:36:07 +08001352 mask |= CRYPTO_ALG_TYPE_MASK;
Herbert Xufce32d72006-08-26 17:35:45 +10001353
1354 return crypto_has_alg(alg_name, type, mask);
1355}
1356
Stephan Mueller58284f02014-11-12 05:29:36 +01001357/**
1358 * crypto_blkcipher_name() - return the name / cra_name from the cipher handle
1359 * @tfm: cipher handle
1360 *
1361 * Return: The character string holding the name of the cipher
1362 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001363static inline const char *crypto_blkcipher_name(struct crypto_blkcipher *tfm)
1364{
1365 return crypto_tfm_alg_name(crypto_blkcipher_tfm(tfm));
1366}
1367
1368static inline struct blkcipher_tfm *crypto_blkcipher_crt(
1369 struct crypto_blkcipher *tfm)
1370{
1371 return &crypto_blkcipher_tfm(tfm)->crt_blkcipher;
1372}
1373
1374static inline struct blkcipher_alg *crypto_blkcipher_alg(
1375 struct crypto_blkcipher *tfm)
1376{
1377 return &crypto_blkcipher_tfm(tfm)->__crt_alg->cra_blkcipher;
1378}
1379
Stephan Mueller58284f02014-11-12 05:29:36 +01001380/**
1381 * crypto_blkcipher_ivsize() - obtain IV size
1382 * @tfm: cipher handle
1383 *
1384 * The size of the IV for the block cipher referenced by the cipher handle is
1385 * returned. This IV size may be zero if the cipher does not need an IV.
1386 *
1387 * Return: IV size in bytes
1388 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001389static inline unsigned int crypto_blkcipher_ivsize(struct crypto_blkcipher *tfm)
1390{
1391 return crypto_blkcipher_alg(tfm)->ivsize;
1392}
1393
Stephan Mueller58284f02014-11-12 05:29:36 +01001394/**
1395 * crypto_blkcipher_blocksize() - obtain block size of cipher
1396 * @tfm: cipher handle
1397 *
1398 * The block size for the block cipher referenced with the cipher handle is
1399 * returned. The caller may use that information to allocate appropriate
1400 * memory for the data returned by the encryption or decryption operation.
1401 *
1402 * Return: block size of cipher
1403 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001404static inline unsigned int crypto_blkcipher_blocksize(
1405 struct crypto_blkcipher *tfm)
1406{
1407 return crypto_tfm_alg_blocksize(crypto_blkcipher_tfm(tfm));
1408}
1409
1410static inline unsigned int crypto_blkcipher_alignmask(
1411 struct crypto_blkcipher *tfm)
1412{
1413 return crypto_tfm_alg_alignmask(crypto_blkcipher_tfm(tfm));
1414}
1415
1416static inline u32 crypto_blkcipher_get_flags(struct crypto_blkcipher *tfm)
1417{
1418 return crypto_tfm_get_flags(crypto_blkcipher_tfm(tfm));
1419}
1420
1421static inline void crypto_blkcipher_set_flags(struct crypto_blkcipher *tfm,
1422 u32 flags)
1423{
1424 crypto_tfm_set_flags(crypto_blkcipher_tfm(tfm), flags);
1425}
1426
1427static inline void crypto_blkcipher_clear_flags(struct crypto_blkcipher *tfm,
1428 u32 flags)
1429{
1430 crypto_tfm_clear_flags(crypto_blkcipher_tfm(tfm), flags);
1431}
1432
Stephan Mueller58284f02014-11-12 05:29:36 +01001433/**
1434 * crypto_blkcipher_setkey() - set key for cipher
1435 * @tfm: cipher handle
1436 * @key: buffer holding the key
1437 * @keylen: length of the key in bytes
1438 *
1439 * The caller provided key is set for the block cipher referenced by the cipher
1440 * handle.
1441 *
1442 * Note, the key length determines the cipher type. Many block ciphers implement
1443 * different cipher modes depending on the key size, such as AES-128 vs AES-192
1444 * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
1445 * is performed.
1446 *
1447 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
1448 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001449static inline int crypto_blkcipher_setkey(struct crypto_blkcipher *tfm,
1450 const u8 *key, unsigned int keylen)
1451{
1452 return crypto_blkcipher_crt(tfm)->setkey(crypto_blkcipher_tfm(tfm),
1453 key, keylen);
1454}
1455
Stephan Mueller58284f02014-11-12 05:29:36 +01001456/**
1457 * crypto_blkcipher_encrypt() - encrypt plaintext
1458 * @desc: reference to the block cipher handle with meta data
1459 * @dst: scatter/gather list that is filled by the cipher operation with the
1460 * ciphertext
1461 * @src: scatter/gather list that holds the plaintext
1462 * @nbytes: number of bytes of the plaintext to encrypt.
1463 *
1464 * Encrypt plaintext data using the IV set by the caller with a preceding
1465 * call of crypto_blkcipher_set_iv.
1466 *
1467 * The blkcipher_desc data structure must be filled by the caller and can
1468 * reside on the stack. The caller must fill desc as follows: desc.tfm is filled
1469 * with the block cipher handle; desc.flags is filled with either
1470 * CRYPTO_TFM_REQ_MAY_SLEEP or 0.
1471 *
1472 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1473 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001474static inline int crypto_blkcipher_encrypt(struct blkcipher_desc *desc,
1475 struct scatterlist *dst,
1476 struct scatterlist *src,
1477 unsigned int nbytes)
1478{
1479 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
1480 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
1481}
1482
Stephan Mueller58284f02014-11-12 05:29:36 +01001483/**
1484 * crypto_blkcipher_encrypt_iv() - encrypt plaintext with dedicated IV
1485 * @desc: reference to the block cipher handle with meta data
1486 * @dst: scatter/gather list that is filled by the cipher operation with the
1487 * ciphertext
1488 * @src: scatter/gather list that holds the plaintext
1489 * @nbytes: number of bytes of the plaintext to encrypt.
1490 *
1491 * Encrypt plaintext data with the use of an IV that is solely used for this
1492 * cipher operation. Any previously set IV is not used.
1493 *
1494 * The blkcipher_desc data structure must be filled by the caller and can
1495 * reside on the stack. The caller must fill desc as follows: desc.tfm is filled
1496 * with the block cipher handle; desc.info is filled with the IV to be used for
1497 * the current operation; desc.flags is filled with either
1498 * CRYPTO_TFM_REQ_MAY_SLEEP or 0.
1499 *
1500 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1501 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001502static inline int crypto_blkcipher_encrypt_iv(struct blkcipher_desc *desc,
1503 struct scatterlist *dst,
1504 struct scatterlist *src,
1505 unsigned int nbytes)
1506{
1507 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
1508}
1509
Stephan Mueller58284f02014-11-12 05:29:36 +01001510/**
1511 * crypto_blkcipher_decrypt() - decrypt ciphertext
1512 * @desc: reference to the block cipher handle with meta data
1513 * @dst: scatter/gather list that is filled by the cipher operation with the
1514 * plaintext
1515 * @src: scatter/gather list that holds the ciphertext
1516 * @nbytes: number of bytes of the ciphertext to decrypt.
1517 *
1518 * Decrypt ciphertext data using the IV set by the caller with a preceding
1519 * call of crypto_blkcipher_set_iv.
1520 *
1521 * The blkcipher_desc data structure must be filled by the caller as documented
1522 * for the crypto_blkcipher_encrypt call above.
1523 *
1524 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1525 *
1526 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001527static inline int crypto_blkcipher_decrypt(struct blkcipher_desc *desc,
1528 struct scatterlist *dst,
1529 struct scatterlist *src,
1530 unsigned int nbytes)
1531{
1532 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
1533 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
1534}
1535
Stephan Mueller58284f02014-11-12 05:29:36 +01001536/**
1537 * crypto_blkcipher_decrypt_iv() - decrypt ciphertext with dedicated IV
1538 * @desc: reference to the block cipher handle with meta data
1539 * @dst: scatter/gather list that is filled by the cipher operation with the
1540 * plaintext
1541 * @src: scatter/gather list that holds the ciphertext
1542 * @nbytes: number of bytes of the ciphertext to decrypt.
1543 *
1544 * Decrypt ciphertext data with the use of an IV that is solely used for this
1545 * cipher operation. Any previously set IV is not used.
1546 *
1547 * The blkcipher_desc data structure must be filled by the caller as documented
1548 * for the crypto_blkcipher_encrypt_iv call above.
1549 *
1550 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1551 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001552static inline int crypto_blkcipher_decrypt_iv(struct blkcipher_desc *desc,
1553 struct scatterlist *dst,
1554 struct scatterlist *src,
1555 unsigned int nbytes)
1556{
1557 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
1558}
1559
Stephan Mueller58284f02014-11-12 05:29:36 +01001560/**
1561 * crypto_blkcipher_set_iv() - set IV for cipher
1562 * @tfm: cipher handle
1563 * @src: buffer holding the IV
1564 * @len: length of the IV in bytes
1565 *
1566 * The caller provided IV is set for the block cipher referenced by the cipher
1567 * handle.
1568 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001569static inline void crypto_blkcipher_set_iv(struct crypto_blkcipher *tfm,
1570 const u8 *src, unsigned int len)
1571{
1572 memcpy(crypto_blkcipher_crt(tfm)->iv, src, len);
1573}
1574
Stephan Mueller58284f02014-11-12 05:29:36 +01001575/**
1576 * crypto_blkcipher_get_iv() - obtain IV from cipher
1577 * @tfm: cipher handle
1578 * @dst: buffer filled with the IV
1579 * @len: length of the buffer dst
1580 *
1581 * The caller can obtain the IV set for the block cipher referenced by the
1582 * cipher handle and store it into the user-provided buffer. If the buffer
1583 * has an insufficient space, the IV is truncated to fit the buffer.
1584 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001585static inline void crypto_blkcipher_get_iv(struct crypto_blkcipher *tfm,
1586 u8 *dst, unsigned int len)
1587{
1588 memcpy(dst, crypto_blkcipher_crt(tfm)->iv, len);
1589}
1590
Stephan Mueller16e61032014-11-12 05:30:06 +01001591/**
1592 * DOC: Single Block Cipher API
1593 *
1594 * The single block cipher API is used with the ciphers of type
1595 * CRYPTO_ALG_TYPE_CIPHER (listed as type "cipher" in /proc/crypto).
1596 *
1597 * Using the single block cipher API calls, operations with the basic cipher
1598 * primitive can be implemented. These cipher primitives exclude any block
1599 * chaining operations including IV handling.
1600 *
1601 * The purpose of this single block cipher API is to support the implementation
1602 * of templates or other concepts that only need to perform the cipher operation
1603 * on one block at a time. Templates invoke the underlying cipher primitive
1604 * block-wise and process either the input or the output data of these cipher
1605 * operations.
1606 */
1607
Herbert Xuf28776a2006-08-13 20:58:18 +10001608static inline struct crypto_cipher *__crypto_cipher_cast(struct crypto_tfm *tfm)
1609{
1610 return (struct crypto_cipher *)tfm;
1611}
1612
1613static inline struct crypto_cipher *crypto_cipher_cast(struct crypto_tfm *tfm)
1614{
1615 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
1616 return __crypto_cipher_cast(tfm);
1617}
1618
Stephan Mueller16e61032014-11-12 05:30:06 +01001619/**
1620 * crypto_alloc_cipher() - allocate single block cipher handle
1621 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1622 * single block cipher
1623 * @type: specifies the type of the cipher
1624 * @mask: specifies the mask for the cipher
1625 *
1626 * Allocate a cipher handle for a single block cipher. The returned struct
1627 * crypto_cipher is the cipher handle that is required for any subsequent API
1628 * invocation for that single block cipher.
1629 *
1630 * Return: allocated cipher handle in case of success; IS_ERR() is true in case
1631 * of an error, PTR_ERR() returns the error code.
1632 */
Herbert Xuf28776a2006-08-13 20:58:18 +10001633static inline struct crypto_cipher *crypto_alloc_cipher(const char *alg_name,
1634 u32 type, u32 mask)
1635{
1636 type &= ~CRYPTO_ALG_TYPE_MASK;
1637 type |= CRYPTO_ALG_TYPE_CIPHER;
1638 mask |= CRYPTO_ALG_TYPE_MASK;
1639
1640 return __crypto_cipher_cast(crypto_alloc_base(alg_name, type, mask));
1641}
1642
1643static inline struct crypto_tfm *crypto_cipher_tfm(struct crypto_cipher *tfm)
1644{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001645 return &tfm->base;
Herbert Xuf28776a2006-08-13 20:58:18 +10001646}
1647
Stephan Mueller16e61032014-11-12 05:30:06 +01001648/**
1649 * crypto_free_cipher() - zeroize and free the single block cipher handle
1650 * @tfm: cipher handle to be freed
1651 */
Herbert Xuf28776a2006-08-13 20:58:18 +10001652static inline void crypto_free_cipher(struct crypto_cipher *tfm)
1653{
1654 crypto_free_tfm(crypto_cipher_tfm(tfm));
1655}
1656
Stephan Mueller16e61032014-11-12 05:30:06 +01001657/**
1658 * crypto_has_cipher() - Search for the availability of a single block cipher
1659 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1660 * single block cipher
1661 * @type: specifies the type of the cipher
1662 * @mask: specifies the mask for the cipher
1663 *
1664 * Return: true when the single block cipher is known to the kernel crypto API;
1665 * false otherwise
1666 */
Herbert Xufce32d72006-08-26 17:35:45 +10001667static inline int crypto_has_cipher(const char *alg_name, u32 type, u32 mask)
1668{
1669 type &= ~CRYPTO_ALG_TYPE_MASK;
1670 type |= CRYPTO_ALG_TYPE_CIPHER;
1671 mask |= CRYPTO_ALG_TYPE_MASK;
1672
1673 return crypto_has_alg(alg_name, type, mask);
1674}
1675
Herbert Xuf28776a2006-08-13 20:58:18 +10001676static inline struct cipher_tfm *crypto_cipher_crt(struct crypto_cipher *tfm)
1677{
1678 return &crypto_cipher_tfm(tfm)->crt_cipher;
1679}
1680
Stephan Mueller16e61032014-11-12 05:30:06 +01001681/**
1682 * crypto_cipher_blocksize() - obtain block size for cipher
1683 * @tfm: cipher handle
1684 *
1685 * The block size for the single block cipher referenced with the cipher handle
1686 * tfm is returned. The caller may use that information to allocate appropriate
1687 * memory for the data returned by the encryption or decryption operation
1688 *
1689 * Return: block size of cipher
1690 */
Herbert Xuf28776a2006-08-13 20:58:18 +10001691static inline unsigned int crypto_cipher_blocksize(struct crypto_cipher *tfm)
1692{
1693 return crypto_tfm_alg_blocksize(crypto_cipher_tfm(tfm));
1694}
1695
1696static inline unsigned int crypto_cipher_alignmask(struct crypto_cipher *tfm)
1697{
1698 return crypto_tfm_alg_alignmask(crypto_cipher_tfm(tfm));
1699}
1700
1701static inline u32 crypto_cipher_get_flags(struct crypto_cipher *tfm)
1702{
1703 return crypto_tfm_get_flags(crypto_cipher_tfm(tfm));
1704}
1705
1706static inline void crypto_cipher_set_flags(struct crypto_cipher *tfm,
1707 u32 flags)
1708{
1709 crypto_tfm_set_flags(crypto_cipher_tfm(tfm), flags);
1710}
1711
1712static inline void crypto_cipher_clear_flags(struct crypto_cipher *tfm,
1713 u32 flags)
1714{
1715 crypto_tfm_clear_flags(crypto_cipher_tfm(tfm), flags);
1716}
1717
Stephan Mueller16e61032014-11-12 05:30:06 +01001718/**
1719 * crypto_cipher_setkey() - set key for cipher
1720 * @tfm: cipher handle
1721 * @key: buffer holding the key
1722 * @keylen: length of the key in bytes
1723 *
1724 * The caller provided key is set for the single block cipher referenced by the
1725 * cipher handle.
1726 *
1727 * Note, the key length determines the cipher type. Many block ciphers implement
1728 * different cipher modes depending on the key size, such as AES-128 vs AES-192
1729 * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
1730 * is performed.
1731 *
1732 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
1733 */
Herbert Xu7226bc872006-08-21 21:40:49 +10001734static inline int crypto_cipher_setkey(struct crypto_cipher *tfm,
1735 const u8 *key, unsigned int keylen)
1736{
1737 return crypto_cipher_crt(tfm)->cit_setkey(crypto_cipher_tfm(tfm),
1738 key, keylen);
1739}
1740
Stephan Mueller16e61032014-11-12 05:30:06 +01001741/**
1742 * crypto_cipher_encrypt_one() - encrypt one block of plaintext
1743 * @tfm: cipher handle
1744 * @dst: points to the buffer that will be filled with the ciphertext
1745 * @src: buffer holding the plaintext to be encrypted
1746 *
1747 * Invoke the encryption operation of one block. The caller must ensure that
1748 * the plaintext and ciphertext buffers are at least one block in size.
1749 */
Herbert Xuf28776a2006-08-13 20:58:18 +10001750static inline void crypto_cipher_encrypt_one(struct crypto_cipher *tfm,
1751 u8 *dst, const u8 *src)
1752{
1753 crypto_cipher_crt(tfm)->cit_encrypt_one(crypto_cipher_tfm(tfm),
1754 dst, src);
1755}
1756
Stephan Mueller16e61032014-11-12 05:30:06 +01001757/**
1758 * crypto_cipher_decrypt_one() - decrypt one block of ciphertext
1759 * @tfm: cipher handle
1760 * @dst: points to the buffer that will be filled with the plaintext
1761 * @src: buffer holding the ciphertext to be decrypted
1762 *
1763 * Invoke the decryption operation of one block. The caller must ensure that
1764 * the plaintext and ciphertext buffers are at least one block in size.
1765 */
Herbert Xuf28776a2006-08-13 20:58:18 +10001766static inline void crypto_cipher_decrypt_one(struct crypto_cipher *tfm,
1767 u8 *dst, const u8 *src)
1768{
1769 crypto_cipher_crt(tfm)->cit_decrypt_one(crypto_cipher_tfm(tfm),
1770 dst, src);
1771}
1772
Herbert Xufce32d72006-08-26 17:35:45 +10001773static inline struct crypto_comp *__crypto_comp_cast(struct crypto_tfm *tfm)
1774{
1775 return (struct crypto_comp *)tfm;
1776}
1777
1778static inline struct crypto_comp *crypto_comp_cast(struct crypto_tfm *tfm)
1779{
1780 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_COMPRESS) &
1781 CRYPTO_ALG_TYPE_MASK);
1782 return __crypto_comp_cast(tfm);
1783}
1784
1785static inline struct crypto_comp *crypto_alloc_comp(const char *alg_name,
1786 u32 type, u32 mask)
1787{
1788 type &= ~CRYPTO_ALG_TYPE_MASK;
1789 type |= CRYPTO_ALG_TYPE_COMPRESS;
1790 mask |= CRYPTO_ALG_TYPE_MASK;
1791
1792 return __crypto_comp_cast(crypto_alloc_base(alg_name, type, mask));
1793}
1794
1795static inline struct crypto_tfm *crypto_comp_tfm(struct crypto_comp *tfm)
1796{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001797 return &tfm->base;
Herbert Xufce32d72006-08-26 17:35:45 +10001798}
1799
1800static inline void crypto_free_comp(struct crypto_comp *tfm)
1801{
1802 crypto_free_tfm(crypto_comp_tfm(tfm));
1803}
1804
1805static inline int crypto_has_comp(const char *alg_name, u32 type, u32 mask)
1806{
1807 type &= ~CRYPTO_ALG_TYPE_MASK;
1808 type |= CRYPTO_ALG_TYPE_COMPRESS;
1809 mask |= CRYPTO_ALG_TYPE_MASK;
1810
1811 return crypto_has_alg(alg_name, type, mask);
1812}
1813
Herbert Xue4d5b792006-08-26 18:12:40 +10001814static inline const char *crypto_comp_name(struct crypto_comp *tfm)
1815{
1816 return crypto_tfm_alg_name(crypto_comp_tfm(tfm));
1817}
1818
Herbert Xufce32d72006-08-26 17:35:45 +10001819static inline struct compress_tfm *crypto_comp_crt(struct crypto_comp *tfm)
1820{
1821 return &crypto_comp_tfm(tfm)->crt_compress;
1822}
1823
1824static inline int crypto_comp_compress(struct crypto_comp *tfm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 const u8 *src, unsigned int slen,
1826 u8 *dst, unsigned int *dlen)
1827{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001828 return crypto_comp_crt(tfm)->cot_compress(crypto_comp_tfm(tfm),
1829 src, slen, dst, dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830}
1831
Herbert Xufce32d72006-08-26 17:35:45 +10001832static inline int crypto_comp_decompress(struct crypto_comp *tfm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 const u8 *src, unsigned int slen,
1834 u8 *dst, unsigned int *dlen)
1835{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001836 return crypto_comp_crt(tfm)->cot_decompress(crypto_comp_tfm(tfm),
1837 src, slen, dst, dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838}
1839
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840#endif /* _LINUX_CRYPTO_H */
1841