blob: 781f7d54602081665ebd955403f902368bf42dc3 [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
Geert Uytterhoevena1d2f092009-03-04 15:05:33 +080056#define CRYPTO_ALG_TYPE_PCOMPRESS 0x0000000f
Herbert Xu055bcee2006-08-19 22:24:23 +100057
58#define CRYPTO_ALG_TYPE_HASH_MASK 0x0000000e
Loc Ho004a4032008-05-14 20:41:47 +080059#define CRYPTO_ALG_TYPE_AHASH_MASK 0x0000000c
Herbert Xu332f88402007-11-15 22:36:07 +080060#define CRYPTO_ALG_TYPE_BLKCIPHER_MASK 0x0000000c
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Herbert Xu28259822006-08-06 21:23:26 +100062#define CRYPTO_ALG_LARVAL 0x00000010
Herbert Xu6bfd4802006-09-21 11:39:29 +100063#define CRYPTO_ALG_DEAD 0x00000020
64#define CRYPTO_ALG_DYING 0x00000040
Herbert Xuf3f632d2006-08-06 23:12:59 +100065#define CRYPTO_ALG_ASYNC 0x00000080
Herbert Xu28259822006-08-06 21:23:26 +100066
Linus Torvalds1da177e2005-04-16 15:20:36 -070067/*
Herbert Xu60104392006-08-26 18:34:10 +100068 * Set this bit if and only if the algorithm requires another algorithm of
69 * the same type to handle corner cases.
70 */
71#define CRYPTO_ALG_NEED_FALLBACK 0x00000100
72
73/*
Herbert Xuecfc4322007-12-05 21:08:36 +110074 * This bit is set for symmetric key ciphers that have already been wrapped
75 * with a generic IV generator to prevent them from being wrapped again.
76 */
77#define CRYPTO_ALG_GENIV 0x00000200
78
79/*
Herbert Xu73d38642008-08-03 21:15:23 +080080 * Set if the algorithm has passed automated run-time testing. Note that
81 * if there is no run-time testing for a given algorithm it is considered
82 * to have passed.
83 */
84
85#define CRYPTO_ALG_TESTED 0x00000400
86
87/*
Steffen Klassert64a947b2011-09-27 07:21:26 +020088 * Set if the algorithm is an instance that is build from templates.
89 */
90#define CRYPTO_ALG_INSTANCE 0x00000800
91
Nikos Mavrogiannopoulosd912bb72011-11-01 13:39:56 +010092/* Set this bit if the algorithm provided is hardware accelerated but
93 * not available to userspace via instruction set or so.
94 */
95#define CRYPTO_ALG_KERN_DRIVER_ONLY 0x00001000
96
Steffen Klassert64a947b2011-09-27 07:21:26 +020097/*
Stephan Mueller06ca7f62015-03-30 21:55:52 +020098 * Mark a cipher as a service implementation only usable by another
99 * cipher and never by a normal user of the kernel crypto API
100 */
101#define CRYPTO_ALG_INTERNAL 0x00002000
102
103/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 * Transform masks and values (for crt_flags).
105 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106#define CRYPTO_TFM_REQ_MASK 0x000fff00
107#define CRYPTO_TFM_RES_MASK 0xfff00000
108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109#define CRYPTO_TFM_REQ_WEAK_KEY 0x00000100
Herbert Xu64baf3c2005-09-01 17:43:05 -0700110#define CRYPTO_TFM_REQ_MAY_SLEEP 0x00000200
Herbert Xu32e39832007-03-24 14:35:34 +1100111#define CRYPTO_TFM_REQ_MAY_BACKLOG 0x00000400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112#define CRYPTO_TFM_RES_WEAK_KEY 0x00100000
113#define CRYPTO_TFM_RES_BAD_KEY_LEN 0x00200000
114#define CRYPTO_TFM_RES_BAD_KEY_SCHED 0x00400000
115#define CRYPTO_TFM_RES_BAD_BLOCK_LEN 0x00800000
116#define CRYPTO_TFM_RES_BAD_FLAGS 0x01000000
117
118/*
119 * Miscellaneous stuff.
120 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121#define CRYPTO_MAX_ALG_NAME 64
122
Herbert Xu79911102006-08-21 21:03:52 +1000123/*
124 * The macro CRYPTO_MINALIGN_ATTR (along with the void * type in the actual
125 * declaration) is used to ensure that the crypto_tfm context structure is
126 * aligned correctly for the given architecture so that there are no alignment
127 * faults for C data types. In particular, this is required on platforms such
128 * as arm where pointers are 32-bit aligned but there are data types such as
129 * u64 which require 64-bit alignment.
130 */
Herbert Xu79911102006-08-21 21:03:52 +1000131#define CRYPTO_MINALIGN ARCH_KMALLOC_MINALIGN
Herbert Xu79911102006-08-21 21:03:52 +1000132
Herbert Xu79911102006-08-21 21:03:52 +1000133#define CRYPTO_MINALIGN_ATTR __attribute__ ((__aligned__(CRYPTO_MINALIGN)))
Herbert Xu79911102006-08-21 21:03:52 +1000134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135struct scatterlist;
Herbert Xu32e39832007-03-24 14:35:34 +1100136struct crypto_ablkcipher;
137struct crypto_async_request;
Herbert Xu1ae97822007-08-30 15:36:14 +0800138struct crypto_aead;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000139struct crypto_blkcipher;
Herbert Xu055bcee2006-08-19 22:24:23 +1000140struct crypto_hash;
Neil Horman17f0f4a2008-08-14 22:15:52 +1000141struct crypto_rng;
Herbert Xu40725182005-07-06 13:51:52 -0700142struct crypto_tfm;
Herbert Xue853c3c2006-08-22 00:06:54 +1000143struct crypto_type;
Herbert Xu743edf52007-12-10 16:18:01 +0800144struct aead_givcrypt_request;
Herbert Xu61da88e2007-12-17 21:51:27 +0800145struct skcipher_givcrypt_request;
Herbert Xu40725182005-07-06 13:51:52 -0700146
Herbert Xu32e39832007-03-24 14:35:34 +1100147typedef void (*crypto_completion_t)(struct crypto_async_request *req, int err);
148
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100149/**
150 * DOC: Block Cipher Context Data Structures
151 *
152 * These data structures define the operating context for each block cipher
153 * type.
154 */
155
Herbert Xu32e39832007-03-24 14:35:34 +1100156struct crypto_async_request {
157 struct list_head list;
158 crypto_completion_t complete;
159 void *data;
160 struct crypto_tfm *tfm;
161
162 u32 flags;
163};
164
165struct ablkcipher_request {
166 struct crypto_async_request base;
167
168 unsigned int nbytes;
169
170 void *info;
171
172 struct scatterlist *src;
173 struct scatterlist *dst;
174
175 void *__ctx[] CRYPTO_MINALIGN_ATTR;
176};
177
Herbert Xu1ae97822007-08-30 15:36:14 +0800178/**
179 * struct aead_request - AEAD request
180 * @base: Common attributes for async crypto requests
181 * @assoclen: Length in bytes of associated data for authentication
182 * @cryptlen: Length of data to be encrypted or decrypted
183 * @iv: Initialisation vector
184 * @assoc: Associated data
185 * @src: Source data
186 * @dst: Destination data
187 * @__ctx: Start of private context data
188 */
189struct aead_request {
190 struct crypto_async_request base;
191
192 unsigned int assoclen;
193 unsigned int cryptlen;
194
195 u8 *iv;
196
197 struct scatterlist *assoc;
198 struct scatterlist *src;
199 struct scatterlist *dst;
200
201 void *__ctx[] CRYPTO_MINALIGN_ATTR;
202};
203
Herbert Xu5cde0af2006-08-22 00:07:53 +1000204struct blkcipher_desc {
205 struct crypto_blkcipher *tfm;
206 void *info;
207 u32 flags;
208};
209
Herbert Xu40725182005-07-06 13:51:52 -0700210struct cipher_desc {
211 struct crypto_tfm *tfm;
Herbert Xu6c2bb982006-05-16 22:09:29 +1000212 void (*crfn)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Herbert Xu40725182005-07-06 13:51:52 -0700213 unsigned int (*prfn)(const struct cipher_desc *desc, u8 *dst,
214 const u8 *src, unsigned int nbytes);
215 void *info;
216};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
Herbert Xu055bcee2006-08-19 22:24:23 +1000218struct hash_desc {
219 struct crypto_hash *tfm;
220 u32 flags;
221};
222
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100223/**
224 * DOC: Block Cipher Algorithm Definitions
225 *
226 * These data structures define modular crypto algorithm implementations,
227 * managed via crypto_register_alg() and crypto_unregister_alg().
228 */
229
230/**
231 * struct ablkcipher_alg - asynchronous block cipher definition
232 * @min_keysize: Minimum key size supported by the transformation. This is the
233 * smallest key length supported by this transformation algorithm.
234 * This must be set to one of the pre-defined values as this is
235 * not hardware specific. Possible values for this field can be
236 * found via git grep "_MIN_KEY_SIZE" include/crypto/
237 * @max_keysize: Maximum key size supported by the transformation. This is the
238 * largest key length supported by this transformation algorithm.
239 * This must be set to one of the pre-defined values as this is
240 * not hardware specific. Possible values for this field can be
241 * found via git grep "_MAX_KEY_SIZE" include/crypto/
242 * @setkey: Set key for the transformation. This function is used to either
243 * program a supplied key into the hardware or store the key in the
244 * transformation context for programming it later. Note that this
245 * function does modify the transformation context. This function can
246 * be called multiple times during the existence of the transformation
247 * object, so one must make sure the key is properly reprogrammed into
248 * the hardware. This function is also responsible for checking the key
249 * length for validity. In case a software fallback was put in place in
250 * the @cra_init call, this function might need to use the fallback if
251 * the algorithm doesn't support all of the key sizes.
252 * @encrypt: Encrypt a scatterlist of blocks. This function is used to encrypt
253 * the supplied scatterlist containing the blocks of data. The crypto
254 * API consumer is responsible for aligning the entries of the
255 * scatterlist properly and making sure the chunks are correctly
256 * sized. In case a software fallback was put in place in the
257 * @cra_init call, this function might need to use the fallback if
258 * the algorithm doesn't support all of the key sizes. In case the
259 * key was stored in transformation context, the key might need to be
260 * re-programmed into the hardware in this function. This function
261 * shall not modify the transformation context, as this function may
262 * be called in parallel with the same transformation object.
263 * @decrypt: Decrypt a single block. This is a reverse counterpart to @encrypt
264 * and the conditions are exactly the same.
265 * @givencrypt: Update the IV for encryption. With this function, a cipher
266 * implementation may provide the function on how to update the IV
267 * for encryption.
268 * @givdecrypt: Update the IV for decryption. This is the reverse of
269 * @givencrypt .
270 * @geniv: The transformation implementation may use an "IV generator" provided
271 * by the kernel crypto API. Several use cases have a predefined
272 * approach how IVs are to be updated. For such use cases, the kernel
273 * crypto API provides ready-to-use implementations that can be
274 * referenced with this variable.
275 * @ivsize: IV size applicable for transformation. The consumer must provide an
276 * IV of exactly that size to perform the encrypt or decrypt operation.
277 *
278 * All fields except @givencrypt , @givdecrypt , @geniv and @ivsize are
279 * mandatory and must be filled.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 */
Herbert Xub5b7f082007-04-16 20:48:54 +1000281struct ablkcipher_alg {
282 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
283 unsigned int keylen);
284 int (*encrypt)(struct ablkcipher_request *req);
285 int (*decrypt)(struct ablkcipher_request *req);
Herbert Xu61da88e2007-12-17 21:51:27 +0800286 int (*givencrypt)(struct skcipher_givcrypt_request *req);
287 int (*givdecrypt)(struct skcipher_givcrypt_request *req);
Herbert Xub5b7f082007-04-16 20:48:54 +1000288
Herbert Xu23508e12007-11-27 21:33:24 +0800289 const char *geniv;
290
Herbert Xub5b7f082007-04-16 20:48:54 +1000291 unsigned int min_keysize;
292 unsigned int max_keysize;
293 unsigned int ivsize;
294};
295
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100296/**
297 * struct aead_alg - AEAD cipher definition
298 * @maxauthsize: Set the maximum authentication tag size supported by the
299 * transformation. A transformation may support smaller tag sizes.
300 * As the authentication tag is a message digest to ensure the
301 * integrity of the encrypted data, a consumer typically wants the
302 * largest authentication tag possible as defined by this
303 * variable.
304 * @setauthsize: Set authentication size for the AEAD transformation. This
305 * function is used to specify the consumer requested size of the
306 * authentication tag to be either generated by the transformation
307 * during encryption or the size of the authentication tag to be
308 * supplied during the decryption operation. This function is also
309 * responsible for checking the authentication tag size for
310 * validity.
311 * @setkey: see struct ablkcipher_alg
312 * @encrypt: see struct ablkcipher_alg
313 * @decrypt: see struct ablkcipher_alg
314 * @givencrypt: see struct ablkcipher_alg
315 * @givdecrypt: see struct ablkcipher_alg
316 * @geniv: see struct ablkcipher_alg
317 * @ivsize: see struct ablkcipher_alg
318 *
319 * All fields except @givencrypt , @givdecrypt , @geniv and @ivsize are
320 * mandatory and must be filled.
321 */
Herbert Xu1ae97822007-08-30 15:36:14 +0800322struct aead_alg {
323 int (*setkey)(struct crypto_aead *tfm, const u8 *key,
324 unsigned int keylen);
Herbert Xu7ba683a2007-12-02 18:49:21 +1100325 int (*setauthsize)(struct crypto_aead *tfm, unsigned int authsize);
Herbert Xu1ae97822007-08-30 15:36:14 +0800326 int (*encrypt)(struct aead_request *req);
327 int (*decrypt)(struct aead_request *req);
Herbert Xu743edf52007-12-10 16:18:01 +0800328 int (*givencrypt)(struct aead_givcrypt_request *req);
329 int (*givdecrypt)(struct aead_givcrypt_request *req);
Herbert Xu1ae97822007-08-30 15:36:14 +0800330
Herbert Xu5b6d2d72007-12-12 19:23:36 +0800331 const char *geniv;
332
Herbert Xu1ae97822007-08-30 15:36:14 +0800333 unsigned int ivsize;
Herbert Xu7ba683a2007-12-02 18:49:21 +1100334 unsigned int maxauthsize;
Herbert Xu1ae97822007-08-30 15:36:14 +0800335};
336
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100337/**
338 * struct blkcipher_alg - synchronous block cipher definition
339 * @min_keysize: see struct ablkcipher_alg
340 * @max_keysize: see struct ablkcipher_alg
341 * @setkey: see struct ablkcipher_alg
342 * @encrypt: see struct ablkcipher_alg
343 * @decrypt: see struct ablkcipher_alg
344 * @geniv: see struct ablkcipher_alg
345 * @ivsize: see struct ablkcipher_alg
346 *
347 * All fields except @geniv and @ivsize are mandatory and must be filled.
348 */
Herbert Xu5cde0af2006-08-22 00:07:53 +1000349struct blkcipher_alg {
350 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
351 unsigned int keylen);
352 int (*encrypt)(struct blkcipher_desc *desc,
353 struct scatterlist *dst, struct scatterlist *src,
354 unsigned int nbytes);
355 int (*decrypt)(struct blkcipher_desc *desc,
356 struct scatterlist *dst, struct scatterlist *src,
357 unsigned int nbytes);
358
Herbert Xu23508e12007-11-27 21:33:24 +0800359 const char *geniv;
360
Herbert Xu5cde0af2006-08-22 00:07:53 +1000361 unsigned int min_keysize;
362 unsigned int max_keysize;
363 unsigned int ivsize;
364};
365
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100366/**
367 * struct cipher_alg - single-block symmetric ciphers definition
368 * @cia_min_keysize: Minimum key size supported by the transformation. This is
369 * the smallest key length supported by this transformation
370 * algorithm. This must be set to one of the pre-defined
371 * values as this is not hardware specific. Possible values
372 * for this field can be found via git grep "_MIN_KEY_SIZE"
373 * include/crypto/
374 * @cia_max_keysize: Maximum key size supported by the transformation. This is
375 * the largest key length supported by this transformation
376 * algorithm. This must be set to one of the pre-defined values
377 * as this is not hardware specific. Possible values for this
378 * field can be found via git grep "_MAX_KEY_SIZE"
379 * include/crypto/
380 * @cia_setkey: Set key for the transformation. This function is used to either
381 * program a supplied key into the hardware or store the key in the
382 * transformation context for programming it later. Note that this
383 * function does modify the transformation context. This function
384 * can be called multiple times during the existence of the
385 * transformation object, so one must make sure the key is properly
386 * reprogrammed into the hardware. This function is also
387 * responsible for checking the key length for validity.
388 * @cia_encrypt: Encrypt a single block. This function is used to encrypt a
389 * single block of data, which must be @cra_blocksize big. This
390 * always operates on a full @cra_blocksize and it is not possible
391 * to encrypt a block of smaller size. The supplied buffers must
392 * therefore also be at least of @cra_blocksize size. Both the
393 * input and output buffers are always aligned to @cra_alignmask.
394 * In case either of the input or output buffer supplied by user
395 * of the crypto API is not aligned to @cra_alignmask, the crypto
396 * API will re-align the buffers. The re-alignment means that a
397 * new buffer will be allocated, the data will be copied into the
398 * new buffer, then the processing will happen on the new buffer,
399 * then the data will be copied back into the original buffer and
400 * finally the new buffer will be freed. In case a software
401 * fallback was put in place in the @cra_init call, this function
402 * might need to use the fallback if the algorithm doesn't support
403 * all of the key sizes. In case the key was stored in
404 * transformation context, the key might need to be re-programmed
405 * into the hardware in this function. This function shall not
406 * modify the transformation context, as this function may be
407 * called in parallel with the same transformation object.
408 * @cia_decrypt: Decrypt a single block. This is a reverse counterpart to
409 * @cia_encrypt, and the conditions are exactly the same.
410 *
411 * All fields are mandatory and must be filled.
412 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413struct cipher_alg {
414 unsigned int cia_min_keysize;
415 unsigned int cia_max_keysize;
Herbert Xu6c2bb982006-05-16 22:09:29 +1000416 int (*cia_setkey)(struct crypto_tfm *tfm, const u8 *key,
Herbert Xu560c06a2006-08-13 14:16:39 +1000417 unsigned int keylen);
Herbert Xu6c2bb982006-05-16 22:09:29 +1000418 void (*cia_encrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
419 void (*cia_decrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420};
421
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422struct compress_alg {
Herbert Xu6c2bb982006-05-16 22:09:29 +1000423 int (*coa_compress)(struct crypto_tfm *tfm, const u8 *src,
424 unsigned int slen, u8 *dst, unsigned int *dlen);
425 int (*coa_decompress)(struct crypto_tfm *tfm, const u8 *src,
426 unsigned int slen, u8 *dst, unsigned int *dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427};
428
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100429/**
430 * struct rng_alg - random number generator definition
431 * @rng_make_random: The function defined by this variable obtains a random
432 * number. The random number generator transform must generate
433 * the random number out of the context provided with this
434 * call.
435 * @rng_reset: Reset of the random number generator by clearing the entire state.
436 * With the invocation of this function call, the random number
437 * generator shall completely reinitialize its state. If the random
438 * number generator requires a seed for setting up a new state,
439 * the seed must be provided by the consumer while invoking this
440 * function. The required size of the seed is defined with
441 * @seedsize .
442 * @seedsize: The seed size required for a random number generator
443 * initialization defined with this variable. Some random number
444 * generators like the SP800-90A DRBG does not require a seed as the
445 * seeding is implemented internally without the need of support by
446 * the consumer. In this case, the seed size is set to zero.
447 */
Neil Horman17f0f4a2008-08-14 22:15:52 +1000448struct rng_alg {
449 int (*rng_make_random)(struct crypto_rng *tfm, u8 *rdata,
450 unsigned int dlen);
451 int (*rng_reset)(struct crypto_rng *tfm, u8 *seed, unsigned int slen);
452
453 unsigned int seedsize;
454};
455
456
Herbert Xub5b7f082007-04-16 20:48:54 +1000457#define cra_ablkcipher cra_u.ablkcipher
Herbert Xu1ae97822007-08-30 15:36:14 +0800458#define cra_aead cra_u.aead
Herbert Xu5cde0af2006-08-22 00:07:53 +1000459#define cra_blkcipher cra_u.blkcipher
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460#define cra_cipher cra_u.cipher
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461#define cra_compress cra_u.compress
Neil Horman17f0f4a2008-08-14 22:15:52 +1000462#define cra_rng cra_u.rng
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100464/**
465 * struct crypto_alg - definition of a cryptograpic cipher algorithm
466 * @cra_flags: Flags describing this transformation. See include/linux/crypto.h
467 * CRYPTO_ALG_* flags for the flags which go in here. Those are
468 * used for fine-tuning the description of the transformation
469 * algorithm.
470 * @cra_blocksize: Minimum block size of this transformation. The size in bytes
471 * of the smallest possible unit which can be transformed with
472 * this algorithm. The users must respect this value.
473 * In case of HASH transformation, it is possible for a smaller
474 * block than @cra_blocksize to be passed to the crypto API for
475 * transformation, in case of any other transformation type, an
476 * error will be returned upon any attempt to transform smaller
477 * than @cra_blocksize chunks.
478 * @cra_ctxsize: Size of the operational context of the transformation. This
479 * value informs the kernel crypto API about the memory size
480 * needed to be allocated for the transformation context.
481 * @cra_alignmask: Alignment mask for the input and output data buffer. The data
482 * buffer containing the input data for the algorithm must be
483 * aligned to this alignment mask. The data buffer for the
484 * output data must be aligned to this alignment mask. Note that
485 * the Crypto API will do the re-alignment in software, but
486 * only under special conditions and there is a performance hit.
487 * The re-alignment happens at these occasions for different
488 * @cra_u types: cipher -- For both input data and output data
489 * buffer; ahash -- For output hash destination buf; shash --
490 * For output hash destination buf.
491 * This is needed on hardware which is flawed by design and
492 * cannot pick data from arbitrary addresses.
493 * @cra_priority: Priority of this transformation implementation. In case
494 * multiple transformations with same @cra_name are available to
495 * the Crypto API, the kernel will use the one with highest
496 * @cra_priority.
497 * @cra_name: Generic name (usable by multiple implementations) of the
498 * transformation algorithm. This is the name of the transformation
499 * itself. This field is used by the kernel when looking up the
500 * providers of particular transformation.
501 * @cra_driver_name: Unique name of the transformation provider. This is the
502 * name of the provider of the transformation. This can be any
503 * arbitrary value, but in the usual case, this contains the
504 * name of the chip or provider and the name of the
505 * transformation algorithm.
506 * @cra_type: Type of the cryptographic transformation. This is a pointer to
507 * struct crypto_type, which implements callbacks common for all
508 * trasnformation types. There are multiple options:
509 * &crypto_blkcipher_type, &crypto_ablkcipher_type,
510 * &crypto_ahash_type, &crypto_aead_type, &crypto_rng_type.
511 * This field might be empty. In that case, there are no common
512 * callbacks. This is the case for: cipher, compress, shash.
513 * @cra_u: Callbacks implementing the transformation. This is a union of
514 * multiple structures. Depending on the type of transformation selected
515 * by @cra_type and @cra_flags above, the associated structure must be
516 * filled with callbacks. This field might be empty. This is the case
517 * for ahash, shash.
518 * @cra_init: Initialize the cryptographic transformation object. This function
519 * is used to initialize the cryptographic transformation object.
520 * This function is called only once at the instantiation time, right
521 * after the transformation context was allocated. In case the
522 * cryptographic hardware has some special requirements which need to
523 * be handled by software, this function shall check for the precise
524 * requirement of the transformation and put any software fallbacks
525 * in place.
526 * @cra_exit: Deinitialize the cryptographic transformation object. This is a
527 * counterpart to @cra_init, used to remove various changes set in
528 * @cra_init.
529 * @cra_module: Owner of this transformation implementation. Set to THIS_MODULE
530 * @cra_list: internally used
531 * @cra_users: internally used
532 * @cra_refcnt: internally used
533 * @cra_destroy: internally used
534 *
535 * The struct crypto_alg describes a generic Crypto API algorithm and is common
536 * for all of the transformations. Any variable not documented here shall not
537 * be used by a cipher implementation as it is internal to the Crypto API.
538 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539struct crypto_alg {
540 struct list_head cra_list;
Herbert Xu6bfd4802006-09-21 11:39:29 +1000541 struct list_head cra_users;
542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 u32 cra_flags;
544 unsigned int cra_blocksize;
545 unsigned int cra_ctxsize;
Herbert Xu95477372005-07-06 13:52:09 -0700546 unsigned int cra_alignmask;
Herbert Xu5cb14542005-11-05 16:58:14 +1100547
548 int cra_priority;
Herbert Xu6521f302006-08-06 20:28:44 +1000549 atomic_t cra_refcnt;
Herbert Xu5cb14542005-11-05 16:58:14 +1100550
Herbert Xud913ea02006-05-21 08:45:26 +1000551 char cra_name[CRYPTO_MAX_ALG_NAME];
552 char cra_driver_name[CRYPTO_MAX_ALG_NAME];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
Herbert Xue853c3c2006-08-22 00:06:54 +1000554 const struct crypto_type *cra_type;
555
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 union {
Herbert Xub5b7f082007-04-16 20:48:54 +1000557 struct ablkcipher_alg ablkcipher;
Herbert Xu1ae97822007-08-30 15:36:14 +0800558 struct aead_alg aead;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000559 struct blkcipher_alg blkcipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 struct cipher_alg cipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 struct compress_alg compress;
Neil Horman17f0f4a2008-08-14 22:15:52 +1000562 struct rng_alg rng;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 } cra_u;
Herbert Xuc7fc0592006-05-24 13:02:26 +1000564
565 int (*cra_init)(struct crypto_tfm *tfm);
566 void (*cra_exit)(struct crypto_tfm *tfm);
Herbert Xu6521f302006-08-06 20:28:44 +1000567 void (*cra_destroy)(struct crypto_alg *alg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
569 struct module *cra_module;
570};
571
572/*
573 * Algorithm registration interface.
574 */
575int crypto_register_alg(struct crypto_alg *alg);
576int crypto_unregister_alg(struct crypto_alg *alg);
Mark Brown4b004342012-01-17 23:34:26 +0000577int crypto_register_algs(struct crypto_alg *algs, int count);
578int crypto_unregister_algs(struct crypto_alg *algs, int count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
580/*
581 * Algorithm query interface.
582 */
Herbert Xufce32d72006-08-26 17:35:45 +1000583int crypto_has_alg(const char *name, u32 type, u32 mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
585/*
586 * Transforms: user-instantiated objects which encapsulate algorithms
Herbert Xu6d7d6842006-07-30 11:53:01 +1000587 * and core processing logic. Managed via crypto_alloc_*() and
588 * crypto_free_*(), as well as the various helpers below.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
Herbert Xu32e39832007-03-24 14:35:34 +1100591struct ablkcipher_tfm {
592 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
593 unsigned int keylen);
594 int (*encrypt)(struct ablkcipher_request *req);
595 int (*decrypt)(struct ablkcipher_request *req);
Herbert Xu61da88e2007-12-17 21:51:27 +0800596 int (*givencrypt)(struct skcipher_givcrypt_request *req);
597 int (*givdecrypt)(struct skcipher_givcrypt_request *req);
598
Herbert Xuecfc4322007-12-05 21:08:36 +1100599 struct crypto_ablkcipher *base;
600
Herbert Xu32e39832007-03-24 14:35:34 +1100601 unsigned int ivsize;
602 unsigned int reqsize;
603};
604
Herbert Xu1ae97822007-08-30 15:36:14 +0800605struct aead_tfm {
606 int (*setkey)(struct crypto_aead *tfm, const u8 *key,
607 unsigned int keylen);
608 int (*encrypt)(struct aead_request *req);
609 int (*decrypt)(struct aead_request *req);
Herbert Xu743edf52007-12-10 16:18:01 +0800610 int (*givencrypt)(struct aead_givcrypt_request *req);
611 int (*givdecrypt)(struct aead_givcrypt_request *req);
Herbert Xu5b6d2d72007-12-12 19:23:36 +0800612
613 struct crypto_aead *base;
614
Herbert Xu1ae97822007-08-30 15:36:14 +0800615 unsigned int ivsize;
616 unsigned int authsize;
617 unsigned int reqsize;
618};
619
Herbert Xu5cde0af2006-08-22 00:07:53 +1000620struct blkcipher_tfm {
621 void *iv;
622 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
623 unsigned int keylen);
624 int (*encrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
625 struct scatterlist *src, unsigned int nbytes);
626 int (*decrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
627 struct scatterlist *src, unsigned int nbytes);
628};
629
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630struct cipher_tfm {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 int (*cit_setkey)(struct crypto_tfm *tfm,
632 const u8 *key, unsigned int keylen);
Herbert Xuf28776a2006-08-13 20:58:18 +1000633 void (*cit_encrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
634 void (*cit_decrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635};
636
Herbert Xu055bcee2006-08-19 22:24:23 +1000637struct hash_tfm {
638 int (*init)(struct hash_desc *desc);
639 int (*update)(struct hash_desc *desc,
640 struct scatterlist *sg, unsigned int nsg);
641 int (*final)(struct hash_desc *desc, u8 *out);
642 int (*digest)(struct hash_desc *desc, struct scatterlist *sg,
643 unsigned int nsg, u8 *out);
644 int (*setkey)(struct crypto_hash *tfm, const u8 *key,
645 unsigned int keylen);
Herbert Xu055bcee2006-08-19 22:24:23 +1000646 unsigned int digestsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647};
648
649struct compress_tfm {
650 int (*cot_compress)(struct crypto_tfm *tfm,
651 const u8 *src, unsigned int slen,
652 u8 *dst, unsigned int *dlen);
653 int (*cot_decompress)(struct crypto_tfm *tfm,
654 const u8 *src, unsigned int slen,
655 u8 *dst, unsigned int *dlen);
656};
657
Herbert Xu32e39832007-03-24 14:35:34 +1100658#define crt_ablkcipher crt_u.ablkcipher
Herbert Xu1ae97822007-08-30 15:36:14 +0800659#define crt_aead crt_u.aead
Herbert Xu5cde0af2006-08-22 00:07:53 +1000660#define crt_blkcipher crt_u.blkcipher
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661#define crt_cipher crt_u.cipher
Herbert Xu055bcee2006-08-19 22:24:23 +1000662#define crt_hash crt_u.hash
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663#define crt_compress crt_u.compress
664
665struct crypto_tfm {
666
667 u32 crt_flags;
668
669 union {
Herbert Xu32e39832007-03-24 14:35:34 +1100670 struct ablkcipher_tfm ablkcipher;
Herbert Xu1ae97822007-08-30 15:36:14 +0800671 struct aead_tfm aead;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000672 struct blkcipher_tfm blkcipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 struct cipher_tfm cipher;
Herbert Xu055bcee2006-08-19 22:24:23 +1000674 struct hash_tfm hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 struct compress_tfm compress;
676 } crt_u;
Herbert Xu4a779482008-09-13 18:19:03 -0700677
678 void (*exit)(struct crypto_tfm *tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
680 struct crypto_alg *__crt_alg;
Herbert Xuf10b7892006-01-25 22:34:01 +1100681
Herbert Xu79911102006-08-21 21:03:52 +1000682 void *__crt_ctx[] CRYPTO_MINALIGN_ATTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683};
684
Herbert Xu32e39832007-03-24 14:35:34 +1100685struct crypto_ablkcipher {
686 struct crypto_tfm base;
687};
688
Herbert Xu1ae97822007-08-30 15:36:14 +0800689struct crypto_aead {
690 struct crypto_tfm base;
691};
692
Herbert Xu5cde0af2006-08-22 00:07:53 +1000693struct crypto_blkcipher {
694 struct crypto_tfm base;
695};
696
Herbert Xu78a1fe42006-12-24 10:02:00 +1100697struct crypto_cipher {
698 struct crypto_tfm base;
699};
700
701struct crypto_comp {
702 struct crypto_tfm base;
703};
704
Herbert Xu055bcee2006-08-19 22:24:23 +1000705struct crypto_hash {
706 struct crypto_tfm base;
707};
708
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000709enum {
710 CRYPTOA_UNSPEC,
711 CRYPTOA_ALG,
Herbert Xuebc610e2007-01-01 18:37:02 +1100712 CRYPTOA_TYPE,
Herbert Xu39e1ee012007-08-29 19:27:26 +0800713 CRYPTOA_U32,
Herbert Xuebc610e2007-01-01 18:37:02 +1100714 __CRYPTOA_MAX,
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000715};
716
Herbert Xuebc610e2007-01-01 18:37:02 +1100717#define CRYPTOA_MAX (__CRYPTOA_MAX - 1)
718
Herbert Xu39e1ee012007-08-29 19:27:26 +0800719/* Maximum number of (rtattr) parameters for each template. */
720#define CRYPTO_MAX_ATTRS 32
721
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000722struct crypto_attr_alg {
723 char name[CRYPTO_MAX_ALG_NAME];
724};
725
Herbert Xuebc610e2007-01-01 18:37:02 +1100726struct crypto_attr_type {
727 u32 type;
728 u32 mask;
729};
730
Herbert Xu39e1ee012007-08-29 19:27:26 +0800731struct crypto_attr_u32 {
732 u32 num;
733};
734
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735/*
736 * Transform user interface.
737 */
738
Herbert Xu6d7d6842006-07-30 11:53:01 +1000739struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask);
Herbert Xu7b2cd922009-02-05 16:48:24 +1100740void crypto_destroy_tfm(void *mem, struct crypto_tfm *tfm);
741
742static inline void crypto_free_tfm(struct crypto_tfm *tfm)
743{
744 return crypto_destroy_tfm(tfm, tfm);
745}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
Herbert Xuda7f0332008-07-31 17:08:25 +0800747int alg_test(const char *driver, const char *alg, u32 type, u32 mask);
748
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749/*
750 * Transform helpers which query the underlying algorithm.
751 */
752static inline const char *crypto_tfm_alg_name(struct crypto_tfm *tfm)
753{
754 return tfm->__crt_alg->cra_name;
755}
756
Michal Ludvigb14cdd62006-07-09 09:02:24 +1000757static inline const char *crypto_tfm_alg_driver_name(struct crypto_tfm *tfm)
758{
759 return tfm->__crt_alg->cra_driver_name;
760}
761
762static inline int crypto_tfm_alg_priority(struct crypto_tfm *tfm)
763{
764 return tfm->__crt_alg->cra_priority;
765}
766
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm)
768{
769 return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK;
770}
771
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm)
773{
774 return tfm->__crt_alg->cra_blocksize;
775}
776
Herbert Xufbdae9f2005-07-06 13:53:29 -0700777static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm *tfm)
778{
779 return tfm->__crt_alg->cra_alignmask;
780}
781
Herbert Xuf28776a2006-08-13 20:58:18 +1000782static inline u32 crypto_tfm_get_flags(struct crypto_tfm *tfm)
783{
784 return tfm->crt_flags;
785}
786
787static inline void crypto_tfm_set_flags(struct crypto_tfm *tfm, u32 flags)
788{
789 tfm->crt_flags |= flags;
790}
791
792static inline void crypto_tfm_clear_flags(struct crypto_tfm *tfm, u32 flags)
793{
794 tfm->crt_flags &= ~flags;
795}
796
Herbert Xu40725182005-07-06 13:51:52 -0700797static inline void *crypto_tfm_ctx(struct crypto_tfm *tfm)
798{
Herbert Xuf10b7892006-01-25 22:34:01 +1100799 return tfm->__crt_ctx;
800}
801
802static inline unsigned int crypto_tfm_ctx_alignment(void)
803{
804 struct crypto_tfm *tfm;
805 return __alignof__(tfm->__crt_ctx);
Herbert Xu40725182005-07-06 13:51:52 -0700806}
807
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808/*
809 * API wrappers.
810 */
Herbert Xu32e39832007-03-24 14:35:34 +1100811static inline struct crypto_ablkcipher *__crypto_ablkcipher_cast(
812 struct crypto_tfm *tfm)
813{
814 return (struct crypto_ablkcipher *)tfm;
815}
816
Herbert Xu378f4f52007-12-17 20:07:31 +0800817static inline u32 crypto_skcipher_type(u32 type)
818{
Herbert Xuecfc4322007-12-05 21:08:36 +1100819 type &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
Herbert Xu378f4f52007-12-17 20:07:31 +0800820 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
821 return type;
822}
823
824static inline u32 crypto_skcipher_mask(u32 mask)
825{
Herbert Xuecfc4322007-12-05 21:08:36 +1100826 mask &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
Herbert Xu378f4f52007-12-17 20:07:31 +0800827 mask |= CRYPTO_ALG_TYPE_BLKCIPHER_MASK;
828 return mask;
829}
830
Stephan Muellerf13ec332014-11-12 05:28:22 +0100831/**
832 * DOC: Asynchronous Block Cipher API
833 *
834 * Asynchronous block cipher API is used with the ciphers of type
835 * CRYPTO_ALG_TYPE_ABLKCIPHER (listed as type "ablkcipher" in /proc/crypto).
836 *
837 * Asynchronous cipher operations imply that the function invocation for a
838 * cipher request returns immediately before the completion of the operation.
839 * The cipher request is scheduled as a separate kernel thread and therefore
840 * load-balanced on the different CPUs via the process scheduler. To allow
841 * the kernel crypto API to inform the caller about the completion of a cipher
842 * request, the caller must provide a callback function. That function is
843 * invoked with the cipher handle when the request completes.
844 *
845 * To support the asynchronous operation, additional information than just the
846 * cipher handle must be supplied to the kernel crypto API. That additional
847 * information is given by filling in the ablkcipher_request data structure.
848 *
849 * For the asynchronous block cipher API, the state is maintained with the tfm
850 * cipher handle. A single tfm can be used across multiple calls and in
851 * parallel. For asynchronous block cipher calls, context data supplied and
852 * only used by the caller can be referenced the request data structure in
853 * addition to the IV used for the cipher request. The maintenance of such
854 * state information would be important for a crypto driver implementer to
855 * have, because when calling the callback function upon completion of the
856 * cipher operation, that callback function may need some information about
857 * which operation just finished if it invoked multiple in parallel. This
858 * state information is unused by the kernel crypto API.
859 */
860
861/**
862 * crypto_alloc_ablkcipher() - allocate asynchronous block cipher handle
863 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
864 * ablkcipher cipher
865 * @type: specifies the type of the cipher
866 * @mask: specifies the mask for the cipher
867 *
868 * Allocate a cipher handle for an ablkcipher. The returned struct
869 * crypto_ablkcipher is the cipher handle that is required for any subsequent
870 * API invocation for that ablkcipher.
871 *
872 * Return: allocated cipher handle in case of success; IS_ERR() is true in case
873 * of an error, PTR_ERR() returns the error code.
874 */
Herbert Xub9c55aa2007-12-04 12:46:48 +1100875struct crypto_ablkcipher *crypto_alloc_ablkcipher(const char *alg_name,
876 u32 type, u32 mask);
Herbert Xu32e39832007-03-24 14:35:34 +1100877
878static inline struct crypto_tfm *crypto_ablkcipher_tfm(
879 struct crypto_ablkcipher *tfm)
880{
881 return &tfm->base;
882}
883
Stephan Muellerf13ec332014-11-12 05:28:22 +0100884/**
885 * crypto_free_ablkcipher() - zeroize and free cipher handle
886 * @tfm: cipher handle to be freed
887 */
Herbert Xu32e39832007-03-24 14:35:34 +1100888static inline void crypto_free_ablkcipher(struct crypto_ablkcipher *tfm)
889{
890 crypto_free_tfm(crypto_ablkcipher_tfm(tfm));
891}
892
Stephan Muellerf13ec332014-11-12 05:28:22 +0100893/**
894 * crypto_has_ablkcipher() - Search for the availability of an ablkcipher.
895 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
896 * ablkcipher
897 * @type: specifies the type of the cipher
898 * @mask: specifies the mask for the cipher
899 *
900 * Return: true when the ablkcipher is known to the kernel crypto API; false
901 * otherwise
902 */
Herbert Xu32e39832007-03-24 14:35:34 +1100903static inline int crypto_has_ablkcipher(const char *alg_name, u32 type,
904 u32 mask)
905{
Herbert Xu378f4f52007-12-17 20:07:31 +0800906 return crypto_has_alg(alg_name, crypto_skcipher_type(type),
907 crypto_skcipher_mask(mask));
Herbert Xu32e39832007-03-24 14:35:34 +1100908}
909
910static inline struct ablkcipher_tfm *crypto_ablkcipher_crt(
911 struct crypto_ablkcipher *tfm)
912{
913 return &crypto_ablkcipher_tfm(tfm)->crt_ablkcipher;
914}
915
Stephan Muellerf13ec332014-11-12 05:28:22 +0100916/**
917 * crypto_ablkcipher_ivsize() - obtain IV size
918 * @tfm: cipher handle
919 *
920 * The size of the IV for the ablkcipher referenced by the cipher handle is
921 * returned. This IV size may be zero if the cipher does not need an IV.
922 *
923 * Return: IV size in bytes
924 */
Herbert Xu32e39832007-03-24 14:35:34 +1100925static inline unsigned int crypto_ablkcipher_ivsize(
926 struct crypto_ablkcipher *tfm)
927{
928 return crypto_ablkcipher_crt(tfm)->ivsize;
929}
930
Stephan Muellerf13ec332014-11-12 05:28:22 +0100931/**
932 * crypto_ablkcipher_blocksize() - obtain block size of cipher
933 * @tfm: cipher handle
934 *
935 * The block size for the ablkcipher referenced with the cipher handle is
936 * returned. The caller may use that information to allocate appropriate
937 * memory for the data returned by the encryption or decryption operation
938 *
939 * Return: block size of cipher
940 */
Herbert Xu32e39832007-03-24 14:35:34 +1100941static inline unsigned int crypto_ablkcipher_blocksize(
942 struct crypto_ablkcipher *tfm)
943{
944 return crypto_tfm_alg_blocksize(crypto_ablkcipher_tfm(tfm));
945}
946
947static inline unsigned int crypto_ablkcipher_alignmask(
948 struct crypto_ablkcipher *tfm)
949{
950 return crypto_tfm_alg_alignmask(crypto_ablkcipher_tfm(tfm));
951}
952
953static inline u32 crypto_ablkcipher_get_flags(struct crypto_ablkcipher *tfm)
954{
955 return crypto_tfm_get_flags(crypto_ablkcipher_tfm(tfm));
956}
957
958static inline void crypto_ablkcipher_set_flags(struct crypto_ablkcipher *tfm,
959 u32 flags)
960{
961 crypto_tfm_set_flags(crypto_ablkcipher_tfm(tfm), flags);
962}
963
964static inline void crypto_ablkcipher_clear_flags(struct crypto_ablkcipher *tfm,
965 u32 flags)
966{
967 crypto_tfm_clear_flags(crypto_ablkcipher_tfm(tfm), flags);
968}
969
Stephan Muellerf13ec332014-11-12 05:28:22 +0100970/**
971 * crypto_ablkcipher_setkey() - set key for cipher
972 * @tfm: cipher handle
973 * @key: buffer holding the key
974 * @keylen: length of the key in bytes
975 *
976 * The caller provided key is set for the ablkcipher referenced by the cipher
977 * handle.
978 *
979 * Note, the key length determines the cipher type. Many block ciphers implement
980 * different cipher modes depending on the key size, such as AES-128 vs AES-192
981 * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
982 * is performed.
983 *
984 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
985 */
Herbert Xu32e39832007-03-24 14:35:34 +1100986static inline int crypto_ablkcipher_setkey(struct crypto_ablkcipher *tfm,
987 const u8 *key, unsigned int keylen)
988{
Herbert Xuecfc4322007-12-05 21:08:36 +1100989 struct ablkcipher_tfm *crt = crypto_ablkcipher_crt(tfm);
990
991 return crt->setkey(crt->base, key, keylen);
Herbert Xu32e39832007-03-24 14:35:34 +1100992}
993
Stephan Muellerf13ec332014-11-12 05:28:22 +0100994/**
995 * crypto_ablkcipher_reqtfm() - obtain cipher handle from request
996 * @req: ablkcipher_request out of which the cipher handle is to be obtained
997 *
998 * Return the crypto_ablkcipher handle when furnishing an ablkcipher_request
999 * data structure.
1000 *
1001 * Return: crypto_ablkcipher handle
1002 */
Herbert Xu32e39832007-03-24 14:35:34 +11001003static inline struct crypto_ablkcipher *crypto_ablkcipher_reqtfm(
1004 struct ablkcipher_request *req)
1005{
1006 return __crypto_ablkcipher_cast(req->base.tfm);
1007}
1008
Stephan Muellerf13ec332014-11-12 05:28:22 +01001009/**
1010 * crypto_ablkcipher_encrypt() - encrypt plaintext
1011 * @req: reference to the ablkcipher_request handle that holds all information
1012 * needed to perform the cipher operation
1013 *
1014 * Encrypt plaintext data using the ablkcipher_request handle. That data
1015 * structure and how it is filled with data is discussed with the
1016 * ablkcipher_request_* functions.
1017 *
1018 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1019 */
Herbert Xu32e39832007-03-24 14:35:34 +11001020static inline int crypto_ablkcipher_encrypt(struct ablkcipher_request *req)
1021{
1022 struct ablkcipher_tfm *crt =
1023 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
1024 return crt->encrypt(req);
1025}
1026
Stephan Muellerf13ec332014-11-12 05:28:22 +01001027/**
1028 * crypto_ablkcipher_decrypt() - decrypt ciphertext
1029 * @req: reference to the ablkcipher_request handle that holds all information
1030 * needed to perform the cipher operation
1031 *
1032 * Decrypt ciphertext data using the ablkcipher_request handle. That data
1033 * structure and how it is filled with data is discussed with the
1034 * ablkcipher_request_* functions.
1035 *
1036 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1037 */
Herbert Xu32e39832007-03-24 14:35:34 +11001038static inline int crypto_ablkcipher_decrypt(struct ablkcipher_request *req)
1039{
1040 struct ablkcipher_tfm *crt =
1041 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
1042 return crt->decrypt(req);
1043}
1044
Stephan Muellerf13ec332014-11-12 05:28:22 +01001045/**
1046 * DOC: Asynchronous Cipher Request Handle
1047 *
1048 * The ablkcipher_request data structure contains all pointers to data
1049 * required for the asynchronous cipher operation. This includes the cipher
1050 * handle (which can be used by multiple ablkcipher_request instances), pointer
1051 * to plaintext and ciphertext, asynchronous callback function, etc. It acts
1052 * as a handle to the ablkcipher_request_* API calls in a similar way as
1053 * ablkcipher handle to the crypto_ablkcipher_* API calls.
1054 */
1055
1056/**
1057 * crypto_ablkcipher_reqsize() - obtain size of the request data structure
1058 * @tfm: cipher handle
1059 *
1060 * Return: number of bytes
1061 */
Herbert Xub16c3a22007-08-29 19:02:04 +08001062static inline unsigned int crypto_ablkcipher_reqsize(
1063 struct crypto_ablkcipher *tfm)
Herbert Xu32e39832007-03-24 14:35:34 +11001064{
1065 return crypto_ablkcipher_crt(tfm)->reqsize;
1066}
1067
Stephan Muellerf13ec332014-11-12 05:28:22 +01001068/**
1069 * ablkcipher_request_set_tfm() - update cipher handle reference in request
1070 * @req: request handle to be modified
1071 * @tfm: cipher handle that shall be added to the request handle
1072 *
1073 * Allow the caller to replace the existing ablkcipher handle in the request
1074 * data structure with a different one.
1075 */
Herbert Xue196d622007-04-14 16:09:14 +10001076static inline void ablkcipher_request_set_tfm(
1077 struct ablkcipher_request *req, struct crypto_ablkcipher *tfm)
1078{
Herbert Xuecfc4322007-12-05 21:08:36 +11001079 req->base.tfm = crypto_ablkcipher_tfm(crypto_ablkcipher_crt(tfm)->base);
Herbert Xue196d622007-04-14 16:09:14 +10001080}
1081
Herbert Xub5b7f082007-04-16 20:48:54 +10001082static inline struct ablkcipher_request *ablkcipher_request_cast(
1083 struct crypto_async_request *req)
1084{
1085 return container_of(req, struct ablkcipher_request, base);
1086}
1087
Stephan Muellerf13ec332014-11-12 05:28:22 +01001088/**
1089 * ablkcipher_request_alloc() - allocate request data structure
1090 * @tfm: cipher handle to be registered with the request
1091 * @gfp: memory allocation flag that is handed to kmalloc by the API call.
1092 *
1093 * Allocate the request data structure that must be used with the ablkcipher
1094 * encrypt and decrypt API calls. During the allocation, the provided ablkcipher
1095 * handle is registered in the request data structure.
1096 *
1097 * Return: allocated request handle in case of success; IS_ERR() is true in case
1098 * of an error, PTR_ERR() returns the error code.
1099 */
Herbert Xu32e39832007-03-24 14:35:34 +11001100static inline struct ablkcipher_request *ablkcipher_request_alloc(
1101 struct crypto_ablkcipher *tfm, gfp_t gfp)
1102{
1103 struct ablkcipher_request *req;
1104
1105 req = kmalloc(sizeof(struct ablkcipher_request) +
1106 crypto_ablkcipher_reqsize(tfm), gfp);
1107
1108 if (likely(req))
Herbert Xue196d622007-04-14 16:09:14 +10001109 ablkcipher_request_set_tfm(req, tfm);
Herbert Xu32e39832007-03-24 14:35:34 +11001110
1111 return req;
1112}
1113
Stephan Muellerf13ec332014-11-12 05:28:22 +01001114/**
1115 * ablkcipher_request_free() - zeroize and free request data structure
1116 * @req: request data structure cipher handle to be freed
1117 */
Herbert Xu32e39832007-03-24 14:35:34 +11001118static inline void ablkcipher_request_free(struct ablkcipher_request *req)
1119{
Herbert Xuaef73cf2009-07-11 22:22:14 +08001120 kzfree(req);
Herbert Xu32e39832007-03-24 14:35:34 +11001121}
1122
Stephan Muellerf13ec332014-11-12 05:28:22 +01001123/**
1124 * ablkcipher_request_set_callback() - set asynchronous callback function
1125 * @req: request handle
1126 * @flags: specify zero or an ORing of the flags
1127 * CRYPTO_TFM_REQ_MAY_BACKLOG the request queue may back log and
1128 * increase the wait queue beyond the initial maximum size;
1129 * CRYPTO_TFM_REQ_MAY_SLEEP the request processing may sleep
1130 * @compl: callback function pointer to be registered with the request handle
1131 * @data: The data pointer refers to memory that is not used by the kernel
1132 * crypto API, but provided to the callback function for it to use. Here,
1133 * the caller can provide a reference to memory the callback function can
1134 * operate on. As the callback function is invoked asynchronously to the
1135 * related functionality, it may need to access data structures of the
1136 * related functionality which can be referenced using this pointer. The
1137 * callback function can access the memory via the "data" field in the
1138 * crypto_async_request data structure provided to the callback function.
1139 *
1140 * This function allows setting the callback function that is triggered once the
1141 * cipher operation completes.
1142 *
1143 * The callback function is registered with the ablkcipher_request handle and
Stephan Mueller379dcfb2015-01-19 00:13:39 +01001144 * must comply with the following template
Stephan Muellerf13ec332014-11-12 05:28:22 +01001145 *
1146 * void callback_function(struct crypto_async_request *req, int error)
1147 */
Herbert Xu32e39832007-03-24 14:35:34 +11001148static inline void ablkcipher_request_set_callback(
1149 struct ablkcipher_request *req,
Mark Rustad3e3dc252014-07-25 02:53:38 -07001150 u32 flags, crypto_completion_t compl, void *data)
Herbert Xu32e39832007-03-24 14:35:34 +11001151{
Mark Rustad3e3dc252014-07-25 02:53:38 -07001152 req->base.complete = compl;
Herbert Xu32e39832007-03-24 14:35:34 +11001153 req->base.data = data;
1154 req->base.flags = flags;
1155}
1156
Stephan Muellerf13ec332014-11-12 05:28:22 +01001157/**
1158 * ablkcipher_request_set_crypt() - set data buffers
1159 * @req: request handle
1160 * @src: source scatter / gather list
1161 * @dst: destination scatter / gather list
1162 * @nbytes: number of bytes to process from @src
1163 * @iv: IV for the cipher operation which must comply with the IV size defined
1164 * by crypto_ablkcipher_ivsize
1165 *
1166 * This function allows setting of the source data and destination data
1167 * scatter / gather lists.
1168 *
1169 * For encryption, the source is treated as the plaintext and the
1170 * destination is the ciphertext. For a decryption operation, the use is
Stephan Mueller379dcfb2015-01-19 00:13:39 +01001171 * reversed - the source is the ciphertext and the destination is the plaintext.
Stephan Muellerf13ec332014-11-12 05:28:22 +01001172 */
Herbert Xu32e39832007-03-24 14:35:34 +11001173static inline void ablkcipher_request_set_crypt(
1174 struct ablkcipher_request *req,
1175 struct scatterlist *src, struct scatterlist *dst,
1176 unsigned int nbytes, void *iv)
1177{
1178 req->src = src;
1179 req->dst = dst;
1180 req->nbytes = nbytes;
1181 req->info = iv;
1182}
1183
Stephan Muellerfced7b02014-11-12 05:29:00 +01001184/**
1185 * DOC: Authenticated Encryption With Associated Data (AEAD) Cipher API
1186 *
1187 * The AEAD cipher API is used with the ciphers of type CRYPTO_ALG_TYPE_AEAD
1188 * (listed as type "aead" in /proc/crypto)
1189 *
1190 * The most prominent examples for this type of encryption is GCM and CCM.
1191 * However, the kernel supports other types of AEAD ciphers which are defined
1192 * with the following cipher string:
1193 *
1194 * authenc(keyed message digest, block cipher)
1195 *
1196 * For example: authenc(hmac(sha256), cbc(aes))
1197 *
1198 * The example code provided for the asynchronous block cipher operation
1199 * applies here as well. Naturally all *ablkcipher* symbols must be exchanged
1200 * the *aead* pendants discussed in the following. In addtion, for the AEAD
1201 * operation, the aead_request_set_assoc function must be used to set the
1202 * pointer to the associated data memory location before performing the
1203 * encryption or decryption operation. In case of an encryption, the associated
1204 * data memory is filled during the encryption operation. For decryption, the
1205 * associated data memory must contain data that is used to verify the integrity
1206 * of the decrypted data. Another deviation from the asynchronous block cipher
1207 * operation is that the caller should explicitly check for -EBADMSG of the
1208 * crypto_aead_decrypt. That error indicates an authentication error, i.e.
1209 * a breach in the integrity of the message. In essence, that -EBADMSG error
1210 * code is the key bonus an AEAD cipher has over "standard" block chaining
1211 * modes.
1212 */
1213
Herbert Xu1ae97822007-08-30 15:36:14 +08001214static inline struct crypto_aead *__crypto_aead_cast(struct crypto_tfm *tfm)
1215{
1216 return (struct crypto_aead *)tfm;
1217}
1218
Stephan Muellerfced7b02014-11-12 05:29:00 +01001219/**
1220 * crypto_alloc_aead() - allocate AEAD cipher handle
1221 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1222 * AEAD cipher
1223 * @type: specifies the type of the cipher
1224 * @mask: specifies the mask for the cipher
1225 *
1226 * Allocate a cipher handle for an AEAD. The returned struct
1227 * crypto_aead is the cipher handle that is required for any subsequent
1228 * API invocation for that AEAD.
1229 *
1230 * Return: allocated cipher handle in case of success; IS_ERR() is true in case
1231 * of an error, PTR_ERR() returns the error code.
1232 */
Herbert Xud29ce982007-12-12 19:24:27 +08001233struct crypto_aead *crypto_alloc_aead(const char *alg_name, u32 type, u32 mask);
Herbert Xu1ae97822007-08-30 15:36:14 +08001234
1235static inline struct crypto_tfm *crypto_aead_tfm(struct crypto_aead *tfm)
1236{
1237 return &tfm->base;
1238}
1239
Stephan Muellerfced7b02014-11-12 05:29:00 +01001240/**
1241 * crypto_free_aead() - zeroize and free aead handle
1242 * @tfm: cipher handle to be freed
1243 */
Herbert Xu1ae97822007-08-30 15:36:14 +08001244static inline void crypto_free_aead(struct crypto_aead *tfm)
1245{
1246 crypto_free_tfm(crypto_aead_tfm(tfm));
1247}
1248
1249static inline struct aead_tfm *crypto_aead_crt(struct crypto_aead *tfm)
1250{
1251 return &crypto_aead_tfm(tfm)->crt_aead;
1252}
1253
Stephan Muellerfced7b02014-11-12 05:29:00 +01001254/**
1255 * crypto_aead_ivsize() - obtain IV size
1256 * @tfm: cipher handle
1257 *
1258 * The size of the IV for the aead referenced by the cipher handle is
1259 * returned. This IV size may be zero if the cipher does not need an IV.
1260 *
1261 * Return: IV size in bytes
1262 */
Herbert Xu1ae97822007-08-30 15:36:14 +08001263static inline unsigned int crypto_aead_ivsize(struct crypto_aead *tfm)
1264{
1265 return crypto_aead_crt(tfm)->ivsize;
1266}
1267
Stephan Muellerfced7b02014-11-12 05:29:00 +01001268/**
1269 * crypto_aead_authsize() - obtain maximum authentication data size
1270 * @tfm: cipher handle
1271 *
1272 * The maximum size of the authentication data for the AEAD cipher referenced
1273 * by the AEAD cipher handle is returned. The authentication data size may be
1274 * zero if the cipher implements a hard-coded maximum.
1275 *
1276 * The authentication data may also be known as "tag value".
1277 *
1278 * Return: authentication data size / tag size in bytes
1279 */
Herbert Xu1ae97822007-08-30 15:36:14 +08001280static inline unsigned int crypto_aead_authsize(struct crypto_aead *tfm)
1281{
1282 return crypto_aead_crt(tfm)->authsize;
1283}
1284
Stephan Muellerfced7b02014-11-12 05:29:00 +01001285/**
1286 * crypto_aead_blocksize() - obtain block size of cipher
1287 * @tfm: cipher handle
1288 *
1289 * The block size for the AEAD referenced with the cipher handle is returned.
1290 * The caller may use that information to allocate appropriate memory for the
1291 * data returned by the encryption or decryption operation
1292 *
1293 * Return: block size of cipher
1294 */
Herbert Xu1ae97822007-08-30 15:36:14 +08001295static inline unsigned int crypto_aead_blocksize(struct crypto_aead *tfm)
1296{
1297 return crypto_tfm_alg_blocksize(crypto_aead_tfm(tfm));
1298}
1299
1300static inline unsigned int crypto_aead_alignmask(struct crypto_aead *tfm)
1301{
1302 return crypto_tfm_alg_alignmask(crypto_aead_tfm(tfm));
1303}
1304
1305static inline u32 crypto_aead_get_flags(struct crypto_aead *tfm)
1306{
1307 return crypto_tfm_get_flags(crypto_aead_tfm(tfm));
1308}
1309
1310static inline void crypto_aead_set_flags(struct crypto_aead *tfm, u32 flags)
1311{
1312 crypto_tfm_set_flags(crypto_aead_tfm(tfm), flags);
1313}
1314
1315static inline void crypto_aead_clear_flags(struct crypto_aead *tfm, u32 flags)
1316{
1317 crypto_tfm_clear_flags(crypto_aead_tfm(tfm), flags);
1318}
1319
Stephan Muellerfced7b02014-11-12 05:29:00 +01001320/**
1321 * crypto_aead_setkey() - set key for cipher
1322 * @tfm: cipher handle
1323 * @key: buffer holding the key
1324 * @keylen: length of the key in bytes
1325 *
1326 * The caller provided key is set for the AEAD referenced by the cipher
1327 * handle.
1328 *
1329 * Note, the key length determines the cipher type. Many block ciphers implement
1330 * different cipher modes depending on the key size, such as AES-128 vs AES-192
1331 * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
1332 * is performed.
1333 *
1334 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
1335 */
Herbert Xu1ae97822007-08-30 15:36:14 +08001336static inline int crypto_aead_setkey(struct crypto_aead *tfm, const u8 *key,
1337 unsigned int keylen)
1338{
Herbert Xu5b6d2d72007-12-12 19:23:36 +08001339 struct aead_tfm *crt = crypto_aead_crt(tfm);
1340
1341 return crt->setkey(crt->base, key, keylen);
Herbert Xu1ae97822007-08-30 15:36:14 +08001342}
1343
Stephan Muellerfced7b02014-11-12 05:29:00 +01001344/**
1345 * crypto_aead_setauthsize() - set authentication data size
1346 * @tfm: cipher handle
1347 * @authsize: size of the authentication data / tag in bytes
1348 *
1349 * Set the authentication data size / tag size. AEAD requires an authentication
1350 * tag (or MAC) in addition to the associated data.
1351 *
1352 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
1353 */
Herbert Xu7ba683a2007-12-02 18:49:21 +11001354int crypto_aead_setauthsize(struct crypto_aead *tfm, unsigned int authsize);
1355
Herbert Xu1ae97822007-08-30 15:36:14 +08001356static inline struct crypto_aead *crypto_aead_reqtfm(struct aead_request *req)
1357{
1358 return __crypto_aead_cast(req->base.tfm);
1359}
1360
Stephan Muellerfced7b02014-11-12 05:29:00 +01001361/**
1362 * crypto_aead_encrypt() - encrypt plaintext
1363 * @req: reference to the aead_request handle that holds all information
1364 * needed to perform the cipher operation
1365 *
1366 * Encrypt plaintext data using the aead_request handle. That data structure
1367 * and how it is filled with data is discussed with the aead_request_*
1368 * functions.
1369 *
1370 * IMPORTANT NOTE The encryption operation creates the authentication data /
1371 * tag. That data is concatenated with the created ciphertext.
1372 * The ciphertext memory size is therefore the given number of
1373 * block cipher blocks + the size defined by the
1374 * crypto_aead_setauthsize invocation. The caller must ensure
1375 * that sufficient memory is available for the ciphertext and
1376 * the authentication tag.
1377 *
1378 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1379 */
Herbert Xu1ae97822007-08-30 15:36:14 +08001380static inline int crypto_aead_encrypt(struct aead_request *req)
1381{
1382 return crypto_aead_crt(crypto_aead_reqtfm(req))->encrypt(req);
1383}
1384
Stephan Muellerfced7b02014-11-12 05:29:00 +01001385/**
1386 * crypto_aead_decrypt() - decrypt ciphertext
1387 * @req: reference to the ablkcipher_request handle that holds all information
1388 * needed to perform the cipher operation
1389 *
1390 * Decrypt ciphertext data using the aead_request handle. That data structure
1391 * and how it is filled with data is discussed with the aead_request_*
1392 * functions.
1393 *
1394 * IMPORTANT NOTE The caller must concatenate the ciphertext followed by the
1395 * authentication data / tag. That authentication data / tag
1396 * must have the size defined by the crypto_aead_setauthsize
1397 * invocation.
1398 *
1399 *
1400 * Return: 0 if the cipher operation was successful; -EBADMSG: The AEAD
1401 * cipher operation performs the authentication of the data during the
1402 * decryption operation. Therefore, the function returns this error if
1403 * the authentication of the ciphertext was unsuccessful (i.e. the
1404 * integrity of the ciphertext or the associated data was violated);
1405 * < 0 if an error occurred.
1406 */
Herbert Xu1ae97822007-08-30 15:36:14 +08001407static inline int crypto_aead_decrypt(struct aead_request *req)
1408{
Stephan Mueller15acabf2015-01-05 12:21:45 +01001409 if (req->cryptlen < crypto_aead_authsize(crypto_aead_reqtfm(req)))
1410 return -EINVAL;
1411
Herbert Xu1ae97822007-08-30 15:36:14 +08001412 return crypto_aead_crt(crypto_aead_reqtfm(req))->decrypt(req);
1413}
1414
Stephan Muellerfced7b02014-11-12 05:29:00 +01001415/**
1416 * DOC: Asynchronous AEAD Request Handle
1417 *
1418 * The aead_request data structure contains all pointers to data required for
1419 * the AEAD cipher operation. This includes the cipher handle (which can be
1420 * used by multiple aead_request instances), pointer to plaintext and
1421 * ciphertext, asynchronous callback function, etc. It acts as a handle to the
1422 * aead_request_* API calls in a similar way as AEAD handle to the
1423 * crypto_aead_* API calls.
1424 */
1425
1426/**
1427 * crypto_aead_reqsize() - obtain size of the request data structure
1428 * @tfm: cipher handle
1429 *
1430 * Return: number of bytes
1431 */
Herbert Xub16c3a22007-08-29 19:02:04 +08001432static inline unsigned int crypto_aead_reqsize(struct crypto_aead *tfm)
Herbert Xu1ae97822007-08-30 15:36:14 +08001433{
1434 return crypto_aead_crt(tfm)->reqsize;
1435}
1436
Stephan Muellerfced7b02014-11-12 05:29:00 +01001437/**
1438 * aead_request_set_tfm() - update cipher handle reference in request
1439 * @req: request handle to be modified
1440 * @tfm: cipher handle that shall be added to the request handle
1441 *
1442 * Allow the caller to replace the existing aead handle in the request
1443 * data structure with a different one.
1444 */
Herbert Xu1ae97822007-08-30 15:36:14 +08001445static inline void aead_request_set_tfm(struct aead_request *req,
1446 struct crypto_aead *tfm)
1447{
Herbert Xu5b6d2d72007-12-12 19:23:36 +08001448 req->base.tfm = crypto_aead_tfm(crypto_aead_crt(tfm)->base);
Herbert Xu1ae97822007-08-30 15:36:14 +08001449}
1450
Stephan Muellerfced7b02014-11-12 05:29:00 +01001451/**
1452 * aead_request_alloc() - allocate request data structure
1453 * @tfm: cipher handle to be registered with the request
1454 * @gfp: memory allocation flag that is handed to kmalloc by the API call.
1455 *
1456 * Allocate the request data structure that must be used with the AEAD
1457 * encrypt and decrypt API calls. During the allocation, the provided aead
1458 * handle is registered in the request data structure.
1459 *
1460 * Return: allocated request handle in case of success; IS_ERR() is true in case
1461 * of an error, PTR_ERR() returns the error code.
1462 */
Herbert Xu1ae97822007-08-30 15:36:14 +08001463static inline struct aead_request *aead_request_alloc(struct crypto_aead *tfm,
1464 gfp_t gfp)
1465{
1466 struct aead_request *req;
1467
1468 req = kmalloc(sizeof(*req) + crypto_aead_reqsize(tfm), gfp);
1469
1470 if (likely(req))
1471 aead_request_set_tfm(req, tfm);
1472
1473 return req;
1474}
1475
Stephan Muellerfced7b02014-11-12 05:29:00 +01001476/**
1477 * aead_request_free() - zeroize and free request data structure
1478 * @req: request data structure cipher handle to be freed
1479 */
Herbert Xu1ae97822007-08-30 15:36:14 +08001480static inline void aead_request_free(struct aead_request *req)
1481{
Herbert Xuaef73cf2009-07-11 22:22:14 +08001482 kzfree(req);
Herbert Xu1ae97822007-08-30 15:36:14 +08001483}
1484
Stephan Muellerfced7b02014-11-12 05:29:00 +01001485/**
1486 * aead_request_set_callback() - set asynchronous callback function
1487 * @req: request handle
1488 * @flags: specify zero or an ORing of the flags
1489 * CRYPTO_TFM_REQ_MAY_BACKLOG the request queue may back log and
1490 * increase the wait queue beyond the initial maximum size;
1491 * CRYPTO_TFM_REQ_MAY_SLEEP the request processing may sleep
1492 * @compl: callback function pointer to be registered with the request handle
1493 * @data: The data pointer refers to memory that is not used by the kernel
1494 * crypto API, but provided to the callback function for it to use. Here,
1495 * the caller can provide a reference to memory the callback function can
1496 * operate on. As the callback function is invoked asynchronously to the
1497 * related functionality, it may need to access data structures of the
1498 * related functionality which can be referenced using this pointer. The
1499 * callback function can access the memory via the "data" field in the
1500 * crypto_async_request data structure provided to the callback function.
1501 *
1502 * Setting the callback function that is triggered once the cipher operation
1503 * completes
1504 *
1505 * The callback function is registered with the aead_request handle and
Stephan Mueller379dcfb2015-01-19 00:13:39 +01001506 * must comply with the following template
Stephan Muellerfced7b02014-11-12 05:29:00 +01001507 *
1508 * void callback_function(struct crypto_async_request *req, int error)
1509 */
Herbert Xu1ae97822007-08-30 15:36:14 +08001510static inline void aead_request_set_callback(struct aead_request *req,
1511 u32 flags,
Mark Rustad3e3dc252014-07-25 02:53:38 -07001512 crypto_completion_t compl,
Herbert Xu1ae97822007-08-30 15:36:14 +08001513 void *data)
1514{
Mark Rustad3e3dc252014-07-25 02:53:38 -07001515 req->base.complete = compl;
Herbert Xu1ae97822007-08-30 15:36:14 +08001516 req->base.data = data;
1517 req->base.flags = flags;
1518}
1519
Stephan Muellerfced7b02014-11-12 05:29:00 +01001520/**
1521 * aead_request_set_crypt - set data buffers
1522 * @req: request handle
1523 * @src: source scatter / gather list
1524 * @dst: destination scatter / gather list
1525 * @cryptlen: number of bytes to process from @src
1526 * @iv: IV for the cipher operation which must comply with the IV size defined
1527 * by crypto_aead_ivsize()
1528 *
1529 * Setting the source data and destination data scatter / gather lists.
1530 *
1531 * For encryption, the source is treated as the plaintext and the
1532 * destination is the ciphertext. For a decryption operation, the use is
Stephan Mueller379dcfb2015-01-19 00:13:39 +01001533 * reversed - the source is the ciphertext and the destination is the plaintext.
Stephan Muellerfced7b02014-11-12 05:29:00 +01001534 *
1535 * IMPORTANT NOTE AEAD requires an authentication tag (MAC). For decryption,
1536 * the caller must concatenate the ciphertext followed by the
1537 * authentication tag and provide the entire data stream to the
1538 * decryption operation (i.e. the data length used for the
1539 * initialization of the scatterlist and the data length for the
1540 * decryption operation is identical). For encryption, however,
1541 * the authentication tag is created while encrypting the data.
1542 * The destination buffer must hold sufficient space for the
1543 * ciphertext and the authentication tag while the encryption
1544 * invocation must only point to the plaintext data size. The
1545 * following code snippet illustrates the memory usage
1546 * buffer = kmalloc(ptbuflen + (enc ? authsize : 0));
1547 * sg_init_one(&sg, buffer, ptbuflen + (enc ? authsize : 0));
1548 * aead_request_set_crypt(req, &sg, &sg, ptbuflen, iv);
1549 */
Herbert Xu1ae97822007-08-30 15:36:14 +08001550static inline void aead_request_set_crypt(struct aead_request *req,
1551 struct scatterlist *src,
1552 struct scatterlist *dst,
1553 unsigned int cryptlen, u8 *iv)
1554{
1555 req->src = src;
1556 req->dst = dst;
1557 req->cryptlen = cryptlen;
1558 req->iv = iv;
1559}
1560
Stephan Muellerfced7b02014-11-12 05:29:00 +01001561/**
1562 * aead_request_set_assoc() - set the associated data scatter / gather list
1563 * @req: request handle
1564 * @assoc: associated data scatter / gather list
1565 * @assoclen: number of bytes to process from @assoc
1566 *
1567 * For encryption, the memory is filled with the associated data. For
1568 * decryption, the memory must point to the associated data.
1569 */
Herbert Xu1ae97822007-08-30 15:36:14 +08001570static inline void aead_request_set_assoc(struct aead_request *req,
1571 struct scatterlist *assoc,
1572 unsigned int assoclen)
1573{
1574 req->assoc = assoc;
1575 req->assoclen = assoclen;
1576}
1577
Stephan Mueller58284f02014-11-12 05:29:36 +01001578/**
1579 * DOC: Synchronous Block Cipher API
1580 *
1581 * The synchronous block cipher API is used with the ciphers of type
1582 * CRYPTO_ALG_TYPE_BLKCIPHER (listed as type "blkcipher" in /proc/crypto)
1583 *
1584 * Synchronous calls, have a context in the tfm. But since a single tfm can be
1585 * used in multiple calls and in parallel, this info should not be changeable
1586 * (unless a lock is used). This applies, for example, to the symmetric key.
1587 * However, the IV is changeable, so there is an iv field in blkcipher_tfm
1588 * structure for synchronous blkcipher api. So, its the only state info that can
1589 * be kept for synchronous calls without using a big lock across a tfm.
1590 *
1591 * The block cipher API allows the use of a complete cipher, i.e. a cipher
1592 * consisting of a template (a block chaining mode) and a single block cipher
1593 * primitive (e.g. AES).
1594 *
1595 * The plaintext data buffer and the ciphertext data buffer are pointed to
1596 * by using scatter/gather lists. The cipher operation is performed
1597 * on all segments of the provided scatter/gather lists.
1598 *
1599 * The kernel crypto API supports a cipher operation "in-place" which means that
1600 * the caller may provide the same scatter/gather list for the plaintext and
1601 * cipher text. After the completion of the cipher operation, the plaintext
1602 * data is replaced with the ciphertext data in case of an encryption and vice
1603 * versa for a decryption. The caller must ensure that the scatter/gather lists
1604 * for the output data point to sufficiently large buffers, i.e. multiples of
1605 * the block size of the cipher.
1606 */
1607
Herbert Xu5cde0af2006-08-22 00:07:53 +10001608static inline struct crypto_blkcipher *__crypto_blkcipher_cast(
1609 struct crypto_tfm *tfm)
1610{
1611 return (struct crypto_blkcipher *)tfm;
1612}
1613
1614static inline struct crypto_blkcipher *crypto_blkcipher_cast(
1615 struct crypto_tfm *tfm)
1616{
1617 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_BLKCIPHER);
1618 return __crypto_blkcipher_cast(tfm);
1619}
1620
Stephan Mueller58284f02014-11-12 05:29:36 +01001621/**
1622 * crypto_alloc_blkcipher() - allocate synchronous block cipher handle
1623 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1624 * blkcipher cipher
1625 * @type: specifies the type of the cipher
1626 * @mask: specifies the mask for the cipher
1627 *
1628 * Allocate a cipher handle for a block cipher. The returned struct
1629 * crypto_blkcipher is the cipher handle that is required for any subsequent
1630 * API invocation for that block cipher.
1631 *
1632 * Return: allocated cipher handle in case of success; IS_ERR() is true in case
1633 * of an error, PTR_ERR() returns the error code.
1634 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001635static inline struct crypto_blkcipher *crypto_alloc_blkcipher(
1636 const char *alg_name, u32 type, u32 mask)
1637{
Herbert Xu332f88402007-11-15 22:36:07 +08001638 type &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xu5cde0af2006-08-22 00:07:53 +10001639 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
Herbert Xu332f88402007-11-15 22:36:07 +08001640 mask |= CRYPTO_ALG_TYPE_MASK;
Herbert Xu5cde0af2006-08-22 00:07:53 +10001641
1642 return __crypto_blkcipher_cast(crypto_alloc_base(alg_name, type, mask));
1643}
1644
1645static inline struct crypto_tfm *crypto_blkcipher_tfm(
1646 struct crypto_blkcipher *tfm)
1647{
1648 return &tfm->base;
1649}
1650
Stephan Mueller58284f02014-11-12 05:29:36 +01001651/**
1652 * crypto_free_blkcipher() - zeroize and free the block cipher handle
1653 * @tfm: cipher handle to be freed
1654 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001655static inline void crypto_free_blkcipher(struct crypto_blkcipher *tfm)
1656{
1657 crypto_free_tfm(crypto_blkcipher_tfm(tfm));
1658}
1659
Stephan Mueller58284f02014-11-12 05:29:36 +01001660/**
1661 * crypto_has_blkcipher() - Search for the availability of a block cipher
1662 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1663 * block cipher
1664 * @type: specifies the type of the cipher
1665 * @mask: specifies the mask for the cipher
1666 *
1667 * Return: true when the block cipher is known to the kernel crypto API; false
1668 * otherwise
1669 */
Herbert Xufce32d72006-08-26 17:35:45 +10001670static inline int crypto_has_blkcipher(const char *alg_name, u32 type, u32 mask)
1671{
Herbert Xu332f88402007-11-15 22:36:07 +08001672 type &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xufce32d72006-08-26 17:35:45 +10001673 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
Herbert Xu332f88402007-11-15 22:36:07 +08001674 mask |= CRYPTO_ALG_TYPE_MASK;
Herbert Xufce32d72006-08-26 17:35:45 +10001675
1676 return crypto_has_alg(alg_name, type, mask);
1677}
1678
Stephan Mueller58284f02014-11-12 05:29:36 +01001679/**
1680 * crypto_blkcipher_name() - return the name / cra_name from the cipher handle
1681 * @tfm: cipher handle
1682 *
1683 * Return: The character string holding the name of the cipher
1684 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001685static inline const char *crypto_blkcipher_name(struct crypto_blkcipher *tfm)
1686{
1687 return crypto_tfm_alg_name(crypto_blkcipher_tfm(tfm));
1688}
1689
1690static inline struct blkcipher_tfm *crypto_blkcipher_crt(
1691 struct crypto_blkcipher *tfm)
1692{
1693 return &crypto_blkcipher_tfm(tfm)->crt_blkcipher;
1694}
1695
1696static inline struct blkcipher_alg *crypto_blkcipher_alg(
1697 struct crypto_blkcipher *tfm)
1698{
1699 return &crypto_blkcipher_tfm(tfm)->__crt_alg->cra_blkcipher;
1700}
1701
Stephan Mueller58284f02014-11-12 05:29:36 +01001702/**
1703 * crypto_blkcipher_ivsize() - obtain IV size
1704 * @tfm: cipher handle
1705 *
1706 * The size of the IV for the block cipher referenced by the cipher handle is
1707 * returned. This IV size may be zero if the cipher does not need an IV.
1708 *
1709 * Return: IV size in bytes
1710 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001711static inline unsigned int crypto_blkcipher_ivsize(struct crypto_blkcipher *tfm)
1712{
1713 return crypto_blkcipher_alg(tfm)->ivsize;
1714}
1715
Stephan Mueller58284f02014-11-12 05:29:36 +01001716/**
1717 * crypto_blkcipher_blocksize() - obtain block size of cipher
1718 * @tfm: cipher handle
1719 *
1720 * The block size for the block cipher referenced with the cipher handle is
1721 * returned. The caller may use that information to allocate appropriate
1722 * memory for the data returned by the encryption or decryption operation.
1723 *
1724 * Return: block size of cipher
1725 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001726static inline unsigned int crypto_blkcipher_blocksize(
1727 struct crypto_blkcipher *tfm)
1728{
1729 return crypto_tfm_alg_blocksize(crypto_blkcipher_tfm(tfm));
1730}
1731
1732static inline unsigned int crypto_blkcipher_alignmask(
1733 struct crypto_blkcipher *tfm)
1734{
1735 return crypto_tfm_alg_alignmask(crypto_blkcipher_tfm(tfm));
1736}
1737
1738static inline u32 crypto_blkcipher_get_flags(struct crypto_blkcipher *tfm)
1739{
1740 return crypto_tfm_get_flags(crypto_blkcipher_tfm(tfm));
1741}
1742
1743static inline void crypto_blkcipher_set_flags(struct crypto_blkcipher *tfm,
1744 u32 flags)
1745{
1746 crypto_tfm_set_flags(crypto_blkcipher_tfm(tfm), flags);
1747}
1748
1749static inline void crypto_blkcipher_clear_flags(struct crypto_blkcipher *tfm,
1750 u32 flags)
1751{
1752 crypto_tfm_clear_flags(crypto_blkcipher_tfm(tfm), flags);
1753}
1754
Stephan Mueller58284f02014-11-12 05:29:36 +01001755/**
1756 * crypto_blkcipher_setkey() - set key for cipher
1757 * @tfm: cipher handle
1758 * @key: buffer holding the key
1759 * @keylen: length of the key in bytes
1760 *
1761 * The caller provided key is set for the block cipher referenced by the cipher
1762 * handle.
1763 *
1764 * Note, the key length determines the cipher type. Many block ciphers implement
1765 * different cipher modes depending on the key size, such as AES-128 vs AES-192
1766 * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
1767 * is performed.
1768 *
1769 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
1770 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001771static inline int crypto_blkcipher_setkey(struct crypto_blkcipher *tfm,
1772 const u8 *key, unsigned int keylen)
1773{
1774 return crypto_blkcipher_crt(tfm)->setkey(crypto_blkcipher_tfm(tfm),
1775 key, keylen);
1776}
1777
Stephan Mueller58284f02014-11-12 05:29:36 +01001778/**
1779 * crypto_blkcipher_encrypt() - encrypt plaintext
1780 * @desc: reference to the block cipher handle with meta data
1781 * @dst: scatter/gather list that is filled by the cipher operation with the
1782 * ciphertext
1783 * @src: scatter/gather list that holds the plaintext
1784 * @nbytes: number of bytes of the plaintext to encrypt.
1785 *
1786 * Encrypt plaintext data using the IV set by the caller with a preceding
1787 * call of crypto_blkcipher_set_iv.
1788 *
1789 * The blkcipher_desc data structure must be filled by the caller and can
1790 * reside on the stack. The caller must fill desc as follows: desc.tfm is filled
1791 * with the block cipher handle; desc.flags is filled with either
1792 * CRYPTO_TFM_REQ_MAY_SLEEP or 0.
1793 *
1794 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1795 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001796static inline int crypto_blkcipher_encrypt(struct blkcipher_desc *desc,
1797 struct scatterlist *dst,
1798 struct scatterlist *src,
1799 unsigned int nbytes)
1800{
1801 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
1802 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
1803}
1804
Stephan Mueller58284f02014-11-12 05:29:36 +01001805/**
1806 * crypto_blkcipher_encrypt_iv() - encrypt plaintext with dedicated IV
1807 * @desc: reference to the block cipher handle with meta data
1808 * @dst: scatter/gather list that is filled by the cipher operation with the
1809 * ciphertext
1810 * @src: scatter/gather list that holds the plaintext
1811 * @nbytes: number of bytes of the plaintext to encrypt.
1812 *
1813 * Encrypt plaintext data with the use of an IV that is solely used for this
1814 * cipher operation. Any previously set IV is not used.
1815 *
1816 * The blkcipher_desc data structure must be filled by the caller and can
1817 * reside on the stack. The caller must fill desc as follows: desc.tfm is filled
1818 * with the block cipher handle; desc.info is filled with the IV to be used for
1819 * the current operation; desc.flags is filled with either
1820 * CRYPTO_TFM_REQ_MAY_SLEEP or 0.
1821 *
1822 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1823 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001824static inline int crypto_blkcipher_encrypt_iv(struct blkcipher_desc *desc,
1825 struct scatterlist *dst,
1826 struct scatterlist *src,
1827 unsigned int nbytes)
1828{
1829 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
1830}
1831
Stephan Mueller58284f02014-11-12 05:29:36 +01001832/**
1833 * crypto_blkcipher_decrypt() - decrypt ciphertext
1834 * @desc: reference to the block cipher handle with meta data
1835 * @dst: scatter/gather list that is filled by the cipher operation with the
1836 * plaintext
1837 * @src: scatter/gather list that holds the ciphertext
1838 * @nbytes: number of bytes of the ciphertext to decrypt.
1839 *
1840 * Decrypt ciphertext data using the IV set by the caller with a preceding
1841 * call of crypto_blkcipher_set_iv.
1842 *
1843 * The blkcipher_desc data structure must be filled by the caller as documented
1844 * for the crypto_blkcipher_encrypt call above.
1845 *
1846 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1847 *
1848 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001849static inline int crypto_blkcipher_decrypt(struct blkcipher_desc *desc,
1850 struct scatterlist *dst,
1851 struct scatterlist *src,
1852 unsigned int nbytes)
1853{
1854 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
1855 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
1856}
1857
Stephan Mueller58284f02014-11-12 05:29:36 +01001858/**
1859 * crypto_blkcipher_decrypt_iv() - decrypt ciphertext with dedicated IV
1860 * @desc: reference to the block cipher handle with meta data
1861 * @dst: scatter/gather list that is filled by the cipher operation with the
1862 * plaintext
1863 * @src: scatter/gather list that holds the ciphertext
1864 * @nbytes: number of bytes of the ciphertext to decrypt.
1865 *
1866 * Decrypt ciphertext data with the use of an IV that is solely used for this
1867 * cipher operation. Any previously set IV is not used.
1868 *
1869 * The blkcipher_desc data structure must be filled by the caller as documented
1870 * for the crypto_blkcipher_encrypt_iv call above.
1871 *
1872 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1873 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001874static inline int crypto_blkcipher_decrypt_iv(struct blkcipher_desc *desc,
1875 struct scatterlist *dst,
1876 struct scatterlist *src,
1877 unsigned int nbytes)
1878{
1879 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
1880}
1881
Stephan Mueller58284f02014-11-12 05:29:36 +01001882/**
1883 * crypto_blkcipher_set_iv() - set IV for cipher
1884 * @tfm: cipher handle
1885 * @src: buffer holding the IV
1886 * @len: length of the IV in bytes
1887 *
1888 * The caller provided IV is set for the block cipher referenced by the cipher
1889 * handle.
1890 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001891static inline void crypto_blkcipher_set_iv(struct crypto_blkcipher *tfm,
1892 const u8 *src, unsigned int len)
1893{
1894 memcpy(crypto_blkcipher_crt(tfm)->iv, src, len);
1895}
1896
Stephan Mueller58284f02014-11-12 05:29:36 +01001897/**
1898 * crypto_blkcipher_get_iv() - obtain IV from cipher
1899 * @tfm: cipher handle
1900 * @dst: buffer filled with the IV
1901 * @len: length of the buffer dst
1902 *
1903 * The caller can obtain the IV set for the block cipher referenced by the
1904 * cipher handle and store it into the user-provided buffer. If the buffer
1905 * has an insufficient space, the IV is truncated to fit the buffer.
1906 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001907static inline void crypto_blkcipher_get_iv(struct crypto_blkcipher *tfm,
1908 u8 *dst, unsigned int len)
1909{
1910 memcpy(dst, crypto_blkcipher_crt(tfm)->iv, len);
1911}
1912
Stephan Mueller16e61032014-11-12 05:30:06 +01001913/**
1914 * DOC: Single Block Cipher API
1915 *
1916 * The single block cipher API is used with the ciphers of type
1917 * CRYPTO_ALG_TYPE_CIPHER (listed as type "cipher" in /proc/crypto).
1918 *
1919 * Using the single block cipher API calls, operations with the basic cipher
1920 * primitive can be implemented. These cipher primitives exclude any block
1921 * chaining operations including IV handling.
1922 *
1923 * The purpose of this single block cipher API is to support the implementation
1924 * of templates or other concepts that only need to perform the cipher operation
1925 * on one block at a time. Templates invoke the underlying cipher primitive
1926 * block-wise and process either the input or the output data of these cipher
1927 * operations.
1928 */
1929
Herbert Xuf28776a2006-08-13 20:58:18 +10001930static inline struct crypto_cipher *__crypto_cipher_cast(struct crypto_tfm *tfm)
1931{
1932 return (struct crypto_cipher *)tfm;
1933}
1934
1935static inline struct crypto_cipher *crypto_cipher_cast(struct crypto_tfm *tfm)
1936{
1937 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
1938 return __crypto_cipher_cast(tfm);
1939}
1940
Stephan Mueller16e61032014-11-12 05:30:06 +01001941/**
1942 * crypto_alloc_cipher() - allocate single block cipher handle
1943 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1944 * single block cipher
1945 * @type: specifies the type of the cipher
1946 * @mask: specifies the mask for the cipher
1947 *
1948 * Allocate a cipher handle for a single block cipher. The returned struct
1949 * crypto_cipher is the cipher handle that is required for any subsequent API
1950 * invocation for that single block cipher.
1951 *
1952 * Return: allocated cipher handle in case of success; IS_ERR() is true in case
1953 * of an error, PTR_ERR() returns the error code.
1954 */
Herbert Xuf28776a2006-08-13 20:58:18 +10001955static inline struct crypto_cipher *crypto_alloc_cipher(const char *alg_name,
1956 u32 type, u32 mask)
1957{
1958 type &= ~CRYPTO_ALG_TYPE_MASK;
1959 type |= CRYPTO_ALG_TYPE_CIPHER;
1960 mask |= CRYPTO_ALG_TYPE_MASK;
1961
1962 return __crypto_cipher_cast(crypto_alloc_base(alg_name, type, mask));
1963}
1964
1965static inline struct crypto_tfm *crypto_cipher_tfm(struct crypto_cipher *tfm)
1966{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001967 return &tfm->base;
Herbert Xuf28776a2006-08-13 20:58:18 +10001968}
1969
Stephan Mueller16e61032014-11-12 05:30:06 +01001970/**
1971 * crypto_free_cipher() - zeroize and free the single block cipher handle
1972 * @tfm: cipher handle to be freed
1973 */
Herbert Xuf28776a2006-08-13 20:58:18 +10001974static inline void crypto_free_cipher(struct crypto_cipher *tfm)
1975{
1976 crypto_free_tfm(crypto_cipher_tfm(tfm));
1977}
1978
Stephan Mueller16e61032014-11-12 05:30:06 +01001979/**
1980 * crypto_has_cipher() - Search for the availability of a single block cipher
1981 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1982 * single block cipher
1983 * @type: specifies the type of the cipher
1984 * @mask: specifies the mask for the cipher
1985 *
1986 * Return: true when the single block cipher is known to the kernel crypto API;
1987 * false otherwise
1988 */
Herbert Xufce32d72006-08-26 17:35:45 +10001989static inline int crypto_has_cipher(const char *alg_name, u32 type, u32 mask)
1990{
1991 type &= ~CRYPTO_ALG_TYPE_MASK;
1992 type |= CRYPTO_ALG_TYPE_CIPHER;
1993 mask |= CRYPTO_ALG_TYPE_MASK;
1994
1995 return crypto_has_alg(alg_name, type, mask);
1996}
1997
Herbert Xuf28776a2006-08-13 20:58:18 +10001998static inline struct cipher_tfm *crypto_cipher_crt(struct crypto_cipher *tfm)
1999{
2000 return &crypto_cipher_tfm(tfm)->crt_cipher;
2001}
2002
Stephan Mueller16e61032014-11-12 05:30:06 +01002003/**
2004 * crypto_cipher_blocksize() - obtain block size for cipher
2005 * @tfm: cipher handle
2006 *
2007 * The block size for the single block cipher referenced with the cipher handle
2008 * tfm is returned. The caller may use that information to allocate appropriate
2009 * memory for the data returned by the encryption or decryption operation
2010 *
2011 * Return: block size of cipher
2012 */
Herbert Xuf28776a2006-08-13 20:58:18 +10002013static inline unsigned int crypto_cipher_blocksize(struct crypto_cipher *tfm)
2014{
2015 return crypto_tfm_alg_blocksize(crypto_cipher_tfm(tfm));
2016}
2017
2018static inline unsigned int crypto_cipher_alignmask(struct crypto_cipher *tfm)
2019{
2020 return crypto_tfm_alg_alignmask(crypto_cipher_tfm(tfm));
2021}
2022
2023static inline u32 crypto_cipher_get_flags(struct crypto_cipher *tfm)
2024{
2025 return crypto_tfm_get_flags(crypto_cipher_tfm(tfm));
2026}
2027
2028static inline void crypto_cipher_set_flags(struct crypto_cipher *tfm,
2029 u32 flags)
2030{
2031 crypto_tfm_set_flags(crypto_cipher_tfm(tfm), flags);
2032}
2033
2034static inline void crypto_cipher_clear_flags(struct crypto_cipher *tfm,
2035 u32 flags)
2036{
2037 crypto_tfm_clear_flags(crypto_cipher_tfm(tfm), flags);
2038}
2039
Stephan Mueller16e61032014-11-12 05:30:06 +01002040/**
2041 * crypto_cipher_setkey() - set key for cipher
2042 * @tfm: cipher handle
2043 * @key: buffer holding the key
2044 * @keylen: length of the key in bytes
2045 *
2046 * The caller provided key is set for the single block cipher referenced by the
2047 * cipher handle.
2048 *
2049 * Note, the key length determines the cipher type. Many block ciphers implement
2050 * different cipher modes depending on the key size, such as AES-128 vs AES-192
2051 * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
2052 * is performed.
2053 *
2054 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
2055 */
Herbert Xu7226bc872006-08-21 21:40:49 +10002056static inline int crypto_cipher_setkey(struct crypto_cipher *tfm,
2057 const u8 *key, unsigned int keylen)
2058{
2059 return crypto_cipher_crt(tfm)->cit_setkey(crypto_cipher_tfm(tfm),
2060 key, keylen);
2061}
2062
Stephan Mueller16e61032014-11-12 05:30:06 +01002063/**
2064 * crypto_cipher_encrypt_one() - encrypt one block of plaintext
2065 * @tfm: cipher handle
2066 * @dst: points to the buffer that will be filled with the ciphertext
2067 * @src: buffer holding the plaintext to be encrypted
2068 *
2069 * Invoke the encryption operation of one block. The caller must ensure that
2070 * the plaintext and ciphertext buffers are at least one block in size.
2071 */
Herbert Xuf28776a2006-08-13 20:58:18 +10002072static inline void crypto_cipher_encrypt_one(struct crypto_cipher *tfm,
2073 u8 *dst, const u8 *src)
2074{
2075 crypto_cipher_crt(tfm)->cit_encrypt_one(crypto_cipher_tfm(tfm),
2076 dst, src);
2077}
2078
Stephan Mueller16e61032014-11-12 05:30:06 +01002079/**
2080 * crypto_cipher_decrypt_one() - decrypt one block of ciphertext
2081 * @tfm: cipher handle
2082 * @dst: points to the buffer that will be filled with the plaintext
2083 * @src: buffer holding the ciphertext to be decrypted
2084 *
2085 * Invoke the decryption operation of one block. The caller must ensure that
2086 * the plaintext and ciphertext buffers are at least one block in size.
2087 */
Herbert Xuf28776a2006-08-13 20:58:18 +10002088static inline void crypto_cipher_decrypt_one(struct crypto_cipher *tfm,
2089 u8 *dst, const u8 *src)
2090{
2091 crypto_cipher_crt(tfm)->cit_decrypt_one(crypto_cipher_tfm(tfm),
2092 dst, src);
2093}
2094
Stephan Mueller47ca5be2014-11-12 05:30:42 +01002095/**
2096 * DOC: Synchronous Message Digest API
2097 *
2098 * The synchronous message digest API is used with the ciphers of type
2099 * CRYPTO_ALG_TYPE_HASH (listed as type "hash" in /proc/crypto)
2100 */
2101
Herbert Xu055bcee2006-08-19 22:24:23 +10002102static inline struct crypto_hash *__crypto_hash_cast(struct crypto_tfm *tfm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103{
Herbert Xu055bcee2006-08-19 22:24:23 +10002104 return (struct crypto_hash *)tfm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105}
2106
Herbert Xu055bcee2006-08-19 22:24:23 +10002107static inline struct crypto_hash *crypto_hash_cast(struct crypto_tfm *tfm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108{
Herbert Xu055bcee2006-08-19 22:24:23 +10002109 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_HASH) &
2110 CRYPTO_ALG_TYPE_HASH_MASK);
2111 return __crypto_hash_cast(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112}
2113
Stephan Mueller47ca5be2014-11-12 05:30:42 +01002114/**
2115 * crypto_alloc_hash() - allocate synchronous message digest handle
2116 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
2117 * message digest cipher
2118 * @type: specifies the type of the cipher
2119 * @mask: specifies the mask for the cipher
2120 *
2121 * Allocate a cipher handle for a message digest. The returned struct
2122 * crypto_hash is the cipher handle that is required for any subsequent
2123 * API invocation for that message digest.
2124 *
2125 * Return: allocated cipher handle in case of success; IS_ERR() is true in case
2126 * of an error, PTR_ERR() returns the error code.
2127 */
Herbert Xu055bcee2006-08-19 22:24:23 +10002128static inline struct crypto_hash *crypto_alloc_hash(const char *alg_name,
2129 u32 type, u32 mask)
2130{
2131 type &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xu551a09a2007-12-01 21:47:07 +11002132 mask &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xu055bcee2006-08-19 22:24:23 +10002133 type |= CRYPTO_ALG_TYPE_HASH;
2134 mask |= CRYPTO_ALG_TYPE_HASH_MASK;
2135
2136 return __crypto_hash_cast(crypto_alloc_base(alg_name, type, mask));
2137}
2138
2139static inline struct crypto_tfm *crypto_hash_tfm(struct crypto_hash *tfm)
2140{
2141 return &tfm->base;
2142}
2143
Stephan Mueller47ca5be2014-11-12 05:30:42 +01002144/**
2145 * crypto_free_hash() - zeroize and free message digest handle
2146 * @tfm: cipher handle to be freed
2147 */
Herbert Xu055bcee2006-08-19 22:24:23 +10002148static inline void crypto_free_hash(struct crypto_hash *tfm)
2149{
2150 crypto_free_tfm(crypto_hash_tfm(tfm));
2151}
2152
Stephan Mueller47ca5be2014-11-12 05:30:42 +01002153/**
2154 * crypto_has_hash() - Search for the availability of a message digest
2155 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
2156 * message digest cipher
2157 * @type: specifies the type of the cipher
2158 * @mask: specifies the mask for the cipher
2159 *
2160 * Return: true when the message digest cipher is known to the kernel crypto
2161 * API; false otherwise
2162 */
Herbert Xufce32d72006-08-26 17:35:45 +10002163static inline int crypto_has_hash(const char *alg_name, u32 type, u32 mask)
2164{
2165 type &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xu551a09a2007-12-01 21:47:07 +11002166 mask &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xufce32d72006-08-26 17:35:45 +10002167 type |= CRYPTO_ALG_TYPE_HASH;
2168 mask |= CRYPTO_ALG_TYPE_HASH_MASK;
2169
2170 return crypto_has_alg(alg_name, type, mask);
2171}
2172
Herbert Xu055bcee2006-08-19 22:24:23 +10002173static inline struct hash_tfm *crypto_hash_crt(struct crypto_hash *tfm)
2174{
2175 return &crypto_hash_tfm(tfm)->crt_hash;
2176}
2177
Stephan Mueller47ca5be2014-11-12 05:30:42 +01002178/**
2179 * crypto_hash_blocksize() - obtain block size for message digest
2180 * @tfm: cipher handle
2181 *
2182 * The block size for the message digest cipher referenced with the cipher
2183 * handle is returned.
2184 *
2185 * Return: block size of cipher
2186 */
Herbert Xu055bcee2006-08-19 22:24:23 +10002187static inline unsigned int crypto_hash_blocksize(struct crypto_hash *tfm)
2188{
2189 return crypto_tfm_alg_blocksize(crypto_hash_tfm(tfm));
2190}
2191
2192static inline unsigned int crypto_hash_alignmask(struct crypto_hash *tfm)
2193{
2194 return crypto_tfm_alg_alignmask(crypto_hash_tfm(tfm));
2195}
2196
Stephan Mueller47ca5be2014-11-12 05:30:42 +01002197/**
2198 * crypto_hash_digestsize() - obtain message digest size
2199 * @tfm: cipher handle
2200 *
2201 * The size for the message digest created by the message digest cipher
2202 * referenced with the cipher handle is returned.
2203 *
2204 * Return: message digest size
2205 */
Herbert Xu055bcee2006-08-19 22:24:23 +10002206static inline unsigned int crypto_hash_digestsize(struct crypto_hash *tfm)
2207{
2208 return crypto_hash_crt(tfm)->digestsize;
2209}
2210
2211static inline u32 crypto_hash_get_flags(struct crypto_hash *tfm)
2212{
2213 return crypto_tfm_get_flags(crypto_hash_tfm(tfm));
2214}
2215
2216static inline void crypto_hash_set_flags(struct crypto_hash *tfm, u32 flags)
2217{
2218 crypto_tfm_set_flags(crypto_hash_tfm(tfm), flags);
2219}
2220
2221static inline void crypto_hash_clear_flags(struct crypto_hash *tfm, u32 flags)
2222{
2223 crypto_tfm_clear_flags(crypto_hash_tfm(tfm), flags);
2224}
2225
Stephan Mueller47ca5be2014-11-12 05:30:42 +01002226/**
2227 * crypto_hash_init() - (re)initialize message digest handle
2228 * @desc: cipher request handle that to be filled by caller --
2229 * desc.tfm is filled with the hash cipher handle;
2230 * desc.flags is filled with either CRYPTO_TFM_REQ_MAY_SLEEP or 0.
2231 *
2232 * The call (re-)initializes the message digest referenced by the hash cipher
2233 * request handle. Any potentially existing state created by previous
2234 * operations is discarded.
2235 *
2236 * Return: 0 if the message digest initialization was successful; < 0 if an
2237 * error occurred
2238 */
Herbert Xu055bcee2006-08-19 22:24:23 +10002239static inline int crypto_hash_init(struct hash_desc *desc)
2240{
2241 return crypto_hash_crt(desc->tfm)->init(desc);
2242}
2243
Stephan Mueller47ca5be2014-11-12 05:30:42 +01002244/**
2245 * crypto_hash_update() - add data to message digest for processing
2246 * @desc: cipher request handle
2247 * @sg: scatter / gather list pointing to the data to be added to the message
2248 * digest
2249 * @nbytes: number of bytes to be processed from @sg
2250 *
2251 * Updates the message digest state of the cipher handle pointed to by the
2252 * hash cipher request handle with the input data pointed to by the
2253 * scatter/gather list.
2254 *
2255 * Return: 0 if the message digest update was successful; < 0 if an error
2256 * occurred
2257 */
Herbert Xu055bcee2006-08-19 22:24:23 +10002258static inline int crypto_hash_update(struct hash_desc *desc,
2259 struct scatterlist *sg,
2260 unsigned int nbytes)
2261{
2262 return crypto_hash_crt(desc->tfm)->update(desc, sg, nbytes);
2263}
2264
Stephan Mueller47ca5be2014-11-12 05:30:42 +01002265/**
2266 * crypto_hash_final() - calculate message digest
2267 * @desc: cipher request handle
2268 * @out: message digest output buffer -- The caller must ensure that the out
2269 * buffer has a sufficient size (e.g. by using the crypto_hash_digestsize
2270 * function).
2271 *
2272 * Finalize the message digest operation and create the message digest
2273 * based on all data added to the cipher handle. The message digest is placed
2274 * into the output buffer.
2275 *
2276 * Return: 0 if the message digest creation was successful; < 0 if an error
2277 * occurred
2278 */
Herbert Xu055bcee2006-08-19 22:24:23 +10002279static inline int crypto_hash_final(struct hash_desc *desc, u8 *out)
2280{
2281 return crypto_hash_crt(desc->tfm)->final(desc, out);
2282}
2283
Stephan Mueller47ca5be2014-11-12 05:30:42 +01002284/**
2285 * crypto_hash_digest() - calculate message digest for a buffer
2286 * @desc: see crypto_hash_final()
2287 * @sg: see crypto_hash_update()
2288 * @nbytes: see crypto_hash_update()
2289 * @out: see crypto_hash_final()
2290 *
2291 * This function is a "short-hand" for the function calls of crypto_hash_init,
2292 * crypto_hash_update and crypto_hash_final. The parameters have the same
2293 * meaning as discussed for those separate three functions.
2294 *
2295 * Return: 0 if the message digest creation was successful; < 0 if an error
2296 * occurred
2297 */
Herbert Xu055bcee2006-08-19 22:24:23 +10002298static inline int crypto_hash_digest(struct hash_desc *desc,
2299 struct scatterlist *sg,
2300 unsigned int nbytes, u8 *out)
2301{
2302 return crypto_hash_crt(desc->tfm)->digest(desc, sg, nbytes, out);
2303}
2304
Stephan Mueller47ca5be2014-11-12 05:30:42 +01002305/**
2306 * crypto_hash_setkey() - set key for message digest
2307 * @hash: cipher handle
2308 * @key: buffer holding the key
2309 * @keylen: length of the key in bytes
2310 *
2311 * The caller provided key is set for the message digest cipher. The cipher
2312 * handle must point to a keyed hash in order for this function to succeed.
2313 *
2314 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
2315 */
Herbert Xu055bcee2006-08-19 22:24:23 +10002316static inline int crypto_hash_setkey(struct crypto_hash *hash,
2317 const u8 *key, unsigned int keylen)
2318{
2319 return crypto_hash_crt(hash)->setkey(hash, key, keylen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320}
2321
Herbert Xufce32d72006-08-26 17:35:45 +10002322static inline struct crypto_comp *__crypto_comp_cast(struct crypto_tfm *tfm)
2323{
2324 return (struct crypto_comp *)tfm;
2325}
2326
2327static inline struct crypto_comp *crypto_comp_cast(struct crypto_tfm *tfm)
2328{
2329 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_COMPRESS) &
2330 CRYPTO_ALG_TYPE_MASK);
2331 return __crypto_comp_cast(tfm);
2332}
2333
2334static inline struct crypto_comp *crypto_alloc_comp(const char *alg_name,
2335 u32 type, u32 mask)
2336{
2337 type &= ~CRYPTO_ALG_TYPE_MASK;
2338 type |= CRYPTO_ALG_TYPE_COMPRESS;
2339 mask |= CRYPTO_ALG_TYPE_MASK;
2340
2341 return __crypto_comp_cast(crypto_alloc_base(alg_name, type, mask));
2342}
2343
2344static inline struct crypto_tfm *crypto_comp_tfm(struct crypto_comp *tfm)
2345{
Herbert Xu78a1fe42006-12-24 10:02:00 +11002346 return &tfm->base;
Herbert Xufce32d72006-08-26 17:35:45 +10002347}
2348
2349static inline void crypto_free_comp(struct crypto_comp *tfm)
2350{
2351 crypto_free_tfm(crypto_comp_tfm(tfm));
2352}
2353
2354static inline int crypto_has_comp(const char *alg_name, u32 type, u32 mask)
2355{
2356 type &= ~CRYPTO_ALG_TYPE_MASK;
2357 type |= CRYPTO_ALG_TYPE_COMPRESS;
2358 mask |= CRYPTO_ALG_TYPE_MASK;
2359
2360 return crypto_has_alg(alg_name, type, mask);
2361}
2362
Herbert Xue4d5b792006-08-26 18:12:40 +10002363static inline const char *crypto_comp_name(struct crypto_comp *tfm)
2364{
2365 return crypto_tfm_alg_name(crypto_comp_tfm(tfm));
2366}
2367
Herbert Xufce32d72006-08-26 17:35:45 +10002368static inline struct compress_tfm *crypto_comp_crt(struct crypto_comp *tfm)
2369{
2370 return &crypto_comp_tfm(tfm)->crt_compress;
2371}
2372
2373static inline int crypto_comp_compress(struct crypto_comp *tfm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 const u8 *src, unsigned int slen,
2375 u8 *dst, unsigned int *dlen)
2376{
Herbert Xu78a1fe42006-12-24 10:02:00 +11002377 return crypto_comp_crt(tfm)->cot_compress(crypto_comp_tfm(tfm),
2378 src, slen, dst, dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379}
2380
Herbert Xufce32d72006-08-26 17:35:45 +10002381static inline int crypto_comp_decompress(struct crypto_comp *tfm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 const u8 *src, unsigned int slen,
2383 u8 *dst, unsigned int *dlen)
2384{
Herbert Xu78a1fe42006-12-24 10:02:00 +11002385 return crypto_comp_crt(tfm)->cot_decompress(crypto_comp_tfm(tfm),
2386 src, slen, dst, dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387}
2388
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389#endif /* _LINUX_CRYPTO_H */
2390