interplane.Get Omega
Read in your language

omtls

omtls: a TLS 1.3 client (RFC 8446) written fresh in seeded Omega, for mailhand. Not audited by a cryptographer. Free (Apache-2.0).

Brought by drakestapleton

branch main Branches Tags History

omtls / src / b64.om

112 lines · 2573 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note b64.om (ot_b64_): strict base64 decoding (RFC 4648 section 4) of bytes in the store ot_der_m.
3note Whitespace (space, tab, CR, LF) between characters is skipped, as RFC 7468 section 3 lets a PEM
4note body wrap lines. Everything else is refused: a character outside the alphabet, a total count
5note that is not a multiple of four, "=" anywhere but the last one or two places of the last group,
6note non-zero pad bits (RFC 4648 3.5: refused here), and any character after the padded group.
7note Output may overlap the input as long as o <= p: each group of four input characters writes at
8note most three bytes, so the write position never passes the read position.
9note Unit order: src/der.om must come earlier in the unit (a global is declared before its first use).
10
11note ot_b64_val: 0..63 for an alphabet character, -2 for "=", -3 for whitespace, -1 otherwise
12fn ot_b64_val c uchar to long
13if and ge c 65 le c 90
14ret long sub c 65
15end
16if and ge c 97 le c 122
17ret add long sub c 97 26
18end
19if and ge c 48 le c 57
20ret add long sub c 48 52
21end
22if eq c 43
23ret 62
24end
25if eq c 47
26ret 63
27end
28if eq c 61
29ret -2
30end
31if or or eq c 32 eq c 9 or eq c 10 eq c 13
32ret -3
33end
34ret -1
35end
36
37note ot_b64_r: decoder state: 0 group bits, 1 characters in the group, 2 "=" seen, 3 write position
38global ot_b64_r ulong 4
39
40note ot_b64_grp: one complete group of four: write 3 - pad bytes; false on non-zero pad bits
41fn ot_b64_grp to bool
42a = get ot_b64_r 0
43pd = get ot_b64_r 2
44w = get ot_b64_r 3
45if eq pd 1
46if ne and a 255 0
47ret false
48end
49end
50if eq pd 2
51if ne and a 65535 0
52ret false
53end
54end
55put ot_der_m w uchar shr a 16
56if lt pd 2
57put ot_der_m add w 1 uchar and shr a 8 255
58end
59if eq pd 0
60put ot_der_m add w 2 uchar and a 255
61end
62put ot_b64_r 3 add w sub 3 pd
63put ot_b64_r 0 0
64put ot_b64_r 1 0
65ret true
66end
67
68note ot_b64_dec: decode the characters in store [p, e) to store offset o; the byte count or -1
69fn ot_b64_dec p ulong e ulong o ulong to long
70if gt o p
71ret -1
72end
73put ot_b64_r 0 0
74put ot_b64_r 1 0
75put ot_b64_r 2 0
76put ot_b64_r 3 o
77dn = false
78i = p
79while lt i e bound 16777216
80v = ot_b64_val get ot_der_m i
81i = add i 1
82if ne v -3
83if or eq v -1 dn
84ret -1
85end
86k = get ot_b64_r 1
87if eq v -2
88if lt k 2
89ret -1
90end
91put ot_b64_r 2 add get ot_b64_r 2 1
92put ot_b64_r 0 shl get ot_b64_r 0 6
93else
94if gt get ot_b64_r 2 0
95ret -1
96end
97put ot_b64_r 0 or shl get ot_b64_r 0 6 ulong v
98end
99put ot_b64_r 1 add k 1
100if eq k 3
101dn = gt get ot_b64_r 2 0
102if eq ot_b64_grp false
103ret -1
104end
105end
106end
107end
108if ne get ot_b64_r 1 0
109ret -1
110end
111ret long sub get ot_b64_r 3 o
112end