2014-05-18 08:28:25 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2014 FH Bielefeld
|
|
|
|
*
|
|
|
|
* This file is part of a FH Bielefeld 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
|
|
|
|
*/
|
|
|
|
|
2014-05-27 23:09:52 +00:00
|
|
|
/* TODO: clean up some iterators */
|
|
|
|
/* TODO: ascii_to_bin_poly() should accept a string, not a c_str */
|
|
|
|
|
2014-05-25 18:50:12 +00:00
|
|
|
/**
|
|
|
|
* @file ascii_poly.c
|
|
|
|
* This file allows to convert between ascii strings
|
|
|
|
* and polynomials.
|
|
|
|
* @brief asci->poly and poly->ascii
|
|
|
|
*/
|
|
|
|
|
2014-05-26 19:30:42 +00:00
|
|
|
#include "ascii_poly.h"
|
2014-05-25 18:49:37 +00:00
|
|
|
#include "common.h"
|
2014-05-18 16:08:36 +00:00
|
|
|
#include "context.h"
|
2014-05-18 08:28:25 +00:00
|
|
|
#include "mem.h"
|
2014-05-26 19:30:42 +00:00
|
|
|
#include "ntru_string.h"
|
2014-05-18 08:28:25 +00:00
|
|
|
#include "poly.h"
|
|
|
|
|
2014-05-27 23:09:52 +00:00
|
|
|
#include <glib.h>
|
|
|
|
|
2014-05-25 17:21:59 +00:00
|
|
|
#include <stdint.h>
|
2014-05-18 08:28:25 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2014-05-25 17:21:59 +00:00
|
|
|
#include <fmpz_poly.h>
|
|
|
|
#include <fmpz.h>
|
|
|
|
|
|
|
|
|
2014-05-18 08:28:25 +00:00
|
|
|
/**
|
2014-05-25 17:21:59 +00:00
|
|
|
* Convert an integer to it's binary representation
|
|
|
|
* as a string and return it.
|
2014-05-18 08:28:25 +00:00
|
|
|
*
|
2014-05-26 19:59:29 +00:00
|
|
|
* As in: 90 => "10110101"
|
|
|
|
*
|
2014-05-25 17:21:59 +00:00
|
|
|
* @param value the integer to convert
|
|
|
|
* @return the binary representation as a newly allocated string
|
2014-05-18 08:28:25 +00:00
|
|
|
*/
|
2014-05-25 21:04:22 +00:00
|
|
|
static char *
|
|
|
|
get_int_to_bin_str(uint8_t value);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts a binary representation of multiple concatenated
|
|
|
|
* integers to the corresponding array of ascii chars, which
|
|
|
|
* is NULL-terminated.
|
|
|
|
*
|
2014-05-26 19:59:29 +00:00
|
|
|
* It reads in 8bit chunks, as in:
|
|
|
|
*
|
|
|
|
* 10110101|00111100|01011001 => 90|60|89 => "Z<Y"
|
|
|
|
*
|
2014-05-29 20:16:50 +00:00
|
|
|
* Chunks that are 00000000 are stripped from the result.
|
|
|
|
*
|
2014-05-25 21:04:22 +00:00
|
|
|
* @param binary_rep the binary representation of multiple
|
|
|
|
* integers concatenated
|
2014-05-28 18:57:40 +00:00
|
|
|
* @return string of corresponding ascii-chars,
|
2014-05-25 21:04:22 +00:00
|
|
|
* newly allocated
|
|
|
|
*/
|
2014-05-28 18:57:40 +00:00
|
|
|
static string *
|
2014-05-25 21:04:22 +00:00
|
|
|
get_bin_arr_to_ascii(char *binary_rep);
|
|
|
|
|
|
|
|
|
2014-05-27 23:17:19 +00:00
|
|
|
/*------------------------------------------------------------------------*/
|
|
|
|
|
2014-05-25 21:04:22 +00:00
|
|
|
static char *
|
|
|
|
get_int_to_bin_str(uint8_t value)
|
2014-05-18 08:28:25 +00:00
|
|
|
{
|
2014-05-25 17:21:59 +00:00
|
|
|
int i;
|
2014-05-25 18:49:37 +00:00
|
|
|
const size_t bin_string_size = ASCII_BITS + 1;
|
|
|
|
char *bin_string = ntru_malloc(sizeof(*bin_string) *
|
2014-05-25 17:21:59 +00:00
|
|
|
(bin_string_size)); /* account for trailing null-byte */
|
2014-05-18 08:28:25 +00:00
|
|
|
|
2014-05-25 17:21:59 +00:00
|
|
|
/* terminate properly */
|
|
|
|
bin_string[bin_string_size - 1] = '\0';
|
2014-05-18 16:08:36 +00:00
|
|
|
|
2014-05-25 18:49:37 +00:00
|
|
|
for (i = ASCII_BITS - 1; i >= 0; --i, value >>= 1)
|
2014-05-25 17:21:59 +00:00
|
|
|
bin_string[i] = (value & 1) + '0';
|
|
|
|
|
|
|
|
return bin_string;
|
|
|
|
}
|
|
|
|
|
2014-05-27 23:17:19 +00:00
|
|
|
/*------------------------------------------------------------------------*/
|
|
|
|
|
2014-05-28 18:57:40 +00:00
|
|
|
static string *
|
2014-05-25 21:04:22 +00:00
|
|
|
get_bin_arr_to_ascii(char *binary_rep)
|
2014-05-25 17:21:59 +00:00
|
|
|
{
|
2014-05-28 09:20:28 +00:00
|
|
|
size_t int_arr_size = 0;
|
|
|
|
uint8_t *int_arr = NULL;
|
2014-05-25 17:21:59 +00:00
|
|
|
uint32_t i = 0;
|
2014-05-28 09:20:28 +00:00
|
|
|
char *int_string = NULL;
|
2014-05-28 18:57:40 +00:00
|
|
|
string *result = ntru_malloc(sizeof(*result));
|
2014-05-25 17:21:59 +00:00
|
|
|
|
2014-05-28 09:20:28 +00:00
|
|
|
if (!binary_rep || !*binary_rep)
|
|
|
|
return NULL;
|
|
|
|
|
2014-05-28 17:40:39 +00:00
|
|
|
int_arr_size = strlen(binary_rep) / ASCII_BITS + 1;
|
2014-05-28 09:20:28 +00:00
|
|
|
int_arr = ntru_malloc(sizeof(*int_arr) * int_arr_size);
|
|
|
|
|
2014-05-28 17:40:39 +00:00
|
|
|
while (*binary_rep) {
|
2014-05-25 17:21:59 +00:00
|
|
|
int_arr[i] = 0;
|
2014-05-28 17:40:39 +00:00
|
|
|
|
|
|
|
/* convert one binary integer to real integer */
|
2014-05-28 09:20:28 +00:00
|
|
|
for (uint32_t j = 0; j < ASCII_BITS && *binary_rep; j++) {
|
2014-05-27 23:09:52 +00:00
|
|
|
if (*binary_rep == '1')
|
2014-05-25 17:21:59 +00:00
|
|
|
int_arr[i] = int_arr[i] * 2 + 1;
|
2014-05-27 23:09:52 +00:00
|
|
|
else if (*binary_rep == '0')
|
2014-05-25 17:21:59 +00:00
|
|
|
int_arr[i] *= 2;
|
2014-05-27 23:09:52 +00:00
|
|
|
binary_rep++;
|
2014-05-18 08:28:25 +00:00
|
|
|
}
|
2014-05-28 17:40:39 +00:00
|
|
|
|
|
|
|
i++; /* amount of real integers */
|
2014-05-18 08:28:25 +00:00
|
|
|
}
|
2014-05-25 17:21:59 +00:00
|
|
|
|
2014-05-29 20:11:05 +00:00
|
|
|
int_string = ntru_malloc(CHAR_SIZE * (i + 1));
|
2014-05-25 17:21:59 +00:00
|
|
|
|
|
|
|
for (uint32_t j = 0; j < i; j++)
|
|
|
|
int_string[j] = (char) int_arr[j];
|
2014-05-28 18:57:40 +00:00
|
|
|
|
|
|
|
result->ptr = int_string;
|
2014-05-30 16:54:51 +00:00
|
|
|
result->len = i;
|
2014-05-25 17:21:59 +00:00
|
|
|
|
2014-05-28 09:20:28 +00:00
|
|
|
free(int_arr);
|
|
|
|
|
2014-05-28 18:57:40 +00:00
|
|
|
return result;
|
2014-05-18 16:08:36 +00:00
|
|
|
}
|
|
|
|
|
2014-05-27 23:17:19 +00:00
|
|
|
/*------------------------------------------------------------------------*/
|
|
|
|
|
2014-05-26 23:08:17 +00:00
|
|
|
fmpz_poly_t *
|
2014-05-27 23:09:52 +00:00
|
|
|
ascii_bin_to_bin_poly(char *to_poly, ntru_context *ctx)
|
2014-05-18 16:08:36 +00:00
|
|
|
{
|
2014-05-26 23:08:17 +00:00
|
|
|
uint32_t i = 0;
|
|
|
|
fmpz_poly_t *new_poly = ntru_malloc(sizeof(*new_poly));
|
|
|
|
|
|
|
|
fmpz_poly_init(*new_poly);
|
|
|
|
|
2014-05-28 09:20:28 +00:00
|
|
|
while (to_poly[i] && i < ctx->N) {
|
2014-05-26 23:08:17 +00:00
|
|
|
fmpz_poly_set_coeff_si(*new_poly,
|
2014-05-28 09:20:28 +00:00
|
|
|
i,
|
2014-05-26 23:08:17 +00:00
|
|
|
(to_poly[i] == '0') ? -1 : 1);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new_poly;
|
|
|
|
}
|
|
|
|
|
2014-05-27 23:17:19 +00:00
|
|
|
/*------------------------------------------------------------------------*/
|
|
|
|
|
2014-05-26 23:08:17 +00:00
|
|
|
fmpz_poly_t **
|
2014-05-28 18:57:40 +00:00
|
|
|
ascii_to_bin_poly_arr(string *to_poly, ntru_context *ctx)
|
2014-05-26 23:08:17 +00:00
|
|
|
{
|
2014-05-28 18:57:40 +00:00
|
|
|
char *cur = to_poly->ptr;
|
2014-05-30 16:54:51 +00:00
|
|
|
char *out = ntru_malloc(CHAR_SIZE * (to_poly->len * ASCII_BITS + 1));
|
|
|
|
uint32_t polyc = 0;
|
2014-05-25 17:21:59 +00:00
|
|
|
fmpz_poly_t **poly_array;
|
|
|
|
|
|
|
|
*out = '\0';
|
|
|
|
|
2014-05-28 18:57:40 +00:00
|
|
|
for (uint32_t i = 0; i < to_poly->len; i++) {
|
2014-05-25 17:21:59 +00:00
|
|
|
char *tmp_string = get_int_to_bin_str((int)(*cur));
|
|
|
|
strcat(out, tmp_string);
|
|
|
|
cur++;
|
|
|
|
free(tmp_string);
|
2014-05-18 16:08:36 +00:00
|
|
|
}
|
|
|
|
|
2014-05-28 17:41:51 +00:00
|
|
|
poly_array = ntru_malloc(sizeof(**poly_array) *
|
2014-05-30 16:54:51 +00:00
|
|
|
(strlen(out) / ctx->N + 1));
|
2014-05-25 17:21:59 +00:00
|
|
|
|
2014-05-26 23:08:17 +00:00
|
|
|
for (uint32_t i = 0; i < strlen(out); i += ctx->N) {
|
|
|
|
char chunk[ctx->N + 1];
|
|
|
|
size_t real_chunk_size;
|
2014-05-26 08:56:42 +00:00
|
|
|
|
2014-05-26 23:08:17 +00:00
|
|
|
real_chunk_size =
|
|
|
|
(strlen(out + i) > ctx->N) ? ctx->N : strlen(out + i);
|
2014-05-25 17:21:59 +00:00
|
|
|
|
2014-05-26 23:08:17 +00:00
|
|
|
memcpy(chunk, out + i, real_chunk_size);
|
|
|
|
chunk[real_chunk_size] = '\0';
|
2014-05-26 18:59:12 +00:00
|
|
|
|
2014-05-27 23:09:52 +00:00
|
|
|
poly_array[polyc] = ascii_bin_to_bin_poly(chunk, ctx);
|
2014-05-26 18:59:12 +00:00
|
|
|
|
2014-05-25 17:21:59 +00:00
|
|
|
polyc++;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(out);
|
|
|
|
|
|
|
|
poly_array[polyc] = NULL;
|
|
|
|
|
|
|
|
return poly_array;
|
|
|
|
}
|
|
|
|
|
2014-05-27 23:17:19 +00:00
|
|
|
/*------------------------------------------------------------------------*/
|
|
|
|
|
2014-05-26 23:08:17 +00:00
|
|
|
string *
|
2014-05-27 23:09:52 +00:00
|
|
|
bin_poly_to_ascii(fmpz_poly_t poly,
|
2014-05-26 23:08:17 +00:00
|
|
|
ntru_context *ctx)
|
|
|
|
{
|
|
|
|
string *result_string = ntru_malloc(sizeof(*result_string));
|
|
|
|
char *binary_rep = ntru_malloc(CHAR_SIZE * (ctx->N));
|
|
|
|
uint32_t i = 0;
|
|
|
|
|
|
|
|
for (uint32_t j = 0; j < ctx->N; j++) {
|
|
|
|
fmpz *coeff = fmpz_poly_get_coeff_ptr(poly, j);
|
|
|
|
|
|
|
|
if (coeff) {
|
2014-05-30 16:54:51 +00:00
|
|
|
if (!fmpz_cmp_si(coeff, 1))
|
2014-05-26 23:08:17 +00:00
|
|
|
binary_rep[i] = '1';
|
2014-05-30 16:54:51 +00:00
|
|
|
else if (!fmpz_cmp_si(coeff, -1))
|
2014-05-26 23:08:17 +00:00
|
|
|
binary_rep[i] = '0';
|
2014-05-28 09:20:28 +00:00
|
|
|
} else {
|
2014-05-30 16:54:51 +00:00
|
|
|
break;
|
2014-05-26 23:08:17 +00:00
|
|
|
}
|
2014-05-30 16:54:51 +00:00
|
|
|
|
2014-05-26 23:08:17 +00:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
result_string->ptr = binary_rep;
|
|
|
|
result_string->len = i;
|
|
|
|
|
|
|
|
return result_string;
|
|
|
|
}
|
|
|
|
|
2014-05-27 23:17:19 +00:00
|
|
|
/*------------------------------------------------------------------------*/
|
|
|
|
|
2014-05-26 23:08:17 +00:00
|
|
|
string *
|
2014-05-27 23:09:52 +00:00
|
|
|
bin_poly_arr_to_ascii(fmpz_poly_t **bin_poly_arr, ntru_context *ctx)
|
2014-05-25 17:21:59 +00:00
|
|
|
{
|
|
|
|
fmpz_poly_t *ascii_poly;
|
|
|
|
char *binary_rep = NULL;
|
2014-05-26 23:08:17 +00:00
|
|
|
size_t string_len = 0;
|
2014-05-28 18:57:40 +00:00
|
|
|
string *ascii_string = NULL;
|
2014-05-25 17:21:59 +00:00
|
|
|
size_t old_length = 0,
|
|
|
|
new_length;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* parse the polynomial coefficients into a string
|
|
|
|
*/
|
2014-05-26 23:08:17 +00:00
|
|
|
binary_rep = ntru_calloc(1, CHAR_SIZE * (ctx->N + 1));
|
2014-05-27 23:09:52 +00:00
|
|
|
while ((ascii_poly = *bin_poly_arr++)) {
|
2014-05-28 09:20:28 +00:00
|
|
|
string *single_poly_string = NULL;
|
2014-05-26 23:08:17 +00:00
|
|
|
|
2014-05-28 09:20:28 +00:00
|
|
|
new_length = CHAR_SIZE * (ctx->N);
|
2014-05-25 17:21:59 +00:00
|
|
|
|
|
|
|
REALLOC(binary_rep,
|
|
|
|
old_length +
|
|
|
|
new_length +
|
|
|
|
1); /* trailing null byte */
|
|
|
|
|
|
|
|
old_length += new_length;
|
|
|
|
|
2014-05-27 23:09:52 +00:00
|
|
|
single_poly_string = bin_poly_to_ascii(*ascii_poly, ctx);
|
2014-05-28 09:20:28 +00:00
|
|
|
|
2014-05-26 23:08:17 +00:00
|
|
|
memcpy(binary_rep + string_len,
|
|
|
|
single_poly_string->ptr,
|
|
|
|
single_poly_string->len);
|
2014-05-28 09:20:28 +00:00
|
|
|
|
2014-05-26 23:08:17 +00:00
|
|
|
string_len += single_poly_string->len;
|
2014-05-27 23:09:52 +00:00
|
|
|
|
|
|
|
string_delete(single_poly_string);
|
2014-05-18 16:08:36 +00:00
|
|
|
}
|
2014-05-26 23:08:17 +00:00
|
|
|
binary_rep[string_len] = '\0';
|
2014-05-25 17:21:59 +00:00
|
|
|
|
|
|
|
ascii_string = get_bin_arr_to_ascii(binary_rep);
|
|
|
|
|
2014-05-27 23:09:52 +00:00
|
|
|
free(binary_rep);
|
|
|
|
|
2014-05-28 18:57:40 +00:00
|
|
|
return ascii_string;
|
2014-05-27 23:09:52 +00:00
|
|
|
}
|
|
|
|
|
2014-05-27 23:17:19 +00:00
|
|
|
/*------------------------------------------------------------------------*/
|
|
|
|
|
2014-05-28 00:04:45 +00:00
|
|
|
string *
|
|
|
|
poly_to_ascii(fmpz_poly_t poly,
|
|
|
|
ntru_context *ctx)
|
|
|
|
{
|
|
|
|
string *result_string = ntru_malloc(sizeof(*result_string));
|
|
|
|
char *string_rep = ntru_malloc(CHAR_SIZE * (ctx->N));
|
|
|
|
|
|
|
|
for (uint32_t j = 0; j < ctx->N; j++) {
|
|
|
|
uint8_t coeff = fmpz_poly_get_coeff_ui(poly, j);
|
|
|
|
if (coeff == ctx->q)
|
2014-05-29 23:37:03 +00:00
|
|
|
string_rep[j] = '\0';
|
2014-05-28 00:04:45 +00:00
|
|
|
else
|
2014-05-29 23:37:03 +00:00
|
|
|
string_rep[j] = (char)coeff;
|
2014-05-28 00:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
result_string->ptr = string_rep;
|
2014-05-29 23:37:03 +00:00
|
|
|
result_string->len = ctx->N;
|
2014-05-28 00:04:45 +00:00
|
|
|
|
|
|
|
return result_string;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*------------------------------------------------------------------------*/
|
|
|
|
|
2014-05-27 23:09:52 +00:00
|
|
|
fmpz_poly_t **
|
|
|
|
base64_to_poly_arr(string *to_poly, ntru_context *ctx)
|
|
|
|
{
|
|
|
|
uint32_t i = 0,
|
|
|
|
polyc = 0;
|
|
|
|
gsize out_len;
|
2014-05-28 00:04:45 +00:00
|
|
|
guchar *base64_decoded = NULL,
|
|
|
|
*base_tmp = NULL;
|
2014-05-27 23:09:52 +00:00
|
|
|
string *new_string = ntru_malloc(sizeof(*new_string));
|
|
|
|
fmpz_poly_t **poly_array;
|
2014-05-28 00:04:45 +00:00
|
|
|
char *tmp = ntru_malloc(sizeof(char) * (to_poly->len + 1));
|
2014-05-27 23:09:52 +00:00
|
|
|
|
|
|
|
/* g_base64_decode() needs it null-terminated */
|
|
|
|
memcpy(tmp, to_poly->ptr, to_poly->len);
|
|
|
|
tmp[to_poly->len] = '\0';
|
|
|
|
|
2014-05-28 00:04:45 +00:00
|
|
|
base_tmp = g_base64_decode((const gchar *)tmp, &out_len);
|
|
|
|
|
|
|
|
/* g_base64_decode() needs it null-terminated */
|
|
|
|
REALLOC(tmp, sizeof(char) * (out_len + 1));
|
|
|
|
memcpy(tmp, base_tmp, out_len);
|
|
|
|
tmp[out_len] = '\0';
|
|
|
|
|
2014-05-27 23:09:52 +00:00
|
|
|
base64_decoded = g_base64_decode((const gchar *)tmp, &out_len);
|
2014-05-28 00:04:45 +00:00
|
|
|
|
2014-05-27 23:09:52 +00:00
|
|
|
new_string->ptr = (char *)base64_decoded;
|
|
|
|
new_string->len = (unsigned long)(out_len);
|
|
|
|
|
|
|
|
poly_array = ntru_malloc(sizeof(**poly_array) *
|
2014-05-28 18:57:40 +00:00
|
|
|
(new_string->len / ctx->N));
|
2014-05-27 23:09:52 +00:00
|
|
|
|
|
|
|
while (i < new_string->len) {
|
|
|
|
uint32_t j = 0;
|
|
|
|
fmpz_poly_t *new_poly = ntru_malloc(sizeof(*new_poly));
|
|
|
|
|
|
|
|
fmpz_poly_init(*new_poly);
|
|
|
|
|
|
|
|
while (j < ctx->N) {
|
|
|
|
fmpz_poly_set_coeff_si(*new_poly,
|
|
|
|
j,
|
|
|
|
(uint8_t)(base64_decoded[i]));
|
|
|
|
j++;
|
|
|
|
i++;
|
|
|
|
}
|
2014-05-26 23:08:17 +00:00
|
|
|
|
2014-05-27 23:09:52 +00:00
|
|
|
/* fill the last poly with q (which is a non-standard
|
|
|
|
* coefficient) */
|
|
|
|
for (uint32_t k = j; k < ctx->N; k++) {
|
|
|
|
fmpz_poly_set_coeff_si(*new_poly,
|
|
|
|
k,
|
|
|
|
ctx->q);
|
|
|
|
}
|
|
|
|
|
|
|
|
poly_array[polyc] = new_poly;
|
|
|
|
polyc++;
|
|
|
|
}
|
|
|
|
|
|
|
|
poly_array[polyc] = NULL;
|
2014-05-26 23:08:17 +00:00
|
|
|
|
2014-05-27 23:09:52 +00:00
|
|
|
string_delete(new_string);
|
2014-05-28 00:04:45 +00:00
|
|
|
free(base_tmp);
|
|
|
|
free(tmp);
|
2014-05-27 23:09:52 +00:00
|
|
|
|
|
|
|
return poly_array;
|
|
|
|
}
|
2014-05-26 23:08:17 +00:00
|
|
|
|
2014-05-27 23:17:19 +00:00
|
|
|
/*------------------------------------------------------------------------*/
|
|
|
|
|
2014-05-26 23:08:17 +00:00
|
|
|
string *
|
2014-05-27 23:09:52 +00:00
|
|
|
poly_to_base64(fmpz_poly_t poly,
|
2014-05-26 23:08:17 +00:00
|
|
|
ntru_context *ctx)
|
|
|
|
{
|
|
|
|
string *result_string = ntru_malloc(sizeof(*result_string));
|
2014-05-28 00:04:45 +00:00
|
|
|
string *string_rep = NULL;
|
|
|
|
gchar *base64_string = NULL,
|
|
|
|
*tmp = NULL;
|
2014-05-26 23:08:17 +00:00
|
|
|
|
2014-05-28 00:04:45 +00:00
|
|
|
string_rep = poly_to_ascii(poly, ctx);
|
|
|
|
|
|
|
|
tmp = g_base64_encode((const guchar *)string_rep->ptr,
|
|
|
|
string_rep->len);
|
2014-05-26 23:08:17 +00:00
|
|
|
|
2014-05-28 00:04:45 +00:00
|
|
|
base64_string = g_base64_encode((const guchar *)tmp,
|
|
|
|
strlen(tmp));
|
2014-05-27 23:09:52 +00:00
|
|
|
|
|
|
|
result_string->ptr = base64_string;
|
|
|
|
result_string->len = strlen(base64_string);
|
|
|
|
|
2014-05-30 17:54:50 +00:00
|
|
|
string_delete(string_rep);
|
2014-05-28 00:04:45 +00:00
|
|
|
free(tmp);
|
2014-05-26 23:08:17 +00:00
|
|
|
|
|
|
|
return result_string;
|
2014-05-18 16:08:36 +00:00
|
|
|
}
|
2014-05-26 18:59:12 +00:00
|
|
|
|
2014-05-27 23:17:19 +00:00
|
|
|
/*------------------------------------------------------------------------*/
|
|
|
|
|
2014-05-26 18:59:12 +00:00
|
|
|
string *
|
2014-05-27 23:09:52 +00:00
|
|
|
poly_arr_to_base64(fmpz_poly_t **poly_array,
|
2014-05-26 18:59:12 +00:00
|
|
|
ntru_context *ctx)
|
|
|
|
{
|
|
|
|
fmpz_poly_t *ascii_poly;
|
|
|
|
char *string_rep = NULL;
|
2014-05-26 23:08:17 +00:00
|
|
|
size_t string_len = 0;
|
2014-05-26 18:59:12 +00:00
|
|
|
string *result_string = ntru_malloc(sizeof(*result_string));
|
|
|
|
size_t old_length = 0,
|
|
|
|
new_length;
|
2014-05-28 00:04:45 +00:00
|
|
|
gchar *base64_string = NULL,
|
|
|
|
*tmp = NULL;
|
2014-05-26 18:59:12 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* parse the polynomial coefficients into a string
|
|
|
|
*/
|
2014-05-26 23:08:17 +00:00
|
|
|
string_rep = ntru_calloc(1, CHAR_SIZE * (ctx->N + 1));
|
2014-05-26 18:59:12 +00:00
|
|
|
while ((ascii_poly = *poly_array++)) {
|
2014-05-27 23:09:52 +00:00
|
|
|
string *poly_str;
|
2014-05-26 23:08:17 +00:00
|
|
|
|
2014-05-28 00:04:45 +00:00
|
|
|
poly_str = poly_to_ascii(*ascii_poly, ctx);
|
2014-05-26 18:59:12 +00:00
|
|
|
|
2014-05-27 23:09:52 +00:00
|
|
|
new_length = CHAR_SIZE * poly_str->len;
|
2014-05-26 18:59:12 +00:00
|
|
|
REALLOC(string_rep,
|
|
|
|
old_length +
|
2014-05-27 23:09:52 +00:00
|
|
|
new_length);
|
2014-05-26 18:59:12 +00:00
|
|
|
old_length += new_length;
|
|
|
|
|
2014-05-26 23:08:17 +00:00
|
|
|
memcpy(string_rep + string_len,
|
2014-05-27 23:09:52 +00:00
|
|
|
poly_str->ptr,
|
|
|
|
poly_str->len);
|
|
|
|
string_len += poly_str->len;
|
|
|
|
|
|
|
|
string_delete(poly_str);
|
2014-05-26 18:59:12 +00:00
|
|
|
}
|
|
|
|
|
2014-05-28 00:04:45 +00:00
|
|
|
tmp = g_base64_encode((const guchar *)string_rep, string_len);
|
|
|
|
base64_string = g_base64_encode((const guchar *)tmp,
|
|
|
|
strlen(tmp));
|
|
|
|
|
|
|
|
result_string->ptr = base64_string;
|
|
|
|
result_string->len = strlen(base64_string);
|
|
|
|
|
|
|
|
free(string_rep);
|
|
|
|
free(tmp);
|
2014-05-26 18:59:12 +00:00
|
|
|
|
|
|
|
return result_string;
|
|
|
|
}
|
2014-05-27 23:17:19 +00:00
|
|
|
|
|
|
|
/*------------------------------------------------------------------------*/
|