Skip to content

Commit 6f3d4ea

Browse files
Sunyang WuAkhil Goyal
authored andcommitted
examples/ipsec-secgw: support SM4-CBC and SM3-HMAC
This patch adds support for Chinese cryptographic algorithms in the IPsec security gateway example application: - Add SM4-CBC cipher algorithm support with 16-byte IV and key; - Add SM3-HMAC authentication algorithm support with 20-byte key; - Update SA configuration parsing to recognize "sm4-cbc" and "sm3-hmac" keywords; - Implement proper IV handling and authentication offset/length configuration. These additions enable the IPsec security gateway to use Chinese national cryptographic standards for secure communications. Signed-off-by: Sunyang Wu <sunyang.wu@jaguarmicro.com> Acked-by: Akhil Goyal <gakhil@marvell.com>
1 parent fe79e9f commit 6f3d4ea

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

doc/guides/sample_app_ug/ipsec_secgw.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,7 @@ where each options means:
576576
* *aes-128-ctr*: AES-CTR 128-bit algorithm
577577
* *3des-cbc*: 3DES-CBC 192-bit algorithm
578578
* *des-cbc*: DES-CBC 64-bit algorithm
579+
* *sm4-cbc*: SM4-CBC 128-bit algorithm
579580

580581
* Syntax: *cipher_algo <your algorithm>*
581582

@@ -605,6 +606,7 @@ where each options means:
605606
* *sha1-hmac*: HMAC SHA1 algorithm
606607
* *sha256-hmac*: HMAC SHA256 algorithm
607608
* *aes-xcbc-mac*: AES XCBC MAC algorithm
609+
* *sm3-hmac*: HMAC SM3 algorithm
608610

609611
``<auth_key>``
610612

@@ -820,6 +822,13 @@ Example SA rules:
820822
src 1111:1111:1111:1111:1111:1111:1111:5555 \
821823
dst 2222:2222:2222:2222:2222:2222:2222:5555
822824
825+
sa out 30 cipher_algo sm4-cbc \
826+
cipher_key 01:23:45:67:89:ab:cd:ef:fe:dc:ba:98:76:54:32:10 \
827+
auth_algo sm3-hmac \
828+
auth_key 01:23:45:67:89:ab:cd:ef:fe:dc:ba:98:76:54:32:10:11:22:33:44 \
829+
mode ipv4-tunnel \
830+
src 172.16.1.5 dst 172.16.2.5
831+
823832
sa in 105 aead_algo aes-128-gcm \
824833
aead_key de:ad:be:ef:de:ad:be:ef:de:ad:be:ef:de:ad:be:ef:de:ad:be:ef \
825834
mode ipv4-tunnel src 172.16.2.5 dst 172.16.1.5

