1 #include "os.h"
2 #include <mp.h>
3 #include <libsec.h>
4
5 EGsig*
egsign(EGpriv * priv,mpint * m)6 egsign(EGpriv *priv, mpint *m)
7 {
8 EGpub *pub = &priv->pub;
9 EGsig *sig;
10 mpint *pm1, *k, *kinv, *r, *s;
11 mpint *p = pub->p, *alpha = pub->alpha;
12 int plen = mpsignif(p);
13
14 pm1 = mpnew(0);
15 kinv = mpnew(0);
16 r = mpnew(0);
17 s = mpnew(0);
18 k = mpnew(0);
19 mpsub(p, mpone, pm1);
20 while(1){
21 mprand(plen, genrandom, k);
22 if((mpcmp(mpone, k) > 0) || (mpcmp(k, pm1) >= 0))
23 continue;
24 mpextendedgcd(k, pm1, r, kinv, s);
25 if(mpcmp(r, mpone) != 0)
26 continue;
27 break;
28 }
29 mpmod(kinv, pm1, kinv); /* make kinv positive */
30 mpexp(alpha, k, p, r);
31 mpmul(priv->secret, r, s);
32 mpmod(s, pm1, s);
33 mpsub(m, s, s);
34 mpmul(kinv, s, s);
35 mpmod(s, pm1, s);
36 sig = egsigalloc();
37 sig->r = r;
38 sig->s = s;
39 mpfree(pm1);
40 mpfree(k);
41 mpfree(kinv);
42 return sig;
43 }
44