From ff40048d55bb07fab8f49c7815be38a4a6d799b9 Mon Sep 17 00:00:00 2001 From: hasufell Date: Mon, 14 Apr 2014 22:29:36 +0200 Subject: [PATCH] RAND: basic randomization No sanity checks like minimum number of non-NULL coefficients for the polynomial. --- src/rand.c | 97 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/rand.h | 35 ++++++++++++++++++++ 2 files changed, 132 insertions(+) create mode 100644 src/rand.c create mode 100644 src/rand.h diff --git a/src/rand.c b/src/rand.c new file mode 100644 index 0000000..db61d48 --- /dev/null +++ b/src/rand.c @@ -0,0 +1,97 @@ +/* + * Copyright (C) 2014 hasufell + * + * This file is part of a hasufell project. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301 USA + */ + +#include "context.h" +#include "err.h" +#include "rand.h" +#include "poly.h" + +#include +#include +#include +#include +#include +#include +#include +#include + + +/** + * Gets randomly a small integer + * from the set {-1, 0, 1} using /dev/urandom. + * + * @param sign stores the signness [out] + * @return random small integer + */ +mp_digit get_urnd_int_small(int *sign) +{ + int random_data; + mp_digit random_int; + ssize_t result; + + random_data = open("/dev/urandom", O_RDONLY); + if ((result = read(random_data, &random_int, sizeof(random_int))) < 0) + NTRU_ABORT("Unable to read /dev/urandom"); + close(random_int); + + if ((random_int % 2) == 0) { + random_int = 0; + *sign = 0; + } else if (random_int % 3) { + random_int = 1; + *sign = 1; + } else { + random_int = 1; + *sign = 0; + } + + return random_int; +} + +/** + * Gets a random polynomial with coefficients + * from the set {-1 ,0 ,1} using /dev/urandom. + * + * @param ctx the NTRU context + * @return newly allocated polynomial + */ +pb_poly *get_urnd_poly_small(pqc_context *ctx) +{ + mp_int *chara = malloc(sizeof(mp_int)); + init_integer(chara); + pb_poly *poly = malloc(sizeof(pb_poly)); + init_polynom_size(poly, chara, ctx->N); + + for (int i = 0; i < ctx->N; i++) { + int sign; + int c = get_urnd_int_small(&sign); + + mp_set(&(poly->terms[i]), (mp_digit)c); + + if (sign == 1) + poly->terms[i].sign = 1; + } + poly->used = ctx->N; + pb_clamp(poly); + + return poly; +} + diff --git a/src/rand.h b/src/rand.h new file mode 100644 index 0000000..41206bd --- /dev/null +++ b/src/rand.h @@ -0,0 +1,35 @@ +/* + * Copyright (C) 2014 hasufell + * + * This file is part of a hasufell project. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301 USA + */ + + +#ifndef NTRU_RAND_H +#define NTRU_RAND_H + +#include "context.h" + +#include + + +mp_digit get_urnd_int_small(int *sign); + +pb_poly *get_urnd_poly_small(pqc_context *ctx); + +#endif /* NTRU_RAND_H */