diff --git a/src/Makefile b/src/Makefile index 30f657e..8bea853 100644 --- a/src/Makefile +++ b/src/Makefile @@ -37,8 +37,8 @@ endif LIBS += -L. # objects -PQC_OBJS = rand.o poly.o mem.o -PQC_HEADERS = err.h rand.h poly.h context.h +PQC_OBJS = rand.o poly.o mem.o ntru_decrypt.o +PQC_HEADERS = err.h rand.h poly.h context.h ntru_decrypt.h # CUNIT_OBJS = cunit.o # includes diff --git a/src/ntru_decrypt.c b/src/ntru_decrypt.c index 3a46676..5594f3e 100644 --- a/src/ntru_decrypt.c +++ b/src/ntru_decrypt.c @@ -19,18 +19,64 @@ * MA 02110-1301 USA */ +#include "ntru_decrypt.h" + +/* + * Legend + * + * N : maximal degree of the polynom + * q : "is given" (... mod q) + * p : "is given" (... mod p) + * f : private key + * Fp: inverse of "modulo p" + * e : encrypted message + * a : result of first multiplication (StarMultiply(f, e, a, N, q)) + * d : result of second multiplication (StarMultiply(a, Fp , d, N, p)), decrypted message + * */ + // Require: N , q, p, secret key f , inverse polynomial Fp , and encrypted message e. -int ntru_decrypt(char *encr_msg, pb_poly *private_key, ntru_context *context, char ** decr_msg){ +pb_poly* ntru_decrypt(pb_poly *encr_msg, pb_poly *private_key, pb_poly *Fp, ntru_context *context, char ** decr_msg){ + + unsigned int q = context->q; + unsigned int p = context->p; + unsigned int N = context->N; + unsigned int i; + // StarMultiply(f, e, a, N, q) - for(int i = 0, i < N, i++){ - if(a[i] < 0 ) { - a[i] = a[i] + q; // Make all coefficients positive + pb_poly *a = build_polynom(NULL, N, context); + pb_starmultiply(private_key, encr_msg, a, context, q); + + mp_int mp_q; + mp_int mp_qdiv2; + mp_int zero; + + init_integer(&mp_q); + init_integer(&mp_qdiv2); + init_integer(&zero); + + MP_SET_INT(&mp_q, q); + mp_div_2(&mp_q, &mp_qdiv2); + mp_zero(&zero); + + for(i = 0; i < N; i++){ + if(mp_cmp(&(a->terms[i]),&zero) == MP_LT) { // Make all coefficients positive + //a->terms[i] = a->terms[i] + q; + mp_add((&a->terms[i]),&mp_q,(&a->terms[i])); } - if(a[i] > q/2) { - a[i] = a[i] - q // Shift coefficients of a into range (−q/2, q/2) + if(mp_cmp(&(a->terms[i]), &mp_qdiv2) == MP_GT) { // Shift coefficients of a into range (−q/2, q/2) + //a->terms[i] = a->terms[i] - mp_q; + mp_sub((&a->terms[i]),&mp_q,(&a->terms[i])); } } + + printf("%s\np:%d", "Nach dem StarMultiply: ", p); + draw_polynom(a); + + pb_poly *d = build_polynom(NULL, N, context); + // StarMultiply(a, Fp , d, N, p) + pb_starmultiply(a, Fp, d, context, p); + // {Decode returns the decrypted message, d, through the argument list.} - return 0; + return d; } diff --git a/src/ntru_decrypt.h b/src/ntru_decrypt.h index 9da81cc..5f44005 100644 --- a/src/ntru_decrypt.h +++ b/src/ntru_decrypt.h @@ -25,6 +25,10 @@ #include "poly.h" #include "context.h" -int ntru_decrypt(char *encr_msg, pb_poly *private_key, ntru_context *context, char ** decr_msg); +pb_poly* ntru_decrypt(pb_poly*, + pb_poly*, + pb_poly*, + ntru_context*, + char**); #endif /* NTRU_DECRYPT */