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 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894
|
/* This program is released under the Common Public License V1.0
*
* You should have received a copy of Common Public License V1.0 along with
* with this program.
*/
/*
* Authors(s): Ralph Wuerthner <rwuerthn@de.ibm.com>
* Holger Dengler <hd@linux.vnet.ibm.com>
* Ingo Tuchscherer <ingo.tuchscherer@linux.vnet.ibm.com>
*
* Copyright IBM Corp. 2001, 2005, 2009, 2010, 2011, 2013
*/
#ifndef __ICA_API_H__
#define __ICA_API_H__
/***************************************************************************
*** ***
*** LICENSED MATERIALS - PROPERTY OF IBM ***
*** ***
*** All Rights Reserved ***
*** ***
*** U.S. Government Users Restricted Rights - Use, ***
*** duplication or disclosure restricted by GSA ADP ***
*** Schedule Contract with IBM Corp. ***
*** ***
*** ***
*** ORIGINS: IBM Charlotte, Department VM9A ***
*** ***
***************************************************************************/
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#define ICA_EXPORT __attribute__((__visibility__("default")))
#define ICA_DEPRECATED __attribute__((deprecated))
#define ica_adapter_handle_t int
typedef ica_adapter_handle_t ICA_ADAPTER_HANDLE;
#define DRIVER_NOT_LOADED (-1)
/**
* Definitions to determine the direction of the symmetric
* encryption/decryption functions.
*/
#define ICA_ENCRYPT 1
#define ICA_DECRYPT 0
/**
* Symetric encryption/decryption modes
*/
#define MODE_ECB 1
#define MODE_CBC 2
#define MODE_CFB 3
#define MODE_OFB 4
#define MODE_CTR 5
#define MODE_XTS 6
#define MODE_GCM 7
#define MODE_CBCCS 8
#define MODE_CCM 9
/**
* CBC Ciphertext Stealing variants
*/
#define ICA_CBCCS_VARIANT1 1
#define ICA_CBCCS_VARIANT2 2
#define ICA_CBCCS_VARIANT3 3
/**
* ICA flags
*/
#define ICA_FLAG_SHW 4 /* static hardware support (symmetric ops - CPACF) */
#define ICA_FLAG_DHW 2 /* dynamic hardware support (asymmetric ops - CEX) */
#define ICA_FLAG_SW 1 /* software implementation (fallback / backup) */
/**
* ICA properties: key lengths
*/
#define ICA_PROPERTY_AES_128 0x00000001
#define ICA_PROPERTY_AES_192 0x00000002
#define ICA_PROPERTY_AES_256 0x00000004
#define ICA_PROPERTY_RSA_ALL 0x0000000F /* All RSA key lengths */
#define ICA_PROPERTY_RSA_FIPS 0x0000000C /* RSA 2k and higher */
#define ICA_PROPERTY_EC_BP 0x00000001 /* Brainpool curves */
#define ICA_PROPERTY_EC_NIST 0x00000002 /* NIST curves */
#define ICA_PROPERTY_EC_ED 0x00000004 /* Edwards curves */
/**
* Algorithms
*/
#define SHA1 1
#define SHA224 2
#define SHA256 3
#define SHA384 4
#define SHA512 5
#define SHA3_224 6
#define SHA3_256 7
#define SHA3_384 8
#define SHA3_512 9
#define SHAKE128 11
#define SHAKE256 12
#define G_HASH 10
#define DES_ECB 20
#define DES_CBC 21
#define DES_CBC_CS 22
#define DES_OFB 23
#define DES_CFB 24
#define DES_CTR 25
#define DES_CTRLST 26
#define DES_CBC_MAC 27
#define DES_CMAC 28
#define DES3_ECB 41
#define DES3_CBC 42
#define DES3_CBC_CS 43
#define DES3_OFB 44
#define DES3_CFB 45
#define DES3_CTR 46
#define DES3_CTRLST 47
#define DES3_CBC_MAC 48
#define DES3_CMAC 49
#define AES_ECB 60
#define AES_CBC 61
#define AES_CBC_CS 62
#define AES_OFB 63
#define AES_CFB 64
#define AES_CTR 65
#define AES_CTRLST 66
#define AES_CBC_MAC 67
#define AES_CMAC 68
#define AES_CCM 69
#define AES_GCM 70
#define AES_XTS 71
#define AES_GCM_KMA 72
#define P_RNG 80
#define EC_DH 85
#define EC_DSA_SIGN 86
#define EC_DSA_VERIFY 87
#define EC_KGEN 88
#define RSA_ME 90
#define RSA_CRT 91
#define RSA_KEY_GEN_ME 92
#define RSA_KEY_GEN_CRT 93
#define SHA512_DRNG 94
#define SHA512_224 95
#define SHA512_256 96
#define ED25519_KEYGEN 100
#define ED25519_SIGN 101
#define ED25519_VERIFY 102
#define ED448_KEYGEN 103
#define ED448_SIGN 104
#define ED448_VERIFY 105
#define X25519_KEYGEN 106
#define X25519_DERIVE 107
#define X448_KEYGEN 108
#define X448_DERIVE 109
/*
* Key length for DES/3DES encryption/decryption
*/
#define DES_KEY_LENGTH (56/8)
#define DES3_KEY_LENGTH (168/8)
/**
* Key length for AES encryption/decryption
*/
#define AES_KEY_LEN128 (128/8)
#define AES_KEY_LEN192 (192/8)
#define AES_KEY_LEN256 (256/8)
/**
* SHA Message parts
*/
#define SHA_MSG_PART_ONLY 0
#define SHA_MSG_PART_FIRST 1
#define SHA_MSG_PART_MIDDLE 2
#define SHA_MSG_PART_FINAL 3
/**
* SHA hash lengths
*/
#define SHA_HASH_LENGTH 20
#define SHA1_HASH_LENGTH SHA_HASH_LENGTH
#define SHA224_HASH_LENGTH 28
#define SHA256_HASH_LENGTH 32
#define SHA384_HASH_LENGTH 48
#define SHA512_HASH_LENGTH 64
#define SHA512_224_HASH_LENGTH SHA224_HASH_LENGTH
#define SHA512_256_HASH_LENGTH SHA256_HASH_LENGTH
#define SHA3_224_HASH_LENGTH SHA224_HASH_LENGTH
#define SHA3_256_HASH_LENGTH SHA256_HASH_LENGTH
#define SHA3_384_HASH_LENGTH SHA384_HASH_LENGTH
#define SHA3_512_HASH_LENGTH SHA512_HASH_LENGTH
#define SHA3_PARMBLOCK_LENGTH 200
/*
* ica_drbg
*/
#define ICA_DRBG_NEW_STATE_HANDLE NULL
#define ICA_DRBG_HEALTH_TEST_FAIL (-1)
#define ICA_DRBG_ENTROPY_SOURCE_FAIL (-2)
/*
* The following status flags are used to examine the return value of the
* status output interface ica_fips_status().
*/
/*
* 'FIPS mode active'-flag
*/
#define ICA_FIPS_MODE 1
/*
* 'Powerup test failed'-flags
*/
/* Cryptographic algorithm test (KAT or pair-wise consistency test) */
#define ICA_FIPS_CRYPTOALG 2
/* Software/Firmware integrity test */
#define ICA_FIPS_INTEGRITY 4
/* Critical functions test (N/A) */
#define ICA_FIPS_CRITICALFUNC 8
/*
* 'Conditional test failed'-flags
*/
/* Pair-wise consistency test for public & private keys (N/A) */
#define ICA_FIPS_CONSISTENCY 16
/* Software/Firmware load test (N/A) */
#define ICA_FIPS_LOAD 32
/* Manual key entry test (N/A) */
#define ICA_FIPS_KEYENTRY 64
/* Continuous random number generator test */
#define ICA_FIPS_RNG 128
/* Bypass test (N/A) */
#define ICA_FIPS_BYPASS 256
/**
* Context for SHA1 operations
*/
typedef struct {
uint64_t runningLength;
unsigned char shaHash[SHA_HASH_LENGTH];
} sha_context_t;
/**
* Context for SHA256 and SHA224 operations
*/
typedef struct {
uint64_t runningLength;
unsigned char sha256Hash[SHA256_HASH_LENGTH];
} sha256_context_t;
/**
* Context for SHA512 and SHA384 operations
*/
typedef struct {
uint64_t runningLengthHigh;
uint64_t runningLengthLow;
unsigned char sha512Hash[SHA512_HASH_LENGTH];
} sha512_context_t;
/**
* Context for SHA3_224 operations
*/
typedef struct {
uint64_t runningLength;
unsigned char sha3_224Hash[SHA3_PARMBLOCK_LENGTH];
} sha3_224_context_t;
/**
* Context for SHA3_256 operations
*/
typedef struct {
uint64_t runningLength;
unsigned char sha3_256Hash[SHA3_PARMBLOCK_LENGTH];
} sha3_256_context_t;
/**
* Context for SHA3_384 operations
*/
typedef struct {
uint64_t runningLengthHigh;
uint64_t runningLengthLow;
unsigned char sha3_384Hash[SHA3_PARMBLOCK_LENGTH];
} sha3_384_context_t;
/**
* Context for SHA3_512 operations
*/
typedef struct {
uint64_t runningLengthHigh;
uint64_t runningLengthLow;
unsigned char sha3_512Hash[SHA3_PARMBLOCK_LENGTH];
} sha3_512_context_t;
/**
* Context for SHAKE_128 operations with variable output length
*/
typedef struct {
uint64_t runningLengthHigh;
uint64_t runningLengthLow;
unsigned int output_length;
unsigned char shake_128Hash[SHA3_PARMBLOCK_LENGTH];
} shake_128_context_t;
/**
* Context for SHAKE_256 operations with variable output length
*/
typedef struct {
uint64_t runningLengthHigh;
uint64_t runningLengthLow;
unsigned int output_length;
unsigned char shake_256Hash[SHA3_PARMBLOCK_LENGTH];
} shake_256_context_t;
/*
* Assumption: *_ENCRYPT members of the kmc_funktion_t and kma_function_t
* enums are even, while *_DECRYPT members are odd.
*/
typedef enum {
DEA_ENCRYPT,
DEA_DECRYPT,
TDEA_192_ENCRYPT,
TDEA_192_DECRYPT,
AES_128_ENCRYPT,
AES_128_DECRYPT,
AES_192_ENCRYPT,
AES_192_DECRYPT,
AES_256_ENCRYPT,
AES_256_DECRYPT,
/* XTS belongs to the KM family */
AES_128_XTS_ENCRYPT,
AES_128_XTS_DECRYPT,
AES_256_XTS_ENCRYPT,
AES_256_XTS_DECRYPT,
/* PRNG only for KMC */
PRNG,
} kmc_functions_t;
typedef enum {
AES_128_GCM_ENCRYPT,
AES_128_GCM_DECRYPT,
AES_192_GCM_ENCRYPT,
AES_192_GCM_DECRYPT,
AES_256_GCM_ENCRYPT,
AES_256_GCM_DECRYPT,
} kma_functions_t;
typedef enum {
ECDSA_VERIFY_P256,
ECDSA_VERIFY_P384,
ECDSA_VERIFY_P521,
ECDSA_SIGN_P256,
ECDSA_SIGN_P384,
ECDSA_SIGN_P521,
EDDSA_VERIFY_ED25519,
EDDSA_VERIFY_ED448,
EDDSA_SIGN_ED25519,
EDDSA_SIGN_ED448,
} kdsa_functions_t;
typedef struct {
unsigned int key_length;
unsigned char* modulus;
unsigned char* exponent;
} ica_rsa_key_mod_expo_t;
typedef struct {
unsigned int key_length;
unsigned char* p;
unsigned char* q;
unsigned char* dp;
unsigned char* dq;
unsigned char* qInverse;
} ica_rsa_key_crt_t;
/**
* DES and AES defines and typedefs
*/
typedef unsigned char ica_des_vector_t[8];
typedef unsigned char ica_des_key_single_t[8];
typedef struct {
ica_des_key_single_t key1;
ica_des_key_single_t key2;
ica_des_key_single_t key3;
} ica_des_key_triple_t;
typedef unsigned char ica_key_t[8];
/**
* AES defines and typedefs
*/
typedef unsigned char ica_aes_vector_t[16];
typedef unsigned char ica_aes_key_single_t[8];
typedef unsigned char ica_aes_key_len_128_t[16];
typedef unsigned char ica_aes_key_len_192_t[24];
typedef unsigned char ica_aes_key_len_256_t[32];
/**
* Libica version information
*/
typedef struct {
unsigned int major_version;
unsigned int minor_version;
unsigned int fixpack_version;
} libica_version_info;
/**
* Definition of a mechanism type
**/
typedef unsigned int libica_mechanism_type;
/**
* Information for a particular crypto mechanism supported by libica.
* Key sizes are specified in bytes and do not apply to all supported
* mechanisms.
**/
typedef struct {
unsigned int min_key_size;
unsigned int max_key_size;
unsigned int flags;
} libica_mechanism_info;
/**
* Definition for a particular crypto mechanism supported by libica.
**/
typedef struct {
libica_mechanism_type mech_type;
libica_mechanism_info mech_info;
} libica_mechanism_list_element;
/*
* internal specification for a specific crypto mechanism supported by libica
**/
typedef struct {
unsigned int mech_mode_id;
unsigned int type;
unsigned int id;
unsigned int flags;
unsigned int property;
} libica_func_list_element_int;
/*
* external specification for a specific crypto mechanism supported by libica
**/
typedef struct {
unsigned int mech_mode_id;
unsigned int flags;
unsigned int property;
} libica_func_list_element;
typedef struct ica_drbg_mech ica_drbg_mech_t;
typedef struct ica_drbg ica_drbg_t;
/**
* Definitions for the ica_set_fallback_mode function.
*/
#define ICA_FALLBACKS_ENABLED 1
#define ICA_FALLBACKS_DISABLED 0
/**
* Environment variable for defining the default Libica fallback mode.
* By default Libica starts with fallbacks enabled. When this environment
* variable exists and has a numeric value, the fallback mode is set
* via ica_set_fallback_mode().
*/
#define ICA_FALLBACK_ENV "LIBICA_FALLBACK_MODE"
/**
* Set Libica fallback mode.
* With fallbacks enabled (that's the default), when there is no hardware
* support available (for example when the crypto cards are offline) Libica
* attempts to cover the request by calling Openssl functions as fallback.
* With fallback disabled, no attempts will be made to fulfill the request
* if there is no hardware support or hardware invocation fails. Instead
* the function will return with ENODEV.
*/
ICA_EXPORT
void ica_set_fallback_mode(int fallback_mode);
/**
* Environment variable for setting libica offload mode.
* By default libica may prefer to do crypto in cpacf instead of adapters.
* If this environment variable is defined to be an integer not equal to zero,
* adapters will always be preferred.
*/
#define ICA_OFFLOAD_ENV "LIBICA_OFFLOAD_MODE"
/**
* Set libica offload mode.
* By default libica may prefer to do crypto in cpacf instead of adapters.
* If this function is called with offload_mode != 0, adapters will always
* be preferred.
*/
ICA_EXPORT
void ica_set_offload_mode(int offload_mode);
/**
* Environment variable for setting libica stats mode.
* By default libica counts its crypto operations in shared memory.
* If this environment variable is defined to be zero, libica will not
* count crypto operations.
*/
#define ICA_STATS_ENV "LIBICA_STATS_MODE"
/**
* Set libica stats mode.
* By default libica counts its crypto operations in shared memory.
* If this function is called with stats_mode = 0, libica will not
* count crypto operations.
*/
ICA_EXPORT
void ica_set_stats_mode(int stats_mode);
/**
* Opens the specified adapter
* @param adapter_handle Pointer to the file descriptor for the adapter or
* to DRIVER_NOT_LOADED if opening the crypto adapter failed.
*
* @return 0 as long as a valid parameter is given.
* EINVAL for invalid parameter.
*/
ICA_EXPORT
unsigned int ica_open_adapter(ica_adapter_handle_t *adapter_handle);
/**
* Closes a device handle.
* @param adapter_handle Pointer to a previously opened device handle.
*
* @return 0 if successful.
* errno of close() if unsuccessful
*/
ICA_EXPORT
unsigned int ica_close_adapter(ica_adapter_handle_t adapter_handle);
/**
* Generate a random number.
*
* Required HW Support
* KMC-PRNG
*
* @param output_length
* Specifies the byte length of the output_data buffer and the desired length
* of the random number.
* @param output_data
* Pointer to the buffer to contain the resulting random number.
*
* @return 0 if successful.
* EINVAL if at least one invalid parameter is given
* ENODEV if neither /dev/hwrng nor /dev/urandom are available.
* EIO if the operation fails. This should never happen.
*/
ICA_EXPORT
unsigned int ica_random_number_generate(unsigned int output_length,
unsigned char *output_data);
/**
* Perform secure hash on input data using the SHA-1 algorithm.
*
* Required HW Support
* KIMD-SHA-1, or KLMD-SHA-1
*
* @param message_part
* The message chaining state. Must be one of the following:
* SHA_MSG_PART_ONLY - A single hash operation
* SHA_MSG_PART_FIRST - The first part
* SHA_MSG_PART_MIDDLE - The middle part
* SHA_MSG_PART_FINAL - The last part
* @param input_length
* The byte length of the input data to be SHA-1 hashed and must be greater
* than zero.
* Note: For SHA_MSG_PART_FIRST and SHA_MSG_PART_MIDDLE calls, the byte length
* must be a multiple of 64 i.e., SHA-1 block size.
* @param input_data
* Pointer to the input data data.
* @param sha_context
* Pointer to the SHA-1 context structure used to store intermediate values
* needed when chaining is used. The contents are ignored for message part
* SHA_MSG_PART_ONLY and SHA_MSG_PART_FIRST. This structure must
* contain the returned value of the preceding call to ica_sha1 for message
* part SHA_MSG_PART_MIDDLE and SHA_MSG_PART_FINAL. For message part
* SHA_MSG_PART_FIRST and SHA_MSG_PART_FINAL, the returned value can
* be used for a chained call of ica_sha1. Therefore, the application must
* not modify the contents of this structure in between chained calls.
* @param output_data
* Pointer to the buffer to contain the resulting hash data. The resulting
* output data will have a length of SHA_HASH_LENGTH. Make sure buffer has
* at least this size.
*
* @return 0 if successful.
* EINVAL if at least one invalid parameter is given
* EIO if the operation fails. This should never happen.
*/
ICA_EXPORT
unsigned int ica_sha1(unsigned int message_part,
unsigned int input_length,
unsigned char *input_data,
sha_context_t *sha_context,
unsigned char *output_data);
/**
* Perform secure hash on input data using the SHA-224 algorithm.
*
* Required HW Support
* KIMD-SHA-256, or KLMD-SHA-256
*
* @param message_part
* The message chaining state. Must be one of the following:
* SHA_MSG_PART_ONLY - A single hash operation
* SHA_MSG_PART_FIRST - The first part
* SHA_MSG_PART_MIDDLE - The middle part
* SHA_MSG_PART_FINAL - The last part
* @param input_length
* The byte length of the input data to be SHA-224 hashed and must be greater
* than zero.
* Note: For SHA_MSG_PART_FIRST and SHA_MSG_PART_MIDDLE calls, the byte length
* must be a multiple of 64 i.e., SHA-224 block size.
* @param input_data
* Pointer to the input data.
* @param sha256_context
* Pointer to the SHA-256 context structure used to store intermediate values
* needed when chaining is used. The contents are ignored for message part
* SHA_MSG_PART_ONLY and SHA_MSG_PART_FIRST. This structure must
* contain the returned value of the preceding call to ica_sha224 for message
* part SHA_MSG_PART_MIDDLE and SHA_MSG_PART_FINAL. For message part
* SHA_MSG_PART_FIRST and SHA_MSG_PART_FINAL, the returned value can
* be used for a chained call of ica_sha224. Therefore, the application must
* not modify the contents of this structure in between chained calls.
* Note: Due to the algorithm used by SHA-224, a SHA-256 context must be
* used.
* @param output_data
* Pointer to the buffer to contain the resulting hash data. The resulting
* output data will have a length of SHA224_HASH_LENGTH. Make sure buffer has
* at least this size.
*
* @return 0 if successful.
* EINVAL if at least one invalid parameter is given
* EIO if the operation fails. This should never happen.
*/
ICA_EXPORT
unsigned int ica_sha224(unsigned int message_part,
unsigned int input_length,
unsigned char *input_data,
sha256_context_t *sha256_context,
unsigned char *output_data);
/**
* Perform secure hash on input data using the SHA-256 algorithm.
*
* Required HW Support
* KIMD-SHA-256, or KLMD-SHA-256
*
* @param message_part
* The message chaining state. Must be one of the following:
* SHA_MSG_PART_ONLY - A single hash operation
* SHA_MSG_PART_FIRST - The first part
* SHA_MSG_PART_MIDDLE - The middle part
* SHA_MSG_PART_FINAL - The last part
* @param input_length
* The byte length of the input data to be SHA-256 hashed and must be greater
* than zero.
* Note: For SHA_MSG_PART_FIRST and SHA_MSG_PART_MIDDLE calls, the byte length
* must be a multiple of 64 i.e., SHA-256 block size.
* @param input_data
* Pointer to the input data.
* @param sha256_context
* Pointer to the SHA-256 context structure used to store intermediate values
* needed when chaining is used. The contents are ignored for message part
* SHA_MSG_PART_ONLY and SHA_MSG_PART_FIRST. This structure must
* contain the returned value of the preceding call to ica_sha256 for message part
* SHA_MSG_PART_MIDDLE and SHA_MSG_PART_FINAL. For message part
* SHA_MSG_PART_FIRST and SHA_MSG_PART_FINAL, the returned value can
* be used for a chained call of ica_sha256. Therefore, the application must not
* modify the contents of this structure in between chained calls.
* @param output_data
* Pointer to the buffer to contain the resulting hash data. The resulting output
* data will have a length of SHA256_HASH_LENGTH. Make sure that the buffer
* has is at least this size.
*
* @return 0 if successful.
* EINVAL if at least one invalid parameter is given
* EIO if the operation fails. This should never happen.
*/
ICA_EXPORT
unsigned int ica_sha256(unsigned int message_part,
unsigned int input_length,
unsigned char *input_data,
sha256_context_t *sha256_context,
unsigned char *output_data);
/**
* Perform secure hash on input data using the SHA-384 algorithm.
*
* Required HW Support
* KIMD-SHA-512, or KLMD-SHA-512
*
* @param message_part
* The message chaining state. Must be one of the following:
* SHA_MSG_PART_ONLY - A single hash operation
* SHA_MSG_PART_FIRST - The first part
* SHA_MSG_PART_MIDDLE - The middle part
* SHA_MSG_PART_FINAL - The last part
* @param input_length
* The byte length of the input data to be SHA-384 hashed and must be greater
* than zero.
* Note: For SHA_MSG_PART_FIRST and SHA_MSG_PART_MIDDLE calls, the byte length
* must be a multiple of 128 i.e., SHA-384 block size.
* @param input_data
* Pointer to the input data.
* @param sha512_context
* Pointer to the SHA-512 context structure used to store intermediate values
* needed when chaining is used. The contents are ignored for message part
* SHA_MSG_PART_ONLY and SHA_MSG_PART_FIRST. This structure must
* contain the returned value of the preceding call to ica_sha384 for message
* part SHA_MSG_PART_MIDDLE and SHA_MSG_PART_FINAL. For message part
* SHA_MSG_PART_FIRST and SHA_MSG_PART_FINAL, the returned value can
* be used for a chained call of ica_sha384. Therefore, the application must
* not modify the contents of this structure in between chained calls.
* Note: Due to the algorithm used by SHA-384, a SHA-512 context must be
* used.
* @param output_data
* Pointer to the buffer to contain the resulting hash data. The resulting
* output data will have a length of SHA384_HASH_LENGTH. Make sure buffer has
* at least this size.
*
* @return 0 if successful.
* EINVAL if at least one invalid parameter is given
* EIO if the operation fails. This should never happen.
*/
ICA_EXPORT
unsigned int ica_sha384(unsigned int message_part,
uint64_t input_length,
unsigned char *input_data,
sha512_context_t *sha512_context,
unsigned char *output_data);
/**
* Perform secure hash on input data using the SHA-512 algorithm.
*
* Required HW Support
* KIMD-SHA-512, or KLMD-SHA-512
*
* @param message_part
* The message chaining state. Must be one of the following:
* SHA_MSG_PART_ONLY - A single hash operation
* SHA_MSG_PART_FIRST - The first part
* SHA_MSG_PART_MIDDLE - The middle part
* SHA_MSG_PART_FINAL - The last part
* @param input_length
* The byte length of the input data to be SHA-512 hashed and must be greater
* than zero.
* Note: For SHA_MSG_PART_FIRST and SHA_MSG_PART_MIDDLE calls, the byte length
* must be a multiple of 128 i.e., SHA-512 block size.
* @param input_data
* Pointer to the input data.
* @param sha512_context
* Pointer to the SHA-512 context structure used to store intermediate values
* needed when chaining is used. The contents are ignored for message part
* SHA_MSG_PART_ONLY and SHA_MSG_PART_FIRST. This structure must
* contain the returned value of the preceding call to ica_sha512 for message
* part SHA_MSG_PART_MIDDLE and SHA_MSG_PART_FINAL. For message part
* SHA_MSG_PART_FIRST and SHA_MSG_PART_FINAL, the returned value can
* be used for a chained call of ica_sha512. Therefore, the application must
* not modify the contents of this structure in between chained calls.
* @param output_data
* Pointer to the buffer to contain the resulting hash data. The resulting
* output data will have a length of SHA512_HASH_LENGTH. Make sure buffer has
* at least this size.
*
* @return 0 if successful.
* EINVAL if at least one invalid parameter is given
* EIO if the operation fails. This should never happen.
*/
ICA_EXPORT
unsigned int ica_sha512(unsigned int message_part,
uint64_t input_length,
unsigned char *input_data,
sha512_context_t *sha512_context,
unsigned char *output_data);
/**
* Perform secure hash on input data using the SHA-512/224 algorithm.
*
* Required HW Support
* KIMD-SHA-512, or KLMD-SHA-512
*
* @param message_part
* The message chaining state. Must be one of the following:
* SHA_MSG_PART_ONLY - A single hash operation
* SHA_MSG_PART_FIRST - The first part
* SHA_MSG_PART_MIDDLE - The middle part
* SHA_MSG_PART_FINAL - The last part
* @param input_length
* The byte length of the input data to be SHA-512/224 hashed and must be greater
* than zero.
* Note: For SHA_MSG_PART_FIRST and SHA_MSG_PART_MIDDLE calls, the byte length
* must be a multiple of 128 i.e., SHA-512 block size.
* @param input_data
* Pointer to the input data.
* @param sha512_context
* Pointer to the SHA-512 context structure used to store intermediate values
* needed when chaining is used. The contents are ignored for message part
* SHA_MSG_PART_ONLY and SHA_MSG_PART_FIRST. This structure must
* contain the returned value of the preceding call to ica_sha512_224 for message
* part SHA_MSG_PART_MIDDLE and SHA_MSG_PART_FINAL. For message part
* SHA_MSG_PART_FIRST and SHA_MSG_PART_FINAL, the returned value can
* be used for a chained call of ica_sha512_224. Therefore, the application must
* not modify the contents of this structure in between chained calls.
* @param output_data
* Pointer to the buffer to contain the resulting hash data. The resulting
* output data will have a length of SHA512_224_HASH_LENGTH. Make sure buffer has
* at least this size.
*
* @return 0 if successful.
* EINVAL if at least one invalid parameter is given
* EIO if the operation fails. This should never happen.
*/
ICA_EXPORT
unsigned int ica_sha512_224(unsigned int message_part,
uint64_t input_length,
unsigned char *input_data,
sha512_context_t *sha512_context,
unsigned char *output_data);
/**
* Perform secure hash on input data using the SHA-512/256 algorithm.
*
* Required HW Support
* KIMD-SHA-512, or KLMD-SHA-512
*
* @param message_part
* The message chaining state. Must be one of the following:
* SHA_MSG_PART_ONLY - A single hash operation
* SHA_MSG_PART_FIRST - The first part
* SHA_MSG_PART_MIDDLE - The middle part
* SHA_MSG_PART_FINAL - The last part
* @param input_length
* The byte length of the input data to be SHA-512/256 hashed and must be greater
* than zero.
* Note: For SHA_MSG_PART_FIRST and SHA_MSG_PART_MIDDLE calls, the byte length
* must be a multiple of 128 i.e., SHA-512 block size.
* @param input_data
* Pointer to the input data.
* @param sha512_context
* Pointer to the SHA-512 context structure used to store intermediate values
* needed when chaining is used. The contents are ignored for message part
* SHA_MSG_PART_ONLY and SHA_MSG_PART_FIRST. This structure must
* contain the returned value of the preceding call to ica_sha512_256 for message
* part SHA_MSG_PART_MIDDLE and SHA_MSG_PART_FINAL. For message part
* SHA_MSG_PART_FIRST and SHA_MSG_PART_FINAL, the returned value can
* be used for a chained call of ica_sha512_256. Therefore, the application must
* not modify the contents of this structure in between chained calls.
* @param output_data
* Pointer to the buffer to contain the resulting hash data. The resulting
* output data will have a length of SHA512_256_HASH_LENGTH. Make sure buffer has
* at least this size.
*
* @return 0 if successful.
* EINVAL if at least one invalid parameter is given
* EIO if the operation fails. This should never happen.
*/ICA_EXPORT
unsigned int ica_sha512_256(unsigned int message_part,
uint64_t input_length,
unsigned char *input_data,
sha512_context_t *sha512_context,
unsigned char *output_data);
ICA_EXPORT
unsigned int ica_sha3_224(unsigned int message_part,
unsigned int input_length,
unsigned char *input_data,
sha3_224_context_t *sha3_224_context,
unsigned char *output_data);
ICA_EXPORT
unsigned int ica_sha3_256(unsigned int message_part,
unsigned int input_length,
unsigned char *input_data,
sha3_256_context_t *sha3_256_context,
unsigned char *output_data);
ICA_EXPORT
unsigned int ica_sha3_384(unsigned int message_part,
uint64_t input_length,
unsigned char *input_data,
sha3_384_context_t *sha3_384_context,
unsigned char *output_data);
ICA_EXPORT
unsigned int ica_sha3_512(unsigned int message_part,
uint64_t input_length,
unsigned char *input_data,
sha3_512_context_t *sha3_512_context,
unsigned char *output_data);
ICA_EXPORT
unsigned int ica_shake_128(unsigned int message_part,
uint64_t input_length,
unsigned char *input_data,
shake_128_context_t *shake_128_context,
unsigned char *output_data,
unsigned int output_length);
ICA_EXPORT
unsigned int ica_shake_256(unsigned int message_part,
uint64_t input_length,
unsigned char *input_data,
shake_256_context_t *shake_256_context,
unsigned char *output_data,
unsigned int output_length);
/*******************************************************************************
*
* Begin of ECC API
*/
#ifndef NID_X25519
# define NID_X25519 1034
#endif
#ifndef NID_X448
# define NID_X448 1035
#endif
#ifndef NID_ED25519
# define NID_ED25519 1087
#endif
#ifndef NID_ED448
# define NID_ED448 1088
#endif
typedef struct ec_key_t ICA_EC_KEY;
/**
* Allocate and return a new ICA_EC_KEY structure.
*
* @param nid
* The identifier of the elliptic curve, on which the new ICA_EC_KEY
* shall be based.
*
* NID Value NID Name (OpenSSL) Elliptic Curve D Length (bytes)
* --------- ---------------------- ---------------- ----------------
* 409 NID_X9_62_prime192v secp192r1 24
* 713 NID_secp224r1 secp224r1 28
* 415 NID_X9_62_prime256v1 secp256r1 32
* 715 NID_secp384r1 secp384r1 48
* 716 NID_secp521r1 secp521r1 66
* 921 NID_brainpoolP160r1 brainpoolP160r1 20
* 923 NID_brainpoolP192r1 brainpoolP192r1 24
* 925 NID_brainpoolP224r1 brainpoolP224r1 28
* 927 NID_brainpoolP256r1 brainpoolP256r1 32
* 929 NID_brainpoolP320r1 brainpoolP320r1 40
* 931 NID_brainpoolP384r1 brainpoolP384r1 48
* 933 NID_brainpoolP512r1 brainpoolP512r1 64
* 1034 NID_X25519 X25519
* 1035 NID_X448 X448
* 1087 NID_ED25519 Ed25519 32
* 1088 NID_ED448 Ed448 57
*
* @param privlen
* A pointer to an unsigned integer buffer where the length of the
* private D-value of the ICA_EC_KEY is returned.
*
* Note: The lengths of X and Y are the same as the length of D.
* Therefore, the public key (X,Y) has twice the length of D.
* Also an ECDSA signature has twice the length of D.
*
* @return Pointer to opaque ICA_EC_KEY structure if success.
* NULL if no memory could be allocated.
*/
ICA_EXPORT
ICA_EC_KEY* ica_ec_key_new(unsigned int nid, unsigned int *privlen);
/**
* Initialize an ICA_EC_KEY with given private (D) and/or public key
* values (X,Y). D may be NULL, if no private key value shall be
* specified. X and Y may both be NULL, if no public key shall be
* specified. If X is specified, also Y must be specified, and vice
* versa.
*
* @param X
* Pointer to the public X-value that shall be assigned to the
* ICA_EC_KEY object.
*
* @param Y
* Pointer to the public Y-value that shall be assigned to the
* ICA_EC_KEY object.
*
* @param D
* Pointer to the private D-value that shall be assigned to the
* ICA_EC_KEY object.
*
* @return 0 if success
* EPERM if the EC curve is not supported in this environment
* EINVAL if at least one invalid parameter is given.
*/
ICA_EXPORT
int ica_ec_key_init(const unsigned char *X, const unsigned char *Y,
const unsigned char *D, ICA_EC_KEY *key);
/**
* Generate private and public values for a given ICA_EC_KEY.
*
* @param adapter_handle
* Pointer to a previously opened device handle.
*
* @param key
* Pointer to a previously allocated ICA_EC_KEY object.
*
* @return 0 if success
* EPERM if the EC curve is not supported in this environment
* EINVAL if at least one invalid parameter is given.
* ENOMEM if memory could not be allocated.
* EIO if an internal processing error occurred.
*/
ICA_EXPORT
int ica_ec_key_generate(ica_adapter_handle_t adapter_handle, ICA_EC_KEY *key);
/**
* Calculate the Diffie-Hellman shared secret (z-value) of a given
* private ICA_EC_KEY A (with given D-value) and a given public
* ICA_EC_KEY B (with given X and Y values).
*
* @param privkey_A
* A pointer to a private ICA_EC_KEY object.
*
* @param pubkey_B
* A pointer to a public ICA_EC_KEY object.
*
* @param z
* Pointer to a writable buffer where the shared secret (z) is returned.
*
* @param z_length
* The length in bytes of the z buffer. This length must be greater or
* equal to privlen, as returned when creating the ICA_EC_KEY objects.
* Both keys are supposed to be based on the same elliptic curve, so
* both keys have the same lengths of D, and (X,Y).
*
* @return 0 if success
* EPERM if the EC curve is not supported in this environment
* EINVAL if at least one invalid parameter is given.
* EIO if an internal processing error occurred.
*/
ICA_EXPORT
int ica_ecdh_derive_secret(ica_adapter_handle_t adapter_handle,
const ICA_EC_KEY *privkey_A, const ICA_EC_KEY *pubkey_B,
unsigned char *z, unsigned int z_length);
/**
* Create an ECDSA signature for the given hash data using the given
* private ICA_EC_KEY.
*
* @param privkey
* Pointer to a readable private ICA_EC_KEY object.
*
* @param hash
* Pointer to a readable buffer containing hashed data.
*
* @param
* The length of the hashed data. Supported lengths are
* 20, 28, 32, 48, and 64 bytes.
*
* @param signature
* Pointer to a writable buffer where the ECDSA signature is returned.
*
* @param signature_length
* The length of the buffer. It must be greater or equal to 2*privlen
* as returned when creating the ICA_EC_KEY object.
*
* @return 0 if success
* EINVAL if at least one invalid parameter is given.
* EIO if an internal processing error occurred.
*/
ICA_EXPORT
int ica_ecdsa_sign(ica_adapter_handle_t adapter_handle,
const ICA_EC_KEY *privkey, const unsigned char *hash, unsigned int hash_length,
unsigned char *signature, unsigned int signature_length);
/**
* Verify a given ECDSA signature with given hash data and public ICA_EC_KEY.
*
* @param pubkey
* Pointer to a readable public ICA_EC_KEY object.
*
* @param hash
* Pointer to a readable buffer containing hashed data.
*
* @param
* The length of the hashed data. Supported lengths are
* 20, 28, 32, 48, and 64 bytes.
*
* @param signature
* Pointer to a writable buffer where the ECDSA signature is returned.
*
* @param signature_length
* The length of the buffer. It must be greater or equal to 2*privlen
* as returned when creating the ICA_EC_KEY object.
*
* @return 0 if success
* EINVAL if at least one invalid parameter is given.
* EIO if an internal processing error occurred.
* EFAULT if signature invalid
*/
ICA_EXPORT
int ica_ecdsa_verify(ica_adapter_handle_t adapter_handle,
const ICA_EC_KEY *pubkey, const unsigned char *hash, unsigned int hash_length,
const unsigned char *signature, unsigned int signature_length);
/**
* provide the public key (X,Y) of the given ICA_EC_KEY.
*
* @param key
* Pointer to a readable ICA_EC_KEY object.
*
* @param q
* Pointer to a writable buffer where (X,Y) is returned.
*
* @param q_len
* Pointer to a unsigned int where the byte length of (X,Y) is returned.
*
* @return 0 if success
* EINVAL if at least one invalid parameter is given.
*/
ICA_EXPORT
int ica_ec_key_get_public_key(const ICA_EC_KEY *key, unsigned char *q, unsigned int *q_len);
/**
* provide the private key (D) of the given ICA_EC_KEY.
*
* @param key
* Pointer to a readable ICA_EC_KEY object.
*
* @param q
* Pointer to a writable buffer where (D) is returned.
*
* @param q_len
* Pointer to a unsigned int where the byte length of (D) is returned.
*
* @return 0 if success
* EINVAL if at least one invalid parameter is given.
*/
ICA_EXPORT
int ica_ec_key_get_private_key(const ICA_EC_KEY *key, unsigned char *d, unsigned int *d_len);
/**
* Free an ICA_EC_KEY.
*
* @param key
* Pointer to ICA_EC_KEY.
*/
ICA_EXPORT
void ica_ec_key_free(ICA_EC_KEY *key);
typedef struct ica_x25519_ctx ICA_X25519_CTX;
typedef struct ica_x448_ctx ICA_X448_CTX;
typedef struct ica_ed25519_ctx ICA_ED25519_CTX;
typedef struct ica_ed448_ctx ICA_ED448_CTX;
/*
* Allocate a new context. MSA9 required.
* Returns 0 if successful. Otherwise, -1 is returned.
*/
ICA_EXPORT
int ica_x25519_ctx_new(ICA_X25519_CTX **ctx);
ICA_EXPORT
int ica_x448_ctx_new(ICA_X448_CTX **ctx);
ICA_EXPORT
int ica_ed25519_ctx_new(ICA_ED25519_CTX **ctx);
ICA_EXPORT
int ica_ed448_ctx_new(ICA_ED448_CTX **ctx);
/*
* Copy the private and public key to the context. MSA9 required.
* Returns 0 if successful. Otherwise, -1 is returned.
*/
ICA_EXPORT
int ica_x25519_key_set(ICA_X25519_CTX *ctx, const unsigned char priv[32],
const unsigned char pub[32]);
ICA_EXPORT
int ica_x448_key_set(ICA_X448_CTX *ctx, const unsigned char priv[56],
const unsigned char pub[56]);
ICA_EXPORT
int ica_ed25519_key_set(ICA_ED25519_CTX *ctx, const unsigned char priv[32],
const unsigned char pub[32]);
ICA_EXPORT
int ica_ed448_key_set(ICA_ED448_CTX *ctx, const unsigned char priv[56],
const unsigned char pub[56]);
/*
* Copy the private and public key from the context. MSA9 required.
* Returns 0 if successful. Otherwise, -1 is returned.
*/
ICA_EXPORT
int ica_x25519_key_get(ICA_X25519_CTX *ctx, unsigned char priv[32],
unsigned char pub[32]);
ICA_EXPORT
int ica_x448_key_get(ICA_X448_CTX *ctx, unsigned char priv[56],
unsigned char pub[56]);
ICA_EXPORT
int ica_ed25519_key_get(ICA_ED25519_CTX *ctx, unsigned char priv[32],
unsigned char pub[32]);
ICA_EXPORT
int ica_ed448_key_get(ICA_ED448_CTX *ctx, unsigned char priv[57],
unsigned char pub[57]);
/*
* Generate a key. MSA9 required.
* Returns 0 if successful. Otherwise, -1 is returned.
*/
ICA_EXPORT
int ica_x25519_key_gen(ICA_X25519_CTX *ctx);
ICA_EXPORT
int ica_x448_key_gen(ICA_X448_CTX *ctx);
ICA_EXPORT
int ica_ed25519_key_gen(ICA_ED25519_CTX *ctx);
ICA_EXPORT
int ica_ed448_key_gen(ICA_ED448_CTX *ctx);
/*
* Derive a shared secret. Requires the context to hold the private key.
* MSA9 required. Returns 0 if successful. Otherwise, -1 is returned.
*/
ICA_EXPORT
int ica_x25519_derive(ICA_X25519_CTX *ctx,
unsigned char shared_secret[32],
const unsigned char peer_pub[32]);
ICA_EXPORT
int ica_x448_derive(ICA_X448_CTX *ctx,
unsigned char shared_secret[56],
const unsigned char peer_pub[56]);
/*
* Sign. Requires the context to hold the private key. MSA9 required.
* Returns 0 if successful. Otherwise, -1 is returned.
*/
ICA_EXPORT
int ica_ed25519_sign(ICA_ED25519_CTX *ctx, unsigned char sig[64],
const unsigned char *msg, size_t msglen);
ICA_EXPORT
int ica_ed448_sign(ICA_ED448_CTX *ctx, unsigned char sig[114],
const unsigned char *msg, size_t msglen);
/*
* Verify. Requires the public key. If the context only holds the private key,
* the public key is derived. MSA9 required.
* Returns 0 if signature is valid. Otherwise, -1 is returned.
*/
ICA_EXPORT
int ica_ed25519_verify(ICA_ED25519_CTX *ctx, const unsigned char sig[64],
const unsigned char *msg, size_t msglen);
ICA_EXPORT
int ica_ed448_verify(ICA_ED448_CTX *ctx, const unsigned char sig[114],
const unsigned char *msg, size_t msglen);
/*
* Delete a context. Its sensitive data is erased. MSA9 required.
* Returns 0 if successful. Otherwise, -1 is returned.
*/
ICA_EXPORT
int ica_x25519_ctx_del(ICA_X25519_CTX **ctx);
ICA_EXPORT
int ica_x448_ctx_del(ICA_X448_CTX **ctx);
ICA_EXPORT
int ica_ed25519_ctx_del(ICA_ED25519_CTX **ctx);
ICA_EXPORT
int ica_ed448_ctx_del(ICA_ED448_CTX **ctx);
/*
* End of ECC API
*
******************************************************************************/
/**
* Generate RSA keys in modulus/exponent format.
* @param adapter_handle
* Pointer to a previously opened device handle.
* @param modulus_bit_length
* Specifies the bit length of the modulus. This value should comply with
* length of the keys.
* @param public_key
* Pointer to where the generated public key is to be placed. If the exponent
* element in the public key is not set, it will be randomly generated. A not
* well chosen exponent may result in the program looping endlessly. Common
* public exponents are 3 and 65537.
* @param private_key
* Pointer to where the generated private key in modulus/exponent format is to
* be placed. Length of both private and public key should be set in bytes.
* This value should comply with modulus bit length. Make sure that buffers in
* the keys fit to this length.
*
* @return 0 if successful.
* EINVAL if at least one invalid parameter is given.
* EFAULT if OpenSSL key generation should fail.
*/
ICA_EXPORT
unsigned int ica_rsa_key_generate_mod_expo(ica_adapter_handle_t adapter_handle,
unsigned int modulus_bit_length,
ica_rsa_key_mod_expo_t *public_key,
ica_rsa_key_mod_expo_t *private_key);
/**
* Generate RSA keys in CRT format.
* @param adapter_handle
* Pointer to a previously opened device handle.
* @param modulus_bit_length
* Specifies the bit length of the modulus. This value should comply with
* length of the keys.
* @param public_key
* Pointer to where the generated public key is to be placed. If the exponent
* element in the public key is not set, it will be randomly generated. A not
* well chosen exponent may result in the program looping endlessly. Common
* public exponents are 3 and 65537.
* @param private_key
* Pointer to where the generated private key in CRT format is to be placed.
* Length of both private and public key should be set in bytes. This value
* should comply with modulus bit length. Make sure that buffers in the keys
* fit to this length.
*
* @return 0 if successful.
* EINVAL if at least one invalid parameter is given.
* EFAULT if OpenSSL key generation should fail.
*/
ICA_EXPORT
unsigned int ica_rsa_key_generate_crt(ica_adapter_handle_t adapter_handle,
unsigned int modulus_bit_length,
ica_rsa_key_mod_expo_t *public_key,
ica_rsa_key_crt_t *private_key);
/**
* @brief Perform a RSA encryption/decryption operation using a key in
* modulus/exponent form.
*
* Make sure your message is padded before using this function. Otherwise you
* will risk security!
* @param adapter_handle
* Pointer to a previously opened device handle.
* @param input_data
* Pointer to input data to be encrypted/decrypted and is in big endian format.
* Make sure input data is not longer than bit length of the key! Byte length
* has to be the same. Thus right justify input data inside the data block.
* @param rsa_key
* Pointer to the key to be used, in modulus/exponent format.
* @param output_data
* Pointer to where the output results are to be placed.
*
* @return 0 if successful.
* EINVAL if at least one invalid parameter is given.
* ENOMEM if memory allocation fails.
* EIO if the operation fails. This should never happen.
*/
ICA_EXPORT
unsigned int ica_rsa_mod_expo(ica_adapter_handle_t adapter_handle,
unsigned char *input_data,
ica_rsa_key_mod_expo_t *rsa_key,
unsigned char *output_data);
/**
* @brief Perform a RSA encryption/decryption operation using a key in CRT
* form.
*
* Make sure your message is padded before using this function. Otherwise you
* will risk security!
* @param adapter_handle
* Pointer to a previously opened device handle.
* @param input_data
* Pointer to input data to be encrypted/decrypted and is in big endian format.
* Make sure input data is not longer than bit length of the key! Byte length
* has to be the same. Thus right justify input data inside the data block.
* @param rsa_key
* Pointer to the key to be used, in CRT format.
* @param output_data
* Pointer to where the output results are to be placed. Buffer has to be as
* large as the input_data and length of the modulus specified in rsa_key.
*
* @return 0 if successful.
* EINVAL if at least one invalid parameter is given.
* ENOMEM if memory allocation fails.
* EIO if the operation fails. This should never happen.
*/
ICA_EXPORT
unsigned int ica_rsa_crt(ica_adapter_handle_t adapter_handle,
unsigned char *input_data,
ica_rsa_key_crt_t *rsa_key,
unsigned char *output_data);
/*
* Check if RSA key credentials in CRT format are presented in
* privileged form, respectively prime 'p' > prime 'q'.
*
* In case of 'p' < 'q', key credentials 'p' and 'q' as well as 'dp'
* and 'dq' will be swapped and qInverse will be recalculated.
*
* @return
* 0 if all key credentials are in the correct format.
* 1 if the key credentials were re-calculated.
* ENOMEM if memory allocation fails.
*/
ICA_EXPORT
unsigned int ica_rsa_crt_key_check(ica_rsa_key_crt_t *rsa_key);
/**
* @deprecated, use ica_des_ecb() or ica_des_cbc() instead.
*
* Encrypt data using a single length DES key.
* @param mode Specifies the operational mode and must be:
* MODE_ECB - Use Electronic Code Book mode
* MODE_CBC - Use Cipher Block Chaining mode
* @param data_length
* Specifies the byte length of the input data. It has to be a multiple of the
* cipher block which has a size of 8 byte.
* @param input_data
* Pointer to the input data data to be encrypted. Must be a multiple of the
* cipher to use hw acceleration.
* @param iv
* Pointer to a valid 8 byte initialization vector when using CBC mode.
* @param des_key
* Pointer to a single length DES key.
* @param output_data
* Pointer to the buffer to contain the resulting encrypted data. Must be a
* multiple of the cipher block and at least as big as the buffer for
* input_data.
*
* @return 0 if successful.
* EINVAL if at least one invalid parameter is given.
* EIO if the operation fails. This should never happen.
*/
/* XXX next major: remove */
ICA_EXPORT ICA_DEPRECATED
unsigned int ica_des_encrypt(unsigned int mode,
unsigned int data_length,
unsigned char *input_data,
ica_des_vector_t *iv,
ica_des_key_single_t *des_key,
unsigned char *output_data);
/**
* @deprecated, use ica_des_ecb() or ica_des_cbc() instead.
*
* Decrypt data using a single length DES key.
* @param mode
* Specifies the operational mode and must be:
* MODE_ECB - Use Electronic Code Book mode
* MODE_CBC - Use Cipher Block Chaining mode
* @param data_length
* Specifies the byte length of the input data. It has to be a multiple of the
* cipher block which has a size of 8 byte.
* @param input_data
* Pointer to the input data data to be decrypted. Must be a multiple of the
* cipher to use hw acceleration.
* @param iv
* Pointer to a valid 8 byte initialization vector when using CBC mode.
* @param des_key
* Pointer to a single length DES key.
* @param output_data
* Pointer to the buffer to contain the resulting decrypted data. Must be a
* multiple of the cipher block and at least as big as the buffer for
* input_data.
*
* @return 0 if successful.
* EINVAL if at least one invalid parameter is given.
* EIO if the operation fails. This should never happen.
*/
/* XXX next major: remove */
ICA_EXPORT ICA_DEPRECATED
unsigned int ica_des_decrypt(unsigned int mode,
unsigned int data_length,
unsigned char *input_data,
ica_des_vector_t *iv,
ica_des_key_single_t *des_key,
unsigned char *output_data);
/**
* @deprecated, use ica_3des_ecb() or ica_3des_cbc() instead.
*
* Encrypt data using a triple length DES key.
* @param mode
* Specifies the operational mode and must be:
* MODE_ECB - Use Electronic Code Book mode
* MODE_CBC - Use Cipher Block Chaining mode
* @param data_length
* Specifies the byte length of the input data. It has to be a multiple of the
* cipher block which has a size of 8 byte.
* @param input_data
* Pointer to the input data data to be encrypted. Must be a multiple of the
* cipher block to use hw acceleration.
* @param iv
* Pointer to a valid 8 byte initialization vector when using CBC mode.
* @param des_key
* Pointer to a triple length DES key.
* @param output_data
* Pointer to the buffer to contain the resulting encrypted data. Must be a
* multiple of the cipher block and at least as big as the buffer for
* input_data.
*
* @return 0 if successful.
* EINVAL if at least one invalid parameter is given.
* EIO if the operation fails. This should never happen.
*/
/* XXX next major: remove */
ICA_EXPORT ICA_DEPRECATED
unsigned int ica_3des_encrypt(unsigned int mode,
unsigned int data_length,
unsigned char *input_data,
ica_des_vector_t *iv,
ica_des_key_triple_t *des_key,
unsigned char *output_data);
/**
* @deprecated, use ica_3des_ecb() or ica_3des_cbc() instead.
*
* Decrypt data using a triple length DES key.
* @param mode
* Specifies the operational mode and must be:
* MODE_ECB - Use Electronic Code Book mode
* MODE_CBC - Use Cipher Block Chaining mode
* @param data_length
* Specifies the byte length of the input data. It has to be a multiple of the
* cipher block which has a size of 8 byte.
* @param input_data
* Pointer to the input data data to be decrypted. Must be a multiple of the
* cipher block to use hw acceleration.
* @param iv
* Pointer to a valid 8 byte initialization vector when using CBC mode.
* @param des_key
* Pointer to a triple length DES key.
* @param output_data
* Pointer to the buffer to contain the resulting decrypted data. Must be a
* multiple of the cipher block and at least as big as the buffer for
* input_data.
*
* @return 0 if successful.
* EINVAL if at least one invalid parameter is given.
* EIO if the operation fails. This should never happen.
*/
/* XXX next major: remove */
ICA_EXPORT ICA_DEPRECATED
unsigned int ica_3des_decrypt(unsigned int mode,
unsigned int data_length,
unsigned char *input_data,
ica_des_vector_t *iv,
ica_des_key_triple_t *des_key,
unsigned char *output_data);
/**
* @deprecated, use ica_aes_ecb() or ica_aes_cbc() instead.
*
* Encrypt data using AES (key_length is 16, 24, or 32)
* @param mode
* Specifies the operational mode and must be:
* MODE_ECB - Use Electronic Code Book mode
* MODE_CBC - Use Cipher Block Chaining mode
* @param data_length
* Specifies the byte length of the input data. Input data length has to be
* a multiple of the AES block length, which is 16 bytes.
* @param input_data
* Pointer to the input data data to be encrypted. Must be a multiple of the
* cipher block to use hw acceleration.
* @param iv
* Pointer to a valid 16 byte initialization vector when using CBC mode.
* @param key_length
* Length of the AES key being used.
* @param aes_key
* Pointer to an AES key.
* @param output_data
* Pointer to the buffer to contain the resulting encrypted data. Must be a
* multiple of the cipher block and at least as big as the buffer for
* input_data.
*
* @return 0 if successful.
* EINVAL if at least one invalid parameter is given.
* EIO if the operation fails. This should never happen.
*/
/* XXX next major: remove */
ICA_EXPORT ICA_DEPRECATED
unsigned int ica_aes_encrypt(unsigned int mode,
unsigned int data_length,
unsigned char *input_data,
ica_aes_vector_t *iv,
unsigned int key_length,
unsigned char *aes_key,
unsigned char *output_data);
/**
* @deprecated, use ica_aes_ecb() or ica_aes_cbc() instead.
*
* Decrypt data using AES (key_length is 16, 24, or 32)
* @param mode
* Specifies the operational mode and must be:
* MODE_ECB - Use Electronic Code Book mode
* MODE_CBC - Use Cipher Block Chaining mode
* @param data_length
* Specifies the byte length of the input data. Input data length has to be
* a multiple of the AES block length, which is 16 bytes.
* @param input_data
* Pointer to the input data to be decrypted. Must be a multiple of the
* cipher block to use hw acceleration.
* @param iv
* Pointer to a valid 16 byte initialization vector when using CBC mode.
* @param key_length
* Length of the AES key being used.
* @param aes_key
* Pointer to an AES key.
* @param output_data
* Pointer to the buffer to contain the resulting decrypted data. Must be a
* multiple of the cipher block and at least as big as the buffer for
* input_data.
*
* @return 0 if successful.
* EINVAL if at least one invalid parameter is given.
* EIO if the operation fails. This should never happen.
*/
/* XXX next major: remove */
ICA_EXPORT ICA_DEPRECATED
unsigned int ica_aes_decrypt(unsigned int mode,
unsigned int data_length,
unsigned char *input_data,
ica_aes_vector_t *iv,
unsigned int key_length,
unsigned char *aes_key,
unsigned char *output_data);
/**
* Encrypt or decrypt data with an DES key using Electronic Cook Book (ECB)
* mode as described in NIST Special Publication 800-38A Chapter 6.1.
*
* Required HW Support
* KM-DEA-192
*
* @param in_data
* Pointer to a readable buffer, that contains the message to be en/decrypted.
* The size of the message in bytes is data_length. The size of this buffer in
* bytes must be at least as big as data_length.
* @param out_data
* Pointer to a writeable buffer, that will contain the resulting en/decrypted
* message. The size of this buffer in bytes must be at least as big as
* data_length.
* @param data_length
* Length in bytes of the message to be en/decrypted, which resides at the
* beginning of in_data. data_length must be a multiple of the cipher block
* size (i.e. a multiple of 8 for DES).
* @param key
* Pointer to a valid DES key of 8 bytes length.
* @param direction
* 0 or 1:
* 0 Use the decrypt function.
* 1 Use the encrypt function.
*
* @return 0 on success
* EINVAL if at least one invalid parameter is given.
* EIO if the operation fails.
*/
ICA_EXPORT
unsigned int ica_des_ecb(const unsigned char *in_data, unsigned char *out_data,
unsigned long data_length, unsigned char *key,
unsigned int direction);
/**
* Encrypt or decrypt data with an DES key using Cipher Block Chaining (CBC)
* mode as described in NIST Special Publication 800-38A Chapter 6.2.
*
* Required HW Support
* KMC-DEA
*
* @param in_data
* Pointer to a readable buffer, that contains the message to be en/decrypted.
* The size of the message in bytes is data_length. The size of this buffer in
* bytes must be at least as big as data_length.
* @param out_data
* Pointer to a writable buffer, that will contain the resulting en/decrypted
* message. The size of this buffer in bytes must be at least as big as
* data_length.
* @param data_length
* Length in bytes of the message to be en/decrypted, which resides at the
* beginning of in_data. data_length must be a multiple of the cipher block
* size (i.e. a multiple of 8 for DES).
* @param key
* Pointer to a valid DES key of 8 bytes length.
* @param iv
* Pointer to a valid initialization vector of cipher block size bytes. This
* vector will be overwritten during the function. The result value in iv may
* be used as initialization vector for a chained ica_des_cbc call with the
* same key.
* @param direction
* 0 or 1:
* 0 Use the decrypt function.
* 1 Use the encrypt function.
*
* @return 0 on success
* EINVAL if at least one invalid parameter is given.
* EIO if the operation fails.
*/
ICA_EXPORT
unsigned int ica_des_cbc(const unsigned char *in_data, unsigned char *out_data,
unsigned long data_length, unsigned char *key,
unsigned char *iv,
unsigned int direction);
/**
* Encrypt or decrypt data with an DES key using Cipher Block Chaining with
* Ciphertext Stealing (CBC-CS) mode as described in NIST Special Publication
* 800-38A Chapter 6.2 and the Addendum to NIST Special Publication 800-38A on
* Recommendation for Block Cipher Modes of Operation: Three Variants of
* Ciphertext Stealing for CBC Moder:
* ica_des_cbc_cs may be used to encrypt or decrypt the last chunk of a
* message consisting of multiple chunks where all but the last chunk are
* encrypted or decrypted by chained calls to ica_des_cbc and the resulting
* iv of the last call to ica_des_cbc is fed into the iv of the ica_des_cbc_cs
* call provided the chunk is greater than cipher block size (greater than
* 8 bytes for DES).
*
* Required HW Support
* KMC-DEA
*
* @param in_data
* Pointer to a readable buffer, that contains the message to be en/decrypted.
* The size of the message in bytes is data_length. The size of this buffer
* in bytes must be at least as big as data_length.
* @param out_data
* Pointer to a writable buffer, that will contain the resulting en/decrypted
* message. The size of this buffer in bytes must be at least as big as
* data_length.
* @param data_length
* Length in bytes of the message to be en/decrypted, which resides at the
* beginning of in_data. data_length must be greater than or equal to the
* cipher block size (i.e. a multiple of 8 bytes for DES).
* @param key
* Pointer to a valid DES key of 8 bytes length.
* @param iv
* Pointer to a valid initialization vector of cipher block size bytes.
* This vector will be overwritten during the function. For variant equals 1
* or variant equals 2 the result value in iv may be used as initialization
* vector for a chained ica_des_cbc call with the same key if data_length is
* a multiple of the cipher block size.
* @param direction
* 0 or 1:
* 0 Use the decrypt function.
* 1 Use the encrypt function.
* @param variant
* 1 Use variant CBC-CS1 of the Addendum to NIST Special Publication 800-38A
* to encrypt or decrypt the message: keep last two blocks in order.
* 2 Use variant CBC-CS2 of the Addendum to NIST Special Publication 800-38A
* to encrypt or decrypt the message: switch order of the last two blocks
* if data_length is not a multiple of the cipher block size (i.e. a
* multiple of 8 for DES).
* 3 Use variant CBC-CS3 of the Addendum to NIST Special Publication 800-38A
* to encrypt or decrypt the message: always switch order of the last two
* blocks.
*
* @return 0 on success
* EINVAL if at least one invalid parameter is given.
* EPERM if required hardware support is not available.
* EIO if the operation fails.
*/
ICA_EXPORT
unsigned int ica_des_cbc_cs(const unsigned char *in_data, unsigned char *out_data,
unsigned long data_length, unsigned char *key,
unsigned char *iv,
unsigned int direction,
unsigned int variant);
/**
* Encrypt or decrypt data with an DES key using Cipher Feedback (CFB) mode as
* described in NIST Special Publication 800-38A Chapter 6.3.
*
* Required HW Support
* KMF-DEA
*
* @param in_data
* Pointer to a readable buffer, that contains the message to be en/decrypted.
* The size of the message in bytes is data_length. The size of this buffer in
* bytes must be at least as big as data_length.
* @param out_data
* Pointer to a writable buffer, that will contain the resulting en/decrypted
* message. The size of this buffer in bytes must be at least as big as
* data_length.
* @param data_length
* Length in bytes of the message to be en/decrypted, which resides at the
* beginning of in_data.
* @param key
* Pointer to a valid DES key of 8 bytes length.
* @param iv
* Pointer to a valid initialization vector of cipher block size bytes (8 bytes
* for DES). This vector will be overwritten during the function. The result
* value in iv may be used as initialization vector for a chained ica_des_cfb
* call with the same key if data_length in the preceding call is a multiple of
* lcfb.
* @param lcfb
* Length in bytes of the cipher feedback which is a value greater than or
* equal to 1 and less than or equal to the cipher block size (i.e. 8 for DES).
* @param direction
* 0 or 1:
* 0 Use the decrypt function.
* 1 Use the encrypt function.
*
* @return 0 on success
* EINVAL if at least one invalid parameter is given.
* EPERM if required hardware support is not available.
* EIO if the operation fails.
*/
ICA_EXPORT
unsigned int ica_des_cfb(const unsigned char *in_data, unsigned char *out_data,
unsigned long data_length, unsigned char *key,
unsigned char *iv, unsigned int lcfb,
unsigned int direction);
/**
* Encrypt or decrypt data with an DES key using Counter (CTR) mode as
* described in NIST Special Publication 800-38A Chapter 6.5. With the counter
* mode each message block of size cipher block size (i.e. 8 bytes for DES) is
* combined with a counter value of the same size during encryption and
* decryption. Starting with an initial counter value to be combined with the
* first message block subsequent counter values to be combined with subsequent
* message blocks will be derived from preceding counter values by an increment
* function. The increment function used in ica_des_ctr is s an arithmetic
* increment without carry on the U least significant bits in the counter
* where M is a parameter to ica_des_ctr.
*
* Required HW Support
* KMCTR-DEA
*
* @param in_data
* Pointer to a readable buffer, that contains the message to be en/decrypted.
* The size of the message in bytes is data_length. The size of this buffer in
* bytes must be at least as big as data_length.
* @param out_data
* Pointer to a writable buffer, that will contain the resulting en/decrypted
* message. The size of this buffer in bytes must be at least as big as
* data_length.
* @param data_length
* Length in bytes of the message to be en/decrypted, which resides at the
* beginning of in_data.
* @param key
* Pointer to a valid DES key of 8 bytes length.
* @param ctr
* Pointer to a readable and writable buffer of size cipher block size bytes.
* ctr contains an initialization value for a counter function and it will be
* replaced by a new value. That new value can be used as an initialization
* value for a counter function in a chained ica_des_ctr call with the same key
* if data_length used in the preceding call is a multiple of the cipher block
* size.
* @param ctr_width
* A number U between 8 and cipher block size in bits. The value is used by the
* counter increment function which increments a counter value by incrementing
* without carry the least significant U bits of the counter value. The value
* must be a multiple of 8. When in FIPS mode, an additional counter overflow
* check is performed, so that the given data length, divided by the cipher
* block size, is not greater than 2 to the power of U.
* @param direction
* 0 or 1:
* 0 Use the decrypt function.
* 1 Use the encrypt function.
*
* @return 0 on success
* EINVAL if at least one invalid parameter is given.
* EPERM if required hardware support is not available.
* EIO if the operation fails.
*/
ICA_EXPORT
unsigned int ica_des_ctr(const unsigned char *in_data, unsigned char *out_data,
unsigned long data_length,
unsigned char *key,
unsigned char *ctr, unsigned int ctr_width,
unsigned int direction);
/**
* Encrypt or decrypt data with an DES key using Counter (CTR) mode as
* described in NIST Special Publication 800-38A, Chapter 6.5. With the counter
* mode each message block of size cipher block size is combined with a counter
* value of the same size during encryption and decryption. The ica_des_ctrlist
* function assumes that a list n of precomputed counter values is provided
* where n is the smallest integer that is less or equal to the message size
* divided by the cipher block size. This function allows to optimally exploit
* System z HW support for non-standard counter functions.
*
* Required HW Support
* KMCTR-DEA
*
* @param in_data
* Pointer to a readable buffer, that contains the message to be en/decrypted.
* The size of the message in bytes is data_length. The size of this buffer in
* bytes must be at least as big as data_length.
* @param out_data
* Pointer to a writable buffer, that will contain the resulting en/decrypted
* message. The size of this buffer in bytes must be at least as big as
* data_length.
* @param data_length
* Length in bytes of the message to be en/decrypted, which resides at the
* beginning of in_data. If data_length is a multiple of the cipher block size
* then calls of ica_des_ctrlist with the same key can be chained if ctrlist
* argument of the chained call contains a list of counters that follows the
* counters used in the first call and data_length used in the preceding call
* is a multiple of the cipher block size.
* @param key
* Pointer to a valid DES key of 8 bytes length.
* @param ctrlist
* Pointer to a readable buffer of that is both of size greater than or equal
* to data_length and a multiple of the cipher block size (i.e. 8 bytes for
* DES). ctrlist should contain a list of precomputed counter values of size
* cipher block size each.
* @param direction
* 0 or 1:
* 0 Use the decrypt function.
* 1 Use the encrypt function.
*
* @return 0 on success
* EINVAL if at least one invalid parameter is given.
* EPERM if required hardware support is not available.
* EIO if the operation fails.
*/
ICA_EXPORT
unsigned int ica_des_ctrlist(const unsigned char *in_data, unsigned char *out_data,
unsigned long data_length,
unsigned char *key,
const unsigned char *ctrlist,
unsigned int direction);
/**
* Encrypt or decrypt data with an DES key using Output Feedback (OFB) mode as
* described in NIST Special Publication 800-38A Chapter 6.4.
*
* Required HW Support
* KMO-DEA
*
* @param in_data
* Pointer to a readable buffer, that contains the message to be en/decrypted.
* The size of the message in bytes is data_length. The size of this buffer in
* bytes must be at least as big as data_length.
* @param out_data
* Pointer to a writable buffer, that contains the resulting en/decrypted
* message. The size of this buffer in bytes must be at least as big as
* data_length.
* @param data_length
* Length in bytes of the message to be en/decrypted, which resides at the
* beginning of in_data.
* @param key
* Pointer to a valid DES key of 8 bytes length.
* @param iv
* Pointer to a valid initialization vector of cipher block size bytes (8 bytes
* for DES). This vector will be overwritten during the function. If
* data_length is a multiple of the cipher block size (i.e. a multiple of 8 for
* DES) the result value in iv may be used as initialization vector for a
* chained ica_des_ofb call with the same key.
* @param direction
* 0 or 1:
* 0 Use the decrypt function.
* 1 Use the encrypt function.
*
* @return 0 on success
* EINVAL if at least one invalid parameter is given.
* EPERM if required hardware support is not available.
* EIO if the operation fails.
*/
ICA_EXPORT
unsigned int ica_des_ofb(const unsigned char *in_data, unsigned char *out_data,
unsigned long data_length, unsigned char *key,
unsigned char *iv, unsigned int direction);
/**
* Authenticate data or verify the authenticity of data with an DES key using
* the Block Cipher Based Message Authetication Code (CMAC) mode as described
* in NIST Special Publication 800-38B. ica_des_cmac can be used to
* authenticate or verify the authenticity of a complete message.
*
* Required HW Support
* KMAC-DEA
* PCC-Compute-Last_block-CMAC-Using-DEA
*
* @param message
* Pointer to a readable buffer of size greater than or equal to message_length
* bytes. It contains a message to be authenticated or of which the
* authenticity shall be verified.
* @param message_length
* Length in bytes of the message to be authenticated or verified.
* @param mac
* Pointer to a buffer of size greater than or equal to mac_length bytes. If
* direction is 1 the buffer must be writable and a message authentication code
* for the message in message of size mac_length bytes will be written to the
* buffer. If direction is 0 the buffer must be readable and contain a message
* authentication code that will be verified against the message in message.
* @param mac_length
* Length in bytes of the message authentication code mac in bytes that is less
* than or equal to the cipher block size (i.e. 8 bytes for DES). It is
* recommended to use values greater than or equal to 8.
* @param key
* Pointer to a valid DES key of 8 bytes length.
* @param direction
* 0 or 1:
* 0 Verify message authentication code
* 1 Compute message authentication code for the message
*
* @return 0 on success
* EINVAL if at least one invalid parameter is given.
* EPERM if required hardware support is not available.
* EIO if the operation fails.
* EFAULT if direction is 0 and the verification of the message authentication
* code fails.
*/
ICA_EXPORT
unsigned int ica_des_cmac(const unsigned char *message, unsigned long message_length,
unsigned char *mac, unsigned int mac_length,
unsigned char *key,
unsigned int direction);
/**
* Authenticate data or verify the authenticity of data with an DES key using
* the Block Cipher Based Message Authentication Code (CMAC) mode as described
* in NIST Special Publication 800-38B.
* ica_des_cmc_intermediate and ica_des_cmac_last can be used when the message
* to be authenticated or to be verified using CMAC is supplied in multiple
* chunks. ica_des_cmac_intermediate is used to process all but the last
* chunk. All message chunks to preprocessed by ica_des_cmac_intermediate
* must have a size that is a multiple of the cipher block size (i.e a
* multiple of 8 bytes for DES).
* Note: ica_des_cmac_intermediate has no direction argument it can be used
* during an authentication and during authenticity verification.
*
* Required HW Support
* KMAC-DEA
*
* @param message
* Pointer to a readable buffer of size greater than or equal to
* message_length bytes. It contains a non final part of a message which
* shall be authenticated or of which the authenticity shall be verified.
* @param message_length
* Length in bytes of the message part in message. It must be a multiple
* of the cipher block size.
* @param key
* Pointer to a valid DES key of 8 bytes length.
* @param iv
* Pointer to a valid initialization vector of size cipher block size (i.e.
* 8 bytes for DES). For the first message part it must be set to a string
* of zeros. For processing the n-th message part it must be the resulting iv
* value of the ica_des_cmac_intermediate applied to the (n-1)-th message
* part. This vector will be overwritten during the function. The result value
* in iv may be used as initialization vector for a chained call to
* ica_des_cmac_initermediate or to ica_des_cmac_last with the same key.
*
* @return 0 on success
* EINVAL if at least one invalid parameter is given.
* EPERM if required hardware support is not available.
* EIO if the operation fails.
*/
ICA_EXPORT
unsigned int ica_des_cmac_intermediate(const unsigned char *message,
unsigned long message_length,
unsigned char *key,
unsigned char *iv);
/**
* Authenticate data or verify the authenticity of data with an DES key using
* the Block Cipher Based Message Authentication Code (CMAC) mode as described
* in NIST Special Publication 800-38B.
* ica_des_cmac_last can be used to authenticate or verify the authenticity of
* a complete message or of the final part of a message for which all
* preceding parts were preprocessed with ica_des_cmac_intermediate.
*
* Required HW Support
* KMAC-DEA,
* PCC-Compute-Last_block-CMAC-Using-DEA
*
* @param message
* Pointer to a readable buffer of size greater than or equal to message_length
* bytes. It contains a message or the final part of a message to be
* authenticated or of which the authenticity shall be verified.
* @param message_length
* Length in bytes of the message to be authenticated or verified.
* @param mac
* Pointer to a buffer of size greater than or equal to mac_length bytes.
* If direction is 1 the buffer must be writable and a message authentication
* code for the message in message of size mac_length bytes will be written to
* the buffer.
* If direction is 0 the buffer must be readable and contain a message
* authentication code that will be verified against the message in message.
* @param mac_length
* Length in bytes of the message authentication code mac in bytes that is less
* than or equal to the cipher block size (i.e. 8 bytes for DES). It is
* recommended to use values greater than or equal to 8.
* @param key
* Pointer to a valid DES key of 8 bytes length.
* @param iv
* Pointer to a valid initialization vector of size cipher block size. If iv is
* NULL message is assumed to be the complete message to be processed.
* Otherwise message is the final part of a composite message to be processed
* and iv contains the output vector resulting from processing all previous
* parts with chained calls to ica_aes_cmac_intermediate, i.e. the value
* returned in iv of the ica_des_cmac_intermediate call applied to the
* penultimate message part.
* @param direction
* 0 or 1:
* 0 Verify message authentication code
* 1 Compute message authentication code for the message
*
* @return 0 on success
* EINVAL if at least one invalid parameter is given.
* EPERM if required hardware support is not available.
* EIO if the operation fails.
* EFAULT if direction is 0 and the verification of the message authentication
* code fails.
*/
ICA_EXPORT
unsigned int ica_des_cmac_last(const unsigned char *message, unsigned long message_length,
unsigned char *mac, unsigned int mac_length,
unsigned char *key,
unsigned char *iv,
unsigned int direction);
/**
* Encrypt or decrypt data with an 3DES key using Electronic Cook Book (ECB)
* mode as described in NIST Special Publication 800-38A Chapter 6.1.
*
* Required HW Support
* KM-DEA-192
*
* @param in_data
* Pointer to a readable buffer, that contains the message to be en/decrypted.
* The size of the message in bytes is data_length. The size of this buffer in
* bytes must be at least as big as data_length.
* @param out_data
* Pointer to a writeable buffer, that will contain the resulting en/decrypted
* message. The size of this buffer in bytes must be at least as big as
* data_length.
* @param data_length
* Length in bytes of the message to be en/decrypted, which resides at the
* beginning of in_data. data_length must be a multiple of the cipher block
* size (i.e. a multiple of 8 for 3DES).
* @param key
* Pointer to a valid 3DES key of 24 bytes length.
* @param direction
* 0 or 1:
* 0 Use the decrypt function.
* 1 Use the encrypt function.
*
* @return 0 on success
* EINVAL if at least one invalid parameter is given.
* EIO if the operation fails.
*/
ICA_EXPORT
unsigned int ica_3des_ecb(const unsigned char *in_data, unsigned char *out_data,
unsigned long data_length, unsigned char *key,
unsigned int direction);
/**
* Encrypt or decrypt data with an 3DES key using Cipher Block Chaining (CBC)
* mode as described in NIST Special Publication 800-38A Chapter 6.2.
*
* Required HW Support
* KMC-TDEA-192
*
* @param in_data
* Pointer to a readable buffer, that contains the message to be en/decrypted.
* The size of the message in bytes is data_length. The size of this buffer in
* bytes must be at least as big as data_length.
* @param out_data
* Pointer to a writable buffer, that will contain the resulting en/decrypted
* message. The size of this buffer in bytes must be at least as big as
* data_length.
* @param data_length
* Length in bytes of the message to be en/decrypted, which resides at the
* beginning of in_data. data_length must be a multiple of the cipher block
* size (i.e. a multiple of 8 for 3DES).
* @param key
* Pointer to a valid 3DES key of 24 bytes length.
* @param iv
* Pointer to a valid initialization vector of cipher block size bytes. This
* vector will be overwritten during the function. The result value in iv may
* be used as initialization vector for a chained ica_3des_cbc call with the
* same key.
* @param direction
* 0 or 1:
* 0 Use the decrypt function.
* 1 Use the encrypt function.
*
* @return 0 on success
* EINVAL if at least one invalid parameter is given.
* EIO if the operation fails.
*/
ICA_EXPORT
unsigned int ica_3des_cbc(const unsigned char *in_data, unsigned char *out_data,
unsigned long data_length, unsigned char *key,
unsigned char *iv,
unsigned int direction);
/**
* Encrypt or decrypt data with an 3DES key using Cipher Block Chaining with
* Ciphertext Stealing (CBC-CS) mode as described in NIST Special Publication
* 800-38A Chapter 6.2 and the Addendum to NIST Special Publication 800-38A on
* "Recommendation for Block Cipher Modes of Operation: Three Variants of
* Ciphertext Stealing for CBC Mode":
* ica_3des_cbc_cs may be used to encrypt o decrypt the last chunk of a
* message consisting of multiple chunks where all but the last chunk are
* encrypted or decrypted by chained calls to ica_3des_cbc and the resulting
* iv of the last call to ica_3des_cbc is fed into the iv of the
* ica_3des_cbc_cs call provided the chunk is greater than cipher block size
* (greater than 8 bytes for 3DES).
*
* Required HW Support
* KMC-TDEA-192
*
* @param in_data
* Pointer to a readable buffer, that contains the message to be en/decrypted.
* The size of the message in bytes is data_length. The size of this buffer
* in bytes must be at least as big as data_length.
* @param out_data
* Pointer to a writable buffer, that will contain the resulting en/decrypted
* message. The size of this buffer in bytes must be at least as big as
* data_length.
* @param data_length
* Length in bytes of the message to be en/decrypted, which resides at the
* beginning of in_data. data_length must be greater than or equal to the
* cipher block size (i.e. a multiple of 8 bytes for 3DES).
* @param key
* Pointer to a valid 3DES key of 24 bytes length.
* @param iv
* Pointer to a valid initialization vector of cipher block size bytes. This
* vector will be overwritten during the function. For variant equals 1 or
* variant equals 2 the result value in iv may be used as initialization vector
* for a chained ica_3des_cbc call with the same key if data_length is a
* multiple of the cipher block size.
* @param direction
* 0 or 1:
* 0 Use the decrypt function.
* 1 Use the encrypt function.
* @param variant
* 1 Use variant CBC-CS1 of the Addendum to NIST Special Publication 800-38A
* to encrypt or decrypt the message: keep last two blocks in order.
* 2 Use variant CBC-CS2 of the Addendum to NIST Special Publication 800-38A
* to encrypt or decrypt the message: switch order of the last two blocks
* if data_length is not a multiple of the cipher block size (i.e. a
* multiple of 8 for DES).
* 3 Use variant CBC-CS3 of the Addendum to NIST Special Publication 800-38A
* to encrypt or decrypt the message: always switch order of the last two
* blocks.
*
* @return 0 on success
* EINVAL if at least one invalid parameter is given.
* EPERM if required hardware support is not available.
* EIO if the operation fails.
*/
ICA_EXPORT
unsigned int ica_3des_cbc_cs(const unsigned char *in_data, unsigned char *out_data,
unsigned long data_length,
unsigned char *key,
unsigned char *iv,
unsigned int direction, unsigned int variant);
/**
* Encrypt or decrypt data with an 3DES key using Cipher Feedback (CFB) mode as
* described in NIST Special Publication 800-38A Chapter 6.3.
*
* Required HW Support
* KMF-TDEA-192
*
* @param in_data
* Pointer to a readable buffer, that contains the message to be en/decrypted.
* The size of the message in bytes is data_length. The size of this buffer in
* bytes must be at least as big as data_length.
* @param out_data
* Pointer to a writable buffer, that will contain the resulting en/decrypted
* message. The size of this buffer in bytes must be at least as big as
* data_length.
* @param data_length
* Length in bytes of the message to be en/decrypted, which resides at the
* beginning of in_data.
* @param key
* Pointer to a valid 3DES key of 24 bytes length.
* @param iv
* Pointer to a valid initialization vector of cipher block size bytes (8 bytes
* for 3DES). This vector will be overwritten during the function. The result
* value in iv may be used as initialization vector for a chained ica_3des_cfb
* call with the same key if data_length in the preceding call is a multiple of
* lcfb.
* @param lcfb
* Length in bytes of the cipher feedback which is a value greater than or
* equal to 1 and less than or equal to the cipher block size (i.e. 8 for
* 3DES).
* @param direction
* 0 or 1:
* 0 Use the decrypt function.
* 1 Use the encrypt function.
*
* @return 0 on success
* EINVAL if at least one invalid parameter is given.
* EPERM if required hardware support is not available.
* EIO if the operation fails.
*/
ICA_EXPORT
unsigned int ica_3des_cfb(const unsigned char *in_data, unsigned char *out_data,
unsigned long data_length, unsigned char *key,
unsigned char *iv, unsigned int lcfb,
unsigned int direction);
/**
* Encrypt or decrypt data with an 3DES key using Counter (CTR) mode as
* described in NIST Special Publication 800-38A Chapter 6.5. With the counter
* mode each message block of size cipher block size (i.e. 8 bytes for 3DES) is
* combined with a counter value of the same size during encryption and
* decryption. Starting with an initial counter value to be combined with the
* first message block subsequent counter values to be combined with subsequent
* message blocks will be derived from preceding counter values by an increment
* function. The increment function used in ica_3des_ctr is s an arithmetic
* increment without carry on the U least significant bits in the counter
* where M is a parameter to ica_3des_ctr.
*
* Required HW Support
* KMCTR-TDEA-192
*
* @param in_data
* Pointer to a readable buffer, that contains the message to be en/decrypted.
* The size of the message in bytes is data_length. The size of this buffer in
* bytes must be at least as big as data_length.
* @param out_data
* Pointer to a writable buffer, that will contain the resulting en/decrypted
* message. The size of this buffer in bytes must be at least as big as
* data_length.
* @param data_length
* Length in bytes of the message to be en/decrypted, which resides at the
* beginning of in_data.
* @param key
* Pointer to a valid 3DES key of 24 bytes length.
* @param ctr
* Pointer to a readable and writable buffer of size cipher block size bytes.
* ctr contains an initialization value for a counter function and it will be
* replaced by a new value. That new value can be used as an initialization
* value for a counter function in a chained ica_3des_ctr call with the same
* key if data_length used in the preceding call is a multiple of the cipher
* block size.
* @param ctr_width
* A number U between 8 and cipher block size in bits. The value is used by the
* counter increment function which increments a counter value by incrementing
* without carry the least significant U bits of the counter value. The value
* must be a multiple of 8. When in FIPS mode, an additional counter overflow
* check is performed, so that the given data length, divided by the cipher
* block size, is not greater than 2 to the power of U.
* @param direction
* 0 or 1:
* 0 Use the decrypt function.
* 1 Use the encrypt function.
*
* @return 0 on success
* EINVAL if at least one invalid parameter is given.
* EPERM if required hardware support is not available.
* EIO if the operation fails.
*/
ICA_EXPORT
unsigned int ica_3des_ctr(const unsigned char *in_data, unsigned char *out_data,
unsigned long data_length,
unsigned char *key,
unsigned char *ctr, unsigned int ctr_width,
unsigned int direction);
/**
* Encrypt or decrypt data with an 3DES key using Counter (CTR) mode as
* described in NIST Special Publication 800-38A ,Chapter 6.5. With the counter
* mode each message block of size cipher block size is combined with a counter
* value of the same size during encryption and decryption. The
* ica_3des_ctrlist function assumes that a list n of precomputed counter
* values is provided where n is the smallest integer that is less or equal to
* the message size divided by the cipher block size. This function allows to
* optimally exploit System z HW support for non-standard counter functions.
*
* Required HW Support
* KMCTR-TDEA-192
*
* @param in_data
* Pointer to a readable buffer, that contains the message to be en/decrypted.
* The size of the message in bytes is data_length. The size of this buffer in
* bytes must be at least as big as data_length.
* @param out_data
* Pointer to a writable buffer, that will contain the resulting en/decrypted
* message. The size of this buffer in bytes must be at least as big as
* data_length.
* @param data_length
* Length in bytes of the message to be en/decrypted, which resides at the
* beginning of in_data. If data_length is a multiple of the cipher block size
* then calls of ica_3des_ctrlist with the same key can be chained if ctrlist
* argument of the chained call contains a list of counters that follows the
* counters used in the first call and data_length used in the preceding call
* is a multiple of the cipher block size.
* @param key
* Pointer to an 3DES key of 24 bytes length.
* @param ctrlist
* Pointer to a readable buffer of that is both of size greater than or equal
* to data_length and a multiple of the cipher block size (i.e. 8 bytes for
* 3DES). ctrlist should contain a list of precomputed counter values of size
* cipher block size each.
* @param direction
* 0 or 1:
* 0 Use the decrypt function.
* 1 Use the encrypt function.
*
* @return 0 on success
* EINVAL if at least one invalid parameter is given.
* EPERM if required hardware support is not available.
* EIO if the operation fails.
*/
ICA_EXPORT
unsigned int ica_3des_ctrlist(const unsigned char *in_data, unsigned char *out_data,
unsigned long data_length,
unsigned char *key,
const unsigned char *ctrlist,
unsigned int direction);
/**
* Encrypt or decrypt data with an 3DES key using Output Feedback (OFB) mode as
* described in NIST Special Publication 800-38A Chapter 6.4.
*
* Required HW Support
* KMO-TDEA-192
*
* @param in_data
* Pointer to a readable buffer, that contains the message to be en/decrypted.
* The size of the message in bytes is data_length. The size of this buffer in
* bytes must be at least as big as data_length.
* @param out_data
* Pointer to a writable buffer, that contains the resulting en/decrypted
* message. The size of this buffer in bytes must be at least as big as
* data_length.
* @param data_length
* Length in bytes of the message to be en/decrypted, which resides at the
* beginning of in_data.
* @param key
* Pointer to a valid 3DES key of 24 bytes length.
* @param iv
* Pointer to a valid initialization vector of cipher block size bytes (8 bytes
* for DES). This vector will be overwritten during the function. If
* data_length is a multiple of the cipher block size (i.e. a multiple of 8 for
* 3DES) the result value in iv may be used as initialization vector for a
* chained ica_3DES_ofb call with the same key.
* @param direction
* 0 or 1:
* 0 Use the decrypt function.
* 1 Use the encrypt function.
*
* @return 0 on success
* EINVAL if at least one invalid parameter is given.
* EPERM if required hardware support is not available.
* EIO if the operation fails.
*/
ICA_EXPORT
unsigned int ica_3des_ofb(const unsigned char *in_data, unsigned char *out_data,
unsigned long data_length, unsigned char *key,
unsigned char *iv, unsigned int direction);
/**
* Authenticate data or verify the authenticity of data with an 3DES key
* using the Block Cipher Based Message Authentication Code (CMAC) mode as
* described in NIST Special Publication 800-38B.
* ica_3des_cmac can be used to authenticate or verify the authenticity of a
* complete message.
*
* Required HW Support
* KMAC-TDEA-192
* PCC-Compute-Last_block-CMAC-Using-TDEA-192
*
* @param message
* Pointer to a readable buffer of size greater than or equal to
* message_length bytes. It contains a message to be authenticated or of
* which the authenticity shall be verified.
* @param message_length
* Length in bytes of the message to be authenticated or verified.
* @param mac
* Pointer to a buffer of size greater than or equal to mac_length bytes.
* If direction is 1 the buffer must be writable and a message authentication
* code for the message in message of size mac_length bytes will be written to
* the buffer.
* If direction is 0 the buffer must be readable and contain a message
* authentication code that will be verified against the message in message.
* @param mac_length
* Length in bytes of the message authentication code mac in bytes that is less
* than or equal to the cipher block size (i.e. 8 bytes for TDES). It is
* recommended to use values greater than or equal to 8.
* @param key
* Pointer to a valid 3DES key of 24 bytes length.
* @param direction
* 0 or 1:
* 0 Verify message authentication code
* 1 Compute message authentication code for the message
*
* @return 0 on success
* EINVAL if at least one invalid parameter is given.
* EPERM if required hardware support is not available.
* EIO if the operation fails.
* EFAULT if direction is 0 and the verification of the message authentication
* code fails.
*/
ICA_EXPORT
unsigned int ica_3des_cmac(const unsigned char *message, unsigned long message_length,
unsigned char *mac, unsigned int mac_length,
unsigned char *key,
unsigned int direction);
/**
* Authenticate data or verify the authenticity of data with an 3DES key using
* the Block Cipher Based Message Authentication Code (CMAC) mode as described
* in NIST Special Publication 800-38B.
* ica_3des_cmc_intermediate and ica_3des_cmac_last can be used when the
* message to be authenticated or to be verified using CMAC is supplied in
* multiple chunks. ica_3des_cmac_intermediate is used to process all but the
* last chunk. All message chunks to preprocessed by
* ica_3des_cmac_intermediate must have a size that is a multiple of the
* cipher block size (i.e a multiple of 8 bytes for 3DES).
* Note: ica_3des_cmac_intermediate has no direction argument it can be used
* during an authentication and during authenticity verification.
*
* Required HW Support
* KMAC-TDEA-192,
*
* @param message
* Pointer to a readable buffer of size greater than or equal to message_length
* bytes. It contains a non final part of a message which shall be
* authenticated or of which the authenticity shall be verified.
* @param message_length
* Length in bytes of the message part in message. It must be a multiple of the
* cipher block size.
* @param key
* Pointer to a valid 3DES key of 24 bytes length.
* @param iv
* Pointer to a valid initialization vector of size cipher block size
* (i.e. 8 bytes for 3DES). For the first message part it must be set to a
* string of zeros. For processing the n-th message part it must be the
* resulting iv value of the ica_3des_cmac_intermediate applied to the
* (n-1)-th message part. This vector will be overwritten during the function.
* The result value in iv may be used as initialization vector for a chained
* call to ica_3des_cmac_initermediate or to ica_3des_cmac_last with the same key.
*
* @return 0 on success
* EINVAL if at least one invalid parameter is given.
* EPERM if required hardware support is not available.
* EIO if the operation fails.
*/
ICA_EXPORT
unsigned int ica_3des_cmac_intermediate(const unsigned char *message, unsigned long message_length,
unsigned char *key,
unsigned char *iv);
/**
* Authenticate data or verify the authenticity of data with an 3DES key using
* the Block Cipher Based Message Authentication Code (CMAC) mode as described
* in NIST Special Publication 800-38B.
* ica_3des_cmac_last can be used to authenticate or verify the authenticity
* of a complete message or of the final part of a message for which all
* preceding parts were preprocessed with ica_3des_cmac_intermediate.
*
* Required HW Support
* KMAC-TDEA-192,
* PCC-Compute-Last_block-CMAC-Using-TDEA-192
*
* @param message
* Pointer to a readable buffer of size greater than or equal to message_length
* bytes. It contains a message or the final part of a message to be
* authenticated or of which the authenticity shall be verified.
* @param message_length
* Length in bytes of the message to be authenticated or verified.
* @param mac
* Pointer to a buffer of size greater than or equal to mac_length bytes.
* If direction is 1 the buffer must be writable and a message authentication
* code for the message in message of size mac_length bytes will be written to
* the buffer.
* If direction is 0 the buffer must be readable and contain a message
* authentication code that will be verified against the message in message.
* @param mac_length
* Length in bytes of the message authentication code mac in bytes that is
* less than or equal to the cipher block size (I.e. 8 bytes for DES). It is
* recommended to use values greater than or equal to 8.
* @param key
* Pointer to a valid 3DES key of 24 bytes length.
* @param iv
* Pointer to a valid initialization vector of size cipher block size. If iv
* is NULL message is assumed to be the complete message to be processed.
* Otherwise message is the final part of a composite message to be processed
* and iv contains the output vector resulting from processing all previous
* parts with chained calls to ica_3des_cmac_intermediate, i.e. the value
* returned in iv of the ica_3des_cmac_intermediate call applied to the
* penultimate message part.
* @param direction
* 0 or 1:
* 0 Verify message authentication code
* 1 Compute message authentication code for the message
*
* @return 0 on success
* EINVAL if at least one invalid parameter is given.
* EPERM if required hardware support is not available.
* EIO if the operation fails.
* EFAULT if direction is 0 and the verification of the message authentication
* code fails.
*/
ICA_EXPORT
unsigned int ica_3des_cmac_last(const unsigned char *message, unsigned long message_length,
unsigned char *mac, unsigned int mac_length,
unsigned char *key, unsigned char *iv,
unsigned int direction);
/**
* Encrypt or decrypt data with an AES key using Electronic Cook Book (ECB)
* mode as described in NIST Special Publication 800-38A Chapter 6.1.
*
* Required HW Support
* KM-AES-128, KM-AES-192 or KM-AES-256
*
* @param in_data
* Pointer to a readable buffer, that contains the message to be en/decrypted.
* The size of the message in bytes is data_length. The size of this buffer in
* bytes must be at least as big as data_length.
* @param out_data
* Pointer to a writable buffer, that will contain the resulting en/decrypted
* message. The size of this buffer in bytes must be at least as big as
* data_length.
* @param data_length
* Length in bytes of the message to be en/decrypted, which resides at the
* beginning of in_data. data_length must be a multiple of the cipher block
* size (i.e. a multiple of 16 for AES).
* @param key
* Pointer to a valid AES key.
* @param key_length
* Length in bytes of the AES key. Supported sizes are 16, 24, and 32 for
* AES-128, AES-192 and AES-256 respectively. Therefore, you can use the
* macros: AES_KEY_LEN128, AES_KEY_LEN192, and AES_KEY_LEN256.
* @param direction
* 0 or 1:
* 0 Use the decrypt function.
* 1 Use the encrypt function.
*
* @return 0 on success
* EINVAL if at least one invalid parameter is given.
* EIO if the operation fails.
*/
ICA_EXPORT
unsigned int ica_aes_ecb(const unsigned char *in_data, unsigned char *out_data,
unsigned long data_length, unsigned char *key,
unsigned int key_length,
unsigned int direction);
/**
* Encrypt or decrypt data with an AES key using Cipher Block Chaining (CBC)
* mode as described in NIST Special Publication 800-38A Chapter 6.2.
*
* Required HW Support
* KMC-AES-128, KMC-AES-192 or KMC-AES-256
*
* @param in_data
* Pointer to a readable buffer, that contains the message to be en/decrypted.
* The size of the message in bytes is data_length. The size of this buffer in
* bytes must be at least as big as data_length.
* @param out_data
* Pointer to a writable buffer, that will contain the resulting en/decrypted
* message. The size of this buffer in bytes must be at least as big as
* data_length.
* @param data_length
* Length in bytes of the message to be en/decrypted, which resides at the
* beginning of in_data. data_length must be a multiple of the cipher block
* size (i.e. a multiple of 16 for AES).
* @param key
* Pointer to a valid AES key.
* @param key_length
* Length in bytes of the AES key. Supported sizes are 16, 24, and 32 for
* AES-128, AES-192 and AES-256 respectively. Therefore, you can use the
* macros: AES_KEY_LEN128, AES_KEY_LEN192, and AES_KEY_LEN256.
* @param iv
* Pointer to a valid initialization vector of size cipher block size. This
* vector will be overwritten during the function. The result value in iv may
* be used as initialization vector for a chained ica_aes_cbc call with the
* same key.
* @param direction
* 0 or 1:
* 0 Use the decrypt function.
* 1 Use the encrypt function.
*
* @return 0 on success
* EINVAL if at least one invalid parameter is given.
* EIO if the operation fails.
*/
ICA_EXPORT
unsigned int ica_aes_cbc(const unsigned char *in_data, unsigned char *out_data,
unsigned long data_length, unsigned char *key,
unsigned int key_length, unsigned char *iv,
unsigned int direction);
/**
* Encrypt or decrypt data with an AES key using Cipher Block Chaining with
* Ciphertext Stealing (CBC-CS) mode as described in NIST Special Publication
* 800-38A Chapter 6.2 and the Addendum to NIST Special Publication 800-38A on
* "Recommendation for Block Cipher Modes of Operation: Three Variants of
* Ciphertext Stealing for CBC Mode":
* ica_aes_cbc_cs may be used to encrypt or decrypt the last chunk of a
* message consisting of multiple chunks where all but the last chunk are
* encrypted or decrypted by chained calls to ica_aes_cbc and the resulting
* iv of the last call to ica_aes_cbc is fed into the iv of the
* ica_aes_cbc_cs call provided the chunk is greater than cipher block size
* (greater than 16 bytes for AES).
*
* Required HW Support
* KMC-AES-128, KMC-AES-192 or KMC-AES-256
*
* @param in_data
* Pointer to a readable buffer, that contains the message to be en/decrypted.
* The size of the message in bytes is data_length. The size of this buffer
* in bytes must be at least as big as data_length.
* @param out_data
* Pointer to a writable buffer, that will contain the resulting en/decrypted
* message. The size of this buffer in bytes must be at least as big as
* data_length.
* @param data_length
* Length in bytes of the message to be en/decrypted, which resides at the
* beginning of in_data. data_length must be greater than or equal to the
* cipher block size (i.e. a multiple of 16 bytes for AES).
* @param key
* Pointer to a valid AES key.
* @param key_length
* Length in bytes of the AES key. Supported sizes are 16, 24, and 32 for
* AES-128, AES-192 and AES-256 respectively. Therefore, you can use the
* macros:
* AES_KEY_LEN128, AES_KEY_LEN192, and AES_KEY_LEN256.
* @param iv
* Pointer to a valid initialization vector of cipher block size bytes. This
* vector will be overwritten during the function. For variant equals 1 or
* variant equals 2 the result value in iv may be used as initialization vector
* for a chained ica_aes_cbc call with the same key if data_length is a
* multiple of the cipher block size.
* @param direction
* 0 or 1:
* 0 Use the decrypt function.
* 1 Use the encrypt function.
* @param variant
* 1 Use variant CBC-CS1 of the Addendum to NIST Special Publication 800-38A
* to encrypt or decrypt the message: keep last two blocks in order.
* 2 Use variant CBC-CS2 of the Addendum to NIST Special Publication 800-38A
* to encrypt or decrypt the message: switch order of the last two blocks
* if data_length is not a multiple of the cipher block size (i.e. a
* multiple of 8 for DES).
* 3 Use variant CBC-CS3 of the Addendum to NIST Special Publication 800-38A
* to encrypt or decrypt the message: always switch order of the last two
* blocks.
*
* @return 0 on success
* EINVAL if at least one invalid parameter is given.
* EPERM if required hardware support is not available.
* EIO if the operation fails.
*/
ICA_EXPORT
unsigned int ica_aes_cbc_cs(const unsigned char *in_data, unsigned char *out_data,
unsigned long data_length,
unsigned char *key, unsigned int key_length,
unsigned char *iv,
unsigned int direction, unsigned int variant);
/**
* Encrypt or decrypt data with an AES key using Cipher Feedback (CFB) mode as
* described in NIST Special Publication 800-38A Chapter 6.3.
*
* Required HW Support
* KMF-AES-128, KMF-AES-192 or KMF-AES-256
*
* @param in_data
* Pointer to a readable buffer, that contains the message to be en/decrypted.
* The size of the message in bytes is data_length. The size of this buffer in
* bytes must be at least as big as data_length.
* @param out_data
* Pointer to a writable buffer, that will contain the resulting en/decrypted
* message. The size of this buffer in bytes must be at least as big as
* data_length.
* @param data_length
* Length in bytes of the message to be en/decrypted, which resides at the
* beginning of in_data.
* @param key
* Pointer to a valid AES key.
* @param key_length
* Length in bytes of the AES key. Supported sizes are 16, 24, and 32 for
* AES-128, AES-192 and AES-256 respectively. Therefore, you can use the
* macros: AES_KEY_LEN128, AES_KEY_LEN192, and AES_KEY_LEN256.
* @param iv
* Pointer to a valid initialization vector of cipher block size bytes (16
* bytes for AES). This vector will be overwritten during the function. The
* result value in iv may be used as initialization vector for a chained
* ica_aes_cfb call with the same key if data_length in the preceding call is a
* multiple of lcfb.
* @param lcfb
* Length in bytes of the cipher feedback which is a value greater than or
* equal to 1 and less than or equal to the cipher block size (i.e. 16 for
* AES).
* @param direction
* 0 or 1:
* 0 Use the decrypt function.
* 1 Use the encrypt function.
*
* @return 0 on success
* EINVAL if at least one invalid parameter is given.
* EPERM if required hardware support is not available.
* EIO if the operation fails.
*/
ICA_EXPORT
unsigned int ica_aes_cfb(const unsigned char *in_data, unsigned char *out_data,
unsigned long data_length, unsigned char *key,
unsigned int key_length, unsigned char *iv, unsigned int lcfb,
unsigned int direction);
/**
* Encrypt or decrypt data with an AES key using Counter (CTR) mode as
* described in NIST Special Publication 800-38A Chapter 6.5. With the counter
* mode each message block of size cipher block size (i.e. 16 bytes for AES) is
* combined with a counter value of the same size during encryption and
* decryption. Starting with an initial counter value to be combined with the
* first message block subsequent counter values to be combined with subsequent
* message blocks will be derived from preceding counter values by an increment
* function. The increment function used in ica_aes_ctr is s an arithmetic
* increment without carry on the U least significant bits in the counter
* where M is a parameter to ica_aes_ctr.
*
* Required HW Support
* KMCTR-AES-128, KMCTR-AES-192 or KMCTR-AES-256
*
* @param in_data
* Pointer to a readable buffer, that contains the message to be en/decrypted.
* The size of the message in bytes is data_length. The size of this buffer in
* bytes must be at least as big as data_length.
* @param out_data
* Pointer to a writable buffer, that will contain the resulting en/decrypted
* message. The size of this buffer in bytes must be at least as big as
* data_length.
* @param data_length
* Length in bytes of the message to be en/decrypted, which resides at the
* beginning of in_data.
* @param key
* Pointer to a valid AES key.
* @param key_length
* Length in bytes of the AES key. Supported sizes are 16, 24, and 32 for
* AES-128, AES-192 and AES-256 respectively. Therefore, you can use the
* macros: AES_KEY_LEN128, AES_KEY_LEN192, and AES_KEY_LEN256.
* @param ctr
* Pointer to a readable and writable buffer of size cipher block size bytes.
* ctr contains an initialization value for a counter function and it will be
* replaced by a new value. That new value can be used as an initialization
* value for a counter function in a chained ica_aes_ctr call with the same key
* if data_length used in the preceding call is a multiple of the cipher block
* size.
* @param ctr_width
* A number U between 8 and cipher block size in bits. The value is used by the
* counter increment function which increments a counter value by incrementing
* without carry the least significant U bits of the counter value. The value
* must be a multiple of 8. When in FIPS mode, an additional counter overflow
* check is performed, so that the given data length, divided by the cipher
* block size, is not greater than 2 to the power of U.
* @param direction
* 0 or 1:
* 0 Use the decrypt function.
* 1 Use the encrypt function.
*
* @return 0 on success
* EINVAL if at least one invalid parameter is given.
* EPERM if required hardware support is not available.
* EIO if the operation fails.
*/
ICA_EXPORT
unsigned int ica_aes_ctr(const unsigned char *in_data, unsigned char *out_data,
unsigned long data_length,
unsigned char *key, unsigned int key_length,
unsigned char *ctr, unsigned int ctr_width,
unsigned int direction);
/**
* Encrypt or decrypt data with an AES key using Counter (CTR) mode as
* described in NIST Special Publication 800-38A ,Chapter 6.5. With the counter
* mode each message block of size cipher block size is combined with a counter
* value of the same size during encryption and decryption. The ica_aes_ctrlist
* function assumes that a list n of precomputed counter values is provided
* where n is the smallest integer that is less or equal to the message size
* divided by the cipher block size. This function allows to optimally exploit
* System z HW support for non-standard counter functions.
*
* Required HW Support
* KMCTR-AES-128, KMCTR-AES-192 or KMCTR-AES-256
*
* @param in_data
* Pointer to a readable buffer, that contains the message to be en/decrypted.
* The size of the message in bytes is data_length. The size of this buffer in
* bytes must be at least as big as data_length.
* @param out_data
* Pointer to a writable buffer, that will contain the resulting en/decrypted
* message. The size of this buffer in bytes must be at least as big as
* data_length.
* @param data_length
* Length in bytes of the message to be en/decrypted, which resides at the
* beginning of in_data. If data_length is a multiple of the cipher block size
* then calls of ica_aes_ctrlist with the same key can be chained if ctrlist
* argument of the chained call contains a list of counters that follows the
* counters used in the first call and data_length used in the preceding call
* is a multiple of the cipher block size.
* @param key
* Pointer to a valid AES key.
* @param key_length
* Length in bytes of the AES key. Supported sizes are 16, 24, and 32 for
* AES-128, AES-192 and AES-256 respectively. Therefore, you can use the
* macros: AES_KEY_LEN128, AES_KEY_LEN192, and AES_KEY_LEN256.
* @param ctrlist
* Pointer to a readable buffer of that is both of size greater than or equal
* to data_length and a multiple of the cipher block size (i.e. 16 bytes for
* AES). ctrlist should contain a list of precomputed counter values of size
* cipher block size each.
* @param direction
* 0 or 1:
* 0 Use the decrypt function.
* 1 Use the encrypt function.
*
* @return 0 on success
* EINVAL if at least one invalid parameter is given.
* EPERM if required hardware support is not available.
* EIO if the operation fails.
*/
ICA_EXPORT
unsigned int ica_aes_ctrlist(const unsigned char *in_data, unsigned char *out_data,
unsigned long data_length,
unsigned char *key, unsigned int key_length,
const unsigned char *ctrlist,
unsigned int direction);
/**
* Encrypt or decrypt data with an AES key using Output Feedback (OFB) mode as
* described in NIST Special Publication 800-38A Chapter 6.4.
*
* Required HW Support
* KMO-AES-128, KMO-AES-192 or KMO-AES-256
*
* @param in_data
* Pointer to a readable buffer, that contains the message to be en/decrypted.
* The size of the message in bytes is data_length. The size of this buffer in
* bytes must be at least as big as data_length.
* @param out_data
* Pointer to a writable buffer, that contains the resulting en/decrypted
* message. The size of this buffer in bytes must be at least as big as
* data_length.
* @param data_length
* Length in bytes of the message to be en/decrypted, which resides at the
* beginning of in_data.
* @param key
* Pointer to a valid AES key.
* @param key_length
* Length in bytes of the AES key. Supported sizes are 16, 24, and 32 for
* AES-128, AES-192 and AES-256 respectively. Therefore, you can use the
* macros: AES_KEY_LEN128, AES_KEY_LEN192, and AES_KEY_LEN256.
* @param iv
* Pointer to a valid 16 byte initialization vector. This vector will be
* overwritten during the function. If data_length is a multiple of the cipher
* block size (i.e. a multiple of 16 for AES) the result value in iv may be
* used as initialization vector for a chained ica_aes_ofb call with the same
* key.
* @param direction
* 0 or 1:
* 0 Use the decrypt function.
* 1 Use the encrypt function.
*
* @return 0 on success
* EINVAL if at least one invalid parameter is given.
* EPERM if required hardware support is not available.
* EIO if the operation fails.
*/
ICA_EXPORT
unsigned int ica_aes_ofb(const unsigned char *in_data, unsigned char *out_data,
unsigned long data_length, unsigned char *key,
unsigned int key_length, unsigned char *iv,
unsigned int direction);
/**
* Authenticate data or verify the authenticity of data with an AES key using
* the Block Cipher Based Message Authentication Code (CMAC) mode as described
* in NIST Special Publication 800-38B. ica_aes_cmac can be used to
* authenticate or verify the authenticity of a complete message.
*
* Required HW Support
* KMAC-AES-128, KMAC-AES-192 or KMAC-AES-256
* PCC-Compute-Last_block-CMAC-Using-AES-128,
* PCC-Compute-Last_block-CMAC-Using-AES-192 or
* PCC-Compute-Last_block-CMAC-Using-AES-256
*
* @param message
* Pointer to a readable buffer of size greater than or equal to message_length
* bytes. It contains a message to be authenticated or of which the
* authenticity shall be verified.
* @param message_length
* Length in bytes of the message to be authenticated or verified.
* @param mac
* Pointer to a buffer of size greater than or equal to mac_length bytes. If
* direction is 1 the buffer must be writable and a message authentication code
* for the message in message of size mac_length bytes will be written to the
* buffer. If direction is 0 the buffer must be readable and contain a message
* authentication code that will be verified against the message in message
* @param mac_length
* Length in bytes of the message authentication code mac in bytes that is less
* than or equal to the cipher block size (I.e. 16 bytes for AES). It is
* recommended to use values greater than or equal to 8.
* @param key
* Pointer to a valid AES key.
* @param key_length
* Length in bytes of the AES key. Supported sizes are 16, 24, and 32 for
* AES-128, AES-192 and AES-256 respectively. Therefore, you can use the
* macros: AES_KEY_LEN128, AES_KEY_LEN192, and AES_KEY_LEN256.
* @param direction
* 0 or 1:
* 0 Verify message authentication code
* 1 Compute message authentication code for the message
*
* @return 0 on success
* EINVAL if at least one invalid parameter is given.
* EPERM if required hardware support is not available.
* EIO if the operation fails.
* EFAULT if direction is 0 and the verification of the message authentication code fails.
*/
ICA_EXPORT
unsigned int ica_aes_cmac(const unsigned char *message, unsigned long message_length,
unsigned char *mac, unsigned int mac_length,
unsigned char *key, unsigned int key_length,
unsigned int direction);
/**
* Authenticate data or verify the authenticity of data with an AES key using
* the Block Cipher Based Message Authentication Code (CMAC) mode as described
* in NIST Special Publication 800-38B.
* ica_aes_cmc_intermediate and ica_aes_cmac_last can be used when the message
* to be authenticated or to be verified using CMAC is supplied in multiple
* chunks. ica_aes_cmac_intermediate is used to process all but the last
* chunk. All message chunks to preprocessed by ica_aes_cmac_intermediate
* must have a size that is a multiple of the cipher block size (i.e. a
* multiple of 16 bytes for AES).
* Note: ica_aes_cmac_intermediate has no direction argument it can be used
* during an authentication and during authenticity verification.
*
* Required HW Support
* KMAC-AES-128, KMAC-AES-192 or KMAC-AES-256
*
* @param message
* Pointer to a readable buffer of size greater than or equal to message_length
* bytes. It contains a non final part of a message which shall be
* authenticated or of which the authenticity shall be verified.
* @param message_length
* Length in bytes of the message part in message. It must be a multiple of
* the cipher block size.
* @param key
* Pointer to a valid AES key.
* @param key_length
* Length in bytes of the AES key. Supported sizes are 16, 24, and 32 for
* AES-128, AES-192 and AES-256 respectively. Therefore, you can use the
* macros: AES_KEY_LEN128, AES_KEY_LEN192, and AES_KEY_LEN256.
* @param iv
* Pointer to a valid initialization vector of size cipher block size (i.e.
* 16 bytes for AES). For the first message part it must be set to a string
* of zeros. For processing the n-th message part it must be the resulting iv
* value of the ica_aes_cmac_intermediate applied to the (n-1)-th message
* part. This vector will be overwritten during the function.
* The result value in iv may be used as initialization vector for a chained
* call to ica_aes_cmac_initermediate or to ica_aes_cmac_last with the
* same key.
*
* @return 0 on success
* EINVAL if at least one invalid parameter is given.
* EPERM if required hardware support is not available.
* EIO if the operation fails.
*/
ICA_EXPORT
unsigned int ica_aes_cmac_intermediate(const unsigned char *message,
unsigned long message_length,
unsigned char *key, unsigned int key_length,
unsigned char *iv);
/**
* Authenticate data or verify the authenticity of data with an AES key using
* the Block Cipher Based Message Authentication Code (CMAC) mode as
* described in NIST Special Publication 800-38B.
* ica_aes_cmac_last can be used to authenticate or verify the authenticity of
* a complete message or of the final part of a message for which all
* preceding parts were preprocessed with ica_aes_cmac_intermediate.
*
* Required HW Support
* KMAC-AES-128, KMAC-AES-192 or KMAC-AES-256
* PCC-Compute-Last_block-CMAC-Using-AES-128,
* PCC-Compute-Last_block-CMAC-Using-AES-192 or
* PCC-Compute-Last_block-CMAC-Using-AES-256.
*
* @param message
* Pointer to a readable buffer of size greater than or equal to message_length
* bytes. It contains a message or the final part of a message to be
* authenticated or of which the authenticity shall be verified.
* @param message_length
* Length in bytes of the message to be authenticated or verified.
* @param mac
* Pointer to a buffer of size greater than or equal to mac_length bytes.
* If direction is 1 the buffer must be writable and a message authentication
* code for the message in message of size mac_length bytes will be written to
* the buffer.
* If direction is 0 the buffer must be readable and contain a message
* authentication code that will be verified against the message in message.
* @param mac_length
* Length in bytes of the message authentication code mac in bytes that is less
* than or equal to the cipher block size (I.e. 16 bytes for AES). It is
* recommended to use values greater than or equal to 8.
* @param key
* Pointer to a valid AES key.
* @param key_length
* Length in bytes of the AES key. Supported sizes are 16, 24, and 32 for
* AES-128, AES-192 and AES-256 respectively. Therefore, you can use the
* macros: AES_KEY_LEN128, AES_KEY_LEN192, and AES_KEY_LEN256.
* @param iv
* Pointer to a valid initialization vector of size cipher block size. If iv
* is NULL message is assumed to be the complete message to be processed.
* Otherwise message is the final part of a composite message to be processed
* and iv contains the output vector resulting from processing all previous
* parts with chained calls to ica_aes_cmac_intermediate, i.e. the value
* returned in iv of the ica_aes_cmac_intermediate call applied to the
* penultimate message part.
* @param direction
* 0 or 1:
* 0 Verify message authentication code
* 1 Compute message authentication code for the message
*
* @return 0 on success
* EINVAL if at least one invalid parameter is given.
* EPERM if required hardware support is not available.
* EIO if the operation fails.
* EFAULT if direction is 0 and the verification of the message authentication
* code fails.
*/
ICA_EXPORT
unsigned int ica_aes_cmac_last(const unsigned char *message, unsigned long message_length,
unsigned char *mac, unsigned int mac_length,
unsigned char *key, unsigned int key_length,
unsigned char *iv,
unsigned int direction);
/**
* Encrypt or decrypt data with an AES key using the XEX Tweakable Bloc Cipher
* with Ciphertext Stealing (XTS) mode as described in NIST Special Publication
* 800-38E and IEEE standard 1619-2007.
*
* Required HW Support
* KM-XTS-AES-128 or KM-XTS-AES-256
* PCC-Compute-XTS-Parameter-Using-AES-128 or
* PCC-Compute-XTS-Parameter-Using-AES-256
*
* @param in_data
* Pointer to a readable buffer, that contains the message to be en/decrypted.
* The size of the message in bytes is data_length. The size of this buffer in
* bytes must be at least as big as data_length.
* @param out_data
* Pointer to a writable buffer, that will contain the resulting en/decrypted
* message. The size of this buffer in bytes must be at least as big as
* data_length.
* @param data_length
* Length in bytes of the message to be en/decrypted, which resides at the
* beginning of in_data. The minimal value of data_length is cipher block size
* (i.e. a multiple of 16 for AES).
* @param key1
* Pointer to a buffer containing a valid AES key. key1 is used for the actual
* encryption of the message buffer combined some vector computed from the
* tweek value (Key1 in IEEE Std 1619-2007).
* @param key2
* Pointer to a buffer containing a valid AES key key2 is used to encrypt the
* tweak (Key2 in IEEE Std 1619-2007).
* @param key_length
* The length in bytes of the AES key. For XTS supported AES key sizes are 16
* and 32 for AES-128 and AES-256 respectively.
* @param tweak
* Pointer to a valid 16 byte tweak value (as in IEEE Std 1619-2007). This
* tweak will be overwritten during the function. If data_length is a multiple
* of the cipher block size the result value in tweak may be used as tweak
* value for a chained ica_aes_xts call with the same key pair.
* @param direction
* 0 or 1:
* 0 Use the decrypt function.
* 1 Use the encrypt function.
*
* @return 0 on success
* EINVAL if at least one invalid parameter is given.
* EPERM if required hardware support is not available.
* EIO if the operation fails.
*/
ICA_EXPORT
unsigned int ica_aes_xts(const unsigned char *in_data, unsigned char *out_data,
unsigned long data_length,
unsigned char *key1, unsigned char *key2,
unsigned int key_length, unsigned char *tweak,
unsigned int direction);
/**
* Encrypt and authenticate or decrypt data and check authenticity of data with
* an AES key using Counter with Cipher Block Chaining Message Authentication
* Code (CCM) mode as described in NIST Special Publication 800-38C.
* Formatting and counter functions are implemented according to
* NIST 800-38C Appendix A.
*
* Required HW Support
* KMCTR-AES-128, KMCTR-AES-192 or KMCTR-AES-256
* KMAC-AES-128, KMAC-AES-192 or KMAC-AES-256
*
* @param payload
* Pointer to a buffer of size greater than or equal to payload_length bytes.
* If direction equals 1 the payload buffer must be readable and contain a
* payload message of size payload_length that will be encrypted.
* If direction equals 0 the payload buffer must be writable. If the
* authentication verification succeeds the decrypted message in the most
* significant payload_length bytes of ciphertext_n_mac will be written to
* the buffer otherwise the contents of the buffer will be undefined.
* @param payload_length
* Length in bytes of the message to be en/decrypted, it may be 0 unless
* assoc_data_length is 0.
* @param ciphertext_n_mac
* Pointer to a buffer of size greater than or equal to payload_length plus
* mac_length bytes.
* If direction equals 1 then the buffer must be writable and the encrypted
* message from payload followed by the message authentication code for the
* nonce, the payload and associated data will be written to that buffer.
* If direction equals 0 then the buffer is readable and contains an encrypted
* message of length payload_length followed by a message authentication code
* of length mac_length.
* @param mac_length
* Length in bytes of the message authentication code in bytes.
* Valid values are 4, 6, 8, 10, 12, 16.
* @param assoc_data
* Pointer to a readable buffer of size greater than or equal to
* assoc_data_length bytes. The associated data in the most significant
* assoc_data_lenght bytes is subject to the authentication code computation
* but will not be encrypted.
* @param assoc_data_length
* Length of the associated data in assoc_data. It may be 0 unless
* payload_length is 0.
* @param nonce
* Pointer to readable buffer of size greater than or equal to nonce_length
* bytes that contains a nonce of size nonce_length bytes.
* @param nonce_length
* Length of the nonce in nonce in bytes. Valid values a greater than 6 and
* less than 14.
* @param key
* Pointer to a valid AES key.
* @param key_length
* Length in bytes of the AES key. Supported sizes are 16, 24, and 32 for
* AES-128, AES-192 and AES-256 respectively. Therefore, you can use the
* macros: AES_KEY_LEN128, AES_KEY_LEN192, and AES_KEY_LEN256.
* @param direction
* 0 or 1:
* 0 Verify message authentication code and decrypt encrypted payload.
* 1 Encrypt payload and compute message authentication code for the nonce,
* the associated data and the payload.
*
* @return 0 on success
* EINVAL if at least one invalid parameter is given.
* EPERM if required hardware support is not available.
* EIO if the operation fails.
* EFAULT if direction is 0 and the verification of the message authentication
* code fails.
*/
ICA_EXPORT
unsigned int ica_aes_ccm(unsigned char *payload, unsigned long payload_length,
unsigned char *ciphertext_n_mac, unsigned int mac_length,
const unsigned char *assoc_data, unsigned long assoc_data_length,
const unsigned char *nonce, unsigned int nonce_length,
unsigned char *key, unsigned int key_length,
unsigned int direction);
/**
* This parameter description applies to:
* ica_aes_gcm(), ica_aes_gcm_initialize(),
* ica_aes_gcm_intermediate() and ica_aes_gcm_last()
*
* Encrypt and authenticate or decrypt data and check authenticity data with
* an AES key using the Galois/Counter (GCM) mode as described in NIST Special
* Publication 800-38D.
* If no message needs to be encrypted or decrypted and only authentication or
* authentication checks are requested then this method implements the GMAC
* mode.
*
* Required HW Support
* KM-AES-128, KM-AES-192 or KM-AES-256
* KIMD-GHASH
* KMCTR-AES-128, KMCTR_AES-192 or KMCTR-AES-256
*
* @param plaintext
* Pointer to a buffer of size greater than or equal to plaintext_length bytes.
* If direction equals 1 the plaintext buffer must be readable and contain a
* payload message of size plaintext_length that will be encrypted.
* If direction equals 0 the plaintext buffer must be writable. If the
* authentication verification succeeds the decrypted message in the most
* significant plaintext_length bytes of ciphertext will be written to the
* buffer otherwise the contents of the buffer will be undefined.
* @param plaintext_length
* Length in bytes of the message to be en/decrypted. It must be equal or
* greater than 0 and less than (2^36)-32.
* In case of intermediate operations the length must not be multiple of
* blocksize. Padding will be done automatically. Be aware that this is only
* useful when this is the last block.
* @param ciphertext
* Pointer to a buffer of size greater than or equal to plaintext_length
* bytes.
* If direction equals 1 then the buffer must be writable and the encrypted
* message from plaintext will be written to that buffer.
* If direction equals 0 then the buffer is readable and contains an encrypted
* message of length plaintext_length.
* @param iv
* Pointer to a readable buffer of size greater than or equal to iv_length
* bytes, that contains an initialization vector of size iv_length.
* @param iv_length
* Length in bytes of the initialization vector in iv. It must be greater
* than 0 and less than 2^61. A length of 12 is recommended.
* @param aad
* Pointer to a readable buffer of size greater than or equal to aad_length
* bytes. The additional authenticated data in the most significant aad_length
* bytes is subject to the authentication code computation but will not be
* encrypted.
* @param aad_length
* Length in bytes of the additional authenticated data in aad. It must be
* equal or greater than 0 and less than 2^61.
* In case of ica_aes_gcm_last(), 'aad_length' contains the overall
* length of authentication data, cumulated over all intermediate operations.
* @param tag
* Pointer to a buffer of size greater than or equal to tag_length bytes.
* If direction is 1 the buffer must be writable and a message authentication
* code for the additional authenticated data in aad and the plain text in
* plaintext of size tag_length bytes will be written to the buffer.
* If direction is 0 the buffer must be readable and contain a message
* authentication code that will be verified against the additional
* authenticated data in aad and decrypted cipher text from ciphertext.
* In case of intermediate operations, ica_aes_gcm_intermediate() or
* ica_aes_gcm_last(), 'tag' contains the temporary hash/tag value.
* @param tag_length
* Length in bytes of the message authentication code tag in bytes.
* Valid values are 4, 8, 12, 13, 14, 15, 16.
* @param key
* Pointer to a valid AES key.
* @param key_length
* Length in bytes of the AES key. Supported sizes are 16, 24, and 32 for
* AES-128, AES-192 and AES-256 respectively. Therefore, you can use the
* macros: AES_KEY_LEN128, AES_KEY_LEN192, and AES_KEY_LEN256.
* @param icb
* initial counter block - Pointer to a writable buffer that will be created
* during ica_aes_gcm_initialize() and will be used in ica_aes_gcm_last() for
* the final tag computation.
* The length of this counter block is AES_BLOCK_SIZE (16 bytes).
* @param ucb
* usage counter block - Pointer to a writable buffer that will be created
* during ica_aes_gcm_initialize() and will be updated (increased) during the
* intermediate update operations.
* The length of this counter block is AES_BLOCK_SIZE (16 bytes).
* @param subkey
* Pointer to a writable buffer, generated in ica_aes_gcm_initialize() and used in
* ica_aes_gcm_intermediate() and ica_aes_gcm_last().
* The length of this buffer is AES_BLOCK_SIZE (16 bytes).
* @param ciph_length
* Length in bytes of the overall ciphertext, cumulated over all intermediate
* operations.
* @param final_tag
* Pointer to a readable buffer of size greater than or equal to
* final_tag_length bytes. If direction is 1 the buffer is not used.
* If direction is 0 this message authentication code (tag) will be verified
* with the computed message authentication code computed over the intermediate
* update operations.
* @param final_tag_length
* Length in bytes of the final message authentication code (tag).
* @param direction
* 0 or 1:
* 0 Verify message authentication code and decrypt encrypted payload.
* 1 Encrypt payload and compute message authentication code for the additional
* authenticated data and the payload.
*
* @return 0 on success
* EINVAL if at least one invalid parameter is given.
* EPERM if required hardware support is not available.
* EIO if the operation fails.
* EFAULT if direction is 0 and the verification of the message authentication
* code fails.
*/
ICA_EXPORT
unsigned int ica_aes_gcm(unsigned char *plaintext, unsigned long plaintext_length,
unsigned char *ciphertext,
const unsigned char *iv, unsigned int iv_length,
const unsigned char *aad, unsigned long aad_length,
unsigned char *tag, unsigned int tag_length,
unsigned char *key, unsigned int key_length,
unsigned int direction);
ICA_EXPORT
unsigned int ica_aes_gcm_initialize(const unsigned char *iv,
unsigned int iv_length,
unsigned char *key, unsigned int key_length,
unsigned char *icb, unsigned char *ucb,
unsigned char *subkey, unsigned int direction);
ICA_EXPORT
unsigned int ica_aes_gcm_intermediate(unsigned char *plaintext,
unsigned long plaintext_length, unsigned char *ciphertext,
unsigned char *ucb,
unsigned char *aad, unsigned long aad_length,
unsigned char *tag, unsigned int tag_length,
unsigned char *key, unsigned int key_length,
unsigned char *subkey, unsigned int direction);
ICA_EXPORT
unsigned int ica_aes_gcm_last(unsigned char *icb, unsigned long aad_length,
unsigned long ciph_length, unsigned char *tag,
unsigned char *final_tag, unsigned int final_tag_length,
unsigned char *key, unsigned int key_length,
unsigned char *subkey, unsigned int direction);
/*******************************************************************************
*
* New gcm API based on KMA.
*/
typedef struct kma_ctx_t kma_ctx;
/**
* Allocate a gcm context. This context is used by ica_aes_gcm_kma_init(),
* ica_aes_gcm_kma_update(), ica_aes_gcm_kma_get_tag(), and
* ica_aes_gcm_kma_verify_tag(). It must be freed by
* ica_aes_gcm_kma_ctx_free() when no longer needed.
*
* @return Pointer to opaque kma_ctx structure if success.
* NULL if no memory could be allocated.
*/
ICA_EXPORT
kma_ctx* ica_aes_gcm_kma_ctx_new();
/**
* Initialize the GCM context.
*
* @param direction
* 0 or 1:
* 0 when initialized for decryption.
* 1 when initialized for encryption.
*
* @param iv
* Pointer to a readable buffer of size greater than or equal to iv_length
* bytes, that contains an initialization vector of size iv_length.
*
* @param iv_length
* Length in bytes of the initialization vector in iv. It must be greater
* than 0 and less than 2^61. A length of 12 is recommended.
*
* @param key
* Pointer to a valid AES key.
*
* @param key_length
* Length in bytes of the AES key. Supported sizes are 16, 24, and 32 for
* AES-128, AES-192 and AES-256 respectively. Therefore, you can use the
* macros: AES_KEY_LEN128, AES_KEY_LEN192, and AES_KEY_LEN256.
*
* @param ctx
* Pointer to a previously allocated gcm context. This buffer is internally used
* as a working area by all other ica_aes_gcm_kma API functions and must not be
* changed by the application. The ctx must be established by calling ica_aes_gcm_ctx_new()
* before any call to any other ica_aes_gcm_kma function, and must be freed by calling
* ica_aes_gcm_ctx_free() after the last call to any ica_aes_gcm_kma function.
*
* @return 0 on success
* EINVAL if at least one invalid parameter is given.
* EPERM if required hardware support is not available.
* EIO if the operation fails.
*/
ICA_EXPORT
int ica_aes_gcm_kma_init(unsigned int direction,
const unsigned char *iv, unsigned int iv_length,
const unsigned char *key, unsigned int key_length,
kma_ctx* ctx);
/**
* Perform encryption or decryption with authentication, depending on the
* direction specified in ica_aes_gcm_kma_init().
*
* @param in_data
* Pointer to a readable buffer of size greater than or equal to data_length bytes.
* If direction equals 1 the in_data must contain a payload message of size
* data_length that will be encrypted and authenticated.
* If direction equals 0 the in_data buffer must contain an encrypted message
* that will be decrypted and verified.
*
* @param out_data
* Pointer to a writable buffer of size greater than or equal to data_length bytes.
* If direction equals 1 then the encrypted message from in_data will be written to
* that buffer.
* If direction equals 0 then the decrypted message from in_data will be written to
* that buffer.
*
* @param data_length
* Length in bytes of the message to be en/decrypted. It must be equal or
* greater than 0 and less than (2^36)-32.
*
* @param aad
* Pointer to a readable buffer of size greater than or equal to aad_length
* bytes. The additional authenticated data in the most significant aad_length
* bytes is subject to the authentication code computation but will not be
* encrypted.
*
* @param aad_length
* Length in bytes of the additional authenticated data in aad. It must be
* equal or greater than 0 and less than 2^61.
* In case of ica_aes_gcm_last(), 'aad_length' contains the overall
* length of authentication data, cumulated over all intermediate operations.
*
* @param end_of_aad
* 0 or 1:
* 0 The application indicates that the current aad is not the last aad chunk. In
* this case, the aad_length must be a multiple of the AES block size (16 bytes).
* 1 The application indicates that the current aad is a single or last aad chunk,
* or the last aad chunk has been provided in an earlier call to ica_aes_gcm_kma.
* In this case, aad_length can have any non-negative value.
* When both, end_of_aad and end_of_data are specified, the process ends.
*
* @param end_of_data
* 0 or 1:
* 0 The application indicates that the current in_data is not the last in_data chunk.
* In this case, the data_length must be a multiple of the AES block size (16 bytes).
* 1 The application indicates that the current in_data is a single or last in_data
* chunk. In this case, aad_length can have any non-negative value. When both, end_of_aad
* and end_of_data are specified, the process ends.
*
* @param ctx
* Pointer to gcm context.
*
* @return 0 on success
* EINVAL if at least one invalid parameter is given.
* EPERM if required hardware support is not available.
* EIO if the operation fails.
*/
ICA_EXPORT
int ica_aes_gcm_kma_update(const unsigned char *in_data,
unsigned char *out_data, unsigned long data_length,
const unsigned char *aad, unsigned long aad_length,
unsigned int end_of_aad, unsigned int end_of_data,
kma_ctx* ctx);
/**
* Obtain the calculated authentication tag after an encryption process.
*
* @param tag
* Pointer to a writable buffer to return the calculated authentication tag.
*
* @param tag_length
* Length in bytes of the message authentication code tag. Valid tag lengths
* are 4, 8, 12, 13, 14, 15, and 16.
*
* @param ctx
* Pointer to gcm context.
*
* @return 0 on success
* EINVAL if at least one invalid parameter is given
* EFAULT if direction is 0.
*/
ICA_EXPORT
int ica_aes_gcm_kma_get_tag(unsigned char *tag, unsigned int tag_length,
const kma_ctx* ctx);
/**
* Verify if the specified known authentication tag is identical to the
* calculated tag after a decryption process.
*
* @param known_tag
* Pointer to a readable buffer containing a known authentication tag.
*
* @param tag_length
* Length in bytes of the message authentication code tag. Valid tag lengths
* are 4, 8, 12, 13, 14, 15, and 16.
*
* @param ctx
* Pointer to gcm context.
*
* @return 0 on success
* EINVAL if at least one invalid parameter is given or direction is 1.
* EFAULT if the verification of the message authentication code fails.
*/
ICA_EXPORT
int ica_aes_gcm_kma_verify_tag(const unsigned char* known_tag, unsigned int tag_length,
const kma_ctx* ctx);
/**
* Free gcm context.
*
* @param ctx
* Pointer to gcm context.
*/
ICA_EXPORT
void ica_aes_gcm_kma_ctx_free(kma_ctx* ctx);
/**
*
* End of new gcm API based on KMA.
*
******************************************************************************/
/**
* Return libica version information.
* @param version_info
* Pointer to a libica_version_info structure. The structure will be
* filled with the current libica version information.
*
* @return 0 if version could be determined successfully
* EIO if version could not be determined
* EINVAL if parameter version_info is NULL
*/
ICA_EXPORT
unsigned int ica_get_version(libica_version_info *version_info);
/* XXX next major: dont export, move to s390_crypto.h */
ICA_EXPORT ICA_DEPRECATED
int s390_initialize_functionlist(void);
/* XXX next major: dont export, move to s390_crypto.h */
ICA_EXPORT ICA_DEPRECATED
int s390_get_functionlist(libica_func_list_element *pmech_list,
unsigned int *pmech_list_len);
/**
* Function that returns a list of crypto mechanisms supported by libica.
* @param pmech_list
* Pointer to an array of libica_func_list_element
* If NULL, the API will return the number of elements to allocate
* in the @mech_list_len parameter.
* If not NULL, libica will assume @mech_list is an array that has
* @num elements.
* On success, @mech_list will be filled out with the supported libica
* crypto mechanisms.
* @param pmech_list_len
* number of list entries
* On input, pointer to the number of elements allocated in the
* @mech_list array.
* On output, @mech_list_len will contain the number of items copied to
* the @mech_list array, or the number of items libica would have returned
* in case the @mech_list parameter is set to NULL.
* @return
* 0 on success
* EINVAL if at least one invalid parameter is given
*
* A typical usage scenario would be that an exploiter makes a first call to
* ica_get_functionlist() with @mech_list set to NULL in order to determine
* the number of elements to allocate. This is followed by a second call to
* ica_get_functionlist() with a valid pointer @list to an array of
* libica_func_list_element structures with @mech_list_len elements.
*/
ICA_EXPORT
unsigned int ica_get_functionlist(libica_func_list_element *pmech_list,
unsigned int *pmech_list_len);
static inline unsigned int des_directed_fc(int direction)
{
if (direction)
return DEA_ENCRYPT;
return DEA_DECRYPT;
}
static inline unsigned int tdes_directed_fc(int direction)
{
if (direction)
return TDEA_192_ENCRYPT;
return TDEA_192_DECRYPT;
}
static inline unsigned int aes_directed_fc(unsigned int key_length, int direction)
{
switch (key_length) {
case AES_KEY_LEN128:
return (direction == ICA_DECRYPT) ?
AES_128_DECRYPT : AES_128_ENCRYPT;
case AES_KEY_LEN192:
return (direction == ICA_DECRYPT) ?
AES_192_DECRYPT : AES_192_ENCRYPT;
case AES_KEY_LEN256:
return (direction == ICA_DECRYPT) ?
AES_256_DECRYPT : AES_256_ENCRYPT;
}
return 0;
}
/*
* ica_drbg: libica's Deterministic Random Bit Generator
* (conforming to NIST SP 800-90A)
*
* Table of currently supported DRBG mechanisms:
*
* DRBG mechanism supported security max. byte length
* strengths (bits) of pers / add
* -------------------------------------------------------------
* DRBG_SHA512 112, 128, 196, 256 256 / 256
*
* An ica_drbg_t object holds the internal state of a DRBG instantiation. A
* DRBG instantiation is identified by an associated ica_drbg_t * pointer
* (state handle).
* State handles that do not identify any DRBG instantiation SHALL be NULL
* (invalid). Therefore a new state handle SHALL be initialized to NULL.
*
* If a catastrophic error (<0) is detected, all existing DRBG instantiations
* of the corresponding mechanism are in error state making uninstantiation
* their only permitted operation. Creation of new DRBG instantiations of
* this mechanism are not permitted.
*/
ICA_EXPORT
extern ica_drbg_mech_t *const ICA_DRBG_SHA512;
/*
* Instantiate function
* (create a new DRBG instantiation)
*
* @sh: State Handle pointer. The (invalid) state handle is set to identify the
* new DRBG instantiation and thus becomes valid.
* @sec: requested instantiation SECurity strength (bits). The new DRBG
* instantiation's security strength is set to the lowest security strength
* supported by it's DRBG mechanism (see table) that is greater than or equal
* to @sec.
* @pr: Prediction Resistance flag. Indicates whether or not prediction
* resistance may be required by the consuming application during one or more
* requests for pseudorandom bytes.
* @mech: MECHanism. The new DRBG instantiation is of this mechanism type.
* @pers: PERSonalization string. An optional input that provides
* personalization information. The personalisation string SHALL be unique for
* all instantiations of the same mechanism type. NULL indicates that no
* personalization string is used (not recommended).
* @pers_len: Byte length of @pers.
*
* @return:
* 0 Success.
* ENOMEM Out of memory.
* EINVAL At least one argument is invalid.
* ENOTSUP Prediction resistance or the requested security
* strength is not supported.
* EPERM Failed to obtain a valid timestamp from clock.
* ICA_DRBG_HEALTH_TEST_FAIL Health test failed.
* ICA_DRBG_ENTROPY_SOURCE_FAIL Entropy source failed.
*/
ICA_EXPORT
int ica_drbg_instantiate(ica_drbg_t **sh,
int sec,
bool pr,
ica_drbg_mech_t *mech,
const unsigned char *pers,
size_t pers_len);
/*
* Reseed function
* (reseed a DRBG instantiation)
*
* @sh: State Handle. Identifies the DRBG instantiation to be reseeded.
* @pr: Prediction Resistance request. Indicates whether or not prediction
* resistance is required.
* @add: ADDitional input: An optional input. NULL indicates that no additional
* input is used.
* @add_len: Byte length of @add.
*
* @return:
* 0 Success.
* ENOMEM Out of memory.
* EINVAL At least one argument is invalid.
* ENOTSUP Prediction resistance is not supported.
* ICA_DRBG_HEALTH_TEST_FAIL Health test failed.
* ICA_DRBG_ENTROPY_SOURCE_FAIL Entropy source failed.
*/
ICA_EXPORT
int ica_drbg_reseed(ica_drbg_t *sh,
bool pr,
const unsigned char *add,
size_t add_len);
/*
* Generate function
* (request pseudorandom bytes from a DRBG instantiation)
*
* @sh: State Handle. Identifies the DRBG instantiation from which pseudorandom
* bytes are requested.
* @sec: requested SECurity strength: Minimum bits of security that the
* generated pseudorandom bytes SHALL offer.
* @pr: Prediction Resistance request. Indicates whether or not prediction
* resistance is required.
* @add: ADDitional input. An optional input. NULL indicates that no additional input
* is used.
* @add_len: Byte length of @add.
* @prnd: PseudoRaNDom bytes.
* @prnd_len: Byte length of @prnd. Requested number of pseudorandom bytes.
*
* @return:
* 0 Success.
* ENOMEM Out of memory.
* EINVAL At least one argument is invalid.
* ENOTSUP Prediction resistance or the requested security
* strength is not supported.
* EPERM Reseed required.
* ICA_DRBG_HEALTH_TEST_FAIL Health test failed.
* ICA_DRBG_ENTROPY_SOURCE_FAIL Entropy source failed.
*/
ICA_EXPORT
int ica_drbg_generate(ica_drbg_t *sh,
int sec,
bool pr,
const unsigned char *add,
size_t add_len,
unsigned char *prnd,
size_t prnd_len);
/*
* Uninstantiate function
* (destroy an existing DRBG instantiation)
*
* @sh: State Handle pointer. The corresponding DRBG instantiation is destroyed
* and the state handle is set to NULL (invalid).
*
* @return:
* 0 Success.
* EINVAL At least one argument is invalid.
*/
ICA_EXPORT
int ica_drbg_uninstantiate(ica_drbg_t **sh);
/*
* Health test function
* (run health test for a DRBG mechanism function)
*
* @func: FUNCtion. Pointer indicating which function should be tested. Options
* are "ica_drbg_instantiate", "ica_drbg_reseed" and "ica_drbg_generate". The
* uninstantiate function is tested whenever other functions are tested.
* @sec: SECurity strength. Argument for the call to @func.
* @pr: PRediction resistance. Argument for the call to @func.
* @mech: MECHanism. The mechanism to be tested.
*
* @return:
* 0 Success.
* EINVAL At least one argument is invalid.
* ENOTSUP Prediction resistance or security strength is
* not supported (when testing instantiate).
* ICA_DRBG_HEALTH_TEST_FAIL Health test failed.
* ICA_DRBG_ENTROPY_SOURCE_FAIL Entropy source failed.
*/
ICA_EXPORT
int ica_drbg_health_test(void *func,
int sec,
bool pr,
ica_drbg_mech_t *mech);
/*
* ica_mp: libica's multiple-precision arithmetic interface
*
* Numbers are represented in radix 2^64. The least-significant digit is stored
* at array element zero.
*
* Example:
*
* uint64_t a[] = {3, 4, 5}; // a = 5*(2^64)^2 + 4*(2^64) + 3
*/
/*
* Multiply the zero-padded 512-bit numbers @a and @b. The zero-padded 1024-bit
* result is stored at @r.
*
* @r: 1024-bit produkt (@r = @a * @b)
* @a: 512-bit factor 1
* @b: 512-bit factor 2
*
* @return:
* 0 Success.
* != 0 Vector facilities are not enabled.
*/
ICA_EXPORT
int ica_mp_mul512(uint64_t r[16], const uint64_t a[8], const uint64_t b[8]);
/*
* Square the zero-padded 512-bit number @a. The zero-padded 1024-bit result is
* stored at @r.
*
* @r: 1024-bit square (@r = @a ^ 2)
* @a: 512-bit base
*
* @return:
* 0 Success.
* != 0 Vector facilities are not enabled.
*/
ICA_EXPORT
int ica_mp_sqr512(uint64_t r[16], const uint64_t a[8]);
#ifdef ICA_FIPS
/*
* Additional FIPS interfaces are available for built-in FIPS mode.
*/
/*
* FIPS status output interface.
*
* @return:
* Returns flags indicating the module status. See the ICA_FIPS_* flags.
*/
ICA_EXPORT
int ica_fips_status(void);
/*
* FIPS powerups tests.
*
* The test results can be viewed via the ica_fips_status function.
*/
ICA_EXPORT
void ica_fips_powerup_tests(void);
#endif /* ICA_FIPS */
#endif /* __ICA_API_H__ */
|