Commit 128cfb88 authored by Tianjia Zhang's avatar Tianjia Zhang Committed by David S. Miller

net/tls: support SM4 CCM algorithm

The IV of CCM mode has special requirements, this patch supports CCM
mode of SM4 algorithm.
Signed-off-by: default avatarTianjia Zhang <tianjia.zhang@linux.alibaba.com>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent 1817750b
...@@ -66,7 +66,7 @@ ...@@ -66,7 +66,7 @@
#define MAX_IV_SIZE 16 #define MAX_IV_SIZE 16
#define TLS_MAX_REC_SEQ_SIZE 8 #define TLS_MAX_REC_SEQ_SIZE 8
/* For AES-CCM, the full 16-bytes of IV is made of '4' fields of given sizes. /* For CCM mode, the full 16-bytes of IV is made of '4' fields of given sizes.
* *
* IV[16] = b0[1] || implicit nonce[4] || explicit nonce[8] || length[3] * IV[16] = b0[1] || implicit nonce[4] || explicit nonce[8] || length[3]
* *
...@@ -74,6 +74,7 @@ ...@@ -74,6 +74,7 @@
* Hence b0 contains (3 - 1) = 2. * Hence b0 contains (3 - 1) = 2.
*/ */
#define TLS_AES_CCM_IV_B0_BYTE 2 #define TLS_AES_CCM_IV_B0_BYTE 2
#define TLS_SM4_CCM_IV_B0_BYTE 2
#define __TLS_INC_STATS(net, field) \ #define __TLS_INC_STATS(net, field) \
__SNMP_INC_STATS((net)->mib.tls_statistics, field) __SNMP_INC_STATS((net)->mib.tls_statistics, field)
......
...@@ -498,9 +498,15 @@ static int tls_do_encryption(struct sock *sk, ...@@ -498,9 +498,15 @@ static int tls_do_encryption(struct sock *sk,
int rc, iv_offset = 0; int rc, iv_offset = 0;
/* For CCM based ciphers, first byte of IV is a constant */ /* For CCM based ciphers, first byte of IV is a constant */
if (prot->cipher_type == TLS_CIPHER_AES_CCM_128) { switch (prot->cipher_type) {
case TLS_CIPHER_AES_CCM_128:
rec->iv_data[0] = TLS_AES_CCM_IV_B0_BYTE; rec->iv_data[0] = TLS_AES_CCM_IV_B0_BYTE;
iv_offset = 1; iv_offset = 1;
break;
case TLS_CIPHER_SM4_CCM:
rec->iv_data[0] = TLS_SM4_CCM_IV_B0_BYTE;
iv_offset = 1;
break;
} }
memcpy(&rec->iv_data[iv_offset], tls_ctx->tx.iv, memcpy(&rec->iv_data[iv_offset], tls_ctx->tx.iv,
...@@ -1457,10 +1463,16 @@ static int decrypt_internal(struct sock *sk, struct sk_buff *skb, ...@@ -1457,10 +1463,16 @@ static int decrypt_internal(struct sock *sk, struct sk_buff *skb,
aad = (u8 *)(sgout + n_sgout); aad = (u8 *)(sgout + n_sgout);
iv = aad + prot->aad_size; iv = aad + prot->aad_size;
/* For CCM based ciphers, first byte of nonce+iv is always '2' */ /* For CCM based ciphers, first byte of nonce+iv is a constant */
if (prot->cipher_type == TLS_CIPHER_AES_CCM_128) { switch (prot->cipher_type) {
iv[0] = 2; case TLS_CIPHER_AES_CCM_128:
iv[0] = TLS_AES_CCM_IV_B0_BYTE;
iv_offset = 1; iv_offset = 1;
break;
case TLS_CIPHER_SM4_CCM:
iv[0] = TLS_SM4_CCM_IV_B0_BYTE;
iv_offset = 1;
break;
} }
/* Prepare IV */ /* Prepare IV */
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment