summaryrefslogtreecommitdiff
path: root/app-crypt/libscrypt/files/libscrypt-1.22-aliasing.patch
blob: 770358b90e6fe4d9be2bed488215765fd9a66d4d (plain)
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
https://bugs.gentoo.org/931556
https://gcc.gnu.org/PR117800
https://github.com/technion/libscrypt/issues/60
https://github.com/technion/libscrypt/pull/62

From 1c20db581099de69d3f8ce8e06981e7c7e14f2e4 Mon Sep 17 00:00:00 2001
From: Sam James <sam@gentoo.org>
Date: Wed, 27 Nov 2024 03:07:57 +0000
Subject: [PATCH 1/3] sysendian.h: fix aliasing violations

Accessing a e.g. uint32_t as uint8_t is an aliasing violation. The
helpers here are nearly idiomatic for avoiding that anyway, so fix
them up accordingly.
--- a/sysendian.h
+++ b/sysendian.h
@@ -49,95 +49,77 @@
 #endif
 
 static INLINE uint32_t
-be32dec(const void *pp)
+be32dec(const unsigned char* p)
 {
-	const uint8_t *p = (uint8_t const *)pp;
-
-	return ((uint32_t)(p[3]) + ((uint32_t)(p[2]) << 8) +
-	    ((uint32_t)(p[1]) << 16) + ((uint32_t)(p[0]) << 24));
+	return ((uint32_t)p[0] << 24) | ((uint32_t)p[1] << 16) | ((uint32_t)p[2] << 8) |
+		((uint32_t)p[3] << 0);
 }
 
 static INLINE void
-be32enc(void *pp, uint32_t x)
+be32enc(unsigned char* p, const uint32_t x)
 {
-	uint8_t * p = (uint8_t *)pp;
-
-	p[3] = x & 0xff;
-	p[2] = (x >> 8) & 0xff;
-	p[1] = (x >> 16) & 0xff;
-	p[0] = (x >> 24) & 0xff;
+	p[0] = (x >> 24);
+	p[1] = (x >> 16);
+	p[2] = (x >> 8);
+	p[3] = (x >> 0);
 }
 
 static INLINE uint64_t
-be64dec(const void *pp)
+be64dec(const unsigned char* p)
 {
-	const uint8_t *p = (uint8_t const *)pp;
-
-	return ((uint64_t)(p[7]) + ((uint64_t)(p[6]) << 8) +
-	    ((uint64_t)(p[5]) << 16) + ((uint64_t)(p[4]) << 24) +
-	    ((uint64_t)(p[3]) << 32) + ((uint64_t)(p[2]) << 40) +
-	    ((uint64_t)(p[1]) << 48) + ((uint64_t)(p[0]) << 56));
+	return ((uint64_t)p[0] << 0) | ((uint64_t)p[1] << 8) | ((uint64_t)p[2] << 16) |
+		((uint64_t)p[3] << 24) | ((uint64_t)p[4] << 32) | ((uint64_t)p[5] << 40) |
+		((uint64_t)p[6] << 48) | ((uint64_t)p[7] << 56);
 }
 
 static INLINE void
-be64enc(void *pp, uint64_t x)
+be64enc(unsigned char* p, const uint64_t x)
 {
-	uint8_t * p = (uint8_t *)pp;
-
-	p[7] = x & 0xff;
-	p[6] = (x >> 8) & 0xff;
-	p[5] = (x >> 16) & 0xff;
-	p[4] = (x >> 24) & 0xff;
-	p[3] = (x >> 32) & 0xff;
-	p[2] = (x >> 40) & 0xff;
-	p[1] = (x >> 48) & 0xff;
-	p[0] = (x >> 56) & 0xff;
+	p[0] = (x >> 56);
+	p[1] = (x >> 48);
+	p[2] = (x >> 40);
+	p[3] = (x >> 32);
+	p[4] = (x >> 24);
+	p[5] = (x >> 16);
+	p[6] = (x >> 8);
+	p[7] = (x >> 0);
 }
 
 static INLINE uint32_t
-le32dec(const void *pp)
+le32dec(const unsigned char* p)
 {
-	const uint8_t *p = (uint8_t const *)pp;
-
-	return ((uint32_t)(p[0]) + ((uint32_t)(p[1]) << 8) +
-	    ((uint32_t)(p[2]) << 16) + ((uint32_t)(p[3]) << 24));
+	return ((uint32_t)p[0] << 0) | ((uint32_t)p[1] << 8) | ((uint32_t)p[2] << 16) |
+		((uint32_t)p[3] << 24);
 }
 
 static INLINE void
