Try to XOR as many words as possible in xorbuf APIs

This commit is contained in:
Juliusz Sosinowicz
2023-03-23 10:46:09 +01:00
parent 81ab16b105
commit 8fe321d5be
2 changed files with 41 additions and 27 deletions

View File

@@ -256,12 +256,13 @@ WC_MISC_STATIC WC_INLINE void ByteReverseWords64(word64* out, const word64* in,
#ifndef WOLFSSL_NO_XOR_OPS #ifndef WOLFSSL_NO_XOR_OPS
/* This routine performs a bitwise XOR operation of <*r> and <*a> for <n> number /* This routine performs a bitwise XOR operation of <*r> and <*a> for <n> number
of wolfssl_words, placing the result in <*r>. */ of wolfssl_words, placing the result in <*r>. */
WC_MISC_STATIC WC_INLINE void XorWordsOut(wolfssl_word* r, WC_MISC_STATIC WC_INLINE void XorWordsOut(wolfssl_word** r,
const wolfssl_word* a, const wolfssl_word* b, word32 n) const wolfssl_word** a, const wolfssl_word** b, word32 n)
{ {
word32 i; word32 i;
for (i = 0; i < n; i++) r[i] = a[i] ^ b[i]; for (i = 0; i < n; i++)
*(*r)++ = *(*a)++ ^ *(*b)++;
} }
/* This routine performs a bitwise XOR operation of <*buf> and <*mask> of n /* This routine performs a bitwise XOR operation of <*buf> and <*mask> of n
@@ -270,28 +271,35 @@ counts, placing the result in <*buf>. */
WC_MISC_STATIC WC_INLINE void xorbufout(void* out, const void* buf, WC_MISC_STATIC WC_INLINE void xorbufout(void* out, const void* buf,
const void* mask, word32 count) const void* mask, word32 count)
{ {
if (((wc_ptr_t)out | (wc_ptr_t)buf | (wc_ptr_t)mask | count) %
WOLFSSL_WORD_SIZE == 0)
XorWordsOut( (wolfssl_word*)out, (wolfssl_word*)buf,
(const wolfssl_word*)mask, count / WOLFSSL_WORD_SIZE);
else {
word32 i; word32 i;
byte* o = (byte*)out; byte* o;
byte* b = (byte*)buf; byte* b;
const byte* m = (const byte*)mask; const byte* m;
for (i = 0; i < count; i++) o[i] = b[i] ^ m[i]; if (((wc_ptr_t)buf | (wc_ptr_t)mask) % WOLFSSL_WORD_SIZE == 0) {
/* Alignment checks out. Possible to XOR words. */
XorWordsOut( (wolfssl_word**)&out, (const wolfssl_word**)&buf,
(const wolfssl_word**)&mask, count / WOLFSSL_WORD_SIZE);
count %= WOLFSSL_WORD_SIZE;
} }
o = (byte*)out;
b = (byte*)buf;
m = (const byte*)mask;
for (i = 0; i < count; i++)
o[i] = b[i] ^ m[i];
} }
/* This routine performs a bitwise XOR operation of <*r> and <*a> for <n> number /* This routine performs a bitwise XOR operation of <*r> and <*a> for <n> number
of wolfssl_words, placing the result in <*r>. */ of wolfssl_words, placing the result in <*r>. */
WC_MISC_STATIC WC_INLINE void XorWords(wolfssl_word* r, const wolfssl_word* a, WC_MISC_STATIC WC_INLINE void XorWords(wolfssl_word** r, const wolfssl_word** a,
word32 n) word32 n)
{ {
word32 i; word32 i;
for (i = 0; i < n; i++) r[i] ^= a[i]; for (i = 0; i < n; i++)
*(*r)++ ^= *(*a)++;
} }
/* This routine performs a bitwise XOR operation of <*buf> and <*mask> of n /* This routine performs a bitwise XOR operation of <*buf> and <*mask> of n
@@ -299,16 +307,22 @@ counts, placing the result in <*buf>. */
WC_MISC_STATIC WC_INLINE void xorbuf(void* buf, const void* mask, word32 count) WC_MISC_STATIC WC_INLINE void xorbuf(void* buf, const void* mask, word32 count)
{ {
if (((wc_ptr_t)buf | (wc_ptr_t)mask | count) % WOLFSSL_WORD_SIZE == 0)
XorWords( (wolfssl_word*)buf,
(const wolfssl_word*)mask, count / WOLFSSL_WORD_SIZE);
else {
word32 i; word32 i;
byte* b = (byte*)buf; byte* b;
const byte* m = (const byte*)mask; const byte* m;
for (i = 0; i < count; i++) b[i] ^= m[i]; if (((wc_ptr_t)buf | (wc_ptr_t)mask) % WOLFSSL_WORD_SIZE == 0) {
/* Alignment checks out. Possible to XOR words. */
XorWords( (wolfssl_word**)&buf,
(const wolfssl_word**)&mask, count / WOLFSSL_WORD_SIZE);
count %= WOLFSSL_WORD_SIZE;
} }
b = (byte*)buf;
m = (const byte*)mask;
for (i = 0; i < count; i++)
b[i] ^= m[i];
} }
#endif #endif

View File

@@ -59,12 +59,12 @@ WOLFSSL_LOCAL
void ByteReverseWords(word32* out, const word32* in, word32 byteCount); void ByteReverseWords(word32* out, const word32* in, word32 byteCount);
WOLFSSL_LOCAL WOLFSSL_LOCAL
void XorWordsOut(wolfssl_word* r, const wolfssl_word* a, const wolfssl_word* b, void XorWordsOut(wolfssl_word** r, const wolfssl_word** a,
word32 n); const wolfssl_word** b, word32 n);
WOLFSSL_LOCAL WOLFSSL_LOCAL
void xorbufout(void* out, const void* buf, const void* mask, word32 count); void xorbufout(void* out, const void* buf, const void* mask, word32 count);
WOLFSSL_LOCAL WOLFSSL_LOCAL
void XorWords(wolfssl_word* r, const wolfssl_word* a, word32 n); void XorWords(wolfssl_word** r, const wolfssl_word** a, word32 n);
WOLFSSL_LOCAL WOLFSSL_LOCAL
void xorbuf(void* buf, const void* mask, word32 count); void xorbuf(void* buf, const void* mask, word32 count);