ASCII->POLY: use double base64 encoding for more entropy
This commit is contained in:
parent
51c6537777
commit
bf192a0498
@ -280,22 +280,56 @@ bin_poly_arr_to_ascii(fmpz_poly_t **bin_poly_arr, ntru_context *ctx)
|
|||||||
|
|
||||||
/*------------------------------------------------------------------------*/
|
/*------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
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));
|
||||||
|
uint32_t i = 0;
|
||||||
|
|
||||||
|
for (uint32_t j = 0; j < ctx->N; j++) {
|
||||||
|
uint8_t coeff = fmpz_poly_get_coeff_ui(poly, j);
|
||||||
|
if (coeff == ctx->q)
|
||||||
|
string_rep[i] = '\0';
|
||||||
|
else
|
||||||
|
string_rep[i] = (char)coeff;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
|
||||||
|
result_string->ptr = string_rep;
|
||||||
|
result_string->len = i;
|
||||||
|
|
||||||
|
return result_string;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*------------------------------------------------------------------------*/
|
||||||
|
|
||||||
fmpz_poly_t **
|
fmpz_poly_t **
|
||||||
base64_to_poly_arr(string *to_poly, ntru_context *ctx)
|
base64_to_poly_arr(string *to_poly, ntru_context *ctx)
|
||||||
{
|
{
|
||||||
uint32_t i = 0,
|
uint32_t i = 0,
|
||||||
polyc = 0;
|
polyc = 0;
|
||||||
gsize out_len;
|
gsize out_len;
|
||||||
guchar *base64_decoded;
|
guchar *base64_decoded = NULL,
|
||||||
|
*base_tmp = NULL;
|
||||||
string *new_string = ntru_malloc(sizeof(*new_string));
|
string *new_string = ntru_malloc(sizeof(*new_string));
|
||||||
fmpz_poly_t **poly_array;
|
fmpz_poly_t **poly_array;
|
||||||
char tmp[to_poly->len + 1];
|
char *tmp = ntru_malloc(sizeof(char) * (to_poly->len + 1));
|
||||||
|
|
||||||
/* g_base64_decode() needs it null-terminated */
|
/* g_base64_decode() needs it null-terminated */
|
||||||
memcpy(tmp, to_poly->ptr, to_poly->len);
|
memcpy(tmp, to_poly->ptr, to_poly->len);
|
||||||
tmp[to_poly->len] = '\0';
|
tmp[to_poly->len] = '\0';
|
||||||
|
|
||||||
|
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';
|
||||||
|
|
||||||
base64_decoded = g_base64_decode((const gchar *)tmp, &out_len);
|
base64_decoded = g_base64_decode((const gchar *)tmp, &out_len);
|
||||||
|
|
||||||
new_string->ptr = (char *)base64_decoded;
|
new_string->ptr = (char *)base64_decoded;
|
||||||
new_string->len = (unsigned long)(out_len);
|
new_string->len = (unsigned long)(out_len);
|
||||||
|
|
||||||
@ -331,6 +365,8 @@ base64_to_poly_arr(string *to_poly, ntru_context *ctx)
|
|||||||
poly_array[polyc] = NULL;
|
poly_array[polyc] = NULL;
|
||||||
|
|
||||||
string_delete(new_string);
|
string_delete(new_string);
|
||||||
|
free(base_tmp);
|
||||||
|
free(tmp);
|
||||||
|
|
||||||
return poly_array;
|
return poly_array;
|
||||||
}
|
}
|
||||||
@ -342,25 +378,23 @@ poly_to_base64(fmpz_poly_t poly,
|
|||||||
ntru_context *ctx)
|
ntru_context *ctx)
|
||||||
{
|
{
|
||||||
string *result_string = ntru_malloc(sizeof(*result_string));
|
string *result_string = ntru_malloc(sizeof(*result_string));
|
||||||
char *string_rep = ntru_malloc(CHAR_SIZE * (ctx->N));
|
string *string_rep = NULL;
|
||||||
uint32_t i = 0;
|
gchar *base64_string = NULL,
|
||||||
gchar *base64_string = NULL;
|
*tmp = NULL;
|
||||||
|
|
||||||
for (uint32_t j = 0; j < ctx->N; j++) {
|
string_rep = poly_to_ascii(poly, ctx);
|
||||||
uint8_t coeff = fmpz_poly_get_coeff_ui(poly, j);
|
|
||||||
if (coeff == ctx->q)
|
|
||||||
string_rep[i] = '\0';
|
|
||||||
else
|
|
||||||
string_rep[i] = (char)coeff;
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
|
|
||||||
base64_string = g_base64_encode((const guchar *)string_rep, i);
|
tmp = g_base64_encode((const guchar *)string_rep->ptr,
|
||||||
|
string_rep->len);
|
||||||
|
|
||||||
|
base64_string = g_base64_encode((const guchar *)tmp,
|
||||||
|
strlen(tmp));
|
||||||
|
|
||||||
result_string->ptr = base64_string;
|
result_string->ptr = base64_string;
|
||||||
result_string->len = strlen(base64_string);
|
result_string->len = strlen(base64_string);
|
||||||
|
|
||||||
free(string_rep);
|
free(string_rep);
|
||||||
|
free(tmp);
|
||||||
|
|
||||||
return result_string;
|
return result_string;
|
||||||
}
|
}
|
||||||
@ -377,6 +411,8 @@ poly_arr_to_base64(fmpz_poly_t **poly_array,
|
|||||||
string *result_string = ntru_malloc(sizeof(*result_string));
|
string *result_string = ntru_malloc(sizeof(*result_string));
|
||||||
size_t old_length = 0,
|
size_t old_length = 0,
|
||||||
new_length;
|
new_length;
|
||||||
|
gchar *base64_string = NULL,
|
||||||
|
*tmp = NULL;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* parse the polynomial coefficients into a string
|
* parse the polynomial coefficients into a string
|
||||||
@ -385,7 +421,7 @@ poly_arr_to_base64(fmpz_poly_t **poly_array,
|
|||||||
while ((ascii_poly = *poly_array++)) {
|
while ((ascii_poly = *poly_array++)) {
|
||||||
string *poly_str;
|
string *poly_str;
|
||||||
|
|
||||||
poly_str = poly_to_base64(*ascii_poly, ctx);
|
poly_str = poly_to_ascii(*ascii_poly, ctx);
|
||||||
|
|
||||||
new_length = CHAR_SIZE * poly_str->len;
|
new_length = CHAR_SIZE * poly_str->len;
|
||||||
REALLOC(string_rep,
|
REALLOC(string_rep,
|
||||||
@ -401,8 +437,15 @@ poly_arr_to_base64(fmpz_poly_t **poly_array,
|
|||||||
string_delete(poly_str);
|
string_delete(poly_str);
|
||||||
}
|
}
|
||||||
|
|
||||||
result_string->ptr = string_rep;
|
tmp = g_base64_encode((const guchar *)string_rep, string_len);
|
||||||
result_string->len = 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);
|
||||||
|
|
||||||
return result_string;
|
return result_string;
|
||||||
}
|
}
|
||||||
|
@ -120,6 +120,23 @@ bin_poly_to_ascii(fmpz_poly_t poly,
|
|||||||
string *
|
string *
|
||||||
bin_poly_arr_to_ascii(fmpz_poly_t **bin_poly_arr, ntru_context *ctx);
|
bin_poly_arr_to_ascii(fmpz_poly_t **bin_poly_arr, ntru_context *ctx);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert a single polynom back to a real string which is
|
||||||
|
* ascii encoded (full 256 C char spectrum).
|
||||||
|
* The polynomial coefficients are expected to be in the range
|
||||||
|
* [0, q-1] and will be casted back to chars without any mapping.
|
||||||
|
*
|
||||||
|
* Trailing q coefficients are only used for filling up the rest of
|
||||||
|
* a polynomial with '\0', so they will not confuse the result.
|
||||||
|
*
|
||||||
|
* @param poly the polynomial to convert
|
||||||
|
* @param ctx the NTRUEncrypt context
|
||||||
|
* @return the real string, newly allocated
|
||||||
|
*/
|
||||||
|
string *
|
||||||
|
poly_to_ascii(fmpz_poly_t poly,
|
||||||
|
ntru_context *ctx);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Convert an base64 encoded string to an array of polyomials with
|
* Convert an base64 encoded string to an array of polyomials with
|
||||||
* coefficients which are expected to be in the range [0, q-1].
|
* coefficients which are expected to be in the range [0, q-1].
|
||||||
|
Loading…
Reference in New Issue
Block a user