dsa_test.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #include <tomcrypt_test.h>
  2. #ifdef LTC_MDSA
  3. int dsa_test(void)
  4. {
  5. unsigned char msg[16], out[1024], out2[1024];
  6. unsigned long x, y;
  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. /* encrypt a message */
  15. for (x = 0; x < 16; x++) { msg[x] = x; }
  16. x = sizeof(out);
  17. DO(dsa_encrypt_key(msg, 16, out, &x, &yarrow_prng, find_prng("yarrow"), find_hash("sha1"), &key));
  18. /* decrypt */
  19. y = sizeof(out2);
  20. DO(dsa_decrypt_key(out, x, out2, &y, &key));
  21. if (y != 16 || memcmp(out2, msg, 16)) {
  22. fprintf(stderr, "dsa_decrypt failed, y == %lu\n", y);
  23. return 1;
  24. }
  25. /* sign the message */
  26. x = sizeof(out);
  27. DO(dsa_sign_hash(msg, sizeof(msg), out, &x, &yarrow_prng, find_prng("yarrow"), &key));
  28. /* verify it once */
  29. DO(dsa_verify_hash(out, x, msg, sizeof(msg), &stat1, &key));
  30. /* Modify and verify again */
  31. msg[0] ^= 1;
  32. DO(dsa_verify_hash(out, x, msg, sizeof(msg), &stat2, &key));
  33. msg[0] ^= 1;
  34. if (!(stat1 == 1 && stat2 == 0)) { fprintf(stderr, "dsa_verify %d %d", stat1, stat2); return 1; }
  35. /* test exporting it */
  36. x = sizeof(out2);
  37. DO(dsa_export(out2, &x, PK_PRIVATE, &key));
  38. DO(dsa_import(out2, x, &key2));
  39. /* verify a signature with it */
  40. DO(dsa_verify_hash(out, x, msg, sizeof(msg), &stat1, &key2));
  41. if (stat1 == 0) { fprintf(stderr, "dsa_verify (import private) %d ", stat1); return 1; }
  42. dsa_free(&key2);
  43. /* export as public now */
  44. x = sizeof(out2);
  45. DO(dsa_export(out2, &x, PK_PUBLIC, &key));
  46. DO(dsa_import(out2, x, &key2));
  47. /* verify a signature with it */
  48. DO(dsa_verify_hash(out, x, msg, sizeof(msg), &stat1, &key2));
  49. if (stat1 == 0) { fprintf(stderr, "dsa_verify (import public) %d ", stat1); return 1; }
  50. dsa_free(&key2);
  51. dsa_free(&key);
  52. return 0;
  53. }
  54. #else
  55. int dsa_test(void)
  56. {
  57. fprintf(stderr, "NOP");
  58. return 0;
  59. }
  60. #endif
  61. /* $Source$ */
  62. /* $Revision$ */
  63. /* $Date$ */