Skip to content

Commit 3d98986

Browse files
[CX]: Move unit-test from source
1 parent f917dec commit 3d98986

File tree

2 files changed

+8
-73
lines changed

2 files changed

+8
-73
lines changed

include/decorators.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,9 @@
6666
#ifndef DEPRECATED
6767
#define DEPRECATED __attribute__((deprecated))
6868
#endif
69+
70+
#ifndef UNIT_TESTING
71+
#define STATIC static
72+
#else
73+
#define STATIC
74+
#endif

lib_cxng/src/cx_chacha.c

Lines changed: 2 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
static const uint8_t constants[16] = "expand 32-byte k";
3737

3838
/* Chacha-20 quarter round function */
39-
static void cx_chacha_quarter_round(uint32_t *a, uint32_t *b, uint32_t *c, uint32_t *d)
39+
STATIC void cx_chacha_quarter_round(uint32_t *a, uint32_t *b, uint32_t *c, uint32_t *d)
4040
{
4141
*a = PLUS(*a, *b);
4242
*d = ROTATE(XOR(*d, *a), 16);
@@ -69,7 +69,7 @@ static void cx_chacha_process_block(cx_chacha_context_t *ctx)
6969
cx_chacha_quarter_round(&x[3], &x[4], &x[9], &x[14]);
7070
}
7171
for (i = 0; i < CHACHA_STATE_ARRAY_SIZE; i++) {
72-
x[i] = PLUS(x[i], ctx->state[i]);
72+
x[i] = x[i] + ctx->state[i];
7373
}
7474
}
7575

@@ -177,74 +177,3 @@ cx_err_t cx_chacha_cipher(uint32_t nrounds,
177177
return error;
178178
}
179179
#endif // HAVE_CHACHA
180-
181-
#ifdef UNIT_TESTING
182-
#include <stdarg.h>
183-
#include <cmocka.h>
184-
185-
static void test_chacha_round(void **state)
186-
{
187-
(void) state;
188-
189-
uint32_t state_array[16] = {0x61707865,
190-
0x3320646e,
191-
0x79622d32,
192-
0x6b206574,
193-
0x03020100,
194-
0x07060504,
195-
0x0b0a0908,
196-
0x0f0e0d0c,
197-
0x13121110,
198-
0x17161514,
199-
0x1b1a1918,
200-
0x1f1e1d1c,
201-
0x00000001,
202-
0x09000000,
203-
0x4a000000,
204-
0x00000000};
205-
uint32_t expected_array[16] = {0x837778ab,
206-
0xe238d763,
207-
0xa67ae21e,
208-
0x5950bb2f,
209-
0xc4f2d0c7,
210-
0xfc62bb2f,
211-
0x8fa018fc,
212-
0x3f5ec7b7,
213-
0x335271c2,
214-
0xf29489f3,
215-
0xeabda8fc,
216-
0x82e46ebd,
217-
0xd19c12b4,
218-
0xb04e16de,
219-
0x9e83d0cb,
220-
0x4e3c50a2};
221-
for (int i = 0; i < 10; i++) {
222-
cx_chacha_quarter_round(
223-
&state_array[0], &state_array[4], &state_array[8], &state_array[12]);
224-
cx_chacha_quarter_round(
225-
&state_array[1], &state_array[5], &state_array[9], &state_array[13]);
226-
cx_chacha_quarter_round(
227-
&state_array[2], &state_array[6], &state_array[10], &state_array[14]);
228-
cx_chacha_quarter_round(
229-
&state_array[3], &state_array[7], &state_array[11], &state_array[15]);
230-
231-
cx_chacha_quarter_round(
232-
&state_array[0], &state_array[5], &state_array[10], &state_array[15]);
233-
cx_chacha_quarter_round(
234-
&state_array[1], &state_array[6], &state_array[11], &state_array[12]);
235-
cx_chacha_quarter_round(
236-
&state_array[2], &state_array[7], &state_array[8], &state_array[13]);
237-
cx_chacha_quarter_round(
238-
&state_array[3], &state_array[4], &state_array[9], &state_array[14]);
239-
}
240-
241-
assert_memory_equal(state_array, expected_array, sizeof(state_array));
242-
}
243-
244-
int main(void)
245-
{
246-
const struct CMUnitTest tests[] = {cmocka_unit_test(test_chacha_round)};
247-
248-
return cmocka_run_group_tests(tests, NULL, NULL);
249-
}
250-
#endif // UNIT_TESTING

0 commit comments

Comments
 (0)