dsaverify.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include "os.h"
  10. #include <mp.h>
  11. #include <libsec.h>
  12. int
  13. dsaverify(DSApub *pub, DSAsig *sig, mpint *m)
  14. {
  15. int rv = -1;
  16. mpint *u1, *u2, *v, *sinv;
  17. if(sig->r->sign < 0 || mpcmp(sig->r, pub->q) >= 0)
  18. return rv;
  19. if(sig->s->sign < 0 || mpcmp(sig->s, pub->q) >= 0)
  20. return rv;
  21. u1 = mpnew(0);
  22. u2 = mpnew(0);
  23. v = mpnew(0);
  24. sinv = mpnew(0);
  25. // find (s**-1) mod q, make sure it exists
  26. mpextendedgcd(sig->s, pub->q, u1, sinv, v);
  27. if(mpcmp(u1, mpone) != 0)
  28. goto out;
  29. // u1 = (sinv * m) mod q, u2 = (r * sinv) mod q
  30. mpmul(sinv, m, u1);
  31. mpmod(u1, pub->q, u1);
  32. mpmul(sig->r, sinv, u2);
  33. mpmod(u2, pub->q, u2);
  34. // v = (((alpha**u1)*(key**u2)) mod p) mod q
  35. mpexp(pub->alpha, u1, pub->p, sinv);
  36. mpexp(pub->key, u2, pub->p, v);
  37. mpmul(sinv, v, v);
  38. mpmod(v, pub->p, v);
  39. mpmod(v, pub->q, v);
  40. if(mpcmp(v, sig->r) == 0)
  41. rv = 0;
  42. out:
  43. mpfree(v);
  44. mpfree(u1);
  45. mpfree(u2);
  46. mpfree(sinv);
  47. return rv;
  48. }