| 1 |
From Coq Require Init.Byte String. |
| 2 |
From Coq Require Export Init.Byte(byte(..)). |
| 3 |
Export String. |
| 4 |
Require Export coqutil.Datatypes.List. Export Lists.List List.ListNotations. |
| 5 |
From Coq Require Export BinInt. Export Zdiv. Local Open Scope Z_scope. |
| 6 |
Require Export coqutil.Byte coqutil.Word.LittleEndianList. |
| 7 |
|
| 8 |
|
| 9 |
Require Import Crypto.Arithmetic.PrimeFieldTheorems. |
| 10 |
|
| 11 |
(* reference: https://datatracker.ietf.org/doc/html/rfc8439 *) |
| 12 |
|
| 13 |
(* |
| 14 |
In terms of Z: |
| 15 |
Definition poly1305 (p:=2^130-5) (k : list byte) (m : list byte) : list byte := |
| 16 |
let r := Z.land (le_combine (firstn 16 k)) 0x0ffffffc0ffffffc0ffffffc0fffffff in |
| 17 |
let t := fold_left (fun a n => (a+le_combine(n++[x01]))*r mod p) (chunk 16 m) 0 in |
| 18 |
le_split 16 (t + le_combine (skipn 16 k)). |
| 19 |
|
| 20 |
TODO: prove equivalent to current spec |
| 21 |
*) |
| 22 |
|
| 23 |
Definition poly1305 (p:=(2^130-5)%positive) (k : list byte) (m : list byte) : list byte := |
| 24 |
let r := F.of_Z p (Z.land (le_combine (firstn 16 k)) 0x0ffffffc0ffffffc0ffffffc0fffffff) in |
| 25 |
let t := fold_left (fun a n => (a+(F.of_Z p (le_combine(n++[x01]))))*r)%F (chunk 16 m) 0%F in |
| 26 |
le_split 16 ((F.to_Z t) + le_combine (skipn 16 k)). |
| 27 |
|
| 28 |
Local Notation "a + b" := (Z.land (a+b) (Z.ones 32)). |
| 29 |
Local Notation "a ^ b" := (Z.lxor a b). |
| 30 |
Local Notation "a <<< b" := (Z.shiftl a b + Z.shiftr a (32-b)) |
| 31 |
(at level 30). |
| 32 |
|
| 33 |
Definition quarter '(a, b, c, d) := |
| 34 |
let a := a + b in let d := d ^ a in let d := d <<< 16 in |
| 35 |
let c := c + d in let b := b ^ c in let b := b <<< 12 in |
| 36 |
let a := a + b in let d := d ^ a in let d := d <<< 8 in |
| 37 |
let c := c + d in let b := b ^ c in let b := b <<< 7 in |
| 38 |
(a, b, c, d). |
| 39 |
|
| 40 |
Definition quarterround x y z t (st : list Z) := |
| 41 |
let '(a,b,c,d) := quarter (nth x st 0, nth y st 0, nth z st 0, nth t st 0) in |
| 42 |
upd (upd (upd (upd st x a) y b) z c) t d. |
| 43 |
|
| 44 |
Definition chacha20_block (*256bit*)key (*32bit+96bit*)nonce := |
| 45 |
let st := (*512bit*) |
| 46 |
map le_combine (chunk 4 (list_byte_of_string"expand 32-byte k")) |
| 47 |
++ map le_combine (chunk 4 key) |
| 48 |
++ map le_combine (chunk 4 nonce) in |
| 49 |
let ss := Nat.iter 10 (fun ss => |
| 50 |
let ss := quarterround 0 4 8 12 ss in |
| 51 |
let ss := quarterround 1 5 9 13 ss in |
| 52 |
let ss := quarterround 2 6 10 14 ss in |
| 53 |
let ss := quarterround 3 7 11 15 ss in |
| 54 |
let ss := quarterround 0 5 10 15 ss in |
| 55 |
let ss := quarterround 1 6 11 12 ss in |
| 56 |
let ss := quarterround 2 7 8 13 ss in |
| 57 |
let ss := quarterround 3 4 9 14 ss in |
| 58 |
ss) st in |
| 59 |
let st := map (fun '(s, t) => s + t) (combine ss st) in |
| 60 |
flat_map (le_split 4) st. |
| 61 |
|
| 62 |
Definition chacha20_encrypt key start nonce plaintext := |
| 63 |
flat_map (fun '(counter, ck) => |
| 64 |
zip byte.xor (chacha20_block key (le_split 4 (Z.of_nat counter) ++ nonce)) ck) |
| 65 |
(enumerate start (chunk 64 plaintext)). |
| 66 |
|
| 67 |
Definition chacha20poly1305_aead_encrypt aad key iv constant plaintext := |
| 68 |
let pad16 xs := repeat x00 (Nat.div_up (length xs) 16 * 16 - length xs) in |
| 69 |
let nonce := constant ++ iv in |
| 70 |
let otk := firstn 32 (chacha20_block key (le_split 4 0 ++ nonce)) in |
| 71 |
let ciphertext := chacha20_encrypt key 1 nonce plaintext in |
| 72 |
let mac_data := aad ++ pad16 aad in |
| 73 |
let mac_data := mac_data ++ ciphertext ++ pad16 ciphertext in |
| 74 |
let mac_data := mac_data ++ le_split 8 (Z.of_nat (length aad)) in |
| 75 |
let mac_data := mac_data ++ le_split 8 (Z.of_nat (length ciphertext)) in |
| 76 |
let tag := poly1305 otk mac_data in |
| 77 |
(ciphertext, tag). |
| 78 |
|
| 79 |
(* For memory layout, see: spec_of_chacha20 in GarageDoor.v *) |