dsa_test.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #include "test.h"
  2. #ifdef MDSA
  3. int dsa_test(void)
  4. {
  5. unsigned char msg[16], out[1024], out2[1024];
  6. unsigned long x;
  7. int stat1, stat2;
  8. dsa_key key, key2;
  9. /* make a random key */
  10. DO(dsa_make_key(&test_yarrow, find_prng("yarrow"), 20, 128, &key));
  11. /* verify it */
  12. DO(dsa_verify_key(&key, &stat1));
  13. if (stat1 == 0) { printf("dsa_verify_key "); return 1; }
  14. /* sign the message */
  15. x = sizeof(out);
  16. DO(dsa_sign_hash(msg, sizeof(msg), out, &x, &test_yarrow, find_prng("yarrow"), &key));
  17. /* verify it once */
  18. DO(dsa_verify_hash(out, x, msg, sizeof(msg), &stat1, &key));
  19. /* Modify and verify again */
  20. msg[0] ^= 1;
  21. DO(dsa_verify_hash(out, x, msg, sizeof(msg), &stat2, &key));
  22. msg[0] ^= 1;
  23. if (!(stat1 == 1 && stat2 == 0)) { printf("dsa_verify %d %d", stat1, stat2); return 1; }
  24. /* test exporting it */
  25. x = sizeof(out2);
  26. DO(dsa_export(out2, &x, PK_PRIVATE, &key));
  27. DO(dsa_import(out2, x, &key2));
  28. /* verify a signature with it */
  29. DO(dsa_verify_hash(out, x, msg, sizeof(msg), &stat1, &key2));
  30. if (stat1 == 0) { printf("dsa_verify (import private) %d ", stat1); return 1; }
  31. dsa_free(&key2);
  32. /* export as public now */
  33. x = sizeof(out2);
  34. DO(dsa_export(out2, &x, PK_PUBLIC, &key));
  35. DO(dsa_import(out2, x, &key2));
  36. /* verify a signature with it */
  37. DO(dsa_verify_hash(out, x, msg, sizeof(msg), &stat1, &key2));
  38. if (stat1 == 0) { printf("dsa_verify (import public) %d ", stat1); return 1; }
  39. dsa_free(&key2);
  40. dsa_free(&key);
  41. return 0;
  42. }
  43. #else
  44. int dsa_test(void)
  45. {
  46. printf("NOP");
  47. return 0;
  48. }
  49. #endif