root/include/linux/crypto.h

/* [<][>][^][v][top][bottom][index][help] */

INCLUDED FROM


DEFINITIONS

This source file includes following definitions.
  1. crypto_stats_init
  2. crypto_stats_get
  3. crypto_stats_ablkcipher_encrypt
  4. crypto_stats_ablkcipher_decrypt
  5. crypto_stats_aead_encrypt
  6. crypto_stats_aead_decrypt
  7. crypto_stats_ahash_update
  8. crypto_stats_ahash_final
  9. crypto_stats_akcipher_encrypt
  10. crypto_stats_akcipher_decrypt
  11. crypto_stats_akcipher_sign
  12. crypto_stats_akcipher_verify
  13. crypto_stats_compress
  14. crypto_stats_decompress
  15. crypto_stats_kpp_set_secret
  16. crypto_stats_kpp_generate_public_key
  17. crypto_stats_kpp_compute_shared_secret
  18. crypto_stats_rng_seed
  19. crypto_stats_rng_generate
  20. crypto_stats_skcipher_encrypt
  21. crypto_stats_skcipher_decrypt
  22. crypto_wait_req
  23. crypto_init_wait
  24. crypto_free_tfm
  25. crypto_tfm_alg_name
  26. crypto_tfm_alg_driver_name
  27. crypto_tfm_alg_priority
  28. crypto_tfm_alg_type
  29. crypto_tfm_alg_blocksize
  30. crypto_tfm_alg_alignmask
  31. crypto_tfm_get_flags
  32. crypto_tfm_set_flags
  33. crypto_tfm_clear_flags
  34. crypto_tfm_ctx
  35. crypto_tfm_ctx_alignment
  36. __crypto_ablkcipher_cast
  37. crypto_skcipher_type
  38. crypto_skcipher_mask
  39. crypto_ablkcipher_tfm
  40. crypto_free_ablkcipher
  41. crypto_has_ablkcipher
  42. crypto_ablkcipher_crt
  43. crypto_ablkcipher_ivsize
  44. crypto_ablkcipher_blocksize
  45. crypto_ablkcipher_alignmask
  46. crypto_ablkcipher_get_flags
  47. crypto_ablkcipher_set_flags
  48. crypto_ablkcipher_clear_flags
  49. crypto_ablkcipher_setkey
  50. crypto_ablkcipher_reqtfm
  51. crypto_ablkcipher_encrypt
  52. crypto_ablkcipher_decrypt
  53. crypto_ablkcipher_reqsize
  54. ablkcipher_request_set_tfm
  55. ablkcipher_request_cast
  56. ablkcipher_request_alloc
  57. ablkcipher_request_free
  58. ablkcipher_request_set_callback
  59. ablkcipher_request_set_crypt
  60. __crypto_blkcipher_cast
  61. crypto_blkcipher_cast
  62. crypto_alloc_blkcipher
  63. crypto_blkcipher_tfm
  64. crypto_free_blkcipher
  65. crypto_has_blkcipher
  66. crypto_blkcipher_name
  67. crypto_blkcipher_crt
  68. crypto_blkcipher_alg
  69. crypto_blkcipher_ivsize
  70. crypto_blkcipher_blocksize
  71. crypto_blkcipher_alignmask
  72. crypto_blkcipher_get_flags
  73. crypto_blkcipher_set_flags
  74. crypto_blkcipher_clear_flags
  75. crypto_blkcipher_setkey
  76. crypto_blkcipher_encrypt
  77. crypto_blkcipher_encrypt_iv
  78. crypto_blkcipher_decrypt
  79. crypto_blkcipher_decrypt_iv
  80. crypto_blkcipher_set_iv
  81. crypto_blkcipher_get_iv
  82. __crypto_cipher_cast
  83. crypto_cipher_cast
  84. crypto_alloc_cipher
  85. crypto_cipher_tfm
  86. crypto_free_cipher
  87. crypto_has_cipher
  88. crypto_cipher_crt
  89. crypto_cipher_blocksize
  90. crypto_cipher_alignmask
  91. crypto_cipher_get_flags
  92. crypto_cipher_set_flags
  93. crypto_cipher_clear_flags
  94. crypto_cipher_setkey
  95. crypto_cipher_encrypt_one
  96. crypto_cipher_decrypt_one
  97. __crypto_comp_cast
  98. crypto_comp_cast
  99. crypto_alloc_comp
  100. crypto_comp_tfm
  101. crypto_free_comp
  102. crypto_has_comp
  103. crypto_comp_name
  104. crypto_comp_crt
  105. crypto_comp_compress
  106. crypto_comp_decompress

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

/* [<][>][^][v][top][bottom][index][help] */