### 5.3 Working with cipher handles

To use a cipher algorithm, you must first allocate an according handle. This is to be done using the open function:

Function: gcry_error_t gcry_cipher_open (gcry_cipher_hd_t *hd, int algo, int mode, unsigned int flags)

  This function creates the context handle required for most of the other cipher functions and returns a handle to it in ‘hd’. In case of an error, an according error code is returned.

  The ID of algorithm to use must be specified via algo. See See Available ciphers, for a list of supported ciphers and the according constants.

  Besides using the constants directly, the function `gcry_cipher_map_name` may be used to convert the textual name of an algorithm into the according numeric ID.

  The cipher mode to use must be specified via mode. See See Available cipher modes, for a list of supported cipher modes and the according constants. Note that some modes are incompatible with some algorithms - in particular, stream mode (`GCRY_CIPHER_MODE_STREAM`) only works with stream ciphers. The block cipher modes (`GCRY_CIPHER_MODE_ECB`, `GCRY_CIPHER_MODE_CBC`, `GCRY_CIPHER_MODE_CFB`, `GCRY_CIPHER_MODE_OFB` and `GCRY_CIPHER_MODE_CTR`) will work with any block cipher algorithm. `GCRY_CIPHER_MODE_CCM` and `GCRY_CIPHER_MODE_GCM` modes will only work with block cipher algorithms which have the block size of 16 bytes.

  The third argument flags can either be passed as `0` or as the bit-wise OR of the following constants.

  `GCRY_CIPHER_SECURE`

    Make sure that all operations are allocated in secure memory. This is useful when the key material is highly confidential.

  `GCRY_CIPHER_ENABLE_SYNC`

    This flag enables the CFB sync mode, which is a special feature of Libgcrypt’s CFB mode implementation to allow for OpenPGP’s CFB variant. See `gcry_cipher_sync`.

  `GCRY_CIPHER_CBC_CTS`

    Enable cipher text stealing (CTS) for the CBC mode. Cannot be used simultaneous as GCRY_CIPHER_CBC_MAC. CTS mode makes it possible to transform data of almost arbitrary size (only limitation is that it must be greater than the algorithm’s block size).

  `GCRY_CIPHER_CBC_MAC`

    Compute CBC-MAC keyed checksums. This is the same as CBC mode, but only output the last block. Cannot be used simultaneous as GCRY_CIPHER_CBC_CTS.

Use the following function to release an existing handle:

Function: void gcry_cipher_close (gcry_cipher_hd_t h)

  This function releases the context created by `gcry_cipher_open`. It also zeroises all sensitive information associated with this cipher handle.

In order to use a handle for performing cryptographic operations, a ‘key’ has to be set first:

Function: gcry_error_t gcry_cipher_setkey (gcry_cipher_hd_t h, const void *k, size_t l)

  Set the key k used for encryption or decryption in the context denoted by the handle h. The length l (in bytes) of the key k must match the required length of the algorithm set for this context or be in the allowed range for algorithms with variable key size. The function checks this and returns an error if there is a problem. A caller should always check for an error.

Most crypto modes requires an initialization vector (IV), which usually is a non-secret random string acting as a kind of salt value. The CTR mode requires a counter, which is also similar to a salt value. To set the IV or CTR, use these functions:

Function: gcry_error_t gcry_cipher_setiv (gcry_cipher_hd_t h, const void *k, size_t l)

  Set the initialization vector used for encryption or decryption. The vector is passed as the buffer K of length l bytes and copied to internal data structures. The function checks that the IV matches the requirement of the selected algorithm and mode.

  This function is also used with the Salsa20 stream cipher to set or update the required nonce. In this case it needs to be called after setting the key.

  This function is also used with the AEAD cipher modes to set or update the required nonce.

Function: gcry_error_t gcry_cipher_setctr (gcry_cipher_hd_t h, const void *c, size_t l)

  Set the counter vector used for encryption or decryption. The counter is passed as the buffer c of length l bytes and copied to internal data structures. The function checks that the counter matches the requirement of the selected algorithm (i.e., it must be the same size as the block size).

