Commit e831ffa2 authored by Jelte Jansen's avatar Jelte Jansen
Browse files

[trac781] bit of cleanup

parent 4eda7bbc
......@@ -76,7 +76,7 @@ CryptoLink::initialize() {
HMAC*
CryptoLink::createHMAC(const void* secret, size_t secret_len,
const HMAC::HashAlgorithm hash_algorithm) {
return new HMAC(secret, secret_len, hash_algorithm);
return (new HMAC(secret, secret_len, hash_algorithm));
}
void
......@@ -84,7 +84,10 @@ signHMAC(const void* data, size_t data_len, const void* secret,
size_t secret_len, const HMAC::HashAlgorithm hash_algorithm,
isc::dns::OutputBuffer& result, size_t len)
{
boost::scoped_ptr<HMAC> hmac(CryptoLink::getCryptoLink().createHMAC(secret, secret_len, hash_algorithm));
boost::scoped_ptr<HMAC> hmac(
CryptoLink::getCryptoLink().createHMAC(secret,
secret_len,
hash_algorithm));
hmac->update(data, data_len);
hmac->sign(result, len);
}
......@@ -95,7 +98,10 @@ verifyHMAC(const void* data, const size_t data_len, const void* secret,
size_t secret_len, const HMAC::HashAlgorithm hash_algorithm,
const void* sig, const size_t sig_len)
{
boost::scoped_ptr<HMAC> hmac(CryptoLink::getCryptoLink().createHMAC(secret, secret_len, hash_algorithm));
boost::scoped_ptr<HMAC> hmac(
CryptoLink::getCryptoLink().createHMAC(secret,
secret_len,
hash_algorithm));
hmac->update(data, data_len);
return (hmac->verify(sig, sig_len));
}
......
......@@ -70,11 +70,13 @@ class CryptoLinkImpl;
/// There is only one way to access it, through getCryptoLink(), which
/// returns a reference to the initialized library. On the first call,
/// it will be initialized automatically. You can however initialize it
/// manually through a call to the initalize(). Any subsequent call to
/// initialize() will be a noop.
/// manually through a call to the initalize(), before your first call
/// to getCryptoLink. Any subsequent call to initialize() will be a
/// noop.
///
/// All other classes within cryptolink should have private
/// constructors, and should have a factory function from this class.
/// \note All other classes within cryptolink should have private
/// constructors as well, and should have a factory function from this
/// class.
///
// Internal note: we can use this class later to initialize and manage
// dynamic (PKCS#11) libs
......@@ -117,10 +119,6 @@ private:
CryptoLinkImpl* impl_;
};
/// Entry point for the API
/// If the library has not been initialized, this will automatically
/// initialize it with default values
/// \brief Create an HMAC signature for the given data
///
/// This is a convenience function that calculates the hmac signature,
......
......@@ -83,9 +83,12 @@ namespace {
OutputBuffer data_buf(data.size());
data_buf.writeData(data.c_str(), data.size());
OutputBuffer hmac_sig(1);
CryptoLink& crypto = CryptoLink::getCryptoLink();
// Sign it
boost::scoped_ptr<HMAC> hmac_sign(CryptoLink::getCryptoLink().createHMAC(secret, secret_len, hash_algorithm));
boost::scoped_ptr<HMAC> hmac_sign(crypto.createHMAC(secret,
secret_len,
hash_algorithm));
hmac_sign->update(data_buf.getData(), data_buf.getLength());
hmac_sign->sign(hmac_sig, hmac_len);
......@@ -93,7 +96,9 @@ namespace {
checkBuffer(hmac_sig, expected_hmac, hmac_len);
// Check whether we can verify it ourselves
boost::scoped_ptr<HMAC> hmac_verify(CryptoLink::getCryptoLink().createHMAC(secret, secret_len, hash_algorithm));
boost::scoped_ptr<HMAC> hmac_verify(crypto.createHMAC(secret,
secret_len,
hash_algorithm));
hmac_verify->update(data_buf.getData(), data_buf.getLength());
EXPECT_TRUE(hmac_verify->verify(hmac_sig.getData(),
hmac_sig.getLength()));
......@@ -111,13 +116,18 @@ namespace {
const HMAC::HashAlgorithm hash_algorithm,
const uint8_t* expected_hmac,
size_t hmac_len) {
boost::scoped_ptr<HMAC> hmac_sign(CryptoLink::getCryptoLink().createHMAC(secret, secret_len, hash_algorithm));
CryptoLink& crypto = CryptoLink::getCryptoLink();
boost::scoped_ptr<HMAC> hmac_sign(crypto.createHMAC(secret,
secret_len,
hash_algorithm));
hmac_sign->update(data.c_str(), data.size());
std::vector<uint8_t> sig = hmac_sign->sign(hmac_len);
ASSERT_EQ(hmac_len, sig.size());
checkData(&sig[0], expected_hmac, hmac_len);
boost::scoped_ptr<HMAC> hmac_verify(CryptoLink::getCryptoLink().createHMAC(secret, secret_len, hash_algorithm));
boost::scoped_ptr<HMAC> hmac_verify(crypto.createHMAC(secret,
secret_len,
hash_algorithm));
hmac_verify->update(data.c_str(), data.size());
EXPECT_TRUE(hmac_verify->verify(&sig[0], sig.size()));
......@@ -131,7 +141,10 @@ namespace {
const HMAC::HashAlgorithm hash_algorithm,
const uint8_t* expected_hmac,
size_t hmac_len) {
boost::scoped_ptr<HMAC> hmac_sign(CryptoLink::getCryptoLink().createHMAC(secret, secret_len, hash_algorithm));
CryptoLink& crypto = CryptoLink::getCryptoLink();
boost::scoped_ptr<HMAC> hmac_sign(crypto.createHMAC(secret,
secret_len,
hash_algorithm));
hmac_sign->update(data.c_str(), data.size());
// note: this is not exception-safe, and will leak, but
......@@ -142,7 +155,9 @@ namespace {
hmac_sign->sign(sig, hmac_len);
checkData(sig, expected_hmac, hmac_len);
boost::scoped_ptr<HMAC> hmac_verify(CryptoLink::getCryptoLink().createHMAC(secret, secret_len, hash_algorithm));
boost::scoped_ptr<HMAC> hmac_verify(crypto.createHMAC(secret,
secret_len,
hash_algorithm));
hmac_verify->update(data.c_str(), data.size());
EXPECT_TRUE(hmac_verify->verify(sig, hmac_len));
......@@ -409,14 +424,17 @@ TEST(CryptoLinkTest, HMAC_SHA256_RFC2202_SIGN) {
namespace {
size_t
sigVectorLength(HMAC::HashAlgorithm alg, size_t len) {
boost::scoped_ptr<HMAC> hmac_sign(CryptoLink::getCryptoLink().createHMAC("asdf", 4, alg));
boost::scoped_ptr<HMAC> hmac_sign(
CryptoLink::getCryptoLink().createHMAC("asdf", 4, alg));
hmac_sign->update("asdf", 4);
const std::vector<uint8_t> sig = hmac_sign->sign(len);
return sig.size();
}
size_t
sigBufferLength(HMAC::HashAlgorithm alg, size_t len) {
boost::scoped_ptr<HMAC> hmac_sign(CryptoLink::getCryptoLink().createHMAC("asdf", 4, alg));
boost::scoped_ptr<HMAC> hmac_sign(
CryptoLink::getCryptoLink().createHMAC("asdf", 4, alg));
hmac_sign->update("asdf", 4);
OutputBuffer sig(0);
hmac_sign->sign(sig, len);
......@@ -468,9 +486,10 @@ TEST(CryptoLinkTest, HMACSigLengthArgument)
TEST(CryptoLinkTest, BadKey) {
OutputBuffer data_buf(0);
OutputBuffer hmac_sig(0);
CryptoLink& crypto = CryptoLink::getCryptoLink();
EXPECT_THROW(CryptoLink::getCryptoLink().createHMAC(NULL, 0, HMAC::MD5), BadKey);
EXPECT_THROW(CryptoLink::getCryptoLink().createHMAC(NULL, 0, HMAC::UNKNOWN), UnsupportedAlgorithm);
EXPECT_THROW(crypto.createHMAC(NULL, 0, HMAC::MD5), BadKey);
EXPECT_THROW(crypto.createHMAC(NULL, 0, HMAC::UNKNOWN), UnsupportedAlgorithm);
EXPECT_THROW(signHMAC(data_buf.getData(), data_buf.getLength(),
NULL, 0, HMAC::MD5, hmac_sig), BadKey);
......@@ -488,9 +507,7 @@ TEST(CryptoLinkTest, BadKey) {
}
TEST(CryptoLinkTest, Singleton) {
/*
CryptoLink& c1 = CryptoLink::getCryptoLink();
CryptoLink& c2 = CryptoLink::getCryptoLink();
ASSERT_EQ(&c1, &c2);
*/
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment