mgf1: Refactored MGF1 as an XOF
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (C) 2014 Andreas Steffen
|
||||
* Copyright (C) 2014-2016 Andreas Steffen
|
||||
* HSR Hochschule fuer Technik Rapperswil
|
||||
*
|
||||
* Copyright (C) 2009-2013 Security Innovation
|
||||
@@ -17,7 +17,7 @@
|
||||
|
||||
#include "ntru_poly.h"
|
||||
|
||||
#include <crypto/mgf1/mgf1_bitspender.h>
|
||||
#include <crypto/xofs/xof_bitspender.h>
|
||||
#include <utils/debug.h>
|
||||
#include <utils/test.h>
|
||||
|
||||
@@ -290,8 +290,9 @@ static private_ntru_poly_t* ntru_poly_create(uint16_t N, uint16_t q,
|
||||
/*
|
||||
* Described in header.
|
||||
*/
|
||||
ntru_poly_t *ntru_poly_create_from_seed(hash_algorithm_t alg, chunk_t seed,
|
||||
uint8_t c_bits, uint16_t N, uint16_t q,
|
||||
ntru_poly_t *ntru_poly_create_from_seed(ext_out_function_t mgf1_type,
|
||||
chunk_t seed, uint8_t c_bits,
|
||||
uint16_t N, uint16_t q,
|
||||
uint32_t indices_len_p,
|
||||
uint32_t indices_len_m,
|
||||
bool is_product_form)
|
||||
@@ -300,9 +301,9 @@ ntru_poly_t *ntru_poly_create_from_seed(hash_algorithm_t alg, chunk_t seed,
|
||||
int n, num_indices, index_i = 0;
|
||||
uint32_t index, limit;
|
||||
uint8_t *used;
|
||||
mgf1_bitspender_t *bitspender;
|
||||
xof_bitspender_t *bitspender;
|
||||
|
||||
bitspender = mgf1_bitspender_create(alg, seed, TRUE);
|
||||
bitspender = xof_bitspender_create(mgf1_type, seed, TRUE);
|
||||
if (!bitspender)
|
||||
{
|
||||
return NULL;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (C) 2014 Andreas Steffen
|
||||
* Copyright (C) 2014-2016 Andreas Steffen
|
||||
* HSR Hochschule fuer Technik Rapperswil
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it
|
||||
@@ -24,6 +24,7 @@
|
||||
typedef struct ntru_poly_t ntru_poly_t;
|
||||
|
||||
#include <library.h>
|
||||
#include <crypto/xofs/xof.h>
|
||||
|
||||
/**
|
||||
* Implements a trinary polynomial storing the indices of non-zero coefficients
|
||||
@@ -63,9 +64,9 @@ struct ntru_poly_t {
|
||||
};
|
||||
|
||||
/**
|
||||
* Create a trits polynomial from a seed using MGF1 with a base hash function
|
||||
* Create a trits polynomial from a seed using MGF1
|
||||
*
|
||||
* @param alg hash algorithm to be used by MGF1
|
||||
* @param alg MGF1 algorithm used(XOF_MGF1_SHA1 or XOF_MGF_SHA256)
|
||||
* @param seed seed used by MGF1 to generate trits from
|
||||
* @param N ring dimension, number of polynomial coefficients
|
||||
* @param q large modulus
|
||||
@@ -74,7 +75,7 @@ struct ntru_poly_t {
|
||||
* @param indices_len_m number of indices for -1 coefficients
|
||||
* @param is_product_form generate multiple polynomials
|
||||
*/
|
||||
ntru_poly_t *ntru_poly_create_from_seed(hash_algorithm_t alg, chunk_t seed,
|
||||
ntru_poly_t *ntru_poly_create_from_seed(ext_out_function_t alg, chunk_t seed,
|
||||
uint8_t c_bits, uint16_t N, uint16_t q,
|
||||
uint32_t indices_len_p,
|
||||
uint32_t indices_len_m,
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (C) 2014 Andreas Steffen
|
||||
* Copyright (C) 2014-2016 Andreas Steffen
|
||||
* HSR Hochschule fuer Technik Rapperswil
|
||||
*
|
||||
* Copyright (C) 2009-2013 Security Innovation
|
||||
@@ -178,7 +178,7 @@ bool ntru_check_min_weight(uint16_t N, uint8_t *t, uint16_t min_wt)
|
||||
METHOD(ntru_private_key_t, decrypt, bool,
|
||||
private_ntru_private_key_t *this, chunk_t ciphertext, chunk_t *plaintext)
|
||||
{
|
||||
hash_algorithm_t hash_algid;
|
||||
ext_out_function_t alg;
|
||||
size_t t_len, seed1_len, seed2_len;
|
||||
uint16_t *t1, *t2, *t = NULL;
|
||||
uint16_t mod_q_mask, q_mod_p, cmprime_len, cm_len = 0, num_zeros;
|
||||
@@ -206,9 +206,9 @@ METHOD(ntru_private_key_t, decrypt, bool,
|
||||
Mtrin = (uint8_t *)t1;
|
||||
M = Mtrin + this->params->N;
|
||||
|
||||
/* set hash algorithm based on security strength */
|
||||
hash_algid = (this->params->sec_strength_len <= 20) ? HASH_SHA1 :
|
||||
HASH_SHA256;
|
||||
/* set MGF1 algorithm type based on security strength */
|
||||
alg = (this->params->sec_strength_len <= 20) ? XOF_MGF1_SHA1 :
|
||||
XOF_MGF1_SHA256;
|
||||
|
||||
/* set constants */
|
||||
mod_q_mask = this->params->q - 1;
|
||||
@@ -307,7 +307,7 @@ METHOD(ntru_private_key_t, decrypt, bool,
|
||||
ntru_coeffs_mod4_2_octets(this->params->N, t2, seed.ptr);
|
||||
|
||||
/* form mask */
|
||||
mask = ntru_trits_create(this->params->N, hash_algid, seed);
|
||||
mask = ntru_trits_create(this->params->N, alg, seed);
|
||||
if (!mask)
|
||||
{
|
||||
DBG1(DBG_LIB, "mask creation failed");
|
||||
@@ -390,9 +390,8 @@ METHOD(ntru_private_key_t, decrypt, bool,
|
||||
|
||||
/* generate cr */
|
||||
DBG2(DBG_LIB, "generate polynomial r");
|
||||
r_poly = ntru_poly_create_from_seed(hash_algid, seed,
|
||||
this->params->c_bits, this->params->N,
|
||||
this->params->q, this->params->dF_r,
|
||||
r_poly = ntru_poly_create_from_seed(alg, seed, this->params->c_bits,
|
||||
this->params->N, this->params->q, this->params->dF_r,
|
||||
this->params->dF_r, this->params->is_product_form);
|
||||
if (!r_poly)
|
||||
{
|
||||
@@ -648,7 +647,7 @@ ntru_private_key_t *ntru_private_key_create(ntru_drbg_t *drbg,
|
||||
size_t t_len;
|
||||
uint16_t *t1, *t2, *t = NULL;
|
||||
uint16_t mod_q_mask;
|
||||
hash_algorithm_t hash_algid;
|
||||
ext_out_function_t alg;
|
||||
ntru_poly_t *g_poly;
|
||||
chunk_t seed;
|
||||
int i;
|
||||
@@ -667,14 +666,8 @@ ntru_private_key_t *ntru_private_key_create(ntru_drbg_t *drbg,
|
||||
);
|
||||
|
||||
/* set hash algorithm and seed length based on security strength */
|
||||
if (params->sec_strength_len <= 20)
|
||||
{
|
||||
hash_algid = HASH_SHA1;
|
||||
}
|
||||
else
|
||||
{
|
||||
hash_algid = HASH_SHA256;
|
||||
}
|
||||
alg = (params->sec_strength_len <= 20) ? XOF_MGF1_SHA1 :
|
||||
XOF_MGF1_SHA256;
|
||||
seed =chunk_alloc(params->sec_strength_len + 8);
|
||||
|
||||
/* get random seed for generating trinary F as a list of indices */
|
||||
@@ -685,7 +678,7 @@ ntru_private_key_t *ntru_private_key_create(ntru_drbg_t *drbg,
|
||||
}
|
||||
|
||||
DBG2(DBG_LIB, "generate polynomial F");
|
||||
this->privkey = ntru_poly_create_from_seed(hash_algid, seed, params->c_bits,
|
||||
this->privkey = ntru_poly_create_from_seed(alg, seed, params->c_bits,
|
||||
params->N, params->q,
|
||||
params->dF_r, params->dF_r,
|
||||
params->is_product_form);
|
||||
@@ -729,7 +722,7 @@ ntru_private_key_t *ntru_private_key_create(ntru_drbg_t *drbg,
|
||||
}
|
||||
|
||||
DBG2(DBG_LIB, "generate polynomial g");
|
||||
g_poly = ntru_poly_create_from_seed(hash_algid, seed, params->c_bits,
|
||||
g_poly = ntru_poly_create_from_seed(alg, seed, params->c_bits,
|
||||
params->N, params->q, params->dg + 1,
|
||||
params->dg, FALSE);
|
||||
if (!g_poly)
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (C) 2014 Andreas Steffen
|
||||
* Copyright (C) 2014-2016 Andreas Steffen
|
||||
* HSR Hochschule fuer Technik Rapperswil
|
||||
*
|
||||
* Copyright (C) 2009-2013 Security Innovation
|
||||
@@ -102,7 +102,7 @@ extern bool ntru_check_min_weight(uint16_t N, uint8_t *t, uint16_t min_wt);
|
||||
METHOD(ntru_public_key_t, encrypt, bool,
|
||||
private_ntru_public_key_t *this, chunk_t plaintext, chunk_t *ciphertext)
|
||||
{
|
||||
hash_algorithm_t hash_algid;
|
||||
ext_out_function_t alg;
|
||||
size_t t_len, seed1_len, seed2_len;
|
||||
uint16_t *t1, *t = NULL;
|
||||
uint8_t b[MAX_SEC_STRENGTH_LEN];
|
||||
@@ -139,8 +139,8 @@ METHOD(ntru_public_key_t, encrypt, bool,
|
||||
M = Mtrin + this->params->N;
|
||||
|
||||
/* set hash algorithm based on security strength */
|
||||
hash_algid = (this->params->sec_strength_len <= 20) ? HASH_SHA1 :
|
||||
HASH_SHA256;
|
||||
alg = (this->params->sec_strength_len <= 20) ? XOF_MGF1_SHA1 :
|
||||
XOF_MGF1_SHA256;
|
||||
/* set constants */
|
||||
mod_q_mask = this->params->q - 1;
|
||||
|
||||
@@ -173,7 +173,7 @@ METHOD(ntru_public_key_t, encrypt, bool,
|
||||
seed.len = seed2_len;
|
||||
|
||||
DBG2(DBG_LIB, "generate polynomial r");
|
||||
r_poly = ntru_poly_create_from_seed(hash_algid, seed, this->params->c_bits,
|
||||
r_poly = ntru_poly_create_from_seed(alg, seed, this->params->c_bits,
|
||||
this->params->N, this->params->q,
|
||||
this->params->dF_r, this->params->dF_r,
|
||||
this->params->is_product_form);
|
||||
@@ -191,7 +191,7 @@ METHOD(ntru_public_key_t, encrypt, bool,
|
||||
seed.len = seed1_len;
|
||||
|
||||
/* form mask */
|
||||
mask = ntru_trits_create(this->params->N, hash_algid, seed);
|
||||
mask = ntru_trits_create(this->params->N, alg, seed);
|
||||
if (!mask)
|
||||
{
|
||||
DBG1(DBG_LIB, "mask creation failed");
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (C) 2013-2014 Andreas Steffen
|
||||
* Copyright (C) 2013-2016 Andreas Steffen
|
||||
* HSR Hochschule fuer Technik Rapperswil
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it
|
||||
@@ -16,7 +16,7 @@
|
||||
#include "ntru_trits.h"
|
||||
#include "ntru_convert.h"
|
||||
|
||||
#include <crypto/mgf1/mgf1_bitspender.h>
|
||||
#include <crypto/xofs/xof_bitspender.h>
|
||||
#include <utils/debug.h>
|
||||
#include <utils/test.h>
|
||||
|
||||
@@ -67,14 +67,15 @@ METHOD(ntru_trits_t, destroy, void,
|
||||
/*
|
||||
* Described in header.
|
||||
*/
|
||||
ntru_trits_t *ntru_trits_create(size_t len, hash_algorithm_t alg, chunk_t seed)
|
||||
ntru_trits_t *ntru_trits_create(size_t len, ext_out_function_t alg,
|
||||
chunk_t seed)
|
||||
{
|
||||
private_ntru_trits_t *this;
|
||||
uint8_t octet, buf[5], *trits;
|
||||
size_t trits_needed;
|
||||
mgf1_bitspender_t *bitspender;
|
||||
xof_bitspender_t *bitspender;
|
||||
|
||||
bitspender = mgf1_bitspender_create(alg, seed, TRUE);
|
||||
bitspender = xof_bitspender_create(alg, seed, TRUE);
|
||||
if (!bitspender)
|
||||
{
|
||||
return NULL;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (C) 2013 Andreas Steffen
|
||||
* Copyright (C) 2013-2016 Andreas Steffen
|
||||
* HSR Hochschule fuer Technik Rapperswil
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it
|
||||
@@ -24,6 +24,7 @@
|
||||
typedef struct ntru_trits_t ntru_trits_t;
|
||||
|
||||
#include <library.h>
|
||||
#include <crypto/xofs/xof.h>
|
||||
|
||||
/**
|
||||
* Implements an array of trinary elements (trits)
|
||||
@@ -52,10 +53,11 @@ struct ntru_trits_t {
|
||||
* Create a trits array from a seed using MGF1 with a base hash function
|
||||
*
|
||||
* @param size size of the trits array
|
||||
* @param alg hash algorithm to be used by MGF1
|
||||
* @param alg MGF1 algorithm used (XOF_MGF1_SHA1 or XOF_MGF_SHA256)
|
||||
* @param seed seed used by MGF1 to generate trits from
|
||||
*/
|
||||
ntru_trits_t *ntru_trits_create(size_t size, hash_algorithm_t alg, chunk_t seed);
|
||||
ntru_trits_t *ntru_trits_create(size_t size, ext_out_function_t alg,
|
||||
chunk_t seed);
|
||||
|
||||
#endif /** NTRU_TRITS_H_ @}*/
|
||||
|
||||
|
||||
Reference in New Issue
Block a user