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
use fmt;
use ;
/// An owned byte vector that zeroizes its allocation when dropped.
///
/// The whole allocation is overwritten, spare capacity included. A `SecretVec` never grows: a
/// buffer that does (`Vec::extend`, `push`) moves to a new allocation and leaves the old one
/// behind unerased, so build key material in a buffer sized up front and wrap it here.
;
/// How much stack [`scrub_stack`] overwrites: 32 KiB in an optimised build, 128 KiB in an
/// unoptimised one (`debug_assertions`, which Cargo's `dev` and `test` profiles turn on with
/// opt-level 0).
///
/// The depth has to cover the stack the work before it used. Building an SRTP context uses
/// about 3 KiB in an optimised build and 42 to 47 KiB in an unoptimised one, where every
/// temporary has a stack slot of its own (`rtci-srtp/tests/key_residue.rs`, measured with a
/// 32 KiB overwrite): the AES key schedules built for the key derivation are the deepest
/// part, below frames such as
/// `common::block_encrypt`'s, which holds a cipher object of every AES key length. With rustc
/// 1.99.1 (x86-64 Linux) the master key and its round keys were left 37.7 to 42.2 KB below the
/// point the measurement starts from, more than 32 KiB below the context's `on_erased_stack`
/// frame (which lies less than 4.9 KB below that point), out of a 32 KiB overwrite's reach.
/// 128 KiB is the deepest unoptimised use measured (47 KiB) times 2.7, for other compilers and
/// profiles. The optimised depth is unchanged, and so is an optimised build's cost.
const SCRUB_STACK_BYTES: usize = if cfg! else ;
/// Overwrites the stack below the caller's frame (32 KiB of it in an optimised build, 128 KiB in
/// an unoptimised one).
///
/// Building a keyed object leaves copies of the key and its schedule in the stack frames of
/// the functions that built it: a value moved out of a frame is not dropped there, so its
/// zeroize-on-drop never runs on the copy left behind, and a provider's own temporaries are
/// not erased at all. Those dead frames are reused by later calls, and a value later built
/// there and moved into the heap (into a `Box`, a `Vec`) carries whatever was in its padding
/// or in the unused part of an enum or union along: the key ends up in memory the heap frees
/// without erasure. Calling this right after a key schedule is built and moved to where it
/// lives, from the frame that called the provider, overwrites what the deeper frames left.
///
/// What it does: one bulk zero fill (`ptr::write_bytes`, a `memset`) of a buffer of that size
/// in its own frame, which is never inlined, so the buffer lies in the stack just below the
/// caller's frame, where the frames of the calls the caller made before lay. The buffer is
/// then handed to [`std::hint::black_box`] and followed by a compiler fence, so the compiler
/// cannot treat the fill as a dead store and drop it. The calling thread needs that much stack
/// free below the caller.
///
/// What it does not do: it cannot reach registers, the caller's own frame or any frame above
/// it, other threads' stacks, or anything deeper than that size below the caller (a build
/// profile with opt-level 0 and `debug-assertions = false` gets the optimised depth); and
/// copies the compiler or a provider leaves that no test can see are not observed, so their
/// absence is not guaranteed. The tests show only that copies carried from dead frames into heap padding
/// are gone (`tests/dtls_secret_erasure.rs`; docs/key-erasure.md).
/// How much stack [`scrub_record_stack`] overwrites: 48 KiB, in an unoptimised build only.
///
/// The deepest record-key form measured in an unoptimised build lay 31,288 bytes below the
/// DTLS key residue measurement's floor (rustc 1.89, the AES-CCM records step,
/// `rtci-dtls/tests/key_residue.rs`, run 46015302538). Every record call's own frame lies below
/// that floor, so what the record work leaves lies less than that below the call. 48 KiB is
/// that depth times 1.5, for other compilers and call paths; it is paid in unoptimised builds
/// only.
const SCRUB_RECORD_STACK_BYTES: usize = 48 * 1024;
/// Overwrites the stack below the caller's frame after one record's work with RustCrypto's AES,
/// in an unoptimised build (`debug_assertions`); in an optimised build it is not compiled.
///
/// At opt-level 0 the AES-NI intrinsics of the `aes` crate are calls whose vector arguments go
/// through stack copies, so every block encrypted or decrypted leaves round keys (round key 0
/// is the key) and AES states in stack temporaries; an optimised build keeps them in registers.
/// Measured: on Windows a DTLS 1.2 AES-CCM write key left there was carried into a freed heap
/// block by the unwritten bytes of a handshake message moved into a map (docs/key-erasure.md);
/// with the `aes` and `ccm` crates optimised, with the portable AES, in an optimised build, or
/// with an overwrite after each AES-CCM call, it was not (runs 46026969169, 46026978042).
pub