-le32enc(void *pp, uint32_t x)
+le32enc(unsigned char* p, const uint32_t x)
 {
-	uint8_t * p = (uint8_t *)pp;
-
-	p[0] = x & 0xff;
-	p[1] = (x >> 8) & 0xff;
-	p[2] = (x >> 16) & 0xff;
-	p[3] = (x >> 24) & 0xff;
+	p[0] = (x >> 0);
+	p[1] = (x >> 8);
+	p[2] = (x >> 16);
+	p[3] = (x >> 24);
 }
 
 static INLINE uint64_t
-le64dec(const void *pp)
+le64dec(const unsigned char* p)
 {
-	const uint8_t *p = (uint8_t const *)pp;
-
-	return ((uint64_t)(p[0]) + ((uint64_t)(p[1]) << 8) +
-	    ((uint64_t)(p[2]) << 16) + ((uint64_t)(p[3]) << 24) +
-	    ((uint64_t)(p[4]) << 32) + ((uint64_t)(p[5]) << 40) +
-	    ((uint64_t)(p[6]) << 48) + ((uint64_t)(p[7]) << 56));
+	return ((uint64_t)p[0] << 0) | ((uint64_t)p[1] << 8) | ((uint64_t)p[2] << 16) |
+		((uint64_t)p[3] << 24) | ((uint64_t)p[4] << 32) | ((uint64_t)p[5] << 40) |
+		((uint64_t)p[6] << 48) | ((uint64_t)p[7] << 56);
 }
 
 static INLINE void
-le64enc(void *pp, uint64_t x)
+le64enc(unsigned char* p, const uint64_t x)
 {
-	uint8_t * p = (uint8_t *)pp;
-
-	p[0] = x & 0xff;
-	p[1] = (x >> 8) & 0xff;
-	p[2] = (x >> 16) & 0xff;
-	p[3] = (x >> 24) & 0xff;
-	p[4] = (x >> 32) & 0xff;
-	p[5] = (x >> 40) & 0xff;
-	p[6] = (x >> 48) & 0xff;
-	p[7] = (x >> 56) & 0xff;
+	p[0] = (x >> 0);
+	p[1] = (x >> 8);
+	p[2] = (x >> 16);
+	p[3] = (x >> 24);
+	p[4] = (x >> 32);
+	p[5] = (x >> 40);
+	p[6] = (x >> 48);
+	p[7] = (x >> 56);
 }
 #endif /* !HAVE_SYS_ENDIAN_H */
 

From da48d4408fb5364ab20ef98696bb5b0f0388f6f9 Mon Sep 17 00:00:00 2001
From: Sam James <sam@gentoo.org>
Date: Wed, 27 Nov 2024 03:09:46 +0000
Subject: [PATCH 2/3] b64: fix -Wold-style-definition

Drop K&R decls. Most of the codebase uses ISO decls anyway.
--- a/b64.c
+++ b/b64.c
@@ -120,11 +120,7 @@ static const char Pad64 = '=';
 */
 
 int
