Skip to content

Commit

Permalink
Merge pull request #8482 from douzzer/20250220-misc-UnalignedWord64
Browse files Browse the repository at this point in the history
20250220-misc-UnalignedWord64
  • Loading branch information
JacobBarthelmeh authored Feb 21, 2025
2 parents 781d852 + a054360 commit 8ae1225
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 49 deletions.
32 changes: 0 additions & 32 deletions wolfcrypt/src/misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -254,36 +254,6 @@ WC_MISC_STATIC WC_INLINE void writeUnalignedWords32(byte *out, const word32 *in,

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

#ifdef WOLFSSL_X86_64_BUILD

WC_MISC_STATIC WC_INLINE word64 readUnalignedWord64(const byte *in)
{
return ((word64*)in)[0];
}

WC_MISC_STATIC WC_INLINE word64 writeUnalignedWord64(void *out, word64 in)
{
return ((word64*)out)[0] = in;
}

WC_MISC_STATIC WC_INLINE void readUnalignedWords64(word64 *out, const byte *in,
size_t count)
{
const word64 *in_word64 = (const word64 *)in;
while (count-- > 0)
*out++ = *in_word64++;
}

WC_MISC_STATIC WC_INLINE void writeUnalignedWords64(byte *out, const word64 *in,
size_t count)
{
word64 *out_word64 = (word64 *)out;
while (count-- > 0)
*out_word64++ = *in++;
}

#else

WC_MISC_STATIC WC_INLINE word64 readUnalignedWord64(const byte *in)
{
if (((wc_ptr_t)in & (wc_ptr_t)(sizeof(word64) - 1U)) == (wc_ptr_t)0)
Expand Down Expand Up @@ -331,8 +301,6 @@ WC_MISC_STATIC WC_INLINE void writeUnalignedWords64(byte *out, const word64 *in,
}
}

#endif

WC_MISC_STATIC WC_INLINE word64 rotlFixed64(word64 x, word64 y)
{
return (x << y) | (x >> (sizeof(y) * 8 - y));
Expand Down
34 changes: 17 additions & 17 deletions wolfcrypt/test/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -17157,18 +17157,18 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t camellia_test(void)
wc_Camellia cam;
WOLFSSL_SMALL_STACK_STATIC const test_vector_t testVectors[] =
{
{CAM_ECB_ENC, pte, ive, c1, k1, sizeof(k1), -114},
{CAM_ECB_ENC, pte, ive, c2, k2, sizeof(k2), -115},
{CAM_ECB_ENC, pte, ive, c3, k3, sizeof(k3), -116},
{CAM_ECB_DEC, pte, ive, c1, k1, sizeof(k1), -117},
{CAM_ECB_DEC, pte, ive, c2, k2, sizeof(k2), -118},
{CAM_ECB_DEC, pte, ive, c3, k3, sizeof(k3), -119},
{CAM_CBC_ENC, ptc, ivc, c4, k4, sizeof(k4), -120},
{CAM_CBC_ENC, ptc, ivc, c5, k5, sizeof(k5), -121},
{CAM_CBC_ENC, ptc, ivc, c6, k6, sizeof(k6), -122},
{CAM_CBC_DEC, ptc, ivc, c4, k4, sizeof(k4), -123},
{CAM_CBC_DEC, ptc, ivc, c5, k5, sizeof(k5), -124},
{CAM_CBC_DEC, ptc, ivc, c6, k6, sizeof(k6), -125}
{CAM_ECB_ENC, pte, ive, c1, k1, sizeof(k1), 114},
{CAM_ECB_ENC, pte, ive, c2, k2, sizeof(k2), 115},
{CAM_ECB_ENC, pte, ive, c3, k3, sizeof(k3), 116},
{CAM_ECB_DEC, pte, ive, c1, k1, sizeof(k1), 117},
{CAM_ECB_DEC, pte, ive, c2, k2, sizeof(k2), 118},
{CAM_ECB_DEC, pte, ive, c3, k3, sizeof(k3), 119},
{CAM_CBC_ENC, ptc, ivc, c4, k4, sizeof(k4), 120},
{CAM_CBC_ENC, ptc, ivc, c5, k5, sizeof(k5), 121},
{CAM_CBC_ENC, ptc, ivc, c6, k6, sizeof(k6), 122},
{CAM_CBC_DEC, ptc, ivc, c4, k4, sizeof(k4), 123},
{CAM_CBC_DEC, ptc, ivc, c5, k5, sizeof(k5), 124},
{CAM_CBC_DEC, ptc, ivc, c6, k6, sizeof(k6), 125}
};
int i, testsSz;
int ret;
Expand All @@ -17178,36 +17178,36 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t camellia_test(void)
for (i = 0; i < testsSz; i++) {
if (wc_CamelliaSetKey(&cam, testVectors[i].key, testVectors[i].keySz,
testVectors[i].iv) != 0)
return testVectors[i].errorCode;
return WC_TEST_RET_ENC_I(testVectors[i].errorCode);

switch (testVectors[i].type) {
case CAM_ECB_ENC:
ret = wc_CamelliaEncryptDirect(&cam, out,
testVectors[i].plaintext);
if (ret != 0 || XMEMCMP(out, testVectors[i].ciphertext,
WC_CAMELLIA_BLOCK_SIZE))
return testVectors[i].errorCode;
return WC_TEST_RET_ENC_I(testVectors[i].errorCode);
break;
case CAM_ECB_DEC:
ret = wc_CamelliaDecryptDirect(&cam, out,
testVectors[i].ciphertext);
if (ret != 0 || XMEMCMP(out, testVectors[i].plaintext,
WC_CAMELLIA_BLOCK_SIZE))
return testVectors[i].errorCode;
return WC_TEST_RET_ENC_I(testVectors[i].errorCode);
break;
case CAM_CBC_ENC:
ret = wc_CamelliaCbcEncrypt(&cam, out, testVectors[i].plaintext,
WC_CAMELLIA_BLOCK_SIZE);
if (ret != 0 || XMEMCMP(out, testVectors[i].ciphertext,
WC_CAMELLIA_BLOCK_SIZE))
return testVectors[i].errorCode;
return WC_TEST_RET_ENC_I(testVectors[i].errorCode);
break;
case CAM_CBC_DEC:
ret = wc_CamelliaCbcDecrypt(&cam, out,
testVectors[i].ciphertext, WC_CAMELLIA_BLOCK_SIZE);
if (ret != 0 || XMEMCMP(out, testVectors[i].plaintext,
WC_CAMELLIA_BLOCK_SIZE))
return testVectors[i].errorCode;
return WC_TEST_RET_ENC_I(testVectors[i].errorCode);
break;
default:
break;
Expand Down
8 changes: 8 additions & 0 deletions wolfssl/wolfcrypt/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,17 @@ decouple library dependencies with standard string, memory and so on.
/* The C standards don't define empty aggregates, but gcc and clang do.
* We need to accommodate them for one of the same reasons C++ does --
* conditionally empty aggregates, e.g. in hash.h.
*
* Nonetheless, in C++, empty aggregates wind up with size 1. If we use
* the [0] construct and the header is compiled by clang++, it warns
* "struct has size 0 in C, size 1 in C++ [-Wextern-c-compat]", despite
* the extern "C" wrapper. We sidestep this warning by recognizing
* here that C++ doesn't support truly empty aggregates. LLVM, for its part,
* deprecates compilation of C code as C++ using clang++.
*/
#if !defined(WOLF_C89) && defined(__GNUC__) && \
!defined(__STRICT_ANSI__) && \
!defined(__cplusplus) && \
defined(HAVE_ANONYMOUS_INLINE_AGGREGATES)
#define HAVE_EMPTY_AGGREGATES 1
#endif
Expand Down

0 comments on commit 8ae1225

Please sign in to comment.