post quantum cryptography
Highly optimized implementation of the NTRUEncrypt algorithm
|
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_decrypt.h" 00031 #include "ntru_params.h" 00032 #include "ntru_poly.h" 00033 #include "ntru_poly_ascii.h" 00034 #include "ntru_string.h" 00035 00036 #include <stdbool.h> 00037 #include <string.h> 00038 00039 #include <fmpz_poly.h> 00040 #include <fmpz.h> 00041 00042 00043 /*------------------------------------------------------------------------*/ 00044 00045 void 00046 ntru_decrypt_poly( 00047 const fmpz_poly_t encr_msg, 00048 const fmpz_poly_t priv_key, 00049 const fmpz_poly_t priv_key_inv, 00050 fmpz_poly_t out_bin, 00051 const ntru_params *params) 00052 { 00053 fmpz_poly_t a, 00054 priv_key_tmp, 00055 priv_key_inv_tmp, 00056 encr_msg_tmp; 00057 00058 if (!encr_msg || !priv_key || !priv_key_inv || !out_bin || !params) 00059 NTRU_ABORT_DEBUG("Unexpected NULL parameters"); 00060 00061 fmpz_poly_init(a); 00062 fmpz_poly_zero(a); 00063 00064 /* 00065 * make sure all are shifted to 00066 * [-q/2, q/2] 00067 */ 00068 fmpz_poly_init(priv_key_tmp); 00069 fmpz_poly_init(priv_key_inv_tmp); 00070 fmpz_poly_init(encr_msg_tmp); 00071 fmpz_poly_set(priv_key_tmp, priv_key); 00072 fmpz_poly_set(priv_key_inv_tmp, priv_key_inv); 00073 fmpz_poly_set(encr_msg_tmp, encr_msg); 00074 fmpz_poly_mod(priv_key_tmp, params->q); 00075 fmpz_poly_mod(priv_key_inv_tmp, params->q); 00076 fmpz_poly_mod(encr_msg_tmp, params->q); 00077 00078 poly_starmultiply(priv_key_tmp, encr_msg_tmp, a, params, params->q); 00079 fmpz_poly_mod(a, params->q); 00080 poly_starmultiply(a, priv_key_inv_tmp, out_bin, params, params->p); 00081 fmpz_poly_mod(out_bin, params->p); 00082 00083 fmpz_poly_clear(a); 00084 fmpz_poly_clear(priv_key_tmp); 00085 fmpz_poly_clear(priv_key_inv_tmp); 00086 fmpz_poly_clear(encr_msg_tmp); 00087 } 00088 00089 /*------------------------------------------------------------------------*/ 00090 00091 string * 00092 ntru_decrypt_string( 00093 const string *encr_msg, 00094 const fmpz_poly_t priv_key, 00095 const fmpz_poly_t priv_key_inv, 00096 const ntru_params *params) 00097 { 00098 uint32_t i = 0; 00099 string *decr_msg; 00100 fmpz_poly_t **poly_array; 00101 00102 if (!encr_msg || !encr_msg->len) 00103 NTRU_ABORT_DEBUG("Unexpected NULL parameters"); 00104 00105 poly_array = base64_to_poly_arr(encr_msg, params); 00106 00107 while (*poly_array[i]) { 00108 ntru_decrypt_poly(*poly_array[i], 00109 priv_key, 00110 priv_key_inv, 00111 *poly_array[i], 00112 params); 00113 i++; 00114 } 00115 00116 decr_msg = bin_poly_arr_to_ascii((const fmpz_poly_t **)poly_array, 00117 i, params); 00118 00119 poly_delete_array(poly_array); 00120 00121 return decr_msg; 00122 } 00123 00124 /*------------------------------------------------------------------------*/