blob: 7f4aee9e1f9118397b46ecce06636698b5ddc63e [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
28/*
Kees Cook5d26a102014-11-20 17:05:53 -080029 * Autoloaded crypto modules should only use a prefixed name to avoid allowing
30 * arbitrary modules to be loaded. Loading from userspace may still need the
31 * unprefixed names, so retains those aliases as well.
32 * This uses __MODULE_INFO directly instead of MODULE_ALIAS because pre-4.3
33 * gcc (e.g. avr32 toolchain) uses __LINE__ for uniqueness, and this macro
34 * expands twice on the same line. Instead, use a separate base name for the
35 * alias.
36 */
37#define MODULE_ALIAS_CRYPTO(name) \
38 __MODULE_INFO(alias, alias_userspace, name); \
39 __MODULE_INFO(alias, alias_crypto, "crypto-" name)
40
41/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 * Algorithm masks and types.
43 */
Herbert Xu28259822006-08-06 21:23:26 +100044#define CRYPTO_ALG_TYPE_MASK 0x0000000f
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#define CRYPTO_ALG_TYPE_CIPHER 0x00000001
Loc Ho004a4032008-05-14 20:41:47 +080046#define CRYPTO_ALG_TYPE_COMPRESS 0x00000002
47#define CRYPTO_ALG_TYPE_AEAD 0x00000003
Herbert Xu055bcee2006-08-19 22:24:23 +100048#define CRYPTO_ALG_TYPE_BLKCIPHER 0x00000004
Herbert Xu332f88402007-11-15 22:36:07 +080049#define CRYPTO_ALG_TYPE_ABLKCIPHER 0x00000005
Herbert Xu61da88e2007-12-17 21:51:27 +080050#define CRYPTO_ALG_TYPE_GIVCIPHER 0x00000006
Loc Ho004a4032008-05-14 20:41:47 +080051#define CRYPTO_ALG_TYPE_DIGEST 0x00000008
Herbert Xu5f7082e2008-08-31 22:21:09 +100052#define CRYPTO_ALG_TYPE_HASH 0x00000008
53#define CRYPTO_ALG_TYPE_SHASH 0x00000009
Loc Ho004a4032008-05-14 20:41:47 +080054#define CRYPTO_ALG_TYPE_AHASH 0x0000000a
Neil Horman17f0f4a2008-08-14 22:15:52 +100055#define CRYPTO_ALG_TYPE_RNG 0x0000000c
Tadeusz Struk3c339ab2015-06-16 10:30:55 -070056#define CRYPTO_ALG_TYPE_AKCIPHER 0x0000000d
Geert Uytterhoevena1d2f092009-03-04 15:05:33 +080057#define CRYPTO_ALG_TYPE_PCOMPRESS 0x0000000f
Herbert Xu055bcee2006-08-19 22:24:23 +100058
59#define CRYPTO_ALG_TYPE_HASH_MASK 0x0000000e
Loc Ho004a4032008-05-14 20:41:47 +080060#define CRYPTO_ALG_TYPE_AHASH_MASK 0x0000000c
Herbert Xu332f88402007-11-15 22:36:07 +080061#define CRYPTO_ALG_TYPE_BLKCIPHER_MASK 0x0000000c
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Herbert Xu28259822006-08-06 21:23:26 +100063#define CRYPTO_ALG_LARVAL 0x00000010
Herbert Xu6bfd4802006-09-21 11:39:29 +100064#define CRYPTO_ALG_DEAD 0x00000020
65#define CRYPTO_ALG_DYING 0x00000040
Herbert Xuf3f632d2006-08-06 23:12:59 +100066#define CRYPTO_ALG_ASYNC 0x00000080
Herbert Xu28259822006-08-06 21:23:26 +100067
Linus Torvalds1da177e2005-04-16 15:20:36 -070068/*
Herbert Xu60104392006-08-26 18:34:10 +100069 * Set this bit if and only if the algorithm requires another algorithm of
70 * the same type to handle corner cases.
71 */
72#define CRYPTO_ALG_NEED_FALLBACK 0x00000100
73
74/*
Herbert Xuecfc4322007-12-05 21:08:36 +110075 * This bit is set for symmetric key ciphers that have already been wrapped
76 * with a generic IV generator to prevent them from being wrapped again.
77 */
78#define CRYPTO_ALG_GENIV 0x00000200
79
80/*
Herbert Xu73d38642008-08-03 21:15:23 +080081 * Set if the algorithm has passed automated run-time testing. Note that
82 * if there is no run-time testing for a given algorithm it is considered
83 * to have passed.
84 */
85
86#define CRYPTO_ALG_TESTED 0x00000400
87
88/*
Steffen Klassert64a947b2011-09-27 07:21:26 +020089 * Set if the algorithm is an instance that is build from templates.
90 */
91#define CRYPTO_ALG_INSTANCE 0x00000800
92
Nikos Mavrogiannopoulosd912bb72011-11-01 13:39:56 +010093/* Set this bit if the algorithm provided is hardware accelerated but
94 * not available to userspace via instruction set or so.
95 */
96#define CRYPTO_ALG_KERN_DRIVER_ONLY 0x00001000
97
Steffen Klassert64a947b2011-09-27 07:21:26 +020098/*
Stephan Mueller06ca7f62015-03-30 21:55:52 +020099 * Mark a cipher as a service implementation only usable by another
100 * cipher and never by a normal user of the kernel crypto API
101 */
102#define CRYPTO_ALG_INTERNAL 0x00002000
103
104/*
Herbert Xu3e909502015-06-22 10:31:40 +0800105 * Temporary flag used to prevent legacy AEAD implementations from
106 * being used by user-space.
107 */
108#define CRYPTO_ALG_AEAD_NEW 0x00004000
109
110/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 * Transform masks and values (for crt_flags).
112 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113#define CRYPTO_TFM_REQ_MASK 0x000fff00
114#define CRYPTO_TFM_RES_MASK 0xfff00000
115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116#define CRYPTO_TFM_REQ_WEAK_KEY 0x00000100
Herbert Xu64baf3c2005-09-01 17:43:05 -0700117#define CRYPTO_TFM_REQ_MAY_SLEEP 0x00000200
Herbert Xu32e39832007-03-24 14:35:34 +1100118#define CRYPTO_TFM_REQ_MAY_BACKLOG 0x00000400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119#define CRYPTO_TFM_RES_WEAK_KEY 0x00100000
120#define CRYPTO_TFM_RES_BAD_KEY_LEN 0x00200000
121#define CRYPTO_TFM_RES_BAD_KEY_SCHED 0x00400000
122#define CRYPTO_TFM_RES_BAD_BLOCK_LEN 0x00800000
123#define CRYPTO_TFM_RES_BAD_FLAGS 0x01000000
124
125/*
126 * Miscellaneous stuff.
127 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128#define CRYPTO_MAX_ALG_NAME 64
129
Herbert Xu79911102006-08-21 21:03:52 +1000130/*
131 * The macro CRYPTO_MINALIGN_ATTR (along with the void * type in the actual
132 * declaration) is used to ensure that the crypto_tfm context structure is
133 * aligned correctly for the given architecture so that there are no alignment
134 * faults for C data types. In particular, this is required on platforms such
135 * as arm where pointers are 32-bit aligned but there are data types such as
136 * u64 which require 64-bit alignment.
137 */
Herbert Xu79911102006-08-21 21:03:52 +1000138#define CRYPTO_MINALIGN ARCH_KMALLOC_MINALIGN
Herbert Xu79911102006-08-21 21:03:52 +1000139
Herbert Xu79911102006-08-21 21:03:52 +1000140#define CRYPTO_MINALIGN_ATTR __attribute__ ((__aligned__(CRYPTO_MINALIGN)))
Herbert Xu79911102006-08-21 21:03:52 +1000141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142struct scatterlist;
Herbert Xu32e39832007-03-24 14:35:34 +1100143struct crypto_ablkcipher;
144struct crypto_async_request;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000145struct crypto_blkcipher;
Herbert Xu055bcee2006-08-19 22:24:23 +1000146struct crypto_hash;
Herbert Xu40725182005-07-06 13:51:52 -0700147struct crypto_tfm;
Herbert Xue853c3c2006-08-22 00:06:54 +1000148struct crypto_type;
Herbert Xu61da88e2007-12-17 21:51:27 +0800149struct skcipher_givcrypt_request;
Herbert Xu40725182005-07-06 13:51:52 -0700150
Herbert Xu32e39832007-03-24 14:35:34 +1100151typedef void (*crypto_completion_t)(struct crypto_async_request *req, int err);
152
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100153/**
154 * DOC: Block Cipher Context Data Structures
155 *
156 * These data structures define the operating context for each block cipher
157 * type.
158 */
159
Herbert Xu32e39832007-03-24 14:35:34 +1100160struct crypto_async_request {
161 struct list_head list;
162 crypto_completion_t complete;
163 void *data;
164 struct crypto_tfm *tfm;
165
166 u32 flags;
167};
168
169struct ablkcipher_request {
170 struct crypto_async_request base;
171
172 unsigned int nbytes;
173
174 void *info;
175
176 struct scatterlist *src;
177 struct scatterlist *dst;
178
179 void *__ctx[] CRYPTO_MINALIGN_ATTR;
180};
181
Herbert Xu5cde0af2006-08-22 00:07:53 +1000182struct blkcipher_desc {
183 struct crypto_blkcipher *tfm;
184 void *info;
185 u32 flags;
186};
187
Herbert Xu40725182005-07-06 13:51:52 -0700188struct cipher_desc {
189 struct crypto_tfm *tfm;
Herbert Xu6c2bb982006-05-16 22:09:29 +1000190 void (*crfn)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Herbert Xu40725182005-07-06 13:51:52 -0700191 unsigned int (*prfn)(const struct cipher_desc *desc, u8 *dst,
192 const u8 *src, unsigned int nbytes);
193 void *info;
194};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Herbert Xu055bcee2006-08-19 22:24:23 +1000196struct hash_desc {
197 struct crypto_hash *tfm;
198 u32 flags;
199};
200
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100201/**
202 * DOC: Block Cipher Algorithm Definitions
203 *
204 * These data structures define modular crypto algorithm implementations,
205 * managed via crypto_register_alg() and crypto_unregister_alg().
206 */
207
208/**
209 * struct ablkcipher_alg - asynchronous block cipher definition
210 * @min_keysize: Minimum key size supported by the transformation. This is the
211 * smallest key length supported by this transformation algorithm.
212 * This must be set to one of the pre-defined values as this is
213 * not hardware specific. Possible values for this field can be
214 * found via git grep "_MIN_KEY_SIZE" include/crypto/
215 * @max_keysize: Maximum key size supported by the transformation. This is the
216 * largest key length supported by this transformation algorithm.
217 * This must be set to one of the pre-defined values as this is
218 * not hardware specific. Possible values for this field can be
219 * found via git grep "_MAX_KEY_SIZE" include/crypto/
220 * @setkey: Set key for the transformation. This function is used to either
221 * program a supplied key into the hardware or store the key in the
222 * transformation context for programming it later. Note that this
223 * function does modify the transformation context. This function can
224 * be called multiple times during the existence of the transformation
225 * object, so one must make sure the key is properly reprogrammed into
226 * the hardware. This function is also responsible for checking the key
227 * length for validity. In case a software fallback was put in place in
228 * the @cra_init call, this function might need to use the fallback if
229 * the algorithm doesn't support all of the key sizes.
230 * @encrypt: Encrypt a scatterlist of blocks. This function is used to encrypt
231 * the supplied scatterlist containing the blocks of data. The crypto
232 * API consumer is responsible for aligning the entries of the
233 * scatterlist properly and making sure the chunks are correctly
234 * sized. In case a software fallback was put in place in the
235 * @cra_init call, this function might need to use the fallback if
236 * the algorithm doesn't support all of the key sizes. In case the
237 * key was stored in transformation context, the key might need to be
238 * re-programmed into the hardware in this function. This function
239 * shall not modify the transformation context, as this function may
240 * be called in parallel with the same transformation object.
241 * @decrypt: Decrypt a single block. This is a reverse counterpart to @encrypt
242 * and the conditions are exactly the same.
243 * @givencrypt: Update the IV for encryption. With this function, a cipher
244 * implementation may provide the function on how to update the IV
245 * for encryption.
246 * @givdecrypt: Update the IV for decryption. This is the reverse of
247 * @givencrypt .
248 * @geniv: The transformation implementation may use an "IV generator" provided
249 * by the kernel crypto API. Several use cases have a predefined
250 * approach how IVs are to be updated. For such use cases, the kernel
251 * crypto API provides ready-to-use implementations that can be
252 * referenced with this variable.
253 * @ivsize: IV size applicable for transformation. The consumer must provide an
254 * IV of exactly that size to perform the encrypt or decrypt operation.
255 *
256 * All fields except @givencrypt , @givdecrypt , @geniv and @ivsize are
257 * mandatory and must be filled.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 */
Herbert Xub5b7f082007-04-16 20:48:54 +1000259struct ablkcipher_alg {
260 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
261 unsigned int keylen);
262 int (*encrypt)(struct ablkcipher_request *req);
263 int (*decrypt)(struct ablkcipher_request *req);
Herbert Xu61da88e2007-12-17 21:51:27 +0800264 int (*givencrypt)(struct skcipher_givcrypt_request *req);
265 int (*givdecrypt)(struct skcipher_givcrypt_request *req);
Herbert Xub5b7f082007-04-16 20:48:54 +1000266
Herbert Xu23508e12007-11-27 21:33:24 +0800267 const char *geniv;
268
Herbert Xub5b7f082007-04-16 20:48:54 +1000269 unsigned int min_keysize;
270 unsigned int max_keysize;
271 unsigned int ivsize;
272};
273
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100274/**
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100275 * struct blkcipher_alg - synchronous block cipher definition
276 * @min_keysize: see struct ablkcipher_alg
277 * @max_keysize: see struct ablkcipher_alg
278 * @setkey: see struct ablkcipher_alg
279 * @encrypt: see struct ablkcipher_alg
280 * @decrypt: see struct ablkcipher_alg
281 * @geniv: see struct ablkcipher_alg
282 * @ivsize: see struct ablkcipher_alg
283 *
284 * All fields except @geniv and @ivsize are mandatory and must be filled.
285 */
Herbert Xu5cde0af2006-08-22 00:07:53 +1000286struct blkcipher_alg {
287 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
288 unsigned int keylen);
289 int (*encrypt)(struct blkcipher_desc *desc,
290 struct scatterlist *dst, struct scatterlist *src,
291 unsigned int nbytes);
292 int (*decrypt)(struct blkcipher_desc *desc,
293 struct scatterlist *dst, struct scatterlist *src,
294 unsigned int nbytes);
295
Herbert Xu23508e12007-11-27 21:33:24 +0800296 const char *geniv;
297
Herbert Xu5cde0af2006-08-22 00:07:53 +1000298 unsigned int min_keysize;
299 unsigned int max_keysize;
300 unsigned int ivsize;
301};
302
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100303/**
304 * struct cipher_alg - single-block symmetric ciphers definition
305 * @cia_min_keysize: Minimum key size supported by the transformation. This is
306 * the smallest key length supported by this transformation
307 * algorithm. This must be set to one of the pre-defined
308 * values as this is not hardware specific. Possible values
309 * for this field can be found via git grep "_MIN_KEY_SIZE"
310 * include/crypto/
311 * @cia_max_keysize: Maximum key size supported by the transformation. This is
312 * the largest key length supported by this transformation
313 * algorithm. This must be set to one of the pre-defined values
314 * as this is not hardware specific. Possible values for this
315 * field can be found via git grep "_MAX_KEY_SIZE"
316 * include/crypto/
317 * @cia_setkey: Set key for the transformation. This function is used to either
318 * program a supplied key into the hardware or store the key in the
319 * transformation context for programming it later. Note that this
320 * function does modify the transformation context. This function
321 * can be called multiple times during the existence of the
322 * transformation object, so one must make sure the key is properly
323 * reprogrammed into the hardware. This function is also
324 * responsible for checking the key length for validity.
325 * @cia_encrypt: Encrypt a single block. This function is used to encrypt a
326 * single block of data, which must be @cra_blocksize big. This
327 * always operates on a full @cra_blocksize and it is not possible
328 * to encrypt a block of smaller size. The supplied buffers must
329 * therefore also be at least of @cra_blocksize size. Both the
330 * input and output buffers are always aligned to @cra_alignmask.
331 * In case either of the input or output buffer supplied by user
332 * of the crypto API is not aligned to @cra_alignmask, the crypto
333 * API will re-align the buffers. The re-alignment means that a
334 * new buffer will be allocated, the data will be copied into the
335 * new buffer, then the processing will happen on the new buffer,
336 * then the data will be copied back into the original buffer and
337 * finally the new buffer will be freed. In case a software
338 * fallback was put in place in the @cra_init call, this function
339 * might need to use the fallback if the algorithm doesn't support
340 * all of the key sizes. In case the key was stored in
341 * transformation context, the key might need to be re-programmed
342 * into the hardware in this function. This function shall not
343 * modify the transformation context, as this function may be
344 * called in parallel with the same transformation object.
345 * @cia_decrypt: Decrypt a single block. This is a reverse counterpart to
346 * @cia_encrypt, and the conditions are exactly the same.
347 *
348 * All fields are mandatory and must be filled.
349 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350struct cipher_alg {
351 unsigned int cia_min_keysize;
352 unsigned int cia_max_keysize;
Herbert Xu6c2bb982006-05-16 22:09:29 +1000353 int (*cia_setkey)(struct crypto_tfm *tfm, const u8 *key,
Herbert Xu560c06a2006-08-13 14:16:39 +1000354 unsigned int keylen);
Herbert Xu6c2bb982006-05-16 22:09:29 +1000355 void (*cia_encrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
356 void (*cia_decrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357};
358
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359struct compress_alg {
Herbert Xu6c2bb982006-05-16 22:09:29 +1000360 int (*coa_compress)(struct crypto_tfm *tfm, const u8 *src,
361 unsigned int slen, u8 *dst, unsigned int *dlen);
362 int (*coa_decompress)(struct crypto_tfm *tfm, const u8 *src,
363 unsigned int slen, u8 *dst, unsigned int *dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364};
365
Neil Horman17f0f4a2008-08-14 22:15:52 +1000366
Herbert Xub5b7f082007-04-16 20:48:54 +1000367#define cra_ablkcipher cra_u.ablkcipher
Herbert Xu5cde0af2006-08-22 00:07:53 +1000368#define cra_blkcipher cra_u.blkcipher
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369#define cra_cipher cra_u.cipher
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370#define cra_compress cra_u.compress
371
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100372/**
373 * struct crypto_alg - definition of a cryptograpic cipher algorithm
374 * @cra_flags: Flags describing this transformation. See include/linux/crypto.h
375 * CRYPTO_ALG_* flags for the flags which go in here. Those are
376 * used for fine-tuning the description of the transformation
377 * algorithm.
378 * @cra_blocksize: Minimum block size of this transformation. The size in bytes
379 * of the smallest possible unit which can be transformed with
380 * this algorithm. The users must respect this value.
381 * In case of HASH transformation, it is possible for a smaller
382 * block than @cra_blocksize to be passed to the crypto API for
383 * transformation, in case of any other transformation type, an
384 * error will be returned upon any attempt to transform smaller
385 * than @cra_blocksize chunks.
386 * @cra_ctxsize: Size of the operational context of the transformation. This
387 * value informs the kernel crypto API about the memory size
388 * needed to be allocated for the transformation context.
389 * @cra_alignmask: Alignment mask for the input and output data buffer. The data
390 * buffer containing the input data for the algorithm must be
391 * aligned to this alignment mask. The data buffer for the
392 * output data must be aligned to this alignment mask. Note that
393 * the Crypto API will do the re-alignment in software, but
394 * only under special conditions and there is a performance hit.
395 * The re-alignment happens at these occasions for different
396 * @cra_u types: cipher -- For both input data and output data
397 * buffer; ahash -- For output hash destination buf; shash --
398 * For output hash destination buf.
399 * This is needed on hardware which is flawed by design and
400 * cannot pick data from arbitrary addresses.
401 * @cra_priority: Priority of this transformation implementation. In case
402 * multiple transformations with same @cra_name are available to
403 * the Crypto API, the kernel will use the one with highest
404 * @cra_priority.
405 * @cra_name: Generic name (usable by multiple implementations) of the
406 * transformation algorithm. This is the name of the transformation
407 * itself. This field is used by the kernel when looking up the
408 * providers of particular transformation.
409 * @cra_driver_name: Unique name of the transformation provider. This is the
410 * name of the provider of the transformation. This can be any
411 * arbitrary value, but in the usual case, this contains the
412 * name of the chip or provider and the name of the
413 * transformation algorithm.
414 * @cra_type: Type of the cryptographic transformation. This is a pointer to
415 * struct crypto_type, which implements callbacks common for all
Masanari Iida12f7c142015-06-04 00:01:21 +0900416 * transformation types. There are multiple options:
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100417 * &crypto_blkcipher_type, &crypto_ablkcipher_type,
Herbert Xub0d955b2015-08-14 15:30:41 +0800418 * &crypto_ahash_type, &crypto_rng_type.
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100419 * This field might be empty. In that case, there are no common
420 * callbacks. This is the case for: cipher, compress, shash.
421 * @cra_u: Callbacks implementing the transformation. This is a union of
422 * multiple structures. Depending on the type of transformation selected
423 * by @cra_type and @cra_flags above, the associated structure must be
424 * filled with callbacks. This field might be empty. This is the case
425 * for ahash, shash.
426 * @cra_init: Initialize the cryptographic transformation object. This function
427 * is used to initialize the cryptographic transformation object.
428 * This function is called only once at the instantiation time, right
429 * after the transformation context was allocated. In case the
430 * cryptographic hardware has some special requirements which need to
431 * be handled by software, this function shall check for the precise
432 * requirement of the transformation and put any software fallbacks
433 * in place.
434 * @cra_exit: Deinitialize the cryptographic transformation object. This is a
435 * counterpart to @cra_init, used to remove various changes set in
436 * @cra_init.
437 * @cra_module: Owner of this transformation implementation. Set to THIS_MODULE
438 * @cra_list: internally used
439 * @cra_users: internally used
440 * @cra_refcnt: internally used
441 * @cra_destroy: internally used
442 *
443 * The struct crypto_alg describes a generic Crypto API algorithm and is common
444 * for all of the transformations. Any variable not documented here shall not
445 * be used by a cipher implementation as it is internal to the Crypto API.
446 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447struct crypto_alg {
448 struct list_head cra_list;
Herbert Xu6bfd4802006-09-21 11:39:29 +1000449 struct list_head cra_users;
450
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 u32 cra_flags;
452 unsigned int cra_blocksize;
453 unsigned int cra_ctxsize;
Herbert Xu95477372005-07-06 13:52:09 -0700454 unsigned int cra_alignmask;
Herbert Xu5cb14542005-11-05 16:58:14 +1100455
456 int cra_priority;
Herbert Xu6521f302006-08-06 20:28:44 +1000457 atomic_t cra_refcnt;
Herbert Xu5cb14542005-11-05 16:58:14 +1100458
Herbert Xud913ea02006-05-21 08:45:26 +1000459 char cra_name[CRYPTO_MAX_ALG_NAME];
460 char cra_driver_name[CRYPTO_MAX_ALG_NAME];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
Herbert Xue853c3c2006-08-22 00:06:54 +1000462 const struct crypto_type *cra_type;
463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 union {
Herbert Xub5b7f082007-04-16 20:48:54 +1000465 struct ablkcipher_alg ablkcipher;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000466 struct blkcipher_alg blkcipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 struct cipher_alg cipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 struct compress_alg compress;
469 } cra_u;
Herbert Xuc7fc0592006-05-24 13:02:26 +1000470
471 int (*cra_init)(struct crypto_tfm *tfm);
472 void (*cra_exit)(struct crypto_tfm *tfm);
Herbert Xu6521f302006-08-06 20:28:44 +1000473 void (*cra_destroy)(struct crypto_alg *alg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
475 struct module *cra_module;
Herbert Xuedf18b92015-06-18 14:00:48 +0800476} CRYPTO_MINALIGN_ATTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
478/*
479 * Algorithm registration interface.
480 */
481int crypto_register_alg(struct crypto_alg *alg);
482int crypto_unregister_alg(struct crypto_alg *alg);
Mark Brown4b004342012-01-17 23:34:26 +0000483int crypto_register_algs(struct crypto_alg *algs, int count);
484int crypto_unregister_algs(struct crypto_alg *algs, int count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
486/*
487 * Algorithm query interface.
488 */
Herbert Xufce32d72006-08-26 17:35:45 +1000489int crypto_has_alg(const char *name, u32 type, u32 mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
491/*
492 * Transforms: user-instantiated objects which encapsulate algorithms
Herbert Xu6d7d6842006-07-30 11:53:01 +1000493 * and core processing logic. Managed via crypto_alloc_*() and
494 * crypto_free_*(), as well as the various helpers below.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
Herbert Xu32e39832007-03-24 14:35:34 +1100497struct ablkcipher_tfm {
498 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
499 unsigned int keylen);
500 int (*encrypt)(struct ablkcipher_request *req);
501 int (*decrypt)(struct ablkcipher_request *req);
Herbert Xu61da88e2007-12-17 21:51:27 +0800502 int (*givencrypt)(struct skcipher_givcrypt_request *req);
503 int (*givdecrypt)(struct skcipher_givcrypt_request *req);
504
Herbert Xuecfc4322007-12-05 21:08:36 +1100505 struct crypto_ablkcipher *base;
506
Herbert Xu32e39832007-03-24 14:35:34 +1100507 unsigned int ivsize;
508 unsigned int reqsize;
509};
510
Herbert Xu5cde0af2006-08-22 00:07:53 +1000511struct blkcipher_tfm {
512 void *iv;
513 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
514 unsigned int keylen);
515 int (*encrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
516 struct scatterlist *src, unsigned int nbytes);
517 int (*decrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
518 struct scatterlist *src, unsigned int nbytes);
519};
520
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521struct cipher_tfm {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 int (*cit_setkey)(struct crypto_tfm *tfm,
523 const u8 *key, unsigned int keylen);
Herbert Xuf28776a2006-08-13 20:58:18 +1000524 void (*cit_encrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
525 void (*cit_decrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526};
527
Herbert Xu055bcee2006-08-19 22:24:23 +1000528struct hash_tfm {
529 int (*init)(struct hash_desc *desc);
530 int (*update)(struct hash_desc *desc,
531 struct scatterlist *sg, unsigned int nsg);
532 int (*final)(struct hash_desc *desc, u8 *out);
533 int (*digest)(struct hash_desc *desc, struct scatterlist *sg,
534 unsigned int nsg, u8 *out);
535 int (*setkey)(struct crypto_hash *tfm, const u8 *key,
536 unsigned int keylen);
Herbert Xu055bcee2006-08-19 22:24:23 +1000537 unsigned int digestsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538};
539
540struct compress_tfm {
541 int (*cot_compress)(struct crypto_tfm *tfm,
542 const u8 *src, unsigned int slen,
543 u8 *dst, unsigned int *dlen);
544 int (*cot_decompress)(struct crypto_tfm *tfm,
545 const u8 *src, unsigned int slen,
546 u8 *dst, unsigned int *dlen);
547};
548
Herbert Xu32e39832007-03-24 14:35:34 +1100549#define crt_ablkcipher crt_u.ablkcipher
Herbert Xu5cde0af2006-08-22 00:07:53 +1000550#define crt_blkcipher crt_u.blkcipher
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551#define crt_cipher crt_u.cipher
Herbert Xu055bcee2006-08-19 22:24:23 +1000552#define crt_hash crt_u.hash
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553#define crt_compress crt_u.compress
554
555struct crypto_tfm {
556
557 u32 crt_flags;
558
559 union {
Herbert Xu32e39832007-03-24 14:35:34 +1100560 struct ablkcipher_tfm ablkcipher;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000561 struct blkcipher_tfm blkcipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 struct cipher_tfm cipher;
Herbert Xu055bcee2006-08-19 22:24:23 +1000563 struct hash_tfm hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 struct compress_tfm compress;
565 } crt_u;
Herbert Xu4a779482008-09-13 18:19:03 -0700566
567 void (*exit)(struct crypto_tfm *tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
569 struct crypto_alg *__crt_alg;
Herbert Xuf10b7892006-01-25 22:34:01 +1100570
Herbert Xu79911102006-08-21 21:03:52 +1000571 void *__crt_ctx[] CRYPTO_MINALIGN_ATTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572};
573
Herbert Xu32e39832007-03-24 14:35:34 +1100574struct crypto_ablkcipher {
575 struct crypto_tfm base;
576};
577
Herbert Xu5cde0af2006-08-22 00:07:53 +1000578struct crypto_blkcipher {
579 struct crypto_tfm base;
580};
581
Herbert Xu78a1fe42006-12-24 10:02:00 +1100582struct crypto_cipher {
583 struct crypto_tfm base;
584};
585
586struct crypto_comp {
587 struct crypto_tfm base;
588};
589
Herbert Xu055bcee2006-08-19 22:24:23 +1000590struct crypto_hash {
591 struct crypto_tfm base;
592};
593
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000594enum {
595 CRYPTOA_UNSPEC,
596 CRYPTOA_ALG,
Herbert Xuebc610e2007-01-01 18:37:02 +1100597 CRYPTOA_TYPE,
Herbert Xu39e1ee012007-08-29 19:27:26 +0800598 CRYPTOA_U32,
Herbert Xuebc610e2007-01-01 18:37:02 +1100599 __CRYPTOA_MAX,
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000600};
601
Herbert Xuebc610e2007-01-01 18:37:02 +1100602#define CRYPTOA_MAX (__CRYPTOA_MAX - 1)
603
Herbert Xu39e1ee012007-08-29 19:27:26 +0800604/* Maximum number of (rtattr) parameters for each template. */
605#define CRYPTO_MAX_ATTRS 32
606
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000607struct crypto_attr_alg {
608 char name[CRYPTO_MAX_ALG_NAME];
609};
610
Herbert Xuebc610e2007-01-01 18:37:02 +1100611struct crypto_attr_type {
612 u32 type;
613 u32 mask;
614};
615
Herbert Xu39e1ee012007-08-29 19:27:26 +0800616struct crypto_attr_u32 {
617 u32 num;
618};
619
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620/*
621 * Transform user interface.
622 */
623
Herbert Xu6d7d6842006-07-30 11:53:01 +1000624struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask);
Herbert Xu7b2cd922009-02-05 16:48:24 +1100625void crypto_destroy_tfm(void *mem, struct crypto_tfm *tfm);
626
627static inline void crypto_free_tfm(struct crypto_tfm *tfm)
628{
629 return crypto_destroy_tfm(tfm, tfm);
630}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
Herbert Xuda7f0332008-07-31 17:08:25 +0800632int alg_test(const char *driver, const char *alg, u32 type, u32 mask);
633
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634/*
635 * Transform helpers which query the underlying algorithm.
636 */
637static inline const char *crypto_tfm_alg_name(struct crypto_tfm *tfm)
638{
639 return tfm->__crt_alg->cra_name;
640}
641
Michal Ludvigb14cdd62006-07-09 09:02:24 +1000642static inline const char *crypto_tfm_alg_driver_name(struct crypto_tfm *tfm)
643{
644 return tfm->__crt_alg->cra_driver_name;
645}
646
647static inline int crypto_tfm_alg_priority(struct crypto_tfm *tfm)
648{
649 return tfm->__crt_alg->cra_priority;
650}
651
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm)
653{
654 return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK;
655}
656
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm)
658{
659 return tfm->__crt_alg->cra_blocksize;
660}
661
Herbert Xufbdae9f2005-07-06 13:53:29 -0700662static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm *tfm)
663{
664 return tfm->__crt_alg->cra_alignmask;
665}
666
Herbert Xuf28776a2006-08-13 20:58:18 +1000667static inline u32 crypto_tfm_get_flags(struct crypto_tfm *tfm)
668{
669 return tfm->crt_flags;
670}
671
672static inline void crypto_tfm_set_flags(struct crypto_tfm *tfm, u32 flags)
673{
674 tfm->crt_flags |= flags;
675}
676
677static inline void crypto_tfm_clear_flags(struct crypto_tfm *tfm, u32 flags)
678{
679 tfm->crt_flags &= ~flags;
680}
681
Herbert Xu40725182005-07-06 13:51:52 -0700682static inline void *crypto_tfm_ctx(struct crypto_tfm *tfm)
683{
Herbert Xuf10b7892006-01-25 22:34:01 +1100684 return tfm->__crt_ctx;
685}
686
687static inline unsigned int crypto_tfm_ctx_alignment(void)
688{
689 struct crypto_tfm *tfm;
690 return __alignof__(tfm->__crt_ctx);
Herbert Xu40725182005-07-06 13:51:52 -0700691}
692
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693/*
694 * API wrappers.
695 */
Herbert Xu32e39832007-03-24 14:35:34 +1100696static inline struct crypto_ablkcipher *__crypto_ablkcipher_cast(
697 struct crypto_tfm *tfm)
698{
699 return (struct crypto_ablkcipher *)tfm;
700}
701
Herbert Xu378f4f52007-12-17 20:07:31 +0800702static inline u32 crypto_skcipher_type(u32 type)
703{
Herbert Xuecfc4322007-12-05 21:08:36 +1100704 type &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
Herbert Xu378f4f52007-12-17 20:07:31 +0800705 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
706 return type;
707}
708
709static inline u32 crypto_skcipher_mask(u32 mask)
710{
Herbert Xuecfc4322007-12-05 21:08:36 +1100711 mask &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
Herbert Xu378f4f52007-12-17 20:07:31 +0800712 mask |= CRYPTO_ALG_TYPE_BLKCIPHER_MASK;
713 return mask;
714}
715
Stephan Muellerf13ec332014-11-12 05:28:22 +0100716/**
717 * DOC: Asynchronous Block Cipher API
718 *
719 * Asynchronous block cipher API is used with the ciphers of type
720 * CRYPTO_ALG_TYPE_ABLKCIPHER (listed as type "ablkcipher" in /proc/crypto).
721 *
722 * Asynchronous cipher operations imply that the function invocation for a
723 * cipher request returns immediately before the completion of the operation.
724 * The cipher request is scheduled as a separate kernel thread and therefore
725 * load-balanced on the different CPUs via the process scheduler. To allow
726 * the kernel crypto API to inform the caller about the completion of a cipher
727 * request, the caller must provide a callback function. That function is
728 * invoked with the cipher handle when the request completes.
729 *
730 * To support the asynchronous operation, additional information than just the
731 * cipher handle must be supplied to the kernel crypto API. That additional
732 * information is given by filling in the ablkcipher_request data structure.
733 *
734 * For the asynchronous block cipher API, the state is maintained with the tfm
735 * cipher handle. A single tfm can be used across multiple calls and in
736 * parallel. For asynchronous block cipher calls, context data supplied and
737 * only used by the caller can be referenced the request data structure in
738 * addition to the IV used for the cipher request. The maintenance of such
739 * state information would be important for a crypto driver implementer to
740 * have, because when calling the callback function upon completion of the
741 * cipher operation, that callback function may need some information about
742 * which operation just finished if it invoked multiple in parallel. This
743 * state information is unused by the kernel crypto API.
744 */
745
746/**
747 * crypto_alloc_ablkcipher() - allocate asynchronous block cipher handle
748 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
749 * ablkcipher cipher
750 * @type: specifies the type of the cipher
751 * @mask: specifies the mask for the cipher
752 *
753 * Allocate a cipher handle for an ablkcipher. The returned struct
754 * crypto_ablkcipher is the cipher handle that is required for any subsequent
755 * API invocation for that ablkcipher.
756 *
757 * Return: allocated cipher handle in case of success; IS_ERR() is true in case
758 * of an error, PTR_ERR() returns the error code.
759 */
Herbert Xub9c55aa2007-12-04 12:46:48 +1100760struct crypto_ablkcipher *crypto_alloc_ablkcipher(const char *alg_name,
761 u32 type, u32 mask);
Herbert Xu32e39832007-03-24 14:35:34 +1100762
763static inline struct crypto_tfm *crypto_ablkcipher_tfm(
764 struct crypto_ablkcipher *tfm)
765{
766 return &tfm->base;
767}
768
Stephan Muellerf13ec332014-11-12 05:28:22 +0100769/**
770 * crypto_free_ablkcipher() - zeroize and free cipher handle
771 * @tfm: cipher handle to be freed
772 */
Herbert Xu32e39832007-03-24 14:35:34 +1100773static inline void crypto_free_ablkcipher(struct crypto_ablkcipher *tfm)
774{
775 crypto_free_tfm(crypto_ablkcipher_tfm(tfm));
776}
777
Stephan Muellerf13ec332014-11-12 05:28:22 +0100778/**
779 * crypto_has_ablkcipher() - Search for the availability of an ablkcipher.
780 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
781 * ablkcipher
782 * @type: specifies the type of the cipher
783 * @mask: specifies the mask for the cipher
784 *
785 * Return: true when the ablkcipher is known to the kernel crypto API; false
786 * otherwise
787 */
Herbert Xu32e39832007-03-24 14:35:34 +1100788static inline int crypto_has_ablkcipher(const char *alg_name, u32 type,
789 u32 mask)
790{
Herbert Xu378f4f52007-12-17 20:07:31 +0800791 return crypto_has_alg(alg_name, crypto_skcipher_type(type),
792 crypto_skcipher_mask(mask));
Herbert Xu32e39832007-03-24 14:35:34 +1100793}
794
795static inline struct ablkcipher_tfm *crypto_ablkcipher_crt(
796 struct crypto_ablkcipher *tfm)
797{
798 return &crypto_ablkcipher_tfm(tfm)->crt_ablkcipher;
799}
800
Stephan Muellerf13ec332014-11-12 05:28:22 +0100801/**
802 * crypto_ablkcipher_ivsize() - obtain IV size
803 * @tfm: cipher handle
804 *
805 * The size of the IV for the ablkcipher referenced by the cipher handle is
806 * returned. This IV size may be zero if the cipher does not need an IV.
807 *
808 * Return: IV size in bytes
809 */
Herbert Xu32e39832007-03-24 14:35:34 +1100810static inline unsigned int crypto_ablkcipher_ivsize(
811 struct crypto_ablkcipher *tfm)
812{
813 return crypto_ablkcipher_crt(tfm)->ivsize;
814}
815
Stephan Muellerf13ec332014-11-12 05:28:22 +0100816/**
817 * crypto_ablkcipher_blocksize() - obtain block size of cipher
818 * @tfm: cipher handle
819 *
820 * The block size for the ablkcipher referenced with the cipher handle is
821 * returned. The caller may use that information to allocate appropriate
822 * memory for the data returned by the encryption or decryption operation
823 *
824 * Return: block size of cipher
825 */
Herbert Xu32e39832007-03-24 14:35:34 +1100826static inline unsigned int crypto_ablkcipher_blocksize(
827 struct crypto_ablkcipher *tfm)
828{
829 return crypto_tfm_alg_blocksize(crypto_ablkcipher_tfm(tfm));
830}
831
832static inline unsigned int crypto_ablkcipher_alignmask(
833 struct crypto_ablkcipher *tfm)
834{
835 return crypto_tfm_alg_alignmask(crypto_ablkcipher_tfm(tfm));
836}
837
838static inline u32 crypto_ablkcipher_get_flags(struct crypto_ablkcipher *tfm)
839{
840 return crypto_tfm_get_flags(crypto_ablkcipher_tfm(tfm));
841}
842
843static inline void crypto_ablkcipher_set_flags(struct crypto_ablkcipher *tfm,
844 u32 flags)
845{
846 crypto_tfm_set_flags(crypto_ablkcipher_tfm(tfm), flags);
847}
848
849static inline void crypto_ablkcipher_clear_flags(struct crypto_ablkcipher *tfm,
850 u32 flags)
851{
852 crypto_tfm_clear_flags(crypto_ablkcipher_tfm(tfm), flags);
853}
854
Stephan Muellerf13ec332014-11-12 05:28:22 +0100855/**
856 * crypto_ablkcipher_setkey() - set key for cipher
857 * @tfm: cipher handle
858 * @key: buffer holding the key
859 * @keylen: length of the key in bytes
860 *
861 * The caller provided key is set for the ablkcipher referenced by the cipher
862 * handle.
863 *
864 * Note, the key length determines the cipher type. Many block ciphers implement
865 * different cipher modes depending on the key size, such as AES-128 vs AES-192
866 * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
867 * is performed.
868 *
869 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
870 */
Herbert Xu32e39832007-03-24 14:35:34 +1100871static inline int crypto_ablkcipher_setkey(struct crypto_ablkcipher *tfm,
872 const u8 *key, unsigned int keylen)
873{
Herbert Xuecfc4322007-12-05 21:08:36 +1100874 struct ablkcipher_tfm *crt = crypto_ablkcipher_crt(tfm);
875
876 return crt->setkey(crt->base, key, keylen);
Herbert Xu32e39832007-03-24 14:35:34 +1100877}
878
Stephan Muellerf13ec332014-11-12 05:28:22 +0100879/**
880 * crypto_ablkcipher_reqtfm() - obtain cipher handle from request
881 * @req: ablkcipher_request out of which the cipher handle is to be obtained
882 *
883 * Return the crypto_ablkcipher handle when furnishing an ablkcipher_request
884 * data structure.
885 *
886 * Return: crypto_ablkcipher handle
887 */
Herbert Xu32e39832007-03-24 14:35:34 +1100888static inline struct crypto_ablkcipher *crypto_ablkcipher_reqtfm(
889 struct ablkcipher_request *req)
890{
891 return __crypto_ablkcipher_cast(req->base.tfm);
892}
893
Stephan Muellerf13ec332014-11-12 05:28:22 +0100894/**
895 * crypto_ablkcipher_encrypt() - encrypt plaintext
896 * @req: reference to the ablkcipher_request handle that holds all information
897 * needed to perform the cipher operation
898 *
899 * Encrypt plaintext data using the ablkcipher_request handle. That data
900 * structure and how it is filled with data is discussed with the
901 * ablkcipher_request_* functions.
902 *
903 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
904 */
Herbert Xu32e39832007-03-24 14:35:34 +1100905static inline int crypto_ablkcipher_encrypt(struct ablkcipher_request *req)
906{
907 struct ablkcipher_tfm *crt =
908 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
909 return crt->encrypt(req);
910}
911
Stephan Muellerf13ec332014-11-12 05:28:22 +0100912/**
913 * crypto_ablkcipher_decrypt() - decrypt ciphertext
914 * @req: reference to the ablkcipher_request handle that holds all information
915 * needed to perform the cipher operation
916 *
917 * Decrypt ciphertext data using the ablkcipher_request handle. That data
918 * structure and how it is filled with data is discussed with the
919 * ablkcipher_request_* functions.
920 *
921 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
922 */
Herbert Xu32e39832007-03-24 14:35:34 +1100923static inline int crypto_ablkcipher_decrypt(struct ablkcipher_request *req)
924{
925 struct ablkcipher_tfm *crt =
926 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
927 return crt->decrypt(req);
928}
929
Stephan Muellerf13ec332014-11-12 05:28:22 +0100930/**
931 * DOC: Asynchronous Cipher Request Handle
932 *
933 * The ablkcipher_request data structure contains all pointers to data
934 * required for the asynchronous cipher operation. This includes the cipher
935 * handle (which can be used by multiple ablkcipher_request instances), pointer
936 * to plaintext and ciphertext, asynchronous callback function, etc. It acts
937 * as a handle to the ablkcipher_request_* API calls in a similar way as
938 * ablkcipher handle to the crypto_ablkcipher_* API calls.
939 */
940
941/**
942 * crypto_ablkcipher_reqsize() - obtain size of the request data structure
943 * @tfm: cipher handle
944 *
945 * Return: number of bytes
946 */
Herbert Xub16c3a22007-08-29 19:02:04 +0800947static inline unsigned int crypto_ablkcipher_reqsize(
948 struct crypto_ablkcipher *tfm)
Herbert Xu32e39832007-03-24 14:35:34 +1100949{
950 return crypto_ablkcipher_crt(tfm)->reqsize;
951}
952
Stephan Muellerf13ec332014-11-12 05:28:22 +0100953/**
954 * ablkcipher_request_set_tfm() - update cipher handle reference in request
955 * @req: request handle to be modified
956 * @tfm: cipher handle that shall be added to the request handle
957 *
958 * Allow the caller to replace the existing ablkcipher handle in the request
959 * data structure with a different one.
960 */
Herbert Xue196d622007-04-14 16:09:14 +1000961static inline void ablkcipher_request_set_tfm(
962 struct ablkcipher_request *req, struct crypto_ablkcipher *tfm)
963{
Herbert Xuecfc4322007-12-05 21:08:36 +1100964 req->base.tfm = crypto_ablkcipher_tfm(crypto_ablkcipher_crt(tfm)->base);
Herbert Xue196d622007-04-14 16:09:14 +1000965}
966
Herbert Xub5b7f082007-04-16 20:48:54 +1000967static inline struct ablkcipher_request *ablkcipher_request_cast(
968 struct crypto_async_request *req)
969{
970 return container_of(req, struct ablkcipher_request, base);
971}
972
Stephan Muellerf13ec332014-11-12 05:28:22 +0100973/**
974 * ablkcipher_request_alloc() - allocate request data structure
975 * @tfm: cipher handle to be registered with the request
976 * @gfp: memory allocation flag that is handed to kmalloc by the API call.
977 *
978 * Allocate the request data structure that must be used with the ablkcipher
979 * encrypt and decrypt API calls. During the allocation, the provided ablkcipher
980 * handle is registered in the request data structure.
981 *
982 * Return: allocated request handle in case of success; IS_ERR() is true in case
983 * of an error, PTR_ERR() returns the error code.
984 */
Herbert Xu32e39832007-03-24 14:35:34 +1100985static inline struct ablkcipher_request *ablkcipher_request_alloc(
986 struct crypto_ablkcipher *tfm, gfp_t gfp)
987{
988 struct ablkcipher_request *req;
989
990 req = kmalloc(sizeof(struct ablkcipher_request) +
991 crypto_ablkcipher_reqsize(tfm), gfp);
992
993 if (likely(req))
Herbert Xue196d622007-04-14 16:09:14 +1000994 ablkcipher_request_set_tfm(req, tfm);
Herbert Xu32e39832007-03-24 14:35:34 +1100995
996 return req;
997}
998
Stephan Muellerf13ec332014-11-12 05:28:22 +0100999/**
1000 * ablkcipher_request_free() - zeroize and free request data structure
1001 * @req: request data structure cipher handle to be freed
1002 */
Herbert Xu32e39832007-03-24 14:35:34 +11001003static inline void ablkcipher_request_free(struct ablkcipher_request *req)
1004{
Herbert Xuaef73cf2009-07-11 22:22:14 +08001005 kzfree(req);
Herbert Xu32e39832007-03-24 14:35:34 +11001006}
1007
Stephan Muellerf13ec332014-11-12 05:28:22 +01001008/**
1009 * ablkcipher_request_set_callback() - set asynchronous callback function
1010 * @req: request handle
1011 * @flags: specify zero or an ORing of the flags
1012 * CRYPTO_TFM_REQ_MAY_BACKLOG the request queue may back log and
1013 * increase the wait queue beyond the initial maximum size;
1014 * CRYPTO_TFM_REQ_MAY_SLEEP the request processing may sleep
1015 * @compl: callback function pointer to be registered with the request handle
1016 * @data: The data pointer refers to memory that is not used by the kernel
1017 * crypto API, but provided to the callback function for it to use. Here,
1018 * the caller can provide a reference to memory the callback function can
1019 * operate on. As the callback function is invoked asynchronously to the
1020 * related functionality, it may need to access data structures of the
1021 * related functionality which can be referenced using this pointer. The
1022 * callback function can access the memory via the "data" field in the
1023 * crypto_async_request data structure provided to the callback function.
1024 *
1025 * This function allows setting the callback function that is triggered once the
1026 * cipher operation completes.
1027 *
1028 * The callback function is registered with the ablkcipher_request handle and
Stephan Mueller379dcfb2015-01-19 00:13:39 +01001029 * must comply with the following template
Stephan Muellerf13ec332014-11-12 05:28:22 +01001030 *
1031 * void callback_function(struct crypto_async_request *req, int error)
1032 */
Herbert Xu32e39832007-03-24 14:35:34 +11001033static inline void ablkcipher_request_set_callback(
1034 struct ablkcipher_request *req,
Mark Rustad3e3dc252014-07-25 02:53:38 -07001035 u32 flags, crypto_completion_t compl, void *data)
Herbert Xu32e39832007-03-24 14:35:34 +11001036{
Mark Rustad3e3dc252014-07-25 02:53:38 -07001037 req->base.complete = compl;
Herbert Xu32e39832007-03-24 14:35:34 +11001038 req->base.data = data;
1039 req->base.flags = flags;
1040}
1041
Stephan Muellerf13ec332014-11-12 05:28:22 +01001042/**
1043 * ablkcipher_request_set_crypt() - set data buffers
1044 * @req: request handle
1045 * @src: source scatter / gather list
1046 * @dst: destination scatter / gather list
1047 * @nbytes: number of bytes to process from @src
1048 * @iv: IV for the cipher operation which must comply with the IV size defined
1049 * by crypto_ablkcipher_ivsize
1050 *
1051 * This function allows setting of the source data and destination data
1052 * scatter / gather lists.
1053 *
1054 * For encryption, the source is treated as the plaintext and the
1055 * destination is the ciphertext. For a decryption operation, the use is
Stephan Mueller379dcfb2015-01-19 00:13:39 +01001056 * reversed - the source is the ciphertext and the destination is the plaintext.
Stephan Muellerf13ec332014-11-12 05:28:22 +01001057 */
Herbert Xu32e39832007-03-24 14:35:34 +11001058static inline void ablkcipher_request_set_crypt(
1059 struct ablkcipher_request *req,
1060 struct scatterlist *src, struct scatterlist *dst,
1061 unsigned int nbytes, void *iv)
1062{
1063 req->src = src;
1064 req->dst = dst;
1065 req->nbytes = nbytes;
1066 req->info = iv;
1067}
1068
Stephan Muellerfced7b02014-11-12 05:29:00 +01001069/**
Stephan Mueller58284f02014-11-12 05:29:36 +01001070 * DOC: Synchronous Block Cipher API
1071 *
1072 * The synchronous block cipher API is used with the ciphers of type
1073 * CRYPTO_ALG_TYPE_BLKCIPHER (listed as type "blkcipher" in /proc/crypto)
1074 *
1075 * Synchronous calls, have a context in the tfm. But since a single tfm can be
1076 * used in multiple calls and in parallel, this info should not be changeable
1077 * (unless a lock is used). This applies, for example, to the symmetric key.
1078 * However, the IV is changeable, so there is an iv field in blkcipher_tfm
1079 * structure for synchronous blkcipher api. So, its the only state info that can
1080 * be kept for synchronous calls without using a big lock across a tfm.
1081 *
1082 * The block cipher API allows the use of a complete cipher, i.e. a cipher
1083 * consisting of a template (a block chaining mode) and a single block cipher
1084 * primitive (e.g. AES).
1085 *
1086 * The plaintext data buffer and the ciphertext data buffer are pointed to
1087 * by using scatter/gather lists. The cipher operation is performed
1088 * on all segments of the provided scatter/gather lists.
1089 *
1090 * The kernel crypto API supports a cipher operation "in-place" which means that
1091 * the caller may provide the same scatter/gather list for the plaintext and
1092 * cipher text. After the completion of the cipher operation, the plaintext
1093 * data is replaced with the ciphertext data in case of an encryption and vice
1094 * versa for a decryption. The caller must ensure that the scatter/gather lists
1095 * for the output data point to sufficiently large buffers, i.e. multiples of
1096 * the block size of the cipher.
1097 */
1098
Herbert Xu5cde0af2006-08-22 00:07:53 +10001099static inline struct crypto_blkcipher *__crypto_blkcipher_cast(
1100 struct crypto_tfm *tfm)
1101{
1102 return (struct crypto_blkcipher *)tfm;
1103}
1104
1105static inline struct crypto_blkcipher *crypto_blkcipher_cast(
1106 struct crypto_tfm *tfm)
1107{
1108 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_BLKCIPHER);
1109 return __crypto_blkcipher_cast(tfm);
1110}
1111
Stephan Mueller58284f02014-11-12 05:29:36 +01001112/**
1113 * crypto_alloc_blkcipher() - allocate synchronous block cipher handle
1114 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1115 * blkcipher cipher
1116 * @type: specifies the type of the cipher
1117 * @mask: specifies the mask for the cipher
1118 *
1119 * Allocate a cipher handle for a block cipher. The returned struct
1120 * crypto_blkcipher is the cipher handle that is required for any subsequent
1121 * API invocation for that block cipher.
1122 *
1123 * Return: allocated cipher handle in case of success; IS_ERR() is true in case
1124 * of an error, PTR_ERR() returns the error code.
1125 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001126static inline struct crypto_blkcipher *crypto_alloc_blkcipher(
1127 const char *alg_name, u32 type, u32 mask)
1128{
Herbert Xu332f88402007-11-15 22:36:07 +08001129 type &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xu5cde0af2006-08-22 00:07:53 +10001130 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
Herbert Xu332f88402007-11-15 22:36:07 +08001131 mask |= CRYPTO_ALG_TYPE_MASK;
Herbert Xu5cde0af2006-08-22 00:07:53 +10001132
1133 return __crypto_blkcipher_cast(crypto_alloc_base(alg_name, type, mask));
1134}
1135
1136static inline struct crypto_tfm *crypto_blkcipher_tfm(
1137 struct crypto_blkcipher *tfm)
1138{
1139 return &tfm->base;
1140}
1141
Stephan Mueller58284f02014-11-12 05:29:36 +01001142/**
1143 * crypto_free_blkcipher() - zeroize and free the block cipher handle
1144 * @tfm: cipher handle to be freed
1145 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001146static inline void crypto_free_blkcipher(struct crypto_blkcipher *tfm)
1147{
1148 crypto_free_tfm(crypto_blkcipher_tfm(tfm));
1149}
1150
Stephan Mueller58284f02014-11-12 05:29:36 +01001151/**
1152 * crypto_has_blkcipher() - Search for the availability of a block cipher
1153 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1154 * block cipher
1155 * @type: specifies the type of the cipher
1156 * @mask: specifies the mask for the cipher
1157 *
1158 * Return: true when the block cipher is known to the kernel crypto API; false
1159 * otherwise
1160 */
Herbert Xufce32d72006-08-26 17:35:45 +10001161static inline int crypto_has_blkcipher(const char *alg_name, u32 type, u32 mask)
1162{
Herbert Xu332f88402007-11-15 22:36:07 +08001163 type &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xufce32d72006-08-26 17:35:45 +10001164 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
Herbert Xu332f88402007-11-15 22:36:07 +08001165 mask |= CRYPTO_ALG_TYPE_MASK;
Herbert Xufce32d72006-08-26 17:35:45 +10001166
1167 return crypto_has_alg(alg_name, type, mask);
1168}
1169
Stephan Mueller58284f02014-11-12 05:29:36 +01001170/**
1171 * crypto_blkcipher_name() - return the name / cra_name from the cipher handle
1172 * @tfm: cipher handle
1173 *
1174 * Return: The character string holding the name of the cipher
1175 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001176static inline const char *crypto_blkcipher_name(struct crypto_blkcipher *tfm)
1177{
1178 return crypto_tfm_alg_name(crypto_blkcipher_tfm(tfm));
1179}
1180
1181static inline struct blkcipher_tfm *crypto_blkcipher_crt(
1182 struct crypto_blkcipher *tfm)
1183{
1184 return &crypto_blkcipher_tfm(tfm)->crt_blkcipher;
1185}
1186
1187static inline struct blkcipher_alg *crypto_blkcipher_alg(
1188 struct crypto_blkcipher *tfm)
1189{
1190 return &crypto_blkcipher_tfm(tfm)->__crt_alg->cra_blkcipher;
1191}
1192
Stephan Mueller58284f02014-11-12 05:29:36 +01001193/**
1194 * crypto_blkcipher_ivsize() - obtain IV size
1195 * @tfm: cipher handle
1196 *
1197 * The size of the IV for the block cipher referenced by the cipher handle is
1198 * returned. This IV size may be zero if the cipher does not need an IV.
1199 *
1200 * Return: IV size in bytes
1201 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001202static inline unsigned int crypto_blkcipher_ivsize(struct crypto_blkcipher *tfm)
1203{
1204 return crypto_blkcipher_alg(tfm)->ivsize;
1205}
1206
Stephan Mueller58284f02014-11-12 05:29:36 +01001207/**
1208 * crypto_blkcipher_blocksize() - obtain block size of cipher
1209 * @tfm: cipher handle
1210 *
1211 * The block size for the block cipher referenced with the cipher handle is
1212 * returned. The caller may use that information to allocate appropriate
1213 * memory for the data returned by the encryption or decryption operation.
1214 *
1215 * Return: block size of cipher
1216 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001217static inline unsigned int crypto_blkcipher_blocksize(
1218 struct crypto_blkcipher *tfm)
1219{
1220 return crypto_tfm_alg_blocksize(crypto_blkcipher_tfm(tfm));
1221}
1222
1223static inline unsigned int crypto_blkcipher_alignmask(
1224 struct crypto_blkcipher *tfm)
1225{
1226 return crypto_tfm_alg_alignmask(crypto_blkcipher_tfm(tfm));
1227}
1228
1229static inline u32 crypto_blkcipher_get_flags(struct crypto_blkcipher *tfm)
1230{
1231 return crypto_tfm_get_flags(crypto_blkcipher_tfm(tfm));
1232}
1233
1234static inline void crypto_blkcipher_set_flags(struct crypto_blkcipher *tfm,
1235 u32 flags)
1236{
1237 crypto_tfm_set_flags(crypto_blkcipher_tfm(tfm), flags);
1238}
1239
1240static inline void crypto_blkcipher_clear_flags(struct crypto_blkcipher *tfm,
1241 u32 flags)
1242{
1243 crypto_tfm_clear_flags(crypto_blkcipher_tfm(tfm), flags);
1244}
1245
Stephan Mueller58284f02014-11-12 05:29:36 +01001246/**
1247 * crypto_blkcipher_setkey() - set key for cipher
1248 * @tfm: cipher handle
1249 * @key: buffer holding the key
1250 * @keylen: length of the key in bytes
1251 *
1252 * The caller provided key is set for the block cipher referenced by the cipher
1253 * handle.
1254 *
1255 * Note, the key length determines the cipher type. Many block ciphers implement
1256 * different cipher modes depending on the key size, such as AES-128 vs AES-192
1257 * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
1258 * is performed.
1259 *
1260 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
1261 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001262static inline int crypto_blkcipher_setkey(struct crypto_blkcipher *tfm,
1263 const u8 *key, unsigned int keylen)
1264{
1265 return crypto_blkcipher_crt(tfm)->setkey(crypto_blkcipher_tfm(tfm),
1266 key, keylen);
1267}
1268
Stephan Mueller58284f02014-11-12 05:29:36 +01001269/**
1270 * crypto_blkcipher_encrypt() - encrypt plaintext
1271 * @desc: reference to the block cipher handle with meta data
1272 * @dst: scatter/gather list that is filled by the cipher operation with the
1273 * ciphertext
1274 * @src: scatter/gather list that holds the plaintext
1275 * @nbytes: number of bytes of the plaintext to encrypt.
1276 *
1277 * Encrypt plaintext data using the IV set by the caller with a preceding
1278 * call of crypto_blkcipher_set_iv.
1279 *
1280 * The blkcipher_desc data structure must be filled by the caller and can
1281 * reside on the stack. The caller must fill desc as follows: desc.tfm is filled
1282 * with the block cipher handle; desc.flags is filled with either
1283 * CRYPTO_TFM_REQ_MAY_SLEEP or 0.
1284 *
1285 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1286 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001287static inline int crypto_blkcipher_encrypt(struct blkcipher_desc *desc,
1288 struct scatterlist *dst,
1289 struct scatterlist *src,
1290 unsigned int nbytes)
1291{
1292 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
1293 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
1294}
1295
Stephan Mueller58284f02014-11-12 05:29:36 +01001296/**
1297 * crypto_blkcipher_encrypt_iv() - encrypt plaintext with dedicated IV
1298 * @desc: reference to the block cipher handle with meta data
1299 * @dst: scatter/gather list that is filled by the cipher operation with the
1300 * ciphertext
1301 * @src: scatter/gather list that holds the plaintext
1302 * @nbytes: number of bytes of the plaintext to encrypt.
1303 *
1304 * Encrypt plaintext data with the use of an IV that is solely used for this
1305 * cipher operation. Any previously set IV is not used.
1306 *
1307 * The blkcipher_desc data structure must be filled by the caller and can
1308 * reside on the stack. The caller must fill desc as follows: desc.tfm is filled
1309 * with the block cipher handle; desc.info is filled with the IV to be used for
1310 * the current operation; desc.flags is filled with either
1311 * CRYPTO_TFM_REQ_MAY_SLEEP or 0.
1312 *
1313 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1314 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001315static inline int crypto_blkcipher_encrypt_iv(struct blkcipher_desc *desc,
1316 struct scatterlist *dst,
1317 struct scatterlist *src,
1318 unsigned int nbytes)
1319{
1320 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
1321}
1322
Stephan Mueller58284f02014-11-12 05:29:36 +01001323/**
1324 * crypto_blkcipher_decrypt() - decrypt ciphertext
1325 * @desc: reference to the block cipher handle with meta data
1326 * @dst: scatter/gather list that is filled by the cipher operation with the
1327 * plaintext
1328 * @src: scatter/gather list that holds the ciphertext
1329 * @nbytes: number of bytes of the ciphertext to decrypt.
1330 *
1331 * Decrypt ciphertext data using the IV set by the caller with a preceding
1332 * call of crypto_blkcipher_set_iv.
1333 *
1334 * The blkcipher_desc data structure must be filled by the caller as documented
1335 * for the crypto_blkcipher_encrypt call above.
1336 *
1337 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1338 *
1339 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001340static inline int crypto_blkcipher_decrypt(struct blkcipher_desc *desc,
1341 struct scatterlist *dst,
1342 struct scatterlist *src,
1343 unsigned int nbytes)
1344{
1345 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
1346 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
1347}
1348
Stephan Mueller58284f02014-11-12 05:29:36 +01001349/**
1350 * crypto_blkcipher_decrypt_iv() - decrypt ciphertext with dedicated IV
1351 * @desc: reference to the block cipher handle with meta data
1352 * @dst: scatter/gather list that is filled by the cipher operation with the
1353 * plaintext
1354 * @src: scatter/gather list that holds the ciphertext
1355 * @nbytes: number of bytes of the ciphertext to decrypt.
1356 *
1357 * Decrypt ciphertext data with the use of an IV that is solely used for this
1358 * cipher operation. Any previously set IV is not used.
1359 *
1360 * The blkcipher_desc data structure must be filled by the caller as documented
1361 * for the crypto_blkcipher_encrypt_iv call above.
1362 *
1363 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1364 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001365static inline int crypto_blkcipher_decrypt_iv(struct blkcipher_desc *desc,
1366 struct scatterlist *dst,
1367 struct scatterlist *src,
1368 unsigned int nbytes)
1369{
1370 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
1371}
1372
Stephan Mueller58284f02014-11-12 05:29:36 +01001373/**
1374 * crypto_blkcipher_set_iv() - set IV for cipher
1375 * @tfm: cipher handle
1376 * @src: buffer holding the IV
1377 * @len: length of the IV in bytes
1378 *
1379 * The caller provided IV is set for the block cipher referenced by the cipher
1380 * handle.
1381 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001382static inline void crypto_blkcipher_set_iv(struct crypto_blkcipher *tfm,
1383 const u8 *src, unsigned int len)
1384{
1385 memcpy(crypto_blkcipher_crt(tfm)->iv, src, len);
1386}
1387
Stephan Mueller58284f02014-11-12 05:29:36 +01001388/**
1389 * crypto_blkcipher_get_iv() - obtain IV from cipher
1390 * @tfm: cipher handle
1391 * @dst: buffer filled with the IV
1392 * @len: length of the buffer dst
1393 *
1394 * The caller can obtain the IV set for the block cipher referenced by the
1395 * cipher handle and store it into the user-provided buffer. If the buffer
1396 * has an insufficient space, the IV is truncated to fit the buffer.
1397 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001398static inline void crypto_blkcipher_get_iv(struct crypto_blkcipher *tfm,
1399 u8 *dst, unsigned int len)
1400{
1401 memcpy(dst, crypto_blkcipher_crt(tfm)->iv, len);
1402}
1403
Stephan Mueller16e61032014-11-12 05:30:06 +01001404/**
1405 * DOC: Single Block Cipher API
1406 *
1407 * The single block cipher API is used with the ciphers of type
1408 * CRYPTO_ALG_TYPE_CIPHER (listed as type "cipher" in /proc/crypto).
1409 *
1410 * Using the single block cipher API calls, operations with the basic cipher
1411 * primitive can be implemented. These cipher primitives exclude any block
1412 * chaining operations including IV handling.
1413 *
1414 * The purpose of this single block cipher API is to support the implementation
1415 * of templates or other concepts that only need to perform the cipher operation
1416 * on one block at a time. Templates invoke the underlying cipher primitive
1417 * block-wise and process either the input or the output data of these cipher
1418 * operations.
1419 */
1420
Herbert Xuf28776a2006-08-13 20:58:18 +10001421static inline struct crypto_cipher *__crypto_cipher_cast(struct crypto_tfm *tfm)
1422{
1423 return (struct crypto_cipher *)tfm;
1424}
1425
1426static inline struct crypto_cipher *crypto_cipher_cast(struct crypto_tfm *tfm)
1427{
1428 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
1429 return __crypto_cipher_cast(tfm);
1430}
1431
Stephan Mueller16e61032014-11-12 05:30:06 +01001432/**
1433 * crypto_alloc_cipher() - allocate single block cipher handle
1434 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1435 * single block cipher
1436 * @type: specifies the type of the cipher
1437 * @mask: specifies the mask for the cipher
1438 *
1439 * Allocate a cipher handle for a single block cipher. The returned struct
1440 * crypto_cipher is the cipher handle that is required for any subsequent API
1441 * invocation for that single block cipher.
1442 *
1443 * Return: allocated cipher handle in case of success; IS_ERR() is true in case
1444 * of an error, PTR_ERR() returns the error code.
1445 */
Herbert Xuf28776a2006-08-13 20:58:18 +10001446static inline struct crypto_cipher *crypto_alloc_cipher(const char *alg_name,
1447 u32 type, u32 mask)
1448{
1449 type &= ~CRYPTO_ALG_TYPE_MASK;
1450 type |= CRYPTO_ALG_TYPE_CIPHER;
1451 mask |= CRYPTO_ALG_TYPE_MASK;
1452
1453 return __crypto_cipher_cast(crypto_alloc_base(alg_name, type, mask));
1454}
1455
1456static inline struct crypto_tfm *crypto_cipher_tfm(struct crypto_cipher *tfm)
1457{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001458 return &tfm->base;
Herbert Xuf28776a2006-08-13 20:58:18 +10001459}
1460
Stephan Mueller16e61032014-11-12 05:30:06 +01001461/**
1462 * crypto_free_cipher() - zeroize and free the single block cipher handle
1463 * @tfm: cipher handle to be freed
1464 */
Herbert Xuf28776a2006-08-13 20:58:18 +10001465static inline void crypto_free_cipher(struct crypto_cipher *tfm)
1466{
1467 crypto_free_tfm(crypto_cipher_tfm(tfm));
1468}
1469
Stephan Mueller16e61032014-11-12 05:30:06 +01001470/**
1471 * crypto_has_cipher() - Search for the availability of a single block cipher
1472 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1473 * single block cipher
1474 * @type: specifies the type of the cipher
1475 * @mask: specifies the mask for the cipher
1476 *
1477 * Return: true when the single block cipher is known to the kernel crypto API;
1478 * false otherwise
1479 */
Herbert Xufce32d72006-08-26 17:35:45 +10001480static inline int crypto_has_cipher(const char *alg_name, u32 type, u32 mask)
1481{
1482 type &= ~CRYPTO_ALG_TYPE_MASK;
1483 type |= CRYPTO_ALG_TYPE_CIPHER;
1484 mask |= CRYPTO_ALG_TYPE_MASK;
1485
1486 return crypto_has_alg(alg_name, type, mask);
1487}
1488
Herbert Xuf28776a2006-08-13 20:58:18 +10001489static inline struct cipher_tfm *crypto_cipher_crt(struct crypto_cipher *tfm)
1490{
1491 return &crypto_cipher_tfm(tfm)->crt_cipher;
1492}
1493
Stephan Mueller16e61032014-11-12 05:30:06 +01001494/**
1495 * crypto_cipher_blocksize() - obtain block size for cipher
1496 * @tfm: cipher handle
1497 *
1498 * The block size for the single block cipher referenced with the cipher handle
1499 * tfm is returned. The caller may use that information to allocate appropriate
1500 * memory for the data returned by the encryption or decryption operation
1501 *
1502 * Return: block size of cipher
1503 */
Herbert Xuf28776a2006-08-13 20:58:18 +10001504static inline unsigned int crypto_cipher_blocksize(struct crypto_cipher *tfm)
1505{
1506 return crypto_tfm_alg_blocksize(crypto_cipher_tfm(tfm));
1507}
1508
1509static inline unsigned int crypto_cipher_alignmask(struct crypto_cipher *tfm)
1510{
1511 return crypto_tfm_alg_alignmask(crypto_cipher_tfm(tfm));
1512}
1513
1514static inline u32 crypto_cipher_get_flags(struct crypto_cipher *tfm)
1515{
1516 return crypto_tfm_get_flags(crypto_cipher_tfm(tfm));
1517}
1518
1519static inline void crypto_cipher_set_flags(struct crypto_cipher *tfm,
1520 u32 flags)
1521{
1522 crypto_tfm_set_flags(crypto_cipher_tfm(tfm), flags);
1523}
1524
1525static inline void crypto_cipher_clear_flags(struct crypto_cipher *tfm,
1526 u32 flags)
1527{
1528 crypto_tfm_clear_flags(crypto_cipher_tfm(tfm), flags);
1529}
1530
Stephan Mueller16e61032014-11-12 05:30:06 +01001531/**
1532 * crypto_cipher_setkey() - set key for cipher
1533 * @tfm: cipher handle
1534 * @key: buffer holding the key
1535 * @keylen: length of the key in bytes
1536 *
1537 * The caller provided key is set for the single block cipher referenced by the
1538 * cipher handle.
1539 *
1540 * Note, the key length determines the cipher type. Many block ciphers implement
1541 * different cipher modes depending on the key size, such as AES-128 vs AES-192
1542 * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
1543 * is performed.
1544 *
1545 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
1546 */
Herbert Xu7226bc872006-08-21 21:40:49 +10001547static inline int crypto_cipher_setkey(struct crypto_cipher *tfm,
1548 const u8 *key, unsigned int keylen)
1549{
1550 return crypto_cipher_crt(tfm)->cit_setkey(crypto_cipher_tfm(tfm),
1551 key, keylen);
1552}
1553
Stephan Mueller16e61032014-11-12 05:30:06 +01001554/**
1555 * crypto_cipher_encrypt_one() - encrypt one block of plaintext
1556 * @tfm: cipher handle
1557 * @dst: points to the buffer that will be filled with the ciphertext
1558 * @src: buffer holding the plaintext to be encrypted
1559 *
1560 * Invoke the encryption operation of one block. The caller must ensure that
1561 * the plaintext and ciphertext buffers are at least one block in size.
1562 */
Herbert Xuf28776a2006-08-13 20:58:18 +10001563static inline void crypto_cipher_encrypt_one(struct crypto_cipher *tfm,
1564 u8 *dst, const u8 *src)
1565{
1566 crypto_cipher_crt(tfm)->cit_encrypt_one(crypto_cipher_tfm(tfm),
1567 dst, src);
1568}
1569
Stephan Mueller16e61032014-11-12 05:30:06 +01001570/**
1571 * crypto_cipher_decrypt_one() - decrypt one block of ciphertext
1572 * @tfm: cipher handle
1573 * @dst: points to the buffer that will be filled with the plaintext
1574 * @src: buffer holding the ciphertext to be decrypted
1575 *
1576 * Invoke the decryption operation of one block. The caller must ensure that
1577 * the plaintext and ciphertext buffers are at least one block in size.
1578 */
Herbert Xuf28776a2006-08-13 20:58:18 +10001579static inline void crypto_cipher_decrypt_one(struct crypto_cipher *tfm,
1580 u8 *dst, const u8 *src)
1581{
1582 crypto_cipher_crt(tfm)->cit_decrypt_one(crypto_cipher_tfm(tfm),
1583 dst, src);
1584}
1585
Stephan Mueller47ca5be2014-11-12 05:30:42 +01001586/**
1587 * DOC: Synchronous Message Digest API
1588 *
1589 * The synchronous message digest API is used with the ciphers of type
1590 * CRYPTO_ALG_TYPE_HASH (listed as type "hash" in /proc/crypto)
1591 */
1592
Herbert Xu055bcee2006-08-19 22:24:23 +10001593static inline struct crypto_hash *__crypto_hash_cast(struct crypto_tfm *tfm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594{
Herbert Xu055bcee2006-08-19 22:24:23 +10001595 return (struct crypto_hash *)tfm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596}
1597
Herbert Xu055bcee2006-08-19 22:24:23 +10001598static inline struct crypto_hash *crypto_hash_cast(struct crypto_tfm *tfm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599{
Herbert Xu055bcee2006-08-19 22:24:23 +10001600 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_HASH) &
1601 CRYPTO_ALG_TYPE_HASH_MASK);
1602 return __crypto_hash_cast(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603}
1604
Stephan Mueller47ca5be2014-11-12 05:30:42 +01001605/**
1606 * crypto_alloc_hash() - allocate synchronous message digest handle
1607 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1608 * message digest cipher
1609 * @type: specifies the type of the cipher
1610 * @mask: specifies the mask for the cipher
1611 *
1612 * Allocate a cipher handle for a message digest. The returned struct
1613 * crypto_hash is the cipher handle that is required for any subsequent
1614 * API invocation for that message digest.
1615 *
1616 * Return: allocated cipher handle in case of success; IS_ERR() is true in case
1617 * of an error, PTR_ERR() returns the error code.
1618 */
Herbert Xu055bcee2006-08-19 22:24:23 +10001619static inline struct crypto_hash *crypto_alloc_hash(const char *alg_name,
1620 u32 type, u32 mask)
1621{
1622 type &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xu551a09a2007-12-01 21:47:07 +11001623 mask &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xu055bcee2006-08-19 22:24:23 +10001624 type |= CRYPTO_ALG_TYPE_HASH;
1625 mask |= CRYPTO_ALG_TYPE_HASH_MASK;
1626
1627 return __crypto_hash_cast(crypto_alloc_base(alg_name, type, mask));
1628}
1629
1630static inline struct crypto_tfm *crypto_hash_tfm(struct crypto_hash *tfm)
1631{
1632 return &tfm->base;
1633}
1634
Stephan Mueller47ca5be2014-11-12 05:30:42 +01001635/**
1636 * crypto_free_hash() - zeroize and free message digest handle
1637 * @tfm: cipher handle to be freed
1638 */
Herbert Xu055bcee2006-08-19 22:24:23 +10001639static inline void crypto_free_hash(struct crypto_hash *tfm)
1640{
1641 crypto_free_tfm(crypto_hash_tfm(tfm));
1642}
1643
Stephan Mueller47ca5be2014-11-12 05:30:42 +01001644/**
1645 * crypto_has_hash() - Search for the availability of a message digest
1646 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1647 * message digest cipher
1648 * @type: specifies the type of the cipher
1649 * @mask: specifies the mask for the cipher
1650 *
1651 * Return: true when the message digest cipher is known to the kernel crypto
1652 * API; false otherwise
1653 */
Herbert Xufce32d72006-08-26 17:35:45 +10001654static inline int crypto_has_hash(const char *alg_name, u32 type, u32 mask)
1655{
1656 type &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xu551a09a2007-12-01 21:47:07 +11001657 mask &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xufce32d72006-08-26 17:35:45 +10001658 type |= CRYPTO_ALG_TYPE_HASH;
1659 mask |= CRYPTO_ALG_TYPE_HASH_MASK;
1660
1661 return crypto_has_alg(alg_name, type, mask);
1662}
1663
Herbert Xu055bcee2006-08-19 22:24:23 +10001664static inline struct hash_tfm *crypto_hash_crt(struct crypto_hash *tfm)
1665{
1666 return &crypto_hash_tfm(tfm)->crt_hash;
1667}
1668
Stephan Mueller47ca5be2014-11-12 05:30:42 +01001669/**
1670 * crypto_hash_blocksize() - obtain block size for message digest
1671 * @tfm: cipher handle
1672 *
1673 * The block size for the message digest cipher referenced with the cipher
1674 * handle is returned.
1675 *
1676 * Return: block size of cipher
1677 */
Herbert Xu055bcee2006-08-19 22:24:23 +10001678static inline unsigned int crypto_hash_blocksize(struct crypto_hash *tfm)
1679{
1680 return crypto_tfm_alg_blocksize(crypto_hash_tfm(tfm));
1681}
1682
1683static inline unsigned int crypto_hash_alignmask(struct crypto_hash *tfm)
1684{
1685 return crypto_tfm_alg_alignmask(crypto_hash_tfm(tfm));
1686}
1687
Stephan Mueller47ca5be2014-11-12 05:30:42 +01001688/**
1689 * crypto_hash_digestsize() - obtain message digest size
1690 * @tfm: cipher handle
1691 *
1692 * The size for the message digest created by the message digest cipher
1693 * referenced with the cipher handle is returned.
1694 *
1695 * Return: message digest size
1696 */
Herbert Xu055bcee2006-08-19 22:24:23 +10001697static inline unsigned int crypto_hash_digestsize(struct crypto_hash *tfm)
1698{
1699 return crypto_hash_crt(tfm)->digestsize;
1700}
1701
1702static inline u32 crypto_hash_get_flags(struct crypto_hash *tfm)
1703{
1704 return crypto_tfm_get_flags(crypto_hash_tfm(tfm));
1705}
1706
1707static inline void crypto_hash_set_flags(struct crypto_hash *tfm, u32 flags)
1708{
1709 crypto_tfm_set_flags(crypto_hash_tfm(tfm), flags);
1710}
1711
1712static inline void crypto_hash_clear_flags(struct crypto_hash *tfm, u32 flags)
1713{
1714 crypto_tfm_clear_flags(crypto_hash_tfm(tfm), flags);
1715}
1716
Stephan Mueller47ca5be2014-11-12 05:30:42 +01001717/**
1718 * crypto_hash_init() - (re)initialize message digest handle
1719 * @desc: cipher request handle that to be filled by caller --
1720 * desc.tfm is filled with the hash cipher handle;
1721 * desc.flags is filled with either CRYPTO_TFM_REQ_MAY_SLEEP or 0.
1722 *
1723 * The call (re-)initializes the message digest referenced by the hash cipher
1724 * request handle. Any potentially existing state created by previous
1725 * operations is discarded.
1726 *
1727 * Return: 0 if the message digest initialization was successful; < 0 if an
1728 * error occurred
1729 */
Herbert Xu055bcee2006-08-19 22:24:23 +10001730static inline int crypto_hash_init(struct hash_desc *desc)
1731{
1732 return crypto_hash_crt(desc->tfm)->init(desc);
1733}
1734
Stephan Mueller47ca5be2014-11-12 05:30:42 +01001735/**
1736 * crypto_hash_update() - add data to message digest for processing
1737 * @desc: cipher request handle
1738 * @sg: scatter / gather list pointing to the data to be added to the message
1739 * digest
1740 * @nbytes: number of bytes to be processed from @sg
1741 *
1742 * Updates the message digest state of the cipher handle pointed to by the
1743 * hash cipher request handle with the input data pointed to by the
1744 * scatter/gather list.
1745 *
1746 * Return: 0 if the message digest update was successful; < 0 if an error
1747 * occurred
1748 */
Herbert Xu055bcee2006-08-19 22:24:23 +10001749static inline int crypto_hash_update(struct hash_desc *desc,
1750 struct scatterlist *sg,
1751 unsigned int nbytes)
1752{
1753 return crypto_hash_crt(desc->tfm)->update(desc, sg, nbytes);
1754}
1755
Stephan Mueller47ca5be2014-11-12 05:30:42 +01001756/**
1757 * crypto_hash_final() - calculate message digest
1758 * @desc: cipher request handle
1759 * @out: message digest output buffer -- The caller must ensure that the out
1760 * buffer has a sufficient size (e.g. by using the crypto_hash_digestsize
1761 * function).
1762 *
1763 * Finalize the message digest operation and create the message digest
1764 * based on all data added to the cipher handle. The message digest is placed
1765 * into the output buffer.
1766 *
1767 * Return: 0 if the message digest creation was successful; < 0 if an error
1768 * occurred
1769 */
Herbert Xu055bcee2006-08-19 22:24:23 +10001770static inline int crypto_hash_final(struct hash_desc *desc, u8 *out)
1771{
1772 return crypto_hash_crt(desc->tfm)->final(desc, out);
1773}
1774
Stephan Mueller47ca5be2014-11-12 05:30:42 +01001775/**
1776 * crypto_hash_digest() - calculate message digest for a buffer
1777 * @desc: see crypto_hash_final()
1778 * @sg: see crypto_hash_update()
1779 * @nbytes: see crypto_hash_update()
1780 * @out: see crypto_hash_final()
1781 *
1782 * This function is a "short-hand" for the function calls of crypto_hash_init,
1783 * crypto_hash_update and crypto_hash_final. The parameters have the same
1784 * meaning as discussed for those separate three functions.
1785 *
1786 * Return: 0 if the message digest creation was successful; < 0 if an error
1787 * occurred
1788 */
Herbert Xu055bcee2006-08-19 22:24:23 +10001789static inline int crypto_hash_digest(struct hash_desc *desc,
1790 struct scatterlist *sg,
1791 unsigned int nbytes, u8 *out)
1792{
1793 return crypto_hash_crt(desc->tfm)->digest(desc, sg, nbytes, out);
1794}
1795
Stephan Mueller47ca5be2014-11-12 05:30:42 +01001796/**
1797 * crypto_hash_setkey() - set key for message digest
1798 * @hash: cipher handle
1799 * @key: buffer holding the key
1800 * @keylen: length of the key in bytes
1801 *
1802 * The caller provided key is set for the message digest cipher. The cipher
1803 * handle must point to a keyed hash in order for this function to succeed.
1804 *
1805 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
1806 */
Herbert Xu055bcee2006-08-19 22:24:23 +10001807static inline int crypto_hash_setkey(struct crypto_hash *hash,
1808 const u8 *key, unsigned int keylen)
1809{
1810 return crypto_hash_crt(hash)->setkey(hash, key, keylen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811}
1812
Herbert Xufce32d72006-08-26 17:35:45 +10001813static inline struct crypto_comp *__crypto_comp_cast(struct crypto_tfm *tfm)
1814{
1815 return (struct crypto_comp *)tfm;
1816}
1817
1818static inline struct crypto_comp *crypto_comp_cast(struct crypto_tfm *tfm)
1819{
1820 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_COMPRESS) &
1821 CRYPTO_ALG_TYPE_MASK);
1822 return __crypto_comp_cast(tfm);
1823}
1824
1825static inline struct crypto_comp *crypto_alloc_comp(const char *alg_name,
1826 u32 type, u32 mask)
1827{
1828 type &= ~CRYPTO_ALG_TYPE_MASK;
1829 type |= CRYPTO_ALG_TYPE_COMPRESS;
1830 mask |= CRYPTO_ALG_TYPE_MASK;
1831
1832 return __crypto_comp_cast(crypto_alloc_base(alg_name, type, mask));
1833}
1834
1835static inline struct crypto_tfm *crypto_comp_tfm(struct crypto_comp *tfm)
1836{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001837 return &tfm->base;
Herbert Xufce32d72006-08-26 17:35:45 +10001838}
1839
1840static inline void crypto_free_comp(struct crypto_comp *tfm)
1841{
1842 crypto_free_tfm(crypto_comp_tfm(tfm));
1843}
1844
1845static inline int crypto_has_comp(const char *alg_name, u32 type, u32 mask)
1846{
1847 type &= ~CRYPTO_ALG_TYPE_MASK;
1848 type |= CRYPTO_ALG_TYPE_COMPRESS;
1849 mask |= CRYPTO_ALG_TYPE_MASK;
1850
1851 return crypto_has_alg(alg_name, type, mask);
1852}
1853
Herbert Xue4d5b792006-08-26 18:12:40 +10001854static inline const char *crypto_comp_name(struct crypto_comp *tfm)
1855{
1856 return crypto_tfm_alg_name(crypto_comp_tfm(tfm));
1857}
1858
Herbert Xufce32d72006-08-26 17:35:45 +10001859static inline struct compress_tfm *crypto_comp_crt(struct crypto_comp *tfm)
1860{
1861 return &crypto_comp_tfm(tfm)->crt_compress;
1862}
1863
1864static inline int crypto_comp_compress(struct crypto_comp *tfm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 const u8 *src, unsigned int slen,
1866 u8 *dst, unsigned int *dlen)
1867{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001868 return crypto_comp_crt(tfm)->cot_compress(crypto_comp_tfm(tfm),
1869 src, slen, dst, dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870}
1871
Herbert Xufce32d72006-08-26 17:35:45 +10001872static inline int crypto_comp_decompress(struct crypto_comp *tfm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 const u8 *src, unsigned int slen,
1874 u8 *dst, unsigned int *dlen)
1875{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001876 return crypto_comp_crt(tfm)->cot_decompress(crypto_comp_tfm(tfm),
1877 src, slen, dst, dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878}
1879
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880#endif /* _LINUX_CRYPTO_H */
1881