]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[sedhcpv6] Added getHashAlgorithm to legacy cryptolink
authorFrancis Dupont <fdupont@isc.org>
Wed, 27 May 2015 20:38:30 +0000 (22:38 +0200)
committerFrancis Dupont <fdupont@isc.org>
Wed, 27 May 2015 20:38:30 +0000 (22:38 +0200)
src/lib/cryptolink/botan_hash.cc
src/lib/cryptolink/botan_hmac.cc
src/lib/cryptolink/crypto_hash.h
src/lib/cryptolink/crypto_hmac.h
src/lib/cryptolink/openssl_asym.cc
src/lib/cryptolink/openssl_hash.cc
src/lib/cryptolink/openssl_hmac.cc
src/lib/cryptolink/tests/hash_unittests.cc
src/lib/cryptolink/tests/hmac_unittests.cc

index 78f944ed392480d0ea504d8bfe8cbd49877ae077..9451f0a1f56da060b2efa8167c23b14c55f36e06 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2014  Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2014, 2015  Internet Systems Consortium, Inc. ("ISC")
 //
 // Permission to use, copy, modify, and/or distribute this software for any
 // purpose with or without fee is hereby granted, provided that the above
@@ -64,7 +64,8 @@ public:
     /// @brief Constructor for specific hash algorithm
     ///
     /// @param hash_algorithm The hash algorithm
