The operation is very similar to the symmetric cipher discussion. During initialization, the struct sockaddr data structure must be filled as follows:
struct sockaddr_alg sa = { .salg_family = AF_ALG, .salg_type = "aead", /* this selects the symmetric cipher */ .salg_name = "gcm(aes)" /* this is the cipher name */ };
Before data can be sent to the kernel using the write/send system call family, the consumer must set the key. The key setting is described with the setsockopt invocation below.
In addition, before data can be sent to the kernel using the write/send system call family, the consumer must set the authentication tag size. To set the authentication tag size, the caller must use the setsockopt invocation described below.
Using the sendmsg() system call, the application provides the data that should be processed for encryption or decryption. In addition, the IV is specified with the data structure provided by the sendmsg() system call.
The sendmsg system call parameter of struct msghdr is embedded into the struct cmsghdr data structure. See recv(2) and cmsg(3) for more information on how the cmsghdr data structure is used together with the send/recv system call family. That cmsghdr data structure holds the following information specified with a separate header instances:
specification of the cipher operation type with one of these flags:
ALG_OP_ENCRYPT - encryption of data
ALG_OP_DECRYPT - decryption of data
specification of the IV information marked with the flag ALG_SET_IV
specification of the associated authentication data (AAD) with the flag ALG_SET_AEAD_ASSOCLEN. The AAD is sent to the kernel together with the plaintext / ciphertext. See below for the memory structure.
The send system call family allows the following flag to be specified:
MSG_MORE: If this flag is set, the send system call acts like a cipher update function where more input data is expected with a subsequent invocation of the send system call.
Note: The kernel reports -EINVAL for any unexpected data. The caller must make sure that all data matches the constraints given in /proc/crypto for the selected cipher.
With the recv() system call, the application can read the result of the cipher operation from the kernel crypto API. The output buffer must be at least as large as defined with the memory structure below. If the output data size is smaller, the cipher operation is not performed.
The authenticated decryption operation may indicate an integrity error. Such breach in integrity is marked with the -EBADMSG error code.
The AEAD cipher operates with the following information that is communicated between user and kernel space as one data stream:
plaintext or ciphertext
associated authentication data (AAD)
authentication tag
The sizes of the AAD and the authentication tag are provided with the sendmsg and setsockopt calls (see there). As the kernel knows the size of the entire data stream, the kernel is now able to calculate the right offsets of the data components in the data stream.
The user space caller must arrange the aforementioned information in the following order:
AEAD encryption input: AAD || plaintext
AEAD decryption input: AAD || ciphertext || authentication tag
The output buffer the user space caller provides must be at least as large to hold the following data:
AEAD encryption output: ciphertext || authentication tag
AEAD decryption output: plaintext