50
50
)
51
51
#endif
52
52
53
+
54
+ /***********************************************************************
55
+ **
56
+ */ static mbedtls_cipher_id_t get_cipher_id (REBCNT sym )
57
+ /*
58
+ ** Note: requires exact word order defined in boot/words.reb file
59
+ **
60
+ ***********************************************************************/
61
+ {
62
+ if (sym >= SYM_AES_128_ECB && sym <= SYM_AES_256_GCM ) {
63
+ return MBEDTLS_CIPHER_ID_AES ;
64
+ }
65
+ if (sym >= SYM_CAMELLIA_128_ECB && sym <= SYM_CAMELLIA_256_GCM ) {
66
+ return MBEDTLS_CIPHER_ID_CAMELLIA ;
67
+ }
68
+ if (sym >= SYM_ARIA_128_ECB && sym <= SYM_ARIA_256_GCM ) {
69
+ return MBEDTLS_CIPHER_ID_ARIA ;
70
+ }
71
+ if (sym >= SYM_CHACHA20 && sym <= SYM_CHACHA20_POLY1305 ) {
72
+ return MBEDTLS_CIPHER_ID_CHACHA20 ;
73
+ }
74
+ return MBEDTLS_CIPHER_ID_NULL ;
75
+ }
76
+
77
+
53
78
static void free_crypt_cipher_context (CRYPT_CTX * ctx );
54
79
55
80
@@ -117,6 +142,7 @@ static void free_crypt_cipher_context(CRYPT_CTX *ctx);
117
142
if (IS_NONE (val )) {
118
143
CLEAR (& ctx -> IV , MBEDTLS_MAX_IV_LENGTH );
119
144
CLEAR (& ctx -> nonce , MBEDTLS_MAX_IV_LENGTH );
145
+ ctx -> IV_len = 0 ;
120
146
return TRUE;
121
147
}
122
148
if (IS_BINARY (val )) {
@@ -127,6 +153,7 @@ static void free_crypt_cipher_context(CRYPT_CTX *ctx);
127
153
CLEAR (& ctx -> IV , MBEDTLS_MAX_IV_LENGTH );
128
154
CLEAR (& ctx -> nonce , MBEDTLS_MAX_IV_LENGTH );
129
155
COPY_MEM (& ctx -> IV , VAL_BIN_AT (val ), len );
156
+ ctx -> IV_len = len ;
130
157
}
131
158
return TRUE;
132
159
}
@@ -255,6 +282,39 @@ static void free_crypt_cipher_context(CRYPT_CTX *ctx);
255
282
ctx -> cipher_block_size = 16 ;
256
283
break ;
257
284
285
+ #ifdef MBEDTLS_CCM_C
286
+ case SYM_AES_128_CCM :
287
+ case SYM_AES_192_CCM :
288
+ case SYM_AES_256_CCM :
289
+ #ifdef MBEDTLS_CAMELLIA_C
290
+ case SYM_CAMELLIA_128_CCM :
291
+ case SYM_CAMELLIA_192_CCM :
292
+ case SYM_CAMELLIA_256_CCM :
293
+ #endif
294
+ #ifdef MBEDTLS_ARIA_C
295
+ case SYM_ARIA_128_CCM :
296
+ case SYM_ARIA_192_CCM :
297
+ case SYM_ARIA_256_CCM :
298
+ #endif
299
+ if (ctx -> cipher_ctx == NULL )
300
+ ctx -> cipher_ctx = malloc (sizeof (mbedtls_ccm_context ));
301
+ mbedtls_ccm_init ((mbedtls_ccm_context * )ctx -> cipher_ctx );
302
+ switch (type ) {
303
+ case SYM_AES_128_CCM :
304
+ case SYM_ARIA_128_CCM :
305
+ case SYM_CAMELLIA_128_CCM : ctx -> key_bitlen = 128 ; break ;
306
+ case SYM_AES_192_CCM :
307
+ case SYM_ARIA_192_CCM :
308
+ case SYM_CAMELLIA_192_CCM : ctx -> key_bitlen = 192 ; break ;
309
+ case SYM_AES_256_CCM :
310
+ case SYM_ARIA_256_CCM :
311
+ case SYM_CAMELLIA_256_CCM : ctx -> key_bitlen = 256 ; break ;
312
+ }
313
+ ctx -> cipher_block_size = 0 ;
314
+ break ;
315
+
316
+ #endif
317
+
258
318
#ifdef MBEDTLS_GCM_C
259
319
case SYM_AES_128_GCM :
260
320
case SYM_AES_192_GCM :
@@ -466,6 +526,30 @@ static void free_crypt_cipher_context(CRYPT_CTX *ctx);
466
526
break ;
467
527
#endif
468
528
529
+ #ifdef MBEDTLS_CCM_C
530
+ case SYM_AES_128_CCM :
531
+ case SYM_AES_192_CCM :
532
+ case SYM_AES_256_CCM :
533
+ #ifdef MBEDTLS_CAMELLIA_C
534
+ case SYM_CAMELLIA_128_CCM :
535
+ case SYM_CAMELLIA_192_CCM :
536
+ case SYM_CAMELLIA_256_CCM :
537
+ #endif
538
+ #ifdef MBEDTLS_ARIA_C
539
+ case SYM_ARIA_128_CCM :
540
+ case SYM_ARIA_192_CCM :
541
+ case SYM_ARIA_256_CCM :
542
+ #endif
543
+ {
544
+ size_t out_bytes = 0 ;
545
+ err = mbedtls_ccm_update ((mbedtls_ccm_context * )ctx -> cipher_ctx , input , len , BIN_TAIL (bin ), len , & out_bytes );
546
+ if (err ) return err ;
547
+ SERIES_TAIL (bin ) += out_bytes ;
548
+ input += out_bytes ;
549
+ break ;
550
+ }
551
+ #endif
552
+
469
553
#ifdef MBEDTLS_GCM_C
470
554
case SYM_AES_128_GCM :
471
555
case SYM_AES_192_GCM :
@@ -589,6 +673,7 @@ static void free_crypt_cipher_context(CRYPT_CTX *ctx);
589
673
return CRYPT_OK ;
590
674
}
591
675
676
+
592
677
/***********************************************************************
593
678
**
594
679
*/ static REBINT Crypt_Init (CRYPT_CTX * ctx )
@@ -619,6 +704,31 @@ static void free_crypt_cipher_context(CRYPT_CTX *ctx);
619
704
}
620
705
break ;
621
706
707
+ #ifdef MBEDTLS_CCM_C
708
+ case SYM_AES_128_CCM :
709
+ case SYM_AES_192_CCM :
710
+ case SYM_AES_256_CCM :
711
+ #ifdef MBEDTLS_CAMELLIA_C
712
+ case SYM_CAMELLIA_128_CCM :
713
+ case SYM_CAMELLIA_192_CCM :
714
+ case SYM_CAMELLIA_256_CCM :
715
+ #endif
716
+ #ifdef MBEDTLS_ARIA_C
717
+ case SYM_ARIA_128_CCM :
718
+ case SYM_ARIA_192_CCM :
719
+ case SYM_ARIA_256_CCM :
720
+ #endif
721
+ {
722
+ mbedtls_ccm_context * ccm = (mbedtls_ccm_context * )ctx -> cipher_ctx ;
723
+ err = mbedtls_ccm_setkey (ccm , get_cipher_id (ctx -> cipher_type ), ctx -> key , ctx -> key_bitlen );
724
+ if (err ) return err ;
725
+ ctx -> IV_len = MAX (7 , MIN (13 , ctx -> IV_len ));
726
+ err = mbedtls_ccm_starts (ccm , ctx -> operation , ctx -> IV , ctx -> IV_len );
727
+ ctx -> unprocessed_len = 0 ;
728
+ break ;
729
+ }
730
+ #endif
731
+
622
732
#ifdef MBEDTLS_GCM_C
623
733
case SYM_AES_128_GCM :
624
734
case SYM_AES_192_GCM :
@@ -635,7 +745,7 @@ static void free_crypt_cipher_context(CRYPT_CTX *ctx);
635
745
#endif
636
746
{
637
747
mbedtls_gcm_context * gcm = (mbedtls_gcm_context * )ctx -> cipher_ctx ;
638
- err = mbedtls_gcm_setkey (gcm , MBEDTLS_CIPHER_ID_AES , ctx -> key , ctx -> key_bitlen );
748
+ err = mbedtls_gcm_setkey (gcm , get_cipher_id ( ctx -> cipher_type ) , ctx -> key , ctx -> key_bitlen );
639
749
if (err ) return err ;
640
750
err = mbedtls_gcm_starts (gcm , ctx -> operation , ctx -> IV , 16 );
641
751
ctx -> unprocessed_len = 0 ;
0 commit comments