2005-11-22 12:30:31 +00:00
|
|
|
/*
|
2006-07-07 08:49:06 +00:00
|
|
|
* Copyright (C) 2005-2006 Martin Willi
|
|
|
|
* Copyright (C) 2005 Jan Hutter
|
2018-05-23 14:04:50 +00:00
|
|
|
* HSR Hochschule fuer Technik Rapperswil
|
2005-11-22 12:30:31 +00:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2006-04-10 08:07:38 +00:00
|
|
|
#include <string.h>
|
2005-11-22 12:30:31 +00:00
|
|
|
|
|
|
|
#include "prf_plus.h"
|
|
|
|
|
2005-11-24 11:30:19 +00:00
|
|
|
typedef struct private_prf_plus_t private_prf_plus_t;
|
|
|
|
|
2019-10-18 14:53:35 +00:00
|
|
|
typedef bool (*apply_prf_t)(private_prf_plus_t *this);
|
|
|
|
|
2005-11-22 12:30:31 +00:00
|
|
|
/**
|
|
|
|
* Private data of an prf_plus_t object.
|
2009-09-04 11:46:09 +00:00
|
|
|
*
|
2005-11-22 12:30:31 +00:00
|
|
|
*/
|
2005-11-24 11:30:19 +00:00
|
|
|
struct private_prf_plus_t {
|
2011-11-22 13:38:43 +00:00
|
|
|
|
2005-11-22 12:30:31 +00:00
|
|
|
/**
|
2005-12-06 15:10:11 +00:00
|
|
|
* Public interface of prf_plus_t.
|
2005-11-22 12:30:31 +00:00
|
|
|
*/
|
|
|
|
prf_plus_t public;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2005-11-22 12:30:31 +00:00
|
|
|
/**
|
2005-12-06 15:10:11 +00:00
|
|
|
* PRF to use.
|
2005-11-22 12:30:31 +00:00
|
|
|
*/
|
|
|
|
prf_t *prf;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2005-11-22 12:30:31 +00:00
|
|
|
/**
|
2005-12-06 15:10:11 +00:00
|
|
|
* Initial seed.
|
2005-11-22 12:30:31 +00:00
|
|
|
*/
|
|
|
|
chunk_t seed;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2005-11-22 12:30:31 +00:00
|
|
|
/**
|
2019-10-18 14:53:35 +00:00
|
|
|
* Octet which will be appended to the seed if a counter is used.
|
2005-11-22 12:30:31 +00:00
|
|
|
*/
|
2016-03-22 12:22:01 +00:00
|
|
|
uint8_t counter;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2005-11-22 12:30:31 +00:00
|
|
|
/**
|
2005-12-06 15:10:11 +00:00
|
|
|
* Already given out bytes in current buffer.
|
2005-11-22 12:30:31 +00:00
|
|
|
*/
|
2011-11-22 13:38:43 +00:00
|
|
|
size_t used;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2005-11-22 12:30:31 +00:00
|
|
|
/**
|
2011-11-25 10:26:20 +00:00
|
|
|
* Buffer to store current PRF result.
|
2005-11-22 12:30:31 +00:00
|
|
|
*/
|
2011-11-25 10:26:20 +00:00
|
|
|
chunk_t buffer;
|
2019-10-18 14:53:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The prf application method depending on whether a counter is used.
|
|
|
|
*/
|
|
|
|
apply_prf_t apply_prf;
|
2005-11-22 12:30:31 +00:00
|
|
|
};
|
|
|
|
|
2019-10-18 14:53:35 +00:00
|
|
|
/**
|
|
|
|
* Apply the PRF using the running counter
|
|
|
|
*/
|
|
|
|
static bool apply_prf_counter(private_prf_plus_t *this)
|
|
|
|
{
|
|
|
|
if (!this->prf->get_bytes(this->prf, this->seed, NULL) ||
|
|
|
|
!this->prf->get_bytes(this->prf, chunk_from_thing(this->counter),
|
|
|
|
this->buffer.ptr))
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
this->counter++;
|
|
|
|
if (!this->counter)
|
|
|
|
{ /* according to RFC 7296, section 2.13, prf+ is undefined once the
|
|
|
|
* counter wrapped, so let's fail for future calls */
|
|
|
|
this->apply_prf = (void*)return_false;
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Apply the PRF using the running counter
|
|
|
|
*/
|
|
|
|
static bool apply_prf(private_prf_plus_t *this)
|
|
|
|
{
|
|
|
|
return this->prf->get_bytes(this->prf, this->seed, this->buffer.ptr);
|
|
|
|
}
|
|
|
|
|
2012-07-06 06:39:15 +00:00
|
|
|
METHOD(prf_plus_t, get_bytes, bool,
|
2016-03-22 12:22:01 +00:00
|
|
|
private_prf_plus_t *this, size_t length, uint8_t *buffer)
|
2009-09-04 11:46:09 +00:00
|
|
|
{
|
2011-11-22 13:38:43 +00:00
|
|
|
size_t round, written = 0;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2005-11-22 15:34:10 +00:00
|
|
|
while (length > 0)
|
2011-11-22 13:38:43 +00:00
|
|
|
{
|
|
|
|
if (this->buffer.len == this->used)
|
|
|
|
{ /* buffer used, get next round */
|
2012-07-06 08:14:29 +00:00
|
|
|
if (!this->prf->get_bytes(this->prf, this->buffer, NULL))
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
2019-10-18 14:53:35 +00:00
|
|
|
if (!this->apply_prf(this))
|
2011-11-22 13:38:43 +00:00
|
|
|
{
|
2019-10-18 14:53:35 +00:00
|
|
|
return FALSE;
|
2011-11-22 13:38:43 +00:00
|
|
|
}
|
|
|
|
this->used = 0;
|
2005-11-22 12:30:31 +00:00
|
|
|
}
|
2011-11-22 13:38:43 +00:00
|
|
|
round = min(length, this->buffer.len - this->used);
|
|
|
|
memcpy(buffer + written, this->buffer.ptr + this->used, round);
|
|
|
|
|
|
|
|
length -= round;
|
|
|
|
this->used += round;
|
|
|
|
written += round;
|
2005-11-22 12:30:31 +00:00
|
|
|
}
|
2012-07-06 06:39:15 +00:00
|
|
|
return TRUE;
|
2005-11-22 12:30:31 +00:00
|
|
|
}
|
|
|
|
|
2012-07-06 06:24:24 +00:00
|
|
|
METHOD(prf_plus_t, allocate_bytes, bool,
|
2011-10-02 09:27:38 +00:00
|
|
|
private_prf_plus_t *this, size_t length, chunk_t *chunk)
|
2005-11-22 12:30:31 +00:00
|
|
|
{
|
2019-10-18 13:11:26 +00:00
|
|
|
*chunk = chunk_alloc(length);
|
|
|
|
if (!get_bytes(this, length, chunk->ptr))
|
2008-10-29 14:12:54 +00:00
|
|
|
{
|
2019-10-18 13:11:26 +00:00
|
|
|
chunk_free(chunk);
|
|
|
|
return FALSE;
|
2008-10-29 14:12:54 +00:00
|
|
|
}
|
2012-07-06 06:24:24 +00:00
|
|
|
return TRUE;
|
2005-11-22 12:30:31 +00:00
|
|
|
}
|
|
|
|
|
2011-10-02 09:27:38 +00:00
|
|
|
METHOD(prf_plus_t, destroy, void,
|
|
|
|
private_prf_plus_t *this)
|
2005-11-22 12:30:31 +00:00
|
|
|
{
|
2017-07-25 08:15:58 +00:00
|
|
|
chunk_clear(&this->buffer);
|
|
|
|
chunk_clear(&this->seed);
|
2006-04-10 08:07:38 +00:00
|
|
|
free(this);
|
2005-11-22 12:30:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2005-12-06 15:10:11 +00:00
|
|
|
* Description in header.
|
2005-11-22 12:30:31 +00:00
|
|
|
*/
|
2011-11-22 13:38:43 +00:00
|
|
|
prf_plus_t *prf_plus_create(prf_t *prf, bool counter, chunk_t seed)
|
2005-11-22 12:30:31 +00:00
|
|
|
{
|
|
|
|
private_prf_plus_t *this;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2011-10-02 09:27:38 +00:00
|
|
|
INIT(this,
|
|
|
|
.public = {
|
|
|
|
.get_bytes = _get_bytes,
|
|
|
|
.allocate_bytes = _allocate_bytes,
|
|
|
|
.destroy = _destroy,
|
|
|
|
},
|
|
|
|
.prf = prf,
|
2011-11-22 13:38:43 +00:00
|
|
|
.seed = chunk_clone(seed),
|
2011-11-25 10:26:20 +00:00
|
|
|
.buffer = chunk_alloc(prf->get_block_size(prf)),
|
2019-10-18 14:53:35 +00:00
|
|
|
.apply_prf = counter ? apply_prf_counter : apply_prf,
|
|
|
|
.counter = 0x01,
|
2011-10-02 09:27:38 +00:00
|
|
|
);
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2019-10-18 14:53:35 +00:00
|
|
|
if (!this->apply_prf(this))
|
2011-11-22 13:38:43 +00:00
|
|
|
{
|
2019-10-18 14:53:35 +00:00
|
|
|
destroy(this);
|
|
|
|
return NULL;
|
2011-11-22 13:38:43 +00:00
|
|
|
}
|
|
|
|
return &this->public;
|
2005-11-22 12:30:31 +00:00
|
|
|
}
|