examples/ipsec-secgw/esp.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ esp_inbound(struct rte_mbuf *m, struct ipsec_sa *sa,
103103
case RTE_CRYPTO_CIPHER_DES_CBC:
104104
case RTE_CRYPTO_CIPHER_3DES_CBC:
105105
case RTE_CRYPTO_CIPHER_AES_CBC:
106+
case RTE_CRYPTO_CIPHER_SM4_CBC:
106107
/* Copy IV at the end of crypto operation */
107108
rte_memcpy(iv_ptr, iv, sa->iv_len);
108109
break;
@@ -123,6 +124,7 @@ esp_inbound(struct rte_mbuf *m, struct ipsec_sa *sa,
123124
case RTE_CRYPTO_AUTH_SHA1_HMAC:
124125
case RTE_CRYPTO_AUTH_SHA256_HMAC:
125126
case RTE_CRYPTO_AUTH_AES_XCBC_MAC:
127+
case RTE_CRYPTO_AUTH_SM3_HMAC:
126128
sym_cop->auth.data.offset = ip_hdr_len;
127129
sym_cop->auth.data.length = sizeof(struct rte_esp_hdr) +
128130
sa->iv_len + payload_len;
@@ -341,6 +343,7 @@ esp_outbound(struct rte_mbuf *m, struct ipsec_sa *sa,
341343
case RTE_CRYPTO_CIPHER_DES_CBC:
342344
case RTE_CRYPTO_CIPHER_3DES_CBC:
343345
case RTE_CRYPTO_CIPHER_AES_CBC:
346+
case RTE_CRYPTO_CIPHER_SM4_CBC:
344347
memset(iv, 0, sa->iv_len);
345348
break;
346349
case RTE_CRYPTO_CIPHER_AES_CTR:
@@ -405,6 +408,7 @@ esp_outbound(struct rte_mbuf *m, struct ipsec_sa *sa,
405408
case RTE_CRYPTO_CIPHER_DES_CBC:
406409
case RTE_CRYPTO_CIPHER_3DES_CBC:
407410
case RTE_CRYPTO_CIPHER_AES_CBC:
411+
case RTE_CRYPTO_CIPHER_SM4_CBC:
408412
sym_cop->cipher.data.offset = ip_hdr_len +
409413
sizeof(struct rte_esp_hdr);
410414
sym_cop->cipher.data.length = pad_payload_len + sa->iv_len;
@@ -436,6 +440,7 @@ esp_outbound(struct rte_mbuf *m, struct ipsec_sa *sa,
436440
case RTE_CRYPTO_AUTH_SHA1_HMAC:
437441
case RTE_CRYPTO_AUTH_SHA256_HMAC:
438442
case RTE_CRYPTO_AUTH_AES_XCBC_MAC:
443+
case RTE_CRYPTO_AUTH_SM3_HMAC:
439444
sym_cop->auth.data.offset = ip_hdr_len;
440445
sym_cop->auth.data.length = sizeof(struct rte_esp_hdr) +
441446
sa->iv_len + pad_payload_len;

examples/ipsec-secgw/sa.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,13 @@ const struct supported_cipher_algo cipher_algos[] = {
128128
.iv_len = 8,
129129
.block_size = 8,
130130
.key_len = 8
131+
},
132+
{
133+
.keyword = "sm4-cbc",
134+
.algo = RTE_CRYPTO_CIPHER_SM4_CBC,
135+
.iv_len = 16,
136+
.block_size = 16,
137+
.key_len = 16
131138
}
132139
};
133140

@@ -175,6 +182,12 @@ const struct supported_auth_algo auth_algos[] = {
175182
.algo = RTE_CRYPTO_AUTH_AES_XCBC_MAC,
176183
.digest_len = 12,
177184
.key_len = 16
185+
},
186+
{
187+
.keyword = "sm3-hmac",
188+
.algo = RTE_CRYPTO_AUTH_SM3_HMAC,
189+
.digest_len = 12,
190+
.key_len = 20
178191
}
179192
};
180193

@@ -502,7 +515,8 @@ parse_sa_tokens(char **tokens, uint32_t n_tokens,
502515
return;
503516

504517
if (algo->algo == RTE_CRYPTO_CIPHER_AES_CBC ||
505-
algo->algo == RTE_CRYPTO_CIPHER_3DES_CBC)
518+
algo->algo == RTE_CRYPTO_CIPHER_3DES_CBC ||
519+
algo->algo == RTE_CRYPTO_CIPHER_SM4_CBC)
506520
rule->salt = (uint32_t)rte_rand();
507521

508522
if (algo->algo == RTE_CRYPTO_CIPHER_AES_CTR) {
@@ -1319,6 +1333,7 @@ sa_add_rules(struct sa_ctx *sa_ctx, const struct ipsec_sa entries[],
13191333
case RTE_CRYPTO_CIPHER_DES_CBC:
13201334
case RTE_CRYPTO_CIPHER_3DES_CBC:
13211335
case RTE_CRYPTO_CIPHER_AES_CBC:
1336+
case RTE_CRYPTO_CIPHER_SM4_CBC:
13221337
iv_length = sa->iv_len;
13231338
break;
13241339
case RTE_CRYPTO_CIPHER_AES_CTR:

0 commit comments

Comments
 (0)