| 1 |
/* |
| 2 |
* Copyright (c) 2013-2016 Galois, Inc. |
| 3 |
* Distributed under the terms of the BSD3 license (see LICENSE file) |
| 4 |
* |
| 5 |
* @tmd - 24 April 2015 - took Ian's SHA512, converted to SHA256 |
| 6 |
* @ian - 15 August 2015 - he lies, probably ment 2014. |
| 7 |
* |
| 8 |
* This is a very simple implementation of SHA256, designed to be as clearly |
| 9 |
* mathced to the specification in NIST's FIPS-PUB-180-4 as possible |
| 10 |
* |
| 11 |
* * The output correctly matches on all test vectors from |
| 12 |
* http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/SHA256.pdf |
| 13 |
*/ |
| 14 |
module SHA256 where |
| 15 |
|
| 16 |
/* |
| 17 |
* SHA256 Functions : Section 4.1.2 |
| 18 |
*/ |
| 19 |
|
| 20 |
Ch : [32] -> [32] -> [32] -> [32] |
| 21 |
Ch x y z = (x && y) ^ (~x && z) |
| 22 |
|
| 23 |
Maj : [32] -> [32] -> [32] -> [32] |
| 24 |
Maj x y z = (x && y) ^ (x && z) ^ (y && z) |
| 25 |
|
| 26 |
S0 : [32] -> [32] |
| 27 |
S0 x = (x >>> 2) ^ (x >>> 13) ^ (x >>> 22) |
| 28 |
|
| 29 |
S1 : [32] -> [32] |
| 30 |
S1 x = (x >>> 6) ^ (x >>> 11) ^ (x >>> 25) |
| 31 |
|
| 32 |
s0 : [32] -> [32] |
| 33 |
s0 x = (x >>> 7) ^ (x >>> 18) ^ (x >> 3) |
| 34 |
|
| 35 |
s1 : [32] -> [32] |
| 36 |
s1 x = (x >>> 17) ^ (x >>> 19) ^ (x >> 10) |
| 37 |
|
| 38 |
/* |
| 39 |
* SHA256 Constants : Section 4.2.2 |
| 40 |
*/ |
| 41 |
|
| 42 |
K : [64][32] |
| 43 |
K = [ 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, |
| 44 |
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, |
| 45 |
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, |
| 46 |
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, |
| 47 |
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, |
| 48 |
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, |
| 49 |
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, |
| 50 |
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2 |
| 51 |
] |
| 52 |
|
| 53 |
/* |
| 54 |
* Preprocessing (padding and parsing) for SHA256 : Section 5.1.1 and 5.2.1 |
| 55 |
*/ |
| 56 |
preprocess : {msgLen,contentLen,chunks,padding} |
| 57 |
( fin msgLen |
| 58 |
, 64 >= width msgLen // message width fits in a word |
| 59 |
, contentLen == msgLen + 65 // message + header |
| 60 |
, chunks == (contentLen+511) / 512 |
| 61 |
, padding == (512 - contentLen % 512) % 512 // prettier if type #'s could be < 0 |
| 62 |
) |
| 63 |
=> [msgLen] -> [chunks][512] |
| 64 |
preprocess msg = split (msg # [True] # (zero:[padding]) # (`msgLen:[64])) |
| 65 |
|
| 66 |
/* |
| 67 |
* SHA256 Initial Hash Value : Section 5.3.3 |
| 68 |
*/ |
| 69 |
|
| 70 |
H0 : [8][32] |
| 71 |
H0 = [ 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, |
| 72 |
0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19] |
| 73 |
|
| 74 |
/* |
| 75 |
* The SHA256 Hash computation : Section 6.2.2 |
| 76 |
* |
| 77 |
* We have split the computation into a message scheduling function, corresponding |
| 78 |
* to step 1 in the documents loop, and a compression function, corresponding to steps 2-4. |
| 79 |
*/ |
| 80 |
|
| 81 |
SHA256MessageSchedule : [16][32] -> [64][32] |
| 82 |
SHA256MessageSchedule M = W where |
| 83 |
W = M # [ s1 (W@(j-2)) + (W@(j-7)) + s0 (W@(j-15)) + (W@(j-16)) | j <- [16 .. 63]:[_][8] ] |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
SHA256Compress : [8][32] -> [64][32] -> [8][32] |
| 88 |
SHA256Compress H W = [as!0 + H@0, bs!0 + H@1, cs!0 + H@2, ds!0 + H@3, es!0 + H@4, fs!0 + H@5, gs!0 + H@6, hs!0 + H@7] where |
| 89 |
T1 = [h + S1 e + Ch e f g + k + w | h <- hs | e <- es | f <- fs | g <- gs | k <- K | w <- W] |
| 90 |
T2 = [S0 a + Maj a b c | a <- as | b <- bs | c <- cs] |
| 91 |
hs = take `{65} ([H@7] # gs) |
| 92 |
gs = take `{65} ([H@6] # fs) |
| 93 |
fs = take `{65} ([H@5] # es) |
| 94 |
es = take `{65} ([H@4] # [d + t1 | d <- ds | t1 <- T1]) |
| 95 |
ds = take `{65} ([H@3] # cs) |
| 96 |
cs = take `{65} ([H@2] # bs) |
| 97 |
bs = take `{65} ([H@1] # as) |
| 98 |
as = take `{65} ([H@0] # [t1 + t2 | t1 <- T1 | t2 <- T2]) |
| 99 |
|
| 100 |
SHA256Block : [8][32] -> [16][32] -> [8][32] |
| 101 |
SHA256Block H M = SHA256Compress H (SHA256MessageSchedule M) |
| 102 |
|
| 103 |
//////// Functional/idiomatic top level //////// |
| 104 |
|
| 105 |
/* |
| 106 |
* The SHA256' function hashes a preprocessed sequence of blocks with the |
| 107 |
* compression function. The SHA256 function hashes a sequence of bytes, and |
| 108 |
* is more likely the function that will be similar to those seein in an |
| 109 |
* implementation to be verified. |
| 110 |
*/ |
| 111 |
|
| 112 |
SHA256' : {a} (fin a) => [a][16][32] -> [8][32] |
| 113 |
SHA256' blocks = hash!0 where |
| 114 |
hash = [H0] # [SHA256Block h b | h <- hash | b <- blocks] |
| 115 |
|
| 116 |
SHA256 : {a} (fin a, 64 >= width (8*a)) => [a][8] -> [256] |
| 117 |
SHA256 msg = join (SHA256' [ split x | x <- preprocess(join msg)]) |
| 118 |
|
| 119 |
property katsPass = ~zero == [test == kat | (test,kat) <- kats ] |
| 120 |
|
| 121 |
kats = [ (SHA256 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" |
| 122 |
, 0x248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1) |
| 123 |
, (SHA256 "" |
| 124 |
,0xe3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855) |
| 125 |
, (SHA256 "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu" |
| 126 |
, 0xcf5b16a778af8380036ce59e7b0492370b249b11e8f07a51afac45037afee9d1) |
| 127 |
// , ([0x61 | i <- [1..1000000] : [_][32]] |
| 128 |
// , 0xcdc76e5c9914fb9281a1c7e284d73e67f1809a48a497200e046d39ccc7112cd0) |
| 129 |
] |
| 130 |
|
| 131 |
//////// Imperative top level //////// |
| 132 |
|
| 133 |
type SHA256State = { h : [8][32] |
| 134 |
, block : [64][8] |
| 135 |
, n : [16] |
| 136 |
, sz : [64] |
| 137 |
} |
| 138 |
|
| 139 |
SHA256Init : SHA256State |
| 140 |
SHA256Init = { h = H0 |
| 141 |
, block = zero |
| 142 |
, n = 0 |
| 143 |
, sz = 0 |
| 144 |
} |
| 145 |
|
| 146 |
SHA256Update1 : SHA256State -> [8] -> SHA256State |
| 147 |
SHA256Update1 s b = |
| 148 |
if s.n == 64 |
| 149 |
then { h = SHA256Block s.h (split (join s.block)) |
| 150 |
, block = [b] # zero |
| 151 |
, n = 1 |
| 152 |
, sz = s.sz + 8 |
| 153 |
} |
| 154 |
else { h = s.h |
| 155 |
, block = update s.block s.n b |
| 156 |
, n = s.n + 1 |
| 157 |
, sz = s.sz + 8 |
| 158 |
} |
| 159 |
|
| 160 |
SHA256Update : {n} (fin n) => SHA256State -> [n][8] -> SHA256State |
| 161 |
SHA256Update sinit bs = ss!0 |
| 162 |
where ss = [sinit] # [ SHA256Update1 s b | s <- ss | b <- bs ] |
| 163 |
|
| 164 |
// Add padding and size and process the final block. |
| 165 |
SHA256Final : SHA256State -> [256] |
| 166 |
SHA256Final s = join (SHA256Block h b') |
| 167 |
// Because the message is always made up of bytes, and the size is a |
| 168 |
// fixed number of bytes, the 1 pad will always be at least a byte. |
| 169 |
where s' = SHA256Update1 s 0x80 |
| 170 |
// Don't need to add zeros. They're already there. Just update |
| 171 |
// the count of bytes in this block. After adding the 1 pad, there |
| 172 |
// are two possible cases: the size will fit in the current block, |
| 173 |
// or it won't. |
| 174 |
(h, b) = if s'.n <= 56 then (s'.h, s'.block) |
| 175 |
else (SHA256Block s'.h (split (join s'.block)), zero) |
| 176 |
b' = split (join b || (zero # s.sz)) |
| 177 |
|
| 178 |
SHA256Imp : {a} (64 >= width (8*a)) => [a][8] -> [256] |
| 179 |
SHA256Imp msg = SHA256Final (SHA256Update SHA256Init msg) |
| 180 |
|
| 181 |
property katsPassImp = ~zero == [test == kat | (test,kat) <- katsImp ] |
| 182 |
|
| 183 |
katsImp = [ (SHA256Imp "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" |
| 184 |
, 0x248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1) |
| 185 |
, (SHA256Imp "" |
| 186 |
, 0xe3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855) |
| 187 |
, (SHA256Imp "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu" |
| 188 |
, 0xcf5b16a778af8380036ce59e7b0492370b249b11e8f07a51afac45037afee9d1) |
| 189 |
// , ([0x61 | i <- [1..1000000] : [_][32]] |
| 190 |
// , 0xcdc76e5c9914fb9281a1c7e284d73e67f1809a48a497200e046d39ccc7112cd0) |
| 191 |
] |
| 192 |
|
| 193 |
property imp_correct msg = SHA256 msg == SHA256Imp msg |