| 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_iterative where |
| 23 |
|
| 24 |
import Array |
| 25 |
import SHA256 |
| 26 |
import Hashing |
| 27 |
|
| 28 |
//////////////////////////////////////////////////////////////// |
| 29 |
// HMAC (specialized to SHA256). |
| 30 |
//////////////////////////////////////////////////////////////// |
| 31 |
|
| 32 |
// s2n_hmac.h |
| 33 |
/* |
| 34 |
typedef enum { S2N_HMAC_NONE, S2N_HMAC_MD5, S2N_HMAC_SHA1, S2N_HMAC_SHA224, S2N_HMAC_SHA256, S2N_HMAC_SHA384, |
| 35 |
S2N_HMAC_SHA512, S2N_HMAC_SSLv3_MD5, S2N_HMAC_SSLv3_SHA1 |
| 36 |
} s2n_hmac_algorithm; |
| 37 |
*/ |
| 38 |
|
| 39 |
S2N_HMAC_NONE = 0:[32] |
| 40 |
S2N_HMAC_MD5 = 1:[32] |
| 41 |
S2N_HMAC_SHA1 = 2:[32] |
| 42 |
S2N_HMAC_SHA224 = 3:[32] |
| 43 |
S2N_HMAC_SHA256 = 4:[32] |
| 44 |
S2N_HMAC_SHA384 = 5:[32] |
| 45 |
S2N_HMAC_SHA512 = 6:[32] |
| 46 |
S2N_HMAC_SSLv3_MD5 = 7:[32] |
| 47 |
S2N_HMAC_SSLv3_SHA1 = 8:[32] |
| 48 |
|
| 49 |
// s2n_hmac.h |
| 50 |
/* |
| 51 |
|
| 52 |
struct s2n_hmac_state { |
| 53 |
s2n_hmac_algorithm alg; |
| 54 |
|
| 55 |
uint16_t hash_block_size; |
| 56 |
uint32_t currently_in_hash_block; |
| 57 |
uint16_t block_size; |
| 58 |
uint8_t digest_size; |
| 59 |
|
| 60 |
struct s2n_hash_state inner; |
| 61 |
struct s2n_hash_state inner_just_key; |
| 62 |
struct s2n_hash_state outer; |
| 63 |
|
| 64 |
/* key needs to be as large as the biggest block size */ |
| 65 |
uint8_t xor_pad[128]; |
| 66 |
|
| 67 |
/* For storing the inner digest */ |
| 68 |
uint8_t digest_pad[SHA512_DIGEST_LENGTH]; |
| 69 |
}; |
| 70 |
*/ |
| 71 |
// No surprises in the LLVM (:/src/hmac.ll) |
| 72 |
/* |
| 73 |
%struct.s2n_hmac_state = type { i32, i16, i32, i16, i8, %struct.s2n_hash_state, %struct.s2n_hash_state, %struct.s2n_hash_state, [128 x i8], [64 x i8] } |
| 74 |
*/ |
| 75 |
|
| 76 |
type HMAC_c_state = |
| 77 |
{ alg : [32] |
| 78 |
, hash_block_size : [16] |
| 79 |
, currently_in_hash_block : [32] |
| 80 |
, block_size : [16] |
| 81 |
, digest_size : [8] |
| 82 |
, inner : SHA512_c_state |
| 83 |
, inner_just_key : SHA512_c_state |
| 84 |
, outer : SHA512_c_state |
| 85 |
, outer_just_key : SHA512_c_state |
| 86 |
, xor_pad : [128][8] |
| 87 |
, digest_pad : [SHA512_DIGEST_LENGTH][8] |
| 88 |
} |
| 89 |
|
| 90 |
//////////////////////////////////////////////////////////////// |
| 91 |
// Deep HMAC specs in terms of C HMAC state. |
| 92 |
// |
| 93 |
// Here "deep" because we reimplement the HMAC functions to call the |
| 94 |
// corresponding hash functions on the raw C hash states, instead of |
| 95 |
// converting them to Cryptol SHA256 hash states. |
| 96 |
|
| 97 |
type ByteArray = Array[64][8] |
| 98 |
|
| 99 |
type hash_init_ty = |
| 100 |
SHA512_c_state -> SHA512_c_state |
| 101 |
type hash_update_ty msg_size = |
| 102 |
SHA512_c_state -> [msg_size][8] -> SHA512_c_state |
| 103 |
type hash_digest_ty digest_size = |
| 104 |
SHA512_c_state -> [digest_size][8] |
| 105 |
|
| 106 |
hash_init_c_state : hash_init_ty |
| 107 |
hash_init_c_state = sha256_init_sha512_c_state |
| 108 |
//hash_init_c_state = undefined |
| 109 |
|
| 110 |
// Cryptol does not support polymorphic arguments (rank 2 |
| 111 |
// polymorphism), so making the hash update function a parameter to |
| 112 |
// the hmac functions is annoying, since we must pass a separate |
| 113 |
// monomorphic copy for each use at a different type. Instead, we just |
| 114 |
// define it at the top level, but can leave it uninterpreted in the |
| 115 |
// "generic" verification. |
| 116 |
hash_update_c_state : |
| 117 |
{msg_size} (fin msg_size) => hash_update_ty msg_size |
| 118 |
hash_update_c_state = sha256_update_sha512_c_state |
| 119 |
//hash_update_c_state = undefined |
| 120 |
hash_update_c_state_unbounded : |
| 121 |
SHA512_c_state -> ByteArray -> [32] -> SHA512_c_state |
| 122 |
hash_update_c_state_unbounded = undefined |
| 123 |
|
| 124 |
|
| 125 |
hash_digest_c_state : |
| 126 |
{digest_size} (fin digest_size) => hash_digest_ty digest_size |
| 127 |
// To support any digest length, we pad and truncate as necessary. |
| 128 |
// This implementation only makes sense for SHA256 size params, but |
| 129 |
// that is not a concern since we leave these functions uninterpreted |
| 130 |
// in the verification against the S2N C code (we want a concrete |
| 131 |
// implementation for debugging and to compare with our functional spec). |
| 132 |
hash_digest_c_state st = |
| 133 |
take `{digest_size} |
| 134 |
(sha256_digest_sha512_c_state st # (zero : [inf][8])) |
| 135 |
//hash_digest_c_state = undefined |
| 136 |
|
| 137 |
|
| 138 |
// Cases depending on key size: |
| 139 |
// |
| 140 |
// * small key (key size <= block size) |
| 141 |
// |
| 142 |
// copy key into 'xor_pad', up to key size |
| 143 |
// |
| 144 |
// * large key (key size > block size) |
| 145 |
// |
| 146 |
// update outer state with key to key size |
| 147 |
// digest outer state into digest pad at digest size |
| 148 |
// |
| 149 |
// The C code that follows the key init sets all remaining bytes, |
| 150 |
// up to block size, to 0x36, and xors the other bytes with 0x36. |
| 151 |
// This is equivalent to setting upper bytes (i.e. up to block size) |
| 152 |
// to zero in key init, and then xoring everything with 0x36. |
| 153 |
// |
| 154 |
// We don't care about 'xor_pad' and 'outer', so we should be able to |
| 155 |
// just ignore them. But for now we just compute them anyway. |
| 156 |
key_init_c_state : { key_size, block_size, digest_size } |
| 157 |
( fin key_size, fin block_size, |
| 158 |
SHA512_DIGEST_LENGTH >= digest_size ) |
| 159 |
=> SHA512_c_state |
| 160 |
-> [SHA512_DIGEST_LENGTH][8] |
| 161 |
-> [key_size][8] |
| 162 |
-> (SHA512_c_state, [SHA512_DIGEST_LENGTH][8], [block_size][8]) |
| 163 |
key_init_c_state outer0 digest_pad0 key = |
| 164 |
if `key_size > (`block_size : [max (width key_size) (width block_size)]) |
| 165 |
then (outer2, digest_pad1, hash') |
| 166 |
else (outer1, digest_pad0, key') |
| 167 |
where |
| 168 |
outer1 = hash_init_c_state outer0 |
| 169 |
|
| 170 |
// Long key. |
| 171 |
outer2 = hash_update_c_state outer1 key |
| 172 |
hash : [digest_size][8] |
| 173 |
hash = hash_digest_c_state outer2 |
| 174 |
digest_pad1 = hash # drop `{digest_size} digest_pad0 |
| 175 |
hash' = take `{block_size} (hash # (zero : [block_size][8])) |
| 176 |
|
| 177 |
// Short key. |
| 178 |
key' = take `{block_size} (key # (zero : [block_size][8])) |
| 179 |
|
| 180 |
key_init_c_state_unbounded : { block_size, digest_size } |
| 181 |
( 16 >= width block_size, SHA512_DIGEST_LENGTH >= digest_size ) |
| 182 |
=> SHA512_c_state |
| 183 |
-> [SHA512_DIGEST_LENGTH][8] |
| 184 |
-> ByteArray |
| 185 |
-> [32] |
| 186 |
-> (SHA512_c_state, [SHA512_DIGEST_LENGTH][8], [block_size][8]) |
| 187 |
key_init_c_state_unbounded outer0 digest_pad0 key klen = |
| 188 |
if klen > (`block_size : [32]) |
| 189 |
then (outer2, digest_pad1, hash') |
| 190 |
else (outer1, digest_pad0, key') |
| 191 |
where |
| 192 |
outer1 = hash_init_c_state outer0 |
| 193 |
|
| 194 |
// Long key. |
| 195 |
outer2 = hash_update_c_state_unbounded outer1 key klen |
| 196 |
hash : [digest_size][8] |
| 197 |
hash = hash_digest_c_state outer2 |
| 198 |
digest_pad1 = hash # drop `{digest_size} digest_pad0 |
| 199 |
hash' = take `{block_size} (hash # (zero : [block_size][8])) |
| 200 |
|
| 201 |
// Short key. |
| 202 |
key' = map |
| 203 |
(\i -> if (i <$ (0 # klen)) then (arrayLookup key i) else 0) |
| 204 |
(take`{block_size} [0 .. block_size]) |
| 205 |
|
| 206 |
hmac_init_c_state : |
| 207 |
{ key_size, block_size, hash_block_size, digest_size } |
| 208 |
( fin key_size |
| 209 |
, 64 >= width (8*key_size) |
| 210 |
, 16 >= width hash_block_size |
| 211 |
, 16 >= width block_size |
| 212 |
, 8 >= width digest_size |
| 213 |
, 128 >= block_size |
| 214 |
, 64 >= digest_size ) |
| 215 |
=> HMAC_c_state |
| 216 |
-> [32] |
| 217 |
-> [key_size][8] |
| 218 |
-> HMAC_c_state |
| 219 |
hmac_init_c_state st0 alg key = |
| 220 |
{ alg = alg |
| 221 |
, hash_block_size = `hash_block_size |
| 222 |
, currently_in_hash_block = currently_in_hash_block |
| 223 |
, block_size = `block_size |
| 224 |
, digest_size = `digest_size |
| 225 |
|
| 226 |
, inner = inner |
| 227 |
, inner_just_key = inner_just_key |
| 228 |
, outer = outer |
| 229 |
, outer_just_key = outer_just_key |
| 230 |
, xor_pad = xor_pad |
| 231 |
, digest_pad = digest_pad |
| 232 |
} |
| 233 |
where |
| 234 |
currently_in_hash_block = 0 |
| 235 |
|
| 236 |
k0 : [block_size][8] |
| 237 |
(outer, digest_pad, k0) = |
| 238 |
key_init_c_state `{digest_size=digest_size} st0.outer st0.digest_pad key |
| 239 |
ikey = [ k ^ 0x36 | k <- k0 ] |
| 240 |
okey = [ k ^ 0x6a | k <- ikey ] |
| 241 |
|
| 242 |
inner_just_key = hash_update_c_state |
| 243 |
(hash_init_c_state st0.inner_just_key) ikey |
| 244 |
inner = inner_just_key |
| 245 |
outer_just_key = hash_update_c_state |
| 246 |
(hash_init_c_state st0.outer_just_key) okey |
| 247 |
xor_pad = zero //okey # drop st0.xor_pad |
| 248 |
|
| 249 |
hmac_init_c_state_unbounded : |
| 250 |
{ block_size, hash_block_size, digest_size } |
| 251 |
( 16 >= width hash_block_size |
| 252 |
, 16 >= width block_size |
| 253 |
, 8 >= width digest_size |
| 254 |
, 128 >= block_size |
| 255 |
, 64 >= digest_size ) |
| 256 |
=> HMAC_c_state |
| 257 |
-> [32] |
| 258 |
-> ByteArray |
| 259 |
-> [32] |
| 260 |
-> HMAC_c_state |
| 261 |
hmac_init_c_state_unbounded st0 alg key klen = |
| 262 |
{ alg = alg |
| 263 |
, hash_block_size = `hash_block_size |
| 264 |
, currently_in_hash_block = currently_in_hash_block |
| 265 |
, block_size = `block_size |
| 266 |
, digest_size = `digest_size |
| 267 |
|
| 268 |
, inner = inner |
| 269 |
, inner_just_key = inner_just_key |
| 270 |
, outer = outer |
| 271 |
, outer_just_key = outer_just_key |
| 272 |
, xor_pad = xor_pad |
| 273 |
, digest_pad = digest_pad |
| 274 |
} |
| 275 |
where |
| 276 |
currently_in_hash_block = 0 |
| 277 |
|
| 278 |
k0 : [block_size][8] |
| 279 |
(outer, digest_pad, k0) = |
| 280 |
key_init_c_state_unbounded `{digest_size=digest_size} |
| 281 |
st0.outer st0.digest_pad key klen |
| 282 |
ikey = [ k ^ 0x36 | k <- k0 ] |
| 283 |
okey = [ k ^ 0x6a | k <- ikey ] |
| 284 |
|
| 285 |
inner_just_key = hash_update_c_state |
| 286 |
(hash_init_c_state st0.inner_just_key) ikey |
| 287 |
inner = inner_just_key |
| 288 |
outer_just_key = hash_update_c_state |
| 289 |
(hash_init_c_state st0.outer_just_key) okey |
| 290 |
xor_pad = zero //okey # drop st0.xor_pad |
| 291 |
|
| 292 |
|
| 293 |
hmac_update_c_state : {msg_size} (32 >= width msg_size) => |
| 294 |
HMAC_c_state -> [msg_size][8] -> HMAC_c_state |
| 295 |
hmac_update_c_state s m = |
| 296 |
{ inner = hash_update_c_state s.inner m |
| 297 |
, currently_in_hash_block = |
| 298 |
(s.currently_in_hash_block + (`msg_size % (zero # s.hash_block_size))) % |
| 299 |
(zero # s.block_size) |
| 300 |
|
| 301 |
// Rest unchanged. |
| 302 |
, alg = s.alg |
| 303 |
, hash_block_size = s.hash_block_size |
| 304 |
, block_size = s.block_size |
| 305 |
, digest_size = s.digest_size |
| 306 |
, inner_just_key = s.inner_just_key |
| 307 |
, outer = s.outer |
| 308 |
, outer_just_key = s.outer_just_key |
| 309 |
, xor_pad = s.xor_pad |
| 310 |
, digest_pad = s.digest_pad |
| 311 |
} |
| 312 |
|
| 313 |
hmac_update_c_state_unbounded : |
| 314 |
HMAC_c_state -> ByteArray -> [32] -> HMAC_c_state |
| 315 |
hmac_update_c_state_unbounded s m sz = |
| 316 |
{ inner = hash_update_c_state_unbounded s.inner m sz |
| 317 |
, currently_in_hash_block = |
| 318 |
(s.currently_in_hash_block + (sz % (zero # s.hash_block_size))) % |
| 319 |
(zero # s.block_size) |
| 320 |
|
| 321 |
// Rest unchanged. |
| 322 |
, alg = s.alg |
| 323 |
, hash_block_size = s.hash_block_size |
| 324 |
, block_size = s.block_size |
| 325 |
, digest_size = s.digest_size |
| 326 |
, inner_just_key = s.inner_just_key |
| 327 |
, outer = s.outer |
| 328 |
, outer_just_key = s.outer_just_key |
| 329 |
, xor_pad = s.xor_pad |
| 330 |
, digest_pad = s.digest_pad |
| 331 |
} |
| 332 |
|
| 333 |
|
| 334 |
// TODO: What about `size` argument to `s2n_hmac_digest`? The `size` |
| 335 |
// argument is supposed to be the "digest length" of the underlying |
| 336 |
// hash. Here we are specializing to SHA256, so `size` would be 32. |
| 337 |
hmac_digest_c_state : |
| 338 |
{ block_size, digest_size } |
| 339 |
( 64 >= digest_size |
| 340 |
, 128 >= block_size ) |
| 341 |
=> HMAC_c_state -> (HMAC_c_state, [8 * digest_size]) |
| 342 |
hmac_digest_c_state s = (sout, out) |
| 343 |
where |
| 344 |
hin : [digest_size][8] |
| 345 |
hin = hash_digest_c_state inner |
| 346 |
digest_pad : [SHA512_DIGEST_LENGTH][8] |
| 347 |
digest_pad = hin # zero |
| 348 |
|
| 349 |
okey : [block_size][8] |
| 350 |
okey = take s.xor_pad |
| 351 |
|
| 352 |
// The `inner` and `outer` here are probably not accurate: |
| 353 |
// in s2n, the `s2n_hash_digest` has been called on them, which |
| 354 |
// presumably can change them (calls `SHA256_Final` from |
| 355 |
// underlying C crypto lib behind the scenes). However, our |
| 356 |
// Cryptol `SHA256Final` does not change the hash state. Even |
| 357 |
// if we leave the hash function uninterpreted later, we will |
| 358 |
// still need to change the interface of our Cryptol `<hash>Final` |
| 359 |
// to additionally return an updated `<hash>State`. |
| 360 |
// |
| 361 |
// However, we probably don't need to say anything about the |
| 362 |
// *hash* state after calls to "final" functions, since |
| 363 |
// 's2n_hash_reset' just calls 's2n_hash_init'. So, the above |
| 364 |
// concern is not much of a concern after all. |
| 365 |
|
| 366 |
// Our goal is to leave the hash functions (init, update, |
| 367 |
// digest/final) uninterpreted, and so we need the structure of |
| 368 |
// our calls to these functions to match the structure of calls in |
| 369 |
// the s2n code, since we don't have lemmas giving algebraic |
| 370 |
// properties of these functions (e.g. here we'd need |
| 371 |
// |
| 372 |
// update (update st x) y == update st (x # y) |
| 373 |
// |
| 374 |
// ). So, we replace |
| 375 |
// |
| 376 |
//outer = SHA256Update SHA256Init (okey # hin) |
| 377 |
// |
| 378 |
// with: |
| 379 |
outer = hash_update_c_state s.outer_just_key hin |
| 380 |
inner = s.inner |
| 381 |
|
| 382 |
out = join (hash_digest_c_state outer) |
| 383 |
|
| 384 |
sout : HMAC_c_state |
| 385 |
sout = |
| 386 |
{ inner = inner |
| 387 |
, outer = s.outer_just_key |
| 388 |
, digest_pad = digest_pad |
| 389 |
|
| 390 |
// Rest unchanged. |
| 391 |
, alg = s.alg |
| 392 |
, hash_block_size = s.hash_block_size |
| 393 |
, currently_in_hash_block = s.currently_in_hash_block |
| 394 |
, block_size = s.block_size |
| 395 |
, digest_size = s.digest_size |
| 396 |
, inner_just_key = s.inner_just_key |
| 397 |
, outer_just_key = s.outer_just_key |
| 398 |
, xor_pad = s.xor_pad |
| 399 |
} |