Skip to content

Commit 202822c

Browse files
authored
Merge pull request wolfSSL#8114 from douzzer/20241025-fixes
20241025-fixes
2 parents bdd6231 + 6f87f57 commit 202822c

6 files changed

Lines changed: 89 additions & 12 deletions

File tree

src/dtls13.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,8 @@ static int Dtls13GetRnMask(WOLFSSL* ssl, const byte* ciphertext, byte* mask,
260260
if (c->aes == NULL)
261261
return BAD_STATE_E;
262262
#if !defined(HAVE_SELFTEST) && \
263-
(!defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))
263+
(!defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)) \
264+
|| defined(WOLFSSL_LINUXKM))
264265
return wc_AesEncryptDirect(c->aes, mask, ciphertext);
265266
#else
266267
wc_AesEncryptDirect(c->aes, mask, ciphertext);

src/ssl_crypto.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3002,7 +3002,8 @@ void wolfSSL_AES_encrypt(const unsigned char* input, unsigned char* output,
30023002
}
30033003
else
30043004
#if !defined(HAVE_SELFTEST) && \
3005-
(!defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))
3005+
(!defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)) \
3006+
|| defined(WOLFSSL_LINUXKM))
30063007
/* Encrypt a block with wolfCrypt AES. */
30073008
if (wc_AesEncryptDirect((Aes*)key, output, input) != 0) {
30083009
WOLFSSL_MSG("wc_AesEncryptDirect failed");

wolfcrypt/src/misc.c

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -209,15 +209,62 @@ WC_MISC_STATIC WC_INLINE void ByteReverseWords(word32* out, const word32* in,
209209
#endif
210210
}
211211

212+
WC_MISC_STATIC WC_INLINE word32 readUnalignedWord32(const byte *in)
213+
{
214+
if (((wc_ptr_t)in & (wc_ptr_t)(sizeof(word32) - 1U)) == (wc_ptr_t)0)
215+
return *(word32 *)in;
216+
else {
217+
word32 out = 0; /* else CONFIG_FORTIFY_SOURCE -Wmaybe-uninitialized */
218+
XMEMCPY(&out, in, sizeof(out));
219+
return out;
220+
}
221+
}
222+
223+
WC_MISC_STATIC WC_INLINE word32 writeUnalignedWord32(void *out, word32 in)
224+
{
225+
if (((wc_ptr_t)out & (wc_ptr_t)(sizeof(word32) - 1U)) == (wc_ptr_t)0)
226+
*(word32 *)out = in;
227+
else {
228+
XMEMCPY(out, &in, sizeof(in));
229+
}
230+
return in;
231+
}
232+
233+
WC_MISC_STATIC WC_INLINE void readUnalignedWords32(word32 *out, const byte *in,
234+
size_t count)
235+
{
236+
if (((wc_ptr_t)in & (wc_ptr_t)(sizeof(word32) - 1U)) == (wc_ptr_t)0) {
237+
const word32 *in_word32 = (const word32 *)in;
238+
while (count-- > 0)
239+
*out++ = *in_word32++;
240+
}
241+
else {
242+
XMEMCPY(out, in, count * sizeof(*out));
243+
}
244+
}
245+
246+
WC_MISC_STATIC WC_INLINE void writeUnalignedWords32(byte *out, const word32 *in,
247+
size_t count)
248+
{
249+
if (((wc_ptr_t)out & (wc_ptr_t)(sizeof(word32) - 1U)) == (wc_ptr_t)0) {
250+
word32 *out_word32 = (word32 *)out;
251+
while (count-- > 0)
252+
*out_word32++ = *in++;
253+
}
254+
else {
255+
XMEMCPY(out, in, count * sizeof(*in));
256+
}
257+
}
258+
212259
#if defined(WORD64_AVAILABLE) && !defined(WOLFSSL_NO_WORD64_OPS)
213260

214261
WC_MISC_STATIC WC_INLINE word64 readUnalignedWord64(const byte *in)
215262
{
216263
if (((wc_ptr_t)in & (wc_ptr_t)(sizeof(word64) - 1U)) == (wc_ptr_t)0)
217264
return *(word64 *)in;
218265
else {
219-
word64 out;
220-
XMEMCPY(&out, in, sizeof(word64));
266+
word64 out = 0; /* else CONFIG_FORTIFY_SOURCE -Wmaybe-uninitialized */
267+
XMEMCPY(&out, in, sizeof(out));
221268
return out;
222269
}
223270
}
@@ -227,7 +274,7 @@ WC_MISC_STATIC WC_INLINE word64 writeUnalignedWord64(void *out, word64 in)
227274
if (((wc_ptr_t)out & (wc_ptr_t)(sizeof(word64) - 1U)) == (wc_ptr_t)0)
228275
*(word64 *)out = in;
229276
else {
230-
XMEMCPY(out, &in, sizeof(word64));
277+
XMEMCPY(out, &in, sizeof(in));
231278
}
232279
return in;
233280
}
@@ -241,7 +288,7 @@ WC_MISC_STATIC WC_INLINE void readUnalignedWords64(word64 *out, const byte *in,
241288
*out++ = *in_word64++;
242289
}
243290
else {
244-
XMEMCPY(out, in, count * sizeof(word64));
291+
XMEMCPY(out, in, count * sizeof(*out));
245292
}
246293
}
247294