Function: gcry_error_t gcry_cipher_reset (gcry_cipher_hd_t h)

  Set the given handle’s context back to the state it had after the last call to gcry_cipher_setkey and clear the initialization vector.

  Note that gcry_cipher_reset is implemented as a macro.

Authenticated Encryption with Associated Data (AEAD) block cipher modes require the handling of the authentication tag and the additional authenticated data, which can be done by using the following functions:

Function: gcry_error_t gcry_cipher_authenticate (gcry_cipher_hd_t h, const void *abuf, size_t abuflen)

  Process the buffer abuf of length abuflen as the additional authenticated data (AAD) for AEAD cipher modes.

Function: gcry_error_t gcry_cipher_gettag (gcry_cipher_hd_t h, void *tag, size_t taglen)

  This function is used to read the authentication tag after encryption. The function finalizes and outputs the authentication tag to the buffer tag of length taglen bytes.

Function: gcry_error_t gcry_cipher_checktag (gcry_cipher_hd_t h, const void *tag, size_t taglen)

  Check the authentication tag after decryption. The authentication tag is passed as the buffer tag of length taglen bytes and compared to internal authentication tag computed during decryption. Error code `GPG_ERR_CHECKSUM` is returned if the authentication tag in the buffer tag does not match the authentication tag calculated during decryption.

The actual encryption and decryption is done by using one of the following functions. They may be used as often as required to process all the data.

Function: gcry_error_t gcry_cipher_encrypt (gcry_cipher_hd_t h, unsigned char *out, size_t outsize, const unsigned char *in, size_t inlen)

  `gcry_cipher_encrypt` is used to encrypt the data. This function can either work in place or with two buffers. It uses the cipher context already setup and described by the handle h. There are 2 ways to use the function: If in is passed as `NULL` and inlen is `0`, in-place encryption of the data in out or length outsize takes place. With in being not `NULL`, inlen bytes are encrypted to the buffer out which must have at least a size of inlen. outsize must be set to the allocated size of out, so that the function can check that there is sufficient space. Note that overlapping buffers are not allowed.

  Depending on the selected algorithms and encryption mode, the length of the buffers must be a multiple of the block size.

  The function returns `0` on success or an error code.

Function: gcry_error_t gcry_cipher_decrypt (gcry_cipher_hd_t h, unsigned char *out, size_t outsize, const unsigned char *in, size_t inlen)

  `gcry_cipher_decrypt` is used to decrypt the data. This function can either work in place or with two buffers. It uses the cipher context already setup and described by the handle h. There are 2 ways to use the function: If in is passed as `NULL` and inlen is `0`, in-place decryption of the data in out or length outsize takes place. With in being not `NULL`, inlen bytes are decrypted to the buffer out which must have at least a size of inlen. outsize must be set to the allocated size of out, so that the function can check that there is sufficient space. Note that overlapping buffers are not allowed.

  Depending on the selected algorithms and encryption mode, the length of the buffers must be a multiple of the block size.

  The function returns `0` on success or an error code.

OpenPGP (as defined in RFC-2440) requires a special sync operation in some places. The following function is used for this:

Function: gcry_error_t gcry_cipher_sync (gcry_cipher_hd_t h)

  Perform the OpenPGP sync operation on context h. Note that this is a no-op unless the context was created with the flag `GCRY_CIPHER_ENABLE_SYNC`

Some of the described functions are implemented as macros utilizing a catch-all control function. This control function is rarely used directly but there is nothing which would inhibit it:

Function: gcry_error_t gcry_cipher_ctl (gcry_cipher_hd_t h, int cmd, void *buffer, size_t buflen)

  `gcry_cipher_ctl` controls various aspects of the cipher module and specific cipher contexts. Usually some more specialized functions or macros are used for this purpose. The semantics of the function and its parameters depends on the the command cmd and the passed context handle h. Please see the comments in the source code (`src/global.c`) for details.

Function: gcry_error_t gcry_cipher_info (gcry_cipher_hd_t h, int what, void *buffer, size_t *nbytes)

  `gcry_cipher_info` is used to retrieve various information about a cipher context or the cipher module in general.

  Currently no information is available.