Commit 92c81e7
authored
hashcrypt: minimize panic code paths (#528)
This pull request enhances error handling in the `hashcrypt` hasher
implementation by introducing a unified `Error` type and updating
relevant methods to return `Result` instead of panicking. This makes the
code more robust, especially for invalid input scenarios, and improves
its safety and reliability in both blocking and asynchronous hashing
modes.
**Error handling improvements:**
* Introduced a new `Error` enum in `src/hashcrypt/mod.rs` to represent
unsupported configurations and updated code to use this error type
instead of panicking.
* Updated internal helper methods in `Hasher` (such as
`init_final_data`, `init_final_block`, and `init_final_len`) to return
`Result<(), Error>` and propagate errors instead of panicking on invalid
buffer access.
**Blocking hasher API changes:**
* Changed public and internal methods in the blocking `Hasher`
implementation (`submit_blocks`, `finalize`, `hash`) to return `Result`
and handle errors gracefully, replacing panics with error returns for
invalid input lengths.
**Async hasher API changes:**
* Updated async `Hasher` methods (`transfer`, `submit_blocks`,
`finalize`, `hash`) to return `Result` and propagate errors, ensuring
safe handling of invalid conditions such as missing DMA channels or
incorrect data lengths.
[[1]](diffhunk://#diff-2b7655c2223a12761dadfbbd782cba8cf016f31e8b298aa46e9519bd81ff2158L130-R150)
[[2]](diffhunk://#diff-2b7655c2223a12761dadfbbd782cba8cf016f31e8b298aa46e9519bd81ff2158L141-R159)
[[3]](diffhunk://#diff-2b7655c2223a12761dadfbbd782cba8cf016f31e8b298aa46e9519bd81ff2158R191-R232)
**General code safety improvements:**
* Added assertions and explicit error returns to ensure buffer sizes and
data lengths are always valid, replacing unchecked unwraps and panics
with error handling.
[[1]](diffhunk://#diff-2b7655c2223a12761dadfbbd782cba8cf016f31e8b298aa46e9519bd81ff2158R78-R138)
[[2]](diffhunk://#diff-2b7655c2223a12761dadfbbd782cba8cf016f31e8b298aa46e9519bd81ff2158L37-R56)1 parent 644bf24 commit 92c81e7
File tree
4 files changed
+94
-56
lines changed- examples/rt685s-evk/src/bin
- src/hashcrypt
4 files changed
+94
-56
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
22 | 22 | | |
23 | 23 | | |
24 | 24 | | |
25 | | - | |
| 25 | + | |
26 | 26 | | |
27 | 27 | | |
28 | 28 | | |
| |||
39 | 39 | | |
40 | 40 | | |
41 | 41 | | |
42 | | - | |
| 42 | + | |
| 43 | + | |
43 | 44 | | |
44 | 45 | | |
45 | 46 | | |
| |||
56 | 57 | | |
57 | 58 | | |
58 | 59 | | |
59 | | - | |
| 60 | + | |
| 61 | + | |
60 | 62 | | |
61 | 63 | | |
62 | 64 | | |
| |||
70 | 72 | | |
71 | 73 | | |
72 | 74 | | |
73 | | - | |
| 75 | + | |
74 | 76 | | |
75 | 77 | | |
76 | 78 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
20 | | - | |
| 20 | + | |
21 | 21 | | |
22 | 22 | | |
23 | 23 | | |
| |||
28 | 28 | | |
29 | 29 | | |
30 | 30 | | |
31 | | - | |
32 | | - | |
33 | | - | |
34 | | - | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
35 | 38 | | |
36 | 39 | | |
37 | 40 | | |
| |||
42 | 45 | | |
43 | 46 | | |
44 | 47 | | |
45 | | - | |
46 | | - | |
47 | | - | |
48 | | - | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
49 | 55 | | |
50 | 56 | | |
51 | 57 | | |
| |||
59 | 65 | | |
60 | 66 | | |
61 | 67 | | |
62 | | - | |
| 68 | + | |
63 | 69 | | |
64 | 70 | | |
65 | 71 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
8 | | - | |
| 8 | + | |
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
| |||
34 | 34 | | |
35 | 35 | | |
36 | 36 | | |
37 | | - | |
38 | | - | |
39 | | - | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
40 | 44 | | |
41 | 45 | | |
42 | | - | |
43 | | - | |
44 | | - | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
45 | 49 | | |
46 | 50 | | |
47 | | - | |
48 | | - | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
49 | 57 | | |
50 | 58 | | |
51 | 59 | | |
| |||
67 | 75 | | |
68 | 76 | | |
69 | 77 | | |
| 78 | + | |
| 79 | + | |
70 | 80 | | |
71 | | - | |
72 | | - | |
73 | | - | |
74 | | - | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
75 | 86 | | |
76 | 87 | | |
77 | 88 | | |
78 | 89 | | |
79 | 90 | | |
80 | | - | |
| 91 | + | |
81 | 92 | | |
82 | | - | |
| 93 | + | |
83 | 94 | | |
84 | 95 | | |
85 | 96 | | |
| 97 | + | |
86 | 98 | | |
87 | 99 | | |
88 | 100 | | |
| 101 | + | |
89 | 102 | | |
90 | 103 | | |
91 | 104 | | |
92 | | - | |
| 105 | + | |
93 | 106 | | |
94 | 107 | | |
95 | 108 | | |
96 | 109 | | |
97 | 110 | | |
98 | | - | |
| 111 | + | |
99 | 112 | | |
100 | 113 | | |
101 | 114 | | |
102 | | - | |
| 115 | + | |
103 | 116 | | |
104 | 117 | | |
105 | 118 | | |
106 | | - | |
| 119 | + | |
107 | 120 | | |
108 | 121 | | |
109 | 122 | | |
110 | 123 | | |
| 124 | + | |
| 125 | + | |
111 | 126 | | |
112 | 127 | | |
113 | 128 | | |
114 | | - | |
115 | | - | |
| 129 | + | |
| 130 | + | |
116 | 131 | | |
117 | | - | |
118 | | - | |
| 132 | + | |
| 133 | + | |
119 | 134 | | |
120 | | - | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
121 | 139 | | |
122 | 140 | | |
123 | 141 | | |
| |||
127 | 145 | | |
128 | 146 | | |
129 | 147 | | |
130 | | - | |
| 148 | + | |
131 | 149 | | |
132 | | - | |
| 150 | + | |
133 | 151 | | |
134 | 152 | | |
135 | 153 | | |
| |||
138 | 156 | | |
139 | 157 | | |
140 | 158 | | |
141 | | - | |
| 159 | + | |
142 | 160 | | |
143 | 161 | | |
144 | 162 | | |
| |||
170 | 188 | | |
171 | 189 | | |
172 | 190 | | |
| 191 | + | |
| 192 | + | |
173 | 193 | | |
174 | 194 | | |
175 | 195 | | |
176 | | - | |
177 | | - | |
| 196 | + | |
| 197 | + | |
178 | 198 | | |
| 199 | + | |
179 | 200 | | |
180 | 201 | | |
181 | 202 | | |
182 | | - | |
| 203 | + | |
183 | 204 | | |
184 | 205 | | |
185 | 206 | | |
186 | 207 | | |
187 | 208 | | |
188 | | - | |
189 | | - | |
| 209 | + | |
| 210 | + | |
190 | 211 | | |
191 | 212 | | |
192 | | - | |
193 | | - | |
194 | | - | |
| 213 | + | |
| 214 | + | |
195 | 215 | | |
196 | | - | |
197 | | - | |
| 216 | + | |
| 217 | + | |
198 | 218 | | |
199 | 219 | | |
200 | 220 | | |
| 221 | + | |
201 | 222 | | |
202 | 223 | | |
203 | 224 | | |
204 | | - | |
205 | | - | |
| 225 | + | |
| 226 | + | |
206 | 227 | | |
207 | | - | |
208 | | - | |
| 228 | + | |
| 229 | + | |
209 | 230 | | |
210 | | - | |
| 231 | + | |
| 232 | + | |
211 | 233 | | |
212 | 234 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
15 | 23 | | |
16 | 24 | | |
17 | 25 | | |
| |||
0 commit comments