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
|
See also:
* https://git.adelielinux.org/adelie/packages/-/issues/1002
* https://github.com/Mbed-TLS/mbedtls/issues/7428
* https://github.com/Mbed-TLS/mbedtls/pull/7440
From 9dc8b6a6a201ccdbfcbf0de8f76b8b0ddfc2f85c Mon Sep 17 00:00:00 2001
From: Dave Rodgman <dave.rodgman@arm.com>
Date: Thu, 13 Apr 2023 12:53:35 +0100
Subject: [PATCH 1/6] Test fixes for big-endian
Signed-off-by: Dave Rodgman <dave.rodgman@arm.com>
---
tests/suites/test_suite_alignment.function | 58 ++++++++++++----------
1 file changed, 31 insertions(+), 27 deletions(-)
diff --git a/tests/suites/test_suite_alignment.function b/tests/suites/test_suite_alignment.function
index f6703318ce7..45080cc7b6a 100644
--- a/tests/suites/test_suite_alignment.function
+++ b/tests/suites/test_suite_alignment.function
@@ -17,6 +17,20 @@ int parse_hex_string(char *hex_string, uint64_t *result)
if (mbedtls_test_unhexify(raw, sizeof(raw), hex_string, &olen) != 0) {
return 0;
}
+
+ /* If < 8 bytes, shift right and pad with leading zeros for big-endian */
+ if (MBEDTLS_IS_BIG_ENDIAN) {
+ if (olen < 8) {
+ int offset = 8 - olen;
+ for (int i = olen - 1; i >= 0; i--) {
+ raw[i + offset] = raw[i];
+ }
+ for (int i = 0; i < offset; i++) {
+ raw[i] = 0;
+ }
+ }
+ }
+
*result = 0;
for (size_t i = 0; i < olen; i++) {
if (MBEDTLS_IS_BIG_ENDIAN) {
@@ -57,38 +71,28 @@ void mbedtls_unaligned_access(int size, int offset)
break;
}
- /* Generate expected result */
- uint64_t expected = 0;
- for (uint8_t i = 0; i < 8; i++) {
- uint8_t shift;
- if (MBEDTLS_IS_BIG_ENDIAN) {
- /*
- * Similar to little-endian case described below, but the shift needs
- * to be inverted
- */
- shift = 7 - (i * 8);
- } else {
- /* example for offset == 1:
- * expected = (( 1 + 0 ) << (0 * 8)) | (( 1 + 1 ) << (1 * 8)) | (( 1 + 2 ) << (2 * 8)))
- * = (1 << 0) | (2 << 8) | (3 << 16) ...
- * = 0x0807060504030201
- * x = { 0, 1, 2, 3, ... }
- * ie expected is the value that would be read from x on a LE system, when
- * byte swapping is not performed
- */
- shift = i * 8;
- }
- uint64_t b = offset + i;
- expected |= b << shift;
+ /* Define expected result by manually aligning the raw bytes, and
+ * reading back with a normal pointer access. */
+ uint64_t raw_aligned = 0;
+ uint8_t *e8 = (uint8_t *) &raw_aligned;
+ uint8_t *r8 = ((uint8_t *) &raw) + offset;
+ /* Make aligned copy */
+ for (int i = 0; i < size / 8; i++) {
+ e8[i] = r8[i];
}
-
- /* Mask out excess bits from expected result */
+ /* Make a 16/32/64 byte read from the aligned location, and copy to expected */
+ uint64_t expected = 0;
switch (size) {
case 16:
- expected &= 0xffff;
+ uint16_t *e16 = (uint16_t *) &raw_aligned;
+ expected = *e16;
break;
case 32:
- expected &= 0xffffffff;
+ uint32_t *e32 = (uint32_t *) &raw_aligned;
+ expected = *e32;
+ break;
+ case 64:
+ expected = raw_aligned;
break;
}
From 0a05e703dbc303fc5e1154e926bc96cb312ace89 Mon Sep 17 00:00:00 2001
From: Dave Rodgman <dave.rodgman@arm.com>
Date: Thu, 13 Apr 2023 13:01:34 +0100
Subject: [PATCH 2/6] Tidy-up
Signed-off-by: Dave Rodgman <dave.rodgman@arm.com>
---
tests/suites/test_suite_alignment.function | 29 ++++++++--------------
1 file changed, 11 insertions(+), 18 deletions(-)
diff --git a/tests/suites/test_suite_alignment.function b/tests/suites/test_suite_alignment.function
index 45080cc7b6a..cd4502005f0 100644
--- a/tests/suites/test_suite_alignment.function
+++ b/tests/suites/test_suite_alignment.function
@@ -19,15 +19,13 @@ int parse_hex_string(char *hex_string, uint64_t *result)
}
/* If < 8 bytes, shift right and pad with leading zeros for big-endian */
- if (MBEDTLS_IS_BIG_ENDIAN) {
- if (olen < 8) {
- int offset = 8 - olen;
- for (int i = olen - 1; i >= 0; i--) {
- raw[i + offset] = raw[i];
- }
- for (int i = 0; i < offset; i++) {
- raw[i] = 0;
- }
+ if (MBEDTLS_IS_BIG_ENDIAN && olen < 8) {
+ int offset = 8 - olen;
+ for (int i = olen - 1; i >= 0; i--) {
+ raw[i + offset] = raw[i];
+ }
+ for (int i = 0; i < offset; i++) {
+ raw[i] = 0;
}
}
@@ -73,13 +71,8 @@ void mbedtls_unaligned_access(int size, int offset)
/* Define expected result by manually aligning the raw bytes, and
* reading back with a normal pointer access. */
- uint64_t raw_aligned = 0;
- uint8_t *e8 = (uint8_t *) &raw_aligned;
- uint8_t *r8 = ((uint8_t *) &raw) + offset;
- /* Make aligned copy */
- for (int i = 0; i < size / 8; i++) {
- e8[i] = r8[i];
- }
+ uint64_t raw_aligned;
+ memcpy(&raw_aligned, ((uint8_t*)&raw) + offset, size / 8);
/* Make a 16/32/64 byte read from the aligned location, and copy to expected */
uint64_t expected = 0;
switch (size) {
@@ -98,7 +91,7 @@ void mbedtls_unaligned_access(int size, int offset)
TEST_EQUAL(r, expected);
- /* Write sentinel to the part of the array we will testing writing to */
+ /* Write sentinel to the part of the array we will test writing to */
for (size_t i = 0; i < (size_t) (size / 8); i++) {
x[i + offset] = 0xff;
}
@@ -319,7 +312,7 @@ void unaligned_access_endian_aware(int size, int offset, int big_endian)
/* Verify read */
TEST_EQUAL(read, expected);
- /* Test writing back to memory. First write sentiel */
+ /* Test writing back to memory. First write sentinel */
for (size_t i = 0; i < (size_t) (size / 8); i++) {
x[i + offset] = 0xff;
}
From df2d5b1ca1569f3a6e129e276756d9d15980f719 Mon Sep 17 00:00:00 2001
From: Dave Rodgman <dave.rodgman@arm.com>
Date: Thu, 13 Apr 2023 13:41:09 +0100
Subject: [PATCH 3/6] Fix compile error
Signed-off-by: Dave Rodgman <dave.rodgman@arm.com>
---
tests/suites/test_suite_alignment.function | 14 +++++++-------
1 file changed, 7 insertions(+), 7 deletions(-)
diff --git a/tests/suites/test_suite_alignment.function b/tests/suites/test_suite_alignment.function
index cd4502005f0..3a5038e98c2 100644
--- a/tests/suites/test_suite_alignment.function
+++ b/tests/suites/test_suite_alignment.function
@@ -71,21 +71,21 @@ void mbedtls_unaligned_access(int size, int offset)
/* Define expected result by manually aligning the raw bytes, and
* reading back with a normal pointer access. */
- uint64_t raw_aligned;
- memcpy(&raw_aligned, ((uint8_t*)&raw) + offset, size / 8);
+ uint64_t raw_aligned_64;
+ uint16_t *raw_aligned_16 = (uint16_t *) &raw_aligned_64;
+ uint32_t *raw_aligned_32 = (uint32_t *) &raw_aligned_64;
+ memcpy(&raw_aligned_64, ((uint8_t *) &raw) + offset, size / 8);
/* Make a 16/32/64 byte read from the aligned location, and copy to expected */
uint64_t expected = 0;
switch (size) {
case 16:
- uint16_t *e16 = (uint16_t *) &raw_aligned;
- expected = *e16;
+ expected = *raw_aligned_16;
break;
case 32:
- uint32_t *e32 = (uint32_t *) &raw_aligned;
- expected = *e32;
+ expected = *raw_aligned_32;
break;
case 64:
- expected = raw_aligned;
+ expected = raw_aligned_64;
break;
}
From b169671c50e7c1779763ac89d35af11dc0997477 Mon Sep 17 00:00:00 2001
From: Dave Rodgman <dave.rodgman@arm.com>
Date: Thu, 13 Apr 2023 13:46:46 +0100
Subject: [PATCH 4/6] Tidy-up
Signed-off-by: Dave Rodgman <dave.rodgman@arm.com>
---
tests/suites/test_suite_alignment.function | 9 ++-------
1 file changed, 2 insertions(+), 7 deletions(-)
diff --git a/tests/suites/test_suite_alignment.function b/tests/suites/test_suite_alignment.function
index 3a5038e98c2..b027c4b1165 100644
--- a/tests/suites/test_suite_alignment.function
+++ b/tests/suites/test_suite_alignment.function
@@ -20,13 +20,8 @@ int parse_hex_string(char *hex_string, uint64_t *result)
/* If < 8 bytes, shift right and pad with leading zeros for big-endian */
if (MBEDTLS_IS_BIG_ENDIAN && olen < 8) {
- int offset = 8 - olen;
- for (int i = olen - 1; i >= 0; i--) {
- raw[i + offset] = raw[i];
- }
- for (int i = 0; i < offset; i++) {
- raw[i] = 0;
- }
+ memmove(raw + 8 - olen, raw, olen);
+ memset(raw, 0, 8 - olen);
}
*result = 0;
From c07df36f9e402ef8b97beb92b25556b04c10c77e Mon Sep 17 00:00:00 2001
From: Dave Rodgman <dave.rodgman@arm.com>
Date: Thu, 13 Apr 2023 14:54:12 +0100
Subject: [PATCH 5/6] More fixes for big-endian
Signed-off-by: Dave Rodgman <dave.rodgman@arm.com>
---
tests/suites/test_suite_alignment.function | 12 +-----------
1 file changed, 1 insertion(+), 11 deletions(-)
diff --git a/tests/suites/test_suite_alignment.function b/tests/suites/test_suite_alignment.function
index b027c4b1165..717c51a6951 100644
--- a/tests/suites/test_suite_alignment.function
+++ b/tests/suites/test_suite_alignment.function
@@ -18,19 +18,9 @@ int parse_hex_string(char *hex_string, uint64_t *result)
return 0;
}
- /* If < 8 bytes, shift right and pad with leading zeros for big-endian */
- if (MBEDTLS_IS_BIG_ENDIAN && olen < 8) {
- memmove(raw + 8 - olen, raw, olen);
- memset(raw, 0, 8 - olen);
- }
-
*result = 0;
for (size_t i = 0; i < olen; i++) {
- if (MBEDTLS_IS_BIG_ENDIAN) {
- *result |= ((uint64_t) raw[i]) << (i * 8);
- } else {
- *result |= ((uint64_t) raw[i]) << ((olen - i - 1) * 8);
- }
+ *result |= ((uint64_t) raw[i]) << ((olen - i - 1) * 8);
}
return 1;
}
From 9145dc46ed98f0bc19e09d52486ba120c1f14589 Mon Sep 17 00:00:00 2001
From: Dave Rodgman <dave.rodgman@arm.com>
Date: Thu, 13 Apr 2023 14:59:03 +0100
Subject: [PATCH 6/6] Ensure variables initialised
Signed-off-by: Dave Rodgman <dave.rodgman@arm.com>
---
tests/suites/test_suite_alignment.function | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/tests/suites/test_suite_alignment.function b/tests/suites/test_suite_alignment.function
index 717c51a6951..ed8f918d8c3 100644
--- a/tests/suites/test_suite_alignment.function
+++ b/tests/suites/test_suite_alignment.function
@@ -12,7 +12,7 @@
*/
int parse_hex_string(char *hex_string, uint64_t *result)
{
- uint8_t raw[8];
+ uint8_t raw[8] = {0};
size_t olen;
if (mbedtls_test_unhexify(raw, sizeof(raw), hex_string, &olen) != 0) {
return 0;
@@ -104,7 +104,7 @@ void mbedtls_unaligned_access(int size, int offset)
/* BEGIN_CASE */
void mbedtls_byteswap(char *input_str, int size, char *expected_str)
{
- uint64_t input, expected;
+ uint64_t input = 0, expected = 0;
TEST_ASSERT(parse_hex_string(input_str, &input));
TEST_ASSERT(parse_hex_string(expected_str, &expected));
|