base16_test.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /* LibTomCrypt, modular cryptographic library -- Tom St Denis */
  2. /* SPDX-License-Identifier: Unlicense */
  3. #include <tomcrypt_test.h>
  4. #ifdef LTC_BASE16
  5. int base16_test(void)
  6. {
  7. unsigned char in[100], tmp[100];
  8. char out[201];
  9. unsigned char testin[] = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF };
  10. const char *testout[2] = {
  11. "0123456789abcdef",
  12. "0123456789ABCDEF",
  13. };
  14. const char *failing_decode = "test";
  15. unsigned long x, l1, l2;
  16. int idx;
  17. for (idx = 0; idx < 2; idx++) {
  18. for (x = 0; x < 100; x++) {
  19. ENSURE(yarrow_read(in, x, &yarrow_prng) == x);
  20. l1 = sizeof(out);
  21. DO(base16_encode(in, x, out, &l1, idx));
  22. l2 = sizeof(tmp);
  23. DO(base16_decode(out, l1, tmp, &l2));
  24. COMPARE_TESTVECTOR(tmp, l2, in, x, "random base16", idx * 100 + x);
  25. }
  26. }
  27. for (idx = 0; idx < 2; idx++) {
  28. l1 = sizeof(out);
  29. DO(base16_encode(testin, sizeof(testin), out, &l1, idx));
  30. COMPARE_TESTVECTOR(out, XSTRLEN(out), testout[idx], XSTRLEN(testout[idx]), "testout base16", idx);
  31. l2 = sizeof(tmp);
  32. DO(base16_decode(out, l1, tmp, &l2));
  33. COMPARE_TESTVECTOR(tmp, l2, testin, sizeof(testin), "testin base16", idx);
  34. }
  35. l1 = 4;
  36. l2 = sizeof(tmp);
  37. SHOULD_FAIL(base16_decode(failing_decode, l1, tmp, &l2));
  38. return CRYPT_OK;
  39. }
  40. #endif