@@ -254,7 +301,7 @@ WC_MISC_STATIC WC_INLINE void writeUnalignedWords64(byte *out, const word64 *in,
254301
*out_word64++ = *in++;
255302
}
256303
else {
257-
XMEMCPY(out, in, count * sizeof(word64));
304+
XMEMCPY(out, in, count * sizeof(*in));
258305
}
259306
}
260307

wolfcrypt/src/port/arm/armv8-aes.c

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16561,6 +16561,7 @@ int wc_AesSetKey(Aes* aes, const byte* userKey, word32 keylen,
1656116561
{
1656216562
#if defined(AES_MAX_KEY_SIZE)
1656316563
const word32 max_key_len = (AES_MAX_KEY_SIZE / 8);
16564+
word32 userKey_aligned[AES_MAX_KEY_SIZE / WOLFSSL_BIT_SIZE / sizeof(word32)];
1656416565
#endif
1656516566

1656616567
if (((keylen != 16) && (keylen != 24) && (keylen != 32)) ||
@@ -16574,6 +16575,14 @@ int wc_AesSetKey(Aes* aes, const byte* userKey, word32 keylen,
1657416575
return BAD_FUNC_ARG;
1657516576
}
1657616577
#endif
16578+
16579+
#if !defined(AES_MAX_KEY_SIZE)
16580+
/* Check alignment */
16581+
if ((unsigned long)userKey & (sizeof(aes->key[0]) - 1U)) {
16582+
return BAD_FUNC_ARG;
16583+
}
16584+
#endif
16585+
1657716586
#ifdef WOLF_CRYPTO_CB
1657816587
if (aes->devId != INVALID_DEVID) {
1657916588
if (keylen > sizeof(aes->devKey)) {
@@ -16590,7 +16599,17 @@ int wc_AesSetKey(Aes* aes, const byte* userKey, word32 keylen,
1659016599
aes->keylen = keylen;
1659116600
aes->rounds = keylen/4 + 6;
1659216601

16593-
AES_set_encrypt_key(userKey, keylen * 8, (byte*)aes->key);
16602+
#if defined(AES_MAX_KEY_SIZE)
16603+
if ((unsigned long)userKey & (sizeof(aes->key[0]) - 1U)) {
16604+
XMEMCPY(userKey_aligned, userKey, keylen);
16605+
AES_set_encrypt_key((byte *)userKey_aligned, keylen * 8, (byte*)aes->key);
16606+
}
16607+
else
16608+
#endif
16609+
{
16610+
AES_set_encrypt_key(userKey, keylen * 8, (byte*)aes->key);
16611+
}
16612+
1659416613
#ifdef HAVE_AES_DECRYPT
1659516614
if (dir == AES_DECRYPTION) {
1659616615
AES_invert_key((byte*)aes->key, aes->rounds);

wolfcrypt/src/siphash.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,14 @@
6969
* @param [in] a Little-endian byte array.
7070
* @return 64-bit number.
7171
*/
72-
#define GET_U64(a) (*(word64*)(a))
72+
#define GET_U64(a) readUnalignedWord64(a)
7373
/**
7474
* Decode little-endian byte array to 32-bit number.
7575
*
7676
* @param [in] a Little-endian byte array.
7777
* @return 32-bit number.
7878
*/
79-
#define GET_U32(a) (*(word32*)(a))
79+
#define GET_U32(a) readUnalignedWord32(a)
8080
/**
8181
* Decode little-endian byte array to 16-bit number.
8282
*
@@ -90,7 +90,7 @@
9090
* @param [out] a Byte array to write into.
9191
* @param [in] n Number to encode.
9292
*/
93-
#define SET_U64(a, n) ((*(word64*)(a)) = (n))
93+
#define SET_U64(a, n) writeUnalignedWord64(a, n)
9494
#else
9595
/**
9696
* Decode little-endian byte array to 64-bit number.
@@ -112,7 +112,7 @@
112112
* @param [in] a Little-endian byte array.
113113
* @return 32-bit number.
114114
*/
115-
#define GET_U32(a) (((word64)((a)[3]) << 24) | \
115+
#define GET_U32(a) (((word32)((a)[3]) << 24) | \
116116
((word32)((a)[2]) << 16) | \
117117
((word32)((a)[1]) << 8) | \
118118
((word32)((a)[0]) ))

wolfssl/wolfcrypt/misc.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,15 @@ void ForceZero(void* mem, word32 len);
7474
WOLFSSL_LOCAL
7575
int ConstantCompare(const byte* a, const byte* b, int length);
7676

77+
WOLFSSL_LOCAL
78+
word32 readUnalignedWord32(const byte *in);
79+
WOLFSSL_LOCAL
80+
word32 writeUnalignedWord32(void *out, word32 in);
81+
WOLFSSL_LOCAL
82+
void readUnalignedWords32(word32 *out, const byte *in, size_t count);
83+
WOLFSSL_LOCAL
84+
void writeUnalignedWords32(byte *out, const word32 *in, size_t count);
85+
7786
#ifdef WORD64_AVAILABLE
7887
WOLFSSL_LOCAL
7988
word64 readUnalignedWord64(const byte *in);

0 commit comments

Comments
 (0)