post quantum cryptography
Highly optimized implementation of the NTRUEncrypt algorithm
/home/travis/build/hasufell/pqc/src/ntru_rnd.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2014 FH Bielefeld
00003  *
00004  * This file is part of a FH Bielefeld project.
00005  *
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2.1 of the License, or (at your option) any later version.
00010  *
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with this library; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00019  * MA  02110-1301  USA
00020  */
00021 
00028 #include "math.h"
00029 #include "ntru_err.h"
00030 #include "ntru_params.h"
00031 #include "ntru_poly.h"
00032 
00033 #include <fmpz_poly.h>
00034 #include <fcntl.h>
00035 #include <stdlib.h>
00036 #include <unistd.h>
00037 
00038 
00039 /*------------------------------------------------------------------------*/
00040 
00041 int
00042 get_rnd_int(void)
00043 {
00044     int rnd_data = open("/dev/random", O_RDONLY);
00045     int rnd_int;
00046     size_t rnd_len = 0;
00047 
00048     while (rnd_len < sizeof(rnd_int)) {
00049         ssize_t result = read(rnd_data, ((char*)&rnd_int) + rnd_len,
00050                 sizeof(rnd_int) - rnd_len);
00051 
00052         if (result < 0)
00053             NTRU_ABORT("Unable to read /dev/random!\n");
00054 
00055         rnd_len += result;
00056     }
00057 
00058     close(rnd_data);
00059 
00060     return rnd_int;
00061 }
00062 
00063 /*------------------------------------------------------------------------*/
00064 
00065 int
00066 get_urnd_int(void)
00067 {
00068     int rnd_data = open("/dev/urandom", O_RDONLY);
00069     int rnd_int;
00070     ssize_t result;
00071 
00072     result = read(rnd_data, ((char*)&rnd_int),
00073             sizeof(rnd_int));
00074 
00075     if (result < 0)
00076         NTRU_ABORT("Unable to read /dev/urandom!\n");
00077 
00078     close(rnd_data);
00079 
00080     return rnd_int;
00081 }
00082 
00083 /*------------------------------------------------------------------------*/
00084 
00085 void
00086 ntru_get_rnd_tern_poly_num(fmpz_poly_t poly,
00087         const ntru_params *params,
00088         uint32_t num_ones,
00089         uint32_t num_neg_ones,
00090         int (*rnd_int)(void))
00091 {
00092     if (!poly || ! params)
00093         NTRU_ABORT_DEBUG("unexpected NULL parameters");
00094 
00095     fmpz_poly_zero(poly);
00096 
00097     while (num_ones != 0 || num_neg_ones != 0) {
00098         int32_t pos = rnd_int() % params->N;
00099 
00100         if (!fmpz_cmp_si_n(fmpz_poly_get_coeff_ptr(poly, pos), 0)) {
00101             if (num_ones > 0) {
00102                 fmpz_poly_set_coeff_si(poly, pos, 1);
00103                 num_ones--;
00104             } else if (num_neg_ones > 0) {
00105                 fmpz_poly_set_coeff_si(poly, pos, -1);
00106                 num_neg_ones--;
00107             }
00108         }
00109     }
00110 }
00111 
00112 /*------------------------------------------------------------------------*/
 All Data Structures Files Functions Variables Typedefs Defines