dsa_test.c 1.5 KB

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