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
| 1 | /- |
| 2 | Omtls.lean: properties of MODELS of omtls's record layer and key schedule (Lean 4, core only, no Mathlib). |
| 3 | Each model is written next to the Omega code it describes (src/tls.om). These theorems do not prove that the |
| 4 | compiled Omega code implements the models; the tests do that work (see proofs/README.md). |
| 5 | -/ |
| 6 | |
| 7 | namespace Omtls |
| 8 | |
| 9 | /-! ## Record-length bounds (RFC 8446 5.1, 5.2; src/tls.om ot_tl_rdrec, ot_tl_dec, ot_tl_pump1, ot_tl_fill) -/ |
| 10 | |
| 11 | /-- The record buffer is `uchar 16645`: 5 header bytes and the body the reader accepted. -/ |
| 12 | def recBuf : Nat := 16645 |
| 13 | /-- ot_tl_rdrec refuses a protected record longer than 2^14 + 256 and a plaintext one longer than 2^14. -/ |
| 14 | def limit (prot : Bool) : Nat := if prot then 16384 + 256 else 16384 |
| 15 | |
| 16 | /-- Every record the reader accepts fits the buffer, header included. -/ |
| 17 | theorem record_fits (prot : Bool) (n : Nat) (h : n ≤ limit prot) : 5 + n ≤ recBuf := by |
| 18 | cases prot <;> simp [limit, recBuf] at * <;> omega |
| 19 | |
| 20 | /-- ot_tl_dec: after the record_overflow check, at most 2^14 plaintext bytes reach ot_tl_pend (`uchar 16400`). -/ |
| 21 | theorem inner_fits_pending (m : Nat) (hcheck : ¬ m > 16384) : m ≤ 16400 := by omega |
| 22 | |
| 23 | /-- Handshake reassembly: a body length read from 24 bits is at most 2^24 - 1, the size of ot_tl_hm. -/ |
| 24 | theorem hs_body_fits (b0 b1 b2 : Nat) (h0 : b0 < 256) (h1 : b1 < 256) (h2 : b2 < 256) : |
| 25 | b0 * 65536 + b1 * 256 + b2 ≤ 16777215 := by omega |
| 26 | |
| 27 | /-- ot_tl_fill appends a record (at most 2^14 bytes) only when at most 3 bytes are queued: |
| 28 | the queue `uchar 32800` never overflows. -/ |
| 29 | theorem queue_fits (q m : Nat) (hq : q ≤ 3) (hm : m ≤ 16384) : q + m ≤ 32800 := by omega |
| 30 | |
| 31 | /-! ## Per-record nonces (RFC 8446 5.3): nonce = iv XOR (64-bit sequence number, left-padded) -/ |
| 32 | |
| 33 | def nonce (iv seq : Nat) : Nat := iv ^^^ seq |
| 34 | |
| 35 | /-- For a fixed key and iv, distinct sequence numbers give distinct nonces. -/ |
| 36 | theorem nonce_injective (iv a b : Nat) (h : nonce iv a = nonce iv b) : a = b := by |
| 37 | unfold nonce at h |
| 38 | have h' : iv ^^^ (iv ^^^ a) = iv ^^^ (iv ^^^ b) := by rw [h] |
| 39 | simpa [← Nat.xor_assoc, Nat.xor_self, Nat.zero_xor] using h' |
| 40 | |
| 41 | /-- ot_tl_dec and ot_tl_wrec refuse sequence number 2^64 - 1, so the counter never wraps: |
| 42 | every sequence number used fits the 64 low bits of the nonce. -/ |
| 43 | theorem seq_fits (s : Nat) (h : s < 2^64 - 1) : s + 1 < 2^64 := by omega |
| 44 | |
| 45 | /-! ## HkdfLabel (RFC 8446 7.1; src/tls.om ot_tl_info) and the key-schedule labels -/ |
| 46 | |
| 47 | /-- uint16 length, opaque label<7..255> = "tls13 " ++ label, opaque context<0..255>. -/ |
| 48 | def hkdfLabel (len : Nat) (label ctx : List Nat) : List Nat := |
| 49 | [len / 256, len % 256] ++ [6 + label.length] ++ ([116, 108, 115, 49, 51, 32] ++ label) ++ [ctx.length] ++ ctx |
| 50 | |
| 51 | theorem hkdfLabel_length (len : Nat) (label ctx : List Nat) : |
| 52 | (hkdfLabel len label ctx).length = 4 + 6 + label.length + ctx.length := by |
| 53 | simp [hkdfLabel]; omega |
| 54 | |
| 55 | /-- Labels of at most 12 bytes and contexts of at most 32 bytes fit ot_tl_info's `uchar 330`. -/ |
| 56 | theorem hkdfLabel_fits (len : Nat) (label ctx : List Nat) (hl : label.length ≤ 12) (hc : ctx.length ≤ 32) : |
| 57 | (hkdfLabel len label ctx).length ≤ 330 := by |
| 58 | rw [hkdfLabel_length]; omega |
| 59 | |
| 60 | /-- The labels used by src/tls.om, as ASCII: derived, c hs traffic, s hs traffic, c ap traffic, s ap traffic, |
| 61 | key, iv, finished, traffic upd. -/ |
| 62 | def labels : List (List Nat) := |
| 63 | [ [100,101,114,105,118,101,100], |
| 64 | [99,32,104,115,32,116,114,97,102,102,105,99], |
| 65 | [115,32,104,115,32,116,114,97,102,102,105,99], |
| 66 | [99,32,97,112,32,116,114,97,102,102,105,99], |
| 67 | [115,32,97,112,32,116,114,97,102,102,105,99], |
| 68 | [107,101,121], |
| 69 | [105,118], |
| 70 | [102,105,110,105,115,104,101,100], |
| 71 | [116,114,97,102,102,105,99,32,117,112,100] ] |
| 72 | |
| 73 | theorem labels_distinct : labels.Nodup := by decide |
| 74 | |
| 75 | theorem labels_short : ∀ l ∈ labels, l.length ≤ 12 := by decide |
| 76 | |
| 77 | /-- The encoding determines the label: two derivations with the same length and context but different |
| 78 | labels feed HKDF-Expand different info strings. -/ |
| 79 | theorem hkdfLabel_injective_label (len : Nat) (l1 l2 ctx : List Nat) |
| 80 | (h : hkdfLabel len l1 ctx = hkdfLabel len l2 ctx) : l1 = l2 := by |
| 81 | simp [hkdfLabel] at h |
| 82 | exact h.2 |
| 83 | |
| 84 | end Omtls |