| 1 |
<?xml version="1.0" encoding="UTF-8"?> |
| 2 |
<!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.1.2//EN" |
| 3 |
"http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd" []> |
| 4 |
|
| 5 |
<book id="libkcapi" revision="@@LIBVERSION@@"> |
| 6 |
<bookinfo> |
| 7 |
<title>Linux Kernel Crypto API User Space Interface Library</title> |
| 8 |
|
| 9 |
<edition>@@LIBVERSION@@</edition> |
| 10 |
|
| 11 |
<authorgroup> |
| 12 |
<author> |
| 13 |
<firstname>Stephan</firstname> |
| 14 |
<surname>Mueller</surname> |
| 15 |
<affiliation> |
| 16 |
<address> |
| 17 |
<email>[email protected]</email> |
| 18 |
</address> |
| 19 |
</affiliation> |
| 20 |
</author> |
| 21 |
</authorgroup> |
| 22 |
|
| 23 |
<copyright> |
| 24 |
<year>2014</year> |
| 25 |
<holder>Stephan Mueller</holder> |
| 26 |
</copyright> |
| 27 |
|
| 28 |
|
| 29 |
<legalnotice> |
| 30 |
<para> |
| 31 |
This documentation is free software; you can redistribute |
| 32 |
it and/or modify it under the terms of the GNU General Public |
| 33 |
License as published by the Free Software Foundation; either |
| 34 |
version 2 of the License, or (at your option) any later |
| 35 |
version. |
| 36 |
</para> |
| 37 |
|
| 38 |
<para> |
| 39 |
This program is distributed in the hope that it will be |
| 40 |
useful, but WITHOUT ANY WARRANTY; without even the implied |
| 41 |
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 42 |
See the GNU General Public License for more details. |
| 43 |
</para> |
| 44 |
|
| 45 |
<para> |
| 46 |
You should have received a copy of the GNU General Public |
| 47 |
License along with this program; if not, write to the Free |
| 48 |
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 49 |
MA 02111-1307 USA |
| 50 |
</para> |
| 51 |
|
| 52 |
<para> |
| 53 |
For more details see the file COPYING in the source |
| 54 |
distribution of Linux. |
| 55 |
</para> |
| 56 |
</legalnotice> |
| 57 |
</bookinfo> |
| 58 |
|
| 59 |
<toc></toc> |
| 60 |
|
| 61 |
<chapter id="Intro"> |
| 62 |
<title>libkcapi - Linux Kernel Crypto API User Space Interface Library</title> |
| 63 |
|
| 64 |
<para>This documentation applies to version @@LIBVERSION@@.</para> |
| 65 |
|
| 66 |
<para> |
| 67 |
The Linux kernel exports a network interface of type AF_ALG to allow user |
| 68 |
space to utilize the kernel crypto API. |
| 69 |
</para> |
| 70 |
|
| 71 |
<para> |
| 72 |
libkcapi uses this network interface and exports easy to use APIs so that |
| 73 |
a developer does not need to consider the low-level network interface |
| 74 |
handling. |
| 75 |
</para> |
| 76 |
|
| 77 |
<para> |
| 78 |
The library does not implement any cipher algorithms. All consumer requests |
| 79 |
are sent to the kernel for processing. Results from the kernel crypto API |
| 80 |
are returned to the consumer via the library API. |
| 81 |
</para> |
| 82 |
|
| 83 |
<para> |
| 84 |
The kernel interface and therefore this library can be used by unprivileged |
| 85 |
processes. |
| 86 |
</para> |
| 87 |
|
| 88 |
<sect1><title>Version Number Schema</title> |
| 89 |
<para> |
| 90 |
The version numbers for this library have the following schema: |
| 91 |
MAJOR.MINOR.PATCHLEVEL |
| 92 |
</para> |
| 93 |
|
| 94 |
<para> |
| 95 |
Changes in the major number implies API and ABI incompatible changes, or |
| 96 |
functional changes that require consumer to be updated (as long as this |
| 97 |
number is zero, the API is not considered stable and can change without a |
| 98 |
bump of the major version). |
| 99 |
</para> |
| 100 |
|
| 101 |
<para> |
| 102 |
Changes in the minor version are API compatible, but the ABI may change. |
| 103 |
Functional enhancements only are added. Thus, a consumer can be left |
| 104 |
unchanged if enhancements are not considered. The consumer only needs to |
| 105 |
be recompiled. |
| 106 |
</para> |
| 107 |
|
| 108 |
<para> |
| 109 |
Patchlevel changes are API / ABI compatible. No functional changes, no |
| 110 |
enhancements are made. This release is a bug fixe release only. The |
| 111 |
consumer can be left unchanged and does not need to be recompiled. |
| 112 |
</para> |
| 113 |
</sect1> |
| 114 |
|
| 115 |
<sect1><title>Purpose Of AF_ALG</title> |
| 116 |
<para> |
| 117 |
With the presence of numerous user space cryptographic libraries, |
| 118 |
one may ask why is there a need for the kernel to expose its kernel |
| 119 |
crypto API to user space. As there are system calls and potentially |
| 120 |
memory copies needed before a cipher can be invoked, it should be |
| 121 |
typically slower than user space shared libraries. |
| 122 |
</para> |
| 123 |
|
| 124 |
<para> |
| 125 |
There are several reasons for AF_ALG: |
| 126 |
<itemizedlist> |
| 127 |
<listitem> |
| 128 |
<para> |
| 129 |
The first and most important item is the access to hardware |
| 130 |
accelerators and hardware devices whose technical interface can |
| 131 |
only be accessed from the kernel mode / supervisor state of the |
| 132 |
processor. Such support cannot be used from user space except |
| 133 |
through AF_ALG. |
| 134 |
</para> |
| 135 |
</listitem> |
| 136 |
<listitem> |
| 137 |
<para> |
| 138 |
When using user space libraries, all key material and other |
| 139 |
cryptographic sensitive parameters remains in the calling |
| 140 |
application's memory even when the application supplied the |
| 141 |
information to the library. When using AF_ALG, the key material |
| 142 |
and other sensitive parameters are handed to the kernel. The |
| 143 |
calling application now can reliably erase that information |
| 144 |
from its memory and just use the cipher handle to perform |
| 145 |
the cryptographic operations. If the application is cracked |
| 146 |
an attacker cannot obtain the key material. |
| 147 |
</para> |
| 148 |
</listitem> |
| 149 |
<listitem> |
| 150 |
<para> |
| 151 |
On memory constrained systems like embedded systems, the additional |
| 152 |
memory footprint of a user space cryptographic library may |
| 153 |
be too much. As the kernel requires the kernel crypto API to be |
| 154 |
present, reusing existing code should reduce the memory footprint. |
| 155 |
</para> |
| 156 |
</listitem> |
| 157 |
</itemizedlist> |
| 158 |
</para> |
| 159 |
|
| 160 |
</sect1> |
| 161 |
</chapter> |
| 162 |
|
| 163 |
<chapter id="Usage"><title>Programming Guidelines</title> |
| 164 |
<para> |
| 165 |
A consumer has to use the kcapi.h header file to link with libkcapi. The |
| 166 |
linking has to be performed using -lkcapi. |
| 167 |
</para> |
| 168 |
|
| 169 |
<para> |
| 170 |
In case a consumer does not want a shared library, the libkcapi C file and |
| 171 |
header file can also just copied to the consumer code and compiled along |
| 172 |
with it. |
| 173 |
</para> |
| 174 |
|
| 175 |
<para> |
| 176 |
A general requirement must be observed: setting of keys must be performed |
| 177 |
before any operation. Re-setting of keys is only permissible once |
| 178 |
all data in flight (sent to the kernel but the kernel's result is not yet |
| 179 |
obtained) is processed, i.e. no data is in flight any more. |
| 180 |
</para> |
| 181 |
|
| 182 |
<sect1><title>Convenience Functions</title> |
| 183 |
<para> |
| 184 |
To support various use cases, the API provided with libkcapi is extensive. |
| 185 |
Though, some developers want to simply use a given cipher without any |
| 186 |
specific details. To accommodate such users, libkcapi provides convenience |
| 187 |
functions or convenience wrappers. |
| 188 |
</para> |
| 189 |
|
| 190 |
<para> |
| 191 |
The convenience functions provide exactly one function call to perform one |
| 192 |
complete cipher operation, such as an AES CBC encryption operation or a |
| 193 |
SHA-256 hashing. The caller only needs to provide the input and |
| 194 |
output buffers of his data. The entire intrinsic operation of libkcapi |
| 195 |
is hidden from the user. |
| 196 |
</para> |
| 197 |
|
| 198 |
<para> |
| 199 |
Convenience functions are provided for the different cipher types. They are |
| 200 |
clearly marked in the API specification below. |
| 201 |
</para> |
| 202 |
</sect1> |
| 203 |
|
| 204 |
<sect1><title>Synchronous Symmetric Cipher API</title> |
| 205 |
<para> |
| 206 |
Symmetric ciphers can be used in the following different ways: |
| 207 |
<itemizedlist> |
| 208 |
<listitem> |
| 209 |
<para> |
| 210 |
One-shot API: The one-shot API performs an encryption operation |
| 211 |
with one API call. With that API call, the caller provides the input |
| 212 |
data and immediately receives the output from the cipher operation. |
| 213 |
</para> |
| 214 |
</listitem> |
| 215 |
<listitem> |
| 216 |
<para> |
| 217 |
Stream API: With the stream API, the caller can implement independent |
| 218 |
calls to send data to the kernel and receive data from the kernel. |
| 219 |
Multiple send calls can be inter-mixed with multiple receive calls. |
| 220 |
If the kernel buffer is full, the caller of a subsequent send call will |
| 221 |
be put to sleep. Conversely, if the buffer is empty, a caller trying |
| 222 |
to read data resulting from a cipher operation will be put to sleep. |
| 223 |
Sleeping callers will be woken up by the kernel once buffer space |
| 224 |
becomes available or data becomes available, respectively. The detached |
| 225 |
nature of the sending/receiving operation allows the implementation of |
| 226 |
multi-threaded applications where one or more threads send data and one |
| 227 |
or more threads receive data. The threads must operate on the same |
| 228 |
cipher handle. However, access to that cipher handle does not need to |
| 229 |
be serialized when the stream API calls are invoked as the API calls |
| 230 |
only read the cipher handle. |
| 231 |
</para> |
| 232 |
</listitem> |
| 233 |
</itemizedlist> |
| 234 |
</para> |
| 235 |
</sect1> |
| 236 |
|
| 237 |
<sect1><title>Asynchronous Symmetric Cipher API</title> |
| 238 |
<para> |
| 239 |
In addition to the symmetric cipher API, an asynchronous API is offered |
| 240 |
with the kcapi_cipher_*_aio and kcapi_aead_*_aio API calls. The concept of |
| 241 |
that API is to perform parallel operations of multiple encryption or |
| 242 |
decryption data streams. |
| 243 |
</para> |
| 244 |
|
| 245 |
<para> |
| 246 |
To use the AIO API, the caller must use the KCAPI_INIT_AIO with the |
| 247 |
kcapi_cipher_init function call to set up all additional logistics for |
| 248 |
handing AIO. That means, users which are not interested in AIO will |
| 249 |
not suffer from the additional memory overhead including the time required |
| 250 |
to allocate that memory required for AIO. |
| 251 |
</para> |
| 252 |
|
| 253 |
<para> |
| 254 |
This implies that the asynchronous API handles the scatter-gather lists |
| 255 |
referenced by the IOVECs differently compared to the synchronous APIs. |
| 256 |
Whereas the synchronous API references different parts of plaintext or |
| 257 |
ciphertext that are processed with one cipher operation, the IOVECs of the |
| 258 |
asynchronous API references plaintext or ciphertext where each IOVEC |
| 259 |
is processed with an independent cipher operation. I.e. when using AES-CBC |
| 260 |
with the synchronous API and the scatter-gather lists, all input data is |
| 261 |
sent to one invocation of the AES-CBC cipher. Conversely, the asynchronous |
| 262 |
API invokes one individual AES-CBC operation for each individual IOVEC. |
| 263 |
</para> |
| 264 |
|
| 265 |
<para> |
| 266 |
The asynchronous API is designed to perform an in-place operation where |
| 267 |
the buffers for the input data are used to store the output data. |
| 268 |
</para> |
| 269 |
|
| 270 |
<para> |
| 271 |
The asynchronous API in libkcapi as well as the kernel has a higher |
| 272 |
overhead for setting the cipher operation up. That means that if the |
| 273 |
caller only uses a one IOVEC with one associated cipher operation, the |
| 274 |
asynchronous API is expected to be slower compared to the synchronous API. |
| 275 |
But already with two or three combined cipher operations, the AIO |
| 276 |
API should be faster than the synchronous API. You may test the difference |
| 277 |
in performance with the test/kcapi test application by using the options -f |
| 278 |
for measuring the time of cipher operations in nanoseconds, -d for the |
| 279 |
number of parallel invocations and -x 1 for a symmetric one-shot cipher |
| 280 |
invocation and -x 9 for an asymmetric cipher operation with the given input |
| 281 |
data. |
| 282 |
</para> |
| 283 |
|
| 284 |
<para> |
| 285 |
The kernel offers the AIO interface since kernel version 4.1 (symmetric |
| 286 |
ciphers) and 4.7 (AEAD ciphers). The libkcapi implements a transparent |
| 287 |
fallback to use the synchronous cipher API in case the AIO support is not |
| 288 |
present for the current kernel. This allows the calling users to be |
| 289 |
agnostic of the kernel support. Nonetheless, libkcapi with report the lack |
| 290 |
of AIO support if AIO is requested as the fallback implementation has a |
| 291 |
slight performance overhead. |
| 292 |
</para> |
| 293 |
</sect1> |
| 294 |
|
| 295 |
<sect1><title>AEAD Cipher API</title> |
| 296 |
<para> |
| 297 |
AEAD ciphers implement a very similar API approach as the symmetric ciphers: |
| 298 |
<itemizedlist> |
| 299 |
<listitem> |
| 300 |
<para> |
| 301 |
One-shot API: The one-shot API performs an encryption operation |
| 302 |
with one API call. With that API call, the caller provides the input |
| 303 |
data and immediately receives the output from the cipher operation. |
| 304 |
</para> |
| 305 |
</listitem> |
| 306 |
<listitem> |
| 307 |
<para> |
| 308 |
Stream API: With the stream API, the caller can implement independent |
| 309 |
calls to send data to the kernel and receive data from the kernel. |
| 310 |
However, unlike the symmetric cipher API, one AEAD cipher operation |
| 311 |
must be considered as one unit as the integrity value is calculated |
| 312 |
for one encryption or decryption operation. The caller can use multiple |
| 313 |
calls to provide the input data. The last chunk of data must be |
| 314 |
sent to the kernel with the API call marking the last submission. |
| 315 |
Then, the cipher operation can be triggered with the recvmsg invocation. |
| 316 |
It is possible to implement a multi-threaded application as the |
| 317 |
thread triggering the cipher operation is put to sleep until the last |
| 318 |
block is received. Once the last block is received, the caller waiting |
| 319 |
on the cipher operation is woken up to obtain the data. |
| 320 |
</para> |
| 321 |
</listitem> |
| 322 |
</itemizedlist> |
| 323 |
</para> |
| 324 |
|
| 325 |
<sect2><title>Aynchronous AEAD Cipher API</title> |
| 326 |
<para> |
| 327 |
Similarly to the symmetric cipher API, the AEAD API supports |
| 328 |
asynchronous operation as well. The same concept regarding the IOVECs |
| 329 |
applies as discussed for the asynchronous symmetric cipher API above. |
| 330 |
</para> |
| 331 |
</sect2> |
| 332 |
|
| 333 |
<sect2><title>AEAD Memory Structure</title> |
| 334 |
<para> |
| 335 |
When using the stream API for AEAD, the caller must observe a particular |
| 336 |
order of data components. It is permissible that for each of the following |
| 337 |
data components multiple send calls are used. But in total, all send calls |
| 338 |
must send the AEAD data in the requested sequence. That sequence has |
| 339 |
changed with kernel 4.9. The following sequence is applicable to kernel |
| 340 |
versions up to and including 4.8: |
| 341 |
</para> |
| 342 |
|
| 343 |
<orderedlist> |
| 344 |
<listitem> |
| 345 |
<para>Associated Authentication Data: The AAD must be provided as a first |
| 346 |
chunk.</para> |
| 347 |
</listitem> |
| 348 |
<listitem> |
| 349 |
<para>Plaintext / Ciphertext: Following the AAD, the entire plaintext |
| 350 |
or ciphertext is provided that shall be encrypted and integrity protected |
| 351 |
or decrypted and whose integrity shall be verified.</para> |
| 352 |
</listitem> |
| 353 |
<listitem> |
| 354 |
<para>Authentication Tag: Regardless of an encryption or decryption, |
| 355 |
the authentication tag memory must be provided.</para> |
| 356 |
</listitem> |
| 357 |
</orderedlist> |
| 358 |
|
| 359 |
<para> |
| 360 |
The caller must provide memory that is identical in size for the input and |
| 361 |
output data, even parts of the memory is unused. For example, for |
| 362 |
encryption, the AEAD cipher operation only needs the AAD and the plaintext. |
| 363 |
Nonetheless, the interface requires that the memory is big enough to |
| 364 |
hold the tag as well. This requirement particularly aids the in-place |
| 365 |
cipher operation. |
| 366 |
</para> |
| 367 |
|
| 368 |
<para> |
| 369 |
Starting with kernel 4.9, the interface changed slightly such that the |
| 370 |
authentication tag memory is only needed in the output buffer for |
| 371 |
encryption and in the input buffer for decryption. |
| 372 |
</para> |
| 373 |
|
| 374 |
<para> |
| 375 |
To allow the calling application to be agnostic about the differences |
| 376 |
in the kernel interface, the calling application is offered additional |
| 377 |
API calls which should be used as follows: |
| 378 |
</para> |
| 379 |
|
| 380 |
<orderedlist> |
| 381 |
<listitem> |
| 382 |
<para>Obtain the required input buffer length for the cryptographic |
| 383 |
operation using the calls kcapi_aead_inbuflen_enc or |
| 384 |
kcapi_aead_inbuflen_dec.</para> |
| 385 |
</listitem> |
| 386 |
<listitem> |
| 387 |
<para>Obtain the required output buffer length for the cryptographic |
| 388 |
operation using the APIs of kcapi_aead_outbuflen_enc or |
| 389 |
kcapi_aead_outbuflen_dec.</para> |
| 390 |
</listitem> |
| 391 |
|
| 392 |
<listitem> |
| 393 |
<para>For an in-place operation with a linear buffer, do the following |
| 394 |
(for an example, see test/kcapi-main.c:cavs_aead()): |
| 395 |
<orderedlist> |
| 396 |
<listitem> |
| 397 |
<para>allocate memory that is max(inbuflen, outbuflen),</para> |
| 398 |
</listitem> |
| 399 |
<listitem> |
| 400 |
<para>call to kcapi_aead_getdata_input and kcapi_aead_getdata_output |
| 401 |
with the allocated memory pointer to obtain the pointers into that |
| 402 |
allocated memory where the AAD, plaintext / ciphertext and tag is to be |
| 403 |
provided,</para> |
| 404 |
</listitem> |
| 405 |
<listitem> |
| 406 |
<para>fill these AAD, plaintext/ciphertext and tag pointers with the |
| 407 |
respective data if they are non-NULL -- note, a NULL pointer may |
| 408 |
be returned for the tag pointer,</para> |
| 409 |
</listitem> |
| 410 |
<listitem> |
| 411 |
<para>invoke the crypto operation with the pointer to the allocated |
| 412 |
buffer and inbuflen is supplied to the.</para> |
| 413 |
</listitem> |
| 414 |
</orderedlist> |
| 415 |
</para> |
| 416 |
</listitem> |
| 417 |
|
| 418 |
<listitem> |
| 419 |
<para>For for separate, potentially non-contiguous buffers, do the |
| 420 |
following (for an example, see test/kcapi-main.c:cavs_aead_stream()): |
| 421 |
<orderedlist> |
| 422 |
<listitem> |
| 423 |
<para>ensure that your total buffer size for input and output complies |
| 424 |
with the result from the buffer lengths supplied by the aforementioned |
| 425 |
API calls,</para> |
| 426 |
</listitem> |
| 427 |
<listitem> |
| 428 |
<para>call to kcapi_aead_getdata_input and kcapi_aead_getdata_output |
| 429 |
with NULL pointers for the memory buffers to obtain the lengths for the |
| 430 |
AEAD data components,</para> |
| 431 |
</listitem> |
| 432 |
<listitem> |
| 433 |
<para>initialize the IOVECs and/or invoke the stream API with the |
| 434 |
independent buffers with the AAD, plaintext/ciphertext and tag if the |
| 435 |
associated length values are non-zero.</para> |
| 436 |
</listitem> |
| 437 |
</orderedlist> |
| 438 |
</para> |
| 439 |
</listitem> |
| 440 |
|
| 441 |
</orderedlist> |
| 442 |
|
| 443 |
<para> |
| 444 |
If the caller chooses to not implement an in-place operation, the kernel |
| 445 |
will copy the AAD data into the output buffer, so that the destination |
| 446 |
buffer will hold the the ciphertext or plaintext, the AAD data and the |
| 447 |
authentication tag (encryption only). The memory structure of the |
| 448 |
destination buffer is identical to the source buffer. (This is currently |
| 449 |
not yet implemented for all ciphers and will be fixed in future |
| 450 |
kernel versions.) |
| 451 |
</para> |
| 452 |
</sect2> |
| 453 |
</sect1> |
| 454 |
|
| 455 |
<sect1><title>Message Digest API</title> |
| 456 |
<para> |
| 457 |
Again, like for the symmetric ciphers, the message digest API implements |
| 458 |
the one-shot and the stream use cases. In addition, convenience wrapper |
| 459 |
functions for SHA-1 through SHA-512 are provided where the caller only |
| 460 |
provides its input data and the return buffer for obtaining a message |
| 461 |
digest or keyed message digest. |
| 462 |
</para> |
| 463 |
</sect1> |
| 464 |
|
| 465 |
<sect1><title>Asymmetric Cipher API</title> |
| 466 |
<para> |
| 467 |
The asymmetric cipher API provides access to the raw asymmetric operations |
| 468 |
(i.e. modular exponentiation). |
| 469 |
</para> |
| 470 |
</sect1> |
| 471 |
|
| 472 |
<sect1><title>Zero Copy</title> |
| 473 |
<para> |
| 474 |
When using the one-shot API for symmetric ciphers, AEAD ciphers, |
| 475 |
as well as message digests, the library uses the zero copy interface to |
| 476 |
provide the input data to the kernel. That means, the kernel operates on |
| 477 |
the user space pages. |
| 478 |
</para> |
| 479 |
|
| 480 |
<para> |
| 481 |
To ensure the efficiency of this zero copy approach, the caller should |
| 482 |
use a page-aligned data buffer for the input data. Non-aligned buffers |
| 483 |
would work also, but the kernel would need to perform more page |
| 484 |
accesses, lowering the throughput. Such an aligned buffer can be created, |
| 485 |
for example, using the following call - the value 4096 should be the size |
| 486 |
of one page on the system: |
| 487 |
</para> |
| 488 |
|
| 489 |
<programlisting> |
| 490 |
unsigned char buf[4096] __attribute__((__aligned__(4096))); |
| 491 |
</programlisting> |
| 492 |
|
| 493 |
<programlisting> |
| 494 |
unsigned char *buf; |
| 495 |
posix_memalign((void *)&buf, PAGE_SIZE, buflen); |
| 496 |
</programlisting> |
| 497 |
</sect1> |
| 498 |
|
| 499 |
<sect1><title>Memory Allocation</title> |
| 500 |
<para> |
| 501 |
The library libkcapi uses the data structure struct kcapi_handle as the |
| 502 |
cipher handle that allows the consumer to operate with the various function |
| 503 |
calls of this library. |
| 504 |
</para> |
| 505 |
|
| 506 |
<para> |
| 507 |
Unlike other crypto libraries, libkcapi does not allocate any memory or |
| 508 |
performs operations that implies memory allocation. struct kcapi_handle only |
| 509 |
holds pointers to the consumer-provided buffers with sensitive data. That |
| 510 |
means that the buffers holding sensitive data like keys are under full |
| 511 |
control of the consumer. Therefore, this library does not offer any memory |
| 512 |
allocation or secure memory clearing functions. |
| 513 |
</para> |
| 514 |
|
| 515 |
<para> |
| 516 |
The consumer must ensure that the memory is appropriately sanitized. The |
| 517 |
caller does not need to sanitize struct kcapi_handle as it does not contain |
| 518 |
any sensitive data. |
| 519 |
</para> |
| 520 |
</sect1> |
| 521 |
|
| 522 |
<sect1><title>Asynchronous I/O Use Cases and Libkcapi</title> |
| 523 |
<para> |
| 524 |
The kernel crypto API user space interface supports different use cases |
| 525 |
with the asynchronous I/O operations which are illustrated in the following |
| 526 |
sections. These sections also illustrate the API calls to be used to follow |
| 527 |
the respective use cases. |
| 528 |
</para> |
| 529 |
|
| 530 |
<para> |
| 531 |
All APIs that perform synchronous operation do not have different purposes |
| 532 |
and thus do not require special precautions when using them. |
| 533 |
</para> |
| 534 |
|
| 535 |
<para> |
| 536 |
The different use cases round asynchronous I/O revolve around different |
| 537 |
ways how to send data to the kernel and to retrieve processed data. |
| 538 |
</para> |
| 539 |
|
| 540 |
<sect2><title>Multiple Staged Cipher Operations</title> |
| 541 |
<para> |
| 542 |
Using the kcapi_cipher_*_aio and kcapi_aead_*_aio API calls, a caller can |
| 543 |
supply one or more IOVECs of data to the kernel. However, the caller can |
| 544 |
only supply one IV to the kernel. |
| 545 |
</para> |
| 546 |
|
| 547 |
<para> |
| 548 |
The API calls only allow specifying one integer defining the number of |
| 549 |
IOVECs in the arrays of the input data as well as the output data. |
| 550 |
The libkcapi library uses the input and output IOVECs as pairs. I.e. |
| 551 |
the first IOVEC of the input array relate to the first IOVEC of the output |
| 552 |
array, and so on. |
| 553 |
</para> |
| 554 |
|
| 555 |
<para> |
| 556 |
The kernel invokes the cipher operation when a recvmsg system call is |
| 557 |
processed. The AIO handling transforms each output IOVEC into one separate |
| 558 |
invocation of the recvmsg handler that processes the data submitted with |
| 559 |
the corresponding input IOVEC. This means, each output IOVEC will |
| 560 |
trigger one cipher operation. When multiple IOVECs are processed by the |
| 561 |
kernel's AIO handling, all resulting recvmsg calls are invoked with the |
| 562 |
in the data same order specified by the list of IOVECs. |
| 563 |
</para> |
| 564 |
|
| 565 |
<para> |
| 566 |
WARNING: Currently, it is not guaranteed that the drivers perform the proper |
| 567 |
serialization of the parallel processing of the different IOVECs. For |
| 568 |
example, when providing two IOVECs, they may be both using the initially |
| 569 |
set IV. Thus, they are not chained. User space is able to serialize |
| 570 |
the AIO operation in this case by invoking the AIO API calls with |
| 571 |
input/output IOVEC arrays holding one entry each only. However, this |
| 572 |
would imply that this type of invocation will not be different from a |
| 573 |
synchronous invocation. |
| 574 |
</para> |
| 575 |
</sect2> |
| 576 |
|
| 577 |
<sect2><title>Multiple Separate Cipher Operations</title> |
| 578 |
<para> |
| 579 |
The kernel and thus libkcapi supports the use case where several of the |
| 580 |
aforementioned multiple staged cipher operations are can be performed |
| 581 |
in parallel which are totally isolated from each other. In this case, |
| 582 |
different IVs are used. |
| 583 |
</para> |
| 584 |
|
| 585 |
<para> |
| 586 |
Using the kcapi_handle_reinit libkcapi API call, the caller can obtain |
| 587 |
a new cipher handle from an existing handle. Both share the same key |
| 588 |
and cipher -- the kernel crypto API maintains the same TFM data structure |
| 589 |
for both. However, both cipher handles can now encrypt or decrypt data |
| 590 |
completely isolated from each other. Specifically, the following data |
| 591 |
of a cipher operation is isolated between the different cipher handles -- |
| 592 |
this is all data that is not set with a setsockopt(2) system call: |
| 593 |
|
| 594 |
<itemizedlist> |
| 595 |
<listitem> |
| 596 |
<para>Input: plaintext (encryption) or ciphertext (decryption)</para> |
| 597 |
</listitem> |
| 598 |
<listitem> |
| 599 |
<para>Output: plaintext (decryption) or ciphertext (encryption)</para> |
| 600 |
</listitem> |
| 601 |
<listitem> |
| 602 |
<para>IV</para> |
| 603 |
</listitem> |
| 604 |
<listitem> |
| 605 |
<para>AEAD: associated authenticated data (AAD) and its length</para> |
| 606 |
</listitem> |
| 607 |
<listitem> |
| 608 |
<para>AEAD: tag</para> |
| 609 |
</listitem> |
| 610 |
</itemizedlist> |
| 611 |
|
| 612 |
The following data is shared between the different cipher handles -- this |
| 613 |
covers all data that can be set with a setsockopt(2) system call: |
| 614 |
|
| 615 |
<itemizedlist> |
| 616 |
<listitem> |
| 617 |
<para>Key</para> |
| 618 |
</listitem> |
| 619 |
<listitem> |
| 620 |
<para>AEAD: Tag length</para> |
| 621 |
</listitem> |
| 622 |
</itemizedlist> |
| 623 |
|
| 624 |
This means that the "multiple staged cipher operations" discussed above |
| 625 |
can be performed with each cipher handle independently. |
| 626 |
</para> |
| 627 |
|
| 628 |
<para> |
| 629 |
The call kcapi_handle_reinit does not open another socket, but implies |
| 630 |
that only a new accept(2) system call is performed. |
| 631 |
</para> |
| 632 |
</sect2> |
| 633 |
</sect1> |
| 634 |
|
| 635 |
<sect1><title>Kernel Interfaces</title> |
| 636 |
<para> |
| 637 |
Depending on the version of your kernel, some of the kernel interfaces |
| 638 |
the library depends on are not available. When using the respective library |
| 639 |
API functions, an error is returned during initialization of the cipher |
| 640 |
handle. The following interfaces are available: |
| 641 |
</para> |
| 642 |
|
| 643 |
<itemizedlist> |
| 644 |
<listitem> |
| 645 |
<para>kcapi_md_* usable since kernel version 3.0</para></listitem> |
| 646 |
<listitem> |
| 647 |
<para>kcapi_cipher_* usable since kernel version 3.0</para> |
| 648 |
</listitem> |
| 649 |
<listitem> |
| 650 |
<para> |
| 651 |
kcapi_rng_* kernel interface integrated into kernel version 4.0 |
| 652 |
</para> |
| 653 |
</listitem> |
| 654 |
<listitem> |
| 655 |
<para> |
| 656 |
kcapi_aead_* kernel interface added to cryptodev-2.6 tree and should be |
| 657 |
usable with kernel version 4.2. |
| 658 |
</para> |
| 659 |
</listitem> |
| 660 |
<listitem> |
| 661 |
<para> |
| 662 |
kcapi_akcipher_* kernel interface is discussed for inclusion to the |
| 663 |
cryptodev-2.6 kernel tree. |
| 664 |
</para> |
| 665 |
</listitem> |
| 666 |
</itemizedlist> |
| 667 |
|
| 668 |
<sect2><title>Kernel Configuration</title> |
| 669 |
<para> |
| 670 |
To use libkcapi, the following kernel options need to be enabled: |
| 671 |
</para> |
| 672 |
|
| 673 |
<itemizedlist> |
| 674 |
<listitem> |
| 675 |
<para> |
| 676 |
CONFIG_CRYPTO_USER enables the NETLINK_CRYPTO interface to allow |
| 677 |
obtaining information about the loaded ciphers. When compiled as module |
| 678 |
in older kernels (pre 3.18) the resulting crypto_user kernel module must |
| 679 |
be loaded manually. |
| 680 |
</para> |
| 681 |
</listitem> |
| 682 |
<listitem> |
| 683 |
<para> |
| 684 |
CONFIG_CRYPTO_USER_API enables the core functionality of the user space |
| 685 |
interface handler. |
| 686 |
</para> |
| 687 |
</listitem> |
| 688 |
<listitem> |
| 689 |
<para> |
| 690 |
CONFIG_CRYPTO_USER_API_HASH enables the "hash" interface (i.e. allows |
| 691 |
the use of all message digest and keyed message digest ciphers). |
| 692 |
</para> |
| 693 |
</listitem> |
| 694 |
<listitem> |
| 695 |
<para> |
| 696 |
CONFIG_CRYPTO_USER_API_SKCIPHER enables the "skcipher" interface |
| 697 |
to use symmetric cipher algorithms. |
| 698 |
</para> |
| 699 |
</listitem> |
| 700 |
<listitem> |
| 701 |
<para> |
| 702 |
CONFIG_CRYPTO_USER_API_AEAD enables the "aead" interface to use AEAD |
| 703 |
cipher algorithms. This support is currently discussed on LKML and |
| 704 |
therefore not present in the mainline kernel. |
| 705 |
</para> |
| 706 |
</listitem> |
| 707 |
<listitem> |
| 708 |
<para> |
| 709 |
CONFIG_CRYPTO_USER_API_RNG enables the "rng" interface to use the |
| 710 |
random number generators. |
| 711 |
</para> |
| 712 |
</listitem> |
| 713 |
|
| 714 |
<listitem> |
| 715 |
<para> |
| 716 |
CONFIG_CRYPTO_USER_API_AKCIPHER enables the "akcipher" interface to use |
| 717 |
the asymmetric ciphers. This support is currently discussed on LKML |
| 718 |
and therefore not present in the mainline kernel. |
| 719 |
</para> |
| 720 |
</listitem> |
| 721 |
</itemizedlist> |
| 722 |
|
| 723 |
<para> |
| 724 |
In addition, the following patch must be applied if a kernel less than |
| 725 |
3.19-rc1 or the cryptodev-2.6 kernel tree is used: |
| 726 |
https://git.kernel.org/cgit/linux/kernel/git/herbert/cryptodev-2.6.git/commit/?id=5d4a5e770d97d87082067886e7097c920b338da5 |
| 727 |
</para> |
| 728 |
|
| 729 |
<para> |
| 730 |
In addition, the following patch must be applied if a kernel less than |
| 731 |
3.19-rc1 or the cryptodev-2.6 kernel tree is used: |
| 732 |
https://git.kernel.org/cgit/linux/kernel/git/herbert/cryptodev-2.6.git/commit/?id=af8e80731a94ff9de9508b01d9e5d931d538dc6b |
| 733 |
</para> |
| 734 |
|
| 735 |
<para> |
| 736 |
In addition, the following patch must be applied if a kernel less than |
| 737 |
3.19-rc1 or the cryptodev-2.6 kernel tree is used: |
| 738 |
https://git.kernel.org/cgit/linux/kernel/git/herbert/cryptodev-2.6.git/commit/?id=25fb8638e919bc7431a73f2fb4a9713818ae2c9d |
| 739 |
</para> |
| 740 |
</sect2> |
| 741 |
</sect1> |
| 742 |
|
| 743 |
<sect1><title>Example Code</title> |
| 744 |
<para> |
| 745 |
Example code covering all available API calls is provided with the test |
| 746 |
code in the test/ directory. |
| 747 |
</para> |
| 748 |
</sect1> |
| 749 |
</chapter> |
| 750 |
|
| 751 |
<chapter id="API"><title>Programming Interface</title> |
| 752 |
<sect1><title>Common API</title> |
| 753 |
!Pkcapi.h Common API |
| 754 |
!Fkcapi.h kcapi_set_verbosity |
| 755 |
!Fkcapi.h kcapi_versionstring |
| 756 |
!Fkcapi.h kcapi_version |
| 757 |
!Fkcapi.h kcapi_pad_iv |
| 758 |
!Fkcapi.h kcapi_memset_secure |
| 759 |
!Fkcapi.h kcapi_handle_reinit |
| 760 |
!Fkcapi.h kcapi_set_maxsplicesize |
| 761 |
!Fkcapi.h kcapi_get_maxsplicesize |
| 762 |
</sect1> |
| 763 |
<sect1><title>Symmetric Cipher API - Generic</title> |
| 764 |
<para> |
| 765 |
These generic API for symmetric ciphers calls are to be used for both, the |
| 766 |
one-shot and the stream encryption/decryption operations. |
| 767 |
</para> |
| 768 |
!Pkcapi.h Symmetric Cipher API |
| 769 |
!Fkcapi.h kcapi_cipher_init |
| 770 |
!Fkcapi.h kcapi_cipher_destroy |
| 771 |
!Fkcapi.h kcapi_cipher_setkey |
| 772 |
!Fkcapi.h kcapi_cipher_ivsize |
| 773 |
!Fkcapi.h kcapi_cipher_blocksize |
| 774 |
</sect1> |
| 775 |
<sect1><title>Synchronous Symmetric Cipher API - One Shot</title> |
| 776 |
!Fkcapi.h kcapi_cipher_encrypt |
| 777 |
!Fkcapi.h kcapi_cipher_decrypt |
| 778 |
</sect1> |
| 779 |
<sect1><title>Symmetric Cipher API - Convenience</title> |
| 780 |
!Fkcapi.h kcapi_cipher_enc_aes_cbc |
| 781 |
!Fkcapi.h kcapi_cipher_dec_aes_cbc |
| 782 |
!Fkcapi.h kcapi_cipher_enc_aes_ctr |
| 783 |
!Fkcapi.h kcapi_cipher_dec_aes_ctr |
| 784 |
!Fkcapi.h kcapi_cipher_enc_sm4_cbc |
| 785 |
!Fkcapi.h kcapi_cipher_dec_sm4_cbc |
| 786 |
!Fkcapi.h kcapi_cipher_enc_sm4_ctr |
| 787 |
!Fkcapi.h kcapi_cipher_dec_sm4_ctr |
| 788 |
</sect1> |
| 789 |
<sect1><title>Asynchronous Symmetric Cipher API - One Shot</title> |
| 790 |
!Fkcapi.h kcapi_cipher_encrypt_aio |
| 791 |
!Fkcapi.h kcapi_cipher_decrypt_aio |
| 792 |
</sect1> |
| 793 |
<sect1><title>Synchronous Symmetric Cipher API - Stream</title> |
| 794 |
<para>The stream API requires that first the cipher operation type is |
| 795 |
set with the init call, followed by an arbitrary number and mix of the |
| 796 |
update and op calls.</para> |
| 797 |
!Fkcapi.h kcapi_cipher_stream_init_enc |
| 798 |
!Fkcapi.h kcapi_cipher_stream_init_dec |
| 799 |
!Fkcapi.h kcapi_cipher_stream_update |
| 800 |
!Fkcapi.h kcapi_cipher_stream_update_last |
| 801 |
!Fkcapi.h kcapi_cipher_stream_op |
| 802 |
</sect1> |
| 803 |
<sect1><title>AEAD Cipher API - Generic</title> |
| 804 |
<para> |
| 805 |
These generic API for symmetric ciphers calls are to be used for both, the |
| 806 |
one-shot and the stream encryption/decryption operations. |
| 807 |
</para> |
| 808 |
!Pkcapi.h AEAD Cipher API |
| 809 |
!Fkcapi.h kcapi_aead_init |
| 810 |
!Fkcapi.h kcapi_aead_destroy |
| 811 |
!Fkcapi.h kcapi_aead_setkey |
| 812 |
!Fkcapi.h kcapi_aead_setassoclen |
| 813 |
!Fkcapi.h kcapi_aead_settaglen |
| 814 |
!Fkcapi.h kcapi_aead_ivsize |
| 815 |
!Fkcapi.h kcapi_aead_blocksize |
| 816 |
!Fkcapi.h kcapi_aead_authsize |
| 817 |
!Fkcapi.h kcapi_aead_inbuflen_enc |
| 818 |
!Fkcapi.h kcapi_aead_inbuflen_dec |
| 819 |
!Fkcapi.h kcapi_aead_outbuflen_enc |
| 820 |
!Fkcapi.h kcapi_aead_outbuflen_dec |
| 821 |
!Fkcapi.h kcapi_aead_ccm_nonce_to_iv |
| 822 |
!Fkcapi.h kcapi_aead_getdata_input |
| 823 |
!Fkcapi.h kcapi_aead_getdata_output |
| 824 |
</sect1> |
| 825 |
<sect1><title>Synchronous AEAD Cipher API - One Shot</title> |
| 826 |
!Fkcapi.h kcapi_aead_encrypt |
| 827 |
!Fkcapi.h kcapi_aead_decrypt |
| 828 |
</sect1> |
| 829 |
<sect1><title>Aynchronous AEAD Cipher API - One Shot</title> |
| 830 |
!Fkcapi.h kcapi_aead_encrypt_aio |
| 831 |
!Fkcapi.h kcapi_aead_decrypt_aio |
| 832 |
</sect1> |
| 833 |
<sect1><title>Synchronous AEAD Cipher API - Stream</title> |
| 834 |
!Fkcapi.h kcapi_aead_stream_init_enc |
| 835 |
!Fkcapi.h kcapi_aead_stream_init_dec |
| 836 |
!Fkcapi.h kcapi_aead_stream_update |
| 837 |
!Fkcapi.h kcapi_aead_stream_update_last |
| 838 |
!Fkcapi.h kcapi_aead_stream_op |
| 839 |
</sect1> |
| 840 |
<sect1><title>Message Digest Cipher API - Generic</title> |
| 841 |
!Pkcapi.h Message Digest Cipher API |
| 842 |
!Fkcapi.h kcapi_md_init |
| 843 |
!Fkcapi.h kcapi_md_destroy |
| 844 |
!Fkcapi.h kcapi_md_setkey |
| 845 |
!Fkcapi.h kcapi_md_digestsize |
| 846 |
</sect1> |
| 847 |
<sect1><title>Message Digest Cipher API - One Shot</title> |
| 848 |
!Fkcapi.h kcapi_md_digest |
| 849 |
</sect1> |
| 850 |
<sect1><title>Message Digest Cipher API - Convenience</title> |
| 851 |
!Fkcapi.h kcapi_md_sha1 |
| 852 |
!Fkcapi.h kcapi_md_sha224 |
| 853 |
!Fkcapi.h kcapi_md_sha256 |
| 854 |
!Fkcapi.h kcapi_md_sha384 |
| 855 |
!Fkcapi.h kcapi_md_sha512 |
| 856 |
!Fkcapi.h kcapi_md_sm3 |
| 857 |
!Fkcapi.h kcapi_md_hmac_sha1 |
| 858 |
!Fkcapi.h kcapi_md_hmac_sha224 |
| 859 |
!Fkcapi.h kcapi_md_hmac_sha256 |
| 860 |
!Fkcapi.h kcapi_md_hmac_sha384 |
| 861 |
!Fkcapi.h kcapi_md_hmac_sha512 |
| 862 |
!Fkcapi.h kcapi_md_hmac_sm3 |
| 863 |
</sect1> |
| 864 |
<sect1><title>Message Digest Cipher API - Stream</title> |
| 865 |
!Fkcapi.h kcapi_md_update |
| 866 |
!Fkcapi.h kcapi_md_final |
| 867 |
</sect1> |
| 868 |
<sect1><title>Random Number API</title> |
| 869 |
!Pkcapi.h Random Number API |
| 870 |
!Fkcapi.h kcapi_rng_init |
| 871 |
!Fkcapi.h kcapi_rng_destroy |
| 872 |
!Fkcapi.h kcapi_rng_seed |
| 873 |
!Fkcapi.h kcapi_rng_generate |
| 874 |
!Fkcapi.h kcapi_rng_seedsize |
| 875 |
</sect1> |
| 876 |
<sect1><title>Random Number API - Convenience</title> |
| 877 |
!Fkcapi.h kcapi_rng_get_bytes |
| 878 |
</sect1> |
| 879 |
<sect1><title>Asymmetric Cipher API - Generic</title> |
| 880 |
!Pkcapi.h Asymmetric Cipher API |
| 881 |
!Fkcapi.h kcapi_akcipher_init |
| 882 |
!Fkcapi.h kcapi_akcipher_destroy |
| 883 |
!Fkcapi.h kcapi_akcipher_setkey |
| 884 |
!Fkcapi.h kcapi_akcipher_setpubkey |
| 885 |
</sect1> |
| 886 |
<sect1><title>Synchronous asymmetric Cipher API - One Shot</title> |
| 887 |
!Fkcapi.h kcapi_akcipher_encrypt |
| 888 |
!Fkcapi.h kcapi_akcipher_decrypt |
| 889 |
!Fkcapi.h kcapi_akcipher_sign |
| 890 |
!Fkcapi.h kcapi_akcipher_verify |
| 891 |
</sect1> |
| 892 |
<sect1><title>Aynchronous asymmetric Cipher API - One Shot</title> |
| 893 |
!Fkcapi.h kcapi_akcipher_encrypt_aio |
| 894 |
!Fkcapi.h kcapi_akcipher_decrypt_aio |
| 895 |
!Fkcapi.h kcapi_akcipher_sign_aio |
| 896 |
!Fkcapi.h kcapi_akcipher_verify_aio |
| 897 |
</sect1> |
| 898 |
<sect1><title>Asymmetric Cipher API - Stream</title> |
| 899 |
!Fkcapi.h kcapi_akcipher_stream_init_enc |
| 900 |
!Fkcapi.h kcapi_akcipher_stream_init_dec |
| 901 |
!Fkcapi.h kcapi_akcipher_stream_init_sgn |
| 902 |
!Fkcapi.h kcapi_akcipher_stream_init_vfy |
| 903 |
!Fkcapi.h kcapi_akcipher_stream_update |
| 904 |
!Fkcapi.h kcapi_akcipher_stream_op |
| 905 |
</sect1> |
| 906 |
<sect1><title>Key Protocol Primitives API - Generic</title> |
| 907 |
!Fkcapi.h kcapi_kpp_init |
| 908 |
!Fkcapi.h kcapi_kpp_destroy |
| 909 |
!Fkcapi.h kcapi_kpp_dh_setparam_pkcs3 |
| 910 |
!Fkcapi.h kcapi_kpp_ecdh_setcurve |
| 911 |
!Fkcapi.h kcapi_kpp_setkey |
| 912 |
</sect1> |
| 913 |
<sect1><title>Synchronous Key Protocol Primitives API - One Shot</title> |
| 914 |
!Fkcapi.h kcapi_kpp_keygen |
| 915 |
!Fkcapi.h kcapi_kpp_ssgen |
| 916 |
</sect1> |
| 917 |
<sect1><title>Asynchronous Key Protocol Primitives API - One Shot</title> |
| 918 |
!Fkcapi.h kcapi_kpp_keygen_aio |
| 919 |
!Fkcapi.h kcapi_kpp_ssgen_aio |
| 920 |
</sect1> |
| 921 |
<sect1><title>Key Derivation Functions</title> |
| 922 |
!Pkcapi.h Key Derivation Functions |
| 923 |
!Fkcapi.h kcapi_kdf_dpi |
| 924 |
!Fkcapi.h kcapi_kdf_fb |
| 925 |
!Fkcapi.h kcapi_kdf_ctr |
| 926 |
!Fkcapi.h kcapi_pbkdf |
| 927 |
!Fkcapi.h kcapi_pbkdf_iteration_count |
| 928 |
!Fkcapi.h kcapi_hkdf |
| 929 |
</sect1> |
| 930 |
</chapter> |
| 931 |
</book> |