base32_test.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /* LibTomCrypt, modular cryptographic library -- Tom St Denis */
  2. /* SPDX-License-Identifier: Unlicense */
  3. #include <tomcrypt_test.h>
  4. #ifdef LTC_BASE32
  5. int base32_test(void)
  6. {
  7. unsigned char in[100], tmp[100];
  8. char out[160];
  9. unsigned char testin[] = { 0x61,0xc2,0xcb,0xbc,0x5e,0x6d,0x2a,0x7a,0x1a,0x19,0x1a,0xae,0xc9,0x02,0xd4,0xbf,0x7d };
  10. const int testid[4] = {
  11. BASE32_RFC4648,
  12. BASE32_BASE32HEX,
  13. BASE32_ZBASE32,
  14. BASE32_CROCKFORD
  15. };
  16. const char *testout[4] = {
  17. "MHBMXPC6NUVHUGQZDKXMSAWUX56Q",
  18. "C71CNF2UDKL7K6GP3ANCI0MKNTUG",
  19. "c8bczxn6pwi8wgo3dkzc1yswz76o",
  20. "C71CQF2YDMN7M6GS3AQCJ0PMQXYG"
  21. };
  22. unsigned long x, l1, l2;
  23. int idx;
  24. for (idx = 0; idx < 4; idx++) {
  25. for (x = 0; x < 100; x++) {
  26. ENSURE(yarrow_read(in, x, &yarrow_prng) == x);
  27. l1 = sizeof(out);
  28. DO(base32_encode(in, x, out, &l1, testid[idx]));
  29. l2 = sizeof(tmp);
  30. DO(base32_decode(out, l1, tmp, &l2, testid[idx]));
  31. COMPARE_TESTVECTOR(tmp, l2, in, x, "random base32", idx * 100 + x);
  32. }
  33. }
  34. for (idx = 0; idx < 4; idx++) {
  35. l1 = sizeof(out);
  36. DO(base32_encode(testin, sizeof(testin), out, &l1, testid[idx]));
  37. COMPARE_TESTVECTOR(out, l1, testout[idx], XSTRLEN(testout[idx]), "testout base32", idx);
  38. l2 = sizeof(tmp);
  39. DO(base32_decode(out, l1, tmp, &l2, testid[idx]));
  40. COMPARE_TESTVECTOR(tmp, l2, testin, sizeof(testin), "testin base32", idx);
  41. }
  42. return CRYPT_OK;
  43. }
  44. #endif