From 0fed44e2987dde1f2784635c5e5942d5405b13d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20Pr=C3=BC=C3=9Fner?= Date: Tue, 8 Apr 2014 11:15:59 +0200 Subject: [PATCH 01/23] Abc DEEFFFFFFFFFFFFFFF --- blah.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 blah.txt diff --git a/blah.txt b/blah.txt new file mode 100644 index 0000000..99eb216 --- /dev/null +++ b/blah.txt @@ -0,0 +1 @@ +Penis \ No newline at end of file From 1d78b707cf6650791a4e403b7efb5747d27ccf76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20Pr=C3=BC=C3=9Fner?= Date: Wed, 23 Apr 2014 12:35:40 +0200 Subject: [PATCH 02/23] Deleted "blah.txt" (test-file) --- .project | 11 +++++++++++ blah.txt | 1 - 2 files changed, 11 insertions(+), 1 deletion(-) create mode 100644 .project delete mode 100644 blah.txt diff --git a/.project b/.project new file mode 100644 index 0000000..a2785b2 --- /dev/null +++ b/.project @@ -0,0 +1,11 @@ + + + Semesterprojekt2014 + + + + + + + + diff --git a/blah.txt b/blah.txt deleted file mode 100644 index 99eb216..0000000 --- a/blah.txt +++ /dev/null @@ -1 +0,0 @@ -Penis \ No newline at end of file From ac004e5cfe93e00a55852e9007704adf2b401183 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20Pr=C3=BC=C3=9Fner?= Date: Wed, 23 Apr 2014 13:19:37 +0200 Subject: [PATCH 03/23] Added the following files: ntru_decrypt.(c|h) --- src/ntru_decrypt.c | 21 +++++++++++++++++++++ src/ntru_decrypt.h | 27 +++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 src/ntru_decrypt.c create mode 100644 src/ntru_decrypt.h diff --git a/src/ntru_decrypt.c b/src/ntru_decrypt.c new file mode 100644 index 0000000..7797a33 --- /dev/null +++ b/src/ntru_decrypt.c @@ -0,0 +1,21 @@ +/* + * Copyright (C) 2014 FH Bielefeld + * + * This file is part of a FH Bielefeld project. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301 USA + */ + diff --git a/src/ntru_decrypt.h b/src/ntru_decrypt.h new file mode 100644 index 0000000..1170d0f --- /dev/null +++ b/src/ntru_decrypt.h @@ -0,0 +1,27 @@ +/* + * Copyright (C) 2014 FH Bielefeld + * + * This file is part of a FH Bielefeld project. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301 USA + */ + +#ifndef NTRU_DECRYPT_H +#define NTRU_DECRYPT_H + +/*int ntru_decrypt(char* encr_msg, pb_poly private_key);*/ + +#endif /* NTRU_DECRYPT */ From 00513d67018f8efcd6e78ec69d6dfcc58373c25b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20Pr=C3=BC=C3=9Fner?= Date: Wed, 23 Apr 2014 13:28:18 +0200 Subject: [PATCH 04/23] modified .gitignore --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index d4e2f23..4905720 100644 --- a/.gitignore +++ b/.gitignore @@ -19,3 +19,5 @@ latex/ # currently masked src/main.c src/main + +.settings/org.eclipse* \ No newline at end of file From 74ddfe65e72f022d7ebcc194d5eccf790b22fea8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20Pr=C3=BC=C3=9Fner?= Date: Wed, 23 Apr 2014 13:45:03 +0200 Subject: [PATCH 05/23] remodified gitignore and defined function "ntru_decrypt(..)" --- .gitignore | 2 +- src/ntru_decrypt.h | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 4905720..566858b 100644 --- a/.gitignore +++ b/.gitignore @@ -20,4 +20,4 @@ latex/ src/main.c src/main -.settings/org.eclipse* \ No newline at end of file +./.settings/org.eclipse* \ No newline at end of file diff --git a/src/ntru_decrypt.h b/src/ntru_decrypt.h index 1170d0f..9da81cc 100644 --- a/src/ntru_decrypt.h +++ b/src/ntru_decrypt.h @@ -22,6 +22,9 @@ #ifndef NTRU_DECRYPT_H #define NTRU_DECRYPT_H -/*int ntru_decrypt(char* encr_msg, pb_poly private_key);*/ +#include "poly.h" +#include "context.h" + +int ntru_decrypt(char *encr_msg, pb_poly *private_key, ntru_context *context, char ** decr_msg); #endif /* NTRU_DECRYPT */ From 98bdd670d844b72e04b25004e43ac9d98ba32c2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20Pr=C3=BC=C3=9Fner?= Date: Wed, 23 Apr 2014 13:58:57 +0200 Subject: [PATCH 06/23] added ntru_decrypt function to ntru_decrypt.c --- src/ntru_decrypt.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/ntru_decrypt.c b/src/ntru_decrypt.c index 7797a33..d385959 100644 --- a/src/ntru_decrypt.c +++ b/src/ntru_decrypt.c @@ -19,3 +19,6 @@ * MA 02110-1301 USA */ +int ntru_decrypt(char *encr_msg, pb_poly *private_key, ntru_context *context, char ** decr_msg){ + return 0; +} From 9cfb19ea7859944f6547569776a6685c4d7dcf75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20Pr=C3=BC=C3=9Fner?= Date: Fri, 2 May 2014 12:18:33 +0200 Subject: [PATCH 07/23] added commentary and implemented the pseudocode algorithm (ms_corourke.pdf) --- src/ntru_decrypt.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/ntru_decrypt.c b/src/ntru_decrypt.c index d385959..3a46676 100644 --- a/src/ntru_decrypt.c +++ b/src/ntru_decrypt.c @@ -19,6 +19,18 @@ * MA 02110-1301 USA */ +// 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){ + // 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 + } + if(a[i] > q/2) { + a[i] = a[i] - q // Shift coefficients of a into range (−q/2, q/2) + } + } + // StarMultiply(a, Fp , d, N, p) + // {Decode returns the decrypted message, d, through the argument list.} return 0; } From ecdc32f5e0a55fc6ce647105d0234a97a3f6481e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20Pr=C3=BC=C3=9Fner?= Date: Fri, 2 May 2014 13:00:00 +0200 Subject: [PATCH 08/23] first implementation of "pseudocode algorithm" (ms_corourke.pdf) --- src/ntru_decrypt.c | 37 ++++++++++++++++++++++++++++++++++--- src/ntru_decrypt.h | 2 ++ 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/src/ntru_decrypt.c b/src/ntru_decrypt.c index 3a46676..d7562a2 100644 --- a/src/ntru_decrypt.c +++ b/src/ntru_decrypt.c @@ -19,9 +19,27 @@ * MA 02110-1301 USA */ +#include "ntru_decrypt.h" + +/* + * Legend + * + * N : highest 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){ - // StarMultiply(f, e, a, N, q) + // toDo q = ?, p = ?, fp = ? + + pb_poly *a = first_multiply(private_key, encr_msg, q);// 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 @@ -30,7 +48,20 @@ int ntru_decrypt(char *encr_msg, pb_poly *private_key, ntru_context *context, ch a[i] = a[i] - q // Shift coefficients of a into range (−q/2, q/2) } } - // StarMultiply(a, Fp , d, N, p) + char* d = second_multiply(a, fp, p)// StarMultiply(a, Fp , d, N, p) + // {Decode returns the decrypted message, d, through the argument list.} - return 0; + return d; +} + +// toDo fix header file definition and types if needed! +pb_poly* first_multiply(pb_poly *private_key, char *encr_msg, int q) { + // toDo a= f*e mod q + return NULL; +} + +// toDo fix header file definition and types if needed! +char* second_multiply(pb_poly *a, pb_poly *fp, int p) { + //toDo a*Fp mod p + return NULL; } diff --git a/src/ntru_decrypt.h b/src/ntru_decrypt.h index 9da81cc..92ac915 100644 --- a/src/ntru_decrypt.h +++ b/src/ntru_decrypt.h @@ -26,5 +26,7 @@ #include "context.h" int ntru_decrypt(char *encr_msg, pb_poly *private_key, ntru_context *context, char ** decr_msg); +pb_poly* first_multiply(pb_poly*, char*, int); +char* first_multiply(pb_poly*, pb_poly*, int); #endif /* NTRU_DECRYPT */ From 4d438bb925c3cbe2fa036dc07e9d5ac3e2522c9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20Pr=C3=BC=C3=9Fner?= Date: Sat, 3 May 2014 13:18:15 +0200 Subject: [PATCH 09/23] changed gitignore --- .gitignore | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 566858b..3837701 100644 --- a/.gitignore +++ b/.gitignore @@ -20,4 +20,6 @@ latex/ src/main.c src/main -./.settings/org.eclipse* \ No newline at end of file +./.settings/org.eclipse* + +.project \ No newline at end of file From 06d359936a2bcf9f18b074c7dc3d3e380462c13f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20Pr=C3=BC=C3=9Fner?= Date: Sat, 3 May 2014 13:19:25 +0200 Subject: [PATCH 10/23] deleted .project file --- .project | 11 ----------- 1 file changed, 11 deletions(-) delete mode 100644 .project diff --git a/.project b/.project deleted file mode 100644 index a2785b2..0000000 --- a/.project +++ /dev/null @@ -1,11 +0,0 @@ - - - Semesterprojekt2014 - - - - - - - - From b4ba4112e5a283166fb09e3ad10a295a60f5dcc3 Mon Sep 17 00:00:00 2001 From: Alexander Rolfes Date: Sat, 3 May 2014 13:32:59 +0200 Subject: [PATCH 11/23] Revert "first implementation of "pseudocode algorithm" (ms_corourke.pdf)" This reverts commit ecdc32f5e0a55fc6ce647105d0234a97a3f6481e. --- src/ntru_decrypt.c | 37 +++---------------------------------- src/ntru_decrypt.h | 2 -- 2 files changed, 3 insertions(+), 36 deletions(-) diff --git a/src/ntru_decrypt.c b/src/ntru_decrypt.c index d7562a2..3a46676 100644 --- a/src/ntru_decrypt.c +++ b/src/ntru_decrypt.c @@ -19,27 +19,9 @@ * MA 02110-1301 USA */ -#include "ntru_decrypt.h" - -/* - * Legend - * - * N : highest 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){ - // toDo q = ?, p = ?, fp = ? - - pb_poly *a = first_multiply(private_key, encr_msg, q);// StarMultiply(f, e, a, N, q) - + // 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 @@ -48,20 +30,7 @@ int ntru_decrypt(char *encr_msg, pb_poly *private_key, ntru_context *context, ch a[i] = a[i] - q // Shift coefficients of a into range (−q/2, q/2) } } - char* d = second_multiply(a, fp, p)// StarMultiply(a, Fp , d, N, p) - + // StarMultiply(a, Fp , d, N, p) // {Decode returns the decrypted message, d, through the argument list.} - return d; -} - -// toDo fix header file definition and types if needed! -pb_poly* first_multiply(pb_poly *private_key, char *encr_msg, int q) { - // toDo a= f*e mod q - return NULL; -} - -// toDo fix header file definition and types if needed! -char* second_multiply(pb_poly *a, pb_poly *fp, int p) { - //toDo a*Fp mod p - return NULL; + return 0; } diff --git a/src/ntru_decrypt.h b/src/ntru_decrypt.h index 92ac915..9da81cc 100644 --- a/src/ntru_decrypt.h +++ b/src/ntru_decrypt.h @@ -26,7 +26,5 @@ #include "context.h" int ntru_decrypt(char *encr_msg, pb_poly *private_key, ntru_context *context, char ** decr_msg); -pb_poly* first_multiply(pb_poly*, char*, int); -char* first_multiply(pb_poly*, pb_poly*, int); #endif /* NTRU_DECRYPT */ From 4de50e2390a2779a10ef850a2a5d3d7243af409f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20Pr=C3=BC=C3=9Fner?= Date: Sat, 3 May 2014 13:51:00 +0200 Subject: [PATCH 12/23] removed redundant multiplication functions --- src/ntru_decrypt.c | 17 +++-------------- src/ntru_decrypt.h | 2 -- 2 files changed, 3 insertions(+), 16 deletions(-) diff --git a/src/ntru_decrypt.c b/src/ntru_decrypt.c index d7562a2..cfe08f4 100644 --- a/src/ntru_decrypt.c +++ b/src/ntru_decrypt.c @@ -20,6 +20,7 @@ */ #include "ntru_decrypt.h" +#include "poly.h" /* * Legend @@ -38,7 +39,7 @@ int ntru_decrypt(char *encr_msg, pb_poly *private_key, ntru_context *context, char ** decr_msg){ // toDo q = ?, p = ?, fp = ? - pb_poly *a = first_multiply(private_key, encr_msg, q);// StarMultiply(f, e, a, N, q) + //toDO StarMultiply(f, e, a, N, q) for(int i = 0, i < N, i++){ if(a[i] < 0 ) { @@ -48,20 +49,8 @@ int ntru_decrypt(char *encr_msg, pb_poly *private_key, ntru_context *context, ch a[i] = a[i] - q // Shift coefficients of a into range (−q/2, q/2) } } - char* d = second_multiply(a, fp, p)// StarMultiply(a, Fp , d, N, p) + //toDo StarMultiply(a, Fp , d, N, p) // {Decode returns the decrypted message, d, through the argument list.} return d; } - -// toDo fix header file definition and types if needed! -pb_poly* first_multiply(pb_poly *private_key, char *encr_msg, int q) { - // toDo a= f*e mod q - return NULL; -} - -// toDo fix header file definition and types if needed! -char* second_multiply(pb_poly *a, pb_poly *fp, int p) { - //toDo a*Fp mod p - return NULL; -} diff --git a/src/ntru_decrypt.h b/src/ntru_decrypt.h index 92ac915..9da81cc 100644 --- a/src/ntru_decrypt.h +++ b/src/ntru_decrypt.h @@ -26,7 +26,5 @@ #include "context.h" int ntru_decrypt(char *encr_msg, pb_poly *private_key, ntru_context *context, char ** decr_msg); -pb_poly* first_multiply(pb_poly*, char*, int); -char* first_multiply(pb_poly*, pb_poly*, int); #endif /* NTRU_DECRYPT */ From 7349c7e8bede89ecc888dcca66b74c9928f4458f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20Pr=C3=BC=C3=9Fner?= Date: Sat, 3 May 2014 14:03:03 +0200 Subject: [PATCH 13/23] used pb_starmultiply for the first multiplication and added p, q, N --- src/ntru_decrypt.c | 10 ++++++++-- src/ntru_decrypt.h | 2 +- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/ntru_decrypt.c b/src/ntru_decrypt.c index cfe08f4..5e048db 100644 --- a/src/ntru_decrypt.c +++ b/src/ntru_decrypt.c @@ -21,11 +21,12 @@ #include "ntru_decrypt.h" #include "poly.h" +#include "context.h" /* * Legend * - * N : highest degree of the polynom + * N : maximal degree of the polynom * q : "is given" (... mod q) * p : "is given" (... mod p) * f : private key @@ -36,10 +37,15 @@ * */ // 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){ +int ntru_decrypt(pb_poly *encr_msg, pb_poly *private_key, ntru_context *context, char ** decr_msg){ // toDo q = ?, p = ?, fp = ? + unsigned int q = *context->q; + unsigned int p = *context->p; + unsigned int N = *context->N; //toDO StarMultiply(f, e, a, N, q) + pb_poly *a; + pb_starmultiply(private_key, encr_msg, a, context, q); for(int i = 0, i < N, i++){ if(a[i] < 0 ) { diff --git a/src/ntru_decrypt.h b/src/ntru_decrypt.h index 9da81cc..2d89c13 100644 --- a/src/ntru_decrypt.h +++ b/src/ntru_decrypt.h @@ -25,6 +25,6 @@ #include "poly.h" #include "context.h" -int ntru_decrypt(char *encr_msg, pb_poly *private_key, ntru_context *context, char ** decr_msg); +int ntru_decrypt(pb_poly *encr_msg, pb_poly *private_key, ntru_context *context, char ** decr_msg); #endif /* NTRU_DECRYPT */ From b4a7870e9767ee65842ab18698770b19e1d14f2b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20Pr=C3=BC=C3=9Fner?= Date: Sat, 3 May 2014 14:07:12 +0200 Subject: [PATCH 14/23] fixed pointer arithmetical mistakes --- src/ntru_decrypt.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/ntru_decrypt.c b/src/ntru_decrypt.c index 5e048db..9f3bae4 100644 --- a/src/ntru_decrypt.c +++ b/src/ntru_decrypt.c @@ -39,11 +39,12 @@ // Require: N , q, p, secret key f , inverse polynomial Fp , and encrypted message e. int ntru_decrypt(pb_poly *encr_msg, pb_poly *private_key, ntru_context *context, char ** decr_msg){ // toDo q = ?, p = ?, fp = ? - unsigned int q = *context->q; - unsigned int p = *context->p; - unsigned int N = *context->N; - //toDO StarMultiply(f, e, a, N, q) + unsigned int q = context->q; + unsigned int p = context->p; + unsigned int N = context->N; + + // StarMultiply(f, e, a, N, q) pb_poly *a; pb_starmultiply(private_key, encr_msg, a, context, q); From fa97531eacfb14f729e3be031eb97ca233aaa5cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20Pr=C3=BC=C3=9Fner?= Date: Sat, 3 May 2014 14:57:05 +0200 Subject: [PATCH 15/23] implemented the coefficient shift into a range of -q/2 and q/2 --- src/ntru_decrypt.c | 24 ++++++++++++++++++------ src/ntru_decrypt.h | 5 ++++- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/src/ntru_decrypt.c b/src/ntru_decrypt.c index 9f3bae4..9815587 100644 --- a/src/ntru_decrypt.c +++ b/src/ntru_decrypt.c @@ -38,22 +38,34 @@ // Require: N , q, p, secret key f , inverse polynomial Fp , and encrypted message e. int ntru_decrypt(pb_poly *encr_msg, pb_poly *private_key, ntru_context *context, char ** decr_msg){ - // toDo q = ?, p = ?, fp = ? + // toDo fp = ? unsigned int q = context->q; unsigned int p = context->p; unsigned int N = context->N; // StarMultiply(f, e, a, N, q) - pb_poly *a; + 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(int i = 0, i < N, i++){ - if(a[i] < 0 ) { - a[i] = a[i] + q; // Make all coefficients positive + if(mp_cmp(&(a->terms[i]),&zero) == MP_LT) { // Make all coefficients positive + a->terms[i] = a->terms[i] + q; } - 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; } } //toDo StarMultiply(a, Fp , d, N, p) diff --git a/src/ntru_decrypt.h b/src/ntru_decrypt.h index 2d89c13..ca22f4e 100644 --- a/src/ntru_decrypt.h +++ b/src/ntru_decrypt.h @@ -25,6 +25,9 @@ #include "poly.h" #include "context.h" -int ntru_decrypt(pb_poly *encr_msg, pb_poly *private_key, ntru_context *context, char ** decr_msg); +int ntru_decrypt(pb_poly *encr_msg, + pb_poly *private_key, + ntru_context *context, + char ** decr_msg); #endif /* NTRU_DECRYPT */ From 5254cf93f3d59ecf5aafe6f71d6cf7658e5ff2c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20Pr=C3=BC=C3=9Fner?= Date: Sat, 3 May 2014 16:02:09 +0200 Subject: [PATCH 16/23] changed return type of ntru_decrypt, implemented second starmultiply, fixed numerical operations to mp_operations --- src/ntru_decrypt.c | 15 ++++++++++----- src/ntru_decrypt.h | 9 +++++---- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/ntru_decrypt.c b/src/ntru_decrypt.c index 9815587..e50daa5 100644 --- a/src/ntru_decrypt.c +++ b/src/ntru_decrypt.c @@ -37,8 +37,7 @@ * */ // Require: N , q, p, secret key f , inverse polynomial Fp , and encrypted message e. -int ntru_decrypt(pb_poly *encr_msg, pb_poly *private_key, ntru_context *context, char ** decr_msg){ - // toDo fp = ? +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; @@ -62,13 +61,19 @@ int ntru_decrypt(pb_poly *encr_msg, pb_poly *private_key, ntru_context *context, for(int 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; + //a->terms[i] = a->terms[i] + q; + mp_add((&a->terms[i]),&mp_q,(&a->terms[i])); } 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; + //a->terms[i] = a->terms[i] - mp_q; + mp_sub((&a->terms[i]),&mp_q,(&a->terms[i])); } } - //toDo StarMultiply(a, Fp , d, N, p) + + pb_poly *d = build_polynom(NULL, N, context); + + // StarMultiply(a, Fp , d, N, p) + pb_starmultiply(a, Fp, d, N, p); // {Decode returns the decrypted message, d, through the argument list.} return d; diff --git a/src/ntru_decrypt.h b/src/ntru_decrypt.h index ca22f4e..5f44005 100644 --- a/src/ntru_decrypt.h +++ b/src/ntru_decrypt.h @@ -25,9 +25,10 @@ #include "poly.h" #include "context.h" -int ntru_decrypt(pb_poly *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 */ From 4e6bb38260dd18ba3866f16a29c16ec4c1a0baa6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20Pr=C3=BC=C3=9Fner?= Date: Sat, 3 May 2014 17:18:23 +0200 Subject: [PATCH 17/23] numerous fixes --- src/Makefile | 4 ++-- src/ntru_decrypt.c | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) 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 e50daa5..abd55df 100644 --- a/src/ntru_decrypt.c +++ b/src/ntru_decrypt.c @@ -20,8 +20,6 @@ */ #include "ntru_decrypt.h" -#include "poly.h" -#include "context.h" /* * Legend @@ -42,11 +40,13 @@ pb_poly* ntru_decrypt(pb_poly *encr_msg, pb_poly *private_key, pb_poly *Fp, ntru unsigned int q = context->q; unsigned int p = context->p; unsigned int N = context->N; + unsigned int i; // StarMultiply(f, e, a, N, q) pb_poly *a = build_polynom(NULL, N, context); pb_starmultiply(private_key, encr_msg, a, context, q); - + printf("%s\n", "Nach dem StarMultiply: "); + draw_polynom(a); mp_int mp_q; mp_int mp_qdiv2; mp_int zero; @@ -56,10 +56,10 @@ pb_poly* ntru_decrypt(pb_poly *encr_msg, pb_poly *private_key, pb_poly *Fp, ntru init_integer(&zero); MP_SET_INT(&mp_q, q); - mp_div_2(&mp_q, mp_qdiv2); + mp_div_2(&mp_q, &mp_qdiv2); mp_zero(&zero); - for(int i = 0, i < N, i++){ + 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])); @@ -73,7 +73,7 @@ pb_poly* ntru_decrypt(pb_poly *encr_msg, pb_poly *private_key, pb_poly *Fp, ntru pb_poly *d = build_polynom(NULL, N, context); // StarMultiply(a, Fp , d, N, p) - pb_starmultiply(a, Fp, d, N, p); + pb_starmultiply(a, Fp, d, context, p); // {Decode returns the decrypted message, d, through the argument list.} return d; From 74a3fa88c78458440fd1dde178228a88d4e1dd0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20Pr=C3=BC=C3=9Fner?= Date: Mon, 5 May 2014 13:58:53 +0200 Subject: [PATCH 18/23] moved some lines --- src/ntru_decrypt.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/ntru_decrypt.c b/src/ntru_decrypt.c index abd55df..5594f3e 100644 --- a/src/ntru_decrypt.c +++ b/src/ntru_decrypt.c @@ -45,8 +45,7 @@ pb_poly* ntru_decrypt(pb_poly *encr_msg, pb_poly *private_key, pb_poly *Fp, ntru // StarMultiply(f, e, a, N, q) pb_poly *a = build_polynom(NULL, N, context); pb_starmultiply(private_key, encr_msg, a, context, q); - printf("%s\n", "Nach dem StarMultiply: "); - draw_polynom(a); + mp_int mp_q; mp_int mp_qdiv2; mp_int zero; @@ -70,6 +69,9 @@ pb_poly* ntru_decrypt(pb_poly *encr_msg, pb_poly *private_key, pb_poly *Fp, ntru } } + 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) From dd73a662a0ef00b68bde9700a1b6b0e3095a6d1a Mon Sep 17 00:00:00 2001 From: Alexander Rolfes Date: Mon, 5 May 2014 21:51:58 +0200 Subject: [PATCH 19/23] DOXYGEN: Documentation of ntru_decrypt function Removed unnecessary comments inside of function and wrote a Doxygen compliant comment above the function --- src/ntru_decrypt.c | 43 ++++++++++++++++++------------------------- 1 file changed, 18 insertions(+), 25 deletions(-) diff --git a/src/ntru_decrypt.c b/src/ntru_decrypt.c index 5594f3e..fd9d93d 100644 --- a/src/ntru_decrypt.c +++ b/src/ntru_decrypt.c @@ -21,28 +21,28 @@ #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. -pb_poly* ntru_decrypt(pb_poly *encr_msg, pb_poly *private_key, pb_poly *Fp, ntru_context *context, char ** decr_msg){ +/** + * Decryption of the given Polynom with the private key, its inverse + * and the fitting ntru_context + * + * @param encr_msg encrypted polynom with maximum length of N from + * the given context + * @param priv_key the polynom containing the private key to decrypt + * the message + * @param priv_key_inv the inverse polynome to the private key + * @param context the ntru_context + * @param decr_msg may contain the decrypted polynome at some point + * @returns the decrypted polynome at the moment + * + * + */ unsigned int q = context->q; unsigned int p = context->p; unsigned int N = context->N; unsigned int i; - // StarMultiply(f, e, a, N, q) pb_poly *a = build_polynom(NULL, N, context); pb_starmultiply(private_key, encr_msg, a, context, q); @@ -59,24 +59,17 @@ pb_poly* ntru_decrypt(pb_poly *encr_msg, pb_poly *private_key, pb_poly *Fp, ntru 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; + if(mp_cmp(&(a->terms[i]),&zero) == MP_LT) { mp_add((&a->terms[i]),&mp_q,(&a->terms[i])); } - 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; + if(mp_cmp(&(a->terms[i]), &mp_qdiv2) == MP_GT) 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 d; } From 01a08fdafad11d3a5ce19c83b398dea92e723951 Mon Sep 17 00:00:00 2001 From: Alexander Rolfes Date: Mon, 5 May 2014 21:53:41 +0200 Subject: [PATCH 20/23] CODE: Rename of key-Parameters Renamed private_key and Fp to priv_key and priv_key_inv --- src/ntru_decrypt.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/ntru_decrypt.c b/src/ntru_decrypt.c index fd9d93d..bccb4b4 100644 --- a/src/ntru_decrypt.c +++ b/src/ntru_decrypt.c @@ -37,6 +37,8 @@ * * */ +pb_poly* ntru_decrypt(pb_poly *encr_msg, pb_poly *priv_key, + pb_poly *priv_key_inv, ntru_context *context, char ** decr_msg){ unsigned int q = context->q; unsigned int p = context->p; @@ -44,7 +46,7 @@ unsigned int i; pb_poly *a = build_polynom(NULL, N, context); - pb_starmultiply(private_key, encr_msg, a, context, q); + pb_starmultiply(priv_key, encr_msg, a, context, q); mp_int mp_q; mp_int mp_qdiv2; @@ -69,7 +71,7 @@ pb_poly *d = build_polynom(NULL, N, context); - pb_starmultiply(a, Fp, d, context, p); + pb_starmultiply(a, priv_key_inv, d, context, p); return d; } From 1412ba0d1adcc9fedbc5be3a6ee6e5b213e90fc7 Mon Sep 17 00:00:00 2001 From: Alexander Rolfes Date: Mon, 5 May 2014 21:56:44 +0200 Subject: [PATCH 21/23] CODE: Fixed missing bracket --- src/ntru_decrypt.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ntru_decrypt.c b/src/ntru_decrypt.c index bccb4b4..3d3d128 100644 --- a/src/ntru_decrypt.c +++ b/src/ntru_decrypt.c @@ -64,7 +64,7 @@ pb_poly* ntru_decrypt(pb_poly *encr_msg, pb_poly *priv_key, if(mp_cmp(&(a->terms[i]),&zero) == MP_LT) { mp_add((&a->terms[i]),&mp_q,(&a->terms[i])); } - if(mp_cmp(&(a->terms[i]), &mp_qdiv2) == MP_GT) + if(mp_cmp(&(a->terms[i]), &mp_qdiv2) == MP_GT) { mp_sub((&a->terms[i]),&mp_q,(&a->terms[i])); } } From 40303356619338eb876546089fd68e38304774f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20Pr=C3=BC=C3=9Fner?= Date: Mon, 12 May 2014 13:46:59 +0200 Subject: [PATCH 22/23] POLY: implemented pb_normalize normalizes a polynom by defining a numerical range for each factor of a polynom. --- src/poly.c | 34 ++++++++++++++++++++++++++++++++++ src/poly.h | 7 +++++++ 2 files changed, 41 insertions(+) diff --git a/src/poly.c b/src/poly.c index c6f77f5..6d9c2b2 100644 --- a/src/poly.c +++ b/src/poly.c @@ -432,3 +432,37 @@ void draw_polynom(pb_poly * const poly) } printf("\n"); } + +void pb_normalize(pb_poly *poly, int low_border, int high_border, ntru_context *ctx){ + unsigned int p = ctx->p; + unsigned int N = ctx->N; + + mp_int mp_p; + mp_int mp_low_border; + mp_int mp_high_border; + + init_integer(&mp_low_border); + init_integer(&mp_high_border); + init_integer(&mp_p); + + MP_SET_INT(&mp_p, p); + MP_SET_INT(&mp_low_border,(unsigned long)abs(low_border)); + mp_neg(&mp_low_border,&mp_low_border); + MP_SET_INT(&mp_high_border,high_border); + + + unsigned int i; + for(i = 0; i < N; i++){ + if(mp_cmp(&(poly->terms[i]),&mp_low_border) == MP_LT) { + mp_add(&(poly->terms[i]),&mp_p,&(poly->terms[i])); + } else if(mp_cmp(&(poly->terms[i]),&mp_high_border) == MP_GT) { + mp_sub(&(poly->terms[i]),&mp_p,&(poly->terms[i])); + } + } +} + +void draw_mp_int(mp_int *digit) { + char buf[8192]; + mp_toradix(digit, buf, 10); + printf("%s\n",buf); +} diff --git a/src/poly.h b/src/poly.h index 77a9b54..7f961a1 100644 --- a/src/poly.h +++ b/src/poly.h @@ -184,4 +184,11 @@ bool pb_inverse_poly_q(pb_poly *a, void draw_polynom(pb_poly * const poly); +void pb_normalize(pb_poly*, + int, + int, + ntru_context*); + +void draw_mp_int(mp_int*); + #endif /* NTRU_POLY_H */ From f8f7634c840f73b25c6ef816751f18e836bf132a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20Pr=C3=BC=C3=9Fner?= Date: Mon, 12 May 2014 13:49:00 +0200 Subject: [PATCH 23/23] DEC ntru_decrypt now uses pb_normalize --- src/ntru_decrypt.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/ntru_decrypt.c b/src/ntru_decrypt.c index 3d3d128..5eba7a9 100644 --- a/src/ntru_decrypt.c +++ b/src/ntru_decrypt.c @@ -73,5 +73,7 @@ pb_poly* ntru_decrypt(pb_poly *encr_msg, pb_poly *priv_key, pb_starmultiply(a, priv_key_inv, d, context, p); + pb_normalize(d,-1,1,context); + return d; }