base32_test.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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_BASE32
  11. int base32_test(void)
  12. {
  13. unsigned char in[100], tmp[100];
  14. char out[160];
  15. unsigned char testin[] = { 0x61,0xc2,0xcb,0xbc,0x5e,0x6d,0x2a,0x7a,0x1a,0x19,0x1a,0xae,0xc9,0x02,0xd4,0xbf,0x7d };
  16. const int testid[4] = {
  17. BASE32_RFC4648,
  18. BASE32_BASE32HEX,
  19. BASE32_ZBASE32,
  20. BASE32_CROCKFORD
  21. };
  22. const char *testout[4] = {
  23. "MHBMXPC6NUVHUGQZDKXMSAWUX56Q",
  24. "C71CNF2UDKL7K6GP3ANCI0MKNTUG",
  25. "c8bczxn6pwi8wgo3dkzc1yswz76o",
  26. "C71CQF2YDMN7M6GS3AQCJ0PMQXYG"
  27. };
  28. unsigned long x, l1, l2;
  29. int idx;
  30. for (idx = 0; idx < 4; idx++) {
  31. for (x = 0; x < 100; x++) {
  32. yarrow_read(in, x, &yarrow_prng);
  33. l1 = sizeof(out);
  34. DO(base32_encode(in, x, out, &l1, testid[idx]));
  35. l2 = sizeof(tmp);
  36. DO(base32_decode(out, l1, tmp, &l2, testid[idx]));
  37. DO(do_compare_testvector(tmp, l2, in, x, "random base32", idx * 100 + x));
  38. }
  39. }
  40. for (idx = 0; idx < 4; idx++) {
  41. l1 = sizeof(out);
  42. DO(base32_encode(testin, sizeof(testin), out, &l1, testid[idx]));
  43. DO(do_compare_testvector(out, l1, testout[idx], strlen(testout[idx]), "testout base32", idx));
  44. l2 = sizeof(tmp);
  45. DO(base32_decode(out, l1, tmp, &l2, testid[idx]));
  46. DO(do_compare_testvector(tmp, l2, testin, sizeof(testin), "testin base32", idx));
  47. }
  48. return CRYPT_OK;
  49. }
  50. #endif
  51. /* ref: $Format:%D$ */
  52. /* git commit: $Format:%H$ */
  53. /* commit time: $Format:%ai$ */