Merge branch 'master' of ssh://gitlab.hasufell.de:22022/wiseqube/quantumcrypto

Conflicts:
	src/poly.h
This commit is contained in:
hasufell 2014-05-19 21:33:38 +02:00
commit 2e222ccad3
No known key found for this signature in database
GPG Key ID: 220CD1C5BDEED020
6 changed files with 160 additions and 2 deletions

4
.gitignore vendored
View File

@ -19,3 +19,7 @@ latex/
# currently masked
src/main.c
src/main
./.settings/org.eclipse*
.project

View File

@ -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

79
src/ntru_decrypt.c Normal file
View File

@ -0,0 +1,79 @@
/*
* 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
*/
#include "ntru_decrypt.h"
/**
* 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
*
*
*/
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;
unsigned int N = context->N;
unsigned int i;
pb_poly *a = build_polynom(NULL, N);
pb_starmultiply(priv_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) {
mp_add((&a->terms[i]),&mp_q,(&a->terms[i]));
}
if(mp_cmp(&(a->terms[i]), &mp_qdiv2) == MP_GT) {
mp_sub((&a->terms[i]),&mp_q,(&a->terms[i]));
}
}
pb_poly *d = build_polynom(NULL, N);
pb_starmultiply(a, priv_key_inv, d, context, p);
pb_normalize(d,-1,1,context);
return d;
}

34
src/ntru_decrypt.h Normal file
View File

@ -0,0 +1,34 @@
/*
* 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
#include "poly.h"
#include "context.h"
pb_poly* ntru_decrypt(pb_poly*,
pb_poly*,
pb_poly*,
ntru_context*,
char**);
#endif /* NTRU_DECRYPT */

View File

@ -600,3 +600,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);
}

View File

@ -214,4 +214,11 @@ bool pb_inverse_poly_p(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 */