blob: 6666ea02ef513449dcfe72c7722a8b0ad9abecd3 [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>
Jaegeuk Kim8dec0742017-06-22 12:14:40 -070027#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
Neil Horman17f0f4a2008-08-14 22:15:52 +100054#define CRYPTO_ALG_TYPE_RNG 0x0000000c
Tadeusz Struk3c339ab2015-06-16 10:30:55 -070055#define CRYPTO_ALG_TYPE_AKCIPHER 0x0000000d
Giovanni Cabiddu63044c42016-06-02 13:28:55 +010056#define CRYPTO_ALG_TYPE_DIGEST 0x0000000e
57#define CRYPTO_ALG_TYPE_HASH 0x0000000e
58#define CRYPTO_ALG_TYPE_SHASH 0x0000000e
59#define CRYPTO_ALG_TYPE_AHASH 0x0000000f
Herbert Xu055bcee2006-08-19 22:24:23 +100060
61#define CRYPTO_ALG_TYPE_HASH_MASK 0x0000000e
Giovanni Cabiddu63044c42016-06-02 13:28:55 +010062#define CRYPTO_ALG_TYPE_AHASH_MASK 0x0000000e
Herbert Xu332f88402007-11-15 22:36:07 +080063#define CRYPTO_ALG_TYPE_BLKCIPHER_MASK 0x0000000c
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Herbert Xu28259822006-08-06 21:23:26 +100065#define CRYPTO_ALG_LARVAL 0x00000010
Herbert Xu6bfd4802006-09-21 11:39:29 +100066#define CRYPTO_ALG_DEAD 0x00000020
67#define CRYPTO_ALG_DYING 0x00000040
Herbert Xuf3f632d2006-08-06 23:12:59 +100068#define CRYPTO_ALG_ASYNC 0x00000080
Herbert Xu28259822006-08-06 21:23:26 +100069
Linus Torvalds1da177e2005-04-16 15:20:36 -070070/*
Herbert Xu60104392006-08-26 18:34:10 +100071 * Set this bit if and only if the algorithm requires another algorithm of
72 * the same type to handle corner cases.
73 */
74#define CRYPTO_ALG_NEED_FALLBACK 0x00000100
75
76/*
Herbert Xuecfc4322007-12-05 21:08:36 +110077 * This bit is set for symmetric key ciphers that have already been wrapped
78 * with a generic IV generator to prevent them from being wrapped again.
79 */
80#define CRYPTO_ALG_GENIV 0x00000200
81
82/*
Herbert Xu73d38642008-08-03 21:15:23 +080083 * Set if the algorithm has passed automated run-time testing. Note that
84 * if there is no run-time testing for a given algorithm it is considered
85 * to have passed.
86 */
87
88#define CRYPTO_ALG_TESTED 0x00000400
89
90/*
Steffen Klassert64a947b2011-09-27 07:21:26 +020091 * Set if the algorithm is an instance that is build from templates.
92 */
93#define CRYPTO_ALG_INSTANCE 0x00000800
94
Nikos Mavrogiannopoulosd912bb72011-11-01 13:39:56 +010095/* Set this bit if the algorithm provided is hardware accelerated but
96 * not available to userspace via instruction set or so.
97 */
98#define CRYPTO_ALG_KERN_DRIVER_ONLY 0x00001000
99
Steffen Klassert64a947b2011-09-27 07:21:26 +0200100/*
Stephan Mueller06ca7f62015-03-30 21:55:52 +0200101 * Mark a cipher as a service implementation only usable by another
102 * cipher and never by a normal user of the kernel crypto API
103 */
104#define CRYPTO_ALG_INTERNAL 0x00002000
105
106/*
Eric Biggersb392a532018-01-03 11:16:26 -0800107 * Set if the algorithm has a ->setkey() method but can be used without
108 * calling it first, i.e. there is a default key.
109 */
110#define CRYPTO_ALG_OPTIONAL_KEY 0x00004000
111
112/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 * Transform masks and values (for crt_flags).
114 */
Eric Biggersadf26e82018-01-03 11:16:27 -0800115#define CRYPTO_TFM_NEED_KEY 0x00000001
116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117#define CRYPTO_TFM_REQ_MASK 0x000fff00
118#define CRYPTO_TFM_RES_MASK 0xfff00000
119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120#define CRYPTO_TFM_REQ_WEAK_KEY 0x00000100
Herbert Xu64baf3c2005-09-01 17:43:05 -0700121#define CRYPTO_TFM_REQ_MAY_SLEEP 0x00000200
Herbert Xu32e39832007-03-24 14:35:34 +1100122#define CRYPTO_TFM_REQ_MAY_BACKLOG 0x00000400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123#define CRYPTO_TFM_RES_WEAK_KEY 0x00100000
124#define CRYPTO_TFM_RES_BAD_KEY_LEN 0x00200000
125#define CRYPTO_TFM_RES_BAD_KEY_SCHED 0x00400000
126#define CRYPTO_TFM_RES_BAD_BLOCK_LEN 0x00800000
127#define CRYPTO_TFM_RES_BAD_FLAGS 0x01000000
128
129/*
130 * Miscellaneous stuff.
131 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132#define CRYPTO_MAX_ALG_NAME 64
133
Herbert Xu79911102006-08-21 21:03:52 +1000134/*
135 * The macro CRYPTO_MINALIGN_ATTR (along with the void * type in the actual
136 * declaration) is used to ensure that the crypto_tfm context structure is
137 * aligned correctly for the given architecture so that there are no alignment
138 * faults for C data types. In particular, this is required on platforms such
139 * as arm where pointers are 32-bit aligned but there are data types such as
140 * u64 which require 64-bit alignment.
141 */
Herbert Xu79911102006-08-21 21:03:52 +1000142#define CRYPTO_MINALIGN ARCH_KMALLOC_MINALIGN
Herbert Xu79911102006-08-21 21:03:52 +1000143
Herbert Xu79911102006-08-21 21:03:52 +1000144#define CRYPTO_MINALIGN_ATTR __attribute__ ((__aligned__(CRYPTO_MINALIGN)))
Herbert Xu79911102006-08-21 21:03:52 +1000145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146struct scatterlist;
Herbert Xu32e39832007-03-24 14:35:34 +1100147struct crypto_ablkcipher;
148struct crypto_async_request;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000149struct crypto_blkcipher;
Herbert Xu40725182005-07-06 13:51:52 -0700150struct crypto_tfm;
Herbert Xue853c3c2006-08-22 00:06:54 +1000151struct crypto_type;
Herbert Xu61da88e2007-12-17 21:51:27 +0800152struct skcipher_givcrypt_request;
Herbert Xu40725182005-07-06 13:51:52 -0700153
Herbert Xu32e39832007-03-24 14:35:34 +1100154typedef void (*crypto_completion_t)(struct crypto_async_request *req, int err);
155
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100156/**
157 * DOC: Block Cipher Context Data Structures
158 *
159 * These data structures define the operating context for each block cipher
160 * type.
161 */
162
Herbert Xu32e39832007-03-24 14:35:34 +1100163struct crypto_async_request {
164 struct list_head list;
165 crypto_completion_t complete;
166 void *data;
167 struct crypto_tfm *tfm;
168
169 u32 flags;
170};
171
172struct ablkcipher_request {
173 struct crypto_async_request base;
174
175 unsigned int nbytes;
176
177 void *info;
178
179 struct scatterlist *src;
180 struct scatterlist *dst;
181
182 void *__ctx[] CRYPTO_MINALIGN_ATTR;
183};
184
Herbert Xu5cde0af2006-08-22 00:07:53 +1000185struct blkcipher_desc {
186 struct crypto_blkcipher *tfm;
187 void *info;
188 u32 flags;
189};
190
Herbert Xu40725182005-07-06 13:51:52 -0700191struct cipher_desc {
192 struct crypto_tfm *tfm;
Herbert Xu6c2bb982006-05-16 22:09:29 +1000193 void (*crfn)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Herbert Xu40725182005-07-06 13:51:52 -0700194 unsigned int (*prfn)(const struct cipher_desc *desc, u8 *dst,
195 const u8 *src, unsigned int nbytes);
196 void *info;
197};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100199/**
200 * DOC: Block Cipher Algorithm Definitions
201 *
202 * These data structures define modular crypto algorithm implementations,
203 * managed via crypto_register_alg() and crypto_unregister_alg().
204 */
205
206/**
207 * struct ablkcipher_alg - asynchronous block cipher definition
208 * @min_keysize: Minimum key size supported by the transformation. This is the
209 * smallest 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 "_MIN_KEY_SIZE" include/crypto/
213 * @max_keysize: Maximum key size supported by the transformation. This is the
214 * largest key length supported by this transformation algorithm.
215 * This must be set to one of the pre-defined values as this is
216 * not hardware specific. Possible values for this field can be
217 * found via git grep "_MAX_KEY_SIZE" include/crypto/
218 * @setkey: Set key for the transformation. This function is used to either
219 * program a supplied key into the hardware or store the key in the
220 * transformation context for programming it later. Note that this
221 * function does modify the transformation context. This function can
222 * be called multiple times during the existence of the transformation
223 * object, so one must make sure the key is properly reprogrammed into
224 * the hardware. This function is also responsible for checking the key
225 * length for validity. In case a software fallback was put in place in
226 * the @cra_init call, this function might need to use the fallback if
227 * the algorithm doesn't support all of the key sizes.
228 * @encrypt: Encrypt a scatterlist of blocks. This function is used to encrypt
229 * the supplied scatterlist containing the blocks of data. The crypto
230 * API consumer is responsible for aligning the entries of the
231 * scatterlist properly and making sure the chunks are correctly
232 * sized. In case a software fallback was put in place in the
233 * @cra_init call, this function might need to use the fallback if
234 * the algorithm doesn't support all of the key sizes. In case the
235 * key was stored in transformation context, the key might need to be
236 * re-programmed into the hardware in this function. This function
237 * shall not modify the transformation context, as this function may
238 * be called in parallel with the same transformation object.
239 * @decrypt: Decrypt a single block. This is a reverse counterpart to @encrypt
240 * and the conditions are exactly the same.
241 * @givencrypt: Update the IV for encryption. With this function, a cipher
242 * implementation may provide the function on how to update the IV
243 * for encryption.
244 * @givdecrypt: Update the IV for decryption. This is the reverse of
245 * @givencrypt .
246 * @geniv: The transformation implementation may use an "IV generator" provided
247 * by the kernel crypto API. Several use cases have a predefined
248 * approach how IVs are to be updated. For such use cases, the kernel
249 * crypto API provides ready-to-use implementations that can be
250 * referenced with this variable.
251 * @ivsize: IV size applicable for transformation. The consumer must provide an
252 * IV of exactly that size to perform the encrypt or decrypt operation.
253 *
254 * All fields except @givencrypt , @givdecrypt , @geniv and @ivsize are
255 * mandatory and must be filled.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 */
Herbert Xub5b7f082007-04-16 20:48:54 +1000257struct ablkcipher_alg {
258 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
259 unsigned int keylen);
260 int (*encrypt)(struct ablkcipher_request *req);
261 int (*decrypt)(struct ablkcipher_request *req);
Herbert Xu61da88e2007-12-17 21:51:27 +0800262 int (*givencrypt)(struct skcipher_givcrypt_request *req);
263 int (*givdecrypt)(struct skcipher_givcrypt_request *req);
Herbert Xub5b7f082007-04-16 20:48:54 +1000264
Herbert Xu23508e12007-11-27 21:33:24 +0800265 const char *geniv;
266
Herbert Xub5b7f082007-04-16 20:48:54 +1000267 unsigned int min_keysize;
268 unsigned int max_keysize;
269 unsigned int ivsize;
270};
271
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100272/**
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100273 * struct blkcipher_alg - synchronous block cipher definition
274 * @min_keysize: see struct ablkcipher_alg
275 * @max_keysize: see struct ablkcipher_alg
276 * @setkey: see struct ablkcipher_alg
277 * @encrypt: see struct ablkcipher_alg
278 * @decrypt: see struct ablkcipher_alg
279 * @geniv: see struct ablkcipher_alg
280 * @ivsize: see struct ablkcipher_alg
281 *
282 * All fields except @geniv and @ivsize are mandatory and must be filled.
283 */
Herbert Xu5cde0af2006-08-22 00:07:53 +1000284struct blkcipher_alg {
285 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
286 unsigned int keylen);
287 int (*encrypt)(struct blkcipher_desc *desc,
288 struct scatterlist *dst, struct scatterlist *src,
289 unsigned int nbytes);
290 int (*decrypt)(struct blkcipher_desc *desc,
291 struct scatterlist *dst, struct scatterlist *src,
292 unsigned int nbytes);
293
Herbert Xu23508e12007-11-27 21:33:24 +0800294 const char *geniv;
295
Herbert Xu5cde0af2006-08-22 00:07:53 +1000296 unsigned int min_keysize;
297 unsigned int max_keysize;
298 unsigned int ivsize;
299};
300
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100301/**
302 * struct cipher_alg - single-block symmetric ciphers definition
303 * @cia_min_keysize: Minimum key size supported by the transformation. This is
304 * the smallest key length supported by this transformation
305 * algorithm. This must be set to one of the pre-defined
306 * values as this is not hardware specific. Possible values
307 * for this field can be found via git grep "_MIN_KEY_SIZE"
308 * include/crypto/
309 * @cia_max_keysize: Maximum key size supported by the transformation. This is
310 * the largest key length supported by this transformation
311 * algorithm. This must be set to one of the pre-defined values
312 * as this is not hardware specific. Possible values for this
313 * field can be found via git grep "_MAX_KEY_SIZE"
314 * include/crypto/
315 * @cia_setkey: Set key for the transformation. This function is used to either
316 * program a supplied key into the hardware or store the key in the
317 * transformation context for programming it later. Note that this
318 * function does modify the transformation context. This function
319 * can be called multiple times during the existence of the
320 * transformation object, so one must make sure the key is properly
321 * reprogrammed into the hardware. This function is also
322 * responsible for checking the key length for validity.
323 * @cia_encrypt: Encrypt a single block. This function is used to encrypt a
324 * single block of data, which must be @cra_blocksize big. This
325 * always operates on a full @cra_blocksize and it is not possible
326 * to encrypt a block of smaller size. The supplied buffers must
327 * therefore also be at least of @cra_blocksize size. Both the
328 * input and output buffers are always aligned to @cra_alignmask.
329 * In case either of the input or output buffer supplied by user
330 * of the crypto API is not aligned to @cra_alignmask, the crypto
331 * API will re-align the buffers. The re-alignment means that a
332 * new buffer will be allocated, the data will be copied into the
333 * new buffer, then the processing will happen on the new buffer,
334 * then the data will be copied back into the original buffer and
335 * finally the new buffer will be freed. In case a software
336 * fallback was put in place in the @cra_init call, this function
337 * might need to use the fallback if the algorithm doesn't support
338 * all of the key sizes. In case the key was stored in
339 * transformation context, the key might need to be re-programmed
340 * into the hardware in this function. This function shall not
341 * modify the transformation context, as this function may be
342 * called in parallel with the same transformation object.
343 * @cia_decrypt: Decrypt a single block. This is a reverse counterpart to
344 * @cia_encrypt, and the conditions are exactly the same.
345 *
346 * All fields are mandatory and must be filled.
347 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348struct cipher_alg {
349 unsigned int cia_min_keysize;
350 unsigned int cia_max_keysize;
Herbert Xu6c2bb982006-05-16 22:09:29 +1000351 int (*cia_setkey)(struct crypto_tfm *tfm, const u8 *key,
Herbert Xu560c06a2006-08-13 14:16:39 +1000352 unsigned int keylen);
Herbert Xu6c2bb982006-05-16 22:09:29 +1000353 void (*cia_encrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
354 void (*cia_decrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355};
356
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357struct compress_alg {
Herbert Xu6c2bb982006-05-16 22:09:29 +1000358 int (*coa_compress)(struct crypto_tfm *tfm, const u8 *src,
359 unsigned int slen, u8 *dst, unsigned int *dlen);
360 int (*coa_decompress)(struct crypto_tfm *tfm, const u8 *src,
361 unsigned int slen, u8 *dst, unsigned int *dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362};
363
Neil Horman17f0f4a2008-08-14 22:15:52 +1000364
Herbert Xub5b7f082007-04-16 20:48:54 +1000365#define cra_ablkcipher cra_u.ablkcipher
Herbert Xu5cde0af2006-08-22 00:07:53 +1000366#define cra_blkcipher cra_u.blkcipher
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367#define cra_cipher cra_u.cipher
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368#define cra_compress cra_u.compress
369
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100370/**
371 * struct crypto_alg - definition of a cryptograpic cipher algorithm
372 * @cra_flags: Flags describing this transformation. See include/linux/crypto.h
373 * CRYPTO_ALG_* flags for the flags which go in here. Those are
374 * used for fine-tuning the description of the transformation
375 * algorithm.
376 * @cra_blocksize: Minimum block size of this transformation. The size in bytes
377 * of the smallest possible unit which can be transformed with
378 * this algorithm. The users must respect this value.
379 * In case of HASH transformation, it is possible for a smaller
380 * block than @cra_blocksize to be passed to the crypto API for
381 * transformation, in case of any other transformation type, an
382 * error will be returned upon any attempt to transform smaller
383 * than @cra_blocksize chunks.
384 * @cra_ctxsize: Size of the operational context of the transformation. This
385 * value informs the kernel crypto API about the memory size
386 * needed to be allocated for the transformation context.
387 * @cra_alignmask: Alignment mask for the input and output data buffer. The data
388 * buffer containing the input data for the algorithm must be
389 * aligned to this alignment mask. The data buffer for the
390 * output data must be aligned to this alignment mask. Note that
391 * the Crypto API will do the re-alignment in software, but
392 * only under special conditions and there is a performance hit.
393 * The re-alignment happens at these occasions for different
394 * @cra_u types: cipher -- For both input data and output data
395 * buffer; ahash -- For output hash destination buf; shash --
396 * For output hash destination buf.
397 * This is needed on hardware which is flawed by design and
398 * cannot pick data from arbitrary addresses.
399 * @cra_priority: Priority of this transformation implementation. In case
400 * multiple transformations with same @cra_name are available to
401 * the Crypto API, the kernel will use the one with highest
402 * @cra_priority.
403 * @cra_name: Generic name (usable by multiple implementations) of the
404 * transformation algorithm. This is the name of the transformation
405 * itself. This field is used by the kernel when looking up the
406 * providers of particular transformation.
407 * @cra_driver_name: Unique name of the transformation provider. This is the
408 * name of the provider of the transformation. This can be any
409 * arbitrary value, but in the usual case, this contains the
410 * name of the chip or provider and the name of the
411 * transformation algorithm.
412 * @cra_type: Type of the cryptographic transformation. This is a pointer to
413 * struct crypto_type, which implements callbacks common for all
Masanari Iida12f7c142015-06-04 00:01:21 +0900414 * transformation types. There are multiple options:
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100415 * &crypto_blkcipher_type, &crypto_ablkcipher_type,
Herbert Xub0d955b2015-08-14 15:30:41 +0800416 * &crypto_ahash_type, &crypto_rng_type.
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100417 * This field might be empty. In that case, there are no common
418 * callbacks. This is the case for: cipher, compress, shash.
419 * @cra_u: Callbacks implementing the transformation. This is a union of
420 * multiple structures. Depending on the type of transformation selected
421 * by @cra_type and @cra_flags above, the associated structure must be
422 * filled with callbacks. This field might be empty. This is the case
423 * for ahash, shash.
424 * @cra_init: Initialize the cryptographic transformation object. This function
425 * is used to initialize the cryptographic transformation object.
426 * This function is called only once at the instantiation time, right
427 * after the transformation context was allocated. In case the
428 * cryptographic hardware has some special requirements which need to
429 * be handled by software, this function shall check for the precise
430 * requirement of the transformation and put any software fallbacks
431 * in place.
432 * @cra_exit: Deinitialize the cryptographic transformation object. This is a
433 * counterpart to @cra_init, used to remove various changes set in
434 * @cra_init.
435 * @cra_module: Owner of this transformation implementation. Set to THIS_MODULE
436 * @cra_list: internally used
437 * @cra_users: internally used
438 * @cra_refcnt: internally used
439 * @cra_destroy: internally used
440 *
441 * The struct crypto_alg describes a generic Crypto API algorithm and is common
442 * for all of the transformations. Any variable not documented here shall not
443 * be used by a cipher implementation as it is internal to the Crypto API.
444 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445struct crypto_alg {
446 struct list_head cra_list;
Herbert Xu6bfd4802006-09-21 11:39:29 +1000447 struct list_head cra_users;
448
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 u32 cra_flags;
450 unsigned int cra_blocksize;
451 unsigned int cra_ctxsize;
Herbert Xu95477372005-07-06 13:52:09 -0700452 unsigned int cra_alignmask;
Herbert Xu5cb14542005-11-05 16:58:14 +1100453
454 int cra_priority;
Herbert Xu6521f302006-08-06 20:28:44 +1000455 atomic_t cra_refcnt;
Herbert Xu5cb14542005-11-05 16:58:14 +1100456
Herbert Xud913ea02006-05-21 08:45:26 +1000457 char cra_name[CRYPTO_MAX_ALG_NAME];
458 char cra_driver_name[CRYPTO_MAX_ALG_NAME];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
Herbert Xue853c3c2006-08-22 00:06:54 +1000460 const struct crypto_type *cra_type;
461
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 union {
Herbert Xub5b7f082007-04-16 20:48:54 +1000463 struct ablkcipher_alg ablkcipher;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000464 struct blkcipher_alg blkcipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 struct cipher_alg cipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 struct compress_alg compress;
467 } cra_u;
Herbert Xuc7fc0592006-05-24 13:02:26 +1000468
469 int (*cra_init)(struct crypto_tfm *tfm);
470 void (*cra_exit)(struct crypto_tfm *tfm);
Herbert Xu6521f302006-08-06 20:28:44 +1000471 void (*cra_destroy)(struct crypto_alg *alg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
473 struct module *cra_module;
Herbert Xuedf18b92015-06-18 14:00:48 +0800474} CRYPTO_MINALIGN_ATTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
476/*
Jaegeuk Kim8dec0742017-06-22 12:14:40 -0700477 * A helper struct for waiting for completion of async crypto ops
478 */
479struct crypto_wait {
480 struct completion completion;
481 int err;
482};
483
484/*
485 * Macro for declaring a crypto op async wait object on stack
486 */
487#define DECLARE_CRYPTO_WAIT(_wait) \
488 struct crypto_wait _wait = { \
489 COMPLETION_INITIALIZER_ONSTACK((_wait).completion), 0 }
490
491/*
492 * Async ops completion helper functioons
493 */
494void crypto_req_done(struct crypto_async_request *req, int err);
495
496static inline int crypto_wait_req(int err, struct crypto_wait *wait)
497{
498 switch (err) {
499 case -EINPROGRESS:
500 case -EBUSY:
501 wait_for_completion(&wait->completion);
502 reinit_completion(&wait->completion);
503 err = wait->err;
504 break;
505 };
506
507 return err;
508}
509
510static inline void crypto_init_wait(struct crypto_wait *wait)
511{
512 init_completion(&wait->completion);
513}
514
515/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 * Algorithm registration interface.
517 */
518int crypto_register_alg(struct crypto_alg *alg);
519int crypto_unregister_alg(struct crypto_alg *alg);
Mark Brown4b004342012-01-17 23:34:26 +0000520int crypto_register_algs(struct crypto_alg *algs, int count);
521int crypto_unregister_algs(struct crypto_alg *algs, int count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
523/*
524 * Algorithm query interface.
525 */
Herbert Xufce32d72006-08-26 17:35:45 +1000526int crypto_has_alg(const char *name, u32 type, u32 mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
528/*
529 * Transforms: user-instantiated objects which encapsulate algorithms
Herbert Xu6d7d6842006-07-30 11:53:01 +1000530 * and core processing logic. Managed via crypto_alloc_*() and
531 * crypto_free_*(), as well as the various helpers below.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
Herbert Xu32e39832007-03-24 14:35:34 +1100534struct ablkcipher_tfm {
535 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
536 unsigned int keylen);
537 int (*encrypt)(struct ablkcipher_request *req);
538 int (*decrypt)(struct ablkcipher_request *req);
Herbert Xu61da88e2007-12-17 21:51:27 +0800539
Herbert Xuecfc4322007-12-05 21:08:36 +1100540 struct crypto_ablkcipher *base;
541
Herbert Xu32e39832007-03-24 14:35:34 +1100542 unsigned int ivsize;
543 unsigned int reqsize;
544};
545
Herbert Xu5cde0af2006-08-22 00:07:53 +1000546struct blkcipher_tfm {
547 void *iv;
548 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
549 unsigned int keylen);
550 int (*encrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
551 struct scatterlist *src, unsigned int nbytes);
552 int (*decrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
553 struct scatterlist *src, unsigned int nbytes);
554};
555
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556struct cipher_tfm {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 int (*cit_setkey)(struct crypto_tfm *tfm,
558 const u8 *key, unsigned int keylen);
Herbert Xuf28776a2006-08-13 20:58:18 +1000559 void (*cit_encrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
560 void (*cit_decrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561};
562
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563struct compress_tfm {
564 int (*cot_compress)(struct crypto_tfm *tfm,
565 const u8 *src, unsigned int slen,
566 u8 *dst, unsigned int *dlen);
567 int (*cot_decompress)(struct crypto_tfm *tfm,
568 const u8 *src, unsigned int slen,
569 u8 *dst, unsigned int *dlen);
570};
571
Herbert Xu32e39832007-03-24 14:35:34 +1100572#define crt_ablkcipher crt_u.ablkcipher
Herbert Xu5cde0af2006-08-22 00:07:53 +1000573#define crt_blkcipher crt_u.blkcipher
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574#define crt_cipher crt_u.cipher
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575#define crt_compress crt_u.compress
576
577struct crypto_tfm {
578
579 u32 crt_flags;
580
581 union {
Herbert Xu32e39832007-03-24 14:35:34 +1100582 struct ablkcipher_tfm ablkcipher;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000583 struct blkcipher_tfm blkcipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 struct cipher_tfm cipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 struct compress_tfm compress;
586 } crt_u;
Herbert Xu4a779482008-09-13 18:19:03 -0700587
588 void (*exit)(struct crypto_tfm *tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
590 struct crypto_alg *__crt_alg;
Herbert Xuf10b7892006-01-25 22:34:01 +1100591
Herbert Xu79911102006-08-21 21:03:52 +1000592 void *__crt_ctx[] CRYPTO_MINALIGN_ATTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593};
594
Herbert Xu32e39832007-03-24 14:35:34 +1100595struct crypto_ablkcipher {
596 struct crypto_tfm base;
597};
598
Herbert Xu5cde0af2006-08-22 00:07:53 +1000599struct crypto_blkcipher {
600 struct crypto_tfm base;
601};
602
Herbert Xu78a1fe42006-12-24 10:02:00 +1100603struct crypto_cipher {
604 struct crypto_tfm base;
605};
606
607struct crypto_comp {
608 struct crypto_tfm base;
609};
610
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000611enum {
612 CRYPTOA_UNSPEC,
613 CRYPTOA_ALG,
Herbert Xuebc610e2007-01-01 18:37:02 +1100614 CRYPTOA_TYPE,
Herbert Xu39e1ee012007-08-29 19:27:26 +0800615 CRYPTOA_U32,
Herbert Xuebc610e2007-01-01 18:37:02 +1100616 __CRYPTOA_MAX,
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000617};
618
Herbert Xuebc610e2007-01-01 18:37:02 +1100619#define CRYPTOA_MAX (__CRYPTOA_MAX - 1)
620
Herbert Xu39e1ee012007-08-29 19:27:26 +0800621/* Maximum number of (rtattr) parameters for each template. */
622#define CRYPTO_MAX_ATTRS 32
623
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000624struct crypto_attr_alg {
625 char name[CRYPTO_MAX_ALG_NAME];
626};
627
Herbert Xuebc610e2007-01-01 18:37:02 +1100628struct crypto_attr_type {
629 u32 type;
630 u32 mask;
631};
632
Herbert Xu39e1ee012007-08-29 19:27:26 +0800633struct crypto_attr_u32 {
634 u32 num;
635};
636
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637/*
638 * Transform user interface.
639 */
640
Herbert Xu6d7d6842006-07-30 11:53:01 +1000641struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask);
Herbert Xu7b2cd922009-02-05 16:48:24 +1100642void crypto_destroy_tfm(void *mem, struct crypto_tfm *tfm);
643
644static inline void crypto_free_tfm(struct crypto_tfm *tfm)
645{
646 return crypto_destroy_tfm(tfm, tfm);
647}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Herbert Xuda7f0332008-07-31 17:08:25 +0800649int alg_test(const char *driver, const char *alg, u32 type, u32 mask);
650
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651/*
652 * Transform helpers which query the underlying algorithm.
653 */
654static inline const char *crypto_tfm_alg_name(struct crypto_tfm *tfm)
655{
656 return tfm->__crt_alg->cra_name;
657}
658
Michal Ludvigb14cdd62006-07-09 09:02:24 +1000659static inline const char *crypto_tfm_alg_driver_name(struct crypto_tfm *tfm)
660{
661 return tfm->__crt_alg->cra_driver_name;
662}
663
664static inline int crypto_tfm_alg_priority(struct crypto_tfm *tfm)
665{
666 return tfm->__crt_alg->cra_priority;
667}
668
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm)
670{
671 return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK;
672}
673
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm)
675{
676 return tfm->__crt_alg->cra_blocksize;
677}
678
Herbert Xufbdae9f2005-07-06 13:53:29 -0700679static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm *tfm)
680{
681 return tfm->__crt_alg->cra_alignmask;
682}
683
Herbert Xuf28776a2006-08-13 20:58:18 +1000684static inline u32 crypto_tfm_get_flags(struct crypto_tfm *tfm)
685{
686 return tfm->crt_flags;
687}
688
689static inline void crypto_tfm_set_flags(struct crypto_tfm *tfm, u32 flags)
690{
691 tfm->crt_flags |= flags;
692}
693
694static inline void crypto_tfm_clear_flags(struct crypto_tfm *tfm, u32 flags)
695{
696 tfm->crt_flags &= ~flags;
697}
698
Herbert Xu40725182005-07-06 13:51:52 -0700699static inline void *crypto_tfm_ctx(struct crypto_tfm *tfm)
700{
Herbert Xuf10b7892006-01-25 22:34:01 +1100701 return tfm->__crt_ctx;
702}
703
704static inline unsigned int crypto_tfm_ctx_alignment(void)
705{
706 struct crypto_tfm *tfm;
707 return __alignof__(tfm->__crt_ctx);
Herbert Xu40725182005-07-06 13:51:52 -0700708}
709
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710/*
711 * API wrappers.
712 */
Herbert Xu32e39832007-03-24 14:35:34 +1100713static inline struct crypto_ablkcipher *__crypto_ablkcipher_cast(
714 struct crypto_tfm *tfm)
715{
716 return (struct crypto_ablkcipher *)tfm;
717}
718
Herbert Xu378f4f52007-12-17 20:07:31 +0800719static inline u32 crypto_skcipher_type(u32 type)
720{
Herbert Xuecfc4322007-12-05 21:08:36 +1100721 type &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
Herbert Xu378f4f52007-12-17 20:07:31 +0800722 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
723 return type;
724}
725
726static inline u32 crypto_skcipher_mask(u32 mask)
727{
Herbert Xuecfc4322007-12-05 21:08:36 +1100728 mask &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
Herbert Xu378f4f52007-12-17 20:07:31 +0800729 mask |= CRYPTO_ALG_TYPE_BLKCIPHER_MASK;
730 return mask;
731}
732
Stephan Muellerf13ec332014-11-12 05:28:22 +0100733/**
734 * DOC: Asynchronous Block Cipher API
735 *
736 * Asynchronous block cipher API is used with the ciphers of type
737 * CRYPTO_ALG_TYPE_ABLKCIPHER (listed as type "ablkcipher" in /proc/crypto).
738 *
739 * Asynchronous cipher operations imply that the function invocation for a
740 * cipher request returns immediately before the completion of the operation.
741 * The cipher request is scheduled as a separate kernel thread and therefore
742 * load-balanced on the different CPUs via the process scheduler. To allow
743 * the kernel crypto API to inform the caller about the completion of a cipher
744 * request, the caller must provide a callback function. That function is
745 * invoked with the cipher handle when the request completes.
746 *
747 * To support the asynchronous operation, additional information than just the
748 * cipher handle must be supplied to the kernel crypto API. That additional
749 * information is given by filling in the ablkcipher_request data structure.
750 *
751 * For the asynchronous block cipher API, the state is maintained with the tfm
752 * cipher handle. A single tfm can be used across multiple calls and in
753 * parallel. For asynchronous block cipher calls, context data supplied and
754 * only used by the caller can be referenced the request data structure in
755 * addition to the IV used for the cipher request. The maintenance of such
756 * state information would be important for a crypto driver implementer to
757 * have, because when calling the callback function upon completion of the
758 * cipher operation, that callback function may need some information about
759 * which operation just finished if it invoked multiple in parallel. This
760 * state information is unused by the kernel crypto API.
761 */
762
Herbert Xu32e39832007-03-24 14:35:34 +1100763static inline struct crypto_tfm *crypto_ablkcipher_tfm(
764 struct crypto_ablkcipher *tfm)
765{
766 return &tfm->base;
767}
768
Stephan Muellerf13ec332014-11-12 05:28:22 +0100769/**
770 * crypto_free_ablkcipher() - zeroize and free cipher handle
771 * @tfm: cipher handle to be freed
772 */
Herbert Xu32e39832007-03-24 14:35:34 +1100773static inline void crypto_free_ablkcipher(struct crypto_ablkcipher *tfm)
774{
775 crypto_free_tfm(crypto_ablkcipher_tfm(tfm));
776}
777
Stephan Muellerf13ec332014-11-12 05:28:22 +0100778/**
779 * crypto_has_ablkcipher() - Search for the availability of an ablkcipher.
780 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
781 * ablkcipher
782 * @type: specifies the type of the cipher
783 * @mask: specifies the mask for the cipher
784 *
785 * Return: true when the ablkcipher is known to the kernel crypto API; false
786 * otherwise
787 */
Herbert Xu32e39832007-03-24 14:35:34 +1100788static inline int crypto_has_ablkcipher(const char *alg_name, u32 type,
789 u32 mask)
790{
Herbert Xu378f4f52007-12-17 20:07:31 +0800791 return crypto_has_alg(alg_name, crypto_skcipher_type(type),
792 crypto_skcipher_mask(mask));
Herbert Xu32e39832007-03-24 14:35:34 +1100793}
794
795static inline struct ablkcipher_tfm *crypto_ablkcipher_crt(
796 struct crypto_ablkcipher *tfm)
797{
798 return &crypto_ablkcipher_tfm(tfm)->crt_ablkcipher;
799}
800
Stephan Muellerf13ec332014-11-12 05:28:22 +0100801/**
802 * crypto_ablkcipher_ivsize() - obtain IV size
803 * @tfm: cipher handle
804 *
805 * The size of the IV for the ablkcipher referenced by the cipher handle is
806 * returned. This IV size may be zero if the cipher does not need an IV.
807 *
808 * Return: IV size in bytes
809 */
Herbert Xu32e39832007-03-24 14:35:34 +1100810static inline unsigned int crypto_ablkcipher_ivsize(
811 struct crypto_ablkcipher *tfm)
812{
813 return crypto_ablkcipher_crt(tfm)->ivsize;
814}
815
Stephan Muellerf13ec332014-11-12 05:28:22 +0100816/**
817 * crypto_ablkcipher_blocksize() - obtain block size of cipher
818 * @tfm: cipher handle
819 *
820 * The block size for the ablkcipher referenced with the cipher handle is
821 * returned. The caller may use that information to allocate appropriate
822 * memory for the data returned by the encryption or decryption operation
823 *
824 * Return: block size of cipher
825 */
Herbert Xu32e39832007-03-24 14:35:34 +1100826static inline unsigned int crypto_ablkcipher_blocksize(
827 struct crypto_ablkcipher *tfm)
828{
829 return crypto_tfm_alg_blocksize(crypto_ablkcipher_tfm(tfm));
830}
831
832static inline unsigned int crypto_ablkcipher_alignmask(
833 struct crypto_ablkcipher *tfm)
834{
835 return crypto_tfm_alg_alignmask(crypto_ablkcipher_tfm(tfm));
836}
837
838static inline u32 crypto_ablkcipher_get_flags(struct crypto_ablkcipher *tfm)
839{
840 return crypto_tfm_get_flags(crypto_ablkcipher_tfm(tfm));
841}
842
843static inline void crypto_ablkcipher_set_flags(struct crypto_ablkcipher *tfm,
844 u32 flags)
845{
846 crypto_tfm_set_flags(crypto_ablkcipher_tfm(tfm), flags);
847}
848
849static inline void crypto_ablkcipher_clear_flags(struct crypto_ablkcipher *tfm,
850 u32 flags)
851{
852 crypto_tfm_clear_flags(crypto_ablkcipher_tfm(tfm), flags);
853}
854
Stephan Muellerf13ec332014-11-12 05:28:22 +0100855/**
856 * crypto_ablkcipher_setkey() - set key for cipher
857 * @tfm: cipher handle
858 * @key: buffer holding the key
859 * @keylen: length of the key in bytes
860 *
861 * The caller provided key is set for the ablkcipher referenced by the cipher
862 * handle.
863 *
864 * Note, the key length determines the cipher type. Many block ciphers implement
865 * different cipher modes depending on the key size, such as AES-128 vs AES-192
866 * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
867 * is performed.
868 *
869 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
870 */
Herbert Xu32e39832007-03-24 14:35:34 +1100871static inline int crypto_ablkcipher_setkey(struct crypto_ablkcipher *tfm,
872 const u8 *key, unsigned int keylen)
873{
Herbert Xuecfc4322007-12-05 21:08:36 +1100874 struct ablkcipher_tfm *crt = crypto_ablkcipher_crt(tfm);
875
876 return crt->setkey(crt->base, key, keylen);
Herbert Xu32e39832007-03-24 14:35:34 +1100877}
878
Stephan Muellerf13ec332014-11-12 05:28:22 +0100879/**
880 * crypto_ablkcipher_reqtfm() - obtain cipher handle from request
881 * @req: ablkcipher_request out of which the cipher handle is to be obtained
882 *
883 * Return the crypto_ablkcipher handle when furnishing an ablkcipher_request
884 * data structure.
885 *
886 * Return: crypto_ablkcipher handle
887 */
Herbert Xu32e39832007-03-24 14:35:34 +1100888static inline struct crypto_ablkcipher *crypto_ablkcipher_reqtfm(
889 struct ablkcipher_request *req)
890{
891 return __crypto_ablkcipher_cast(req->base.tfm);
892}
893
Stephan Muellerf13ec332014-11-12 05:28:22 +0100894/**
895 * crypto_ablkcipher_encrypt() - encrypt plaintext
896 * @req: reference to the ablkcipher_request handle that holds all information
897 * needed to perform the cipher operation
898 *
899 * Encrypt plaintext data using the ablkcipher_request handle. That data
900 * structure and how it is filled with data is discussed with the
901 * ablkcipher_request_* functions.
902 *
903 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
904 */
Herbert Xu32e39832007-03-24 14:35:34 +1100905static inline int crypto_ablkcipher_encrypt(struct ablkcipher_request *req)
906{
907 struct ablkcipher_tfm *crt =
908 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
909 return crt->encrypt(req);
910}
911
Stephan Muellerf13ec332014-11-12 05:28:22 +0100912/**
913 * crypto_ablkcipher_decrypt() - decrypt ciphertext
914 * @req: reference to the ablkcipher_request handle that holds all information
915 * needed to perform the cipher operation
916 *
917 * Decrypt ciphertext data using the ablkcipher_request handle. That data
918 * structure and how it is filled with data is discussed with the
919 * ablkcipher_request_* functions.
920 *
921 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
922 */
Herbert Xu32e39832007-03-24 14:35:34 +1100923static inline int crypto_ablkcipher_decrypt(struct ablkcipher_request *req)
924{
925 struct ablkcipher_tfm *crt =
926 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
927 return crt->decrypt(req);
928}
929
Stephan Muellerf13ec332014-11-12 05:28:22 +0100930/**
931 * DOC: Asynchronous Cipher Request Handle
932 *
933 * The ablkcipher_request data structure contains all pointers to data
934 * required for the asynchronous cipher operation. This includes the cipher
935 * handle (which can be used by multiple ablkcipher_request instances), pointer
936 * to plaintext and ciphertext, asynchronous callback function, etc. It acts
937 * as a handle to the ablkcipher_request_* API calls in a similar way as
938 * ablkcipher handle to the crypto_ablkcipher_* API calls.
939 */
940
941/**
942 * crypto_ablkcipher_reqsize() - obtain size of the request data structure
943 * @tfm: cipher handle
944 *
945 * Return: number of bytes
946 */
Herbert Xub16c3a22007-08-29 19:02:04 +0800947static inline unsigned int crypto_ablkcipher_reqsize(
948 struct crypto_ablkcipher *tfm)
Herbert Xu32e39832007-03-24 14:35:34 +1100949{
950 return crypto_ablkcipher_crt(tfm)->reqsize;
951}
952
Stephan Muellerf13ec332014-11-12 05:28:22 +0100953/**
954 * ablkcipher_request_set_tfm() - update cipher handle reference in request
955 * @req: request handle to be modified
956 * @tfm: cipher handle that shall be added to the request handle
957 *
958 * Allow the caller to replace the existing ablkcipher handle in the request
959 * data structure with a different one.
960 */
Herbert Xue196d622007-04-14 16:09:14 +1000961static inline void ablkcipher_request_set_tfm(
962 struct ablkcipher_request *req, struct crypto_ablkcipher *tfm)
963{
Herbert Xuecfc4322007-12-05 21:08:36 +1100964 req->base.tfm = crypto_ablkcipher_tfm(crypto_ablkcipher_crt(tfm)->base);
Herbert Xue196d622007-04-14 16:09:14 +1000965}
966
Herbert Xub5b7f082007-04-16 20:48:54 +1000967static inline struct ablkcipher_request *ablkcipher_request_cast(
968 struct crypto_async_request *req)
969{
970 return container_of(req, struct ablkcipher_request, base);
971}
972
Stephan Muellerf13ec332014-11-12 05:28:22 +0100973/**
974 * ablkcipher_request_alloc() - allocate request data structure
975 * @tfm: cipher handle to be registered with the request
976 * @gfp: memory allocation flag that is handed to kmalloc by the API call.
977 *
978 * Allocate the request data structure that must be used with the ablkcipher
979 * encrypt and decrypt API calls. During the allocation, the provided ablkcipher
980 * handle is registered in the request data structure.
981 *
Eric Biggers6eae29e2016-04-02 10:54:56 -0500982 * Return: allocated request handle in case of success, or NULL if out of memory
Stephan Muellerf13ec332014-11-12 05:28:22 +0100983 */
Herbert Xu32e39832007-03-24 14:35:34 +1100984static inline struct ablkcipher_request *ablkcipher_request_alloc(
985 struct crypto_ablkcipher *tfm, gfp_t gfp)
986{
987 struct ablkcipher_request *req;
988
989 req = kmalloc(sizeof(struct ablkcipher_request) +
990 crypto_ablkcipher_reqsize(tfm), gfp);
991
992 if (likely(req))
Herbert Xue196d622007-04-14 16:09:14 +1000993 ablkcipher_request_set_tfm(req, tfm);
Herbert Xu32e39832007-03-24 14:35:34 +1100994
995 return req;
996}
997
Stephan Muellerf13ec332014-11-12 05:28:22 +0100998/**
999 * ablkcipher_request_free() - zeroize and free request data structure
1000 * @req: request data structure cipher handle to be freed
1001 */
Herbert Xu32e39832007-03-24 14:35:34 +11001002static inline void ablkcipher_request_free(struct ablkcipher_request *req)
1003{
Herbert Xuaef73cf2009-07-11 22:22:14 +08001004 kzfree(req);
Herbert Xu32e39832007-03-24 14:35:34 +11001005}
1006
Stephan Muellerf13ec332014-11-12 05:28:22 +01001007/**
1008 * ablkcipher_request_set_callback() - set asynchronous callback function
1009 * @req: request handle
1010 * @flags: specify zero or an ORing of the flags
1011 * CRYPTO_TFM_REQ_MAY_BACKLOG the request queue may back log and
1012 * increase the wait queue beyond the initial maximum size;
1013 * CRYPTO_TFM_REQ_MAY_SLEEP the request processing may sleep
1014 * @compl: callback function pointer to be registered with the request handle
1015 * @data: The data pointer refers to memory that is not used by the kernel
1016 * crypto API, but provided to the callback function for it to use. Here,
1017 * the caller can provide a reference to memory the callback function can
1018 * operate on. As the callback function is invoked asynchronously to the
1019 * related functionality, it may need to access data structures of the
1020 * related functionality which can be referenced using this pointer. The
1021 * callback function can access the memory via the "data" field in the
1022 * crypto_async_request data structure provided to the callback function.
1023 *
1024 * This function allows setting the callback function that is triggered once the
1025 * cipher operation completes.
1026 *
1027 * The callback function is registered with the ablkcipher_request handle and
Stephan Mueller379dcfb2015-01-19 00:13:39 +01001028 * must comply with the following template
Stephan Muellerf13ec332014-11-12 05:28:22 +01001029 *
1030 * void callback_function(struct crypto_async_request *req, int error)
1031 */
Herbert Xu32e39832007-03-24 14:35:34 +11001032static inline void ablkcipher_request_set_callback(
1033 struct ablkcipher_request *req,
Mark Rustad3e3dc252014-07-25 02:53:38 -07001034 u32 flags, crypto_completion_t compl, void *data)
Herbert Xu32e39832007-03-24 14:35:34 +11001035{
Mark Rustad3e3dc252014-07-25 02:53:38 -07001036 req->base.complete = compl;
Herbert Xu32e39832007-03-24 14:35:34 +11001037 req->base.data = data;
1038 req->base.flags = flags;
1039}
1040
Stephan Muellerf13ec332014-11-12 05:28:22 +01001041/**
1042 * ablkcipher_request_set_crypt() - set data buffers
1043 * @req: request handle
1044 * @src: source scatter / gather list
1045 * @dst: destination scatter / gather list
1046 * @nbytes: number of bytes to process from @src
1047 * @iv: IV for the cipher operation which must comply with the IV size defined
1048 * by crypto_ablkcipher_ivsize
1049 *
1050 * This function allows setting of the source data and destination data
1051 * scatter / gather lists.
1052 *
1053 * For encryption, the source is treated as the plaintext and the
1054 * destination is the ciphertext. For a decryption operation, the use is
Stephan Mueller379dcfb2015-01-19 00:13:39 +01001055 * reversed - the source is the ciphertext and the destination is the plaintext.
Stephan Muellerf13ec332014-11-12 05:28:22 +01001056 */
Herbert Xu32e39832007-03-24 14:35:34 +11001057static inline void ablkcipher_request_set_crypt(
1058 struct ablkcipher_request *req,
1059 struct scatterlist *src, struct scatterlist *dst,
1060 unsigned int nbytes, void *iv)
1061{
1062 req->src = src;
1063 req->dst = dst;
1064 req->nbytes = nbytes;
1065 req->info = iv;
1066}
1067
Stephan Muellerfced7b02014-11-12 05:29:00 +01001068/**
Stephan Mueller58284f02014-11-12 05:29:36 +01001069 * DOC: Synchronous Block Cipher API
1070 *
1071 * The synchronous block cipher API is used with the ciphers of type
1072 * CRYPTO_ALG_TYPE_BLKCIPHER (listed as type "blkcipher" in /proc/crypto)
1073 *
1074 * Synchronous calls, have a context in the tfm. But since a single tfm can be
1075 * used in multiple calls and in parallel, this info should not be changeable
1076 * (unless a lock is used). This applies, for example, to the symmetric key.
1077 * However, the IV is changeable, so there is an iv field in blkcipher_tfm
1078 * structure for synchronous blkcipher api. So, its the only state info that can
1079 * be kept for synchronous calls without using a big lock across a tfm.
1080 *
1081 * The block cipher API allows the use of a complete cipher, i.e. a cipher
1082 * consisting of a template (a block chaining mode) and a single block cipher
1083 * primitive (e.g. AES).
1084 *
1085 * The plaintext data buffer and the ciphertext data buffer are pointed to
1086 * by using scatter/gather lists. The cipher operation is performed
1087 * on all segments of the provided scatter/gather lists.
1088 *
1089 * The kernel crypto API supports a cipher operation "in-place" which means that
1090 * the caller may provide the same scatter/gather list for the plaintext and
1091 * cipher text. After the completion of the cipher operation, the plaintext
1092 * data is replaced with the ciphertext data in case of an encryption and vice
1093 * versa for a decryption. The caller must ensure that the scatter/gather lists
1094 * for the output data point to sufficiently large buffers, i.e. multiples of
1095 * the block size of the cipher.
1096 */
1097
Herbert Xu5cde0af2006-08-22 00:07:53 +10001098static inline struct crypto_blkcipher *__crypto_blkcipher_cast(
1099 struct crypto_tfm *tfm)
1100{
1101 return (struct crypto_blkcipher *)tfm;
1102}
1103
1104static inline struct crypto_blkcipher *crypto_blkcipher_cast(
1105 struct crypto_tfm *tfm)
1106{
1107 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_BLKCIPHER);
1108 return __crypto_blkcipher_cast(tfm);
1109}
1110
Stephan Mueller58284f02014-11-12 05:29:36 +01001111/**
1112 * crypto_alloc_blkcipher() - allocate synchronous block cipher handle
1113 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1114 * blkcipher cipher
1115 * @type: specifies the type of the cipher
1116 * @mask: specifies the mask for the cipher
1117 *
1118 * Allocate a cipher handle for a block cipher. The returned struct
1119 * crypto_blkcipher is the cipher handle that is required for any subsequent
1120 * API invocation for that block cipher.
1121 *
1122 * Return: allocated cipher handle in case of success; IS_ERR() is true in case
1123 * of an error, PTR_ERR() returns the error code.
1124 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001125static inline struct crypto_blkcipher *crypto_alloc_blkcipher(
1126 const char *alg_name, u32 type, u32 mask)
1127{
Herbert Xu332f88402007-11-15 22:36:07 +08001128 type &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xu5cde0af2006-08-22 00:07:53 +10001129 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
Herbert Xu332f88402007-11-15 22:36:07 +08001130 mask |= CRYPTO_ALG_TYPE_MASK;
Herbert Xu5cde0af2006-08-22 00:07:53 +10001131
1132 return __crypto_blkcipher_cast(crypto_alloc_base(alg_name, type, mask));
1133}
1134
1135static inline struct crypto_tfm *crypto_blkcipher_tfm(
1136 struct crypto_blkcipher *tfm)
1137{
1138 return &tfm->base;
1139}
1140
Stephan Mueller58284f02014-11-12 05:29:36 +01001141/**
1142 * crypto_free_blkcipher() - zeroize and free the block cipher handle
1143 * @tfm: cipher handle to be freed
1144 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001145static inline void crypto_free_blkcipher(struct crypto_blkcipher *tfm)
1146{
1147 crypto_free_tfm(crypto_blkcipher_tfm(tfm));
1148}
1149
Stephan Mueller58284f02014-11-12 05:29:36 +01001150/**
1151 * crypto_has_blkcipher() - Search for the availability of a block cipher
1152 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1153 * block cipher
1154 * @type: specifies the type of the cipher
1155 * @mask: specifies the mask for the cipher
1156 *
1157 * Return: true when the block cipher is known to the kernel crypto API; false
1158 * otherwise
1159 */
Herbert Xufce32d72006-08-26 17:35:45 +10001160static inline int crypto_has_blkcipher(const char *alg_name, u32 type, u32 mask)
1161{
Herbert Xu332f88402007-11-15 22:36:07 +08001162 type &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xufce32d72006-08-26 17:35:45 +10001163 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
Herbert Xu332f88402007-11-15 22:36:07 +08001164 mask |= CRYPTO_ALG_TYPE_MASK;
Herbert Xufce32d72006-08-26 17:35:45 +10001165
1166 return crypto_has_alg(alg_name, type, mask);
1167}
1168
Stephan Mueller58284f02014-11-12 05:29:36 +01001169/**
1170 * crypto_blkcipher_name() - return the name / cra_name from the cipher handle
1171 * @tfm: cipher handle
1172 *
1173 * Return: The character string holding the name of the cipher
1174 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001175static inline const char *crypto_blkcipher_name(struct crypto_blkcipher *tfm)
1176{
1177 return crypto_tfm_alg_name(crypto_blkcipher_tfm(tfm));
1178}
1179
1180static inline struct blkcipher_tfm *crypto_blkcipher_crt(
1181 struct crypto_blkcipher *tfm)
1182{
1183 return &crypto_blkcipher_tfm(tfm)->crt_blkcipher;
1184}
1185
1186static inline struct blkcipher_alg *crypto_blkcipher_alg(
1187 struct crypto_blkcipher *tfm)
1188{
1189 return &crypto_blkcipher_tfm(tfm)->__crt_alg->cra_blkcipher;
1190}
1191
Stephan Mueller58284f02014-11-12 05:29:36 +01001192/**
1193 * crypto_blkcipher_ivsize() - obtain IV size
1194 * @tfm: cipher handle
1195 *
1196 * The size of the IV for the block cipher referenced by the cipher handle is
1197 * returned. This IV size may be zero if the cipher does not need an IV.
1198 *
1199 * Return: IV size in bytes
1200 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001201static inline unsigned int crypto_blkcipher_ivsize(struct crypto_blkcipher *tfm)
1202{
1203 return crypto_blkcipher_alg(tfm)->ivsize;
1204}
1205
Stephan Mueller58284f02014-11-12 05:29:36 +01001206/**
1207 * crypto_blkcipher_blocksize() - obtain block size of cipher
1208 * @tfm: cipher handle
1209 *
1210 * The block size for the block cipher referenced with the cipher handle is
1211 * returned. The caller may use that information to allocate appropriate
1212 * memory for the data returned by the encryption or decryption operation.
1213 *
1214 * Return: block size of cipher
1215 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001216static inline unsigned int crypto_blkcipher_blocksize(
1217 struct crypto_blkcipher *tfm)
1218{
1219 return crypto_tfm_alg_blocksize(crypto_blkcipher_tfm(tfm));
1220}
1221
1222static inline unsigned int crypto_blkcipher_alignmask(
1223 struct crypto_blkcipher *tfm)
1224{
1225 return crypto_tfm_alg_alignmask(crypto_blkcipher_tfm(tfm));
1226}
1227
1228static inline u32 crypto_blkcipher_get_flags(struct crypto_blkcipher *tfm)
1229{
1230 return crypto_tfm_get_flags(crypto_blkcipher_tfm(tfm));
1231}
1232
1233static inline void crypto_blkcipher_set_flags(struct crypto_blkcipher *tfm,
1234 u32 flags)
1235{
1236 crypto_tfm_set_flags(crypto_blkcipher_tfm(tfm), flags);
1237}
1238
1239static inline void crypto_blkcipher_clear_flags(struct crypto_blkcipher *tfm,
1240 u32 flags)
1241{
1242 crypto_tfm_clear_flags(crypto_blkcipher_tfm(tfm), flags);
1243}
1244
Stephan Mueller58284f02014-11-12 05:29:36 +01001245/**
1246 * crypto_blkcipher_setkey() - set key for cipher
1247 * @tfm: cipher handle
1248 * @key: buffer holding the key
1249 * @keylen: length of the key in bytes
1250 *
1251 * The caller provided key is set for the block cipher referenced by the cipher
1252 * handle.
1253 *
1254 * Note, the key length determines the cipher type. Many block ciphers implement
1255 * different cipher modes depending on the key size, such as AES-128 vs AES-192
1256 * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
1257 * is performed.
1258 *
1259 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
1260 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001261static inline int crypto_blkcipher_setkey(struct crypto_blkcipher *tfm,
1262 const u8 *key, unsigned int keylen)
1263{
1264 return crypto_blkcipher_crt(tfm)->setkey(crypto_blkcipher_tfm(tfm),
1265 key, keylen);
1266}
1267
Stephan Mueller58284f02014-11-12 05:29:36 +01001268/**
1269 * crypto_blkcipher_encrypt() - encrypt plaintext
1270 * @desc: reference to the block cipher handle with meta data
1271 * @dst: scatter/gather list that is filled by the cipher operation with the
1272 * ciphertext
1273 * @src: scatter/gather list that holds the plaintext
1274 * @nbytes: number of bytes of the plaintext to encrypt.
1275 *
1276 * Encrypt plaintext data using the IV set by the caller with a preceding
1277 * call of crypto_blkcipher_set_iv.
1278 *
1279 * The blkcipher_desc data structure must be filled by the caller and can
1280 * reside on the stack. The caller must fill desc as follows: desc.tfm is filled
1281 * with the block cipher handle; desc.flags is filled with either
1282 * CRYPTO_TFM_REQ_MAY_SLEEP or 0.
1283 *
1284 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1285 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001286static inline int crypto_blkcipher_encrypt(struct blkcipher_desc *desc,
1287 struct scatterlist *dst,
1288 struct scatterlist *src,
1289 unsigned int nbytes)
1290{
1291 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
1292 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
1293}
1294
Stephan Mueller58284f02014-11-12 05:29:36 +01001295/**
1296 * crypto_blkcipher_encrypt_iv() - encrypt plaintext with dedicated IV
1297 * @desc: reference to the block cipher handle with meta data
1298 * @dst: scatter/gather list that is filled by the cipher operation with the
1299 * ciphertext
1300 * @src: scatter/gather list that holds the plaintext
1301 * @nbytes: number of bytes of the plaintext to encrypt.
1302 *
1303 * Encrypt plaintext data with the use of an IV that is solely used for this
1304 * cipher operation. Any previously set IV is not used.
1305 *
1306 * The blkcipher_desc data structure must be filled by the caller and can
1307 * reside on the stack. The caller must fill desc as follows: desc.tfm is filled
1308 * with the block cipher handle; desc.info is filled with the IV to be used for
1309 * the current operation; desc.flags is filled with either
1310 * CRYPTO_TFM_REQ_MAY_SLEEP or 0.
1311 *
1312 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1313 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001314static inline int crypto_blkcipher_encrypt_iv(struct blkcipher_desc *desc,
1315 struct scatterlist *dst,
1316 struct scatterlist *src,
1317 unsigned int nbytes)
1318{
1319 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
1320}
1321
Stephan Mueller58284f02014-11-12 05:29:36 +01001322/**
1323 * crypto_blkcipher_decrypt() - decrypt ciphertext
1324 * @desc: reference to the block cipher handle with meta data
1325 * @dst: scatter/gather list that is filled by the cipher operation with the
1326 * plaintext
1327 * @src: scatter/gather list that holds the ciphertext
1328 * @nbytes: number of bytes of the ciphertext to decrypt.
1329 *
1330 * Decrypt ciphertext data using the IV set by the caller with a preceding
1331 * call of crypto_blkcipher_set_iv.
1332 *
1333 * The blkcipher_desc data structure must be filled by the caller as documented
1334 * for the crypto_blkcipher_encrypt call above.
1335 *
1336 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1337 *
1338 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001339static inline int crypto_blkcipher_decrypt(struct blkcipher_desc *desc,
1340 struct scatterlist *dst,
1341 struct scatterlist *src,
1342 unsigned int nbytes)
1343{
1344 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
1345 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
1346}
1347
Stephan Mueller58284f02014-11-12 05:29:36 +01001348/**
1349 * crypto_blkcipher_decrypt_iv() - decrypt ciphertext with dedicated IV
1350 * @desc: reference to the block cipher handle with meta data
1351 * @dst: scatter/gather list that is filled by the cipher operation with the
1352 * plaintext
1353 * @src: scatter/gather list that holds the ciphertext
1354 * @nbytes: number of bytes of the ciphertext to decrypt.
1355 *
1356 * Decrypt ciphertext data with the use of an IV that is solely used for this
1357 * cipher operation. Any previously set IV is not used.
1358 *
1359 * The blkcipher_desc data structure must be filled by the caller as documented
1360 * for the crypto_blkcipher_encrypt_iv call above.
1361 *
1362 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1363 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001364static inline int crypto_blkcipher_decrypt_iv(struct blkcipher_desc *desc,
1365 struct scatterlist *dst,
1366 struct scatterlist *src,
1367 unsigned int nbytes)
1368{
1369 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
1370}
1371
Stephan Mueller58284f02014-11-12 05:29:36 +01001372/**
1373 * crypto_blkcipher_set_iv() - set IV for cipher
1374 * @tfm: cipher handle
1375 * @src: buffer holding the IV
1376 * @len: length of the IV in bytes
1377 *
1378 * The caller provided IV is set for the block cipher referenced by the cipher
1379 * handle.
1380 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001381static inline void crypto_blkcipher_set_iv(struct crypto_blkcipher *tfm,
1382 const u8 *src, unsigned int len)
1383{
1384 memcpy(crypto_blkcipher_crt(tfm)->iv, src, len);
1385}
1386
Stephan Mueller58284f02014-11-12 05:29:36 +01001387/**
1388 * crypto_blkcipher_get_iv() - obtain IV from cipher
1389 * @tfm: cipher handle
1390 * @dst: buffer filled with the IV
1391 * @len: length of the buffer dst
1392 *
1393 * The caller can obtain the IV set for the block cipher referenced by the
1394 * cipher handle and store it into the user-provided buffer. If the buffer
1395 * has an insufficient space, the IV is truncated to fit the buffer.
1396 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001397static inline void crypto_blkcipher_get_iv(struct crypto_blkcipher *tfm,
1398 u8 *dst, unsigned int len)
1399{
1400 memcpy(dst, crypto_blkcipher_crt(tfm)->iv, len);
1401}
1402
Stephan Mueller16e61032014-11-12 05:30:06 +01001403/**
1404 * DOC: Single Block Cipher API
1405 *
1406 * The single block cipher API is used with the ciphers of type
1407 * CRYPTO_ALG_TYPE_CIPHER (listed as type "cipher" in /proc/crypto).
1408 *
1409 * Using the single block cipher API calls, operations with the basic cipher
1410 * primitive can be implemented. These cipher primitives exclude any block
1411 * chaining operations including IV handling.
1412 *
1413 * The purpose of this single block cipher API is to support the implementation
1414 * of templates or other concepts that only need to perform the cipher operation
1415 * on one block at a time. Templates invoke the underlying cipher primitive
1416 * block-wise and process either the input or the output data of these cipher
1417 * operations.
1418 */
1419
Herbert Xuf28776a2006-08-13 20:58:18 +10001420static inline struct crypto_cipher *__crypto_cipher_cast(struct crypto_tfm *tfm)
1421{
1422 return (struct crypto_cipher *)tfm;
1423}
1424
1425static inline struct crypto_cipher *crypto_cipher_cast(struct crypto_tfm *tfm)
1426{
1427 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
1428 return __crypto_cipher_cast(tfm);
1429}
1430
Stephan Mueller16e61032014-11-12 05:30:06 +01001431/**
1432 * crypto_alloc_cipher() - allocate single block cipher handle
1433 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1434 * single block cipher
1435 * @type: specifies the type of the cipher
1436 * @mask: specifies the mask for the cipher
1437 *
1438 * Allocate a cipher handle for a single block cipher. The returned struct
1439 * crypto_cipher is the cipher handle that is required for any subsequent API
1440 * invocation for that single block cipher.
1441 *
1442 * Return: allocated cipher handle in case of success; IS_ERR() is true in case
1443 * of an error, PTR_ERR() returns the error code.
1444 */
Herbert Xuf28776a2006-08-13 20:58:18 +10001445static inline struct crypto_cipher *crypto_alloc_cipher(const char *alg_name,
1446 u32 type, u32 mask)
1447{
1448 type &= ~CRYPTO_ALG_TYPE_MASK;
1449 type |= CRYPTO_ALG_TYPE_CIPHER;
1450 mask |= CRYPTO_ALG_TYPE_MASK;
1451
1452 return __crypto_cipher_cast(crypto_alloc_base(alg_name, type, mask));
1453}
1454
1455static inline struct crypto_tfm *crypto_cipher_tfm(struct crypto_cipher *tfm)
1456{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001457 return &tfm->base;
Herbert Xuf28776a2006-08-13 20:58:18 +10001458}
1459
Stephan Mueller16e61032014-11-12 05:30:06 +01001460/**
1461 * crypto_free_cipher() - zeroize and free the single block cipher handle
1462 * @tfm: cipher handle to be freed
1463 */
Herbert Xuf28776a2006-08-13 20:58:18 +10001464static inline void crypto_free_cipher(struct crypto_cipher *tfm)
1465{
1466 crypto_free_tfm(crypto_cipher_tfm(tfm));
1467}
1468
Stephan Mueller16e61032014-11-12 05:30:06 +01001469/**
1470 * crypto_has_cipher() - Search for the availability of a single block cipher
1471 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1472 * single block cipher
1473 * @type: specifies the type of the cipher
1474 * @mask: specifies the mask for the cipher
1475 *
1476 * Return: true when the single block cipher is known to the kernel crypto API;
1477 * false otherwise
1478 */
Herbert Xufce32d72006-08-26 17:35:45 +10001479static inline int crypto_has_cipher(const char *alg_name, u32 type, u32 mask)
1480{
1481 type &= ~CRYPTO_ALG_TYPE_MASK;
1482 type |= CRYPTO_ALG_TYPE_CIPHER;
1483 mask |= CRYPTO_ALG_TYPE_MASK;
1484
1485 return crypto_has_alg(alg_name, type, mask);
1486}
1487
Herbert Xuf28776a2006-08-13 20:58:18 +10001488static inline struct cipher_tfm *crypto_cipher_crt(struct crypto_cipher *tfm)
1489{
1490 return &crypto_cipher_tfm(tfm)->crt_cipher;
1491}
1492
Stephan Mueller16e61032014-11-12 05:30:06 +01001493/**
1494 * crypto_cipher_blocksize() - obtain block size for cipher
1495 * @tfm: cipher handle
1496 *
1497 * The block size for the single block cipher referenced with the cipher handle
1498 * tfm is returned. The caller may use that information to allocate appropriate
1499 * memory for the data returned by the encryption or decryption operation
1500 *
1501 * Return: block size of cipher
1502 */
Herbert Xuf28776a2006-08-13 20:58:18 +10001503static inline unsigned int crypto_cipher_blocksize(struct crypto_cipher *tfm)
1504{
1505 return crypto_tfm_alg_blocksize(crypto_cipher_tfm(tfm));
1506}
1507
1508static inline unsigned int crypto_cipher_alignmask(struct crypto_cipher *tfm)
1509{
1510 return crypto_tfm_alg_alignmask(crypto_cipher_tfm(tfm));
1511}
1512
1513static inline u32 crypto_cipher_get_flags(struct crypto_cipher *tfm)
1514{
1515 return crypto_tfm_get_flags(crypto_cipher_tfm(tfm));
1516}
1517
1518static inline void crypto_cipher_set_flags(struct crypto_cipher *tfm,
1519 u32 flags)
1520{
1521 crypto_tfm_set_flags(crypto_cipher_tfm(tfm), flags);
1522}
1523
1524static inline void crypto_cipher_clear_flags(struct crypto_cipher *tfm,
1525 u32 flags)
1526{
1527 crypto_tfm_clear_flags(crypto_cipher_tfm(tfm), flags);
1528}
1529
Stephan Mueller16e61032014-11-12 05:30:06 +01001530/**
1531 * crypto_cipher_setkey() - set key for cipher
1532 * @tfm: cipher handle
1533 * @key: buffer holding the key
1534 * @keylen: length of the key in bytes
1535 *
1536 * The caller provided key is set for the single block cipher referenced by the
1537 * cipher handle.
1538 *
1539 * Note, the key length determines the cipher type. Many block ciphers implement
1540 * different cipher modes depending on the key size, such as AES-128 vs AES-192
1541 * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
1542 * is performed.
1543 *
1544 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
1545 */
Herbert Xu7226bc872006-08-21 21:40:49 +10001546static inline int crypto_cipher_setkey(struct crypto_cipher *tfm,
1547 const u8 *key, unsigned int keylen)
1548{
1549 return crypto_cipher_crt(tfm)->cit_setkey(crypto_cipher_tfm(tfm),
1550 key, keylen);
1551}
1552
Stephan Mueller16e61032014-11-12 05:30:06 +01001553/**
1554 * crypto_cipher_encrypt_one() - encrypt one block of plaintext
1555 * @tfm: cipher handle
1556 * @dst: points to the buffer that will be filled with the ciphertext
1557 * @src: buffer holding the plaintext to be encrypted
1558 *
1559 * Invoke the encryption operation of one block. The caller must ensure that
1560 * the plaintext and ciphertext buffers are at least one block in size.
1561 */
Herbert Xuf28776a2006-08-13 20:58:18 +10001562static inline void crypto_cipher_encrypt_one(struct crypto_cipher *tfm,
1563 u8 *dst, const u8 *src)
1564{
1565 crypto_cipher_crt(tfm)->cit_encrypt_one(crypto_cipher_tfm(tfm),
1566 dst, src);
1567}
1568
Stephan Mueller16e61032014-11-12 05:30:06 +01001569/**
1570 * crypto_cipher_decrypt_one() - decrypt one block of ciphertext
1571 * @tfm: cipher handle
1572 * @dst: points to the buffer that will be filled with the plaintext
1573 * @src: buffer holding the ciphertext to be decrypted
1574 *
1575 * Invoke the decryption operation of one block. The caller must ensure that
1576 * the plaintext and ciphertext buffers are at least one block in size.
1577 */
Herbert Xuf28776a2006-08-13 20:58:18 +10001578static inline void crypto_cipher_decrypt_one(struct crypto_cipher *tfm,
1579 u8 *dst, const u8 *src)
1580{
1581 crypto_cipher_crt(tfm)->cit_decrypt_one(crypto_cipher_tfm(tfm),
1582 dst, src);
1583}
1584
Herbert Xufce32d72006-08-26 17:35:45 +10001585static inline struct crypto_comp *__crypto_comp_cast(struct crypto_tfm *tfm)
1586{
1587 return (struct crypto_comp *)tfm;
1588}
1589
1590static inline struct crypto_comp *crypto_comp_cast(struct crypto_tfm *tfm)
1591{
1592 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_COMPRESS) &
1593 CRYPTO_ALG_TYPE_MASK);
1594 return __crypto_comp_cast(tfm);
1595}
1596
1597static inline struct crypto_comp *crypto_alloc_comp(const char *alg_name,
1598 u32 type, u32 mask)
1599{
1600 type &= ~CRYPTO_ALG_TYPE_MASK;
1601 type |= CRYPTO_ALG_TYPE_COMPRESS;
1602 mask |= CRYPTO_ALG_TYPE_MASK;
1603
1604 return __crypto_comp_cast(crypto_alloc_base(alg_name, type, mask));
1605}
1606
1607static inline struct crypto_tfm *crypto_comp_tfm(struct crypto_comp *tfm)
1608{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001609 return &tfm->base;
Herbert Xufce32d72006-08-26 17:35:45 +10001610}
1611
1612static inline void crypto_free_comp(struct crypto_comp *tfm)
1613{
1614 crypto_free_tfm(crypto_comp_tfm(tfm));
1615}
1616
1617static inline int crypto_has_comp(const char *alg_name, u32 type, u32 mask)
1618{
1619 type &= ~CRYPTO_ALG_TYPE_MASK;
1620 type |= CRYPTO_ALG_TYPE_COMPRESS;
1621 mask |= CRYPTO_ALG_TYPE_MASK;
1622
1623 return crypto_has_alg(alg_name, type, mask);
1624}
1625
Herbert Xue4d5b792006-08-26 18:12:40 +10001626static inline const char *crypto_comp_name(struct crypto_comp *tfm)
1627{
1628 return crypto_tfm_alg_name(crypto_comp_tfm(tfm));
1629}
1630
Herbert Xufce32d72006-08-26 17:35:45 +10001631static inline struct compress_tfm *crypto_comp_crt(struct crypto_comp *tfm)
1632{
1633 return &crypto_comp_tfm(tfm)->crt_compress;
1634}
1635
1636static inline int crypto_comp_compress(struct crypto_comp *tfm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 const u8 *src, unsigned int slen,
1638 u8 *dst, unsigned int *dlen)
1639{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001640 return crypto_comp_crt(tfm)->cot_compress(crypto_comp_tfm(tfm),
1641 src, slen, dst, dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642}
1643
Herbert Xufce32d72006-08-26 17:35:45 +10001644static inline int crypto_comp_decompress(struct crypto_comp *tfm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 const u8 *src, unsigned int slen,
1646 u8 *dst, unsigned int *dlen)
1647{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001648 return crypto_comp_crt(tfm)->cot_decompress(crypto_comp_tfm(tfm),
1649 src, slen, dst, dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650}
1651
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652#endif /* _LINUX_CRYPTO_H */
1653