Symmetric Key Cipher
Block Cipher Algorithm Definitions #
These data structures define modular crypto algorithm implementations,
managed via crypto_register_alg() and crypto_unregister_alg().
struct cipher_alg #
single-block symmetric ciphers definition
Definition
;
Members
cia_min_keysize: Minimum key size supported by the transformation. This is the smallest key length supported by this transformation algorithm. This must be set to one of the pre-defined values as this is not hardware specific. Possible values for this field can be found via git grep "_MIN_KEY_SIZE" include/crypto/cia_max_keysize: Maximum key size supported by the transformation. This is the largest key length supported by this transformation algorithm. This must be set to one of the pre-defined values as this is not hardware specific. Possible values for this field can be found via git grep "_MAX_KEY_SIZE" include/crypto/cia_setkey: Set key for the transformation. This function is used to either program a supplied key into the hardware or store the key in the transformation context for programming it later. Note that this function does modify the transformation context. This function can be called multiple times during the existence of the transformation object, so one must make sure the key is properly reprogrammed into the hardware. This function is also responsible for checking the key length for validity.cia_encrypt: Encrypt a single block. This function is used to encrypt a single block of data, which must be cra_blocksize big. This always operates on a full cra_blocksize and it is not possible to encrypt a block of smaller size. The supplied buffers must therefore also be at least of cra_blocksize size. Both the input and output buffers are always aligned to cra_alignmask. In case either of the input or output buffer supplied by user of the crypto API is not aligned to cra_alignmask, the crypto API will re-align the buffers. The re-alignment means that a new buffer will be allocated, the data will be copied into the new buffer, then the processing will happen on the new buffer, then the data will be copied back into the original buffer and finally the new buffer will be freed. In case a software fallback was put in place in the cra_init call, this function might need to use the fallback if the algorithm doesn't support all of the key sizes. In case the key was stored in transformation context, the key might need to be re-programmed into the hardware in this function. This function shall not modify the transformation context, as this function may be called in parallel with the same transformation object.cia_decrypt: Decrypt a single block. This is a reverse counterpart to cia_encrypt, and the conditions are exactly the same.
Description
All fields are mandatory and must be filled.
struct crypto_alg #
definition of a cryptograpic cipher algorithm
Definition
;
Members
cra_list: internally usedcra_users: internally usedcra_flags: Flags describing this transformation. See include/linux/crypto.h CRYPTO_ALG_* flags for the flags which go in here. Those are used for fine-tuning the description of the transformation algorithm.cra_blocksize: Minimum block size of this transformation. The size in bytes of the smallest possible unit which can be transformed with this algorithm. The users must respect this value. In case of HASH transformation, it is possible for a smaller block than cra_blocksize to be passed to the crypto API for transformation, in case of any other transformation type, an error will be returned upon any attempt to transform smaller than cra_blocksize chunks.cra_ctxsize: Size of the operational context of the transformation. This value informs the kernel crypto API about the memory size needed to be allocated for the transformation context.cra_alignmask: For cipher, skcipher, lskcipher, and aead algorithms this is 1 less than the alignment, in bytes, that the algorithm implementation requires for input and output buffers. When the crypto API is invoked with buffers that are not aligned to this alignment, the crypto API automatically utilizes appropriately aligned temporary buffers to comply with what the algorithm needs. (For scatterlists this happens only if the algorithm uses the skcipher_walk helper functions.) This misalignment handling carries a performance penalty, so it is preferred that algorithms do not set a nonzero alignmask. Also, crypto API users may wish to allocate buffers aligned to the alignmask of the algorithm being used, in order to avoid the API having to realign them. Note: the alignmask is not supported for hash algorithms and is always 0 for them.cra_reqsize: Size of the request context for this algorithm.cra_priority: Priority of this transformation implementation. In case multiple transformations with same cra_name are available to the Crypto API, the kernel will use the one with highest cra_priority.cra_refcnt: internally usedcra_name: Generic name (usable by multiple implementations) of the transformation algorithm. This is the name of the transformation itself. This field is used by the kernel when looking up the providers of particular transformation.cra_driver_name: Unique name of the transformation provider. This is the name of the provider of the transformation. This can be any arbitrary value, but in the usual case, this contains the name of the chip or provider and the name of the transformation algorithm.cra_type: Type of the cryptographic transformation. This is a pointer to struct crypto_type, which implements callbacks common for all transformation types. There are multiple options, such ascrypto_skcipher_type,crypto_ahash_type,crypto_rng_type. This field might be empty. In that case, there are no common callbacks. This is the case for: cipher.cra_u: Callbacks implementing the transformation. This is a union of multiple structures. Depending on the type of transformation selected by cra_type and cra_flags above, the associated structure must be filled with callbacks. This field might be empty. This is the case for ahash, shash.cra_u.cipher: Union member which contains a single-block symmetric cipher definition. See struct cipher_alg.cra_init: Deprecated, do not use.cra_exit: Deprecated, do not use.cra_destroy: internally usedcra_module: Owner of this transformation implementation. Set to THIS_MODULE
Description
The struct crypto_alg describes a generic Crypto API algorithm and is common for all of the transformations. Any variable not documented here shall not be used by a cipher implementation as it is internal to the Crypto API.
Symmetric Key Cipher API #
Symmetric key cipher API is used with the ciphers of type CRYPTO_ALG_TYPE_SKCIPHER (listed as type "skcipher" in /proc/crypto).
Asynchronous cipher operations imply that the function invocation for a cipher request returns immediately before the completion of the operation. The cipher request is scheduled as a separate kernel thread and therefore load-balanced on the different CPUs via the process scheduler. To allow the kernel crypto API to inform the caller about the completion of a cipher request, the caller must provide a callback function. That function is invoked with the cipher handle when the request completes.
To support the asynchronous operation, additional information than just the cipher handle must be supplied to the kernel crypto API. That additional information is given by filling in the skcipher_request data structure.
For the symmetric key cipher API, the state is maintained with the tfm cipher handle. A single tfm can be used across multiple calls and in parallel. For asynchronous block cipher calls, context data supplied and only used by the caller can be referenced the request data structure in addition to the IV used for the cipher request. The maintenance of such state information would be important for a crypto driver implementer to have, because when calling the callback function upon completion of the cipher operation, that callback function may need some information about which operation just finished if it invoked multiple in parallel. This state information is unused by the kernel crypto API.
crypto_alloc_skcipher() #
struct crypto_skcipher * allocate symmetric key cipher handle
Parameters
const char *alg_name: is the cra_name / name or cra_driver_name / driver name of the skcipher cipheru32 type: specifies the type of the cipheru32 mask: specifies the mask for the cipher
Description
Allocate a cipher handle for an skcipher. The returned struct crypto_skcipher is the cipher handle that is required for any subsequent API invocation for that skcipher.
Return
allocated cipher handle in case of success; IS_ERR() is true in case
of an error, PTR_ERR() returns the error code.
crypto_free_skcipher() #
void zeroize and free cipher handle
Parameters
struct crypto_skcipher *tfm: cipher handle to be freed
Description
If tfm is a NULL or error pointer, this function does nothing.
crypto_has_skcipher() #
int Search for the availability of an skcipher.
Parameters
const char *alg_name: is the cra_name / name or cra_driver_name / driver name of the skcipheru32 type: specifies the type of the skcipheru32 mask: specifies the mask for the skcipher
Return
true when the skcipher is known to the kernel crypto API; false otherwise
crypto_skcipher_ivsize() #
unsigned int obtain IV size
Parameters
struct crypto_skcipher *tfm: cipher handle
Description
The size of the IV for the skcipher referenced by the cipher handle is returned. This IV size may be zero if the cipher does not need an IV.
Return
IV size in bytes
crypto_skcipher_blocksize() #
unsigned int obtain block size of cipher
Parameters
struct crypto_skcipher *tfm: cipher handle
Description
The block size for the skcipher referenced with the cipher handle is returned. The caller may use that information to allocate appropriate memory for the data returned by the encryption or decryption operation
Return
block size of cipher
crypto_skcipher_setkey() #
int set key for cipher
Parameters
struct crypto_skcipher *tfm: cipher handleconst u8 *key: buffer holding the keyunsigned int keylen: length of the key in bytes
Description
The caller provided key is set for the skcipher referenced by the cipher handle.
Note, the key length determines the cipher type. Many block ciphers implement different cipher modes depending on the key size, such as AES-128 vs AES-192 vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128 is performed.
Return
0 if the setting of the key was successful; < 0 if an error occurred
crypto_skcipher_reqtfm() #
struct crypto_skcipher * obtain cipher handle from request
Parameters
struct skcipher_request *req: skcipher_request out of which the cipher handle is to be obtained
Description
Return the crypto_skcipher handle when furnishing an skcipher_request data structure.
Return
crypto_skcipher handle
crypto_skcipher_encrypt() #
int encrypt plaintext
Parameters
struct skcipher_request *req: reference to the skcipher_request handle that holds all information needed to perform the cipher operation
Description
Encrypt plaintext data using the skcipher_request handle. That data structure and how it is filled with data is discussed with the skcipher_request_* functions.
Return
0 if the cipher operation was successful; < 0 if an error occurred
crypto_skcipher_decrypt() #
int decrypt ciphertext
Parameters
struct skcipher_request *req: reference to the skcipher_request handle that holds all information needed to perform the cipher operation
Description
Decrypt ciphertext data using the skcipher_request handle. That data structure and how it is filled with data is discussed with the skcipher_request_* functions.
Return
0 if the cipher operation was successful; < 0 if an error occurred
Symmetric Key Cipher Request Handle #
The skcipher_request data structure contains all pointers to data required for the symmetric key cipher operation. This includes the cipher handle (which can be used by multiple skcipher_request instances), pointer to plaintext and ciphertext, asynchronous callback function, etc. It acts as a handle to the skcipher_request_* API calls in a similar way as skcipher handle to the crypto_skcipher_* API calls.
crypto_skcipher_reqsize() #
unsigned int obtain size of the request data structure
Parameters
struct crypto_skcipher *tfm: cipher handle
Return
number of bytes
skcipher_request_set_tfm() #
void update cipher handle reference in request
Parameters
struct skcipher_request *req: request handle to be modifiedstruct crypto_skcipher *tfm: cipher handle that shall be added to the request handle
Description
Allow the caller to replace the existing skcipher handle in the request data structure with a different one.
skcipher_request_alloc() #
struct skcipher_request * allocate request data structure
Parameters
struct crypto_skcipher *tfm: cipher handle to be registered with the requestgfp_t gfp: memory allocation flag that is handed to kmalloc by the API call.
Description
Allocate the request data structure that must be used with the skcipher encrypt and decrypt API calls. During the allocation, the provided skcipher handle is registered in the request data structure.
Return
allocated request handle in case of success, or NULL if out of memory
skcipher_request_free() #
void zeroize and free request data structure
Parameters
struct skcipher_request *req: request data structure cipher handle to be freed
skcipher_request_set_callback() #
void set asynchronous callback function
Parameters
struct skcipher_request *req: request handleu32 flags: specify zero or an ORing of the flags CRYPTO_TFM_REQ_MAY_BACKLOG the request queue may back log and increase the wait queue beyond the initial maximum size; CRYPTO_TFM_REQ_MAY_SLEEP the request processing may sleepcrypto_completion_t compl: callback function pointer to be registered with the request handlevoid *data: The data pointer refers to memory that is not used by the kernel crypto API, but provided to the callback function for it to use. Here, the caller can provide a reference to memory the callback function can operate on. As the callback function is invoked asynchronously to the related functionality, it may need to access data structures of the related functionality which can be referenced using this pointer. The callback function can access the memory via the "data" field in the crypto_async_request data structure provided to the callback function.
Description
This function allows setting the callback function that is triggered once the cipher operation completes.
The callback function is registered with the skcipher_request handle and must comply with the following template:
void callback_function(struct crypto_async_request *req, int error)
skcipher_request_set_crypt() #
void set data buffers
Parameters
struct skcipher_request *req: request handlestruct scatterlist *src: source scatter / gather liststruct scatterlist *dst: destination scatter / gather listunsigned int cryptlen: number of bytes to process from srcvoid *iv: IV for the cipher operation which must comply with the IV size defined by crypto_skcipher_ivsize
Description
This function allows setting of the source data and destination data scatter / gather lists.
For encryption, the source is treated as the plaintext and the destination is the ciphertext. For a decryption operation, the use is reversed - the source is the ciphertext and the destination is the plaintext.
Single Block Cipher API #
The single block cipher API is used with the ciphers of type CRYPTO_ALG_TYPE_CIPHER (listed as type "cipher" in /proc/crypto).
Using the single block cipher API calls, operations with the basic cipher primitive can be implemented. These cipher primitives exclude any block chaining operations including IV handling.
The purpose of this single block cipher API is to support the implementation of templates or other concepts that only need to perform the cipher operation on one block at a time. Templates invoke the underlying cipher primitive block-wise and process either the input or the output data of these cipher operations.
crypto_alloc_cipher() #
struct crypto_cipher * allocate single block cipher handle
Parameters
const char *alg_name: is the cra_name / name or cra_driver_name / driver name of the single block cipheru32 type: specifies the type of the cipheru32 mask: specifies the mask for the cipher
Description
Allocate a cipher handle for a single block cipher. The returned struct crypto_cipher is the cipher handle that is required for any subsequent API invocation for that single block cipher.
Return
allocated cipher handle in case of success; IS_ERR() is true in case
of an error, PTR_ERR() returns the error code.
crypto_free_cipher() #
void zeroize and free the single block cipher handle
Parameters
struct crypto_cipher *tfm: cipher handle to be freed
crypto_has_cipher() #
int Search for the availability of a single block cipher
Parameters
const char *alg_name: is the cra_name / name or cra_driver_name / driver name of the single block cipheru32 type: specifies the type of the cipheru32 mask: specifies the mask for the cipher
Return
true when the single block cipher is known to the kernel crypto API; false otherwise
crypto_cipher_blocksize() #
unsigned int obtain block size for cipher
Parameters
struct crypto_cipher *tfm: cipher handle
Description
The block size for the single block cipher referenced with the cipher handle tfm is returned. The caller may use that information to allocate appropriate memory for the data returned by the encryption or decryption operation
Return
block size of cipher
crypto_cipher_setkey() #
int set key for cipher
Parameters
struct crypto_cipher *tfm: cipher handleconst u8 *key: buffer holding the keyunsigned int keylen: length of the key in bytes
Description
The caller provided key is set for the single block cipher referenced by the cipher handle.
Note, the key length determines the cipher type. Many block ciphers implement different cipher modes depending on the key size, such as AES-128 vs AES-192 vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128 is performed.
Return
0 if the setting of the key was successful; < 0 if an error occurred
crypto_cipher_encrypt_one() #
void encrypt one block of plaintext
Parameters
struct crypto_cipher *tfm: cipher handleu8 *dst: points to the buffer that will be filled with the ciphertextconst u8 *src: buffer holding the plaintext to be encrypted
Description
Invoke the encryption operation of one block. The caller must ensure that the plaintext and ciphertext buffers are at least one block in size.
crypto_cipher_decrypt_one() #
void decrypt one block of ciphertext
Parameters
struct crypto_cipher *tfm: cipher handleu8 *dst: points to the buffer that will be filled with the plaintextconst u8 *src: buffer holding the ciphertext to be decrypted
Description
Invoke the decryption operation of one block. The caller must ensure that the plaintext and ciphertext buffers are at least one block in size.