| 1 |
//////////////////////////////////////////////////////////////// |
| 2 |
// Copyright 2020 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 |
enable_experimental; |
| 23 |
|
| 24 |
include "HMAC/spec/HMAC.saw"; |
| 25 |
|
| 26 |
m <- llvm_load_module "bitcode/all_llvm.bc"; |
| 27 |
|
| 28 |
// Verifies s2n_hmac_init at 3 key sizes in the given config |
| 29 |
let verify_s2n_hmac_init |
| 30 |
(cfg : { name : String |
| 31 |
, hmac_alg : Term |
| 32 |
, digest_size : Int |
| 33 |
, block_size : Int |
| 34 |
, hash_block_size : Int |
| 35 |
}) = do { |
| 36 |
set_base 16; |
| 37 |
|
| 38 |
print ""; |
| 39 |
print (str_concat "Verifying HMAC init: alg = " cfg.name); |
| 40 |
|
| 41 |
print "Creating hash overrides..."; |
| 42 |
(t, hash_init_ov) <- with_time (crucible_llvm_unsafe_assume_spec m "s2n_hash_init" hash_init_spec); |
| 43 |
(t, hmac_digest_size_ov) <- with_time (crucible_llvm_unsafe_assume_spec m "s2n_hmac_digest_size" (hmac_digest_size_spec cfg)); |
| 44 |
(t, hash_copy_ov) <- with_time (crucible_llvm_unsafe_assume_spec m "s2n_hash_copy" hash_copy_spec); |
| 45 |
(t, hash_reset_ov) <- with_time (crucible_llvm_unsafe_assume_spec m "s2n_hash_reset" hash_reset_spec); |
| 46 |
(t, hash_get_currently_in_hash_total_ov) <- with_time (crucible_llvm_unsafe_assume_spec m "s2n_hash_get_currently_in_hash_total" (hash_get_currently_in_hash_total_spec)); |
| 47 |
(t, hash_update_block_size_ov) <- with_time (crucible_llvm_unsafe_assume_spec m "s2n_hash_update" (hash_update_spec cfg.block_size)); |
| 48 |
(t, hash_update_key_size_ov) <- with_time (crucible_llvm_unsafe_assume_spec m "s2n_hash_update" hash_update_unbounded_spec); |
| 49 |
(t, hash_digest_ov) <- with_time (crucible_llvm_unsafe_assume_spec m "s2n_hash_digest" (hash_digest_spec (cfg.digest_size))); |
| 50 |
print "Done"; |
| 51 |
|
| 52 |
let ovs = |
| 53 |
[ hash_init_ov |
| 54 |
, hmac_digest_size_ov |
| 55 |
, hash_copy_ov |
| 56 |
, hash_reset_ov |
| 57 |
, hash_get_currently_in_hash_total_ov |
| 58 |
, hash_update_block_size_ov |
| 59 |
, hash_update_key_size_ov |
| 60 |
, hash_digest_ov |
| 61 |
]; |
| 62 |
let block_size = cfg.block_size; |
| 63 |
|
| 64 |
with_time (crucible_llvm_verify m "s2n_hmac_init" ovs false (hmac_init_spec cfg) z3_hash_unint); |
| 65 |
|
| 66 |
print "Finished proving s2n_hmac_init"; |
| 67 |
}; |
| 68 |
|
| 69 |
// Verifies s2n_hmac_update at the given message sizes and config |
| 70 |
let verify_s2n_hmac_update |
| 71 |
(cfg : { name : String |
| 72 |
, hmac_alg : Term |
| 73 |
, digest_size : Int |
| 74 |
, block_size : Int |
| 75 |
, hash_block_size : Int |
| 76 |
}) = do { |
| 77 |
set_base 16; |
| 78 |
|
| 79 |
print ""; |
| 80 |
print (str_concat "Verifying HMAC update: alg = " cfg.name); |
| 81 |
|
| 82 |
(t, hash_update_msg_size_ov) <- with_time (crucible_llvm_unsafe_assume_spec m "s2n_hash_update" hash_update_unbounded_spec); |
| 83 |
with_time (crucible_llvm_verify m "s2n_hmac_update" [hash_update_msg_size_ov] false (hmac_update_spec cfg) z3_hash_unint); |
| 84 |
|
| 85 |
print "Finished proving s2n_hmac_update"; |
| 86 |
}; |
| 87 |
|
| 88 |
// Verifies s2n_hmac_digest |
| 89 |
let verify_s2n_hmac_digest |
| 90 |
(cfg : { name : String |
| 91 |
, hmac_alg : Term |
| 92 |
, digest_size : Int |
| 93 |
, block_size : Int |
| 94 |
, hash_block_size : Int |
| 95 |
}) = do { |
| 96 |
set_base 16; |
| 97 |
|
| 98 |
print ""; |
| 99 |
print (str_concat "Verifying HMAC digest: alg = " cfg.name); |
| 100 |
|
| 101 |
print "Creating hash overrides..."; |
| 102 |
(t, hash_copy_ov) <- with_time (crucible_llvm_unsafe_assume_spec m "s2n_hash_copy" hash_copy_spec); |
| 103 |
(t, hash_update_block_size_ov) <- with_time (crucible_llvm_unsafe_assume_spec m "s2n_hash_update" (hash_update_spec cfg.digest_size)); |
| 104 |
(t, hash_digest_ov) <- with_time (crucible_llvm_unsafe_assume_spec m "s2n_hash_digest" (hash_digest_spec (cfg.digest_size))); |
| 105 |
print "Done"; |
| 106 |
|
| 107 |
let ovs = |
| 108 |
[ hash_copy_ov |
| 109 |
, hash_update_block_size_ov |
| 110 |
, hash_digest_ov |
| 111 |
]; |
| 112 |
|
| 113 |
(t, hmac_digest_ov) <- |
| 114 |
with_time (crucible_llvm_verify m "s2n_hmac_digest" ovs false (hmac_digest_spec cfg) z3_hash_unint); |
| 115 |
|
| 116 |
print "Finished proving s2n_hmac_digest"; |
| 117 |
}; |
| 118 |
|
| 119 |
let none_cfg = |
| 120 |
{ name = "NONE" |
| 121 |
, hmac_alg = {{ S2N_HMAC_NONE }} |
| 122 |
, digest_size = 0 |
| 123 |
, block_size = 64 |
| 124 |
, hash_block_size = 64 |
| 125 |
}; |
| 126 |
|
| 127 |
// let sslv3_md5_cfg = <not supported> |
| 128 |
|
| 129 |
let md5_cfg = |
| 130 |
{ name = "MD5" |
| 131 |
, hmac_alg = {{ S2N_HMAC_MD5 }} |
| 132 |
, digest_size = 16 |
| 133 |
, block_size = 64 |
| 134 |
, hash_block_size = 64 |
| 135 |
}; |
| 136 |
|
| 137 |
// let sslv3_sha1_cfg = <not supported> |
| 138 |
|
| 139 |
let sha1_cfg = |
| 140 |
{ name = "SHA1" |
| 141 |
, hmac_alg = {{ S2N_HMAC_SHA1 }} |
| 142 |
, digest_size = 20 |
| 143 |
, block_size = 64 |
| 144 |
, hash_block_size = 64 |
| 145 |
}; |
| 146 |
|
| 147 |
let sha224_cfg = |
| 148 |
{ name = "SHA224" |
| 149 |
, hmac_alg = {{ S2N_HMAC_SHA224 }} |
| 150 |
, digest_size = 28 |
| 151 |
, block_size = 64 |
| 152 |
, hash_block_size = 64 |
| 153 |
}; |
| 154 |
|
| 155 |
let sha256_cfg = |
| 156 |
{ name = "SHA256" |
| 157 |
, hmac_alg = {{ S2N_HMAC_SHA256 }} |
| 158 |
, digest_size = 32 |
| 159 |
, block_size = 64 |
| 160 |
, hash_block_size = 64 |
| 161 |
}; |
| 162 |
|
| 163 |
let sha384_cfg = |
| 164 |
{ name = "SHA384" |
| 165 |
, hmac_alg = {{ S2N_HMAC_SHA384 }} |
| 166 |
, digest_size = 48 |
| 167 |
, block_size = 128 |
| 168 |
, hash_block_size = 128 |
| 169 |
}; |
| 170 |
|
| 171 |
let sha512_cfg = |
| 172 |
{ name = "SHA512" |
| 173 |
, hmac_alg = {{ S2N_HMAC_SHA512 }} |
| 174 |
, digest_size = 64 |
| 175 |
, block_size = 128 |
| 176 |
, hash_block_size = 128 |
| 177 |
}; |
| 178 |
|
| 179 |
let verify_s2n_hmac_at_config |
| 180 |
(cfg : { name : String |
| 181 |
, hmac_alg : Term |
| 182 |
, digest_size : Int |
| 183 |
, block_size : Int |
| 184 |
, hash_block_size : Int |
| 185 |
}) = do { |
| 186 |
verify_s2n_hmac_init cfg; |
| 187 |
verify_s2n_hmac_update cfg; |
| 188 |
verify_s2n_hmac_digest cfg; |
| 189 |
}; |
| 190 |
|
| 191 |
let verify_s2n_hmac = do { |
| 192 |
let configs = [md5_cfg, sha1_cfg, sha224_cfg, sha256_cfg, sha384_cfg, sha512_cfg]; |
| 193 |
|
| 194 |
for configs (\config -> verify_s2n_hmac_at_config config); |
| 195 |
}; |
| 196 |
|
| 197 |
verify_s2n_hmac; |