cipher_hash_test.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /* test the ciphers and hashes using their built-in self-tests */
  2. #include <tomcrypt_test.h>
  3. int cipher_hash_test(void)
  4. {
  5. int x;
  6. unsigned char buf[4096];
  7. unsigned long n, one;
  8. prng_state nprng;
  9. /* test ciphers */
  10. for (x = 0; cipher_descriptor[x].name != NULL; x++) {
  11. DOX(cipher_descriptor[x].test(), cipher_descriptor[x].name);
  12. }
  13. /* stream ciphers */
  14. #ifdef LTC_CHACHA
  15. DO(chacha_test());
  16. #endif
  17. #ifdef LTC_RC4_STREAM
  18. DO(rc4_stream_test());
  19. #endif
  20. #ifdef LTC_SOBER128_STREAM
  21. DO(sober128_stream_test());
  22. #endif
  23. /* test hashes */
  24. for (x = 0; hash_descriptor[x].name != NULL; x++) {
  25. DOX(hash_descriptor[x].test(), hash_descriptor[x].name);
  26. }
  27. /* SHAKE128 + SHAKE256 tests are a bit special */
  28. DOX(sha3_shake_test(), "sha3_shake");
  29. /* test prngs (test, import/export */
  30. for (x = 0; prng_descriptor[x].name != NULL; x++) {
  31. DOX(prng_descriptor[x].test(), prng_descriptor[x].name);
  32. DOX(prng_descriptor[x].start(&nprng), prng_descriptor[x].name);
  33. DOX(prng_descriptor[x].add_entropy((unsigned char *)"helloworld12", 12, &nprng), prng_descriptor[x].name);
  34. DOX(prng_descriptor[x].ready(&nprng), prng_descriptor[x].name);
  35. n = sizeof(buf);
  36. if (strcmp(prng_descriptor[x].name, "sprng")) {
  37. one = 1;
  38. if (prng_descriptor[x].pexport(buf, &one, &nprng) != CRYPT_BUFFER_OVERFLOW) {
  39. fprintf(stderr, "Error testing pexport with a short buffer (%s)\n", prng_descriptor[x].name);
  40. return CRYPT_ERROR;
  41. }
  42. }
  43. DOX(prng_descriptor[x].pexport(buf, &n, &nprng), prng_descriptor[x].name);
  44. prng_descriptor[x].done(&nprng);
  45. DOX(prng_descriptor[x].pimport(buf, n, &nprng), prng_descriptor[x].name);
  46. DOX(prng_descriptor[x].pimport(buf, 4096, &nprng), prng_descriptor[x].name); /* try to import larger data */
  47. DOX(prng_descriptor[x].ready(&nprng), prng_descriptor[x].name);
  48. if (prng_descriptor[x].read(buf, 100, &nprng) != 100) {
  49. fprintf(stderr, "Error reading from imported PRNG (%s)!\n", prng_descriptor[x].name);
  50. return CRYPT_ERROR;
  51. }
  52. prng_descriptor[x].done(&nprng);
  53. }
  54. return 0;
  55. }
  56. /* $Source$ */
  57. /* $Revision$ */
  58. /* $Date$ */