42
42
#include <net/strparser.h>
43
43
#include <net/tls.h>
44
44
45
- #define MAX_IV_SIZE TLS_CIPHER_AES_GCM_128_IV_SIZE
46
-
47
45
static int __skb_nsg (struct sk_buff * skb , int offset , int len ,
48
46
unsigned int recursion_level )
49
47
{
@@ -479,11 +477,18 @@ static int tls_do_encryption(struct sock *sk,
479
477
struct tls_rec * rec = ctx -> open_rec ;
480
478
struct sk_msg * msg_en = & rec -> msg_encrypted ;
481
479
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 );
483
490
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 );
487
492
488
493
sge -> offset += prot -> prepend_size ;
489
494
sge -> length -= prot -> prepend_size ;
@@ -1344,6 +1349,7 @@ static int decrypt_internal(struct sock *sk, struct sk_buff *skb,
1344
1349
struct scatterlist * sgout = NULL ;
1345
1350
const int data_len = rxm -> full_len - prot -> overhead_size +
1346
1351
prot -> tail_size ;
1352
+ int iv_offset = 0 ;
1347
1353
1348
1354
if (* zc && (out_iov || out_sg )) {
1349
1355
if (out_iov )
@@ -1386,18 +1392,25 @@ static int decrypt_internal(struct sock *sk, struct sk_buff *skb,
1386
1392
aad = (u8 * )(sgout + n_sgout );
1387
1393
iv = aad + prot -> aad_size ;
1388
1394
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
+
1389
1401
/* Prepare IV */
1390
1402
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 ,
1392
1404
prot -> iv_size );
1393
1405
if (err < 0 ) {
1394
1406
kfree (mem );
1395
1407
return err ;
1396
1408
}
1397
1409
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 ));
1399
1412
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 );
1401
1414
1402
1415
xor_iv_with_seq (prot -> version , iv , tls_ctx -> rx .rec_seq );
1403
1416
@@ -2152,14 +2165,15 @@ int tls_set_sw_offload(struct sock *sk, struct tls_context *ctx, int tx)
2152
2165
struct tls_crypto_info * crypto_info ;
2153
2166
struct tls12_crypto_info_aes_gcm_128 * gcm_128_info ;
2154
2167
struct tls12_crypto_info_aes_gcm_256 * gcm_256_info ;
2168
+ struct tls12_crypto_info_aes_ccm_128 * ccm_128_info ;
2155
2169
struct tls_sw_context_tx * sw_ctx_tx = NULL ;
2156
2170
struct tls_sw_context_rx * sw_ctx_rx = NULL ;
2157
2171
struct cipher_context * cctx ;
2158
2172
struct crypto_aead * * aead ;
2159
2173
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 ;
2161
2175
struct crypto_tfm * tfm ;
2162
- char * iv , * rec_seq , * key , * salt ;
2176
+ char * iv , * rec_seq , * key , * salt , * cipher_name ;
2163
2177
size_t keysize ;
2164
2178
int rc = 0 ;
2165
2179
@@ -2224,6 +2238,8 @@ int tls_set_sw_offload(struct sock *sk, struct tls_context *ctx, int tx)
2224
2238
keysize = TLS_CIPHER_AES_GCM_128_KEY_SIZE ;
2225
2239
key = gcm_128_info -> key ;
2226
2240
salt = gcm_128_info -> salt ;
2241
+ salt_size = TLS_CIPHER_AES_GCM_128_SALT_SIZE ;
2242
+ cipher_name = "gcm(aes)" ;
2227
2243
break ;
2228
2244
}
2229
2245
case TLS_CIPHER_AES_GCM_256 : {
@@ -2239,6 +2255,25 @@ int tls_set_sw_offload(struct sock *sk, struct tls_context *ctx, int tx)
2239
2255
keysize = TLS_CIPHER_AES_GCM_256_KEY_SIZE ;
2240
2256
key = gcm_256_info -> key ;
2241
2257
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)" ;
2242
2277
break ;
2243
2278
}
2244
2279
default :
@@ -2268,24 +2303,24 @@ int tls_set_sw_offload(struct sock *sk, struct tls_context *ctx, int tx)
2268
2303
prot -> overhead_size = prot -> prepend_size +
2269
2304
prot -> tag_size + prot -> tail_size ;
2270
2305
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 );
2273
2308
if (!cctx -> iv ) {
2274
2309
rc = - ENOMEM ;
2275
2310
goto free_priv ;
2276
2311
}
2277
2312
/* 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 );
2280
2313
prot -> rec_seq_size = rec_seq_size ;
2314
+ memcpy (cctx -> iv , salt , salt_size );
2315
+ memcpy (cctx -> iv + salt_size , iv , iv_size );
2281
2316
cctx -> rec_seq = kmemdup (rec_seq , rec_seq_size , GFP_KERNEL );
2282
2317
if (!cctx -> rec_seq ) {
2283
2318
rc = - ENOMEM ;
2284
2319
goto free_iv ;
2285
2320
}
2286
2321
2287
2322
if (!* aead ) {
2288
- * aead = crypto_alloc_aead ("gcm(aes)" , 0 , 0 );
2323
+ * aead = crypto_alloc_aead (cipher_name , 0 , 0 );
2289
2324
if (IS_ERR (* aead )) {
2290
2325
rc = PTR_ERR (* aead );
2291
2326
* aead = NULL ;
0 commit comments