Skip to content

Commit 7d0849b

Browse files
committed
FEAT: added support for GCM (Galois/Counter Mode) cipher mode
1 parent 1b6b88a commit 7d0849b

File tree

5 files changed

+128
-20
lines changed

5 files changed

+128
-20
lines changed

make/rebol3.nest

+18-19
Original file line numberDiff line numberDiff line change
@@ -464,14 +464,6 @@ include-cipher-aria: [
464464
core-files: %core/mbedtls/aria.c
465465
config: MBEDTLS_ARIA_C
466466
]
467-
include-cipher-gcm: [
468-
core-files: %core/mbedtls/gcm.c
469-
config: MBEDTLS_GCM_C
470-
]
471-
include-cipher-cbc: [
472-
; costs cca 1.5kB uncompressed (for AES)
473-
config: MBEDTLS_CIPHER_MODE_CBC
474-
]
475467
include-cipher-chacha20: [
476468
; costs cca 3kB uncompressed
477469
core-files: %core/mbedtls/chacha20.c
@@ -483,16 +475,27 @@ include-cipher-chachapoly: [
483475
config: MBEDTLS_CHACHAPOLY_C
484476
config: MBEDTLS_POLY1305_C
485477
]
478+
include-cipher-mode-gcm: [
479+
core-files: %core/mbedtls/gcm.c
480+
; GCM runs with multiple ciphers and internally uses the generic wrapper
481+
core-files: %core/mbedtls/cipher.c
482+
core-files: %core/mbedtls/cipher_wrap.c
483+
config: MBEDTLS_GCM_C
484+
]
485+
include-cipher-mode-cbc: [
486+
; costs cca 1.5kB uncompressed (for AES)
487+
config: MBEDTLS_CIPHER_MODE_CBC
488+
]
486489

487-
include-cipher-aes-deprecated: [
490+
include-deprecated-cipher-aes: [
488491
; costs cca 5kB uncompressed
489492
config: INCLUDE_AES_DEPRECATED
490493
include: %src/include/deprecated/
491494
core-files: %core/deprecated/u-aes.c
492495
core-files: %core/deprecated/n-crypt-aes.c
493496
]
494497

495-
include-cipher-chacha20-deprecated: [
498+
include-deprecated-cipher-chacha20: [
496499
; costs cca 10kB uncompressed
497500
config: INCLUDE_CHACHA20POLY1305_DEPRECATED
498501
include: %src/include/deprecated/
@@ -537,8 +540,6 @@ include-curves-x: [
537540
config: MBEDTLS_ECP_DP_CURVE448_ENABLED ; costs 7.6kB; together with CURVE25519 8.1!
538541
]
539542

540-
541-
542543
include-cryptography: [
543544
; so far cca 183kB uncompressed (basic AES, all ellyptic curves, rsa, rc4, bignum, entropy )
544545
config: INCLUDE_CRYPTOGRAPHY
@@ -570,9 +571,6 @@ include-cryptography: [
570571
%core/mbedtls/ecdsa.c
571572
%core/mbedtls/ecp.c
572573
%core/mbedtls/ecp_curves.c
573-
574-
; %core/mbedtls/cipher.c
575-
; %core/mbedtls/cipher_wrap.c
576574
]
577575

578576
:include-curves-sec1
@@ -585,12 +583,13 @@ include-cryptography: [
585583

586584
:include-cipher-aria
587585
:include-cipher-camelia
588-
:include-cipher-cbc
589-
;:include-cipher-gcm
590586
:include-cipher-chacha20
591587
:include-cipher-chachapoly
592-
:include-cipher-chacha20-deprecated
593-
:include-cipher-aes-deprecated
588+
:include-cipher-mode-cbc
589+
:include-cipher-mode-gcm
590+
591+
:include-deprecated-cipher-chacha20
592+
:include-deprecated-cipher-aes
594593

595594
:include-codec-crt
596595
:include-codec-der

src/boot/words.reb

+10
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ REBOL [
33
Title: "Canonical words"
44
Rights: {
55
Copyright 2012 REBOL Technologies
6+
Copyright 2012-2022 Rebol Open Source Contributors
67
REBOL is a trademark of REBOL Technologies
78
}
89
License: {
@@ -314,17 +315,26 @@ aes-256-ecb
314315
aes-128-cbc
315316
aes-192-cbc
316317
aes-256-cbc
318+
aes-128-gcm
319+
aes-192-gcm
320+
aes-256-gcm
317321
camellia-128-ecb
318322
camellia-192-ecb
319323
camellia-256-ecb
320324
camellia-128-cbc
321325
camellia-192-cbc
322326
camellia-256-cbc
327+
camellia-128-gcm
328+
camellia-192-gcm
329+
camellia-256-gcm
323330
aria-128-ecb
324331
aria-192-ecb
325332
aria-256-ecb
326333
aria-128-cbc
327334
aria-192-cbc
328335
aria-256-cbc
336+
aria-128-gcm
337+
aria-192-gcm
338+
aria-256-gcm
329339
chacha20
330340
chacha20-poly1305

src/core/n-crypt.c

+16-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
** REBOL [R3] Language Interpreter and Run-time Environment
44
**
55
** Copyright 2012 REBOL Technologies
6-
** Copyright 2012-2021 Rebol Open Source Contributors
6+
** Copyright 2012-2022 Rebol Open Source Contributors
77
** REBOL is a trademark of REBOL Technologies
88
**
99
** Licensed under the Apache License, Version 2.0 (the "License");
@@ -91,6 +91,11 @@ static mbedtls_ctr_drbg_context ctr_drbg;
9191
add_ec_word(SYM_AES_192_CBC)
9292
add_ec_word(SYM_AES_256_CBC)
9393
#endif
94+
#ifdef MBEDTLS_GCM_C
95+
add_ec_word(SYM_AES_128_GCM)
96+
add_ec_word(SYM_AES_192_GCM)
97+
add_ec_word(SYM_AES_256_GCM)
98+
#endif
9499
#ifdef MBEDTLS_CAMELLIA_C
95100
add_ec_word(SYM_CAMELLIA_128_ECB)
96101
add_ec_word(SYM_CAMELLIA_192_ECB)
@@ -100,6 +105,11 @@ static mbedtls_ctr_drbg_context ctr_drbg;
100105
add_ec_word(SYM_CAMELLIA_192_CBC)
101106
add_ec_word(SYM_CAMELLIA_256_CBC)
102107
#endif
108+
#ifdef MBEDTLS_GCM_C
109+
add_ec_word(SYM_CAMELLIA_128_GCM)
110+
add_ec_word(SYM_CAMELLIA_192_GCM)
111+
add_ec_word(SYM_CAMELLIA_256_GCM)
112+
#endif
103113
#endif
104114
#ifdef MBEDTLS_ARIA_C
105115
add_ec_word(SYM_ARIA_128_ECB)
@@ -110,6 +120,11 @@ static mbedtls_ctr_drbg_context ctr_drbg;
110120
add_ec_word(SYM_ARIA_192_CBC)
111121
add_ec_word(SYM_ARIA_256_CBC)
112122
#endif
123+
#ifdef MBEDTLS_GCM_C
124+
add_ec_word(SYM_ARIA_128_GCM)
125+
add_ec_word(SYM_ARIA_192_GCM)
126+
add_ec_word(SYM_ARIA_256_GCM)
127+
#endif
113128
#endif
114129
#ifdef MBEDTLS_CHACHA20_C
115130
add_ec_word(SYM_CHACHA20)

src/core/p-crypt.c

+79
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,37 @@ static void free_crypt_cipher_context(CRYPT_CTX *ctx);
255255
ctx->cipher_block_size = 16;
256256
break;
257257

258+
#ifdef MBEDTLS_GCM_C
259+
case SYM_AES_128_GCM:
260+
case SYM_AES_192_GCM:
261+
case SYM_AES_256_GCM:
262+
#ifdef MBEDTLS_CAMELLIA_C
263+
case SYM_CAMELLIA_128_GCM:
264+
case SYM_CAMELLIA_192_GCM:
265+
case SYM_CAMELLIA_256_GCM:
266+
#endif
267+
#ifdef MBEDTLS_ARIA_C
268+
case SYM_ARIA_128_GCM:
269+
case SYM_ARIA_192_GCM:
270+
case SYM_ARIA_256_GCM:
271+
#endif
272+
if (ctx->cipher_ctx == NULL)
273+
ctx->cipher_ctx = malloc(sizeof(mbedtls_gcm_context));
274+
mbedtls_gcm_init((mbedtls_gcm_context *)ctx->cipher_ctx);
275+
switch (type) {
276+
case SYM_AES_128_GCM:
277+
case SYM_ARIA_128_GCM:
278+
case SYM_CAMELLIA_128_GCM: ctx->key_bitlen = 128; break;
279+
case SYM_AES_192_GCM:
280+
case SYM_ARIA_192_GCM:
281+
case SYM_CAMELLIA_192_GCM: ctx->key_bitlen = 192; break;
282+
case SYM_AES_256_GCM:
283+
case SYM_ARIA_256_GCM:
284+
case SYM_CAMELLIA_256_GCM: ctx->key_bitlen = 256; break;
285+
}
286+
ctx->cipher_block_size = 0;
287+
break;
288+
#endif
258289

259290
#ifdef MBEDTLS_CAMELLIA_C
260291
case SYM_CAMELLIA_128_ECB:
@@ -442,6 +473,30 @@ static void free_crypt_cipher_context(CRYPT_CTX *ctx);
442473
break;
443474
#endif
444475

476+
#ifdef MBEDTLS_GCM_C
477+
case SYM_AES_128_GCM:
478+
case SYM_AES_192_GCM:
479+
case SYM_AES_256_GCM:
480+
#ifdef MBEDTLS_CAMELLIA_C
481+
case SYM_CAMELLIA_128_GCM:
482+
case SYM_CAMELLIA_192_GCM:
483+
case SYM_CAMELLIA_256_GCM:
484+
#endif
485+
#ifdef MBEDTLS_ARIA_C
486+
case SYM_ARIA_128_GCM:
487+
case SYM_ARIA_192_GCM:
488+
case SYM_ARIA_256_GCM:
489+
#endif
490+
{
491+
size_t out_bytes = 0;
492+
err = mbedtls_gcm_update((mbedtls_gcm_context *)ctx->cipher_ctx, input, len, BIN_TAIL(bin), len, &out_bytes);
493+
if (err) return err;
494+
SERIES_TAIL(bin) += out_bytes;
495+
input += out_bytes;
496+
break;
497+
}
498+
#endif
499+
445500
#ifdef MBEDTLS_CAMELLIA_C
446501
case SYM_CAMELLIA_128_ECB:
447502
case SYM_CAMELLIA_192_ECB:
@@ -571,6 +626,29 @@ static void free_crypt_cipher_context(CRYPT_CTX *ctx);
571626
}
572627
break;
573628

629+
#ifdef MBEDTLS_GCM_C
630+
case SYM_AES_128_GCM:
631+
case SYM_AES_192_GCM:
632+
case SYM_AES_256_GCM:
633+
#ifdef MBEDTLS_CAMELLIA_C
634+
case SYM_CAMELLIA_128_GCM:
635+
case SYM_CAMELLIA_192_GCM:
636+
case SYM_CAMELLIA_256_GCM:
637+
#endif
638+
#ifdef MBEDTLS_ARIA_C
639+
case SYM_ARIA_128_GCM:
640+
case SYM_ARIA_192_GCM:
641+
case SYM_ARIA_256_GCM:
642+
#endif
643+
{
644+
mbedtls_gcm_context *gcm = (mbedtls_gcm_context *)ctx->cipher_ctx;
645+
err = mbedtls_gcm_setkey(gcm, MBEDTLS_CIPHER_ID_AES, ctx->key, ctx->key_bitlen);
646+
if (err) return err;
647+
err = mbedtls_gcm_starts(gcm, ctx->operation, ctx->IV, 16);
648+
ctx->unprocessed_len = 0;
649+
break;
650+
}
651+
#endif
574652

575653
#ifdef MBEDTLS_CAMELLIA_C
576654
case SYM_CAMELLIA_128_ECB:
@@ -692,6 +770,7 @@ static void free_crypt_cipher_context(CRYPT_CTX *ctx);
692770
// we have enough data to call crypt
693771
Crypt_Crypt(ctx, input, len, &olen);
694772
if (olen > len) return CRYPT_ERROR_BAD_PROCESSED_SIZE;
773+
input += olen;
695774
len -= olen;
696775
}
697776
// test if there are some unprocessed data

src/include/sys-crypt.h

+5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
** REBOL [R3] Language Interpreter and Run-time Environment
44
**
55
** Copyright 2012 REBOL Technologies
6+
** Copyright 2012-2022 Rebol Open Source Contributors
67
** REBOL is a trademark of REBOL Technologies
78
**
89
** Licensed under the Apache License, Version 2.0 (the "License");
@@ -61,6 +62,10 @@ typedef mbedtls_chachapoly_context CHACHAPOLY_CTX;
6162
#define CHACHAPOLY_STATE_CIPHERTEXT (2)
6263
#endif
6364

65+
#ifdef MBEDTLS_GCM_C
66+
#include "mbedtls/gcm.h"
67+
#endif
68+
6469

6570
typedef mbedtls_rsa_context RSA_CTX;
6671
typedef mbedtls_dhm_context DHM_CTX;

0 commit comments

Comments
 (0)