Migrated fips_prf plugin to INIT/METHOD macros

This commit is contained in:
Andreas Steffen
2010-12-04 20:56:21 +01:00
parent c7b778450e
commit 7e02e49112
2 changed files with 35 additions and 34 deletions
+25 -28
View File
@@ -106,7 +106,8 @@ static void chunk_mod(size_t length, chunk_t chunk, u_int8_t buffer[])
* 0xcb, 0x0f, 0x6c, 0x55, 0xba, 0xbb, 0x13, 0x78, * 0xcb, 0x0f, 0x6c, 0x55, 0xba, 0xbb, 0x13, 0x78,
* 0x8e, 0x20, 0xd7, 0x37, 0xa3, 0x27, 0x51, 0x16 * 0x8e, 0x20, 0xd7, 0x37, 0xa3, 0x27, 0x51, 0x16
*/ */
static void get_bytes(private_fips_prf_t *this, chunk_t seed, u_int8_t w[]) METHOD(prf_t, get_bytes, void,
private_fips_prf_t *this, chunk_t seed, u_int8_t w[])
{ {
int i; int i;
u_int8_t xval[this->b]; u_int8_t xval[this->b];
@@ -139,34 +140,26 @@ static void get_bytes(private_fips_prf_t *this, chunk_t seed, u_int8_t w[])
/* 3.3 done already, mod q not used */ /* 3.3 done already, mod q not used */
} }
/** METHOD(prf_t, get_block_size, size_t,
* Implementation of prf_t.get_block_size. private_fips_prf_t *this)
*/
static size_t get_block_size(private_fips_prf_t *this)
{ {
return 2 * this->b; return 2 * this->b;
} }
/** METHOD(prf_t, allocate_bytes, void,
* Implementation of prf_t.allocate_bytes. private_fips_prf_t *this, chunk_t seed, chunk_t *chunk)
*/
static void allocate_bytes(private_fips_prf_t *this, chunk_t seed, chunk_t *chunk)
{ {
*chunk = chunk_alloc(get_block_size(this)); *chunk = chunk_alloc(get_block_size(this));
get_bytes(this, seed, chunk->ptr); get_bytes(this, seed, chunk->ptr);
} }
/** METHOD(prf_t, get_key_size, size_t,
* Implementation of prf_t.get_key_size. private_fips_prf_t *this)
*/
static size_t get_key_size(private_fips_prf_t *this)
{ {
return this->b; return this->b;
} }
/** METHOD(prf_t, set_key, void,
* Implementation of prf_t.set_key. private_fips_prf_t *this, chunk_t key)
*/
static void set_key(private_fips_prf_t *this, chunk_t key)
{ {
/* save key as "key mod 2^b" */ /* save key as "key mod 2^b" */
chunk_mod(this->b, key, this->key); chunk_mod(this->b, key, this->key);
@@ -198,10 +191,8 @@ void g_sha1(private_fips_prf_t *this, chunk_t c, u_int8_t res[])
this->keyed_prf->get_bytes(this->keyed_prf, c, res); this->keyed_prf->get_bytes(this->keyed_prf, c, res);
} }
/** METHOD(prf_t, destroy, void,
* Implementation of prf_t.destroy. private_fips_prf_t *this)
*/
static void destroy(private_fips_prf_t *this)
{ {
this->keyed_prf->destroy(this->keyed_prf); this->keyed_prf->destroy(this->keyed_prf);
free(this->key); free(this->key);
@@ -213,14 +204,20 @@ static void destroy(private_fips_prf_t *this)
*/ */
fips_prf_t *fips_prf_create(pseudo_random_function_t algo) fips_prf_t *fips_prf_create(pseudo_random_function_t algo)
{ {
private_fips_prf_t *this = malloc_thing(private_fips_prf_t); private_fips_prf_t *this;
this->public.prf_interface.get_bytes = (void (*) (prf_t *,chunk_t,u_int8_t*))get_bytes; INIT(this,
this->public.prf_interface.allocate_bytes = (void (*) (prf_t*,chunk_t,chunk_t*))allocate_bytes; .public = {
this->public.prf_interface.get_block_size = (size_t (*) (prf_t*))get_block_size; .prf_interface = {
this->public.prf_interface.get_key_size = (size_t (*) (prf_t*))get_key_size; .get_bytes = _get_bytes,
this->public.prf_interface.set_key = (void (*) (prf_t *,chunk_t))set_key; .allocate_bytes = _allocate_bytes,
this->public.prf_interface.destroy = (void (*) (prf_t *))destroy; .get_block_size = _get_block_size,
.get_key_size = _get_key_size,
.set_key = _set_key,
.destroy = _destroy,
},
},
);
switch (algo) switch (algo)
{ {
@@ -31,10 +31,8 @@ struct private_fips_prf_plugin_t {
fips_prf_plugin_t public; fips_prf_plugin_t public;
}; };
/** METHOD(plugin_t, destroy, void,
* Implementation of fips_prf_plugin_t.destroy private_fips_prf_plugin_t *this)
*/
static void destroy(private_fips_prf_plugin_t *this)
{ {
lib->crypto->remove_prf(lib->crypto, lib->crypto->remove_prf(lib->crypto,
(prf_constructor_t)fips_prf_create); (prf_constructor_t)fips_prf_create);
@@ -46,9 +44,15 @@ static void destroy(private_fips_prf_plugin_t *this)
*/ */
plugin_t *fips_prf_plugin_create() plugin_t *fips_prf_plugin_create()
{ {
private_fips_prf_plugin_t *this = malloc_thing(private_fips_prf_plugin_t); private_fips_prf_plugin_t *this;
this->public.plugin.destroy = (void(*)(plugin_t*))destroy; INIT(this,
.public = {
.plugin = {
.destroy = _destroy,
},
},
);
lib->crypto->add_prf(lib->crypto, PRF_FIPS_SHA1_160, lib->crypto->add_prf(lib->crypto, PRF_FIPS_SHA1_160,
(prf_constructor_t)fips_prf_create); (prf_constructor_t)fips_prf_create);