post quantum cryptography
Highly optimized implementation of the NTRUEncrypt algorithm
 All Data Structures Files Functions Variables Typedefs Macros Pages
ntru_decrypt.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_decrypt.h"
31 #include "ntru_params.h"
32 #include "ntru_poly.h"
33 #include "ntru_poly_ascii.h"
34 #include "ntru_string.h"
35 
36 #include <stdbool.h>
37 #include <string.h>
38 
39 #include <fmpz_poly.h>
40 #include <fmpz.h>
41 
42 
43 /*------------------------------------------------------------------------*/
44 
45 void
47  const fmpz_poly_t encr_msg,
48  const fmpz_poly_t priv_key,
49  const fmpz_poly_t priv_key_inv,
50  fmpz_poly_t out_bin,
51  const ntru_params *params)
52 {
53  fmpz_poly_t a,
54  priv_key_tmp,
55  priv_key_inv_tmp,
56  encr_msg_tmp;
57 
58  if (!encr_msg || !priv_key || !priv_key_inv || !out_bin || !params)
59  NTRU_ABORT_DEBUG("Unexpected NULL parameters");
60 
61  fmpz_poly_init(a);
62  fmpz_poly_zero(a);
63 
64  /*
65  * make sure all are shifted to
66  * [-q/2, q/2]
67  */
68  fmpz_poly_init(priv_key_tmp);
69  fmpz_poly_init(priv_key_inv_tmp);
70  fmpz_poly_init(encr_msg_tmp);
71  fmpz_poly_set(priv_key_tmp, priv_key);
72  fmpz_poly_set(priv_key_inv_tmp, priv_key_inv);
73  fmpz_poly_set(encr_msg_tmp, encr_msg);
74  fmpz_poly_mod(priv_key_tmp, params->q);
75  fmpz_poly_mod(priv_key_inv_tmp, params->q);
76  fmpz_poly_mod(encr_msg_tmp, params->q);
77 
78  poly_starmultiply(priv_key_tmp, encr_msg_tmp, a, params, params->q);
79  fmpz_poly_mod(a, params->q);
80  poly_starmultiply(a, priv_key_inv_tmp, out_bin, params, params->p);
81  fmpz_poly_mod(out_bin, params->p);
82 
83  fmpz_poly_clear(a);
84  fmpz_poly_clear(priv_key_tmp);
85  fmpz_poly_clear(priv_key_inv_tmp);
86  fmpz_poly_clear(encr_msg_tmp);
87 }
88 
89 /*------------------------------------------------------------------------*/
90 
91 string *
93  const string *encr_msg,
94  const fmpz_poly_t priv_key,
95  const fmpz_poly_t priv_key_inv,
96  const ntru_params *params)
97 {
98  uint32_t i = 0;
99  string *decr_msg;
100  fmpz_poly_t **poly_array;
101 
102  if (!encr_msg || !encr_msg->len)
103  NTRU_ABORT_DEBUG("Unexpected NULL parameters");
104 
105  poly_array = base64_to_poly_arr(encr_msg, params);
106 
107  while (*poly_array[i]) {
108  ntru_decrypt_poly(*poly_array[i],
109  priv_key,
110  priv_key_inv,
111  *poly_array[i],
112  params);
113  i++;
114  }
115 
116  decr_msg = bin_poly_arr_to_ascii((const fmpz_poly_t **)poly_array,
117  i, params);
118 
119  poly_delete_array(poly_array);
120 
121  return decr_msg;
122 }
123 
124 /*------------------------------------------------------------------------*/
size_t len
Definition: ntru_string.h:53
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
fmpz_poly_t ** base64_to_poly_arr(const string *to_poly, const ntru_params *params)
header for ntru_decrypt.c
header for ntru_poly_ascii.c
NTRU parameters.
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
void poly_delete_array(fmpz_poly_t **poly_array)
Definition: ntru_poly.c:131
string * bin_poly_arr_to_ascii(const fmpz_poly_t **bin_poly_arr, const uint32_t poly_c, const ntru_params *params)
uint32_t q
Definition: ntru_params.h:52
void ntru_decrypt_poly(const fmpz_poly_t encr_msg, const fmpz_poly_t priv_key, const fmpz_poly_t priv_key_inv, fmpz_poly_t out_bin, const ntru_params *params)
Definition: ntru_decrypt.c:46
header for ntru_string.c
string * ntru_decrypt_string(const string *encr_msg, const fmpz_poly_t priv_key, const fmpz_poly_t priv_key_inv, const ntru_params *params)
Definition: ntru_decrypt.c:92
header for ntru_poly.c
header for ntru_ascii_poly.c