2022-11-22 18:31:50 +01:00
|
|
|
/* BEGIN_HEADER */
|
2023-08-30 16:34:34 +02:00
|
|
|
#include "common.h"
|
2022-11-22 18:31:50 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
void fill_arrays(unsigned char *a, unsigned char *b, unsigned char *r1, unsigned char *r2, size_t n)
|
2022-11-22 18:31:50 +01:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
for (size_t i = 0; i < n; i++) {
|
2022-11-22 18:31:50 +01:00
|
|
|
a[i] = (unsigned char) i * 3;
|
|
|
|
b[i] = (unsigned char) i * 3 + 1;
|
|
|
|
r1[i] = (unsigned char) i * 3 + 2;
|
|
|
|
r2[i] = r1[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* END_HEADER */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_xor(int len)
|
2022-11-22 18:31:50 +01:00
|
|
|
{
|
|
|
|
size_t n = (size_t) len;
|
|
|
|
unsigned char *a = NULL, *b = NULL, *r1 = NULL, *r2 = NULL;
|
2023-07-21 12:31:13 +02:00
|
|
|
TEST_CALLOC(a, n + 1);
|
|
|
|
TEST_CALLOC(b, n + 1);
|
|
|
|
TEST_CALLOC(r1, n + 1);
|
|
|
|
TEST_CALLOC(r2, n + 1);
|
2022-11-22 18:31:50 +01:00
|
|
|
|
|
|
|
/* Test non-overlapping */
|
2023-01-11 14:50:10 +01:00
|
|
|
fill_arrays(a, b, r1, r2, n);
|
|
|
|
for (size_t i = 0; i < n; i++) {
|
2022-11-22 18:31:50 +01:00
|
|
|
r1[i] = a[i] ^ b[i];
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_xor(r2, a, b, n);
|
2023-07-21 12:40:20 +02:00
|
|
|
TEST_MEMORY_COMPARE(r1, n, r2, n);
|
2022-11-22 18:31:50 +01:00
|
|
|
|
|
|
|
/* Test r == a */
|
2023-01-11 14:50:10 +01:00
|
|
|
fill_arrays(a, b, r1, r2, n);
|
|
|
|
for (size_t i = 0; i < n; i++) {
|
2022-11-22 18:31:50 +01:00
|
|
|
r1[i] = r1[i] ^ b[i];
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_xor(r2, r2, b, n);
|
2023-07-21 12:40:20 +02:00
|
|
|
TEST_MEMORY_COMPARE(r1, n, r2, n);
|
2022-11-22 18:31:50 +01:00
|
|
|
|
|
|
|
/* Test r == b */
|
2023-01-11 14:50:10 +01:00
|
|
|
fill_arrays(a, b, r1, r2, n);
|
|
|
|
for (size_t i = 0; i < n; i++) {
|
2022-11-22 18:31:50 +01:00
|
|
|
r1[i] = a[i] ^ r1[i];
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_xor(r2, a, r2, n);
|
2023-07-21 12:40:20 +02:00
|
|
|
TEST_MEMORY_COMPARE(r1, n, r2, n);
|
2022-11-22 18:31:50 +01:00
|
|
|
|
|
|
|
/* Test a == b */
|
2023-01-11 14:50:10 +01:00
|
|
|
fill_arrays(a, b, r1, r2, n);
|
|
|
|
for (size_t i = 0; i < n; i++) {
|
2022-11-22 18:31:50 +01:00
|
|
|
r1[i] = a[i] ^ a[i];
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_xor(r2, a, a, n);
|
2023-07-21 12:40:20 +02:00
|
|
|
TEST_MEMORY_COMPARE(r1, n, r2, n);
|
2022-11-22 18:31:50 +01:00
|
|
|
|
|
|
|
/* Test a == b == r */
|
2023-01-11 14:50:10 +01:00
|
|
|
fill_arrays(a, b, r1, r2, n);
|
|
|
|
for (size_t i = 0; i < n; i++) {
|
2022-11-22 18:31:50 +01:00
|
|
|
r1[i] = r1[i] ^ r1[i];
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_xor(r2, r2, r2, n);
|
2023-07-21 12:40:20 +02:00
|
|
|
TEST_MEMORY_COMPARE(r1, n, r2, n);
|
2022-11-22 19:15:55 +01:00
|
|
|
|
2022-11-23 17:11:04 +01:00
|
|
|
/* Test non-word-aligned buffers, for all combinations of alignedness */
|
2023-01-11 14:50:10 +01:00
|
|
|
for (int i = 0; i < 7; i++) {
|
2022-11-23 17:11:04 +01:00
|
|
|
int r_off = i & 1, a_off = (i & 2) >> 1, b_off = (i & 4) >> 2;
|
2023-01-11 14:50:10 +01:00
|
|
|
fill_arrays(a, b, r1, r2, n + 1);
|
2022-11-23 17:11:04 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (size_t j = 0; j < n; j++) {
|
2022-11-23 20:38:13 +01:00
|
|
|
r1[j + r_off] = a[j + a_off] ^ b[j + b_off];
|
2022-11-23 17:11:04 +01:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_xor(r2 + r_off, a + a_off, b + b_off, n);
|
2023-07-21 12:40:20 +02:00
|
|
|
TEST_MEMORY_COMPARE(r1 + r_off, n, r2 + r_off, n);
|
2022-11-22 19:15:55 +01:00
|
|
|
}
|
2022-11-22 18:31:50 +01:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_free(a);
|
|
|
|
mbedtls_free(b);
|
|
|
|
mbedtls_free(r1);
|
|
|
|
mbedtls_free(r2);
|
2022-11-22 18:31:50 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|