base32_test.c 1.9 KB

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