| 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 Hashing where |
| 23 |
|
| 24 |
import SHA256 |
| 25 |
|
| 26 |
|
| 27 |
//////////////////////////////////////////////////////////////// |
| 28 |
// Hashing |
| 29 |
//////////////////////////////////////////////////////////////// |
| 30 |
|
| 31 |
// s2n_hash.h |
| 32 |
/* |
| 33 |
typedef enum { S2N_HASH_NONE, S2N_HASH_MD5, S2N_HASH_SHA1, S2N_HASH_SHA224, S2N_HASH_SHA256, S2N_HASH_SHA384, |
| 34 |
S2N_HASH_SHA512, S2N_HASH_MD5_SHA1 |
| 35 |
} s2n_hash_algorithm; |
| 36 |
*/ |
| 37 |
S2N_HASH_NONE = 0:[32] |
| 38 |
S2N_HASH_MD5 = 1:[32] |
| 39 |
S2N_HASH_SHA1 = 2:[32] |
| 40 |
S2N_HASH_SHA224 = 3:[32] |
| 41 |
S2N_HASH_SHA256 = 4:[32] |
| 42 |
S2N_HASH_SHA384 = 5:[32] |
| 43 |
S2N_HASH_SHA512 = 6:[32] |
| 44 |
S2N_HASH_MD5_SHA1 = 7:[32] |
| 45 |
|
| 46 |
// /usr/include/openssl/sha.h |
| 47 |
/* |
| 48 |
134 typedef struct SHA256state_st { |
| 49 |
135 SHA_LONG h[8]; |
| 50 |
136 SHA_LONG Nl, Nh; |
| 51 |
137 SHA_LONG data[SHA_LBLOCK]; |
| 52 |
138 unsigned int num, md_len; |
| 53 |
139 } SHA256_CTX; |
| 54 |
*/ |
| 55 |
// Looking at the generated LLVM in ':/src/hmac.ll' gives the precise |
| 56 |
// layout, without having to find the definitions of the above |
| 57 |
// macros. |
| 58 |
/* |
| 59 |
%struct.SHA512state_st = type { [8 x i64], i64, i64, %union.anon.0, i32, i32 } |
| 60 |
%union.anon.0 = type { [16 x i64] } |
| 61 |
*/ |
| 62 |
|
| 63 |
// The hash state in s2n is stored in a union, and the largest member |
| 64 |
// of that union is the SHA512 hash state. So, we need to translate |
| 65 |
// between SHA512 and SHA256 hash states. |
| 66 |
// |
| 67 |
// /usr/include/openssl/sha.h |
| 68 |
/* |
| 69 |
183 typedef struct SHA512state_st { |
| 70 |
184 SHA_LONG64 h[8]; |
| 71 |
185 SHA_LONG64 Nl, Nh; |
| 72 |
186 union { |
| 73 |
187 SHA_LONG64 d[SHA_LBLOCK]; |
| 74 |
188 unsigned char p[SHA512_CBLOCK]; |
| 75 |
189 } u; |
| 76 |
190 unsigned int num, md_len; |
| 77 |
191 } SHA512_CTX; |
| 78 |
*/ |
| 79 |
// :/src/hmac.ll |
| 80 |
/* |
| 81 |
%struct.SHA256state_st = type { [8 x i32], i32, i32, [16 x i32], i32, i32 } |
| 82 |
*/ |
| 83 |
|
| 84 |
type SHA512_c_state = |
| 85 |
{ h : [8][64] |
| 86 |
, Nl : [64] |
| 87 |
, Nh : [64] |
| 88 |
, u : [16][64] |
| 89 |
, num : [32] |
| 90 |
, md_len : [32] |
| 91 |
} |
| 92 |
type SHA512_c_bits = 8*64+64+64+16*64+32+32 |
| 93 |
|
| 94 |
join512_c_state : SHA512_c_state -> [SHA512_c_bits] |
| 95 |
join512_c_state st = join st.h # st.Nl # st.Nh # join st.u # st.num # st.md_len |
| 96 |
|
| 97 |
type SHA256_c_state = |
| 98 |
{ h : [8][32] |
| 99 |
, Nl : [32] // The low bits of 'sz'. |
| 100 |
, Nh : [32] // The high bits of 'sz'. |
| 101 |
, u : [16][32] // The 'block': '[16][32] == [8][64]' when flattened. |
| 102 |
, num : [32] // The 'n', but extended to 32 bits. |
| 103 |
, md_len : [32] // The value of 'md_len' is always 32, |
| 104 |
} // i.e. 'SHA256_DIGEST_LENGTH'. |
| 105 |
type SHA256_c_bits = 8*32+32+32+16*32+32+32 |
| 106 |
|
| 107 |
join256_c_state : SHA256_c_state -> [SHA256_c_bits] |
| 108 |
join256_c_state st = join st.h # st.Nl # st.Nh # join st.u # st.num # st.md_len |
| 109 |
|
| 110 |
type SHA256_DIGEST_LENGTH = 32 |
| 111 |
type SHA512_DIGEST_LENGTH = 64 |
| 112 |
|
| 113 |
|
| 114 |
// Recall the 'SHA256State' in our Cryptol model in './SHA256.cry': |
| 115 |
/* |
| 116 |
type SHA256State = { h : [8][32] |
| 117 |
, block : [64][8] |
| 118 |
, n : [16] |
| 119 |
, sz : [64] |
| 120 |
} |
| 121 |
*/ |
| 122 |
|
| 123 |
//////////////////////////////////////////////////////////////// |
| 124 |
// Basic hash-state translations. |
| 125 |
|
| 126 |
// The following code describes how to marshall back and forth between a |
| 127 |
// SHA256 C state and a SHA512 C state. Doing this is unnecessary for the |
| 128 |
// HMAC verification, but it is necessary to concretely evaluate the specs. |
| 129 |
// The rest of this section, therefore, does not need to be trusted. |
| 130 |
|
| 131 |
// I'm using the initial, high order bits of the SHA512 state to |
| 132 |
// construct the SHA256 state. The LLVM code doesn't make it clear |
| 133 |
// that this is correct -- it depends on the semantics of 'bitcast' -- |
| 134 |
// but I did an experiment which validates this choice; see |
| 135 |
// 'examples/llvm/union' in the SAWScript repo. |
| 136 |
sha512_c_state_to_sha256_c_state : SHA512_c_state -> SHA256_c_state |
| 137 |
sha512_c_state_to_sha256_c_state st = |
| 138 |
{ h = split (take bits) |
| 139 |
, Nl = take (drop`{front=8*32} bits) |
| 140 |
, Nh = take (drop`{front=8*32+32} bits) |
| 141 |
, u = split (take (drop`{front=8*32+32+32} bits)) |
| 142 |
, num = take (drop`{front=8*32+32+32+16*32} bits) |
| 143 |
, md_len = drop`{front=8*32+32+32+16*32+32} bits |
| 144 |
} |
| 145 |
where |
| 146 |
bits : [SHA256_c_bits] |
| 147 |
bits = take bits0 |
| 148 |
bits0 : [SHA512_c_bits] |
| 149 |
bits0 = join512_c_state st |
| 150 |
|
| 151 |
// We need the original state, 'st0', to compute the trailing SHA512 |
| 152 |
// bits which aren't used in the SHA256 state. |
| 153 |
sha256_c_state_to_sha512_c_state : SHA512_c_state -> SHA256_c_state -> SHA512_c_state |
| 154 |
sha256_c_state_to_sha512_c_state st0 st = |
| 155 |
{ h = split (take bits) |
| 156 |
, Nl = take (drop`{front=8*64} bits) |
| 157 |
, Nh = take (drop`{front=8*64+64} bits) |
| 158 |
, u = split (take (drop`{front=8*64+64+64} bits)) |
| 159 |
, num = take (drop`{front=8*64+64+64+16*64} bits) |
| 160 |
, md_len = drop`{front=8*64+64+64+16*64+32} bits |
| 161 |
} |
| 162 |
where |
| 163 |
bits : [SHA512_c_bits] |
| 164 |
bits = join256_c_state st # drop bits0 |
| 165 |
bits0 : [SHA512_c_bits] |
| 166 |
bits0 = join512_c_state st0 |
| 167 |
|
| 168 |
sha256_c_state_to_sha256_state : SHA256_c_state -> SHA256State |
| 169 |
sha256_c_state_to_sha256_state st = |
| 170 |
{ h = st.h |
| 171 |
, block = split (join st.u) |
| 172 |
, n = drop st.num |
| 173 |
, sz = st.Nh # st.Nl |
| 174 |
} |
| 175 |
where |
| 176 |
bits : [SHA256_c_bits] |
| 177 |
bits = join256_c_state st |
| 178 |
|
| 179 |
sha256_state_to_sha256_c_state : SHA256State -> SHA256_c_state |
| 180 |
sha256_state_to_sha256_c_state st = |
| 181 |
{ h = st.h |
| 182 |
, Nl = Nl |
| 183 |
, Nh = Nh |
| 184 |
, u = split (join st.block) |
| 185 |
, num = (zero # st.n) : [32] |
| 186 |
, md_len = `SHA256_DIGEST_LENGTH : [32] |
| 187 |
} |
| 188 |
where |
| 189 |
[Nh, Nl] = split st.sz |
| 190 |
|
| 191 |
//////////////////////////////////////////////////////////////// |
| 192 |
// Composed hash-state translations. |
| 193 |
|
| 194 |
sha512_c_state_to_sha256_state : SHA512_c_state -> SHA256State |
| 195 |
sha512_c_state_to_sha256_state st = |
| 196 |
sha256_c_state_to_sha256_state (sha512_c_state_to_sha256_c_state st) |
| 197 |
|
| 198 |
sha256_state_to_sha512_c_state : SHA512_c_state -> SHA256State -> SHA512_c_state |
| 199 |
sha256_state_to_sha512_c_state st0 st = |
| 200 |
sha256_c_state_to_sha512_c_state st0 (sha256_state_to_sha256_c_state st) |
| 201 |
|
| 202 |
//////////////////////////////////////////////////////////////// |
| 203 |
// SHA256 specs in terms of the SHA512 C hash state. |
| 204 |
|
| 205 |
sha256_init_sha512_c_state : SHA512_c_state -> SHA512_c_state |
| 206 |
sha256_init_sha512_c_state st0_c_512 = st1_c_512 |
| 207 |
where |
| 208 |
st0_256 = SHA256Init |
| 209 |
st1_c_512 = sha256_state_to_sha512_c_state st0_c_512 st0_256 |
| 210 |
|
| 211 |
sha256_update_sha512_c_state : {n} (fin n) => SHA512_c_state -> [n][8] -> SHA512_c_state |
| 212 |
sha256_update_sha512_c_state st0_c_512 in = st1_c_512 |
| 213 |
where |
| 214 |
st0_256 = sha512_c_state_to_sha256_state st0_c_512 |
| 215 |
st1_256 = SHA256Update st0_256 in |
| 216 |
st1_c_512 = sha256_state_to_sha512_c_state st0_c_512 st1_256 |
| 217 |
|
| 218 |
// We don't return a new 'SHA512_c_state', since it's unspecified (I |
| 219 |
// think ...). |
| 220 |
sha256_digest_sha512_c_state : SHA512_c_state -> [SHA256_DIGEST_LENGTH][8] |
| 221 |
sha256_digest_sha512_c_state st0_c_512 = out1 |
| 222 |
where |
| 223 |
st0_256 = sha512_c_state_to_sha256_state st0_c_512 |
| 224 |
out1 = split (SHA256Final st0_256) |