blob: 23365a9d062e3fa0daaba31d17e192281a58fbad [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 Xu5cb14542005-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 Xu4e6c3df2016-07-12 13:17:31 +080044#define CRYPTO_ALG_TYPE_SKCIPHER 0x00000005
Salvatore Benedetto4e5f2c42016-06-22 17:49:13 +010045#define CRYPTO_ALG_TYPE_KPP 0x00000008
Giovanni Cabiddu2ebda742016-10-21 13:19:47 +010046#define CRYPTO_ALG_TYPE_ACOMPRESS 0x0000000a
Giovanni Cabiddu1ab53a72016-10-21 13:19:48 +010047#define CRYPTO_ALG_TYPE_SCOMPRESS 0x0000000b
Neil Horman17f0f4a2008-08-14 22:15:52 +100048#define CRYPTO_ALG_TYPE_RNG 0x0000000c
Tadeusz Struk3c339ab2015-06-16 10:30:55 -070049#define CRYPTO_ALG_TYPE_AKCIPHER 0x0000000d
Giovanni Cabiddu63044c42016-06-02 13:28:55 +010050#define CRYPTO_ALG_TYPE_HASH 0x0000000e
51#define CRYPTO_ALG_TYPE_SHASH 0x0000000e
52#define CRYPTO_ALG_TYPE_AHASH 0x0000000f
Herbert Xu055bcee2006-08-19 22:24:23 +100053
54#define CRYPTO_ALG_TYPE_HASH_MASK 0x0000000e
Giovanni Cabiddu63044c42016-06-02 13:28:55 +010055#define CRYPTO_ALG_TYPE_AHASH_MASK 0x0000000e
Giovanni Cabiddu1ab53a72016-10-21 13:19:48 +010056#define CRYPTO_ALG_TYPE_ACOMPRESS_MASK 0x0000000e
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Herbert Xu28259822006-08-06 21:23:26 +100058#define CRYPTO_ALG_LARVAL 0x00000010
Herbert Xu6bfd4802006-09-21 11:39:29 +100059#define CRYPTO_ALG_DEAD 0x00000020
60#define CRYPTO_ALG_DYING 0x00000040
Herbert Xuf3f632d2006-08-06 23:12:59 +100061#define CRYPTO_ALG_ASYNC 0x00000080
Herbert Xu28259822006-08-06 21:23:26 +100062
Linus Torvalds1da177e2005-04-16 15:20:36 -070063/*
Herbert Xu60104392006-08-26 18:34:10 +100064 * Set this bit if and only if the algorithm requires another algorithm of
65 * the same type to handle corner cases.
66 */
67#define CRYPTO_ALG_NEED_FALLBACK 0x00000100
68
69/*
Herbert Xu73d38642008-08-03 21:15:23 +080070 * Set if the algorithm has passed automated run-time testing. Note that
71 * if there is no run-time testing for a given algorithm it is considered
72 * to have passed.
73 */
74
75#define CRYPTO_ALG_TESTED 0x00000400
76
77/*
Baruch Siach864e0982016-11-30 15:16:08 +020078 * Set if the algorithm is an instance that is built from templates.
Steffen Klassert64a947b2011-09-27 07:21:26 +020079 */
80#define CRYPTO_ALG_INSTANCE 0x00000800
81
Nikos Mavrogiannopoulosd912bb72011-11-01 13:39:56 +010082/* Set this bit if the algorithm provided is hardware accelerated but
83 * not available to userspace via instruction set or so.
84 */
85#define CRYPTO_ALG_KERN_DRIVER_ONLY 0x00001000
86
Steffen Klassert64a947b2011-09-27 07:21:26 +020087/*
Stephan Mueller06ca7f62015-03-30 21:55:52 +020088 * Mark a cipher as a service implementation only usable by another
89 * cipher and never by a normal user of the kernel crypto API
90 */
91#define CRYPTO_ALG_INTERNAL 0x00002000
92
93/*
Eric Biggersa208fa82018-01-03 11:16:26 -080094 * Set if the algorithm has a ->setkey() method but can be used without
95 * calling it first, i.e. there is a default key.
96 */
97#define CRYPTO_ALG_OPTIONAL_KEY 0x00004000
98
99/*
Matthew Garrette2861fa2018-06-08 14:57:42 -0700100 * Don't trigger module loading
101 */
102#define CRYPTO_NOLOAD 0x00008000
103
104/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 * Transform masks and values (for crt_flags).
106 */
Eric Biggers9fa68f62018-01-03 11:16:27 -0800107#define CRYPTO_TFM_NEED_KEY 0x00000001
108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109#define CRYPTO_TFM_REQ_MASK 0x000fff00
110#define CRYPTO_TFM_RES_MASK 0xfff00000
111
Eric Biggers231baec2019-01-18 22:48:00 -0800112#define CRYPTO_TFM_REQ_FORBID_WEAK_KEYS 0x00000100
Herbert Xu64baf3c2005-09-01 17:43:05 -0700113#define CRYPTO_TFM_REQ_MAY_SLEEP 0x00000200
Herbert Xu32e39832007-03-24 14:35:34 +1100114#define CRYPTO_TFM_REQ_MAY_BACKLOG 0x00000400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115#define CRYPTO_TFM_RES_WEAK_KEY 0x00100000
116#define CRYPTO_TFM_RES_BAD_KEY_LEN 0x00200000
117#define CRYPTO_TFM_RES_BAD_KEY_SCHED 0x00400000
118#define CRYPTO_TFM_RES_BAD_BLOCK_LEN 0x00800000
119#define CRYPTO_TFM_RES_BAD_FLAGS 0x01000000
120
121/*
122 * Miscellaneous stuff.
123 */
Herbert Xuf437a3f2017-04-06 16:16:11 +0800124#define CRYPTO_MAX_ALG_NAME 128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Herbert Xu79911102006-08-21 21:03:52 +1000126/*
127 * The macro CRYPTO_MINALIGN_ATTR (along with the void * type in the actual
128 * declaration) is used to ensure that the crypto_tfm context structure is
129 * aligned correctly for the given architecture so that there are no alignment
130 * faults for C data types. In particular, this is required on platforms such
131 * as arm where pointers are 32-bit aligned but there are data types such as
132 * u64 which require 64-bit alignment.
133 */
Herbert Xu79911102006-08-21 21:03:52 +1000134#define CRYPTO_MINALIGN ARCH_KMALLOC_MINALIGN
Herbert Xu79911102006-08-21 21:03:52 +1000135
Herbert Xu79911102006-08-21 21:03:52 +1000136#define CRYPTO_MINALIGN_ATTR __attribute__ ((__aligned__(CRYPTO_MINALIGN)))
Herbert Xu79911102006-08-21 21:03:52 +1000137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138struct scatterlist;
Herbert Xu32e39832007-03-24 14:35:34 +1100139struct crypto_async_request;
Herbert Xu40725182005-07-06 13:51:52 -0700140struct crypto_tfm;
Herbert Xue853c3c2006-08-22 00:06:54 +1000141struct crypto_type;
Herbert Xu40725182005-07-06 13:51:52 -0700142
Herbert Xu32e39832007-03-24 14:35:34 +1100143typedef void (*crypto_completion_t)(struct crypto_async_request *req, int err);
144
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100145/**
146 * DOC: Block Cipher Context Data Structures
147 *
148 * These data structures define the operating context for each block cipher
149 * type.
150 */
151
Herbert Xu32e39832007-03-24 14:35:34 +1100152struct crypto_async_request {
153 struct list_head list;
154 crypto_completion_t complete;
155 void *data;
156 struct crypto_tfm *tfm;
157
158 u32 flags;
159};
160
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100161/**
162 * DOC: Block Cipher Algorithm Definitions
163 *
164 * These data structures define modular crypto algorithm implementations,
165 * managed via crypto_register_alg() and crypto_unregister_alg().
166 */
167
168/**
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100169 * struct cipher_alg - single-block symmetric ciphers definition
170 * @cia_min_keysize: Minimum key size supported by the transformation. This is
171 * the smallest key length supported by this transformation
172 * algorithm. This must be set to one of the pre-defined
173 * values as this is not hardware specific. Possible values
174 * for this field can be found via git grep "_MIN_KEY_SIZE"
175 * include/crypto/
176 * @cia_max_keysize: Maximum key size supported by the transformation. This is
177 * the largest key length supported by this transformation
178 * algorithm. This must be set to one of the pre-defined values
179 * as this is not hardware specific. Possible values for this
180 * field can be found via git grep "_MAX_KEY_SIZE"
181 * include/crypto/
182 * @cia_setkey: Set key for the transformation. This function is used to either
183 * program a supplied key into the hardware or store the key in the
184 * transformation context for programming it later. Note that this
185 * function does modify the transformation context. This function
186 * can be called multiple times during the existence of the
187 * transformation object, so one must make sure the key is properly
188 * reprogrammed into the hardware. This function is also
189 * responsible for checking the key length for validity.
190 * @cia_encrypt: Encrypt a single block. This function is used to encrypt a
191 * single block of data, which must be @cra_blocksize big. This
192 * always operates on a full @cra_blocksize and it is not possible
193 * to encrypt a block of smaller size. The supplied buffers must
194 * therefore also be at least of @cra_blocksize size. Both the
195 * input and output buffers are always aligned to @cra_alignmask.
196 * In case either of the input or output buffer supplied by user
197 * of the crypto API is not aligned to @cra_alignmask, the crypto
198 * API will re-align the buffers. The re-alignment means that a
199 * new buffer will be allocated, the data will be copied into the
200 * new buffer, then the processing will happen on the new buffer,
201 * then the data will be copied back into the original buffer and
202 * finally the new buffer will be freed. In case a software
203 * fallback was put in place in the @cra_init call, this function
204 * might need to use the fallback if the algorithm doesn't support
205 * all of the key sizes. In case the key was stored in
206 * transformation context, the key might need to be re-programmed
207 * into the hardware in this function. This function shall not
208 * modify the transformation context, as this function may be
209 * called in parallel with the same transformation object.
210 * @cia_decrypt: Decrypt a single block. This is a reverse counterpart to
211 * @cia_encrypt, and the conditions are exactly the same.
212 *
213 * All fields are mandatory and must be filled.
214 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215struct cipher_alg {
216 unsigned int cia_min_keysize;
217 unsigned int cia_max_keysize;
Herbert Xu6c2bb982006-05-16 22:09:29 +1000218 int (*cia_setkey)(struct crypto_tfm *tfm, const u8 *key,
Herbert Xu560c06a2006-08-13 14:16:39 +1000219 unsigned int keylen);
Herbert Xu6c2bb982006-05-16 22:09:29 +1000220 void (*cia_encrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
221 void (*cia_decrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222};
223
Hook, Gary5a353162019-06-25 23:43:43 +0000224/**
225 * struct compress_alg - compression/decompression algorithm
226 * @coa_compress: Compress a buffer of specified length, storing the resulting
227 * data in the specified buffer. Return the length of the
228 * compressed data in dlen.
229 * @coa_decompress: Decompress the source buffer, storing the uncompressed
230 * data in the specified buffer. The length of the data is
231 * returned in dlen.
232 *
233 * All fields are mandatory.
234 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235struct compress_alg {
Herbert Xu6c2bb982006-05-16 22:09:29 +1000236 int (*coa_compress)(struct crypto_tfm *tfm, const u8 *src,
237 unsigned int slen, u8 *dst, unsigned int *dlen);
238 int (*coa_decompress)(struct crypto_tfm *tfm, const u8 *src,
239 unsigned int slen, u8 *dst, unsigned int *dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240};
241
Corentin Labbe17c18f92018-11-29 14:42:24 +0000242#ifdef CONFIG_CRYPTO_STATS
243/*
244 * struct crypto_istat_aead - statistics for AEAD algorithm
245 * @encrypt_cnt: number of encrypt requests
246 * @encrypt_tlen: total data size handled by encrypt requests
247 * @decrypt_cnt: number of decrypt requests
248 * @decrypt_tlen: total data size handled by decrypt requests
Corentin Labbe44f13132018-11-29 14:42:25 +0000249 * @err_cnt: number of error for AEAD requests
Corentin Labbe17c18f92018-11-29 14:42:24 +0000250 */
251struct crypto_istat_aead {
252 atomic64_t encrypt_cnt;
253 atomic64_t encrypt_tlen;
254 atomic64_t decrypt_cnt;
255 atomic64_t decrypt_tlen;
Corentin Labbe44f13132018-11-29 14:42:25 +0000256 atomic64_t err_cnt;
Corentin Labbe17c18f92018-11-29 14:42:24 +0000257};
258
259/*
260 * struct crypto_istat_akcipher - statistics for akcipher algorithm
261 * @encrypt_cnt: number of encrypt requests
262 * @encrypt_tlen: total data size handled by encrypt requests
263 * @decrypt_cnt: number of decrypt requests
264 * @decrypt_tlen: total data size handled by decrypt requests
265 * @verify_cnt: number of verify operation
266 * @sign_cnt: number of sign requests
Corentin Labbe44f13132018-11-29 14:42:25 +0000267 * @err_cnt: number of error for akcipher requests
Corentin Labbe17c18f92018-11-29 14:42:24 +0000268 */
269struct crypto_istat_akcipher {
270 atomic64_t encrypt_cnt;
271 atomic64_t encrypt_tlen;
272 atomic64_t decrypt_cnt;
273 atomic64_t decrypt_tlen;
274 atomic64_t verify_cnt;
275 atomic64_t sign_cnt;
Corentin Labbe44f13132018-11-29 14:42:25 +0000276 atomic64_t err_cnt;
Corentin Labbe17c18f92018-11-29 14:42:24 +0000277};
278
279/*
280 * struct crypto_istat_cipher - statistics for cipher algorithm
281 * @encrypt_cnt: number of encrypt requests
282 * @encrypt_tlen: total data size handled by encrypt requests
283 * @decrypt_cnt: number of decrypt requests
284 * @decrypt_tlen: total data size handled by decrypt requests
Corentin Labbe44f13132018-11-29 14:42:25 +0000285 * @err_cnt: number of error for cipher requests
Corentin Labbe17c18f92018-11-29 14:42:24 +0000286 */
287struct crypto_istat_cipher {
288 atomic64_t encrypt_cnt;
289 atomic64_t encrypt_tlen;
290 atomic64_t decrypt_cnt;
291 atomic64_t decrypt_tlen;
Corentin Labbe44f13132018-11-29 14:42:25 +0000292 atomic64_t err_cnt;
Corentin Labbe17c18f92018-11-29 14:42:24 +0000293};
294
295/*
296 * struct crypto_istat_compress - statistics for compress algorithm
297 * @compress_cnt: number of compress requests
298 * @compress_tlen: total data size handled by compress requests
299 * @decompress_cnt: number of decompress requests
300 * @decompress_tlen: total data size handled by decompress requests
Corentin Labbe44f13132018-11-29 14:42:25 +0000301 * @err_cnt: number of error for compress requests
Corentin Labbe17c18f92018-11-29 14:42:24 +0000302 */
303struct crypto_istat_compress {
304 atomic64_t compress_cnt;
305 atomic64_t compress_tlen;
306 atomic64_t decompress_cnt;
307 atomic64_t decompress_tlen;
Corentin Labbe44f13132018-11-29 14:42:25 +0000308 atomic64_t err_cnt;
Corentin Labbe17c18f92018-11-29 14:42:24 +0000309};
310
311/*
312 * struct crypto_istat_hash - statistics for has algorithm
313 * @hash_cnt: number of hash requests
314 * @hash_tlen: total data size hashed
Corentin Labbe44f13132018-11-29 14:42:25 +0000315 * @err_cnt: number of error for hash requests
Corentin Labbe17c18f92018-11-29 14:42:24 +0000316 */
317struct crypto_istat_hash {
318 atomic64_t hash_cnt;
319 atomic64_t hash_tlen;
Corentin Labbe44f13132018-11-29 14:42:25 +0000320 atomic64_t err_cnt;
Corentin Labbe17c18f92018-11-29 14:42:24 +0000321};
322
323/*
324 * struct crypto_istat_kpp - statistics for KPP algorithm
325 * @setsecret_cnt: number of setsecrey operation
326 * @generate_public_key_cnt: number of generate_public_key operation
327 * @compute_shared_secret_cnt: number of compute_shared_secret operation
Corentin Labbe44f13132018-11-29 14:42:25 +0000328 * @err_cnt: number of error for KPP requests
Corentin Labbe17c18f92018-11-29 14:42:24 +0000329 */
330struct crypto_istat_kpp {
331 atomic64_t setsecret_cnt;
332 atomic64_t generate_public_key_cnt;
333 atomic64_t compute_shared_secret_cnt;
Corentin Labbe44f13132018-11-29 14:42:25 +0000334 atomic64_t err_cnt;
Corentin Labbe17c18f92018-11-29 14:42:24 +0000335};
336
337/*
338 * struct crypto_istat_rng: statistics for RNG algorithm
339 * @generate_cnt: number of RNG generate requests
340 * @generate_tlen: total data size of generated data by the RNG
341 * @seed_cnt: number of times the RNG was seeded
Corentin Labbe44f13132018-11-29 14:42:25 +0000342 * @err_cnt: number of error for RNG requests
Corentin Labbe17c18f92018-11-29 14:42:24 +0000343 */
344struct crypto_istat_rng {
345 atomic64_t generate_cnt;
346 atomic64_t generate_tlen;
347 atomic64_t seed_cnt;
Corentin Labbe44f13132018-11-29 14:42:25 +0000348 atomic64_t err_cnt;
Corentin Labbe17c18f92018-11-29 14:42:24 +0000349};
350#endif /* CONFIG_CRYPTO_STATS */
Neil Horman17f0f4a2008-08-14 22:15:52 +1000351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352#define cra_cipher cra_u.cipher
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353#define cra_compress cra_u.compress
354
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100355/**
356 * struct crypto_alg - definition of a cryptograpic cipher algorithm
357 * @cra_flags: Flags describing this transformation. See include/linux/crypto.h
358 * CRYPTO_ALG_* flags for the flags which go in here. Those are
359 * used for fine-tuning the description of the transformation
360 * algorithm.
361 * @cra_blocksize: Minimum block size of this transformation. The size in bytes
362 * of the smallest possible unit which can be transformed with
363 * this algorithm. The users must respect this value.
364 * In case of HASH transformation, it is possible for a smaller
365 * block than @cra_blocksize to be passed to the crypto API for
366 * transformation, in case of any other transformation type, an
367 * error will be returned upon any attempt to transform smaller
368 * than @cra_blocksize chunks.
369 * @cra_ctxsize: Size of the operational context of the transformation. This
370 * value informs the kernel crypto API about the memory size
371 * needed to be allocated for the transformation context.
372 * @cra_alignmask: Alignment mask for the input and output data buffer. The data
373 * buffer containing the input data for the algorithm must be
374 * aligned to this alignment mask. The data buffer for the
375 * output data must be aligned to this alignment mask. Note that
376 * the Crypto API will do the re-alignment in software, but
377 * only under special conditions and there is a performance hit.
378 * The re-alignment happens at these occasions for different
379 * @cra_u types: cipher -- For both input data and output data
380 * buffer; ahash -- For output hash destination buf; shash --
381 * For output hash destination buf.
382 * This is needed on hardware which is flawed by design and
383 * cannot pick data from arbitrary addresses.
384 * @cra_priority: Priority of this transformation implementation. In case
385 * multiple transformations with same @cra_name are available to
386 * the Crypto API, the kernel will use the one with highest
387 * @cra_priority.
388 * @cra_name: Generic name (usable by multiple implementations) of the
389 * transformation algorithm. This is the name of the transformation
390 * itself. This field is used by the kernel when looking up the
391 * providers of particular transformation.
392 * @cra_driver_name: Unique name of the transformation provider. This is the
393 * name of the provider of the transformation. This can be any
394 * arbitrary value, but in the usual case, this contains the
395 * name of the chip or provider and the name of the
396 * transformation algorithm.
397 * @cra_type: Type of the cryptographic transformation. This is a pointer to
398 * struct crypto_type, which implements callbacks common for all
Eric Biggersc65058b2019-10-25 12:41:12 -0700399 * transformation types. There are multiple options, such as
400 * &crypto_skcipher_type, &crypto_ahash_type, &crypto_rng_type.
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100401 * This field might be empty. In that case, there are no common
402 * callbacks. This is the case for: cipher, compress, shash.
403 * @cra_u: Callbacks implementing the transformation. This is a union of
404 * multiple structures. Depending on the type of transformation selected
405 * by @cra_type and @cra_flags above, the associated structure must be
406 * filled with callbacks. This field might be empty. This is the case
407 * for ahash, shash.
408 * @cra_init: Initialize the cryptographic transformation object. This function
409 * is used to initialize the cryptographic transformation object.
410 * This function is called only once at the instantiation time, right
411 * after the transformation context was allocated. In case the
412 * cryptographic hardware has some special requirements which need to
413 * be handled by software, this function shall check for the precise
414 * requirement of the transformation and put any software fallbacks
415 * in place.
416 * @cra_exit: Deinitialize the cryptographic transformation object. This is a
417 * counterpart to @cra_init, used to remove various changes set in
418 * @cra_init.
Gary R Hook0063ec42018-03-14 17:15:52 -0500419 * @cra_u.cipher: Union member which contains a single-block symmetric cipher
420 * definition. See @struct @cipher_alg.
421 * @cra_u.compress: Union member which contains a (de)compression algorithm.
422 * See @struct @compress_alg.
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100423 * @cra_module: Owner of this transformation implementation. Set to THIS_MODULE
424 * @cra_list: internally used
425 * @cra_users: internally used
426 * @cra_refcnt: internally used
427 * @cra_destroy: internally used
428 *
Corentin Labbe17c18f92018-11-29 14:42:24 +0000429 * @stats: union of all possible crypto_istat_xxx structures
Corentin Labbebfad6cb2018-12-13 08:36:38 +0000430 * @stats.aead: statistics for AEAD algorithm
431 * @stats.akcipher: statistics for akcipher algorithm
432 * @stats.cipher: statistics for cipher algorithm
433 * @stats.compress: statistics for compress algorithm
434 * @stats.hash: statistics for hash algorithm
435 * @stats.rng: statistics for rng algorithm
436 * @stats.kpp: statistics for KPP algorithm
Corentin Labbecac58182018-09-19 10:10:54 +0000437 *
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100438 * The struct crypto_alg describes a generic Crypto API algorithm and is common
439 * for all of the transformations. Any variable not documented here shall not
440 * be used by a cipher implementation as it is internal to the Crypto API.
441 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442struct crypto_alg {
443 struct list_head cra_list;
Herbert Xu6bfd4802006-09-21 11:39:29 +1000444 struct list_head cra_users;
445
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 u32 cra_flags;
447 unsigned int cra_blocksize;
448 unsigned int cra_ctxsize;
Herbert Xu95477372005-07-06 13:52:09 -0700449 unsigned int cra_alignmask;
Herbert Xu5cb14542005-11-05 16:58:14 +1100450
451 int cra_priority;
Eric Biggersce8614a2017-12-29 10:00:46 -0600452 refcount_t cra_refcnt;
Herbert Xu5cb14542005-11-05 16:58:14 +1100453
Herbert Xud913ea02006-05-21 08:45:26 +1000454 char cra_name[CRYPTO_MAX_ALG_NAME];
455 char cra_driver_name[CRYPTO_MAX_ALG_NAME];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
Herbert Xue853c3c2006-08-22 00:06:54 +1000457 const struct crypto_type *cra_type;
458
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 union {
460 struct cipher_alg cipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 struct compress_alg compress;
462 } cra_u;
Herbert Xuc7fc0592006-05-24 13:02:26 +1000463
464 int (*cra_init)(struct crypto_tfm *tfm);
465 void (*cra_exit)(struct crypto_tfm *tfm);
Herbert Xu6521f302006-08-06 20:28:44 +1000466 void (*cra_destroy)(struct crypto_alg *alg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
468 struct module *cra_module;
Corentin Labbecac58182018-09-19 10:10:54 +0000469
Corentin Labbe2ced2602018-11-29 14:42:16 +0000470#ifdef CONFIG_CRYPTO_STATS
Corentin Labbecac58182018-09-19 10:10:54 +0000471 union {
Corentin Labbe17c18f92018-11-29 14:42:24 +0000472 struct crypto_istat_aead aead;
473 struct crypto_istat_akcipher akcipher;
474 struct crypto_istat_cipher cipher;
475 struct crypto_istat_compress compress;
476 struct crypto_istat_hash hash;
477 struct crypto_istat_rng rng;
478 struct crypto_istat_kpp kpp;
479 } stats;
Corentin Labbe2ced2602018-11-29 14:42:16 +0000480#endif /* CONFIG_CRYPTO_STATS */
Corentin Labbecac58182018-09-19 10:10:54 +0000481
Herbert Xuedf18b92015-06-18 14:00:48 +0800482} CRYPTO_MINALIGN_ATTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
Corentin Labbef7d76e02018-11-29 14:42:21 +0000484#ifdef CONFIG_CRYPTO_STATS
Corentin Labbe1f6669b2018-11-29 14:42:26 +0000485void crypto_stats_init(struct crypto_alg *alg);
Corentin Labbef7d76e02018-11-29 14:42:21 +0000486void crypto_stats_get(struct crypto_alg *alg);
Corentin Labbef7d76e02018-11-29 14:42:21 +0000487void crypto_stats_aead_encrypt(unsigned int cryptlen, struct crypto_alg *alg, int ret);
488void crypto_stats_aead_decrypt(unsigned int cryptlen, struct crypto_alg *alg, int ret);
489void crypto_stats_ahash_update(unsigned int nbytes, int ret, struct crypto_alg *alg);
490void crypto_stats_ahash_final(unsigned int nbytes, int ret, struct crypto_alg *alg);
491void crypto_stats_akcipher_encrypt(unsigned int src_len, int ret, struct crypto_alg *alg);
492void crypto_stats_akcipher_decrypt(unsigned int src_len, int ret, struct crypto_alg *alg);
493void crypto_stats_akcipher_sign(int ret, struct crypto_alg *alg);
494void crypto_stats_akcipher_verify(int ret, struct crypto_alg *alg);
495void crypto_stats_compress(unsigned int slen, int ret, struct crypto_alg *alg);
496void crypto_stats_decompress(unsigned int slen, int ret, struct crypto_alg *alg);
497void crypto_stats_kpp_set_secret(struct crypto_alg *alg, int ret);
498void crypto_stats_kpp_generate_public_key(struct crypto_alg *alg, int ret);
499void crypto_stats_kpp_compute_shared_secret(struct crypto_alg *alg, int ret);
500void crypto_stats_rng_seed(struct crypto_alg *alg, int ret);
501void crypto_stats_rng_generate(struct crypto_alg *alg, unsigned int dlen, int ret);
502void crypto_stats_skcipher_encrypt(unsigned int cryptlen, int ret, struct crypto_alg *alg);
503void crypto_stats_skcipher_decrypt(unsigned int cryptlen, int ret, struct crypto_alg *alg);
504#else
Corentin Labbe1f6669b2018-11-29 14:42:26 +0000505static inline void crypto_stats_init(struct crypto_alg *alg)
506{}
Corentin Labbef7d76e02018-11-29 14:42:21 +0000507static inline void crypto_stats_get(struct crypto_alg *alg)
508{}
Corentin Labbef7d76e02018-11-29 14:42:21 +0000509static inline void crypto_stats_aead_encrypt(unsigned int cryptlen, struct crypto_alg *alg, int ret)
510{}
511static inline void crypto_stats_aead_decrypt(unsigned int cryptlen, struct crypto_alg *alg, int ret)
512{}
513static inline void crypto_stats_ahash_update(unsigned int nbytes, int ret, struct crypto_alg *alg)
514{}
515static inline void crypto_stats_ahash_final(unsigned int nbytes, int ret, struct crypto_alg *alg)
516{}
517static inline void crypto_stats_akcipher_encrypt(unsigned int src_len, int ret, struct crypto_alg *alg)
518{}
519static inline void crypto_stats_akcipher_decrypt(unsigned int src_len, int ret, struct crypto_alg *alg)
520{}
521static inline void crypto_stats_akcipher_sign(int ret, struct crypto_alg *alg)
522{}
523static inline void crypto_stats_akcipher_verify(int ret, struct crypto_alg *alg)
524{}
525static inline void crypto_stats_compress(unsigned int slen, int ret, struct crypto_alg *alg)
526{}
527static inline void crypto_stats_decompress(unsigned int slen, int ret, struct crypto_alg *alg)
528{}
529static inline void crypto_stats_kpp_set_secret(struct crypto_alg *alg, int ret)
530{}
531static inline void crypto_stats_kpp_generate_public_key(struct crypto_alg *alg, int ret)
532{}
533static inline void crypto_stats_kpp_compute_shared_secret(struct crypto_alg *alg, int ret)
534{}
535static inline void crypto_stats_rng_seed(struct crypto_alg *alg, int ret)
536{}
537static inline void crypto_stats_rng_generate(struct crypto_alg *alg, unsigned int dlen, int ret)
538{}
539static inline void crypto_stats_skcipher_encrypt(unsigned int cryptlen, int ret, struct crypto_alg *alg)
540{}
541static inline void crypto_stats_skcipher_decrypt(unsigned int cryptlen, int ret, struct crypto_alg *alg)
542{}
543#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544/*
Gilad Ben-Yossefada69a12017-10-18 08:00:38 +0100545 * A helper struct for waiting for completion of async crypto ops
546 */
547struct crypto_wait {
548 struct completion completion;
549 int err;
550};
551
552/*
553 * Macro for declaring a crypto op async wait object on stack
554 */
555#define DECLARE_CRYPTO_WAIT(_wait) \
556 struct crypto_wait _wait = { \
557 COMPLETION_INITIALIZER_ONSTACK((_wait).completion), 0 }
558
559/*
560 * Async ops completion helper functioons
561 */
562void crypto_req_done(struct crypto_async_request *req, int err);
563
564static inline int crypto_wait_req(int err, struct crypto_wait *wait)
565{
566 switch (err) {
567 case -EINPROGRESS:
568 case -EBUSY:
569 wait_for_completion(&wait->completion);
570 reinit_completion(&wait->completion);
571 err = wait->err;
572 break;
573 };
574
575 return err;
576}
577
578static inline void crypto_init_wait(struct crypto_wait *wait)
579{
580 init_completion(&wait->completion);
581}
582
583/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 * Algorithm registration interface.
585 */
586int crypto_register_alg(struct crypto_alg *alg);
587int crypto_unregister_alg(struct crypto_alg *alg);
Mark Brown4b004342012-01-17 23:34:26 +0000588int crypto_register_algs(struct crypto_alg *algs, int count);
589int crypto_unregister_algs(struct crypto_alg *algs, int count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
591/*
592 * Algorithm query interface.
593 */
Herbert Xufce32d72006-08-26 17:35:45 +1000594int crypto_has_alg(const char *name, u32 type, u32 mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
596/*
597 * Transforms: user-instantiated objects which encapsulate algorithms
Herbert Xu6d7d6842006-07-30 11:53:01 +1000598 * and core processing logic. Managed via crypto_alloc_*() and
599 * crypto_free_*(), as well as the various helpers below.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
602struct cipher_tfm {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 int (*cit_setkey)(struct crypto_tfm *tfm,
604 const u8 *key, unsigned int keylen);
Herbert Xuf28776a2006-08-13 20:58:18 +1000605 void (*cit_encrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
606 void (*cit_decrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607};
608
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609struct compress_tfm {
610 int (*cot_compress)(struct crypto_tfm *tfm,
611 const u8 *src, unsigned int slen,
612 u8 *dst, unsigned int *dlen);
613 int (*cot_decompress)(struct crypto_tfm *tfm,
614 const u8 *src, unsigned int slen,
615 u8 *dst, unsigned int *dlen);
616};
617
618#define crt_cipher crt_u.cipher
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619#define crt_compress crt_u.compress
620
621struct crypto_tfm {
622
623 u32 crt_flags;
624
625 union {
626 struct cipher_tfm cipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 struct compress_tfm compress;
628 } crt_u;
Herbert Xu4a779482008-09-13 18:19:03 -0700629
630 void (*exit)(struct crypto_tfm *tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
632 struct crypto_alg *__crt_alg;
Herbert Xuf10b7892006-01-25 22:34:01 +1100633
Herbert Xu79911102006-08-21 21:03:52 +1000634 void *__crt_ctx[] CRYPTO_MINALIGN_ATTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635};
636
Herbert Xu78a1fe42006-12-24 10:02:00 +1100637struct crypto_cipher {
638 struct crypto_tfm base;
639};
640
641struct crypto_comp {
642 struct crypto_tfm base;
643};
644
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000645enum {
646 CRYPTOA_UNSPEC,
647 CRYPTOA_ALG,
Herbert Xuebc610e2007-01-01 18:37:02 +1100648 CRYPTOA_TYPE,
Herbert Xu39e1ee012007-08-29 19:27:26 +0800649 CRYPTOA_U32,
Herbert Xuebc610e2007-01-01 18:37:02 +1100650 __CRYPTOA_MAX,
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000651};
652
Herbert Xuebc610e2007-01-01 18:37:02 +1100653#define CRYPTOA_MAX (__CRYPTOA_MAX - 1)
654
Herbert Xu39e1ee012007-08-29 19:27:26 +0800655/* Maximum number of (rtattr) parameters for each template. */
656#define CRYPTO_MAX_ATTRS 32
657
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000658struct crypto_attr_alg {
659 char name[CRYPTO_MAX_ALG_NAME];
660};
661
Herbert Xuebc610e2007-01-01 18:37:02 +1100662struct crypto_attr_type {
663 u32 type;
664 u32 mask;
665};
666
Herbert Xu39e1ee012007-08-29 19:27:26 +0800667struct crypto_attr_u32 {
668 u32 num;
669};
670
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671/*
672 * Transform user interface.
673 */
674
Herbert Xu6d7d6842006-07-30 11:53:01 +1000675struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask);
Herbert Xu7b2cd922009-02-05 16:48:24 +1100676void crypto_destroy_tfm(void *mem, struct crypto_tfm *tfm);
677
678static inline void crypto_free_tfm(struct crypto_tfm *tfm)
679{
680 return crypto_destroy_tfm(tfm, tfm);
681}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
Herbert Xuda7f0332008-07-31 17:08:25 +0800683int alg_test(const char *driver, const char *alg, u32 type, u32 mask);
684
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685/*
686 * Transform helpers which query the underlying algorithm.
687 */
688static inline const char *crypto_tfm_alg_name(struct crypto_tfm *tfm)
689{
690 return tfm->__crt_alg->cra_name;
691}
692
Michal Ludvigb14cdd62006-07-09 09:02:24 +1000693static inline const char *crypto_tfm_alg_driver_name(struct crypto_tfm *tfm)
694{
695 return tfm->__crt_alg->cra_driver_name;
696}
697
698static inline int crypto_tfm_alg_priority(struct crypto_tfm *tfm)
699{
700 return tfm->__crt_alg->cra_priority;
701}
702
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm)
704{
705 return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK;
706}
707
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm)
709{
710 return tfm->__crt_alg->cra_blocksize;
711}
712
Herbert Xufbdae9f2005-07-06 13:53:29 -0700713static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm *tfm)
714{
715 return tfm->__crt_alg->cra_alignmask;
716}
717
Herbert Xuf28776a2006-08-13 20:58:18 +1000718static inline u32 crypto_tfm_get_flags(struct crypto_tfm *tfm)
719{
720 return tfm->crt_flags;
721}
722
723static inline void crypto_tfm_set_flags(struct crypto_tfm *tfm, u32 flags)
724{
725 tfm->crt_flags |= flags;
726}
727
728static inline void crypto_tfm_clear_flags(struct crypto_tfm *tfm, u32 flags)
729{
730 tfm->crt_flags &= ~flags;
731}
732
Herbert Xu40725182005-07-06 13:51:52 -0700733static inline void *crypto_tfm_ctx(struct crypto_tfm *tfm)
734{
Herbert Xuf10b7892006-01-25 22:34:01 +1100735 return tfm->__crt_ctx;
736}
737
738static inline unsigned int crypto_tfm_ctx_alignment(void)
739{
740 struct crypto_tfm *tfm;
741 return __alignof__(tfm->__crt_ctx);
Herbert Xu40725182005-07-06 13:51:52 -0700742}
743
Stephan Muellerfced7b02014-11-12 05:29:00 +0100744/**
Stephan Mueller16e61032014-11-12 05:30:06 +0100745 * DOC: Single Block Cipher API
746 *
747 * The single block cipher API is used with the ciphers of type
748 * CRYPTO_ALG_TYPE_CIPHER (listed as type "cipher" in /proc/crypto).
749 *
750 * Using the single block cipher API calls, operations with the basic cipher
751 * primitive can be implemented. These cipher primitives exclude any block
752 * chaining operations including IV handling.
753 *
754 * The purpose of this single block cipher API is to support the implementation
755 * of templates or other concepts that only need to perform the cipher operation
756 * on one block at a time. Templates invoke the underlying cipher primitive
757 * block-wise and process either the input or the output data of these cipher
758 * operations.
759 */
760
Herbert Xuf28776a2006-08-13 20:58:18 +1000761static inline struct crypto_cipher *__crypto_cipher_cast(struct crypto_tfm *tfm)
762{
763 return (struct crypto_cipher *)tfm;
764}
765
766static inline struct crypto_cipher *crypto_cipher_cast(struct crypto_tfm *tfm)
767{
768 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
769 return __crypto_cipher_cast(tfm);
770}
771
Stephan Mueller16e61032014-11-12 05:30:06 +0100772/**
773 * crypto_alloc_cipher() - allocate single block cipher handle
774 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
775 * single block cipher
776 * @type: specifies the type of the cipher
777 * @mask: specifies the mask for the cipher
778 *
779 * Allocate a cipher handle for a single block cipher. The returned struct
780 * crypto_cipher is the cipher handle that is required for any subsequent API
781 * invocation for that single block cipher.
782 *
783 * Return: allocated cipher handle in case of success; IS_ERR() is true in case
784 * of an error, PTR_ERR() returns the error code.
785 */
Herbert Xuf28776a2006-08-13 20:58:18 +1000786static inline struct crypto_cipher *crypto_alloc_cipher(const char *alg_name,
787 u32 type, u32 mask)
788{
789 type &= ~CRYPTO_ALG_TYPE_MASK;
790 type |= CRYPTO_ALG_TYPE_CIPHER;
791 mask |= CRYPTO_ALG_TYPE_MASK;
792
793 return __crypto_cipher_cast(crypto_alloc_base(alg_name, type, mask));
794}
795
796static inline struct crypto_tfm *crypto_cipher_tfm(struct crypto_cipher *tfm)
797{
Herbert Xu78a1fe42006-12-24 10:02:00 +1100798 return &tfm->base;
Herbert Xuf28776a2006-08-13 20:58:18 +1000799}
800
Stephan Mueller16e61032014-11-12 05:30:06 +0100801/**
802 * crypto_free_cipher() - zeroize and free the single block cipher handle
803 * @tfm: cipher handle to be freed
804 */
Herbert Xuf28776a2006-08-13 20:58:18 +1000805static inline void crypto_free_cipher(struct crypto_cipher *tfm)
806{
807 crypto_free_tfm(crypto_cipher_tfm(tfm));
808}
809
Stephan Mueller16e61032014-11-12 05:30:06 +0100810/**
811 * crypto_has_cipher() - Search for the availability of a single block cipher
812 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
813 * single block cipher
814 * @type: specifies the type of the cipher
815 * @mask: specifies the mask for the cipher
816 *
817 * Return: true when the single block cipher is known to the kernel crypto API;
818 * false otherwise
819 */
Herbert Xufce32d72006-08-26 17:35:45 +1000820static inline int crypto_has_cipher(const char *alg_name, u32 type, u32 mask)
821{
822 type &= ~CRYPTO_ALG_TYPE_MASK;
823 type |= CRYPTO_ALG_TYPE_CIPHER;
824 mask |= CRYPTO_ALG_TYPE_MASK;
825
826 return crypto_has_alg(alg_name, type, mask);
827}
828
Herbert Xuf28776a2006-08-13 20:58:18 +1000829static inline struct cipher_tfm *crypto_cipher_crt(struct crypto_cipher *tfm)
830{
831 return &crypto_cipher_tfm(tfm)->crt_cipher;
832}
833
Stephan Mueller16e61032014-11-12 05:30:06 +0100834/**
835 * crypto_cipher_blocksize() - obtain block size for cipher
836 * @tfm: cipher handle
837 *
838 * The block size for the single block cipher referenced with the cipher handle
839 * tfm is returned. The caller may use that information to allocate appropriate
840 * memory for the data returned by the encryption or decryption operation
841 *
842 * Return: block size of cipher
843 */
Herbert Xuf28776a2006-08-13 20:58:18 +1000844static inline unsigned int crypto_cipher_blocksize(struct crypto_cipher *tfm)
845{
846 return crypto_tfm_alg_blocksize(crypto_cipher_tfm(tfm));
847}
848
849static inline unsigned int crypto_cipher_alignmask(struct crypto_cipher *tfm)
850{
851 return crypto_tfm_alg_alignmask(crypto_cipher_tfm(tfm));
852}
853
854static inline u32 crypto_cipher_get_flags(struct crypto_cipher *tfm)
855{
856 return crypto_tfm_get_flags(crypto_cipher_tfm(tfm));
857}
858
859static inline void crypto_cipher_set_flags(struct crypto_cipher *tfm,
860 u32 flags)
861{
862 crypto_tfm_set_flags(crypto_cipher_tfm(tfm), flags);
863}
864
865static inline void crypto_cipher_clear_flags(struct crypto_cipher *tfm,
866 u32 flags)
867{
868 crypto_tfm_clear_flags(crypto_cipher_tfm(tfm), flags);
869}
870
Stephan Mueller16e61032014-11-12 05:30:06 +0100871/**
872 * crypto_cipher_setkey() - set key for cipher
873 * @tfm: cipher handle
874 * @key: buffer holding the key
875 * @keylen: length of the key in bytes
876 *
877 * The caller provided key is set for the single block cipher referenced by the
878 * cipher handle.
879 *
880 * Note, the key length determines the cipher type. Many block ciphers implement
881 * different cipher modes depending on the key size, such as AES-128 vs AES-192
882 * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
883 * is performed.
884 *
885 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
886 */
Herbert Xu7226bc872006-08-21 21:40:49 +1000887static inline int crypto_cipher_setkey(struct crypto_cipher *tfm,
888 const u8 *key, unsigned int keylen)
889{
890 return crypto_cipher_crt(tfm)->cit_setkey(crypto_cipher_tfm(tfm),
891 key, keylen);
892}
893
Stephan Mueller16e61032014-11-12 05:30:06 +0100894/**
895 * crypto_cipher_encrypt_one() - encrypt one block of plaintext
896 * @tfm: cipher handle
897 * @dst: points to the buffer that will be filled with the ciphertext
898 * @src: buffer holding the plaintext to be encrypted
899 *
900 * Invoke the encryption operation of one block. The caller must ensure that
901 * the plaintext and ciphertext buffers are at least one block in size.
902 */
Herbert Xuf28776a2006-08-13 20:58:18 +1000903static inline void crypto_cipher_encrypt_one(struct crypto_cipher *tfm,
904 u8 *dst, const u8 *src)
905{
906 crypto_cipher_crt(tfm)->cit_encrypt_one(crypto_cipher_tfm(tfm),
907 dst, src);
908}
909
Stephan Mueller16e61032014-11-12 05:30:06 +0100910/**
911 * crypto_cipher_decrypt_one() - decrypt one block of ciphertext
912 * @tfm: cipher handle
913 * @dst: points to the buffer that will be filled with the plaintext
914 * @src: buffer holding the ciphertext to be decrypted
915 *
916 * Invoke the decryption operation of one block. The caller must ensure that
917 * the plaintext and ciphertext buffers are at least one block in size.
918 */
Herbert Xuf28776a2006-08-13 20:58:18 +1000919static inline void crypto_cipher_decrypt_one(struct crypto_cipher *tfm,
920 u8 *dst, const u8 *src)
921{
922 crypto_cipher_crt(tfm)->cit_decrypt_one(crypto_cipher_tfm(tfm),
923 dst, src);
924}
925
Herbert Xufce32d72006-08-26 17:35:45 +1000926static inline struct crypto_comp *__crypto_comp_cast(struct crypto_tfm *tfm)
927{
928 return (struct crypto_comp *)tfm;
929}
930
931static inline struct crypto_comp *crypto_comp_cast(struct crypto_tfm *tfm)
932{
933 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_COMPRESS) &
934 CRYPTO_ALG_TYPE_MASK);
935 return __crypto_comp_cast(tfm);
936}
937
938static inline struct crypto_comp *crypto_alloc_comp(const char *alg_name,
939 u32 type, u32 mask)
940{
941 type &= ~CRYPTO_ALG_TYPE_MASK;
942 type |= CRYPTO_ALG_TYPE_COMPRESS;
943 mask |= CRYPTO_ALG_TYPE_MASK;
944
945 return __crypto_comp_cast(crypto_alloc_base(alg_name, type, mask));
946}
947
948static inline struct crypto_tfm *crypto_comp_tfm(struct crypto_comp *tfm)
949{
Herbert Xu78a1fe42006-12-24 10:02:00 +1100950 return &tfm->base;
Herbert Xufce32d72006-08-26 17:35:45 +1000951}
952
953static inline void crypto_free_comp(struct crypto_comp *tfm)
954{
955 crypto_free_tfm(crypto_comp_tfm(tfm));
956}
957
958static inline int crypto_has_comp(const char *alg_name, u32 type, u32 mask)
959{
960 type &= ~CRYPTO_ALG_TYPE_MASK;
961 type |= CRYPTO_ALG_TYPE_COMPRESS;
962 mask |= CRYPTO_ALG_TYPE_MASK;
963
964 return crypto_has_alg(alg_name, type, mask);
965}
966
Herbert Xue4d5b792006-08-26 18:12:40 +1000967static inline const char *crypto_comp_name(struct crypto_comp *tfm)
968{
969 return crypto_tfm_alg_name(crypto_comp_tfm(tfm));
970}
971
Herbert Xufce32d72006-08-26 17:35:45 +1000972static inline struct compress_tfm *crypto_comp_crt(struct crypto_comp *tfm)
973{
974 return &crypto_comp_tfm(tfm)->crt_compress;
975}
976
977static inline int crypto_comp_compress(struct crypto_comp *tfm,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 const u8 *src, unsigned int slen,
979 u8 *dst, unsigned int *dlen)
980{
Herbert Xu78a1fe42006-12-24 10:02:00 +1100981 return crypto_comp_crt(tfm)->cot_compress(crypto_comp_tfm(tfm),
982 src, slen, dst, dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983}
984
Herbert Xufce32d72006-08-26 17:35:45 +1000985static inline int crypto_comp_decompress(struct crypto_comp *tfm,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 const u8 *src, unsigned int slen,
987 u8 *dst, unsigned int *dlen)
988{
Herbert Xu78a1fe42006-12-24 10:02:00 +1100989 return crypto_comp_crt(tfm)->cot_decompress(crypto_comp_tfm(tfm),
990 src, slen, dst, dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991}
992
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993#endif /* _LINUX_CRYPTO_H */
994