summaryrefslogtreecommitdiff
path: root/libsec/port/egencrypt.c
diff options
context:
space:
mode:
authorCharles.Forsyth <devnull@localhost>2006-12-22 17:07:39 +0000
committerCharles.Forsyth <devnull@localhost>2006-12-22 17:07:39 +0000
commit37da2899f40661e3e9631e497da8dc59b971cbd0 (patch)
treecbc6d4680e347d906f5fa7fca73214418741df72 /libsec/port/egencrypt.c
parent54bc8ff236ac10b3eaa928fd6bcfc0cdb2ba46ae (diff)
20060303a
Diffstat (limited to 'libsec/port/egencrypt.c')
-rw-r--r--libsec/port/egencrypt.c38
1 files changed, 38 insertions, 0 deletions
diff --git a/libsec/port/egencrypt.c b/libsec/port/egencrypt.c
new file mode 100644
index 00000000..9b6b12c6
--- /dev/null
+++ b/libsec/port/egencrypt.c
@@ -0,0 +1,38 @@
+#include "os.h"
+#include <mp.h>
+#include <libsec.h>
+
+mpint*
+egencrypt(EGpub *pub, mpint *in, mpint *out)
+{
+ mpint *m, *k, *gamma, *delta, *pm1;
+ mpint *p = pub->p, *alpha = pub->alpha;
+ int plen = mpsignif(p);
+ int shift = ((plen+Dbits)/Dbits)*Dbits;
+ // in libcrypt version, (int)(LENGTH(pub->p)*sizeof(NumType)*CHARBITS);
+
+ if(out == nil)
+ out = mpnew(0);
+ pm1 = mpnew(0);
+ m = mpnew(0);
+ gamma = mpnew(0);
+ delta = mpnew(0);
+ mpmod(in, p, m);
+ while(1){
+ k = mprand(plen, genrandom, nil);
+ if((mpcmp(mpone, k) <= 0) && (mpcmp(k, pm1) < 0))
+ break;
+ }
+ mpexp(alpha, k, p, gamma);
+ mpexp(pub->key, k, p, delta);
+ mpmul(m, delta, delta);
+ mpmod(delta, p, delta);
+ mpleft(gamma, shift, out);
+ mpadd(delta, out, out);
+ mpfree(pm1);
+ mpfree(m);
+ mpfree(k);
+ mpfree(gamma);
+ mpfree(delta);
+ return out;
+}