162306a36Sopenharmony_ciUser Space Interface 262306a36Sopenharmony_ci==================== 362306a36Sopenharmony_ci 462306a36Sopenharmony_ciIntroduction 562306a36Sopenharmony_ci------------ 662306a36Sopenharmony_ci 762306a36Sopenharmony_ciThe concepts of the kernel crypto API visible to kernel space is fully 862306a36Sopenharmony_ciapplicable to the user space interface as well. Therefore, the kernel 962306a36Sopenharmony_cicrypto API high level discussion for the in-kernel use cases applies 1062306a36Sopenharmony_cihere as well. 1162306a36Sopenharmony_ci 1262306a36Sopenharmony_ciThe major difference, however, is that user space can only act as a 1362306a36Sopenharmony_ciconsumer and never as a provider of a transformation or cipher 1462306a36Sopenharmony_cialgorithm. 1562306a36Sopenharmony_ci 1662306a36Sopenharmony_ciThe following covers the user space interface exported by the kernel 1762306a36Sopenharmony_cicrypto API. A working example of this description is libkcapi that can 1862306a36Sopenharmony_cibe obtained from [1]. That library can be used by user space 1962306a36Sopenharmony_ciapplications that require cryptographic services from the kernel. 2062306a36Sopenharmony_ci 2162306a36Sopenharmony_ciSome details of the in-kernel kernel crypto API aspects do not apply to 2262306a36Sopenharmony_ciuser space, however. This includes the difference between synchronous 2362306a36Sopenharmony_ciand asynchronous invocations. The user space API call is fully 2462306a36Sopenharmony_cisynchronous. 2562306a36Sopenharmony_ci 2662306a36Sopenharmony_ci[1] https://www.chronox.de/libkcapi.html 2762306a36Sopenharmony_ci 2862306a36Sopenharmony_ciUser Space API General Remarks 2962306a36Sopenharmony_ci------------------------------ 3062306a36Sopenharmony_ci 3162306a36Sopenharmony_ciThe kernel crypto API is accessible from user space. Currently, the 3262306a36Sopenharmony_cifollowing ciphers are accessible: 3362306a36Sopenharmony_ci 3462306a36Sopenharmony_ci- Message digest including keyed message digest (HMAC, CMAC) 3562306a36Sopenharmony_ci 3662306a36Sopenharmony_ci- Symmetric ciphers 3762306a36Sopenharmony_ci 3862306a36Sopenharmony_ci- AEAD ciphers 3962306a36Sopenharmony_ci 4062306a36Sopenharmony_ci- Random Number Generators 4162306a36Sopenharmony_ci 4262306a36Sopenharmony_ciThe interface is provided via socket type using the type AF_ALG. In 4362306a36Sopenharmony_ciaddition, the setsockopt option type is SOL_ALG. In case the user space 4462306a36Sopenharmony_ciheader files do not export these flags yet, use the following macros: 4562306a36Sopenharmony_ci 4662306a36Sopenharmony_ci:: 4762306a36Sopenharmony_ci 4862306a36Sopenharmony_ci #ifndef AF_ALG 4962306a36Sopenharmony_ci #define AF_ALG 38 5062306a36Sopenharmony_ci #endif 5162306a36Sopenharmony_ci #ifndef SOL_ALG 5262306a36Sopenharmony_ci #define SOL_ALG 279 5362306a36Sopenharmony_ci #endif 5462306a36Sopenharmony_ci 5562306a36Sopenharmony_ci 5662306a36Sopenharmony_ciA cipher is accessed with the same name as done for the in-kernel API 5762306a36Sopenharmony_cicalls. This includes the generic vs. unique naming schema for ciphers as 5862306a36Sopenharmony_ciwell as the enforcement of priorities for generic names. 5962306a36Sopenharmony_ci 6062306a36Sopenharmony_ciTo interact with the kernel crypto API, a socket must be created by the 6162306a36Sopenharmony_ciuser space application. User space invokes the cipher operation with the 6262306a36Sopenharmony_cisend()/write() system call family. The result of the cipher operation is 6362306a36Sopenharmony_ciobtained with the read()/recv() system call family. 6462306a36Sopenharmony_ci 6562306a36Sopenharmony_ciThe following API calls assume that the socket descriptor is already 6662306a36Sopenharmony_ciopened by the user space application and discusses only the kernel 6762306a36Sopenharmony_cicrypto API specific invocations. 6862306a36Sopenharmony_ci 6962306a36Sopenharmony_ciTo initialize the socket interface, the following sequence has to be 7062306a36Sopenharmony_ciperformed by the consumer: 7162306a36Sopenharmony_ci 7262306a36Sopenharmony_ci1. Create a socket of type AF_ALG with the struct sockaddr_alg 7362306a36Sopenharmony_ci parameter specified below for the different cipher types. 7462306a36Sopenharmony_ci 7562306a36Sopenharmony_ci2. Invoke bind with the socket descriptor 7662306a36Sopenharmony_ci 7762306a36Sopenharmony_ci3. Invoke accept with the socket descriptor. The accept system call 7862306a36Sopenharmony_ci returns a new file descriptor that is to be used to interact with the 7962306a36Sopenharmony_ci particular cipher instance. When invoking send/write or recv/read 8062306a36Sopenharmony_ci system calls to send data to the kernel or obtain data from the 8162306a36Sopenharmony_ci kernel, the file descriptor returned by accept must be used. 8262306a36Sopenharmony_ci 8362306a36Sopenharmony_ciIn-place Cipher operation 8462306a36Sopenharmony_ci------------------------- 8562306a36Sopenharmony_ci 8662306a36Sopenharmony_ciJust like the in-kernel operation of the kernel crypto API, the user 8762306a36Sopenharmony_cispace interface allows the cipher operation in-place. That means that 8862306a36Sopenharmony_cithe input buffer used for the send/write system call and the output 8962306a36Sopenharmony_cibuffer used by the read/recv system call may be one and the same. This 9062306a36Sopenharmony_ciis of particular interest for symmetric cipher operations where a 9162306a36Sopenharmony_cicopying of the output data to its final destination can be avoided. 9262306a36Sopenharmony_ci 9362306a36Sopenharmony_ciIf a consumer on the other hand wants to maintain the plaintext and the 9462306a36Sopenharmony_ciciphertext in different memory locations, all a consumer needs to do is 9562306a36Sopenharmony_cito provide different memory pointers for the encryption and decryption 9662306a36Sopenharmony_cioperation. 9762306a36Sopenharmony_ci 9862306a36Sopenharmony_ciMessage Digest API 9962306a36Sopenharmony_ci------------------ 10062306a36Sopenharmony_ci 10162306a36Sopenharmony_ciThe message digest type to be used for the cipher operation is selected 10262306a36Sopenharmony_ciwhen invoking the bind syscall. bind requires the caller to provide a 10362306a36Sopenharmony_cifilled struct sockaddr data structure. This data structure must be 10462306a36Sopenharmony_cifilled as follows: 10562306a36Sopenharmony_ci 10662306a36Sopenharmony_ci:: 10762306a36Sopenharmony_ci 10862306a36Sopenharmony_ci struct sockaddr_alg sa = { 10962306a36Sopenharmony_ci .salg_family = AF_ALG, 11062306a36Sopenharmony_ci .salg_type = "hash", /* this selects the hash logic in the kernel */ 11162306a36Sopenharmony_ci .salg_name = "sha1" /* this is the cipher name */ 11262306a36Sopenharmony_ci }; 11362306a36Sopenharmony_ci 11462306a36Sopenharmony_ci 11562306a36Sopenharmony_ciThe salg_type value "hash" applies to message digests and keyed message 11662306a36Sopenharmony_cidigests. Though, a keyed message digest is referenced by the appropriate 11762306a36Sopenharmony_cisalg_name. Please see below for the setsockopt interface that explains 11862306a36Sopenharmony_cihow the key can be set for a keyed message digest. 11962306a36Sopenharmony_ci 12062306a36Sopenharmony_ciUsing the send() system call, the application provides the data that 12162306a36Sopenharmony_cishould be processed with the message digest. The send system call allows 12262306a36Sopenharmony_cithe following flags to be specified: 12362306a36Sopenharmony_ci 12462306a36Sopenharmony_ci- MSG_MORE: If this flag is set, the send system call acts like a 12562306a36Sopenharmony_ci message digest update function where the final hash is not yet 12662306a36Sopenharmony_ci calculated. If the flag is not set, the send system call calculates 12762306a36Sopenharmony_ci the final message digest immediately. 12862306a36Sopenharmony_ci 12962306a36Sopenharmony_ciWith the recv() system call, the application can read the message digest 13062306a36Sopenharmony_cifrom the kernel crypto API. If the buffer is too small for the message 13162306a36Sopenharmony_cidigest, the flag MSG_TRUNC is set by the kernel. 13262306a36Sopenharmony_ci 13362306a36Sopenharmony_ciIn order to set a message digest key, the calling application must use 13462306a36Sopenharmony_cithe setsockopt() option of ALG_SET_KEY or ALG_SET_KEY_BY_KEY_SERIAL. If the 13562306a36Sopenharmony_cikey is not set the HMAC operation is performed without the initial HMAC state 13662306a36Sopenharmony_cichange caused by the key. 13762306a36Sopenharmony_ci 13862306a36Sopenharmony_ciSymmetric Cipher API 13962306a36Sopenharmony_ci-------------------- 14062306a36Sopenharmony_ci 14162306a36Sopenharmony_ciThe operation is very similar to the message digest discussion. During 14262306a36Sopenharmony_ciinitialization, the struct sockaddr data structure must be filled as 14362306a36Sopenharmony_cifollows: 14462306a36Sopenharmony_ci 14562306a36Sopenharmony_ci:: 14662306a36Sopenharmony_ci 14762306a36Sopenharmony_ci struct sockaddr_alg sa = { 14862306a36Sopenharmony_ci .salg_family = AF_ALG, 14962306a36Sopenharmony_ci .salg_type = "skcipher", /* this selects the symmetric cipher */ 15062306a36Sopenharmony_ci .salg_name = "cbc(aes)" /* this is the cipher name */ 15162306a36Sopenharmony_ci }; 15262306a36Sopenharmony_ci 15362306a36Sopenharmony_ci 15462306a36Sopenharmony_ciBefore data can be sent to the kernel using the write/send system call 15562306a36Sopenharmony_cifamily, the consumer must set the key. The key setting is described with 15662306a36Sopenharmony_cithe setsockopt invocation below. 15762306a36Sopenharmony_ci 15862306a36Sopenharmony_ciUsing the sendmsg() system call, the application provides the data that 15962306a36Sopenharmony_cishould be processed for encryption or decryption. In addition, the IV is 16062306a36Sopenharmony_cispecified with the data structure provided by the sendmsg() system call. 16162306a36Sopenharmony_ci 16262306a36Sopenharmony_ciThe sendmsg system call parameter of struct msghdr is embedded into the 16362306a36Sopenharmony_cistruct cmsghdr data structure. See recv(2) and cmsg(3) for more 16462306a36Sopenharmony_ciinformation on how the cmsghdr data structure is used together with the 16562306a36Sopenharmony_cisend/recv system call family. That cmsghdr data structure holds the 16662306a36Sopenharmony_cifollowing information specified with a separate header instances: 16762306a36Sopenharmony_ci 16862306a36Sopenharmony_ci- specification of the cipher operation type with one of these flags: 16962306a36Sopenharmony_ci 17062306a36Sopenharmony_ci - ALG_OP_ENCRYPT - encryption of data 17162306a36Sopenharmony_ci 17262306a36Sopenharmony_ci - ALG_OP_DECRYPT - decryption of data 17362306a36Sopenharmony_ci 17462306a36Sopenharmony_ci- specification of the IV information marked with the flag ALG_SET_IV 17562306a36Sopenharmony_ci 17662306a36Sopenharmony_ciThe send system call family allows the following flag to be specified: 17762306a36Sopenharmony_ci 17862306a36Sopenharmony_ci- MSG_MORE: If this flag is set, the send system call acts like a 17962306a36Sopenharmony_ci cipher update function where more input data is expected with a 18062306a36Sopenharmony_ci subsequent invocation of the send system call. 18162306a36Sopenharmony_ci 18262306a36Sopenharmony_ciNote: The kernel reports -EINVAL for any unexpected data. The caller 18362306a36Sopenharmony_cimust make sure that all data matches the constraints given in 18462306a36Sopenharmony_ci/proc/crypto for the selected cipher. 18562306a36Sopenharmony_ci 18662306a36Sopenharmony_ciWith the recv() system call, the application can read the result of the 18762306a36Sopenharmony_cicipher operation from the kernel crypto API. The output buffer must be 18862306a36Sopenharmony_ciat least as large as to hold all blocks of the encrypted or decrypted 18962306a36Sopenharmony_cidata. If the output data size is smaller, only as many blocks are 19062306a36Sopenharmony_cireturned that fit into that output buffer size. 19162306a36Sopenharmony_ci 19262306a36Sopenharmony_ciAEAD Cipher API 19362306a36Sopenharmony_ci--------------- 19462306a36Sopenharmony_ci 19562306a36Sopenharmony_ciThe operation is very similar to the symmetric cipher discussion. During 19662306a36Sopenharmony_ciinitialization, the struct sockaddr data structure must be filled as 19762306a36Sopenharmony_cifollows: 19862306a36Sopenharmony_ci 19962306a36Sopenharmony_ci:: 20062306a36Sopenharmony_ci 20162306a36Sopenharmony_ci struct sockaddr_alg sa = { 20262306a36Sopenharmony_ci .salg_family = AF_ALG, 20362306a36Sopenharmony_ci .salg_type = "aead", /* this selects the symmetric cipher */ 20462306a36Sopenharmony_ci .salg_name = "gcm(aes)" /* this is the cipher name */ 20562306a36Sopenharmony_ci }; 20662306a36Sopenharmony_ci 20762306a36Sopenharmony_ci 20862306a36Sopenharmony_ciBefore data can be sent to the kernel using the write/send system call 20962306a36Sopenharmony_cifamily, the consumer must set the key. The key setting is described with 21062306a36Sopenharmony_cithe setsockopt invocation below. 21162306a36Sopenharmony_ci 21262306a36Sopenharmony_ciIn addition, before data can be sent to the kernel using the write/send 21362306a36Sopenharmony_cisystem call family, the consumer must set the authentication tag size. 21462306a36Sopenharmony_ciTo set the authentication tag size, the caller must use the setsockopt 21562306a36Sopenharmony_ciinvocation described below. 21662306a36Sopenharmony_ci 21762306a36Sopenharmony_ciUsing the sendmsg() system call, the application provides the data that 21862306a36Sopenharmony_cishould be processed for encryption or decryption. In addition, the IV is 21962306a36Sopenharmony_cispecified with the data structure provided by the sendmsg() system call. 22062306a36Sopenharmony_ci 22162306a36Sopenharmony_ciThe sendmsg system call parameter of struct msghdr is embedded into the 22262306a36Sopenharmony_cistruct cmsghdr data structure. See recv(2) and cmsg(3) for more 22362306a36Sopenharmony_ciinformation on how the cmsghdr data structure is used together with the 22462306a36Sopenharmony_cisend/recv system call family. That cmsghdr data structure holds the 22562306a36Sopenharmony_cifollowing information specified with a separate header instances: 22662306a36Sopenharmony_ci 22762306a36Sopenharmony_ci- specification of the cipher operation type with one of these flags: 22862306a36Sopenharmony_ci 22962306a36Sopenharmony_ci - ALG_OP_ENCRYPT - encryption of data 23062306a36Sopenharmony_ci 23162306a36Sopenharmony_ci - ALG_OP_DECRYPT - decryption of data 23262306a36Sopenharmony_ci 23362306a36Sopenharmony_ci- specification of the IV information marked with the flag ALG_SET_IV 23462306a36Sopenharmony_ci 23562306a36Sopenharmony_ci- specification of the associated authentication data (AAD) with the 23662306a36Sopenharmony_ci flag ALG_SET_AEAD_ASSOCLEN. The AAD is sent to the kernel together 23762306a36Sopenharmony_ci with the plaintext / ciphertext. See below for the memory structure. 23862306a36Sopenharmony_ci 23962306a36Sopenharmony_ciThe send system call family allows the following flag to be specified: 24062306a36Sopenharmony_ci 24162306a36Sopenharmony_ci- MSG_MORE: If this flag is set, the send system call acts like a 24262306a36Sopenharmony_ci cipher update function where more input data is expected with a 24362306a36Sopenharmony_ci subsequent invocation of the send system call. 24462306a36Sopenharmony_ci 24562306a36Sopenharmony_ciNote: The kernel reports -EINVAL for any unexpected data. The caller 24662306a36Sopenharmony_cimust make sure that all data matches the constraints given in 24762306a36Sopenharmony_ci/proc/crypto for the selected cipher. 24862306a36Sopenharmony_ci 24962306a36Sopenharmony_ciWith the recv() system call, the application can read the result of the 25062306a36Sopenharmony_cicipher operation from the kernel crypto API. The output buffer must be 25162306a36Sopenharmony_ciat least as large as defined with the memory structure below. If the 25262306a36Sopenharmony_cioutput data size is smaller, the cipher operation is not performed. 25362306a36Sopenharmony_ci 25462306a36Sopenharmony_ciThe authenticated decryption operation may indicate an integrity error. 25562306a36Sopenharmony_ciSuch breach in integrity is marked with the -EBADMSG error code. 25662306a36Sopenharmony_ci 25762306a36Sopenharmony_ciAEAD Memory Structure 25862306a36Sopenharmony_ci~~~~~~~~~~~~~~~~~~~~~ 25962306a36Sopenharmony_ci 26062306a36Sopenharmony_ciThe AEAD cipher operates with the following information that is 26162306a36Sopenharmony_cicommunicated between user and kernel space as one data stream: 26262306a36Sopenharmony_ci 26362306a36Sopenharmony_ci- plaintext or ciphertext 26462306a36Sopenharmony_ci 26562306a36Sopenharmony_ci- associated authentication data (AAD) 26662306a36Sopenharmony_ci 26762306a36Sopenharmony_ci- authentication tag 26862306a36Sopenharmony_ci 26962306a36Sopenharmony_ciThe sizes of the AAD and the authentication tag are provided with the 27062306a36Sopenharmony_cisendmsg and setsockopt calls (see there). As the kernel knows the size 27162306a36Sopenharmony_ciof the entire data stream, the kernel is now able to calculate the right 27262306a36Sopenharmony_cioffsets of the data components in the data stream. 27362306a36Sopenharmony_ci 27462306a36Sopenharmony_ciThe user space caller must arrange the aforementioned information in the 27562306a36Sopenharmony_cifollowing order: 27662306a36Sopenharmony_ci 27762306a36Sopenharmony_ci- AEAD encryption input: AAD \|\| plaintext 27862306a36Sopenharmony_ci 27962306a36Sopenharmony_ci- AEAD decryption input: AAD \|\| ciphertext \|\| authentication tag 28062306a36Sopenharmony_ci 28162306a36Sopenharmony_ciThe output buffer the user space caller provides must be at least as 28262306a36Sopenharmony_cilarge to hold the following data: 28362306a36Sopenharmony_ci 28462306a36Sopenharmony_ci- AEAD encryption output: ciphertext \|\| authentication tag 28562306a36Sopenharmony_ci 28662306a36Sopenharmony_ci- AEAD decryption output: plaintext 28762306a36Sopenharmony_ci 28862306a36Sopenharmony_ciRandom Number Generator API 28962306a36Sopenharmony_ci--------------------------- 29062306a36Sopenharmony_ci 29162306a36Sopenharmony_ciAgain, the operation is very similar to the other APIs. During 29262306a36Sopenharmony_ciinitialization, the struct sockaddr data structure must be filled as 29362306a36Sopenharmony_cifollows: 29462306a36Sopenharmony_ci 29562306a36Sopenharmony_ci:: 29662306a36Sopenharmony_ci 29762306a36Sopenharmony_ci struct sockaddr_alg sa = { 29862306a36Sopenharmony_ci .salg_family = AF_ALG, 29962306a36Sopenharmony_ci .salg_type = "rng", /* this selects the random number generator */ 30062306a36Sopenharmony_ci .salg_name = "drbg_nopr_sha256" /* this is the RNG name */ 30162306a36Sopenharmony_ci }; 30262306a36Sopenharmony_ci 30362306a36Sopenharmony_ci 30462306a36Sopenharmony_ciDepending on the RNG type, the RNG must be seeded. The seed is provided 30562306a36Sopenharmony_ciusing the setsockopt interface to set the key. For example, the 30662306a36Sopenharmony_ciansi_cprng requires a seed. The DRBGs do not require a seed, but may be 30762306a36Sopenharmony_ciseeded. The seed is also known as a *Personalization String* in NIST SP 800-90A 30862306a36Sopenharmony_cistandard. 30962306a36Sopenharmony_ci 31062306a36Sopenharmony_ciUsing the read()/recvmsg() system calls, random numbers can be obtained. 31162306a36Sopenharmony_ciThe kernel generates at most 128 bytes in one call. If user space 31262306a36Sopenharmony_cirequires more data, multiple calls to read()/recvmsg() must be made. 31362306a36Sopenharmony_ci 31462306a36Sopenharmony_ciWARNING: The user space caller may invoke the initially mentioned accept 31562306a36Sopenharmony_cisystem call multiple times. In this case, the returned file descriptors 31662306a36Sopenharmony_cihave the same state. 31762306a36Sopenharmony_ci 31862306a36Sopenharmony_ciFollowing CAVP testing interfaces are enabled when kernel is built with 31962306a36Sopenharmony_ciCRYPTO_USER_API_RNG_CAVP option: 32062306a36Sopenharmony_ci 32162306a36Sopenharmony_ci- the concatenation of *Entropy* and *Nonce* can be provided to the RNG via 32262306a36Sopenharmony_ci ALG_SET_DRBG_ENTROPY setsockopt interface. Setting the entropy requires 32362306a36Sopenharmony_ci CAP_SYS_ADMIN permission. 32462306a36Sopenharmony_ci 32562306a36Sopenharmony_ci- *Additional Data* can be provided using the send()/sendmsg() system calls, 32662306a36Sopenharmony_ci but only after the entropy has been set. 32762306a36Sopenharmony_ci 32862306a36Sopenharmony_ciZero-Copy Interface 32962306a36Sopenharmony_ci------------------- 33062306a36Sopenharmony_ci 33162306a36Sopenharmony_ciIn addition to the send/write/read/recv system call family, the AF_ALG 33262306a36Sopenharmony_ciinterface can be accessed with the zero-copy interface of 33362306a36Sopenharmony_cisplice/vmsplice. As the name indicates, the kernel tries to avoid a copy 33462306a36Sopenharmony_cioperation into kernel space. 33562306a36Sopenharmony_ci 33662306a36Sopenharmony_ciThe zero-copy operation requires data to be aligned at the page 33762306a36Sopenharmony_ciboundary. Non-aligned data can be used as well, but may require more 33862306a36Sopenharmony_cioperations of the kernel which would defeat the speed gains obtained 33962306a36Sopenharmony_cifrom the zero-copy interface. 34062306a36Sopenharmony_ci 34162306a36Sopenharmony_ciThe system-inherent limit for the size of one zero-copy operation is 16 34262306a36Sopenharmony_cipages. If more data is to be sent to AF_ALG, user space must slice the 34362306a36Sopenharmony_ciinput into segments with a maximum size of 16 pages. 34462306a36Sopenharmony_ci 34562306a36Sopenharmony_ciZero-copy can be used with the following code example (a complete 34662306a36Sopenharmony_ciworking example is provided with libkcapi): 34762306a36Sopenharmony_ci 34862306a36Sopenharmony_ci:: 34962306a36Sopenharmony_ci 35062306a36Sopenharmony_ci int pipes[2]; 35162306a36Sopenharmony_ci 35262306a36Sopenharmony_ci pipe(pipes); 35362306a36Sopenharmony_ci /* input data in iov */ 35462306a36Sopenharmony_ci vmsplice(pipes[1], iov, iovlen, SPLICE_F_GIFT); 35562306a36Sopenharmony_ci /* opfd is the file descriptor returned from accept() system call */ 35662306a36Sopenharmony_ci splice(pipes[0], NULL, opfd, NULL, ret, 0); 35762306a36Sopenharmony_ci read(opfd, out, outlen); 35862306a36Sopenharmony_ci 35962306a36Sopenharmony_ci 36062306a36Sopenharmony_ciSetsockopt Interface 36162306a36Sopenharmony_ci-------------------- 36262306a36Sopenharmony_ci 36362306a36Sopenharmony_ciIn addition to the read/recv and send/write system call handling to send 36462306a36Sopenharmony_ciand retrieve data subject to the cipher operation, a consumer also needs 36562306a36Sopenharmony_cito set the additional information for the cipher operation. This 36662306a36Sopenharmony_ciadditional information is set using the setsockopt system call that must 36762306a36Sopenharmony_cibe invoked with the file descriptor of the open cipher (i.e. the file 36862306a36Sopenharmony_cidescriptor returned by the accept system call). 36962306a36Sopenharmony_ci 37062306a36Sopenharmony_ciEach setsockopt invocation must use the level SOL_ALG. 37162306a36Sopenharmony_ci 37262306a36Sopenharmony_ciThe setsockopt interface allows setting the following data using the 37362306a36Sopenharmony_cimentioned optname: 37462306a36Sopenharmony_ci 37562306a36Sopenharmony_ci- ALG_SET_KEY -- Setting the key. Key setting is applicable to: 37662306a36Sopenharmony_ci 37762306a36Sopenharmony_ci - the skcipher cipher type (symmetric ciphers) 37862306a36Sopenharmony_ci 37962306a36Sopenharmony_ci - the hash cipher type (keyed message digests) 38062306a36Sopenharmony_ci 38162306a36Sopenharmony_ci - the AEAD cipher type 38262306a36Sopenharmony_ci 38362306a36Sopenharmony_ci - the RNG cipher type to provide the seed 38462306a36Sopenharmony_ci 38562306a36Sopenharmony_ci- ALG_SET_KEY_BY_KEY_SERIAL -- Setting the key via keyring key_serial_t. 38662306a36Sopenharmony_ci This operation behaves the same as ALG_SET_KEY. The decrypted 38762306a36Sopenharmony_ci data is copied from a keyring key, and uses that data as the 38862306a36Sopenharmony_ci key for symmetric encryption. 38962306a36Sopenharmony_ci 39062306a36Sopenharmony_ci The passed in key_serial_t must have the KEY_(POS|USR|GRP|OTH)_SEARCH 39162306a36Sopenharmony_ci permission set, otherwise -EPERM is returned. Supports key types: user, 39262306a36Sopenharmony_ci logon, encrypted, and trusted. 39362306a36Sopenharmony_ci 39462306a36Sopenharmony_ci- ALG_SET_AEAD_AUTHSIZE -- Setting the authentication tag size for 39562306a36Sopenharmony_ci AEAD ciphers. For a encryption operation, the authentication tag of 39662306a36Sopenharmony_ci the given size will be generated. For a decryption operation, the 39762306a36Sopenharmony_ci provided ciphertext is assumed to contain an authentication tag of 39862306a36Sopenharmony_ci the given size (see section about AEAD memory layout below). 39962306a36Sopenharmony_ci 40062306a36Sopenharmony_ci- ALG_SET_DRBG_ENTROPY -- Setting the entropy of the random number generator. 40162306a36Sopenharmony_ci This option is applicable to RNG cipher type only. 40262306a36Sopenharmony_ci 40362306a36Sopenharmony_ciUser space API example 40462306a36Sopenharmony_ci---------------------- 40562306a36Sopenharmony_ci 40662306a36Sopenharmony_ciPlease see [1] for libkcapi which provides an easy-to-use wrapper around 40762306a36Sopenharmony_cithe aforementioned Netlink kernel interface. [1] also contains a test 40862306a36Sopenharmony_ciapplication that invokes all libkcapi API calls. 40962306a36Sopenharmony_ci 41062306a36Sopenharmony_ci[1] https://www.chronox.de/libkcapi.html 411