post quantum cryptography
Highly optimized implementation of the NTRUEncrypt algorithm
 All Data Structures Files Functions Variables Typedefs Macros Pages
ntru_rnd.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 
28 #include "math.h"
29 #include "ntru_err.h"
30 #include "ntru_params.h"
31 #include "ntru_poly.h"
32 
33 #include <fmpz_poly.h>
34 #include <fcntl.h>
35 #include <stdlib.h>
36 #include <unistd.h>
37 
38 
39 /*------------------------------------------------------------------------*/
40 
41 int
43 {
44  int rnd_data = open("/dev/random", O_RDONLY);
45  int rnd_int;
46  size_t rnd_len = 0;
47 
48  while (rnd_len < sizeof(rnd_int)) {
49  ssize_t result = read(rnd_data, ((char*)&rnd_int) + rnd_len,
50  sizeof(rnd_int) - rnd_len);
51 
52  if (result < 0)
53  NTRU_ABORT("Unable to read /dev/random!\n");
54 
55  rnd_len += result;
56  }
57 
58  close(rnd_data);
59 
60  return rnd_int;
61 }
62 
63 /*------------------------------------------------------------------------*/
64 
65 int
67 {
68  int rnd_data = open("/dev/urandom", O_RDONLY);
69  int rnd_int;
70  ssize_t result;
71 
72  result = read(rnd_data, ((char*)&rnd_int),
73  sizeof(rnd_int));
74 
75  if (result < 0)
76  NTRU_ABORT("Unable to read /dev/urandom!\n");
77 
78  close(rnd_data);
79 
80  return rnd_int;
81 }
82 
83 /*------------------------------------------------------------------------*/
84 
85 void
86 ntru_get_rnd_tern_poly_num(fmpz_poly_t poly,
87  const ntru_params *params,
88  uint32_t num_ones,
89  uint32_t num_neg_ones,
90  int (*rnd_int)(void))
91 {
92  if (!poly || ! params)
93  NTRU_ABORT_DEBUG("unexpected NULL parameters");
94 
95  fmpz_poly_zero(poly);
96 
97  while (num_ones != 0 || num_neg_ones != 0) {
98  int32_t pos = rnd_int() % params->N;
99 
100  if (!fmpz_cmp_si_n(fmpz_poly_get_coeff_ptr(poly, pos), 0)) {
101  if (num_ones > 0) {
102  fmpz_poly_set_coeff_si(poly, pos, 1);
103  num_ones--;
104  } else if (num_neg_ones > 0) {
105  fmpz_poly_set_coeff_si(poly, pos, -1);
106  num_neg_ones--;
107  }
108  }
109  }
110 }
111 
112 /*------------------------------------------------------------------------*/
uint32_t N
Definition: ntru_params.h:48
int get_urnd_int(void)
Definition: ntru_rnd.c:66
#define NTRU_ABORT_DEBUG(...)
Definition: ntru_err.h:39
int get_rnd_int(void)
Definition: ntru_rnd.c:42
NTRU parameters.
void ntru_get_rnd_tern_poly_num(fmpz_poly_t poly, const ntru_params *params, uint32_t num_ones, uint32_t num_neg_ones, int(*rnd_int)(void))
Definition: ntru_rnd.c:86
error handling
int fmpz_cmp_si_n(const fmpz_t f, slong g)
Definition: ntru_poly.c:93
header for ntru_poly.c
#define NTRU_ABORT(...)
Definition: ntru_err.h:33