From 37da2899f40661e3e9631e497da8dc59b971cbd0 Mon Sep 17 00:00:00 2001 From: "Charles.Forsyth" Date: Fri, 22 Dec 2006 17:07:39 +0000 Subject: 20060303a --- libsec/port/rsadecrypt.c | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 libsec/port/rsadecrypt.c (limited to 'libsec/port/rsadecrypt.c') diff --git a/libsec/port/rsadecrypt.c b/libsec/port/rsadecrypt.c new file mode 100644 index 00000000..1e937bec --- /dev/null +++ b/libsec/port/rsadecrypt.c @@ -0,0 +1,37 @@ +#include "os.h" +#include +#include + +// decrypt rsa using garner's algorithm for the chinese remainder theorem +// seminumerical algorithms, knuth, pp 253-254 +// applied cryptography, menezes et al, pg 612 +mpint* +rsadecrypt(RSApriv *rsa, mpint *in, mpint *out) +{ + mpint *v1, *v2; + + if(out == nil) + out = mpnew(0); + + // convert in to modular representation + v1 = mpnew(0); + mpmod(in, rsa->p, v1); + v2 = mpnew(0); + mpmod(in, rsa->q, v2); + + // exponentiate the modular rep + mpexp(v1, rsa->kp, rsa->p, v1); + mpexp(v2, rsa->kq, rsa->q, v2); + + // out = v1 + p*((v2-v1)*c2 mod q) + mpsub(v2, v1, v2); + mpmul(v2, rsa->c2, v2); + mpmod(v2, rsa->q, v2); + mpmul(v2, rsa->p, out); + mpadd(v1, out, out); + + mpfree(v1); + mpfree(v2); + + return out; +} -- cgit v1.2.3