blob: 37a652d1639d3c001b617cea670d939378e190dc [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 Xu4e6c3df2016-07-12 13:17:31 +080050#define CRYPTO_ALG_TYPE_SKCIPHER 0x00000005
Herbert Xu61da88e2007-12-17 21:51:27 +080051#define CRYPTO_ALG_TYPE_GIVCIPHER 0x00000006
Salvatore Benedetto4e5f2c42016-06-22 17:49:13 +010052#define CRYPTO_ALG_TYPE_KPP 0x00000008
Neil Horman17f0f4a2008-08-14 22:15:52 +100053#define CRYPTO_ALG_TYPE_RNG 0x0000000c
Tadeusz Struk3c339ab2015-06-16 10:30:55 -070054#define CRYPTO_ALG_TYPE_AKCIPHER 0x0000000d
Giovanni Cabiddu63044c42016-06-02 13:28:55 +010055#define CRYPTO_ALG_TYPE_DIGEST 0x0000000e
56#define CRYPTO_ALG_TYPE_HASH 0x0000000e
57#define CRYPTO_ALG_TYPE_SHASH 0x0000000e
58#define CRYPTO_ALG_TYPE_AHASH 0x0000000f
Herbert Xu055bcee2006-08-19 22:24:23 +100059
60#define CRYPTO_ALG_TYPE_HASH_MASK 0x0000000e
Giovanni Cabiddu63044c42016-06-02 13:28:55 +010061#define CRYPTO_ALG_TYPE_AHASH_MASK 0x0000000e
Herbert Xu332f88402007-11-15 22:36:07 +080062#define CRYPTO_ALG_TYPE_BLKCIPHER_MASK 0x0000000c
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Herbert Xu28259822006-08-06 21:23:26 +100064#define CRYPTO_ALG_LARVAL 0x00000010
Herbert Xu6bfd4802006-09-21 11:39:29 +100065#define CRYPTO_ALG_DEAD 0x00000020
66#define CRYPTO_ALG_DYING 0x00000040
Herbert Xuf3f632d2006-08-06 23:12:59 +100067#define CRYPTO_ALG_ASYNC 0x00000080
Herbert Xu28259822006-08-06 21:23:26 +100068
Linus Torvalds1da177e2005-04-16 15:20:36 -070069/*
Herbert Xu60104392006-08-26 18:34:10 +100070 * Set this bit if and only if the algorithm requires another algorithm of
71 * the same type to handle corner cases.
72 */
73#define CRYPTO_ALG_NEED_FALLBACK 0x00000100
74
75/*
Herbert Xuecfc4322007-12-05 21:08:36 +110076 * This bit is set for symmetric key ciphers that have already been wrapped
77 * with a generic IV generator to prevent them from being wrapped again.
78 */
79#define CRYPTO_ALG_GENIV 0x00000200
80
81/*
Herbert Xu73d38642008-08-03 21:15:23 +080082 * Set if the algorithm has passed automated run-time testing. Note that
83 * if there is no run-time testing for a given algorithm it is considered
84 * to have passed.
85 */
86
87#define CRYPTO_ALG_TESTED 0x00000400
88
89/*
Steffen Klassert64a947b2011-09-27 07:21:26 +020090 * Set if the algorithm is an instance that is build from templates.
91 */
92#define CRYPTO_ALG_INSTANCE 0x00000800
93
Nikos Mavrogiannopoulosd912bb72011-11-01 13:39:56 +010094/* Set this bit if the algorithm provided is hardware accelerated but
95 * not available to userspace via instruction set or so.
96 */
97#define CRYPTO_ALG_KERN_DRIVER_ONLY 0x00001000
98
Steffen Klassert64a947b2011-09-27 07:21:26 +020099/*
Stephan Mueller06ca7f62015-03-30 21:55:52 +0200100 * Mark a cipher as a service implementation only usable by another
101 * cipher and never by a normal user of the kernel crypto API
102 */
103#define CRYPTO_ALG_INTERNAL 0x00002000
104
105/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 * Transform masks and values (for crt_flags).
107 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108#define CRYPTO_TFM_REQ_MASK 0x000fff00
109#define CRYPTO_TFM_RES_MASK 0xfff00000
110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111#define CRYPTO_TFM_REQ_WEAK_KEY 0x00000100
Herbert Xu64baf3c2005-09-01 17:43:05 -0700112#define CRYPTO_TFM_REQ_MAY_SLEEP 0x00000200
Herbert Xu32e3983f2007-03-24 14:35:34 +1100113#define CRYPTO_TFM_REQ_MAY_BACKLOG 0x00000400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114#define CRYPTO_TFM_RES_WEAK_KEY 0x00100000
115#define CRYPTO_TFM_RES_BAD_KEY_LEN 0x00200000
116#define CRYPTO_TFM_RES_BAD_KEY_SCHED 0x00400000
117#define CRYPTO_TFM_RES_BAD_BLOCK_LEN 0x00800000
118#define CRYPTO_TFM_RES_BAD_FLAGS 0x01000000
119
120/*
121 * Miscellaneous stuff.
122 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123#define CRYPTO_MAX_ALG_NAME 64
124
Herbert Xu79911102006-08-21 21:03:52 +1000125/*
126 * The macro CRYPTO_MINALIGN_ATTR (along with the void * type in the actual
127 * declaration) is used to ensure that the crypto_tfm context structure is
128 * aligned correctly for the given architecture so that there are no alignment
129 * faults for C data types. In particular, this is required on platforms such
130 * as arm where pointers are 32-bit aligned but there are data types such as
131 * u64 which require 64-bit alignment.
132 */
Herbert Xu79911102006-08-21 21:03:52 +1000133#define CRYPTO_MINALIGN ARCH_KMALLOC_MINALIGN
Herbert Xu79911102006-08-21 21:03:52 +1000134
Herbert Xu79911102006-08-21 21:03:52 +1000135#define CRYPTO_MINALIGN_ATTR __attribute__ ((__aligned__(CRYPTO_MINALIGN)))
Herbert Xu79911102006-08-21 21:03:52 +1000136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137struct scatterlist;
Herbert Xu32e3983f2007-03-24 14:35:34 +1100138struct crypto_ablkcipher;
139struct crypto_async_request;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000140struct crypto_blkcipher;
Herbert Xu40725182005-07-06 13:51:52 -0700141struct crypto_tfm;
Herbert Xue853c3c2006-08-22 00:06:54 +1000142struct crypto_type;
Herbert Xu61da88e2007-12-17 21:51:27 +0800143struct skcipher_givcrypt_request;
Herbert Xu40725182005-07-06 13:51:52 -0700144
Herbert Xu32e3983f2007-03-24 14:35:34 +1100145typedef void (*crypto_completion_t)(struct crypto_async_request *req, int err);
146
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100147/**
148 * DOC: Block Cipher Context Data Structures
149 *
150 * These data structures define the operating context for each block cipher
151 * type.
152 */
153
Herbert Xu32e3983f2007-03-24 14:35:34 +1100154struct crypto_async_request {
155 struct list_head list;
156 crypto_completion_t complete;
157 void *data;
158 struct crypto_tfm *tfm;
159
160 u32 flags;
161};
162
163struct ablkcipher_request {
164 struct crypto_async_request base;
165
166 unsigned int nbytes;
167
168 void *info;
169
170 struct scatterlist *src;
171 struct scatterlist *dst;
172
173 void *__ctx[] CRYPTO_MINALIGN_ATTR;
174};
175
Herbert Xu5cde0af2006-08-22 00:07:53 +1000176struct blkcipher_desc {
177 struct crypto_blkcipher *tfm;
178 void *info;
179 u32 flags;
180};
181
Herbert Xu40725182005-07-06 13:51:52 -0700182struct cipher_desc {
183 struct crypto_tfm *tfm;
Herbert Xu6c2bb982006-05-16 22:09:29 +1000184 void (*crfn)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Herbert Xu40725182005-07-06 13:51:52 -0700185 unsigned int (*prfn)(const struct cipher_desc *desc, u8 *dst,
186 const u8 *src, unsigned int nbytes);
187 void *info;
188};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100190/**
191 * DOC: Block Cipher Algorithm Definitions
192 *
193 * These data structures define modular crypto algorithm implementations,
194 * managed via crypto_register_alg() and crypto_unregister_alg().
195 */
196
197/**
198 * struct ablkcipher_alg - asynchronous block cipher definition
199 * @min_keysize: Minimum key size supported by the transformation. This is the
200 * smallest key length supported by this transformation algorithm.
201 * This must be set to one of the pre-defined values as this is
202 * not hardware specific. Possible values for this field can be
203 * found via git grep "_MIN_KEY_SIZE" include/crypto/
204 * @max_keysize: Maximum key size supported by the transformation. This is the
205 * largest key length supported by this transformation algorithm.
206 * This must be set to one of the pre-defined values as this is
207 * not hardware specific. Possible values for this field can be
208 * found via git grep "_MAX_KEY_SIZE" include/crypto/
209 * @setkey: Set key for the transformation. This function is used to either
210 * program a supplied key into the hardware or store the key in the
211 * transformation context for programming it later. Note that this
212 * function does modify the transformation context. This function can
213 * be called multiple times during the existence of the transformation
214 * object, so one must make sure the key is properly reprogrammed into
215 * the hardware. This function is also responsible for checking the key
216 * length for validity. In case a software fallback was put in place in
217 * the @cra_init call, this function might need to use the fallback if
218 * the algorithm doesn't support all of the key sizes.
219 * @encrypt: Encrypt a scatterlist of blocks. This function is used to encrypt
220 * the supplied scatterlist containing the blocks of data. The crypto
221 * API consumer is responsible for aligning the entries of the
222 * scatterlist properly and making sure the chunks are correctly
223 * sized. In case a software fallback was put in place in the
224 * @cra_init call, this function might need to use the fallback if
225 * the algorithm doesn't support all of the key sizes. In case the
226 * key was stored in transformation context, the key might need to be
227 * re-programmed into the hardware in this function. This function
228 * shall not modify the transformation context, as this function may
229 * be called in parallel with the same transformation object.
230 * @decrypt: Decrypt a single block. This is a reverse counterpart to @encrypt
231 * and the conditions are exactly the same.
232 * @givencrypt: Update the IV for encryption. With this function, a cipher
233 * implementation may provide the function on how to update the IV
234 * for encryption.
235 * @givdecrypt: Update the IV for decryption. This is the reverse of
236 * @givencrypt .
237 * @geniv: The transformation implementation may use an "IV generator" provided
238 * by the kernel crypto API. Several use cases have a predefined
239 * approach how IVs are to be updated. For such use cases, the kernel
240 * crypto API provides ready-to-use implementations that can be
241 * referenced with this variable.
242 * @ivsize: IV size applicable for transformation. The consumer must provide an
243 * IV of exactly that size to perform the encrypt or decrypt operation.
244 *
245 * All fields except @givencrypt , @givdecrypt , @geniv and @ivsize are
246 * mandatory and must be filled.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 */
Herbert Xub5b7f082007-04-16 20:48:54 +1000248struct ablkcipher_alg {
249 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
250 unsigned int keylen);
251 int (*encrypt)(struct ablkcipher_request *req);
252 int (*decrypt)(struct ablkcipher_request *req);
Herbert Xu61da88e2007-12-17 21:51:27 +0800253 int (*givencrypt)(struct skcipher_givcrypt_request *req);
254 int (*givdecrypt)(struct skcipher_givcrypt_request *req);
Herbert Xub5b7f082007-04-16 20:48:54 +1000255
Herbert Xu23508e12007-11-27 21:33:24 +0800256 const char *geniv;
257
Herbert Xub5b7f082007-04-16 20:48:54 +1000258 unsigned int min_keysize;
259 unsigned int max_keysize;
260 unsigned int ivsize;
261};
262
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100263/**
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100264 * struct blkcipher_alg - synchronous block cipher definition
265 * @min_keysize: see struct ablkcipher_alg
266 * @max_keysize: see struct ablkcipher_alg
267 * @setkey: see struct ablkcipher_alg
268 * @encrypt: see struct ablkcipher_alg
269 * @decrypt: see struct ablkcipher_alg
270 * @geniv: see struct ablkcipher_alg
271 * @ivsize: see struct ablkcipher_alg
272 *
273 * All fields except @geniv and @ivsize are mandatory and must be filled.
274 */
Herbert Xu5cde0af2006-08-22 00:07:53 +1000275struct blkcipher_alg {
276 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
277 unsigned int keylen);
278 int (*encrypt)(struct blkcipher_desc *desc,
279 struct scatterlist *dst, struct scatterlist *src,
280 unsigned int nbytes);
281 int (*decrypt)(struct blkcipher_desc *desc,
282 struct scatterlist *dst, struct scatterlist *src,
283 unsigned int nbytes);
284
Herbert Xu23508e12007-11-27 21:33:24 +0800285 const char *geniv;
286
Herbert Xu5cde0af2006-08-22 00:07:53 +1000287 unsigned int min_keysize;
288 unsigned int max_keysize;
289 unsigned int ivsize;
290};
291
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100292/**
293 * struct cipher_alg - single-block symmetric ciphers definition
294 * @cia_min_keysize: Minimum key size supported by the transformation. This is
295 * the smallest key length supported by this transformation
296 * algorithm. This must be set to one of the pre-defined
297 * values as this is not hardware specific. Possible values
298 * for this field can be found via git grep "_MIN_KEY_SIZE"
299 * include/crypto/
300 * @cia_max_keysize: Maximum key size supported by the transformation. This is
301 * the largest key length supported by this transformation
302 * algorithm. This must be set to one of the pre-defined values
303 * as this is not hardware specific. Possible values for this
304 * field can be found via git grep "_MAX_KEY_SIZE"
305 * include/crypto/
306 * @cia_setkey: Set key for the transformation. This function is used to either
307 * program a supplied key into the hardware or store the key in the
308 * transformation context for programming it later. Note that this
309 * function does modify the transformation context. This function
310 * can be called multiple times during the existence of the
311 * transformation object, so one must make sure the key is properly
312 * reprogrammed into the hardware. This function is also
313 * responsible for checking the key length for validity.
314 * @cia_encrypt: Encrypt a single block. This function is used to encrypt a
315 * single block of data, which must be @cra_blocksize big. This
316 * always operates on a full @cra_blocksize and it is not possible
317 * to encrypt a block of smaller size. The supplied buffers must
318 * therefore also be at least of @cra_blocksize size. Both the
319 * input and output buffers are always aligned to @cra_alignmask.
320 * In case either of the input or output buffer supplied by user
321 * of the crypto API is not aligned to @cra_alignmask, the crypto
322 * API will re-align the buffers. The re-alignment means that a
323 * new buffer will be allocated, the data will be copied into the
324 * new buffer, then the processing will happen on the new buffer,
325 * then the data will be copied back into the original buffer and
326 * finally the new buffer will be freed. In case a software
327 * fallback was put in place in the @cra_init call, this function
328 * might need to use the fallback if the algorithm doesn't support
329 * all of the key sizes. In case the key was stored in
330 * transformation context, the key might need to be re-programmed
331 * into the hardware in this function. This function shall not
332 * modify the transformation context, as this function may be
333 * called in parallel with the same transformation object.
334 * @cia_decrypt: Decrypt a single block. This is a reverse counterpart to
335 * @cia_encrypt, and the conditions are exactly the same.
336 *
337 * All fields are mandatory and must be filled.
338 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339struct cipher_alg {
340 unsigned int cia_min_keysize;
341 unsigned int cia_max_keysize;
Herbert Xu6c2bb982006-05-16 22:09:29 +1000342 int (*cia_setkey)(struct crypto_tfm *tfm, const u8 *key,
Herbert Xu560c06a2006-08-13 14:16:39 +1000343 unsigned int keylen);
Herbert Xu6c2bb982006-05-16 22:09:29 +1000344 void (*cia_encrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
345 void (*cia_decrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346};
347
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348struct compress_alg {
Herbert Xu6c2bb982006-05-16 22:09:29 +1000349 int (*coa_compress)(struct crypto_tfm *tfm, const u8 *src,
350 unsigned int slen, u8 *dst, unsigned int *dlen);
351 int (*coa_decompress)(struct crypto_tfm *tfm, const u8 *src,
352 unsigned int slen, u8 *dst, unsigned int *dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353};
354
Neil Horman17f0f4a2008-08-14 22:15:52 +1000355
Herbert Xub5b7f082007-04-16 20:48:54 +1000356#define cra_ablkcipher cra_u.ablkcipher
Herbert Xu5cde0af2006-08-22 00:07:53 +1000357#define cra_blkcipher cra_u.blkcipher
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358#define cra_cipher cra_u.cipher
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359#define cra_compress cra_u.compress
360
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100361/**
362 * struct crypto_alg - definition of a cryptograpic cipher algorithm
363 * @cra_flags: Flags describing this transformation. See include/linux/crypto.h
364 * CRYPTO_ALG_* flags for the flags which go in here. Those are
365 * used for fine-tuning the description of the transformation
366 * algorithm.
367 * @cra_blocksize: Minimum block size of this transformation. The size in bytes
368 * of the smallest possible unit which can be transformed with
369 * this algorithm. The users must respect this value.
370 * In case of HASH transformation, it is possible for a smaller
371 * block than @cra_blocksize to be passed to the crypto API for
372 * transformation, in case of any other transformation type, an
373 * error will be returned upon any attempt to transform smaller
374 * than @cra_blocksize chunks.
375 * @cra_ctxsize: Size of the operational context of the transformation. This
376 * value informs the kernel crypto API about the memory size
377 * needed to be allocated for the transformation context.
378 * @cra_alignmask: Alignment mask for the input and output data buffer. The data
379 * buffer containing the input data for the algorithm must be
380 * aligned to this alignment mask. The data buffer for the
381 * output data must be aligned to this alignment mask. Note that
382 * the Crypto API will do the re-alignment in software, but
383 * only under special conditions and there is a performance hit.
384 * The re-alignment happens at these occasions for different
385 * @cra_u types: cipher -- For both input data and output data
386 * buffer; ahash -- For output hash destination buf; shash --
387 * For output hash destination buf.
388 * This is needed on hardware which is flawed by design and
389 * cannot pick data from arbitrary addresses.
390 * @cra_priority: Priority of this transformation implementation. In case
391 * multiple transformations with same @cra_name are available to
392 * the Crypto API, the kernel will use the one with highest
393 * @cra_priority.
394 * @cra_name: Generic name (usable by multiple implementations) of the
395 * transformation algorithm. This is the name of the transformation
396 * itself. This field is used by the kernel when looking up the
397 * providers of particular transformation.
398 * @cra_driver_name: Unique name of the transformation provider. This is the
399 * name of the provider of the transformation. This can be any
400 * arbitrary value, but in the usual case, this contains the
401 * name of the chip or provider and the name of the
402 * transformation algorithm.
403 * @cra_type: Type of the cryptographic transformation. This is a pointer to
404 * struct crypto_type, which implements callbacks common for all
Masanari Iida12f7c142015-06-04 00:01:21 +0900405 * transformation types. There are multiple options:
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100406 * &crypto_blkcipher_type, &crypto_ablkcipher_type,
Herbert Xub0d955b2015-08-14 15:30:41 +0800407 * &crypto_ahash_type, &crypto_rng_type.
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100408 * This field might be empty. In that case, there are no common
409 * callbacks. This is the case for: cipher, compress, shash.
410 * @cra_u: Callbacks implementing the transformation. This is a union of
411 * multiple structures. Depending on the type of transformation selected
412 * by @cra_type and @cra_flags above, the associated structure must be
413 * filled with callbacks. This field might be empty. This is the case
414 * for ahash, shash.
415 * @cra_init: Initialize the cryptographic transformation object. This function
416 * is used to initialize the cryptographic transformation object.
417 * This function is called only once at the instantiation time, right
418 * after the transformation context was allocated. In case the
419 * cryptographic hardware has some special requirements which need to
420 * be handled by software, this function shall check for the precise
421 * requirement of the transformation and put any software fallbacks
422 * in place.
423 * @cra_exit: Deinitialize the cryptographic transformation object. This is a
424 * counterpart to @cra_init, used to remove various changes set in
425 * @cra_init.
426 * @cra_module: Owner of this transformation implementation. Set to THIS_MODULE
427 * @cra_list: internally used
428 * @cra_users: internally used
429 * @cra_refcnt: internally used
430 * @cra_destroy: internally used
431 *
432 * The struct crypto_alg describes a generic Crypto API algorithm and is common
433 * for all of the transformations. Any variable not documented here shall not
434 * be used by a cipher implementation as it is internal to the Crypto API.
435 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436struct crypto_alg {
437 struct list_head cra_list;
Herbert Xu6bfd4802006-09-21 11:39:29 +1000438 struct list_head cra_users;
439
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 u32 cra_flags;
441 unsigned int cra_blocksize;
442 unsigned int cra_ctxsize;
Herbert Xu95477372005-07-06 13:52:09 -0700443 unsigned int cra_alignmask;
Herbert Xu5cb1454b2005-11-05 16:58:14 +1100444
445 int cra_priority;
Herbert Xu6521f302006-08-06 20:28:44 +1000446 atomic_t cra_refcnt;
Herbert Xu5cb1454b2005-11-05 16:58:14 +1100447
Herbert Xud913ea02006-05-21 08:45:26 +1000448 char cra_name[CRYPTO_MAX_ALG_NAME];
449 char cra_driver_name[CRYPTO_MAX_ALG_NAME];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
Herbert Xue853c3c2006-08-22 00:06:54 +1000451 const struct crypto_type *cra_type;
452
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 union {
Herbert Xub5b7f082007-04-16 20:48:54 +1000454 struct ablkcipher_alg ablkcipher;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000455 struct blkcipher_alg blkcipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 struct cipher_alg cipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 struct compress_alg compress;
458 } cra_u;
Herbert Xuc7fc0592006-05-24 13:02:26 +1000459
460 int (*cra_init)(struct crypto_tfm *tfm);
461 void (*cra_exit)(struct crypto_tfm *tfm);
Herbert Xu6521f302006-08-06 20:28:44 +1000462 void (*cra_destroy)(struct crypto_alg *alg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
464 struct module *cra_module;
Herbert Xuedf18b92015-06-18 14:00:48 +0800465} CRYPTO_MINALIGN_ATTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
467/*
468 * Algorithm registration interface.
469 */
470int crypto_register_alg(struct crypto_alg *alg);
471int crypto_unregister_alg(struct crypto_alg *alg);
Mark Brown4b004342012-01-17 23:34:26 +0000472int crypto_register_algs(struct crypto_alg *algs, int count);
473int crypto_unregister_algs(struct crypto_alg *algs, int count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
475/*
476 * Algorithm query interface.
477 */
Herbert Xufce32d72006-08-26 17:35:45 +1000478int crypto_has_alg(const char *name, u32 type, u32 mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
480/*
481 * Transforms: user-instantiated objects which encapsulate algorithms
Herbert Xu6d7d684d2006-07-30 11:53:01 +1000482 * and core processing logic. Managed via crypto_alloc_*() and
483 * crypto_free_*(), as well as the various helpers below.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
Herbert Xu32e3983f2007-03-24 14:35:34 +1100486struct ablkcipher_tfm {
487 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
488 unsigned int keylen);
489 int (*encrypt)(struct ablkcipher_request *req);
490 int (*decrypt)(struct ablkcipher_request *req);
Herbert Xu61da88e2007-12-17 21:51:27 +0800491 int (*givencrypt)(struct skcipher_givcrypt_request *req);
492 int (*givdecrypt)(struct skcipher_givcrypt_request *req);
493
Herbert Xuecfc4322007-12-05 21:08:36 +1100494 struct crypto_ablkcipher *base;
495
Herbert Xu32e3983f2007-03-24 14:35:34 +1100496 unsigned int ivsize;
497 unsigned int reqsize;
498};
499
Herbert Xu5cde0af2006-08-22 00:07:53 +1000500struct blkcipher_tfm {
501 void *iv;
502 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
503 unsigned int keylen);
504 int (*encrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
505 struct scatterlist *src, unsigned int nbytes);
506 int (*decrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
507 struct scatterlist *src, unsigned int nbytes);
508};
509
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510struct cipher_tfm {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 int (*cit_setkey)(struct crypto_tfm *tfm,
512 const u8 *key, unsigned int keylen);
Herbert Xuf28776a2006-08-13 20:58:18 +1000513 void (*cit_encrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
514 void (*cit_decrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515};
516
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517struct compress_tfm {
518 int (*cot_compress)(struct crypto_tfm *tfm,
519 const u8 *src, unsigned int slen,
520 u8 *dst, unsigned int *dlen);
521 int (*cot_decompress)(struct crypto_tfm *tfm,
522 const u8 *src, unsigned int slen,
523 u8 *dst, unsigned int *dlen);
524};
525
Herbert Xu32e3983f2007-03-24 14:35:34 +1100526#define crt_ablkcipher crt_u.ablkcipher
Herbert Xu5cde0af2006-08-22 00:07:53 +1000527#define crt_blkcipher crt_u.blkcipher
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528#define crt_cipher crt_u.cipher
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529#define crt_compress crt_u.compress
530
531struct crypto_tfm {
532
533 u32 crt_flags;
534
535 union {
Herbert Xu32e3983f2007-03-24 14:35:34 +1100536 struct ablkcipher_tfm ablkcipher;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000537 struct blkcipher_tfm blkcipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 struct cipher_tfm cipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 struct compress_tfm compress;
540 } crt_u;
Herbert Xu4a779482008-09-13 18:19:03 -0700541
542 void (*exit)(struct crypto_tfm *tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
544 struct crypto_alg *__crt_alg;
Herbert Xuf10b7892006-01-25 22:34:01 +1100545
Herbert Xu79911102006-08-21 21:03:52 +1000546 void *__crt_ctx[] CRYPTO_MINALIGN_ATTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547};
548
Herbert Xu32e3983f2007-03-24 14:35:34 +1100549struct crypto_ablkcipher {
550 struct crypto_tfm base;
551};
552
Herbert Xu5cde0af2006-08-22 00:07:53 +1000553struct crypto_blkcipher {
554 struct crypto_tfm base;
555};
556
Herbert Xu78a1fe42006-12-24 10:02:00 +1100557struct crypto_cipher {
558 struct crypto_tfm base;
559};
560
561struct crypto_comp {
562 struct crypto_tfm base;
563};
564
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000565enum {
566 CRYPTOA_UNSPEC,
567 CRYPTOA_ALG,
Herbert Xuebc610e2007-01-01 18:37:02 +1100568 CRYPTOA_TYPE,
Herbert Xu39e1ee012007-08-29 19:27:26 +0800569 CRYPTOA_U32,
Herbert Xuebc610e2007-01-01 18:37:02 +1100570 __CRYPTOA_MAX,
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000571};
572
Herbert Xuebc610e2007-01-01 18:37:02 +1100573#define CRYPTOA_MAX (__CRYPTOA_MAX - 1)
574
Herbert Xu39e1ee012007-08-29 19:27:26 +0800575/* Maximum number of (rtattr) parameters for each template. */
576#define CRYPTO_MAX_ATTRS 32
577
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000578struct crypto_attr_alg {
579 char name[CRYPTO_MAX_ALG_NAME];
580};
581
Herbert Xuebc610e2007-01-01 18:37:02 +1100582struct crypto_attr_type {
583 u32 type;
584 u32 mask;
585};
586
Herbert Xu39e1ee012007-08-29 19:27:26 +0800587struct crypto_attr_u32 {
588 u32 num;
589};
590
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591/*
592 * Transform user interface.
593 */
594
Herbert Xu6d7d684d2006-07-30 11:53:01 +1000595struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask);
Herbert Xu7b2cd922009-02-05 16:48:24 +1100596void crypto_destroy_tfm(void *mem, struct crypto_tfm *tfm);
597
598static inline void crypto_free_tfm(struct crypto_tfm *tfm)
599{
600 return crypto_destroy_tfm(tfm, tfm);
601}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
Herbert Xuda7f0332008-07-31 17:08:25 +0800603int alg_test(const char *driver, const char *alg, u32 type, u32 mask);
604
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605/*
606 * Transform helpers which query the underlying algorithm.
607 */
608static inline const char *crypto_tfm_alg_name(struct crypto_tfm *tfm)
609{
610 return tfm->__crt_alg->cra_name;
611}
612
Michal Ludvigb14cdd62006-07-09 09:02:24 +1000613static inline const char *crypto_tfm_alg_driver_name(struct crypto_tfm *tfm)
614{
615 return tfm->__crt_alg->cra_driver_name;
616}
617
618static inline int crypto_tfm_alg_priority(struct crypto_tfm *tfm)
619{
620 return tfm->__crt_alg->cra_priority;
621}
622
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm)
624{
625 return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK;
626}
627
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm)
629{
630 return tfm->__crt_alg->cra_blocksize;
631}
632
Herbert Xufbdae9f2005-07-06 13:53:29 -0700633static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm *tfm)
634{
635 return tfm->__crt_alg->cra_alignmask;
636}
637
Herbert Xuf28776a2006-08-13 20:58:18 +1000638static inline u32 crypto_tfm_get_flags(struct crypto_tfm *tfm)
639{
640 return tfm->crt_flags;
641}
642
643static inline void crypto_tfm_set_flags(struct crypto_tfm *tfm, u32 flags)
644{
645 tfm->crt_flags |= flags;
646}
647
648static inline void crypto_tfm_clear_flags(struct crypto_tfm *tfm, u32 flags)
649{
650 tfm->crt_flags &= ~flags;
651}
652
Herbert Xu40725182005-07-06 13:51:52 -0700653static inline void *crypto_tfm_ctx(struct crypto_tfm *tfm)
654{
Herbert Xuf10b7892006-01-25 22:34:01 +1100655 return tfm->__crt_ctx;
656}
657
658static inline unsigned int crypto_tfm_ctx_alignment(void)
659{
660 struct crypto_tfm *tfm;
661 return __alignof__(tfm->__crt_ctx);
Herbert Xu40725182005-07-06 13:51:52 -0700662}
663
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664/*
665 * API wrappers.
666 */
Herbert Xu32e3983f2007-03-24 14:35:34 +1100667static inline struct crypto_ablkcipher *__crypto_ablkcipher_cast(
668 struct crypto_tfm *tfm)
669{
670 return (struct crypto_ablkcipher *)tfm;
671}
672
Herbert Xu378f4f52007-12-17 20:07:31 +0800673static inline u32 crypto_skcipher_type(u32 type)
674{
Herbert Xuecfc4322007-12-05 21:08:36 +1100675 type &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
Herbert Xu378f4f52007-12-17 20:07:31 +0800676 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
677 return type;
678}
679
680static inline u32 crypto_skcipher_mask(u32 mask)
681{
Herbert Xuecfc4322007-12-05 21:08:36 +1100682 mask &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
Herbert Xu378f4f52007-12-17 20:07:31 +0800683 mask |= CRYPTO_ALG_TYPE_BLKCIPHER_MASK;
684 return mask;
685}
686
Stephan Muellerf13ec332014-11-12 05:28:22 +0100687/**
688 * DOC: Asynchronous Block Cipher API
689 *
690 * Asynchronous block cipher API is used with the ciphers of type
691 * CRYPTO_ALG_TYPE_ABLKCIPHER (listed as type "ablkcipher" in /proc/crypto).
692 *
693 * Asynchronous cipher operations imply that the function invocation for a
694 * cipher request returns immediately before the completion of the operation.
695 * The cipher request is scheduled as a separate kernel thread and therefore
696 * load-balanced on the different CPUs via the process scheduler. To allow
697 * the kernel crypto API to inform the caller about the completion of a cipher
698 * request, the caller must provide a callback function. That function is
699 * invoked with the cipher handle when the request completes.
700 *
701 * To support the asynchronous operation, additional information than just the
702 * cipher handle must be supplied to the kernel crypto API. That additional
703 * information is given by filling in the ablkcipher_request data structure.
704 *
705 * For the asynchronous block cipher API, the state is maintained with the tfm
706 * cipher handle. A single tfm can be used across multiple calls and in
707 * parallel. For asynchronous block cipher calls, context data supplied and
708 * only used by the caller can be referenced the request data structure in
709 * addition to the IV used for the cipher request. The maintenance of such
710 * state information would be important for a crypto driver implementer to
711 * have, because when calling the callback function upon completion of the
712 * cipher operation, that callback function may need some information about
713 * which operation just finished if it invoked multiple in parallel. This
714 * state information is unused by the kernel crypto API.
715 */
716
717/**
718 * crypto_alloc_ablkcipher() - allocate asynchronous block cipher handle
719 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
720 * ablkcipher cipher
721 * @type: specifies the type of the cipher
722 * @mask: specifies the mask for the cipher
723 *
724 * Allocate a cipher handle for an ablkcipher. The returned struct
725 * crypto_ablkcipher is the cipher handle that is required for any subsequent
726 * API invocation for that ablkcipher.
727 *
728 * Return: allocated cipher handle in case of success; IS_ERR() is true in case
729 * of an error, PTR_ERR() returns the error code.
730 */
Herbert Xub9c55aa2007-12-04 12:46:48 +1100731struct crypto_ablkcipher *crypto_alloc_ablkcipher(const char *alg_name,
732 u32 type, u32 mask);
Herbert Xu32e3983f2007-03-24 14:35:34 +1100733
734static inline struct crypto_tfm *crypto_ablkcipher_tfm(
735 struct crypto_ablkcipher *tfm)
736{
737 return &tfm->base;
738}
739
Stephan Muellerf13ec332014-11-12 05:28:22 +0100740/**
741 * crypto_free_ablkcipher() - zeroize and free cipher handle
742 * @tfm: cipher handle to be freed
743 */
Herbert Xu32e3983f2007-03-24 14:35:34 +1100744static inline void crypto_free_ablkcipher(struct crypto_ablkcipher *tfm)
745{
746 crypto_free_tfm(crypto_ablkcipher_tfm(tfm));
747}
748
Stephan Muellerf13ec332014-11-12 05:28:22 +0100749/**
750 * crypto_has_ablkcipher() - Search for the availability of an ablkcipher.
751 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
752 * ablkcipher
753 * @type: specifies the type of the cipher
754 * @mask: specifies the mask for the cipher
755 *
756 * Return: true when the ablkcipher is known to the kernel crypto API; false
757 * otherwise
758 */
Herbert Xu32e3983f2007-03-24 14:35:34 +1100759static inline int crypto_has_ablkcipher(const char *alg_name, u32 type,
760 u32 mask)
761{
Herbert Xu378f4f52007-12-17 20:07:31 +0800762 return crypto_has_alg(alg_name, crypto_skcipher_type(type),
763 crypto_skcipher_mask(mask));
Herbert Xu32e3983f2007-03-24 14:35:34 +1100764}
765
766static inline struct ablkcipher_tfm *crypto_ablkcipher_crt(
767 struct crypto_ablkcipher *tfm)
768{
769 return &crypto_ablkcipher_tfm(tfm)->crt_ablkcipher;
770}
771
Stephan Muellerf13ec332014-11-12 05:28:22 +0100772/**
773 * crypto_ablkcipher_ivsize() - obtain IV size
774 * @tfm: cipher handle
775 *
776 * The size of the IV for the ablkcipher referenced by the cipher handle is
777 * returned. This IV size may be zero if the cipher does not need an IV.
778 *
779 * Return: IV size in bytes
780 */
Herbert Xu32e3983f2007-03-24 14:35:34 +1100781static inline unsigned int crypto_ablkcipher_ivsize(
782 struct crypto_ablkcipher *tfm)
783{
784 return crypto_ablkcipher_crt(tfm)->ivsize;
785}
786
Stephan Muellerf13ec332014-11-12 05:28:22 +0100787/**
788 * crypto_ablkcipher_blocksize() - obtain block size of cipher
789 * @tfm: cipher handle
790 *
791 * The block size for the ablkcipher referenced with the cipher handle is
792 * returned. The caller may use that information to allocate appropriate
793 * memory for the data returned by the encryption or decryption operation
794 *
795 * Return: block size of cipher
796 */
Herbert Xu32e3983f2007-03-24 14:35:34 +1100797static inline unsigned int crypto_ablkcipher_blocksize(
798 struct crypto_ablkcipher *tfm)
799{
800 return crypto_tfm_alg_blocksize(crypto_ablkcipher_tfm(tfm));
801}
802
803static inline unsigned int crypto_ablkcipher_alignmask(
804 struct crypto_ablkcipher *tfm)
805{
806 return crypto_tfm_alg_alignmask(crypto_ablkcipher_tfm(tfm));
807}
808
809static inline u32 crypto_ablkcipher_get_flags(struct crypto_ablkcipher *tfm)
810{
811 return crypto_tfm_get_flags(crypto_ablkcipher_tfm(tfm));
812}
813
814static inline void crypto_ablkcipher_set_flags(struct crypto_ablkcipher *tfm,
815 u32 flags)
816{
817 crypto_tfm_set_flags(crypto_ablkcipher_tfm(tfm), flags);
818}
819
820static inline void crypto_ablkcipher_clear_flags(struct crypto_ablkcipher *tfm,
821 u32 flags)
822{
823 crypto_tfm_clear_flags(crypto_ablkcipher_tfm(tfm), flags);
824}
825
Stephan Muellerf13ec332014-11-12 05:28:22 +0100826/**
827 * crypto_ablkcipher_setkey() - set key for cipher
828 * @tfm: cipher handle
829 * @key: buffer holding the key
830 * @keylen: length of the key in bytes
831 *
832 * The caller provided key is set for the ablkcipher referenced by the cipher
833 * handle.
834 *
835 * Note, the key length determines the cipher type. Many block ciphers implement
836 * different cipher modes depending on the key size, such as AES-128 vs AES-192
837 * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
838 * is performed.
839 *
840 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
841 */
Herbert Xu32e3983f2007-03-24 14:35:34 +1100842static inline int crypto_ablkcipher_setkey(struct crypto_ablkcipher *tfm,
843 const u8 *key, unsigned int keylen)
844{
Herbert Xuecfc4322007-12-05 21:08:36 +1100845 struct ablkcipher_tfm *crt = crypto_ablkcipher_crt(tfm);
846
847 return crt->setkey(crt->base, key, keylen);
Herbert Xu32e3983f2007-03-24 14:35:34 +1100848}
849
Stephan Muellerf13ec332014-11-12 05:28:22 +0100850/**
851 * crypto_ablkcipher_reqtfm() - obtain cipher handle from request
852 * @req: ablkcipher_request out of which the cipher handle is to be obtained
853 *
854 * Return the crypto_ablkcipher handle when furnishing an ablkcipher_request
855 * data structure.
856 *
857 * Return: crypto_ablkcipher handle
858 */
Herbert Xu32e3983f2007-03-24 14:35:34 +1100859static inline struct crypto_ablkcipher *crypto_ablkcipher_reqtfm(
860 struct ablkcipher_request *req)
861{
862 return __crypto_ablkcipher_cast(req->base.tfm);
863}
864
Stephan Muellerf13ec332014-11-12 05:28:22 +0100865/**
866 * crypto_ablkcipher_encrypt() - encrypt plaintext
867 * @req: reference to the ablkcipher_request handle that holds all information
868 * needed to perform the cipher operation
869 *
870 * Encrypt plaintext data using the ablkcipher_request handle. That data
871 * structure and how it is filled with data is discussed with the
872 * ablkcipher_request_* functions.
873 *
874 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
875 */
Herbert Xu32e3983f2007-03-24 14:35:34 +1100876static inline int crypto_ablkcipher_encrypt(struct ablkcipher_request *req)
877{
878 struct ablkcipher_tfm *crt =
879 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
880 return crt->encrypt(req);
881}
882
Stephan Muellerf13ec332014-11-12 05:28:22 +0100883/**
884 * crypto_ablkcipher_decrypt() - decrypt ciphertext
885 * @req: reference to the ablkcipher_request handle that holds all information
886 * needed to perform the cipher operation
887 *
888 * Decrypt ciphertext data using the ablkcipher_request handle. That data
889 * structure and how it is filled with data is discussed with the
890 * ablkcipher_request_* functions.
891 *
892 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
893 */
Herbert Xu32e3983f2007-03-24 14:35:34 +1100894static inline int crypto_ablkcipher_decrypt(struct ablkcipher_request *req)
895{
896 struct ablkcipher_tfm *crt =
897 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
898 return crt->decrypt(req);
899}
900
Stephan Muellerf13ec332014-11-12 05:28:22 +0100901/**
902 * DOC: Asynchronous Cipher Request Handle
903 *
904 * The ablkcipher_request data structure contains all pointers to data
905 * required for the asynchronous cipher operation. This includes the cipher
906 * handle (which can be used by multiple ablkcipher_request instances), pointer
907 * to plaintext and ciphertext, asynchronous callback function, etc. It acts
908 * as a handle to the ablkcipher_request_* API calls in a similar way as
909 * ablkcipher handle to the crypto_ablkcipher_* API calls.
910 */
911
912/**
913 * crypto_ablkcipher_reqsize() - obtain size of the request data structure
914 * @tfm: cipher handle
915 *
916 * Return: number of bytes
917 */
Herbert Xub16c3a22007-08-29 19:02:04 +0800918static inline unsigned int crypto_ablkcipher_reqsize(
919 struct crypto_ablkcipher *tfm)
Herbert Xu32e3983f2007-03-24 14:35:34 +1100920{
921 return crypto_ablkcipher_crt(tfm)->reqsize;
922}
923
Stephan Muellerf13ec332014-11-12 05:28:22 +0100924/**
925 * ablkcipher_request_set_tfm() - update cipher handle reference in request
926 * @req: request handle to be modified
927 * @tfm: cipher handle that shall be added to the request handle
928 *
929 * Allow the caller to replace the existing ablkcipher handle in the request
930 * data structure with a different one.
931 */
Herbert Xue196d622007-04-14 16:09:14 +1000932static inline void ablkcipher_request_set_tfm(
933 struct ablkcipher_request *req, struct crypto_ablkcipher *tfm)
934{
Herbert Xuecfc4322007-12-05 21:08:36 +1100935 req->base.tfm = crypto_ablkcipher_tfm(crypto_ablkcipher_crt(tfm)->base);
Herbert Xue196d622007-04-14 16:09:14 +1000936}
937
Herbert Xub5b7f082007-04-16 20:48:54 +1000938static inline struct ablkcipher_request *ablkcipher_request_cast(
939 struct crypto_async_request *req)
940{
941 return container_of(req, struct ablkcipher_request, base);
942}
943
Stephan Muellerf13ec332014-11-12 05:28:22 +0100944/**
945 * ablkcipher_request_alloc() - allocate request data structure
946 * @tfm: cipher handle to be registered with the request
947 * @gfp: memory allocation flag that is handed to kmalloc by the API call.
948 *
949 * Allocate the request data structure that must be used with the ablkcipher
950 * encrypt and decrypt API calls. During the allocation, the provided ablkcipher
951 * handle is registered in the request data structure.
952 *
Eric Biggers6eae29e2016-04-02 10:54:56 -0500953 * Return: allocated request handle in case of success, or NULL if out of memory
Stephan Muellerf13ec332014-11-12 05:28:22 +0100954 */
Herbert Xu32e3983f2007-03-24 14:35:34 +1100955static inline struct ablkcipher_request *ablkcipher_request_alloc(
956 struct crypto_ablkcipher *tfm, gfp_t gfp)
957{
958 struct ablkcipher_request *req;
959
960 req = kmalloc(sizeof(struct ablkcipher_request) +
961 crypto_ablkcipher_reqsize(tfm), gfp);
962
963 if (likely(req))
Herbert Xue196d622007-04-14 16:09:14 +1000964 ablkcipher_request_set_tfm(req, tfm);
Herbert Xu32e3983f2007-03-24 14:35:34 +1100965
966 return req;
967}
968
Stephan Muellerf13ec332014-11-12 05:28:22 +0100969/**
970 * ablkcipher_request_free() - zeroize and free request data structure
971 * @req: request data structure cipher handle to be freed
972 */
Herbert Xu32e3983f2007-03-24 14:35:34 +1100973static inline void ablkcipher_request_free(struct ablkcipher_request *req)
974{
Herbert Xuaef73cf2009-07-11 22:22:14 +0800975 kzfree(req);
Herbert Xu32e3983f2007-03-24 14:35:34 +1100976}
977
Stephan Muellerf13ec332014-11-12 05:28:22 +0100978/**
979 * ablkcipher_request_set_callback() - set asynchronous callback function
980 * @req: request handle
981 * @flags: specify zero or an ORing of the flags
982 * CRYPTO_TFM_REQ_MAY_BACKLOG the request queue may back log and
983 * increase the wait queue beyond the initial maximum size;
984 * CRYPTO_TFM_REQ_MAY_SLEEP the request processing may sleep
985 * @compl: callback function pointer to be registered with the request handle
986 * @data: The data pointer refers to memory that is not used by the kernel
987 * crypto API, but provided to the callback function for it to use. Here,
988 * the caller can provide a reference to memory the callback function can
989 * operate on. As the callback function is invoked asynchronously to the
990 * related functionality, it may need to access data structures of the
991 * related functionality which can be referenced using this pointer. The
992 * callback function can access the memory via the "data" field in the
993 * crypto_async_request data structure provided to the callback function.
994 *
995 * This function allows setting the callback function that is triggered once the
996 * cipher operation completes.
997 *
998 * The callback function is registered with the ablkcipher_request handle and
Stephan Mueller379dcfb2015-01-19 00:13:39 +0100999 * must comply with the following template
Stephan Muellerf13ec332014-11-12 05:28:22 +01001000 *
1001 * void callback_function(struct crypto_async_request *req, int error)
1002 */
Herbert Xu32e3983f2007-03-24 14:35:34 +11001003static inline void ablkcipher_request_set_callback(
1004 struct ablkcipher_request *req,
Mark Rustad3e3dc252014-07-25 02:53:38 -07001005 u32 flags, crypto_completion_t compl, void *data)
Herbert Xu32e3983f2007-03-24 14:35:34 +11001006{
Mark Rustad3e3dc252014-07-25 02:53:38 -07001007 req->base.complete = compl;
Herbert Xu32e3983f2007-03-24 14:35:34 +11001008 req->base.data = data;
1009 req->base.flags = flags;
1010}
1011
Stephan Muellerf13ec332014-11-12 05:28:22 +01001012/**
1013 * ablkcipher_request_set_crypt() - set data buffers
1014 * @req: request handle
1015 * @src: source scatter / gather list
1016 * @dst: destination scatter / gather list
1017 * @nbytes: number of bytes to process from @src
1018 * @iv: IV for the cipher operation which must comply with the IV size defined
1019 * by crypto_ablkcipher_ivsize
1020 *
1021 * This function allows setting of the source data and destination data
1022 * scatter / gather lists.
1023 *
1024 * For encryption, the source is treated as the plaintext and the
1025 * destination is the ciphertext. For a decryption operation, the use is
Stephan Mueller379dcfb2015-01-19 00:13:39 +01001026 * reversed - the source is the ciphertext and the destination is the plaintext.
Stephan Muellerf13ec332014-11-12 05:28:22 +01001027 */
Herbert Xu32e3983f2007-03-24 14:35:34 +11001028static inline void ablkcipher_request_set_crypt(
1029 struct ablkcipher_request *req,
1030 struct scatterlist *src, struct scatterlist *dst,
1031 unsigned int nbytes, void *iv)
1032{
1033 req->src = src;
1034 req->dst = dst;
1035 req->nbytes = nbytes;
1036 req->info = iv;
1037}
1038
Stephan Muellerfced7b02014-11-12 05:29:00 +01001039/**
Stephan Mueller58284f02014-11-12 05:29:36 +01001040 * DOC: Synchronous Block Cipher API
1041 *
1042 * The synchronous block cipher API is used with the ciphers of type
1043 * CRYPTO_ALG_TYPE_BLKCIPHER (listed as type "blkcipher" in /proc/crypto)
1044 *
1045 * Synchronous calls, have a context in the tfm. But since a single tfm can be
1046 * used in multiple calls and in parallel, this info should not be changeable
1047 * (unless a lock is used). This applies, for example, to the symmetric key.
1048 * However, the IV is changeable, so there is an iv field in blkcipher_tfm
1049 * structure for synchronous blkcipher api. So, its the only state info that can
1050 * be kept for synchronous calls without using a big lock across a tfm.
1051 *
1052 * The block cipher API allows the use of a complete cipher, i.e. a cipher
1053 * consisting of a template (a block chaining mode) and a single block cipher
1054 * primitive (e.g. AES).
1055 *
1056 * The plaintext data buffer and the ciphertext data buffer are pointed to
1057 * by using scatter/gather lists. The cipher operation is performed
1058 * on all segments of the provided scatter/gather lists.
1059 *
1060 * The kernel crypto API supports a cipher operation "in-place" which means that
1061 * the caller may provide the same scatter/gather list for the plaintext and
1062 * cipher text. After the completion of the cipher operation, the plaintext
1063 * data is replaced with the ciphertext data in case of an encryption and vice
1064 * versa for a decryption. The caller must ensure that the scatter/gather lists
1065 * for the output data point to sufficiently large buffers, i.e. multiples of
1066 * the block size of the cipher.
1067 */
1068
Herbert Xu5cde0af2006-08-22 00:07:53 +10001069static inline struct crypto_blkcipher *__crypto_blkcipher_cast(
1070 struct crypto_tfm *tfm)
1071{
1072 return (struct crypto_blkcipher *)tfm;
1073}
1074
1075static inline struct crypto_blkcipher *crypto_blkcipher_cast(
1076 struct crypto_tfm *tfm)
1077{
1078 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_BLKCIPHER);
1079 return __crypto_blkcipher_cast(tfm);
1080}
1081
Stephan Mueller58284f02014-11-12 05:29:36 +01001082/**
1083 * crypto_alloc_blkcipher() - allocate synchronous block cipher handle
1084 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1085 * blkcipher cipher
1086 * @type: specifies the type of the cipher
1087 * @mask: specifies the mask for the cipher
1088 *
1089 * Allocate a cipher handle for a block cipher. The returned struct
1090 * crypto_blkcipher is the cipher handle that is required for any subsequent
1091 * API invocation for that block cipher.
1092 *
1093 * Return: allocated cipher handle in case of success; IS_ERR() is true in case
1094 * of an error, PTR_ERR() returns the error code.
1095 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001096static inline struct crypto_blkcipher *crypto_alloc_blkcipher(
1097 const char *alg_name, u32 type, u32 mask)
1098{
Herbert Xu332f88402007-11-15 22:36:07 +08001099 type &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xu5cde0af2006-08-22 00:07:53 +10001100 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
Herbert Xu332f88402007-11-15 22:36:07 +08001101 mask |= CRYPTO_ALG_TYPE_MASK;
Herbert Xu5cde0af2006-08-22 00:07:53 +10001102
1103 return __crypto_blkcipher_cast(crypto_alloc_base(alg_name, type, mask));
1104}
1105
1106static inline struct crypto_tfm *crypto_blkcipher_tfm(
1107 struct crypto_blkcipher *tfm)
1108{
1109 return &tfm->base;
1110}
1111
Stephan Mueller58284f02014-11-12 05:29:36 +01001112/**
1113 * crypto_free_blkcipher() - zeroize and free the block cipher handle
1114 * @tfm: cipher handle to be freed
1115 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001116static inline void crypto_free_blkcipher(struct crypto_blkcipher *tfm)
1117{
1118 crypto_free_tfm(crypto_blkcipher_tfm(tfm));
1119}
1120
Stephan Mueller58284f02014-11-12 05:29:36 +01001121/**
1122 * crypto_has_blkcipher() - Search for the availability of a block cipher
1123 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1124 * block cipher
1125 * @type: specifies the type of the cipher
1126 * @mask: specifies the mask for the cipher
1127 *
1128 * Return: true when the block cipher is known to the kernel crypto API; false
1129 * otherwise
1130 */
Herbert Xufce32d72006-08-26 17:35:45 +10001131static inline int crypto_has_blkcipher(const char *alg_name, u32 type, u32 mask)
1132{
Herbert Xu332f88402007-11-15 22:36:07 +08001133 type &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xufce32d72006-08-26 17:35:45 +10001134 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
Herbert Xu332f88402007-11-15 22:36:07 +08001135 mask |= CRYPTO_ALG_TYPE_MASK;
Herbert Xufce32d72006-08-26 17:35:45 +10001136
1137 return crypto_has_alg(alg_name, type, mask);
1138}
1139
Stephan Mueller58284f02014-11-12 05:29:36 +01001140/**
1141 * crypto_blkcipher_name() - return the name / cra_name from the cipher handle
1142 * @tfm: cipher handle
1143 *
1144 * Return: The character string holding the name of the cipher
1145 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001146static inline const char *crypto_blkcipher_name(struct crypto_blkcipher *tfm)
1147{
1148 return crypto_tfm_alg_name(crypto_blkcipher_tfm(tfm));
1149}
1150
1151static inline struct blkcipher_tfm *crypto_blkcipher_crt(
1152 struct crypto_blkcipher *tfm)
1153{
1154 return &crypto_blkcipher_tfm(tfm)->crt_blkcipher;
1155}
1156
1157static inline struct blkcipher_alg *crypto_blkcipher_alg(
1158 struct crypto_blkcipher *tfm)
1159{
1160 return &crypto_blkcipher_tfm(tfm)->__crt_alg->cra_blkcipher;
1161}
1162
Stephan Mueller58284f02014-11-12 05:29:36 +01001163/**
1164 * crypto_blkcipher_ivsize() - obtain IV size
1165 * @tfm: cipher handle
1166 *
1167 * The size of the IV for the block cipher referenced by the cipher handle is
1168 * returned. This IV size may be zero if the cipher does not need an IV.
1169 *
1170 * Return: IV size in bytes
1171 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001172static inline unsigned int crypto_blkcipher_ivsize(struct crypto_blkcipher *tfm)
1173{
1174 return crypto_blkcipher_alg(tfm)->ivsize;
1175}
1176
Stephan Mueller58284f02014-11-12 05:29:36 +01001177/**
1178 * crypto_blkcipher_blocksize() - obtain block size of cipher
1179 * @tfm: cipher handle
1180 *
1181 * The block size for the block cipher referenced with the cipher handle is
1182 * returned. The caller may use that information to allocate appropriate
1183 * memory for the data returned by the encryption or decryption operation.
1184 *
1185 * Return: block size of cipher
1186 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001187static inline unsigned int crypto_blkcipher_blocksize(
1188 struct crypto_blkcipher *tfm)
1189{
1190 return crypto_tfm_alg_blocksize(crypto_blkcipher_tfm(tfm));
1191}
1192
1193static inline unsigned int crypto_blkcipher_alignmask(
1194 struct crypto_blkcipher *tfm)
1195{
1196 return crypto_tfm_alg_alignmask(crypto_blkcipher_tfm(tfm));
1197}
1198
1199static inline u32 crypto_blkcipher_get_flags(struct crypto_blkcipher *tfm)
1200{
1201 return crypto_tfm_get_flags(crypto_blkcipher_tfm(tfm));
1202}
1203
1204static inline void crypto_blkcipher_set_flags(struct crypto_blkcipher *tfm,
1205 u32 flags)
1206{
1207 crypto_tfm_set_flags(crypto_blkcipher_tfm(tfm), flags);
1208}
1209
1210static inline void crypto_blkcipher_clear_flags(struct crypto_blkcipher *tfm,
1211 u32 flags)
1212{
1213 crypto_tfm_clear_flags(crypto_blkcipher_tfm(tfm), flags);
1214}
1215
Stephan Mueller58284f02014-11-12 05:29:36 +01001216/**
1217 * crypto_blkcipher_setkey() - set key for cipher
1218 * @tfm: cipher handle
1219 * @key: buffer holding the key
1220 * @keylen: length of the key in bytes
1221 *
1222 * The caller provided key is set for the block cipher referenced by the cipher
1223 * handle.
1224 *
1225 * Note, the key length determines the cipher type. Many block ciphers implement
1226 * different cipher modes depending on the key size, such as AES-128 vs AES-192
1227 * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
1228 * is performed.
1229 *
1230 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
1231 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001232static inline int crypto_blkcipher_setkey(struct crypto_blkcipher *tfm,
1233 const u8 *key, unsigned int keylen)
1234{
1235 return crypto_blkcipher_crt(tfm)->setkey(crypto_blkcipher_tfm(tfm),
1236 key, keylen);
1237}
1238
Stephan Mueller58284f02014-11-12 05:29:36 +01001239/**
1240 * crypto_blkcipher_encrypt() - encrypt plaintext
1241 * @desc: reference to the block cipher handle with meta data
1242 * @dst: scatter/gather list that is filled by the cipher operation with the
1243 * ciphertext
1244 * @src: scatter/gather list that holds the plaintext
1245 * @nbytes: number of bytes of the plaintext to encrypt.
1246 *
1247 * Encrypt plaintext data using the IV set by the caller with a preceding
1248 * call of crypto_blkcipher_set_iv.
1249 *
1250 * The blkcipher_desc data structure must be filled by the caller and can
1251 * reside on the stack. The caller must fill desc as follows: desc.tfm is filled
1252 * with the block cipher handle; desc.flags is filled with either
1253 * CRYPTO_TFM_REQ_MAY_SLEEP or 0.
1254 *
1255 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1256 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001257static inline int crypto_blkcipher_encrypt(struct blkcipher_desc *desc,
1258 struct scatterlist *dst,
1259 struct scatterlist *src,
1260 unsigned int nbytes)
1261{
1262 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
1263 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
1264}
1265
Stephan Mueller58284f02014-11-12 05:29:36 +01001266/**
1267 * crypto_blkcipher_encrypt_iv() - encrypt plaintext with dedicated IV
1268 * @desc: reference to the block cipher handle with meta data
1269 * @dst: scatter/gather list that is filled by the cipher operation with the
1270 * ciphertext
1271 * @src: scatter/gather list that holds the plaintext
1272 * @nbytes: number of bytes of the plaintext to encrypt.
1273 *
1274 * Encrypt plaintext data with the use of an IV that is solely used for this
1275 * cipher operation. Any previously set IV is not used.
1276 *
1277 * The blkcipher_desc data structure must be filled by the caller and can
1278 * reside on the stack. The caller must fill desc as follows: desc.tfm is filled
1279 * with the block cipher handle; desc.info is filled with the IV to be used for
1280 * the current operation; desc.flags is filled with either
1281 * CRYPTO_TFM_REQ_MAY_SLEEP or 0.
1282 *
1283 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1284 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001285static inline int crypto_blkcipher_encrypt_iv(struct blkcipher_desc *desc,
1286 struct scatterlist *dst,
1287 struct scatterlist *src,
1288 unsigned int nbytes)
1289{
1290 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
1291}
1292
Stephan Mueller58284f02014-11-12 05:29:36 +01001293/**
1294 * crypto_blkcipher_decrypt() - decrypt ciphertext
1295 * @desc: reference to the block cipher handle with meta data
1296 * @dst: scatter/gather list that is filled by the cipher operation with the
1297 * plaintext
1298 * @src: scatter/gather list that holds the ciphertext
1299 * @nbytes: number of bytes of the ciphertext to decrypt.
1300 *
1301 * Decrypt ciphertext data using the IV set by the caller with a preceding
1302 * call of crypto_blkcipher_set_iv.
1303 *
1304 * The blkcipher_desc data structure must be filled by the caller as documented
1305 * for the crypto_blkcipher_encrypt call above.
1306 *
1307 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1308 *
1309 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001310static inline int crypto_blkcipher_decrypt(struct blkcipher_desc *desc,
1311 struct scatterlist *dst,
1312 struct scatterlist *src,
1313 unsigned int nbytes)
1314{
1315 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
1316 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
1317}
1318
Stephan Mueller58284f02014-11-12 05:29:36 +01001319/**
1320 * crypto_blkcipher_decrypt_iv() - decrypt ciphertext with dedicated IV
1321 * @desc: reference to the block cipher handle with meta data
1322 * @dst: scatter/gather list that is filled by the cipher operation with the
1323 * plaintext
1324 * @src: scatter/gather list that holds the ciphertext
1325 * @nbytes: number of bytes of the ciphertext to decrypt.
1326 *
1327 * Decrypt ciphertext data with the use of an IV that is solely used for this
1328 * cipher operation. Any previously set IV is not used.
1329 *
1330 * The blkcipher_desc data structure must be filled by the caller as documented
1331 * for the crypto_blkcipher_encrypt_iv call above.
1332 *
1333 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1334 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001335static inline int crypto_blkcipher_decrypt_iv(struct blkcipher_desc *desc,
1336 struct scatterlist *dst,
1337 struct scatterlist *src,
1338 unsigned int nbytes)
1339{
1340 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
1341}
1342
Stephan Mueller58284f02014-11-12 05:29:36 +01001343/**
1344 * crypto_blkcipher_set_iv() - set IV for cipher
1345 * @tfm: cipher handle
1346 * @src: buffer holding the IV
1347 * @len: length of the IV in bytes
1348 *
1349 * The caller provided IV is set for the block cipher referenced by the cipher
1350 * handle.
1351 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001352static inline void crypto_blkcipher_set_iv(struct crypto_blkcipher *tfm,
1353 const u8 *src, unsigned int len)
1354{
1355 memcpy(crypto_blkcipher_crt(tfm)->iv, src, len);
1356}
1357
Stephan Mueller58284f02014-11-12 05:29:36 +01001358/**
1359 * crypto_blkcipher_get_iv() - obtain IV from cipher
1360 * @tfm: cipher handle
1361 * @dst: buffer filled with the IV
1362 * @len: length of the buffer dst
1363 *
1364 * The caller can obtain the IV set for the block cipher referenced by the
1365 * cipher handle and store it into the user-provided buffer. If the buffer
1366 * has an insufficient space, the IV is truncated to fit the buffer.
1367 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001368static inline void crypto_blkcipher_get_iv(struct crypto_blkcipher *tfm,
1369 u8 *dst, unsigned int len)
1370{
1371 memcpy(dst, crypto_blkcipher_crt(tfm)->iv, len);
1372}
1373
Stephan Mueller16e61032014-11-12 05:30:06 +01001374/**
1375 * DOC: Single Block Cipher API
1376 *
1377 * The single block cipher API is used with the ciphers of type
1378 * CRYPTO_ALG_TYPE_CIPHER (listed as type "cipher" in /proc/crypto).
1379 *
1380 * Using the single block cipher API calls, operations with the basic cipher
1381 * primitive can be implemented. These cipher primitives exclude any block
1382 * chaining operations including IV handling.
1383 *
1384 * The purpose of this single block cipher API is to support the implementation
1385 * of templates or other concepts that only need to perform the cipher operation
1386 * on one block at a time. Templates invoke the underlying cipher primitive
1387 * block-wise and process either the input or the output data of these cipher
1388 * operations.
1389 */
1390
Herbert Xuf28776a2006-08-13 20:58:18 +10001391static inline struct crypto_cipher *__crypto_cipher_cast(struct crypto_tfm *tfm)
1392{
1393 return (struct crypto_cipher *)tfm;
1394}
1395
1396static inline struct crypto_cipher *crypto_cipher_cast(struct crypto_tfm *tfm)
1397{
1398 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
1399 return __crypto_cipher_cast(tfm);
1400}
1401
Stephan Mueller16e61032014-11-12 05:30:06 +01001402/**
1403 * crypto_alloc_cipher() - allocate single block cipher handle
1404 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1405 * single block cipher
1406 * @type: specifies the type of the cipher
1407 * @mask: specifies the mask for the cipher
1408 *
1409 * Allocate a cipher handle for a single block cipher. The returned struct
1410 * crypto_cipher is the cipher handle that is required for any subsequent API
1411 * invocation for that single block cipher.
1412 *
1413 * Return: allocated cipher handle in case of success; IS_ERR() is true in case
1414 * of an error, PTR_ERR() returns the error code.
1415 */
Herbert Xuf28776a2006-08-13 20:58:18 +10001416static inline struct crypto_cipher *crypto_alloc_cipher(const char *alg_name,
1417 u32 type, u32 mask)
1418{
1419 type &= ~CRYPTO_ALG_TYPE_MASK;
1420 type |= CRYPTO_ALG_TYPE_CIPHER;
1421 mask |= CRYPTO_ALG_TYPE_MASK;
1422
1423 return __crypto_cipher_cast(crypto_alloc_base(alg_name, type, mask));
1424}
1425
1426static inline struct crypto_tfm *crypto_cipher_tfm(struct crypto_cipher *tfm)
1427{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001428 return &tfm->base;
Herbert Xuf28776a2006-08-13 20:58:18 +10001429}
1430
Stephan Mueller16e61032014-11-12 05:30:06 +01001431/**
1432 * crypto_free_cipher() - zeroize and free the single block cipher handle
1433 * @tfm: cipher handle to be freed
1434 */
Herbert Xuf28776a2006-08-13 20:58:18 +10001435static inline void crypto_free_cipher(struct crypto_cipher *tfm)
1436{
1437 crypto_free_tfm(crypto_cipher_tfm(tfm));
1438}
1439
Stephan Mueller16e61032014-11-12 05:30:06 +01001440/**
1441 * crypto_has_cipher() - Search for the availability of a single block cipher
1442 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1443 * single block cipher
1444 * @type: specifies the type of the cipher
1445 * @mask: specifies the mask for the cipher
1446 *
1447 * Return: true when the single block cipher is known to the kernel crypto API;
1448 * false otherwise
1449 */
Herbert Xufce32d72006-08-26 17:35:45 +10001450static inline int crypto_has_cipher(const char *alg_name, u32 type, u32 mask)
1451{
1452 type &= ~CRYPTO_ALG_TYPE_MASK;
1453 type |= CRYPTO_ALG_TYPE_CIPHER;
1454 mask |= CRYPTO_ALG_TYPE_MASK;
1455
1456 return crypto_has_alg(alg_name, type, mask);
1457}
1458
Herbert Xuf28776a2006-08-13 20:58:18 +10001459static inline struct cipher_tfm *crypto_cipher_crt(struct crypto_cipher *tfm)
1460{
1461 return &crypto_cipher_tfm(tfm)->crt_cipher;
1462}
1463
Stephan Mueller16e61032014-11-12 05:30:06 +01001464/**
1465 * crypto_cipher_blocksize() - obtain block size for cipher
1466 * @tfm: cipher handle
1467 *
1468 * The block size for the single block cipher referenced with the cipher handle
1469 * tfm is returned. The caller may use that information to allocate appropriate
1470 * memory for the data returned by the encryption or decryption operation
1471 *
1472 * Return: block size of cipher
1473 */
Herbert Xuf28776a2006-08-13 20:58:18 +10001474static inline unsigned int crypto_cipher_blocksize(struct crypto_cipher *tfm)
1475{
1476 return crypto_tfm_alg_blocksize(crypto_cipher_tfm(tfm));
1477}
1478
1479static inline unsigned int crypto_cipher_alignmask(struct crypto_cipher *tfm)
1480{
1481 return crypto_tfm_alg_alignmask(crypto_cipher_tfm(tfm));
1482}
1483
1484static inline u32 crypto_cipher_get_flags(struct crypto_cipher *tfm)
1485{
1486 return crypto_tfm_get_flags(crypto_cipher_tfm(tfm));
1487}
1488
1489static inline void crypto_cipher_set_flags(struct crypto_cipher *tfm,
1490 u32 flags)
1491{
1492 crypto_tfm_set_flags(crypto_cipher_tfm(tfm), flags);
1493}
1494
1495static inline void crypto_cipher_clear_flags(struct crypto_cipher *tfm,
1496 u32 flags)
1497{
1498 crypto_tfm_clear_flags(crypto_cipher_tfm(tfm), flags);
1499}
1500
Stephan Mueller16e61032014-11-12 05:30:06 +01001501/**
1502 * crypto_cipher_setkey() - set key for cipher
1503 * @tfm: cipher handle
1504 * @key: buffer holding the key
1505 * @keylen: length of the key in bytes
1506 *
1507 * The caller provided key is set for the single block cipher referenced by the
1508 * cipher handle.
1509 *
1510 * Note, the key length determines the cipher type. Many block ciphers implement
1511 * different cipher modes depending on the key size, such as AES-128 vs AES-192
1512 * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
1513 * is performed.
1514 *
1515 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
1516 */
Herbert Xu7226bc872006-08-21 21:40:49 +10001517static inline int crypto_cipher_setkey(struct crypto_cipher *tfm,
1518 const u8 *key, unsigned int keylen)
1519{
1520 return crypto_cipher_crt(tfm)->cit_setkey(crypto_cipher_tfm(tfm),
1521 key, keylen);
1522}
1523
Stephan Mueller16e61032014-11-12 05:30:06 +01001524/**
1525 * crypto_cipher_encrypt_one() - encrypt one block of plaintext
1526 * @tfm: cipher handle
1527 * @dst: points to the buffer that will be filled with the ciphertext
1528 * @src: buffer holding the plaintext to be encrypted
1529 *
1530 * Invoke the encryption operation of one block. The caller must ensure that
1531 * the plaintext and ciphertext buffers are at least one block in size.
1532 */
Herbert Xuf28776a2006-08-13 20:58:18 +10001533static inline void crypto_cipher_encrypt_one(struct crypto_cipher *tfm,
1534 u8 *dst, const u8 *src)
1535{
1536 crypto_cipher_crt(tfm)->cit_encrypt_one(crypto_cipher_tfm(tfm),
1537 dst, src);
1538}
1539
Stephan Mueller16e61032014-11-12 05:30:06 +01001540/**
1541 * crypto_cipher_decrypt_one() - decrypt one block of ciphertext
1542 * @tfm: cipher handle
1543 * @dst: points to the buffer that will be filled with the plaintext
1544 * @src: buffer holding the ciphertext to be decrypted
1545 *
1546 * Invoke the decryption operation of one block. The caller must ensure that
1547 * the plaintext and ciphertext buffers are at least one block in size.
1548 */
Herbert Xuf28776a2006-08-13 20:58:18 +10001549static inline void crypto_cipher_decrypt_one(struct crypto_cipher *tfm,
1550 u8 *dst, const u8 *src)
1551{
1552 crypto_cipher_crt(tfm)->cit_decrypt_one(crypto_cipher_tfm(tfm),
1553 dst, src);
1554}
1555
Herbert Xufce32d72006-08-26 17:35:45 +10001556static inline struct crypto_comp *__crypto_comp_cast(struct crypto_tfm *tfm)
1557{
1558 return (struct crypto_comp *)tfm;
1559}
1560
1561static inline struct crypto_comp *crypto_comp_cast(struct crypto_tfm *tfm)
1562{
1563 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_COMPRESS) &
1564 CRYPTO_ALG_TYPE_MASK);
1565 return __crypto_comp_cast(tfm);
1566}
1567
1568static inline struct crypto_comp *crypto_alloc_comp(const char *alg_name,
1569 u32 type, u32 mask)
1570{
1571 type &= ~CRYPTO_ALG_TYPE_MASK;
1572 type |= CRYPTO_ALG_TYPE_COMPRESS;
1573 mask |= CRYPTO_ALG_TYPE_MASK;
1574
1575 return __crypto_comp_cast(crypto_alloc_base(alg_name, type, mask));
1576}
1577
1578static inline struct crypto_tfm *crypto_comp_tfm(struct crypto_comp *tfm)
1579{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001580 return &tfm->base;
Herbert Xufce32d72006-08-26 17:35:45 +10001581}
1582
1583static inline void crypto_free_comp(struct crypto_comp *tfm)
1584{
1585 crypto_free_tfm(crypto_comp_tfm(tfm));
1586}
1587
1588static inline int crypto_has_comp(const char *alg_name, u32 type, u32 mask)
1589{
1590 type &= ~CRYPTO_ALG_TYPE_MASK;
1591 type |= CRYPTO_ALG_TYPE_COMPRESS;
1592 mask |= CRYPTO_ALG_TYPE_MASK;
1593
1594 return crypto_has_alg(alg_name, type, mask);
1595}
1596
Herbert Xue4d5b792006-08-26 18:12:40 +10001597static inline const char *crypto_comp_name(struct crypto_comp *tfm)
1598{
1599 return crypto_tfm_alg_name(crypto_comp_tfm(tfm));
1600}
1601
Herbert Xufce32d72006-08-26 17:35:45 +10001602static inline struct compress_tfm *crypto_comp_crt(struct crypto_comp *tfm)
1603{
1604 return &crypto_comp_tfm(tfm)->crt_compress;
1605}
1606
1607static inline int crypto_comp_compress(struct crypto_comp *tfm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 const u8 *src, unsigned int slen,
1609 u8 *dst, unsigned int *dlen)
1610{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001611 return crypto_comp_crt(tfm)->cot_compress(crypto_comp_tfm(tfm),
1612 src, slen, dst, dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613}
1614
Herbert Xufce32d72006-08-26 17:35:45 +10001615static inline int crypto_comp_decompress(struct crypto_comp *tfm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 const u8 *src, unsigned int slen,
1617 u8 *dst, unsigned int *dlen)
1618{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001619 return crypto_comp_crt(tfm)->cot_decompress(crypto_comp_tfm(tfm),
1620 src, slen, dst, dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621}
1622
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623#endif /* _LINUX_CRYPTO_H */
1624