Skip to content

Commit e745186

Browse files
committed
CHANGE: crypt port (work in progress)
1 parent f46f9ba commit e745186

38 files changed

+12494
-180
lines changed

make/rebol3.nest

+105-6
Original file line numberDiff line numberDiff line change
@@ -453,16 +453,96 @@ include-iconv: [
453453
]
454454

455455
;- native cryptography:
456+
include-cipher-camelia: [
457+
core-files: %core/mbedtls/camellia.c
458+
config: MBEDTLS_CAMELLIA_C
459+
]
460+
include-cipher-gcm: [
461+
core-files: %core/mbedtls/gcm.c
462+
config: MBEDTLS_GCM_C
463+
]
464+
include-cipher-cbc: [
465+
; costs cca 1.5kB uncompressed (for AES)
466+
config: MBEDTLS_CIPHER_MODE_CBC
467+
]
468+
include-cipher-chacha20: [
469+
; costs cca 3kB uncompressed
470+
core-files: %core/mbedtls/chacha20.c
471+
config: MBEDTLS_CHACHA20_C
472+
]
473+
include-cipher-chachapoly: [
474+
core-files: %core/mbedtls/chachapoly.c
475+
core-files: %core/mbedtls/poly1305.c
476+
config: MBEDTLS_CHACHAPOLY_C
477+
config: MBEDTLS_POLY1305_C
478+
]
479+
480+
include-cipher-aes-deprecated: [
481+
; costs cca 5kB uncompressed
482+
config: INCLUDE_AES_DEPRECATED
483+
include: %src/include/deprecated/
484+
core-files: %core/deprecated/u-aes.c
485+
core-files: %core/deprecated/n-crypt-aes.c
486+
]
487+
488+
include-cipher-chacha20-deprecated: [
489+
; costs cca 10kB uncompressed
490+
config: INCLUDE_CHACHA20POLY1305_DEPRECATED
491+
include: %src/include/deprecated/
492+
core-files: %core/deprecated/u-chacha20.c
493+
core-files: %core/deprecated/u-poly1305.c
494+
]
495+
496+
include-rsa: [
497+
; costs cca 12kB uncompressed
498+
config: INCLUDE_RSA
499+
core-files: %core/mbedtls/rsa.c
500+
core-files: %core/mbedtls/rsa_alt_helpers.c
501+
]
502+
include-rc4: [
503+
; costs cca 0.5kB uncompressed
504+
config: INCLUDE_RC4
505+
core-files: %core/u-rc4.c
506+
]
507+
508+
include-curves-sec1: [
509+
; curves defined by FIPS 186-4 and SEC1
510+
config: MBEDTLS_ECP_DP_SECP192R1_ENABLED ; costs 2kB
511+
config: MBEDTLS_ECP_DP_SECP224R1_ENABLED ; costs 2.5kB
512+
config: MBEDTLS_ECP_DP_SECP256R1_ENABLED ; costs 2.5kB
513+
config: MBEDTLS_ECP_DP_SECP384R1_ENABLED
514+
config: MBEDTLS_ECP_DP_SECP521R1_ENABLED
515+
]
516+
include-curves-koblitz: [
517+
; "Koblitz" curves
518+
config: MBEDTLS_ECP_DP_SECP192K1_ENABLED
519+
config: MBEDTLS_ECP_DP_SECP224K1_ENABLED
520+
config: MBEDTLS_ECP_DP_SECP256K1_ENABLED
521+
]
522+
include-curves-brainpool: [
523+
; "Brainpool" curves
524+
config: MBEDTLS_ECP_DP_BP256R1_ENABLED
525+
config: MBEDTLS_ECP_DP_BP384R1_ENABLED
526+
config: MBEDTLS_ECP_DP_BP512R1_ENABLED
527+
]
528+
include-curves-x: [
529+
config: MBEDTLS_ECP_DP_CURVE25519_ENABLED ; costs 7.6kB
530+
config: MBEDTLS_ECP_DP_CURVE448_ENABLED ; costs 7.6kB; together with CURVE25519 8.1!
531+
]
532+
533+
534+
456535
include-cryptography: [
536+
; so far cca 183kB uncompressed (basic AES, all ellyptic curves, rsa, rc4, bignum, entropy )
457537
config: INCLUDE_CRYPTOGRAPHY
538+
458539
core-files: [
459540
%core/n-crypt.c
460-
%core/u-aes.c
541+
461542
;%core/deprecated/u-bigint.c ;needed for RSA abd DH which is needed in TLS protocol (HTTPS)
462-
%core/u-chacha20.c
463543
;%core/deprecated/u-dh.c
464-
%core/u-poly1305.c
465-
%core/u-rc4.c
544+
;%core/u-poly1305.c
545+
466546
;%core/deprecated/u-rsa.c
467547
;%core/deprecated/u-uECC.c
468548

@@ -474,8 +554,7 @@ include-cryptography: [
474554
%core/mbedtls/md.c
475555
%core/mbedtls/oid.c
476556
%core/mbedtls/bignum.c
477-
%core/mbedtls/rsa.c
478-
%core/mbedtls/rsa_alt_helpers.c
557+
479558
%core/mbedtls/constant_time.c
480559
%core/mbedtls/ctr_drbg.c
481560
%core/mbedtls/entropy.c
@@ -484,7 +563,27 @@ include-cryptography: [
484563
%core/mbedtls/ecdsa.c
485564
%core/mbedtls/ecp.c
486565
%core/mbedtls/ecp_curves.c
566+
567+
%core/mbedtls/cipher.c
568+
%core/mbedtls/cipher_wrap.c
487569
]
570+
571+
:include-curves-sec1
572+
:include-curves-koblitz
573+
:include-curves-brainpool
574+
:include-curves-x
575+
576+
:include-rsa
577+
:include-rc4
578+
579+
:include-cipher-camelia
580+
:include-cipher-cbc
581+
;:include-cipher-gcm
582+
;:include-cipher-chacha20
583+
;:include-cipher-chachapoly
584+
:include-cipher-chacha20-deprecated
585+
:include-cipher-aes-deprecated
586+
488587
:include-codec-crt
489588
:include-codec-der
490589
:include-codec-pkix

src/boot/sysobj.reb

+10-16
Original file line numberDiff line numberDiff line change
@@ -62,19 +62,10 @@ catalog: object [
6262
checksums: [adler32 crc24 crc32 tcp md4 md5 sha1 sha224 sha256 sha384 sha512 ripemd160]
6363
compressions: [gzip deflate zlib lzma crush]
6464
elliptic-curves: [
65-
secp192r1 ; 192-bit curve defined by FIPS 186-4 and SEC1
66-
secp224r1 ; 224-bit curve defined by FIPS 186-4 and SEC1
67-
secp256r1 ; 256-bit curve defined by FIPS 186-4 and SEC1
68-
secp384r1 ; 384-bit curve defined by FIPS 186-4 and SEC1
69-
secp521r1 ; 521-bit curve defined by FIPS 186-4 and SEC1
70-
bp256r1 ; 256-bit Brainpool curve
71-
bp384r1 ; 384-bit Brainpool curve
72-
bp512r1 ; 512-bit Brainpool curve
73-
curve25519 ; Curve25519
74-
secp192k1 ; 192-bit "Koblitz" curve
75-
secp224k1 ; 224-bit "Koblitz" curve
76-
secp256k1 ; 256-bit "Koblitz" curve
77-
curve448 ; Curve448
65+
; will be filled on boot from `Init_Crypt` in `n-crypt.c`
66+
]
67+
ciphers: [
68+
; will be filled on boot from `Init_Crypt` in `n-crypt.c`
7869
]
7970
]
8071

@@ -283,9 +274,12 @@ standard: object [
283274
]
284275

285276
port-spec-crypt: make port-spec-head [
286-
scheme: 'crypt
287-
algorithm: 'aes
288-
direction: 'encrypt
277+
scheme: 'crypt
278+
direction: 'encrypt
279+
algorithm:
280+
;hash: 'SHA1
281+
key: ;#{E76B2413958B00E193}
282+
iv: none
289283
]
290284

291285
port-spec-midi: make port-spec-head [

src/core/c-error.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,7 @@ static REBOL_STATE Top_State; // Boot var: holds error state during boot
633633
val = Get_Object(spec, STD_PORT_SPEC_HEAD_REF); // most informative
634634
if (IS_NONE(val)) val = Get_Object(spec, STD_PORT_SPEC_HEAD_TITLE);
635635

636-
DS_PUSH_INTEGER(err_code);
636+
DS_PUSH_INTEGER(-err_code);
637637
Trap2(errnum, val, DS_TOP);
638638
}
639639

src/core/c-port.c

+5-2
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,7 @@ xx*/ REBINT Wait_Device(REBREQ *req, REBCNT timeout)
517517
**
518518
***********************************************************************/
519519

520-
#define MAX_SCHEMES 12 // max native schemes
520+
#define MAX_SCHEMES 13 // max native schemes
521521

522522
typedef struct rebol_scheme_actions {
523523
REBCNT sym;
@@ -619,7 +619,7 @@ SCHEME_ACTIONS *Scheme_Actions; // Initial Global (not threaded)
619619
**
620620
** In mezz-ports.reb add a make-scheme.
621621
** Add an Init_*_Scheme() here.
622-
** Be sure host-devices.c has the device enabled.
622+
** Be sure host-device.c has the device enabled.
623623
**
624624
***********************************************************************/
625625
{
@@ -639,6 +639,9 @@ SCHEME_ACTIONS *Scheme_Actions; // Initial Global (not threaded)
639639
#ifdef INCLUDE_MIDI_DEVICE
640640
Init_MIDI_Scheme();
641641
#endif
642+
#ifdef INCLUDE_CRYPTOGRAPHY
643+
Init_Crypt_Scheme();
644+
#endif
642645
}
643646

644647
/***********************************************************************

src/core/deprecated/n-crypt-aes.c

+139
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
/***********************************************************************
2+
**
3+
** REBOL [R3] Language Interpreter and Run-time Environment
4+
**
5+
** Copyright 2012 REBOL Technologies
6+
** Copyright 2012-2021 Rebol Open Source Contributors
7+
** REBOL is a trademark of REBOL Technologies
8+
**
9+
** Licensed under the Apache License, Version 2.0 (the "License");
10+
** you may not use this file except in compliance with the License.
11+
** You may obtain a copy of the License at
12+
**
13+
** http://www.apache.org/licenses/LICENSE-2.0
14+
**
15+
** Unless required by applicable law or agreed to in writing, software
16+
** distributed under the License is distributed on an "AS IS" BASIS,
17+
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
** See the License for the specific language governing permissions and
19+
** limitations under the License.
20+
**
21+
************************************************************************
22+
**
23+
** Module: n-crypt-aes.c
24+
** Summary: native functions for old AES code
25+
** Section: natives
26+
** Author: Oldes, Cyphre
27+
** Notes:
28+
**
29+
***********************************************************************/
30+
31+
#include "sys-core.h"
32+
#include "sys-aes.h"
33+
34+
35+
/***********************************************************************
36+
**
37+
*/ REBNATIVE(aes)
38+
/*
39+
// aes: native [
40+
// "Encrypt/decrypt data using AES algorithm. Returns stream cipher context handle or encrypted/decrypted data."
41+
// /key "Provided only for the first time to get stream HANDLE!"
42+
// crypt-key [binary!] "Crypt key (16 or 32 bytes)."
43+
// iv [none! binary!] "Optional initialization vector (16 bytes)."
44+
// /decrypt "Use the crypt-key for decryption (default is to encrypt)"
45+
// /stream
46+
// ctx [handle!] "Stream cipher context."
47+
// data [binary!] "Data to encrypt/decrypt."
48+
// ]
49+
***********************************************************************/
50+
{
51+
REBOOL ref_key = D_REF(1);
52+
REBVAL *val_crypt_key = D_ARG(2);
53+
REBVAL *val_iv = D_ARG(3);
54+
REBOOL ref_decrypt = D_REF(4);
55+
REBOOL ref_stream = D_REF(5);
56+
REBVAL *val_ctx = D_ARG(6);
57+
REBVAL *val_data = D_ARG(7);
58+
59+
REBVAL *ret = D_RET;
60+
REBINT len, pad_len;
61+
62+
//TODO: could be optimized by reusing the handle
63+
64+
if (ref_key) {
65+
//key defined - setup new context
66+
67+
uint8_t iv[AES_IV_SIZE];
68+
69+
if (IS_BINARY(val_iv)) {
70+
if (VAL_LEN(val_iv) < AES_IV_SIZE) {
71+
return R_NONE;
72+
}
73+
memcpy(iv, VAL_BIN_AT(val_iv), AES_IV_SIZE);
74+
} else {
75+
//TODO: Use ECB encryption if IV is not specified
76+
memset(iv, 0, AES_IV_SIZE);
77+
}
78+
79+
len = VAL_LEN(val_crypt_key) << 3;
80+
81+
if (len != 128 && len != 256) {
82+
return R_NONE;
83+
}
84+
85+
MAKE_HANDLE(ret, SYM_AES);
86+
87+
AES_set_key(
88+
(AES_CTX*)VAL_HANDLE_CONTEXT_DATA(ret),
89+
VAL_BIN_AT(val_crypt_key),
90+
(const uint8_t *)iv,
91+
(len == 128) ? AES_MODE_128 : AES_MODE_256
92+
);
93+
94+
if (ref_decrypt) AES_convert_key((AES_CTX*)VAL_HANDLE_CONTEXT_DATA(ret));
95+
96+
} else if(ref_stream) {
97+
98+
if (NOT_VALID_CONTEXT_HANDLE(val_ctx, SYM_AES)) {
99+
Trap0(RE_INVALID_HANDLE);
100+
return R_NONE;
101+
}
102+
AES_CTX *aes_ctx = (AES_CTX *)VAL_HANDLE_CONTEXT_DATA(val_ctx);
103+
104+
len = VAL_LEN(val_data);
105+
if (len == 0) return R_NONE;
106+
107+
pad_len = (((len - 1) >> 4) << 4) + AES_BLOCKSIZE;
108+
109+
REBYTE *data = VAL_BIN_AT(val_data);
110+
REBYTE *pad_data;
111+
112+
if (len < pad_len) {
113+
// make new data input with zero-padding
114+
//TODO: instead of making new data, the original could be extended with padding.
115+
pad_data = (REBYTE*)MAKE_MEM(pad_len);
116+
memset(pad_data, 0, pad_len);
117+
memcpy(pad_data, data, len);
118+
data = pad_data;
119+
}
120+
else {
121+
pad_data = NULL;
122+
}
123+
124+
REBSER *binaryOut = Make_Binary(pad_len);
125+
126+
if (aes_ctx->key_mode == AES_MODE_DECRYPT) {
127+
AES_cbc_decrypt(aes_ctx, data, BIN_HEAD(binaryOut), pad_len);
128+
}
129+
else {
130+
AES_cbc_encrypt(aes_ctx, data, BIN_HEAD(binaryOut), pad_len);
131+
}
132+
if (pad_data) FREE_MEM(pad_data);
133+
134+
SET_BINARY(ret, binaryOut);
135+
VAL_TAIL(ret) = pad_len;
136+
137+
}
138+
return R_RET;
139+
}
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)