Herbert Xu | 61da88e | 2007-12-17 21:51:27 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Symmetric key ciphers. |
| 3 | * |
Herbert Xu | 7a7ffe6 | 2015-08-20 15:21:45 +0800 | [diff] [blame] | 4 | * Copyright (c) 2007-2015 Herbert Xu <herbert@gondor.apana.org.au> |
Herbert Xu | 61da88e | 2007-12-17 21:51:27 +0800 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify it |
| 7 | * under the terms of the GNU General Public License as published by the Free |
| 8 | * Software Foundation; either version 2 of the License, or (at your option) |
| 9 | * any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
| 13 | #ifndef _CRYPTO_SKCIPHER_H |
| 14 | #define _CRYPTO_SKCIPHER_H |
| 15 | |
| 16 | #include <linux/crypto.h> |
Herbert Xu | 03bf712 | 2007-12-01 18:35:38 +1100 | [diff] [blame] | 17 | #include <linux/kernel.h> |
| 18 | #include <linux/slab.h> |
Herbert Xu | 61da88e | 2007-12-17 21:51:27 +0800 | [diff] [blame] | 19 | |
| 20 | /** |
Herbert Xu | 7a7ffe6 | 2015-08-20 15:21:45 +0800 | [diff] [blame] | 21 | * struct skcipher_request - Symmetric key cipher request |
| 22 | * @cryptlen: Number of bytes to encrypt or decrypt |
| 23 | * @iv: Initialisation Vector |
| 24 | * @src: Source SG list |
| 25 | * @dst: Destination SG list |
| 26 | * @base: Underlying async request request |
| 27 | * @__ctx: Start of private context data |
| 28 | */ |
| 29 | struct skcipher_request { |
| 30 | unsigned int cryptlen; |
| 31 | |
| 32 | u8 *iv; |
| 33 | |
| 34 | struct scatterlist *src; |
| 35 | struct scatterlist *dst; |
| 36 | |
| 37 | struct crypto_async_request base; |
| 38 | |
| 39 | void *__ctx[] CRYPTO_MINALIGN_ATTR; |
| 40 | }; |
| 41 | |
| 42 | /** |
Herbert Xu | 61da88e | 2007-12-17 21:51:27 +0800 | [diff] [blame] | 43 | * struct skcipher_givcrypt_request - Crypto request with IV generation |
| 44 | * @seq: Sequence number for IV generation |
| 45 | * @giv: Space for generated IV |
| 46 | * @creq: The crypto request itself |
| 47 | */ |
| 48 | struct skcipher_givcrypt_request { |
| 49 | u64 seq; |
| 50 | u8 *giv; |
| 51 | |
| 52 | struct ablkcipher_request creq; |
| 53 | }; |
| 54 | |
Herbert Xu | 7a7ffe6 | 2015-08-20 15:21:45 +0800 | [diff] [blame] | 55 | struct crypto_skcipher { |
| 56 | int (*setkey)(struct crypto_skcipher *tfm, const u8 *key, |
| 57 | unsigned int keylen); |
| 58 | int (*encrypt)(struct skcipher_request *req); |
| 59 | int (*decrypt)(struct skcipher_request *req); |
| 60 | |
| 61 | unsigned int ivsize; |
| 62 | unsigned int reqsize; |
Herbert Xu | 973fb3f | 2016-01-21 17:10:56 +0800 | [diff] [blame] | 63 | unsigned int keysize; |
Herbert Xu | a1383cd | 2016-01-11 21:26:50 +0800 | [diff] [blame] | 64 | |
Herbert Xu | 7a7ffe6 | 2015-08-20 15:21:45 +0800 | [diff] [blame] | 65 | struct crypto_tfm base; |
| 66 | }; |
| 67 | |
Kees Cook | b350bee | 2018-09-18 19:10:38 -0700 | [diff] [blame^] | 68 | struct crypto_sync_skcipher { |
| 69 | struct crypto_skcipher base; |
| 70 | }; |
| 71 | |
Herbert Xu | 4e6c3df | 2016-07-12 13:17:31 +0800 | [diff] [blame] | 72 | /** |
| 73 | * struct skcipher_alg - symmetric key cipher definition |
| 74 | * @min_keysize: Minimum key size supported by the transformation. This is the |
| 75 | * smallest key length supported by this transformation algorithm. |
| 76 | * This must be set to one of the pre-defined values as this is |
| 77 | * not hardware specific. Possible values for this field can be |
| 78 | * found via git grep "_MIN_KEY_SIZE" include/crypto/ |
| 79 | * @max_keysize: Maximum key size supported by the transformation. This is the |
| 80 | * largest key length supported by this transformation algorithm. |
| 81 | * This must be set to one of the pre-defined values as this is |
| 82 | * not hardware specific. Possible values for this field can be |
| 83 | * found via git grep "_MAX_KEY_SIZE" include/crypto/ |
| 84 | * @setkey: Set key for the transformation. This function is used to either |
| 85 | * program a supplied key into the hardware or store the key in the |
| 86 | * transformation context for programming it later. Note that this |
| 87 | * function does modify the transformation context. This function can |
| 88 | * be called multiple times during the existence of the transformation |
| 89 | * object, so one must make sure the key is properly reprogrammed into |
| 90 | * the hardware. This function is also responsible for checking the key |
| 91 | * length for validity. In case a software fallback was put in place in |
| 92 | * the @cra_init call, this function might need to use the fallback if |
| 93 | * the algorithm doesn't support all of the key sizes. |
| 94 | * @encrypt: Encrypt a scatterlist of blocks. This function is used to encrypt |
| 95 | * the supplied scatterlist containing the blocks of data. The crypto |
| 96 | * API consumer is responsible for aligning the entries of the |
| 97 | * scatterlist properly and making sure the chunks are correctly |
| 98 | * sized. In case a software fallback was put in place in the |
| 99 | * @cra_init call, this function might need to use the fallback if |
| 100 | * the algorithm doesn't support all of the key sizes. In case the |
| 101 | * key was stored in transformation context, the key might need to be |
| 102 | * re-programmed into the hardware in this function. This function |
| 103 | * shall not modify the transformation context, as this function may |
| 104 | * be called in parallel with the same transformation object. |
| 105 | * @decrypt: Decrypt a single block. This is a reverse counterpart to @encrypt |
| 106 | * and the conditions are exactly the same. |
| 107 | * @init: Initialize the cryptographic transformation object. This function |
| 108 | * is used to initialize the cryptographic transformation object. |
| 109 | * This function is called only once at the instantiation time, right |
| 110 | * after the transformation context was allocated. In case the |
| 111 | * cryptographic hardware has some special requirements which need to |
| 112 | * be handled by software, this function shall check for the precise |
| 113 | * requirement of the transformation and put any software fallbacks |
| 114 | * in place. |
| 115 | * @exit: Deinitialize the cryptographic transformation object. This is a |
| 116 | * counterpart to @init, used to remove various changes set in |
| 117 | * @init. |
| 118 | * @ivsize: IV size applicable for transformation. The consumer must provide an |
| 119 | * IV of exactly that size to perform the encrypt or decrypt operation. |
| 120 | * @chunksize: Equal to the block size except for stream ciphers such as |
| 121 | * CTR where it is set to the underlying block size. |
Ard Biesheuvel | c821f6a | 2016-12-29 14:09:08 +0000 | [diff] [blame] | 122 | * @walksize: Equal to the chunk size except in cases where the algorithm is |
| 123 | * considerably more efficient if it can operate on multiple chunks |
| 124 | * in parallel. Should be a multiple of chunksize. |
Herbert Xu | 5c56233 | 2016-07-19 00:59:30 +0800 | [diff] [blame] | 125 | * @base: Definition of a generic crypto algorithm. |
Herbert Xu | 4e6c3df | 2016-07-12 13:17:31 +0800 | [diff] [blame] | 126 | * |
| 127 | * All fields except @ivsize are mandatory and must be filled. |
| 128 | */ |
| 129 | struct skcipher_alg { |
| 130 | int (*setkey)(struct crypto_skcipher *tfm, const u8 *key, |
| 131 | unsigned int keylen); |
| 132 | int (*encrypt)(struct skcipher_request *req); |
| 133 | int (*decrypt)(struct skcipher_request *req); |
| 134 | int (*init)(struct crypto_skcipher *tfm); |
| 135 | void (*exit)(struct crypto_skcipher *tfm); |
| 136 | |
| 137 | unsigned int min_keysize; |
| 138 | unsigned int max_keysize; |
| 139 | unsigned int ivsize; |
| 140 | unsigned int chunksize; |
Ard Biesheuvel | c821f6a | 2016-12-29 14:09:08 +0000 | [diff] [blame] | 141 | unsigned int walksize; |
Herbert Xu | 4e6c3df | 2016-07-12 13:17:31 +0800 | [diff] [blame] | 142 | |
| 143 | struct crypto_alg base; |
| 144 | }; |
| 145 | |
Kees Cook | b350bee | 2018-09-18 19:10:38 -0700 | [diff] [blame^] | 146 | #define MAX_SYNC_SKCIPHER_REQSIZE 384 |
| 147 | /* |
| 148 | * This performs a type-check against the "tfm" argument to make sure |
| 149 | * all users have the correct skcipher tfm for doing on-stack requests. |
| 150 | */ |
| 151 | #define SYNC_SKCIPHER_REQUEST_ON_STACK(name, tfm) \ |
| 152 | char __##name##_desc[sizeof(struct skcipher_request) + \ |
| 153 | MAX_SYNC_SKCIPHER_REQSIZE + \ |
| 154 | (!(sizeof((struct crypto_sync_skcipher *)1 == \ |
| 155 | (typeof(tfm))1))) \ |
| 156 | ] CRYPTO_MINALIGN_ATTR; \ |
| 157 | struct skcipher_request *name = (void *)__##name##_desc |
| 158 | |
Herbert Xu | 7a7ffe6 | 2015-08-20 15:21:45 +0800 | [diff] [blame] | 159 | #define SKCIPHER_REQUEST_ON_STACK(name, tfm) \ |
| 160 | char __##name##_desc[sizeof(struct skcipher_request) + \ |
| 161 | crypto_skcipher_reqsize(tfm)] CRYPTO_MINALIGN_ATTR; \ |
| 162 | struct skcipher_request *name = (void *)__##name##_desc |
| 163 | |
Herbert Xu | 7a7ffe6 | 2015-08-20 15:21:45 +0800 | [diff] [blame] | 164 | /** |
| 165 | * DOC: Symmetric Key Cipher API |
| 166 | * |
| 167 | * Symmetric key cipher API is used with the ciphers of type |
| 168 | * CRYPTO_ALG_TYPE_SKCIPHER (listed as type "skcipher" in /proc/crypto). |
| 169 | * |
| 170 | * Asynchronous cipher operations imply that the function invocation for a |
| 171 | * cipher request returns immediately before the completion of the operation. |
| 172 | * The cipher request is scheduled as a separate kernel thread and therefore |
| 173 | * load-balanced on the different CPUs via the process scheduler. To allow |
| 174 | * the kernel crypto API to inform the caller about the completion of a cipher |
| 175 | * request, the caller must provide a callback function. That function is |
| 176 | * invoked with the cipher handle when the request completes. |
| 177 | * |
| 178 | * To support the asynchronous operation, additional information than just the |
| 179 | * cipher handle must be supplied to the kernel crypto API. That additional |
| 180 | * information is given by filling in the skcipher_request data structure. |
| 181 | * |
| 182 | * For the symmetric key cipher API, the state is maintained with the tfm |
| 183 | * cipher handle. A single tfm can be used across multiple calls and in |
| 184 | * parallel. For asynchronous block cipher calls, context data supplied and |
| 185 | * only used by the caller can be referenced the request data structure in |
| 186 | * addition to the IV used for the cipher request. The maintenance of such |
| 187 | * state information would be important for a crypto driver implementer to |
| 188 | * have, because when calling the callback function upon completion of the |
| 189 | * cipher operation, that callback function may need some information about |
| 190 | * which operation just finished if it invoked multiple in parallel. This |
| 191 | * state information is unused by the kernel crypto API. |
| 192 | */ |
| 193 | |
| 194 | static inline struct crypto_skcipher *__crypto_skcipher_cast( |
| 195 | struct crypto_tfm *tfm) |
| 196 | { |
| 197 | return container_of(tfm, struct crypto_skcipher, base); |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * crypto_alloc_skcipher() - allocate symmetric key cipher handle |
| 202 | * @alg_name: is the cra_name / name or cra_driver_name / driver name of the |
| 203 | * skcipher cipher |
| 204 | * @type: specifies the type of the cipher |
| 205 | * @mask: specifies the mask for the cipher |
| 206 | * |
| 207 | * Allocate a cipher handle for an skcipher. The returned struct |
| 208 | * crypto_skcipher is the cipher handle that is required for any subsequent |
| 209 | * API invocation for that skcipher. |
| 210 | * |
| 211 | * Return: allocated cipher handle in case of success; IS_ERR() is true in case |
| 212 | * of an error, PTR_ERR() returns the error code. |
| 213 | */ |
| 214 | struct crypto_skcipher *crypto_alloc_skcipher(const char *alg_name, |
| 215 | u32 type, u32 mask); |
| 216 | |
Kees Cook | b350bee | 2018-09-18 19:10:38 -0700 | [diff] [blame^] | 217 | struct crypto_sync_skcipher *crypto_alloc_sync_skcipher(const char *alg_name, |
| 218 | u32 type, u32 mask); |
| 219 | |
Herbert Xu | 7a7ffe6 | 2015-08-20 15:21:45 +0800 | [diff] [blame] | 220 | static inline struct crypto_tfm *crypto_skcipher_tfm( |
| 221 | struct crypto_skcipher *tfm) |
| 222 | { |
| 223 | return &tfm->base; |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * crypto_free_skcipher() - zeroize and free cipher handle |
| 228 | * @tfm: cipher handle to be freed |
| 229 | */ |
| 230 | static inline void crypto_free_skcipher(struct crypto_skcipher *tfm) |
| 231 | { |
| 232 | crypto_destroy_tfm(tfm, crypto_skcipher_tfm(tfm)); |
| 233 | } |
| 234 | |
Kees Cook | b350bee | 2018-09-18 19:10:38 -0700 | [diff] [blame^] | 235 | static inline void crypto_free_sync_skcipher(struct crypto_sync_skcipher *tfm) |
| 236 | { |
| 237 | crypto_free_skcipher(&tfm->base); |
| 238 | } |
| 239 | |
Herbert Xu | 7a7ffe6 | 2015-08-20 15:21:45 +0800 | [diff] [blame] | 240 | /** |
| 241 | * crypto_has_skcipher() - Search for the availability of an skcipher. |
| 242 | * @alg_name: is the cra_name / name or cra_driver_name / driver name of the |
| 243 | * skcipher |
| 244 | * @type: specifies the type of the cipher |
| 245 | * @mask: specifies the mask for the cipher |
| 246 | * |
| 247 | * Return: true when the skcipher is known to the kernel crypto API; false |
| 248 | * otherwise |
| 249 | */ |
| 250 | static inline int crypto_has_skcipher(const char *alg_name, u32 type, |
| 251 | u32 mask) |
| 252 | { |
| 253 | return crypto_has_alg(alg_name, crypto_skcipher_type(type), |
| 254 | crypto_skcipher_mask(mask)); |
| 255 | } |
| 256 | |
Herbert Xu | 4e6c3df | 2016-07-12 13:17:31 +0800 | [diff] [blame] | 257 | /** |
| 258 | * crypto_has_skcipher2() - Search for the availability of an skcipher. |
| 259 | * @alg_name: is the cra_name / name or cra_driver_name / driver name of the |
| 260 | * skcipher |
| 261 | * @type: specifies the type of the skcipher |
| 262 | * @mask: specifies the mask for the skcipher |
| 263 | * |
| 264 | * Return: true when the skcipher is known to the kernel crypto API; false |
| 265 | * otherwise |
| 266 | */ |
| 267 | int crypto_has_skcipher2(const char *alg_name, u32 type, u32 mask); |
| 268 | |
Herbert Xu | a2d382a | 2016-01-26 22:14:36 +0800 | [diff] [blame] | 269 | static inline const char *crypto_skcipher_driver_name( |
| 270 | struct crypto_skcipher *tfm) |
| 271 | { |
Herbert Xu | 92b3cad | 2016-02-01 21:36:51 +0800 | [diff] [blame] | 272 | return crypto_tfm_alg_driver_name(crypto_skcipher_tfm(tfm)); |
Herbert Xu | a2d382a | 2016-01-26 22:14:36 +0800 | [diff] [blame] | 273 | } |
| 274 | |
Herbert Xu | 4e6c3df | 2016-07-12 13:17:31 +0800 | [diff] [blame] | 275 | static inline struct skcipher_alg *crypto_skcipher_alg( |
| 276 | struct crypto_skcipher *tfm) |
| 277 | { |
| 278 | return container_of(crypto_skcipher_tfm(tfm)->__crt_alg, |
| 279 | struct skcipher_alg, base); |
| 280 | } |
| 281 | |
| 282 | static inline unsigned int crypto_skcipher_alg_ivsize(struct skcipher_alg *alg) |
| 283 | { |
| 284 | if ((alg->base.cra_flags & CRYPTO_ALG_TYPE_MASK) == |
| 285 | CRYPTO_ALG_TYPE_BLKCIPHER) |
| 286 | return alg->base.cra_blkcipher.ivsize; |
| 287 | |
| 288 | if (alg->base.cra_ablkcipher.encrypt) |
| 289 | return alg->base.cra_ablkcipher.ivsize; |
| 290 | |
| 291 | return alg->ivsize; |
| 292 | } |
| 293 | |
Herbert Xu | 7a7ffe6 | 2015-08-20 15:21:45 +0800 | [diff] [blame] | 294 | /** |
| 295 | * crypto_skcipher_ivsize() - obtain IV size |
| 296 | * @tfm: cipher handle |
| 297 | * |
| 298 | * The size of the IV for the skcipher referenced by the cipher handle is |
| 299 | * returned. This IV size may be zero if the cipher does not need an IV. |
| 300 | * |
| 301 | * Return: IV size in bytes |
| 302 | */ |
| 303 | static inline unsigned int crypto_skcipher_ivsize(struct crypto_skcipher *tfm) |
| 304 | { |
| 305 | return tfm->ivsize; |
| 306 | } |
| 307 | |
Kees Cook | b350bee | 2018-09-18 19:10:38 -0700 | [diff] [blame^] | 308 | static inline unsigned int crypto_sync_skcipher_ivsize( |
| 309 | struct crypto_sync_skcipher *tfm) |
| 310 | { |
| 311 | return crypto_skcipher_ivsize(&tfm->base); |
| 312 | } |
| 313 | |
Herbert Xu | 4e6c3df | 2016-07-12 13:17:31 +0800 | [diff] [blame] | 314 | static inline unsigned int crypto_skcipher_alg_chunksize( |
| 315 | struct skcipher_alg *alg) |
| 316 | { |
| 317 | if ((alg->base.cra_flags & CRYPTO_ALG_TYPE_MASK) == |
| 318 | CRYPTO_ALG_TYPE_BLKCIPHER) |
| 319 | return alg->base.cra_blocksize; |
| 320 | |
| 321 | if (alg->base.cra_ablkcipher.encrypt) |
| 322 | return alg->base.cra_blocksize; |
| 323 | |
| 324 | return alg->chunksize; |
| 325 | } |
| 326 | |
Ard Biesheuvel | c821f6a | 2016-12-29 14:09:08 +0000 | [diff] [blame] | 327 | static inline unsigned int crypto_skcipher_alg_walksize( |
| 328 | struct skcipher_alg *alg) |
| 329 | { |
| 330 | if ((alg->base.cra_flags & CRYPTO_ALG_TYPE_MASK) == |
| 331 | CRYPTO_ALG_TYPE_BLKCIPHER) |
| 332 | return alg->base.cra_blocksize; |
| 333 | |
| 334 | if (alg->base.cra_ablkcipher.encrypt) |
| 335 | return alg->base.cra_blocksize; |
| 336 | |
| 337 | return alg->walksize; |
| 338 | } |
| 339 | |
Herbert Xu | 4e6c3df | 2016-07-12 13:17:31 +0800 | [diff] [blame] | 340 | /** |
| 341 | * crypto_skcipher_chunksize() - obtain chunk size |
| 342 | * @tfm: cipher handle |
| 343 | * |
| 344 | * The block size is set to one for ciphers such as CTR. However, |
| 345 | * you still need to provide incremental updates in multiples of |
| 346 | * the underlying block size as the IV does not have sub-block |
| 347 | * granularity. This is known in this API as the chunk size. |
| 348 | * |
| 349 | * Return: chunk size in bytes |
| 350 | */ |
| 351 | static inline unsigned int crypto_skcipher_chunksize( |
| 352 | struct crypto_skcipher *tfm) |
| 353 | { |
| 354 | return crypto_skcipher_alg_chunksize(crypto_skcipher_alg(tfm)); |
| 355 | } |
| 356 | |
Herbert Xu | 7a7ffe6 | 2015-08-20 15:21:45 +0800 | [diff] [blame] | 357 | /** |
Ard Biesheuvel | c821f6a | 2016-12-29 14:09:08 +0000 | [diff] [blame] | 358 | * crypto_skcipher_walksize() - obtain walk size |
| 359 | * @tfm: cipher handle |
| 360 | * |
| 361 | * In some cases, algorithms can only perform optimally when operating on |
| 362 | * multiple blocks in parallel. This is reflected by the walksize, which |
| 363 | * must be a multiple of the chunksize (or equal if the concern does not |
| 364 | * apply) |
| 365 | * |
| 366 | * Return: walk size in bytes |
| 367 | */ |
| 368 | static inline unsigned int crypto_skcipher_walksize( |
| 369 | struct crypto_skcipher *tfm) |
| 370 | { |
| 371 | return crypto_skcipher_alg_walksize(crypto_skcipher_alg(tfm)); |
| 372 | } |
| 373 | |
| 374 | /** |
Herbert Xu | 7a7ffe6 | 2015-08-20 15:21:45 +0800 | [diff] [blame] | 375 | * crypto_skcipher_blocksize() - obtain block size of cipher |
| 376 | * @tfm: cipher handle |
| 377 | * |
| 378 | * The block size for the skcipher referenced with the cipher handle is |
| 379 | * returned. The caller may use that information to allocate appropriate |
| 380 | * memory for the data returned by the encryption or decryption operation |
| 381 | * |
| 382 | * Return: block size of cipher |
| 383 | */ |
| 384 | static inline unsigned int crypto_skcipher_blocksize( |
| 385 | struct crypto_skcipher *tfm) |
| 386 | { |
| 387 | return crypto_tfm_alg_blocksize(crypto_skcipher_tfm(tfm)); |
| 388 | } |
| 389 | |
Kees Cook | b350bee | 2018-09-18 19:10:38 -0700 | [diff] [blame^] | 390 | static inline unsigned int crypto_sync_skcipher_blocksize( |
| 391 | struct crypto_sync_skcipher *tfm) |
| 392 | { |
| 393 | return crypto_skcipher_blocksize(&tfm->base); |
| 394 | } |
| 395 | |
Herbert Xu | 7a7ffe6 | 2015-08-20 15:21:45 +0800 | [diff] [blame] | 396 | static inline unsigned int crypto_skcipher_alignmask( |
| 397 | struct crypto_skcipher *tfm) |
| 398 | { |
| 399 | return crypto_tfm_alg_alignmask(crypto_skcipher_tfm(tfm)); |
| 400 | } |
| 401 | |
| 402 | static inline u32 crypto_skcipher_get_flags(struct crypto_skcipher *tfm) |
| 403 | { |
| 404 | return crypto_tfm_get_flags(crypto_skcipher_tfm(tfm)); |
| 405 | } |
| 406 | |
| 407 | static inline void crypto_skcipher_set_flags(struct crypto_skcipher *tfm, |
| 408 | u32 flags) |
| 409 | { |
| 410 | crypto_tfm_set_flags(crypto_skcipher_tfm(tfm), flags); |
| 411 | } |
| 412 | |
| 413 | static inline void crypto_skcipher_clear_flags(struct crypto_skcipher *tfm, |
| 414 | u32 flags) |
| 415 | { |
| 416 | crypto_tfm_clear_flags(crypto_skcipher_tfm(tfm), flags); |
| 417 | } |
| 418 | |
Kees Cook | b350bee | 2018-09-18 19:10:38 -0700 | [diff] [blame^] | 419 | static inline u32 crypto_sync_skcipher_get_flags( |
| 420 | struct crypto_sync_skcipher *tfm) |
| 421 | { |
| 422 | return crypto_skcipher_get_flags(&tfm->base); |
| 423 | } |
| 424 | |
| 425 | static inline void crypto_sync_skcipher_set_flags( |
| 426 | struct crypto_sync_skcipher *tfm, u32 flags) |
| 427 | { |
| 428 | crypto_skcipher_set_flags(&tfm->base, flags); |
| 429 | } |
| 430 | |
| 431 | static inline void crypto_sync_skcipher_clear_flags( |
| 432 | struct crypto_sync_skcipher *tfm, u32 flags) |
| 433 | { |
| 434 | crypto_skcipher_clear_flags(&tfm->base, flags); |
| 435 | } |
| 436 | |
Herbert Xu | 7a7ffe6 | 2015-08-20 15:21:45 +0800 | [diff] [blame] | 437 | /** |
| 438 | * crypto_skcipher_setkey() - set key for cipher |
| 439 | * @tfm: cipher handle |
| 440 | * @key: buffer holding the key |
| 441 | * @keylen: length of the key in bytes |
| 442 | * |
| 443 | * The caller provided key is set for the skcipher referenced by the cipher |
| 444 | * handle. |
| 445 | * |
| 446 | * Note, the key length determines the cipher type. Many block ciphers implement |
| 447 | * different cipher modes depending on the key size, such as AES-128 vs AES-192 |
| 448 | * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128 |
| 449 | * is performed. |
| 450 | * |
| 451 | * Return: 0 if the setting of the key was successful; < 0 if an error occurred |
| 452 | */ |
| 453 | static inline int crypto_skcipher_setkey(struct crypto_skcipher *tfm, |
| 454 | const u8 *key, unsigned int keylen) |
| 455 | { |
| 456 | return tfm->setkey(tfm, key, keylen); |
| 457 | } |
| 458 | |
Kees Cook | b350bee | 2018-09-18 19:10:38 -0700 | [diff] [blame^] | 459 | static inline int crypto_sync_skcipher_setkey(struct crypto_sync_skcipher *tfm, |
| 460 | const u8 *key, unsigned int keylen) |
| 461 | { |
| 462 | return crypto_skcipher_setkey(&tfm->base, key, keylen); |
| 463 | } |
| 464 | |
Herbert Xu | 973fb3f | 2016-01-21 17:10:56 +0800 | [diff] [blame] | 465 | static inline unsigned int crypto_skcipher_default_keysize( |
| 466 | struct crypto_skcipher *tfm) |
| 467 | { |
| 468 | return tfm->keysize; |
Herbert Xu | a1383cd | 2016-01-11 21:26:50 +0800 | [diff] [blame] | 469 | } |
| 470 | |
Herbert Xu | 7a7ffe6 | 2015-08-20 15:21:45 +0800 | [diff] [blame] | 471 | /** |
| 472 | * crypto_skcipher_reqtfm() - obtain cipher handle from request |
| 473 | * @req: skcipher_request out of which the cipher handle is to be obtained |
| 474 | * |
| 475 | * Return the crypto_skcipher handle when furnishing an skcipher_request |
| 476 | * data structure. |
| 477 | * |
| 478 | * Return: crypto_skcipher handle |
| 479 | */ |
| 480 | static inline struct crypto_skcipher *crypto_skcipher_reqtfm( |
| 481 | struct skcipher_request *req) |
| 482 | { |
| 483 | return __crypto_skcipher_cast(req->base.tfm); |
| 484 | } |
| 485 | |
Kees Cook | b350bee | 2018-09-18 19:10:38 -0700 | [diff] [blame^] | 486 | static inline struct crypto_sync_skcipher *crypto_sync_skcipher_reqtfm( |
| 487 | struct skcipher_request *req) |
| 488 | { |
| 489 | struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); |
| 490 | |
| 491 | return container_of(tfm, struct crypto_sync_skcipher, base); |
| 492 | } |
| 493 | |
Herbert Xu | 7a7ffe6 | 2015-08-20 15:21:45 +0800 | [diff] [blame] | 494 | /** |
| 495 | * crypto_skcipher_encrypt() - encrypt plaintext |
| 496 | * @req: reference to the skcipher_request handle that holds all information |
| 497 | * needed to perform the cipher operation |
| 498 | * |
| 499 | * Encrypt plaintext data using the skcipher_request handle. That data |
| 500 | * structure and how it is filled with data is discussed with the |
| 501 | * skcipher_request_* functions. |
| 502 | * |
| 503 | * Return: 0 if the cipher operation was successful; < 0 if an error occurred |
| 504 | */ |
| 505 | static inline int crypto_skcipher_encrypt(struct skcipher_request *req) |
| 506 | { |
| 507 | struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); |
| 508 | |
Eric Biggers | f8d33fa | 2018-01-03 11:16:29 -0800 | [diff] [blame] | 509 | if (crypto_skcipher_get_flags(tfm) & CRYPTO_TFM_NEED_KEY) |
| 510 | return -ENOKEY; |
| 511 | |
Herbert Xu | 7a7ffe6 | 2015-08-20 15:21:45 +0800 | [diff] [blame] | 512 | return tfm->encrypt(req); |
| 513 | } |
| 514 | |
| 515 | /** |
| 516 | * crypto_skcipher_decrypt() - decrypt ciphertext |
| 517 | * @req: reference to the skcipher_request handle that holds all information |
| 518 | * needed to perform the cipher operation |
| 519 | * |
| 520 | * Decrypt ciphertext data using the skcipher_request handle. That data |
| 521 | * structure and how it is filled with data is discussed with the |
| 522 | * skcipher_request_* functions. |
| 523 | * |
| 524 | * Return: 0 if the cipher operation was successful; < 0 if an error occurred |
| 525 | */ |
| 526 | static inline int crypto_skcipher_decrypt(struct skcipher_request *req) |
| 527 | { |
| 528 | struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); |
| 529 | |
Eric Biggers | f8d33fa | 2018-01-03 11:16:29 -0800 | [diff] [blame] | 530 | if (crypto_skcipher_get_flags(tfm) & CRYPTO_TFM_NEED_KEY) |
| 531 | return -ENOKEY; |
| 532 | |
Herbert Xu | 7a7ffe6 | 2015-08-20 15:21:45 +0800 | [diff] [blame] | 533 | return tfm->decrypt(req); |
| 534 | } |
| 535 | |
| 536 | /** |
| 537 | * DOC: Symmetric Key Cipher Request Handle |
| 538 | * |
| 539 | * The skcipher_request data structure contains all pointers to data |
| 540 | * required for the symmetric key cipher operation. This includes the cipher |
| 541 | * handle (which can be used by multiple skcipher_request instances), pointer |
| 542 | * to plaintext and ciphertext, asynchronous callback function, etc. It acts |
| 543 | * as a handle to the skcipher_request_* API calls in a similar way as |
| 544 | * skcipher handle to the crypto_skcipher_* API calls. |
| 545 | */ |
| 546 | |
| 547 | /** |
| 548 | * crypto_skcipher_reqsize() - obtain size of the request data structure |
| 549 | * @tfm: cipher handle |
| 550 | * |
| 551 | * Return: number of bytes |
| 552 | */ |
| 553 | static inline unsigned int crypto_skcipher_reqsize(struct crypto_skcipher *tfm) |
| 554 | { |
| 555 | return tfm->reqsize; |
| 556 | } |
| 557 | |
| 558 | /** |
| 559 | * skcipher_request_set_tfm() - update cipher handle reference in request |
| 560 | * @req: request handle to be modified |
| 561 | * @tfm: cipher handle that shall be added to the request handle |
| 562 | * |
| 563 | * Allow the caller to replace the existing skcipher handle in the request |
| 564 | * data structure with a different one. |
| 565 | */ |
| 566 | static inline void skcipher_request_set_tfm(struct skcipher_request *req, |
| 567 | struct crypto_skcipher *tfm) |
| 568 | { |
| 569 | req->base.tfm = crypto_skcipher_tfm(tfm); |
| 570 | } |
| 571 | |
Kees Cook | b350bee | 2018-09-18 19:10:38 -0700 | [diff] [blame^] | 572 | static inline void skcipher_request_set_sync_tfm(struct skcipher_request *req, |
| 573 | struct crypto_sync_skcipher *tfm) |
| 574 | { |
| 575 | skcipher_request_set_tfm(req, &tfm->base); |
| 576 | } |
| 577 | |
Herbert Xu | 7a7ffe6 | 2015-08-20 15:21:45 +0800 | [diff] [blame] | 578 | static inline struct skcipher_request *skcipher_request_cast( |
| 579 | struct crypto_async_request *req) |
| 580 | { |
| 581 | return container_of(req, struct skcipher_request, base); |
| 582 | } |
| 583 | |
| 584 | /** |
| 585 | * skcipher_request_alloc() - allocate request data structure |
| 586 | * @tfm: cipher handle to be registered with the request |
| 587 | * @gfp: memory allocation flag that is handed to kmalloc by the API call. |
| 588 | * |
| 589 | * Allocate the request data structure that must be used with the skcipher |
| 590 | * encrypt and decrypt API calls. During the allocation, the provided skcipher |
| 591 | * handle is registered in the request data structure. |
| 592 | * |
Eric Biggers | 6eae29e | 2016-04-02 10:54:56 -0500 | [diff] [blame] | 593 | * Return: allocated request handle in case of success, or NULL if out of memory |
Herbert Xu | 7a7ffe6 | 2015-08-20 15:21:45 +0800 | [diff] [blame] | 594 | */ |
| 595 | static inline struct skcipher_request *skcipher_request_alloc( |
| 596 | struct crypto_skcipher *tfm, gfp_t gfp) |
| 597 | { |
| 598 | struct skcipher_request *req; |
| 599 | |
| 600 | req = kmalloc(sizeof(struct skcipher_request) + |
| 601 | crypto_skcipher_reqsize(tfm), gfp); |
| 602 | |
| 603 | if (likely(req)) |
| 604 | skcipher_request_set_tfm(req, tfm); |
| 605 | |
| 606 | return req; |
| 607 | } |
| 608 | |
| 609 | /** |
| 610 | * skcipher_request_free() - zeroize and free request data structure |
| 611 | * @req: request data structure cipher handle to be freed |
| 612 | */ |
| 613 | static inline void skcipher_request_free(struct skcipher_request *req) |
| 614 | { |
| 615 | kzfree(req); |
| 616 | } |
| 617 | |
Herbert Xu | 1aaa753 | 2016-01-22 23:21:10 +0800 | [diff] [blame] | 618 | static inline void skcipher_request_zero(struct skcipher_request *req) |
| 619 | { |
| 620 | struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); |
| 621 | |
| 622 | memzero_explicit(req, sizeof(*req) + crypto_skcipher_reqsize(tfm)); |
| 623 | } |
| 624 | |
Herbert Xu | 7a7ffe6 | 2015-08-20 15:21:45 +0800 | [diff] [blame] | 625 | /** |
| 626 | * skcipher_request_set_callback() - set asynchronous callback function |
| 627 | * @req: request handle |
| 628 | * @flags: specify zero or an ORing of the flags |
Stephan Mueller | 0184cfe | 2016-10-21 04:57:27 +0200 | [diff] [blame] | 629 | * CRYPTO_TFM_REQ_MAY_BACKLOG the request queue may back log and |
Herbert Xu | 7a7ffe6 | 2015-08-20 15:21:45 +0800 | [diff] [blame] | 630 | * increase the wait queue beyond the initial maximum size; |
| 631 | * CRYPTO_TFM_REQ_MAY_SLEEP the request processing may sleep |
| 632 | * @compl: callback function pointer to be registered with the request handle |
| 633 | * @data: The data pointer refers to memory that is not used by the kernel |
| 634 | * crypto API, but provided to the callback function for it to use. Here, |
| 635 | * the caller can provide a reference to memory the callback function can |
| 636 | * operate on. As the callback function is invoked asynchronously to the |
| 637 | * related functionality, it may need to access data structures of the |
| 638 | * related functionality which can be referenced using this pointer. The |
| 639 | * callback function can access the memory via the "data" field in the |
| 640 | * crypto_async_request data structure provided to the callback function. |
| 641 | * |
| 642 | * This function allows setting the callback function that is triggered once the |
| 643 | * cipher operation completes. |
| 644 | * |
| 645 | * The callback function is registered with the skcipher_request handle and |
Stephan Mueller | 0184cfe | 2016-10-21 04:57:27 +0200 | [diff] [blame] | 646 | * must comply with the following template:: |
Herbert Xu | 7a7ffe6 | 2015-08-20 15:21:45 +0800 | [diff] [blame] | 647 | * |
| 648 | * void callback_function(struct crypto_async_request *req, int error) |
| 649 | */ |
| 650 | static inline void skcipher_request_set_callback(struct skcipher_request *req, |
| 651 | u32 flags, |
| 652 | crypto_completion_t compl, |
| 653 | void *data) |
| 654 | { |
| 655 | req->base.complete = compl; |
| 656 | req->base.data = data; |
| 657 | req->base.flags = flags; |
| 658 | } |
| 659 | |
| 660 | /** |
| 661 | * skcipher_request_set_crypt() - set data buffers |
| 662 | * @req: request handle |
| 663 | * @src: source scatter / gather list |
| 664 | * @dst: destination scatter / gather list |
| 665 | * @cryptlen: number of bytes to process from @src |
| 666 | * @iv: IV for the cipher operation which must comply with the IV size defined |
| 667 | * by crypto_skcipher_ivsize |
| 668 | * |
| 669 | * This function allows setting of the source data and destination data |
| 670 | * scatter / gather lists. |
| 671 | * |
| 672 | * For encryption, the source is treated as the plaintext and the |
| 673 | * destination is the ciphertext. For a decryption operation, the use is |
| 674 | * reversed - the source is the ciphertext and the destination is the plaintext. |
| 675 | */ |
| 676 | static inline void skcipher_request_set_crypt( |
| 677 | struct skcipher_request *req, |
| 678 | struct scatterlist *src, struct scatterlist *dst, |
| 679 | unsigned int cryptlen, void *iv) |
| 680 | { |
| 681 | req->src = src; |
| 682 | req->dst = dst; |
| 683 | req->cryptlen = cryptlen; |
| 684 | req->iv = iv; |
| 685 | } |
| 686 | |
Herbert Xu | 61da88e | 2007-12-17 21:51:27 +0800 | [diff] [blame] | 687 | #endif /* _CRYPTO_SKCIPHER_H */ |
| 688 | |