blob: e2fd24714e001140d60e48ad362a0091eee96de7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Scatterlist Cryptographic API.
3 *
4 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
5 * Copyright (c) 2002 David S. Miller (davem@redhat.com)
Herbert Xu5cb1454b2005-11-05 16:58:14 +11006 * Copyright (c) 2005 Herbert Xu <herbert@gondor.apana.org.au>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * Portions derived from Cryptoapi, by Alexander Kjeldaas <astor@fast.no>
John Anthony Kazos Jr18735dd2007-10-19 23:07:36 +02009 * and Nettle, by Niels Möller.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the Free
13 * Software Foundation; either version 2 of the License, or (at your option)
14 * any later version.
15 *
16 */
17#ifndef _LINUX_CRYPTO_H
18#define _LINUX_CRYPTO_H
19
Arun Sharma600634972011-07-26 16:09:06 -070020#include <linux/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/list.h>
Paul Gortmaker187f1882011-11-23 20:12:59 -050023#include <linux/bug.h>
Herbert Xu79911102006-08-21 21:03:52 +100024#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/string.h>
Herbert Xu79911102006-08-21 21:03:52 +100026#include <linux/uaccess.h>
Gilad Ben-Yossefada69a12017-10-18 08:00:38 +010027#include <linux/completion.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29/*
Kees Cook5d26a102014-11-20 17:05:53 -080030 * Autoloaded crypto modules should only use a prefixed name to avoid allowing
31 * arbitrary modules to be loaded. Loading from userspace may still need the
32 * unprefixed names, so retains those aliases as well.
33 * This uses __MODULE_INFO directly instead of MODULE_ALIAS because pre-4.3
34 * gcc (e.g. avr32 toolchain) uses __LINE__ for uniqueness, and this macro
35 * expands twice on the same line. Instead, use a separate base name for the
36 * alias.
37 */
38#define MODULE_ALIAS_CRYPTO(name) \
39 __MODULE_INFO(alias, alias_userspace, name); \
40 __MODULE_INFO(alias, alias_crypto, "crypto-" name)
41
42/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 * Algorithm masks and types.
44 */
Herbert Xu28259822006-08-06 21:23:26 +100045#define CRYPTO_ALG_TYPE_MASK 0x0000000f
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#define CRYPTO_ALG_TYPE_CIPHER 0x00000001
Loc Ho004a4032008-05-14 20:41:47 +080047#define CRYPTO_ALG_TYPE_COMPRESS 0x00000002
48#define CRYPTO_ALG_TYPE_AEAD 0x00000003
Herbert Xu055bcee2006-08-19 22:24:23 +100049#define CRYPTO_ALG_TYPE_BLKCIPHER 0x00000004
Herbert Xu332f88402007-11-15 22:36:07 +080050#define CRYPTO_ALG_TYPE_ABLKCIPHER 0x00000005
Herbert Xu4e6c3df2016-07-12 13:17:31 +080051#define CRYPTO_ALG_TYPE_SKCIPHER 0x00000005
Herbert Xu61da88e2007-12-17 21:51:27 +080052#define CRYPTO_ALG_TYPE_GIVCIPHER 0x00000006
Salvatore Benedetto4e5f2c42016-06-22 17:49:13 +010053#define CRYPTO_ALG_TYPE_KPP 0x00000008
Giovanni Cabiddu2ebda742016-10-21 13:19:47 +010054#define CRYPTO_ALG_TYPE_ACOMPRESS 0x0000000a
Giovanni Cabiddu1ab53a72016-10-21 13:19:48 +010055#define CRYPTO_ALG_TYPE_SCOMPRESS 0x0000000b
Neil Horman17f0f4a2008-08-14 22:15:52 +100056#define CRYPTO_ALG_TYPE_RNG 0x0000000c
Tadeusz Struk3c339ab2015-06-16 10:30:55 -070057#define CRYPTO_ALG_TYPE_AKCIPHER 0x0000000d
Giovanni Cabiddu63044c42016-06-02 13:28:55 +010058#define CRYPTO_ALG_TYPE_DIGEST 0x0000000e
59#define CRYPTO_ALG_TYPE_HASH 0x0000000e
60#define CRYPTO_ALG_TYPE_SHASH 0x0000000e
61#define CRYPTO_ALG_TYPE_AHASH 0x0000000f
Herbert Xu055bcee2006-08-19 22:24:23 +100062
63#define CRYPTO_ALG_TYPE_HASH_MASK 0x0000000e
Giovanni Cabiddu63044c42016-06-02 13:28:55 +010064#define CRYPTO_ALG_TYPE_AHASH_MASK 0x0000000e
Herbert Xu332f88402007-11-15 22:36:07 +080065#define CRYPTO_ALG_TYPE_BLKCIPHER_MASK 0x0000000c
Giovanni Cabiddu1ab53a72016-10-21 13:19:48 +010066#define CRYPTO_ALG_TYPE_ACOMPRESS_MASK 0x0000000e
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Herbert Xu28259822006-08-06 21:23:26 +100068#define CRYPTO_ALG_LARVAL 0x00000010
Herbert Xu6bfd4802006-09-21 11:39:29 +100069#define CRYPTO_ALG_DEAD 0x00000020
70#define CRYPTO_ALG_DYING 0x00000040
Herbert Xuf3f632d2006-08-06 23:12:59 +100071#define CRYPTO_ALG_ASYNC 0x00000080
Herbert Xu28259822006-08-06 21:23:26 +100072
Linus Torvalds1da177e2005-04-16 15:20:36 -070073/*
Herbert Xu60104392006-08-26 18:34:10 +100074 * Set this bit if and only if the algorithm requires another algorithm of
75 * the same type to handle corner cases.
76 */
77#define CRYPTO_ALG_NEED_FALLBACK 0x00000100
78
79/*
Herbert Xuecfc4322007-12-05 21:08:36 +110080 * This bit is set for symmetric key ciphers that have already been wrapped
81 * with a generic IV generator to prevent them from being wrapped again.
82 */
83#define CRYPTO_ALG_GENIV 0x00000200
84
85/*
Herbert Xu73d38642008-08-03 21:15:23 +080086 * Set if the algorithm has passed automated run-time testing. Note that
87 * if there is no run-time testing for a given algorithm it is considered
88 * to have passed.
89 */
90
91#define CRYPTO_ALG_TESTED 0x00000400
92
93/*
Baruch Siach864e0982016-11-30 15:16:08 +020094 * Set if the algorithm is an instance that is built from templates.
Steffen Klassert64a947b2011-09-27 07:21:26 +020095 */
96#define CRYPTO_ALG_INSTANCE 0x00000800
97
Nikos Mavrogiannopoulosd912bb72011-11-01 13:39:56 +010098/* Set this bit if the algorithm provided is hardware accelerated but
99 * not available to userspace via instruction set or so.
100 */
101#define CRYPTO_ALG_KERN_DRIVER_ONLY 0x00001000
102
Steffen Klassert64a947b2011-09-27 07:21:26 +0200103/*
Stephan Mueller06ca7f62015-03-30 21:55:52 +0200104 * Mark a cipher as a service implementation only usable by another
105 * cipher and never by a normal user of the kernel crypto API
106 */
107#define CRYPTO_ALG_INTERNAL 0x00002000
108
109/*
Eric Biggersa208fa82018-01-03 11:16:26 -0800110 * Set if the algorithm has a ->setkey() method but can be used without
111 * calling it first, i.e. there is a default key.
112 */
113#define CRYPTO_ALG_OPTIONAL_KEY 0x00004000
114
115/*
Matthew Garrette2861fa2018-06-08 14:57:42 -0700116 * Don't trigger module loading
117 */
118#define CRYPTO_NOLOAD 0x00008000
119
120/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 * Transform masks and values (for crt_flags).
122 */
Eric Biggers9fa68f62018-01-03 11:16:27 -0800123#define CRYPTO_TFM_NEED_KEY 0x00000001
124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125#define CRYPTO_TFM_REQ_MASK 0x000fff00
126#define CRYPTO_TFM_RES_MASK 0xfff00000
127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128#define CRYPTO_TFM_REQ_WEAK_KEY 0x00000100
Herbert Xu64baf3c2005-09-01 17:43:05 -0700129#define CRYPTO_TFM_REQ_MAY_SLEEP 0x00000200
Herbert Xu32e3983f2007-03-24 14:35:34 +1100130#define CRYPTO_TFM_REQ_MAY_BACKLOG 0x00000400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131#define CRYPTO_TFM_RES_WEAK_KEY 0x00100000
132#define CRYPTO_TFM_RES_BAD_KEY_LEN 0x00200000
133#define CRYPTO_TFM_RES_BAD_KEY_SCHED 0x00400000
134#define CRYPTO_TFM_RES_BAD_BLOCK_LEN 0x00800000
135#define CRYPTO_TFM_RES_BAD_FLAGS 0x01000000
136
137/*
138 * Miscellaneous stuff.
139 */
Herbert Xuf437a3f2017-04-06 16:16:11 +0800140#define CRYPTO_MAX_ALG_NAME 128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Herbert Xu79911102006-08-21 21:03:52 +1000142/*
143 * The macro CRYPTO_MINALIGN_ATTR (along with the void * type in the actual
144 * declaration) is used to ensure that the crypto_tfm context structure is
145 * aligned correctly for the given architecture so that there are no alignment
146 * faults for C data types. In particular, this is required on platforms such
147 * as arm where pointers are 32-bit aligned but there are data types such as
148 * u64 which require 64-bit alignment.
149 */
Herbert Xu79911102006-08-21 21:03:52 +1000150#define CRYPTO_MINALIGN ARCH_KMALLOC_MINALIGN
Herbert Xu79911102006-08-21 21:03:52 +1000151
Herbert Xu79911102006-08-21 21:03:52 +1000152#define CRYPTO_MINALIGN_ATTR __attribute__ ((__aligned__(CRYPTO_MINALIGN)))
Herbert Xu79911102006-08-21 21:03:52 +1000153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154struct scatterlist;
Herbert Xu32e3983f2007-03-24 14:35:34 +1100155struct crypto_ablkcipher;
156struct crypto_async_request;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000157struct crypto_blkcipher;
Herbert Xu40725182005-07-06 13:51:52 -0700158struct crypto_tfm;
Herbert Xue853c3c2006-08-22 00:06:54 +1000159struct crypto_type;
Herbert Xu61da88e2007-12-17 21:51:27 +0800160struct skcipher_givcrypt_request;
Herbert Xu40725182005-07-06 13:51:52 -0700161
Herbert Xu32e3983f2007-03-24 14:35:34 +1100162typedef void (*crypto_completion_t)(struct crypto_async_request *req, int err);
163
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100164/**
165 * DOC: Block Cipher Context Data Structures
166 *
167 * These data structures define the operating context for each block cipher
168 * type.
169 */
170
Herbert Xu32e3983f2007-03-24 14:35:34 +1100171struct crypto_async_request {
172 struct list_head list;
173 crypto_completion_t complete;
174 void *data;
175 struct crypto_tfm *tfm;
176
177 u32 flags;
178};
179
180struct ablkcipher_request {
181 struct crypto_async_request base;
182
183 unsigned int nbytes;
184
185 void *info;
186
187 struct scatterlist *src;
188 struct scatterlist *dst;
189
190 void *__ctx[] CRYPTO_MINALIGN_ATTR;
191};
192
Herbert Xu5cde0af2006-08-22 00:07:53 +1000193struct blkcipher_desc {
194 struct crypto_blkcipher *tfm;
195 void *info;
196 u32 flags;
197};
198
Herbert Xu40725182005-07-06 13:51:52 -0700199struct cipher_desc {
200 struct crypto_tfm *tfm;
Herbert Xu6c2bb982006-05-16 22:09:29 +1000201 void (*crfn)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Herbert Xu40725182005-07-06 13:51:52 -0700202 unsigned int (*prfn)(const struct cipher_desc *desc, u8 *dst,
203 const u8 *src, unsigned int nbytes);
204 void *info;
205};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100207/**
208 * DOC: Block Cipher Algorithm Definitions
209 *
210 * These data structures define modular crypto algorithm implementations,
211 * managed via crypto_register_alg() and crypto_unregister_alg().
212 */
213
214/**
215 * struct ablkcipher_alg - asynchronous block cipher definition
216 * @min_keysize: Minimum key size supported by the transformation. This is the
217 * smallest key length supported by this transformation algorithm.
218 * This must be set to one of the pre-defined values as this is
219 * not hardware specific. Possible values for this field can be
220 * found via git grep "_MIN_KEY_SIZE" include/crypto/
221 * @max_keysize: Maximum key size supported by the transformation. This is the
222 * largest key length supported by this transformation algorithm.
223 * This must be set to one of the pre-defined values as this is
224 * not hardware specific. Possible values for this field can be
225 * found via git grep "_MAX_KEY_SIZE" include/crypto/
226 * @setkey: Set key for the transformation. This function is used to either
227 * program a supplied key into the hardware or store the key in the
228 * transformation context for programming it later. Note that this
229 * function does modify the transformation context. This function can
230 * be called multiple times during the existence of the transformation
231 * object, so one must make sure the key is properly reprogrammed into
232 * the hardware. This function is also responsible for checking the key
233 * length for validity. In case a software fallback was put in place in
234 * the @cra_init call, this function might need to use the fallback if
235 * the algorithm doesn't support all of the key sizes.
236 * @encrypt: Encrypt a scatterlist of blocks. This function is used to encrypt
237 * the supplied scatterlist containing the blocks of data. The crypto
238 * API consumer is responsible for aligning the entries of the
239 * scatterlist properly and making sure the chunks are correctly
240 * sized. In case a software fallback was put in place in the
241 * @cra_init call, this function might need to use the fallback if
242 * the algorithm doesn't support all of the key sizes. In case the
243 * key was stored in transformation context, the key might need to be
244 * re-programmed into the hardware in this function. This function
245 * shall not modify the transformation context, as this function may
246 * be called in parallel with the same transformation object.
247 * @decrypt: Decrypt a single block. This is a reverse counterpart to @encrypt
248 * and the conditions are exactly the same.
249 * @givencrypt: Update the IV for encryption. With this function, a cipher
250 * implementation may provide the function on how to update the IV
251 * for encryption.
252 * @givdecrypt: Update the IV for decryption. This is the reverse of
253 * @givencrypt .
254 * @geniv: The transformation implementation may use an "IV generator" provided
255 * by the kernel crypto API. Several use cases have a predefined
256 * approach how IVs are to be updated. For such use cases, the kernel
257 * crypto API provides ready-to-use implementations that can be
258 * referenced with this variable.
259 * @ivsize: IV size applicable for transformation. The consumer must provide an
260 * IV of exactly that size to perform the encrypt or decrypt operation.
261 *
262 * All fields except @givencrypt , @givdecrypt , @geniv and @ivsize are
263 * mandatory and must be filled.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 */
Herbert Xub5b7f082007-04-16 20:48:54 +1000265struct ablkcipher_alg {
266 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
267 unsigned int keylen);
268 int (*encrypt)(struct ablkcipher_request *req);
269 int (*decrypt)(struct ablkcipher_request *req);
Herbert Xu61da88e2007-12-17 21:51:27 +0800270 int (*givencrypt)(struct skcipher_givcrypt_request *req);
271 int (*givdecrypt)(struct skcipher_givcrypt_request *req);
Herbert Xub5b7f082007-04-16 20:48:54 +1000272
Herbert Xu23508e12007-11-27 21:33:24 +0800273 const char *geniv;
274
Herbert Xub5b7f082007-04-16 20:48:54 +1000275 unsigned int min_keysize;
276 unsigned int max_keysize;
277 unsigned int ivsize;
278};
279
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100280/**
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100281 * struct blkcipher_alg - synchronous block cipher definition
282 * @min_keysize: see struct ablkcipher_alg
283 * @max_keysize: see struct ablkcipher_alg
284 * @setkey: see struct ablkcipher_alg
285 * @encrypt: see struct ablkcipher_alg
286 * @decrypt: see struct ablkcipher_alg
287 * @geniv: see struct ablkcipher_alg
288 * @ivsize: see struct ablkcipher_alg
289 *
290 * All fields except @geniv and @ivsize are mandatory and must be filled.
291 */
Herbert Xu5cde0af2006-08-22 00:07:53 +1000292struct blkcipher_alg {
293 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
294 unsigned int keylen);
295 int (*encrypt)(struct blkcipher_desc *desc,
296 struct scatterlist *dst, struct scatterlist *src,
297 unsigned int nbytes);
298 int (*decrypt)(struct blkcipher_desc *desc,
299 struct scatterlist *dst, struct scatterlist *src,
300 unsigned int nbytes);
301
Herbert Xu23508e12007-11-27 21:33:24 +0800302 const char *geniv;
303
Herbert Xu5cde0af2006-08-22 00:07:53 +1000304 unsigned int min_keysize;
305 unsigned int max_keysize;
306 unsigned int ivsize;
307};
308
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100309/**
310 * struct cipher_alg - single-block symmetric ciphers definition
311 * @cia_min_keysize: Minimum key size supported by the transformation. This is
312 * the smallest key length supported by this transformation
313 * algorithm. This must be set to one of the pre-defined
314 * values as this is not hardware specific. Possible values
315 * for this field can be found via git grep "_MIN_KEY_SIZE"
316 * include/crypto/
317 * @cia_max_keysize: Maximum key size supported by the transformation. This is
318 * the largest key length supported by this transformation
319 * algorithm. This must be set to one of the pre-defined values
320 * as this is not hardware specific. Possible values for this
321 * field can be found via git grep "_MAX_KEY_SIZE"
322 * include/crypto/
323 * @cia_setkey: Set key for the transformation. This function is used to either
324 * program a supplied key into the hardware or store the key in the
325 * transformation context for programming it later. Note that this
326 * function does modify the transformation context. This function
327 * can be called multiple times during the existence of the
328 * transformation object, so one must make sure the key is properly
329 * reprogrammed into the hardware. This function is also
330 * responsible for checking the key length for validity.
331 * @cia_encrypt: Encrypt a single block. This function is used to encrypt a
332 * single block of data, which must be @cra_blocksize big. This
333 * always operates on a full @cra_blocksize and it is not possible
334 * to encrypt a block of smaller size. The supplied buffers must
335 * therefore also be at least of @cra_blocksize size. Both the
336 * input and output buffers are always aligned to @cra_alignmask.
337 * In case either of the input or output buffer supplied by user
338 * of the crypto API is not aligned to @cra_alignmask, the crypto
339 * API will re-align the buffers. The re-alignment means that a
340 * new buffer will be allocated, the data will be copied into the
341 * new buffer, then the processing will happen on the new buffer,
342 * then the data will be copied back into the original buffer and
343 * finally the new buffer will be freed. In case a software
344 * fallback was put in place in the @cra_init call, this function
345 * might need to use the fallback if the algorithm doesn't support
346 * all of the key sizes. In case the key was stored in
347 * transformation context, the key might need to be re-programmed
348 * into the hardware in this function. This function shall not
349 * modify the transformation context, as this function may be
350 * called in parallel with the same transformation object.
351 * @cia_decrypt: Decrypt a single block. This is a reverse counterpart to
352 * @cia_encrypt, and the conditions are exactly the same.
353 *
354 * All fields are mandatory and must be filled.
355 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356struct cipher_alg {
357 unsigned int cia_min_keysize;
358 unsigned int cia_max_keysize;
Herbert Xu6c2bb982006-05-16 22:09:29 +1000359 int (*cia_setkey)(struct crypto_tfm *tfm, const u8 *key,
Herbert Xu560c06a2006-08-13 14:16:39 +1000360 unsigned int keylen);
Herbert Xu6c2bb982006-05-16 22:09:29 +1000361 void (*cia_encrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
362 void (*cia_decrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363};
364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365struct compress_alg {
Herbert Xu6c2bb982006-05-16 22:09:29 +1000366 int (*coa_compress)(struct crypto_tfm *tfm, const u8 *src,
367 unsigned int slen, u8 *dst, unsigned int *dlen);
368 int (*coa_decompress)(struct crypto_tfm *tfm, const u8 *src,
369 unsigned int slen, u8 *dst, unsigned int *dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370};
371
Neil Horman17f0f4a2008-08-14 22:15:52 +1000372
Herbert Xub5b7f082007-04-16 20:48:54 +1000373#define cra_ablkcipher cra_u.ablkcipher
Herbert Xu5cde0af2006-08-22 00:07:53 +1000374#define cra_blkcipher cra_u.blkcipher
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375#define cra_cipher cra_u.cipher
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376#define cra_compress cra_u.compress
377
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100378/**
379 * struct crypto_alg - definition of a cryptograpic cipher algorithm
380 * @cra_flags: Flags describing this transformation. See include/linux/crypto.h
381 * CRYPTO_ALG_* flags for the flags which go in here. Those are
382 * used for fine-tuning the description of the transformation
383 * algorithm.
384 * @cra_blocksize: Minimum block size of this transformation. The size in bytes
385 * of the smallest possible unit which can be transformed with
386 * this algorithm. The users must respect this value.
387 * In case of HASH transformation, it is possible for a smaller
388 * block than @cra_blocksize to be passed to the crypto API for
389 * transformation, in case of any other transformation type, an
390 * error will be returned upon any attempt to transform smaller
391 * than @cra_blocksize chunks.
392 * @cra_ctxsize: Size of the operational context of the transformation. This
393 * value informs the kernel crypto API about the memory size
394 * needed to be allocated for the transformation context.
395 * @cra_alignmask: Alignment mask for the input and output data buffer. The data
396 * buffer containing the input data for the algorithm must be
397 * aligned to this alignment mask. The data buffer for the
398 * output data must be aligned to this alignment mask. Note that
399 * the Crypto API will do the re-alignment in software, but
400 * only under special conditions and there is a performance hit.
401 * The re-alignment happens at these occasions for different
402 * @cra_u types: cipher -- For both input data and output data
403 * buffer; ahash -- For output hash destination buf; shash --
404 * For output hash destination buf.
405 * This is needed on hardware which is flawed by design and
406 * cannot pick data from arbitrary addresses.
407 * @cra_priority: Priority of this transformation implementation. In case
408 * multiple transformations with same @cra_name are available to
409 * the Crypto API, the kernel will use the one with highest
410 * @cra_priority.
411 * @cra_name: Generic name (usable by multiple implementations) of the
412 * transformation algorithm. This is the name of the transformation
413 * itself. This field is used by the kernel when looking up the
414 * providers of particular transformation.
415 * @cra_driver_name: Unique name of the transformation provider. This is the
416 * name of the provider of the transformation. This can be any
417 * arbitrary value, but in the usual case, this contains the
418 * name of the chip or provider and the name of the
419 * transformation algorithm.
420 * @cra_type: Type of the cryptographic transformation. This is a pointer to
421 * struct crypto_type, which implements callbacks common for all
Masanari Iida12f7c142015-06-04 00:01:21 +0900422 * transformation types. There are multiple options:
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100423 * &crypto_blkcipher_type, &crypto_ablkcipher_type,
Herbert Xub0d955b2015-08-14 15:30:41 +0800424 * &crypto_ahash_type, &crypto_rng_type.
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100425 * This field might be empty. In that case, there are no common
426 * callbacks. This is the case for: cipher, compress, shash.
427 * @cra_u: Callbacks implementing the transformation. This is a union of
428 * multiple structures. Depending on the type of transformation selected
429 * by @cra_type and @cra_flags above, the associated structure must be
430 * filled with callbacks. This field might be empty. This is the case
431 * for ahash, shash.
432 * @cra_init: Initialize the cryptographic transformation object. This function
433 * is used to initialize the cryptographic transformation object.
434 * This function is called only once at the instantiation time, right
435 * after the transformation context was allocated. In case the
436 * cryptographic hardware has some special requirements which need to
437 * be handled by software, this function shall check for the precise
438 * requirement of the transformation and put any software fallbacks
439 * in place.
440 * @cra_exit: Deinitialize the cryptographic transformation object. This is a
441 * counterpart to @cra_init, used to remove various changes set in
442 * @cra_init.
Gary R Hook0063ec42018-03-14 17:15:52 -0500443 * @cra_u.ablkcipher: Union member which contains an asynchronous block cipher
444 * definition. See @struct @ablkcipher_alg.
445 * @cra_u.blkcipher: Union member which contains a synchronous block cipher
446 * definition See @struct @blkcipher_alg.
447 * @cra_u.cipher: Union member which contains a single-block symmetric cipher
448 * definition. See @struct @cipher_alg.
449 * @cra_u.compress: Union member which contains a (de)compression algorithm.
450 * See @struct @compress_alg.
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100451 * @cra_module: Owner of this transformation implementation. Set to THIS_MODULE
452 * @cra_list: internally used
453 * @cra_users: internally used
454 * @cra_refcnt: internally used
455 * @cra_destroy: internally used
456 *
Corentin Labbecac58182018-09-19 10:10:54 +0000457 * All following statistics are for this crypto_alg
458 * @encrypt_cnt: number of encrypt requests
459 * @decrypt_cnt: number of decrypt requests
460 * @compress_cnt: number of compress requests
461 * @decompress_cnt: number of decompress requests
462 * @generate_cnt: number of RNG generate requests
463 * @seed_cnt: number of times the rng was seeded
464 * @hash_cnt: number of hash requests
465 * @sign_cnt: number of sign requests
466 * @setsecret_cnt: number of setsecrey operation
467 * @generate_public_key_cnt: number of generate_public_key operation
468 * @verify_cnt: number of verify operation
469 * @compute_shared_secret_cnt: number of compute_shared_secret operation
470 * @encrypt_tlen: total data size handled by encrypt requests
471 * @decrypt_tlen: total data size handled by decrypt requests
472 * @compress_tlen: total data size handled by compress requests
473 * @decompress_tlen: total data size handled by decompress requests
474 * @generate_tlen: total data size of generated data by the RNG
475 * @hash_tlen: total data size hashed
476 * @akcipher_err_cnt: number of error for akcipher requests
477 * @cipher_err_cnt: number of error for akcipher requests
478 * @compress_err_cnt: number of error for akcipher requests
479 * @aead_err_cnt: number of error for akcipher requests
480 * @hash_err_cnt: number of error for akcipher requests
481 * @rng_err_cnt: number of error for akcipher requests
482 * @kpp_err_cnt: number of error for akcipher requests
483 *
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100484 * The struct crypto_alg describes a generic Crypto API algorithm and is common
485 * for all of the transformations. Any variable not documented here shall not
486 * be used by a cipher implementation as it is internal to the Crypto API.
487 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488struct crypto_alg {
489 struct list_head cra_list;
Herbert Xu6bfd4802006-09-21 11:39:29 +1000490 struct list_head cra_users;
491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 u32 cra_flags;
493 unsigned int cra_blocksize;
494 unsigned int cra_ctxsize;
Herbert Xu95477372005-07-06 13:52:09 -0700495 unsigned int cra_alignmask;
Herbert Xu5cb1454b2005-11-05 16:58:14 +1100496
497 int cra_priority;
Eric Biggersce8614a2017-12-29 10:00:46 -0600498 refcount_t cra_refcnt;
Herbert Xu5cb1454b2005-11-05 16:58:14 +1100499
Herbert Xud913ea02006-05-21 08:45:26 +1000500 char cra_name[CRYPTO_MAX_ALG_NAME];
501 char cra_driver_name[CRYPTO_MAX_ALG_NAME];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
Herbert Xue853c3c2006-08-22 00:06:54 +1000503 const struct crypto_type *cra_type;
504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 union {
Herbert Xub5b7f082007-04-16 20:48:54 +1000506 struct ablkcipher_alg ablkcipher;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000507 struct blkcipher_alg blkcipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 struct cipher_alg cipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 struct compress_alg compress;
510 } cra_u;
Herbert Xuc7fc0592006-05-24 13:02:26 +1000511
512 int (*cra_init)(struct crypto_tfm *tfm);
513 void (*cra_exit)(struct crypto_tfm *tfm);
Herbert Xu6521f302006-08-06 20:28:44 +1000514 void (*cra_destroy)(struct crypto_alg *alg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
516 struct module *cra_module;
Corentin Labbecac58182018-09-19 10:10:54 +0000517
Corentin Labbe2ced2602018-11-29 14:42:16 +0000518#ifdef CONFIG_CRYPTO_STATS
Corentin Labbecac58182018-09-19 10:10:54 +0000519 union {
Corentin Labbe6e8e72c2018-11-29 14:42:18 +0000520 atomic64_t encrypt_cnt;
521 atomic64_t compress_cnt;
522 atomic64_t generate_cnt;
523 atomic64_t hash_cnt;
524 atomic64_t setsecret_cnt;
Corentin Labbecac58182018-09-19 10:10:54 +0000525 };
526 union {
527 atomic64_t encrypt_tlen;
528 atomic64_t compress_tlen;
529 atomic64_t generate_tlen;
530 atomic64_t hash_tlen;
531 };
532 union {
Corentin Labbe6e8e72c2018-11-29 14:42:18 +0000533 atomic64_t akcipher_err_cnt;
534 atomic64_t cipher_err_cnt;
535 atomic64_t compress_err_cnt;
536 atomic64_t aead_err_cnt;
537 atomic64_t hash_err_cnt;
538 atomic64_t rng_err_cnt;
539 atomic64_t kpp_err_cnt;
Corentin Labbecac58182018-09-19 10:10:54 +0000540 };
541 union {
Corentin Labbe6e8e72c2018-11-29 14:42:18 +0000542 atomic64_t decrypt_cnt;
543 atomic64_t decompress_cnt;
544 atomic64_t seed_cnt;
545 atomic64_t generate_public_key_cnt;
Corentin Labbecac58182018-09-19 10:10:54 +0000546 };
547 union {
548 atomic64_t decrypt_tlen;
549 atomic64_t decompress_tlen;
550 };
551 union {
Corentin Labbe6e8e72c2018-11-29 14:42:18 +0000552 atomic64_t verify_cnt;
553 atomic64_t compute_shared_secret_cnt;
Corentin Labbecac58182018-09-19 10:10:54 +0000554 };
Corentin Labbe6e8e72c2018-11-29 14:42:18 +0000555 atomic64_t sign_cnt;
Corentin Labbe2ced2602018-11-29 14:42:16 +0000556#endif /* CONFIG_CRYPTO_STATS */
Corentin Labbecac58182018-09-19 10:10:54 +0000557
Herbert Xuedf18b92015-06-18 14:00:48 +0800558} CRYPTO_MINALIGN_ATTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Corentin Labbef7d76e02018-11-29 14:42:21 +0000560#ifdef CONFIG_CRYPTO_STATS
561void crypto_stats_get(struct crypto_alg *alg);
562void crypto_stats_ablkcipher_encrypt(unsigned int nbytes, int ret, struct crypto_alg *alg);
563void crypto_stats_ablkcipher_decrypt(unsigned int nbytes, int ret, struct crypto_alg *alg);
564void crypto_stats_aead_encrypt(unsigned int cryptlen, struct crypto_alg *alg, int ret);
565void crypto_stats_aead_decrypt(unsigned int cryptlen, struct crypto_alg *alg, int ret);
566void crypto_stats_ahash_update(unsigned int nbytes, int ret, struct crypto_alg *alg);
567void crypto_stats_ahash_final(unsigned int nbytes, int ret, struct crypto_alg *alg);
568void crypto_stats_akcipher_encrypt(unsigned int src_len, int ret, struct crypto_alg *alg);
569void crypto_stats_akcipher_decrypt(unsigned int src_len, int ret, struct crypto_alg *alg);
570void crypto_stats_akcipher_sign(int ret, struct crypto_alg *alg);
571void crypto_stats_akcipher_verify(int ret, struct crypto_alg *alg);
572void crypto_stats_compress(unsigned int slen, int ret, struct crypto_alg *alg);
573void crypto_stats_decompress(unsigned int slen, int ret, struct crypto_alg *alg);
574void crypto_stats_kpp_set_secret(struct crypto_alg *alg, int ret);
575void crypto_stats_kpp_generate_public_key(struct crypto_alg *alg, int ret);
576void crypto_stats_kpp_compute_shared_secret(struct crypto_alg *alg, int ret);
577void crypto_stats_rng_seed(struct crypto_alg *alg, int ret);
578void crypto_stats_rng_generate(struct crypto_alg *alg, unsigned int dlen, int ret);
579void crypto_stats_skcipher_encrypt(unsigned int cryptlen, int ret, struct crypto_alg *alg);
580void crypto_stats_skcipher_decrypt(unsigned int cryptlen, int ret, struct crypto_alg *alg);
581#else
582static inline void crypto_stats_get(struct crypto_alg *alg)
583{}
584static inline void crypto_stats_ablkcipher_encrypt(unsigned int nbytes, int ret, struct crypto_alg *alg)
585{}
586static inline void crypto_stats_ablkcipher_decrypt(unsigned int nbytes, int ret, struct crypto_alg *alg)
587{}
588static inline void crypto_stats_aead_encrypt(unsigned int cryptlen, struct crypto_alg *alg, int ret)
589{}
590static inline void crypto_stats_aead_decrypt(unsigned int cryptlen, struct crypto_alg *alg, int ret)
591{}
592static inline void crypto_stats_ahash_update(unsigned int nbytes, int ret, struct crypto_alg *alg)
593{}
594static inline void crypto_stats_ahash_final(unsigned int nbytes, int ret, struct crypto_alg *alg)
595{}
596static inline void crypto_stats_akcipher_encrypt(unsigned int src_len, int ret, struct crypto_alg *alg)
597{}
598static inline void crypto_stats_akcipher_decrypt(unsigned int src_len, int ret, struct crypto_alg *alg)
599{}
600static inline void crypto_stats_akcipher_sign(int ret, struct crypto_alg *alg)
601{}
602static inline void crypto_stats_akcipher_verify(int ret, struct crypto_alg *alg)
603{}
604static inline void crypto_stats_compress(unsigned int slen, int ret, struct crypto_alg *alg)
605{}
606static inline void crypto_stats_decompress(unsigned int slen, int ret, struct crypto_alg *alg)
607{}
608static inline void crypto_stats_kpp_set_secret(struct crypto_alg *alg, int ret)
609{}
610static inline void crypto_stats_kpp_generate_public_key(struct crypto_alg *alg, int ret)
611{}
612static inline void crypto_stats_kpp_compute_shared_secret(struct crypto_alg *alg, int ret)
613{}
614static inline void crypto_stats_rng_seed(struct crypto_alg *alg, int ret)
615{}
616static inline void crypto_stats_rng_generate(struct crypto_alg *alg, unsigned int dlen, int ret)
617{}
618static inline void crypto_stats_skcipher_encrypt(unsigned int cryptlen, int ret, struct crypto_alg *alg)
619{}
620static inline void crypto_stats_skcipher_decrypt(unsigned int cryptlen, int ret, struct crypto_alg *alg)
621{}
622#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623/*
Gilad Ben-Yossefada69a12017-10-18 08:00:38 +0100624 * A helper struct for waiting for completion of async crypto ops
625 */
626struct crypto_wait {
627 struct completion completion;
628 int err;
629};
630
631/*
632 * Macro for declaring a crypto op async wait object on stack
633 */
634#define DECLARE_CRYPTO_WAIT(_wait) \
635 struct crypto_wait _wait = { \
636 COMPLETION_INITIALIZER_ONSTACK((_wait).completion), 0 }
637
638/*
639 * Async ops completion helper functioons
640 */
641void crypto_req_done(struct crypto_async_request *req, int err);
642
643static inline int crypto_wait_req(int err, struct crypto_wait *wait)
644{
645 switch (err) {
646 case -EINPROGRESS:
647 case -EBUSY:
648 wait_for_completion(&wait->completion);
649 reinit_completion(&wait->completion);
650 err = wait->err;
651 break;
652 };
653
654 return err;
655}
656
657static inline void crypto_init_wait(struct crypto_wait *wait)
658{
659 init_completion(&wait->completion);
660}
661
662/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 * Algorithm registration interface.
664 */
665int crypto_register_alg(struct crypto_alg *alg);
666int crypto_unregister_alg(struct crypto_alg *alg);
Mark Brown4b004342012-01-17 23:34:26 +0000667int crypto_register_algs(struct crypto_alg *algs, int count);
668int crypto_unregister_algs(struct crypto_alg *algs, int count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
670/*
671 * Algorithm query interface.
672 */
Herbert Xufce32d72006-08-26 17:35:45 +1000673int crypto_has_alg(const char *name, u32 type, u32 mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
675/*
676 * Transforms: user-instantiated objects which encapsulate algorithms
Herbert Xu6d7d684d2006-07-30 11:53:01 +1000677 * and core processing logic. Managed via crypto_alloc_*() and
678 * crypto_free_*(), as well as the various helpers below.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
Herbert Xu32e3983f2007-03-24 14:35:34 +1100681struct ablkcipher_tfm {
682 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
683 unsigned int keylen);
684 int (*encrypt)(struct ablkcipher_request *req);
685 int (*decrypt)(struct ablkcipher_request *req);
Herbert Xu61da88e2007-12-17 21:51:27 +0800686
Herbert Xuecfc4322007-12-05 21:08:36 +1100687 struct crypto_ablkcipher *base;
688
Herbert Xu32e3983f2007-03-24 14:35:34 +1100689 unsigned int ivsize;
690 unsigned int reqsize;
691};
692
Herbert Xu5cde0af2006-08-22 00:07:53 +1000693struct blkcipher_tfm {
694 void *iv;
695 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
696 unsigned int keylen);
697 int (*encrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
698 struct scatterlist *src, unsigned int nbytes);
699 int (*decrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
700 struct scatterlist *src, unsigned int nbytes);
701};
702
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703struct cipher_tfm {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 int (*cit_setkey)(struct crypto_tfm *tfm,
705 const u8 *key, unsigned int keylen);
Herbert Xuf28776a2006-08-13 20:58:18 +1000706 void (*cit_encrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
707 void (*cit_decrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708};
709
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710struct compress_tfm {
711 int (*cot_compress)(struct crypto_tfm *tfm,
712 const u8 *src, unsigned int slen,
713 u8 *dst, unsigned int *dlen);
714 int (*cot_decompress)(struct crypto_tfm *tfm,
715 const u8 *src, unsigned int slen,
716 u8 *dst, unsigned int *dlen);
717};
718
Herbert Xu32e3983f2007-03-24 14:35:34 +1100719#define crt_ablkcipher crt_u.ablkcipher
Herbert Xu5cde0af2006-08-22 00:07:53 +1000720#define crt_blkcipher crt_u.blkcipher
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721#define crt_cipher crt_u.cipher
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722#define crt_compress crt_u.compress
723
724struct crypto_tfm {
725
726 u32 crt_flags;
727
728 union {
Herbert Xu32e3983f2007-03-24 14:35:34 +1100729 struct ablkcipher_tfm ablkcipher;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000730 struct blkcipher_tfm blkcipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 struct cipher_tfm cipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 struct compress_tfm compress;
733 } crt_u;
Herbert Xu4a779482008-09-13 18:19:03 -0700734
735 void (*exit)(struct crypto_tfm *tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
737 struct crypto_alg *__crt_alg;
Herbert Xuf10b7892006-01-25 22:34:01 +1100738
Herbert Xu79911102006-08-21 21:03:52 +1000739 void *__crt_ctx[] CRYPTO_MINALIGN_ATTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740};
741
Herbert Xu32e3983f2007-03-24 14:35:34 +1100742struct crypto_ablkcipher {
743 struct crypto_tfm base;
744};
745
Herbert Xu5cde0af2006-08-22 00:07:53 +1000746struct crypto_blkcipher {
747 struct crypto_tfm base;
748};
749
Herbert Xu78a1fe42006-12-24 10:02:00 +1100750struct crypto_cipher {
751 struct crypto_tfm base;
752};
753
754struct crypto_comp {
755 struct crypto_tfm base;
756};
757
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000758enum {
759 CRYPTOA_UNSPEC,
760 CRYPTOA_ALG,
Herbert Xuebc610e2007-01-01 18:37:02 +1100761 CRYPTOA_TYPE,
Herbert Xu39e1ee012007-08-29 19:27:26 +0800762 CRYPTOA_U32,
Herbert Xuebc610e2007-01-01 18:37:02 +1100763 __CRYPTOA_MAX,
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000764};
765
Herbert Xuebc610e2007-01-01 18:37:02 +1100766#define CRYPTOA_MAX (__CRYPTOA_MAX - 1)
767
Herbert Xu39e1ee012007-08-29 19:27:26 +0800768/* Maximum number of (rtattr) parameters for each template. */
769#define CRYPTO_MAX_ATTRS 32
770
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000771struct crypto_attr_alg {
772 char name[CRYPTO_MAX_ALG_NAME];
773};
774
Herbert Xuebc610e2007-01-01 18:37:02 +1100775struct crypto_attr_type {
776 u32 type;
777 u32 mask;
778};
779
Herbert Xu39e1ee012007-08-29 19:27:26 +0800780struct crypto_attr_u32 {
781 u32 num;
782};
783
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784/*
785 * Transform user interface.
786 */
787
Herbert Xu6d7d684d2006-07-30 11:53:01 +1000788struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask);
Herbert Xu7b2cd922009-02-05 16:48:24 +1100789void crypto_destroy_tfm(void *mem, struct crypto_tfm *tfm);
790
791static inline void crypto_free_tfm(struct crypto_tfm *tfm)
792{
793 return crypto_destroy_tfm(tfm, tfm);
794}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
Herbert Xuda7f0332008-07-31 17:08:25 +0800796int alg_test(const char *driver, const char *alg, u32 type, u32 mask);
797
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798/*
799 * Transform helpers which query the underlying algorithm.
800 */
801static inline const char *crypto_tfm_alg_name(struct crypto_tfm *tfm)
802{
803 return tfm->__crt_alg->cra_name;
804}
805
Michal Ludvigb14cdd62006-07-09 09:02:24 +1000806static inline const char *crypto_tfm_alg_driver_name(struct crypto_tfm *tfm)
807{
808 return tfm->__crt_alg->cra_driver_name;
809}
810
811static inline int crypto_tfm_alg_priority(struct crypto_tfm *tfm)
812{
813 return tfm->__crt_alg->cra_priority;
814}
815
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm)
817{
818 return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK;
819}
820
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm)
822{
823 return tfm->__crt_alg->cra_blocksize;
824}
825
Herbert Xufbdae9f2005-07-06 13:53:29 -0700826static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm *tfm)
827{
828 return tfm->__crt_alg->cra_alignmask;
829}
830
Herbert Xuf28776a2006-08-13 20:58:18 +1000831static inline u32 crypto_tfm_get_flags(struct crypto_tfm *tfm)
832{
833 return tfm->crt_flags;
834}
835
836static inline void crypto_tfm_set_flags(struct crypto_tfm *tfm, u32 flags)
837{
838 tfm->crt_flags |= flags;
839}
840
841static inline void crypto_tfm_clear_flags(struct crypto_tfm *tfm, u32 flags)
842{
843 tfm->crt_flags &= ~flags;
844}
845
Herbert Xu40725182005-07-06 13:51:52 -0700846static inline void *crypto_tfm_ctx(struct crypto_tfm *tfm)
847{
Herbert Xuf10b7892006-01-25 22:34:01 +1100848 return tfm->__crt_ctx;
849}
850
851static inline unsigned int crypto_tfm_ctx_alignment(void)
852{
853 struct crypto_tfm *tfm;
854 return __alignof__(tfm->__crt_ctx);
Herbert Xu40725182005-07-06 13:51:52 -0700855}
856
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857/*
858 * API wrappers.
859 */
Herbert Xu32e3983f2007-03-24 14:35:34 +1100860static inline struct crypto_ablkcipher *__crypto_ablkcipher_cast(
861 struct crypto_tfm *tfm)
862{
863 return (struct crypto_ablkcipher *)tfm;
864}
865
Herbert Xu378f4f52007-12-17 20:07:31 +0800866static inline u32 crypto_skcipher_type(u32 type)
867{
Herbert Xuecfc4322007-12-05 21:08:36 +1100868 type &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
Herbert Xu378f4f52007-12-17 20:07:31 +0800869 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
870 return type;
871}
872
873static inline u32 crypto_skcipher_mask(u32 mask)
874{
Herbert Xuecfc4322007-12-05 21:08:36 +1100875 mask &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
Herbert Xu378f4f52007-12-17 20:07:31 +0800876 mask |= CRYPTO_ALG_TYPE_BLKCIPHER_MASK;
877 return mask;
878}
879
Stephan Muellerf13ec332014-11-12 05:28:22 +0100880/**
881 * DOC: Asynchronous Block Cipher API
882 *
883 * Asynchronous block cipher API is used with the ciphers of type
884 * CRYPTO_ALG_TYPE_ABLKCIPHER (listed as type "ablkcipher" in /proc/crypto).
885 *
886 * Asynchronous cipher operations imply that the function invocation for a
887 * cipher request returns immediately before the completion of the operation.
888 * The cipher request is scheduled as a separate kernel thread and therefore
889 * load-balanced on the different CPUs via the process scheduler. To allow
890 * the kernel crypto API to inform the caller about the completion of a cipher
891 * request, the caller must provide a callback function. That function is
892 * invoked with the cipher handle when the request completes.
893 *
894 * To support the asynchronous operation, additional information than just the
895 * cipher handle must be supplied to the kernel crypto API. That additional
896 * information is given by filling in the ablkcipher_request data structure.
897 *
898 * For the asynchronous block cipher API, the state is maintained with the tfm
899 * cipher handle. A single tfm can be used across multiple calls and in
900 * parallel. For asynchronous block cipher calls, context data supplied and
901 * only used by the caller can be referenced the request data structure in
902 * addition to the IV used for the cipher request. The maintenance of such
903 * state information would be important for a crypto driver implementer to
904 * have, because when calling the callback function upon completion of the
905 * cipher operation, that callback function may need some information about
906 * which operation just finished if it invoked multiple in parallel. This
907 * state information is unused by the kernel crypto API.
908 */
909
Herbert Xu32e3983f2007-03-24 14:35:34 +1100910static inline struct crypto_tfm *crypto_ablkcipher_tfm(
911 struct crypto_ablkcipher *tfm)
912{
913 return &tfm->base;
914}
915
Stephan Muellerf13ec332014-11-12 05:28:22 +0100916/**
917 * crypto_free_ablkcipher() - zeroize and free cipher handle
918 * @tfm: cipher handle to be freed
919 */
Herbert Xu32e3983f2007-03-24 14:35:34 +1100920static inline void crypto_free_ablkcipher(struct crypto_ablkcipher *tfm)
921{
922 crypto_free_tfm(crypto_ablkcipher_tfm(tfm));
923}
924
Stephan Muellerf13ec332014-11-12 05:28:22 +0100925/**
926 * crypto_has_ablkcipher() - Search for the availability of an ablkcipher.
927 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
928 * ablkcipher
929 * @type: specifies the type of the cipher
930 * @mask: specifies the mask for the cipher
931 *
932 * Return: true when the ablkcipher is known to the kernel crypto API; false
933 * otherwise
934 */
Herbert Xu32e3983f2007-03-24 14:35:34 +1100935static inline int crypto_has_ablkcipher(const char *alg_name, u32 type,
936 u32 mask)
937{
Herbert Xu378f4f52007-12-17 20:07:31 +0800938 return crypto_has_alg(alg_name, crypto_skcipher_type(type),
939 crypto_skcipher_mask(mask));
Herbert Xu32e3983f2007-03-24 14:35:34 +1100940}
941
942static inline struct ablkcipher_tfm *crypto_ablkcipher_crt(
943 struct crypto_ablkcipher *tfm)
944{
945 return &crypto_ablkcipher_tfm(tfm)->crt_ablkcipher;
946}
947
Stephan Muellerf13ec332014-11-12 05:28:22 +0100948/**
949 * crypto_ablkcipher_ivsize() - obtain IV size
950 * @tfm: cipher handle
951 *
952 * The size of the IV for the ablkcipher referenced by the cipher handle is
953 * returned. This IV size may be zero if the cipher does not need an IV.
954 *
955 * Return: IV size in bytes
956 */
Herbert Xu32e3983f2007-03-24 14:35:34 +1100957static inline unsigned int crypto_ablkcipher_ivsize(
958 struct crypto_ablkcipher *tfm)
959{
960 return crypto_ablkcipher_crt(tfm)->ivsize;
961}
962
Stephan Muellerf13ec332014-11-12 05:28:22 +0100963/**
964 * crypto_ablkcipher_blocksize() - obtain block size of cipher
965 * @tfm: cipher handle
966 *
967 * The block size for the ablkcipher referenced with the cipher handle is
968 * returned. The caller may use that information to allocate appropriate
969 * memory for the data returned by the encryption or decryption operation
970 *
971 * Return: block size of cipher
972 */
Herbert Xu32e3983f2007-03-24 14:35:34 +1100973static inline unsigned int crypto_ablkcipher_blocksize(
974 struct crypto_ablkcipher *tfm)
975{
976 return crypto_tfm_alg_blocksize(crypto_ablkcipher_tfm(tfm));
977}
978
979static inline unsigned int crypto_ablkcipher_alignmask(
980 struct crypto_ablkcipher *tfm)
981{
982 return crypto_tfm_alg_alignmask(crypto_ablkcipher_tfm(tfm));
983}
984
985static inline u32 crypto_ablkcipher_get_flags(struct crypto_ablkcipher *tfm)
986{
987 return crypto_tfm_get_flags(crypto_ablkcipher_tfm(tfm));
988}
989
990static inline void crypto_ablkcipher_set_flags(struct crypto_ablkcipher *tfm,
991 u32 flags)
992{
993 crypto_tfm_set_flags(crypto_ablkcipher_tfm(tfm), flags);
994}
995
996static inline void crypto_ablkcipher_clear_flags(struct crypto_ablkcipher *tfm,
997 u32 flags)
998{
999 crypto_tfm_clear_flags(crypto_ablkcipher_tfm(tfm), flags);
1000}
1001
Stephan Muellerf13ec332014-11-12 05:28:22 +01001002/**
1003 * crypto_ablkcipher_setkey() - set key for cipher
1004 * @tfm: cipher handle
1005 * @key: buffer holding the key
1006 * @keylen: length of the key in bytes
1007 *
1008 * The caller provided key is set for the ablkcipher referenced by the cipher
1009 * handle.
1010 *
1011 * Note, the key length determines the cipher type. Many block ciphers implement
1012 * different cipher modes depending on the key size, such as AES-128 vs AES-192
1013 * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
1014 * is performed.
1015 *
1016 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
1017 */
Herbert Xu32e3983f2007-03-24 14:35:34 +11001018static inline int crypto_ablkcipher_setkey(struct crypto_ablkcipher *tfm,
1019 const u8 *key, unsigned int keylen)
1020{
Herbert Xuecfc4322007-12-05 21:08:36 +11001021 struct ablkcipher_tfm *crt = crypto_ablkcipher_crt(tfm);
1022
1023 return crt->setkey(crt->base, key, keylen);
Herbert Xu32e3983f2007-03-24 14:35:34 +11001024}
1025
Stephan Muellerf13ec332014-11-12 05:28:22 +01001026/**
1027 * crypto_ablkcipher_reqtfm() - obtain cipher handle from request
1028 * @req: ablkcipher_request out of which the cipher handle is to be obtained
1029 *
1030 * Return the crypto_ablkcipher handle when furnishing an ablkcipher_request
1031 * data structure.
1032 *
1033 * Return: crypto_ablkcipher handle
1034 */
Herbert Xu32e3983f2007-03-24 14:35:34 +11001035static inline struct crypto_ablkcipher *crypto_ablkcipher_reqtfm(
1036 struct ablkcipher_request *req)
1037{
1038 return __crypto_ablkcipher_cast(req->base.tfm);
1039}
1040
Stephan Muellerf13ec332014-11-12 05:28:22 +01001041/**
1042 * crypto_ablkcipher_encrypt() - encrypt plaintext
1043 * @req: reference to the ablkcipher_request handle that holds all information
1044 * needed to perform the cipher operation
1045 *
1046 * Encrypt plaintext data using the ablkcipher_request handle. That data
1047 * structure and how it is filled with data is discussed with the
1048 * ablkcipher_request_* functions.
1049 *
1050 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1051 */
Herbert Xu32e3983f2007-03-24 14:35:34 +11001052static inline int crypto_ablkcipher_encrypt(struct ablkcipher_request *req)
1053{
1054 struct ablkcipher_tfm *crt =
1055 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
Corentin Labbef7d76e02018-11-29 14:42:21 +00001056 struct crypto_alg *alg = crt->base->base.__crt_alg;
1057 unsigned int nbytes = req->nbytes;
Corentin Labbecac58182018-09-19 10:10:54 +00001058 int ret;
1059
Corentin Labbef7d76e02018-11-29 14:42:21 +00001060 crypto_stats_get(alg);
Corentin Labbecac58182018-09-19 10:10:54 +00001061 ret = crt->encrypt(req);
Corentin Labbef7d76e02018-11-29 14:42:21 +00001062 crypto_stats_ablkcipher_encrypt(nbytes, ret, alg);
Corentin Labbecac58182018-09-19 10:10:54 +00001063 return ret;
Herbert Xu32e3983f2007-03-24 14:35:34 +11001064}
1065
Stephan Muellerf13ec332014-11-12 05:28:22 +01001066/**
1067 * crypto_ablkcipher_decrypt() - decrypt ciphertext
1068 * @req: reference to the ablkcipher_request handle that holds all information
1069 * needed to perform the cipher operation
1070 *
1071 * Decrypt ciphertext data using the ablkcipher_request handle. That data
1072 * structure and how it is filled with data is discussed with the
1073 * ablkcipher_request_* functions.
1074 *
1075 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1076 */
Herbert Xu32e3983f2007-03-24 14:35:34 +11001077static inline int crypto_ablkcipher_decrypt(struct ablkcipher_request *req)
1078{
1079 struct ablkcipher_tfm *crt =
1080 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
Corentin Labbef7d76e02018-11-29 14:42:21 +00001081 struct crypto_alg *alg = crt->base->base.__crt_alg;
1082 unsigned int nbytes = req->nbytes;
Corentin Labbecac58182018-09-19 10:10:54 +00001083 int ret;
1084
Corentin Labbef7d76e02018-11-29 14:42:21 +00001085 crypto_stats_get(alg);
Corentin Labbecac58182018-09-19 10:10:54 +00001086 ret = crt->decrypt(req);
Corentin Labbef7d76e02018-11-29 14:42:21 +00001087 crypto_stats_ablkcipher_decrypt(nbytes, ret, alg);
Corentin Labbecac58182018-09-19 10:10:54 +00001088 return ret;
Herbert Xu32e3983f2007-03-24 14:35:34 +11001089}
1090
Stephan Muellerf13ec332014-11-12 05:28:22 +01001091/**
1092 * DOC: Asynchronous Cipher Request Handle
1093 *
1094 * The ablkcipher_request data structure contains all pointers to data
1095 * required for the asynchronous cipher operation. This includes the cipher
1096 * handle (which can be used by multiple ablkcipher_request instances), pointer
1097 * to plaintext and ciphertext, asynchronous callback function, etc. It acts
1098 * as a handle to the ablkcipher_request_* API calls in a similar way as
1099 * ablkcipher handle to the crypto_ablkcipher_* API calls.
1100 */
1101
1102/**
1103 * crypto_ablkcipher_reqsize() - obtain size of the request data structure
1104 * @tfm: cipher handle
1105 *
1106 * Return: number of bytes
1107 */
Herbert Xub16c3a22007-08-29 19:02:04 +08001108static inline unsigned int crypto_ablkcipher_reqsize(
1109 struct crypto_ablkcipher *tfm)
Herbert Xu32e3983f2007-03-24 14:35:34 +11001110{
1111 return crypto_ablkcipher_crt(tfm)->reqsize;
1112}
1113
Stephan Muellerf13ec332014-11-12 05:28:22 +01001114/**
1115 * ablkcipher_request_set_tfm() - update cipher handle reference in request
1116 * @req: request handle to be modified
1117 * @tfm: cipher handle that shall be added to the request handle
1118 *
1119 * Allow the caller to replace the existing ablkcipher handle in the request
1120 * data structure with a different one.
1121 */
Herbert Xue196d622007-04-14 16:09:14 +10001122static inline void ablkcipher_request_set_tfm(
1123 struct ablkcipher_request *req, struct crypto_ablkcipher *tfm)
1124{
Herbert Xuecfc4322007-12-05 21:08:36 +11001125 req->base.tfm = crypto_ablkcipher_tfm(crypto_ablkcipher_crt(tfm)->base);
Herbert Xue196d622007-04-14 16:09:14 +10001126}
1127
Herbert Xub5b7f082007-04-16 20:48:54 +10001128static inline struct ablkcipher_request *ablkcipher_request_cast(
1129 struct crypto_async_request *req)
1130{
1131 return container_of(req, struct ablkcipher_request, base);
1132}
1133
Stephan Muellerf13ec332014-11-12 05:28:22 +01001134/**
1135 * ablkcipher_request_alloc() - allocate request data structure
1136 * @tfm: cipher handle to be registered with the request
1137 * @gfp: memory allocation flag that is handed to kmalloc by the API call.
1138 *
1139 * Allocate the request data structure that must be used with the ablkcipher
1140 * encrypt and decrypt API calls. During the allocation, the provided ablkcipher
1141 * handle is registered in the request data structure.
1142 *
Eric Biggers6eae29e2016-04-02 10:54:56 -05001143 * Return: allocated request handle in case of success, or NULL if out of memory
Stephan Muellerf13ec332014-11-12 05:28:22 +01001144 */
Herbert Xu32e3983f2007-03-24 14:35:34 +11001145static inline struct ablkcipher_request *ablkcipher_request_alloc(
1146 struct crypto_ablkcipher *tfm, gfp_t gfp)
1147{
1148 struct ablkcipher_request *req;
1149
1150 req = kmalloc(sizeof(struct ablkcipher_request) +
1151 crypto_ablkcipher_reqsize(tfm), gfp);
1152
1153 if (likely(req))
Herbert Xue196d622007-04-14 16:09:14 +10001154 ablkcipher_request_set_tfm(req, tfm);
Herbert Xu32e3983f2007-03-24 14:35:34 +11001155
1156 return req;
1157}
1158
Stephan Muellerf13ec332014-11-12 05:28:22 +01001159/**
1160 * ablkcipher_request_free() - zeroize and free request data structure
1161 * @req: request data structure cipher handle to be freed
1162 */
Herbert Xu32e3983f2007-03-24 14:35:34 +11001163static inline void ablkcipher_request_free(struct ablkcipher_request *req)
1164{
Herbert Xuaef73cf2009-07-11 22:22:14 +08001165 kzfree(req);
Herbert Xu32e3983f2007-03-24 14:35:34 +11001166}
1167
Stephan Muellerf13ec332014-11-12 05:28:22 +01001168/**
1169 * ablkcipher_request_set_callback() - set asynchronous callback function
1170 * @req: request handle
1171 * @flags: specify zero or an ORing of the flags
Stephan Mueller0184cfe2016-10-21 04:57:27 +02001172 * CRYPTO_TFM_REQ_MAY_BACKLOG the request queue may back log and
Stephan Muellerf13ec332014-11-12 05:28:22 +01001173 * increase the wait queue beyond the initial maximum size;
1174 * CRYPTO_TFM_REQ_MAY_SLEEP the request processing may sleep
1175 * @compl: callback function pointer to be registered with the request handle
1176 * @data: The data pointer refers to memory that is not used by the kernel
1177 * crypto API, but provided to the callback function for it to use. Here,
1178 * the caller can provide a reference to memory the callback function can
1179 * operate on. As the callback function is invoked asynchronously to the
1180 * related functionality, it may need to access data structures of the
1181 * related functionality which can be referenced using this pointer. The
1182 * callback function can access the memory via the "data" field in the
1183 * crypto_async_request data structure provided to the callback function.
1184 *
1185 * This function allows setting the callback function that is triggered once the
1186 * cipher operation completes.
1187 *
1188 * The callback function is registered with the ablkcipher_request handle and
Stephan Mueller0184cfe2016-10-21 04:57:27 +02001189 * must comply with the following template::
Stephan Muellerf13ec332014-11-12 05:28:22 +01001190 *
1191 * void callback_function(struct crypto_async_request *req, int error)
1192 */
Herbert Xu32e3983f2007-03-24 14:35:34 +11001193static inline void ablkcipher_request_set_callback(
1194 struct ablkcipher_request *req,
Mark Rustad3e3dc252014-07-25 02:53:38 -07001195 u32 flags, crypto_completion_t compl, void *data)
Herbert Xu32e3983f2007-03-24 14:35:34 +11001196{
Mark Rustad3e3dc252014-07-25 02:53:38 -07001197 req->base.complete = compl;
Herbert Xu32e3983f2007-03-24 14:35:34 +11001198 req->base.data = data;
1199 req->base.flags = flags;
1200}
1201
Stephan Muellerf13ec332014-11-12 05:28:22 +01001202/**
1203 * ablkcipher_request_set_crypt() - set data buffers
1204 * @req: request handle
1205 * @src: source scatter / gather list
1206 * @dst: destination scatter / gather list
1207 * @nbytes: number of bytes to process from @src
1208 * @iv: IV for the cipher operation which must comply with the IV size defined
1209 * by crypto_ablkcipher_ivsize
1210 *
1211 * This function allows setting of the source data and destination data
1212 * scatter / gather lists.
1213 *
1214 * For encryption, the source is treated as the plaintext and the
1215 * destination is the ciphertext. For a decryption operation, the use is
Stephan Mueller379dcfb2015-01-19 00:13:39 +01001216 * reversed - the source is the ciphertext and the destination is the plaintext.
Stephan Muellerf13ec332014-11-12 05:28:22 +01001217 */
Herbert Xu32e3983f2007-03-24 14:35:34 +11001218static inline void ablkcipher_request_set_crypt(
1219 struct ablkcipher_request *req,
1220 struct scatterlist *src, struct scatterlist *dst,
1221 unsigned int nbytes, void *iv)
1222{
1223 req->src = src;
1224 req->dst = dst;
1225 req->nbytes = nbytes;
1226 req->info = iv;
1227}
1228
Stephan Muellerfced7b02014-11-12 05:29:00 +01001229/**
Stephan Mueller58284f02014-11-12 05:29:36 +01001230 * DOC: Synchronous Block Cipher API
1231 *
1232 * The synchronous block cipher API is used with the ciphers of type
1233 * CRYPTO_ALG_TYPE_BLKCIPHER (listed as type "blkcipher" in /proc/crypto)
1234 *
1235 * Synchronous calls, have a context in the tfm. But since a single tfm can be
1236 * used in multiple calls and in parallel, this info should not be changeable
1237 * (unless a lock is used). This applies, for example, to the symmetric key.
1238 * However, the IV is changeable, so there is an iv field in blkcipher_tfm
1239 * structure for synchronous blkcipher api. So, its the only state info that can
1240 * be kept for synchronous calls without using a big lock across a tfm.
1241 *
1242 * The block cipher API allows the use of a complete cipher, i.e. a cipher
1243 * consisting of a template (a block chaining mode) and a single block cipher
1244 * primitive (e.g. AES).
1245 *
1246 * The plaintext data buffer and the ciphertext data buffer are pointed to
1247 * by using scatter/gather lists. The cipher operation is performed
1248 * on all segments of the provided scatter/gather lists.
1249 *
1250 * The kernel crypto API supports a cipher operation "in-place" which means that
1251 * the caller may provide the same scatter/gather list for the plaintext and
1252 * cipher text. After the completion of the cipher operation, the plaintext
1253 * data is replaced with the ciphertext data in case of an encryption and vice
1254 * versa for a decryption. The caller must ensure that the scatter/gather lists
1255 * for the output data point to sufficiently large buffers, i.e. multiples of
1256 * the block size of the cipher.
1257 */
1258
Herbert Xu5cde0af2006-08-22 00:07:53 +10001259static inline struct crypto_blkcipher *__crypto_blkcipher_cast(
1260 struct crypto_tfm *tfm)
1261{
1262 return (struct crypto_blkcipher *)tfm;
1263}
1264
1265static inline struct crypto_blkcipher *crypto_blkcipher_cast(
1266 struct crypto_tfm *tfm)
1267{
1268 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_BLKCIPHER);
1269 return __crypto_blkcipher_cast(tfm);
1270}
1271
Stephan Mueller58284f02014-11-12 05:29:36 +01001272/**
1273 * crypto_alloc_blkcipher() - allocate synchronous block cipher handle
1274 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1275 * blkcipher cipher
1276 * @type: specifies the type of the cipher
1277 * @mask: specifies the mask for the cipher
1278 *
1279 * Allocate a cipher handle for a block cipher. The returned struct
1280 * crypto_blkcipher is the cipher handle that is required for any subsequent
1281 * API invocation for that block cipher.
1282 *
1283 * Return: allocated cipher handle in case of success; IS_ERR() is true in case
1284 * of an error, PTR_ERR() returns the error code.
1285 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001286static inline struct crypto_blkcipher *crypto_alloc_blkcipher(
1287 const char *alg_name, u32 type, u32 mask)
1288{
Herbert Xu332f88402007-11-15 22:36:07 +08001289 type &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xu5cde0af2006-08-22 00:07:53 +10001290 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
Herbert Xu332f88402007-11-15 22:36:07 +08001291 mask |= CRYPTO_ALG_TYPE_MASK;
Herbert Xu5cde0af2006-08-22 00:07:53 +10001292
1293 return __crypto_blkcipher_cast(crypto_alloc_base(alg_name, type, mask));
1294}
1295
1296static inline struct crypto_tfm *crypto_blkcipher_tfm(
1297 struct crypto_blkcipher *tfm)
1298{
1299 return &tfm->base;
1300}
1301
Stephan Mueller58284f02014-11-12 05:29:36 +01001302/**
1303 * crypto_free_blkcipher() - zeroize and free the block cipher handle
1304 * @tfm: cipher handle to be freed
1305 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001306static inline void crypto_free_blkcipher(struct crypto_blkcipher *tfm)
1307{
1308 crypto_free_tfm(crypto_blkcipher_tfm(tfm));
1309}
1310
Stephan Mueller58284f02014-11-12 05:29:36 +01001311/**
1312 * crypto_has_blkcipher() - Search for the availability of a block cipher
1313 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1314 * block cipher
1315 * @type: specifies the type of the cipher
1316 * @mask: specifies the mask for the cipher
1317 *
1318 * Return: true when the block cipher is known to the kernel crypto API; false
1319 * otherwise
1320 */
Herbert Xufce32d72006-08-26 17:35:45 +10001321static inline int crypto_has_blkcipher(const char *alg_name, u32 type, u32 mask)
1322{
Herbert Xu332f88402007-11-15 22:36:07 +08001323 type &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xufce32d72006-08-26 17:35:45 +10001324 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
Herbert Xu332f88402007-11-15 22:36:07 +08001325 mask |= CRYPTO_ALG_TYPE_MASK;
Herbert Xufce32d72006-08-26 17:35:45 +10001326
1327 return crypto_has_alg(alg_name, type, mask);
1328}
1329
Stephan Mueller58284f02014-11-12 05:29:36 +01001330/**
1331 * crypto_blkcipher_name() - return the name / cra_name from the cipher handle
1332 * @tfm: cipher handle
1333 *
1334 * Return: The character string holding the name of the cipher
1335 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001336static inline const char *crypto_blkcipher_name(struct crypto_blkcipher *tfm)
1337{
1338 return crypto_tfm_alg_name(crypto_blkcipher_tfm(tfm));
1339}
1340
1341static inline struct blkcipher_tfm *crypto_blkcipher_crt(
1342 struct crypto_blkcipher *tfm)
1343{
1344 return &crypto_blkcipher_tfm(tfm)->crt_blkcipher;
1345}
1346
1347static inline struct blkcipher_alg *crypto_blkcipher_alg(
1348 struct crypto_blkcipher *tfm)
1349{
1350 return &crypto_blkcipher_tfm(tfm)->__crt_alg->cra_blkcipher;
1351}
1352
Stephan Mueller58284f02014-11-12 05:29:36 +01001353/**
1354 * crypto_blkcipher_ivsize() - obtain IV size
1355 * @tfm: cipher handle
1356 *
1357 * The size of the IV for the block cipher referenced by the cipher handle is
1358 * returned. This IV size may be zero if the cipher does not need an IV.
1359 *
1360 * Return: IV size in bytes
1361 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001362static inline unsigned int crypto_blkcipher_ivsize(struct crypto_blkcipher *tfm)
1363{
1364 return crypto_blkcipher_alg(tfm)->ivsize;
1365}
1366
Stephan Mueller58284f02014-11-12 05:29:36 +01001367/**
1368 * crypto_blkcipher_blocksize() - obtain block size of cipher
1369 * @tfm: cipher handle
1370 *
1371 * The block size for the block cipher referenced with the cipher handle is
1372 * returned. The caller may use that information to allocate appropriate
1373 * memory for the data returned by the encryption or decryption operation.
1374 *
1375 * Return: block size of cipher
1376 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001377static inline unsigned int crypto_blkcipher_blocksize(
1378 struct crypto_blkcipher *tfm)
1379{
1380 return crypto_tfm_alg_blocksize(crypto_blkcipher_tfm(tfm));
1381}
1382
1383static inline unsigned int crypto_blkcipher_alignmask(
1384 struct crypto_blkcipher *tfm)
1385{
1386 return crypto_tfm_alg_alignmask(crypto_blkcipher_tfm(tfm));
1387}
1388
1389static inline u32 crypto_blkcipher_get_flags(struct crypto_blkcipher *tfm)
1390{
1391 return crypto_tfm_get_flags(crypto_blkcipher_tfm(tfm));
1392}
1393
1394static inline void crypto_blkcipher_set_flags(struct crypto_blkcipher *tfm,
1395 u32 flags)
1396{
1397 crypto_tfm_set_flags(crypto_blkcipher_tfm(tfm), flags);
1398}
1399
1400static inline void crypto_blkcipher_clear_flags(struct crypto_blkcipher *tfm,
1401 u32 flags)
1402{
1403 crypto_tfm_clear_flags(crypto_blkcipher_tfm(tfm), flags);
1404}
1405
Stephan Mueller58284f02014-11-12 05:29:36 +01001406/**
1407 * crypto_blkcipher_setkey() - set key for cipher
1408 * @tfm: cipher handle
1409 * @key: buffer holding the key
1410 * @keylen: length of the key in bytes
1411 *
1412 * The caller provided key is set for the block cipher referenced by the cipher
1413 * handle.
1414 *
1415 * Note, the key length determines the cipher type. Many block ciphers implement
1416 * different cipher modes depending on the key size, such as AES-128 vs AES-192
1417 * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
1418 * is performed.
1419 *
1420 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
1421 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001422static inline int crypto_blkcipher_setkey(struct crypto_blkcipher *tfm,
1423 const u8 *key, unsigned int keylen)
1424{
1425 return crypto_blkcipher_crt(tfm)->setkey(crypto_blkcipher_tfm(tfm),
1426 key, keylen);
1427}
1428
Stephan Mueller58284f02014-11-12 05:29:36 +01001429/**
1430 * crypto_blkcipher_encrypt() - encrypt plaintext
1431 * @desc: reference to the block cipher handle with meta data
1432 * @dst: scatter/gather list that is filled by the cipher operation with the
1433 * ciphertext
1434 * @src: scatter/gather list that holds the plaintext
1435 * @nbytes: number of bytes of the plaintext to encrypt.
1436 *
1437 * Encrypt plaintext data using the IV set by the caller with a preceding
1438 * call of crypto_blkcipher_set_iv.
1439 *
1440 * The blkcipher_desc data structure must be filled by the caller and can
1441 * reside on the stack. The caller must fill desc as follows: desc.tfm is filled
1442 * with the block cipher handle; desc.flags is filled with either
1443 * CRYPTO_TFM_REQ_MAY_SLEEP or 0.
1444 *
1445 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1446 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001447static inline int crypto_blkcipher_encrypt(struct blkcipher_desc *desc,
1448 struct scatterlist *dst,
1449 struct scatterlist *src,
1450 unsigned int nbytes)
1451{
1452 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
1453 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
1454}
1455
Stephan Mueller58284f02014-11-12 05:29:36 +01001456/**
1457 * crypto_blkcipher_encrypt_iv() - encrypt plaintext with dedicated IV
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 with the use of an IV that is solely used for this
1465 * cipher operation. Any previously set IV is not used.
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.info is filled with the IV to be used for
1470 * the current operation; desc.flags is filled with either
1471 * CRYPTO_TFM_REQ_MAY_SLEEP or 0.
1472 *
1473 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1474 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001475static inline int crypto_blkcipher_encrypt_iv(struct blkcipher_desc *desc,
1476 struct scatterlist *dst,
1477 struct scatterlist *src,
1478 unsigned int nbytes)
1479{
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_decrypt() - decrypt ciphertext
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 * plaintext
1488 * @src: scatter/gather list that holds the ciphertext
1489 * @nbytes: number of bytes of the ciphertext to decrypt.
1490 *
1491 * Decrypt ciphertext data using the IV set by the caller with a preceding
1492 * call of crypto_blkcipher_set_iv.
1493 *
1494 * The blkcipher_desc data structure must be filled by the caller as documented
1495 * for the crypto_blkcipher_encrypt call above.
1496 *
1497 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1498 *
1499 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001500static inline int crypto_blkcipher_decrypt(struct blkcipher_desc *desc,
1501 struct scatterlist *dst,
1502 struct scatterlist *src,
1503 unsigned int nbytes)
1504{
1505 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
1506 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
1507}
1508
Stephan Mueller58284f02014-11-12 05:29:36 +01001509/**
1510 * crypto_blkcipher_decrypt_iv() - decrypt ciphertext with dedicated IV
1511 * @desc: reference to the block cipher handle with meta data
1512 * @dst: scatter/gather list that is filled by the cipher operation with the
1513 * plaintext
1514 * @src: scatter/gather list that holds the ciphertext
1515 * @nbytes: number of bytes of the ciphertext to decrypt.
1516 *
1517 * Decrypt ciphertext data with the use of an IV that is solely used for this
1518 * cipher operation. Any previously set IV is not used.
1519 *
1520 * The blkcipher_desc data structure must be filled by the caller as documented
1521 * for the crypto_blkcipher_encrypt_iv call above.
1522 *
1523 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1524 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001525static inline int crypto_blkcipher_decrypt_iv(struct blkcipher_desc *desc,
1526 struct scatterlist *dst,
1527 struct scatterlist *src,
1528 unsigned int nbytes)
1529{
1530 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
1531}
1532
Stephan Mueller58284f02014-11-12 05:29:36 +01001533/**
1534 * crypto_blkcipher_set_iv() - set IV for cipher
1535 * @tfm: cipher handle
1536 * @src: buffer holding the IV
1537 * @len: length of the IV in bytes
1538 *
1539 * The caller provided IV is set for the block cipher referenced by the cipher
1540 * handle.
1541 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001542static inline void crypto_blkcipher_set_iv(struct crypto_blkcipher *tfm,
1543 const u8 *src, unsigned int len)
1544{
1545 memcpy(crypto_blkcipher_crt(tfm)->iv, src, len);
1546}
1547
Stephan Mueller58284f02014-11-12 05:29:36 +01001548/**
1549 * crypto_blkcipher_get_iv() - obtain IV from cipher
1550 * @tfm: cipher handle
1551 * @dst: buffer filled with the IV
1552 * @len: length of the buffer dst
1553 *
1554 * The caller can obtain the IV set for the block cipher referenced by the
1555 * cipher handle and store it into the user-provided buffer. If the buffer
1556 * has an insufficient space, the IV is truncated to fit the buffer.
1557 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001558static inline void crypto_blkcipher_get_iv(struct crypto_blkcipher *tfm,
1559 u8 *dst, unsigned int len)
1560{
1561 memcpy(dst, crypto_blkcipher_crt(tfm)->iv, len);
1562}
1563
Stephan Mueller16e61032014-11-12 05:30:06 +01001564/**
1565 * DOC: Single Block Cipher API
1566 *
1567 * The single block cipher API is used with the ciphers of type
1568 * CRYPTO_ALG_TYPE_CIPHER (listed as type "cipher" in /proc/crypto).
1569 *
1570 * Using the single block cipher API calls, operations with the basic cipher
1571 * primitive can be implemented. These cipher primitives exclude any block
1572 * chaining operations including IV handling.
1573 *
1574 * The purpose of this single block cipher API is to support the implementation
1575 * of templates or other concepts that only need to perform the cipher operation
1576 * on one block at a time. Templates invoke the underlying cipher primitive
1577 * block-wise and process either the input or the output data of these cipher
1578 * operations.
1579 */
1580
Herbert Xuf28776a2006-08-13 20:58:18 +10001581static inline struct crypto_cipher *__crypto_cipher_cast(struct crypto_tfm *tfm)
1582{
1583 return (struct crypto_cipher *)tfm;
1584}
1585
1586static inline struct crypto_cipher *crypto_cipher_cast(struct crypto_tfm *tfm)
1587{
1588 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
1589 return __crypto_cipher_cast(tfm);
1590}
1591
Stephan Mueller16e61032014-11-12 05:30:06 +01001592/**
1593 * crypto_alloc_cipher() - allocate single block cipher handle
1594 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1595 * single block cipher
1596 * @type: specifies the type of the cipher
1597 * @mask: specifies the mask for the cipher
1598 *
1599 * Allocate a cipher handle for a single block cipher. The returned struct
1600 * crypto_cipher is the cipher handle that is required for any subsequent API
1601 * invocation for that single block cipher.
1602 *
1603 * Return: allocated cipher handle in case of success; IS_ERR() is true in case
1604 * of an error, PTR_ERR() returns the error code.
1605 */
Herbert Xuf28776a2006-08-13 20:58:18 +10001606static inline struct crypto_cipher *crypto_alloc_cipher(const char *alg_name,
1607 u32 type, u32 mask)
1608{
1609 type &= ~CRYPTO_ALG_TYPE_MASK;
1610 type |= CRYPTO_ALG_TYPE_CIPHER;
1611 mask |= CRYPTO_ALG_TYPE_MASK;
1612
1613 return __crypto_cipher_cast(crypto_alloc_base(alg_name, type, mask));
1614}
1615
1616static inline struct crypto_tfm *crypto_cipher_tfm(struct crypto_cipher *tfm)
1617{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001618 return &tfm->base;
Herbert Xuf28776a2006-08-13 20:58:18 +10001619}
1620
Stephan Mueller16e61032014-11-12 05:30:06 +01001621/**
1622 * crypto_free_cipher() - zeroize and free the single block cipher handle
1623 * @tfm: cipher handle to be freed
1624 */
Herbert Xuf28776a2006-08-13 20:58:18 +10001625static inline void crypto_free_cipher(struct crypto_cipher *tfm)
1626{
1627 crypto_free_tfm(crypto_cipher_tfm(tfm));
1628}
1629
Stephan Mueller16e61032014-11-12 05:30:06 +01001630/**
1631 * crypto_has_cipher() - Search for the availability of a single block cipher
1632 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1633 * single block cipher
1634 * @type: specifies the type of the cipher
1635 * @mask: specifies the mask for the cipher
1636 *
1637 * Return: true when the single block cipher is known to the kernel crypto API;
1638 * false otherwise
1639 */
Herbert Xufce32d72006-08-26 17:35:45 +10001640static inline int crypto_has_cipher(const char *alg_name, u32 type, u32 mask)
1641{
1642 type &= ~CRYPTO_ALG_TYPE_MASK;
1643 type |= CRYPTO_ALG_TYPE_CIPHER;
1644 mask |= CRYPTO_ALG_TYPE_MASK;
1645
1646 return crypto_has_alg(alg_name, type, mask);
1647}
1648
Herbert Xuf28776a2006-08-13 20:58:18 +10001649static inline struct cipher_tfm *crypto_cipher_crt(struct crypto_cipher *tfm)
1650{
1651 return &crypto_cipher_tfm(tfm)->crt_cipher;
1652}
1653
Stephan Mueller16e61032014-11-12 05:30:06 +01001654/**
1655 * crypto_cipher_blocksize() - obtain block size for cipher
1656 * @tfm: cipher handle
1657 *
1658 * The block size for the single block cipher referenced with the cipher handle
1659 * tfm is returned. The caller may use that information to allocate appropriate
1660 * memory for the data returned by the encryption or decryption operation
1661 *
1662 * Return: block size of cipher
1663 */
Herbert Xuf28776a2006-08-13 20:58:18 +10001664static inline unsigned int crypto_cipher_blocksize(struct crypto_cipher *tfm)
1665{
1666 return crypto_tfm_alg_blocksize(crypto_cipher_tfm(tfm));
1667}
1668
1669static inline unsigned int crypto_cipher_alignmask(struct crypto_cipher *tfm)
1670{
1671 return crypto_tfm_alg_alignmask(crypto_cipher_tfm(tfm));
1672}
1673
1674static inline u32 crypto_cipher_get_flags(struct crypto_cipher *tfm)
1675{
1676 return crypto_tfm_get_flags(crypto_cipher_tfm(tfm));
1677}
1678
1679static inline void crypto_cipher_set_flags(struct crypto_cipher *tfm,
1680 u32 flags)
1681{
1682 crypto_tfm_set_flags(crypto_cipher_tfm(tfm), flags);
1683}
1684
1685static inline void crypto_cipher_clear_flags(struct crypto_cipher *tfm,
1686 u32 flags)
1687{
1688 crypto_tfm_clear_flags(crypto_cipher_tfm(tfm), flags);
1689}
1690
Stephan Mueller16e61032014-11-12 05:30:06 +01001691/**
1692 * crypto_cipher_setkey() - set key for cipher
1693 * @tfm: cipher handle
1694 * @key: buffer holding the key
1695 * @keylen: length of the key in bytes
1696 *
1697 * The caller provided key is set for the single block cipher referenced by the
1698 * cipher handle.
1699 *
1700 * Note, the key length determines the cipher type. Many block ciphers implement
1701 * different cipher modes depending on the key size, such as AES-128 vs AES-192
1702 * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
1703 * is performed.
1704 *
1705 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
1706 */
Herbert Xu7226bc872006-08-21 21:40:49 +10001707static inline int crypto_cipher_setkey(struct crypto_cipher *tfm,
1708 const u8 *key, unsigned int keylen)
1709{
1710 return crypto_cipher_crt(tfm)->cit_setkey(crypto_cipher_tfm(tfm),
1711 key, keylen);
1712}
1713
Stephan Mueller16e61032014-11-12 05:30:06 +01001714/**
1715 * crypto_cipher_encrypt_one() - encrypt one block of plaintext
1716 * @tfm: cipher handle
1717 * @dst: points to the buffer that will be filled with the ciphertext
1718 * @src: buffer holding the plaintext to be encrypted
1719 *
1720 * Invoke the encryption operation of one block. The caller must ensure that
1721 * the plaintext and ciphertext buffers are at least one block in size.
1722 */
Herbert Xuf28776a2006-08-13 20:58:18 +10001723static inline void crypto_cipher_encrypt_one(struct crypto_cipher *tfm,
1724 u8 *dst, const u8 *src)
1725{
1726 crypto_cipher_crt(tfm)->cit_encrypt_one(crypto_cipher_tfm(tfm),
1727 dst, src);
1728}
1729
Stephan Mueller16e61032014-11-12 05:30:06 +01001730/**
1731 * crypto_cipher_decrypt_one() - decrypt one block of ciphertext
1732 * @tfm: cipher handle
1733 * @dst: points to the buffer that will be filled with the plaintext
1734 * @src: buffer holding the ciphertext to be decrypted
1735 *
1736 * Invoke the decryption operation of one block. The caller must ensure that
1737 * the plaintext and ciphertext buffers are at least one block in size.
1738 */
Herbert Xuf28776a2006-08-13 20:58:18 +10001739static inline void crypto_cipher_decrypt_one(struct crypto_cipher *tfm,
1740 u8 *dst, const u8 *src)
1741{
1742 crypto_cipher_crt(tfm)->cit_decrypt_one(crypto_cipher_tfm(tfm),
1743 dst, src);
1744}
1745
Herbert Xufce32d72006-08-26 17:35:45 +10001746static inline struct crypto_comp *__crypto_comp_cast(struct crypto_tfm *tfm)
1747{
1748 return (struct crypto_comp *)tfm;
1749}
1750
1751static inline struct crypto_comp *crypto_comp_cast(struct crypto_tfm *tfm)
1752{
1753 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_COMPRESS) &
1754 CRYPTO_ALG_TYPE_MASK);
1755 return __crypto_comp_cast(tfm);
1756}
1757
1758static inline struct crypto_comp *crypto_alloc_comp(const char *alg_name,
1759 u32 type, u32 mask)
1760{
1761 type &= ~CRYPTO_ALG_TYPE_MASK;
1762 type |= CRYPTO_ALG_TYPE_COMPRESS;
1763 mask |= CRYPTO_ALG_TYPE_MASK;
1764
1765 return __crypto_comp_cast(crypto_alloc_base(alg_name, type, mask));
1766}
1767
1768static inline struct crypto_tfm *crypto_comp_tfm(struct crypto_comp *tfm)
1769{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001770 return &tfm->base;
Herbert Xufce32d72006-08-26 17:35:45 +10001771}
1772
1773static inline void crypto_free_comp(struct crypto_comp *tfm)
1774{
1775 crypto_free_tfm(crypto_comp_tfm(tfm));
1776}
1777
1778static inline int crypto_has_comp(const char *alg_name, u32 type, u32 mask)
1779{
1780 type &= ~CRYPTO_ALG_TYPE_MASK;
1781 type |= CRYPTO_ALG_TYPE_COMPRESS;
1782 mask |= CRYPTO_ALG_TYPE_MASK;
1783
1784 return crypto_has_alg(alg_name, type, mask);
1785}
1786
Herbert Xue4d5b792006-08-26 18:12:40 +10001787static inline const char *crypto_comp_name(struct crypto_comp *tfm)
1788{
1789 return crypto_tfm_alg_name(crypto_comp_tfm(tfm));
1790}
1791
Herbert Xufce32d72006-08-26 17:35:45 +10001792static inline struct compress_tfm *crypto_comp_crt(struct crypto_comp *tfm)
1793{
1794 return &crypto_comp_tfm(tfm)->crt_compress;
1795}
1796
1797static inline int crypto_comp_compress(struct crypto_comp *tfm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 const u8 *src, unsigned int slen,
1799 u8 *dst, unsigned int *dlen)
1800{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001801 return crypto_comp_crt(tfm)->cot_compress(crypto_comp_tfm(tfm),
1802 src, slen, dst, dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803}
1804
Herbert Xufce32d72006-08-26 17:35:45 +10001805static inline int crypto_comp_decompress(struct crypto_comp *tfm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 const u8 *src, unsigned int slen,
1807 u8 *dst, unsigned int *dlen)
1808{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001809 return crypto_comp_crt(tfm)->cot_decompress(crypto_comp_tfm(tfm),
1810 src, slen, dst, dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811}
1812
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813#endif /* _LINUX_CRYPTO_H */
1814