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
|
#include <string.h>
#include <openssl/core_dispatch.h>
#include <openssl/params.h>
#include <openssl/err.h>
#include <openssl/rsa.h>
#include <openssl/core_names.h>
#include <openssl/evp.h>
#include <openssl/proverr.h>
#include <openssl/rand.h>
#include <openssl/sha.h>
#include "e_qat.h"
#include "qat_provider.h"
#include "qat_prov_rsa.h"
#include "qat_utils.h"
#ifdef ENABLE_QAT_FIPS
# include "qat_prov_cmvp.h"
#endif
#ifdef QAT_HW
#include "qat_hw_rsa.h"
#endif
#ifdef QAT_SW
#include "qat_sw_rsa.h"
#endif
# define OSSL_NELEM(x) (sizeof(x)/sizeof((x)[0]))
#define rsa_pss_restricted(prsactx) (prsactx->min_saltlen != -1)
#define SSL_SIG_LENGTH 36
#define RSA_DEFAULT_DIGEST_NAME OSSL_DIGEST_NAME_SHA1
#define ASN1_SEQUENCE 0x30
#define ASN1_OCTET_STRING_ 0x04
#define ASN1_NULL 0x05
#define ASN1_OID 0x06
#define NID_mdc2 95
#define ENCODE_DIGESTINFO_SHA(name, n, sz) \
static const unsigned char digestinfo_##name##_der[] = { \
ASN1_SEQUENCE, 0x11 + sz, \
ASN1_SEQUENCE, 0x0d, \
ASN1_OID, 0x09, 2 * 40 + 16, 0x86, 0x48, 1, 101, 3, 4, 2, n, \
ASN1_NULL, 0x00, \
ASN1_OCTET_STRING_, sz \
};
#define RSA_KEY_SIZE 8
#define RSA_PSS_SALTLEN_AUTO_DIGEST_MAX -4
#define OSSL_PKEY_RSA_PSS_SALT_LEN_AUTO_DIGEST_MAX "auto-digestmax"
#if defined(ENABLE_QAT_HW_RSA) || defined(ENABLE_QAT_SW_RSA)
struct evp_signature_st {
int name_id;
char *type_name;
const char *description;
OSSL_PROVIDER *prov;
QAT_CRYPTO_REF_COUNT references;
#if OPENSSL_VERSION_NUMBER < 0x30200000
CRYPTO_RWLOCK *lock;
#endif
OSSL_FUNC_signature_newctx_fn *newctx;
OSSL_FUNC_signature_sign_init_fn *sign_init;
OSSL_FUNC_signature_sign_fn *sign;
#if OPENSSL_VERSION_NUMBER >= 0x30400000
OSSL_FUNC_signature_sign_message_init_fn *sign_message_init;
OSSL_FUNC_signature_sign_message_update_fn *sign_message_update;
OSSL_FUNC_signature_sign_message_final_fn *sign_message_final;
#endif
OSSL_FUNC_signature_verify_init_fn *verify_init;
OSSL_FUNC_signature_verify_fn *verify;
#if OPENSSL_VERSION_NUMBER >= 0x30400000
OSSL_FUNC_signature_verify_message_init_fn *verify_message_init;
OSSL_FUNC_signature_verify_message_update_fn *verify_message_update;
OSSL_FUNC_signature_verify_message_final_fn *verify_message_final;
#endif
OSSL_FUNC_signature_verify_recover_init_fn *verify_recover_init;
OSSL_FUNC_signature_verify_recover_fn *verify_recover;
OSSL_FUNC_signature_digest_sign_init_fn *digest_sign_init;
OSSL_FUNC_signature_digest_sign_update_fn *digest_sign_update;
OSSL_FUNC_signature_digest_sign_final_fn *digest_sign_final;
OSSL_FUNC_signature_digest_sign_fn *digest_sign;
OSSL_FUNC_signature_digest_verify_init_fn *digest_verify_init;
OSSL_FUNC_signature_digest_verify_update_fn *digest_verify_update;
OSSL_FUNC_signature_digest_verify_final_fn *digest_verify_final;
OSSL_FUNC_signature_digest_verify_fn *digest_verify;
OSSL_FUNC_signature_freectx_fn *freectx;
OSSL_FUNC_signature_dupctx_fn *dupctx;
OSSL_FUNC_signature_get_ctx_params_fn *get_ctx_params;
OSSL_FUNC_signature_gettable_ctx_params_fn *gettable_ctx_params;
OSSL_FUNC_signature_set_ctx_params_fn *set_ctx_params;
OSSL_FUNC_signature_settable_ctx_params_fn *settable_ctx_params;
OSSL_FUNC_signature_get_ctx_md_params_fn *get_ctx_md_params;
OSSL_FUNC_signature_gettable_ctx_md_params_fn *gettable_ctx_md_params;
OSSL_FUNC_signature_set_ctx_md_params_fn *set_ctx_md_params;
OSSL_FUNC_signature_settable_ctx_md_params_fn *settable_ctx_md_params;
} /* EVP_SIGNATURE */;
static EVP_SIGNATURE get_default_rsa_signature()
{
static EVP_SIGNATURE s_signature;
static int initilazed = 0;
if (!initilazed) {
EVP_SIGNATURE *signature = (EVP_SIGNATURE *)EVP_SIGNATURE_fetch(NULL, "RSA",
"provider=default");
if (signature) {
s_signature = *signature;
EVP_SIGNATURE_free((EVP_SIGNATURE *)signature);
initilazed = 1;
} else {
WARN("EVP_SIGNATURE_fetch from default provider failed");
}
}
return s_signature;
}
ENCODE_DIGESTINFO_SHA(sha256, 0x01, SHA256_DIGEST_LENGTH)
ENCODE_DIGESTINFO_SHA(sha384, 0x02, SHA384_DIGEST_LENGTH)
ENCODE_DIGESTINFO_SHA(sha512, 0x03, SHA512_DIGEST_LENGTH)
ENCODE_DIGESTINFO_SHA(sha224, 0x04, SHA224_DIGEST_LENGTH)
ENCODE_DIGESTINFO_SHA(sha512_224, 0x05, SHA224_DIGEST_LENGTH)
ENCODE_DIGESTINFO_SHA(sha512_256, 0x06, SHA256_DIGEST_LENGTH)
ENCODE_DIGESTINFO_SHA(sha3_224, 0x07, SHA224_DIGEST_LENGTH)
ENCODE_DIGESTINFO_SHA(sha3_256, 0x08, SHA256_DIGEST_LENGTH)
ENCODE_DIGESTINFO_SHA(sha3_384, 0x09, SHA384_DIGEST_LENGTH)
ENCODE_DIGESTINFO_SHA(sha3_512, 0x0a, SHA512_DIGEST_LENGTH)
static const unsigned char digestinfo_sha1_der[] = {
ASN1_SEQUENCE, 0x0d + SHA_DIGEST_LENGTH,
ASN1_SEQUENCE, 0x09,
ASN1_OID, 0x05, 1 * 40 + 3, 14, 3, 2, 26,
ASN1_NULL, 0x00,
ASN1_OCTET_STRING_, SHA_DIGEST_LENGTH
};
#define MD_CASE(name) \
case NID_##name: \
*len = sizeof(digestinfo_##name##_der); \
return digestinfo_##name##_der;
#define MD_NID_CASE(name, sz) \
case NID_##name: \
return sz;
const unsigned char *qat_rsa_digestinfo_encoding(int md_nid, size_t *len)
{
switch (md_nid) {
MD_CASE(sha1)
MD_CASE(sha224)
MD_CASE(sha256)
MD_CASE(sha384)
MD_CASE(sha512)
MD_CASE(sha512_224)
MD_CASE(sha512_256)
MD_CASE(sha3_224)
MD_CASE(sha3_256)
MD_CASE(sha3_384)
MD_CASE(sha3_512)
default:
return NULL;
}
}
static const unsigned char zeroes[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
static const OSSL_ITEM oaeppss_name_nid_map[] = {
{ NID_sha1, OSSL_DIGEST_NAME_SHA1 },
{ NID_sha224, OSSL_DIGEST_NAME_SHA2_224 },
{ NID_sha256, OSSL_DIGEST_NAME_SHA2_256 },
{ NID_sha384, OSSL_DIGEST_NAME_SHA2_384 },
{ NID_sha512, OSSL_DIGEST_NAME_SHA2_512 },
{ NID_sha512_224, OSSL_DIGEST_NAME_SHA2_512_224 },
{ NID_sha512_256, OSSL_DIGEST_NAME_SHA2_512_256 },
};
static OSSL_ITEM padding_item[] = {
{ RSA_PKCS1_PADDING, OSSL_PKEY_RSA_PAD_MODE_PKCSV15 },
{ RSA_NO_PADDING, OSSL_PKEY_RSA_PAD_MODE_NONE },
{ RSA_X931_PADDING, OSSL_PKEY_RSA_PAD_MODE_X931 },
{ RSA_PKCS1_PSS_PADDING, OSSL_PKEY_RSA_PAD_MODE_PSS },
{ 0, NULL }
};
const QAT_RSA_PSS_PARAMS_30 default_RSASSA_PSS_params = {
NID_sha1, /* default hashAlgorithm */
{
NID_mgf1, /* default maskGenAlgorithm */
NID_sha1 /* default MGF1 hash */
},
20, /* default saltLength */
1 /* default trailerField (0xBC) */
};
static const OSSL_PARAM settable_ctx_params[] = {
OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST, NULL, 0),
OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_PROPERTIES, NULL, 0),
OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_PAD_MODE, NULL, 0),
OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_MGF1_DIGEST, NULL, 0),
OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_MGF1_PROPERTIES, NULL, 0),
OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_PSS_SALTLEN, NULL, 0),
OSSL_PARAM_END};
static const OSSL_PARAM settable_ctx_params_no_digest[] = {
OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_PAD_MODE, NULL, 0),
OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_MGF1_DIGEST, NULL, 0),
OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_MGF1_PROPERTIES, NULL, 0),
OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_PSS_SALTLEN, NULL, 0),
OSSL_PARAM_END};
static const OSSL_PARAM known_gettable_ctx_params[] = {
OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_PAD_MODE, NULL, 0),
OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST, NULL, 0),
OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_MGF1_DIGEST, NULL, 0),
OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_PSS_SALTLEN, NULL, 0),
OSSL_PARAM_END
};
static int qat_rsa_private_encrypt(int flen, const unsigned char *from,
unsigned char *to, QAT_RSA *rsa,
int padding)
{
int ret = 0;
#ifdef ENABLE_QAT_HW_RSA
if (qat_hw_rsa_offload)
ret = qat_rsa_priv_enc(flen, from, to, rsa, padding);
#endif
#ifdef ENABLE_QAT_SW_RSA
if (qat_sw_rsa_offload)
ret = multibuff_rsa_priv_enc(flen, from, to, rsa, padding);
#endif
return ret;
}
static int qat_rsa_public_decrypt(int flen, const unsigned char *from,
unsigned char *to, QAT_RSA *rsa,
int padding)
{
int ret = 0;
#ifdef ENABLE_QAT_HW_RSA
if (qat_hw_rsa_offload)
ret = qat_rsa_pub_dec(flen, from, to, rsa, padding);
#endif
#ifdef ENABLE_QAT_SW_RSA
if (qat_sw_rsa_offload)
ret = multibuff_rsa_pub_dec(flen, from, to, rsa, padding);
#endif
return ret;
}
static size_t rsa_get_md_size(const QAT_PROV_RSA_CTX *prsactx)
{
if (prsactx->md != NULL)
return EVP_MD_size(prsactx->md);
return 0;
}
static int setup_tbuf(QAT_PROV_RSA_CTX *ctx)
{
if (ctx->tbuf != NULL)
return 1;
if ((ctx->tbuf = OPENSSL_malloc(QAT_RSA_size(ctx->rsa))) == NULL) {
QATerr(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
return 0;
}
return 1;
}
static void clean_tbuf(QAT_PROV_RSA_CTX *ctx)
{
if (ctx->tbuf != NULL)
OPENSSL_cleanse(ctx->tbuf, QAT_RSA_size(ctx->rsa));
}
static void free_tbuf(QAT_PROV_RSA_CTX *ctx)
{
clean_tbuf(ctx);
OPENSSL_free(ctx->tbuf);
ctx->tbuf = NULL;
}
static int encode_pkcs1(unsigned char **out, size_t *out_len, int type,
const unsigned char *m, size_t m_len)
{
size_t di_prefix_len, dig_info_len;
const unsigned char *di_prefix;
unsigned char *dig_info;
if (type == NID_undef) {
QATerr(ERR_LIB_RSA, RSA_R_UNKNOWN_ALGORITHM_TYPE);
return 0;
}
di_prefix = qat_rsa_digestinfo_encoding(type, &di_prefix_len);
if (di_prefix == NULL) {
QATerr(ERR_LIB_RSA,
RSA_R_THE_ASN1_OBJECT_IDENTIFIER_IS_NOT_KNOWN_FOR_THIS_MD);
return 0;
}
dig_info_len = di_prefix_len + m_len;
dig_info = OPENSSL_malloc(dig_info_len);
if (dig_info == NULL) {
QATerr(ERR_LIB_RSA, ERR_R_MALLOC_FAILURE);
return 0;
}
memcpy(dig_info, di_prefix, di_prefix_len);
memcpy(dig_info + di_prefix_len, m, m_len);
*out = dig_info;
*out_len = dig_info_len;
return 1;
}
#ifdef ENABLE_QAT_FIPS
/**
* @brief Checks if the given RSA key size is FIPS approved.
*
* This function checks if the key size (in bytes) is within the FIPS approved range.
* It sets the qat_fips_service_indicator as needed.
*
* @param rsasize RSA modulus size in bytes.
* @param is_sign 1 if used for signing, 0 if used for verification.
*
* @return 1 if key size is FIPS approved, 0 otherwise.
*/
static int qat_fips_check_rsa_key_size(size_t rsasize, int is_sign)
{
int key_size = rsasize * RSA_KEY_SIZE;
qat_fips_service_indicator = 0;
if (is_sign) {
if (key_size < FIPS_RSA_SIGN_MIN_SIZE || key_size > FIPS_RSA_MAX_SIZE) {
WARN("%d is FIPS non approved size\n", key_size);
return 0;
}
qat_fips_service_indicator = 1;
} else {
if (key_size != FIPS_RSA_VER_MIN_SIZE) {
if (key_size < FIPS_RSA_SIGN_MIN_SIZE || key_size > FIPS_RSA_MAX_SIZE) {
WARN("%d is FIPS non approved size\n", key_size);
return 0;
}
qat_fips_service_indicator = 1;
} else {
INFO("Given Key size %d is Allowed. But, FIPS non-approved size\n", FIPS_RSA_VER_MIN_SIZE);
}
}
return 1;
}
#endif
/**
* @brief Adds PKCS#1 PSS padding with MGF1 to a message hash for RSA signature.
*
* This function applies PKCS#1 PSS (Probabilistic Signature Scheme) padding using MGF1
* to the provided message hash, preparing it for RSA signature generation. It supports
* custom hash algorithms for both the main digest and the MGF1 mask generation function,
* and allows for variable salt lengths as required by the PSS scheme.
*
* @param rsa Pointer to the QAT_RSA key structure.
* @param EM Output buffer for the encoded message (should be at least RSA_size(rsa) bytes).
* @param mHash Input message hash to be padded.
* @param Hash Digest method for the main hash.
* @param mgf1Hash Digest method for the MGF1 mask generation function (if NULL, Hash is used).
* @param sLenOut Pointer to the salt length (input/output).
*
* @return 1 on success, 0 on failure.
*/
int QAT_RSA_padding_add_PKCS1_PSS_mgf1(QAT_RSA *rsa, unsigned char *EM,
const unsigned char *mHash,
const EVP_MD *Hash,
const EVP_MD *mgf1Hash,
int *sLenOut)
{
int i;
int ret = 0;
int hLen, maskedDBLen, MSBits, emLen;
int sLen = *sLenOut;
unsigned char *H, *salt = NULL, *p;
EVP_MD_CTX *ctx = NULL;
if (mgf1Hash == NULL)
mgf1Hash = Hash;
hLen = EVP_MD_get_size(Hash);
if (hLen < 0)
goto err;
/*-
* Negative sLen has special meanings:
* -1 sLen == hLen
* -2 salt length is maximized
* -3 same as above (on signing)
* -N reserved
*/
if (sLen == RSA_PSS_SALTLEN_DIGEST) {
sLen = hLen;
} else if (sLen == RSA_PSS_SALTLEN_MAX_SIGN) {
sLen = RSA_PSS_SALTLEN_MAX;
} else if (sLen < RSA_PSS_SALTLEN_MAX) {
QATerr(ERR_LIB_RSA, RSA_R_SLEN_CHECK_FAILED);
goto err;
}
MSBits = (BN_num_bits(rsa->n) - 1) & 0x7;
emLen = QAT_RSA_size(rsa);
if (MSBits == 0) {
*EM++ = 0;
emLen--;
}
if (emLen < hLen + 2) {
QATerr(ERR_LIB_RSA, RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
goto err;
}
if (sLen == RSA_PSS_SALTLEN_MAX) {
sLen = emLen - hLen - 2;
} else if (sLen > emLen - hLen - 2) {
QATerr(ERR_LIB_RSA, RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
goto err;
}
if (sLen > 0) {
salt = OPENSSL_malloc(sLen);
if (salt == NULL) {
QATerr(ERR_LIB_RSA, ERR_R_MALLOC_FAILURE);
goto err;
}
if (RAND_bytes_ex(rsa->libctx, salt, sLen, 0) <= 0)
goto err;
}
maskedDBLen = emLen - hLen - 1;
H = EM + maskedDBLen;
ctx = EVP_MD_CTX_new();
if (ctx == NULL)
goto err;
if (!EVP_DigestInit_ex(ctx, Hash, NULL)
|| !EVP_DigestUpdate(ctx, zeroes, sizeof(zeroes))
|| !EVP_DigestUpdate(ctx, mHash, hLen))
goto err;
if (sLen && !EVP_DigestUpdate(ctx, salt, sLen))
goto err;
if (!EVP_DigestFinal_ex(ctx, H, NULL))
goto err;
/* Generate dbMask in place then perform XOR on it */
if (QAT_PKCS1_MGF1(EM, maskedDBLen, H, hLen, mgf1Hash))
goto err;
p = EM;
/*
* Initial PS XORs with all zeroes which is a NOP so just update pointer.
* Note from a test above this value is guaranteed to be non-negative.
*/
p += emLen - sLen - hLen - 2;
*p++ ^= 0x1;
if (sLen > 0) {
for (i = 0; i < sLen; i++)
*p++ ^= salt[i];
}
if (MSBits)
EM[0] &= 0xFF >> (8 - MSBits);
/* H is already in place so just set final 0xbc */
EM[emLen - 1] = 0xbc;
ret = 1;
*sLenOut = sLen;
err:
EVP_MD_CTX_free(ctx);
OPENSSL_clear_free(salt, (size_t)sLen); /* salt != NULL implies sLen > 0 */
return ret;
}
/**
* @brief Signs a message digest using RSA and PKCS#1 v1.5 padding.
*
* This function creates an RSA signature for the given message digest using the specified
* digest type and the provided QAT_RSA key. It encodes the digest using the appropriate
* ASN.1 DigestInfo structure (unless the digest type is NID_md5_sha1), applies PKCS#1 v1.5
* padding, and performs the RSA private key operation.
*
* @param prsactx Pointer to the provider RSA context.
* @param type NID of the digest algorithm used (e.g., NID_sha256).
* @param m Pointer to the message digest to sign.
* @param m_len Length of the message digest.
* @param sigret Output buffer for the resulting signature.
* @param siglen Pointer to an unsigned int to receive the signature length.
* @param rsa Pointer to the QAT_RSA key structure.
*
* @return 1 on success, 0 on failure.
*/
int QAT_RSA_sign(void *prsactx, int type, const unsigned char *m, unsigned int m_len,
unsigned char *sigret, size_t *sigLen, size_t sigsize, unsigned int *siglen,
QAT_RSA *rsa)
{
int encrypt_len, ret = 0;
size_t encoded_len = 0;
unsigned char *tmps = NULL;
const unsigned char *encoded = NULL;
/* Compute the encoded digest. */
if (type == NID_md5_sha1) {
/*
* NID_md5_sha1 corresponds to the MD5/SHA1 combination in TLS 1.1 and
* earlier. It has no DigestInfo wrapper but otherwise is
* RSASSA-PKCS1-v1_5.
*/
if (m_len != SSL_SIG_LENGTH) {
QATerr(ERR_LIB_RSA, RSA_R_INVALID_MESSAGE_LENGTH);
return 0;
}
encoded_len = SSL_SIG_LENGTH;
encoded = m;
} else {
if (!encode_pkcs1(&tmps, &encoded_len, type, m, m_len))
goto err;
encoded = tmps;
}
if (encoded_len + RSA_PKCS1_PADDING_SIZE > (size_t)QAT_RSA_size(rsa)) {
QATerr(ERR_LIB_RSA, RSA_R_DIGEST_TOO_BIG_FOR_RSA_KEY);
goto err;
}
if (qat_hw_rsa_offload || qat_sw_rsa_offload) {
encrypt_len = qat_rsa_private_encrypt((int)encoded_len, encoded, sigret,
rsa, RSA_PKCS1_PADDING);
} else {
typedef int (*fun_ptr)(void *prsactx, unsigned char *sigret,
size_t *sigLen, size_t sigsize,
const unsigned char *m,
size_t m_len);
fun_ptr fun = get_default_rsa_signature().sign;
if (!fun)
goto err;
return fun(prsactx, sigret, sigLen, sigsize, m, m_len);
}
if (encrypt_len <= 0)
goto err;
*siglen = encrypt_len;
ret = 1;
err:
OPENSSL_clear_free(tmps, encoded_len);
return ret;
}
static int rsa_sign_directly(QAT_PROV_RSA_CTX *prsactx, unsigned char *sig,
size_t *siglen, size_t sigsize,
const unsigned char *tbs, size_t tbslen)
{
size_t rsasize = QAT_RSA_size(prsactx->rsa);
size_t mdsize = rsa_get_md_size(prsactx);
int ret = 0;
if (!qat_prov_is_running())
return 0;
if (sig == NULL) {
*siglen = rsasize;
return 1;
}
if (sigsize < rsasize) {
WARN("signature size is %zu, should be at least %zu", sigsize, rsasize);
QATerr(ERR_LIB_PROV, PROV_R_INVALID_SIGNATURE_SIZE);
return 0;
}
if (mdsize != 0) {
if (tbslen != mdsize) {
QATerr(ERR_LIB_PROV, PROV_R_INVALID_DIGEST_LENGTH);
return 0;
}
switch (prsactx->pad_mode) {
case RSA_X931_PADDING:
if ((size_t)QAT_RSA_size(prsactx->rsa) < tbslen + 1) {
WARN("RSA key size = %d, expected minimum = %zu",
QAT_RSA_size(prsactx->rsa), tbslen + 1);
QATerr(ERR_LIB_PROV, QAT_R_PROV_KEY_SIZE_TOO_SMALL);
return 0;
}
if (!setup_tbuf(prsactx)) {
QATerr(ERR_LIB_PROV, QAT_R_PROV_INVALID_CTX_LIB);
return 0;
}
memcpy(prsactx->tbuf, tbs, tbslen);
prsactx->tbuf[tbslen] = RSA_X931_hash_id(prsactx->mdnid);
if (qat_hw_rsa_offload || qat_sw_rsa_offload) {
ret = qat_rsa_private_encrypt(tbslen + 1, prsactx->tbuf,
sig, prsactx->rsa,
RSA_X931_PADDING);
} else {
typedef int (*fun_ptr)(void *prsactx, unsigned char *sig,
size_t *siglen, size_t sigsize,
const unsigned char *tbs,
size_t tbslen);
fun_ptr fun = get_default_rsa_signature().sign;
if (!fun)
return 0;
return fun(prsactx, sig, siglen, sigsize, tbs, tbslen);
}
clean_tbuf(prsactx);
break;
case RSA_PKCS1_PADDING:
{
unsigned int sltmp = 0;
ret = QAT_RSA_sign(prsactx, prsactx->mdnid, tbs, tbslen, sig,
siglen, sigsize, &sltmp,
prsactx->rsa);
if (ret <= 0) {
QATerr(ERR_LIB_PROV, ERR_R_RSA_LIB);
return 0;
}
ret = sltmp;
}
break;
case RSA_PKCS1_PSS_PADDING:
{
int saltlen = -1;
if (rsa_pss_restricted(prsactx)) {
switch (prsactx->saltlen) {
case RSA_PSS_SALTLEN_DIGEST:
if (prsactx->min_saltlen > EVP_MD_get_size(prsactx->md)) {
WARN("minimum salt length set to %d, but the digest only gives %d",
prsactx->min_saltlen, EVP_MD_get_size(prsactx->md));
QATerr(ERR_LIB_PROV, PROV_R_PSS_SALTLEN_TOO_SMALL);
return 0;
}
/* FALLTHRU */
default:
if ((prsactx->saltlen >= 0)
&& (prsactx->saltlen < prsactx->min_saltlen)) {
WARN("minimum salt length set to %d, but the actual salt length is only set to %d",
prsactx->min_saltlen, prsactx->saltlen);
QATerr(ERR_LIB_PROV, PROV_R_PSS_SALTLEN_TOO_SMALL);
return 0;
}
break;
}
}
if (!setup_tbuf(prsactx))
return 0;
saltlen = prsactx->saltlen;
if (!QAT_RSA_padding_add_PKCS1_PSS_mgf1(prsactx->rsa,
prsactx->tbuf, tbs,
prsactx->md,
prsactx->mgf1_md,
&saltlen)) {
QATerr(ERR_LIB_PROV, ERR_R_RSA_LIB);
return 0;
}
if (qat_hw_rsa_offload || qat_sw_rsa_offload) {
ret = qat_rsa_private_encrypt(RSA_size(prsactx->rsa), prsactx->tbuf,
sig, prsactx->rsa, RSA_NO_PADDING);
}
else {
typedef int (*fun_ptr)(void *prsactx, unsigned char *sig,
size_t *siglen, size_t sigsize,
const unsigned char *tbs,
size_t tbslen);
fun_ptr fun = get_default_rsa_signature().sign;
if (!fun)
return 0;
return fun(prsactx, sig, siglen, sigsize, tbs, tbslen);
}
clean_tbuf(prsactx);
}
break;
default:
WARN("Only X.931, PKCS#1 v1.5 or PSS padding allowed");
QATerr(ERR_LIB_PROV, PROV_R_INVALID_PADDING_MODE);
return 0;
}
} else {
if (qat_hw_rsa_offload || qat_sw_rsa_offload) {
ret = qat_rsa_private_encrypt(tbslen, tbs, sig, prsactx->rsa,
prsactx->pad_mode);
} else {
typedef int (*fun_ptr)(void *prsactx, unsigned char *sig,
size_t *siglen, size_t sigsize,
const unsigned char *tbs,
size_t tbslen);
fun_ptr fun = get_default_rsa_signature().sign;
if (!fun)
return 0;
return fun(prsactx, sig, siglen, sigsize, tbs, tbslen);
}
}
if (ret <= 0) {
QATerr(ERR_LIB_PROV, ERR_R_RSA_LIB);
return 0;
}
*siglen = ret;
return 1;
}
/**
* @brief Updates the message digest for RSA sign or verify operations.
*
* This function feeds additional data into the message digest context associated
* with the QAT_PROV_RSA_CTX during a multi-part (streaming) RSA sign or verify operation.
* It ensures that updates are allowed in the current context state and disables
* one-shot mode if called.
*
* @param vprsactx Pointer to the QAT_PROV_RSA_CTX context.
* @param data Pointer to the input data to update the digest with.
* @param datalen Length of the input data.
*
* @return 1 on success, 0 on failure.
*/
static int rsa_signverify_message_update(void *vprsactx,
const unsigned char *data,
size_t datalen)
{
QAT_PROV_RSA_CTX *prsactx = (QAT_PROV_RSA_CTX *)vprsactx;
if (prsactx == NULL || prsactx->mdctx == NULL)
return 0;
if (!prsactx->flag_allow_update) {
QATerr(ERR_LIB_PROV, QAT_R_PROV_UPDATE_CALL_OUT_OF_ORDER);
return 0;
}
prsactx->flag_allow_oneshot = 0;
return EVP_DigestUpdate(prsactx->mdctx, data, datalen);
}
/**
* @brief Finalizes a multi-part RSA sign operation and produces the signature.
*
* This function completes a streaming (multi-part) RSA sign operation by finalizing
* the message digest, performing any necessary padding, and generating the final
* RSA signature. It ensures the context is in a valid state for finalization and
* disables further updates or one-shot calls after completion.
*
* @param vprsactx Pointer to the QAT_PROV_RSA_CTX context.
* @param sig Output buffer for the resulting signature (may be NULL to query size).
* @param siglen Pointer to a size_t to receive the signature length.
* @param sigsize Size of the output buffer.
*
* @return 1 on success, 0 on failure.
*/
static int rsa_sign_message_final(void *vprsactx, unsigned char *sig,
size_t *siglen, size_t sigsize)
{
DEBUG("%s\n", __func__);
QAT_PROV_RSA_CTX *prsactx = (QAT_PROV_RSA_CTX *)vprsactx;
unsigned char digest[EVP_MAX_MD_SIZE];
unsigned int dlen = 0;
if (!qat_prov_is_running() || prsactx == NULL)
return 0;
if (prsactx->mdctx == NULL)
return 0;
if (!prsactx->flag_allow_final) {
QATerr(ERR_LIB_PROV, QAT_R_PROV_FINAL_CALL_OUT_OF_ORDER);
return 0;
}
if (sig != NULL) {
if (!EVP_DigestFinal_ex(prsactx->mdctx, digest, &dlen))
return 0;
prsactx->flag_allow_update = 0;
prsactx->flag_allow_oneshot = 0;
prsactx->flag_allow_final = 0;
}
return rsa_sign_directly(prsactx, sig, siglen, sigsize, digest, dlen);
}
QAT_RSA_PSS_PARAMS_30 *qat_rsa_get0_pss_params_30(QAT_RSA *r)
{
return &r->pss_params;
}
int qat_rsa_pss_params_30_is_unrestricted(const QAT_RSA_PSS_PARAMS_30 *rsa_pss_params)
{
static QAT_RSA_PSS_PARAMS_30 pss_params_cmp = { 0, };
return rsa_pss_params == NULL
|| memcmp(rsa_pss_params, &pss_params_cmp,
sizeof(*rsa_pss_params)) == 0;
}
int qat_rsa_pss_params_30_hashalg(const QAT_RSA_PSS_PARAMS_30 *rsa_pss_params)
{
if (rsa_pss_params == NULL)
return default_RSASSA_PSS_params.hash_algorithm_nid;
return rsa_pss_params->hash_algorithm_nid;
}
int qat_rsa_pss_params_30_maskgenhashalg(const QAT_RSA_PSS_PARAMS_30 *rsa_pss_params)
{
if (rsa_pss_params == NULL)
return default_RSASSA_PSS_params.hash_algorithm_nid;
return rsa_pss_params->mask_gen.hash_algorithm_nid;
}
int qat_rsa_pss_params_30_saltlen(const QAT_RSA_PSS_PARAMS_30 *rsa_pss_params)
{
if (rsa_pss_params == NULL)
return default_RSASSA_PSS_params.salt_len;
return rsa_pss_params->salt_len;
}
const char *nid2name(int meth, const OSSL_ITEM *items, size_t items_n)
{
size_t i;
for (i = 0; i < items_n; i++)
if (meth == (int)items[i].id)
return items[i].ptr;
return NULL;
}
const char *qat_rsa_oaeppss_nid2name(int md)
{
return nid2name(md, oaeppss_name_nid_map, OSSL_NELEM(oaeppss_name_nid_map));
}
static int qat_rsa_check_padding(const QAT_PROV_RSA_CTX *prsactx,
const char *mdname,
const char *mgf1_mdname,
int mdnid)
{
switch(prsactx->pad_mode) {
case RSA_NO_PADDING:
QATerr(ERR_LIB_PROV, PROV_R_INVALID_PADDING_MODE);
return 0;
case RSA_X931_PADDING:
if (RSA_X931_hash_id(mdnid) == -1) {
QATerr(ERR_LIB_PROV, PROV_R_INVALID_X931_DIGEST);
return 0;
}
break;
case RSA_PKCS1_PSS_PADDING:
if (rsa_pss_restricted(prsactx)) {
if ((mdname != NULL && !EVP_MD_is_a(prsactx->md, mdname))
|| (mgf1_mdname != NULL
&& !EVP_MD_is_a(prsactx->mgf1_md, mgf1_mdname))) {
QATerr(ERR_LIB_PROV, PROV_R_DIGEST_NOT_ALLOWED);
return 0;
}
}
break;
default:
break;
}
return 1;
}
int qat_digest_md_to_nid(const EVP_MD *md, const OSSL_ITEM *it, size_t it_len)
{
size_t i;
if (md == NULL)
return NID_undef;
for (i = 0; i < it_len; i++)
if (EVP_MD_is_a(md, it[i].ptr))
return (int)it[i].id;
return NID_undef;
}
int qat_digest_get_approved_nid(const EVP_MD *md)
{
static const OSSL_ITEM name_to_nid[] = {
{ NID_sha1, OSSL_DIGEST_NAME_SHA1 },
{ NID_sha224, OSSL_DIGEST_NAME_SHA2_224 },
{ NID_sha256, OSSL_DIGEST_NAME_SHA2_256 },
{ NID_sha384, OSSL_DIGEST_NAME_SHA2_384 },
{ NID_sha512, OSSL_DIGEST_NAME_SHA2_512 },
{ NID_sha512_224, OSSL_DIGEST_NAME_SHA2_512_224 },
{ NID_sha512_256, OSSL_DIGEST_NAME_SHA2_512_256 },
{ NID_sha3_224, OSSL_DIGEST_NAME_SHA3_224 },
{ NID_sha3_256, OSSL_DIGEST_NAME_SHA3_256 },
{ NID_sha3_384, OSSL_DIGEST_NAME_SHA3_384 },
{ NID_sha3_512, OSSL_DIGEST_NAME_SHA3_512 },
};
return qat_digest_md_to_nid(md, name_to_nid, OSSL_NELEM(name_to_nid));
}
int qat_digest_rsa_sign_get_md_nid(OSSL_LIB_CTX *ctx, const EVP_MD *md,
int sha1_allowed)
{
return qat_digest_get_approved_nid(md);
}
/**
* @brief Sets up the MGF1 digest method for RSA-PSS operations in the QAT provider context.
*
* This function fetches and assigns the specified MGF1 digest algorithm to the QAT_PROV_RSA_CTX
* structure. It validates the digest, checks compatibility with the current padding mode,
* and updates the context with the digest name and NID. If a previous MGF1 digest is set,
* it is freed before assignment.
*
* @param ctx Pointer to the QAT_PROV_RSA_CTX context.
* @param mdname Name of the MGF1 digest algorithm to fetch.
* @param mdprops Optional property query string for digest selection (may be NULL).
*
* @return 1 on success, 0 on failure.
*/
static int qat_rsa_setup_mgf1_md(QAT_PROV_RSA_CTX *ctx, const char *mdname,
const char *mdprops)
{
size_t len;
EVP_MD *md = NULL;
int mdnid;
if (mdprops == NULL)
mdprops = ctx->propq;
if ((md = EVP_MD_fetch(ctx->libctx, mdname, mdprops)) == NULL) {
WARN("%s could not be fetched", mdname);
QATerr(ERR_LIB_PROV, PROV_R_INVALID_DIGEST);
return 0;
}
/* The default for mgf1 is SHA1 - so allow SHA1 */
if ((mdnid = qat_digest_rsa_sign_get_md_nid(ctx->libctx, md, 1)) <= 0
|| !qat_rsa_check_padding(ctx, NULL, mdname, mdnid)) {
if (mdnid <= 0) {
WARN("digest=%s", mdname);
QATerr(ERR_LIB_PROV, PROV_R_DIGEST_NOT_ALLOWED);
}
EVP_MD_free(md);
return 0;
}
len = OPENSSL_strlcpy(ctx->mgf1_mdname, mdname, sizeof(ctx->mgf1_mdname));
if (len >= sizeof(ctx->mgf1_mdname)) {
WARN("%s exceeds name buffer length", mdname);
QATerr(ERR_LIB_PROV, PROV_R_INVALID_DIGEST);
EVP_MD_free(md);
return 0;
}
EVP_MD_free(ctx->mgf1_md);
ctx->mgf1_md = md;
ctx->mgf1_mdnid = mdnid;
ctx->mgf1_md_set = 1;
return 1;
}
/**
* @brief Sets up the main digest method for RSA operations in the QAT provider context.
*
* This function fetches and assigns the specified digest algorithm to the QAT_PROV_RSA_CTX
* structure. It validates the digest, checks compatibility with the current padding mode,
* and updates the context with the digest name and NID. If the MGF1 digest is not set,
* it also assigns the main digest as the MGF1 digest. The function manages memory for
* previous digest assignments and ensures the context is updated consistently.
*
* @param ctx Pointer to the QAT_PROV_RSA_CTX context.
* @param mdname Name of the digest algorithm to fetch.
* @param mdprops Optional property query string for digest selection (may be NULL).
*
* @return 1 on success, 0 on failure.
*/
static int qat_rsa_setup_md(QAT_PROV_RSA_CTX *ctx, const char *mdname,
const char *mdprops)
{
if (mdprops == NULL)
mdprops = ctx->propq;
if (mdname != NULL) {
EVP_MD *md = EVP_MD_fetch(ctx->libctx, mdname, mdprops);
int sha1_allowed = (ctx->operation != EVP_PKEY_OP_SIGN);
int md_nid = qat_digest_rsa_sign_get_md_nid(ctx->libctx, md,
sha1_allowed);
size_t mdname_len = strlen(mdname);
if (md == NULL
|| md_nid <= 0
|| !qat_rsa_check_padding(ctx, mdname, NULL, md_nid)
|| mdname_len >= sizeof(ctx->mdname)) {
if (md == NULL) {
WARN("%s could not be fetched", mdname);
QATerr(ERR_LIB_PROV, PROV_R_INVALID_DIGEST);
}
if (md_nid <= 0) {
WARN("digest=%s", mdname);
QATerr(ERR_LIB_PROV, PROV_R_DIGEST_NOT_ALLOWED);
}
if (mdname_len >= sizeof(ctx->mdname)) {
WARN("%s exceeds name buffer length", mdname);
QATerr(ERR_LIB_PROV, PROV_R_INVALID_DIGEST);
}
EVP_MD_free(md);
return 0;
}
if (!ctx->mgf1_md_set) {
if (!EVP_MD_up_ref(md)) {
EVP_MD_free(md);
return 0;
}
EVP_MD_free(ctx->mgf1_md);
ctx->mgf1_md = md;
ctx->mgf1_mdnid = md_nid;
OPENSSL_strlcpy(ctx->mgf1_mdname, mdname, sizeof(ctx->mgf1_mdname));
}
EVP_MD_CTX_free(ctx->mdctx);
EVP_MD_free(ctx->md);
ctx->mdctx = NULL;
ctx->md = md;
ctx->mdnid = md_nid;
OPENSSL_strlcpy(ctx->mdname, mdname, sizeof(ctx->mdname));
}
return 1;
}
static int qat_rsa_check_parameters(QAT_PROV_RSA_CTX *prsactx, int min_saltlen)
{
if (prsactx->pad_mode == RSA_PKCS1_PSS_PADDING) {
int max_saltlen;
/* See if minimum salt length exceeds maximum possible */
max_saltlen = QAT_RSA_size(prsactx->rsa) - EVP_MD_size(prsactx->md);
if ((QAT_RSA_bits(prsactx->rsa) & 0x7) == 1)
max_saltlen--;
if (min_saltlen < 0 || min_saltlen > max_saltlen) {
QATerr(ERR_LIB_PROV, PROV_R_INVALID_SALT_LENGTH);
return 0;
}
prsactx->min_saltlen = min_saltlen;
}
return 1;
}
/**
* @brief Generates a mask using the PKCS#1 MGF1 (Mask Generation Function 1) algorithm.
*
* This function implements the MGF1 algorithm as specified in PKCS#1, which is used
* in OAEP and PSS padding schemes for RSA. It generates a mask of the requested length
* by repeatedly hashing the seed concatenated with a 4-byte counter, using the specified
* digest algorithm.
*
* @param mask Output buffer for the generated mask.
* @param len Desired length of the mask in bytes.
* @param seed Input seed for the mask generation.
* @param seedlen Length of the input seed.
* @param dgst Digest algorithm to use for hashing (e.g., EVP_sha256()).
*
* @return 0 on success, -1 on failure.
*/
int QAT_PKCS1_MGF1(unsigned char *mask, long len, const unsigned char *seed,
long seedlen, const EVP_MD *dgst)
{
long i, outlen = 0;
unsigned char cnt[4];
EVP_MD_CTX *c = EVP_MD_CTX_new();
unsigned char md[EVP_MAX_MD_SIZE];
int mdlen;
int rv = -1;
if (c == NULL)
goto err;
mdlen = EVP_MD_get_size(dgst);
if (mdlen < 0)
goto err;
/* step 4 */
for (i = 0; outlen < len; i++) {
/* step 4a: D = I2BS(counter, 4) */
cnt[0] = (unsigned char)((i >> 24) & 255);
cnt[1] = (unsigned char)((i >> 16) & 255);
cnt[2] = (unsigned char)((i >> 8)) & 255;
cnt[3] = (unsigned char)(i & 255);
/* step 4b: T =T || hash(mgfSeed || D) */
if (!EVP_DigestInit_ex(c, dgst, NULL)
|| !EVP_DigestUpdate(c, seed, seedlen)
|| !EVP_DigestUpdate(c, cnt, 4))
goto err;
if (outlen + mdlen <= len) {
if (!EVP_DigestFinal_ex(c, mask + outlen, NULL))
goto err;
outlen += mdlen;
} else {
if (!EVP_DigestFinal_ex(c, md, NULL))
goto err;
memcpy(mask + outlen, md, len - outlen);
outlen = len;
}
}
rv = 0;
err:
OPENSSL_cleanse(md, sizeof(md));
EVP_MD_CTX_free(c);
return rv;
}
/**
* @brief Verifies an RSA-PSS signature using PKCS#1 PSS padding with MGF1.
*
* This function checks that the provided encoded message (EM) is a valid PKCS#1 PSS
* encoding of the given message hash (mHash), using the specified hash and MGF1
* algorithms and salt length. It supports automatic and explicit salt length handling,
* and performs all necessary decoding and comparison steps as described in PKCS#1.
*
* @param rsa Pointer to the QAT_RSA key structure.
* @param mHash Input message hash to verify.
* @param Hash Digest method for the main hash.
* @param mgf1Hash Digest method for the MGF1 mask generation function (if NULL, Hash is used).
* @param EM Encoded message (signature to verify).
* @param sLenout Pointer to the salt length (input/output).
*
* @return 1 on successful verification, 0 on failure.
*/
int QAT_RSA_verify_PKCS1_PSS_mgf1(QAT_RSA *rsa, const unsigned char *mHash,
const EVP_MD *Hash, const EVP_MD *mgf1Hash,
const unsigned char *EM, int *sLenout)
{
int i;
int ret = 0;
int hLen, maskedDBLen, MSBits, emLen;
const unsigned char *H;
int sLen = *sLenout;
unsigned char *DB = NULL;
EVP_MD_CTX *ctx = EVP_MD_CTX_new();
unsigned char H_[EVP_MAX_MD_SIZE];
if (ctx == NULL)
goto err;
if (mgf1Hash == NULL)
mgf1Hash = Hash;
hLen = EVP_MD_get_size(Hash);
if (hLen < 0)
goto err;
/*-
* Negative sLen has special meanings:
* -1 sLen == hLen
* -2 salt length is autorecovered from signature
* -3 salt length is maximized
* -N reserved
*/
if (sLen == RSA_PSS_SALTLEN_DIGEST) {
sLen = hLen;
} else if (sLen < RSA_PSS_SALTLEN_MAX) {
QATerr(ERR_LIB_RSA, RSA_R_SLEN_CHECK_FAILED);
goto err;
}
MSBits = (BN_num_bits(rsa->n) - 1) & 0x7;
emLen = QAT_RSA_size(rsa);
if (EM[0] & (0xFF << MSBits)) {
QATerr(ERR_LIB_RSA, RSA_R_FIRST_OCTET_INVALID);
goto err;
}
if (MSBits == 0) {
EM++;
emLen--;
}
if (emLen < hLen + 2) {
QATerr(ERR_LIB_RSA, RSA_R_DATA_TOO_LARGE);
goto err;
}
if (sLen == RSA_PSS_SALTLEN_MAX) {
sLen = emLen - hLen - 2;
} else if (sLen > emLen - hLen - 2) { /* sLen can be small negative */
QATerr(ERR_LIB_RSA, RSA_R_DATA_TOO_LARGE);
goto err;
}
if (EM[emLen - 1] != 0xbc) {
QATerr(ERR_LIB_RSA, RSA_R_LAST_OCTET_INVALID);
goto err;
}
maskedDBLen = emLen - hLen - 1;
H = EM + maskedDBLen;
DB = OPENSSL_malloc(maskedDBLen);
if (DB == NULL) {
QATerr(ERR_LIB_RSA, ERR_R_MALLOC_FAILURE);
goto err;
}
if (QAT_PKCS1_MGF1(DB, maskedDBLen, H, hLen, mgf1Hash) < 0)
goto err;
for (i = 0; i < maskedDBLen; i++)
DB[i] ^= EM[i];
if (MSBits)
DB[0] &= 0xFF >> (8 - MSBits);
for (i = 0; DB[i] == 0 && i < (maskedDBLen - 1); i++) ;
if (DB[i++] != 0x1) {
QATerr(ERR_LIB_RSA, RSA_R_SLEN_RECOVERY_FAILED);
goto err;
}
if (sLen != RSA_PSS_SALTLEN_AUTO && (maskedDBLen - i) != sLen) {
WARN("expected: %d retrieved: %d", sLen, maskedDBLen - i);
QATerr(ERR_LIB_RSA, RSA_R_SLEN_CHECK_FAILED);
goto err;
}
if (!EVP_DigestInit_ex(ctx, Hash, NULL)
|| !EVP_DigestUpdate(ctx, zeroes, sizeof(zeroes))
|| !EVP_DigestUpdate(ctx, mHash, hLen))
goto err;
if (maskedDBLen - i) {
if (!EVP_DigestUpdate(ctx, DB + i, maskedDBLen - i))
goto err;
}
if (!EVP_DigestFinal_ex(ctx, H_, NULL))
goto err;
if (memcmp(H_, H, hLen)) {
QATerr(ERR_LIB_RSA, RSA_R_BAD_SIGNATURE);
ret = 0;
} else {
ret = 1;
}
*sLenout = sLen;
err:
OPENSSL_free(DB);
EVP_MD_CTX_free(ctx);
return ret;
}
static int digest_sz_from_nid(int nid)
{
switch (nid) {
MD_NID_CASE(sha1, SHA_DIGEST_LENGTH)
MD_NID_CASE(sha224, SHA224_DIGEST_LENGTH)
MD_NID_CASE(sha256, SHA256_DIGEST_LENGTH)
MD_NID_CASE(sha384, SHA384_DIGEST_LENGTH)
MD_NID_CASE(sha512, SHA512_DIGEST_LENGTH)
MD_NID_CASE(sha512_224, SHA224_DIGEST_LENGTH)
MD_NID_CASE(sha512_256, SHA256_DIGEST_LENGTH)
MD_NID_CASE(sha3_224, SHA224_DIGEST_LENGTH)
MD_NID_CASE(sha3_256, SHA256_DIGEST_LENGTH)
MD_NID_CASE(sha3_384, SHA384_DIGEST_LENGTH)
MD_NID_CASE(sha3_512, SHA512_DIGEST_LENGTH)
default:
return 0;
}
}
/**
* @brief Verifies an RSA signature using PKCS#1 v1.5 padding.
*
* This function verifies an RSA signature by decrypting the signature using the public key,
* reconstructing the expected encoded digest, and comparing it to the decrypted value.
* It supports special cases for MD5/SHA1 (TLS 1.1 and earlier) and MDC2 digests, as well as
* generic digest types. If the signature is valid, the function can optionally recover the
* original digest value.
*
* @param prsactx Pointer to the provider RSA context.
* @param type NID of the digest algorithm used (e.g., NID_sha256).
* @param m Pointer to the message digest to verify against.
* @param m_len Length of the message digest.
* @param rm Output buffer for the recovered digest (may be NULL if not needed).
* @param prm_len Pointer to a size_t to receive the length of the recovered digest (may be NULL).
* @param sigbuf Input buffer containing the signature to verify.
* @param siglen Length of the signature buffer.
* @param rsa Pointer to the QAT_RSA key structure.
*
* @return 1 on successful verification, 0 on failure.
*/
int QAT_RSA_verify(void *prsactx, int type, const unsigned char *m,
unsigned int m_len, unsigned char *rm,
size_t *prm_len,
const unsigned char *sigbuf,
size_t siglen, QAT_RSA *rsa)
{
DEBUG("%s\n", __func__);
int len, ret = 0;
size_t decrypt_len, encoded_len = 0;
unsigned char *decrypt_buf = NULL, *encoded = NULL;
if (siglen != (size_t)QAT_RSA_size(rsa)) {
QATerr(ERR_LIB_RSA, RSA_R_WRONG_SIGNATURE_LENGTH);
return 0;
}
/* Recover the encoded digest. */
decrypt_buf = OPENSSL_malloc(siglen);
if (decrypt_buf == NULL) {
QATerr(ERR_LIB_RSA, ERR_R_MALLOC_FAILURE);
goto err;
}
if (qat_hw_rsa_offload || qat_sw_rsa_offload) {
len = qat_rsa_public_decrypt((int)siglen, sigbuf, decrypt_buf, rsa,
RSA_PKCS1_PADDING);
}
else {
typedef int (*fun_ptr)(void *prsactx, const unsigned char *sigbuf,
size_t siglen, const unsigned char *m,
size_t m_len);
fun_ptr fun = get_default_rsa_signature().verify;
if (!fun)
return 0;
return fun(prsactx, sigbuf, siglen, m, m_len);
}
if (len <= 0)
goto err;
decrypt_len = len;
if (type == NID_md5_sha1) {
/*
* NID_md5_sha1 corresponds to the MD5/SHA1 combination in TLS 1.1 and
* earlier. It has no DigestInfo wrapper but otherwise is
* RSASSA-PKCS1-v1_5.
*/
if (decrypt_len != SSL_SIG_LENGTH) {
QATerr(ERR_LIB_RSA, RSA_R_BAD_SIGNATURE);
goto err;
}
if (rm != NULL) {
memcpy(rm, decrypt_buf, SSL_SIG_LENGTH);
*prm_len = SSL_SIG_LENGTH;
} else {
if (m_len != SSL_SIG_LENGTH) {
QATerr(ERR_LIB_RSA, RSA_R_INVALID_MESSAGE_LENGTH);
goto err;
}
if (memcmp(decrypt_buf, m, SSL_SIG_LENGTH) != 0) {
QATerr(ERR_LIB_RSA, RSA_R_BAD_SIGNATURE);
goto err;
}
}
} else if (type == NID_mdc2 && decrypt_len == 2 + 16
&& decrypt_buf[0] == 0x04 && decrypt_buf[1] == 0x10) {
/*
* Oddball MDC2 case: signature can be OCTET STRING. check for correct
* tag and length octets.
*/
if (rm != NULL) {
memcpy(rm, decrypt_buf + 2, 16);
*prm_len = 16;
} else {
if (m_len != 16) {
QATerr(ERR_LIB_RSA, RSA_R_INVALID_MESSAGE_LENGTH);
goto err;
}
if (memcmp(m, decrypt_buf + 2, 16) != 0) {
QATerr(ERR_LIB_RSA, RSA_R_BAD_SIGNATURE);
goto err;
}
}
} else
{
/*
* If recovering the digest, extract a digest-sized output from the end
* of |decrypt_buf| for |encode_pkcs1|, then compare the decryption
* output as in a standard verification.
*/
if (rm != NULL) {
len = digest_sz_from_nid(type);
if (len <= 0)
goto err;
m_len = (unsigned int)len;
if (m_len > decrypt_len) {
QATerr(ERR_LIB_RSA, RSA_R_INVALID_DIGEST_LENGTH);
goto err;
}
m = decrypt_buf + decrypt_len - m_len;
}
/* Construct the encoded digest and ensure it matches. */
if (!encode_pkcs1(&encoded, &encoded_len, type, m, m_len))
goto err;
if (encoded_len != decrypt_len
|| memcmp(encoded, decrypt_buf, encoded_len) != 0) {
QATerr(ERR_LIB_RSA, RSA_R_BAD_SIGNATURE);
goto err;
}
/* Output the recovered digest. */
if (rm != NULL) {
memcpy(rm, m, m_len);
*prm_len = m_len;
}
}
ret = 1;
err:
OPENSSL_clear_free(encoded, encoded_len);
OPENSSL_clear_free(decrypt_buf, siglen);
return ret;
}
static int ossl_param_is_empty(const OSSL_PARAM params[])
{
return params == NULL || params->key == NULL;
}
/**
* @brief Sets context parameters for the QAT RSA signature operation.
*
* This function updates the QAT_PROV_RSA_CTX context with new settings provided in the
* OSSL_PARAM array, such as digest algorithm, padding mode, PSS salt length, and MGF1
* digest. It validates the parameters, applies restrictions based on the current context
* and key type, and ensures the context is consistent for subsequent sign or verify
* operations.
*
* @param vprsactx Pointer to the QAT_PROV_RSA_CTX context.
* @param params Array of OSSL_PARAM containing the parameters to set.
*
* @return 1 on success, 0 on failure.
*/
static int qat_signature_rsa_set_ctx_params(void *vprsactx,
const OSSL_PARAM params[])
{
DEBUG("%s\n", __func__);
QAT_PROV_RSA_CTX *prsactx = (QAT_PROV_RSA_CTX *)vprsactx;
const OSSL_PARAM *p;
int pad_mode;
int saltlen;
char mdname[50] = "", *pmdname = NULL;
char mdprops[256] = "", *pmdprops = NULL;
char mgf1mdname[50] = "", *pmgf1mdname = NULL;
char mgf1mdprops[256] = "", *pmgf1mdprops = NULL;
if (prsactx == NULL)
return 0;
if (ossl_param_is_empty(params))
return 1;
pad_mode = prsactx->pad_mode;
saltlen = prsactx->saltlen;
p = OSSL_PARAM_locate_const(params, OSSL_SIGNATURE_PARAM_DIGEST);
/* Not allowed during certain operations */
if (p != NULL && !prsactx->flag_allow_md)
return 0;
if (p != NULL) {
const OSSL_PARAM *propsp =
OSSL_PARAM_locate_const(params,
OSSL_SIGNATURE_PARAM_PROPERTIES);
pmdname = mdname;
if (!OSSL_PARAM_get_utf8_string(p, &pmdname, sizeof(mdname)))
return 0;
if (propsp != NULL) {
pmdprops = mdprops;
if (!OSSL_PARAM_get_utf8_string(propsp,
&pmdprops, sizeof(mdprops)))
return 0;
}
}
p = OSSL_PARAM_locate_const(params, OSSL_SIGNATURE_PARAM_PAD_MODE);
if (p != NULL) {
const char *err_extra_text = NULL;
switch (p->data_type) {
case OSSL_PARAM_INTEGER: /* Support for legacy pad mode number */
if (!OSSL_PARAM_get_int(p, &pad_mode))
return 0;
break;
case OSSL_PARAM_UTF8_STRING:
{
int i;
if (p->data == NULL)
return 0;
for (i = 0; padding_item[i].id != 0; i++) {
if (strcmp(p->data, padding_item[i].ptr) == 0) {
pad_mode = padding_item[i].id;
break;
}
}
}
break;
default:
return 0;
}
switch (pad_mode) {
case RSA_PKCS1_OAEP_PADDING:
/*
* OAEP padding is for asymmetric cipher only so is not compatible
* with signature use.
*/
err_extra_text = "OAEP padding not allowed for signing / verifying";
goto bad_pad;
case RSA_PKCS1_PSS_PADDING:
#if OPENSSL_VERSION_NUMBER >= 0x30400000
if ((prsactx->operation
& (EVP_PKEY_OP_SIGN | EVP_PKEY_OP_SIGNMSG
| EVP_PKEY_OP_VERIFY | EVP_PKEY_OP_VERIFYMSG)) == 0) {
err_extra_text =
"PSS padding only allowed for sign and verify operations";
goto bad_pad;
}
#else
if ((prsactx->operation
& (EVP_PKEY_OP_SIGN | EVP_PKEY_OP_VERIFY)) == 0) {
err_extra_text =
"PSS padding only allowed for sign and verify operations";
goto bad_pad;
}
#endif
break;
case RSA_PKCS1_PADDING:
err_extra_text = "PKCS#1 padding not allowed with RSA-PSS";
goto cont;
case RSA_NO_PADDING:
err_extra_text = "No padding not allowed with RSA-PSS";
goto cont;
case RSA_X931_PADDING:
err_extra_text = "X.931 padding not allowed with RSA-PSS";
cont:
if (QAT_RSA_test_flags(prsactx->rsa,
RSA_FLAG_TYPE_MASK) == RSA_FLAG_TYPE_RSA)
break;
/* FALLTHRU */
default:
bad_pad:
if (err_extra_text == NULL) {
QATerr(ERR_LIB_PROV,
PROV_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE);
} else {
WARN("%s", err_extra_text);
QATerr(ERR_LIB_PROV,
PROV_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE);
}
return 0;
}
}
p = OSSL_PARAM_locate_const(params, OSSL_SIGNATURE_PARAM_PSS_SALTLEN);
if (p != NULL) {
if (pad_mode != RSA_PKCS1_PSS_PADDING) {
WARN("PSS saltlen can only be specified if PSS padding has been specified first");
QATerr(ERR_LIB_PROV, PROV_R_NOT_SUPPORTED);
return 0;
}
switch (p->data_type) {
case OSSL_PARAM_INTEGER: /* Support for legacy pad mode number */
if (!OSSL_PARAM_get_int(p, &saltlen))
return 0;
break;
case OSSL_PARAM_UTF8_STRING:
if (strcmp(p->data, OSSL_PKEY_RSA_PSS_SALT_LEN_DIGEST) == 0)
saltlen = RSA_PSS_SALTLEN_DIGEST;
else if (strcmp(p->data, OSSL_PKEY_RSA_PSS_SALT_LEN_MAX) == 0)
saltlen = RSA_PSS_SALTLEN_MAX;
else if (strcmp(p->data, OSSL_PKEY_RSA_PSS_SALT_LEN_AUTO) == 0)
saltlen = RSA_PSS_SALTLEN_AUTO;
else
saltlen = atoi(p->data);
break;
default:
return 0;
}
/*
* RSA_PSS_SALTLEN_MAX seems curiously named in this check.
* Contrary to what it's name suggests, it's the currently
* lowest saltlen number possible.
*/
if (saltlen < RSA_PSS_SALTLEN_MAX) {
QATerr(ERR_LIB_PROV, PROV_R_INVALID_SALT_LENGTH);
return 0;
}
if (rsa_pss_restricted(prsactx)) {
switch (saltlen) {
case RSA_PSS_SALTLEN_AUTO:
case RSA_PSS_SALTLEN_AUTO_DIGEST_MAX:
if (prsactx->operation == EVP_PKEY_OP_VERIFY) {
WARN("Cannot use autodetected salt length");
QATerr(ERR_LIB_PROV, PROV_R_INVALID_SALT_LENGTH);
return 0;
}
#if OPENSSL_VERSION_NUMBER >= 0x30400000
if (prsactx->operation == EVP_PKEY_OP_VERIFYMSG) {
WARN("Cannot use autodetected salt length");
QATerr(ERR_LIB_PROV, PROV_R_INVALID_SALT_LENGTH);
return 0;
}
#endif
break;
case RSA_PSS_SALTLEN_DIGEST:
if (prsactx->min_saltlen > EVP_MD_size(prsactx->md)) {
WARN("minimum salt length set to %d, but the digest only gives %d",
prsactx->min_saltlen, EVP_MD_get_size(prsactx->md));
QATerr(ERR_LIB_PROV, PROV_R_PSS_SALTLEN_TOO_SMALL);
return 0;
}
break;
default:
if ((saltlen >= 0) && (saltlen < prsactx->min_saltlen)) {
WARN("minimum salt length set to %d, but the actual salt length is only set to %d",
prsactx->min_saltlen, prsactx->saltlen);
QATerr(ERR_LIB_PROV, PROV_R_PSS_SALTLEN_TOO_SMALL);
return 0;
}
}
}
}
p = OSSL_PARAM_locate_const(params, OSSL_SIGNATURE_PARAM_MGF1_DIGEST);
if (p != NULL) {
const OSSL_PARAM *propsp =
OSSL_PARAM_locate_const(params,
OSSL_SIGNATURE_PARAM_MGF1_PROPERTIES);
pmgf1mdname = mgf1mdname;
if (!OSSL_PARAM_get_utf8_string(p, &pmgf1mdname, sizeof(mgf1mdname)))
return 0;
if (propsp != NULL) {
pmgf1mdprops = mgf1mdprops;
if (!OSSL_PARAM_get_utf8_string(propsp,
&pmgf1mdprops, sizeof(mgf1mdprops)))
return 0;
}
if (pad_mode != RSA_PKCS1_PSS_PADDING) {
QATerr(ERR_LIB_PROV, PROV_R_INVALID_MGF1_MD);
return 0;
}
}
prsactx->saltlen = saltlen;
prsactx->pad_mode = pad_mode;
if (prsactx->md == NULL && pmdname == NULL
&& pad_mode == RSA_PKCS1_PSS_PADDING)
pmdname = RSA_DEFAULT_DIGEST_NAME;
if (pmgf1mdname != NULL
&& !qat_rsa_setup_mgf1_md(prsactx, pmgf1mdname, pmgf1mdprops))
return 0;
if (pmdname != NULL) {
if (!qat_rsa_setup_md(prsactx, pmdname, pmdprops))
return 0;
} else {
if (!qat_rsa_check_padding(prsactx, NULL, NULL, prsactx->mdnid))
return 0;
}
return 1;
}
/**
* @brief Allocates and initializes a new QAT RSA signature context.
*
* This function creates a new QAT_PROV_RSA_CTX structure for RSA signature operations,
* initializes its fields, and copies the provided property query string if present.
* It sets default values for salt length and minimum salt length, and associates
* the context with the appropriate OpenSSL library context.
*
* @param provctx Pointer to the provider context.
* @param propq Optional property query string (may be NULL).
*
* @return Pointer to the newly allocated QAT_PROV_RSA_CTX, or NULL on failure.
*/
static void *qat_signature_rsa_newctx(void *provctx, const char *propq)
{
DEBUG("%s\n", __func__);
QAT_PROV_RSA_CTX *prsactx = NULL;
char *propq_copy = NULL;
if ((prsactx = OPENSSL_zalloc(sizeof(QAT_PROV_RSA_CTX))) == NULL
|| (propq != NULL && (propq_copy = OPENSSL_strdup(propq)) == NULL)) {
OPENSSL_free(prsactx);
QATerr(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
return NULL;
}
prsactx->libctx = prov_libctx_of(provctx);
prsactx->flag_allow_md = 1;
prsactx->propq = propq_copy;
prsactx->saltlen = RSA_PSS_SALTLEN_AUTO_DIGEST_MAX;
prsactx->min_saltlen = -1;
return prsactx;
}
/**
* @brief Frees and cleans up a QAT RSA signature context.
*
* This function releases all resources associated with a QAT_PROV_RSA_CTX structure,
* including digest contexts, digest algorithms, property query strings, temporary buffers,
* and the associated RSA key. It securely clears sensitive memory before freeing.
*
* @param vprsactx Pointer to the QAT_PROV_RSA_CTX context to free.
*/
static void qat_signature_rsa_freectx(void *vprsactx)
{
DEBUG("%s\n", __func__);
QAT_PROV_RSA_CTX *prsactx = (QAT_PROV_RSA_CTX *)vprsactx;
if (prsactx == NULL)
return;
EVP_MD_CTX_free(prsactx->mdctx);
EVP_MD_free(prsactx->md);
EVP_MD_free(prsactx->mgf1_md);
OPENSSL_free(prsactx->propq);
free_tbuf(prsactx);
QAT_RSA_free((RSA*)prsactx->rsa);
OPENSSL_clear_free(prsactx, sizeof(*prsactx));
return;
}
/**
* @brief Recovers the original message from an RSA signature using the QAT provider context.
*
* This function attempts to recover the original message or digest from the provided RSA signature,
* according to the current padding mode and digest settings in the QAT_PROV_RSA_CTX context.
* It supports PKCS#1 v1.5 and X9.31 padding modes, and handles output buffer sizing and validation.
*
* @param vprsactx Pointer to the QAT_PROV_RSA_CTX context.
* @param rout Output buffer for the recovered message (may be NULL to query size).
* @param routlen Pointer to a size_t to receive the length of the recovered message.
* @param routsize Size of the output buffer.
* @param sig Input buffer containing the signature.
* @param siglen Length of the signature buffer.
*
* @return 1 on success, 0 on failure.
*/
static int qat_signature_rsa_verify_recover(void *vprsactx,
unsigned char *rout,
size_t *routlen,
size_t routsize,
const unsigned char *sig,
size_t siglen)
{
DEBUG("%s\n", __func__);
QAT_PROV_RSA_CTX *prsactx = (QAT_PROV_RSA_CTX *)vprsactx;
int ret;
if (!qat_prov_is_running())
return 0;
if (rout == NULL) {
*routlen = QAT_RSA_size(prsactx->rsa);
return 1;
}
if (prsactx->md != NULL) {
switch (prsactx->pad_mode) {
case RSA_X931_PADDING:
if (!setup_tbuf(prsactx))
return 0;
if (qat_hw_rsa_offload || qat_sw_rsa_offload) {
ret = qat_rsa_public_decrypt(siglen, sig, prsactx->tbuf,
prsactx->rsa,
RSA_X931_PADDING);
} else {
typedef int (*fun_ptr)(void *prsactx, const unsigned char *sig,
size_t siglen, const unsigned char *tbs,
size_t tbslen);
fun_ptr fun = get_default_rsa_signature().verify;
if (!fun)
return 0;
return fun(prsactx, sig, siglen, NULL, 0);
}
if (ret < 1) {
QATerr(ERR_LIB_PROV, ERR_R_RSA_LIB);
return 0;
}
ret--;
if (prsactx->tbuf[ret] != RSA_X931_hash_id(prsactx->mdnid)) {
QATerr(ERR_LIB_PROV, PROV_R_ALGORITHM_MISMATCH);
return 0;
}
if (ret != EVP_MD_size(prsactx->md)) {
WARN("Should be %d, but got %d", EVP_MD_size(prsactx->md), ret);
QATerr(ERR_LIB_PROV, PROV_R_INVALID_DIGEST_LENGTH);
return 0;
}
*routlen = ret;
if (rout != prsactx->tbuf) {
if (routsize < (size_t)ret) {
WARN("buffer size is %zu, should be %d", routsize, ret);
QATerr(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
return 0;
}
memcpy(rout, prsactx->tbuf, ret);
}
break;
case RSA_PKCS1_PADDING:
{
size_t sltmp;
ret = QAT_RSA_verify(prsactx, prsactx->mdnid, NULL, 0, rout, &sltmp,
sig, siglen, prsactx->rsa);
if (ret <= 0) {
QATerr(ERR_LIB_PROV, ERR_R_RSA_LIB);
return 0;
}
ret = sltmp;
}
break;
default:
WARN("Only X.931 or PKCS#1 v1.5 padding allowed");
QATerr(ERR_LIB_PROV, PROV_R_INVALID_PADDING_MODE);
return 0;
}
} else {
if (qat_hw_rsa_offload || qat_sw_rsa_offload) {
ret = qat_rsa_public_decrypt(siglen, sig, rout, prsactx->rsa,
prsactx->pad_mode);
} else {
typedef int (*fun_ptr)(void *prsactx, const unsigned char *sig,
size_t siglen, const unsigned char *tbs,
size_t tbslen);
fun_ptr fun = get_default_rsa_signature().verify;
if (!fun)
return 0;
return fun(prsactx, sig, siglen, NULL, 0);
}
if (ret < 0) {
QATerr(ERR_LIB_PROV, ERR_R_RSA_LIB);
return 0;
}
}
*routlen = ret;
return 1;
}
static int rsa_verify_directly(QAT_PROV_RSA_CTX *prsactx,
const unsigned char *sig, size_t siglen,
const unsigned char *tbs, size_t tbslen)
{
size_t rslen;
int ret = 0;
if (!qat_prov_is_running())
goto end;
if (prsactx->md != NULL) {
switch (prsactx->pad_mode) {
case RSA_PKCS1_PADDING:
if (!QAT_RSA_verify(prsactx, prsactx->mdnid, tbs, tbslen, NULL, NULL,
sig, siglen, prsactx->rsa)) {
QATerr(ERR_LIB_PROV, ERR_R_RSA_LIB);
goto end;
}
ret = 1;
goto end;
case RSA_X931_PADDING:
if (!setup_tbuf(prsactx))
return 0;
if (qat_signature_rsa_verify_recover(prsactx, prsactx->tbuf, &rslen, 0,
sig, siglen) <= 0)
goto end;
break;
case RSA_PKCS1_PSS_PADDING:
{
int ret = 0;
int saltlen = -1;
size_t mdsize;
mdsize = rsa_get_md_size(prsactx);
if (tbslen != mdsize) {
WARN("Should be %zu, but got %zu", mdsize, tbslen);
QATerr(ERR_LIB_PROV, PROV_R_INVALID_DIGEST_LENGTH);
goto end;
}
if (!setup_tbuf(prsactx))
goto end;
if (qat_hw_rsa_offload || qat_sw_rsa_offload) {
ret = qat_rsa_public_decrypt(siglen, sig, prsactx->tbuf,
prsactx->rsa, RSA_NO_PADDING);
} else {
typedef int (*fun_ptr)(void *prsactx, const unsigned char *sig,
size_t siglen, const unsigned char *tbs,
size_t tbslen);
fun_ptr fun = get_default_rsa_signature().verify;
if (!fun)
return 0;
return fun(prsactx, sig, siglen, tbs, tbslen);
}
if (ret <= 0) {
QATerr(ERR_LIB_PROV, ERR_R_RSA_LIB);
goto end;
}
saltlen = prsactx->saltlen;
ret = QAT_RSA_verify_PKCS1_PSS_mgf1(prsactx->rsa, tbs,
prsactx->md, prsactx->mgf1_md,
prsactx->tbuf, &saltlen);
if (ret <= 0) {
QATerr(ERR_LIB_PROV, ERR_R_RSA_LIB);
goto end;
}
return 1;
}
default:
WARN("Only X.931, PKCS#1 v1.5 or PSS padding allowed");
QATerr(ERR_LIB_PROV, PROV_R_INVALID_PADDING_MODE);
goto end;
}
} else {
int verify_ret = 0;
if (!setup_tbuf(prsactx))
goto end;
if (qat_hw_rsa_offload || qat_sw_rsa_offload) {
verify_ret = qat_rsa_public_decrypt(siglen, sig, prsactx->tbuf,
prsactx->rsa,
prsactx->pad_mode);
if (verify_ret <= 0) {
QATerr(ERR_LIB_PROV, ERR_R_RSA_LIB);
goto end;
}
} else {
typedef int (*fun_ptr)(void *prsactx, const unsigned char *sig,
size_t siglen, const unsigned char *tbs,
size_t tbslen);
fun_ptr fun = get_default_rsa_signature().verify;
if (!fun)
return 0;
return fun(prsactx, sig, siglen, tbs, tbslen);
}
rslen = (size_t)verify_ret;
}
if ((rslen != tbslen) || memcmp(tbs, prsactx->tbuf, rslen))
goto end;
ret = 1;
end:
return ret;
}
#if OPENSSL_VERSION_NUMBER >= 0x30400000
static int rsa_verify_set_sig(void *vprsactx, const unsigned char *sig,
size_t siglen)
{
QAT_PROV_RSA_CTX *prsactx = (QAT_PROV_RSA_CTX *)vprsactx;
const OSSL_PARAM *p;
OSSL_PARAM params[2];
params[0] =
OSSL_PARAM_construct_octet_string(OSSL_SIGNATURE_PARAM_SIGNATURE,
(unsigned char *)sig, siglen);
params[1] = OSSL_PARAM_construct_end();
if (prsactx->operation == EVP_PKEY_OP_VERIFYMSG) {
p = OSSL_PARAM_locate_const(params, OSSL_SIGNATURE_PARAM_SIGNATURE);
if (p != NULL) {
OPENSSL_free(prsactx->sig);
prsactx->sig = NULL;
prsactx->siglen = 0;
if (!OSSL_PARAM_get_octet_string(p, (void **)&prsactx->sig,
0, &prsactx->siglen)) {
return 0;
}
}
}
return 1;
}
/**
* @brief Finalizes a multi-part RSA verify operation and checks the signature.
*
* This function completes a streaming (multi-part) RSA verify operation by finalizing
* the message digest, performing any necessary padding checks, and verifying the provided
* signature against the computed digest. It ensures the context is in a valid state for
* finalization and disables further updates or one-shot calls after completion.
*
* @param vprsactx Pointer to the QAT_PROV_RSA_CTX context.
*
* @return 1 on successful verification, 0 on failure.
*/
static int rsa_verify_message_final(void *vprsactx)
{
DEBUG("%s\n", __func__);
QAT_PROV_RSA_CTX *prsactx = (QAT_PROV_RSA_CTX *)vprsactx;
unsigned char digest[EVP_MAX_MD_SIZE];
unsigned int dlen = 0;
if (!qat_prov_is_running() || prsactx == NULL)
return 0;
if (prsactx->mdctx == NULL)
return 0;
if (!prsactx->flag_allow_final) {
QATerr(ERR_LIB_PROV, QAT_R_PROV_FINAL_CALL_OUT_OF_ORDER);
return 0;
}
if (!EVP_DigestFinal_ex(prsactx->mdctx, digest, &dlen))
return 0;
prsactx->flag_allow_update = 0;
prsactx->flag_allow_final = 0;
prsactx->flag_allow_oneshot = 0;
return rsa_verify_directly(prsactx, prsactx->sig, prsactx->siglen,
digest, dlen);
}
#endif
/**
* @brief Initializes the QAT RSA sign/verify context for a signature operation.
*
* This function sets up the QAT_PROV_RSA_CTX structure for a sign or verify operation,
* associates it with the given RSA key, sets the operation type, and initializes
* padding mode and salt length according to the key type and any PSS restrictions.
* It also applies any context parameters provided and prepares the context for
* subsequent signing or verification steps.
*
* @param prsactx Pointer to the QAT_PROV_RSA_CTX context to initialize.
* @param vrsa Pointer to the QAT_RSA key structure (may be NULL if already set).
* @param params Optional OSSL_PARAM array of context parameters.
* @param operation Operation type (e.g., EVP_PKEY_OP_SIGN, EVP_PKEY_OP_VERIFY).
*
* @return 1 on success, 0 on failure.
*/
static int qat_rsa_signverify_init(QAT_PROV_RSA_CTX *prsactx, void *vrsa,
const OSSL_PARAM params[], int operation)
{
DEBUG("%s\n", __func__);
if (!qat_prov_is_running())
return 0;
if (prsactx == NULL || vrsa == NULL)
return 0;
if (vrsa != NULL) {
if (!QAT_RSA_up_ref(vrsa))
return 0;
QAT_RSA_free(prsactx->rsa);
prsactx->rsa = vrsa;
}
prsactx->operation = operation;
prsactx->flag_allow_update = 1;
prsactx->flag_allow_final = 1;
prsactx->flag_allow_oneshot = 1;
/* Maximum for sign, auto for verify */
prsactx->saltlen = RSA_PSS_SALTLEN_AUTO;
prsactx->min_saltlen = -1;
switch (QAT_RSA_test_flags(prsactx->rsa, RSA_FLAG_TYPE_MASK)) {
case RSA_FLAG_TYPE_RSA:
prsactx->pad_mode = RSA_PKCS1_PADDING;
break;
case RSA_FLAG_TYPE_RSASSAPSS:
prsactx->pad_mode = RSA_PKCS1_PSS_PADDING;
{
const QAT_RSA_PSS_PARAMS_30 *pss =
qat_rsa_get0_pss_params_30(prsactx->rsa);
if (!qat_rsa_pss_params_30_is_unrestricted(pss)) {
int md_nid = qat_rsa_pss_params_30_hashalg(pss);
int mgf1md_nid = qat_rsa_pss_params_30_maskgenhashalg(pss);
int min_saltlen = qat_rsa_pss_params_30_saltlen(pss);
const char *mdname, *mgf1mdname;
size_t len;
mdname = qat_rsa_oaeppss_nid2name(md_nid);
mgf1mdname = qat_rsa_oaeppss_nid2name(mgf1md_nid);
if (mdname == NULL) {
WARN("PSS restrictions lack hash algorithm");
QATerr(ERR_LIB_PROV, PROV_R_INVALID_DIGEST);
return 0;
}
if (mgf1mdname == NULL) {
WARN("PSS restrictions lack MGF1 hash algorithm");
QATerr(ERR_LIB_PROV, PROV_R_INVALID_DIGEST);
return 0;
}
len = OPENSSL_strlcpy(prsactx->mdname, mdname,
sizeof(prsactx->mdname));
if (len >= sizeof(prsactx->mdname)) {
WARN("hash algorithm name too long");
QATerr(ERR_LIB_PROV, PROV_R_INVALID_DIGEST);
return 0;
}
len = OPENSSL_strlcpy(prsactx->mgf1_mdname, mgf1mdname,
sizeof(prsactx->mgf1_mdname));
if (len >= sizeof(prsactx->mgf1_mdname)) {
WARN("MGF1 hash algorithm name too long");
QATerr(ERR_LIB_PROV, PROV_R_INVALID_DIGEST);
return 0;
}
prsactx->saltlen = min_saltlen;
/* call rsa_setup_mgf1_md before rsa_setup_md to avoid duplication */
return qat_rsa_setup_mgf1_md(prsactx, mgf1mdname, prsactx->propq)
&& qat_rsa_setup_md(prsactx, mdname, prsactx->propq)
&& qat_rsa_check_parameters(prsactx, min_saltlen);
}
}
break;
default:
QATerr(ERR_LIB_RSA, PROV_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
return 0;
}
if (!qat_signature_rsa_set_ctx_params(prsactx, params))
return 0;
return 1;
}
static int qat_signature_rsa_sign_init(void *vprsactx, void *vrsa,
const OSSL_PARAM params[])
{
DEBUG("qat_rsa_sign_init.\n");
if (!qat_prov_is_running())
return 0;
return qat_rsa_signverify_init(vprsactx, vrsa, params, EVP_PKEY_OP_SIGN);
}
/**
* @brief Signs a message using the QAT RSA signature context.
*
* This function generates an RSA signature for the provided message or digest using the
* QAT_PROV_RSA_CTX context. It supports both one-shot and streaming (multi-part) signing
* operations, applies the appropriate padding mode, and handles FIPS checks if enabled.
* The function writes the signature to the output buffer and sets the signature length.
*
* @param vprsactx Pointer to the QAT_PROV_RSA_CTX context.
* @param sig Output buffer for the resulting signature.
* @param siglen Pointer to a size_t to receive the signature length.
* @param sigsize Size of the output buffer.
* @param tbs Input data to be signed (message or digest).
* @param tbslen Length of the input data.
*
* @return 1 on success, 0 on failure.
*/
static int qat_signature_rsa_sign(void *vprsactx, unsigned char *sig,
size_t *siglen, size_t sigsize,
const unsigned char *tbs, size_t tbslen)
{
DEBUG("%s\n", __func__);
QAT_PROV_RSA_CTX *prsactx = (QAT_PROV_RSA_CTX *)vprsactx;
#ifdef ENABLE_QAT_FIPS
size_t rsasize = QAT_RSA_size(prsactx->rsa);
if (!qat_fips_check_rsa_key_size(rsasize, 1))
return 0;
#endif
if (!qat_prov_is_running())
return 0;
if (!prsactx->flag_allow_oneshot) {
QATerr(ERR_LIB_PROV, QAT_R_PROV_ONESHOT_CALL_OUT_OF_ORDER);
return 0;
}
#if OPENSSL_VERSION_NUMBER >= 0x30400000
if (prsactx->operation == EVP_PKEY_OP_SIGNMSG) {
if (sig == NULL)
return rsa_sign_message_final(prsactx, sig, siglen, sigsize);
return rsa_signverify_message_update(prsactx, tbs, tbslen)
&& rsa_sign_message_final(prsactx, sig, siglen, sigsize);
}
#endif
return rsa_sign_directly(prsactx, sig, siglen, sigsize, tbs, tbslen);
}
static int qat_signature_rsa_verify_recover_init(void *vprsactx, void *vrsa,
const OSSL_PARAM params[])
{
DEBUG("qat_rsa_verify_recover_init.\n");
if (!qat_prov_is_running())
return 0;
return qat_rsa_signverify_init(vprsactx, vrsa, params,
EVP_PKEY_OP_VERIFYRECOVER);
}
static int qat_signature_rsa_verify_init(void *vprsactx, void *vrsa,
const OSSL_PARAM params[])
{
DEBUG("qat_rsa_verify_init.\n");
if (!qat_prov_is_running())
return 0;
return qat_rsa_signverify_init(vprsactx, vrsa, params, EVP_PKEY_OP_VERIFY);
}
/**
* @brief Verifies an RSA signature using the QAT RSA signature context.
*
* This function checks the validity of the provided signature against the given message or digest,
* using the QAT_PROV_RSA_CTX context. It supports both one-shot and streaming (multi-part) verify
* operations, applies the appropriate padding mode, and handles FIPS checks if enabled. The function
* returns 1 if the signature is valid, 0 otherwise.
*
* @param vprsactx Pointer to the QAT_PROV_RSA_CTX context.
* @param sig Input buffer containing the signature to verify.
* @param siglen Length of the signature buffer.
* @param tbs Input data to verify against (message or digest).
* @param tbslen Length of the input data.
*
* @return 1 on successful verification, 0 on failure.
*/
static int qat_signature_rsa_verify(void *vprsactx, const unsigned char *sig,
size_t siglen, const unsigned char *tbs,
size_t tbslen)
{
DEBUG("%s\n", __func__);
QAT_PROV_RSA_CTX *prsactx = (QAT_PROV_RSA_CTX *)vprsactx;
#ifdef ENABLE_QAT_FIPS
size_t rsasize = QAT_RSA_size(prsactx->rsa);
if (!qat_fips_check_rsa_key_size(rsasize, 0))
return 0;
#endif
if (!prsactx->flag_allow_oneshot) {
QATerr(ERR_LIB_PROV, QAT_R_PROV_ONESHOT_CALL_OUT_OF_ORDER);
return 0;
}
#if OPENSSL_VERSION_NUMBER >= 0x30400000
if (prsactx->operation == EVP_PKEY_OP_VERIFYMSG) {
return rsa_verify_set_sig(prsactx, sig, siglen)
&& rsa_signverify_message_update(prsactx, tbs, tbslen)
&& rsa_verify_message_final(prsactx);
}
#endif
return rsa_verify_directly(prsactx, sig, siglen, tbs, tbslen);
}
static const OSSL_PARAM *qat_signature_rsa_settable_ctx_params(void *vprsactx,
ossl_unused void *provctx)
{
QAT_PROV_RSA_CTX *prsactx = (QAT_PROV_RSA_CTX *)vprsactx;
if (prsactx != NULL && !prsactx->flag_allow_md)
return settable_ctx_params_no_digest;
return settable_ctx_params;
}
/**
* @brief Initializes the QAT RSA signature context for digest sign or verify operations.
*
* This function prepares the QAT_PROV_RSA_CTX context for a digest sign or verify operation,
* associates it with the given RSA key, sets the operation type, and initializes the digest
* method if specified. It also applies any context parameters provided and allocates the
* message digest context for subsequent streaming or one-shot operations.
*
* @param vprsactx Pointer to the QAT_PROV_RSA_CTX context.
* @param mdname Name of the digest algorithm to use (may be NULL).
* @param vrsa Pointer to the QAT_RSA key structure.
* @param params Optional OSSL_PARAM array of context parameters.
* @param operation Operation type (e.g., EVP_PKEY_OP_SIGN, EVP_PKEY_OP_VERIFY).
*
* @return 1 on success, 0 on failure.
*/
static int qat_signature_rsa_digest_signverify_init(void *vprsactx,
const char *mdname,
void *vrsa,
const OSSL_PARAM params[],
int operation)
{
DEBUG("%s\n", __func__);
QAT_PROV_RSA_CTX *prsactx = (QAT_PROV_RSA_CTX *)vprsactx;
if (!qat_prov_is_running())
return 0;
if (!qat_rsa_signverify_init(prsactx, vrsa, params, operation))
return 0;
if (mdname != NULL
/* was rsa_setup_md already called in rsa_signverify_init()? */
&& (mdname[0] == '\0' || OPENSSL_strcasecmp(prsactx->mdname, mdname) != 0)
&& !qat_rsa_setup_md(prsactx, mdname, prsactx->propq))
return 0;
prsactx->flag_allow_md = 0;
if (prsactx->mdctx == NULL) {
prsactx->mdctx = EVP_MD_CTX_new();
if (prsactx->mdctx == NULL)
goto error;
}
if (!EVP_DigestInit_ex2(prsactx->mdctx, prsactx->md, params))
goto error;
return 1;
DEBUG("%s\n", __func__);
error:
EVP_MD_CTX_free(prsactx->mdctx);
prsactx->mdctx = NULL;
return 0;
}
static int qat_signature_rsa_digest_sign_init(void *vprsactx, const char *mdname,
void *vrsa, const OSSL_PARAM params[])
{
DEBUG("%s\n", __func__);
if (!qat_prov_is_running())
return 0;
#if OPENSSL_VERSION_NUMBER < 0x30400000
return qat_signature_rsa_digest_signverify_init(vprsactx, mdname, vrsa,
params, EVP_PKEY_OP_SIGN);
#else
return qat_signature_rsa_digest_signverify_init(vprsactx, mdname, vrsa,
params, EVP_PKEY_OP_SIGNMSG);
#endif
}
static int qat_signature_rsa_digest_sign_update(void *vprsactx,
const unsigned char *data,
size_t datalen)
{
DEBUG("%s\n", __func__);
QAT_PROV_RSA_CTX *prsactx = (QAT_PROV_RSA_CTX *)vprsactx;
return rsa_signverify_message_update(prsactx, data, datalen);
}
static int qat_signature_rsa_digest_verify_update(void *vprsactx,
const unsigned char *data,
size_t datalen)
{
DEBUG("%s\n", __func__);
QAT_PROV_RSA_CTX *prsactx = (QAT_PROV_RSA_CTX *)vprsactx;
if (prsactx == NULL)
return 0;
return rsa_signverify_message_update(prsactx, data, datalen);
}
static int qat_signature_rsa_digest_sign_final(void *vprsactx, unsigned char *sig,
size_t *siglen, size_t sigsize)
{
DEBUG("%s\n", __func__);
QAT_PROV_RSA_CTX *prsactx = (QAT_PROV_RSA_CTX *)vprsactx;
int ret = 0;
if (!qat_prov_is_running() || prsactx == NULL)
return 0;
if (rsa_sign_message_final(prsactx, sig, siglen, sigsize))
ret = 1;
/*
* If sig is NULL then we're just finding out the sig size. Other fields
* are ignored. Defer to rsa_sign.
*/
prsactx->flag_allow_md = 1;
return ret;
}
static int qat_signature_rsa_digest_verify_init(void *vprsactx, const char *mdname,
void *vrsa, const OSSL_PARAM params[])
{
DEBUG("%s\n", __func__);
if (!qat_prov_is_running())
return 0;
#if OPENSSL_VERSION_NUMBER < 0x30400000
return qat_signature_rsa_digest_signverify_init(vprsactx, mdname, vrsa,
params, EVP_PKEY_OP_VERIFY);
#else
return qat_signature_rsa_digest_signverify_init(vprsactx, mdname, vrsa,
params, EVP_PKEY_OP_VERIFYMSG);
#endif
}
/**
* @brief Finalizes a multi-part RSA digest verify operation and checks the signature.
*
* This function completes a streaming (multi-part) RSA digest verify operation by finalizing
* the message digest, performing any necessary padding checks, and verifying the provided
* signature against the computed digest. It supports both one-shot and streaming usage,
* and handles OpenSSL version differences for signature verification.
*
* @param vprsactx Pointer to the QAT_PROV_RSA_CTX context.
* @param sig Input buffer containing the signature to verify.
* @param siglen Length of the signature buffer.
*
* @return 1 on successful verification, 0 on failure.
*/
int qat_signature_rsa_digest_verify_final(void *vprsactx, const unsigned char *sig,
size_t siglen)
{
QAT_PROV_RSA_CTX *prsactx = (QAT_PROV_RSA_CTX *)vprsactx;
if (!qat_prov_is_running())
return 0;
if (prsactx == NULL)
return 0;
prsactx->flag_allow_md = 1;
#if OPENSSL_VERSION_NUMBER >= 0x30400000
int ret = 0;
ret = rsa_verify_set_sig(prsactx, sig, siglen);
if (ret)
ret = rsa_verify_message_final(vprsactx);
return ret;
#else
unsigned char digest[EVP_MAX_MD_SIZE];
unsigned int dlen = 0;
if (prsactx->mdctx == NULL)
return 0;
if (!EVP_DigestFinal_ex(prsactx->mdctx, digest, &dlen))
return 0;
return rsa_verify_directly(prsactx, sig, siglen,
digest, dlen);
#endif
}
/**
* @brief Duplicates a QAT RSA signature context.
*
* This function creates a deep copy of the given QAT_PROV_RSA_CTX context, including
* all associated resources such as the RSA key, digest algorithms, digest contexts,
* property query string, and temporary buffers. Reference counts are incremented for
* shared resources. The duplicated context can be used independently of the original.
*
* @param vprsactx Pointer to the QAT_PROV_RSA_CTX context to duplicate.
*
* @return Pointer to the newly allocated duplicate context, or NULL on failure.
*/
static void *qat_signature_rsa_dupctx(void *vprsactx)
{
DEBUG("%s\n", __func__);
QAT_PROV_RSA_CTX *srcctx = (QAT_PROV_RSA_CTX *)vprsactx;
QAT_PROV_RSA_CTX *dstctx;
if (!qat_prov_is_running())
return NULL;
dstctx = OPENSSL_zalloc(sizeof(*srcctx));
if (dstctx == NULL) {
QATerr(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
return NULL;
}
*dstctx = *srcctx;
dstctx->rsa = NULL;
dstctx->md = NULL;
dstctx->mdctx = NULL;
dstctx->tbuf = NULL;
dstctx->propq = NULL;
if (srcctx->rsa != NULL && !QAT_RSA_up_ref(srcctx->rsa))
goto err;
dstctx->rsa = srcctx->rsa;
if (srcctx->md != NULL && !EVP_MD_up_ref(srcctx->md))
goto err;
dstctx->md = srcctx->md;
if (srcctx->mgf1_md != NULL && !EVP_MD_up_ref(srcctx->mgf1_md))
goto err;
dstctx->mgf1_md = srcctx->mgf1_md;
if (srcctx->mdctx != NULL) {
dstctx->mdctx = EVP_MD_CTX_new();
if (dstctx->mdctx == NULL
|| !EVP_MD_CTX_copy_ex(dstctx->mdctx, srcctx->mdctx))
goto err;
}
if (srcctx->propq != NULL) {
dstctx->propq = OPENSSL_strdup(srcctx->propq);
if (dstctx->propq == NULL)
goto err;
}
return dstctx;
err:
qat_signature_rsa_freectx(dstctx);
return NULL;
}
/**
* @brief Retrieves context parameters for the QAT RSA signature context.
*
* This function populates the provided OSSL_PARAM array with the current settings
* from the QAT_PROV_RSA_CTX context, such as padding mode, digest algorithm name,
* MGF1 digest name, and PSS salt length. It is used by OpenSSL to query the state
* of the RSA signature context.
*
* @param vprsactx Pointer to the QAT_PROV_RSA_CTX context.
* @param params Array of OSSL_PARAM to be populated with context parameters.
*
* @return 1 on success, 0 on failure.
*/
static int qat_signature_rsa_get_ctx_params(void *vprsactx, OSSL_PARAM *params)
{
DEBUG("%s\n", __func__);
QAT_PROV_RSA_CTX *prsactx = (QAT_PROV_RSA_CTX *)vprsactx;
OSSL_PARAM *p;
if (prsactx == NULL)
return 0;
p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_PAD_MODE);
if (p != NULL) {
switch (p->data_type) {
case OSSL_PARAM_INTEGER:
if (!OSSL_PARAM_set_int(p, prsactx->pad_mode))
return 0;
break;
case OSSL_PARAM_UTF8_STRING:
{
int i;
const char *word = NULL;
for (i = 0; padding_item[i].id != 0; i++) {
if (prsactx->pad_mode == (int)padding_item[i].id) {
word = padding_item[i].ptr;
break;
}
}
if (word != NULL) {
if (!OSSL_PARAM_set_utf8_string(p, word))
return 0;
} else {
QATerr(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
}
}
break;
default:
return 0;
}
}
p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_DIGEST);
if (p != NULL && !OSSL_PARAM_set_utf8_string(p, prsactx->mdname))
return 0;
p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_MGF1_DIGEST);
if (p != NULL && !OSSL_PARAM_set_utf8_string(p, prsactx->mgf1_mdname))
return 0;
p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_PSS_SALTLEN);
if (p != NULL) {
if (p->data_type == OSSL_PARAM_INTEGER) {
if (!OSSL_PARAM_set_int(p, prsactx->saltlen))
return 0;
} else if (p->data_type == OSSL_PARAM_UTF8_STRING) {
const char *value = NULL;
switch (prsactx->saltlen) {
case RSA_PSS_SALTLEN_DIGEST:
value = OSSL_PKEY_RSA_PSS_SALT_LEN_DIGEST;
break;
case RSA_PSS_SALTLEN_MAX:
value = OSSL_PKEY_RSA_PSS_SALT_LEN_MAX;
break;
case RSA_PSS_SALTLEN_AUTO:
value = OSSL_PKEY_RSA_PSS_SALT_LEN_AUTO;
break;
case RSA_PSS_SALTLEN_AUTO_DIGEST_MAX:
value = OSSL_PKEY_RSA_PSS_SALT_LEN_AUTO_DIGEST_MAX;
break;
default:
{
int len = BIO_snprintf(p->data, p->data_size, "%d",
prsactx->saltlen);
if (len <= 0)
return 0;
p->return_size = len;
break;
}
}
if (value != NULL
&& !OSSL_PARAM_set_utf8_string(p, value))
return 0;
}
}
return 1;
}
/**
* @brief Retrieves the set of gettable message digest context parameters for QAT RSA signature.
*
* This function returns the list of OSSL_PARAM descriptors that can be queried from the
* message digest context associated with the QAT_PROV_RSA_CTX. It is used by OpenSSL to
* determine which digest-related parameters are available for retrieval.
*
* @param vprsactx Pointer to the QAT_PROV_RSA_CTX context.
*
* @return Pointer to an array of OSSL_PARAM descriptors, or NULL if unavailable.
*/
static const OSSL_PARAM *qat_signature_rsa_gettable_ctx_md_params(void *vprsactx)
{
QAT_PROV_RSA_CTX *prsactx = (QAT_PROV_RSA_CTX *)vprsactx;
if (prsactx->md == NULL)
return 0;
return EVP_MD_gettable_ctx_params(prsactx->md);
}
/**
* @brief Sets message digest context parameters for the QAT RSA signature context.
*
* This function updates the message digest context (EVP_MD_CTX) associated with the
* QAT_PROV_RSA_CTX using the provided OSSL_PARAM array. It is used by OpenSSL to
* configure digest-specific parameters (such as control parameters for the digest)
* before performing signature operations.
*
* @param vprsactx Pointer to the QAT_PROV_RSA_CTX context.
* @param params Array of OSSL_PARAM containing the digest parameters to set.
*
* @return 1 on success, 0 on failure.
*/
static int qat_signature_rsa_set_ctx_md_params(void *vprsactx,
const OSSL_PARAM params[])
{
QAT_PROV_RSA_CTX *prsactx = (QAT_PROV_RSA_CTX *)vprsactx;
if (prsactx->mdctx == NULL)
return 0;
return EVP_MD_CTX_set_params(prsactx->mdctx, params);
}
/**
* @brief Retrieves the set of settable message digest context parameters for QAT RSA signature.
*
* This function returns the list of OSSL_PARAM descriptors that can be set on the
* message digest context associated with the QAT_PROV_RSA_CTX. It is used by OpenSSL to
* determine which digest-related parameters are available for configuration before
* performing signature operations.
*
* @param vprsactx Pointer to the QAT_PROV_RSA_CTX context.
*
* @return Pointer to an array of OSSL_PARAM descriptors, or NULL if unavailable.
*/
static const OSSL_PARAM *qat_signature_rsa_settable_ctx_md_params(void *vprsactx)
{
QAT_PROV_RSA_CTX *prsactx = (QAT_PROV_RSA_CTX *)vprsactx;
if (prsactx->md == NULL)
return 0;
return EVP_MD_settable_ctx_params(prsactx->md);
}
static const OSSL_PARAM *qat_signature_rsa_gettable_ctx_params(ossl_unused void *vprsactx,
ossl_unused void *provctx)
{
return known_gettable_ctx_params;
}
/**
* @brief Retrieves message digest context parameters for the QAT RSA signature context.
*
* This function populates the provided OSSL_PARAM array with the current settings
* from the message digest context (EVP_MD_CTX) associated with the QAT_PROV_RSA_CTX.
* It is used by OpenSSL to query digest-specific parameters, such as digest size or
* control parameters, from the signature context.
*
* @param vprsactx Pointer to the QAT_PROV_RSA_CTX context.
* @param params Array of OSSL_PARAM to be populated with digest context parameters.
*
* @return 1 on success, 0 on failure.
*/
static int qat_signature_rsa_get_ctx_md_params(void *vprsactx, OSSL_PARAM *params)
{
QAT_PROV_RSA_CTX *prsactx = (QAT_PROV_RSA_CTX *)vprsactx;
if (prsactx->mdctx == NULL)
return 0;
return EVP_MD_CTX_get_params(prsactx->mdctx, params);
}
const OSSL_DISPATCH qat_rsa_signature_functions[] = {
{OSSL_FUNC_SIGNATURE_NEWCTX, (void (*)(void))qat_signature_rsa_newctx},
{OSSL_FUNC_SIGNATURE_SIGN_INIT, (void (*)(void))qat_signature_rsa_sign_init},
{OSSL_FUNC_SIGNATURE_SIGN, (void (*)(void))qat_signature_rsa_sign},
{ OSSL_FUNC_SIGNATURE_VERIFY_INIT, (void (*)(void))qat_signature_rsa_verify_init },
{ OSSL_FUNC_SIGNATURE_VERIFY, (void (*)(void))qat_signature_rsa_verify },
{ OSSL_FUNC_SIGNATURE_VERIFY_RECOVER_INIT,
(void (*)(void))qat_signature_rsa_verify_recover_init },
{ OSSL_FUNC_SIGNATURE_VERIFY_RECOVER,
(void (*)(void))qat_signature_rsa_verify_recover },
{OSSL_FUNC_SIGNATURE_FREECTX, (void (*)(void))qat_signature_rsa_freectx},
{OSSL_FUNC_SIGNATURE_SET_CTX_PARAMS, (void (*)(void))qat_signature_rsa_set_ctx_params},
{OSSL_FUNC_SIGNATURE_SETTABLE_CTX_PARAMS,
(void (*)(void))qat_signature_rsa_settable_ctx_params},
{ OSSL_FUNC_SIGNATURE_DIGEST_SIGN_INIT,
(void (*)(void))qat_signature_rsa_digest_sign_init },
{ OSSL_FUNC_SIGNATURE_DIGEST_SIGN_UPDATE,
(void (*)(void))qat_signature_rsa_digest_sign_update },
{ OSSL_FUNC_SIGNATURE_DIGEST_SIGN_FINAL,
(void (*)(void))qat_signature_rsa_digest_sign_final },
{ OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_INIT,
(void (*)(void))qat_signature_rsa_digest_verify_init },
{ OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_UPDATE,
(void (*)(void))qat_signature_rsa_digest_verify_update },
{ OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_FINAL,
(void (*)(void))qat_signature_rsa_digest_verify_final },
{ OSSL_FUNC_SIGNATURE_DUPCTX, (void (*)(void))qat_signature_rsa_dupctx },
{ OSSL_FUNC_SIGNATURE_GET_CTX_PARAMS, (void (*)(void))qat_signature_rsa_get_ctx_params },
{ OSSL_FUNC_SIGNATURE_GETTABLE_CTX_PARAMS,
(void (*)(void))qat_signature_rsa_gettable_ctx_params },
{ OSSL_FUNC_SIGNATURE_GET_CTX_MD_PARAMS,
(void (*)(void))qat_signature_rsa_get_ctx_md_params },
{ OSSL_FUNC_SIGNATURE_GETTABLE_CTX_MD_PARAMS,
(void (*)(void))qat_signature_rsa_gettable_ctx_md_params },
{ OSSL_FUNC_SIGNATURE_SET_CTX_MD_PARAMS,
(void (*)(void))qat_signature_rsa_set_ctx_md_params },
{ OSSL_FUNC_SIGNATURE_SETTABLE_CTX_MD_PARAMS,
(void (*)(void))qat_signature_rsa_settable_ctx_md_params },
{0, NULL}
};
#endif
|