post quantum cryptography
Highly optimized implementation of the NTRUEncrypt algorithm
/home/travis/build/hasufell/pqc/src/ntru_encrypt.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 
00029 #include "ntru_ascii_poly.h"
00030 #include "ntru_encrypt.h"
00031 #include "ntru_mem.h"
00032 #include "ntru_params.h"
00033 #include "ntru_poly.h"
00034 #include "ntru_poly_ascii.h"
00035 #include "ntru_string.h"
00036 
00037 #include <string.h>
00038 
00039 #include <fmpz_poly.h>
00040 #include <fmpz.h>
00041 
00042 
00043 /*------------------------------------------------------------------------*/
00044 
00045 void
00046 ntru_encrypt_poly(
00047         const fmpz_poly_t msg_bin,
00048         const fmpz_poly_t pub_key,
00049         const fmpz_poly_t rnd,
00050         fmpz_poly_t out,
00051         const ntru_params *params)
00052 {
00053     fmpz_poly_t tmp_poly_msg;
00054 
00055     if (!msg_bin || !pub_key || !rnd || !out || !params)
00056         NTRU_ABORT_DEBUG("Unexpected NULL parameters");
00057 
00058     /* allow aliasing */
00059     fmpz_poly_init(tmp_poly_msg);
00060     fmpz_poly_set(tmp_poly_msg, msg_bin);
00061 
00062     fmpz_poly_zero(out);
00063     poly_starmultiply(pub_key, rnd, out, params, params->q);
00064 
00065     fmpz_poly_add(out, out, tmp_poly_msg);
00066     fmpz_poly_mod_unsigned(out, params->q);
00067 
00068     fmpz_poly_clear(tmp_poly_msg);
00069 }
00070 
00071 /*------------------------------------------------------------------------*/
00072 
00073 string *
00074 ntru_encrypt_string(
00075         const string *msg,
00076         const fmpz_poly_t pub_key,
00077         const fmpz_poly_t rnd,
00078         const ntru_params *params)
00079 {
00080     uint32_t i = 0;
00081     string *enc_msg;
00082     fmpz_poly_t **poly_array;
00083 
00084     if (!msg || !msg->len)
00085         NTRU_ABORT_DEBUG("Unexpected NULL parameters");
00086 
00087     poly_array = ascii_to_bin_poly_arr(msg, params);
00088 
00089     while (*poly_array[i]) {
00090         ntru_encrypt_poly(*poly_array[i],
00091                 pub_key,
00092                 rnd,
00093                 *poly_array[i],
00094                 params);
00095         i++;
00096     }
00097 
00098     enc_msg = poly_arr_to_base64((const fmpz_poly_t **)poly_array,
00099             i, params);
00100 
00101     poly_delete_array(poly_array);
00102 
00103     return enc_msg;
00104 }
00105 
00106 /*------------------------------------------------------------------------*/
 All Data Structures Files Functions Variables Typedefs Defines