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