ntru: Replaced ntru_drbg by drbg

This commit is contained in:
Andreas Steffen
2019-10-16 16:46:24 +02:00
parent 737375a2d2
commit 6d3a743d90
10 changed files with 31 additions and 679 deletions
@@ -13,7 +13,6 @@ endif
libstrongswan_ntru_la_SOURCES = \
ntru_plugin.h ntru_plugin.c \
ntru_convert.h ntru_convert.c \
ntru_drbg.h ntru_drbg.c \
ntru_ke.h ntru_ke.c \
ntru_param_set.h ntru_param_set.c \
ntru_poly.h ntru_poly.c \
-295
View File
@@ -1,295 +0,0 @@
/*
* Copyright (C) 2013 Andreas Steffen
* HSR Hochschule fuer Technik Rapperswil
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
*
* This program 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 General Public License
* for more details.
*/
#include "ntru_drbg.h"
#include <utils/debug.h>
#include <utils/test.h>
#define MAX_STRENGTH_BITS 256
#define MAX_DRBG_REQUESTS 0xfffffffe
typedef struct private_ntru_drbg_t private_ntru_drbg_t;
/**
* Private data of an ntru_drbg_t object.
*/
struct private_ntru_drbg_t {
/**
* Public ntru_drbg_t interface.
*/
ntru_drbg_t public;
/**
* Security strength in bits of the DRBG
*/
uint32_t strength;
/**
* Number of requests for pseudorandom bits
*/
uint32_t reseed_counter;
/**
* Maximum number of requests for pseudorandom bits
*/
uint32_t max_requests;
/**
* True entropy source
*/
rng_t *entropy;
/**
* HMAC-SHA256
*/
signer_t *hmac;
/**
* Internal state of HMAC-SHA256: key
*/
chunk_t key;
/**
* Internal state of HMAC-SHA256: value
*/
chunk_t value;
/**
* reference count
*/
refcount_t ref;
};
/**
* Update the internal state of the HMAC_DRBG
*/
static bool update(private_ntru_drbg_t *this, chunk_t data)
{
chunk_t ch_00 = chunk_from_chars(0x00);
chunk_t ch_01 = chunk_from_chars(0x01);
if (!this->hmac->set_key(this->hmac, this->key) ||
!this->hmac->get_signature(this->hmac, this->value, NULL) ||
!this->hmac->get_signature(this->hmac, ch_00, NULL) ||
!this->hmac->get_signature(this->hmac, data, this->key.ptr) ||
!this->hmac->set_key(this->hmac, this->key) ||
!this->hmac->get_signature(this->hmac, this->value,
this->value.ptr))
{
return FALSE;
}
if (data.len > 0)
{
if (!this->hmac->set_key(this->hmac, this->key) ||
!this->hmac->get_signature(this->hmac, this->value, NULL) ||
!this->hmac->get_signature(this->hmac, ch_01, NULL) ||
!this->hmac->get_signature(this->hmac, data, this->key.ptr) ||
!this->hmac->set_key(this->hmac, this->key) ||
!this->hmac->get_signature(this->hmac, this->value,
this->value.ptr))
{
return FALSE;
}
}
DBG4(DBG_LIB, "HMAC_DRBG V: %B", &this->value);
DBG4(DBG_LIB, "HMAC_DRBG K: %B", &this->key);
return TRUE;
}
METHOD(ntru_drbg_t, get_strength, uint32_t,
private_ntru_drbg_t *this)
{
return this->strength;
}
METHOD(ntru_drbg_t, reseed, bool,
private_ntru_drbg_t *this)
{
chunk_t seed;
seed = chunk_alloc(this->strength / BITS_PER_BYTE);
DBG2(DBG_LIB, "DRBG requests %u bytes of entropy", seed.len);
if (!this->entropy->get_bytes(this->entropy, seed.len, seed.ptr))
{
chunk_free(&seed);
return FALSE;
}
if (!update(this, seed))
{
chunk_free(&seed);
return FALSE;
}
chunk_clear(&seed);
this->reseed_counter = 1;
return TRUE;
}
METHOD(ntru_drbg_t, generate, bool,
private_ntru_drbg_t *this, uint32_t strength, uint32_t len, uint8_t *out)
{
size_t delta;
chunk_t output;
DBG2(DBG_LIB, "DRBG generates %u pseudorandom bytes", len);
if (!out || len == 0)
{
return FALSE;
}
output = chunk_create(out, len);
if (this->reseed_counter > this->max_requests)
{
if (!reseed(this))
{
return FALSE;
}
}
while (len)
{
if (!this->hmac->get_signature(this->hmac, this->value,
this->value.ptr))
{
return FALSE;
}
delta = min(len, this->value.len);
memcpy(out, this->value.ptr, delta);
len -= delta;
out += delta;
}
DBG4(DBG_LIB, "HMAC_DRBG Out: %B", &output);
if (!update(this, chunk_empty))
{
return FALSE;
}
this->reseed_counter++;
return TRUE;
}
METHOD(ntru_drbg_t, get_ref, ntru_drbg_t*,
private_ntru_drbg_t *this)
{
ref_get(&this->ref);
return &this->public;
}
METHOD(ntru_drbg_t, destroy, void,
private_ntru_drbg_t *this)
{
if (ref_put(&this->ref))
{
this->hmac->destroy(this->hmac);
chunk_clear(&this->key);
chunk_clear(&this->value);
free(this);
}
}
/*
* Described in header.
*/
ntru_drbg_t *ntru_drbg_create(uint32_t strength, chunk_t pers_str,
rng_t *entropy)
{
private_ntru_drbg_t *this;
chunk_t seed;
signer_t *hmac;
size_t entropy_len;
uint32_t max_requests;
if (strength > MAX_STRENGTH_BITS)
{
return NULL;
}
if (strength <= 112)
{
strength = 112;
}
else if (strength <= 128)
{
strength = 128;
}
else if (strength <= 192)
{
strength = 192;
}
else
{
strength = 256;
}
hmac = lib->crypto->create_signer(lib->crypto, AUTH_HMAC_SHA2_256_256);
if (!hmac)
{
DBG1(DBG_LIB, "could not instantiate HMAC-SHA256");
return NULL;
}
max_requests = lib->settings->get_int(lib->settings,
"%s.plugins.ntru.max_drbg_requests",
MAX_DRBG_REQUESTS, lib->ns);
INIT(this,
.public = {
.get_strength = _get_strength,
.reseed = _reseed,
.generate = _generate,
.get_ref = _get_ref,
.destroy = _destroy,
},
.strength = strength,
.entropy = entropy,
.hmac = hmac,
.key = chunk_alloc(hmac->get_key_size(hmac)),
.value = chunk_alloc(hmac->get_block_size(hmac)),
.max_requests = max_requests,
.reseed_counter = 1,
.ref = 1,
);
memset(this->key.ptr, 0x00, this->key.len);
memset(this->value.ptr, 0x01, this->value.len);
entropy_len = (strength + strength/2) / BITS_PER_BYTE;
seed = chunk_alloc(entropy_len + pers_str.len);
DBG2(DBG_LIB, "DRBG requests %u bytes of entropy", entropy_len);
if (!this->entropy->get_bytes(this->entropy, entropy_len, seed.ptr))
{
chunk_free(&seed);
destroy(this);
return NULL;
}
memcpy(seed.ptr + entropy_len, pers_str.ptr, pers_str.len);
DBG4(DBG_LIB, "seed: %B", &seed);
if (!update(this, seed))
{
chunk_free(&seed);
destroy(this);
return NULL;
}
chunk_clear(&seed);
return &this->public;
}
EXPORT_FUNCTION_FOR_TESTS(ntru, ntru_drbg_create);
@@ -1,84 +0,0 @@
/*
* Copyright (C) 2013 Andreas Steffen
* HSR Hochschule fuer Technik Rapperswil
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
*
* This program 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 General Public License
* for more details.
*/
/**
* @defgroup ntru_drbg ntru_drbg
* @{ @ingroup ntru_p
*/
#ifndef NTRU_DRBG_H_
#define NTRU_DRBG_H_
typedef struct ntru_drbg_t ntru_drbg_t;
#include <library.h>
/**
* Implements a HMAC Deterministic Random Bit Generator (HMAC_DRBG)
* compliant with NIST SP 800-90A
*/
struct ntru_drbg_t {
/**
* Reseed the instantiated DRBG
*
* @return configured security strength in bits
*/
uint32_t (*get_strength)(ntru_drbg_t *this);
/**
* Reseed the instantiated DRBG
*
* @return TRUE if successful
*/
bool (*reseed)(ntru_drbg_t *this);
/**
* Generate pseudorandom bytes.
* If the maximum number of requests has been reached, reseeding occurs
*
* @param strength requested security strength in bits
* @param len number of octets to generate
* @param out address of output buffer
* @return TRUE if successful
*/
bool (*generate)(ntru_drbg_t *this, uint32_t strength, uint32_t len,
uint8_t *out);
/**
* Get a reference on an ntru_drbg_t object increasing the count by one
*
* @return reference to the ntru_drbg_t object
*/
ntru_drbg_t* (*get_ref)(ntru_drbg_t *this);
/**
* Uninstantiate and destroy the DRBG object
*/
void (*destroy)(ntru_drbg_t *this);
};
/**
* Create and instantiate a new DRBG object.
*
* @param strength security strength in bits
* @param pers_str personalization string
* @param entropy entropy source to use
*/
ntru_drbg_t *ntru_drbg_create(uint32_t strength, chunk_t pers_str,
rng_t *entropy);
#endif /** NTRU_DRBG_H_ @}*/
+7 -6
View File
@@ -14,12 +14,12 @@
*/
#include "ntru_ke.h"
#include "ntru_drbg.h"
#include "ntru_param_set.h"
#include "ntru_private_key.h"
#include "ntru_public_key.h"
#include <crypto/diffie_hellman.h>
#include <crypto/drbgs/drbg.h>
#include <utils/debug.h>
typedef struct private_ntru_ke_t private_ntru_ke_t;
@@ -106,7 +106,7 @@ struct private_ntru_ke_t {
/**
* Deterministic Random Bit Generator
*/
ntru_drbg_t *drbg;
drbg_t *drbg;
};
METHOD(diffie_hellman_t, get_my_public_value, bool,
@@ -199,8 +199,8 @@ METHOD(diffie_hellman_t, set_other_public_value, bool,
this->shared_secret = chunk_alloc(2 * this->strength / BITS_PER_BYTE);
/* generate the random shared secret */
if (!this->drbg->generate(this->drbg, this->strength,
this->shared_secret.len, this->shared_secret.ptr))
if (!this->drbg->generate(this->drbg, this->shared_secret.len,
this->shared_secret.ptr))
{
DBG1(DBG_LIB, "generation of shared secret failed");
chunk_free(&this->shared_secret);
@@ -246,7 +246,7 @@ ntru_ke_t *ntru_ke_create(diffie_hellman_group_t group, chunk_t g, chunk_t p)
const ntru_param_set_id_t *param_sets;
ntru_param_set_id_t param_set_id;
rng_t *entropy;
ntru_drbg_t *drbg;
drbg_t *drbg;
char *parameter_set;
uint32_t strength;
@@ -301,7 +301,8 @@ ntru_ke_t *ntru_ke_create(diffie_hellman_group_t group, chunk_t g, chunk_t p)
return NULL;
}
drbg = ntru_drbg_create(strength, chunk_from_str("IKE NTRU-KE"), entropy);
drbg = lib->crypto->create_drbg(lib->crypto, DRBG_HMAC_SHA256, strength,
entropy, chunk_from_str("IKE NTRU-KE"));
if (!drbg)
{
DBG1(DBG_LIB, "could not instantiate DRBG at %u bit security", strength);
@@ -58,7 +58,7 @@ struct private_ntru_private_key_t {
/**
* Deterministic Random Bit Generator
*/
ntru_drbg_t *drbg;
drbg_t *drbg;
};
@@ -640,7 +640,7 @@ static bool ring_inv(uint16_t *a, uint16_t N, uint16_t q, uint16_t *t,
/*
* Described in header.
*/
ntru_private_key_t *ntru_private_key_create(ntru_drbg_t *drbg,
ntru_private_key_t *ntru_private_key_create(drbg_t *drbg,
const ntru_param_set_t *params)
{
private_ntru_private_key_t *this;
@@ -671,8 +671,7 @@ ntru_private_key_t *ntru_private_key_create(ntru_drbg_t *drbg,
seed =chunk_alloc(params->sec_strength_len + 8);
/* get random seed for generating trinary F as a list of indices */
if (!drbg->generate(drbg, params->sec_strength_len * BITS_PER_BYTE,
seed.len, seed.ptr))
if (!drbg->generate(drbg, seed.len, seed.ptr))
{
goto err;
}
@@ -715,8 +714,7 @@ ntru_private_key_t *ntru_private_key_create(ntru_drbg_t *drbg,
}
/* get random seed for generating trinary g as a list of indices */
if (!drbg->generate(drbg, params->sec_strength_len * BITS_PER_BYTE,
seed.len, seed.ptr))
if (!drbg->generate(drbg, seed.len, seed.ptr))
{
goto err;
}
@@ -760,7 +758,7 @@ err:
/*
* Described in header.
*/
ntru_private_key_t *ntru_private_key_create_from_data(ntru_drbg_t *drbg,
ntru_private_key_t *ntru_private_key_create_from_data(drbg_t *drbg,
chunk_t data)
{
private_ntru_private_key_t *this;
@@ -23,11 +23,11 @@
typedef struct ntru_private_key_t ntru_private_key_t;
#include "ntru_drbg.h"
#include "ntru_param_set.h"
#include "ntru_public_key.h"
#include <library.h>
#include <crypto/drbgs/drbg.h>
/**
* Implements an NTRU encryption public/private key pair
@@ -77,7 +77,7 @@ struct ntru_private_key_t {
* @param drbg Digital Random Bit Generator used for key generation
* @param params NTRU encryption parameter set to be used
*/
ntru_private_key_t *ntru_private_key_create(ntru_drbg_t *drbg,
ntru_private_key_t *ntru_private_key_create(drbg_t *drbg,
const ntru_param_set_t *params);
/**
@@ -86,7 +86,7 @@ ntru_private_key_t *ntru_private_key_create(ntru_drbg_t *drbg,
* @param drbg Deterministic random bit generator
* @param data Encoded NTRU private key
*/
ntru_private_key_t *ntru_private_key_create_from_data(ntru_drbg_t *drbg,
ntru_private_key_t *ntru_private_key_create_from_data(drbg_t *drbg,
chunk_t data);
#endif /** NTRU_PRIVATE_KEY_H_ @}*/
@@ -52,7 +52,7 @@ struct private_ntru_public_key_t {
/**
* Deterministic Random Bit Generator
*/
ntru_drbg_t *drbg;
drbg_t *drbg;
};
@@ -152,9 +152,7 @@ METHOD(ntru_public_key_t, encrypt, bool,
/* loop until a message representative with proper weight is achieved */
do
{
if (!this->drbg->generate(this->drbg,
this->params->sec_strength_len * BITS_PER_BYTE,
this->params->sec_strength_len, b))
if (!this->drbg->generate(this->drbg, this->params->sec_strength_len, b))
{
goto err;
}
@@ -319,7 +317,7 @@ METHOD(ntru_public_key_t, destroy, void,
/*
* Described in header.
*/
ntru_public_key_t *ntru_public_key_create(ntru_drbg_t *drbg,
ntru_public_key_t *ntru_public_key_create(drbg_t *drbg,
const ntru_param_set_t *params,
uint16_t *pubkey)
{
@@ -352,7 +350,7 @@ ntru_public_key_t *ntru_public_key_create(ntru_drbg_t *drbg,
/*
* Described in header.
*/
ntru_public_key_t *ntru_public_key_create_from_data(ntru_drbg_t *drbg,
ntru_public_key_t *ntru_public_key_create_from_data(drbg_t *drbg,
chunk_t data)
{
private_ntru_public_key_t *this;
@@ -24,9 +24,9 @@
typedef struct ntru_public_key_t ntru_public_key_t;
#include "ntru_param_set.h"
#include "ntru_drbg.h"
#include <library.h>
#include <crypto/drbgs/drbg.h>
/**
* Implements an NTRU encryption public key
@@ -70,7 +70,7 @@ struct ntru_public_key_t {
* @param params NTRU encryption parameter set to be used
* @param pubkey Coefficients of public key polynomial h
*/
ntru_public_key_t *ntru_public_key_create(ntru_drbg_t *drbg,
ntru_public_key_t *ntru_public_key_create(drbg_t *drbg,
const ntru_param_set_t *params,
uint16_t *pubkey);
@@ -80,7 +80,7 @@ ntru_public_key_t *ntru_public_key_create(ntru_drbg_t *drbg,
* @param drbg Deterministic random bit generator
* @param data Encoded NTRU public key
*/
ntru_public_key_t *ntru_public_key_create_from_data(ntru_drbg_t *drbg,
ntru_public_key_t *ntru_public_key_create_from_data(drbg_t *drbg,
chunk_t data);