| 1 |
(* This file has been taken from https://github.com/PrincetonUniversity/VST/blob/master/hmacfcf/HMAC_spec.v *) |
| 2 |
(* It has been formatted to fit your repository *) |
| 3 |
|
| 4 |
(* Copyright 2012-2015 by Adam Petcher. * |
| 5 |
* Use of this source code is governed by the license described * |
| 6 |
* in the LICENSE file at the root of the source tree. *) |
| 7 |
Set Implicit Arguments. |
| 8 |
|
| 9 |
|
| 10 |
Require Import Bvector. |
| 11 |
Require Import List. |
| 12 |
Require Import Arith. |
| 13 |
|
| 14 |
Definition Blist := list bool. |
| 15 |
|
| 16 |
Fixpoint splitVector(A : Set)(n m : nat) : Vector.t A (n + m) -> (Vector.t A n * Vector.t A m) := |
| 17 |
match n with |
| 18 |
| 0 => |
| 19 |
fun (v : Vector.t A (O + m)) => (@Vector.nil A, v) |
| 20 |
| S n' => |
| 21 |
fun (v : Vector.t A (S n' + m)) => |
| 22 |
let (v1, v2) := splitVector _ _ (Vector.tl v) in |
| 23 |
(Vector.cons _ (Vector.hd v) _ v1, v2) |
| 24 |
end. |
| 25 |
|
| 26 |
Section HMAC. |
| 27 |
|
| 28 |
Variable c p : nat. |
| 29 |
Definition b := c + p. |
| 30 |
|
| 31 |
(* The compression function *) |
| 32 |
Variable h : Bvector c -> Bvector b -> Bvector c. |
| 33 |
(* The initialization vector is part of the spec of the hash function. *) |
| 34 |
Variable iv : Bvector c. |
| 35 |
(* The iteration of the compression function gives a keyed hash function on lists of words. *) |
| 36 |
Definition h_star k (m : list (Bvector b)) := |
| 37 |
fold_left h m k. |
| 38 |
(* The composition of the keyed hash function with the IV gives a hash function on lists of words. *) |
| 39 |
Definition hash_words := h_star iv. |
| 40 |
|
| 41 |
Variable Message : Set. |
| 42 |
Variable splitAndPad : Message -> list (Bvector b). |
| 43 |
Hypothesis splitAndPad_1_1 : |
| 44 |
forall b1 b2, |
| 45 |
splitAndPad b1 = splitAndPad b2 -> |
| 46 |
b1 = b2. |
| 47 |
|
| 48 |
Variable fpad : Bvector c -> Bvector p. |
| 49 |
Definition app_fpad (x : Bvector c) : Bvector b := |
| 50 |
(Vector.append x (fpad x)). |
| 51 |
|
| 52 |
Definition h_star_pad k x := |
| 53 |
app_fpad (h_star k x). |
| 54 |
|
| 55 |
Definition GNMAC k m := |
| 56 |
let (k_Out, k_In) := splitVector c c k in |
| 57 |
h k_Out (app_fpad (h_star k_In m)). |
| 58 |
|
| 59 |
(* The "two-key" version of GHMAC and HMAC. *) |
| 60 |
Definition GHMAC_2K (k : Bvector (b + b)) m := |
| 61 |
let (k_Out, k_In) := splitVector b b k in |
| 62 |
let h_in := (hash_words (k_In :: m)) in |
| 63 |
hash_words (k_Out :: (app_fpad h_in) :: nil). |
| 64 |
|
| 65 |
Definition HMAC_2K (k : Bvector (b + b)) (m : Message) := |
| 66 |
GHMAC_2K k (splitAndPad m). |
| 67 |
|
| 68 |
(* opad and ipad are constants defined in the HMAC spec. *) |
| 69 |
Variable opad ipad : Bvector b. |
| 70 |
Hypothesis opad_ne_ipad : opad <> ipad. |
| 71 |
|
| 72 |
Definition GHMAC (k : Bvector b) := |
| 73 |
GHMAC_2K (Vector.append (BVxor _ k opad) (BVxor _ k ipad)). |
| 74 |
|
| 75 |
Definition HMAC (k : Bvector b) := |
| 76 |
HMAC_2K (Vector.append (BVxor _ k opad) (BVxor _ k ipad)). |
| 77 |
|
| 78 |
End HMAC. |