base16_test.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /* LibTomCrypt, modular cryptographic library -- Tom St Denis
  2. *
  3. * LibTomCrypt is a library that provides various cryptographic
  4. * algorithms in a highly modular and flexible manner.
  5. *
  6. * The library is free for all purposes without any express
  7. * guarantee it works.
  8. */
  9. #include <tomcrypt_test.h>
  10. #ifdef LTC_BASE16
  11. int base16_test(void)
  12. {
  13. unsigned char in[100], tmp[100];
  14. char out[201];
  15. unsigned char testin[] = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF };
  16. const char *testout[2] = {
  17. "0123456789abcdef",
  18. "0123456789ABCDEF",
  19. };
  20. const char *failing_decode = "test";
  21. unsigned long x, l1, l2;
  22. int idx;
  23. for (idx = 0; idx < 2; idx++) {
  24. for (x = 0; x < 100; x++) {
  25. yarrow_read(in, x, &yarrow_prng);
  26. l1 = sizeof(out);
  27. DO(base16_encode(in, x, out, &l1, idx));
  28. l2 = sizeof(tmp);
  29. DO(base16_decode(out, l1, tmp, &l2));
  30. DO(do_compare_testvector(tmp, l2, in, x, "random base16", idx * 100 + x));
  31. }
  32. }
  33. for (idx = 0; idx < 2; idx++) {
  34. l1 = sizeof(out);
  35. DO(base16_encode(testin, sizeof(testin), out, &l1, idx));
  36. DO(do_compare_testvector(out, strlen(out), testout[idx], strlen(testout[idx]), "testout base16", idx));
  37. l2 = sizeof(tmp);
  38. DO(base16_decode(out, l1, tmp, &l2));
  39. DO(do_compare_testvector(tmp, l2, testin, sizeof(testin), "testin base16", idx));
  40. }
  41. l1 = 4;
  42. l2 = sizeof(tmp);
  43. DO(base16_decode(failing_decode, l1, tmp, &l2) == CRYPT_OK ? CRYPT_FAIL_TESTVECTOR : CRYPT_OK);
  44. return CRYPT_OK;
  45. }
  46. #endif
  47. /* ref: $Format:%D$ */
  48. /* git commit: $Format:%H$ */
  49. /* commit time: $Format:%ai$ */