205 lines · 8.1 KB · Text
RawDownload
Introduction============ The concepts of the kernel crypto API visible to kernel space is fullyapplicable to the user space interface as well. Therefore, the kernel crypto APIhigh level discussion for the in-kernel use cases applies here as well. The major difference, however, is that user space can only act as a consumerand never as a provider of a transformation or cipher algorithm. The following covers the user space interface exported by the kernel cryptoAPI. A working example of this description is libkcapi that can be obtained from[1]. That library can be used by user space applications that requirecryptographic services from the kernel. Some details of the in-kernel kernel crypto API aspects do notapply to user space, however. This includes the difference between synchronousand asynchronous invocations. The user space API call is fully synchronous.In addition, only a subset of all cipher types are available as documentedbelow.  User space API general remarks============================== The kernel crypto API is accessible from user space. Currently, the followingciphers are accessible: 	* Message digest including keyed message digest (HMAC, CMAC) 	* Symmetric ciphers Note, AEAD ciphers are currently not supported via the symmetric cipherinterface. The interface is provided via Netlink using the type AF_ALG. In addition, thesetsockopt option type is SOL_ALG. In case the user space header files do notexport these flags yet, use the following macros: #ifndef AF_ALG#define AF_ALG 38#endif#ifndef SOL_ALG#define SOL_ALG 279#endif A cipher is accessed with the same name as done for the in-kernel API calls.This includes the generic vs. unique naming schema for ciphers as well as theenforcement of priorities for generic names. To interact with the kernel crypto API, a Netlink socket must be created bythe user space application. User space invokes the cipher operation with thesend/write system call family. The result of the cipher operation is obtainedwith the read/recv system call family. The following API calls assume that the Netlink socket descriptor is alreadyopened by the user space application and discusses only the kernel crypto APIspecific invocations. To initialize a Netlink interface, the following sequence has to be performedby the consumer: 	1. Create a socket of type AF_ALG with the struct sockaddr_alg parameter	   specified below for the different cipher types. 	2. Invoke bind with the socket descriptor 	3. Invoke accept with the socket descriptor. The accept system call	   returns a new file descriptor that is to be used to interact with	   the particular cipher instance. When invoking send/write or recv/read	   system calls to send data to the kernel or obtain data from the	   kernel, the file descriptor returned by accept must be used. In-place cipher operation========================= Just like the in-kernel operation of the kernel crypto API, the user spaceinterface allows the cipher operation in-place. That means that the input bufferused for the send/write system call and the output buffer used by the read/recvsystem call may be one and the same. This is of particular interest forsymmetric cipher operations where a copying of the output data to its finaldestination can be avoided. If a consumer on the other hand wants to maintain the plaintext and theciphertext in different memory locations, all a consumer needs to do is toprovide different memory pointers for the encryption and decryption operation. Message digest API================== The message digest type to be used for the cipher operation is selected wheninvoking the bind syscall. bind requires the caller to provide a filledstruct sockaddr data structure. This data structure must be filled as follows: struct sockaddr_alg sa = {	.salg_family = AF_ALG,	.salg_type = "hash", /* this selects the hash logic in the kernel */	.salg_name = "sha1" /* this is the cipher name */}; The salg_type value "hash" applies to message digests and keyed message digests.Though, a keyed message digest is referenced by the appropriate salg_name.Please see below for the setsockopt interface that explains how the key can beset for a keyed message digest. Using the send() system call, the application provides the data that should beprocessed with the message digest. The send system call allows the followingflags to be specified: 	* MSG_MORE: If this flag is set, the send system call acts like a		    message digest update function where the final hash is not		    yet calculated. If the flag is not set, the send system call		    calculates the final message digest immediately. With the recv() system call, the application can read the message digest fromthe kernel crypto API. If the buffer is too small for the message digest, theflag MSG_TRUNC is set by the kernel. In order to set a message digest key, the calling application must use thesetsockopt() option of ALG_SET_KEY. If the key is not set the HMAC operation isperformed without the initial HMAC state change caused by the key.  Symmetric cipher API==================== The operation is very similar to the message digest discussion. Duringinitialization, the struct sockaddr data structure must be filled as follows: struct sockaddr_alg sa = {	.salg_family = AF_ALG,	.salg_type = "skcipher", /* this selects the symmetric cipher */	.salg_name = "cbc(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 setsockoptinvocation below. Using the sendmsg() system call, the application provides the data that shouldbe processed for encryption or decryption. In addition, the IV is specifiedwith the data structure provided by the sendmsg() system call. The sendmsg system call parameter of struct msghdr is embedded into thestruct cmsghdr data structure. See recv(2) and cmsg(3) for more informationon how the cmsghdr data structure is used together with the send/recv systemcall family. That cmsghdr data structure holds the following informationspecified 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 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 mustmake sure that all data matches the constraints given in /proc/crypto for theselected cipher. With the recv() system call, the application can read the result of thecipher operation from the kernel crypto API. The output buffer must be at leastas large as to hold all blocks of the encrypted or decrypted data. If the outputdata size is smaller, only as many blocks are returned that fit into thatoutput buffer size. Setsockopt interface==================== In addition to the read/recv and send/write system call handling to send andretrieve data subject to the cipher operation, a consumer also needs to setthe additional information for the cipher operation. This additional informationis set using the setsockopt system call that must be invoked with the filedescriptor of the open cipher (i.e. the file descriptor returned by theaccept system call). Each setsockopt invocation must use the level SOL_ALG. The setsockopt interface allows setting the following data using the mentionedoptname: 	* ALG_SET_KEY -- Setting the key. Key setting is applicable to: 		- the skcipher cipher type (symmetric ciphers) 		- the hash cipher type (keyed message digests) User space API example====================== Please see [1] for libkcapi which provides an easy-to-use wrapper around theaforementioned Netlink kernel interface. [1] also contains a test applicationthat invokes all libkcapi API calls. [1] http://www.chronox.de/libkcapi.html Author====== Stephan Mueller <smueller@chronox.de>

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t