at v4.0-rc2 205 lines 8.3 kB view raw
1Introduction 2============ 3 4The concepts of the kernel crypto API visible to kernel space is fully 5applicable to the user space interface as well. Therefore, the kernel crypto API 6high level discussion for the in-kernel use cases applies here as well. 7 8The major difference, however, is that user space can only act as a consumer 9and never as a provider of a transformation or cipher algorithm. 10 11The following covers the user space interface exported by the kernel crypto 12API. A working example of this description is libkcapi that can be obtained from 13[1]. That library can be used by user space applications that require 14cryptographic services from the kernel. 15 16Some details of the in-kernel kernel crypto API aspects do not 17apply to user space, however. This includes the difference between synchronous 18and asynchronous invocations. The user space API call is fully synchronous. 19In addition, only a subset of all cipher types are available as documented 20below. 21 22 23User space API general remarks 24============================== 25 26The kernel crypto API is accessible from user space. Currently, the following 27ciphers are accessible: 28 29 * Message digest including keyed message digest (HMAC, CMAC) 30 31 * Symmetric ciphers 32 33Note, AEAD ciphers are currently not supported via the symmetric cipher 34interface. 35 36The interface is provided via Netlink using the type AF_ALG. In addition, the 37setsockopt option type is SOL_ALG. In case the user space header files do not 38export these flags yet, use the following macros: 39 40#ifndef AF_ALG 41#define AF_ALG 38 42#endif 43#ifndef SOL_ALG 44#define SOL_ALG 279 45#endif 46 47A cipher is accessed with the same name as done for the in-kernel API calls. 48This includes the generic vs. unique naming schema for ciphers as well as the 49enforcement of priorities for generic names. 50 51To interact with the kernel crypto API, a Netlink socket must be created by 52the user space application. User space invokes the cipher operation with the 53send/write system call family. The result of the cipher operation is obtained 54with the read/recv system call family. 55 56The following API calls assume that the Netlink socket descriptor is already 57opened by the user space application and discusses only the kernel crypto API 58specific invocations. 59 60To initialize a Netlink interface, the following sequence has to be performed 61by the consumer: 62 63 1. Create a socket of type AF_ALG with the struct sockaddr_alg parameter 64 specified below for the different cipher types. 65 66 2. Invoke bind with the socket descriptor 67 68 3. Invoke accept with the socket descriptor. The accept system call 69 returns a new file descriptor that is to be used to interact with 70 the particular cipher instance. When invoking send/write or recv/read 71 system calls to send data to the kernel or obtain data from the 72 kernel, the file descriptor returned by accept must be used. 73 74In-place cipher operation 75========================= 76 77Just like the in-kernel operation of the kernel crypto API, the user space 78interface allows the cipher operation in-place. That means that the input buffer 79used for the send/write system call and the output buffer used by the read/recv 80system call may be one and the same. This is of particular interest for 81symmetric cipher operations where a copying of the output data to its final 82destination can be avoided. 83 84If a consumer on the other hand wants to maintain the plaintext and the 85ciphertext in different memory locations, all a consumer needs to do is to 86provide different memory pointers for the encryption and decryption operation. 87 88Message digest API 89================== 90 91The message digest type to be used for the cipher operation is selected when 92invoking the bind syscall. bind requires the caller to provide a filled 93struct sockaddr data structure. This data structure must be filled as follows: 94 95struct sockaddr_alg sa = { 96 .salg_family = AF_ALG, 97 .salg_type = "hash", /* this selects the hash logic in the kernel */ 98 .salg_name = "sha1" /* this is the cipher name */ 99}; 100 101The salg_type value "hash" applies to message digests and keyed message digests. 102Though, a keyed message digest is referenced by the appropriate salg_name. 103Please see below for the setsockopt interface that explains how the key can be 104set for a keyed message digest. 105 106Using the send() system call, the application provides the data that should be 107processed with the message digest. The send system call allows the following 108flags to be specified: 109 110 * MSG_MORE: If this flag is set, the send system call acts like a 111 message digest update function where the final hash is not 112 yet calculated. If the flag is not set, the send system call 113 calculates the final message digest immediately. 114 115With the recv() system call, the application can read the message digest from 116the kernel crypto API. If the buffer is too small for the message digest, the 117flag MSG_TRUNC is set by the kernel. 118 119In order to set a message digest key, the calling application must use the 120setsockopt() option of ALG_SET_KEY. If the key is not set the HMAC operation is 121performed without the initial HMAC state change caused by the key. 122 123 124Symmetric cipher API 125==================== 126 127The operation is very similar to the message digest discussion. During 128initialization, the struct sockaddr data structure must be filled as follows: 129 130struct sockaddr_alg sa = { 131 .salg_family = AF_ALG, 132 .salg_type = "skcipher", /* this selects the symmetric cipher */ 133 .salg_name = "cbc(aes)" /* this is the cipher name */ 134}; 135 136Before data can be sent to the kernel using the write/send system call family, 137the consumer must set the key. The key setting is described with the setsockopt 138invocation below. 139 140Using the sendmsg() system call, the application provides the data that should 141be processed for encryption or decryption. In addition, the IV is specified 142with the data structure provided by the sendmsg() system call. 143 144The sendmsg system call parameter of struct msghdr is embedded into the 145struct cmsghdr data structure. See recv(2) and cmsg(3) for more information 146on how the cmsghdr data structure is used together with the send/recv system 147call family. That cmsghdr data structure holds the following information 148specified with a separate header instances: 149 150 * specification of the cipher operation type with one of these flags: 151 ALG_OP_ENCRYPT - encryption of data 152 ALG_OP_DECRYPT - decryption of data 153 154 * specification of the IV information marked with the flag ALG_SET_IV 155 156The send system call family allows the following flag to be specified: 157 158 * MSG_MORE: If this flag is set, the send system call acts like a 159 cipher update function where more input data is expected 160 with a subsequent invocation of the send system call. 161 162Note: The kernel reports -EINVAL for any unexpected data. The caller must 163make sure that all data matches the constraints given in /proc/crypto for the 164selected cipher. 165 166With the recv() system call, the application can read the result of the 167cipher operation from the kernel crypto API. The output buffer must be at least 168as large as to hold all blocks of the encrypted or decrypted data. If the output 169data size is smaller, only as many blocks are returned that fit into that 170output buffer size. 171 172Setsockopt interface 173==================== 174 175In addition to the read/recv and send/write system call handling to send and 176retrieve data subject to the cipher operation, a consumer also needs to set 177the additional information for the cipher operation. This additional information 178is set using the setsockopt system call that must be invoked with the file 179descriptor of the open cipher (i.e. the file descriptor returned by the 180accept system call). 181 182Each setsockopt invocation must use the level SOL_ALG. 183 184The setsockopt interface allows setting the following data using the mentioned 185optname: 186 187 * ALG_SET_KEY -- Setting the key. Key setting is applicable to: 188 189 - the skcipher cipher type (symmetric ciphers) 190 191 - the hash cipher type (keyed message digests) 192 193User space API example 194====================== 195 196Please see [1] for libkcapi which provides an easy-to-use wrapper around the 197aforementioned Netlink kernel interface. [1] also contains a test application 198that invokes all libkcapi API calls. 199 200[1] http://www.chronox.de/libkcapi.html 201 202Author 203====== 204 205Stephan Mueller <smueller@chronox.de>