-
Notifications
You must be signed in to change notification settings - Fork 18
/
HMAC-array.saw
336 lines (265 loc) · 12.9 KB
/
HMAC-array.saw
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
enable_experimental;
// Make a symbolic hmac_state_st
let fresh_hmac_state_st_array name = do {
i_ctx <- fresh_sha512_state_st_array_n32 (str_concat name ".i_ctx");
o_ctx <- fresh_sha512_state_st (str_concat name ".o_ctx") 0;
md_ctx <- fresh_sha512_state_st_array_n32 (str_concat name ".md_ctx");
return {{ { i_ctx = i_ctx, o_ctx = o_ctx, md_ctx = md_ctx } }};
};
let fresh_hmac_state_st_array_uninitialized name = do {
i_ctx <- fresh_sha512_state_st_array_n32_uninitialized (str_concat name ".i_ctx");
o_ctx <- fresh_sha512_state_st_uninitialized (str_concat name ".o_ctx");
md_ctx <- fresh_sha512_state_st_array_n32_uninitialized (str_concat name ".md_ctx");
return {{ { i_ctx = i_ctx, o_ctx = o_ctx, md_ctx = md_ctx } }};
};
// The points-to for i_ctx, o_ctx and md_ctx inside a hmac_ctx_st
let points_to_hmac_ctx_st_sha_ctx_array ptr context = do {
let i_ctx_ptr = llvm_cast_pointer (llvm_field ptr "i_ctx") (llvm_alias "struct.sha512_state_st");
let o_ctx_ptr = llvm_cast_pointer (llvm_field ptr "o_ctx") (llvm_alias "struct.sha512_state_st");
let md_ctx_ptr = llvm_cast_pointer (llvm_field ptr "md_ctx") (llvm_alias "struct.sha512_state_st");
points_to_sha512_state_st_array_n32 i_ctx_ptr {{ context.i_ctx }};
points_to_sha512_state_st o_ctx_ptr {{ context.o_ctx }} 0;
points_to_sha512_state_st_array_n32 md_ctx_ptr {{ context.md_ctx }};
};
let points_to_hmac_ctx_st_sha_ctx_array_uninitialized ptr context = do {
let i_ctx_ptr = llvm_cast_pointer (llvm_field ptr "i_ctx") (llvm_alias "struct.sha512_state_st");
let o_ctx_ptr = llvm_cast_pointer (llvm_field ptr "o_ctx") (llvm_alias "struct.sha512_state_st");
let md_ctx_ptr = llvm_cast_pointer (llvm_field ptr "md_ctx") (llvm_alias "struct.sha512_state_st");
points_to_sha512_state_st_array_n32_uninitialized i_ctx_ptr {{ context.i_ctx }};
points_to_sha512_state_st_uninitialized o_ctx_ptr {{ context.o_ctx }} 0;
points_to_sha512_state_st_array_n32_uninitialized md_ctx_ptr {{ context.md_ctx }};
};
// The points-to for hmac_ctx_st
let points_to_hmac_ctx_st_array ptr context = do {
points_to_hmac_ctx_st_sha_ctx_array ptr context;
crucible_points_to (crucible_field ptr "md") (crucible_global (md_storage HMAC_MD_INDEX));
crucible_alloc_global HMAC_METHODS_STORAGE;
points_to_AWSLC_hmac_in_place_methods (crucible_global (md_storage HMAC_MD_INDEX));
crucible_points_to (crucible_field ptr "methods") global_md_methods;
};
let points_to_hmac_ctx_st_array_uninitialized ptr context = do {
points_to_hmac_ctx_st_sha_ctx_array_uninitialized ptr context;
zeroed_hmac_ctx_st ptr;
};
// hmac_ctx has been initialized and state is either 1 or 2
let hmac_ctx_has_been_initialized ptr = do {
state <- points_to_hmac_ctx_state ptr;
crucible_postcond {{state == 1 \/ state == 2}};
};
// hmac_ctx is ready for use but still need HMAC_Init_ex called
let hmac_ctx_ready_precond ptr = do {
state <- points_to_hmac_ctx_state ptr;
crucible_precond {{state == 3}};
};
let hmac_ctx_ready_postcond ptr = do {
state <- points_to_hmac_ctx_state ptr;
crucible_postcond {{state == 3}};
};
// The invariable that internal buffer offsets n are less than SHA512_CBLOCK and
// the internal buffer offset for o_ctx equal 0
let hmac_ctx_n_inv ctx = {{
ctx.i_ctx.n < `SHA512_CBLOCK /\
ctx.o_ctx.n == 0 /\
ctx.md_ctx.n < `SHA512_CBLOCK
}};
let hmac_ctx_n_precond ctx = crucible_precond (hmac_ctx_n_inv ctx);
let hmac_ctx_n_postcond ctx = crucible_postcond (hmac_ctx_n_inv ctx);
let hmac_precond ptr ctx = do {
points_to_env_md_st (crucible_global (md_storage HMAC_MD_INDEX));
hmac_ctx_is_initialized ptr;
hmac_ctx_n_precond ctx;
};
let hmac_postcond ptr ctx = do {
points_to_env_md_st (crucible_global (md_storage HMAC_MD_INDEX));
hmac_ctx_has_been_initialized ptr;
hmac_ctx_n_postcond ctx;
};
let hmac_ready_precond ptr ctx = do {
points_to_env_md_st (crucible_global (md_storage HMAC_MD_INDEX));
hmac_ctx_ready_precond ptr;
hmac_ctx_n_precond ctx;
};
let hmac_ready_postcond ptr ctx = do {
points_to_env_md_st (crucible_global (md_storage HMAC_MD_INDEX));
hmac_ctx_ready_postcond ptr;
hmac_ctx_n_postcond ctx;
};
// Specification for HMAC_CTX_init
let HMAC_CTX_init_array_spec = do {
hmac_ctx_ptr <- crucible_alloc (llvm_struct "struct.hmac_ctx_st");
crucible_execute_func [hmac_ctx_ptr];
hmac_ctx <- fresh_hmac_state_st_array_uninitialized "hmac_ctx";
points_to_hmac_ctx_st_array_uninitialized hmac_ctx_ptr hmac_ctx;
};
// Specification for HMAC_Init_ex_array
let HMAC_Init_ex_arrayRangeEq_i_ctx = "HMAC_Init_ex.arrayRangeEq.i_ctx";
let HMAC_Init_ex_arrayRangeEq_md_ctx = "HMAC_Init_ex.arrayRangeEq.md_ctx";
let HMAC_Init_ex_array_spec = do {
global_alloc_init "OPENSSL_ia32cap_P" {{ ia32cap }};
alloc_md_globals;
alloc_hmac_globals;
points_to_env_md_st (crucible_global (md_storage HMAC_MD_INDEX));
hmac_ctx_ptr <- llvm_alloc (llvm_struct "struct.hmac_ctx_st");
hmac_ctx <- fresh_hmac_state_st_array_uninitialized "hmac_ctx";
points_to_hmac_ctx_st_array_uninitialized hmac_ctx_ptr hmac_ctx;
key_len <- crucible_fresh_var "key_len" i64;
(key, key_ptr) <- ptr_to_fresh_array_readonly "key" key_len;
// The ENGINE input is not used in the body of function HMAC_Init_ex
// (The assertion is optimized away by LLVM).
// Due to LLVM optimizations, the ENGINE input argument is optimized to be `undef`.
// When conducting an override with HMAC_Init_ex, `crucible_null` will not match
// with `undef`. Making engine_ptr a crucible_fresh_var allows the override to succeed.
engine_ptr <- crucible_fresh_var "engine_ptr" i64;
crucible_execute_func
[ hmac_ctx_ptr
, key_ptr
, (crucible_term key_len)
, (crucible_global (md_storage HMAC_MD_INDEX))
, (crucible_term engine_ptr)
];
global_points_to "OPENSSL_ia32cap_P" {{ ia32cap }};
let res = {{ HMACInit_Array hmac_ctx key key_len }};
// There exists a ByteArray called res_i_ctx_block, satisfying the constraint that
// its first 128 bytes is equal to the first 128 bytes of the result res.i_ctx.block returned from the spec,
// such that the result of the C function HMAC_Init_ex points to it.
res_i_ctx_block <- crucible_fresh_cryptol_var "res_i_ctx_block" {| ByteArray |};
res_md_ctx_block <- crucible_fresh_cryptol_var "res_md_ctx_block" {| ByteArray |};
points_to_hmac_ctx_st_array
hmac_ctx_ptr
{{ { i_ctx = { h = res.i_ctx.h, block = res_i_ctx_block, n = res.i_ctx.n, sz = res.i_ctx.sz }
, o_ctx = res.o_ctx
, md_ctx = { h = res.md_ctx.h, block = res_md_ctx_block, n = res.md_ctx.n, sz = res.md_ctx.sz } } }};
// llvm_setup_with_tag tags this postcondition with the name "HMAC_Init_ex.arrayRangeEq.i_ctx"
// so that the proof script can refer to this subgoal
llvm_setup_with_tag HMAC_Init_ex_arrayRangeEq_i_ctx
(crucible_postcond {{ arrayRangeEq res_i_ctx_block 0 res.i_ctx.block 0 `SHA512_CBLOCK }});
llvm_setup_with_tag HMAC_Init_ex_arrayRangeEq_md_ctx
(crucible_postcond {{ arrayRangeEq res_md_ctx_block 0 res.md_ctx.block 0 `SHA512_CBLOCK }});
hmac_postcond hmac_ctx_ptr res;
crucible_return (crucible_term {{ 1 : [32] }});
};
let HMAC_Init_ex_null_spec = do {
global_alloc_init "OPENSSL_ia32cap_P" {{ ia32cap }};
alloc_md_globals;
hmac_ctx_ptr <- llvm_alloc (llvm_struct "struct.hmac_ctx_st");
hmac_ctx <- fresh_hmac_state_st_array "hmac_ctx";
points_to_hmac_ctx_st_array hmac_ctx_ptr hmac_ctx;
hmac_ready_precond hmac_ctx_ptr hmac_ctx;
// The ENGINE input is not used in the body of function HMAC_Init_ex
// (The assertion is optimized away by LLVM).
// Due to LLVM optimizations, the ENGINE input argument is optimized to be `undef`.
// When conducting an override with HMAC_Init_ex, `crucible_null` will not match
// with `undef`. Making engine_ptr a crucible_fresh_var allows the override to succeed.
engine_ptr <- crucible_fresh_var "engine_ptr" i64;
crucible_execute_func
[ hmac_ctx_ptr
, crucible_null
, (crucible_term {{ 0 : [64] }})
, crucible_null
, (crucible_term engine_ptr)
];
global_points_to "OPENSSL_ia32cap_P" {{ ia32cap }};
points_to_hmac_ctx_st_array hmac_ctx_ptr hmac_ctx;
hmac_postcond hmac_ctx_ptr hmac_ctx;
crucible_return (crucible_term {{ 1 : [32] }});
};
// Specification for HMAC_Update_Array
let HMAC_Update_array_spec = do {
global_alloc_init "OPENSSL_ia32cap_P" {{ ia32cap }};
alloc_md_globals;
hmac_ctx_ptr <- llvm_alloc (llvm_struct "struct.hmac_ctx_st");
hmac_ctx <- fresh_hmac_state_st_array "hmac_ctx";
points_to_hmac_ctx_st_array hmac_ctx_ptr hmac_ctx;
hmac_precond hmac_ctx_ptr hmac_ctx;
len <- crucible_fresh_var "len" i64;
(data, data_ptr) <- ptr_to_fresh_array_readonly "data" len;
crucible_execute_func [ hmac_ctx_ptr , data_ptr , (crucible_term len) ];
global_points_to "OPENSSL_ia32cap_P" {{ ia32cap }};
let res = {{ HMACUpdate_Array hmac_ctx data len }};
res_md_ctx_block <- crucible_fresh_cryptol_var "res_md_ctx_block" {| ByteArray |};
points_to_hmac_ctx_st_array
hmac_ctx_ptr
{{ { i_ctx = res.i_ctx
, o_ctx = res.o_ctx
, md_ctx = { h = res.md_ctx.h, block = res_md_ctx_block, n = res.md_ctx.n, sz = res.md_ctx.sz } } }};
// There exists a ByteArray called res_i_ctx_block, satisfying the constraint that
// its first 128 bytes is equal to the first 128 bytes of the result res.md_ctx.block returned from the spec,
// such that the result of the C function HMAC_Update points to it
crucible_postcond {{ arrayRangeEq res_md_ctx_block 0 res.md_ctx.block 0 `SHA512_CBLOCK }};
hmac_postcond hmac_ctx_ptr res;
crucible_return (crucible_term {{ 1 : [32] }});
};
// Specification for HMAC_Final_Array
let HMAC_Final_array_spec withLength = do {
global_alloc_init "OPENSSL_ia32cap_P" {{ ia32cap }};
alloc_md_globals;
hmac_ctx_ptr <- llvm_alloc (llvm_struct "struct.hmac_ctx_st");
hmac_ctx <- fresh_hmac_state_st_array "hmac_ctx";
points_to_hmac_ctx_st_array hmac_ctx_ptr hmac_ctx;
hmac_precond hmac_ctx_ptr hmac_ctx;
(md_out_ptr, s_ptr) <- digestOut_pre withLength;
crucible_execute_func [ hmac_ctx_ptr , md_out_ptr, s_ptr ];
global_points_to "OPENSSL_ia32cap_P" {{ ia32cap }};
points_to_env_md_st (crucible_global (md_storage HMAC_MD_INDEX));
digestOut_post withLength md_out_ptr s_ptr (crucible_term {{ HMACFinal_Array hmac_ctx }});
crucible_return (crucible_term {{ 1 : [32] }});
};
// Specification for HMAC_Final_Array with postcond on hmac_ctx
let HMAC_Final_array_with_hmac_ctx_spec withLength = do {
global_alloc_init "OPENSSL_ia32cap_P" {{ ia32cap }};
alloc_md_globals;
hmac_ctx_ptr <- llvm_alloc (llvm_struct "struct.hmac_ctx_st");
hmac_ctx <- fresh_hmac_state_st_array "hmac_ctx";
points_to_hmac_ctx_st_array hmac_ctx_ptr hmac_ctx;
hmac_precond hmac_ctx_ptr hmac_ctx;
(md_out_ptr, s_ptr) <- digestOut_pre withLength;
crucible_execute_func [ hmac_ctx_ptr , md_out_ptr, s_ptr ];
global_points_to "OPENSSL_ia32cap_P" {{ ia32cap }};
points_to_env_md_st (crucible_global (md_storage HMAC_MD_INDEX));
let res = {{ { i_ctx = hmac_ctx.i_ctx , o_ctx = hmac_ctx.o_ctx , md_ctx = hmac_ctx.i_ctx } }};
res_md_ctx_block <- crucible_fresh_cryptol_var "res_md_ctx_block" {| ByteArray |};
points_to_hmac_ctx_st_array
hmac_ctx_ptr
{{ { i_ctx = res.i_ctx
, o_ctx = res.o_ctx
, md_ctx = { h = res.md_ctx.h, block = res_md_ctx_block, n = res.md_ctx.n, sz = res.md_ctx.sz } } }};
// We say the hmac_ctx.md_ctx.block points to some block res_md_ctx_block.
// Since the content in block doesn't matter, this postcondition is enough for
// satisfying the preconditions of the next HMAC_Init_ex call.
hmac_ready_postcond hmac_ctx_ptr res;
digestOut_post withLength md_out_ptr s_ptr (crucible_term {{ HMACFinal_Array hmac_ctx }});
crucible_return (crucible_term {{ 1 : [32] }});
};
// Specification for HMAC_array
let HMAC_md_out = "HMAC.md_out";
let HMAC_array_spec withLength = do {
global_alloc_init "OPENSSL_ia32cap_P" {{ ia32cap }};
alloc_md_globals;
alloc_hmac_globals;
points_to_env_md_st (crucible_global (md_storage HMAC_MD_INDEX));
key_len <- crucible_fresh_var "key_len" i64;
(key, key_ptr) <- ptr_to_fresh_array_readonly "key" key_len;
data_len <- crucible_fresh_var "data_len" i64;
(data, data_ptr) <- ptr_to_fresh_array_readonly "data" data_len;
(md_out_ptr, md_out_len_ptr) <- digestOut_pre withLength;
crucible_execute_func
[ (crucible_global (md_storage HMAC_MD_INDEX))
, key_ptr
, (crucible_term key_len)
, data_ptr
, (crucible_term data_len)
, md_out_ptr
, md_out_len_ptr
];
global_points_to "OPENSSL_ia32cap_P" {{ ia32cap }};
points_to_env_md_st (crucible_global (md_storage HMAC_MD_INDEX));
// llvm_setup_with_tag tags this postcondition with the name "HMAC.md_out"
// so that the proof script can refer to this subgoal
llvm_setup_with_tag HMAC_md_out
(digestOut_post withLength md_out_ptr md_out_len_ptr (crucible_term {{ HMAC_Array (HMACState_zeroized) key key_len data data_len }}));
crucible_return md_out_ptr;
};