diff options
author | Aleksandr Grinberg <alex@cessp.it> | 2012-06-20 13:43:47 -0700 |
---|---|---|
committer | Tobias Brunner <tobias@strongswan.org> | 2012-06-25 16:35:05 +0200 |
commit | 0504b0a09f3848b57cbe141b756f1640f8f33cef (patch) | |
tree | 1b15ce738c542f3923fbca3042098f8b2efe870d /src | |
parent | 4faece7b1e6afeb89c77845a7c955e021de85020 (diff) | |
download | strongswan-0504b0a09f3848b57cbe141b756f1640f8f33cef.tar.bz2 strongswan-0504b0a09f3848b57cbe141b756f1640f8f33cef.tar.xz |
Adding OpenSSL HMAC pseudo random functions to openssl plugin
Diffstat (limited to 'src')
-rw-r--r-- | src/libstrongswan/plugins/openssl/Makefile.am | 3 | ||||
-rw-r--r-- | src/libstrongswan/plugins/openssl/openssl_hmac_prf.c | 172 | ||||
-rw-r--r-- | src/libstrongswan/plugins/openssl/openssl_hmac_prf.h | 54 | ||||
-rw-r--r-- | src/libstrongswan/plugins/openssl/openssl_plugin.c | 17 |
4 files changed, 245 insertions, 1 deletions
diff --git a/src/libstrongswan/plugins/openssl/Makefile.am b/src/libstrongswan/plugins/openssl/Makefile.am index ffbeabafa..22bc8652b 100644 --- a/src/libstrongswan/plugins/openssl/Makefile.am +++ b/src/libstrongswan/plugins/openssl/Makefile.am @@ -23,7 +23,8 @@ libstrongswan_openssl_la_SOURCES = \ openssl_ec_public_key.c openssl_ec_public_key.h \ openssl_x509.c openssl_x509.h \ openssl_crl.c openssl_crl.h \ - openssl_rng.c openssl_rng.h + openssl_rng.c openssl_rng.h \ + openssl_hmac_prf.c openssl_hmac_prf.h libstrongswan_openssl_la_LDFLAGS = -module -avoid-version libstrongswan_openssl_la_LIBADD = -lcrypto diff --git a/src/libstrongswan/plugins/openssl/openssl_hmac_prf.c b/src/libstrongswan/plugins/openssl/openssl_hmac_prf.c new file mode 100644 index 000000000..874a09687 --- /dev/null +++ b/src/libstrongswan/plugins/openssl/openssl_hmac_prf.c @@ -0,0 +1,172 @@ +/* + * Copyright (C) 2012 Aleksandr Grinberg + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include <openssl/evp.h> +#include <openssl/hmac.h> + +#include "openssl_hmac_prf.h" + +typedef struct private_openssl_hmac_prf_t private_openssl_hmac_prf_t; + +/** + * Private data of openssl_hmac_prf_t + */ +struct private_openssl_hmac_prf_t { + + /** + * Public part of this class. + */ + openssl_hmac_prf_t public; + + /** + * Hasher to use + */ + const EVP_MD *hasher; + + /** + * Current HMAC context + */ + HMAC_CTX hmac; + + /** + * Key stored for reuse + */ + chunk_t key; +}; + +METHOD(prf_t, get_block_size, size_t, + private_openssl_hmac_prf_t *this) +{ + return EVP_MD_size(this->hasher); +} + +METHOD(prf_t, get_key_size, size_t, + private_openssl_hmac_prf_t *this) +{ + /* for HMAC prfs, IKEv2 uses block size as key size */ + return EVP_MD_size(this->hasher); +} + +/** + * Resets HMAC context + */ +static void reset(private_openssl_hmac_prf_t *this) +{ + HMAC_Init_ex(&this->hmac, this->key.ptr, this->key.len, this->hasher, NULL); +} + +METHOD(prf_t, get_bytes, void, + private_openssl_hmac_prf_t *this, chunk_t seed, u_int8_t *out) +{ + if (out == NULL) + { + HMAC_Update(&this->hmac, seed.ptr, seed.len); + } + else + { + HMAC_Update(&this->hmac, seed.ptr, seed.len); + HMAC_Final(&this->hmac, out, NULL); + reset(this); + } +} + +METHOD(prf_t, allocate_bytes, void, + private_openssl_hmac_prf_t *this, chunk_t seed, chunk_t *out) +{ + if (out == NULL) + { + get_bytes(this, seed, NULL); + } + else + { + *out = chunk_alloc(EVP_MD_size(this->hasher)); + get_bytes(this, seed, out->ptr); + } +} + +METHOD(prf_t, set_key, void, + private_openssl_hmac_prf_t *this, chunk_t key) +{ + chunk_clear(&this->key); + this->key = chunk_clone(key); + reset(this); +} + +METHOD(prf_t, destroy, void, + private_openssl_hmac_prf_t *this) +{ + HMAC_CTX_cleanup(&this->hmac); + chunk_clear(&this->key); + free(this); +} + +/* + * Described in header + */ +openssl_hmac_prf_t *openssl_hmac_prf_create(pseudo_random_function_t algo) +{ + private_openssl_hmac_prf_t *this; + + INIT(this, + .public = { + .prf = { + .get_bytes = _get_bytes, + .allocate_bytes = _allocate_bytes, + .get_block_size = _get_block_size, + .get_key_size = _get_key_size, + .set_key = _set_key, + .destroy = _destroy, + }, + }, + ); + + switch (algo) + { + case PRF_HMAC_MD5: + this->hasher = EVP_get_digestbyname("md5"); + break; + case PRF_HMAC_SHA1: + this->hasher = EVP_get_digestbyname("sha1"); + break; + case PRF_HMAC_SHA2_256: + this->hasher = EVP_get_digestbyname("sha256"); + break; + case PRF_HMAC_SHA2_384: + this->hasher = EVP_get_digestbyname("sha384"); + break; + case PRF_HMAC_SHA2_512: + this->hasher = EVP_get_digestbyname("sha512"); + break; + default: + break; + } + + if (!this->hasher) + { + free(this); + return NULL; + } + + HMAC_CTX_init(&this->hmac); + + return &this->public; +} diff --git a/src/libstrongswan/plugins/openssl/openssl_hmac_prf.h b/src/libstrongswan/plugins/openssl/openssl_hmac_prf.h new file mode 100644 index 000000000..1758da238 --- /dev/null +++ b/src/libstrongswan/plugins/openssl/openssl_hmac_prf.h @@ -0,0 +1,54 @@ +/* + * Copyright (C) 2012 Aleksandr Grinberg + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +/** + * @defgroup openssl_hmac_prf openssl_hmac_prf + * @{ @ingroup openssl_p + */ + +#ifndef OPENSSL_HMAC_PRF_H_ +#define OPENSSL_HMAC_PRF_H_ + +typedef struct openssl_hmac_prf_t openssl_hmac_prf_t; + +#include <crypto/prfs/prf.h> + +/** + * Implementation of pseudo random functions using OpenSSL. + */ +struct openssl_hmac_prf_t { + + /** + * Implements prf_t interface. + */ + prf_t prf; +}; + +/** + * Constructor to create openssl_hmac_prf_t. + * + * @param algo algorithm + * @return openssl_hmac_prf_t, NULL if not supported + */ +openssl_hmac_prf_t *openssl_hmac_prf_create(pseudo_random_function_t algo); + +#endif /** OPENSSL_HMAC_PRF_H_ @}*/ diff --git a/src/libstrongswan/plugins/openssl/openssl_plugin.c b/src/libstrongswan/plugins/openssl/openssl_plugin.c index c95237758..5ba00d4da 100644 --- a/src/libstrongswan/plugins/openssl/openssl_plugin.c +++ b/src/libstrongswan/plugins/openssl/openssl_plugin.c @@ -41,6 +41,7 @@ #include "openssl_x509.h" #include "openssl_crl.h" #include "openssl_rng.h" +#include "openssl_hmac_prf.h" typedef struct private_openssl_plugin_t private_openssl_plugin_t; @@ -261,6 +262,22 @@ METHOD(plugin_t, get_features, int, PLUGIN_REGISTER(PRF, openssl_sha1_prf_create), PLUGIN_PROVIDE(PRF, PRF_KEYED_SHA1), #endif +#ifndef OPENSSL_NO_HMAC + PLUGIN_REGISTER(PRF, openssl_hmac_prf_create), +#ifndef OPENSSL_NO_MD5 + PLUGIN_PROVIDE(PRF, PRF_HMAC_MD5), +#endif +#ifndef OPENSSL_NO_SHA1 + PLUGIN_PROVIDE(PRF, PRF_HMAC_SHA1), +#endif +#ifndef OPENSSL_NO_SHA256 + PLUGIN_PROVIDE(PRF, PRF_HMAC_SHA2_256), +#endif +#ifndef OPENSSL_NO_SHA512 + PLUGIN_PROVIDE(PRF, PRF_HMAC_SHA2_384), + PLUGIN_PROVIDE(PRF, PRF_HMAC_SHA2_512), +#endif +#endif /* OPENSSL_NO_HMAC */ #ifndef OPENSSL_NO_DH /* MODP DH groups */ PLUGIN_REGISTER(DH, openssl_diffie_hellman_create), |