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_poly_ascii.h" 00030 #include "ntru_common.h" 00031 #include "ntru_mem.h" 00032 #include "ntru_params.h" 00033 #include "ntru_poly.h" 00034 #include "ntru_string.h" 00035 00036 #include <glib.h> 00037 00038 #include <stdint.h> 00039 #include <stdlib.h> 00040 #include <string.h> 00041 00042 #include <fmpz_poly.h> 00043 #include <fmpz.h> 00044 00045 00062 static string * 00063 get_bin_arr_to_ascii(const char *binary_rep); 00064 00065 00066 /*------------------------------------------------------------------------*/ 00067 00068 static string * 00069 get_bin_arr_to_ascii(const char *binary_rep) 00070 { 00071 size_t int_arr_size = 0; 00072 uint8_t *int_arr = NULL; 00073 uint32_t i = 0; 00074 char *int_string = NULL; 00075 string *result = ntru_malloc(sizeof(*result)); 00076 00077 if (!binary_rep || !*binary_rep) 00078 return NULL; 00079 00080 int_arr_size = strlen(binary_rep) / ASCII_BITS + 1; 00081 int_arr = ntru_malloc(sizeof(*int_arr) * int_arr_size); 00082 00083 while (*binary_rep) { 00084 int_arr[i] = 0; 00085 00086 /* convert one binary integer to real integer */ 00087 for (uint32_t j = 0; j < ASCII_BITS && *binary_rep; j++) { 00088 if (*binary_rep == '1') 00089 int_arr[i] = int_arr[i] * 2 + 1; 00090 else if (*binary_rep == '0') 00091 int_arr[i] *= 2; 00092 binary_rep++; 00093 } 00094 00095 i++; /* amount of real integers */ 00096 } 00097 00098 int_string = ntru_malloc(CHAR_SIZE * (i + 1)); 00099 00100 for (uint32_t j = 0; j < i; j++) 00101 int_string[j] = (char) int_arr[j]; 00102 00103 result->ptr = int_string; 00104 result->len = i; 00105 00106 free(int_arr); 00107 00108 return result; 00109 } 00110 00111 /*------------------------------------------------------------------------*/ 00112 00113 string * 00114 bin_poly_to_ascii(const fmpz_poly_t poly, 00115 const ntru_params *params) 00116 { 00117 string *result_string = ntru_malloc(sizeof(*result_string)); 00118 char *binary_rep = ntru_malloc(CHAR_SIZE * (params->N)); 00119 uint32_t i = 0; 00120 00121 for (i = 0; i < params->N; i++) { 00122 fmpz *coeff = fmpz_poly_get_coeff_ptr(poly, i); 00123 00124 if (coeff) { 00125 if (!fmpz_cmp_si(coeff, 1)) 00126 binary_rep[i] = '1'; 00127 else if (!fmpz_cmp_si(coeff, -1)) 00128 binary_rep[i] = '0'; 00129 } else { 00130 break; 00131 } 00132 } 00133 00134 result_string->ptr = binary_rep; 00135 result_string->len = i; 00136 00137 return result_string; 00138 } 00139 00140 /*------------------------------------------------------------------------*/ 00141 00142 string * 00143 bin_poly_arr_to_ascii(const fmpz_poly_t **bin_poly_arr, 00144 const uint32_t poly_c, 00145 const ntru_params *params) 00146 { 00147 char *binary_rep = NULL; 00148 size_t string_len = 0; 00149 string *ascii_string = NULL; 00150 00151 /* 00152 * parse the polynomial coefficients into a string 00153 */ 00154 binary_rep = ntru_malloc(CHAR_SIZE * (params->N * poly_c + 1)); 00155 for (uint32_t i = 0; i < poly_c; i++) { 00156 string *single_poly_string = NULL; 00157 00158 single_poly_string = bin_poly_to_ascii(*bin_poly_arr[i], params); 00159 00160 memcpy(binary_rep + string_len, 00161 single_poly_string->ptr, 00162 single_poly_string->len); 00163 00164 string_len += single_poly_string->len; 00165 00166 string_delete(single_poly_string); 00167 } 00168 binary_rep[string_len] = '\0'; 00169 00170 ascii_string = get_bin_arr_to_ascii(binary_rep); 00171 00172 free(binary_rep); 00173 00174 return ascii_string; 00175 } 00176 00177 /*------------------------------------------------------------------------*/ 00178 00179 string * 00180 poly_to_ascii(const fmpz_poly_t poly, 00181 const ntru_params *params) 00182 { 00183 string *result_string = ntru_malloc(sizeof(*result_string)); 00184 char *string_rep = ntru_malloc(CHAR_SIZE * (params->N)); 00185 00186 for (uint32_t j = 0; j < params->N; j++) { 00187 uint8_t coeff = fmpz_poly_get_coeff_ui(poly, j); 00188 if (coeff == params->q) 00189 string_rep[j] = '\0'; 00190 else 00191 string_rep[j] = (char)coeff; 00192 } 00193 00194 result_string->ptr = string_rep; 00195 result_string->len = params->N; 00196 00197 return result_string; 00198 } 00199 00200 /*------------------------------------------------------------------------*/ 00201 00202 string * 00203 poly_arr_to_ascii(const fmpz_poly_t **poly_array, 00204 const uint32_t poly_c, 00205 const ntru_params *params) 00206 { 00207 char *string_rep = NULL; 00208 size_t string_len = 0; 00209 string *result_string = ntru_malloc(sizeof(*result_string)); 00210 00211 /* 00212 * parse the polynomial coefficients into a string 00213 */ 00214 string_rep = ntru_malloc(CHAR_SIZE * (params->N * poly_c + 1)); 00215 for (uint32_t i = 0; i < poly_c; i++) { 00216 string *poly_str; 00217 00218 poly_str = poly_to_ascii(*poly_array[i], params); 00219 00220 memcpy(string_rep + string_len, 00221 poly_str->ptr, 00222 poly_str->len); 00223 string_len += poly_str->len; 00224 00225 string_delete(poly_str); 00226 } 00227 00228 result_string->ptr = string_rep; 00229 result_string->len = string_len; 00230 00231 return result_string; 00232 } 00233 00234 /*------------------------------------------------------------------------*/ 00235 00236 string * 00237 poly_to_base64(const fmpz_poly_t poly, 00238 const ntru_params *params) 00239 { 00240 string *result_string = ntru_malloc(sizeof(*result_string)); 00241 string *string_rep = NULL; 00242 gchar *base64_string = NULL, 00243 *tmp = NULL; 00244 00245 string_rep = poly_to_ascii(poly, params); 00246 00247 tmp = g_base64_encode((const guchar *)string_rep->ptr, 00248 string_rep->len); 00249 base64_string = g_base64_encode((const guchar *)tmp, 00250 strlen(tmp)); 00251 00252 result_string->ptr = base64_string; 00253 result_string->len = strlen(base64_string); 00254 00255 string_delete(string_rep); 00256 free(tmp); 00257 00258 return result_string; 00259 } 00260 00261 /*------------------------------------------------------------------------*/ 00262 00263 string * 00264 poly_arr_to_base64(const fmpz_poly_t **poly_array, 00265 const uint32_t poly_c, 00266 const ntru_params *params) 00267 { 00268 string *string_rep; 00269 string *result_string = ntru_malloc(sizeof(*result_string)); 00270 00271 gchar *base64_string = NULL, 00272 *tmp = NULL; 00273 00274 string_rep = poly_arr_to_ascii(poly_array, poly_c, params); 00275 00276 tmp = g_base64_encode((const guchar *)string_rep->ptr, string_rep->len); 00277 base64_string = g_base64_encode((const guchar *)tmp, 00278 strlen(tmp)); 00279 00280 result_string->ptr = base64_string; 00281 result_string->len = strlen(base64_string); 00282 00283 string_delete(string_rep); 00284 free(tmp); 00285 00286 return result_string; 00287 } 00288 00289 /*------------------------------------------------------------------------*/