blob: 81339e184ae7d3263522e777e34944b06fab4f39 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Scatterlist Cryptographic API.
3 *
4 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
5 * Copyright (c) 2002 David S. Miller (davem@redhat.com)
Herbert Xu5cb14542005-11-05 16:58:14 +11006 * Copyright (c) 2005 Herbert Xu <herbert@gondor.apana.org.au>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * Portions derived from Cryptoapi, by Alexander Kjeldaas <astor@fast.no>
John Anthony Kazos Jr18735dd2007-10-19 23:07:36 +02009 * and Nettle, by Niels Möller.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the Free
13 * Software Foundation; either version 2 of the License, or (at your option)
14 * any later version.
15 *
16 */
17#ifndef _LINUX_CRYPTO_H
18#define _LINUX_CRYPTO_H
19
Arun Sharma600634972011-07-26 16:09:06 -070020#include <linux/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/list.h>
Paul Gortmaker187f1882011-11-23 20:12:59 -050023#include <linux/bug.h>
Herbert Xu79911102006-08-21 21:03:52 +100024#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/string.h>
Herbert Xu79911102006-08-21 21:03:52 +100026#include <linux/uaccess.h>
Jaegeuk Kim8dec0742017-06-22 12:14:40 -070027#include <linux/completion.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29/*
Kees Cook5d26a102014-11-20 17:05:53 -080030 * Autoloaded crypto modules should only use a prefixed name to avoid allowing
31 * arbitrary modules to be loaded. Loading from userspace may still need the
32 * unprefixed names, so retains those aliases as well.
33 * This uses __MODULE_INFO directly instead of MODULE_ALIAS because pre-4.3
34 * gcc (e.g. avr32 toolchain) uses __LINE__ for uniqueness, and this macro
35 * expands twice on the same line. Instead, use a separate base name for the
36 * alias.
37 */
38#define MODULE_ALIAS_CRYPTO(name) \
39 __MODULE_INFO(alias, alias_userspace, name); \
40 __MODULE_INFO(alias, alias_crypto, "crypto-" name)
41
42/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 * Algorithm masks and types.
44 */
Herbert Xu28259822006-08-06 21:23:26 +100045#define CRYPTO_ALG_TYPE_MASK 0x0000000f
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#define CRYPTO_ALG_TYPE_CIPHER 0x00000001
Loc Ho004a4032008-05-14 20:41:47 +080047#define CRYPTO_ALG_TYPE_COMPRESS 0x00000002
48#define CRYPTO_ALG_TYPE_AEAD 0x00000003
Herbert Xu055bcee2006-08-19 22:24:23 +100049#define CRYPTO_ALG_TYPE_BLKCIPHER 0x00000004
Herbert Xu332f88402007-11-15 22:36:07 +080050#define CRYPTO_ALG_TYPE_ABLKCIPHER 0x00000005
Herbert Xu4e6c3df2016-07-12 13:17:31 +080051#define CRYPTO_ALG_TYPE_SKCIPHER 0x00000005
Herbert Xu61da88e2007-12-17 21:51:27 +080052#define CRYPTO_ALG_TYPE_GIVCIPHER 0x00000006
Salvatore Benedetto4e5f2c42016-06-22 17:49:13 +010053#define CRYPTO_ALG_TYPE_KPP 0x00000008
Neil Horman17f0f4a2008-08-14 22:15:52 +100054#define CRYPTO_ALG_TYPE_RNG 0x0000000c
Tadeusz Struk3c339ab2015-06-16 10:30:55 -070055#define CRYPTO_ALG_TYPE_AKCIPHER 0x0000000d
Giovanni Cabiddu63044c42016-06-02 13:28:55 +010056#define CRYPTO_ALG_TYPE_DIGEST 0x0000000e
57#define CRYPTO_ALG_TYPE_HASH 0x0000000e
58#define CRYPTO_ALG_TYPE_SHASH 0x0000000e
59#define CRYPTO_ALG_TYPE_AHASH 0x0000000f
Herbert Xu055bcee2006-08-19 22:24:23 +100060
61#define CRYPTO_ALG_TYPE_HASH_MASK 0x0000000e
Giovanni Cabiddu63044c42016-06-02 13:28:55 +010062#define CRYPTO_ALG_TYPE_AHASH_MASK 0x0000000e
Herbert Xu332f88402007-11-15 22:36:07 +080063#define CRYPTO_ALG_TYPE_BLKCIPHER_MASK 0x0000000c
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Herbert Xu28259822006-08-06 21:23:26 +100065#define CRYPTO_ALG_LARVAL 0x00000010
Herbert Xu6bfd4802006-09-21 11:39:29 +100066#define CRYPTO_ALG_DEAD 0x00000020
67#define CRYPTO_ALG_DYING 0x00000040
Herbert Xuf3f632d2006-08-06 23:12:59 +100068#define CRYPTO_ALG_ASYNC 0x00000080
Herbert Xu28259822006-08-06 21:23:26 +100069
Linus Torvalds1da177e2005-04-16 15:20:36 -070070/*
Herbert Xu60104392006-08-26 18:34:10 +100071 * Set this bit if and only if the algorithm requires another algorithm of
72 * the same type to handle corner cases.
73 */
74#define CRYPTO_ALG_NEED_FALLBACK 0x00000100
75
76/*
Herbert Xuecfc4322007-12-05 21:08:36 +110077 * This bit is set for symmetric key ciphers that have already been wrapped
78 * with a generic IV generator to prevent them from being wrapped again.
79 */
80#define CRYPTO_ALG_GENIV 0x00000200
81
82/*
Herbert Xu73d38642008-08-03 21:15:23 +080083 * Set if the algorithm has passed automated run-time testing. Note that
84 * if there is no run-time testing for a given algorithm it is considered
85 * to have passed.
86 */
87
88#define CRYPTO_ALG_TESTED 0x00000400
89
90/*
Steffen Klassert64a947b2011-09-27 07:21:26 +020091 * Set if the algorithm is an instance that is build from templates.
92 */
93#define CRYPTO_ALG_INSTANCE 0x00000800
94
Nikos Mavrogiannopoulosd912bb72011-11-01 13:39:56 +010095/* Set this bit if the algorithm provided is hardware accelerated but
96 * not available to userspace via instruction set or so.
97 */
98#define CRYPTO_ALG_KERN_DRIVER_ONLY 0x00001000
99
Steffen Klassert64a947b2011-09-27 07:21:26 +0200100/*
Stephan Mueller06ca7f62015-03-30 21:55:52 +0200101 * Mark a cipher as a service implementation only usable by another
102 * cipher and never by a normal user of the kernel crypto API
103 */
104#define CRYPTO_ALG_INTERNAL 0x00002000
105
106/*
Eric Biggersb392a532018-01-03 11:16:26 -0800107 * Set if the algorithm has a ->setkey() method but can be used without
108 * calling it first, i.e. there is a default key.
109 */
110#define CRYPTO_ALG_OPTIONAL_KEY 0x00004000
111
112/*
Matthew Garretteddbab12018-06-08 14:57:42 -0700113 * Don't trigger module loading
114 */
115#define CRYPTO_NOLOAD 0x00008000
116
117/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 * Transform masks and values (for crt_flags).
119 */
Eric Biggersadf26e82018-01-03 11:16:27 -0800120#define CRYPTO_TFM_NEED_KEY 0x00000001
121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122#define CRYPTO_TFM_REQ_MASK 0x000fff00
123#define CRYPTO_TFM_RES_MASK 0xfff00000
124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125#define CRYPTO_TFM_REQ_WEAK_KEY 0x00000100
Herbert Xu64baf3c2005-09-01 17:43:05 -0700126#define CRYPTO_TFM_REQ_MAY_SLEEP 0x00000200
Herbert Xu32e39832007-03-24 14:35:34 +1100127#define CRYPTO_TFM_REQ_MAY_BACKLOG 0x00000400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128#define CRYPTO_TFM_RES_WEAK_KEY 0x00100000
129#define CRYPTO_TFM_RES_BAD_KEY_LEN 0x00200000
130#define CRYPTO_TFM_RES_BAD_KEY_SCHED 0x00400000
131#define CRYPTO_TFM_RES_BAD_BLOCK_LEN 0x00800000
132#define CRYPTO_TFM_RES_BAD_FLAGS 0x01000000
133
134/*
135 * Miscellaneous stuff.
136 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137#define CRYPTO_MAX_ALG_NAME 64
138
Herbert Xu79911102006-08-21 21:03:52 +1000139/*
140 * The macro CRYPTO_MINALIGN_ATTR (along with the void * type in the actual
141 * declaration) is used to ensure that the crypto_tfm context structure is
142 * aligned correctly for the given architecture so that there are no alignment
143 * faults for C data types. In particular, this is required on platforms such
144 * as arm where pointers are 32-bit aligned but there are data types such as
145 * u64 which require 64-bit alignment.
146 */
Herbert Xu79911102006-08-21 21:03:52 +1000147#define CRYPTO_MINALIGN ARCH_KMALLOC_MINALIGN
Herbert Xu79911102006-08-21 21:03:52 +1000148
Herbert Xu79911102006-08-21 21:03:52 +1000149#define CRYPTO_MINALIGN_ATTR __attribute__ ((__aligned__(CRYPTO_MINALIGN)))
Herbert Xu79911102006-08-21 21:03:52 +1000150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151struct scatterlist;
Herbert Xu32e39832007-03-24 14:35:34 +1100152struct crypto_ablkcipher;
153struct crypto_async_request;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000154struct crypto_blkcipher;
Herbert Xu40725182005-07-06 13:51:52 -0700155struct crypto_tfm;
Herbert Xue853c3c2006-08-22 00:06:54 +1000156struct crypto_type;
Herbert Xu61da88e2007-12-17 21:51:27 +0800157struct skcipher_givcrypt_request;
Herbert Xu40725182005-07-06 13:51:52 -0700158
Herbert Xu32e39832007-03-24 14:35:34 +1100159typedef void (*crypto_completion_t)(struct crypto_async_request *req, int err);
160
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100161/**
162 * DOC: Block Cipher Context Data Structures
163 *
164 * These data structures define the operating context for each block cipher
165 * type.
166 */
167
Herbert Xu32e39832007-03-24 14:35:34 +1100168struct crypto_async_request {
169 struct list_head list;
170 crypto_completion_t complete;
171 void *data;
172 struct crypto_tfm *tfm;
173
174 u32 flags;
175};
176
177struct ablkcipher_request {
178 struct crypto_async_request base;
179
180 unsigned int nbytes;
181
182 void *info;
183
184 struct scatterlist *src;
185 struct scatterlist *dst;
186
187 void *__ctx[] CRYPTO_MINALIGN_ATTR;
188};
189
Herbert Xu5cde0af2006-08-22 00:07:53 +1000190struct blkcipher_desc {
191 struct crypto_blkcipher *tfm;
192 void *info;
193 u32 flags;
194};
195
Herbert Xu40725182005-07-06 13:51:52 -0700196struct cipher_desc {
197 struct crypto_tfm *tfm;
Herbert Xu6c2bb982006-05-16 22:09:29 +1000198 void (*crfn)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Herbert Xu40725182005-07-06 13:51:52 -0700199 unsigned int (*prfn)(const struct cipher_desc *desc, u8 *dst,
200 const u8 *src, unsigned int nbytes);
201 void *info;
202};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100204/**
205 * DOC: Block Cipher Algorithm Definitions
206 *
207 * These data structures define modular crypto algorithm implementations,
208 * managed via crypto_register_alg() and crypto_unregister_alg().
209 */
210
211/**
212 * struct ablkcipher_alg - asynchronous block cipher definition
213 * @min_keysize: Minimum key size supported by the transformation. This is the
214 * smallest key length supported by this transformation algorithm.
215 * This must be set to one of the pre-defined values as this is
216 * not hardware specific. Possible values for this field can be
217 * found via git grep "_MIN_KEY_SIZE" include/crypto/
218 * @max_keysize: Maximum key size supported by the transformation. This is the
219 * largest key length supported by this transformation algorithm.
220 * This must be set to one of the pre-defined values as this is
221 * not hardware specific. Possible values for this field can be
222 * found via git grep "_MAX_KEY_SIZE" include/crypto/
223 * @setkey: Set key for the transformation. This function is used to either
224 * program a supplied key into the hardware or store the key in the
225 * transformation context for programming it later. Note that this
226 * function does modify the transformation context. This function can
227 * be called multiple times during the existence of the transformation
228 * object, so one must make sure the key is properly reprogrammed into
229 * the hardware. This function is also responsible for checking the key
230 * length for validity. In case a software fallback was put in place in
231 * the @cra_init call, this function might need to use the fallback if
232 * the algorithm doesn't support all of the key sizes.
233 * @encrypt: Encrypt a scatterlist of blocks. This function is used to encrypt
234 * the supplied scatterlist containing the blocks of data. The crypto
235 * API consumer is responsible for aligning the entries of the
236 * scatterlist properly and making sure the chunks are correctly
237 * sized. In case a software fallback was put in place in the
238 * @cra_init call, this function might need to use the fallback if
239 * the algorithm doesn't support all of the key sizes. In case the
240 * key was stored in transformation context, the key might need to be
241 * re-programmed into the hardware in this function. This function
242 * shall not modify the transformation context, as this function may
243 * be called in parallel with the same transformation object.
244 * @decrypt: Decrypt a single block. This is a reverse counterpart to @encrypt
245 * and the conditions are exactly the same.
246 * @givencrypt: Update the IV for encryption. With this function, a cipher
247 * implementation may provide the function on how to update the IV
248 * for encryption.
249 * @givdecrypt: Update the IV for decryption. This is the reverse of
250 * @givencrypt .
251 * @geniv: The transformation implementation may use an "IV generator" provided
252 * by the kernel crypto API. Several use cases have a predefined
253 * approach how IVs are to be updated. For such use cases, the kernel
254 * crypto API provides ready-to-use implementations that can be
255 * referenced with this variable.
256 * @ivsize: IV size applicable for transformation. The consumer must provide an
257 * IV of exactly that size to perform the encrypt or decrypt operation.
258 *
259 * All fields except @givencrypt , @givdecrypt , @geniv and @ivsize are
260 * mandatory and must be filled.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 */
Herbert Xub5b7f082007-04-16 20:48:54 +1000262struct ablkcipher_alg {
263 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
264 unsigned int keylen);
265 int (*encrypt)(struct ablkcipher_request *req);
266 int (*decrypt)(struct ablkcipher_request *req);
Herbert Xu61da88e2007-12-17 21:51:27 +0800267 int (*givencrypt)(struct skcipher_givcrypt_request *req);
268 int (*givdecrypt)(struct skcipher_givcrypt_request *req);
Herbert Xub5b7f082007-04-16 20:48:54 +1000269
Herbert Xu23508e12007-11-27 21:33:24 +0800270 const char *geniv;
271
Herbert Xub5b7f082007-04-16 20:48:54 +1000272 unsigned int min_keysize;
273 unsigned int max_keysize;
274 unsigned int ivsize;
275};
276
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100277/**
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100278 * struct blkcipher_alg - synchronous block cipher definition
279 * @min_keysize: see struct ablkcipher_alg
280 * @max_keysize: see struct ablkcipher_alg
281 * @setkey: see struct ablkcipher_alg
282 * @encrypt: see struct ablkcipher_alg
283 * @decrypt: see struct ablkcipher_alg
284 * @geniv: see struct ablkcipher_alg
285 * @ivsize: see struct ablkcipher_alg
286 *
287 * All fields except @geniv and @ivsize are mandatory and must be filled.
288 */
Herbert Xu5cde0af2006-08-22 00:07:53 +1000289struct blkcipher_alg {
290 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
291 unsigned int keylen);
292 int (*encrypt)(struct blkcipher_desc *desc,
293 struct scatterlist *dst, struct scatterlist *src,
294 unsigned int nbytes);
295 int (*decrypt)(struct blkcipher_desc *desc,
296 struct scatterlist *dst, struct scatterlist *src,
297 unsigned int nbytes);
298
Herbert Xu23508e12007-11-27 21:33:24 +0800299 const char *geniv;
300
Herbert Xu5cde0af2006-08-22 00:07:53 +1000301 unsigned int min_keysize;
302 unsigned int max_keysize;
303 unsigned int ivsize;
304};
305
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100306/**
307 * struct cipher_alg - single-block symmetric ciphers definition
308 * @cia_min_keysize: Minimum key size supported by the transformation. This is
309 * the smallest key length supported by this transformation
310 * algorithm. This must be set to one of the pre-defined
311 * values as this is not hardware specific. Possible values
312 * for this field can be found via git grep "_MIN_KEY_SIZE"
313 * include/crypto/
314 * @cia_max_keysize: Maximum key size supported by the transformation. This is
315 * the largest key length supported by this transformation
316 * algorithm. This must be set to one of the pre-defined values
317 * as this is not hardware specific. Possible values for this
318 * field can be found via git grep "_MAX_KEY_SIZE"
319 * include/crypto/
320 * @cia_setkey: Set key for the transformation. This function is used to either
321 * program a supplied key into the hardware or store the key in the
322 * transformation context for programming it later. Note that this
323 * function does modify the transformation context. This function
324 * can be called multiple times during the existence of the
325 * transformation object, so one must make sure the key is properly
326 * reprogrammed into the hardware. This function is also
327 * responsible for checking the key length for validity.
328 * @cia_encrypt: Encrypt a single block. This function is used to encrypt a
329 * single block of data, which must be @cra_blocksize big. This
330 * always operates on a full @cra_blocksize and it is not possible
331 * to encrypt a block of smaller size. The supplied buffers must
332 * therefore also be at least of @cra_blocksize size. Both the
333 * input and output buffers are always aligned to @cra_alignmask.
334 * In case either of the input or output buffer supplied by user
335 * of the crypto API is not aligned to @cra_alignmask, the crypto
336 * API will re-align the buffers. The re-alignment means that a
337 * new buffer will be allocated, the data will be copied into the
338 * new buffer, then the processing will happen on the new buffer,
339 * then the data will be copied back into the original buffer and
340 * finally the new buffer will be freed. In case a software
341 * fallback was put in place in the @cra_init call, this function
342 * might need to use the fallback if the algorithm doesn't support
343 * all of the key sizes. In case the key was stored in
344 * transformation context, the key might need to be re-programmed
345 * into the hardware in this function. This function shall not
346 * modify the transformation context, as this function may be
347 * called in parallel with the same transformation object.
348 * @cia_decrypt: Decrypt a single block. This is a reverse counterpart to
349 * @cia_encrypt, and the conditions are exactly the same.
350 *
351 * All fields are mandatory and must be filled.
352 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353struct cipher_alg {
354 unsigned int cia_min_keysize;
355 unsigned int cia_max_keysize;
Herbert Xu6c2bb982006-05-16 22:09:29 +1000356 int (*cia_setkey)(struct crypto_tfm *tfm, const u8 *key,
Herbert Xu560c06a2006-08-13 14:16:39 +1000357 unsigned int keylen);
Herbert Xu6c2bb982006-05-16 22:09:29 +1000358 void (*cia_encrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
359 void (*cia_decrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360};
361
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362struct compress_alg {
Herbert Xu6c2bb982006-05-16 22:09:29 +1000363 int (*coa_compress)(struct crypto_tfm *tfm, const u8 *src,
364 unsigned int slen, u8 *dst, unsigned int *dlen);
365 int (*coa_decompress)(struct crypto_tfm *tfm, const u8 *src,
366 unsigned int slen, u8 *dst, unsigned int *dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367};
368
Neil Horman17f0f4a2008-08-14 22:15:52 +1000369
Herbert Xub5b7f082007-04-16 20:48:54 +1000370#define cra_ablkcipher cra_u.ablkcipher
Herbert Xu5cde0af2006-08-22 00:07:53 +1000371#define cra_blkcipher cra_u.blkcipher
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372#define cra_cipher cra_u.cipher
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373#define cra_compress cra_u.compress
374
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100375/**
376 * struct crypto_alg - definition of a cryptograpic cipher algorithm
377 * @cra_flags: Flags describing this transformation. See include/linux/crypto.h
378 * CRYPTO_ALG_* flags for the flags which go in here. Those are
379 * used for fine-tuning the description of the transformation
380 * algorithm.
381 * @cra_blocksize: Minimum block size of this transformation. The size in bytes
382 * of the smallest possible unit which can be transformed with
383 * this algorithm. The users must respect this value.
384 * In case of HASH transformation, it is possible for a smaller
385 * block than @cra_blocksize to be passed to the crypto API for
386 * transformation, in case of any other transformation type, an
387 * error will be returned upon any attempt to transform smaller
388 * than @cra_blocksize chunks.
389 * @cra_ctxsize: Size of the operational context of the transformation. This
390 * value informs the kernel crypto API about the memory size
391 * needed to be allocated for the transformation context.
392 * @cra_alignmask: Alignment mask for the input and output data buffer. The data
393 * buffer containing the input data for the algorithm must be
394 * aligned to this alignment mask. The data buffer for the
395 * output data must be aligned to this alignment mask. Note that
396 * the Crypto API will do the re-alignment in software, but
397 * only under special conditions and there is a performance hit.
398 * The re-alignment happens at these occasions for different
399 * @cra_u types: cipher -- For both input data and output data
400 * buffer; ahash -- For output hash destination buf; shash --
401 * For output hash destination buf.
402 * This is needed on hardware which is flawed by design and
403 * cannot pick data from arbitrary addresses.
404 * @cra_priority: Priority of this transformation implementation. In case
405 * multiple transformations with same @cra_name are available to
406 * the Crypto API, the kernel will use the one with highest
407 * @cra_priority.
408 * @cra_name: Generic name (usable by multiple implementations) of the
409 * transformation algorithm. This is the name of the transformation
410 * itself. This field is used by the kernel when looking up the
411 * providers of particular transformation.
412 * @cra_driver_name: Unique name of the transformation provider. This is the
413 * name of the provider of the transformation. This can be any
414 * arbitrary value, but in the usual case, this contains the
415 * name of the chip or provider and the name of the
416 * transformation algorithm.
417 * @cra_type: Type of the cryptographic transformation. This is a pointer to
418 * struct crypto_type, which implements callbacks common for all
Masanari Iida12f7c142015-06-04 00:01:21 +0900419 * transformation types. There are multiple options:
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100420 * &crypto_blkcipher_type, &crypto_ablkcipher_type,
Herbert Xub0d955b2015-08-14 15:30:41 +0800421 * &crypto_ahash_type, &crypto_rng_type.
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100422 * This field might be empty. In that case, there are no common
423 * callbacks. This is the case for: cipher, compress, shash.
424 * @cra_u: Callbacks implementing the transformation. This is a union of
425 * multiple structures. Depending on the type of transformation selected
426 * by @cra_type and @cra_flags above, the associated structure must be
427 * filled with callbacks. This field might be empty. This is the case
428 * for ahash, shash.
429 * @cra_init: Initialize the cryptographic transformation object. This function
430 * is used to initialize the cryptographic transformation object.
431 * This function is called only once at the instantiation time, right
432 * after the transformation context was allocated. In case the
433 * cryptographic hardware has some special requirements which need to
434 * be handled by software, this function shall check for the precise
435 * requirement of the transformation and put any software fallbacks
436 * in place.
437 * @cra_exit: Deinitialize the cryptographic transformation object. This is a
438 * counterpart to @cra_init, used to remove various changes set in
439 * @cra_init.
440 * @cra_module: Owner of this transformation implementation. Set to THIS_MODULE
441 * @cra_list: internally used
442 * @cra_users: internally used
443 * @cra_refcnt: internally used
444 * @cra_destroy: internally used
445 *
446 * The struct crypto_alg describes a generic Crypto API algorithm and is common
447 * for all of the transformations. Any variable not documented here shall not
448 * be used by a cipher implementation as it is internal to the Crypto API.
449 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450struct crypto_alg {
451 struct list_head cra_list;
Herbert Xu6bfd4802006-09-21 11:39:29 +1000452 struct list_head cra_users;
453
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 u32 cra_flags;
455 unsigned int cra_blocksize;
456 unsigned int cra_ctxsize;
Herbert Xu95477372005-07-06 13:52:09 -0700457 unsigned int cra_alignmask;
Herbert Xu5cb14542005-11-05 16:58:14 +1100458
459 int cra_priority;
Herbert Xu6521f302006-08-06 20:28:44 +1000460 atomic_t cra_refcnt;
Herbert Xu5cb14542005-11-05 16:58:14 +1100461
Herbert Xud913ea02006-05-21 08:45:26 +1000462 char cra_name[CRYPTO_MAX_ALG_NAME];
463 char cra_driver_name[CRYPTO_MAX_ALG_NAME];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
Herbert Xue853c3c2006-08-22 00:06:54 +1000465 const struct crypto_type *cra_type;
466
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 union {
Herbert Xub5b7f082007-04-16 20:48:54 +1000468 struct ablkcipher_alg ablkcipher;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000469 struct blkcipher_alg blkcipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 struct cipher_alg cipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 struct compress_alg compress;
472 } cra_u;
Herbert Xuc7fc0592006-05-24 13:02:26 +1000473
474 int (*cra_init)(struct crypto_tfm *tfm);
475 void (*cra_exit)(struct crypto_tfm *tfm);
Herbert Xu6521f302006-08-06 20:28:44 +1000476 void (*cra_destroy)(struct crypto_alg *alg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
478 struct module *cra_module;
Herbert Xuedf18b92015-06-18 14:00:48 +0800479} CRYPTO_MINALIGN_ATTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
481/*
Jaegeuk Kim8dec0742017-06-22 12:14:40 -0700482 * A helper struct for waiting for completion of async crypto ops
483 */
484struct crypto_wait {
485 struct completion completion;
486 int err;
487};
488
489/*
490 * Macro for declaring a crypto op async wait object on stack
491 */
492#define DECLARE_CRYPTO_WAIT(_wait) \
493 struct crypto_wait _wait = { \
494 COMPLETION_INITIALIZER_ONSTACK((_wait).completion), 0 }
495
496/*
497 * Async ops completion helper functioons
498 */
499void crypto_req_done(struct crypto_async_request *req, int err);
500
501static inline int crypto_wait_req(int err, struct crypto_wait *wait)
502{
503 switch (err) {
504 case -EINPROGRESS:
505 case -EBUSY:
506 wait_for_completion(&wait->completion);
507 reinit_completion(&wait->completion);
508 err = wait->err;
509 break;
510 };
511
512 return err;
513}
514
515static inline void crypto_init_wait(struct crypto_wait *wait)
516{
517 init_completion(&wait->completion);
518}
519
520/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 * Algorithm registration interface.
522 */
523int crypto_register_alg(struct crypto_alg *alg);
524int crypto_unregister_alg(struct crypto_alg *alg);
Mark Brown4b004342012-01-17 23:34:26 +0000525int crypto_register_algs(struct crypto_alg *algs, int count);
526int crypto_unregister_algs(struct crypto_alg *algs, int count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
528/*
529 * Algorithm query interface.
530 */
Herbert Xufce32d72006-08-26 17:35:45 +1000531int crypto_has_alg(const char *name, u32 type, u32 mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
533/*
534 * Transforms: user-instantiated objects which encapsulate algorithms
Herbert Xu6d7d6842006-07-30 11:53:01 +1000535 * and core processing logic. Managed via crypto_alloc_*() and
536 * crypto_free_*(), as well as the various helpers below.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
Herbert Xu32e39832007-03-24 14:35:34 +1100539struct ablkcipher_tfm {
540 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
541 unsigned int keylen);
542 int (*encrypt)(struct ablkcipher_request *req);
543 int (*decrypt)(struct ablkcipher_request *req);
Herbert Xu61da88e2007-12-17 21:51:27 +0800544
Herbert Xuecfc4322007-12-05 21:08:36 +1100545 struct crypto_ablkcipher *base;
546
Herbert Xu32e39832007-03-24 14:35:34 +1100547 unsigned int ivsize;
548 unsigned int reqsize;
549};
550
Herbert Xu5cde0af2006-08-22 00:07:53 +1000551struct blkcipher_tfm {
552 void *iv;
553 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
554 unsigned int keylen);
555 int (*encrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
556 struct scatterlist *src, unsigned int nbytes);
557 int (*decrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
558 struct scatterlist *src, unsigned int nbytes);
559};
560
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561struct cipher_tfm {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 int (*cit_setkey)(struct crypto_tfm *tfm,
563 const u8 *key, unsigned int keylen);
Herbert Xuf28776a2006-08-13 20:58:18 +1000564 void (*cit_encrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
565 void (*cit_decrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566};
567
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568struct compress_tfm {
569 int (*cot_compress)(struct crypto_tfm *tfm,
570 const u8 *src, unsigned int slen,
571 u8 *dst, unsigned int *dlen);
572 int (*cot_decompress)(struct crypto_tfm *tfm,
573 const u8 *src, unsigned int slen,
574 u8 *dst, unsigned int *dlen);
575};
576
Herbert Xu32e39832007-03-24 14:35:34 +1100577#define crt_ablkcipher crt_u.ablkcipher
Herbert Xu5cde0af2006-08-22 00:07:53 +1000578#define crt_blkcipher crt_u.blkcipher
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579#define crt_cipher crt_u.cipher
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580#define crt_compress crt_u.compress
581
582struct crypto_tfm {
583
584 u32 crt_flags;
585
586 union {
Herbert Xu32e39832007-03-24 14:35:34 +1100587 struct ablkcipher_tfm ablkcipher;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000588 struct blkcipher_tfm blkcipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 struct cipher_tfm cipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 struct compress_tfm compress;
591 } crt_u;
Herbert Xu4a779482008-09-13 18:19:03 -0700592
593 void (*exit)(struct crypto_tfm *tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
595 struct crypto_alg *__crt_alg;
Herbert Xuf10b7892006-01-25 22:34:01 +1100596
Herbert Xu79911102006-08-21 21:03:52 +1000597 void *__crt_ctx[] CRYPTO_MINALIGN_ATTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598};
599
Herbert Xu32e39832007-03-24 14:35:34 +1100600struct crypto_ablkcipher {
601 struct crypto_tfm base;
602};
603
Herbert Xu5cde0af2006-08-22 00:07:53 +1000604struct crypto_blkcipher {
605 struct crypto_tfm base;
606};
607
Herbert Xu78a1fe42006-12-24 10:02:00 +1100608struct crypto_cipher {
609 struct crypto_tfm base;
610};
611
612struct crypto_comp {
613 struct crypto_tfm base;
614};
615
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000616enum {
617 CRYPTOA_UNSPEC,
618 CRYPTOA_ALG,
Herbert Xuebc610e2007-01-01 18:37:02 +1100619 CRYPTOA_TYPE,
Herbert Xu39e1ee012007-08-29 19:27:26 +0800620 CRYPTOA_U32,
Herbert Xuebc610e2007-01-01 18:37:02 +1100621 __CRYPTOA_MAX,
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000622};
623
Herbert Xuebc610e2007-01-01 18:37:02 +1100624#define CRYPTOA_MAX (__CRYPTOA_MAX - 1)
625
Herbert Xu39e1ee012007-08-29 19:27:26 +0800626/* Maximum number of (rtattr) parameters for each template. */
627#define CRYPTO_MAX_ATTRS 32
628
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000629struct crypto_attr_alg {
630 char name[CRYPTO_MAX_ALG_NAME];
631};
632
Herbert Xuebc610e2007-01-01 18:37:02 +1100633struct crypto_attr_type {
634 u32 type;
635 u32 mask;
636};
637
Herbert Xu39e1ee012007-08-29 19:27:26 +0800638struct crypto_attr_u32 {
639 u32 num;
640};
641
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642/*
643 * Transform user interface.
644 */
645
Herbert Xu6d7d6842006-07-30 11:53:01 +1000646struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask);
Herbert Xu7b2cd922009-02-05 16:48:24 +1100647void crypto_destroy_tfm(void *mem, struct crypto_tfm *tfm);
648
649static inline void crypto_free_tfm(struct crypto_tfm *tfm)
650{
651 return crypto_destroy_tfm(tfm, tfm);
652}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
Herbert Xuda7f0332008-07-31 17:08:25 +0800654int alg_test(const char *driver, const char *alg, u32 type, u32 mask);
655
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656/*
657 * Transform helpers which query the underlying algorithm.
658 */
659static inline const char *crypto_tfm_alg_name(struct crypto_tfm *tfm)
660{
661 return tfm->__crt_alg->cra_name;
662}
663
Michal Ludvigb14cdd62006-07-09 09:02:24 +1000664static inline const char *crypto_tfm_alg_driver_name(struct crypto_tfm *tfm)
665{
666 return tfm->__crt_alg->cra_driver_name;
667}
668
669static inline int crypto_tfm_alg_priority(struct crypto_tfm *tfm)
670{
671 return tfm->__crt_alg->cra_priority;
672}
673
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm)
675{
676 return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK;
677}
678
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm)
680{
681 return tfm->__crt_alg->cra_blocksize;
682}
683
Herbert Xufbdae9f2005-07-06 13:53:29 -0700684static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm *tfm)
685{
686 return tfm->__crt_alg->cra_alignmask;
687}
688
Herbert Xuf28776a2006-08-13 20:58:18 +1000689static inline u32 crypto_tfm_get_flags(struct crypto_tfm *tfm)
690{
691 return tfm->crt_flags;
692}
693
694static inline void crypto_tfm_set_flags(struct crypto_tfm *tfm, u32 flags)
695{
696 tfm->crt_flags |= flags;
697}
698
699static inline void crypto_tfm_clear_flags(struct crypto_tfm *tfm, u32 flags)
700{
701 tfm->crt_flags &= ~flags;
702}
703
Herbert Xu40725182005-07-06 13:51:52 -0700704static inline void *crypto_tfm_ctx(struct crypto_tfm *tfm)
705{
Herbert Xuf10b7892006-01-25 22:34:01 +1100706 return tfm->__crt_ctx;
707}
708
709static inline unsigned int crypto_tfm_ctx_alignment(void)
710{
711 struct crypto_tfm *tfm;
712 return __alignof__(tfm->__crt_ctx);
Herbert Xu40725182005-07-06 13:51:52 -0700713}
714
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715/*
716 * API wrappers.
717 */
Herbert Xu32e39832007-03-24 14:35:34 +1100718static inline struct crypto_ablkcipher *__crypto_ablkcipher_cast(
719 struct crypto_tfm *tfm)
720{
721 return (struct crypto_ablkcipher *)tfm;
722}
723
Herbert Xu378f4f52007-12-17 20:07:31 +0800724static inline u32 crypto_skcipher_type(u32 type)
725{
Herbert Xuecfc4322007-12-05 21:08:36 +1100726 type &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
Herbert Xu378f4f52007-12-17 20:07:31 +0800727 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
728 return type;
729}
730
731static inline u32 crypto_skcipher_mask(u32 mask)
732{
Herbert Xuecfc4322007-12-05 21:08:36 +1100733 mask &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
Herbert Xu378f4f52007-12-17 20:07:31 +0800734 mask |= CRYPTO_ALG_TYPE_BLKCIPHER_MASK;
735 return mask;
736}
737
Stephan Muellerf13ec332014-11-12 05:28:22 +0100738/**
739 * DOC: Asynchronous Block Cipher API
740 *
741 * Asynchronous block cipher API is used with the ciphers of type
742 * CRYPTO_ALG_TYPE_ABLKCIPHER (listed as type "ablkcipher" in /proc/crypto).
743 *
744 * Asynchronous cipher operations imply that the function invocation for a
745 * cipher request returns immediately before the completion of the operation.
746 * The cipher request is scheduled as a separate kernel thread and therefore
747 * load-balanced on the different CPUs via the process scheduler. To allow
748 * the kernel crypto API to inform the caller about the completion of a cipher
749 * request, the caller must provide a callback function. That function is
750 * invoked with the cipher handle when the request completes.
751 *
752 * To support the asynchronous operation, additional information than just the
753 * cipher handle must be supplied to the kernel crypto API. That additional
754 * information is given by filling in the ablkcipher_request data structure.
755 *
756 * For the asynchronous block cipher API, the state is maintained with the tfm
757 * cipher handle. A single tfm can be used across multiple calls and in
758 * parallel. For asynchronous block cipher calls, context data supplied and
759 * only used by the caller can be referenced the request data structure in
760 * addition to the IV used for the cipher request. The maintenance of such
761 * state information would be important for a crypto driver implementer to
762 * have, because when calling the callback function upon completion of the
763 * cipher operation, that callback function may need some information about
764 * which operation just finished if it invoked multiple in parallel. This
765 * state information is unused by the kernel crypto API.
766 */
767
Herbert Xu32e39832007-03-24 14:35:34 +1100768static inline struct crypto_tfm *crypto_ablkcipher_tfm(
769 struct crypto_ablkcipher *tfm)
770{
771 return &tfm->base;
772}
773
Stephan Muellerf13ec332014-11-12 05:28:22 +0100774/**
775 * crypto_free_ablkcipher() - zeroize and free cipher handle
776 * @tfm: cipher handle to be freed
777 */
Herbert Xu32e39832007-03-24 14:35:34 +1100778static inline void crypto_free_ablkcipher(struct crypto_ablkcipher *tfm)
779{
780 crypto_free_tfm(crypto_ablkcipher_tfm(tfm));
781}
782
Stephan Muellerf13ec332014-11-12 05:28:22 +0100783/**
784 * crypto_has_ablkcipher() - Search for the availability of an ablkcipher.
785 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
786 * ablkcipher
787 * @type: specifies the type of the cipher
788 * @mask: specifies the mask for the cipher
789 *
790 * Return: true when the ablkcipher is known to the kernel crypto API; false
791 * otherwise
792 */
Herbert Xu32e39832007-03-24 14:35:34 +1100793static inline int crypto_has_ablkcipher(const char *alg_name, u32 type,
794 u32 mask)
795{
Herbert Xu378f4f52007-12-17 20:07:31 +0800796 return crypto_has_alg(alg_name, crypto_skcipher_type(type),
797 crypto_skcipher_mask(mask));
Herbert Xu32e39832007-03-24 14:35:34 +1100798}
799
800static inline struct ablkcipher_tfm *crypto_ablkcipher_crt(
801 struct crypto_ablkcipher *tfm)
802{
803 return &crypto_ablkcipher_tfm(tfm)->crt_ablkcipher;
804}
805
Stephan Muellerf13ec332014-11-12 05:28:22 +0100806/**
807 * crypto_ablkcipher_ivsize() - obtain IV size
808 * @tfm: cipher handle
809 *
810 * The size of the IV for the ablkcipher referenced by the cipher handle is
811 * returned. This IV size may be zero if the cipher does not need an IV.
812 *
813 * Return: IV size in bytes
814 */
Herbert Xu32e39832007-03-24 14:35:34 +1100815static inline unsigned int crypto_ablkcipher_ivsize(
816 struct crypto_ablkcipher *tfm)
817{
818 return crypto_ablkcipher_crt(tfm)->ivsize;
819}
820
Stephan Muellerf13ec332014-11-12 05:28:22 +0100821/**
822 * crypto_ablkcipher_blocksize() - obtain block size of cipher
823 * @tfm: cipher handle
824 *
825 * The block size for the ablkcipher referenced with the cipher handle is
826 * returned. The caller may use that information to allocate appropriate
827 * memory for the data returned by the encryption or decryption operation
828 *
829 * Return: block size of cipher
830 */
Herbert Xu32e39832007-03-24 14:35:34 +1100831static inline unsigned int crypto_ablkcipher_blocksize(
832 struct crypto_ablkcipher *tfm)
833{
834 return crypto_tfm_alg_blocksize(crypto_ablkcipher_tfm(tfm));
835}
836
837static inline unsigned int crypto_ablkcipher_alignmask(
838 struct crypto_ablkcipher *tfm)
839{
840 return crypto_tfm_alg_alignmask(crypto_ablkcipher_tfm(tfm));
841}
842
843static inline u32 crypto_ablkcipher_get_flags(struct crypto_ablkcipher *tfm)
844{
845 return crypto_tfm_get_flags(crypto_ablkcipher_tfm(tfm));
846}
847
848static inline void crypto_ablkcipher_set_flags(struct crypto_ablkcipher *tfm,
849 u32 flags)
850{
851 crypto_tfm_set_flags(crypto_ablkcipher_tfm(tfm), flags);
852}
853
854static inline void crypto_ablkcipher_clear_flags(struct crypto_ablkcipher *tfm,
855 u32 flags)
856{
857 crypto_tfm_clear_flags(crypto_ablkcipher_tfm(tfm), flags);
858}
859
Stephan Muellerf13ec332014-11-12 05:28:22 +0100860/**
861 * crypto_ablkcipher_setkey() - set key for cipher
862 * @tfm: cipher handle
863 * @key: buffer holding the key
864 * @keylen: length of the key in bytes
865 *
866 * The caller provided key is set for the ablkcipher referenced by the cipher
867 * handle.
868 *
869 * Note, the key length determines the cipher type. Many block ciphers implement
870 * different cipher modes depending on the key size, such as AES-128 vs AES-192
871 * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
872 * is performed.
873 *
874 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
875 */
Herbert Xu32e39832007-03-24 14:35:34 +1100876static inline int crypto_ablkcipher_setkey(struct crypto_ablkcipher *tfm,
877 const u8 *key, unsigned int keylen)
878{
Herbert Xuecfc4322007-12-05 21:08:36 +1100879 struct ablkcipher_tfm *crt = crypto_ablkcipher_crt(tfm);
880
881 return crt->setkey(crt->base, key, keylen);
Herbert Xu32e39832007-03-24 14:35:34 +1100882}
883
Stephan Muellerf13ec332014-11-12 05:28:22 +0100884/**
885 * crypto_ablkcipher_reqtfm() - obtain cipher handle from request
886 * @req: ablkcipher_request out of which the cipher handle is to be obtained
887 *
888 * Return the crypto_ablkcipher handle when furnishing an ablkcipher_request
889 * data structure.
890 *
891 * Return: crypto_ablkcipher handle
892 */
Herbert Xu32e39832007-03-24 14:35:34 +1100893static inline struct crypto_ablkcipher *crypto_ablkcipher_reqtfm(
894 struct ablkcipher_request *req)
895{
896 return __crypto_ablkcipher_cast(req->base.tfm);
897}
898
Stephan Muellerf13ec332014-11-12 05:28:22 +0100899/**
900 * crypto_ablkcipher_encrypt() - encrypt plaintext
901 * @req: reference to the ablkcipher_request handle that holds all information
902 * needed to perform the cipher operation
903 *
904 * Encrypt plaintext data using the ablkcipher_request handle. That data
905 * structure and how it is filled with data is discussed with the
906 * ablkcipher_request_* functions.
907 *
908 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
909 */
Herbert Xu32e39832007-03-24 14:35:34 +1100910static inline int crypto_ablkcipher_encrypt(struct ablkcipher_request *req)
911{
912 struct ablkcipher_tfm *crt =
913 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
914 return crt->encrypt(req);
915}
916
Stephan Muellerf13ec332014-11-12 05:28:22 +0100917/**
918 * crypto_ablkcipher_decrypt() - decrypt ciphertext
919 * @req: reference to the ablkcipher_request handle that holds all information
920 * needed to perform the cipher operation
921 *
922 * Decrypt ciphertext data using the ablkcipher_request handle. That data
923 * structure and how it is filled with data is discussed with the
924 * ablkcipher_request_* functions.
925 *
926 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
927 */
Herbert Xu32e39832007-03-24 14:35:34 +1100928static inline int crypto_ablkcipher_decrypt(struct ablkcipher_request *req)
929{
930 struct ablkcipher_tfm *crt =
931 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
932 return crt->decrypt(req);
933}
934
Stephan Muellerf13ec332014-11-12 05:28:22 +0100935/**
936 * DOC: Asynchronous Cipher Request Handle
937 *
938 * The ablkcipher_request data structure contains all pointers to data
939 * required for the asynchronous cipher operation. This includes the cipher
940 * handle (which can be used by multiple ablkcipher_request instances), pointer
941 * to plaintext and ciphertext, asynchronous callback function, etc. It acts
942 * as a handle to the ablkcipher_request_* API calls in a similar way as
943 * ablkcipher handle to the crypto_ablkcipher_* API calls.
944 */
945
946/**
947 * crypto_ablkcipher_reqsize() - obtain size of the request data structure
948 * @tfm: cipher handle
949 *
950 * Return: number of bytes
951 */
Herbert Xub16c3a22007-08-29 19:02:04 +0800952static inline unsigned int crypto_ablkcipher_reqsize(
953 struct crypto_ablkcipher *tfm)
Herbert Xu32e39832007-03-24 14:35:34 +1100954{
955 return crypto_ablkcipher_crt(tfm)->reqsize;
956}
957
Stephan Muellerf13ec332014-11-12 05:28:22 +0100958/**
959 * ablkcipher_request_set_tfm() - update cipher handle reference in request
960 * @req: request handle to be modified
961 * @tfm: cipher handle that shall be added to the request handle
962 *
963 * Allow the caller to replace the existing ablkcipher handle in the request
964 * data structure with a different one.
965 */
Herbert Xue196d622007-04-14 16:09:14 +1000966static inline void ablkcipher_request_set_tfm(
967 struct ablkcipher_request *req, struct crypto_ablkcipher *tfm)
968{
Herbert Xuecfc4322007-12-05 21:08:36 +1100969 req->base.tfm = crypto_ablkcipher_tfm(crypto_ablkcipher_crt(tfm)->base);
Herbert Xue196d622007-04-14 16:09:14 +1000970}
971
Herbert Xub5b7f082007-04-16 20:48:54 +1000972static inline struct ablkcipher_request *ablkcipher_request_cast(
973 struct crypto_async_request *req)
974{
975 return container_of(req, struct ablkcipher_request, base);
976}
977
Stephan Muellerf13ec332014-11-12 05:28:22 +0100978/**
979 * ablkcipher_request_alloc() - allocate request data structure
980 * @tfm: cipher handle to be registered with the request
981 * @gfp: memory allocation flag that is handed to kmalloc by the API call.
982 *
983 * Allocate the request data structure that must be used with the ablkcipher
984 * encrypt and decrypt API calls. During the allocation, the provided ablkcipher
985 * handle is registered in the request data structure.
986 *
Eric Biggers6eae29e2016-04-02 10:54:56 -0500987 * Return: allocated request handle in case of success, or NULL if out of memory
Stephan Muellerf13ec332014-11-12 05:28:22 +0100988 */
Herbert Xu32e39832007-03-24 14:35:34 +1100989static inline struct ablkcipher_request *ablkcipher_request_alloc(
990 struct crypto_ablkcipher *tfm, gfp_t gfp)
991{
992 struct ablkcipher_request *req;
993
994 req = kmalloc(sizeof(struct ablkcipher_request) +
995 crypto_ablkcipher_reqsize(tfm), gfp);
996
997 if (likely(req))
Herbert Xue196d622007-04-14 16:09:14 +1000998 ablkcipher_request_set_tfm(req, tfm);
Herbert Xu32e39832007-03-24 14:35:34 +1100999
1000 return req;
1001}
1002
Stephan Muellerf13ec332014-11-12 05:28:22 +01001003/**
1004 * ablkcipher_request_free() - zeroize and free request data structure
1005 * @req: request data structure cipher handle to be freed
1006 */
Herbert Xu32e39832007-03-24 14:35:34 +11001007static inline void ablkcipher_request_free(struct ablkcipher_request *req)
1008{
Herbert Xuaef73cf2009-07-11 22:22:14 +08001009 kzfree(req);
Herbert Xu32e39832007-03-24 14:35:34 +11001010}
1011
Stephan Muellerf13ec332014-11-12 05:28:22 +01001012/**
1013 * ablkcipher_request_set_callback() - set asynchronous callback function
1014 * @req: request handle
1015 * @flags: specify zero or an ORing of the flags
1016 * CRYPTO_TFM_REQ_MAY_BACKLOG the request queue may back log and
1017 * increase the wait queue beyond the initial maximum size;
1018 * CRYPTO_TFM_REQ_MAY_SLEEP the request processing may sleep
1019 * @compl: callback function pointer to be registered with the request handle
1020 * @data: The data pointer refers to memory that is not used by the kernel
1021 * crypto API, but provided to the callback function for it to use. Here,
1022 * the caller can provide a reference to memory the callback function can
1023 * operate on. As the callback function is invoked asynchronously to the
1024 * related functionality, it may need to access data structures of the
1025 * related functionality which can be referenced using this pointer. The
1026 * callback function can access the memory via the "data" field in the
1027 * crypto_async_request data structure provided to the callback function.
1028 *
1029 * This function allows setting the callback function that is triggered once the
1030 * cipher operation completes.
1031 *
1032 * The callback function is registered with the ablkcipher_request handle and
Stephan Mueller379dcfb2015-01-19 00:13:39 +01001033 * must comply with the following template
Stephan Muellerf13ec332014-11-12 05:28:22 +01001034 *
1035 * void callback_function(struct crypto_async_request *req, int error)
1036 */
Herbert Xu32e39832007-03-24 14:35:34 +11001037static inline void ablkcipher_request_set_callback(
1038 struct ablkcipher_request *req,
Mark Rustad3e3dc252014-07-25 02:53:38 -07001039 u32 flags, crypto_completion_t compl, void *data)
Herbert Xu32e39832007-03-24 14:35:34 +11001040{
Mark Rustad3e3dc252014-07-25 02:53:38 -07001041 req->base.complete = compl;
Herbert Xu32e39832007-03-24 14:35:34 +11001042 req->base.data = data;
1043 req->base.flags = flags;
1044}
1045
Stephan Muellerf13ec332014-11-12 05:28:22 +01001046/**
1047 * ablkcipher_request_set_crypt() - set data buffers
1048 * @req: request handle
1049 * @src: source scatter / gather list
1050 * @dst: destination scatter / gather list
1051 * @nbytes: number of bytes to process from @src
1052 * @iv: IV for the cipher operation which must comply with the IV size defined
1053 * by crypto_ablkcipher_ivsize
1054 *
1055 * This function allows setting of the source data and destination data
1056 * scatter / gather lists.
1057 *
1058 * For encryption, the source is treated as the plaintext and the
1059 * destination is the ciphertext. For a decryption operation, the use is
Stephan Mueller379dcfb2015-01-19 00:13:39 +01001060 * reversed - the source is the ciphertext and the destination is the plaintext.
Stephan Muellerf13ec332014-11-12 05:28:22 +01001061 */
Herbert Xu32e39832007-03-24 14:35:34 +11001062static inline void ablkcipher_request_set_crypt(
1063 struct ablkcipher_request *req,
1064 struct scatterlist *src, struct scatterlist *dst,
1065 unsigned int nbytes, void *iv)
1066{
1067 req->src = src;
1068 req->dst = dst;
1069 req->nbytes = nbytes;
1070 req->info = iv;
1071}
1072
Stephan Muellerfced7b02014-11-12 05:29:00 +01001073/**
Stephan Mueller58284f02014-11-12 05:29:36 +01001074 * DOC: Synchronous Block Cipher API
1075 *
1076 * The synchronous block cipher API is used with the ciphers of type
1077 * CRYPTO_ALG_TYPE_BLKCIPHER (listed as type "blkcipher" in /proc/crypto)
1078 *
1079 * Synchronous calls, have a context in the tfm. But since a single tfm can be
1080 * used in multiple calls and in parallel, this info should not be changeable
1081 * (unless a lock is used). This applies, for example, to the symmetric key.
1082 * However, the IV is changeable, so there is an iv field in blkcipher_tfm
1083 * structure for synchronous blkcipher api. So, its the only state info that can
1084 * be kept for synchronous calls without using a big lock across a tfm.
1085 *
1086 * The block cipher API allows the use of a complete cipher, i.e. a cipher
1087 * consisting of a template (a block chaining mode) and a single block cipher
1088 * primitive (e.g. AES).
1089 *
1090 * The plaintext data buffer and the ciphertext data buffer are pointed to
1091 * by using scatter/gather lists. The cipher operation is performed
1092 * on all segments of the provided scatter/gather lists.
1093 *
1094 * The kernel crypto API supports a cipher operation "in-place" which means that
1095 * the caller may provide the same scatter/gather list for the plaintext and
1096 * cipher text. After the completion of the cipher operation, the plaintext
1097 * data is replaced with the ciphertext data in case of an encryption and vice
1098 * versa for a decryption. The caller must ensure that the scatter/gather lists
1099 * for the output data point to sufficiently large buffers, i.e. multiples of
1100 * the block size of the cipher.
1101 */
1102
Herbert Xu5cde0af2006-08-22 00:07:53 +10001103static inline struct crypto_blkcipher *__crypto_blkcipher_cast(
1104 struct crypto_tfm *tfm)
1105{
1106 return (struct crypto_blkcipher *)tfm;
1107}
1108
1109static inline struct crypto_blkcipher *crypto_blkcipher_cast(
1110 struct crypto_tfm *tfm)
1111{
1112 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_BLKCIPHER);
1113 return __crypto_blkcipher_cast(tfm);
1114}
1115
Stephan Mueller58284f02014-11-12 05:29:36 +01001116/**
1117 * crypto_alloc_blkcipher() - allocate synchronous block cipher handle
1118 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1119 * blkcipher cipher
1120 * @type: specifies the type of the cipher
1121 * @mask: specifies the mask for the cipher
1122 *
1123 * Allocate a cipher handle for a block cipher. The returned struct
1124 * crypto_blkcipher is the cipher handle that is required for any subsequent
1125 * API invocation for that block cipher.
1126 *
1127 * Return: allocated cipher handle in case of success; IS_ERR() is true in case
1128 * of an error, PTR_ERR() returns the error code.
1129 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001130static inline struct crypto_blkcipher *crypto_alloc_blkcipher(
1131 const char *alg_name, u32 type, u32 mask)
1132{
Herbert Xu332f88402007-11-15 22:36:07 +08001133 type &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xu5cde0af2006-08-22 00:07:53 +10001134 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
Herbert Xu332f88402007-11-15 22:36:07 +08001135 mask |= CRYPTO_ALG_TYPE_MASK;
Herbert Xu5cde0af2006-08-22 00:07:53 +10001136
1137 return __crypto_blkcipher_cast(crypto_alloc_base(alg_name, type, mask));
1138}
1139
1140static inline struct crypto_tfm *crypto_blkcipher_tfm(
1141 struct crypto_blkcipher *tfm)
1142{
1143 return &tfm->base;
1144}
1145
Stephan Mueller58284f02014-11-12 05:29:36 +01001146/**
1147 * crypto_free_blkcipher() - zeroize and free the block cipher handle
1148 * @tfm: cipher handle to be freed
1149 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001150static inline void crypto_free_blkcipher(struct crypto_blkcipher *tfm)
1151{
1152 crypto_free_tfm(crypto_blkcipher_tfm(tfm));
1153}
1154
Stephan Mueller58284f02014-11-12 05:29:36 +01001155/**
1156 * crypto_has_blkcipher() - Search for the availability of a block cipher
1157 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1158 * block cipher
1159 * @type: specifies the type of the cipher
1160 * @mask: specifies the mask for the cipher
1161 *
1162 * Return: true when the block cipher is known to the kernel crypto API; false
1163 * otherwise
1164 */
Herbert Xufce32d72006-08-26 17:35:45 +10001165static inline int crypto_has_blkcipher(const char *alg_name, u32 type, u32 mask)
1166{
Herbert Xu332f88402007-11-15 22:36:07 +08001167 type &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xufce32d72006-08-26 17:35:45 +10001168 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
Herbert Xu332f88402007-11-15 22:36:07 +08001169 mask |= CRYPTO_ALG_TYPE_MASK;
Herbert Xufce32d72006-08-26 17:35:45 +10001170
1171 return crypto_has_alg(alg_name, type, mask);
1172}
1173
Stephan Mueller58284f02014-11-12 05:29:36 +01001174/**
1175 * crypto_blkcipher_name() - return the name / cra_name from the cipher handle
1176 * @tfm: cipher handle
1177 *
1178 * Return: The character string holding the name of the cipher
1179 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001180static inline const char *crypto_blkcipher_name(struct crypto_blkcipher *tfm)
1181{
1182 return crypto_tfm_alg_name(crypto_blkcipher_tfm(tfm));
1183}
1184
1185static inline struct blkcipher_tfm *crypto_blkcipher_crt(
1186 struct crypto_blkcipher *tfm)
1187{
1188 return &crypto_blkcipher_tfm(tfm)->crt_blkcipher;
1189}
1190
1191static inline struct blkcipher_alg *crypto_blkcipher_alg(
1192 struct crypto_blkcipher *tfm)
1193{
1194 return &crypto_blkcipher_tfm(tfm)->__crt_alg->cra_blkcipher;
1195}
1196
Stephan Mueller58284f02014-11-12 05:29:36 +01001197/**
1198 * crypto_blkcipher_ivsize() - obtain IV size
1199 * @tfm: cipher handle
1200 *
1201 * The size of the IV for the block cipher referenced by the cipher handle is
1202 * returned. This IV size may be zero if the cipher does not need an IV.
1203 *
1204 * Return: IV size in bytes
1205 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001206static inline unsigned int crypto_blkcipher_ivsize(struct crypto_blkcipher *tfm)
1207{
1208 return crypto_blkcipher_alg(tfm)->ivsize;
1209}
1210
Stephan Mueller58284f02014-11-12 05:29:36 +01001211/**
1212 * crypto_blkcipher_blocksize() - obtain block size of cipher
1213 * @tfm: cipher handle
1214 *
1215 * The block size for the block cipher referenced with the cipher handle is
1216 * returned. The caller may use that information to allocate appropriate
1217 * memory for the data returned by the encryption or decryption operation.
1218 *
1219 * Return: block size of cipher
1220 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001221static inline unsigned int crypto_blkcipher_blocksize(
1222 struct crypto_blkcipher *tfm)
1223{
1224 return crypto_tfm_alg_blocksize(crypto_blkcipher_tfm(tfm));
1225}
1226
1227static inline unsigned int crypto_blkcipher_alignmask(
1228 struct crypto_blkcipher *tfm)
1229{
1230 return crypto_tfm_alg_alignmask(crypto_blkcipher_tfm(tfm));
1231}
1232
1233static inline u32 crypto_blkcipher_get_flags(struct crypto_blkcipher *tfm)
1234{
1235 return crypto_tfm_get_flags(crypto_blkcipher_tfm(tfm));
1236}
1237
1238static inline void crypto_blkcipher_set_flags(struct crypto_blkcipher *tfm,
1239 u32 flags)
1240{
1241 crypto_tfm_set_flags(crypto_blkcipher_tfm(tfm), flags);
1242}
1243
1244static inline void crypto_blkcipher_clear_flags(struct crypto_blkcipher *tfm,
1245 u32 flags)
1246{
1247 crypto_tfm_clear_flags(crypto_blkcipher_tfm(tfm), flags);
1248}
1249
Stephan Mueller58284f02014-11-12 05:29:36 +01001250/**
1251 * crypto_blkcipher_setkey() - set key for cipher
1252 * @tfm: cipher handle
1253 * @key: buffer holding the key
1254 * @keylen: length of the key in bytes
1255 *
1256 * The caller provided key is set for the block cipher referenced by the cipher
1257 * handle.
1258 *
1259 * Note, the key length determines the cipher type. Many block ciphers implement
1260 * different cipher modes depending on the key size, such as AES-128 vs AES-192
1261 * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
1262 * is performed.
1263 *
1264 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
1265 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001266static inline int crypto_blkcipher_setkey(struct crypto_blkcipher *tfm,
1267 const u8 *key, unsigned int keylen)
1268{
1269 return crypto_blkcipher_crt(tfm)->setkey(crypto_blkcipher_tfm(tfm),
1270 key, keylen);
1271}
1272
Stephan Mueller58284f02014-11-12 05:29:36 +01001273/**
1274 * crypto_blkcipher_encrypt() - encrypt plaintext
1275 * @desc: reference to the block cipher handle with meta data
1276 * @dst: scatter/gather list that is filled by the cipher operation with the
1277 * ciphertext
1278 * @src: scatter/gather list that holds the plaintext
1279 * @nbytes: number of bytes of the plaintext to encrypt.
1280 *
1281 * Encrypt plaintext data using the IV set by the caller with a preceding
1282 * call of crypto_blkcipher_set_iv.
1283 *
1284 * The blkcipher_desc data structure must be filled by the caller and can
1285 * reside on the stack. The caller must fill desc as follows: desc.tfm is filled
1286 * with the block cipher handle; desc.flags is filled with either
1287 * CRYPTO_TFM_REQ_MAY_SLEEP or 0.
1288 *
1289 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1290 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001291static inline int crypto_blkcipher_encrypt(struct blkcipher_desc *desc,
1292 struct scatterlist *dst,
1293 struct scatterlist *src,
1294 unsigned int nbytes)
1295{
1296 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
1297 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
1298}
1299
Stephan Mueller58284f02014-11-12 05:29:36 +01001300/**
1301 * crypto_blkcipher_encrypt_iv() - encrypt plaintext with dedicated IV
1302 * @desc: reference to the block cipher handle with meta data
1303 * @dst: scatter/gather list that is filled by the cipher operation with the
1304 * ciphertext
1305 * @src: scatter/gather list that holds the plaintext
1306 * @nbytes: number of bytes of the plaintext to encrypt.
1307 *
1308 * Encrypt plaintext data with the use of an IV that is solely used for this
1309 * cipher operation. Any previously set IV is not used.
1310 *
1311 * The blkcipher_desc data structure must be filled by the caller and can
1312 * reside on the stack. The caller must fill desc as follows: desc.tfm is filled
1313 * with the block cipher handle; desc.info is filled with the IV to be used for
1314 * the current operation; desc.flags is filled with either
1315 * CRYPTO_TFM_REQ_MAY_SLEEP or 0.
1316 *
1317 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1318 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001319static inline int crypto_blkcipher_encrypt_iv(struct blkcipher_desc *desc,
1320 struct scatterlist *dst,
1321 struct scatterlist *src,
1322 unsigned int nbytes)
1323{
1324 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
1325}
1326
Stephan Mueller58284f02014-11-12 05:29:36 +01001327/**
1328 * crypto_blkcipher_decrypt() - decrypt ciphertext
1329 * @desc: reference to the block cipher handle with meta data
1330 * @dst: scatter/gather list that is filled by the cipher operation with the
1331 * plaintext
1332 * @src: scatter/gather list that holds the ciphertext
1333 * @nbytes: number of bytes of the ciphertext to decrypt.
1334 *
1335 * Decrypt ciphertext data using the IV set by the caller with a preceding
1336 * call of crypto_blkcipher_set_iv.
1337 *
1338 * The blkcipher_desc data structure must be filled by the caller as documented
1339 * for the crypto_blkcipher_encrypt call above.
1340 *
1341 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1342 *
1343 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001344static inline int crypto_blkcipher_decrypt(struct blkcipher_desc *desc,
1345 struct scatterlist *dst,
1346 struct scatterlist *src,
1347 unsigned int nbytes)
1348{
1349 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
1350 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
1351}
1352
Stephan Mueller58284f02014-11-12 05:29:36 +01001353/**
1354 * crypto_blkcipher_decrypt_iv() - decrypt ciphertext with dedicated IV
1355 * @desc: reference to the block cipher handle with meta data
1356 * @dst: scatter/gather list that is filled by the cipher operation with the
1357 * plaintext
1358 * @src: scatter/gather list that holds the ciphertext
1359 * @nbytes: number of bytes of the ciphertext to decrypt.
1360 *
1361 * Decrypt ciphertext data with the use of an IV that is solely used for this
1362 * cipher operation. Any previously set IV is not used.
1363 *
1364 * The blkcipher_desc data structure must be filled by the caller as documented
1365 * for the crypto_blkcipher_encrypt_iv call above.
1366 *
1367 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1368 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001369static inline int crypto_blkcipher_decrypt_iv(struct blkcipher_desc *desc,
1370 struct scatterlist *dst,
1371 struct scatterlist *src,
1372 unsigned int nbytes)
1373{
1374 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
1375}
1376
Stephan Mueller58284f02014-11-12 05:29:36 +01001377/**
1378 * crypto_blkcipher_set_iv() - set IV for cipher
1379 * @tfm: cipher handle
1380 * @src: buffer holding the IV
1381 * @len: length of the IV in bytes
1382 *
1383 * The caller provided IV is set for the block cipher referenced by the cipher
1384 * handle.
1385 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001386static inline void crypto_blkcipher_set_iv(struct crypto_blkcipher *tfm,
1387 const u8 *src, unsigned int len)
1388{
1389 memcpy(crypto_blkcipher_crt(tfm)->iv, src, len);
1390}
1391
Stephan Mueller58284f02014-11-12 05:29:36 +01001392/**
1393 * crypto_blkcipher_get_iv() - obtain IV from cipher
1394 * @tfm: cipher handle
1395 * @dst: buffer filled with the IV
1396 * @len: length of the buffer dst
1397 *
1398 * The caller can obtain the IV set for the block cipher referenced by the
1399 * cipher handle and store it into the user-provided buffer. If the buffer
1400 * has an insufficient space, the IV is truncated to fit the buffer.
1401 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001402static inline void crypto_blkcipher_get_iv(struct crypto_blkcipher *tfm,
1403 u8 *dst, unsigned int len)
1404{
1405 memcpy(dst, crypto_blkcipher_crt(tfm)->iv, len);
1406}
1407
Stephan Mueller16e61032014-11-12 05:30:06 +01001408/**
1409 * DOC: Single Block Cipher API
1410 *
1411 * The single block cipher API is used with the ciphers of type
1412 * CRYPTO_ALG_TYPE_CIPHER (listed as type "cipher" in /proc/crypto).
1413 *
1414 * Using the single block cipher API calls, operations with the basic cipher
1415 * primitive can be implemented. These cipher primitives exclude any block
1416 * chaining operations including IV handling.
1417 *
1418 * The purpose of this single block cipher API is to support the implementation
1419 * of templates or other concepts that only need to perform the cipher operation
1420 * on one block at a time. Templates invoke the underlying cipher primitive
1421 * block-wise and process either the input or the output data of these cipher
1422 * operations.
1423 */
1424
Herbert Xuf28776a2006-08-13 20:58:18 +10001425static inline struct crypto_cipher *__crypto_cipher_cast(struct crypto_tfm *tfm)
1426{
1427 return (struct crypto_cipher *)tfm;
1428}
1429
1430static inline struct crypto_cipher *crypto_cipher_cast(struct crypto_tfm *tfm)
1431{
1432 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
1433 return __crypto_cipher_cast(tfm);
1434}
1435
Stephan Mueller16e61032014-11-12 05:30:06 +01001436/**
1437 * crypto_alloc_cipher() - allocate single block cipher handle
1438 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1439 * single block cipher
1440 * @type: specifies the type of the cipher
1441 * @mask: specifies the mask for the cipher
1442 *
1443 * Allocate a cipher handle for a single block cipher. The returned struct
1444 * crypto_cipher is the cipher handle that is required for any subsequent API
1445 * invocation for that single block cipher.
1446 *
1447 * Return: allocated cipher handle in case of success; IS_ERR() is true in case
1448 * of an error, PTR_ERR() returns the error code.
1449 */
Herbert Xuf28776a2006-08-13 20:58:18 +10001450static inline struct crypto_cipher *crypto_alloc_cipher(const char *alg_name,
1451 u32 type, u32 mask)
1452{
1453 type &= ~CRYPTO_ALG_TYPE_MASK;
1454 type |= CRYPTO_ALG_TYPE_CIPHER;
1455 mask |= CRYPTO_ALG_TYPE_MASK;
1456
1457 return __crypto_cipher_cast(crypto_alloc_base(alg_name, type, mask));
1458}
1459
1460static inline struct crypto_tfm *crypto_cipher_tfm(struct crypto_cipher *tfm)
1461{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001462 return &tfm->base;
Herbert Xuf28776a2006-08-13 20:58:18 +10001463}
1464
Stephan Mueller16e61032014-11-12 05:30:06 +01001465/**
1466 * crypto_free_cipher() - zeroize and free the single block cipher handle
1467 * @tfm: cipher handle to be freed
1468 */
Herbert Xuf28776a2006-08-13 20:58:18 +10001469static inline void crypto_free_cipher(struct crypto_cipher *tfm)
1470{
1471 crypto_free_tfm(crypto_cipher_tfm(tfm));
1472}
1473
Stephan Mueller16e61032014-11-12 05:30:06 +01001474/**
1475 * crypto_has_cipher() - Search for the availability of a single block cipher
1476 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1477 * single block cipher
1478 * @type: specifies the type of the cipher
1479 * @mask: specifies the mask for the cipher
1480 *
1481 * Return: true when the single block cipher is known to the kernel crypto API;
1482 * false otherwise
1483 */
Herbert Xufce32d72006-08-26 17:35:45 +10001484static inline int crypto_has_cipher(const char *alg_name, u32 type, u32 mask)
1485{
1486 type &= ~CRYPTO_ALG_TYPE_MASK;
1487 type |= CRYPTO_ALG_TYPE_CIPHER;
1488 mask |= CRYPTO_ALG_TYPE_MASK;
1489
1490 return crypto_has_alg(alg_name, type, mask);
1491}
1492
Herbert Xuf28776a2006-08-13 20:58:18 +10001493static inline struct cipher_tfm *crypto_cipher_crt(struct crypto_cipher *tfm)
1494{
1495 return &crypto_cipher_tfm(tfm)->crt_cipher;
1496}
1497
Stephan Mueller16e61032014-11-12 05:30:06 +01001498/**
1499 * crypto_cipher_blocksize() - obtain block size for cipher
1500 * @tfm: cipher handle
1501 *
1502 * The block size for the single block cipher referenced with the cipher handle
1503 * tfm is returned. The caller may use that information to allocate appropriate
1504 * memory for the data returned by the encryption or decryption operation
1505 *
1506 * Return: block size of cipher
1507 */
Herbert Xuf28776a2006-08-13 20:58:18 +10001508static inline unsigned int crypto_cipher_blocksize(struct crypto_cipher *tfm)
1509{
1510 return crypto_tfm_alg_blocksize(crypto_cipher_tfm(tfm));
1511}
1512
1513static inline unsigned int crypto_cipher_alignmask(struct crypto_cipher *tfm)
1514{
1515 return crypto_tfm_alg_alignmask(crypto_cipher_tfm(tfm));
1516}
1517
1518static inline u32 crypto_cipher_get_flags(struct crypto_cipher *tfm)
1519{
1520 return crypto_tfm_get_flags(crypto_cipher_tfm(tfm));
1521}
1522
1523static inline void crypto_cipher_set_flags(struct crypto_cipher *tfm,
1524 u32 flags)
1525{
1526 crypto_tfm_set_flags(crypto_cipher_tfm(tfm), flags);
1527}
1528
1529static inline void crypto_cipher_clear_flags(struct crypto_cipher *tfm,
1530 u32 flags)
1531{
1532 crypto_tfm_clear_flags(crypto_cipher_tfm(tfm), flags);
1533}
1534
Stephan Mueller16e61032014-11-12 05:30:06 +01001535/**
1536 * crypto_cipher_setkey() - set key for cipher
1537 * @tfm: cipher handle
1538 * @key: buffer holding the key
1539 * @keylen: length of the key in bytes
1540 *
1541 * The caller provided key is set for the single block cipher referenced by the
1542 * cipher handle.
1543 *
1544 * Note, the key length determines the cipher type. Many block ciphers implement
1545 * different cipher modes depending on the key size, such as AES-128 vs AES-192
1546 * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
1547 * is performed.
1548 *
1549 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
1550 */
Herbert Xu7226bc872006-08-21 21:40:49 +10001551static inline int crypto_cipher_setkey(struct crypto_cipher *tfm,
1552 const u8 *key, unsigned int keylen)
1553{
1554 return crypto_cipher_crt(tfm)->cit_setkey(crypto_cipher_tfm(tfm),
1555 key, keylen);
1556}
1557
Stephan Mueller16e61032014-11-12 05:30:06 +01001558/**
1559 * crypto_cipher_encrypt_one() - encrypt one block of plaintext
1560 * @tfm: cipher handle
1561 * @dst: points to the buffer that will be filled with the ciphertext
1562 * @src: buffer holding the plaintext to be encrypted
1563 *
1564 * Invoke the encryption operation of one block. The caller must ensure that
1565 * the plaintext and ciphertext buffers are at least one block in size.
1566 */
Herbert Xuf28776a2006-08-13 20:58:18 +10001567static inline void crypto_cipher_encrypt_one(struct crypto_cipher *tfm,
1568 u8 *dst, const u8 *src)
1569{
1570 crypto_cipher_crt(tfm)->cit_encrypt_one(crypto_cipher_tfm(tfm),
1571 dst, src);
1572}
1573
Stephan Mueller16e61032014-11-12 05:30:06 +01001574/**
1575 * crypto_cipher_decrypt_one() - decrypt one block of ciphertext
1576 * @tfm: cipher handle
1577 * @dst: points to the buffer that will be filled with the plaintext
1578 * @src: buffer holding the ciphertext to be decrypted
1579 *
1580 * Invoke the decryption operation of one block. The caller must ensure that
1581 * the plaintext and ciphertext buffers are at least one block in size.
1582 */
Herbert Xuf28776a2006-08-13 20:58:18 +10001583static inline void crypto_cipher_decrypt_one(struct crypto_cipher *tfm,
1584 u8 *dst, const u8 *src)
1585{
1586 crypto_cipher_crt(tfm)->cit_decrypt_one(crypto_cipher_tfm(tfm),
1587 dst, src);
1588}
1589
Herbert Xufce32d72006-08-26 17:35:45 +10001590static inline struct crypto_comp *__crypto_comp_cast(struct crypto_tfm *tfm)
1591{
1592 return (struct crypto_comp *)tfm;
1593}
1594
1595static inline struct crypto_comp *crypto_comp_cast(struct crypto_tfm *tfm)
1596{
1597 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_COMPRESS) &
1598 CRYPTO_ALG_TYPE_MASK);
1599 return __crypto_comp_cast(tfm);
1600}
1601
1602static inline struct crypto_comp *crypto_alloc_comp(const char *alg_name,
1603 u32 type, u32 mask)
1604{
1605 type &= ~CRYPTO_ALG_TYPE_MASK;
1606 type |= CRYPTO_ALG_TYPE_COMPRESS;
1607 mask |= CRYPTO_ALG_TYPE_MASK;
1608
1609 return __crypto_comp_cast(crypto_alloc_base(alg_name, type, mask));
1610}
1611
1612static inline struct crypto_tfm *crypto_comp_tfm(struct crypto_comp *tfm)
1613{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001614 return &tfm->base;
Herbert Xufce32d72006-08-26 17:35:45 +10001615}
1616
1617static inline void crypto_free_comp(struct crypto_comp *tfm)
1618{
1619 crypto_free_tfm(crypto_comp_tfm(tfm));
1620}
1621
1622static inline int crypto_has_comp(const char *alg_name, u32 type, u32 mask)
1623{
1624 type &= ~CRYPTO_ALG_TYPE_MASK;
1625 type |= CRYPTO_ALG_TYPE_COMPRESS;
1626 mask |= CRYPTO_ALG_TYPE_MASK;
1627
1628 return crypto_has_alg(alg_name, type, mask);
1629}
1630
Herbert Xue4d5b792006-08-26 18:12:40 +10001631static inline const char *crypto_comp_name(struct crypto_comp *tfm)
1632{
1633 return crypto_tfm_alg_name(crypto_comp_tfm(tfm));
1634}
1635
Herbert Xufce32d72006-08-26 17:35:45 +10001636static inline struct compress_tfm *crypto_comp_crt(struct crypto_comp *tfm)
1637{
1638 return &crypto_comp_tfm(tfm)->crt_compress;
1639}
1640
1641static inline int crypto_comp_compress(struct crypto_comp *tfm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 const u8 *src, unsigned int slen,
1643 u8 *dst, unsigned int *dlen)
1644{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001645 return crypto_comp_crt(tfm)->cot_compress(crypto_comp_tfm(tfm),
1646 src, slen, dst, dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647}
1648
Herbert Xufce32d72006-08-26 17:35:45 +10001649static inline int crypto_comp_decompress(struct crypto_comp *tfm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 const u8 *src, unsigned int slen,
1651 u8 *dst, unsigned int *dlen)
1652{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001653 return crypto_comp_crt(tfm)->cot_decompress(crypto_comp_tfm(tfm),
1654 src, slen, dst, dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655}
1656
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657#endif /* _LINUX_CRYPTO_H */
1658