-    explicit HashImpl(const HashAlgorithm hash_algorithm) {
+    explicit HashImpl(const HashAlgorithm hash_algorithm)
+    : hash_algorithm_(hash_algorithm), hash_() {
         Botan::HashFunction* hash;
         try {
             hash = Botan::get_hash(btn::getHashAlgorithmName(hash_algorithm));
@@ -82,6 +83,11 @@ public:
     /// @brief Destructor
     ~HashImpl() { }
 
+    /// @brief Returns the HashAlgorithm of the object
+    HashAlgorithm getHashAlgorithm() const {
+        return (hash_algorithm_);
+    }
+
     /// @brief Returns the output size of the digest
     ///
     /// @return output size of the digest
@@ -157,7 +163,10 @@ public:
     }
 
 private:
-    /// \brief The protected pointer to the Botan HashFunction object
+    /// @brief The hash algorithm
+    HashAlgorithm hash_algorithm_;
+
+    /// @brief The protected pointer to the Botan HashFunction object
     boost::scoped_ptr<Botan::HashFunction> hash_;
 };
 
@@ -170,6 +179,11 @@ Hash::~Hash() {
     delete impl_;
 }
 
+HashAlgorithm
+Hash:getHashAlgorithm() const {
+    return (impl_->getHashAlgorithm());
+}
+
 size_t
 Hash::getOutputLength() const {
     return (impl_->getOutputLength());
index dfb43e63599146699e99be28526c723bbb658d93..4b19202ae0660fccaa37616566f74383db281fe8 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2011, 2014  Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2011, 2014, 2015  Internet Systems Consortium, Inc. ("ISC")
 //
 // Permission to use, copy, modify, and/or distribute this software for any
 // purpose with or without fee is hereby granted, provided that the above
@@ -42,7 +42,8 @@ public:
     /// @param secret_len The length of the secret
     /// @param hash_algorithm The hash algorithm
     explicit HMACImpl(const void* secret, size_t secret_len,
-                      const HashAlgorithm hash_algorithm) {
+                      const HashAlgorithm hash_algorithm)
+    : hash_algorithm_(hash_algorithm), hmac_() {
         Botan::HashFunction* hash;
         try {
             hash = Botan::get_hash(btn::getHashAlgorithmName(hash_algorithm));
@@ -95,6 +96,11 @@ public:
     ~HMACImpl() {
     }
 
+    /// @brief Returns the HashAlgorithm of the object
+    HashAlgorithm getHashAlgorithm() const {
+        return (hash_algorithm_);
+    }
+
     /// @brief Returns the output size of the digest
     ///
     /// @return output size of the digest
@@ -195,7 +201,10 @@ public:
     }
 
 private:
-    /// \brief The protected pointer to the Botan HMAC object
+    /// @brief The hash algorithm
+    HashAlgorithm hash_algorithm_;
+
+    /// @brief The protected pointer to the Botan HMAC object
     boost::scoped_ptr<Botan::HMAC> hmac_;
 };
 
@@ -209,6 +218,11 @@ HMAC::~HMAC() {
     delete impl_;
 }
 
+HashAlgorithm
+HMAC:getHashAlgorithm() const {
+    return (impl_->getHashAlgorithm());
+}
+
 size_t
 HMAC::getOutputLength() const {
     return (impl_->getOutputLength());
index 2ad2e6894d6064394a33b6a2ea75d15869a2a6e0..0e37ed5d1042f9b1f30458e6b7cc7cfda52eb395 100644 (file)
@@ -50,6 +50,9 @@ public:
     /// \brief Destructor
     ~Hash();
 
+    /// \brief Returns the HashAlgorithm of the object
+    HashAlgorithm getHashAlgorithm() const;
+
     /// \brief Returns the output size of the digest
     ///
     /// \return output size of the digest
index be15e7ada0ddac76665a1e8f814ff34d2e16f157..040e5731825991603a55a71eb37b812b066bf9a2 100644 (file)
@@ -60,6 +60,9 @@ public:
     /// \brief Destructor
     ~HMAC();
 
+    /// \brief Returns the HashAlgorithm of the object
+    HashAlgorithm getHashAlgorithm() const;
+
     /// \brief Returns the output size of the digest
     ///
     /// \return output size of the digest
index 01831e43958b3862e32696fc6065dbb36e624acb..d32e0aff6f32d64fde78e0eb5d0dd2b044f369ca 100644 (file)
@@ -678,7 +678,7 @@ public:
                 }
                 found = true;
                 if (line[strlen(line) - 1] == '\n') {
-                    line[strlen(line) -1] = 0;
+                    line[strlen(line) - 1] = 0;
                 }
                 break;
             }
index a8bcbf112f831fc518b186eb54e259ad80d46310..96cd5250cc062987470d6341f3e7640b26d2549c 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2014  Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2014, 2015  Internet Systems Consortium, Inc. ("ISC")
 //
 // Permission to use, copy, modify, and/or distribute this software for any
 // purpose with or without fee is hereby granted, provided that the above
@@ -62,7 +62,8 @@ public:
     /// @brief Constructor for specific hash algorithm
     ///
     /// @param hash_algorithm The hash algorithm
-    explicit HashImpl(const HashAlgorithm hash_algorithm) {
+    explicit HashImpl(const HashAlgorithm hash_algorithm)
+    : hash_algorithm_(hash_algorithm), md_() {
         const EVP_MD* algo = ossl::getHashAlgorithm(hash_algorithm);
         if (algo == 0) {
             isc_throw(isc::cryptolink::UnsupportedAlgorithm,
@@ -84,6 +85,11 @@ public:
         }
     }
 
+    /// @brief Returns the HashAlgorithm of the object
+    HashAlgorithm getHashAlgorithm() const {
+        return (hash_algorithm_);
+    }
+
     /// @brief Returns the output size of the digest
     ///
     /// @return output size of the digest
@@ -138,6 +144,9 @@ public:
     }
 
 private:
+    /// @brief The hash algorithm
+    HashAlgorithm hash_algorithm_;
+
     /// @brief The protected pointer to the OpenSSL EVP_MD_CTX structure
     boost::scoped_ptr<EVP_MD_CTX> md_;
 };
@@ -151,6 +160,11 @@ Hash::~Hash() {
     delete impl_;
 }
 
+HashAlgorithm
+Hash::getHashAlgorithm() const {
+    return (impl_->getHashAlgorithm());
+}
+
 size_t
 Hash::getOutputLength() const {
     return (impl_->getOutputLength());
index 831b33d5d0b4b1dbbc36a45cd1805949f9533869..ed1a618177abe66c13672079df64e47cb3ba4905 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2014  Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2014, 2015  Internet Systems Consortium, Inc. ("ISC")
 //
 // Permission to use, copy, modify, and/or distribute this software for any
 // purpose with or without fee is hereby granted, provided that the above
@@ -38,7 +38,8 @@ public:
     /// @param secret_len The length of the secret
     /// @param hash_algorithm The hash algorithm
     explicit HMACImpl(const void* secret, size_t secret_len,
-                      const HashAlgorithm hash_algorithm) {
+                      const HashAlgorithm hash_algorithm)
+    : hash_algorithm_(hash_algorithm), md_() {
         const EVP_MD* algo = ossl::getHashAlgorithm(hash_algorithm);
         if (algo == 0) {
             isc_throw(UnsupportedAlgorithm,
@@ -64,6 +65,11 @@ public:
         }
     }
 
+    /// @brief Returns the HashAlgorithm of the object
+    HashAlgorithm getHashAlgorithm() const {
+        return (hash_algorithm_);
+    }
+
     /// @brief Returns the output size of the digest
     ///
     /// @return output size of the digest
@@ -138,6 +144,8 @@ public:
     }
 
 private:
+    /// @brief The hash algorithm
+    HashAlgorithm hash_algorithm_;
 
     /// @brief The protected pointer to the OpenSSL HMAC_CTX structure
     boost::scoped_ptr<HMAC_CTX> md_;
@@ -153,6 +161,11 @@ HMAC::~HMAC() {
     delete impl_;
 }
 
+HashAlgorithm
+HMAC::getHashAlgorithm() const {
+    return (impl_->getHashAlgorithm());
+}
+
 size_t
 HMAC::getOutputLength() const {
     return (impl_->getOutputLength());
index 4c122d1e6d9c0f26124262f5b650593829269774..6011f5a1846aab1a8a5c2724966b2db754c56d52 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2014  Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2014, 2015  Internet Systems Consortium, Inc. ("ISC")
 //
 // Permission to use, copy, modify, and/or distribute this software for any
 // purpose with or without fee is hereby granted, provided that the above
@@ -526,6 +526,28 @@ TEST(HashTest, SHA512_RFC6234) {
                SHA512, hash_expected4, 64);
 }
 
+namespace {
+    /// @brief Get the hash algorithm
+    /// @param alg Hash algorithm enum
+    /// @return Hash algorithm enum
+    HashAlgorithm
+    digestHashAlgorithm(HashAlgorithm alg) {
+        boost::shared_ptr<Hash> hash_digest(
+            CryptoLink::getCryptoLink().createHash(alg),
+            deleteHash);
+        return (hash_digest->getHashAlgorithm());
+    }
+}
+
+TEST(HashTest, HashAlgorithm) {
+    EXPECT_EQ(MD5, digestHashAlgorithm(MD5));
+    EXPECT_EQ(SHA1, digestHashAlgorithm(SHA1));
+    EXPECT_EQ(SHA256, digestHashAlgorithm(SHA256));
+    EXPECT_EQ(SHA224, digestHashAlgorithm(SHA224));
+    EXPECT_EQ(SHA384, digestHashAlgorithm(SHA384));
+    EXPECT_EQ(SHA512, digestHashAlgorithm(SHA512));
+}
+
 namespace {
     /// @brief Compute the vector digest length
     /// @param alg Hash algorithm enum
index 34320a96bce07a380d724634464f9a3ae1723163..f148dca59e3d960cb74a5dcf78c60d4e0ee60006 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2011, 2014  Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2011, 2014, 2015  Internet Systems Consortium, Inc. ("ISC")
 //
 // Permission to use, copy, modify, and/or distribute this software for any
 // purpose with or without fee is hereby granted, provided that the above
@@ -563,6 +563,28 @@ TEST(HMACTest, HMAC_SHA256_RFC2202_SIGN_TRUNCATED) {
                hmac_expected5, 16);
 }
 
+namespace {
+    /// @brief Get the hash algorithm
+    /// @param alg Hash algorithm enum
+    /// @return Hash algorithm enum
+    HashAlgorithm
+    signHashAlgorithm(HashAlgorithm alg) {
+        boost::shared_ptr<HMAC> hmac_sign(
+            CryptoLink::getCryptoLink().createHMAC("asdf", 4, alg),
+            deleteHMAC);
+        return (hmac_sign->getHashAlgorithm());
+    }
+}
+
+TEST(HMACTest, HashAlgorithm) {
+    EXPECT_EQ(MD5, signHashAlgorithm(MD5));
+    EXPECT_EQ(SHA1, signHashAlgorithm(SHA1));
+    EXPECT_EQ(SHA256, signHashAlgorithm(SHA256));
+    EXPECT_EQ(SHA224, signHashAlgorithm(SHA224));
+    EXPECT_EQ(SHA384, signHashAlgorithm(SHA384));
+    EXPECT_EQ(SHA512, signHashAlgorithm(SHA512));
+}
+
 namespace {
     /// @brief Compute the vector signature length
     /// @param alg Hash algorithm enum