post quantum cryptography
Highly optimized implementation of the NTRUEncrypt algorithm
 All Data Structures Files Functions Variables Typedefs Macros Pages
ntru_encrypt.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_encrypt.h"
31 #include "ntru_mem.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 <string.h>
38 
39 #include <fmpz_poly.h>
40 #include <fmpz.h>
41 
42 
43 /*------------------------------------------------------------------------*/
44 
45 void
47  const fmpz_poly_t msg_bin,
48  const fmpz_poly_t pub_key,
49  const fmpz_poly_t rnd,
50  fmpz_poly_t out,
51  const ntru_params *params)
52 {
53  fmpz_poly_t tmp_poly_msg;
54 
55  if (!msg_bin || !pub_key || !rnd || !out || !params)
56  NTRU_ABORT_DEBUG("Unexpected NULL parameters");
57 
58  /* allow aliasing */
59  fmpz_poly_init(tmp_poly_msg);
60  fmpz_poly_set(tmp_poly_msg, msg_bin);
61 
62  fmpz_poly_zero(out);
63  poly_starmultiply(pub_key, rnd, out, params, params->q);
64 
65  fmpz_poly_add(out, out, tmp_poly_msg);
66  fmpz_poly_mod_unsigned(out, params->q);
67 
68  fmpz_poly_clear(tmp_poly_msg);
69 }
70 
71 /*------------------------------------------------------------------------*/
72 
73 string *
75  const string *msg,
76  const fmpz_poly_t pub_key,
77  const fmpz_poly_t rnd,
78  const ntru_params *params)
79 {
80  uint32_t i = 0;
81  string *enc_msg;
82  fmpz_poly_t **poly_array;
83 
84  if (!msg || !msg->len)
85  NTRU_ABORT_DEBUG("Unexpected NULL parameters");
86 
87  poly_array = ascii_to_bin_poly_arr(msg, params);
88 
89  while (*poly_array[i]) {
90  ntru_encrypt_poly(*poly_array[i],
91  pub_key,
92  rnd,
93  *poly_array[i],
94  params);
95  i++;
96  }
97 
98  enc_msg = poly_arr_to_base64((const fmpz_poly_t **)poly_array,
99  i, params);
100 
101  poly_delete_array(poly_array);
102 
103  return enc_msg;
104 }
105 
106 /*------------------------------------------------------------------------*/
fmpz_poly_t ** ascii_to_bin_poly_arr(const string *to_poly, const ntru_params *params)
size_t len
Definition: ntru_string.h:53
void ntru_encrypt_poly(const fmpz_poly_t msg_bin, const fmpz_poly_t pub_key, const fmpz_poly_t rnd, fmpz_poly_t out, const ntru_params *params)
Definition: ntru_encrypt.c:46
string * poly_arr_to_base64(const fmpz_poly_t **poly_array, const uint32_t poly_c, const ntru_params *params)
void fmpz_poly_mod_unsigned(fmpz_poly_t a, const uint32_t mod)
Definition: ntru_poly.c:166
#define NTRU_ABORT_DEBUG(...)
Definition: ntru_err.h:39
header for encrypt.c
header for ntru_mem.c
header for ntru_poly_ascii.c
NTRU parameters.
string * ntru_encrypt_string(const string *msg, const fmpz_poly_t pub_key, const fmpz_poly_t rnd, const ntru_params *params)
Definition: ntru_encrypt.c:74
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
uint32_t q
Definition: ntru_params.h:52
header for ntru_string.c
header for ntru_poly.c
header for ntru_ascii_poly.c