Skip to content

Commit 39957a4

Browse files
committed
FEAT: updated TLS protocol to support TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 cipher suite
1 parent 6ec6976 commit 39957a4

File tree

5 files changed

+196
-163
lines changed

5 files changed

+196
-163
lines changed

src/core/n-crypt.c

+6-14
Original file line numberDiff line numberDiff line change
@@ -894,6 +894,7 @@ typedef struct {
894894
chacha20poly1305_ctx *chacha;
895895
unsigned char poly1305_key[POLY1305_KEYLEN];
896896
size_t aad_size;
897+
REBU64 sequence = 0;
897898

898899
if (ref_init) {
899900
ctx_ser = Make_Series(sizeof(chacha20poly1305_ctx), (REBCNT)1, FALSE);
@@ -911,19 +912,16 @@ typedef struct {
911912
Trap1(RE_INVALID_DATA, val_remote_key);
912913
chacha20_keysetup(&chacha->remote_chacha, VAL_BIN_AT(val_remote_key), len);
913914

914-
chacha->local_sequence = 0;
915-
chacha->remote_sequence = 0;
916-
917915
len = VAL_LEN(val_local_iv);
918916
if (!(len == 12 || len == 8))
919917
Trap1(RE_INVALID_DATA, val_local_iv);
920-
chacha20_ivsetup(&chacha->local_chacha, VAL_BIN_AT(val_local_iv), len, 1, (u8 *)&chacha->local_sequence);
918+
chacha20_ivsetup(&chacha->local_chacha, VAL_BIN_AT(val_local_iv), len, 1, (u8 *)&sequence);
921919
memcpy(chacha->local_iv, VAL_BIN_AT(val_local_iv), len);
922920

923921
len = VAL_LEN(val_remote_iv);
924922
if (!(len == 12 || len == 8))
925923
Trap1(RE_INVALID_DATA, val_remote_iv);
926-
chacha20_ivsetup(&chacha->remote_chacha, VAL_BIN_AT(val_remote_iv), len, 1, (u8 *)&chacha->remote_sequence);
924+
chacha20_ivsetup(&chacha->remote_chacha, VAL_BIN_AT(val_remote_iv), len, 1, (u8 *)&sequence);
927925
memcpy(chacha->remote_iv, VAL_BIN_AT(val_remote_iv), len);
928926
return R_ARG1;
929927
}
@@ -936,7 +934,7 @@ typedef struct {
936934
chacha = (chacha20poly1305_ctx*)ctx_ser->data;
937935

938936
if (ref_encrypt) {
939-
chacha20_ivsetup(&chacha->local_chacha, chacha->local_iv, 12, 1, (u8 *)&chacha->local_sequence);
937+
chacha20_ivsetup(&chacha->local_chacha, chacha->local_iv, 12, 1, VAL_BIN_AT(val_local_aad));
940938
chacha20_poly1305_key(&chacha->local_chacha, poly1305_key);
941939
//puts("poly1305_key:"); Dump_Bytes(poly1305_key, POLY1305_KEYLEN);
942940

@@ -1006,18 +1004,12 @@ typedef struct {
10061004
poly1305_finish(&aead_ctx, mac_tag);
10071005

10081006
if (!poly1305_verify(mac_tag, VAL_BIN_TAIL(val_cipher) - POLY1305_TAGLEN)) {
1009-
puts("MAC verification failed!");
1010-
}
1011-
else {
1012-
puts("MAC OK!");
1007+
//puts("MAC verification failed!");
1008+
return R_NONE;
10131009
}
10141010

10151011
//puts("mac result:"); Dump_Bytes(mac_tag, POLY1305_TAGLEN);
10161012

1017-
chacha->remote_sequence++;
1018-
1019-
1020-
10211013
SERIES_TAIL(ctx_ser) = len;
10221014
SET_BINARY(val_ctx, ctx_ser);
10231015
}

src/core/u-chacha20.c

+3-1
Original file line numberDiff line numberDiff line change
@@ -307,11 +307,13 @@ int chacha20_poly1305_aead(struct chacha20_ctx *ctx, u8 *pt, u32 len, u8 *aad,
307307
if (rem)
308308
poly1305_update(&aead_ctx, zeropad, 16 - rem);
309309

310-
U32TO8_LE(&trail[0], aad_len);
310+
U32TO8_LE(&trail[0], (aad_len == 5) ? 5 : 13);
311311
*(int *)&trail[4] = 0;
312312
U32TO8_LE(&trail[8], len);
313313
*(int *)&trail[12] = 0;
314314

315+
//puts("trail:"); Dump_Bytes(trail, 16);
316+
315317
poly1305_update(&aead_ctx, trail, 16);
316318
poly1305_finish(&aead_ctx, out + len);
317319

src/core/u-poly1305.c

+3
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ poly1305_auth(u8 mac[16], const u8 *m, size_t bytes, const u8 key[32]) {
8686
int
8787
poly1305_verify(const u8 mac1[16], const u8 mac2[16]) {
8888
size_t i;
89+
90+
//puts("mac1:"); Dump_Bytes(mac1, 16);
91+
//puts("mac2:"); Dump_Bytes(mac2, 16);
8992
unsigned int dif = 0;
9093
for (i = 0; i < 16; i++)
9194
dif |= (mac1[i] ^ mac2[i]);

src/include/sys-chacha20.h

-2
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,8 @@ typedef struct chacha20_ctx chacha20_ctx;
5151

5252
typedef struct chacha20poly1305_ctx {
5353
chacha20_ctx local_chacha;
54-
uint64_t local_sequence;
5554
uint8_t local_iv[12];
5655
chacha20_ctx remote_chacha;
57-
uint64_t remote_sequence;
5856
uint8_t remote_iv[12];
5957
} chacha20poly1305_ctx;
6058

0 commit comments

Comments
 (0)