blob: c3c98a62e503bf625674739b89745ad10c8c675b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Scatterlist Cryptographic API.
3 *
4 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
5 * Copyright (c) 2002 David S. Miller (davem@redhat.com)
Herbert Xu5cb1454b2005-11-05 16:58:14 +11006 * Copyright (c) 2005 Herbert Xu <herbert@gondor.apana.org.au>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * Portions derived from Cryptoapi, by Alexander Kjeldaas <astor@fast.no>
John Anthony Kazos Jr18735dd2007-10-19 23:07:36 +02009 * and Nettle, by Niels Möller.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the Free
13 * Software Foundation; either version 2 of the License, or (at your option)
14 * any later version.
15 *
16 */
17#ifndef _LINUX_CRYPTO_H
18#define _LINUX_CRYPTO_H
19
Arun Sharma600634972011-07-26 16:09:06 -070020#include <linux/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/list.h>
Paul Gortmaker187f1882011-11-23 20:12:59 -050023#include <linux/bug.h>
Herbert Xu79911102006-08-21 21:03:52 +100024#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/string.h>
Herbert Xu79911102006-08-21 21:03:52 +100026#include <linux/uaccess.h>
Gilad Ben-Yossefada69a12017-10-18 08:00:38 +010027#include <linux/completion.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29/*
Kees Cook5d26a102014-11-20 17:05:53 -080030 * Autoloaded crypto modules should only use a prefixed name to avoid allowing
31 * arbitrary modules to be loaded. Loading from userspace may still need the
32 * unprefixed names, so retains those aliases as well.
33 * This uses __MODULE_INFO directly instead of MODULE_ALIAS because pre-4.3
34 * gcc (e.g. avr32 toolchain) uses __LINE__ for uniqueness, and this macro
35 * expands twice on the same line. Instead, use a separate base name for the
36 * alias.
37 */
38#define MODULE_ALIAS_CRYPTO(name) \
39 __MODULE_INFO(alias, alias_userspace, name); \
40 __MODULE_INFO(alias, alias_crypto, "crypto-" name)
41
42/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 * Algorithm masks and types.
44 */
Herbert Xu28259822006-08-06 21:23:26 +100045#define CRYPTO_ALG_TYPE_MASK 0x0000000f
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#define CRYPTO_ALG_TYPE_CIPHER 0x00000001
Loc Ho004a4032008-05-14 20:41:47 +080047#define CRYPTO_ALG_TYPE_COMPRESS 0x00000002
48#define CRYPTO_ALG_TYPE_AEAD 0x00000003
Herbert Xu055bcee2006-08-19 22:24:23 +100049#define CRYPTO_ALG_TYPE_BLKCIPHER 0x00000004
Herbert Xu332f88402007-11-15 22:36:07 +080050#define CRYPTO_ALG_TYPE_ABLKCIPHER 0x00000005
Herbert Xu4e6c3df2016-07-12 13:17:31 +080051#define CRYPTO_ALG_TYPE_SKCIPHER 0x00000005
Salvatore Benedetto4e5f2c42016-06-22 17:49:13 +010052#define CRYPTO_ALG_TYPE_KPP 0x00000008
Giovanni Cabiddu2ebda742016-10-21 13:19:47 +010053#define CRYPTO_ALG_TYPE_ACOMPRESS 0x0000000a
Giovanni Cabiddu1ab53a72016-10-21 13:19:48 +010054#define CRYPTO_ALG_TYPE_SCOMPRESS 0x0000000b
Neil Horman17f0f4a2008-08-14 22:15:52 +100055#define CRYPTO_ALG_TYPE_RNG 0x0000000c
Tadeusz Struk3c339ab2015-06-16 10:30:55 -070056#define CRYPTO_ALG_TYPE_AKCIPHER 0x0000000d
Giovanni Cabiddu63044c42016-06-02 13:28:55 +010057#define CRYPTO_ALG_TYPE_DIGEST 0x0000000e
58#define CRYPTO_ALG_TYPE_HASH 0x0000000e
59#define CRYPTO_ALG_TYPE_SHASH 0x0000000e
60#define CRYPTO_ALG_TYPE_AHASH 0x0000000f
Herbert Xu055bcee2006-08-19 22:24:23 +100061
62#define CRYPTO_ALG_TYPE_HASH_MASK 0x0000000e
Giovanni Cabiddu63044c42016-06-02 13:28:55 +010063#define CRYPTO_ALG_TYPE_AHASH_MASK 0x0000000e
Herbert Xu332f88402007-11-15 22:36:07 +080064#define CRYPTO_ALG_TYPE_BLKCIPHER_MASK 0x0000000c
Giovanni Cabiddu1ab53a72016-10-21 13:19:48 +010065#define CRYPTO_ALG_TYPE_ACOMPRESS_MASK 0x0000000e
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Herbert Xu28259822006-08-06 21:23:26 +100067#define CRYPTO_ALG_LARVAL 0x00000010
Herbert Xu6bfd4802006-09-21 11:39:29 +100068#define CRYPTO_ALG_DEAD 0x00000020
69#define CRYPTO_ALG_DYING 0x00000040
Herbert Xuf3f632d2006-08-06 23:12:59 +100070#define CRYPTO_ALG_ASYNC 0x00000080
Herbert Xu28259822006-08-06 21:23:26 +100071
Linus Torvalds1da177e2005-04-16 15:20:36 -070072/*
Herbert Xu60104392006-08-26 18:34:10 +100073 * Set this bit if and only if the algorithm requires another algorithm of
74 * the same type to handle corner cases.
75 */
76#define CRYPTO_ALG_NEED_FALLBACK 0x00000100
77
78/*
Herbert Xu73d38642008-08-03 21:15:23 +080079 * Set if the algorithm has passed automated run-time testing. Note that
80 * if there is no run-time testing for a given algorithm it is considered
81 * to have passed.
82 */
83
84#define CRYPTO_ALG_TESTED 0x00000400
85
86/*
Baruch Siach864e0982016-11-30 15:16:08 +020087 * Set if the algorithm is an instance that is built from templates.
Steffen Klassert64a947b2011-09-27 07:21:26 +020088 */
89#define CRYPTO_ALG_INSTANCE 0x00000800
90
Nikos Mavrogiannopoulosd912bb72011-11-01 13:39:56 +010091/* Set this bit if the algorithm provided is hardware accelerated but
92 * not available to userspace via instruction set or so.
93 */
94#define CRYPTO_ALG_KERN_DRIVER_ONLY 0x00001000
95
Steffen Klassert64a947b2011-09-27 07:21:26 +020096/*
Stephan Mueller06ca7f62015-03-30 21:55:52 +020097 * Mark a cipher as a service implementation only usable by another
98 * cipher and never by a normal user of the kernel crypto API
99 */
100#define CRYPTO_ALG_INTERNAL 0x00002000
101
102/*
Eric Biggersa208fa82018-01-03 11:16:26 -0800103 * Set if the algorithm has a ->setkey() method but can be used without
104 * calling it first, i.e. there is a default key.
105 */
106#define CRYPTO_ALG_OPTIONAL_KEY 0x00004000
107
108/*
Matthew Garrette2861fa2018-06-08 14:57:42 -0700109 * Don't trigger module loading
110 */
111#define CRYPTO_NOLOAD 0x00008000
112
113/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 * Transform masks and values (for crt_flags).
115 */
Eric Biggers9fa68f62018-01-03 11:16:27 -0800116#define CRYPTO_TFM_NEED_KEY 0x00000001
117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118#define CRYPTO_TFM_REQ_MASK 0x000fff00
119#define CRYPTO_TFM_RES_MASK 0xfff00000
120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121#define CRYPTO_TFM_REQ_WEAK_KEY 0x00000100
Herbert Xu64baf3c2005-09-01 17:43:05 -0700122#define CRYPTO_TFM_REQ_MAY_SLEEP 0x00000200
Herbert Xu32e3983f2007-03-24 14:35:34 +1100123#define CRYPTO_TFM_REQ_MAY_BACKLOG 0x00000400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124#define CRYPTO_TFM_RES_WEAK_KEY 0x00100000
125#define CRYPTO_TFM_RES_BAD_KEY_LEN 0x00200000
126#define CRYPTO_TFM_RES_BAD_KEY_SCHED 0x00400000
127#define CRYPTO_TFM_RES_BAD_BLOCK_LEN 0x00800000
128#define CRYPTO_TFM_RES_BAD_FLAGS 0x01000000
129
130/*
131 * Miscellaneous stuff.
132 */
Herbert Xuf437a3f2017-04-06 16:16:11 +0800133#define CRYPTO_MAX_ALG_NAME 128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Herbert Xu79911102006-08-21 21:03:52 +1000135/*
136 * The macro CRYPTO_MINALIGN_ATTR (along with the void * type in the actual
137 * declaration) is used to ensure that the crypto_tfm context structure is
138 * aligned correctly for the given architecture so that there are no alignment
139 * faults for C data types. In particular, this is required on platforms such
140 * as arm where pointers are 32-bit aligned but there are data types such as
141 * u64 which require 64-bit alignment.
142 */
Herbert Xu79911102006-08-21 21:03:52 +1000143#define CRYPTO_MINALIGN ARCH_KMALLOC_MINALIGN
Herbert Xu79911102006-08-21 21:03:52 +1000144
Herbert Xu79911102006-08-21 21:03:52 +1000145#define CRYPTO_MINALIGN_ATTR __attribute__ ((__aligned__(CRYPTO_MINALIGN)))
Herbert Xu79911102006-08-21 21:03:52 +1000146
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147struct scatterlist;
Herbert Xu32e3983f2007-03-24 14:35:34 +1100148struct crypto_ablkcipher;
149struct crypto_async_request;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000150struct crypto_blkcipher;
Herbert Xu40725182005-07-06 13:51:52 -0700151struct crypto_tfm;
Herbert Xue853c3c2006-08-22 00:06:54 +1000152struct crypto_type;
Herbert Xu40725182005-07-06 13:51:52 -0700153
Herbert Xu32e3983f2007-03-24 14:35:34 +1100154typedef void (*crypto_completion_t)(struct crypto_async_request *req, int err);
155
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100156/**
157 * DOC: Block Cipher Context Data Structures
158 *
159 * These data structures define the operating context for each block cipher
160 * type.
161 */
162
Herbert Xu32e3983f2007-03-24 14:35:34 +1100163struct crypto_async_request {
164 struct list_head list;
165 crypto_completion_t complete;
166 void *data;
167 struct crypto_tfm *tfm;
168
169 u32 flags;
170};
171
172struct ablkcipher_request {
173 struct crypto_async_request base;
174
175 unsigned int nbytes;
176
177 void *info;
178
179 struct scatterlist *src;
180 struct scatterlist *dst;
181
182 void *__ctx[] CRYPTO_MINALIGN_ATTR;
183};
184
Herbert Xu5cde0af2006-08-22 00:07:53 +1000185struct blkcipher_desc {
186 struct crypto_blkcipher *tfm;
187 void *info;
188 u32 flags;
189};
190
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100191/**
192 * DOC: Block Cipher Algorithm Definitions
193 *
194 * These data structures define modular crypto algorithm implementations,
195 * managed via crypto_register_alg() and crypto_unregister_alg().
196 */
197
198/**
199 * struct ablkcipher_alg - asynchronous block cipher definition
200 * @min_keysize: Minimum key size supported by the transformation. This is the
201 * smallest key length supported by this transformation algorithm.
202 * This must be set to one of the pre-defined values as this is
203 * not hardware specific. Possible values for this field can be
204 * found via git grep "_MIN_KEY_SIZE" include/crypto/
205 * @max_keysize: Maximum key size supported by the transformation. This is the
206 * largest key length supported by this transformation algorithm.
207 * This must be set to one of the pre-defined values as this is
208 * not hardware specific. Possible values for this field can be
209 * found via git grep "_MAX_KEY_SIZE" include/crypto/
210 * @setkey: Set key for the transformation. This function is used to either
211 * program a supplied key into the hardware or store the key in the
212 * transformation context for programming it later. Note that this
213 * function does modify the transformation context. This function can
214 * be called multiple times during the existence of the transformation
215 * object, so one must make sure the key is properly reprogrammed into
216 * the hardware. This function is also responsible for checking the key
217 * length for validity. In case a software fallback was put in place in
218 * the @cra_init call, this function might need to use the fallback if
219 * the algorithm doesn't support all of the key sizes.
220 * @encrypt: Encrypt a scatterlist of blocks. This function is used to encrypt
221 * the supplied scatterlist containing the blocks of data. The crypto
222 * API consumer is responsible for aligning the entries of the
223 * scatterlist properly and making sure the chunks are correctly
224 * sized. In case a software fallback was put in place in the
225 * @cra_init call, this function might need to use the fallback if
226 * the algorithm doesn't support all of the key sizes. In case the
227 * key was stored in transformation context, the key might need to be
228 * re-programmed into the hardware in this function. This function
229 * shall not modify the transformation context, as this function may
230 * be called in parallel with the same transformation object.
231 * @decrypt: Decrypt a single block. This is a reverse counterpart to @encrypt
232 * and the conditions are exactly the same.
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100233 * @ivsize: IV size applicable for transformation. The consumer must provide an
234 * IV of exactly that size to perform the encrypt or decrypt operation.
235 *
Eric Biggersc79b4112018-12-16 15:55:06 -0800236 * All fields except @ivsize are mandatory and must be filled.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 */
Herbert Xub5b7f082007-04-16 20:48:54 +1000238struct ablkcipher_alg {
239 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
240 unsigned int keylen);
241 int (*encrypt)(struct ablkcipher_request *req);
242 int (*decrypt)(struct ablkcipher_request *req);
Herbert Xu23508e12007-11-27 21:33:24 +0800243
Herbert Xub5b7f082007-04-16 20:48:54 +1000244 unsigned int min_keysize;
245 unsigned int max_keysize;
246 unsigned int ivsize;
247};
248
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100249/**
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100250 * struct blkcipher_alg - synchronous block cipher definition
251 * @min_keysize: see struct ablkcipher_alg
252 * @max_keysize: see struct ablkcipher_alg
253 * @setkey: see struct ablkcipher_alg
254 * @encrypt: see struct ablkcipher_alg
255 * @decrypt: see struct ablkcipher_alg
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100256 * @ivsize: see struct ablkcipher_alg
257 *
Eric Biggersc79b4112018-12-16 15:55:06 -0800258 * All fields except @ivsize are mandatory and must be filled.
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100259 */
Herbert Xu5cde0af2006-08-22 00:07:53 +1000260struct blkcipher_alg {
261 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
262 unsigned int keylen);
263 int (*encrypt)(struct blkcipher_desc *desc,
264 struct scatterlist *dst, struct scatterlist *src,
265 unsigned int nbytes);
266 int (*decrypt)(struct blkcipher_desc *desc,
267 struct scatterlist *dst, struct scatterlist *src,
268 unsigned int nbytes);
269
270 unsigned int min_keysize;
271 unsigned int max_keysize;
272 unsigned int ivsize;
273};
274
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100275/**
276 * struct cipher_alg - single-block symmetric ciphers definition
277 * @cia_min_keysize: Minimum key size supported by the transformation. This is
278 * the smallest key length supported by this transformation
279 * algorithm. This must be set to one of the pre-defined
280 * values as this is not hardware specific. Possible values
281 * for this field can be found via git grep "_MIN_KEY_SIZE"
282 * include/crypto/
283 * @cia_max_keysize: Maximum key size supported by the transformation. This is
284 * the largest key length supported by this transformation
285 * algorithm. This must be set to one of the pre-defined values
286 * as this is not hardware specific. Possible values for this
287 * field can be found via git grep "_MAX_KEY_SIZE"
288 * include/crypto/
289 * @cia_setkey: Set key for the transformation. This function is used to either
290 * program a supplied key into the hardware or store the key in the
291 * transformation context for programming it later. Note that this
292 * function does modify the transformation context. This function
293 * can be called multiple times during the existence of the
294 * transformation object, so one must make sure the key is properly
295 * reprogrammed into the hardware. This function is also
296 * responsible for checking the key length for validity.
297 * @cia_encrypt: Encrypt a single block. This function is used to encrypt a
298 * single block of data, which must be @cra_blocksize big. This
299 * always operates on a full @cra_blocksize and it is not possible
300 * to encrypt a block of smaller size. The supplied buffers must
301 * therefore also be at least of @cra_blocksize size. Both the
302 * input and output buffers are always aligned to @cra_alignmask.
303 * In case either of the input or output buffer supplied by user
304 * of the crypto API is not aligned to @cra_alignmask, the crypto
305 * API will re-align the buffers. The re-alignment means that a
306 * new buffer will be allocated, the data will be copied into the
307 * new buffer, then the processing will happen on the new buffer,
308 * then the data will be copied back into the original buffer and
309 * finally the new buffer will be freed. In case a software
310 * fallback was put in place in the @cra_init call, this function
311 * might need to use the fallback if the algorithm doesn't support
312 * all of the key sizes. In case the key was stored in
313 * transformation context, the key might need to be re-programmed
314 * into the hardware in this function. This function shall not
315 * modify the transformation context, as this function may be
316 * called in parallel with the same transformation object.
317 * @cia_decrypt: Decrypt a single block. This is a reverse counterpart to
318 * @cia_encrypt, and the conditions are exactly the same.
319 *
320 * All fields are mandatory and must be filled.
321 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322struct cipher_alg {
323 unsigned int cia_min_keysize;
324 unsigned int cia_max_keysize;
Herbert Xu6c2bb982006-05-16 22:09:29 +1000325 int (*cia_setkey)(struct crypto_tfm *tfm, const u8 *key,
Herbert Xu560c06a2006-08-13 14:16:39 +1000326 unsigned int keylen);
Herbert Xu6c2bb982006-05-16 22:09:29 +1000327 void (*cia_encrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
328 void (*cia_decrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329};
330
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331struct compress_alg {
Herbert Xu6c2bb982006-05-16 22:09:29 +1000332 int (*coa_compress)(struct crypto_tfm *tfm, const u8 *src,
333 unsigned int slen, u8 *dst, unsigned int *dlen);
334 int (*coa_decompress)(struct crypto_tfm *tfm, const u8 *src,
335 unsigned int slen, u8 *dst, unsigned int *dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336};
337
Corentin Labbe17c18f92018-11-29 14:42:24 +0000338#ifdef CONFIG_CRYPTO_STATS
339/*
340 * struct crypto_istat_aead - statistics for AEAD algorithm
341 * @encrypt_cnt: number of encrypt requests
342 * @encrypt_tlen: total data size handled by encrypt requests
343 * @decrypt_cnt: number of decrypt requests
344 * @decrypt_tlen: total data size handled by decrypt requests
Corentin Labbe44f13132018-11-29 14:42:25 +0000345 * @err_cnt: number of error for AEAD requests
Corentin Labbe17c18f92018-11-29 14:42:24 +0000346 */
347struct crypto_istat_aead {
348 atomic64_t encrypt_cnt;
349 atomic64_t encrypt_tlen;
350 atomic64_t decrypt_cnt;
351 atomic64_t decrypt_tlen;
Corentin Labbe44f13132018-11-29 14:42:25 +0000352 atomic64_t err_cnt;
Corentin Labbe17c18f92018-11-29 14:42:24 +0000353};
354
355/*
356 * struct crypto_istat_akcipher - statistics for akcipher algorithm
357 * @encrypt_cnt: number of encrypt requests
358 * @encrypt_tlen: total data size handled by encrypt requests
359 * @decrypt_cnt: number of decrypt requests
360 * @decrypt_tlen: total data size handled by decrypt requests
361 * @verify_cnt: number of verify operation
362 * @sign_cnt: number of sign requests
Corentin Labbe44f13132018-11-29 14:42:25 +0000363 * @err_cnt: number of error for akcipher requests
Corentin Labbe17c18f92018-11-29 14:42:24 +0000364 */
365struct crypto_istat_akcipher {
366 atomic64_t encrypt_cnt;
367 atomic64_t encrypt_tlen;
368 atomic64_t decrypt_cnt;
369 atomic64_t decrypt_tlen;
370 atomic64_t verify_cnt;
371 atomic64_t sign_cnt;
Corentin Labbe44f13132018-11-29 14:42:25 +0000372 atomic64_t err_cnt;
Corentin Labbe17c18f92018-11-29 14:42:24 +0000373};
374
375/*
376 * struct crypto_istat_cipher - statistics for cipher algorithm
377 * @encrypt_cnt: number of encrypt requests
378 * @encrypt_tlen: total data size handled by encrypt requests
379 * @decrypt_cnt: number of decrypt requests
380 * @decrypt_tlen: total data size handled by decrypt requests
Corentin Labbe44f13132018-11-29 14:42:25 +0000381 * @err_cnt: number of error for cipher requests
Corentin Labbe17c18f92018-11-29 14:42:24 +0000382 */
383struct crypto_istat_cipher {
384 atomic64_t encrypt_cnt;
385 atomic64_t encrypt_tlen;
386 atomic64_t decrypt_cnt;
387 atomic64_t decrypt_tlen;
Corentin Labbe44f13132018-11-29 14:42:25 +0000388 atomic64_t err_cnt;
Corentin Labbe17c18f92018-11-29 14:42:24 +0000389};
390
391/*
392 * struct crypto_istat_compress - statistics for compress algorithm
393 * @compress_cnt: number of compress requests
394 * @compress_tlen: total data size handled by compress requests
395 * @decompress_cnt: number of decompress requests
396 * @decompress_tlen: total data size handled by decompress requests
Corentin Labbe44f13132018-11-29 14:42:25 +0000397 * @err_cnt: number of error for compress requests
Corentin Labbe17c18f92018-11-29 14:42:24 +0000398 */
399struct crypto_istat_compress {
400 atomic64_t compress_cnt;
401 atomic64_t compress_tlen;
402 atomic64_t decompress_cnt;
403 atomic64_t decompress_tlen;
Corentin Labbe44f13132018-11-29 14:42:25 +0000404 atomic64_t err_cnt;
Corentin Labbe17c18f92018-11-29 14:42:24 +0000405};
406
407/*
408 * struct crypto_istat_hash - statistics for has algorithm
409 * @hash_cnt: number of hash requests
410 * @hash_tlen: total data size hashed
Corentin Labbe44f13132018-11-29 14:42:25 +0000411 * @err_cnt: number of error for hash requests
Corentin Labbe17c18f92018-11-29 14:42:24 +0000412 */
413struct crypto_istat_hash {
414 atomic64_t hash_cnt;
415 atomic64_t hash_tlen;
Corentin Labbe44f13132018-11-29 14:42:25 +0000416 atomic64_t err_cnt;
Corentin Labbe17c18f92018-11-29 14:42:24 +0000417};
418
419/*
420 * struct crypto_istat_kpp - statistics for KPP algorithm
421 * @setsecret_cnt: number of setsecrey operation
422 * @generate_public_key_cnt: number of generate_public_key operation
423 * @compute_shared_secret_cnt: number of compute_shared_secret operation
Corentin Labbe44f13132018-11-29 14:42:25 +0000424 * @err_cnt: number of error for KPP requests
Corentin Labbe17c18f92018-11-29 14:42:24 +0000425 */
426struct crypto_istat_kpp {
427 atomic64_t setsecret_cnt;
428 atomic64_t generate_public_key_cnt;
429 atomic64_t compute_shared_secret_cnt;
Corentin Labbe44f13132018-11-29 14:42:25 +0000430 atomic64_t err_cnt;
Corentin Labbe17c18f92018-11-29 14:42:24 +0000431};
432
433/*
434 * struct crypto_istat_rng: statistics for RNG algorithm
435 * @generate_cnt: number of RNG generate requests
436 * @generate_tlen: total data size of generated data by the RNG
437 * @seed_cnt: number of times the RNG was seeded
Corentin Labbe44f13132018-11-29 14:42:25 +0000438 * @err_cnt: number of error for RNG requests
Corentin Labbe17c18f92018-11-29 14:42:24 +0000439 */
440struct crypto_istat_rng {
441 atomic64_t generate_cnt;
442 atomic64_t generate_tlen;
443 atomic64_t seed_cnt;
Corentin Labbe44f13132018-11-29 14:42:25 +0000444 atomic64_t err_cnt;
Corentin Labbe17c18f92018-11-29 14:42:24 +0000445};
446#endif /* CONFIG_CRYPTO_STATS */
Neil Horman17f0f4a2008-08-14 22:15:52 +1000447
Herbert Xub5b7f082007-04-16 20:48:54 +1000448#define cra_ablkcipher cra_u.ablkcipher
Herbert Xu5cde0af2006-08-22 00:07:53 +1000449#define cra_blkcipher cra_u.blkcipher
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450#define cra_cipher cra_u.cipher
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451#define cra_compress cra_u.compress
452
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100453/**
454 * struct crypto_alg - definition of a cryptograpic cipher algorithm
455 * @cra_flags: Flags describing this transformation. See include/linux/crypto.h
456 * CRYPTO_ALG_* flags for the flags which go in here. Those are
457 * used for fine-tuning the description of the transformation
458 * algorithm.
459 * @cra_blocksize: Minimum block size of this transformation. The size in bytes
460 * of the smallest possible unit which can be transformed with
461 * this algorithm. The users must respect this value.
462 * In case of HASH transformation, it is possible for a smaller
463 * block than @cra_blocksize to be passed to the crypto API for
464 * transformation, in case of any other transformation type, an
465 * error will be returned upon any attempt to transform smaller
466 * than @cra_blocksize chunks.
467 * @cra_ctxsize: Size of the operational context of the transformation. This
468 * value informs the kernel crypto API about the memory size
469 * needed to be allocated for the transformation context.
470 * @cra_alignmask: Alignment mask for the input and output data buffer. The data
471 * buffer containing the input data for the algorithm must be
472 * aligned to this alignment mask. The data buffer for the
473 * output data must be aligned to this alignment mask. Note that
474 * the Crypto API will do the re-alignment in software, but
475 * only under special conditions and there is a performance hit.
476 * The re-alignment happens at these occasions for different
477 * @cra_u types: cipher -- For both input data and output data
478 * buffer; ahash -- For output hash destination buf; shash --
479 * For output hash destination buf.
480 * This is needed on hardware which is flawed by design and
481 * cannot pick data from arbitrary addresses.
482 * @cra_priority: Priority of this transformation implementation. In case
483 * multiple transformations with same @cra_name are available to
484 * the Crypto API, the kernel will use the one with highest
485 * @cra_priority.
486 * @cra_name: Generic name (usable by multiple implementations) of the
487 * transformation algorithm. This is the name of the transformation
488 * itself. This field is used by the kernel when looking up the
489 * providers of particular transformation.
490 * @cra_driver_name: Unique name of the transformation provider. This is the
491 * name of the provider of the transformation. This can be any
492 * arbitrary value, but in the usual case, this contains the
493 * name of the chip or provider and the name of the
494 * transformation algorithm.
495 * @cra_type: Type of the cryptographic transformation. This is a pointer to
496 * struct crypto_type, which implements callbacks common for all
Masanari Iida12f7c142015-06-04 00:01:21 +0900497 * transformation types. There are multiple options:
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100498 * &crypto_blkcipher_type, &crypto_ablkcipher_type,
Herbert Xub0d955b2015-08-14 15:30:41 +0800499 * &crypto_ahash_type, &crypto_rng_type.
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100500 * This field might be empty. In that case, there are no common
501 * callbacks. This is the case for: cipher, compress, shash.
502 * @cra_u: Callbacks implementing the transformation. This is a union of
503 * multiple structures. Depending on the type of transformation selected
504 * by @cra_type and @cra_flags above, the associated structure must be
505 * filled with callbacks. This field might be empty. This is the case
506 * for ahash, shash.
507 * @cra_init: Initialize the cryptographic transformation object. This function
508 * is used to initialize the cryptographic transformation object.
509 * This function is called only once at the instantiation time, right
510 * after the transformation context was allocated. In case the
511 * cryptographic hardware has some special requirements which need to
512 * be handled by software, this function shall check for the precise
513 * requirement of the transformation and put any software fallbacks
514 * in place.
515 * @cra_exit: Deinitialize the cryptographic transformation object. This is a
516 * counterpart to @cra_init, used to remove various changes set in
517 * @cra_init.
Gary R Hook0063ec42018-03-14 17:15:52 -0500518 * @cra_u.ablkcipher: Union member which contains an asynchronous block cipher
519 * definition. See @struct @ablkcipher_alg.
520 * @cra_u.blkcipher: Union member which contains a synchronous block cipher
521 * definition See @struct @blkcipher_alg.
522 * @cra_u.cipher: Union member which contains a single-block symmetric cipher
523 * definition. See @struct @cipher_alg.
524 * @cra_u.compress: Union member which contains a (de)compression algorithm.
525 * See @struct @compress_alg.
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100526 * @cra_module: Owner of this transformation implementation. Set to THIS_MODULE
527 * @cra_list: internally used
528 * @cra_users: internally used
529 * @cra_refcnt: internally used
530 * @cra_destroy: internally used
531 *
Corentin Labbe17c18f92018-11-29 14:42:24 +0000532 * @stats: union of all possible crypto_istat_xxx structures
Corentin Labbebfad6cb2018-12-13 08:36:38 +0000533 * @stats.aead: statistics for AEAD algorithm
534 * @stats.akcipher: statistics for akcipher algorithm
535 * @stats.cipher: statistics for cipher algorithm
536 * @stats.compress: statistics for compress algorithm
537 * @stats.hash: statistics for hash algorithm
538 * @stats.rng: statistics for rng algorithm
539 * @stats.kpp: statistics for KPP algorithm
Corentin Labbecac58182018-09-19 10:10:54 +0000540 *
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100541 * The struct crypto_alg describes a generic Crypto API algorithm and is common
542 * for all of the transformations. Any variable not documented here shall not
543 * be used by a cipher implementation as it is internal to the Crypto API.
544 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545struct crypto_alg {
546 struct list_head cra_list;
Herbert Xu6bfd4802006-09-21 11:39:29 +1000547 struct list_head cra_users;
548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 u32 cra_flags;
550 unsigned int cra_blocksize;
551 unsigned int cra_ctxsize;
Herbert Xu95477372005-07-06 13:52:09 -0700552 unsigned int cra_alignmask;
Herbert Xu5cb1454b2005-11-05 16:58:14 +1100553
554 int cra_priority;
Eric Biggersce8614a2017-12-29 10:00:46 -0600555 refcount_t cra_refcnt;
Herbert Xu5cb1454b2005-11-05 16:58:14 +1100556
Herbert Xud913ea02006-05-21 08:45:26 +1000557 char cra_name[CRYPTO_MAX_ALG_NAME];
558 char cra_driver_name[CRYPTO_MAX_ALG_NAME];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Herbert Xue853c3c2006-08-22 00:06:54 +1000560 const struct crypto_type *cra_type;
561
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 union {
Herbert Xub5b7f082007-04-16 20:48:54 +1000563 struct ablkcipher_alg ablkcipher;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000564 struct blkcipher_alg blkcipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 struct cipher_alg cipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 struct compress_alg compress;
567 } cra_u;
Herbert Xuc7fc0592006-05-24 13:02:26 +1000568
569 int (*cra_init)(struct crypto_tfm *tfm);
570 void (*cra_exit)(struct crypto_tfm *tfm);
Herbert Xu6521f302006-08-06 20:28:44 +1000571 void (*cra_destroy)(struct crypto_alg *alg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
573 struct module *cra_module;
Corentin Labbecac58182018-09-19 10:10:54 +0000574
Corentin Labbe2ced2602018-11-29 14:42:16 +0000575#ifdef CONFIG_CRYPTO_STATS
Corentin Labbecac58182018-09-19 10:10:54 +0000576 union {
Corentin Labbe17c18f92018-11-29 14:42:24 +0000577 struct crypto_istat_aead aead;
578 struct crypto_istat_akcipher akcipher;
579 struct crypto_istat_cipher cipher;
580 struct crypto_istat_compress compress;
581 struct crypto_istat_hash hash;
582 struct crypto_istat_rng rng;
583 struct crypto_istat_kpp kpp;
584 } stats;
Corentin Labbe2ced2602018-11-29 14:42:16 +0000585#endif /* CONFIG_CRYPTO_STATS */
Corentin Labbecac58182018-09-19 10:10:54 +0000586
Herbert Xuedf18b92015-06-18 14:00:48 +0800587} CRYPTO_MINALIGN_ATTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
Corentin Labbef7d76e02018-11-29 14:42:21 +0000589#ifdef CONFIG_CRYPTO_STATS
Corentin Labbe1f6669b2018-11-29 14:42:26 +0000590void crypto_stats_init(struct crypto_alg *alg);
Corentin Labbef7d76e02018-11-29 14:42:21 +0000591void crypto_stats_get(struct crypto_alg *alg);
592void crypto_stats_ablkcipher_encrypt(unsigned int nbytes, int ret, struct crypto_alg *alg);
593void crypto_stats_ablkcipher_decrypt(unsigned int nbytes, int ret, struct crypto_alg *alg);
594void crypto_stats_aead_encrypt(unsigned int cryptlen, struct crypto_alg *alg, int ret);
595void crypto_stats_aead_decrypt(unsigned int cryptlen, struct crypto_alg *alg, int ret);
596void crypto_stats_ahash_update(unsigned int nbytes, int ret, struct crypto_alg *alg);
597void crypto_stats_ahash_final(unsigned int nbytes, int ret, struct crypto_alg *alg);
598void crypto_stats_akcipher_encrypt(unsigned int src_len, int ret, struct crypto_alg *alg);
599void crypto_stats_akcipher_decrypt(unsigned int src_len, int ret, struct crypto_alg *alg);
600void crypto_stats_akcipher_sign(int ret, struct crypto_alg *alg);
601void crypto_stats_akcipher_verify(int ret, struct crypto_alg *alg);
602void crypto_stats_compress(unsigned int slen, int ret, struct crypto_alg *alg);
603void crypto_stats_decompress(unsigned int slen, int ret, struct crypto_alg *alg);
604void crypto_stats_kpp_set_secret(struct crypto_alg *alg, int ret);
605void crypto_stats_kpp_generate_public_key(struct crypto_alg *alg, int ret);
606void crypto_stats_kpp_compute_shared_secret(struct crypto_alg *alg, int ret);
607void crypto_stats_rng_seed(struct crypto_alg *alg, int ret);
608void crypto_stats_rng_generate(struct crypto_alg *alg, unsigned int dlen, int ret);
609void crypto_stats_skcipher_encrypt(unsigned int cryptlen, int ret, struct crypto_alg *alg);
610void crypto_stats_skcipher_decrypt(unsigned int cryptlen, int ret, struct crypto_alg *alg);
611#else
Corentin Labbe1f6669b2018-11-29 14:42:26 +0000612static inline void crypto_stats_init(struct crypto_alg *alg)
613{}
Corentin Labbef7d76e02018-11-29 14:42:21 +0000614static inline void crypto_stats_get(struct crypto_alg *alg)
615{}
616static inline void crypto_stats_ablkcipher_encrypt(unsigned int nbytes, int ret, struct crypto_alg *alg)
617{}
618static inline void crypto_stats_ablkcipher_decrypt(unsigned int nbytes, int ret, struct crypto_alg *alg)
619{}
620static inline void crypto_stats_aead_encrypt(unsigned int cryptlen, struct crypto_alg *alg, int ret)
621{}
622static inline void crypto_stats_aead_decrypt(unsigned int cryptlen, struct crypto_alg *alg, int ret)
623{}
624static inline void crypto_stats_ahash_update(unsigned int nbytes, int ret, struct crypto_alg *alg)
625{}
626static inline void crypto_stats_ahash_final(unsigned int nbytes, int ret, struct crypto_alg *alg)
627{}
628static inline void crypto_stats_akcipher_encrypt(unsigned int src_len, int ret, struct crypto_alg *alg)
629{}
630static inline void crypto_stats_akcipher_decrypt(unsigned int src_len, int ret, struct crypto_alg *alg)
631{}
632static inline void crypto_stats_akcipher_sign(int ret, struct crypto_alg *alg)
633{}
634static inline void crypto_stats_akcipher_verify(int ret, struct crypto_alg *alg)
635{}
636static inline void crypto_stats_compress(unsigned int slen, int ret, struct crypto_alg *alg)
637{}
638static inline void crypto_stats_decompress(unsigned int slen, int ret, struct crypto_alg *alg)
639{}
640static inline void crypto_stats_kpp_set_secret(struct crypto_alg *alg, int ret)
641{}
642static inline void crypto_stats_kpp_generate_public_key(struct crypto_alg *alg, int ret)
643{}
644static inline void crypto_stats_kpp_compute_shared_secret(struct crypto_alg *alg, int ret)
645{}
646static inline void crypto_stats_rng_seed(struct crypto_alg *alg, int ret)
647{}
648static inline void crypto_stats_rng_generate(struct crypto_alg *alg, unsigned int dlen, int ret)
649{}
650static inline void crypto_stats_skcipher_encrypt(unsigned int cryptlen, int ret, struct crypto_alg *alg)
651{}
652static inline void crypto_stats_skcipher_decrypt(unsigned int cryptlen, int ret, struct crypto_alg *alg)
653{}
654#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655/*
Gilad Ben-Yossefada69a12017-10-18 08:00:38 +0100656 * A helper struct for waiting for completion of async crypto ops
657 */
658struct crypto_wait {
659 struct completion completion;
660 int err;
661};
662
663/*
664 * Macro for declaring a crypto op async wait object on stack
665 */
666#define DECLARE_CRYPTO_WAIT(_wait) \
667 struct crypto_wait _wait = { \
668 COMPLETION_INITIALIZER_ONSTACK((_wait).completion), 0 }
669
670/*
671 * Async ops completion helper functioons
672 */
673void crypto_req_done(struct crypto_async_request *req, int err);
674
675static inline int crypto_wait_req(int err, struct crypto_wait *wait)
676{
677 switch (err) {
678 case -EINPROGRESS:
679 case -EBUSY:
680 wait_for_completion(&wait->completion);
681 reinit_completion(&wait->completion);
682 err = wait->err;
683 break;
684 };
685
686 return err;
687}
688
689static inline void crypto_init_wait(struct crypto_wait *wait)
690{
691 init_completion(&wait->completion);
692}
693
694/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 * Algorithm registration interface.
696 */
697int crypto_register_alg(struct crypto_alg *alg);
698int crypto_unregister_alg(struct crypto_alg *alg);
Mark Brown4b004342012-01-17 23:34:26 +0000699int crypto_register_algs(struct crypto_alg *algs, int count);
700int crypto_unregister_algs(struct crypto_alg *algs, int count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
702/*
703 * Algorithm query interface.
704 */
Herbert Xufce32d72006-08-26 17:35:45 +1000705int crypto_has_alg(const char *name, u32 type, u32 mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
707/*
708 * Transforms: user-instantiated objects which encapsulate algorithms
Herbert Xu6d7d684d2006-07-30 11:53:01 +1000709 * and core processing logic. Managed via crypto_alloc_*() and
710 * crypto_free_*(), as well as the various helpers below.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
Herbert Xu32e3983f2007-03-24 14:35:34 +1100713struct ablkcipher_tfm {
714 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
715 unsigned int keylen);
716 int (*encrypt)(struct ablkcipher_request *req);
717 int (*decrypt)(struct ablkcipher_request *req);
Herbert Xu61da88e2007-12-17 21:51:27 +0800718
Herbert Xuecfc4322007-12-05 21:08:36 +1100719 struct crypto_ablkcipher *base;
720
Herbert Xu32e3983f2007-03-24 14:35:34 +1100721 unsigned int ivsize;
722 unsigned int reqsize;
723};
724
Herbert Xu5cde0af2006-08-22 00:07:53 +1000725struct blkcipher_tfm {
726 void *iv;
727 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
728 unsigned int keylen);
729 int (*encrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
730 struct scatterlist *src, unsigned int nbytes);
731 int (*decrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
732 struct scatterlist *src, unsigned int nbytes);
733};
734
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735struct cipher_tfm {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 int (*cit_setkey)(struct crypto_tfm *tfm,
737 const u8 *key, unsigned int keylen);
Herbert Xuf28776a2006-08-13 20:58:18 +1000738 void (*cit_encrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
739 void (*cit_decrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740};
741
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742struct compress_tfm {
743 int (*cot_compress)(struct crypto_tfm *tfm,
744 const u8 *src, unsigned int slen,
745 u8 *dst, unsigned int *dlen);
746 int (*cot_decompress)(struct crypto_tfm *tfm,
747 const u8 *src, unsigned int slen,
748 u8 *dst, unsigned int *dlen);
749};
750
Herbert Xu32e3983f2007-03-24 14:35:34 +1100751#define crt_ablkcipher crt_u.ablkcipher
Herbert Xu5cde0af2006-08-22 00:07:53 +1000752#define crt_blkcipher crt_u.blkcipher
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753#define crt_cipher crt_u.cipher
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754#define crt_compress crt_u.compress
755
756struct crypto_tfm {
757
758 u32 crt_flags;
759
760 union {
Herbert Xu32e3983f2007-03-24 14:35:34 +1100761 struct ablkcipher_tfm ablkcipher;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000762 struct blkcipher_tfm blkcipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 struct cipher_tfm cipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 struct compress_tfm compress;
765 } crt_u;
Herbert Xu4a779482008-09-13 18:19:03 -0700766
767 void (*exit)(struct crypto_tfm *tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
769 struct crypto_alg *__crt_alg;
Herbert Xuf10b7892006-01-25 22:34:01 +1100770
Herbert Xu79911102006-08-21 21:03:52 +1000771 void *__crt_ctx[] CRYPTO_MINALIGN_ATTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772};
773
Herbert Xu32e3983f2007-03-24 14:35:34 +1100774struct crypto_ablkcipher {
775 struct crypto_tfm base;
776};
777
Herbert Xu5cde0af2006-08-22 00:07:53 +1000778struct crypto_blkcipher {
779 struct crypto_tfm base;
780};
781
Herbert Xu78a1fe42006-12-24 10:02:00 +1100782struct crypto_cipher {
783 struct crypto_tfm base;
784};
785
786struct crypto_comp {
787 struct crypto_tfm base;
788};
789
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000790enum {
791 CRYPTOA_UNSPEC,
792 CRYPTOA_ALG,
Herbert Xuebc610e2007-01-01 18:37:02 +1100793 CRYPTOA_TYPE,
Herbert Xu39e1ee012007-08-29 19:27:26 +0800794 CRYPTOA_U32,
Herbert Xuebc610e2007-01-01 18:37:02 +1100795 __CRYPTOA_MAX,
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000796};
797
Herbert Xuebc610e2007-01-01 18:37:02 +1100798#define CRYPTOA_MAX (__CRYPTOA_MAX - 1)
799
Herbert Xu39e1ee012007-08-29 19:27:26 +0800800/* Maximum number of (rtattr) parameters for each template. */
801#define CRYPTO_MAX_ATTRS 32
802
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000803struct crypto_attr_alg {
804 char name[CRYPTO_MAX_ALG_NAME];
805};
806
Herbert Xuebc610e2007-01-01 18:37:02 +1100807struct crypto_attr_type {
808 u32 type;
809 u32 mask;
810};
811
Herbert Xu39e1ee012007-08-29 19:27:26 +0800812struct crypto_attr_u32 {
813 u32 num;
814};
815
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816/*
817 * Transform user interface.
818 */
819
Herbert Xu6d7d684d2006-07-30 11:53:01 +1000820struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask);
Herbert Xu7b2cd922009-02-05 16:48:24 +1100821void crypto_destroy_tfm(void *mem, struct crypto_tfm *tfm);
822
823static inline void crypto_free_tfm(struct crypto_tfm *tfm)
824{
825 return crypto_destroy_tfm(tfm, tfm);
826}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
Herbert Xuda7f0332008-07-31 17:08:25 +0800828int alg_test(const char *driver, const char *alg, u32 type, u32 mask);
829
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830/*
831 * Transform helpers which query the underlying algorithm.
832 */
833static inline const char *crypto_tfm_alg_name(struct crypto_tfm *tfm)
834{
835 return tfm->__crt_alg->cra_name;
836}
837
Michal Ludvigb14cdd62006-07-09 09:02:24 +1000838static inline const char *crypto_tfm_alg_driver_name(struct crypto_tfm *tfm)
839{
840 return tfm->__crt_alg->cra_driver_name;
841}
842
843static inline int crypto_tfm_alg_priority(struct crypto_tfm *tfm)
844{
845 return tfm->__crt_alg->cra_priority;
846}
847
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm)
849{
850 return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK;
851}
852
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm)
854{
855 return tfm->__crt_alg->cra_blocksize;
856}
857
Herbert Xufbdae9f2005-07-06 13:53:29 -0700858static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm *tfm)
859{
860 return tfm->__crt_alg->cra_alignmask;
861}
862
Herbert Xuf28776a2006-08-13 20:58:18 +1000863static inline u32 crypto_tfm_get_flags(struct crypto_tfm *tfm)
864{
865 return tfm->crt_flags;
866}
867
868static inline void crypto_tfm_set_flags(struct crypto_tfm *tfm, u32 flags)
869{
870 tfm->crt_flags |= flags;
871}
872
873static inline void crypto_tfm_clear_flags(struct crypto_tfm *tfm, u32 flags)
874{
875 tfm->crt_flags &= ~flags;
876}
877
Herbert Xu40725182005-07-06 13:51:52 -0700878static inline void *crypto_tfm_ctx(struct crypto_tfm *tfm)
879{
Herbert Xuf10b7892006-01-25 22:34:01 +1100880 return tfm->__crt_ctx;
881}
882
883static inline unsigned int crypto_tfm_ctx_alignment(void)
884{
885 struct crypto_tfm *tfm;
886 return __alignof__(tfm->__crt_ctx);
Herbert Xu40725182005-07-06 13:51:52 -0700887}
888
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889/*
890 * API wrappers.
891 */
Herbert Xu32e3983f2007-03-24 14:35:34 +1100892static inline struct crypto_ablkcipher *__crypto_ablkcipher_cast(
893 struct crypto_tfm *tfm)
894{
895 return (struct crypto_ablkcipher *)tfm;
896}
897
Herbert Xu378f4f52007-12-17 20:07:31 +0800898static inline u32 crypto_skcipher_type(u32 type)
899{
Eric Biggersc79b4112018-12-16 15:55:06 -0800900 type &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xu378f4f52007-12-17 20:07:31 +0800901 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
902 return type;
903}
904
905static inline u32 crypto_skcipher_mask(u32 mask)
906{
Eric Biggersc79b4112018-12-16 15:55:06 -0800907 mask &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xu378f4f52007-12-17 20:07:31 +0800908 mask |= CRYPTO_ALG_TYPE_BLKCIPHER_MASK;
909 return mask;
910}
911
Stephan Muellerf13ec332014-11-12 05:28:22 +0100912/**
913 * DOC: Asynchronous Block Cipher API
914 *
915 * Asynchronous block cipher API is used with the ciphers of type
916 * CRYPTO_ALG_TYPE_ABLKCIPHER (listed as type "ablkcipher" in /proc/crypto).
917 *
918 * Asynchronous cipher operations imply that the function invocation for a
919 * cipher request returns immediately before the completion of the operation.
920 * The cipher request is scheduled as a separate kernel thread and therefore
921 * load-balanced on the different CPUs via the process scheduler. To allow
922 * the kernel crypto API to inform the caller about the completion of a cipher
923 * request, the caller must provide a callback function. That function is
924 * invoked with the cipher handle when the request completes.
925 *
926 * To support the asynchronous operation, additional information than just the
927 * cipher handle must be supplied to the kernel crypto API. That additional
928 * information is given by filling in the ablkcipher_request data structure.
929 *
930 * For the asynchronous block cipher API, the state is maintained with the tfm
931 * cipher handle. A single tfm can be used across multiple calls and in
932 * parallel. For asynchronous block cipher calls, context data supplied and
933 * only used by the caller can be referenced the request data structure in
934 * addition to the IV used for the cipher request. The maintenance of such
935 * state information would be important for a crypto driver implementer to
936 * have, because when calling the callback function upon completion of the
937 * cipher operation, that callback function may need some information about
938 * which operation just finished if it invoked multiple in parallel. This
939 * state information is unused by the kernel crypto API.
940 */
941
Herbert Xu32e3983f2007-03-24 14:35:34 +1100942static inline struct crypto_tfm *crypto_ablkcipher_tfm(
943 struct crypto_ablkcipher *tfm)
944{
945 return &tfm->base;
946}
947
Stephan Muellerf13ec332014-11-12 05:28:22 +0100948/**
949 * crypto_free_ablkcipher() - zeroize and free cipher handle
950 * @tfm: cipher handle to be freed
951 */
Herbert Xu32e3983f2007-03-24 14:35:34 +1100952static inline void crypto_free_ablkcipher(struct crypto_ablkcipher *tfm)
953{
954 crypto_free_tfm(crypto_ablkcipher_tfm(tfm));
955}
956
Stephan Muellerf13ec332014-11-12 05:28:22 +0100957/**
958 * crypto_has_ablkcipher() - Search for the availability of an ablkcipher.
959 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
960 * ablkcipher
961 * @type: specifies the type of the cipher
962 * @mask: specifies the mask for the cipher
963 *
964 * Return: true when the ablkcipher is known to the kernel crypto API; false
965 * otherwise
966 */
Herbert Xu32e3983f2007-03-24 14:35:34 +1100967static inline int crypto_has_ablkcipher(const char *alg_name, u32 type,
968 u32 mask)
969{
Herbert Xu378f4f52007-12-17 20:07:31 +0800970 return crypto_has_alg(alg_name, crypto_skcipher_type(type),
971 crypto_skcipher_mask(mask));
Herbert Xu32e3983f2007-03-24 14:35:34 +1100972}
973
974static inline struct ablkcipher_tfm *crypto_ablkcipher_crt(
975 struct crypto_ablkcipher *tfm)
976{
977 return &crypto_ablkcipher_tfm(tfm)->crt_ablkcipher;
978}
979
Stephan Muellerf13ec332014-11-12 05:28:22 +0100980/**
981 * crypto_ablkcipher_ivsize() - obtain IV size
982 * @tfm: cipher handle
983 *
984 * The size of the IV for the ablkcipher referenced by the cipher handle is
985 * returned. This IV size may be zero if the cipher does not need an IV.
986 *
987 * Return: IV size in bytes
988 */
Herbert Xu32e3983f2007-03-24 14:35:34 +1100989static inline unsigned int crypto_ablkcipher_ivsize(
990 struct crypto_ablkcipher *tfm)
991{
992 return crypto_ablkcipher_crt(tfm)->ivsize;
993}
994
Stephan Muellerf13ec332014-11-12 05:28:22 +0100995/**
996 * crypto_ablkcipher_blocksize() - obtain block size of cipher
997 * @tfm: cipher handle
998 *
999 * The block size for the ablkcipher referenced with the cipher handle is
1000 * returned. The caller may use that information to allocate appropriate
1001 * memory for the data returned by the encryption or decryption operation
1002 *
1003 * Return: block size of cipher
1004 */
Herbert Xu32e3983f2007-03-24 14:35:34 +11001005static inline unsigned int crypto_ablkcipher_blocksize(
1006 struct crypto_ablkcipher *tfm)
1007{
1008 return crypto_tfm_alg_blocksize(crypto_ablkcipher_tfm(tfm));
1009}
1010
1011static inline unsigned int crypto_ablkcipher_alignmask(
1012 struct crypto_ablkcipher *tfm)
1013{
1014 return crypto_tfm_alg_alignmask(crypto_ablkcipher_tfm(tfm));
1015}
1016
1017static inline u32 crypto_ablkcipher_get_flags(struct crypto_ablkcipher *tfm)
1018{
1019 return crypto_tfm_get_flags(crypto_ablkcipher_tfm(tfm));
1020}
1021
1022static inline void crypto_ablkcipher_set_flags(struct crypto_ablkcipher *tfm,
1023 u32 flags)
1024{
1025 crypto_tfm_set_flags(crypto_ablkcipher_tfm(tfm), flags);
1026}
1027
1028static inline void crypto_ablkcipher_clear_flags(struct crypto_ablkcipher *tfm,
1029 u32 flags)
1030{
1031 crypto_tfm_clear_flags(crypto_ablkcipher_tfm(tfm), flags);
1032}
1033
Stephan Muellerf13ec332014-11-12 05:28:22 +01001034/**
1035 * crypto_ablkcipher_setkey() - set key for cipher
1036 * @tfm: cipher handle
1037 * @key: buffer holding the key
1038 * @keylen: length of the key in bytes
1039 *
1040 * The caller provided key is set for the ablkcipher referenced by the cipher
1041 * handle.
1042 *
1043 * Note, the key length determines the cipher type. Many block ciphers implement
1044 * different cipher modes depending on the key size, such as AES-128 vs AES-192
1045 * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
1046 * is performed.
1047 *
1048 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
1049 */
Herbert Xu32e3983f2007-03-24 14:35:34 +11001050static inline int crypto_ablkcipher_setkey(struct crypto_ablkcipher *tfm,
1051 const u8 *key, unsigned int keylen)
1052{
Herbert Xuecfc4322007-12-05 21:08:36 +11001053 struct ablkcipher_tfm *crt = crypto_ablkcipher_crt(tfm);
1054
1055 return crt->setkey(crt->base, key, keylen);
Herbert Xu32e3983f2007-03-24 14:35:34 +11001056}
1057
Stephan Muellerf13ec332014-11-12 05:28:22 +01001058/**
1059 * crypto_ablkcipher_reqtfm() - obtain cipher handle from request
1060 * @req: ablkcipher_request out of which the cipher handle is to be obtained
1061 *
1062 * Return the crypto_ablkcipher handle when furnishing an ablkcipher_request
1063 * data structure.
1064 *
1065 * Return: crypto_ablkcipher handle
1066 */
Herbert Xu32e3983f2007-03-24 14:35:34 +11001067static inline struct crypto_ablkcipher *crypto_ablkcipher_reqtfm(
1068 struct ablkcipher_request *req)
1069{
1070 return __crypto_ablkcipher_cast(req->base.tfm);
1071}
1072
Stephan Muellerf13ec332014-11-12 05:28:22 +01001073/**
1074 * crypto_ablkcipher_encrypt() - encrypt plaintext
1075 * @req: reference to the ablkcipher_request handle that holds all information
1076 * needed to perform the cipher operation
1077 *
1078 * Encrypt plaintext data using the ablkcipher_request handle. That data
1079 * structure and how it is filled with data is discussed with the
1080 * ablkcipher_request_* functions.
1081 *
1082 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1083 */
Herbert Xu32e3983f2007-03-24 14:35:34 +11001084static inline int crypto_ablkcipher_encrypt(struct ablkcipher_request *req)
1085{
1086 struct ablkcipher_tfm *crt =
1087 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
Corentin Labbef7d76e02018-11-29 14:42:21 +00001088 struct crypto_alg *alg = crt->base->base.__crt_alg;
1089 unsigned int nbytes = req->nbytes;
Corentin Labbecac58182018-09-19 10:10:54 +00001090 int ret;
1091
Corentin Labbef7d76e02018-11-29 14:42:21 +00001092 crypto_stats_get(alg);
Corentin Labbecac58182018-09-19 10:10:54 +00001093 ret = crt->encrypt(req);
Corentin Labbef7d76e02018-11-29 14:42:21 +00001094 crypto_stats_ablkcipher_encrypt(nbytes, ret, alg);
Corentin Labbecac58182018-09-19 10:10:54 +00001095 return ret;
Herbert Xu32e3983f2007-03-24 14:35:34 +11001096}
1097
Stephan Muellerf13ec332014-11-12 05:28:22 +01001098/**
1099 * crypto_ablkcipher_decrypt() - decrypt ciphertext
1100 * @req: reference to the ablkcipher_request handle that holds all information
1101 * needed to perform the cipher operation
1102 *
1103 * Decrypt ciphertext data using the ablkcipher_request handle. That data
1104 * structure and how it is filled with data is discussed with the
1105 * ablkcipher_request_* functions.
1106 *
1107 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1108 */
Herbert Xu32e3983f2007-03-24 14:35:34 +11001109static inline int crypto_ablkcipher_decrypt(struct ablkcipher_request *req)
1110{
1111 struct ablkcipher_tfm *crt =
1112 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
Corentin Labbef7d76e02018-11-29 14:42:21 +00001113 struct crypto_alg *alg = crt->base->base.__crt_alg;
1114 unsigned int nbytes = req->nbytes;
Corentin Labbecac58182018-09-19 10:10:54 +00001115 int ret;
1116
Corentin Labbef7d76e02018-11-29 14:42:21 +00001117 crypto_stats_get(alg);
Corentin Labbecac58182018-09-19 10:10:54 +00001118 ret = crt->decrypt(req);
Corentin Labbef7d76e02018-11-29 14:42:21 +00001119 crypto_stats_ablkcipher_decrypt(nbytes, ret, alg);
Corentin Labbecac58182018-09-19 10:10:54 +00001120 return ret;
Herbert Xu32e3983f2007-03-24 14:35:34 +11001121}
1122
Stephan Muellerf13ec332014-11-12 05:28:22 +01001123/**
1124 * DOC: Asynchronous Cipher Request Handle
1125 *
1126 * The ablkcipher_request data structure contains all pointers to data
1127 * required for the asynchronous cipher operation. This includes the cipher
1128 * handle (which can be used by multiple ablkcipher_request instances), pointer
1129 * to plaintext and ciphertext, asynchronous callback function, etc. It acts
1130 * as a handle to the ablkcipher_request_* API calls in a similar way as
1131 * ablkcipher handle to the crypto_ablkcipher_* API calls.
1132 */
1133
1134/**
1135 * crypto_ablkcipher_reqsize() - obtain size of the request data structure
1136 * @tfm: cipher handle
1137 *
1138 * Return: number of bytes
1139 */
Herbert Xub16c3a22007-08-29 19:02:04 +08001140static inline unsigned int crypto_ablkcipher_reqsize(
1141 struct crypto_ablkcipher *tfm)
Herbert Xu32e3983f2007-03-24 14:35:34 +11001142{
1143 return crypto_ablkcipher_crt(tfm)->reqsize;
1144}
1145
Stephan Muellerf13ec332014-11-12 05:28:22 +01001146/**
1147 * ablkcipher_request_set_tfm() - update cipher handle reference in request
1148 * @req: request handle to be modified
1149 * @tfm: cipher handle that shall be added to the request handle
1150 *
1151 * Allow the caller to replace the existing ablkcipher handle in the request
1152 * data structure with a different one.
1153 */
Herbert Xue196d622007-04-14 16:09:14 +10001154static inline void ablkcipher_request_set_tfm(
1155 struct ablkcipher_request *req, struct crypto_ablkcipher *tfm)
1156{
Herbert Xuecfc4322007-12-05 21:08:36 +11001157 req->base.tfm = crypto_ablkcipher_tfm(crypto_ablkcipher_crt(tfm)->base);
Herbert Xue196d622007-04-14 16:09:14 +10001158}
1159
Herbert Xub5b7f082007-04-16 20:48:54 +10001160static inline struct ablkcipher_request *ablkcipher_request_cast(
1161 struct crypto_async_request *req)
1162{
1163 return container_of(req, struct ablkcipher_request, base);
1164}
1165
Stephan Muellerf13ec332014-11-12 05:28:22 +01001166/**
1167 * ablkcipher_request_alloc() - allocate request data structure
1168 * @tfm: cipher handle to be registered with the request
1169 * @gfp: memory allocation flag that is handed to kmalloc by the API call.
1170 *
1171 * Allocate the request data structure that must be used with the ablkcipher
1172 * encrypt and decrypt API calls. During the allocation, the provided ablkcipher
1173 * handle is registered in the request data structure.
1174 *
Eric Biggers6eae29e2016-04-02 10:54:56 -05001175 * Return: allocated request handle in case of success, or NULL if out of memory
Stephan Muellerf13ec332014-11-12 05:28:22 +01001176 */
Herbert Xu32e3983f2007-03-24 14:35:34 +11001177static inline struct ablkcipher_request *ablkcipher_request_alloc(
1178 struct crypto_ablkcipher *tfm, gfp_t gfp)
1179{
1180 struct ablkcipher_request *req;
1181
1182 req = kmalloc(sizeof(struct ablkcipher_request) +
1183 crypto_ablkcipher_reqsize(tfm), gfp);
1184
1185 if (likely(req))
Herbert Xue196d622007-04-14 16:09:14 +10001186 ablkcipher_request_set_tfm(req, tfm);
Herbert Xu32e3983f2007-03-24 14:35:34 +11001187
1188 return req;
1189}
1190
Stephan Muellerf13ec332014-11-12 05:28:22 +01001191/**
1192 * ablkcipher_request_free() - zeroize and free request data structure
1193 * @req: request data structure cipher handle to be freed
1194 */
Herbert Xu32e3983f2007-03-24 14:35:34 +11001195static inline void ablkcipher_request_free(struct ablkcipher_request *req)
1196{
Herbert Xuaef73cf2009-07-11 22:22:14 +08001197 kzfree(req);
Herbert Xu32e3983f2007-03-24 14:35:34 +11001198}
1199
Stephan Muellerf13ec332014-11-12 05:28:22 +01001200/**
1201 * ablkcipher_request_set_callback() - set asynchronous callback function
1202 * @req: request handle
1203 * @flags: specify zero or an ORing of the flags
Stephan Mueller0184cfe2016-10-21 04:57:27 +02001204 * CRYPTO_TFM_REQ_MAY_BACKLOG the request queue may back log and
Stephan Muellerf13ec332014-11-12 05:28:22 +01001205 * increase the wait queue beyond the initial maximum size;
1206 * CRYPTO_TFM_REQ_MAY_SLEEP the request processing may sleep
1207 * @compl: callback function pointer to be registered with the request handle
1208 * @data: The data pointer refers to memory that is not used by the kernel
1209 * crypto API, but provided to the callback function for it to use. Here,
1210 * the caller can provide a reference to memory the callback function can
1211 * operate on. As the callback function is invoked asynchronously to the
1212 * related functionality, it may need to access data structures of the
1213 * related functionality which can be referenced using this pointer. The
1214 * callback function can access the memory via the "data" field in the
1215 * crypto_async_request data structure provided to the callback function.
1216 *
1217 * This function allows setting the callback function that is triggered once the
1218 * cipher operation completes.
1219 *
1220 * The callback function is registered with the ablkcipher_request handle and
Stephan Mueller0184cfe2016-10-21 04:57:27 +02001221 * must comply with the following template::
Stephan Muellerf13ec332014-11-12 05:28:22 +01001222 *
1223 * void callback_function(struct crypto_async_request *req, int error)
1224 */
Herbert Xu32e3983f2007-03-24 14:35:34 +11001225static inline void ablkcipher_request_set_callback(
1226 struct ablkcipher_request *req,
Mark Rustad3e3dc252014-07-25 02:53:38 -07001227 u32 flags, crypto_completion_t compl, void *data)
Herbert Xu32e3983f2007-03-24 14:35:34 +11001228{
Mark Rustad3e3dc252014-07-25 02:53:38 -07001229 req->base.complete = compl;
Herbert Xu32e3983f2007-03-24 14:35:34 +11001230 req->base.data = data;
1231 req->base.flags = flags;
1232}
1233
Stephan Muellerf13ec332014-11-12 05:28:22 +01001234/**
1235 * ablkcipher_request_set_crypt() - set data buffers
1236 * @req: request handle
1237 * @src: source scatter / gather list
1238 * @dst: destination scatter / gather list
1239 * @nbytes: number of bytes to process from @src
1240 * @iv: IV for the cipher operation which must comply with the IV size defined
1241 * by crypto_ablkcipher_ivsize
1242 *
1243 * This function allows setting of the source data and destination data
1244 * scatter / gather lists.
1245 *
1246 * For encryption, the source is treated as the plaintext and the
1247 * destination is the ciphertext. For a decryption operation, the use is
Stephan Mueller379dcfb2015-01-19 00:13:39 +01001248 * reversed - the source is the ciphertext and the destination is the plaintext.
Stephan Muellerf13ec332014-11-12 05:28:22 +01001249 */
Herbert Xu32e3983f2007-03-24 14:35:34 +11001250static inline void ablkcipher_request_set_crypt(
1251 struct ablkcipher_request *req,
1252 struct scatterlist *src, struct scatterlist *dst,
1253 unsigned int nbytes, void *iv)
1254{
1255 req->src = src;
1256 req->dst = dst;
1257 req->nbytes = nbytes;
1258 req->info = iv;
1259}
1260
Stephan Muellerfced7b02014-11-12 05:29:00 +01001261/**
Stephan Mueller58284f02014-11-12 05:29:36 +01001262 * DOC: Synchronous Block Cipher API
1263 *
1264 * The synchronous block cipher API is used with the ciphers of type
1265 * CRYPTO_ALG_TYPE_BLKCIPHER (listed as type "blkcipher" in /proc/crypto)
1266 *
1267 * Synchronous calls, have a context in the tfm. But since a single tfm can be
1268 * used in multiple calls and in parallel, this info should not be changeable
1269 * (unless a lock is used). This applies, for example, to the symmetric key.
1270 * However, the IV is changeable, so there is an iv field in blkcipher_tfm
1271 * structure for synchronous blkcipher api. So, its the only state info that can
1272 * be kept for synchronous calls without using a big lock across a tfm.
1273 *
1274 * The block cipher API allows the use of a complete cipher, i.e. a cipher
1275 * consisting of a template (a block chaining mode) and a single block cipher
1276 * primitive (e.g. AES).
1277 *
1278 * The plaintext data buffer and the ciphertext data buffer are pointed to
1279 * by using scatter/gather lists. The cipher operation is performed
1280 * on all segments of the provided scatter/gather lists.
1281 *
1282 * The kernel crypto API supports a cipher operation "in-place" which means that
1283 * the caller may provide the same scatter/gather list for the plaintext and
1284 * cipher text. After the completion of the cipher operation, the plaintext
1285 * data is replaced with the ciphertext data in case of an encryption and vice
1286 * versa for a decryption. The caller must ensure that the scatter/gather lists
1287 * for the output data point to sufficiently large buffers, i.e. multiples of
1288 * the block size of the cipher.
1289 */
1290
Herbert Xu5cde0af2006-08-22 00:07:53 +10001291static inline struct crypto_blkcipher *__crypto_blkcipher_cast(
1292 struct crypto_tfm *tfm)
1293{
1294 return (struct crypto_blkcipher *)tfm;
1295}
1296
1297static inline struct crypto_blkcipher *crypto_blkcipher_cast(
1298 struct crypto_tfm *tfm)
1299{
1300 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_BLKCIPHER);
1301 return __crypto_blkcipher_cast(tfm);
1302}
1303
Stephan Mueller58284f02014-11-12 05:29:36 +01001304/**
1305 * crypto_alloc_blkcipher() - allocate synchronous block cipher handle
1306 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1307 * blkcipher cipher
1308 * @type: specifies the type of the cipher
1309 * @mask: specifies the mask for the cipher
1310 *
1311 * Allocate a cipher handle for a block cipher. The returned struct
1312 * crypto_blkcipher is the cipher handle that is required for any subsequent
1313 * API invocation for that block cipher.
1314 *
1315 * Return: allocated cipher handle in case of success; IS_ERR() is true in case
1316 * of an error, PTR_ERR() returns the error code.
1317 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001318static inline struct crypto_blkcipher *crypto_alloc_blkcipher(
1319 const char *alg_name, u32 type, u32 mask)
1320{
Herbert Xu332f88402007-11-15 22:36:07 +08001321 type &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xu5cde0af2006-08-22 00:07:53 +10001322 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
Herbert Xu332f88402007-11-15 22:36:07 +08001323 mask |= CRYPTO_ALG_TYPE_MASK;
Herbert Xu5cde0af2006-08-22 00:07:53 +10001324
1325 return __crypto_blkcipher_cast(crypto_alloc_base(alg_name, type, mask));
1326}
1327
1328static inline struct crypto_tfm *crypto_blkcipher_tfm(
1329 struct crypto_blkcipher *tfm)
1330{
1331 return &tfm->base;
1332}
1333
Stephan Mueller58284f02014-11-12 05:29:36 +01001334/**
1335 * crypto_free_blkcipher() - zeroize and free the block cipher handle
1336 * @tfm: cipher handle to be freed
1337 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001338static inline void crypto_free_blkcipher(struct crypto_blkcipher *tfm)
1339{
1340 crypto_free_tfm(crypto_blkcipher_tfm(tfm));
1341}
1342
Stephan Mueller58284f02014-11-12 05:29:36 +01001343/**
1344 * crypto_has_blkcipher() - Search for the availability of a block cipher
1345 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1346 * block cipher
1347 * @type: specifies the type of the cipher
1348 * @mask: specifies the mask for the cipher
1349 *
1350 * Return: true when the block cipher is known to the kernel crypto API; false
1351 * otherwise
1352 */
Herbert Xufce32d72006-08-26 17:35:45 +10001353static inline int crypto_has_blkcipher(const char *alg_name, u32 type, u32 mask)
1354{
Herbert Xu332f88402007-11-15 22:36:07 +08001355 type &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xufce32d72006-08-26 17:35:45 +10001356 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
Herbert Xu332f88402007-11-15 22:36:07 +08001357 mask |= CRYPTO_ALG_TYPE_MASK;
Herbert Xufce32d72006-08-26 17:35:45 +10001358
1359 return crypto_has_alg(alg_name, type, mask);
1360}
1361
Stephan Mueller58284f02014-11-12 05:29:36 +01001362/**
1363 * crypto_blkcipher_name() - return the name / cra_name from the cipher handle
1364 * @tfm: cipher handle
1365 *
1366 * Return: The character string holding the name of the cipher
1367 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001368static inline const char *crypto_blkcipher_name(struct crypto_blkcipher *tfm)
1369{
1370 return crypto_tfm_alg_name(crypto_blkcipher_tfm(tfm));
1371}
1372
1373static inline struct blkcipher_tfm *crypto_blkcipher_crt(
1374 struct crypto_blkcipher *tfm)
1375{
1376 return &crypto_blkcipher_tfm(tfm)->crt_blkcipher;
1377}
1378
1379static inline struct blkcipher_alg *crypto_blkcipher_alg(
1380 struct crypto_blkcipher *tfm)
1381{
1382 return &crypto_blkcipher_tfm(tfm)->__crt_alg->cra_blkcipher;
1383}
1384
Stephan Mueller58284f02014-11-12 05:29:36 +01001385/**
1386 * crypto_blkcipher_ivsize() - obtain IV size
1387 * @tfm: cipher handle
1388 *
1389 * The size of the IV for the block cipher referenced by the cipher handle is
1390 * returned. This IV size may be zero if the cipher does not need an IV.
1391 *
1392 * Return: IV size in bytes
1393 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001394static inline unsigned int crypto_blkcipher_ivsize(struct crypto_blkcipher *tfm)
1395{
1396 return crypto_blkcipher_alg(tfm)->ivsize;
1397}
1398
Stephan Mueller58284f02014-11-12 05:29:36 +01001399/**
1400 * crypto_blkcipher_blocksize() - obtain block size of cipher
1401 * @tfm: cipher handle
1402 *
1403 * The block size for the block cipher referenced with the cipher handle is
1404 * returned. The caller may use that information to allocate appropriate
1405 * memory for the data returned by the encryption or decryption operation.
1406 *
1407 * Return: block size of cipher
1408 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001409static inline unsigned int crypto_blkcipher_blocksize(
1410 struct crypto_blkcipher *tfm)
1411{
1412 return crypto_tfm_alg_blocksize(crypto_blkcipher_tfm(tfm));
1413}
1414
1415static inline unsigned int crypto_blkcipher_alignmask(
1416 struct crypto_blkcipher *tfm)
1417{
1418 return crypto_tfm_alg_alignmask(crypto_blkcipher_tfm(tfm));
1419}
1420
1421static inline u32 crypto_blkcipher_get_flags(struct crypto_blkcipher *tfm)
1422{
1423 return crypto_tfm_get_flags(crypto_blkcipher_tfm(tfm));
1424}
1425
1426static inline void crypto_blkcipher_set_flags(struct crypto_blkcipher *tfm,
1427 u32 flags)
1428{
1429 crypto_tfm_set_flags(crypto_blkcipher_tfm(tfm), flags);
1430}
1431
1432static inline void crypto_blkcipher_clear_flags(struct crypto_blkcipher *tfm,
1433 u32 flags)
1434{
1435 crypto_tfm_clear_flags(crypto_blkcipher_tfm(tfm), flags);
1436}
1437
Stephan Mueller58284f02014-11-12 05:29:36 +01001438/**
1439 * crypto_blkcipher_setkey() - set key for cipher
1440 * @tfm: cipher handle
1441 * @key: buffer holding the key
1442 * @keylen: length of the key in bytes
1443 *
1444 * The caller provided key is set for the block cipher referenced by the cipher
1445 * handle.
1446 *
1447 * Note, the key length determines the cipher type. Many block ciphers implement
1448 * different cipher modes depending on the key size, such as AES-128 vs AES-192
1449 * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
1450 * is performed.
1451 *
1452 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
1453 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001454static inline int crypto_blkcipher_setkey(struct crypto_blkcipher *tfm,
1455 const u8 *key, unsigned int keylen)
1456{
1457 return crypto_blkcipher_crt(tfm)->setkey(crypto_blkcipher_tfm(tfm),
1458 key, keylen);
1459}
1460
Stephan Mueller58284f02014-11-12 05:29:36 +01001461/**
1462 * crypto_blkcipher_encrypt() - encrypt plaintext
1463 * @desc: reference to the block cipher handle with meta data
1464 * @dst: scatter/gather list that is filled by the cipher operation with the
1465 * ciphertext
1466 * @src: scatter/gather list that holds the plaintext
1467 * @nbytes: number of bytes of the plaintext to encrypt.
1468 *
1469 * Encrypt plaintext data using the IV set by the caller with a preceding
1470 * call of crypto_blkcipher_set_iv.
1471 *
1472 * The blkcipher_desc data structure must be filled by the caller and can
1473 * reside on the stack. The caller must fill desc as follows: desc.tfm is filled
1474 * with the block cipher handle; desc.flags is filled with either
1475 * CRYPTO_TFM_REQ_MAY_SLEEP or 0.
1476 *
1477 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1478 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001479static inline int crypto_blkcipher_encrypt(struct blkcipher_desc *desc,
1480 struct scatterlist *dst,
1481 struct scatterlist *src,
1482 unsigned int nbytes)
1483{
1484 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
1485 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
1486}
1487
Stephan Mueller58284f02014-11-12 05:29:36 +01001488/**
1489 * crypto_blkcipher_encrypt_iv() - encrypt plaintext with dedicated IV
1490 * @desc: reference to the block cipher handle with meta data
1491 * @dst: scatter/gather list that is filled by the cipher operation with the
1492 * ciphertext
1493 * @src: scatter/gather list that holds the plaintext
1494 * @nbytes: number of bytes of the plaintext to encrypt.
1495 *
1496 * Encrypt plaintext data with the use of an IV that is solely used for this
1497 * cipher operation. Any previously set IV is not used.
1498 *
1499 * The blkcipher_desc data structure must be filled by the caller and can
1500 * reside on the stack. The caller must fill desc as follows: desc.tfm is filled
1501 * with the block cipher handle; desc.info is filled with the IV to be used for
1502 * the current operation; desc.flags is filled with either
1503 * CRYPTO_TFM_REQ_MAY_SLEEP or 0.
1504 *
1505 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1506 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001507static inline int crypto_blkcipher_encrypt_iv(struct blkcipher_desc *desc,
1508 struct scatterlist *dst,
1509 struct scatterlist *src,
1510 unsigned int nbytes)
1511{
1512 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
1513}
1514
Stephan Mueller58284f02014-11-12 05:29:36 +01001515/**
1516 * crypto_blkcipher_decrypt() - decrypt ciphertext
1517 * @desc: reference to the block cipher handle with meta data
1518 * @dst: scatter/gather list that is filled by the cipher operation with the
1519 * plaintext
1520 * @src: scatter/gather list that holds the ciphertext
1521 * @nbytes: number of bytes of the ciphertext to decrypt.
1522 *
1523 * Decrypt ciphertext data using the IV set by the caller with a preceding
1524 * call of crypto_blkcipher_set_iv.
1525 *
1526 * The blkcipher_desc data structure must be filled by the caller as documented
1527 * for the crypto_blkcipher_encrypt call above.
1528 *
1529 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1530 *
1531 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001532static inline int crypto_blkcipher_decrypt(struct blkcipher_desc *desc,
1533 struct scatterlist *dst,
1534 struct scatterlist *src,
1535 unsigned int nbytes)
1536{
1537 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
1538 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
1539}
1540
Stephan Mueller58284f02014-11-12 05:29:36 +01001541/**
1542 * crypto_blkcipher_decrypt_iv() - decrypt ciphertext with dedicated IV
1543 * @desc: reference to the block cipher handle with meta data
1544 * @dst: scatter/gather list that is filled by the cipher operation with the
1545 * plaintext
1546 * @src: scatter/gather list that holds the ciphertext
1547 * @nbytes: number of bytes of the ciphertext to decrypt.
1548 *
1549 * Decrypt ciphertext data with the use of an IV that is solely used for this
1550 * cipher operation. Any previously set IV is not used.
1551 *
1552 * The blkcipher_desc data structure must be filled by the caller as documented
1553 * for the crypto_blkcipher_encrypt_iv call above.
1554 *
1555 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1556 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001557static inline int crypto_blkcipher_decrypt_iv(struct blkcipher_desc *desc,
1558 struct scatterlist *dst,
1559 struct scatterlist *src,
1560 unsigned int nbytes)
1561{
1562 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
1563}
1564
Stephan Mueller58284f02014-11-12 05:29:36 +01001565/**
1566 * crypto_blkcipher_set_iv() - set IV for cipher
1567 * @tfm: cipher handle
1568 * @src: buffer holding the IV
1569 * @len: length of the IV in bytes
1570 *
1571 * The caller provided IV is set for the block cipher referenced by the cipher
1572 * handle.
1573 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001574static inline void crypto_blkcipher_set_iv(struct crypto_blkcipher *tfm,
1575 const u8 *src, unsigned int len)
1576{
1577 memcpy(crypto_blkcipher_crt(tfm)->iv, src, len);
1578}
1579
Stephan Mueller58284f02014-11-12 05:29:36 +01001580/**
1581 * crypto_blkcipher_get_iv() - obtain IV from cipher
1582 * @tfm: cipher handle
1583 * @dst: buffer filled with the IV
1584 * @len: length of the buffer dst
1585 *
1586 * The caller can obtain the IV set for the block cipher referenced by the
1587 * cipher handle and store it into the user-provided buffer. If the buffer
1588 * has an insufficient space, the IV is truncated to fit the buffer.
1589 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001590static inline void crypto_blkcipher_get_iv(struct crypto_blkcipher *tfm,
1591 u8 *dst, unsigned int len)
1592{
1593 memcpy(dst, crypto_blkcipher_crt(tfm)->iv, len);
1594}
1595
Stephan Mueller16e61032014-11-12 05:30:06 +01001596/**
1597 * DOC: Single Block Cipher API
1598 *
1599 * The single block cipher API is used with the ciphers of type
1600 * CRYPTO_ALG_TYPE_CIPHER (listed as type "cipher" in /proc/crypto).
1601 *
1602 * Using the single block cipher API calls, operations with the basic cipher
1603 * primitive can be implemented. These cipher primitives exclude any block
1604 * chaining operations including IV handling.
1605 *
1606 * The purpose of this single block cipher API is to support the implementation
1607 * of templates or other concepts that only need to perform the cipher operation
1608 * on one block at a time. Templates invoke the underlying cipher primitive
1609 * block-wise and process either the input or the output data of these cipher
1610 * operations.
1611 */
1612
Herbert Xuf28776a2006-08-13 20:58:18 +10001613static inline struct crypto_cipher *__crypto_cipher_cast(struct crypto_tfm *tfm)
1614{
1615 return (struct crypto_cipher *)tfm;
1616}
1617
1618static inline struct crypto_cipher *crypto_cipher_cast(struct crypto_tfm *tfm)
1619{
1620 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
1621 return __crypto_cipher_cast(tfm);
1622}
1623
Stephan Mueller16e61032014-11-12 05:30:06 +01001624/**
1625 * crypto_alloc_cipher() - allocate single block cipher handle
1626 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1627 * single block cipher
1628 * @type: specifies the type of the cipher
1629 * @mask: specifies the mask for the cipher
1630 *
1631 * Allocate a cipher handle for a single block cipher. The returned struct
1632 * crypto_cipher is the cipher handle that is required for any subsequent API
1633 * invocation for that single block cipher.
1634 *
1635 * Return: allocated cipher handle in case of success; IS_ERR() is true in case
1636 * of an error, PTR_ERR() returns the error code.
1637 */
Herbert Xuf28776a2006-08-13 20:58:18 +10001638static inline struct crypto_cipher *crypto_alloc_cipher(const char *alg_name,
1639 u32 type, u32 mask)
1640{
1641 type &= ~CRYPTO_ALG_TYPE_MASK;
1642 type |= CRYPTO_ALG_TYPE_CIPHER;
1643 mask |= CRYPTO_ALG_TYPE_MASK;
1644
1645 return __crypto_cipher_cast(crypto_alloc_base(alg_name, type, mask));
1646}
1647
1648static inline struct crypto_tfm *crypto_cipher_tfm(struct crypto_cipher *tfm)
1649{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001650 return &tfm->base;
Herbert Xuf28776a2006-08-13 20:58:18 +10001651}
1652
Stephan Mueller16e61032014-11-12 05:30:06 +01001653/**
1654 * crypto_free_cipher() - zeroize and free the single block cipher handle
1655 * @tfm: cipher handle to be freed
1656 */
Herbert Xuf28776a2006-08-13 20:58:18 +10001657static inline void crypto_free_cipher(struct crypto_cipher *tfm)
1658{
1659 crypto_free_tfm(crypto_cipher_tfm(tfm));
1660}
1661
Stephan Mueller16e61032014-11-12 05:30:06 +01001662/**
1663 * crypto_has_cipher() - Search for the availability of a single block cipher
1664 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1665 * single block cipher
1666 * @type: specifies the type of the cipher
1667 * @mask: specifies the mask for the cipher
1668 *
1669 * Return: true when the single block cipher is known to the kernel crypto API;
1670 * false otherwise
1671 */
Herbert Xufce32d72006-08-26 17:35:45 +10001672static inline int crypto_has_cipher(const char *alg_name, u32 type, u32 mask)
1673{
1674 type &= ~CRYPTO_ALG_TYPE_MASK;
1675 type |= CRYPTO_ALG_TYPE_CIPHER;
1676 mask |= CRYPTO_ALG_TYPE_MASK;
1677
1678 return crypto_has_alg(alg_name, type, mask);
1679}
1680
Herbert Xuf28776a2006-08-13 20:58:18 +10001681static inline struct cipher_tfm *crypto_cipher_crt(struct crypto_cipher *tfm)
1682{
1683 return &crypto_cipher_tfm(tfm)->crt_cipher;
1684}
1685
Stephan Mueller16e61032014-11-12 05:30:06 +01001686/**
1687 * crypto_cipher_blocksize() - obtain block size for cipher
1688 * @tfm: cipher handle
1689 *
1690 * The block size for the single block cipher referenced with the cipher handle
1691 * tfm is returned. The caller may use that information to allocate appropriate
1692 * memory for the data returned by the encryption or decryption operation
1693 *
1694 * Return: block size of cipher
1695 */
Herbert Xuf28776a2006-08-13 20:58:18 +10001696static inline unsigned int crypto_cipher_blocksize(struct crypto_cipher *tfm)
1697{
1698 return crypto_tfm_alg_blocksize(crypto_cipher_tfm(tfm));
1699}
1700
1701static inline unsigned int crypto_cipher_alignmask(struct crypto_cipher *tfm)
1702{
1703 return crypto_tfm_alg_alignmask(crypto_cipher_tfm(tfm));
1704}
1705
1706static inline u32 crypto_cipher_get_flags(struct crypto_cipher *tfm)
1707{
1708 return crypto_tfm_get_flags(crypto_cipher_tfm(tfm));
1709}
1710
1711static inline void crypto_cipher_set_flags(struct crypto_cipher *tfm,
1712 u32 flags)
1713{
1714 crypto_tfm_set_flags(crypto_cipher_tfm(tfm), flags);
1715}
1716
1717static inline void crypto_cipher_clear_flags(struct crypto_cipher *tfm,
1718 u32 flags)
1719{
1720 crypto_tfm_clear_flags(crypto_cipher_tfm(tfm), flags);
1721}
1722
Stephan Mueller16e61032014-11-12 05:30:06 +01001723/**
1724 * crypto_cipher_setkey() - set key for cipher
1725 * @tfm: cipher handle
1726 * @key: buffer holding the key
1727 * @keylen: length of the key in bytes
1728 *
1729 * The caller provided key is set for the single block cipher referenced by the
1730 * cipher handle.
1731 *
1732 * Note, the key length determines the cipher type. Many block ciphers implement
1733 * different cipher modes depending on the key size, such as AES-128 vs AES-192
1734 * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
1735 * is performed.
1736 *
1737 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
1738 */
Herbert Xu7226bc872006-08-21 21:40:49 +10001739static inline int crypto_cipher_setkey(struct crypto_cipher *tfm,
1740 const u8 *key, unsigned int keylen)
1741{
1742 return crypto_cipher_crt(tfm)->cit_setkey(crypto_cipher_tfm(tfm),
1743 key, keylen);
1744}
1745
Stephan Mueller16e61032014-11-12 05:30:06 +01001746/**
1747 * crypto_cipher_encrypt_one() - encrypt one block of plaintext
1748 * @tfm: cipher handle
1749 * @dst: points to the buffer that will be filled with the ciphertext
1750 * @src: buffer holding the plaintext to be encrypted
1751 *
1752 * Invoke the encryption operation of one block. The caller must ensure that
1753 * the plaintext and ciphertext buffers are at least one block in size.
1754 */
Herbert Xuf28776a2006-08-13 20:58:18 +10001755static inline void crypto_cipher_encrypt_one(struct crypto_cipher *tfm,
1756 u8 *dst, const u8 *src)
1757{
1758 crypto_cipher_crt(tfm)->cit_encrypt_one(crypto_cipher_tfm(tfm),
1759 dst, src);
1760}
1761
Stephan Mueller16e61032014-11-12 05:30:06 +01001762/**
1763 * crypto_cipher_decrypt_one() - decrypt one block of ciphertext
1764 * @tfm: cipher handle
1765 * @dst: points to the buffer that will be filled with the plaintext
1766 * @src: buffer holding the ciphertext to be decrypted
1767 *
1768 * Invoke the decryption operation of one block. The caller must ensure that
1769 * the plaintext and ciphertext buffers are at least one block in size.
1770 */
Herbert Xuf28776a2006-08-13 20:58:18 +10001771static inline void crypto_cipher_decrypt_one(struct crypto_cipher *tfm,
1772 u8 *dst, const u8 *src)
1773{
1774 crypto_cipher_crt(tfm)->cit_decrypt_one(crypto_cipher_tfm(tfm),
1775 dst, src);
1776}
1777
Herbert Xufce32d72006-08-26 17:35:45 +10001778static inline struct crypto_comp *__crypto_comp_cast(struct crypto_tfm *tfm)
1779{
1780 return (struct crypto_comp *)tfm;
1781}
1782
1783static inline struct crypto_comp *crypto_comp_cast(struct crypto_tfm *tfm)
1784{
1785 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_COMPRESS) &
1786 CRYPTO_ALG_TYPE_MASK);
1787 return __crypto_comp_cast(tfm);
1788}
1789
1790static inline struct crypto_comp *crypto_alloc_comp(const char *alg_name,
1791 u32 type, u32 mask)
1792{
1793 type &= ~CRYPTO_ALG_TYPE_MASK;
1794 type |= CRYPTO_ALG_TYPE_COMPRESS;
1795 mask |= CRYPTO_ALG_TYPE_MASK;
1796
1797 return __crypto_comp_cast(crypto_alloc_base(alg_name, type, mask));
1798}
1799
1800static inline struct crypto_tfm *crypto_comp_tfm(struct crypto_comp *tfm)
1801{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001802 return &tfm->base;
Herbert Xufce32d72006-08-26 17:35:45 +10001803}
1804
1805static inline void crypto_free_comp(struct crypto_comp *tfm)
1806{
1807 crypto_free_tfm(crypto_comp_tfm(tfm));
1808}
1809
1810static inline int crypto_has_comp(const char *alg_name, u32 type, u32 mask)
1811{
1812 type &= ~CRYPTO_ALG_TYPE_MASK;
1813 type |= CRYPTO_ALG_TYPE_COMPRESS;
1814 mask |= CRYPTO_ALG_TYPE_MASK;
1815
1816 return crypto_has_alg(alg_name, type, mask);
1817}
1818
Herbert Xue4d5b792006-08-26 18:12:40 +10001819static inline const char *crypto_comp_name(struct crypto_comp *tfm)
1820{
1821 return crypto_tfm_alg_name(crypto_comp_tfm(tfm));
1822}
1823
Herbert Xufce32d72006-08-26 17:35:45 +10001824static inline struct compress_tfm *crypto_comp_crt(struct crypto_comp *tfm)
1825{
1826 return &crypto_comp_tfm(tfm)->crt_compress;
1827}
1828
1829static inline int crypto_comp_compress(struct crypto_comp *tfm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 const u8 *src, unsigned int slen,
1831 u8 *dst, unsigned int *dlen)
1832{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001833 return crypto_comp_crt(tfm)->cot_compress(crypto_comp_tfm(tfm),
1834 src, slen, dst, dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835}
1836
Herbert Xufce32d72006-08-26 17:35:45 +10001837static inline int crypto_comp_decompress(struct crypto_comp *tfm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 const u8 *src, unsigned int slen,
1839 u8 *dst, unsigned int *dlen)
1840{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001841 return crypto_comp_crt(tfm)->cot_decompress(crypto_comp_tfm(tfm),
1842 src, slen, dst, dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843}
1844
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845#endif /* _LINUX_CRYPTO_H */
1846