blob: 8edb3ba6f640064eafb0356e91579d72e621048b [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 Xu4e6c3df2016-07-12 13:17:31 +080050#define CRYPTO_ALG_TYPE_SKCIPHER 0x00000005
Herbert Xu61da88e2007-12-17 21:51:27 +080051#define CRYPTO_ALG_TYPE_GIVCIPHER 0x00000006
Salvatore Benedetto4e5f2c42016-06-22 17:49:13 +010052#define CRYPTO_ALG_TYPE_KPP 0x00000008
Neil Horman17f0f4a2008-08-14 22:15:52 +100053#define CRYPTO_ALG_TYPE_RNG 0x0000000c
Tadeusz Struk3c339ab2015-06-16 10:30:55 -070054#define CRYPTO_ALG_TYPE_AKCIPHER 0x0000000d
Giovanni Cabiddu63044c42016-06-02 13:28:55 +010055#define CRYPTO_ALG_TYPE_DIGEST 0x0000000e
56#define CRYPTO_ALG_TYPE_HASH 0x0000000e
57#define CRYPTO_ALG_TYPE_SHASH 0x0000000e
58#define CRYPTO_ALG_TYPE_AHASH 0x0000000f
Herbert Xu055bcee2006-08-19 22:24:23 +100059
60#define CRYPTO_ALG_TYPE_HASH_MASK 0x0000000e
Giovanni Cabiddu63044c42016-06-02 13:28:55 +010061#define CRYPTO_ALG_TYPE_AHASH_MASK 0x0000000e
Herbert Xu332f88402007-11-15 22:36:07 +080062#define CRYPTO_ALG_TYPE_BLKCIPHER_MASK 0x0000000c
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Herbert Xu28259822006-08-06 21:23:26 +100064#define CRYPTO_ALG_LARVAL 0x00000010
Herbert Xu6bfd4802006-09-21 11:39:29 +100065#define CRYPTO_ALG_DEAD 0x00000020
66#define CRYPTO_ALG_DYING 0x00000040
Herbert Xuf3f632d2006-08-06 23:12:59 +100067#define CRYPTO_ALG_ASYNC 0x00000080
Herbert Xu28259822006-08-06 21:23:26 +100068
Linus Torvalds1da177e2005-04-16 15:20:36 -070069/*
Herbert Xu60104392006-08-26 18:34:10 +100070 * Set this bit if and only if the algorithm requires another algorithm of
71 * the same type to handle corner cases.
72 */
73#define CRYPTO_ALG_NEED_FALLBACK 0x00000100
74
75/*
Herbert Xuecfc4322007-12-05 21:08:36 +110076 * This bit is set for symmetric key ciphers that have already been wrapped
77 * with a generic IV generator to prevent them from being wrapped again.
78 */
79#define CRYPTO_ALG_GENIV 0x00000200
80
81/*
Herbert Xu73d38642008-08-03 21:15:23 +080082 * Set if the algorithm has passed automated run-time testing. Note that
83 * if there is no run-time testing for a given algorithm it is considered
84 * to have passed.
85 */
86
87#define CRYPTO_ALG_TESTED 0x00000400
88
89/*
Steffen Klassert64a947b2011-09-27 07:21:26 +020090 * Set if the algorithm is an instance that is build from templates.
91 */
92#define CRYPTO_ALG_INSTANCE 0x00000800
93
Nikos Mavrogiannopoulosd912bb72011-11-01 13:39:56 +010094/* Set this bit if the algorithm provided is hardware accelerated but
95 * not available to userspace via instruction set or so.
96 */
97#define CRYPTO_ALG_KERN_DRIVER_ONLY 0x00001000
98
Steffen Klassert64a947b2011-09-27 07:21:26 +020099/*
Stephan Mueller06ca7f62015-03-30 21:55:52 +0200100 * Mark a cipher as a service implementation only usable by another
101 * cipher and never by a normal user of the kernel crypto API
102 */
103#define CRYPTO_ALG_INTERNAL 0x00002000
104
105/*
Eric Biggersb392a532018-01-03 11:16:26 -0800106 * Set if the algorithm has a ->setkey() method but can be used without
107 * calling it first, i.e. there is a default key.
108 */
109#define CRYPTO_ALG_OPTIONAL_KEY 0x00004000
110
111/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 * Transform masks and values (for crt_flags).
113 */
Eric Biggersadf26e82018-01-03 11:16:27 -0800114#define CRYPTO_TFM_NEED_KEY 0x00000001
115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116#define CRYPTO_TFM_REQ_MASK 0x000fff00
117#define CRYPTO_TFM_RES_MASK 0xfff00000
118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119#define CRYPTO_TFM_REQ_WEAK_KEY 0x00000100
Herbert Xu64baf3c2005-09-01 17:43:05 -0700120#define CRYPTO_TFM_REQ_MAY_SLEEP 0x00000200
Herbert Xu32e39832007-03-24 14:35:34 +1100121#define CRYPTO_TFM_REQ_MAY_BACKLOG 0x00000400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122#define CRYPTO_TFM_RES_WEAK_KEY 0x00100000
123#define CRYPTO_TFM_RES_BAD_KEY_LEN 0x00200000
124#define CRYPTO_TFM_RES_BAD_KEY_SCHED 0x00400000
125#define CRYPTO_TFM_RES_BAD_BLOCK_LEN 0x00800000
126#define CRYPTO_TFM_RES_BAD_FLAGS 0x01000000
127
128/*
129 * Miscellaneous stuff.
130 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131#define CRYPTO_MAX_ALG_NAME 64
132
Herbert Xu79911102006-08-21 21:03:52 +1000133/*
134 * The macro CRYPTO_MINALIGN_ATTR (along with the void * type in the actual
135 * declaration) is used to ensure that the crypto_tfm context structure is
136 * aligned correctly for the given architecture so that there are no alignment
137 * faults for C data types. In particular, this is required on platforms such
138 * as arm where pointers are 32-bit aligned but there are data types such as
139 * u64 which require 64-bit alignment.
140 */
Herbert Xu79911102006-08-21 21:03:52 +1000141#define CRYPTO_MINALIGN ARCH_KMALLOC_MINALIGN
Herbert Xu79911102006-08-21 21:03:52 +1000142
Herbert Xu79911102006-08-21 21:03:52 +1000143#define CRYPTO_MINALIGN_ATTR __attribute__ ((__aligned__(CRYPTO_MINALIGN)))
Herbert Xu79911102006-08-21 21:03:52 +1000144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145struct scatterlist;
Herbert Xu32e39832007-03-24 14:35:34 +1100146struct crypto_ablkcipher;
147struct crypto_async_request;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000148struct crypto_blkcipher;
Herbert Xu40725182005-07-06 13:51:52 -0700149struct crypto_tfm;
Herbert Xue853c3c2006-08-22 00:06:54 +1000150struct crypto_type;
Herbert Xu61da88e2007-12-17 21:51:27 +0800151struct skcipher_givcrypt_request;
Herbert Xu40725182005-07-06 13:51:52 -0700152
Herbert Xu32e39832007-03-24 14:35:34 +1100153typedef void (*crypto_completion_t)(struct crypto_async_request *req, int err);
154
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100155/**
156 * DOC: Block Cipher Context Data Structures
157 *
158 * These data structures define the operating context for each block cipher
159 * type.
160 */
161
Herbert Xu32e39832007-03-24 14:35:34 +1100162struct crypto_async_request {
163 struct list_head list;
164 crypto_completion_t complete;
165 void *data;
166 struct crypto_tfm *tfm;
167
168 u32 flags;
169};
170
171struct ablkcipher_request {
172 struct crypto_async_request base;
173
174 unsigned int nbytes;
175
176 void *info;
177
178 struct scatterlist *src;
179 struct scatterlist *dst;
180
181 void *__ctx[] CRYPTO_MINALIGN_ATTR;
182};
183
Herbert Xu5cde0af2006-08-22 00:07:53 +1000184struct blkcipher_desc {
185 struct crypto_blkcipher *tfm;
186 void *info;
187 u32 flags;
188};
189
Herbert Xu40725182005-07-06 13:51:52 -0700190struct cipher_desc {
191 struct crypto_tfm *tfm;
Herbert Xu6c2bb982006-05-16 22:09:29 +1000192 void (*crfn)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Herbert Xu40725182005-07-06 13:51:52 -0700193 unsigned int (*prfn)(const struct cipher_desc *desc, u8 *dst,
194 const u8 *src, unsigned int nbytes);
195 void *info;
196};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100198/**
199 * DOC: Block Cipher Algorithm Definitions
200 *
201 * These data structures define modular crypto algorithm implementations,
202 * managed via crypto_register_alg() and crypto_unregister_alg().
203 */
204
205/**
206 * struct ablkcipher_alg - asynchronous block cipher definition
207 * @min_keysize: Minimum key size supported by the transformation. This is the
208 * smallest key length supported by this transformation algorithm.
209 * This must be set to one of the pre-defined values as this is
210 * not hardware specific. Possible values for this field can be
211 * found via git grep "_MIN_KEY_SIZE" include/crypto/
212 * @max_keysize: Maximum key size supported by the transformation. This is the
213 * largest key length supported by this transformation algorithm.
214 * This must be set to one of the pre-defined values as this is
215 * not hardware specific. Possible values for this field can be
216 * found via git grep "_MAX_KEY_SIZE" include/crypto/
217 * @setkey: Set key for the transformation. This function is used to either
218 * program a supplied key into the hardware or store the key in the
219 * transformation context for programming it later. Note that this
220 * function does modify the transformation context. This function can
221 * be called multiple times during the existence of the transformation
222 * object, so one must make sure the key is properly reprogrammed into
223 * the hardware. This function is also responsible for checking the key
224 * length for validity. In case a software fallback was put in place in
225 * the @cra_init call, this function might need to use the fallback if
226 * the algorithm doesn't support all of the key sizes.
227 * @encrypt: Encrypt a scatterlist of blocks. This function is used to encrypt
228 * the supplied scatterlist containing the blocks of data. The crypto
229 * API consumer is responsible for aligning the entries of the
230 * scatterlist properly and making sure the chunks are correctly
231 * sized. In case a software fallback was put in place in the
232 * @cra_init call, this function might need to use the fallback if
233 * the algorithm doesn't support all of the key sizes. In case the
234 * key was stored in transformation context, the key might need to be
235 * re-programmed into the hardware in this function. This function
236 * shall not modify the transformation context, as this function may
237 * be called in parallel with the same transformation object.
238 * @decrypt: Decrypt a single block. This is a reverse counterpart to @encrypt
239 * and the conditions are exactly the same.
240 * @givencrypt: Update the IV for encryption. With this function, a cipher
241 * implementation may provide the function on how to update the IV
242 * for encryption.
243 * @givdecrypt: Update the IV for decryption. This is the reverse of
244 * @givencrypt .
245 * @geniv: The transformation implementation may use an "IV generator" provided
246 * by the kernel crypto API. Several use cases have a predefined
247 * approach how IVs are to be updated. For such use cases, the kernel
248 * crypto API provides ready-to-use implementations that can be
249 * referenced with this variable.
250 * @ivsize: IV size applicable for transformation. The consumer must provide an
251 * IV of exactly that size to perform the encrypt or decrypt operation.
252 *
253 * All fields except @givencrypt , @givdecrypt , @geniv and @ivsize are
254 * mandatory and must be filled.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 */
Herbert Xub5b7f082007-04-16 20:48:54 +1000256struct ablkcipher_alg {
257 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
258 unsigned int keylen);
259 int (*encrypt)(struct ablkcipher_request *req);
260 int (*decrypt)(struct ablkcipher_request *req);
Herbert Xu61da88e2007-12-17 21:51:27 +0800261 int (*givencrypt)(struct skcipher_givcrypt_request *req);
262 int (*givdecrypt)(struct skcipher_givcrypt_request *req);
Herbert Xub5b7f082007-04-16 20:48:54 +1000263
Herbert Xu23508e12007-11-27 21:33:24 +0800264 const char *geniv;
265
Herbert Xub5b7f082007-04-16 20:48:54 +1000266 unsigned int min_keysize;
267 unsigned int max_keysize;
268 unsigned int ivsize;
269};
270
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100271/**
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100272 * struct blkcipher_alg - synchronous block cipher definition
273 * @min_keysize: see struct ablkcipher_alg
274 * @max_keysize: see struct ablkcipher_alg
275 * @setkey: see struct ablkcipher_alg
276 * @encrypt: see struct ablkcipher_alg
277 * @decrypt: see struct ablkcipher_alg
278 * @geniv: see struct ablkcipher_alg
279 * @ivsize: see struct ablkcipher_alg
280 *
281 * All fields except @geniv and @ivsize are mandatory and must be filled.
282 */
Herbert Xu5cde0af2006-08-22 00:07:53 +1000283struct blkcipher_alg {
284 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
285 unsigned int keylen);
286 int (*encrypt)(struct blkcipher_desc *desc,
287 struct scatterlist *dst, struct scatterlist *src,
288 unsigned int nbytes);
289 int (*decrypt)(struct blkcipher_desc *desc,
290 struct scatterlist *dst, struct scatterlist *src,
291 unsigned int nbytes);
292
Herbert Xu23508e12007-11-27 21:33:24 +0800293 const char *geniv;
294
Herbert Xu5cde0af2006-08-22 00:07:53 +1000295 unsigned int min_keysize;
296 unsigned int max_keysize;
297 unsigned int ivsize;
298};
299
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100300/**
301 * struct cipher_alg - single-block symmetric ciphers definition
302 * @cia_min_keysize: Minimum key size supported by the transformation. This is
303 * the smallest key length supported by this transformation
304 * algorithm. This must be set to one of the pre-defined
305 * values as this is not hardware specific. Possible values
306 * for this field can be found via git grep "_MIN_KEY_SIZE"
307 * include/crypto/
308 * @cia_max_keysize: Maximum key size supported by the transformation. This is
309 * the largest key length supported by this transformation
310 * algorithm. This must be set to one of the pre-defined values
311 * as this is not hardware specific. Possible values for this
312 * field can be found via git grep "_MAX_KEY_SIZE"
313 * include/crypto/
314 * @cia_setkey: Set key for the transformation. This function is used to either
315 * program a supplied key into the hardware or store the key in the
316 * transformation context for programming it later. Note that this
317 * function does modify the transformation context. This function
318 * can be called multiple times during the existence of the
319 * transformation object, so one must make sure the key is properly
320 * reprogrammed into the hardware. This function is also
321 * responsible for checking the key length for validity.
322 * @cia_encrypt: Encrypt a single block. This function is used to encrypt a
323 * single block of data, which must be @cra_blocksize big. This
324 * always operates on a full @cra_blocksize and it is not possible
325 * to encrypt a block of smaller size. The supplied buffers must
326 * therefore also be at least of @cra_blocksize size. Both the
327 * input and output buffers are always aligned to @cra_alignmask.
328 * In case either of the input or output buffer supplied by user
329 * of the crypto API is not aligned to @cra_alignmask, the crypto
330 * API will re-align the buffers. The re-alignment means that a
331 * new buffer will be allocated, the data will be copied into the
332 * new buffer, then the processing will happen on the new buffer,
333 * then the data will be copied back into the original buffer and
334 * finally the new buffer will be freed. In case a software
335 * fallback was put in place in the @cra_init call, this function
336 * might need to use the fallback if the algorithm doesn't support
337 * all of the key sizes. In case the key was stored in
338 * transformation context, the key might need to be re-programmed
339 * into the hardware in this function. This function shall not
340 * modify the transformation context, as this function may be
341 * called in parallel with the same transformation object.
342 * @cia_decrypt: Decrypt a single block. This is a reverse counterpart to
343 * @cia_encrypt, and the conditions are exactly the same.
344 *
345 * All fields are mandatory and must be filled.
346 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347struct cipher_alg {
348 unsigned int cia_min_keysize;
349 unsigned int cia_max_keysize;
Herbert Xu6c2bb982006-05-16 22:09:29 +1000350 int (*cia_setkey)(struct crypto_tfm *tfm, const u8 *key,
Herbert Xu560c06a2006-08-13 14:16:39 +1000351 unsigned int keylen);
Herbert Xu6c2bb982006-05-16 22:09:29 +1000352 void (*cia_encrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
353 void (*cia_decrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354};
355
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356struct compress_alg {
Herbert Xu6c2bb982006-05-16 22:09:29 +1000357 int (*coa_compress)(struct crypto_tfm *tfm, const u8 *src,
358 unsigned int slen, u8 *dst, unsigned int *dlen);
359 int (*coa_decompress)(struct crypto_tfm *tfm, const u8 *src,
360 unsigned int slen, u8 *dst, unsigned int *dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361};
362
Neil Horman17f0f4a2008-08-14 22:15:52 +1000363
Herbert Xub5b7f082007-04-16 20:48:54 +1000364#define cra_ablkcipher cra_u.ablkcipher
Herbert Xu5cde0af2006-08-22 00:07:53 +1000365#define cra_blkcipher cra_u.blkcipher
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366#define cra_cipher cra_u.cipher
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367#define cra_compress cra_u.compress
368
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100369/**
370 * struct crypto_alg - definition of a cryptograpic cipher algorithm
371 * @cra_flags: Flags describing this transformation. See include/linux/crypto.h
372 * CRYPTO_ALG_* flags for the flags which go in here. Those are
373 * used for fine-tuning the description of the transformation
374 * algorithm.
375 * @cra_blocksize: Minimum block size of this transformation. The size in bytes
376 * of the smallest possible unit which can be transformed with
377 * this algorithm. The users must respect this value.
378 * In case of HASH transformation, it is possible for a smaller
379 * block than @cra_blocksize to be passed to the crypto API for
380 * transformation, in case of any other transformation type, an
381 * error will be returned upon any attempt to transform smaller
382 * than @cra_blocksize chunks.
383 * @cra_ctxsize: Size of the operational context of the transformation. This
384 * value informs the kernel crypto API about the memory size
385 * needed to be allocated for the transformation context.
386 * @cra_alignmask: Alignment mask for the input and output data buffer. The data
387 * buffer containing the input data for the algorithm must be
388 * aligned to this alignment mask. The data buffer for the
389 * output data must be aligned to this alignment mask. Note that
390 * the Crypto API will do the re-alignment in software, but
391 * only under special conditions and there is a performance hit.
392 * The re-alignment happens at these occasions for different
393 * @cra_u types: cipher -- For both input data and output data
394 * buffer; ahash -- For output hash destination buf; shash --
395 * For output hash destination buf.
396 * This is needed on hardware which is flawed by design and
397 * cannot pick data from arbitrary addresses.
398 * @cra_priority: Priority of this transformation implementation. In case
399 * multiple transformations with same @cra_name are available to
400 * the Crypto API, the kernel will use the one with highest
401 * @cra_priority.
402 * @cra_name: Generic name (usable by multiple implementations) of the
403 * transformation algorithm. This is the name of the transformation
404 * itself. This field is used by the kernel when looking up the
405 * providers of particular transformation.
406 * @cra_driver_name: Unique name of the transformation provider. This is the
407 * name of the provider of the transformation. This can be any
408 * arbitrary value, but in the usual case, this contains the
409 * name of the chip or provider and the name of the
410 * transformation algorithm.
411 * @cra_type: Type of the cryptographic transformation. This is a pointer to
412 * struct crypto_type, which implements callbacks common for all
Masanari Iida12f7c142015-06-04 00:01:21 +0900413 * transformation types. There are multiple options:
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100414 * &crypto_blkcipher_type, &crypto_ablkcipher_type,
Herbert Xub0d955b2015-08-14 15:30:41 +0800415 * &crypto_ahash_type, &crypto_rng_type.
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100416 * This field might be empty. In that case, there are no common
417 * callbacks. This is the case for: cipher, compress, shash.
418 * @cra_u: Callbacks implementing the transformation. This is a union of
419 * multiple structures. Depending on the type of transformation selected
420 * by @cra_type and @cra_flags above, the associated structure must be
421 * filled with callbacks. This field might be empty. This is the case
422 * for ahash, shash.
423 * @cra_init: Initialize the cryptographic transformation object. This function
424 * is used to initialize the cryptographic transformation object.
425 * This function is called only once at the instantiation time, right
426 * after the transformation context was allocated. In case the
427 * cryptographic hardware has some special requirements which need to
428 * be handled by software, this function shall check for the precise
429 * requirement of the transformation and put any software fallbacks
430 * in place.
431 * @cra_exit: Deinitialize the cryptographic transformation object. This is a
432 * counterpart to @cra_init, used to remove various changes set in
433 * @cra_init.
434 * @cra_module: Owner of this transformation implementation. Set to THIS_MODULE
435 * @cra_list: internally used
436 * @cra_users: internally used
437 * @cra_refcnt: internally used
438 * @cra_destroy: internally used
439 *
440 * The struct crypto_alg describes a generic Crypto API algorithm and is common
441 * for all of the transformations. Any variable not documented here shall not
442 * be used by a cipher implementation as it is internal to the Crypto API.
443 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444struct crypto_alg {
445 struct list_head cra_list;
Herbert Xu6bfd4802006-09-21 11:39:29 +1000446 struct list_head cra_users;
447
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 u32 cra_flags;
449 unsigned int cra_blocksize;
450 unsigned int cra_ctxsize;
Herbert Xu95477372005-07-06 13:52:09 -0700451 unsigned int cra_alignmask;
Herbert Xu5cb14542005-11-05 16:58:14 +1100452
453 int cra_priority;
Herbert Xu6521f302006-08-06 20:28:44 +1000454 atomic_t cra_refcnt;
Herbert Xu5cb14542005-11-05 16:58:14 +1100455
Herbert Xud913ea02006-05-21 08:45:26 +1000456 char cra_name[CRYPTO_MAX_ALG_NAME];
457 char cra_driver_name[CRYPTO_MAX_ALG_NAME];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
Herbert Xue853c3c2006-08-22 00:06:54 +1000459 const struct crypto_type *cra_type;
460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 union {
Herbert Xub5b7f082007-04-16 20:48:54 +1000462 struct ablkcipher_alg ablkcipher;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000463 struct blkcipher_alg blkcipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 struct cipher_alg cipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 struct compress_alg compress;
466 } cra_u;
Herbert Xuc7fc0592006-05-24 13:02:26 +1000467
468 int (*cra_init)(struct crypto_tfm *tfm);
469 void (*cra_exit)(struct crypto_tfm *tfm);
Herbert Xu6521f302006-08-06 20:28:44 +1000470 void (*cra_destroy)(struct crypto_alg *alg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
472 struct module *cra_module;
Herbert Xuedf18b92015-06-18 14:00:48 +0800473} CRYPTO_MINALIGN_ATTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
475/*
476 * Algorithm registration interface.
477 */
478int crypto_register_alg(struct crypto_alg *alg);
479int crypto_unregister_alg(struct crypto_alg *alg);
Mark Brown4b004342012-01-17 23:34:26 +0000480int crypto_register_algs(struct crypto_alg *algs, int count);
481int crypto_unregister_algs(struct crypto_alg *algs, int count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
483/*
484 * Algorithm query interface.
485 */
Herbert Xufce32d72006-08-26 17:35:45 +1000486int crypto_has_alg(const char *name, u32 type, u32 mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
488/*
489 * Transforms: user-instantiated objects which encapsulate algorithms
Herbert Xu6d7d6842006-07-30 11:53:01 +1000490 * and core processing logic. Managed via crypto_alloc_*() and
491 * crypto_free_*(), as well as the various helpers below.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
Herbert Xu32e39832007-03-24 14:35:34 +1100494struct ablkcipher_tfm {
495 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
496 unsigned int keylen);
497 int (*encrypt)(struct ablkcipher_request *req);
498 int (*decrypt)(struct ablkcipher_request *req);
Herbert Xu61da88e2007-12-17 21:51:27 +0800499
Herbert Xuecfc4322007-12-05 21:08:36 +1100500 struct crypto_ablkcipher *base;
501
Herbert Xu32e39832007-03-24 14:35:34 +1100502 unsigned int ivsize;
503 unsigned int reqsize;
504};
505
Herbert Xu5cde0af2006-08-22 00:07:53 +1000506struct blkcipher_tfm {
507 void *iv;
508 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
509 unsigned int keylen);
510 int (*encrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
511 struct scatterlist *src, unsigned int nbytes);
512 int (*decrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
513 struct scatterlist *src, unsigned int nbytes);
514};
515
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516struct cipher_tfm {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 int (*cit_setkey)(struct crypto_tfm *tfm,
518 const u8 *key, unsigned int keylen);
Herbert Xuf28776a2006-08-13 20:58:18 +1000519 void (*cit_encrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
520 void (*cit_decrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521};
522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523struct compress_tfm {
524 int (*cot_compress)(struct crypto_tfm *tfm,
525 const u8 *src, unsigned int slen,
526 u8 *dst, unsigned int *dlen);
527 int (*cot_decompress)(struct crypto_tfm *tfm,
528 const u8 *src, unsigned int slen,
529 u8 *dst, unsigned int *dlen);
530};
531
Herbert Xu32e39832007-03-24 14:35:34 +1100532#define crt_ablkcipher crt_u.ablkcipher
Herbert Xu5cde0af2006-08-22 00:07:53 +1000533#define crt_blkcipher crt_u.blkcipher
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534#define crt_cipher crt_u.cipher
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535#define crt_compress crt_u.compress
536
537struct crypto_tfm {
538
539 u32 crt_flags;
540
541 union {
Herbert Xu32e39832007-03-24 14:35:34 +1100542 struct ablkcipher_tfm ablkcipher;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000543 struct blkcipher_tfm blkcipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 struct cipher_tfm cipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 struct compress_tfm compress;
546 } crt_u;
Herbert Xu4a779482008-09-13 18:19:03 -0700547
548 void (*exit)(struct crypto_tfm *tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
550 struct crypto_alg *__crt_alg;
Herbert Xuf10b7892006-01-25 22:34:01 +1100551
Herbert Xu79911102006-08-21 21:03:52 +1000552 void *__crt_ctx[] CRYPTO_MINALIGN_ATTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553};
554
Herbert Xu32e39832007-03-24 14:35:34 +1100555struct crypto_ablkcipher {
556 struct crypto_tfm base;
557};
558
Herbert Xu5cde0af2006-08-22 00:07:53 +1000559struct crypto_blkcipher {
560 struct crypto_tfm base;
561};
562
Herbert Xu78a1fe42006-12-24 10:02:00 +1100563struct crypto_cipher {
564 struct crypto_tfm base;
565};
566
567struct crypto_comp {
568 struct crypto_tfm base;
569};
570
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000571enum {
572 CRYPTOA_UNSPEC,
573 CRYPTOA_ALG,
Herbert Xuebc610e2007-01-01 18:37:02 +1100574 CRYPTOA_TYPE,
Herbert Xu39e1ee012007-08-29 19:27:26 +0800575 CRYPTOA_U32,
Herbert Xuebc610e2007-01-01 18:37:02 +1100576 __CRYPTOA_MAX,
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000577};
578
Herbert Xuebc610e2007-01-01 18:37:02 +1100579#define CRYPTOA_MAX (__CRYPTOA_MAX - 1)
580
Herbert Xu39e1ee012007-08-29 19:27:26 +0800581/* Maximum number of (rtattr) parameters for each template. */
582#define CRYPTO_MAX_ATTRS 32
583
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000584struct crypto_attr_alg {
585 char name[CRYPTO_MAX_ALG_NAME];
586};
587
Herbert Xuebc610e2007-01-01 18:37:02 +1100588struct crypto_attr_type {
589 u32 type;
590 u32 mask;
591};
592
Herbert Xu39e1ee012007-08-29 19:27:26 +0800593struct crypto_attr_u32 {
594 u32 num;
595};
596
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597/*
598 * Transform user interface.
599 */
600
Herbert Xu6d7d6842006-07-30 11:53:01 +1000601struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask);
Herbert Xu7b2cd922009-02-05 16:48:24 +1100602void crypto_destroy_tfm(void *mem, struct crypto_tfm *tfm);
603
604static inline void crypto_free_tfm(struct crypto_tfm *tfm)
605{
606 return crypto_destroy_tfm(tfm, tfm);
607}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
Herbert Xuda7f0332008-07-31 17:08:25 +0800609int alg_test(const char *driver, const char *alg, u32 type, u32 mask);
610
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611/*
612 * Transform helpers which query the underlying algorithm.
613 */
614static inline const char *crypto_tfm_alg_name(struct crypto_tfm *tfm)
615{
616 return tfm->__crt_alg->cra_name;
617}
618
Michal Ludvigb14cdd62006-07-09 09:02:24 +1000619static inline const char *crypto_tfm_alg_driver_name(struct crypto_tfm *tfm)
620{
621 return tfm->__crt_alg->cra_driver_name;
622}
623
624static inline int crypto_tfm_alg_priority(struct crypto_tfm *tfm)
625{
626 return tfm->__crt_alg->cra_priority;
627}
628
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm)
630{
631 return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK;
632}
633
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm)
635{
636 return tfm->__crt_alg->cra_blocksize;
637}
638
Herbert Xufbdae9f2005-07-06 13:53:29 -0700639static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm *tfm)
640{
641 return tfm->__crt_alg->cra_alignmask;
642}
643
Herbert Xuf28776a2006-08-13 20:58:18 +1000644static inline u32 crypto_tfm_get_flags(struct crypto_tfm *tfm)
645{
646 return tfm->crt_flags;
647}
648
649static inline void crypto_tfm_set_flags(struct crypto_tfm *tfm, u32 flags)
650{
651 tfm->crt_flags |= flags;
652}
653
654static inline void crypto_tfm_clear_flags(struct crypto_tfm *tfm, u32 flags)
655{
656 tfm->crt_flags &= ~flags;
657}
658
Herbert Xu40725182005-07-06 13:51:52 -0700659static inline void *crypto_tfm_ctx(struct crypto_tfm *tfm)
660{
Herbert Xuf10b7892006-01-25 22:34:01 +1100661 return tfm->__crt_ctx;
662}
663
664static inline unsigned int crypto_tfm_ctx_alignment(void)
665{
666 struct crypto_tfm *tfm;
667 return __alignof__(tfm->__crt_ctx);
Herbert Xu40725182005-07-06 13:51:52 -0700668}
669
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670/*
671 * API wrappers.
672 */
Herbert Xu32e39832007-03-24 14:35:34 +1100673static inline struct crypto_ablkcipher *__crypto_ablkcipher_cast(
674 struct crypto_tfm *tfm)
675{
676 return (struct crypto_ablkcipher *)tfm;
677}
678
Herbert Xu378f4f52007-12-17 20:07:31 +0800679static inline u32 crypto_skcipher_type(u32 type)
680{
Herbert Xuecfc4322007-12-05 21:08:36 +1100681 type &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
Herbert Xu378f4f52007-12-17 20:07:31 +0800682 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
683 return type;
684}
685
686static inline u32 crypto_skcipher_mask(u32 mask)
687{
Herbert Xuecfc4322007-12-05 21:08:36 +1100688 mask &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
Herbert Xu378f4f52007-12-17 20:07:31 +0800689 mask |= CRYPTO_ALG_TYPE_BLKCIPHER_MASK;
690 return mask;
691}
692
Stephan Muellerf13ec332014-11-12 05:28:22 +0100693/**
694 * DOC: Asynchronous Block Cipher API
695 *
696 * Asynchronous block cipher API is used with the ciphers of type
697 * CRYPTO_ALG_TYPE_ABLKCIPHER (listed as type "ablkcipher" in /proc/crypto).
698 *
699 * Asynchronous cipher operations imply that the function invocation for a
700 * cipher request returns immediately before the completion of the operation.
701 * The cipher request is scheduled as a separate kernel thread and therefore
702 * load-balanced on the different CPUs via the process scheduler. To allow
703 * the kernel crypto API to inform the caller about the completion of a cipher
704 * request, the caller must provide a callback function. That function is
705 * invoked with the cipher handle when the request completes.
706 *
707 * To support the asynchronous operation, additional information than just the
708 * cipher handle must be supplied to the kernel crypto API. That additional
709 * information is given by filling in the ablkcipher_request data structure.
710 *
711 * For the asynchronous block cipher API, the state is maintained with the tfm
712 * cipher handle. A single tfm can be used across multiple calls and in
713 * parallel. For asynchronous block cipher calls, context data supplied and
714 * only used by the caller can be referenced the request data structure in
715 * addition to the IV used for the cipher request. The maintenance of such
716 * state information would be important for a crypto driver implementer to
717 * have, because when calling the callback function upon completion of the
718 * cipher operation, that callback function may need some information about
719 * which operation just finished if it invoked multiple in parallel. This
720 * state information is unused by the kernel crypto API.
721 */
722
Herbert Xu32e39832007-03-24 14:35:34 +1100723static inline struct crypto_tfm *crypto_ablkcipher_tfm(
724 struct crypto_ablkcipher *tfm)
725{
726 return &tfm->base;
727}
728
Stephan Muellerf13ec332014-11-12 05:28:22 +0100729/**
730 * crypto_free_ablkcipher() - zeroize and free cipher handle
731 * @tfm: cipher handle to be freed
732 */
Herbert Xu32e39832007-03-24 14:35:34 +1100733static inline void crypto_free_ablkcipher(struct crypto_ablkcipher *tfm)
734{
735 crypto_free_tfm(crypto_ablkcipher_tfm(tfm));
736}
737
Stephan Muellerf13ec332014-11-12 05:28:22 +0100738/**
739 * crypto_has_ablkcipher() - Search for the availability of an ablkcipher.
740 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
741 * ablkcipher
742 * @type: specifies the type of the cipher
743 * @mask: specifies the mask for the cipher
744 *
745 * Return: true when the ablkcipher is known to the kernel crypto API; false
746 * otherwise
747 */
Herbert Xu32e39832007-03-24 14:35:34 +1100748static inline int crypto_has_ablkcipher(const char *alg_name, u32 type,
749 u32 mask)
750{
Herbert Xu378f4f52007-12-17 20:07:31 +0800751 return crypto_has_alg(alg_name, crypto_skcipher_type(type),
752 crypto_skcipher_mask(mask));
Herbert Xu32e39832007-03-24 14:35:34 +1100753}
754
755static inline struct ablkcipher_tfm *crypto_ablkcipher_crt(
756 struct crypto_ablkcipher *tfm)
757{
758 return &crypto_ablkcipher_tfm(tfm)->crt_ablkcipher;
759}
760
Stephan Muellerf13ec332014-11-12 05:28:22 +0100761/**
762 * crypto_ablkcipher_ivsize() - obtain IV size
763 * @tfm: cipher handle
764 *
765 * The size of the IV for the ablkcipher referenced by the cipher handle is
766 * returned. This IV size may be zero if the cipher does not need an IV.
767 *
768 * Return: IV size in bytes
769 */
Herbert Xu32e39832007-03-24 14:35:34 +1100770static inline unsigned int crypto_ablkcipher_ivsize(
771 struct crypto_ablkcipher *tfm)
772{
773 return crypto_ablkcipher_crt(tfm)->ivsize;
774}
775
Stephan Muellerf13ec332014-11-12 05:28:22 +0100776/**
777 * crypto_ablkcipher_blocksize() - obtain block size of cipher
778 * @tfm: cipher handle
779 *
780 * The block size for the ablkcipher referenced with the cipher handle is
781 * returned. The caller may use that information to allocate appropriate
782 * memory for the data returned by the encryption or decryption operation
783 *
784 * Return: block size of cipher
785 */
Herbert Xu32e39832007-03-24 14:35:34 +1100786static inline unsigned int crypto_ablkcipher_blocksize(
787 struct crypto_ablkcipher *tfm)
788{
789 return crypto_tfm_alg_blocksize(crypto_ablkcipher_tfm(tfm));
790}
791
792static inline unsigned int crypto_ablkcipher_alignmask(
793 struct crypto_ablkcipher *tfm)
794{
795 return crypto_tfm_alg_alignmask(crypto_ablkcipher_tfm(tfm));
796}
797
798static inline u32 crypto_ablkcipher_get_flags(struct crypto_ablkcipher *tfm)
799{
800 return crypto_tfm_get_flags(crypto_ablkcipher_tfm(tfm));
801}
802
803static inline void crypto_ablkcipher_set_flags(struct crypto_ablkcipher *tfm,
804 u32 flags)
805{
806 crypto_tfm_set_flags(crypto_ablkcipher_tfm(tfm), flags);
807}
808
809static inline void crypto_ablkcipher_clear_flags(struct crypto_ablkcipher *tfm,
810 u32 flags)
811{
812 crypto_tfm_clear_flags(crypto_ablkcipher_tfm(tfm), flags);
813}
814
Stephan Muellerf13ec332014-11-12 05:28:22 +0100815/**
816 * crypto_ablkcipher_setkey() - set key for cipher
817 * @tfm: cipher handle
818 * @key: buffer holding the key
819 * @keylen: length of the key in bytes
820 *
821 * The caller provided key is set for the ablkcipher referenced by the cipher
822 * handle.
823 *
824 * Note, the key length determines the cipher type. Many block ciphers implement
825 * different cipher modes depending on the key size, such as AES-128 vs AES-192
826 * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
827 * is performed.
828 *
829 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
830 */
Herbert Xu32e39832007-03-24 14:35:34 +1100831static inline int crypto_ablkcipher_setkey(struct crypto_ablkcipher *tfm,
832 const u8 *key, unsigned int keylen)
833{
Herbert Xuecfc4322007-12-05 21:08:36 +1100834 struct ablkcipher_tfm *crt = crypto_ablkcipher_crt(tfm);
835
836 return crt->setkey(crt->base, key, keylen);
Herbert Xu32e39832007-03-24 14:35:34 +1100837}
838
Stephan Muellerf13ec332014-11-12 05:28:22 +0100839/**
840 * crypto_ablkcipher_reqtfm() - obtain cipher handle from request
841 * @req: ablkcipher_request out of which the cipher handle is to be obtained
842 *
843 * Return the crypto_ablkcipher handle when furnishing an ablkcipher_request
844 * data structure.
845 *
846 * Return: crypto_ablkcipher handle
847 */
Herbert Xu32e39832007-03-24 14:35:34 +1100848static inline struct crypto_ablkcipher *crypto_ablkcipher_reqtfm(
849 struct ablkcipher_request *req)
850{
851 return __crypto_ablkcipher_cast(req->base.tfm);
852}
853
Stephan Muellerf13ec332014-11-12 05:28:22 +0100854/**
855 * crypto_ablkcipher_encrypt() - encrypt plaintext
856 * @req: reference to the ablkcipher_request handle that holds all information
857 * needed to perform the cipher operation
858 *
859 * Encrypt plaintext data using the ablkcipher_request handle. That data
860 * structure and how it is filled with data is discussed with the
861 * ablkcipher_request_* functions.
862 *
863 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
864 */
Herbert Xu32e39832007-03-24 14:35:34 +1100865static inline int crypto_ablkcipher_encrypt(struct ablkcipher_request *req)
866{
867 struct ablkcipher_tfm *crt =
868 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
869 return crt->encrypt(req);
870}
871
Stephan Muellerf13ec332014-11-12 05:28:22 +0100872/**
873 * crypto_ablkcipher_decrypt() - decrypt ciphertext
874 * @req: reference to the ablkcipher_request handle that holds all information
875 * needed to perform the cipher operation
876 *
877 * Decrypt ciphertext data using the ablkcipher_request handle. That data
878 * structure and how it is filled with data is discussed with the
879 * ablkcipher_request_* functions.
880 *
881 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
882 */
Herbert Xu32e39832007-03-24 14:35:34 +1100883static inline int crypto_ablkcipher_decrypt(struct ablkcipher_request *req)
884{
885 struct ablkcipher_tfm *crt =
886 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
887 return crt->decrypt(req);
888}
889
Stephan Muellerf13ec332014-11-12 05:28:22 +0100890/**
891 * DOC: Asynchronous Cipher Request Handle
892 *
893 * The ablkcipher_request data structure contains all pointers to data
894 * required for the asynchronous cipher operation. This includes the cipher
895 * handle (which can be used by multiple ablkcipher_request instances), pointer
896 * to plaintext and ciphertext, asynchronous callback function, etc. It acts
897 * as a handle to the ablkcipher_request_* API calls in a similar way as
898 * ablkcipher handle to the crypto_ablkcipher_* API calls.
899 */
900
901/**
902 * crypto_ablkcipher_reqsize() - obtain size of the request data structure
903 * @tfm: cipher handle
904 *
905 * Return: number of bytes
906 */
Herbert Xub16c3a22007-08-29 19:02:04 +0800907static inline unsigned int crypto_ablkcipher_reqsize(
908 struct crypto_ablkcipher *tfm)
Herbert Xu32e39832007-03-24 14:35:34 +1100909{
910 return crypto_ablkcipher_crt(tfm)->reqsize;
911}
912
Stephan Muellerf13ec332014-11-12 05:28:22 +0100913/**
914 * ablkcipher_request_set_tfm() - update cipher handle reference in request
915 * @req: request handle to be modified
916 * @tfm: cipher handle that shall be added to the request handle
917 *
918 * Allow the caller to replace the existing ablkcipher handle in the request
919 * data structure with a different one.
920 */
Herbert Xue196d622007-04-14 16:09:14 +1000921static inline void ablkcipher_request_set_tfm(
922 struct ablkcipher_request *req, struct crypto_ablkcipher *tfm)
923{
Herbert Xuecfc4322007-12-05 21:08:36 +1100924 req->base.tfm = crypto_ablkcipher_tfm(crypto_ablkcipher_crt(tfm)->base);
Herbert Xue196d622007-04-14 16:09:14 +1000925}
926
Herbert Xub5b7f082007-04-16 20:48:54 +1000927static inline struct ablkcipher_request *ablkcipher_request_cast(
928 struct crypto_async_request *req)
929{
930 return container_of(req, struct ablkcipher_request, base);
931}
932
Stephan Muellerf13ec332014-11-12 05:28:22 +0100933/**
934 * ablkcipher_request_alloc() - allocate request data structure
935 * @tfm: cipher handle to be registered with the request
936 * @gfp: memory allocation flag that is handed to kmalloc by the API call.
937 *
938 * Allocate the request data structure that must be used with the ablkcipher
939 * encrypt and decrypt API calls. During the allocation, the provided ablkcipher
940 * handle is registered in the request data structure.
941 *
Eric Biggers6eae29e2016-04-02 10:54:56 -0500942 * Return: allocated request handle in case of success, or NULL if out of memory
Stephan Muellerf13ec332014-11-12 05:28:22 +0100943 */
Herbert Xu32e39832007-03-24 14:35:34 +1100944static inline struct ablkcipher_request *ablkcipher_request_alloc(
945 struct crypto_ablkcipher *tfm, gfp_t gfp)
946{
947 struct ablkcipher_request *req;
948
949 req = kmalloc(sizeof(struct ablkcipher_request) +
950 crypto_ablkcipher_reqsize(tfm), gfp);
951
952 if (likely(req))
Herbert Xue196d622007-04-14 16:09:14 +1000953 ablkcipher_request_set_tfm(req, tfm);
Herbert Xu32e39832007-03-24 14:35:34 +1100954
955 return req;
956}
957
Stephan Muellerf13ec332014-11-12 05:28:22 +0100958/**
959 * ablkcipher_request_free() - zeroize and free request data structure
960 * @req: request data structure cipher handle to be freed
961 */
Herbert Xu32e39832007-03-24 14:35:34 +1100962static inline void ablkcipher_request_free(struct ablkcipher_request *req)
963{
Herbert Xuaef73cf2009-07-11 22:22:14 +0800964 kzfree(req);
Herbert Xu32e39832007-03-24 14:35:34 +1100965}
966
Stephan Muellerf13ec332014-11-12 05:28:22 +0100967/**
968 * ablkcipher_request_set_callback() - set asynchronous callback function
969 * @req: request handle
970 * @flags: specify zero or an ORing of the flags
971 * CRYPTO_TFM_REQ_MAY_BACKLOG the request queue may back log and
972 * increase the wait queue beyond the initial maximum size;
973 * CRYPTO_TFM_REQ_MAY_SLEEP the request processing may sleep
974 * @compl: callback function pointer to be registered with the request handle
975 * @data: The data pointer refers to memory that is not used by the kernel
976 * crypto API, but provided to the callback function for it to use. Here,
977 * the caller can provide a reference to memory the callback function can
978 * operate on. As the callback function is invoked asynchronously to the
979 * related functionality, it may need to access data structures of the
980 * related functionality which can be referenced using this pointer. The
981 * callback function can access the memory via the "data" field in the
982 * crypto_async_request data structure provided to the callback function.
983 *
984 * This function allows setting the callback function that is triggered once the
985 * cipher operation completes.
986 *
987 * The callback function is registered with the ablkcipher_request handle and
Stephan Mueller379dcfb2015-01-19 00:13:39 +0100988 * must comply with the following template
Stephan Muellerf13ec332014-11-12 05:28:22 +0100989 *
990 * void callback_function(struct crypto_async_request *req, int error)
991 */
Herbert Xu32e39832007-03-24 14:35:34 +1100992static inline void ablkcipher_request_set_callback(
993 struct ablkcipher_request *req,
Mark Rustad3e3dc252014-07-25 02:53:38 -0700994 u32 flags, crypto_completion_t compl, void *data)
Herbert Xu32e39832007-03-24 14:35:34 +1100995{
Mark Rustad3e3dc252014-07-25 02:53:38 -0700996 req->base.complete = compl;
Herbert Xu32e39832007-03-24 14:35:34 +1100997 req->base.data = data;
998 req->base.flags = flags;
999}
1000
Stephan Muellerf13ec332014-11-12 05:28:22 +01001001/**
1002 * ablkcipher_request_set_crypt() - set data buffers
1003 * @req: request handle
1004 * @src: source scatter / gather list
1005 * @dst: destination scatter / gather list
1006 * @nbytes: number of bytes to process from @src
1007 * @iv: IV for the cipher operation which must comply with the IV size defined
1008 * by crypto_ablkcipher_ivsize
1009 *
1010 * This function allows setting of the source data and destination data
1011 * scatter / gather lists.
1012 *
1013 * For encryption, the source is treated as the plaintext and the
1014 * destination is the ciphertext. For a decryption operation, the use is
Stephan Mueller379dcfb2015-01-19 00:13:39 +01001015 * reversed - the source is the ciphertext and the destination is the plaintext.
Stephan Muellerf13ec332014-11-12 05:28:22 +01001016 */
Herbert Xu32e39832007-03-24 14:35:34 +11001017static inline void ablkcipher_request_set_crypt(
1018 struct ablkcipher_request *req,
1019 struct scatterlist *src, struct scatterlist *dst,
1020 unsigned int nbytes, void *iv)
1021{
1022 req->src = src;
1023 req->dst = dst;
1024 req->nbytes = nbytes;
1025 req->info = iv;
1026}
1027
Stephan Muellerfced7b02014-11-12 05:29:00 +01001028/**
Stephan Mueller58284f02014-11-12 05:29:36 +01001029 * DOC: Synchronous Block Cipher API
1030 *
1031 * The synchronous block cipher API is used with the ciphers of type
1032 * CRYPTO_ALG_TYPE_BLKCIPHER (listed as type "blkcipher" in /proc/crypto)
1033 *
1034 * Synchronous calls, have a context in the tfm. But since a single tfm can be
1035 * used in multiple calls and in parallel, this info should not be changeable
1036 * (unless a lock is used). This applies, for example, to the symmetric key.
1037 * However, the IV is changeable, so there is an iv field in blkcipher_tfm
1038 * structure for synchronous blkcipher api. So, its the only state info that can
1039 * be kept for synchronous calls without using a big lock across a tfm.
1040 *
1041 * The block cipher API allows the use of a complete cipher, i.e. a cipher
1042 * consisting of a template (a block chaining mode) and a single block cipher
1043 * primitive (e.g. AES).
1044 *
1045 * The plaintext data buffer and the ciphertext data buffer are pointed to
1046 * by using scatter/gather lists. The cipher operation is performed
1047 * on all segments of the provided scatter/gather lists.
1048 *
1049 * The kernel crypto API supports a cipher operation "in-place" which means that
1050 * the caller may provide the same scatter/gather list for the plaintext and
1051 * cipher text. After the completion of the cipher operation, the plaintext
1052 * data is replaced with the ciphertext data in case of an encryption and vice
1053 * versa for a decryption. The caller must ensure that the scatter/gather lists
1054 * for the output data point to sufficiently large buffers, i.e. multiples of
1055 * the block size of the cipher.
1056 */
1057
Herbert Xu5cde0af2006-08-22 00:07:53 +10001058static inline struct crypto_blkcipher *__crypto_blkcipher_cast(
1059 struct crypto_tfm *tfm)
1060{
1061 return (struct crypto_blkcipher *)tfm;
1062}
1063
1064static inline struct crypto_blkcipher *crypto_blkcipher_cast(
1065 struct crypto_tfm *tfm)
1066{
1067 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_BLKCIPHER);
1068 return __crypto_blkcipher_cast(tfm);
1069}
1070
Stephan Mueller58284f02014-11-12 05:29:36 +01001071/**
1072 * crypto_alloc_blkcipher() - allocate synchronous block cipher handle
1073 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1074 * blkcipher cipher
1075 * @type: specifies the type of the cipher
1076 * @mask: specifies the mask for the cipher
1077 *
1078 * Allocate a cipher handle for a block cipher. The returned struct
1079 * crypto_blkcipher is the cipher handle that is required for any subsequent
1080 * API invocation for that block cipher.
1081 *
1082 * Return: allocated cipher handle in case of success; IS_ERR() is true in case
1083 * of an error, PTR_ERR() returns the error code.
1084 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001085static inline struct crypto_blkcipher *crypto_alloc_blkcipher(
1086 const char *alg_name, u32 type, u32 mask)
1087{
Herbert Xu332f88402007-11-15 22:36:07 +08001088 type &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xu5cde0af2006-08-22 00:07:53 +10001089 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
Herbert Xu332f88402007-11-15 22:36:07 +08001090 mask |= CRYPTO_ALG_TYPE_MASK;
Herbert Xu5cde0af2006-08-22 00:07:53 +10001091
1092 return __crypto_blkcipher_cast(crypto_alloc_base(alg_name, type, mask));
1093}
1094
1095static inline struct crypto_tfm *crypto_blkcipher_tfm(
1096 struct crypto_blkcipher *tfm)
1097{
1098 return &tfm->base;
1099}
1100
Stephan Mueller58284f02014-11-12 05:29:36 +01001101/**
1102 * crypto_free_blkcipher() - zeroize and free the block cipher handle
1103 * @tfm: cipher handle to be freed
1104 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001105static inline void crypto_free_blkcipher(struct crypto_blkcipher *tfm)
1106{
1107 crypto_free_tfm(crypto_blkcipher_tfm(tfm));
1108}
1109
Stephan Mueller58284f02014-11-12 05:29:36 +01001110/**
1111 * crypto_has_blkcipher() - Search for the availability of a block cipher
1112 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1113 * block cipher
1114 * @type: specifies the type of the cipher
1115 * @mask: specifies the mask for the cipher
1116 *
1117 * Return: true when the block cipher is known to the kernel crypto API; false
1118 * otherwise
1119 */
Herbert Xufce32d72006-08-26 17:35:45 +10001120static inline int crypto_has_blkcipher(const char *alg_name, u32 type, u32 mask)
1121{
Herbert Xu332f88402007-11-15 22:36:07 +08001122 type &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xufce32d72006-08-26 17:35:45 +10001123 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
Herbert Xu332f88402007-11-15 22:36:07 +08001124 mask |= CRYPTO_ALG_TYPE_MASK;
Herbert Xufce32d72006-08-26 17:35:45 +10001125
1126 return crypto_has_alg(alg_name, type, mask);
1127}
1128
Stephan Mueller58284f02014-11-12 05:29:36 +01001129/**
1130 * crypto_blkcipher_name() - return the name / cra_name from the cipher handle
1131 * @tfm: cipher handle
1132 *
1133 * Return: The character string holding the name of the cipher
1134 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001135static inline const char *crypto_blkcipher_name(struct crypto_blkcipher *tfm)
1136{
1137 return crypto_tfm_alg_name(crypto_blkcipher_tfm(tfm));
1138}
1139
1140static inline struct blkcipher_tfm *crypto_blkcipher_crt(
1141 struct crypto_blkcipher *tfm)
1142{
1143 return &crypto_blkcipher_tfm(tfm)->crt_blkcipher;
1144}
1145
1146static inline struct blkcipher_alg *crypto_blkcipher_alg(
1147 struct crypto_blkcipher *tfm)
1148{
1149 return &crypto_blkcipher_tfm(tfm)->__crt_alg->cra_blkcipher;
1150}
1151
Stephan Mueller58284f02014-11-12 05:29:36 +01001152/**
1153 * crypto_blkcipher_ivsize() - obtain IV size
1154 * @tfm: cipher handle
1155 *
1156 * The size of the IV for the block cipher referenced by the cipher handle is
1157 * returned. This IV size may be zero if the cipher does not need an IV.
1158 *
1159 * Return: IV size in bytes
1160 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001161static inline unsigned int crypto_blkcipher_ivsize(struct crypto_blkcipher *tfm)
1162{
1163 return crypto_blkcipher_alg(tfm)->ivsize;
1164}
1165
Stephan Mueller58284f02014-11-12 05:29:36 +01001166/**
1167 * crypto_blkcipher_blocksize() - obtain block size of cipher
1168 * @tfm: cipher handle
1169 *
1170 * The block size for the block cipher referenced with the cipher handle is
1171 * returned. The caller may use that information to allocate appropriate
1172 * memory for the data returned by the encryption or decryption operation.
1173 *
1174 * Return: block size of cipher
1175 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001176static inline unsigned int crypto_blkcipher_blocksize(
1177 struct crypto_blkcipher *tfm)
1178{
1179 return crypto_tfm_alg_blocksize(crypto_blkcipher_tfm(tfm));
1180}
1181
1182static inline unsigned int crypto_blkcipher_alignmask(
1183 struct crypto_blkcipher *tfm)
1184{
1185 return crypto_tfm_alg_alignmask(crypto_blkcipher_tfm(tfm));
1186}
1187
1188static inline u32 crypto_blkcipher_get_flags(struct crypto_blkcipher *tfm)
1189{
1190 return crypto_tfm_get_flags(crypto_blkcipher_tfm(tfm));
1191}
1192
1193static inline void crypto_blkcipher_set_flags(struct crypto_blkcipher *tfm,
1194 u32 flags)
1195{
1196 crypto_tfm_set_flags(crypto_blkcipher_tfm(tfm), flags);
1197}
1198
1199static inline void crypto_blkcipher_clear_flags(struct crypto_blkcipher *tfm,
1200 u32 flags)
1201{
1202 crypto_tfm_clear_flags(crypto_blkcipher_tfm(tfm), flags);
1203}
1204
Stephan Mueller58284f02014-11-12 05:29:36 +01001205/**
1206 * crypto_blkcipher_setkey() - set key for cipher
1207 * @tfm: cipher handle
1208 * @key: buffer holding the key
1209 * @keylen: length of the key in bytes
1210 *
1211 * The caller provided key is set for the block cipher referenced by the cipher
1212 * handle.
1213 *
1214 * Note, the key length determines the cipher type. Many block ciphers implement
1215 * different cipher modes depending on the key size, such as AES-128 vs AES-192
1216 * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
1217 * is performed.
1218 *
1219 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
1220 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001221static inline int crypto_blkcipher_setkey(struct crypto_blkcipher *tfm,
1222 const u8 *key, unsigned int keylen)
1223{
1224 return crypto_blkcipher_crt(tfm)->setkey(crypto_blkcipher_tfm(tfm),
1225 key, keylen);
1226}
1227
Stephan Mueller58284f02014-11-12 05:29:36 +01001228/**
1229 * crypto_blkcipher_encrypt() - encrypt plaintext
1230 * @desc: reference to the block cipher handle with meta data
1231 * @dst: scatter/gather list that is filled by the cipher operation with the
1232 * ciphertext
1233 * @src: scatter/gather list that holds the plaintext
1234 * @nbytes: number of bytes of the plaintext to encrypt.
1235 *
1236 * Encrypt plaintext data using the IV set by the caller with a preceding
1237 * call of crypto_blkcipher_set_iv.
1238 *
1239 * The blkcipher_desc data structure must be filled by the caller and can
1240 * reside on the stack. The caller must fill desc as follows: desc.tfm is filled
1241 * with the block cipher handle; desc.flags is filled with either
1242 * CRYPTO_TFM_REQ_MAY_SLEEP or 0.
1243 *
1244 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1245 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001246static inline int crypto_blkcipher_encrypt(struct blkcipher_desc *desc,
1247 struct scatterlist *dst,
1248 struct scatterlist *src,
1249 unsigned int nbytes)
1250{
1251 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
1252 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
1253}
1254
Stephan Mueller58284f02014-11-12 05:29:36 +01001255/**
1256 * crypto_blkcipher_encrypt_iv() - encrypt plaintext with dedicated IV
1257 * @desc: reference to the block cipher handle with meta data
1258 * @dst: scatter/gather list that is filled by the cipher operation with the
1259 * ciphertext
1260 * @src: scatter/gather list that holds the plaintext
1261 * @nbytes: number of bytes of the plaintext to encrypt.
1262 *
1263 * Encrypt plaintext data with the use of an IV that is solely used for this
1264 * cipher operation. Any previously set IV is not used.
1265 *
1266 * The blkcipher_desc data structure must be filled by the caller and can
1267 * reside on the stack. The caller must fill desc as follows: desc.tfm is filled
1268 * with the block cipher handle; desc.info is filled with the IV to be used for
1269 * the current operation; desc.flags is filled with either
1270 * CRYPTO_TFM_REQ_MAY_SLEEP or 0.
1271 *
1272 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1273 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001274static inline int crypto_blkcipher_encrypt_iv(struct blkcipher_desc *desc,
1275 struct scatterlist *dst,
1276 struct scatterlist *src,
1277 unsigned int nbytes)
1278{
1279 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
1280}
1281
Stephan Mueller58284f02014-11-12 05:29:36 +01001282/**
1283 * crypto_blkcipher_decrypt() - decrypt ciphertext
1284 * @desc: reference to the block cipher handle with meta data
1285 * @dst: scatter/gather list that is filled by the cipher operation with the
1286 * plaintext
1287 * @src: scatter/gather list that holds the ciphertext
1288 * @nbytes: number of bytes of the ciphertext to decrypt.
1289 *
1290 * Decrypt ciphertext data using the IV set by the caller with a preceding
1291 * call of crypto_blkcipher_set_iv.
1292 *
1293 * The blkcipher_desc data structure must be filled by the caller as documented
1294 * for the crypto_blkcipher_encrypt call above.
1295 *
1296 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1297 *
1298 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001299static inline int crypto_blkcipher_decrypt(struct blkcipher_desc *desc,
1300 struct scatterlist *dst,
1301 struct scatterlist *src,
1302 unsigned int nbytes)
1303{
1304 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
1305 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
1306}
1307
Stephan Mueller58284f02014-11-12 05:29:36 +01001308/**
1309 * crypto_blkcipher_decrypt_iv() - decrypt ciphertext with dedicated IV
1310 * @desc: reference to the block cipher handle with meta data
1311 * @dst: scatter/gather list that is filled by the cipher operation with the
1312 * plaintext
1313 * @src: scatter/gather list that holds the ciphertext
1314 * @nbytes: number of bytes of the ciphertext to decrypt.
1315 *
1316 * Decrypt ciphertext data with the use of an IV that is solely used for this
1317 * cipher operation. Any previously set IV is not used.
1318 *
1319 * The blkcipher_desc data structure must be filled by the caller as documented
1320 * for the crypto_blkcipher_encrypt_iv call above.
1321 *
1322 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1323 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001324static inline int crypto_blkcipher_decrypt_iv(struct blkcipher_desc *desc,
1325 struct scatterlist *dst,
1326 struct scatterlist *src,
1327 unsigned int nbytes)
1328{
1329 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
1330}
1331
Stephan Mueller58284f02014-11-12 05:29:36 +01001332/**
1333 * crypto_blkcipher_set_iv() - set IV for cipher
1334 * @tfm: cipher handle
1335 * @src: buffer holding the IV
1336 * @len: length of the IV in bytes
1337 *
1338 * The caller provided IV is set for the block cipher referenced by the cipher
1339 * handle.
1340 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001341static inline void crypto_blkcipher_set_iv(struct crypto_blkcipher *tfm,
1342 const u8 *src, unsigned int len)
1343{
1344 memcpy(crypto_blkcipher_crt(tfm)->iv, src, len);
1345}
1346
Stephan Mueller58284f02014-11-12 05:29:36 +01001347/**
1348 * crypto_blkcipher_get_iv() - obtain IV from cipher
1349 * @tfm: cipher handle
1350 * @dst: buffer filled with the IV
1351 * @len: length of the buffer dst
1352 *
1353 * The caller can obtain the IV set for the block cipher referenced by the
1354 * cipher handle and store it into the user-provided buffer. If the buffer
1355 * has an insufficient space, the IV is truncated to fit the buffer.
1356 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001357static inline void crypto_blkcipher_get_iv(struct crypto_blkcipher *tfm,
1358 u8 *dst, unsigned int len)
1359{
1360 memcpy(dst, crypto_blkcipher_crt(tfm)->iv, len);
1361}
1362
Stephan Mueller16e61032014-11-12 05:30:06 +01001363/**
1364 * DOC: Single Block Cipher API
1365 *
1366 * The single block cipher API is used with the ciphers of type
1367 * CRYPTO_ALG_TYPE_CIPHER (listed as type "cipher" in /proc/crypto).
1368 *
1369 * Using the single block cipher API calls, operations with the basic cipher
1370 * primitive can be implemented. These cipher primitives exclude any block
1371 * chaining operations including IV handling.
1372 *
1373 * The purpose of this single block cipher API is to support the implementation
1374 * of templates or other concepts that only need to perform the cipher operation
1375 * on one block at a time. Templates invoke the underlying cipher primitive
1376 * block-wise and process either the input or the output data of these cipher
1377 * operations.
1378 */
1379
Herbert Xuf28776a2006-08-13 20:58:18 +10001380static inline struct crypto_cipher *__crypto_cipher_cast(struct crypto_tfm *tfm)
1381{
1382 return (struct crypto_cipher *)tfm;
1383}
1384
1385static inline struct crypto_cipher *crypto_cipher_cast(struct crypto_tfm *tfm)
1386{
1387 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
1388 return __crypto_cipher_cast(tfm);
1389}
1390
Stephan Mueller16e61032014-11-12 05:30:06 +01001391/**
1392 * crypto_alloc_cipher() - allocate single block cipher handle
1393 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1394 * single block cipher
1395 * @type: specifies the type of the cipher
1396 * @mask: specifies the mask for the cipher
1397 *
1398 * Allocate a cipher handle for a single block cipher. The returned struct
1399 * crypto_cipher is the cipher handle that is required for any subsequent API
1400 * invocation for that single block cipher.
1401 *
1402 * Return: allocated cipher handle in case of success; IS_ERR() is true in case
1403 * of an error, PTR_ERR() returns the error code.
1404 */
Herbert Xuf28776a2006-08-13 20:58:18 +10001405static inline struct crypto_cipher *crypto_alloc_cipher(const char *alg_name,
1406 u32 type, u32 mask)
1407{
1408 type &= ~CRYPTO_ALG_TYPE_MASK;
1409 type |= CRYPTO_ALG_TYPE_CIPHER;
1410 mask |= CRYPTO_ALG_TYPE_MASK;
1411
1412 return __crypto_cipher_cast(crypto_alloc_base(alg_name, type, mask));
1413}
1414
1415static inline struct crypto_tfm *crypto_cipher_tfm(struct crypto_cipher *tfm)
1416{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001417 return &tfm->base;
Herbert Xuf28776a2006-08-13 20:58:18 +10001418}
1419
Stephan Mueller16e61032014-11-12 05:30:06 +01001420/**
1421 * crypto_free_cipher() - zeroize and free the single block cipher handle
1422 * @tfm: cipher handle to be freed
1423 */
Herbert Xuf28776a2006-08-13 20:58:18 +10001424static inline void crypto_free_cipher(struct crypto_cipher *tfm)
1425{
1426 crypto_free_tfm(crypto_cipher_tfm(tfm));
1427}
1428
Stephan Mueller16e61032014-11-12 05:30:06 +01001429/**
1430 * crypto_has_cipher() - Search for the availability of a single block cipher
1431 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1432 * single block cipher
1433 * @type: specifies the type of the cipher
1434 * @mask: specifies the mask for the cipher
1435 *
1436 * Return: true when the single block cipher is known to the kernel crypto API;
1437 * false otherwise
1438 */
Herbert Xufce32d72006-08-26 17:35:45 +10001439static inline int crypto_has_cipher(const char *alg_name, u32 type, u32 mask)
1440{
1441 type &= ~CRYPTO_ALG_TYPE_MASK;
1442 type |= CRYPTO_ALG_TYPE_CIPHER;
1443 mask |= CRYPTO_ALG_TYPE_MASK;
1444
1445 return crypto_has_alg(alg_name, type, mask);
1446}
1447
Herbert Xuf28776a2006-08-13 20:58:18 +10001448static inline struct cipher_tfm *crypto_cipher_crt(struct crypto_cipher *tfm)
1449{
1450 return &crypto_cipher_tfm(tfm)->crt_cipher;
1451}
1452
Stephan Mueller16e61032014-11-12 05:30:06 +01001453/**
1454 * crypto_cipher_blocksize() - obtain block size for cipher
1455 * @tfm: cipher handle
1456 *
1457 * The block size for the single block cipher referenced with the cipher handle
1458 * tfm is returned. The caller may use that information to allocate appropriate
1459 * memory for the data returned by the encryption or decryption operation
1460 *
1461 * Return: block size of cipher
1462 */
Herbert Xuf28776a2006-08-13 20:58:18 +10001463static inline unsigned int crypto_cipher_blocksize(struct crypto_cipher *tfm)
1464{
1465 return crypto_tfm_alg_blocksize(crypto_cipher_tfm(tfm));
1466}
1467
1468static inline unsigned int crypto_cipher_alignmask(struct crypto_cipher *tfm)
1469{
1470 return crypto_tfm_alg_alignmask(crypto_cipher_tfm(tfm));
1471}
1472
1473static inline u32 crypto_cipher_get_flags(struct crypto_cipher *tfm)
1474{
1475 return crypto_tfm_get_flags(crypto_cipher_tfm(tfm));
1476}
1477
1478static inline void crypto_cipher_set_flags(struct crypto_cipher *tfm,
1479 u32 flags)
1480{
1481 crypto_tfm_set_flags(crypto_cipher_tfm(tfm), flags);
1482}
1483
1484static inline void crypto_cipher_clear_flags(struct crypto_cipher *tfm,
1485 u32 flags)
1486{
1487 crypto_tfm_clear_flags(crypto_cipher_tfm(tfm), flags);
1488}
1489
Stephan Mueller16e61032014-11-12 05:30:06 +01001490/**
1491 * crypto_cipher_setkey() - set key for cipher
1492 * @tfm: cipher handle
1493 * @key: buffer holding the key
1494 * @keylen: length of the key in bytes
1495 *
1496 * The caller provided key is set for the single block cipher referenced by the
1497 * cipher handle.
1498 *
1499 * Note, the key length determines the cipher type. Many block ciphers implement
1500 * different cipher modes depending on the key size, such as AES-128 vs AES-192
1501 * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
1502 * is performed.
1503 *
1504 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
1505 */
Herbert Xu7226bc872006-08-21 21:40:49 +10001506static inline int crypto_cipher_setkey(struct crypto_cipher *tfm,
1507 const u8 *key, unsigned int keylen)
1508{
1509 return crypto_cipher_crt(tfm)->cit_setkey(crypto_cipher_tfm(tfm),
1510 key, keylen);
1511}
1512
Stephan Mueller16e61032014-11-12 05:30:06 +01001513/**
1514 * crypto_cipher_encrypt_one() - encrypt one block of plaintext
1515 * @tfm: cipher handle
1516 * @dst: points to the buffer that will be filled with the ciphertext
1517 * @src: buffer holding the plaintext to be encrypted
1518 *
1519 * Invoke the encryption operation of one block. The caller must ensure that
1520 * the plaintext and ciphertext buffers are at least one block in size.
1521 */
Herbert Xuf28776a2006-08-13 20:58:18 +10001522static inline void crypto_cipher_encrypt_one(struct crypto_cipher *tfm,
1523 u8 *dst, const u8 *src)
1524{
1525 crypto_cipher_crt(tfm)->cit_encrypt_one(crypto_cipher_tfm(tfm),
1526 dst, src);
1527}
1528
Stephan Mueller16e61032014-11-12 05:30:06 +01001529/**
1530 * crypto_cipher_decrypt_one() - decrypt one block of ciphertext
1531 * @tfm: cipher handle
1532 * @dst: points to the buffer that will be filled with the plaintext
1533 * @src: buffer holding the ciphertext to be decrypted
1534 *
1535 * Invoke the decryption operation of one block. The caller must ensure that
1536 * the plaintext and ciphertext buffers are at least one block in size.
1537 */
Herbert Xuf28776a2006-08-13 20:58:18 +10001538static inline void crypto_cipher_decrypt_one(struct crypto_cipher *tfm,
1539 u8 *dst, const u8 *src)
1540{
1541 crypto_cipher_crt(tfm)->cit_decrypt_one(crypto_cipher_tfm(tfm),
1542 dst, src);
1543}
1544
Herbert Xufce32d72006-08-26 17:35:45 +10001545static inline struct crypto_comp *__crypto_comp_cast(struct crypto_tfm *tfm)
1546{
1547 return (struct crypto_comp *)tfm;
1548}
1549
1550static inline struct crypto_comp *crypto_comp_cast(struct crypto_tfm *tfm)
1551{
1552 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_COMPRESS) &
1553 CRYPTO_ALG_TYPE_MASK);
1554 return __crypto_comp_cast(tfm);
1555}
1556
1557static inline struct crypto_comp *crypto_alloc_comp(const char *alg_name,
1558 u32 type, u32 mask)
1559{
1560 type &= ~CRYPTO_ALG_TYPE_MASK;
1561 type |= CRYPTO_ALG_TYPE_COMPRESS;
1562 mask |= CRYPTO_ALG_TYPE_MASK;
1563
1564 return __crypto_comp_cast(crypto_alloc_base(alg_name, type, mask));
1565}
1566
1567static inline struct crypto_tfm *crypto_comp_tfm(struct crypto_comp *tfm)
1568{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001569 return &tfm->base;
Herbert Xufce32d72006-08-26 17:35:45 +10001570}
1571
1572static inline void crypto_free_comp(struct crypto_comp *tfm)
1573{
1574 crypto_free_tfm(crypto_comp_tfm(tfm));
1575}
1576
1577static inline int crypto_has_comp(const char *alg_name, u32 type, u32 mask)
1578{
1579 type &= ~CRYPTO_ALG_TYPE_MASK;
1580 type |= CRYPTO_ALG_TYPE_COMPRESS;
1581 mask |= CRYPTO_ALG_TYPE_MASK;
1582
1583 return crypto_has_alg(alg_name, type, mask);
1584}
1585
Herbert Xue4d5b792006-08-26 18:12:40 +10001586static inline const char *crypto_comp_name(struct crypto_comp *tfm)
1587{
1588 return crypto_tfm_alg_name(crypto_comp_tfm(tfm));
1589}
1590
Herbert Xufce32d72006-08-26 17:35:45 +10001591static inline struct compress_tfm *crypto_comp_crt(struct crypto_comp *tfm)
1592{
1593 return &crypto_comp_tfm(tfm)->crt_compress;
1594}
1595
1596static inline int crypto_comp_compress(struct crypto_comp *tfm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 const u8 *src, unsigned int slen,
1598 u8 *dst, unsigned int *dlen)
1599{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001600 return crypto_comp_crt(tfm)->cot_compress(crypto_comp_tfm(tfm),
1601 src, slen, dst, dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602}
1603
Herbert Xufce32d72006-08-26 17:35:45 +10001604static inline int crypto_comp_decompress(struct crypto_comp *tfm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 const u8 *src, unsigned int slen,
1606 u8 *dst, unsigned int *dlen)
1607{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001608 return crypto_comp_crt(tfm)->cot_decompress(crypto_comp_tfm(tfm),
1609 src, slen, dst, dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610}
1611
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612#endif /* _LINUX_CRYPTO_H */
1613