drbg: Implemented NIST SP-800-90A DRBG

This commit is contained in:
Andreas Steffen
2019-10-16 16:46:24 +02:00
parent 2a7937f179
commit 737375a2d2
33 changed files with 2825 additions and 37 deletions
@@ -0,0 +1,18 @@
AM_CPPFLAGS = \
-I$(top_srcdir)/src/libstrongswan
AM_CFLAGS = \
$(PLUGIN_CFLAGS)
if MONOLITHIC
noinst_LTLIBRARIES = libstrongswan-drbg.la
else
plugin_LTLIBRARIES = libstrongswan-drbg.la
endif
libstrongswan_drbg_la_SOURCES = \
drbg_plugin.h drbg_plugin.c \
drbg_ctr.h drbg_ctr.c \
drbg_hmac.h drbg_hmac.c
libstrongswan_drbg_la_LDFLAGS = -module -avoid-version
+356
View File
@@ -0,0 +1,356 @@
/*
* Copyright (C) 2016-2019 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 "drbg_ctr.h"
#define MAX_DRBG_REQUESTS 0xfffffffe
typedef struct private_drbg_ctr_t private_drbg_ctr_t;
/**
* Private data of an drbg_ctr_t object.
*/
struct private_drbg_ctr_t {
/**
* Public drbg_ctr_t interface.
*/
drbg_ctr_t public;
/**
* DRBG type.
*/
drbg_type_t type;
/**
* Security strength in bits.
*/
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;
/**
* Block cipher in counter mode used by the DRBG
*/
crypter_t *crypter;
/**
* Internal state of HMAC: key
*/
chunk_t key;
/**
* Internal state of HMAC: value
*/
chunk_t value;
/**
* reference count
*/
refcount_t ref;
};
METHOD(drbg_t, get_type, drbg_type_t,
private_drbg_ctr_t *this)
{
return this->type;
}
METHOD(drbg_t, get_strength, uint32_t,
private_drbg_ctr_t *this)
{
return this->strength;
}
static bool encrypt_ctr(private_drbg_ctr_t *this, chunk_t out)
{
chunk_t iv = chunk_alloca(this->value.len);
chunk_t bl = chunk_alloca(this->value.len);
chunk_t block;
size_t delta, pos = 0;
int bit;
/* Initialize IV to all zeroes for ECB mode */
memset(iv.ptr, 0x00, iv.len);
if (!this->crypter->set_key(this->crypter, this->key))
{
return FALSE;
}
while (pos < out.len)
{
/* Increment counter by one */
chunk_increment(this->value);
/* Copy current counter to input block */
delta = out.len - pos;
block = (delta < this->value.len) ?
bl : chunk_create(out.ptr + pos, this->value.len);
memcpy(block.ptr, this->value.ptr, this->value.len);
/* ECB encryption */
if (!this->crypter->encrypt(this->crypter, block, iv, NULL))
{
return FALSE;
}
/* Partial output block at the end? */
if (delta < this->value.len)
{
memcpy(out.ptr + pos, block.ptr, delta);
}
pos += this->value.len;
}
return TRUE;
}
/**
* Update the internal state of the CTR_DRBG
*/
static bool update(private_drbg_ctr_t *this, chunk_t data)
{
chunk_t temp;
if (data.len && data.len != (this->key.len + this->value.len))
{
return FALSE;
}
temp = chunk_alloca(this->key.len + this->value.len);
if (!encrypt_ctr(this, temp))
{
return FALSE;
}
/* Apply data */
memxor(temp.ptr, data.ptr, data.len);
/* Copy new key and value */
memcpy(this->key.ptr, temp.ptr, this->key.len);
memcpy(this->value.ptr, temp.ptr + this->key.len, this->value.len);
memwipe(temp.ptr, temp.len);
DBG4(DBG_LIB, "CTR_DRBG K: %B", &this->key);
DBG4(DBG_LIB, "CTR_DRBG V: %B", &this->value);
return TRUE;
}
METHOD(drbg_t, reseed, bool,
private_drbg_ctr_t *this)
{
chunk_t seed;
bool success;
seed = chunk_alloc(this->key.len + this->value.len);
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;
}
DBG4(DBG_LIB, "reseed: %B", &seed);
success = update(this, seed);
chunk_clear(&seed);
if (!success)
{
return FALSE;
}
this->reseed_counter = 1;
return TRUE;
}
METHOD(drbg_t, generate, bool,
private_drbg_ctr_t *this, uint32_t len, uint8_t *out)
{
chunk_t output;
if (this->reseed_counter > this->max_requests)
{
if (!reseed(this))
{
return FALSE;
}
}
DBG2(DBG_LIB, "DRBG generates %u pseudorandom bytes", len);
if (!out || len == 0)
{
return FALSE;
}
output = chunk_create(out, len);
if (!encrypt_ctr(this, output))
{
return FALSE;
}
DBG4(DBG_LIB, "CTR_DRBG Out: %B", &output);
if (!update(this, chunk_empty))
{
return FALSE;
}
this->reseed_counter++;
return TRUE;
}
METHOD(drbg_t, get_ref, drbg_t*,
private_drbg_ctr_t *this)
{
ref_get(&this->ref);
return &this->public.interface;
}
METHOD(drbg_t, destroy, void,
private_drbg_ctr_t *this)
{
if (ref_put(&this->ref))
{
this->crypter->destroy(this->crypter);
chunk_clear(&this->key);
chunk_clear(&this->value);
free(this);
}
}
/**
* See header
*/
drbg_ctr_t *drbg_ctr_create(drbg_type_t type, uint32_t strength,
rng_t *entropy, chunk_t personalization_str)
{
private_drbg_ctr_t *this;
encryption_algorithm_t crypter_type;
crypter_t *crypter;
chunk_t seed;
size_t key_len, out_len, seed_len;
uint32_t max_requests;
bool success;
switch (type)
{
case DRBG_CTR_AES128:
crypter_type = ENCR_AES_CBC;
key_len = 16;
break;
case DRBG_CTR_AES192:
crypter_type = ENCR_AES_CBC;
key_len = 24;
break;
case DRBG_CTR_AES256:
crypter_type = ENCR_AES_CBC;
key_len = 32;
break;
default:
DBG1(DBG_LIB, "%N not supported", drbg_type_names, type);
return NULL;
}
if (strength > key_len * BITS_PER_BYTE)
{
DBG1(DBG_LIB, "%d bit block encryption key not sufficient for security "
"strength of % bits", key_len * BITS_PER_BYTE, strength);
return NULL;
}
crypter = lib->crypto->create_crypter(lib->crypto, crypter_type, key_len);
if (!crypter)
{
DBG1(DBG_LIB, "creation of %N for DRBG failed",
encryption_algorithm_names, crypter_type);
return NULL;
}
out_len = crypter->get_block_size(crypter);
seed_len = key_len + out_len;
if (personalization_str.len > seed_len)
{
DBG1(DBG_LIB, "personalization string length of %d bytes is larger "
"than seed length of % bytes", personalization_str.len, seed_len);
crypter->destroy(crypter);
return NULL;
}
max_requests = lib->settings->get_int(lib->settings,
"%s.plugins.drbg.max_drbg_requests",
MAX_DRBG_REQUESTS, lib->ns);
INIT(this,
.public = {
.interface = {
.get_type = _get_type,
.get_strength = _get_strength,
.reseed = _reseed,
.generate = _generate,
.get_ref = _get_ref,
.destroy = _destroy,
},
},
.type = type,
.strength = strength,
.entropy = entropy,
.crypter = crypter,
.key = chunk_alloc(key_len),
.value = chunk_alloc(out_len),
.max_requests = max_requests,
.reseed_counter = 1,
.ref = 1,
);
memset(this->key.ptr, 0x00, key_len);
memset(this->value.ptr, 0x00, out_len);
seed = chunk_alloc(seed_len);
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);
destroy(this);
return NULL;
}
memxor(seed.ptr, personalization_str.ptr, personalization_str.len);
DBG4(DBG_LIB, "seed: %B", &seed);
success = update(this, seed);
chunk_clear(&seed);
if (!success)
{
destroy(this);
return NULL;
}
return &this->public;
}
+51
View File
@@ -0,0 +1,51 @@
/*
* Copyright (C) 2019 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 drbg_ctr drbg_ctr
* @{ @ingroup drbg
*/
#ifndef DRBG_CTR_H_
#define DRBG_CTR_H_
#include <crypto/drbgs/drbg.h>
typedef struct drbg_ctr_t drbg_ctr_t;
/**
* NIST SP 800-90A CTR DRBC implementation
*/
struct drbg_ctr_t {
/**
* Public Deterministic Random Bit Generator (DRBG) Interface
*/
drbg_t interface;
};
/**
* Create a drbg_ctr instance.
*
* @param type DRBG CTR type
* @param strength security strength in bits
* @param entropy entropy source to be used
* @param personalization_str optional personalization string
* @return drbg_ctr_t object, NULL if not supported
*/
drbg_ctr_t *drbg_ctr_create(drbg_type_t type, uint32_t strength,
rng_t *entropy, chunk_t personalization_str);
#endif /** DRBG_CTR_H_ @}*/
+319
View File
@@ -0,0 +1,319 @@
/*
* Copyright (C) 2016-2019 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 "drbg_hmac.h"
#define MAX_DRBG_REQUESTS 0xfffffffe
typedef struct private_drbg_hmac_t private_drbg_hmac_t;
/**
* Private data of an drbg_prf_t object.
*/
struct private_drbg_hmac_t {
/**
* Public drbg_prf_t interface.
*/
drbg_hmac_t public;
/**
* DRBG type.
*/
drbg_type_t type;
/**
* Security strength in bits.
*/
uint32_t strength;
/**
* Number of requests for pseudorandom bits
*/
size_t reseed_counter;
/**
* Maximum number of requests for pseudorandom bits
*/
size_t max_requests;
/**
* True entropy source
*/
rng_t *entropy;
/**
* HMAC PRF used by the DRBG
*/
prf_t *prf;
/**
* Internal state of HMAC: key
*/
chunk_t key;
/**
* Internal state of HMAC: value
*/
chunk_t value;
/**
* reference count
*/
refcount_t ref;
};
METHOD(drbg_t, get_type, drbg_type_t,
private_drbg_hmac_t *this)
{
return this->type;
}
METHOD(drbg_t, get_strength, uint32_t,
private_drbg_hmac_t *this)
{
return this->strength;
}
/**
* Update the internal state of the HMAC_DRBG
*/
static bool update(private_drbg_hmac_t *this, chunk_t data)
{
chunk_t ch_00 = chunk_from_chars(0x00);
chunk_t ch_01 = chunk_from_chars(0x01);
if (!this->prf->set_key(this->prf, this->key) ||
!this->prf->get_bytes(this->prf, this->value, NULL) ||
!this->prf->get_bytes(this->prf, ch_00, NULL) ||
!this->prf->get_bytes(this->prf, data, this->key.ptr) ||
!this->prf->set_key(this->prf, this->key) ||
!this->prf->get_bytes(this->prf, this->value, this->value.ptr))
{
return FALSE;
}
if (data.len > 0)
{
if (!this->prf->set_key(this->prf, this->key) ||
!this->prf->get_bytes(this->prf, this->value, NULL) ||
!this->prf->get_bytes(this->prf, ch_01, NULL) ||
!this->prf->get_bytes(this->prf, data, this->key.ptr) ||
!this->prf->set_key(this->prf, this->key) ||
!this->prf->get_bytes(this->prf, this->value, this->value.ptr))
{
return FALSE;
}
}
DBG4(DBG_LIB, "HMAC_DRBG K: %B", &this->key);
DBG4(DBG_LIB, "HMAC_DRBG V: %B", &this->value);
return TRUE;
}
METHOD(drbg_t, reseed, bool,
private_drbg_hmac_t *this)
{
chunk_t seed;
bool success;
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;
}
DBG4(DBG_LIB, "reseed: %B", &seed);
success = update(this, seed);
chunk_clear(&seed);
if (!success)
{
return FALSE;
}
this->reseed_counter = 1;
return TRUE;
}
METHOD(drbg_t, generate, bool,
private_drbg_hmac_t *this, 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->prf->get_bytes(this->prf, 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(drbg_t, get_ref, drbg_t*,
private_drbg_hmac_t *this)
{
ref_get(&this->ref);
return &this->public.interface;
}
METHOD(drbg_t, destroy, void,
private_drbg_hmac_t *this)
{
if (ref_put(&this->ref))
{
this->prf->destroy(this->prf);
chunk_clear(&this->key);
chunk_clear(&this->value);
free(this);
}
}
/**
* See header
*/
drbg_hmac_t *drbg_hmac_create(drbg_type_t type, uint32_t strength,
rng_t *entropy, chunk_t personalization_str)
{
private_drbg_hmac_t *this;
pseudo_random_function_t prf_type = PRF_UNDEFINED;
size_t out_len, entropy_len;
uint32_t max_requests;
chunk_t seed;
prf_t * prf;
bool success;
switch (type)
{
case DRBG_HMAC_SHA1:
prf_type = PRF_HMAC_SHA1;
break;
case DRBG_HMAC_SHA256:
prf_type = PRF_HMAC_SHA2_256;
break;
case DRBG_HMAC_SHA384:
prf_type = PRF_HMAC_SHA2_384;
break;
case DRBG_HMAC_SHA512:
prf_type = PRF_HMAC_SHA2_512;
break;
default:
DBG1(DBG_LIB, "%N not supported", drbg_type_names, type);
return NULL;
}
prf = lib->crypto->create_prf(lib->crypto, prf_type);
if (!prf)
{
DBG1(DBG_LIB, "creation of %N for DRBG failed",
pseudo_random_function_names, prf_type);
return NULL;
}
out_len = prf->get_key_size(prf);
if (strength > out_len * BITS_PER_BYTE)
{
DBG1(DBG_LIB, "%N not sufficient for security strength of % bits",
pseudo_random_function_names, prf_type, strength);
prf->destroy(prf);
return NULL;
}
max_requests = lib->settings->get_int(lib->settings,
"%s.plugins.drbg.max_drbg_requests",
MAX_DRBG_REQUESTS, lib->ns);
INIT(this,
.public = {
.interface = {
.get_type = _get_type,
.get_strength = _get_strength,
.reseed = _reseed,
.generate = _generate,
.get_ref = _get_ref,
.destroy = _destroy,
},
},
.type = type,
.strength = strength,
.entropy = entropy,
.prf = prf,
.key = chunk_alloc(out_len),
.value = chunk_alloc(out_len),
.max_requests = max_requests,
.reseed_counter = 1,
.ref = 1,
);
memset(this->key.ptr, 0x00, out_len);
memset(this->value.ptr, 0x01, out_len);
entropy_len = (strength + strength/2) / BITS_PER_BYTE;
seed = chunk_alloc(entropy_len + personalization_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,
personalization_str.ptr, personalization_str.len);
DBG4(DBG_LIB, "seed: %B", &seed);
success = update(this, seed);
chunk_clear(&seed);
if (!success)
{
destroy(this);
return NULL;
}
return &this->public;
}
@@ -0,0 +1,51 @@
/*
* Copyright (C) 2019 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 drbg_hmac drbg_hmac
* @{ @ingroup drbg
*/
#ifndef DRBG_HMAC_H_
#define DRBG_HMAC_H_
#include <crypto/drbgs/drbg.h>
typedef struct drbg_hmac_t drbg_hmac_t;
/**
* NIST SP 800-90A HMAC DRBC implementation
*/
struct drbg_hmac_t {
/**
* Public Deterministic Random Bit Generator (DRBG) Interface
*/
drbg_t interface;
};
/**
* Create a drbg_hmac instance.
*
* @param type DRBG HMAC type
* @param strength security strength in bits
* @param entropy entropy source to be used
* @param personalization_str optional personalization string
* @return drbg_hmac_t object, NULL if not supported
*/
drbg_hmac_t *drbg_hmac_create(drbg_type_t type, uint32_t strength,
rng_t *entropy, chunk_t personalization_str);
#endif /** DRBG_HMAC_H_ @}*/
@@ -0,0 +1,92 @@
/*
* Copyright (C) 2019 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 "drbg_plugin.h"
#include "drbg_ctr.h"
#include "drbg_hmac.h"
#include <library.h>
typedef struct private_drbg_plugin_t private_drbg_plugin_t;
/**
* private data of drbg_plugin
*/
struct private_drbg_plugin_t {
/**
* public functions
*/
drbg_plugin_t public;
};
METHOD(plugin_t, get_name, char*,
private_drbg_plugin_t *this)
{
return "drbg";
}
METHOD(plugin_t, get_features, int,
private_drbg_plugin_t *this, plugin_feature_t *features[])
{
static plugin_feature_t f[] = {
/* NIST CTR DRBG */
PLUGIN_REGISTER(DRBG, drbg_ctr_create),
PLUGIN_PROVIDE(DRBG, DRBG_CTR_AES128),
PLUGIN_DEPENDS(CRYPTER, ENCR_AES_CBC, 16),
PLUGIN_PROVIDE(DRBG, DRBG_CTR_AES192),
PLUGIN_DEPENDS(CRYPTER, ENCR_AES_CBC, 24),
PLUGIN_PROVIDE(DRBG, DRBG_CTR_AES256),
PLUGIN_DEPENDS(CRYPTER, ENCR_AES_CBC, 32),
/* NIST HMAC DRBG */
PLUGIN_REGISTER(DRBG, drbg_hmac_create),
PLUGIN_PROVIDE(DRBG, DRBG_HMAC_SHA1),
PLUGIN_DEPENDS(PRF, PRF_HMAC_SHA1),
PLUGIN_PROVIDE(DRBG, DRBG_HMAC_SHA256),
PLUGIN_DEPENDS(PRF, PRF_HMAC_SHA2_256),
PLUGIN_PROVIDE(DRBG, DRBG_HMAC_SHA384),
PLUGIN_DEPENDS(PRF, PRF_HMAC_SHA2_384),
PLUGIN_PROVIDE(DRBG, DRBG_HMAC_SHA512),
PLUGIN_DEPENDS(PRF, PRF_HMAC_SHA2_512),
};
*features = f;
return countof(f);
}
METHOD(plugin_t, destroy, void,
private_drbg_plugin_t *this)
{
free(this);
}
/*
* see header file
*/
plugin_t *drbg_plugin_create()
{
private_drbg_plugin_t *this;
INIT(this,
.public = {
.plugin = {
.get_name = _get_name,
.get_features = _get_features,
.destroy = _destroy,
},
},
);
return &this->public.plugin;
}
@@ -0,0 +1,42 @@
/*
* Copyright (C) 2019 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 drbg_p drbg
* @ingroup plugins
*
* @defgroup drbg_plugin drbg_plugin
* @{ @ingroup drbg_p
*/
#ifndef DRBG_PLUGIN_H_
#define DRBG_PLUGIN_H_
#include <plugins/plugin.h>
typedef struct drbg_plugin_t drbg_plugin_t;
/**
* Plugin providing a NIST CTR and HMAC DRBG implementation
*/
struct drbg_plugin_t {
/**
* implements plugin interface
*/
plugin_t plugin;
};
#endif /** DRBG_PLUGIN_H_ @}*/