| 1 |
//////////////////////////////////////////////////////////////// |
| 2 |
// Copyright 2016 Galois, Inc. All Rights Reserved |
| 3 |
// |
| 4 |
// Authors: |
| 5 |
// Aaron Tomb : [email protected] |
| 6 |
// Nathan Collins : [email protected] |
| 7 |
// Joey Dodds : [email protected] |
| 8 |
// |
| 9 |
// Licensed under the Apache License, Version 2.0 (the "License"). |
| 10 |
// You may not use this file except in compliance with the License. |
| 11 |
// A copy of the License is located at |
| 12 |
// |
| 13 |
// http://aws.amazon.com/apache2.0 |
| 14 |
// |
| 15 |
// or in the "license" file accompanying this file. This file is distributed |
| 16 |
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 17 |
// express or implied. See the License for the specific language governing |
| 18 |
// permissions and limitations under the License. |
| 19 |
// |
| 20 |
//////////////////////////////////////////////////////////////// |
| 21 |
|
| 22 |
module HMAC_properties where |
| 23 |
|
| 24 |
import HMAC |
| 25 |
import Hashing |
| 26 |
import HMAC_iterative |
| 27 |
|
| 28 |
//////// Equivalence of implementations //////// |
| 29 |
// |
| 30 |
// This is specialized to SHA256, since we don't have concrete |
| 31 |
// implementations of the other algorithms. |
| 32 |
hmac_c_state : { key_size, msg_size } |
| 33 |
( 32 >= width msg_size, 64 >= width (8 * key_size) ) |
| 34 |
=> HMAC_c_state -> [key_size][8] -> [msg_size][8] -> [SHA256_DIGEST_LENGTH * 8] |
| 35 |
hmac_c_state st0 key msg = digest |
| 36 |
where |
| 37 |
(st1, digest) = |
| 38 |
hmac_digest_c_state `{block_size=64} |
| 39 |
(hmac_update_c_state |
| 40 |
(hmac_init_c_state `{block_size=64,hash_block_size=64,digest_size=SHA256_DIGEST_LENGTH} |
| 41 |
st0 alg key) |
| 42 |
msg) |
| 43 |
// Specialize to SHA256. |
| 44 |
alg = S2N_HMAC_SHA256 |
| 45 |
|
| 46 |
hmac_c_state_correct : { key_size, msg_size } |
| 47 |
( 32 >= width msg_size, 64 >= width (8 * key_size) ) |
| 48 |
=> HMAC_c_state -> [key_size][8] -> [msg_size][8] -> Bit |
| 49 |
property hmac_c_state_correct st0 key msg = |
| 50 |
hmacSHA256 key msg == hmac_c_state st0 key msg |
| 51 |
|
| 52 |
hmac_c_state_multi : { key_size, msg_size, msg_chunks} |
| 53 |
( 32 >= width msg_size, 64 >= width (8 * key_size), fin msg_chunks ) |
| 54 |
=> HMAC_c_state -> [key_size][8] -> [msg_chunks][msg_size][8] -> [SHA256_DIGEST_LENGTH * 8] |
| 55 |
hmac_c_state_multi st0 key msgs = digest |
| 56 |
where |
| 57 |
initial_state = (hmac_init_c_state `{block_size=64,hash_block_size=64,digest_size=SHA256_DIGEST_LENGTH} |
| 58 |
st0 alg key) |
| 59 |
mid_state = hmac_update_c_state_multi initial_state msgs |
| 60 |
(st1, digest) = hmac_digest_c_state `{block_size=64} mid_state |
| 61 |
// Specialize to SHA256. |
| 62 |
alg = S2N_HMAC_SHA256 |
| 63 |
|
| 64 |
hmac_update_c_state_multi : {msg_size, msg_chunks} |
| 65 |
( 32 >= width msg_size, fin msg_chunks) |
| 66 |
=> HMAC_c_state -> [msg_chunks][msg_size][8] -> HMAC_c_state |
| 67 |
hmac_update_c_state_multi st msgs = states ! 0 |
| 68 |
where |
| 69 |
states = [st] # [hmac_update_c_state s msg | msg <- msgs | s <- states] |
| 70 |
|
| 71 |
hmac_c_state_multi_correct : { key_size, msg_size, msg_chunks } |
| 72 |
( 32 >= width msg_size |
| 73 |
, 64 >= width (8 * key_size) |
| 74 |
, fin msg_chunks |
| 75 |
, 32 >= width (msg_chunks * msg_size) |
| 76 |
, 64 >= width (8 * (64 + msg_size * msg_chunks)) |
| 77 |
) |
| 78 |
=> HMAC_c_state -> [key_size][8] -> [msg_chunks][msg_size][8] -> Bit |
| 79 |
property hmac_c_state_multi_correct st0 key msgs = |
| 80 |
hmacSHA256 key (join msgs) == hmac_c_state_multi st0 key msgs |
| 81 |
|
| 82 |
hmac_update_append x y s = |
| 83 |
hmac_update_c_state (hmac_update_c_state s x) y == hmac_update_c_state s (x # y) |
| 84 |
|
| 85 |
hash_update_append x y s = |
| 86 |
hash_update_c_state (hash_update_c_state s x) y == hash_update_c_state s (x # y) |
| 87 |
|
| 88 |
hmac_update_append_init x y k st0 = |
| 89 |
hmac_update_c_state (hmac_update_c_state s x) y == hmac_update_c_state s (x # y) |
| 90 |
where |
| 91 |
s = hmac_init_c_state st0 S2N_HMAC_SHA256 k |
| 92 |
|
| 93 |
property hash_update_empty s = hash_update_c_state s [] == s |
| 94 |
|
| 95 |
property hmac_update_empty s = |
| 96 |
s.currently_in_hash_block == s.currently_in_hash_block % (zero # s.block_size) |
| 97 |
==> |
| 98 |
hmac_update_c_state s [] == s |
| 99 |
|
| 100 |
property pass = |
| 101 |
~zero == |
| 102 |
[ hmacSHA256 [0x0b | _ <- [1..20] : [_][6]] "Hi There" == 0xb0344c61d8db38535ca8afceaf0bf12b881dc200c9833da726e9376c2e32cff7 |
| 103 |
, hmacSHA256 "Jefe" "what do ya want for nothing?" == 0x5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843 |
| 104 |
] |