StupidFS/libcrypto/tests/test_hchacha.c

48 lines
No EOL
1.3 KiB
C

#include <stdarg.h>
#include <stddef.h>
#include <stdint.h>
#include <setjmp.h>
#include <cmocka.h>
#include "chacha.h"
/* test from https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-xchacha-03#section-2.2 */
void
hchacha(uint8_t out[HCHACHA_OUT_BYTES],
const uint8_t key[CHACHA_KEY_BYTES],
const uint8_t nonce[HCHACHA_NONCE_BYTES],
int round);
uint8_t key[CHACHA_KEY_BYTES] = {
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
};
uint8_t nonce[HCHACHA_NONCE_BYTES] = {
0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x4a, 0x00, 0x00, 0x00, 0x00, 0x31, 0x41, 0x59, 0x27
};
static void
test_hchacha20(void **state)
{
uint8_t out[HCHACHA_OUT_BYTES];
uint8_t expected[HCHACHA_OUT_BYTES] = {
0x82, 0x41, 0x3b, 0x42, 0x27, 0xb2, 0x7b, 0xfe, 0xd3, 0x0e, 0x42, 0x50, 0x8a, 0x87, 0x7d, 0x73,
0xa0, 0xf9, 0xe4, 0xd5, 0x8a, 0x74, 0xa8, 0x53, 0xc1, 0x2e, 0xc4, 0x13, 0x26, 0xd3, 0xec, 0xdc
};
hchacha(out, key, nonce, 20);
assert_memory_equal(expected, out, HCHACHA_OUT_BYTES);
}
int
main(void)
{
const struct CMUnitTest tests[] = {
cmocka_unit_test(test_hchacha20),
};
return cmocka_run_group_tests(tests, NULL, NULL);
}