diff options
Diffstat (limited to 'src/libstrongswan/plugins/sha2')
-rw-r--r-- | src/libstrongswan/plugins/sha2/Makefile.in | 7 | ||||
-rw-r--r-- | src/libstrongswan/plugins/sha2/sha2_hasher.c | 279 | ||||
-rw-r--r-- | src/libstrongswan/plugins/sha2/sha2_plugin.c | 27 |
3 files changed, 158 insertions, 155 deletions
diff --git a/src/libstrongswan/plugins/sha2/Makefile.in b/src/libstrongswan/plugins/sha2/Makefile.in index bdc235555..c99f30e43 100644 --- a/src/libstrongswan/plugins/sha2/Makefile.in +++ b/src/libstrongswan/plugins/sha2/Makefile.in @@ -191,6 +191,9 @@ am__leading_dot = @am__leading_dot@ am__quote = @am__quote@ am__tar = @am__tar@ am__untar = @am__untar@ +attest_plugins = @attest_plugins@ +axis2c_CFLAGS = @axis2c_CFLAGS@ +axis2c_LIBS = @axis2c_LIBS@ bindir = @bindir@ build = @build@ build_alias = @build_alias@ @@ -199,6 +202,7 @@ build_os = @build_os@ build_vendor = @build_vendor@ builddir = @builddir@ c_plugins = @c_plugins@ +clearsilver_LIBS = @clearsilver_LIBS@ datadir = @datadir@ datarootdir = @datarootdir@ dbusservicedir = @dbusservicedir@ @@ -215,11 +219,13 @@ host_cpu = @host_cpu@ host_os = @host_os@ host_vendor = @host_vendor@ htmldir = @htmldir@ +imcvdir = @imcvdir@ includedir = @includedir@ infodir = @infodir@ install_sh = @install_sh@ ipsecdir = @ipsecdir@ ipsecgroup = @ipsecgroup@ +ipseclibdir = @ipseclibdir@ ipsecuser = @ipsecuser@ libcharon_plugins = @libcharon_plugins@ libdir = @libdir@ @@ -263,6 +269,7 @@ sharedstatedir = @sharedstatedir@ soup_CFLAGS = @soup_CFLAGS@ soup_LIBS = @soup_LIBS@ srcdir = @srcdir@ +starter_plugins = @starter_plugins@ strongswan_conf = @strongswan_conf@ sysconfdir = @sysconfdir@ systemdsystemunitdir = @systemdsystemunitdir@ diff --git a/src/libstrongswan/plugins/sha2/sha2_hasher.c b/src/libstrongswan/plugins/sha2/sha2_hasher.c index d407fad1b..60fe4bd20 100644 --- a/src/libstrongswan/plugins/sha2/sha2_hasher.c +++ b/src/libstrongswan/plugins/sha2/sha2_hasher.c @@ -426,71 +426,90 @@ static void sha512_final(private_sha512_hasher_t *ctx) } while(++j < 8); } -/** - * Implementation of hasher_t.get_hash for SHA224. - */ -static void get_hash224(private_sha256_hasher_t *this, - chunk_t chunk, u_int8_t *buffer) +METHOD(hasher_t, reset224, void, + private_sha256_hasher_t *this) +{ + memcpy(&this->sha_H[0], &sha224_hashInit[0], sizeof(this->sha_H)); + this->sha_blocks = 0; + this->sha_bufCnt = 0; +} + +METHOD(hasher_t, reset256, void, + private_sha256_hasher_t *this) +{ + memcpy(&this->sha_H[0], &sha256_hashInit[0], sizeof(this->sha_H)); + this->sha_blocks = 0; + this->sha_bufCnt = 0; +} + +METHOD(hasher_t, reset384, void, + private_sha512_hasher_t *this) +{ + memcpy(&this->sha_H[0], &sha384_hashInit[0], sizeof(this->sha_H)); + this->sha_blocks = 0; + this->sha_blocksMSB = 0; + this->sha_bufCnt = 0; +} + +METHOD(hasher_t, reset512, void, + private_sha512_hasher_t *this) +{ + memcpy(&this->sha_H[0], &sha512_hashInit[0], sizeof(this->sha_H)); + this->sha_blocks = 0; + this->sha_blocksMSB = 0; + this->sha_bufCnt = 0; +} + +METHOD(hasher_t, get_hash224, void, + private_sha256_hasher_t *this, chunk_t chunk, u_int8_t *buffer) { sha256_write(this, chunk.ptr, chunk.len); if (buffer != NULL) { sha256_final(this); memcpy(buffer, this->sha_out, HASH_SIZE_SHA224); - this->public.hasher_interface.reset(&(this->public.hasher_interface)); + reset224(this); } } -/** - * Implementation of hasher_t.get_hash for SHA256. - */ -static void get_hash256(private_sha256_hasher_t *this, - chunk_t chunk, u_int8_t *buffer) +METHOD(hasher_t, get_hash256, void, + private_sha256_hasher_t *this, chunk_t chunk, u_int8_t *buffer) { sha256_write(this, chunk.ptr, chunk.len); if (buffer != NULL) { sha256_final(this); memcpy(buffer, this->sha_out, HASH_SIZE_SHA256); - this->public.hasher_interface.reset(&(this->public.hasher_interface)); + reset256(this); } } -/** - * Implementation of hasher_t.get_hash for SHA384. - */ -static void get_hash384(private_sha512_hasher_t *this, - chunk_t chunk, u_int8_t *buffer) +METHOD(hasher_t, get_hash384, void, + private_sha512_hasher_t *this, chunk_t chunk, u_int8_t *buffer) { sha512_write(this, chunk.ptr, chunk.len); if (buffer != NULL) { sha512_final(this); memcpy(buffer, this->sha_out, HASH_SIZE_SHA384); - this->public.hasher_interface.reset(&(this->public.hasher_interface)); + reset384(this); } } -/** - * Implementation of hasher_t.get_hash for SHA512. - */ -static void get_hash512(private_sha512_hasher_t *this, - chunk_t chunk, u_int8_t *buffer) +METHOD(hasher_t, get_hash512, void, + private_sha512_hasher_t *this, chunk_t chunk, u_int8_t *buffer) { sha512_write(this, chunk.ptr, chunk.len); if (buffer != NULL) { sha512_final(this); memcpy(buffer, this->sha_out, HASH_SIZE_SHA512); - this->public.hasher_interface.reset(&(this->public.hasher_interface)); + reset512(this); } } -/** - * Implementation of hasher_t.allocate_hash for SHA224. - */ -static void allocate_hash224(private_sha256_hasher_t *this, - chunk_t chunk, chunk_t *hash) +METHOD(hasher_t, allocate_hash224, void, + private_sha256_hasher_t *this, chunk_t chunk, chunk_t *hash) { chunk_t allocated_hash; @@ -500,16 +519,13 @@ static void allocate_hash224(private_sha256_hasher_t *this, sha256_final(this); allocated_hash = chunk_alloc(HASH_SIZE_SHA224); memcpy(allocated_hash.ptr, this->sha_out, HASH_SIZE_SHA224); - this->public.hasher_interface.reset(&(this->public.hasher_interface)); + reset224(this); *hash = allocated_hash; } } -/** - * Implementation of hasher_t.allocate_hash for SHA256. - */ -static void allocate_hash256(private_sha256_hasher_t *this, - chunk_t chunk, chunk_t *hash) +METHOD(hasher_t, allocate_hash256, void, + private_sha256_hasher_t *this, chunk_t chunk, chunk_t *hash) { chunk_t allocated_hash; @@ -519,16 +535,13 @@ static void allocate_hash256(private_sha256_hasher_t *this, sha256_final(this); allocated_hash = chunk_alloc(HASH_SIZE_SHA256); memcpy(allocated_hash.ptr, this->sha_out, HASH_SIZE_SHA256); - this->public.hasher_interface.reset(&(this->public.hasher_interface)); + reset256(this); *hash = allocated_hash; } } -/** - * Implementation of hasher_t.allocate_hash for SHA384. - */ -static void allocate_hash384(private_sha512_hasher_t *this, - chunk_t chunk, chunk_t *hash) +METHOD(hasher_t, allocate_hash384, void, + private_sha512_hasher_t *this, chunk_t chunk, chunk_t *hash) { chunk_t allocated_hash; @@ -538,16 +551,13 @@ static void allocate_hash384(private_sha512_hasher_t *this, sha512_final(this); allocated_hash = chunk_alloc(HASH_SIZE_SHA384); memcpy(allocated_hash.ptr, this->sha_out, HASH_SIZE_SHA384); - this->public.hasher_interface.reset(&(this->public.hasher_interface)); + reset384(this); *hash = allocated_hash; } } -/** - * Implementation of hasher_t.allocate_hash for SHA512. - */ -static void allocate_hash512(private_sha512_hasher_t *this, - chunk_t chunk, chunk_t *hash) +METHOD(hasher_t, allocate_hash512, void, + private_sha512_hasher_t *this, chunk_t chunk, chunk_t *hash) { chunk_t allocated_hash; @@ -557,89 +567,37 @@ static void allocate_hash512(private_sha512_hasher_t *this, sha512_final(this); allocated_hash = chunk_alloc(HASH_SIZE_SHA512); memcpy(allocated_hash.ptr, this->sha_out, HASH_SIZE_SHA512); - this->public.hasher_interface.reset(&(this->public.hasher_interface)); + reset512(this); *hash = allocated_hash; } } -/** - * Implementation of hasher_t.get_hash_size for SHA224. - */ -static size_t get_hash_size224(private_sha256_hasher_t *this) +METHOD(hasher_t, get_hash_size224, size_t, + private_sha256_hasher_t *this) { return HASH_SIZE_SHA224; } -/** - * Implementation of hasher_t.get_hash_size for SHA256. - */ -static size_t get_hash_size256(private_sha256_hasher_t *this) +METHOD(hasher_t, get_hash_size256, size_t, + private_sha256_hasher_t *this) { return HASH_SIZE_SHA256; } -/** - * Implementation of hasher_t.get_hash_size for SHA384. - */ -static size_t get_hash_size384(private_sha512_hasher_t *this) +METHOD(hasher_t, get_hash_size384, size_t, + private_sha512_hasher_t *this) { return HASH_SIZE_SHA384; } -/** - * Implementation of hasher_t.get_hash_size for SHA512. - */ -static size_t get_hash_size512(private_sha512_hasher_t *this) +METHOD(hasher_t, get_hash_size512, size_t, + private_sha512_hasher_t *this) { return HASH_SIZE_SHA512; } -/** - * Implementation of hasher_t.reset for SHA224 - */ -static void reset224(private_sha256_hasher_t *ctx) -{ - memcpy(&ctx->sha_H[0], &sha224_hashInit[0], sizeof(ctx->sha_H)); - ctx->sha_blocks = 0; - ctx->sha_bufCnt = 0; -} - -/** - * Implementation of hasher_t.reset for SHA256 - */ -static void reset256(private_sha256_hasher_t *ctx) -{ - memcpy(&ctx->sha_H[0], &sha256_hashInit[0], sizeof(ctx->sha_H)); - ctx->sha_blocks = 0; - ctx->sha_bufCnt = 0; -} - -/** - * Implementation of hasher_t.reset for SHA384 - */ -static void reset384(private_sha512_hasher_t *ctx) -{ - memcpy(&ctx->sha_H[0], &sha384_hashInit[0], sizeof(ctx->sha_H)); - ctx->sha_blocks = 0; - ctx->sha_blocksMSB = 0; - ctx->sha_bufCnt = 0; -} - -/** - * Implementation of hasher_t.reset for SHA512 - */ -static void reset512(private_sha512_hasher_t *ctx) -{ - memcpy(&ctx->sha_H[0], &sha512_hashInit[0], sizeof(ctx->sha_H)); - ctx->sha_blocks = 0; - ctx->sha_blocksMSB = 0; - ctx->sha_bufCnt = 0; -} - -/** - * Implementation of hasher_t.destroy. - */ -static void destroy(sha2_hasher_t *this) +METHOD(hasher_t, destroy, void, + sha2_hasher_t *this) { free(this); } @@ -649,46 +607,81 @@ static void destroy(sha2_hasher_t *this) */ sha2_hasher_t *sha2_hasher_create(hash_algorithm_t algorithm) { - sha2_hasher_t *this; - switch (algorithm) { case HASH_SHA224: - this = (sha2_hasher_t*)malloc_thing(private_sha256_hasher_t); - this->hasher_interface.reset = (void(*)(hasher_t*))reset224; - this->hasher_interface.get_hash_size = (size_t(*)(hasher_t*))get_hash_size224; - this->hasher_interface.get_hash = (void(*)(hasher_t*,chunk_t,u_int8_t*))get_hash224; - this->hasher_interface.allocate_hash = (void(*)(hasher_t*,chunk_t,chunk_t*))allocate_hash224; - break; + { + private_sha256_hasher_t *this; + + INIT(this, + .public = { + .hasher_interface = { + .reset = _reset224, + .get_hash_size = _get_hash_size224, + .get_hash = _get_hash224, + .allocate_hash = _allocate_hash224, + .destroy = _destroy, + }, + }, + ); + reset224(this); + return &this->public; + } case HASH_SHA256: - this = (sha2_hasher_t*)malloc_thing(private_sha256_hasher_t); - this->hasher_interface.reset = (void(*)(hasher_t*))reset256; - this->hasher_interface.get_hash_size = (size_t(*)(hasher_t*))get_hash_size256; - this->hasher_interface.get_hash = (void(*)(hasher_t*,chunk_t,u_int8_t*))get_hash256; - this->hasher_interface.allocate_hash = (void(*)(hasher_t*,chunk_t,chunk_t*))allocate_hash256; - break; + { + private_sha256_hasher_t *this; + + INIT(this, + .public = { + .hasher_interface = { + .reset = _reset256, + .get_hash_size = _get_hash_size256, + .get_hash = _get_hash256, + .allocate_hash = _allocate_hash256, + .destroy = _destroy, + }, + }, + ); + reset256(this); + return &this->public; + } case HASH_SHA384: - /* uses SHA512 data structure */ - this = (sha2_hasher_t*)malloc_thing(private_sha512_hasher_t); - this->hasher_interface.reset = (void(*)(hasher_t*))reset384; - this->hasher_interface.get_hash_size = (size_t(*)(hasher_t*))get_hash_size384; - this->hasher_interface.get_hash = (void(*)(hasher_t*,chunk_t,u_int8_t*))get_hash384; - this->hasher_interface.allocate_hash = (void(*)(hasher_t*,chunk_t,chunk_t*))allocate_hash384; - break; + { + private_sha512_hasher_t *this; + + INIT(this, + .public = { + .hasher_interface = { + .reset = _reset384, + .get_hash_size = _get_hash_size384, + .get_hash = _get_hash384, + .allocate_hash = _allocate_hash384, + .destroy = _destroy, + }, + }, + ); + reset384(this); + return &this->public; + } case HASH_SHA512: - this = (sha2_hasher_t*)malloc_thing(private_sha512_hasher_t); - this->hasher_interface.reset = (void(*)(hasher_t*))reset512; - this->hasher_interface.get_hash_size = (size_t(*)(hasher_t*))get_hash_size512; - this->hasher_interface.get_hash = (void(*)(hasher_t*,chunk_t,u_int8_t*))get_hash512; - this->hasher_interface.allocate_hash = (void(*)(hasher_t*,chunk_t,chunk_t*))allocate_hash512; - break; + { + private_sha512_hasher_t *this; + + INIT(this, + .public = { + .hasher_interface = { + .reset = _reset512, + .get_hash_size = _get_hash_size512, + .get_hash = _get_hash512, + .allocate_hash = _allocate_hash512, + .destroy = _destroy, + }, + }, + ); + reset512(this); + return &this->public; + } default: return NULL; } - this->hasher_interface.destroy = (void(*)(hasher_t*))destroy; - - /* initialize */ - this->hasher_interface.reset(&this->hasher_interface); - - return this; } diff --git a/src/libstrongswan/plugins/sha2/sha2_plugin.c b/src/libstrongswan/plugins/sha2/sha2_plugin.c index 4ec03a268..94a7ccd61 100644 --- a/src/libstrongswan/plugins/sha2/sha2_plugin.c +++ b/src/libstrongswan/plugins/sha2/sha2_plugin.c @@ -37,11 +37,23 @@ METHOD(plugin_t, get_name, char*, return "sha2"; } +METHOD(plugin_t, get_features, int, + private_sha2_plugin_t *this, plugin_feature_t *features[]) +{ + static plugin_feature_t f[] = { + PLUGIN_REGISTER(HASHER, sha2_hasher_create), + PLUGIN_PROVIDE(HASHER, HASH_SHA224), + PLUGIN_PROVIDE(HASHER, HASH_SHA256), + PLUGIN_PROVIDE(HASHER, HASH_SHA384), + PLUGIN_PROVIDE(HASHER, HASH_SHA512), + }; + *features = f; + return countof(f); +} + METHOD(plugin_t, destroy, void, private_sha2_plugin_t *this) { - lib->crypto->remove_hasher(lib->crypto, - (hasher_constructor_t)sha2_hasher_create); free(this); } @@ -56,21 +68,12 @@ plugin_t *sha2_plugin_create() .public = { .plugin = { .get_name = _get_name, - .reload = (void*)return_false, + .get_features = _get_features, .destroy = _destroy, }, }, ); - lib->crypto->add_hasher(lib->crypto, HASH_SHA224, get_name(this), - (hasher_constructor_t)sha2_hasher_create); - lib->crypto->add_hasher(lib->crypto, HASH_SHA256, get_name(this), - (hasher_constructor_t)sha2_hasher_create); - lib->crypto->add_hasher(lib->crypto, HASH_SHA384, get_name(this), - (hasher_constructor_t)sha2_hasher_create); - lib->crypto->add_hasher(lib->crypto, HASH_SHA512, get_name(this), - (hasher_constructor_t)sha2_hasher_create); - return &this->public.plugin; } |