Next: General cipher functions, Previous: Available cipher modes, Up: Symmetric cryptography [Contents][Index]
To use a cipher algorithm, you must first allocate an according handle. This is to be done using the open function:
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.
Poly1305 AEAD mode (GCRY_CIPHER_MODE_POLY1305
) only works with
ChaCha20 stream cipher. 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. GCM mode (GCRY_CIPHER_MODE_CCM
), CCM mode
(GCRY_CIPHER_MODE_GCM
), and OCB mode
(GCRY_CIPHER_MODE_OCB
) 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:
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:
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:
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 by AEAD modes and with Salsa20 and ChaCha20 stream ciphers to set or update the required nonce. In these cases it needs to be called after setting the key.
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).
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:
Process the buffer abuf of length abuflen as the additional authenticated data (AAD) for AEAD cipher modes.
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.
Depending on the used mode certain restrictions for taglen are enforced: For GCM taglen must be at least 16 or one of the allowed truncated lengths (4, 8, 12, 13, 14, or 15).
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.
Depending on the used mode certain restrictions for taglen are enforced: For GCM taglen must either be 16 or one of the allowed truncated lengths (4, 8, 12, 13, 14, or 15).
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.
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 of
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.
Some encryption modes require that gcry_cipher_final
is used
before the final data chunk is passed to this function.
The function returns 0
on success or an error code.
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.
Some encryption modes require that gcry_cipher_final
is used
before the final data chunk is passed to this function.
The function returns 0
on success or an error code.
The OCB mode features integrated padding and must thus be told about the end of the input data. This is done with:
Set a flag in the context to tell the encrypt and decrypt functions that their next call will provide the last chunk of data. Only the first call to this function has an effect and only for modes which support it. Checking the error is in general not necessary. This is implemented as a macro.
OpenPGP (as defined in RFC-4880) requires a special sync operation in some places. The following function is used for this:
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:
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.
gcry_cipher_info
is used to retrieve various
information about a cipher context or the cipher module in general.
GCRYCTL_GET_TAGLEN:
Return the length of the tag for an AE algorithm mode. An error is returned for modes which do not support a tag. buffer must be given as NULL. On success the result is stored nbytes. The taglen is returned in bytes.
Next: General cipher functions, Previous: Available cipher modes, Up: Symmetric cryptography [Contents][Index]