blob: 25a4b71d6d1f30b0c6d1703f1cc7175b6c8a5bd8 [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;
Herbert Xu40725182005-07-06 13:51:52 -0700141struct crypto_tfm;
Herbert Xue853c3c2006-08-22 00:06:54 +1000142struct crypto_type;
Herbert Xu5d1d65f2015-05-11 17:48:12 +0800143struct aead_request;
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 Xu5cde0af2006-08-22 00:07:53 +1000178struct blkcipher_desc {
179 struct crypto_blkcipher *tfm;
180 void *info;
181 u32 flags;
182};
183
Herbert Xu40725182005-07-06 13:51:52 -0700184struct cipher_desc {
185 struct crypto_tfm *tfm;
Herbert Xu6c2bb982006-05-16 22:09:29 +1000186 void (*crfn)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Herbert Xu40725182005-07-06 13:51:52 -0700187 unsigned int (*prfn)(const struct cipher_desc *desc, u8 *dst,
188 const u8 *src, unsigned int nbytes);
189 void *info;
190};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Herbert Xu055bcee2006-08-19 22:24:23 +1000192struct hash_desc {
193 struct crypto_hash *tfm;
194 u32 flags;
195};
196
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100197/**
198 * DOC: Block Cipher Algorithm Definitions
199 *
200 * These data structures define modular crypto algorithm implementations,
201 * managed via crypto_register_alg() and crypto_unregister_alg().
202 */
203
204/**
205 * struct ablkcipher_alg - asynchronous block cipher definition
206 * @min_keysize: Minimum key size supported by the transformation. This is the
207 * smallest key length supported by this transformation algorithm.
208 * This must be set to one of the pre-defined values as this is
209 * not hardware specific. Possible values for this field can be
210 * found via git grep "_MIN_KEY_SIZE" include/crypto/
211 * @max_keysize: Maximum key size supported by the transformation. This is the
212 * largest key length supported by this transformation algorithm.
213 * This must be set to one of the pre-defined values as this is
214 * not hardware specific. Possible values for this field can be
215 * found via git grep "_MAX_KEY_SIZE" include/crypto/
216 * @setkey: Set key for the transformation. This function is used to either
217 * program a supplied key into the hardware or store the key in the
218 * transformation context for programming it later. Note that this
219 * function does modify the transformation context. This function can
220 * be called multiple times during the existence of the transformation
221 * object, so one must make sure the key is properly reprogrammed into
222 * the hardware. This function is also responsible for checking the key
223 * length for validity. In case a software fallback was put in place in
224 * the @cra_init call, this function might need to use the fallback if
225 * the algorithm doesn't support all of the key sizes.
226 * @encrypt: Encrypt a scatterlist of blocks. This function is used to encrypt
227 * the supplied scatterlist containing the blocks of data. The crypto
228 * API consumer is responsible for aligning the entries of the
229 * scatterlist properly and making sure the chunks are correctly
230 * sized. In case a software fallback was put in place in the
231 * @cra_init call, this function might need to use the fallback if
232 * the algorithm doesn't support all of the key sizes. In case the
233 * key was stored in transformation context, the key might need to be
234 * re-programmed into the hardware in this function. This function
235 * shall not modify the transformation context, as this function may
236 * be called in parallel with the same transformation object.
237 * @decrypt: Decrypt a single block. This is a reverse counterpart to @encrypt
238 * and the conditions are exactly the same.
239 * @givencrypt: Update the IV for encryption. With this function, a cipher
240 * implementation may provide the function on how to update the IV
241 * for encryption.
242 * @givdecrypt: Update the IV for decryption. This is the reverse of
243 * @givencrypt .
244 * @geniv: The transformation implementation may use an "IV generator" provided
245 * by the kernel crypto API. Several use cases have a predefined
246 * approach how IVs are to be updated. For such use cases, the kernel
247 * crypto API provides ready-to-use implementations that can be
248 * referenced with this variable.
249 * @ivsize: IV size applicable for transformation. The consumer must provide an
250 * IV of exactly that size to perform the encrypt or decrypt operation.
251 *
252 * All fields except @givencrypt , @givdecrypt , @geniv and @ivsize are
253 * mandatory and must be filled.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 */
Herbert Xub5b7f082007-04-16 20:48:54 +1000255struct ablkcipher_alg {
256 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
257 unsigned int keylen);
258 int (*encrypt)(struct ablkcipher_request *req);
259 int (*decrypt)(struct ablkcipher_request *req);
Herbert Xu61da88e2007-12-17 21:51:27 +0800260 int (*givencrypt)(struct skcipher_givcrypt_request *req);
261 int (*givdecrypt)(struct skcipher_givcrypt_request *req);
Herbert Xub5b7f082007-04-16 20:48:54 +1000262
Herbert Xu23508e12007-11-27 21:33:24 +0800263 const char *geniv;
264
Herbert Xub5b7f082007-04-16 20:48:54 +1000265 unsigned int min_keysize;
266 unsigned int max_keysize;
267 unsigned int ivsize;
268};
269
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100270/**
Herbert Xu2d0f2302015-05-21 15:11:02 +0800271 * struct old_aead_alg - AEAD cipher definition
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100272 * @maxauthsize: Set the maximum authentication tag size supported by the
273 * transformation. A transformation may support smaller tag sizes.
274 * As the authentication tag is a message digest to ensure the
275 * integrity of the encrypted data, a consumer typically wants the
276 * largest authentication tag possible as defined by this
277 * variable.
278 * @setauthsize: Set authentication size for the AEAD transformation. This
279 * function is used to specify the consumer requested size of the
280 * authentication tag to be either generated by the transformation
281 * during encryption or the size of the authentication tag to be
282 * supplied during the decryption operation. This function is also
283 * responsible for checking the authentication tag size for
284 * validity.
285 * @setkey: see struct ablkcipher_alg
286 * @encrypt: see struct ablkcipher_alg
287 * @decrypt: see struct ablkcipher_alg
288 * @givencrypt: see struct ablkcipher_alg
289 * @givdecrypt: see struct ablkcipher_alg
290 * @geniv: see struct ablkcipher_alg
291 * @ivsize: see struct ablkcipher_alg
292 *
293 * All fields except @givencrypt , @givdecrypt , @geniv and @ivsize are
294 * mandatory and must be filled.
295 */
Herbert Xu2d0f2302015-05-21 15:11:02 +0800296struct old_aead_alg {
Herbert Xu1ae97822007-08-30 15:36:14 +0800297 int (*setkey)(struct crypto_aead *tfm, const u8 *key,
298 unsigned int keylen);
Herbert Xu7ba683a2007-12-02 18:49:21 +1100299 int (*setauthsize)(struct crypto_aead *tfm, unsigned int authsize);
Herbert Xu1ae97822007-08-30 15:36:14 +0800300 int (*encrypt)(struct aead_request *req);
301 int (*decrypt)(struct aead_request *req);
Herbert Xu743edf52007-12-10 16:18:01 +0800302 int (*givencrypt)(struct aead_givcrypt_request *req);
303 int (*givdecrypt)(struct aead_givcrypt_request *req);
Herbert Xu1ae97822007-08-30 15:36:14 +0800304
Herbert Xu5b6d2d72007-12-12 19:23:36 +0800305 const char *geniv;
306
Herbert Xu1ae97822007-08-30 15:36:14 +0800307 unsigned int ivsize;
Herbert Xu7ba683a2007-12-02 18:49:21 +1100308 unsigned int maxauthsize;
Herbert Xu1ae97822007-08-30 15:36:14 +0800309};
310
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100311/**
312 * struct blkcipher_alg - synchronous block cipher definition
313 * @min_keysize: see struct ablkcipher_alg
314 * @max_keysize: see struct ablkcipher_alg
315 * @setkey: see struct ablkcipher_alg
316 * @encrypt: see struct ablkcipher_alg
317 * @decrypt: see struct ablkcipher_alg
318 * @geniv: see struct ablkcipher_alg
319 * @ivsize: see struct ablkcipher_alg
320 *
321 * All fields except @geniv and @ivsize are mandatory and must be filled.
322 */
Herbert Xu5cde0af2006-08-22 00:07:53 +1000323struct blkcipher_alg {
324 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
325 unsigned int keylen);
326 int (*encrypt)(struct blkcipher_desc *desc,
327 struct scatterlist *dst, struct scatterlist *src,
328 unsigned int nbytes);
329 int (*decrypt)(struct blkcipher_desc *desc,
330 struct scatterlist *dst, struct scatterlist *src,
331 unsigned int nbytes);
332
Herbert Xu23508e12007-11-27 21:33:24 +0800333 const char *geniv;
334
Herbert Xu5cde0af2006-08-22 00:07:53 +1000335 unsigned int min_keysize;
336 unsigned int max_keysize;
337 unsigned int ivsize;
338};
339
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100340/**
341 * struct cipher_alg - single-block symmetric ciphers definition
342 * @cia_min_keysize: Minimum key size supported by the transformation. This is
343 * the smallest key length supported by this transformation
344 * algorithm. This must be set to one of the pre-defined
345 * values as this is not hardware specific. Possible values
346 * for this field can be found via git grep "_MIN_KEY_SIZE"
347 * include/crypto/
348 * @cia_max_keysize: Maximum key size supported by the transformation. This is
349 * the largest key length supported by this transformation
350 * algorithm. This must be set to one of the pre-defined values
351 * as this is not hardware specific. Possible values for this
352 * field can be found via git grep "_MAX_KEY_SIZE"
353 * include/crypto/
354 * @cia_setkey: Set key for the transformation. This function is used to either
355 * program a supplied key into the hardware or store the key in the
356 * transformation context for programming it later. Note that this
357 * function does modify the transformation context. This function
358 * can be called multiple times during the existence of the
359 * transformation object, so one must make sure the key is properly
360 * reprogrammed into the hardware. This function is also
361 * responsible for checking the key length for validity.
362 * @cia_encrypt: Encrypt a single block. This function is used to encrypt a
363 * single block of data, which must be @cra_blocksize big. This
364 * always operates on a full @cra_blocksize and it is not possible
365 * to encrypt a block of smaller size. The supplied buffers must
366 * therefore also be at least of @cra_blocksize size. Both the
367 * input and output buffers are always aligned to @cra_alignmask.
368 * In case either of the input or output buffer supplied by user
369 * of the crypto API is not aligned to @cra_alignmask, the crypto
370 * API will re-align the buffers. The re-alignment means that a
371 * new buffer will be allocated, the data will be copied into the
372 * new buffer, then the processing will happen on the new buffer,
373 * then the data will be copied back into the original buffer and
374 * finally the new buffer will be freed. In case a software
375 * fallback was put in place in the @cra_init call, this function
376 * might need to use the fallback if the algorithm doesn't support
377 * all of the key sizes. In case the key was stored in
378 * transformation context, the key might need to be re-programmed
379 * into the hardware in this function. This function shall not
380 * modify the transformation context, as this function may be
381 * called in parallel with the same transformation object.
382 * @cia_decrypt: Decrypt a single block. This is a reverse counterpart to
383 * @cia_encrypt, and the conditions are exactly the same.
384 *
385 * All fields are mandatory and must be filled.
386 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387struct cipher_alg {
388 unsigned int cia_min_keysize;
389 unsigned int cia_max_keysize;
Herbert Xu6c2bb982006-05-16 22:09:29 +1000390 int (*cia_setkey)(struct crypto_tfm *tfm, const u8 *key,
Herbert Xu560c06a2006-08-13 14:16:39 +1000391 unsigned int keylen);
Herbert Xu6c2bb982006-05-16 22:09:29 +1000392 void (*cia_encrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
393 void (*cia_decrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394};
395
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396struct compress_alg {
Herbert Xu6c2bb982006-05-16 22:09:29 +1000397 int (*coa_compress)(struct crypto_tfm *tfm, const u8 *src,
398 unsigned int slen, u8 *dst, unsigned int *dlen);
399 int (*coa_decompress)(struct crypto_tfm *tfm, const u8 *src,
400 unsigned int slen, u8 *dst, unsigned int *dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401};
402
Neil Horman17f0f4a2008-08-14 22:15:52 +1000403
Herbert Xub5b7f082007-04-16 20:48:54 +1000404#define cra_ablkcipher cra_u.ablkcipher
Herbert Xu1ae97822007-08-30 15:36:14 +0800405#define cra_aead cra_u.aead
Herbert Xu5cde0af2006-08-22 00:07:53 +1000406#define cra_blkcipher cra_u.blkcipher
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407#define cra_cipher cra_u.cipher
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408#define cra_compress cra_u.compress
409
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100410/**
411 * struct crypto_alg - definition of a cryptograpic cipher algorithm
412 * @cra_flags: Flags describing this transformation. See include/linux/crypto.h
413 * CRYPTO_ALG_* flags for the flags which go in here. Those are
414 * used for fine-tuning the description of the transformation
415 * algorithm.
416 * @cra_blocksize: Minimum block size of this transformation. The size in bytes
417 * of the smallest possible unit which can be transformed with
418 * this algorithm. The users must respect this value.
419 * In case of HASH transformation, it is possible for a smaller
420 * block than @cra_blocksize to be passed to the crypto API for
421 * transformation, in case of any other transformation type, an
422 * error will be returned upon any attempt to transform smaller
423 * than @cra_blocksize chunks.
424 * @cra_ctxsize: Size of the operational context of the transformation. This
425 * value informs the kernel crypto API about the memory size
426 * needed to be allocated for the transformation context.
427 * @cra_alignmask: Alignment mask for the input and output data buffer. The data
428 * buffer containing the input data for the algorithm must be
429 * aligned to this alignment mask. The data buffer for the
430 * output data must be aligned to this alignment mask. Note that
431 * the Crypto API will do the re-alignment in software, but
432 * only under special conditions and there is a performance hit.
433 * The re-alignment happens at these occasions for different
434 * @cra_u types: cipher -- For both input data and output data
435 * buffer; ahash -- For output hash destination buf; shash --
436 * For output hash destination buf.
437 * This is needed on hardware which is flawed by design and
438 * cannot pick data from arbitrary addresses.
439 * @cra_priority: Priority of this transformation implementation. In case
440 * multiple transformations with same @cra_name are available to
441 * the Crypto API, the kernel will use the one with highest
442 * @cra_priority.
443 * @cra_name: Generic name (usable by multiple implementations) of the
444 * transformation algorithm. This is the name of the transformation
445 * itself. This field is used by the kernel when looking up the
446 * providers of particular transformation.
447 * @cra_driver_name: Unique name of the transformation provider. This is the
448 * name of the provider of the transformation. This can be any
449 * arbitrary value, but in the usual case, this contains the
450 * name of the chip or provider and the name of the
451 * transformation algorithm.
452 * @cra_type: Type of the cryptographic transformation. This is a pointer to
453 * struct crypto_type, which implements callbacks common for all
Masanari Iida12f7c142015-06-04 00:01:21 +0900454 * transformation types. There are multiple options:
Stephan Mueller0d7f4882014-11-12 05:27:49 +0100455 * &crypto_blkcipher_type, &crypto_ablkcipher_type,
456 * &crypto_ahash_type, &crypto_aead_type, &crypto_rng_type.
457 * This field might be empty. In that case, there are no common
458 * callbacks. This is the case for: cipher, compress, shash.
459 * @cra_u: Callbacks implementing the transformation. This is a union of
460 * multiple structures. Depending on the type of transformation selected
461 * by @cra_type and @cra_flags above, the associated structure must be
462 * filled with callbacks. This field might be empty. This is the case
463 * for ahash, shash.
464 * @cra_init: Initialize the cryptographic transformation object. This function
465 * is used to initialize the cryptographic transformation object.
466 * This function is called only once at the instantiation time, right
467 * after the transformation context was allocated. In case the
468 * cryptographic hardware has some special requirements which need to
469 * be handled by software, this function shall check for the precise
470 * requirement of the transformation and put any software fallbacks
471 * in place.
472 * @cra_exit: Deinitialize the cryptographic transformation object. This is a
473 * counterpart to @cra_init, used to remove various changes set in
474 * @cra_init.
475 * @cra_module: Owner of this transformation implementation. Set to THIS_MODULE
476 * @cra_list: internally used
477 * @cra_users: internally used
478 * @cra_refcnt: internally used
479 * @cra_destroy: internally used
480 *
481 * The struct crypto_alg describes a generic Crypto API algorithm and is common
482 * for all of the transformations. Any variable not documented here shall not
483 * be used by a cipher implementation as it is internal to the Crypto API.
484 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485struct crypto_alg {
486 struct list_head cra_list;
Herbert Xu6bfd4802006-09-21 11:39:29 +1000487 struct list_head cra_users;
488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 u32 cra_flags;
490 unsigned int cra_blocksize;
491 unsigned int cra_ctxsize;
Herbert Xu95477372005-07-06 13:52:09 -0700492 unsigned int cra_alignmask;
Herbert Xu5cb14542005-11-05 16:58:14 +1100493
494 int cra_priority;
Herbert Xu6521f302006-08-06 20:28:44 +1000495 atomic_t cra_refcnt;
Herbert Xu5cb14542005-11-05 16:58:14 +1100496
Herbert Xud913ea02006-05-21 08:45:26 +1000497 char cra_name[CRYPTO_MAX_ALG_NAME];
498 char cra_driver_name[CRYPTO_MAX_ALG_NAME];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
Herbert Xue853c3c2006-08-22 00:06:54 +1000500 const struct crypto_type *cra_type;
501
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 union {
Herbert Xub5b7f082007-04-16 20:48:54 +1000503 struct ablkcipher_alg ablkcipher;
Herbert Xu2d0f2302015-05-21 15:11:02 +0800504 struct old_aead_alg aead;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000505 struct blkcipher_alg blkcipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 struct cipher_alg cipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 struct compress_alg compress;
508 } cra_u;
Herbert Xuc7fc0592006-05-24 13:02:26 +1000509
510 int (*cra_init)(struct crypto_tfm *tfm);
511 void (*cra_exit)(struct crypto_tfm *tfm);
Herbert Xu6521f302006-08-06 20:28:44 +1000512 void (*cra_destroy)(struct crypto_alg *alg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
514 struct module *cra_module;
515};
516
517/*
518 * Algorithm registration interface.
519 */
520int crypto_register_alg(struct crypto_alg *alg);
521int crypto_unregister_alg(struct crypto_alg *alg);
Mark Brown4b004342012-01-17 23:34:26 +0000522int crypto_register_algs(struct crypto_alg *algs, int count);
523int crypto_unregister_algs(struct crypto_alg *algs, int count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
525/*
526 * Algorithm query interface.
527 */
Herbert Xufce32d72006-08-26 17:35:45 +1000528int crypto_has_alg(const char *name, u32 type, u32 mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
530/*
531 * Transforms: user-instantiated objects which encapsulate algorithms
Herbert Xu6d7d6842006-07-30 11:53:01 +1000532 * and core processing logic. Managed via crypto_alloc_*() and
533 * crypto_free_*(), as well as the various helpers below.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
Herbert Xu32e39832007-03-24 14:35:34 +1100536struct ablkcipher_tfm {
537 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
538 unsigned int keylen);
539 int (*encrypt)(struct ablkcipher_request *req);
540 int (*decrypt)(struct ablkcipher_request *req);
Herbert Xu61da88e2007-12-17 21:51:27 +0800541 int (*givencrypt)(struct skcipher_givcrypt_request *req);
542 int (*givdecrypt)(struct skcipher_givcrypt_request *req);
543
Herbert Xuecfc4322007-12-05 21:08:36 +1100544 struct crypto_ablkcipher *base;
545
Herbert Xu32e39832007-03-24 14:35:34 +1100546 unsigned int ivsize;
547 unsigned int reqsize;
548};
549
Herbert Xu5cde0af2006-08-22 00:07:53 +1000550struct blkcipher_tfm {
551 void *iv;
552 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
553 unsigned int keylen);
554 int (*encrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
555 struct scatterlist *src, unsigned int nbytes);
556 int (*decrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
557 struct scatterlist *src, unsigned int nbytes);
558};
559
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560struct cipher_tfm {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 int (*cit_setkey)(struct crypto_tfm *tfm,
562 const u8 *key, unsigned int keylen);
Herbert Xuf28776a2006-08-13 20:58:18 +1000563 void (*cit_encrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
564 void (*cit_decrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565};
566
Herbert Xu055bcee2006-08-19 22:24:23 +1000567struct hash_tfm {
568 int (*init)(struct hash_desc *desc);
569 int (*update)(struct hash_desc *desc,
570 struct scatterlist *sg, unsigned int nsg);
571 int (*final)(struct hash_desc *desc, u8 *out);
572 int (*digest)(struct hash_desc *desc, struct scatterlist *sg,
573 unsigned int nsg, u8 *out);
574 int (*setkey)(struct crypto_hash *tfm, const u8 *key,
575 unsigned int keylen);
Herbert Xu055bcee2006-08-19 22:24:23 +1000576 unsigned int digestsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577};
578
579struct compress_tfm {
580 int (*cot_compress)(struct crypto_tfm *tfm,
581 const u8 *src, unsigned int slen,
582 u8 *dst, unsigned int *dlen);
583 int (*cot_decompress)(struct crypto_tfm *tfm,
584 const u8 *src, unsigned int slen,
585 u8 *dst, unsigned int *dlen);
586};
587
Herbert Xu32e39832007-03-24 14:35:34 +1100588#define crt_ablkcipher crt_u.ablkcipher
Herbert Xu5cde0af2006-08-22 00:07:53 +1000589#define crt_blkcipher crt_u.blkcipher
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590#define crt_cipher crt_u.cipher
Herbert Xu055bcee2006-08-19 22:24:23 +1000591#define crt_hash crt_u.hash
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592#define crt_compress crt_u.compress
593
594struct crypto_tfm {
595
596 u32 crt_flags;
597
598 union {
Herbert Xu32e39832007-03-24 14:35:34 +1100599 struct ablkcipher_tfm ablkcipher;
Herbert Xu5cde0af2006-08-22 00:07:53 +1000600 struct blkcipher_tfm blkcipher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 struct cipher_tfm cipher;
Herbert Xu055bcee2006-08-19 22:24:23 +1000602 struct hash_tfm hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 struct compress_tfm compress;
604 } crt_u;
Herbert Xu4a779482008-09-13 18:19:03 -0700605
606 void (*exit)(struct crypto_tfm *tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
608 struct crypto_alg *__crt_alg;
Herbert Xuf10b7892006-01-25 22:34:01 +1100609
Herbert Xu79911102006-08-21 21:03:52 +1000610 void *__crt_ctx[] CRYPTO_MINALIGN_ATTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611};
612
Herbert Xu32e39832007-03-24 14:35:34 +1100613struct crypto_ablkcipher {
614 struct crypto_tfm base;
615};
616
Herbert Xu5cde0af2006-08-22 00:07:53 +1000617struct crypto_blkcipher {
618 struct crypto_tfm base;
619};
620
Herbert Xu78a1fe42006-12-24 10:02:00 +1100621struct crypto_cipher {
622 struct crypto_tfm base;
623};
624
625struct crypto_comp {
626 struct crypto_tfm base;
627};
628
Herbert Xu055bcee2006-08-19 22:24:23 +1000629struct crypto_hash {
630 struct crypto_tfm base;
631};
632
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000633enum {
634 CRYPTOA_UNSPEC,
635 CRYPTOA_ALG,
Herbert Xuebc610e2007-01-01 18:37:02 +1100636 CRYPTOA_TYPE,
Herbert Xu39e1ee012007-08-29 19:27:26 +0800637 CRYPTOA_U32,
Herbert Xuebc610e2007-01-01 18:37:02 +1100638 __CRYPTOA_MAX,
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000639};
640
Herbert Xuebc610e2007-01-01 18:37:02 +1100641#define CRYPTOA_MAX (__CRYPTOA_MAX - 1)
642
Herbert Xu39e1ee012007-08-29 19:27:26 +0800643/* Maximum number of (rtattr) parameters for each template. */
644#define CRYPTO_MAX_ATTRS 32
645
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000646struct crypto_attr_alg {
647 char name[CRYPTO_MAX_ALG_NAME];
648};
649
Herbert Xuebc610e2007-01-01 18:37:02 +1100650struct crypto_attr_type {
651 u32 type;
652 u32 mask;
653};
654
Herbert Xu39e1ee012007-08-29 19:27:26 +0800655struct crypto_attr_u32 {
656 u32 num;
657};
658
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659/*
660 * Transform user interface.
661 */
662
Herbert Xu6d7d6842006-07-30 11:53:01 +1000663struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask);
Herbert Xu7b2cd922009-02-05 16:48:24 +1100664void crypto_destroy_tfm(void *mem, struct crypto_tfm *tfm);
665
666static inline void crypto_free_tfm(struct crypto_tfm *tfm)
667{
668 return crypto_destroy_tfm(tfm, tfm);
669}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
Herbert Xuda7f0332008-07-31 17:08:25 +0800671int alg_test(const char *driver, const char *alg, u32 type, u32 mask);
672
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673/*
674 * Transform helpers which query the underlying algorithm.
675 */
676static inline const char *crypto_tfm_alg_name(struct crypto_tfm *tfm)
677{
678 return tfm->__crt_alg->cra_name;
679}
680
Michal Ludvigb14cdd62006-07-09 09:02:24 +1000681static inline const char *crypto_tfm_alg_driver_name(struct crypto_tfm *tfm)
682{
683 return tfm->__crt_alg->cra_driver_name;
684}
685
686static inline int crypto_tfm_alg_priority(struct crypto_tfm *tfm)
687{
688 return tfm->__crt_alg->cra_priority;
689}
690
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm)
692{
693 return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK;
694}
695
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm)
697{
698 return tfm->__crt_alg->cra_blocksize;
699}
700
Herbert Xufbdae9f2005-07-06 13:53:29 -0700701static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm *tfm)
702{
703 return tfm->__crt_alg->cra_alignmask;
704}
705
Herbert Xuf28776a2006-08-13 20:58:18 +1000706static inline u32 crypto_tfm_get_flags(struct crypto_tfm *tfm)
707{
708 return tfm->crt_flags;
709}
710
711static inline void crypto_tfm_set_flags(struct crypto_tfm *tfm, u32 flags)
712{
713 tfm->crt_flags |= flags;
714}
715
716static inline void crypto_tfm_clear_flags(struct crypto_tfm *tfm, u32 flags)
717{
718 tfm->crt_flags &= ~flags;
719}
720
Herbert Xu40725182005-07-06 13:51:52 -0700721static inline void *crypto_tfm_ctx(struct crypto_tfm *tfm)
722{
Herbert Xuf10b7892006-01-25 22:34:01 +1100723 return tfm->__crt_ctx;
724}
725
726static inline unsigned int crypto_tfm_ctx_alignment(void)
727{
728 struct crypto_tfm *tfm;
729 return __alignof__(tfm->__crt_ctx);
Herbert Xu40725182005-07-06 13:51:52 -0700730}
731
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732/*
733 * API wrappers.
734 */
Herbert Xu32e39832007-03-24 14:35:34 +1100735static inline struct crypto_ablkcipher *__crypto_ablkcipher_cast(
736 struct crypto_tfm *tfm)
737{
738 return (struct crypto_ablkcipher *)tfm;
739}
740
Herbert Xu378f4f52007-12-17 20:07:31 +0800741static inline u32 crypto_skcipher_type(u32 type)
742{
Herbert Xuecfc4322007-12-05 21:08:36 +1100743 type &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
Herbert Xu378f4f52007-12-17 20:07:31 +0800744 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
745 return type;
746}
747
748static inline u32 crypto_skcipher_mask(u32 mask)
749{
Herbert Xuecfc4322007-12-05 21:08:36 +1100750 mask &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
Herbert Xu378f4f52007-12-17 20:07:31 +0800751 mask |= CRYPTO_ALG_TYPE_BLKCIPHER_MASK;
752 return mask;
753}
754
Stephan Muellerf13ec332014-11-12 05:28:22 +0100755/**
756 * DOC: Asynchronous Block Cipher API
757 *
758 * Asynchronous block cipher API is used with the ciphers of type
759 * CRYPTO_ALG_TYPE_ABLKCIPHER (listed as type "ablkcipher" in /proc/crypto).
760 *
761 * Asynchronous cipher operations imply that the function invocation for a
762 * cipher request returns immediately before the completion of the operation.
763 * The cipher request is scheduled as a separate kernel thread and therefore
764 * load-balanced on the different CPUs via the process scheduler. To allow
765 * the kernel crypto API to inform the caller about the completion of a cipher
766 * request, the caller must provide a callback function. That function is
767 * invoked with the cipher handle when the request completes.
768 *
769 * To support the asynchronous operation, additional information than just the
770 * cipher handle must be supplied to the kernel crypto API. That additional
771 * information is given by filling in the ablkcipher_request data structure.
772 *
773 * For the asynchronous block cipher API, the state is maintained with the tfm
774 * cipher handle. A single tfm can be used across multiple calls and in
775 * parallel. For asynchronous block cipher calls, context data supplied and
776 * only used by the caller can be referenced the request data structure in
777 * addition to the IV used for the cipher request. The maintenance of such
778 * state information would be important for a crypto driver implementer to
779 * have, because when calling the callback function upon completion of the
780 * cipher operation, that callback function may need some information about
781 * which operation just finished if it invoked multiple in parallel. This
782 * state information is unused by the kernel crypto API.
783 */
784
785/**
786 * crypto_alloc_ablkcipher() - allocate asynchronous block cipher handle
787 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
788 * ablkcipher cipher
789 * @type: specifies the type of the cipher
790 * @mask: specifies the mask for the cipher
791 *
792 * Allocate a cipher handle for an ablkcipher. The returned struct
793 * crypto_ablkcipher is the cipher handle that is required for any subsequent
794 * API invocation for that ablkcipher.
795 *
796 * Return: allocated cipher handle in case of success; IS_ERR() is true in case
797 * of an error, PTR_ERR() returns the error code.
798 */
Herbert Xub9c55aa2007-12-04 12:46:48 +1100799struct crypto_ablkcipher *crypto_alloc_ablkcipher(const char *alg_name,
800 u32 type, u32 mask);
Herbert Xu32e39832007-03-24 14:35:34 +1100801
802static inline struct crypto_tfm *crypto_ablkcipher_tfm(
803 struct crypto_ablkcipher *tfm)
804{
805 return &tfm->base;
806}
807
Stephan Muellerf13ec332014-11-12 05:28:22 +0100808/**
809 * crypto_free_ablkcipher() - zeroize and free cipher handle
810 * @tfm: cipher handle to be freed
811 */
Herbert Xu32e39832007-03-24 14:35:34 +1100812static inline void crypto_free_ablkcipher(struct crypto_ablkcipher *tfm)
813{
814 crypto_free_tfm(crypto_ablkcipher_tfm(tfm));
815}
816
Stephan Muellerf13ec332014-11-12 05:28:22 +0100817/**
818 * crypto_has_ablkcipher() - Search for the availability of an ablkcipher.
819 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
820 * ablkcipher
821 * @type: specifies the type of the cipher
822 * @mask: specifies the mask for the cipher
823 *
824 * Return: true when the ablkcipher is known to the kernel crypto API; false
825 * otherwise
826 */
Herbert Xu32e39832007-03-24 14:35:34 +1100827static inline int crypto_has_ablkcipher(const char *alg_name, u32 type,
828 u32 mask)
829{
Herbert Xu378f4f52007-12-17 20:07:31 +0800830 return crypto_has_alg(alg_name, crypto_skcipher_type(type),
831 crypto_skcipher_mask(mask));
Herbert Xu32e39832007-03-24 14:35:34 +1100832}
833
834static inline struct ablkcipher_tfm *crypto_ablkcipher_crt(
835 struct crypto_ablkcipher *tfm)
836{
837 return &crypto_ablkcipher_tfm(tfm)->crt_ablkcipher;
838}
839
Stephan Muellerf13ec332014-11-12 05:28:22 +0100840/**
841 * crypto_ablkcipher_ivsize() - obtain IV size
842 * @tfm: cipher handle
843 *
844 * The size of the IV for the ablkcipher referenced by the cipher handle is
845 * returned. This IV size may be zero if the cipher does not need an IV.
846 *
847 * Return: IV size in bytes
848 */
Herbert Xu32e39832007-03-24 14:35:34 +1100849static inline unsigned int crypto_ablkcipher_ivsize(
850 struct crypto_ablkcipher *tfm)
851{
852 return crypto_ablkcipher_crt(tfm)->ivsize;
853}
854
Stephan Muellerf13ec332014-11-12 05:28:22 +0100855/**
856 * crypto_ablkcipher_blocksize() - obtain block size of cipher
857 * @tfm: cipher handle
858 *
859 * The block size for the ablkcipher referenced with the cipher handle is
860 * returned. The caller may use that information to allocate appropriate
861 * memory for the data returned by the encryption or decryption operation
862 *
863 * Return: block size of cipher
864 */
Herbert Xu32e39832007-03-24 14:35:34 +1100865static inline unsigned int crypto_ablkcipher_blocksize(
866 struct crypto_ablkcipher *tfm)
867{
868 return crypto_tfm_alg_blocksize(crypto_ablkcipher_tfm(tfm));
869}
870
871static inline unsigned int crypto_ablkcipher_alignmask(
872 struct crypto_ablkcipher *tfm)
873{
874 return crypto_tfm_alg_alignmask(crypto_ablkcipher_tfm(tfm));
875}
876
877static inline u32 crypto_ablkcipher_get_flags(struct crypto_ablkcipher *tfm)
878{
879 return crypto_tfm_get_flags(crypto_ablkcipher_tfm(tfm));
880}
881
882static inline void crypto_ablkcipher_set_flags(struct crypto_ablkcipher *tfm,
883 u32 flags)
884{
885 crypto_tfm_set_flags(crypto_ablkcipher_tfm(tfm), flags);
886}
887
888static inline void crypto_ablkcipher_clear_flags(struct crypto_ablkcipher *tfm,
889 u32 flags)
890{
891 crypto_tfm_clear_flags(crypto_ablkcipher_tfm(tfm), flags);
892}
893
Stephan Muellerf13ec332014-11-12 05:28:22 +0100894/**
895 * crypto_ablkcipher_setkey() - set key for cipher
896 * @tfm: cipher handle
897 * @key: buffer holding the key
898 * @keylen: length of the key in bytes
899 *
900 * The caller provided key is set for the ablkcipher referenced by the cipher
901 * handle.
902 *
903 * Note, the key length determines the cipher type. Many block ciphers implement
904 * different cipher modes depending on the key size, such as AES-128 vs AES-192
905 * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
906 * is performed.
907 *
908 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
909 */
Herbert Xu32e39832007-03-24 14:35:34 +1100910static inline int crypto_ablkcipher_setkey(struct crypto_ablkcipher *tfm,
911 const u8 *key, unsigned int keylen)
912{
Herbert Xuecfc4322007-12-05 21:08:36 +1100913 struct ablkcipher_tfm *crt = crypto_ablkcipher_crt(tfm);
914
915 return crt->setkey(crt->base, key, keylen);
Herbert Xu32e39832007-03-24 14:35:34 +1100916}
917
Stephan Muellerf13ec332014-11-12 05:28:22 +0100918/**
919 * crypto_ablkcipher_reqtfm() - obtain cipher handle from request
920 * @req: ablkcipher_request out of which the cipher handle is to be obtained
921 *
922 * Return the crypto_ablkcipher handle when furnishing an ablkcipher_request
923 * data structure.
924 *
925 * Return: crypto_ablkcipher handle
926 */
Herbert Xu32e39832007-03-24 14:35:34 +1100927static inline struct crypto_ablkcipher *crypto_ablkcipher_reqtfm(
928 struct ablkcipher_request *req)
929{
930 return __crypto_ablkcipher_cast(req->base.tfm);
931}
932
Stephan Muellerf13ec332014-11-12 05:28:22 +0100933/**
934 * crypto_ablkcipher_encrypt() - encrypt plaintext
935 * @req: reference to the ablkcipher_request handle that holds all information
936 * needed to perform the cipher operation
937 *
938 * Encrypt plaintext data using the ablkcipher_request handle. That data
939 * structure and how it is filled with data is discussed with the
940 * ablkcipher_request_* functions.
941 *
942 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
943 */
Herbert Xu32e39832007-03-24 14:35:34 +1100944static inline int crypto_ablkcipher_encrypt(struct ablkcipher_request *req)
945{
946 struct ablkcipher_tfm *crt =
947 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
948 return crt->encrypt(req);
949}
950
Stephan Muellerf13ec332014-11-12 05:28:22 +0100951/**
952 * crypto_ablkcipher_decrypt() - decrypt ciphertext
953 * @req: reference to the ablkcipher_request handle that holds all information
954 * needed to perform the cipher operation
955 *
956 * Decrypt ciphertext data using the ablkcipher_request handle. That data
957 * structure and how it is filled with data is discussed with the
958 * ablkcipher_request_* functions.
959 *
960 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
961 */
Herbert Xu32e39832007-03-24 14:35:34 +1100962static inline int crypto_ablkcipher_decrypt(struct ablkcipher_request *req)
963{
964 struct ablkcipher_tfm *crt =
965 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
966 return crt->decrypt(req);
967}
968
Stephan Muellerf13ec332014-11-12 05:28:22 +0100969/**
970 * DOC: Asynchronous Cipher Request Handle
971 *
972 * The ablkcipher_request data structure contains all pointers to data
973 * required for the asynchronous cipher operation. This includes the cipher
974 * handle (which can be used by multiple ablkcipher_request instances), pointer
975 * to plaintext and ciphertext, asynchronous callback function, etc. It acts
976 * as a handle to the ablkcipher_request_* API calls in a similar way as
977 * ablkcipher handle to the crypto_ablkcipher_* API calls.
978 */
979
980/**
981 * crypto_ablkcipher_reqsize() - obtain size of the request data structure
982 * @tfm: cipher handle
983 *
984 * Return: number of bytes
985 */
Herbert Xub16c3a22007-08-29 19:02:04 +0800986static inline unsigned int crypto_ablkcipher_reqsize(
987 struct crypto_ablkcipher *tfm)
Herbert Xu32e39832007-03-24 14:35:34 +1100988{
989 return crypto_ablkcipher_crt(tfm)->reqsize;
990}
991
Stephan Muellerf13ec332014-11-12 05:28:22 +0100992/**
993 * ablkcipher_request_set_tfm() - update cipher handle reference in request
994 * @req: request handle to be modified
995 * @tfm: cipher handle that shall be added to the request handle
996 *
997 * Allow the caller to replace the existing ablkcipher handle in the request
998 * data structure with a different one.
999 */
Herbert Xue196d622007-04-14 16:09:14 +10001000static inline void ablkcipher_request_set_tfm(
1001 struct ablkcipher_request *req, struct crypto_ablkcipher *tfm)
1002{
Herbert Xuecfc4322007-12-05 21:08:36 +11001003 req->base.tfm = crypto_ablkcipher_tfm(crypto_ablkcipher_crt(tfm)->base);
Herbert Xue196d622007-04-14 16:09:14 +10001004}
1005
Herbert Xub5b7f082007-04-16 20:48:54 +10001006static inline struct ablkcipher_request *ablkcipher_request_cast(
1007 struct crypto_async_request *req)
1008{
1009 return container_of(req, struct ablkcipher_request, base);
1010}
1011
Stephan Muellerf13ec332014-11-12 05:28:22 +01001012/**
1013 * ablkcipher_request_alloc() - allocate request data structure
1014 * @tfm: cipher handle to be registered with the request
1015 * @gfp: memory allocation flag that is handed to kmalloc by the API call.
1016 *
1017 * Allocate the request data structure that must be used with the ablkcipher
1018 * encrypt and decrypt API calls. During the allocation, the provided ablkcipher
1019 * handle is registered in the request data structure.
1020 *
1021 * Return: allocated request handle in case of success; IS_ERR() is true in case
1022 * of an error, PTR_ERR() returns the error code.
1023 */
Herbert Xu32e39832007-03-24 14:35:34 +11001024static inline struct ablkcipher_request *ablkcipher_request_alloc(
1025 struct crypto_ablkcipher *tfm, gfp_t gfp)
1026{
1027 struct ablkcipher_request *req;
1028
1029 req = kmalloc(sizeof(struct ablkcipher_request) +
1030 crypto_ablkcipher_reqsize(tfm), gfp);
1031
1032 if (likely(req))
Herbert Xue196d622007-04-14 16:09:14 +10001033 ablkcipher_request_set_tfm(req, tfm);
Herbert Xu32e39832007-03-24 14:35:34 +11001034
1035 return req;
1036}
1037
Stephan Muellerf13ec332014-11-12 05:28:22 +01001038/**
1039 * ablkcipher_request_free() - zeroize and free request data structure
1040 * @req: request data structure cipher handle to be freed
1041 */
Herbert Xu32e39832007-03-24 14:35:34 +11001042static inline void ablkcipher_request_free(struct ablkcipher_request *req)
1043{
Herbert Xuaef73cf2009-07-11 22:22:14 +08001044 kzfree(req);
Herbert Xu32e39832007-03-24 14:35:34 +11001045}
1046
Stephan Muellerf13ec332014-11-12 05:28:22 +01001047/**
1048 * ablkcipher_request_set_callback() - set asynchronous callback function
1049 * @req: request handle
1050 * @flags: specify zero or an ORing of the flags
1051 * CRYPTO_TFM_REQ_MAY_BACKLOG the request queue may back log and
1052 * increase the wait queue beyond the initial maximum size;
1053 * CRYPTO_TFM_REQ_MAY_SLEEP the request processing may sleep
1054 * @compl: callback function pointer to be registered with the request handle
1055 * @data: The data pointer refers to memory that is not used by the kernel
1056 * crypto API, but provided to the callback function for it to use. Here,
1057 * the caller can provide a reference to memory the callback function can
1058 * operate on. As the callback function is invoked asynchronously to the
1059 * related functionality, it may need to access data structures of the
1060 * related functionality which can be referenced using this pointer. The
1061 * callback function can access the memory via the "data" field in the
1062 * crypto_async_request data structure provided to the callback function.
1063 *
1064 * This function allows setting the callback function that is triggered once the
1065 * cipher operation completes.
1066 *
1067 * The callback function is registered with the ablkcipher_request handle and
Stephan Mueller379dcfb2015-01-19 00:13:39 +01001068 * must comply with the following template
Stephan Muellerf13ec332014-11-12 05:28:22 +01001069 *
1070 * void callback_function(struct crypto_async_request *req, int error)
1071 */
Herbert Xu32e39832007-03-24 14:35:34 +11001072static inline void ablkcipher_request_set_callback(
1073 struct ablkcipher_request *req,
Mark Rustad3e3dc252014-07-25 02:53:38 -07001074 u32 flags, crypto_completion_t compl, void *data)
Herbert Xu32e39832007-03-24 14:35:34 +11001075{
Mark Rustad3e3dc252014-07-25 02:53:38 -07001076 req->base.complete = compl;
Herbert Xu32e39832007-03-24 14:35:34 +11001077 req->base.data = data;
1078 req->base.flags = flags;
1079}
1080
Stephan Muellerf13ec332014-11-12 05:28:22 +01001081/**
1082 * ablkcipher_request_set_crypt() - set data buffers
1083 * @req: request handle
1084 * @src: source scatter / gather list
1085 * @dst: destination scatter / gather list
1086 * @nbytes: number of bytes to process from @src
1087 * @iv: IV for the cipher operation which must comply with the IV size defined
1088 * by crypto_ablkcipher_ivsize
1089 *
1090 * This function allows setting of the source data and destination data
1091 * scatter / gather lists.
1092 *
1093 * For encryption, the source is treated as the plaintext and the
1094 * destination is the ciphertext. For a decryption operation, the use is
Stephan Mueller379dcfb2015-01-19 00:13:39 +01001095 * reversed - the source is the ciphertext and the destination is the plaintext.
Stephan Muellerf13ec332014-11-12 05:28:22 +01001096 */
Herbert Xu32e39832007-03-24 14:35:34 +11001097static inline void ablkcipher_request_set_crypt(
1098 struct ablkcipher_request *req,
1099 struct scatterlist *src, struct scatterlist *dst,
1100 unsigned int nbytes, void *iv)
1101{
1102 req->src = src;
1103 req->dst = dst;
1104 req->nbytes = nbytes;
1105 req->info = iv;
1106}
1107
Stephan Muellerfced7b02014-11-12 05:29:00 +01001108/**
Stephan Mueller58284f02014-11-12 05:29:36 +01001109 * DOC: Synchronous Block Cipher API
1110 *
1111 * The synchronous block cipher API is used with the ciphers of type
1112 * CRYPTO_ALG_TYPE_BLKCIPHER (listed as type "blkcipher" in /proc/crypto)
1113 *
1114 * Synchronous calls, have a context in the tfm. But since a single tfm can be
1115 * used in multiple calls and in parallel, this info should not be changeable
1116 * (unless a lock is used). This applies, for example, to the symmetric key.
1117 * However, the IV is changeable, so there is an iv field in blkcipher_tfm
1118 * structure for synchronous blkcipher api. So, its the only state info that can
1119 * be kept for synchronous calls without using a big lock across a tfm.
1120 *
1121 * The block cipher API allows the use of a complete cipher, i.e. a cipher
1122 * consisting of a template (a block chaining mode) and a single block cipher
1123 * primitive (e.g. AES).
1124 *
1125 * The plaintext data buffer and the ciphertext data buffer are pointed to
1126 * by using scatter/gather lists. The cipher operation is performed
1127 * on all segments of the provided scatter/gather lists.
1128 *
1129 * The kernel crypto API supports a cipher operation "in-place" which means that
1130 * the caller may provide the same scatter/gather list for the plaintext and
1131 * cipher text. After the completion of the cipher operation, the plaintext
1132 * data is replaced with the ciphertext data in case of an encryption and vice
1133 * versa for a decryption. The caller must ensure that the scatter/gather lists
1134 * for the output data point to sufficiently large buffers, i.e. multiples of
1135 * the block size of the cipher.
1136 */
1137
Herbert Xu5cde0af2006-08-22 00:07:53 +10001138static inline struct crypto_blkcipher *__crypto_blkcipher_cast(
1139 struct crypto_tfm *tfm)
1140{
1141 return (struct crypto_blkcipher *)tfm;
1142}
1143
1144static inline struct crypto_blkcipher *crypto_blkcipher_cast(
1145 struct crypto_tfm *tfm)
1146{
1147 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_BLKCIPHER);
1148 return __crypto_blkcipher_cast(tfm);
1149}
1150
Stephan Mueller58284f02014-11-12 05:29:36 +01001151/**
1152 * crypto_alloc_blkcipher() - allocate synchronous block cipher handle
1153 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1154 * blkcipher cipher
1155 * @type: specifies the type of the cipher
1156 * @mask: specifies the mask for the cipher
1157 *
1158 * Allocate a cipher handle for a block cipher. The returned struct
1159 * crypto_blkcipher is the cipher handle that is required for any subsequent
1160 * API invocation for that block cipher.
1161 *
1162 * Return: allocated cipher handle in case of success; IS_ERR() is true in case
1163 * of an error, PTR_ERR() returns the error code.
1164 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001165static inline struct crypto_blkcipher *crypto_alloc_blkcipher(
1166 const char *alg_name, u32 type, u32 mask)
1167{
Herbert Xu332f88402007-11-15 22:36:07 +08001168 type &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xu5cde0af2006-08-22 00:07:53 +10001169 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
Herbert Xu332f88402007-11-15 22:36:07 +08001170 mask |= CRYPTO_ALG_TYPE_MASK;
Herbert Xu5cde0af2006-08-22 00:07:53 +10001171
1172 return __crypto_blkcipher_cast(crypto_alloc_base(alg_name, type, mask));
1173}
1174
1175static inline struct crypto_tfm *crypto_blkcipher_tfm(
1176 struct crypto_blkcipher *tfm)
1177{
1178 return &tfm->base;
1179}
1180
Stephan Mueller58284f02014-11-12 05:29:36 +01001181/**
1182 * crypto_free_blkcipher() - zeroize and free the block cipher handle
1183 * @tfm: cipher handle to be freed
1184 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001185static inline void crypto_free_blkcipher(struct crypto_blkcipher *tfm)
1186{
1187 crypto_free_tfm(crypto_blkcipher_tfm(tfm));
1188}
1189
Stephan Mueller58284f02014-11-12 05:29:36 +01001190/**
1191 * crypto_has_blkcipher() - Search for the availability of a block cipher
1192 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1193 * block cipher
1194 * @type: specifies the type of the cipher
1195 * @mask: specifies the mask for the cipher
1196 *
1197 * Return: true when the block cipher is known to the kernel crypto API; false
1198 * otherwise
1199 */
Herbert Xufce32d72006-08-26 17:35:45 +10001200static inline int crypto_has_blkcipher(const char *alg_name, u32 type, u32 mask)
1201{
Herbert Xu332f88402007-11-15 22:36:07 +08001202 type &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xufce32d72006-08-26 17:35:45 +10001203 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
Herbert Xu332f88402007-11-15 22:36:07 +08001204 mask |= CRYPTO_ALG_TYPE_MASK;
Herbert Xufce32d72006-08-26 17:35:45 +10001205
1206 return crypto_has_alg(alg_name, type, mask);
1207}
1208
Stephan Mueller58284f02014-11-12 05:29:36 +01001209/**
1210 * crypto_blkcipher_name() - return the name / cra_name from the cipher handle
1211 * @tfm: cipher handle
1212 *
1213 * Return: The character string holding the name of the cipher
1214 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001215static inline const char *crypto_blkcipher_name(struct crypto_blkcipher *tfm)
1216{
1217 return crypto_tfm_alg_name(crypto_blkcipher_tfm(tfm));
1218}
1219
1220static inline struct blkcipher_tfm *crypto_blkcipher_crt(
1221 struct crypto_blkcipher *tfm)
1222{
1223 return &crypto_blkcipher_tfm(tfm)->crt_blkcipher;
1224}
1225
1226static inline struct blkcipher_alg *crypto_blkcipher_alg(
1227 struct crypto_blkcipher *tfm)
1228{
1229 return &crypto_blkcipher_tfm(tfm)->__crt_alg->cra_blkcipher;
1230}
1231
Stephan Mueller58284f02014-11-12 05:29:36 +01001232/**
1233 * crypto_blkcipher_ivsize() - obtain IV size
1234 * @tfm: cipher handle
1235 *
1236 * The size of the IV for the block cipher referenced by the cipher handle is
1237 * returned. This IV size may be zero if the cipher does not need an IV.
1238 *
1239 * Return: IV size in bytes
1240 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001241static inline unsigned int crypto_blkcipher_ivsize(struct crypto_blkcipher *tfm)
1242{
1243 return crypto_blkcipher_alg(tfm)->ivsize;
1244}
1245
Stephan Mueller58284f02014-11-12 05:29:36 +01001246/**
1247 * crypto_blkcipher_blocksize() - obtain block size of cipher
1248 * @tfm: cipher handle
1249 *
1250 * The block size for the block cipher referenced with the cipher handle is
1251 * returned. The caller may use that information to allocate appropriate
1252 * memory for the data returned by the encryption or decryption operation.
1253 *
1254 * Return: block size of cipher
1255 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001256static inline unsigned int crypto_blkcipher_blocksize(
1257 struct crypto_blkcipher *tfm)
1258{
1259 return crypto_tfm_alg_blocksize(crypto_blkcipher_tfm(tfm));
1260}
1261
1262static inline unsigned int crypto_blkcipher_alignmask(
1263 struct crypto_blkcipher *tfm)
1264{
1265 return crypto_tfm_alg_alignmask(crypto_blkcipher_tfm(tfm));
1266}
1267
1268static inline u32 crypto_blkcipher_get_flags(struct crypto_blkcipher *tfm)
1269{
1270 return crypto_tfm_get_flags(crypto_blkcipher_tfm(tfm));
1271}
1272
1273static inline void crypto_blkcipher_set_flags(struct crypto_blkcipher *tfm,
1274 u32 flags)
1275{
1276 crypto_tfm_set_flags(crypto_blkcipher_tfm(tfm), flags);
1277}
1278
1279static inline void crypto_blkcipher_clear_flags(struct crypto_blkcipher *tfm,
1280 u32 flags)
1281{
1282 crypto_tfm_clear_flags(crypto_blkcipher_tfm(tfm), flags);
1283}
1284
Stephan Mueller58284f02014-11-12 05:29:36 +01001285/**
1286 * crypto_blkcipher_setkey() - set key for cipher
1287 * @tfm: cipher handle
1288 * @key: buffer holding the key
1289 * @keylen: length of the key in bytes
1290 *
1291 * The caller provided key is set for the block cipher referenced by the cipher
1292 * handle.
1293 *
1294 * Note, the key length determines the cipher type. Many block ciphers implement
1295 * different cipher modes depending on the key size, such as AES-128 vs AES-192
1296 * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
1297 * is performed.
1298 *
1299 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
1300 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001301static inline int crypto_blkcipher_setkey(struct crypto_blkcipher *tfm,
1302 const u8 *key, unsigned int keylen)
1303{
1304 return crypto_blkcipher_crt(tfm)->setkey(crypto_blkcipher_tfm(tfm),
1305 key, keylen);
1306}
1307
Stephan Mueller58284f02014-11-12 05:29:36 +01001308/**
1309 * crypto_blkcipher_encrypt() - encrypt plaintext
1310 * @desc: reference to the block cipher handle with meta data
1311 * @dst: scatter/gather list that is filled by the cipher operation with the
1312 * ciphertext
1313 * @src: scatter/gather list that holds the plaintext
1314 * @nbytes: number of bytes of the plaintext to encrypt.
1315 *
1316 * Encrypt plaintext data using the IV set by the caller with a preceding
1317 * call of crypto_blkcipher_set_iv.
1318 *
1319 * The blkcipher_desc data structure must be filled by the caller and can
1320 * reside on the stack. The caller must fill desc as follows: desc.tfm is filled
1321 * with the block cipher handle; desc.flags is filled with either
1322 * CRYPTO_TFM_REQ_MAY_SLEEP or 0.
1323 *
1324 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1325 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001326static inline int crypto_blkcipher_encrypt(struct blkcipher_desc *desc,
1327 struct scatterlist *dst,
1328 struct scatterlist *src,
1329 unsigned int nbytes)
1330{
1331 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
1332 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
1333}
1334
Stephan Mueller58284f02014-11-12 05:29:36 +01001335/**
1336 * crypto_blkcipher_encrypt_iv() - encrypt plaintext with dedicated IV
1337 * @desc: reference to the block cipher handle with meta data
1338 * @dst: scatter/gather list that is filled by the cipher operation with the
1339 * ciphertext
1340 * @src: scatter/gather list that holds the plaintext
1341 * @nbytes: number of bytes of the plaintext to encrypt.
1342 *
1343 * Encrypt plaintext data with the use of an IV that is solely used for this
1344 * cipher operation. Any previously set IV is not used.
1345 *
1346 * The blkcipher_desc data structure must be filled by the caller and can
1347 * reside on the stack. The caller must fill desc as follows: desc.tfm is filled
1348 * with the block cipher handle; desc.info is filled with the IV to be used for
1349 * the current operation; desc.flags is filled with either
1350 * CRYPTO_TFM_REQ_MAY_SLEEP or 0.
1351 *
1352 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1353 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001354static inline int crypto_blkcipher_encrypt_iv(struct blkcipher_desc *desc,
1355 struct scatterlist *dst,
1356 struct scatterlist *src,
1357 unsigned int nbytes)
1358{
1359 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
1360}
1361
Stephan Mueller58284f02014-11-12 05:29:36 +01001362/**
1363 * crypto_blkcipher_decrypt() - decrypt ciphertext
1364 * @desc: reference to the block cipher handle with meta data
1365 * @dst: scatter/gather list that is filled by the cipher operation with the
1366 * plaintext
1367 * @src: scatter/gather list that holds the ciphertext
1368 * @nbytes: number of bytes of the ciphertext to decrypt.
1369 *
1370 * Decrypt ciphertext data using the IV set by the caller with a preceding
1371 * call of crypto_blkcipher_set_iv.
1372 *
1373 * The blkcipher_desc data structure must be filled by the caller as documented
1374 * for the crypto_blkcipher_encrypt call above.
1375 *
1376 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1377 *
1378 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001379static inline int crypto_blkcipher_decrypt(struct blkcipher_desc *desc,
1380 struct scatterlist *dst,
1381 struct scatterlist *src,
1382 unsigned int nbytes)
1383{
1384 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
1385 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
1386}
1387
Stephan Mueller58284f02014-11-12 05:29:36 +01001388/**
1389 * crypto_blkcipher_decrypt_iv() - decrypt ciphertext with dedicated IV
1390 * @desc: reference to the block cipher handle with meta data
1391 * @dst: scatter/gather list that is filled by the cipher operation with the
1392 * plaintext
1393 * @src: scatter/gather list that holds the ciphertext
1394 * @nbytes: number of bytes of the ciphertext to decrypt.
1395 *
1396 * Decrypt ciphertext data with the use of an IV that is solely used for this
1397 * cipher operation. Any previously set IV is not used.
1398 *
1399 * The blkcipher_desc data structure must be filled by the caller as documented
1400 * for the crypto_blkcipher_encrypt_iv call above.
1401 *
1402 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1403 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001404static inline int crypto_blkcipher_decrypt_iv(struct blkcipher_desc *desc,
1405 struct scatterlist *dst,
1406 struct scatterlist *src,
1407 unsigned int nbytes)
1408{
1409 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
1410}
1411
Stephan Mueller58284f02014-11-12 05:29:36 +01001412/**
1413 * crypto_blkcipher_set_iv() - set IV for cipher
1414 * @tfm: cipher handle
1415 * @src: buffer holding the IV
1416 * @len: length of the IV in bytes
1417 *
1418 * The caller provided IV is set for the block cipher referenced by the cipher
1419 * handle.
1420 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001421static inline void crypto_blkcipher_set_iv(struct crypto_blkcipher *tfm,
1422 const u8 *src, unsigned int len)
1423{
1424 memcpy(crypto_blkcipher_crt(tfm)->iv, src, len);
1425}
1426
Stephan Mueller58284f02014-11-12 05:29:36 +01001427/**
1428 * crypto_blkcipher_get_iv() - obtain IV from cipher
1429 * @tfm: cipher handle
1430 * @dst: buffer filled with the IV
1431 * @len: length of the buffer dst
1432 *
1433 * The caller can obtain the IV set for the block cipher referenced by the
1434 * cipher handle and store it into the user-provided buffer. If the buffer
1435 * has an insufficient space, the IV is truncated to fit the buffer.
1436 */
Herbert Xu5cde0af2006-08-22 00:07:53 +10001437static inline void crypto_blkcipher_get_iv(struct crypto_blkcipher *tfm,
1438 u8 *dst, unsigned int len)
1439{
1440 memcpy(dst, crypto_blkcipher_crt(tfm)->iv, len);
1441}
1442
Stephan Mueller16e61032014-11-12 05:30:06 +01001443/**
1444 * DOC: Single Block Cipher API
1445 *
1446 * The single block cipher API is used with the ciphers of type
1447 * CRYPTO_ALG_TYPE_CIPHER (listed as type "cipher" in /proc/crypto).
1448 *
1449 * Using the single block cipher API calls, operations with the basic cipher
1450 * primitive can be implemented. These cipher primitives exclude any block
1451 * chaining operations including IV handling.
1452 *
1453 * The purpose of this single block cipher API is to support the implementation
1454 * of templates or other concepts that only need to perform the cipher operation
1455 * on one block at a time. Templates invoke the underlying cipher primitive
1456 * block-wise and process either the input or the output data of these cipher
1457 * operations.
1458 */
1459
Herbert Xuf28776a2006-08-13 20:58:18 +10001460static inline struct crypto_cipher *__crypto_cipher_cast(struct crypto_tfm *tfm)
1461{
1462 return (struct crypto_cipher *)tfm;
1463}
1464
1465static inline struct crypto_cipher *crypto_cipher_cast(struct crypto_tfm *tfm)
1466{
1467 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
1468 return __crypto_cipher_cast(tfm);
1469}
1470
Stephan Mueller16e61032014-11-12 05:30:06 +01001471/**
1472 * crypto_alloc_cipher() - allocate single block cipher handle
1473 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1474 * single block cipher
1475 * @type: specifies the type of the cipher
1476 * @mask: specifies the mask for the cipher
1477 *
1478 * Allocate a cipher handle for a single block cipher. The returned struct
1479 * crypto_cipher is the cipher handle that is required for any subsequent API
1480 * invocation for that single block cipher.
1481 *
1482 * Return: allocated cipher handle in case of success; IS_ERR() is true in case
1483 * of an error, PTR_ERR() returns the error code.
1484 */
Herbert Xuf28776a2006-08-13 20:58:18 +10001485static inline struct crypto_cipher *crypto_alloc_cipher(const char *alg_name,
1486 u32 type, u32 mask)
1487{
1488 type &= ~CRYPTO_ALG_TYPE_MASK;
1489 type |= CRYPTO_ALG_TYPE_CIPHER;
1490 mask |= CRYPTO_ALG_TYPE_MASK;
1491
1492 return __crypto_cipher_cast(crypto_alloc_base(alg_name, type, mask));
1493}
1494
1495static inline struct crypto_tfm *crypto_cipher_tfm(struct crypto_cipher *tfm)
1496{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001497 return &tfm->base;
Herbert Xuf28776a2006-08-13 20:58:18 +10001498}
1499
Stephan Mueller16e61032014-11-12 05:30:06 +01001500/**
1501 * crypto_free_cipher() - zeroize and free the single block cipher handle
1502 * @tfm: cipher handle to be freed
1503 */
Herbert Xuf28776a2006-08-13 20:58:18 +10001504static inline void crypto_free_cipher(struct crypto_cipher *tfm)
1505{
1506 crypto_free_tfm(crypto_cipher_tfm(tfm));
1507}
1508
Stephan Mueller16e61032014-11-12 05:30:06 +01001509/**
1510 * crypto_has_cipher() - Search for the availability of a single block cipher
1511 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1512 * single block cipher
1513 * @type: specifies the type of the cipher
1514 * @mask: specifies the mask for the cipher
1515 *
1516 * Return: true when the single block cipher is known to the kernel crypto API;
1517 * false otherwise
1518 */
Herbert Xufce32d72006-08-26 17:35:45 +10001519static inline int crypto_has_cipher(const char *alg_name, u32 type, u32 mask)
1520{
1521 type &= ~CRYPTO_ALG_TYPE_MASK;
1522 type |= CRYPTO_ALG_TYPE_CIPHER;
1523 mask |= CRYPTO_ALG_TYPE_MASK;
1524
1525 return crypto_has_alg(alg_name, type, mask);
1526}
1527
Herbert Xuf28776a2006-08-13 20:58:18 +10001528static inline struct cipher_tfm *crypto_cipher_crt(struct crypto_cipher *tfm)
1529{
1530 return &crypto_cipher_tfm(tfm)->crt_cipher;
1531}
1532
Stephan Mueller16e61032014-11-12 05:30:06 +01001533/**
1534 * crypto_cipher_blocksize() - obtain block size for cipher
1535 * @tfm: cipher handle
1536 *
1537 * The block size for the single block cipher referenced with the cipher handle
1538 * tfm is returned. The caller may use that information to allocate appropriate
1539 * memory for the data returned by the encryption or decryption operation
1540 *
1541 * Return: block size of cipher
1542 */
Herbert Xuf28776a2006-08-13 20:58:18 +10001543static inline unsigned int crypto_cipher_blocksize(struct crypto_cipher *tfm)
1544{
1545 return crypto_tfm_alg_blocksize(crypto_cipher_tfm(tfm));
1546}
1547
1548static inline unsigned int crypto_cipher_alignmask(struct crypto_cipher *tfm)
1549{
1550 return crypto_tfm_alg_alignmask(crypto_cipher_tfm(tfm));
1551}
1552
1553static inline u32 crypto_cipher_get_flags(struct crypto_cipher *tfm)
1554{
1555 return crypto_tfm_get_flags(crypto_cipher_tfm(tfm));
1556}
1557
1558static inline void crypto_cipher_set_flags(struct crypto_cipher *tfm,
1559 u32 flags)
1560{
1561 crypto_tfm_set_flags(crypto_cipher_tfm(tfm), flags);
1562}
1563
1564static inline void crypto_cipher_clear_flags(struct crypto_cipher *tfm,
1565 u32 flags)
1566{
1567 crypto_tfm_clear_flags(crypto_cipher_tfm(tfm), flags);
1568}
1569
Stephan Mueller16e61032014-11-12 05:30:06 +01001570/**
1571 * crypto_cipher_setkey() - set key for cipher
1572 * @tfm: cipher handle
1573 * @key: buffer holding the key
1574 * @keylen: length of the key in bytes
1575 *
1576 * The caller provided key is set for the single block cipher referenced by the
1577 * cipher handle.
1578 *
1579 * Note, the key length determines the cipher type. Many block ciphers implement
1580 * different cipher modes depending on the key size, such as AES-128 vs AES-192
1581 * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
1582 * is performed.
1583 *
1584 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
1585 */
Herbert Xu7226bc872006-08-21 21:40:49 +10001586static inline int crypto_cipher_setkey(struct crypto_cipher *tfm,
1587 const u8 *key, unsigned int keylen)
1588{
1589 return crypto_cipher_crt(tfm)->cit_setkey(crypto_cipher_tfm(tfm),
1590 key, keylen);
1591}
1592
Stephan Mueller16e61032014-11-12 05:30:06 +01001593/**
1594 * crypto_cipher_encrypt_one() - encrypt one block of plaintext
1595 * @tfm: cipher handle
1596 * @dst: points to the buffer that will be filled with the ciphertext
1597 * @src: buffer holding the plaintext to be encrypted
1598 *
1599 * Invoke the encryption operation of one block. The caller must ensure that
1600 * the plaintext and ciphertext buffers are at least one block in size.
1601 */
Herbert Xuf28776a2006-08-13 20:58:18 +10001602static inline void crypto_cipher_encrypt_one(struct crypto_cipher *tfm,
1603 u8 *dst, const u8 *src)
1604{
1605 crypto_cipher_crt(tfm)->cit_encrypt_one(crypto_cipher_tfm(tfm),
1606 dst, src);
1607}
1608
Stephan Mueller16e61032014-11-12 05:30:06 +01001609/**
1610 * crypto_cipher_decrypt_one() - decrypt one block of ciphertext
1611 * @tfm: cipher handle
1612 * @dst: points to the buffer that will be filled with the plaintext
1613 * @src: buffer holding the ciphertext to be decrypted
1614 *
1615 * Invoke the decryption operation of one block. The caller must ensure that
1616 * the plaintext and ciphertext buffers are at least one block in size.
1617 */
Herbert Xuf28776a2006-08-13 20:58:18 +10001618static inline void crypto_cipher_decrypt_one(struct crypto_cipher *tfm,
1619 u8 *dst, const u8 *src)
1620{
1621 crypto_cipher_crt(tfm)->cit_decrypt_one(crypto_cipher_tfm(tfm),
1622 dst, src);
1623}
1624
Stephan Mueller47ca5be2014-11-12 05:30:42 +01001625/**
1626 * DOC: Synchronous Message Digest API
1627 *
1628 * The synchronous message digest API is used with the ciphers of type
1629 * CRYPTO_ALG_TYPE_HASH (listed as type "hash" in /proc/crypto)
1630 */
1631
Herbert Xu055bcee2006-08-19 22:24:23 +10001632static inline struct crypto_hash *__crypto_hash_cast(struct crypto_tfm *tfm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633{
Herbert Xu055bcee2006-08-19 22:24:23 +10001634 return (struct crypto_hash *)tfm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635}
1636
Herbert Xu055bcee2006-08-19 22:24:23 +10001637static inline struct crypto_hash *crypto_hash_cast(struct crypto_tfm *tfm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638{
Herbert Xu055bcee2006-08-19 22:24:23 +10001639 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_HASH) &
1640 CRYPTO_ALG_TYPE_HASH_MASK);
1641 return __crypto_hash_cast(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642}
1643
Stephan Mueller47ca5be2014-11-12 05:30:42 +01001644/**
1645 * crypto_alloc_hash() - allocate synchronous message digest handle
1646 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1647 * message digest cipher
1648 * @type: specifies the type of the cipher
1649 * @mask: specifies the mask for the cipher
1650 *
1651 * Allocate a cipher handle for a message digest. The returned struct
1652 * crypto_hash is the cipher handle that is required for any subsequent
1653 * API invocation for that message digest.
1654 *
1655 * Return: allocated cipher handle in case of success; IS_ERR() is true in case
1656 * of an error, PTR_ERR() returns the error code.
1657 */
Herbert Xu055bcee2006-08-19 22:24:23 +10001658static inline struct crypto_hash *crypto_alloc_hash(const char *alg_name,
1659 u32 type, u32 mask)
1660{
1661 type &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xu551a09a2007-12-01 21:47:07 +11001662 mask &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xu055bcee2006-08-19 22:24:23 +10001663 type |= CRYPTO_ALG_TYPE_HASH;
1664 mask |= CRYPTO_ALG_TYPE_HASH_MASK;
1665
1666 return __crypto_hash_cast(crypto_alloc_base(alg_name, type, mask));
1667}
1668
1669static inline struct crypto_tfm *crypto_hash_tfm(struct crypto_hash *tfm)
1670{
1671 return &tfm->base;
1672}
1673
Stephan Mueller47ca5be2014-11-12 05:30:42 +01001674/**
1675 * crypto_free_hash() - zeroize and free message digest handle
1676 * @tfm: cipher handle to be freed
1677 */
Herbert Xu055bcee2006-08-19 22:24:23 +10001678static inline void crypto_free_hash(struct crypto_hash *tfm)
1679{
1680 crypto_free_tfm(crypto_hash_tfm(tfm));
1681}
1682
Stephan Mueller47ca5be2014-11-12 05:30:42 +01001683/**
1684 * crypto_has_hash() - Search for the availability of a message digest
1685 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1686 * message digest cipher
1687 * @type: specifies the type of the cipher
1688 * @mask: specifies the mask for the cipher
1689 *
1690 * Return: true when the message digest cipher is known to the kernel crypto
1691 * API; false otherwise
1692 */
Herbert Xufce32d72006-08-26 17:35:45 +10001693static inline int crypto_has_hash(const char *alg_name, u32 type, u32 mask)
1694{
1695 type &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xu551a09a2007-12-01 21:47:07 +11001696 mask &= ~CRYPTO_ALG_TYPE_MASK;
Herbert Xufce32d72006-08-26 17:35:45 +10001697 type |= CRYPTO_ALG_TYPE_HASH;
1698 mask |= CRYPTO_ALG_TYPE_HASH_MASK;
1699
1700 return crypto_has_alg(alg_name, type, mask);
1701}
1702
Herbert Xu055bcee2006-08-19 22:24:23 +10001703static inline struct hash_tfm *crypto_hash_crt(struct crypto_hash *tfm)
1704{
1705 return &crypto_hash_tfm(tfm)->crt_hash;
1706}
1707
Stephan Mueller47ca5be2014-11-12 05:30:42 +01001708/**
1709 * crypto_hash_blocksize() - obtain block size for message digest
1710 * @tfm: cipher handle
1711 *
1712 * The block size for the message digest cipher referenced with the cipher
1713 * handle is returned.
1714 *
1715 * Return: block size of cipher
1716 */
Herbert Xu055bcee2006-08-19 22:24:23 +10001717static inline unsigned int crypto_hash_blocksize(struct crypto_hash *tfm)
1718{
1719 return crypto_tfm_alg_blocksize(crypto_hash_tfm(tfm));
1720}
1721
1722static inline unsigned int crypto_hash_alignmask(struct crypto_hash *tfm)
1723{
1724 return crypto_tfm_alg_alignmask(crypto_hash_tfm(tfm));
1725}
1726
Stephan Mueller47ca5be2014-11-12 05:30:42 +01001727/**
1728 * crypto_hash_digestsize() - obtain message digest size
1729 * @tfm: cipher handle
1730 *
1731 * The size for the message digest created by the message digest cipher
1732 * referenced with the cipher handle is returned.
1733 *
1734 * Return: message digest size
1735 */
Herbert Xu055bcee2006-08-19 22:24:23 +10001736static inline unsigned int crypto_hash_digestsize(struct crypto_hash *tfm)
1737{
1738 return crypto_hash_crt(tfm)->digestsize;
1739}
1740
1741static inline u32 crypto_hash_get_flags(struct crypto_hash *tfm)
1742{
1743 return crypto_tfm_get_flags(crypto_hash_tfm(tfm));
1744}
1745
1746static inline void crypto_hash_set_flags(struct crypto_hash *tfm, u32 flags)
1747{
1748 crypto_tfm_set_flags(crypto_hash_tfm(tfm), flags);
1749}
1750
1751static inline void crypto_hash_clear_flags(struct crypto_hash *tfm, u32 flags)
1752{
1753 crypto_tfm_clear_flags(crypto_hash_tfm(tfm), flags);
1754}
1755
Stephan Mueller47ca5be2014-11-12 05:30:42 +01001756/**
1757 * crypto_hash_init() - (re)initialize message digest handle
1758 * @desc: cipher request handle that to be filled by caller --
1759 * desc.tfm is filled with the hash cipher handle;
1760 * desc.flags is filled with either CRYPTO_TFM_REQ_MAY_SLEEP or 0.
1761 *
1762 * The call (re-)initializes the message digest referenced by the hash cipher
1763 * request handle. Any potentially existing state created by previous
1764 * operations is discarded.
1765 *
1766 * Return: 0 if the message digest initialization was successful; < 0 if an
1767 * error occurred
1768 */
Herbert Xu055bcee2006-08-19 22:24:23 +10001769static inline int crypto_hash_init(struct hash_desc *desc)
1770{
1771 return crypto_hash_crt(desc->tfm)->init(desc);
1772}
1773
Stephan Mueller47ca5be2014-11-12 05:30:42 +01001774/**
1775 * crypto_hash_update() - add data to message digest for processing
1776 * @desc: cipher request handle
1777 * @sg: scatter / gather list pointing to the data to be added to the message
1778 * digest
1779 * @nbytes: number of bytes to be processed from @sg
1780 *
1781 * Updates the message digest state of the cipher handle pointed to by the
1782 * hash cipher request handle with the input data pointed to by the
1783 * scatter/gather list.
1784 *
1785 * Return: 0 if the message digest update was successful; < 0 if an error
1786 * occurred
1787 */
Herbert Xu055bcee2006-08-19 22:24:23 +10001788static inline int crypto_hash_update(struct hash_desc *desc,
1789 struct scatterlist *sg,
1790 unsigned int nbytes)
1791{
1792 return crypto_hash_crt(desc->tfm)->update(desc, sg, nbytes);
1793}
1794
Stephan Mueller47ca5be2014-11-12 05:30:42 +01001795/**
1796 * crypto_hash_final() - calculate message digest
1797 * @desc: cipher request handle
1798 * @out: message digest output buffer -- The caller must ensure that the out
1799 * buffer has a sufficient size (e.g. by using the crypto_hash_digestsize
1800 * function).
1801 *
1802 * Finalize the message digest operation and create the message digest
1803 * based on all data added to the cipher handle. The message digest is placed
1804 * into the output buffer.
1805 *
1806 * Return: 0 if the message digest creation was successful; < 0 if an error
1807 * occurred
1808 */
Herbert Xu055bcee2006-08-19 22:24:23 +10001809static inline int crypto_hash_final(struct hash_desc *desc, u8 *out)
1810{
1811 return crypto_hash_crt(desc->tfm)->final(desc, out);
1812}
1813
Stephan Mueller47ca5be2014-11-12 05:30:42 +01001814/**
1815 * crypto_hash_digest() - calculate message digest for a buffer
1816 * @desc: see crypto_hash_final()
1817 * @sg: see crypto_hash_update()
1818 * @nbytes: see crypto_hash_update()
1819 * @out: see crypto_hash_final()
1820 *
1821 * This function is a "short-hand" for the function calls of crypto_hash_init,
1822 * crypto_hash_update and crypto_hash_final. The parameters have the same
1823 * meaning as discussed for those separate three functions.
1824 *
1825 * Return: 0 if the message digest creation was successful; < 0 if an error
1826 * occurred
1827 */
Herbert Xu055bcee2006-08-19 22:24:23 +10001828static inline int crypto_hash_digest(struct hash_desc *desc,
1829 struct scatterlist *sg,
1830 unsigned int nbytes, u8 *out)
1831{
1832 return crypto_hash_crt(desc->tfm)->digest(desc, sg, nbytes, out);
1833}
1834
Stephan Mueller47ca5be2014-11-12 05:30:42 +01001835/**
1836 * crypto_hash_setkey() - set key for message digest
1837 * @hash: cipher handle
1838 * @key: buffer holding the key
1839 * @keylen: length of the key in bytes
1840 *
1841 * The caller provided key is set for the message digest cipher. The cipher
1842 * handle must point to a keyed hash in order for this function to succeed.
1843 *
1844 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
1845 */
Herbert Xu055bcee2006-08-19 22:24:23 +10001846static inline int crypto_hash_setkey(struct crypto_hash *hash,
1847 const u8 *key, unsigned int keylen)
1848{
1849 return crypto_hash_crt(hash)->setkey(hash, key, keylen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850}
1851
Herbert Xufce32d72006-08-26 17:35:45 +10001852static inline struct crypto_comp *__crypto_comp_cast(struct crypto_tfm *tfm)
1853{
1854 return (struct crypto_comp *)tfm;
1855}
1856
1857static inline struct crypto_comp *crypto_comp_cast(struct crypto_tfm *tfm)
1858{
1859 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_COMPRESS) &
1860 CRYPTO_ALG_TYPE_MASK);
1861 return __crypto_comp_cast(tfm);
1862}
1863
1864static inline struct crypto_comp *crypto_alloc_comp(const char *alg_name,
1865 u32 type, u32 mask)
1866{
1867 type &= ~CRYPTO_ALG_TYPE_MASK;
1868 type |= CRYPTO_ALG_TYPE_COMPRESS;
1869 mask |= CRYPTO_ALG_TYPE_MASK;
1870
1871 return __crypto_comp_cast(crypto_alloc_base(alg_name, type, mask));
1872}
1873
1874static inline struct crypto_tfm *crypto_comp_tfm(struct crypto_comp *tfm)
1875{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001876 return &tfm->base;
Herbert Xufce32d72006-08-26 17:35:45 +10001877}
1878
1879static inline void crypto_free_comp(struct crypto_comp *tfm)
1880{
1881 crypto_free_tfm(crypto_comp_tfm(tfm));
1882}
1883
1884static inline int crypto_has_comp(const char *alg_name, u32 type, u32 mask)
1885{
1886 type &= ~CRYPTO_ALG_TYPE_MASK;
1887 type |= CRYPTO_ALG_TYPE_COMPRESS;
1888 mask |= CRYPTO_ALG_TYPE_MASK;
1889
1890 return crypto_has_alg(alg_name, type, mask);
1891}
1892
Herbert Xue4d5b792006-08-26 18:12:40 +10001893static inline const char *crypto_comp_name(struct crypto_comp *tfm)
1894{
1895 return crypto_tfm_alg_name(crypto_comp_tfm(tfm));
1896}
1897
Herbert Xufce32d72006-08-26 17:35:45 +10001898static inline struct compress_tfm *crypto_comp_crt(struct crypto_comp *tfm)
1899{
1900 return &crypto_comp_tfm(tfm)->crt_compress;
1901}
1902
1903static inline int crypto_comp_compress(struct crypto_comp *tfm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 const u8 *src, unsigned int slen,
1905 u8 *dst, unsigned int *dlen)
1906{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001907 return crypto_comp_crt(tfm)->cot_compress(crypto_comp_tfm(tfm),
1908 src, slen, dst, dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909}
1910
Herbert Xufce32d72006-08-26 17:35:45 +10001911static inline int crypto_comp_decompress(struct crypto_comp *tfm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 const u8 *src, unsigned int slen,
1913 u8 *dst, unsigned int *dlen)
1914{
Herbert Xu78a1fe42006-12-24 10:02:00 +11001915 return crypto_comp_crt(tfm)->cot_decompress(crypto_comp_tfm(tfm),
1916 src, slen, dst, dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917}
1918
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919#endif /* _LINUX_CRYPTO_H */
1920