2020-06-17 10:15:27 +02:00
|
|
|
/* LibTomCrypt, modular cryptographic library -- Tom St Denis */
|
|
|
|
|
/* SPDX-License-Identifier: Unlicense */
|
2018-03-22 15:44:01 +01: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 16:23:11 +02:00
|
|
|
const char *failing_decode = "test";
|
2018-03-22 15:44:01 +01:00
|
|
|
unsigned long x, l1, l2;
|
|
|
|
|
int idx;
|
|
|
|
|
|
|
|
|
|
for (idx = 0; idx < 2; idx++) {
|
|
|
|
|
for (x = 0; x < 100; x++) {
|
2022-03-17 13:36:08 +01:00
|
|
|
ENSURE(yarrow_read(in, x, &yarrow_prng) == x);
|
2018-03-22 15:44:01 +01:00
|
|
|
l1 = sizeof(out);
|
|
|
|
|
DO(base16_encode(in, x, out, &l1, idx));
|
|
|
|
|
l2 = sizeof(tmp);
|
2018-03-26 15:42:58 +02:00
|
|
|
DO(base16_decode(out, l1, tmp, &l2));
|
2025-06-16 14:09:33 +02:00
|
|
|
COMPARE_TESTVECTOR(tmp, l2, in, x, "random base16", idx * 100 + x);
|
2018-03-22 15:44:01 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (idx = 0; idx < 2; idx++) {
|
|
|
|
|
l1 = sizeof(out);
|
|
|
|
|
DO(base16_encode(testin, sizeof(testin), out, &l1, idx));
|
2025-06-16 14:09:33 +02:00
|
|
|
COMPARE_TESTVECTOR(out, XSTRLEN(out), testout[idx], XSTRLEN(testout[idx]), "testout base16", idx);
|
2018-03-22 15:44:01 +01:00
|
|
|
l2 = sizeof(tmp);
|
2018-03-26 15:42:58 +02:00
|
|
|
DO(base16_decode(out, l1, tmp, &l2));
|
2025-06-16 14:09:33 +02:00
|
|
|
COMPARE_TESTVECTOR(tmp, l2, testin, sizeof(testin), "testin base16", idx);
|
2018-03-22 15:44:01 +01:00
|
|
|
}
|
|
|
|
|
|
2018-03-26 16:23:11 +02:00
|
|
|
l1 = 4;
|
|
|
|
|
l2 = sizeof(tmp);
|
2019-10-14 12:42:59 +02:00
|
|
|
SHOULD_FAIL(base16_decode(failing_decode, l1, tmp, &l2));
|
2018-03-26 16:23:11 +02:00
|
|
|
|
2018-03-22 15:44:01 +01:00
|
|
|
return CRYPT_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif
|