blob: 0a726257f74ac0730d8408b4c7c098797190ea5f [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/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 * Transform masks and values (for crt_flags).
108 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109#define CRYPTO_TFM_REQ_MASK 0x000fff00
110#define CRYPTO_TFM_RES_MASK 0xfff00000
111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112#define CRYPTO_TFM_REQ_WEAK_KEY 0x00000100
Herbert Xu64baf3c2005-09-01 17:43:05 -0700113#define CRYPTO_TFM_REQ_MAY_SLEEP 0x00000200
Herbert Xu32e39832007-03-24 14:35:34 +1100114#define CRYPTO_TFM_REQ_MAY_BACKLOG 0x00000400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115#define CRYPTO_TFM_RES_WEAK_KEY 0x00100000
116#define CRYPTO_TFM_RES_BAD_KEY_LEN 0x00200000
117#define CRYPTO_TFM_RES_BAD_KEY_SCHED 0x00400000
118#define CRYPTO_TFM_RES_BAD_BLOCK_LEN 0x00800000
119#define CRYPTO_TFM_RES_BAD_FLAGS 0x01000000
120
121/*
122 * Miscellaneous stuff.
123 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124#define CRYPTO_MAX_ALG_NAME 64
125
Herbert Xu79911102006-08-21 21:03:52 +1000126/*
127 * The macro CRYPTO_MINALIGN_ATTR (along with the void * type in the actual
128 * declaration) is used to ensure that the crypto_tfm context structure is
129 * aligned correctly for the given architecture so that there are no alignment
130 * faults for C data types. In particular, this is required on platforms such
131 * as arm where pointers are 32-bit aligned but there are data types such as
132 * u64 which require 64-bit alignment.
133 */
Herbert Xu79911102006-08-21 21:03:52 +1000134#define CRYPTO_MINALIGN ARCH_KMALLOC_MINALIGN
Herbert Xu79911102006-08-21 21:03:52 +1000135
Herbert Xu79911102006-08-21 21:03:52 +1000136#define CRYPTO_MINALIGN_ATTR __attribute__ ((__aligned__(CRYPTO_MINALIGN)))
Herbert Xu79911102006-08-21 21:03:52 +1000137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138struct scatterlist;
Herbert Xu32e39832007-03-24 14:35:34 +1100139struct crypto_ablkcipher;
140struct crypto_async_request;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000141struct crypto_blkcipher;
Herbert Xu40725182005-07-06 13:51:52 -0700142struct crypto_tfm;
Herbert Xue853c3c2006-08-22 00:06:54 +1000143struct crypto_type;
Herbert Xu61da88e2007-12-17 21:51:27 +0800144struct skcipher_givcrypt_request;
Herbert Xu40725182005-07-06 13:51:52 -0700145
Herbert Xu32e39832007-03-24 14:35:34 +1100146typedef void (*crypto_completion_t)(struct crypto_async_request *req, int err);
147
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100148/**
149 * DOC: Block Cipher Context Data Structures
150 *
151 * These data structures define the operating context for each block cipher
152 * type.
153 */
154
Herbert Xu32e39832007-03-24 14:35:34 +1100155struct crypto_async_request {
156 struct list_head list;
157 crypto_completion_t complete;
158 void *data;
159 struct crypto_tfm *tfm;
160
161 u32 flags;
162};
163
164struct ablkcipher_request {
165 struct crypto_async_request base;
166
167 unsigned int nbytes;
168
169 void *info;
170
171 struct scatterlist *src;
172 struct scatterlist *dst;
173
174 void *__ctx[] CRYPTO_MINALIGN_ATTR;
175};
176
Herbert Xu5cde0af2006-08-22 00:07:53 +1000177struct blkcipher_desc {
178 struct crypto_blkcipher *tfm;
179 void *info;
180 u32 flags;
181};
182
Herbert Xu40725182005-07-06 13:51:52 -0700183struct cipher_desc {
184 struct crypto_tfm *tfm;
Herbert Xu6c2bb982006-05-16 22:09:29 +1000185 void (*crfn)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Herbert Xu40725182005-07-06 13:51:52 -0700186 unsigned int (*prfn)(const struct cipher_desc *desc, u8 *dst,
187 const u8 *src, unsigned int nbytes);
188 void *info;
189};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100191/**
192 * DOC: Block Cipher Algorithm Definitions
193 *
194 * These data structures define modular crypto algorithm implementations,
195 * managed via crypto_register_alg() and crypto_unregister_alg().
196 */
197
198/**
199 * struct ablkcipher_alg - asynchronous block cipher definition
200 * @min_keysize: Minimum key size supported by the transformation. This is the
201 * smallest key length supported by this transformation algorithm.
202 * This must be set to one of the pre-defined values as this is
203 * not hardware specific. Possible values for this field can be
204 * found via git grep "_MIN_KEY_SIZE" include/crypto/
205 * @max_keysize: Maximum key size supported by the transformation. This is the
206 * largest key length supported by this transformation algorithm.
207 * This must be set to one of the pre-defined values as this is
208 * not hardware specific. Possible values for this field can be
209 * found via git grep "_MAX_KEY_SIZE" include/crypto/
210 * @setkey: Set key for the transformation. This function is used to either
211 * program a supplied key into the hardware or store the key in the
212 * transformation context for programming it later. Note that this
213 * function does modify the transformation context. This function can
214 * be called multiple times during the existence of the transformation
215 * object, so one must make sure the key is properly reprogrammed into
216 * the hardware. This function is also responsible for checking the key
217 * length for validity. In case a software fallback was put in place in
218 * the @cra_init call, this function might need to use the fallback if
219 * the algorithm doesn't support all of the key sizes.
220 * @encrypt: Encrypt a scatterlist of blocks. This function is used to encrypt
221 * the supplied scatterlist containing the blocks of data. The crypto
222 * API consumer is responsible for aligning the entries of the
223 * scatterlist properly and making sure the chunks are correctly
224 * sized. In case a software fallback was put in place in the
225 * @cra_init call, this function might need to use the fallback if
226 * the algorithm doesn't support all of the key sizes. In case the
227 * key was stored in transformation context, the key might need to be
228 * re-programmed into the hardware in this function. This function
229 * shall not modify the transformation context, as this function may
230 * be called in parallel with the same transformation object.
231 * @decrypt: Decrypt a single block. This is a reverse counterpart to @encrypt
232 * and the conditions are exactly the same.
233 * @givencrypt: Update the IV for encryption. With this function, a cipher
234 * implementation may provide the function on how to update the IV
235 * for encryption.
236 * @givdecrypt: Update the IV for decryption. This is the reverse of
237 * @givencrypt .
238 * @geniv: The transformation implementation may use an "IV generator" provided
239 * by the kernel crypto API. Several use cases have a predefined
240 * approach how IVs are to be updated. For such use cases, the kernel
241 * crypto API provides ready-to-use implementations that can be
242 * referenced with this variable.
243 * @ivsize: IV size applicable for transformation. The consumer must provide an
244 * IV of exactly that size to perform the encrypt or decrypt operation.
245 *
246 * All fields except @givencrypt , @givdecrypt , @geniv and @ivsize are
247 * mandatory and must be filled.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 */
Herbert Xub5b7f082007-04-16 20:48:54 +1000249struct ablkcipher_alg {
250 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
251 unsigned int keylen);
252 int (*encrypt)(struct ablkcipher_request *req);
253 int (*decrypt)(struct ablkcipher_request *req);
Herbert Xu61da88e2007-12-17 21:51:27 +0800254 int (*givencrypt)(struct skcipher_givcrypt_request *req);
255 int (*givdecrypt)(struct skcipher_givcrypt_request *req);
Herbert Xub5b7f082007-04-16 20:48:54 +1000256
Herbert Xu23508e12007-11-27 21:33:24 +0800257 const char *geniv;
258
Herbert Xub5b7f082007-04-16 20:48:54 +1000259 unsigned int min_keysize;
260 unsigned int max_keysize;
261 unsigned int ivsize;
262};
263
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100264/**
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100265 * struct blkcipher_alg - synchronous block cipher definition
266 * @min_keysize: see struct ablkcipher_alg
267 * @max_keysize: see struct ablkcipher_alg
268 * @setkey: see struct ablkcipher_alg
269 * @encrypt: see struct ablkcipher_alg
270 * @decrypt: see struct ablkcipher_alg
271 * @geniv: see struct ablkcipher_alg
272 * @ivsize: see struct ablkcipher_alg
273 *
274 * All fields except @geniv and @ivsize are mandatory and must be filled.
275 */
Herbert Xu5cde0af2006-08-22 00:07:53 +1000276struct blkcipher_alg {
277 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
278 unsigned int keylen);
279 int (*encrypt)(struct blkcipher_desc *desc,
280 struct scatterlist *dst, struct scatterlist *src,
281 unsigned int nbytes);
282 int (*decrypt)(struct blkcipher_desc *desc,
283 struct scatterlist *dst, struct scatterlist *src,
284 unsigned int nbytes);
285
Herbert Xu23508e12007-11-27 21:33:24 +0800286 const char *geniv;
287
Herbert Xu5cde0af2006-08-22 00:07:53 +1000288 unsigned int min_keysize;
289 unsigned int max_keysize;
290 unsigned int ivsize;
291};
292
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100293/**
294 * struct cipher_alg - single-block symmetric ciphers definition
295 * @cia_min_keysize: Minimum key size supported by the transformation. This is
296 * the smallest key length supported by this transformation
297 * algorithm. This must be set to one of the pre-defined
298 * values as this is not hardware specific. Possible values
299 * for this field can be found via git grep "_MIN_KEY_SIZE"
300 * include/crypto/
301 * @cia_max_keysize: Maximum key size supported by the transformation. This is
302 * the largest key length supported by this transformation
303 * algorithm. This must be set to one of the pre-defined values
304 * as this is not hardware specific. Possible values for this
305 * field can be found via git grep "_MAX_KEY_SIZE"
306 * include/crypto/
307 * @cia_setkey: Set key for the transformation. This function is used to either
308 * program a supplied key into the hardware or store the key in the
309 * transformation context for programming it later. Note that this
310 * function does modify the transformation context. This function
311 * can be called multiple times during the existence of the
312 * transformation object, so one must make sure the key is properly
313 * reprogrammed into the hardware. This function is also
314 * responsible for checking the key length for validity.
315 * @cia_encrypt: Encrypt a single block. This function is used to encrypt a
316 * single block of data, which must be @cra_blocksize big. This
317 * always operates on a full @cra_blocksize and it is not possible
318 * to encrypt a block of smaller size. The supplied buffers must
319 * therefore also be at least of @cra_blocksize size. Both the
320 * input and output buffers are always aligned to @cra_alignmask.
321 * In case either of the input or output buffer supplied by user
322 * of the crypto API is not aligned to @cra_alignmask, the crypto
323 * API will re-align the buffers. The re-alignment means that a
324 * new buffer will be allocated, the data will be copied into the
325 * new buffer, then the processing will happen on the new buffer,
326 * then the data will be copied back into the original buffer and
327 * finally the new buffer will be freed. In case a software
328 * fallback was put in place in the @cra_init call, this function
329 * might need to use the fallback if the algorithm doesn't support
330 * all of the key sizes. In case the key was stored in
331 * transformation context, the key might need to be re-programmed
332 * into the hardware in this function. This function shall not
333 * modify the transformation context, as this function may be
334 * called in parallel with the same transformation object.
335 * @cia_decrypt: Decrypt a single block. This is a reverse counterpart to
336 * @cia_encrypt, and the conditions are exactly the same.
337 *
338 * All fields are mandatory and must be filled.
339 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340struct cipher_alg {
341 unsigned int cia_min_keysize;
342 unsigned int cia_max_keysize;
Herbert Xu6c2bb982006-05-16 22:09:29 +1000343 int (*cia_setkey)(struct crypto_tfm *tfm, const u8 *key,
Herbert Xu560c06a2006-08-13 14:16:39 +1000344 unsigned int keylen);
Herbert Xu6c2bb982006-05-16 22:09:29 +1000345 void (*cia_encrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
346 void (*cia_decrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347};
348
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349struct compress_alg {
Herbert Xu6c2bb982006-05-16 22:09:29 +1000350 int (*coa_compress)(struct crypto_tfm *tfm, const u8 *src,
351 unsigned int slen, u8 *dst, unsigned int *dlen);
352 int (*coa_decompress)(struct crypto_tfm *tfm, const u8 *src,
353 unsigned int slen, u8 *dst, unsigned int *dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354};
355
Neil Horman17f0f4a2008-08-14 22:15:52 +1000356
Herbert Xub5b7f082007-04-16 20:48:54 +1000357#define cra_ablkcipher cra_u.ablkcipher
Herbert Xu5cde0af2006-08-22 00:07:53 +1000358#define cra_blkcipher cra_u.blkcipher
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359#define cra_cipher cra_u.cipher
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360#define cra_compress cra_u.compress
361
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100362/**
363 * struct crypto_alg - definition of a cryptograpic cipher algorithm
364 * @cra_flags: Flags describing this transformation. See include/linux/crypto.h
365 * CRYPTO_ALG_* flags for the flags which go in here. Those are
366 * used for fine-tuning the description of the transformation
367 * algorithm.
368 * @cra_blocksize: Minimum block size of this transformation. The size in bytes
369 * of the smallest possible unit which can be transformed with
370 * this algorithm. The users must respect this value.
371 * In case of HASH transformation, it is possible for a smaller
372 * block than @cra_blocksize to be passed to the crypto API for
373 * transformation, in case of any other transformation type, an
374 * error will be returned upon any attempt to transform smaller
375 * than @cra_blocksize chunks.
376 * @cra_ctxsize: Size of the operational context of the transformation. This
377 * value informs the kernel crypto API about the memory size
378 * needed to be allocated for the transformation context.
379 * @cra_alignmask: Alignment mask for the input and output data buffer. The data
380 * buffer containing the input data for the algorithm must be
381 * aligned to this alignment mask. The data buffer for the
382 * output data must be aligned to this alignment mask. Note that
383 * the Crypto API will do the re-alignment in software, but
384 * only under special conditions and there is a performance hit.
385 * The re-alignment happens at these occasions for different
386 * @cra_u types: cipher -- For both input data and output data
387 * buffer; ahash -- For output hash destination buf; shash --
388 * For output hash destination buf.
389 * This is needed on hardware which is flawed by design and
390 * cannot pick data from arbitrary addresses.
391 * @cra_priority: Priority of this transformation implementation. In case
392 * multiple transformations with same @cra_name are available to
393 * the Crypto API, the kernel will use the one with highest
394 * @cra_priority.
395 * @cra_name: Generic name (usable by multiple implementations) of the
396 * transformation algorithm. This is the name of the transformation
397 * itself. This field is used by the kernel when looking up the
398 * providers of particular transformation.
399 * @cra_driver_name: Unique name of the transformation provider. This is the
400 * name of the provider of the transformation. This can be any
401 * arbitrary value, but in the usual case, this contains the
402 * name of the chip or provider and the name of the
403 * transformation algorithm.
404 * @cra_type: Type of the cryptographic transformation. This is a pointer to
405 * struct crypto_type, which implements callbacks common for all
Masanari Iida12f7c142015-06-04 00:01:21 +0900406 * transformation types. There are multiple options:
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100407 * &crypto_blkcipher_type, &crypto_ablkcipher_type,
Herbert Xub0d955b2015-08-14 15:30:41 +0800408 * &crypto_ahash_type, &crypto_rng_type.
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100409 * This field might be empty. In that case, there are no common
410 * callbacks. This is the case for: cipher, compress, shash.
411 * @cra_u: Callbacks implementing the transformation. This is a union of
412 * multiple structures. Depending on the type of transformation selected
413 * by @cra_type and @cra_flags above, the associated structure must be
414 * filled with callbacks. This field might be empty. This is the case
415 * for ahash, shash.
416 * @cra_init: Initialize the cryptographic transformation object. This function
417 * is used to initialize the cryptographic transformation object.
418 * This function is called only once at the instantiation time, right
419 * after the transformation context was allocated. In case the
420 * cryptographic hardware has some special requirements which need to
421 * be handled by software, this function shall check for the precise
422 * requirement of the transformation and put any software fallbacks
423 * in place.
424 * @cra_exit: Deinitialize the cryptographic transformation object. This is a
425 * counterpart to @cra_init, used to remove various changes set in
426 * @cra_init.
427 * @cra_module: Owner of this transformation implementation. Set to THIS_MODULE
428 * @cra_list: internally used
429 * @cra_users: internally used
430 * @cra_refcnt: internally used
431 * @cra_destroy: internally used
432 *
433 * The struct crypto_alg describes a generic Crypto API algorithm and is common
434 * for all of the transformations. Any variable not documented here shall not
435 * be used by a cipher implementation as it is internal to the Crypto API.
436 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437struct crypto_alg {
438 struct list_head cra_list;
Herbert Xu6bfd4802006-09-21 11:39:29 +1000439 struct list_head cra_users;
440
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 u32 cra_flags;
442 unsigned int cra_blocksize;
443 unsigned int cra_ctxsize;
Herbert Xu95477372005-07-06 13:52:09 -0700444 unsigned int cra_alignmask;
Herbert Xu5cb14542005-11-05 16:58:14 +1100445
446 int cra_priority;
Herbert Xu6521f302006-08-06 20:28:44 +1000447 atomic_t cra_refcnt;
Herbert Xu5cb14542005-11-05 16:58:14 +1100448
Herbert Xud913ea02006-05-21 08:45:26 +1000449 char cra_name[CRYPTO_MAX_ALG_NAME];
450 char cra_driver_name[CRYPTO_MAX_ALG_NAME];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
Herbert Xue853c3c2006-08-22 00:06:54 +1000452 const struct crypto_type *cra_type;
453
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 union {
Herbert Xub5b7f082007-04-16 20:48:54 +1000455 struct ablkcipher_alg ablkcipher;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000456 struct blkcipher_alg blkcipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 struct cipher_alg cipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 struct compress_alg compress;
459 } cra_u;
Herbert Xuc7fc0592006-05-24 13:02:26 +1000460
461 int (*cra_init)(struct crypto_tfm *tfm);
462 void (*cra_exit)(struct crypto_tfm *tfm);
Herbert Xu6521f302006-08-06 20:28:44 +1000463 void (*cra_destroy)(struct crypto_alg *alg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
465 struct module *cra_module;
Herbert Xuedf18b92015-06-18 14:00:48 +0800466} CRYPTO_MINALIGN_ATTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
468/*
Jaegeuk Kim8dec0742017-06-22 12:14:40 -0700469 * A helper struct for waiting for completion of async crypto ops
470 */
471struct crypto_wait {
472 struct completion completion;
473 int err;
474};
475
476/*
477 * Macro for declaring a crypto op async wait object on stack
478 */
479#define DECLARE_CRYPTO_WAIT(_wait) \
480 struct crypto_wait _wait = { \
481 COMPLETION_INITIALIZER_ONSTACK((_wait).completion), 0 }
482
483/*
484 * Async ops completion helper functioons
485 */
486void crypto_req_done(struct crypto_async_request *req, int err);
487
488static inline int crypto_wait_req(int err, struct crypto_wait *wait)
489{
490 switch (err) {
491 case -EINPROGRESS:
492 case -EBUSY:
493 wait_for_completion(&wait->completion);
494 reinit_completion(&wait->completion);
495 err = wait->err;
496 break;
497 };
498
499 return err;
500}
501
502static inline void crypto_init_wait(struct crypto_wait *wait)
503{
504 init_completion(&wait->completion);
505}
506
507/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 * Algorithm registration interface.
509 */
510int crypto_register_alg(struct crypto_alg *alg);
511int crypto_unregister_alg(struct crypto_alg *alg);
Mark Brown4b004342012-01-17 23:34:26 +0000512int crypto_register_algs(struct crypto_alg *algs, int count);
513int crypto_unregister_algs(struct crypto_alg *algs, int count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
515/*
516 * Algorithm query interface.
517 */
Herbert Xufce32d72006-08-26 17:35:45 +1000518int crypto_has_alg(const char *name, u32 type, u32 mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
520/*
521 * Transforms: user-instantiated objects which encapsulate algorithms
Herbert Xu6d7d6842006-07-30 11:53:01 +1000522 * and core processing logic. Managed via crypto_alloc_*() and
523 * crypto_free_*(), as well as the various helpers below.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Herbert Xu32e39832007-03-24 14:35:34 +1100526struct ablkcipher_tfm {
527 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
528 unsigned int keylen);
529 int (*encrypt)(struct ablkcipher_request *req);
530 int (*decrypt)(struct ablkcipher_request *req);
Herbert Xu61da88e2007-12-17 21:51:27 +0800531
Herbert Xuecfc4322007-12-05 21:08:36 +1100532 struct crypto_ablkcipher *base;
533
Herbert Xu32e39832007-03-24 14:35:34 +1100534 unsigned int ivsize;
535 unsigned int reqsize;
536};
537
Herbert Xu5cde0af2006-08-22 00:07:53 +1000538struct blkcipher_tfm {
539 void *iv;
540 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
541 unsigned int keylen);
542 int (*encrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
543 struct scatterlist *src, unsigned int nbytes);
544 int (*decrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
545 struct scatterlist *src, unsigned int nbytes);
546};
547
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548struct cipher_tfm {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 int (*cit_setkey)(struct crypto_tfm *tfm,
550 const u8 *key, unsigned int keylen);
Herbert Xuf28776a2006-08-13 20:58:18 +1000551 void (*cit_encrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
552 void (*cit_decrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553};
554
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555struct compress_tfm {
556 int (*cot_compress)(struct crypto_tfm *tfm,
557 const u8 *src, unsigned int slen,
558 u8 *dst, unsigned int *dlen);
559 int (*cot_decompress)(struct crypto_tfm *tfm,
560 const u8 *src, unsigned int slen,
561 u8 *dst, unsigned int *dlen);
562};
563
Herbert Xu32e39832007-03-24 14:35:34 +1100564#define crt_ablkcipher crt_u.ablkcipher
Herbert Xu5cde0af2006-08-22 00:07:53 +1000565#define crt_blkcipher crt_u.blkcipher
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566#define crt_cipher crt_u.cipher
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567#define crt_compress crt_u.compress
568
569struct crypto_tfm {
570
571 u32 crt_flags;
572
573 union {
Herbert Xu32e39832007-03-24 14:35:34 +1100574 struct ablkcipher_tfm ablkcipher;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000575 struct blkcipher_tfm blkcipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 struct cipher_tfm cipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 struct compress_tfm compress;
578 } crt_u;
Herbert Xu4a779482008-09-13 18:19:03 -0700579
580 void (*exit)(struct crypto_tfm *tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
582 struct crypto_alg *__crt_alg;
Herbert Xuf10b7892006-01-25 22:34:01 +1100583
Herbert Xu79911102006-08-21 21:03:52 +1000584 void *__crt_ctx[] CRYPTO_MINALIGN_ATTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585};
586
Herbert Xu32e39832007-03-24 14:35:34 +1100587struct crypto_ablkcipher {
588 struct crypto_tfm base;
589};
590
Herbert Xu5cde0af2006-08-22 00:07:53 +1000591struct crypto_blkcipher {
592 struct crypto_tfm base;
593};
594
Herbert Xu78a1fe42006-12-24 10:02:00 +1100595struct crypto_cipher {
596 struct crypto_tfm base;
597};
598
599struct crypto_comp {
600 struct crypto_tfm base;
601};
602
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000603enum {
604 CRYPTOA_UNSPEC,
605 CRYPTOA_ALG,
Herbert Xuebc610e2007-01-01 18:37:02 +1100606 CRYPTOA_TYPE,
Herbert Xu39e1ee012007-08-29 19:27:26 +0800607 CRYPTOA_U32,
Herbert Xuebc610e2007-01-01 18:37:02 +1100608 __CRYPTOA_MAX,
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000609};
610
Herbert Xuebc610e2007-01-01 18:37:02 +1100611#define CRYPTOA_MAX (__CRYPTOA_MAX - 1)
612
Herbert Xu39e1ee012007-08-29 19:27:26 +0800613/* Maximum number of (rtattr) parameters for each template. */
614#define CRYPTO_MAX_ATTRS 32
615
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000616struct crypto_attr_alg {
617 char name[CRYPTO_MAX_ALG_NAME];
618};
619
Herbert Xuebc610e2007-01-01 18:37:02 +1100620struct crypto_attr_type {
621 u32 type;
622 u32 mask;
623};
624
Herbert Xu39e1ee012007-08-29 19:27:26 +0800625struct crypto_attr_u32 {
626 u32 num;
627};
628
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629/*
630 * Transform user interface.
631 */
632
Herbert Xu6d7d6842006-07-30 11:53:01 +1000633struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask);
Herbert Xu7b2cd922009-02-05 16:48:24 +1100634void crypto_destroy_tfm(void *mem, struct crypto_tfm *tfm);
635
636static inline void crypto_free_tfm(struct crypto_tfm *tfm)
637{
638 return crypto_destroy_tfm(tfm, tfm);
639}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
Herbert Xuda7f0332008-07-31 17:08:25 +0800641int alg_test(const char *driver, const char *alg, u32 type, u32 mask);
642
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643/*
644 * Transform helpers which query the underlying algorithm.
645 */
646static inline const char *crypto_tfm_alg_name(struct crypto_tfm *tfm)
647{
648 return tfm->__crt_alg->cra_name;
649}
650
Michal Ludvigb14cdd62006-07-09 09:02:24 +1000651static inline const char *crypto_tfm_alg_driver_name(struct crypto_tfm *tfm)
652{
653 return tfm->__crt_alg->cra_driver_name;
654}
655
656static inline int crypto_tfm_alg_priority(struct crypto_tfm *tfm)
657{
658 return tfm->__crt_alg->cra_priority;
659}
660
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm)
662{
663 return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK;
664}
665
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm)
667{
668 return tfm->__crt_alg->cra_blocksize;
669}
670
Herbert Xufbdae9f2005-07-06 13:53:29 -0700671static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm *tfm)
672{
673 return tfm->__crt_alg->cra_alignmask;
674}
675
Herbert Xuf28776a2006-08-13 20:58:18 +1000676static inline u32 crypto_tfm_get_flags(struct crypto_tfm *tfm)
677{
678 return tfm->crt_flags;
679}
680
681static inline void crypto_tfm_set_flags(struct crypto_tfm *tfm, u32 flags)
682{
683 tfm->crt_flags |= flags;
684}
685
686static inline void crypto_tfm_clear_flags(struct crypto_tfm *tfm, u32 flags)
687{
688 tfm->crt_flags &= ~flags;
689}
690
Herbert Xu40725182005-07-06 13:51:52 -0700691static inline void *crypto_tfm_ctx(struct crypto_tfm *tfm)
692{
Herbert Xuf10b7892006-01-25 22:34:01 +1100693 return tfm->__crt_ctx;
694}
695
696static inline unsigned int crypto_tfm_ctx_alignment(void)
697{
698 struct crypto_tfm *tfm;
699 return __alignof__(tfm->__crt_ctx);
Herbert Xu40725182005-07-06 13:51:52 -0700700}
701
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702/*
703 * API wrappers.
704 */
Herbert Xu32e39832007-03-24 14:35:34 +1100705static inline struct crypto_ablkcipher *__crypto_ablkcipher_cast(
706 struct crypto_tfm *tfm)
707{
708 return (struct crypto_ablkcipher *)tfm;
709}
710
Herbert Xu378f4f52007-12-17 20:07:31 +0800711static inline u32 crypto_skcipher_type(u32 type)
712{
Herbert Xuecfc4322007-12-05 21:08:36 +1100713 type &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
Herbert Xu378f4f52007-12-17 20:07:31 +0800714 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
715 return type;
716}
717
718static inline u32 crypto_skcipher_mask(u32 mask)
719{
Herbert Xuecfc4322007-12-05 21:08:36 +1100720 mask &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
Herbert Xu378f4f52007-12-17 20:07:31 +0800721 mask |= CRYPTO_ALG_TYPE_BLKCIPHER_MASK;
722 return mask;
723}
724
Stephan Muellerf13ec332014-11-12 05:28:22 +0100725/**
726 * DOC: Asynchronous Block Cipher API
727 *
728 * Asynchronous block cipher API is used with the ciphers of type
729 * CRYPTO_ALG_TYPE_ABLKCIPHER (listed as type "ablkcipher" in /proc/crypto).
730 *
731 * Asynchronous cipher operations imply that the function invocation for a
732 * cipher request returns immediately before the completion of the operation.
733 * The cipher request is scheduled as a separate kernel thread and therefore
734 * load-balanced on the different CPUs via the process scheduler. To allow
735 * the kernel crypto API to inform the caller about the completion of a cipher
736 * request, the caller must provide a callback function. That function is
737 * invoked with the cipher handle when the request completes.
738 *
739 * To support the asynchronous operation, additional information than just the
740 * cipher handle must be supplied to the kernel crypto API. That additional
741 * information is given by filling in the ablkcipher_request data structure.
742 *
743 * For the asynchronous block cipher API, the state is maintained with the tfm
744 * cipher handle. A single tfm can be used across multiple calls and in
745 * parallel. For asynchronous block cipher calls, context data supplied and
746 * only used by the caller can be referenced the request data structure in
747 * addition to the IV used for the cipher request. The maintenance of such
748 * state information would be important for a crypto driver implementer to
749 * have, because when calling the callback function upon completion of the
750 * cipher operation, that callback function may need some information about
751 * which operation just finished if it invoked multiple in parallel. This
752 * state information is unused by the kernel crypto API.
753 */
754
Herbert Xu32e39832007-03-24 14:35:34 +1100755static inline struct crypto_tfm *crypto_ablkcipher_tfm(
756 struct crypto_ablkcipher *tfm)
757{
758 return &tfm->base;
759}
760
Stephan Muellerf13ec332014-11-12 05:28:22 +0100761/**
762 * crypto_free_ablkcipher() - zeroize and free cipher handle
763 * @tfm: cipher handle to be freed
764 */
Herbert Xu32e39832007-03-24 14:35:34 +1100765static inline void crypto_free_ablkcipher(struct crypto_ablkcipher *tfm)
766{
767 crypto_free_tfm(crypto_ablkcipher_tfm(tfm));
768}
769
Stephan Muellerf13ec332014-11-12 05:28:22 +0100770/**
771 * crypto_has_ablkcipher() - Search for the availability of an ablkcipher.
772 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
773 * ablkcipher
774 * @type: specifies the type of the cipher
775 * @mask: specifies the mask for the cipher
776 *
777 * Return: true when the ablkcipher is known to the kernel crypto API; false
778 * otherwise
779 */
Herbert Xu32e39832007-03-24 14:35:34 +1100780static inline int crypto_has_ablkcipher(const char *alg_name, u32 type,
781 u32 mask)
782{
Herbert Xu378f4f52007-12-17 20:07:31 +0800783 return crypto_has_alg(alg_name, crypto_skcipher_type(type),
784 crypto_skcipher_mask(mask));
Herbert Xu32e39832007-03-24 14:35:34 +1100785}
786
787static inline struct ablkcipher_tfm *crypto_ablkcipher_crt(
788 struct crypto_ablkcipher *tfm)
789{
790 return &crypto_ablkcipher_tfm(tfm)->crt_ablkcipher;
791}
792
Stephan Muellerf13ec332014-11-12 05:28:22 +0100793/**
794 * crypto_ablkcipher_ivsize() - obtain IV size
795 * @tfm: cipher handle
796 *
797 * The size of the IV for the ablkcipher referenced by the cipher handle is
798 * returned. This IV size may be zero if the cipher does not need an IV.
799 *
800 * Return: IV size in bytes
801 */
Herbert Xu32e39832007-03-24 14:35:34 +1100802static inline unsigned int crypto_ablkcipher_ivsize(
803 struct crypto_ablkcipher *tfm)
804{
805 return crypto_ablkcipher_crt(tfm)->ivsize;
806}
807
Stephan Muellerf13ec332014-11-12 05:28:22 +0100808/**
809 * crypto_ablkcipher_blocksize() - obtain block size of cipher
810 * @tfm: cipher handle
811 *
812 * The block size for the ablkcipher referenced with the cipher handle is
813 * returned. The caller may use that information to allocate appropriate
814 * memory for the data returned by the encryption or decryption operation
815 *
816 * Return: block size of cipher
817 */
Herbert Xu32e39832007-03-24 14:35:34 +1100818static inline unsigned int crypto_ablkcipher_blocksize(
819 struct crypto_ablkcipher *tfm)
820{
821 return crypto_tfm_alg_blocksize(crypto_ablkcipher_tfm(tfm));
822}
823
824static inline unsigned int crypto_ablkcipher_alignmask(
825 struct crypto_ablkcipher *tfm)
826{
827 return crypto_tfm_alg_alignmask(crypto_ablkcipher_tfm(tfm));
828}
829
830static inline u32 crypto_ablkcipher_get_flags(struct crypto_ablkcipher *tfm)
831{
832 return crypto_tfm_get_flags(crypto_ablkcipher_tfm(tfm));
833}
834
835static inline void crypto_ablkcipher_set_flags(struct crypto_ablkcipher *tfm,
836 u32 flags)
837{
838 crypto_tfm_set_flags(crypto_ablkcipher_tfm(tfm), flags);
839}
840
841static inline void crypto_ablkcipher_clear_flags(struct crypto_ablkcipher *tfm,
842 u32 flags)
843{
844 crypto_tfm_clear_flags(crypto_ablkcipher_tfm(tfm), flags);
845}
846
Stephan Muellerf13ec332014-11-12 05:28:22 +0100847/**
848 * crypto_ablkcipher_setkey() - set key for cipher
849 * @tfm: cipher handle
850 * @key: buffer holding the key
851 * @keylen: length of the key in bytes
852 *
853 * The caller provided key is set for the ablkcipher referenced by the cipher
854 * handle.
855 *
856 * Note, the key length determines the cipher type. Many block ciphers implement
857 * different cipher modes depending on the key size, such as AES-128 vs AES-192
858 * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
859 * is performed.
860 *
861 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
862 */
Herbert Xu32e39832007-03-24 14:35:34 +1100863static inline int crypto_ablkcipher_setkey(struct crypto_ablkcipher *tfm,
864 const u8 *key, unsigned int keylen)
865{
Herbert Xuecfc4322007-12-05 21:08:36 +1100866 struct ablkcipher_tfm *crt = crypto_ablkcipher_crt(tfm);
867
868 return crt->setkey(crt->base, key, keylen);
Herbert Xu32e39832007-03-24 14:35:34 +1100869}
870
Stephan Muellerf13ec332014-11-12 05:28:22 +0100871/**
872 * crypto_ablkcipher_reqtfm() - obtain cipher handle from request
873 * @req: ablkcipher_request out of which the cipher handle is to be obtained
874 *
875 * Return the crypto_ablkcipher handle when furnishing an ablkcipher_request
876 * data structure.
877 *
878 * Return: crypto_ablkcipher handle
879 */
Herbert Xu32e39832007-03-24 14:35:34 +1100880static inline struct crypto_ablkcipher *crypto_ablkcipher_reqtfm(
881 struct ablkcipher_request *req)
882{
883 return __crypto_ablkcipher_cast(req->base.tfm);
884}
885
Stephan Muellerf13ec332014-11-12 05:28:22 +0100886/**
887 * crypto_ablkcipher_encrypt() - encrypt plaintext
888 * @req: reference to the ablkcipher_request handle that holds all information
889 * needed to perform the cipher operation
890 *
891 * Encrypt plaintext data using the ablkcipher_request handle. That data
892 * structure and how it is filled with data is discussed with the
893 * ablkcipher_request_* functions.
894 *
895 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
896 */
Herbert Xu32e39832007-03-24 14:35:34 +1100897static inline int crypto_ablkcipher_encrypt(struct ablkcipher_request *req)
898{
899 struct ablkcipher_tfm *crt =
900 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
901 return crt->encrypt(req);
902}
903
Stephan Muellerf13ec332014-11-12 05:28:22 +0100904/**
905 * crypto_ablkcipher_decrypt() - decrypt ciphertext
906 * @req: reference to the ablkcipher_request handle that holds all information
907 * needed to perform the cipher operation
908 *
909 * Decrypt ciphertext data using the ablkcipher_request handle. That data
910 * structure and how it is filled with data is discussed with the
911 * ablkcipher_request_* functions.
912 *
913 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
914 */
Herbert Xu32e39832007-03-24 14:35:34 +1100915static inline int crypto_ablkcipher_decrypt(struct ablkcipher_request *req)
916{
917 struct ablkcipher_tfm *crt =
918 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
919 return crt->decrypt(req);
920}
921
Stephan Muellerf13ec332014-11-12 05:28:22 +0100922/**
923 * DOC: Asynchronous Cipher Request Handle
924 *
925 * The ablkcipher_request data structure contains all pointers to data
926 * required for the asynchronous cipher operation. This includes the cipher
927 * handle (which can be used by multiple ablkcipher_request instances), pointer
928 * to plaintext and ciphertext, asynchronous callback function, etc. It acts
929 * as a handle to the ablkcipher_request_* API calls in a similar way as
930 * ablkcipher handle to the crypto_ablkcipher_* API calls.
931 */
932
933/**
934 * crypto_ablkcipher_reqsize() - obtain size of the request data structure
935 * @tfm: cipher handle
936 *
937 * Return: number of bytes
938 */
Herbert Xub16c3a22007-08-29 19:02:04 +0800939static inline unsigned int crypto_ablkcipher_reqsize(
940 struct crypto_ablkcipher *tfm)
Herbert Xu32e39832007-03-24 14:35:34 +1100941{
942 return crypto_ablkcipher_crt(tfm)->reqsize;
943}
944
Stephan Muellerf13ec332014-11-12 05:28:22 +0100945/**
946 * ablkcipher_request_set_tfm() - update cipher handle reference in request
947 * @req: request handle to be modified
948 * @tfm: cipher handle that shall be added to the request handle
949 *
950 * Allow the caller to replace the existing ablkcipher handle in the request
951 * data structure with a different one.
952 */
Herbert Xue196d622007-04-14 16:09:14 +1000953static inline void ablkcipher_request_set_tfm(
954 struct ablkcipher_request *req, struct crypto_ablkcipher *tfm)
955{
Herbert Xuecfc4322007-12-05 21:08:36 +1100956 req->base.tfm = crypto_ablkcipher_tfm(crypto_ablkcipher_crt(tfm)->base);
Herbert Xue196d622007-04-14 16:09:14 +1000957}
958
Herbert Xub5b7f082007-04-16 20:48:54 +1000959static inline struct ablkcipher_request *ablkcipher_request_cast(
960 struct crypto_async_request *req)
961{
962 return container_of(req, struct ablkcipher_request, base);
963}
964
Stephan Muellerf13ec332014-11-12 05:28:22 +0100965/**
966 * ablkcipher_request_alloc() - allocate request data structure
967 * @tfm: cipher handle to be registered with the request
968 * @gfp: memory allocation flag that is handed to kmalloc by the API call.
969 *
970 * Allocate the request data structure that must be used with the ablkcipher
971 * encrypt and decrypt API calls. During the allocation, the provided ablkcipher
972 * handle is registered in the request data structure.
973 *
Eric Biggers6eae29e2016-04-02 10:54:56 -0500974 * Return: allocated request handle in case of success, or NULL if out of memory
Stephan Muellerf13ec332014-11-12 05:28:22 +0100975 */
Herbert Xu32e39832007-03-24 14:35:34 +1100976static inline struct ablkcipher_request *ablkcipher_request_alloc(
977 struct crypto_ablkcipher *tfm, gfp_t gfp)
978{
979 struct ablkcipher_request *req;
980
981 req = kmalloc(sizeof(struct ablkcipher_request) +
982 crypto_ablkcipher_reqsize(tfm), gfp);
983
984 if (likely(req))
Herbert Xue196d622007-04-14 16:09:14 +1000985 ablkcipher_request_set_tfm(req, tfm);
Herbert Xu32e39832007-03-24 14:35:34 +1100986
987 return req;
988}
989
Stephan Muellerf13ec332014-11-12 05:28:22 +0100990/**
991 * ablkcipher_request_free() - zeroize and free request data structure
992 * @req: request data structure cipher handle to be freed
993 */
Herbert Xu32e39832007-03-24 14:35:34 +1100994static inline void ablkcipher_request_free(struct ablkcipher_request *req)
995{
Herbert Xuaef73cf2009-07-11 22:22:14 +0800996 kzfree(req);
Herbert Xu32e39832007-03-24 14:35:34 +1100997}
998
Stephan Muellerf13ec332014-11-12 05:28:22 +0100999/**
1000 * ablkcipher_request_set_callback() - set asynchronous callback function
1001 * @req: request handle
1002 * @flags: specify zero or an ORing of the flags
1003 * CRYPTO_TFM_REQ_MAY_BACKLOG the request queue may back log and
1004 * increase the wait queue beyond the initial maximum size;
1005 * CRYPTO_TFM_REQ_MAY_SLEEP the request processing may sleep
1006 * @compl: callback function pointer to be registered with the request handle
1007 * @data: The data pointer refers to memory that is not used by the kernel
1008 * crypto API, but provided to the callback function for it to use. Here,
1009 * the caller can provide a reference to memory the callback function can
1010 * operate on. As the callback function is invoked asynchronously to the
1011 * related functionality, it may need to access data structures of the
1012 * related functionality which can be referenced using this pointer. The
1013 * callback function can access the memory via the "data" field in the
1014 * crypto_async_request data structure provided to the callback function.
1015 *
1016 * This function allows setting the callback function that is triggered once the
1017 * cipher operation completes.
1018 *
1019 * The callback function is registered with the ablkcipher_request handle and
Stephan Mueller379dcfb2015-01-19 00:13:39 +01001020 * must comply with the following template
Stephan Muellerf13ec332014-11-12 05:28:22 +01001021 *
1022 * void callback_function(struct crypto_async_request *req, int error)
1023 */
Herbert Xu32e39832007-03-24 14:35:34 +11001024static inline void ablkcipher_request_set_callback(
1025 struct ablkcipher_request *req,
Mark Rustad3e3dc252014-07-25 02:53:38 -07001026 u32 flags, crypto_completion_t compl, void *data)
Herbert Xu32e39832007-03-24 14:35:34 +11001027{
Mark Rustad3e3dc252014-07-25 02:53:38 -07001028 req->base.complete = compl;
Herbert Xu32e39832007-03-24 14:35:34 +11001029 req->base.data = data;
1030 req->base.flags = flags;
1031}
1032
Stephan Muellerf13ec332014-11-12 05:28:22 +01001033/**
1034 * ablkcipher_request_set_crypt() - set data buffers
1035 * @req: request handle
1036 * @src: source scatter / gather list
1037 * @dst: destination scatter / gather list
1038 * @nbytes: number of bytes to process from @src
1039 * @iv: IV for the cipher operation which must comply with the IV size defined
1040 * by crypto_ablkcipher_ivsize
1041 *
1042 * This function allows setting of the source data and destination data
1043 * scatter / gather lists.
1044 *
1045 * For encryption, the source is treated as the plaintext and the
1046 * destination is the ciphertext. For a decryption operation, the use is
Stephan Mueller379dcfb2015-01-19 00:13:39 +01001047 * reversed - the source is the ciphertext and the destination is the plaintext.
Stephan Muellerf13ec332014-11-12 05:28:22 +01001048 */
Herbert Xu32e39832007-03-24 14:35:34 +11001049static inline void ablkcipher_request_set_crypt(
1050 struct ablkcipher_request *req,
1051 struct scatterlist *src, struct scatterlist *dst,
1052 unsigned int nbytes, void *iv)
1053{
1054 req->src = src;
1055 req->dst = dst;
1056 req->nbytes = nbytes;
1057 req->info = iv;
1058}
1059
Stephan Muellerfced7b02014-11-12 05:29:00 +01001060/**
Stephan Mueller58284f02014-11-12 05:29:36 +01001061 * DOC: Synchronous Block Cipher API
1062 *
1063 * The synchronous block cipher API is used with the ciphers of type
1064 * CRYPTO_ALG_TYPE_BLKCIPHER (listed as type "blkcipher" in /proc/crypto)
1065 *
1066 * Synchronous calls, have a context in the tfm. But since a single tfm can be
1067 * used in multiple calls and in parallel, this info should not be changeable
1068 * (unless a lock is used). This applies, for example, to the symmetric key.
1069 * However, the IV is changeable, so there is an iv field in blkcipher_tfm
1070 * structure for synchronous blkcipher api. So, its the only state info that can
1071 * be kept for synchronous calls without using a big lock across a tfm.
1072 *
1073 * The block cipher API allows the use of a complete cipher, i.e. a cipher
1074 * consisting of a template (a block chaining mode) and a single block cipher
1075 * primitive (e.g. AES).
1076 *
1077 * The plaintext data buffer and the ciphertext data buffer are pointed to
1078 * by using scatter/gather lists. The cipher operation is performed
1079 * on all segments of the provided scatter/gather lists.
1080 *
1081 * The kernel crypto API supports a cipher operation "in-place" which means that
1082 * the caller may provide the same scatter/gather list for the plaintext and
1083 * cipher text. After the completion of the cipher operation, the plaintext
1084 * data is replaced with the ciphertext data in case of an encryption and vice
1085 * versa for a decryption. The caller must ensure that the scatter/gather lists
1086 * for the output data point to sufficiently large buffers, i.e. multiples of
1087 * the block size of the cipher.
1088 */
1089
Herbert Xu5cde0af2006-08-22 00:07:53 +10001090static inline struct crypto_blkcipher *__crypto_blkcipher_cast(
1091 struct crypto_tfm *tfm)
1092{
1093 return (struct crypto_blkcipher *)tfm;
1094}
1095
1096static inline struct crypto_blkcipher *crypto_blkcipher_cast(
1097 struct crypto_tfm *tfm)
1098{
1099 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_BLKCIPHER);
1100 return __crypto_blkcipher_cast(tfm);
1101}
1102
Stephan Mueller58284f02014-11-12 05:29:36 +01001103/**
1104 * crypto_alloc_blkcipher() - allocate synchronous block cipher handle
1105 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1106 * blkcipher cipher
1107 * @type: specifies the type of the cipher
1108 * @mask: specifies the mask for the cipher
1109 *
1110 * Allocate a cipher handle for a block cipher. The returned struct
1111 * crypto_blkcipher is the cipher handle that is required for any subsequent
1112 * API invocation for that block cipher.
1113 *
1114 * Return: allocated cipher handle in case of success; IS_ERR() is true in case
1115 * of an error, PTR_ERR() returns the error code.
1116 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001117static inline struct crypto_blkcipher *crypto_alloc_blkcipher(
1118 const char *alg_name, u32 type, u32 mask)
1119{
Herbert Xu332f88402007-11-15 22:36:07 +08001120 type &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xu5cde0af2006-08-22 00:07:53 +10001121 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
Herbert Xu332f88402007-11-15 22:36:07 +08001122 mask |= CRYPTO_ALG_TYPE_MASK;
Herbert Xu5cde0af2006-08-22 00:07:53 +10001123
1124 return __crypto_blkcipher_cast(crypto_alloc_base(alg_name, type, mask));
1125}
1126
1127static inline struct crypto_tfm *crypto_blkcipher_tfm(
1128 struct crypto_blkcipher *tfm)
1129{
1130 return &tfm->base;
1131}
1132
Stephan Mueller58284f02014-11-12 05:29:36 +01001133/**
1134 * crypto_free_blkcipher() - zeroize and free the block cipher handle
1135 * @tfm: cipher handle to be freed
1136 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001137static inline void crypto_free_blkcipher(struct crypto_blkcipher *tfm)
1138{
1139 crypto_free_tfm(crypto_blkcipher_tfm(tfm));
1140}
1141
Stephan Mueller58284f02014-11-12 05:29:36 +01001142/**
1143 * crypto_has_blkcipher() - Search for the availability of a block cipher
1144 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1145 * block cipher
1146 * @type: specifies the type of the cipher
1147 * @mask: specifies the mask for the cipher
1148 *
1149 * Return: true when the block cipher is known to the kernel crypto API; false
1150 * otherwise
1151 */
Herbert Xufce32d72006-08-26 17:35:45 +10001152static inline int crypto_has_blkcipher(const char *alg_name, u32 type, u32 mask)
1153{
Herbert Xu332f88402007-11-15 22:36:07 +08001154 type &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xufce32d72006-08-26 17:35:45 +10001155 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
Herbert Xu332f88402007-11-15 22:36:07 +08001156 mask |= CRYPTO_ALG_TYPE_MASK;
Herbert Xufce32d72006-08-26 17:35:45 +10001157
1158 return crypto_has_alg(alg_name, type, mask);
1159}
1160
Stephan Mueller58284f02014-11-12 05:29:36 +01001161/**
1162 * crypto_blkcipher_name() - return the name / cra_name from the cipher handle
1163 * @tfm: cipher handle
1164 *
1165 * Return: The character string holding the name of the cipher
1166 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001167static inline const char *crypto_blkcipher_name(struct crypto_blkcipher *tfm)
1168{
1169 return crypto_tfm_alg_name(crypto_blkcipher_tfm(tfm));
1170}
1171
1172static inline struct blkcipher_tfm *crypto_blkcipher_crt(
1173 struct crypto_blkcipher *tfm)
1174{
1175 return &crypto_blkcipher_tfm(tfm)->crt_blkcipher;
1176}
1177
1178static inline struct blkcipher_alg *crypto_blkcipher_alg(
1179 struct crypto_blkcipher *tfm)
1180{
1181 return &crypto_blkcipher_tfm(tfm)->__crt_alg->cra_blkcipher;
1182}
1183
Stephan Mueller58284f02014-11-12 05:29:36 +01001184/**
1185 * crypto_blkcipher_ivsize() - obtain IV size
1186 * @tfm: cipher handle
1187 *
1188 * The size of the IV for the block cipher referenced by the cipher handle is
1189 * returned. This IV size may be zero if the cipher does not need an IV.
1190 *
1191 * Return: IV size in bytes
1192 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001193static inline unsigned int crypto_blkcipher_ivsize(struct crypto_blkcipher *tfm)
1194{
1195 return crypto_blkcipher_alg(tfm)->ivsize;
1196}
1197
Stephan Mueller58284f02014-11-12 05:29:36 +01001198/**
1199 * crypto_blkcipher_blocksize() - obtain block size of cipher
1200 * @tfm: cipher handle
1201 *
1202 * The block size for the block cipher referenced with the cipher handle is
1203 * returned. The caller may use that information to allocate appropriate
1204 * memory for the data returned by the encryption or decryption operation.
1205 *
1206 * Return: block size of cipher
1207 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001208static inline unsigned int crypto_blkcipher_blocksize(
1209 struct crypto_blkcipher *tfm)
1210{
1211 return crypto_tfm_alg_blocksize(crypto_blkcipher_tfm(tfm));
1212}
1213
1214static inline unsigned int crypto_blkcipher_alignmask(
1215 struct crypto_blkcipher *tfm)
1216{
1217 return crypto_tfm_alg_alignmask(crypto_blkcipher_tfm(tfm));
1218}
1219
1220static inline u32 crypto_blkcipher_get_flags(struct crypto_blkcipher *tfm)
1221{
1222 return crypto_tfm_get_flags(crypto_blkcipher_tfm(tfm));
1223}
1224
1225static inline void crypto_blkcipher_set_flags(struct crypto_blkcipher *tfm,
1226 u32 flags)
1227{
1228 crypto_tfm_set_flags(crypto_blkcipher_tfm(tfm), flags);
1229}
1230
1231static inline void crypto_blkcipher_clear_flags(struct crypto_blkcipher *tfm,
1232 u32 flags)
1233{
1234 crypto_tfm_clear_flags(crypto_blkcipher_tfm(tfm), flags);
1235}
1236
Stephan Mueller58284f02014-11-12 05:29:36 +01001237/**
1238 * crypto_blkcipher_setkey() - set key for cipher
1239 * @tfm: cipher handle
1240 * @key: buffer holding the key
1241 * @keylen: length of the key in bytes
1242 *
1243 * The caller provided key is set for the block cipher referenced by the cipher
1244 * handle.
1245 *
1246 * Note, the key length determines the cipher type. Many block ciphers implement
1247 * different cipher modes depending on the key size, such as AES-128 vs AES-192
1248 * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
1249 * is performed.
1250 *
1251 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
1252 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001253static inline int crypto_blkcipher_setkey(struct crypto_blkcipher *tfm,
1254 const u8 *key, unsigned int keylen)
1255{
1256 return crypto_blkcipher_crt(tfm)->setkey(crypto_blkcipher_tfm(tfm),
1257 key, keylen);
1258}
1259
Stephan Mueller58284f02014-11-12 05:29:36 +01001260/**
1261 * crypto_blkcipher_encrypt() - encrypt plaintext
1262 * @desc: reference to the block cipher handle with meta data
1263 * @dst: scatter/gather list that is filled by the cipher operation with the
1264 * ciphertext
1265 * @src: scatter/gather list that holds the plaintext
1266 * @nbytes: number of bytes of the plaintext to encrypt.
1267 *
1268 * Encrypt plaintext data using the IV set by the caller with a preceding
1269 * call of crypto_blkcipher_set_iv.
1270 *
1271 * The blkcipher_desc data structure must be filled by the caller and can
1272 * reside on the stack. The caller must fill desc as follows: desc.tfm is filled
1273 * with the block cipher handle; desc.flags is filled with either
1274 * CRYPTO_TFM_REQ_MAY_SLEEP or 0.
1275 *
1276 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1277 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001278static inline int crypto_blkcipher_encrypt(struct blkcipher_desc *desc,
1279 struct scatterlist *dst,
1280 struct scatterlist *src,
1281 unsigned int nbytes)
1282{
1283 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
1284 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
1285}
1286
Stephan Mueller58284f02014-11-12 05:29:36 +01001287/**
1288 * crypto_blkcipher_encrypt_iv() - encrypt plaintext with dedicated IV
1289 * @desc: reference to the block cipher handle with meta data
1290 * @dst: scatter/gather list that is filled by the cipher operation with the
1291 * ciphertext
1292 * @src: scatter/gather list that holds the plaintext
1293 * @nbytes: number of bytes of the plaintext to encrypt.
1294 *
1295 * Encrypt plaintext data with the use of an IV that is solely used for this
1296 * cipher operation. Any previously set IV is not used.
1297 *
1298 * The blkcipher_desc data structure must be filled by the caller and can
1299 * reside on the stack. The caller must fill desc as follows: desc.tfm is filled
1300 * with the block cipher handle; desc.info is filled with the IV to be used for
1301 * the current operation; desc.flags is filled with either
1302 * CRYPTO_TFM_REQ_MAY_SLEEP or 0.
1303 *
1304 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1305 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001306static inline int crypto_blkcipher_encrypt_iv(struct blkcipher_desc *desc,
1307 struct scatterlist *dst,
1308 struct scatterlist *src,
1309 unsigned int nbytes)
1310{
1311 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
1312}
1313
Stephan Mueller58284f02014-11-12 05:29:36 +01001314/**
1315 * crypto_blkcipher_decrypt() - decrypt ciphertext
1316 * @desc: reference to the block cipher handle with meta data
1317 * @dst: scatter/gather list that is filled by the cipher operation with the
1318 * plaintext
1319 * @src: scatter/gather list that holds the ciphertext
1320 * @nbytes: number of bytes of the ciphertext to decrypt.
1321 *
1322 * Decrypt ciphertext data using the IV set by the caller with a preceding
1323 * call of crypto_blkcipher_set_iv.
1324 *
1325 * The blkcipher_desc data structure must be filled by the caller as documented
1326 * for the crypto_blkcipher_encrypt call above.
1327 *
1328 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1329 *
1330 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001331static inline int crypto_blkcipher_decrypt(struct blkcipher_desc *desc,
1332 struct scatterlist *dst,
1333 struct scatterlist *src,
1334 unsigned int nbytes)
1335{
1336 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
1337 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
1338}
1339
Stephan Mueller58284f02014-11-12 05:29:36 +01001340/**
1341 * crypto_blkcipher_decrypt_iv() - decrypt ciphertext with dedicated IV
1342 * @desc: reference to the block cipher handle with meta data
1343 * @dst: scatter/gather list that is filled by the cipher operation with the
1344 * plaintext
1345 * @src: scatter/gather list that holds the ciphertext
1346 * @nbytes: number of bytes of the ciphertext to decrypt.
1347 *
1348 * Decrypt ciphertext data with the use of an IV that is solely used for this
1349 * cipher operation. Any previously set IV is not used.
1350 *
1351 * The blkcipher_desc data structure must be filled by the caller as documented
1352 * for the crypto_blkcipher_encrypt_iv call above.
1353 *
1354 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1355 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001356static inline int crypto_blkcipher_decrypt_iv(struct blkcipher_desc *desc,
1357 struct scatterlist *dst,
1358 struct scatterlist *src,
1359 unsigned int nbytes)
1360{
1361 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
1362}
1363
Stephan Mueller58284f02014-11-12 05:29:36 +01001364/**
1365 * crypto_blkcipher_set_iv() - set IV for cipher
1366 * @tfm: cipher handle
1367 * @src: buffer holding the IV
1368 * @len: length of the IV in bytes
1369 *
1370 * The caller provided IV is set for the block cipher referenced by the cipher
1371 * handle.
1372 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001373static inline void crypto_blkcipher_set_iv(struct crypto_blkcipher *tfm,
1374 const u8 *src, unsigned int len)
1375{
1376 memcpy(crypto_blkcipher_crt(tfm)->iv, src, len);
1377}
1378
Stephan Mueller58284f02014-11-12 05:29:36 +01001379/**
1380 * crypto_blkcipher_get_iv() - obtain IV from cipher
1381 * @tfm: cipher handle
1382 * @dst: buffer filled with the IV
1383 * @len: length of the buffer dst
1384 *
1385 * The caller can obtain the IV set for the block cipher referenced by the
1386 * cipher handle and store it into the user-provided buffer. If the buffer
1387 * has an insufficient space, the IV is truncated to fit the buffer.
1388 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001389static inline void crypto_blkcipher_get_iv(struct crypto_blkcipher *tfm,
1390 u8 *dst, unsigned int len)
1391{
1392 memcpy(dst, crypto_blkcipher_crt(tfm)->iv, len);
1393}
1394
Stephan Mueller16e61032014-11-12 05:30:06 +01001395/**
1396 * DOC: Single Block Cipher API
1397 *
1398 * The single block cipher API is used with the ciphers of type
1399 * CRYPTO_ALG_TYPE_CIPHER (listed as type "cipher" in /proc/crypto).
1400 *
1401 * Using the single block cipher API calls, operations with the basic cipher
1402 * primitive can be implemented. These cipher primitives exclude any block
1403 * chaining operations including IV handling.
1404 *
1405 * The purpose of this single block cipher API is to support the implementation
1406 * of templates or other concepts that only need to perform the cipher operation
1407 * on one block at a time. Templates invoke the underlying cipher primitive
1408 * block-wise and process either the input or the output data of these cipher
1409 * operations.
1410 */
1411
Herbert Xuf28776a2006-08-13 20:58:18 +10001412static inline struct crypto_cipher *__crypto_cipher_cast(struct crypto_tfm *tfm)
1413{
1414 return (struct crypto_cipher *)tfm;
1415}
1416
1417static inline struct crypto_cipher *crypto_cipher_cast(struct crypto_tfm *tfm)
1418{
1419 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
1420 return __crypto_cipher_cast(tfm);
1421}
1422
Stephan Mueller16e61032014-11-12 05:30:06 +01001423/**
1424 * crypto_alloc_cipher() - allocate single block cipher handle
1425 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1426 * single block cipher
1427 * @type: specifies the type of the cipher
1428 * @mask: specifies the mask for the cipher
1429 *
1430 * Allocate a cipher handle for a single block cipher. The returned struct
1431 * crypto_cipher is the cipher handle that is required for any subsequent API
1432 * invocation for that single block cipher.
1433 *
1434 * Return: allocated cipher handle in case of success; IS_ERR() is true in case
1435 * of an error, PTR_ERR() returns the error code.
1436 */
Herbert Xuf28776a2006-08-13 20:58:18 +10001437static inline struct crypto_cipher *crypto_alloc_cipher(const char *alg_name,
1438 u32 type, u32 mask)
1439{
1440 type &= ~CRYPTO_ALG_TYPE_MASK;
1441 type |= CRYPTO_ALG_TYPE_CIPHER;
1442 mask |= CRYPTO_ALG_TYPE_MASK;
1443
1444 return __crypto_cipher_cast(crypto_alloc_base(alg_name, type, mask));
1445}
1446
1447static inline struct crypto_tfm *crypto_cipher_tfm(struct crypto_cipher *tfm)
1448{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001449 return &tfm->base;
Herbert Xuf28776a2006-08-13 20:58:18 +10001450}
1451
Stephan Mueller16e61032014-11-12 05:30:06 +01001452/**
1453 * crypto_free_cipher() - zeroize and free the single block cipher handle
1454 * @tfm: cipher handle to be freed
1455 */
Herbert Xuf28776a2006-08-13 20:58:18 +10001456static inline void crypto_free_cipher(struct crypto_cipher *tfm)
1457{
1458 crypto_free_tfm(crypto_cipher_tfm(tfm));
1459}
1460
Stephan Mueller16e61032014-11-12 05:30:06 +01001461/**
1462 * crypto_has_cipher() - Search for the availability of a single block cipher
1463 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1464 * single block cipher
1465 * @type: specifies the type of the cipher
1466 * @mask: specifies the mask for the cipher
1467 *
1468 * Return: true when the single block cipher is known to the kernel crypto API;
1469 * false otherwise
1470 */
Herbert Xufce32d72006-08-26 17:35:45 +10001471static inline int crypto_has_cipher(const char *alg_name, u32 type, u32 mask)
1472{
1473 type &= ~CRYPTO_ALG_TYPE_MASK;
1474 type |= CRYPTO_ALG_TYPE_CIPHER;
1475 mask |= CRYPTO_ALG_TYPE_MASK;
1476
1477 return crypto_has_alg(alg_name, type, mask);
1478}
1479
Herbert Xuf28776a2006-08-13 20:58:18 +10001480static inline struct cipher_tfm *crypto_cipher_crt(struct crypto_cipher *tfm)
1481{
1482 return &crypto_cipher_tfm(tfm)->crt_cipher;
1483}
1484
Stephan Mueller16e61032014-11-12 05:30:06 +01001485/**
1486 * crypto_cipher_blocksize() - obtain block size for cipher
1487 * @tfm: cipher handle
1488 *
1489 * The block size for the single block cipher referenced with the cipher handle
1490 * tfm is returned. The caller may use that information to allocate appropriate
1491 * memory for the data returned by the encryption or decryption operation
1492 *
1493 * Return: block size of cipher
1494 */
Herbert Xuf28776a2006-08-13 20:58:18 +10001495static inline unsigned int crypto_cipher_blocksize(struct crypto_cipher *tfm)
1496{
1497 return crypto_tfm_alg_blocksize(crypto_cipher_tfm(tfm));
1498}
1499
1500static inline unsigned int crypto_cipher_alignmask(struct crypto_cipher *tfm)
1501{
1502 return crypto_tfm_alg_alignmask(crypto_cipher_tfm(tfm));
1503}
1504
1505static inline u32 crypto_cipher_get_flags(struct crypto_cipher *tfm)
1506{
1507 return crypto_tfm_get_flags(crypto_cipher_tfm(tfm));
1508}
1509
1510static inline void crypto_cipher_set_flags(struct crypto_cipher *tfm,
1511 u32 flags)
1512{
1513 crypto_tfm_set_flags(crypto_cipher_tfm(tfm), flags);
1514}
1515
1516static inline void crypto_cipher_clear_flags(struct crypto_cipher *tfm,
1517 u32 flags)
1518{
1519 crypto_tfm_clear_flags(crypto_cipher_tfm(tfm), flags);
1520}
1521
Stephan Mueller16e61032014-11-12 05:30:06 +01001522/**
1523 * crypto_cipher_setkey() - set key for cipher
1524 * @tfm: cipher handle
1525 * @key: buffer holding the key
1526 * @keylen: length of the key in bytes
1527 *
1528 * The caller provided key is set for the single block cipher referenced by the
1529 * cipher handle.
1530 *
1531 * Note, the key length determines the cipher type. Many block ciphers implement
1532 * different cipher modes depending on the key size, such as AES-128 vs AES-192
1533 * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
1534 * is performed.
1535 *
1536 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
1537 */
Herbert Xu7226bc872006-08-21 21:40:49 +10001538static inline int crypto_cipher_setkey(struct crypto_cipher *tfm,
1539 const u8 *key, unsigned int keylen)
1540{
1541 return crypto_cipher_crt(tfm)->cit_setkey(crypto_cipher_tfm(tfm),
1542 key, keylen);
1543}
1544
Stephan Mueller16e61032014-11-12 05:30:06 +01001545/**
1546 * crypto_cipher_encrypt_one() - encrypt one block of plaintext
1547 * @tfm: cipher handle
1548 * @dst: points to the buffer that will be filled with the ciphertext
1549 * @src: buffer holding the plaintext to be encrypted
1550 *
1551 * Invoke the encryption operation of one block. The caller must ensure that
1552 * the plaintext and ciphertext buffers are at least one block in size.
1553 */
Herbert Xuf28776a2006-08-13 20:58:18 +10001554static inline void crypto_cipher_encrypt_one(struct crypto_cipher *tfm,
1555 u8 *dst, const u8 *src)
1556{
1557 crypto_cipher_crt(tfm)->cit_encrypt_one(crypto_cipher_tfm(tfm),
1558 dst, src);
1559}
1560
Stephan Mueller16e61032014-11-12 05:30:06 +01001561/**
1562 * crypto_cipher_decrypt_one() - decrypt one block of ciphertext
1563 * @tfm: cipher handle
1564 * @dst: points to the buffer that will be filled with the plaintext
1565 * @src: buffer holding the ciphertext to be decrypted
1566 *
1567 * Invoke the decryption operation of one block. The caller must ensure that
1568 * the plaintext and ciphertext buffers are at least one block in size.
1569 */
Herbert Xuf28776a2006-08-13 20:58:18 +10001570static inline void crypto_cipher_decrypt_one(struct crypto_cipher *tfm,
1571 u8 *dst, const u8 *src)
1572{
1573 crypto_cipher_crt(tfm)->cit_decrypt_one(crypto_cipher_tfm(tfm),
1574 dst, src);
1575}
1576
Herbert Xufce32d72006-08-26 17:35:45 +10001577static inline struct crypto_comp *__crypto_comp_cast(struct crypto_tfm *tfm)
1578{
1579 return (struct crypto_comp *)tfm;
1580}
1581
1582static inline struct crypto_comp *crypto_comp_cast(struct crypto_tfm *tfm)
1583{
1584 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_COMPRESS) &
1585 CRYPTO_ALG_TYPE_MASK);
1586 return __crypto_comp_cast(tfm);
1587}
1588
1589static inline struct crypto_comp *crypto_alloc_comp(const char *alg_name,
1590 u32 type, u32 mask)
1591{
1592 type &= ~CRYPTO_ALG_TYPE_MASK;
1593 type |= CRYPTO_ALG_TYPE_COMPRESS;
1594 mask |= CRYPTO_ALG_TYPE_MASK;
1595
1596 return __crypto_comp_cast(crypto_alloc_base(alg_name, type, mask));
1597}
1598
1599static inline struct crypto_tfm *crypto_comp_tfm(struct crypto_comp *tfm)
1600{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001601 return &tfm->base;
Herbert Xufce32d72006-08-26 17:35:45 +10001602}
1603
1604static inline void crypto_free_comp(struct crypto_comp *tfm)
1605{
1606 crypto_free_tfm(crypto_comp_tfm(tfm));
1607}
1608
1609static inline int crypto_has_comp(const char *alg_name, u32 type, u32 mask)
1610{
1611 type &= ~CRYPTO_ALG_TYPE_MASK;
1612 type |= CRYPTO_ALG_TYPE_COMPRESS;
1613 mask |= CRYPTO_ALG_TYPE_MASK;
1614
1615 return crypto_has_alg(alg_name, type, mask);
1616}
1617
Herbert Xue4d5b792006-08-26 18:12:40 +10001618static inline const char *crypto_comp_name(struct crypto_comp *tfm)
1619{
1620 return crypto_tfm_alg_name(crypto_comp_tfm(tfm));
1621}
1622
Herbert Xufce32d72006-08-26 17:35:45 +10001623static inline struct compress_tfm *crypto_comp_crt(struct crypto_comp *tfm)
1624{
1625 return &crypto_comp_tfm(tfm)->crt_compress;
1626}
1627
1628static inline int crypto_comp_compress(struct crypto_comp *tfm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 const u8 *src, unsigned int slen,
1630 u8 *dst, unsigned int *dlen)
1631{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001632 return crypto_comp_crt(tfm)->cot_compress(crypto_comp_tfm(tfm),
1633 src, slen, dst, dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634}
1635
Herbert Xufce32d72006-08-26 17:35:45 +10001636static inline int crypto_comp_decompress(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_decompress(crypto_comp_tfm(tfm),
1641 src, slen, dst, dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642}
1643
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644#endif /* _LINUX_CRYPTO_H */
1645