added API for random number generators, served through credential factory
ported randomizer_t to a rng_t on top of /dev/(u)random (plugin random)
This commit is contained in:
+10
-13
@@ -90,12 +90,6 @@ static void dbg_bus(int level, char *fmt, ...)
|
||||
charon->bus->vsignal(charon->bus, DBG_LIB, level, fmt, args);
|
||||
va_end(args);
|
||||
}
|
||||
/**
|
||||
* Logging hook for library logs before logging facility initiated
|
||||
*/
|
||||
static void dbg_silent(int level, char *fmt, ...)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Logging hook for library logs, using stderr output
|
||||
@@ -104,11 +98,14 @@ static void dbg_stderr(int level, char *fmt, ...)
|
||||
{
|
||||
va_list args;
|
||||
|
||||
va_start(args, fmt);
|
||||
fprintf(stderr, "00[LIB] ");
|
||||
vfprintf(stderr, fmt, args);
|
||||
fprintf(stderr, "\n");
|
||||
va_end(args);
|
||||
if (level <= 1)
|
||||
{
|
||||
va_start(args, fmt);
|
||||
fprintf(stderr, "00[LIB] ");
|
||||
vfprintf(stderr, fmt, args);
|
||||
fprintf(stderr, "\n");
|
||||
va_end(args);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -483,8 +480,8 @@ int main(int argc, char *argv[])
|
||||
level_t levels[DBG_MAX];
|
||||
int signal;
|
||||
|
||||
/* silence the library during initialization, as we have no bus yet */
|
||||
dbg = dbg_silent;
|
||||
/* logging for library during initialization, as we have no bus yet */
|
||||
dbg = dbg_stderr;
|
||||
|
||||
/* initialize library */
|
||||
library_init(STRONGSWAN_CONF);
|
||||
|
||||
@@ -27,7 +27,6 @@
|
||||
#include <encoding/generator.h>
|
||||
#include <encoding/parser.h>
|
||||
#include <utils/iterator.h>
|
||||
#include <utils/randomizer.h>
|
||||
#include <crypto/signers/signer.h>
|
||||
|
||||
|
||||
@@ -322,7 +321,7 @@ static void generate(private_encryption_payload_t *this)
|
||||
static status_t encrypt(private_encryption_payload_t *this)
|
||||
{
|
||||
chunk_t iv, padding, to_crypt, result;
|
||||
randomizer_t *randomizer;
|
||||
rng_t *rng;
|
||||
status_t status;
|
||||
size_t block_size;
|
||||
|
||||
@@ -333,8 +332,12 @@ static status_t encrypt(private_encryption_payload_t *this)
|
||||
}
|
||||
|
||||
/* for random data in iv and padding */
|
||||
randomizer = randomizer_create();
|
||||
|
||||
rng = lib->crypto->create_rng(lib->crypto, RNG_WEAK);
|
||||
if (!rng)
|
||||
{
|
||||
DBG1(DBG_ENC, "could not encrypt, no RNG found");
|
||||
return FAILED;
|
||||
}
|
||||
/* build payload chunk */
|
||||
generate(this);
|
||||
|
||||
@@ -344,12 +347,7 @@ static status_t encrypt(private_encryption_payload_t *this)
|
||||
/* build padding */
|
||||
block_size = this->crypter->get_block_size(this->crypter);
|
||||
padding.len = block_size - ((this->decrypted.len + 1) % block_size);
|
||||
status = randomizer->allocate_pseudo_random_bytes(randomizer, padding.len, &padding);
|
||||
if (status != SUCCESS)
|
||||
{
|
||||
randomizer->destroy(randomizer);
|
||||
return status;
|
||||
}
|
||||
rng->allocate_bytes(rng, padding.len, &padding);
|
||||
|
||||
/* concatenate payload data, padding, padding len */
|
||||
to_crypt.len = this->decrypted.len + padding.len + 1;
|
||||
@@ -361,14 +359,8 @@ static status_t encrypt(private_encryption_payload_t *this)
|
||||
|
||||
/* build iv */
|
||||
iv.len = block_size;
|
||||
status = randomizer->allocate_pseudo_random_bytes(randomizer, iv.len, &iv);
|
||||
randomizer->destroy(randomizer);
|
||||
if (status != SUCCESS)
|
||||
{
|
||||
chunk_free(&to_crypt);
|
||||
chunk_free(&padding);
|
||||
return status;
|
||||
}
|
||||
rng->allocate_bytes(rng, iv.len, &iv);
|
||||
rng->destroy(rng);
|
||||
|
||||
DBG3(DBG_ENC, "data before encryption with padding %B", &to_crypt);
|
||||
|
||||
|
||||
@@ -88,9 +88,9 @@ struct private_receiver_t {
|
||||
u_int32_t secret_offset;
|
||||
|
||||
/**
|
||||
* the randomizer to use for secret generation
|
||||
* the RNG to use for secret generation
|
||||
*/
|
||||
randomizer_t *randomizer;
|
||||
rng_t *rng;
|
||||
|
||||
/**
|
||||
* hasher to use for cookie calculation
|
||||
@@ -304,8 +304,7 @@ static job_requeue_t receive_packets(private_receiver_t *this)
|
||||
DBG1(DBG_NET, "generating new cookie secret after %d uses",
|
||||
this->secret_used);
|
||||
memcpy(this->secret_old, this->secret, SECRET_LENGTH);
|
||||
this->randomizer->get_pseudo_random_bytes(this->randomizer,
|
||||
SECRET_LENGTH, this->secret);
|
||||
this->rng->get_bytes(this->rng, SECRET_LENGTH, this->secret);
|
||||
this->secret_switch = now;
|
||||
this->secret_used = 0;
|
||||
}
|
||||
@@ -333,7 +332,7 @@ static job_requeue_t receive_packets(private_receiver_t *this)
|
||||
static void destroy(private_receiver_t *this)
|
||||
{
|
||||
this->job->cancel(this->job);
|
||||
this->randomizer->destroy(this->randomizer);
|
||||
this->rng->destroy(this->rng);
|
||||
this->hasher->destroy(this->hasher);
|
||||
free(this);
|
||||
}
|
||||
@@ -355,12 +354,18 @@ receiver_t *receiver_create()
|
||||
free(this);
|
||||
return NULL;
|
||||
}
|
||||
this->randomizer = randomizer_create();
|
||||
this->rng = lib->crypto->create_rng(lib->crypto, RNG_STRONG);
|
||||
if (this->rng == NULL)
|
||||
{
|
||||
DBG1(DBG_NET, "creating cookie RNG failed, no RNG supported");
|
||||
this->hasher->destroy(this->hasher);
|
||||
free(this);
|
||||
return NULL;
|
||||
}
|
||||
this->secret_switch = now;
|
||||
this->secret_offset = random() % now;
|
||||
this->secret_used = 0;
|
||||
this->randomizer->get_pseudo_random_bytes(this->randomizer, SECRET_LENGTH,
|
||||
this->secret);
|
||||
this->rng->get_bytes(this->rng, SECRET_LENGTH, this->secret);
|
||||
memcpy(this->secret_old, this->secret, SECRET_LENGTH);
|
||||
|
||||
this->job = callback_job_create((callback_job_cb_t)receive_packets,
|
||||
|
||||
@@ -45,7 +45,6 @@
|
||||
|
||||
#include <daemon.h>
|
||||
#include <library.h>
|
||||
#include <utils/randomizer.h>
|
||||
#include <crypto/hashers/hasher.h>
|
||||
|
||||
/* Use test vectors specified in S.S0055
|
||||
@@ -835,7 +834,7 @@ static eap_payload_t *build_aka_payload(private_eap_aka_t *this, eap_code_t code
|
||||
static status_t server_initiate_challenge(private_eap_aka_t *this, chunk_t sqn,
|
||||
eap_payload_t **out)
|
||||
{
|
||||
randomizer_t *randomizer;
|
||||
rng_t *rng;
|
||||
status_t status;
|
||||
chunk_t mac, ak, autn;
|
||||
|
||||
@@ -845,16 +844,16 @@ static status_t server_initiate_challenge(private_eap_aka_t *this, chunk_t sqn,
|
||||
chunk_free(&this->xres);
|
||||
|
||||
/* generate RAND:
|
||||
* we use our standard randomizer, not f0() proposed in S.S0055
|
||||
* we use a registered RNG, not f0() proposed in S.S0055
|
||||
*/
|
||||
randomizer = randomizer_create();
|
||||
status = randomizer->allocate_pseudo_random_bytes(randomizer, RAND_LENGTH, &this->rand);
|
||||
randomizer->destroy(randomizer);
|
||||
if (status != SUCCESS)
|
||||
rng = lib->crypto->create_rng(lib->crypto, RNG_WEAK);
|
||||
if (!rng)
|
||||
{
|
||||
DBG1(DBG_IKE, "generating RAND for EAP-AKA authentication failed");
|
||||
return FAILED;
|
||||
}
|
||||
rng->allocate_bytes(rng, RAND_LENGTH, &this->rand);
|
||||
rng->destroy(rng);
|
||||
|
||||
# ifdef TEST_VECTORS
|
||||
/* Test vector for RAND */
|
||||
|
||||
@@ -122,18 +122,16 @@ static status_t initiate_peer(private_eap_md5_t *this, eap_payload_t **out)
|
||||
*/
|
||||
static status_t initiate_server(private_eap_md5_t *this, eap_payload_t **out)
|
||||
{
|
||||
randomizer_t *randomizer;
|
||||
status_t status;
|
||||
rng_t *rng;
|
||||
eap_md5_header_t *req;
|
||||
|
||||
randomizer = randomizer_create();
|
||||
status = randomizer->allocate_pseudo_random_bytes(randomizer, CHALLENGE_LEN,
|
||||
&this->challenge);
|
||||
randomizer->destroy(randomizer);
|
||||
if (status != SUCCESS)
|
||||
rng = lib->crypto->create_rng(lib->crypto, RNG_WEAK);
|
||||
if (!rng)
|
||||
{
|
||||
return FAILED;
|
||||
}
|
||||
rng->allocate_bytes(rng, CHALLENGE_LEN, &this->challenge);
|
||||
rng->destroy(rng);
|
||||
|
||||
req = alloca(PAYLOAD_LEN);
|
||||
req->length = htons(PAYLOAD_LEN);
|
||||
|
||||
@@ -1037,7 +1037,7 @@ eap_sim_t *eap_sim_create_generic(eap_role_t role, identification_t *server,
|
||||
identification_t *peer)
|
||||
{
|
||||
private_eap_sim_t *this;
|
||||
randomizer_t *randomizer;
|
||||
rng_t *rng;
|
||||
void *symbol;
|
||||
char *name;
|
||||
|
||||
@@ -1098,16 +1098,15 @@ eap_sim_t *eap_sim_create_generic(eap_role_t role, identification_t *server,
|
||||
this->public.eap_method_interface.process = (status_t(*)(eap_method_t*,eap_payload_t*,eap_payload_t**))peer_process;
|
||||
this->alg = symbol;
|
||||
this->type = EAP_RESPONSE;
|
||||
randomizer = randomizer_create();
|
||||
if (randomizer->allocate_pseudo_random_bytes(randomizer, NONCE_LEN,
|
||||
&this->nonce))
|
||||
rng = lib->crypto->create_rng(lib->crypto, RNG_WEAK);
|
||||
if (!rng)
|
||||
{
|
||||
DBG1(DBG_IKE, "unable to generate NONCE for EAP_SIM");
|
||||
randomizer->destroy(randomizer);
|
||||
DBG1(DBG_IKE, "unable to generate NONCE for EAP_SIM");
|
||||
free(this);
|
||||
return NULL;
|
||||
}
|
||||
randomizer->destroy(randomizer);
|
||||
rng->allocate_bytes(rng, NONCE_LEN, &this->nonce);
|
||||
rng->destroy(rng);
|
||||
break;
|
||||
default:
|
||||
free(this);
|
||||
|
||||
@@ -574,7 +574,7 @@ static err_t extract_secret(chunk_t *secret, chunk_t *line)
|
||||
|
||||
if (ugh != NULL)
|
||||
{
|
||||
chunk_free_randomized(secret);
|
||||
chunk_clear(secret);
|
||||
return ugh;
|
||||
}
|
||||
secret->len = len;
|
||||
@@ -702,7 +702,7 @@ static void load_secrets(private_stroke_cred_t *this)
|
||||
this->private->insert_last(this->private, key);
|
||||
}
|
||||
}
|
||||
chunk_free_randomized(&secret);
|
||||
chunk_clear(&secret);
|
||||
}
|
||||
else if ((match("PSK", &token) && (type = SHARED_IKE)) ||
|
||||
(match("EAP", &token) && (type = SHARED_EAP)) ||
|
||||
@@ -774,7 +774,7 @@ static void load_secrets(private_stroke_cred_t *this)
|
||||
}
|
||||
error:
|
||||
this->mutex->unlock(this->mutex);
|
||||
chunk_free_randomized(&chunk);
|
||||
chunk_clear(&chunk);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -38,7 +38,6 @@ typedef struct ike_sa_t ike_sa_t;
|
||||
#include <sa/ike_sa_id.h>
|
||||
#include <sa/child_sa.h>
|
||||
#include <sa/tasks/task.h>
|
||||
#include <utils/randomizer.h>
|
||||
#include <crypto/prfs/prf.h>
|
||||
#include <crypto/crypters/crypter.h>
|
||||
#include <crypto/signers/signer.h>
|
||||
|
||||
@@ -142,9 +142,9 @@ struct private_ike_sa_manager_t {
|
||||
linked_list_t *ike_sa_list;
|
||||
|
||||
/**
|
||||
* A randomizer, to get random SPIs for our side
|
||||
* RNG to get random SPIs for our side
|
||||
*/
|
||||
randomizer_t *randomizer;
|
||||
rng_t *rng;
|
||||
|
||||
/**
|
||||
* SHA1 hasher for IKE_SA_INIT retransmit detection
|
||||
@@ -304,8 +304,7 @@ static u_int64_t get_next_spi(private_ike_sa_manager_t *this)
|
||||
{
|
||||
u_int64_t spi;
|
||||
|
||||
this->randomizer->get_pseudo_random_bytes(this->randomizer, sizeof(spi),
|
||||
(u_int8_t*)&spi);
|
||||
this->rng->get_bytes(this->rng, sizeof(spi), (u_int8_t*)&spi);
|
||||
return spi;
|
||||
}
|
||||
|
||||
@@ -933,7 +932,7 @@ static void destroy(private_ike_sa_manager_t *this)
|
||||
this->ike_sa_list->destroy_function(this->ike_sa_list, (void*)entry_destroy);
|
||||
pthread_mutex_unlock(&(this->mutex));
|
||||
|
||||
this->randomizer->destroy(this->randomizer);
|
||||
this->rng->destroy(this->rng);
|
||||
this->hasher->destroy(this->hasher);
|
||||
|
||||
free(this);
|
||||
@@ -968,9 +967,16 @@ ike_sa_manager_t *ike_sa_manager_create()
|
||||
free(this);
|
||||
return NULL;
|
||||
}
|
||||
this->rng = lib->crypto->create_rng(lib->crypto, RNG_WEAK);
|
||||
if (this->rng == NULL)
|
||||
{
|
||||
DBG1(DBG_MGR, "manager initialization failed, no RNG supported");
|
||||
this->hasher->destroy(this->hasher);
|
||||
free(this);
|
||||
return NULL;
|
||||
}
|
||||
this->ike_sa_list = linked_list_create();
|
||||
pthread_mutex_init(&this->mutex, NULL);
|
||||
this->randomizer = randomizer_create();
|
||||
return &this->public;
|
||||
}
|
||||
|
||||
|
||||
@@ -136,17 +136,16 @@ static status_t get_nonce(message_t *message, chunk_t *nonce)
|
||||
*/
|
||||
static status_t generate_nonce(chunk_t *nonce)
|
||||
{
|
||||
status_t status;
|
||||
randomizer_t *randomizer = randomizer_create();
|
||||
rng_t *rng;
|
||||
|
||||
status = randomizer->allocate_pseudo_random_bytes(randomizer, NONCE_SIZE,
|
||||
nonce);
|
||||
randomizer->destroy(randomizer);
|
||||
if (status != SUCCESS)
|
||||
rng = lib->crypto->create_rng(lib->crypto, RNG_WEAK);
|
||||
if (!rng)
|
||||
{
|
||||
DBG1(DBG_IKE, "error generating random nonce value");
|
||||
DBG1(DBG_IKE, "error generating nonce value, no RNG found");
|
||||
return FAILED;
|
||||
}
|
||||
rng->allocate_bytes(rng, NONCE_SIZE, nonce);
|
||||
rng->destroy(rng);
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
@@ -218,8 +218,7 @@ static void process_payloads(private_ike_init_t *this, message_t *message)
|
||||
*/
|
||||
static status_t build_i(private_ike_init_t *this, message_t *message)
|
||||
{
|
||||
randomizer_t *randomizer;
|
||||
status_t status;
|
||||
rng_t *rng;
|
||||
|
||||
this->config = this->ike_sa->get_ike_cfg(this->ike_sa);
|
||||
SIG(IKE_UP_START, "initiating IKE_SA '%s' to %H",
|
||||
@@ -249,15 +248,14 @@ static status_t build_i(private_ike_init_t *this, message_t *message)
|
||||
/* generate nonce only when we are trying the first time */
|
||||
if (this->my_nonce.ptr == NULL)
|
||||
{
|
||||
randomizer = randomizer_create();
|
||||
status = randomizer->allocate_pseudo_random_bytes(randomizer, NONCE_SIZE,
|
||||
&this->my_nonce);
|
||||
randomizer->destroy(randomizer);
|
||||
if (status != SUCCESS)
|
||||
rng = lib->crypto->create_rng(lib->crypto, RNG_WEAK);
|
||||
if (!rng)
|
||||
{
|
||||
SIG(IKE_UP_FAILED, "error generating random nonce value");
|
||||
SIG(IKE_UP_FAILED, "error generating nonce");
|
||||
return FAILED;
|
||||
}
|
||||
rng->allocate_bytes(rng, NONCE_SIZE, &this->my_nonce);
|
||||
rng->destroy(rng);
|
||||
}
|
||||
|
||||
if (this->cookie.ptr)
|
||||
@@ -285,20 +283,21 @@ static status_t build_i(private_ike_init_t *this, message_t *message)
|
||||
*/
|
||||
static status_t process_r(private_ike_init_t *this, message_t *message)
|
||||
{
|
||||
randomizer_t *randomizer;
|
||||
rng_t *rng;
|
||||
|
||||
this->config = this->ike_sa->get_ike_cfg(this->ike_sa);
|
||||
SIG(IKE_UP_START, "%H is initiating an IKE_SA",
|
||||
message->get_source(message));
|
||||
this->ike_sa->set_state(this->ike_sa, IKE_CONNECTING);
|
||||
|
||||
randomizer = randomizer_create();
|
||||
if (randomizer->allocate_pseudo_random_bytes(randomizer, NONCE_SIZE,
|
||||
&this->my_nonce) != SUCCESS)
|
||||
rng = lib->crypto->create_rng(lib->crypto, RNG_WEAK);
|
||||
if (!rng)
|
||||
{
|
||||
DBG1(DBG_IKE, "error generating random nonce value");
|
||||
DBG1(DBG_IKE, "error generating nonce");
|
||||
return FAILED;
|
||||
}
|
||||
randomizer->destroy(randomizer);
|
||||
rng->allocate_bytes(rng, NONCE_SIZE, &this->my_nonce);
|
||||
rng->destroy(rng);
|
||||
|
||||
#ifdef ME
|
||||
{
|
||||
|
||||
@@ -274,33 +274,25 @@ static status_t build_i(private_ike_me_t *this, message_t *message)
|
||||
case ME_CONNECT:
|
||||
{
|
||||
id_payload_t *id_payload;
|
||||
randomizer_t *rand = randomizer_create();
|
||||
rng_t *rng;
|
||||
|
||||
id_payload = id_payload_create_from_identification(ID_PEER, this->peer_id);
|
||||
message->add_payload(message, (payload_t*)id_payload);
|
||||
|
||||
rng = lib->crypto->create_rng(lib->crypto, RNG_STRONG);
|
||||
if (!rng)
|
||||
{
|
||||
DBG1(DBG_IKE, "unable to generate connect ID for ME_CONNECT");
|
||||
return FAILED;
|
||||
}
|
||||
if (!this->response)
|
||||
{
|
||||
/* only the initiator creates a connect ID. the responder returns
|
||||
* the connect ID that it received from the initiator */
|
||||
if (rand->allocate_pseudo_random_bytes(rand,
|
||||
ME_CONNECTID_LEN, &this->connect_id) != SUCCESS)
|
||||
{
|
||||
DBG1(DBG_IKE, "unable to generate connect ID for ME_CONNECT");
|
||||
rand->destroy(rand);
|
||||
return FAILED;
|
||||
}
|
||||
rng->allocate_bytes(rng, ME_CONNECTID_LEN, &this->connect_id);
|
||||
}
|
||||
|
||||
if (rand->allocate_pseudo_random_bytes(rand,
|
||||
ME_CONNECTKEY_LEN, &this->connect_key) != SUCCESS)
|
||||
{
|
||||
DBG1(DBG_IKE, "unable to generate connect key for ME_CONNECT");
|
||||
rand->destroy(rand);
|
||||
return FAILED;
|
||||
}
|
||||
|
||||
rand->destroy(rand);
|
||||
rng->allocate_bytes(rng, ME_CONNECTKEY_LEN, &this->connect_key);
|
||||
rng->destroy(rng);
|
||||
|
||||
message->add_notify(message, FALSE, ME_CONNECTID, this->connect_id);
|
||||
message->add_notify(message, FALSE, ME_CONNECTKEY, this->connect_key);
|
||||
|
||||
@@ -113,17 +113,17 @@ static chunk_t generate_natd_hash(private_ike_natd_t *this,
|
||||
*/
|
||||
static chunk_t generate_natd_hash_faked(private_ike_natd_t *this)
|
||||
{
|
||||
randomizer_t *randomizer;
|
||||
rng_t *rng;
|
||||
chunk_t chunk;
|
||||
|
||||
randomizer = randomizer_create();
|
||||
if (randomizer->allocate_pseudo_random_bytes(randomizer, HASH_SIZE_SHA1,
|
||||
&chunk) != SUCCESS)
|
||||
rng = lib->crypto->create_rng(lib->crypto, RNG_WEAK);
|
||||
if (!rng)
|
||||
{
|
||||
DBG1(DBG_IKE, "unable to get random bytes for NATD fake");
|
||||
chunk = chunk_empty;
|
||||
return chunk_empty;
|
||||
}
|
||||
randomizer->destroy(randomizer);
|
||||
rng->allocate_bytes(rng, HASH_SIZE_SHA1, &chunk);
|
||||
rng->destroy(rng);
|
||||
return chunk;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user