Skip to content

Commit 4e28919

Browse files
committed
Add SM3 and SM4 crypto cb
1 parent dd3ff88 commit 4e28919

11 files changed

Lines changed: 3856 additions & 2 deletions

src/wh_client_crypto.c

Lines changed: 1059 additions & 0 deletions
Large diffs are not rendered by default.

src/wh_client_cryptocb.c

Lines changed: 205 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#include "wolfssl/wolfcrypt/cryptocb.h"
4242
#include "wolfssl/wolfcrypt/asn.h"
4343
#include "wolfssl/wolfcrypt/aes.h"
44+
#include "wolfssl/wolfcrypt/sm4.h"
4445
#include "wolfssl/wolfcrypt/cmac.h"
4546
#include "wolfssl/wolfcrypt/rsa.h"
4647
#include "wolfssl/wolfcrypt/curve25519.h"
@@ -144,7 +145,7 @@ int wh_Client_CryptoCbStd(int devId, wc_CryptoInfo* info, void* inCtx)
144145
/* Based on the info type, process the request */
145146
switch (info->algo_type)
146147
{
147-
#if !defined(NO_AES) || !defined(NO_DES3)
148+
#if !defined(NO_AES) || !defined(NO_DES3) || defined(WOLFSSL_SM4)
148149
case WC_ALGO_TYPE_CIPHER:
149150
switch (info->cipher.type)
150151
{
@@ -233,6 +234,108 @@ int wh_Client_CryptoCbStd(int devId, wc_CryptoInfo* info, void* inCtx)
233234
#endif /* HAVE_AESGCM */
234235
#endif /* !NO_AES */
235236

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+
236339
default:
237340
ret = CRYPTOCB_UNAVAILABLE;
238341
break;
@@ -1655,7 +1758,7 @@ int wh_Client_CryptoCbDma(int devId, wc_CryptoInfo* info, void* inCtx)
16551758
} break;
16561759
#endif
16571760

1658-
#if !defined(NO_AES) || !defined(NO_DES3)
1761+
#if !defined(NO_AES) || !defined(NO_DES3) || defined(WOLFSSL_SM4)
16591762
case WC_ALGO_TYPE_CIPHER:
16601763
switch (info->cipher.type) {
16611764
#ifndef NO_AES
@@ -1729,6 +1832,106 @@ int wh_Client_CryptoCbDma(int devId, wc_CryptoInfo* info, void* inCtx)
17291832
} break;
17301833
#endif /* HAVE_AES_ECB */
17311834
#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 */
17321935
default:
17331936
ret = CRYPTOCB_UNAVAILABLE;
17341937
break;

0 commit comments

Comments
 (0)