Skip to content

Commit c336659

Browse files
authored
Merge pull request #10707 from SparkiDev/mlkem_mldsa_unaligned
ML-KEM/ML-DSA: unaligned reads
2 parents 039e97d + bce9fdd commit c336659

4 files changed

Lines changed: 136 additions & 57 deletions

File tree

wolfcrypt/src/misc.c

Lines changed: 70 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -231,30 +231,52 @@ WC_MISC_STATIC WC_INLINE void ByteReverseWords(word32* out, const word32* in,
231231
#endif
232232
}
233233

234+
#if ((defined(WOLFSSL_AARCH64_BUILD) || defined(__aarch64__)) && \
235+
defined(__APPLE__)) || \
236+
defined(WOLFSSL_X86_64_BUILD) || defined(WOLFSSL_X86_BUILD)
237+
#ifndef WOLFSSL_RW_UNALIGNED_32
238+
#define WOLFSSL_RW_UNALIGNED_32
239+
#endif
240+
#endif
241+
#ifdef WOLFSSL_RW_UNALIGNED_32
242+
typedef word32 MAYBE_UNALIGNED uword32;
243+
#endif
244+
234245
WC_MISC_STATIC WC_INLINE word32 readUnalignedWord32(const byte *in)
235246
{
236-
if (((wc_ptr_t)in & (wc_ptr_t)(sizeof(word32) - 1U)) == (wc_ptr_t)0)
247+
#ifndef WOLFSSL_RW_UNALIGNED_32
248+
if (((wc_ptr_t)in & (wc_ptr_t)(sizeof(word32) - 1U)) == (wc_ptr_t)0) {
237249
return *(const word32 *)in;
250+
}
238251
else {
239252
word32 out = 0; /* else CONFIG_FORTIFY_SOURCE -Wmaybe-uninitialized */
240253
XMEMCPY(&out, in, sizeof(out));
241254
return out;
242255
}
256+
#else
257+
return *(const uword32 *)in;
258+
#endif
243259
}
244260

245261
WC_MISC_STATIC WC_INLINE word32 writeUnalignedWord32(void *out, word32 in)
246262
{
247-
if (((wc_ptr_t)out & (wc_ptr_t)(sizeof(word32) - 1U)) == (wc_ptr_t)0)
263+
#ifndef WOLFSSL_RW_UNALIGNED_32
264+
if (((wc_ptr_t)out & (wc_ptr_t)(sizeof(word32) - 1U)) == (wc_ptr_t)0) {
248265
*(word32 *)out = in;
266+
}
249267
else {
250268
XMEMCPY(out, &in, sizeof(in));
251269
}
270+
#else
271+
*(uword32 *)out = in;
272+
#endif
252273
return in;
253274
}
254275

