libtomcrypt/tests/base16_test.c

49 lines
1.3 KiB
C
Raw Normal View History

2020-06-17 08:15:27 +00:00
/* LibTomCrypt, modular cryptographic library -- Tom St Denis */
/* SPDX-License-Identifier: Unlicense */
2018-03-22 14:44:01 +00:00
#include <tomcrypt_test.h>
#ifdef LTC_BASE16
int base16_test(void)
{
unsigned char in[100], tmp[100];
char out[201];
unsigned char testin[] = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF };
const char *testout[2] = {
"0123456789abcdef",
"0123456789ABCDEF",
};
2018-03-26 14:23:11 +00:00
const char *failing_decode = "test";
2018-03-22 14:44:01 +00:00
unsigned long x, l1, l2;
int idx;
for (idx = 0; idx < 2; idx++) {
for (x = 0; x < 100; x++) {
ENSURE(yarrow_read(in, x, &yarrow_prng) == x);
2018-03-22 14:44:01 +00:00
l1 = sizeof(out);
DO(base16_encode(in, x, out, &l1, idx));
l2 = sizeof(tmp);
2018-03-26 13:42:58 +00:00
DO(base16_decode(out, l1, tmp, &l2));
2018-03-22 14:44:01 +00:00
DO(do_compare_testvector(tmp, l2, in, x, "random base16", idx * 100 + x));
}
}
for (idx = 0; idx < 2; idx++) {
l1 = sizeof(out);
DO(base16_encode(testin, sizeof(testin), out, &l1, idx));
2019-10-03 13:18:25 +00:00
DO(do_compare_testvector(out, XSTRLEN(out), testout[idx], XSTRLEN(testout[idx]), "testout base16", idx));
2018-03-22 14:44:01 +00:00
l2 = sizeof(tmp);
2018-03-26 13:42:58 +00:00
DO(base16_decode(out, l1, tmp, &l2));
2018-03-22 14:44:01 +00:00
DO(do_compare_testvector(tmp, l2, testin, sizeof(testin), "testin base16", idx));
}
2018-03-26 14:23:11 +00:00
l1 = 4;
l2 = sizeof(tmp);
2019-10-14 10:42:59 +00:00
SHOULD_FAIL(base16_decode(failing_decode, l1, tmp, &l2));
2018-03-26 14:23:11 +00:00
2018-03-22 14:44:01 +00:00
return CRYPT_OK;
}
#endif