/usr/include/kcapi.h is in libkcapi-dev 1.0.3-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 | /*
* Copyright (C) 2015 - 2018, Stephan Mueller <smueller@chronox.de>
*
* License: see COPYING file in root directory
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
* WHICH ARE HEREBY DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
* USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*/
#ifndef KCAPI_H
#define KCAPI_H
#include <stdint.h>
#include <sys/uio.h>
#ifdef __cplusplus
extern "C"
{
#endif
#define DSO_DEPRECATED(x) \
__attribute__ ((deprecated ("API deprecated with library version " x)))
/*
* Flags for the encrypt / decrypt operations
*
* @KCAPI_ACCESS_HEURISTIC Allow the libkcapi heuristic to determine the
* optimal kernel access type
* @KCAPI_ACCESS_VMSPLICE Require libkcapi to always use the vmsplice zero
* copy kernel interface
* @KCAPI_ACCESS_SENDMSG Require libkcapi to always use the sendmsg kernel
* interface
*/
#define KCAPI_ACCESS_HEURISTIC 0x0
#define KCAPI_ACCESS_VMSPLICE 0x1
#define KCAPI_ACCESS_SENDMSG 0x2
/*
* Flags for initializing a cipher handle
*
* @KCAPI_INIT_AIO Handle uses AIO kernel interface if available
*/
#define KCAPI_INIT_AIO (1<<0)
/*
* Opaque cipher handle
*/
struct kcapi_handle;
/**
* DOC: Symmetric Cipher API
*
* API function calls used to invoke symmetric ciphers.
*/
/**
* kcapi_cipher_init() - initialize cipher handle
*
* @handle: [out] cipher handle filled during the call
* @ciphername: [in] kernel crypto API cipher name as specified in
* /proc/crypto
* @flags: [in] flags specifying the type of cipher handle
*
* This function provides the initialization of a symmetric cipher handle and
* establishes the connection to the kernel.
*
* On success, a pointer to kcapi_handle object is returned in *handle.
* Function kcapi_cipher_destroy should be called afterwards to free resources.
*
* @return 0 upon success;
* -ENOENT - algorithm not available;
* -EOPNOTSUPP - AF_ALG family not available;
* -EINVAL - accept syscall failed
* -ENOMEM - cipher handle cannot be allocated
*/
int kcapi_cipher_init(struct kcapi_handle **handle, const char *ciphername,
uint32_t flags);
/**
* kcapi_cipher_destroy() - close the cipher handle and release resources
*
* @handle: [in] cipher handle to release
*/
void kcapi_cipher_destroy(struct kcapi_handle *handle);
/**
* kcapi_cipher_setkey() - set the key for the cipher handle
*
* @handle: [in] cipher handle
* @key: [in] key buffer
* @keylen: [in] length of key buffer
*
* With this function, the caller sets the key for subsequent encryption or
* decryption operations.
*
* After the caller provided the key, the caller may securely destroy the key
* as it is now maintained by the kernel.
*
* @return 0 upon success (in case of an akcipher handle, a positive integer
* is returned that denominates the maximum output size of the
* cryptographic operation -- this value must be used as the size
* of the output buffer for one cryptographic operation);
* a negative errno-style error code if an error occurred
*/
int kcapi_cipher_setkey(struct kcapi_handle *handle,
const uint8_t *key, uint32_t keylen);
/**
* kcapi_cipher_encrypt() - encrypt data (synchronous one shot)
*
* @handle: [in] cipher handle
* @in: [in] plaintext data buffer
* @inlen: [in] length of in buffer
* @iv: [in] IV to be used for cipher operation
* @out: [out] ciphertext data buffer
* @outlen: [in] length of out buffer
* @access: [in] kernel access type (KCAPI_ACCESS_HEURISTIC - use internal
* heuristic for fastest kernel access; KCAPI_ACCESS_VMSPLICE - use
* vmsplice access; KCAPI_ACCESS_SENDMSG - sendmsg access)
*
* It is perfectly legal to use the same buffer as the plaintext and
* ciphertext pointers. That would mean that after the encryption operation,
* the plaintext is overwritten with the ciphertext.
*
* The memory should be aligned at the page boundary using
* posix_memalign(sysconf(_SC_PAGESIZE)), If it is not aligned at the page
* boundary, the vmsplice call may not send all data to the kernel.
*
* The IV buffer must be exactly kcapi_cipher_ivsize() bytes in size.
*
* @return number of bytes encrypted upon success;
* a negative errno-style error code if an error occurred
*/
int32_t kcapi_cipher_encrypt(struct kcapi_handle *handle,
const uint8_t *in, uint32_t inlen,
const uint8_t *iv,
uint8_t *out, uint32_t outlen, int access);
/**
* kcapi_cipher_encrypt_aio() - encrypt data (asynchronous one shot)
*
* @handle: [in] cipher handle
* @iniov: [in] head of scatter-gather list array holding the plaintext
* @outiov: [out] head of scatter-gather list of the destination buffers filled
* with ciphertext
* @iovlen: [in] number of scatter-gather list entries
* @iv: [in] IV to be used for cipher operation
* @access: [in] kernel access type (KCAPI_ACCESS_HEURISTIC - use internal
* heuristic for fastest kernel access; KCAPI_ACCESS_VMSPLICE - use
* vmsplice access; KCAPI_ACCESS_SENDMSG - sendmsg access)
*
* The individual scatter-gather list entries are processed with
* separate invocations of the the given cipher.
*
* The memory should be aligned at the page boundary using
* posix_memalign(sysconf(_SC_PAGESIZE)), If it is not aligned at the page
* boundary, the vmsplice call may not send all data to the kernel.
*
* The IV buffer must be exactly kcapi_cipher_ivsize() bytes in size.
*
* @return number of bytes encrypted upon success;
* a negative errno-style error code if an error occurred
*/
int32_t kcapi_cipher_encrypt_aio(struct kcapi_handle *handle,
struct iovec *iniov, struct iovec *outiov,
uint32_t iovlen, const uint8_t *iv,
int access);
/**
* kcapi_cipher_decrypt() - decrypt data (synchronous one shot)
*
* @handle: [in] cipher handle
* @in: [in] ciphertext data buffer
* @inlen: [in] length of in buffer
* @iv: [in] IV to be used for cipher operation
* @out: [out] plaintext data buffer
* @outlen: [in] length of out bufferS
* @access: [in] kernel access type (KCAPI_ACCESS_HEURISTIC - use internal
* heuristic for fastest kernel access; KCAPI_ACCESS_VMSPLICE - use
* vmsplice access; KCAPI_ACCESS_SENDMSG - sendmsg access)
*
* It is perfectly legal to use the same buffer as the plaintext and
* ciphertext pointers. That would mean that after the encryption operation,
* the ciphertext is overwritten with the plaintext.
*
* The memory should be aligned at the page boundary using
* posix_memalign(sysconf(_SC_PAGESIZE)), If it is not aligned at the page
* boundary, the vmsplice call may not send all data to the kernel.
*
* The IV buffer must be exactly kcapi_cipher_ivsize() bytes in size.
*
* @return number of bytes decrypted upon success;
* a negative errno-style error code if an error occurred
*/
int32_t kcapi_cipher_decrypt(struct kcapi_handle *handle,
const uint8_t *in, uint32_t inlen,
const uint8_t *iv,
uint8_t *out, uint32_t outlen, int access);
/**
* kcapi_cipher_decrypt_aio() - decrypt data (asynchronous one shot)
*
* @handle: [in] cipher handle
* @iniov: [in] head of scatter-gather list array holding the ciphertext
* @outiov: [out] head of scatter-gather list with the destination buffers for
* the plaintext
* @iovlen: [in] number of scatter-gather list entries
* @iv: [in] IV to be used for cipher operation
* @access: [in] kernel access type (KCAPI_ACCESS_HEURISTIC - use internal
* heuristic for fastest kernel access; KCAPI_ACCESS_VMSPLICE - use
* vmsplice access; KCAPI_ACCESS_SENDMSG - sendmsg access)
*
* The individual scatter-gather list entries are processed with
* separate invocations of the the given cipher.
*
* The memory should be aligned at the page boundary using
* posix_memalign(sysconf(_SC_PAGESIZE)), If it is not aligned at the page
* boundary, the vmsplice call may not send all data to the kernel.
*
* The IV buffer must be exactly kcapi_cipher_ivsize() bytes in size.
*
* @return number of bytes decrypted upon success;
* a negative errno-style error code if an error occurred
*/
int32_t kcapi_cipher_decrypt_aio(struct kcapi_handle *handle,
struct iovec *iniov, struct iovec *outiov,
uint32_t iovlen, const uint8_t *iv,
int access);
/**
* kcapi_cipher_stream_init_enc() - start an encryption operation (stream)
*
* @handle: [in] cipher handle
* @iv: [in] IV to be used for cipher operation
* @iov: [in] scatter/gather list with data to be encrypted. This is
* the pointer to the first iov entry if an array of iov
* entries is supplied. See sendmsg(2) for details on how iov is
* to be used. This pointer may be NULL if no data to be encrypted
* is available at the point of the call.
* @iovlen: [in] number of scatter/gather list elements. If iov
* is NULL, this value must be zero.
*
* A stream encryption operation is started with this call. Multiple
* successive kcapi_cipher_stream_update() function calls can be invoked to
* send more plaintext data to be encrypted. The kernel buffers the input
* until kcapi_cipher_stream_op() picks up the encrypted data. Once plaintext
* is encrypted during the kcapi_cipher_stream_op() it is removed from the
* kernel buffer.
*
* The function calls of kcapi_cipher_stream_update() and
* kcapi_cipher_stream_op() can be mixed, even by multiple threads of an
* application.
*
* The IV buffer must be exactly kcapi_cipher_ivsize() bytes in size.
*
* @return number of bytes sent to the kernel upon success;
* a negative errno-style error code if an error occurred
*/
int32_t kcapi_cipher_stream_init_enc(struct kcapi_handle *handle,
const uint8_t *iv,
struct iovec *iov, uint32_t iovlen);
/**
* kcapi_cipher_stream_init_dec() - start a decryption operation (stream)
*
* @handle: [in] cipher handle
* @iv: [in] IV to be used for cipher operation
* @iov: [in] scatter/gather list with data to be encrypted. This is
* the pointer to the first iov entry if an array of iov
* entries is supplied. See sendmsg(2) for details on how iov is
* to be used. This pointer may be NULL if no data to be encrypted
* is available at the point of the call.
* @iovlen: [in] number of scatter/gather list elements. If iov
* is NULL, this value must be zero.
*
* A stream decryption operation is started with this call. Multiple
* successive kcapi_cipher_stream_update() function calls can be invoked to
* send more ciphertext data to be decrypted. The kernel buffers the input
* until kcapi_cipher_stream_op() picks up the decrypted data. Once ciphertext
* is decrypted during the kcapi_cipher_stream_op() it is removed from the
* kernel buffer.
*
* The function calls of kcapi_cipher_stream_update() and
* kcapi_cipher_stream_op() can be mixed, even by multiple threads of an
* application.
*
* The IV buffer must be exactly kcapi_cipher_ivsize() bytes in size.
*
* @return number of bytes sent to the kernel upon success;
* a negative errno-style error code if an error occurred
*/
int32_t kcapi_cipher_stream_init_dec(struct kcapi_handle *handle,
const uint8_t *iv,
struct iovec *iov, uint32_t iovlen);
/**
* kcapi_cipher_stream_update() - send more data for processing (stream)
*
* @handle: [in] cipher handle
* @iov: [in] scatter/gather list with data to be processed by the
* cipher operation.
* @iovlen: [in] number of scatter/gather list elements.
*
* Using this function call, more plaintext for encryption or ciphertext for
* decryption can be submitted to the kernel.
*
* This function may cause the caller to sleep if the kernel buffer holding
* the data is getting full. The process will be woken up once more buffer
* space becomes available by calling kcapi_cipher_stream_op().
*
* Note: with the separate API calls of kcapi_cipher_stream_update() and
* kcapi_cipher_stream_op() a multi-threaded application can be implemented
* where one thread sends data to be processed and one thread picks up data
* processed by the cipher operation.
*
* IMPORTANT NOTE: The kernel will only process
* sysconf(_SC_PAGESIZE) * ALG_MAX_PAGES at one time. If your input data is
* larger than this threshold, you MUST segment it into chunks of at most
* sysconf(_SC_PAGESIZE) * ALG_MAX_PAGES and invoke the
* kcapi_cipher_stream_update() on that segment followed by
* kcapi_cipher_stream_op() before the next chunk is processed. If this
* rule is not obeyed, the thread invoking kcapi_cipher_stream_update()
* will be put to sleep until another thread invokes kcapi_cipher_stream_op().
*
* WARNING: The memory referenced by @iov is not accessed by the kernel
* during this call. The memory is first accessed when kcapi_cipher_stream_op()
* is called. Thus, you MUST make sure that the referenced memory is still
* present at the time kcapi_cipher_stream_op() is called.
*
* @return number of bytes sent to the kernel upon success;
* a negative errno-style error code if an error occurred
*/
int32_t kcapi_cipher_stream_update(struct kcapi_handle *handle,
struct iovec *iov, uint32_t iovlen);
/**
* kcapi_cipher_stream_op() - obtain processed data (stream)
*
* @handle: [in] cipher handle
* @iov: [out] scatter/gather list pointing to buffers to be filled with
* the resulting data from a cipher operation.
* @iovlen: [in] number of scatter/gather list elements.
*
* This call can be called interleaved with kcapi_cipher_stream_update() to
* fetch the processed data.
*
* This function may cause the caller to sleep if the kernel buffer holding
* the data is empty. The process will be woken up once more data is sent
* by calling kcapi_cipher_stream_update().
*
* Note, when supplying buffers that are not multiple of block size, the buffers
* will only be filled up to the maximum number of full block sizes that fit
* into the buffer.
*
* The kernel supports multithreaded applications where one or more threads
* send data via the kcapi_cipher_stream_update() function and another thread
* collects the processed data via kcapi_cipher_stream_op. The kernel, however,
* will return data via kcapi_cipher_stream_op() as soon as it has some data
* available. For example, one thread sends 1000 bytes to be encrypted and
* another thread already waits for the ciphertext. The kernel may send only,
* say, 500 bytes back to the waiting process during one
* kcapi_cipher_stream_op() call. In a subsequent calls to
* kcapi_cipher_stream_op() more ciphertext is returned. This implies that when
* the receiving thread shall collect all data there is,
* kcapi_cipher_stream_op() must be called in a loop until all data is received.
*
* @return number of bytes obtained from the kernel upon success;
* a negative errno-style error code if an error occurred
*/
int32_t kcapi_cipher_stream_op(struct kcapi_handle *handle,
struct iovec *iov, uint32_t iovlen);
/**
* kcapi_cipher_enc_aes_cbc - Convenience function for AES CBC encryption
*
* @key: [in] key buffer
* @keylen: [in] length of key buffer
* @in: [in] plaintext data buffer
* @inlen: [in] length of in buffer
* @iv: [in] IV to be used for cipher operation
* @out: [out] ciphertext data buffer
* @outlen: [in] length of out buffer
*
* The convenience function performs an AES CBC encryption operation
* using the provided key, the given input buffer and the given IV.
* The output is stored in the out buffer.
*
* Note, AES CBC requires an input data that is a multiple of 16 bytes.
* If you have data that is not guaranteed to be multiples of 16 bytes, either
* add zero bytes at the end of the buffer to pad it up to a multiple of 16
* bytes. Otherwise, the CTR mode encryption operation may be usable.
*
* The output buffer must be at least as large as the input buffer.
*
* The IV must be exactly 16 bytes in size.
*
* The AES type (AES-128, AES-192 or AES-256) is determined by the size
* of the given key. If the key is 16 bytes long, AES-128 is used. A 24 byte
* key implies AES-192 and a 32 byte key implies AES-256.
*
* @return number of bytes generated upon success;
* a negative errno-style error code if an error occurred
*/
int32_t kcapi_cipher_enc_aes_cbc(const uint8_t *key, uint32_t keylen,
const uint8_t *in, uint32_t inlen,
const uint8_t *iv,
uint8_t *out, uint32_t outlen);
/**
* kcapi_cipher_dec_aes_cbc - Convenience function for AES CBC decryption
*
* @key: [in] key buffer
* @keylen: [in] length of key buffer
* @in: [in] ciphertext data buffer
* @inlen: [in] length of in buffer
* @iv: [in] IV to be used for cipher operation
* @out: [out] plaintext data buffer
* @outlen: [in] length of out buffer
*
* The convenience function performs an AES CBC decryption operation
* using the provided key, the given input buffer and the given IV.
* The output is stored in the out buffer.
*
* Note, AES CBC requires an input data that is a multiple of 16 bytes.
* If you have data that is not guaranteed to be multiples of 16 bytes, either
* add zero bytes at the end of the buffer to pad it up to a multiple of 16
* bytes. Otherwise, the CTR mode encryption operation may be usable.
*
* The output buffer must be at least as large as the input buffer.
*
* The IV must be exactly 16 bytes in size.
*
* The AES type (AES-128, AES-192 or AES-256) is determined by the size
* of the given key. If the key is 16 bytes long, AES-128 is used. A 24 byte
* key implies AES-192 and a 32 byte key implies AES-256.
*
* @return number of bytes generated upon success;
* a negative errno-style error code if an error occurred
*/
int32_t kcapi_cipher_dec_aes_cbc(const uint8_t *key, uint32_t keylen,
const uint8_t *in, uint32_t inlen,
const uint8_t *iv,
uint8_t *out, uint32_t outlen);
/**
* kcapi_cipher_enc_aes_ctr - Convenience function for AES CTR encryption
*
* @key: [in] key buffer
* @keylen: [in] length of key buffer
* @in: [in] plaintext data buffer
* @inlen: [in] length of in buffer
* @ctr: [in] start counter value to be used for cipher operation
* @out: [out] ciphertext data buffer
* @outlen: [in] length of out buffer
*
* The convenience function performs an AES counter mode encryption operation
* using the provided key, the given input buffer and the given IV.
* The output is stored in the out buffer.
*
* The input buffer can be of arbitrary length.
*
* The output buffer must be at least as large as the input buffer.
*
* The start counter can contain all zeros (not a NULL buffer!) and must be
* exactly 16 bytes in size.
*
* The AES type (AES-128, AES-192 or AES-256) is determined by the size
* of the given key. If the key is 16 bytes long, AES-128 is used. A 24 byte
* key implies AES-192 and a 32 byte key implies AES-256.
*
* @return number of bytes generated upon success;
* a negative errno-style error code if an error occurred
*/
int32_t kcapi_cipher_enc_aes_ctr(const uint8_t *key, uint32_t keylen,
const uint8_t *in, uint32_t inlen,
const uint8_t *ctr,
uint8_t *out, uint32_t outlen);
/**
* kcapi_cipher_dec_aes_ctr - Convenience function for AES CTR decryption
*
* @key: [in] key buffer
* @keylen: [in] length of key buffer
* @in: [in] ciphertext data buffer
* @inlen: [in] length of in buffer
* @ctr: [in] start counter value to be used for cipher operation
* @out: [out] plaintext data buffer
* @outlen: [in] length of out buffer
*
* The convenience function performs an AES counter mode encryption operation
* using the provided key, the given input buffer and the given IV.
* The output is stored in the out buffer.
*
* The input buffer can be of arbitrary length.
*
* The output buffer must be at least as large as the input buffer.
*
* The start counter can contain all zeros (not a NULL buffer!) and must be
* exactly 16 bytes in size.
*
* The AES type (AES-128, AES-192 or AES-256) is determined by the size
* of the given key. If the key is 16 bytes long, AES-128 is used. A 24 byte
* key implies AES-192 and a 32 byte key implies AES-256.
*
* @return number of bytes generated upon success;
* a negative errno-style error code if an error occurred
*/
int32_t kcapi_cipher_dec_aes_ctr(const uint8_t *key, uint32_t keylen,
const uint8_t *in, uint32_t inlen,
const uint8_t *ctr,
uint8_t *out, uint32_t outlen);
/**
* kcapi_cipher_ivsize() - return size of IV required for cipher
*
* @handle: [in] cipher handle
*
* @return > 0 specifying the IV size;
* 0 on error
*/
uint32_t kcapi_cipher_ivsize(struct kcapi_handle *handle);
/**
* kcapi_cipher_blocksize() - return size of one block of the cipher
*
* @handle: [in] cipher handle
*
* @return > 0 specifying the block size;
* 0 on error
*/
uint32_t kcapi_cipher_blocksize(struct kcapi_handle *handle);
/**
* DOC: AEAD Cipher API
*
* The following API calls allow using the Authenticated Encryption with
* Associated Data.
*/
/**
* kcapi_aead_init() - initialization of cipher handle
*
* @handle: [out] cipher handle filled during the call
* @ciphername: [in] kernel crypto API cipher name as specified in
* /proc/crypto
* @flags: [in] flags specifying the type of cipher handle
*
* This function initializes an AEAD cipher handle and establishes the
* connection to the kernel.
*
* On success, a pointer to kcapi_handle object is returned in *handle.
* Function kcapi_aead_destroy should be called afterwards to free resources.
*
* @return 0 upon success;
* -ENOENT - algorithm not available;
* -EOPNOTSUPP - AF_ALG family not available;
* -EINVAL - accept syscall failed
* -ENOMEM - cipher handle cannot be allocated
*/
int kcapi_aead_init(struct kcapi_handle **handle, const char *ciphername,
uint32_t flags);
/**
* kcapi_aead_destroy() - close the AEAD handle and release resources
*
* @handle: [in] cipher handle to release
*/
void kcapi_aead_destroy(struct kcapi_handle *handle);
/**
* kcapi_aead_setkey() - set the key for the AEAD handle
*
* @handle: [in] cipher handle
* @key: [in] key buffer
* @keylen: [in] length of key buffer
*
* With this function, the caller sets the key for subsequent encryption or
* decryption operations.
*
* After the caller provided the key, the caller may securely destroy the key
* as it is now maintained by the kernel.
*
* @return 0 upon success;
* a negative errno-style error code if an error occurred
*/
int kcapi_aead_setkey(struct kcapi_handle *handle,
const uint8_t *key, uint32_t keylen);
/**
* kcapi_aead_settaglen() - set authentication tag size
*
* @handle: [in] cipher handle
* @taglen: [in] length of authentication tag
*
* Set the authentication tag size needed for encryption operation. The tag is
* created during encryption operation with the size provided with this call.
*
* @return 0 upon success;
* a negative errno-style error code if an error occurred
*/
int kcapi_aead_settaglen(struct kcapi_handle *handle, uint32_t taglen);
/**
* kcapi_aead_setassoclen() - set authentication data size
*
* @handle: [in] cipher handle
* @assoclen: [in] length of associated data length
*
* The associated data is retained in the cipher handle. During initialization
* of a cipher handle, it is sent to the kernel. The kernel cipher
* implementations may verify the appropriateness of the authentication
* data size and may return an error during initialization if the
* authentication size is not considered appropriate.
*/
void kcapi_aead_setassoclen(struct kcapi_handle *handle, uint32_t assoclen);
/**
* kcapi_aead_encrypt() - synchronously encrypt AEAD data (one shot)
*
* @handle: [in] cipher handle
* @in: [in] plaintext data buffer
* @inlen: [in] length of plaintext buffer
* @iv: [in] IV to be used for cipher operation
* @out: [out] data buffer holding cipher text and authentication tag
* @outlen: [in] length of out buffer
* @access: [in] kernel access type (KCAPI_ACCESS_HEURISTIC - use internal
* heuristic for fastest kernel access; KCAPI_ACCESS_VMSPLICE - use
* vmsplice access; KCAPI_ACCESS_SENDMSG - sendmsg access)
*
* The AEAD cipher operation requires the furnishing of the associated
* authentication data. In case such data is not required, it can be set to
* NULL and length value must be set to zero.
*
* It is perfectly legal to use the same buffer as the plaintext and
* ciphertext pointers. That would mean that after the encryption operation,
* the plaintext is overwritten with the ciphertext.
*
* The memory should be aligned at the page boundary using
* posix_memalign(sysconf(_SC_PAGESIZE)), If it is not aligned at the page
* boundary, the vmsplice call may not send all data to the kernel.
*
* The IV buffer must be exactly kcapi_cipher_ivsize() bytes in size.
*
* After invoking this function the caller should use
* kcapi_aead_getdata_output() to obtain the resulting ciphertext and
* authentication tag references.
*
* IMPORTANT NOTE: The kernel will only process
* sysconf(_SC_PAGESIZE) * ALG_MAX_PAGES at one time. Longer input data cannot
* be handled by the kernel.
*
* @return number of bytes encrypted upon success;
* a negative errno-style error code if an error occurred
*/
int32_t kcapi_aead_encrypt(struct kcapi_handle *handle,
const uint8_t *in, uint32_t inlen,
const uint8_t *iv,
uint8_t *out, uint32_t outlen,
int access);
/**
* kcapi_aead_encrypt_aio() - asynchronously encrypt AEAD data (one shot)
*
* @handle: [in] cipher handle
* @iniov: [in] array of scatter-gather list with input buffers
* @outiov: [out] array of scatter-gather list with output buffers
* @iovlen: [in] number of IOVECs in array
* @iv: [in] IV to be used for cipher operation
* @access: [in] kernel access type (KCAPI_ACCESS_HEURISTIC - use internal
* heuristic for fastest kernel access; KCAPI_ACCESS_VMSPLICE - use
* vmsplice access; KCAPI_ACCESS_SENDMSG - sendmsg access)
*
* The AEAD cipher operation requires the furnishing of the associated
* authentication data. In case such data is not required, it can be set to
* NULL and length value must be set to zero.
*
* Each IOVEC is processed with its individual AEAD cipher operation. The
* memory holding the input data will receive the processed data.
*
* The memory should be aligned at the page boundary using
* posix_memalign(sysconf(_SC_PAGESIZE)), If it is not aligned at the page
* boundary, the vmsplice call may not send all data to the kernel.
*
* The IV buffer must be exactly kcapi_cipher_ivsize() bytes in size.
*
* After invoking this function the caller should use
* kcapi_aead_getdata_output() to obtain the resulting ciphertext and
* authentication tag references.
*
* IMPORTANT NOTE: The kernel will only process
* sysconf(_SC_PAGESIZE) * ALG_MAX_PAGES at one time. Longer input data cannot
* be handled by the kernel.
*
* @return number of bytes encrypted upon success;
* a negative errno-style error code if an error occurred
*/
int32_t kcapi_aead_encrypt_aio(struct kcapi_handle *handle, struct iovec *iniov,
struct iovec *outiov, uint32_t iovlen,
const uint8_t *iv, int access);
/**
* kcapi_aead_getdata_input() - get the pointers into input buffer
*
* @handle: [in] cipher handle
* @encdata: [in] data buffer returned by the encryption operation
* @encdatalen: [in] size of the encryption data buffer
* @enc: [in] does output buffer hold encryption or decryption result?
* @aad: [out] AD buffer pointer; when set to NULL, no data pointer is
* returned
* @aadlen: [out] length of AD; when aad was set to NULL, no information is
* returned
* @data: [out] pointer to output buffer from AEAD encryption operation
* when set to NULL, no data pointer is returned
* @datalen: [out] length of data buffer; when data was set to NULL, no
* information is returned
* @tag: [out] tag buffer pointer; when set to NULL, no data pointer is
* returned
* @taglen: [out] length of tag; when tag was set to NULL, no information
* is returned
*
* This function is a service function to the consumer to locate the right
* ciphertext buffer offset holding the authentication tag. In addition, it
* provides the consumer with the length of the tag and the length of the
* ciphertext.
*/
void kcapi_aead_getdata_input(struct kcapi_handle *handle,
uint8_t *encdata, uint32_t encdatalen, int enc,
uint8_t **aad, uint32_t *aadlen,
uint8_t **data, uint32_t *datalen,
uint8_t **tag, uint32_t *taglen);
/**
* kcapi_aead_getdata_output() - get the pointers into output buffer
*
* @handle: [in] cipher handle
* @encdata: [in] data buffer returned by the encryption operation
* @encdatalen: [in] size of the encryption data buffer
* @enc: [in] does output buffer hold encryption or decryption result?
* @aad: [out] AD buffer pointer; when set to NULL, no data pointer is
* returned; returned pointer may also be NULL
* @aadlen: [out] length of AD; when aad was set to NULL, no information is
* returned
* @data: [out] pointer to output buffer from AEAD encryption operation
* when set to NULL, no data pointer is returned
* @datalen: [out] length of data buffer; when data was set to NULL, no
* information is returned
* @tag: [out] tag buffer pointer; when set to NULL, no data pointer is
* returned; returned pointer may also be NULL
* @taglen: [out] length of tag; when tag was set to NULL, no information
* is returned
*
* This function is a service function to the consumer to locate the right
* ciphertext buffer offset holding the authentication tag. In addition, it
* provides the consumer with the length of the tag and the length of the
* ciphertext.
*/
void kcapi_aead_getdata_output(struct kcapi_handle *handle,
uint8_t *encdata, uint32_t encdatalen, int enc,
uint8_t **aad, uint32_t *aadlen,
uint8_t **data, uint32_t *datalen,
uint8_t **tag, uint32_t *taglen);
/**
* kcapi_aead_decrypt() - synchronously decrypt AEAD data (one shot)
*
* @handle: [in] cipher handle
* @in: [in] ciphertext data buffer
* @inlen: [in] length of in buffer
* @iv: [in] IV to be used for cipher operation
* @out: [out] plaintext data buffer
* @outlen: [in] length of out buffer
* @access: [in] kernel access type (KCAPI_ACCESS_HEURISTIC - use internal
* heuristic for fastest kernel access; KCAPI_ACCESS_VMSPLICE
* use vmsplice access; KCAPI_ACCESS_SENDMSG sendmsg access)
*
* The AEAD cipher operation requires the furnishing of the associated
* authentication data. In case such data is not required, it can be set to
* NULL and length value must be set to zero.
*
* It is perfectly legal to use the same buffer as the plaintext and
* ciphertext pointers. That would mean that after the encryption operation,
* the ciphertext is overwritten with the plaintext.
*
* The memory should be aligned at the page boundary using
* posix_memalign(sysconf(_SC_PAGESIZE)), If it is not aligned at the page
* boundary, the vmsplice call may not send all data to the kernel.
*
* The IV buffer must be exactly kcapi_cipher_ivsize() bytes in size.
*
* To catch authentication errors (i.e. integrity violations) during
* the decryption operation, the return value of this call should be
* checked. If this function returns -EBADMSG, an authentication error
* was detected.
*
* IMPORTANT NOTE: The kernel will only process
* sysconf(_SC_PAGESIZE) * ALG_MAX_PAGES at one time. Longer input data cannot
* be handled by the kernel.
*
* @return number of bytes decrypted upon success;
* a negative errno-style error code if an error occurred
*/
int32_t kcapi_aead_decrypt(struct kcapi_handle *handle,
const uint8_t *in, uint32_t inlen,
const uint8_t *iv,
uint8_t *out, uint32_t outlen, int access);
/**
* kcapi_aead_decrypt_aio() - asynchronously decrypt AEAD data (one shot)
*
* @handle: [in] cipher handle
* @iniov: [in] array of scatter-gather list with input buffers
* @outiov: [out] array of scatter-gather list with output buffers
* @iovlen: [in] number of IOVECs in array
* @iv: [in] IV to be used for cipher operation
* @access: [in] kernel access type (KCAPI_ACCESS_HEURISTIC - use internal
* heuristic for fastest kernel access; KCAPI_ACCESS_VMSPLICE - use
* vmsplice access; KCAPI_ACCESS_SENDMSG - sendmsg access)
*
* The AEAD cipher operation requires the furnishing of the associated
* authentication data. In case such data is not required, it can be set to
* NULL and length value must be set to zero.
*
* Each IOVEC is processed with its individual AEAD cipher operation. The
* memory holding the input data will receive the processed data.
*
* The memory should be aligned at the page boundary using
* posix_memalign(sysconf(_SC_PAGESIZE)), If it is not aligned at the page
* boundary, the vmsplice call may not send all data to the kernel.
*
* The IV buffer must be exactly kcapi_cipher_ivsize() bytes in size.
*
* To catch authentication errors (i.e. integrity violations) during
* the decryption operation, the return value of this call should be
* checked. If this function returns -EBADMSG, an authentication error
* was detected.
*
* IMPORTANT NOTE: The kernel will only process
* sysconf(_SC_PAGESIZE) * ALG_MAX_PAGES at one time. Longer input data cannot
* be handled by the kernel.
*
* @return number of bytes encrypted upon success;
* a negative errno-style error code if an error occurred
*/
int32_t kcapi_aead_decrypt_aio(struct kcapi_handle *handle, struct iovec *iniov,
struct iovec *outiov, uint32_t iovlen,
const uint8_t *iv, int access);
/**
* kcapi_aead_stream_init_enc() - start an encryption operation (stream)
*
* @handle: [in] cipher handle
* @iv: [in] IV to be used for cipher operation
* @iov: [in] scatter/gather list with data to be encrypted. This is the
* pointer to
* the first iov entry if an array of iov entries is supplied. See
* sendmsg(2) for details on how iov is to be used. This pointer may be
* NULL if no data to be encrypted is available at the point of the call.
* @iovlen: [in] number of scatter/gather list elements. If iov is NULL,
* this value must be zero.
*
* A stream encryption operation is started with this call. Multiple
* successive kcapi_aead_stream_update() function calls can be invoked to
* send more plaintext data to be encrypted. The kernel buffers the input
* until kcapi_aead_stream_op() picks up the encrypted data. Once plaintext
* is encrypted during the kcapi_aead_stream_op() it is removed from the
* kernel buffer.
*
* Note, unlike the corresponding symmetric cipher API, the function calls of
* kcapi_aead_stream_update() and kcapi_aead_stream_op() cannot be mixed! This
* due to the nature of AEAD where the cipher operation ensures the integrity
* of the entire data (decryption) or calculates a message digest over the
* entire data (encryption).
*
* When using the stream API, the caller must ensure that data is sent
* in the correct order (regardless whether data is sent in multiple chunks
* using kcapi_aead_stream_init_enc() or kcapi_cipher_stream_update()): (i)
* the complete associated data must be provided, followed by (ii) the
* plaintext.
*
* The IV buffer must be exactly kcapi_cipher_ivsize() bytes in size.
*
* @return number of bytes sent to the kernel upon success;
* a negative errno-style error code if an error occurred
*/
int32_t kcapi_aead_stream_init_enc(struct kcapi_handle *handle,
const uint8_t *iv,
struct iovec *iov, uint32_t iovlen);
/**
* kcapi_aead_stream_init_dec() - start a decryption operation (stream)
*
* @handle: [in] cipher handle
* @iv: [in] IV to be used for cipher operation
* @iov: [in] scatter/gather list with data to be encrypted. This is the
* pointer to the first iov entry if an array of iov entries is supplied.
* See sendmsg(2) for details on how iov is to be used. This pointer may be
* NULL if no data to be encrypted is available at the point of the call.
* @iovlen: [in] number of scatter/gather list elements. If iov is NULL,
* this value must be zero.
*
* A stream decryption operation is started with this call. Multiple
* successive kcapi_aead_stream_update() function calls can be invoked to
* send more ciphertext data to be encrypted. The kernel buffers the input
* until kcapi_aead_stream_op() picks up the decrypted data. Once ciphertext
* is decrypted during the kcapi_aead_stream_op() it is removed from the
* kernel buffer.
*
* Note, unlike the corresponding symmetric cipher API, the function calls of
* kcapi_aead_stream_update() and kcapi_aead_stream_op() cannot be mixed! This
* due to the nature of AEAD where the cipher operation ensures the integrity
* of the entire data (decryption) or calculates a message digest over the
* entire data (encryption).
*
* When using the stream API, the caller must ensure that data is sent
* in the correct order (regardless whether data is sent in multiple chunks
* using kcapi_aead_stream_init_enc() or kcapi_cipher_stream_update()): (i)
* the complete associated data must be provided, followed by (ii) the
* plaintext. For decryption, also (iii) the tag value must be sent.
*
* The IV buffer must be exactly kcapi_cipher_ivsize() bytes in size.
*
* @return number of bytes sent to the kernel upon success;
* a negative errno-style error code if an error occurred
*/
int32_t kcapi_aead_stream_init_dec(struct kcapi_handle *handle,
const uint8_t *iv,
struct iovec *iov, uint32_t iovlen);
/**
* kcapi_aead_stream_update() - send more data for processing (stream)
*
* @handle: [in] cipher handle
* @iov: [in] scatter/gather list with data to be processed by the cipher
* operation.
* @iovlen: [in] number of scatter/gather list elements.
*
* Using this function call, more plaintext for encryption or ciphertext for
* decryption can be submitted to the kernel.
*
* Note, see the order of input data as outlined in
* kcapi_aead_stream_init_dec().
*
* This function may cause the caller to sleep if the kernel buffer holding
* the data is getting full. The process will be woken up once more buffer
* space becomes available by calling kcapi_aead_stream_op().
*
* Note: The last block of input data MUST be provided with
* kcapi_aead_stream_update_last() as the kernel must be informed about the
* completion of the input data.
*
* With the separate API calls of kcapi_aead_stream_update() and
* kcapi_aead_stream_op() a multi-threaded application can be implemented
* where one thread sends data to be processed and one thread picks up data
* processed by the cipher operation.
*
* IMPORTANT NOTE: The kernel will only process
* sysconf(_SC_PAGESIZE) * ALG_MAX_PAGES at one time. Longer input data cannot
* be handled by the kernel.
*
* WARNING: The memory referenced by @iov is not accessed by the kernel
* during this call. The memory is first accessed when kcapi_cipher_stream_op()
* is called. Thus, you MUST make sure that the referenced memory is still
* present at the time kcapi_cipher_stream_op() is called.
*
* @return number of bytes sent to the kernel upon success;
* a negative errno-style error code if an error occurred
*/
int32_t kcapi_aead_stream_update(struct kcapi_handle *handle,
struct iovec *iov, uint32_t iovlen);
/**
* kcapi_aead_stream_update_last() - send last data for processing (stream)
*
* @handle: [in] cipher handle
* @iov: [in] scatter/gather list with data to be processed by the cipher
* operation.
* @iovlen: [in] number of scatter/gather list elements.
*
* Using this function call, more plaintext for encryption or ciphertext for
* decryption can be submitted to the kernel.
*
* This call is identical to the kcapi_aead_stream_update() call with the
* exception that it marks the last data buffer before the cipher operation
* is triggered. Typically, the tag value is provided with this call.
*
* WARNING: The memory referenced by @iov is not accessed by the kernel
* during this call. The memory is first accessed when kcapi_cipher_stream_op()
* is called. Thus, you MUST make sure that the referenced memory is still
* present at the time kcapi_cipher_stream_op() is called.
*
* @return number of bytes sent to the kernel upon success;
* a negative errno-style error code if an error occurred
*/
int32_t kcapi_aead_stream_update_last(struct kcapi_handle *handle,
struct iovec *iov, uint32_t iovlen);
/**
* kcapi_aead_stream_op() - obtain processed data (stream)
*
* @handle: [in] cipher handle
* @iov: [out] scatter/gather list pointing to buffers to be filled with
* the resulting data from a cipher operation.
* @iovlen: [in] number of outiov scatter/gather list elements.
*
* This function may cause the caller to sleep if the kernel buffer holding
* the data is empty. The process will be woken up once more data is sent
* by calling kcapi_cipher_stream_update().
*
* Note, when supplying buffers that are not multiple of block size, the buffers
* will only be filled up to the maximum number of full block sizes that fit
* into the buffer.
*
* @return number of bytes obtained from the kernel upon success;
* a negative errno-style error code if an error occurred
*/
int32_t kcapi_aead_stream_op(struct kcapi_handle *handle,
struct iovec *iov, uint32_t iovlen);
/**
* kcapi_aead_ivsize() - return size of IV required for cipher
*
* @handle: [in] cipher handle
*
* @return > 0 specifying the IV size;
* 0 on error
*/
uint32_t kcapi_aead_ivsize(struct kcapi_handle *handle);
/**
* kcapi_aead_blocksize() - return size of one block of the cipher
*
* @handle: [in] cipher handle
*
* @return > 0 specifying the block size;
* 0 on error
*/
uint32_t kcapi_aead_blocksize(struct kcapi_handle *handle);
/**
* kcapi_aead_authsize() - return the maximum size of the tag
*
* @handle: [in] cipher handle
*
* The returned maximum is the largest size of the authenticaation tag that can
* be produced by the AEAD cipher. Smaller tag sizes may be chosen depending on
* the AEAD cipher type.
*
* @return > 0 specifying the block size;
* 0 on error
*/
uint32_t kcapi_aead_authsize(struct kcapi_handle *handle);
/**
* kcapi_aead_inbuflen_enc() - return minimum encryption input buffer length
*
* @handle: [in] cipher handle
* @inlen: [in] size of plaintext
* @assoclen: [in] size of associated data (AD)
* @taglen: [in] size of authentication tag
*
* @return minimum size of input data length in bytes
*/
uint32_t kcapi_aead_inbuflen_enc(struct kcapi_handle *handle,
uint32_t inlen, uint32_t assoclen,
uint32_t taglen);
/**
* kcapi_aead_inbuflen_dec() - return minimum decryption input buffer length
*
* @handle: [in] cipher handle
* @inlen: [in] size of ciphertext
* @assoclen: [in] size of associated data (AD)
* @taglen: [in] size of authentication tag
*
* @return minimum size of output data length in bytes
*/
uint32_t kcapi_aead_inbuflen_dec(struct kcapi_handle *handle,
uint32_t inlen, uint32_t assoclen,
uint32_t taglen);
/**
* kcapi_aead_outbuflen_enc() - return minimum encryption output buffer length
*
* @handle: [in] cipher handle
* @inlen: [in] size of plaintext
* @assoclen: [in] size of associated data (AD)
* @taglen: [in] size of authentication tag
*
* @return minimum size of output data length in bytes
*/
uint32_t kcapi_aead_outbuflen_enc(struct kcapi_handle *handle,
uint32_t inlen, uint32_t assoclen,
uint32_t taglen);
/**
* kcapi_aead_outbuflen_dec() - return minimum decryption output buffer length
*
* @handle: [in] cipher handle
* @inlen: [in] size of ciphertext
* @assoclen: [in] size of associated data (AD)
* @taglen: [in] size of authentication tag
*
* @return minimum size of output data length in bytes
*/
uint32_t kcapi_aead_outbuflen_dec(struct kcapi_handle *handle,
uint32_t inlen, uint32_t assoclen,
uint32_t taglen);
/**
* kcapi_aead_ccm_nonce_to_iv() - convert CCM nonce into IV
*
* @nonce: [in] buffer with nonce
* @noncelen: [in] length of nonce
* @iv: [out] newly allocated buffer with IV
* @ivlen: [out] length of IV
*
* This service function converts a CCM nonce value into an IV usable by
* the kernel crypto API.
*
* Caller must free iv.
*
* @return 0 upon success;
* < 0 upon failure
*/
int kcapi_aead_ccm_nonce_to_iv(const uint8_t *nonce, uint32_t noncelen,
uint8_t **iv, uint32_t *ivlen);
/**
* DOC: Message Digest Cipher API
*/
/**
* kcapi_md_init() - initialize cipher handle
*
* @handle: [out] cipher handle filled during the call
* @ciphername: [in] kernel crypto API cipher name as specified in /proc/crypto
* @flags: [in] flags specifying the type of cipher handle
*
* This function provides the initialization of a (keyed) message digest handle
* and establishes the connection to the kernel.
*
* On success, a pointer to kcapi_handle object is returned in *handle.
* Function kcapi_md_destroy should be called afterwards to free resources.
*
* @return 0 upon success;
* -ENOENT - algorithm not available;
* -EOPNOTSUPP - AF_ALG family not available;
* -EINVAL - accept syscall failed;
* -ENOMEM - cipher handle cannot be allocated
*/
int kcapi_md_init(struct kcapi_handle **handle, const char *ciphername,
uint32_t flags);
/**
* kcapi_md_destroy() - close the message digest handle and release resources
*
* @handle: [in] cipher handle to release
*/
void kcapi_md_destroy(struct kcapi_handle *handle);
/**
* kcapi_md_setkey() - set the key for the message digest handle
*
* @handle: [in] cipher handle
* @key: [in] key buffer
* @keylen: [in] length of key buffer
*
* With this function, the caller sets the key for subsequent hashing
* operations. This call is applicable for keyed message digests.
*
* After the caller provided the key, the caller may securely destroy the key
* as it is now maintained by the kernel.
*
* @return 0 upon success;
* a negative errno-style error code if an error occurred
*/
int kcapi_md_setkey(struct kcapi_handle *handle,
const uint8_t *key, uint32_t keylen);
/**
* kcapi_md_update() - message digest update function (stream)
*
* @handle: [in] cipher handle
* @buffer: [in] holding the data to add to the message digest
* @len: [in] buffer length
*
* The input buffer can be at most INT_MAX in size.
*
* @return 0 upon success;
* a negative errno-style error code if an error occurred
*/
int32_t kcapi_md_update(struct kcapi_handle *handle,
const uint8_t *buffer, uint32_t len);
/**
* kcapi_md_final() - message digest finalization function (stream)
*
* @handle: [in] cipher handle
* @buffer: [out] filled with the message digest
* @len: [in] buffer length
*
* @return size of message digest upon success;
* -EIO - data cannot be obtained;
* -ENOMEM - buffer is too small for the complete message digest,
* the buffer is filled with the truncated message digest
*/
int32_t kcapi_md_final(struct kcapi_handle *handle,
uint8_t *buffer, uint32_t len);
/**
* kcapi_md_digest() - calculate message digest on buffer (one-shot)
*
* @handle: [in] cipher handle
* @in: [in] buffer with input data
* @inlen: [in] length of input buffer
* @out: [out] buffer for message digest
* @outlen: [in] length of out
*
* With this one-shot function, a message digest of the given buffer is
* generated. The output buffer must be allocated by the caller and have at
* least the length of the message digest size for the chosen message digest.
*
* The message digest handle must have been initialized, potentially by also
* setting the key using the generic message digest API functions.
*
* The input buffer can be at most INT_MAX in size.
*
* @return size of message digest upon success;
* -EIO - data cannot be obtained;
* -ENOMEM - buffer is too small for the complete message digest,
* the buffer is filled with the truncated message digest
*/
int32_t kcapi_md_digest(struct kcapi_handle *handle,
const uint8_t *in, uint32_t inlen,
uint8_t *out, uint32_t outlen);
/**
* kcapi_md_sha1 - SHA-1 message digest on one buffer
*
* @in: [in] buffer with input data
* @inlen: [in] length of input buffer
* @out: [out] buffer for message digest
* @outlen: [in] length of out
*
* With this one-shot convenience function, a message digest of the given buffer
* is generated. The output buffer must be allocated by the caller and have at
* least the length of the message digest size for the chosen message digest.
*
* @return size of message digest upon success;
* -EIO - data cannot be obtained;
* -ENOMEM - buffer is too small for the complete message digest,
* the buffer is filled with the truncated message digest
*/
int32_t kcapi_md_sha1(const uint8_t *in, uint32_t inlen,
uint8_t *out, uint32_t outlen);
/**
* kcapi_md_sha224 - SHA-224 message digest on one buffer
*
* @in: [in] buffer with input data
* @inlen: [in] length of input buffer
* @out: [out] buffer for message digest
* @outlen: [in] length of out
*
* With this one-shot convenience function, a message digest of the given buffer
* is generated. The output buffer must be allocated by the caller and have at
* least the length of the message digest size for the chosen message digest.
*
* @return size of message digest upon success;
* -EIO - data cannot be obtained;
* -ENOMEM - buffer is too small for the complete message digest,
* the buffer is filled with the truncated message digest
*/
int32_t kcapi_md_sha224(const uint8_t *in, uint32_t inlen,
uint8_t *out, uint32_t outlen);
/**
* kcapi_md_sha256 - SHA-256 message digest on one buffer
*
* @in: [in] buffer with input data
* @inlen: [in] length of input buffer
* @out: [out] buffer for message digest
* @outlen: [in] length of out
*
* With this one-shot convenience function, a message digest of the given buffer
* is generated. The output buffer must be allocated by the caller and have at
* least the length of the message digest size for the chosen message digest.
*
* @return size of message digest upon success;
* -EIO - data cannot be obtained;
* -ENOMEM - buffer is too small for the complete message digest,
* the buffer is filled with the truncated message digest
*/
int32_t kcapi_md_sha256(const uint8_t *in, uint32_t inlen,
uint8_t *out, uint32_t outlen);
/**
* kcapi_md_sha384 - SHA-384 message digest on one buffer
*
* @in: [in] buffer with input data
* @inlen: [in] length of input buffer
* @out: [out] buffer for message digest
* @outlen: [in] length of out
*
* With this one-shot convenience function, a message digest of the given buffer
* is generated. The output buffer must be allocated by the caller and have at
* least the length of the message digest size for the chosen message digest.
*
* @return size of message digest upon success;
* -EIO - data cannot be obtained;
* -ENOMEM - buffer is too small for the complete message digest,
* the buffer is filled with the truncated message digest
*/
int32_t kcapi_md_sha384(const uint8_t *in, uint32_t inlen,
uint8_t *out, uint32_t outlen);
/**
* kcapi_md_sha512 - SHA-512 message digest on one buffer
*
* @in: [in] buffer with input data
* @inlen: [in] length of input buffer
* @out: [out] buffer for message digest
* @outlen: [in] length of out
*
* With this one-shot convenience function, a message digest of the given buffer
* is generated. The output buffer must be allocated by the caller and have at
* least the length of the message digest size for the chosen message digest.
*
* @return size of message digest upon success;
* -EIO - data cannot be obtained;
* -ENOMEM - buffer is too small for the complete message digest,
* the buffer is filled with the truncated message digest
*/
int32_t kcapi_md_sha512(const uint8_t *in, uint32_t inlen,
uint8_t *out, uint32_t outlen);
/**
* kcapi_md_hmac_sha1 - HMAC SHA-1 keyed message digest on one buffer
*
* @key: [in] buffer with HMAC key
* @keylen: [in] length of HMAC key buffer
* @in: [in] buffer with input data
* @inlen: [in] length of input buffer
* @out: [out] buffer for message digest
* @outlen: [in] length of out
*
* With this one-shot convenience function, a keyed message digest of the given
* buffer is generated. The output buffer must be allocated by the caller and
* have at least the length of the message digest size for the chosen keyed
* message digest.
*
* @return size of message digest upon success;
* -EIO - data cannot be obtained;
* -ENOMEM - buffer is too small for the complete message digest,
* the buffer is filled with the truncated message digest
*/
int32_t kcapi_md_hmac_sha1(const uint8_t *key, uint32_t keylen,
const uint8_t *in, uint32_t inlen,
uint8_t *out, uint32_t outlen);
/**
* kcapi_md_hmac_sha224 - HMAC SHA-224 keyed message digest on one buffer
*
* @key: [in] buffer with HMAC key
* @keylen: [in] length of HMAC key buffer
* @in: [in] buffer with input data
* @inlen: [in] length of input buffer
* @out: [out] buffer for message digest
* @outlen: [in] length of out
*
* With this one-shot convenience function, a keyed message digest of the given
* buffer is generated. The output buffer must be allocated by the caller and
* have at least the length of the message digest size for the chosen keyed
* message digest.
*
* @return size of message digest upon success;
* -EIO - data cannot be obtained;
* -ENOMEM - buffer is too small for the complete message digest,
* the buffer is filled with the truncated message digest
*/
int32_t kcapi_md_hmac_sha224(const uint8_t *key, uint32_t keylen,
const uint8_t *in, uint32_t inlen,
uint8_t *out, uint32_t outlen);
/**
* kcapi_md_hmac_sha256 - HMAC SHA-256 keyed message digest on one buffer
*
* @key: [in] buffer with HMAC key
* @keylen: [in] length of HMAC key buffer
* @in: [in] buffer with input data
* @inlen: [in] length of input buffer
* @out: [out] buffer for message digest
* @outlen: [in] length of out
*
* With this one-shot convenience function, a keyed message digest of the given
* buffer is generated. The output buffer must be allocated by the caller and
* have at least the length of the message digest size for the chosen keyed
* message digest.
*
* @return size of message digest upon success;
* -EIO - data cannot be obtained;
* -ENOMEM - buffer is too small for the complete message digest,
* the buffer is filled with the truncated message digest
*/
int32_t kcapi_md_hmac_sha256(const uint8_t *key, uint32_t keylen,
const uint8_t *in, uint32_t inlen,
uint8_t *out, uint32_t outlen);
/**
* kcapi_md_hmac_sha384 - HMAC SHA-384 keyed message digest on one buffer
*
* @key: [in] buffer with HMAC key
* @keylen: [in] length of HMAC key buffer
* @in: [in] buffer with input data
* @inlen: [in] length of input buffer
* @out: [out] buffer for message digest
* @outlen: [in] length of out
*
* With this one-shot convenience function, a keyed message digest of the given
* buffer is generated. The output buffer must be allocated by the caller and
* have at least the length of the message digest size for the chosen keyed
* message digest.
*
* @return size of message digest upon success;
* -EIO - data cannot be obtained;
* -ENOMEM - buffer is too small for the complete message digest,
* the buffer is filled with the truncated message digest
*/
int32_t kcapi_md_hmac_sha384(const uint8_t *key, uint32_t keylen,
const uint8_t *in, uint32_t inlen,
uint8_t *out, uint32_t outlen);
/**
* kcapi_md_hmac_sha512 - HMAC SHA-512 keyed message digest on one buffer
*
* @key: [in] buffer with HMAC key
* @keylen: [in] length of HMAC key buffer
* @in: [in] buffer with input data
* @inlen: [in] length of input buffer
* @out: [out] buffer for message digest
* @outlen: [in] length of out
*
* With this one-shot convenience function, a keyed message digest of the given
* buffer is generated. The output buffer must be allocated by the caller and
* have at least the length of the message digest size for the chosen keyed
* message digest.
*
* @return size of message digest upon success;
* -EIO - data cannot be obtained;
* -ENOMEM - buffer is too small for the complete message digest,
* the buffer is filled with the truncated message digest
*/
int32_t kcapi_md_hmac_sha512(const uint8_t *key, uint32_t keylen,
const uint8_t *in, uint32_t inlen,
uint8_t *out, uint32_t outlen);
/**
* kcapi_md_digestsize() - return the size of the message digest
*
* @handle: [in] cipher handle
*
* The returned message digest size can be used before the kcapi_md_final
* function invocation to determine the right memory size to be allocated for
* this call.
*
* @return > 0 specifying the block size;
* 0 on error
*/
uint32_t kcapi_md_digestsize(struct kcapi_handle *handle);
/**
* kcapi_md_blocksize() - return size of one block of the message digest
*
* @handle: [in] cipher handle
*
* @return > 0 specifying the block size;
* 0 on error
*/
uint32_t kcapi_md_blocksize(struct kcapi_handle *handle);
/**
* DOC: Random Number API
*/
/**
* kcapi_rng_init() - initialize cipher handle
*
* @handle: [out] cipher handle filled during the call
* @ciphername: [in] kernel crypto API cipher name as specified in
* /proc/crypto
* @flags: [in] flags specifying the type of cipher handle (unused for RNG)
*
* This function provides the initialization of a random number generator handle
* and establishes the connection to the kernel.
*
* On success, a pointer to kcapi_handle object is returned in *handle.
* Function kcapi_rng_destroy should be called afterwards to free resources.
*
* @return 0 upon success;
* -ENOENT - algorithm not available;
* -EOPNOTSUPP - AF_ALG family not available;
* -EINVAL - accept syscall failed
* -ENOMEM - cipher handle cannot be allocated
*/
int kcapi_rng_init(struct kcapi_handle **handle, const char *ciphername,
uint32_t flags);
/**
* kcapi_rng_destroy() - close the RNG handle and release resources
*
* @handle: [in] cipher handle to release
*/
void kcapi_rng_destroy(struct kcapi_handle *handle);
/**
* kcapi_rng_seed() - seed the RNG
*
* @handle: [in] cipher handle
* @seed: [in] seed data
* @seedlen: [in] size of seed
*
* Note, this call must be called to initialize the selected RNG. When the
* SP800-90A DRBG is used, this call causes the DRBG to seed itself from the
* internal noise sources.
*
* Note, in case of using the SP800-90A DRBG, the seed buffer may be NULL. If
* it is not NULL, the DRBG uses the given data either as personalization string
* in case of the initial seeding or additional data for reseeding.
*
* @return 0 upon success;
* a negative errno-style error code if an error occurred
*/
int kcapi_rng_seed(struct kcapi_handle *handle, uint8_t *seed,
uint32_t seedlen);
/**
* kcapi_rng_generate() - generate a random number
*
* @handle: [in] cipher handle
* @buffer: [out] filled with the random number
* @len: [in] buffer length
*
* @return size of random number generated upon success;
* -EIO - data cannot be obtained
*/
int32_t kcapi_rng_generate(struct kcapi_handle *handle,
uint8_t *buffer, uint32_t len);
/**
* kcapi_rng_get_bytes - Convenience function to generate random bytes
*
* @buffer: [out] filled with the random number
* @outlen: [in] buffer length
*
* This convenience function generates random bytes of the size of outlen
* and stores them into the provided buffer.
*
* @return size of random number generated upon success;
* -EIO - data cannot be obtained
*/
int32_t kcapi_rng_get_bytes(uint8_t *buffer, uint32_t outlen);
/**
* kcapi_rng_seedsize() - return required seed size of DRNG
*
* @handle: [in] cipher handle
*
* @return > 0 specifying the block size;
* 0 on error
*/
uint32_t kcapi_rng_seedsize(struct kcapi_handle *handle);
/**
* DOC: Common API
*
* The following API calls are common to all cipher types.
*/
enum kcapi_verbosity {
KCAPI_LOG_NONE,
KCAPI_LOG_ERR,
KCAPI_LOG_WARN,
KCAPI_LOG_VERBOSE,
KCAPI_LOG_DEBUG,
};
/**
* kcapi_set_verbosity() - set the verbosity level of the library
*
* @level: [in] verbosity level:
* LOG_ERR: only log error messages (default)
* LOG_WARN: log warnings and error messages
* LOG_VERBOSE: log verbose messages, warnings and error messages
* LOG_DEBUG: log all details of library operation
*/
void kcapi_set_verbosity(enum kcapi_verbosity level);
/**
* kcapi_versionstring() - obtain version string of kcapi library
*
* @buf: [out] buffer to place version string into
* @buflen: [in] length of buffer
*/
void kcapi_versionstring(char *buf, uint32_t buflen);
/**
* kcapi_version() - return machine-usable version number of kcapi library
*
* The function returns a version number that is monotonic increasing
* for newer versions. The version numbers are multiples of 100. For example,
* version 1.2.3 is converted to 1020300 -- the last two digits are reserved
* for future use.
*
* The result of this function can be used in comparing the version number
* in a calling program if version-specific calls need to be make.
*
* @return Version number of kcapi library
*/
uint32_t kcapi_version(void);
/**
* kcapi_pad_iv() - realign the IV as necessary for cipher
*
* @handle: [in] cipher handle
* @iv: [in] current IV buffer
* @ivlen: [in] length of IV buffer
* @newiv: [out] buffer of aligned IV
* @newivlen: [out] length of newly aligned IV
*
* The function pads the least significant bits of the provided IV up to the
* block size of the cipher with zeros. In case the provided IV is longer than
* the block size, the least significant bits are truncated to the block size.
*
* The function allocates memory for newiv in case the return code indicates
* success. The consumer must free the memory after use.
*
* @return 0 for success;
* a negative errno-style error code if an error occurred
*/
int kcapi_pad_iv(struct kcapi_handle *handle,
const uint8_t *iv, uint32_t ivlen,
uint8_t **newiv, uint32_t *newivlen);
/**
* kcapi_memset_secure() - memset() implementation that will not be optimized
* away by the compiler
*
* @s: [in] see memset(3)
* @c: [in] see memset(3)
* @n: [in] see memset(3)
*
* The parameters, he logic and the return code is identical to memset(3).
*/
void kcapi_memset_secure(void *s, int c, uint32_t n);
/**
* DOC: Asymmetric Cipher API
*
* API function calls used to invoke asymmetric ciphers.
*/
/**
* kcapi_akcipher_init() - initialize cipher handle
*
* @handle: [out] cipher handle filled during the call
* @ciphername: [in] kernel crypto API cipher name as specified in
* /proc/crypto
* @flags: [in] flags specifying the type of cipher handle
*
* This function provides the initialization of an asymmetric cipher handle and
* establishes the connection to the kernel.
*
* On success, a pointer to kcapi_handle object is returned in *handle.
* Function kcapi_akcipher_destroy should be called afterwards to free
* resources.
*
* @return 0 upon success;
* -ENOENT - algorithm not available;
* -EOPNOTSUPP - AF_ALG family not available;
* -EINVAL - accept syscall failed
* -ENOMEM - cipher handle cannot be allocated
*/
int kcapi_akcipher_init(struct kcapi_handle **handle, const char *ciphername,
uint32_t flags);
/**
* kcapi_akcipher_destroy() - close the cipher handle and release resources
*
* @handle: [in] cipher handle to release
*/
void kcapi_akcipher_destroy(struct kcapi_handle *handle);
/**
* kcapi_akcipher_setkey() - set the private key for the cipher handle
*
* @handle: [in] cipher handle
* @key: [in] key buffer in DER format
* @keylen: [in] length of key buffer
*
* With this function, the caller sets the key for subsequent cipher operations.
*
* The key must be in DER format as follows
*
* SEQUENCE {
* version INTEGER,
* n INTEGER ({ rsa_get_n }),
* e INTEGER ({ rsa_get_e }),
* d INTEGER ({ rsa_get_d }),
* prime1 INTEGER,
* prime2 INTEGER,
* exponent1 INTEGER,
* exponent2 INTEGER,
* coefficient INTEGER
*}
*
* After the caller provided the key, the caller may securely destroy the key
* as it is now maintained by the kernel.
*
* @return upon success the value of the maximum size for the asymmetric
* operation is returned (e.g. the modulus size);
* a negative errno-style error code if an error occurred
*/
int kcapi_akcipher_setkey(struct kcapi_handle *handle,
const uint8_t *key, uint32_t keylen);
/**
* kcapi_akcipher_setpubkey() - set the public key for the cipher handle
*
* @handle: [in] cipher handle
* @key: [in] key buffer in DER format
* @keylen: [in] length of key buffer
*
* With this function, the caller sets the key for subsequent cipher operations.
*
* The key must be in DER format as follows
*
* SEQUENCE {
* n INTEGER ({ rsa_get_n }),
* e INTEGER ({ rsa_get_e })
*}
*
* After the caller provided the key, the caller may securely destroy the key
* as it is now maintained by the kernel.
*
* @return upon success the value of the maximum size for the asymmetric
* operation is returned (e.g. the modulus size);
* a negative errno-style error code if an error occurred
*/
int kcapi_akcipher_setpubkey(struct kcapi_handle *handle,
const uint8_t *key, uint32_t keylen);
/**
* kcapi_akcipher_encrypt() - encrypt data
*
* @handle: [in] cipher handle
* @in: [in] plaintext data buffer
* @inlen: [in] length of in buffer
* @out: [out] ciphertext data buffer
* @outlen: [in] length of out buffer
* @access: [in] kernel access type (KCAPI_ACCESS_HEURISTIC - use internal
* heuristic for fastest kernel access; KCAPI_ACCESS_VMSPLICE - use
* vmsplice access; KCAPI_ACCESS_SENDMSG - sendmsg access)
*
* It is perfectly legal to use the same buffer as the plaintext and
* ciphertext pointers. That would mean that after the encryption operation,
* the plaintext is overwritten with the ciphertext.
*
* The memory should be aligned at the page boundary using
* posix_memalign(sysconf(_SC_PAGESIZE)), If it is not aligned at the page
* boundary, the vmsplice call may not send all data to the kernel.
*
* If the output size is insufficiently large, -EINVAL is returned. The
* output buffer must be at least as large as the modululs of the uses key.
*
* @return number of bytes returned by the encryption operation upon success;
* a negative errno-style error code if an error occurred
*/
int32_t kcapi_akcipher_encrypt(struct kcapi_handle *handle,
const uint8_t *in, uint32_t inlen,
uint8_t *out, uint32_t outlen, int access);
/**
* kcapi_akcipher_encrypt_aio() - encrypt data (asynchronous one shot)
*
* @handle: [in] cipher handle
* @iniov: [in] head of scatter-gather list array holding the plaintext
* @outiov: [out] head of scatter-gather list of the destination buffers filled
* with ciphertext
* @iovlen: [in] number of scatter-gather list entries
* @access: [in] kernel access type (KCAPI_ACCESS_HEURISTIC - use internal
* heuristic for fastest kernel access; KCAPI_ACCESS_VMSPLICE - use
* vmsplice access; KCAPI_ACCESS_SENDMSG - sendmsg access)
*
* The individual scatter-gather list entries are processed with
* separate invocations of the the given cipher.
*
* The memory should be aligned at the page boundary using
* posix_memalign(sysconf(_SC_PAGESIZE)), If it is not aligned at the page
* boundary, the vmsplice call may not send all data to the kernel.
*
* @return number of bytes encrypted upon success;
* a negative errno-style error code if an error occurred
*/
int32_t kcapi_akcipher_encrypt_aio(struct kcapi_handle *handle,
struct iovec *iniov, struct iovec *outiov,
uint32_t iovlen, int access);
/**
* kcapi_akcipher_decrypt() - decrypt data
*
* @handle: [in] cipher handle
* @in: [in] ciphertext data buffer
* @inlen: [in] length of in buffer
* @out: [out] plaintext data buffer
* @outlen: [in] length of out buffer
* @access: [in] kernel access type (KCAPI_ACCESS_HEURISTIC - use internal
* heuristic for fastest kernel access; KCAPI_ACCESS_VMSPLICE - use
* vmsplice access; KCAPI_ACCESS_SENDMSG - sendmsg access)
*
* It is perfectly legal to use the same buffer as the plaintext and
* ciphertext pointers. That would mean that after the decryption operation,
* the ciphertext is overwritten with the plaintext.
*
* The memory should be aligned at the page boundary using
* posix_memalign(sysconf(_SC_PAGESIZE)), If it is not aligned at the page
* boundary, the vmsplice call may not send all data to the kernel.
*
* If the output size is insufficiently large, -EINVAL is returned. The
* output buffer must be at least as large as the modululs of the uses key.
*
* @return number of bytes returned by the decryption operation upon success;
* a negative errno-style error code if an error occurred
*/
int32_t kcapi_akcipher_decrypt(struct kcapi_handle *handle,
const uint8_t *in, uint32_t inlen,
uint8_t *out, uint32_t outlen, int access);
/**
* kcapi_akcipher_decrypt_aio() - decrypt data (asynchronous one shot)
*
* @handle: [in] cipher handle
* @iniov: [in] head of scatter-gather list array holding the plaintext
* @outiov: [out] head of scatter-gather list of the destination buffers filled
* with ciphertext
* @iovlen: [in] number of scatter-gather list entries
* @access: [in] kernel access type (KCAPI_ACCESS_HEURISTIC - use internal
* heuristic for fastest kernel access; KCAPI_ACCESS_VMSPLICE - use
* vmsplice access; KCAPI_ACCESS_SENDMSG - sendmsg access)
*
* The individual scatter-gather list entries are processed with
* separate invocations of the the given cipher.
*
* The memory should be aligned at the page boundary using
* posix_memalign(sysconf(_SC_PAGESIZE)), If it is not aligned at the page
* boundary, the vmsplice call may not send all data to the kernel.
*
* @return number of bytes decrypted upon success;
* a negative errno-style error code if an error occurred
*/
int32_t kcapi_akcipher_decrypt_aio(struct kcapi_handle *handle,
struct iovec *iniov, struct iovec *outiov,
uint32_t iovlen, int access);
/**
* kcapi_akcipher_sign() - signature generation
*
* @handle: [in] cipher handle
* @in: [in] message data buffer
* @inlen: [in] length of in buffer
* @out: [out] signature data buffer
* @outlen: [in] length of out buffer
* @access: [in] kernel access type (KCAPI_ACCESS_HEURISTIC - use internal
* heuristic for fastest kernel access; KCAPI_ACCESS_VMSPLICE - use
* vmsplice access; KCAPI_ACCESS_SENDMSG - sendmsg access)
*
* It is perfectly legal to use the same buffer as the message and
* signature pointers. That would mean that after the signature generation
* operation, the message is overwritten with the signature.
*
* The memory should be aligned at the page boundary using
* posix_memalign(sysconf(_SC_PAGESIZE)), If it is not aligned at the page
* boundary, the vmsplice call may not send all data to the kernel.
*
* If the output size is insufficiently large, -EINVAL is returned. The
* output buffer must be at least as large as the modululs of the uses key.
*
* @return number of bytes returned by the signature gen operation upon success;
* a negative errno-style error code if an error occurred
*/
int32_t kcapi_akcipher_sign(struct kcapi_handle *handle,
const uint8_t *in, uint32_t inlen,
uint8_t *out, uint32_t outlen, int access);
/**
* kcapi_akcipher_sign_aio() - sign data (asynchronous one shot)
*
* @handle: [in] cipher handle
* @iniov: [in] head of scatter-gather list array holding the plaintext
* @outiov: [out] head of scatter-gather list of the destination buffers filled
* with ciphertext
* @iovlen: [in] number of scatter-gather list entries
* @access: [in] kernel access type (KCAPI_ACCESS_HEURISTIC - use internal
* heuristic for fastest kernel access; KCAPI_ACCESS_VMSPLICE - use
* vmsplice access; KCAPI_ACCESS_SENDMSG - sendmsg access)
*
* The individual scatter-gather list entries are processed with
* separate invocations of the the given cipher.
*
* The memory should be aligned at the page boundary using
* posix_memalign(sysconf(_SC_PAGESIZE)), If it is not aligned at the page
* boundary, the vmsplice call may not send all data to the kernel.
*
* @return number of bytes signed upon success;
* a negative errno-style error code if an error occurred
*/
int32_t kcapi_akcipher_sign_aio(struct kcapi_handle *handle,
struct iovec *iniov, struct iovec *outiov,
uint32_t iovlen, int access);
/**
* kcapi_akcipher_verify() - signature verification
*
* @handle: [in] cipher handle
* @in: [in] message data buffer
* @inlen: [in] length of in buffer
* @out: [out] signature data buffer
* @outlen: [in] length of out buffer
* @access: [in] kernel access type (KCAPI_ACCESS_HEURISTIC - use internal
* heuristic for fastest kernel access; KCAPI_ACCESS_VMSPLICE - use
* vmsplice access; KCAPI_ACCESS_SENDMSG - sendmsg access)
*
* It is perfectly legal to use the same buffer as the message and
* signature pointers. That would mean that after the signature generation
* operation, the message is overwritten with the signature.
*
* The memory should be aligned at the page boundary using
* posix_memalign(sysconf(_SC_PAGESIZE)), If it is not aligned at the page
* boundary, the vmsplice call may not send all data to the kernel.
*
* If the output size is insufficiently large, -EINVAL is returned. The
* output buffer must be at least as large as the modululs of the uses key.
*
* To catch signature verification errors, the return value of this
* call should be checked. If this function returns -EBADMSG, the
* verification of the signature failed.
*
* @return number of bytes returned by the signature ver operation upon success;
* a negative errno-style error code if an error occurred
*/
int32_t kcapi_akcipher_verify(struct kcapi_handle *handle,
const uint8_t *in, uint32_t inlen,
uint8_t *out, uint32_t outlen, int access);
/**
* kcapi_akcipher_verify_aio() - verify data (asynchronous one shot)
*
* @handle: [in] cipher handle
* @iniov: [in] head of scatter-gather list array holding the plaintext
* @outiov: [out] head of scatter-gather list of the destination buffers filled
* with ciphertext
* @iovlen: [in] number of scatter-gather list entries
* @access: [in] kernel access type (KCAPI_ACCESS_HEURISTIC - use internal
* heuristic for fastest kernel access; KCAPI_ACCESS_VMSPLICE - use
* vmsplice access; KCAPI_ACCESS_SENDMSG - sendmsg access)
*
* The individual scatter-gather list entries are processed with
* separate invocations of the the given cipher.
*
* The memory should be aligned at the page boundary using
* posix_memalign(sysconf(_SC_PAGESIZE)), If it is not aligned at the page
* boundary, the vmsplice call may not send all data to the kernel.
*
* @return number of bytes verify upon success;
* a negative errno-style error code if an error occurred
*/
int32_t kcapi_akcipher_verify_aio(struct kcapi_handle *handle,
struct iovec *iniov, struct iovec *outiov,
uint32_t iovlen, int access);
/**
* kcapi_akcipher_stream_init_enc() - start an encryption operation (stream)
*
* @handle: [in] cipher handle
* @iov: [in] scatter/gather list with data to be encrypted. This is the
* pointer to the first iov entry if an array of iov entries is supplied.
* See sendmsg(2) for details on how iov is to be used. This pointer may be
* NULL if no data to be encrypted is available at the point of the call.
* @iovlen: [in] number of scatter/gather list elements. If iov is NULL,
* this value must be zero.
*
* A stream encryption operation is started with this call. Multiple
* successive kcapi_akcipher_stream_update() function calls can be invoked to
* send more plaintext data to be encrypted. The last invocation to supply data
* must be done with kcapi_akcipher_stream_update_last(). The kernel buffers the
* input until kcapi_akcipher_stream_op() picks up the encrypted data. Once
* plaintext is encrypted during the kcapi_cipher_stream_op() it is removed
* from the kernel buffer.
*
* The function calls of kcapi_akcipher_stream_update() and
* kcapi_akcipher_stream_op() can be mixed, even by multiple threads of an
* application.
*
* @return number of bytes sent to the kernel upon success;
* a negative errno-style error code if an error occurred
*/
int32_t kcapi_akcipher_stream_init_enc(struct kcapi_handle *handle,
struct iovec *iov, uint32_t iovlen);
/**
* kcapi_akcipher_stream_init_dec() - start an decryption operation (stream)
* @handle: [in] cipher handle
* @iov: [in] scatter/gather list with data to be decrypted. This is the
* pointer to the first iov entry if an array of iov entries is supplied.
* See sendmsg(2) for details on how iov is to be used. This pointer may be
* NULL if no data to be decrypted is available at the point of the call.
* @iovlen: [in] number of scatter/gather list elements. If iov is NULL,
* this value must be zero.
*
* A stream decryption operation is started with this call. Multiple
* successive kcapi_akcipher_stream_update() function calls can be invoked to
* send more plaintext data to be decrypted. The last invocation to supply data
* must be done with kcapi_akcipher_stream_update_last(). The kernel buffers the
* input until kcapi_akcipher_stream_op() picks up the encrypted data. Once
* plaintext is decrypted during the kcapi_cipher_stream_op() it is removed
* from the kernel buffer.
*
* The function calls of kcapi_akcipher_stream_update() and
* kcapi_akcipher_stream_op() can be mixed, even by multiple threads of an
* application.
*
* @return number of bytes sent to the kernel upon success;
* a negative errno-style error code if an error occurred
*/
int32_t kcapi_akcipher_stream_init_dec(struct kcapi_handle *handle,
struct iovec *iov, uint32_t iovlen);
/**
* kcapi_akcipher_stream_init_sgn() - start an signing operation (stream)
*
* @handle: [in] cipher handle
* @iov: [in] scatter/gather list with data to be signed. This is the
* pointer to the first iov entry if an array of iov entries is supplied.
* See sendmsg(2) for details on how iov is to be used. This pointer may be
* NULL if no data to be signed is available at the point of the call.
* @iovlen: [in] number of scatter/gather list elements. If iov is NULL,
* this value must be zero.
*
* A stream signing operation is started with this call. Multiple
* successive kcapi_akcipher_stream_update() function calls can be invoked to
* send more plaintext data to be signed. The last invocation to supply data
* must be done with kcapi_akcipher_stream_update_last(). The kernel buffers the
* input until kcapi_akcipher_stream_op() picks up the signed data. Once
* plaintext is signed during the kcapi_cipher_stream_op() it is removed
* from the kernel buffer.
*
* The function calls of kcapi_akcipher_stream_update() and
* kcapi_akcipher_stream_op() can be mixed, even by multiple threads of an
* application.
*
* @return number of bytes sent to the kernel upon success;
* a negative errno-style error code if an error occurred
*/
int32_t kcapi_akcipher_stream_init_sgn(struct kcapi_handle *handle,
struct iovec *iov, uint32_t iovlen);
/**
* kcapi_akcipher_stream_init_vfy() - start an signature verification operation
* (stream)
*
* @handle: [in] cipher handle
* @iov: [in] scatter/gather list with data to be verified. This is the
* pointer to the first iov entry if an array of iov entries is supplied.
* See sendmsg(2) for details on how iov is to be used. This pointer may be
* NULL if no data to be verified is available at the point of the call.
* @iovlen: [in] number of scatter/gather list elements. If iov is NULL,
* this value must be zero.
*
* A stream signature verification operation is started with this call. Multiple
* successive kcapi_akcipher_stream_update() function calls can be invoked to
* send more plaintext data to be verified. The last invocation to supply data
* must be done with kcapi_akcipher_stream_update_last(). The kernel buffers the
* input until kcapi_akcipher_stream_op() picks up the verified data. Once
* plaintext is verified during the kcapi_cipher_stream_op() it is removed
* from the kernel buffer.
*
* The function calls of kcapi_akcipher_stream_update() and
* kcapi_akcipher_stream_op() can be mixed, even by multiple threads of an
* application.
*
* @return number of bytes sent to the kernel upon success;
* a negative errno-style error code if an error occurred
*/
int32_t kcapi_akcipher_stream_init_vfy(struct kcapi_handle *handle,
struct iovec *iov, uint32_t iovlen);
/**
* kcapi_akcipher_stream_update() - send more data for processing (stream)
*
* @handle: [in] cipher handle
* @iov: [in] scatter/gather list with data to be processed by the cipher
* operation.
* @iovlen: [in] number of scatter/gather list elements.
*
* Using this function call, more data can be submitted to the kernel.
*
* This function may cause the caller to sleep if the kernel buffer holding
* the data is getting full. The process will be woken up once more buffer
* space becomes available by calling kcapi_akcipher_stream_op().
*
* Note: with the separate API calls of kcapi_akcipher_stream_update() and
* kcapi_akcipher_stream_op() a multi-threaded application can be implemented
* where one thread sends data to be processed and one thread picks up data
* processed by the cipher operation.
*
* WARNING: The memory referenced by @iov is not accessed by the kernel
* during this call. The memory is first accessed when kcapi_cipher_stream_op()
* is called. Thus, you MUST make sure that the referenced memory is still
* present at the time kcapi_cipher_stream_op() is called.
*
* @return number of bytes sent to the kernel upon success;
* a negative errno-style error code if an error occurred
*/
int32_t kcapi_akcipher_stream_update(struct kcapi_handle *handle,
struct iovec *iov, uint32_t iovlen);
/**
* kcapi_akcipher_stream_update_last() - send last data for processing (stream)
*
* @handle: [in] cipher handle
* @iov: [in] scatter/gather list with data to be processed by the cipher
* operation.
* @iovlen: [in] number of scatter/gather list elements.
*
* Using this function call, more data can be submitted to the kernel.
*
* This call is identical to the kcapi_akcipher_stream_update() call with the
* exception that it marks the last data buffer before the cipher operation
* is triggered.
*
* This call must be used if all data is delivered to the kernel and
* kcapi_akcipher_stream_op() will be invoked as a next step. This call
* notifies the kernel that no further data is to be expected.
*
* WARNING: The memory referenced by @iov is not accessed by the kernel
* during this call. The memory is first accessed when kcapi_cipher_stream_op()
* is called. Thus, you MUST make sure that the referenced memory is still
* present at the time kcapi_cipher_stream_op() is called.
*
* @return number of bytes sent to the kernel upon success;
* a negative errno-style error code if an error occurred
*/
int32_t kcapi_akcipher_stream_update_last(struct kcapi_handle *handle,
struct iovec *iov, uint32_t iovlen);
/**
* kcapi_akcipher_stream_op() - obtain processed data (stream)
*
* @handle: [in] cipher handle
* @iov: [in/out] scatter/gather list pointing to buffers to be filled
* with the resulting data from a cipher operation.
* @iovlen: [in] number of scatter/gather list elements.
*
* This call can be called interleaved with kcapi_akcipher_stream_update() to
* fetch the processed data.
*
* This function may cause the caller to sleep if the kernel buffer holding
* the data is empty. The process will be woken up once more data is sent
* by calling kcapi_cipher_stream_update().
*
* Note, when supplying buffers that are not multiple of block size, the buffers
* will only be filled up to the maximum number of full block sizes that fit
* into the buffer.
*
* @return number of bytes obtained from the kernel upon success;
* a negative errno-style error code if an error occurred
*/
int32_t kcapi_akcipher_stream_op(struct kcapi_handle *handle,
struct iovec *iov, uint32_t iovlen);
/**
* DOC: Key-Agreement Protocol Primitives
*
* API function calls used to invoke Diffie-Hellmand or EC-Diffie-Hellman
* operations.
*/
/**
* kcapi_kpp_init() - initialize cipher handle
*
* @handle: [out] cipher handle filled during the call
* @ciphername: [in] kernel crypto API cipher name as specified in
* /proc/crypto
* @flags: [in] flags specifying the type of cipher handle
*
* This function provides the initialization of a KPP cipher handle and
* establishes the connection to the kernel.
*
* On success, a pointer to kcapi_handle object is returned in *handle.
* Function kcapi_kpp_destroy should be called afterwards to free
* resources.
*
* @return 0 upon success;
* -ENOENT - algorithm not available;
* -EOPNOTSUPP - AF_ALG family not available;
* -EINVAL - accept syscall failed
* -ENOMEM - cipher handle cannot be allocated
*/
int kcapi_kpp_init(struct kcapi_handle **handle, const char *ciphername,
uint32_t flags);
/**
* kcapi_kpp_destroy() - close the cipher handle and release resources
*
* @handle: [in] cipher handle to release
*/
void kcapi_kpp_destroy(struct kcapi_handle *handle);
/**
* kcapi_kpp_dh_setparam_pkcs3 - set the PG parameters using PKCS3 format
*
* @handle: [in] cipher handle
* @pkcs3: [in] parameter buffer in DER format
* @pkcs3len: [in] length of key buffer
*
* With this function, the caller sets the PG parameters for subsequent cipher
* operations.
*
* The parameter set must be in DER format as follows
*
* SEQUENCE {
* prime INTEGER ({ dh_get_p }),
* base INTEGER ({ dh_get_g })
*}
*
* The following command generates such parameter set where the output
* file content is has the correct DER structure:
*
* openssl dhparam -outform DER -out dhparam.der 2048
*
* Note, this function defines that the subsequent key generation and
* shared secret operation performs an FFC Diffie-Hellman operation.
*
* After the caller provided the key, the caller may destroy the parameter
* as it is now maintained by the kernel.
*
* @return upon success the value of the maximum size for the KPP
* operation is returned (e.g. the prime size);
* a negative errno-style error code if an error occurred
*/
int kcapi_kpp_dh_setparam_pkcs3(struct kcapi_handle *handle,
const uint8_t *pkcs3, uint32_t pkcs3len);
/* ECC curve IDs */
#define ECC_CURVE_NIST_P192 0x0001
#define ECC_CURVE_NIST_P256 0x0002
/**
* kcapi_kpp_ecdh_setcurve - set the ECC curve to be used for ECDH
*
* @handle: [in] cipher handle
* @curve_id: [in] ID of the ECC curve
*
* With this function, the caller sets the ECC curve for subsequent cipher
* operations. The curve ID is one of the ECC_CURVE_* identifiers.
*
* Note, this function defines that the subsequent key generation and
* shared secret operation performs an ECC Diffie-Hellman operation.
*
* @return 0 upon success;
* a negative errno-style error code if an error occurred
*/
int kcapi_kpp_ecdh_setcurve(struct kcapi_handle *handle,
unsigned long curve_id);
/**
* kcapi_kpp_setkey - set the private key of the DH / ECDH operation
*
* @handle: [in] cipher handle
* @key: [in] key buffer
* @keylen: [in] length of key buffer
*
* With this function, the caller sets the key for subsequent DH / ECDH
* public key generation or shared secret generation operations.
*
* If the key / keylen is zero, the kernel tries to generate the private key
* itself and retains it internally. This is useful if the DH / ECDH operation
* shall be performed on ephemeral keys where the caller is only interested
* in eventually obtain the shared secret.
*
* After the caller provided the key, the caller may securely destroy the key
* as it is now maintained by the kernel.
*
* Note, the key can only be set after the DH parameters or the ECC curve
* has been set.
*
* @return in case of success a positive integer is returned that denominates
* the maximum output size of the cryptographic operation -- this value
* must be used as the size of the output buffer for one cryptographic
* operation);
* a negative errno-style error code if an error occurred -- the error
* -EOPNOTSUPP is returned in case a kernel-triggered private
* key generation is requested, but the underlying cipher implementation
* does not support this operation.
*/
int kcapi_kpp_setkey(struct kcapi_handle *handle,
const uint8_t *key, uint32_t keylen);
/**
* kcapi_kpp_keygen - generate a public key
*
* @handle: [in] cipher handle
* @pubkey: [out] generated public key
* @pubkeylen: [in] length of key buffer
* @access: [in] kernel access type (KCAPI_ACCESS_HEURISTIC - use internal
* heuristic for fastest kernel access; KCAPI_ACCESS_VMSPLICE - use
* vmsplice access; KCAPI_ACCESS_SENDMSG - sendmsg access)
*
* @return number of bytes returned by the key generation operation upon
* success; a negative errno-style error code if an error occurred
*/
int32_t kcapi_kpp_keygen(struct kcapi_handle *handle,
uint8_t *pubkey, uint32_t pubkeylen, int access);
/**
* kcapi_kpp_ssgen - generate a shared secret
*
* @handle: [in] cipher handle
* @pubkey: [in] public key of peer that shall be used to generate the shared
* secret with
* @pubkeylen: [in] length of the public key buffer
* @ss: [out] generated shared secret
* @sslen: [in] length of key buffer
* @access: [in] kernel access type (KCAPI_ACCESS_HEURISTIC - use internal
* heuristic for fastest kernel access; KCAPI_ACCESS_VMSPLICE - use
* vmsplice access; KCAPI_ACCESS_SENDMSG - sendmsg access)
*
* @return number of bytes returned by the shared secret generation operation
* upon success; a negative errno-style error code if an error occurred
*/
int32_t kcapi_kpp_ssgen(struct kcapi_handle *handle,
const uint8_t *pubkey, uint32_t pubkeylen,
uint8_t *ss, uint32_t sslen, int access);
/**
* kcapi_kpp_keygen_aio() - generate a public key (asynchronous one shot)
*
* @handle: [in] cipher handle
* @outiov: [out] head of scatter-gather list of the destination buffers filled
* with the generated public key
* @iovlen: [in] number of scatter-gather list entries
* @access: [in] kernel access type (KCAPI_ACCESS_HEURISTIC - use internal
* heuristic for fastest kernel access; KCAPI_ACCESS_VMSPLICE - use
* vmsplice access; KCAPI_ACCESS_SENDMSG - sendmsg access)
*
* The individual scatter-gather list entries are processed with
* separate invocations of the the given cipher.
*
* The memory should be aligned at the page boundary using
* posix_memalign(sysconf(_SC_PAGESIZE)), If it is not aligned at the page
* boundary, the vmsplice call may not send all data to the kernel.
*
* @return number of bytes verify upon success;
* a negative errno-style error code if an error occurred
*/
int32_t kcapi_kpp_keygen_aio(struct kcapi_handle *handle, struct iovec *outiov,
uint32_t iovlen, int access);
/**
* kcapi_kpp_ssgen_aio() - generate a shared secret (asynchronous one shot)
*
* @handle: [in] cipher handle
* @iniov: [in] head of scatter-gather list of the source buffers with the
* public keys of the peer
* @outiov: [out] head of scatter-gather list of the destination buffers filled
* with the generated shared secret
* @iovlen: [in] number of scatter-gather list entries
* @access: [in] kernel access type (KCAPI_ACCESS_HEURISTIC - use internal
* heuristic for fastest kernel access; KCAPI_ACCESS_VMSPLICE - use
* vmsplice access; KCAPI_ACCESS_SENDMSG - sendmsg access)
*
* The individual scatter-gather list entries are processed with
* separate invocations of the the given cipher.
*
* The memory should be aligned at the page boundary using
* posix_memalign(sysconf(_SC_PAGESIZE)), If it is not aligned at the page
* boundary, the vmsplice call may not send all data to the kernel.
*
* @return number of bytes verify upon success;
* a negative errno-style error code if an error occurred
*/
int32_t kcapi_kpp_ssgen_aio(struct kcapi_handle *handle,
struct iovec *iniov, struct iovec *outiov,
uint32_t iovlen, int access);
/**
* DOC: Key Derivation Functions
*
* API function calls used to invoke a KDF.
* The KDF functions are based on a message digest or keyed message digest
* function. The caller must have the handle allocated with kcapi_md_init.
* If the caller wishes to use a keyed message digest, the caller must invoke
* kcapi_md_setkey before those functions.
*/
/**
* kcapi_kdf_dpi() - Double Pipeline Mode Key Derivation Function
*
* @handle: [in] cipher handle allocated by caller. This cipher handle
* must be allocated with kcapi_md_init(). If the caller is interested in
* a KDF using a keyed message digest, the caller should also call
* kcapi_md_setkey() before invoking this function.
* @src: [in] Input data that should be transformed into a key (see below).
* @slen: [in] Length of the src input data.
* @dst: [out] Buffer to store the generated key in,
* @dlen: [in] Length of the dst buffer. This value defines the number of bytes
* generated by the KDF.
*
* This function is an implementation of the KDF in double pipeline iteration
* mode according with counter to SP800-108 section 5.3.
*
* The caller must provide Label || 0x00 || Context in src. This src pointer
* may also be NULL if the caller wishes not to provide anything.
*
* @return 0 upon success;
* a negative errno-style error code if an error occurred
*/
int32_t kcapi_kdf_dpi(struct kcapi_handle *handle,
const uint8_t *src, uint32_t slen,
uint8_t *dst, uint32_t dlen);
/**
* kcapi_kdf_fb() - Feedback Mode Key Derivation Function
*
* @handle: [in] cipher handle allocated by caller. This cipher handle
* must be allocated with kcapi_md_init(). If the caller is interested in
* a KDF using a keyed message digest, the caller should also call
* kcapi_md_setkey() before invoking this function.
* @src: [in] Input data that should be transformed into a key (see below).
* @slen: [in] Length of the src input data.
* @dst: [out] Buffer to store the generated key in,
* @dlen: [in] Length of the dst buffer. This value defines the number of bytes
* generated by the KDF.
*
* This function is an implementation of the KDF in feedback mode with a
* non-NULL IV and with counter according to SP800-108 section 5.2. The IV is
* supplied with src and must be equal to the digestsize of the used cipher.
*
* In addition, the caller must provide Label || 0x00 || Context in src. This
* src pointer must not be NULL as the IV is required. The ultimate format of
* the src pointer is IV || Label || 0x00 || Context where the length of the
* IV is equal to the block size (i.e. the digest size of the underlying
* hash) of the PRF.
*
* @return 0 upon success;
* a negative errno-style error code if an error occurred
*/
int32_t kcapi_kdf_fb(struct kcapi_handle *handle,
const uint8_t *src, uint32_t slen,
uint8_t *dst, uint32_t dlen);
/**
* kcapi_kdf_ctr() - Counter Mode Key Derivation Function
*
* @handle: [in] cipher handle allocated by caller. This cipher handle
* must be allocated with kcapi_md_init(). If the caller is interested in
* a KDF using a keyed message digest, the caller should also call
* kcapi_md_setkey() before invoking this function.
* @src: [in] Input data that should be transformed into a key (see below).
* @slen: [in] Length of the src input data.
* @dst: [out] Buffer to store the generated key in,
* @dlen: [in] Length of the dst buffer. This value defines the number of bytes
* generated by the KDF.
*
* This function is an implementation of the KDF in counter mode according to
* SP800-108 section 5.1 as well as SP800-56A section 5.8.1 (Single-step KDF).
*
* SP800-108:
* The caller must provide Label || 0x00 || Context in src. This src pointer
* may also be NULL if the caller wishes not to provide anything.
*
* SP800-56A:
* If a keyed MAC is used, the key shall NOT be the shared secret from the DH
* operation, but an independently generated key. The src pointer is defined
* as Z || other info where Z is the shared secret from DH and other info is an
* arbitrary string (see SP800-56A section 5.8.1.2).
*
* @return 0 upon success;
* a negative errno-style error code if an error occurred
*/
int32_t kcapi_kdf_ctr(struct kcapi_handle *handle,
const uint8_t *src, uint32_t slen,
uint8_t *dst, uint32_t dlen);
/**
* kcapi_pbkdf() - Password-based Key Derivation Function
*
* @hashname: [in] kernel crypto API name of a keyed hash (e.g. hmac(sha1))
* @pw: [in] Password a key shall be derived from
* @pwlen: [in] Length of password string
* @salt: [in] Salt as defined in SP800-132
* @saltlen: [in] Length of salt buffer
* @count: [in] Numbers of iterations to be performed for the PBKDF
* @key: [out] Buffer to store the generated key in
* @keylen: [in] Size of the key to be generated (i.e. length of the key buffer)
*
* This function is an implementation of the PBKDF as defined in SP800-132.
*
* @return 0 upon success;
* a negative errno-style error code if an error occurred
*/
int32_t kcapi_pbkdf(const char *hashname,
const uint8_t *pw, uint32_t pwlen,
const uint8_t *salt, uint32_t saltlen,
uint32_t count,
uint8_t *key, uint32_t keylen);
/**
* kcapi_pbkdf_iteration_count() - Calculate numbers of iterations for a PBKDF
*
* @hashname: [in] kernel crypto API name of a keyed hash (e.g. hmac(sha1))
* @timeshresh: [in] Time duration in nanoseconds that the PBKDF operation
* shall at least require. If that value is 0, a default of (1<<27)
* nanoseconds is used.
*
* The function measures the time the PBKDF operation takes for different
* round counts for the given keyed message digest type.
*
* The result should be taken as the iteration count for a PBKDF operation.
*
* If an error occurs with the PBKDF calculation, a value of 1<<18 is returned.
*
* @return number of iterations a PBKDF should take on this computer.
*/
uint32_t kcapi_pbkdf_iteration_count(const char *hashname, uint64_t timeshresh);
/**
* kcapi_hkdf() - Extract-and-Expand HKDF (RFC5869)
*
* @hashname: [in] kernel crypto API name of a keyed hash (e.g. hmac(sha1))
* @ikm: [in] Input Keying Material (IKM) -- must be provided
* @ikmlen: [in] IKM buffer length -- must be non-zero
* @salt: [in] salt buffer -- may be NULL
* @saltlen: [in] salt buffer length -- may be zero
* @info: [in] info buffer -- may be NULL
* @infolen: [in] info buffer length -- may be zero
* @dst: [out] Buffer to store the generated key in,
* @dlen: [in] Length of the dst buffer. This value defines the number of bytes
* generated by the KDF.
*
* Perform the key-derivation function according to RFC5869. The input data
* is defined in sections 2.2 und 2.3 of RFC5869.
*
* @return 0 upon success;
* a negative errno-style error code if an error occurred
*/
int32_t kcapi_hkdf(const char *hashname,
const uint8_t *ikm, uint32_t ikmlen,
const uint8_t *salt, uint32_t saltlen,
const uint8_t *info, uint32_t infolen,
uint8_t *dst, uint32_t dlen);
#ifdef __cplusplus
}
#endif
#endif /* KCAPI_H */
|