255276
WC_MISC_STATIC WC_INLINE void readUnalignedWords32(word32 *out, const byte *in,
256277
size_t count)
257278
{
279+
#ifndef WOLFSSL_RW_UNALIGNED_32
258280
if (((wc_ptr_t)in & (wc_ptr_t)(sizeof(word32) - 1U)) == (wc_ptr_t)0) {
259281
const word32 *in_word32 = (const word32 *)in;
260282
while (count-- > 0)
@@ -263,11 +285,17 @@ WC_MISC_STATIC WC_INLINE void readUnalignedWords32(word32 *out, const byte *in,
263285
else {
264286
XMEMCPY(out, in, count * sizeof(*out));
265287
}
288+
#else
289+
const uword32 *in_word32 = (const uword32 *)in;
290+
while (count-- > 0)
291+
*out++ = *in_word32++;
292+
#endif
266293
}
267294

268295
WC_MISC_STATIC WC_INLINE void writeUnalignedWords32(byte *out, const word32 *in,
269296
size_t count)
270297
{
298+
#ifndef WOLFSSL_RW_UNALIGNED_32
271299
if (((wc_ptr_t)out & (wc_ptr_t)(sizeof(word32) - 1U)) == (wc_ptr_t)0) {
272300
word32 *out_word32 = (word32 *)out;
273301
while (count-- > 0)
@@ -276,34 +304,61 @@ WC_MISC_STATIC WC_INLINE void writeUnalignedWords32(byte *out, const word32 *in,
276304
else {
277305
XMEMCPY(out, in, count * sizeof(*in));
278306
}
307+
#else
308+
uword32 *out_word32 = (uword32 *)out;
309+
while (count-- > 0)
310+
*out_word32++ = *in++;
311+
#endif
279312
}
280313

281314
#if defined(WORD64_AVAILABLE) && !defined(WOLFSSL_NO_WORD64_OPS)
282315

316+
#if ((defined(WOLFSSL_AARCH64_BUILD) || defined(__aarch64__)) && \
317+
defined(__APPLE__)) || \
318+
defined(WOLFSSL_X86_64_BUILD) || defined(WOLFSSL_X86_BUILD)
319+
#ifndef WOLFSSL_RW_UNALIGNED_64
320+
#define WOLFSSL_RW_UNALIGNED_64
321+
#endif
322+
#endif
323+
#ifdef WOLFSSL_RW_UNALIGNED_64
324+
typedef word64 MAYBE_UNALIGNED uword64;
325+
#endif
326+
283327
WC_MISC_STATIC WC_INLINE word64 readUnalignedWord64(const byte *in)
284328
{
285-
if (((wc_ptr_t)in & (wc_ptr_t)(sizeof(word64) - 1U)) == (wc_ptr_t)0)
329+
#ifndef WOLFSSL_RW_UNALIGNED_64
330+
if (((wc_ptr_t)in & (wc_ptr_t)(sizeof(word64) - 1U)) == (wc_ptr_t)0) {
286331
return *(const word64 *)in;
332+
}
287333
else {
288334
word64 out = 0; /* else CONFIG_FORTIFY_SOURCE -Wmaybe-uninitialized */
289335
XMEMCPY(&out, in, sizeof(out));
290336
return out;
291337
}
338+
#else
339+
return *(const uword64 *)in;
340+
#endif
292341
}
293342

294343
WC_MISC_STATIC WC_INLINE word64 writeUnalignedWord64(void *out, word64 in)
295344
{
296-
if (((wc_ptr_t)out & (wc_ptr_t)(sizeof(word64) - 1U)) == (wc_ptr_t)0)
345+
#ifndef WOLFSSL_RW_UNALIGNED_64
346+
if (((wc_ptr_t)out & (wc_ptr_t)(sizeof(word64) - 1U)) == (wc_ptr_t)0) {
297347
*(word64 *)out = in;
348+
}
298349
else {
299350
XMEMCPY(out, &in, sizeof(in));
300351
}
352+
#else
353+
*(uword64 *)out = in;
354+
#endif
301355
return in;
302356
}
303357

304358
WC_MISC_STATIC WC_INLINE void readUnalignedWords64(word64 *out, const byte *in,
305359
size_t count)
306360
{
361+
#ifndef WOLFSSL_RW_UNALIGNED_64
307362
if (((wc_ptr_t)in & (wc_ptr_t)(sizeof(word64) - 1U)) == (wc_ptr_t)0) {
308363
const word64 *in_word64 = (const word64 *)in;
309364
while (count-- > 0)
@@ -312,11 +367,17 @@ WC_MISC_STATIC WC_INLINE void readUnalignedWords64(word64 *out, const byte *in,
312367
else {
313368
XMEMCPY(out, in, count * sizeof(*out));
314369
}
370+
#else
371+
const uword64 *in_word64 = (const uword64 *)in;
372+
while (count-- > 0)
373+
*out++ = *in_word64++;
374+
#endif
315375
}
316376

317377
WC_MISC_STATIC WC_INLINE void writeUnalignedWords64(byte *out, const word64 *in,
318378
size_t count)
319379
{
380+
#ifndef WOLFSSL_RW_UNALIGNED_64
320381
if (((wc_ptr_t)out & (wc_ptr_t)(sizeof(word64) - 1U)) == (wc_ptr_t)0) {
321382
word64 *out_word64 = (word64 *)out;
322383
while (count-- > 0)
@@ -325,6 +386,11 @@ WC_MISC_STATIC WC_INLINE void writeUnalignedWords64(byte *out, const word64 *in,
325386
else {
326387
XMEMCPY(out, in, count * sizeof(*in));
327388
}
389+
#else
390+
uword64 *out_word64 = (uword64 *)out;
391+
while (count-- > 0)
392+
*out_word64++ = *in++;
393+
#endif
328394
}
329395

330396
WC_MISC_STATIC WC_INLINE word64 rotlFixed64(word64 x, word64 y)

0 commit comments

Comments
 (0)