egsign.c 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. EGsig*
  13. egsign(EGpriv *priv, mpint *m)
  14. {
  15. EGpub *pub = &priv->pub;
  16. EGsig *sig;
  17. mpint *pm1, *k, *kinv, *r, *s;
  18. mpint *p = pub->p, *alpha = pub->alpha;
  19. int plen = mpsignif(p);
  20. pm1 = mpnew(0);
  21. kinv = mpnew(0);
  22. r = mpnew(0);
  23. s = mpnew(0);
  24. k = mpnew(0);
  25. mpsub(p, mpone, pm1);
  26. while(1){
  27. mprand(plen, genrandom, k);
  28. if((mpcmp(mpone, k) > 0) || (mpcmp(k, pm1) >= 0))
  29. continue;
  30. mpextendedgcd(k, pm1, r, kinv, s);
  31. if(mpcmp(r, mpone) != 0)
  32. continue;
  33. break;
  34. }
  35. mpmod(kinv, pm1, kinv); // make kinv positive
  36. mpexp(alpha, k, p, r);
  37. mpmul(priv->secret, r, s);
  38. mpmod(s, pm1, s);
  39. mpsub(m, s, s);
  40. mpmul(kinv, s, s);
  41. mpmod(s, pm1, s);
  42. sig = egsigalloc();
  43. sig->r = r;
  44. sig->s = s;
  45. mpfree(pm1);
  46. mpfree(k);
  47. mpfree(kinv);
  48. return sig;
  49. }