Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 | /**
******************************************************************************
* @file stm32l4xx_hal_cryp_ex.c
* @author MCD Application Team
* @brief CRYPEx HAL module driver.
* This file provides firmware functions to manage the extended
* functionalities of the Cryptography (CRYP) peripheral.
*
******************************************************************************
* @attention
*
* <h2><center>© COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. 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.
* 3. Neither the name of STMicroelectronics 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 HOLDER 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.
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "stm32l4xx_hal.h"
#ifdef HAL_CRYP_MODULE_ENABLED
#if defined(AES)
/** @addtogroup STM32L4xx_HAL_Driver
* @{
*/
/** @defgroup CRYPEx CRYPEx
* @brief CRYP Extended HAL module driver
* @{
*/
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/** @defgroup CRYPEx_Private_Constants CRYPEx Private Constants
* @{
*/
#define CRYP_CCF_TIMEOUTVALUE 22000 /*!< CCF flag raising time-out value */
#define CRYP_BUSY_TIMEOUTVALUE 22000 /*!< BUSY flag reset time-out value */
#define CRYP_POLLING_OFF 0x0 /*!< No polling when padding */
#define CRYP_POLLING_ON 0x1 /*!< Polling when padding */
#if defined(AES_CR_NPBLB)
#define AES_POSITION_CR_NPBLB (uint32_t)POSITION_VAL(AES_CR_NPBLB) /*!< Required left shift to set background CLUT size */
#endif
/**
* @}
*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/** @defgroup CRYPEx_Private_Functions CRYPEx Private Functions
* @{
*/
static HAL_StatusTypeDef CRYP_ProcessData(CRYP_HandleTypeDef *hcryp, uint8_t* Input, uint16_t Ilength, uint8_t* Output, uint32_t Timeout);
static HAL_StatusTypeDef CRYP_ReadKey(CRYP_HandleTypeDef *hcryp, uint8_t* Output, uint32_t Timeout);
static void CRYP_SetDMAConfig(CRYP_HandleTypeDef *hcryp, uint32_t inputaddr, uint16_t Size, uint32_t outputaddr);
static void CRYP_Authentication_SetDMAConfig(CRYP_HandleTypeDef *hcryp, uint32_t inputaddr, uint16_t Size, uint32_t outputaddr);
static void CRYP_Authentication_DMAInCplt(DMA_HandleTypeDef *hdma);
static void CRYP_Authentication_DMAError(DMA_HandleTypeDef *hdma);
static void CRYP_Authentication_DMAOutCplt(DMA_HandleTypeDef *hdma);
static HAL_StatusTypeDef CRYP_WaitOnCCFlag(CRYP_HandleTypeDef const * const hcryp, uint32_t Timeout);
static HAL_StatusTypeDef CRYP_WaitOnBusyFlagReset(CRYP_HandleTypeDef const * const hcryp, uint32_t Timeout);
static void CRYP_DMAInCplt(DMA_HandleTypeDef *hdma);
static void CRYP_DMAOutCplt(DMA_HandleTypeDef *hdma);
static void CRYP_DMAError(DMA_HandleTypeDef *hdma);
static void CRYP_Padding(CRYP_HandleTypeDef *hcryp, uint32_t difflength, uint32_t polling);
/**
* @}
*/
/* Exported functions ---------------------------------------------------------*/
/** @defgroup CRYPEx_Exported_Functions CRYPEx Exported Functions
* @{
*/
/** @defgroup CRYPEx_Exported_Functions_Group1 Extended callback function
* @brief Extended callback functions.
*
@verbatim
===============================================================================
##### Extended callback functions #####
===============================================================================
[..] This section provides callback function:
(+) Computation completed.
@endverbatim
* @{
*/
/**
* @brief Computation completed callbacks.
* @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
* the configuration information for CRYP module
* @retval None
*/
__weak void HAL_CRYPEx_ComputationCpltCallback(CRYP_HandleTypeDef *hcryp)
{
/* Prevent unused argument(s) compilation warning */
UNUSED(hcryp);
/* NOTE : This function should not be modified; when the callback is needed,
the HAL_CRYPEx_ComputationCpltCallback can be implemented in the user file
*/
}
/**
* @}
*/
/** @defgroup CRYPEx_Exported_Functions_Group2 AES extended processing functions
* @brief Extended processing functions.
*
@verbatim
==============================================================================
##### AES extended processing functions #####
==============================================================================
[..] This section provides functions allowing to:
(+) Encrypt plaintext or decrypt cipher text using AES algorithm in different chaining modes.
Functions are generic (handles ECB, CBC and CTR and all modes) and are only differentiated
based on the processing type. Three processing types are available:
(++) Polling mode
(++) Interrupt mode
(++) DMA mode
(+) Generate and authentication tag in addition to encrypt/decrypt a plain/cipher text using AES
algorithm in different chaining modes.
Functions are generic (handles GCM, GMAC, CMAC and CCM when applicable) and process only one phase
so that steps can be skipped if so required. Functions are only differentiated based on the processing type.
Three processing types are available:
(++) Polling mode
(++) Interrupt mode
(++) DMA mode
@endverbatim
* @{
*/
/**
* @brief Carry out in polling mode the ciphering or deciphering operation according to
* hcryp->Init structure fields, all operating modes (encryption, key derivation and/or decryption) and
* chaining modes ECB, CBC and CTR are managed by this function in polling mode.
* @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
* the configuration information for CRYP module
* @param pInputData: Pointer to the plain text in case of encryption or cipher text in case of decryption
* or key derivation+decryption.
* Parameter is meaningless in case of key derivation.
* @param Size: Length of the input data buffer in bytes, must be a multiple of 16.
* Parameter is meaningless in case of key derivation.
* @param pOutputData: Pointer to the cipher text in case of encryption or plain text in case of
* decryption/key derivation+decryption, or pointer to the derivative keys in
* case of key derivation only.
* @param Timeout: Specify Timeout value
* @retval HAL status
*/
HAL_StatusTypeDef HAL_CRYPEx_AES(CRYP_HandleTypeDef *hcryp, uint8_t *pInputData, uint16_t Size, uint8_t *pOutputData, uint32_t Timeout)
{
if (hcryp->State == HAL_CRYP_STATE_READY)
{
/* Check parameters setting */
if (hcryp->Init.OperatingMode == CRYP_ALGOMODE_KEYDERIVATION)
{
if (pOutputData == NULL)
{
return HAL_ERROR;
}
}
else
{
if ((pInputData == NULL) || (pOutputData == NULL) || (Size == 0U))
{
return HAL_ERROR;
}
}
/* Process Locked */
__HAL_LOCK(hcryp);
/* Change the CRYP state */
hcryp->State = HAL_CRYP_STATE_BUSY;
/* Call CRYP_ReadKey() API if the operating mode is set to
key derivation, CRYP_ProcessData() otherwise */
if (hcryp->Init.OperatingMode == CRYP_ALGOMODE_KEYDERIVATION)
{
if(CRYP_ReadKey(hcryp, pOutputData, Timeout) != HAL_OK)
{
return HAL_TIMEOUT;
}
}
else
{
if(CRYP_ProcessData(hcryp, pInputData, Size, pOutputData, Timeout) != HAL_OK)
{
return HAL_TIMEOUT;
}
}
/* If the state has not been set to SUSPENDED, set it to
READY, otherwise keep it as it is */
if (hcryp->State != HAL_CRYP_STATE_SUSPENDED)
{
hcryp->State = HAL_CRYP_STATE_READY;
}
/* Process Unlocked */
__HAL_UNLOCK(hcryp);
return HAL_OK;
}
else
{
return HAL_BUSY;
}
}
/**
* @brief Carry out in interrupt mode the ciphering or deciphering operation according to
* hcryp->Init structure fields, all operating modes (encryption, key derivation and/or decryption) and
* chaining modes ECB, CBC and CTR are managed by this function in interrupt mode.
* @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
* the configuration information for CRYP module
* @param pInputData: Pointer to the plain text in case of encryption or cipher text in case of decryption
* or key derivation+decryption.
* Parameter is meaningless in case of key derivation.
* @param Size: Length of the input data buffer in bytes, must be a multiple of 16.
* Parameter is meaningless in case of key derivation.
* @param pOutputData: Pointer to the cipher text in case of encryption or plain text in case of
* decryption/key derivation+decryption, or pointer to the derivative keys in
* case of key derivation only.
* @retval HAL status
*/
HAL_StatusTypeDef HAL_CRYPEx_AES_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pInputData, uint16_t Size, uint8_t *pOutputData)
{
uint32_t inputaddr;
if(hcryp->State == HAL_CRYP_STATE_READY)
{
/* Check parameters setting */
if (hcryp->Init.OperatingMode == CRYP_ALGOMODE_KEYDERIVATION)
{
if (pOutputData == NULL)
{
return HAL_ERROR;
}
}
else
{
if ((pInputData == NULL) || (pOutputData == NULL) || (Size == 0U))
{
return HAL_ERROR;
}
}
/* Process Locked */
__HAL_LOCK(hcryp);
/* If operating mode is not limited to key derivation only,
get the buffers addresses and sizes */
if (hcryp->Init.OperatingMode != CRYP_ALGOMODE_KEYDERIVATION)
{
hcryp->CrypInCount = Size;
hcryp->pCrypInBuffPtr = pInputData;
hcryp->pCrypOutBuffPtr = pOutputData;
hcryp->CrypOutCount = Size;
}
else
{
/* For key derivation, set output buffer only
(will point at derivated key) */
hcryp->pCrypOutBuffPtr = pOutputData;
}
/* Change the CRYP state */
hcryp->State = HAL_CRYP_STATE_BUSY;
/* Process Unlocked */
__HAL_UNLOCK(hcryp);
/* Enable Computation Complete Flag and Error Interrupts */
__HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_CCFIE|CRYP_IT_ERRIE);
/* If operating mode is key derivation only, the input data have
already been entered during the initialization process. For
the other operating modes, they are fed to the CRYP hardware
block at this point. */
if (hcryp->Init.OperatingMode != CRYP_ALGOMODE_KEYDERIVATION)
{
/* Initiate the processing under interrupt in entering
the first input data */
inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
/* Increment/decrement instance pointer/counter */
hcryp->pCrypInBuffPtr += 16;
hcryp->CrypInCount -= 16U;
/* Write the first input block in the Data Input register */
hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
inputaddr+=4U;
hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
inputaddr+=4U;
hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
inputaddr+=4U;
hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
}
/* Return function status */
return HAL_OK;
}
else
{
return HAL_BUSY;
}
}
/**
* @brief Carry out in DMA mode the ciphering or deciphering operation according to
* hcryp->Init structure fields.
* @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
* the configuration information for CRYP module
* @param pInputData: Pointer to the plain text in case of encryption or cipher text in case of decryption
* or key derivation+decryption.
* @param Size: Length of the input data buffer in bytes, must be a multiple of 16.
* @param pOutputData: Pointer to the cipher text in case of encryption or plain text in case of
* decryption/key derivation+decryption.
* @note Chaining modes ECB, CBC and CTR are managed by this function in DMA mode.
* @note Supported operating modes are encryption, decryption and key derivation with decryption.
* @note No DMA channel is provided for key derivation only and therefore, access to AES_KEYRx
* registers must be done by software.
* @note This API is not applicable to key derivation only; for such a mode, access to AES_KEYRx
* registers must be done by software thru HAL_CRYPEx_AES() or HAL_CRYPEx_AES_IT() APIs.
* @note pInputData and pOutputData buffers must be 32-bit aligned to ensure a correct DMA transfer to and from the IP.
* @retval HAL status
*/
HAL_StatusTypeDef HAL_CRYPEx_AES_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pInputData, uint16_t Size, uint8_t *pOutputData)
{
uint32_t inputaddr;
uint32_t outputaddr;
if (hcryp->State == HAL_CRYP_STATE_READY)
{
/* Check parameters setting */
if (hcryp->Init.OperatingMode == CRYP_ALGOMODE_KEYDERIVATION)
{
/* no DMA channel is provided for key derivation operating mode,
access to AES_KEYRx registers must be done by software */
return HAL_ERROR;
}
else
{
if ((pInputData == NULL) || (pOutputData == NULL) || (Size == 0U))
{
return HAL_ERROR;
}
}
/* Process Locked */
__HAL_LOCK(hcryp);
inputaddr = (uint32_t)pInputData;
outputaddr = (uint32_t)pOutputData;
/* Change the CRYP state */
hcryp->State = HAL_CRYP_STATE_BUSY;
/* Set the input and output addresses and start DMA transfer */
CRYP_SetDMAConfig(hcryp, inputaddr, Size, outputaddr);
/* Process Unlocked */
__HAL_UNLOCK(hcryp);
/* Return function status */
return HAL_OK;
}
else
{
return HAL_BUSY;
}
}
/**
* @brief Carry out in polling mode the authentication tag generation as well as the ciphering or deciphering
* operation according to hcryp->Init structure fields.
* @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
* the configuration information for CRYP module
* @param pInputData:
* - pointer to payload data in GCM or CCM payload phase,
* - pointer to B0 block in CMAC header phase,
* - pointer to C block in CMAC final phase.
* - Parameter is meaningless in case of GCM/GMAC/CCM init, header and final phases.
* @param Size:
* - length of the input payload data buffer in bytes in GCM or CCM payload phase,
* - length of B0 block (in bytes) in CMAC header phase,
* - length of C block (in bytes) in CMAC final phase.
* - Parameter is meaningless in case of GCM/GMAC/CCM init and header phases.
* - Parameter is meaningless in case of CCM final phase.
* - Parameter is message length in bytes in case of GCM final phase.
* - Parameter must be set to zero in case of GMAC final phase.
* @param pOutputData:
* - pointer to plain or cipher text in GCM/CCM payload phase,
* - pointer to authentication tag in GCM/GMAC/CCM/CMAC final phase.
* - Parameter is meaningless in case of GCM/GMAC/CCM init and header phases.
* - Parameter is meaningless in case of CMAC header phase.
* @param Timeout: Specify Timeout value
* @note Supported operating modes are encryption and decryption, supported chaining modes are GCM, GMAC, CMAC and CCM when the latter is applicable.
* @note Phases are singly processed according to hcryp->Init.GCMCMACPhase so that steps in these specific chaining modes
* can be skipped by the user if so required.
* @retval HAL status
*/
HAL_StatusTypeDef HAL_CRYPEx_AES_Auth(CRYP_HandleTypeDef *hcryp, uint8_t *pInputData, uint64_t Size, uint8_t *pOutputData, uint32_t Timeout)
{
uint32_t index ;
uint32_t inputaddr ;
uint32_t outputaddr ;
uint32_t tagaddr ;
uint64_t headerlength ;
uint64_t inputlength ;
uint64_t payloadlength ;
uint32_t difflength = 0;
uint32_t addhoc_process = 0;
if (hcryp->State == HAL_CRYP_STATE_READY)
{
/* input/output parameters check */
if (hcryp->Init.GCMCMACPhase == CRYP_INIT_PHASE)
{
/* No processing required */
}
else if (hcryp->Init.GCMCMACPhase == CRYP_HEADER_PHASE)
{
if (((hcryp->Init.Header != NULL) && (hcryp->Init.HeaderSize == 0U)) ||
((hcryp->Init.Header == NULL) && (hcryp->Init.HeaderSize != 0U)))
{
return HAL_ERROR;
}
#if defined(AES_CR_NPBLB)
if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CCM)
#else
if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC)
#endif
{
/* In case of CMAC or CCM (when applicable) header phase resumption, we can have pInputData = NULL and Size = 0 */
if (((pInputData != NULL) && (Size == 0U)) || ((pInputData == NULL) && (Size != 0U)))
{
return HAL_ERROR;
}
}
}
else if (hcryp->Init.GCMCMACPhase == CRYP_PAYLOAD_PHASE)
{
if (((pInputData == NULL) && (Size != 0U)) || \
((pInputData != NULL) && (Size == 0U)) || \
((pInputData != NULL) && (Size != 0U) && (pOutputData == NULL)))
{
return HAL_ERROR;
}
}
else if (hcryp->Init.GCMCMACPhase == CRYP_FINAL_PHASE)
{
if (pOutputData == NULL)
{
return HAL_ERROR;
}
#if !defined(AES_CR_NPBLB)
if ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC) && (pInputData == NULL))
{
return HAL_ERROR;
}
#endif
}
else
{
/* Unspecified Phase */
return HAL_ERROR;
}
/* Process Locked */
__HAL_LOCK(hcryp);
/* Change the CRYP state */
hcryp->State = HAL_CRYP_STATE_BUSY;
/*==============================================*/
/* GCM/GMAC (or CCM when applicable) init phase */
/*==============================================*/
/* In case of init phase, the input data (Key and Initialization Vector) have
already been entered during the initialization process. Therefore, the
API just waits for the CCF flag to be set. */
if (hcryp->Init.GCMCMACPhase == CRYP_INIT_PHASE)
{
/* just wait for hash computation */
if(CRYP_WaitOnCCFlag(hcryp, Timeout) != HAL_OK)
{
hcryp->State = HAL_CRYP_STATE_READY;
__HAL_UNLOCK(hcryp);
return HAL_TIMEOUT;
}
/* Clear CCF Flag */
__HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
/* Mark that the initialization phase is over */
hcryp->Phase = HAL_CRYP_PHASE_INIT_OVER;
}
/*=======================================================*/
/* GCM/GMAC or (CCM / CMAC when applicable) header phase */
/*=======================================================*/
else if (hcryp->Init.GCMCMACPhase == CRYP_HEADER_PHASE)
{
#if !defined(AES_CR_NPBLB)
/* Set header phase; for GCM or GMAC, set data-byte at this point */
if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC)
{
MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH|AES_CR_DATATYPE, CRYP_HEADER_PHASE|hcryp->Init.DataType);
}
else
#endif
{
MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, CRYP_HEADER_PHASE);
}
/* Enable the Peripheral */
__HAL_CRYP_ENABLE(hcryp);
#if !defined(AES_CR_NPBLB)
/* in case of CMAC, enter B0 block in header phase, before the header itself. */
/* If Size = 0 (possible case of resumption after CMAC header phase suspension),
skip these steps and go directly to header buffer feeding to the HW */
if ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC) && (Size != 0U))
{
uint64_t index_test;
inputaddr = (uint32_t)pInputData;
for(index=0U ; (index < Size); index += 16U)
{
/* Write the Input block in the Data Input register */
hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
inputaddr+=4U;
hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
inputaddr+=4U;
hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
inputaddr+=4U;
hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
inputaddr+=4U;
if(CRYP_WaitOnCCFlag(hcryp, Timeout) != HAL_OK)
{
hcryp->State = HAL_CRYP_STATE_READY;
__HAL_UNLOCK(hcryp);
return HAL_TIMEOUT;
}
/* Clear CCF Flag */
__HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
/* If the suspension flag has been raised and if the processing is not about
to end, suspend processing */
index_test = (uint64_t)index + 16U;
if ((hcryp->SuspendRequest == HAL_CRYP_SUSPEND) && (index_test < Size))
{
/* reset SuspendRequest */
hcryp->SuspendRequest = HAL_CRYP_SUSPEND_NONE;
/* Change the CRYP state */
hcryp->State = HAL_CRYP_STATE_SUSPENDED;
/* Mark that the header phase is over */
hcryp->Phase = HAL_CRYP_PHASE_HEADER_SUSPENDED;
/* Save current reading and writing locations of Input and Output buffers */
hcryp->pCrypInBuffPtr = (uint8_t *)inputaddr;
/* Save the total number of bytes (B blocks + header) that remain to be
processed at this point */
hcryp->CrypInCount = (uint32_t) (hcryp->Init.HeaderSize + Size - index_test);
/* Process Unlocked */
__HAL_UNLOCK(hcryp);
return HAL_OK;
}
} /* for(index=0; (index < Size); index += 16) */
}
#endif /* !defined(AES_CR_NPBLB) */
/* Enter header */
inputaddr = (uint32_t)hcryp->Init.Header;
/* Local variable headerlength is a number of bytes multiple of 128 bits,
remaining header data (if any) are handled after this loop */
headerlength = (((hcryp->Init.HeaderSize)/16U)*16U) ;
if ((hcryp->Init.HeaderSize % 16U) != 0U)
{
difflength = (uint32_t) (hcryp->Init.HeaderSize - headerlength);
}
for(index=0U ; index < headerlength; index += 16U)
{
uint64_t index_temp;
/* Write the Input block in the Data Input register */
hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
inputaddr+=4U;
hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
inputaddr+=4U;
hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
inputaddr+=4U;
hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
inputaddr+=4U;
if(CRYP_WaitOnCCFlag(hcryp, Timeout) != HAL_OK)
{
hcryp->State = HAL_CRYP_STATE_READY;
__HAL_UNLOCK(hcryp);
return HAL_TIMEOUT;
}
/* Clear CCF Flag */
__HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
/* If the suspension flag has been raised and if the processing is not about
to end, suspend processing */
index_temp = (uint64_t)index + 16U;
if ((hcryp->SuspendRequest == HAL_CRYP_SUSPEND) && (index_temp < headerlength))
{
/* reset SuspendRequest */
hcryp->SuspendRequest = HAL_CRYP_SUSPEND_NONE;
/* Change the CRYP state */
hcryp->State = HAL_CRYP_STATE_SUSPENDED;
/* Mark that the header phase is over */
hcryp->Phase = HAL_CRYP_PHASE_HEADER_SUSPENDED;
/* Save current reading and writing locations of Input and Output buffers */
hcryp->pCrypInBuffPtr = (uint8_t *)inputaddr;
/* Save the total number of bytes that remain to be processed at this point */
hcryp->CrypInCount = (uint32_t) (hcryp->Init.HeaderSize - index_temp);
/* Process Unlocked */
__HAL_UNLOCK(hcryp);
return HAL_OK;
}
}
/* Case header length is not a multiple of 16 bytes */
if (difflength != 0U)
{
hcryp->pCrypInBuffPtr = (uint8_t *)inputaddr;
CRYP_Padding(hcryp, difflength, CRYP_POLLING_ON);
}
/* Mark that the header phase is over */
hcryp->Phase = HAL_CRYP_PHASE_HEADER_OVER;
}
/*============================================*/
/* GCM (or CCM when applicable) payload phase */
/*============================================*/
else if (hcryp->Init.GCMCMACPhase == CRYP_PAYLOAD_PHASE)
{
MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, CRYP_PAYLOAD_PHASE);
/* if the header phase has been bypassed, AES must be enabled again */
if (hcryp->Phase == HAL_CRYP_PHASE_INIT_OVER)
{
__HAL_CRYP_ENABLE(hcryp);
}
inputaddr = (uint32_t)pInputData;
outputaddr = (uint32_t)pOutputData;
/* Enter payload */
/* Specific handling to manage payload last block size less than 128 bits */
if ((Size % 16U) != 0U)
{
payloadlength = (Size/16U) * 16U;
difflength = (uint32_t) (Size - payloadlength);
addhoc_process = 1;
}
else
{
payloadlength = Size;
}
/* Feed payload */
for(index=0U ; index < payloadlength; index += 16U)
{
uint64_t index_temp;
/* Write the Input block in the Data Input register */
hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
inputaddr+=4U;
hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
inputaddr+=4U;
hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
inputaddr+=4U;
hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
inputaddr+=4U;
if(CRYP_WaitOnCCFlag(hcryp, Timeout) != HAL_OK)
{
hcryp->State = HAL_CRYP_STATE_READY;
__HAL_UNLOCK(hcryp);
return HAL_TIMEOUT;
}
/* Clear CCF Flag */
__HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
/* Retrieve output data: read the output block
from the Data Output Register */
*(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
outputaddr+=4U;
*(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
outputaddr+=4U;
*(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
outputaddr+=4U;
*(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
outputaddr+=4U;
/* If the suspension flag has been raised and if the processing is not about
to end, suspend processing */
index_temp = (uint64_t)index + 16U;
if ((hcryp->SuspendRequest == HAL_CRYP_SUSPEND) && (index_temp < payloadlength))
{
/* no flag waiting under IRQ handling */
if (hcryp->Init.OperatingMode == CRYP_ALGOMODE_ENCRYPT)
{
/* Ensure that Busy flag is reset */
if(CRYP_WaitOnBusyFlagReset(hcryp, CRYP_BUSY_TIMEOUTVALUE) != HAL_OK)
{
hcryp->State = HAL_CRYP_STATE_READY;
__HAL_UNLOCK(hcryp);
return HAL_TIMEOUT;
}
}
/* reset SuspendRequest */
hcryp->SuspendRequest = HAL_CRYP_SUSPEND_NONE;
/* Change the CRYP state */
hcryp->State = HAL_CRYP_STATE_SUSPENDED;
/* Mark that the header phase is over */
hcryp->Phase = HAL_CRYP_PHASE_HEADER_SUSPENDED;
/* Save current reading and writing locations of Input and Output buffers */
hcryp->pCrypOutBuffPtr = (uint8_t *)outputaddr;
hcryp->pCrypInBuffPtr = (uint8_t *)inputaddr;
/* Save the number of bytes that remain to be processed at this point */
hcryp->CrypInCount = (uint32_t) (Size - index_temp);
/* Process Unlocked */
__HAL_UNLOCK(hcryp);
return HAL_OK;
}
}
/* Additional processing to manage GCM(/CCM) encryption and decryption cases when
payload last block size less than 128 bits */
if (addhoc_process == 1U)
{
hcryp->pCrypInBuffPtr = (uint8_t *)inputaddr;
hcryp->pCrypOutBuffPtr = (uint8_t *)outputaddr;
CRYP_Padding(hcryp, difflength, CRYP_POLLING_ON);
} /* (addhoc_process == 1) */
/* Mark that the payload phase is over */
hcryp->Phase = HAL_CRYP_PHASE_PAYLOAD_OVER;
}
/*==================================*/
/* GCM/GMAC/CCM or CMAC final phase */
/*==================================*/
else
{
tagaddr = (uint32_t)pOutputData;
#if defined(AES_CR_NPBLB)
/* By default, clear NPBLB field */
CLEAR_BIT(hcryp->Instance->CR, AES_CR_NPBLB);
#endif
MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, CRYP_FINAL_PHASE);
/* if the header and payload phases have been bypassed, AES must be enabled again */
if (hcryp->Phase == HAL_CRYP_PHASE_INIT_OVER)
{
__HAL_CRYP_ENABLE(hcryp);
}
if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC)
{
headerlength = hcryp->Init.HeaderSize * 8U; /* Header length in bits */
inputlength = Size * 8U; /* input length in bits */
#if !defined(AES_CR_NPBLB)
if(hcryp->Init.DataType == CRYP_DATATYPE_1B)
{
hcryp->Instance->DINR = __RBIT((uint32_t)(headerlength>>32));
hcryp->Instance->DINR = __RBIT((uint32_t)headerlength);
hcryp->Instance->DINR = __RBIT((uint32_t)(inputlength>>32));
hcryp->Instance->DINR = __RBIT((uint32_t)inputlength);
}
else if(hcryp->Init.DataType == CRYP_DATATYPE_8B)
{
hcryp->Instance->DINR = __REV((uint32_t)(headerlength>>32));
hcryp->Instance->DINR = __REV((uint32_t)headerlength);
hcryp->Instance->DINR = __REV((uint32_t)(inputlength>>32));
hcryp->Instance->DINR = __REV((uint32_t)inputlength);
}
else if(hcryp->Init.DataType == CRYP_DATATYPE_16B)
{
hcryp->Instance->DINR = __ROR((uint32_t)(headerlength>>32), 16);
hcryp->Instance->DINR = __ROR((uint32_t)headerlength, 16);
hcryp->Instance->DINR = __ROR((uint32_t)(inputlength>>32), 16);
hcryp->Instance->DINR = __ROR((uint32_t)inputlength, 16);
}
else if(hcryp->Init.DataType == CRYP_DATATYPE_32B)
{
hcryp->Instance->DINR = (uint32_t)(headerlength>>32);
hcryp->Instance->DINR = (uint32_t)(headerlength);
hcryp->Instance->DINR = (uint32_t)(inputlength>>32);
hcryp->Instance->DINR = (uint32_t)(inputlength);
}
else
{
/* Unspecified Data Type */
return HAL_ERROR;
}
#else
hcryp->Instance->DINR = (uint32_t)(headerlength>>32);
hcryp->Instance->DINR = (uint32_t)(headerlength);
hcryp->Instance->DINR = (uint32_t)(inputlength>>32);
hcryp->Instance->DINR = (uint32_t)(inputlength);
#endif
}
#if !defined(AES_CR_NPBLB)
else if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC)
{
inputaddr = (uint32_t)pInputData;
/* Enter the last block made of a 128-bit value formatted
from the original B0 packet. */
hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
inputaddr+=4U;
hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
inputaddr+=4U;
hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
inputaddr+=4U;
hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
}
else
{
/* Unspecified Chaining Mode */
return HAL_ERROR;
}
#endif
if(CRYP_WaitOnCCFlag(hcryp, Timeout) != HAL_OK)
{
hcryp->State = HAL_CRYP_STATE_READY;
__HAL_UNLOCK(hcryp);
return HAL_TIMEOUT;
}
/* Read the Auth TAG in the Data Out register */
*(uint32_t*)(tagaddr) = hcryp->Instance->DOUTR;
tagaddr+=4U;
*(uint32_t*)(tagaddr) = hcryp->Instance->DOUTR;
tagaddr+=4U;
*(uint32_t*)(tagaddr) = hcryp->Instance->DOUTR;
tagaddr+=4U;
*(uint32_t*)(tagaddr) = hcryp->Instance->DOUTR;
/* Clear CCF Flag */
__HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
/* Mark that the final phase is over */
hcryp->Phase = HAL_CRYP_PHASE_FINAL_OVER;
/* Disable the Peripheral */
__HAL_CRYP_DISABLE(hcryp);
}
/* Change the CRYP state */
hcryp->State = HAL_CRYP_STATE_READY;
/* Process Unlocked */
__HAL_UNLOCK(hcryp);
return HAL_OK;
}
else
{
return HAL_BUSY;
}
}
/**
* @brief Carry out in interrupt mode the authentication tag generation as well as the ciphering or deciphering
* operation according to hcryp->Init structure fields.
* @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
* the configuration information for CRYP module
* @param pInputData:
* - pointer to payload data in GCM or CCM payload phase,
* - pointer to B0 block in CMAC header phase,
* - pointer to C block in CMAC final phase.
* - Parameter is meaningless in case of GCM/GMAC/CCM init, header and final phases.
* @param Size:
* - length of the input payload data buffer in bytes in GCM or CCM payload phase,
* - length of B0 block (in bytes) in CMAC header phase,
* - length of C block (in bytes) in CMAC final phase.
* - Parameter is meaningless in case of GCM/GMAC/CCM init and header phases.
* - Parameter is meaningless in case of CCM final phase.
* - Parameter is message length in bytes in case of GCM final phase.
* - Parameter must be set to zero in case of GMAC final phase.
* @param pOutputData:
* - pointer to plain or cipher text in GCM/CCM payload phase,
* - pointer to authentication tag in GCM/GMAC/CCM/CMAC final phase.
* - Parameter is meaningless in case of GCM/GMAC/CCM init and header phases.
* - Parameter is meaningless in case of CMAC header phase.
* @note Supported operating modes are encryption and decryption, supported chaining modes are GCM, GMAC and CMAC.
* @note Phases are singly processed according to hcryp->Init.GCMCMACPhase so that steps in these specific chaining modes
* can be skipped by the user if so required.
* @retval HAL status
*/
HAL_StatusTypeDef HAL_CRYPEx_AES_Auth_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pInputData, uint64_t Size, uint8_t *pOutputData)
{
uint32_t inputaddr ;
uint64_t headerlength ;
uint64_t inputlength ;
uint32_t index ;
uint32_t addhoc_process = 0;
uint32_t difflength = 0;
uint32_t difflengthmod4 = 0;
uint32_t mask[4][3];
uint32_t mask_index = hcryp->Init.DataType >> AES_CR_DATATYPE_Pos;
mask[0][0] = 0xFF000000U; mask[0][1] = 0xFFFF0000U; mask[0][2] = 0xFFFFFF00U; /* 32-bit data */
mask[1][0] = 0x0000FF00U; mask[1][1] = 0x0000FFFFU; mask[1][2] = 0xFF00FFFFU; /* 16-bit data */
mask[2][0] = 0x000000FFU; mask[2][1] = 0x0000FFFFU; mask[2][2] = 0x00FFFFFFU; /* 8-bit data */
mask[3][0] = 0x000000FFU; mask[3][1] = 0x0000FFFFU; mask[3][2] = 0x00FFFFFFU; /* Bit data */
if (hcryp->State == HAL_CRYP_STATE_READY)
{
/* input/output parameters check */
if (hcryp->Init.GCMCMACPhase == CRYP_INIT_PHASE)
{
/* No processing required */
}
else if (hcryp->Init.GCMCMACPhase == CRYP_HEADER_PHASE)
{
if (((hcryp->Init.Header != NULL) && (hcryp->Init.HeaderSize == 0U)) ||
((hcryp->Init.Header == NULL) && (hcryp->Init.HeaderSize != 0U)))
{
return HAL_ERROR;
}
#if defined(AES_CR_NPBLB)
if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CCM)
#else
if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC)
#endif
{
/* In case of CMAC or CCM header phase resumption, we can have pInputData = NULL and Size = 0 */
if (((pInputData != NULL) && (Size == 0U)) || ((pInputData == NULL) && (Size != 0U)))
{
return HAL_ERROR;
}
}
}
else if (hcryp->Init.GCMCMACPhase == CRYP_PAYLOAD_PHASE)
{
if ((pInputData != NULL) && (Size != 0U) && (pOutputData == NULL))
{
return HAL_ERROR;
}
}
else if (hcryp->Init.GCMCMACPhase == CRYP_FINAL_PHASE)
{
if (pOutputData == NULL)
{
return HAL_ERROR;
}
#if !defined(AES_CR_NPBLB)
if ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC) && (pInputData == NULL))
{
return HAL_ERROR;
}
#endif
}
else
{
/* Unspecified Phase */
return HAL_ERROR;
}
/* Process Locked */
__HAL_LOCK(hcryp);
/* Change the CRYP state */
hcryp->State = HAL_CRYP_STATE_BUSY;
/* Process Unlocked */
__HAL_UNLOCK(hcryp);
/* Enable Computation Complete Flag and Error Interrupts */
__HAL_CRYP_ENABLE_IT(hcryp, CRYP_IT_CCFIE|CRYP_IT_ERRIE);
/*==============================================*/
/* GCM/GMAC (or CCM when applicable) init phase */
/*==============================================*/
if (hcryp->Init.GCMCMACPhase == CRYP_INIT_PHASE)
{
/* In case of init phase, the input data (Key and Initialization Vector) have
already been entered during the initialization process. Therefore, the
software just waits for the CCF interrupt to be raised and which will
be handled by CRYP_AES_Auth_IT() API. */
}
/*===================================*/
/* GCM/GMAC/CCM or CMAC header phase */
/*===================================*/
else if (hcryp->Init.GCMCMACPhase == CRYP_HEADER_PHASE)
{
#if defined(AES_CR_NPBLB)
if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CCM)
#else
if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC)
#endif
{
/* In case of CMAC, B blocks are first entered, before the header.
Therefore, B blocks and the header are entered back-to-back
as if it was only one single block.
However, in case of resumption after suspension, if all the
B blocks have been entered (in that case, Size = 0), only the
remainder of the non-processed header bytes are entered. */
if (Size != 0U)
{
hcryp->CrypInCount = (uint32_t)(Size + hcryp->Init.HeaderSize);
hcryp->pCrypInBuffPtr = pInputData;
}
else
{
hcryp->CrypInCount = (uint32_t)hcryp->Init.HeaderSize;
hcryp->pCrypInBuffPtr = hcryp->Init.Header;
}
}
else
{
/* Get the header addresses and sizes */
hcryp->CrypInCount = (uint32_t)hcryp->Init.HeaderSize;
hcryp->pCrypInBuffPtr = hcryp->Init.Header;
}
inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
#if !defined(AES_CR_NPBLB)
/* Set header phase; for GCM or GMAC, set data-byte at this point */
if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC)
{
MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH|AES_CR_DATATYPE, CRYP_HEADER_PHASE|hcryp->Init.DataType);
}
else
#endif
{
MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, CRYP_HEADER_PHASE);
}
/* Enable the Peripheral */
__HAL_CRYP_ENABLE(hcryp);
/* Increment/decrement instance pointer/counter */
if (hcryp->CrypInCount == 0U)
{
/* Case of no header */
hcryp->State = HAL_CRYP_STATE_READY;
/* Mark that the header phase is over */
hcryp->Phase = HAL_CRYP_PHASE_HEADER_OVER;
return HAL_OK;
}
else if (hcryp->CrypInCount < 16U)
{
hcryp->CrypInCount = 0;
addhoc_process = 1;
difflength = (uint32_t) (hcryp->Init.HeaderSize);
difflengthmod4 = difflength%4U;
}
else
{
hcryp->pCrypInBuffPtr += 16;
hcryp->CrypInCount -= 16U;
}
#if defined(AES_CR_NPBLB)
if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CCM)
#else
if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC)
#endif
{
if (hcryp->CrypInCount == hcryp->Init.HeaderSize)
{
/* All B blocks will have been entered after the next
four DINR writing, so point at header buffer for
the next iteration */
hcryp->pCrypInBuffPtr = hcryp->Init.Header;
}
}
/* Enter header first block to initiate the process
in the Data Input register */
if (addhoc_process == 0U)
{
/* Header has size equal or larger than 128 bits */
hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
inputaddr+=4U;
hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
inputaddr+=4U;
hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
inputaddr+=4U;
hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
}
else
{
/* Header has size less than 128 bits */
/* Enter complete words when possible */
for(index=0U ; index < (difflength/4U); index ++)
{
/* Write the Input block in the Data Input register */
hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
inputaddr+=4U;
}
/* Enter incomplete word padded with zeroes if applicable
(case of header length not a multiple of 32-bits) */
if (difflengthmod4 != 0U)
{
hcryp->Instance->DINR = ((*(uint32_t*)(inputaddr)) & mask[mask_index][difflengthmod4-1U]);
}
/* Pad with zero-words to reach 128-bit long block and wrap-up header feeding to the IP */
for(index=0U; index < (4U - ((difflength+3U)/4U)); index ++)
{
hcryp->Instance->DINR = 0;
}
}
}
/*============================================*/
/* GCM (or CCM when applicable) payload phase */
/*============================================*/
else if (hcryp->Init.GCMCMACPhase == CRYP_PAYLOAD_PHASE)
{
/* Get the buffer addresses and sizes */
hcryp->CrypInCount = (uint32_t)Size;
hcryp->pCrypInBuffPtr = pInputData;
hcryp->pCrypOutBuffPtr = pOutputData;
hcryp->CrypOutCount = (uint32_t)Size;
inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, CRYP_PAYLOAD_PHASE);
/* if the header phase has been bypassed, AES must be enabled again */
if (hcryp->Phase == HAL_CRYP_PHASE_INIT_OVER)
{
__HAL_CRYP_ENABLE(hcryp);
}
/* No payload case */
if (pInputData == NULL)
{
hcryp->State = HAL_CRYP_STATE_READY;
/* Mark that the header phase is over */
hcryp->Phase = HAL_CRYP_PHASE_PAYLOAD_OVER;
/* Process Unlocked */
__HAL_UNLOCK(hcryp);
return HAL_OK;
}
/* Specific handling to manage payload size less than 128 bits */
if (Size < 16U)
{
difflength = (uint32_t) (Size);
#if defined(AES_CR_NPBLB)
/* In case of GCM encryption or CCM decryption, specify the number of padding
bytes in last block of payload */
if (READ_BIT(hcryp->Instance->CR, AES_CR_GCMPH) == CRYP_PAYLOAD_PHASE)
{
uint32_t cr_temp = hcryp->Instance->CR;
if (((cr_temp & (AES_CR_CHMOD|AES_CR_MODE)) == (CRYP_CHAINMODE_AES_GCM_GMAC|CRYP_ALGOMODE_ENCRYPT))
|| ((cr_temp & (AES_CR_CHMOD|AES_CR_MODE)) == (CRYP_CHAINMODE_AES_CCM|CRYP_ALGOMODE_DECRYPT)))
{
/* Set NPBLB field in writing the number of padding bytes
for the last block of payload */
MODIFY_REG(hcryp->Instance->CR, AES_CR_NPBLB, (16U - difflength) << AES_POSITION_CR_NPBLB);
}
}
#else
/* Software workaround applied to GCM encryption only */
if (hcryp->Init.OperatingMode == CRYP_ALGOMODE_ENCRYPT)
{
/* Change the mode configured in CHMOD bits of CR register to select CTR mode */
__HAL_CRYP_SET_CHAININGMODE(hcryp, CRYP_CHAINMODE_AES_CTR);
}
#endif
/* Set hcryp->CrypInCount to 0 (no more data to enter) */
hcryp->CrypInCount = 0;
/* Insert the last block (which size is inferior to 128 bits) padded with zeroes,
to have a complete block of 128 bits */
difflengthmod4 = difflength%4U;
/* Insert the last block (which size is inferior to 128 bits) padded with zeroes
to have a complete block of 128 bits */
for(index=0U; index < (difflength/4U); index ++)
{
/* Write the Input block in the Data Input register */
hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
inputaddr+=4U;
}
/* If required, manage input data size not multiple of 32 bits */
if (difflengthmod4 != 0U)
{
hcryp->Instance->DINR = ((*(uint32_t*)(inputaddr)) & mask[mask_index][difflengthmod4-1U]);
}
/* Wrap-up in padding with zero-words if applicable */
for(index=0U; index < (4U - ((difflength+3U)/4U)); index ++)
{
hcryp->Instance->DINR = 0;
}
}
else
{
/* Increment/decrement instance pointer/counter */
hcryp->pCrypInBuffPtr += 16;
hcryp->CrypInCount -= 16U;
/* Enter payload first block to initiate the process
in the Data Input register */
hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
inputaddr+=4U;
hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
inputaddr+=4U;
hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
inputaddr+=4U;
hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
}
}
/*==================================*/
/* GCM/GMAC/CCM or CMAC final phase */
/*==================================*/
else
{
hcryp->pCrypOutBuffPtr = pOutputData;
#if defined(AES_CR_NPBLB)
/* By default, clear NPBLB field */
CLEAR_BIT(hcryp->Instance->CR, AES_CR_NPBLB);
#endif
MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, CRYP_FINAL_PHASE);
/* if the header and payload phases have been bypassed, AES must be enabled again */
if (hcryp->Phase == HAL_CRYP_PHASE_INIT_OVER)
{
__HAL_CRYP_ENABLE(hcryp);
}
if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC)
{
headerlength = hcryp->Init.HeaderSize * 8U; /* Header length in bits */
inputlength = Size * 8U; /* Input length in bits */
/* Write the number of bits in the header on 64 bits followed by the number
of bits in the payload on 64 bits as well */
#if !defined(AES_CR_NPBLB)
if(hcryp->Init.DataType == CRYP_DATATYPE_1B)
{
hcryp->Instance->DINR = __RBIT((uint32_t)((headerlength)>>32));
hcryp->Instance->DINR = __RBIT((uint32_t)headerlength);
hcryp->Instance->DINR = __RBIT((uint32_t)((inputlength)>>32));
hcryp->Instance->DINR = __RBIT((uint32_t)inputlength);
}
else if(hcryp->Init.DataType == CRYP_DATATYPE_8B)
{
hcryp->Instance->DINR = __REV((uint32_t)(headerlength>>32));
hcryp->Instance->DINR = __REV((uint32_t)headerlength);
hcryp->Instance->DINR = __REV((uint32_t)(inputlength>>32));
hcryp->Instance->DINR = __REV((uint32_t)inputlength);
}
else if(hcryp->Init.DataType == CRYP_DATATYPE_16B)
{
hcryp->Instance->DINR = __ROR((uint32_t)((headerlength)>>32), 16);
hcryp->Instance->DINR = __ROR((uint32_t)headerlength, 16);
hcryp->Instance->DINR = __ROR((uint32_t)((inputlength)>>32), 16);
hcryp->Instance->DINR = __ROR((uint32_t)inputlength, 16);
}
else if(hcryp->Init.DataType == CRYP_DATATYPE_32B)
{
hcryp->Instance->DINR = (uint32_t)(headerlength>>32);
hcryp->Instance->DINR = (uint32_t)(headerlength);
hcryp->Instance->DINR = (uint32_t)(inputlength>>32);
hcryp->Instance->DINR = (uint32_t)(inputlength);
}
else
{
/* Unspecified Data Type */
return HAL_ERROR;
}
#else
hcryp->Instance->DINR = (uint32_t)(headerlength>>32);
hcryp->Instance->DINR = (uint32_t)(headerlength);
hcryp->Instance->DINR = (uint32_t)(inputlength>>32);
hcryp->Instance->DINR = (uint32_t)(inputlength);
#endif
}
#if !defined(AES_CR_NPBLB)
else if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC)
{
inputaddr = (uint32_t)pInputData;
/* Enter the last block made of a 128-bit value formatted
from the original B0 packet. */
hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
inputaddr+=4U;
hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
inputaddr+=4U;
hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
inputaddr+=4U;
hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
}
else
{
/* Unspecified Chaining Mode */
return HAL_ERROR;
}
#endif
}
return HAL_OK;
}
else
{
return HAL_BUSY;
}
}
/**
* @brief Carry out in DMA mode the authentication tag generation as well as the ciphering or deciphering
* operation according to hcryp->Init structure fields.
* @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
* the configuration information for CRYP module
* @param pInputData:
* - pointer to payload data in GCM or CCM payload phase,
* - pointer to B0 block in CMAC header phase,
* - pointer to C block in CMAC final phase.
* - Parameter is meaningless in case of GCM/GMAC/CCM init, header and final phases.
* @param Size:
* - length of the input payload data buffer in bytes in GCM or CCM payload phase,
* - length of B0 block (in bytes) in CMAC header phase,
* - length of C block (in bytes) in CMAC final phase.
* - Parameter is meaningless in case of GCM/GMAC/CCM init and header phases.
* - Parameter is meaningless in case of CCM final phase.
* - Parameter is message length in bytes in case of GCM final phase.
* - Parameter must be set to zero in case of GMAC final phase.
* @param pOutputData:
* - pointer to plain or cipher text in GCM/CCM payload phase,
* - pointer to authentication tag in GCM/GMAC/CCM/CMAC final phase.
* - Parameter is meaningless in case of GCM/GMAC/CCM init and header phases.
* - Parameter is meaningless in case of CMAC header phase.
* @note Supported operating modes are encryption and decryption, supported chaining modes are GCM, GMAC and CMAC.
* @note Phases are singly processed according to hcryp->Init.GCMCMACPhase so that steps in these specific chaining modes
* can be skipped by the user if so required.
* @note pInputData and pOutputData buffers must be 32-bit aligned to ensure a correct DMA transfer to and from the IP.
* @retval HAL status
*/
HAL_StatusTypeDef HAL_CRYPEx_AES_Auth_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pInputData, uint64_t Size, uint8_t *pOutputData)
{
uint32_t inputaddr ;
uint32_t outputaddr ;
uint32_t tagaddr ;
uint64_t headerlength ;
uint64_t inputlength ;
uint64_t payloadlength ;
if (hcryp->State == HAL_CRYP_STATE_READY)
{
/* input/output parameters check */
if (hcryp->Init.GCMCMACPhase == CRYP_INIT_PHASE)
{
/* No processing required */
}
else if (hcryp->Init.GCMCMACPhase == CRYP_HEADER_PHASE)
{
if ((hcryp->Init.Header != NULL) && (hcryp->Init.HeaderSize == 0U))
{
return HAL_ERROR;
}
#if defined(AES_CR_NPBLB)
if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CCM)
{
/* In case of CMAC or CCM header phase resumption, we can have pInputData = NULL and Size = 0 */
if (((pInputData != NULL) && (Size == 0U)) || ((pInputData == NULL) && (Size != 0U)))
{
return HAL_ERROR;
}
}
#else
if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC)
{
if ((pInputData == NULL) || (Size == 0U))
{
return HAL_ERROR;
}
}
#endif
}
else if (hcryp->Init.GCMCMACPhase == CRYP_PAYLOAD_PHASE)
{
if ((pInputData != NULL) && (Size != 0U) && (pOutputData == NULL))
{
return HAL_ERROR;
}
}
else if (hcryp->Init.GCMCMACPhase == CRYP_FINAL_PHASE)
{
if (pOutputData == NULL)
{
return HAL_ERROR;
}
#if !defined(AES_CR_NPBLB)
if ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC) && (pInputData == NULL))
{
return HAL_ERROR;
}
#endif
}
else
{
/* Unspecified Phase */
return HAL_ERROR;
}
/* Process Locked */
__HAL_LOCK(hcryp);
/* Change the CRYP state */
hcryp->State = HAL_CRYP_STATE_BUSY;
/*==============================================*/
/* GCM/GMAC (or CCM when applicable) init phase */
/*==============================================*/
/* In case of init phase, the input data (Key and Initialization Vector) have
already been entered during the initialization process. No DMA transfer is
required at that point therefore, the software just waits for the CCF flag
to be raised. */
if (hcryp->Init.GCMCMACPhase == CRYP_INIT_PHASE)
{
/* just wait for hash computation */
if(CRYP_WaitOnCCFlag(hcryp, CRYP_CCF_TIMEOUTVALUE) != HAL_OK)
{
hcryp->State = HAL_CRYP_STATE_READY;
__HAL_UNLOCK(hcryp);
return HAL_TIMEOUT;
}
/* Clear CCF Flag */
__HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
/* Mark that the initialization phase is over */
hcryp->Phase = HAL_CRYP_PHASE_INIT_OVER;
hcryp->State = HAL_CRYP_STATE_READY;
}
/*====================================*/
/* GCM/GMAC/ CCM or CMAC header phase */
/*====================================*/
else if (hcryp->Init.GCMCMACPhase == CRYP_HEADER_PHASE)
{
#if !defined(AES_CR_NPBLB)
/* Set header phase; for GCM or GMAC, set data-byte at this point */
if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC)
{
MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH|AES_CR_DATATYPE, CRYP_HEADER_PHASE|hcryp->Init.DataType);
}
else
#endif
{
MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, CRYP_HEADER_PHASE);
}
/* Enable the CRYP peripheral */
__HAL_CRYP_ENABLE(hcryp);
#if !defined(AES_CR_NPBLB)
/* enter first B0 block in polling mode (no DMA transfer for B0) */
if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC)
{
inputaddr = (uint32_t)pInputData;
hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
inputaddr+=4U;
hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
inputaddr+=4U;
hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
inputaddr+=4U;
hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
if(CRYP_WaitOnCCFlag(hcryp, CRYP_CCF_TIMEOUTVALUE) != HAL_OK)
{
hcryp->State = HAL_CRYP_STATE_READY;
__HAL_UNLOCK(hcryp);
return HAL_TIMEOUT;
}
/* Clear CCF Flag */
__HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
}
#endif
/* No header case */
if (hcryp->Init.Header == NULL)
{
hcryp->State = HAL_CRYP_STATE_READY;
/* Mark that the header phase is over */
hcryp->Phase = HAL_CRYP_PHASE_HEADER_OVER;
/* Process Unlocked */
__HAL_UNLOCK(hcryp);
return HAL_OK;
}
inputaddr = (uint32_t)hcryp->Init.Header;
if ((hcryp->Init.HeaderSize % 16U) != 0U)
{
if (hcryp->Init.HeaderSize < 16U)
{
hcryp->pCrypInBuffPtr = (uint8_t *)inputaddr;
CRYP_Padding(hcryp, (uint32_t) (hcryp->Init.HeaderSize), CRYP_POLLING_OFF);
hcryp->State = HAL_CRYP_STATE_READY;
/* Mark that the header phase is over */
hcryp->Phase = HAL_CRYP_PHASE_HEADER_OVER;
/* CCF flag indicating header phase AES processing completion
will be checked at the start of the next phase:
- payload phase (GCM / CCM when applicable)
- final phase (GMAC or CMAC when applicable). */
}
else
{
/* Local variable headerlength is a number of bytes multiple of 128 bits,
remaining header data (if any) are handled after this loop */
headerlength = (((hcryp->Init.HeaderSize)/16U)*16U) ;
/* Store the ending transfer point */
hcryp->pCrypInBuffPtr = hcryp->Init.Header + headerlength;
hcryp->CrypInCount = (uint32_t)(hcryp->Init.HeaderSize - headerlength); /* remainder */
/* Set the input and output addresses and start DMA transfer */
/* (incomplete DMA transfer, will be wrapped up after completion of
the first one (initiated here) with data padding */
CRYP_Authentication_SetDMAConfig(hcryp, inputaddr, (uint16_t)headerlength, 0);
}
}
else
{
hcryp->CrypInCount = 0;
/* Set the input address and start DMA transfer */
CRYP_Authentication_SetDMAConfig(hcryp, inputaddr, (uint16_t)hcryp->Init.HeaderSize, 0);
}
}
/*============================================*/
/* GCM (or CCM when applicable) payload phase */
/*============================================*/
else if (hcryp->Init.GCMCMACPhase == CRYP_PAYLOAD_PHASE)
{
/* Coming from header phase, wait for CCF flag to be raised
if header present and fed to the IP in the previous phase */
if (hcryp->Init.Header != NULL)
{
if(CRYP_WaitOnCCFlag(hcryp, CRYP_CCF_TIMEOUTVALUE) != HAL_OK)
{
hcryp->State = HAL_CRYP_STATE_READY;
__HAL_UNLOCK(hcryp);
return HAL_TIMEOUT;
}
}
else
{
/* Enable the Peripheral since wasn't in header phase (no header case) */
__HAL_CRYP_ENABLE(hcryp);
}
/* Clear CCF Flag */
__HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, CRYP_PAYLOAD_PHASE);
/* No payload case */
if (pInputData == NULL)
{
hcryp->State = HAL_CRYP_STATE_READY;
/* Mark that the header phase is over */
hcryp->Phase = HAL_CRYP_PHASE_PAYLOAD_OVER;
/* Process Unlocked */
__HAL_UNLOCK(hcryp);
return HAL_OK;
}
/* Specific handling to manage payload size less than 128 bits */
if ((Size % 16U) != 0U)
{
inputaddr = (uint32_t)pInputData;
outputaddr = (uint32_t)pOutputData;
if (Size < 16U)
{
/* Block is now entered in polling mode, no actual gain in resorting to DMA */
hcryp->pCrypInBuffPtr = (uint8_t *)inputaddr;
hcryp->pCrypOutBuffPtr = (uint8_t *)outputaddr;
CRYP_Padding(hcryp, (uint32_t)Size, CRYP_POLLING_ON);
/* Change the CRYP state to ready */
hcryp->State = HAL_CRYP_STATE_READY;
/* Mark that the payload phase is over */
hcryp->Phase = HAL_CRYP_PHASE_PAYLOAD_OVER;
/* Call output data transfer complete callback */
#if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
hcryp->OutCpltCallback(hcryp);
#else
HAL_CRYP_OutCpltCallback(hcryp);
#endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
}
else
{
payloadlength = (Size/16U) * 16U;
/* Store the ending transfer points */
hcryp->pCrypInBuffPtr = pInputData;
hcryp->pCrypInBuffPtr += payloadlength;
hcryp->pCrypOutBuffPtr = pOutputData;
hcryp->pCrypOutBuffPtr += payloadlength;
hcryp->CrypInCount = (uint32_t)(Size - payloadlength); /* remainder */
/* Set the input and output addresses and start DMA transfer */
/* (incomplete DMA transfer, will be wrapped up with data padding
after completion of the one initiated here) */
CRYP_Authentication_SetDMAConfig(hcryp, inputaddr, (uint16_t)payloadlength, outputaddr);
}
}
else
{
hcryp->CrypInCount = 0;
inputaddr = (uint32_t)pInputData;
outputaddr = (uint32_t)pOutputData;
/* Set the input and output addresses and start DMA transfer */
CRYP_Authentication_SetDMAConfig(hcryp, inputaddr, (uint16_t)Size, outputaddr);
}
}
/*==================================*/
/* GCM/GMAC/CCM or CMAC final phase */
/*==================================*/
else
{
/* If coming from header phase (GMAC or CMAC case when applicable),
wait for CCF flag to be raised */
if (READ_BIT(hcryp->Instance->CR, AES_CR_GCMPH) == CRYP_HEADER_PHASE)
{
if(CRYP_WaitOnCCFlag(hcryp, CRYP_CCF_TIMEOUTVALUE) != HAL_OK)
{
hcryp->State = HAL_CRYP_STATE_READY;
__HAL_UNLOCK(hcryp);
return HAL_TIMEOUT;
}
/* Clear CCF Flag */
__HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
}
tagaddr = (uint32_t)pOutputData;
#if defined(AES_CR_NPBLB)
/* By default, clear NPBLB field */
CLEAR_BIT(hcryp->Instance->CR, AES_CR_NPBLB);
#endif
MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, CRYP_FINAL_PHASE);
/* if the header and payload phases have been bypassed, AES must be enabled again */
if (hcryp->Phase == HAL_CRYP_PHASE_INIT_OVER)
{
__HAL_CRYP_ENABLE(hcryp);
}
if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC)
{
headerlength = hcryp->Init.HeaderSize * 8U; /* Header length in bits */
inputlength = Size * 8U; /* input length in bits */
/* Write the number of bits in the header on 64 bits followed by the number
of bits in the payload on 64 bits as well */
#if !defined(AES_CR_NPBLB)
if(hcryp->Init.DataType == CRYP_DATATYPE_1B)
{
hcryp->Instance->DINR = __RBIT((uint32_t)(headerlength>>32));
hcryp->Instance->DINR = __RBIT((uint32_t)headerlength);
hcryp->Instance->DINR = __RBIT((uint32_t)(inputlength>>32));
hcryp->Instance->DINR = __RBIT((uint32_t)inputlength);
}
else if(hcryp->Init.DataType == CRYP_DATATYPE_8B)
{
hcryp->Instance->DINR = __REV((uint32_t)(headerlength>>32));
hcryp->Instance->DINR = __REV((uint32_t)headerlength);
hcryp->Instance->DINR = __REV((uint32_t)(inputlength>>32));
hcryp->Instance->DINR = __REV((uint32_t)inputlength);
}
else if(hcryp->Init.DataType == CRYP_DATATYPE_16B)
{
hcryp->Instance->DINR = __ROR((uint32_t)(headerlength>>32), 16);
hcryp->Instance->DINR = __ROR((uint32_t)headerlength, 16);
hcryp->Instance->DINR = __ROR((uint32_t)(inputlength>>32), 16);
hcryp->Instance->DINR = __ROR((uint32_t)inputlength, 16);
}
else if(hcryp->Init.DataType == CRYP_DATATYPE_32B)
{
hcryp->Instance->DINR = (uint32_t)(headerlength>>32);
hcryp->Instance->DINR = (uint32_t)(headerlength);
hcryp->Instance->DINR = (uint32_t)(inputlength>>32);
hcryp->Instance->DINR = (uint32_t)(inputlength);
}
else
{
/* Unspecified Data Type */
return HAL_ERROR;
}
#else
hcryp->Instance->DINR = (uint32_t)(headerlength>>32);
hcryp->Instance->DINR = (uint32_t)(headerlength);
hcryp->Instance->DINR = (uint32_t)(inputlength>>32);
hcryp->Instance->DINR = (uint32_t)(inputlength);
#endif
}
#if !defined(AES_CR_NPBLB)
else if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC)
{
__HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
inputaddr = (uint32_t)pInputData;
/* Enter the last block made of a 128-bit value formatted
from the original B0 packet. */
hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
inputaddr+=4U;
hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
inputaddr+=4U;
hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
inputaddr+=4U;
hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
}
else
{
/* Unspecified Chaining Mode */
return HAL_ERROR;
}
#endif
/* No DMA transfer is required at that point therefore, the software
just waits for the CCF flag to be raised. */
if(CRYP_WaitOnCCFlag(hcryp, CRYP_CCF_TIMEOUTVALUE) != HAL_OK)
{
hcryp->State = HAL_CRYP_STATE_READY;
__HAL_UNLOCK(hcryp);
return HAL_TIMEOUT;
}
/* Read the Auth TAG in the IN FIFO */
*(uint32_t*)(tagaddr) = hcryp->Instance->DOUTR;
tagaddr+=4U;
*(uint32_t*)(tagaddr) = hcryp->Instance->DOUTR;
tagaddr+=4U;
*(uint32_t*)(tagaddr) = hcryp->Instance->DOUTR;
tagaddr+=4U;
*(uint32_t*)(tagaddr) = hcryp->Instance->DOUTR;
/* Clear CCF Flag */
__HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
/* Mark that the final phase is over */
hcryp->Phase = HAL_CRYP_PHASE_FINAL_OVER;
hcryp->State = HAL_CRYP_STATE_READY;
/* Disable the Peripheral */
__HAL_CRYP_DISABLE(hcryp);
}
/* Process Unlocked */
__HAL_UNLOCK(hcryp);
return HAL_OK;
}
else
{
return HAL_BUSY;
}
}
/**
* @}
*/
/** @defgroup CRYPEx_Exported_Functions_Group3 AES suspension/resumption functions
* @brief Extended processing functions.
*
@verbatim
==============================================================================
##### AES extended suspension and resumption functions #####
==============================================================================
[..] This section provides functions allowing to:
(+) save in memory the Initialization Vector, the Key registers, the Control register or
the Suspend registers when a process is suspended by a higher priority message
(+) write back in CRYP hardware block the saved values listed above when the suspended
lower priority message processing is resumed.
@endverbatim
* @{
*/
/**
* @brief In case of message processing suspension, read the Initialization Vector.
* @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
* the configuration information for CRYP module.
* @param Output: Pointer to the buffer containing the saved Initialization Vector.
* @note This value has to be stored for reuse by writing the AES_IVRx registers
* as soon as the interrupted processing has to be resumed.
* Applicable to all chaining modes.
* @note AES must be disabled when reading or resetting the IV values.
* @retval None
*/
void HAL_CRYPEx_Read_IVRegisters(CRYP_HandleTypeDef *hcryp, uint8_t* Output)
{
uint32_t outputaddr = (uint32_t)Output;
*(uint32_t*)(outputaddr) = __REV(hcryp->Instance->IVR3);
outputaddr+=4U;
*(uint32_t*)(outputaddr) = __REV(hcryp->Instance->IVR2);
outputaddr+=4U;
*(uint32_t*)(outputaddr) = __REV(hcryp->Instance->IVR1);
outputaddr+=4U;
*(uint32_t*)(outputaddr) = __REV(hcryp->Instance->IVR0);
}
/**
* @brief In case of message processing resumption, rewrite the Initialization
* Vector in the AES_IVRx registers.
* @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
* the configuration information for CRYP module.
* @param Input: Pointer to the buffer containing the saved Initialization Vector to
* write back in the CRYP hardware block.
* @note Applicable to all chaining modes.
* @note AES must be disabled when reading or resetting the IV values.
* @retval None
*/
void HAL_CRYPEx_Write_IVRegisters(CRYP_HandleTypeDef *hcryp, uint8_t* Input)
{
uint32_t ivaddr = (uint32_t)Input;
hcryp->Instance->IVR3 = __REV(*(uint32_t*)(ivaddr));
ivaddr+=4U;
hcryp->Instance->IVR2 = __REV(*(uint32_t*)(ivaddr));
ivaddr+=4U;
hcryp->Instance->IVR1 = __REV(*(uint32_t*)(ivaddr));
ivaddr+=4U;
hcryp->Instance->IVR0 = __REV(*(uint32_t*)(ivaddr));
}
/**
* @brief In case of message GCM/GMAC (CCM/CMAC when applicable) processing suspension,
* read the Suspend Registers.
* @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
* the configuration information for CRYP module.
* @param Output: Pointer to the buffer containing the saved Suspend Registers.
* @note These values have to be stored for reuse by writing back the AES_SUSPxR registers
* as soon as the interrupted processing has to be resumed.
* @retval None
*/
void HAL_CRYPEx_Read_SuspendRegisters(CRYP_HandleTypeDef *hcryp, uint8_t* Output)
{
uint32_t outputaddr = (uint32_t)Output;
/* In case of GCM payload phase encryption, check that suspension can be carried out */
if (READ_BIT(hcryp->Instance->CR, (AES_CR_GCMPH|AES_CR_MODE)) == (CRYP_PAYLOAD_PHASE|CRYP_ALGOMODE_ENCRYPT))
{
/* Ensure that Busy flag is reset */
if(CRYP_WaitOnBusyFlagReset(hcryp, CRYP_BUSY_TIMEOUTVALUE) != HAL_OK)
{
hcryp->ErrorCode |= HAL_CRYP_BUSY_ERROR;
hcryp->State = HAL_CRYP_STATE_ERROR;
/* Process Unlocked */
__HAL_UNLOCK(hcryp);
#if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
hcryp->ErrorCallback(hcryp);
#else
HAL_CRYP_ErrorCallback(hcryp);
#endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
return ;
}
}
*(uint32_t*)(outputaddr) = __REV(hcryp->Instance->SUSP7R);
outputaddr+=4U;
*(uint32_t*)(outputaddr) = __REV(hcryp->Instance->SUSP6R);
outputaddr+=4U;
*(uint32_t*)(outputaddr) = __REV(hcryp->Instance->SUSP5R);
outputaddr+=4U;
*(uint32_t*)(outputaddr) = __REV(hcryp->Instance->SUSP4R);
outputaddr+=4U;
*(uint32_t*)(outputaddr) = __REV(hcryp->Instance->SUSP3R);
outputaddr+=4U;
*(uint32_t*)(outputaddr) = __REV(hcryp->Instance->SUSP2R);
outputaddr+=4U;
*(uint32_t*)(outputaddr) = __REV(hcryp->Instance->SUSP1R);
outputaddr+=4U;
*(uint32_t*)(outputaddr) = __REV(hcryp->Instance->SUSP0R);
}
/**
* @brief In case of message GCM/GMAC (CCM/CMAC when applicable) processing resumption, rewrite the Suspend
* Registers in the AES_SUSPxR registers.
* @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
* the configuration information for CRYP module.
* @param Input: Pointer to the buffer containing the saved suspend registers to
* write back in the CRYP hardware block.
* @retval None
*/
void HAL_CRYPEx_Write_SuspendRegisters(CRYP_HandleTypeDef *hcryp, uint8_t* Input)
{
uint32_t ivaddr = (uint32_t)Input;
hcryp->Instance->SUSP7R = __REV(*(uint32_t*)(ivaddr));
ivaddr+=4U;
hcryp->Instance->SUSP6R = __REV(*(uint32_t*)(ivaddr));
ivaddr+=4U;
hcryp->Instance->SUSP5R = __REV(*(uint32_t*)(ivaddr));
ivaddr+=4U;
hcryp->Instance->SUSP4R = __REV(*(uint32_t*)(ivaddr));
ivaddr+=4U;
hcryp->Instance->SUSP3R = __REV(*(uint32_t*)(ivaddr));
ivaddr+=4U;
hcryp->Instance->SUSP2R = __REV(*(uint32_t*)(ivaddr));
ivaddr+=4U;
hcryp->Instance->SUSP1R = __REV(*(uint32_t*)(ivaddr));
ivaddr+=4U;
hcryp->Instance->SUSP0R = __REV(*(uint32_t*)(ivaddr));
}
/**
* @brief In case of message GCM/GMAC (CCM/CMAC when applicable) processing suspension, read the Key Registers.
* @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
* the configuration information for CRYP module.
* @param Output: Pointer to the buffer containing the saved Key Registers.
* @param KeySize: Indicates the key size (128 or 256 bits).
* @note These values have to be stored for reuse by writing back the AES_KEYRx registers
* as soon as the interrupted processing has to be resumed.
* @retval None
*/
void HAL_CRYPEx_Read_KeyRegisters(CRYP_HandleTypeDef *hcryp, uint8_t* Output, uint32_t KeySize)
{
uint32_t keyaddr = (uint32_t)Output;
if (KeySize == CRYP_KEYSIZE_256B)
{
*(uint32_t*)(keyaddr) = __REV(hcryp->Instance->KEYR7);
keyaddr+=4U;
*(uint32_t*)(keyaddr) = __REV(hcryp->Instance->KEYR6);
keyaddr+=4U;
*(uint32_t*)(keyaddr) = __REV(hcryp->Instance->KEYR5);
keyaddr+=4U;
*(uint32_t*)(keyaddr) = __REV(hcryp->Instance->KEYR4);
keyaddr+=4U;
}
*(uint32_t*)(keyaddr) = __REV(hcryp->Instance->KEYR3);
keyaddr+=4U;
*(uint32_t*)(keyaddr) = __REV(hcryp->Instance->KEYR2);
keyaddr+=4U;
*(uint32_t*)(keyaddr) = __REV(hcryp->Instance->KEYR1);
keyaddr+=4U;
*(uint32_t*)(keyaddr) = __REV(hcryp->Instance->KEYR0);
}
/**
* @brief In case of message GCM/GMAC (CCM/CMAC when applicable) processing resumption, rewrite the Key
* Registers in the AES_KEYRx registers.
* @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
* the configuration information for CRYP module.
* @param Input: Pointer to the buffer containing the saved key registers to
* write back in the CRYP hardware block.
* @param KeySize: Indicates the key size (128 or 256 bits)
* @retval None
*/
void HAL_CRYPEx_Write_KeyRegisters(CRYP_HandleTypeDef *hcryp, uint8_t* Input, uint32_t KeySize)
{
uint32_t keyaddr = (uint32_t)Input;
if (KeySize == CRYP_KEYSIZE_256B)
{
hcryp->Instance->KEYR7 = __REV(*(uint32_t*)(keyaddr));
keyaddr+=4U;
hcryp->Instance->KEYR6 = __REV(*(uint32_t*)(keyaddr));
keyaddr+=4U;
hcryp->Instance->KEYR5 = __REV(*(uint32_t*)(keyaddr));
keyaddr+=4U;
hcryp->Instance->KEYR4 = __REV(*(uint32_t*)(keyaddr));
keyaddr+=4U;
}
hcryp->Instance->KEYR3 = __REV(*(uint32_t*)(keyaddr));
keyaddr+=4U;
hcryp->Instance->KEYR2 = __REV(*(uint32_t*)(keyaddr));
keyaddr+=4U;
hcryp->Instance->KEYR1 = __REV(*(uint32_t*)(keyaddr));
keyaddr+=4U;
hcryp->Instance->KEYR0 = __REV(*(uint32_t*)(keyaddr));
}
/**
* @brief In case of message GCM/GMAC (CCM/CMAC when applicable) processing suspension, read the Control Register.
* @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
* the configuration information for CRYP module.
* @param Output: Pointer to the buffer containing the saved Control Register.
* @note This values has to be stored for reuse by writing back the AES_CR register
* as soon as the interrupted processing has to be resumed.
* @retval None
*/
void HAL_CRYPEx_Read_ControlRegister(CRYP_HandleTypeDef *hcryp, uint8_t* Output)
{
*(uint32_t*)(void *)(Output) = hcryp->Instance->CR; /* Derogation MisraC2012 R.11.5 */
}
/**
* @brief In case of message GCM/GMAC (CCM/CMAC when applicable) processing resumption, rewrite the Control
* Registers in the AES_CR register.
* @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
* the configuration information for CRYP module.
* @param Input: Pointer to the buffer containing the saved Control Register to
* write back in the CRYP hardware block.
* @retval None
*/
void HAL_CRYPEx_Write_ControlRegister(CRYP_HandleTypeDef *hcryp, uint8_t* Input)
{
hcryp->Instance->CR = *(uint32_t*)(void *)(Input); /* Derogation MisraC2012 R.11.5 */
/* At the same time, set handle state back to READY to be able to resume the AES calculations
without the processing APIs returning HAL_BUSY when called. */
hcryp->State = HAL_CRYP_STATE_READY;
}
/**
* @brief Request CRYP processing suspension when in polling or interruption mode.
* @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
* the configuration information for CRYP module.
* @note Set the handle field SuspendRequest to the appropriate value so that
* the on-going CRYP processing is suspended as soon as the required
* conditions are met.
* @note It is advised not to suspend the CRYP processing when the DMA controller
* is managing the data transfer
* @retval None
*/
void HAL_CRYPEx_ProcessSuspend(CRYP_HandleTypeDef *hcryp)
{
/* Set Handle Suspend Request field */
hcryp->SuspendRequest = HAL_CRYP_SUSPEND;
}
/**
* @}
*/
/**
* @}
*/
/** @addtogroup CRYPEx_Private_Functions
* @{
*/
/**
* @brief DMA CRYP Input Data process complete callback
* for GCM, GMAC, CCM or CMAC chaining modes.
* @note Specific setting of hcryp fields are required only
* in the case of header phase where no output data DMA
* transfer is on-going (only input data transfer is enabled
* in such a case).
* @param hdma: DMA handle.
* @retval None
*/
static void CRYP_Authentication_DMAInCplt(DMA_HandleTypeDef *hdma)
{
uint32_t difflength;
CRYP_HandleTypeDef* hcryp = (CRYP_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent; /* Derogation MisraC2012 R.11.5 */
/* Disable the DMA transfer for input request */
CLEAR_BIT(hcryp->Instance->CR, AES_CR_DMAINEN);
if (hcryp->Init.GCMCMACPhase == CRYP_HEADER_PHASE)
{
if (hcryp->CrypInCount != 0U)
{
/* Last block is now entered in polling mode, no actual gain in resorting to DMA */
difflength = hcryp->CrypInCount;
hcryp->CrypInCount = 0;
CRYP_Padding(hcryp, difflength, CRYP_POLLING_OFF);
}
hcryp->State = HAL_CRYP_STATE_READY;
/* Mark that the header phase is over */
hcryp->Phase = HAL_CRYP_PHASE_HEADER_OVER;
}
/* CCF flag indicating header phase AES processing completion
will be checked at the start of the next phase:
- payload phase (GCM or CCM when applicable)
- final phase (GMAC or CMAC).
This allows to avoid the Wait on Flag within the IRQ handling. */
/* Call input data transfer complete callback */
#if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
hcryp->InCpltCallback(hcryp);
#else
HAL_CRYP_InCpltCallback(hcryp);
#endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
}
/**
* @brief DMA CRYP Output Data process complete callback
* for GCM, GMAC, CCM or CMAC chaining modes.
* @note This callback is called only in the payload phase.
* @param hdma: DMA handle.
* @retval None
*/
static void CRYP_Authentication_DMAOutCplt(DMA_HandleTypeDef *hdma)
{
uint32_t difflength;
CRYP_HandleTypeDef* hcryp = (CRYP_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent; /* Derogation MisraC2012 R.11.5 */
/* Disable the DMA transfer for output request */
CLEAR_BIT(hcryp->Instance->CR, AES_CR_DMAOUTEN);
/* Clear CCF Flag */
__HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
/* Initiate additional transfer to wrap-up data feeding to the IP */
if (hcryp->CrypInCount != 0U)
{
/* Last block is now entered in polling mode, no actual gain in resorting to DMA */
difflength = hcryp->CrypInCount;
hcryp->CrypInCount = 0;
CRYP_Padding(hcryp, difflength, CRYP_POLLING_ON);
}
/* Change the CRYP state to ready */
hcryp->State = HAL_CRYP_STATE_READY;
/* Mark that the payload phase is over */
hcryp->Phase = HAL_CRYP_PHASE_PAYLOAD_OVER;
/* Call output data transfer complete callback */
#if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
hcryp->OutCpltCallback(hcryp);
#else
HAL_CRYP_OutCpltCallback(hcryp);
#endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
}
/**
* @brief DMA CRYP communication error callback
* for GCM, GMAC, CCM or CMAC chaining modes.
* @param hdma: DMA handle
* @retval None
*/
static void CRYP_Authentication_DMAError(DMA_HandleTypeDef *hdma)
{
CRYP_HandleTypeDef* hcryp = (CRYP_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent; /* Derogation MisraC2012 R.11.5 */
hcryp->State= HAL_CRYP_STATE_ERROR;
hcryp->ErrorCode |= HAL_CRYP_DMA_ERROR;
#if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
hcryp->ErrorCallback(hcryp);
#else
HAL_CRYP_ErrorCallback(hcryp);
#endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
/* Clear Error Flag */
__HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_ERR_CLEAR);
}
/**
* @brief Handle CRYP block input/output data handling under interruption
* for GCM, GMAC, CCM or CMAC chaining modes.
* @note The function is called under interruption only, once
* interruptions have been enabled by HAL_CRYPEx_AES_Auth_IT().
* @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
* the configuration information for CRYP module
* @retval HAL status
*/
HAL_StatusTypeDef CRYP_AES_Auth_IT(CRYP_HandleTypeDef *hcryp)
{
uint32_t inputaddr ;
uint32_t outputaddr ;
uint32_t index ;
uint32_t addhoc_process = 0;
uint32_t difflength = 0;
uint32_t difflengthmod4 = 0;
uint32_t mask[4][3] ;
uint32_t mask_index = hcryp->Init.DataType >> AES_CR_DATATYPE_Pos;
uint32_t intermediate_data[4] = {0};
mask[0][0] = 0xFF000000U; mask[0][1] = 0xFFFF0000U; mask[0][2] = 0xFFFFFF00U; /* 32-bit data */
mask[1][0] = 0x0000FF00U; mask[1][1] = 0x0000FFFFU; mask[1][2] = 0xFF00FFFFU; /* 16-bit data */
mask[2][0] = 0x000000FFU; mask[2][1] = 0x0000FFFFU; mask[2][2] = 0x00FFFFFFU; /* 8-bit data */
mask[3][0] = 0x000000FFU; mask[3][1] = 0x0000FFFFU; mask[3][2] = 0x00FFFFFFU; /* Bit data */
if(hcryp->State == HAL_CRYP_STATE_BUSY)
{
/*===========================*/
/* GCM/GMAC(/CCM) init phase */
/*===========================*/
if (hcryp->Init.GCMCMACPhase == CRYP_INIT_PHASE)
{
/* Clear Computation Complete Flag */
__HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
/* Disable Computation Complete Flag and Errors Interrupts */
__HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_CCFIE|CRYP_IT_ERRIE);
/* Change the CRYP state */
hcryp->State = HAL_CRYP_STATE_READY;
/* Mark that the initialization phase is over */
hcryp->Phase = HAL_CRYP_PHASE_INIT_OVER;
/* Process Unlocked */
__HAL_UNLOCK(hcryp);
/* Call computation complete callback */
#if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
hcryp->CompCpltCallback(hcryp);
#else
HAL_CRYPEx_ComputationCpltCallback(hcryp);
#endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
return HAL_OK;
}
/*========================================*/
/* GCM/GMAC (or CCM or CMAC) header phase */
/*========================================*/
else if (hcryp->Init.GCMCMACPhase == CRYP_HEADER_PHASE)
{
/* Check if all input header data have been entered */
if (hcryp->CrypInCount == 0U)
{
/* Clear Computation Complete Flag */
__HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
/* Disable Computation Complete Flag and Errors Interrupts */
__HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_CCFIE|CRYP_IT_ERRIE);
/* Change the CRYP state */
hcryp->State = HAL_CRYP_STATE_READY;
/* Mark that the header phase is over */
hcryp->Phase = HAL_CRYP_PHASE_HEADER_OVER;
/* Process Unlocked */
__HAL_UNLOCK(hcryp);
/* Call computation complete callback */
#if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
hcryp->CompCpltCallback(hcryp);
#else
HAL_CRYPEx_ComputationCpltCallback(hcryp);
#endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
return HAL_OK;
}
/* If suspension flag has been raised, suspend processing */
else if (hcryp->SuspendRequest == HAL_CRYP_SUSPEND)
{
/* Clear CCF Flag */
__HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
/* reset SuspendRequest */
hcryp->SuspendRequest = HAL_CRYP_SUSPEND_NONE;
/* Disable Computation Complete Flag and Errors Interrupts */
__HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_CCFIE|CRYP_IT_ERRIE);
/* Change the CRYP state */
hcryp->State = HAL_CRYP_STATE_SUSPENDED;
/* Mark that the header phase is suspended */
hcryp->Phase = HAL_CRYP_PHASE_HEADER_SUSPENDED;
/* Process Unlocked */
__HAL_UNLOCK(hcryp);
return HAL_OK;
}
else /* Carry on feeding input data to the CRYP hardware block */
{
/* Clear Computation Complete Flag */
__HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
/* Get the last Input data address */
inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
/* Increment/decrement instance pointer/counter */
if (hcryp->CrypInCount < 16U)
{
difflength = hcryp->CrypInCount;
hcryp->CrypInCount = 0;
addhoc_process = 1;
difflengthmod4 = difflength%4U;
}
else
{
hcryp->pCrypInBuffPtr += 16;
hcryp->CrypInCount -= 16U;
}
#if defined(AES_CR_NPBLB)
if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CCM)
#else
if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC)
#endif
{
if (hcryp->CrypInCount == hcryp->Init.HeaderSize)
{
/* All B blocks will have been entered after the next
four DINR writing, so point at header buffer for
the next iteration */
hcryp->pCrypInBuffPtr = hcryp->Init.Header;
}
}
/* Write the Input block in the Data Input register */
if (addhoc_process == 0U)
{
hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
inputaddr+=4U;
hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
inputaddr+=4U;
hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
inputaddr+=4U;
hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
}
else
{
/* Header remainder has size less than 128 bits */
/* Enter complete words when possible */
for(index=0U ; index < (difflength/4U); index ++)
{
/* Write the Input block in the Data Input register */
hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
inputaddr+=4U;
}
/* Enter incomplete word padded with zeroes if applicable
(case of header length not a multiple of 32-bits) */
if (difflengthmod4 != 0U)
{
hcryp->Instance->DINR = ((*(uint32_t*)(inputaddr)) & mask[mask_index][difflengthmod4-1U]);
}
/* Pad with zero-words to reach 128-bit long block and wrap-up header feeding to the IP */
for(index=0U; index < (4U - ((difflength+3U)/4U)); index ++)
{
hcryp->Instance->DINR = 0;
}
}
return HAL_OK;
}
}
/*=======================*/
/* GCM/CCM payload phase */
/*=======================*/
else if (hcryp->Init.GCMCMACPhase == CRYP_PAYLOAD_PHASE)
{
/* Get the last output data address */
outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;
/* Specific handling to manage payload size less than 128 bits
when GCM (or CCM when applicable) encryption or decryption is selected.
Check here if the last block output data are read */
#if defined(AES_CR_NPBLB)
if ((hcryp->CrypOutCount < 16U) && \
(hcryp->CrypOutCount > 0U))
#else
if ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC) && \
(hcryp->CrypOutCount < 16U) && \
(hcryp->CrypOutCount > 0U))
#endif
{
difflength = hcryp->CrypOutCount;
difflengthmod4 = difflength%4U;
hcryp->CrypOutCount = 0; /* mark that no more output data will be needed */
/* Retrieve intermediate data */
for(index=0U ; index < 4U; index ++)
{
intermediate_data[index] = hcryp->Instance->DOUTR;
}
/* Retrieve last words of cyphered data */
/* First, retrieve complete output words */
for(index=0U ; index < (difflength/4U); index ++)
{
*(uint32_t*)(outputaddr) = intermediate_data[index];
outputaddr+=4U;
}
/* Next, retrieve partial output word if applicable;
at the same time, start masking intermediate data
with a mask of zeros of same size than the padding
applied to the last block of payload */
if (difflengthmod4 != 0U)
{
intermediate_data[difflength/4U] &= mask[mask_index][difflengthmod4-1U];
*(uint32_t*)(outputaddr) = intermediate_data[difflength/4U];
}
#if !defined(AES_CR_NPBLB)
if (hcryp->Init.OperatingMode == CRYP_ALGOMODE_ENCRYPT)
{
/* Change again CHMOD configuration to GCM mode */
__HAL_CRYP_SET_CHAININGMODE(hcryp, CRYP_CHAINMODE_AES_GCM_GMAC);
/* Select FINAL phase */
MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, CRYP_FINAL_PHASE);
/* Before inserting the intermediate data, carry on masking operation
with a mask of zeros of same size than the padding applied to the last block of payload */
for(index=0U ; index < (4U - ((difflength+3U)/4U)); index ++)
{
intermediate_data[((difflength+3U)/4U)+index] = 0;
}
/* Insert intermediate data to trigger an additional DOUTR reading round */
/* Clear Computation Complete Flag before entering new block */
__HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
for(index=0U ; index < 4U; index ++)
{
hcryp->Instance->DINR = intermediate_data[index];
}
}
else
#endif
{
/* Payload phase is now over */
/* Clear Computation Complete Flag */
__HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
/* Disable Computation Complete Flag and Errors Interrupts */
__HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_CCFIE|CRYP_IT_ERRIE);
/* Change the CRYP state */
hcryp->State = HAL_CRYP_STATE_READY;
/* Mark that the payload phase is over */
hcryp->Phase = HAL_CRYP_PHASE_PAYLOAD_OVER;
/* Process Unlocked */
__HAL_UNLOCK(hcryp);
/* Call computation complete callback */
#if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
hcryp->CompCpltCallback(hcryp);
#else
HAL_CRYPEx_ComputationCpltCallback(hcryp);
#endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
}
return HAL_OK;
}
else
{
if (hcryp->CrypOutCount != 0U)
{
/* Usual case (different than GCM/CCM last block < 128 bits ciphering) */
/* Retrieve the last block available from the CRYP hardware block:
read the output block from the Data Output Register */
*(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
outputaddr+=4U;
*(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
outputaddr+=4U;
*(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
outputaddr+=4U;
*(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
/* Increment/decrement instance pointer/counter */
hcryp->pCrypOutBuffPtr += 16;
hcryp->CrypOutCount -= 16U;
}
#if !defined(AES_CR_NPBLB)
else
{
/* Software work-around: additional DOUTR reading round to discard the data */
for(index=0U ; index < 4U; index ++)
{
intermediate_data[index] = hcryp->Instance->DOUTR;
}
}
#endif
}
/* Check if all output text has been retrieved */
if (hcryp->CrypOutCount == 0U)
{
/* Clear Computation Complete Flag */
__HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
/* Disable Computation Complete Flag and Errors Interrupts */
__HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_CCFIE|CRYP_IT_ERRIE);
/* Change the CRYP state */
hcryp->State = HAL_CRYP_STATE_READY;
/* Mark that the payload phase is over */
hcryp->Phase = HAL_CRYP_PHASE_PAYLOAD_OVER;
/* Process Unlocked */
__HAL_UNLOCK(hcryp);
/* Call computation complete callback */
#if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
hcryp->CompCpltCallback(hcryp);
#else
HAL_CRYPEx_ComputationCpltCallback(hcryp);
#endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
return HAL_OK;
}
/* If suspension flag has been raised, suspend processing */
else if (hcryp->SuspendRequest == HAL_CRYP_SUSPEND)
{
/* Clear CCF Flag */
__HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
/* reset SuspendRequest */
hcryp->SuspendRequest = HAL_CRYP_SUSPEND_NONE;
/* Disable Computation Complete Flag and Errors Interrupts */
__HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_CCFIE|CRYP_IT_ERRIE);
/* Change the CRYP state */
hcryp->State = HAL_CRYP_STATE_SUSPENDED;
/* Mark that the payload phase is suspended */
hcryp->Phase = HAL_CRYP_PHASE_PAYLOAD_SUSPENDED;
/* Process Unlocked */
__HAL_UNLOCK(hcryp);
return HAL_OK;
}
else /* Output data are still expected, carry on feeding the CRYP
hardware block with input data */
{
/* Clear Computation Complete Flag */
__HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
/* Get the last Input data address */
inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
/* Usual input data feeding case */
if (hcryp->CrypInCount < 16U)
{
difflength = (uint32_t) (hcryp->CrypInCount);
difflengthmod4 = difflength%4U;
hcryp->CrypInCount = 0;
#if defined(AES_CR_NPBLB)
/* In case of GCM encryption or CCM decryption, specify the number of padding
bytes in last block of payload */
{
uint32_t cr_temp = hcryp->Instance->CR;
if (((cr_temp & (AES_CR_CHMOD|AES_CR_MODE)) == (CRYP_CHAINMODE_AES_GCM_GMAC|CRYP_ALGOMODE_ENCRYPT))
|| ((cr_temp & (AES_CR_CHMOD|AES_CR_MODE)) == (CRYP_CHAINMODE_AES_CCM|CRYP_ALGOMODE_DECRYPT)))
{
/* Set NPBLB field in writing the number of padding bytes
for the last block of payload */
MODIFY_REG(hcryp->Instance->CR, AES_CR_NPBLB, (16U - difflength) << AES_POSITION_CR_NPBLB);
}
}
#else
/* Software workaround applied to GCM encryption only */
if (hcryp->Init.OperatingMode == CRYP_ALGOMODE_ENCRYPT)
{
/* Change the mode configured in CHMOD bits of CR register to select CTR mode */
__HAL_CRYP_SET_CHAININGMODE(hcryp, CRYP_CHAINMODE_AES_CTR);
}
#endif
/* Insert the last block (which size is inferior to 128 bits) padded with zeroes
to have a complete block of 128 bits */
for(index=0U ; index < (difflength/4U); index ++)
{
/* Write the Input block in the Data Input register */
hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
inputaddr+=4U;
}
/* If required, manage input data size not multiple of 32 bits */
if (difflengthmod4 != 0U)
{
hcryp->Instance->DINR = ((*(uint32_t*)(inputaddr)) & mask[mask_index][difflengthmod4-1U]);
}
/* Wrap-up in padding with zero-words if applicable */
for(index=0U ; index < (4U - ((difflength+3U)/4U)); index ++)
{
hcryp->Instance->DINR = 0;
}
}
else
{
hcryp->pCrypInBuffPtr += 16;
hcryp->CrypInCount -= 16U;
/* Write the Input block in the Data Input register */
hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
inputaddr+=4U;
hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
inputaddr+=4U;
hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
inputaddr+=4U;
hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
}
return HAL_OK;
}
}
/*=======================================*/
/* GCM/GMAC (or CCM or CMAC) final phase */
/*=======================================*/
else if (hcryp->Init.GCMCMACPhase == CRYP_FINAL_PHASE)
{
/* Clear Computation Complete Flag */
__HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
/* Get the last output data address */
outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;
/* Retrieve the last expected data from the CRYP hardware block:
read the output block from the Data Output Register */
*(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
outputaddr+=4U;
*(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
outputaddr+=4U;
*(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
outputaddr+=4U;
*(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
/* Disable Computation Complete Flag and Errors Interrupts */
__HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_CCFIE|CRYP_IT_ERRIE);
/* Change the CRYP state */
hcryp->State = HAL_CRYP_STATE_READY;
/* Mark that the header phase is over */
hcryp->Phase = HAL_CRYP_PHASE_FINAL_OVER;
/* Disable the Peripheral */
__HAL_CRYP_DISABLE(hcryp);
/* Process Unlocked */
__HAL_UNLOCK(hcryp);
/* Call computation complete callback */
#if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
hcryp->CompCpltCallback(hcryp);
#else
HAL_CRYPEx_ComputationCpltCallback(hcryp);
#endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
return HAL_OK;
}
else
{
/* Clear Computation Complete Flag */
__HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
hcryp->State = HAL_CRYP_STATE_ERROR;
__HAL_UNLOCK(hcryp);
return HAL_ERROR;
}
}
else
{
return HAL_BUSY;
}
}
/**
* @brief Set the DMA configuration and start the DMA transfer
* for GCM, GMAC, CCM or CMAC chaining modes.
* @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
* the configuration information for CRYP module.
* @param inputaddr: Address of the Input buffer.
* @param Size: Size of the Input buffer un bytes, must be a multiple of 16.
* @param outputaddr: Address of the Output buffer, null pointer when no output DMA stream
* has to be configured.
* @retval None
*/
static void CRYP_Authentication_SetDMAConfig(CRYP_HandleTypeDef *hcryp, uint32_t inputaddr, uint16_t Size, uint32_t outputaddr)
{
/* Set the input CRYP DMA transfer complete callback */
hcryp->hdmain->XferCpltCallback = CRYP_Authentication_DMAInCplt;
/* Set the DMA error callback */
hcryp->hdmain->XferErrorCallback = CRYP_Authentication_DMAError;
if (outputaddr != 0U)
{
/* Set the output CRYP DMA transfer complete callback */
hcryp->hdmaout->XferCpltCallback = CRYP_Authentication_DMAOutCplt;
/* Set the DMA error callback */
hcryp->hdmaout->XferErrorCallback = CRYP_Authentication_DMAError;
}
/* Enable the CRYP peripheral */
__HAL_CRYP_ENABLE(hcryp);
/* Enable the DMA input stream */
if (HAL_DMA_Start_IT(hcryp->hdmain, inputaddr, (uint32_t)&hcryp->Instance->DINR, ((uint32_t)Size)/4U) != HAL_OK)
{
#if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
hcryp->ErrorCallback(hcryp);
#else
HAL_CRYP_ErrorCallback(hcryp);
#endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
}
/* Enable the DMA input request */
SET_BIT(hcryp->Instance->CR, AES_CR_DMAINEN);
if (outputaddr != 0U)
{
/* Enable the DMA output stream */
if (HAL_DMA_Start_IT(hcryp->hdmaout, (uint32_t)&hcryp->Instance->DOUTR, outputaddr, ((uint32_t)Size)/4U) != HAL_OK)
{
#if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
hcryp->ErrorCallback(hcryp);
#else
HAL_CRYP_ErrorCallback(hcryp);
#endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
}
/* Enable the DMA output request */
SET_BIT(hcryp->Instance->CR, AES_CR_DMAOUTEN);
}
}
/**
* @brief Write/read input/output data in polling mode.
* @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
* the configuration information for CRYP module.
* @param Input: Pointer to the Input buffer.
* @param Ilength: Length of the Input buffer in bytes, must be a multiple of 16.
* @param Output: Pointer to the returned buffer.
* @param Timeout: Specify Timeout value.
* @retval HAL status
*/
static HAL_StatusTypeDef CRYP_ProcessData(CRYP_HandleTypeDef *hcryp, uint8_t* Input, uint16_t Ilength, uint8_t* Output, uint32_t Timeout)
{
uint32_t index;
uint32_t inputaddr = (uint32_t)Input;
uint32_t outputaddr = (uint32_t)Output;
for(index=0U ; (index < Ilength); index += 16U)
{
/* Write the Input block in the Data Input register */
hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
inputaddr+=4U;
hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
inputaddr+=4U;
hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
inputaddr+=4U;
hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
inputaddr+=4U;
/* Wait for CCF flag to be raised */
if(CRYP_WaitOnCCFlag(hcryp, Timeout) != HAL_OK)
{
hcryp->State = HAL_CRYP_STATE_READY;
__HAL_UNLOCK(hcryp);
return HAL_TIMEOUT;
}
/* Clear CCF Flag */
__HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
/* Read the Output block from the Data Output Register */
*(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
outputaddr+=4U;
*(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
outputaddr+=4U;
*(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
outputaddr+=4U;
*(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
outputaddr+=4U;
/* If the suspension flag has been raised and if the processing is not about
to end, suspend processing */
if ((hcryp->SuspendRequest == HAL_CRYP_SUSPEND) && ((index+16U) < Ilength))
{
/* Reset SuspendRequest */
hcryp->SuspendRequest = HAL_CRYP_SUSPEND_NONE;
/* Save current reading and writing locations of Input and Output buffers */
hcryp->pCrypOutBuffPtr = (uint8_t *)outputaddr;
hcryp->pCrypInBuffPtr = (uint8_t *)inputaddr;
/* Save the number of bytes that remain to be processed at this point */
hcryp->CrypInCount = Ilength - (index+16U);
/* Change the CRYP state */
hcryp->State = HAL_CRYP_STATE_SUSPENDED;
return HAL_OK;
}
}
/* Return function status */
return HAL_OK;
}
/**
* @brief Read derivative key in polling mode when CRYP hardware block is set
* in key derivation operating mode (mode 2).
* @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
* the configuration information for CRYP module.
* @param Output: Pointer to the returned buffer.
* @param Timeout: Specify Timeout value.
* @retval HAL status
*/
static HAL_StatusTypeDef CRYP_ReadKey(CRYP_HandleTypeDef *hcryp, uint8_t* Output, uint32_t Timeout)
{
uint32_t outputaddr = (uint32_t)Output;
/* Wait for CCF flag to be raised */
if(CRYP_WaitOnCCFlag(hcryp, Timeout) != HAL_OK)
{
hcryp->State = HAL_CRYP_STATE_READY;
__HAL_UNLOCK(hcryp);
return HAL_TIMEOUT;
}
/* Clear CCF Flag */
__HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
/* Read the derivative key from the AES_KEYRx registers */
if (hcryp->Init.KeySize == CRYP_KEYSIZE_256B)
{
*(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR7);
outputaddr+=4U;
*(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR6);
outputaddr+=4U;
*(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR5);
outputaddr+=4U;
*(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR4);
outputaddr+=4U;
}
*(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR3);
outputaddr+=4U;
*(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR2);
outputaddr+=4U;
*(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR1);
outputaddr+=4U;
*(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR0);
/* Return function status */
return HAL_OK;
}
/**
* @brief Set the DMA configuration and start the DMA transfer.
* @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
* the configuration information for CRYP module.
* @param inputaddr: Address of the Input buffer.
* @param Size: Size of the Input buffer in bytes, must be a multiple of 16.
* @param outputaddr: Address of the Output buffer.
* @retval None
*/
static void CRYP_SetDMAConfig(CRYP_HandleTypeDef *hcryp, uint32_t inputaddr, uint16_t Size, uint32_t outputaddr)
{
/* Set the CRYP DMA transfer complete callback */
hcryp->hdmain->XferCpltCallback = CRYP_DMAInCplt;
/* Set the DMA error callback */
hcryp->hdmain->XferErrorCallback = CRYP_DMAError;
/* Set the CRYP DMA transfer complete callback */
hcryp->hdmaout->XferCpltCallback = CRYP_DMAOutCplt;
/* Set the DMA error callback */
hcryp->hdmaout->XferErrorCallback = CRYP_DMAError;
/* Enable the DMA input stream */
if (HAL_DMA_Start_IT(hcryp->hdmain, inputaddr, (uint32_t)&hcryp->Instance->DINR, ((uint32_t)Size)/4U) != HAL_OK)
{
#if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
hcryp->ErrorCallback(hcryp);
#else
HAL_CRYP_ErrorCallback(hcryp);
#endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
}
/* Enable the DMA output stream */
if (HAL_DMA_Start_IT(hcryp->hdmaout, (uint32_t)&hcryp->Instance->DOUTR, outputaddr, ((uint32_t)Size)/4U) != HAL_OK)
{
#if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
hcryp->ErrorCallback(hcryp);
#else
HAL_CRYP_ErrorCallback(hcryp);
#endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
}
/* Enable In and Out DMA requests */
SET_BIT(hcryp->Instance->CR, (AES_CR_DMAINEN | AES_CR_DMAOUTEN));
/* Enable the CRYP peripheral */
__HAL_CRYP_ENABLE(hcryp);
}
/**
* @brief Handle CRYP hardware block Timeout when waiting for CCF flag to be raised.
* @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
* the configuration information for CRYP module.
* @param Timeout: Timeout duration.
* @retval HAL status
*/
static HAL_StatusTypeDef CRYP_WaitOnCCFlag(CRYP_HandleTypeDef const * const hcryp, uint32_t Timeout)
{
uint32_t tickstart;
/* Get timeout */
tickstart = HAL_GetTick();
while(HAL_IS_BIT_CLR(hcryp->Instance->SR, AES_SR_CCF))
{
/* Check for the Timeout */
if(Timeout != HAL_MAX_DELAY)
{
if((HAL_GetTick() - tickstart ) > Timeout)
{
return HAL_TIMEOUT;
}
}
}
return HAL_OK;
}
/**
* @brief Wait for Busy Flag to be reset during a GCM payload encryption process suspension.
* @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
* the configuration information for CRYP module.
* @param Timeout: Timeout duration.
* @retval HAL status
*/
static HAL_StatusTypeDef CRYP_WaitOnBusyFlagReset(CRYP_HandleTypeDef const * const hcryp, uint32_t Timeout)
{
uint32_t tickstart;
/* Get timeout */
tickstart = HAL_GetTick();
while(HAL_IS_BIT_SET(hcryp->Instance->SR, AES_SR_BUSY))
{
/* Check for the Timeout */
if(Timeout != HAL_MAX_DELAY)
{
if((HAL_GetTick() - tickstart ) > Timeout)
{
return HAL_TIMEOUT;
}
}
}
return HAL_OK;
}
/**
* @brief DMA CRYP Input Data process complete callback.
* @param hdma: DMA handle.
* @retval None
*/
static void CRYP_DMAInCplt(DMA_HandleTypeDef *hdma)
{
CRYP_HandleTypeDef* hcryp = (CRYP_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent; /* Derogation MisraC2012 R.11.5 */
/* Disable the DMA transfer for input request */
CLEAR_BIT(hcryp->Instance->CR, AES_CR_DMAINEN);
/* Call input data transfer complete callback */
#if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
hcryp->InCpltCallback(hcryp);
#else
HAL_CRYP_InCpltCallback(hcryp);
#endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
}
/**
* @brief DMA CRYP Output Data process complete callback.
* @param hdma: DMA handle.
* @retval None
*/
static void CRYP_DMAOutCplt(DMA_HandleTypeDef *hdma)
{
CRYP_HandleTypeDef* hcryp = (CRYP_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent; /* Derogation MisraC2012 R.11.5 */
/* Disable the DMA transfer for output request */
CLEAR_BIT(hcryp->Instance->CR, AES_CR_DMAOUTEN);
/* Clear CCF Flag */
__HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
/* Disable CRYP */
__HAL_CRYP_DISABLE(hcryp);
/* Change the CRYP state to ready */
hcryp->State = HAL_CRYP_STATE_READY;
/* Call output data transfer complete callback */
#if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
hcryp->OutCpltCallback(hcryp);
#else
HAL_CRYP_OutCpltCallback(hcryp);
#endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
}
/**
* @brief DMA CRYP communication error callback.
* @param hdma: DMA handle.
* @retval None
*/
static void CRYP_DMAError(DMA_HandleTypeDef *hdma)
{
CRYP_HandleTypeDef* hcryp = (CRYP_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent; /* Derogation MisraC2012 R.11.5 */
hcryp->State= HAL_CRYP_STATE_ERROR;
hcryp->ErrorCode |= HAL_CRYP_DMA_ERROR;
#if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
hcryp->ErrorCallback(hcryp);
#else
HAL_CRYP_ErrorCallback(hcryp);
#endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
/* Clear Error Flag */
__HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_ERR_CLEAR);
}
/**
* @brief Last header or payload block padding when size is not a multiple of 128 bits.
* @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
* the configuration information for CRYP module.
* @param difflength: size remainder after having fed all complete 128-bit blocks.
* @param polling: specifies whether or not polling on CCF must be done after having
* entered a complete block.
* @retval None
*/
static void CRYP_Padding(CRYP_HandleTypeDef *hcryp, uint32_t difflength, uint32_t polling)
{
uint32_t index;
uint32_t difflengthmod4 = difflength%4U;
uint32_t inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
uint32_t outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;
uint32_t mask[4][3];
uint32_t mask_index = hcryp->Init.DataType >> AES_CR_DATATYPE_Pos;
uint32_t intermediate_data[4] = {0};
mask[0][0] = 0xFF000000U; mask[0][1] = 0xFFFF0000U; mask[0][2] = 0xFFFFFF00U; /* 32-bit data */
mask[1][0] = 0x0000FF00U; mask[1][1] = 0x0000FFFFU; mask[1][2] = 0xFF00FFFFU; /* 16-bit data */
mask[2][0] = 0x000000FFU; mask[2][1] = 0x0000FFFFU; mask[2][2] = 0x00FFFFFFU; /* 8-bit data */
mask[3][0] = 0x000000FFU; mask[3][1] = 0x0000FFFFU; mask[3][2] = 0x00FFFFFFU; /* Bit data */
#if defined(AES_CR_NPBLB)
/* In case of GCM encryption or CCM decryption, specify the number of padding
bytes in last block of payload */
if (READ_BIT(hcryp->Instance->CR,AES_CR_GCMPH) == CRYP_PAYLOAD_PHASE)
{
uint32_t cr_temp = hcryp->Instance->CR;
if (((cr_temp & (AES_CR_CHMOD|AES_CR_MODE)) == (CRYP_CHAINMODE_AES_GCM_GMAC|CRYP_ALGOMODE_ENCRYPT))
|| ((cr_temp & (AES_CR_CHMOD|AES_CR_MODE)) == (CRYP_CHAINMODE_AES_CCM|CRYP_ALGOMODE_DECRYPT)))
{
/* Set NPBLB field in writing the number of padding bytes
for the last block of payload */
MODIFY_REG(hcryp->Instance->CR, AES_CR_NPBLB, (16U - difflength) << AES_POSITION_CR_NPBLB);
}
}
#else
/* Software workaround applied to GCM encryption only */
if ((hcryp->Init.GCMCMACPhase == CRYP_PAYLOAD_PHASE) &&
(hcryp->Init.OperatingMode == CRYP_ALGOMODE_ENCRYPT))
{
/* Change the mode configured in CHMOD bits of CR register to select CTR mode */
__HAL_CRYP_SET_CHAININGMODE(hcryp, CRYP_CHAINMODE_AES_CTR);
}
#endif
/* Wrap-up entering header or payload data */
/* Enter complete words when possible */
for(index=0U ; index < (difflength/4U); index ++)
{
/* Write the Input block in the Data Input register */
hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
inputaddr+=4U;
}
/* Enter incomplete word padded with zeroes if applicable
(case of header length not a multiple of 32-bits) */
if (difflengthmod4 != 0U)
{
hcryp->Instance->DINR = ((*(uint32_t*)(inputaddr)) & mask[mask_index][difflengthmod4-1U]);
}
/* Pad with zero-words to reach 128-bit long block and wrap-up header feeding to the IP */
for(index=0U ; index < (4U - ((difflength+3U)/4U)); index ++)
{
hcryp->Instance->DINR = 0;
}
if (polling == (uint32_t)CRYP_POLLING_ON)
{
if(CRYP_WaitOnCCFlag(hcryp, CRYP_CCF_TIMEOUTVALUE) != HAL_OK)
{
hcryp->State = HAL_CRYP_STATE_READY;
__HAL_UNLOCK(hcryp);
#if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
hcryp->ErrorCallback(hcryp);
#else
HAL_CRYP_ErrorCallback(hcryp);
#endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
}
/* Clear CCF Flag */
__HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
}
/* if payload */
if (hcryp->Init.GCMCMACPhase == CRYP_PAYLOAD_PHASE)
{
/* Retrieve intermediate data */
for(index=0U ; index < 4U; index ++)
{
intermediate_data[index] = hcryp->Instance->DOUTR;
}
/* Retrieve last words of cyphered data */
/* First, retrieve complete output words */
for(index=0U ; index < (difflength/4U); index ++)
{
*(uint32_t*)(outputaddr) = intermediate_data[index];
outputaddr+=4U;
}
/* Next, retrieve partial output word if applicable;
at the same time, start masking intermediate data
with a mask of zeros of same size than the padding
applied to the last block of payload */
if (difflengthmod4 != 0U)
{
intermediate_data[difflength/4U] &= mask[mask_index][difflengthmod4-1U];
*(uint32_t*)(outputaddr) = intermediate_data[difflength/4U];
}
#if !defined(AES_CR_NPBLB)
/* Software workaround applied to GCM encryption only,
applicable for AES IP v2 version (where NPBLB is not defined) */
if (hcryp->Init.OperatingMode == CRYP_ALGOMODE_ENCRYPT)
{
/* Change again CHMOD configuration to GCM mode */
__HAL_CRYP_SET_CHAININGMODE(hcryp, CRYP_CHAINMODE_AES_GCM_GMAC);
/* Select FINAL phase */
MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, CRYP_FINAL_PHASE);
/* Before inserting the intermediate data, carry on masking operation
with a mask of zeros of same size than the padding applied to the last block of payload */
for(index=0U ; index < (4U - ((difflength+3U)/4U)); index ++)
{
intermediate_data[((difflength+3U)/4U)+index] = 0;
}
/* Insert intermediate data */
for(index=0U ; index < 4U; index ++)
{
hcryp->Instance->DINR = intermediate_data[index];
}
/* Wait for completion, and read data on DOUT. This data is to discard. */
if(CRYP_WaitOnCCFlag(hcryp, CRYP_CCF_TIMEOUTVALUE) != HAL_OK)
{
hcryp->State = HAL_CRYP_STATE_READY;
__HAL_UNLOCK(hcryp);
HAL_CRYP_ErrorCallback(hcryp);
}
/* Read data to discard */
/* Clear CCF Flag */
__HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
for(index=0U ; index < 4U; index ++)
{
intermediate_data[index] = hcryp->Instance->DOUTR;
}
} /* if (hcryp->Init.OperatingMode == CRYP_ALGOMODE_ENCRYPT) */
#endif /* !defined(AES_CR_NPBLB) */
} /* if (hcryp->Init.GCMCMACPhase == CRYP_PAYLOAD_PHASE) */
}
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
#endif /* AES */
#endif /* HAL_CRYP_MODULE_ENABLED */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
|