aboutsummaryrefslogtreecommitdiffstats
path: root/libtransport/src/auth/crypto_hash.cc
diff options
context:
space:
mode:
Diffstat (limited to 'libtransport/src/auth/crypto_hash.cc')
-rw-r--r--libtransport/src/auth/crypto_hash.cc150
1 files changed, 61 insertions, 89 deletions
diff --git a/libtransport/src/auth/crypto_hash.cc b/libtransport/src/auth/crypto_hash.cc
index b4b0a8b81..cf781d08e 100644
--- a/libtransport/src/auth/crypto_hash.cc
+++ b/libtransport/src/auth/crypto_hash.cc
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2017-2019 Cisco and/or its affiliates.
+ * Copyright (c) 2021-2022 Cisco and/or its affiliates.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
@@ -13,9 +13,9 @@
* limitations under the License.
*/
+#include <glog/logging.h>
#include <hicn/transport/auth/crypto_hash.h>
-
-using namespace std;
+#include <hicn/transport/core/global_object_pool.h>
namespace transport {
namespace auth {
@@ -27,90 +27,77 @@ CryptoHash::CryptoHash(const CryptoHash &other)
digest_(other.digest_),
digest_size_(other.digest_size_) {}
-CryptoHash::CryptoHash(CryptoHash &&other)
- : digest_type_(move(other.digest_type_)),
- digest_(other.digest_),
- digest_size_(other.digest_size_) {
- other.reset();
-}
+CryptoHash::CryptoHash(CryptoHash &&other) noexcept
+ : digest_type_(std::move(other.digest_type_)),
+ digest_(std::move(other.digest_)),
+ digest_size_(other.digest_size_) {}
-CryptoHash::CryptoHash(CryptoHashType hash_type) { setType(hash_type); }
+CryptoHash::CryptoHash(CryptoHashType hash_type)
+ : digest_(core::PacketManager<>::getInstance().getMemBuf()) {
+ setType(hash_type);
+}
CryptoHash::CryptoHash(const uint8_t *hash, size_t size,
CryptoHashType hash_type)
: digest_type_(hash_type), digest_size_(size) {
- digest_.resize(size);
- memcpy(digest_.data(), hash, size);
+ digest_ = core::PacketManager<>::getInstance().getMemBuf();
+ digest_->append(size);
+ memcpy(digest_->writableData(), hash, size);
}
-CryptoHash::CryptoHash(const vector<uint8_t> &hash, CryptoHashType hash_type)
+CryptoHash::CryptoHash(const std::vector<uint8_t> &hash,
+ CryptoHashType hash_type)
: CryptoHash(hash.data(), hash.size(), hash_type) {}
CryptoHash &CryptoHash::operator=(const CryptoHash &other) {
- digest_type_ = other.digest_type_;
- digest_ = other.digest_;
- digest_size_ = other.digest_size_;
+ if (this != &other) {
+ digest_type_ = other.digest_type_;
+ digest_ = other.digest_;
+ digest_size_ = other.digest_size_;
+ }
return *this;
}
bool CryptoHash::operator==(const CryptoHash &other) const {
- return (digest_type_ == other.digest_type_ && digest_ == other.digest_);
+ return (digest_type_ == other.digest_type_ && *digest_ == *other.digest_);
}
void CryptoHash::computeDigest(const uint8_t *buffer, size_t len) {
- CryptoHashEVP hash_evp = CryptoHash::getEVP(digest_type_);
-
- if (hash_evp == nullptr) {
+ const EVP_MD *hash_md = CryptoHash::getMD(digest_type_);
+ if (hash_md == nullptr) {
throw errors::RuntimeException("Unknown hash type");
}
- EVP_Digest(buffer, len, digest_.data(), (unsigned int *)&digest_size_,
- (*hash_evp)(), nullptr);
+ if (EVP_Digest(buffer, len, digest_->writableData(),
+ reinterpret_cast<unsigned int *>(&digest_size_), hash_md,
+ nullptr) != 1) {
+ throw errors::RuntimeException("Digest computation failed.");
+ };
}
-void CryptoHash::computeDigest(const vector<uint8_t> &buffer) {
+void CryptoHash::computeDigest(const std::vector<uint8_t> &buffer) {
computeDigest(buffer.data(), buffer.size());
}
void CryptoHash::computeDigest(const utils::MemBuf *buffer) {
- CryptoHashEVP hash_evp = CryptoHash::getEVP(digest_type_);
-
- if (hash_evp == nullptr) {
- throw errors::RuntimeException("Unknown hash type");
+ if (buffer->isChained()) {
+ throw errors::RuntimeException(
+ "Digest of chained membuf is not supported.");
}
- EVP_MD_CTX *mcdtx = EVP_MD_CTX_new();
- const utils::MemBuf *p = buffer;
-
- if (EVP_DigestInit_ex(mcdtx, (*hash_evp)(), nullptr) == 0) {
- throw errors::RuntimeException("Digest initialization failed");
- }
-
- do {
- if (EVP_DigestUpdate(mcdtx, p->data(), p->length()) != 1) {
- throw errors::RuntimeException("Digest update failed");
- }
-
- p = p->next();
- } while (p != buffer);
-
- if (EVP_DigestFinal_ex(mcdtx, digest_.data(),
- (unsigned int *)&digest_size_) != 1) {
- throw errors::RuntimeException("Digest computation failed");
- }
-
- EVP_MD_CTX_free(mcdtx);
+ computeDigest(buffer->data(), buffer->length());
}
-vector<uint8_t> CryptoHash::getDigest() const { return digest_; }
+const utils::MemBuf::Ptr &CryptoHash::getDigest() const { return digest_; }
-string CryptoHash::getStringDigest() const {
- stringstream string_digest;
+std::string CryptoHash::getStringDigest() const {
+ std::stringstream string_digest;
- string_digest << hex << setfill('0');
+ string_digest << std::hex << std::setfill('0');
- for (auto byte : digest_) {
- string_digest << hex << setw(2) << static_cast<int>(byte);
+ for (size_t i = 0; i < digest_size_; ++i) {
+ string_digest << std::hex << std::setw(2)
+ << static_cast<int>(digest_->data()[i]);
}
return string_digest.str();
@@ -121,80 +108,65 @@ CryptoHashType CryptoHash::getType() const { return digest_type_; }
size_t CryptoHash::getSize() const { return digest_size_; }
void CryptoHash::setType(CryptoHashType hash_type) {
- reset();
digest_type_ = hash_type;
digest_size_ = CryptoHash::getSize(hash_type);
- digest_.resize(digest_size_);
+ DCHECK(digest_size_ <= digest_->tailroom());
+ digest_->setLength(digest_size_);
}
void CryptoHash::display() {
switch (digest_type_) {
case CryptoHashType::SHA256:
- cout << "SHA256";
+ LOG(INFO) << "SHA256: " << getStringDigest();
break;
case CryptoHashType::SHA512:
- cout << "SHA512";
+ LOG(INFO) << "SHA512: " << getStringDigest();
break;
case CryptoHashType::BLAKE2S256:
- cout << "BLAKE2s256";
+ LOG(INFO) << "BLAKE2s256: " << getStringDigest();
break;
case CryptoHashType::BLAKE2B512:
- cout << "BLAKE2b512";
+ LOG(INFO) << "BLAKE2b512: " << getStringDigest();
break;
default:
- cout << "UNKNOWN";
+ LOG(INFO) << "UNKNOWN: " << getStringDigest();
break;
}
-
- cout << ": " << getStringDigest() << endl;
}
void CryptoHash::reset() {
digest_type_ = CryptoHashType::UNKNOWN;
- digest_.clear();
digest_size_ = 0;
+ digest_->setLength(0);
}
-CryptoHashEVP CryptoHash::getEVP(CryptoHashType hash_type) {
+const EVP_MD *CryptoHash::getMD(CryptoHashType hash_type) {
switch (hash_type) {
case CryptoHashType::SHA256:
- return &EVP_sha256;
- break;
+ return EVP_sha256();
case CryptoHashType::SHA512:
- return &EVP_sha512;
- break;
+ return EVP_sha512();
case CryptoHashType::BLAKE2S256:
- return &EVP_blake2s256;
- break;
+ return EVP_blake2s256();
case CryptoHashType::BLAKE2B512:
- return &EVP_blake2b512;
- break;
+ return EVP_blake2b512();
default:
return nullptr;
- break;
}
}
size_t CryptoHash::getSize(CryptoHashType hash_type) {
- CryptoHashEVP hash_evp = CryptoHash::getEVP(hash_type);
-
- if (hash_evp == nullptr) {
- return 0;
- }
-
- return EVP_MD_size((*hash_evp)());
+ const EVP_MD *hash_md = CryptoHash::getMD(hash_type);
+ return hash_md == nullptr ? 0 : EVP_MD_size(hash_md);
}
bool CryptoHash::compareDigest(const uint8_t *digest1, const uint8_t *digest2,
CryptoHashType hash_type) {
- CryptoHashEVP hash_evp = CryptoHash::getEVP(hash_type);
-
- if (hash_evp == nullptr) {
- return false;
- }
-
- return !static_cast<bool>(
- memcmp(digest1, digest2, CryptoHash::getSize(hash_type)));
+ const EVP_MD *hash_md = CryptoHash::getMD(hash_type);
+ return hash_md == nullptr
+ ? false
+ : !static_cast<bool>(
+ memcmp(digest1, digest2, CryptoHash::getSize(hash_type)));
}
} // namespace auth