post quantum cryptography
Highly optimized implementation of the NTRUEncrypt algorithm
 All Data Structures Files Functions Variables Typedefs Macros Pages
ntru_keypair.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2014 FH Bielefeld
3  *
4  * This file is part of a FH Bielefeld project.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19  * MA 02110-1301 USA
20  */
21 
29 #include "ntru_ascii_poly.h"
30 #include "ntru_file.h"
31 #include "ntru_keypair.h"
32 #include "ntru_params.h"
33 #include "ntru_poly.h"
34 #include "ntru_poly_ascii.h"
35 #include "ntru_string.h"
36 
37 #include <fmpz_poly.h>
38 #include <fmpz.h>
39 
40 #include <stdbool.h>
41 #include <string.h>
42 
43 
44 /*------------------------------------------------------------------------*/
45 
46 bool
48  fmpz_poly_t f,
49  fmpz_poly_t g,
50  keypair *pair,
51  ntru_params *params)
52 {
53  bool retval = false;
54  fmpz_poly_t Fq,
55  Fp,
56  pub;
57 
58  if (!f || !g || !params)
59  goto _return;
60 
61  fmpz_poly_init(Fq);
62  fmpz_poly_init(Fp);
63  fmpz_poly_init(pub);
64 
65  if (!poly_inverse_poly_q(f, Fq, params))
66  goto _cleanup;
67 
68  if (!poly_inverse_poly_p(f, Fp, params))
69  goto _cleanup;
70 
71  poly_starmultiply(Fq, g, pub, params, params->q);
72  fmpz_poly_scalar_mul_ui(pub, pub, params->p);
73  fmpz_poly_mod_unsigned(pub, params->q);
74 
75  fmpz_poly_init(pair->priv);
76  fmpz_poly_init(pair->priv_inv);
77  fmpz_poly_init(pair->pub);
78 
79  fmpz_poly_set(pair->priv, f);
80  fmpz_poly_set(pair->priv_inv, Fp);
81  fmpz_poly_set(pair->pub, pub);
82 
83  retval = true;
84 
85 _cleanup:
86  fmpz_poly_clear(Fq);
87  fmpz_poly_clear(Fp);
88  fmpz_poly_clear(pub);
89 _return:
90  return retval;
91 }
92 
93 /*------------------------------------------------------------------------*/
94 
95 void
96 export_public_key(char const * const filename,
97  fmpz_poly_t pub,
98  ntru_params *params)
99 {
100  string *pub_string;
101 
102  pub_string = poly_to_base64(pub, params);
103  write_file(pub_string, filename);
104 
105  string_delete(pub_string);
106 }
107 
108 /*------------------------------------------------------------------------*/
109 
110 void
111 export_priv_key(char const * const filename,
112  fmpz_poly_t priv,
113  ntru_params *params)
114 {
115  string *priv_string;
116  fmpz_poly_t priv_u;
117 
118  fmpz_poly_init(priv_u);
119  fmpz_poly_set(priv_u, priv);
120  fmpz_poly_mod_unsigned(priv_u, params->p);
121 
122  priv_string = poly_to_base64(priv_u, params);
123  write_file(priv_string, filename);
124 
125  fmpz_poly_clear(priv_u);
126  string_delete(priv_string);
127 }
128 
129 /*------------------------------------------------------------------------*/
130 
131 void
132 import_public_key(char const * const filename,
133  fmpz_poly_t pub,
134  ntru_params *params)
135 {
136  string *pub_string;
137  fmpz_poly_t **imported;
138 
139  pub_string = read_file(filename);
140  imported = base64_to_poly_arr(pub_string, params);
141 
142  /* if the array exceeds one element, then something
143  * went horribly wrong */
144  if (*imported[1])
145  NTRU_ABORT_DEBUG("Failed importing public key!");
146 
147  fmpz_poly_set(pub, **imported);
148 
149  string_delete(pub_string);
150  poly_delete_array(imported);
151  free(imported);
152 }
153 
154 /*------------------------------------------------------------------------*/
155 
156 void
157 import_priv_key(char const * const filename,
158  fmpz_poly_t priv,
159  fmpz_poly_t priv_inv,
160  ntru_params *params)
161 {
162  string *pub_string;
163  fmpz_poly_t **imported,
164  Fp;
165 
166  fmpz_poly_init(Fp);
167 
168  pub_string = read_file(filename);
169 
170  imported = base64_to_poly_arr(pub_string, params);
171  fmpz_poly_mod(**imported, params->p);
172 
173  /* if the array exceeds one element, then something
174  * went horribly wrong */
175  if (*imported[1])
176  NTRU_ABORT_DEBUG("Failed importing private key!");
177 
178  fmpz_poly_set(priv, **imported);
179 
180  if (!poly_inverse_poly_p(priv, Fp, params))
181  goto cleanup;
182 
183  fmpz_poly_mod(Fp, params->p);
184 
185  fmpz_poly_set(priv_inv, Fp);
186  fmpz_poly_clear(Fp);
187 
188 cleanup:
189  string_delete(pub_string);
190  poly_delete_array(imported);
191  free(imported);
192 }
193 
194 /*------------------------------------------------------------------------*/
195 
196 void
198 {
199  fmpz_poly_clear(pair->priv_inv);
200  fmpz_poly_clear(pair->priv);
201  fmpz_poly_clear(pair->pub);
202 }
203 
204 /*------------------------------------------------------------------------*/
bool poly_inverse_poly_q(const fmpz_poly_t a, fmpz_poly_t Fq, const ntru_params *params)
Definition: ntru_poly.c:297
void string_delete(string *del_string)
Definition: ntru_string.c:47
header for ntru_keypair.c
void ntru_delete_keypair(keypair *pair)
Definition: ntru_keypair.c:197
void fmpz_poly_mod_unsigned(fmpz_poly_t a, const uint32_t mod)
Definition: ntru_poly.c:166
bool write_file(string const *wstring, char const *const filename)
Definition: ntru_file.c:107
void fmpz_poly_mod(fmpz_poly_t a, const uint32_t mod)
Definition: ntru_poly.c:182
#define NTRU_ABORT_DEBUG(...)
Definition: ntru_err.h:39
uint32_t p
Definition: ntru_params.h:56
bool poly_inverse_poly_p(const fmpz_poly_t a, fmpz_poly_t Fp, const ntru_params *params)
Definition: ntru_poly.c:409
fmpz_poly_t ** base64_to_poly_arr(const string *to_poly, const ntru_params *params)
header for ntru_file.c
fmpz_poly_t priv_inv
Definition: ntru_keypair.h:56
header for ntru_poly_ascii.c
NTRU parameters.
string * read_file(char const *const filename)
Definition: ntru_file.c:50
void export_public_key(char const *const filename, fmpz_poly_t pub, ntru_params *params)
Definition: ntru_keypair.c:96
fmpz_poly_t pub
Definition: ntru_keypair.h:61
void poly_starmultiply(const fmpz_poly_t a, const fmpz_poly_t b, fmpz_poly_t c, const ntru_params *params, uint32_t modulus)
Definition: ntru_poly.c:239
string * poly_to_base64(const fmpz_poly_t poly, const ntru_params *params)
void poly_delete_array(fmpz_poly_t **poly_array)
Definition: ntru_poly.c:131
fmpz_poly_t priv
Definition: ntru_keypair.h:51
uint32_t q
Definition: ntru_params.h:52
header for ntru_string.c
bool ntru_create_keypair(fmpz_poly_t f, fmpz_poly_t g, keypair *pair, ntru_params *params)
Definition: ntru_keypair.c:47
void export_priv_key(char const *const filename, fmpz_poly_t priv, ntru_params *params)
Definition: ntru_keypair.c:111
void import_public_key(char const *const filename, fmpz_poly_t pub, ntru_params *params)
Definition: ntru_keypair.c:132
void import_priv_key(char const *const filename, fmpz_poly_t priv, fmpz_poly_t priv_inv, ntru_params *params)
Definition: ntru_keypair.c:157
header for ntru_poly.c
header for ntru_ascii_poly.c