Linux 5.1.15
[linux/fpc-iii.git] / include / crypto / skcipher.h
blobe555294ed77fe2f6ac87fbbbbdf4771b35b59d4d
1 /*
2 * Symmetric key ciphers.
3 *
4 * Copyright (c) 2007-2015 Herbert Xu <herbert@gondor.apana.org.au>
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.
13 #ifndef _CRYPTO_SKCIPHER_H
14 #define _CRYPTO_SKCIPHER_H
16 #include <linux/crypto.h>
17 #include <linux/kernel.h>
18 #include <linux/slab.h>
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
29 struct skcipher_request {
30 unsigned int cryptlen;
32 u8 *iv;
34 struct scatterlist *src;
35 struct scatterlist *dst;
37 struct crypto_async_request base;
39 void *__ctx[] CRYPTO_MINALIGN_ATTR;
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);
48 unsigned int ivsize;
49 unsigned int reqsize;
50 unsigned int keysize;
52 struct crypto_tfm base;
55 struct crypto_sync_skcipher {
56 struct crypto_skcipher base;
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.
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.
112 * @base: Definition of a generic crypto algorithm.
114 * All fields except @ivsize are mandatory and must be filled.
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);
124 unsigned int min_keysize;
125 unsigned int max_keysize;
126 unsigned int ivsize;
127 unsigned int chunksize;
128 unsigned int walksize;
130 struct crypto_alg base;
133 #define MAX_SYNC_SKCIPHER_REQSIZE 384
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.
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
147 * DOC: Symmetric Key Cipher API
149 * Symmetric key cipher API is used with the ciphers of type
150 * CRYPTO_ALG_TYPE_SKCIPHER (listed as type "skcipher" in /proc/crypto).
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.
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.
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.
176 static inline struct crypto_skcipher *__crypto_skcipher_cast(
177 struct crypto_tfm *tfm)
179 return container_of(tfm, struct crypto_skcipher, base);
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
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.
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.
196 struct crypto_skcipher *crypto_alloc_skcipher(const char *alg_name,
197 u32 type, u32 mask);
199 struct crypto_sync_skcipher *crypto_alloc_sync_skcipher(const char *alg_name,
200 u32 type, u32 mask);
202 static inline struct crypto_tfm *crypto_skcipher_tfm(
203 struct crypto_skcipher *tfm)
205 return &tfm->base;
209 * crypto_free_skcipher() - zeroize and free cipher handle
210 * @tfm: cipher handle to be freed
212 static inline void crypto_free_skcipher(struct crypto_skcipher *tfm)
214 crypto_destroy_tfm(tfm, crypto_skcipher_tfm(tfm));
217 static inline void crypto_free_sync_skcipher(struct crypto_sync_skcipher *tfm)
219 crypto_free_skcipher(&tfm->base);
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
229 * Return: true when the skcipher is known to the kernel crypto API; false
230 * otherwise
232 static inline int crypto_has_skcipher(const char *alg_name, u32 type,
233 u32 mask)
235 return crypto_has_alg(alg_name, crypto_skcipher_type(type),
236 crypto_skcipher_mask(mask));
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
246 * Return: true when the skcipher is known to the kernel crypto API; false
247 * otherwise
249 int crypto_has_skcipher2(const char *alg_name, u32 type, u32 mask);
251 static inline const char *crypto_skcipher_driver_name(
252 struct crypto_skcipher *tfm)
254 return crypto_tfm_alg_driver_name(crypto_skcipher_tfm(tfm));
257 static inline struct skcipher_alg *crypto_skcipher_alg(
258 struct crypto_skcipher *tfm)
260 return container_of(crypto_skcipher_tfm(tfm)->__crt_alg,
261 struct skcipher_alg, base);
264 static inline unsigned int crypto_skcipher_alg_ivsize(struct skcipher_alg *alg)
266 if ((alg->base.cra_flags & CRYPTO_ALG_TYPE_MASK) ==
267 CRYPTO_ALG_TYPE_BLKCIPHER)
268 return alg->base.cra_blkcipher.ivsize;
270 if (alg->base.cra_ablkcipher.encrypt)
271 return alg->base.cra_ablkcipher.ivsize;
273 return alg->ivsize;
277 * crypto_skcipher_ivsize() - obtain IV size
278 * @tfm: cipher handle
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.
283 * Return: IV size in bytes
285 static inline unsigned int crypto_skcipher_ivsize(struct crypto_skcipher *tfm)
287 return tfm->ivsize;
290 static inline unsigned int crypto_sync_skcipher_ivsize(
291 struct crypto_sync_skcipher *tfm)
293 return crypto_skcipher_ivsize(&tfm->base);
296 static inline unsigned int crypto_skcipher_alg_chunksize(
297 struct skcipher_alg *alg)
299 if ((alg->base.cra_flags & CRYPTO_ALG_TYPE_MASK) ==
300 CRYPTO_ALG_TYPE_BLKCIPHER)
301 return alg->base.cra_blocksize;
303 if (alg->base.cra_ablkcipher.encrypt)
304 return alg->base.cra_blocksize;
306 return alg->chunksize;
309 static inline unsigned int crypto_skcipher_alg_walksize(
310 struct skcipher_alg *alg)
312 if ((alg->base.cra_flags & CRYPTO_ALG_TYPE_MASK) ==
313 CRYPTO_ALG_TYPE_BLKCIPHER)
314 return alg->base.cra_blocksize;
316 if (alg->base.cra_ablkcipher.encrypt)
317 return alg->base.cra_blocksize;
319 return alg->walksize;
323 * crypto_skcipher_chunksize() - obtain chunk size
324 * @tfm: cipher handle
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.
331 * Return: chunk size in bytes
333 static inline unsigned int crypto_skcipher_chunksize(
334 struct crypto_skcipher *tfm)
336 return crypto_skcipher_alg_chunksize(crypto_skcipher_alg(tfm));
340 * crypto_skcipher_walksize() - obtain walk size
341 * @tfm: cipher handle
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)
348 * Return: walk size in bytes
350 static inline unsigned int crypto_skcipher_walksize(
351 struct crypto_skcipher *tfm)
353 return crypto_skcipher_alg_walksize(crypto_skcipher_alg(tfm));
357 * crypto_skcipher_blocksize() - obtain block size of cipher
358 * @tfm: cipher handle
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
364 * Return: block size of cipher
366 static inline unsigned int crypto_skcipher_blocksize(
367 struct crypto_skcipher *tfm)
369 return crypto_tfm_alg_blocksize(crypto_skcipher_tfm(tfm));
372 static inline unsigned int crypto_sync_skcipher_blocksize(
373 struct crypto_sync_skcipher *tfm)
375 return crypto_skcipher_blocksize(&tfm->base);
378 static inline unsigned int crypto_skcipher_alignmask(
379 struct crypto_skcipher *tfm)
381 return crypto_tfm_alg_alignmask(crypto_skcipher_tfm(tfm));
384 static inline u32 crypto_skcipher_get_flags(struct crypto_skcipher *tfm)
386 return crypto_tfm_get_flags(crypto_skcipher_tfm(tfm));
389 static inline void crypto_skcipher_set_flags(struct crypto_skcipher *tfm,
390 u32 flags)
392 crypto_tfm_set_flags(crypto_skcipher_tfm(tfm), flags);
395 static inline void crypto_skcipher_clear_flags(struct crypto_skcipher *tfm,
396 u32 flags)
398 crypto_tfm_clear_flags(crypto_skcipher_tfm(tfm), flags);
401 static inline u32 crypto_sync_skcipher_get_flags(
402 struct crypto_sync_skcipher *tfm)
404 return crypto_skcipher_get_flags(&tfm->base);
407 static inline void crypto_sync_skcipher_set_flags(
408 struct crypto_sync_skcipher *tfm, u32 flags)
410 crypto_skcipher_set_flags(&tfm->base, flags);
413 static inline void crypto_sync_skcipher_clear_flags(
414 struct crypto_sync_skcipher *tfm, u32 flags)
416 crypto_skcipher_clear_flags(&tfm->base, flags);
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
425 * The caller provided key is set for the skcipher referenced by the cipher
426 * handle.
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.
433 * Return: 0 if the setting of the key was successful; < 0 if an error occurred
435 static inline int crypto_skcipher_setkey(struct crypto_skcipher *tfm,
436 const u8 *key, unsigned int keylen)
438 return tfm->setkey(tfm, key, keylen);
441 static inline int crypto_sync_skcipher_setkey(struct crypto_sync_skcipher *tfm,
442 const u8 *key, unsigned int keylen)
444 return crypto_skcipher_setkey(&tfm->base, key, keylen);
447 static inline unsigned int crypto_skcipher_default_keysize(
448 struct crypto_skcipher *tfm)
450 return tfm->keysize;
454 * crypto_skcipher_reqtfm() - obtain cipher handle from request
455 * @req: skcipher_request out of which the cipher handle is to be obtained
457 * Return the crypto_skcipher handle when furnishing an skcipher_request
458 * data structure.
460 * Return: crypto_skcipher handle
462 static inline struct crypto_skcipher *crypto_skcipher_reqtfm(
463 struct skcipher_request *req)
465 return __crypto_skcipher_cast(req->base.tfm);
468 static inline struct crypto_sync_skcipher *crypto_sync_skcipher_reqtfm(
469 struct skcipher_request *req)
471 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
473 return container_of(tfm, struct crypto_sync_skcipher, base);
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
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.
485 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
487 static inline int crypto_skcipher_encrypt(struct skcipher_request *req)
489 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
490 struct crypto_alg *alg = tfm->base.__crt_alg;
491 unsigned int cryptlen = req->cryptlen;
492 int ret;
494 crypto_stats_get(alg);
495 if (crypto_skcipher_get_flags(tfm) & CRYPTO_TFM_NEED_KEY)
496 ret = -ENOKEY;
497 else
498 ret = tfm->encrypt(req);
499 crypto_stats_skcipher_encrypt(cryptlen, ret, alg);
500 return ret;
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
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.
512 * Return: 0 if the cipher operation was successful; < 0 if an error occurred
514 static inline int crypto_skcipher_decrypt(struct skcipher_request *req)
516 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
517 struct crypto_alg *alg = tfm->base.__crt_alg;
518 unsigned int cryptlen = req->cryptlen;
519 int ret;
521 crypto_stats_get(alg);
522 if (crypto_skcipher_get_flags(tfm) & CRYPTO_TFM_NEED_KEY)
523 ret = -ENOKEY;
524 else
525 ret = tfm->decrypt(req);
526 crypto_stats_skcipher_decrypt(cryptlen, ret, alg);
527 return ret;
531 * DOC: Symmetric Key Cipher Request Handle
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.
542 * crypto_skcipher_reqsize() - obtain size of the request data structure
543 * @tfm: cipher handle
545 * Return: number of bytes
547 static inline unsigned int crypto_skcipher_reqsize(struct crypto_skcipher *tfm)
549 return tfm->reqsize;
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
557 * Allow the caller to replace the existing skcipher handle in the request
558 * data structure with a different one.
560 static inline void skcipher_request_set_tfm(struct skcipher_request *req,
561 struct crypto_skcipher *tfm)
563 req->base.tfm = crypto_skcipher_tfm(tfm);
566 static inline void skcipher_request_set_sync_tfm(struct skcipher_request *req,
567 struct crypto_sync_skcipher *tfm)
569 skcipher_request_set_tfm(req, &tfm->base);
572 static inline struct skcipher_request *skcipher_request_cast(
573 struct crypto_async_request *req)
575 return container_of(req, struct skcipher_request, base);
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.
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.
587 * Return: allocated request handle in case of success, or NULL if out of memory
589 static inline struct skcipher_request *skcipher_request_alloc(
590 struct crypto_skcipher *tfm, gfp_t gfp)
592 struct skcipher_request *req;
594 req = kmalloc(sizeof(struct skcipher_request) +
595 crypto_skcipher_reqsize(tfm), gfp);
597 if (likely(req))
598 skcipher_request_set_tfm(req, tfm);
600 return req;
604 * skcipher_request_free() - zeroize and free request data structure
605 * @req: request data structure cipher handle to be freed
607 static inline void skcipher_request_free(struct skcipher_request *req)
609 kzfree(req);
612 static inline void skcipher_request_zero(struct skcipher_request *req)
614 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
616 memzero_explicit(req, sizeof(*req) + crypto_skcipher_reqsize(tfm));
620 * skcipher_request_set_callback() - set asynchronous callback function
621 * @req: request handle
622 * @flags: specify zero or an ORing of the flags
623 * CRYPTO_TFM_REQ_MAY_BACKLOG the request queue may back log and
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.
636 * This function allows setting the callback function that is triggered once the
637 * cipher operation completes.
639 * The callback function is registered with the skcipher_request handle and
640 * must comply with the following template::
642 * void callback_function(struct crypto_async_request *req, int error)
644 static inline void skcipher_request_set_callback(struct skcipher_request *req,
645 u32 flags,
646 crypto_completion_t compl,
647 void *data)
649 req->base.complete = compl;
650 req->base.data = data;
651 req->base.flags = flags;
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
663 * This function allows setting of the source data and destination data
664 * scatter / gather lists.
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.
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)
675 req->src = src;
676 req->dst = dst;
677 req->cryptlen = cryptlen;
678 req->iv = iv;
681 #endif /* _CRYPTO_SKCIPHER_H */