1 /*
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)
6 * Copyright (c) 2005 Herbert Xu <herbert@gondor.apana.org.au>
7 *
8 * Portions derived from Cryptoapi, by Alexander Kjeldaas <astor@fast.no>
9 * and Nettle, by Niels Möller.
10 *
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
20 #include <linux/atomic.h>
21 #include <linux/kernel.h>
22 #include <linux/list.h>
23 #include <linux/bug.h>
24 #include <linux/slab.h>
25 #include <linux/string.h>
26 #include <linux/uaccess.h>
27 #include <linux/completion.h>
28
29 /*
30 * 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 /*
43 * Algorithm masks and types.
44 */
45 #define CRYPTO_ALG_TYPE_MASK 0x0000000f
46 #define CRYPTO_ALG_TYPE_CIPHER 0x00000001
47 #define CRYPTO_ALG_TYPE_COMPRESS 0x00000002
48 #define CRYPTO_ALG_TYPE_AEAD 0x00000003
49 #define CRYPTO_ALG_TYPE_BLKCIPHER 0x00000004
50 #define CRYPTO_ALG_TYPE_ABLKCIPHER 0x00000005
51 #define CRYPTO_ALG_TYPE_GIVCIPHER 0x00000006
52 #define CRYPTO_ALG_TYPE_DIGEST 0x00000008
53 #define CRYPTO_ALG_TYPE_HASH 0x00000008
54 #define CRYPTO_ALG_TYPE_SHASH 0x00000009
55 #define CRYPTO_ALG_TYPE_AHASH 0x0000000a
56 #define CRYPTO_ALG_TYPE_RNG 0x0000000c
57 #define CRYPTO_ALG_TYPE_AKCIPHER 0x0000000d
58 #define CRYPTO_ALG_TYPE_PCOMPRESS 0x0000000f
59
60 #define CRYPTO_ALG_TYPE_HASH_MASK 0x0000000e
61 #define CRYPTO_ALG_TYPE_AHASH_MASK 0x0000000c
62 #define CRYPTO_ALG_TYPE_BLKCIPHER_MASK 0x0000000c
63
64 #define CRYPTO_ALG_LARVAL 0x00000010
65 #define CRYPTO_ALG_DEAD 0x00000020
66 #define CRYPTO_ALG_DYING 0x00000040
67 #define CRYPTO_ALG_ASYNC 0x00000080
68
69 /*
70 * Set this bit if and only if the algorithm requires another algorithm of
71 * the same type to handle corner cases.
72 */
73 #define CRYPTO_ALG_NEED_FALLBACK 0x00000100
74
75 /*
76 * This bit is set for symmetric key ciphers that have already been wrapped
77 * with a generic IV generator to prevent them from being wrapped again.
78 */
79 #define CRYPTO_ALG_GENIV 0x00000200
80
81 /*
82 * Set if the algorithm has passed automated run-time testing. Note that
83 * if there is no run-time testing for a given algorithm it is considered
84 * to have passed.
85 */
86
87 #define CRYPTO_ALG_TESTED 0x00000400
88
89 /*
90 * Set if the algorithm is an instance that is build from templates.
91 */
92 #define CRYPTO_ALG_INSTANCE 0x00000800
93
94 /* Set this bit if the algorithm provided is hardware accelerated but
95 * not available to userspace via instruction set or so.
96 */
97 #define CRYPTO_ALG_KERN_DRIVER_ONLY 0x00001000
98
99 /*
100 * Mark a cipher as a service implementation only usable by another
101 * cipher and never by a normal user of the kernel crypto API
102 */
103 #define CRYPTO_ALG_INTERNAL 0x00002000
104
105 /*
106 * Transform masks and values (for crt_flags).
107 */
108 #define CRYPTO_TFM_REQ_MASK 0x000fff00
109 #define CRYPTO_TFM_RES_MASK 0xfff00000
110
111 #define CRYPTO_TFM_REQ_WEAK_KEY 0x00000100
112 #define CRYPTO_TFM_REQ_MAY_SLEEP 0x00000200
113 #define CRYPTO_TFM_REQ_MAY_BACKLOG 0x00000400
114 #define CRYPTO_TFM_RES_WEAK_KEY 0x00100000
115 #define CRYPTO_TFM_RES_BAD_KEY_LEN 0x00200000
116 #define CRYPTO_TFM_RES_BAD_KEY_SCHED 0x00400000
117 #define CRYPTO_TFM_RES_BAD_BLOCK_LEN 0x00800000
118 #define CRYPTO_TFM_RES_BAD_FLAGS 0x01000000
119
120 /*
121 * Miscellaneous stuff.
122 */
123 #define CRYPTO_MAX_ALG_NAME 64
124
125 /*
126 * The macro CRYPTO_MINALIGN_ATTR (along with the void * type in the actual
127 * declaration) is used to ensure that the crypto_tfm context structure is
128 * aligned correctly for the given architecture so that there are no alignment
129 * faults for C data types. In particular, this is required on platforms such
130 * as arm where pointers are 32-bit aligned but there are data types such as
131 * u64 which require 64-bit alignment.
132 */
133 #define CRYPTO_MINALIGN ARCH_KMALLOC_MINALIGN
134
135 #define CRYPTO_MINALIGN_ATTR __attribute__ ((__aligned__(CRYPTO_MINALIGN)))
136
137 struct scatterlist;
138 struct crypto_ablkcipher;
139 struct crypto_async_request;
140 struct crypto_blkcipher;
141 struct crypto_hash;
142 struct crypto_tfm;
143 struct crypto_type;
144 struct skcipher_givcrypt_request;
145
146 typedef void (*crypto_completion_t)(struct crypto_async_request *req, int err);
147
148 /**
149 * DOC: Block Cipher Context Data Structures
150 *
151 * These data structures define the operating context for each block cipher
152 * type.
153 */
154
155 struct crypto_async_request {
156 struct list_head list;
157 crypto_completion_t complete;
158 void *data;
159 struct crypto_tfm *tfm;
160
161 u32 flags;
162 };
163
164 struct ablkcipher_request {
165 struct crypto_async_request base;
166
167 unsigned int nbytes;
168
169 void *info;
170
171 struct scatterlist *src;
172 struct scatterlist *dst;
173
174 void *__ctx[] CRYPTO_MINALIGN_ATTR;
175 };
176
177 struct blkcipher_desc {
178 struct crypto_blkcipher *tfm;
179 void *info;
180 u32 flags;
181 };
182
183 struct cipher_desc {
184 struct crypto_tfm *tfm;
185 void (*crfn)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
186 unsigned int (*prfn)(const struct cipher_desc *desc, u8 *dst,
187 const u8 *src, unsigned int nbytes);
188 void *info;
189 };
190
191 struct hash_desc {
192 struct crypto_hash *tfm;
193 u32 flags;
194 };
195
196 /**
197 * DOC: Block Cipher Algorithm Definitions
198 *
199 * These data structures define modular crypto algorithm implementations,
200 * managed via crypto_register_alg() and crypto_unregister_alg().
201 */
202
203 /**
204 * struct ablkcipher_alg - asynchronous block cipher definition
205 * @min_keysize: Minimum key size supported by the transformation. This is the
206 * smallest 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 "_MIN_KEY_SIZE" include/crypto/
210 * @max_keysize: Maximum key size supported by the transformation. This is the
211 * largest key length supported by this transformation algorithm.
212 * This must be set to one of the pre-defined values as this is
213 * not hardware specific. Possible values for this field can be
214 * found via git grep "_MAX_KEY_SIZE" include/crypto/
215 * @setkey: Set key for the transformation. This function is used to either
216 * program a supplied key into the hardware or store the key in the
217 * transformation context for programming it later. Note that this
218 * function does modify the transformation context. This function can
219 * be called multiple times during the existence of the transformation
220 * object, so one must make sure the key is properly reprogrammed into
221 * the hardware. This function is also responsible for checking the key
222 * length for validity. In case a software fallback was put in place in
223 * the @cra_init call, this function might need to use the fallback if
224 * the algorithm doesn't support all of the key sizes.
225 * @encrypt: Encrypt a scatterlist of blocks. This function is used to encrypt
226 * the supplied scatterlist containing the blocks of data. The crypto
227 * API consumer is responsible for aligning the entries of the
228 * scatterlist properly and making sure the chunks are correctly
229 * sized. In case a software fallback was put in place in the
230 * @cra_init call, this function might need to use the fallback if
231 * the algorithm doesn't support all of the key sizes. In case the
232 * key was stored in transformation context, the key might need to be
233 * re-programmed into the hardware in this function. This function
234 * shall not modify the transformation context, as this function may
235 * be called in parallel with the same transformation object.
236 * @decrypt: Decrypt a single block. This is a reverse counterpart to @encrypt
237 * and the conditions are exactly the same.
238 * @givencrypt: Update the IV for encryption. With this function, a cipher
239 * implementation may provide the function on how to update the IV
240 * for encryption.
241 * @givdecrypt: Update the IV for decryption. This is the reverse of
242 * @givencrypt .
243 * @geniv: The transformation implementation may use an "IV generator" provided
244 * by the kernel crypto API. Several use cases have a predefined
245 * approach how IVs are to be updated. For such use cases, the kernel
246 * crypto API provides ready-to-use implementations that can be
247 * referenced with this variable.
248 * @ivsize: IV size applicable for transformation. The consumer must provide an
249 * IV of exactly that size to perform the encrypt or decrypt operation.
250 *
251 * All fields except @givencrypt , @givdecrypt , @geniv and @ivsize are
252 * mandatory and must be filled.
253 */
254 struct ablkcipher_alg {
255 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
256 unsigned int keylen);
257 int (*encrypt)(struct ablkcipher_request *req);
258 int (*decrypt)(struct ablkcipher_request *req);
259 int (*givencrypt)(struct skcipher_givcrypt_request *req);
260 int (*givdecrypt)(struct skcipher_givcrypt_request *req);
261
262 const char *geniv;
263
264 unsigned int min_keysize;
265 unsigned int max_keysize;
266 unsigned int ivsize;
267 };
268
269 /**
270 * struct blkcipher_alg - synchronous block cipher definition
271 * @min_keysize: see struct ablkcipher_alg
272 * @max_keysize: see struct ablkcipher_alg
273 * @setkey: see struct ablkcipher_alg
274 * @encrypt: see struct ablkcipher_alg
275 * @decrypt: see struct ablkcipher_alg
276 * @geniv: see struct ablkcipher_alg
277 * @ivsize: see struct ablkcipher_alg
278 *
279 * All fields except @geniv and @ivsize are mandatory and must be filled.
280 */
281 struct blkcipher_alg {
282 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
283 unsigned int keylen);
284 int (*encrypt)(struct blkcipher_desc *desc,
285 struct scatterlist *dst, struct scatterlist *src,
286 unsigned int nbytes);
287 int (*decrypt)(struct blkcipher_desc *desc,
288 struct scatterlist *dst, struct scatterlist *src,
289 unsigned int nbytes);
290
291 const char *geniv;
292
293 unsigned int min_keysize;
294 unsigned int max_keysize;
295 unsigned int ivsize;
296 };
297
298 /**
299 * struct cipher_alg - single-block symmetric ciphers definition
300 * @cia_min_keysize: Minimum key size supported by the transformation. This is
301 * the smallest key length supported by this transformation
302 * algorithm. This must be set to one of the pre-defined
303 * values as this is not hardware specific. Possible values
304 * for this field can be found via git grep "_MIN_KEY_SIZE"
305 * include/crypto/
306 * @cia_max_keysize: Maximum key size supported by the transformation. This is
307 * the largest key length supported by this transformation
308 * algorithm. This must be set to one of the pre-defined values
309 * as this is not hardware specific. Possible values for this
310 * field can be found via git grep "_MAX_KEY_SIZE"
311 * include/crypto/
312 * @cia_setkey: Set key for the transformation. This function is used to either
313 * program a supplied key into the hardware or store the key in the
314 * transformation context for programming it later. Note that this
315 * function does modify the transformation context. This function
316 * can be called multiple times during the existence of the
317 * transformation object, so one must make sure the key is properly
318 * reprogrammed into the hardware. This function is also
319 * responsible for checking the key length for validity.
320 * @cia_encrypt: Encrypt a single block. This function is used to encrypt a
321 * single block of data, which must be @cra_blocksize big. This
322 * always operates on a full @cra_blocksize and it is not possible
323 * to encrypt a block of smaller size. The supplied buffers must
324 * therefore also be at least of @cra_blocksize size. Both the
325 * input and output buffers are always aligned to @cra_alignmask.
326 * In case either of the input or output buffer supplied by user
327 * of the crypto API is not aligned to @cra_alignmask, the crypto
328 * API will re-align the buffers. The re-alignment means that a
329 * new buffer will be allocated, the data will be copied into the
330 * new buffer, then the processing will happen on the new buffer,
331 * then the data will be copied back into the original buffer and
332 * finally the new buffer will be freed. In case a software
333 * fallback was put in place in the @cra_init call, this function
334 * might need to use the fallback if the algorithm doesn't support
335 * all of the key sizes. In case the key was stored in
336 * transformation context, the key might need to be re-programmed
337 * into the hardware in this function. This function shall not
338 * modify the transformation context, as this function may be
339 * called in parallel with the same transformation object.
340 * @cia_decrypt: Decrypt a single block. This is a reverse counterpart to
341 * @cia_encrypt, and the conditions are exactly the same.
342 *
343 * All fields are mandatory and must be filled.
344 */
345 struct cipher_alg {
346 unsigned int cia_min_keysize;
347 unsigned int cia_max_keysize;
348 int (*cia_setkey)(struct crypto_tfm *tfm, const u8 *key,
349 unsigned int keylen);
350 void (*cia_encrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
351 void (*cia_decrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
352 };
353
354 struct compress_alg {
355 int (*coa_compress)(struct crypto_tfm *tfm, const u8 *src,
356 unsigned int slen, u8 *dst, unsigned int *dlen);
357 int (*coa_decompress)(struct crypto_tfm *tfm, const u8 *src,
358 unsigned int slen, u8 *dst, unsigned int *dlen);
359 };
360
361
362 #define cra_ablkcipher cra_u.ablkcipher
363 #define cra_blkcipher cra_u.blkcipher
364 #define cra_cipher cra_u.cipher
365 #define cra_compress cra_u.compress
366
367 /**
368 * struct crypto_alg - definition of a cryptograpic cipher algorithm
369 * @cra_flags: Flags describing this transformation. See include/linux/crypto.h
370 * CRYPTO_ALG_* flags for the flags which go in here. Those are
371 * used for fine-tuning the description of the transformation
372 * algorithm.
373 * @cra_blocksize: Minimum block size of this transformation. The size in bytes
374 * of the smallest possible unit which can be transformed with
375 * this algorithm. The users must respect this value.
376 * In case of HASH transformation, it is possible for a smaller
377 * block than @cra_blocksize to be passed to the crypto API for
378 * transformation, in case of any other transformation type, an
379 * error will be returned upon any attempt to transform smaller
380 * than @cra_blocksize chunks.
381 * @cra_ctxsize: Size of the operational context of the transformation. This
382 * value informs the kernel crypto API about the memory size
383 * needed to be allocated for the transformation context.
384 * @cra_alignmask: Alignment mask for the input and output data buffer. The data
385 * buffer containing the input data for the algorithm must be
386 * aligned to this alignment mask. The data buffer for the
387 * output data must be aligned to this alignment mask. Note that
388 * the Crypto API will do the re-alignment in software, but
389 * only under special conditions and there is a performance hit.
390 * The re-alignment happens at these occasions for different
391 * @cra_u types: cipher -- For both input data and output data
392 * buffer; ahash -- For output hash destination buf; shash --
393 * For output hash destination buf.
394 * This is needed on hardware which is flawed by design and
395 * cannot pick data from arbitrary addresses.
396 * @cra_priority: Priority of this transformation implementation. In case
397 * multiple transformations with same @cra_name are available to
398 * the Crypto API, the kernel will use the one with highest
399 * @cra_priority.
400 * @cra_name: Generic name (usable by multiple implementations) of the
401 * transformation algorithm. This is the name of the transformation
402 * itself. This field is used by the kernel when looking up the
403 * providers of particular transformation.
404 * @cra_driver_name: Unique name of the transformation provider. This is the
405 * name of the provider of the transformation. This can be any
406 * arbitrary value, but in the usual case, this contains the
407 * name of the chip or provider and the name of the
408 * transformation algorithm.
409 * @cra_type: Type of the cryptographic transformation. This is a pointer to
410 * struct crypto_type, which implements callbacks common for all
411 * transformation types. There are multiple options:
412 * &crypto_blkcipher_type, &crypto_ablkcipher_type,
413 * &crypto_ahash_type, &crypto_rng_type.
414 * This field might be empty. In that case, there are no common
415 * callbacks. This is the case for: cipher, compress, shash.
416 * @cra_u: Callbacks implementing the transformation. This is a union of
417 * multiple structures. Depending on the type of transformation selected
418 * by @cra_type and @cra_flags above, the associated structure must be
419 * filled with callbacks. This field might be empty. This is the case
420 * for ahash, shash.
421 * @cra_init: Initialize the cryptographic transformation object. This function
422 * is used to initialize the cryptographic transformation object.
423 * This function is called only once at the instantiation time, right
424 * after the transformation context was allocated. In case the
425 * cryptographic hardware has some special requirements which need to
426 * be handled by software, this function shall check for the precise
427 * requirement of the transformation and put any software fallbacks
428 * in place.
429 * @cra_exit: Deinitialize the cryptographic transformation object. This is a
430 * counterpart to @cra_init, used to remove various changes set in
431 * @cra_init.
432 * @cra_module: Owner of this transformation implementation. Set to THIS_MODULE
433 * @cra_list: internally used
434 * @cra_users: internally used
435 * @cra_refcnt: internally used
436 * @cra_destroy: internally used
437 *
438 * The struct crypto_alg describes a generic Crypto API algorithm and is common
439 * for all of the transformations. Any variable not documented here shall not
440 * be used by a cipher implementation as it is internal to the Crypto API.
441 */
442 struct crypto_alg {
443 struct list_head cra_list;
444 struct list_head cra_users;
445
446 u32 cra_flags;
447 unsigned int cra_blocksize;
448 unsigned int cra_ctxsize;
449 unsigned int cra_alignmask;
450
451 int cra_priority;
452 atomic_t cra_refcnt;
453
454 char cra_name[CRYPTO_MAX_ALG_NAME];
455 char cra_driver_name[CRYPTO_MAX_ALG_NAME];
456
457 const struct crypto_type *cra_type;
458
459 union {
460 struct ablkcipher_alg ablkcipher;
461 struct blkcipher_alg blkcipher;
462 struct cipher_alg cipher;
463 struct compress_alg compress;
464 } cra_u;
465
466 int (*cra_init)(struct crypto_tfm *tfm);
467 void (*cra_exit)(struct crypto_tfm *tfm);
468 void (*cra_destroy)(struct crypto_alg *alg);
469
470 struct module *cra_module;
471 } CRYPTO_MINALIGN_ATTR;
472
473 /*
474 * A helper struct for waiting for completion of async crypto ops
475 */
476 struct crypto_wait {
477 struct completion completion;
478 int err;
479 };
480
481 /*
482 * Macro for declaring a crypto op async wait object on stack
483 */
484 #define DECLARE_CRYPTO_WAIT(_wait) \
485 struct crypto_wait _wait = { \
486 COMPLETION_INITIALIZER_ONSTACK((_wait).completion), 0 }
487
488 /*
489 * Async ops completion helper functioons
490 */
491 void crypto_req_done(struct crypto_async_request *req, int err);
492
crypto_wait_req(int err,struct crypto_wait * wait)493 static inline int crypto_wait_req(int err, struct crypto_wait *wait)
494 {
495 switch (err) {
496 case -EINPROGRESS:
497 case -EBUSY:
498 wait_for_completion(&wait->completion);
499 reinit_completion(&wait->completion);
500 err = wait->err;
501 break;
502 };
503
504 return err;
505 }
506
crypto_init_wait(struct crypto_wait * wait)507 static inline void crypto_init_wait(struct crypto_wait *wait)
508 {
509 init_completion(&wait->completion);
510 }
511
512 /*
513 * Algorithm registration interface.
514 */
515 int crypto_register_alg(struct crypto_alg *alg);
516 int crypto_unregister_alg(struct crypto_alg *alg);
517 int crypto_register_algs(struct crypto_alg *algs, int count);
518 int crypto_unregister_algs(struct crypto_alg *algs, int count);
519
520 /*
521 * Algorithm query interface.
522 */
523 int crypto_has_alg(const char *name, u32 type, u32 mask);
524
525 /*
526 * Transforms: user-instantiated objects which encapsulate algorithms
527 * and core processing logic. Managed via crypto_alloc_*() and
528 * crypto_free_*(), as well as the various helpers below.
529 */
530
531 struct ablkcipher_tfm {
532 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
533 unsigned int keylen);
534 int (*encrypt)(struct ablkcipher_request *req);
535 int (*decrypt)(struct ablkcipher_request *req);
536 int (*givencrypt)(struct skcipher_givcrypt_request *req);
537 int (*givdecrypt)(struct skcipher_givcrypt_request *req);
538
539 struct crypto_ablkcipher *base;
540
541 unsigned int ivsize;
542 unsigned int reqsize;
543 };
544
545 struct blkcipher_tfm {
546 void *iv;
547 int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
548 unsigned int keylen);
549 int (*encrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
550 struct scatterlist *src, unsigned int nbytes);
551 int (*decrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
552 struct scatterlist *src, unsigned int nbytes);
553 };
554
555 struct cipher_tfm {
556 int (*cit_setkey)(struct crypto_tfm *tfm,
557 const u8 *key, unsigned int keylen);
558 void (*cit_encrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
559 void (*cit_decrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
560 };
561
562 struct hash_tfm {
563 int (*init)(struct hash_desc *desc);
564 int (*update)(struct hash_desc *desc,
565 struct scatterlist *sg, unsigned int nsg);
566 int (*final)(struct hash_desc *desc, u8 *out);
567 int (*digest)(struct hash_desc *desc, struct scatterlist *sg,
568 unsigned int nsg, u8 *out);
569 int (*setkey)(struct crypto_hash *tfm, const u8 *key,
570 unsigned int keylen);
571 unsigned int digestsize;
572 };
573
574 struct compress_tfm {
575 int (*cot_compress)(struct crypto_tfm *tfm,
576 const u8 *src, unsigned int slen,
577 u8 *dst, unsigned int *dlen);
578 int (*cot_decompress)(struct crypto_tfm *tfm,
579 const u8 *src, unsigned int slen,
580 u8 *dst, unsigned int *dlen);
581 };
582
583 #define crt_ablkcipher crt_u.ablkcipher
584 #define crt_blkcipher crt_u.blkcipher
585 #define crt_cipher crt_u.cipher
586 #define crt_hash crt_u.hash
587 #define crt_compress crt_u.compress
588
589 struct crypto_tfm {
590
591 u32 crt_flags;
592
593 union {
594 struct ablkcipher_tfm ablkcipher;
595 struct blkcipher_tfm blkcipher;
596 struct cipher_tfm cipher;
597 struct hash_tfm hash;
598 struct compress_tfm compress;
599 } crt_u;
600
601 void (*exit)(struct crypto_tfm *tfm);
602
603 struct crypto_alg *__crt_alg;
604
605 void *__crt_ctx[] CRYPTO_MINALIGN_ATTR;
606 };
607
608 struct crypto_ablkcipher {
609 struct crypto_tfm base;
610 };
611
612 struct crypto_blkcipher {
613 struct crypto_tfm base;
614 };
615
616 struct crypto_cipher {
617 struct crypto_tfm base;
618 };
619
620 struct crypto_comp {
621 struct crypto_tfm base;
622 };
623
624 struct crypto_hash {
625 struct crypto_tfm base;
626 };
627
628 enum {
629 CRYPTOA_UNSPEC,
630 CRYPTOA_ALG,
631 CRYPTOA_TYPE,
632 CRYPTOA_U32,
633 __CRYPTOA_MAX,
634 };
635
636 #define CRYPTOA_MAX (__CRYPTOA_MAX - 1)
637
638 /* Maximum number of (rtattr) parameters for each template. */
639 #define CRYPTO_MAX_ATTRS 32
640
641 struct crypto_attr_alg {
642 char name[CRYPTO_MAX_ALG_NAME];
643 };
644
645 struct crypto_attr_type {
646 u32 type;
647 u32 mask;
648 };
649
650 struct crypto_attr_u32 {
651 u32 num;
652 };
653
654 /*
655 * Transform user interface.
656 */
657
658 struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask);
659 void crypto_destroy_tfm(void *mem, struct crypto_tfm *tfm);
660
crypto_free_tfm(struct crypto_tfm * tfm)661 static inline void crypto_free_tfm(struct crypto_tfm *tfm)
662 {
663 return crypto_destroy_tfm(tfm, tfm);
664 }
665
666 int alg_test(const char *driver, const char *alg, u32 type, u32 mask);
667
668 /*
669 * Transform helpers which query the underlying algorithm.
670 */
crypto_tfm_alg_name(struct crypto_tfm * tfm)671 static inline const char *crypto_tfm_alg_name(struct crypto_tfm *tfm)
672 {
673 return tfm->__crt_alg->cra_name;
674 }
675
crypto_tfm_alg_driver_name(struct crypto_tfm * tfm)676 static inline const char *crypto_tfm_alg_driver_name(struct crypto_tfm *tfm)
677 {
678 return tfm->__crt_alg->cra_driver_name;
679 }
680
crypto_tfm_alg_priority(struct crypto_tfm * tfm)681 static inline int crypto_tfm_alg_priority(struct crypto_tfm *tfm)
682 {
683 return tfm->__crt_alg->cra_priority;
684 }
685
crypto_tfm_alg_type(struct crypto_tfm * tfm)686 static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm)
687 {
688 return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK;
689 }
690
crypto_tfm_alg_blocksize(struct crypto_tfm * tfm)691 static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm)
692 {
693 return tfm->__crt_alg->cra_blocksize;
694 }
695
crypto_tfm_alg_alignmask(struct crypto_tfm * tfm)696 static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm *tfm)
697 {
698 return tfm->__crt_alg->cra_alignmask;
699 }
700
crypto_tfm_get_flags(struct crypto_tfm * tfm)701 static inline u32 crypto_tfm_get_flags(struct crypto_tfm *tfm)
702 {
703 return tfm->crt_flags;
704 }
705
crypto_tfm_set_flags(struct crypto_tfm * tfm,u32 flags)706 static inline void crypto_tfm_set_flags(struct crypto_tfm *tfm, u32 flags)
707 {
708 tfm->crt_flags |= flags;
709 }
710
crypto_tfm_clear_flags(struct crypto_tfm * tfm,u32 flags)711 static inline void crypto_tfm_clear_flags(struct crypto_tfm *tfm, u32 flags)
712 {
713 tfm->crt_flags &= ~flags;
714 }
715
crypto_tfm_ctx(struct crypto_tfm * tfm)716 static inline void *crypto_tfm_ctx(struct crypto_tfm *tfm)
717 {
718 return tfm->__crt_ctx;
719 }
720
crypto_tfm_ctx_alignment(void)721 static inline unsigned int crypto_tfm_ctx_alignment(void)
722 {
723 struct crypto_tfm *tfm;
724 return __alignof__(tfm->__crt_ctx);
725 }
726
727 /*
728 * API wrappers.
729 */
__crypto_ablkcipher_cast(struct crypto_tfm * tfm)730 static inline struct crypto_ablkcipher *__crypto_ablkcipher_cast(
731 struct crypto_tfm *tfm)
732 {
733 return (struct crypto_ablkcipher *)tfm;
734 }
735
crypto_skcipher_type(u32 type)736 static inline u32 crypto_skcipher_type(u32 type)
737 {
738 type &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
739 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
740 return type;
741 }
742
crypto_skcipher_mask(u32 mask)743 static inline u32 crypto_skcipher_mask(u32 mask)
744 {
745 mask &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
746 mask |= CRYPTO_ALG_TYPE_BLKCIPHER_MASK;
747 return mask;
748 }
749
750 /**
751 * DOC: Asynchronous Block Cipher API
752 *
753 * Asynchronous block cipher API is used with the ciphers of type
754 * CRYPTO_ALG_TYPE_ABLKCIPHER (listed as type "ablkcipher" in /proc/crypto).
755 *
756 * Asynchronous cipher operations imply that the function invocation for a
757 * cipher request returns immediately before the completion of the operation.
758 * The cipher request is scheduled as a separate kernel thread and therefore
759 * load-balanced on the different CPUs via the process scheduler. To allow
760 * the kernel crypto API to inform the caller about the completion of a cipher
761 * request, the caller must provide a callback function. That function is
762 * invoked with the cipher handle when the request completes.
763 *
764 * To support the asynchronous operation, additional information than just the
765 * cipher handle must be supplied to the kernel crypto API. That additional
766 * information is given by filling in the ablkcipher_request data structure.
767 *
768 * For the asynchronous block cipher API, the state is maintained with the tfm
769 * cipher handle. A single tfm can be used across multiple calls and in
770 * parallel. For asynchronous block cipher calls, context data supplied and
771 * only used by the caller can be referenced the request data structure in
772 * addition to the IV used for the cipher request. The maintenance of such
773 * state information would be important for a crypto driver implementer to
774 * have, because when calling the callback function upon completion of the
775 * cipher operation, that callback function may need some information about
776 * which operation just finished if it invoked multiple in parallel. This
777 * state information is unused by the kernel crypto API.
778 */
779
780 /**
781 * crypto_alloc_ablkcipher() - allocate asynchronous block cipher handle
782 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
783 * ablkcipher cipher
784 * @type: specifies the type of the cipher
785 * @mask: specifies the mask for the cipher
786 *
787 * Allocate a cipher handle for an ablkcipher. The returned struct
788 * crypto_ablkcipher is the cipher handle that is required for any subsequent
789 * API invocation for that ablkcipher.
790 *
791 * Return: allocated cipher handle in case of success; IS_ERR() is true in case
792 * of an error, PTR_ERR() returns the error code.
793 */
794 struct crypto_ablkcipher *crypto_alloc_ablkcipher(const char *alg_name,
795 u32 type, u32 mask);
796
crypto_ablkcipher_tfm(struct crypto_ablkcipher * tfm)797 static inline struct crypto_tfm *crypto_ablkcipher_tfm(
798 struct crypto_ablkcipher *tfm)
799 {
800 return &tfm->base;
801 }
802
803 /**
804 * crypto_free_ablkcipher() - zeroize and free cipher handle
805 * @tfm: cipher handle to be freed
806 */
crypto_free_ablkcipher(struct crypto_ablkcipher * tfm)807 static inline void crypto_free_ablkcipher(struct crypto_ablkcipher *tfm)
808 {
809 crypto_free_tfm(crypto_ablkcipher_tfm(tfm));
810 }
811
812 /**
813 * crypto_has_ablkcipher() - Search for the availability of an ablkcipher.
814 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
815 * ablkcipher
816 * @type: specifies the type of the cipher
817 * @mask: specifies the mask for the cipher
818 *
819 * Return: true when the ablkcipher is known to the kernel crypto API; false
820 * otherwise
821 */
crypto_has_ablkcipher(const char * alg_name,u32 type,u32 mask)822 static inline int crypto_has_ablkcipher(const char *alg_name, u32 type,
823 u32 mask)
824 {
825 return crypto_has_alg(alg_name, crypto_skcipher_type(type),
826 crypto_skcipher_mask(mask));
827 }
828
crypto_ablkcipher_crt(struct crypto_ablkcipher * tfm)829 static inline struct ablkcipher_tfm *crypto_ablkcipher_crt(
830 struct crypto_ablkcipher *tfm)
831 {
832 return &crypto_ablkcipher_tfm(tfm)->crt_ablkcipher;
833 }
834
835 /**
836 * crypto_ablkcipher_ivsize() - obtain IV size
837 * @tfm: cipher handle
838 *
839 * The size of the IV for the ablkcipher referenced by the cipher handle is
840 * returned. This IV size may be zero if the cipher does not need an IV.
841 *
842 * Return: IV size in bytes
843 */
crypto_ablkcipher_ivsize(struct crypto_ablkcipher * tfm)844 static inline unsigned int crypto_ablkcipher_ivsize(
845 struct crypto_ablkcipher *tfm)
846 {
847 return crypto_ablkcipher_crt(tfm)->ivsize;
848 }
849
850 /**
851 * crypto_ablkcipher_blocksize() - obtain block size of cipher
852 * @tfm: cipher handle
853 *
854 * The block size for the ablkcipher referenced with the cipher handle is
855 * returned. The caller may use that information to allocate appropriate
856 * memory for the data returned by the encryption or decryption operation
857 *
858 * Return: block size of cipher
859 */
crypto_ablkcipher_blocksize(struct crypto_ablkcipher * tfm)860 static inline unsigned int crypto_ablkcipher_blocksize(
861 struct crypto_ablkcipher *tfm)
862 {
863 return crypto_tfm_alg_blocksize(crypto_ablkcipher_tfm(tfm));
864 }
865
crypto_ablkcipher_alignmask(struct crypto_ablkcipher * tfm)866 static inline unsigned int crypto_ablkcipher_alignmask(
867 struct crypto_ablkcipher *tfm)
868 {
869 return crypto_tfm_alg_alignmask(crypto_ablkcipher_tfm(tfm));
870 }
871
crypto_ablkcipher_get_flags(struct crypto_ablkcipher * tfm)872 static inline u32 crypto_ablkcipher_get_flags(struct crypto_ablkcipher *tfm)
873 {
874 return crypto_tfm_get_flags(crypto_ablkcipher_tfm(tfm));
875 }
876
crypto_ablkcipher_set_flags(struct crypto_ablkcipher * tfm,u32 flags)877 static inline void crypto_ablkcipher_set_flags(struct crypto_ablkcipher *tfm,
878 u32 flags)
879 {
880 crypto_tfm_set_flags(crypto_ablkcipher_tfm(tfm), flags);
881 }
882
crypto_ablkcipher_clear_flags(struct crypto_ablkcipher * tfm,u32 flags)883 static inline void crypto_ablkcipher_clear_flags(struct crypto_ablkcipher *tfm,
884 u32 flags)
885 {
886 crypto_tfm_clear_flags(crypto_ablkcipher_tfm(tfm), flags);
887 }
888
889 /**
890 * crypto_ablkcipher_setkey() - set key for cipher
891 * @tfm: cipher handle
892 * @key: buffer holding the key
893 * @keylen: length of the key in bytes
894 *
895 * The caller provided key is set for the ablkcipher referenced by the cipher
896 * handle.
897 *
898 * Note, the key length determines the cipher type. Many block ciphers implement
899 * different cipher modes depending on the key size, such as AES-128 vs AES-192
900 * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
901 * is performed.
902 *
903 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
904 */
crypto_ablkcipher_setkey(struct crypto_ablkcipher * tfm,const u8 * key,unsigned int keylen)905 static inline int crypto_ablkcipher_setkey(struct crypto_ablkcipher *tfm,
906 const u8 *key, unsigned int keylen)
907 {
908 struct ablkcipher_tfm *crt = crypto_ablkcipher_crt(tfm);
909
910 return crt->setkey(crt->base, key, keylen);
911 }
912
913 /**
914 * crypto_ablkcipher_reqtfm() - obtain cipher handle from request
915 * @req: ablkcipher_request out of which the cipher handle is to be obtained
916 *
917 * Return the crypto_ablkcipher handle when furnishing an ablkcipher_request
918 * data structure.
919 *
920 * Return: crypto_ablkcipher handle
921 */
crypto_ablkcipher_reqtfm(struct ablkcipher_request * req)922 static inline struct crypto_ablkcipher *crypto_ablkcipher_reqtfm(
923 struct ablkcipher_request *req)
924 {
925 return __crypto_ablkcipher_cast(req->base.tfm);
926 }
927
928 /**
929 * crypto_ablkcipher_encrypt() - encrypt plaintext
930 * @req: reference to the ablkcipher_request handle that holds all information
931 * needed to perform the cipher operation
932 *
933 * Encrypt plaintext data using the ablkcipher_request handle. That data
934 * structure and how it is filled with data is discussed with the
935 * ablkcipher_request_* functions.
936 *
937 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
938 */
crypto_ablkcipher_encrypt(struct ablkcipher_request * req)939 static inline int crypto_ablkcipher_encrypt(struct ablkcipher_request *req)
940 {
941 struct ablkcipher_tfm *crt =
942 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
943 return crt->encrypt(req);
944 }
945
946 /**
947 * crypto_ablkcipher_decrypt() - decrypt ciphertext
948 * @req: reference to the ablkcipher_request handle that holds all information
949 * needed to perform the cipher operation
950 *
951 * Decrypt ciphertext data using the ablkcipher_request handle. That data
952 * structure and how it is filled with data is discussed with the
953 * ablkcipher_request_* functions.
954 *
955 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
956 */
crypto_ablkcipher_decrypt(struct ablkcipher_request * req)957 static inline int crypto_ablkcipher_decrypt(struct ablkcipher_request *req)
958 {
959 struct ablkcipher_tfm *crt =
960 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
961 return crt->decrypt(req);
962 }
963
964 /**
965 * DOC: Asynchronous Cipher Request Handle
966 *
967 * The ablkcipher_request data structure contains all pointers to data
968 * required for the asynchronous cipher operation. This includes the cipher
969 * handle (which can be used by multiple ablkcipher_request instances), pointer
970 * to plaintext and ciphertext, asynchronous callback function, etc. It acts
971 * as a handle to the ablkcipher_request_* API calls in a similar way as
972 * ablkcipher handle to the crypto_ablkcipher_* API calls.
973 */
974
975 /**
976 * crypto_ablkcipher_reqsize() - obtain size of the request data structure
977 * @tfm: cipher handle
978 *
979 * Return: number of bytes
980 */
crypto_ablkcipher_reqsize(struct crypto_ablkcipher * tfm)981 static inline unsigned int crypto_ablkcipher_reqsize(
982 struct crypto_ablkcipher *tfm)
983 {
984 return crypto_ablkcipher_crt(tfm)->reqsize;
985 }
986
987 /**
988 * ablkcipher_request_set_tfm() - update cipher handle reference in request
989 * @req: request handle to be modified
990 * @tfm: cipher handle that shall be added to the request handle
991 *
992 * Allow the caller to replace the existing ablkcipher handle in the request
993 * data structure with a different one.
994 */
ablkcipher_request_set_tfm(struct ablkcipher_request * req,struct crypto_ablkcipher * tfm)995 static inline void ablkcipher_request_set_tfm(
996 struct ablkcipher_request *req, struct crypto_ablkcipher *tfm)
997 {
998 req->base.tfm = crypto_ablkcipher_tfm(crypto_ablkcipher_crt(tfm)->base);
999 }
1000
ablkcipher_request_cast(struct crypto_async_request * req)1001 static inline struct ablkcipher_request *ablkcipher_request_cast(
1002 struct crypto_async_request *req)
1003 {
1004 return container_of(req, struct ablkcipher_request, base);
1005 }
1006
1007 /**
1008 * ablkcipher_request_alloc() - allocate request data structure
1009 * @tfm: cipher handle to be registered with the request
1010 * @gfp: memory allocation flag that is handed to kmalloc by the API call.
1011 *
1012 * Allocate the request data structure that must be used with the ablkcipher
1013 * encrypt and decrypt API calls. During the allocation, the provided ablkcipher
1014 * handle is registered in the request data structure.
1015 *
1016 * Return: allocated request handle in case of success; IS_ERR() is true in case
1017 * of an error, PTR_ERR() returns the error code.
1018 */
ablkcipher_request_alloc(struct crypto_ablkcipher * tfm,gfp_t gfp)1019 static inline struct ablkcipher_request *ablkcipher_request_alloc(
1020 struct crypto_ablkcipher *tfm, gfp_t gfp)
1021 {
1022 struct ablkcipher_request *req;
1023
1024 req = kmalloc(sizeof(struct ablkcipher_request) +
1025 crypto_ablkcipher_reqsize(tfm), gfp);
1026
1027 if (likely(req))
1028 ablkcipher_request_set_tfm(req, tfm);
1029
1030 return req;
1031 }
1032
1033 /**
1034 * ablkcipher_request_free() - zeroize and free request data structure
1035 * @req: request data structure cipher handle to be freed
1036 */
ablkcipher_request_free(struct ablkcipher_request * req)1037 static inline void ablkcipher_request_free(struct ablkcipher_request *req)
1038 {
1039 kzfree(req);
1040 }
1041
1042 /**
1043 * ablkcipher_request_set_callback() - set asynchronous callback function
1044 * @req: request handle
1045 * @flags: specify zero or an ORing of the flags
1046 * CRYPTO_TFM_REQ_MAY_BACKLOG the request queue may back log and
1047 * increase the wait queue beyond the initial maximum size;
1048 * CRYPTO_TFM_REQ_MAY_SLEEP the request processing may sleep
1049 * @compl: callback function pointer to be registered with the request handle
1050 * @data: The data pointer refers to memory that is not used by the kernel
1051 * crypto API, but provided to the callback function for it to use. Here,
1052 * the caller can provide a reference to memory the callback function can
1053 * operate on. As the callback function is invoked asynchronously to the
1054 * related functionality, it may need to access data structures of the
1055 * related functionality which can be referenced using this pointer. The
1056 * callback function can access the memory via the "data" field in the
1057 * crypto_async_request data structure provided to the callback function.
1058 *
1059 * This function allows setting the callback function that is triggered once the
1060 * cipher operation completes.
1061 *
1062 * The callback function is registered with the ablkcipher_request handle and
1063 * must comply with the following template
1064 *
1065 * void callback_function(struct crypto_async_request *req, int error)
1066 */
ablkcipher_request_set_callback(struct ablkcipher_request * req,u32 flags,crypto_completion_t compl,void * data)1067 static inline void ablkcipher_request_set_callback(
1068 struct ablkcipher_request *req,
1069 u32 flags, crypto_completion_t compl, void *data)
1070 {
1071 req->base.complete = compl;
1072 req->base.data = data;
1073 req->base.flags = flags;
1074 }
1075
1076 /**
1077 * ablkcipher_request_set_crypt() - set data buffers
1078 * @req: request handle
1079 * @src: source scatter / gather list
1080 * @dst: destination scatter / gather list
1081 * @nbytes: number of bytes to process from @src
1082 * @iv: IV for the cipher operation which must comply with the IV size defined
1083 * by crypto_ablkcipher_ivsize
1084 *
1085 * This function allows setting of the source data and destination data
1086 * scatter / gather lists.
1087 *
1088 * For encryption, the source is treated as the plaintext and the
1089 * destination is the ciphertext. For a decryption operation, the use is
1090 * reversed - the source is the ciphertext and the destination is the plaintext.
1091 */
ablkcipher_request_set_crypt(struct ablkcipher_request * req,struct scatterlist * src,struct scatterlist * dst,unsigned int nbytes,void * iv)1092 static inline void ablkcipher_request_set_crypt(
1093 struct ablkcipher_request *req,
1094 struct scatterlist *src, struct scatterlist *dst,
1095 unsigned int nbytes, void *iv)
1096 {
1097 req->src = src;
1098 req->dst = dst;
1099 req->nbytes = nbytes;
1100 req->info = iv;
1101 }
1102
1103 /**
1104 * DOC: Synchronous Block Cipher API
1105 *
1106 * The synchronous block cipher API is used with the ciphers of type
1107 * CRYPTO_ALG_TYPE_BLKCIPHER (listed as type "blkcipher" in /proc/crypto)
1108 *
1109 * Synchronous calls, have a context in the tfm. But since a single tfm can be
1110 * used in multiple calls and in parallel, this info should not be changeable
1111 * (unless a lock is used). This applies, for example, to the symmetric key.
1112 * However, the IV is changeable, so there is an iv field in blkcipher_tfm
1113 * structure for synchronous blkcipher api. So, its the only state info that can
1114 * be kept for synchronous calls without using a big lock across a tfm.
1115 *
1116 * The block cipher API allows the use of a complete cipher, i.e. a cipher
1117 * consisting of a template (a block chaining mode) and a single block cipher
1118 * primitive (e.g. AES).
1119 *
1120 * The plaintext data buffer and the ciphertext data buffer are pointed to
1121 * by using scatter/gather lists. The cipher operation is performed
1122 * on all segments of the provided scatter/gather lists.
1123 *
1124 * The kernel crypto API supports a cipher operation "in-place" which means that
1125 * the caller may provide the same scatter/gather list for the plaintext and
1126 * cipher text. After the completion of the cipher operation, the plaintext
1127 * data is replaced with the ciphertext data in case of an encryption and vice
1128 * versa for a decryption. The caller must ensure that the scatter/gather lists
1129 * for the output data point to sufficiently large buffers, i.e. multiples of
1130 * the block size of the cipher.
1131 */
1132
__crypto_blkcipher_cast(struct crypto_tfm * tfm)1133 static inline struct crypto_blkcipher *__crypto_blkcipher_cast(
1134 struct crypto_tfm *tfm)
1135 {
1136 return (struct crypto_blkcipher *)tfm;
1137 }
1138
crypto_blkcipher_cast(struct crypto_tfm * tfm)1139 static inline struct crypto_blkcipher *crypto_blkcipher_cast(
1140 struct crypto_tfm *tfm)
1141 {
1142 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_BLKCIPHER);
1143 return __crypto_blkcipher_cast(tfm);
1144 }
1145
1146 /**
1147 * crypto_alloc_blkcipher() - allocate synchronous block cipher handle
1148 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1149 * blkcipher cipher
1150 * @type: specifies the type of the cipher
1151 * @mask: specifies the mask for the cipher
1152 *
1153 * Allocate a cipher handle for a block cipher. The returned struct
1154 * crypto_blkcipher is the cipher handle that is required for any subsequent
1155 * API invocation for that block cipher.
1156 *
1157 * Return: allocated cipher handle in case of success; IS_ERR() is true in case
1158 * of an error, PTR_ERR() returns the error code.
1159 */
crypto_alloc_blkcipher(const char * alg_name,u32 type,u32 mask)1160 static inline struct crypto_blkcipher *crypto_alloc_blkcipher(
1161 const char *alg_name, u32 type, u32 mask)
1162 {
1163 type &= ~CRYPTO_ALG_TYPE_MASK;
1164 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
1165 mask |= CRYPTO_ALG_TYPE_MASK;
1166
1167 return __crypto_blkcipher_cast(crypto_alloc_base(alg_name, type, mask));
1168 }
1169
crypto_blkcipher_tfm(struct crypto_blkcipher * tfm)1170 static inline struct crypto_tfm *crypto_blkcipher_tfm(
1171 struct crypto_blkcipher *tfm)
1172 {
1173 return &tfm->base;
1174 }
1175
1176 /**
1177 * crypto_free_blkcipher() - zeroize and free the block cipher handle
1178 * @tfm: cipher handle to be freed
1179 */
crypto_free_blkcipher(struct crypto_blkcipher * tfm)1180 static inline void crypto_free_blkcipher(struct crypto_blkcipher *tfm)
1181 {
1182 crypto_free_tfm(crypto_blkcipher_tfm(tfm));
1183 }
1184
1185 /**
1186 * crypto_has_blkcipher() - Search for the availability of a block cipher
1187 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1188 * block cipher
1189 * @type: specifies the type of the cipher
1190 * @mask: specifies the mask for the cipher
1191 *
1192 * Return: true when the block cipher is known to the kernel crypto API; false
1193 * otherwise
1194 */
crypto_has_blkcipher(const char * alg_name,u32 type,u32 mask)1195 static inline int crypto_has_blkcipher(const char *alg_name, u32 type, u32 mask)
1196 {
1197 type &= ~CRYPTO_ALG_TYPE_MASK;
1198 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
1199 mask |= CRYPTO_ALG_TYPE_MASK;
1200
1201 return crypto_has_alg(alg_name, type, mask);
1202 }
1203
1204 /**
1205 * crypto_blkcipher_name() - return the name / cra_name from the cipher handle
1206 * @tfm: cipher handle
1207 *
1208 * Return: The character string holding the name of the cipher
1209 */
crypto_blkcipher_name(struct crypto_blkcipher * tfm)1210 static inline const char *crypto_blkcipher_name(struct crypto_blkcipher *tfm)
1211 {
1212 return crypto_tfm_alg_name(crypto_blkcipher_tfm(tfm));
1213 }
1214
crypto_blkcipher_crt(struct crypto_blkcipher * tfm)1215 static inline struct blkcipher_tfm *crypto_blkcipher_crt(
1216 struct crypto_blkcipher *tfm)
1217 {
1218 return &crypto_blkcipher_tfm(tfm)->crt_blkcipher;
1219 }
1220
crypto_blkcipher_alg(struct crypto_blkcipher * tfm)1221 static inline struct blkcipher_alg *crypto_blkcipher_alg(
1222 struct crypto_blkcipher *tfm)
1223 {
1224 return &crypto_blkcipher_tfm(tfm)->__crt_alg->cra_blkcipher;
1225 }
1226
1227 /**
1228 * crypto_blkcipher_ivsize() - obtain IV size
1229 * @tfm: cipher handle
1230 *
1231 * The size of the IV for the block cipher referenced by the cipher handle is
1232 * returned. This IV size may be zero if the cipher does not need an IV.
1233 *
1234 * Return: IV size in bytes
1235 */
crypto_blkcipher_ivsize(struct crypto_blkcipher * tfm)1236 static inline unsigned int crypto_blkcipher_ivsize(struct crypto_blkcipher *tfm)
1237 {
1238 return crypto_blkcipher_alg(tfm)->ivsize;
1239 }
1240
1241 /**
1242 * crypto_blkcipher_blocksize() - obtain block size of cipher
1243 * @tfm: cipher handle
1244 *
1245 * The block size for the block cipher referenced with the cipher handle is
1246 * returned. The caller may use that information to allocate appropriate
1247 * memory for the data returned by the encryption or decryption operation.
1248 *
1249 * Return: block size of cipher
1250 */
crypto_blkcipher_blocksize(struct crypto_blkcipher * tfm)1251 static inline unsigned int crypto_blkcipher_blocksize(
1252 struct crypto_blkcipher *tfm)
1253 {
1254 return crypto_tfm_alg_blocksize(crypto_blkcipher_tfm(tfm));
1255 }
1256
crypto_blkcipher_alignmask(struct crypto_blkcipher * tfm)1257 static inline unsigned int crypto_blkcipher_alignmask(
1258 struct crypto_blkcipher *tfm)
1259 {
1260 return crypto_tfm_alg_alignmask(crypto_blkcipher_tfm(tfm));
1261 }
1262
crypto_blkcipher_get_flags(struct crypto_blkcipher * tfm)1263 static inline u32 crypto_blkcipher_get_flags(struct crypto_blkcipher *tfm)
1264 {
1265 return crypto_tfm_get_flags(crypto_blkcipher_tfm(tfm));
1266 }
1267
crypto_blkcipher_set_flags(struct crypto_blkcipher * tfm,u32 flags)1268 static inline void crypto_blkcipher_set_flags(struct crypto_blkcipher *tfm,
1269 u32 flags)
1270 {
1271 crypto_tfm_set_flags(crypto_blkcipher_tfm(tfm), flags);
1272 }
1273
crypto_blkcipher_clear_flags(struct crypto_blkcipher * tfm,u32 flags)1274 static inline void crypto_blkcipher_clear_flags(struct crypto_blkcipher *tfm,
1275 u32 flags)
1276 {
1277 crypto_tfm_clear_flags(crypto_blkcipher_tfm(tfm), flags);
1278 }
1279
1280 /**
1281 * crypto_blkcipher_setkey() - set key for cipher
1282 * @tfm: cipher handle
1283 * @key: buffer holding the key
1284 * @keylen: length of the key in bytes
1285 *
1286 * The caller provided key is set for the block cipher referenced by the cipher
1287 * handle.
1288 *
1289 * Note, the key length determines the cipher type. Many block ciphers implement
1290 * different cipher modes depending on the key size, such as AES-128 vs AES-192
1291 * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
1292 * is performed.
1293 *
1294 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
1295 */
crypto_blkcipher_setkey(struct crypto_blkcipher * tfm,const u8 * key,unsigned int keylen)1296 static inline int crypto_blkcipher_setkey(struct crypto_blkcipher *tfm,
1297 const u8 *key, unsigned int keylen)
1298 {
1299 return crypto_blkcipher_crt(tfm)->setkey(crypto_blkcipher_tfm(tfm),
1300 key, keylen);
1301 }
1302
1303 /**
1304 * crypto_blkcipher_encrypt() - encrypt plaintext
1305 * @desc: reference to the block cipher handle with meta data
1306 * @dst: scatter/gather list that is filled by the cipher operation with the
1307 * ciphertext
1308 * @src: scatter/gather list that holds the plaintext
1309 * @nbytes: number of bytes of the plaintext to encrypt.
1310 *
1311 * Encrypt plaintext data using the IV set by the caller with a preceding
1312 * call of crypto_blkcipher_set_iv.
1313 *
1314 * The blkcipher_desc data structure must be filled by the caller and can
1315 * reside on the stack. The caller must fill desc as follows: desc.tfm is filled
1316 * with the block cipher handle; desc.flags is filled with either
1317 * CRYPTO_TFM_REQ_MAY_SLEEP or 0.
1318 *
1319 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1320 */
crypto_blkcipher_encrypt(struct blkcipher_desc * desc,struct scatterlist * dst,struct scatterlist * src,unsigned int nbytes)1321 static inline int crypto_blkcipher_encrypt(struct blkcipher_desc *desc,
1322 struct scatterlist *dst,
1323 struct scatterlist *src,
1324 unsigned int nbytes)
1325 {
1326 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
1327 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
1328 }
1329
1330 /**
1331 * crypto_blkcipher_encrypt_iv() - encrypt plaintext with dedicated IV
1332 * @desc: reference to the block cipher handle with meta data
1333 * @dst: scatter/gather list that is filled by the cipher operation with the
1334 * ciphertext
1335 * @src: scatter/gather list that holds the plaintext
1336 * @nbytes: number of bytes of the plaintext to encrypt.
1337 *
1338 * Encrypt plaintext data with the use of an IV that is solely used for this
1339 * cipher operation. Any previously set IV is not used.
1340 *
1341 * The blkcipher_desc data structure must be filled by the caller and can
1342 * reside on the stack. The caller must fill desc as follows: desc.tfm is filled
1343 * with the block cipher handle; desc.info is filled with the IV to be used for
1344 * the current operation; desc.flags is filled with either
1345 * CRYPTO_TFM_REQ_MAY_SLEEP or 0.
1346 *
1347 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1348 */
crypto_blkcipher_encrypt_iv(struct blkcipher_desc * desc,struct scatterlist * dst,struct scatterlist * src,unsigned int nbytes)1349 static inline int crypto_blkcipher_encrypt_iv(struct blkcipher_desc *desc,
1350 struct scatterlist *dst,
1351 struct scatterlist *src,
1352 unsigned int nbytes)
1353 {
1354 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
1355 }
1356
1357 /**
1358 * crypto_blkcipher_decrypt() - decrypt ciphertext
1359 * @desc: reference to the block cipher handle with meta data
1360 * @dst: scatter/gather list that is filled by the cipher operation with the
1361 * plaintext
1362 * @src: scatter/gather list that holds the ciphertext
1363 * @nbytes: number of bytes of the ciphertext to decrypt.
1364 *
1365 * Decrypt ciphertext data using the IV set by the caller with a preceding
1366 * call of crypto_blkcipher_set_iv.
1367 *
1368 * The blkcipher_desc data structure must be filled by the caller as documented
1369 * for the crypto_blkcipher_encrypt call above.
1370 *
1371 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1372 *
1373 */
crypto_blkcipher_decrypt(struct blkcipher_desc * desc,struct scatterlist * dst,struct scatterlist * src,unsigned int nbytes)1374 static inline int crypto_blkcipher_decrypt(struct blkcipher_desc *desc,
1375 struct scatterlist *dst,
1376 struct scatterlist *src,
1377 unsigned int nbytes)
1378 {
1379 desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
1380 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
1381 }
1382
1383 /**
1384 * crypto_blkcipher_decrypt_iv() - decrypt ciphertext with dedicated IV
1385 * @desc: reference to the block cipher handle with meta data
1386 * @dst: scatter/gather list that is filled by the cipher operation with the
1387 * plaintext
1388 * @src: scatter/gather list that holds the ciphertext
1389 * @nbytes: number of bytes of the ciphertext to decrypt.
1390 *
1391 * Decrypt ciphertext data with the use of an IV that is solely used for this
1392 * cipher operation. Any previously set IV is not used.
1393 *
1394 * The blkcipher_desc data structure must be filled by the caller as documented
1395 * for the crypto_blkcipher_encrypt_iv call above.
1396 *
1397 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
1398 */
crypto_blkcipher_decrypt_iv(struct blkcipher_desc * desc,struct scatterlist * dst,struct scatterlist * src,unsigned int nbytes)1399 static inline int crypto_blkcipher_decrypt_iv(struct blkcipher_desc *desc,
1400 struct scatterlist *dst,
1401 struct scatterlist *src,
1402 unsigned int nbytes)
1403 {
1404 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
1405 }
1406
1407 /**
1408 * crypto_blkcipher_set_iv() - set IV for cipher
1409 * @tfm: cipher handle
1410 * @src: buffer holding the IV
1411 * @len: length of the IV in bytes
1412 *
1413 * The caller provided IV is set for the block cipher referenced by the cipher
1414 * handle.
1415 */
crypto_blkcipher_set_iv(struct crypto_blkcipher * tfm,const u8 * src,unsigned int len)1416 static inline void crypto_blkcipher_set_iv(struct crypto_blkcipher *tfm,
1417 const u8 *src, unsigned int len)
1418 {
1419 memcpy(crypto_blkcipher_crt(tfm)->iv, src, len);
1420 }
1421
1422 /**
1423 * crypto_blkcipher_get_iv() - obtain IV from cipher
1424 * @tfm: cipher handle
1425 * @dst: buffer filled with the IV
1426 * @len: length of the buffer dst
1427 *
1428 * The caller can obtain the IV set for the block cipher referenced by the
1429 * cipher handle and store it into the user-provided buffer. If the buffer
1430 * has an insufficient space, the IV is truncated to fit the buffer.
1431 */
crypto_blkcipher_get_iv(struct crypto_blkcipher * tfm,u8 * dst,unsigned int len)1432 static inline void crypto_blkcipher_get_iv(struct crypto_blkcipher *tfm,
1433 u8 *dst, unsigned int len)
1434 {
1435 memcpy(dst, crypto_blkcipher_crt(tfm)->iv, len);
1436 }
1437
1438 /**
1439 * DOC: Single Block Cipher API
1440 *
1441 * The single block cipher API is used with the ciphers of type
1442 * CRYPTO_ALG_TYPE_CIPHER (listed as type "cipher" in /proc/crypto).
1443 *
1444 * Using the single block cipher API calls, operations with the basic cipher
1445 * primitive can be implemented. These cipher primitives exclude any block
1446 * chaining operations including IV handling.
1447 *
1448 * The purpose of this single block cipher API is to support the implementation
1449 * of templates or other concepts that only need to perform the cipher operation
1450 * on one block at a time. Templates invoke the underlying cipher primitive
1451 * block-wise and process either the input or the output data of these cipher
1452 * operations.
1453 */
1454
__crypto_cipher_cast(struct crypto_tfm * tfm)1455 static inline struct crypto_cipher *__crypto_cipher_cast(struct crypto_tfm *tfm)
1456 {
1457 return (struct crypto_cipher *)tfm;
1458 }
1459
crypto_cipher_cast(struct crypto_tfm * tfm)1460 static inline struct crypto_cipher *crypto_cipher_cast(struct crypto_tfm *tfm)
1461 {
1462 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
1463 return __crypto_cipher_cast(tfm);
1464 }
1465
1466 /**
1467 * crypto_alloc_cipher() - allocate single block cipher handle
1468 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1469 * single block cipher
1470 * @type: specifies the type of the cipher
1471 * @mask: specifies the mask for the cipher
1472 *
1473 * Allocate a cipher handle for a single block cipher. The returned struct
1474 * crypto_cipher is the cipher handle that is required for any subsequent API
1475 * invocation for that single block cipher.
1476 *
1477 * Return: allocated cipher handle in case of success; IS_ERR() is true in case
1478 * of an error, PTR_ERR() returns the error code.
1479 */
crypto_alloc_cipher(const char * alg_name,u32 type,u32 mask)1480 static inline struct crypto_cipher *crypto_alloc_cipher(const char *alg_name,
1481 u32 type, u32 mask)
1482 {
1483 type &= ~CRYPTO_ALG_TYPE_MASK;
1484 type |= CRYPTO_ALG_TYPE_CIPHER;
1485 mask |= CRYPTO_ALG_TYPE_MASK;
1486
1487 return __crypto_cipher_cast(crypto_alloc_base(alg_name, type, mask));
1488 }
1489
crypto_cipher_tfm(struct crypto_cipher * tfm)1490 static inline struct crypto_tfm *crypto_cipher_tfm(struct crypto_cipher *tfm)
1491 {
1492 return &tfm->base;
1493 }
1494
1495 /**
1496 * crypto_free_cipher() - zeroize and free the single block cipher handle
1497 * @tfm: cipher handle to be freed
1498 */
crypto_free_cipher(struct crypto_cipher * tfm)1499 static inline void crypto_free_cipher(struct crypto_cipher *tfm)
1500 {
1501 crypto_free_tfm(crypto_cipher_tfm(tfm));
1502 }
1503
1504 /**
1505 * crypto_has_cipher() - Search for the availability of a single block cipher
1506 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1507 * single block cipher
1508 * @type: specifies the type of the cipher
1509 * @mask: specifies the mask for the cipher
1510 *
1511 * Return: true when the single block cipher is known to the kernel crypto API;
1512 * false otherwise
1513 */
crypto_has_cipher(const char * alg_name,u32 type,u32 mask)1514 static inline int crypto_has_cipher(const char *alg_name, u32 type, u32 mask)
1515 {
1516 type &= ~CRYPTO_ALG_TYPE_MASK;
1517 type |= CRYPTO_ALG_TYPE_CIPHER;
1518 mask |= CRYPTO_ALG_TYPE_MASK;
1519
1520 return crypto_has_alg(alg_name, type, mask);
1521 }
1522
crypto_cipher_crt(struct crypto_cipher * tfm)1523 static inline struct cipher_tfm *crypto_cipher_crt(struct crypto_cipher *tfm)
1524 {
1525 return &crypto_cipher_tfm(tfm)->crt_cipher;
1526 }
1527
1528 /**
1529 * crypto_cipher_blocksize() - obtain block size for cipher
1530 * @tfm: cipher handle
1531 *
1532 * The block size for the single block cipher referenced with the cipher handle
1533 * tfm is returned. The caller may use that information to allocate appropriate
1534 * memory for the data returned by the encryption or decryption operation
1535 *
1536 * Return: block size of cipher
1537 */
crypto_cipher_blocksize(struct crypto_cipher * tfm)1538 static inline unsigned int crypto_cipher_blocksize(struct crypto_cipher *tfm)
1539 {
1540 return crypto_tfm_alg_blocksize(crypto_cipher_tfm(tfm));
1541 }
1542
crypto_cipher_alignmask(struct crypto_cipher * tfm)1543 static inline unsigned int crypto_cipher_alignmask(struct crypto_cipher *tfm)
1544 {
1545 return crypto_tfm_alg_alignmask(crypto_cipher_tfm(tfm));
1546 }
1547
crypto_cipher_get_flags(struct crypto_cipher * tfm)1548 static inline u32 crypto_cipher_get_flags(struct crypto_cipher *tfm)
1549 {
1550 return crypto_tfm_get_flags(crypto_cipher_tfm(tfm));
1551 }
1552
crypto_cipher_set_flags(struct crypto_cipher * tfm,u32 flags)1553 static inline void crypto_cipher_set_flags(struct crypto_cipher *tfm,
1554 u32 flags)
1555 {
1556 crypto_tfm_set_flags(crypto_cipher_tfm(tfm), flags);
1557 }
1558
crypto_cipher_clear_flags(struct crypto_cipher * tfm,u32 flags)1559 static inline void crypto_cipher_clear_flags(struct crypto_cipher *tfm,
1560 u32 flags)
1561 {
1562 crypto_tfm_clear_flags(crypto_cipher_tfm(tfm), flags);
1563 }
1564
1565 /**
1566 * crypto_cipher_setkey() - set key for cipher
1567 * @tfm: cipher handle
1568 * @key: buffer holding the key
1569 * @keylen: length of the key in bytes
1570 *
1571 * The caller provided key is set for the single block cipher referenced by the
1572 * cipher handle.
1573 *
1574 * Note, the key length determines the cipher type. Many block ciphers implement
1575 * different cipher modes depending on the key size, such as AES-128 vs AES-192
1576 * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
1577 * is performed.
1578 *
1579 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
1580 */
crypto_cipher_setkey(struct crypto_cipher * tfm,const u8 * key,unsigned int keylen)1581 static inline int crypto_cipher_setkey(struct crypto_cipher *tfm,
1582 const u8 *key, unsigned int keylen)
1583 {
1584 return crypto_cipher_crt(tfm)->cit_setkey(crypto_cipher_tfm(tfm),
1585 key, keylen);
1586 }
1587
1588 /**
1589 * crypto_cipher_encrypt_one() - encrypt one block of plaintext
1590 * @tfm: cipher handle
1591 * @dst: points to the buffer that will be filled with the ciphertext
1592 * @src: buffer holding the plaintext to be encrypted
1593 *
1594 * Invoke the encryption operation of one block. The caller must ensure that
1595 * the plaintext and ciphertext buffers are at least one block in size.
1596 */
crypto_cipher_encrypt_one(struct crypto_cipher * tfm,u8 * dst,const u8 * src)1597 static inline void crypto_cipher_encrypt_one(struct crypto_cipher *tfm,
1598 u8 *dst, const u8 *src)
1599 {
1600 crypto_cipher_crt(tfm)->cit_encrypt_one(crypto_cipher_tfm(tfm),
1601 dst, src);
1602 }
1603
1604 /**
1605 * crypto_cipher_decrypt_one() - decrypt one block of ciphertext
1606 * @tfm: cipher handle
1607 * @dst: points to the buffer that will be filled with the plaintext
1608 * @src: buffer holding the ciphertext to be decrypted
1609 *
1610 * Invoke the decryption operation of one block. The caller must ensure that
1611 * the plaintext and ciphertext buffers are at least one block in size.
1612 */
crypto_cipher_decrypt_one(struct crypto_cipher * tfm,u8 * dst,const u8 * src)1613 static inline void crypto_cipher_decrypt_one(struct crypto_cipher *tfm,
1614 u8 *dst, const u8 *src)
1615 {
1616 crypto_cipher_crt(tfm)->cit_decrypt_one(crypto_cipher_tfm(tfm),
1617 dst, src);
1618 }
1619
1620 /**
1621 * DOC: Synchronous Message Digest API
1622 *
1623 * The synchronous message digest API is used with the ciphers of type
1624 * CRYPTO_ALG_TYPE_HASH (listed as type "hash" in /proc/crypto)
1625 */
1626
__crypto_hash_cast(struct crypto_tfm * tfm)1627 static inline struct crypto_hash *__crypto_hash_cast(struct crypto_tfm *tfm)
1628 {
1629 return (struct crypto_hash *)tfm;
1630 }
1631
crypto_hash_cast(struct crypto_tfm * tfm)1632 static inline struct crypto_hash *crypto_hash_cast(struct crypto_tfm *tfm)
1633 {
1634 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_HASH) &
1635 CRYPTO_ALG_TYPE_HASH_MASK);
1636 return __crypto_hash_cast(tfm);
1637 }
1638
1639 /**
1640 * crypto_alloc_hash() - allocate synchronous message digest handle
1641 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1642 * message digest cipher
1643 * @type: specifies the type of the cipher
1644 * @mask: specifies the mask for the cipher
1645 *
1646 * Allocate a cipher handle for a message digest. The returned struct
1647 * crypto_hash is the cipher handle that is required for any subsequent
1648 * API invocation for that message digest.
1649 *
1650 * Return: allocated cipher handle in case of success; IS_ERR() is true in case
1651 * of an error, PTR_ERR() returns the error code.
1652 */
crypto_alloc_hash(const char * alg_name,u32 type,u32 mask)1653 static inline struct crypto_hash *crypto_alloc_hash(const char *alg_name,
1654 u32 type, u32 mask)
1655 {
1656 type &= ~CRYPTO_ALG_TYPE_MASK;
1657 mask &= ~CRYPTO_ALG_TYPE_MASK;
1658 type |= CRYPTO_ALG_TYPE_HASH;
1659 mask |= CRYPTO_ALG_TYPE_HASH_MASK;
1660
1661 return __crypto_hash_cast(crypto_alloc_base(alg_name, type, mask));
1662 }
1663
crypto_hash_tfm(struct crypto_hash * tfm)1664 static inline struct crypto_tfm *crypto_hash_tfm(struct crypto_hash *tfm)
1665 {
1666 return &tfm->base;
1667 }
1668
1669 /**
1670 * crypto_free_hash() - zeroize and free message digest handle
1671 * @tfm: cipher handle to be freed
1672 */
crypto_free_hash(struct crypto_hash * tfm)1673 static inline void crypto_free_hash(struct crypto_hash *tfm)
1674 {
1675 crypto_free_tfm(crypto_hash_tfm(tfm));
1676 }
1677
1678 /**
1679 * crypto_has_hash() - Search for the availability of a message digest
1680 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
1681 * message digest cipher
1682 * @type: specifies the type of the cipher
1683 * @mask: specifies the mask for the cipher
1684 *
1685 * Return: true when the message digest cipher is known to the kernel crypto
1686 * API; false otherwise
1687 */
crypto_has_hash(const char * alg_name,u32 type,u32 mask)1688 static inline int crypto_has_hash(const char *alg_name, u32 type, u32 mask)
1689 {
1690 type &= ~CRYPTO_ALG_TYPE_MASK;
1691 mask &= ~CRYPTO_ALG_TYPE_MASK;
1692 type |= CRYPTO_ALG_TYPE_HASH;
1693 mask |= CRYPTO_ALG_TYPE_HASH_MASK;
1694
1695 return crypto_has_alg(alg_name, type, mask);
1696 }
1697
crypto_hash_crt(struct crypto_hash * tfm)1698 static inline struct hash_tfm *crypto_hash_crt(struct crypto_hash *tfm)
1699 {
1700 return &crypto_hash_tfm(tfm)->crt_hash;
1701 }
1702
1703 /**
1704 * crypto_hash_blocksize() - obtain block size for message digest
1705 * @tfm: cipher handle
1706 *
1707 * The block size for the message digest cipher referenced with the cipher
1708 * handle is returned.
1709 *
1710 * Return: block size of cipher
1711 */
crypto_hash_blocksize(struct crypto_hash * tfm)1712 static inline unsigned int crypto_hash_blocksize(struct crypto_hash *tfm)
1713 {
1714 return crypto_tfm_alg_blocksize(crypto_hash_tfm(tfm));
1715 }
1716
crypto_hash_alignmask(struct crypto_hash * tfm)1717 static inline unsigned int crypto_hash_alignmask(struct crypto_hash *tfm)
1718 {
1719 return crypto_tfm_alg_alignmask(crypto_hash_tfm(tfm));
1720 }
1721
1722 /**
1723 * crypto_hash_digestsize() - obtain message digest size
1724 * @tfm: cipher handle
1725 *
1726 * The size for the message digest created by the message digest cipher
1727 * referenced with the cipher handle is returned.
1728 *
1729 * Return: message digest size
1730 */
crypto_hash_digestsize(struct crypto_hash * tfm)1731 static inline unsigned int crypto_hash_digestsize(struct crypto_hash *tfm)
1732 {
1733 return crypto_hash_crt(tfm)->digestsize;
1734 }
1735
crypto_hash_get_flags(struct crypto_hash * tfm)1736 static inline u32 crypto_hash_get_flags(struct crypto_hash *tfm)
1737 {
1738 return crypto_tfm_get_flags(crypto_hash_tfm(tfm));
1739 }
1740
crypto_hash_set_flags(struct crypto_hash * tfm,u32 flags)1741 static inline void crypto_hash_set_flags(struct crypto_hash *tfm, u32 flags)
1742 {
1743 crypto_tfm_set_flags(crypto_hash_tfm(tfm), flags);
1744 }
1745
crypto_hash_clear_flags(struct crypto_hash * tfm,u32 flags)1746 static inline void crypto_hash_clear_flags(struct crypto_hash *tfm, u32 flags)
1747 {
1748 crypto_tfm_clear_flags(crypto_hash_tfm(tfm), flags);
1749 }
1750
1751 /**
1752 * crypto_hash_init() - (re)initialize message digest handle
1753 * @desc: cipher request handle that to be filled by caller --
1754 * desc.tfm is filled with the hash cipher handle;
1755 * desc.flags is filled with either CRYPTO_TFM_REQ_MAY_SLEEP or 0.
1756 *
1757 * The call (re-)initializes the message digest referenced by the hash cipher
1758 * request handle. Any potentially existing state created by previous
1759 * operations is discarded.
1760 *
1761 * Return: 0 if the message digest initialization was successful; < 0 if an
1762 * error occurred
1763 */
crypto_hash_init(struct hash_desc * desc)1764 static inline int crypto_hash_init(struct hash_desc *desc)
1765 {
1766 return crypto_hash_crt(desc->tfm)->init(desc);
1767 }
1768
1769 /**
1770 * crypto_hash_update() - add data to message digest for processing
1771 * @desc: cipher request handle
1772 * @sg: scatter / gather list pointing to the data to be added to the message
1773 * digest
1774 * @nbytes: number of bytes to be processed from @sg
1775 *
1776 * Updates the message digest state of the cipher handle pointed to by the
1777 * hash cipher request handle with the input data pointed to by the
1778 * scatter/gather list.
1779 *
1780 * Return: 0 if the message digest update was successful; < 0 if an error
1781 * occurred
1782 */
crypto_hash_update(struct hash_desc * desc,struct scatterlist * sg,unsigned int nbytes)1783 static inline int crypto_hash_update(struct hash_desc *desc,
1784 struct scatterlist *sg,
1785 unsigned int nbytes)
1786 {
1787 return crypto_hash_crt(desc->tfm)->update(desc, sg, nbytes);
1788 }
1789
1790 /**
1791 * crypto_hash_final() - calculate message digest
1792 * @desc: cipher request handle
1793 * @out: message digest output buffer -- The caller must ensure that the out
1794 * buffer has a sufficient size (e.g. by using the crypto_hash_digestsize
1795 * function).
1796 *
1797 * Finalize the message digest operation and create the message digest
1798 * based on all data added to the cipher handle. The message digest is placed
1799 * into the output buffer.
1800 *
1801 * Return: 0 if the message digest creation was successful; < 0 if an error
1802 * occurred
1803 */
crypto_hash_final(struct hash_desc * desc,u8 * out)1804 static inline int crypto_hash_final(struct hash_desc *desc, u8 *out)
1805 {
1806 return crypto_hash_crt(desc->tfm)->final(desc, out);
1807 }
1808
1809 /**
1810 * crypto_hash_digest() - calculate message digest for a buffer
1811 * @desc: see crypto_hash_final()
1812 * @sg: see crypto_hash_update()
1813 * @nbytes: see crypto_hash_update()
1814 * @out: see crypto_hash_final()
1815 *
1816 * This function is a "short-hand" for the function calls of crypto_hash_init,
1817 * crypto_hash_update and crypto_hash_final. The parameters have the same
1818 * meaning as discussed for those separate three functions.
1819 *
1820 * Return: 0 if the message digest creation was successful; < 0 if an error
1821 * occurred
1822 */
crypto_hash_digest(struct hash_desc * desc,struct scatterlist * sg,unsigned int nbytes,u8 * out)1823 static inline int crypto_hash_digest(struct hash_desc *desc,
1824 struct scatterlist *sg,
1825 unsigned int nbytes, u8 *out)
1826 {
1827 return crypto_hash_crt(desc->tfm)->digest(desc, sg, nbytes, out);
1828 }
1829
1830 /**
1831 * crypto_hash_setkey() - set key for message digest
1832 * @hash: cipher handle
1833 * @key: buffer holding the key
1834 * @keylen: length of the key in bytes
1835 *
1836 * The caller provided key is set for the message digest cipher. The cipher
1837 * handle must point to a keyed hash in order for this function to succeed.
1838 *
1839 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
1840 */
crypto_hash_setkey(struct crypto_hash * hash,const u8 * key,unsigned int keylen)1841 static inline int crypto_hash_setkey(struct crypto_hash *hash,
1842 const u8 *key, unsigned int keylen)
1843 {
1844 return crypto_hash_crt(hash)->setkey(hash, key, keylen);
1845 }
1846
__crypto_comp_cast(struct crypto_tfm * tfm)1847 static inline struct crypto_comp *__crypto_comp_cast(struct crypto_tfm *tfm)
1848 {
1849 return (struct crypto_comp *)tfm;
1850 }
1851
crypto_comp_cast(struct crypto_tfm * tfm)1852 static inline struct crypto_comp *crypto_comp_cast(struct crypto_tfm *tfm)
1853 {
1854 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_COMPRESS) &
1855 CRYPTO_ALG_TYPE_MASK);
1856 return __crypto_comp_cast(tfm);
1857 }
1858
crypto_alloc_comp(const char * alg_name,u32 type,u32 mask)1859 static inline struct crypto_comp *crypto_alloc_comp(const char *alg_name,
1860 u32 type, u32 mask)
1861 {
1862 type &= ~CRYPTO_ALG_TYPE_MASK;
1863 type |= CRYPTO_ALG_TYPE_COMPRESS;
1864 mask |= CRYPTO_ALG_TYPE_MASK;
1865
1866 return __crypto_comp_cast(crypto_alloc_base(alg_name, type, mask));
1867 }
1868
crypto_comp_tfm(struct crypto_comp * tfm)1869 static inline struct crypto_tfm *crypto_comp_tfm(struct crypto_comp *tfm)
1870 {
1871 return &tfm->base;
1872 }
1873
crypto_free_comp(struct crypto_comp * tfm)1874 static inline void crypto_free_comp(struct crypto_comp *tfm)
1875 {
1876 crypto_free_tfm(crypto_comp_tfm(tfm));
1877 }
1878
crypto_has_comp(const char * alg_name,u32 type,u32 mask)1879 static inline int crypto_has_comp(const char *alg_name, u32 type, u32 mask)
1880 {
1881 type &= ~CRYPTO_ALG_TYPE_MASK;
1882 type |= CRYPTO_ALG_TYPE_COMPRESS;
1883 mask |= CRYPTO_ALG_TYPE_MASK;
1884
1885 return crypto_has_alg(alg_name, type, mask);
1886 }
1887
crypto_comp_name(struct crypto_comp * tfm)1888 static inline const char *crypto_comp_name(struct crypto_comp *tfm)
1889 {
1890 return crypto_tfm_alg_name(crypto_comp_tfm(tfm));
1891 }
1892
crypto_comp_crt(struct crypto_comp * tfm)1893 static inline struct compress_tfm *crypto_comp_crt(struct crypto_comp *tfm)
1894 {
1895 return &crypto_comp_tfm(tfm)->crt_compress;
1896 }
1897
crypto_comp_compress(struct crypto_comp * tfm,const u8 * src,unsigned int slen,u8 * dst,unsigned int * dlen)1898 static inline int crypto_comp_compress(struct crypto_comp *tfm,
1899 const u8 *src, unsigned int slen,
1900 u8 *dst, unsigned int *dlen)
1901 {
1902 return crypto_comp_crt(tfm)->cot_compress(crypto_comp_tfm(tfm),
1903 src, slen, dst, dlen);
1904 }
1905
crypto_comp_decompress(struct crypto_comp * tfm,const u8 * src,unsigned int slen,u8 * dst,unsigned int * dlen)1906 static inline int crypto_comp_decompress(struct crypto_comp *tfm,
1907 const u8 *src, unsigned int slen,
1908 u8 *dst, unsigned int *dlen)
1909 {
1910 return crypto_comp_crt(tfm)->cot_decompress(crypto_comp_tfm(tfm),
1911 src, slen, dst, dlen);
1912 }
1913
1914 #endif /* _LINUX_CRYPTO_H */
1915
1916