-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathBlake2b.cry
174 lines (141 loc) · 5.6 KB
/
Blake2b.cry
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
Copyright (c) 2015-2018, Galois Inc.
www.cryptol.net
Author: Thomas M. DuBuisson
*/
module Primitive::Keyless::Hash::Blake2b where
type Block = [16][64]
type State = [8][64]
type LocalState = [16][64]
type Counter = [128]
type Context = { state : State, counter : Counter }
// Block size
bbVal : [128]
bbVal = 128
IV : [8][64]
IV = [ 0x6A09E667F3BCC908, 0xBB67AE8584CAA73B,
0x3C6EF372FE94F82B, 0xA54FF53A5F1D36F1,
0x510E527FADE682D1, 0x9B05688C2B3E6C1F,
0x1F83D9ABFB41BD6B, 0x5BE0CD19137E2179
]
SIGMA_TABLE : [12][16][4]
SIGMA_TABLE =
[ [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 ]
, [ 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 ]
, [ 11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4 ]
, [ 7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8 ]
, [ 9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13 ]
, [ 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9 ]
, [ 12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11 ]
, [ 13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10 ]
, [ 6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5 ]
, [ 10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13, 0 ]
, [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 ]
, [ 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 ]
]
// Section 2.1 constants
R1,R2,R3,R4 : [6]
R1 = 32
R2 = 24
R3 = 16
R4 = 63
// Section 3.1: Mixing function 'G'
G : [4] -> [4] -> LocalState -> [4][6] -> Block -> [16][64]
G r i vec abcd ms = updates vec abcd new
where
new = G' r i (vec @@ abcd) ms
G' : [4] -> [4] -> [4][64] -> Block -> [4][64]
G' r i vals ms = [a2, b2, c2, d2]
where
x = ms @ (SIGMA_TABLE @ r @ (2*i))
y = ms @ (SIGMA_TABLE @ r @ (2*i+1))
[a,b,c,d] = vals
a1 = a + b + x
d1 = (d ^ a1) >>> R1
c1 = c + d1
b1 = (b ^ c1) >>> R2
a2 = a1 + b1 + y
d2 = (d1 ^ a2) >>> R3
c2 = c1 + d2
b2 = (b1 ^ c2) >>> R4
// Section 3.2: Compression function
F : State -> Block -> Counter -> Bit -> State
F h ms t f = h ^ (take (vs!0)) ^ (drop (vs!0))
where
v = h # IV ^ (zero # [t0,t1,f0,zero])
[t1,t0] = split t : [2][64]
f0 = [f | _ <- [0..63] : [_][6]]
vs = [v] # [ round i ms v' | v' <- vs | i <- [0..11] ]
// Sub-function of F (first for loop)
round : [4] -> Block -> LocalState -> LocalState
round r ms v = vs ! 0
where
vs = [v] # [ G r i v' ix ms
| v' <- vs
| i <- [0..7]
| ix <- [ [0,4,8,12], [1,5,9,13]
, [2,6,10,14], [3,7,11,15]
, [0,5,10,15], [1,6,11,12]
, [2,7,8,13], [3,4,9,14] ]
]
// ix (ms @ (s @ sIx1)) (ms @ (s @ sIx2))
// | sIx1 <- [0,2,4,6,8,10,12,14] : [8][4]
// | sIx2 <- [1,3,5,7,9,11,13,15] : [8][4]
// ]
// Section 3.3: Padding
// Recall kk == key bytes (0)
// nn == hash bytes (64)
// ll == input bytes (ceiling $ len / 8)
// bb == block bytes (128, 1024 bits)
// dd == nr Blocks
blake2b : {ll,dd}
(fin ll, fin dd
, dd == (max 1 ((ll+127)/128)) - 1
// ^^^ ugly We'd like , dd == (ll-1)/128 but then we can't hash the empty string
, 128 >= width ll
)
=> [ll][8]
-> [512]
blake2b m = blake2Finish ({ state = (hs!0).state, counter = `ll}) lastBlock
where
fullBlocks : [dd]Block
fullBlocks = [mkBlock b | b <- split (take (join m))]
partialBlock = drop `{128*dd} m
lastBlock : Block
lastBlock =
if (`ll == (zero:[128]))
then zero // Special case full zero block for empty messages
else mkBlock ((split (join (partialBlock # (zero : [inf][8]))))@0)
h : Context
h = { state = [IV@0 ^ `0x01010040] # drop `{1} IV, counter = 0 }
hs : [dd+1]Context
hs = [h] # [blake2Update h' dX | h' <- hs | dX <- fullBlocks]
blake2Update : Context -> Block -> Context
blake2Update ctx d = { state = newState, counter = newCount }
where newState = F ctx.state d newCount False
newCount = ctx.counter + (128 : [128])
postprocess : {ll} (128 >= ll, 128 >= width ll) => Counter -> [ll][8] -> Block
postprocess c m =
if c == 0 /\ `ll == (zero:[128])
then zero
else split (join m # zero)
// The final round of blake
blake2Finish : Context -> Block -> [512]
// blake2Finish ctx b = ctx.state@0 # zero
blake2Finish ctx b = join [ reverseBytes w | w <- F ctx.state b ctx.counter True ]
////////////////////////////////////////////////////////////////////////////////
// Utilities
////////////////////////////////////////////////////////////////////////////////
mkBlock x = reverse (split (reverseBytes x))
reverseBytes x = join (reverse (bytes x))
bytes x = split x : [_] [8]
testZeros =
and [ blake2b (zero : [128][8]) == 0x865939e120e6805438478841afb739ae4250cf372653078a065cdcfffca4caf798e6d462b65d658fc165782640eded70963449ae1500fb0f24981d7727e22c41
, blake2b (zero : [0][8]) == 0x786A02F742015903C6C6FD852552D272912F4740E15847618A86E217F71F5419D25E1031AFEE585313896444934EB04B903A685B1448B755D56F701AFE9BE2CE ]
testZerosLarge =
and [ blake2b (zero : [129][8]) == 0xa60edba343e7a6933c14d203d2e535f35e6deb6c8a4f8e624c1a6f6e2612860447cb4c37e5aa11bcf03b7c3eea7228eb8b998f922794f2d1b8f2dc63f03bd3fa
]
testKATs =
and [ blake2b "The quick brown fox jumps over the lazy dog" == 0xA8ADD4BDDDFD93E4877D2746E62817B116364A1FA7BC148D95090BC7333B3673F82401CF7AA2E4CB1ECD90296E3F14CB5413F8ED77BE73045B13914CDCD6A918
]
property katsPass = and [ testZeros, testZerosLarge, testKATs ]