summaryrefslogtreecommitdiff
path: root/src/mongo/crypto
diff options
context:
space:
mode:
authorShreyas Kalyan <shreyas.kalyan@10gen.com>2019-03-05 13:02:35 -0500
committerShreyas Kalyan <shreyas.kalyan@10gen.com>2019-03-21 12:02:43 -0400
commit0adb2f088e4326c35d82afa42e9e5d5969530c5a (patch)
tree8ea58ec95409ae9206a1975f56acf25ba0f8dcfa /src/mongo/crypto
parent9dc7556e1f02e09956ec60118fa7a1bfa3edad98 (diff)
downloadmongo-0adb2f088e4326c35d82afa42e9e5d5969530c5a.tar.gz
SERVER-39893 Implement AEAD_AES_256_CBC_HMAC_SHA_512
Diffstat (limited to 'src/mongo/crypto')
-rw-r--r--src/mongo/crypto/SConscript4
-rw-r--r--src/mongo/crypto/sha1_block.h5
-rw-r--r--src/mongo/crypto/sha256_block.h3
-rw-r--r--src/mongo/crypto/sha512_block.h60
-rw-r--r--src/mongo/crypto/sha512_block.idl46
-rw-r--r--src/mongo/crypto/sha512_block_test.cpp216
-rw-r--r--src/mongo/crypto/sha_block.h21
-rw-r--r--src/mongo/crypto/sha_block_apple.cpp43
-rw-r--r--src/mongo/crypto/sha_block_openssl.cpp59
-rw-r--r--src/mongo/crypto/sha_block_tom.cpp45
-rw-r--r--src/mongo/crypto/sha_block_windows.cpp56
11 files changed, 510 insertions, 48 deletions
diff --git a/src/mongo/crypto/SConscript b/src/mongo/crypto/SConscript
index 8f6e35d7b60..97f73e3356a 100644
--- a/src/mongo/crypto/SConscript
+++ b/src/mongo/crypto/SConscript
@@ -65,6 +65,10 @@ env.CppUnitTest('sha256_block_test',
['sha256_block_test.cpp'],
LIBDEPS=['sha_block_${MONGO_CRYPTO}'])
+env.CppUnitTest('sha512_block_test',
+ ['sha512_block_test.cpp'],
+ LIBDEPS=['sha_block_${MONGO_CRYPTO}'])
+
env.CppUnitTest('mechanism_scram_test',
['mechanism_scram_test.cpp'],
LIBDEPS_PRIVATE=[
diff --git a/src/mongo/crypto/sha1_block.h b/src/mongo/crypto/sha1_block.h
index 79fa3ca8af0..fcebf441869 100644
--- a/src/mongo/crypto/sha1_block.h
+++ b/src/mongo/crypto/sha1_block.h
@@ -36,7 +36,7 @@
namespace mongo {
/**
- * A Traits type for adapting SHABlock to sha256 hashes.
+ * A Traits type for adapting SHABlock to sha1 hashes.
*/
struct SHA1BlockTraits {
using HashType = MakeArrayType<std::uint8_t, 20, SHA1BlockTraits>;
@@ -47,8 +47,7 @@ struct SHA1BlockTraits {
static void computeHmac(const uint8_t* key,
size_t keyLen,
- const uint8_t* input,
- size_t inputLen,
+ std::initializer_list<ConstDataRange> input,
HashType* const output);
};
diff --git a/src/mongo/crypto/sha256_block.h b/src/mongo/crypto/sha256_block.h
index ce8793c8bd8..7514e33bd82 100644
--- a/src/mongo/crypto/sha256_block.h
+++ b/src/mongo/crypto/sha256_block.h
@@ -47,8 +47,7 @@ struct SHA256BlockTraits {
static void computeHmac(const uint8_t* key,
size_t keyLen,
- const uint8_t* input,
- size_t inputLen,
+ std::initializer_list<ConstDataRange> input,
HashType* const output);
};
diff --git a/src/mongo/crypto/sha512_block.h b/src/mongo/crypto/sha512_block.h
new file mode 100644
index 00000000000..af8350d19e3
--- /dev/null
+++ b/src/mongo/crypto/sha512_block.h
@@ -0,0 +1,60 @@
+/**
+ * Copyright (C) 2019-present MongoDB, Inc.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the Server Side Public License, version 1,
+ * as published by MongoDB, Inc.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * Server Side Public License for more details.
+ *
+ * You should have received a copy of the Server Side Public License
+ * along with this program. If not, see
+ * <http://www.mongodb.com/licensing/server-side-public-license>.
+ *
+ * As a special exception, the copyright holders give permission to link the
+ * code of portions of this program with the OpenSSL library under certain
+ * conditions as described in each individual source file and distribute
+ * linked combinations including the program with the OpenSSL library. You
+ * must comply with the Server Side Public License in all respects for
+ * all of the code used other than as permitted herein. If you modify file(s)
+ * with this exception, you may extend this exception to your version of the
+ * file(s), but you are not obligated to do so. If you do not wish to do so,
+ * delete this exception statement from your version. If you delete this
+ * exception statement from all source files in the program, then also delete
+ * it in the license file.
+ */
+
+#pragma once
+
+#include "mongo/crypto/sha_block.h"
+
+#include "mongo/util/make_array_type.h"
+
+namespace mongo {
+
+/**
+ * A Traits type for adapting SHABlock to sha512 hashes.
+ */
+struct SHA512BlockTraits {
+ using HashType = MakeArrayType<std::uint8_t, 64, SHA512BlockTraits>;
+
+ static constexpr StringData name = "SHA512Block"_sd;
+
+ static HashType computeHash(std::initializer_list<ConstDataRange> input);
+
+ static void computeHmac(const uint8_t* key,
+ size_t keyLen,
+ std::initializer_list<ConstDataRange> input,
+ HashType* const output);
+
+ static HashType computeHmac(const uint8_t* key,
+ size_t keyLen,
+ std::initializer_list<ConstDataRange> input);
+};
+
+using SHA512Block = SHABlock<SHA512BlockTraits>;
+
+} // namespace mongo
diff --git a/src/mongo/crypto/sha512_block.idl b/src/mongo/crypto/sha512_block.idl
new file mode 100644
index 00000000000..46b35d13613
--- /dev/null
+++ b/src/mongo/crypto/sha512_block.idl
@@ -0,0 +1,46 @@
+# Copyright (C) 2019-present MongoDB, Inc.
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the Server Side Public License, version 1,
+# as published by MongoDB, Inc.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# Server Side Public License for more details.
+#
+# You should have received a copy of the Server Side Public License
+# along with this program. If not, see
+# <http://www.mongodb.com/licensing/server-side-public-license>.
+#
+# As a special exception, the copyright holders give permission to link the
+# code of portions of this program with the OpenSSL library under certain
+# conditions as described in each individual source file and distribute
+# linked combinations including the program with the OpenSSL library. You
+# must comply with the Server Side Public License in all respects for
+# all of the code used other than as permitted herein. If you modify file(s)
+# with this exception, you may extend this exception to your version of the
+# file(s), but you are not obligated to do so. If you do not wish to do so,
+# delete this exception statement from your version. If you delete this
+# exception statement from all source files in the program, then also delete
+# it in the license file.
+#
+
+# SHA512Block type
+
+global:
+ cpp_namespace: "mongo"
+ cpp_includes:
+ - "mongo/crypto/sha512_block.h"
+
+imports:
+ - "mongo/idl/basic_types.idl"
+
+types:
+ sha512Block:
+ bson_serialization_type: bindata
+ bindata_subtype: generic
+ description: "A fixed-size byte array that holds the result of a SHA512 computation"
+ cpp_type: mongo::SHA512Block
+ serializer: "mongo::SHA512Block::toCDR"
+ deserializer: "mongo::SHA512Block::fromBinData"
diff --git a/src/mongo/crypto/sha512_block_test.cpp b/src/mongo/crypto/sha512_block_test.cpp
new file mode 100644
index 00000000000..79ef4e3ffcc
--- /dev/null
+++ b/src/mongo/crypto/sha512_block_test.cpp
@@ -0,0 +1,216 @@
+/**
+ * Copyright (C) 2019-present MongoDB, Inc.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the Server Side Public License, version 1,
+ * as published by MongoDB, Inc.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * Server Side Public License for more details.
+ *
+ * You should have received a copy of the Server Side Public License
+ * along with this program. If not, see
+ * <http://www.mongodb.com/licensing/server-side-public-license>.
+ *
+ * As a special exception, the copyright holders give permission to link the
+ * code of portions of this program with the OpenSSL library under certain
+ * conditions as described in each individual source file and distribute
+ * linked combinations including the program with the OpenSSL library. You
+ * must comply with the Server Side Public License in all respects for
+ * all of the code used other than as permitted herein. If you modify file(s)
+ * with this exception, you may extend this exception to your version of the
+ * file(s), but you are not obligated to do so. If you do not wish to do so,
+ * delete this exception statement from your version. If you delete this
+ * exception statement from all source files in the program, then also delete
+ * it in the license file.
+ */
+
+#include "mongo/platform/basic.h"
+
+#include "mongo/bson/bsonmisc.h"
+#include "mongo/bson/bsonobjbuilder.h"
+#include "mongo/crypto/sha512_block.h"
+#include "mongo/unittest/unittest.h"
+
+namespace mongo {
+namespace {
+
+ConstDataRange makeTestItem(StringData sd) {
+ return ConstDataRange(sd.rawData(), sd.size());
+}
+
+// SHA-512 test vectors from http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/SHA_All.pdf
+const struct {
+ std::initializer_list<ConstDataRange> msg;
+ SHA512Block hash;
+} sha512Tests[] = {
+ {{makeTestItem("abc")},
+ SHA512Block::HashType{0xdd, 0xaf, 0x35, 0xa1, 0x93, 0x61, 0x7a, 0xba, 0xcc, 0x41, 0x73,
+ 0x49, 0xae, 0x20, 0x41, 0x31, 0x12, 0xe6, 0xfa, 0x4e, 0x89, 0xa9,
+ 0x7e, 0xa2, 0x0a, 0x9e, 0xee, 0xe6, 0x4b, 0x55, 0xd3, 0x9a, 0x21,
+ 0x92, 0x99, 0x2a, 0x27, 0x4f, 0xc1, 0xa8, 0x36, 0xba, 0x3c, 0x23,
+ 0xa3, 0xfe, 0xeb, 0xbd, 0x45, 0x4d, 0x44, 0x23, 0x64, 0x3c, 0xe8,
+ 0x0e, 0x2a, 0x9a, 0xc9, 0x4f, 0xa5, 0x4c, 0xa4, 0x9f}},
+
+ {{makeTestItem("abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjkl"
+ "mnopqklmnopqrlmnopqrsmnopqrstnopqrstu")},
+ SHA512Block::HashType{0x8e, 0x95, 0x9b, 0x75, 0xda, 0xe3, 0x13, 0xda, 0x8c, 0xf4, 0xf7,
+ 0x28, 0x14, 0xfc, 0x14, 0x3f, 0x8f, 0x77, 0x79, 0xc6, 0xeb, 0x9f,
+ 0x7f, 0xa1, 0x72, 0x99, 0xae, 0xad, 0xb6, 0x88, 0x90, 0x18, 0x50,
+ 0x1d, 0x28, 0x9e, 0x49, 0x00, 0xf7, 0xe4, 0x33, 0x1b, 0x99, 0xde,
+ 0xc4, 0xb5, 0x43, 0x3a, 0xc7, 0xd3, 0x29, 0xee, 0xb6, 0xdd, 0x26,
+ 0x54, 0x5e, 0x96, 0xe5, 0x5b, 0x87, 0x4b, 0xe9, 0x09}},
+
+ {{makeTestItem("abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopj"),
+ makeTestItem("klmnopqklmnopqrlmnopqrsmnopqrstnopqrstu")},
+ SHA512Block::HashType{0x8e, 0x95, 0x9b, 0x75, 0xda, 0xe3, 0x13, 0xda, 0x8c, 0xf4, 0xf7,
+ 0x28, 0x14, 0xfc, 0x14, 0x3f, 0x8f, 0x77, 0x79, 0xc6, 0xeb, 0x9f,
+ 0x7f, 0xa1, 0x72, 0x99, 0xae, 0xad, 0xb6, 0x88, 0x90, 0x18, 0x50,
+ 0x1d, 0x28, 0x9e, 0x49, 0x00, 0xf7, 0xe4, 0x33, 0x1b, 0x99, 0xde,
+ 0xc4, 0xb5, 0x43, 0x3a, 0xc7, 0xd3, 0x29, 0xee, 0xb6, 0xdd, 0x26,
+ 0x54, 0x5e, 0x96, 0xe5, 0x5b, 0x87, 0x4b, 0xe9, 0x09}},
+
+};
+
+TEST(CryptoVectors, SHA512) {
+ size_t numTests = sizeof(sha512Tests) / sizeof(sha512Tests[0]);
+ for (size_t i = 0; i < numTests; i++) {
+ SHA512Block result = SHA512Block::computeHash(sha512Tests[i].msg);
+ ASSERT(sha512Tests[i].hash == result) << "Failed SHA512 iteration " << i;
+ }
+}
+
+const int maxKeySize = 80;
+const int maxDataSize = 54;
+// HMAC-SHA-512 test vectors from https://tools.ietf.org/html/rfc4231#section-4.2
+const struct {
+ unsigned char key[maxKeySize];
+ int keyLen;
+ unsigned char data[maxDataSize];
+ int dataLen;
+ SHA512Block hash;
+} hmacSha512Tests[] = {
+ // RFC test case 1
+ {{0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
+ 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0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 0xc5, 0x78, 0x80, 0x63, 0xa5, 0xf1, 0x97, 0x41, 0x12, 0x0c, 0x4f,
+ 0x2d, 0xe2, 0xad, 0xeb, 0xeb, 0x10, 0xa2, 0x98, 0xdd}},
+};
+
+TEST(CryptoVectors, HMACSHA512) {
+ size_t numTests = sizeof(hmacSha512Tests) / sizeof(hmacSha512Tests[0]);
+ for (size_t i = 0; i < numTests; i++) {
+ SHA512Block result = SHA512Block::computeHmac(hmacSha512Tests[i].key,
+ hmacSha512Tests[i].keyLen,
+ hmacSha512Tests[i].data,
+ hmacSha512Tests[i].dataLen);
+ ASSERT(hmacSha512Tests[i].hash == result) << "Failed HMAC-SHA512 iteration " << i;
+ }
+}
+
+TEST(SHA512Block, BinDataRoundTrip) {
+ SHA512Block::HashType rawHash;
+ rawHash.fill(0);
+ for (size_t i = 0; i < rawHash.size(); i++) {
+ rawHash[i] = i;
+ }
+
+ SHA512Block testHash(rawHash);
+
+ BSONObjBuilder builder;
+ testHash.appendAsBinData(builder, "hash");
+ auto newObj = builder.done();
+
+ auto hashElem = newObj["hash"];
+ ASSERT_EQ(BinData, hashElem.type());
+ ASSERT_EQ(BinDataGeneral, hashElem.binDataType());
+
+ int binLen = 0;
+ auto rawBinData = hashElem.binData(binLen);
+ ASSERT_EQ(SHA512Block::kHashLength, static_cast<size_t>(binLen));
+
+ auto newHashStatus =
+ SHA512Block::fromBinData(BSONBinData(rawBinData, binLen, hashElem.binDataType()));
+ ASSERT_OK(newHashStatus.getStatus());
+ ASSERT_TRUE(testHash == newHashStatus.getValue());
+}
+
+TEST(SHA512Block, CanOnlyConstructFromBinGeneral) {
+ std::string dummy(SHA512Block::kHashLength, 'x');
+
+ auto newHashStatus =
+ SHA512Block::fromBinData(BSONBinData(dummy.c_str(), dummy.size(), newUUID));
+ ASSERT_EQ(ErrorCodes::UnsupportedFormat, newHashStatus.getStatus());
+}
+
+TEST(SHA512Block, FromBinDataShouldRejectWrongSize) {
+ std::string dummy(SHA512Block::kHashLength - 1, 'x');
+
+ auto newHashStatus =
+ SHA512Block::fromBinData(BSONBinData(dummy.c_str(), dummy.size(), BinDataGeneral));
+ ASSERT_EQ(ErrorCodes::UnsupportedFormat, newHashStatus.getStatus());
+
+ dummy = std::string(SHA512Block::kHashLength - 1, 'x');
+
+ newHashStatus =
+ SHA512Block::fromBinData(BSONBinData(dummy.c_str(), dummy.size(), BinDataGeneral));
+ ASSERT_EQ(ErrorCodes::UnsupportedFormat, newHashStatus.getStatus());
+}
+
+TEST(SHA512Block, FromBufferShouldRejectWrongLength) {
+ std::string dummy(SHA512Block::kHashLength - 1, 'x');
+
+ auto newHashStatus =
+ SHA512Block::fromBuffer(reinterpret_cast<const uint8_t*>(dummy.c_str()), dummy.size());
+ ASSERT_EQ(ErrorCodes::InvalidLength, newHashStatus.getStatus());
+
+ dummy = std::string(SHA512Block::kHashLength + 1, 'x');
+
+ newHashStatus =
+ SHA512Block::fromBuffer(reinterpret_cast<const uint8_t*>(dummy.c_str()), dummy.size());
+ ASSERT_EQ(ErrorCodes::InvalidLength, newHashStatus.getStatus());
+}
+
+} // namespace
+} // namespace mongo
diff --git a/src/mongo/crypto/sha_block.h b/src/mongo/crypto/sha_block.h
index afd0d3e75e5..e9a0ce338b9 100644
--- a/src/mongo/crypto/sha_block.h
+++ b/src/mongo/crypto/sha_block.h
@@ -99,7 +99,8 @@ public:
const uint8_t* input,
size_t inputLen) {
SHABlock output;
- SHABlock::computeHmac(key, keyLen, input, inputLen, &output);
+ SHABlock::computeHmac(
+ key, keyLen, {ConstDataRange(reinterpret_cast<const char*>(input), inputLen)}, &output);
return output;
}
@@ -112,7 +113,23 @@ public:
const uint8_t* input,
size_t inputLen,
SHABlock* const output) {
- return Traits::computeHmac(key, keyLen, input, inputLen, &(output->_hash));
+ SHABlock::computeHmac(
+ key, keyLen, {ConstDataRange(reinterpret_cast<const char*>(input), inputLen)}, output);
+ }
+
+ static SHABlock computeHmac(const uint8_t* key,
+ size_t keyLen,
+ std::initializer_list<ConstDataRange> input) {
+ SHABlock output;
+ SHABlock::computeHmac(key, keyLen, input, &output);
+ return output;
+ }
+
+ static void computeHmac(const uint8_t* key,
+ size_t keyLen,
+ std::initializer_list<ConstDataRange> input,
+ SHABlock* const output) {
+ Traits::computeHmac(key, keyLen, input, &(output->_hash));
}
const uint8_t* data() const& {
diff --git a/src/mongo/crypto/sha_block_apple.cpp b/src/mongo/crypto/sha_block_apple.cpp
index 3679c3fd339..daa63eb2af9 100644
--- a/src/mongo/crypto/sha_block_apple.cpp
+++ b/src/mongo/crypto/sha_block_apple.cpp
@@ -34,6 +34,7 @@
#include "mongo/crypto/sha1_block.h"
#include "mongo/crypto/sha256_block.h"
+#include "mongo/crypto/sha512_block.h"
namespace mongo {
using CDRinit = std::initializer_list<ConstDataRange>;
@@ -66,29 +67,59 @@ SHA256BlockTraits::HashType SHA256BlockTraits::computeHash(CDRinit input) {
return ret;
}
+SHA512BlockTraits::HashType SHA512BlockTraits::computeHash(CDRinit input) {
+ CC_SHA512_CTX ctx;
+ CC_SHA512_Init(&ctx);
+ for (const auto& range : input) {
+ CC_SHA512_Update(&ctx, range.data(), range.length());
+ }
+
+ SHA512BlockTraits::HashType ret;
+ static_assert(sizeof(ret) == CC_SHA512_DIGEST_LENGTH,
+ "SHA512 HashType size doesn't match expected digest output size");
+ CC_SHA512_Final(ret.data(), &ctx);
+ return ret;
+}
+
void SHA1BlockTraits::computeHmac(const uint8_t* key,
size_t keyLen,
- const uint8_t* input,
- size_t inputLen,
+ CDRinit input,
SHA1BlockTraits::HashType* const output) {
static_assert(sizeof(*output) == CC_SHA1_DIGEST_LENGTH,
"SHA1 HashType size doesn't match expected hmac output size");
CCHmacContext ctx;
CCHmacInit(&ctx, kCCHmacAlgSHA1, key, keyLen);
- CCHmacUpdate(&ctx, input, inputLen);
+ for (const auto& range : input) {
+ CCHmacUpdate(&ctx, range.data(), range.length());
+ }
CCHmacFinal(&ctx, output);
}
void SHA256BlockTraits::computeHmac(const uint8_t* key,
size_t keyLen,
- const uint8_t* input,
- size_t inputLen,
+ CDRinit input,
SHA256BlockTraits::HashType* const output) {
static_assert(sizeof(*output) == CC_SHA256_DIGEST_LENGTH,
"SHA256 HashType size doesn't match expected hmac output size");
CCHmacContext ctx;
CCHmacInit(&ctx, kCCHmacAlgSHA256, key, keyLen);
- CCHmacUpdate(&ctx, input, inputLen);
+ for (const auto& range : input) {
+ CCHmacUpdate(&ctx, range.data(), range.length());
+ }
+ CCHmacFinal(&ctx, output);
+}
+
+void SHA512BlockTraits::computeHmac(const uint8_t* key,
+ size_t keyLen,
+ CDRinit input,
+ SHA512BlockTraits::HashType* const output) {
+ static_assert(sizeof(*output) == CC_SHA512_DIGEST_LENGTH,
+ "SHA512 HashType size doesn't match expected hmac output size");
+ CCHmacContext ctx;
+ CCHmacInit(&ctx, kCCHmacAlgSHA512, key, keyLen);
+ for (const auto& range : input) {
+ CCHmacUpdate(&ctx, range.data(), range.length());
+ }
CCHmacFinal(&ctx, output);
}
diff --git a/src/mongo/crypto/sha_block_openssl.cpp b/src/mongo/crypto/sha_block_openssl.cpp
index 71a73c1266d..9937afba477 100644
--- a/src/mongo/crypto/sha_block_openssl.cpp
+++ b/src/mongo/crypto/sha_block_openssl.cpp
@@ -31,6 +31,7 @@
#include "mongo/crypto/sha1_block.h"
#include "mongo/crypto/sha256_block.h"
+#include "mongo/crypto/sha512_block.h"
#include "mongo/config.h"
#include "mongo/stdx/memory.h"
@@ -63,6 +64,21 @@ void EVP_MD_CTX_free(EVP_MD_CTX* ctx) {
EVP_MD_CTX_cleanup(ctx);
OPENSSL_free(ctx);
}
+
+HMAC_CTX* HMAC_CTX_new() {
+ void* ctx = OPENSSL_malloc(sizeof(HMAC_CTX));
+
+ if (ctx != NULL) {
+ memset(ctx, 0, sizeof(HMAC_CTX));
+ }
+ return static_cast<HMAC_CTX*>(ctx);
+}
+
+void HMAC_CTX_free(HMAC_CTX* ctx) {
+ HMAC_CTX_cleanup(ctx);
+ OPENSSL_free(ctx);
+}
+
} // namespace
#endif
@@ -91,17 +107,24 @@ HashType computeHashImpl(const EVP_MD* md, std::initializer_list<ConstDataRange>
return output;
}
-/*
- * Computes a HMAC SHA'd keyed hash of 'input' using the key 'key', writes output into 'output'.
- */
template <typename HashType>
void computeHmacImpl(const EVP_MD* md,
const uint8_t* key,
size_t keyLen,
- const uint8_t* input,
- size_t inputLen,
+ std::initializer_list<ConstDataRange> input,
HashType* const output) {
- fassert(40380, HMAC(md, key, keyLen, input, inputLen, output->data(), NULL) != NULL);
+ std::unique_ptr<HMAC_CTX, decltype(&HMAC_CTX_free)> digestCtx(HMAC_CTX_new(), HMAC_CTX_free);
+
+ fassert(40380,
+ HMAC_Init_ex(digestCtx.get(), key, keyLen, md, NULL) == 1 &&
+ std::all_of(begin(input),
+ end(input),
+ [&](const auto& i) {
+ return HMAC_Update(digestCtx.get(),
+ reinterpret_cast<const unsigned char*>(i.data()),
+ i.length()) == 1;
+ }) &&
+ HMAC_Final(digestCtx.get(), output->data(), NULL) == 1);
}
} // namespace
@@ -116,22 +139,30 @@ SHA256BlockTraits::HashType SHA256BlockTraits::computeHash(
return computeHashImpl<SHA256BlockTraits::HashType>(EVP_sha256(), input);
}
+SHA512BlockTraits::HashType SHA512BlockTraits::computeHash(
+ std::initializer_list<ConstDataRange> input) {
+ return computeHashImpl<SHA512BlockTraits::HashType>(EVP_sha512(), input);
+}
+
void SHA1BlockTraits::computeHmac(const uint8_t* key,
size_t keyLen,
- const uint8_t* input,
- size_t inputLen,
+ std::initializer_list<ConstDataRange> input,
SHA1BlockTraits::HashType* const output) {
- return computeHmacImpl<SHA1BlockTraits::HashType>(
- EVP_sha1(), key, keyLen, input, inputLen, output);
+ return computeHmacImpl<SHA1BlockTraits::HashType>(EVP_sha1(), key, keyLen, input, output);
}
void SHA256BlockTraits::computeHmac(const uint8_t* key,
size_t keyLen,
- const uint8_t* input,
- size_t inputLen,
+ std::initializer_list<ConstDataRange> input,
SHA256BlockTraits::HashType* const output) {
- return computeHmacImpl<SHA256BlockTraits::HashType>(
- EVP_sha256(), key, keyLen, input, inputLen, output);
+ return computeHmacImpl<SHA256BlockTraits::HashType>(EVP_sha256(), key, keyLen, input, output);
+}
+
+void SHA512BlockTraits::computeHmac(const uint8_t* key,
+ size_t keyLen,
+ std::initializer_list<ConstDataRange> input,
+ SHA512BlockTraits::HashType* const output) {
+ return computeHmacImpl<SHA512BlockTraits::HashType>(EVP_sha512(), key, keyLen, input, output);
}
} // namespace mongo
diff --git a/src/mongo/crypto/sha_block_tom.cpp b/src/mongo/crypto/sha_block_tom.cpp
index 8a53b91aec9..8f477ad8fad 100644
--- a/src/mongo/crypto/sha_block_tom.cpp
+++ b/src/mongo/crypto/sha_block_tom.cpp
@@ -31,6 +31,7 @@
#include "mongo/crypto/sha1_block.h"
#include "mongo/crypto/sha256_block.h"
+#include "mongo/crypto/sha512_block.h"
#include "mongo/config.h"
#include "mongo/util/assert_util.h"
@@ -76,10 +77,11 @@ template <typename HashType>
void computeHmacImpl(const ltc_hash_descriptor* desc,
const uint8_t* key,
size_t keyLen,
- const uint8_t* input,
- size_t inputLen,
+ std::initializer_list<ConstDataRange> input,
HashType* const output) {
- invariant(key && input);
+ invariant(key);
+ invariant(
+ std::all_of(begin(input), end(input), [&](const auto& i) { return i.data() != nullptr; }));
static const struct Magic {
Magic(const ltc_hash_descriptor* desc) {
@@ -90,10 +92,20 @@ void computeHmacImpl(const ltc_hash_descriptor* desc,
int hashId;
} magic(desc);
+ Hmac_state hmacState;
unsigned long shaHashLen = sizeof(HashType);
+
fassert(40382,
- hmac_memory(magic.hashId, key, keyLen, input, inputLen, output->data(), &shaHashLen) ==
- CRYPT_OK);
+ hmac_init(&hmacState, magic.hashId, key, keyLen) == CRYPT_OK &&
+ std::all_of(begin(input),
+ end(input),
+ [&](const auto& i) {
+ return hmac_process(
+ &hmacState,
+ reinterpret_cast<const unsigned char*>(i.data()),
+ i.length()) == CRYPT_OK;
+ }) &&
+ hmac_done(&hmacState, output->data(), &shaHashLen) == CRYPT_OK);
}
} // namespace
@@ -109,20 +121,31 @@ SHA256BlockTraits::HashType SHA256BlockTraits::computeHash(
input);
}
+SHA512BlockTraits::HashType SHA512BlockTraits::computeHash(
+ std::initializer_list<ConstDataRange> input) {
+ return computeHashImpl<SHA512BlockTraits::HashType, sha512_init, sha512_process, sha512_done>(
+ input);
+}
+
void SHA1BlockTraits::computeHmac(const uint8_t* key,
size_t keyLen,
- const uint8_t* input,
- size_t inputLen,
+ std::initializer_list<ConstDataRange> input,
HashType* const output) {
- return computeHmacImpl<HashType>(&sha1_desc, key, keyLen, input, inputLen, output);
+ return computeHmacImpl<HashType>(&sha1_desc, key, keyLen, input, output);
}
void SHA256BlockTraits::computeHmac(const uint8_t* key,
size_t keyLen,
- const uint8_t* input,
- size_t inputLen,
+ std::initializer_list<ConstDataRange> input,
+ HashType* const output) {
+ return computeHmacImpl<HashType>(&sha256_desc, key, keyLen, input, output);
+}
+
+void SHA512BlockTraits::computeHmac(const uint8_t* key,
+ size_t keyLen,
+ std::initializer_list<ConstDataRange> input,
HashType* const output) {
- return computeHmacImpl<HashType>(&sha256_desc, key, keyLen, input, inputLen, output);
+ return computeHmacImpl<HashType>(&sha512_desc, key, keyLen, input, output);
}
} // namespace mongo
diff --git a/src/mongo/crypto/sha_block_windows.cpp b/src/mongo/crypto/sha_block_windows.cpp
index f4bdcb6857b..78ab4281c55 100644
--- a/src/mongo/crypto/sha_block_windows.cpp
+++ b/src/mongo/crypto/sha_block_windows.cpp
@@ -33,6 +33,7 @@
#include "mongo/crypto/sha1_block.h"
#include "mongo/crypto/sha256_block.h"
+#include "mongo/crypto/sha512_block.h"
#include "mongo/config.h"
#include "mongo/util/assert_util.h"
@@ -49,18 +50,26 @@ public:
BCryptHashLoader() {
loadAlgo(&_algoSHA1, BCRYPT_SHA1_ALGORITHM, false);
loadAlgo(&_algoSHA256, BCRYPT_SHA256_ALGORITHM, false);
+ loadAlgo(&_algoSHA512, BCRYPT_SHA512_ALGORITHM, false);
loadAlgo(&_algoSHA1Hmac, BCRYPT_SHA1_ALGORITHM, true);
loadAlgo(&_algoSHA256Hmac, BCRYPT_SHA256_ALGORITHM, true);
+ loadAlgo(&_algoSHA512Hmac, BCRYPT_SHA512_ALGORITHM, true);
}
~BCryptHashLoader() {
+ invariant(BCryptCloseAlgorithmProvider(_algoSHA512, 0) == STATUS_SUCCESS);
invariant(BCryptCloseAlgorithmProvider(_algoSHA256, 0) == STATUS_SUCCESS);
invariant(BCryptCloseAlgorithmProvider(_algoSHA1, 0) == STATUS_SUCCESS);
+ invariant(BCryptCloseAlgorithmProvider(_algoSHA512Hmac, 0) == STATUS_SUCCESS);
invariant(BCryptCloseAlgorithmProvider(_algoSHA256Hmac, 0) == STATUS_SUCCESS);
invariant(BCryptCloseAlgorithmProvider(_algoSHA1Hmac, 0) == STATUS_SUCCESS);
}
+ BCRYPT_ALG_HANDLE getAlgoSHA512() {
+ return _algoSHA512;
+ }
+
BCRYPT_ALG_HANDLE getAlgoSHA256() {
return _algoSHA256;
}
@@ -69,6 +78,10 @@ public:
return _algoSHA1;
}
+ BCRYPT_ALG_HANDLE getAlgoSHA512Hmac() {
+ return _algoSHA512Hmac;
+ };
+
BCRYPT_ALG_HANDLE getAlgoSHA256Hmac() {
return _algoSHA256Hmac;
};
@@ -86,8 +99,10 @@ private:
}
private:
+ BCRYPT_ALG_HANDLE _algoSHA512;
BCRYPT_ALG_HANDLE _algoSHA256;
BCRYPT_ALG_HANDLE _algoSHA1;
+ BCRYPT_ALG_HANDLE _algoSHA512Hmac;
BCRYPT_ALG_HANDLE _algoSHA256Hmac;
BCRYPT_ALG_HANDLE _algoSHA1Hmac;
};
@@ -133,10 +148,11 @@ template <typename HashType>
void computeHmacImpl(BCRYPT_ALG_HANDLE algo,
const uint8_t* key,
size_t keyLen,
- const uint8_t* input,
- size_t inputLen,
+ std::initializer_list<ConstDataRange> input,
HashType* const output) {
- invariant(key && input);
+ invariant(key);
+ invariant(
+ std::all_of(begin(input), end(input), [&](const auto& i) { return i.data() != nullptr; }));
BCRYPT_HASH_HANDLE hHash;
@@ -144,7 +160,15 @@ void computeHmacImpl(BCRYPT_ALG_HANDLE algo,
BCryptCreateHash(algo, &hHash, NULL, 0, const_cast<PUCHAR>(key), keyLen, 0) ==
STATUS_SUCCESS &&
- BCryptHashData(hHash, const_cast<PUCHAR>(input), inputLen, 0) == STATUS_SUCCESS &&
+ std::all_of(begin(input),
+ end(input),
+ [&](const auto& i) {
+ return BCryptHashData(
+ hHash,
+ reinterpret_cast<PUCHAR>(const_cast<char*>(i.data())),
+ i.length(),
+ 0) == STATUS_SUCCESS;
+ }) &&
BCryptFinishHash(hHash, output->data(), output->size(), 0) == STATUS_SUCCESS &&
@@ -165,22 +189,34 @@ SHA256BlockTraits::HashType SHA256BlockTraits::computeHash(
std::move(input));
}
+SHA512BlockTraits::HashType SHA512BlockTraits::computeHash(
+ std::initializer_list<ConstDataRange> input) {
+ return computeHashImpl<SHA512BlockTraits::HashType>(getBCryptHashLoader().getAlgoSHA512(),
+ std::move(input));
+}
+
void SHA1BlockTraits::computeHmac(const uint8_t* key,
size_t keyLen,
- const uint8_t* input,
- size_t inputLen,
+ std::initializer_list<ConstDataRange> input,
HashType* const output) {
return computeHmacImpl<HashType>(
- getBCryptHashLoader().getAlgoSHA1Hmac(), key, keyLen, input, inputLen, output);
+ getBCryptHashLoader().getAlgoSHA1Hmac(), key, keyLen, input, output);
}
void SHA256BlockTraits::computeHmac(const uint8_t* key,
size_t keyLen,
- const uint8_t* input,
- size_t inputLen,
+ std::initializer_list<ConstDataRange> input,
+ HashType* const output) {
+ return computeHmacImpl<HashType>(
+ getBCryptHashLoader().getAlgoSHA256Hmac(), key, keyLen, input, output);
+}
+
+void SHA512BlockTraits::computeHmac(const uint8_t* key,
+ size_t keyLen,
+ std::initializer_list<ConstDataRange> input,
HashType* const output) {
return computeHmacImpl<HashType>(
- getBCryptHashLoader().getAlgoSHA256Hmac(), key, keyLen, input, inputLen, output);
+ getBCryptHashLoader().getAlgoSHA512Hmac(), key, keyLen, input, output);
}
} // namespace mongo