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
|
/* ====================================================================
*
*
* BSD LICENSE
*
* Copyright(c) 2020-2025 Intel Corporation.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Intel Corporation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*
* ====================================================================
*/
/*****************************************************************************
* @file qat_hw_gcm.c
*
* This file contains the engine implementations for GCM cipher operations
*
*****************************************************************************/
#ifndef _GNU_SOURCE
# define _GNU_SOURCE
#endif
#include <string.h>
#include <pthread.h>
#include <signal.h>
#include "e_qat.h"
#include "qat_utils.h"
#include "qat_hw_callback.h"
#include "qat_hw_polling.h"
#include "qat_events.h"
#include "cpa.h"
#include "cpa_types.h"
#include "cpa_cy_sym.h"
#include "qat_evp.h"
#include "qat_hw_gcm.h"
#include "qat_hw_ciphers.h"
#include <openssl/evp.h>
#include <openssl/aes.h>
#include <openssl/err.h>
#include <openssl/sha.h>
#include <openssl/tls1.h>
#include <openssl/ssl.h>
#ifdef ENABLE_QAT_FIPS
# include "qat_prov_cmvp.h"
#endif
#ifdef ENABLE_QAT_HW_GCM
# ifdef ENABLE_QAT_FIPS
extern int qat_fips_key_zeroize;
# endif
/******************************************************************************
* function:
* qat_session_data_init(EVP_CIPHER_CTX *ctx,
* qat_aes_gcm_ctx *qctx,
* const unsigned char* key,
* int keylen,
* const unsigned char* iv,
* int ivlen,
* int enc)
*
* @param ctx [IN] - pointer to the evp context
* @param qctx [IN] - pointer to the qat context
* @param key [IN] - pointer to the cipher key
* @param iv [IN] - pointer to the iv this maybe NULL.
* @param enc [IN] - whether we are doing encryption (1) or decryption (0).
*
* description:
* This function is to create QAT specific session data.
* It is called from the function qat_aes_gcm_init().
*
* It will return 1 if successful and 0 on failure.
******************************************************************************/
#ifdef QAT_OPENSSL_PROVIDER
static int qat_session_data_init(QAT_GCM_CTX *ctx,
const unsigned char* key,
int keylen,
const unsigned char* iv,
int ivlen,
int enc)
#else
static int qat_session_data_init(EVP_CIPHER_CTX *ctx,
qat_gcm_ctx *qctx,
const unsigned char* key,
const unsigned char* iv,
int enc)
#endif
{
#ifdef QAT_OPENSSL_PROVIDER
QAT_GCM_CTX *qctx = (QAT_GCM_CTX *)ctx;
#endif
DEBUG("QAT HW GCM Started\n");
if (NULL == qctx || NULL == ctx) {
WARN("qctx or ctx is NULL\n");
QATerr(QAT_F_QAT_SESSION_DATA_INIT, QAT_R_QCTX_CTX_NULL);
return 0;
}
if (key != NULL) {
if (qctx->qat_svm) {
if (qctx->cipher_key){
OPENSSL_free(qctx->cipher_key);
qctx->cipher_key = NULL;
}
#ifdef QAT_OPENSSL_PROVIDER
qctx->cipher_key = OPENSSL_zalloc(keylen);
#else
qctx->cipher_key = OPENSSL_zalloc(EVP_CIPHER_CTX_key_length(ctx));
#endif
} else {
if (qctx->cipher_key) {
qaeCryptoMemFreeNonZero(qctx->cipher_key);
qctx->cipher_key = NULL;
}
#ifdef QAT_OPENSSL_PROVIDER
qctx->cipher_key = qaeCryptoMemAlloc(keylen, __FILE__, __LINE__);
#else
qctx->cipher_key = qaeCryptoMemAlloc(EVP_CIPHER_CTX_key_length(ctx), __FILE__, __LINE__);
#endif
}
if (qctx->cipher_key == NULL) {
WARN("Unable to allocate memory for qctx->cipher_key.\n");
QATerr(QAT_F_QAT_SESSION_DATA_INIT, QAT_R_KEY_MALLOC_FAILURE);
return 0;
}
#ifdef QAT_OPENSSL_PROVIDER
memcpy(qctx->cipher_key, key, keylen);
#else
memcpy(qctx->cipher_key, key, EVP_CIPHER_CTX_key_length(ctx));
#endif
qctx->key_set = 1;
}
if (iv != NULL && qctx->iv_set) {
qctx->OpData.pIv = qctx->iv;
qctx->OpData.ivLenInBytes = qctx->iv_len;
}
if (NULL == qctx->session_data) {
qctx->session_data = OPENSSL_zalloc(sizeof(CpaCySymSessionSetupData));
if (NULL == qctx->session_data) {
WARN("session setup data zalloc failure\n");
QATerr(QAT_F_QAT_SESSION_DATA_INIT, QAT_R_SSD_MALLOC_FAILURE);
return 0;
}
}
/* Set priority and operation of this session */
qctx->session_data->sessionPriority = CPA_CY_PRIORITY_HIGH;
qctx->session_data->symOperation = CPA_CY_SYM_OP_ALGORITHM_CHAINING;
/* --- Cipher configuration --- */
/* Cipher algorithm and mode */
qctx->session_data->cipherSetupData.cipherAlgorithm =
CPA_CY_SYM_CIPHER_AES_GCM;
/* Cipher key length */
#ifdef QAT_OPENSSL_PROVIDER
qctx->session_data->cipherSetupData.cipherKeyLenInBytes = (Cpa32U)keylen;
#else
qctx->session_data->cipherSetupData.cipherKeyLenInBytes = (Cpa32U)EVP_CIPHER_CTX_key_length(ctx);
#endif
if (qctx->key_set){
qctx->session_data->cipherSetupData.pCipherKey = (Cpa8U *)qctx->cipher_key;
#ifdef QAT_OPENSSL_PROVIDER
qctx->session_data->cipherSetupData.cipherKeyLenInBytes = (Cpa32U)qctx->keylen;
#endif
}
/* Operation to perform */
if (enc) {
qctx->session_data->cipherSetupData.cipherDirection =
CPA_CY_SYM_CIPHER_DIRECTION_ENCRYPT;
qctx->session_data->algChainOrder =
CPA_CY_SYM_ALG_CHAIN_ORDER_CIPHER_THEN_HASH;
} else {
qctx->session_data->cipherSetupData.cipherDirection =
CPA_CY_SYM_CIPHER_DIRECTION_DECRYPT;
qctx->session_data->algChainOrder =
CPA_CY_SYM_ALG_CHAIN_ORDER_HASH_THEN_CIPHER;
}
/* --- Hash Configuration --- */
/* Set the hash mode and the length of the digest */
qctx->session_data->hashSetupData.hashAlgorithm = CPA_CY_SYM_HASH_AES_GCM;
qctx->session_data->hashSetupData.hashMode = CPA_CY_SYM_HASH_MODE_AUTH;
qctx->session_data->hashSetupData.digestResultLenInBytes = EVP_GCM_TLS_TAG_LEN;
/* For GCM authKey and authKeyLen are not required.
* This information is provided by the cipherKey in cipherSetupData */
qctx->session_data->hashSetupData.authModeSetupData.authKey = NULL;
qctx->session_data->hashSetupData.authModeSetupData.authKeyLenInBytes = 0;
/* Set the length of the AAD to the default value */
qctx->session_data->hashSetupData.authModeSetupData.aadLenInBytes = 0;
/* Tag follows immediately after the region to hash */
qctx->session_data->digestIsAppended = CPA_FALSE;
/* digestVerify is not required to be set. For GCM authenticated
* encryption this value is understood to be CPA_FALSE during encryption and
* CPA_TRUE during decryption */
qctx->init_params_set = 1;
return 1;
}
/******************************************************************************
* function:
* qat_aes_gcm_init(EVP_CIPHER_CTX *ctx, const unsigned char *inkey,
* int keylen, const unsigned char *iv, int ivlen,
* int enc)
*
* @param ctx [IN] - pointer to existing context
* @param inKey [IN] - input cipher key
* @param iv [IN] - initialisation vector
* @param enc [IN] - 1 encrypt 0 decrypt
*
* @retval 1 function succeeded
* @retval 0 function failed
*
* description:
* This function initialises the cipher and hash algorithm parameters for this
* EVP context.
*
******************************************************************************/
#ifdef QAT_OPENSSL_PROVIDER
int qat_aes_gcm_init(QAT_GCM_CTX *ctx, const unsigned char *inkey,
int keylen, const unsigned char *iv, int ivlen,
int enc)
#else
int qat_aes_gcm_init(EVP_CIPHER_CTX *ctx,
const unsigned char *inkey,
const unsigned char *iv,
int enc)
#endif
{
#ifdef QAT_OPENSSL_PROVIDER
QAT_GCM_CTX* qctx = NULL;
int nid = 0;
QAT_EVP_CIPHER sw_aes_gcm_cipher;
#else
qat_gcm_ctx* qctx = NULL;
#endif
int ret = 0;
if (NULL == ctx) {
WARN("ctx is NULL\n");
QATerr(QAT_F_QAT_AES_GCM_INIT, QAT_R_CTX_NULL);
return 0;
}
DEBUG("CTX = %p, key = %p, iv = %p, enc = %d\n",
(void*)ctx, (void*)inkey, (void*)iv, enc);
#ifdef QAT_OPENSSL_PROVIDER
qctx = (QAT_GCM_CTX *)ctx;
qctx->iv = (Cpa8U *)qctx->iv;
qctx->next_iv = (Cpa8U *)qctx->next_iv;
qctx->enc = enc;
nid = qat_aes_gcm_ctx_get_nid((QAT_AES_GCM_CTX *)ctx);
#else
qctx = QAT_GCM_GET_CTX(ctx);
#endif
/* Initialise a QAT session and set the cipher keys*/
if (NULL == qctx) {
WARN("qctx is NULL\n");
QATerr(QAT_F_QAT_AES_GCM_INIT, QAT_R_QCTX_NULL);
return 0;
}
if (qat_get_sw_fallback_enabled()) {
#ifndef QAT_OPENSSL_PROVIDER
EVP_CIPHER_CTX_set_cipher_data(ctx, qctx->sw_ctx_cipher_data);
/* Run the software init function */
ret =
EVP_CIPHER_meth_get_init(GET_SW_AES_GCM_CIPHER(ctx)) (ctx, inkey, iv,
enc);
EVP_CIPHER_CTX_set_cipher_data(ctx, qctx);
#else
OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
sw_aes_gcm_cipher = get_default_cipher_aes_gcm(nid);
if (enc) {
if (!qctx->sw_ctx)
qctx->sw_ctx = sw_aes_gcm_cipher.newctx(ctx);
ret =
sw_aes_gcm_cipher.einit(qctx->sw_ctx, inkey, keylen, iv, ivlen,
params);
} else {
if (!qctx->sw_ctx)
qctx->sw_ctx = sw_aes_gcm_cipher.newctx(ctx);
unsigned int pad = 0;
params[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_PADDING, &pad);
ret =
sw_aes_gcm_cipher.dinit(qctx->sw_ctx, inkey, keylen, iv, ivlen,
params);
}
#endif
if (ret != 1)
goto err;
}
if (!inkey && !iv) {
DEBUG("key and IV not set\n");
return 1;
}
qctx->inst_num = get_instance(QAT_INSTANCE_SYM, QAT_INSTANCE_ANY);
if (qctx->inst_num == QAT_INVALID_INSTANCE) {
WARN("Failed to get a QAT instance.\n");
goto err;
}
qctx->qat_svm = !qat_instance_details[qctx->inst_num].qat_instance_info.requiresPhysicallyContiguousMemory;
if (NULL == qctx->iv) {
/* The length of the IV in the TLS case is fixed = 12 Bytes */
qctx->iv_len = QAT_GCM_TLS_TOTAL_IV_LEN;
#ifdef QAT_OPENSSL_PROVIDER
qctx->iv = qat_mem_alloc(GCM_IV_MAX_SIZE, qctx->qat_svm, __FILE__, __LINE__);
#else
qctx->iv = qat_mem_alloc(EVP_CIPHER_CTX_iv_length(ctx), qctx->qat_svm, __FILE__, __LINE__);
#endif
if (qctx->iv == NULL) {
WARN("iv is NULL.\n");
QATerr(QAT_F_QAT_AES_GCM_INIT, QAT_R_IV_MALLOC_FAILURE);
goto err;
}
}
#ifdef QAT_OPENSSL_PROVIDER
if (!qctx->next_iv)
qctx->next_iv = OPENSSL_zalloc(EVP_MAX_IV_LENGTH);
#endif
if (iv) {
/* Set the value of the IV */
memcpy(qctx->iv, iv, qctx->iv_len);
memcpy(qctx->next_iv, iv, qctx->iv_len);
qctx->iv_set = 1;
DUMPL("iv", iv, qctx->iv_len);
}
qctx->tls_aad_len = -1;
qctx->tag_len = -1;
qctx->iv_gen = 0;
qctx->tag_set = 0;
/* Initialize QAT session */
#ifdef QAT_OPENSSL_PROVIDER
if (0 == qat_session_data_init(ctx, inkey, keylen, iv, ivlen, enc)) {
#else
if (0 == qat_session_data_init(ctx, qctx, inkey, iv, enc)) {
#endif
WARN("qat_session_data_init failed.\n");
goto err;
}
return 1;
err:
if (NULL != qctx->iv) {
#ifndef QAT_OPENSSL_PROVIDER
if (qctx->iv != EVP_CIPHER_CTX_iv_noconst(ctx)) {
#else
if (qctx->iv != ctx->iv) {
#endif
QAT_MEM_FREE_NONZERO_BUFF(qctx->iv, qctx->qat_svm);
qctx->iv = NULL;
}
}
QAT_MEM_FREE_BUFF(qctx->cipher_key, qctx->qat_svm);
return ret;
}
/******************************************************************************
* * function:
* aes_gcm_increment_counter(unsigned char *ifc)
*
* @param ifc [IN,OUT] - pointer to invocation field counter
*
* description:
* Increment provided invocation field counter (64-bit int) by 1
*
*******************************************************************************/
static inline void qat_aes_gcm_inc_ctr(unsigned char* ifc)
{
int inv_field_size = 8;
unsigned char byte;
/* Loop over ifc starting with the least significant byte
* and work towards the most significant byte of ifc*/
do {
--inv_field_size;
byte = ifc[inv_field_size];
/* Increment by one and copy back to invocation field */
++byte;
ifc[inv_field_size] = byte;
if (byte)
return;
} while (inv_field_size);
}
/******************************************************************************
* function:
* qat_aes_gcm_ctrl(EVP_CIPHER_CTX *ctx,
* int type, int arg, void *ptr)
*
* @param ctx [IN] - pointer to existing context
* @param type [IN] - type of request either
* EVP_CTRL_AEAD_SET_MAC_KEY or EVP_CTRL_AEAD_TLS1_AAD
* @param arg [IN] - size of the pointed to by ptr
* @param ptr [IN] - input buffer contain the necessary parameters
*
* @retval x The return value is dependent on the type of request being made
* EVP_CTRL_AEAD_SET_MAC_KEY return of 1 is success
* EVP_CTRL_AEAD_TLS1_AAD return value indicates the amount
* for padding to be applied to the SSL/TLS record
* @retval -1 function failed
*
* description:
* This function is a generic control interface provided by the EVP API. For
* chained requests this interface is used for setting the hmac key value for
* authentication of the SSL/TLS record.
* The second type is used to specify the TLS virtual header which is used
* in the authentication calculation and to identify record payload size.
*
******************************************************************************/
#ifdef QAT_OPENSSL_PROVIDER
int qat_aes_gcm_ctrl(void *ctx, int type, int arg, void *ptr)
#else
int qat_aes_gcm_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
#endif
{
#ifdef QAT_OPENSSL_PROVIDER
QAT_GCM_CTX* qctx = NULL;
#else
qat_gcm_ctx *qctx = NULL;
int ret_sw = 0;
#endif
unsigned int plen = 0;
int enc = 0;
int ret = 0;
if (NULL == ctx) {
WARN("ctx is NULL.\n");
QATerr(QAT_F_QAT_AES_GCM_CTRL, QAT_R_CTX_NULL);
return 0;
}
#ifdef QAT_OPENSSL_PROVIDER
qctx = (QAT_GCM_CTX *)ctx;
qctx->iv = (Cpa8U *)qctx->iv;
qctx->next_iv = (Cpa8U *)qctx->next_iv;
#else
qctx = QAT_GCM_GET_CTX(ctx);
#endif
if (NULL == qctx) {
WARN("qctx is NULL\n");
QATerr(QAT_F_QAT_AES_GCM_CTRL, QAT_R_QCTX_NULL);
return 0;
}
#ifdef QAT_OPENSSL_PROVIDER
enc = QAT_GCM_GET_ENC(qctx);
#else
enc = EVP_CIPHER_CTX_encrypting(ctx);
#endif
switch (type) {
case EVP_CTRL_INIT:
DEBUG("EVP_CTRL_INIT, ctx = %p, type = %d, "
"arg = %d, ptr = %p\n", (void*)ctx, type, arg, ptr);
qctx->key_set = 0;
qctx->iv_len = 0;
qctx->iv_set = 0;
qctx->iv_gen = 0;
qctx->tls_aad_len = -1;
qctx->tag_len = -1;
qctx->tag_set = 0;
#ifndef QAT_OPENSSL_PROVIDER
if (qctx->sw_ctx_cipher_data == NULL && qat_get_sw_fallback_enabled()) {
unsigned int sw_size = 0;
sw_size = EVP_CIPHER_impl_ctx_size(GET_SW_AES_GCM_CIPHER(ctx));
qctx->sw_ctx_cipher_data = OPENSSL_zalloc(sw_size);
if (qctx->sw_ctx_cipher_data == NULL) {
WARN("Unable to allocate memory for sw_ctx_cipher_data\n");
return -1;
}
goto sw_ctrl;
}
#endif
return 1;
case EVP_CTRL_GET_IVLEN:
DEBUG("EVP_CTRL_GCM_GET_IVLEN, ctx = %p, type = %d,"
" arg = %d, ptr = %p\n", (void*)ctx, type, arg, ptr);
*(int*)ptr = qctx->iv_len;
ret = 1;
goto sw_ctrl;
case EVP_CTRL_GCM_SET_IVLEN:
DEBUG("EVP_CTRL_GCM_SET_IVLEN, ctx = %p, type = %d,"
" arg = %d, ptr = %p\n", (void*)ctx, type, arg, ptr);
/* At the moment we support only IV with length of 12 Bytes.
* This is the length of the IV in TLS.
*
* If the user wants to use an IV with different length we should
* implement the function J0 as described in GCM standard
*/
if (QAT_GCM_TLS_TOTAL_IV_LEN != arg) {
WARN("Unsupported IV length %d\n", arg);
QATerr(QAT_F_QAT_AES_GCM_CTRL, QAT_R_IV_LEN_NOT_SUPPORTED);
return 0;
}
ret = 1;
goto sw_ctrl;
case EVP_CTRL_GCM_SET_TAG:
DEBUG("EVP_CTRL_GCM_SET_TAG, ctx = %p, type = %d,"
" arg = %d, ptr = %p\n", (void*)ctx, type, arg, ptr);
if (arg <= QAT_GCM_TAG_MIN_LEN || arg > QAT_GCM_TAG_MAX_LEN || enc) {
WARN("TAG length invalid or invalid operation enc\n");
QATerr(QAT_F_QAT_AES_GCM_CTRL, QAT_R_SET_TAG_INVALID_OP);
return 0;
}
if (EVP_GCM_TLS_TAG_LEN != arg) {
WARN("TAG length %d not supported. Accepted value = 16\n", arg);
QATerr(QAT_F_QAT_AES_GCM_CTRL, QAT_R_IV_LEN_NOT_SUPPORTED);
return 0;
}
if (NULL == EVP_CIPHER_CTX_buf_noconst(ctx) || NULL == ptr) {
WARN("Memory pointer is not valid\n");
QATerr(QAT_F_QAT_AES_GCM_CTRL, QAT_R_INVALID_PTR);
return 0;
}
/* ctx->buf is a static buffer of size
* EVP_MAX_BLOCK_LENGTH = 32
*/
if (ptr) {
memcpy(EVP_CIPHER_CTX_buf_noconst(ctx), ptr, arg);
qctx->tag_set = 1;
}
qctx->tag_len = arg;
ret = 1;
goto sw_ctrl;
case EVP_CTRL_GCM_GET_TAG:
DEBUG("EVP_CTRL_GCM_GET_TAG, ctx = %p, type = %d,"
" arg = %d, ptr = %p\n", (void*)ctx, type, arg, ptr);
if (arg <= QAT_GCM_TAG_MIN_LEN || arg > QAT_GCM_TAG_MAX_LEN ||
!enc) {
WARN("TAG length invalid or invalid operation (!enc)\n");
QATerr(QAT_F_QAT_AES_GCM_CTRL, QAT_R_SET_TAG_INVALID_OP);
return 0;
}
if (NULL == EVP_CIPHER_CTX_buf_noconst(ctx) || NULL == ptr) {
WARN("Memory pointer is not valid\n");
QATerr(QAT_F_QAT_AES_GCM_CTRL, QAT_R_INVALID_PTR);
return 0;
}
memcpy(ptr, EVP_CIPHER_CTX_buf_noconst(ctx), arg);
return 1;
case EVP_CTRL_GCM_SET_IV_FIXED:
DEBUG("EVP_CTRL_GCM_SET_IV_FIXED, ctx = %p, type = %d,"
" arg = %d, ptr = %p\n", (void*)ctx, type, arg, ptr);
if (NULL == ptr) {
WARN("Memory pointer is not valid\n");
QATerr(QAT_F_QAT_AES_GCM_CTRL, QAT_R_INVALID_PTR);
return 0;
}
/* Special case: -1 length restores whole IV */
if (arg == -1) {
memcpy(qctx->next_iv, ptr, qctx->iv_len);
qctx->iv_gen = 1;
ret = 1;
goto sw_ctrl;
}
/* Fixed field must be at least 4 bytes (EVP_GCM_TLS_FIXED_IV_LEN)
* and invocation field at least 8 (EVP_GCM_TLS_EXPLICIT_IV_LEN)
*/
if ((arg < EVP_GCM_TLS_FIXED_IV_LEN) ||
(qctx->iv_len - arg) < EVP_GCM_TLS_EXPLICIT_IV_LEN) {
WARN("IV length invalid\n");
return 0;
}
if (arg != EVP_GCM_TLS_FIXED_IV_LEN) {
WARN("IV length not supported\n");
return 0;
}
if (arg) {
#ifdef QAT_OPENSSL_PROVIDER
if (qctx->next_iv != NULL)
#endif
memcpy(qctx->next_iv, ptr, arg);
}
/* Generate the explicit part of the IV for encryption */
#ifdef QAT_OPENSSL_PROVIDER
if (qctx->next_iv != NULL)
#endif
if (enc && RAND_bytes(qctx->next_iv + arg, qctx->iv_len - arg) <= 0) {
WARN("Error while generating random explicit IV\n");
if (!qat_get_sw_fallback_enabled())
QATerr(QAT_F_QAT_AES_GCM_CTRL, QAT_R_RAND_FAILURE);
ret = 0;
goto sw_ctrl;
}
qctx->iv_gen = 1;
ret = 1;
goto sw_ctrl;
case EVP_CTRL_GCM_IV_GEN:
/* Called in TLS case before encryption */
DEBUG("EVP_CTRL_GCM_IV_GEN, ctx = %p, type = %d,"
" arg = %d, ptr = %p\n", (void*)ctx, type, arg, ptr);
if (qctx->sw_tls_ctrl != TLS_CIPHER_SW_CTRL)
goto sw_ctrl;
if (NULL == qctx->iv || NULL == ptr) {
WARN("Memory pointer is not valid\n");
QATerr(QAT_F_QAT_AES_GCM_CTRL, QAT_R_IV_NULL_PTR_INVALID);
return 0;
}
if (0 == qctx->iv_gen) {
WARN("Operation is not valid\n");
QATerr(QAT_F_QAT_AES_GCM_CTRL, QAT_R_IV_GEN_INVALID);
return 0;
}
/* Set the IV that will be used in the current operation */
memcpy(qctx->iv, qctx->next_iv, qctx->iv_len);
if (arg <= 0 || arg > qctx->iv_len) {
arg = qctx->iv_len;
}
/* Copy the explicit IV in the output buffer */
memcpy(ptr, qctx->next_iv + qctx->iv_len - arg, arg);
/* Invocation field will be at least 8 bytes in size and
* so no need to check wrap around or increment more than
* last 8 bytes.
*/
qat_aes_gcm_inc_ctr(qctx->next_iv + qctx->iv_len - 8);
qctx->iv_set = 1;
return 1;
case EVP_CTRL_GCM_SET_IV_INV:
/* Called in TLS case before decryption */
DEBUG("EVP_CTRL_GCM_SET_IV_INV, ctx = %p, type = %d,"
" arg = %d, ptr = %p\n", (void*)ctx, type, arg, ptr);
if (qctx->sw_tls_ctrl != TLS_CIPHER_SW_CTRL)
goto sw_ctrl;
if (0 == qctx->iv_gen || enc) {
WARN("Operation is not valid\n");
QATerr(QAT_F_QAT_AES_GCM_CTRL, QAT_R_IV_NVALID);
return 0;
}
if (NULL == qctx->iv || NULL == ptr) {
WARN("Memory pointer is not valid\n");
QATerr(QAT_F_QAT_AES_GCM_CTRL, QAT_R_IV_INVALID);
return 0;
}
/* Retrieve the explicit IV from the message buffer */
memcpy(qctx->next_iv + qctx->iv_len - arg, ptr, arg);
/* Set the IV that will be used in the current operation */
memcpy(qctx->iv, qctx->next_iv, qctx->iv_len);
qctx->iv_set = 1;
return 1;
case EVP_CTRL_AEAD_TLS1_AAD:
DEBUG("EVP_CTRL_AEAD_TLS1_AAD, ctx = %p, type = %d,"
" arg = %d, ptr = %p\n", (void*)ctx, type, arg, ptr);
if (TLS_VIRT_HDR_SIZE != arg) {
WARN("AAD length is not valid\n");
QATerr(QAT_F_QAT_AES_GCM_CTRL, QAT_R_AAD_LEN_INVALID);
return 0;
}
/* Allocate the memory only the first time */
if (qctx->tls_aad_len < 0) {
int aad_buffer_len = TLS_VIRT_HDR_SIZE;
DEBUG("Allocating memory for AAD in TLS sync\n");
/* For QAT the length of the buffer for AAD must be multiple
* of block size */
if (aad_buffer_len % AES_BLOCK_SIZE) {
aad_buffer_len += AES_BLOCK_SIZE - (aad_buffer_len % AES_BLOCK_SIZE);
DEBUG("Adjusting AAD buffer length = %d\n", aad_buffer_len);
}
qctx->aad = qat_mem_alloc(aad_buffer_len, qctx->qat_svm, __FILE__, __LINE__);
if (NULL == qctx->aad) {
WARN("Unable to allocate memory for TLS header\n");
QATerr(QAT_F_QAT_AES_GCM_CTRL, QAT_R_AAD_MALLOC_FAILURE);
return 0;
}
/* Set the flag to mark the TLS case */
qctx->tls_aad_len = TLS_VIRT_HDR_SIZE;
/* Set the length of the AAD in the session
* The session hasn't been initialized yet here and this value
* should never change in the TLS case */
if (qctx->session_data != NULL)
qctx->session_data->hashSetupData.authModeSetupData.aadLenInBytes = TLS_VIRT_HDR_SIZE;
}
if (NULL == qctx->aad || NULL == ptr) {
WARN("Memory pointer is not valid\n");
QATerr(QAT_F_QAT_AES_GCM_CTRL, QAT_R_AAD_INVALID_PTR);
return 0;
}
/* Copy the header from p into the buffer */
memcpy(qctx->aad, ptr, TLS_VIRT_HDR_SIZE);
/* Extract the length of the payload from the TLS header */
plen = qctx->aad[arg - QAT_GCM_TLS_PAYLOADLENGTH_MSB_OFFSET]
<< QAT_BYTE_SHIFT |
qctx->aad[arg - QAT_GCM_TLS_PAYLOADLENGTH_LSB_OFFSET];
DEBUG("IN plen = %d\n", plen);
DUMPL("IN qctx->aad", qctx->aad, TLS_VIRT_HDR_SIZE);
/* The payload contains the explicit IV -> correct the length */
plen -= EVP_GCM_TLS_EXPLICIT_IV_LEN;
/* If decrypting correct for tag too */
if (!enc) {
plen -= EVP_GCM_TLS_TAG_LEN;
}
/* Fix the length like in the SW version of GCM */
qctx->aad[TLS_VIRT_HDR_SIZE - QAT_GCM_TLS_PAYLOADLENGTH_MSB_OFFSET]
= plen >> QAT_BYTE_SHIFT;
qctx->aad[TLS_VIRT_HDR_SIZE - QAT_GCM_TLS_PAYLOADLENGTH_LSB_OFFSET]
= plen;
DEBUG("OUT plen = %d\n", plen);
DUMPL("OUT qctx->aad", qctx->aad, TLS_VIRT_HDR_SIZE);
/* Return the length of the TAG */
ret = EVP_GCM_TLS_TAG_LEN;
goto sw_ctrl;
default:
WARN("Invalid type %d\n", type);
QATerr(QAT_F_QAT_AES_GCM_CTRL, QAT_R_INVALID_CTRL_TYPE);
return -1;
}
sw_ctrl:
#ifndef QAT_OPENSSL_PROVIDER
if (qat_get_sw_fallback_enabled()) {
if (type != EVP_CTRL_GCM_SET_IV_INV && type != EVP_CTRL_GCM_IV_GEN)
EVP_CIPHER_CTX_set_cipher_data(ctx, qctx->sw_ctx_cipher_data);
ret_sw =
EVP_CIPHER_meth_get_ctrl(GET_SW_AES_GCM_CIPHER(ctx)) (ctx, type, arg,
ptr);
if (type != EVP_CTRL_GCM_SET_IV_INV && type != EVP_CTRL_GCM_IV_GEN)
EVP_CIPHER_CTX_set_cipher_data(ctx, qctx);
if (ret_sw <= 0) {
WARN("s/w chained ciphers ctrl function failed.\n");
return ret_sw;
}
return ret_sw;
}
#endif
return ret;
}
/******************************************************************************
* function:
* qat_aes_gcm_cleanup(EVP_CIPHER_CTX *ctx)
*
* @param ctx [IN] - pointer to existing context
*
* @retval 1 function succeeded
* @retval 0 function failed
*
* description:
* This function will cleanup all allocated resources required to perform the
* cryptographic transform.
*
******************************************************************************/
#ifdef QAT_OPENSSL_PROVIDER
int qat_aes_gcm_cleanup(void *ctx)
#else
int qat_aes_gcm_cleanup(EVP_CIPHER_CTX *ctx)
#endif
{
#ifdef QAT_OPENSSL_PROVIDER
# ifdef ENABLE_QAT_FIPS
qat_fips_key_zeroize = 0;
# endif
QAT_GCM_CTX *qctx = NULL;
#else
qat_gcm_ctx *qctx = NULL;
#endif
CpaStatus sts = 0;
CpaCySymSessionSetupData* session_data = NULL;
CpaBoolean sessionInUse = CPA_FALSE;
int ret_val = 1;
DEBUG("- Entering\n");
if (NULL == ctx) {
WARN("ctx is NULL\n");
QATerr(QAT_F_QAT_AES_GCM_CLEANUP, QAT_R_CTX_NULL);
return 0;
}
#ifdef QAT_OPENSSL_PROVIDER
qctx = (QAT_GCM_CTX *)ctx;
qctx->iv = (Cpa8U *)qctx->iv;
#else
qctx = QAT_GCM_GET_CTX(ctx);
#endif
if (NULL == qctx) {
WARN("qctx is NULL\n");
QATerr(QAT_F_QAT_AES_GCM_CLEANUP, QAT_R_QCTX_NULL);
return 0;
}
/* Wait for in-flight requests before removing session */
if (qctx->qat_ctx != NULL) {
do {
cpaCySymSessionInUse(qctx->qat_ctx, &sessionInUse);
} while (sessionInUse);
}
session_data = qctx->session_data;
if (session_data) {
/* Remove the session */
if (qctx->qat_ctx) {
if (is_instance_available(qctx->inst_num)) {
sts = cpaCySymRemoveSession(qat_instance_handles[qctx->inst_num],
qctx->qat_ctx);
if (sts != CPA_STATUS_SUCCESS) {
WARN("cpaCySymRemoveSession FAILED, sts = %d.!\n", sts);
ret_val = 0;
/* Lets not return yet and instead make a best effort to
* cleanup the rest to avoid memory leaks
*/
}
}
/* Cleanup the memory */
QAT_MEM_FREE_NONZERO_BUFF(qctx->qat_ctx, qctx->qat_svm);
}
if (!qctx->qat_svm)
QAT_MEM_FREE_NONZERO_BUFF(qctx->aad, qctx->qat_svm);
QAT_MEM_FREE_NONZERO_BUFF(qctx->srcBufferList.pPrivateMetaData, qctx->qat_svm);
QAT_MEM_FREE_NONZERO_BUFF(qctx->dstBufferList.pPrivateMetaData, qctx->qat_svm);
QAT_MEM_FREE_NONZERO_BUFF(qctx->iv, qctx->qat_svm);
QAT_MEM_FREE_BUFF(qctx->cipher_key, qctx->qat_svm);
QAT_MEM_FREE_BUFF(qctx->OpData.pDigestResult, qctx->qat_svm);
#ifdef QAT_OPENSSL_PROVIDER
if (qctx->next_iv) {
OPENSSL_free(qctx->next_iv);
qctx->next_iv = NULL;
}
#endif
session_data->cipherSetupData.pCipherKey = NULL;
OPENSSL_clear_free(session_data, sizeof(CpaCySymSessionSetupData));
}
qctx->is_session_init = 0;
#ifdef ENABLE_QAT_FIPS
qat_fips_key_zeroize = 1;
qat_fips_get_key_zeroize_status();
#endif
#ifndef QAT_OPENSSL_PROVIDER
if (qctx->sw_ctx_cipher_data) {
OPENSSL_free(qctx->sw_ctx_cipher_data);
qctx->sw_ctx_cipher_data = NULL;
}
#else
if (qctx->sw_ctx) {
OPENSSL_free(qctx->sw_ctx);
qctx->sw_ctx = NULL;
}
#endif
return ret_val;
}
/******************************************************************************
* * function:
*
* static void qat_gcm_cb(void *pCallbackTag, CpaStatus status,
* const CpaCySymOp operationType,
* void *pOpData, CpaBufferList *pDstBuffer,
* CpaBoolean verifyResult)
*
* @param pCallbackTag [IN] - Opaque value provided by user while making
* individual function call. Cast to op_done_pipe_t.
* @param status [IN] - Status of the operation.
* @param operationType [IN] - Identifies the operation type requested.
* @param pOpData [IN] - Pointer to structure with input parameters.
* @param pDstBuffer [IN] - Destination buffer to hold the data output.
* @param verifyResult [IN] - Used to verify digest result.
*
* description:
Callback to indicate the completion of crypto operation
******************************************************************************/
static void qat_gcm_cb(void *pCallbackTag, CpaStatus status,
const CpaCySymOp operationType,
void *pOpData, CpaBufferList *pDstBuffer,
CpaBoolean verifyResult)
{
if (enable_heuristic_polling) {
QAT_ATOMIC_DEC(num_cipher_pipeline_requests_in_flight);
}
DEBUG("status is = %d | verifyResult is = %d | tag function called %p \n", status, verifyResult, (struct COMPLETION_STRUCT *)pCallbackTag);
qat_crypto_callbackFn(pCallbackTag, status, CPA_CY_SYM_OP_CIPHER, pOpData,
NULL, verifyResult);
}
/******************************************************************************
* function:
* qat_aes_gcm_session_init(EVP_CIPHER_CTX *ctx)
*
* @param ctx [IN] - pointer to context
*
* @retval 1 function succeeded
* @retval 0 function failed
*
* description:
* This function synchronises the initialisation of the QAT session and
* pre-allocates the necessary buffers for the session.
******************************************************************************/
#ifdef QAT_OPENSSL_PROVIDER
static int qat_aes_gcm_session_init(void *ctx, int *fallback)
#else
static int qat_aes_gcm_session_init(EVP_CIPHER_CTX *ctx, int *fallback)
#endif
{
#ifdef QAT_OPENSSL_PROVIDER
QAT_GCM_CTX* qctx = NULL;
#else
qat_gcm_ctx* qctx = NULL;
#endif
CpaCySymSessionSetupData *sessionSetupData = NULL;
Cpa32U sessionCtxSize = 0;
CpaCySymSessionCtx pSessionCtx = NULL;
int numBuffers = 1, enc = 0;
int sts = -1;
DEBUG("- Entering\n");
if (NULL == ctx) {
WARN("parameter ctx is NULL\n");
QATerr(QAT_F_QAT_AES_GCM_SESSION_INIT, QAT_R_CTX_NULL);
return 0;
}
#ifdef QAT_OPENSSL_PROVIDER
qctx = (QAT_GCM_CTX *)ctx;
qctx->iv = (Cpa8U *)qctx->iv;
enc = QAT_GCM_GET_ENC(qctx);
#else
qctx = QAT_GCM_GET_CTX(ctx);
enc = EVP_CIPHER_CTX_encrypting(ctx);
#endif
if (NULL == qctx) {
WARN("qctx is NULL\n");
QATerr(QAT_F_QAT_AES_GCM_SESSION_INIT, QAT_R_QCTX_NULL);
return 0;
}
/* All parameters have not been set yet or we have already been
* initialised. */
if ((1 != qctx->init_params_set) || (1 == qctx->is_session_init)) {
WARN("Parameters not set or session already initialised\n");
if (qat_get_sw_fallback_enabled()) {
*fallback = 1;
}
return 0;
}
sessionSetupData = qctx->session_data;
if (NULL == sessionSetupData) {
WARN("sessionSetupData is NULL\n");
QATerr(QAT_F_QAT_AES_GCM_SESSION_INIT, QAT_R_SSD_NULL);
return 0;
}
qctx->inst_num = get_instance(QAT_INSTANCE_SYM, QAT_INSTANCE_ANY);
if (qctx->inst_num == QAT_INVALID_INSTANCE) {
WARN("Failed to get a QAT instance.\n");
if (qat_get_sw_fallback_enabled()) {
CRYPTO_QAT_LOG("Failed to get an instance - fallback to SW - %s\n", __func__);
*fallback = 1;
}
return 0;
}
qctx->qat_svm = !qat_instance_details[qctx->inst_num].qat_instance_info.requiresPhysicallyContiguousMemory;
/* Update digestResultLenInBytes with qctx->tag_len if both lengths
are mismatch for decryption */
if (!enc) {
DEBUG("digestResultLenInBytes = %d, tag len = %d\n", sessionSetupData->hashSetupData.digestResultLenInBytes, qctx->tag_len);
if (!(qctx->tag_len < 0) && sessionSetupData->hashSetupData.digestResultLenInBytes != qctx->tag_len) {
sessionSetupData->hashSetupData.digestResultLenInBytes = qctx->tag_len;
DEBUG("Taglen updated\n");
}
}
sts = cpaCySymSessionCtxGetSize(qat_instance_handles[qctx->inst_num],
sessionSetupData,
&sessionCtxSize);
if (sts != CPA_STATUS_SUCCESS) {
WARN("Failed to get SessionCtx size.\n");
if (qat_get_sw_fallback_enabled()) {
*fallback = 1;
}
return 0;
}
pSessionCtx = (CpaCySymSessionCtx) qat_mem_alloc(sessionCtxSize, qctx->qat_svm,
__FILE__, __LINE__);
if (NULL == pSessionCtx) {
WARN("pSessionCtx malloc failed\n");
QATerr(QAT_F_QAT_AES_GCM_SESSION_INIT, ERR_R_INTERNAL_ERROR);
return 0;
}
DUMP_SESSION_SETUP_DATA(sessionSetupData);
sts = cpaCySymInitSession(qat_instance_handles[qctx->inst_num],
qat_gcm_cb,
sessionSetupData,
pSessionCtx);
if (sts == CPA_STATUS_SUCCESS) {
if (qat_get_sw_fallback_enabled()) {
CRYPTO_QAT_LOG("Submit success qat inst_num %d device_id %d - %s\n",
qctx->inst_num,
qat_instance_details[qctx->inst_num].qat_instance_info.physInstId.packageId,
__func__);
}
} else {
WARN("cpaCySymInitSession failed! Status = %d\n", sts);
QAT_MEM_FREE_BUFF(pSessionCtx, qctx->qat_svm);
if (qat_get_sw_fallback_enabled() &&
((sts == CPA_STATUS_RESTARTING) || (sts == CPA_STATUS_FAIL))) {
CRYPTO_QAT_LOG("Failed to submit request to qat inst_num %d device_id %d - fallback to SW - %s\n",
qctx->inst_num,
qat_instance_details[qctx->inst_num].qat_instance_info.physInstId.packageId,
__func__);
*fallback = 1;
return 0;
} else {
WARN("- No QAT instance available and s/w fallback not enabled.\n");
return 0;
}
}
qctx->qat_ctx = pSessionCtx;
/* Setup meta data for buffer lists */
sts = cpaCyBufferListGetMetaSize(qat_instance_handles[qctx->inst_num],
numBuffers,
&(qctx->meta_size));
if (sts != CPA_STATUS_SUCCESS) {
WARN("cpaCyBufferListGetBufferSize failed.\n");
QAT_MEM_FREE_BUFF(pSessionCtx, qctx->qat_svm);
if (qat_get_sw_fallback_enabled()) {
*fallback = 1;
return 0;
}
QATerr(QAT_F_QAT_AES_GCM_SESSION_INIT, ERR_R_INTERNAL_ERROR);
}
qctx->srcBufferList.numBuffers = numBuffers;
qctx->srcBufferList.pBuffers = &qctx->srcFlatBuffer;
qctx->srcBufferList.pUserData = NULL;
qctx->dstBufferList.numBuffers = numBuffers;
qctx->dstBufferList.pBuffers = &qctx->dstFlatBuffer;
qctx->dstBufferList.pUserData = NULL;
if (qctx->meta_size) {
qctx->srcBufferList.pPrivateMetaData =
qat_mem_alloc(qctx->meta_size, qctx->qat_svm, __FILE__, __LINE__);
if (NULL == qctx->srcBufferList.pPrivateMetaData) {
WARN("srcBufferList.pPrivateMetaData is NULL.\n");
QATerr(QAT_F_QAT_AES_GCM_SESSION_INIT, ERR_R_INTERNAL_ERROR);
QAT_MEM_FREE_NONZERO_BUFF(pSessionCtx, qctx->qat_svm);
return 0;
}
qctx->dstBufferList.pPrivateMetaData =
qat_mem_alloc(qctx->meta_size, qctx->qat_svm, __FILE__, __LINE__);
if (NULL == qctx->dstBufferList.pPrivateMetaData) {
WARN("dstBufferList.pPrivateMetaData is NULL.\n");
QATerr(QAT_F_QAT_AES_GCM_SESSION_INIT, ERR_R_INTERNAL_ERROR);
QAT_MEM_FREE_NONZERO_BUFF(qctx->srcBufferList.pPrivateMetaData, qctx->qat_svm);
qctx->srcBufferList.pPrivateMetaData = NULL;
QAT_MEM_FREE_NONZERO_BUFF(pSessionCtx, qctx->qat_svm);
return 0;
}
} else {
qctx->srcBufferList.pPrivateMetaData = NULL;
qctx->dstBufferList.pPrivateMetaData = NULL;
}
/* Create the OpData structure to remove this processing from the data
* path */
qctx->OpData.sessionCtx = qctx->qat_ctx;
qctx->OpData.packetType = CPA_CY_SYM_PACKET_TYPE_FULL;
/* Set the IV */
/* Set the AAD */
qctx->OpData.pAdditionalAuthData = qctx->aad;
DUMPL("qctx->OpData.pAdditionalAuthData",
qctx->OpData.pAdditionalAuthData,
qctx->session_data->hashSetupData.authModeSetupData.aadLenInBytes);
/* All the data in the buffer must be encrypted */
qctx->OpData.cryptoStartSrcOffsetInBytes = 0;
/* Following parameters are ignored in GCM */
qctx->OpData.messageLenToHashInBytes = 0;
qctx->OpData.hashStartSrcOffsetInBytes = 0;
qctx->OpData.pDigestResult = qat_mem_alloc(EVP_GCM_TLS_TAG_LEN, qctx->qat_svm,__FILE__, __LINE__);
if (qctx->OpData.pDigestResult == NULL) {
WARN("Unable to allocate memory for qctx->OpData.pDigestResult \n");
QATerr(QAT_F_QAT_AES_GCM_SESSION_INIT, QAT_R_KEY_MALLOC_FAILURE);
QAT_MEM_FREE_NONZERO_BUFF(qctx->srcBufferList.pPrivateMetaData, qctx->qat_svm);
QAT_MEM_FREE_NONZERO_BUFF(qctx->dstBufferList.pPrivateMetaData, qctx->qat_svm);
QAT_MEM_FREE_NONZERO_BUFF(pSessionCtx, qctx->qat_svm);
qctx->srcBufferList.pPrivateMetaData = NULL;
qctx->dstBufferList.pPrivateMetaData = NULL;
return 0;
}
qctx->is_session_init = 1;
return 1;
}
/******************************************************************************
* function:
* qat_aes_gcm_tls_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
* const unsigned char *in, size_t len)
*
* @param ctx [IN] - pointer to existing context
* @param out [OUT] - output buffer for transform result
* @param in [IN] - input buffer
* @param len [IN] - length of input buffer
*
* @retval 0 function failed
* @retval 1 function succeeded
*
* description:
* This function performs the cryptographic transform according to the
* parameters setup during initialisation.
*
* This is the function used in the TLS case.
*
******************************************************************************/
#ifdef QAT_OPENSSL_PROVIDER
int qat_aes_gcm_tls_cipher(void *ctx, unsigned char *out,
size_t *padlen, size_t outsize,
const unsigned char *in, size_t len)
#else
int qat_aes_gcm_tls_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
const unsigned char *in, size_t len)
#endif
{
#ifdef QAT_OPENSSL_PROVIDER
QAT_GCM_CTX *qctx = NULL;
int nid;
QAT_EVP_CIPHER sw_aes_gcm_cipher;
#else
qat_gcm_ctx *qctx = NULL;
#endif
CpaStatus sts = 0;
op_done_t op_done;
int ret_val = -1;
int job_ret = 0;
int enc = 0;
int fallback = 0;
unsigned int message_len = 0;
unsigned int buffer_len = 0;
thread_local_variables_t *tlv = NULL;
CRYPTO_QAT_LOG("CIPHER - %s\n", __func__);
/* Encrypt/decrypt must be performed in place */
if (NULL == in ||
out != in ||
len < (EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN)) {
WARN("Input parameters are not valid.\n");
QATerr(QAT_F_QAT_AES_GCM_TLS_CIPHER, QAT_R_INVALID_LEN);
return -1;
}
if (NULL == ctx) {
WARN("ctx is NULL\n");
QATerr(QAT_F_QAT_AES_GCM_TLS_CIPHER, QAT_R_CTX_NULL);
return -1;
}
#ifdef QAT_OPENSSL_PROVIDER
qctx = (QAT_GCM_CTX *)ctx;
qctx->iv = (Cpa8U *)qctx->iv;
nid = qat_aes_gcm_ctx_get_nid((QAT_AES_GCM_CTX *)ctx);
#else
qctx = QAT_GCM_GET_CTX(ctx);
#endif
if (NULL == qctx) {
WARN("qctx is NULL\n");
QATerr(QAT_F_QAT_AES_GCM_TLS_CIPHER, QAT_R_QCTX_NULL);
return -1 ;
}
#ifdef QAT_OPENSSL_PROVIDER
enc = QAT_GCM_GET_ENC(qctx);
#else
enc = EVP_CIPHER_CTX_encrypting(ctx);
#endif
DEBUG("enc = %d - ctx = %p, out = %p, in = %p, len = %zu\n",
enc, (void*)ctx, (void*)out, (void*)in, len);
/* The key has been set in the init function: no need to check it here*/
/* Initialize the session if not done before */
if (0 == qctx->is_session_init) {
#ifdef QAT_OPENSSL_PROVIDER
if (0 == qat_aes_gcm_session_init(qctx, &fallback)) {
#else
if (0 == qat_aes_gcm_session_init(ctx, &fallback)) {
#endif
WARN("Unable to initialise Cipher context.\n");
goto err;
}
}
/* Encryption: generate explicit IV and write to start of buffer.
* Decryption: read the explicit IV from start of buffer
*/
qctx->sw_tls_ctrl = TLS_CIPHER_SW_CTRL;
#ifdef QAT_OPENSSL_PROVIDER
if (qat_aes_gcm_ctrl(ctx, enc ? EVP_CTRL_GCM_IV_GEN : EVP_CTRL_GCM_SET_IV_INV,
EVP_GCM_TLS_EXPLICIT_IV_LEN, out) <= 0) {
WARN("vaesgcm_ciphers_ctrl Failed\n");
qctx->sw_tls_ctrl = 0;
goto err;
}
#else
if (EVP_CIPHER_CTX_ctrl(ctx, enc ?
EVP_CTRL_GCM_IV_GEN : EVP_CTRL_GCM_SET_IV_INV,
EVP_GCM_TLS_EXPLICIT_IV_LEN, out) <= 0) {
qctx->sw_tls_ctrl = 0;
goto err;
}
#endif
qctx->sw_tls_ctrl = 0;
DUMPL("Post ctrl IV: ", qctx->iv, qctx->iv_len);
DUMPL("Post ctrl next IV: ", qctx->next_iv, qctx->iv_len);
DUMPL("qctx->OpData.pAdditionalAuthData",
qctx->OpData.pAdditionalAuthData,
qctx->session_data->hashSetupData.authModeSetupData.aadLenInBytes);
/* Set the generated IV to OpData */
if (qctx->iv_set) {
qctx->OpData.pIv = qctx->iv;
qctx->OpData.ivLenInBytes = qctx->iv_len;
}
/* If key or IV not set, throw error here and return. */
if (!qctx->key_set || !qctx->iv_set) {
WARN("Cipher key or IV not set.\n");
if (qat_get_sw_fallback_enabled()) {
fallback = 1;
goto err;
}
QATerr(QAT_F_QAT_AES_GCM_TLS_CIPHER, QAT_R_KEY_IV_NOT_SET);
goto err;
}
/* Include the explicit part of the IV at the beginning of the output */
in += EVP_GCM_TLS_EXPLICIT_IV_LEN;
out += EVP_GCM_TLS_EXPLICIT_IV_LEN;
/* This is the length of the message that must be encrypted */
message_len = len - (EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN);
/* The buffer must have enough memory to save also the TAG */
buffer_len = message_len + EVP_GCM_TLS_TAG_LEN;
/* Build request/response buffers */
/* Allocate the memory of the FlatBuffer and copy the payload */
if (!qctx->qat_svm)
qctx->srcFlatBuffer.pData = qaeCryptoMemAlloc(buffer_len, __FILE__, __LINE__);
else
qctx->srcFlatBuffer.pData = (Cpa8U *)in;
if (NULL == qctx->srcFlatBuffer.pData) {
WARN("src/dst buffer allocation.\n");
goto err;
}
if (!qctx->qat_svm)
qctx->dstFlatBuffer.pData = qctx->srcFlatBuffer.pData;
else
qctx->dstFlatBuffer.pData = (Cpa8U *)out;
if (!enc) {
/* Decryption: the tag is appended and must be copied to the buffer */
if (!qctx->qat_svm)
memcpy(qctx->srcFlatBuffer.pData, in, message_len);
memcpy(qctx->OpData.pDigestResult, in + message_len, EVP_GCM_TLS_TAG_LEN);
qctx->tag_len = EVP_GCM_TLS_TAG_LEN;
} else {
/* Encryption: copy only the payload */
if (!qctx->qat_svm)
memcpy(qctx->srcFlatBuffer.pData, in, message_len);
}
/* The operation is done in place in the buffers
* The variables in and out remain separate */
qctx->srcFlatBuffer.dataLenInBytes = message_len;
qctx->srcBufferList.pUserData = NULL;
qctx->dstFlatBuffer.dataLenInBytes = message_len;
qctx->dstBufferList.pUserData = NULL;
qctx->OpData.messageLenToCipherInBytes = message_len;
tlv = qat_check_create_local_variables();
if (NULL == tlv) {
WARN("could not create local variables\n");
QATerr(QAT_F_QAT_AES_GCM_TLS_CIPHER, ERR_R_INTERNAL_ERROR);
goto err;
}
qat_init_op_done(&op_done);
if (op_done.job != NULL) {
if (qat_setup_async_event_notification(op_done.job) == 0) {
WARN("Failure to setup async event notifications\n");
QATerr(QAT_F_QAT_AES_GCM_TLS_CIPHER, ERR_R_INTERNAL_ERROR);
qat_cleanup_op_done(&op_done);
goto err;
}
}
CRYPTO_QAT_LOG("CIPHER - %s\n", __func__);
DUMP_SYM_PERFORM_OP_GCM_CCM(qat_instance_handles[qctx->inst_num],
qctx->OpData, qctx->srcBufferList,
qctx->dstBufferList);
sts = qat_sym_perform_op(qctx->inst_num,
&op_done,
&(qctx->OpData),
&(qctx->srcBufferList),
&(qctx->dstBufferList),
&(qctx->session_data->verifyDigest));
if (sts != CPA_STATUS_SUCCESS) {
if (!qctx->qat_svm) {
qaeCryptoMemFreeNonZero(qctx->srcFlatBuffer.pData);
qctx->srcFlatBuffer.pData = NULL;
qctx->dstFlatBuffer.pData = NULL;
}
qat_cleanup_op_done(&op_done);
WARN("cpaCySymPerformOp failed sts=%d.\n",sts);
if (qat_get_sw_fallback_enabled() &&
(sts == CPA_STATUS_RESTARTING || sts == CPA_STATUS_FAIL)) {
CRYPTO_QAT_LOG("Failed to submit request to qat inst_num %d device_id %d - fallback to SW - %s\n",
qctx->inst_num,
qat_instance_details[qctx->inst_num].qat_instance_info.physInstId.packageId,
__func__);
fallback = 1;
}
else if (sts == CPA_STATUS_UNSUPPORTED) {
WARN("Algorithm Unsupported in QAT_HW! Using OpenSSL SW\n");
fallback = 1;
} else {
QATerr(QAT_F_QAT_AES_GCM_TLS_CIPHER, ERR_R_INTERNAL_ERROR);
}
goto err;
}
QAT_INC_IN_FLIGHT_REQS(num_requests_in_flight, tlv);
if (qat_use_signals()) {
if (tlv->localOpsInFlight == 1) {
if (sem_post(&hw_polling_thread_sem) != 0) {
WARN("hw sem_post failed!, hw_polling_thread_sem address: %p.\n",
&hw_polling_thread_sem);
QATerr(QAT_F_QAT_AES_GCM_TLS_CIPHER, ERR_R_INTERNAL_ERROR);
QAT_DEC_IN_FLIGHT_REQS(num_requests_in_flight, tlv);
goto err;
}
}
}
if (enable_heuristic_polling) {
QAT_ATOMIC_INC(num_cipher_pipeline_requests_in_flight);
}
do {
if (op_done.job != NULL) {
/* If we get a failure on qat_pause_job then we will
not flag an error here and quit because we have
an asynchronous request in flight.
We don't want to start cleaning up data
structures that are still being used. If
qat_pause_job fails we will just yield and
loop around and try again until the request
completes and we can continue. */
if ((job_ret = qat_pause_job(op_done.job, ASYNC_STATUS_OK)) == 0)
sched_yield();
} else {
sched_yield();
}
} while (!op_done.flag ||
QAT_CHK_JOB_RESUMED_UNEXPECTEDLY(job_ret));
if (enc && CPA_STATUS_SUCCESS == op_done.status) {
#ifdef QAT_OPENSSL_PROVIDER
*padlen = len;
ret_val = 1;
#else
ret_val = len;
#endif
DEBUG("Encryption succeeded\n");
} else if (!enc && CPA_TRUE == op_done.verifyResult) {
#ifdef QAT_OPENSSL_PROVIDER
*padlen = message_len;
ret_val = 1;
#else
ret_val = message_len;
#endif
DEBUG("Decryption succeeded\n");
} else {
if (enc)
DEBUG("Encryption failed\n");
else
DEBUG("Decryption failed\n");
if (qat_get_sw_fallback_enabled()) {
CRYPTO_QAT_LOG("Verification of result failed for qat inst_num %d device_id %d - fallback to SW - %s\n",
qctx->inst_num,
qat_instance_details[qctx->inst_num].qat_instance_info.physInstId.packageId,
__func__);
fallback = 1; /* Probably already set anyway */
}
}
DUMP_SYM_PERFORM_OP_GCM_CCM_OUTPUT(qctx->dstBufferList);
QAT_DEC_IN_FLIGHT_REQS(num_requests_in_flight, tlv);
qat_cleanup_op_done(&op_done);
if (fallback)
goto err;
if (!qctx->qat_svm) {
memcpy(out, qctx->dstFlatBuffer.pData, message_len);
qaeCryptoMemFreeNonZero(qctx->srcFlatBuffer.pData);
qctx->srcFlatBuffer.pData = NULL;
qctx->dstFlatBuffer.pData = NULL;
}
memcpy(out + message_len, qctx->OpData.pDigestResult, EVP_GCM_TLS_TAG_LEN);
DUMPL("pDigestResult", qctx->OpData.pDigestResult, EVP_GCM_TLS_TAG_LEN);
err:
/* Don't reuse the IV */
qctx->iv_set = 0;
if (fallback) {
WARN("- Fallback to software mode.\n");
CRYPTO_QAT_LOG("Resubmitting request to SW - %s\n", __func__);
#ifndef QAT_OPENSSL_PROVIDER
EVP_CIPHER_CTX_set_cipher_data(ctx, qctx->sw_ctx_cipher_data);
ret_val = EVP_CIPHER_meth_get_do_cipher(GET_SW_AES_GCM_CIPHER(ctx))
(ctx, out, in, len);
EVP_CIPHER_CTX_set_cipher_data(ctx, qctx);
#else
sw_aes_gcm_cipher = get_default_cipher_aes_gcm(nid);
if (sw_aes_gcm_cipher.cupdate == NULL)
return 0;
if (in != NULL)
ret_val = sw_aes_gcm_cipher.cupdate(qctx->sw_ctx, out, padlen,
outsize, in, len);
else
ret_val =
sw_aes_gcm_cipher.cfinal(qctx->sw_ctx, out, padlen, outsize);
*padlen = len;
if (!ret_val)
return 0;
#endif
}
return ret_val;
}
/******************************************************************************
* function:
* qat_aes_gcm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
* size_t *padlen, const unsigned char *in,
* size_t len)
*
* @param ctx [IN] - pointer to existing context
* @param out [OUT] - output buffer for transform result
* @param in [IN] - input buffer
* @param len [IN] - length of input buffer
*
* @retval -1 function failed
* @retval len function succeeded
*
* description:
* This function performs the cryptographic transform according to the
* parameters setup during initialisation.
*
* This is the implementation of the Update case: the AAD is added by calling
* UpdateEncrypt() with out == NULL.
* This is the case used in the speed test.
*
* The flag EVP_CIPH_FLAG_CUSTOM_CIPHER is required to have the Update function
* working correctly when out == NULL, that is when setting the AAD.
* When this flag is enabled, the function must return -1 in case of failure and
* the length of the output (value >= 0) for success.
*
* QAT vs SW Engine
* ================
*
* In the Update use case, the behavior of the functions is different between
* the QAT and the SW engine.
*
* EVP Function SW Engine QAT Engine
* ----------------------------------------------------------------------------
* Encrypt Update Encrypt the payload Encrypt the payload AND
* compute the tag
*
* Encrypt Final Compute the tag Does nothing
*
* Decrypt Update Decrypt the payload Decrpyt the payload and
* verify the TAG. Return failure
* if the TAG is not correct
*
* Decrypt Final Verify the TAG and Does nothing
* return failure if not correct
*
* This doesn't impact the TLS case because Update and Final are considered
* a single operation like in the QAT engine.
*
******************************************************************************/
#ifdef QAT_OPENSSL_PROVIDER
int qat_aes_gcm_cipher(void *ctx, unsigned char *out,
size_t *padlen, size_t outsize,
const unsigned char *in, size_t len)
#else
int qat_aes_gcm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
const unsigned char *in, size_t len)
#endif
{
#ifdef QAT_OPENSSL_PROVIDER
QAT_GCM_CTX *qctx = NULL;
const int RET_SUCCESS = 1;
int nid;
QAT_EVP_CIPHER sw_aes_gcm_cipher;
#else
qat_gcm_ctx *qctx = NULL;
const int RET_SUCCESS = 0;
#endif
CpaStatus sts = 0;
op_done_t op_done;
const int RET_FAIL = -1;
int ret_val = RET_FAIL;
int job_ret = 0;
int enc = 0;
size_t aad_len = 0;
int aad_buffer_len = 0;
unsigned buffer_len = 0;
int fallback = 0;
thread_local_variables_t *tlv = NULL;
CRYPTO_QAT_LOG("CIPHER - %s\n", __func__);
if (NULL == ctx) {
WARN("ctx is NULL\n");
QATerr(QAT_F_QAT_AES_GCM_CIPHER, QAT_R_CTX_NULL);
return RET_FAIL;
}
#ifdef QAT_OPENSSL_PROVIDER
qctx = (QAT_GCM_CTX *)ctx;
qctx->iv = (Cpa8U *)qctx->iv;
nid = qat_aes_gcm_ctx_get_nid((QAT_AES_GCM_CTX *)ctx);
#else
qctx = QAT_GCM_GET_CTX(ctx);
#endif
if (NULL == qctx) {
WARN("qctx is NULL\n");
QATerr(QAT_F_QAT_AES_GCM_CIPHER, QAT_R_QCTX_NULL);
return RET_FAIL;
}
#ifdef ENABLE_QAT_FIPS
qat_fips_get_approved_status();
#endif
#ifdef QAT_OPENSSL_PROVIDER
enc = QAT_GCM_GET_ENC(qctx);
#else
enc = EVP_CIPHER_CTX_encrypting(ctx);
#endif
DEBUG("enc = %d - ctx = %p, out = %p, in = %p, len = %zu\n",
enc, (void*)ctx, (void*)out, (void*)in, len);
/* Distinguish the Update and TLS case */
if (qctx->tls_aad_len >= 0) {
#ifdef QAT_OPENSSL_PROVIDER
return qat_aes_gcm_tls_cipher(ctx, out, padlen, outsize, in, len);
#else
return qat_aes_gcm_tls_cipher(ctx, out, in, len);
#endif
}
/* If either key or IV not set, throw error here. */
if (!qctx->key_set || !qctx->iv_set) {
WARN("Cipher key or IV not set.\n");
if (qat_get_sw_fallback_enabled()) {
fallback = 1;
ret_val = RET_FAIL;
WARN("- Fallback to software mode.\n");
CRYPTO_QAT_LOG("Resubmitting request to SW - %s\n", __func__);
goto err;
}
QATerr(QAT_F_QAT_AES_GCM_CIPHER, QAT_R_KEY_IV_NOT_SET);
return RET_FAIL;
}
if (in) {
/* This is called when doing Update */
if (NULL == out) {
DEBUG("in != NULL && out == NULL -> Adding AAD\n");
aad_len = len;
/* Check if the length of the AAD has changed */
if (qctx->session_data->hashSetupData.authModeSetupData.aadLenInBytes != aad_len) {
/* Free the memory used for the previous AAD */
if (qctx->aad) {
if (!qctx->qat_svm)
qaeCryptoMemFreeNonZero(qctx->aad);
qctx->aad = NULL;
}
/* For QAT the length of the buffer for AAD must be multiple of block size */
aad_buffer_len = aad_len;
if (aad_buffer_len % AES_BLOCK_SIZE) {
aad_buffer_len += AES_BLOCK_SIZE - (aad_buffer_len % AES_BLOCK_SIZE);
DEBUG("Adjusting AAD buffer length = %d\n", aad_buffer_len);
}
if (!qctx->qat_svm) {
qctx->aad = qaeCryptoMemAlloc(aad_buffer_len, __FILE__, __LINE__);
if (NULL == qctx->aad) {
WARN("Unable to allocate memory for AAD\n");
QATerr(QAT_F_QAT_AES_GCM_CIPHER, ERR_R_INTERNAL_ERROR);
goto err;
}
}
/* Set the length of the AAD */
qctx->session_data->hashSetupData.authModeSetupData.aadLenInBytes = aad_len;
}
#ifdef QAT_OPENSSL_PROVIDER
*padlen = aad_len;
#endif
if (!qctx->qat_svm)
memcpy(qctx->aad, in, aad_len);
else
qctx->aad = (Cpa8U *) in;
DUMPL("qctx->aad", qctx->aad, aad_len);
/* The pAdditionalAuthData will be initialized firstly in
* qat_aes_gcm_session_init(), but the AAD can be updated
* for several times. So it is very important to update the
* AAD pointer in qat OpData structure in time. */
qctx->OpData.pAdditionalAuthData = qctx->aad;
/* In this case no data is actually encrypted/decrypted.
* The return value follows the standard rule of OpenSSL: success -> 1
*/
if (qat_get_sw_fallback_enabled()) {
fallback = 1;
ret_val = 1;
goto err;
}
return 1;
} else {
/* The key has been set in the init function: no need to check it */
if (0 == qctx->is_session_init) {
#ifdef QAT_OPENSSL_PROVIDER
if (0 == qat_aes_gcm_session_init(qctx, &fallback)) {
#else
if (0 == qat_aes_gcm_session_init(ctx, &fallback)) {
#endif
WARN("Unable to initialise Cipher context.\n");
if (fallback) {
WARN("- Fallback to software mode.\n");
CRYPTO_QAT_LOG("Resubmitting request to SW - %s\n", __func__);
goto err;
}
return RET_FAIL;
}
}
/* Update buffer length as tag length if input buffer
length is zero */
if (len == 0)
buffer_len = EVP_GCM_TLS_TAG_LEN;
else
buffer_len = len;
/* Build request/response buffers */
/* Allocate the memory of the FlatBuffer and copy the payload */
if (!qctx->qat_svm)
qctx->srcFlatBuffer.pData = qaeCryptoMemAlloc(buffer_len, __FILE__, __LINE__);
else
qctx->srcFlatBuffer.pData = (Cpa8U *)in;
if (NULL == qctx->srcFlatBuffer.pData) {
WARN("src/dst buffer allocation.\n");
QATerr(QAT_F_QAT_AES_GCM_CIPHER, ERR_R_INTERNAL_ERROR);
return RET_FAIL;
}
if (!qctx->qat_svm) {
qctx->dstFlatBuffer.pData = qctx->srcFlatBuffer.pData;
memcpy(qctx->srcFlatBuffer.pData, in, len);
} else {
qctx->dstFlatBuffer.pData = (Cpa8U *)out;
}
/* The operation is done in place in the buffers
* The variables in and out remain separate */
qctx->srcFlatBuffer.dataLenInBytes = buffer_len;
qctx->srcBufferList.pUserData = NULL;
qctx->dstFlatBuffer.dataLenInBytes = buffer_len;
qctx->dstBufferList.pUserData = NULL;
qctx->OpData.messageLenToCipherInBytes = len;
/* Decryption: set the digest (tag) for verification
* This is different from SW implementation. Here we have a single
* function to decrypt AND verify
*/
if (!enc) {
/* Copy EVP_GCM_TLS_TAG_LEN bytes from tag buffer
as the maximum tag length can only be
EVP_GCM_TLS_TAG_LEN */
#ifdef QAT_OPENSSL_PROVIDER
memcpy(qctx->OpData.pDigestResult, qctx->buf,
EVP_GCM_TLS_TAG_LEN);
#else
if (NULL == EVP_CIPHER_CTX_buf_noconst(ctx)) {
WARN("Tag not set\n");
if (!qctx->qat_svm) {
qaeCryptoMemFreeNonZero(qctx->srcFlatBuffer.pData);
qctx->srcFlatBuffer.pData = NULL;
qctx->dstFlatBuffer.pData = NULL;
}
return RET_FAIL;
} else {
memcpy(qctx->OpData.pDigestResult, EVP_CIPHER_CTX_buf_noconst(ctx),
EVP_GCM_TLS_TAG_LEN);
}
#endif
}
tlv = qat_check_create_local_variables();
if (NULL == tlv) {
WARN("could not create local variables\n");
QATerr(QAT_F_QAT_AES_GCM_CIPHER, ERR_R_INTERNAL_ERROR);
return RET_FAIL;
}
qat_init_op_done(&op_done);
if (op_done.job != NULL) {
if (qat_setup_async_event_notification(op_done.job) == 0) {
WARN("Failure to setup async event notifications\n");
qat_cleanup_op_done(&op_done);
QATerr(QAT_F_QAT_AES_GCM_CIPHER, ERR_R_INTERNAL_ERROR);
return RET_FAIL;
}
}
DUMP_SYM_PERFORM_OP_GCM_CCM(qat_instance_handles[qctx->inst_num],
qctx->OpData, qctx->srcBufferList,
qctx->dstBufferList);
DUMPL("AAD: ", qctx->OpData.pAdditionalAuthData,
qctx->session_data->hashSetupData.authModeSetupData.aadLenInBytes);
sts = qat_sym_perform_op(qctx->inst_num,
&op_done,
&(qctx->OpData),
&(qctx->srcBufferList),
&(qctx->dstBufferList),
&(qctx->session_data->verifyDigest));
if (sts != CPA_STATUS_SUCCESS) {
if (!qctx->qat_svm) {
qaeCryptoMemFreeNonZero(qctx->srcFlatBuffer.pData);
qctx->srcFlatBuffer.pData = NULL;
qctx->dstFlatBuffer.pData = NULL;
}
qat_cleanup_op_done(&op_done);
WARN("cpaCySymPerformOp failed sts=%d.\n",sts);
if (qat_get_sw_fallback_enabled() &&
(sts == CPA_STATUS_RESTARTING || sts == CPA_STATUS_FAIL)) {
CRYPTO_QAT_LOG("Failed to submit request to qat inst_num %d device_id %d - fallback to SW - %s\n",
qctx->inst_num,
qat_instance_details[qctx->inst_num].qat_instance_info.physInstId.packageId,
__func__);
fallback = 1;
WARN("- Fallback to software mode.\n");
CRYPTO_QAT_LOG("Resubmitting request to SW - %s\n", __func__);
}
else if (sts == CPA_STATUS_UNSUPPORTED) {
WARN("Algorithm Unsupported in QAT_HW! Using OpenSSL SW\n");
fallback = 1;
CRYPTO_QAT_LOG("Resubmitting request to SW - %s\n", __func__);
} else {
QATerr(QAT_F_QAT_AES_GCM_CIPHER, ERR_R_INTERNAL_ERROR);
}
goto err;
}
QAT_INC_IN_FLIGHT_REQS(num_requests_in_flight, tlv);
if (qat_use_signals()) {
if (tlv->localOpsInFlight == 1) {
if (sem_post(&hw_polling_thread_sem) != 0) {
WARN("hw sem_post failed!, hw_polling_thread_sem address: %p.\n",
&hw_polling_thread_sem);
QATerr(QAT_F_QAT_AES_GCM_CIPHER, ERR_R_INTERNAL_ERROR);
QAT_DEC_IN_FLIGHT_REQS(num_requests_in_flight, tlv);
goto err;
}
}
}
if (enable_heuristic_polling) {
QAT_ATOMIC_INC(num_cipher_pipeline_requests_in_flight);
}
do {
if (op_done.job != NULL) {
/* If we get a failure on qat_pause_job then we will
not flag an error here and quit because we have
an asynchronous request in flight.
We don't want to start cleaning up data
structures that are still being used. If
qat_pause_job fails we will just yield and
loop around and try again until the request
completes and we can continue. */
if ((job_ret = qat_pause_job(op_done.job, 0)) == 0)
sched_yield();
} else {
sched_yield();
}
} while (!op_done.flag ||
QAT_CHK_JOB_RESUMED_UNEXPECTEDLY(job_ret));
DUMP_SYM_PERFORM_OP_GCM_CCM_OUTPUT(qctx->dstBufferList);
if (enc && op_done.status == CPA_STATUS_SUCCESS) {
ret_val = len;
DEBUG("Encryption succeeded\n");
} else if (!enc && (CPA_TRUE == op_done.verifyResult)) {
ret_val = len;
DEBUG("Decryption succeeded\n");
} else {
if (enc)
DEBUG("Encryption failed\n");
else
DEBUG("Decryption failed\n");
if (qat_get_sw_fallback_enabled()) {
CRYPTO_QAT_LOG(
"Verification of result failed for qat inst_num %d device_id %d - fallback to SW - %s\n",
qctx->inst_num,
qat_instance_details[qctx->inst_num].qat_instance_info.physInstId.packageId,
__func__);
fallback = 1; /* Probably already set anyway */
WARN("- Fallback to software mode.\n");
CRYPTO_QAT_LOG("Resubmitting request to SW - %s\n", __func__);
}
}
QAT_DEC_IN_FLIGHT_REQS(num_requests_in_flight, tlv);
qat_cleanup_op_done(&op_done);
if (fallback)
goto err;
if (enc) {
/* After encryption, copy the TAG from the buffer to the ctx */
#ifdef QAT_OPENSSL_PROVIDER
memcpy(qctx->buf, qctx->OpData.pDigestResult,
EVP_GCM_TLS_TAG_LEN);
DUMPL("TAG calculated by QAT", qctx->buf, 16);
#else
memcpy(EVP_CIPHER_CTX_buf_noconst(ctx), qctx->OpData.pDigestResult,
EVP_GCM_TLS_TAG_LEN);
DUMPL("TAG calculated by QAT",
EVP_CIPHER_CTX_buf_noconst(ctx), 16);
#endif
qctx->tag_len = EVP_GCM_TLS_TAG_LEN;
qctx->tag_set = 1;
}
if (!qctx->qat_svm) {
memcpy(out, qctx->dstFlatBuffer.pData, len);
qaeCryptoMemFreeNonZero(qctx->srcFlatBuffer.pData);
qctx->srcFlatBuffer.pData = NULL;
qctx->dstFlatBuffer.pData = NULL;
}
#ifdef QAT_OPENSSL_PROVIDER
*padlen = len;
#endif
return ret_val;
}
} else {
/* This is executed when Final is called */
if (!enc) {
# if OPENSSL_VERSION_NUMBER < 0x30200000
if (qctx->tag_len < 0) {
return RET_FAIL;
}
# endif
/* Don't reuse the IV */
qctx->iv_set = 0;
if (qat_get_sw_fallback_enabled() && !qctx->tag_set) {
fallback = 1;
}
DEBUG("Decrypt Final()\n");
/* The SW implem here compares the TAGs and returns -1 if they are different.
* Now the TAGs are checked when decrypting the payload so Final always return success
*/
ret_val = RET_SUCCESS;
goto err;
}
DEBUG("Encrypt Final()\n");
/* The SW implem here copy the TAG to ctx->buf so that it can be
* retrieved using ctrl() with GET_TAG.
* Now the TAG is appended and has already been copied to that location
* hence we do nothing.
*/
/* Don't reuse the IV */
qctx->iv_set = 0;
if (qat_get_sw_fallback_enabled() && !qctx->tag_set)
fallback = 1;
ret_val = RET_SUCCESS;
}
err:
if (fallback) {
#ifndef QAT_OPENSSL_PROVIDER
EVP_CIPHER_CTX_set_cipher_data(ctx, qctx->sw_ctx_cipher_data);
ret_val = EVP_CIPHER_meth_get_do_cipher(GET_SW_AES_GCM_CIPHER(ctx))
(ctx, out, in, len);
EVP_CIPHER_CTX_set_cipher_data(ctx, qctx);
#else
sw_aes_gcm_cipher = get_default_cipher_aes_gcm(nid);
if (sw_aes_gcm_cipher.cupdate == NULL)
return 0;
if (in != NULL)
ret_val = sw_aes_gcm_cipher.cupdate(qctx->sw_ctx, out, padlen,
outsize, in, len);
else
ret_val =
sw_aes_gcm_cipher.cfinal(qctx->sw_ctx, out, padlen, outsize);
*padlen = len;
if (!ret_val)
return RET_FAIL;
#endif
}
return ret_val;
}
#endif
|