blob: 231e59f90d32c6ad93544513b0695ad5b363de22 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Scatterlist Cryptographic API.
3 *
4 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
5 * Copyright (c) 2002 David S. Miller (davem@redhat.com)
Herbert Xu5cb14542005-11-05 16:58:14 +11006 * Copyright (c) 2005 Herbert Xu <herbert@gondor.apana.org.au>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * Portions derived from Cryptoapi, by Alexander Kjeldaas <astor@fast.no>
John Anthony Kazos Jr18735dd2007-10-19 23:07:36 +02009 * and Nettle, by Niels Möller.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the Free
13 * Software Foundation; either version 2 of the License, or (at your option)
14 * any later version.
15 *
16 */
17#ifndef _LINUX_CRYPTO_H
18#define _LINUX_CRYPTO_H
19
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/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 * Transform masks and values (for crt_flags).
111 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112#define CRYPTO_TFM_REQ_MASK 0x000fff00
113#define CRYPTO_TFM_RES_MASK 0xfff00000
114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115#define CRYPTO_TFM_REQ_WEAK_KEY 0x00000100
Herbert Xu64baf3c2005-09-01 17:43:05 -0700116#define CRYPTO_TFM_REQ_MAY_SLEEP 0x00000200
Herbert Xu32e39832007-03-24 14:35:34 +1100117#define CRYPTO_TFM_REQ_MAY_BACKLOG 0x00000400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118#define CRYPTO_TFM_RES_WEAK_KEY 0x00100000
119#define CRYPTO_TFM_RES_BAD_KEY_LEN 0x00200000
120#define CRYPTO_TFM_RES_BAD_KEY_SCHED 0x00400000
121#define CRYPTO_TFM_RES_BAD_BLOCK_LEN 0x00800000
122#define CRYPTO_TFM_RES_BAD_FLAGS 0x01000000
123
124/*
125 * Miscellaneous stuff.
126 */
Herbert Xuf437a3f2017-04-06 16:16:11 +0800127#define CRYPTO_MAX_ALG_NAME 128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Herbert Xu79911102006-08-21 21:03:52 +1000129/*
130 * The macro CRYPTO_MINALIGN_ATTR (along with the void * type in the actual
131 * declaration) is used to ensure that the crypto_tfm context structure is
132 * aligned correctly for the given architecture so that there are no alignment
133 * faults for C data types. In particular, this is required on platforms such
134 * as arm where pointers are 32-bit aligned but there are data types such as
135 * u64 which require 64-bit alignment.
136 */
Herbert Xu79911102006-08-21 21:03:52 +1000137#define CRYPTO_MINALIGN ARCH_KMALLOC_MINALIGN
Herbert Xu79911102006-08-21 21:03:52 +1000138
Herbert Xu79911102006-08-21 21:03:52 +1000139#define CRYPTO_MINALIGN_ATTR __attribute__ ((__aligned__(CRYPTO_MINALIGN)))
Herbert Xu79911102006-08-21 21:03:52 +1000140
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141struct scatterlist;
Herbert Xu32e39832007-03-24 14:35:34 +1100142struct crypto_ablkcipher;
143struct crypto_async_request;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000144struct crypto_blkcipher;
Herbert Xu40725182005-07-06 13:51:52 -0700145struct crypto_tfm;
Herbert Xue853c3c2006-08-22 00:06:54 +1000146struct crypto_type;
Herbert Xu61da88e2007-12-17 21:51:27 +0800147struct skcipher_givcrypt_request;
Herbert Xu40725182005-07-06 13:51:52 -0700148
Herbert Xu32e39832007-03-24 14:35:34 +1100149typedef void (*crypto_completion_t)(struct crypto_async_request *req, int err);
150
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100151/**
152 * DOC: Block Cipher Context Data Structures
153 *
154 * These data structures define the operating context for each block cipher
155 * type.
156 */
157
Herbert Xu32e39832007-03-24 14:35:34 +1100158struct crypto_async_request {
159 struct list_head list;
160 crypto_completion_t complete;
161 void *data;
162 struct crypto_tfm *tfm;
163
164 u32 flags;
165};
166
167struct ablkcipher_request {
168 struct crypto_async_request base;
169
170 unsigned int nbytes;
171
172 void *info;
173
174 struct scatterlist *src;
175 struct scatterlist *dst;
176
177 void *__ctx[] CRYPTO_MINALIGN_ATTR;
178};
179
Herbert Xu5cde0af2006-08-22 00:07:53 +1000180struct blkcipher_desc {
181 struct crypto_blkcipher *tfm;
182 void *info;
183 u32 flags;
184};
185
Herbert Xu40725182005-07-06 13:51:52 -0700186struct cipher_desc {
187 struct crypto_tfm *tfm;
Herbert Xu6c2bb982006-05-16 22:09:29 +1000188 void (*crfn)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Herbert Xu40725182005-07-06 13:51:52 -0700189 unsigned int (*prfn)(const struct cipher_desc *desc, u8 *dst,
190 const u8 *src, unsigned int nbytes);
191 void *info;
192};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100194/**
195 * DOC: Block Cipher Algorithm Definitions
196 *
197 * These data structures define modular crypto algorithm implementations,
198 * managed via crypto_register_alg() and crypto_unregister_alg().
199 */
200
201/**
202 * struct ablkcipher_alg - asynchronous block cipher definition
203 * @min_keysize: Minimum key size supported by the transformation. This is the
204 * smallest key length supported by this transformation algorithm.
205 * This must be set to one of the pre-defined values as this is
206 * not hardware specific. Possible values for this field can be
207 * found via git grep "_MIN_KEY_SIZE" include/crypto/
208 * @max_keysize: Maximum key size supported by the transformation. This is the
209 * largest key length supported by this transformation algorithm.
210 * This must be set to one of the pre-defined values as this is
211 * not hardware specific. Possible values for this field can be
212 * found via git grep "_MAX_KEY_SIZE" include/crypto/
213 * @setkey: Set key for the transformation. This function is used to either
214 * program a supplied key into the hardware or store the key in the
215 * transformation context for programming it later. Note that this
216 * function does modify the transformation context. This function can
217 * be called multiple times during the existence of the transformation
218 * object, so one must make sure the key is properly reprogrammed into
219 * the hardware. This function is also responsible for checking the key
220 * length for validity. In case a software fallback was put in place in
221 * the @cra_init call, this function might need to use the fallback if
222 * the algorithm doesn't support all of the key sizes.
223 * @encrypt: Encrypt a scatterlist of blocks. This function is used to encrypt
224 * the supplied scatterlist containing the blocks of data. The crypto
225 * API consumer is responsible for aligning the entries of the
226 * scatterlist properly and making sure the chunks are correctly
227 * sized. In case a software fallback was put in place in the
228 * @cra_init call, this function might need to use the fallback if
229 * the algorithm doesn't support all of the key sizes. In case the
230 * key was stored in transformation context, the key might need to be
231 * re-programmed into the hardware in this function. This function
232 * shall not modify the transformation context, as this function may
233 * be called in parallel with the same transformation object.
234 * @decrypt: Decrypt a single block. This is a reverse counterpart to @encrypt
235 * and the conditions are exactly the same.
236 * @givencrypt: Update the IV for encryption. With this function, a cipher
237 * implementation may provide the function on how to update the IV
238 * for encryption.
239 * @givdecrypt: Update the IV for decryption. This is the reverse of
240 * @givencrypt .
241 * @geniv: The transformation implementation may use an "IV generator" provided
242 * by the kernel crypto API. Several use cases have a predefined
243 * approach how IVs are to be updated. For such use cases, the kernel
244 * crypto API provides ready-to-use implementations that can be
245 * referenced with this variable.
246 * @ivsize: IV size applicable for transformation. The consumer must provide an
247 * IV of exactly that size to perform the encrypt or decrypt operation.
248 *
249 * All fields except @givencrypt , @givdecrypt , @geniv and @ivsize are
250 * mandatory and must be filled.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 */
Herbert Xub5b7f082007-04-16 20:48:54 +1000252struct ablkcipher_alg {
253 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
254 unsigned int keylen);
255 int (*encrypt)(struct ablkcipher_request *req);
256 int (*decrypt)(struct ablkcipher_request *req);
Herbert Xu61da88e2007-12-17 21:51:27 +0800257 int (*givencrypt)(struct skcipher_givcrypt_request *req);
258 int (*givdecrypt)(struct skcipher_givcrypt_request *req);
Herbert Xub5b7f082007-04-16 20:48:54 +1000259
Herbert Xu23508e12007-11-27 21:33:24 +0800260 const char *geniv;
261
Herbert Xub5b7f082007-04-16 20:48:54 +1000262 unsigned int min_keysize;
263 unsigned int max_keysize;
264 unsigned int ivsize;
265};
266
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100267/**
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100268 * struct blkcipher_alg - synchronous block cipher definition
269 * @min_keysize: see struct ablkcipher_alg
270 * @max_keysize: see struct ablkcipher_alg
271 * @setkey: see struct ablkcipher_alg
272 * @encrypt: see struct ablkcipher_alg
273 * @decrypt: see struct ablkcipher_alg
274 * @geniv: see struct ablkcipher_alg
275 * @ivsize: see struct ablkcipher_alg
276 *
277 * All fields except @geniv and @ivsize are mandatory and must be filled.
278 */
Herbert Xu5cde0af2006-08-22 00:07:53 +1000279struct blkcipher_alg {
280 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
281 unsigned int keylen);
282 int (*encrypt)(struct blkcipher_desc *desc,
283 struct scatterlist *dst, struct scatterlist *src,
284 unsigned int nbytes);
285 int (*decrypt)(struct blkcipher_desc *desc,
286 struct scatterlist *dst, struct scatterlist *src,
287 unsigned int nbytes);
288
Herbert Xu23508e12007-11-27 21:33:24 +0800289 const char *geniv;
290
Herbert Xu5cde0af2006-08-22 00:07:53 +1000291 unsigned int min_keysize;
292 unsigned int max_keysize;
293 unsigned int ivsize;
294};
295
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100296/**
297 * struct cipher_alg - single-block symmetric ciphers definition
298 * @cia_min_keysize: Minimum key size supported by the transformation. This is
299 * the smallest key length supported by this transformation
300 * algorithm. This must be set to one of the pre-defined
301 * values as this is not hardware specific. Possible values
302 * for this field can be found via git grep "_MIN_KEY_SIZE"
303 * include/crypto/
304 * @cia_max_keysize: Maximum key size supported by the transformation. This is
305 * the largest key length supported by this transformation
306 * algorithm. This must be set to one of the pre-defined values
307 * as this is not hardware specific. Possible values for this
308 * field can be found via git grep "_MAX_KEY_SIZE"
309 * include/crypto/
310 * @cia_setkey: Set key for the transformation. This function is used to either
311 * program a supplied key into the hardware or store the key in the
312 * transformation context for programming it later. Note that this
313 * function does modify the transformation context. This function
314 * can be called multiple times during the existence of the
315 * transformation object, so one must make sure the key is properly
316 * reprogrammed into the hardware. This function is also
317 * responsible for checking the key length for validity.
318 * @cia_encrypt: Encrypt a single block. This function is used to encrypt a
319 * single block of data, which must be @cra_blocksize big. This
320 * always operates on a full @cra_blocksize and it is not possible
321 * to encrypt a block of smaller size. The supplied buffers must
322 * therefore also be at least of @cra_blocksize size. Both the
323 * input and output buffers are always aligned to @cra_alignmask.
324 * In case either of the input or output buffer supplied by user
325 * of the crypto API is not aligned to @cra_alignmask, the crypto
326 * API will re-align the buffers. The re-alignment means that a
327 * new buffer will be allocated, the data will be copied into the
328 * new buffer, then the processing will happen on the new buffer,
329 * then the data will be copied back into the original buffer and
330 * finally the new buffer will be freed. In case a software
331 * fallback was put in place in the @cra_init call, this function
332 * might need to use the fallback if the algorithm doesn't support
333 * all of the key sizes. In case the key was stored in
334 * transformation context, the key might need to be re-programmed
335 * into the hardware in this function. This function shall not
336 * modify the transformation context, as this function may be
337 * called in parallel with the same transformation object.
338 * @cia_decrypt: Decrypt a single block. This is a reverse counterpart to
339 * @cia_encrypt, and the conditions are exactly the same.
340 *
341 * All fields are mandatory and must be filled.
342 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343struct cipher_alg {
344 unsigned int cia_min_keysize;
345 unsigned int cia_max_keysize;
Herbert Xu6c2bb982006-05-16 22:09:29 +1000346 int (*cia_setkey)(struct crypto_tfm *tfm, const u8 *key,
Herbert Xu560c06a2006-08-13 14:16:39 +1000347 unsigned int keylen);
Herbert Xu6c2bb982006-05-16 22:09:29 +1000348 void (*cia_encrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
349 void (*cia_decrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350};
351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352struct compress_alg {
Herbert Xu6c2bb982006-05-16 22:09:29 +1000353 int (*coa_compress)(struct crypto_tfm *tfm, const u8 *src,
354 unsigned int slen, u8 *dst, unsigned int *dlen);
355 int (*coa_decompress)(struct crypto_tfm *tfm, const u8 *src,
356 unsigned int slen, u8 *dst, unsigned int *dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357};
358
Neil Horman17f0f4a2008-08-14 22:15:52 +1000359
Herbert Xub5b7f082007-04-16 20:48:54 +1000360#define cra_ablkcipher cra_u.ablkcipher
Herbert Xu5cde0af2006-08-22 00:07:53 +1000361#define cra_blkcipher cra_u.blkcipher
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362#define cra_cipher cra_u.cipher
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363#define cra_compress cra_u.compress
364
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100365/**
366 * struct crypto_alg - definition of a cryptograpic cipher algorithm
367 * @cra_flags: Flags describing this transformation. See include/linux/crypto.h
368 * CRYPTO_ALG_* flags for the flags which go in here. Those are
369 * used for fine-tuning the description of the transformation
370 * algorithm.
371 * @cra_blocksize: Minimum block size of this transformation. The size in bytes
372 * of the smallest possible unit which can be transformed with
373 * this algorithm. The users must respect this value.
374 * In case of HASH transformation, it is possible for a smaller
375 * block than @cra_blocksize to be passed to the crypto API for
376 * transformation, in case of any other transformation type, an
377 * error will be returned upon any attempt to transform smaller
378 * than @cra_blocksize chunks.
379 * @cra_ctxsize: Size of the operational context of the transformation. This
380 * value informs the kernel crypto API about the memory size
381 * needed to be allocated for the transformation context.
382 * @cra_alignmask: Alignment mask for the input and output data buffer. The data
383 * buffer containing the input data for the algorithm must be
384 * aligned to this alignment mask. The data buffer for the
385 * output data must be aligned to this alignment mask. Note that
386 * the Crypto API will do the re-alignment in software, but
387 * only under special conditions and there is a performance hit.
388 * The re-alignment happens at these occasions for different
389 * @cra_u types: cipher -- For both input data and output data
390 * buffer; ahash -- For output hash destination buf; shash --
391 * For output hash destination buf.
392 * This is needed on hardware which is flawed by design and
393 * cannot pick data from arbitrary addresses.
394 * @cra_priority: Priority of this transformation implementation. In case
395 * multiple transformations with same @cra_name are available to
396 * the Crypto API, the kernel will use the one with highest
397 * @cra_priority.
398 * @cra_name: Generic name (usable by multiple implementations) of the
399 * transformation algorithm. This is the name of the transformation
400 * itself. This field is used by the kernel when looking up the
401 * providers of particular transformation.
402 * @cra_driver_name: Unique name of the transformation provider. This is the
403 * name of the provider of the transformation. This can be any
404 * arbitrary value, but in the usual case, this contains the
405 * name of the chip or provider and the name of the
406 * transformation algorithm.
407 * @cra_type: Type of the cryptographic transformation. This is a pointer to
408 * struct crypto_type, which implements callbacks common for all
Masanari Iida12f7c142015-06-04 00:01:21 +0900409 * transformation types. There are multiple options:
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100410 * &crypto_blkcipher_type, &crypto_ablkcipher_type,
Herbert Xub0d955b2015-08-14 15:30:41 +0800411 * &crypto_ahash_type, &crypto_rng_type.
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100412 * This field might be empty. In that case, there are no common
413 * callbacks. This is the case for: cipher, compress, shash.
414 * @cra_u: Callbacks implementing the transformation. This is a union of
415 * multiple structures. Depending on the type of transformation selected
416 * by @cra_type and @cra_flags above, the associated structure must be
417 * filled with callbacks. This field might be empty. This is the case
418 * for ahash, shash.
419 * @cra_init: Initialize the cryptographic transformation object. This function
420 * is used to initialize the cryptographic transformation object.
421 * This function is called only once at the instantiation time, right
422 * after the transformation context was allocated. In case the
423 * cryptographic hardware has some special requirements which need to
424 * be handled by software, this function shall check for the precise
425 * requirement of the transformation and put any software fallbacks
426 * in place.
427 * @cra_exit: Deinitialize the cryptographic transformation object. This is a
428 * counterpart to @cra_init, used to remove various changes set in
429 * @cra_init.
430 * @cra_module: Owner of this transformation implementation. Set to THIS_MODULE
431 * @cra_list: internally used
432 * @cra_users: internally used
433 * @cra_refcnt: internally used
434 * @cra_destroy: internally used
435 *
436 * The struct crypto_alg describes a generic Crypto API algorithm and is common
437 * for all of the transformations. Any variable not documented here shall not
438 * be used by a cipher implementation as it is internal to the Crypto API.
439 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440struct crypto_alg {
441 struct list_head cra_list;
Herbert Xu6bfd4802006-09-21 11:39:29 +1000442 struct list_head cra_users;
443
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 u32 cra_flags;
445 unsigned int cra_blocksize;
446 unsigned int cra_ctxsize;
Herbert Xu95477372005-07-06 13:52:09 -0700447 unsigned int cra_alignmask;
Herbert Xu5cb14542005-11-05 16:58:14 +1100448
449 int cra_priority;
Eric Biggersce8614a2017-12-29 10:00:46 -0600450 refcount_t cra_refcnt;
Herbert Xu5cb14542005-11-05 16:58:14 +1100451
Herbert Xud913ea02006-05-21 08:45:26 +1000452 char cra_name[CRYPTO_MAX_ALG_NAME];
453 char cra_driver_name[CRYPTO_MAX_ALG_NAME];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
Herbert Xue853c3c2006-08-22 00:06:54 +1000455 const struct crypto_type *cra_type;
456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 union {
Herbert Xub5b7f082007-04-16 20:48:54 +1000458 struct ablkcipher_alg ablkcipher;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000459 struct blkcipher_alg blkcipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 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;
Herbert Xuedf18b92015-06-18 14:00:48 +0800469} CRYPTO_MINALIGN_ATTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
471/*
Gilad Ben-Yossefada69a12017-10-18 08:00:38 +0100472 * A helper struct for waiting for completion of async crypto ops
473 */
474struct crypto_wait {
475 struct completion completion;
476 int err;
477};
478
479/*
480 * Macro for declaring a crypto op async wait object on stack
481 */
482#define DECLARE_CRYPTO_WAIT(_wait) \
483 struct crypto_wait _wait = { \
484 COMPLETION_INITIALIZER_ONSTACK((_wait).completion), 0 }
485
486/*
487 * Async ops completion helper functioons
488 */
489void crypto_req_done(struct crypto_async_request *req, int err);
490
491static inline int crypto_wait_req(int err, struct crypto_wait *wait)
492{
493 switch (err) {
494 case -EINPROGRESS:
495 case -EBUSY:
496 wait_for_completion(&wait->completion);
497 reinit_completion(&wait->completion);
498 err = wait->err;
499 break;
500 };
501
502 return err;
503}
504
505static inline void crypto_init_wait(struct crypto_wait *wait)
506{
507 init_completion(&wait->completion);
508}
509
510/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 * Algorithm registration interface.
512 */
513int crypto_register_alg(struct crypto_alg *alg);
514int crypto_unregister_alg(struct crypto_alg *alg);
Mark Brown4b004342012-01-17 23:34:26 +0000515int crypto_register_algs(struct crypto_alg *algs, int count);
516int crypto_unregister_algs(struct crypto_alg *algs, int count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
518/*
519 * Algorithm query interface.
520 */
Herbert Xufce32d72006-08-26 17:35:45 +1000521int crypto_has_alg(const char *name, u32 type, u32 mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
523/*
524 * Transforms: user-instantiated objects which encapsulate algorithms
Herbert Xu6d7d6842006-07-30 11:53:01 +1000525 * and core processing logic. Managed via crypto_alloc_*() and
526 * crypto_free_*(), as well as the various helpers below.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
Herbert Xu32e39832007-03-24 14:35:34 +1100529struct ablkcipher_tfm {
530 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
531 unsigned int keylen);
532 int (*encrypt)(struct ablkcipher_request *req);
533 int (*decrypt)(struct ablkcipher_request *req);
Herbert Xu61da88e2007-12-17 21:51:27 +0800534
Herbert Xuecfc4322007-12-05 21:08:36 +1100535 struct crypto_ablkcipher *base;
536
Herbert Xu32e39832007-03-24 14:35:34 +1100537 unsigned int ivsize;
538 unsigned int reqsize;
539};
540
Herbert Xu5cde0af2006-08-22 00:07:53 +1000541struct blkcipher_tfm {
542 void *iv;
543 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
544 unsigned int keylen);
545 int (*encrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
546 struct scatterlist *src, unsigned int nbytes);
547 int (*decrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
548 struct scatterlist *src, unsigned int nbytes);
549};
550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551struct cipher_tfm {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 int (*cit_setkey)(struct crypto_tfm *tfm,
553 const u8 *key, unsigned int keylen);
Herbert Xuf28776a2006-08-13 20:58:18 +1000554 void (*cit_encrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
555 void (*cit_decrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556};
557
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558struct compress_tfm {
559 int (*cot_compress)(struct crypto_tfm *tfm,
560 const u8 *src, unsigned int slen,
561 u8 *dst, unsigned int *dlen);
562 int (*cot_decompress)(struct crypto_tfm *tfm,
563 const u8 *src, unsigned int slen,
564 u8 *dst, unsigned int *dlen);
565};
566
Herbert Xu32e39832007-03-24 14:35:34 +1100567#define crt_ablkcipher crt_u.ablkcipher
Herbert Xu5cde0af2006-08-22 00:07:53 +1000568#define crt_blkcipher crt_u.blkcipher
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569#define crt_cipher crt_u.cipher
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570#define crt_compress crt_u.compress
571
572struct crypto_tfm {
573
574 u32 crt_flags;
575
576 union {
Herbert Xu32e39832007-03-24 14:35:34 +1100577 struct ablkcipher_tfm ablkcipher;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000578 struct blkcipher_tfm blkcipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 struct cipher_tfm cipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 struct compress_tfm compress;
581 } crt_u;
Herbert Xu4a779482008-09-13 18:19:03 -0700582
583 void (*exit)(struct crypto_tfm *tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
585 struct crypto_alg *__crt_alg;
Herbert Xuf10b7892006-01-25 22:34:01 +1100586
Herbert Xu79911102006-08-21 21:03:52 +1000587 void *__crt_ctx[] CRYPTO_MINALIGN_ATTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588};
589
Herbert Xu32e39832007-03-24 14:35:34 +1100590struct crypto_ablkcipher {
591 struct crypto_tfm base;
592};
593
Herbert Xu5cde0af2006-08-22 00:07:53 +1000594struct crypto_blkcipher {
595 struct crypto_tfm base;
596};
597
Herbert Xu78a1fe42006-12-24 10:02:00 +1100598struct crypto_cipher {
599 struct crypto_tfm base;
600};
601
602struct crypto_comp {
603 struct crypto_tfm base;
604};
605
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000606enum {
607 CRYPTOA_UNSPEC,
608 CRYPTOA_ALG,
Herbert Xuebc610e2007-01-01 18:37:02 +1100609 CRYPTOA_TYPE,
Herbert Xu39e1ee012007-08-29 19:27:26 +0800610 CRYPTOA_U32,
Herbert Xuebc610e2007-01-01 18:37:02 +1100611 __CRYPTOA_MAX,
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000612};
613
Herbert Xuebc610e2007-01-01 18:37:02 +1100614#define CRYPTOA_MAX (__CRYPTOA_MAX - 1)
615
Herbert Xu39e1ee012007-08-29 19:27:26 +0800616/* Maximum number of (rtattr) parameters for each template. */
617#define CRYPTO_MAX_ATTRS 32
618
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000619struct crypto_attr_alg {
620 char name[CRYPTO_MAX_ALG_NAME];
621};
622
Herbert Xuebc610e2007-01-01 18:37:02 +1100623struct crypto_attr_type {
624 u32 type;
625 u32 mask;
626};
627
Herbert Xu39e1ee012007-08-29 19:27:26 +0800628struct crypto_attr_u32 {
629 u32 num;
630};
631
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632/*
633 * Transform user interface.
634 */
635
Herbert Xu6d7d6842006-07-30 11:53:01 +1000636struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask);
Herbert Xu7b2cd922009-02-05 16:48:24 +1100637void crypto_destroy_tfm(void *mem, struct crypto_tfm *tfm);
638
639static inline void crypto_free_tfm(struct crypto_tfm *tfm)
640{
641 return crypto_destroy_tfm(tfm, tfm);
642}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
Herbert Xuda7f0332008-07-31 17:08:25 +0800644int alg_test(const char *driver, const char *alg, u32 type, u32 mask);
645
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646/*
647 * Transform helpers which query the underlying algorithm.
648 */
649static inline const char *crypto_tfm_alg_name(struct crypto_tfm *tfm)
650{
651 return tfm->__crt_alg->cra_name;
652}
653
Michal Ludvigb14cdd62006-07-09 09:02:24 +1000654static inline const char *crypto_tfm_alg_driver_name(struct crypto_tfm *tfm)
655{
656 return tfm->__crt_alg->cra_driver_name;
657}
658
659static inline int crypto_tfm_alg_priority(struct crypto_tfm *tfm)
660{
661 return tfm->__crt_alg->cra_priority;
662}
663
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm)
665{
666 return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK;
667}
668
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm)
670{
671 return tfm->__crt_alg->cra_blocksize;
672}
673
Herbert Xufbdae9f2005-07-06 13:53:29 -0700674static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm *tfm)
675{
676 return tfm->__crt_alg->cra_alignmask;
677}
678
Herbert Xuf28776a2006-08-13 20:58:18 +1000679static inline u32 crypto_tfm_get_flags(struct crypto_tfm *tfm)
680{
681 return tfm->crt_flags;
682}
683
684static inline void crypto_tfm_set_flags(struct crypto_tfm *tfm, u32 flags)
685{
686 tfm->crt_flags |= flags;
687}
688
689static inline void crypto_tfm_clear_flags(struct crypto_tfm *tfm, u32 flags)
690{
691 tfm->crt_flags &= ~flags;
692}
693
Herbert Xu40725182005-07-06 13:51:52 -0700694static inline void *crypto_tfm_ctx(struct crypto_tfm *tfm)
695{
Herbert Xuf10b7892006-01-25 22:34:01 +1100696 return tfm->__crt_ctx;
697}
698
699static inline unsigned int crypto_tfm_ctx_alignment(void)
700{
701 struct crypto_tfm *tfm;
702 return __alignof__(tfm->__crt_ctx);
Herbert Xu40725182005-07-06 13:51:52 -0700703}
704
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705/*
706 * API wrappers.
707 */
Herbert Xu32e39832007-03-24 14:35:34 +1100708static inline struct crypto_ablkcipher *__crypto_ablkcipher_cast(
709 struct crypto_tfm *tfm)
710{
711 return (struct crypto_ablkcipher *)tfm;
712}
713
Herbert Xu378f4f52007-12-17 20:07:31 +0800714static inline u32 crypto_skcipher_type(u32 type)
715{
Herbert Xuecfc4322007-12-05 21:08:36 +1100716 type &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
Herbert Xu378f4f52007-12-17 20:07:31 +0800717 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
718 return type;
719}
720
721static inline u32 crypto_skcipher_mask(u32 mask)
722{
Herbert Xuecfc4322007-12-05 21:08:36 +1100723 mask &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
Herbert Xu378f4f52007-12-17 20:07:31 +0800724 mask |= CRYPTO_ALG_TYPE_BLKCIPHER_MASK;
725 return mask;
726}
727
Stephan Muellerf13ec332014-11-12 05:28:22 +0100728/**
729 * DOC: Asynchronous Block Cipher API
730 *
731 * Asynchronous block cipher API is used with the ciphers of type
732 * CRYPTO_ALG_TYPE_ABLKCIPHER (listed as type "ablkcipher" in /proc/crypto).
733 *
734 * Asynchronous cipher operations imply that the function invocation for a
735 * cipher request returns immediately before the completion of the operation.
736 * The cipher request is scheduled as a separate kernel thread and therefore
737 * load-balanced on the different CPUs via the process scheduler. To allow
738 * the kernel crypto API to inform the caller about the completion of a cipher
739 * request, the caller must provide a callback function. That function is
740 * invoked with the cipher handle when the request completes.
741 *
742 * To support the asynchronous operation, additional information than just the
743 * cipher handle must be supplied to the kernel crypto API. That additional
744 * information is given by filling in the ablkcipher_request data structure.
745 *
746 * For the asynchronous block cipher API, the state is maintained with the tfm
747 * cipher handle. A single tfm can be used across multiple calls and in
748 * parallel. For asynchronous block cipher calls, context data supplied and
749 * only used by the caller can be referenced the request data structure in
750 * addition to the IV used for the cipher request. The maintenance of such
751 * state information would be important for a crypto driver implementer to
752 * have, because when calling the callback function upon completion of the
753 * cipher operation, that callback function may need some information about
754 * which operation just finished if it invoked multiple in parallel. This
755 * state information is unused by the kernel crypto API.
756 */
757
Herbert Xu32e39832007-03-24 14:35:34 +1100758static inline struct crypto_tfm *crypto_ablkcipher_tfm(
759 struct crypto_ablkcipher *tfm)
760{
761 return &tfm->base;
762}
763
Stephan Muellerf13ec332014-11-12 05:28:22 +0100764/**
765 * crypto_free_ablkcipher() - zeroize and free cipher handle
766 * @tfm: cipher handle to be freed
767 */
Herbert Xu32e39832007-03-24 14:35:34 +1100768static inline void crypto_free_ablkcipher(struct crypto_ablkcipher *tfm)
769{
770 crypto_free_tfm(crypto_ablkcipher_tfm(tfm));
771}
772
Stephan Muellerf13ec332014-11-12 05:28:22 +0100773/**
774 * crypto_has_ablkcipher() - Search for the availability of an ablkcipher.
775 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
776 * ablkcipher
777 * @type: specifies the type of the cipher
778 * @mask: specifies the mask for the cipher
779 *
780 * Return: true when the ablkcipher is known to the kernel crypto API; false
781 * otherwise
782 */
Herbert Xu32e39832007-03-24 14:35:34 +1100783static inline int crypto_has_ablkcipher(const char *alg_name, u32 type,
784 u32 mask)
785{
Herbert Xu378f4f52007-12-17 20:07:31 +0800786 return crypto_has_alg(alg_name, crypto_skcipher_type(type),
787 crypto_skcipher_mask(mask));
Herbert Xu32e39832007-03-24 14:35:34 +1100788}
789
790static inline struct ablkcipher_tfm *crypto_ablkcipher_crt(
791 struct crypto_ablkcipher *tfm)
792{
793 return &crypto_ablkcipher_tfm(tfm)->crt_ablkcipher;
794}
795
Stephan Muellerf13ec332014-11-12 05:28:22 +0100796/**
797 * crypto_ablkcipher_ivsize() - obtain IV size
798 * @tfm: cipher handle
799 *
800 * The size of the IV for the ablkcipher referenced by the cipher handle is
801 * returned. This IV size may be zero if the cipher does not need an IV.
802 *
803 * Return: IV size in bytes
804 */
Herbert Xu32e39832007-03-24 14:35:34 +1100805static inline unsigned int crypto_ablkcipher_ivsize(
806 struct crypto_ablkcipher *tfm)
807{
808 return crypto_ablkcipher_crt(tfm)->ivsize;
809}
810
Stephan Muellerf13ec332014-11-12 05:28:22 +0100811/**
812 * crypto_ablkcipher_blocksize() - obtain block size of cipher
813 * @tfm: cipher handle
814 *
815 * The block size for the ablkcipher referenced with the cipher handle is
816 * returned. The caller may use that information to allocate appropriate
817 * memory for the data returned by the encryption or decryption operation
818 *
819 * Return: block size of cipher
820 */
Herbert Xu32e39832007-03-24 14:35:34 +1100821static inline unsigned int crypto_ablkcipher_blocksize(
822 struct crypto_ablkcipher *tfm)
823{
824 return crypto_tfm_alg_blocksize(crypto_ablkcipher_tfm(tfm));
825}
826
827static inline unsigned int crypto_ablkcipher_alignmask(
828 struct crypto_ablkcipher *tfm)
829{
830 return crypto_tfm_alg_alignmask(crypto_ablkcipher_tfm(tfm));
831}
832
833static inline u32 crypto_ablkcipher_get_flags(struct crypto_ablkcipher *tfm)
834{
835 return crypto_tfm_get_flags(crypto_ablkcipher_tfm(tfm));
836}
837
838static inline void crypto_ablkcipher_set_flags(struct crypto_ablkcipher *tfm,
839 u32 flags)
840{
841 crypto_tfm_set_flags(crypto_ablkcipher_tfm(tfm), flags);
842}
843
844static inline void crypto_ablkcipher_clear_flags(struct crypto_ablkcipher *tfm,
845 u32 flags)
846{
847 crypto_tfm_clear_flags(crypto_ablkcipher_tfm(tfm), flags);
848}
849
Stephan Muellerf13ec332014-11-12 05:28:22 +0100850/**
851 * crypto_ablkcipher_setkey() - set key for cipher
852 * @tfm: cipher handle
853 * @key: buffer holding the key
854 * @keylen: length of the key in bytes
855 *
856 * The caller provided key is set for the ablkcipher referenced by the cipher
857 * handle.
858 *
859 * Note, the key length determines the cipher type. Many block ciphers implement
860 * different cipher modes depending on the key size, such as AES-128 vs AES-192
861 * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
862 * is performed.
863 *
864 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
865 */
Herbert Xu32e39832007-03-24 14:35:34 +1100866static inline int crypto_ablkcipher_setkey(struct crypto_ablkcipher *tfm,
867 const u8 *key, unsigned int keylen)
868{
Herbert Xuecfc4322007-12-05 21:08:36 +1100869 struct ablkcipher_tfm *crt = crypto_ablkcipher_crt(tfm);
870
871 return crt->setkey(crt->base, key, keylen);
Herbert Xu32e39832007-03-24 14:35:34 +1100872}
873
Stephan Muellerf13ec332014-11-12 05:28:22 +0100874/**
875 * crypto_ablkcipher_reqtfm() - obtain cipher handle from request
876 * @req: ablkcipher_request out of which the cipher handle is to be obtained
877 *
878 * Return the crypto_ablkcipher handle when furnishing an ablkcipher_request
879 * data structure.
880 *
881 * Return: crypto_ablkcipher handle
882 */
Herbert Xu32e39832007-03-24 14:35:34 +1100883static inline struct crypto_ablkcipher *crypto_ablkcipher_reqtfm(
884 struct ablkcipher_request *req)
885{
886 return __crypto_ablkcipher_cast(req->base.tfm);
887}
888
Stephan Muellerf13ec332014-11-12 05:28:22 +0100889/**
890 * crypto_ablkcipher_encrypt() - encrypt plaintext
891 * @req: reference to the ablkcipher_request handle that holds all information
892 * needed to perform the cipher operation
893 *
894 * Encrypt plaintext data using the ablkcipher_request handle. That data
895 * structure and how it is filled with data is discussed with the
896 * ablkcipher_request_* functions.
897 *
898 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
899 */
Herbert Xu32e39832007-03-24 14:35:34 +1100900static inline int crypto_ablkcipher_encrypt(struct ablkcipher_request *req)
901{
902 struct ablkcipher_tfm *crt =
903 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
904 return crt->encrypt(req);
905}
906
Stephan Muellerf13ec332014-11-12 05:28:22 +0100907/**
908 * crypto_ablkcipher_decrypt() - decrypt ciphertext
909 * @req: reference to the ablkcipher_request handle that holds all information
910 * needed to perform the cipher operation
911 *
912 * Decrypt ciphertext data using the ablkcipher_request handle. That data
913 * structure and how it is filled with data is discussed with the
914 * ablkcipher_request_* functions.
915 *
916 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
917 */
Herbert Xu32e39832007-03-24 14:35:34 +1100918static inline int crypto_ablkcipher_decrypt(struct ablkcipher_request *req)
919{
920 struct ablkcipher_tfm *crt =
921 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
922 return crt->decrypt(req);
923}
924
Stephan Muellerf13ec332014-11-12 05:28:22 +0100925/**
926 * DOC: Asynchronous Cipher Request Handle
927 *
928 * The ablkcipher_request data structure contains all pointers to data
929 * required for the asynchronous cipher operation. This includes the cipher
930 * handle (which can be used by multiple ablkcipher_request instances), pointer
931 * to plaintext and ciphertext, asynchronous callback function, etc. It acts
932 * as a handle to the ablkcipher_request_* API calls in a similar way as
933 * ablkcipher handle to the crypto_ablkcipher_* API calls.
934 */
935
936/**
937 * crypto_ablkcipher_reqsize() - obtain size of the request data structure
938 * @tfm: cipher handle
939 *
940 * Return: number of bytes
941 */
Herbert Xub16c3a22007-08-29 19:02:04 +0800942static inline unsigned int crypto_ablkcipher_reqsize(
943 struct crypto_ablkcipher *tfm)
Herbert Xu32e39832007-03-24 14:35:34 +1100944{
945 return crypto_ablkcipher_crt(tfm)->reqsize;
946}
947
Stephan Muellerf13ec332014-11-12 05:28:22 +0100948/**
949 * ablkcipher_request_set_tfm() - update cipher handle reference in request
950 * @req: request handle to be modified
951 * @tfm: cipher handle that shall be added to the request handle
952 *
953 * Allow the caller to replace the existing ablkcipher handle in the request
954 * data structure with a different one.
955 */
Herbert Xue196d622007-04-14 16:09:14 +1000956static inline void ablkcipher_request_set_tfm(
957 struct ablkcipher_request *req, struct crypto_ablkcipher *tfm)
958{
Herbert Xuecfc4322007-12-05 21:08:36 +1100959 req->base.tfm = crypto_ablkcipher_tfm(crypto_ablkcipher_crt(tfm)->base);
Herbert Xue196d622007-04-14 16:09:14 +1000960}
961
Herbert Xub5b7f082007-04-16 20:48:54 +1000962static inline struct ablkcipher_request *ablkcipher_request_cast(
963 struct crypto_async_request *req)
964{
965 return container_of(req, struct ablkcipher_request, base);
966}
967
Stephan Muellerf13ec332014-11-12 05:28:22 +0100968/**
969 * ablkcipher_request_alloc() - allocate request data structure
970 * @tfm: cipher handle to be registered with the request
971 * @gfp: memory allocation flag that is handed to kmalloc by the API call.
972 *
973 * Allocate the request data structure that must be used with the ablkcipher
974 * encrypt and decrypt API calls. During the allocation, the provided ablkcipher
975 * handle is registered in the request data structure.
976 *
Eric Biggers6eae29e2016-04-02 10:54:56 -0500977 * Return: allocated request handle in case of success, or NULL if out of memory
Stephan Muellerf13ec332014-11-12 05:28:22 +0100978 */
Herbert Xu32e39832007-03-24 14:35:34 +1100979static inline struct ablkcipher_request *ablkcipher_request_alloc(
980 struct crypto_ablkcipher *tfm, gfp_t gfp)
981{
982 struct ablkcipher_request *req;
983
984 req = kmalloc(sizeof(struct ablkcipher_request) +
985 crypto_ablkcipher_reqsize(tfm), gfp);
986
987 if (likely(req))
Herbert Xue196d622007-04-14 16:09:14 +1000988 ablkcipher_request_set_tfm(req, tfm);
Herbert Xu32e39832007-03-24 14:35:34 +1100989
990 return req;
991}
992
Stephan Muellerf13ec332014-11-12 05:28:22 +0100993/**
994 * ablkcipher_request_free() - zeroize and free request data structure
995 * @req: request data structure cipher handle to be freed
996 */
Herbert Xu32e39832007-03-24 14:35:34 +1100997static inline void ablkcipher_request_free(struct ablkcipher_request *req)
998{
Herbert Xuaef73cf2009-07-11 22:22:14 +0800999 kzfree(req);
Herbert Xu32e39832007-03-24 14:35:34 +11001000}
1001
Stephan Muellerf13ec332014-11-12 05:28:22 +01001002/**
1003 * ablkcipher_request_set_callback() - set asynchronous callback function
1004 * @req: request handle
1005 * @flags: specify zero or an ORing of the flags
Stephan Mueller0184cfe2016-10-21 04:57:27 +02001006 * CRYPTO_TFM_REQ_MAY_BACKLOG the request queue may back log and
Stephan Muellerf13ec332014-11-12 05:28:22 +01001007 * increase the wait queue beyond the initial maximum size;
1008 * CRYPTO_TFM_REQ_MAY_SLEEP the request processing may sleep
1009 * @compl: callback function pointer to be registered with the request handle
1010 * @data: The data pointer refers to memory that is not used by the kernel
1011 * crypto API, but provided to the callback function for it to use. Here,
1012 * the caller can provide a reference to memory the callback function can
1013 * operate on. As the callback function is invoked asynchronously to the
1014 * related functionality, it may need to access data structures of the
1015 * related functionality which can be referenced using this pointer. The
1016 * callback function can access the memory via the "data" field in the
1017 * crypto_async_request data structure provided to the callback function.
1018 *
1019 * This function allows setting the callback function that is triggered once the
1020 * cipher operation completes.
1021 *
1022 * The callback function is registered with the ablkcipher_request handle and
Stephan Mueller0184cfe2016-10-21 04:57:27 +02001023 * must comply with the following template::
Stephan Muellerf13ec332014-11-12 05:28:22 +01001024 *
1025 * void callback_function(struct crypto_async_request *req, int error)
1026 */
Herbert Xu32e39832007-03-24 14:35:34 +11001027static inline void ablkcipher_request_set_callback(
1028 struct ablkcipher_request *req,
Mark Rustad3e3dc252014-07-25 02:53:38 -07001029 u32 flags, crypto_completion_t compl, void *data)
Herbert Xu32e39832007-03-24 14:35:34 +11001030{
Mark Rustad3e3dc252014-07-25 02:53:38 -07001031 req->base.complete = compl;
Herbert Xu32e39832007-03-24 14:35:34 +11001032 req->base.data = data;
1033 req->base.flags = flags;
1034}
1035
Stephan Muellerf13ec332014-11-12 05:28:22 +01001036/**
1037 * ablkcipher_request_set_crypt() - set data buffers
1038 * @req: request handle
1039 * @src: source scatter / gather list
1040 * @dst: destination scatter / gather list
1041 * @nbytes: number of bytes to process from @src
1042 * @iv: IV for the cipher operation which must comply with the IV size defined
1043 * by crypto_ablkcipher_ivsize
1044 *
1045 * This function allows setting of the source data and destination data
1046 * scatter / gather lists.
1047 *
1048 * For encryption, the source is treated as the plaintext and the
1049 * destination is the ciphertext. For a decryption operation, the use is
Stephan Mueller379dcfb2015-01-19 00:13:39 +01001050 * reversed - the source is the ciphertext and the destination is the plaintext.
Stephan Muellerf13ec332014-11-12 05:28:22 +01001051 */
Herbert Xu32e39832007-03-24 14:35:34 +11001052static inline void ablkcipher_request_set_crypt(
1053 struct ablkcipher_request *req,
1054 struct scatterlist *src, struct scatterlist *dst,
1055 unsigned int nbytes, void *iv)
1056{
1057 req->src = src;
1058 req->dst = dst;
1059 req->nbytes = nbytes;
1060 req->info = iv;
1061}
1062
Stephan Muellerfced7b02014-11-12 05:29:00 +01001063/**
Stephan Mueller58284f02014-11-12 05:29:36 +01001064 * DOC: Synchronous Block Cipher API
1065 *
1066 * The synchronous block cipher API is used with the ciphers of type
1067 * CRYPTO_ALG_TYPE_BLKCIPHER (listed as type "blkcipher" in /proc/crypto)
1068 *
1069 * Synchronous calls, have a context in the tfm. But since a single tfm can be
1070 * used in multiple calls and in parallel, this info should not be changeable
1071 * (unless a lock is used). This applies, for example, to the symmetric key.
1072 * However, the IV is changeable, so there is an iv field in blkcipher_tfm
1073 * structure for synchronous blkcipher api. So, its the only state info that can
1074 * be kept for synchronous calls without using a big lock across a tfm.
1075 *
1076 * The block cipher API allows the use of a complete cipher, i.e. a cipher
1077 * consisting of a template (a block chaining mode) and a single block cipher
1078 * primitive (e.g. AES).
1079 *
1080 * The plaintext data buffer and the ciphertext data buffer are pointed to
1081 * by using scatter/gather lists. The cipher operation is performed
1082 * on all segments of the provided scatter/gather lists.
1083 *
1084 * The kernel crypto API supports a cipher operation "in-place" which means that
1085 * the caller may provide the same scatter/gather list for the plaintext and
1086 * cipher text. After the completion of the cipher operation, the plaintext
1087 * data is replaced with the ciphertext data in case of an encryption and vice
1088 * versa for a decryption. The caller must ensure that the scatter/gather lists
1089 * for the output data point to sufficiently large buffers, i.e. multiples of
1090 * the block size of the cipher.
1091 */
1092
Herbert Xu5cde0af2006-08-22 00:07:53 +10001093static inline struct crypto_blkcipher *__crypto_blkcipher_cast(
1094 struct crypto_tfm *tfm)
1095{
1096 return (struct crypto_blkcipher *)tfm;
1097}
1098
1099static inline struct crypto_blkcipher *crypto_blkcipher_cast(
1100 struct crypto_tfm *tfm)
1101{
1102 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_BLKCIPHER);
1103 return __crypto_blkcipher_cast(tfm);
1104}
1105
Stephan Mueller58284f02014-11-12 05:29:36 +01001106/**
1107 * crypto_alloc_blkcipher() - allocate synchronous block cipher handle
1108 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1109 * blkcipher cipher
1110 * @type: specifies the type of the cipher
1111 * @mask: specifies the mask for the cipher
1112 *
1113 * Allocate a cipher handle for a block cipher. The returned struct
1114 * crypto_blkcipher is the cipher handle that is required for any subsequent
1115 * API invocation for that block cipher.
1116 *
1117 * Return: allocated cipher handle in case of success; IS_ERR() is true in case
1118 * of an error, PTR_ERR() returns the error code.
1119 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001120static inline struct crypto_blkcipher *crypto_alloc_blkcipher(
1121 const char *alg_name, u32 type, u32 mask)
1122{
Herbert Xu332f88402007-11-15 22:36:07 +08001123 type &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xu5cde0af2006-08-22 00:07:53 +10001124 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
Herbert Xu332f88402007-11-15 22:36:07 +08001125 mask |= CRYPTO_ALG_TYPE_MASK;
Herbert Xu5cde0af2006-08-22 00:07:53 +10001126
1127 return __crypto_blkcipher_cast(crypto_alloc_base(alg_name, type, mask));
1128}
1129
1130static inline struct crypto_tfm *crypto_blkcipher_tfm(
1131 struct crypto_blkcipher *tfm)
1132{
1133 return &tfm->base;
1134}
1135
Stephan Mueller58284f02014-11-12 05:29:36 +01001136/**
1137 * crypto_free_blkcipher() - zeroize and free the block cipher handle
1138 * @tfm: cipher handle to be freed
1139 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001140static inline void crypto_free_blkcipher(struct crypto_blkcipher *tfm)
1141{
1142 crypto_free_tfm(crypto_blkcipher_tfm(tfm));
1143}
1144
Stephan Mueller58284f02014-11-12 05:29:36 +01001145/**
1146 * crypto_has_blkcipher() - Search for the availability of a block cipher
1147 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1148 * block cipher
1149 * @type: specifies the type of the cipher
1150 * @mask: specifies the mask for the cipher
1151 *
1152 * Return: true when the block cipher is known to the kernel crypto API; false
1153 * otherwise
1154 */
Herbert Xufce32d72006-08-26 17:35:45 +10001155static inline int crypto_has_blkcipher(const char *alg_name, u32 type, u32 mask)
1156{
Herbert Xu332f88402007-11-15 22:36:07 +08001157 type &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xufce32d72006-08-26 17:35:45 +10001158 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
Herbert Xu332f88402007-11-15 22:36:07 +08001159 mask |= CRYPTO_ALG_TYPE_MASK;
Herbert Xufce32d72006-08-26 17:35:45 +10001160
1161 return crypto_has_alg(alg_name, type, mask);
1162}
1163
Stephan Mueller58284f02014-11-12 05:29:36 +01001164/**
1165 * crypto_blkcipher_name() - return the name / cra_name from the cipher handle
1166 * @tfm: cipher handle
1167 *
1168 * Return: The character string holding the name of the cipher
1169 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001170static inline const char *crypto_blkcipher_name(struct crypto_blkcipher *tfm)
1171{
1172 return crypto_tfm_alg_name(crypto_blkcipher_tfm(tfm));
1173}
1174
1175static inline struct blkcipher_tfm *crypto_blkcipher_crt(
1176 struct crypto_blkcipher *tfm)
1177{
1178 return &crypto_blkcipher_tfm(tfm)->crt_blkcipher;
1179}
1180
1181static inline struct blkcipher_alg *crypto_blkcipher_alg(
1182 struct crypto_blkcipher *tfm)
1183{
1184 return &crypto_blkcipher_tfm(tfm)->__crt_alg->cra_blkcipher;
1185}
1186
Stephan Mueller58284f02014-11-12 05:29:36 +01001187/**
1188 * crypto_blkcipher_ivsize() - obtain IV size
1189 * @tfm: cipher handle
1190 *
1191 * The size of the IV for the block cipher referenced by the cipher handle is
1192 * returned. This IV size may be zero if the cipher does not need an IV.
1193 *
1194 * Return: IV size in bytes
1195 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001196static inline unsigned int crypto_blkcipher_ivsize(struct crypto_blkcipher *tfm)
1197{
1198 return crypto_blkcipher_alg(tfm)->ivsize;
1199}
1200
Stephan Mueller58284f02014-11-12 05:29:36 +01001201/**
1202 * crypto_blkcipher_blocksize() - obtain block size of cipher
1203 * @tfm: cipher handle
1204 *
1205 * The block size for the block cipher referenced with the cipher handle is
1206 * returned. The caller may use that information to allocate appropriate
1207 * memory for the data returned by the encryption or decryption operation.
1208 *
1209 * Return: block size of cipher
1210 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001211static inline unsigned int crypto_blkcipher_blocksize(
1212 struct crypto_blkcipher *tfm)
1213{
1214 return crypto_tfm_alg_blocksize(crypto_blkcipher_tfm(tfm));
1215}
1216
1217static inline unsigned int crypto_blkcipher_alignmask(
1218 struct crypto_blkcipher *tfm)
1219{
1220 return crypto_tfm_alg_alignmask(crypto_blkcipher_tfm(tfm));
1221}
1222
1223static inline u32 crypto_blkcipher_get_flags(struct crypto_blkcipher *tfm)
1224{
1225 return crypto_tfm_get_flags(crypto_blkcipher_tfm(tfm));
1226}
1227
1228static inline void crypto_blkcipher_set_flags(struct crypto_blkcipher *tfm,
1229 u32 flags)
1230{
1231 crypto_tfm_set_flags(crypto_blkcipher_tfm(tfm), flags);
1232}
1233
1234static inline void crypto_blkcipher_clear_flags(struct crypto_blkcipher *tfm,
1235 u32 flags)
1236{
1237 crypto_tfm_clear_flags(crypto_blkcipher_tfm(tfm), flags);
1238}
1239
Stephan Mueller58284f02014-11-12 05:29:36 +01001240/**
1241 * crypto_blkcipher_setkey() - set key for cipher
1242 * @tfm: cipher handle
1243 * @key: buffer holding the key
1244 * @keylen: length of the key in bytes
1245 *
1246 * The caller provided key is set for the block cipher referenced by the cipher
1247 * handle.
1248 *
1249 * Note, the key length determines the cipher type. Many block ciphers implement
1250 * different cipher modes depending on the key size, such as AES-128 vs AES-192
1251 * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
1252 * is performed.
1253 *
1254 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
1255 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001256static inline int crypto_blkcipher_setkey(struct crypto_blkcipher *tfm,
1257 const u8 *key, unsigned int keylen)
1258{
1259 return crypto_blkcipher_crt(tfm)->setkey(crypto_blkcipher_tfm(tfm),
1260 key, keylen);
1261}
1262
Stephan Mueller58284f02014-11-12 05:29:36 +01001263/**
1264 * crypto_blkcipher_encrypt() - encrypt plaintext
1265 * @desc: reference to the block cipher handle with meta data
1266 * @dst: scatter/gather list that is filled by the cipher operation with the
1267 * ciphertext
1268 * @src: scatter/gather list that holds the plaintext
1269 * @nbytes: number of bytes of the plaintext to encrypt.
1270 *
1271 * Encrypt plaintext data using the IV set by the caller with a preceding
1272 * call of crypto_blkcipher_set_iv.
1273 *
1274 * The blkcipher_desc data structure must be filled by the caller and can
1275 * reside on the stack. The caller must fill desc as follows: desc.tfm is filled
1276 * with the block cipher handle; desc.flags is filled with either
1277 * CRYPTO_TFM_REQ_MAY_SLEEP or 0.
1278 *
1279 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1280 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001281static inline int crypto_blkcipher_encrypt(struct blkcipher_desc *desc,
1282 struct scatterlist *dst,
1283 struct scatterlist *src,
1284 unsigned int nbytes)
1285{
1286 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
1287 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
1288}
1289
Stephan Mueller58284f02014-11-12 05:29:36 +01001290/**
1291 * crypto_blkcipher_encrypt_iv() - encrypt plaintext with dedicated IV
1292 * @desc: reference to the block cipher handle with meta data
1293 * @dst: scatter/gather list that is filled by the cipher operation with the
1294 * ciphertext
1295 * @src: scatter/gather list that holds the plaintext
1296 * @nbytes: number of bytes of the plaintext to encrypt.
1297 *
1298 * Encrypt plaintext data with the use of an IV that is solely used for this
1299 * cipher operation. Any previously set IV is not used.
1300 *
1301 * The blkcipher_desc data structure must be filled by the caller and can
1302 * reside on the stack. The caller must fill desc as follows: desc.tfm is filled
1303 * with the block cipher handle; desc.info is filled with the IV to be used for
1304 * the current operation; desc.flags is filled with either
1305 * CRYPTO_TFM_REQ_MAY_SLEEP or 0.
1306 *
1307 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1308 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001309static inline int crypto_blkcipher_encrypt_iv(struct blkcipher_desc *desc,
1310 struct scatterlist *dst,
1311 struct scatterlist *src,
1312 unsigned int nbytes)
1313{
1314 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
1315}
1316
Stephan Mueller58284f02014-11-12 05:29:36 +01001317/**
1318 * crypto_blkcipher_decrypt() - decrypt ciphertext
1319 * @desc: reference to the block cipher handle with meta data
1320 * @dst: scatter/gather list that is filled by the cipher operation with the
1321 * plaintext
1322 * @src: scatter/gather list that holds the ciphertext
1323 * @nbytes: number of bytes of the ciphertext to decrypt.
1324 *
1325 * Decrypt ciphertext data using the IV set by the caller with a preceding
1326 * call of crypto_blkcipher_set_iv.
1327 *
1328 * The blkcipher_desc data structure must be filled by the caller as documented
1329 * for the crypto_blkcipher_encrypt call above.
1330 *
1331 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1332 *
1333 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001334static inline int crypto_blkcipher_decrypt(struct blkcipher_desc *desc,
1335 struct scatterlist *dst,
1336 struct scatterlist *src,
1337 unsigned int nbytes)
1338{
1339 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
1340 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
1341}
1342
Stephan Mueller58284f02014-11-12 05:29:36 +01001343/**
1344 * crypto_blkcipher_decrypt_iv() - decrypt ciphertext with dedicated IV
1345 * @desc: reference to the block cipher handle with meta data
1346 * @dst: scatter/gather list that is filled by the cipher operation with the
1347 * plaintext
1348 * @src: scatter/gather list that holds the ciphertext
1349 * @nbytes: number of bytes of the ciphertext to decrypt.
1350 *
1351 * Decrypt ciphertext data with the use of an IV that is solely used for this
1352 * cipher operation. Any previously set IV is not used.
1353 *
1354 * The blkcipher_desc data structure must be filled by the caller as documented
1355 * for the crypto_blkcipher_encrypt_iv call above.
1356 *
1357 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1358 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001359static inline int crypto_blkcipher_decrypt_iv(struct blkcipher_desc *desc,
1360 struct scatterlist *dst,
1361 struct scatterlist *src,
1362 unsigned int nbytes)
1363{
1364 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
1365}
1366
Stephan Mueller58284f02014-11-12 05:29:36 +01001367/**
1368 * crypto_blkcipher_set_iv() - set IV for cipher
1369 * @tfm: cipher handle
1370 * @src: buffer holding the IV
1371 * @len: length of the IV in bytes
1372 *
1373 * The caller provided IV is set for the block cipher referenced by the cipher
1374 * handle.
1375 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001376static inline void crypto_blkcipher_set_iv(struct crypto_blkcipher *tfm,
1377 const u8 *src, unsigned int len)
1378{
1379 memcpy(crypto_blkcipher_crt(tfm)->iv, src, len);
1380}
1381
Stephan Mueller58284f02014-11-12 05:29:36 +01001382/**
1383 * crypto_blkcipher_get_iv() - obtain IV from cipher
1384 * @tfm: cipher handle
1385 * @dst: buffer filled with the IV
1386 * @len: length of the buffer dst
1387 *
1388 * The caller can obtain the IV set for the block cipher referenced by the
1389 * cipher handle and store it into the user-provided buffer. If the buffer
1390 * has an insufficient space, the IV is truncated to fit the buffer.
1391 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001392static inline void crypto_blkcipher_get_iv(struct crypto_blkcipher *tfm,
1393 u8 *dst, unsigned int len)
1394{
1395 memcpy(dst, crypto_blkcipher_crt(tfm)->iv, len);
1396}
1397
Stephan Mueller16e61032014-11-12 05:30:06 +01001398/**
1399 * DOC: Single Block Cipher API
1400 *
1401 * The single block cipher API is used with the ciphers of type
1402 * CRYPTO_ALG_TYPE_CIPHER (listed as type "cipher" in /proc/crypto).
1403 *
1404 * Using the single block cipher API calls, operations with the basic cipher
1405 * primitive can be implemented. These cipher primitives exclude any block
1406 * chaining operations including IV handling.
1407 *
1408 * The purpose of this single block cipher API is to support the implementation
1409 * of templates or other concepts that only need to perform the cipher operation
1410 * on one block at a time. Templates invoke the underlying cipher primitive
1411 * block-wise and process either the input or the output data of these cipher
1412 * operations.
1413 */
1414
Herbert Xuf28776a2006-08-13 20:58:18 +10001415static inline struct crypto_cipher *__crypto_cipher_cast(struct crypto_tfm *tfm)
1416{
1417 return (struct crypto_cipher *)tfm;
1418}
1419
1420static inline struct crypto_cipher *crypto_cipher_cast(struct crypto_tfm *tfm)
1421{
1422 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
1423 return __crypto_cipher_cast(tfm);
1424}
1425
Stephan Mueller16e61032014-11-12 05:30:06 +01001426/**
1427 * crypto_alloc_cipher() - allocate single block cipher handle
1428 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1429 * single block cipher
1430 * @type: specifies the type of the cipher
1431 * @mask: specifies the mask for the cipher
1432 *
1433 * Allocate a cipher handle for a single block cipher. The returned struct
1434 * crypto_cipher is the cipher handle that is required for any subsequent API
1435 * invocation for that single block cipher.
1436 *
1437 * Return: allocated cipher handle in case of success; IS_ERR() is true in case
1438 * of an error, PTR_ERR() returns the error code.
1439 */
Herbert Xuf28776a2006-08-13 20:58:18 +10001440static inline struct crypto_cipher *crypto_alloc_cipher(const char *alg_name,
1441 u32 type, u32 mask)
1442{
1443 type &= ~CRYPTO_ALG_TYPE_MASK;
1444 type |= CRYPTO_ALG_TYPE_CIPHER;
1445 mask |= CRYPTO_ALG_TYPE_MASK;
1446
1447 return __crypto_cipher_cast(crypto_alloc_base(alg_name, type, mask));
1448}
1449
1450static inline struct crypto_tfm *crypto_cipher_tfm(struct crypto_cipher *tfm)
1451{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001452 return &tfm->base;
Herbert Xuf28776a2006-08-13 20:58:18 +10001453}
1454
Stephan Mueller16e61032014-11-12 05:30:06 +01001455/**
1456 * crypto_free_cipher() - zeroize and free the single block cipher handle
1457 * @tfm: cipher handle to be freed
1458 */
Herbert Xuf28776a2006-08-13 20:58:18 +10001459static inline void crypto_free_cipher(struct crypto_cipher *tfm)
1460{
1461 crypto_free_tfm(crypto_cipher_tfm(tfm));
1462}
1463
Stephan Mueller16e61032014-11-12 05:30:06 +01001464/**
1465 * crypto_has_cipher() - Search for the availability of a single block cipher
1466 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1467 * single block cipher
1468 * @type: specifies the type of the cipher
1469 * @mask: specifies the mask for the cipher
1470 *
1471 * Return: true when the single block cipher is known to the kernel crypto API;
1472 * false otherwise
1473 */
Herbert Xufce32d72006-08-26 17:35:45 +10001474static inline int crypto_has_cipher(const char *alg_name, u32 type, u32 mask)
1475{
1476 type &= ~CRYPTO_ALG_TYPE_MASK;
1477 type |= CRYPTO_ALG_TYPE_CIPHER;
1478 mask |= CRYPTO_ALG_TYPE_MASK;
1479
1480 return crypto_has_alg(alg_name, type, mask);
1481}
1482
Herbert Xuf28776a2006-08-13 20:58:18 +10001483static inline struct cipher_tfm *crypto_cipher_crt(struct crypto_cipher *tfm)
1484{
1485 return &crypto_cipher_tfm(tfm)->crt_cipher;
1486}
1487
Stephan Mueller16e61032014-11-12 05:30:06 +01001488/**
1489 * crypto_cipher_blocksize() - obtain block size for cipher
1490 * @tfm: cipher handle
1491 *
1492 * The block size for the single block cipher referenced with the cipher handle
1493 * tfm is returned. The caller may use that information to allocate appropriate
1494 * memory for the data returned by the encryption or decryption operation
1495 *
1496 * Return: block size of cipher
1497 */
Herbert Xuf28776a2006-08-13 20:58:18 +10001498static inline unsigned int crypto_cipher_blocksize(struct crypto_cipher *tfm)
1499{
1500 return crypto_tfm_alg_blocksize(crypto_cipher_tfm(tfm));
1501}
1502
1503static inline unsigned int crypto_cipher_alignmask(struct crypto_cipher *tfm)
1504{
1505 return crypto_tfm_alg_alignmask(crypto_cipher_tfm(tfm));
1506}
1507
1508static inline u32 crypto_cipher_get_flags(struct crypto_cipher *tfm)
1509{
1510 return crypto_tfm_get_flags(crypto_cipher_tfm(tfm));
1511}
1512
1513static inline void crypto_cipher_set_flags(struct crypto_cipher *tfm,
1514 u32 flags)
1515{
1516 crypto_tfm_set_flags(crypto_cipher_tfm(tfm), flags);
1517}
1518
1519static inline void crypto_cipher_clear_flags(struct crypto_cipher *tfm,
1520 u32 flags)
1521{
1522 crypto_tfm_clear_flags(crypto_cipher_tfm(tfm), flags);
1523}
1524
Stephan Mueller16e61032014-11-12 05:30:06 +01001525/**
1526 * crypto_cipher_setkey() - set key for cipher
1527 * @tfm: cipher handle
1528 * @key: buffer holding the key
1529 * @keylen: length of the key in bytes
1530 *
1531 * The caller provided key is set for the single block cipher referenced by the
1532 * cipher handle.
1533 *
1534 * Note, the key length determines the cipher type. Many block ciphers implement
1535 * different cipher modes depending on the key size, such as AES-128 vs AES-192
1536 * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
1537 * is performed.
1538 *
1539 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
1540 */
Herbert Xu7226bc872006-08-21 21:40:49 +10001541static inline int crypto_cipher_setkey(struct crypto_cipher *tfm,
1542 const u8 *key, unsigned int keylen)
1543{
1544 return crypto_cipher_crt(tfm)->cit_setkey(crypto_cipher_tfm(tfm),
1545 key, keylen);
1546}
1547
Stephan Mueller16e61032014-11-12 05:30:06 +01001548/**
1549 * crypto_cipher_encrypt_one() - encrypt one block of plaintext
1550 * @tfm: cipher handle
1551 * @dst: points to the buffer that will be filled with the ciphertext
1552 * @src: buffer holding the plaintext to be encrypted
1553 *
1554 * Invoke the encryption operation of one block. The caller must ensure that
1555 * the plaintext and ciphertext buffers are at least one block in size.
1556 */
Herbert Xuf28776a2006-08-13 20:58:18 +10001557static inline void crypto_cipher_encrypt_one(struct crypto_cipher *tfm,
1558 u8 *dst, const u8 *src)
1559{
1560 crypto_cipher_crt(tfm)->cit_encrypt_one(crypto_cipher_tfm(tfm),
1561 dst, src);
1562}
1563
Stephan Mueller16e61032014-11-12 05:30:06 +01001564/**
1565 * crypto_cipher_decrypt_one() - decrypt one block of ciphertext
1566 * @tfm: cipher handle
1567 * @dst: points to the buffer that will be filled with the plaintext
1568 * @src: buffer holding the ciphertext to be decrypted
1569 *
1570 * Invoke the decryption operation of one block. The caller must ensure that
1571 * the plaintext and ciphertext buffers are at least one block in size.
1572 */
Herbert Xuf28776a2006-08-13 20:58:18 +10001573static inline void crypto_cipher_decrypt_one(struct crypto_cipher *tfm,
1574 u8 *dst, const u8 *src)
1575{
1576 crypto_cipher_crt(tfm)->cit_decrypt_one(crypto_cipher_tfm(tfm),
1577 dst, src);
1578}
1579
Herbert Xufce32d72006-08-26 17:35:45 +10001580static inline struct crypto_comp *__crypto_comp_cast(struct crypto_tfm *tfm)
1581{
1582 return (struct crypto_comp *)tfm;
1583}
1584
1585static inline struct crypto_comp *crypto_comp_cast(struct crypto_tfm *tfm)
1586{
1587 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_COMPRESS) &
1588 CRYPTO_ALG_TYPE_MASK);
1589 return __crypto_comp_cast(tfm);
1590}
1591
1592static inline struct crypto_comp *crypto_alloc_comp(const char *alg_name,
1593 u32 type, u32 mask)
1594{
1595 type &= ~CRYPTO_ALG_TYPE_MASK;
1596 type |= CRYPTO_ALG_TYPE_COMPRESS;
1597 mask |= CRYPTO_ALG_TYPE_MASK;
1598
1599 return __crypto_comp_cast(crypto_alloc_base(alg_name, type, mask));
1600}
1601
1602static inline struct crypto_tfm *crypto_comp_tfm(struct crypto_comp *tfm)
1603{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001604 return &tfm->base;
Herbert Xufce32d72006-08-26 17:35:45 +10001605}
1606
1607static inline void crypto_free_comp(struct crypto_comp *tfm)
1608{
1609 crypto_free_tfm(crypto_comp_tfm(tfm));
1610}
1611
1612static inline int crypto_has_comp(const char *alg_name, u32 type, u32 mask)
1613{
1614 type &= ~CRYPTO_ALG_TYPE_MASK;
1615 type |= CRYPTO_ALG_TYPE_COMPRESS;
1616 mask |= CRYPTO_ALG_TYPE_MASK;
1617
1618 return crypto_has_alg(alg_name, type, mask);
1619}
1620
Herbert Xue4d5b792006-08-26 18:12:40 +10001621static inline const char *crypto_comp_name(struct crypto_comp *tfm)
1622{
1623 return crypto_tfm_alg_name(crypto_comp_tfm(tfm));
1624}
1625
Herbert Xufce32d72006-08-26 17:35:45 +10001626static inline struct compress_tfm *crypto_comp_crt(struct crypto_comp *tfm)
1627{
1628 return &crypto_comp_tfm(tfm)->crt_compress;
1629}
1630
1631static inline int crypto_comp_compress(struct crypto_comp *tfm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 const u8 *src, unsigned int slen,
1633 u8 *dst, unsigned int *dlen)
1634{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001635 return crypto_comp_crt(tfm)->cot_compress(crypto_comp_tfm(tfm),
1636 src, slen, dst, dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637}
1638
Herbert Xufce32d72006-08-26 17:35:45 +10001639static inline int crypto_comp_decompress(struct crypto_comp *tfm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 const u8 *src, unsigned int slen,
1641 u8 *dst, unsigned int *dlen)
1642{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001643 return crypto_comp_crt(tfm)->cot_decompress(crypto_comp_tfm(tfm),
1644 src, slen, dst, dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645}
1646
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647#endif /* _LINUX_CRYPTO_H */
1648