|
41 | 41 | #include "wolfssl/wolfcrypt/cryptocb.h" |
42 | 42 | #include "wolfssl/wolfcrypt/asn.h" |
43 | 43 | #include "wolfssl/wolfcrypt/aes.h" |
| 44 | +#include "wolfssl/wolfcrypt/sm4.h" |
44 | 45 | #include "wolfssl/wolfcrypt/cmac.h" |
45 | 46 | #include "wolfssl/wolfcrypt/rsa.h" |
46 | 47 | #include "wolfssl/wolfcrypt/curve25519.h" |
@@ -144,7 +145,7 @@ int wh_Client_CryptoCbStd(int devId, wc_CryptoInfo* info, void* inCtx) |
144 | 145 | /* Based on the info type, process the request */ |
145 | 146 | switch (info->algo_type) |
146 | 147 | { |
147 | | -#if !defined(NO_AES) || !defined(NO_DES3) |
| 148 | +#if !defined(NO_AES) || !defined(NO_DES3) || defined(WOLFSSL_SM4) |
148 | 149 | case WC_ALGO_TYPE_CIPHER: |
149 | 150 | switch (info->cipher.type) |
150 | 151 | { |
@@ -233,6 +234,108 @@ int wh_Client_CryptoCbStd(int devId, wc_CryptoInfo* info, void* inCtx) |
233 | 234 | #endif /* HAVE_AESGCM */ |
234 | 235 | #endif /* !NO_AES */ |
235 | 236 |
|
| 237 | +#ifdef WOLFSSL_SM4 |
| 238 | +#ifdef WOLFSSL_SM4_ECB |
| 239 | + case WC_CIPHER_SM4_ECB: { |
| 240 | + uint32_t enc = info->cipher.enc; |
| 241 | + wc_Sm4* sm4 = info->cipher.sm4ecb.sm4; |
| 242 | + const uint8_t* in = info->cipher.sm4ecb.in; |
| 243 | + uint32_t len = info->cipher.sm4ecb.sz; |
| 244 | + uint8_t* out = info->cipher.sm4ecb.out; |
| 245 | + |
| 246 | + ret = wh_Client_Sm4Ecb(ctx, sm4, enc, in, len, out); |
| 247 | + } break; |
| 248 | +#endif /* WOLFSSL_SM4_ECB */ |
| 249 | + |
| 250 | +#ifdef WOLFSSL_SM4_CBC |
| 251 | + case WC_CIPHER_SM4_CBC: { |
| 252 | + uint32_t enc = info->cipher.enc; |
| 253 | + wc_Sm4* sm4 = info->cipher.sm4cbc.sm4; |
| 254 | + const uint8_t* in = info->cipher.sm4cbc.in; |
| 255 | + uint32_t len = info->cipher.sm4cbc.sz; |
| 256 | + uint8_t* out = info->cipher.sm4cbc.out; |
| 257 | + |
| 258 | + ret = wh_Client_Sm4Cbc(ctx, sm4, enc, in, len, out); |
| 259 | + } break; |
| 260 | +#endif /* WOLFSSL_SM4_CBC */ |
| 261 | + |
| 262 | +#ifdef WOLFSSL_SM4_CTR |
| 263 | + case WC_CIPHER_SM4_CTR: { |
| 264 | + /* CTR is its own inverse, so the direction is not carried */ |
| 265 | + wc_Sm4* sm4 = info->cipher.sm4ctr.sm4; |
| 266 | + const uint8_t* in = info->cipher.sm4ctr.in; |
| 267 | + uint32_t len = info->cipher.sm4ctr.sz; |
| 268 | + uint8_t* out = info->cipher.sm4ctr.out; |
| 269 | + |
| 270 | + ret = wh_Client_Sm4Ctr(ctx, sm4, in, len, out); |
| 271 | + } break; |
| 272 | +#endif /* WOLFSSL_SM4_CTR */ |
| 273 | + |
| 274 | +#ifdef WOLFSSL_SM4_GCM |
| 275 | + case WC_CIPHER_SM4_GCM: { |
| 276 | + uint32_t enc = info->cipher.enc; |
| 277 | + wc_Sm4* sm4 = (enc == 0) ? info->cipher.sm4gcm_dec.sm4 |
| 278 | + : info->cipher.sm4gcm_enc.sm4; |
| 279 | + uint32_t len = (enc == 0) ? info->cipher.sm4gcm_dec.sz |
| 280 | + : info->cipher.sm4gcm_enc.sz; |
| 281 | + uint32_t iv_len = (enc == 0) ? info->cipher.sm4gcm_dec.nonceSz |
| 282 | + : info->cipher.sm4gcm_enc.nonceSz; |
| 283 | + uint32_t authin_len = (enc == 0) ? info->cipher.sm4gcm_dec.authInSz |
| 284 | + : info->cipher.sm4gcm_enc.authInSz; |
| 285 | + uint32_t tag_len = (enc == 0) ? info->cipher.sm4gcm_dec.authTagSz |
| 286 | + : info->cipher.sm4gcm_enc.authTagSz; |
| 287 | + const uint8_t* in = (enc == 0) ? info->cipher.sm4gcm_dec.in |
| 288 | + : info->cipher.sm4gcm_enc.in; |
| 289 | + const uint8_t* iv = (enc == 0) ? info->cipher.sm4gcm_dec.nonce |
| 290 | + : info->cipher.sm4gcm_enc.nonce; |
| 291 | + const uint8_t* authin = (enc == 0) |
| 292 | + ? info->cipher.sm4gcm_dec.authIn |
| 293 | + : info->cipher.sm4gcm_enc.authIn; |
| 294 | + uint8_t* out = (enc == 0) ? info->cipher.sm4gcm_dec.out |
| 295 | + : info->cipher.sm4gcm_enc.out; |
| 296 | + /* The decrypt tag is const on the wolfCrypt side; the client only |
| 297 | + * writes a tag back when encrypting. */ |
| 298 | + uint8_t* tag = |
| 299 | + (enc == 0) ? (uint8_t*)info->cipher.sm4gcm_dec.authTag |
| 300 | + : info->cipher.sm4gcm_enc.authTag; |
| 301 | + |
| 302 | + ret = wh_Client_Sm4Gcm(ctx, sm4, enc, in, len, iv, iv_len, authin, |
| 303 | + authin_len, tag, tag_len, out); |
| 304 | + } break; |
| 305 | +#endif /* WOLFSSL_SM4_GCM */ |
| 306 | + |
| 307 | +#ifdef WOLFSSL_SM4_CCM |
| 308 | + case WC_CIPHER_SM4_CCM: { |
| 309 | + uint32_t enc = info->cipher.enc; |
| 310 | + wc_Sm4* sm4 = (enc == 0) ? info->cipher.sm4ccm_dec.sm4 |
| 311 | + : info->cipher.sm4ccm_enc.sm4; |
| 312 | + uint32_t len = (enc == 0) ? info->cipher.sm4ccm_dec.sz |
| 313 | + : info->cipher.sm4ccm_enc.sz; |
| 314 | + uint32_t iv_len = (enc == 0) ? info->cipher.sm4ccm_dec.nonceSz |
| 315 | + : info->cipher.sm4ccm_enc.nonceSz; |
| 316 | + uint32_t authin_len = (enc == 0) ? info->cipher.sm4ccm_dec.authInSz |
| 317 | + : info->cipher.sm4ccm_enc.authInSz; |
| 318 | + uint32_t tag_len = (enc == 0) ? info->cipher.sm4ccm_dec.authTagSz |
| 319 | + : info->cipher.sm4ccm_enc.authTagSz; |
| 320 | + const uint8_t* in = (enc == 0) ? info->cipher.sm4ccm_dec.in |
| 321 | + : info->cipher.sm4ccm_enc.in; |
| 322 | + const uint8_t* iv = (enc == 0) ? info->cipher.sm4ccm_dec.nonce |
| 323 | + : info->cipher.sm4ccm_enc.nonce; |
| 324 | + const uint8_t* authin = (enc == 0) |
| 325 | + ? info->cipher.sm4ccm_dec.authIn |
| 326 | + : info->cipher.sm4ccm_enc.authIn; |
| 327 | + uint8_t* out = (enc == 0) ? info->cipher.sm4ccm_dec.out |
| 328 | + : info->cipher.sm4ccm_enc.out; |
| 329 | + uint8_t* tag = |
| 330 | + (enc == 0) ? (uint8_t*)info->cipher.sm4ccm_dec.authTag |
| 331 | + : info->cipher.sm4ccm_enc.authTag; |
| 332 | + |
| 333 | + ret = wh_Client_Sm4Ccm(ctx, sm4, enc, in, len, iv, iv_len, authin, |
| 334 | + authin_len, tag, tag_len, out); |
| 335 | + } break; |
| 336 | +#endif /* WOLFSSL_SM4_CCM */ |
| 337 | +#endif /* WOLFSSL_SM4 */ |
| 338 | + |
236 | 339 | default: |
237 | 340 | ret = CRYPTOCB_UNAVAILABLE; |
238 | 341 | break; |
@@ -1655,7 +1758,7 @@ int wh_Client_CryptoCbDma(int devId, wc_CryptoInfo* info, void* inCtx) |
1655 | 1758 | } break; |
1656 | 1759 | #endif |
1657 | 1760 |
|
1658 | | -#if !defined(NO_AES) || !defined(NO_DES3) |
| 1761 | +#if !defined(NO_AES) || !defined(NO_DES3) || defined(WOLFSSL_SM4) |
1659 | 1762 | case WC_ALGO_TYPE_CIPHER: |
1660 | 1763 | switch (info->cipher.type) { |
1661 | 1764 | #ifndef NO_AES |
@@ -1729,6 +1832,106 @@ int wh_Client_CryptoCbDma(int devId, wc_CryptoInfo* info, void* inCtx) |
1729 | 1832 | } break; |
1730 | 1833 | #endif /* HAVE_AES_ECB */ |
1731 | 1834 | #endif /* !NO_AES */ |
| 1835 | +#ifdef WOLFSSL_SM4 |
| 1836 | +#ifdef WOLFSSL_SM4_ECB |
| 1837 | + case WC_CIPHER_SM4_ECB: { |
| 1838 | + uint32_t enc = info->cipher.enc; |
| 1839 | + wc_Sm4* sm4 = info->cipher.sm4ecb.sm4; |
| 1840 | + const uint8_t* in = info->cipher.sm4ecb.in; |
| 1841 | + uint32_t len = info->cipher.sm4ecb.sz; |
| 1842 | + uint8_t* out = info->cipher.sm4ecb.out; |
| 1843 | + |
| 1844 | + ret = wh_Client_Sm4EcbDma(ctx, sm4, enc, in, len, out); |
| 1845 | + } break; |
| 1846 | +#endif /* WOLFSSL_SM4_ECB */ |
| 1847 | +#ifdef WOLFSSL_SM4_CBC |
| 1848 | + case WC_CIPHER_SM4_CBC: { |
| 1849 | + uint32_t enc = info->cipher.enc; |
| 1850 | + wc_Sm4* sm4 = info->cipher.sm4cbc.sm4; |
| 1851 | + const uint8_t* in = info->cipher.sm4cbc.in; |
| 1852 | + uint32_t len = info->cipher.sm4cbc.sz; |
| 1853 | + uint8_t* out = info->cipher.sm4cbc.out; |
| 1854 | + |
| 1855 | + ret = wh_Client_Sm4CbcDma(ctx, sm4, enc, in, len, out); |
| 1856 | + } break; |
| 1857 | +#endif /* WOLFSSL_SM4_CBC */ |
| 1858 | +#ifdef WOLFSSL_SM4_CTR |
| 1859 | + case WC_CIPHER_SM4_CTR: { |
| 1860 | + wc_Sm4* sm4 = info->cipher.sm4ctr.sm4; |
| 1861 | + const uint8_t* in = info->cipher.sm4ctr.in; |
| 1862 | + uint32_t len = info->cipher.sm4ctr.sz; |
| 1863 | + uint8_t* out = info->cipher.sm4ctr.out; |
| 1864 | + |
| 1865 | + ret = wh_Client_Sm4CtrDma(ctx, sm4, in, len, out); |
| 1866 | + } break; |
| 1867 | +#endif /* WOLFSSL_SM4_CTR */ |
| 1868 | +#ifdef WOLFSSL_SM4_GCM |
| 1869 | + case WC_CIPHER_SM4_GCM: { |
| 1870 | + uint32_t enc = info->cipher.enc; |
| 1871 | + wc_Sm4* sm4 = (enc == 0) ? info->cipher.sm4gcm_dec.sm4 |
| 1872 | + : info->cipher.sm4gcm_enc.sm4; |
| 1873 | + uint32_t len = (enc == 0) ? info->cipher.sm4gcm_dec.sz |
| 1874 | + : info->cipher.sm4gcm_enc.sz; |
| 1875 | + uint32_t iv_len = (enc == 0) ? info->cipher.sm4gcm_dec.nonceSz |
| 1876 | + : info->cipher.sm4gcm_enc.nonceSz; |
| 1877 | + uint32_t authin_len = |
| 1878 | + (enc == 0) ? info->cipher.sm4gcm_dec.authInSz |
| 1879 | + : info->cipher.sm4gcm_enc.authInSz; |
| 1880 | + uint32_t tag_len = |
| 1881 | + (enc == 0) ? info->cipher.sm4gcm_dec.authTagSz |
| 1882 | + : info->cipher.sm4gcm_enc.authTagSz; |
| 1883 | + const uint8_t* in = (enc == 0) ? info->cipher.sm4gcm_dec.in |
| 1884 | + : info->cipher.sm4gcm_enc.in; |
| 1885 | + const uint8_t* iv = (enc == 0) ? info->cipher.sm4gcm_dec.nonce |
| 1886 | + : info->cipher.sm4gcm_enc.nonce; |
| 1887 | + const uint8_t* authin = |
| 1888 | + (enc == 0) ? info->cipher.sm4gcm_dec.authIn |
| 1889 | + : info->cipher.sm4gcm_enc.authIn; |
| 1890 | + uint8_t* out = (enc == 0) ? info->cipher.sm4gcm_dec.out |
| 1891 | + : info->cipher.sm4gcm_enc.out; |
| 1892 | + uint8_t* tag = |
| 1893 | + (enc == 0) ? (uint8_t*)info->cipher.sm4gcm_dec.authTag |
| 1894 | + : info->cipher.sm4gcm_enc.authTag; |
| 1895 | + |
| 1896 | + ret = wh_Client_Sm4GcmDma(ctx, sm4, enc, in, len, iv, iv_len, |
| 1897 | + authin, authin_len, tag, tag_len, |
| 1898 | + out); |
| 1899 | + } break; |
| 1900 | +#endif /* WOLFSSL_SM4_GCM */ |
| 1901 | +#ifdef WOLFSSL_SM4_CCM |
| 1902 | + case WC_CIPHER_SM4_CCM: { |
| 1903 | + uint32_t enc = info->cipher.enc; |
| 1904 | + wc_Sm4* sm4 = (enc == 0) ? info->cipher.sm4ccm_dec.sm4 |
| 1905 | + : info->cipher.sm4ccm_enc.sm4; |
| 1906 | + uint32_t len = (enc == 0) ? info->cipher.sm4ccm_dec.sz |
| 1907 | + : info->cipher.sm4ccm_enc.sz; |
| 1908 | + uint32_t iv_len = (enc == 0) ? info->cipher.sm4ccm_dec.nonceSz |
| 1909 | + : info->cipher.sm4ccm_enc.nonceSz; |
| 1910 | + uint32_t authin_len = |
| 1911 | + (enc == 0) ? info->cipher.sm4ccm_dec.authInSz |
| 1912 | + : info->cipher.sm4ccm_enc.authInSz; |
| 1913 | + uint32_t tag_len = |
| 1914 | + (enc == 0) ? info->cipher.sm4ccm_dec.authTagSz |
| 1915 | + : info->cipher.sm4ccm_enc.authTagSz; |
| 1916 | + const uint8_t* in = (enc == 0) ? info->cipher.sm4ccm_dec.in |
| 1917 | + : info->cipher.sm4ccm_enc.in; |
| 1918 | + const uint8_t* iv = (enc == 0) ? info->cipher.sm4ccm_dec.nonce |
| 1919 | + : info->cipher.sm4ccm_enc.nonce; |
| 1920 | + const uint8_t* authin = |
| 1921 | + (enc == 0) ? info->cipher.sm4ccm_dec.authIn |
| 1922 | + : info->cipher.sm4ccm_enc.authIn; |
| 1923 | + uint8_t* out = (enc == 0) ? info->cipher.sm4ccm_dec.out |
| 1924 | + : info->cipher.sm4ccm_enc.out; |
| 1925 | + uint8_t* tag = |
| 1926 | + (enc == 0) ? (uint8_t*)info->cipher.sm4ccm_dec.authTag |
| 1927 | + : info->cipher.sm4ccm_enc.authTag; |
| 1928 | + |
| 1929 | + ret = wh_Client_Sm4CcmDma(ctx, sm4, enc, in, len, iv, iv_len, |
| 1930 | + authin, authin_len, tag, tag_len, |
| 1931 | + out); |
| 1932 | + } break; |
| 1933 | +#endif /* WOLFSSL_SM4_CCM */ |
| 1934 | +#endif /* WOLFSSL_SM4 */ |
1732 | 1935 | default: |
1733 | 1936 | ret = CRYPTOCB_UNAVAILABLE; |
1734 | 1937 | break; |
|
0 commit comments