|
44 | 44 | #include "wolfssl/wolfcrypt/kdf.h" |
45 | 45 | #endif |
46 | 46 |
|
| 47 | +#ifdef WOLFSSL_HAVE_SLHDSA |
| 48 | +#include "wolfssl/wolfcrypt/wc_slhdsa.h" |
| 49 | +#endif |
| 50 | + |
47 | 51 | #include "wh_demo_client_crypto.h" |
48 | 52 |
|
49 | 53 | #if !defined(NO_RSA) |
@@ -1692,4 +1696,91 @@ int wh_DemoClient_CryptoCmacKdfCacheInputs(whClientContext* clientContext) |
1692 | 1696 |
|
1693 | 1697 | #endif /* HAVE_CMAC_KDF && WOLFSSL_CMAC */ |
1694 | 1698 |
|
| 1699 | +#if defined(WOLFSSL_HAVE_SLHDSA) && !defined(WOLFSSL_SLHDSA_VERIFY_ONLY) |
| 1700 | + |
| 1701 | +/* Generate an SLH-DSA key that stays on the server and use it purely by key |
| 1702 | + * id. Only the smallest parameter set produces a signature that fits the comm |
| 1703 | + * buffer, so that is what this demo asks for. */ |
| 1704 | +int wh_DemoClient_CryptoSlhDsa(whClientContext* clientContext) |
| 1705 | +{ |
| 1706 | + int ret; |
| 1707 | + int devId = WH_CLIENT_DEVID(clientContext); |
| 1708 | + whKeyId keyId = WH_KEYID_ERASED; |
| 1709 | + SlhDsaKey pub[1]; |
| 1710 | + SlhDsaKey handle[1]; |
| 1711 | + uint8_t label[] = "slhdsa-demo"; |
| 1712 | + byte message[] = "wolfHSM SLH-DSA demo message"; |
| 1713 | + byte signature[WC_SLHDSA_SHAKE128S_SIG_LEN]; |
| 1714 | + word32 sigLen = sizeof(signature); |
| 1715 | + |
| 1716 | + ret = wc_SlhDsaKey_Init(pub, SLHDSA_SHAKE128S, NULL, devId); |
| 1717 | + if (ret != 0) { |
| 1718 | + WOLFHSM_CFG_PRINTF("Failed to wc_SlhDsaKey_Init %d\n", ret); |
| 1719 | + return ret; |
| 1720 | + } |
| 1721 | + |
| 1722 | + ret = wc_SlhDsaKey_Init(handle, SLHDSA_SHAKE128S, NULL, devId); |
| 1723 | + if (ret != 0) { |
| 1724 | + WOLFHSM_CFG_PRINTF("Failed to wc_SlhDsaKey_Init %d\n", ret); |
| 1725 | + wc_SlhDsaKey_Free(pub); |
| 1726 | + return ret; |
| 1727 | + } |
| 1728 | + |
| 1729 | + /* The private key is generated on and never leaves the HSM; only the |
| 1730 | + * public key comes back. */ |
| 1731 | + ret = wh_Client_SlhDsaMakeCacheKeyAndExportPublic( |
| 1732 | + clientContext, SLHDSA_SHAKE128S, &keyId, |
| 1733 | + WH_NVM_FLAGS_USAGE_SIGN | WH_NVM_FLAGS_USAGE_VERIFY, sizeof(label), |
| 1734 | + label, pub); |
| 1735 | + if (ret != 0) { |
| 1736 | + WOLFHSM_CFG_PRINTF("Failed to generate SLH-DSA key %d\n", ret); |
| 1737 | + goto exit; |
| 1738 | + } |
| 1739 | + |
| 1740 | + /* handle holds no key material at all, just the server key id */ |
| 1741 | + ret = wh_Client_SlhDsaSetKeyId(handle, keyId); |
| 1742 | + if (ret != 0) { |
| 1743 | + WOLFHSM_CFG_PRINTF("Failed to wh_Client_SlhDsaSetKeyId %d\n", ret); |
| 1744 | + goto exit; |
| 1745 | + } |
| 1746 | + |
| 1747 | + ret = wc_SlhDsaKey_SignDeterministic(handle, NULL, 0, message, |
| 1748 | + sizeof(message), signature, &sigLen); |
| 1749 | + if (ret != 0) { |
| 1750 | + WOLFHSM_CFG_PRINTF("Failed to wc_SlhDsaKey_SignDeterministic %d\n", |
| 1751 | + ret); |
| 1752 | + goto exit; |
| 1753 | + } |
| 1754 | + |
| 1755 | + ret = wc_SlhDsaKey_Verify(pub, NULL, 0, message, sizeof(message), |
| 1756 | + signature, sigLen); |
| 1757 | + if (ret != 0) { |
| 1758 | + WOLFHSM_CFG_PRINTF("Failed to wc_SlhDsaKey_Verify %d\n", ret); |
| 1759 | + goto exit; |
| 1760 | + } |
| 1761 | + |
| 1762 | + /* A tampered signature must not verify */ |
| 1763 | + signature[0] ^= 0xFF; |
| 1764 | + if (wc_SlhDsaKey_Verify(pub, NULL, 0, message, sizeof(message), signature, |
| 1765 | + sigLen) == 0) { |
| 1766 | + WOLFHSM_CFG_PRINTF("SLH-DSA verified a tampered signature\n"); |
| 1767 | + ret = -1; |
| 1768 | + goto exit; |
| 1769 | + } |
| 1770 | + |
| 1771 | + WOLFHSM_CFG_PRINTF("SLH-DSA sign/verify with a server-resident key: " |
| 1772 | + "SUCCESS\n"); |
| 1773 | + ret = 0; |
| 1774 | + |
| 1775 | +exit: |
| 1776 | + if (!WH_KEYID_ISERASED(keyId)) { |
| 1777 | + (void)wh_Client_KeyEvict(clientContext, keyId); |
| 1778 | + } |
| 1779 | + wc_SlhDsaKey_Free(handle); |
| 1780 | + wc_SlhDsaKey_Free(pub); |
| 1781 | + return ret; |
| 1782 | +} |
| 1783 | + |
| 1784 | +#endif /* WOLFSSL_HAVE_SLHDSA && !WOLFSSL_SLHDSA_VERIFY_ONLY */ |
| 1785 | + |
1695 | 1786 | #endif /* WOLFHSM_CFG_NO_CRYPTO */ |
0 commit comments