0001 User Space Interface
0002 ====================
0003
0004 Introduction
0005 ------------
0006
0007 The concepts of the kernel crypto API visible to kernel space is fully
0008 applicable to the user space interface as well. Therefore, the kernel
0009 crypto API high level discussion for the in-kernel use cases applies
0010 here as well.
0011
0012 The major difference, however, is that user space can only act as a
0013 consumer and never as a provider of a transformation or cipher
0014 algorithm.
0015
0016 The following covers the user space interface exported by the kernel
0017 crypto API. A working example of this description is libkcapi that can
0018 be obtained from [1]. That library can be used by user space
0019 applications that require cryptographic services from the kernel.
0020
0021 Some details of the in-kernel kernel crypto API aspects do not apply to
0022 user space, however. This includes the difference between synchronous
0023 and asynchronous invocations. The user space API call is fully
0024 synchronous.
0025
0026 [1] https://www.chronox.de/libkcapi.html
0027
0028 User Space API General Remarks
0029 ------------------------------
0030
0031 The kernel crypto API is accessible from user space. Currently, the
0032 following ciphers are accessible:
0033
0034 - Message digest including keyed message digest (HMAC, CMAC)
0035
0036 - Symmetric ciphers
0037
0038 - AEAD ciphers
0039
0040 - Random Number Generators
0041
0042 The interface is provided via socket type using the type AF_ALG. In
0043 addition, the setsockopt option type is SOL_ALG. In case the user space
0044 header files do not export these flags yet, use the following macros:
0045
0046 ::
0047
0048 #ifndef AF_ALG
0049 #define AF_ALG 38
0050 #endif
0051 #ifndef SOL_ALG
0052 #define SOL_ALG 279
0053 #endif
0054
0055
0056 A cipher is accessed with the same name as done for the in-kernel API
0057 calls. This includes the generic vs. unique naming schema for ciphers as
0058 well as the enforcement of priorities for generic names.
0059
0060 To interact with the kernel crypto API, a socket must be created by the
0061 user space application. User space invokes the cipher operation with the
0062 send()/write() system call family. The result of the cipher operation is
0063 obtained with the read()/recv() system call family.
0064
0065 The following API calls assume that the socket descriptor is already
0066 opened by the user space application and discusses only the kernel
0067 crypto API specific invocations.
0068
0069 To initialize the socket interface, the following sequence has to be
0070 performed by the consumer:
0071
0072 1. Create a socket of type AF_ALG with the struct sockaddr_alg
0073 parameter specified below for the different cipher types.
0074
0075 2. Invoke bind with the socket descriptor
0076
0077 3. Invoke accept with the socket descriptor. The accept system call
0078 returns a new file descriptor that is to be used to interact with the
0079 particular cipher instance. When invoking send/write or recv/read
0080 system calls to send data to the kernel or obtain data from the
0081 kernel, the file descriptor returned by accept must be used.
0082
0083 In-place Cipher operation
0084 -------------------------
0085
0086 Just like the in-kernel operation of the kernel crypto API, the user
0087 space interface allows the cipher operation in-place. That means that
0088 the input buffer used for the send/write system call and the output
0089 buffer used by the read/recv system call may be one and the same. This
0090 is of particular interest for symmetric cipher operations where a
0091 copying of the output data to its final destination can be avoided.
0092
0093 If a consumer on the other hand wants to maintain the plaintext and the
0094 ciphertext in different memory locations, all a consumer needs to do is
0095 to provide different memory pointers for the encryption and decryption
0096 operation.
0097
0098 Message Digest API
0099 ------------------
0100
0101 The message digest type to be used for the cipher operation is selected
0102 when invoking the bind syscall. bind requires the caller to provide a
0103 filled struct sockaddr data structure. This data structure must be
0104 filled as follows:
0105
0106 ::
0107
0108 struct sockaddr_alg sa = {
0109 .salg_family = AF_ALG,
0110 .salg_type = "hash", /* this selects the hash logic in the kernel */
0111 .salg_name = "sha1" /* this is the cipher name */
0112 };
0113
0114
0115 The salg_type value "hash" applies to message digests and keyed message
0116 digests. Though, a keyed message digest is referenced by the appropriate
0117 salg_name. Please see below for the setsockopt interface that explains
0118 how the key can be set for a keyed message digest.
0119
0120 Using the send() system call, the application provides the data that
0121 should be processed with the message digest. The send system call allows
0122 the following flags to be specified:
0123
0124 - MSG_MORE: If this flag is set, the send system call acts like a
0125 message digest update function where the final hash is not yet
0126 calculated. If the flag is not set, the send system call calculates
0127 the final message digest immediately.
0128
0129 With the recv() system call, the application can read the message digest
0130 from the kernel crypto API. If the buffer is too small for the message
0131 digest, the flag MSG_TRUNC is set by the kernel.
0132
0133 In order to set a message digest key, the calling application must use
0134 the setsockopt() option of ALG_SET_KEY. If the key is not set the HMAC
0135 operation is performed without the initial HMAC state change caused by
0136 the key.
0137
0138 Symmetric Cipher API
0139 --------------------
0140
0141 The operation is very similar to the message digest discussion. During
0142 initialization, the struct sockaddr data structure must be filled as
0143 follows:
0144
0145 ::
0146
0147 struct sockaddr_alg sa = {
0148 .salg_family = AF_ALG,
0149 .salg_type = "skcipher", /* this selects the symmetric cipher */
0150 .salg_name = "cbc(aes)" /* this is the cipher name */
0151 };
0152
0153
0154 Before data can be sent to the kernel using the write/send system call
0155 family, the consumer must set the key. The key setting is described with
0156 the setsockopt invocation below.
0157
0158 Using the sendmsg() system call, the application provides the data that
0159 should be processed for encryption or decryption. In addition, the IV is
0160 specified with the data structure provided by the sendmsg() system call.
0161
0162 The sendmsg system call parameter of struct msghdr is embedded into the
0163 struct cmsghdr data structure. See recv(2) and cmsg(3) for more
0164 information on how the cmsghdr data structure is used together with the
0165 send/recv system call family. That cmsghdr data structure holds the
0166 following information specified with a separate header instances:
0167
0168 - specification of the cipher operation type with one of these flags:
0169
0170 - ALG_OP_ENCRYPT - encryption of data
0171
0172 - ALG_OP_DECRYPT - decryption of data
0173
0174 - specification of the IV information marked with the flag ALG_SET_IV
0175
0176 The send system call family allows the following flag to be specified:
0177
0178 - MSG_MORE: If this flag is set, the send system call acts like a
0179 cipher update function where more input data is expected with a
0180 subsequent invocation of the send system call.
0181
0182 Note: The kernel reports -EINVAL for any unexpected data. The caller
0183 must make sure that all data matches the constraints given in
0184 /proc/crypto for the selected cipher.
0185
0186 With the recv() system call, the application can read the result of the
0187 cipher operation from the kernel crypto API. The output buffer must be
0188 at least as large as to hold all blocks of the encrypted or decrypted
0189 data. If the output data size is smaller, only as many blocks are
0190 returned that fit into that output buffer size.
0191
0192 AEAD Cipher API
0193 ---------------
0194
0195 The operation is very similar to the symmetric cipher discussion. During
0196 initialization, the struct sockaddr data structure must be filled as
0197 follows:
0198
0199 ::
0200
0201 struct sockaddr_alg sa = {
0202 .salg_family = AF_ALG,
0203 .salg_type = "aead", /* this selects the symmetric cipher */
0204 .salg_name = "gcm(aes)" /* this is the cipher name */
0205 };
0206
0207
0208 Before data can be sent to the kernel using the write/send system call
0209 family, the consumer must set the key. The key setting is described with
0210 the setsockopt invocation below.
0211
0212 In addition, before data can be sent to the kernel using the write/send
0213 system call family, the consumer must set the authentication tag size.
0214 To set the authentication tag size, the caller must use the setsockopt
0215 invocation described below.
0216
0217 Using the sendmsg() system call, the application provides the data that
0218 should be processed for encryption or decryption. In addition, the IV is
0219 specified with the data structure provided by the sendmsg() system call.
0220
0221 The sendmsg system call parameter of struct msghdr is embedded into the
0222 struct cmsghdr data structure. See recv(2) and cmsg(3) for more
0223 information on how the cmsghdr data structure is used together with the
0224 send/recv system call family. That cmsghdr data structure holds the
0225 following information specified with a separate header instances:
0226
0227 - specification of the cipher operation type with one of these flags:
0228
0229 - ALG_OP_ENCRYPT - encryption of data
0230
0231 - ALG_OP_DECRYPT - decryption of data
0232
0233 - specification of the IV information marked with the flag ALG_SET_IV
0234
0235 - specification of the associated authentication data (AAD) with the
0236 flag ALG_SET_AEAD_ASSOCLEN. The AAD is sent to the kernel together
0237 with the plaintext / ciphertext. See below for the memory structure.
0238
0239 The send system call family allows the following flag to be specified:
0240
0241 - MSG_MORE: If this flag is set, the send system call acts like a
0242 cipher update function where more input data is expected with a
0243 subsequent invocation of the send system call.
0244
0245 Note: The kernel reports -EINVAL for any unexpected data. The caller
0246 must make sure that all data matches the constraints given in
0247 /proc/crypto for the selected cipher.
0248
0249 With the recv() system call, the application can read the result of the
0250 cipher operation from the kernel crypto API. The output buffer must be
0251 at least as large as defined with the memory structure below. If the
0252 output data size is smaller, the cipher operation is not performed.
0253
0254 The authenticated decryption operation may indicate an integrity error.
0255 Such breach in integrity is marked with the -EBADMSG error code.
0256
0257 AEAD Memory Structure
0258 ~~~~~~~~~~~~~~~~~~~~~
0259
0260 The AEAD cipher operates with the following information that is
0261 communicated between user and kernel space as one data stream:
0262
0263 - plaintext or ciphertext
0264
0265 - associated authentication data (AAD)
0266
0267 - authentication tag
0268
0269 The sizes of the AAD and the authentication tag are provided with the
0270 sendmsg and setsockopt calls (see there). As the kernel knows the size
0271 of the entire data stream, the kernel is now able to calculate the right
0272 offsets of the data components in the data stream.
0273
0274 The user space caller must arrange the aforementioned information in the
0275 following order:
0276
0277 - AEAD encryption input: AAD \|\| plaintext
0278
0279 - AEAD decryption input: AAD \|\| ciphertext \|\| authentication tag
0280
0281 The output buffer the user space caller provides must be at least as
0282 large to hold the following data:
0283
0284 - AEAD encryption output: ciphertext \|\| authentication tag
0285
0286 - AEAD decryption output: plaintext
0287
0288 Random Number Generator API
0289 ---------------------------
0290
0291 Again, the operation is very similar to the other APIs. During
0292 initialization, the struct sockaddr data structure must be filled as
0293 follows:
0294
0295 ::
0296
0297 struct sockaddr_alg sa = {
0298 .salg_family = AF_ALG,
0299 .salg_type = "rng", /* this selects the random number generator */
0300 .salg_name = "drbg_nopr_sha256" /* this is the RNG name */
0301 };
0302
0303
0304 Depending on the RNG type, the RNG must be seeded. The seed is provided
0305 using the setsockopt interface to set the key. For example, the
0306 ansi_cprng requires a seed. The DRBGs do not require a seed, but may be
0307 seeded. The seed is also known as a *Personalization String* in NIST SP 800-90A
0308 standard.
0309
0310 Using the read()/recvmsg() system calls, random numbers can be obtained.
0311 The kernel generates at most 128 bytes in one call. If user space
0312 requires more data, multiple calls to read()/recvmsg() must be made.
0313
0314 WARNING: The user space caller may invoke the initially mentioned accept
0315 system call multiple times. In this case, the returned file descriptors
0316 have the same state.
0317
0318 Following CAVP testing interfaces are enabled when kernel is built with
0319 CRYPTO_USER_API_RNG_CAVP option:
0320
0321 - the concatenation of *Entropy* and *Nonce* can be provided to the RNG via
0322 ALG_SET_DRBG_ENTROPY setsockopt interface. Setting the entropy requires
0323 CAP_SYS_ADMIN permission.
0324
0325 - *Additional Data* can be provided using the send()/sendmsg() system calls,
0326 but only after the entropy has been set.
0327
0328 Zero-Copy Interface
0329 -------------------
0330
0331 In addition to the send/write/read/recv system call family, the AF_ALG
0332 interface can be accessed with the zero-copy interface of
0333 splice/vmsplice. As the name indicates, the kernel tries to avoid a copy
0334 operation into kernel space.
0335
0336 The zero-copy operation requires data to be aligned at the page
0337 boundary. Non-aligned data can be used as well, but may require more
0338 operations of the kernel which would defeat the speed gains obtained
0339 from the zero-copy interface.
0340
0341 The system-inherent limit for the size of one zero-copy operation is 16
0342 pages. If more data is to be sent to AF_ALG, user space must slice the
0343 input into segments with a maximum size of 16 pages.
0344
0345 Zero-copy can be used with the following code example (a complete
0346 working example is provided with libkcapi):
0347
0348 ::
0349
0350 int pipes[2];
0351
0352 pipe(pipes);
0353 /* input data in iov */
0354 vmsplice(pipes[1], iov, iovlen, SPLICE_F_GIFT);
0355 /* opfd is the file descriptor returned from accept() system call */
0356 splice(pipes[0], NULL, opfd, NULL, ret, 0);
0357 read(opfd, out, outlen);
0358
0359
0360 Setsockopt Interface
0361 --------------------
0362
0363 In addition to the read/recv and send/write system call handling to send
0364 and retrieve data subject to the cipher operation, a consumer also needs
0365 to set the additional information for the cipher operation. This
0366 additional information is set using the setsockopt system call that must
0367 be invoked with the file descriptor of the open cipher (i.e. the file
0368 descriptor returned by the accept system call).
0369
0370 Each setsockopt invocation must use the level SOL_ALG.
0371
0372 The setsockopt interface allows setting the following data using the
0373 mentioned optname:
0374
0375 - ALG_SET_KEY -- Setting the key. Key setting is applicable to:
0376
0377 - the skcipher cipher type (symmetric ciphers)
0378
0379 - the hash cipher type (keyed message digests)
0380
0381 - the AEAD cipher type
0382
0383 - the RNG cipher type to provide the seed
0384
0385 - ALG_SET_AEAD_AUTHSIZE -- Setting the authentication tag size for
0386 AEAD ciphers. For a encryption operation, the authentication tag of
0387 the given size will be generated. For a decryption operation, the
0388 provided ciphertext is assumed to contain an authentication tag of
0389 the given size (see section about AEAD memory layout below).
0390
0391 - ALG_SET_DRBG_ENTROPY -- Setting the entropy of the random number generator.
0392 This option is applicable to RNG cipher type only.
0393
0394 User space API example
0395 ----------------------
0396
0397 Please see [1] for libkcapi which provides an easy-to-use wrapper around
0398 the aforementioned Netlink kernel interface. [1] also contains a test
0399 application that invokes all libkcapi API calls.
0400
0401 [1] https://www.chronox.de/libkcapi.html