4242#include <net/strparser.h>
4343#include <net/tls.h>
4444
45- #define MAX_IV_SIZE TLS_CIPHER_AES_GCM_128_IV_SIZE
46-
4745static int __skb_nsg (struct sk_buff * skb , int offset , int len ,
4846 unsigned int recursion_level )
4947{
@@ -479,11 +477,18 @@ static int tls_do_encryption(struct sock *sk,
479477 struct tls_rec * rec = ctx -> open_rec ;
480478 struct sk_msg * msg_en = & rec -> msg_encrypted ;
481479 struct scatterlist * sge = sk_msg_elem (msg_en , start );
482- int rc ;
480+ int rc , iv_offset = 0 ;
481+
482+ /* For CCM based ciphers, first byte of IV is a constant */
483+ if (prot -> cipher_type == TLS_CIPHER_AES_CCM_128 ) {
484+ rec -> iv_data [0 ] = TLS_AES_CCM_IV_B0_BYTE ;
485+ iv_offset = 1 ;
486+ }
487+
488+ memcpy (& rec -> iv_data [iv_offset ], tls_ctx -> tx .iv ,
489+ prot -> iv_size + prot -> salt_size );
483490
484- memcpy (rec -> iv_data , tls_ctx -> tx .iv , sizeof (rec -> iv_data ));
485- xor_iv_with_seq (prot -> version , rec -> iv_data ,
486- tls_ctx -> tx .rec_seq );
491+ xor_iv_with_seq (prot -> version , rec -> iv_data , tls_ctx -> tx .rec_seq );
487492
488493 sge -> offset += prot -> prepend_size ;
489494 sge -> length -= prot -> prepend_size ;
@@ -1344,6 +1349,7 @@ static int decrypt_internal(struct sock *sk, struct sk_buff *skb,
13441349 struct scatterlist * sgout = NULL ;
13451350 const int data_len = rxm -> full_len - prot -> overhead_size +
13461351 prot -> tail_size ;
1352+ int iv_offset = 0 ;
13471353
13481354 if (* zc && (out_iov || out_sg )) {
13491355 if (out_iov )
@@ -1386,18 +1392,25 @@ static int decrypt_internal(struct sock *sk, struct sk_buff *skb,
13861392 aad = (u8 * )(sgout + n_sgout );
13871393 iv = aad + prot -> aad_size ;
13881394
1395+ /* For CCM based ciphers, first byte of nonce+iv is always '2' */
1396+ if (prot -> cipher_type == TLS_CIPHER_AES_CCM_128 ) {
1397+ iv [0 ] = 2 ;
1398+ iv_offset = 1 ;
1399+ }
1400+
13891401 /* Prepare IV */
13901402 err = skb_copy_bits (skb , rxm -> offset + TLS_HEADER_SIZE ,
1391- iv + TLS_CIPHER_AES_GCM_128_SALT_SIZE ,
1403+ iv + iv_offset + prot -> salt_size ,
13921404 prot -> iv_size );
13931405 if (err < 0 ) {
13941406 kfree (mem );
13951407 return err ;
13961408 }
13971409 if (prot -> version == TLS_1_3_VERSION )
1398- memcpy (iv , tls_ctx -> rx .iv , crypto_aead_ivsize (ctx -> aead_recv ));
1410+ memcpy (iv + iv_offset , tls_ctx -> rx .iv ,
1411+ crypto_aead_ivsize (ctx -> aead_recv ));
13991412 else
1400- memcpy (iv , tls_ctx -> rx .iv , TLS_CIPHER_AES_GCM_128_SALT_SIZE );
1413+ memcpy (iv + iv_offset , tls_ctx -> rx .iv , prot -> salt_size );
14011414
14021415 xor_iv_with_seq (prot -> version , iv , tls_ctx -> rx .rec_seq );
14031416
@@ -2152,14 +2165,15 @@ int tls_set_sw_offload(struct sock *sk, struct tls_context *ctx, int tx)
21522165 struct tls_crypto_info * crypto_info ;
21532166 struct tls12_crypto_info_aes_gcm_128 * gcm_128_info ;
21542167 struct tls12_crypto_info_aes_gcm_256 * gcm_256_info ;
2168+ struct tls12_crypto_info_aes_ccm_128 * ccm_128_info ;
21552169 struct tls_sw_context_tx * sw_ctx_tx = NULL ;
21562170 struct tls_sw_context_rx * sw_ctx_rx = NULL ;
21572171 struct cipher_context * cctx ;
21582172 struct crypto_aead * * aead ;
21592173 struct strp_callbacks cb ;
2160- u16 nonce_size , tag_size , iv_size , rec_seq_size ;
2174+ u16 nonce_size , tag_size , iv_size , rec_seq_size , salt_size ;
21612175 struct crypto_tfm * tfm ;
2162- char * iv , * rec_seq , * key , * salt ;
2176+ char * iv , * rec_seq , * key , * salt , * cipher_name ;
21632177 size_t keysize ;
21642178 int rc = 0 ;
21652179
@@ -2224,6 +2238,8 @@ int tls_set_sw_offload(struct sock *sk, struct tls_context *ctx, int tx)
22242238 keysize = TLS_CIPHER_AES_GCM_128_KEY_SIZE ;
22252239 key = gcm_128_info -> key ;
22262240 salt = gcm_128_info -> salt ;
2241+ salt_size = TLS_CIPHER_AES_GCM_128_SALT_SIZE ;
2242+ cipher_name = "gcm(aes)" ;
22272243 break ;
22282244 }
22292245 case TLS_CIPHER_AES_GCM_256 : {
@@ -2239,6 +2255,25 @@ int tls_set_sw_offload(struct sock *sk, struct tls_context *ctx, int tx)
22392255 keysize = TLS_CIPHER_AES_GCM_256_KEY_SIZE ;
22402256 key = gcm_256_info -> key ;
22412257 salt = gcm_256_info -> salt ;
2258+ salt_size = TLS_CIPHER_AES_GCM_256_SALT_SIZE ;
2259+ cipher_name = "gcm(aes)" ;
2260+ break ;
2261+ }
2262+ case TLS_CIPHER_AES_CCM_128 : {
2263+ nonce_size = TLS_CIPHER_AES_CCM_128_IV_SIZE ;
2264+ tag_size = TLS_CIPHER_AES_CCM_128_TAG_SIZE ;
2265+ iv_size = TLS_CIPHER_AES_CCM_128_IV_SIZE ;
2266+ iv = ((struct tls12_crypto_info_aes_ccm_128 * )crypto_info )-> iv ;
2267+ rec_seq_size = TLS_CIPHER_AES_CCM_128_REC_SEQ_SIZE ;
2268+ rec_seq =
2269+ ((struct tls12_crypto_info_aes_ccm_128 * )crypto_info )-> rec_seq ;
2270+ ccm_128_info =
2271+ (struct tls12_crypto_info_aes_ccm_128 * )crypto_info ;
2272+ keysize = TLS_CIPHER_AES_CCM_128_KEY_SIZE ;
2273+ key = ccm_128_info -> key ;
2274+ salt = ccm_128_info -> salt ;
2275+ salt_size = TLS_CIPHER_AES_CCM_128_SALT_SIZE ;
2276+ cipher_name = "ccm(aes)" ;
22422277 break ;
22432278 }
22442279 default :
@@ -2268,24 +2303,24 @@ int tls_set_sw_offload(struct sock *sk, struct tls_context *ctx, int tx)
22682303 prot -> overhead_size = prot -> prepend_size +
22692304 prot -> tag_size + prot -> tail_size ;
22702305 prot -> iv_size = iv_size ;
2271- cctx -> iv = kmalloc ( iv_size + TLS_CIPHER_AES_GCM_128_SALT_SIZE ,
2272- GFP_KERNEL );
2306+ prot -> salt_size = salt_size ;
2307+ cctx -> iv = kmalloc ( iv_size + salt_size , GFP_KERNEL );
22732308 if (!cctx -> iv ) {
22742309 rc = - ENOMEM ;
22752310 goto free_priv ;
22762311 }
22772312 /* Note: 128 & 256 bit salt are the same size */
2278- memcpy (cctx -> iv , salt , TLS_CIPHER_AES_GCM_128_SALT_SIZE );
2279- memcpy (cctx -> iv + TLS_CIPHER_AES_GCM_128_SALT_SIZE , iv , iv_size );
22802313 prot -> rec_seq_size = rec_seq_size ;
2314+ memcpy (cctx -> iv , salt , salt_size );
2315+ memcpy (cctx -> iv + salt_size , iv , iv_size );
22812316 cctx -> rec_seq = kmemdup (rec_seq , rec_seq_size , GFP_KERNEL );
22822317 if (!cctx -> rec_seq ) {
22832318 rc = - ENOMEM ;
22842319 goto free_iv ;
22852320 }
22862321
22872322 if (!* aead ) {
2288- * aead = crypto_alloc_aead ("gcm(aes)" , 0 , 0 );
2323+ * aead = crypto_alloc_aead (cipher_name , 0 , 0 );
22892324 if (IS_ERR (* aead )) {
22902325 rc = PTR_ERR (* aead );
22912326 * aead = NULL ;
0 commit comments