crypto: skcipher - make chunksize and walksize accessors internal
[linux-2.6-block.git] / include / crypto / skcipher.h
CommitLineData
61da88e2
HX
1/*
2 * Symmetric key ciphers.
3 *
7a7ffe65 4 * Copyright (c) 2007-2015 Herbert Xu <herbert@gondor.apana.org.au>
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 */
29struct 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
42struct 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
55struct 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 */
116struct 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
176static 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 */
196struct crypto_skcipher *crypto_alloc_skcipher(const char *alg_name,
197 u32 type, u32 mask);
198
b350bee5
KC
199struct crypto_sync_skcipher *crypto_alloc_sync_skcipher(const char *alg_name,
200 u32 type, u32 mask);
201
7a7ffe65
HX
202static 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 */
212static inline void crypto_free_skcipher(struct crypto_skcipher *tfm)
213{
214 crypto_destroy_tfm(tfm, crypto_skcipher_tfm(tfm));
215}
216
b350bee5
KC
217static 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 */
232static 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 */
249int crypto_has_skcipher2(const char *alg_name, u32 type, u32 mask);
250
a2d382a4
HX
251static 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
257static 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
264static 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 */
285static inline unsigned int crypto_skcipher_ivsize(struct crypto_skcipher *tfm)
286{
287 return tfm->ivsize;
288}
289
b350bee5
KC
290static inline unsigned int crypto_sync_skcipher_ivsize(
291 struct crypto_sync_skcipher *tfm)
292{
293 return crypto_skcipher_ivsize(&tfm->base);
294}
295
7a7ffe65
HX
296/**
297 * crypto_skcipher_blocksize() - obtain block size of cipher
298 * @tfm: cipher handle
299 *
300 * The block size for the skcipher referenced with the cipher handle is
301 * returned. The caller may use that information to allocate appropriate
302 * memory for the data returned by the encryption or decryption operation
303 *
304 * Return: block size of cipher
305 */
306static inline unsigned int crypto_skcipher_blocksize(
307 struct crypto_skcipher *tfm)
308{
309 return crypto_tfm_alg_blocksize(crypto_skcipher_tfm(tfm));
310}
311
b350bee5
KC
312static inline unsigned int crypto_sync_skcipher_blocksize(
313 struct crypto_sync_skcipher *tfm)
314{
315 return crypto_skcipher_blocksize(&tfm->base);
316}
317
7a7ffe65
HX
318static inline unsigned int crypto_skcipher_alignmask(
319 struct crypto_skcipher *tfm)
320{
321 return crypto_tfm_alg_alignmask(crypto_skcipher_tfm(tfm));
322}
323
324static inline u32 crypto_skcipher_get_flags(struct crypto_skcipher *tfm)
325{
326 return crypto_tfm_get_flags(crypto_skcipher_tfm(tfm));
327}
328
329static inline void crypto_skcipher_set_flags(struct crypto_skcipher *tfm,
330 u32 flags)
331{
332 crypto_tfm_set_flags(crypto_skcipher_tfm(tfm), flags);
333}
334
335static inline void crypto_skcipher_clear_flags(struct crypto_skcipher *tfm,
336 u32 flags)
337{
338 crypto_tfm_clear_flags(crypto_skcipher_tfm(tfm), flags);
339}
340
b350bee5
KC
341static inline u32 crypto_sync_skcipher_get_flags(
342 struct crypto_sync_skcipher *tfm)
343{
344 return crypto_skcipher_get_flags(&tfm->base);
345}
346
347static inline void crypto_sync_skcipher_set_flags(
348 struct crypto_sync_skcipher *tfm, u32 flags)
349{
350 crypto_skcipher_set_flags(&tfm->base, flags);
351}
352
353static inline void crypto_sync_skcipher_clear_flags(
354 struct crypto_sync_skcipher *tfm, u32 flags)
355{
356 crypto_skcipher_clear_flags(&tfm->base, flags);
357}
358
7a7ffe65
HX
359/**
360 * crypto_skcipher_setkey() - set key for cipher
361 * @tfm: cipher handle
362 * @key: buffer holding the key
363 * @keylen: length of the key in bytes
364 *
365 * The caller provided key is set for the skcipher referenced by the cipher
366 * handle.
367 *
368 * Note, the key length determines the cipher type. Many block ciphers implement
369 * different cipher modes depending on the key size, such as AES-128 vs AES-192
370 * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128
371 * is performed.
372 *
373 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
374 */
375static inline int crypto_skcipher_setkey(struct crypto_skcipher *tfm,
376 const u8 *key, unsigned int keylen)
377{
378 return tfm->setkey(tfm, key, keylen);
379}
380
b350bee5
KC
381static inline int crypto_sync_skcipher_setkey(struct crypto_sync_skcipher *tfm,
382 const u8 *key, unsigned int keylen)
383{
384 return crypto_skcipher_setkey(&tfm->base, key, keylen);
385}
386
973fb3fb
HX
387static inline unsigned int crypto_skcipher_default_keysize(
388 struct crypto_skcipher *tfm)
389{
390 return tfm->keysize;
a1383cd8
HX
391}
392
7a7ffe65
HX
393/**
394 * crypto_skcipher_reqtfm() - obtain cipher handle from request
395 * @req: skcipher_request out of which the cipher handle is to be obtained
396 *
397 * Return the crypto_skcipher handle when furnishing an skcipher_request
398 * data structure.
399 *
400 * Return: crypto_skcipher handle
401 */
402static inline struct crypto_skcipher *crypto_skcipher_reqtfm(
403 struct skcipher_request *req)
404{
405 return __crypto_skcipher_cast(req->base.tfm);
406}
407
b350bee5
KC
408static inline struct crypto_sync_skcipher *crypto_sync_skcipher_reqtfm(
409 struct skcipher_request *req)
410{
411 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
412
413 return container_of(tfm, struct crypto_sync_skcipher, base);
414}
415
7a7ffe65
HX
416/**
417 * crypto_skcipher_encrypt() - encrypt plaintext
418 * @req: reference to the skcipher_request handle that holds all information
419 * needed to perform the cipher operation
420 *
421 * Encrypt plaintext data using the skcipher_request handle. That data
422 * structure and how it is filled with data is discussed with the
423 * skcipher_request_* functions.
424 *
425 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
426 */
81bcbb1e 427int crypto_skcipher_encrypt(struct skcipher_request *req);
7a7ffe65
HX
428
429/**
430 * crypto_skcipher_decrypt() - decrypt ciphertext
431 * @req: reference to the skcipher_request handle that holds all information
432 * needed to perform the cipher operation
433 *
434 * Decrypt ciphertext data using the skcipher_request handle. That data
435 * structure and how it is filled with data is discussed with the
436 * skcipher_request_* functions.
437 *
438 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
439 */
81bcbb1e 440int crypto_skcipher_decrypt(struct skcipher_request *req);
7a7ffe65
HX
441
442/**
443 * DOC: Symmetric Key Cipher Request Handle
444 *
445 * The skcipher_request data structure contains all pointers to data
446 * required for the symmetric key cipher operation. This includes the cipher
447 * handle (which can be used by multiple skcipher_request instances), pointer
448 * to plaintext and ciphertext, asynchronous callback function, etc. It acts
449 * as a handle to the skcipher_request_* API calls in a similar way as
450 * skcipher handle to the crypto_skcipher_* API calls.
451 */
452
453/**
454 * crypto_skcipher_reqsize() - obtain size of the request data structure
455 * @tfm: cipher handle
456 *
457 * Return: number of bytes
458 */
459static inline unsigned int crypto_skcipher_reqsize(struct crypto_skcipher *tfm)
460{
461 return tfm->reqsize;
462}
463
464/**
465 * skcipher_request_set_tfm() - update cipher handle reference in request
466 * @req: request handle to be modified
467 * @tfm: cipher handle that shall be added to the request handle
468 *
469 * Allow the caller to replace the existing skcipher handle in the request
470 * data structure with a different one.
471 */
472static inline void skcipher_request_set_tfm(struct skcipher_request *req,
473 struct crypto_skcipher *tfm)
474{
475 req->base.tfm = crypto_skcipher_tfm(tfm);
476}
477
b350bee5
KC
478static inline void skcipher_request_set_sync_tfm(struct skcipher_request *req,
479 struct crypto_sync_skcipher *tfm)
480{
481 skcipher_request_set_tfm(req, &tfm->base);
482}
483
7a7ffe65
HX
484static inline struct skcipher_request *skcipher_request_cast(
485 struct crypto_async_request *req)
486{
487 return container_of(req, struct skcipher_request, base);
488}
489
490/**
491 * skcipher_request_alloc() - allocate request data structure
492 * @tfm: cipher handle to be registered with the request
493 * @gfp: memory allocation flag that is handed to kmalloc by the API call.
494 *
495 * Allocate the request data structure that must be used with the skcipher
496 * encrypt and decrypt API calls. During the allocation, the provided skcipher
497 * handle is registered in the request data structure.
498 *
6eae29e7 499 * Return: allocated request handle in case of success, or NULL if out of memory
7a7ffe65
HX
500 */
501static inline struct skcipher_request *skcipher_request_alloc(
502 struct crypto_skcipher *tfm, gfp_t gfp)
503{
504 struct skcipher_request *req;
505
506 req = kmalloc(sizeof(struct skcipher_request) +
507 crypto_skcipher_reqsize(tfm), gfp);
508
509 if (likely(req))
510 skcipher_request_set_tfm(req, tfm);
511
512 return req;
513}
514
515/**
516 * skcipher_request_free() - zeroize and free request data structure
517 * @req: request data structure cipher handle to be freed
518 */
519static inline void skcipher_request_free(struct skcipher_request *req)
520{
521 kzfree(req);
522}
523
1aaa753d
HX
524static inline void skcipher_request_zero(struct skcipher_request *req)
525{
526 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
527
528 memzero_explicit(req, sizeof(*req) + crypto_skcipher_reqsize(tfm));
529}
530
7a7ffe65
HX
531/**
532 * skcipher_request_set_callback() - set asynchronous callback function
533 * @req: request handle
534 * @flags: specify zero or an ORing of the flags
0184cfe7 535 * CRYPTO_TFM_REQ_MAY_BACKLOG the request queue may back log and
7a7ffe65
HX
536 * increase the wait queue beyond the initial maximum size;
537 * CRYPTO_TFM_REQ_MAY_SLEEP the request processing may sleep
538 * @compl: callback function pointer to be registered with the request handle
539 * @data: The data pointer refers to memory that is not used by the kernel
540 * crypto API, but provided to the callback function for it to use. Here,
541 * the caller can provide a reference to memory the callback function can
542 * operate on. As the callback function is invoked asynchronously to the
543 * related functionality, it may need to access data structures of the
544 * related functionality which can be referenced using this pointer. The
545 * callback function can access the memory via the "data" field in the
546 * crypto_async_request data structure provided to the callback function.
547 *
548 * This function allows setting the callback function that is triggered once the
549 * cipher operation completes.
550 *
551 * The callback function is registered with the skcipher_request handle and
0184cfe7 552 * must comply with the following template::
7a7ffe65
HX
553 *
554 * void callback_function(struct crypto_async_request *req, int error)
555 */
556static inline void skcipher_request_set_callback(struct skcipher_request *req,
557 u32 flags,
558 crypto_completion_t compl,
559 void *data)
560{
561 req->base.complete = compl;
562 req->base.data = data;
563 req->base.flags = flags;
564}
565
566/**
567 * skcipher_request_set_crypt() - set data buffers
568 * @req: request handle
569 * @src: source scatter / gather list
570 * @dst: destination scatter / gather list
571 * @cryptlen: number of bytes to process from @src
572 * @iv: IV for the cipher operation which must comply with the IV size defined
573 * by crypto_skcipher_ivsize
574 *
575 * This function allows setting of the source data and destination data
576 * scatter / gather lists.
577 *
578 * For encryption, the source is treated as the plaintext and the
579 * destination is the ciphertext. For a decryption operation, the use is
580 * reversed - the source is the ciphertext and the destination is the plaintext.
581 */
582static inline void skcipher_request_set_crypt(
583 struct skcipher_request *req,
584 struct scatterlist *src, struct scatterlist *dst,
585 unsigned int cryptlen, void *iv)
586{
587 req->src = src;
588 req->dst = dst;
589 req->cryptlen = cryptlen;
590 req->iv = iv;
591}
592
61da88e2
HX
593#endif /* _CRYPTO_SKCIPHER_H */
594