dsa_test.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #include <tomcrypt_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(&yarrow_prng, find_prng("yarrow"), 20, 128, &key));
  11. /* verify it */
  12. DO(dsa_verify_key(&key, &stat1));
  13. if (stat1 == 0) { fprintf(stderr, "dsa_verify_key "); return 1; }
  14. /* sign the message */
  15. x = sizeof(out);
  16. DO(dsa_sign_hash(msg, sizeof(msg), out, &x, &yarrow_prng, 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)) { fprintf(stderr, "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) { fprintf(stderr, "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) { fprintf(stderr, "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. fprintf(stderr, "NOP");
  47. return 0;
  48. }
  49. #endif
  50. /* $Source$ */
  51. /* $Revision$ */
  52. /* $Date$ */