interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / aienux / allen / tls.om

1303 lines · 18959 bytes Raw Blame History
1note RFC8446 bounded TLS1.3 client: api.github.com, X25519, 1303, CV0403.
2note Concatenate tls-crypto.om and tls-cert.om before this file. No platform I/O.
3note tlsinput(n): NEW bytes in Tlsin[0..n); internal residual retained; Tlsinlen becomes0.
4note tlswrite(n)->bool: atomic acceptance, n<=16384; false means retain plaintext.
5note tlssent acknowledges the ENTIRE output queue, then pumps deferred work.
6note Tlsstate:0 lifecycle idle0 handshake1 open2 closed3 error4;1 error;2 peer close_notify.
7note Errors:1 API bounds,2 capacity,3 crypto,4 record overflow,5 unexpected record,
8note 6 bad MAC,7 malformed,8 unsupported,9 transcript cap,10 certificate,11 CV,
9note 12 Finished,13 sequence limit,14 peer alert,15 HRR unsupported,16 key boundary,
10note 17 KeyUpdate unsupported,18 absent time. Fatal/reset discards and wipes all queues.
11note Limits: handshake and transcript65536, ciphertext16640, record plaintext16384,
12note receive residual65536, app262144, epoch2^32-1 records. No tickets used or early data.
13global Tlsin uchar 65536
14global Tlsout uchar 65536
15global Tlsplain uchar 65536
16global Tlsapp uchar 262144
17global Tlsrand uchar 64
18global Tlsinlen ulong 1
19global Tlsoutn ulong 1
20global Tlsappn ulong 1
21global Tlsstate ulong 8
22global Tsrecv uchar 65536
23global Tshand uchar 65536
24global Tstrans uchar 65536
25global Tsmsg uchar 1024
26global Tssec uchar 192
27global Tskey uchar 128
28global Tsiv uchar 48
29global Tshash uchar 32
30global Tslbl uchar 16
31global Tsseen uchar 8192
32global Tsseq ulong 2
33note Tsctl:0 recvlen,1 handlen,2 transcriptlen,3 localclose requested,
34note 4 localclose queued,5 peerclose,6 phase,7 msglen,8 labellen,9 validated epoch time.
35note phase0 SH,1 EE,2 Certificate,3 CV,4 Finished,5 application,6 client Finished pending.
36global Tsctl ulong 10
37text Tshost "api.github.com"
38text Tsalpn "http/1.1"
39text Tsprefix "tls13 "
40text Tsderiv "derived"
41text Tschs "c hs traffic"
42text Tsshs "s hs traffic"
43text Tscap "c ap traffic"
44text Tssap "s ap traffic"
45text Tsklab "key"
46text Tsvlab "iv"
47text Tsflab "finished"
48text Tscvctx "TLS 1.3, server CertificateVerify"
49text Tshrr "cf21ad74e59a6111be1d8c021e65b891c2a211167abb8c5e079e09e2c8a8339c"
50fn tshex c uchar to uchar
51if le c 57
52ret sub c 48
53end
54ret sub c 87
55end
56fn tswipe to void
57for i ulong 192
58put Tssec i 0
59end
60for i ulong 128
61put Tskey i 0
62end
63for i ulong 48
64put Tsiv i 0
65end
66for i ulong 32
67put Tshash i 0
68end
69for i ulong 16
70put Tslbl i 0
71end
72for i ulong 2
73put Tsseq i 0
74end
75tcwipe
76certreset
77end
78fn tsclear to void
79tswipe
80for i ulong 65536
81put Tlsin i 0
82put Tlsout i 0
83put Tlsplain i 0
84put Tsrecv i 0
85put Tshand i 0
86put Tstrans i 0
87end
88for i ulong 262144
89put Tlsapp i 0
90end
91for i ulong 1024
92put Tsmsg i 0
93end
94for i ulong 8192
95put Tsseen i 0
96end
97for i ulong 10
98put Tsctl i 0
99end
100for i ulong 8
101put Tlsstate i 0
102end
103put Tlsinlen 0 0
104put Tlsoutn 0 0
105put Tlsappn 0 0
106end
107fn tsfail code ulong to void
108tsclear
109for i ulong 64
110put Tlsrand i 0
111end
112put Tlsstate 0 4
113put Tlsstate 1 code
114end
115fn tsh16 p ulong to ulong
116ret add shl ulong get Tshand p 8 ulong get Tshand add p 1
117end
118fn tsh24 p ulong to ulong
119ret add shl ulong get Tshand p 16 tsh16 add p 1
120end
121fn tsput c uchar to void
122p = get Tsctl 7
123put Tsmsg p c
124put Tsctl 7 add p 1
125end
126fn tsu16 x ulong to void
127tsput uchar and shr x 8 255
128tsput uchar and x 255
129end
130fn tsu24 x ulong to void
131tsput uchar and shr x 16 255
132tsu16 and x 65535
133end
134fn tshashdo to bool
135n = get Tsctl 2
136for i ulong n
137put Tcbuf i get Tstrans i
138end
139if not tcsha n
140ret false
141end
142for i ulong 32
143put Tshash i get Tcdigest i
144end
145ret true
146end
147fn tsempty to bool
148if not tcsha 0
149ret false
150end
151for i ulong 32
152put Tshash i get Tcdigest i
153end
154ret true
155end
156fn tssave p ulong to void
157for i ulong 32
158put Tssec add p i get Tcdigest i
159end
160end
161fn tslab which ulong to void
162put Tsctl 8 0
163if eq which 0
164for i ulong len Tsderiv
165put Tslbl i get Tsderiv i
166end
167put Tsctl 8 len Tsderiv
168end
169if eq which 1
170for i ulong len Tschs
171put Tslbl i get Tschs i
172end
173put Tsctl 8 len Tschs
174end
175if eq which 2
176for i ulong len Tsshs
177put Tslbl i get Tsshs i
178end
179put Tsctl 8 len Tsshs
180end
181if eq which 3
182for i ulong len Tscap
183put Tslbl i get Tscap i
184end
185put Tsctl 8 len Tscap
186end
187if eq which 4
188for i ulong len Tssap
189put Tslbl i get Tssap i
190end
191put Tsctl 8 len Tssap
192end
193if eq which 5
194for i ulong len Tsklab
195put Tslbl i get Tsklab i
196end
197put Tsctl 8 len Tsklab
198end
199if eq which 6
200for i ulong len Tsvlab
201put Tslbl i get Tsvlab i
202end
203put Tsctl 8 len Tsvlab
204end
205if eq which 7
206for i ulong len Tsflab
207put Tslbl i get Tsflab i
208end
209put Tsctl 8 len Tsflab
210end
211end
212note All expansions <=Hash.length, so RFC5869 has exactly one block T(1).
213fn tsexpand which ulong secat ulong ctx bool olen ulong to bool
214if or eq olen 0 gt olen 32
215ret false
216end
217tslab which
218n = get Tsctl 8
219if eq n 0
220ret false
221end
222for i ulong 32
223put Tckey i get Tssec add secat i
224end
225put Tcbuf 0 0
226put Tcbuf 1 uchar olen
227put Tcbuf 2 uchar add n 6
228for i ulong 6
229put Tcbuf add i 3 get Tsprefix i
230end
231for i ulong n
232put Tcbuf add i 9 get Tslbl i
233end
234p = add n 9
235put Tcbuf p 0
236if ctx
237put Tcbuf p 32
238p = add p 1
239for i ulong 32
240put Tcbuf add p i get Tshash i
241end
242p = add p 32
243else
244p = add p 1
245end
246put Tcbuf p 1
247ret tchmac 32 add p 1
248end
249fn tstraff secat ulong keyat ulong ivat ulong to bool
250if not tsexpand 5 secat false 32
251ret false
252end
253for i ulong 32
254put Tskey add keyat i get Tcdigest i
255end
256if not tsexpand 6 secat false 12
257ret false
258end
259for i ulong 12
260put Tsiv add ivat i get Tcdigest i
261end
262ret true
263end
264fn tshkeys to bool
265note HKDF-Extract(zeros32,zeros32), then Derive-Secret early derived Hash(empty).
266for i ulong 32
267put Tckey i 0
268put Tcbuf i 0
269end
270if not tchmac 32 32
271ret false
272end
273tssave 0
274if not tsempty
275ret false
276end
277if not tsexpand 0 0 true 32
278ret false
279end
280for i ulong 32
281put Tckey i get Tcdigest i
282put Tcbuf i get Tcxshare i
283end
284if not tchmac 32 32
285ret false
286end
287tssave 0
288if not tshashdo
289ret false
290end
291if not tsexpand 1 0 true 32
292ret false
293end
294tssave 32
295if not tsexpand 2 0 true 32
296ret false
297end
298tssave 64
299if not tstraff 32 0 0
300ret false
301end
302if not tstraff 64 32 12
303ret false
304end
305if not tsempty
306ret false
307end
308if not tsexpand 0 0 true 32
309ret false
310end
311for i ulong 32
312put Tckey i get Tcdigest i
313put Tcbuf i 0
314end
315if not tchmac 32 32
316ret false
317end
318tssave 96
319for i ulong 32
320put Tssec i 0
321put Tcxpriv i 0
322put Tcxshare i 0
323end
324put Tsseq 0 0
325put Tsseq 1 0
326ret true
327end
328fn tsakeys to bool
329if not tshashdo
330ret false
331end
332if not tsexpand 3 96 true 32
333ret false
334end
335tssave 128
336if not tsexpand 4 96 true 32
337ret false
338end
339tssave 160
340if not tstraff 128 64 24
341ret false
342end
343if not tstraff 160 96 36
344ret false
345end
346for i ulong 32
347put Tssec add i 96 0
348end
349put Tsseq 1 0
350ret true
351end
352fn tsfinmac secat ulong to bool
353if not tshashdo
354ret false
355end
356if not tsexpand 7 secat false 32
357ret false
358end
359for i ulong 32
360put Tckey i get Tcdigest i
361put Tcbuf i get Tshash i
362end
363ret tchmac 32 32
364end
365fn tsnonce keyat ulong ivat ulong seq ulong to void
366for i ulong 32
367put Tckey i get Tskey add keyat i
368end
369for i ulong 12
370put Tciv i get Tsiv add ivat i
371end
372for i ulong 8
373p = sub ulong 11 i
374b = uchar and shr seq mul i 8 255
375put Tciv p xor get Tciv p b
376end
377end
378note Tcbuf contains plaintext before tsseal; queue is published only after authentication.
379fn tsseal n ulong typ uchar epoch ulong to bool
380if ne get Tlsoutn 0 0
381ret false
382end
383if gt n 16384
384ret false
385end
386s = get Tsseq 0
387if ge s 4294967295
388ret false
389end
390tsnonce mul epoch 64 mul epoch 24 s
391put Tcbuf n typ
392put Tcaad 0 23
393put Tcaad 1 3
394put Tcaad 2 3
395put Tcaad 3 uchar shr add n 17 8
396put Tcaad 4 uchar and add n 17 255
397if not tcseal add n 1 5
398ret false
399end
400for i ulong 5
401put Tlsout i get Tcaad i
402end
403for i ulong add n 1
404put Tlsout add i 5 get Tcbuf i
405end
406for i ulong 16
407put Tlsout add add n 6 i get Tctag i
408end
409put Tlsoutn 0 add n 22
410put Tsseq 0 add s 1
411ret true
412end
413fn tstradd n ulong to bool
414p = get Tsctl 2
415if gt n sub ulong 65536 p
416ret false
417end
418for i ulong n
419put Tstrans add p i get Tshand i
420end
421put Tsctl 2 add p n
422ret true
423end
424fn tsseen0 to void
425for i ulong 8192
426put Tsseen i 0
427end
428end
429fn tsunique typ ulong to bool
430p = div typ 8
431b = uchar shl ulong 1 rem typ 8
432if ne and get Tsseen p b 0
433ret false
434end
435put Tsseen p or get Tsseen p b
436ret true
437end
438fn tsshare p ulong n ulong to bool
439if ne n 36
440ret false
441end
442if or ne tsh16 p 29 ne tsh16 add p 2 32
443ret false
444end
445for i ulong 32
446put Tcxpeer i get Tshand add add p 4 i
447end
448ret true
449end
450fn tssh n ulong to bool
451if lt n 44
452ret false
453end
454if ne tsh16 4 771
455ret false
456end
457note Exact HRR random is refused; never interpreted as an ordinary random.
458d = uchar 0
459for i ulong 32
460b = add shl tshex get Tshrr mul i 2 4 tshex get Tshrr add mul i 2 1
461d = or d xor get Tshand add i 6 b
462end
463if eq d 0
464tsfail 15
465ret false
466end
467if ne get Tshand 38 0
468ret false
469end
470if or ne tsh16 39 4867 ne get Tshand 41 0
471ret false
472end
473if ne tsh16 42 sub n 44
474ret false
475end
476tsseen0
477p = ulong 44
478flags = ulong 0
479while lt p n bound 16384
480if lt sub n p 4
481ret false
482end
483t = tsh16 p
484z = tsh16 add p 2
485p = add p 4
486if gt z sub n p
487ret false
488end
489if not tsunique t
490ret false
491end
492if eq t 43
493if ne z 2
494ret false
495end
496if ne tsh16 p 772
497ret false
498end
499flags = or flags 1
500else
501if eq t 51
502if not tsshare p z
503ret false
504end
505flags = or flags 2
506else
507ret false
508end
509end
510p = add p z
511end
512ret eq flags 3
513end
514fn tsext p ulong z ulong typ ulong to bool
515if eq typ 0
516ret eq z 0
517end
518if eq typ 16
519if ne z 11
520ret false
521end
522if or ne tsh16 p 9 ne get Tshand add p 2 8
523ret false
524end
525for i ulong 8
526if ne get Tshand add add p 3 i get Tsalpn i
527ret false
528end
529end
530ret true
531end
532if eq typ 10
533if lt z 4
534ret false
535end
536if or ne rem z 2 0 ne tsh16 p sub z 2
537ret false
538end
539ret true
540end
541ret false
542end
543fn tsee n ulong to bool
544if lt n 6
545ret false
546end
547if ne tsh16 4 sub n 6
548ret false
549end
550tsseen0
551p = ulong 6
552while lt p n bound 16384
553if lt sub n p 4
554ret false
555end
556t = tsh16 p
557z = tsh16 add p 2
558p = add p 4
559if gt z sub n p
560ret false
561end
562if not tsunique t
563ret false
564end
565if not tsext p z t
566ret false
567end
568p = add p z
569end
570ret true
571end
572fn tscert n ulong to bool
573z = sub n 4
574for i ulong z
575put Certbuf i get Tshand add i 4
576end
577put Certlen 0 z
578note Cert owner may reset its state; supply this session's time immediately before checking.
579put Certnow 0 get Tsctl 9
580ret certcheck
581end
582fn tscv n ulong to bool
583if lt n 8
584ret false
585end
586if ne tsh16 4 1027
587ret false
588end
589z = tsh16 6
590if or gt z 128 ne z sub n 8
591ret false
592end
593if not tshashdo
594ret false
595end
596for i ulong 64
597put Certmsg i 32
598end
599for i ulong len Tscvctx
600put Certmsg add i 64 get Tscvctx i
601end
602p = add ulong 64 len Tscvctx
603put Certmsg p 0
604p = add p 1
605for i ulong 32
606put Certmsg add p i get Tshash i
607end
608for i ulong z
609put Certsig i get Tshand add i 8
610end
611ret certsig add p 32 z
612end
613note NewSessionTicket syntax is checked, including duplicate extensions; nothing is retained.
614fn tsticket n ulong to bool
615if lt n 17
616ret false
617end
618life = add shl tsh16 4 16 tsh16 6
619if gt life 604800
620ret false
621end
622p = add ulong 13 ulong get Tshand 12
623if gt p n
624ret false
625end
626if lt sub n p 2
627ret false
628end
629z = tsh16 p
630p = add p 2
631if or eq z 0 gt z sub n p
632ret false
633end
634p = add p z
635if lt sub n p 2
636ret false
637end
638z = tsh16 p
639p = add p 2
640if ne z sub n p
641ret false
642end
643tsseen0
644while lt p n bound 16384
645if lt sub n p 4
646ret false
647end
648t = tsh16 p
649z = tsh16 add p 2
650p = add p 4
651if gt z sub n p
652ret false
653end
654if not tsunique t
655ret false
656end
657if and eq t 42 ne z 4
658ret false
659end
660p = add p z
661end
662ret true
663end
664fn tsfinish n ulong to bool
665if ne n 36
666ret false
667end
668if not tsfinmac 64
669ret false
670end
671d = uchar 0
672for i ulong 32
673d = or d xor get Tcdigest i get Tshand add i 4
674end
675ret eq d 0
676end
677fn tshmsg n ulong to void
678p = get Tsctl 6
679t = get Tshand 0
680if eq p 5
681if eq t 4
682if not tsticket n
683tsfail 7
684end
685ret
686end
687if eq t 24
688tsfail 17
689ret
690end
691tsfail 8
692ret
693end
694if eq p 0
695if ne t 2
696tsfail 8
697ret
698end
699if not tssh n
700if ne get Tlsstate 0 4
701tsfail 7
702end
703ret
704end
705if not tcx25519
706tsfail 3
707ret
708end
709d = uchar 0
710for i ulong 32
711d = or d get Tcxshare i
712end
713if eq d 0
714tsfail 3
715ret
716end
717else
718if eq p 1
719if ne t 8
720tsfail 8
721ret
722end
723if not tsee n
724tsfail 7
725ret
726end
727else
728if eq p 2
729if ne t 11
730tsfail 8
731ret
732end
733if not tscert n
734tsfail 10
735ret
736end
737else
738if eq p 3
739if ne t 15
740tsfail 8
741ret
742end
743if not tscv n
744tsfail 11
745ret
746end
747else
748if ne t 20
749tsfail 8
750ret
751end
752if not tsfinish n
753tsfail 12
754ret
755end
756end
757end
758end
759end
760if not tstradd n
761tsfail 9
762ret
763end
764if eq p 0
765if not tshkeys
766tsfail 3
767ret
768end
769end
770if eq p 4
771if not tsakeys
772tsfail 3
773ret
774end
775put Tsctl 6 6
776else
777put Tsctl 6 add p 1
778end
779end
780fn tshfeed n ulong to void
781if eq n 0
782tsfail 5
783ret
784end
785p = get Tsctl 1
786if gt n sub ulong 65536 p
787tsfail 2
788ret
789end
790for i ulong n
791put Tshand add p i get Tcbuf i
792end
793put Tsctl 1 add p n
794while ge get Tsctl 1 4 bound 16384
795z = add ulong 4 tsh24 1
796if gt z 65536
797tsfail 2
798ret
799end
800p = get Tsctl 1
801if gt z p
802ret
803end
804t = get Tshand 0
805if or or eq t 2 eq t 20 eq t 24
806if ne z p
807tsfail 16
808ret
809end
810end
811tshmsg z
812if eq get Tlsstate 0 4
813ret
814end
815p = sub p z
816for i ulong p
817put Tshand i get Tshand add i z
818end
819for i ulong z
820put Tshand add p i 0
821end
822put Tsctl 1 p
823end
824end
825fn tsalert n ulong secure bool to void
826if ne n 2
827tsfail 7
828ret
829end
830if ne get Tsctl 1 0
831tsfail 16
832ret
833end
834if or eq get Tcbuf 0 0 gt get Tcbuf 0 2
835tsfail 7
836ret
837end
838if eq get Tcbuf 1 0
839if or not secure ne get Tsctl 6 5
840tsfail 5
841ret
842end
843put Tsctl 5 1
844put Tsctl 3 1
845put Tlsstate 0 3
846put Tlsstate 2 1
847ret
848end
849a = ulong get Tcbuf 1
850tsfail 14
851put Tlsstate 3 a
852end
853fn tsinner n ulong to void
854while gt n 0 bound 16640
855if ne get Tcbuf sub n 1 0
856break
857end
858n = sub n 1
859end
860if eq n 0
861tsfail 5
862ret
863end
864n = sub n 1
865t = get Tcbuf n
866if gt n 16384
867tsfail 4
868ret
869end
870if eq t 22
871tshfeed n
872ret
873end
874if eq t 21
875tsalert n true
876ret
877end
878if ne t 23
879tsfail 5
880ret
881end
882if or ne get Tsctl 6 5 ne get Tsctl 1 0
883tsfail 5
884ret
885end
886if ne get Tsctl 5 0
887tsfail 5
888ret
889end
890p = get Tlsappn 0
891if gt n sub ulong 262144 p
892tsfail 2
893ret
894end
895for i ulong n
896put Tlsapp add p i get Tcbuf i
897end
898put Tlsappn 0 add p n
899end
900fn tsopen n ulong to void
901if lt n 17
902tsfail 7
903ret
904end
905if eq get Tsctl 6 0
906tsfail 5
907ret
908end
909s = get Tsseq 1
910if ge s 4294967295
911tsfail 13
912ret
913end
914epoch = ulong 0
915if eq get Tsctl 6 5
916epoch = 1
917end
918tsnonce add ulong 32 mul epoch 64 add ulong 12 mul epoch 24 s
919for i ulong 5
920put Tcaad i get Tsrecv i
921end
922n = sub n 16
923for i ulong n
924put Tcbuf i get Tsrecv add i 5
925end
926for i ulong 16
927put Tctag i get Tsrecv add add i n 5
928end
929if not tcopen n 5
930tsfail 6
931ret
932end
933put Tsseq 1 add s 1
934tsinner n
935end
936fn tsrecord n ulong to void
937t = get Tsrecv 0
938if eq t 20
939if or ne n 1 ne get Tsrecv 5 1
940tsfail 5
941ret
942end
943if or ne get Tlsstate 0 1 ne get Tsctl 1 0
944tsfail 5
945ret
946end
947if eq get Tsctl 6 6
948tsfail 5
949end
950ret
951end
952if eq t 23
953if or ne get Tsrecv 1 3 ne get Tsrecv 2 3
954tsfail 7
955ret
956end
957tsopen n
958ret
959end
960if ne get Tsctl 6 0
961tsfail 5
962ret
963end
964for i ulong n
965put Tcbuf i get Tsrecv add i 5
966end
967if eq t 22
968tshfeed n
969ret
970end
971if eq t 21
972tsalert n false
973ret
974end
975tsfail 5
976end
977fn tssendf to void
978if not tsfinmac 32
979tsfail 3
980ret
981end
982put Tsmsg 0 20
983put Tsmsg 1 0
984put Tsmsg 2 0
985put Tsmsg 3 32
986for i ulong 32
987put Tsmsg add i 4 get Tcdigest i
988end
989for i ulong 36
990put Tcbuf i get Tsmsg i
991end
992if not tsseal 36 22 0
993tsfail 3
994ret
995end
996note No resumption/exporter: discard transcript and handshake secrets after client Finished.
997for i ulong 192
998put Tssec i 0
999end
1000for i ulong 64
1001put Tskey i 0
1002end
1003for i ulong 24
1004put Tsiv i 0
1005end
1006for i ulong 65536
1007put Tstrans i 0
1008end
1009put Tsctl 2 0
1010put Tsseq 0 0
1011put Tsctl 6 5
1012put Tlsstate 0 2
1013tcwipe
1014certreset
1015end
1016fn tsclosing to void
1017if eq get Tsctl 3 0
1018ret
1019end
1020if ne get Tsctl 4 0
1021if ne get Tsctl 5 0
1022tswipe
1023end
1024ret
1025end
1026if ne get Tlsoutn 0 0
1027ret
1028end
1029put Tcbuf 0 1
1030put Tcbuf 1 0
1031if not tsseal 2 21 1
1032tsfail 3
1033ret
1034end
1035put Tsctl 4 1
1036put Tlsstate 0 3
1037if ne get Tsctl 5 0
1038tswipe
1039end
1040end
1041fn tspump to void
1042if or eq get Tlsstate 0 0 eq get Tlsstate 0 4
1043ret
1044end
1045if eq get Tsctl 6 6
1046if ne get Tlsoutn 0 0
1047ret
1048end
1049tssendf
1050if eq get Tlsstate 0 4
1051ret
1052end
1053end
1054while ge get Tsctl 0 5 bound 13108
1055if ne get Tsctl 5 0
1056break
1057end
1058n = add shl ulong get Tsrecv 3 8 ulong get Tsrecv 4
1059lim = ulong 16384
1060if eq get Tsrecv 0 23
1061lim = 16640
1062end
1063if gt n lim
1064tsfail 4
1065ret
1066end
1067z = add n 5
1068if gt z get Tsctl 0
1069break
1070end
1071tsrecord n
1072if eq get Tlsstate 0 4
1073ret
1074end
1075p = sub get Tsctl 0 z
1076for i ulong p
1077put Tsrecv i get Tsrecv add i z
1078end
1079for i ulong z
1080put Tsrecv add p i 0
1081end
1082put Tsctl 0 p
1083if eq get Tsctl 6 6
1084if ne get Tlsoutn 0 0
1085ret
1086end
1087tssendf
1088if eq get Tlsstate 0 4
1089ret
1090end
1091end
1092end
1093if ne get Tsctl 5 0
1094for i ulong get Tsctl 0
1095put Tsrecv i 0
1096end
1097put Tsctl 0 0
1098end
1099if eq get Tsctl 6 5
1100tsclosing
1101end
1102end
1103fn tlsinput n ulong to void
1104if gt n 65536
1105tsfail 1
1106ret
1107end
1108if or eq get Tlsstate 0 0 eq get Tlsstate 0 4
1109ret
1110end
1111if ne get Tsctl 5 0
1112for i ulong n
1113put Tlsin i 0
1114end
1115put Tlsinlen 0 0
1116ret
1117end
1118p = get Tsctl 0
1119if gt n sub ulong 65536 p
1120tsfail 2
1121ret
1122end
1123for i ulong n
1124put Tsrecv add p i get Tlsin i
1125put Tlsin i 0
1126end
1127put Tsctl 0 add p n
1128put Tlsinlen 0 0
1129tspump
1130end
1131fn tlssent to void
1132n = get Tlsoutn 0
1133for i ulong n
1134put Tlsout i 0
1135end
1136put Tlsoutn 0 0
1137tspump
1138end
1139fn tlswrite n ulong to bool
1140if or ne get Tlsstate 0 2 ne get Tsctl 3 0
1141ret false
1142end
1143if ne get Tlsoutn 0 0
1144ret false
1145end
1146if gt n 16384
1147ret false
1148end
1149if ge get Tsseq 0 4294967295
1150tsfail 13
1151ret false
1152end
1153for i ulong n
1154put Tcbuf i get Tlsplain i
1155end
1156if not tsseal n 23 1
1157tsfail 3
1158ret false
1159end
1160for i ulong n
1161put Tlsplain i 0
1162end
1163tcwipe
1164ret true
1165end
1166fn tlsconsume n ulong to void
1167p = get Tlsappn 0
1168if gt n p
1169tsfail 1
1170ret
1171end
1172p = sub p n
1173for i ulong p
1174put Tlsapp i get Tlsapp add i n
1175end
1176for i ulong n
1177put Tlsapp add p i 0
1178end
1179put Tlsappn 0 p
1180end
1181fn tlsclose to void
1182if or eq get Tlsstate 0 0 eq get Tlsstate 0 4
1183ret
1184end
1185if eq get Tlsstate 0 1
1186tsclear
1187for i ulong 64
1188put Tlsrand i 0
1189end
1190put Tlsstate 0 3
1191ret
1192end
1193put Tsctl 3 1
1194tsclosing
1195end
1196fn tshello to void
1197put Tsctl 7 0
1198tsput 1
1199tsu24 0
1200tsu16 771
1201for i ulong 32
1202tsput get Tlsrand i
1203end
1204tsput 0
1205tsu16 2
1206tsu16 4867
1207tsput 1
1208tsput 0
1209note Extension total patched after serializing exact offered values.
1210tsu16 0
1211tsu16 0
1212tsu16 add ulong 5 len Tshost
1213tsu16 add ulong 3 len Tshost
1214tsput 0
1215tsu16 len Tshost
1216for i ulong len Tshost
1217tsput get Tshost i
1218end
1219tsu16 43
1220tsu16 3
1221tsput 2
1222tsu16 772
1223tsu16 10
1224tsu16 4
1225tsu16 2
1226tsu16 29
1227tsu16 13
1228tsu16 4
1229tsu16 2
1230tsu16 1027
1231tsu16 50
1232tsu16 6
1233tsu16 4
1234tsu16 1027
1235tsu16 1283
1236tsu16 51
1237tsu16 38
1238tsu16 36
1239tsu16 29
1240tsu16 32
1241for i ulong 32
1242tsput get Tcxpub i
1243end
1244tsu16 16
1245tsu16 11
1246tsu16 9
1247tsput 8
1248for i ulong 8
1249tsput get Tsalpn i
1250end
1251n = get Tsctl 7
1252put Tsmsg 1 uchar and shr sub n 4 16 255
1253put Tsmsg 2 uchar and shr sub n 4 8 255
1254put Tsmsg 3 uchar and sub n 4 255
1255put Tsmsg 45 uchar shr sub n 47 8
1256put Tsmsg 46 uchar and sub n 47 255
1257for i ulong n
1258put Tstrans i get Tsmsg i
1259put Tlsout add i 5 get Tsmsg i
1260end
1261put Tsctl 2 n
1262put Tlsout 0 22
1263put Tlsout 1 3
1264put Tlsout 2 1
1265put Tlsout 3 uchar shr n 8
1266put Tlsout 4 uchar and n 255
1267put Tlsoutn 0 add n 5
1268end
1269fn tlsinit now ulong to void
1270note Parent supplies FRESH secure Tlsrand before every init; reset preserves only this input.
1271tsclear
1272if eq now 0
1273tsfail 18
1274ret
1275end
1276z1 = true
1277z2 = true
1278for i ulong 32
1279if ne get Tlsrand i 0
1280z1 = false
1281end
1282if ne get Tlsrand add i 32 0
1283z2 = false
1284end
1285end
1286if or z1 z2
1287tsfail 3
1288ret
1289end
1290put Tsctl 9 now
1291for i ulong 32
1292put Tcxpriv i get Tlsrand add i 32
1293end
1294if not tcxpublic
1295tsfail 3
1296ret
1297end
1298tshello
1299for i ulong 64
1300put Tlsrand i 0
1301end
1302put Tlsstate 0 1
1303end