blob: 992cfc2e5df1d9485f24234f6951c3efdab93def [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Scatterlist Cryptographic API.
3 *
4 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
5 * Copyright (c) 2002 David S. Miller (davem@redhat.com)
Herbert Xu5cb1454b2005-11-05 16:58:14 +11006 * Copyright (c) 2005 Herbert Xu <herbert@gondor.apana.org.au>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * Portions derived from Cryptoapi, by Alexander Kjeldaas <astor@fast.no>
John Anthony Kazos Jr18735dd2007-10-19 23:07:36 +02009 * and Nettle, by Niels Möller.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the Free
13 * Software Foundation; either version 2 of the License, or (at your option)
14 * any later version.
15 *
16 */
17#ifndef _LINUX_CRYPTO_H
18#define _LINUX_CRYPTO_H
19
Arun Sharma600634972011-07-26 16:09:06 -070020#include <linux/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/list.h>
Paul Gortmaker187f1882011-11-23 20:12:59 -050023#include <linux/bug.h>
Herbert Xu79911102006-08-21 21:03:52 +100024#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/string.h>
Herbert Xu79911102006-08-21 21:03:52 +100026#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
28/*
Kees Cook5d26a102014-11-20 17:05:53 -080029 * Autoloaded crypto modules should only use a prefixed name to avoid allowing
30 * arbitrary modules to be loaded. Loading from userspace may still need the
31 * unprefixed names, so retains those aliases as well.
32 * This uses __MODULE_INFO directly instead of MODULE_ALIAS because pre-4.3
33 * gcc (e.g. avr32 toolchain) uses __LINE__ for uniqueness, and this macro
34 * expands twice on the same line. Instead, use a separate base name for the
35 * alias.
36 */
37#define MODULE_ALIAS_CRYPTO(name) \
38 __MODULE_INFO(alias, alias_userspace, name); \
39 __MODULE_INFO(alias, alias_crypto, "crypto-" name)
40
41/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 * Algorithm masks and types.
43 */
Herbert Xu28259822006-08-06 21:23:26 +100044#define CRYPTO_ALG_TYPE_MASK 0x0000000f
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#define CRYPTO_ALG_TYPE_CIPHER 0x00000001
Loc Ho004a4032008-05-14 20:41:47 +080046#define CRYPTO_ALG_TYPE_COMPRESS 0x00000002
47#define CRYPTO_ALG_TYPE_AEAD 0x00000003
Herbert Xu055bcee2006-08-19 22:24:23 +100048#define CRYPTO_ALG_TYPE_BLKCIPHER 0x00000004
Herbert Xu332f88402007-11-15 22:36:07 +080049#define CRYPTO_ALG_TYPE_ABLKCIPHER 0x00000005
Herbert Xu61da88e2007-12-17 21:51:27 +080050#define CRYPTO_ALG_TYPE_GIVCIPHER 0x00000006
Salvatore Benedetto4e5f2c42016-06-22 17:49:13 +010051#define CRYPTO_ALG_TYPE_KPP 0x00000008
Neil Horman17f0f4a2008-08-14 22:15:52 +100052#define CRYPTO_ALG_TYPE_RNG 0x0000000c
Tadeusz Struk3c339ab2015-06-16 10:30:55 -070053#define CRYPTO_ALG_TYPE_AKCIPHER 0x0000000d
Giovanni Cabiddu63044c42016-06-02 13:28:55 +010054#define CRYPTO_ALG_TYPE_DIGEST 0x0000000e
55#define CRYPTO_ALG_TYPE_HASH 0x0000000e
56#define CRYPTO_ALG_TYPE_SHASH 0x0000000e
57#define CRYPTO_ALG_TYPE_AHASH 0x0000000f
Herbert Xu055bcee2006-08-19 22:24:23 +100058
59#define CRYPTO_ALG_TYPE_HASH_MASK 0x0000000e
Giovanni Cabiddu63044c42016-06-02 13:28:55 +010060#define CRYPTO_ALG_TYPE_AHASH_MASK 0x0000000e
Herbert Xu332f88402007-11-15 22:36:07 +080061#define CRYPTO_ALG_TYPE_BLKCIPHER_MASK 0x0000000c
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Herbert Xu28259822006-08-06 21:23:26 +100063#define CRYPTO_ALG_LARVAL 0x00000010
Herbert Xu6bfd4802006-09-21 11:39:29 +100064#define CRYPTO_ALG_DEAD 0x00000020
65#define CRYPTO_ALG_DYING 0x00000040
Herbert Xuf3f632d2006-08-06 23:12:59 +100066#define CRYPTO_ALG_ASYNC 0x00000080
Herbert Xu28259822006-08-06 21:23:26 +100067
Linus Torvalds1da177e2005-04-16 15:20:36 -070068/*
Herbert Xu60104392006-08-26 18:34:10 +100069 * Set this bit if and only if the algorithm requires another algorithm of
70 * the same type to handle corner cases.
71 */
72#define CRYPTO_ALG_NEED_FALLBACK 0x00000100
73
74/*
Herbert Xuecfc4322007-12-05 21:08:36 +110075 * This bit is set for symmetric key ciphers that have already been wrapped
76 * with a generic IV generator to prevent them from being wrapped again.
77 */
78#define CRYPTO_ALG_GENIV 0x00000200
79
80/*
Herbert Xu73d38642008-08-03 21:15:23 +080081 * Set if the algorithm has passed automated run-time testing. Note that
82 * if there is no run-time testing for a given algorithm it is considered
83 * to have passed.
84 */
85
86#define CRYPTO_ALG_TESTED 0x00000400
87
88/*
Steffen Klassert64a947b2011-09-27 07:21:26 +020089 * Set if the algorithm is an instance that is build from templates.
90 */
91#define CRYPTO_ALG_INSTANCE 0x00000800
92
Nikos Mavrogiannopoulosd912bb72011-11-01 13:39:56 +010093/* Set this bit if the algorithm provided is hardware accelerated but
94 * not available to userspace via instruction set or so.
95 */
96#define CRYPTO_ALG_KERN_DRIVER_ONLY 0x00001000
97
Steffen Klassert64a947b2011-09-27 07:21:26 +020098/*
Stephan Mueller06ca7f62015-03-30 21:55:52 +020099 * Mark a cipher as a service implementation only usable by another
100 * cipher and never by a normal user of the kernel crypto API
101 */
102#define CRYPTO_ALG_INTERNAL 0x00002000
103
104/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 * Transform masks and values (for crt_flags).
106 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107#define CRYPTO_TFM_REQ_MASK 0x000fff00
108#define CRYPTO_TFM_RES_MASK 0xfff00000
109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110#define CRYPTO_TFM_REQ_WEAK_KEY 0x00000100
Herbert Xu64baf3c2005-09-01 17:43:05 -0700111#define CRYPTO_TFM_REQ_MAY_SLEEP 0x00000200
Herbert Xu32e3983f2007-03-24 14:35:34 +1100112#define CRYPTO_TFM_REQ_MAY_BACKLOG 0x00000400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113#define CRYPTO_TFM_RES_WEAK_KEY 0x00100000
114#define CRYPTO_TFM_RES_BAD_KEY_LEN 0x00200000
115#define CRYPTO_TFM_RES_BAD_KEY_SCHED 0x00400000
116#define CRYPTO_TFM_RES_BAD_BLOCK_LEN 0x00800000
117#define CRYPTO_TFM_RES_BAD_FLAGS 0x01000000
118
119/*
120 * Miscellaneous stuff.
121 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122#define CRYPTO_MAX_ALG_NAME 64
123
Herbert Xu79911102006-08-21 21:03:52 +1000124/*
125 * The macro CRYPTO_MINALIGN_ATTR (along with the void * type in the actual
126 * declaration) is used to ensure that the crypto_tfm context structure is
127 * aligned correctly for the given architecture so that there are no alignment
128 * faults for C data types. In particular, this is required on platforms such
129 * as arm where pointers are 32-bit aligned but there are data types such as
130 * u64 which require 64-bit alignment.
131 */
Herbert Xu79911102006-08-21 21:03:52 +1000132#define CRYPTO_MINALIGN ARCH_KMALLOC_MINALIGN
Herbert Xu79911102006-08-21 21:03:52 +1000133
Herbert Xu79911102006-08-21 21:03:52 +1000134#define CRYPTO_MINALIGN_ATTR __attribute__ ((__aligned__(CRYPTO_MINALIGN)))
Herbert Xu79911102006-08-21 21:03:52 +1000135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136struct scatterlist;
Herbert Xu32e3983f2007-03-24 14:35:34 +1100137struct crypto_ablkcipher;
138struct crypto_async_request;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000139struct crypto_blkcipher;
Herbert Xu40725182005-07-06 13:51:52 -0700140struct crypto_tfm;
Herbert Xue853c3c2006-08-22 00:06:54 +1000141struct crypto_type;
Herbert Xu61da88e2007-12-17 21:51:27 +0800142struct skcipher_givcrypt_request;
Herbert Xu40725182005-07-06 13:51:52 -0700143
Herbert Xu32e3983f2007-03-24 14:35:34 +1100144typedef void (*crypto_completion_t)(struct crypto_async_request *req, int err);
145
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100146/**
147 * DOC: Block Cipher Context Data Structures
148 *
149 * These data structures define the operating context for each block cipher
150 * type.
151 */
152
Herbert Xu32e3983f2007-03-24 14:35:34 +1100153struct crypto_async_request {
154 struct list_head list;
155 crypto_completion_t complete;
156 void *data;
157 struct crypto_tfm *tfm;
158
159 u32 flags;
160};
161
162struct ablkcipher_request {
163 struct crypto_async_request base;
164
165 unsigned int nbytes;
166
167 void *info;
168
169 struct scatterlist *src;
170 struct scatterlist *dst;
171
172 void *__ctx[] CRYPTO_MINALIGN_ATTR;
173};
174
Herbert Xu5cde0af2006-08-22 00:07:53 +1000175struct blkcipher_desc {
176 struct crypto_blkcipher *tfm;
177 void *info;
178 u32 flags;
179};
180
Herbert Xu40725182005-07-06 13:51:52 -0700181struct cipher_desc {
182 struct crypto_tfm *tfm;
Herbert Xu6c2bb982006-05-16 22:09:29 +1000183 void (*crfn)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Herbert Xu40725182005-07-06 13:51:52 -0700184 unsigned int (*prfn)(const struct cipher_desc *desc, u8 *dst,
185 const u8 *src, unsigned int nbytes);
186 void *info;
187};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100189/**
190 * DOC: Block Cipher Algorithm Definitions
191 *
192 * These data structures define modular crypto algorithm implementations,
193 * managed via crypto_register_alg() and crypto_unregister_alg().
194 */
195
196/**
197 * struct ablkcipher_alg - asynchronous block cipher definition
198 * @min_keysize: Minimum key size supported by the transformation. This is the
199 * smallest key length supported by this transformation algorithm.
200 * This must be set to one of the pre-defined values as this is
201 * not hardware specific. Possible values for this field can be
202 * found via git grep "_MIN_KEY_SIZE" include/crypto/
203 * @max_keysize: Maximum key size supported by the transformation. This is the
204 * largest key length supported by this transformation algorithm.
205 * This must be set to one of the pre-defined values as this is
206 * not hardware specific. Possible values for this field can be
207 * found via git grep "_MAX_KEY_SIZE" include/crypto/
208 * @setkey: Set key for the transformation. This function is used to either
209 * program a supplied key into the hardware or store the key in the
210 * transformation context for programming it later. Note that this
211 * function does modify the transformation context. This function can
212 * be called multiple times during the existence of the transformation
213 * object, so one must make sure the key is properly reprogrammed into
214 * the hardware. This function is also responsible for checking the key
215 * length for validity. In case a software fallback was put in place in
216 * the @cra_init call, this function might need to use the fallback if
217 * the algorithm doesn't support all of the key sizes.
218 * @encrypt: Encrypt a scatterlist of blocks. This function is used to encrypt
219 * the supplied scatterlist containing the blocks of data. The crypto
220 * API consumer is responsible for aligning the entries of the
221 * scatterlist properly and making sure the chunks are correctly
222 * sized. In case a software fallback was put in place in the
223 * @cra_init call, this function might need to use the fallback if
224 * the algorithm doesn't support all of the key sizes. In case the
225 * key was stored in transformation context, the key might need to be
226 * re-programmed into the hardware in this function. This function
227 * shall not modify the transformation context, as this function may
228 * be called in parallel with the same transformation object.
229 * @decrypt: Decrypt a single block. This is a reverse counterpart to @encrypt
230 * and the conditions are exactly the same.
231 * @givencrypt: Update the IV for encryption. With this function, a cipher
232 * implementation may provide the function on how to update the IV
233 * for encryption.
234 * @givdecrypt: Update the IV for decryption. This is the reverse of
235 * @givencrypt .
236 * @geniv: The transformation implementation may use an "IV generator" provided
237 * by the kernel crypto API. Several use cases have a predefined
238 * approach how IVs are to be updated. For such use cases, the kernel
239 * crypto API provides ready-to-use implementations that can be
240 * referenced with this variable.
241 * @ivsize: IV size applicable for transformation. The consumer must provide an
242 * IV of exactly that size to perform the encrypt or decrypt operation.
243 *
244 * All fields except @givencrypt , @givdecrypt , @geniv and @ivsize are
245 * mandatory and must be filled.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 */
Herbert Xub5b7f082007-04-16 20:48:54 +1000247struct ablkcipher_alg {
248 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
249 unsigned int keylen);
250 int (*encrypt)(struct ablkcipher_request *req);
251 int (*decrypt)(struct ablkcipher_request *req);
Herbert Xu61da88e2007-12-17 21:51:27 +0800252 int (*givencrypt)(struct skcipher_givcrypt_request *req);
253 int (*givdecrypt)(struct skcipher_givcrypt_request *req);
Herbert Xub5b7f082007-04-16 20:48:54 +1000254
Herbert Xu23508e12007-11-27 21:33:24 +0800255 const char *geniv;
256
Herbert Xub5b7f082007-04-16 20:48:54 +1000257 unsigned int min_keysize;
258 unsigned int max_keysize;
259 unsigned int ivsize;
260};
261
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100262/**
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100263 * struct blkcipher_alg - synchronous block cipher definition
264 * @min_keysize: see struct ablkcipher_alg
265 * @max_keysize: see struct ablkcipher_alg
266 * @setkey: see struct ablkcipher_alg
267 * @encrypt: see struct ablkcipher_alg
268 * @decrypt: see struct ablkcipher_alg
269 * @geniv: see struct ablkcipher_alg
270 * @ivsize: see struct ablkcipher_alg
271 *
272 * All fields except @geniv and @ivsize are mandatory and must be filled.
273 */
Herbert Xu5cde0af2006-08-22 00:07:53 +1000274struct blkcipher_alg {
275 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
276 unsigned int keylen);
277 int (*encrypt)(struct blkcipher_desc *desc,
278 struct scatterlist *dst, struct scatterlist *src,
279 unsigned int nbytes);
280 int (*decrypt)(struct blkcipher_desc *desc,
281 struct scatterlist *dst, struct scatterlist *src,
282 unsigned int nbytes);
283
Herbert Xu23508e12007-11-27 21:33:24 +0800284 const char *geniv;
285
Herbert Xu5cde0af2006-08-22 00:07:53 +1000286 unsigned int min_keysize;
287 unsigned int max_keysize;
288 unsigned int ivsize;
289};
290
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100291/**
292 * struct cipher_alg - single-block symmetric ciphers definition
293 * @cia_min_keysize: Minimum key size supported by the transformation. This is
294 * the smallest key length supported by this transformation
295 * algorithm. This must be set to one of the pre-defined
296 * values as this is not hardware specific. Possible values
297 * for this field can be found via git grep "_MIN_KEY_SIZE"
298 * include/crypto/
299 * @cia_max_keysize: Maximum key size supported by the transformation. This is
300 * the largest key length supported by this transformation
301 * algorithm. This must be set to one of the pre-defined values
302 * as this is not hardware specific. Possible values for this
303 * field can be found via git grep "_MAX_KEY_SIZE"
304 * include/crypto/
305 * @cia_setkey: Set key for the transformation. This function is used to either
306 * program a supplied key into the hardware or store the key in the
307 * transformation context for programming it later. Note that this
308 * function does modify the transformation context. This function
309 * can be called multiple times during the existence of the
310 * transformation object, so one must make sure the key is properly
311 * reprogrammed into the hardware. This function is also
312 * responsible for checking the key length for validity.
313 * @cia_encrypt: Encrypt a single block. This function is used to encrypt a
314 * single block of data, which must be @cra_blocksize big. This
315 * always operates on a full @cra_blocksize and it is not possible
316 * to encrypt a block of smaller size. The supplied buffers must
317 * therefore also be at least of @cra_blocksize size. Both the
318 * input and output buffers are always aligned to @cra_alignmask.
319 * In case either of the input or output buffer supplied by user
320 * of the crypto API is not aligned to @cra_alignmask, the crypto
321 * API will re-align the buffers. The re-alignment means that a
322 * new buffer will be allocated, the data will be copied into the
323 * new buffer, then the processing will happen on the new buffer,
324 * then the data will be copied back into the original buffer and
325 * finally the new buffer will be freed. In case a software
326 * fallback was put in place in the @cra_init call, this function
327 * might need to use the fallback if the algorithm doesn't support
328 * all of the key sizes. In case the key was stored in
329 * transformation context, the key might need to be re-programmed
330 * into the hardware in this function. This function shall not
331 * modify the transformation context, as this function may be
332 * called in parallel with the same transformation object.
333 * @cia_decrypt: Decrypt a single block. This is a reverse counterpart to
334 * @cia_encrypt, and the conditions are exactly the same.
335 *
336 * All fields are mandatory and must be filled.
337 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338struct cipher_alg {
339 unsigned int cia_min_keysize;
340 unsigned int cia_max_keysize;
Herbert Xu6c2bb982006-05-16 22:09:29 +1000341 int (*cia_setkey)(struct crypto_tfm *tfm, const u8 *key,
Herbert Xu560c06a2006-08-13 14:16:39 +1000342 unsigned int keylen);
Herbert Xu6c2bb982006-05-16 22:09:29 +1000343 void (*cia_encrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
344 void (*cia_decrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345};
346
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347struct compress_alg {
Herbert Xu6c2bb982006-05-16 22:09:29 +1000348 int (*coa_compress)(struct crypto_tfm *tfm, const u8 *src,
349 unsigned int slen, u8 *dst, unsigned int *dlen);
350 int (*coa_decompress)(struct crypto_tfm *tfm, const u8 *src,
351 unsigned int slen, u8 *dst, unsigned int *dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352};
353
Neil Horman17f0f4a2008-08-14 22:15:52 +1000354
Herbert Xub5b7f082007-04-16 20:48:54 +1000355#define cra_ablkcipher cra_u.ablkcipher
Herbert Xu5cde0af2006-08-22 00:07:53 +1000356#define cra_blkcipher cra_u.blkcipher
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357#define cra_cipher cra_u.cipher
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358#define cra_compress cra_u.compress
359
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100360/**
361 * struct crypto_alg - definition of a cryptograpic cipher algorithm
362 * @cra_flags: Flags describing this transformation. See include/linux/crypto.h
363 * CRYPTO_ALG_* flags for the flags which go in here. Those are
364 * used for fine-tuning the description of the transformation
365 * algorithm.
366 * @cra_blocksize: Minimum block size of this transformation. The size in bytes
367 * of the smallest possible unit which can be transformed with
368 * this algorithm. The users must respect this value.
369 * In case of HASH transformation, it is possible for a smaller
370 * block than @cra_blocksize to be passed to the crypto API for
371 * transformation, in case of any other transformation type, an
372 * error will be returned upon any attempt to transform smaller
373 * than @cra_blocksize chunks.
374 * @cra_ctxsize: Size of the operational context of the transformation. This
375 * value informs the kernel crypto API about the memory size
376 * needed to be allocated for the transformation context.
377 * @cra_alignmask: Alignment mask for the input and output data buffer. The data
378 * buffer containing the input data for the algorithm must be
379 * aligned to this alignment mask. The data buffer for the
380 * output data must be aligned to this alignment mask. Note that
381 * the Crypto API will do the re-alignment in software, but
382 * only under special conditions and there is a performance hit.
383 * The re-alignment happens at these occasions for different
384 * @cra_u types: cipher -- For both input data and output data
385 * buffer; ahash -- For output hash destination buf; shash --
386 * For output hash destination buf.
387 * This is needed on hardware which is flawed by design and
388 * cannot pick data from arbitrary addresses.
389 * @cra_priority: Priority of this transformation implementation. In case
390 * multiple transformations with same @cra_name are available to
391 * the Crypto API, the kernel will use the one with highest
392 * @cra_priority.
393 * @cra_name: Generic name (usable by multiple implementations) of the
394 * transformation algorithm. This is the name of the transformation
395 * itself. This field is used by the kernel when looking up the
396 * providers of particular transformation.
397 * @cra_driver_name: Unique name of the transformation provider. This is the
398 * name of the provider of the transformation. This can be any
399 * arbitrary value, but in the usual case, this contains the
400 * name of the chip or provider and the name of the
401 * transformation algorithm.
402 * @cra_type: Type of the cryptographic transformation. This is a pointer to
403 * struct crypto_type, which implements callbacks common for all
Masanari Iida12f7c142015-06-04 00:01:21 +0900404 * transformation types. There are multiple options:
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100405 * &crypto_blkcipher_type, &crypto_ablkcipher_type,
Herbert Xub0d955b2015-08-14 15:30:41 +0800406 * &crypto_ahash_type, &crypto_rng_type.
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100407 * This field might be empty. In that case, there are no common
408 * callbacks. This is the case for: cipher, compress, shash.
409 * @cra_u: Callbacks implementing the transformation. This is a union of
410 * multiple structures. Depending on the type of transformation selected
411 * by @cra_type and @cra_flags above, the associated structure must be
412 * filled with callbacks. This field might be empty. This is the case
413 * for ahash, shash.
414 * @cra_init: Initialize the cryptographic transformation object. This function
415 * is used to initialize the cryptographic transformation object.
416 * This function is called only once at the instantiation time, right
417 * after the transformation context was allocated. In case the
418 * cryptographic hardware has some special requirements which need to
419 * be handled by software, this function shall check for the precise
420 * requirement of the transformation and put any software fallbacks
421 * in place.
422 * @cra_exit: Deinitialize the cryptographic transformation object. This is a
423 * counterpart to @cra_init, used to remove various changes set in
424 * @cra_init.
425 * @cra_module: Owner of this transformation implementation. Set to THIS_MODULE
426 * @cra_list: internally used
427 * @cra_users: internally used
428 * @cra_refcnt: internally used
429 * @cra_destroy: internally used
430 *
431 * The struct crypto_alg describes a generic Crypto API algorithm and is common
432 * for all of the transformations. Any variable not documented here shall not
433 * be used by a cipher implementation as it is internal to the Crypto API.
434 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435struct crypto_alg {
436 struct list_head cra_list;
Herbert Xu6bfd4802006-09-21 11:39:29 +1000437 struct list_head cra_users;
438
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 u32 cra_flags;
440 unsigned int cra_blocksize;
441 unsigned int cra_ctxsize;
Herbert Xu95477372005-07-06 13:52:09 -0700442 unsigned int cra_alignmask;
Herbert Xu5cb1454b2005-11-05 16:58:14 +1100443
444 int cra_priority;
Herbert Xu6521f302006-08-06 20:28:44 +1000445 atomic_t cra_refcnt;
Herbert Xu5cb1454b2005-11-05 16:58:14 +1100446
Herbert Xud913ea02006-05-21 08:45:26 +1000447 char cra_name[CRYPTO_MAX_ALG_NAME];
448 char cra_driver_name[CRYPTO_MAX_ALG_NAME];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
Herbert Xue853c3c2006-08-22 00:06:54 +1000450 const struct crypto_type *cra_type;
451
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 union {
Herbert Xub5b7f082007-04-16 20:48:54 +1000453 struct ablkcipher_alg ablkcipher;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000454 struct blkcipher_alg blkcipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 struct cipher_alg cipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 struct compress_alg compress;
457 } cra_u;
Herbert Xuc7fc0592006-05-24 13:02:26 +1000458
459 int (*cra_init)(struct crypto_tfm *tfm);
460 void (*cra_exit)(struct crypto_tfm *tfm);
Herbert Xu6521f302006-08-06 20:28:44 +1000461 void (*cra_destroy)(struct crypto_alg *alg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
463 struct module *cra_module;
Herbert Xuedf18b92015-06-18 14:00:48 +0800464} CRYPTO_MINALIGN_ATTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
466/*
467 * Algorithm registration interface.
468 */
469int crypto_register_alg(struct crypto_alg *alg);
470int crypto_unregister_alg(struct crypto_alg *alg);
Mark Brown4b004342012-01-17 23:34:26 +0000471int crypto_register_algs(struct crypto_alg *algs, int count);
472int crypto_unregister_algs(struct crypto_alg *algs, int count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
474/*
475 * Algorithm query interface.
476 */
Herbert Xufce32d72006-08-26 17:35:45 +1000477int crypto_has_alg(const char *name, u32 type, u32 mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
479/*
480 * Transforms: user-instantiated objects which encapsulate algorithms
Herbert Xu6d7d684d2006-07-30 11:53:01 +1000481 * and core processing logic. Managed via crypto_alloc_*() and
482 * crypto_free_*(), as well as the various helpers below.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Herbert Xu32e3983f2007-03-24 14:35:34 +1100485struct ablkcipher_tfm {
486 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
487 unsigned int keylen);
488 int (*encrypt)(struct ablkcipher_request *req);
489 int (*decrypt)(struct ablkcipher_request *req);
Herbert Xu61da88e2007-12-17 21:51:27 +0800490 int (*givencrypt)(struct skcipher_givcrypt_request *req);
491 int (*givdecrypt)(struct skcipher_givcrypt_request *req);
492
Herbert Xuecfc4322007-12-05 21:08:36 +1100493 struct crypto_ablkcipher *base;
494
Herbert Xu32e3983f2007-03-24 14:35:34 +1100495 unsigned int ivsize;
496 unsigned int reqsize;
497};
498
Herbert Xu5cde0af2006-08-22 00:07:53 +1000499struct blkcipher_tfm {
500 void *iv;
501 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
502 unsigned int keylen);
503 int (*encrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
504 struct scatterlist *src, unsigned int nbytes);
505 int (*decrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
506 struct scatterlist *src, unsigned int nbytes);
507};
508
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509struct cipher_tfm {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 int (*cit_setkey)(struct crypto_tfm *tfm,
511 const u8 *key, unsigned int keylen);
Herbert Xuf28776a2006-08-13 20:58:18 +1000512 void (*cit_encrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
513 void (*cit_decrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514};
515
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516struct compress_tfm {
517 int (*cot_compress)(struct crypto_tfm *tfm,
518 const u8 *src, unsigned int slen,
519 u8 *dst, unsigned int *dlen);
520 int (*cot_decompress)(struct crypto_tfm *tfm,
521 const u8 *src, unsigned int slen,
522 u8 *dst, unsigned int *dlen);
523};
524
Herbert Xu32e3983f2007-03-24 14:35:34 +1100525#define crt_ablkcipher crt_u.ablkcipher
Herbert Xu5cde0af2006-08-22 00:07:53 +1000526#define crt_blkcipher crt_u.blkcipher
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527#define crt_cipher crt_u.cipher
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528#define crt_compress crt_u.compress
529
530struct crypto_tfm {
531
532 u32 crt_flags;
533
534 union {
Herbert Xu32e3983f2007-03-24 14:35:34 +1100535 struct ablkcipher_tfm ablkcipher;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000536 struct blkcipher_tfm blkcipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 struct cipher_tfm cipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 struct compress_tfm compress;
539 } crt_u;
Herbert Xu4a779482008-09-13 18:19:03 -0700540
541 void (*exit)(struct crypto_tfm *tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
543 struct crypto_alg *__crt_alg;
Herbert Xuf10b7892006-01-25 22:34:01 +1100544
Herbert Xu79911102006-08-21 21:03:52 +1000545 void *__crt_ctx[] CRYPTO_MINALIGN_ATTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546};
547
Herbert Xu32e3983f2007-03-24 14:35:34 +1100548struct crypto_ablkcipher {
549 struct crypto_tfm base;
550};
551
Herbert Xu5cde0af2006-08-22 00:07:53 +1000552struct crypto_blkcipher {
553 struct crypto_tfm base;
554};
555
Herbert Xu78a1fe42006-12-24 10:02:00 +1100556struct crypto_cipher {
557 struct crypto_tfm base;
558};
559
560struct crypto_comp {
561 struct crypto_tfm base;
562};
563
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000564enum {
565 CRYPTOA_UNSPEC,
566 CRYPTOA_ALG,
Herbert Xuebc610e2007-01-01 18:37:02 +1100567 CRYPTOA_TYPE,
Herbert Xu39e1ee012007-08-29 19:27:26 +0800568 CRYPTOA_U32,
Herbert Xuebc610e2007-01-01 18:37:02 +1100569 __CRYPTOA_MAX,
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000570};
571
Herbert Xuebc610e2007-01-01 18:37:02 +1100572#define CRYPTOA_MAX (__CRYPTOA_MAX - 1)
573
Herbert Xu39e1ee012007-08-29 19:27:26 +0800574/* Maximum number of (rtattr) parameters for each template. */
575#define CRYPTO_MAX_ATTRS 32
576
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000577struct crypto_attr_alg {
578 char name[CRYPTO_MAX_ALG_NAME];
579};
580
Herbert Xuebc610e2007-01-01 18:37:02 +1100581struct crypto_attr_type {
582 u32 type;
583 u32 mask;
584};
585
Herbert Xu39e1ee012007-08-29 19:27:26 +0800586struct crypto_attr_u32 {
587 u32 num;
588};
589
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590/*
591 * Transform user interface.
592 */
593
Herbert Xu6d7d684d2006-07-30 11:53:01 +1000594struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask);
Herbert Xu7b2cd922009-02-05 16:48:24 +1100595void crypto_destroy_tfm(void *mem, struct crypto_tfm *tfm);
596
597static inline void crypto_free_tfm(struct crypto_tfm *tfm)
598{
599 return crypto_destroy_tfm(tfm, tfm);
600}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
Herbert Xuda7f0332008-07-31 17:08:25 +0800602int alg_test(const char *driver, const char *alg, u32 type, u32 mask);
603
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604/*
605 * Transform helpers which query the underlying algorithm.
606 */
607static inline const char *crypto_tfm_alg_name(struct crypto_tfm *tfm)
608{
609 return tfm->__crt_alg->cra_name;
610}
611
Michal Ludvigb14cdd62006-07-09 09:02:24 +1000612static inline const char *crypto_tfm_alg_driver_name(struct crypto_tfm *tfm)
613{
614 return tfm->__crt_alg->cra_driver_name;
615}
616
617static inline int crypto_tfm_alg_priority(struct crypto_tfm *tfm)
618{
619 return tfm->__crt_alg->cra_priority;
620}
621
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm)
623{
624 return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK;
625}
626
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm)
628{
629 return tfm->__crt_alg->cra_blocksize;
630}
631
Herbert Xufbdae9f2005-07-06 13:53:29 -0700632static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm *tfm)
633{
634 return tfm->__crt_alg->cra_alignmask;
635}
636
Herbert Xuf28776a2006-08-13 20:58:18 +1000637static inline u32 crypto_tfm_get_flags(struct crypto_tfm *tfm)
638{
639 return tfm->crt_flags;
640}
641
642static inline void crypto_tfm_set_flags(struct crypto_tfm *tfm, u32 flags)
643{
644 tfm->crt_flags |= flags;
645}
646
647static inline void crypto_tfm_clear_flags(struct crypto_tfm *tfm, u32 flags)
648{
649 tfm->crt_flags &= ~flags;
650}
651
Herbert Xu40725182005-07-06 13:51:52 -0700652static inline void *crypto_tfm_ctx(struct crypto_tfm *tfm)
653{
Herbert Xuf10b7892006-01-25 22:34:01 +1100654 return tfm->__crt_ctx;
655}
656
657static inline unsigned int crypto_tfm_ctx_alignment(void)
658{
659 struct crypto_tfm *tfm;
660 return __alignof__(tfm->__crt_ctx);
Herbert Xu40725182005-07-06 13:51:52 -0700661}
662
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663/*
664 * API wrappers.
665 */
Herbert Xu32e3983f2007-03-24 14:35:34 +1100666static inline struct crypto_ablkcipher *__crypto_ablkcipher_cast(
667 struct crypto_tfm *tfm)
668{
669 return (struct crypto_ablkcipher *)tfm;
670}
671
Herbert Xu378f4f52007-12-17 20:07:31 +0800672static inline u32 crypto_skcipher_type(u32 type)
673{
Herbert Xuecfc4322007-12-05 21:08:36 +1100674 type &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
Herbert Xu378f4f52007-12-17 20:07:31 +0800675 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
676 return type;
677}
678
679static inline u32 crypto_skcipher_mask(u32 mask)
680{
Herbert Xuecfc4322007-12-05 21:08:36 +1100681 mask &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
Herbert Xu378f4f52007-12-17 20:07:31 +0800682 mask |= CRYPTO_ALG_TYPE_BLKCIPHER_MASK;
683 return mask;
684}
685
Stephan Muellerf13ec332014-11-12 05:28:22 +0100686/**
687 * DOC: Asynchronous Block Cipher API
688 *
689 * Asynchronous block cipher API is used with the ciphers of type
690 * CRYPTO_ALG_TYPE_ABLKCIPHER (listed as type "ablkcipher" in /proc/crypto).
691 *
692 * Asynchronous cipher operations imply that the function invocation for a
693 * cipher request returns immediately before the completion of the operation.
694 * The cipher request is scheduled as a separate kernel thread and therefore
695 * load-balanced on the different CPUs via the process scheduler. To allow
696 * the kernel crypto API to inform the caller about the completion of a cipher
697 * request, the caller must provide a callback function. That function is
698 * invoked with the cipher handle when the request completes.
699 *
700 * To support the asynchronous operation, additional information than just the
701 * cipher handle must be supplied to the kernel crypto API. That additional
702 * information is given by filling in the ablkcipher_request data structure.
703 *
704 * For the asynchronous block cipher API, the state is maintained with the tfm
705 * cipher handle. A single tfm can be used across multiple calls and in
706 * parallel. For asynchronous block cipher calls, context data supplied and
707 * only used by the caller can be referenced the request data structure in
708 * addition to the IV used for the cipher request. The maintenance of such
709 * state information would be important for a crypto driver implementer to
710 * have, because when calling the callback function upon completion of the
711 * cipher operation, that callback function may need some information about
712 * which operation just finished if it invoked multiple in parallel. This
713 * state information is unused by the kernel crypto API.
714 */
715
716/**
717 * crypto_alloc_ablkcipher() - allocate asynchronous block cipher handle
718 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
719 * ablkcipher cipher
720 * @type: specifies the type of the cipher
721 * @mask: specifies the mask for the cipher
722 *
723 * Allocate a cipher handle for an ablkcipher. The returned struct
724 * crypto_ablkcipher is the cipher handle that is required for any subsequent
725 * API invocation for that ablkcipher.
726 *
727 * Return: allocated cipher handle in case of success; IS_ERR() is true in case
728 * of an error, PTR_ERR() returns the error code.
729 */
Herbert Xub9c55aa2007-12-04 12:46:48 +1100730struct crypto_ablkcipher *crypto_alloc_ablkcipher(const char *alg_name,
731 u32 type, u32 mask);
Herbert Xu32e3983f2007-03-24 14:35:34 +1100732
733static inline struct crypto_tfm *crypto_ablkcipher_tfm(
734 struct crypto_ablkcipher *tfm)
735{
736 return &tfm->base;
737}
738
Stephan Muellerf13ec332014-11-12 05:28:22 +0100739/**
740 * crypto_free_ablkcipher() - zeroize and free cipher handle
741 * @tfm: cipher handle to be freed
742 */
Herbert Xu32e3983f2007-03-24 14:35:34 +1100743static inline void crypto_free_ablkcipher(struct crypto_ablkcipher *tfm)
744{
745 crypto_free_tfm(crypto_ablkcipher_tfm(tfm));
746}
747
Stephan Muellerf13ec332014-11-12 05:28:22 +0100748/**
749 * crypto_has_ablkcipher() - Search for the availability of an ablkcipher.
750 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
751 * ablkcipher
752 * @type: specifies the type of the cipher
753 * @mask: specifies the mask for the cipher
754 *
755 * Return: true when the ablkcipher is known to the kernel crypto API; false
756 * otherwise
757 */
Herbert Xu32e3983f2007-03-24 14:35:34 +1100758static inline int crypto_has_ablkcipher(const char *alg_name, u32 type,
759 u32 mask)
760{
Herbert Xu378f4f52007-12-17 20:07:31 +0800761 return crypto_has_alg(alg_name, crypto_skcipher_type(type),
762 crypto_skcipher_mask(mask));
Herbert Xu32e3983f2007-03-24 14:35:34 +1100763}
764
765static inline struct ablkcipher_tfm *crypto_ablkcipher_crt(
766 struct crypto_ablkcipher *tfm)
767{
768 return &crypto_ablkcipher_tfm(tfm)->crt_ablkcipher;
769}
770
Stephan Muellerf13ec332014-11-12 05:28:22 +0100771/**
772 * crypto_ablkcipher_ivsize() - obtain IV size
773 * @tfm: cipher handle
774 *
775 * The size of the IV for the ablkcipher referenced by the cipher handle is
776 * returned. This IV size may be zero if the cipher does not need an IV.
777 *
778 * Return: IV size in bytes
779 */
Herbert Xu32e3983f2007-03-24 14:35:34 +1100780static inline unsigned int crypto_ablkcipher_ivsize(
781 struct crypto_ablkcipher *tfm)
782{
783 return crypto_ablkcipher_crt(tfm)->ivsize;
784}
785
Stephan Muellerf13ec332014-11-12 05:28:22 +0100786/**
787 * crypto_ablkcipher_blocksize() - obtain block size of cipher
788 * @tfm: cipher handle
789 *
790 * The block size for the ablkcipher referenced with the cipher handle is
791 * returned. The caller may use that information to allocate appropriate
792 * memory for the data returned by the encryption or decryption operation
793 *
794 * Return: block size of cipher
795 */
Herbert Xu32e3983f2007-03-24 14:35:34 +1100796static inline unsigned int crypto_ablkcipher_blocksize(
797 struct crypto_ablkcipher *tfm)
798{
799 return crypto_tfm_alg_blocksize(crypto_ablkcipher_tfm(tfm));
800}
801
802static inline unsigned int crypto_ablkcipher_alignmask(
803 struct crypto_ablkcipher *tfm)
804{
805 return crypto_tfm_alg_alignmask(crypto_ablkcipher_tfm(tfm));
806}
807
808static inline u32 crypto_ablkcipher_get_flags(struct crypto_ablkcipher *tfm)
809{
810 return crypto_tfm_get_flags(crypto_ablkcipher_tfm(tfm));
811}
812
813static inline void crypto_ablkcipher_set_flags(struct crypto_ablkcipher *tfm,
814 u32 flags)
815{
816 crypto_tfm_set_flags(crypto_ablkcipher_tfm(tfm), flags);
817}
818
819static inline void crypto_ablkcipher_clear_flags(struct crypto_ablkcipher *tfm,
820 u32 flags)
821{
822 crypto_tfm_clear_flags(crypto_ablkcipher_tfm(tfm), flags);
823}
824
Stephan Muellerf13ec332014-11-12 05:28:22 +0100825/**
826 * crypto_ablkcipher_setkey() - set key for cipher
827 * @tfm: cipher handle
828 * @key: buffer holding the key
829 * @keylen: length of the key in bytes
830 *
831 * The caller provided key is set for the ablkcipher referenced by the cipher
832 * handle.
833 *
834 * Note, the key length determines the cipher type. Many block ciphers implement
835 * different cipher modes depending on the key size, such as AES-128 vs AES-192
836 * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
837 * is performed.
838 *
839 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
840 */
Herbert Xu32e3983f2007-03-24 14:35:34 +1100841static inline int crypto_ablkcipher_setkey(struct crypto_ablkcipher *tfm,
842 const u8 *key, unsigned int keylen)
843{
Herbert Xuecfc4322007-12-05 21:08:36 +1100844 struct ablkcipher_tfm *crt = crypto_ablkcipher_crt(tfm);
845
846 return crt->setkey(crt->base, key, keylen);
Herbert Xu32e3983f2007-03-24 14:35:34 +1100847}
848
Stephan Muellerf13ec332014-11-12 05:28:22 +0100849/**
850 * crypto_ablkcipher_reqtfm() - obtain cipher handle from request
851 * @req: ablkcipher_request out of which the cipher handle is to be obtained
852 *
853 * Return the crypto_ablkcipher handle when furnishing an ablkcipher_request
854 * data structure.
855 *
856 * Return: crypto_ablkcipher handle
857 */
Herbert Xu32e3983f2007-03-24 14:35:34 +1100858static inline struct crypto_ablkcipher *crypto_ablkcipher_reqtfm(
859 struct ablkcipher_request *req)
860{
861 return __crypto_ablkcipher_cast(req->base.tfm);
862}
863
Stephan Muellerf13ec332014-11-12 05:28:22 +0100864/**
865 * crypto_ablkcipher_encrypt() - encrypt plaintext
866 * @req: reference to the ablkcipher_request handle that holds all information
867 * needed to perform the cipher operation
868 *
869 * Encrypt plaintext data using the ablkcipher_request handle. That data
870 * structure and how it is filled with data is discussed with the
871 * ablkcipher_request_* functions.
872 *
873 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
874 */
Herbert Xu32e3983f2007-03-24 14:35:34 +1100875static inline int crypto_ablkcipher_encrypt(struct ablkcipher_request *req)
876{
877 struct ablkcipher_tfm *crt =
878 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
879 return crt->encrypt(req);
880}
881
Stephan Muellerf13ec332014-11-12 05:28:22 +0100882/**
883 * crypto_ablkcipher_decrypt() - decrypt ciphertext
884 * @req: reference to the ablkcipher_request handle that holds all information
885 * needed to perform the cipher operation
886 *
887 * Decrypt ciphertext data using the ablkcipher_request handle. That data
888 * structure and how it is filled with data is discussed with the
889 * ablkcipher_request_* functions.
890 *
891 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
892 */
Herbert Xu32e3983f2007-03-24 14:35:34 +1100893static inline int crypto_ablkcipher_decrypt(struct ablkcipher_request *req)
894{
895 struct ablkcipher_tfm *crt =
896 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
897 return crt->decrypt(req);
898}
899
Stephan Muellerf13ec332014-11-12 05:28:22 +0100900/**
901 * DOC: Asynchronous Cipher Request Handle
902 *
903 * The ablkcipher_request data structure contains all pointers to data
904 * required for the asynchronous cipher operation. This includes the cipher
905 * handle (which can be used by multiple ablkcipher_request instances), pointer
906 * to plaintext and ciphertext, asynchronous callback function, etc. It acts
907 * as a handle to the ablkcipher_request_* API calls in a similar way as
908 * ablkcipher handle to the crypto_ablkcipher_* API calls.
909 */
910
911/**
912 * crypto_ablkcipher_reqsize() - obtain size of the request data structure
913 * @tfm: cipher handle
914 *
915 * Return: number of bytes
916 */
Herbert Xub16c3a22007-08-29 19:02:04 +0800917static inline unsigned int crypto_ablkcipher_reqsize(
918 struct crypto_ablkcipher *tfm)
Herbert Xu32e3983f2007-03-24 14:35:34 +1100919{
920 return crypto_ablkcipher_crt(tfm)->reqsize;
921}
922
Stephan Muellerf13ec332014-11-12 05:28:22 +0100923/**
924 * ablkcipher_request_set_tfm() - update cipher handle reference in request
925 * @req: request handle to be modified
926 * @tfm: cipher handle that shall be added to the request handle
927 *
928 * Allow the caller to replace the existing ablkcipher handle in the request
929 * data structure with a different one.
930 */
Herbert Xue196d622007-04-14 16:09:14 +1000931static inline void ablkcipher_request_set_tfm(
932 struct ablkcipher_request *req, struct crypto_ablkcipher *tfm)
933{
Herbert Xuecfc4322007-12-05 21:08:36 +1100934 req->base.tfm = crypto_ablkcipher_tfm(crypto_ablkcipher_crt(tfm)->base);
Herbert Xue196d622007-04-14 16:09:14 +1000935}
936
Herbert Xub5b7f082007-04-16 20:48:54 +1000937static inline struct ablkcipher_request *ablkcipher_request_cast(
938 struct crypto_async_request *req)
939{
940 return container_of(req, struct ablkcipher_request, base);
941}
942
Stephan Muellerf13ec332014-11-12 05:28:22 +0100943/**
944 * ablkcipher_request_alloc() - allocate request data structure
945 * @tfm: cipher handle to be registered with the request
946 * @gfp: memory allocation flag that is handed to kmalloc by the API call.
947 *
948 * Allocate the request data structure that must be used with the ablkcipher
949 * encrypt and decrypt API calls. During the allocation, the provided ablkcipher
950 * handle is registered in the request data structure.
951 *
Eric Biggers6eae29e2016-04-02 10:54:56 -0500952 * Return: allocated request handle in case of success, or NULL if out of memory
Stephan Muellerf13ec332014-11-12 05:28:22 +0100953 */
Herbert Xu32e3983f2007-03-24 14:35:34 +1100954static inline struct ablkcipher_request *ablkcipher_request_alloc(
955 struct crypto_ablkcipher *tfm, gfp_t gfp)
956{
957 struct ablkcipher_request *req;
958
959 req = kmalloc(sizeof(struct ablkcipher_request) +
960 crypto_ablkcipher_reqsize(tfm), gfp);
961
962 if (likely(req))
Herbert Xue196d622007-04-14 16:09:14 +1000963 ablkcipher_request_set_tfm(req, tfm);
Herbert Xu32e3983f2007-03-24 14:35:34 +1100964
965 return req;
966}
967
Stephan Muellerf13ec332014-11-12 05:28:22 +0100968/**
969 * ablkcipher_request_free() - zeroize and free request data structure
970 * @req: request data structure cipher handle to be freed
971 */
Herbert Xu32e3983f2007-03-24 14:35:34 +1100972static inline void ablkcipher_request_free(struct ablkcipher_request *req)
973{
Herbert Xuaef73cf2009-07-11 22:22:14 +0800974 kzfree(req);
Herbert Xu32e3983f2007-03-24 14:35:34 +1100975}
976
Stephan Muellerf13ec332014-11-12 05:28:22 +0100977/**
978 * ablkcipher_request_set_callback() - set asynchronous callback function
979 * @req: request handle
980 * @flags: specify zero or an ORing of the flags
981 * CRYPTO_TFM_REQ_MAY_BACKLOG the request queue may back log and
982 * increase the wait queue beyond the initial maximum size;
983 * CRYPTO_TFM_REQ_MAY_SLEEP the request processing may sleep
984 * @compl: callback function pointer to be registered with the request handle
985 * @data: The data pointer refers to memory that is not used by the kernel
986 * crypto API, but provided to the callback function for it to use. Here,
987 * the caller can provide a reference to memory the callback function can
988 * operate on. As the callback function is invoked asynchronously to the
989 * related functionality, it may need to access data structures of the
990 * related functionality which can be referenced using this pointer. The
991 * callback function can access the memory via the "data" field in the
992 * crypto_async_request data structure provided to the callback function.
993 *
994 * This function allows setting the callback function that is triggered once the
995 * cipher operation completes.
996 *
997 * The callback function is registered with the ablkcipher_request handle and
Stephan Mueller379dcfb2015-01-19 00:13:39 +0100998 * must comply with the following template
Stephan Muellerf13ec332014-11-12 05:28:22 +0100999 *
1000 * void callback_function(struct crypto_async_request *req, int error)
1001 */
Herbert Xu32e3983f2007-03-24 14:35:34 +11001002static inline void ablkcipher_request_set_callback(
1003 struct ablkcipher_request *req,
Mark Rustad3e3dc252014-07-25 02:53:38 -07001004 u32 flags, crypto_completion_t compl, void *data)
Herbert Xu32e3983f2007-03-24 14:35:34 +11001005{
Mark Rustad3e3dc252014-07-25 02:53:38 -07001006 req->base.complete = compl;
Herbert Xu32e3983f2007-03-24 14:35:34 +11001007 req->base.data = data;
1008 req->base.flags = flags;
1009}
1010
Stephan Muellerf13ec332014-11-12 05:28:22 +01001011/**
1012 * ablkcipher_request_set_crypt() - set data buffers
1013 * @req: request handle
1014 * @src: source scatter / gather list
1015 * @dst: destination scatter / gather list
1016 * @nbytes: number of bytes to process from @src
1017 * @iv: IV for the cipher operation which must comply with the IV size defined
1018 * by crypto_ablkcipher_ivsize
1019 *
1020 * This function allows setting of the source data and destination data
1021 * scatter / gather lists.
1022 *
1023 * For encryption, the source is treated as the plaintext and the
1024 * destination is the ciphertext. For a decryption operation, the use is
Stephan Mueller379dcfb2015-01-19 00:13:39 +01001025 * reversed - the source is the ciphertext and the destination is the plaintext.
Stephan Muellerf13ec332014-11-12 05:28:22 +01001026 */
Herbert Xu32e3983f2007-03-24 14:35:34 +11001027static inline void ablkcipher_request_set_crypt(
1028 struct ablkcipher_request *req,
1029 struct scatterlist *src, struct scatterlist *dst,
1030 unsigned int nbytes, void *iv)
1031{
1032 req->src = src;
1033 req->dst = dst;
1034 req->nbytes = nbytes;
1035 req->info = iv;
1036}
1037
Stephan Muellerfced7b02014-11-12 05:29:00 +01001038/**
Stephan Mueller58284f02014-11-12 05:29:36 +01001039 * DOC: Synchronous Block Cipher API
1040 *
1041 * The synchronous block cipher API is used with the ciphers of type
1042 * CRYPTO_ALG_TYPE_BLKCIPHER (listed as type "blkcipher" in /proc/crypto)
1043 *
1044 * Synchronous calls, have a context in the tfm. But since a single tfm can be
1045 * used in multiple calls and in parallel, this info should not be changeable
1046 * (unless a lock is used). This applies, for example, to the symmetric key.
1047 * However, the IV is changeable, so there is an iv field in blkcipher_tfm
1048 * structure for synchronous blkcipher api. So, its the only state info that can
1049 * be kept for synchronous calls without using a big lock across a tfm.
1050 *
1051 * The block cipher API allows the use of a complete cipher, i.e. a cipher
1052 * consisting of a template (a block chaining mode) and a single block cipher
1053 * primitive (e.g. AES).
1054 *
1055 * The plaintext data buffer and the ciphertext data buffer are pointed to
1056 * by using scatter/gather lists. The cipher operation is performed
1057 * on all segments of the provided scatter/gather lists.
1058 *
1059 * The kernel crypto API supports a cipher operation "in-place" which means that
1060 * the caller may provide the same scatter/gather list for the plaintext and
1061 * cipher text. After the completion of the cipher operation, the plaintext
1062 * data is replaced with the ciphertext data in case of an encryption and vice
1063 * versa for a decryption. The caller must ensure that the scatter/gather lists
1064 * for the output data point to sufficiently large buffers, i.e. multiples of
1065 * the block size of the cipher.
1066 */
1067
Herbert Xu5cde0af2006-08-22 00:07:53 +10001068static inline struct crypto_blkcipher *__crypto_blkcipher_cast(
1069 struct crypto_tfm *tfm)
1070{
1071 return (struct crypto_blkcipher *)tfm;
1072}
1073
1074static inline struct crypto_blkcipher *crypto_blkcipher_cast(
1075 struct crypto_tfm *tfm)
1076{
1077 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_BLKCIPHER);
1078 return __crypto_blkcipher_cast(tfm);
1079}
1080
Stephan Mueller58284f02014-11-12 05:29:36 +01001081/**
1082 * crypto_alloc_blkcipher() - allocate synchronous block cipher handle
1083 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1084 * blkcipher cipher
1085 * @type: specifies the type of the cipher
1086 * @mask: specifies the mask for the cipher
1087 *
1088 * Allocate a cipher handle for a block cipher. The returned struct
1089 * crypto_blkcipher is the cipher handle that is required for any subsequent
1090 * API invocation for that block cipher.
1091 *
1092 * Return: allocated cipher handle in case of success; IS_ERR() is true in case
1093 * of an error, PTR_ERR() returns the error code.
1094 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001095static inline struct crypto_blkcipher *crypto_alloc_blkcipher(
1096 const char *alg_name, u32 type, u32 mask)
1097{
Herbert Xu332f88402007-11-15 22:36:07 +08001098 type &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xu5cde0af2006-08-22 00:07:53 +10001099 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
Herbert Xu332f88402007-11-15 22:36:07 +08001100 mask |= CRYPTO_ALG_TYPE_MASK;
Herbert Xu5cde0af2006-08-22 00:07:53 +10001101
1102 return __crypto_blkcipher_cast(crypto_alloc_base(alg_name, type, mask));
1103}
1104
1105static inline struct crypto_tfm *crypto_blkcipher_tfm(
1106 struct crypto_blkcipher *tfm)
1107{
1108 return &tfm->base;
1109}
1110
Stephan Mueller58284f02014-11-12 05:29:36 +01001111/**
1112 * crypto_free_blkcipher() - zeroize and free the block cipher handle
1113 * @tfm: cipher handle to be freed
1114 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001115static inline void crypto_free_blkcipher(struct crypto_blkcipher *tfm)
1116{
1117 crypto_free_tfm(crypto_blkcipher_tfm(tfm));
1118}
1119
Stephan Mueller58284f02014-11-12 05:29:36 +01001120/**
1121 * crypto_has_blkcipher() - Search for the availability of a block cipher
1122 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1123 * block cipher
1124 * @type: specifies the type of the cipher
1125 * @mask: specifies the mask for the cipher
1126 *
1127 * Return: true when the block cipher is known to the kernel crypto API; false
1128 * otherwise
1129 */
Herbert Xufce32d72006-08-26 17:35:45 +10001130static inline int crypto_has_blkcipher(const char *alg_name, u32 type, u32 mask)
1131{
Herbert Xu332f88402007-11-15 22:36:07 +08001132 type &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xufce32d72006-08-26 17:35:45 +10001133 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
Herbert Xu332f88402007-11-15 22:36:07 +08001134 mask |= CRYPTO_ALG_TYPE_MASK;
Herbert Xufce32d72006-08-26 17:35:45 +10001135
1136 return crypto_has_alg(alg_name, type, mask);
1137}
1138
Stephan Mueller58284f02014-11-12 05:29:36 +01001139/**
1140 * crypto_blkcipher_name() - return the name / cra_name from the cipher handle
1141 * @tfm: cipher handle
1142 *
1143 * Return: The character string holding the name of the cipher
1144 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001145static inline const char *crypto_blkcipher_name(struct crypto_blkcipher *tfm)
1146{
1147 return crypto_tfm_alg_name(crypto_blkcipher_tfm(tfm));
1148}
1149
1150static inline struct blkcipher_tfm *crypto_blkcipher_crt(
1151 struct crypto_blkcipher *tfm)
1152{
1153 return &crypto_blkcipher_tfm(tfm)->crt_blkcipher;
1154}
1155
1156static inline struct blkcipher_alg *crypto_blkcipher_alg(
1157 struct crypto_blkcipher *tfm)
1158{
1159 return &crypto_blkcipher_tfm(tfm)->__crt_alg->cra_blkcipher;
1160}
1161
Stephan Mueller58284f02014-11-12 05:29:36 +01001162/**
1163 * crypto_blkcipher_ivsize() - obtain IV size
1164 * @tfm: cipher handle
1165 *
1166 * The size of the IV for the block cipher referenced by the cipher handle is
1167 * returned. This IV size may be zero if the cipher does not need an IV.
1168 *
1169 * Return: IV size in bytes
1170 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001171static inline unsigned int crypto_blkcipher_ivsize(struct crypto_blkcipher *tfm)
1172{
1173 return crypto_blkcipher_alg(tfm)->ivsize;
1174}
1175
Stephan Mueller58284f02014-11-12 05:29:36 +01001176/**
1177 * crypto_blkcipher_blocksize() - obtain block size of cipher
1178 * @tfm: cipher handle
1179 *
1180 * The block size for the block cipher referenced with the cipher handle is
1181 * returned. The caller may use that information to allocate appropriate
1182 * memory for the data returned by the encryption or decryption operation.
1183 *
1184 * Return: block size of cipher
1185 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001186static inline unsigned int crypto_blkcipher_blocksize(
1187 struct crypto_blkcipher *tfm)
1188{
1189 return crypto_tfm_alg_blocksize(crypto_blkcipher_tfm(tfm));
1190}
1191
1192static inline unsigned int crypto_blkcipher_alignmask(
1193 struct crypto_blkcipher *tfm)
1194{
1195 return crypto_tfm_alg_alignmask(crypto_blkcipher_tfm(tfm));
1196}
1197
1198static inline u32 crypto_blkcipher_get_flags(struct crypto_blkcipher *tfm)
1199{
1200 return crypto_tfm_get_flags(crypto_blkcipher_tfm(tfm));
1201}
1202
1203static inline void crypto_blkcipher_set_flags(struct crypto_blkcipher *tfm,
1204 u32 flags)
1205{
1206 crypto_tfm_set_flags(crypto_blkcipher_tfm(tfm), flags);
1207}
1208
1209static inline void crypto_blkcipher_clear_flags(struct crypto_blkcipher *tfm,
1210 u32 flags)
1211{
1212 crypto_tfm_clear_flags(crypto_blkcipher_tfm(tfm), flags);
1213}
1214
Stephan Mueller58284f02014-11-12 05:29:36 +01001215/**
1216 * crypto_blkcipher_setkey() - set key for cipher
1217 * @tfm: cipher handle
1218 * @key: buffer holding the key
1219 * @keylen: length of the key in bytes
1220 *
1221 * The caller provided key is set for the block cipher referenced by the cipher
1222 * handle.
1223 *
1224 * Note, the key length determines the cipher type. Many block ciphers implement
1225 * different cipher modes depending on the key size, such as AES-128 vs AES-192
1226 * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
1227 * is performed.
1228 *
1229 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
1230 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001231static inline int crypto_blkcipher_setkey(struct crypto_blkcipher *tfm,
1232 const u8 *key, unsigned int keylen)
1233{
1234 return crypto_blkcipher_crt(tfm)->setkey(crypto_blkcipher_tfm(tfm),
1235 key, keylen);
1236}
1237
Stephan Mueller58284f02014-11-12 05:29:36 +01001238/**
1239 * crypto_blkcipher_encrypt() - encrypt plaintext
1240 * @desc: reference to the block cipher handle with meta data
1241 * @dst: scatter/gather list that is filled by the cipher operation with the
1242 * ciphertext
1243 * @src: scatter/gather list that holds the plaintext
1244 * @nbytes: number of bytes of the plaintext to encrypt.
1245 *
1246 * Encrypt plaintext data using the IV set by the caller with a preceding
1247 * call of crypto_blkcipher_set_iv.
1248 *
1249 * The blkcipher_desc data structure must be filled by the caller and can
1250 * reside on the stack. The caller must fill desc as follows: desc.tfm is filled
1251 * with the block cipher handle; desc.flags is filled with either
1252 * CRYPTO_TFM_REQ_MAY_SLEEP or 0.
1253 *
1254 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1255 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001256static inline int crypto_blkcipher_encrypt(struct blkcipher_desc *desc,
1257 struct scatterlist *dst,
1258 struct scatterlist *src,
1259 unsigned int nbytes)
1260{
1261 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
1262 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
1263}
1264
Stephan Mueller58284f02014-11-12 05:29:36 +01001265/**
1266 * crypto_blkcipher_encrypt_iv() - encrypt plaintext with dedicated IV
1267 * @desc: reference to the block cipher handle with meta data
1268 * @dst: scatter/gather list that is filled by the cipher operation with the
1269 * ciphertext
1270 * @src: scatter/gather list that holds the plaintext
1271 * @nbytes: number of bytes of the plaintext to encrypt.
1272 *
1273 * Encrypt plaintext data with the use of an IV that is solely used for this
1274 * cipher operation. Any previously set IV is not used.
1275 *
1276 * The blkcipher_desc data structure must be filled by the caller and can
1277 * reside on the stack. The caller must fill desc as follows: desc.tfm is filled
1278 * with the block cipher handle; desc.info is filled with the IV to be used for
1279 * the current operation; desc.flags is filled with either
1280 * CRYPTO_TFM_REQ_MAY_SLEEP or 0.
1281 *
1282 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1283 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001284static inline int crypto_blkcipher_encrypt_iv(struct blkcipher_desc *desc,
1285 struct scatterlist *dst,
1286 struct scatterlist *src,
1287 unsigned int nbytes)
1288{
1289 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
1290}
1291
Stephan Mueller58284f02014-11-12 05:29:36 +01001292/**
1293 * crypto_blkcipher_decrypt() - decrypt ciphertext
1294 * @desc: reference to the block cipher handle with meta data
1295 * @dst: scatter/gather list that is filled by the cipher operation with the
1296 * plaintext
1297 * @src: scatter/gather list that holds the ciphertext
1298 * @nbytes: number of bytes of the ciphertext to decrypt.
1299 *
1300 * Decrypt ciphertext data using the IV set by the caller with a preceding
1301 * call of crypto_blkcipher_set_iv.
1302 *
1303 * The blkcipher_desc data structure must be filled by the caller as documented
1304 * for the crypto_blkcipher_encrypt call above.
1305 *
1306 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1307 *
1308 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001309static inline int crypto_blkcipher_decrypt(struct blkcipher_desc *desc,
1310 struct scatterlist *dst,
1311 struct scatterlist *src,
1312 unsigned int nbytes)
1313{
1314 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
1315 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
1316}
1317
Stephan Mueller58284f02014-11-12 05:29:36 +01001318/**
1319 * crypto_blkcipher_decrypt_iv() - decrypt ciphertext with dedicated IV
1320 * @desc: reference to the block cipher handle with meta data
1321 * @dst: scatter/gather list that is filled by the cipher operation with the
1322 * plaintext
1323 * @src: scatter/gather list that holds the ciphertext
1324 * @nbytes: number of bytes of the ciphertext to decrypt.
1325 *
1326 * Decrypt ciphertext data with the use of an IV that is solely used for this
1327 * cipher operation. Any previously set IV is not used.
1328 *
1329 * The blkcipher_desc data structure must be filled by the caller as documented
1330 * for the crypto_blkcipher_encrypt_iv call above.
1331 *
1332 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1333 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001334static inline int crypto_blkcipher_decrypt_iv(struct blkcipher_desc *desc,
1335 struct scatterlist *dst,
1336 struct scatterlist *src,
1337 unsigned int nbytes)
1338{
1339 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
1340}
1341
Stephan Mueller58284f02014-11-12 05:29:36 +01001342/**
1343 * crypto_blkcipher_set_iv() - set IV for cipher
1344 * @tfm: cipher handle
1345 * @src: buffer holding the IV
1346 * @len: length of the IV in bytes
1347 *
1348 * The caller provided IV is set for the block cipher referenced by the cipher
1349 * handle.
1350 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001351static inline void crypto_blkcipher_set_iv(struct crypto_blkcipher *tfm,
1352 const u8 *src, unsigned int len)
1353{
1354 memcpy(crypto_blkcipher_crt(tfm)->iv, src, len);
1355}
1356
Stephan Mueller58284f02014-11-12 05:29:36 +01001357/**
1358 * crypto_blkcipher_get_iv() - obtain IV from cipher
1359 * @tfm: cipher handle
1360 * @dst: buffer filled with the IV
1361 * @len: length of the buffer dst
1362 *
1363 * The caller can obtain the IV set for the block cipher referenced by the
1364 * cipher handle and store it into the user-provided buffer. If the buffer
1365 * has an insufficient space, the IV is truncated to fit the buffer.
1366 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001367static inline void crypto_blkcipher_get_iv(struct crypto_blkcipher *tfm,
1368 u8 *dst, unsigned int len)
1369{
1370 memcpy(dst, crypto_blkcipher_crt(tfm)->iv, len);
1371}
1372
Stephan Mueller16e61032014-11-12 05:30:06 +01001373/**
1374 * DOC: Single Block Cipher API
1375 *
1376 * The single block cipher API is used with the ciphers of type
1377 * CRYPTO_ALG_TYPE_CIPHER (listed as type "cipher" in /proc/crypto).
1378 *
1379 * Using the single block cipher API calls, operations with the basic cipher
1380 * primitive can be implemented. These cipher primitives exclude any block
1381 * chaining operations including IV handling.
1382 *
1383 * The purpose of this single block cipher API is to support the implementation
1384 * of templates or other concepts that only need to perform the cipher operation
1385 * on one block at a time. Templates invoke the underlying cipher primitive
1386 * block-wise and process either the input or the output data of these cipher
1387 * operations.
1388 */
1389
Herbert Xuf28776a2006-08-13 20:58:18 +10001390static inline struct crypto_cipher *__crypto_cipher_cast(struct crypto_tfm *tfm)
1391{
1392 return (struct crypto_cipher *)tfm;
1393}
1394
1395static inline struct crypto_cipher *crypto_cipher_cast(struct crypto_tfm *tfm)
1396{
1397 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
1398 return __crypto_cipher_cast(tfm);
1399}
1400
Stephan Mueller16e61032014-11-12 05:30:06 +01001401/**
1402 * crypto_alloc_cipher() - allocate single block cipher handle
1403 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1404 * single block cipher
1405 * @type: specifies the type of the cipher
1406 * @mask: specifies the mask for the cipher
1407 *
1408 * Allocate a cipher handle for a single block cipher. The returned struct
1409 * crypto_cipher is the cipher handle that is required for any subsequent API
1410 * invocation for that single block cipher.
1411 *
1412 * Return: allocated cipher handle in case of success; IS_ERR() is true in case
1413 * of an error, PTR_ERR() returns the error code.
1414 */
Herbert Xuf28776a2006-08-13 20:58:18 +10001415static inline struct crypto_cipher *crypto_alloc_cipher(const char *alg_name,
1416 u32 type, u32 mask)
1417{
1418 type &= ~CRYPTO_ALG_TYPE_MASK;
1419 type |= CRYPTO_ALG_TYPE_CIPHER;
1420 mask |= CRYPTO_ALG_TYPE_MASK;
1421
1422 return __crypto_cipher_cast(crypto_alloc_base(alg_name, type, mask));
1423}
1424
1425static inline struct crypto_tfm *crypto_cipher_tfm(struct crypto_cipher *tfm)
1426{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001427 return &tfm->base;
Herbert Xuf28776a2006-08-13 20:58:18 +10001428}
1429
Stephan Mueller16e61032014-11-12 05:30:06 +01001430/**
1431 * crypto_free_cipher() - zeroize and free the single block cipher handle
1432 * @tfm: cipher handle to be freed
1433 */
Herbert Xuf28776a2006-08-13 20:58:18 +10001434static inline void crypto_free_cipher(struct crypto_cipher *tfm)
1435{
1436 crypto_free_tfm(crypto_cipher_tfm(tfm));
1437}
1438
Stephan Mueller16e61032014-11-12 05:30:06 +01001439/**
1440 * crypto_has_cipher() - Search for the availability of a single block cipher
1441 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1442 * single block cipher
1443 * @type: specifies the type of the cipher
1444 * @mask: specifies the mask for the cipher
1445 *
1446 * Return: true when the single block cipher is known to the kernel crypto API;
1447 * false otherwise
1448 */
Herbert Xufce32d72006-08-26 17:35:45 +10001449static inline int crypto_has_cipher(const char *alg_name, u32 type, u32 mask)
1450{
1451 type &= ~CRYPTO_ALG_TYPE_MASK;
1452 type |= CRYPTO_ALG_TYPE_CIPHER;
1453 mask |= CRYPTO_ALG_TYPE_MASK;
1454
1455 return crypto_has_alg(alg_name, type, mask);
1456}
1457
Herbert Xuf28776a2006-08-13 20:58:18 +10001458static inline struct cipher_tfm *crypto_cipher_crt(struct crypto_cipher *tfm)
1459{
1460 return &crypto_cipher_tfm(tfm)->crt_cipher;
1461}
1462
Stephan Mueller16e61032014-11-12 05:30:06 +01001463/**
1464 * crypto_cipher_blocksize() - obtain block size for cipher
1465 * @tfm: cipher handle
1466 *
1467 * The block size for the single block cipher referenced with the cipher handle
1468 * tfm is returned. The caller may use that information to allocate appropriate
1469 * memory for the data returned by the encryption or decryption operation
1470 *
1471 * Return: block size of cipher
1472 */
Herbert Xuf28776a2006-08-13 20:58:18 +10001473static inline unsigned int crypto_cipher_blocksize(struct crypto_cipher *tfm)
1474{
1475 return crypto_tfm_alg_blocksize(crypto_cipher_tfm(tfm));
1476}
1477
1478static inline unsigned int crypto_cipher_alignmask(struct crypto_cipher *tfm)
1479{
1480 return crypto_tfm_alg_alignmask(crypto_cipher_tfm(tfm));
1481}
1482
1483static inline u32 crypto_cipher_get_flags(struct crypto_cipher *tfm)
1484{
1485 return crypto_tfm_get_flags(crypto_cipher_tfm(tfm));
1486}
1487
1488static inline void crypto_cipher_set_flags(struct crypto_cipher *tfm,
1489 u32 flags)
1490{
1491 crypto_tfm_set_flags(crypto_cipher_tfm(tfm), flags);
1492}
1493
1494static inline void crypto_cipher_clear_flags(struct crypto_cipher *tfm,
1495 u32 flags)
1496{
1497 crypto_tfm_clear_flags(crypto_cipher_tfm(tfm), flags);
1498}
1499
Stephan Mueller16e61032014-11-12 05:30:06 +01001500/**
1501 * crypto_cipher_setkey() - set key for cipher
1502 * @tfm: cipher handle
1503 * @key: buffer holding the key
1504 * @keylen: length of the key in bytes
1505 *
1506 * The caller provided key is set for the single block cipher referenced by the
1507 * cipher handle.
1508 *
1509 * Note, the key length determines the cipher type. Many block ciphers implement
1510 * different cipher modes depending on the key size, such as AES-128 vs AES-192
1511 * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
1512 * is performed.
1513 *
1514 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
1515 */
Herbert Xu7226bc872006-08-21 21:40:49 +10001516static inline int crypto_cipher_setkey(struct crypto_cipher *tfm,
1517 const u8 *key, unsigned int keylen)
1518{
1519 return crypto_cipher_crt(tfm)->cit_setkey(crypto_cipher_tfm(tfm),
1520 key, keylen);
1521}
1522
Stephan Mueller16e61032014-11-12 05:30:06 +01001523/**
1524 * crypto_cipher_encrypt_one() - encrypt one block of plaintext
1525 * @tfm: cipher handle
1526 * @dst: points to the buffer that will be filled with the ciphertext
1527 * @src: buffer holding the plaintext to be encrypted
1528 *
1529 * Invoke the encryption operation of one block. The caller must ensure that
1530 * the plaintext and ciphertext buffers are at least one block in size.
1531 */
Herbert Xuf28776a2006-08-13 20:58:18 +10001532static inline void crypto_cipher_encrypt_one(struct crypto_cipher *tfm,
1533 u8 *dst, const u8 *src)
1534{
1535 crypto_cipher_crt(tfm)->cit_encrypt_one(crypto_cipher_tfm(tfm),
1536 dst, src);
1537}
1538
Stephan Mueller16e61032014-11-12 05:30:06 +01001539/**
1540 * crypto_cipher_decrypt_one() - decrypt one block of ciphertext
1541 * @tfm: cipher handle
1542 * @dst: points to the buffer that will be filled with the plaintext
1543 * @src: buffer holding the ciphertext to be decrypted
1544 *
1545 * Invoke the decryption operation of one block. The caller must ensure that
1546 * the plaintext and ciphertext buffers are at least one block in size.
1547 */
Herbert Xuf28776a2006-08-13 20:58:18 +10001548static inline void crypto_cipher_decrypt_one(struct crypto_cipher *tfm,
1549 u8 *dst, const u8 *src)
1550{
1551 crypto_cipher_crt(tfm)->cit_decrypt_one(crypto_cipher_tfm(tfm),
1552 dst, src);
1553}
1554
Herbert Xufce32d72006-08-26 17:35:45 +10001555static inline struct crypto_comp *__crypto_comp_cast(struct crypto_tfm *tfm)
1556{
1557 return (struct crypto_comp *)tfm;
1558}
1559
1560static inline struct crypto_comp *crypto_comp_cast(struct crypto_tfm *tfm)
1561{
1562 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_COMPRESS) &
1563 CRYPTO_ALG_TYPE_MASK);
1564 return __crypto_comp_cast(tfm);
1565}
1566
1567static inline struct crypto_comp *crypto_alloc_comp(const char *alg_name,
1568 u32 type, u32 mask)
1569{
1570 type &= ~CRYPTO_ALG_TYPE_MASK;
1571 type |= CRYPTO_ALG_TYPE_COMPRESS;
1572 mask |= CRYPTO_ALG_TYPE_MASK;
1573
1574 return __crypto_comp_cast(crypto_alloc_base(alg_name, type, mask));
1575}
1576
1577static inline struct crypto_tfm *crypto_comp_tfm(struct crypto_comp *tfm)
1578{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001579 return &tfm->base;
Herbert Xufce32d72006-08-26 17:35:45 +10001580}
1581
1582static inline void crypto_free_comp(struct crypto_comp *tfm)
1583{
1584 crypto_free_tfm(crypto_comp_tfm(tfm));
1585}
1586
1587static inline int crypto_has_comp(const char *alg_name, u32 type, u32 mask)
1588{
1589 type &= ~CRYPTO_ALG_TYPE_MASK;
1590 type |= CRYPTO_ALG_TYPE_COMPRESS;
1591 mask |= CRYPTO_ALG_TYPE_MASK;
1592
1593 return crypto_has_alg(alg_name, type, mask);
1594}
1595
Herbert Xue4d5b792006-08-26 18:12:40 +10001596static inline const char *crypto_comp_name(struct crypto_comp *tfm)
1597{
1598 return crypto_tfm_alg_name(crypto_comp_tfm(tfm));
1599}
1600
Herbert Xufce32d72006-08-26 17:35:45 +10001601static inline struct compress_tfm *crypto_comp_crt(struct crypto_comp *tfm)
1602{
1603 return &crypto_comp_tfm(tfm)->crt_compress;
1604}
1605
1606static inline int crypto_comp_compress(struct crypto_comp *tfm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 const u8 *src, unsigned int slen,
1608 u8 *dst, unsigned int *dlen)
1609{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001610 return crypto_comp_crt(tfm)->cot_compress(crypto_comp_tfm(tfm),
1611 src, slen, dst, dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612}
1613
Herbert Xufce32d72006-08-26 17:35:45 +10001614static inline int crypto_comp_decompress(struct crypto_comp *tfm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 const u8 *src, unsigned int slen,
1616 u8 *dst, unsigned int *dlen)
1617{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001618 return crypto_comp_crt(tfm)->cot_decompress(crypto_comp_tfm(tfm),
1619 src, slen, dst, dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620}
1621
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622#endif /* _LINUX_CRYPTO_H */
1623