-libscrypt_b64_encode(src, srclength, target, targsize)
-	unsigned char const *src;
-	size_t srclength;
-	char *target;
-	size_t targsize;
+libscrypt_b64_encode(unsigned char const *src, size_t srclength, char *target, size_t targsize)
 {
 	size_t datalength = 0;
 	unsigned char input[3];
@@ -184,10 +180,7 @@ libscrypt_b64_encode(src, srclength, target, targsize)
  */
 
 int
-libscrypt_b64_decode(src, target, targsize)
-	char const *src;
-	unsigned char *target;
-	size_t targsize;
+libscrypt_b64_decode(char const *src, unsigned char *target, size_t targsize)
 {
 	int state, ch;
     unsigned int tarindex;

From 7b574b9c517a3d1f9bd0e265a5f287155293cb85 Mon Sep 17 00:00:00 2001
From: Sam James <sam@gentoo.org>
Date: Wed, 27 Nov 2024 03:48:29 +0000
Subject: [PATCH 3/3] crypto_scrypt-nosse: fix aliasing violations

blkcpy was accessing memory as size_t* where it started as uint32_t*
which is an aliasing violation. But just replace it with memcpy rather
than reinventing the wheel.

It turns out this got fixed in scrypt a while ago, so let's do the same
fix as they did: https://github.com/Tarsnap/scrypt/commit/209fd279c9357010d1dabd446c458dfeb9820e6c.

Closes: https://github.com/technion/libscrypt/issues/60
--- a/crypto_scrypt-nosse.c
+++ b/crypto_scrypt-nosse.c
@@ -41,35 +41,19 @@
 
 #include "libscrypt.h"
 
-static void blkcpy(void *, void *, size_t);
-static void blkxor(void *, void *, size_t);
+static void blkxor(uint32_t * dest, uint32_t * src, size_t len);
 static void salsa20_8(uint32_t[16]);
 static void blockmix_salsa8(uint32_t *, uint32_t *, uint32_t *, size_t);
-static uint64_t integerify(void *, size_t);
+static uint64_t integerify(uint32_t * B, size_t r);
 static void smix(uint8_t *, size_t, uint64_t, uint32_t *, uint32_t *);
 
 static void
-blkcpy(void * dest, void * src, size_t len)
+blkxor(uint32_t * dest, uint32_t * src, size_t len)
 {
-	size_t * D = dest;
-	size_t * S = src;
-	size_t L = len / sizeof(size_t);
 	size_t i;
 
-	for (i = 0; i < L; i++)
-		D[i] = S[i];
-}
-
-static void
-blkxor(void * dest, void * src, size_t len)
-{
-	size_t * D = dest;
-	size_t * S = src;
-	size_t L = len / sizeof(size_t);
-	size_t i;
-
-	for (i = 0; i < L; i++)
-		D[i] ^= S[i];
+	for (i = 0; i < len / 4; i++)
+		dest[i] ^= src[i];
 }
 
 /**
@@ -82,7 +66,7 @@ salsa20_8(uint32_t B[16])
 	uint32_t x[16];
 	size_t i;
 
-	blkcpy(x, B, 64);
+	memcpy(x, B, 64);
 	for (i = 0; i < 8; i += 2) {
 #define R(a,b) (((a) << (b)) | ((a) >> (32 - (b))))
 		/* Operate on columns. */
@@ -128,7 +112,7 @@ blockmix_salsa8(uint32_t * Bin, uint32_t * Bout, uint32_t * X, size_t r)
 	size_t i;
 
 	/* 1: X <-- B_{2r - 1} */
-	blkcpy(X, &Bin[(2 * r - 1) * 16], 64);
+	memcpy(X, &Bin[(2 * r - 1) * 16], 64);
 
 	/* 2: for i = 0 to 2r - 1 do */
 	for (i = 0; i < 2 * r; i += 2) {
@@ -138,7 +122,7 @@ blockmix_salsa8(uint32_t * Bin, uint32_t * Bout, uint32_t * X, size_t r)
 
 		/* 4: Y_i <-- X */
 		/* 6: B' <-- (Y_0, Y_2 ... Y_{2r-2}, Y_1, Y_3 ... Y_{2r-1}) */
-		blkcpy(&Bout[i * 8], X, 64);
+		memcpy(&Bout[i * 8], X, 64);
 
 		/* 3: X <-- H(X \xor B_i) */
 		blkxor(X, &Bin[i * 16 + 16], 64);
@@ -146,7 +130,7 @@ blockmix_salsa8(uint32_t * Bin, uint32_t * Bout, uint32_t * X, size_t r)
 
 		/* 4: Y_i <-- X */
 		/* 6: B' <-- (Y_0, Y_2 ... Y_{2r-2}, Y_1, Y_3 ... Y_{2r-1}) */
-		blkcpy(&Bout[i * 8 + r * 16], X, 64);
+		memcpy(&Bout[i * 8 + r * 16], X, 64);
 	}
 }
 
@@ -155,10 +139,9 @@ blockmix_salsa8(uint32_t * Bin, uint32_t * Bout, uint32_t * X, size_t r)
  * Return the result of parsing B_{2r-1} as a little-endian integer.
  */
 static uint64_t
-integerify(void * B, size_t r)
+integerify(uint32_t * B, size_t r)
 {
-	uint32_t * X = (void *)((uintptr_t)(B) + (2 * r - 1) * 64);
-
+	const uint32_t * X = B + (2 * r - 1) * 16;
 	return (((uint64_t)(X[1]) << 32) + X[0]);
 }
 
@@ -187,13 +170,13 @@ smix(uint8_t * B, size_t r, uint64_t N, uint32_t * V, uint32_t * XY)
 	/* 2: for i = 0 to N - 1 do */
 	for (i = 0; i < N; i += 2) {
 		/* 3: V_i <-- X */
-		blkcpy(&V[i * (32 * r)], X, 128 * r);
+		memcpy(&V[i * (32 * r)], X, 128 * r);
 
 		/* 4: X <-- H(X) */
 		blockmix_salsa8(X, Y, Z, r);
 
 		/* 3: V_i <-- X */
-		blkcpy(&V[(i + 1) * (32 * r)], Y, 128 * r);
+		memcpy(&V[(i + 1) * (32 * r)], Y, 128 * r);
 
 		/* 4: X <-- H(X) */
 		blockmix_salsa8(Y, X, Z, r);