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
|
/* wolfCrypt.cs
*
* Copyright (C) 2006-2025 wolfSSL Inc.
*
* This file is part of wolfSSL.
*
* wolfSSL is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* wolfSSL is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
*/
using System;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Text;
namespace wolfSSL.CSharp
{
public class wolfcrypt
{
private const string wolfssl_dll = "wolfssl.dll";
/********************************
* Init wolfSSL library
*/
#if WindowsCE
[DllImport(wolfssl_dll)]
private extern static int wolfCrypt_Init();
[DllImport(wolfssl_dll)]
private extern static int wolfCrypt_Cleanup();
#else
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static int wolfCrypt_Init();
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static int wolfCrypt_Cleanup();
#endif
/********************************
* Random
*/
#if WindowsCE
[DllImport(wolfssl_dll)]
private extern static IntPtr wc_rng_new(IntPtr nonce, UInt32 nonceSz, IntPtr heap);
[DllImport(wolfssl_dll)]
private extern static void wc_rng_free(IntPtr rng);
[DllImport(wolfssl_dll)]
private extern static int wc_RNG_GenerateBlock(IntPtr rng, IntPtr output, UInt32 sz);
#else
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static IntPtr wc_rng_new(IntPtr nonce, UInt32 nonceSz, IntPtr heap);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static void wc_rng_free(IntPtr rng);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static int wc_RNG_GenerateBlock(IntPtr rng, IntPtr output, UInt32 sz);
#endif
/********************************
* ECC
*/
#if WindowsCE
[DllImport(wolfssl_dll)]
private extern static IntPtr wc_ecc_key_new(IntPtr heap);
[DllImport(wolfssl_dll)]
private extern static void wc_ecc_key_free(IntPtr key);
[DllImport(wolfssl_dll)]
private extern static int wc_ecc_set_rng(IntPtr key, IntPtr rng);
[DllImport(wolfssl_dll)]
private extern static int wc_ecc_make_key_ex(IntPtr rng, int keysize, IntPtr key, int curve_id);
[DllImport(wolfssl_dll)]
private extern static int wc_ecc_sign_hash(IntPtr hashPtr, uint hashlen, IntPtr sigPtr, IntPtr siglen, IntPtr rng, IntPtr key);
[DllImport(wolfssl_dll)]
private extern static int wc_ecc_verify_hash(IntPtr sigPtr, uint siglen, IntPtr hashPtr, uint hashlen, IntPtr res, IntPtr key);
/* ASN.1 DER format */
[DllImport(wolfssl_dll)]
private extern static int wc_EccPrivateKeyDecode(IntPtr keyBuf, IntPtr idx, IntPtr key, uint keyBufSz);
[DllImport(wolfssl_dll)]
private static extern int wc_EccPublicKeyDecode(byte[] input, ref uint inOutIdx, IntPtr key, uint inSz);
[DllImport(wolfssl_dll)]
private static extern int wc_EccPrivateKeyToDer(IntPtr key, byte[] output, uint inLen);
[DllImport(wolfssl_dll)]
private static extern int wc_EccPublicKeyToDer(IntPtr key, byte[] output, uint inLen, int with_AlgCurve);
#else
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static IntPtr wc_ecc_key_new(IntPtr heap);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static void wc_ecc_key_free(IntPtr key);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static int wc_ecc_set_rng(IntPtr key, IntPtr rng);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static int wc_ecc_make_key_ex(IntPtr rng, int keysize, IntPtr key, int curve_id);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static int wc_ecc_sign_hash(IntPtr hashPtr, uint hashlen, IntPtr sigPtr, IntPtr siglen, IntPtr rng, IntPtr key);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static int wc_ecc_verify_hash(IntPtr sigPtr, uint siglen, IntPtr hashPtr, uint hashlen, IntPtr res, IntPtr key);
/* ASN.1 DER format */
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static int wc_EccPrivateKeyDecode(IntPtr keyBuf, IntPtr idx, IntPtr key, uint keyBufSz);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private static extern int wc_EccPublicKeyDecode(byte[] input, ref uint inOutIdx, IntPtr key, uint inSz);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private static extern int wc_EccPrivateKeyToDer(IntPtr key, byte[] output, uint inLen);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private static extern int wc_EccPublicKeyToDer(IntPtr key, byte[] output, uint inLen, int with_AlgCurve);
#endif
/********************************
* ECIES
*/
#if WindowsCE
[DllImport(wolfssl_dll)]
private extern static IntPtr wc_ecc_ctx_new(int flags, IntPtr rng);
[DllImport(wolfssl_dll)]
private extern static IntPtr wc_ecc_ctx_new_ex(int flags, IntPtr rng, IntPtr heap);
[DllImport(wolfssl_dll)]
private extern static void wc_ecc_ctx_free(IntPtr ctx);
[DllImport(wolfssl_dll)]
private extern static int wc_ecc_ctx_reset(IntPtr ctx, IntPtr rng);
[DllImport(wolfssl_dll)]
private extern static int wc_ecc_ctx_set_algo(IntPtr ctx, byte encAlgo, byte kdfAlgo, byte macAlgo);
[DllImport(wolfssl_dll)]
private extern static IntPtr wc_ecc_ctx_get_own_salt(IntPtr ctx);
[DllImport(wolfssl_dll)]
private extern static int wc_ecc_ctx_set_peer_salt(IntPtr ctx, IntPtr salt);
[DllImport(wolfssl_dll)]
private extern static int wc_ecc_ctx_set_own_salt(IntPtr ctx, IntPtr salt, uint sz);
[DllImport(wolfssl_dll)]
private extern static int wc_ecc_ctx_set_kdf_salt(IntPtr ctx, IntPtr salt, uint sz);
[DllImport(wolfssl_dll)]
private extern static int wc_ecc_ctx_set_info(IntPtr ctx, IntPtr info, int sz);
[DllImport(wolfssl_dll)]
private extern static int wc_ecc_encrypt(IntPtr privKey, IntPtr pubKey, IntPtr msg, uint msgSz, IntPtr outBuffer, IntPtr outSz, IntPtr ctx);
[DllImport(wolfssl_dll)]
private extern static int wc_ecc_encrypt_ex(IntPtr privKey, IntPtr pubKey, IntPtr msg, uint msgSz, IntPtr outBuffer, IntPtr outSz, IntPtr ctx, int compressed);
[DllImport(wolfssl_dll)]
private extern static int wc_ecc_decrypt(IntPtr privKey, IntPtr pubKey, IntPtr msg, uint msgSz, IntPtr outBuffer, IntPtr outSz, IntPtr ctx);
#else
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static IntPtr wc_ecc_ctx_new(int flags, IntPtr rng);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static IntPtr wc_ecc_ctx_new_ex(int flags, IntPtr rng, IntPtr heap);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static void wc_ecc_ctx_free(IntPtr ctx);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static int wc_ecc_ctx_reset(IntPtr ctx, IntPtr rng);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static int wc_ecc_ctx_set_algo(IntPtr ctx, byte encAlgo, byte kdfAlgo, byte macAlgo);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static IntPtr wc_ecc_ctx_get_own_salt(IntPtr ctx);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static int wc_ecc_ctx_set_peer_salt(IntPtr ctx, IntPtr salt);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static int wc_ecc_ctx_set_own_salt(IntPtr ctx, IntPtr salt, uint sz);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static int wc_ecc_ctx_set_kdf_salt(IntPtr ctx, IntPtr salt, uint sz);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static int wc_ecc_ctx_set_info(IntPtr ctx, IntPtr info, int sz);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static int wc_ecc_encrypt(IntPtr privKey, IntPtr pubKey, IntPtr msg, uint msgSz, IntPtr outBuffer, IntPtr outSz, IntPtr ctx);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static int wc_ecc_encrypt_ex(IntPtr privKey, IntPtr pubKey, IntPtr msg, uint msgSz, IntPtr outBuffer, IntPtr outSz, IntPtr ctx, int compressed);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static int wc_ecc_decrypt(IntPtr privKey, IntPtr pubKey, IntPtr msg, uint msgSz, IntPtr outBuffer, IntPtr outSz, IntPtr ctx);
#endif
/********************************
* ECDHE
*/
#if WindowsCE
[DllImport(wolfssl_dll)]
private extern static int wc_ecc_shared_secret(IntPtr privateKey, IntPtr publicKey, byte[] outSharedSecret, ref int outlen);
#else
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static int wc_ecc_shared_secret(IntPtr privateKey, IntPtr publicKey, byte[] outSharedSecret, ref int outlen);
#endif
/********************************
* RSA
*/
#if WindowsCE
[DllImport(wolfssl_dll)]
private static extern IntPtr wc_NewRsaKey(IntPtr heap, int devId, IntPtr result_code);
[DllImport(wolfssl_dll)]
private static extern int wc_DeleteRsaKey(IntPtr key, IntPtr key_p);
[DllImport(wolfssl_dll)]
private extern static int wc_InitRsaKey(IntPtr key, IntPtr heap);
[DllImport(wolfssl_dll)]
private extern static void wc_FreeRsaKey(IntPtr key);
[DllImport(wolfssl_dll)]
private extern static int wc_MakeRsaKey(IntPtr key, int keysize, Int32 exponent, IntPtr rng);
[DllImport(wolfssl_dll)]
private extern static int wc_RsaSSL_Sign(IntPtr hashPtr, int hashLen, IntPtr sigPtr, int sigLen, IntPtr key, IntPtr rng);
[DllImport(wolfssl_dll)]
private extern static int wc_RsaSSL_Verify(IntPtr sigPtr, int sigLen, IntPtr hashPtr, int hashLen, IntPtr key);
/* ASN.1 DER format */
[DllImport(wolfssl_dll)]
private extern static int wc_RsaPublicEncrypt(IntPtr inPtr, int inLen, IntPtr outPtr, int outLen, IntPtr key);
[DllImport(wolfssl_dll)]
private extern static int wc_RsaPrivateDecrypt(IntPtr inPtr, int inLen, IntPtr outPtr, int outLen, IntPtr key);
[DllImport(wolfssl_dll)]
private extern static int wc_RsaPrivateKeyDecode(IntPtr keyBuf, IntPtr idx, IntPtr key, uint keyBufSz);
[DllImport(wolfssl_dll)]
private extern static int wc_RsaPublicKeyDecode(IntPtr keyBuf, IntPtr idx, IntPtr key, uint keyBufSz);
[DllImport(wolfssl_dll)]
private extern static int wc_RsaPSS_Sign(IntPtr hashPtr, int hashLen, IntPtr sigPtr, int sigLen, int hashType, IntPtr rng, IntPtr key);
[DllImport(wolfssl_dll)]
private extern static int wc_RsaPSS_Verify(IntPtr sigPtr, int sigLen, IntPtr hashPtr, int hashLen, int hashType, IntPtr key);
[DllImport(wolfssl_dll)]
private extern static int wc_RsaPSS_CheckPadding(IntPtr sigPtr, int sigLen, int hashType, IntPtr key);
#else
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr wc_NewRsaKey(IntPtr heap, int devId, IntPtr result_code);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private static extern int wc_DeleteRsaKey(IntPtr key, IntPtr key_p);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static int wc_InitRsaKey(IntPtr key, IntPtr heap);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static void wc_FreeRsaKey(IntPtr key);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static int wc_MakeRsaKey(IntPtr key, int keysize, Int32 exponent, IntPtr rng);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static int wc_RsaSSL_Sign(IntPtr hashPtr, int hashLen, IntPtr sigPtr, int sigLen, IntPtr key, IntPtr rng);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static int wc_RsaSSL_Verify(IntPtr sigPtr, int sigLen, IntPtr hashPtr, int hashLen, IntPtr key);
/* ASN.1 DER format */
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static int wc_RsaPublicEncrypt(IntPtr inPtr, int inLen, IntPtr outPtr, int outLen, IntPtr key);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static int wc_RsaPrivateDecrypt(IntPtr inPtr, int inLen, IntPtr outPtr, int outLen, IntPtr key);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static int wc_RsaPrivateKeyDecode(IntPtr keyBuf, IntPtr idx, IntPtr key, uint keyBufSz);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static int wc_RsaPublicKeyDecode(IntPtr keyBuf, IntPtr idx, IntPtr key, uint keyBufSz);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static int wc_RsaPSS_Sign(IntPtr hashPtr, int hashLen, IntPtr sigPtr, int sigLen, int hashType, IntPtr rng, IntPtr key);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static int wc_RsaPSS_Verify(IntPtr sigPtr, int sigLen, IntPtr hashPtr, int hashLen, int hashType, IntPtr key);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static int wc_RsaPSS_CheckPadding(IntPtr sigPtr, int sigLen, int hashType, IntPtr key);
#endif
/********************************
* ED25519
*/
#if WindowsCE
[DllImport(wolfssl_dll)]
private static extern IntPtr wc_ed25519_new(IntPtr heap, int devId, IntPtr result_code);
[DllImport(wolfssl_dll)]
private static extern int wc_ed25519_delete(IntPtr key, IntPtr key_p);
[DllImport(wolfssl_dll)]
private static extern int wc_ed25519_init(IntPtr key);
[DllImport(wolfssl_dll)]
private static extern void wc_ed25519_free(IntPtr key);
[DllImport(wolfssl_dll)]
private static extern int wc_ed25519_make_key(IntPtr rng, int keysize, IntPtr key);
[DllImport(wolfssl_dll)]
private static extern int wc_ed25519_sign_msg(IntPtr inMsg, uint inlen, IntPtr outMsg, ref uint outlen, IntPtr key);
[DllImport(wolfssl_dll)]
private static extern int wc_ed25519_verify_msg(IntPtr sig, uint siglen, IntPtr msg, uint msgLen, ref int ret, IntPtr key);
/* ASN.1 DER format */
[DllImport(wolfssl_dll)]
private static extern int wc_Ed25519PrivateKeyDecode(byte[] input, ref uint inOutIdx, IntPtr key, uint inSz);
[DllImport(wolfssl_dll)]
private static extern int wc_Ed25519PublicKeyDecode(byte[] input, ref uint inOutIdx, IntPtr key, uint inSz);
[DllImport(wolfssl_dll)]
private static extern int wc_Ed25519KeyToDer(IntPtr key, byte[] output, uint inLen);
[DllImport(wolfssl_dll)]
private static extern int wc_Ed25519PrivateKeyToDer(IntPtr key, byte[] output, uint inLen);
[DllImport(wolfssl_dll)]
private static extern int wc_Ed25519PublicKeyToDer(IntPtr key, byte[] output, uint inLen, int withAlg);
/* RAW format */
[DllImport(wolfssl_dll)]
private static extern int wc_ed25519_make_public(IntPtr key, IntPtr pubKey, uint pubKeySz);
[DllImport(wolfssl_dll)]
private static extern int wc_ed25519_import_public(IntPtr inMsg, uint inLen, IntPtr key);
[DllImport(wolfssl_dll)]
private static extern int wc_ed25519_export_public(IntPtr key, IntPtr outMsg, ref uint outLen);
[DllImport(wolfssl_dll)]
private static extern int wc_ed25519_export_private(IntPtr key, IntPtr outMsg, ref uint outLen);
[DllImport(wolfssl_dll)]
private static extern int wc_ed25519_size(IntPtr key);
#else
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr wc_ed25519_new(IntPtr heap, int devId, IntPtr result_code);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private static extern int wc_ed25519_delete(IntPtr key, IntPtr key_p);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private static extern int wc_ed25519_init(IntPtr key);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private static extern void wc_ed25519_free(IntPtr key);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private static extern int wc_ed25519_make_key(IntPtr rng, int keysize, IntPtr key);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private static extern int wc_ed25519_sign_msg(IntPtr inMsg, uint inlen, IntPtr outMsg, ref uint outlen, IntPtr key);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private static extern int wc_ed25519_verify_msg(IntPtr sig, uint siglen, IntPtr msg, uint msgLen, ref int ret, IntPtr key);
/* ASN.1 DER format */
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private static extern int wc_Ed25519PrivateKeyDecode(byte[] input, ref uint inOutIdx, IntPtr key, uint inSz);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private static extern int wc_Ed25519PublicKeyDecode(byte[] input, ref uint inOutIdx, IntPtr key, uint inSz);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private static extern int wc_Ed25519KeyToDer(IntPtr key, byte[] output, uint inLen);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private static extern int wc_Ed25519PrivateKeyToDer(IntPtr key, byte[] output, uint inLen);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private static extern int wc_Ed25519PublicKeyToDer(IntPtr key, byte[] output, uint inLen, int withAlg);
/* RAW format */
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private static extern int wc_ed25519_make_public(IntPtr key, IntPtr pubKey, uint pubKeySz);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private static extern int wc_ed25519_import_public(IntPtr inMsg, uint inLen, IntPtr key);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private static extern int wc_ed25519_export_public(IntPtr key, IntPtr outMsg, ref uint outLen);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private static extern int wc_ed25519_export_private(IntPtr key, IntPtr outMsg, ref uint outLen);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private static extern int wc_ed25519_size(IntPtr key);
#endif
/********************************
* Curve25519
*/
#if WindowsCE
[DllImport(wolfssl_dll)]
private static extern IntPtr wc_curve25519_new(IntPtr heap, int devId, IntPtr result_code);
[DllImport(wolfssl_dll)]
private static extern int wc_curve25519_delete(IntPtr key, IntPtr key_p);
[DllImport(wolfssl_dll)]
private extern static int wc_curve25519_init(IntPtr key);
[DllImport(wolfssl_dll)]
private extern static void wc_curve25519_free(IntPtr key);
[DllImport(wolfssl_dll)]
private extern static int wc_curve25519_make_key(IntPtr rng, int keysize, IntPtr key);
[DllImport(wolfssl_dll)]
private extern static int wc_curve25519_shared_secret(IntPtr privateKey, IntPtr publicKey, byte[] outSharedSecret, ref int outlen);
/* ASN.1 DER format */
[DllImport(wolfssl_dll)]
private static extern int wc_Curve25519PrivateKeyDecode(byte[] input, ref uint inOutIdx, IntPtr key, uint inSz);
[DllImport(wolfssl_dll)]
private static extern int wc_Curve25519PublicKeyDecode(byte[] input, ref uint inOutIdx, IntPtr key, uint inSz);
[DllImport(wolfssl_dll)]
private static extern int wc_Curve25519PrivateKeyToDer(IntPtr key, byte[] output, uint inLen);
[DllImport(wolfssl_dll)]
private static extern int wc_Curve25519PublicKeyToDer(IntPtr key, byte[] output, uint inLen, int withAlg);
/* RAW format */
[DllImport(wolfssl_dll)]
private extern static int wc_curve25519_import_private(IntPtr privKey, int privKeySz, IntPtr key);
[DllImport(wolfssl_dll)]
private static extern int wc_curve25519_export_public(IntPtr key, byte[] outBuffer, ref uint outLen);
[DllImport(wolfssl_dll)]
private extern static int wc_curve25519_import_public(IntPtr pubKey, int pubKeySz, IntPtr key);
[DllImport(wolfssl_dll)]
private extern static int wc_curve25519_export_public(IntPtr key, IntPtr outPubKey, ref int outlen);
[DllImport(wolfssl_dll)]
private static extern int wc_curve25519_export_key_raw(IntPtr key, byte[] priv, ref uint privSz, byte[] pub, ref uint pubSz);
[DllImport(wolfssl_dll)]
private extern static int wc_curve25519_import_private_raw(IntPtr privKey, IntPtr pubKey, IntPtr key);
[DllImport(wolfssl_dll)]
private extern static int wc_curve25519_export_private_raw(IntPtr key, IntPtr outPrivKey, IntPtr outPubKey);
#else
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr wc_curve25519_new(IntPtr heap, int devId, IntPtr result_code);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private static extern int wc_curve25519_delete(IntPtr key, IntPtr key_p);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static int wc_curve25519_init(IntPtr key);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static void wc_curve25519_free(IntPtr key);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static int wc_curve25519_make_key(IntPtr rng, int keysize, IntPtr key);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static int wc_curve25519_shared_secret(IntPtr privateKey, IntPtr publicKey, byte[] outSharedSecret, ref int outlen);
/* ASN.1 DER format */
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private static extern int wc_Curve25519PrivateKeyDecode(byte[] input, ref uint inOutIdx, IntPtr key, uint inSz);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private static extern int wc_Curve25519PublicKeyDecode(byte[] input, ref uint inOutIdx, IntPtr key, uint inSz);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private static extern int wc_Curve25519PrivateKeyToDer(IntPtr key, byte[] output, uint inLen);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private static extern int wc_Curve25519PublicKeyToDer(IntPtr key, byte[] output, uint inLen, int withAlg);
/* RAW format */
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static int wc_curve25519_import_private(IntPtr privKey, int privKeySz, IntPtr key);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private static extern int wc_curve25519_export_public(IntPtr key, byte[] outBuffer, ref uint outLen);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static int wc_curve25519_import_public(IntPtr pubKey, int pubKeySz, IntPtr key);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static int wc_curve25519_export_public(IntPtr key, IntPtr outPubKey, ref int outlen);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private static extern int wc_curve25519_export_key_raw(IntPtr key, byte[] priv, ref uint privSz, byte[] pub, ref uint pubSz);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static int wc_curve25519_import_private_raw(IntPtr privKey, IntPtr pubKey, IntPtr key);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static int wc_curve25519_export_private_raw(IntPtr key, IntPtr outPrivKey, IntPtr outPubKey);
#endif
/********************************
* AES-GCM
*/
#if WindowsCE
[DllImport(wolfssl_dll)]
private extern static IntPtr wc_AesNew(IntPtr heap, int devId, IntPtr result_code);
[DllImport(wolfssl_dll)]
private extern static int wc_AesDelete(IntPtr aes, IntPtr aes_p);
[DllImport(wolfssl_dll)]
private extern static int wc_AesFree(IntPtr aes);
[DllImport(wolfssl_dll)]
private extern static int wc_AesInit(IntPtr aes, IntPtr heap, int devId);
[DllImport(wolfssl_dll)]
private extern static int wc_AesGcmInit(IntPtr aes, IntPtr key, uint len, IntPtr iv, uint ivSz);
[DllImport(wolfssl_dll)]
private extern static int wc_AesGcmSetKey(IntPtr aes, IntPtr key, uint len);
[DllImport(wolfssl_dll)]
private extern static int wc_AesGcmEncrypt(IntPtr aes, IntPtr output, IntPtr input, uint sz, IntPtr iv, uint ivSz, IntPtr authTag, uint authTagSz, IntPtr authIn, uint authInSz);
[DllImport(wolfssl_dll)]
private extern static int wc_AesGcmDecrypt(IntPtr aes, IntPtr output, IntPtr input, uint sz, IntPtr iv, uint ivSz, IntPtr authTag, uint authTagSz, IntPtr authIn, uint authInSz);
#else
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static IntPtr wc_AesNew(IntPtr heap, int devId, IntPtr result_code);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static int wc_AesDelete(IntPtr aes, IntPtr aes_p);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static int wc_AesFree(IntPtr aes);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static int wc_AesInit(IntPtr aes, IntPtr heap, int devId);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static int wc_AesGcmInit(IntPtr aes, IntPtr key, uint len, IntPtr iv, uint ivSz);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static int wc_AesGcmSetKey(IntPtr aes, IntPtr key, uint len);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static int wc_AesGcmEncrypt(IntPtr aes, IntPtr output, IntPtr input, uint sz, IntPtr iv, uint ivSz, IntPtr authTag, uint authTagSz, IntPtr authIn, uint authInSz);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static int wc_AesGcmDecrypt(IntPtr aes, IntPtr output, IntPtr input, uint sz, IntPtr iv, uint ivSz, IntPtr authTag, uint authTagSz, IntPtr authIn, uint authInSz);
#endif
/********************************
* HASH
*/
#if WindowsCE
[DllImport(wolfssl_dll)]
private extern static IntPtr wc_HashNew(uint hashType, IntPtr heap, int devId, IntPtr result_code);
[DllImport(wolfssl_dll)]
private extern static int wc_HashDelete(IntPtr hash, IntPtr hash_p);
[DllImport(wolfssl_dll)]
private extern static int wc_HashInit(IntPtr hash, uint hashType);
[DllImport(wolfssl_dll)]
private extern static int wc_HashUpdate(IntPtr hash, uint hashType, IntPtr data, uint dataSz);
[DllImport(wolfssl_dll)]
private extern static int wc_HashFinal(IntPtr hash, uint hashType, IntPtr output);
[DllImport(wolfssl_dll)]
private extern static int wc_HashFree(IntPtr hash, uint hashType);
[DllImport(wolfssl_dll)]
private extern static int wc_HashGetDigestSize(uint hashType);
#else
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static IntPtr wc_HashNew(uint hashType, IntPtr heap, int devId, IntPtr result_code);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static int wc_HashDelete(IntPtr hash, IntPtr hash_p);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static int wc_HashInit(IntPtr hash, uint hashType);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static int wc_HashUpdate(IntPtr hash, uint hashType, IntPtr data, uint dataSz);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static int wc_HashFinal(IntPtr hash, uint hashType, IntPtr output);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static int wc_HashFree(IntPtr hash, uint hashType);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static int wc_HashGetDigestSize(uint hashType);
#endif
/********************************
* Logging
*/
#if WindowsCE
[DllImport(wolfssl_dll)]
private extern static IntPtr wc_GetErrorString(int error);
public delegate void loggingCb(int lvl, string msg);
#else
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static IntPtr wc_GetErrorString(int error);
public delegate void loggingCb(int lvl, StringBuilder msg);
#endif
private static loggingCb internal_log;
/// <summary>
/// Log a message to set logging function
/// </summary>
/// <param name="lvl">Level of log message</param>
/// <param name="msg">Message to log</param>
#if WindowsCE
private static void log(int lvl, string msg)
{
/* if log is not set then print nothing */
if (internal_log == null)
return;
internal_log(lvl, msg);
}
#else
private static void log(int lvl, string msg)
{
/* if log is not set then print nothing */
if (internal_log == null)
return;
StringBuilder ptr = new StringBuilder(msg);
internal_log(lvl, ptr);
}
#endif
/********************************
* Enum types from wolfSSL library
*/
/* Logging levels */
public static readonly int ERROR_LOG = 0;
public static readonly int INFO_LOG = 1;
public static readonly int ENTER_LOG = 2;
public static readonly int LEAVE_LOG = 3;
public static readonly int OTHER_LOG = 4;
public static readonly int INVALID_DEVID = -2;
public static readonly int ECC_MAX_SIG_SIZE = 141; /* ECC max sig size */
public static readonly int ECC_KEY_SIZE = 32; /* ECC key size */
public static readonly int MAX_ECIES_TEST_SZ = 200; /* ECIES max sig size */
public static readonly int ED25519_SIG_SIZE = 64; /* ED25519 pub + priv */
public static readonly int ED25519_KEY_SIZE = 32; /* Private key only */
public static readonly int ED25519_PUB_KEY_SIZE = 32; /* Compressed public */
public static readonly int AES_128_KEY_SIZE = 16; /* for 128 bit */
public static readonly int AES_192_KEY_SIZE = 24; /* for 192 bit */
public static readonly int AES_256_KEY_SIZE = 32; /* for 256 bit */
public static readonly int AES_BLOCK_SIZE = 16;
/* Error codes */
public static readonly int SUCCESS = 0;
public static readonly int EXCEPTION_E = -1;
public static readonly int MEMORY_E = -125; /* Out of memory error */
public static readonly int BUFFER_E = -131; /* RSA buffer error, output too small/large */
public static readonly int ASN_PARSE_E = -140; /* ASN parsing error, invalid input */
public static readonly int ASN_VERSION_E = -141; /* ASN version error, invalid number */
public static readonly int ASN_GETINT_E = -142; /* ASN get big int error, invalid data */
public static readonly int ASN_RSA_KEY_E = -143; /* ASN key init error, invalid input */
public static readonly int ASN_OBJECT_ID_E = -144; /* ASN object id error, invalid id */
public static readonly int ASN_TAG_NULL_E = -145; /* ASN tag error, not null */
public static readonly int ASN_EXPECT_0_E = -146; /* ASN expect error, not zero */
public static readonly int ASN_BITSTR_E = -147; /* ASN bit string error, wrong id */
public static readonly int ASN_UNKNOWN_OID_E = -148; /* ASN oid error, unknown sum id */
public static readonly int ASN_DATE_SZ_E = -149; /* ASN date error, bad size */
public static readonly int ASN_BEFORE_DATE_E = -150; /* ASN date error, current date before */
public static readonly int ASN_AFTER_DATE_E = -151; /* ASN date error, current date after */
public static readonly int ASN_SIG_OID_E = -152; /* ASN signature error, mismatched oid */
public static readonly int ASN_TIME_E = -153; /* ASN time error, unknown time type */
public static readonly int ASN_INPUT_E = -154; /* ASN input error, not enough data */
public static readonly int ASN_SIG_CONFIRM_E = -155; /* ASN sig error, confirm failure */
public static readonly int ASN_SIG_HASH_E = -156; /* ASN sig error, unsupported hash type */
public static readonly int ASN_SIG_KEY_E = -157; /* ASN sig error, unsupported key type */
public static readonly int SIG_VERIFY_E = -229; /* wolfcrypt signature verify error */
/***********************************************************************
* Class Public Functions
**********************************************************************/
/// <summary>
/// Initialize wolfCrypt library
/// </summary>
/// <returns>0 on success</returns>
public static int Init()
{
int ret;
try
{
ret = wolfCrypt_Init();
}
catch (Exception e)
{
log(ERROR_LOG, "wolfCrypt init error " + e.ToString());
ret = EXCEPTION_E;
}
return ret;
}
/// <summary>
/// Clean up wolfCrypt library memory
/// </summary>
/// <returns>0 on success</returns>
public static int Cleanup()
{
int ret;
try
{
ret = wolfCrypt_Cleanup();
}
catch (Exception e)
{
log(ERROR_LOG, "wolfCrypt cleanup error " + e.ToString());
ret = EXCEPTION_E;
}
return ret;
}
/***********************************************************************
* Random
**********************************************************************/
/// <summary>
/// Create new WC_RNG context
/// </summary>
/// <returns>Pointer to allocated WC_RNG or null</returns>
public static IntPtr RandomNew()
{
IntPtr rng;
try
{
/* Allocate and init new WC_RNG structure */
rng = wc_rng_new(
IntPtr.Zero, 0, /* Nonce (optional / used by FIPS only) */
IntPtr.Zero); /* Heap hint for static memory only */
}
catch (Exception e)
{
log(ERROR_LOG, "random new exception " + e.ToString());
rng = IntPtr.Zero;
}
return rng;
}
/// <summary>
/// Free WC_RNG context
/// </summary>
/// <param name="rng">Pointer to allocated WC_RNG</param>
public static void RandomFree(IntPtr rng)
{
if (rng != IntPtr.Zero)
{
/* Free WC_RNG structure */
wc_rng_free(rng);
}
}
/// <summary>
/// Generate random data (use existing WC_RNG context)
/// </summary>
/// <param name="rng">WC_RNG created from RandomNew()</param>
/// <param name="buf">buffer to populate random data</param>
/// <param name="sz">size of buffer</param>
/// <returns>0=success or negative for error</returns>
public static int Random(IntPtr rng, byte[] buf, int sz)
{
int ret;
IntPtr data;
try
{
/* Allocate global buffer for wolfAPI random */
data = Marshal.AllocHGlobal(sz);
if (data != IntPtr.Zero)
{
/* Generate random block */
ret = wc_RNG_GenerateBlock(rng, data, Convert.ToUInt32(sz));
if (ret == 0)
{
/* copy returned data */
Marshal.Copy(data, buf, 0, sz);
}
else
{
log(ERROR_LOG, "random generate block error " + ret + ": " + GetError(ret));
}
Marshal.FreeHGlobal(data);
}
else
{
ret = MEMORY_E;
}
}
catch (Exception e)
{
log(ERROR_LOG, "random generate block exception " + e.ToString());
ret = EXCEPTION_E;
}
return ret;
}
/// <summary>
/// Generate random data (single shot)
/// </summary>
/// <param name="buf">buffer to populate random data</param>
/// <param name="sz">size of buffer</param>
/// <returns>0=success or negative for error</returns>
public static int Random(byte[] buf, int sz)
{
int ret;
IntPtr rng = RandomNew();
if (rng == IntPtr.Zero)
{
return MEMORY_E;
}
ret = Random(rng, buf, sz);
RandomFree(rng);
return ret;
}
/* END Random */
/***********************************************************************
* ECC
**********************************************************************/
/// <summary>
/// Generate a new ECC private / public key pair
/// </summary>
/// <param name="keysize">Key size in bytes (example: SECP256R1 = 32)</param>
/// <returns>Allocated ECC key structure or null</returns>
public static IntPtr EccMakeKey(int keysize, IntPtr rng)
{
int ret;
IntPtr key = IntPtr.Zero;
try
{
/* Allocate and init new WC_RNG structure */
key = wc_ecc_key_new(IntPtr.Zero);
if (key != IntPtr.Zero)
{
ret = wc_ecc_make_key_ex(rng, keysize, key, 0); /* 0=use default curve */
if (ret != 0)
{
EccFreeKey(key);
key = IntPtr.Zero;
}
}
}
catch (Exception e)
{
log(ERROR_LOG, "ECC make key exception " + e.ToString());
EccFreeKey(key);
key = IntPtr.Zero;
}
return key;
}
/// <summary>
/// Sets the ECC rng structure
/// </summary>
/// <param name="key">Supplied key as a pointer</param>
/// <param name="rng">rng context as a pointer</param>
/// <returns>Returns 0 on success</returns>
public static int EccSetRng(IntPtr key, IntPtr rng)
{
int ret = 0;
try
{
/* Check */
if (key == IntPtr.Zero)
{
log(ERROR_LOG, "Invalid key or rng pointer.");
return MEMORY_E;
}
/* Set ECC rng */
ret = wc_ecc_set_rng(key, rng);
if (ret != 0)
{
log(ERROR_LOG, "ECC set rng failed returned:" + ret);
}
}
catch (Exception e)
{
log(ERROR_LOG, "ECC set rng exception " + e.ToString());
}
return ret;
}
/// <summary>
/// Generate a new ECC private / public key pair
/// </summary>
/// <param name="keyASN1">ASN.1 private key buffer (see ecc_clikey_der_256)</param>
/// <returns>Allocated ECC key structure or null</returns>
public static IntPtr EccImportKey(byte[] keyASN1)
{
int ret;
IntPtr key = IntPtr.Zero;
try
{
key = wc_ecc_key_new(IntPtr.Zero);
if (key != IntPtr.Zero)
{
IntPtr idx = Marshal.AllocHGlobal(sizeof(uint));
IntPtr keydata = Marshal.AllocHGlobal(keyASN1.Length);
Marshal.WriteInt32(idx, 0);
Marshal.Copy(keyASN1, 0, keydata, keyASN1.Length);
ret = wc_EccPrivateKeyDecode(keydata, idx, key, Convert.ToUInt32(keyASN1.Length));
if (ret != 0)
{
EccFreeKey(key);
key = IntPtr.Zero;
}
Marshal.FreeHGlobal(idx); /* not used */
Marshal.FreeHGlobal(keydata);
}
}
catch (Exception e)
{
log(ERROR_LOG, "ECC import key exception " + e.ToString());
EccFreeKey(key); /* make sure its free'd */
key = IntPtr.Zero;
}
return key;
}
/// <summary>
/// Sign a hash using ECC
/// </summary>
/// <param name="key">ECC key structure</param>
/// <param name="hash">Hash to sign</param>
/// <param name="signature">Buffer to receive the signature</param>
/// <returns>Length of the signature on success, otherwise a negative error code</returns>
public static int EccSign(IntPtr key, byte[] hash, byte[] signature)
{
int ret;
int signedLength = 0;
IntPtr hashPtr = IntPtr.Zero;
IntPtr sigPtr = IntPtr.Zero;
IntPtr sigLen = IntPtr.Zero;
IntPtr rng = IntPtr.Zero;
try
{
rng = RandomNew();
hashPtr = Marshal.AllocHGlobal(hash.Length);
sigPtr = Marshal.AllocHGlobal(signature.Length);
sigLen = Marshal.AllocHGlobal(sizeof(uint));
Marshal.WriteInt32(sigLen, signature.Length);
Marshal.Copy(hash, 0, hashPtr, hash.Length);
ret = wc_ecc_sign_hash(hashPtr, Convert.ToUInt32(hash.Length), sigPtr, sigLen, rng, key);
/* Output actual signature length */
if (ret == 0)
{
signedLength = Marshal.ReadInt32(sigLen);
if (signedLength <= signature.Length)
{
Marshal.Copy(sigPtr, signature, 0, signedLength);
}
else
{
ret = BUFFER_E;
}
}
}
catch (Exception e)
{
log(ERROR_LOG, "ECC sign exception: " + e.ToString());
ret = EXCEPTION_E;
}
finally
{
if (hashPtr != IntPtr.Zero) Marshal.FreeHGlobal(hashPtr);
if (sigPtr != IntPtr.Zero) Marshal.FreeHGlobal(sigPtr);
if (sigLen != IntPtr.Zero) Marshal.FreeHGlobal(sigLen);
if (rng != IntPtr.Zero) RandomFree(rng);
}
return ret == 0 ? signedLength : ret;
}
/// <summary>
/// Verify a signature using ECC
/// </summary>
/// <param name="key">ECC key structure</param>
/// <param name="signature">Signature to verify</param>
/// <param name="hash">Expected hash value</param>
/// <returns>0 on success, otherwise an error code</returns>
public static int EccVerify(IntPtr key, byte[] signature, byte[] hash)
{
int ret;
IntPtr hashPtr = IntPtr.Zero;
IntPtr sigPtr = IntPtr.Zero;
IntPtr res = IntPtr.Zero;
try
{
hashPtr = Marshal.AllocHGlobal(hash.Length);
sigPtr = Marshal.AllocHGlobal(signature.Length);
res = Marshal.AllocHGlobal(sizeof(int));
Marshal.Copy(hash, 0, hashPtr, hash.Length);
Marshal.Copy(signature, 0, sigPtr, signature.Length);
ret = wc_ecc_verify_hash(sigPtr, Convert.ToUInt32(signature.Length), hashPtr, Convert.ToUInt32(hash.Length), res, key);
if (ret == 0)
{
int verifyResult = Marshal.ReadInt32(res);
ret = verifyResult == 1 ? 0 : EXCEPTION_E;
}
}
catch (Exception e)
{
log(ERROR_LOG, "ECC verify exception " + e.ToString());
ret = EXCEPTION_E;
}
finally
{
if (hashPtr != IntPtr.Zero) Marshal.FreeHGlobal(hashPtr);
if (sigPtr != IntPtr.Zero) Marshal.FreeHGlobal(sigPtr);
if (res != IntPtr.Zero) Marshal.FreeHGlobal(res);
}
return ret;
}
/// <summary>
/// Export ECC Private Key to DER format
/// </summary>
/// <param name="key">ECC key structure</param>
/// <returns>DER-encoded private key as byte array</returns>
public static int EccExportPrivateKeyToDer(IntPtr key, out byte[] derKey)
{
int ret;
derKey = null;
try
{
int bufferSize = wc_EccPrivateKeyToDer(key, null, 0);
if (bufferSize < 0) {
log(ERROR_LOG, "ECC private key get size failed " + bufferSize.ToString());
return bufferSize;
}
derKey = new byte[bufferSize];
ret = wc_EccPrivateKeyToDer(key, derKey, (uint)bufferSize);
if (ret < 0)
{
log(ERROR_LOG, "ECC private key to der failed " + ret.ToString());
}
}
catch (Exception e)
{
log(ERROR_LOG, "ECC export private exception " + e.ToString());
ret = EXCEPTION_E;
}
return ret;
}
/// <summary>
/// Export ECC Public Key to DER format
/// </summary>
/// <param name="key">ECC key structure</param>
/// <param name="includeCurve">Include algorithm curve in the output</param>
/// <returns>DER-encoded public key as byte array</returns>
public static int EccExportPublicKeyToDer(IntPtr key, out byte[] derKey, bool includeCurve)
{
int ret;
derKey = null;
try
{
int bufferSize = wc_EccPublicKeyToDer(key, null, 0, includeCurve ? 1 : 0);
if (bufferSize < 0) {
log(ERROR_LOG, "ECC public key get size failed " + bufferSize.ToString());
return bufferSize;
}
derKey = new byte[bufferSize];
ret = wc_EccPublicKeyToDer(key, derKey, (uint)bufferSize, includeCurve ? 1 : 0);
if (ret < 0)
{
log(ERROR_LOG, "ECC public key to der failed " + ret.ToString());
}
}
catch (Exception e)
{
log(ERROR_LOG, "ECC export public exception " + e.ToString());
ret = EXCEPTION_E;
}
return ret;
}
/// <summary>
/// Import ECC Public Key from DER format
/// </summary>
/// <param name="keyDer">DER-encoded public key</param>
/// <returns>Allocated ECC key structure or null</returns>
public static IntPtr EccImportPublicKeyFromDer(byte[] keyDer)
{
int ret;
IntPtr key = IntPtr.Zero;
try
{
key = wc_ecc_key_new(IntPtr.Zero);
if (key != IntPtr.Zero)
{
uint idx = 0;
ret = wc_EccPublicKeyDecode(keyDer, ref idx, key, (uint)keyDer.Length);
if (ret != 0)
{
EccFreeKey(key);
key = IntPtr.Zero;
}
}
}
catch (Exception e)
{
log(ERROR_LOG, "ECC import public key exception " + e.ToString());
EccFreeKey(key);
key = IntPtr.Zero;
}
return key;
}
/// <summary>
/// Free an ECC key structure
/// </summary>
/// <param name="key">ECC key structure allocated using EccMakeKey() or EccImportKey()</param>
public static void EccFreeKey(IntPtr key)
{
if (key != IntPtr.Zero)
{
wc_ecc_key_free(key);
}
}
/* END ECC */
/***********************************************************************
* ECIES
**********************************************************************/
/// <summary>
/// Create a new ECIES context with flags, RNG, and custom heap.
/// </summary>
/// <param name="flags">Flags for the context initialization.</param>
/// <param name="rng">Random Number Generator (RNG) pointer.</param>
/// <param name="heap">Custom heap pointer for memory allocations.</param>
/// <returns>Pointer to the newly created ECIES context or IntPtr.Zero on failure.</returns>
public static IntPtr EciesNewCtx(int flags, IntPtr rng, IntPtr heap)
{
IntPtr ctx = IntPtr.Zero;
heap = IntPtr.Zero;
try
{
ctx = wc_ecc_ctx_new_ex(flags, rng, heap);
if (ctx == IntPtr.Zero)
{
log(ERROR_LOG, "ECIES context creation with custom heap failed: returned IntPtr.Zero");
}
}
catch (Exception e)
{
log(ERROR_LOG, "ECIES context creation with custom heap failed: " + e.ToString());
return IntPtr.Zero;
}
return ctx;
}
/// <summary>
/// Reset the ECIES context with a new RNG.
/// </summary>
/// <param name="ctx">Pointer to the ECIES context to reset.</param>
/// <param name="rng">New RNG to set.</param>
/// <returns>0 on success, or a negative error code on failure.</returns>
public static int EciesCtxReset(IntPtr ctx, IntPtr rng)
{
int ret;
try
{
ret = wc_ecc_ctx_reset(ctx, rng);
}
catch (Exception e)
{
log(ERROR_LOG, "ECIES context reset exception: " + e.ToString());
ret = EXCEPTION_E;
}
return ret;
}
/// <summary>
/// Set encryption, KDF, and MAC algorithms for the ECIES context.
/// </summary>
/// <param name="ctx">Pointer to the ECIES context.</param>
/// <param name="encAlgo">Encryption algorithm identifier.</param>
/// <param name="kdfAlgo">Key Derivation Function (KDF) algorithm identifier.</param>
/// <param name="macAlgo">MAC algorithm identifier.</param>
/// <returns>0 on success, or a negative error code on failure.</returns>
public static int EciesSetAlgo(IntPtr ctx, byte encAlgo, byte kdfAlgo, byte macAlgo)
{
int ret;
try
{
ret = wc_ecc_ctx_set_algo(ctx, encAlgo, kdfAlgo, macAlgo);
}
catch (Exception e)
{
log(ERROR_LOG, "ECIES set algorithm exception: " + e.ToString());
ret = EXCEPTION_E;
}
return ret;
}
/// <summary>
/// Get the ECIES own salt as a byte array.
/// </summary>
/// <param name="ctx">Pointer to the ECIES context.</param>
/// <returns>Byte array representing the own salt, or null if there is an error.</returns>
public static byte[] EciesGetOwnSalt(IntPtr ctx)
{
IntPtr saltPtr = IntPtr.Zero;
byte[] salt = null;
try
{
/* Check ctx */
if (ctx == IntPtr.Zero)
{
log(ERROR_LOG, "Invalid ECIES context pointer.");
return null;
}
/* Get own salt */
saltPtr = wc_ecc_ctx_get_own_salt(ctx);
if (saltPtr == IntPtr.Zero)
{
log(ERROR_LOG, "Failed to get own salt.");
return null;
}
/* Allocate salt size and copy to byte array */
salt = new byte[(int)ecKeySize.EXCHANGE_SALT_SZ];
Marshal.Copy(saltPtr, salt, 0, (int)ecKeySize.EXCHANGE_SALT_SZ);
}
catch (Exception e)
{
log(ERROR_LOG, "ECIES get own salt exception: " + e.ToString());
return null;
}
finally
{
/* Cleanup */
if (saltPtr != IntPtr.Zero) Marshal.FreeHGlobal(saltPtr);
}
return salt;
}
/// <summary>
/// Set the peer salt for the ECIES context.
/// </summary>
/// <param name="ctx">Pointer to the ECIES context.</param>
/// <param name="salt">Peer salt as a byte array.</param>
/// <returns>0 on success, or a negative error code on failure.</returns>
public static int EciesSetPeerSalt(IntPtr ctx, byte[] salt)
{
IntPtr saltPtr = IntPtr.Zero;
int ret;
try
{
/* Allocate memory */
saltPtr = Marshal.AllocHGlobal(salt.Length);
Marshal.Copy(salt, 0, saltPtr, salt.Length);
/* Set the peer salt */
ret = wc_ecc_ctx_set_peer_salt(ctx, saltPtr);
}
catch (Exception e)
{
log(ERROR_LOG, "ECIES set peer salt exception: " + e.ToString());
ret = EXCEPTION_E;
}
finally
{
/* Cleanup */
if (saltPtr != IntPtr.Zero) Marshal.FreeHGlobal(saltPtr);
}
return ret;
}
/// <summary>
/// Set the own salt for the ECIES context.
/// </summary>
/// <param name="ctx">Pointer to the ECIES context.</param>
/// <param name="salt">Own salt as a byte array.</param>
/// <returns>0 on success, or a negative error code on failure.</returns>
public static int EciesSetOwnSalt(IntPtr ctx, byte[] salt)
{
IntPtr saltPtr = IntPtr.Zero;
uint saltSz;
int ret;
try
{
/* Allocate memory */
saltSz = (uint)salt.Length;
saltPtr = Marshal.AllocHGlobal(salt.Length);
Marshal.Copy(salt, 0, saltPtr, salt.Length);
/* Set the own salt */
ret = wc_ecc_ctx_set_own_salt(ctx, saltPtr, saltSz);
}
catch (Exception e)
{
log(ERROR_LOG, "ECIES set own salt exception: " + e.ToString());
ret = EXCEPTION_E;
}
finally
{
/* Cleanup */
if (saltPtr != IntPtr.Zero) Marshal.FreeHGlobal(saltPtr);
}
return ret;
}
/// <summary>
/// Set the KDF salt for the ECIES context.
/// </summary>
/// <param name="ctx">Pointer to the ECIES context.</param>
/// <param name="salt">KDF salt as a byte array.</param>
/// <returns>0 on success, or a negative error code on failure.</returns>
public static int EciesSetKdfSalt(IntPtr ctx, byte[] salt)
{
IntPtr saltPtr = IntPtr.Zero;
uint saltSz;
int ret;
try
{
/* Allocate memory */
saltSz = (uint)salt.Length;
saltPtr = Marshal.AllocHGlobal(salt.Length);
Marshal.Copy(salt, 0, saltPtr, salt.Length);
/* Set the KDF salt */
ret = wc_ecc_ctx_set_kdf_salt(ctx, saltPtr, saltSz);
}
catch (Exception e)
{
log(ERROR_LOG, "ECIES set KDF salt exception: " + e.ToString());
ret = EXCEPTION_E;
}
finally
{
/* Cleanup */
if (saltPtr != IntPtr.Zero) Marshal.FreeHGlobal(saltPtr);
}
return ret;
}
/// <summary>
/// Set the info for the ECIES context.
/// </summary>
/// <param name="ctx">Pointer to the ECIES context.</param>
/// <param name="info">Info as a byte array.</param>
/// <returns>0 on success, or a negative error code on failure.</returns>
public static int EciesSetInfo(IntPtr ctx, byte[] info)
{
IntPtr infoPtr = IntPtr.Zero;
int ret;
try
{
/* Allocate memory */
infoPtr = Marshal.AllocHGlobal(info.Length);
Marshal.Copy(info, 0, infoPtr, info.Length);
/* Set the info */
ret = wc_ecc_ctx_set_info(ctx, infoPtr, info.Length);
}
catch (Exception e)
{
log(ERROR_LOG, "ECIES set info exception: " + e.ToString());
ret = EXCEPTION_E;
}
finally
{
/* Cleanup */
if (infoPtr != IntPtr.Zero) Marshal.FreeHGlobal(infoPtr);
}
return ret;
}
/// <summary>
/// Encrypt a message using ECIES.
/// </summary>
/// <param name="privKey">Private key.</param>
/// <param name="pubKey">Public key.</param>
/// <param name="msg">Message to encrypt.</param>
/// <param name="msgSz">Message size.</param>
/// <param name="outBuffer">Output buffer.</param>
/// <param name="ctx">ECIES context.</param>
/// <returns>0 on success, or a negative error code on failure.</returns>
public static int EciesEncrypt(IntPtr privKey, IntPtr pubKey, byte[] msg, uint msgSz, byte[] outBuffer, IntPtr ctx)
{
int ret;
int outBufferLength = 0;
IntPtr msgPtr = IntPtr.Zero;
IntPtr outBufferPtr = IntPtr.Zero;
IntPtr outSz = IntPtr.Zero;
try
{
/* Allocate memory */
msgPtr = Marshal.AllocHGlobal(msg.Length);
outBufferPtr = Marshal.AllocHGlobal(outBuffer.Length);
outSz = Marshal.AllocHGlobal(sizeof(uint));
Marshal.WriteInt32(outSz, outBuffer.Length);
Marshal.Copy(msg, 0, msgPtr, msg.Length);
/* Encrypt */
ret = wc_ecc_encrypt(privKey, pubKey, msgPtr, msgSz, outBufferPtr, outSz, ctx);
if (ret < 0)
{
log(ERROR_LOG, "Failed to encrypt message using ECIES. Error code: " + ret);
}
/* Output actual output buffer length */
if (ret == 0)
{
outBufferLength = Marshal.ReadInt32(outSz);
if (outBufferLength <= outBuffer.Length)
{
Marshal.Copy(outBufferPtr, outBuffer, 0, outBufferLength);
}
else
{
ret = BUFFER_E;
}
}
}
catch (Exception e)
{
log(ERROR_LOG, "ECIES encryption exception: " + e.ToString());
ret = EXCEPTION_E;
}
finally
{
/* Cleanup */
if (msgPtr != IntPtr.Zero) Marshal.FreeHGlobal(msgPtr);
if (outBufferPtr != IntPtr.Zero) Marshal.FreeHGlobal(outBufferPtr);
if (outSz != IntPtr.Zero) Marshal.FreeHGlobal(outSz);
}
return ret == 0 ? outBufferLength : ret;
}
/// <summary>
/// Decrypt a message using ECIES.
/// </summary>
/// <param name="privKey">Private key.</param>
/// <param name="pubKey">Public key.</param>
/// <param name="msg">Encrypted message.</param>
/// <param name="msgSz">Message size.</param>
/// <param name="outBuffer">Output buffer for the decrypted message.</param>
/// <param name="ctx">ECIES context.</param>
/// <returns>0 on success, or a negative error code on failure.</returns>
public static int EciesDecrypt(IntPtr privKey, IntPtr pubKey, byte[] msg, uint msgSz, byte[] outBuffer, IntPtr ctx)
{
int ret;
int outBufferLength = 0;
IntPtr msgPtr = IntPtr.Zero;
IntPtr outBufferPtr = IntPtr.Zero;
IntPtr outSz = IntPtr.Zero;
try
{
/* Allocate memory */
msgPtr = Marshal.AllocHGlobal(msg.Length);
outBufferPtr = Marshal.AllocHGlobal(outBuffer.Length);
outSz = Marshal.AllocHGlobal(sizeof(uint));
Marshal.WriteInt32(outSz, outBuffer.Length);
Marshal.Copy(msg, 0, msgPtr, msg.Length);
/* Decrypt */
ret = wc_ecc_decrypt(privKey, pubKey, msgPtr, msgSz, outBufferPtr, outSz, ctx);
if (ret < 0)
{
log(ERROR_LOG, "Failed to decrypt message using ECIES. Error code: " + ret);
}
/* Output actual output buffer length */
if (ret == 0)
{
outBufferLength = Marshal.ReadInt32(outSz);
if (outBufferLength <= outBuffer.Length)
{
Marshal.Copy(outBufferPtr, outBuffer, 0, outBufferLength);
}
else
{
ret = BUFFER_E;
}
}
}
catch (Exception e)
{
log(ERROR_LOG, "ECIES decryption exception: " + e.ToString());
return EXCEPTION_E;
}
finally
{
/* Cleanup */
if (msgPtr != IntPtr.Zero) Marshal.FreeHGlobal(msgPtr);
if (outBufferPtr != IntPtr.Zero) Marshal.FreeHGlobal(outBufferPtr);
if (outSz != IntPtr.Zero) Marshal.FreeHGlobal(outSz);
}
return ret == 0 ? outBufferLength : ret;
}
/// <summary>
/// Free the ECIES context.
/// </summary>
/// <param name="ctx">Pointer to the ECIES context to free.</param>
public static void EciesFreeCtx(IntPtr ctx)
{
if (ctx != IntPtr.Zero)
{
wc_ecc_ctx_free(ctx);
}
}
/********************************
* ENUMS
*/
public enum ecEncAlgo {
ecAES_128_CBC = 1, /* default */
ecAES_256_CBC = 2,
ecAES_128_CTR = 3,
ecAES_256_CTR = 4
}
public enum ecKdfAlgo {
ecHKDF_SHA256 = 1, /* default */
ecHKDF_SHA1 = 2,
ecKDF_X963_SHA1 = 3,
ecKDF_X963_SHA256 = 4,
ecKDF_SHA1 = 5,
ecKDF_SHA256 = 6
}
public enum ecMacAlgo {
ecHMAC_SHA256 = 1, /* default */
ecHMAC_SHA1 = 2
}
public enum ecKeySize {
KEY_SIZE_128 = 16,
KEY_SIZE_256 = 32,
IV_SIZE_64 = 8,
IV_SIZE_128 = 16,
ECC_MAX_IV_SIZE = 16,
EXCHANGE_SALT_SZ = 16,
EXCHANGE_INFO_SZ = 23
}
public enum ecFlags {
REQ_RESP_CLIENT = 1,
REQ_RESP_SERVER = 2
}
/* END ECIES */
/***********************************************************************
* ECDHE
**********************************************************************/
/// <summary>
/// Generate a shared secret using ECC
/// </summary>
/// <param name="privateKey">ECC private key</param>
/// <param name="publicKey">ECC public key</param>
/// <param name="secret">Buffer to receive the shared secret</param>
/// <returns>0 on success, otherwise an error code</returns>
public static int EcdheSharedSecret(IntPtr privateKey, IntPtr publicKey, byte[] secret, IntPtr rng)
{
int ret;
int secretLength = secret.Length;
try
{
/* set RNG for Public Key */
ret = EccSetRng(privateKey, rng);
if (ret != 0)
{
throw new Exception("Failed to set Public Key RNG Error code: " + ret);
}
/* set RNG for Private Key */
ret = EccSetRng(publicKey, rng);
if (ret != 0)
{
throw new Exception("Failed to set Private Key RNG. Error code: " + ret);
}
/* Generate shared secret */
if (privateKey != IntPtr.Zero || publicKey != IntPtr.Zero)
{
ret = wc_ecc_shared_secret(privateKey, publicKey, secret, ref secretLength);
if (ret != 0)
{
throw new Exception("Failed to compute ECC shared secret. Error code: " + ret);
}
}
}
catch (Exception e)
{
log(ERROR_LOG, "ECC shared secret exception " + e.ToString());
ret = EXCEPTION_E;
}
return ret;
}
/* END ECDHE */
/***********************************************************************
* RSA
**********************************************************************/
/// <summary>
/// Generate a new RSA private/public key pair
/// </summary>
/// <param name="heap">Pointer to the heap for memory allocation
/// (use IntPtr.Zero if not applicable)</param>
/// <param name="devId">Device ID (if applicable, otherwise use 0)</param>
/// <param name="keysize">Key size in bits (example: 2048)</param>
/// <param name="exponent">Exponent for RSA key generation (default is 65537)</param>
/// <returns>Allocated RSA key structure or null on failure</returns>
public static IntPtr RsaMakeKey(IntPtr heap, int devId, int keysize, Int32 exponent)
{
int ret;
IntPtr key = IntPtr.Zero;
IntPtr rng = IntPtr.Zero;
try
{
/* Allocate and init new RSA key structure */
key = wc_NewRsaKey(heap, devId, IntPtr.Zero);
if (key != IntPtr.Zero)
{
rng = RandomNew();
if (rng == IntPtr.Zero)
{
throw new Exception("Failed to create rng.");
}
ret = wc_MakeRsaKey(key, keysize, exponent, rng);
if (ret != 0)
{
RsaFreeKey(key);
key = IntPtr.Zero;
}
RandomFree(rng);
rng = IntPtr.Zero;
}
}
catch (Exception e)
{
log(ERROR_LOG, "RSA make key exception " + e.ToString());
if (rng != IntPtr.Zero) RandomFree(rng);
if (key != IntPtr.Zero) RsaFreeKey(key);
key = IntPtr.Zero;
}
return key;
}
public static IntPtr RsaMakeKey(IntPtr heap, int devId, int keysize)
{
return RsaMakeKey(heap, devId, keysize, 65537);
}
/// <summary>
/// Import an RSA private key from ASN.1 buffer
/// </summary>
/// <param name="keyASN1">ASN.1 private key buffer</param>
/// <returns>Allocated RSA key structure or null</returns>
public static IntPtr RsaImportKey(byte[] keyASN1)
{
int ret;
IntPtr key = IntPtr.Zero;
try
{
key = wc_NewRsaKey(IntPtr.Zero, INVALID_DEVID, IntPtr.Zero);
if (key != IntPtr.Zero)
{
IntPtr idx = Marshal.AllocHGlobal(sizeof(uint));
IntPtr keydata = Marshal.AllocHGlobal(keyASN1.Length);
Marshal.WriteInt32(idx, 0);
Marshal.Copy(keyASN1, 0, keydata, keyASN1.Length);
ret = wc_RsaPrivateKeyDecode(keydata, idx, key, Convert.ToUInt32(keyASN1.Length));
if (ret != 0)
{
RsaFreeKey(key);
key = IntPtr.Zero;
}
Marshal.FreeHGlobal(idx); /* not used */
Marshal.FreeHGlobal(keydata);
}
}
catch (Exception e)
{
log(ERROR_LOG, "RSA make key exception " + e.ToString());
RsaFreeKey(key); /* make sure its free'd */
key = IntPtr.Zero;
}
return key;
}
/// <summary>
/// Sign a hash using RSA and SSL-style padding
/// </summary>
/// <param name="key">RSA key structure</param>
/// <param name="hash">Hash to sign</param>
/// <param name="signature">Buffer to receive the signature</param>
/// <returns>Length of the signature on success, otherwise an error code</returns>
public static int RsaSignSSL(IntPtr key, byte[] hash, byte[] signature)
{
IntPtr hashPtr = Marshal.AllocHGlobal(hash.Length);
IntPtr sigPtr = Marshal.AllocHGlobal(signature.Length);
IntPtr rng = IntPtr.Zero;
int ret;
try
{
rng = RandomNew();
if (rng == IntPtr.Zero)
{
throw new Exception("Failed to create RNG.");
}
Marshal.Copy(hash, 0, hashPtr, hash.Length);
ret = wc_RsaSSL_Sign(hashPtr, hash.Length, sigPtr, signature.Length, key, rng);
if (ret >= 0) /* `wc_RsaSSL_Sign` returns the signature length on success */
{
Marshal.Copy(sigPtr, signature, 0, ret);
}
}
finally
{
if (hashPtr != IntPtr.Zero) Marshal.FreeHGlobal(hashPtr);
if (sigPtr != IntPtr.Zero) Marshal.FreeHGlobal(sigPtr);
if (rng != IntPtr.Zero) RandomFree(rng);
}
return ret;
}
/// <summary>
/// Verify a signature using RSA and SSL-style padding
/// </summary>
/// <param name="key">RSA key structure</param>
/// <param name="signature">Signature to verify</param>
/// <param name="hash">Expected hash value</param>
/// <returns>0 on success, otherwise an error code</returns>
public static int RsaVerifySSL(IntPtr key, byte[] signature, byte[] hash)
{
IntPtr hashPtr = IntPtr.Zero;
IntPtr sigPtr = IntPtr.Zero;
int ret;
try
{
hashPtr = Marshal.AllocHGlobal(hash.Length);
sigPtr = Marshal.AllocHGlobal(signature.Length);
Marshal.Copy(signature, 0, sigPtr, signature.Length);
ret = wc_RsaSSL_Verify(sigPtr, signature.Length, hashPtr, hash.Length, key);
if (ret == hash.Length)
{
byte[] verifiedHash = new byte[hash.Length];
Marshal.Copy(hashPtr, verifiedHash, 0, hash.Length);
if (ByteArrayVerify(verifiedHash, hash))
{
ret = 0;
}
else
{
ret = SIG_VERIFY_E;
}
}
}
catch (Exception e)
{
log(ERROR_LOG, "RSA verify exception: " + e.ToString());
ret = EXCEPTION_E;
}
finally
{
if (hashPtr != IntPtr.Zero) Marshal.FreeHGlobal(hashPtr);
if (sigPtr != IntPtr.Zero) Marshal.FreeHGlobal(sigPtr);
}
return ret;
}
/// <summary>
/// Encrypt data using RSA public key encryption
/// </summary>
/// <param name="key">RSA key structure</param>
/// <param name="input">Data to encrypt</param>
/// <param name="output">Buffer to receive the encrypted data</param>
/// <returns>0 on success, otherwise an error code</returns>
public static int RsaPublicEncrypt(IntPtr key, byte[] input, byte[] output)
{
IntPtr inPtr = Marshal.AllocHGlobal(input.Length);
IntPtr outPtr = Marshal.AllocHGlobal(output.Length);
Marshal.Copy(input, 0, inPtr, input.Length);
int ret = wc_RsaPublicEncrypt(inPtr, input.Length, outPtr, output.Length, key);
if (ret > 0)
{
Marshal.Copy(outPtr, output, 0, ret);
}
Marshal.FreeHGlobal(inPtr);
Marshal.FreeHGlobal(outPtr);
return ret > 0 ? 0 : ret;
}
/// <summary>
/// Decrypt data using RSA private key decryption
/// </summary>
/// <param name="key">RSA key structure</param>
/// <param name="input">Encrypted data</param>
/// <param name="output">Buffer to receive the decrypted data</param>
/// <returns>0 on success, otherwise an error code</returns>
public static int RsaPrivateDecrypt(IntPtr key, byte[] input, byte[] output)
{
IntPtr inPtr = Marshal.AllocHGlobal(input.Length);
IntPtr outPtr = Marshal.AllocHGlobal(output.Length);
Marshal.Copy(input, 0, inPtr, input.Length);
int ret = wc_RsaPrivateDecrypt(inPtr, input.Length, outPtr, output.Length, key);
if (ret > 0)
{
Marshal.Copy(outPtr, output, 0, ret);
}
Marshal.FreeHGlobal(inPtr);
Marshal.FreeHGlobal(outPtr);
return ret > 0 ? 0 : ret;
}
/// <summary>
/// Free an RSA key structure
/// </summary>
/// <param name="key">RSA key structure allocated using RsaMakeKey() or RsaImportKey()</param>
public static void RsaFreeKey(IntPtr key)
{
if (key != IntPtr.Zero)
{
wc_DeleteRsaKey(key, IntPtr.Zero);
key = IntPtr.Zero;
}
}
/* END RSA */
/***********************************************************************
* ED25519
**********************************************************************/
/// <summary>
/// Generate a new ED25519 key pair with a specified heap, device ID, and internally managed RNG.
/// </summary>
/// <param name="heap">Heap to use for memory allocations (can be IntPtr.Zero).</param>
/// <param name="devId">Device ID for hardware-based keys (can be 0 for software).</param>
/// <returns>0 on success, or an error code on failure.</returns>
public static IntPtr Ed25519MakeKey(IntPtr heap, int devId)
{
int ret = 0;
IntPtr rng = IntPtr.Zero;
IntPtr key = IntPtr.Zero;
try
{
rng = RandomNew();
if (rng == IntPtr.Zero)
{
throw new Exception("Failed to create RNG.");
}
key = wc_ed25519_new(heap, devId, IntPtr.Zero);
if (key != IntPtr.Zero)
{
ret = wc_ed25519_make_key(rng, 32, key);
}
}
catch (Exception e)
{
log(ERROR_LOG, "ED25519 make key exception: " + e.ToString());
ret = EXCEPTION_E;
}
finally
{
/* Cleanup */
if (rng != IntPtr.Zero) RandomFree(rng);
if (ret != 0)
{
wc_ed25519_delete(key, IntPtr.Zero);
key = IntPtr.Zero;
}
}
return key;
}
/// <summary>
/// Sign a message with an ED25519 private key.
/// </summary>
/// <param name="inMsg">Message to be signed</param>
/// <param name="outMsg">Buffer to receive the signature</param>
/// <param name="key">Private key used for signing</param>
/// <returns>0 on success, otherwise an error code</returns>
public static int Ed25519SignMsg(byte[] inMsg, out byte[] outMsg, IntPtr key)
{
int ret;
IntPtr inMsgPtr = Marshal.AllocHGlobal(inMsg.Length);
IntPtr outMsgPtr = Marshal.AllocHGlobal(ED25519_SIG_SIZE);
outMsg = null;
try
{
Marshal.Copy(inMsg, 0, inMsgPtr, inMsg.Length);
uint outMsgSize = (uint)ED25519_SIG_SIZE;
ret = wc_ed25519_sign_msg(inMsgPtr, (uint)inMsg.Length, outMsgPtr, ref outMsgSize, key);
if (ret == 0)
{
outMsg = new byte[outMsgSize];
Marshal.Copy(outMsgPtr, outMsg, 0, (int)outMsgSize);
}
}
finally
{
/* Cleanup */
if (inMsgPtr != IntPtr.Zero) Marshal.FreeHGlobal(inMsgPtr);
if (outMsgPtr != IntPtr.Zero) Marshal.FreeHGlobal(outMsgPtr);
}
return ret;
}
/// <summary>
/// Verify a signature of a message with an ED25519 public key.
/// </summary>
/// <param name="sig">Signature to verify</param>
/// <param name="msg">Message that was signed</param>
/// <param name="key">Public key used for verification</param>
/// <returns>0 if the verification succeeds, otherwise an error code</returns>
public static int Ed25519VerifyMsg(byte[] sig, byte[] msg, IntPtr key)
{
IntPtr sigPtr = IntPtr.Zero;
IntPtr msgPtr = IntPtr.Zero;
int ret = 0;
try
{
/* Allocate memory */
sigPtr = Marshal.AllocHGlobal(sig.Length);
msgPtr = Marshal.AllocHGlobal(msg.Length);
Marshal.Copy(sig, 0, sigPtr, sig.Length);
Marshal.Copy(msg, 0, msgPtr, msg.Length);
int verify = 0;
ret = wc_ed25519_verify_msg(sigPtr, (uint)sig.Length, msgPtr, (uint)msg.Length, ref verify, key);
if (ret == 0 && verify == 1)
{
ret = 0;
}
else
{
ret = SIG_VERIFY_E;
}
}
catch (Exception e)
{
log(ERROR_LOG, "ED25519 verify exception: " + e.ToString());
ret = EXCEPTION_E;
}
finally
{
/* Cleanup */
if (sigPtr != IntPtr.Zero) Marshal.FreeHGlobal(sigPtr);
if (msgPtr != IntPtr.Zero) Marshal.FreeHGlobal(msgPtr);
}
return ret;
}
/// <summary>
/// Decode an ED25519 private key from DER format.
/// </summary>
/// <param name="input">DER-encoded private key as byte array.</param>
/// <returns>Allocated ED25519 key structure or IntPtr.Zero on failure.</returns>
public static IntPtr Ed25519PrivateKeyDecode(byte[] input)
{
IntPtr key = IntPtr.Zero;
uint idx = 0;
int ret;
try
{
key = wc_ed25519_new(IntPtr.Zero, INVALID_DEVID, IntPtr.Zero);
if (key != IntPtr.Zero)
{
ret = wc_Ed25519PrivateKeyDecode(input, ref idx, key, (uint)input.Length);
if (ret != 0)
{
Ed25519FreeKey(key);
key = IntPtr.Zero;
}
}
}
catch (Exception e)
{
log(ERROR_LOG, "ED25519 private key decode exception: " + e.ToString());
if (key != IntPtr.Zero) Ed25519FreeKey(key);
key = IntPtr.Zero;
}
return key;
}
/// <summary>
/// Decode an ED25519 public key from DER format.
/// </summary>
/// <param name="input">DER-encoded public key as byte array.</param>
/// <returns>Allocated ED25519 key structure or IntPtr.Zero on failure.</returns>
public static IntPtr Ed25519PublicKeyDecode(byte[] input)
{
IntPtr key = IntPtr.Zero;
uint idx = 0;
int ret;
try
{
key = wc_ed25519_new(IntPtr.Zero, INVALID_DEVID, IntPtr.Zero);
if (key != IntPtr.Zero)
{
ret = wc_Ed25519PublicKeyDecode(input, ref idx, key, (uint)input.Length);
if (ret != 0)
{
Ed25519FreeKey(key);
key = IntPtr.Zero;
}
}
}
catch (Exception e)
{
log(ERROR_LOG, "ED25519 public key decode exception: " + e.ToString());
if (key != IntPtr.Zero) Ed25519FreeKey(key);
key = IntPtr.Zero;
}
return key;
}
/// <summary>
/// Export an ED25519 key to DER format.
/// </summary>
/// <param name="key">ED25519 key structure.</param>
/// <param name="privKey">DER-encoded public key as byte array.</param>
/// <returns>DER-encoded key as byte array.</returns>
public static int Ed25519ExportKeyToDer(IntPtr key, out byte[] privKey)
{
int ret;
privKey = null;
try
{
/* Get length */
int len = wc_Ed25519KeyToDer(key, null, 0);
if (len < 0)
{
log(ERROR_LOG, "Failed to determine length. Error code: " + len);
return len;
}
privKey = new byte[len];
ret = wc_Ed25519KeyToDer(key, privKey, (uint)privKey.Length);
if (ret < 0)
{
log(ERROR_LOG, "Failed to export ED25519 private key to DER format. Error code: " + ret);
return ret;
}
}
catch(Exception e)
{
log(ERROR_LOG, "ED25519 export private key to DER exception: " + e.ToString());
return EXCEPTION_E;
}
return ret;
}
/// <summary>
/// Export an ED25519 private key to DER format.
/// </summary>
/// <param name="key">ED25519 private key structure.</param>
/// <param name="derKey">DER-encoded private key as byte array.</param>
/// <returns>DER-encoded private key as byte array.</returns>
public static int Ed25519ExportPrivateKeyToDer(IntPtr key, out byte[] derKey)
{
int ret;
derKey = null;
try
{
/* Determine length */
int len = wc_Ed25519PrivateKeyToDer(key, null, 0);
if (len < 0)
{
log(ERROR_LOG, "Failed to determine length. Error code: " + len);
return len;
}
derKey = new byte[len];
ret = wc_Ed25519PrivateKeyToDer(key, derKey, (uint)derKey.Length);
if (ret < 0)
{
log(ERROR_LOG, "Failed to export ED25519 private key to DER format. Error code: " + ret);
return ret;
}
}
catch (Exception e)
{
log(ERROR_LOG, "ED25519 export private key to DER exception: " + e.ToString());
return EXCEPTION_E;
}
return ret;
}
/// <summary>
/// Export an ED25519 public key to DER format.
/// </summary>
/// <param name="key">ED25519 public key structure.</param>
/// <param name="includeAlg">Whether to include the algorithm identifier in the output.</param>
/// <param name="pubKey">DER-encoded public key as byte array.</param>
/// <returns>An error code indicating success (0) or failure (negative value).</returns>
public static int Ed25519ExportPublicKeyToDer(IntPtr key, out byte[] pubKey, bool includeAlg)
{
int ret;
pubKey = null;
try
{
/* Determine length */
int len = wc_Ed25519PublicKeyToDer(key, null, 0, 1);
if (len < 0)
{
log(ERROR_LOG, "Failed to determine length. Error code: " + len);
return len;
}
pubKey = new byte[len];
ret = wc_Ed25519PublicKeyToDer(key, pubKey, (uint)pubKey.Length, includeAlg ? 1 : 0);
if (ret < 0)
{
log(ERROR_LOG, "Failed to export ED25519 public key to DER format. Error code: " + ret);
return ret;
}
}
catch (Exception e)
{
log(ERROR_LOG, "ED25519 export public key to DER exception: " + e.ToString());
return EXCEPTION_E;
}
return ret;
}
/// <summary>
/// Free an ED25519 key.
/// </summary>
/// <param name="key">Key to be freed</param>
public static void Ed25519FreeKey(IntPtr key)
{
wc_ed25519_delete(key, IntPtr.Zero);
key = IntPtr.Zero;
}
/* END ED25519 */
/***********************************************************************
* RAW ED25519
**********************************************************************/
/// <summary>
/// Initialize an ED25519 key.
/// </summary>
/// <param name="key">Buffer to receive the initialized key</param>
/// <returns>0 on success, otherwise an error code</returns>
public static int Ed25519InitKey(out IntPtr key)
{
key = IntPtr.Zero;
try
{
key = Marshal.AllocHGlobal(ED25519_SIG_SIZE);
int ret = wc_ed25519_init(key);
if (ret != 0)
{
Marshal.FreeHGlobal(key);
key = IntPtr.Zero;
}
return ret;
}
catch
{
/* Cleanup */
Marshal.FreeHGlobal(key);
key = IntPtr.Zero;
throw;
}
}
/// <summary>
/// Import a public key into an ED25519 key structure.
/// </summary>
/// <param name="inMsg">Public key to import</param>
/// <param name="inLen">Length of the public key</param>
/// <param name="key">Buffer to receive the imported key</param>
/// <returns>0 on success, otherwise an error code</returns>
public static int Ed25519ImportPublic(byte[] inMsg, uint inLen, out IntPtr key)
{
int ret;
key = IntPtr.Zero;
IntPtr inMsgPtr = IntPtr.Zero;
try
{
/* Allocate memory */
key = wc_ed25519_new(IntPtr.Zero, INVALID_DEVID, IntPtr.Zero);
if (key == IntPtr.Zero)
{
throw new OutOfMemoryException("Failed to allocate memory for the key.");
}
inMsgPtr = Marshal.AllocHGlobal(inMsg.Length);
if (inMsgPtr == IntPtr.Zero)
{
throw new OutOfMemoryException("Failed to allocate memory for the input message.");
}
Marshal.Copy(inMsg, 0, inMsgPtr, inMsg.Length);
ret = wc_ed25519_import_public(inMsgPtr, inLen, key);
if (ret != 0)
{
if (key != IntPtr.Zero) {
wc_ed25519_delete(key, IntPtr.Zero);
key = IntPtr.Zero;
}
return ret;
}
}
catch (Exception ex)
{
Console.WriteLine("Exception in EdImportPublic: " + ex.Message);
if (key != IntPtr.Zero) {
wc_ed25519_delete(key, IntPtr.Zero);
key = IntPtr.Zero;
}
return EXCEPTION_E;
}
finally
{
/* Cleanup */
if (inMsgPtr != IntPtr.Zero) Marshal.FreeHGlobal(inMsgPtr);
}
return ret;
}
/// <summary>
/// Export a public key from an ED25519 key structure.
/// </summary>
/// <param name="key">ED25519 key structure</param>
/// <param name="outMsg">Buffer to receive the exported public key</param>
/// <param name="outLen">Length of the exported public key</param>
/// <returns>0 on success, otherwise an error code</returns>
public static int Ed25519ExportPublic(IntPtr key, byte[] outMsg, out uint outLen)
{
int ret;
IntPtr outMsgPtr = IntPtr.Zero;
try
{
outMsgPtr = Marshal.AllocHGlobal(outMsg.Length);
outLen = (uint)outMsg.Length;
ret = wc_ed25519_export_public(key, outMsgPtr, ref outLen);
if (ret == 0)
{
Marshal.Copy(outMsgPtr, outMsg, 0, (int)outLen);
}
else
{
outLen = 0;
}
}
finally
{
/* Cleanup */
if (outMsgPtr != IntPtr.Zero) Marshal.FreeHGlobal(outMsgPtr);
}
return ret;
}
/// <summary>
/// Export a private key from an ED25519 key structure.
/// </summary>
/// <param name="key">ED25519 key structure</param>
/// <param name="outMsg">Buffer to receive the exported private key</param>
/// <param name="outLen">Length of the exported private key</param>
/// <returns>0 on success, otherwise an error code</returns>
public static int Ed25519ExportPrivate(IntPtr key, byte[] outMsg, out uint outLen)
{
int ret;
IntPtr outMsgPtr = IntPtr.Zero;
try
{
outMsgPtr = Marshal.AllocHGlobal(outMsg.Length);
outLen = (uint)outMsg.Length;
ret = wc_ed25519_export_private(key, outMsgPtr, ref outLen);
if (ret == 0)
{
Marshal.Copy(outMsgPtr, outMsg, 0, (int)outLen);
}
else
{
outLen = 0;
}
}
finally
{
/* Cleanup */
if (outMsgPtr != IntPtr.Zero) Marshal.FreeHGlobal(outMsgPtr);
}
return ret;
}
/// <summary>
/// Generate a public key from a private key.
/// </summary>
/// <param name="key">The private key used to generate the public key</param>
/// <param name="pubKey">Buffer to receive the public key</param>
/// <param name="pubKeySz">Size of the public key buffer</param>
/// <returns>0 on success, otherwise an error code</returns>
public static int Ed25519MakePublic(IntPtr key, byte[] pubKey, out uint pubKeySz)
{
int ret;
IntPtr pubKeyPtr = Marshal.AllocHGlobal(pubKey.Length);
try
{
pubKeySz = (uint)pubKey.Length;
ret = wc_ed25519_make_public(key, pubKeyPtr, pubKeySz);
if (ret == 0)
{
Marshal.Copy(pubKeyPtr, pubKey, 0, (int)pubKeySz);
}
}
finally
{
/* Cleanup */
if (pubKeyPtr != IntPtr.Zero) Marshal.FreeHGlobal(pubKeyPtr);
}
return ret;
}
/// <summary>
/// Get the size of the ED25519 key.
/// </summary>
/// <param name="key">ED25519 key structure</param>
/// <returns>Size of the key, or an error code if failed</returns>
public static int Ed25519GetKeySize(IntPtr key)
{
return wc_ed25519_size(key);
}
/* END RAW ED25519 */
/***********************************************************************
* Curve25519
**********************************************************************/
/// <summary>
/// Generate a new Curve25519 key pair with a specified heap, device ID, and internally managed RNG.
/// </summary>
/// <param name="heap">Heap to use for memory allocations (can be IntPtr.Zero).</param>
/// <param name="devId">Device ID for hardware-based keys (can be 0 for software).</param>
/// <returns>0 on success, or an error code on failure.</returns>
public static IntPtr Curve25519MakeKey(IntPtr heap, int devId)
{
int ret = 0;
IntPtr rng = IntPtr.Zero;
IntPtr key = IntPtr.Zero;
try
{
rng = RandomNew();
if (rng == IntPtr.Zero)
{
throw new Exception("Failed to create RNG.");
}
key = wc_curve25519_new(heap, devId, IntPtr.Zero);
if (key != IntPtr.Zero)
{
ret = wc_curve25519_make_key(rng, 32, key);
}
}
catch (Exception e)
{
log(ERROR_LOG, "Curve25519 make key exception: " + e.ToString());
ret = EXCEPTION_E;
}
finally
{
/* Cleanup */
if (rng != IntPtr.Zero) RandomFree(rng);
if (ret != 0)
{
wc_curve25519_delete(key, IntPtr.Zero);
key = IntPtr.Zero;
}
}
return key;
}
/// <summary>
/// Decode an Curve25519 private key from DER format.
/// </summary>
/// <param name="input">DER-encoded private key as byte array.</param>
/// <returns>Allocated Curve25519 key structure or IntPtr.Zero on failure.</returns>
public static IntPtr Curve25519PrivateKeyDecode(byte[] input)
{
IntPtr key = IntPtr.Zero;
uint idx = 0;
int ret;
try
{
key = wc_curve25519_new(IntPtr.Zero, INVALID_DEVID, IntPtr.Zero);
if (key != IntPtr.Zero)
{
ret = wc_Ed25519PrivateKeyDecode(input, ref idx, key, (uint)input.Length);
if (ret != 0)
{
Curve25519FreeKey(key);
key = IntPtr.Zero;
}
}
}
catch (Exception e)
{
log(ERROR_LOG, "Curve25519 private key decode exception: " + e.ToString());
if (key != IntPtr.Zero) Curve25519FreeKey(key);
key = IntPtr.Zero;
}
return key;
}
/// <summary>
/// Decode an Curve25519 public key from DER format.
/// </summary>
/// <param name="input">DER-encoded public key as byte array.</param>
/// <returns>Allocated Curve25519 key structure or IntPtr.Zero on failure.</returns>
public static IntPtr Curve25519PublicKeyDecode(byte[] input)
{
IntPtr key = IntPtr.Zero;
uint idx = 0;
int ret;
try
{
key = wc_curve25519_new(IntPtr.Zero, INVALID_DEVID, IntPtr.Zero);
if (key != IntPtr.Zero)
{
ret = wc_Curve25519PublicKeyDecode(input, ref idx, key, (uint)input.Length);
if (ret != 0)
{
Curve25519FreeKey(key);
key = IntPtr.Zero;
}
}
}
catch (Exception e)
{
log(ERROR_LOG, "Curve25519 public key decode exception: " + e.ToString());
if (key != IntPtr.Zero) Curve25519FreeKey(key);
key = IntPtr.Zero;
}
return key;
}
/// <summary>
/// Export an Curve25519 key to DER format.
/// </summary>
/// <param name="key">Curve25519 key structure.</param>
/// <param name="derKey">DER-encoded public key as byte array.</param>
/// <returns>DER-encoded key as byte array.</returns>
public static int Curve25519ExportPrivateKeyToDer(IntPtr key, out byte[] derKey)
{
int ret;
derKey = null;
try
{
/* Determine length */
int len = wc_Curve25519PrivateKeyToDer(key, null, 0);
if (len < 0)
{
log(ERROR_LOG, "Failed to determine length. Error code: " + len);
return len;
}
derKey = new byte[len];
ret = wc_Curve25519PrivateKeyToDer(key, derKey, (uint)derKey.Length);
if (ret < 0)
{
log(ERROR_LOG, "Failed to export Curve25519 private key to DER format. Error code: " + ret);
return ret;
}
}
catch (Exception e)
{
log(ERROR_LOG, "CURVE25519 export private key to DER exception: " + e.ToString());
return EXCEPTION_E;
}
return ret;
}
/// <summary>
/// Export an Curve25519 public key to DER format.
/// </summary>
/// <param name="key">Curve25519 public key structure.</param>
/// <param name="includeAlg">Whether to include the algorithm identifier in the output.</param>
/// <param name="derKey">DER-encoded public key as byte array.</param>
/// <returns>An error code indicating success (0) or failure (negative value).</returns>
public static int Curve25519ExportPublicKeyToDer(IntPtr key, out byte[] derKey, bool includeAlg)
{
int ret;
derKey = null;
try
{
/* Determine length */
int len = wc_Curve25519PublicKeyToDer(key, null, 0, 1);
if (len < 0)
{
log(ERROR_LOG, "Failed to determine length. Error code: " + len);
return len;
}
derKey = new byte[len];
ret = wc_Curve25519PublicKeyToDer(key, derKey, (uint)derKey.Length, includeAlg ? 1 : 0);
if (ret < 0)
{
log(ERROR_LOG, "Failed to export Curve25519 public key to DER format. Error code: " + ret);
}
}
catch (Exception e)
{
log(ERROR_LOG, "Curve25519 export public key to DER exception: " + e.ToString());
ret = EXCEPTION_E;
}
return ret;
}
/// <summary>
/// Free an Curve25519 key.
/// </summary>
/// <param name="key">Key to be freed</param>
public static void Curve25519FreeKey(IntPtr key)
{
wc_curve25519_delete(key, IntPtr.Zero);
key = IntPtr.Zero;
}
/* END Curve25519 */
/***********************************************************************
* RAW Curve25519
**********************************************************************/
/// <summary>
/// Generate a shared secret using Curve25519
/// </summary>
/// <param name="privateKey">Curve25519 private key</param>
/// <param name="publicKey">Curve25519 public key</param>
/// <param name="secret">Buffer to receive the shared secret</param>
/// <returns>0 on success, otherwise an error code</returns>
public static int Curve25519SharedSecret(IntPtr privateKey, IntPtr publicKey, byte[] secret)
{
int ret;
int secretLength = secret.Length;
try
{
ret = wc_curve25519_shared_secret(privateKey, publicKey, secret, ref secretLength);
if (ret != 0)
{
throw new Exception("Failed to compute Curve25519 shared secret. Error code: " + ret);
}
}
catch (Exception e)
{
log(ERROR_LOG, "Curve25519 shared secret exception " + e.ToString());
ret = EXCEPTION_E;
}
return ret;
}
/// <summary>
/// Import a Curve25519 private key from a byte array
/// </summary>
/// <param name="privateKey">Private key byte array</param>
/// <returns>Allocated Curve25519 key structure or null</returns>
public static IntPtr Curve25519ImportPrivateKey(byte[] privateKey)
{
IntPtr key = IntPtr.Zero;
try
{
key = Marshal.AllocHGlobal(privateKey.Length);
Marshal.Copy(privateKey, 0, key, privateKey.Length);
int ret = wc_curve25519_import_private(key, privateKey.Length, key);
if (ret != 0)
{
Marshal.FreeHGlobal(key);
key = IntPtr.Zero;
}
}
catch (Exception e)
{
log(ERROR_LOG, "Curve25519 import private key exception " + e.ToString());
if (key != IntPtr.Zero) Marshal.FreeHGlobal(key);
key = IntPtr.Zero;
}
return key;
}
/// <summary>
/// Import a Curve25519 public key from a byte array
/// </summary>
/// <param name="publicKey">Public key byte array</param>
/// <returns>Allocated Curve25519 key structure or null</returns>
public static IntPtr Curve25519ImportPublicKey(byte[] publicKey)
{
IntPtr key = IntPtr.Zero;
try
{
key = Marshal.AllocHGlobal(publicKey.Length);
Marshal.Copy(publicKey, 0, key, publicKey.Length);
int ret = wc_curve25519_import_public(key, publicKey.Length, key);
if (ret != 0)
{
Marshal.FreeHGlobal(key);
key = IntPtr.Zero;
}
}
catch (Exception e)
{
log(ERROR_LOG, "Curve25519 import public key exception " + e.ToString());
if (key != IntPtr.Zero) Marshal.FreeHGlobal(key);
key = IntPtr.Zero;
}
return key;
}
/// <summary>
/// Export a Curve25519 private key to a byte array
/// </summary>
/// <param name="key">Curve25519 key structure</param>
/// <returns>Private key as byte array</returns>
public static byte[] Curve25519ExportPrivateKey(IntPtr key)
{
byte[] privateKey = new byte[ED25519_KEY_SIZE];
uint privSize = (uint)privateKey.Length;
int ret = wc_curve25519_export_public(key, privateKey, ref privSize);
if (ret != 0)
{
throw new Exception("Failed to export Curve25519 private key. Error code: " + ret);
}
return privateKey;
}
/// <summary>
/// Export a Curve25519 public key to a byte array
/// </summary>
/// <param name="key">Curve25519 key structure</param>
/// <returns>Public key as byte array</returns>
public static byte[] Curve25519ExportPublicKey(IntPtr key)
{
byte[] publicKey = new byte[ED25519_PUB_KEY_SIZE];
uint pubSize = (uint)publicKey.Length;
int ret = wc_curve25519_export_public(key, publicKey, ref pubSize);
if (ret != 0)
{
throw new Exception("Failed to export Curve25519 public key. Error code: " + ret);
}
return publicKey;
}
/// <summary>
/// Export both private and public keys from a Curve25519 key structure
/// </summary>
/// <param name="key">Curve25519 key structure</param>
/// <param name="privateKey">returned raw private key as byte array</param>
/// <param name="publicKey">returned raw public key as byte array</param>
public static void Curve25519ExportKeyRaw(IntPtr key, out byte[] privateKey, out byte[] publicKey)
{
privateKey = new byte[ED25519_KEY_SIZE];
publicKey = new byte[ED25519_PUB_KEY_SIZE];
uint privSize = (uint)privateKey.Length;
uint pubSize = (uint)publicKey.Length;
int ret = wc_curve25519_export_key_raw(key, privateKey, ref privSize, publicKey, ref pubSize);
if (ret != 0)
{
throw new Exception("Failed to export Curve25519 keys. Error code: " + ret);
}
return;
}
/* END RAW Curve25519 */
/***********************************************************************
* AES-GCM
**********************************************************************/
/// <summary>
/// Creates a new AES context.
/// </summary>
/// <param name="heap">Pointer to a memory heap, or IntPtr.Zero to use the default heap.</param>
/// <param name="devId">The device ID to associate with this AES context.</param>
/// <returns>A pointer to the newly created AES context, or IntPtr.Zero on failure.</returns>
public static IntPtr AesNew(IntPtr heap, int devId)
{
IntPtr aesPtr = IntPtr.Zero;
try
{
aesPtr = wc_AesNew(heap, devId, IntPtr.Zero);
if (aesPtr == IntPtr.Zero)
{
throw new Exception("Failed to create AES context.");
}
}
catch (Exception ex)
{
Console.WriteLine("AES context creation failed: " + ex.Message);
}
return aesPtr;
}
/// <summary>
/// Initialize and set the AES key for AES-GCM operations.
/// </summary>
/// <param name="aes">AES-GCM context pointer.</param>
/// <param name="key">The AES key (either 128, 192, or 256 bits).</param>
/// <returns>0 on success, otherwise an error code.</returns>
public static int AesGcmSetKey(IntPtr aes, byte[] key)
{
IntPtr keyPtr = IntPtr.Zero;
int ret;
try
{
/* Allocate memory */
keyPtr = Marshal.AllocHGlobal(key.Length);
Marshal.Copy(key, 0, keyPtr, key.Length);
ret = wc_AesGcmSetKey(aes, keyPtr, (uint)key.Length);
if (ret != 0)
{
throw new Exception("AES-GCM initialization failed with error code ret = " + ret.ToString());
}
}
finally
{
/* Cleanup */
if (keyPtr != IntPtr.Zero) Marshal.FreeHGlobal(keyPtr);
}
return ret;
}
/// <summary>
/// Wrapper method to initialize the AES-GCM context with a given key and IV.
/// </summary>
/// <param name="aes">Pointer to the AES-GCM context that needs to be initialized.</param>
/// <param name="key">Byte array containing the AES key.</param>
/// <param name="iv">Byte array containing the initialization vector (IV).</param>
public static int AesGcmInit(IntPtr aes, byte[] key, byte[] iv)
{
IntPtr keyPtr = IntPtr.Zero;
IntPtr ivPtr = IntPtr.Zero;
int ret;
try
{
/* Allocate memory for key and IV */
keyPtr = Marshal.AllocHGlobal(key.Length);
Marshal.Copy(key, 0, keyPtr, key.Length);
ivPtr = Marshal.AllocHGlobal(iv.Length);
Marshal.Copy(iv, 0, ivPtr, iv.Length);
ret = wc_AesGcmInit(aes, keyPtr, (uint)key.Length, ivPtr, (uint)iv.Length);
if (ret != 0)
{
throw new Exception("AES-GCM initialization failed with error code ret = " + ret.ToString());
}
}
finally
{
/* Cleanup */
if (keyPtr != IntPtr.Zero) Marshal.FreeHGlobal(keyPtr);
if (ivPtr != IntPtr.Zero) Marshal.FreeHGlobal(ivPtr);
}
return ret;
}
/// <summary>
/// Encrypt data using AES-GCM
/// </summary>
/// <param name="aes">AES-GCM context pointer.</param>
/// <param name="iv">Initialization Vector (IV)</param>
/// <param name="plaintext">Data to encrypt</param>
/// <param name="ciphertext">Buffer to receive the encrypted data</param>
/// <param name="authTag">Buffer to receive the authentication tag</param>
/// <returns>0 on success, otherwise an error code</returns>
public static int AesGcmEncrypt(IntPtr aes, byte[] iv, byte[] plaintext,
byte[] ciphertext, byte[] authTag, byte[] addAuth)
{
int ret;
IntPtr ivPtr = IntPtr.Zero;
IntPtr ciphertextPtr = IntPtr.Zero;
IntPtr plaintextPtr = IntPtr.Zero;
IntPtr authTagPtr = IntPtr.Zero;
IntPtr addAuthPtr = IntPtr.Zero;
uint addAuthSz = 0;
try
{
/* Allocate memory */
ivPtr = Marshal.AllocHGlobal(iv.Length);
ciphertextPtr = Marshal.AllocHGlobal(ciphertext.Length);
plaintextPtr = Marshal.AllocHGlobal(plaintext.Length);
authTagPtr = Marshal.AllocHGlobal(authTag.Length);
if (addAuth != null) {
addAuthSz = (uint)addAuth.Length;
addAuthPtr = Marshal.AllocHGlobal(addAuth.Length);
Marshal.Copy(addAuth, 0, addAuthPtr, addAuth.Length);
}
Marshal.Copy(iv, 0, ivPtr, iv.Length);
Marshal.Copy(plaintext, 0, plaintextPtr, plaintext.Length);
/* Encrypt data */
ret = wc_AesGcmEncrypt(aes, ciphertextPtr, plaintextPtr, (uint)plaintext.Length,
ivPtr, (uint)iv.Length, authTagPtr, (uint)authTag.Length, addAuthPtr, addAuthSz);
if (ret < 0)
{
log(ERROR_LOG, "Failed to Encrypt data using AES-GCM. Error code: " + ret);
}
else {
Marshal.Copy(ciphertextPtr, ciphertext, 0, ciphertext.Length);
Marshal.Copy(authTagPtr, authTag, 0, authTag.Length);
ret = 0;
}
}
catch (Exception e)
{
log(ERROR_LOG, "AES-GCM Encryption failed: " + e.ToString());
ret = EXCEPTION_E;
}
finally
{
/* Cleanup */
if (ivPtr != IntPtr.Zero) Marshal.FreeHGlobal(ivPtr);
if (ciphertextPtr != IntPtr.Zero) Marshal.FreeHGlobal(ciphertextPtr);
if (plaintextPtr != IntPtr.Zero) Marshal.FreeHGlobal(plaintextPtr);
if (authTagPtr != IntPtr.Zero) Marshal.FreeHGlobal(authTagPtr);
if (addAuthPtr != IntPtr.Zero) Marshal.FreeHGlobal(addAuthPtr);
}
return ret;
}
public static int AesGcmEncrypt(IntPtr aes, byte[] iv, byte[] plaintext,
byte[] ciphertext, byte[] authTag)
{
return AesGcmEncrypt(aes, iv, plaintext, ciphertext, null);
}
/// <summary>
/// Decrypt data using AES-GCM
/// </summary>
/// <param name="aes">AES-GCM context pointer.</param>
/// <param name="iv">Initialization Vector (IV)</param>
/// <param name="ciphertext">Data to decrypt</param>
/// <param name="plaintext">Buffer to receive the decrypted data</param>
/// <param name="authTag">Authentication tag for verification</param>
/// <returns>0 on success, otherwise an error code</returns>
public static int AesGcmDecrypt(IntPtr aes, byte[] iv, byte[] ciphertext,
byte[] plaintext, byte[] authTag, byte[] addAuth)
{
int ret;
IntPtr ivPtr = IntPtr.Zero;
IntPtr ciphertextPtr = IntPtr.Zero;
IntPtr plaintextPtr = IntPtr.Zero;
IntPtr authTagPtr = IntPtr.Zero;
IntPtr addAuthPtr = IntPtr.Zero;
uint addAuthSz = 0;
try
{
/* Allocate memory */
ivPtr = Marshal.AllocHGlobal(iv.Length);
ciphertextPtr = Marshal.AllocHGlobal(ciphertext.Length);
plaintextPtr = Marshal.AllocHGlobal(plaintext.Length);
authTagPtr = Marshal.AllocHGlobal(authTag.Length);
if (addAuth != null) {
addAuthSz = (uint)addAuth.Length;
addAuthPtr = Marshal.AllocHGlobal(addAuth.Length);
Marshal.Copy(addAuth, 0, addAuthPtr, addAuth.Length);
}
Marshal.Copy(iv, 0, ivPtr, iv.Length);
Marshal.Copy(ciphertext, 0, ciphertextPtr, ciphertext.Length);
Marshal.Copy(authTag, 0, authTagPtr, authTag.Length);
/* Decrypt data */
ret = wc_AesGcmDecrypt(aes, plaintextPtr, ciphertextPtr, (uint)ciphertext.Length,
ivPtr, (uint)iv.Length, authTagPtr, (uint)authTag.Length, addAuthPtr, addAuthSz);
if (ret < 0)
{
log(ERROR_LOG, "Failed to Decrypt data using AES-GCM. Error code: " + ret);
}
else {
Marshal.Copy(plaintextPtr, plaintext, 0, plaintext.Length);
ret = 0;
}
}
catch (Exception e)
{
log(ERROR_LOG, "AES-GCM Decryption failed: " + e.ToString());
ret = EXCEPTION_E;
}
finally
{
/* Cleanup */
if (ivPtr != IntPtr.Zero) Marshal.FreeHGlobal(ivPtr);
if (ciphertextPtr != IntPtr.Zero) Marshal.FreeHGlobal(ciphertextPtr);
if (plaintextPtr != IntPtr.Zero) Marshal.FreeHGlobal(plaintextPtr);
if (authTagPtr != IntPtr.Zero) Marshal.FreeHGlobal(authTagPtr);
if (addAuthPtr != IntPtr.Zero) Marshal.FreeHGlobal(addAuthPtr);
}
return ret;
}
public static int AesGcmDecrypt(IntPtr aes, byte[] iv, byte[] ciphertext,
byte[] plaintext, byte[] authTag)
{
return AesGcmDecrypt(aes, iv, ciphertext, plaintext, authTag, null);
}
/// <summary>
/// Free AES-GCM context
/// </summary>
/// <param name="aes">AES-GCM context</param>
public static void AesGcmFree(IntPtr aes)
{
if (aes != IntPtr.Zero)
{
wc_AesDelete(aes, IntPtr.Zero);
aes = IntPtr.Zero;
}
}
/* END AES-GCM */
/***********************************************************************
* HASH
**********************************************************************/
/// <summary>
/// Allocate and set up a new hash context with proper error handling
/// </summary>
/// <param name="hashType">The type of hash (SHA-256, SHA-384, etc.)</param>
/// <param name="heap">Pointer to the heap for memory allocation (use IntPtr.Zero if not applicable)</param>
/// <param name="devId">Device ID (if applicable, otherwise use INVALID_DEVID)</param>
/// <returns>Allocated hash context pointer or IntPtr.Zero on failure</returns>
public static IntPtr HashNew(uint hashType, IntPtr heap, int devId)
{
IntPtr hash = IntPtr.Zero;
try
{
/* Allocate new hash */
hash = wc_HashNew(hashType, heap, devId, IntPtr.Zero);
if (hash == IntPtr.Zero)
{
throw new Exception("Failed to allocate new hash context.");
}
}
catch (Exception e)
{
log(ERROR_LOG, "HashNew Exception: " + e.ToString());
}
return hash;
}
/// <summary>
/// Initialize the hash context for a specific hash type with proper error handling
/// </summary>
/// <param name="hash">Hash context pointer</param>
/// <param name="hashType">The type of hash (SHA-256, SHA-384, etc.)</param>
/// <returns>0 on success, otherwise an error code</returns>
public static int InitHash(IntPtr hash, uint hashType)
{
int ret = 0;
try
{
/* Check hash */
if (hash == IntPtr.Zero)
throw new Exception("Hash context is null.");
ret = wc_HashInit(hash, hashType);
if (ret != 0)
{
throw new Exception("Failed to initialize hash context. Error code: ret = " + ret.ToString());
}
}
catch (Exception e)
{
/* Cleanup */
log(ERROR_LOG, "InitHash Exception: " + e.ToString());
if (hash != IntPtr.Zero) {
wc_HashDelete(hash, IntPtr.Zero);
hash = IntPtr.Zero;
}
}
return ret;
}
/// <summary>
/// Update the hash with data
/// </summary>
/// <param name="hash">Hash context pointer</param>
/// <param name="hashType">The type of hash</param>
/// <param name="data">Byte array of the data to hash</param>
/// <returns>0 on success, otherwise an error code</returns>
public static int HashUpdate(IntPtr hash, uint hashType, byte[] data)
{
int ret = 0;
IntPtr dataPtr = IntPtr.Zero;
try
{
/* Check parameters */
if (hash == IntPtr.Zero)
throw new Exception("Hash context is null.");
if (data == null || data.Length == 0)
throw new Exception("Invalid data array.");
/* Allocate memory */
dataPtr = Marshal.AllocHGlobal(data.Length);
Marshal.Copy(data, 0, dataPtr, data.Length);
/* Update hash */
ret = wc_HashUpdate(hash, hashType, dataPtr, (uint)data.Length);
if (ret != 0)
{
throw new Exception("Failed to update hash. Error code: ret = " + ret.ToString());
}
}
catch (Exception e)
{
log(ERROR_LOG, "HashUpdate Exception: " + e.ToString());
}
finally
{
/* Cleanup */
if (dataPtr != IntPtr.Zero) Marshal.FreeHGlobal(dataPtr);
}
return ret;
}
/// <summary>
/// Finalize the hash and output the result
/// </summary>
/// <param name="hash">Hash context pointer</param>
/// <param name="hashType">The type of hash</param>
/// <param name="output">Byte array where the hash output will be stored</param>
/// <returns>0 on success, otherwise an error code</returns>
public static int HashFinal(IntPtr hash, uint hashType, out byte[] output)
{
int ret = 0;
IntPtr outputPtr = IntPtr.Zero;
try
{
/* Get hash size and initialize */
int hashSize = wc_HashGetDigestSize(hashType);
output = new byte[hashSize];
/* Check hash */
if (hash == IntPtr.Zero)
throw new Exception("Hash context is null.");
if (hashSize <= 0)
throw new Exception("Invalid hash size.");
/* Allocate memory */
outputPtr = Marshal.AllocHGlobal(hashSize);
ret = wc_HashFinal(hash, hashType, outputPtr);
if (ret != 0)
{
throw new Exception("Failed to finalize hash. Error code: ret = " + ret.ToString());
}
Marshal.Copy(outputPtr, output, 0, hashSize);
}
catch (Exception e)
{
log(ERROR_LOG, "HashFinal Exception: " + e.ToString());
output = null;
}
finally
{
/* Cleanup */
if (outputPtr != IntPtr.Zero) Marshal.FreeHGlobal(outputPtr);
}
return ret;
}
/// <summary>
/// Free the allocated hash context with proper error handling
/// </summary>
/// <param name="hash">Hash context pointer to be freed</param>
/// <param name="hashType">The type of hash</param>
/// <returns>0 on success, otherwise an error code</returns>
public static int HashFree(IntPtr hash, uint hashType)
{
int ret = 0;
try
{
/* Check hash */
if (hash == IntPtr.Zero)
throw new Exception("Hash context is null, cannot free.");
/* Free hash */
ret = wc_HashDelete(hash, IntPtr.Zero);
hash = IntPtr.Zero;
if (ret != 0)
{
throw new Exception("Failed to free hash context. Error code: ret = " + ret.ToString());
}
}
catch (Exception e)
{
log(ERROR_LOG, "HashFree Exception: " + e.ToString());
}
return ret;
}
/// <summary>
/// Hash type enum values
/// </summary>
public enum hashType
{
WC_HASH_TYPE_NONE = 0,
WC_HASH_TYPE_MD2 = 1,
WC_HASH_TYPE_MD4 = 2,
WC_HASH_TYPE_MD5 = 3,
WC_HASH_TYPE_SHA = 4, /* SHA-1 (not old SHA-0) */
WC_HASH_TYPE_SHA224 = 5,
WC_HASH_TYPE_SHA256 = 6,
WC_HASH_TYPE_SHA384 = 7,
WC_HASH_TYPE_SHA512 = 8,
WC_HASH_TYPE_MD5_SHA = 9,
WC_HASH_TYPE_SHA3_224 = 10,
WC_HASH_TYPE_SHA3_256 = 11,
WC_HASH_TYPE_SHA3_384 = 12,
WC_HASH_TYPE_SHA3_512 = 13,
WC_HASH_TYPE_BLAKE2B = 14,
WC_HASH_TYPE_BLAKE2S = 15,
}
/* END HASH */
/***********************************************************************
* Logging / Other
**********************************************************************/
/// <summary>
/// Set the function to use for logging
/// </summary>
/// <param name="input">Function that conforms as to loggingCb</param>
/// <returns>0 on success</returns>
public static int SetLogging(loggingCb input)
{
internal_log = input;
return SUCCESS;
}
/// <summary>
/// Get error string for wolfCrypt error codes
/// </summary>
/// <param name="error">Negative error number from wolfCrypt API</param>
/// <returns>Error string</returns>
public static string GetError(int error)
{
try
{
IntPtr errStr = wc_GetErrorString(error);
return wolfssl.PtrToStringAnsi(errStr);
}
catch (Exception e)
{
log(ERROR_LOG, "Get error exception " + e.ToString());
return string.Empty;
}
}
/// <summary>
/// Compares two byte arrays.
/// </summary>
/// <param name="array1">The first byte array to compare.</param>
/// <param name="array2">The second byte array to compare.</param>
/// <returns>True if both arrays are equal; otherwise, false.</returns>
public static bool ByteArrayVerify(byte[] array1, byte[] array2)
{
if (ReferenceEquals(array1, array2)) return true;
if (array1 == null || array2 == null) return false;
if (array1.Length != array2.Length) return false;
for (int i = 0; i < array1.Length; i++)
{
if (array1[i] != array2[i]) return false;
}
return true;
}
}
}
|