|
54 | 54 |
|
55 | 55 | #include "wolfhsm/wh_message_crypto.h" |
56 | 56 |
|
| 57 | +/** Helper functions */ |
| 58 | +#ifdef WOLFHSM_CFG_CANCEL_API |
| 59 | +/** |
| 60 | + * Check if the current operation should be canceled |
| 61 | + * @param ctx Server context |
| 62 | + * @param seq Sequence number to check against |
| 63 | + * @return WH_ERROR_CANCEL if canceled, 0 if not canceled, or error code |
| 64 | + */ |
| 65 | +static int _CheckCancellation(whServerContext* ctx, uint16_t seq) |
| 66 | +{ |
| 67 | + uint16_t cancelSeq; |
| 68 | + int ret = wh_Server_GetCanceledSequence(ctx, &cancelSeq); |
| 69 | + if (ret == 0 && cancelSeq == seq) { |
| 70 | + return WH_ERROR_CANCEL; |
| 71 | + } |
| 72 | + return ret; |
| 73 | +} |
| 74 | +#endif |
| 75 | + |
57 | 76 | /** Forward declarations */ |
58 | 77 | #ifndef NO_RSA |
59 | 78 | #ifdef WOLFSSL_KEY_GEN |
@@ -1798,8 +1817,7 @@ static int _HandleCmac(whServerContext* ctx, uint16_t magic, uint16_t seq, |
1798 | 1817 | } |
1799 | 1818 |
|
1800 | 1819 | uint32_t i; |
1801 | | - word32 len; |
1802 | | - uint16_t cancelSeq; |
| 1820 | + word32 len; |
1803 | 1821 | whKeyId keyId = WH_KEYID_ERASED; |
1804 | 1822 |
|
1805 | 1823 | /* Setup fixed size fields */ |
@@ -1890,49 +1908,60 @@ static int _HandleCmac(whServerContext* ctx, uint16_t magic, uint16_t seq, |
1890 | 1908 | } |
1891 | 1909 | /* Handle CMAC update, checking for cancellation */ |
1892 | 1910 | if (ret == 0 && req.inSz != 0) { |
| 1911 | +#ifndef WOLFHSM_CFG_CANCEL_API |
| 1912 | + (void)seq; |
| 1913 | +#endif |
1893 | 1914 | for (i = 0; ret == 0 && i < req.inSz; i += AES_BLOCK_SIZE) { |
1894 | 1915 | if (i + AES_BLOCK_SIZE > req.inSz) { |
1895 | 1916 | blockSz = req.inSz - i; |
1896 | 1917 | } |
1897 | 1918 | ret = wc_CmacUpdate(ctx->crypto->algoCtx.cmac, in + i, |
1898 | 1919 | blockSz); |
| 1920 | +#ifdef WOLFHSM_CFG_CANCEL_API |
1899 | 1921 | if (ret == 0) { |
1900 | | - ret = wh_Server_GetCanceledSequence(ctx, &cancelSeq); |
1901 | | - if (ret == 0 && cancelSeq == seq) { |
1902 | | - ret = WH_ERROR_CANCEL; |
1903 | | - } |
| 1922 | + ret = _CheckCancellation(ctx, seq); |
1904 | 1923 | } |
| 1924 | +#endif |
1905 | 1925 | } |
1906 | 1926 | #ifdef DEBUG_CRYPTOCB_VERBOSE |
1907 | 1927 | printf("[server] cmac update done. ret:%d\n", ret); |
1908 | 1928 | #endif |
1909 | 1929 | } |
1910 | | - /* do final and evict the struct if outSz is set, otherwise cache the |
1911 | | - * struct for a future call */ |
1912 | | - if ((ret == 0 && req.outSz != 0) || ret == WH_ERROR_CANCEL) { |
1913 | | - if (ret != WH_ERROR_CANCEL) { |
1914 | | - keyId = req.keyId; |
1915 | | - len = req.outSz; |
| 1930 | + |
| 1931 | + /* Check if we should finalize and evict, or cache for future calls |
| 1932 | + */ |
| 1933 | + if (ret == 0 && req.outSz != 0) { |
| 1934 | + /* Finalize CMAC operation */ |
| 1935 | + keyId = req.keyId; |
| 1936 | + len = req.outSz; |
1916 | 1937 | #ifdef DEBUG_CRYPTOCB_VERBOSE |
1917 | | - printf("[server] cmac final keyId:%x len:%d\n",keyId, len); |
| 1938 | + printf("[server] cmac final keyId:%x len:%d\n", keyId, len); |
1918 | 1939 | #endif |
1919 | | - ret = wc_CmacFinal(ctx->crypto->algoCtx.cmac, out, &len); |
1920 | | - res.outSz = len; |
1921 | | - res.keyId = WH_KEYID_ERASED; |
1922 | | - } |
1923 | | - /* evict the key, canceling means abandoning the current state */ |
1924 | | - if (ret == 0 || ret == WH_ERROR_CANCEL) { |
1925 | | - if (!WH_KEYID_ISERASED(keyId)) { |
1926 | | - /* Don't override return value except on failure */ |
1927 | | - int tmpRet = wh_Server_KeystoreEvictKey( |
1928 | | - ctx, WH_MAKE_KEYID(WH_KEYTYPE_CRYPTO, |
1929 | | - ctx->comm->client_id, keyId)); |
1930 | | - if (tmpRet != 0) { |
1931 | | - ret = tmpRet; |
1932 | | - } |
| 1940 | + ret = wc_CmacFinal(ctx->crypto->algoCtx.cmac, out, &len); |
| 1941 | + res.outSz = len; |
| 1942 | + res.keyId = WH_KEYID_ERASED; |
| 1943 | + |
| 1944 | + /* Evict the key from cache */ |
| 1945 | + if (!WH_KEYID_ISERASED(keyId)) { |
| 1946 | + /* Don't override return value except on failure */ |
| 1947 | + int tmpRet = wh_Server_KeystoreEvictKey( |
| 1948 | + ctx, WH_MAKE_KEYID(WH_KEYTYPE_CRYPTO, |
| 1949 | + ctx->comm->client_id, keyId)); |
| 1950 | + if (tmpRet != 0) { |
| 1951 | + ret = tmpRet; |
1933 | 1952 | } |
1934 | 1953 | } |
1935 | 1954 | } |
| 1955 | +#ifdef WOLFHSM_CFG_CANCEL_API |
| 1956 | + else if (ret == WH_ERROR_CANCEL) { |
| 1957 | + /* Handle cancellation - evict key and abandon state */ |
| 1958 | + if (!WH_KEYID_ISERASED(req.keyId)) { |
| 1959 | + wh_Server_KeystoreEvictKey( |
| 1960 | + ctx, WH_MAKE_KEYID(WH_KEYTYPE_CRYPTO, |
| 1961 | + ctx->comm->client_id, req.keyId)); |
| 1962 | + } |
| 1963 | + } |
| 1964 | +#endif |
1936 | 1965 | /* Cache the CMAC struct for a future update call */ |
1937 | 1966 | else if (ret == 0) { |
1938 | 1967 | /* cache/re-cache updated struct */ |
@@ -2930,9 +2959,13 @@ int wh_Server_HandleCryptoRequest(whServerContext* ctx, uint16_t magic, |
2930 | 2959 | /* Since crypto error codes are propagated to the client in the response |
2931 | 2960 | * packet, return success to the caller unless a cancellation has occurred |
2932 | 2961 | */ |
| 2962 | +#ifdef WOLFHSM_CFG_CANCEL_API |
2933 | 2963 | if (ret != WH_ERROR_CANCEL) { |
2934 | 2964 | ret = WH_ERROR_OK; |
2935 | 2965 | } |
| 2966 | +#else |
| 2967 | + ret = WH_ERROR_OK; |
| 2968 | +#endif |
2936 | 2969 | return ret; |
2937 | 2970 | } |
2938 | 2971 |
|
@@ -4303,9 +4336,13 @@ int wh_Server_HandleCryptoDmaRequest(whServerContext* ctx, uint16_t magic, |
4303 | 4336 | /* Since crypto error codes are propagated to the client in the response |
4304 | 4337 | * packet, return success to the caller unless a cancellation has occurred |
4305 | 4338 | */ |
| 4339 | +#ifdef WOLFHSM_CFG_CANCEL_API |
4306 | 4340 | if (ret != WH_ERROR_CANCEL) { |
4307 | 4341 | ret = WH_ERROR_OK; |
4308 | 4342 | } |
| 4343 | +#else |
| 4344 | + ret = WH_ERROR_OK; |
| 4345 | +#endif |
4309 | 4346 | return ret; |
4310 | 4347 | } |
4311 | 4348 | #endif /* WOLFHSM_CFG_DMA */ |
|
0 commit comments