Debian Codegraph @

crt/s2n/tests/saw/HMAC/spec/HMAC.saw

raw
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 import "HMAC_iterative.cry";
23 import "Hashing.cry";
24
25 ////////////////////////////////////////////////////////////////
26 // Generic Utilities.
27 //
28
29 let alloc_init ty v = do {
30 p <- crucible_alloc ty;
31 crucible_points_to p v;
32 return p;
33 };
34
35 let alloc_init_readonly ty v = do {
36 p <- crucible_alloc_readonly ty;
37 crucible_points_to p (crucible_term v);
38 return p;
39 };
40
41 let ptr_to_fresh n ty = do {
42 x <- crucible_fresh_var n ty;
43 p <- alloc_init ty (crucible_term x);
44 return (x, p);
45 };
46
47 let ptr_to_fresh_readonly n ty = do {
48 x <- crucible_fresh_var n ty;
49 p <- alloc_init_readonly ty x;
50 return (x, p);
51 };
52
53 let z3_hash_unint =
54 w4_unint_z3 [ "hash_init_c_state"
55 , "hash_update_c_state"
56 , "hash_update_c_state_unbounded"
57 , "hash_digest_c_state"
58 ];
59
60 ////////////////////////////////////////////////////////////////
61 // Hash.
62 //
63
64 let setup_hash_state pstate = do {
65 alg0 <- crucible_fresh_var "alg" (llvm_int 32);
66 h0 <- crucible_fresh_var "h" (llvm_array 8 (llvm_int 64));
67 Nl0 <- crucible_fresh_var "Nl" (llvm_int 64);
68 Nh0 <- crucible_fresh_var "Nh" (llvm_int 64);
69 u0 <- crucible_fresh_var "u" (llvm_array 16 (llvm_int 64));
70 num0 <- crucible_fresh_var "num" (llvm_int 32);
71 is_ready_for_input0 <- crucible_fresh_var "is_ready_for_input" (llvm_int 8);
72 currently_in_hash0 <- crucible_fresh_var "currently_in_hash" (llvm_int 64);
73 md_len0 <- crucible_fresh_var "md_len" (llvm_int 32);
74 (_, pimpl) <- ptr_to_fresh_readonly "impl" (llvm_struct "struct.s2n_hash");
75 crucible_points_to pstate
76 (crucible_struct
77 [ pimpl
78 , crucible_term alg0
79 , crucible_term is_ready_for_input0
80 , crucible_term currently_in_hash0
81 , crucible_struct
82 [ crucible_struct
83 [ crucible_struct
84 [ crucible_term h0
85 , crucible_term Nl0
86 , crucible_term Nh0
87 , crucible_struct [ crucible_term u0 ]
88 , crucible_term num0
89 , crucible_term md_len0
90 ]
91 ]
92 ]
93 ]);
94 let st = {{
95 { h = h0
96 , Nl = Nl0
97 , Nh = Nh0
98 , u = u0
99 , num = num0
100 , md_len = md_len0
101 }
102 }};
103 return (st, currently_in_hash0);
104 };
105
106 let update_hash_state pstate st = do {
107 alg <- crucible_fresh_var "alg" (llvm_int 32);
108 is_ready_for_input <- crucible_fresh_var "is_ready_for_input" (llvm_int 8);
109 currently_in_hash <- crucible_fresh_var "currently_in_hash" (llvm_int 64);
110 (_, pimpl) <- ptr_to_fresh_readonly "impl" (llvm_struct "struct.s2n_hash");
111
112 crucible_points_to pstate
113 (crucible_struct
114 [ pimpl
115 , crucible_term alg
116 , crucible_term is_ready_for_input
117 , crucible_term currently_in_hash
118 , crucible_struct
119 [ crucible_struct
120 [ crucible_struct
121 [ crucible_term {{ st.h }}
122 , crucible_term {{ st.Nl }}
123 , crucible_term {{ st.Nh }}
124 , crucible_struct [ crucible_term {{ st.u }} ]
125 , crucible_term {{ st.num }}
126 , crucible_term {{ st.md_len }}
127 ]
128 ]
129 ]
130 ]);
131 };
132
133 let hash_init_spec = do {
134 pstate <- crucible_alloc (llvm_struct "struct.s2n_hash_state");
135 (st0, _) <- setup_hash_state pstate;
136 alg <- crucible_fresh_var "alg" (llvm_int 32);
137 crucible_execute_func [pstate, crucible_term alg];
138 // We need to pass in the starting state since many of the bits in
139 // the union are unused by many of the hash algorithms.
140 let st1 = {{ hash_init_c_state st0 }};
141 update_hash_state pstate st1;
142 crucible_return (crucible_term {{ 0 : [32] }});
143 };
144
145 let hash_reset_spec = do {
146 pstate <- crucible_alloc (llvm_struct "struct.s2n_hash_state");
147 (st0, _) <- setup_hash_state pstate;
148 crucible_execute_func [pstate];
149 let st1 = {{ hash_init_c_state st0 }};
150 update_hash_state pstate st1;
151 crucible_return (crucible_term {{ 0 : [32] }});
152 };
153
154 let hash_copy_spec = do {
155 pstate1 <- crucible_alloc (llvm_struct "struct.s2n_hash_state");
156 pstate2 <- crucible_alloc (llvm_struct "struct.s2n_hash_state");
157 (st1, _) <- setup_hash_state pstate1;
158 (st2, _) <- setup_hash_state pstate2;
159 crucible_execute_func [pstate1, pstate2];
160 update_hash_state pstate1 st2;
161 update_hash_state pstate2 st2;
162 crucible_return (crucible_term {{ 0 : [32] }});
163 };
164
165 let hash_update_spec msg_size = do {
166 pstate <- crucible_alloc (llvm_struct "struct.s2n_hash_state");
167 (msg, pmsg) <- ptr_to_fresh_readonly "msg" (llvm_array msg_size (llvm_int 8));
168 (st0, _) <- setup_hash_state pstate;
169 let size = crucible_term {{ `msg_size : [32] }};
170 crucible_execute_func [pstate, pmsg, size];
171 let st1 = {{ hash_update_c_state`{msg_size=msg_size} st0 msg }};
172 update_hash_state pstate st1;
173 crucible_return (crucible_term {{ 0 : [32] }});
174 };
175
176 let hash_update_unbounded_spec = do {
177 pstate <- crucible_alloc (llvm_struct "struct.s2n_hash_state");
178 (st0, _) <- setup_hash_state pstate;
179
180 size <- crucible_fresh_var "size" (llvm_int 32);
181 pmsg <- crucible_symbolic_alloc true 1 {{ (0 # size) : [64] }};
182 msg <- crucible_fresh_cryptol_var "msg" {| ByteArray |};
183 crucible_points_to_array_prefix pmsg msg {{ (0 # size) : [64] }};
184
185 crucible_execute_func [pstate, pmsg, (crucible_term size)];
186
187 let st1 = {{ hash_update_c_state_unbounded st0 msg size }};
188 update_hash_state pstate st1;
189
190 crucible_return (crucible_term {{ 0 : [32] }});
191 };
192
193 let hash_digest_spec digest_size = do {
194 pstate <- crucible_alloc (llvm_struct "struct.s2n_hash_state");
195 (dgst, pdgst) <- ptr_to_fresh "out" (llvm_array digest_size (llvm_int 8));
196 (st0, _) <- setup_hash_state pstate;
197 size <- crucible_fresh_var "size" (llvm_int 32);
198 crucible_execute_func [pstate, pdgst, crucible_term size];
199 update_hash_state pstate st0;
200 let out1 = {{ hash_digest_c_state`{digest_size=digest_size} st0 }};
201 crucible_points_to pdgst (crucible_term out1);
202 crucible_return (crucible_term {{ 0 : [32] }});
203 };
204
205 let hash_get_currently_in_hash_total_spec = do {
206 pstate <- crucible_alloc (llvm_struct "struct.s2n_hash_state");
207 pout <- crucible_alloc (llvm_int 64);
208 (st0, currently_in_hash) <- setup_hash_state pstate;
209 crucible_execute_func [pstate, pout];
210 update_hash_state pstate st0;
211 crucible_points_to pout (crucible_term {{zero: [64]}} );
212 crucible_return (crucible_term {{ 0 : [32] }});
213 };
214
215 ////////////////////////////////////////////////////////////////
216 // HMAC.
217
218 let setup_hmac_state alg0 hash_block_size0 block_size0 digest_size0 = do {
219 pstate <- crucible_alloc (llvm_struct "struct.s2n_hmac_state");
220 currently_in_hash_block0 <- crucible_fresh_var "currently_in_hash_block" (llvm_int 32);
221 xor_pad0 <- crucible_fresh_var "xor_pad" (llvm_array 128 (llvm_int 8));
222 let digest_size = eval_size {| SHA512_DIGEST_LENGTH |};
223 digest_pad0 <- crucible_fresh_var "digest_pad" (llvm_array digest_size (llvm_int 8));
224
225 crucible_points_to (crucible_field pstate "alg") (crucible_term alg0);
226 crucible_points_to (crucible_field pstate "hash_block_size") (crucible_term hash_block_size0);
227 crucible_points_to (crucible_field pstate "currently_in_hash_block") (crucible_term currently_in_hash_block0);
228 crucible_points_to (crucible_field pstate "xor_pad_size") (crucible_term block_size0);
229 crucible_points_to (crucible_field pstate "digest_size") (crucible_term digest_size0);
230 (inner0, _) <- setup_hash_state (crucible_field pstate "inner");
231 (inner_just_key0, _) <- setup_hash_state (crucible_field pstate "inner_just_key");
232 (outer_just_key0, _) <- setup_hash_state (crucible_field pstate "outer_just_key");
233 (outer0, _) <- setup_hash_state (crucible_field pstate "outer");
234 crucible_points_to (crucible_field pstate "xor_pad") (crucible_term xor_pad0);
235 crucible_points_to (crucible_field pstate "digest_pad") (crucible_term digest_pad0);
236
237 let st0 = {{
238 { alg = alg0
239 , hash_block_size = hash_block_size0
240 , currently_in_hash_block = currently_in_hash_block0
241 , block_size = block_size0
242 , digest_size = digest_size0
243 , inner = inner0
244 , inner_just_key = inner_just_key0
245 , outer = outer0
246 , outer_just_key = outer_just_key0
247 , xor_pad = xor_pad0
248 , digest_pad = digest_pad0
249 }
250 }};
251 return (pstate, st0);
252 };
253
254 let check_hmac_state pstate st = do {
255 crucible_points_to (crucible_field pstate "alg") (crucible_term {{ st.alg }});
256 crucible_points_to (crucible_field pstate "hash_block_size") (crucible_term {{ st.hash_block_size }});
257 crucible_points_to (crucible_field pstate "currently_in_hash_block") (crucible_term {{ st.currently_in_hash_block }});
258 crucible_points_to (crucible_field pstate "xor_pad_size") (crucible_term {{ st.block_size }});
259 crucible_points_to (crucible_field pstate "digest_size") (crucible_term {{ st.digest_size }});
260 update_hash_state (crucible_field pstate "inner") {{ st.inner }};
261 update_hash_state (crucible_field pstate "inner_just_key") {{ st.inner_just_key }};
262
263 // XXX: Don't care about 'outer' because it gets overwritten by
264 // 's2n_hash_reset' before use in 's2n_hmac_digest'.
265 //
266 //update_hash_state (crucible_elem pstate 7) {{ st.outer }};
267 update_hash_state (crucible_field pstate "outer_just_key") ({{ st.outer_just_key }});
268 crucible_points_to (crucible_field pstate "xor_pad") (crucible_term {{ st.xor_pad }});
269
270 // Don't care about 'digest_pad', because it gets overwritten
271 // using 's2n_hash_digest' before use in 's2n_hmac_digest'.
272 //
273 // However, if we leave it in, the proof still goes through
274 // (since we model exactly what happens).
275 //
276 //crucible_points_to (crucible_elem pstate 9) (crucible_term {{ st.digest_pad }});
277 };
278
279 let hmac_invariants
280 st
281 (cfg : { name : String
282 , hmac_alg : Term
283 , digest_size : Int
284 , block_size : Int
285 , hash_block_size : Int
286 }) = do {
287 // Specify the HMAC algorithm.
288 crucible_equal (crucible_term {{ st.alg }}) (crucible_term cfg.hmac_alg);
289
290 // Specify sizes
291 let hash_block_size = cfg.hash_block_size;
292 let block_size = cfg.block_size;
293 let digest_size = cfg.digest_size;
294 crucible_equal (crucible_term {{ st.hash_block_size }}) (crucible_term {{ `hash_block_size : [16] }});
295 crucible_equal (crucible_term {{ st.block_size }}) (crucible_term {{ `block_size : [16] }});
296 crucible_equal (crucible_term {{ st.digest_size }}) (crucible_term {{ `digest_size : [8] }});
297 };
298
299 ////////////////////////////////////////////////////////////////
300
301 let hmac_init_spec
302 (cfg : { name : String
303 , hmac_alg : Term
304 , digest_size : Int
305 , block_size : Int
306 , hash_block_size : Int
307 }) = do {
308 alg0 <- crucible_fresh_var "alg" (llvm_int 32);
309 hash_block_size0 <- crucible_fresh_var "hash_block_size" (llvm_int 16);
310 block_size0 <- crucible_fresh_var "block_size" (llvm_int 16);
311 digest_size0 <- crucible_fresh_var "digest_size" (llvm_int 8);
312 (pstate, st0) <- setup_hmac_state alg0 hash_block_size0 block_size0 digest_size0;
313
314 klen <- crucible_fresh_var "klen" (llvm_int 32);
315 pkey <- crucible_symbolic_alloc true 1 {{ (0 # klen) : [64] }};
316 key <- crucible_fresh_cryptol_var "key" {| ByteArray |};
317 crucible_points_to_array_prefix pkey key {{ (0 # klen) : [64] }};
318
319 crucible_execute_func [pstate, crucible_term (cfg.hmac_alg), pkey, (crucible_term klen)];
320
321 let block_size = cfg.block_size;
322 let hash_block_size = cfg.hash_block_size;
323 let digest_size = cfg.digest_size;
324 let alg0 = cfg.hmac_alg;
325
326 let st1 = {{
327 hmac_init_c_state_unbounded
328 `{block_size=block_size
329 ,hash_block_size=hash_block_size
330 ,digest_size=digest_size}
331 st0 alg0 key klen
332 }};
333 check_hmac_state pstate st1;
334 hmac_invariants st1 cfg;
335 crucible_return (crucible_term {{ 0 : [32] }});
336 };
337
338 let hmac_update_spec
339 (cfg : { name : String
340 , hmac_alg : Term
341 , digest_size : Int
342 , block_size : Int
343 , hash_block_size : Int
344 }) = do {
345 let digest_size = cfg.digest_size;
346 let block_size = cfg.block_size;
347 let hash_block_size = cfg.hash_block_size;
348 (pstate, st0) <- setup_hmac_state
349 cfg.hmac_alg
350 {{ `hash_block_size : [16] }}
351 {{ `block_size : [16] }}
352 {{ `digest_size : [8] }};
353
354 hmac_invariants st0 cfg;
355
356 size <- crucible_fresh_var "size" (llvm_int 32);
357 let uint32_max = {{~0 : [32]}};
358 let highest_32_bit = {{4294949760 : [32]}};
359 crucible_precond {{ size <= uint32_max - highest_32_bit }};
360 crucible_precond {{ ((highest_32_bit + size) % (0 # st0.hash_block_size)) <= uint32_max - st0.currently_in_hash_block }};
361
362 pmsg <- crucible_symbolic_alloc true 1 {{ (0 # size) : [64] }};
363 msg <- crucible_fresh_cryptol_var "msg" {| ByteArray |};
364 crucible_points_to_array_prefix pmsg msg {{ (0 # size) : [64] }};
365
366 crucible_execute_func [pstate, pmsg, (crucible_term size)];
367
368 let st1 = {{ hmac_update_c_state_unbounded st0 msg size }};
369 check_hmac_state pstate st1;
370 hmac_invariants st1 cfg;
371
372 crucible_return (crucible_term {{ 0 : [32] }});
373 };
374
375 let hmac_digest_spec
376 (cfg : { name : String
377 , hmac_alg : Term
378 , digest_size : Int
379 , block_size : Int
380 , hash_block_size : Int
381 }) = do {
382 (out, pout) <- ptr_to_fresh "out" (llvm_array cfg.digest_size (llvm_int 8));
383
384 let digest_size = cfg.digest_size;
385 let block_size = cfg.block_size;
386 let hash_block_size = cfg.hash_block_size;
387 (pstate, st0) <- setup_hmac_state
388 cfg.hmac_alg
389 {{ `hash_block_size : [16] }}
390 {{ `block_size : [16] }}
391 {{ `digest_size : [8] }};
392
393 hmac_invariants st0 cfg;
394
395 let hash_block_size = cfg.hash_block_size;
396 let block_size = cfg.block_size;
397 let digest_size = cfg.digest_size;
398 let size = {{ `digest_size : [32] }};
399 crucible_execute_func [pstate, pout, crucible_term size];
400 let st1_digest = {{
401 hmac_digest_c_state`{block_size=block_size,digest_size=digest_size} st0
402 }};
403 let st1 = {{ st1_digest.0 }};
404 let digest = {{ split (st1_digest.1) : [digest_size][8] }};
405
406 crucible_points_to pout (crucible_term digest);
407 crucible_return (crucible_term {{ 0 : [32] }});
408 };
409
410 let hmac_digest_size_spec
411 (cfg : { name : String
412 , hmac_alg : Term
413 , digest_size : Int
414 , block_size : Int
415 , hash_block_size : Int
416 }) = do {
417 psize <- crucible_alloc (llvm_int 8);
418 crucible_execute_func [crucible_term cfg.hmac_alg, psize];
419 let digest_size = cfg.digest_size;
420 crucible_points_to psize (crucible_term {{ `digest_size : [8] }});
421 crucible_return (crucible_term {{ 0 : [32] }});
422 };