-
Notifications
You must be signed in to change notification settings - Fork 90
/
tlse.c
12396 lines (11173 loc) · 481 KB
/
tlse.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/********************************************************************************
Copyright (c) 2016-2024, Eduard Suica
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this
list of conditions and the following disclaimer in the documentation and/or other
materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
********************************************************************************/
#ifndef TLSE_C
#define TLSE_C
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <time.h>
#ifdef _WIN32
#ifdef SSL_COMPATIBLE_INTERFACE
#include <winsock2.h>
#endif
#include <windows.h>
#include <wincrypt.h>
#ifndef strcasecmp
#define strcasecmp stricmp
#endif
#else
// hton* and ntoh* functions
#include <arpa/inet.h>
#include <unistd.h>
#include <errno.h>
#endif
#ifdef TLS_AMALGAMATION
#ifdef I
#pragma push_macro("I")
#define TLS_I_MACRO
#undef I
#endif
#include "libtomcrypt.c"
#ifdef TLS_I_MACRO
#pragma pop_macro("I")
#undef TLS_I_MACRO
#endif
#else
#include <tomcrypt.h>
#endif
#if (CRYPT <= 0x0117)
#define LTC_PKCS_1_EMSA LTC_LTC_PKCS_1_EMSA
#define LTC_PKCS_1_V1_5 LTC_LTC_PKCS_1_V1_5
#define LTC_PKCS_1_PSS LTC_LTC_PKCS_1_PSS
#endif
#ifdef WITH_KTLS
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/tcp.h>
// should get /usr/include/linux/tls.h (linux headers)
// rename it to ktls.h and add it to your project
#include "ktls.h"
// or just include tls.h instead of ktls.h
// #include "linux/tls.h"
#endif
#include "tlse.h"
#ifdef TLS_CURVE25519
#include "curve25519.c"
#endif
// using ChaCha20 implementation by D. J. Bernstein
#ifndef TLS_FORWARD_SECRECY
#undef TLS_ECDSA_SUPPORTED
#endif
#ifndef TLS_ECDSA_SUPPORTED
// disable client ECDSA if not supported
#undef TLS_CLIENT_ECDSA
#endif
#define TLS_DH_DEFAULT_P "87A8E61DB4B6663CFFBBD19C651959998CEEF608660DD0F25D2CEED4435E3B00E00DF8F1D61957D4FAF7DF4561B2AA3016C3D91134096FAA3BF4296D830E9A7C209E0C6497517ABD5A8A9D306BCF67ED91F9E6725B4758C022E0B1EF4275BF7B6C5BFC11D45F9088B941F54EB1E59BB8BC39A0BF12307F5C4FDB70C581B23F76B63ACAE1CAA6B7902D52526735488A0EF13C6D9A51BFA4AB3AD8347796524D8EF6A167B5A41825D967E144E5140564251CCACB83E6B486F6B3CA3F7971506026C0B857F689962856DED4010ABD0BE621C3A3960A54E710C375F26375D7014103A4B54330C198AF126116D2276E11715F693877FAD7EF09CADB094AE91E1A1597"
#define TLS_DH_DEFAULT_G "3FB32C9B73134D0B2E77506660EDBD484CA7B18F21EF205407F4793A1A0BA12510DBC15077BE463FFF4FED4AAC0BB555BE3A6C1B0C6B47B1BC3773BF7E8C6F62901228F8C28CBB18A55AE31341000A650196F931C77A57F2DDF463E5E9EC144B777DE62AAAB8A8628AC376D282D6ED3864E67982428EBC831D14348F6F2F9193B5045AF2767164E1DFC967C1FB3F2E55A4BD1BFFE83B9C80D052B985D182EA0ADB2A3B7313D3FE14C8484B1E052588B9B7D2BBD2DF016199ECD06E1557CD0915B3353BBB64E0EC377FD028370DF92B52C7891428CDC67EB6184B523D1DB246C32F63078490F00EF8D647D148D47954515E2327CFEF98C582664B4C0F6CC41659"
#define TLS_DHE_KEY_SIZE 2048
// you should never use weak DH groups (1024 bits)
// but if you have old devices (like grandstream ip phones)
// that can't handle 2048bit DHE, uncomment next lines
// and define TLS_WEAK_DH_LEGACY_DEVICES
// #ifdef TLS_WEAK_DH_LEGACY_DEVICES
// #define TLS_DH_DEFAULT_P "B10B8F96A080E01DDE92DE5EAE5D54EC52C99FBCFB06A3C69A6A9DCA52D23B616073E28675A23D189838EF1E2EE652C013ECB4AEA906112324975C3CD49B83BFACCBDD7D90C4BD7098488E9C219A73724EFFD6FAE5644738FAA31A4FF55BCCC0A151AF5F0DC8B4BD45BF37DF365C1A65E68CFDA76D4DA708DF1FB2BC2E4A4371"
// #define TLS_DH_DEFAULT_G "A4D1CBD5C3FD34126765A442EFB99905F8104DD258AC507FD6406CFF14266D31266FEA1E5C41564B777E690F5504F213160217B4B01B886A5E91547F9E2749F4D7FBD7D3B9A92EE1909D0D2263F80A76A6A24C087A091F531DBF0A0169B6A28AD662A4D18E73AFA32D779D5918D08BC8858F4DCEF97C2A24855E6EEB22B3B2E5"
// #define TLS_DHE_KEY_SIZE 1024
// #endif
#ifndef TLS_MALLOC
#define TLS_MALLOC(size) malloc(size)
#endif
#ifndef TLS_REALLOC
#define TLS_REALLOC(ptr, size) realloc(ptr, size)
#endif
#ifndef TLS_FREE
#define TLS_FREE(ptr) if (ptr) free(ptr)
#endif
#define TLS_ERROR(err, statement) if (err) statement;
#ifdef DEBUG
#define DEBUG_PRINT(...) fprintf(stderr, __VA_ARGS__)
#define DEBUG_DUMP_HEX(buf, len) {if (buf) { int _i_; for (_i_ = 0; _i_ < len; _i_++) { DEBUG_PRINT("%02X ", (unsigned int)(buf)[_i_]); } } else { fprintf(stderr, "(null)"); } }
#define DEBUG_INDEX(fields) print_index(fields)
#define DEBUG_DUMP(buf, length) fwrite(buf, 1, length, stderr);
#define DEBUG_DUMP_HEX_LABEL(title, buf, len) {fprintf(stderr, "%s (%i): ", title, (int)len); DEBUG_DUMP_HEX(buf, len); fprintf(stderr, "\n");}
#else
#define DEBUG_PRINT(...) { }
#define DEBUG_DUMP_HEX(buf, len) { }
#define DEBUG_INDEX(fields) { }
#define DEBUG_DUMP(buf, length) { }
#define DEBUG_DUMP_HEX_LABEL(title, buf, len) { }
#endif
#ifndef htonll
#define htonll(x) ((1==htonl(1)) ? (x) : ((uint64_t)htonl((x) & 0xFFFFFFFF) << 32) | htonl((x) >> 32))
#endif
#ifndef ntohll
#define ntohll(x) ((1==ntohl(1)) ? (x) : ((uint64_t)ntohl((x) & 0xFFFFFFFF) << 32) | ntohl((x) >> 32))
#endif
#define TLS_CHANGE_CIPHER 0x14
#define TLS_ALERT 0x15
#define TLS_HANDSHAKE 0x16
#define TLS_APPLICATION_DATA 0x17
#define TLS_SERIALIZED_OBJECT 0xFE
#define TLS_CLIENT_HELLO_MINSIZE 41
#define TLS_CLIENT_RANDOM_SIZE 32
#define TLS_SERVER_RANDOM_SIZE 32
#define TLS_MAX_SESSION_ID 32
#define TLS_SHA256_MAC_SIZE 32
#define TLS_SHA1_MAC_SIZE 20
#define TLS_SHA384_MAC_SIZE 48
#define TLS_MAX_MAC_SIZE TLS_SHA384_MAC_SIZE
// 160
#define TLS_MAX_KEY_EXPANSION_SIZE 192
// 512bits (sha256) = 64 bytes
#define TLS_MAX_HASH_LEN 64
#define TLS_AES_IV_LENGTH 16
#define TLS_AES_BLOCK_SIZE 16
#define TLS_AES_GCM_IV_LENGTH 4
#define TLS_13_AES_GCM_IV_LENGTH 12
#define TLS_GCM_TAG_LEN 16
#define TLS_MAX_TAG_LEN 16
#define TLS_MIN_FINISHED_OPAQUE_LEN 12
#define TLS_BLOB_INCREMENT 0xFFF
#define TLS_ASN1_MAXLEVEL 0xFF
#define DTLS_COOKIE_SIZE 32
#define DTLS_MAX_FRAGMENT_SIZE 0x40000
#define TLS_MAX_SHA_SIZE 48
// 16(md5) + 20(sha1)
#define TLS_V11_HASH_SIZE 36
#define TLS_MAX_HASH_SIZE TLS_MAX_SHA_SIZE
// 16(md5) + 20(sha1)
#define TLS_MAX_RSA_KEY 2048
#define TLS_MAXTLS_APP_SIZE 0x4000
// max 1 second sleep
#define TLS_MAX_ERROR_SLEEP_uS 1000000
// max 5 seconds context sleep
#define TLS_MAX_ERROR_IDLE_S 5
#define TLS_V13_MAX_KEY_SIZE 32
#define TLS_V13_MAX_IV_SIZE 12
#define VERSION_SUPPORTED(version, err) if ((version != TLS_V13) && (version != TLS_V12) && (version != TLS_V11) && (version != TLS_V10) && (version != DTLS_V13) && (version != DTLS_V12) && (version != DTLS_V10)) { if ((version == SSL_V30) && (context->connection_status == 0)) { version = TLS_V12; } else { DEBUG_PRINT("UNSUPPORTED TLS VERSION %x\n", (int)version); return err;} }
#define CHECK_SIZE(size, buf_size, err) if (((int)(size) > (int)(buf_size)) || ((int)(buf_size) < 0)) { DEBUG_PRINT("[EXPECTED AT LEAST %i IN BUFFER OF SIZE %i]\n", (int)(size), (int)(buf_size)); return err; }
#define TLS_IMPORT_CHECK_SIZE(buf_pos, size, buf_size) if (((int)size > (int)buf_size - buf_pos) || ((int)buf_pos > (int)buf_size)) { DEBUG_PRINT("IMPORT ELEMENT SIZE ERROR\n"); tls_destroy_context(context); return NULL; }
#define CHECK_HANDSHAKE_STATE(context, n, limit) { if (context->hs_messages[n] >= limit) { if (context->dtls) { DEBUG_PRINT("* REPEATED MESSAGE, RE-HASHING\n"); _private_dtls_rehash(context, type); context->hs_messages[n]++;} else { DEBUG_PRINT("* UNEXPECTED MESSAGE (%i)\n", (int)n); payload_res = TLS_UNEXPECTED_MESSAGE; break; } } context->hs_messages[n]++; }
#define TLS_24_BIT(buf, index, val) { unsigned int u_val = (unsigned int)val; buf[index] = u_val / 0x10000; u_val %= 0x10000; buf[index + 1] = u_val / 0x100; u_val %= 0x100; buf[index + 2] = u_val; }
#if CRYPT >= 0x0118
#define TLS_TOMCRYPT_PRIVATE_DP(key) (&((key)->dp))
#define TLS_TOMCRYPT_PRIVATE_SET_INDEX(key, k_idx)
#else
#define TLS_TOMCRYPT_PRIVATE_DP(key) ((key)->dp)
#define TLS_TOMCRYPT_PRIVATE_SET_INDEX(key, k_idx) key->idx = k_idx
#endif
#ifdef TLS_WITH_CHACHA20_POLY1305
#define TLS_CHACHA20_IV_LENGTH 12
// ChaCha20 implementation by D. J. Bernstein
// Public domain.
#define CHACHA_MINKEYLEN 16
#define CHACHA_NONCELEN 8
#define CHACHA_NONCELEN_96 12
#define CHACHA_CTRLEN 8
#define CHACHA_CTRLEN_96 4
#define CHACHA_STATELEN (CHACHA_NONCELEN+CHACHA_CTRLEN)
#define CHACHA_BLOCKLEN 64
#define POLY1305_MAX_AAD 32
#define POLY1305_KEYLEN 32
#define POLY1305_TAGLEN 16
#define u_int unsigned int
#define uint8_t unsigned char
#define u_char unsigned char
#ifndef NULL
#define NULL (void *)0
#endif
#if (CRYPT >= 0x0117) && (0)
// to do: use ltc chacha/poly1305 implementation (working on big-endian machines)
#define chacha_ctx chacha20poly1305_state
#define poly1305_context poly1305_state
#define _private_tls_poly1305_init(ctx, key, len) poly1305_init(ctx, key, len)
#define _private_tls_poly1305_update(ctx, in, len) poly1305_process(ctx, in, len)
#define _private_tls_poly1305_finish(ctx, mac) poly1305_done(ctx, mac, 16)
#else
struct chacha_ctx {
u_int input[16];
uint8_t ks[CHACHA_BLOCKLEN];
uint8_t unused;
};
static inline void chacha_keysetup(struct chacha_ctx *x, const u_char *k, u_int kbits);
static inline void chacha_ivsetup(struct chacha_ctx *x, const u_char *iv, const u_char *ctr);
static inline void chacha_ivsetup_96bitnonce(struct chacha_ctx *x, const u_char *iv, const u_char *ctr);
static inline void chacha_encrypt_bytes(struct chacha_ctx *x, const u_char *m, u_char *c, u_int bytes);
static inline int poly1305_generate_key(unsigned char *key256, unsigned char *nonce, unsigned int noncelen, unsigned char *poly_key, unsigned int counter);
#define poly1305_block_size 16
#define poly1305_context poly1305_state_internal_t
//========== ChaCha20 from D. J. Bernstein ========= //
// Source available at https://cr.yp.to/chacha.html //
typedef unsigned char u8;
typedef unsigned int u32;
typedef struct chacha_ctx chacha_ctx;
#define U8C(v) (v##U)
#define U32C(v) (v##U)
#define U8V(v) ((u8)(v) & U8C(0xFF))
#define U32V(v) ((u32)(v) & U32C(0xFFFFFFFF))
#define ROTL32(v, n) \
(U32V((v) << (n)) | ((v) >> (32 - (n))))
#define _private_tls_U8TO32_LITTLE(p) \
(((u32)((p)[0])) | \
((u32)((p)[1]) << 8) | \
((u32)((p)[2]) << 16) | \
((u32)((p)[3]) << 24))
#define _private_tls_U32TO8_LITTLE(p, v) \
do { \
(p)[0] = U8V((v)); \
(p)[1] = U8V((v) >> 8); \
(p)[2] = U8V((v) >> 16); \
(p)[3] = U8V((v) >> 24); \
} while (0)
#define ROTATE(v,c) (ROTL32(v,c))
#define XOR(v,w) ((v) ^ (w))
#define PLUS(v,w) (U32V((v) + (w)))
#define PLUSONE(v) (PLUS((v),1))
#define QUARTERROUND(a,b,c,d) \
a = PLUS(a,b); d = ROTATE(XOR(d,a),16); \
c = PLUS(c,d); b = ROTATE(XOR(b,c),12); \
a = PLUS(a,b); d = ROTATE(XOR(d,a), 8); \
c = PLUS(c,d); b = ROTATE(XOR(b,c), 7);
static const char sigma[] = "expand 32-byte k";
static const char tau[] = "expand 16-byte k";
static inline void chacha_keysetup(chacha_ctx *x, const u8 *k, u32 kbits) {
const char *constants;
x->input[4] = _private_tls_U8TO32_LITTLE(k + 0);
x->input[5] = _private_tls_U8TO32_LITTLE(k + 4);
x->input[6] = _private_tls_U8TO32_LITTLE(k + 8);
x->input[7] = _private_tls_U8TO32_LITTLE(k + 12);
if (kbits == 256) { /* recommended */
k += 16;
constants = sigma;
} else { /* kbits == 128 */
constants = tau;
}
x->input[8] = _private_tls_U8TO32_LITTLE(k + 0);
x->input[9] = _private_tls_U8TO32_LITTLE(k + 4);
x->input[10] = _private_tls_U8TO32_LITTLE(k + 8);
x->input[11] = _private_tls_U8TO32_LITTLE(k + 12);
x->input[0] = _private_tls_U8TO32_LITTLE(constants + 0);
x->input[1] = _private_tls_U8TO32_LITTLE(constants + 4);
x->input[2] = _private_tls_U8TO32_LITTLE(constants + 8);
x->input[3] = _private_tls_U8TO32_LITTLE(constants + 12);
}
static inline void chacha_key(chacha_ctx *x, u8 *k) {
_private_tls_U32TO8_LITTLE(k, x->input[4]);
_private_tls_U32TO8_LITTLE(k + 4, x->input[5]);
_private_tls_U32TO8_LITTLE(k + 8, x->input[6]);
_private_tls_U32TO8_LITTLE(k + 12, x->input[7]);
_private_tls_U32TO8_LITTLE(k + 16, x->input[8]);
_private_tls_U32TO8_LITTLE(k + 20, x->input[9]);
_private_tls_U32TO8_LITTLE(k + 24, x->input[10]);
_private_tls_U32TO8_LITTLE(k + 28, x->input[11]);
}
static inline void chacha_nonce(chacha_ctx *x, u8 *nonce) {
_private_tls_U32TO8_LITTLE(nonce + 0, x->input[13]);
_private_tls_U32TO8_LITTLE(nonce + 4, x->input[14]);
_private_tls_U32TO8_LITTLE(nonce + 8, x->input[15]);
}
static inline void chacha_ivsetup(chacha_ctx *x, const u8 *iv, const u8 *counter) {
x->input[12] = counter == NULL ? 0 : _private_tls_U8TO32_LITTLE(counter + 0);
x->input[13] = counter == NULL ? 0 : _private_tls_U8TO32_LITTLE(counter + 4);
if (iv) {
x->input[14] = _private_tls_U8TO32_LITTLE(iv + 0);
x->input[15] = _private_tls_U8TO32_LITTLE(iv + 4);
}
}
static inline void chacha_ivsetup_96bitnonce(chacha_ctx *x, const u8 *iv, const u8 *counter) {
x->input[12] = counter == NULL ? 0 : _private_tls_U8TO32_LITTLE(counter + 0);
if (iv) {
x->input[13] = _private_tls_U8TO32_LITTLE(iv + 0);
x->input[14] = _private_tls_U8TO32_LITTLE(iv + 4);
x->input[15] = _private_tls_U8TO32_LITTLE(iv + 8);
}
}
static inline void chacha_ivupdate(chacha_ctx *x, const u8 *iv, const u8 *aad, const u8 *counter) {
x->input[12] = counter == NULL ? 0 : _private_tls_U8TO32_LITTLE(counter + 0);
x->input[13] = _private_tls_U8TO32_LITTLE(iv + 0);
x->input[14] = _private_tls_U8TO32_LITTLE(iv + 4) ^ _private_tls_U8TO32_LITTLE(aad);
x->input[15] = _private_tls_U8TO32_LITTLE(iv + 8) ^ _private_tls_U8TO32_LITTLE(aad + 4);
}
static inline void chacha_encrypt_bytes(chacha_ctx *x, const u8 *m, u8 *c, u32 bytes) {
u32 x0, x1, x2, x3, x4, x5, x6, x7;
u32 x8, x9, x10, x11, x12, x13, x14, x15;
u32 j0, j1, j2, j3, j4, j5, j6, j7;
u32 j8, j9, j10, j11, j12, j13, j14, j15;
u8 *ctarget = NULL;
u8 tmp[64];
u_int i;
if (!bytes)
return;
j0 = x->input[0];
j1 = x->input[1];
j2 = x->input[2];
j3 = x->input[3];
j4 = x->input[4];
j5 = x->input[5];
j6 = x->input[6];
j7 = x->input[7];
j8 = x->input[8];
j9 = x->input[9];
j10 = x->input[10];
j11 = x->input[11];
j12 = x->input[12];
j13 = x->input[13];
j14 = x->input[14];
j15 = x->input[15];
for (;;) {
if (bytes < 64) {
for (i = 0; i < bytes; ++i)
tmp[i] = m[i];
m = tmp;
ctarget = c;
c = tmp;
}
x0 = j0;
x1 = j1;
x2 = j2;
x3 = j3;
x4 = j4;
x5 = j5;
x6 = j6;
x7 = j7;
x8 = j8;
x9 = j9;
x10 = j10;
x11 = j11;
x12 = j12;
x13 = j13;
x14 = j14;
x15 = j15;
for (i = 20; i > 0; i -= 2) {
QUARTERROUND(x0, x4, x8, x12)
QUARTERROUND(x1, x5, x9, x13)
QUARTERROUND(x2, x6, x10, x14)
QUARTERROUND(x3, x7, x11, x15)
QUARTERROUND(x0, x5, x10, x15)
QUARTERROUND(x1, x6, x11, x12)
QUARTERROUND(x2, x7, x8, x13)
QUARTERROUND(x3, x4, x9, x14)
}
x0 = PLUS(x0, j0);
x1 = PLUS(x1, j1);
x2 = PLUS(x2, j2);
x3 = PLUS(x3, j3);
x4 = PLUS(x4, j4);
x5 = PLUS(x5, j5);
x6 = PLUS(x6, j6);
x7 = PLUS(x7, j7);
x8 = PLUS(x8, j8);
x9 = PLUS(x9, j9);
x10 = PLUS(x10, j10);
x11 = PLUS(x11, j11);
x12 = PLUS(x12, j12);
x13 = PLUS(x13, j13);
x14 = PLUS(x14, j14);
x15 = PLUS(x15, j15);
if (bytes < 64) {
_private_tls_U32TO8_LITTLE(x->ks + 0, x0);
_private_tls_U32TO8_LITTLE(x->ks + 4, x1);
_private_tls_U32TO8_LITTLE(x->ks + 8, x2);
_private_tls_U32TO8_LITTLE(x->ks + 12, x3);
_private_tls_U32TO8_LITTLE(x->ks + 16, x4);
_private_tls_U32TO8_LITTLE(x->ks + 20, x5);
_private_tls_U32TO8_LITTLE(x->ks + 24, x6);
_private_tls_U32TO8_LITTLE(x->ks + 28, x7);
_private_tls_U32TO8_LITTLE(x->ks + 32, x8);
_private_tls_U32TO8_LITTLE(x->ks + 36, x9);
_private_tls_U32TO8_LITTLE(x->ks + 40, x10);
_private_tls_U32TO8_LITTLE(x->ks + 44, x11);
_private_tls_U32TO8_LITTLE(x->ks + 48, x12);
_private_tls_U32TO8_LITTLE(x->ks + 52, x13);
_private_tls_U32TO8_LITTLE(x->ks + 56, x14);
_private_tls_U32TO8_LITTLE(x->ks + 60, x15);
}
x0 = XOR(x0, _private_tls_U8TO32_LITTLE(m + 0));
x1 = XOR(x1, _private_tls_U8TO32_LITTLE(m + 4));
x2 = XOR(x2, _private_tls_U8TO32_LITTLE(m + 8));
x3 = XOR(x3, _private_tls_U8TO32_LITTLE(m + 12));
x4 = XOR(x4, _private_tls_U8TO32_LITTLE(m + 16));
x5 = XOR(x5, _private_tls_U8TO32_LITTLE(m + 20));
x6 = XOR(x6, _private_tls_U8TO32_LITTLE(m + 24));
x7 = XOR(x7, _private_tls_U8TO32_LITTLE(m + 28));
x8 = XOR(x8, _private_tls_U8TO32_LITTLE(m + 32));
x9 = XOR(x9, _private_tls_U8TO32_LITTLE(m + 36));
x10 = XOR(x10, _private_tls_U8TO32_LITTLE(m + 40));
x11 = XOR(x11, _private_tls_U8TO32_LITTLE(m + 44));
x12 = XOR(x12, _private_tls_U8TO32_LITTLE(m + 48));
x13 = XOR(x13, _private_tls_U8TO32_LITTLE(m + 52));
x14 = XOR(x14, _private_tls_U8TO32_LITTLE(m + 56));
x15 = XOR(x15, _private_tls_U8TO32_LITTLE(m + 60));
j12 = PLUSONE(j12);
if (!j12) {
j13 = PLUSONE(j13);
/*
* Stopping at 2^70 bytes per nonce is the user's
* responsibility.
*/
}
_private_tls_U32TO8_LITTLE(c + 0, x0);
_private_tls_U32TO8_LITTLE(c + 4, x1);
_private_tls_U32TO8_LITTLE(c + 8, x2);
_private_tls_U32TO8_LITTLE(c + 12, x3);
_private_tls_U32TO8_LITTLE(c + 16, x4);
_private_tls_U32TO8_LITTLE(c + 20, x5);
_private_tls_U32TO8_LITTLE(c + 24, x6);
_private_tls_U32TO8_LITTLE(c + 28, x7);
_private_tls_U32TO8_LITTLE(c + 32, x8);
_private_tls_U32TO8_LITTLE(c + 36, x9);
_private_tls_U32TO8_LITTLE(c + 40, x10);
_private_tls_U32TO8_LITTLE(c + 44, x11);
_private_tls_U32TO8_LITTLE(c + 48, x12);
_private_tls_U32TO8_LITTLE(c + 52, x13);
_private_tls_U32TO8_LITTLE(c + 56, x14);
_private_tls_U32TO8_LITTLE(c + 60, x15);
if (bytes <= 64) {
if (bytes < 64) {
for (i = 0; i < bytes; ++i)
ctarget[i] = c[i];
}
x->input[12] = j12;
x->input[13] = j13;
x->unused = 64 - bytes;
return;
}
bytes -= 64;
c += 64;
m += 64;
}
}
static inline void chacha20_block(chacha_ctx *x, unsigned char *c, u_int len) {
u_int i;
unsigned int state[16];
for (i = 0; i < 16; i++)
state[i] = x->input[i];
for (i = 20; i > 0; i -= 2) {
QUARTERROUND(state[0], state[4], state[8], state[12])
QUARTERROUND(state[1], state[5], state[9], state[13])
QUARTERROUND(state[2], state[6], state[10], state[14])
QUARTERROUND(state[3], state[7], state[11], state[15])
QUARTERROUND(state[0], state[5], state[10], state[15])
QUARTERROUND(state[1], state[6], state[11], state[12])
QUARTERROUND(state[2], state[7], state[8], state[13])
QUARTERROUND(state[3], state[4], state[9], state[14])
}
for (i = 0; i < 16; i++)
x->input[i] = PLUS(x->input[i], state[i]);
for (i = 0; i < len; i += 4) {
_private_tls_U32TO8_LITTLE(c + i, x->input[i/4]);
}
}
static inline int poly1305_generate_key(unsigned char *key256, unsigned char *nonce, unsigned int noncelen, unsigned char *poly_key, unsigned int counter) {
struct chacha_ctx ctx;
uint64_t ctr;
memset(&ctx, 0, sizeof(ctx));
chacha_keysetup(&ctx, key256, 256);
switch (noncelen) {
case 8:
ctr = counter;
chacha_ivsetup(&ctx, nonce, (unsigned char *)&ctr);
break;
case 12:
chacha_ivsetup_96bitnonce(&ctx, nonce, (unsigned char *)&counter);
break;
default:
return -1;
}
chacha20_block(&ctx, poly_key, POLY1305_KEYLEN);
return 0;
}
/* 17 + sizeof(size_t) + 14*sizeof(unsigned long) */
typedef struct poly1305_state_internal_t {
unsigned long r[5];
unsigned long h[5];
unsigned long pad[4];
size_t leftover;
unsigned char buffer[poly1305_block_size];
unsigned char final;
} poly1305_state_internal_t;
/* interpret four 8 bit unsigned integers as a 32 bit unsigned integer in little endian */
static unsigned long _private_tls_U8TO32(const unsigned char *p) {
return
(((unsigned long)(p[0] & 0xff) ) |
((unsigned long)(p[1] & 0xff) << 8) |
((unsigned long)(p[2] & 0xff) << 16) |
((unsigned long)(p[3] & 0xff) << 24));
}
/* store a 32 bit unsigned integer as four 8 bit unsigned integers in little endian */
static void _private_tls_U32TO8(unsigned char *p, unsigned long v) {
p[0] = (v ) & 0xff;
p[1] = (v >> 8) & 0xff;
p[2] = (v >> 16) & 0xff;
p[3] = (v >> 24) & 0xff;
}
void _private_tls_poly1305_init(poly1305_context *ctx, const unsigned char key[32]) {
poly1305_state_internal_t *st = (poly1305_state_internal_t *)ctx;
/* r &= 0xffffffc0ffffffc0ffffffc0fffffff */
st->r[0] = (_private_tls_U8TO32(&key[ 0]) ) & 0x3ffffff;
st->r[1] = (_private_tls_U8TO32(&key[ 3]) >> 2) & 0x3ffff03;
st->r[2] = (_private_tls_U8TO32(&key[ 6]) >> 4) & 0x3ffc0ff;
st->r[3] = (_private_tls_U8TO32(&key[ 9]) >> 6) & 0x3f03fff;
st->r[4] = (_private_tls_U8TO32(&key[12]) >> 8) & 0x00fffff;
/* h = 0 */
st->h[0] = 0;
st->h[1] = 0;
st->h[2] = 0;
st->h[3] = 0;
st->h[4] = 0;
/* save pad for later */
st->pad[0] = _private_tls_U8TO32(&key[16]);
st->pad[1] = _private_tls_U8TO32(&key[20]);
st->pad[2] = _private_tls_U8TO32(&key[24]);
st->pad[3] = _private_tls_U8TO32(&key[28]);
st->leftover = 0;
st->final = 0;
}
static void _private_tls_poly1305_blocks(poly1305_state_internal_t *st, const unsigned char *m, size_t bytes) {
const unsigned long hibit = (st->final) ? 0 : (1UL << 24); /* 1 << 128 */
unsigned long r0,r1,r2,r3,r4;
unsigned long s1,s2,s3,s4;
unsigned long h0,h1,h2,h3,h4;
unsigned long long d0,d1,d2,d3,d4;
unsigned long c;
r0 = st->r[0];
r1 = st->r[1];
r2 = st->r[2];
r3 = st->r[3];
r4 = st->r[4];
s1 = r1 * 5;
s2 = r2 * 5;
s3 = r3 * 5;
s4 = r4 * 5;
h0 = st->h[0];
h1 = st->h[1];
h2 = st->h[2];
h3 = st->h[3];
h4 = st->h[4];
while (bytes >= poly1305_block_size) {
/* h += m[i] */
h0 += (_private_tls_U8TO32(m+ 0) ) & 0x3ffffff;
h1 += (_private_tls_U8TO32(m+ 3) >> 2) & 0x3ffffff;
h2 += (_private_tls_U8TO32(m+ 6) >> 4) & 0x3ffffff;
h3 += (_private_tls_U8TO32(m+ 9) >> 6) & 0x3ffffff;
h4 += (_private_tls_U8TO32(m+12) >> 8) | hibit;
/* h *= r */
d0 = ((unsigned long long)h0 * r0) + ((unsigned long long)h1 * s4) + ((unsigned long long)h2 * s3) + ((unsigned long long)h3 * s2) + ((unsigned long long)h4 * s1);
d1 = ((unsigned long long)h0 * r1) + ((unsigned long long)h1 * r0) + ((unsigned long long)h2 * s4) + ((unsigned long long)h3 * s3) + ((unsigned long long)h4 * s2);
d2 = ((unsigned long long)h0 * r2) + ((unsigned long long)h1 * r1) + ((unsigned long long)h2 * r0) + ((unsigned long long)h3 * s4) + ((unsigned long long)h4 * s3);
d3 = ((unsigned long long)h0 * r3) + ((unsigned long long)h1 * r2) + ((unsigned long long)h2 * r1) + ((unsigned long long)h3 * r0) + ((unsigned long long)h4 * s4);
d4 = ((unsigned long long)h0 * r4) + ((unsigned long long)h1 * r3) + ((unsigned long long)h2 * r2) + ((unsigned long long)h3 * r1) + ((unsigned long long)h4 * r0);
/* (partial) h %= p */
c = (unsigned long)(d0 >> 26); h0 = (unsigned long)d0 & 0x3ffffff;
d1 += c; c = (unsigned long)(d1 >> 26); h1 = (unsigned long)d1 & 0x3ffffff;
d2 += c; c = (unsigned long)(d2 >> 26); h2 = (unsigned long)d2 & 0x3ffffff;
d3 += c; c = (unsigned long)(d3 >> 26); h3 = (unsigned long)d3 & 0x3ffffff;
d4 += c; c = (unsigned long)(d4 >> 26); h4 = (unsigned long)d4 & 0x3ffffff;
h0 += c * 5; c = (h0 >> 26); h0 = h0 & 0x3ffffff;
h1 += c;
m += poly1305_block_size;
bytes -= poly1305_block_size;
}
st->h[0] = h0;
st->h[1] = h1;
st->h[2] = h2;
st->h[3] = h3;
st->h[4] = h4;
}
void _private_tls_poly1305_finish(poly1305_context *ctx, unsigned char mac[16]) {
poly1305_state_internal_t *st = (poly1305_state_internal_t *)ctx;
unsigned long h0,h1,h2,h3,h4,c;
unsigned long g0,g1,g2,g3,g4;
unsigned long long f;
unsigned long mask;
/* process the remaining block */
if (st->leftover) {
size_t i = st->leftover;
st->buffer[i++] = 1;
for (; i < poly1305_block_size; i++)
st->buffer[i] = 0;
st->final = 1;
_private_tls_poly1305_blocks(st, st->buffer, poly1305_block_size);
}
/* fully carry h */
h0 = st->h[0];
h1 = st->h[1];
h2 = st->h[2];
h3 = st->h[3];
h4 = st->h[4];
c = h1 >> 26; h1 = h1 & 0x3ffffff;
h2 += c; c = h2 >> 26; h2 = h2 & 0x3ffffff;
h3 += c; c = h3 >> 26; h3 = h3 & 0x3ffffff;
h4 += c; c = h4 >> 26; h4 = h4 & 0x3ffffff;
h0 += c * 5; c = h0 >> 26; h0 = h0 & 0x3ffffff;
h1 += c;
/* compute h + -p */
g0 = h0 + 5; c = g0 >> 26; g0 &= 0x3ffffff;
g1 = h1 + c; c = g1 >> 26; g1 &= 0x3ffffff;
g2 = h2 + c; c = g2 >> 26; g2 &= 0x3ffffff;
g3 = h3 + c; c = g3 >> 26; g3 &= 0x3ffffff;
g4 = h4 + c - (1UL << 26);
/* select h if h < p, or h + -p if h >= p */
mask = (g4 >> ((sizeof(unsigned long) * 8) - 1)) - 1;
g0 &= mask;
g1 &= mask;
g2 &= mask;
g3 &= mask;
g4 &= mask;
mask = ~mask;
h0 = (h0 & mask) | g0;
h1 = (h1 & mask) | g1;
h2 = (h2 & mask) | g2;
h3 = (h3 & mask) | g3;
h4 = (h4 & mask) | g4;
/* h = h % (2^128) */
h0 = ((h0 ) | (h1 << 26)) & 0xffffffff;
h1 = ((h1 >> 6) | (h2 << 20)) & 0xffffffff;
h2 = ((h2 >> 12) | (h3 << 14)) & 0xffffffff;
h3 = ((h3 >> 18) | (h4 << 8)) & 0xffffffff;
/* mac = (h + pad) % (2^128) */
f = (unsigned long long)h0 + st->pad[0] ; h0 = (unsigned long)f;
f = (unsigned long long)h1 + st->pad[1] + (f >> 32); h1 = (unsigned long)f;
f = (unsigned long long)h2 + st->pad[2] + (f >> 32); h2 = (unsigned long)f;
f = (unsigned long long)h3 + st->pad[3] + (f >> 32); h3 = (unsigned long)f;
_private_tls_U32TO8(mac + 0, h0);
_private_tls_U32TO8(mac + 4, h1);
_private_tls_U32TO8(mac + 8, h2);
_private_tls_U32TO8(mac + 12, h3);
/* zero out the state */
st->h[0] = 0;
st->h[1] = 0;
st->h[2] = 0;
st->h[3] = 0;
st->h[4] = 0;
st->r[0] = 0;
st->r[1] = 0;
st->r[2] = 0;
st->r[3] = 0;
st->r[4] = 0;
st->pad[0] = 0;
st->pad[1] = 0;
st->pad[2] = 0;
st->pad[3] = 0;
}
void _private_tls_poly1305_update(poly1305_context *ctx, const unsigned char *m, size_t bytes) {
poly1305_state_internal_t *st = (poly1305_state_internal_t *)ctx;
size_t i;
/* handle leftover */
if (st->leftover) {
size_t want = (poly1305_block_size - st->leftover);
if (want > bytes)
want = bytes;
for (i = 0; i < want; i++)
st->buffer[st->leftover + i] = m[i];
bytes -= want;
m += want;
st->leftover += want;
if (st->leftover < poly1305_block_size)
return;
_private_tls_poly1305_blocks(st, st->buffer, poly1305_block_size);
st->leftover = 0;
}
/* process full blocks */
if (bytes >= poly1305_block_size) {
size_t want = (bytes & ~(poly1305_block_size - 1));
_private_tls_poly1305_blocks(st, m, want);
m += want;
bytes -= want;
}
/* store leftover */
if (bytes) {
for (i = 0; i < bytes; i++)
st->buffer[st->leftover + i] = m[i];
st->leftover += bytes;
}
}
int poly1305_verify(const unsigned char mac1[16], const unsigned char mac2[16]) {
size_t i;
unsigned int dif = 0;
for (i = 0; i < 16; i++)
dif |= (mac1[i] ^ mac2[i]);
dif = (dif - 1) >> ((sizeof(unsigned int) * 8) - 1);
return (dif & 1);
}
void chacha20_poly1305_key(struct chacha_ctx *ctx, unsigned char *poly1305_key) {
unsigned char key[32];
unsigned char nonce[12];
chacha_key(ctx, key);
chacha_nonce(ctx, nonce);
poly1305_generate_key(key, nonce, sizeof(nonce), poly1305_key, 0);
}
int chacha20_poly1305_aead(struct chacha_ctx *ctx, unsigned char *pt, unsigned int len, unsigned char *aad, unsigned int aad_len, unsigned char *poly_key, unsigned char *out) {
static unsigned char zeropad[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
if (aad_len > POLY1305_MAX_AAD)
return -1;
unsigned int counter = 1;
chacha_ivsetup_96bitnonce(ctx, NULL, (unsigned char *)&counter);
chacha_encrypt_bytes(ctx, pt, out, len);
poly1305_context aead_ctx;
_private_tls_poly1305_init(&aead_ctx, poly_key);
_private_tls_poly1305_update(&aead_ctx, aad, aad_len);
int rem = aad_len % 16;
if (rem)
_private_tls_poly1305_update(&aead_ctx, zeropad, 16 - rem);
_private_tls_poly1305_update(&aead_ctx, out, len);
rem = len % 16;
if (rem)
_private_tls_poly1305_update(&aead_ctx, zeropad, 16 - rem);
unsigned char trail[16];
_private_tls_U32TO8(trail, aad_len);
*(int *)(trail + 4) = 0;
_private_tls_U32TO8(trail + 8, len);
*(int *)(trail + 12) = 0;
_private_tls_poly1305_update(&aead_ctx, trail, 16);
_private_tls_poly1305_finish(&aead_ctx, out + len);
return len + POLY1305_TAGLEN;
}
#endif
#endif
typedef enum {
KEA_dhe_dss,
KEA_dhe_rsa,
KEA_dh_anon,
KEA_rsa,
KEA_dh_dss,
KEA_dh_rsa,
KEA_ec_diffie_hellman
} KeyExchangeAlgorithm;
typedef enum {
rsa_sign = 1,
dss_sign = 2,
rsa_fixed_dh = 3,
dss_fixed_dh = 4,
rsa_ephemeral_dh_RESERVED = 5,
dss_ephemeral_dh_RESERVED = 6,
fortezza_dms_RESERVED = 20,
ecdsa_sign = 64,
rsa_fixed_ecdh = 65,
ecdsa_fixed_ecdh = 66
} TLSClientCertificateType;
typedef enum {
none = 0,
md5 = 1,
sha1 = 2,
sha224 = 3,
sha256 = 4,
sha384 = 5,
sha512 = 6,
_md5_sha1 = 255
} TLSHashAlgorithm;
#define TLS_HASH_ALGO_NUMBER (sha512 - md5 + 1)
typedef enum {
anonymous = 0,
rsa = 1,
dsa = 2,
ecdsa = 3
} TLSSignatureAlgorithm;
#define TLS_SIGN_ALGO_NUMBER (ecdsa - rsa + 1)
struct _private_OID_chain {
void *top;
unsigned char *oid;
};
struct TLSCertificate {
unsigned short version;
unsigned int algorithm;
unsigned int key_algorithm;
unsigned int ec_algorithm;
unsigned char *exponent;
unsigned int exponent_len;
unsigned char *pk;
unsigned int pk_len;
unsigned char *priv;
unsigned int priv_len;
unsigned char *issuer_country;
unsigned char *issuer_state;
unsigned char *issuer_location;
unsigned char *issuer_entity;
unsigned char *issuer_subject;
unsigned char *not_before;
unsigned char *not_after;
unsigned char *country;
unsigned char *state;
unsigned char *location;
unsigned char *entity;
unsigned char *subject;
unsigned char **san;
unsigned short san_length;
unsigned char *ocsp;
unsigned char *serial_number;
unsigned int serial_len;
unsigned char *sign_key;
unsigned int sign_len;
unsigned char *fingerprint;
unsigned char *der_bytes;
unsigned int der_len;
unsigned char *bytes;
unsigned int len;
};
typedef struct {
union {
symmetric_CBC aes_local;
gcm_state aes_gcm_local;
#ifdef TLS_WITH_CHACHA20_POLY1305
chacha_ctx chacha_local;
#endif
} ctx_local;
union {
symmetric_CBC aes_remote;
gcm_state aes_gcm_remote;
#ifdef TLS_WITH_CHACHA20_POLY1305
chacha_ctx chacha_remote;
#endif
} ctx_remote;
union {
unsigned char local_mac[TLS_MAX_MAC_SIZE];
unsigned char local_aead_iv[TLS_AES_GCM_IV_LENGTH];
#ifdef WITH_TLS_13
unsigned char local_iv[TLS_13_AES_GCM_IV_LENGTH];
#endif
#ifdef TLS_WITH_CHACHA20_POLY1305
unsigned char local_nonce[TLS_CHACHA20_IV_LENGTH];
#endif
} ctx_local_mac;
union {
unsigned char remote_aead_iv[TLS_AES_GCM_IV_LENGTH];
unsigned char remote_mac[TLS_MAX_MAC_SIZE];
#ifdef WITH_TLS_13
unsigned char remote_iv[TLS_13_AES_GCM_IV_LENGTH];
#endif
#ifdef TLS_WITH_CHACHA20_POLY1305
unsigned char remote_nonce[TLS_CHACHA20_IV_LENGTH];
#endif
} ctx_remote_mac;
unsigned char created;
} TLSCipher;
typedef struct {
hash_state hash32;
hash_state hash48;
#ifdef TLS_LEGACY_SUPPORT
hash_state hash2;
#endif
unsigned char created;
} TLSHash;
#ifdef TLS_FORWARD_SECRECY
#define mp_init(a) ltc_mp.init(a)
#define mp_init_multi ltc_init_multi
#define mp_clear(a) ltc_mp.deinit(a)
#define mp_clear_multi ltc_deinit_multi
#define mp_count_bits(a) ltc_mp.count_bits(a)
#define mp_read_radix(a, b, c) ltc_mp.read_radix(a, b, c)
#define mp_unsigned_bin_size(a) ltc_mp.unsigned_size(a)
#define mp_to_unsigned_bin(a, b) ltc_mp.unsigned_write(a, b)
#define mp_read_unsigned_bin(a, b, c) ltc_mp.unsigned_read(a, b, c)