summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJason Carey <jcarey@argv.me>2017-07-18 16:13:32 -0400
committerJason Carey <jcarey@argv.me>2017-07-25 10:48:44 -0400
commitffe9014101f7bd53ed9674b770e9371324fcdf34 (patch)
tree7f1051ee8d382d41cc56cf61ce39949b50ea8133 /src
parent4e27af1e980a5f5479fc26a3d616a5b56245261a (diff)
downloadmongo-ffe9014101f7bd53ed9674b770e9371324fcdf34.tar.gz
SERVER-30218 Add SHA256 Support
The addition of a SHA256Block will give us the flexibility to handle new workloads that aren't appropriate for SHA1. As part of this change, let's make the current code templatized so we can limit as strongly as possible the duplication of code between sha1 and sha256.
Diffstat (limited to 'src')
-rw-r--r--src/mongo/crypto/SConscript49
-rw-r--r--src/mongo/crypto/sha1_block.cpp111
-rw-r--r--src/mongo/crypto/sha1_block.h143
-rw-r--r--src/mongo/crypto/sha1_block_openssl.cpp94
-rw-r--r--src/mongo/crypto/sha1_block_test.cpp63
-rw-r--r--src/mongo/crypto/sha1_block_tom.cpp80
-rw-r--r--src/mongo/crypto/sha256_block.cpp37
-rw-r--r--src/mongo/crypto/sha256_block.h56
-rw-r--r--src/mongo/crypto/sha256_block.idl33
-rw-r--r--src/mongo/crypto/sha256_block_test.cpp126
-rw-r--r--src/mongo/crypto/sha_block.h208
-rw-r--r--src/mongo/crypto/sha_block_openssl.cpp135
-rw-r--r--src/mongo/crypto/sha_block_tom.cpp127
-rw-r--r--src/mongo/crypto/tom/SConscript24
-rw-r--r--src/mongo/crypto/tom/tomcrypt.h104
-rw-r--r--src/mongo/crypto/tom/tomcrypt_argchk.h63
-rw-r--r--src/mongo/crypto/tom/tomcrypt_cfg.h160
-rw-r--r--src/mongo/crypto/tom/tomcrypt_cipher.h970
-rw-r--r--src/mongo/crypto/tom/tomcrypt_custom.h422
-rw-r--r--src/mongo/crypto/tom/tomcrypt_hash.h403
-rw-r--r--src/mongo/crypto/tom/tomcrypt_mac.h473
-rw-r--r--src/mongo/crypto/tom/tomcrypt_macros.h512
-rw-r--r--src/mongo/crypto/tom/tomcrypt_misc.h42
-rw-r--r--src/mongo/db/SConscript2
-rw-r--r--src/mongo/util/make_array_type.h222
-rw-r--r--src/third_party/SConscript21
-rw-r--r--src/third_party/scripts/tomcrypt_get_sources.sh62
-rw-r--r--src/third_party/shim_tomcrypt.cpp3
-rw-r--r--src/third_party/tomcrypt-1.18.0-rc2/LICENSE3
-rw-r--r--src/third_party/tomcrypt-1.18.0-rc2/SConscript40
-rw-r--r--src/third_party/tomcrypt-1.18.0-rc2/src/hashes/helper/hash_memory.c (renamed from src/mongo/crypto/tom/hash_memory.c)16
-rw-r--r--src/third_party/tomcrypt-1.18.0-rc2/src/hashes/sha1.c (renamed from src/mongo/crypto/tom/sha1.c)14
-rw-r--r--src/third_party/tomcrypt-1.18.0-rc2/src/hashes/sha2/sha256.c334
-rw-r--r--src/third_party/tomcrypt-1.18.0-rc2/src/headers/tomcrypt.h102
-rw-r--r--src/third_party/tomcrypt-1.18.0-rc2/src/headers/tomcrypt_argchk.h53
-rw-r--r--src/third_party/tomcrypt-1.18.0-rc2/src/headers/tomcrypt_cfg.h277
-rw-r--r--src/third_party/tomcrypt-1.18.0-rc2/src/headers/tomcrypt_cipher.h1008
-rw-r--r--src/third_party/tomcrypt-1.18.0-rc2/src/headers/tomcrypt_custom.h598
-rw-r--r--src/third_party/tomcrypt-1.18.0-rc2/src/headers/tomcrypt_hash.h531
-rw-r--r--src/third_party/tomcrypt-1.18.0-rc2/src/headers/tomcrypt_mac.h568
-rw-r--r--src/third_party/tomcrypt-1.18.0-rc2/src/headers/tomcrypt_macros.h446
-rw-r--r--src/third_party/tomcrypt-1.18.0-rc2/src/headers/tomcrypt_math.h582
-rw-r--r--src/third_party/tomcrypt-1.18.0-rc2/src/headers/tomcrypt_misc.h115
-rw-r--r--src/third_party/tomcrypt-1.18.0-rc2/src/headers/tomcrypt_pk.h745
-rw-r--r--src/third_party/tomcrypt-1.18.0-rc2/src/headers/tomcrypt_pkcs.h108
-rw-r--r--src/third_party/tomcrypt-1.18.0-rc2/src/headers/tomcrypt_prng.h232
-rw-r--r--src/third_party/tomcrypt-1.18.0-rc2/src/mac/hmac/hmac_done.c (renamed from src/mongo/crypto/tom/hmac_done.c)26
-rw-r--r--src/third_party/tomcrypt-1.18.0-rc2/src/mac/hmac/hmac_init.c (renamed from src/mongo/crypto/tom/hmac_init.c)28
-rw-r--r--src/third_party/tomcrypt-1.18.0-rc2/src/mac/hmac/hmac_memory.c (renamed from src/mongo/crypto/tom/hmac_memory.c)18
-rw-r--r--src/third_party/tomcrypt-1.18.0-rc2/src/mac/hmac/hmac_process.c (renamed from src/mongo/crypto/tom/hmac_process.c)16
-rw-r--r--src/third_party/tomcrypt-1.18.0-rc2/src/misc/compare_testvector.c87
-rw-r--r--src/third_party/tomcrypt-1.18.0-rc2/src/misc/crypt/crypt_argchk.c (renamed from src/mongo/crypto/tom/crypt_argchk.c)13
-rw-r--r--src/third_party/tomcrypt-1.18.0-rc2/src/misc/crypt/crypt_find_hash.c (renamed from src/mongo/crypto/tom/crypt_find_hash.c)10
-rw-r--r--src/third_party/tomcrypt-1.18.0-rc2/src/misc/crypt/crypt_hash_descriptor.c (renamed from src/mongo/crypto/tom/crypt_hash_descriptor.c)8
-rw-r--r--src/third_party/tomcrypt-1.18.0-rc2/src/misc/crypt/crypt_hash_is_valid.c (renamed from src/mongo/crypto/tom/crypt_hash_is_valid.c)8
-rw-r--r--src/third_party/tomcrypt-1.18.0-rc2/src/misc/crypt/crypt_register_hash.c (renamed from src/mongo/crypto/tom/crypt_register_hash.c)8
-rw-r--r--src/third_party/tomcrypt-1.18.0-rc2/src/misc/zeromem.c (renamed from src/mongo/crypto/tom/zeromem.c)14
57 files changed, 7061 insertions, 3692 deletions
diff --git a/src/mongo/crypto/SConscript b/src/mongo/crypto/SConscript
index b7f06827201..ecf857b1217 100644
--- a/src/mongo/crypto/SConscript
+++ b/src/mongo/crypto/SConscript
@@ -4,15 +4,6 @@ Import("env")
env = env.Clone()
-env.SConscript(
- dirs=[
- 'tom',
- ],
- exports=[
- 'env',
- ],
-)
-
env.Library('sha1_block',
source=[
'sha1_block.cpp'
@@ -22,24 +13,42 @@ env.Library('sha1_block',
'$BUILD_DIR/mongo/util/secure_compare_memory',
])
-env.Library(
- target='sha1_block_tom',
+env.Library('sha256_block',
source=[
- 'sha1_block_tom.cpp'
+ 'sha256_block.cpp'
],
LIBDEPS=[
+ '$BUILD_DIR/mongo/base',
+ '$BUILD_DIR/mongo/util/secure_compare_memory',
+ ])
+
+if "tom" in env["MONGO_CRYPTO"]:
+ tomEnv = env.Clone();
+ tomEnv.InjectThirdPartyIncludePaths(libraries=['tomcrypt'])
+
+ tomEnv.Library(
+ target='sha_block_tom',
+ source=[
+ 'sha_block_tom.cpp'
+ ],
+ LIBDEPS=[
'$BUILD_DIR/mongo/base',
'sha1_block',
- 'tom/tomcrypt'
- ])
+ 'sha256_block',
+ ],
+ LIBDEPS_PRIVATE=[
+ '$BUILD_DIR/third_party/shim_tomcrypt',
+ ]
+ )
-env.Library('sha1_block_openssl',
+env.Library('sha_block_openssl',
source=[
- 'sha1_block_openssl.cpp'
+ 'sha_block_openssl.cpp'
],
LIBDEPS=[
'$BUILD_DIR/mongo/base',
'sha1_block',
+ 'sha256_block',
])
env.Library('scramauth',
@@ -48,8 +57,12 @@ env.Library('scramauth',
'$BUILD_DIR/mongo/base/secure_allocator',
'$BUILD_DIR/mongo/util/secure_compare_memory',
'$BUILD_DIR/mongo/util/secure_zero_memory',
- 'sha1_block_${MONGO_CRYPTO}'])
+ 'sha_block_${MONGO_CRYPTO}'])
env.CppUnitTest('sha1_block_test',
['sha1_block_test.cpp'],
- LIBDEPS=['sha1_block_${MONGO_CRYPTO}'])
+ LIBDEPS=['sha_block_${MONGO_CRYPTO}'])
+
+env.CppUnitTest('sha256_block_test',
+ ['sha256_block_test.cpp'],
+ LIBDEPS=['sha_block_${MONGO_CRYPTO}'])
diff --git a/src/mongo/crypto/sha1_block.cpp b/src/mongo/crypto/sha1_block.cpp
index f433b3ac698..2e3e0c8de84 100644
--- a/src/mongo/crypto/sha1_block.cpp
+++ b/src/mongo/crypto/sha1_block.cpp
@@ -1,104 +1,37 @@
/**
- * Copyright (C) 2017 MongoDB, Inc.
+ * Copyright (C) 2017 MongoDB Inc.
*
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License, version 3,
- * as published by the Free Software Foundation.
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License, version 3,
+ * as published by the Free Software Foundation.
*
- * 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
- * GNU Affero General Public License for more details.
+ * 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
+ * GNU Affero General Public License for more details.
*
- * You should have received a copy of the GNU Affero General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
*
- * 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 GNU Affero General 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.
+ * 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 GNU Affero General 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/crypto/sha1_block.h"
-#include "mongo/bson/bsonmisc.h"
-#include "mongo/bson/bsonobjbuilder.h"
-#include "mongo/util/base64.h"
-#include "mongo/util/mongoutils/str.h"
-#include "mongo/util/secure_compare_memory.h"
-
namespace mongo {
-constexpr size_t SHA1Block::kHashLength;
-
-SHA1Block::SHA1Block(HashType hash) : _hash(std::move(hash)) {}
-
-StatusWith<SHA1Block> SHA1Block::fromBuffer(const uint8_t* input, size_t inputLen) {
- if (inputLen != kHashLength) {
- return {ErrorCodes::InvalidLength,
- str::stream() << "Unsupported SHA1Hash hash length: " << inputLen};
- }
-
- HashType newHash;
- memcpy(newHash.data(), input, inputLen);
- return SHA1Block(newHash);
-}
-
-StatusWith<SHA1Block> SHA1Block::fromBinData(const BSONBinData& binData) {
- if (binData.type != BinDataGeneral) {
- return {ErrorCodes::UnsupportedFormat, "SHA1Block only accepts BinDataGeneral type"};
- }
-
- if (binData.length != kHashLength) {
- return {ErrorCodes::UnsupportedFormat,
- str::stream() << "Unsupported SHA1Block hash length: " << binData.length};
- }
-
- HashType newHash;
- memcpy(newHash.data(), binData.data, binData.length);
- return SHA1Block(newHash);
-}
-
-SHA1Block SHA1Block::fromBinData(std::vector<unsigned char> bytes) {
- HashType newHash;
- invariant(bytes.size() == kHashLength);
- memcpy(newHash.data(), bytes.data(), bytes.size());
- return SHA1Block(newHash);
-}
-
-std::string SHA1Block::toString() const {
- return base64::encode(reinterpret_cast<const char*>(_hash.data()), _hash.size());
-}
-
-void SHA1Block::appendAsBinData(BSONObjBuilder& builder, StringData fieldName) const {
- builder.appendBinData(fieldName, _hash.size(), BinDataGeneral, _hash.data());
-}
-
-void SHA1Block::xorInline(const SHA1Block& other) {
- for (size_t x = 0; x < _hash.size(); x++) {
- _hash[x] ^= other._hash[x];
- }
-}
-
-bool SHA1Block::operator==(const SHA1Block& other) const {
- return consttimeMemEqual(this->_hash.data(), other._hash.data(), kHashLength);
-}
-
-bool SHA1Block::operator!=(const SHA1Block& other) const {
- return !(*this == other);
-}
-
-std::ostream& operator<<(std::ostream& os, const SHA1Block& sha1) {
- return os << sha1.toString();
-}
+constexpr StringData SHA1BlockTraits::name;
} // namespace mongo
diff --git a/src/mongo/crypto/sha1_block.h b/src/mongo/crypto/sha1_block.h
index 7beaba8074d..51ed7fac155 100644
--- a/src/mongo/crypto/sha1_block.h
+++ b/src/mongo/crypto/sha1_block.h
@@ -1,129 +1,56 @@
/**
-* Copyright (C) 2017 MongoDB Inc.
-*
-* This program is free software: you can redistribute it and/or modify
-* it under the terms of the GNU Affero General Public License, version 3,
-* as published by the Free Software Foundation.
-*
-* 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
-* GNU Affero General Public License for more details.
-*
-* You should have received a copy of the GNU Affero General Public License
-* along with this program. If not, see <http://www.gnu.org/licenses/>.
-*
-* 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 GNU Affero General 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.
-*/
+ * Copyright (C) 2017 MongoDB Inc.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License, version 3,
+ * as published by the Free Software Foundation.
+ *
+ * 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
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * 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 GNU Affero General 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 <array>
-#include <cstddef>
-#include <string>
-#include <vector>
+#include "mongo/crypto/sha_block.h"
-#include "mongo/base/data_range.h"
-#include "mongo/base/status_with.h"
+#include "mongo/util/make_array_type.h"
namespace mongo {
-struct BSONBinData;
-class BSONObjBuilder;
-
/**
- * Data structure with fixed sized byte array that can be used as HMAC key or result of a SHA1
- * computation.
+ * A Traits type for adapting SHABlock to sha256 hashes.
*/
-class SHA1Block {
-public:
- static constexpr size_t kHashLength = 20;
- using HashType = std::array<std::uint8_t, kHashLength>;
+struct SHA1BlockTraits {
+ using HashType = MakeArrayType<std::uint8_t, 20, SHA1BlockTraits>;
- SHA1Block() = default;
- SHA1Block(HashType rawHash);
- static StatusWith<SHA1Block> fromBuffer(const uint8_t* input, size_t inputLen);
+ static constexpr StringData name = "SHA1Block"_sd;
- /**
- * Computes a SHA-1 hash of 'input'.
- */
- static SHA1Block computeHash(const uint8_t* input, size_t inputLen);
+ static HashType computeHash(std::initializer_list<ConstDataRange> input);
- /**
- * Computes a HMAC SHA-1 keyed hash of 'input' using the key 'key'
- */
- static SHA1Block computeHmac(const uint8_t* key,
- size_t keyLen,
- const uint8_t* input,
- size_t inputLen) {
- SHA1Block output;
- SHA1Block::computeHmac(key, keyLen, input, inputLen, &output);
- return output;
- }
-
- /**
- * Computes a HMAC SHA-1 keyed hash of 'input' using the key 'key'. Writes the results into
- * a pre-allocated SHA1Block. This lets us allocate SHA1Blocks with the SecureAllocator.
- */
static void computeHmac(const uint8_t* key,
size_t keyLen,
const uint8_t* input,
size_t inputLen,
- SHA1Block* const output);
-
- const uint8_t* data() const& {
- return _hash.data();
- }
-
- uint8_t* data() const&& = delete;
-
- ConstDataRange toCDR() const {
- return ConstDataRange(reinterpret_cast<const char*>(_hash.data()), kHashLength);
- }
-
- size_t size() const {
- return _hash.size();
- }
-
- /**
- * Make a new SHA1Block from a BSON BinData value.
- */
- static StatusWith<SHA1Block> fromBinData(const BSONBinData& binData);
-
- /**
- * Make a new SHA1Block from a vector of bytes representing bindata. For IDL.
- */
- static SHA1Block fromBinData(std::vector<unsigned char> bytes);
-
- /**
- * Append this to a builder using the given name as a BSON BinData type value.
- */
- void appendAsBinData(BSONObjBuilder& builder, StringData fieldName) const;
-
- /**
- * Do a bitwise xor against another SHA1Block and replace the current contents of this block
- * with the result.
- */
- void xorInline(const SHA1Block& other);
-
- std::string toString() const;
- bool operator==(const SHA1Block& other) const;
- bool operator!=(const SHA1Block& other) const;
-
-private:
- HashType _hash;
+ HashType* const output);
};
-std::ostream& operator<<(std::ostream& os, const SHA1Block& sha1);
+using SHA1Block = SHABlock<SHA1BlockTraits>;
} // namespace mongo
diff --git a/src/mongo/crypto/sha1_block_openssl.cpp b/src/mongo/crypto/sha1_block_openssl.cpp
deleted file mode 100644
index 75bb53e3960..00000000000
--- a/src/mongo/crypto/sha1_block_openssl.cpp
+++ /dev/null
@@ -1,94 +0,0 @@
-/*
- * Copyright (C) 2014 10gen Inc.
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License, version 3,
- * as published by the Free Software Foundation.
- *
- * 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
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- *
- * 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 GNU Affero General 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/crypto/sha1_block.h"
-
-#include "mongo/config.h"
-#include "mongo/stdx/memory.h"
-#include "mongo/util/assert_util.h"
-
-#ifndef MONGO_CONFIG_SSL
-#error This file should only be included in SSL-enabled builds
-#endif
-
-#include <cstring>
-#include <openssl/evp.h>
-#include <openssl/hmac.h>
-#include <openssl/sha.h>
-#if OPENSSL_VERSION_NUMBER < 0x10100000L
-namespace {
-// Copies of OpenSSL after 1.1.0 define new EVP digest routines. We must
-// polyfill used definitions to interact with older OpenSSL versions.
-EVP_MD_CTX* EVP_MD_CTX_new() {
- void* ret = OPENSSL_malloc(sizeof(EVP_MD_CTX));
-
- if (ret != NULL) {
- memset(ret, 0, sizeof(EVP_MD_CTX));
- }
- return static_cast<EVP_MD_CTX*>(ret);
-}
-
-void EVP_MD_CTX_free(EVP_MD_CTX* ctx) {
- EVP_MD_CTX_cleanup(ctx);
- OPENSSL_free(ctx);
-}
-} // namespace
-#endif
-
-namespace mongo {
-/*
- * Computes a SHA-1 hash of 'input'.
- */
-SHA1Block SHA1Block::computeHash(const uint8_t* input, size_t inputLen) {
- HashType output;
-
- std::unique_ptr<EVP_MD_CTX, decltype(&EVP_MD_CTX_free)> digestCtx(EVP_MD_CTX_new(),
- EVP_MD_CTX_free);
-
- fassert(40379,
- EVP_DigestInit_ex(digestCtx.get(), EVP_sha1(), NULL) == 1 &&
- EVP_DigestUpdate(digestCtx.get(), input, inputLen) == 1 &&
- EVP_DigestFinal_ex(digestCtx.get(), output.data(), NULL) == 1);
- return SHA1Block(output);
-}
-
-/*
- * Computes a HMAC SHA-1 keyed hash of 'input' using the key 'key', writes output into 'output'.
- */
-void SHA1Block::computeHmac(const uint8_t* key,
- size_t keyLen,
- const uint8_t* input,
- size_t inputLen,
- SHA1Block* const output) {
- fassert(40380,
- HMAC(EVP_sha1(), key, keyLen, input, inputLen, output->_hash.data(), NULL) != NULL);
-}
-
-} // namespace mongo
diff --git a/src/mongo/crypto/sha1_block_test.cpp b/src/mongo/crypto/sha1_block_test.cpp
index fa227f048a4..0315e59b173 100644
--- a/src/mongo/crypto/sha1_block_test.cpp
+++ b/src/mongo/crypto/sha1_block_test.cpp
@@ -1,29 +1,29 @@
/**
- * Copyright (C) 2014 10gen Inc.
+ * Copyright (C) 2017 MongoDB Inc.
*
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License, version 3,
- * as published by the Free Software Foundation.
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License, version 3,
+ * as published by the Free Software Foundation.
*
- * 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
- * GNU Affero General Public License for more details.
+ * 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
+ * GNU Affero General Public License for more details.
*
- * You should have received a copy of the GNU Affero General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
*
- * 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 GNU Affero General 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.
+ * 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 GNU Affero General 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"
@@ -36,23 +36,32 @@
namespace mongo {
namespace {
+ConstDataRange makeTestItem(StringData sd) {
+ return ConstDataRange(sd.rawData(), sd.size());
+}
+
// SHA-1 test vectors from http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/SHA_All.pdf
const struct {
- const char* msg;
+ std::initializer_list<ConstDataRange> msg;
SHA1Block hash;
} sha1Tests[] = {
- {"abc", SHA1Block::HashType{0xa9, 0x99, 0x3e, 0x36, 0x47, 0x06, 0x81, 0x6a, 0xba, 0x3e,
- 0x25, 0x71, 0x78, 0x50, 0xc2, 0x6c, 0x9c, 0xd0, 0xd8, 0x9d}},
+ {{makeTestItem("abc")},
+ SHA1Block::HashType{0xa9, 0x99, 0x3e, 0x36, 0x47, 0x06, 0x81, 0x6a, 0xba, 0x3e,
+ 0x25, 0x71, 0x78, 0x50, 0xc2, 0x6c, 0x9c, 0xd0, 0xd8, 0x9d}},
+
+ {{makeTestItem("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq")},
+ SHA1Block::HashType{0x84, 0x98, 0x3E, 0x44, 0x1C, 0x3B, 0xD2, 0x6E, 0xBA, 0xAE,
+ 0x4A, 0xA1, 0xF9, 0x51, 0x29, 0xE5, 0xE5, 0x46, 0x70, 0xF1}},
- {"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
+ // A variation on the above to test digesting multiple parts
+ {{makeTestItem("abcdbcdecdefdefgefghfghighijhijkijkljklmkl"), makeTestItem("mnlmnomnopnopq")},
SHA1Block::HashType{0x84, 0x98, 0x3E, 0x44, 0x1C, 0x3B, 0xD2, 0x6E, 0xBA, 0xAE,
0x4A, 0xA1, 0xF9, 0x51, 0x29, 0xE5, 0xE5, 0x46, 0x70, 0xF1}}};
TEST(CryptoVectors, SHA1) {
size_t numTests = sizeof(sha1Tests) / sizeof(sha1Tests[0]);
for (size_t i = 0; i < numTests; i++) {
- SHA1Block result = SHA1Block::computeHash(
- reinterpret_cast<const unsigned char*>(sha1Tests[i].msg), strlen(sha1Tests[i].msg));
+ SHA1Block result = SHA1Block::computeHash(sha1Tests[i].msg);
ASSERT(sha1Tests[i].hash == result) << "Failed SHA1 iteration " << i;
}
}
diff --git a/src/mongo/crypto/sha1_block_tom.cpp b/src/mongo/crypto/sha1_block_tom.cpp
deleted file mode 100644
index 7d32dbc2f53..00000000000
--- a/src/mongo/crypto/sha1_block_tom.cpp
+++ /dev/null
@@ -1,80 +0,0 @@
-/*
- * Copyright (C) 2014 10gen Inc.
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License, version 3,
- * as published by the Free Software Foundation.
- *
- * 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
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- *
- * 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 GNU Affero General 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/crypto/sha1_block.h"
-
-#include "mongo/config.h"
-#include "mongo/util/assert_util.h"
-
-#ifdef MONGO_CONFIG_SSL
-#error This file should not be included if compiling with SSL support
-#endif
-
-#include "mongo/crypto/tom/tomcrypt.h"
-
-namespace mongo {
-
-/*
- * Computes a SHA-1 hash of 'input'.
- */
-SHA1Block SHA1Block::computeHash(const uint8_t* input, size_t inputLen) {
- HashType output;
-
- hash_state hashState;
- fassert(40381,
- sha1_init(&hashState) == CRYPT_OK &&
- sha1_process(&hashState, input, inputLen) == CRYPT_OK &&
- sha1_done(&hashState, output.data()) == CRYPT_OK);
- return SHA1Block(output);
-}
-
-/*
- * Computes a HMAC SHA-1 keyed hash of 'input' using the key 'key'
- */
-void SHA1Block::computeHmac(const uint8_t* key,
- size_t keyLen,
- const uint8_t* input,
- size_t inputLen,
- SHA1Block* const output) {
- invariant(key && input);
-
- static int hashId = -1;
- if (hashId == -1) {
- register_hash(&sha1_desc);
- hashId = find_hash("sha1");
- }
-
- unsigned long sha1HashLen = 20;
- fassert(40382,
- hmac_memory(hashId, key, keyLen, input, inputLen, output->_hash.data(), &sha1HashLen) ==
- CRYPT_OK);
-}
-
-} // namespace mongo
diff --git a/src/mongo/crypto/sha256_block.cpp b/src/mongo/crypto/sha256_block.cpp
new file mode 100644
index 00000000000..1b5600e4147
--- /dev/null
+++ b/src/mongo/crypto/sha256_block.cpp
@@ -0,0 +1,37 @@
+/**
+ * Copyright (C) 2017 MongoDB Inc.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License, version 3,
+ * as published by the Free Software Foundation.
+ *
+ * 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
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * 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 GNU Affero General 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/crypto/sha256_block.h"
+
+namespace mongo {
+
+constexpr StringData SHA256BlockTraits::name;
+
+} // namespace mongo
diff --git a/src/mongo/crypto/sha256_block.h b/src/mongo/crypto/sha256_block.h
new file mode 100644
index 00000000000..2e24e551dc8
--- /dev/null
+++ b/src/mongo/crypto/sha256_block.h
@@ -0,0 +1,56 @@
+/**
+ * Copyright (C) 2017 MongoDB Inc.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License, version 3,
+ * as published by the Free Software Foundation.
+ *
+ * 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
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * 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 GNU Affero General 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 sha256 hashes.
+ */
+struct SHA256BlockTraits {
+ using HashType = MakeArrayType<std::uint8_t, 32, SHA256BlockTraits>;
+
+ static constexpr StringData name = "SHA256Block"_sd;
+
+ static HashType computeHash(std::initializer_list<ConstDataRange> input);
+
+ static void computeHmac(const uint8_t* key,
+ size_t keyLen,
+ const uint8_t* input,
+ size_t inputLen,
+ HashType* const output);
+};
+
+using SHA256Block = SHABlock<SHA256BlockTraits>;
+
+} // namespace mongo
diff --git a/src/mongo/crypto/sha256_block.idl b/src/mongo/crypto/sha256_block.idl
new file mode 100644
index 00000000000..25343a990db
--- /dev/null
+++ b/src/mongo/crypto/sha256_block.idl
@@ -0,0 +1,33 @@
+# Copyright (C) 2017 MongoDB Inc.
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License, version 3,
+# as published by the Free Software Foundation.
+#
+# 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
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+#
+
+# SHA256Block type
+
+global:
+ cpp_namespace: "mongo"
+ cpp_includes:
+ - "mongo/crypto/sha256_block.h"
+
+imports:
+ - "mongo/idl/basic_types.idl"
+
+types:
+ sha256Block:
+ bson_serialization_type: bindata
+ bindata_subtype: generic
+ description: "A fixed-size byte array that holds the result of a SHA256 computation"
+ cpp_type: mongo::SHA256Block
+ serializer: "mongo::SHA256Block::toCDR"
+ deserializer: "mongo::SHA256Block::fromBinData"
diff --git a/src/mongo/crypto/sha256_block_test.cpp b/src/mongo/crypto/sha256_block_test.cpp
new file mode 100644
index 00000000000..8b3ff5db4e9
--- /dev/null
+++ b/src/mongo/crypto/sha256_block_test.cpp
@@ -0,0 +1,126 @@
+/**
+ * Copyright (C) 2017 MongoDB Inc.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License, version 3,
+ * as published by the Free Software Foundation.
+ *
+ * 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
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * 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 GNU Affero General 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/sha256_block.h"
+#include "mongo/unittest/unittest.h"
+
+namespace mongo {
+namespace {
+
+ConstDataRange makeTestItem(StringData sd) {
+ return ConstDataRange(sd.rawData(), sd.size());
+}
+
+// SHA-256 test vectors from tom crypt
+const struct {
+ std::initializer_list<ConstDataRange> msg;
+ SHA256Block hash;
+} sha256Tests[] = {
+ {{makeTestItem("abc")},
+ SHA256Block::HashType{0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea, 0x41, 0x41, 0x40,
+ 0xde, 0x5d, 0xae, 0x22, 0x23, 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17,
+ 0x7a, 0x9c, 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad}},
+
+ {{makeTestItem("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq")},
+ SHA256Block::HashType{0x24, 0x8d, 0x6a, 0x61, 0xd2, 0x06, 0x38, 0xb8, 0xe5, 0xc0, 0x26,
+ 0x93, 0x0c, 0x3e, 0x60, 0x39, 0xa3, 0x3c, 0xe4, 0x59, 0x64, 0xff,
+ 0x21, 0x67, 0xf6, 0xec, 0xed, 0xd4, 0x19, 0xdb, 0x06, 0xc1}},
+
+ // A variation on the above to test digesting multiple parts
+ {{makeTestItem("abcdbcdecdefdefgefghfghi"), makeTestItem("ghijhijkijkljklmklmnlmnomnopnopq")},
+ SHA256Block::HashType{0x24, 0x8d, 0x6a, 0x61, 0xd2, 0x06, 0x38, 0xb8, 0xe5, 0xc0, 0x26,
+ 0x93, 0x0c, 0x3e, 0x60, 0x39, 0xa3, 0x3c, 0xe4, 0x59, 0x64, 0xff,
+ 0x21, 0x67, 0xf6, 0xec, 0xed, 0xd4, 0x19, 0xdb, 0x06, 0xc1}}};
+
+
+TEST(CryptoVectors, SHA256) {
+ size_t numTests = sizeof(sha256Tests) / sizeof(sha256Tests[0]);
+ for (size_t i = 0; i < numTests; i++) {
+ SHA256Block result = SHA256Block::computeHash(sha256Tests[i].msg);
+ ASSERT(sha256Tests[i].hash == result) << "Failed SHA256 iteration " << i;
+ }
+}
+
+TEST(SHA256Block, BinDataRoundTrip) {
+ SHA256Block::HashType rawHash;
+ rawHash.fill(0);
+ for (size_t i = 0; i < rawHash.size(); i++) {
+ rawHash[i] = i;
+ }
+
+ SHA256Block 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(SHA256Block::kHashLength, static_cast<size_t>(binLen));
+
+ auto newHashStatus =
+ SHA256Block::fromBinData(BSONBinData(rawBinData, binLen, hashElem.binDataType()));
+ ASSERT_OK(newHashStatus.getStatus());
+ ASSERT_TRUE(testHash == newHashStatus.getValue());
+}
+
+TEST(SHA256Block, CanOnlyConstructFromBinGeneral) {
+ std::string dummy(SHA256Block::kHashLength, 'x');
+
+ auto newHashStatus =
+ SHA256Block::fromBinData(BSONBinData(dummy.c_str(), dummy.size(), newUUID));
+ ASSERT_EQ(ErrorCodes::UnsupportedFormat, newHashStatus.getStatus());
+}
+
+TEST(SHA256Block, FromBinDataShouldRegectWrongSize) {
+ std::string dummy(SHA256Block::kHashLength - 1, 'x');
+
+ auto newHashStatus =
+ SHA256Block::fromBinData(BSONBinData(dummy.c_str(), dummy.size(), BinDataGeneral));
+ ASSERT_EQ(ErrorCodes::UnsupportedFormat, newHashStatus.getStatus());
+}
+
+TEST(SHA256Block, FromBufferShouldRejectWrongLength) {
+ std::string dummy(SHA256Block::kHashLength - 1, 'x');
+
+ auto newHashStatus =
+ SHA256Block::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
new file mode 100644
index 00000000000..443ef36ebf4
--- /dev/null
+++ b/src/mongo/crypto/sha_block.h
@@ -0,0 +1,208 @@
+/**
+ * Copyright (C) 2017 MongoDB Inc.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License, version 3,
+ * as published by the Free Software Foundation.
+ *
+ * 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
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * 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 GNU Affero General 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 <array>
+#include <cstddef>
+#include <string>
+#include <vector>
+
+#include "mongo/base/data_range.h"
+#include "mongo/base/status_with.h"
+#include "mongo/bson/bsonmisc.h"
+#include "mongo/bson/bsonobjbuilder.h"
+#include "mongo/util/base64.h"
+#include "mongo/util/secure_compare_memory.h"
+
+namespace mongo {
+
+struct BSONBinData;
+class BSONObjBuilder;
+
+/**
+ * Data structure with fixed sized byte array that can be used as HMAC key or result of a SHA
+ * computation.
+ */
+template <typename Traits>
+class SHABlock {
+public:
+ using HashType = typename Traits::HashType;
+ static constexpr size_t kHashLength = sizeof(HashType);
+
+ SHABlock() = default;
+
+ SHABlock(HashType rawHash) : _hash(rawHash) {}
+
+ /**
+ * Constructs a SHABlock from a buffer of specified size.
+ */
+ static StatusWith<SHABlock> fromBuffer(const uint8_t* input, size_t inputLen) {
+ if (inputLen != kHashLength) {
+ return {
+ ErrorCodes::InvalidLength,
+ str::stream() << "Unsupported " << Traits::name << " hash length: " << inputLen};
+ }
+
+ HashType newHash;
+ memcpy(newHash.data(), input, inputLen);
+ return SHABlock(newHash);
+ }
+
+ /**
+ * Computes a hash of 'input' from multiple contigous buffers.
+ */
+ static SHABlock computeHash(std::initializer_list<ConstDataRange> input) {
+ return SHABlock{Traits::computeHash(input)};
+ }
+
+ /**
+ * Computes a hash of 'input' from one buffer.
+ */
+ static SHABlock computeHash(const uint8_t* input, size_t inputLen) {
+ return computeHash({ConstDataRange(reinterpret_cast<const char*>(input), inputLen)});
+ }
+
+ /**
+ * Computes a HMAC keyed hash of 'input' using the key 'key'.
+ */
+ static SHABlock computeHmac(const uint8_t* key,
+ size_t keyLen,
+ const uint8_t* input,
+ size_t inputLen) {
+ SHABlock output;
+ SHABlock::computeHmac(key, keyLen, input, inputLen, &output);
+ return output;
+ }
+
+ /**
+ * Computes a HMAC keyed hash of 'input' using the key 'key'. Writes the results into
+ * a pre-allocated SHABlock. This lets us allocate SHABlocks with the SecureAllocator.
+ */
+ static void computeHmac(const uint8_t* key,
+ size_t keyLen,
+ const uint8_t* input,
+ size_t inputLen,
+ SHABlock* const output) {
+ return Traits::computeHmac(key, keyLen, input, inputLen, &(output->_hash));
+ }
+
+ const uint8_t* data() const& {
+ return _hash.data();
+ }
+
+ uint8_t* data() && = delete;
+
+ ConstDataRange toCDR() && = delete;
+
+ ConstDataRange toCDR() const& {
+ return ConstDataRange(reinterpret_cast<const char*>(_hash.data()), kHashLength);
+ }
+
+ size_t size() const {
+ return _hash.size();
+ }
+
+ /**
+ * Make a new SHABlock from a BSON BinData value.
+ */
+ static StatusWith<SHABlock> fromBinData(const BSONBinData& binData) {
+ if (binData.type != BinDataGeneral) {
+ return {ErrorCodes::UnsupportedFormat,
+ str::stream() << Traits::name << " only accepts BinDataGeneral type"};
+ }
+
+ if (binData.length != kHashLength) {
+ return {ErrorCodes::UnsupportedFormat,
+ str::stream() << "Unsupported " << Traits::name << " hash length: "
+ << binData.length};
+ }
+
+ HashType newHash;
+ memcpy(newHash.data(), binData.data, binData.length);
+ return SHABlock(newHash);
+ }
+
+ /**
+ * Make a new SHABlock from a vector of bytes representing bindata. For IDL.
+ */
+ static SHABlock fromBinData(const std::vector<unsigned char>& bytes) {
+ HashType newHash;
+ uassert(ErrorCodes::UnsupportedFormat,
+ str::stream() << "Unsupported " << Traits::name << " hash length: " << bytes.size(),
+ bytes.size() == kHashLength);
+ memcpy(newHash.data(), bytes.data(), bytes.size());
+ return SHABlock(newHash);
+ }
+
+ /**
+ * Append this to a builder using the given name as a BSON BinData type value.
+ */
+ void appendAsBinData(BSONObjBuilder& builder, StringData fieldName) const {
+ builder.appendBinData(fieldName, _hash.size(), BinDataGeneral, _hash.data());
+ }
+
+ /**
+ * Do a bitwise xor against another SHABlock and replace the current contents of this block
+ * with the result.
+ */
+ void xorInline(const SHABlock& other) {
+ for (size_t x = 0; x < _hash.size(); x++) {
+ _hash[x] ^= other._hash[x];
+ }
+ }
+
+ /**
+ * Base64 encoding of the sha block as a string.
+ */
+ std::string toString() const {
+ return base64::encode(reinterpret_cast<const char*>(_hash.data()), _hash.size());
+ }
+
+ bool operator==(const SHABlock& other) const {
+ return consttimeMemEqual(this->_hash.data(), other._hash.data(), kHashLength);
+ }
+
+ bool operator!=(const SHABlock& other) const {
+ return !(*this == other);
+ }
+
+private:
+ // The backing array of bytes for the sha block
+ HashType _hash;
+};
+
+template <typename T>
+constexpr size_t SHABlock<T>::kHashLength;
+
+template <typename Traits>
+std::ostream& operator<<(std::ostream& os, const SHABlock<Traits>& sha) {
+ return os << sha.toString();
+}
+
+} // namespace mongo
diff --git a/src/mongo/crypto/sha_block_openssl.cpp b/src/mongo/crypto/sha_block_openssl.cpp
new file mode 100644
index 00000000000..dce8ca88427
--- /dev/null
+++ b/src/mongo/crypto/sha_block_openssl.cpp
@@ -0,0 +1,135 @@
+/**
+ * Copyright (C) 2017 MongoDB Inc.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License, version 3,
+ * as published by the Free Software Foundation.
+ *
+ * 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
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * 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 GNU Affero General 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/crypto/sha1_block.h"
+#include "mongo/crypto/sha256_block.h"
+
+#include "mongo/config.h"
+#include "mongo/stdx/memory.h"
+#include "mongo/util/assert_util.h"
+
+#ifndef MONGO_CONFIG_SSL
+#error This file should only be included in SSL-enabled builds
+#endif
+
+#include <cstring>
+#include <openssl/evp.h>
+#include <openssl/hmac.h>
+#include <openssl/sha.h>
+
+#if OPENSSL_VERSION_NUMBER < 0x10100000L
+namespace {
+// Copies of OpenSSL after 1.1.0 define new EVP digest routines. We must
+// polyfill used definitions to interact with older OpenSSL versions.
+EVP_MD_CTX* EVP_MD_CTX_new() {
+ void* ret = OPENSSL_malloc(sizeof(EVP_MD_CTX));
+
+ if (ret != NULL) {
+ memset(ret, 0, sizeof(EVP_MD_CTX));
+ }
+ return static_cast<EVP_MD_CTX*>(ret);
+}
+
+void EVP_MD_CTX_free(EVP_MD_CTX* ctx) {
+ EVP_MD_CTX_cleanup(ctx);
+ OPENSSL_free(ctx);
+}
+} // namespace
+#endif
+
+namespace mongo {
+
+namespace {
+
+/*
+ * Computes a SHA hash of 'input'.
+ */
+template <typename HashType>
+HashType computeHashImpl(const EVP_MD* md, std::initializer_list<ConstDataRange> input) {
+ HashType output;
+
+ std::unique_ptr<EVP_MD_CTX, decltype(&EVP_MD_CTX_free)> digestCtx(EVP_MD_CTX_new(),
+ EVP_MD_CTX_free);
+
+ fassert(40379,
+ EVP_DigestInit_ex(digestCtx.get(), md, NULL) == 1 &&
+ std::all_of(begin(input),
+ end(input),
+ [&](const auto& i) {
+ return EVP_DigestUpdate(digestCtx.get(), i.data(), i.length()) == 1;
+ }) &&
+ EVP_DigestFinal_ex(digestCtx.get(), output.data(), NULL) == 1);
+ 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,
+ HashType* const output) {
+ fassert(40380, HMAC(md, key, keyLen, input, inputLen, output->data(), NULL) != NULL);
+}
+
+} // namespace
+
+SHA1BlockTraits::HashType SHA1BlockTraits::computeHash(
+ std::initializer_list<ConstDataRange> input) {
+ return computeHashImpl<SHA1BlockTraits::HashType>(EVP_sha1(), input);
+}
+
+SHA256BlockTraits::HashType SHA256BlockTraits::computeHash(
+ std::initializer_list<ConstDataRange> input) {
+ return computeHashImpl<SHA256BlockTraits::HashType>(EVP_sha256(), input);
+}
+
+void SHA1BlockTraits::computeHmac(const uint8_t* key,
+ size_t keyLen,
+ const uint8_t* input,
+ size_t inputLen,
+ SHA1BlockTraits::HashType* const output) {
+ return computeHmacImpl<SHA1BlockTraits::HashType>(
+ EVP_sha1(), key, keyLen, input, inputLen, output);
+}
+
+void SHA256BlockTraits::computeHmac(const uint8_t* key,
+ size_t keyLen,
+ const uint8_t* input,
+ size_t inputLen,
+ SHA256BlockTraits::HashType* const output) {
+ return computeHmacImpl<SHA256BlockTraits::HashType>(
+ EVP_sha256(), key, keyLen, input, inputLen, output);
+}
+
+} // namespace mongo
diff --git a/src/mongo/crypto/sha_block_tom.cpp b/src/mongo/crypto/sha_block_tom.cpp
new file mode 100644
index 00000000000..20e11d34ce8
--- /dev/null
+++ b/src/mongo/crypto/sha_block_tom.cpp
@@ -0,0 +1,127 @@
+/**
+ * Copyright (C) 2017 MongoDB Inc.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License, version 3,
+ * as published by the Free Software Foundation.
+ *
+ * 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
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * 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 GNU Affero General 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/crypto/sha1_block.h"
+#include "mongo/crypto/sha256_block.h"
+
+#include "mongo/config.h"
+#include "mongo/util/assert_util.h"
+
+#ifdef MONGO_CONFIG_SSL
+#error This file should not be included if compiling with SSL support
+#endif
+
+#include "tomcrypt.h"
+
+namespace mongo {
+
+namespace {
+
+/**
+ * Computes a SHA hash of 'input'.
+ */
+template <typename HashType,
+ int (*Init)(hash_state*),
+ int (*Process)(hash_state*, const unsigned char*, unsigned long),
+ int (*Done)(hash_state*, unsigned char*)>
+HashType computeHashImpl(std::initializer_list<ConstDataRange> input) {
+ HashType output;
+
+ hash_state hashState;
+ fassert(40381,
+ Init(&hashState) == CRYPT_OK &&
+ std::all_of(begin(input),
+ end(input),
+ [&](const auto& i) {
+ return Process(&hashState,
+ reinterpret_cast<const unsigned char*>(i.data()),
+ i.length()) == CRYPT_OK;
+ }) &&
+ Done(&hashState, output.data()) == CRYPT_OK);
+ 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 ltc_hash_descriptor* desc,
+ const uint8_t* key,
+ size_t keyLen,
+ const uint8_t* input,
+ size_t inputLen,
+ HashType* const output) {
+ invariant(key && input);
+
+ static const struct Magic {
+ Magic(const ltc_hash_descriptor* desc) {
+ register_hash(desc);
+ hashId = find_hash(desc->name);
+ }
+
+ int hashId;
+ } magic(desc);
+
+ unsigned long shaHashLen = sizeof(HashType);
+ fassert(40382,
+ hmac_memory(magic.hashId, key, keyLen, input, inputLen, output->data(), &shaHashLen) ==
+ CRYPT_OK);
+}
+
+} // namespace
+
+SHA1BlockTraits::HashType SHA1BlockTraits::computeHash(
+ std::initializer_list<ConstDataRange> input) {
+ return computeHashImpl<SHA1BlockTraits::HashType, sha1_init, sha1_process, sha1_done>(input);
+}
+
+SHA256BlockTraits::HashType SHA256BlockTraits::computeHash(
+ std::initializer_list<ConstDataRange> input) {
+ return computeHashImpl<SHA256BlockTraits::HashType, sha256_init, sha256_process, sha256_done>(
+ input);
+}
+
+void SHA1BlockTraits::computeHmac(const uint8_t* key,
+ size_t keyLen,
+ const uint8_t* input,
+ size_t inputLen,
+ HashType* const output) {
+ return computeHmacImpl<HashType>(&sha1_desc, key, keyLen, input, inputLen, output);
+}
+
+void SHA256BlockTraits::computeHmac(const uint8_t* key,
+ size_t keyLen,
+ const uint8_t* input,
+ size_t inputLen,
+ HashType* const output) {
+ return computeHmacImpl<HashType>(&sha256_desc, key, keyLen, input, inputLen, output);
+}
+
+} // namespace mongo
diff --git a/src/mongo/crypto/tom/SConscript b/src/mongo/crypto/tom/SConscript
deleted file mode 100644
index 4a888bd282b..00000000000
--- a/src/mongo/crypto/tom/SConscript
+++ /dev/null
@@ -1,24 +0,0 @@
-# -*- mode: python -*-
-
-Import("env")
-
-env = env.Clone()
-
-env.Library('tomcrypt',
- ['crypt_argchk.c',
- 'crypt_find_hash.c',
- 'crypt_hash_descriptor.c',
- 'crypt_hash_is_valid.c',
- 'crypt_register_hash.c',
- 'hash_memory.c',
- 'hmac_done.c',
- 'hmac_init.c',
- 'hmac_memory.c',
- 'hmac_process.c',
- 'sha1.c',
- 'zeromem.c'],
- LIBDEPS=[],
- LIBDEPS_TAGS=[
- 'init-no-global-side-effects',
- ],
-)
diff --git a/src/mongo/crypto/tom/tomcrypt.h b/src/mongo/crypto/tom/tomcrypt.h
deleted file mode 100644
index aa4d5c02df5..00000000000
--- a/src/mongo/crypto/tom/tomcrypt.h
+++ /dev/null
@@ -1,104 +0,0 @@
-/* Copyright 2014 MongoDB Inc.
- *
- * 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
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#ifndef TOMCRYPT_H_
-#define TOMCRYPT_H_
-#include <assert.h>
-#include <ctype.h>
-#include <limits.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <time.h>
-
-/* use configuration data */
-#include "tomcrypt_custom.h"
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-/* version */
-#define CRYPT 0x0117
-#define SCRYPT "1.17"
-
-/* max size of either a cipher/hash block or symmetric key [largest of the two] */
-#define MAXBLOCKSIZE 128
-
-/* descriptor table size */
-#define TAB_SIZE 32
-
-/* error codes [will be expanded in future releases] */
-enum {
- CRYPT_OK = 0, /* Result OK */
- CRYPT_ERROR, /* Generic Error */
- CRYPT_NOP, /* Not a failure but no operation was performed */
-
- CRYPT_INVALID_KEYSIZE, /* Invalid key size given */
- CRYPT_INVALID_ROUNDS, /* Invalid number of rounds */
- CRYPT_FAIL_TESTVECTOR, /* Algorithm failed test vectors */
-
- CRYPT_BUFFER_OVERFLOW, /* Not enough space for output */
- CRYPT_INVALID_PACKET, /* Invalid input packet given */
-
- CRYPT_INVALID_PRNGSIZE, /* Invalid number of bits for a PRNG */
- CRYPT_ERROR_READPRNG, /* Could not read enough from PRNG */
-
- CRYPT_INVALID_CIPHER, /* Invalid cipher specified */
- CRYPT_INVALID_HASH, /* Invalid hash specified */
- CRYPT_INVALID_PRNG, /* Invalid PRNG specified */
-
- CRYPT_MEM, /* Out of memory */
-
- CRYPT_PK_TYPE_MISMATCH, /* Not equivalent types of PK keys */
- CRYPT_PK_NOT_PRIVATE, /* Requires a private PK key */
-
- CRYPT_INVALID_ARG, /* Generic invalid argument */
- CRYPT_FILE_NOTFOUND, /* File Not Found */
-
- CRYPT_PK_INVALID_TYPE, /* Invalid type of PK key */
- CRYPT_PK_INVALID_SYSTEM, /* Invalid PK system specified */
- CRYPT_PK_DUP, /* Duplicate key already in key ring */
- CRYPT_PK_NOT_FOUND, /* Key not found in keyring */
- CRYPT_PK_INVALID_SIZE, /* Invalid size input for PK parameters */
-
- CRYPT_INVALID_PRIME_SIZE, /* Invalid size of prime requested */
- CRYPT_PK_INVALID_PADDING /* Invalid padding on input */
-};
-
-// clang-format off
-#include "tomcrypt_cfg.h"
-#include "tomcrypt_macros.h"
-#include "tomcrypt_cipher.h"
-#include "tomcrypt_hash.h"
-#include "tomcrypt_mac.h"
-//#include <tomcrypt_prng.h>
-//#include <tomcrypt_pk.h>
-//#include <tomcrypt_math.h>
-#include "tomcrypt_misc.h"
-#include "tomcrypt_argchk.h"
-//#include <tomcrypt_pkcs.h>
-// clang-format on
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif /* TOMCRYPT_H_ */
-
-
-/* $Source: /cvs/libtom/libtomcrypt/src/headers/tomcrypt.h,v $ */
-/* $Revision: 1.21 $ */
-/* $Date: 2006/12/16 19:34:05 $ */
diff --git a/src/mongo/crypto/tom/tomcrypt_argchk.h b/src/mongo/crypto/tom/tomcrypt_argchk.h
deleted file mode 100644
index 895402552f5..00000000000
--- a/src/mongo/crypto/tom/tomcrypt_argchk.h
+++ /dev/null
@@ -1,63 +0,0 @@
-/* Copyright 2014 MongoDB Inc.
- *
- * 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
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-/* Defines the LTC_ARGCHK macro used within the library */
-/* ARGTYPE is defined in mycrypt_cfg.h */
-#if ARGTYPE == 0
-
-#include <signal.h>
-
-/* this is the default LibTomCrypt macro */
-void crypt_argchk(const char* v, const char* s, int d);
-#define LTC_ARGCHK(x) \
- if (!(x)) { \
- crypt_argchk(#x, __FILE__, __LINE__); \
- }
-#define LTC_ARGCHKVD(x) LTC_ARGCHK(x)
-
-#elif ARGTYPE == 1
-
-/* fatal type of error */
-#define LTC_ARGCHK(x) assert((x))
-#define LTC_ARGCHKVD(x) LTC_ARGCHK(x)
-
-#elif ARGTYPE == 2
-
-#define LTC_ARGCHK(x) \
- if (!(x)) { \
- fprintf(stderr, "\nwarning: ARGCHK failed at %s:%d\n", __FILE__, __LINE__); \
- }
-#define LTC_ARGCHKVD(x) LTC_ARGCHK(x)
-
-#elif ARGTYPE == 3
-
-#define LTC_ARGCHK(x)
-#define LTC_ARGCHKVD(x) LTC_ARGCHK(x)
-
-#elif ARGTYPE == 4
-
-#define LTC_ARGCHK(x) \
- if (!(x)) \
- return CRYPT_INVALID_ARG;
-#define LTC_ARGCHKVD(x) \
- if (!(x)) \
- return;
-
-#endif
-
-
-/* $Source: /cvs/libtom/libtomcrypt/src/headers/tomcrypt_argchk.h,v $ */
-/* $Revision: 1.5 $ */
-/* $Date: 2006/08/27 20:50:21 $ */
diff --git a/src/mongo/crypto/tom/tomcrypt_cfg.h b/src/mongo/crypto/tom/tomcrypt_cfg.h
deleted file mode 100644
index 37dc04780d9..00000000000
--- a/src/mongo/crypto/tom/tomcrypt_cfg.h
+++ /dev/null
@@ -1,160 +0,0 @@
-/* Copyright 2014 MongoDB Inc.
- *
- * 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
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-/* This is the build config file.
- *
- * With this you can setup what to inlcude/exclude automatically during any build. Just comment
- * out the line that #define's the word for the thing you want to remove. phew!
- */
-
-#ifndef TOMCRYPT_CFG_H
-#define TOMCRYPT_CFG_H
-
-#if defined(_WIN32) || defined(_MSC_VER)
-#define LTC_CALL __cdecl
-#else
-#ifndef LTC_CALL
-#define LTC_CALL
-#endif
-#endif
-
-#ifndef LTC_EXPORT
-#define LTC_EXPORT
-#endif
-
-/* certain platforms use macros for these, making the prototypes broken */
-#ifndef LTC_NO_PROTOTYPES
-
-/* you can change how memory allocation works ... */
-LTC_EXPORT void* LTC_CALL XMALLOC(size_t n);
-LTC_EXPORT void* LTC_CALL XREALLOC(void* p, size_t n);
-LTC_EXPORT void* LTC_CALL XCALLOC(size_t n, size_t s);
-LTC_EXPORT void LTC_CALL XFREE(void* p);
-
-LTC_EXPORT void LTC_CALL XQSORT(void* base,
- size_t nmemb,
- size_t size,
- int (*compar)(const void*, const void*));
-
-
-/* change the clock function too */
-LTC_EXPORT clock_t LTC_CALL XCLOCK(void);
-
-/* various other functions */
-LTC_EXPORT void* LTC_CALL XMEMCPY(void* dest, const void* src, size_t n);
-LTC_EXPORT int LTC_CALL XMEMCMP(const void* s1, const void* s2, size_t n);
-LTC_EXPORT void* LTC_CALL XMEMSET(void* s, int c, size_t n);
-
-LTC_EXPORT int LTC_CALL XSTRCMP(const char* s1, const char* s2);
-
-#endif
-
-/* type of argument checking, 0=default, 1=fatal and 2=error+continue, 3=nothing */
-#ifndef ARGTYPE
-#define ARGTYPE 0
-#endif
-
-/* Controls endianess and size of registers. Leave uncommented to get platform neutral [slower]
- * code
- *
- * Note: in order to use the optimized macros your platform must support unaligned 32 and 64 bit
- * read/writes. The x86 platforms allow this but some others [ARM for instance] do not. On those
- * platforms you **MUST** use the portable [slower] macros.
- */
-
-/* detect x86-32 machines somewhat */
-#if !defined(__STRICT_ANSI__) && \
- (defined(INTEL_CC) || (defined(_MSC_VER) && defined(WIN32)) || \
- (defined(__GNUC__) && \
- (defined(__DJGPP__) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__i386__))))
-#define ENDIAN_LITTLE
-#define ENDIAN_32BITWORD
-#define LTC_FAST
-#define LTC_FAST_TYPE unsigned long
-#endif
-
-/* detects MIPS R5900 processors (PS2) */
-#if (defined(__R5900) || defined(R5900) || defined(__R5900__)) && \
- (defined(_mips) || defined(__mips__) || defined(mips))
-#define ENDIAN_LITTLE
-#define ENDIAN_64BITWORD
-#endif
-
-/* detect amd64 */
-#if !defined(__STRICT_ANSI__) && defined(__x86_64__)
-#define ENDIAN_LITTLE
-#define ENDIAN_64BITWORD
-#define LTC_FAST
-#define LTC_FAST_TYPE unsigned long
-#endif
-
-/* detect PPC32 */
-#if !defined(__STRICT_ANSI__) && defined(LTC_PPC32)
-#define ENDIAN_BIG
-#define ENDIAN_32BITWORD
-#define LTC_FAST
-#define LTC_FAST_TYPE unsigned long
-#endif
-
-/* detect sparc and sparc64 */
-#if defined(__sparc__)
-#define ENDIAN_BIG
-#if defined(__arch64__)
-#define ENDIAN_64BITWORD
-#else
-#define ENDIAN_32BITWORD
-#endif
-#endif
-
-
-#ifdef LTC_NO_FAST
-#ifdef LTC_FAST
-#undef LTC_FAST
-#endif
-#endif
-
-/* No asm is a quick way to disable anything "not portable" */
-#ifdef LTC_NO_ASM
-#undef ENDIAN_LITTLE
-#undef ENDIAN_BIG
-#undef ENDIAN_32BITWORD
-#undef ENDIAN_64BITWORD
-#undef LTC_FAST
-#undef LTC_FAST_TYPE
-#define LTC_NO_ROLC
-#define LTC_NO_BSWAP
-#endif
-
-/* #define ENDIAN_LITTLE */
-/* #define ENDIAN_BIG */
-
-/* #define ENDIAN_32BITWORD */
-/* #define ENDIAN_64BITWORD */
-
-#if (defined(ENDIAN_BIG) || defined(ENDIAN_LITTLE)) && \
- !(defined(ENDIAN_32BITWORD) || defined(ENDIAN_64BITWORD))
-#error You must specify a word size as well as endianess in tomcrypt_cfg.h
-#endif
-
-#if !(defined(ENDIAN_BIG) || defined(ENDIAN_LITTLE))
-#define ENDIAN_NEUTRAL
-#endif
-
-#endif
-
-
-/* $Source: /cvs/libtom/libtomcrypt/src/headers/tomcrypt_cfg.h,v $ */
-/* $Revision: 1.19 $ */
-/* $Date: 2006/12/04 02:19:48 $ */
diff --git a/src/mongo/crypto/tom/tomcrypt_cipher.h b/src/mongo/crypto/tom/tomcrypt_cipher.h
deleted file mode 100644
index 43efbacc752..00000000000
--- a/src/mongo/crypto/tom/tomcrypt_cipher.h
+++ /dev/null
@@ -1,970 +0,0 @@
-/* Copyright 2014 MongoDB Inc.
- *
- * 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
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-/* ---- SYMMETRIC KEY STUFF -----
- *
- * We put each of the ciphers scheduled keys in their own structs then we put all of
- * the key formats in one union. This makes the function prototypes easier to use.
- */
-#ifdef LTC_BLOWFISH
-struct blowfish_key {
- ulong32 S[4][256];
- ulong32 K[18];
-};
-#endif
-
-#ifdef LTC_RC5
-struct rc5_key {
- int rounds;
- ulong32 K[50];
-};
-#endif
-
-#ifdef LTC_RC6
-struct rc6_key {
- ulong32 K[44];
-};
-#endif
-
-#ifdef LTC_SAFERP
-struct saferp_key {
- unsigned char K[33][16];
- long rounds;
-};
-#endif
-
-#ifdef LTC_RIJNDAEL
-struct rijndael_key {
- ulong32 eK[60], dK[60];
- int Nr;
-};
-#endif
-
-#ifdef LTC_KSEED
-struct kseed_key {
- ulong32 K[32], dK[32];
-};
-#endif
-
-#ifdef LTC_KASUMI
-struct kasumi_key {
- ulong32 KLi1[8], KLi2[8], KOi1[8], KOi2[8], KOi3[8], KIi1[8], KIi2[8], KIi3[8];
-};
-#endif
-
-#ifdef LTC_XTEA
-struct xtea_key {
- unsigned long A[32], B[32];
-};
-#endif
-
-#ifdef LTC_TWOFISH
-#ifndef LTC_TWOFISH_SMALL
-struct twofish_key {
- ulong32 S[4][256], K[40];
-};
-#else
-struct twofish_key {
- ulong32 K[40];
- unsigned char S[32], start;
-};
-#endif
-#endif
-
-#ifdef LTC_SAFER
-#define LTC_SAFER_K64_DEFAULT_NOF_ROUNDS 6
-#define LTC_SAFER_K128_DEFAULT_NOF_ROUNDS 10
-#define LTC_SAFER_SK64_DEFAULT_NOF_ROUNDS 8
-#define LTC_SAFER_SK128_DEFAULT_NOF_ROUNDS 10
-#define LTC_SAFER_MAX_NOF_ROUNDS 13
-#define LTC_SAFER_BLOCK_LEN 8
-#define LTC_SAFER_KEY_LEN (1 + LTC_SAFER_BLOCK_LEN * (1 + 2 * LTC_SAFER_MAX_NOF_ROUNDS))
-typedef unsigned char safer_block_t[LTC_SAFER_BLOCK_LEN];
-typedef unsigned char safer_key_t[LTC_SAFER_KEY_LEN];
-struct safer_key {
- safer_key_t key;
-};
-#endif
-
-#ifdef LTC_RC2
-struct rc2_key {
- unsigned xkey[64];
-};
-#endif
-
-#ifdef LTC_DES
-struct des_key {
- ulong32 ek[32], dk[32];
-};
-
-struct des3_key {
- ulong32 ek[3][32], dk[3][32];
-};
-#endif
-
-#ifdef LTC_CAST5
-struct cast5_key {
- ulong32 K[32], keylen;
-};
-#endif
-
-#ifdef LTC_NOEKEON
-struct noekeon_key {
- ulong32 K[4], dK[4];
-};
-#endif
-
-#ifdef LTC_SKIPJACK
-struct skipjack_key {
- unsigned char key[10];
-};
-#endif
-
-#ifdef LTC_KHAZAD
-struct khazad_key {
- ulong64 roundKeyEnc[8 + 1];
- ulong64 roundKeyDec[8 + 1];
-};
-#endif
-
-#ifdef LTC_ANUBIS
-struct anubis_key {
- int keyBits;
- int R;
- ulong32 roundKeyEnc[18 + 1][4];
- ulong32 roundKeyDec[18 + 1][4];
-};
-#endif
-
-#ifdef LTC_MULTI2
-struct multi2_key {
- int N;
- ulong32 uk[8];
-};
-#endif
-
-typedef union Symmetric_key {
-#ifdef LTC_DES
- struct des_key des;
- struct des3_key des3;
-#endif
-#ifdef LTC_RC2
- struct rc2_key rc2;
-#endif
-#ifdef LTC_SAFER
- struct safer_key safer;
-#endif
-#ifdef LTC_TWOFISH
- struct twofish_key twofish;
-#endif
-#ifdef LTC_BLOWFISH
- struct blowfish_key blowfish;
-#endif
-#ifdef LTC_RC5
- struct rc5_key rc5;
-#endif
-#ifdef LTC_RC6
- struct rc6_key rc6;
-#endif
-#ifdef LTC_SAFERP
- struct saferp_key saferp;
-#endif
-#ifdef LTC_RIJNDAEL
- struct rijndael_key rijndael;
-#endif
-#ifdef LTC_XTEA
- struct xtea_key xtea;
-#endif
-#ifdef LTC_CAST5
- struct cast5_key cast5;
-#endif
-#ifdef LTC_NOEKEON
- struct noekeon_key noekeon;
-#endif
-#ifdef LTC_SKIPJACK
- struct skipjack_key skipjack;
-#endif
-#ifdef LTC_KHAZAD
- struct khazad_key khazad;
-#endif
-#ifdef LTC_ANUBIS
- struct anubis_key anubis;
-#endif
-#ifdef LTC_KSEED
- struct kseed_key kseed;
-#endif
-#ifdef LTC_KASUMI
- struct kasumi_key kasumi;
-#endif
-#ifdef LTC_MULTI2
- struct multi2_key multi2;
-#endif
- void* data;
-} symmetric_key;
-
-#ifdef LTC_ECB_MODE
-/** A block cipher ECB structure */
-typedef struct {
- /** The index of the cipher chosen */
- int cipher,
- /** The block size of the given cipher */
- blocklen;
- /** The scheduled key */
- symmetric_key key;
-} symmetric_ECB;
-#endif
-
-#ifdef LTC_CFB_MODE
-/** A block cipher CFB structure */
-typedef struct {
- /** The index of the cipher chosen */
- int cipher,
- /** The block size of the given cipher */
- blocklen,
- /** The padding offset */
- padlen;
- /** The current IV */
- unsigned char IV[MAXBLOCKSIZE],
- /** The pad used to encrypt/decrypt */
- pad[MAXBLOCKSIZE];
- /** The scheduled key */
- symmetric_key key;
-} symmetric_CFB;
-#endif
-
-#ifdef LTC_OFB_MODE
-/** A block cipher OFB structure */
-typedef struct {
- /** The index of the cipher chosen */
- int cipher,
- /** The block size of the given cipher */
- blocklen,
- /** The padding offset */
- padlen;
- /** The current IV */
- unsigned char IV[MAXBLOCKSIZE];
- /** The scheduled key */
- symmetric_key key;
-} symmetric_OFB;
-#endif
-
-#ifdef LTC_CBC_MODE
-/** A block cipher CBC structure */
-typedef struct {
- /** The index of the cipher chosen */
- int cipher,
- /** The block size of the given cipher */
- blocklen;
- /** The current IV */
- unsigned char IV[MAXBLOCKSIZE];
- /** The scheduled key */
- symmetric_key key;
-} symmetric_CBC;
-#endif
-
-
-#ifdef LTC_CTR_MODE
-/** A block cipher CTR structure */
-typedef struct {
- /** The index of the cipher chosen */
- int cipher,
- /** The block size of the given cipher */
- blocklen,
- /** The padding offset */
- padlen,
- /** The mode (endianess) of the CTR, 0==little, 1==big */
- mode,
- /** counter width */
- ctrlen;
-
- /** The counter */
- unsigned char ctr[MAXBLOCKSIZE],
- /** The pad used to encrypt/decrypt */
- pad[MAXBLOCKSIZE];
- /** The scheduled key */
- symmetric_key key;
-} symmetric_CTR;
-#endif
-
-
-#ifdef LTC_LRW_MODE
-/** A LRW structure */
-typedef struct {
- /** The index of the cipher chosen (must be a 128-bit block cipher) */
- int cipher;
-
- /** The current IV */
- unsigned char IV[16],
-
- /** the tweak key */
- tweak[16],
-
- /** The current pad, it's the product of the first 15 bytes against the tweak key */
- pad[16];
-
- /** The scheduled symmetric key */
- symmetric_key key;
-
-#ifdef LRW_TABLES
- /** The pre-computed multiplication table */
- unsigned char PC[16][256][16];
-#endif
-} symmetric_LRW;
-#endif
-
-#ifdef LTC_F8_MODE
-/** A block cipher F8 structure */
-typedef struct {
- /** The index of the cipher chosen */
- int cipher,
- /** The block size of the given cipher */
- blocklen,
- /** The padding offset */
- padlen;
- /** The current IV */
- unsigned char IV[MAXBLOCKSIZE], MIV[MAXBLOCKSIZE];
- /** Current block count */
- ulong32 blockcnt;
- /** The scheduled key */
- symmetric_key key;
-} symmetric_F8;
-#endif
-
-
-/** cipher descriptor table, last entry has "name == NULL" to mark the end of table */
-extern struct ltc_cipher_descriptor {
- /** name of cipher */
- char* name;
- /** internal ID */
- unsigned char ID;
- /** min keysize (octets) */
- int min_key_length,
- /** max keysize (octets) */
- max_key_length,
- /** block size (octets) */
- block_length,
- /** default number of rounds */
- default_rounds;
- /** Setup the cipher
- @param key The input symmetric key
- @param keylen The length of the input key (octets)
- @param num_rounds The requested number of rounds (0==default)
- @param skey [out] The destination of the scheduled key
- @return CRYPT_OK if successful
- */
- int (*setup)(const unsigned char* key, int keylen, int num_rounds, symmetric_key* skey);
- /** Encrypt a block
- @param pt The plaintex
- @param ct [out] The ciphertex
- @param skey The scheduled key
- @return CRYPT_OK if successful
- */
- int (*ecb_encrypt)(const unsigned char* pt, unsigned char* ct, symmetric_key* skey);
- /** Decrypt a block
- @param ct The ciphertex
- @param pt [out] The plaintex
- @param skey The scheduled key
- @return CRYPT_OK if successful
- */
- int (*ecb_decrypt)(const unsigned char* ct, unsigned char* pt, symmetric_key* skey);
- /** Test the block cipher
- @return CRYPT_OK if successful, CRYPT_NOP if self-testing has been disabled
- */
- int (*test)(void);
-
- /** Terminate the contex
- @param skey The scheduled key
- */
- void (*done)(symmetric_key* skey);
-
- /** Determine a key size
- @param keysize [in/out] The size of the key desired and the suggested size
- @return CRYPT_OK if successful
- */
- int (*keysize)(int* keysize);
-
- /** Accelerators **/
- /** Accelerated ECB encryption
- @param pt Plaintex
- @param ct Ciphertex
- @param blocks The number of complete blocks to process
- @param skey The scheduled key contex
- @return CRYPT_OK if successful
- */
- int (*accel_ecb_encrypt)(const unsigned char* pt,
- unsigned char* ct,
- unsigned long blocks,
- symmetric_key* skey);
-
- /** Accelerated ECB decryption
- @param pt Plaintex
- @param ct Ciphertex
- @param blocks The number of complete blocks to process
- @param skey The scheduled key contex
- @return CRYPT_OK if successful
- */
- int (*accel_ecb_decrypt)(const unsigned char* ct,
- unsigned char* pt,
- unsigned long blocks,
- symmetric_key* skey);
-
- /** Accelerated CBC encryption
- @param pt Plaintex
- @param ct Ciphertex
- @param blocks The number of complete blocks to process
- @param IV The initial value (input/output)
- @param skey The scheduled key contex
- @return CRYPT_OK if successful
- */
- int (*accel_cbc_encrypt)(const unsigned char* pt,
- unsigned char* ct,
- unsigned long blocks,
- unsigned char* IV,
- symmetric_key* skey);
-
- /** Accelerated CBC decryption
- @param pt Plaintex
- @param ct Ciphertex
- @param blocks The number of complete blocks to process
- @param IV The initial value (input/output)
- @param skey The scheduled key contex
- @return CRYPT_OK if successful
- */
- int (*accel_cbc_decrypt)(const unsigned char* ct,
- unsigned char* pt,
- unsigned long blocks,
- unsigned char* IV,
- symmetric_key* skey);
-
- /** Accelerated CTR encryption
- @param pt Plaintex
- @param ct Ciphertex
- @param blocks The number of complete blocks to process
- @param IV The initial value (input/output)
- @param mode little or big endian counter (mode=0 or mode=1)
- @param skey The scheduled key contex
- @return CRYPT_OK if successful
- */
- int (*accel_ctr_encrypt)(const unsigned char* pt,
- unsigned char* ct,
- unsigned long blocks,
- unsigned char* IV,
- int mode,
- symmetric_key* skey);
-
- /** Accelerated LRW
- @param pt Plaintex
- @param ct Ciphertex
- @param blocks The number of complete blocks to process
- @param IV The initial value (input/output)
- @param tweak The LRW tweak
- @param skey The scheduled key contex
- @return CRYPT_OK if successful
- */
- int (*accel_lrw_encrypt)(const unsigned char* pt,
- unsigned char* ct,
- unsigned long blocks,
- unsigned char* IV,
- const unsigned char* tweak,
- symmetric_key* skey);
-
- /** Accelerated LRW
- @param ct Ciphertex
- @param pt Plaintex
- @param blocks The number of complete blocks to process
- @param IV The initial value (input/output)
- @param tweak The LRW tweak
- @param skey The scheduled key contex
- @return CRYPT_OK if successful
- */
- int (*accel_lrw_decrypt)(const unsigned char* ct,
- unsigned char* pt,
- unsigned long blocks,
- unsigned char* IV,
- const unsigned char* tweak,
- symmetric_key* skey);
-
- /** Accelerated CCM packet (one-shot)
- @param key The secret key to use
- @param keylen The length of the secret key (octets)
- @param uskey A previously scheduled key [optional can be NULL]
- @param nonce The session nonce [use once]
- @param noncelen The length of the nonce
- @param header The header for the session
- @param headerlen The length of the header (octets)
- @param pt [out] The plaintex
- @param ptlen The length of the plaintext (octets)
- @param ct [out] The ciphertex
- @param tag [out] The destination tag
- @param taglen [in/out] The max size and resulting size of the authentication tag
- @param direction Encrypt or Decrypt direction (0 or 1)
- @return CRYPT_OK if successful
- */
- int (*accel_ccm_memory)(const unsigned char* key,
- unsigned long keylen,
- symmetric_key* uskey,
- const unsigned char* nonce,
- unsigned long noncelen,
- const unsigned char* header,
- unsigned long headerlen,
- unsigned char* pt,
- unsigned long ptlen,
- unsigned char* ct,
- unsigned char* tag,
- unsigned long* taglen,
- int direction);
-
- /** Accelerated GCM packet (one shot)
- @param key The secret key
- @param keylen The length of the secret key
- @param IV The initial vector
- @param IVlen The length of the initial vector
- @param adata The additional authentication data (header)
- @param adatalen The length of the adata
- @param pt The plaintex
- @param ptlen The length of the plaintext (ciphertext length is the same)
- @param ct The ciphertex
- @param tag [out] The MAC tag
- @param taglen [in/out] The MAC tag length
- @param direction Encrypt or Decrypt mode (GCM_ENCRYPT or GCM_DECRYPT)
- @return CRYPT_OK on success
- */
- int (*accel_gcm_memory)(const unsigned char* key,
- unsigned long keylen,
- const unsigned char* IV,
- unsigned long IVlen,
- const unsigned char* adata,
- unsigned long adatalen,
- unsigned char* pt,
- unsigned long ptlen,
- unsigned char* ct,
- unsigned char* tag,
- unsigned long* taglen,
- int direction);
-
- /** Accelerated one shot LTC_OMAC
- @param key The secret key
- @param keylen The key length (octets)
- @param in The message
- @param inlen Length of message (octets)
- @param out [out] Destination for tag
- @param outlen [in/out] Initial and final size of ou
- @return CRYPT_OK on success
- */
- int (*omac_memory)(const unsigned char* key,
- unsigned long keylen,
- const unsigned char* in,
- unsigned long inlen,
- unsigned char* out,
- unsigned long* outlen);
-
- /** Accelerated one shot XCBC
- @param key The secret key
- @param keylen The key length (octets)
- @param in The message
- @param inlen Length of message (octets)
- @param out [out] Destination for tag
- @param outlen [in/out] Initial and final size of ou
- @return CRYPT_OK on success
- */
- int (*xcbc_memory)(const unsigned char* key,
- unsigned long keylen,
- const unsigned char* in,
- unsigned long inlen,
- unsigned char* out,
- unsigned long* outlen);
-
- /** Accelerated one shot F9
- @param key The secret key
- @param keylen The key length (octets)
- @param in The message
- @param inlen Length of message (octets)
- @param out [out] Destination for tag
- @param outlen [in/out] Initial and final size of ou
- @return CRYPT_OK on success
- @remark Requires manual padding
- */
- int (*f9_memory)(const unsigned char* key,
- unsigned long keylen,
- const unsigned char* in,
- unsigned long inlen,
- unsigned char* out,
- unsigned long* outlen);
-} cipher_descriptor[];
-
-#ifdef LTC_BLOWFISH
-int blowfish_setup(const unsigned char* key, int keylen, int num_rounds, symmetric_key* skey);
-int blowfish_ecb_encrypt(const unsigned char* pt, unsigned char* ct, symmetric_key* skey);
-int blowfish_ecb_decrypt(const unsigned char* ct, unsigned char* pt, symmetric_key* skey);
-int blowfish_test(void);
-void blowfish_done(symmetric_key* skey);
-int blowfish_keysize(int* keysize);
-extern const struct ltc_cipher_descriptor blowfish_desc;
-#endif
-
-#ifdef LTC_RC5
-int rc5_setup(const unsigned char* key, int keylen, int num_rounds, symmetric_key* skey);
-int rc5_ecb_encrypt(const unsigned char* pt, unsigned char* ct, symmetric_key* skey);
-int rc5_ecb_decrypt(const unsigned char* ct, unsigned char* pt, symmetric_key* skey);
-int rc5_test(void);
-void rc5_done(symmetric_key* skey);
-int rc5_keysize(int* keysize);
-extern const struct ltc_cipher_descriptor rc5_desc;
-#endif
-
-#ifdef LTC_RC6
-int rc6_setup(const unsigned char* key, int keylen, int num_rounds, symmetric_key* skey);
-int rc6_ecb_encrypt(const unsigned char* pt, unsigned char* ct, symmetric_key* skey);
-int rc6_ecb_decrypt(const unsigned char* ct, unsigned char* pt, symmetric_key* skey);
-int rc6_test(void);
-void rc6_done(symmetric_key* skey);
-int rc6_keysize(int* keysize);
-extern const struct ltc_cipher_descriptor rc6_desc;
-#endif
-
-#ifdef LTC_RC2
-int rc2_setup(const unsigned char* key, int keylen, int num_rounds, symmetric_key* skey);
-int rc2_ecb_encrypt(const unsigned char* pt, unsigned char* ct, symmetric_key* skey);
-int rc2_ecb_decrypt(const unsigned char* ct, unsigned char* pt, symmetric_key* skey);
-int rc2_test(void);
-void rc2_done(symmetric_key* skey);
-int rc2_keysize(int* keysize);
-extern const struct ltc_cipher_descriptor rc2_desc;
-#endif
-
-#ifdef LTC_SAFERP
-int saferp_setup(const unsigned char* key, int keylen, int num_rounds, symmetric_key* skey);
-int saferp_ecb_encrypt(const unsigned char* pt, unsigned char* ct, symmetric_key* skey);
-int saferp_ecb_decrypt(const unsigned char* ct, unsigned char* pt, symmetric_key* skey);
-int saferp_test(void);
-void saferp_done(symmetric_key* skey);
-int saferp_keysize(int* keysize);
-extern const struct ltc_cipher_descriptor saferp_desc;
-#endif
-
-#ifdef LTC_SAFER
-int safer_k64_setup(const unsigned char* key, int keylen, int num_rounds, symmetric_key* skey);
-int safer_sk64_setup(const unsigned char* key, int keylen, int num_rounds, symmetric_key* skey);
-int safer_k128_setup(const unsigned char* key, int keylen, int num_rounds, symmetric_key* skey);
-int safer_sk128_setup(const unsigned char* key, int keylen, int num_rounds, symmetric_key* skey);
-int safer_ecb_encrypt(const unsigned char* pt, unsigned char* ct, symmetric_key* key);
-int safer_ecb_decrypt(const unsigned char* ct, unsigned char* pt, symmetric_key* key);
-int safer_k64_test(void);
-int safer_sk64_test(void);
-int safer_sk128_test(void);
-void safer_done(symmetric_key* skey);
-int safer_64_keysize(int* keysize);
-int safer_128_keysize(int* keysize);
-extern const struct ltc_cipher_descriptor safer_k64_desc, safer_k128_desc, safer_sk64_desc,
- safer_sk128_desc;
-#endif
-
-#ifdef LTC_RIJNDAEL
-
-/* make aes an alias */
-#define aes_setup rijndael_setup
-#define aes_ecb_encrypt rijndael_ecb_encryp
-#define aes_ecb_decrypt rijndael_ecb_decryp
-#define aes_test rijndael_tes
-#define aes_done rijndael_done
-#define aes_keysize rijndael_keysize
-
-#define aes_enc_setup rijndael_enc_setup
-#define aes_enc_ecb_encrypt rijndael_enc_ecb_encryp
-#define aes_enc_keysize rijndael_enc_keysize
-
-int rijndael_setup(const unsigned char* key, int keylen, int num_rounds, symmetric_key* skey);
-int rijndael_ecb_encrypt(const unsigned char* pt, unsigned char* ct, symmetric_key* skey);
-int rijndael_ecb_decrypt(const unsigned char* ct, unsigned char* pt, symmetric_key* skey);
-int rijndael_test(void);
-void rijndael_done(symmetric_key* skey);
-int rijndael_keysize(int* keysize);
-int rijndael_enc_setup(const unsigned char* key, int keylen, int num_rounds, symmetric_key* skey);
-int rijndael_enc_ecb_encrypt(const unsigned char* pt, unsigned char* ct, symmetric_key* skey);
-void rijndael_enc_done(symmetric_key* skey);
-int rijndael_enc_keysize(int* keysize);
-extern const struct ltc_cipher_descriptor rijndael_desc, aes_desc;
-extern const struct ltc_cipher_descriptor rijndael_enc_desc, aes_enc_desc;
-#endif
-
-#ifdef LTC_XTEA
-int xtea_setup(const unsigned char* key, int keylen, int num_rounds, symmetric_key* skey);
-int xtea_ecb_encrypt(const unsigned char* pt, unsigned char* ct, symmetric_key* skey);
-int xtea_ecb_decrypt(const unsigned char* ct, unsigned char* pt, symmetric_key* skey);
-int xtea_test(void);
-void xtea_done(symmetric_key* skey);
-int xtea_keysize(int* keysize);
-extern const struct ltc_cipher_descriptor xtea_desc;
-#endif
-
-#ifdef LTC_TWOFISH
-int twofish_setup(const unsigned char* key, int keylen, int num_rounds, symmetric_key* skey);
-int twofish_ecb_encrypt(const unsigned char* pt, unsigned char* ct, symmetric_key* skey);
-int twofish_ecb_decrypt(const unsigned char* ct, unsigned char* pt, symmetric_key* skey);
-int twofish_test(void);
-void twofish_done(symmetric_key* skey);
-int twofish_keysize(int* keysize);
-extern const struct ltc_cipher_descriptor twofish_desc;
-#endif
-
-#ifdef LTC_DES
-int des_setup(const unsigned char* key, int keylen, int num_rounds, symmetric_key* skey);
-int des_ecb_encrypt(const unsigned char* pt, unsigned char* ct, symmetric_key* skey);
-int des_ecb_decrypt(const unsigned char* ct, unsigned char* pt, symmetric_key* skey);
-int des_test(void);
-void des_done(symmetric_key* skey);
-int des_keysize(int* keysize);
-int des3_setup(const unsigned char* key, int keylen, int num_rounds, symmetric_key* skey);
-int des3_ecb_encrypt(const unsigned char* pt, unsigned char* ct, symmetric_key* skey);
-int des3_ecb_decrypt(const unsigned char* ct, unsigned char* pt, symmetric_key* skey);
-int des3_test(void);
-void des3_done(symmetric_key* skey);
-int des3_keysize(int* keysize);
-extern const struct ltc_cipher_descriptor des_desc, des3_desc;
-#endif
-
-#ifdef LTC_CAST5
-int cast5_setup(const unsigned char* key, int keylen, int num_rounds, symmetric_key* skey);
-int cast5_ecb_encrypt(const unsigned char* pt, unsigned char* ct, symmetric_key* skey);
-int cast5_ecb_decrypt(const unsigned char* ct, unsigned char* pt, symmetric_key* skey);
-int cast5_test(void);
-void cast5_done(symmetric_key* skey);
-int cast5_keysize(int* keysize);
-extern const struct ltc_cipher_descriptor cast5_desc;
-#endif
-
-#ifdef LTC_NOEKEON
-int noekeon_setup(const unsigned char* key, int keylen, int num_rounds, symmetric_key* skey);
-int noekeon_ecb_encrypt(const unsigned char* pt, unsigned char* ct, symmetric_key* skey);
-int noekeon_ecb_decrypt(const unsigned char* ct, unsigned char* pt, symmetric_key* skey);
-int noekeon_test(void);
-void noekeon_done(symmetric_key* skey);
-int noekeon_keysize(int* keysize);
-extern const struct ltc_cipher_descriptor noekeon_desc;
-#endif
-
-#ifdef LTC_SKIPJACK
-int skipjack_setup(const unsigned char* key, int keylen, int num_rounds, symmetric_key* skey);
-int skipjack_ecb_encrypt(const unsigned char* pt, unsigned char* ct, symmetric_key* skey);
-int skipjack_ecb_decrypt(const unsigned char* ct, unsigned char* pt, symmetric_key* skey);
-int skipjack_test(void);
-void skipjack_done(symmetric_key* skey);
-int skipjack_keysize(int* keysize);
-extern const struct ltc_cipher_descriptor skipjack_desc;
-#endif
-
-#ifdef LTC_KHAZAD
-int khazad_setup(const unsigned char* key, int keylen, int num_rounds, symmetric_key* skey);
-int khazad_ecb_encrypt(const unsigned char* pt, unsigned char* ct, symmetric_key* skey);
-int khazad_ecb_decrypt(const unsigned char* ct, unsigned char* pt, symmetric_key* skey);
-int khazad_test(void);
-void khazad_done(symmetric_key* skey);
-int khazad_keysize(int* keysize);
-extern const struct ltc_cipher_descriptor khazad_desc;
-#endif
-
-#ifdef LTC_ANUBIS
-int anubis_setup(const unsigned char* key, int keylen, int num_rounds, symmetric_key* skey);
-int anubis_ecb_encrypt(const unsigned char* pt, unsigned char* ct, symmetric_key* skey);
-int anubis_ecb_decrypt(const unsigned char* ct, unsigned char* pt, symmetric_key* skey);
-int anubis_test(void);
-void anubis_done(symmetric_key* skey);
-int anubis_keysize(int* keysize);
-extern const struct ltc_cipher_descriptor anubis_desc;
-#endif
-
-#ifdef LTC_KSEED
-int kseed_setup(const unsigned char* key, int keylen, int num_rounds, symmetric_key* skey);
-int kseed_ecb_encrypt(const unsigned char* pt, unsigned char* ct, symmetric_key* skey);
-int kseed_ecb_decrypt(const unsigned char* ct, unsigned char* pt, symmetric_key* skey);
-int kseed_test(void);
-void kseed_done(symmetric_key* skey);
-int kseed_keysize(int* keysize);
-extern const struct ltc_cipher_descriptor kseed_desc;
-#endif
-
-#ifdef LTC_KASUMI
-int kasumi_setup(const unsigned char* key, int keylen, int num_rounds, symmetric_key* skey);
-int kasumi_ecb_encrypt(const unsigned char* pt, unsigned char* ct, symmetric_key* skey);
-int kasumi_ecb_decrypt(const unsigned char* ct, unsigned char* pt, symmetric_key* skey);
-int kasumi_test(void);
-void kasumi_done(symmetric_key* skey);
-int kasumi_keysize(int* keysize);
-extern const struct ltc_cipher_descriptor kasumi_desc;
-#endif
-
-
-#ifdef LTC_MULTI2
-int multi2_setup(const unsigned char* key, int keylen, int num_rounds, symmetric_key* skey);
-int multi2_ecb_encrypt(const unsigned char* pt, unsigned char* ct, symmetric_key* skey);
-int multi2_ecb_decrypt(const unsigned char* ct, unsigned char* pt, symmetric_key* skey);
-int multi2_test(void);
-void multi2_done(symmetric_key* skey);
-int multi2_keysize(int* keysize);
-extern const struct ltc_cipher_descriptor multi2_desc;
-#endif
-
-#ifdef LTC_ECB_MODE
-int ecb_start(int cipher, const unsigned char* key, int keylen, int num_rounds, symmetric_ECB* ecb);
-int ecb_encrypt(const unsigned char* pt, unsigned char* ct, unsigned long len, symmetric_ECB* ecb);
-int ecb_decrypt(const unsigned char* ct, unsigned char* pt, unsigned long len, symmetric_ECB* ecb);
-int ecb_done(symmetric_ECB* ecb);
-#endif
-
-#ifdef LTC_CFB_MODE
-int cfb_start(int cipher,
- const unsigned char* IV,
- const unsigned char* key,
- int keylen,
- int num_rounds,
- symmetric_CFB* cfb);
-int cfb_encrypt(const unsigned char* pt, unsigned char* ct, unsigned long len, symmetric_CFB* cfb);
-int cfb_decrypt(const unsigned char* ct, unsigned char* pt, unsigned long len, symmetric_CFB* cfb);
-int cfb_getiv(unsigned char* IV, unsigned long* len, symmetric_CFB* cfb);
-int cfb_setiv(const unsigned char* IV, unsigned long len, symmetric_CFB* cfb);
-int cfb_done(symmetric_CFB* cfb);
-#endif
-
-#ifdef LTC_OFB_MODE
-int ofb_start(int cipher,
- const unsigned char* IV,
- const unsigned char* key,
- int keylen,
- int num_rounds,
- symmetric_OFB* ofb);
-int ofb_encrypt(const unsigned char* pt, unsigned char* ct, unsigned long len, symmetric_OFB* ofb);
-int ofb_decrypt(const unsigned char* ct, unsigned char* pt, unsigned long len, symmetric_OFB* ofb);
-int ofb_getiv(unsigned char* IV, unsigned long* len, symmetric_OFB* ofb);
-int ofb_setiv(const unsigned char* IV, unsigned long len, symmetric_OFB* ofb);
-int ofb_done(symmetric_OFB* ofb);
-#endif
-
-#ifdef LTC_CBC_MODE
-int cbc_start(int cipher,
- const unsigned char* IV,
- const unsigned char* key,
- int keylen,
- int num_rounds,
- symmetric_CBC* cbc);
-int cbc_encrypt(const unsigned char* pt, unsigned char* ct, unsigned long len, symmetric_CBC* cbc);
-int cbc_decrypt(const unsigned char* ct, unsigned char* pt, unsigned long len, symmetric_CBC* cbc);
-int cbc_getiv(unsigned char* IV, unsigned long* len, symmetric_CBC* cbc);
-int cbc_setiv(const unsigned char* IV, unsigned long len, symmetric_CBC* cbc);
-int cbc_done(symmetric_CBC* cbc);
-#endif
-
-#ifdef LTC_CTR_MODE
-
-#define CTR_COUNTER_LITTLE_ENDIAN 0x0000
-#define CTR_COUNTER_BIG_ENDIAN 0x1000
-#define LTC_CTR_RFC3686 0x2000
-
-int ctr_start(int cipher,
- const unsigned char* IV,
- const unsigned char* key,
- int keylen,
- int num_rounds,
- int ctr_mode,
- symmetric_CTR* ctr);
-int ctr_encrypt(const unsigned char* pt, unsigned char* ct, unsigned long len, symmetric_CTR* ctr);
-int ctr_decrypt(const unsigned char* ct, unsigned char* pt, unsigned long len, symmetric_CTR* ctr);
-int ctr_getiv(unsigned char* IV, unsigned long* len, symmetric_CTR* ctr);
-int ctr_setiv(const unsigned char* IV, unsigned long len, symmetric_CTR* ctr);
-int ctr_done(symmetric_CTR* ctr);
-int ctr_test(void);
-#endif
-
-#ifdef LTC_LRW_MODE
-
-#define LRW_ENCRYPT 0
-#define LRW_DECRYPT 1
-
-int lrw_start(int cipher,
- const unsigned char* IV,
- const unsigned char* key,
- int keylen,
- const unsigned char* tweak,
- int num_rounds,
- symmetric_LRW* lrw);
-int lrw_encrypt(const unsigned char* pt, unsigned char* ct, unsigned long len, symmetric_LRW* lrw);
-int lrw_decrypt(const unsigned char* ct, unsigned char* pt, unsigned long len, symmetric_LRW* lrw);
-int lrw_getiv(unsigned char* IV, unsigned long* len, symmetric_LRW* lrw);
-int lrw_setiv(const unsigned char* IV, unsigned long len, symmetric_LRW* lrw);
-int lrw_done(symmetric_LRW* lrw);
-int lrw_test(void);
-
-/* don't call */
-int lrw_process(
- const unsigned char* pt, unsigned char* ct, unsigned long len, int mode, symmetric_LRW* lrw);
-#endif
-
-#ifdef LTC_F8_MODE
-int f8_start(int cipher,
- const unsigned char* IV,
- const unsigned char* key,
- int keylen,
- const unsigned char* salt_key,
- int skeylen,
- int num_rounds,
- symmetric_F8* f8);
-int f8_encrypt(const unsigned char* pt, unsigned char* ct, unsigned long len, symmetric_F8* f8);
-int f8_decrypt(const unsigned char* ct, unsigned char* pt, unsigned long len, symmetric_F8* f8);
-int f8_getiv(unsigned char* IV, unsigned long* len, symmetric_F8* f8);
-int f8_setiv(const unsigned char* IV, unsigned long len, symmetric_F8* f8);
-int f8_done(symmetric_F8* f8);
-int f8_test_mode(void);
-#endif
-
-#ifdef LTC_XTS_MODE
-typedef struct {
- symmetric_key key1, key2;
- int cipher;
-} symmetric_xts;
-
-int xts_start(int cipher,
- const unsigned char* key1,
- const unsigned char* key2,
- unsigned long keylen,
- int num_rounds,
- symmetric_xts* xts);
-
-int xts_encrypt(const unsigned char* pt,
- unsigned long ptlen,
- unsigned char* ct,
- const unsigned char* tweak,
- symmetric_xts* xts);
-int xts_decrypt(const unsigned char* ct,
- unsigned long ptlen,
- unsigned char* pt,
- const unsigned char* tweak,
- symmetric_xts* xts);
-
-void xts_done(symmetric_xts* xts);
-int xts_test(void);
-void xts_mult_x(unsigned char* I);
-#endif
-
-int find_cipher(const char* name);
-int find_cipher_any(const char* name, int blocklen, int keylen);
-int find_cipher_id(unsigned char ID);
-int register_cipher(const struct ltc_cipher_descriptor* cipher);
-int unregister_cipher(const struct ltc_cipher_descriptor* cipher);
-int cipher_is_valid(int idx);
-
-LTC_MUTEX_PROTO(ltc_cipher_mutex)
-
-/* $Source: /cvs/libtom/libtomcrypt/src/headers/tomcrypt_cipher.h,v $ */
-/* $Revision: 1.54 $ */
-/* $Date: 2007/05/12 14:37:41 $ */
diff --git a/src/mongo/crypto/tom/tomcrypt_custom.h b/src/mongo/crypto/tom/tomcrypt_custom.h
deleted file mode 100644
index 9d64e630010..00000000000
--- a/src/mongo/crypto/tom/tomcrypt_custom.h
+++ /dev/null
@@ -1,422 +0,0 @@
-/* Copyright 2014 MongoDB Inc.
- *
- * 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
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#ifndef TOMCRYPT_CUSTOM_H_
-#define TOMCRYPT_CUSTOM_H_
-
-/* macros for various libc functions you can change for embedded targets */
-#ifndef XMALLOC
-#ifdef malloc
-#define LTC_NO_PROTOTYPES
-#endif
-#define XMALLOC malloc
-#endif
-#ifndef XREALLOC
-#ifdef realloc
-#define LTC_NO_PROTOTYPES
-#endif
-#define XREALLOC realloc
-#endif
-#ifndef XCALLOC
-#ifdef calloc
-#define LTC_NO_PROTOTYPES
-#endif
-#define XCALLOC calloc
-#endif
-#ifndef XFREE
-#ifdef free
-#define LTC_NO_PROTOTYPES
-#endif
-#define XFREE free
-#endif
-
-#ifndef XMEMSET
-#ifdef memse
-#define LTC_NO_PROTOTYPES
-#endif
-#define XMEMSET memse
-#endif
-#ifndef XMEMCPY
-#ifdef memcpy
-#define LTC_NO_PROTOTYPES
-#endif
-#define XMEMCPY memcpy
-#endif
-#ifndef XMEMCMP
-#ifdef memcmp
-#define LTC_NO_PROTOTYPES
-#endif
-#define XMEMCMP memcmp
-#endif
-#ifndef XSTRCMP
-#ifdef strcmp
-#define LTC_NO_PROTOTYPES
-#endif
-#define XSTRCMP strcmp
-#endif
-
-#ifndef XCLOCK
-#define XCLOCK clock
-#endif
-#ifndef XCLOCKS_PER_SEC
-#define XCLOCKS_PER_SEC CLOCKS_PER_SEC
-#endif
-
-#ifndef XQSORT
-#ifdef qsor
-#define LTC_NO_PROTOTYPES
-#endif
-#define XQSORT qsor
-#endif
-
-/* Easy button? */
-#ifdef LTC_EASY
-#define LTC_NO_CIPHERS
-#define LTC_RIJNDAEL
-#define LTC_BLOWFISH
-#define LTC_DES
-#define LTC_CAST5
-
-#define LTC_NO_MODES
-#define LTC_ECB_MODE
-#define LTC_CBC_MODE
-#define LTC_CTR_MODE
-
-#define LTC_NO_HASHES
-#define LTC_SHA1
-#define LTC_SHA512
-#define LTC_SHA384
-#define LTC_SHA256
-#define LTC_SHA224
-
-#define LTC_NO_MACS
-#define LTC_HMAC
-#define LTC_OMAC
-#define LTC_CCM_MODE
-
-#define LTC_NO_PRNGS
-#define LTC_SPRNG
-#define LTC_YARROW
-#define LTC_DEVRANDOM
-#define TRY_URANDOM_FIRST
-
-#define LTC_NO_PK
-#define LTC_MRSA
-#define LTC_MECC
-#endif
-
-/* Use small code where possible */
-/* #define LTC_SMALL_CODE */
-
-/* Enable self-test test vector checking */
-#ifndef LTC_NO_TEST
-#define LTC_TEST
-#endif
-
-/* clean the stack of functions which put private information on stack */
-/* #define LTC_CLEAN_STACK */
-
-/* disable all file related functions */
-/* #define LTC_NO_FILE */
-
-/* disable all forms of ASM */
-#define LTC_NO_ASM
-
-/* disable FAST mode */
-/* #define LTC_NO_FAST */
-
-/* disable BSWAP on x86 */
-/* #define LTC_NO_BSWAP */
-
-/* ---> Symmetric Block Ciphers <--- */
-#ifndef LTC_NO_CIPHERS
-
-#define LTC_BLOWFISH
-#define LTC_RC2
-#define LTC_RC5
-#define LTC_RC6
-#define LTC_SAFERP
-#define LTC_RIJNDAEL
-#define LTC_XTEA
-/* _TABLES tells it to use tables during setup, _SMALL means to use the smaller scheduled key forma
- * (saves 4KB of ram), _ALL_TABLES enables all tables during setup */
-#define LTC_TWOFISH
-#ifndef LTC_NO_TABLES
-#define LTC_TWOFISH_TABLES
-/* #define LTC_TWOFISH_ALL_TABLES */
-#else
-#define LTC_TWOFISH_SMALL
-#endif
-/* #define LTC_TWOFISH_SMALL */
-/* LTC_DES includes EDE triple-LTC_DES */
-#define LTC_DES
-#define LTC_CAST5
-#define LTC_NOEKEON
-#define LTC_SKIPJACK
-#define LTC_SAFER
-#define LTC_KHAZAD
-#define LTC_ANUBIS
-#define LTC_ANUBIS_TWEAK
-#define LTC_KSEED
-#define LTC_KASUMI
-
-#endif /* LTC_NO_CIPHERS */
-
-
-/* ---> Block Cipher Modes of Operation <--- */
-#ifndef LTC_NO_MODES
-
-#define LTC_CFB_MODE
-#define LTC_OFB_MODE
-#define LTC_ECB_MODE
-#define LTC_CBC_MODE
-#define LTC_CTR_MODE
-
-/* F8 chaining mode */
-#define LTC_F8_MODE
-
-/* LRW mode */
-#define LTC_LRW_MODE
-#ifndef LTC_NO_TABLES
-/* like GCM mode this will enable 16 8x128 tables [64KB] that make
- * seeking very fast.
- */
-#define LRW_TABLES
-#endif
-
-/* XTS mode */
-#define LTC_XTS_MODE
-
-#endif /* LTC_NO_MODES */
-
-/* ---> One-Way Hash Functions <--- */
-#ifndef LTC_NO_HASHES
-
-//#define LTC_CHC_HASH
-//#define LTC_WHIRLPOOL
-//#define LTC_SHA512
-//#define LTC_SHA384
-//#define LTC_SHA256
-//#define LTC_SHA224
-//#define LTC_TIGER
-#define LTC_SHA1
-//#define LTC_MD5
-//#define LTC_MD4
-//#define LTC_MD2
-//#define LTC_RIPEMD128
-//#define LTC_RIPEMD160
-//#define LTC_RIPEMD256
-//#define LTC_RIPEMD320
-
-#endif /* LTC_NO_HASHES */
-
-/* ---> MAC functions <--- */
-#ifndef LTC_NO_MACS
-
-#define LTC_HMAC
-#define LTC_OMAC
-//#define LTC_PMAC
-//#define LTC_XCBC
-//#define LTC_F9_MODE
-//#define LTC_PELICAN
-
-#if defined(LTC_PELICAN) && !defined(LTC_RIJNDAEL)
-#error Pelican-MAC requires LTC_RIJNDAEL
-#endif
-
-/* ---> Encrypt + Authenticate Modes <--- */
-
-#define LTC_EAX_MODE
-#if defined(LTC_EAX_MODE) && !(defined(LTC_CTR_MODE) && defined(LTC_OMAC))
-#error LTC_EAX_MODE requires CTR and LTC_OMAC mode
-#endif
-
-#define LTC_OCB_MODE
-#define LTC_CCM_MODE
-#define LTC_GCM_MODE
-
-/* Use 64KiB tables */
-#ifndef LTC_NO_TABLES
-#define LTC_GCM_TABLES
-#endif
-
-/* USE SSE2? requires GCC works on x86_32 and x86_64*/
-#ifdef LTC_GCM_TABLES
-/* #define LTC_GCM_TABLES_SSE2 */
-#endif
-
-#endif /* LTC_NO_MACS */
-
-/* Various tidbits of modern neatoness */
-#define LTC_BASE64
-
-/* --> Pseudo Random Number Generators <--- */
-#ifndef LTC_NO_PRNGS
-
-/* Yarrow */
-#define LTC_YARROW
-/* which descriptor of AES to use? */
-/* 0 = rijndael_enc 1 = aes_enc, 2 = rijndael [full], 3 = aes [full] */
-#define LTC_YARROW_AES 0
-
-#if defined(LTC_YARROW) && !defined(LTC_CTR_MODE)
-#error LTC_YARROW requires LTC_CTR_MODE chaining mode to be defined!
-#endif
-
-/* a PRNG that simply reads from an available system source */
-#define LTC_SPRNG
-
-/* The LTC_RC4 stream cipher */
-#define LTC_RC4
-
-/* Fortuna PRNG */
-#define LTC_FORTUNA
-/* reseed every N calls to the read function */
-#define LTC_FORTUNA_WD 10
-/* number of pools (4..32) can save a bit of ram by lowering the count */
-#define LTC_FORTUNA_POOLS 32
-
-/* Greg's LTC_SOBER128 PRNG ;-0 */
-#define LTC_SOBER128
-
-/* the *nix style /dev/random device */
-#define LTC_DEVRANDOM
-/* try /dev/urandom before trying /dev/random */
-#define TRY_URANDOM_FIRST
-
-#endif /* LTC_NO_PRNGS */
-
-/* ---> math provider? <--- */
-#ifndef LTC_NO_MATH
-
-/* LibTomMath */
-/* #define LTM_LTC_DESC */
-
-/* TomsFastMath */
-/* #define TFM_LTC_DESC */
-
-#endif /* LTC_NO_MATH */
-
-/* ---> Public Key Crypto <--- */
-#ifndef LTC_NO_PK
-
-/* Include RSA support */
-#define LTC_MRSA
-
-/* Include Katja (a Rabin variant like RSA) */
-/* #define MKAT */
-
-/* Digital Signature Algorithm */
-#define LTC_MDSA
-
-/* ECC */
-#define LTC_MECC
-
-/* use Shamir's trick for point mul (speeds up signature verification) */
-#define LTC_ECC_SHAMIR
-
-#if defined(TFM_LTC_DESC) && defined(LTC_MECC)
-#define LTC_MECC_ACCEL
-#endif
-
-/* do we want fixed point ECC */
-/* #define LTC_MECC_FP */
-
-/* Timing Resistant? */
-/* #define LTC_ECC_TIMING_RESISTANT */
-
-#endif /* LTC_NO_PK */
-
-/* LTC_PKCS #1 (RSA) and #5 (Password Handling) stuff */
-#ifndef LTC_NO_PKCS
-
-#define LTC_PKCS_1
-#define LTC_PKCS_5
-
-/* Include ASN.1 DER (required by DSA/RSA) */
-#define LTC_DER
-
-#endif /* LTC_NO_PKCS */
-
-/* cleanup */
-
-#ifdef LTC_MECC
-/* Supported ECC Key Sizes */
-#ifndef LTC_NO_CURVES
-#define ECC112
-#define ECC128
-#define ECC160
-#define ECC192
-#define ECC224
-#define ECC256
-#define ECC384
-#define ECC521
-#endif
-#endif
-
-#if defined(LTC_MECC) || defined(LTC_MRSA) || defined(LTC_MDSA) || defined(MKATJA)
-/* Include the MPI functionality? (required by the PK algorithms) */
-#define MPI
-#endif
-
-#ifdef LTC_MRSA
-#define LTC_PKCS_1
-#endif
-
-#if defined(LTC_DER) && !defined(MPI)
-#error ASN.1 DER requires MPI functionality
-#endif
-
-#if (defined(LTC_MDSA) || defined(LTC_MRSA) || defined(LTC_MECC) || defined(MKATJA)) && \
- !defined(LTC_DER)
-#error PK requires ASN.1 DER functionality, make sure LTC_DER is enabled
-#endif
-
-/* THREAD management */
-#ifdef LTC_PTHREAD
-
-#include <pthread.h>
-
-#define LTC_MUTEX_GLOBAL(x) pthread_mutex_t x = PTHREAD_MUTEX_INITIALIZER;
-#define LTC_MUTEX_PROTO(x) extern pthread_mutex_t x;
-#define LTC_MUTEX_TYPE(x) pthread_mutex_t x;
-#define LTC_MUTEX_INIT(x) pthread_mutex_init(x, NULL);
-#define LTC_MUTEX_LOCK(x) pthread_mutex_lock(x);
-#define LTC_MUTEX_UNLOCK(x) pthread_mutex_unlock(x);
-
-#else
-
-/* default no functions */
-#define LTC_MUTEX_GLOBAL(x)
-#define LTC_MUTEX_PROTO(x)
-#define LTC_MUTEX_TYPE(x)
-#define LTC_MUTEX_INIT(x)
-#define LTC_MUTEX_LOCK(x)
-#define LTC_MUTEX_UNLOCK(x)
-
-#endif
-
-/* Debuggers */
-
-/* define this if you use Valgrind, note: it CHANGES the way SOBER-128 and LTC_RC4 work (see the
- * code) */
-/* #define LTC_VALGRIND */
-
-#endif
-
-
-/* $Source: /cvs/libtom/libtomcrypt/src/headers/tomcrypt_custom.h,v $ */
-/* $Revision: 1.73 $ */
-/* $Date: 2007/05/12 14:37:41 $ */
diff --git a/src/mongo/crypto/tom/tomcrypt_hash.h b/src/mongo/crypto/tom/tomcrypt_hash.h
deleted file mode 100644
index db3cd46c152..00000000000
--- a/src/mongo/crypto/tom/tomcrypt_hash.h
+++ /dev/null
@@ -1,403 +0,0 @@
-/* Copyright 2014 MongoDB Inc.
- *
- * 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
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-/* ---- HASH FUNCTIONS ---- */
-#ifdef LTC_SHA512
-struct sha512_state {
- ulong64 length, state[8];
- unsigned long curlen;
- unsigned char buf[128];
-};
-#endif
-
-#ifdef LTC_SHA256
-struct sha256_state {
- ulong64 length;
- ulong32 state[8], curlen;
- unsigned char buf[64];
-};
-#endif
-
-#ifdef LTC_SHA1
-struct sha1_state {
- ulong64 length;
- ulong32 state[5], curlen;
- unsigned char buf[64];
-};
-#endif
-
-#ifdef LTC_MD5
-struct md5_state {
- ulong64 length;
- ulong32 state[4], curlen;
- unsigned char buf[64];
-};
-#endif
-
-#ifdef LTC_MD4
-struct md4_state {
- ulong64 length;
- ulong32 state[4], curlen;
- unsigned char buf[64];
-};
-#endif
-
-#ifdef LTC_TIGER
-struct tiger_state {
- ulong64 state[3], length;
- unsigned long curlen;
- unsigned char buf[64];
-};
-#endif
-
-#ifdef LTC_MD2
-struct md2_state {
- unsigned char chksum[16], X[48], buf[16];
- unsigned long curlen;
-};
-#endif
-
-#ifdef LTC_RIPEMD128
-struct rmd128_state {
- ulong64 length;
- unsigned char buf[64];
- ulong32 curlen, state[4];
-};
-#endif
-
-#ifdef LTC_RIPEMD160
-struct rmd160_state {
- ulong64 length;
- unsigned char buf[64];
- ulong32 curlen, state[5];
-};
-#endif
-
-#ifdef LTC_RIPEMD256
-struct rmd256_state {
- ulong64 length;
- unsigned char buf[64];
- ulong32 curlen, state[8];
-};
-#endif
-
-#ifdef LTC_RIPEMD320
-struct rmd320_state {
- ulong64 length;
- unsigned char buf[64];
- ulong32 curlen, state[10];
-};
-#endif
-
-#ifdef LTC_WHIRLPOOL
-struct whirlpool_state {
- ulong64 length, state[8];
- unsigned char buf[64];
- ulong32 curlen;
-};
-#endif
-
-#ifdef LTC_CHC_HASH
-struct chc_state {
- ulong64 length;
- unsigned char state[MAXBLOCKSIZE], buf[MAXBLOCKSIZE];
- ulong32 curlen;
-};
-#endif
-
-typedef union Hash_state {
- char dummy[1];
-#ifdef LTC_CHC_HASH
- struct chc_state chc;
-#endif
-#ifdef LTC_WHIRLPOOL
- struct whirlpool_state whirlpool;
-#endif
-#ifdef LTC_SHA512
- struct sha512_state sha512;
-#endif
-#ifdef LTC_SHA256
- struct sha256_state sha256;
-#endif
-#ifdef LTC_SHA1
- struct sha1_state sha1;
-#endif
-#ifdef LTC_MD5
- struct md5_state md5;
-#endif
-#ifdef LTC_MD4
- struct md4_state md4;
-#endif
-#ifdef LTC_MD2
- struct md2_state md2;
-#endif
-#ifdef LTC_TIGER
- struct tiger_state tiger;
-#endif
-#ifdef LTC_RIPEMD128
- struct rmd128_state rmd128;
-#endif
-#ifdef LTC_RIPEMD160
- struct rmd160_state rmd160;
-#endif
-#ifdef LTC_RIPEMD256
- struct rmd256_state rmd256;
-#endif
-#ifdef LTC_RIPEMD320
- struct rmd320_state rmd320;
-#endif
- void* data;
-} hash_state;
-
-/** hash descriptor */
-extern struct ltc_hash_descriptor {
- /** name of hash */
- char* name;
- /** internal ID */
- unsigned char ID;
- /** Size of digest in octets */
- unsigned long hashsize;
- /** Input block size in octets */
- unsigned long blocksize;
- /** ASN.1 OID */
- unsigned long OID[16];
- /** Length of DER encoding */
- unsigned long OIDlen;
-
- /** Init a hash state
- @param hash The hash to initialize
- @return CRYPT_OK if successful
- */
- int (*init)(hash_state* hash);
- /** Process a block of data
- @param hash The hash state
- @param in The data to hash
- @param inlen The length of the data (octets)
- @return CRYPT_OK if successful
- */
- int (*process)(hash_state* hash, const unsigned char* in, unsigned long inlen);
- /** Produce the digest and store it
- @param hash The hash state
- @param out [out] The destination of the digest
- @return CRYPT_OK if successful
- */
- int (*done)(hash_state* hash, unsigned char* out);
- /** Self-test
- @return CRYPT_OK if successful, CRYPT_NOP if self-tests have been disabled
- */
- int (*test)(void);
-
- /* accelerated hmac callback: if you need to-do multiple packets just use the generic
- * hmac_memory and provide a hash callback */
- int (*hmac_block)(const unsigned char* key,
- unsigned long keylen,
- const unsigned char* in,
- unsigned long inlen,
- unsigned char* out,
- unsigned long* outlen);
-
-} hash_descriptor[];
-
-#ifdef LTC_CHC_HASH
-int chc_register(int cipher);
-int chc_init(hash_state* md);
-int chc_process(hash_state* md, const unsigned char* in, unsigned long inlen);
-int chc_done(hash_state* md, unsigned char* hash);
-int chc_test(void);
-extern const struct ltc_hash_descriptor chc_desc;
-#endif
-
-#ifdef LTC_WHIRLPOOL
-int whirlpool_init(hash_state* md);
-int whirlpool_process(hash_state* md, const unsigned char* in, unsigned long inlen);
-int whirlpool_done(hash_state* md, unsigned char* hash);
-int whirlpool_test(void);
-extern const struct ltc_hash_descriptor whirlpool_desc;
-#endif
-
-#ifdef LTC_SHA512
-int sha512_init(hash_state* md);
-int sha512_process(hash_state* md, const unsigned char* in, unsigned long inlen);
-int sha512_done(hash_state* md, unsigned char* hash);
-int sha512_test(void);
-extern const struct ltc_hash_descriptor sha512_desc;
-#endif
-
-#ifdef LTC_SHA384
-#ifndef LTC_SHA512
-#error LTC_SHA512 is required for LTC_SHA384
-#endif
-int sha384_init(hash_state* md);
-#define sha384_process sha512_process
-int sha384_done(hash_state* md, unsigned char* hash);
-int sha384_test(void);
-extern const struct ltc_hash_descriptor sha384_desc;
-#endif
-
-#ifdef LTC_SHA256
-int sha256_init(hash_state* md);
-int sha256_process(hash_state* md, const unsigned char* in, unsigned long inlen);
-int sha256_done(hash_state* md, unsigned char* hash);
-int sha256_test(void);
-extern const struct ltc_hash_descriptor sha256_desc;
-
-#ifdef LTC_SHA224
-#ifndef LTC_SHA256
-#error LTC_SHA256 is required for LTC_SHA224
-#endif
-int sha224_init(hash_state* md);
-#define sha224_process sha256_process
-int sha224_done(hash_state* md, unsigned char* hash);
-int sha224_test(void);
-extern const struct ltc_hash_descriptor sha224_desc;
-#endif
-#endif
-
-#ifdef LTC_SHA1
-int sha1_init(hash_state* md);
-int sha1_process(hash_state* md, const unsigned char* in, unsigned long inlen);
-int sha1_done(hash_state* md, unsigned char* hash);
-int sha1_test(void);
-extern const struct ltc_hash_descriptor sha1_desc;
-#endif
-
-#ifdef LTC_MD5
-int md5_init(hash_state* md);
-int md5_process(hash_state* md, const unsigned char* in, unsigned long inlen);
-int md5_done(hash_state* md, unsigned char* hash);
-int md5_test(void);
-extern const struct ltc_hash_descriptor md5_desc;
-#endif
-
-#ifdef LTC_MD4
-int md4_init(hash_state* md);
-int md4_process(hash_state* md, const unsigned char* in, unsigned long inlen);
-int md4_done(hash_state* md, unsigned char* hash);
-int md4_test(void);
-extern const struct ltc_hash_descriptor md4_desc;
-#endif
-
-#ifdef LTC_MD2
-int md2_init(hash_state* md);
-int md2_process(hash_state* md, const unsigned char* in, unsigned long inlen);
-int md2_done(hash_state* md, unsigned char* hash);
-int md2_test(void);
-extern const struct ltc_hash_descriptor md2_desc;
-#endif
-
-#ifdef LTC_TIGER
-int tiger_init(hash_state* md);
-int tiger_process(hash_state* md, const unsigned char* in, unsigned long inlen);
-int tiger_done(hash_state* md, unsigned char* hash);
-int tiger_test(void);
-extern const struct ltc_hash_descriptor tiger_desc;
-#endif
-
-#ifdef LTC_RIPEMD128
-int rmd128_init(hash_state* md);
-int rmd128_process(hash_state* md, const unsigned char* in, unsigned long inlen);
-int rmd128_done(hash_state* md, unsigned char* hash);
-int rmd128_test(void);
-extern const struct ltc_hash_descriptor rmd128_desc;
-#endif
-
-#ifdef LTC_RIPEMD160
-int rmd160_init(hash_state* md);
-int rmd160_process(hash_state* md, const unsigned char* in, unsigned long inlen);
-int rmd160_done(hash_state* md, unsigned char* hash);
-int rmd160_test(void);
-extern const struct ltc_hash_descriptor rmd160_desc;
-#endif
-
-#ifdef LTC_RIPEMD256
-int rmd256_init(hash_state* md);
-int rmd256_process(hash_state* md, const unsigned char* in, unsigned long inlen);
-int rmd256_done(hash_state* md, unsigned char* hash);
-int rmd256_test(void);
-extern const struct ltc_hash_descriptor rmd256_desc;
-#endif
-
-#ifdef LTC_RIPEMD320
-int rmd320_init(hash_state* md);
-int rmd320_process(hash_state* md, const unsigned char* in, unsigned long inlen);
-int rmd320_done(hash_state* md, unsigned char* hash);
-int rmd320_test(void);
-extern const struct ltc_hash_descriptor rmd320_desc;
-#endif
-
-
-int find_hash(const char* name);
-int find_hash_id(unsigned char ID);
-int find_hash_oid(const unsigned long* ID, unsigned long IDlen);
-int find_hash_any(const char* name, int digestlen);
-int register_hash(const struct ltc_hash_descriptor* hash);
-int unregister_hash(const struct ltc_hash_descriptor* hash);
-int hash_is_valid(int idx);
-
-LTC_MUTEX_PROTO(ltc_hash_mutex)
-
-int hash_memory(int hash,
- const unsigned char* in,
- unsigned long inlen,
- unsigned char* out,
- unsigned long* outlen);
-int hash_memory_multi(int hash,
- unsigned char* out,
- unsigned long* outlen,
- const unsigned char* in,
- unsigned long inlen,
- ...);
-int hash_filehandle(int hash, FILE* in, unsigned char* out, unsigned long* outlen);
-int hash_file(int hash, const char* fname, unsigned char* out, unsigned long* outlen);
-
-/* a simple macro for making hash "process" functions */
-#define HASH_PROCESS(func_name, compress_name, state_var, block_size) \
- int func_name(hash_state* md, const unsigned char* in, unsigned long inlen) { \
- unsigned long n; \
- int err; \
- LTC_ARGCHK(md != NULL); \
- LTC_ARGCHK(in != NULL); \
- if (md->state_var.curlen > sizeof(md->state_var.buf)) { \
- return CRYPT_INVALID_ARG; \
- } \
- while (inlen > 0) { \
- if (md->state_var.curlen == 0 && inlen >= block_size) { \
- if ((err = compress_name(md, (unsigned char*)in)) != CRYPT_OK) { \
- return err; \
- } \
- md->state_var.length += block_size * 8; \
- in += block_size; \
- inlen -= block_size; \
- } else { \
- n = MIN(inlen, (block_size - md->state_var.curlen)); \
- memcpy(md->state_var.buf + md->state_var.curlen, in, (size_t)n); \
- md->state_var.curlen += n; \
- in += n; \
- inlen -= n; \
- if (md->state_var.curlen == block_size) { \
- if ((err = compress_name(md, md->state_var.buf)) != CRYPT_OK) { \
- return err; \
- } \
- md->state_var.length += 8 * block_size; \
- md->state_var.curlen = 0; \
- } \
- } \
- } \
- return CRYPT_OK; \
- }
-
-/* $Source: /cvs/libtom/libtomcrypt/src/headers/tomcrypt_hash.h,v $ */
-/* $Revision: 1.22 $ */
-/* $Date: 2007/05/12 14:32:35 $ */
diff --git a/src/mongo/crypto/tom/tomcrypt_mac.h b/src/mongo/crypto/tom/tomcrypt_mac.h
deleted file mode 100644
index e850aa88a3a..00000000000
--- a/src/mongo/crypto/tom/tomcrypt_mac.h
+++ /dev/null
@@ -1,473 +0,0 @@
-/* Copyright 2014 MongoDB Inc.
- *
- * 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
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#ifdef LTC_HMAC
-typedef struct Hmac_state {
- hash_state md;
- int hash;
- hash_state hashstate;
- unsigned char* key;
-} hmac_state;
-
-int hmac_init(hmac_state* hmac, int hash, const unsigned char* key, unsigned long keylen);
-int hmac_process(hmac_state* hmac, const unsigned char* in, unsigned long inlen);
-int hmac_done(hmac_state* hmac, unsigned char* out, unsigned long* outlen);
-int hmac_test(void);
-int hmac_memory(int hash,
- const unsigned char* key,
- unsigned long keylen,
- const unsigned char* in,
- unsigned long inlen,
- unsigned char* out,
- unsigned long* outlen);
-int hmac_memory_multi(int hash,
- const unsigned char* key,
- unsigned long keylen,
- unsigned char* out,
- unsigned long* outlen,
- const unsigned char* in,
- unsigned long inlen,
- ...);
-int hmac_file(int hash,
- const char* fname,
- const unsigned char* key,
- unsigned long keylen,
- unsigned char* dst,
- unsigned long* dstlen);
-#endif
-
-#ifdef LTC_OMAC
-
-typedef struct {
- int cipher_idx, buflen, blklen;
- unsigned char block[MAXBLOCKSIZE], prev[MAXBLOCKSIZE], Lu[2][MAXBLOCKSIZE];
- symmetric_key key;
-} omac_state;
-
-int omac_init(omac_state* omac, int cipher, const unsigned char* key, unsigned long keylen);
-int omac_process(omac_state* omac, const unsigned char* in, unsigned long inlen);
-int omac_done(omac_state* omac, unsigned char* out, unsigned long* outlen);
-int omac_memory(int cipher,
- const unsigned char* key,
- unsigned long keylen,
- const unsigned char* in,
- unsigned long inlen,
- unsigned char* out,
- unsigned long* outlen);
-int omac_memory_multi(int cipher,
- const unsigned char* key,
- unsigned long keylen,
- unsigned char* out,
- unsigned long* outlen,
- const unsigned char* in,
- unsigned long inlen,
- ...);
-int omac_file(int cipher,
- const unsigned char* key,
- unsigned long keylen,
- const char* filename,
- unsigned char* out,
- unsigned long* outlen);
-int omac_test(void);
-#endif /* LTC_OMAC */
-
-#ifdef LTC_PMAC
-
-typedef struct {
- unsigned char Ls[32][MAXBLOCKSIZE], /* L shifted by i bits to the left */
- Li[MAXBLOCKSIZE], /* value of Li [current value, we calc from previous recall] */
- Lr[MAXBLOCKSIZE], /* L * x^-1 */
- block[MAXBLOCKSIZE], /* currently accumulated block */
- checksum[MAXBLOCKSIZE]; /* current checksum */
-
- symmetric_key key; /* scheduled key for cipher */
- unsigned long block_index; /* index # for current block */
- int cipher_idx, /* cipher idx */
- block_len, /* length of block */
- buflen; /* number of bytes in the buffer */
-} pmac_state;
-
-int pmac_init(pmac_state* pmac, int cipher, const unsigned char* key, unsigned long keylen);
-int pmac_process(pmac_state* pmac, const unsigned char* in, unsigned long inlen);
-int pmac_done(pmac_state* pmac, unsigned char* out, unsigned long* outlen);
-
-int pmac_memory(int cipher,
- const unsigned char* key,
- unsigned long keylen,
- const unsigned char* msg,
- unsigned long msglen,
- unsigned char* out,
- unsigned long* outlen);
-
-int pmac_memory_multi(int cipher,
- const unsigned char* key,
- unsigned long keylen,
- unsigned char* out,
- unsigned long* outlen,
- const unsigned char* in,
- unsigned long inlen,
- ...);
-
-int pmac_file(int cipher,
- const unsigned char* key,
- unsigned long keylen,
- const char* filename,
- unsigned char* out,
- unsigned long* outlen);
-
-int pmac_test(void);
-
-/* internal functions */
-int pmac_ntz(unsigned long x);
-void pmac_shift_xor(pmac_state* pmac);
-
-#endif /* PMAC */
-
-#ifdef LTC_EAX_MODE
-
-#if !(defined(LTC_OMAC) && defined(LTC_CTR_MODE))
-#error LTC_EAX_MODE requires LTC_OMAC and CTR
-#endif
-
-typedef struct {
- unsigned char N[MAXBLOCKSIZE];
- symmetric_CTR ctr;
- omac_state headeromac, ctomac;
-} eax_state;
-
-int eax_init(eax_state* eax,
- int cipher,
- const unsigned char* key,
- unsigned long keylen,
- const unsigned char* nonce,
- unsigned long noncelen,
- const unsigned char* header,
- unsigned long headerlen);
-
-int eax_encrypt(eax_state* eax, const unsigned char* pt, unsigned char* ct, unsigned long length);
-int eax_decrypt(eax_state* eax, const unsigned char* ct, unsigned char* pt, unsigned long length);
-int eax_addheader(eax_state* eax, const unsigned char* header, unsigned long length);
-int eax_done(eax_state* eax, unsigned char* tag, unsigned long* taglen);
-
-int eax_encrypt_authenticate_memory(int cipher,
- const unsigned char* key,
- unsigned long keylen,
- const unsigned char* nonce,
- unsigned long noncelen,
- const unsigned char* header,
- unsigned long headerlen,
- const unsigned char* pt,
- unsigned long ptlen,
- unsigned char* ct,
- unsigned char* tag,
- unsigned long* taglen);
-
-int eax_decrypt_verify_memory(int cipher,
- const unsigned char* key,
- unsigned long keylen,
- const unsigned char* nonce,
- unsigned long noncelen,
- const unsigned char* header,
- unsigned long headerlen,
- const unsigned char* ct,
- unsigned long ctlen,
- unsigned char* pt,
- unsigned char* tag,
- unsigned long taglen,
- int* stat);
-
-int eax_test(void);
-#endif /* EAX MODE */
-
-#ifdef LTC_OCB_MODE
-typedef struct {
- unsigned char L[MAXBLOCKSIZE], /* L value */
- Ls[32][MAXBLOCKSIZE], /* L shifted by i bits to the left */
- Li[MAXBLOCKSIZE], /* value of Li [current value, we calc from previous recall] */
- Lr[MAXBLOCKSIZE], /* L * x^-1 */
- R[MAXBLOCKSIZE], /* R value */
- checksum[MAXBLOCKSIZE]; /* current checksum */
-
- symmetric_key key; /* scheduled key for cipher */
- unsigned long block_index; /* index # for current block */
- int cipher, /* cipher idx */
- block_len; /* length of block */
-} ocb_state;
-
-int ocb_init(ocb_state* ocb,
- int cipher,
- const unsigned char* key,
- unsigned long keylen,
- const unsigned char* nonce);
-
-int ocb_encrypt(ocb_state* ocb, const unsigned char* pt, unsigned char* ct);
-int ocb_decrypt(ocb_state* ocb, const unsigned char* ct, unsigned char* pt);
-
-int ocb_done_encrypt(ocb_state* ocb,
- const unsigned char* pt,
- unsigned long ptlen,
- unsigned char* ct,
- unsigned char* tag,
- unsigned long* taglen);
-
-int ocb_done_decrypt(ocb_state* ocb,
- const unsigned char* ct,
- unsigned long ctlen,
- unsigned char* pt,
- const unsigned char* tag,
- unsigned long taglen,
- int* stat);
-
-int ocb_encrypt_authenticate_memory(int cipher,
- const unsigned char* key,
- unsigned long keylen,
- const unsigned char* nonce,
- const unsigned char* pt,
- unsigned long ptlen,
- unsigned char* ct,
- unsigned char* tag,
- unsigned long* taglen);
-
-int ocb_decrypt_verify_memory(int cipher,
- const unsigned char* key,
- unsigned long keylen,
- const unsigned char* nonce,
- const unsigned char* ct,
- unsigned long ctlen,
- unsigned char* pt,
- const unsigned char* tag,
- unsigned long taglen,
- int* stat);
-
-int ocb_test(void);
-
-/* internal functions */
-void ocb_shift_xor(ocb_state* ocb, unsigned char* Z);
-int ocb_ntz(unsigned long x);
-int s_ocb_done(ocb_state* ocb,
- const unsigned char* pt,
- unsigned long ptlen,
- unsigned char* ct,
- unsigned char* tag,
- unsigned long* taglen,
- int mode);
-
-#endif /* LTC_OCB_MODE */
-
-#ifdef LTC_CCM_MODE
-
-#define CCM_ENCRYPT 0
-#define CCM_DECRYPT 1
-
-int ccm_memory(int cipher,
- const unsigned char* key,
- unsigned long keylen,
- symmetric_key* uskey,
- const unsigned char* nonce,
- unsigned long noncelen,
- const unsigned char* header,
- unsigned long headerlen,
- unsigned char* pt,
- unsigned long ptlen,
- unsigned char* ct,
- unsigned char* tag,
- unsigned long* taglen,
- int direction);
-
-int ccm_test(void);
-
-#endif /* LTC_CCM_MODE */
-
-#if defined(LRW_MODE) || defined(LTC_GCM_MODE)
-void gcm_gf_mult(const unsigned char* a, const unsigned char* b, unsigned char* c);
-#endif
-
-
-/* table shared between GCM and LRW */
-#if defined(LTC_GCM_TABLES) || defined(LRW_TABLES) || \
- ((defined(LTC_GCM_MODE) || defined(LTC_GCM_MODE)) && defined(LTC_FAST))
-extern const unsigned char gcm_shift_table[];
-#endif
-
-#ifdef LTC_GCM_MODE
-
-#define GCM_ENCRYPT 0
-#define GCM_DECRYPT 1
-
-#define LTC_GCM_MODE_IV 0
-#define LTC_GCM_MODE_AAD 1
-#define LTC_GCM_MODE_TEXT 2
-
-typedef struct {
- symmetric_key K;
- unsigned char H[16], /* multiplier */
- X[16], /* accumulator */
- Y[16], /* counter */
- Y_0[16], /* initial counter */
- buf[16]; /* buffer for stuff */
-
- int cipher, /* which cipher */
- ivmode, /* Which mode is the IV in? */
- mode, /* mode the GCM code is in */
- buflen; /* length of data in buf */
-
- ulong64 totlen, /* 64-bit counter used for IV and AAD */
- pttotlen; /* 64-bit counter for the PT */
-
-#ifdef LTC_GCM_TABLES
- unsigned char PC[16][256][16] /* 16 tables of 8x128 */
-#ifdef LTC_GCM_TABLES_SSE2
- __attribute__((aligned(16)))
-#endif
- ;
-#endif
-} gcm_state;
-
-void gcm_mult_h(gcm_state* gcm, unsigned char* I);
-
-int gcm_init(gcm_state* gcm, int cipher, const unsigned char* key, int keylen);
-
-int gcm_reset(gcm_state* gcm);
-
-int gcm_add_iv(gcm_state* gcm, const unsigned char* IV, unsigned long IVlen);
-
-int gcm_add_aad(gcm_state* gcm, const unsigned char* adata, unsigned long adatalen);
-
-int gcm_process(
- gcm_state* gcm, unsigned char* pt, unsigned long ptlen, unsigned char* ct, int direction);
-
-int gcm_done(gcm_state* gcm, unsigned char* tag, unsigned long* taglen);
-
-int gcm_memory(int cipher,
- const unsigned char* key,
- unsigned long keylen,
- const unsigned char* IV,
- unsigned long IVlen,
- const unsigned char* adata,
- unsigned long adatalen,
- unsigned char* pt,
- unsigned long ptlen,
- unsigned char* ct,
- unsigned char* tag,
- unsigned long* taglen,
- int direction);
-int gcm_test(void);
-
-#endif /* LTC_GCM_MODE */
-
-#ifdef LTC_PELICAN
-
-typedef struct pelican_state {
- symmetric_key K;
- unsigned char state[16];
- int buflen;
-} pelican_state;
-
-int pelican_init(pelican_state* pelmac, const unsigned char* key, unsigned long keylen);
-int pelican_process(pelican_state* pelmac, const unsigned char* in, unsigned long inlen);
-int pelican_done(pelican_state* pelmac, unsigned char* out);
-int pelican_test(void);
-
-int pelican_memory(const unsigned char* key,
- unsigned long keylen,
- const unsigned char* in,
- unsigned long inlen,
- unsigned char* out);
-
-#endif
-
-#ifdef LTC_XCBC
-
-/* add this to "keylen" to xcbc_init to use a pure three-key XCBC MAC */
-#define LTC_XCBC_PURE 0x8000UL
-
-typedef struct {
- unsigned char K[3][MAXBLOCKSIZE], IV[MAXBLOCKSIZE];
-
- symmetric_key key;
-
- int cipher, buflen, blocksize;
-} xcbc_state;
-
-int xcbc_init(xcbc_state* xcbc, int cipher, const unsigned char* key, unsigned long keylen);
-int xcbc_process(xcbc_state* xcbc, const unsigned char* in, unsigned long inlen);
-int xcbc_done(xcbc_state* xcbc, unsigned char* out, unsigned long* outlen);
-int xcbc_memory(int cipher,
- const unsigned char* key,
- unsigned long keylen,
- const unsigned char* in,
- unsigned long inlen,
- unsigned char* out,
- unsigned long* outlen);
-int xcbc_memory_multi(int cipher,
- const unsigned char* key,
- unsigned long keylen,
- unsigned char* out,
- unsigned long* outlen,
- const unsigned char* in,
- unsigned long inlen,
- ...);
-int xcbc_file(int cipher,
- const unsigned char* key,
- unsigned long keylen,
- const char* filename,
- unsigned char* out,
- unsigned long* outlen);
-int xcbc_test(void);
-
-#endif
-
-#ifdef LTC_F9_MODE
-
-typedef struct {
- unsigned char akey[MAXBLOCKSIZE], ACC[MAXBLOCKSIZE], IV[MAXBLOCKSIZE];
-
- symmetric_key key;
-
- int cipher, buflen, keylen, blocksize;
-} f9_state;
-
-int f9_init(f9_state* f9, int cipher, const unsigned char* key, unsigned long keylen);
-int f9_process(f9_state* f9, const unsigned char* in, unsigned long inlen);
-int f9_done(f9_state* f9, unsigned char* out, unsigned long* outlen);
-int f9_memory(int cipher,
- const unsigned char* key,
- unsigned long keylen,
- const unsigned char* in,
- unsigned long inlen,
- unsigned char* out,
- unsigned long* outlen);
-int f9_memory_multi(int cipher,
- const unsigned char* key,
- unsigned long keylen,
- unsigned char* out,
- unsigned long* outlen,
- const unsigned char* in,
- unsigned long inlen,
- ...);
-int f9_file(int cipher,
- const unsigned char* key,
- unsigned long keylen,
- const char* filename,
- unsigned char* out,
- unsigned long* outlen);
-int f9_test(void);
-
-#endif
-
-
-/* $Source: /cvs/libtom/libtomcrypt/src/headers/tomcrypt_mac.h,v $ */
-/* $Revision: 1.23 $ */
-/* $Date: 2007/05/12 14:37:41 $ */
diff --git a/src/mongo/crypto/tom/tomcrypt_macros.h b/src/mongo/crypto/tom/tomcrypt_macros.h
deleted file mode 100644
index e043c2bb36d..00000000000
--- a/src/mongo/crypto/tom/tomcrypt_macros.h
+++ /dev/null
@@ -1,512 +0,0 @@
-/* Copyright 2014 MongoDB Inc.
- *
- * 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
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-/* fix for MSVC ...evil! */
-#ifdef _MSC_VER
-#define CONST64(n) n##ui64
-typedef unsigned __int64 ulong64;
-#else
-#define CONST64(n) n##ULL
-typedef unsigned long long ulong64;
-#endif
-
-/* this is the "32-bit at least" data type
- * Re-define it to suit your platform but it must be at least 32-bits
- */
-#if defined(__x86_64__) || (defined(__sparc__) && defined(__arch64__))
-typedef unsigned ulong32;
-#else
-typedef unsigned long ulong32;
-#endif
-
-/* ---- HELPER MACROS ---- */
-#ifdef ENDIAN_NEUTRAL
-
-#define STORE32L(x, y) \
- { \
- (y)[3] = (unsigned char)(((x) >> 24) & 255); \
- (y)[2] = (unsigned char)(((x) >> 16) & 255); \
- (y)[1] = (unsigned char)(((x) >> 8) & 255); \
- (y)[0] = (unsigned char)((x)&255); \
- }
-
-#define LOAD32L(x, y) \
- { \
- x = ((unsigned long)((y)[3] & 255) << 24) | ((unsigned long)((y)[2] & 255) << 16) | \
- ((unsigned long)((y)[1] & 255) << 8) | ((unsigned long)((y)[0] & 255)); \
- }
-
-#define STORE64L(x, y) \
- { \
- (y)[7] = (unsigned char)(((x) >> 56) & 255); \
- (y)[6] = (unsigned char)(((x) >> 48) & 255); \
- (y)[5] = (unsigned char)(((x) >> 40) & 255); \
- (y)[4] = (unsigned char)(((x) >> 32) & 255); \
- (y)[3] = (unsigned char)(((x) >> 24) & 255); \
- (y)[2] = (unsigned char)(((x) >> 16) & 255); \
- (y)[1] = (unsigned char)(((x) >> 8) & 255); \
- (y)[0] = (unsigned char)((x)&255); \
- }
-
-#define LOAD64L(x, y) \
- { \
- x = (((ulong64)((y)[7] & 255)) << 56) | (((ulong64)((y)[6] & 255)) << 48) | \
- (((ulong64)((y)[5] & 255)) << 40) | (((ulong64)((y)[4] & 255)) << 32) | \
- (((ulong64)((y)[3] & 255)) << 24) | (((ulong64)((y)[2] & 255)) << 16) | \
- (((ulong64)((y)[1] & 255)) << 8) | (((ulong64)((y)[0] & 255))); \
- }
-
-#define STORE32H(x, y) \
- { \
- (y)[0] = (unsigned char)(((x) >> 24) & 255); \
- (y)[1] = (unsigned char)(((x) >> 16) & 255); \
- (y)[2] = (unsigned char)(((x) >> 8) & 255); \
- (y)[3] = (unsigned char)((x)&255); \
- }
-
-#define LOAD32H(x, y) \
- { \
- x = ((unsigned long)((y)[0] & 255) << 24) | ((unsigned long)((y)[1] & 255) << 16) | \
- ((unsigned long)((y)[2] & 255) << 8) | ((unsigned long)((y)[3] & 255)); \
- }
-
-#define STORE64H(x, y) \
- { \
- (y)[0] = (unsigned char)(((x) >> 56) & 255); \
- (y)[1] = (unsigned char)(((x) >> 48) & 255); \
- (y)[2] = (unsigned char)(((x) >> 40) & 255); \
- (y)[3] = (unsigned char)(((x) >> 32) & 255); \
- (y)[4] = (unsigned char)(((x) >> 24) & 255); \
- (y)[5] = (unsigned char)(((x) >> 16) & 255); \
- (y)[6] = (unsigned char)(((x) >> 8) & 255); \
- (y)[7] = (unsigned char)((x)&255); \
- }
-
-#define LOAD64H(x, y) \
- { \
- x = (((ulong64)((y)[0] & 255)) << 56) | (((ulong64)((y)[1] & 255)) << 48) | \
- (((ulong64)((y)[2] & 255)) << 40) | (((ulong64)((y)[3] & 255)) << 32) | \
- (((ulong64)((y)[4] & 255)) << 24) | (((ulong64)((y)[5] & 255)) << 16) | \
- (((ulong64)((y)[6] & 255)) << 8) | (((ulong64)((y)[7] & 255))); \
- }
-
-#endif /* ENDIAN_NEUTRAL */
-
-#ifdef ENDIAN_LITTLE
-
-#if !defined(LTC_NO_BSWAP) && \
- (defined(INTEL_CC) || \
- (defined(__GNUC__) && (defined(__DJGPP__) || defined(__CYGWIN__) || defined(__MINGW32__) || \
- defined(__i386__) || defined(__x86_64__))))
-
-#define STORE32H(x, y) \
- asm __volatile__( \
- "bswapl %0 \n\t" \
- "movl %0,(%1)\n\t" \
- "bswapl %0 \n\t" ::"r"(x), \
- "r"(y));
-
-#define LOAD32H(x, y) \
- asm __volatile__( \
- "movl (%1),%0\n\t" \
- "bswapl %0\n\t" \
- : "=r"(x) \
- : "r"(y));
-
-#else
-
-#define STORE32H(x, y) \
- { \
- (y)[0] = (unsigned char)(((x) >> 24) & 255); \
- (y)[1] = (unsigned char)(((x) >> 16) & 255); \
- (y)[2] = (unsigned char)(((x) >> 8) & 255); \
- (y)[3] = (unsigned char)((x)&255); \
- }
-
-#define LOAD32H(x, y) \
- { \
- x = ((unsigned long)((y)[0] & 255) << 24) | ((unsigned long)((y)[1] & 255) << 16) | \
- ((unsigned long)((y)[2] & 255) << 8) | ((unsigned long)((y)[3] & 255)); \
- }
-
-#endif
-
-
-/* x86_64 processor */
-#if !defined(LTC_NO_BSWAP) && (defined(__GNUC__) && defined(__x86_64__))
-
-#define STORE64H(x, y) \
- asm __volatile__( \
- "bswapq %0 \n\t" \
- "movq %0,(%1)\n\t" \
- "bswapq %0 \n\t" ::"r"(x), \
- "r"(y));
-
-#define LOAD64H(x, y) \
- asm __volatile__( \
- "movq (%1),%0\n\t" \
- "bswapq %0\n\t" \
- : "=r"(x) \
- : "r"(y));
-
-#else
-
-#define STORE64H(x, y) \
- { \
- (y)[0] = (unsigned char)(((x) >> 56) & 255); \
- (y)[1] = (unsigned char)(((x) >> 48) & 255); \
- (y)[2] = (unsigned char)(((x) >> 40) & 255); \
- (y)[3] = (unsigned char)(((x) >> 32) & 255); \
- (y)[4] = (unsigned char)(((x) >> 24) & 255); \
- (y)[5] = (unsigned char)(((x) >> 16) & 255); \
- (y)[6] = (unsigned char)(((x) >> 8) & 255); \
- (y)[7] = (unsigned char)((x)&255); \
- }
-
-#define LOAD64H(x, y) \
- { \
- x = (((ulong64)((y)[0] & 255)) << 56) | (((ulong64)((y)[1] & 255)) << 48) | \
- (((ulong64)((y)[2] & 255)) << 40) | (((ulong64)((y)[3] & 255)) << 32) | \
- (((ulong64)((y)[4] & 255)) << 24) | (((ulong64)((y)[5] & 255)) << 16) | \
- (((ulong64)((y)[6] & 255)) << 8) | (((ulong64)((y)[7] & 255))); \
- }
-
-#endif
-
-#ifdef ENDIAN_32BITWORD
-
-#define STORE32L(x, y) \
- { \
- ulong32 __t = (x); \
- XMEMCPY(y, &__t, 4); \
- }
-
-#define LOAD32L(x, y) XMEMCPY(&(x), y, 4);
-
-#define STORE64L(x, y) \
- { \
- (y)[7] = (unsigned char)(((x) >> 56) & 255); \
- (y)[6] = (unsigned char)(((x) >> 48) & 255); \
- (y)[5] = (unsigned char)(((x) >> 40) & 255); \
- (y)[4] = (unsigned char)(((x) >> 32) & 255); \
- (y)[3] = (unsigned char)(((x) >> 24) & 255); \
- (y)[2] = (unsigned char)(((x) >> 16) & 255); \
- (y)[1] = (unsigned char)(((x) >> 8) & 255); \
- (y)[0] = (unsigned char)((x)&255); \
- }
-
-#define LOAD64L(x, y) \
- { \
- x = (((ulong64)((y)[7] & 255)) << 56) | (((ulong64)((y)[6] & 255)) << 48) | \
- (((ulong64)((y)[5] & 255)) << 40) | (((ulong64)((y)[4] & 255)) << 32) | \
- (((ulong64)((y)[3] & 255)) << 24) | (((ulong64)((y)[2] & 255)) << 16) | \
- (((ulong64)((y)[1] & 255)) << 8) | (((ulong64)((y)[0] & 255))); \
- }
-
-#else /* 64-bit words then */
-
-#define STORE32L(x, y) \
- { \
- ulong32 __t = (x); \
- XMEMCPY(y, &__t, 4); \
- }
-
-#define LOAD32L(x, y) \
- { \
- XMEMCPY(&(x), y, 4); \
- x &= 0xFFFFFFFF; \
- }
-
-#define STORE64L(x, y) \
- { \
- ulong64 __t = (x); \
- XMEMCPY(y, &__t, 8); \
- }
-
-#define LOAD64L(x, y) \
- { XMEMCPY(&(x), y, 8); }
-
-#endif /* ENDIAN_64BITWORD */
-
-#endif /* ENDIAN_LITTLE */
-
-#ifdef ENDIAN_BIG
-#define STORE32L(x, y) \
- { \
- (y)[3] = (unsigned char)(((x) >> 24) & 255); \
- (y)[2] = (unsigned char)(((x) >> 16) & 255); \
- (y)[1] = (unsigned char)(((x) >> 8) & 255); \
- (y)[0] = (unsigned char)((x)&255); \
- }
-
-#define LOAD32L(x, y) \
- { \
- x = ((unsigned long)((y)[3] & 255) << 24) | ((unsigned long)((y)[2] & 255) << 16) | \
- ((unsigned long)((y)[1] & 255) << 8) | ((unsigned long)((y)[0] & 255)); \
- }
-
-#define STORE64L(x, y) \
- { \
- (y)[7] = (unsigned char)(((x) >> 56) & 255); \
- (y)[6] = (unsigned char)(((x) >> 48) & 255); \
- (y)[5] = (unsigned char)(((x) >> 40) & 255); \
- (y)[4] = (unsigned char)(((x) >> 32) & 255); \
- (y)[3] = (unsigned char)(((x) >> 24) & 255); \
- (y)[2] = (unsigned char)(((x) >> 16) & 255); \
- (y)[1] = (unsigned char)(((x) >> 8) & 255); \
- (y)[0] = (unsigned char)((x)&255); \
- }
-
-#define LOAD64L(x, y) \
- { \
- x = (((ulong64)((y)[7] & 255)) << 56) | (((ulong64)((y)[6] & 255)) << 48) | \
- (((ulong64)((y)[5] & 255)) << 40) | (((ulong64)((y)[4] & 255)) << 32) | \
- (((ulong64)((y)[3] & 255)) << 24) | (((ulong64)((y)[2] & 255)) << 16) | \
- (((ulong64)((y)[1] & 255)) << 8) | (((ulong64)((y)[0] & 255))); \
- }
-
-#ifdef ENDIAN_32BITWORD
-
-#define STORE32H(x, y) \
- { \
- ulong32 __t = (x); \
- XMEMCPY(y, &__t, 4); \
- }
-
-#define LOAD32H(x, y) XMEMCPY(&(x), y, 4);
-
-#define STORE64H(x, y) \
- { \
- (y)[0] = (unsigned char)(((x) >> 56) & 255); \
- (y)[1] = (unsigned char)(((x) >> 48) & 255); \
- (y)[2] = (unsigned char)(((x) >> 40) & 255); \
- (y)[3] = (unsigned char)(((x) >> 32) & 255); \
- (y)[4] = (unsigned char)(((x) >> 24) & 255); \
- (y)[5] = (unsigned char)(((x) >> 16) & 255); \
- (y)[6] = (unsigned char)(((x) >> 8) & 255); \
- (y)[7] = (unsigned char)((x)&255); \
- }
-
-#define LOAD64H(x, y) \
- { \
- x = (((ulong64)((y)[0] & 255)) << 56) | (((ulong64)((y)[1] & 255)) << 48) | \
- (((ulong64)((y)[2] & 255)) << 40) | (((ulong64)((y)[3] & 255)) << 32) | \
- (((ulong64)((y)[4] & 255)) << 24) | (((ulong64)((y)[5] & 255)) << 16) | \
- (((ulong64)((y)[6] & 255)) << 8) | (((ulong64)((y)[7] & 255))); \
- }
-
-#else /* 64-bit words then */
-
-#define STORE32H(x, y) \
- { \
- ulong32 __t = (x); \
- XMEMCPY(y, &__t, 4); \
- }
-
-#define LOAD32H(x, y) \
- { \
- XMEMCPY(&(x), y, 4); \
- x &= 0xFFFFFFFF; \
- }
-
-#define STORE64H(x, y) \
- { \
- ulong64 __t = (x); \
- XMEMCPY(y, &__t, 8); \
- }
-
-#define LOAD64H(x, y) \
- { XMEMCPY(&(x), y, 8); }
-
-#endif /* ENDIAN_64BITWORD */
-#endif /* ENDIAN_BIG */
-
-#define BSWAP(x) \
- (((x >> 24) & 0x000000FFUL) | ((x << 24) & 0xFF000000UL) | ((x >> 8) & 0x0000FF00UL) | \
- ((x << 8) & 0x00FF0000UL))
-
-
-/* 32-bit Rotates */
-#if defined(_MSC_VER)
-
-/* instrinsic rotate */
-#include <stdlib.h>
-#pragma intrinsic(_lrotr, _lrotl)
-#define ROR(x, n) _lrotr(x, n)
-#define ROL(x, n) _lrotl(x, n)
-#define RORc(x, n) _lrotr(x, n)
-#define ROLc(x, n) _lrotl(x, n)
-
-#elif !defined(__STRICT_ANSI__) && defined(__GNUC__) && \
- (defined(__i386__) || defined(__x86_64__)) && !defined(INTEL_CC) && !defined(LTC_NO_ASM)
-
-static inline unsigned ROL(unsigned word, int i) {
- asm("roll %%cl,%0" : "=r"(word) : "0"(word), "c"(i));
- return word;
-}
-
-static inline unsigned ROR(unsigned word, int i) {
- asm("rorl %%cl,%0" : "=r"(word) : "0"(word), "c"(i));
- return word;
-}
-
-#ifndef LTC_NO_ROLC
-
-static inline unsigned ROLc(unsigned word, const int i) {
- asm("roll %2,%0" : "=r"(word) : "0"(word), "I"(i));
- return word;
-}
-
-static inline unsigned RORc(unsigned word, const int i) {
- asm("rorl %2,%0" : "=r"(word) : "0"(word), "I"(i));
- return word;
-}
-
-#else
-
-#define ROLc ROL
-#define RORc ROR
-
-#endif
-
-#elif !defined(__STRICT_ANSI__) && defined(LTC_PPC32)
-
-static inline unsigned ROL(unsigned word, int i) {
- asm("rotlw %0,%0,%2" : "=r"(word) : "0"(word), "r"(i));
- return word;
-}
-
-static inline unsigned ROR(unsigned word, int i) {
- asm("rotlw %0,%0,%2" : "=r"(word) : "0"(word), "r"(32 - i));
- return word;
-}
-
-#ifndef LTC_NO_ROLC
-
-static inline unsigned ROLc(unsigned word, const int i) {
- asm("rotlwi %0,%0,%2" : "=r"(word) : "0"(word), "I"(i));
- return word;
-}
-
-static inline unsigned RORc(unsigned word, const int i) {
- asm("rotrwi %0,%0,%2" : "=r"(word) : "0"(word), "I"(i));
- return word;
-}
-
-#else
-
-#define ROLc ROL
-#define RORc ROR
-
-#endif
-
-
-#else
-
-/* rotates the hard way */
-#define ROL(x, y) \
- ((((unsigned long)(x) << (unsigned long)((y)&31)) | \
- (((unsigned long)(x)&0xFFFFFFFFUL) >> (unsigned long)(32 - ((y)&31)))) & \
- 0xFFFFFFFFUL)
-#define ROR(x, y) \
- (((((unsigned long)(x)&0xFFFFFFFFUL) >> (unsigned long)((y)&31)) | \
- ((unsigned long)(x) << (unsigned long)(32 - ((y)&31)))) & \
- 0xFFFFFFFFUL)
-#define ROLc(x, y) \
- ((((unsigned long)(x) << (unsigned long)((y)&31)) | \
- (((unsigned long)(x)&0xFFFFFFFFUL) >> (unsigned long)(32 - ((y)&31)))) & \
- 0xFFFFFFFFUL)
-#define RORc(x, y) \
- (((((unsigned long)(x)&0xFFFFFFFFUL) >> (unsigned long)((y)&31)) | \
- ((unsigned long)(x) << (unsigned long)(32 - ((y)&31)))) & \
- 0xFFFFFFFFUL)
-
-#endif
-
-
-/* 64-bit Rotates */
-#if !defined(__STRICT_ANSI__) && defined(__GNUC__) && defined(__x86_64__) && !defined(LTC_NO_ASM)
-
-static inline unsigned long ROL64(unsigned long word, int i) {
- asm("rolq %%cl,%0" : "=r"(word) : "0"(word), "c"(i));
- return word;
-}
-
-static inline unsigned long ROR64(unsigned long word, int i) {
- asm("rorq %%cl,%0" : "=r"(word) : "0"(word), "c"(i));
- return word;
-}
-
-#ifndef LTC_NO_ROLC
-
-static inline unsigned long ROL64c(unsigned long word, const int i) {
- asm("rolq %2,%0" : "=r"(word) : "0"(word), "J"(i));
- return word;
-}
-
-static inline unsigned long ROR64c(unsigned long word, const int i) {
- asm("rorq %2,%0" : "=r"(word) : "0"(word), "J"(i));
- return word;
-}
-
-#else /* LTC_NO_ROLC */
-
-#define ROL64c ROL64
-#define ROR64c ROR64
-
-#endif
-
-#else /* Not x86_64 */
-
-#define ROL64(x, y) \
- ((((x) << ((ulong64)(y)&63)) | \
- (((x)&CONST64(0xFFFFFFFFFFFFFFFF)) >> ((ulong64)64 - ((y)&63)))) & \
- CONST64(0xFFFFFFFFFFFFFFFF))
-
-#define ROR64(x, y) \
- (((((x)&CONST64(0xFFFFFFFFFFFFFFFF)) >> ((ulong64)(y)&CONST64(63))) | \
- ((x) << ((ulong64)(64 - ((y)&CONST64(63)))))) & \
- CONST64(0xFFFFFFFFFFFFFFFF))
-
-#define ROL64c(x, y) \
- ((((x) << ((ulong64)(y)&63)) | \
- (((x)&CONST64(0xFFFFFFFFFFFFFFFF)) >> ((ulong64)64 - ((y)&63)))) & \
- CONST64(0xFFFFFFFFFFFFFFFF))
-
-#define ROR64c(x, y) \
- (((((x)&CONST64(0xFFFFFFFFFFFFFFFF)) >> ((ulong64)(y)&CONST64(63))) | \
- ((x) << ((ulong64)(64 - ((y)&CONST64(63)))))) & \
- CONST64(0xFFFFFFFFFFFFFFFF))
-
-#endif
-
-#ifndef MAX
-#define MAX(x, y) (((x) > (y)) ? (x) : (y))
-#endif
-
-#ifndef MIN
-#define MIN(x, y) (((x) < (y)) ? (x) : (y))
-#endif
-
-/* extract a byte portably */
-#ifdef _MSC_VER
-#define byte(x, n) ((unsigned char)((x) >> (8 * (n))))
-#else
-#define byte(x, n) (((x) >> (8 * (n))) & 255)
-#endif
-
-/* $Source: /cvs/libtom/libtomcrypt/src/headers/tomcrypt_macros.h,v $ */
-/* $Revision: 1.15 $ */
-/* $Date: 2006/11/29 23:43:57 $ */
diff --git a/src/mongo/crypto/tom/tomcrypt_misc.h b/src/mongo/crypto/tom/tomcrypt_misc.h
deleted file mode 100644
index 86559cf9852..00000000000
--- a/src/mongo/crypto/tom/tomcrypt_misc.h
+++ /dev/null
@@ -1,42 +0,0 @@
-/* Copyright 2014 MongoDB Inc.
- *
- * 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
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-/* ---- LTC_BASE64 Routines ---- */
-#ifdef LTC_BASE64
-int base64_encode(const unsigned char* in,
- unsigned long len,
- unsigned char* out,
- unsigned long* outlen);
-
-int base64_decode(const unsigned char* in,
- unsigned long len,
- unsigned char* out,
- unsigned long* outlen);
-#endif
-
-/* ---- MEM routines ---- */
-void zeromem(void* dst, size_t len);
-void burn_stack(unsigned long len);
-
-const char* error_to_string(int err);
-
-extern const char* crypt_build_settings;
-
-/* ---- HMM ---- */
-int crypt_fsa(void* mp, ...);
-
-/* $Source: /cvs/libtom/libtomcrypt/src/headers/tomcrypt_misc.h,v $ */
-/* $Revision: 1.5 $ */
-/* $Date: 2007/05/12 14:32:35 $ */
diff --git a/src/mongo/db/SConscript b/src/mongo/db/SConscript
index a8c9d2d5f8d..4ede84a6e4c 100644
--- a/src/mongo/db/SConscript
+++ b/src/mongo/db/SConscript
@@ -1111,7 +1111,7 @@ env.Library(
'time_proof_service.cpp',
],
LIBDEPS=[
- '$BUILD_DIR/mongo/crypto/sha1_block_${MONGO_CRYPTO}',
+ '$BUILD_DIR/mongo/crypto/sha_block_${MONGO_CRYPTO}',
'logical_time',
],
)
diff --git a/src/mongo/util/make_array_type.h b/src/mongo/util/make_array_type.h
new file mode 100644
index 00000000000..a86501bdd24
--- /dev/null
+++ b/src/mongo/util/make_array_type.h
@@ -0,0 +1,222 @@
+/**
+ * Copyright (C) 2017 MongoDB Inc.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License, version 3,
+ * as published by the Free Software Foundation.
+ *
+ * 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
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * 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 GNU Affero General 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 <array>
+
+#pragma push_macro("MONGO_CONSTEXPR")
+#undef MONGO_CONSTEXPR
+
+#ifdef _MSC_VER
+#define MONGO_CONSTEXPR
+#else
+#define MONGO_CONSTEXPR constexpr
+#endif
+
+namespace mongo {
+
+/**
+ * A synthetic std::array alike builder for making array's with unique types.
+ */
+template <typename T, size_t N, typename Tag>
+struct MakeArrayType {
+ using Array = std::array<T, N>;
+
+ using value_type = typename Array::value_type;
+ using size_type = typename Array::size_type;
+ using difference_type = typename Array::difference_type;
+ using reference = typename Array::reference;
+ using const_reference = typename Array::const_reference;
+ using pointer = typename Array::pointer;
+ using const_pointer = typename Array::const_pointer;
+ using iterator = typename Array::iterator;
+ using const_iterator = typename Array::const_iterator;
+ using reverse_iterator = typename Array::reverse_iterator;
+ using const_reverse_iterator = typename Array::const_reverse_iterator;
+
+ reference at(size_type pos) {
+ return _data.at(pos);
+ }
+
+ const_reference at(size_type pos) const {
+ return _data.at(pos);
+ }
+
+ reference operator[](size_type pos) {
+ return _data[pos];
+ }
+
+ const_reference operator[](size_type pos) const {
+ return _data[pos];
+ }
+
+ reference front() {
+ return _data.front();
+ }
+
+ const_reference front() const {
+ return _data.front();
+ }
+
+ reference back() {
+ return _data.back();
+ }
+
+ const_reference back() const {
+ return _data.back();
+ }
+
+ pointer data() {
+ return _data.data();
+ }
+
+ const_pointer data() const {
+ return _data.data();
+ }
+
+ MONGO_CONSTEXPR iterator begin() noexcept {
+ return _data.begin();
+ }
+
+ MONGO_CONSTEXPR const_iterator begin() const noexcept {
+ return _data.begin();
+ }
+
+ MONGO_CONSTEXPR const_iterator cbegin() const noexcept {
+ return _data.begin();
+ }
+
+ MONGO_CONSTEXPR iterator end() noexcept {
+ return _data.end();
+ }
+
+ MONGO_CONSTEXPR const_iterator end() const noexcept {
+ return _data.end();
+ }
+
+ MONGO_CONSTEXPR const_iterator cend() const noexcept {
+ return _data.end();
+ }
+
+ MONGO_CONSTEXPR reverse_iterator rbegin() noexcept {
+ return _data.rbegin();
+ }
+
+ MONGO_CONSTEXPR const_reverse_iterator rbegin() const noexcept {
+ return _data.rbegin();
+ }
+
+ MONGO_CONSTEXPR const_reverse_iterator crbegin() const noexcept {
+ return _data.rbegin();
+ }
+
+ MONGO_CONSTEXPR reverse_iterator rend() noexcept {
+ return _data.rend();
+ }
+
+ MONGO_CONSTEXPR const_reverse_iterator rend() const noexcept {
+ return _data.rend();
+ }
+
+ MONGO_CONSTEXPR const_reverse_iterator crend() const noexcept {
+ return _data.rend();
+ }
+
+ MONGO_CONSTEXPR bool empty() const noexcept {
+ return _data.empty();
+ }
+
+ MONGO_CONSTEXPR size_t size() const noexcept {
+ return _data.size();
+ }
+
+ MONGO_CONSTEXPR size_t max_size() const noexcept {
+ return _data.max_size();
+ }
+
+ void fill(const T& value) {
+ return _data.fill(value);
+ }
+
+ void swap(MakeArrayType& other) noexcept {
+ return _data.swap(other._data);
+ }
+
+ friend bool operator==(const MakeArrayType& lhs, const MakeArrayType& rhs) {
+ return lhs._data == rhs._data;
+ }
+
+ friend bool operator!=(const MakeArrayType& lhs, const MakeArrayType& rhs) {
+ return lhs._data != rhs._data;
+ }
+
+ friend bool operator<(const MakeArrayType& lhs, const MakeArrayType& rhs) {
+ return lhs._data < rhs._data;
+ }
+
+ friend bool operator>(const MakeArrayType& lhs, const MakeArrayType& rhs) {
+ return lhs._data > rhs._data;
+ }
+
+ friend bool operator<=(const MakeArrayType& lhs, const MakeArrayType& rhs) {
+ return lhs._data <= rhs._data;
+ }
+
+ friend bool operator>=(const MakeArrayType& lhs, const MakeArrayType& rhs) {
+ return lhs._data >= rhs._data;
+ }
+
+ friend MONGO_CONSTEXPR T& get(MakeArrayType& mat) noexcept {
+ return get(mat._data);
+ }
+
+ friend MONGO_CONSTEXPR T&& get(MakeArrayType&& mat) noexcept {
+ return get(mat._data);
+ }
+
+ friend MONGO_CONSTEXPR const T& get(const MakeArrayType& mat) noexcept {
+ return get(mat._data);
+ }
+
+ friend MONGO_CONSTEXPR const T&& get(const MakeArrayType&& mat) noexcept {
+ return get(mat._data);
+ }
+
+ friend void swap(MakeArrayType& lhs, MakeArrayType& rhs) noexcept {
+ using std::swap;
+ return swap(lhs._data, rhs._data);
+ }
+
+ Array _data;
+};
+
+} // namespace mongo
+
+#undef MONGO_CONSTEXPR
+#pragma pop_macro("MONGO_CONSTEXPR")
diff --git a/src/third_party/SConscript b/src/third_party/SConscript
index 802ab49e3e4..e24497f67dd 100644
--- a/src/third_party/SConscript
+++ b/src/third_party/SConscript
@@ -14,6 +14,7 @@ yamlSuffix = '-0.5.3'
icuSuffix = '-57.1'
gperftoolsSuffix = '-2.5'
timelibSuffix = '-2017.05beta5'
+tomcryptSuffix = '-1.18.0-rc2'
thirdPartyIncludePathList = [
('s2', '#/src/third_party/s2'),
@@ -57,6 +58,12 @@ if True:
'#/src/third_party/mozjs' + mozjsSuffix + '/platform/' + env["TARGET_ARCH"] + "/" + env["TARGET_OS"] + "/include",
]))
+if "tom" in env["MONGO_CRYPTO"]:
+ thirdPartyIncludePathList.append(
+ ('tomcrypt', ['#/src/third_party/tomcrypt' + tomcryptSuffix + '/src/headers',
+ ]))
+
+
if not use_system_version_of_library('stemmer'):
thirdPartyIncludePathList.append(
('stemmer', '#/src/third_party/libstemmer_c/include'))
@@ -224,6 +231,20 @@ if usemozjs:
'shim_mozjs.cpp',
])
+if "tom" in env["MONGO_CRYPTO"]:
+ tomcryptEnv = env.Clone()
+ tomcryptEnv.SConscript('tomcrypt' + tomcryptSuffix + '/SConscript', exports={'env' : tomcryptEnv })
+ tomcryptEnv = tomcryptEnv.Clone(
+ LIBDEPS_INTERFACE=[
+ 'tomcrypt' + tomcryptSuffix + '/tomcrypt',
+ ])
+
+ tomcryptEnv.Library(
+ target="shim_tomcrypt",
+ source=[
+ 'shim_tomcrypt.cpp',
+ ])
+
gperftoolsEnv = env
if (gperftoolsEnv['MONGO_ALLOCATOR'] == "tcmalloc"):
if use_system_version_of_library("tcmalloc"):
diff --git a/src/third_party/scripts/tomcrypt_get_sources.sh b/src/third_party/scripts/tomcrypt_get_sources.sh
new file mode 100644
index 00000000000..08d8c55b3f9
--- /dev/null
+++ b/src/third_party/scripts/tomcrypt_get_sources.sh
@@ -0,0 +1,62 @@
+#!/bin/sh
+
+set -o verbose
+set -o errexit
+
+set -euo pipefail
+IFS=$'\n\t'
+
+if [ "$#" -ne 0 ]; then
+ echo "This script does not take any arguments"
+ exit 1
+fi
+
+# how we got the last tom crypt sources
+
+VERSION=1.18.0-rc2
+
+cd `git rev-parse --show-toplevel`/src/third_party/tomcrypt-$VERSION
+
+TARBALL=crypt-$VERSION.tar.xz
+if [ ! -f $TARBALL ]; then
+ wget "https://github.com/libtom/libtomcrypt/releases/download/v$VERSION/$TARBALL"
+fi
+
+xzcat $TARBALL | tar -xf-
+
+rm $TARBALL
+
+mv libtomcrypt-$VERSION libtomcrypt-release
+
+rm -rf src
+
+mkdir src
+mkdir src/hashes
+mkdir src/hashes/helper
+mkdir src/hashes/sha2
+mkdir src/mac
+mkdir src/mac/hmac
+mkdir src/misc
+mkdir src/misc/crypt
+
+FILES=(
+ hashes/helper/hash_memory.c
+ hashes/sha1.c
+ hashes/sha2/sha256.c
+ mac/hmac/hmac_done.c
+ mac/hmac/hmac_init.c
+ mac/hmac/hmac_memory.c
+ mac/hmac/hmac_process.c
+ misc/compare_testvector.c
+ misc/crypt/crypt_argchk.c
+ misc/crypt/crypt_find_hash.c
+ misc/crypt/crypt_hash_descriptor.c
+ misc/crypt/crypt_hash_is_valid.c
+ misc/crypt/crypt_register_hash.c
+ misc/zeromem.c)
+
+cp -r libtomcrypt-release/src/headers src
+
+for file in "${FILES[@]}"
+ do cp libtomcrypt-release/src/$file src/$file
+done
diff --git a/src/third_party/shim_tomcrypt.cpp b/src/third_party/shim_tomcrypt.cpp
new file mode 100644
index 00000000000..d9fe3e92ccd
--- /dev/null
+++ b/src/third_party/shim_tomcrypt.cpp
@@ -0,0 +1,3 @@
+// This file intentionally blank. shim_tomcrypt.cpp is part of the
+// third_party/tomcrypt library, which is just a placeholder for forwarding
+// library dependencies.
diff --git a/src/third_party/tomcrypt-1.18.0-rc2/LICENSE b/src/third_party/tomcrypt-1.18.0-rc2/LICENSE
new file mode 100644
index 00000000000..294248fa5d3
--- /dev/null
+++ b/src/third_party/tomcrypt-1.18.0-rc2/LICENSE
@@ -0,0 +1,3 @@
+LibTomCrypt is public domain. As should all quality software be.
+
+Tom St Denis
diff --git a/src/third_party/tomcrypt-1.18.0-rc2/SConscript b/src/third_party/tomcrypt-1.18.0-rc2/SConscript
new file mode 100644
index 00000000000..e7dbfb08ac1
--- /dev/null
+++ b/src/third_party/tomcrypt-1.18.0-rc2/SConscript
@@ -0,0 +1,40 @@
+# -*- mode: python -*-
+
+Import([
+ "env",
+ ])
+
+env = env.Clone()
+
+def removeIfPresent(lst, item):
+ try:
+ lst.remove(item)
+ except ValueError:
+ pass
+
+for to_remove in ['-Werror', '-Wall', '-W']:
+ removeIfPresent(env['CCFLAGS'], to_remove)
+
+env.Prepend(CPPPATH=[
+ 'src/headers',
+])
+
+env.Library(
+ target="tomcrypt",
+ source= [
+ "src/hashes/helper/hash_memory.c",
+ "src/hashes/sha1.c",
+ "src/hashes/sha2/sha256.c",
+ "src/mac/hmac/hmac_done.c",
+ "src/mac/hmac/hmac_init.c",
+ "src/mac/hmac/hmac_memory.c",
+ "src/mac/hmac/hmac_process.c",
+ "src/misc/compare_testvector.c",
+ "src/misc/crypt/crypt_argchk.c",
+ "src/misc/crypt/crypt_find_hash.c",
+ "src/misc/crypt/crypt_hash_descriptor.c",
+ "src/misc/crypt/crypt_hash_is_valid.c",
+ "src/misc/crypt/crypt_register_hash.c",
+ "src/misc/zeromem.c",
+ ],
+)
diff --git a/src/mongo/crypto/tom/hash_memory.c b/src/third_party/tomcrypt-1.18.0-rc2/src/hashes/helper/hash_memory.c
index d0c467d22c9..a7a01eb3262 100644
--- a/src/mongo/crypto/tom/hash_memory.c
+++ b/src/third_party/tomcrypt-1.18.0-rc2/src/hashes/helper/hash_memory.c
@@ -5,11 +5,10 @@
*
* The library is free for all purposes without any express
* guarantee it works.
- *
- * Tom St Denis, tomstdenis@gmail.com, http://libtom.org
*/
#include "tomcrypt.h"
+#ifdef LTC_HASH_HELPERS
/**
@file hash_memory.c
Hash memory helper, Tom St Denis
@@ -20,8 +19,8 @@
@param hash The index of the hash you wish to use
@param in The data you wish to hash
@param inlen The length of the data to hash (octets)
- @param out [out] Where to store the diges
- @param outlen [in/out] Max size and resulting size of the diges
+ @param out [out] Where to store the digest
+ @param outlen [in/out] Max size and resulting size of the digest
@return CRYPT_OK if successful
*/
int hash_memory(int hash, const unsigned char *in, unsigned long inlen, unsigned char *out, unsigned long *outlen)
@@ -42,7 +41,7 @@ int hash_memory(int hash, const unsigned char *in, unsigned long inlen, unsigned
return CRYPT_BUFFER_OVERFLOW;
}
- md = (hash_state*)XMALLOC(sizeof(hash_state));
+ md = XMALLOC(sizeof(hash_state));
if (md == NULL) {
return CRYPT_MEM;
}
@@ -63,7 +62,8 @@ LBL_ERR:
return err;
}
+#endif /* #ifdef LTC_HASH_HELPERS */
-/* $Source: /cvs/libtom/libtomcrypt/src/hashes/helper/hash_memory.c,v $ */
-/* $Revision: 1.6 $ */
-/* $Date: 2006/12/28 01:27:23 $ */
+/* ref: HEAD -> release/1.18.0, tag: v1.18.0-rc2 */
+/* git commit: aa0f396c0c8828ce39456129507fc72ef0208bd0 */
+/* commit time: 2017-07-13 14:58:01 +0200 */
diff --git a/src/mongo/crypto/tom/sha1.c b/src/third_party/tomcrypt-1.18.0-rc2/src/hashes/sha1.c
index 15cac46800b..120da72f1fc 100644
--- a/src/mongo/crypto/tom/sha1.c
+++ b/src/third_party/tomcrypt-1.18.0-rc2/src/hashes/sha1.c
@@ -5,8 +5,6 @@
*
* The library is free for all purposes without any express
* guarantee it works.
- *
- * Tom St Denis, tomstdenis@gmail.com, http://libtom.org
*/
#include "tomcrypt.h"
@@ -185,7 +183,7 @@ int sha1_init(hash_state * md)
HASH_PROCESS(sha1_process, sha1_compress, sha1, 64)
/**
- Terminate the hash to get the diges
+ Terminate the hash to get the digest
@param md The hash state
@param out [out] The destination of the hash (20 bytes)
@return CRYPT_OK if successful
@@ -248,7 +246,7 @@ int sha1_test(void)
return CRYPT_NOP;
#else
static const struct {
- const char *msg;
+ char *msg;
unsigned char hash[20];
} tests[] = {
{ "abc",
@@ -271,7 +269,7 @@ int sha1_test(void)
sha1_init(&md);
sha1_process(&md, (unsigned char*)tests[i].msg, (unsigned long)strlen(tests[i].msg));
sha1_done(&md, tmp);
- if (XMEMCMP(tmp, tests[i].hash, 20) != 0) {
+ if (compare_testvector(tmp, sizeof(tmp), tests[i].hash, sizeof(tests[i].hash), "SHA1", i)) {
return CRYPT_FAIL_TESTVECTOR;
}
}
@@ -283,6 +281,6 @@ int sha1_test(void)
-/* $Source: /cvs/libtom/libtomcrypt/src/hashes/sha1.c,v $ */
-/* $Revision: 1.10 $ */
-/* $Date: 2007/05/12 14:25:28 $ */
+/* ref: HEAD -> release/1.18.0, tag: v1.18.0-rc2 */
+/* git commit: aa0f396c0c8828ce39456129507fc72ef0208bd0 */
+/* commit time: 2017-07-13 14:58:01 +0200 */
diff --git a/src/third_party/tomcrypt-1.18.0-rc2/src/hashes/sha2/sha256.c b/src/third_party/tomcrypt-1.18.0-rc2/src/hashes/sha2/sha256.c
new file mode 100644
index 00000000000..28de8211a07
--- /dev/null
+++ b/src/third_party/tomcrypt-1.18.0-rc2/src/hashes/sha2/sha256.c
@@ -0,0 +1,334 @@
+/* LibTomCrypt, modular cryptographic library -- Tom St Denis
+ *
+ * LibTomCrypt is a library that provides various cryptographic
+ * algorithms in a highly modular and flexible manner.
+ *
+ * The library is free for all purposes without any express
+ * guarantee it works.
+ */
+#include "tomcrypt.h"
+
+/**
+ @file sha256.c
+ LTC_SHA256 by Tom St Denis
+*/
+
+#ifdef LTC_SHA256
+
+const struct ltc_hash_descriptor sha256_desc =
+{
+ "sha256",
+ 0,
+ 32,
+ 64,
+
+ /* OID */
+ { 2, 16, 840, 1, 101, 3, 4, 2, 1, },
+ 9,
+
+ &sha256_init,
+ &sha256_process,
+ &sha256_done,
+ &sha256_test,
+ NULL
+};
+
+#ifdef LTC_SMALL_CODE
+/* the K array */
+static const ulong32 K[64] = {
+ 0x428a2f98UL, 0x71374491UL, 0xb5c0fbcfUL, 0xe9b5dba5UL, 0x3956c25bUL,
+ 0x59f111f1UL, 0x923f82a4UL, 0xab1c5ed5UL, 0xd807aa98UL, 0x12835b01UL,
+ 0x243185beUL, 0x550c7dc3UL, 0x72be5d74UL, 0x80deb1feUL, 0x9bdc06a7UL,
+ 0xc19bf174UL, 0xe49b69c1UL, 0xefbe4786UL, 0x0fc19dc6UL, 0x240ca1ccUL,
+ 0x2de92c6fUL, 0x4a7484aaUL, 0x5cb0a9dcUL, 0x76f988daUL, 0x983e5152UL,
+ 0xa831c66dUL, 0xb00327c8UL, 0xbf597fc7UL, 0xc6e00bf3UL, 0xd5a79147UL,
+ 0x06ca6351UL, 0x14292967UL, 0x27b70a85UL, 0x2e1b2138UL, 0x4d2c6dfcUL,
+ 0x53380d13UL, 0x650a7354UL, 0x766a0abbUL, 0x81c2c92eUL, 0x92722c85UL,
+ 0xa2bfe8a1UL, 0xa81a664bUL, 0xc24b8b70UL, 0xc76c51a3UL, 0xd192e819UL,
+ 0xd6990624UL, 0xf40e3585UL, 0x106aa070UL, 0x19a4c116UL, 0x1e376c08UL,
+ 0x2748774cUL, 0x34b0bcb5UL, 0x391c0cb3UL, 0x4ed8aa4aUL, 0x5b9cca4fUL,
+ 0x682e6ff3UL, 0x748f82eeUL, 0x78a5636fUL, 0x84c87814UL, 0x8cc70208UL,
+ 0x90befffaUL, 0xa4506cebUL, 0xbef9a3f7UL, 0xc67178f2UL
+};
+#endif
+
+/* Various logical functions */
+#define Ch(x,y,z) (z ^ (x & (y ^ z)))
+#define Maj(x,y,z) (((x | y) & z) | (x & y))
+#define S(x, n) RORc((x),(n))
+#define R(x, n) (((x)&0xFFFFFFFFUL)>>(n))
+#define Sigma0(x) (S(x, 2) ^ S(x, 13) ^ S(x, 22))
+#define Sigma1(x) (S(x, 6) ^ S(x, 11) ^ S(x, 25))
+#define Gamma0(x) (S(x, 7) ^ S(x, 18) ^ R(x, 3))
+#define Gamma1(x) (S(x, 17) ^ S(x, 19) ^ R(x, 10))
+
+/* compress 512-bits */
+#ifdef LTC_CLEAN_STACK
+static int _sha256_compress(hash_state * md, unsigned char *buf)
+#else
+static int sha256_compress(hash_state * md, unsigned char *buf)
+#endif
+{
+ ulong32 S[8], W[64], t0, t1;
+#ifdef LTC_SMALL_CODE
+ ulong32 t;
+#endif
+ int i;
+
+ /* copy state into S */
+ for (i = 0; i < 8; i++) {
+ S[i] = md->sha256.state[i];
+ }
+
+ /* copy the state into 512-bits into W[0..15] */
+ for (i = 0; i < 16; i++) {
+ LOAD32H(W[i], buf + (4*i));
+ }
+
+ /* fill W[16..63] */
+ for (i = 16; i < 64; i++) {
+ W[i] = Gamma1(W[i - 2]) + W[i - 7] + Gamma0(W[i - 15]) + W[i - 16];
+ }
+
+ /* Compress */
+#ifdef LTC_SMALL_CODE
+#define RND(a,b,c,d,e,f,g,h,i) \
+ t0 = h + Sigma1(e) + Ch(e, f, g) + K[i] + W[i]; \
+ t1 = Sigma0(a) + Maj(a, b, c); \
+ d += t0; \
+ h = t0 + t1;
+
+ for (i = 0; i < 64; ++i) {
+ RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],i);
+ t = S[7]; S[7] = S[6]; S[6] = S[5]; S[5] = S[4];
+ S[4] = S[3]; S[3] = S[2]; S[2] = S[1]; S[1] = S[0]; S[0] = t;
+ }
+#else
+#define RND(a,b,c,d,e,f,g,h,i,ki) \
+ t0 = h + Sigma1(e) + Ch(e, f, g) + ki + W[i]; \
+ t1 = Sigma0(a) + Maj(a, b, c); \
+ d += t0; \
+ h = t0 + t1;
+
+ RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],0,0x428a2f98);
+ RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],1,0x71374491);
+ RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],2,0xb5c0fbcf);
+ RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],3,0xe9b5dba5);
+ RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],4,0x3956c25b);
+ RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],5,0x59f111f1);
+ RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],6,0x923f82a4);
+ RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],7,0xab1c5ed5);
+ RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],8,0xd807aa98);
+ RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],9,0x12835b01);
+ RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],10,0x243185be);
+ RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],11,0x550c7dc3);
+ RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],12,0x72be5d74);
+ RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],13,0x80deb1fe);
+ RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],14,0x9bdc06a7);
+ RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],15,0xc19bf174);
+ RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],16,0xe49b69c1);
+ RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],17,0xefbe4786);
+ RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],18,0x0fc19dc6);
+ RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],19,0x240ca1cc);
+ RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],20,0x2de92c6f);
+ RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],21,0x4a7484aa);
+ RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],22,0x5cb0a9dc);
+ RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],23,0x76f988da);
+ RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],24,0x983e5152);
+ RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],25,0xa831c66d);
+ RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],26,0xb00327c8);
+ RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],27,0xbf597fc7);
+ RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],28,0xc6e00bf3);
+ RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],29,0xd5a79147);
+ RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],30,0x06ca6351);
+ RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],31,0x14292967);
+ RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],32,0x27b70a85);
+ RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],33,0x2e1b2138);
+ RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],34,0x4d2c6dfc);
+ RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],35,0x53380d13);
+ RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],36,0x650a7354);
+ RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],37,0x766a0abb);
+ RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],38,0x81c2c92e);
+ RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],39,0x92722c85);
+ RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],40,0xa2bfe8a1);
+ RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],41,0xa81a664b);
+ RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],42,0xc24b8b70);
+ RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],43,0xc76c51a3);
+ RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],44,0xd192e819);
+ RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],45,0xd6990624);
+ RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],46,0xf40e3585);
+ RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],47,0x106aa070);
+ RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],48,0x19a4c116);
+ RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],49,0x1e376c08);
+ RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],50,0x2748774c);
+ RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],51,0x34b0bcb5);
+ RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],52,0x391c0cb3);
+ RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],53,0x4ed8aa4a);
+ RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],54,0x5b9cca4f);
+ RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],55,0x682e6ff3);
+ RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],56,0x748f82ee);
+ RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],57,0x78a5636f);
+ RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],58,0x84c87814);
+ RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],59,0x8cc70208);
+ RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],60,0x90befffa);
+ RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],61,0xa4506ceb);
+ RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],62,0xbef9a3f7);
+ RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],63,0xc67178f2);
+
+#undef RND
+
+#endif
+
+ /* feedback */
+ for (i = 0; i < 8; i++) {
+ md->sha256.state[i] = md->sha256.state[i] + S[i];
+ }
+ return CRYPT_OK;
+}
+
+#ifdef LTC_CLEAN_STACK
+static int sha256_compress(hash_state * md, unsigned char *buf)
+{
+ int err;
+ err = _sha256_compress(md, buf);
+ burn_stack(sizeof(ulong32) * 74);
+ return err;
+}
+#endif
+
+/**
+ Initialize the hash state
+ @param md The hash state you wish to initialize
+ @return CRYPT_OK if successful
+*/
+int sha256_init(hash_state * md)
+{
+ LTC_ARGCHK(md != NULL);
+
+ md->sha256.curlen = 0;
+ md->sha256.length = 0;
+ md->sha256.state[0] = 0x6A09E667UL;
+ md->sha256.state[1] = 0xBB67AE85UL;
+ md->sha256.state[2] = 0x3C6EF372UL;
+ md->sha256.state[3] = 0xA54FF53AUL;
+ md->sha256.state[4] = 0x510E527FUL;
+ md->sha256.state[5] = 0x9B05688CUL;
+ md->sha256.state[6] = 0x1F83D9ABUL;
+ md->sha256.state[7] = 0x5BE0CD19UL;
+ return CRYPT_OK;
+}
+
+/**
+ Process a block of memory though the hash
+ @param md The hash state
+ @param in The data to hash
+ @param inlen The length of the data (octets)
+ @return CRYPT_OK if successful
+*/
+HASH_PROCESS(sha256_process, sha256_compress, sha256, 64)
+
+/**
+ Terminate the hash to get the digest
+ @param md The hash state
+ @param out [out] The destination of the hash (32 bytes)
+ @return CRYPT_OK if successful
+*/
+int sha256_done(hash_state * md, unsigned char *out)
+{
+ int i;
+
+ LTC_ARGCHK(md != NULL);
+ LTC_ARGCHK(out != NULL);
+
+ if (md->sha256.curlen >= sizeof(md->sha256.buf)) {
+ return CRYPT_INVALID_ARG;
+ }
+
+
+ /* increase the length of the message */
+ md->sha256.length += md->sha256.curlen * 8;
+
+ /* append the '1' bit */
+ md->sha256.buf[md->sha256.curlen++] = (unsigned char)0x80;
+
+ /* if the length is currently above 56 bytes we append zeros
+ * then compress. Then we can fall back to padding zeros and length
+ * encoding like normal.
+ */
+ if (md->sha256.curlen > 56) {
+ while (md->sha256.curlen < 64) {
+ md->sha256.buf[md->sha256.curlen++] = (unsigned char)0;
+ }
+ sha256_compress(md, md->sha256.buf);
+ md->sha256.curlen = 0;
+ }
+
+ /* pad upto 56 bytes of zeroes */
+ while (md->sha256.curlen < 56) {
+ md->sha256.buf[md->sha256.curlen++] = (unsigned char)0;
+ }
+
+ /* store length */
+ STORE64H(md->sha256.length, md->sha256.buf+56);
+ sha256_compress(md, md->sha256.buf);
+
+ /* copy output */
+ for (i = 0; i < 8; i++) {
+ STORE32H(md->sha256.state[i], out+(4*i));
+ }
+#ifdef LTC_CLEAN_STACK
+ zeromem(md, sizeof(hash_state));
+#endif
+ return CRYPT_OK;
+}
+
+/**
+ Self-test the hash
+ @return CRYPT_OK if successful, CRYPT_NOP if self-tests have been disabled
+*/
+int sha256_test(void)
+{
+ #ifndef LTC_TEST
+ return CRYPT_NOP;
+ #else
+ static const struct {
+ char *msg;
+ unsigned char hash[32];
+ } tests[] = {
+ { "abc",
+ { 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea,
+ 0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23,
+ 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c,
+ 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad }
+ },
+ { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
+ { 0x24, 0x8d, 0x6a, 0x61, 0xd2, 0x06, 0x38, 0xb8,
+ 0xe5, 0xc0, 0x26, 0x93, 0x0c, 0x3e, 0x60, 0x39,
+ 0xa3, 0x3c, 0xe4, 0x59, 0x64, 0xff, 0x21, 0x67,
+ 0xf6, 0xec, 0xed, 0xd4, 0x19, 0xdb, 0x06, 0xc1 }
+ },
+ };
+
+ int i;
+ unsigned char tmp[32];
+ hash_state md;
+
+ for (i = 0; i < (int)(sizeof(tests) / sizeof(tests[0])); i++) {
+ sha256_init(&md);
+ sha256_process(&md, (unsigned char*)tests[i].msg, (unsigned long)strlen(tests[i].msg));
+ sha256_done(&md, tmp);
+ if (compare_testvector(tmp, sizeof(tmp), tests[i].hash, sizeof(tests[i].hash), "SHA256", i)) {
+ return CRYPT_FAIL_TESTVECTOR;
+ }
+ }
+ return CRYPT_OK;
+ #endif
+}
+
+#endif
+
+
+
+/* ref: HEAD -> release/1.18.0, tag: v1.18.0-rc2 */
+/* git commit: aa0f396c0c8828ce39456129507fc72ef0208bd0 */
+/* commit time: 2017-07-13 14:58:01 +0200 */
diff --git a/src/third_party/tomcrypt-1.18.0-rc2/src/headers/tomcrypt.h b/src/third_party/tomcrypt-1.18.0-rc2/src/headers/tomcrypt.h
new file mode 100644
index 00000000000..9df0afb57e3
--- /dev/null
+++ b/src/third_party/tomcrypt-1.18.0-rc2/src/headers/tomcrypt.h
@@ -0,0 +1,102 @@
+/* LibTomCrypt, modular cryptographic library -- Tom St Denis
+ *
+ * LibTomCrypt is a library that provides various cryptographic
+ * algorithms in a highly modular and flexible manner.
+ *
+ * The library is free for all purposes without any express
+ * guarantee it works.
+ */
+
+#ifndef TOMCRYPT_H_
+#define TOMCRYPT_H_
+#include <assert.h>
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <stddef.h>
+#include <time.h>
+#include <ctype.h>
+#include <limits.h>
+
+/* use configuration data */
+#include <tomcrypt_custom.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/* version */
+#define CRYPT 0x0118
+#define SCRYPT "1.18.0-rc2"
+
+/* max size of either a cipher/hash block or symmetric key [largest of the two] */
+#define MAXBLOCKSIZE 128
+
+/* descriptor table size */
+#define TAB_SIZE 32
+
+/* error codes [will be expanded in future releases] */
+enum {
+ CRYPT_OK=0, /* Result OK */
+ CRYPT_ERROR, /* Generic Error */
+ CRYPT_NOP, /* Not a failure but no operation was performed */
+
+ CRYPT_INVALID_KEYSIZE, /* Invalid key size given */
+ CRYPT_INVALID_ROUNDS, /* Invalid number of rounds */
+ CRYPT_FAIL_TESTVECTOR, /* Algorithm failed test vectors */
+
+ CRYPT_BUFFER_OVERFLOW, /* Not enough space for output */
+ CRYPT_INVALID_PACKET, /* Invalid input packet given */
+
+ CRYPT_INVALID_PRNGSIZE, /* Invalid number of bits for a PRNG */
+ CRYPT_ERROR_READPRNG, /* Could not read enough from PRNG */
+
+ CRYPT_INVALID_CIPHER, /* Invalid cipher specified */
+ CRYPT_INVALID_HASH, /* Invalid hash specified */
+ CRYPT_INVALID_PRNG, /* Invalid PRNG specified */
+
+ CRYPT_MEM, /* Out of memory */
+
+ CRYPT_PK_TYPE_MISMATCH, /* Not equivalent types of PK keys */
+ CRYPT_PK_NOT_PRIVATE, /* Requires a private PK key */
+
+ CRYPT_INVALID_ARG, /* Generic invalid argument */
+ CRYPT_FILE_NOTFOUND, /* File Not Found */
+
+ CRYPT_PK_INVALID_TYPE, /* Invalid type of PK key */
+
+ CRYPT_OVERFLOW, /* An overflow of a value was detected/prevented */
+
+ CRYPT_UNUSED1, /* UNUSED1 */
+ CRYPT_UNUSED2, /* UNUSED2 */
+
+ CRYPT_PK_INVALID_SIZE, /* Invalid size input for PK parameters */
+
+ CRYPT_INVALID_PRIME_SIZE,/* Invalid size of prime requested */
+ CRYPT_PK_INVALID_PADDING, /* Invalid padding on input */
+
+ CRYPT_HASH_OVERFLOW /* Hash applied to too many bits */
+};
+
+#include <tomcrypt_cfg.h>
+#include <tomcrypt_macros.h>
+#include <tomcrypt_cipher.h>
+#include <tomcrypt_hash.h>
+#include <tomcrypt_mac.h>
+#include <tomcrypt_prng.h>
+#include <tomcrypt_pk.h>
+#include <tomcrypt_math.h>
+#include <tomcrypt_misc.h>
+#include <tomcrypt_argchk.h>
+#include <tomcrypt_pkcs.h>
+
+#ifdef __cplusplus
+ }
+#endif
+
+#endif /* TOMCRYPT_H_ */
+
+
+/* ref: HEAD -> release/1.18.0, tag: v1.18.0-rc2 */
+/* git commit: aa0f396c0c8828ce39456129507fc72ef0208bd0 */
+/* commit time: 2017-07-13 14:58:01 +0200 */
diff --git a/src/third_party/tomcrypt-1.18.0-rc2/src/headers/tomcrypt_argchk.h b/src/third_party/tomcrypt-1.18.0-rc2/src/headers/tomcrypt_argchk.h
new file mode 100644
index 00000000000..b0786260a39
--- /dev/null
+++ b/src/third_party/tomcrypt-1.18.0-rc2/src/headers/tomcrypt_argchk.h
@@ -0,0 +1,53 @@
+/* LibTomCrypt, modular cryptographic library -- Tom St Denis
+ *
+ * LibTomCrypt is a library that provides various cryptographic
+ * algorithms in a highly modular and flexible manner.
+ *
+ * The library is free for all purposes without any express
+ * guarantee it works.
+ */
+
+/* Defines the LTC_ARGCHK macro used within the library */
+/* ARGTYPE is defined in tomcrypt_cfg.h */
+#if ARGTYPE == 0
+
+#include <signal.h>
+
+/* this is the default LibTomCrypt macro */
+#if defined(__clang__) || defined(__GNUC_MINOR__)
+#define NORETURN __attribute__ ((noreturn))
+#else
+#define NORETURN
+#endif
+
+void crypt_argchk(char *v, char *s, int d) NORETURN;
+#define LTC_ARGCHK(x) do { if (!(x)) { crypt_argchk(#x, __FILE__, __LINE__); } }while(0)
+#define LTC_ARGCHKVD(x) do { if (!(x)) { crypt_argchk(#x, __FILE__, __LINE__); } }while(0)
+
+#elif ARGTYPE == 1
+
+/* fatal type of error */
+#define LTC_ARGCHK(x) assert((x))
+#define LTC_ARGCHKVD(x) LTC_ARGCHK(x)
+
+#elif ARGTYPE == 2
+
+#define LTC_ARGCHK(x) if (!(x)) { fprintf(stderr, "\nwarning: ARGCHK failed at %s:%d\n", __FILE__, __LINE__); }
+#define LTC_ARGCHKVD(x) LTC_ARGCHK(x)
+
+#elif ARGTYPE == 3
+
+#define LTC_ARGCHK(x)
+#define LTC_ARGCHKVD(x) LTC_ARGCHK(x)
+
+#elif ARGTYPE == 4
+
+#define LTC_ARGCHK(x) if (!(x)) return CRYPT_INVALID_ARG;
+#define LTC_ARGCHKVD(x) if (!(x)) return;
+
+#endif
+
+
+/* ref: HEAD -> release/1.18.0, tag: v1.18.0-rc2 */
+/* git commit: aa0f396c0c8828ce39456129507fc72ef0208bd0 */
+/* commit time: 2017-07-13 14:58:01 +0200 */
diff --git a/src/third_party/tomcrypt-1.18.0-rc2/src/headers/tomcrypt_cfg.h b/src/third_party/tomcrypt-1.18.0-rc2/src/headers/tomcrypt_cfg.h
new file mode 100644
index 00000000000..d283636d301
--- /dev/null
+++ b/src/third_party/tomcrypt-1.18.0-rc2/src/headers/tomcrypt_cfg.h
@@ -0,0 +1,277 @@
+/* LibTomCrypt, modular cryptographic library -- Tom St Denis
+ *
+ * LibTomCrypt is a library that provides various cryptographic
+ * algorithms in a highly modular and flexible manner.
+ *
+ * The library is free for all purposes without any express
+ * guarantee it works.
+ */
+
+/* This is the build config file.
+ *
+ * With this you can setup what to inlcude/exclude automatically during any build. Just comment
+ * out the line that #define's the word for the thing you want to remove. phew!
+ */
+
+#ifndef TOMCRYPT_CFG_H
+#define TOMCRYPT_CFG_H
+
+#if defined(_WIN32) || defined(_MSC_VER)
+ #define LTC_CALL __cdecl
+#elif !defined(LTC_CALL)
+ #define LTC_CALL
+#endif
+
+#ifndef LTC_EXPORT
+ #define LTC_EXPORT
+#endif
+
+/* certain platforms use macros for these, making the prototypes broken */
+#ifndef LTC_NO_PROTOTYPES
+
+/* you can change how memory allocation works ... */
+LTC_EXPORT void * LTC_CALL XMALLOC(size_t n);
+LTC_EXPORT void * LTC_CALL XREALLOC(void *p, size_t n);
+LTC_EXPORT void * LTC_CALL XCALLOC(size_t n, size_t s);
+LTC_EXPORT void LTC_CALL XFREE(void *p);
+
+LTC_EXPORT void LTC_CALL XQSORT(void *base, size_t nmemb, size_t size, int(*compar)(const void *, const void *));
+
+
+/* change the clock function too */
+LTC_EXPORT clock_t LTC_CALL XCLOCK(void);
+
+/* various other functions */
+LTC_EXPORT void * LTC_CALL XMEMCPY(void *dest, const void *src, size_t n);
+LTC_EXPORT int LTC_CALL XMEMCMP(const void *s1, const void *s2, size_t n);
+LTC_EXPORT void * LTC_CALL XMEMSET(void *s, int c, size_t n);
+
+LTC_EXPORT int LTC_CALL XSTRCMP(const char *s1, const char *s2);
+
+#endif
+
+/* some compilers do not like "inline" (or maybe "static inline"), namely: HP cc, IBM xlc */
+#if defined(__HP_cc) || defined(__xlc__)
+ #define LTC_INLINE
+#elif defined(_MSC_VER)
+ #define LTC_INLINE __inline
+#else
+ #define LTC_INLINE inline
+#endif
+
+/* type of argument checking, 0=default, 1=fatal and 2=error+continue, 3=nothing */
+#ifndef ARGTYPE
+ #define ARGTYPE 0
+#endif
+
+/* Controls endianess and size of registers. Leave uncommented to get platform neutral [slower] code
+ *
+ * Note: in order to use the optimized macros your platform must support unaligned 32 and 64 bit read/writes.
+ * The x86 platforms allow this but some others [ARM for instance] do not. On those platforms you **MUST**
+ * use the portable [slower] macros.
+ */
+/* detect x86/i386 32bit */
+#if defined(__i386__) || defined(__i386) || defined(_M_IX86)
+ #define ENDIAN_LITTLE
+ #define ENDIAN_32BITWORD
+ #define LTC_FAST
+#endif
+
+/* detect amd64/x64 */
+#if defined(__x86_64__) || defined(_M_X64) || defined(_M_AMD64)
+ #define ENDIAN_LITTLE
+ #define ENDIAN_64BITWORD
+ #define LTC_FAST
+#endif
+
+/* detect PPC32 */
+#if defined(LTC_PPC32)
+ #define ENDIAN_BIG
+ #define ENDIAN_32BITWORD
+ #define LTC_FAST
+#endif
+
+/* detects MIPS R5900 processors (PS2) */
+#if (defined(__R5900) || defined(R5900) || defined(__R5900__)) && (defined(_mips) || defined(__mips__) || defined(mips))
+ #define ENDIAN_64BITWORD
+ #if defined(_MIPSEB) || defined(__MIPSEB) || defined(__MIPSEB__)
+ #define ENDIAN_BIG
+ #endif
+ #define ENDIAN_LITTLE
+ #endif
+#endif
+
+/* detect AIX */
+#if defined(_AIX) && defined(_BIG_ENDIAN)
+ #define ENDIAN_BIG
+ #if defined(__LP64__) || defined(_ARCH_PPC64)
+ #define ENDIAN_64BITWORD
+ #else
+ #define ENDIAN_32BITWORD
+ #endif
+#endif
+
+/* detect HP-UX */
+#if defined(__hpux) || defined(__hpux__)
+ #define ENDIAN_BIG
+ #if defined(__ia64) || defined(__ia64__) || defined(__LP64__)
+ #define ENDIAN_64BITWORD
+ #else
+ #define ENDIAN_32BITWORD
+ #endif
+#endif
+
+/* detect Apple OS X */
+#if defined(__APPLE__) && defined(__MACH__)
+ #if defined(__LITTLE_ENDIAN__) || defined(__x86_64__)
+ #define ENDIAN_LITTLE
+ #else
+ #define ENDIAN_BIG
+ #endif
+ #if defined(__LP64__) || defined(__x86_64__)
+ #define ENDIAN_64BITWORD
+ #else
+ #define ENDIAN_32BITWORD
+ #endif
+#endif
+
+/* detect SPARC and SPARC64 */
+#if defined(__sparc__) || defined(__sparc)
+ #define ENDIAN_BIG
+ #if defined(__arch64__) || defined(__sparcv9) || defined(__sparc_v9__)
+ #define ENDIAN_64BITWORD
+ #else
+ #define ENDIAN_32BITWORD
+ #endif
+#endif
+
+/* detect IBM S390(x) */
+#if defined(__s390x__) || defined(__s390__)
+ #define ENDIAN_BIG
+ #if defined(__s390x__)
+ #define ENDIAN_64BITWORD
+ #else
+ #define ENDIAN_32BITWORD
+ #endif
+#endif
+
+/* detect PPC64 */
+#if defined(__powerpc64__) || defined(__ppc64__) || defined(__PPC64__)
+ #define ENDIAN_64BITWORD
+ #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
+ #define ENDIAN_BIG
+ #elif __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
+ #define ENDIAN_LITTLE
+ #endif
+ #define LTC_FAST
+#endif
+
+/* endianness fallback */
+#if !defined(ENDIAN_BIG) && !defined(ENDIAN_LITTLE)
+ #if defined(__BYTE_ORDER) && __BYTE_ORDER == __BIG_ENDIAN || \
+ defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ || \
+ defined(__BIG_ENDIAN__) || defined(_BIG_ENDIAN) || \
+ defined(__ARMEB__) || defined(__THUMBEB__) || defined(__AARCH64EB__) || \
+ defined(_MIPSEB) || defined(__MIPSEB) || defined(__MIPSEB__)
+ #define ENDIAN_BIG
+ #elif defined(__BYTE_ORDER) && __BYTE_ORDER == __LITTLE_ENDIAN || \
+ defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ || \
+ defined(__LITTLE_ENDIAN__) || defined(_LITTLE_ENDIAN) || \
+ defined(__ARMEL__) || defined(__THUMBEL__) || defined(__AARCH64EL__) || \
+ defined(_MIPSEL) || defined(__MIPSEL) || defined(__MIPSEL__)
+ #define ENDIAN_LITTLE
+ #else
+ #error Cannot detect endianness
+ #endif
+#endif
+
+/* ulong64: 64-bit data type */
+#ifdef _MSC_VER
+ #define CONST64(n) n ## ui64
+ typedef unsigned __int64 ulong64;
+#else
+ #define CONST64(n) n ## ULL
+ typedef unsigned long long ulong64;
+#endif
+
+/* ulong32: "32-bit at least" data type */
+#if defined(__x86_64__) || defined(_M_X64) || defined(_M_AMD64) || \
+ defined(__powerpc64__) || defined(__ppc64__) || defined(__PPC64__) || \
+ defined(__s390x__) || defined(__arch64__) || defined(__aarch64__) || \
+ defined(__sparcv9) || defined(__sparc_v9__) || defined(__sparc64__) || \
+ defined(__ia64) || defined(__ia64__) || defined(__itanium__) || defined(_M_IA64) || \
+ defined(__LP64__) || defined(_LP64) || defined(__64BIT__)
+ typedef unsigned ulong32;
+ #if !defined(ENDIAN_64BITWORD) && !defined(ENDIAN_32BITWORD)
+ #define ENDIAN_64BITWORD
+ #endif
+#else
+ typedef unsigned long ulong32;
+ #if !defined(ENDIAN_64BITWORD) && !defined(ENDIAN_32BITWORD)
+ #define ENDIAN_32BITWORD
+ #endif
+#endif
+
+/* No LTC_FAST if: explicitly disabled OR non-gcc/non-clang compiler OR old gcc OR using -ansi -std=c99 */
+#if defined(LTC_NO_FAST) || (__GNUC__ < 4) || defined(__STRICT_ANSI__)
+ #undef LTC_FAST
+#endif
+
+#ifdef LTC_FAST
+ #define LTC_FAST_TYPE_PTR_CAST(x) ((LTC_FAST_TYPE*)(void*)(x))
+ #ifdef ENDIAN_64BITWORD
+ typedef ulong64 __attribute__((__may_alias__)) LTC_FAST_TYPE;
+ #else
+ typedef ulong32 __attribute__((__may_alias__)) LTC_FAST_TYPE;
+ #endif
+#endif
+
+#ifdef ENDIAN_64BITWORD
+typedef ulong64 ltc_mp_digit;
+#else
+typedef ulong32 ltc_mp_digit;
+#endif
+
+/* No asm is a quick way to disable anything "not portable" */
+#ifdef LTC_NO_ASM
+ #define ENDIAN_NEUTRAL
+ #undef ENDIAN_32BITWORD
+ #undef ENDIAN_64BITWORD
+ #undef LTC_FAST
+ #undef LTC_FAST_TYPE
+ #define LTC_NO_ROLC
+ #define LTC_NO_BSWAP
+#endif
+
+#if !defined(ENDIAN_NEUTRAL) && (defined(ENDIAN_BIG) || defined(ENDIAN_LITTLE)) && !(defined(ENDIAN_32BITWORD) || defined(ENDIAN_64BITWORD))
+ #error You must specify a word size as well as endianess in tomcrypt_cfg.h
+#endif
+
+#if !(defined(ENDIAN_BIG) || defined(ENDIAN_LITTLE))
+ #define ENDIAN_NEUTRAL
+#endif
+
+#if (defined(ENDIAN_32BITWORD) && defined(ENDIAN_64BITWORD))
+ #error Cannot be 32 and 64 bit words...
+#endif
+
+/* gcc 4.3 and up has a bswap builtin; detect it by gcc version.
+ * clang also supports the bswap builtin, and although clang pretends
+ * to be gcc (macro-wise, anyway), clang pretends to be a version
+ * prior to gcc 4.3, so we can't detect bswap that way. Instead,
+ * clang has a __has_builtin mechanism that can be used to check
+ * for builtins:
+ * http://clang.llvm.org/docs/LanguageExtensions.html#feature_check */
+#ifndef __has_builtin
+ #define __has_builtin(x) 0
+#endif
+#if !defined(LTC_NO_BSWAP) && defined(__GNUC__) && \
+ ((__GNUC__ * 100 + __GNUC_MINOR__ >= 403) || \
+ (__has_builtin(__builtin_bswap32) && __has_builtin(__builtin_bswap64)))
+ #define LTC_HAVE_BSWAP_BUILTIN
+#endif
+
+
+/* ref: HEAD -> release/1.18.0, tag: v1.18.0-rc2 */
+/* git commit: aa0f396c0c8828ce39456129507fc72ef0208bd0 */
+/* commit time: 2017-07-13 14:58:01 +0200 */
diff --git a/src/third_party/tomcrypt-1.18.0-rc2/src/headers/tomcrypt_cipher.h b/src/third_party/tomcrypt-1.18.0-rc2/src/headers/tomcrypt_cipher.h
new file mode 100644
index 00000000000..0510aedeaaa
--- /dev/null
+++ b/src/third_party/tomcrypt-1.18.0-rc2/src/headers/tomcrypt_cipher.h
@@ -0,0 +1,1008 @@
+/* LibTomCrypt, modular cryptographic library -- Tom St Denis
+ *
+ * LibTomCrypt is a library that provides various cryptographic
+ * algorithms in a highly modular and flexible manner.
+ *
+ * The library is free for all purposes without any express
+ * guarantee it works.
+ */
+
+/* ---- SYMMETRIC KEY STUFF -----
+ *
+ * We put each of the ciphers scheduled keys in their own structs then we put all of
+ * the key formats in one union. This makes the function prototypes easier to use.
+ */
+#ifdef LTC_BLOWFISH
+struct blowfish_key {
+ ulong32 S[4][256];
+ ulong32 K[18];
+};
+#endif
+
+#ifdef LTC_RC5
+struct rc5_key {
+ int rounds;
+ ulong32 K[50];
+};
+#endif
+
+#ifdef LTC_RC6
+struct rc6_key {
+ ulong32 K[44];
+};
+#endif
+
+#ifdef LTC_SAFERP
+struct saferp_key {
+ unsigned char K[33][16];
+ long rounds;
+};
+#endif
+
+#ifdef LTC_RIJNDAEL
+struct rijndael_key {
+ ulong32 eK[60], dK[60];
+ int Nr;
+};
+#endif
+
+#ifdef LTC_KSEED
+struct kseed_key {
+ ulong32 K[32], dK[32];
+};
+#endif
+
+#ifdef LTC_KASUMI
+struct kasumi_key {
+ ulong32 KLi1[8], KLi2[8],
+ KOi1[8], KOi2[8], KOi3[8],
+ KIi1[8], KIi2[8], KIi3[8];
+};
+#endif
+
+#ifdef LTC_XTEA
+struct xtea_key {
+ unsigned long A[32], B[32];
+};
+#endif
+
+#ifdef LTC_TWOFISH
+#ifndef LTC_TWOFISH_SMALL
+ struct twofish_key {
+ ulong32 S[4][256], K[40];
+ };
+#else
+ struct twofish_key {
+ ulong32 K[40];
+ unsigned char S[32], start;
+ };
+#endif
+#endif
+
+#ifdef LTC_SAFER
+#define LTC_SAFER_K64_DEFAULT_NOF_ROUNDS 6
+#define LTC_SAFER_K128_DEFAULT_NOF_ROUNDS 10
+#define LTC_SAFER_SK64_DEFAULT_NOF_ROUNDS 8
+#define LTC_SAFER_SK128_DEFAULT_NOF_ROUNDS 10
+#define LTC_SAFER_MAX_NOF_ROUNDS 13
+#define LTC_SAFER_BLOCK_LEN 8
+#define LTC_SAFER_KEY_LEN (1 + LTC_SAFER_BLOCK_LEN * (1 + 2 * LTC_SAFER_MAX_NOF_ROUNDS))
+typedef unsigned char safer_block_t[LTC_SAFER_BLOCK_LEN];
+typedef unsigned char safer_key_t[LTC_SAFER_KEY_LEN];
+struct safer_key { safer_key_t key; };
+#endif
+
+#ifdef LTC_RC2
+struct rc2_key { unsigned xkey[64]; };
+#endif
+
+#ifdef LTC_DES
+struct des_key {
+ ulong32 ek[32], dk[32];
+};
+
+struct des3_key {
+ ulong32 ek[3][32], dk[3][32];
+};
+#endif
+
+#ifdef LTC_CAST5
+struct cast5_key {
+ ulong32 K[32], keylen;
+};
+#endif
+
+#ifdef LTC_NOEKEON
+struct noekeon_key {
+ ulong32 K[4], dK[4];
+};
+#endif
+
+#ifdef LTC_SKIPJACK
+struct skipjack_key {
+ unsigned char key[10];
+};
+#endif
+
+#ifdef LTC_KHAZAD
+struct khazad_key {
+ ulong64 roundKeyEnc[8 + 1];
+ ulong64 roundKeyDec[8 + 1];
+};
+#endif
+
+#ifdef LTC_ANUBIS
+struct anubis_key {
+ int keyBits;
+ int R;
+ ulong32 roundKeyEnc[18 + 1][4];
+ ulong32 roundKeyDec[18 + 1][4];
+};
+#endif
+
+#ifdef LTC_MULTI2
+struct multi2_key {
+ int N;
+ ulong32 uk[8];
+};
+#endif
+
+#ifdef LTC_CAMELLIA
+struct camellia_key {
+ int R;
+ ulong64 kw[4], k[24], kl[6];
+};
+#endif
+
+typedef union Symmetric_key {
+#ifdef LTC_DES
+ struct des_key des;
+ struct des3_key des3;
+#endif
+#ifdef LTC_RC2
+ struct rc2_key rc2;
+#endif
+#ifdef LTC_SAFER
+ struct safer_key safer;
+#endif
+#ifdef LTC_TWOFISH
+ struct twofish_key twofish;
+#endif
+#ifdef LTC_BLOWFISH
+ struct blowfish_key blowfish;
+#endif
+#ifdef LTC_RC5
+ struct rc5_key rc5;
+#endif
+#ifdef LTC_RC6
+ struct rc6_key rc6;
+#endif
+#ifdef LTC_SAFERP
+ struct saferp_key saferp;
+#endif
+#ifdef LTC_RIJNDAEL
+ struct rijndael_key rijndael;
+#endif
+#ifdef LTC_XTEA
+ struct xtea_key xtea;
+#endif
+#ifdef LTC_CAST5
+ struct cast5_key cast5;
+#endif
+#ifdef LTC_NOEKEON
+ struct noekeon_key noekeon;
+#endif
+#ifdef LTC_SKIPJACK
+ struct skipjack_key skipjack;
+#endif
+#ifdef LTC_KHAZAD
+ struct khazad_key khazad;
+#endif
+#ifdef LTC_ANUBIS
+ struct anubis_key anubis;
+#endif
+#ifdef LTC_KSEED
+ struct kseed_key kseed;
+#endif
+#ifdef LTC_KASUMI
+ struct kasumi_key kasumi;
+#endif
+#ifdef LTC_MULTI2
+ struct multi2_key multi2;
+#endif
+#ifdef LTC_CAMELLIA
+ struct camellia_key camellia;
+#endif
+ void *data;
+} symmetric_key;
+
+#ifdef LTC_ECB_MODE
+/** A block cipher ECB structure */
+typedef struct {
+ /** The index of the cipher chosen */
+ int cipher,
+ /** The block size of the given cipher */
+ blocklen;
+ /** The scheduled key */
+ symmetric_key key;
+} symmetric_ECB;
+#endif
+
+#ifdef LTC_CFB_MODE
+/** A block cipher CFB structure */
+typedef struct {
+ /** The index of the cipher chosen */
+ int cipher,
+ /** The block size of the given cipher */
+ blocklen,
+ /** The padding offset */
+ padlen;
+ /** The current IV */
+ unsigned char IV[MAXBLOCKSIZE],
+ /** The pad used to encrypt/decrypt */
+ pad[MAXBLOCKSIZE];
+ /** The scheduled key */
+ symmetric_key key;
+} symmetric_CFB;
+#endif
+
+#ifdef LTC_OFB_MODE
+/** A block cipher OFB structure */
+typedef struct {
+ /** The index of the cipher chosen */
+ int cipher,
+ /** The block size of the given cipher */
+ blocklen,
+ /** The padding offset */
+ padlen;
+ /** The current IV */
+ unsigned char IV[MAXBLOCKSIZE];
+ /** The scheduled key */
+ symmetric_key key;
+} symmetric_OFB;
+#endif
+
+#ifdef LTC_CBC_MODE
+/** A block cipher CBC structure */
+typedef struct {
+ /** The index of the cipher chosen */
+ int cipher,
+ /** The block size of the given cipher */
+ blocklen;
+ /** The current IV */
+ unsigned char IV[MAXBLOCKSIZE];
+ /** The scheduled key */
+ symmetric_key key;
+} symmetric_CBC;
+#endif
+
+
+#ifdef LTC_CTR_MODE
+/** A block cipher CTR structure */
+typedef struct {
+ /** The index of the cipher chosen */
+ int cipher,
+ /** The block size of the given cipher */
+ blocklen,
+ /** The padding offset */
+ padlen,
+ /** The mode (endianess) of the CTR, 0==little, 1==big */
+ mode,
+ /** counter width */
+ ctrlen;
+
+ /** The counter */
+ unsigned char ctr[MAXBLOCKSIZE],
+ /** The pad used to encrypt/decrypt */
+ pad[MAXBLOCKSIZE];
+ /** The scheduled key */
+ symmetric_key key;
+} symmetric_CTR;
+#endif
+
+
+#ifdef LTC_LRW_MODE
+/** A LRW structure */
+typedef struct {
+ /** The index of the cipher chosen (must be a 128-bit block cipher) */
+ int cipher;
+
+ /** The current IV */
+ unsigned char IV[16],
+
+ /** the tweak key */
+ tweak[16],
+
+ /** The current pad, it's the product of the first 15 bytes against the tweak key */
+ pad[16];
+
+ /** The scheduled symmetric key */
+ symmetric_key key;
+
+#ifdef LTC_LRW_TABLES
+ /** The pre-computed multiplication table */
+ unsigned char PC[16][256][16];
+#endif
+} symmetric_LRW;
+#endif
+
+#ifdef LTC_F8_MODE
+/** A block cipher F8 structure */
+typedef struct {
+ /** The index of the cipher chosen */
+ int cipher,
+ /** The block size of the given cipher */
+ blocklen,
+ /** The padding offset */
+ padlen;
+ /** The current IV */
+ unsigned char IV[MAXBLOCKSIZE],
+ MIV[MAXBLOCKSIZE];
+ /** Current block count */
+ ulong32 blockcnt;
+ /** The scheduled key */
+ symmetric_key key;
+} symmetric_F8;
+#endif
+
+
+/** cipher descriptor table, last entry has "name == NULL" to mark the end of table */
+extern struct ltc_cipher_descriptor {
+ /** name of cipher */
+ char *name;
+ /** internal ID */
+ unsigned char ID;
+ /** min keysize (octets) */
+ int min_key_length,
+ /** max keysize (octets) */
+ max_key_length,
+ /** block size (octets) */
+ block_length,
+ /** default number of rounds */
+ default_rounds;
+ /** Setup the cipher
+ @param key The input symmetric key
+ @param keylen The length of the input key (octets)
+ @param num_rounds The requested number of rounds (0==default)
+ @param skey [out] The destination of the scheduled key
+ @return CRYPT_OK if successful
+ */
+ int (*setup)(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey);
+ /** Encrypt a block
+ @param pt The plaintext
+ @param ct [out] The ciphertext
+ @param skey The scheduled key
+ @return CRYPT_OK if successful
+ */
+ int (*ecb_encrypt)(const unsigned char *pt, unsigned char *ct, symmetric_key *skey);
+ /** Decrypt a block
+ @param ct The ciphertext
+ @param pt [out] The plaintext
+ @param skey The scheduled key
+ @return CRYPT_OK if successful
+ */
+ int (*ecb_decrypt)(const unsigned char *ct, unsigned char *pt, symmetric_key *skey);
+ /** Test the block cipher
+ @return CRYPT_OK if successful, CRYPT_NOP if self-testing has been disabled
+ */
+ int (*test)(void);
+
+ /** Terminate the context
+ @param skey The scheduled key
+ */
+ void (*done)(symmetric_key *skey);
+
+ /** Determine a key size
+ @param keysize [in/out] The size of the key desired and the suggested size
+ @return CRYPT_OK if successful
+ */
+ int (*keysize)(int *keysize);
+
+/** Accelerators **/
+ /** Accelerated ECB encryption
+ @param pt Plaintext
+ @param ct Ciphertext
+ @param blocks The number of complete blocks to process
+ @param skey The scheduled key context
+ @return CRYPT_OK if successful
+ */
+ int (*accel_ecb_encrypt)(const unsigned char *pt, unsigned char *ct, unsigned long blocks, symmetric_key *skey);
+
+ /** Accelerated ECB decryption
+ @param pt Plaintext
+ @param ct Ciphertext
+ @param blocks The number of complete blocks to process
+ @param skey The scheduled key context
+ @return CRYPT_OK if successful
+ */
+ int (*accel_ecb_decrypt)(const unsigned char *ct, unsigned char *pt, unsigned long blocks, symmetric_key *skey);
+
+ /** Accelerated CBC encryption
+ @param pt Plaintext
+ @param ct Ciphertext
+ @param blocks The number of complete blocks to process
+ @param IV The initial value (input/output)
+ @param skey The scheduled key context
+ @return CRYPT_OK if successful
+ */
+ int (*accel_cbc_encrypt)(const unsigned char *pt, unsigned char *ct, unsigned long blocks, unsigned char *IV, symmetric_key *skey);
+
+ /** Accelerated CBC decryption
+ @param pt Plaintext
+ @param ct Ciphertext
+ @param blocks The number of complete blocks to process
+ @param IV The initial value (input/output)
+ @param skey The scheduled key context
+ @return CRYPT_OK if successful
+ */
+ int (*accel_cbc_decrypt)(const unsigned char *ct, unsigned char *pt, unsigned long blocks, unsigned char *IV, symmetric_key *skey);
+
+ /** Accelerated CTR encryption
+ @param pt Plaintext
+ @param ct Ciphertext
+ @param blocks The number of complete blocks to process
+ @param IV The initial value (input/output)
+ @param mode little or big endian counter (mode=0 or mode=1)
+ @param skey The scheduled key context
+ @return CRYPT_OK if successful
+ */
+ int (*accel_ctr_encrypt)(const unsigned char *pt, unsigned char *ct, unsigned long blocks, unsigned char *IV, int mode, symmetric_key *skey);
+
+ /** Accelerated LRW
+ @param pt Plaintext
+ @param ct Ciphertext
+ @param blocks The number of complete blocks to process
+ @param IV The initial value (input/output)
+ @param tweak The LRW tweak
+ @param skey The scheduled key context
+ @return CRYPT_OK if successful
+ */
+ int (*accel_lrw_encrypt)(const unsigned char *pt, unsigned char *ct, unsigned long blocks, unsigned char *IV, const unsigned char *tweak, symmetric_key *skey);
+
+ /** Accelerated LRW
+ @param ct Ciphertext
+ @param pt Plaintext
+ @param blocks The number of complete blocks to process
+ @param IV The initial value (input/output)
+ @param tweak The LRW tweak
+ @param skey The scheduled key context
+ @return CRYPT_OK if successful
+ */
+ int (*accel_lrw_decrypt)(const unsigned char *ct, unsigned char *pt, unsigned long blocks, unsigned char *IV, const unsigned char *tweak, symmetric_key *skey);
+
+ /** Accelerated CCM packet (one-shot)
+ @param key The secret key to use
+ @param keylen The length of the secret key (octets)
+ @param uskey A previously scheduled key [optional can be NULL]
+ @param nonce The session nonce [use once]
+ @param noncelen The length of the nonce
+ @param header The header for the session
+ @param headerlen The length of the header (octets)
+ @param pt [out] The plaintext
+ @param ptlen The length of the plaintext (octets)
+ @param ct [out] The ciphertext
+ @param tag [out] The destination tag
+ @param taglen [in/out] The max size and resulting size of the authentication tag
+ @param direction Encrypt or Decrypt direction (0 or 1)
+ @return CRYPT_OK if successful
+ */
+ int (*accel_ccm_memory)(
+ const unsigned char *key, unsigned long keylen,
+ symmetric_key *uskey,
+ const unsigned char *nonce, unsigned long noncelen,
+ const unsigned char *header, unsigned long headerlen,
+ unsigned char *pt, unsigned long ptlen,
+ unsigned char *ct,
+ unsigned char *tag, unsigned long *taglen,
+ int direction);
+
+ /** Accelerated GCM packet (one shot)
+ @param key The secret key
+ @param keylen The length of the secret key
+ @param IV The initial vector
+ @param IVlen The length of the initial vector
+ @param adata The additional authentication data (header)
+ @param adatalen The length of the adata
+ @param pt The plaintext
+ @param ptlen The length of the plaintext (ciphertext length is the same)
+ @param ct The ciphertext
+ @param tag [out] The MAC tag
+ @param taglen [in/out] The MAC tag length
+ @param direction Encrypt or Decrypt mode (GCM_ENCRYPT or GCM_DECRYPT)
+ @return CRYPT_OK on success
+ */
+ int (*accel_gcm_memory)(
+ const unsigned char *key, unsigned long keylen,
+ const unsigned char *IV, unsigned long IVlen,
+ const unsigned char *adata, unsigned long adatalen,
+ unsigned char *pt, unsigned long ptlen,
+ unsigned char *ct,
+ unsigned char *tag, unsigned long *taglen,
+ int direction);
+
+ /** Accelerated one shot LTC_OMAC
+ @param key The secret key
+ @param keylen The key length (octets)
+ @param in The message
+ @param inlen Length of message (octets)
+ @param out [out] Destination for tag
+ @param outlen [in/out] Initial and final size of out
+ @return CRYPT_OK on success
+ */
+ int (*omac_memory)(
+ const unsigned char *key, unsigned long keylen,
+ const unsigned char *in, unsigned long inlen,
+ unsigned char *out, unsigned long *outlen);
+
+ /** Accelerated one shot XCBC
+ @param key The secret key
+ @param keylen The key length (octets)
+ @param in The message
+ @param inlen Length of message (octets)
+ @param out [out] Destination for tag
+ @param outlen [in/out] Initial and final size of out
+ @return CRYPT_OK on success
+ */
+ int (*xcbc_memory)(
+ const unsigned char *key, unsigned long keylen,
+ const unsigned char *in, unsigned long inlen,
+ unsigned char *out, unsigned long *outlen);
+
+ /** Accelerated one shot F9
+ @param key The secret key
+ @param keylen The key length (octets)
+ @param in The message
+ @param inlen Length of message (octets)
+ @param out [out] Destination for tag
+ @param outlen [in/out] Initial and final size of out
+ @return CRYPT_OK on success
+ @remark Requires manual padding
+ */
+ int (*f9_memory)(
+ const unsigned char *key, unsigned long keylen,
+ const unsigned char *in, unsigned long inlen,
+ unsigned char *out, unsigned long *outlen);
+
+ /** Accelerated XTS encryption
+ @param pt Plaintext
+ @param ct Ciphertext
+ @param blocks The number of complete blocks to process
+ @param tweak The 128-bit encryption tweak (input/output).
+ The tweak should not be encrypted on input, but
+ next tweak will be copied encrypted on output.
+ @param skey1 The first scheduled key context
+ @param skey2 The second scheduled key context
+ @return CRYPT_OK if successful
+ */
+ int (*accel_xts_encrypt)(const unsigned char *pt, unsigned char *ct,
+ unsigned long blocks, unsigned char *tweak, symmetric_key *skey1,
+ symmetric_key *skey2);
+
+ /** Accelerated XTS decryption
+ @param ct Ciphertext
+ @param pt Plaintext
+ @param blocks The number of complete blocks to process
+ @param tweak The 128-bit encryption tweak (input/output).
+ The tweak should not be encrypted on input, but
+ next tweak will be copied encrypted on output.
+ @param skey1 The first scheduled key context
+ @param skey2 The second scheduled key context
+ @return CRYPT_OK if successful
+ */
+ int (*accel_xts_decrypt)(const unsigned char *ct, unsigned char *pt,
+ unsigned long blocks, unsigned char *tweak, symmetric_key *skey1,
+ symmetric_key *skey2);
+} cipher_descriptor[];
+
+#ifdef LTC_BLOWFISH
+int blowfish_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey);
+int blowfish_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey);
+int blowfish_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey);
+int blowfish_test(void);
+void blowfish_done(symmetric_key *skey);
+int blowfish_keysize(int *keysize);
+extern const struct ltc_cipher_descriptor blowfish_desc;
+#endif
+
+#ifdef LTC_RC5
+int rc5_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey);
+int rc5_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey);
+int rc5_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey);
+int rc5_test(void);
+void rc5_done(symmetric_key *skey);
+int rc5_keysize(int *keysize);
+extern const struct ltc_cipher_descriptor rc5_desc;
+#endif
+
+#ifdef LTC_RC6
+int rc6_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey);
+int rc6_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey);
+int rc6_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey);
+int rc6_test(void);
+void rc6_done(symmetric_key *skey);
+int rc6_keysize(int *keysize);
+extern const struct ltc_cipher_descriptor rc6_desc;
+#endif
+
+#ifdef LTC_RC2
+int rc2_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey);
+int rc2_setup_ex(const unsigned char *key, int keylen, int bits, int num_rounds, symmetric_key *skey);
+int rc2_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey);
+int rc2_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey);
+int rc2_test(void);
+void rc2_done(symmetric_key *skey);
+int rc2_keysize(int *keysize);
+extern const struct ltc_cipher_descriptor rc2_desc;
+#endif
+
+#ifdef LTC_SAFERP
+int saferp_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey);
+int saferp_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey);
+int saferp_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey);
+int saferp_test(void);
+void saferp_done(symmetric_key *skey);
+int saferp_keysize(int *keysize);
+extern const struct ltc_cipher_descriptor saferp_desc;
+#endif
+
+#ifdef LTC_SAFER
+int safer_k64_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey);
+int safer_sk64_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey);
+int safer_k128_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey);
+int safer_sk128_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey);
+int safer_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *key);
+int safer_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *key);
+int safer_k64_test(void);
+int safer_sk64_test(void);
+int safer_sk128_test(void);
+void safer_done(symmetric_key *skey);
+int safer_64_keysize(int *keysize);
+int safer_128_keysize(int *keysize);
+extern const struct ltc_cipher_descriptor safer_k64_desc, safer_k128_desc, safer_sk64_desc, safer_sk128_desc;
+#endif
+
+#ifdef LTC_RIJNDAEL
+
+/* make aes an alias */
+#define aes_setup rijndael_setup
+#define aes_ecb_encrypt rijndael_ecb_encrypt
+#define aes_ecb_decrypt rijndael_ecb_decrypt
+#define aes_test rijndael_test
+#define aes_done rijndael_done
+#define aes_keysize rijndael_keysize
+
+#define aes_enc_setup rijndael_enc_setup
+#define aes_enc_ecb_encrypt rijndael_enc_ecb_encrypt
+#define aes_enc_keysize rijndael_enc_keysize
+
+int rijndael_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey);
+int rijndael_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey);
+int rijndael_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey);
+int rijndael_test(void);
+void rijndael_done(symmetric_key *skey);
+int rijndael_keysize(int *keysize);
+int rijndael_enc_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey);
+int rijndael_enc_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey);
+void rijndael_enc_done(symmetric_key *skey);
+int rijndael_enc_keysize(int *keysize);
+extern const struct ltc_cipher_descriptor rijndael_desc, aes_desc;
+extern const struct ltc_cipher_descriptor rijndael_enc_desc, aes_enc_desc;
+#endif
+
+#ifdef LTC_XTEA
+int xtea_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey);
+int xtea_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey);
+int xtea_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey);
+int xtea_test(void);
+void xtea_done(symmetric_key *skey);
+int xtea_keysize(int *keysize);
+extern const struct ltc_cipher_descriptor xtea_desc;
+#endif
+
+#ifdef LTC_TWOFISH
+int twofish_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey);
+int twofish_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey);
+int twofish_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey);
+int twofish_test(void);
+void twofish_done(symmetric_key *skey);
+int twofish_keysize(int *keysize);
+extern const struct ltc_cipher_descriptor twofish_desc;
+#endif
+
+#ifdef LTC_DES
+int des_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey);
+int des_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey);
+int des_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey);
+int des_test(void);
+void des_done(symmetric_key *skey);
+int des_keysize(int *keysize);
+int des3_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey);
+int des3_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey);
+int des3_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey);
+int des3_test(void);
+void des3_done(symmetric_key *skey);
+int des3_keysize(int *keysize);
+extern const struct ltc_cipher_descriptor des_desc, des3_desc;
+#endif
+
+#ifdef LTC_CAST5
+int cast5_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey);
+int cast5_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey);
+int cast5_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey);
+int cast5_test(void);
+void cast5_done(symmetric_key *skey);
+int cast5_keysize(int *keysize);
+extern const struct ltc_cipher_descriptor cast5_desc;
+#endif
+
+#ifdef LTC_NOEKEON
+int noekeon_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey);
+int noekeon_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey);
+int noekeon_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey);
+int noekeon_test(void);
+void noekeon_done(symmetric_key *skey);
+int noekeon_keysize(int *keysize);
+extern const struct ltc_cipher_descriptor noekeon_desc;
+#endif
+
+#ifdef LTC_SKIPJACK
+int skipjack_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey);
+int skipjack_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey);
+int skipjack_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey);
+int skipjack_test(void);
+void skipjack_done(symmetric_key *skey);
+int skipjack_keysize(int *keysize);
+extern const struct ltc_cipher_descriptor skipjack_desc;
+#endif
+
+#ifdef LTC_KHAZAD
+int khazad_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey);
+int khazad_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey);
+int khazad_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey);
+int khazad_test(void);
+void khazad_done(symmetric_key *skey);
+int khazad_keysize(int *keysize);
+extern const struct ltc_cipher_descriptor khazad_desc;
+#endif
+
+#ifdef LTC_ANUBIS
+int anubis_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey);
+int anubis_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey);
+int anubis_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey);
+int anubis_test(void);
+void anubis_done(symmetric_key *skey);
+int anubis_keysize(int *keysize);
+extern const struct ltc_cipher_descriptor anubis_desc;
+#endif
+
+#ifdef LTC_KSEED
+int kseed_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey);
+int kseed_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey);
+int kseed_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey);
+int kseed_test(void);
+void kseed_done(symmetric_key *skey);
+int kseed_keysize(int *keysize);
+extern const struct ltc_cipher_descriptor kseed_desc;
+#endif
+
+#ifdef LTC_KASUMI
+int kasumi_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey);
+int kasumi_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey);
+int kasumi_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey);
+int kasumi_test(void);
+void kasumi_done(symmetric_key *skey);
+int kasumi_keysize(int *keysize);
+extern const struct ltc_cipher_descriptor kasumi_desc;
+#endif
+
+
+#ifdef LTC_MULTI2
+int multi2_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey);
+int multi2_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey);
+int multi2_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey);
+int multi2_test(void);
+void multi2_done(symmetric_key *skey);
+int multi2_keysize(int *keysize);
+extern const struct ltc_cipher_descriptor multi2_desc;
+#endif
+
+#ifdef LTC_CAMELLIA
+int camellia_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey);
+int camellia_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey);
+int camellia_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey);
+int camellia_test(void);
+void camellia_done(symmetric_key *skey);
+int camellia_keysize(int *keysize);
+extern const struct ltc_cipher_descriptor camellia_desc;
+#endif
+
+#ifdef LTC_ECB_MODE
+int ecb_start(int cipher, const unsigned char *key,
+ int keylen, int num_rounds, symmetric_ECB *ecb);
+int ecb_encrypt(const unsigned char *pt, unsigned char *ct, unsigned long len, symmetric_ECB *ecb);
+int ecb_decrypt(const unsigned char *ct, unsigned char *pt, unsigned long len, symmetric_ECB *ecb);
+int ecb_done(symmetric_ECB *ecb);
+#endif
+
+#ifdef LTC_CFB_MODE
+int cfb_start(int cipher, const unsigned char *IV, const unsigned char *key,
+ int keylen, int num_rounds, symmetric_CFB *cfb);
+int cfb_encrypt(const unsigned char *pt, unsigned char *ct, unsigned long len, symmetric_CFB *cfb);
+int cfb_decrypt(const unsigned char *ct, unsigned char *pt, unsigned long len, symmetric_CFB *cfb);
+int cfb_getiv(unsigned char *IV, unsigned long *len, symmetric_CFB *cfb);
+int cfb_setiv(const unsigned char *IV, unsigned long len, symmetric_CFB *cfb);
+int cfb_done(symmetric_CFB *cfb);
+#endif
+
+#ifdef LTC_OFB_MODE
+int ofb_start(int cipher, const unsigned char *IV, const unsigned char *key,
+ int keylen, int num_rounds, symmetric_OFB *ofb);
+int ofb_encrypt(const unsigned char *pt, unsigned char *ct, unsigned long len, symmetric_OFB *ofb);
+int ofb_decrypt(const unsigned char *ct, unsigned char *pt, unsigned long len, symmetric_OFB *ofb);
+int ofb_getiv(unsigned char *IV, unsigned long *len, symmetric_OFB *ofb);
+int ofb_setiv(const unsigned char *IV, unsigned long len, symmetric_OFB *ofb);
+int ofb_done(symmetric_OFB *ofb);
+#endif
+
+#ifdef LTC_CBC_MODE
+int cbc_start(int cipher, const unsigned char *IV, const unsigned char *key,
+ int keylen, int num_rounds, symmetric_CBC *cbc);
+int cbc_encrypt(const unsigned char *pt, unsigned char *ct, unsigned long len, symmetric_CBC *cbc);
+int cbc_decrypt(const unsigned char *ct, unsigned char *pt, unsigned long len, symmetric_CBC *cbc);
+int cbc_getiv(unsigned char *IV, unsigned long *len, symmetric_CBC *cbc);
+int cbc_setiv(const unsigned char *IV, unsigned long len, symmetric_CBC *cbc);
+int cbc_done(symmetric_CBC *cbc);
+#endif
+
+#ifdef LTC_CTR_MODE
+
+#define CTR_COUNTER_LITTLE_ENDIAN 0x0000
+#define CTR_COUNTER_BIG_ENDIAN 0x1000
+#define LTC_CTR_RFC3686 0x2000
+
+int ctr_start( int cipher,
+ const unsigned char *IV,
+ const unsigned char *key, int keylen,
+ int num_rounds, int ctr_mode,
+ symmetric_CTR *ctr);
+int ctr_encrypt(const unsigned char *pt, unsigned char *ct, unsigned long len, symmetric_CTR *ctr);
+int ctr_decrypt(const unsigned char *ct, unsigned char *pt, unsigned long len, symmetric_CTR *ctr);
+int ctr_getiv(unsigned char *IV, unsigned long *len, symmetric_CTR *ctr);
+int ctr_setiv(const unsigned char *IV, unsigned long len, symmetric_CTR *ctr);
+int ctr_done(symmetric_CTR *ctr);
+int ctr_test(void);
+#endif
+
+#ifdef LTC_LRW_MODE
+
+#define LRW_ENCRYPT 0
+#define LRW_DECRYPT 1
+
+int lrw_start( int cipher,
+ const unsigned char *IV,
+ const unsigned char *key, int keylen,
+ const unsigned char *tweak,
+ int num_rounds,
+ symmetric_LRW *lrw);
+int lrw_encrypt(const unsigned char *pt, unsigned char *ct, unsigned long len, symmetric_LRW *lrw);
+int lrw_decrypt(const unsigned char *ct, unsigned char *pt, unsigned long len, symmetric_LRW *lrw);
+int lrw_getiv(unsigned char *IV, unsigned long *len, symmetric_LRW *lrw);
+int lrw_setiv(const unsigned char *IV, unsigned long len, symmetric_LRW *lrw);
+int lrw_done(symmetric_LRW *lrw);
+int lrw_test(void);
+
+/* don't call */
+int lrw_process(const unsigned char *pt, unsigned char *ct, unsigned long len, int mode, symmetric_LRW *lrw);
+#endif
+
+#ifdef LTC_F8_MODE
+int f8_start( int cipher, const unsigned char *IV,
+ const unsigned char *key, int keylen,
+ const unsigned char *salt_key, int skeylen,
+ int num_rounds, symmetric_F8 *f8);
+int f8_encrypt(const unsigned char *pt, unsigned char *ct, unsigned long len, symmetric_F8 *f8);
+int f8_decrypt(const unsigned char *ct, unsigned char *pt, unsigned long len, symmetric_F8 *f8);
+int f8_getiv(unsigned char *IV, unsigned long *len, symmetric_F8 *f8);
+int f8_setiv(const unsigned char *IV, unsigned long len, symmetric_F8 *f8);
+int f8_done(symmetric_F8 *f8);
+int f8_test_mode(void);
+#endif
+
+#ifdef LTC_XTS_MODE
+typedef struct {
+ symmetric_key key1, key2;
+ int cipher;
+} symmetric_xts;
+
+int xts_start( int cipher,
+ const unsigned char *key1,
+ const unsigned char *key2,
+ unsigned long keylen,
+ int num_rounds,
+ symmetric_xts *xts);
+
+int xts_encrypt(
+ const unsigned char *pt, unsigned long ptlen,
+ unsigned char *ct,
+ unsigned char *tweak,
+ symmetric_xts *xts);
+int xts_decrypt(
+ const unsigned char *ct, unsigned long ptlen,
+ unsigned char *pt,
+ unsigned char *tweak,
+ symmetric_xts *xts);
+
+void xts_done(symmetric_xts *xts);
+int xts_test(void);
+void xts_mult_x(unsigned char *I);
+#endif
+
+int find_cipher(const char *name);
+int find_cipher_any(const char *name, int blocklen, int keylen);
+int find_cipher_id(unsigned char ID);
+int register_cipher(const struct ltc_cipher_descriptor *cipher);
+int unregister_cipher(const struct ltc_cipher_descriptor *cipher);
+int register_all_ciphers(void);
+int cipher_is_valid(int idx);
+
+LTC_MUTEX_PROTO(ltc_cipher_mutex)
+
+/* ---- stream ciphers ---- */
+
+#ifdef LTC_CHACHA
+
+typedef struct {
+ ulong32 input[16];
+ unsigned char kstream[64];
+ unsigned long ksleft;
+ unsigned long ivlen;
+ int rounds;
+} chacha_state;
+
+int chacha_setup(chacha_state *st, const unsigned char *key, unsigned long keylen, int rounds);
+int chacha_ivctr32(chacha_state *st, const unsigned char *iv, unsigned long ivlen, ulong32 counter);
+int chacha_ivctr64(chacha_state *st, const unsigned char *iv, unsigned long ivlen, ulong64 counter);
+int chacha_crypt(chacha_state *st, const unsigned char *in, unsigned long inlen, unsigned char *out);
+int chacha_keystream(chacha_state *st, unsigned char *out, unsigned long outlen);
+int chacha_done(chacha_state *st);
+int chacha_test(void);
+
+#endif /* LTC_CHACHA */
+
+#ifdef LTC_RC4_STREAM
+
+typedef struct {
+ unsigned int x, y;
+ unsigned char buf[256];
+} rc4_state;
+
+int rc4_stream_setup(rc4_state *st, const unsigned char *key, unsigned long keylen);
+int rc4_stream_crypt(rc4_state *st, const unsigned char *in, unsigned long inlen, unsigned char *out);
+int rc4_stream_keystream(rc4_state *st, unsigned char *out, unsigned long outlen);
+int rc4_stream_done(rc4_state *st);
+int rc4_stream_test(void);
+
+#endif /* LTC_RC4_STREAM */
+
+#ifdef LTC_SOBER128_STREAM
+
+typedef struct {
+ ulong32 R[17], /* Working storage for the shift register */
+ initR[17], /* saved register contents */
+ konst, /* key dependent constant */
+ sbuf; /* partial word encryption buffer */
+ int nbuf; /* number of part-word stream bits buffered */
+} sober128_state;
+
+int sober128_stream_setup(sober128_state *st, const unsigned char *key, unsigned long keylen);
+int sober128_stream_setiv(sober128_state *st, const unsigned char *iv, unsigned long ivlen);
+int sober128_stream_crypt(sober128_state *st, const unsigned char *in, unsigned long inlen, unsigned char *out);
+int sober128_stream_keystream(sober128_state *st, unsigned char *out, unsigned long outlen);
+int sober128_stream_done(sober128_state *st);
+int sober128_stream_test(void);
+
+#endif /* LTC_SOBER128_STREAM */
+
+/* ref: HEAD -> release/1.18.0, tag: v1.18.0-rc2 */
+/* git commit: aa0f396c0c8828ce39456129507fc72ef0208bd0 */
+/* commit time: 2017-07-13 14:58:01 +0200 */
diff --git a/src/third_party/tomcrypt-1.18.0-rc2/src/headers/tomcrypt_custom.h b/src/third_party/tomcrypt-1.18.0-rc2/src/headers/tomcrypt_custom.h
new file mode 100644
index 00000000000..6a990fd4d47
--- /dev/null
+++ b/src/third_party/tomcrypt-1.18.0-rc2/src/headers/tomcrypt_custom.h
@@ -0,0 +1,598 @@
+/* LibTomCrypt, modular cryptographic library -- Tom St Denis
+ *
+ * LibTomCrypt is a library that provides various cryptographic
+ * algorithms in a highly modular and flexible manner.
+ *
+ * The library is free for all purposes without any express
+ * guarantee it works.
+ */
+
+#ifndef TOMCRYPT_CUSTOM_H_
+#define TOMCRYPT_CUSTOM_H_
+
+/* macros for various libc functions you can change for embedded targets */
+#ifndef XMALLOC
+#define XMALLOC malloc
+#endif
+#ifndef XREALLOC
+#define XREALLOC realloc
+#endif
+#ifndef XCALLOC
+#define XCALLOC calloc
+#endif
+#ifndef XFREE
+#define XFREE free
+#endif
+
+#ifndef XMEMSET
+#define XMEMSET memset
+#endif
+#ifndef XMEMCPY
+#define XMEMCPY memcpy
+#endif
+#ifndef XMEMMOVE
+#define XMEMMOVE memmove
+#endif
+#ifndef XMEMCMP
+#define XMEMCMP memcmp
+#endif
+/* A memory compare function that has to run in constant time,
+ * c.f. mem_neq() API summary.
+ */
+#ifndef XMEM_NEQ
+#define XMEM_NEQ mem_neq
+#endif
+#ifndef XSTRCMP
+#define XSTRCMP strcmp
+#endif
+
+#ifndef XCLOCK
+#define XCLOCK clock
+#endif
+
+#ifndef XQSORT
+#define XQSORT qsort
+#endif
+
+#if ( defined(malloc) || defined(realloc) || defined(calloc) || defined(free) || \
+ defined(memset) || defined(memcpy) || defined(memcmp) || defined(strcmp) || \
+ defined(clock) || defined(qsort) ) && !defined(LTC_NO_PROTOTYPES)
+#define LTC_NO_PROTOTYPES
+#endif
+
+/* shortcut to disable automatic inclusion */
+#if defined LTC_NOTHING && !defined LTC_EASY
+ #define LTC_NO_MATH
+ #define LTC_NO_CIPHERS
+ #define LTC_NO_MODES
+ #define LTC_NO_HASHES
+ #define LTC_NO_MACS
+ #define LTC_NO_PRNGS
+ #define LTC_NO_PK
+ #define LTC_NO_PKCS
+ #define LTC_NO_MISC
+ #define LTC_NO_FILE
+#endif /* LTC_NOTHING */
+
+/* Easy button? */
+#ifdef LTC_EASY
+ #define LTC_NO_CIPHERS
+ #define LTC_RIJNDAEL
+ #define LTC_BLOWFISH
+ #define LTC_DES
+ #define LTC_CAST5
+
+ #define LTC_NO_MODES
+ #define LTC_ECB_MODE
+ #define LTC_CBC_MODE
+ #define LTC_CTR_MODE
+
+ #define LTC_NO_HASHES
+ #define LTC_SHA1
+ #define LTC_SHA3
+ #define LTC_SHA512
+ #define LTC_SHA384
+ #define LTC_SHA256
+ #define LTC_SHA224
+ #define LTC_HASH_HELPERS
+
+ #define LTC_NO_MACS
+ #define LTC_HMAC
+ #define LTC_OMAC
+ #define LTC_CCM_MODE
+
+ #define LTC_NO_PRNGS
+ #define LTC_SPRNG
+ #define LTC_YARROW
+ #define LTC_DEVRANDOM
+ #define LTC_TRY_URANDOM_FIRST
+ #define LTC_RNG_GET_BYTES
+ #define LTC_RNG_MAKE_PRNG
+
+ #define LTC_NO_PK
+ #define LTC_MRSA
+ #define LTC_MECC
+
+ #define LTC_NO_MISC
+ #define LTC_BASE64
+#endif
+
+/* The minimal set of functionality to run the tests */
+#ifdef LTC_MINIMAL
+ #define LTC_RIJNDAEL
+ #define LTC_SHA256
+ #define LTC_YARROW
+ #define LTC_CTR_MODE
+
+ #define LTC_RNG_MAKE_PRNG
+ #define LTC_RNG_GET_BYTES
+ #define LTC_DEVRANDOM
+ #define LTC_TRY_URANDOM_FIRST
+
+ #undef LTC_NO_FILE
+#endif
+
+/* Enable self-test test vector checking */
+#ifndef LTC_NO_TEST
+ #define LTC_TEST
+#endif
+/* Enable extended self-tests */
+/* #define LTC_TEST_EXT */
+
+/* Use small code where possible */
+/* #define LTC_SMALL_CODE */
+
+/* clean the stack of functions which put private information on stack */
+/* #define LTC_CLEAN_STACK */
+
+/* disable all file related functions */
+/* #define LTC_NO_FILE */
+
+/* disable all forms of ASM */
+/* #define LTC_NO_ASM */
+
+/* disable FAST mode */
+/* #define LTC_NO_FAST */
+
+/* disable BSWAP on x86 */
+/* #define LTC_NO_BSWAP */
+
+/* ---> math provider? <--- */
+#ifndef LTC_NO_MATH
+
+/* LibTomMath */
+/* #define LTM_DESC */
+
+/* TomsFastMath */
+/* #define TFM_DESC */
+
+/* GNU Multiple Precision Arithmetic Library */
+/* #define GMP_DESC */
+
+#endif /* LTC_NO_MATH */
+
+/* ---> Symmetric Block Ciphers <--- */
+#ifndef LTC_NO_CIPHERS
+
+#define LTC_BLOWFISH
+#define LTC_RC2
+#define LTC_RC5
+#define LTC_RC6
+#define LTC_SAFERP
+#define LTC_RIJNDAEL
+#define LTC_XTEA
+/* _TABLES tells it to use tables during setup, _SMALL means to use the smaller scheduled key format
+ * (saves 4KB of ram), _ALL_TABLES enables all tables during setup */
+#define LTC_TWOFISH
+#ifndef LTC_NO_TABLES
+ #define LTC_TWOFISH_TABLES
+ /* #define LTC_TWOFISH_ALL_TABLES */
+#else
+ #define LTC_TWOFISH_SMALL
+#endif
+/* #define LTC_TWOFISH_SMALL */
+/* LTC_DES includes EDE triple-DES */
+#define LTC_DES
+#define LTC_CAST5
+#define LTC_NOEKEON
+#define LTC_SKIPJACK
+#define LTC_SAFER
+#define LTC_KHAZAD
+#define LTC_ANUBIS
+#define LTC_ANUBIS_TWEAK
+#define LTC_KSEED
+#define LTC_KASUMI
+#define LTC_MULTI2
+#define LTC_CAMELLIA
+
+/* stream ciphers */
+#define LTC_CHACHA
+#define LTC_RC4_STREAM
+#define LTC_SOBER128_STREAM
+
+#endif /* LTC_NO_CIPHERS */
+
+
+/* ---> Block Cipher Modes of Operation <--- */
+#ifndef LTC_NO_MODES
+
+#define LTC_CFB_MODE
+#define LTC_OFB_MODE
+#define LTC_ECB_MODE
+#define LTC_CBC_MODE
+#define LTC_CTR_MODE
+
+/* F8 chaining mode */
+#define LTC_F8_MODE
+
+/* LRW mode */
+#define LTC_LRW_MODE
+#ifndef LTC_NO_TABLES
+ /* like GCM mode this will enable 16 8x128 tables [64KB] that make
+ * seeking very fast.
+ */
+ #define LTC_LRW_TABLES
+#endif
+
+/* XTS mode */
+#define LTC_XTS_MODE
+
+#endif /* LTC_NO_MODES */
+
+/* ---> One-Way Hash Functions <--- */
+#ifndef LTC_NO_HASHES
+
+#define LTC_CHC_HASH
+#define LTC_WHIRLPOOL
+#define LTC_SHA3
+#define LTC_SHA512
+#define LTC_SHA512_256
+#define LTC_SHA512_224
+#define LTC_SHA384
+#define LTC_SHA256
+#define LTC_SHA224
+#define LTC_TIGER
+#define LTC_SHA1
+#define LTC_MD5
+#define LTC_MD4
+#define LTC_MD2
+#define LTC_RIPEMD128
+#define LTC_RIPEMD160
+#define LTC_RIPEMD256
+#define LTC_RIPEMD320
+#define LTC_BLAKE2S
+#define LTC_BLAKE2B
+
+#define LTC_HASH_HELPERS
+
+#endif /* LTC_NO_HASHES */
+
+
+/* ---> MAC functions <--- */
+#ifndef LTC_NO_MACS
+
+#define LTC_HMAC
+#define LTC_OMAC
+#define LTC_PMAC
+#define LTC_XCBC
+#define LTC_F9_MODE
+#define LTC_PELICAN
+#define LTC_POLY1305
+#define LTC_BLAKE2SMAC
+#define LTC_BLAKE2BMAC
+
+/* ---> Encrypt + Authenticate Modes <--- */
+
+#define LTC_EAX_MODE
+
+#define LTC_OCB_MODE
+#define LTC_OCB3_MODE
+#define LTC_CCM_MODE
+#define LTC_GCM_MODE
+#define LTC_CHACHA20POLY1305_MODE
+
+/* Use 64KiB tables */
+#ifndef LTC_NO_TABLES
+ #define LTC_GCM_TABLES
+#endif
+
+/* USE SSE2? requires GCC works on x86_32 and x86_64*/
+#ifdef LTC_GCM_TABLES
+/* #define LTC_GCM_TABLES_SSE2 */
+#endif
+
+#endif /* LTC_NO_MACS */
+
+
+/* --> Pseudo Random Number Generators <--- */
+#ifndef LTC_NO_PRNGS
+
+/* Yarrow */
+#define LTC_YARROW
+
+/* a PRNG that simply reads from an available system source */
+#define LTC_SPRNG
+
+/* The RC4 stream cipher based PRNG */
+#define LTC_RC4
+
+/* The ChaCha20 stream cipher based PRNG */
+#define LTC_CHACHA20_PRNG
+
+/* Fortuna PRNG */
+#define LTC_FORTUNA
+
+/* Greg's SOBER128 stream cipher based PRNG */
+#define LTC_SOBER128
+
+/* the *nix style /dev/random device */
+#define LTC_DEVRANDOM
+/* try /dev/urandom before trying /dev/random
+ * are you sure you want to disable this? http://www.2uo.de/myths-about-urandom/ */
+#define LTC_TRY_URANDOM_FIRST
+/* rng_get_bytes() */
+#define LTC_RNG_GET_BYTES
+/* rng_make_prng() */
+#define LTC_RNG_MAKE_PRNG
+
+/* enable the ltc_rng hook to integrate e.g. embedded hardware RNG's easily */
+/* #define LTC_PRNG_ENABLE_LTC_RNG */
+
+#endif /* LTC_NO_PRNGS */
+
+#ifdef LTC_YARROW
+
+/* which descriptor of AES to use? */
+/* 0 = rijndael_enc 1 = aes_enc, 2 = rijndael [full], 3 = aes [full] */
+#ifdef ENCRYPT_ONLY
+ #define LTC_YARROW_AES 0
+#else
+ #define LTC_YARROW_AES 2
+#endif
+
+#endif
+
+#ifdef LTC_FORTUNA
+
+#ifndef LTC_FORTUNA_WD
+/* reseed every N calls to the read function */
+#define LTC_FORTUNA_WD 10
+#endif
+
+#ifndef LTC_FORTUNA_POOLS
+/* number of pools (4..32) can save a bit of ram by lowering the count */
+#define LTC_FORTUNA_POOLS 32
+#endif
+
+#endif /* LTC_FORTUNA */
+
+
+/* ---> Public Key Crypto <--- */
+#ifndef LTC_NO_PK
+
+/* Include RSA support */
+#define LTC_MRSA
+
+/* Include Diffie-Hellman support */
+/* is_prime fails for GMP */
+#define LTC_MDH
+/* Supported Key Sizes */
+#define LTC_DH768
+#define LTC_DH1024
+#define LTC_DH1536
+#define LTC_DH2048
+
+#ifndef TFM_DESC
+/* tfm has a problem in fp_isprime for larger key sizes */
+#define LTC_DH3072
+#define LTC_DH4096
+#define LTC_DH6144
+#define LTC_DH8192
+#endif
+
+/* Include Katja (a Rabin variant like RSA) */
+/* #define LTC_MKAT */
+
+/* Digital Signature Algorithm */
+#define LTC_MDSA
+
+/* ECC */
+#define LTC_MECC
+
+/* use Shamir's trick for point mul (speeds up signature verification) */
+#define LTC_ECC_SHAMIR
+
+#if defined(TFM_DESC) && defined(LTC_MECC)
+ #define LTC_MECC_ACCEL
+#endif
+
+/* do we want fixed point ECC */
+/* #define LTC_MECC_FP */
+
+#endif /* LTC_NO_PK */
+
+#if defined(LTC_MRSA) && !defined(LTC_NO_RSA_BLINDING)
+/* Enable RSA blinding when doing private key operations by default */
+#define LTC_RSA_BLINDING
+#endif /* LTC_NO_RSA_BLINDING */
+
+#if defined(LTC_MRSA) && !defined(LTC_NO_RSA_CRT_HARDENING)
+/* Enable RSA CRT hardening when doing private key operations by default */
+#define LTC_RSA_CRT_HARDENING
+#endif /* LTC_NO_RSA_CRT_HARDENING */
+
+#if defined(LTC_MECC) && !defined(LTC_NO_ECC_TIMING_RESISTANT)
+/* Enable ECC timing resistant version by default */
+#define LTC_ECC_TIMING_RESISTANT
+#endif
+
+/* define these PK sizes out of LTC_NO_PK
+ * to have them always defined
+ */
+#if defined(LTC_MRSA)
+/* Min and Max RSA key sizes (in bits) */
+#ifndef MIN_RSA_SIZE
+#define MIN_RSA_SIZE 1024
+#endif
+#ifndef MAX_RSA_SIZE
+#define MAX_RSA_SIZE 4096
+#endif
+#endif
+
+/* in cases where you want ASN.1/DER functionality, but no
+ * RSA, you can define this externally if 1024 is not enough
+ */
+#if defined(LTC_MRSA)
+#define LTC_DER_MAX_PUBKEY_SIZE MAX_RSA_SIZE
+#elif !defined(LTC_DER_MAX_PUBKEY_SIZE)
+/* this includes DSA */
+#define LTC_DER_MAX_PUBKEY_SIZE 1024
+#endif
+
+
+/* PKCS #1 (RSA) and #5 (Password Handling) stuff */
+#ifndef LTC_NO_PKCS
+
+#define LTC_PKCS_1
+#define LTC_PKCS_5
+
+/* Include ASN.1 DER (required by DSA/RSA) */
+#define LTC_DER
+
+#endif /* LTC_NO_PKCS */
+
+/* misc stuff */
+#ifndef LTC_NO_MISC
+
+/* Various tidbits of modern neatoness */
+#define LTC_BASE64
+/* ... and it's URL safe version */
+#define LTC_BASE64_URL
+
+/* Keep LTC_NO_HKDF for compatibility reasons
+ * superseeded by LTC_NO_MISC*/
+#ifndef LTC_NO_HKDF
+/* HKDF Key Derivation/Expansion stuff */
+#define LTC_HKDF
+#endif /* LTC_NO_HKDF */
+
+#define LTC_ADLER32
+
+#define LTC_CRC32
+
+#endif /* LTC_NO_MISC */
+
+/* cleanup */
+
+#ifdef LTC_MECC
+/* Supported ECC Key Sizes */
+#ifndef LTC_NO_CURVES
+ #define LTC_ECC112
+ #define LTC_ECC128
+ #define LTC_ECC160
+ #define LTC_ECC192
+ #define LTC_ECC224
+ #define LTC_ECC256
+ #define LTC_ECC384
+ #define LTC_ECC521
+#endif
+#endif
+
+#if defined(LTC_MECC) || defined(LTC_MRSA) || defined(LTC_MDSA) || defined(LTC_MKAT)
+ /* Include the MPI functionality? (required by the PK algorithms) */
+ #define LTC_MPI
+#endif
+
+#ifdef LTC_MRSA
+ #define LTC_PKCS_1
+#endif
+
+#if defined(LTC_PELICAN) && !defined(LTC_RIJNDAEL)
+ #error Pelican-MAC requires LTC_RIJNDAEL
+#endif
+
+#if defined(LTC_EAX_MODE) && !(defined(LTC_CTR_MODE) && defined(LTC_OMAC))
+ #error LTC_EAX_MODE requires CTR and LTC_OMAC mode
+#endif
+
+#if defined(LTC_YARROW) && !defined(LTC_CTR_MODE)
+ #error LTC_YARROW requires LTC_CTR_MODE chaining mode to be defined!
+#endif
+
+#if defined(LTC_DER) && !defined(LTC_MPI)
+ #error ASN.1 DER requires MPI functionality
+#endif
+
+#if (defined(LTC_MDSA) || defined(LTC_MRSA) || defined(LTC_MECC) || defined(LTC_MKAT)) && !defined(LTC_DER)
+ #error PK requires ASN.1 DER functionality, make sure LTC_DER is enabled
+#endif
+
+#if defined(LTC_CHACHA20POLY1305_MODE) && (!defined(LTC_CHACHA) || !defined(LTC_POLY1305))
+ #error LTC_CHACHA20POLY1305_MODE requires LTC_CHACHA + LTC_POLY1305
+#endif
+
+#if defined(LTC_CHACHA20_PRNG) && !defined(LTC_CHACHA)
+ #error LTC_CHACHA20_PRNG requires LTC_CHACHA
+#endif
+
+#if defined(LTC_RC4) && !defined(LTC_RC4_STREAM)
+ #error LTC_RC4 requires LTC_RC4_STREAM
+#endif
+
+#if defined(LTC_SOBER128) && !defined(LTC_SOBER128_STREAM)
+ #error LTC_SOBER128 requires LTC_SOBER128_STREAM
+#endif
+
+#if defined(LTC_BLAKE2SMAC) && !defined(LTC_BLAKE2S)
+ #error LTC_BLAKE2SMAC requires LTC_BLAKE2S
+#endif
+
+#if defined(LTC_BLAKE2BMAC) && !defined(LTC_BLAKE2B)
+ #error LTC_BLAKE2BMAC requires LTC_BLAKE2B
+#endif
+
+#if defined(LTC_NO_MATH) && (defined(LTM_DESC) || defined(TFM_DESC) || defined(GMP_DESC))
+ #error LTC_NO_MATH defined, but also a math descriptor
+#endif
+
+/* THREAD management */
+#ifdef LTC_PTHREAD
+
+#include <pthread.h>
+
+#define LTC_MUTEX_GLOBAL(x) pthread_mutex_t x = PTHREAD_MUTEX_INITIALIZER;
+#define LTC_MUTEX_PROTO(x) extern pthread_mutex_t x;
+#define LTC_MUTEX_TYPE(x) pthread_mutex_t x;
+#define LTC_MUTEX_INIT(x) LTC_ARGCHK(pthread_mutex_init(x, NULL) == 0);
+#define LTC_MUTEX_LOCK(x) LTC_ARGCHK(pthread_mutex_lock(x) == 0);
+#define LTC_MUTEX_UNLOCK(x) LTC_ARGCHK(pthread_mutex_unlock(x) == 0);
+
+#else
+
+/* default no functions */
+#define LTC_MUTEX_GLOBAL(x)
+#define LTC_MUTEX_PROTO(x)
+#define LTC_MUTEX_TYPE(x)
+#define LTC_MUTEX_INIT(x)
+#define LTC_MUTEX_LOCK(x)
+#define LTC_MUTEX_UNLOCK(x)
+
+#endif
+
+/* Debuggers */
+
+/* define this if you use Valgrind, note: it CHANGES the way SOBER-128 and RC4 work (see the code) */
+/* #define LTC_VALGRIND */
+
+#endif
+
+#ifndef LTC_NO_FILE
+ /* buffer size for reading from a file via fread(..) */
+ #ifndef LTC_FILE_READ_BUFSIZE
+ #define LTC_FILE_READ_BUFSIZE 8192
+ #endif
+#endif
+
+/* ref: HEAD -> release/1.18.0, tag: v1.18.0-rc2 */
+/* git commit: aa0f396c0c8828ce39456129507fc72ef0208bd0 */
+/* commit time: 2017-07-13 14:58:01 +0200 */
diff --git a/src/third_party/tomcrypt-1.18.0-rc2/src/headers/tomcrypt_hash.h b/src/third_party/tomcrypt-1.18.0-rc2/src/headers/tomcrypt_hash.h
new file mode 100644
index 00000000000..1898717b0be
--- /dev/null
+++ b/src/third_party/tomcrypt-1.18.0-rc2/src/headers/tomcrypt_hash.h
@@ -0,0 +1,531 @@
+/* LibTomCrypt, modular cryptographic library -- Tom St Denis
+ *
+ * LibTomCrypt is a library that provides various cryptographic
+ * algorithms in a highly modular and flexible manner.
+ *
+ * The library is free for all purposes without any express
+ * guarantee it works.
+ */
+
+/* ---- HASH FUNCTIONS ---- */
+#ifdef LTC_SHA3
+struct sha3_state {
+ ulong64 saved; /* the portion of the input message that we didn't consume yet */
+ ulong64 s[25];
+ unsigned char sb[25 * 8]; /* used for storing `ulong64 s[25]` as little-endian bytes */
+ unsigned short byte_index; /* 0..7--the next byte after the set one (starts from 0; 0--none are buffered) */
+ unsigned short word_index; /* 0..24--the next word to integrate input (starts from 0) */
+ unsigned short capacity_words; /* the double size of the hash output in words (e.g. 16 for Keccak 512) */
+ unsigned short xof_flag;
+};
+#endif
+
+#ifdef LTC_SHA512
+struct sha512_state {
+ ulong64 length, state[8];
+ unsigned long curlen;
+ unsigned char buf[128];
+};
+#endif
+
+#ifdef LTC_SHA256
+struct sha256_state {
+ ulong64 length;
+ ulong32 state[8], curlen;
+ unsigned char buf[64];
+};
+#endif
+
+#ifdef LTC_SHA1
+struct sha1_state {
+ ulong64 length;
+ ulong32 state[5], curlen;
+ unsigned char buf[64];
+};
+#endif
+
+#ifdef LTC_MD5
+struct md5_state {
+ ulong64 length;
+ ulong32 state[4], curlen;
+ unsigned char buf[64];
+};
+#endif
+
+#ifdef LTC_MD4
+struct md4_state {
+ ulong64 length;
+ ulong32 state[4], curlen;
+ unsigned char buf[64];
+};
+#endif
+
+#ifdef LTC_TIGER
+struct tiger_state {
+ ulong64 state[3], length;
+ unsigned long curlen;
+ unsigned char buf[64];
+};
+#endif
+
+#ifdef LTC_MD2
+struct md2_state {
+ unsigned char chksum[16], X[48], buf[16];
+ unsigned long curlen;
+};
+#endif
+
+#ifdef LTC_RIPEMD128
+struct rmd128_state {
+ ulong64 length;
+ unsigned char buf[64];
+ ulong32 curlen, state[4];
+};
+#endif
+
+#ifdef LTC_RIPEMD160
+struct rmd160_state {
+ ulong64 length;
+ unsigned char buf[64];
+ ulong32 curlen, state[5];
+};
+#endif
+
+#ifdef LTC_RIPEMD256
+struct rmd256_state {
+ ulong64 length;
+ unsigned char buf[64];
+ ulong32 curlen, state[8];
+};
+#endif
+
+#ifdef LTC_RIPEMD320
+struct rmd320_state {
+ ulong64 length;
+ unsigned char buf[64];
+ ulong32 curlen, state[10];
+};
+#endif
+
+#ifdef LTC_WHIRLPOOL
+struct whirlpool_state {
+ ulong64 length, state[8];
+ unsigned char buf[64];
+ ulong32 curlen;
+};
+#endif
+
+#ifdef LTC_CHC_HASH
+struct chc_state {
+ ulong64 length;
+ unsigned char state[MAXBLOCKSIZE], buf[MAXBLOCKSIZE];
+ ulong32 curlen;
+};
+#endif
+
+#ifdef LTC_BLAKE2S
+struct blake2s_state {
+ ulong32 h[8];
+ ulong32 t[2];
+ ulong32 f[2];
+ unsigned char buf[64];
+ unsigned long curlen;
+ unsigned long outlen;
+ unsigned char last_node;
+};
+#endif
+
+#ifdef LTC_BLAKE2B
+struct blake2b_state {
+ ulong64 h[8];
+ ulong64 t[2];
+ ulong64 f[2];
+ unsigned char buf[128];
+ unsigned long curlen;
+ unsigned long outlen;
+ unsigned char last_node;
+};
+#endif
+
+typedef union Hash_state {
+ char dummy[1];
+#ifdef LTC_CHC_HASH
+ struct chc_state chc;
+#endif
+#ifdef LTC_WHIRLPOOL
+ struct whirlpool_state whirlpool;
+#endif
+#ifdef LTC_SHA3
+ struct sha3_state sha3;
+#endif
+#ifdef LTC_SHA512
+ struct sha512_state sha512;
+#endif
+#ifdef LTC_SHA256
+ struct sha256_state sha256;
+#endif
+#ifdef LTC_SHA1
+ struct sha1_state sha1;
+#endif
+#ifdef LTC_MD5
+ struct md5_state md5;
+#endif
+#ifdef LTC_MD4
+ struct md4_state md4;
+#endif
+#ifdef LTC_MD2
+ struct md2_state md2;
+#endif
+#ifdef LTC_TIGER
+ struct tiger_state tiger;
+#endif
+#ifdef LTC_RIPEMD128
+ struct rmd128_state rmd128;
+#endif
+#ifdef LTC_RIPEMD160
+ struct rmd160_state rmd160;
+#endif
+#ifdef LTC_RIPEMD256
+ struct rmd256_state rmd256;
+#endif
+#ifdef LTC_RIPEMD320
+ struct rmd320_state rmd320;
+#endif
+#ifdef LTC_BLAKE2S
+ struct blake2s_state blake2s;
+#endif
+#ifdef LTC_BLAKE2B
+ struct blake2b_state blake2b;
+#endif
+
+ void *data;
+} hash_state;
+
+/** hash descriptor */
+extern struct ltc_hash_descriptor {
+ /** name of hash */
+ char *name;
+ /** internal ID */
+ unsigned char ID;
+ /** Size of digest in octets */
+ unsigned long hashsize;
+ /** Input block size in octets */
+ unsigned long blocksize;
+ /** ASN.1 OID */
+ unsigned long OID[16];
+ /** Length of DER encoding */
+ unsigned long OIDlen;
+
+ /** Init a hash state
+ @param hash The hash to initialize
+ @return CRYPT_OK if successful
+ */
+ int (*init)(hash_state *hash);
+ /** Process a block of data
+ @param hash The hash state
+ @param in The data to hash
+ @param inlen The length of the data (octets)
+ @return CRYPT_OK if successful
+ */
+ int (*process)(hash_state *hash, const unsigned char *in, unsigned long inlen);
+ /** Produce the digest and store it
+ @param hash The hash state
+ @param out [out] The destination of the digest
+ @return CRYPT_OK if successful
+ */
+ int (*done)(hash_state *hash, unsigned char *out);
+ /** Self-test
+ @return CRYPT_OK if successful, CRYPT_NOP if self-tests have been disabled
+ */
+ int (*test)(void);
+
+ /* accelerated hmac callback: if you need to-do multiple packets just use the generic hmac_memory and provide a hash callback */
+ int (*hmac_block)(const unsigned char *key, unsigned long keylen,
+ const unsigned char *in, unsigned long inlen,
+ unsigned char *out, unsigned long *outlen);
+
+} hash_descriptor[];
+
+#ifdef LTC_CHC_HASH
+int chc_register(int cipher);
+int chc_init(hash_state * md);
+int chc_process(hash_state * md, const unsigned char *in, unsigned long inlen);
+int chc_done(hash_state * md, unsigned char *hash);
+int chc_test(void);
+extern const struct ltc_hash_descriptor chc_desc;
+#endif
+
+#ifdef LTC_WHIRLPOOL
+int whirlpool_init(hash_state * md);
+int whirlpool_process(hash_state * md, const unsigned char *in, unsigned long inlen);
+int whirlpool_done(hash_state * md, unsigned char *hash);
+int whirlpool_test(void);
+extern const struct ltc_hash_descriptor whirlpool_desc;
+#endif
+
+#ifdef LTC_SHA3
+int sha3_512_init(hash_state * md);
+int sha3_512_test(void);
+extern const struct ltc_hash_descriptor sha3_512_desc;
+int sha3_384_init(hash_state * md);
+int sha3_384_test(void);
+extern const struct ltc_hash_descriptor sha3_384_desc;
+int sha3_256_init(hash_state * md);
+int sha3_256_test(void);
+extern const struct ltc_hash_descriptor sha3_256_desc;
+int sha3_224_init(hash_state * md);
+int sha3_224_test(void);
+extern const struct ltc_hash_descriptor sha3_224_desc;
+/* process + done are the same for all variants */
+int sha3_process(hash_state * md, const unsigned char *in, unsigned long inlen);
+int sha3_done(hash_state *md, unsigned char *hash);
+/* SHAKE128 + SHAKE256 */
+int sha3_shake_init(hash_state *md, int num);
+#define sha3_shake_process(a,b,c) sha3_process(a,b,c)
+int sha3_shake_done(hash_state *md, unsigned char *out, unsigned long outlen);
+int sha3_shake_test(void);
+int sha3_shake_memory(int num, const unsigned char *in, unsigned long inlen, unsigned char *out, unsigned long *outlen);
+#endif
+
+#ifdef LTC_SHA512
+int sha512_init(hash_state * md);
+int sha512_process(hash_state * md, const unsigned char *in, unsigned long inlen);
+int sha512_done(hash_state * md, unsigned char *hash);
+int sha512_test(void);
+extern const struct ltc_hash_descriptor sha512_desc;
+#endif
+
+#ifdef LTC_SHA384
+#ifndef LTC_SHA512
+ #error LTC_SHA512 is required for LTC_SHA384
+#endif
+int sha384_init(hash_state * md);
+#define sha384_process sha512_process
+int sha384_done(hash_state * md, unsigned char *hash);
+int sha384_test(void);
+extern const struct ltc_hash_descriptor sha384_desc;
+#endif
+
+#ifdef LTC_SHA512_256
+#ifndef LTC_SHA512
+ #error LTC_SHA512 is required for LTC_SHA512_256
+#endif
+int sha512_256_init(hash_state * md);
+#define sha512_256_process sha512_process
+int sha512_256_done(hash_state * md, unsigned char *hash);
+int sha512_256_test(void);
+extern const struct ltc_hash_descriptor sha512_256_desc;
+#endif
+
+#ifdef LTC_SHA512_224
+#ifndef LTC_SHA512
+ #error LTC_SHA512 is required for LTC_SHA512_224
+#endif
+int sha512_224_init(hash_state * md);
+#define sha512_224_process sha512_process
+int sha512_224_done(hash_state * md, unsigned char *hash);
+int sha512_224_test(void);
+extern const struct ltc_hash_descriptor sha512_224_desc;
+#endif
+
+#ifdef LTC_SHA256
+int sha256_init(hash_state * md);
+int sha256_process(hash_state * md, const unsigned char *in, unsigned long inlen);
+int sha256_done(hash_state * md, unsigned char *hash);
+int sha256_test(void);
+extern const struct ltc_hash_descriptor sha256_desc;
+
+#ifdef LTC_SHA224
+#ifndef LTC_SHA256
+ #error LTC_SHA256 is required for LTC_SHA224
+#endif
+int sha224_init(hash_state * md);
+#define sha224_process sha256_process
+int sha224_done(hash_state * md, unsigned char *hash);
+int sha224_test(void);
+extern const struct ltc_hash_descriptor sha224_desc;
+#endif
+#endif
+
+#ifdef LTC_SHA1
+int sha1_init(hash_state * md);
+int sha1_process(hash_state * md, const unsigned char *in, unsigned long inlen);
+int sha1_done(hash_state * md, unsigned char *hash);
+int sha1_test(void);
+extern const struct ltc_hash_descriptor sha1_desc;
+#endif
+
+#ifdef LTC_BLAKE2S
+extern const struct ltc_hash_descriptor blake2s_256_desc;
+int blake2s_256_init(hash_state * md);
+int blake2s_256_test(void);
+
+extern const struct ltc_hash_descriptor blake2s_224_desc;
+int blake2s_224_init(hash_state * md);
+int blake2s_224_test(void);
+
+extern const struct ltc_hash_descriptor blake2s_160_desc;
+int blake2s_160_init(hash_state * md);
+int blake2s_160_test(void);
+
+extern const struct ltc_hash_descriptor blake2s_128_desc;
+int blake2s_128_init(hash_state * md);
+int blake2s_128_test(void);
+
+int blake2s_init(hash_state * md, unsigned long outlen, const unsigned char *key, unsigned long keylen);
+int blake2s_process(hash_state * md, const unsigned char *in, unsigned long inlen);
+int blake2s_done(hash_state * md, unsigned char *hash);
+#endif
+
+#ifdef LTC_BLAKE2B
+extern const struct ltc_hash_descriptor blake2b_512_desc;
+int blake2b_512_init(hash_state * md);
+int blake2b_512_test(void);
+
+extern const struct ltc_hash_descriptor blake2b_384_desc;
+int blake2b_384_init(hash_state * md);
+int blake2b_384_test(void);
+
+extern const struct ltc_hash_descriptor blake2b_256_desc;
+int blake2b_256_init(hash_state * md);
+int blake2b_256_test(void);
+
+extern const struct ltc_hash_descriptor blake2b_160_desc;
+int blake2b_160_init(hash_state * md);
+int blake2b_160_test(void);
+
+int blake2b_init(hash_state * md, unsigned long outlen, const unsigned char *key, unsigned long keylen);
+int blake2b_process(hash_state * md, const unsigned char *in, unsigned long inlen);
+int blake2b_done(hash_state * md, unsigned char *hash);
+#endif
+
+#ifdef LTC_MD5
+int md5_init(hash_state * md);
+int md5_process(hash_state * md, const unsigned char *in, unsigned long inlen);
+int md5_done(hash_state * md, unsigned char *hash);
+int md5_test(void);
+extern const struct ltc_hash_descriptor md5_desc;
+#endif
+
+#ifdef LTC_MD4
+int md4_init(hash_state * md);
+int md4_process(hash_state * md, const unsigned char *in, unsigned long inlen);
+int md4_done(hash_state * md, unsigned char *hash);
+int md4_test(void);
+extern const struct ltc_hash_descriptor md4_desc;
+#endif
+
+#ifdef LTC_MD2
+int md2_init(hash_state * md);
+int md2_process(hash_state * md, const unsigned char *in, unsigned long inlen);
+int md2_done(hash_state * md, unsigned char *hash);
+int md2_test(void);
+extern const struct ltc_hash_descriptor md2_desc;
+#endif
+
+#ifdef LTC_TIGER
+int tiger_init(hash_state * md);
+int tiger_process(hash_state * md, const unsigned char *in, unsigned long inlen);
+int tiger_done(hash_state * md, unsigned char *hash);
+int tiger_test(void);
+extern const struct ltc_hash_descriptor tiger_desc;
+#endif
+
+#ifdef LTC_RIPEMD128
+int rmd128_init(hash_state * md);
+int rmd128_process(hash_state * md, const unsigned char *in, unsigned long inlen);
+int rmd128_done(hash_state * md, unsigned char *hash);
+int rmd128_test(void);
+extern const struct ltc_hash_descriptor rmd128_desc;
+#endif
+
+#ifdef LTC_RIPEMD160
+int rmd160_init(hash_state * md);
+int rmd160_process(hash_state * md, const unsigned char *in, unsigned long inlen);
+int rmd160_done(hash_state * md, unsigned char *hash);
+int rmd160_test(void);
+extern const struct ltc_hash_descriptor rmd160_desc;
+#endif
+
+#ifdef LTC_RIPEMD256
+int rmd256_init(hash_state * md);
+int rmd256_process(hash_state * md, const unsigned char *in, unsigned long inlen);
+int rmd256_done(hash_state * md, unsigned char *hash);
+int rmd256_test(void);
+extern const struct ltc_hash_descriptor rmd256_desc;
+#endif
+
+#ifdef LTC_RIPEMD320
+int rmd320_init(hash_state * md);
+int rmd320_process(hash_state * md, const unsigned char *in, unsigned long inlen);
+int rmd320_done(hash_state * md, unsigned char *hash);
+int rmd320_test(void);
+extern const struct ltc_hash_descriptor rmd320_desc;
+#endif
+
+
+int find_hash(const char *name);
+int find_hash_id(unsigned char ID);
+int find_hash_oid(const unsigned long *ID, unsigned long IDlen);
+int find_hash_any(const char *name, int digestlen);
+int register_hash(const struct ltc_hash_descriptor *hash);
+int unregister_hash(const struct ltc_hash_descriptor *hash);
+int register_all_hashes(void);
+int hash_is_valid(int idx);
+
+LTC_MUTEX_PROTO(ltc_hash_mutex)
+
+int hash_memory(int hash,
+ const unsigned char *in, unsigned long inlen,
+ unsigned char *out, unsigned long *outlen);
+int hash_memory_multi(int hash, unsigned char *out, unsigned long *outlen,
+ const unsigned char *in, unsigned long inlen, ...);
+
+#ifndef LTC_NO_FILE
+int hash_filehandle(int hash, FILE *in, unsigned char *out, unsigned long *outlen);
+int hash_file(int hash, const char *fname, unsigned char *out, unsigned long *outlen);
+#endif
+
+/* a simple macro for making hash "process" functions */
+#define HASH_PROCESS(func_name, compress_name, state_var, block_size) \
+int func_name (hash_state * md, const unsigned char *in, unsigned long inlen) \
+{ \
+ unsigned long n; \
+ int err; \
+ LTC_ARGCHK(md != NULL); \
+ LTC_ARGCHK(in != NULL); \
+ if (md-> state_var .curlen > sizeof(md-> state_var .buf)) { \
+ return CRYPT_INVALID_ARG; \
+ } \
+ if ((md-> state_var .length + inlen) < md-> state_var .length) { \
+ return CRYPT_HASH_OVERFLOW; \
+ } \
+ while (inlen > 0) { \
+ if (md-> state_var .curlen == 0 && inlen >= block_size) { \
+ if ((err = compress_name (md, (unsigned char *)in)) != CRYPT_OK) { \
+ return err; \
+ } \
+ md-> state_var .length += block_size * 8; \
+ in += block_size; \
+ inlen -= block_size; \
+ } else { \
+ n = MIN(inlen, (block_size - md-> state_var .curlen)); \
+ XMEMCPY(md-> state_var .buf + md-> state_var.curlen, in, (size_t)n); \
+ md-> state_var .curlen += n; \
+ in += n; \
+ inlen -= n; \
+ if (md-> state_var .curlen == block_size) { \
+ if ((err = compress_name (md, md-> state_var .buf)) != CRYPT_OK) { \
+ return err; \
+ } \
+ md-> state_var .length += 8*block_size; \
+ md-> state_var .curlen = 0; \
+ } \
+ } \
+ } \
+ return CRYPT_OK; \
+}
+
+/* ref: HEAD -> release/1.18.0, tag: v1.18.0-rc2 */
+/* git commit: aa0f396c0c8828ce39456129507fc72ef0208bd0 */
+/* commit time: 2017-07-13 14:58:01 +0200 */
diff --git a/src/third_party/tomcrypt-1.18.0-rc2/src/headers/tomcrypt_mac.h b/src/third_party/tomcrypt-1.18.0-rc2/src/headers/tomcrypt_mac.h
new file mode 100644
index 00000000000..a64ba2cd080
--- /dev/null
+++ b/src/third_party/tomcrypt-1.18.0-rc2/src/headers/tomcrypt_mac.h
@@ -0,0 +1,568 @@
+/* LibTomCrypt, modular cryptographic library -- Tom St Denis
+ *
+ * LibTomCrypt is a library that provides various cryptographic
+ * algorithms in a highly modular and flexible manner.
+ *
+ * The library is free for all purposes without any express
+ * guarantee it works.
+ */
+
+#ifdef LTC_HMAC
+typedef struct Hmac_state {
+ hash_state md;
+ int hash;
+ hash_state hashstate;
+ unsigned char *key;
+} hmac_state;
+
+int hmac_init(hmac_state *hmac, int hash, const unsigned char *key, unsigned long keylen);
+int hmac_process(hmac_state *hmac, const unsigned char *in, unsigned long inlen);
+int hmac_done(hmac_state *hmac, unsigned char *out, unsigned long *outlen);
+int hmac_test(void);
+int hmac_memory(int hash,
+ const unsigned char *key, unsigned long keylen,
+ const unsigned char *in, unsigned long inlen,
+ unsigned char *out, unsigned long *outlen);
+int hmac_memory_multi(int hash,
+ const unsigned char *key, unsigned long keylen,
+ unsigned char *out, unsigned long *outlen,
+ const unsigned char *in, unsigned long inlen, ...);
+int hmac_file(int hash, const char *fname, const unsigned char *key,
+ unsigned long keylen,
+ unsigned char *dst, unsigned long *dstlen);
+#endif
+
+#ifdef LTC_OMAC
+
+typedef struct {
+ int cipher_idx,
+ buflen,
+ blklen;
+ unsigned char block[MAXBLOCKSIZE],
+ prev[MAXBLOCKSIZE],
+ Lu[2][MAXBLOCKSIZE];
+ symmetric_key key;
+} omac_state;
+
+int omac_init(omac_state *omac, int cipher, const unsigned char *key, unsigned long keylen);
+int omac_process(omac_state *omac, const unsigned char *in, unsigned long inlen);
+int omac_done(omac_state *omac, unsigned char *out, unsigned long *outlen);
+int omac_memory(int cipher,
+ const unsigned char *key, unsigned long keylen,
+ const unsigned char *in, unsigned long inlen,
+ unsigned char *out, unsigned long *outlen);
+int omac_memory_multi(int cipher,
+ const unsigned char *key, unsigned long keylen,
+ unsigned char *out, unsigned long *outlen,
+ const unsigned char *in, unsigned long inlen, ...);
+int omac_file(int cipher,
+ const unsigned char *key, unsigned long keylen,
+ const char *filename,
+ unsigned char *out, unsigned long *outlen);
+int omac_test(void);
+#endif /* LTC_OMAC */
+
+#ifdef LTC_PMAC
+
+typedef struct {
+ unsigned char Ls[32][MAXBLOCKSIZE], /* L shifted by i bits to the left */
+ Li[MAXBLOCKSIZE], /* value of Li [current value, we calc from previous recall] */
+ Lr[MAXBLOCKSIZE], /* L * x^-1 */
+ block[MAXBLOCKSIZE], /* currently accumulated block */
+ checksum[MAXBLOCKSIZE]; /* current checksum */
+
+ symmetric_key key; /* scheduled key for cipher */
+ unsigned long block_index; /* index # for current block */
+ int cipher_idx, /* cipher idx */
+ block_len, /* length of block */
+ buflen; /* number of bytes in the buffer */
+} pmac_state;
+
+int pmac_init(pmac_state *pmac, int cipher, const unsigned char *key, unsigned long keylen);
+int pmac_process(pmac_state *pmac, const unsigned char *in, unsigned long inlen);
+int pmac_done(pmac_state *pmac, unsigned char *out, unsigned long *outlen);
+
+int pmac_memory(int cipher,
+ const unsigned char *key, unsigned long keylen,
+ const unsigned char *msg, unsigned long msglen,
+ unsigned char *out, unsigned long *outlen);
+
+int pmac_memory_multi(int cipher,
+ const unsigned char *key, unsigned long keylen,
+ unsigned char *out, unsigned long *outlen,
+ const unsigned char *in, unsigned long inlen, ...);
+
+int pmac_file(int cipher,
+ const unsigned char *key, unsigned long keylen,
+ const char *filename,
+ unsigned char *out, unsigned long *outlen);
+
+int pmac_test(void);
+
+/* internal functions */
+int pmac_ntz(unsigned long x);
+void pmac_shift_xor(pmac_state *pmac);
+
+#endif /* PMAC */
+
+#ifdef LTC_POLY1305
+typedef struct {
+ ulong32 r[5];
+ ulong32 h[5];
+ ulong32 pad[4];
+ unsigned long leftover;
+ unsigned char buffer[16];
+ int final;
+} poly1305_state;
+
+int poly1305_init(poly1305_state *st, const unsigned char *key, unsigned long keylen);
+int poly1305_process(poly1305_state *st, const unsigned char *in, unsigned long inlen);
+int poly1305_done(poly1305_state *st, unsigned char *mac, unsigned long *maclen);
+int poly1305_test(void);
+int poly1305_memory(const unsigned char *key, unsigned long keylen, const unsigned char *in, unsigned long inlen, unsigned char *mac, unsigned long *maclen);
+int poly1305_memory_multi(const unsigned char *key, unsigned long keylen, unsigned char *mac, unsigned long *maclen, const unsigned char *in, unsigned long inlen, ...);
+int poly1305_file(const char *fname, const unsigned char *key, unsigned long keylen, unsigned char *mac, unsigned long *maclen);
+int poly1305_test(void);
+#endif /* LTC_POLY1305 */
+
+#ifdef LTC_BLAKE2SMAC
+typedef hash_state blake2smac_state;
+int blake2smac_init(blake2smac_state *st, unsigned long outlen, const unsigned char *key, unsigned long keylen);
+int blake2smac_process(blake2smac_state *st, const unsigned char *in, unsigned long inlen);
+int blake2smac_done(blake2smac_state *st, unsigned char *mac, unsigned long *maclen);
+int blake2smac_test(void);
+int blake2smac_memory(const unsigned char *key, unsigned long keylen, const unsigned char *in, unsigned long inlen, unsigned char *mac, unsigned long *maclen);
+int blake2smac_memory_multi(const unsigned char *key, unsigned long keylen, unsigned char *mac, unsigned long *maclen, const unsigned char *in, unsigned long inlen, ...);
+int blake2smac_file(const char *fname, const unsigned char *key, unsigned long keylen, unsigned char *mac, unsigned long *maclen);
+int blake2smac_test(void);
+#endif /* LTC_BLAKE2SMAC */
+
+#ifdef LTC_BLAKE2BMAC
+typedef hash_state blake2bmac_state;
+int blake2bmac_init(blake2bmac_state *st, unsigned long outlen, const unsigned char *key, unsigned long keylen);
+int blake2bmac_process(blake2bmac_state *st, const unsigned char *in, unsigned long inlen);
+int blake2bmac_done(blake2bmac_state *st, unsigned char *mac, unsigned long *maclen);
+int blake2bmac_test(void);
+int blake2bmac_memory(const unsigned char *key, unsigned long keylen, const unsigned char *in, unsigned long inlen, unsigned char *mac, unsigned long *maclen);
+int blake2bmac_memory_multi(const unsigned char *key, unsigned long keylen, unsigned char *mac, unsigned long *maclen, const unsigned char *in, unsigned long inlen, ...);
+int blake2bmac_file(const char *fname, const unsigned char *key, unsigned long keylen, unsigned char *mac, unsigned long *maclen);
+int blake2bmac_test(void);
+#endif /* LTC_BLAKE2BMAC */
+
+#ifdef LTC_EAX_MODE
+
+#if !(defined(LTC_OMAC) && defined(LTC_CTR_MODE))
+ #error LTC_EAX_MODE requires LTC_OMAC and CTR
+#endif
+
+typedef struct {
+ unsigned char N[MAXBLOCKSIZE];
+ symmetric_CTR ctr;
+ omac_state headeromac, ctomac;
+} eax_state;
+
+int eax_init(eax_state *eax, int cipher, const unsigned char *key, unsigned long keylen,
+ const unsigned char *nonce, unsigned long noncelen,
+ const unsigned char *header, unsigned long headerlen);
+
+int eax_encrypt(eax_state *eax, const unsigned char *pt, unsigned char *ct, unsigned long length);
+int eax_decrypt(eax_state *eax, const unsigned char *ct, unsigned char *pt, unsigned long length);
+int eax_addheader(eax_state *eax, const unsigned char *header, unsigned long length);
+int eax_done(eax_state *eax, unsigned char *tag, unsigned long *taglen);
+
+int eax_encrypt_authenticate_memory(int cipher,
+ const unsigned char *key, unsigned long keylen,
+ const unsigned char *nonce, unsigned long noncelen,
+ const unsigned char *header, unsigned long headerlen,
+ const unsigned char *pt, unsigned long ptlen,
+ unsigned char *ct,
+ unsigned char *tag, unsigned long *taglen);
+
+int eax_decrypt_verify_memory(int cipher,
+ const unsigned char *key, unsigned long keylen,
+ const unsigned char *nonce, unsigned long noncelen,
+ const unsigned char *header, unsigned long headerlen,
+ const unsigned char *ct, unsigned long ctlen,
+ unsigned char *pt,
+ unsigned char *tag, unsigned long taglen,
+ int *stat);
+
+ int eax_test(void);
+#endif /* EAX MODE */
+
+#ifdef LTC_OCB_MODE
+typedef struct {
+ unsigned char L[MAXBLOCKSIZE], /* L value */
+ Ls[32][MAXBLOCKSIZE], /* L shifted by i bits to the left */
+ Li[MAXBLOCKSIZE], /* value of Li [current value, we calc from previous recall] */
+ Lr[MAXBLOCKSIZE], /* L * x^-1 */
+ R[MAXBLOCKSIZE], /* R value */
+ checksum[MAXBLOCKSIZE]; /* current checksum */
+
+ symmetric_key key; /* scheduled key for cipher */
+ unsigned long block_index; /* index # for current block */
+ int cipher, /* cipher idx */
+ block_len; /* length of block */
+} ocb_state;
+
+int ocb_init(ocb_state *ocb, int cipher,
+ const unsigned char *key, unsigned long keylen, const unsigned char *nonce);
+
+int ocb_encrypt(ocb_state *ocb, const unsigned char *pt, unsigned char *ct);
+int ocb_decrypt(ocb_state *ocb, const unsigned char *ct, unsigned char *pt);
+
+int ocb_done_encrypt(ocb_state *ocb,
+ const unsigned char *pt, unsigned long ptlen,
+ unsigned char *ct,
+ unsigned char *tag, unsigned long *taglen);
+
+int ocb_done_decrypt(ocb_state *ocb,
+ const unsigned char *ct, unsigned long ctlen,
+ unsigned char *pt,
+ const unsigned char *tag, unsigned long taglen, int *stat);
+
+int ocb_encrypt_authenticate_memory(int cipher,
+ const unsigned char *key, unsigned long keylen,
+ const unsigned char *nonce,
+ const unsigned char *pt, unsigned long ptlen,
+ unsigned char *ct,
+ unsigned char *tag, unsigned long *taglen);
+
+int ocb_decrypt_verify_memory(int cipher,
+ const unsigned char *key, unsigned long keylen,
+ const unsigned char *nonce,
+ const unsigned char *ct, unsigned long ctlen,
+ unsigned char *pt,
+ const unsigned char *tag, unsigned long taglen,
+ int *stat);
+
+int ocb_test(void);
+
+/* internal functions */
+void ocb_shift_xor(ocb_state *ocb, unsigned char *Z);
+int ocb_ntz(unsigned long x);
+int s_ocb_done(ocb_state *ocb, const unsigned char *pt, unsigned long ptlen,
+ unsigned char *ct, unsigned char *tag, unsigned long *taglen, int mode);
+
+#endif /* LTC_OCB_MODE */
+
+#ifdef LTC_OCB3_MODE
+typedef struct {
+ unsigned char Offset_0[MAXBLOCKSIZE], /* Offset_0 value */
+ Offset_current[MAXBLOCKSIZE], /* Offset_{current_block_index} value */
+ L_dollar[MAXBLOCKSIZE], /* L_$ value */
+ L_star[MAXBLOCKSIZE], /* L_* value */
+ L_[32][MAXBLOCKSIZE], /* L_{i} values */
+ tag_part[MAXBLOCKSIZE], /* intermediate result of tag calculation */
+ checksum[MAXBLOCKSIZE]; /* current checksum */
+
+ /* AAD related members */
+ unsigned char aSum_current[MAXBLOCKSIZE], /* AAD related helper variable */
+ aOffset_current[MAXBLOCKSIZE], /* AAD related helper variable */
+ adata_buffer[MAXBLOCKSIZE]; /* AAD buffer */
+ int adata_buffer_bytes; /* bytes in AAD buffer */
+ unsigned long ablock_index; /* index # for current adata (AAD) block */
+
+ symmetric_key key; /* scheduled key for cipher */
+ unsigned long block_index; /* index # for current data block */
+ int cipher, /* cipher idx */
+ block_len; /* length of block */
+} ocb3_state;
+
+int ocb3_init(ocb3_state *ocb, int cipher,
+ const unsigned char *key, unsigned long keylen,
+ const unsigned char *nonce, unsigned long noncelen);
+
+int ocb3_encrypt(ocb3_state *ocb, const unsigned char *pt, unsigned long ptlen, unsigned char *ct);
+int ocb3_decrypt(ocb3_state *ocb, const unsigned char *ct, unsigned long ctlen, unsigned char *pt);
+int ocb3_encrypt_last(ocb3_state *ocb, const unsigned char *pt, unsigned long ptlen, unsigned char *ct);
+int ocb3_decrypt_last(ocb3_state *ocb, const unsigned char *ct, unsigned long ctlen, unsigned char *pt);
+int ocb3_add_aad(ocb3_state *ocb, const unsigned char *aad, unsigned long aadlen);
+int ocb3_done(ocb3_state *ocb, unsigned char *tag, unsigned long *taglen);
+
+int ocb3_encrypt_authenticate_memory(int cipher,
+ const unsigned char *key, unsigned long keylen,
+ const unsigned char *nonce, unsigned long noncelen,
+ const unsigned char *adata, unsigned long adatalen,
+ const unsigned char *pt, unsigned long ptlen,
+ unsigned char *ct,
+ unsigned char *tag, unsigned long *taglen);
+
+int ocb3_decrypt_verify_memory(int cipher,
+ const unsigned char *key, unsigned long keylen,
+ const unsigned char *nonce, unsigned long noncelen,
+ const unsigned char *adata, unsigned long adatalen,
+ const unsigned char *ct, unsigned long ctlen,
+ unsigned char *pt,
+ const unsigned char *tag, unsigned long taglen,
+ int *stat);
+
+int ocb3_test(void);
+
+#ifdef LTC_SOURCE
+/* internal helper functions */
+int ocb3_int_aad_add_block(ocb3_state *ocb, const unsigned char *aad_block);
+void ocb3_int_calc_offset_zero(ocb3_state *ocb, const unsigned char *nonce, unsigned long noncelen);
+int ocb3_int_ntz(unsigned long x);
+void ocb3_int_xor_blocks(unsigned char *out, const unsigned char *block_a, const unsigned char *block_b, unsigned long block_len);
+#endif /* LTC_SOURCE */
+
+#endif /* LTC_OCB3_MODE */
+
+#ifdef LTC_CCM_MODE
+
+#define CCM_ENCRYPT 0
+#define CCM_DECRYPT 1
+
+typedef struct {
+ symmetric_key K;
+ int cipher, /* which cipher */
+ taglen, /* length of the tag */
+ x; /* index in PAD */
+
+ unsigned long L, /* L value */
+ ptlen, /* length that will be enc / dec */
+ current_ptlen, /* current processed length */
+ aadlen, /* length of the aad */
+ current_aadlen, /* length of the currently provided add */
+ noncelen; /* length of the nonce */
+
+ unsigned char PAD[16],
+ ctr[16],
+ CTRPAD[16],
+ CTRlen;
+} ccm_state;
+
+int ccm_init(ccm_state *ccm, int cipher,
+ const unsigned char *key, int keylen, int ptlen, int taglen, int aad_len);
+
+int ccm_reset(ccm_state *ccm);
+
+int ccm_add_nonce(ccm_state *ccm,
+ const unsigned char *nonce, unsigned long noncelen);
+
+int ccm_add_aad(ccm_state *ccm,
+ const unsigned char *adata, unsigned long adatalen);
+
+int ccm_process(ccm_state *ccm,
+ unsigned char *pt, unsigned long ptlen,
+ unsigned char *ct,
+ int direction);
+
+int ccm_done(ccm_state *ccm,
+ unsigned char *tag, unsigned long *taglen);
+
+int ccm_memory(int cipher,
+ const unsigned char *key, unsigned long keylen,
+ symmetric_key *uskey,
+ const unsigned char *nonce, unsigned long noncelen,
+ const unsigned char *header, unsigned long headerlen,
+ unsigned char *pt, unsigned long ptlen,
+ unsigned char *ct,
+ unsigned char *tag, unsigned long *taglen,
+ int direction);
+
+int ccm_test(void);
+
+#endif /* LTC_CCM_MODE */
+
+#if defined(LRW_MODE) || defined(LTC_GCM_MODE)
+void gcm_gf_mult(const unsigned char *a, const unsigned char *b, unsigned char *c);
+#endif
+
+
+/* table shared between GCM and LRW */
+#if defined(LTC_GCM_TABLES) || defined(LTC_LRW_TABLES) || ((defined(LTC_GCM_MODE) || defined(LTC_GCM_MODE)) && defined(LTC_FAST))
+extern const unsigned char gcm_shift_table[];
+#endif
+
+#ifdef LTC_GCM_MODE
+
+#define GCM_ENCRYPT 0
+#define GCM_DECRYPT 1
+
+#define LTC_GCM_MODE_IV 0
+#define LTC_GCM_MODE_AAD 1
+#define LTC_GCM_MODE_TEXT 2
+
+typedef struct {
+ symmetric_key K;
+ unsigned char H[16], /* multiplier */
+ X[16], /* accumulator */
+ Y[16], /* counter */
+ Y_0[16], /* initial counter */
+ buf[16]; /* buffer for stuff */
+
+ int cipher, /* which cipher */
+ ivmode, /* Which mode is the IV in? */
+ mode, /* mode the GCM code is in */
+ buflen; /* length of data in buf */
+
+ ulong64 totlen, /* 64-bit counter used for IV and AAD */
+ pttotlen; /* 64-bit counter for the PT */
+
+#ifdef LTC_GCM_TABLES
+ unsigned char PC[16][256][16] /* 16 tables of 8x128 */
+#ifdef LTC_GCM_TABLES_SSE2
+__attribute__ ((aligned (16)))
+#endif
+;
+#endif
+} gcm_state;
+
+void gcm_mult_h(gcm_state *gcm, unsigned char *I);
+
+int gcm_init(gcm_state *gcm, int cipher,
+ const unsigned char *key, int keylen);
+
+int gcm_reset(gcm_state *gcm);
+
+int gcm_add_iv(gcm_state *gcm,
+ const unsigned char *IV, unsigned long IVlen);
+
+int gcm_add_aad(gcm_state *gcm,
+ const unsigned char *adata, unsigned long adatalen);
+
+int gcm_process(gcm_state *gcm,
+ unsigned char *pt, unsigned long ptlen,
+ unsigned char *ct,
+ int direction);
+
+int gcm_done(gcm_state *gcm,
+ unsigned char *tag, unsigned long *taglen);
+
+int gcm_memory( int cipher,
+ const unsigned char *key, unsigned long keylen,
+ const unsigned char *IV, unsigned long IVlen,
+ const unsigned char *adata, unsigned long adatalen,
+ unsigned char *pt, unsigned long ptlen,
+ unsigned char *ct,
+ unsigned char *tag, unsigned long *taglen,
+ int direction);
+int gcm_test(void);
+
+#endif /* LTC_GCM_MODE */
+
+#ifdef LTC_PELICAN
+
+typedef struct pelican_state
+{
+ symmetric_key K;
+ unsigned char state[16];
+ int buflen;
+} pelican_state;
+
+int pelican_init(pelican_state *pelmac, const unsigned char *key, unsigned long keylen);
+int pelican_process(pelican_state *pelmac, const unsigned char *in, unsigned long inlen);
+int pelican_done(pelican_state *pelmac, unsigned char *out);
+int pelican_test(void);
+
+int pelican_memory(const unsigned char *key, unsigned long keylen,
+ const unsigned char *in, unsigned long inlen,
+ unsigned char *out);
+
+#endif
+
+#ifdef LTC_XCBC
+
+/* add this to "keylen" to xcbc_init to use a pure three-key XCBC MAC */
+#define LTC_XCBC_PURE 0x8000UL
+
+typedef struct {
+ unsigned char K[3][MAXBLOCKSIZE],
+ IV[MAXBLOCKSIZE];
+
+ symmetric_key key;
+
+ int cipher,
+ buflen,
+ blocksize;
+} xcbc_state;
+
+int xcbc_init(xcbc_state *xcbc, int cipher, const unsigned char *key, unsigned long keylen);
+int xcbc_process(xcbc_state *xcbc, const unsigned char *in, unsigned long inlen);
+int xcbc_done(xcbc_state *xcbc, unsigned char *out, unsigned long *outlen);
+int xcbc_memory(int cipher,
+ const unsigned char *key, unsigned long keylen,
+ const unsigned char *in, unsigned long inlen,
+ unsigned char *out, unsigned long *outlen);
+int xcbc_memory_multi(int cipher,
+ const unsigned char *key, unsigned long keylen,
+ unsigned char *out, unsigned long *outlen,
+ const unsigned char *in, unsigned long inlen, ...);
+int xcbc_file(int cipher,
+ const unsigned char *key, unsigned long keylen,
+ const char *filename,
+ unsigned char *out, unsigned long *outlen);
+int xcbc_test(void);
+
+#endif
+
+#ifdef LTC_F9_MODE
+
+typedef struct {
+ unsigned char akey[MAXBLOCKSIZE],
+ ACC[MAXBLOCKSIZE],
+ IV[MAXBLOCKSIZE];
+
+ symmetric_key key;
+
+ int cipher,
+ buflen,
+ keylen,
+ blocksize;
+} f9_state;
+
+int f9_init(f9_state *f9, int cipher, const unsigned char *key, unsigned long keylen);
+int f9_process(f9_state *f9, const unsigned char *in, unsigned long inlen);
+int f9_done(f9_state *f9, unsigned char *out, unsigned long *outlen);
+int f9_memory(int cipher,
+ const unsigned char *key, unsigned long keylen,
+ const unsigned char *in, unsigned long inlen,
+ unsigned char *out, unsigned long *outlen);
+int f9_memory_multi(int cipher,
+ const unsigned char *key, unsigned long keylen,
+ unsigned char *out, unsigned long *outlen,
+ const unsigned char *in, unsigned long inlen, ...);
+int f9_file(int cipher,
+ const unsigned char *key, unsigned long keylen,
+ const char *filename,
+ unsigned char *out, unsigned long *outlen);
+int f9_test(void);
+
+#endif
+
+#ifdef LTC_CHACHA20POLY1305_MODE
+
+typedef struct {
+ poly1305_state poly;
+ chacha_state chacha;
+ ulong64 aadlen;
+ ulong64 ctlen;
+ int aadflg;
+} chacha20poly1305_state;
+
+#define CHCHA20POLY1305_ENCRYPT 0
+#define CHCHA20POLY1305_DECRYPT 1
+
+int chacha20poly1305_init(chacha20poly1305_state *st, const unsigned char *key, unsigned long keylen);
+int chacha20poly1305_setiv(chacha20poly1305_state *st, const unsigned char *iv, unsigned long ivlen);
+int chacha20poly1305_setiv_rfc7905(chacha20poly1305_state *st, const unsigned char *iv, unsigned long ivlen, ulong64 sequence_number);
+int chacha20poly1305_add_aad(chacha20poly1305_state *st, const unsigned char *in, unsigned long inlen);
+int chacha20poly1305_encrypt(chacha20poly1305_state *st, const unsigned char *in, unsigned long inlen, unsigned char *out);
+int chacha20poly1305_decrypt(chacha20poly1305_state *st, const unsigned char *in, unsigned long inlen, unsigned char *out);
+int chacha20poly1305_done(chacha20poly1305_state *st, unsigned char *tag, unsigned long *taglen);
+int chacha20poly1305_memory(const unsigned char *key, unsigned long keylen,
+ const unsigned char *iv, unsigned long ivlen,
+ const unsigned char *aad, unsigned long aadlen,
+ const unsigned char *in, unsigned long inlen,
+ unsigned char *out,
+ unsigned char *tag, unsigned long *taglen,
+ int direction);
+int chacha20poly1305_test(void);
+
+#endif /* LTC_CHACHA20POLY1305_MODE */
+
+/* ref: HEAD -> release/1.18.0, tag: v1.18.0-rc2 */
+/* git commit: aa0f396c0c8828ce39456129507fc72ef0208bd0 */
+/* commit time: 2017-07-13 14:58:01 +0200 */
diff --git a/src/third_party/tomcrypt-1.18.0-rc2/src/headers/tomcrypt_macros.h b/src/third_party/tomcrypt-1.18.0-rc2/src/headers/tomcrypt_macros.h
new file mode 100644
index 00000000000..240493f3afe
--- /dev/null
+++ b/src/third_party/tomcrypt-1.18.0-rc2/src/headers/tomcrypt_macros.h
@@ -0,0 +1,446 @@
+/* LibTomCrypt, modular cryptographic library -- Tom St Denis
+ *
+ * LibTomCrypt is a library that provides various cryptographic
+ * algorithms in a highly modular and flexible manner.
+ *
+ * The library is free for all purposes without any express
+ * guarantee it works.
+ */
+
+/* ---- HELPER MACROS ---- */
+#ifdef ENDIAN_NEUTRAL
+
+#define STORE32L(x, y) \
+ do { (y)[3] = (unsigned char)(((x)>>24)&255); (y)[2] = (unsigned char)(((x)>>16)&255); \
+ (y)[1] = (unsigned char)(((x)>>8)&255); (y)[0] = (unsigned char)((x)&255); } while(0)
+
+#define LOAD32L(x, y) \
+ do { x = ((ulong32)((y)[3] & 255)<<24) | \
+ ((ulong32)((y)[2] & 255)<<16) | \
+ ((ulong32)((y)[1] & 255)<<8) | \
+ ((ulong32)((y)[0] & 255)); } while(0)
+
+#define STORE64L(x, y) \
+ do { (y)[7] = (unsigned char)(((x)>>56)&255); (y)[6] = (unsigned char)(((x)>>48)&255); \
+ (y)[5] = (unsigned char)(((x)>>40)&255); (y)[4] = (unsigned char)(((x)>>32)&255); \
+ (y)[3] = (unsigned char)(((x)>>24)&255); (y)[2] = (unsigned char)(((x)>>16)&255); \
+ (y)[1] = (unsigned char)(((x)>>8)&255); (y)[0] = (unsigned char)((x)&255); } while(0)
+
+#define LOAD64L(x, y) \
+ do { x = (((ulong64)((y)[7] & 255))<<56)|(((ulong64)((y)[6] & 255))<<48)| \
+ (((ulong64)((y)[5] & 255))<<40)|(((ulong64)((y)[4] & 255))<<32)| \
+ (((ulong64)((y)[3] & 255))<<24)|(((ulong64)((y)[2] & 255))<<16)| \
+ (((ulong64)((y)[1] & 255))<<8)|(((ulong64)((y)[0] & 255))); } while(0)
+
+#define STORE32H(x, y) \
+ do { (y)[0] = (unsigned char)(((x)>>24)&255); (y)[1] = (unsigned char)(((x)>>16)&255); \
+ (y)[2] = (unsigned char)(((x)>>8)&255); (y)[3] = (unsigned char)((x)&255); } while(0)
+
+#define LOAD32H(x, y) \
+ do { x = ((ulong32)((y)[0] & 255)<<24) | \
+ ((ulong32)((y)[1] & 255)<<16) | \
+ ((ulong32)((y)[2] & 255)<<8) | \
+ ((ulong32)((y)[3] & 255)); } while(0)
+
+#define STORE64H(x, y) \
+do { (y)[0] = (unsigned char)(((x)>>56)&255); (y)[1] = (unsigned char)(((x)>>48)&255); \
+ (y)[2] = (unsigned char)(((x)>>40)&255); (y)[3] = (unsigned char)(((x)>>32)&255); \
+ (y)[4] = (unsigned char)(((x)>>24)&255); (y)[5] = (unsigned char)(((x)>>16)&255); \
+ (y)[6] = (unsigned char)(((x)>>8)&255); (y)[7] = (unsigned char)((x)&255); } while(0)
+
+#define LOAD64H(x, y) \
+do { x = (((ulong64)((y)[0] & 255))<<56)|(((ulong64)((y)[1] & 255))<<48) | \
+ (((ulong64)((y)[2] & 255))<<40)|(((ulong64)((y)[3] & 255))<<32) | \
+ (((ulong64)((y)[4] & 255))<<24)|(((ulong64)((y)[5] & 255))<<16) | \
+ (((ulong64)((y)[6] & 255))<<8)|(((ulong64)((y)[7] & 255))); } while(0)
+
+
+#elif defined(ENDIAN_LITTLE)
+
+#ifdef LTC_HAVE_BSWAP_BUILTIN
+
+#define STORE32H(x, y) \
+do { ulong32 __t = __builtin_bswap32 ((x)); \
+ XMEMCPY ((y), &__t, 4); } while(0)
+
+#define LOAD32H(x, y) \
+do { XMEMCPY (&(x), (y), 4); \
+ (x) = __builtin_bswap32 ((x)); } while(0)
+
+#elif !defined(LTC_NO_BSWAP) && (defined(INTEL_CC) || (defined(__GNUC__) && (defined(__DJGPP__) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__i386__) || defined(__x86_64__))))
+
+#define STORE32H(x, y) \
+asm __volatile__ ( \
+ "bswapl %0 \n\t" \
+ "movl %0,(%1)\n\t" \
+ "bswapl %0 \n\t" \
+ ::"r"(x), "r"(y));
+
+#define LOAD32H(x, y) \
+asm __volatile__ ( \
+ "movl (%1),%0\n\t" \
+ "bswapl %0\n\t" \
+ :"=r"(x): "r"(y));
+
+#else
+
+#define STORE32H(x, y) \
+ do { (y)[0] = (unsigned char)(((x)>>24)&255); (y)[1] = (unsigned char)(((x)>>16)&255); \
+ (y)[2] = (unsigned char)(((x)>>8)&255); (y)[3] = (unsigned char)((x)&255); } while(0)
+
+#define LOAD32H(x, y) \
+ do { x = ((ulong32)((y)[0] & 255)<<24) | \
+ ((ulong32)((y)[1] & 255)<<16) | \
+ ((ulong32)((y)[2] & 255)<<8) | \
+ ((ulong32)((y)[3] & 255)); } while(0)
+
+#endif
+
+#ifdef LTC_HAVE_BSWAP_BUILTIN
+
+#define STORE64H(x, y) \
+do { ulong64 __t = __builtin_bswap64 ((x)); \
+ XMEMCPY ((y), &__t, 8); } while(0)
+
+#define LOAD64H(x, y) \
+do { XMEMCPY (&(x), (y), 8); \
+ (x) = __builtin_bswap64 ((x)); } while(0)
+
+/* x86_64 processor */
+#elif !defined(LTC_NO_BSWAP) && (defined(__GNUC__) && defined(__x86_64__))
+
+#define STORE64H(x, y) \
+asm __volatile__ ( \
+ "bswapq %0 \n\t" \
+ "movq %0,(%1)\n\t" \
+ "bswapq %0 \n\t" \
+ ::"r"(x), "r"(y): "memory");
+
+#define LOAD64H(x, y) \
+asm __volatile__ ( \
+ "movq (%1),%0\n\t" \
+ "bswapq %0\n\t" \
+ :"=r"(x): "r"(y): "memory");
+
+#else
+
+#define STORE64H(x, y) \
+do { (y)[0] = (unsigned char)(((x)>>56)&255); (y)[1] = (unsigned char)(((x)>>48)&255); \
+ (y)[2] = (unsigned char)(((x)>>40)&255); (y)[3] = (unsigned char)(((x)>>32)&255); \
+ (y)[4] = (unsigned char)(((x)>>24)&255); (y)[5] = (unsigned char)(((x)>>16)&255); \
+ (y)[6] = (unsigned char)(((x)>>8)&255); (y)[7] = (unsigned char)((x)&255); } while(0)
+
+#define LOAD64H(x, y) \
+do { x = (((ulong64)((y)[0] & 255))<<56)|(((ulong64)((y)[1] & 255))<<48) | \
+ (((ulong64)((y)[2] & 255))<<40)|(((ulong64)((y)[3] & 255))<<32) | \
+ (((ulong64)((y)[4] & 255))<<24)|(((ulong64)((y)[5] & 255))<<16) | \
+ (((ulong64)((y)[6] & 255))<<8)|(((ulong64)((y)[7] & 255))); } while(0)
+
+#endif
+
+#ifdef ENDIAN_32BITWORD
+
+#define STORE32L(x, y) \
+ do { ulong32 __t = (x); XMEMCPY(y, &__t, 4); } while(0)
+
+#define LOAD32L(x, y) \
+ do { XMEMCPY(&(x), y, 4); } while(0)
+
+#define STORE64L(x, y) \
+ do { (y)[7] = (unsigned char)(((x)>>56)&255); (y)[6] = (unsigned char)(((x)>>48)&255); \
+ (y)[5] = (unsigned char)(((x)>>40)&255); (y)[4] = (unsigned char)(((x)>>32)&255); \
+ (y)[3] = (unsigned char)(((x)>>24)&255); (y)[2] = (unsigned char)(((x)>>16)&255); \
+ (y)[1] = (unsigned char)(((x)>>8)&255); (y)[0] = (unsigned char)((x)&255); } while(0)
+
+#define LOAD64L(x, y) \
+ do { x = (((ulong64)((y)[7] & 255))<<56)|(((ulong64)((y)[6] & 255))<<48)| \
+ (((ulong64)((y)[5] & 255))<<40)|(((ulong64)((y)[4] & 255))<<32)| \
+ (((ulong64)((y)[3] & 255))<<24)|(((ulong64)((y)[2] & 255))<<16)| \
+ (((ulong64)((y)[1] & 255))<<8)|(((ulong64)((y)[0] & 255))); } while(0)
+
+#else /* 64-bit words then */
+
+#define STORE32L(x, y) \
+ do { ulong32 __t = (x); XMEMCPY(y, &__t, 4); } while(0)
+
+#define LOAD32L(x, y) \
+ do { XMEMCPY(&(x), y, 4); x &= 0xFFFFFFFF; } while(0)
+
+#define STORE64L(x, y) \
+ do { ulong64 __t = (x); XMEMCPY(y, &__t, 8); } while(0)
+
+#define LOAD64L(x, y) \
+ do { XMEMCPY(&(x), y, 8); } while(0)
+
+#endif /* ENDIAN_64BITWORD */
+
+#elif defined(ENDIAN_BIG)
+
+#define STORE32L(x, y) \
+ do { (y)[3] = (unsigned char)(((x)>>24)&255); (y)[2] = (unsigned char)(((x)>>16)&255); \
+ (y)[1] = (unsigned char)(((x)>>8)&255); (y)[0] = (unsigned char)((x)&255); } while(0)
+
+#define LOAD32L(x, y) \
+ do { x = ((ulong32)((y)[3] & 255)<<24) | \
+ ((ulong32)((y)[2] & 255)<<16) | \
+ ((ulong32)((y)[1] & 255)<<8) | \
+ ((ulong32)((y)[0] & 255)); } while(0)
+
+#define STORE64L(x, y) \
+do { (y)[7] = (unsigned char)(((x)>>56)&255); (y)[6] = (unsigned char)(((x)>>48)&255); \
+ (y)[5] = (unsigned char)(((x)>>40)&255); (y)[4] = (unsigned char)(((x)>>32)&255); \
+ (y)[3] = (unsigned char)(((x)>>24)&255); (y)[2] = (unsigned char)(((x)>>16)&255); \
+ (y)[1] = (unsigned char)(((x)>>8)&255); (y)[0] = (unsigned char)((x)&255); } while(0)
+
+#define LOAD64L(x, y) \
+do { x = (((ulong64)((y)[7] & 255))<<56)|(((ulong64)((y)[6] & 255))<<48) | \
+ (((ulong64)((y)[5] & 255))<<40)|(((ulong64)((y)[4] & 255))<<32) | \
+ (((ulong64)((y)[3] & 255))<<24)|(((ulong64)((y)[2] & 255))<<16) | \
+ (((ulong64)((y)[1] & 255))<<8)|(((ulong64)((y)[0] & 255))); } while(0)
+
+#ifdef ENDIAN_32BITWORD
+
+#define STORE32H(x, y) \
+ do { ulong32 __t = (x); XMEMCPY(y, &__t, 4); } while(0)
+
+#define LOAD32H(x, y) \
+ do { XMEMCPY(&(x), y, 4); } while(0)
+
+#define STORE64H(x, y) \
+ do { (y)[0] = (unsigned char)(((x)>>56)&255); (y)[1] = (unsigned char)(((x)>>48)&255); \
+ (y)[2] = (unsigned char)(((x)>>40)&255); (y)[3] = (unsigned char)(((x)>>32)&255); \
+ (y)[4] = (unsigned char)(((x)>>24)&255); (y)[5] = (unsigned char)(((x)>>16)&255); \
+ (y)[6] = (unsigned char)(((x)>>8)&255); (y)[7] = (unsigned char)((x)&255); } while(0)
+
+#define LOAD64H(x, y) \
+ do { x = (((ulong64)((y)[0] & 255))<<56)|(((ulong64)((y)[1] & 255))<<48)| \
+ (((ulong64)((y)[2] & 255))<<40)|(((ulong64)((y)[3] & 255))<<32)| \
+ (((ulong64)((y)[4] & 255))<<24)|(((ulong64)((y)[5] & 255))<<16)| \
+ (((ulong64)((y)[6] & 255))<<8)| (((ulong64)((y)[7] & 255))); } while(0)
+
+#else /* 64-bit words then */
+
+#define STORE32H(x, y) \
+ do { ulong32 __t = (x); XMEMCPY(y, &__t, 4); } while(0)
+
+#define LOAD32H(x, y) \
+ do { XMEMCPY(&(x), y, 4); x &= 0xFFFFFFFF; } while(0)
+
+#define STORE64H(x, y) \
+ do { ulong64 __t = (x); XMEMCPY(y, &__t, 8); } while(0)
+
+#define LOAD64H(x, y) \
+ do { XMEMCPY(&(x), y, 8); } while(0)
+
+#endif /* ENDIAN_64BITWORD */
+#endif /* ENDIAN_BIG */
+
+#define BSWAP(x) ( ((x>>24)&0x000000FFUL) | ((x<<24)&0xFF000000UL) | \
+ ((x>>8)&0x0000FF00UL) | ((x<<8)&0x00FF0000UL) )
+
+
+/* 32-bit Rotates */
+#if defined(_MSC_VER)
+#define LTC_ROx_ASM
+
+/* instrinsic rotate */
+#include <stdlib.h>
+#pragma intrinsic(_lrotr,_lrotl)
+#define ROR(x,n) _lrotr(x,n)
+#define ROL(x,n) _lrotl(x,n)
+#define RORc(x,n) _lrotr(x,n)
+#define ROLc(x,n) _lrotl(x,n)
+
+#elif !defined(__STRICT_ANSI__) && defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__)) && !defined(INTEL_CC) && !defined(LTC_NO_ASM)
+#define LTC_ROx_ASM
+
+static inline ulong32 ROL(ulong32 word, int i)
+{
+ asm ("roll %%cl,%0"
+ :"=r" (word)
+ :"0" (word),"c" (i));
+ return word;
+}
+
+static inline ulong32 ROR(ulong32 word, int i)
+{
+ asm ("rorl %%cl,%0"
+ :"=r" (word)
+ :"0" (word),"c" (i));
+ return word;
+}
+
+#ifndef LTC_NO_ROLC
+
+#define ROLc(word,i) ({ \
+ ulong32 __ROLc_tmp = (word); \
+ __asm__ ("roll %2, %0" : \
+ "=r" (__ROLc_tmp) : \
+ "0" (__ROLc_tmp), \
+ "I" (i)); \
+ __ROLc_tmp; \
+ })
+#define RORc(word,i) ({ \
+ ulong32 __RORc_tmp = (word); \
+ __asm__ ("rorl %2, %0" : \
+ "=r" (__RORc_tmp) : \
+ "0" (__RORc_tmp), \
+ "I" (i)); \
+ __RORc_tmp; \
+ })
+
+#else
+
+#define ROLc ROL
+#define RORc ROR
+
+#endif
+
+#elif !defined(__STRICT_ANSI__) && defined(LTC_PPC32)
+#define LTC_ROx_ASM
+
+static inline ulong32 ROL(ulong32 word, int i)
+{
+ asm ("rotlw %0,%0,%2"
+ :"=r" (word)
+ :"0" (word),"r" (i));
+ return word;
+}
+
+static inline ulong32 ROR(ulong32 word, int i)
+{
+ asm ("rotlw %0,%0,%2"
+ :"=r" (word)
+ :"0" (word),"r" (32-i));
+ return word;
+}
+
+#ifndef LTC_NO_ROLC
+
+static inline ulong32 ROLc(ulong32 word, const int i)
+{
+ asm ("rotlwi %0,%0,%2"
+ :"=r" (word)
+ :"0" (word),"I" (i));
+ return word;
+}
+
+static inline ulong32 RORc(ulong32 word, const int i)
+{
+ asm ("rotrwi %0,%0,%2"
+ :"=r" (word)
+ :"0" (word),"I" (i));
+ return word;
+}
+
+#else
+
+#define ROLc ROL
+#define RORc ROR
+
+#endif
+
+
+#else
+
+/* rotates the hard way */
+#define ROL(x, y) ( (((ulong32)(x)<<(ulong32)((y)&31)) | (((ulong32)(x)&0xFFFFFFFFUL)>>(ulong32)((32-((y)&31))&31))) & 0xFFFFFFFFUL)
+#define ROR(x, y) ( ((((ulong32)(x)&0xFFFFFFFFUL)>>(ulong32)((y)&31)) | ((ulong32)(x)<<(ulong32)((32-((y)&31))&31))) & 0xFFFFFFFFUL)
+#define ROLc(x, y) ( (((ulong32)(x)<<(ulong32)((y)&31)) | (((ulong32)(x)&0xFFFFFFFFUL)>>(ulong32)((32-((y)&31))&31))) & 0xFFFFFFFFUL)
+#define RORc(x, y) ( ((((ulong32)(x)&0xFFFFFFFFUL)>>(ulong32)((y)&31)) | ((ulong32)(x)<<(ulong32)((32-((y)&31))&31))) & 0xFFFFFFFFUL)
+
+#endif
+
+
+/* 64-bit Rotates */
+#if !defined(__STRICT_ANSI__) && defined(__GNUC__) && defined(__x86_64__) && !defined(_WIN64) && !defined(LTC_NO_ASM)
+
+static inline ulong64 ROL64(ulong64 word, int i)
+{
+ asm("rolq %%cl,%0"
+ :"=r" (word)
+ :"0" (word),"c" (i));
+ return word;
+}
+
+static inline ulong64 ROR64(ulong64 word, int i)
+{
+ asm("rorq %%cl,%0"
+ :"=r" (word)
+ :"0" (word),"c" (i));
+ return word;
+}
+
+#ifndef LTC_NO_ROLC
+
+#define ROL64c(word,i) ({ \
+ ulong64 __ROL64c_tmp = word; \
+ __asm__ ("rolq %2, %0" : \
+ "=r" (__ROL64c_tmp) : \
+ "0" (__ROL64c_tmp), \
+ "J" (i)); \
+ __ROL64c_tmp; \
+ })
+#define ROR64c(word,i) ({ \
+ ulong64 __ROR64c_tmp = word; \
+ __asm__ ("rorq %2, %0" : \
+ "=r" (__ROR64c_tmp) : \
+ "0" (__ROR64c_tmp), \
+ "J" (i)); \
+ __ROR64c_tmp; \
+ })
+
+#else /* LTC_NO_ROLC */
+
+#define ROL64c ROL64
+#define ROR64c ROR64
+
+#endif
+
+#else /* Not x86_64 */
+
+#define ROL64(x, y) \
+ ( (((x)<<((ulong64)(y)&63)) | \
+ (((x)&CONST64(0xFFFFFFFFFFFFFFFF))>>(((ulong64)64-((y)&63))&63))) & CONST64(0xFFFFFFFFFFFFFFFF))
+
+#define ROR64(x, y) \
+ ( ((((x)&CONST64(0xFFFFFFFFFFFFFFFF))>>((ulong64)(y)&CONST64(63))) | \
+ ((x)<<(((ulong64)64-((y)&63))&63))) & CONST64(0xFFFFFFFFFFFFFFFF))
+
+#define ROL64c(x, y) \
+ ( (((x)<<((ulong64)(y)&63)) | \
+ (((x)&CONST64(0xFFFFFFFFFFFFFFFF))>>(((ulong64)64-((y)&63))&63))) & CONST64(0xFFFFFFFFFFFFFFFF))
+
+#define ROR64c(x, y) \
+ ( ((((x)&CONST64(0xFFFFFFFFFFFFFFFF))>>((ulong64)(y)&CONST64(63))) | \
+ ((x)<<(((ulong64)64-((y)&63))&63))) & CONST64(0xFFFFFFFFFFFFFFFF))
+
+#endif
+
+#ifndef MAX
+ #define MAX(x, y) ( ((x)>(y))?(x):(y) )
+#endif
+
+#ifndef MIN
+ #define MIN(x, y) ( ((x)<(y))?(x):(y) )
+#endif
+
+#ifndef LTC_UNUSED_PARAM
+ #define LTC_UNUSED_PARAM(x) (void)(x)
+#endif
+
+/* extract a byte portably */
+#ifdef _MSC_VER
+ #define byte(x, n) ((unsigned char)((x) >> (8 * (n))))
+#else
+ #define byte(x, n) (((x) >> (8 * (n))) & 255)
+#endif
+
+/* there is no snprintf before Visual C++ 2015 */
+#if defined(_MSC_VER) && _MSC_VER < 1900
+#define snprintf _snprintf
+#endif
+
+/* ref: HEAD -> release/1.18.0, tag: v1.18.0-rc2 */
+/* git commit: aa0f396c0c8828ce39456129507fc72ef0208bd0 */
+/* commit time: 2017-07-13 14:58:01 +0200 */
diff --git a/src/third_party/tomcrypt-1.18.0-rc2/src/headers/tomcrypt_math.h b/src/third_party/tomcrypt-1.18.0-rc2/src/headers/tomcrypt_math.h
new file mode 100644
index 00000000000..fd7534d5de8
--- /dev/null
+++ b/src/third_party/tomcrypt-1.18.0-rc2/src/headers/tomcrypt_math.h
@@ -0,0 +1,582 @@
+/* LibTomCrypt, modular cryptographic library -- Tom St Denis
+ *
+ * LibTomCrypt is a library that provides various cryptographic
+ * algorithms in a highly modular and flexible manner.
+ *
+ * The library is free for all purposes without any express
+ * guarantee it works.
+ */
+
+/** math functions **/
+
+#define LTC_MP_LT -1
+#define LTC_MP_EQ 0
+#define LTC_MP_GT 1
+
+#define LTC_MP_NO 0
+#define LTC_MP_YES 1
+
+#ifndef LTC_MECC
+ typedef void ecc_point;
+#endif
+
+#ifndef LTC_MRSA
+ typedef void rsa_key;
+#endif
+
+#ifndef LTC_MILLER_RABIN_REPS
+ /* Number of rounds of the Miller-Rabin test
+ * "Reasonable values of reps are between 15 and 50." c.f. gmp doc of mpz_probab_prime_p() */
+ #define LTC_MILLER_RABIN_REPS 35
+#endif
+
+int radix_to_bin(const void *in, int radix, void *out, size_t* len);
+
+/** math descriptor */
+typedef struct {
+ /** Name of the math provider */
+ char *name;
+
+ /** Bits per digit, amount of bits must fit in an unsigned long */
+ int bits_per_digit;
+
+/* ---- init/deinit functions ---- */
+
+ /** initialize a bignum
+ @param a The number to initialize
+ @return CRYPT_OK on success
+ */
+ int (*init)(void **a);
+
+ /** init copy
+ @param dst The number to initialize and write to
+ @param src The number to copy from
+ @return CRYPT_OK on success
+ */
+ int (*init_copy)(void **dst, void *src);
+
+ /** deinit
+ @param a The number to free
+ @return CRYPT_OK on success
+ */
+ void (*deinit)(void *a);
+
+/* ---- data movement ---- */
+
+ /** negate
+ @param src The number to negate
+ @param dst The destination
+ @return CRYPT_OK on success
+ */
+ int (*neg)(void *src, void *dst);
+
+ /** copy
+ @param src The number to copy from
+ @param dst The number to write to
+ @return CRYPT_OK on success
+ */
+ int (*copy)(void *src, void *dst);
+
+/* ---- trivial low level functions ---- */
+
+ /** set small constant
+ @param a Number to write to
+ @param n Source upto bits_per_digit (actually meant for very small constants)
+ @return CRYPT_OK on success
+ */
+ int (*set_int)(void *a, ltc_mp_digit n);
+
+ /** get small constant
+ @param a Small number to read,
+ only fetches up to bits_per_digit from the number
+ @return The lower bits_per_digit of the integer (unsigned)
+ */
+ unsigned long (*get_int)(void *a);
+
+ /** get digit n
+ @param a The number to read from
+ @param n The number of the digit to fetch
+ @return The bits_per_digit sized n'th digit of a
+ */
+ ltc_mp_digit (*get_digit)(void *a, int n);
+
+ /** Get the number of digits that represent the number
+ @param a The number to count
+ @return The number of digits used to represent the number
+ */
+ int (*get_digit_count)(void *a);
+
+ /** compare two integers
+ @param a The left side integer
+ @param b The right side integer
+ @return LTC_MP_LT if a < b,
+ LTC_MP_GT if a > b and
+ LTC_MP_EQ otherwise. (signed comparison)
+ */
+ int (*compare)(void *a, void *b);
+
+ /** compare against int
+ @param a The left side integer
+ @param b The right side integer (upto bits_per_digit)
+ @return LTC_MP_LT if a < b,
+ LTC_MP_GT if a > b and
+ LTC_MP_EQ otherwise. (signed comparison)
+ */
+ int (*compare_d)(void *a, ltc_mp_digit n);
+
+ /** Count the number of bits used to represent the integer
+ @param a The integer to count
+ @return The number of bits required to represent the integer
+ */
+ int (*count_bits)(void * a);
+
+ /** Count the number of LSB bits which are zero
+ @param a The integer to count
+ @return The number of contiguous zero LSB bits
+ */
+ int (*count_lsb_bits)(void *a);
+
+ /** Compute a power of two
+ @param a The integer to store the power in
+ @param n The power of two you want to store (a = 2^n)
+ @return CRYPT_OK on success
+ */
+ int (*twoexpt)(void *a , int n);
+
+/* ---- radix conversions ---- */
+
+ /** read ascii string
+ @param a The integer to store into
+ @param str The string to read
+ @param radix The radix the integer has been represented in (2-64)
+ @return CRYPT_OK on success
+ */
+ int (*read_radix)(void *a, const char *str, int radix);
+
+ /** write number to string
+ @param a The integer to store
+ @param str The destination for the string
+ @param radix The radix the integer is to be represented in (2-64)
+ @return CRYPT_OK on success
+ */
+ int (*write_radix)(void *a, char *str, int radix);
+
+ /** get size as unsigned char string
+ @param a The integer to get the size (when stored in array of octets)
+ @return The length of the integer in octets
+ */
+ unsigned long (*unsigned_size)(void *a);
+
+ /** store an integer as an array of octets
+ @param src The integer to store
+ @param dst The buffer to store the integer in
+ @return CRYPT_OK on success
+ */
+ int (*unsigned_write)(void *src, unsigned char *dst);
+
+ /** read an array of octets and store as integer
+ @param dst The integer to load
+ @param src The array of octets
+ @param len The number of octets
+ @return CRYPT_OK on success
+ */
+ int (*unsigned_read)( void *dst,
+ unsigned char *src,
+ unsigned long len);
+
+/* ---- basic math ---- */
+
+ /** add two integers
+ @param a The first source integer
+ @param b The second source integer
+ @param c The destination of "a + b"
+ @return CRYPT_OK on success
+ */
+ int (*add)(void *a, void *b, void *c);
+
+ /** add two integers
+ @param a The first source integer
+ @param b The second source integer
+ (single digit of upto bits_per_digit in length)
+ @param c The destination of "a + b"
+ @return CRYPT_OK on success
+ */
+ int (*addi)(void *a, ltc_mp_digit b, void *c);
+
+ /** subtract two integers
+ @param a The first source integer
+ @param b The second source integer
+ @param c The destination of "a - b"
+ @return CRYPT_OK on success
+ */
+ int (*sub)(void *a, void *b, void *c);
+
+ /** subtract two integers
+ @param a The first source integer
+ @param b The second source integer
+ (single digit of upto bits_per_digit in length)
+ @param c The destination of "a - b"
+ @return CRYPT_OK on success
+ */
+ int (*subi)(void *a, ltc_mp_digit b, void *c);
+
+ /** multiply two integers
+ @param a The first source integer
+ @param b The second source integer
+ (single digit of upto bits_per_digit in length)
+ @param c The destination of "a * b"
+ @return CRYPT_OK on success
+ */
+ int (*mul)(void *a, void *b, void *c);
+
+ /** multiply two integers
+ @param a The first source integer
+ @param b The second source integer
+ (single digit of upto bits_per_digit in length)
+ @param c The destination of "a * b"
+ @return CRYPT_OK on success
+ */
+ int (*muli)(void *a, ltc_mp_digit b, void *c);
+
+ /** Square an integer
+ @param a The integer to square
+ @param b The destination
+ @return CRYPT_OK on success
+ */
+ int (*sqr)(void *a, void *b);
+
+ /** Divide an integer
+ @param a The dividend
+ @param b The divisor
+ @param c The quotient (can be NULL to signify don't care)
+ @param d The remainder (can be NULL to signify don't care)
+ @return CRYPT_OK on success
+ */
+ int (*mpdiv)(void *a, void *b, void *c, void *d);
+
+ /** divide by two
+ @param a The integer to divide (shift right)
+ @param b The destination
+ @return CRYPT_OK on success
+ */
+ int (*div_2)(void *a, void *b);
+
+ /** Get remainder (small value)
+ @param a The integer to reduce
+ @param b The modulus (upto bits_per_digit in length)
+ @param c The destination for the residue
+ @return CRYPT_OK on success
+ */
+ int (*modi)(void *a, ltc_mp_digit b, ltc_mp_digit *c);
+
+ /** gcd
+ @param a The first integer
+ @param b The second integer
+ @param c The destination for (a, b)
+ @return CRYPT_OK on success
+ */
+ int (*gcd)(void *a, void *b, void *c);
+
+ /** lcm
+ @param a The first integer
+ @param b The second integer
+ @param c The destination for [a, b]
+ @return CRYPT_OK on success
+ */
+ int (*lcm)(void *a, void *b, void *c);
+
+ /** Modular multiplication
+ @param a The first source
+ @param b The second source
+ @param c The modulus
+ @param d The destination (a*b mod c)
+ @return CRYPT_OK on success
+ */
+ int (*mulmod)(void *a, void *b, void *c, void *d);
+
+ /** Modular squaring
+ @param a The first source
+ @param b The modulus
+ @param c The destination (a*a mod b)
+ @return CRYPT_OK on success
+ */
+ int (*sqrmod)(void *a, void *b, void *c);
+
+ /** Modular inversion
+ @param a The value to invert
+ @param b The modulus
+ @param c The destination (1/a mod b)
+ @return CRYPT_OK on success
+ */
+ int (*invmod)(void *, void *, void *);
+
+/* ---- reduction ---- */
+
+ /** setup Montgomery
+ @param a The modulus
+ @param b The destination for the reduction digit
+ @return CRYPT_OK on success
+ */
+ int (*montgomery_setup)(void *a, void **b);
+
+ /** get normalization value
+ @param a The destination for the normalization value
+ @param b The modulus
+ @return CRYPT_OK on success
+ */
+ int (*montgomery_normalization)(void *a, void *b);
+
+ /** reduce a number
+ @param a The number [and dest] to reduce
+ @param b The modulus
+ @param c The value "b" from montgomery_setup()
+ @return CRYPT_OK on success
+ */
+ int (*montgomery_reduce)(void *a, void *b, void *c);
+
+ /** clean up (frees memory)
+ @param a The value "b" from montgomery_setup()
+ @return CRYPT_OK on success
+ */
+ void (*montgomery_deinit)(void *a);
+
+/* ---- exponentiation ---- */
+
+ /** Modular exponentiation
+ @param a The base integer
+ @param b The power (can be negative) integer
+ @param c The modulus integer
+ @param d The destination
+ @return CRYPT_OK on success
+ */
+ int (*exptmod)(void *a, void *b, void *c, void *d);
+
+ /** Primality testing
+ @param a The integer to test
+ @param b The number of Miller-Rabin tests that shall be executed
+ @param c The destination of the result (FP_YES if prime)
+ @return CRYPT_OK on success
+ */
+ int (*isprime)(void *a, int b, int *c);
+
+/* ---- (optional) ecc point math ---- */
+
+ /** ECC GF(p) point multiplication (from the NIST curves)
+ @param k The integer to multiply the point by
+ @param G The point to multiply
+ @param R The destination for kG
+ @param modulus The modulus for the field
+ @param map Boolean indicated whether to map back to affine or not
+ (can be ignored if you work in affine only)
+ @return CRYPT_OK on success
+ */
+ int (*ecc_ptmul)( void *k,
+ ecc_point *G,
+ ecc_point *R,
+ void *modulus,
+ int map);
+
+ /** ECC GF(p) point addition
+ @param P The first point
+ @param Q The second point
+ @param R The destination of P + Q
+ @param modulus The modulus
+ @param mp The "b" value from montgomery_setup()
+ @return CRYPT_OK on success
+ */
+ int (*ecc_ptadd)(ecc_point *P,
+ ecc_point *Q,
+ ecc_point *R,
+ void *modulus,
+ void *mp);
+
+ /** ECC GF(p) point double
+ @param P The first point
+ @param R The destination of 2P
+ @param modulus The modulus
+ @param mp The "b" value from montgomery_setup()
+ @return CRYPT_OK on success
+ */
+ int (*ecc_ptdbl)(ecc_point *P,
+ ecc_point *R,
+ void *modulus,
+ void *mp);
+
+ /** ECC mapping from projective to affine,
+ currently uses (x,y,z) => (x/z^2, y/z^3, 1)
+ @param P The point to map
+ @param modulus The modulus
+ @param mp The "b" value from montgomery_setup()
+ @return CRYPT_OK on success
+ @remark The mapping can be different but keep in mind a
+ ecc_point only has three integers (x,y,z) so if
+ you use a different mapping you have to make it fit.
+ */
+ int (*ecc_map)(ecc_point *P, void *modulus, void *mp);
+
+ /** Computes kA*A + kB*B = C using Shamir's Trick
+ @param A First point to multiply
+ @param kA What to multiple A by
+ @param B Second point to multiply
+ @param kB What to multiple B by
+ @param C [out] Destination point (can overlap with A or B)
+ @param modulus Modulus for curve
+ @return CRYPT_OK on success
+ */
+ int (*ecc_mul2add)(ecc_point *A, void *kA,
+ ecc_point *B, void *kB,
+ ecc_point *C,
+ void *modulus);
+
+/* ---- (optional) rsa optimized math (for internal CRT) ---- */
+
+ /** RSA Key Generation
+ @param prng An active PRNG state
+ @param wprng The index of the PRNG desired
+ @param size The size of the key in octets
+ @param e The "e" value (public key).
+ e==65537 is a good choice
+ @param key [out] Destination of a newly created private key pair
+ @return CRYPT_OK if successful, upon error all allocated ram is freed
+ */
+ int (*rsa_keygen)(prng_state *prng,
+ int wprng,
+ int size,
+ long e,
+ rsa_key *key);
+
+ /** RSA exponentiation
+ @param in The octet array representing the base
+ @param inlen The length of the input
+ @param out The destination (to be stored in an octet array format)
+ @param outlen The length of the output buffer and the resulting size
+ (zero padded to the size of the modulus)
+ @param which PK_PUBLIC for public RSA and PK_PRIVATE for private RSA
+ @param key The RSA key to use
+ @return CRYPT_OK on success
+ */
+ int (*rsa_me)(const unsigned char *in, unsigned long inlen,
+ unsigned char *out, unsigned long *outlen, int which,
+ rsa_key *key);
+
+/* ---- basic math continued ---- */
+
+ /** Modular addition
+ @param a The first source
+ @param b The second source
+ @param c The modulus
+ @param d The destination (a + b mod c)
+ @return CRYPT_OK on success
+ */
+ int (*addmod)(void *a, void *b, void *c, void *d);
+
+ /** Modular substraction
+ @param a The first source
+ @param b The second source
+ @param c The modulus
+ @param d The destination (a - b mod c)
+ @return CRYPT_OK on success
+ */
+ int (*submod)(void *a, void *b, void *c, void *d);
+
+/* ---- misc stuff ---- */
+
+ /** Make a pseudo-random mpi
+ @param a The mpi to make random
+ @param size The desired length
+ @return CRYPT_OK on success
+ */
+ int (*rand)(void *a, int size);
+} ltc_math_descriptor;
+
+extern ltc_math_descriptor ltc_mp;
+
+int ltc_init_multi(void **a, ...);
+void ltc_deinit_multi(void *a, ...);
+void ltc_cleanup_multi(void **a, ...);
+
+#ifdef LTM_DESC
+extern const ltc_math_descriptor ltm_desc;
+#endif
+
+#ifdef TFM_DESC
+extern const ltc_math_descriptor tfm_desc;
+#endif
+
+#ifdef GMP_DESC
+extern const ltc_math_descriptor gmp_desc;
+#endif
+
+#if !defined(DESC_DEF_ONLY) && defined(LTC_SOURCE)
+
+#define MP_DIGIT_BIT ltc_mp.bits_per_digit
+
+/* some handy macros */
+#define mp_init(a) ltc_mp.init(a)
+#define mp_init_multi ltc_init_multi
+#define mp_clear(a) ltc_mp.deinit(a)
+#define mp_clear_multi ltc_deinit_multi
+#define mp_cleanup_multi ltc_cleanup_multi
+#define mp_init_copy(a, b) ltc_mp.init_copy(a, b)
+
+#define mp_neg(a, b) ltc_mp.neg(a, b)
+#define mp_copy(a, b) ltc_mp.copy(a, b)
+
+#define mp_set(a, b) ltc_mp.set_int(a, b)
+#define mp_set_int(a, b) ltc_mp.set_int(a, b)
+#define mp_get_int(a) ltc_mp.get_int(a)
+#define mp_get_digit(a, n) ltc_mp.get_digit(a, n)
+#define mp_get_digit_count(a) ltc_mp.get_digit_count(a)
+#define mp_cmp(a, b) ltc_mp.compare(a, b)
+#define mp_cmp_d(a, b) ltc_mp.compare_d(a, b)
+#define mp_count_bits(a) ltc_mp.count_bits(a)
+#define mp_cnt_lsb(a) ltc_mp.count_lsb_bits(a)
+#define mp_2expt(a, b) ltc_mp.twoexpt(a, b)
+
+#define mp_read_radix(a, b, c) ltc_mp.read_radix(a, b, c)
+#define mp_toradix(a, b, c) ltc_mp.write_radix(a, b, c)
+#define mp_unsigned_bin_size(a) ltc_mp.unsigned_size(a)
+#define mp_to_unsigned_bin(a, b) ltc_mp.unsigned_write(a, b)
+#define mp_read_unsigned_bin(a, b, c) ltc_mp.unsigned_read(a, b, c)
+
+#define mp_add(a, b, c) ltc_mp.add(a, b, c)
+#define mp_add_d(a, b, c) ltc_mp.addi(a, b, c)
+#define mp_sub(a, b, c) ltc_mp.sub(a, b, c)
+#define mp_sub_d(a, b, c) ltc_mp.subi(a, b, c)
+#define mp_mul(a, b, c) ltc_mp.mul(a, b, c)
+#define mp_mul_d(a, b, c) ltc_mp.muli(a, b, c)
+#define mp_sqr(a, b) ltc_mp.sqr(a, b)
+#define mp_div(a, b, c, d) ltc_mp.mpdiv(a, b, c, d)
+#define mp_div_2(a, b) ltc_mp.div_2(a, b)
+#define mp_mod(a, b, c) ltc_mp.mpdiv(a, b, NULL, c)
+#define mp_mod_d(a, b, c) ltc_mp.modi(a, b, c)
+#define mp_gcd(a, b, c) ltc_mp.gcd(a, b, c)
+#define mp_lcm(a, b, c) ltc_mp.lcm(a, b, c)
+
+#define mp_addmod(a, b, c, d) ltc_mp.addmod(a, b, c, d)
+#define mp_submod(a, b, c, d) ltc_mp.submod(a, b, c, d)
+#define mp_mulmod(a, b, c, d) ltc_mp.mulmod(a, b, c, d)
+#define mp_sqrmod(a, b, c) ltc_mp.sqrmod(a, b, c)
+#define mp_invmod(a, b, c) ltc_mp.invmod(a, b, c)
+
+#define mp_montgomery_setup(a, b) ltc_mp.montgomery_setup(a, b)
+#define mp_montgomery_normalization(a, b) ltc_mp.montgomery_normalization(a, b)
+#define mp_montgomery_reduce(a, b, c) ltc_mp.montgomery_reduce(a, b, c)
+#define mp_montgomery_free(a) ltc_mp.montgomery_deinit(a)
+
+#define mp_exptmod(a,b,c,d) ltc_mp.exptmod(a,b,c,d)
+#define mp_prime_is_prime(a, b, c) ltc_mp.isprime(a, b, c)
+
+#define mp_iszero(a) (mp_cmp_d(a, 0) == LTC_MP_EQ ? LTC_MP_YES : LTC_MP_NO)
+#define mp_isodd(a) (mp_get_digit_count(a) > 0 ? (mp_get_digit(a, 0) & 1 ? LTC_MP_YES : LTC_MP_NO) : LTC_MP_NO)
+#define mp_exch(a, b) do { void *ABC__tmp = a; a = b; b = ABC__tmp; } while(0)
+
+#define mp_tohex(a, b) mp_toradix(a, b, 16)
+
+#define mp_rand(a, b) ltc_mp.rand(a, b)
+
+#endif
+
+/* ref: HEAD -> release/1.18.0, tag: v1.18.0-rc2 */
+/* git commit: aa0f396c0c8828ce39456129507fc72ef0208bd0 */
+/* commit time: 2017-07-13 14:58:01 +0200 */
diff --git a/src/third_party/tomcrypt-1.18.0-rc2/src/headers/tomcrypt_misc.h b/src/third_party/tomcrypt-1.18.0-rc2/src/headers/tomcrypt_misc.h
new file mode 100644
index 00000000000..cb863289213
--- /dev/null
+++ b/src/third_party/tomcrypt-1.18.0-rc2/src/headers/tomcrypt_misc.h
@@ -0,0 +1,115 @@
+/* LibTomCrypt, modular cryptographic library -- Tom St Denis
+ *
+ * LibTomCrypt is a library that provides various cryptographic
+ * algorithms in a highly modular and flexible manner.
+ *
+ * The library is free for all purposes without any express
+ * guarantee it works.
+ */
+
+/* ---- LTC_BASE64 Routines ---- */
+#ifdef LTC_BASE64
+int base64_encode(const unsigned char *in, unsigned long len,
+ unsigned char *out, unsigned long *outlen);
+
+int base64_decode(const unsigned char *in, unsigned long len,
+ unsigned char *out, unsigned long *outlen);
+int base64_strict_decode(const unsigned char *in, unsigned long len,
+ unsigned char *out, unsigned long *outlen);
+#endif
+
+#ifdef LTC_BASE64_URL
+int base64url_encode(const unsigned char *in, unsigned long len,
+ unsigned char *out, unsigned long *outlen);
+int base64url_strict_encode(const unsigned char *in, unsigned long inlen,
+ unsigned char *out, unsigned long *outlen);
+
+int base64url_decode(const unsigned char *in, unsigned long len,
+ unsigned char *out, unsigned long *outlen);
+int base64url_strict_decode(const unsigned char *in, unsigned long len,
+ unsigned char *out, unsigned long *outlen);
+#endif
+
+/* ===> LTC_HKDF -- RFC5869 HMAC-based Key Derivation Function <=== */
+#ifdef LTC_HKDF
+
+int hkdf_test(void);
+
+int hkdf_extract(int hash_idx,
+ const unsigned char *salt, unsigned long saltlen,
+ const unsigned char *in, unsigned long inlen,
+ unsigned char *out, unsigned long *outlen);
+
+int hkdf_expand(int hash_idx,
+ const unsigned char *info, unsigned long infolen,
+ const unsigned char *in, unsigned long inlen,
+ unsigned char *out, unsigned long outlen);
+
+int hkdf(int hash_idx,
+ const unsigned char *salt, unsigned long saltlen,
+ const unsigned char *info, unsigned long infolen,
+ const unsigned char *in, unsigned long inlen,
+ unsigned char *out, unsigned long outlen);
+
+#endif /* LTC_HKDF */
+
+/* ---- MEM routines ---- */
+int mem_neq(const void *a, const void *b, size_t len);
+void zeromem(volatile void *dst, size_t len);
+void burn_stack(unsigned long len);
+
+const char *error_to_string(int err);
+
+extern const char *crypt_build_settings;
+
+/* ---- HMM ---- */
+int crypt_fsa(void *mp, ...);
+
+/* ---- Dynamic language support ---- */
+int crypt_get_constant(const char* namein, int *valueout);
+int crypt_list_all_constants(char *names_list, unsigned int *names_list_size);
+
+int crypt_get_size(const char* namein, unsigned int *sizeout);
+int crypt_list_all_sizes(char *names_list, unsigned int *names_list_size);
+
+#ifdef LTM_DESC
+void init_LTM(void);
+#endif
+#ifdef TFM_DESC
+void init_TFM(void);
+#endif
+/* *** use of GMP is untested ***
+#ifdef GMP_DESC
+void init_GMP(void);
+#endif
+*/
+
+#ifdef LTC_ADLER32
+typedef struct adler32_state_s
+{
+ unsigned short s[2];
+} adler32_state;
+
+void adler32_init(adler32_state *ctx);
+void adler32_update(adler32_state *ctx, const unsigned char *input, unsigned long length);
+void adler32_finish(adler32_state *ctx, void *hash, unsigned long size);
+int adler32_test(void);
+#endif
+
+#ifdef LTC_CRC32
+typedef struct crc32_state_s
+{
+ ulong32 crc;
+} crc32_state;
+
+void crc32_init(crc32_state *ctx);
+void crc32_update(crc32_state *ctx, const unsigned char *input, unsigned long length);
+void crc32_finish(crc32_state *ctx, void *hash, unsigned long size);
+int crc32_test(void);
+#endif
+
+int compare_testvector(const void* is, const unsigned long is_len, const void* should, const unsigned long should_len, const char* what, int which);
+
+/* ref: HEAD -> release/1.18.0, tag: v1.18.0-rc2 */
+/* git commit: aa0f396c0c8828ce39456129507fc72ef0208bd0 */
+/* commit time: 2017-07-13 14:58:01 +0200 */
diff --git a/src/third_party/tomcrypt-1.18.0-rc2/src/headers/tomcrypt_pk.h b/src/third_party/tomcrypt-1.18.0-rc2/src/headers/tomcrypt_pk.h
new file mode 100644
index 00000000000..a9139df4715
--- /dev/null
+++ b/src/third_party/tomcrypt-1.18.0-rc2/src/headers/tomcrypt_pk.h
@@ -0,0 +1,745 @@
+/* LibTomCrypt, modular cryptographic library -- Tom St Denis
+ *
+ * LibTomCrypt is a library that provides various cryptographic
+ * algorithms in a highly modular and flexible manner.
+ *
+ * The library is free for all purposes without any express
+ * guarantee it works.
+ */
+
+/* ---- NUMBER THEORY ---- */
+
+enum {
+ PK_PUBLIC=0,
+ PK_PRIVATE=1
+};
+
+/* Indicates standard output formats that can be read e.g. by OpenSSL or GnuTLS */
+#define PK_STD 0x1000
+
+/* iterations limit for retry-loops */
+#define PK_MAX_RETRIES 20
+
+int rand_prime(void *N, long len, prng_state *prng, int wprng);
+
+#ifdef LTC_SOURCE
+/* internal helper functions */
+int rand_bn_bits(void *N, int bits, prng_state *prng, int wprng);
+int rand_bn_upto(void *N, void *limit, prng_state *prng, int wprng);
+
+enum public_key_algorithms {
+ PKA_RSA,
+ PKA_DSA
+};
+
+typedef struct Oid {
+ unsigned long OID[16];
+ /** Length of DER encoding */
+ unsigned long OIDlen;
+} oid_st;
+
+int pk_get_oid(int pk, oid_st *st);
+#endif /* LTC_SOURCE */
+
+/* ---- RSA ---- */
+#ifdef LTC_MRSA
+
+/** RSA PKCS style key */
+typedef struct Rsa_key {
+ /** Type of key, PK_PRIVATE or PK_PUBLIC */
+ int type;
+ /** The public exponent */
+ void *e;
+ /** The private exponent */
+ void *d;
+ /** The modulus */
+ void *N;
+ /** The p factor of N */
+ void *p;
+ /** The q factor of N */
+ void *q;
+ /** The 1/q mod p CRT param */
+ void *qP;
+ /** The d mod (p - 1) CRT param */
+ void *dP;
+ /** The d mod (q - 1) CRT param */
+ void *dQ;
+} rsa_key;
+
+int rsa_make_key(prng_state *prng, int wprng, int size, long e, rsa_key *key);
+
+int rsa_get_size(rsa_key *key);
+
+int rsa_exptmod(const unsigned char *in, unsigned long inlen,
+ unsigned char *out, unsigned long *outlen, int which,
+ rsa_key *key);
+
+void rsa_free(rsa_key *key);
+
+/* These use PKCS #1 v2.0 padding */
+#define rsa_encrypt_key(_in, _inlen, _out, _outlen, _lparam, _lparamlen, _prng, _prng_idx, _hash_idx, _key) \
+ rsa_encrypt_key_ex(_in, _inlen, _out, _outlen, _lparam, _lparamlen, _prng, _prng_idx, _hash_idx, LTC_PKCS_1_OAEP, _key)
+
+#define rsa_decrypt_key(_in, _inlen, _out, _outlen, _lparam, _lparamlen, _hash_idx, _stat, _key) \
+ rsa_decrypt_key_ex(_in, _inlen, _out, _outlen, _lparam, _lparamlen, _hash_idx, LTC_PKCS_1_OAEP, _stat, _key)
+
+#define rsa_sign_hash(_in, _inlen, _out, _outlen, _prng, _prng_idx, _hash_idx, _saltlen, _key) \
+ rsa_sign_hash_ex(_in, _inlen, _out, _outlen, LTC_PKCS_1_PSS, _prng, _prng_idx, _hash_idx, _saltlen, _key)
+
+#define rsa_verify_hash(_sig, _siglen, _hash, _hashlen, _hash_idx, _saltlen, _stat, _key) \
+ rsa_verify_hash_ex(_sig, _siglen, _hash, _hashlen, LTC_PKCS_1_PSS, _hash_idx, _saltlen, _stat, _key)
+
+#define rsa_sign_saltlen_get_max(_hash_idx, _key) \
+ rsa_sign_saltlen_get_max_ex(LTC_PKCS_1_PSS, _hash_idx, _key)
+
+/* These can be switched between PKCS #1 v2.x and PKCS #1 v1.5 paddings */
+int rsa_encrypt_key_ex(const unsigned char *in, unsigned long inlen,
+ unsigned char *out, unsigned long *outlen,
+ const unsigned char *lparam, unsigned long lparamlen,
+ prng_state *prng, int prng_idx, int hash_idx, int padding, rsa_key *key);
+
+int rsa_decrypt_key_ex(const unsigned char *in, unsigned long inlen,
+ unsigned char *out, unsigned long *outlen,
+ const unsigned char *lparam, unsigned long lparamlen,
+ int hash_idx, int padding,
+ int *stat, rsa_key *key);
+
+int rsa_sign_hash_ex(const unsigned char *in, unsigned long inlen,
+ unsigned char *out, unsigned long *outlen,
+ int padding,
+ prng_state *prng, int prng_idx,
+ int hash_idx, unsigned long saltlen,
+ rsa_key *key);
+
+int rsa_verify_hash_ex(const unsigned char *sig, unsigned long siglen,
+ const unsigned char *hash, unsigned long hashlen,
+ int padding,
+ int hash_idx, unsigned long saltlen,
+ int *stat, rsa_key *key);
+
+int rsa_sign_saltlen_get_max_ex(int padding, int hash_idx, rsa_key *key);
+
+/* PKCS #1 import/export */
+int rsa_export(unsigned char *out, unsigned long *outlen, int type, rsa_key *key);
+int rsa_import(const unsigned char *in, unsigned long inlen, rsa_key *key);
+
+int rsa_import_x509(const unsigned char *in, unsigned long inlen, rsa_key *key);
+int rsa_import_pkcs8(const unsigned char *in, unsigned long inlen,
+ const void *passwd, unsigned long passwdlen, rsa_key *key);
+
+int rsa_set_key(const unsigned char *N, unsigned long Nlen,
+ const unsigned char *e, unsigned long elen,
+ const unsigned char *d, unsigned long dlen,
+ rsa_key *key);
+int rsa_set_factors(const unsigned char *p, unsigned long plen,
+ const unsigned char *q, unsigned long qlen,
+ rsa_key *key);
+int rsa_set_crt_params(const unsigned char *dP, unsigned long dPlen,
+ const unsigned char *dQ, unsigned long dQlen,
+ const unsigned char *qP, unsigned long qPlen,
+ rsa_key *key);
+#endif
+
+/* ---- Katja ---- */
+#ifdef LTC_MKAT
+
+/* Min and Max KAT key sizes (in bits) */
+#define MIN_KAT_SIZE 1024
+#define MAX_KAT_SIZE 4096
+
+/** Katja PKCS style key */
+typedef struct KAT_key {
+ /** Type of key, PK_PRIVATE or PK_PUBLIC */
+ int type;
+ /** The private exponent */
+ void *d;
+ /** The modulus */
+ void *N;
+ /** The p factor of N */
+ void *p;
+ /** The q factor of N */
+ void *q;
+ /** The 1/q mod p CRT param */
+ void *qP;
+ /** The d mod (p - 1) CRT param */
+ void *dP;
+ /** The d mod (q - 1) CRT param */
+ void *dQ;
+ /** The pq param */
+ void *pq;
+} katja_key;
+
+int katja_make_key(prng_state *prng, int wprng, int size, katja_key *key);
+
+int katja_exptmod(const unsigned char *in, unsigned long inlen,
+ unsigned char *out, unsigned long *outlen, int which,
+ katja_key *key);
+
+void katja_free(katja_key *key);
+
+/* These use PKCS #1 v2.0 padding */
+int katja_encrypt_key(const unsigned char *in, unsigned long inlen,
+ unsigned char *out, unsigned long *outlen,
+ const unsigned char *lparam, unsigned long lparamlen,
+ prng_state *prng, int prng_idx, int hash_idx, katja_key *key);
+
+int katja_decrypt_key(const unsigned char *in, unsigned long inlen,
+ unsigned char *out, unsigned long *outlen,
+ const unsigned char *lparam, unsigned long lparamlen,
+ int hash_idx, int *stat,
+ katja_key *key);
+
+/* PKCS #1 import/export */
+int katja_export(unsigned char *out, unsigned long *outlen, int type, katja_key *key);
+int katja_import(const unsigned char *in, unsigned long inlen, katja_key *key);
+
+#endif
+
+/* ---- DH Routines ---- */
+#ifdef LTC_MDH
+
+typedef struct {
+ int size;
+ char *name, *base, *prime;
+} ltc_dh_set_type;
+
+extern const ltc_dh_set_type ltc_dh_sets[];
+
+typedef struct {
+ int type;
+ void *x;
+ void *y;
+ void *base;
+ void *prime;
+} dh_key;
+
+int dh_get_groupsize(dh_key *key);
+
+int dh_export(unsigned char *out, unsigned long *outlen, int type, dh_key *key);
+int dh_import(const unsigned char *in, unsigned long inlen, dh_key *key);
+
+int dh_set_pg(const unsigned char *p, unsigned long plen,
+ const unsigned char *g, unsigned long glen,
+ dh_key *key);
+int dh_set_pg_dhparam(const unsigned char *dhparam, unsigned long dhparamlen, dh_key *key);
+int dh_set_pg_groupsize(int groupsize, dh_key *key);
+
+int dh_set_key(const unsigned char *in, unsigned long inlen, int type, dh_key *key);
+int dh_generate_key(prng_state *prng, int wprng, dh_key *key);
+
+int dh_shared_secret(dh_key *private_key, dh_key *public_key,
+ unsigned char *out, unsigned long *outlen);
+
+void dh_free(dh_key *key);
+
+int dh_export_key(void *out, unsigned long *outlen, int type, dh_key *key);
+
+#ifdef LTC_SOURCE
+/* internal helper functions */
+int dh_check_pubkey(dh_key *key);
+#endif
+
+#endif /* LTC_MDH */
+
+
+/* ---- ECC Routines ---- */
+#ifdef LTC_MECC
+
+/* size of our temp buffers for exported keys */
+#define ECC_BUF_SIZE 256
+
+/* max private key size */
+#define ECC_MAXSIZE 66
+
+/** Structure defines a NIST GF(p) curve */
+typedef struct {
+ /** The size of the curve in octets */
+ int size;
+
+ /** name of curve */
+ char *name;
+
+ /** The prime that defines the field the curve is in (encoded in hex) */
+ char *prime;
+
+ /** The fields B param (hex) */
+ char *B;
+
+ /** The order of the curve (hex) */
+ char *order;
+
+ /** The x co-ordinate of the base point on the curve (hex) */
+ char *Gx;
+
+ /** The y co-ordinate of the base point on the curve (hex) */
+ char *Gy;
+} ltc_ecc_set_type;
+
+/** A point on a ECC curve, stored in Jacbobian format such that (x,y,z) => (x/z^2, y/z^3, 1) when interpretted as affine */
+typedef struct {
+ /** The x co-ordinate */
+ void *x;
+
+ /** The y co-ordinate */
+ void *y;
+
+ /** The z co-ordinate */
+ void *z;
+} ecc_point;
+
+/** An ECC key */
+typedef struct {
+ /** Type of key, PK_PRIVATE or PK_PUBLIC */
+ int type;
+
+ /** Index into the ltc_ecc_sets[] for the parameters of this curve; if -1, then this key is using user supplied curve in dp */
+ int idx;
+
+ /** pointer to domain parameters; either points to NIST curves (identified by idx >= 0) or user supplied curve */
+ const ltc_ecc_set_type *dp;
+
+ /** The public key */
+ ecc_point pubkey;
+
+ /** The private key */
+ void *k;
+} ecc_key;
+
+/** the ECC params provided */
+extern const ltc_ecc_set_type ltc_ecc_sets[];
+
+int ecc_test(void);
+void ecc_sizes(int *low, int *high);
+int ecc_get_size(ecc_key *key);
+
+int ecc_make_key(prng_state *prng, int wprng, int keysize, ecc_key *key);
+int ecc_make_key_ex(prng_state *prng, int wprng, ecc_key *key, const ltc_ecc_set_type *dp);
+void ecc_free(ecc_key *key);
+
+int ecc_export(unsigned char *out, unsigned long *outlen, int type, ecc_key *key);
+int ecc_import(const unsigned char *in, unsigned long inlen, ecc_key *key);
+int ecc_import_ex(const unsigned char *in, unsigned long inlen, ecc_key *key, const ltc_ecc_set_type *dp);
+
+int ecc_ansi_x963_export(ecc_key *key, unsigned char *out, unsigned long *outlen);
+int ecc_ansi_x963_import(const unsigned char *in, unsigned long inlen, ecc_key *key);
+int ecc_ansi_x963_import_ex(const unsigned char *in, unsigned long inlen, ecc_key *key, ltc_ecc_set_type *dp);
+
+int ecc_shared_secret(ecc_key *private_key, ecc_key *public_key,
+ unsigned char *out, unsigned long *outlen);
+
+int ecc_encrypt_key(const unsigned char *in, unsigned long inlen,
+ unsigned char *out, unsigned long *outlen,
+ prng_state *prng, int wprng, int hash,
+ ecc_key *key);
+
+int ecc_decrypt_key(const unsigned char *in, unsigned long inlen,
+ unsigned char *out, unsigned long *outlen,
+ ecc_key *key);
+
+int ecc_sign_hash_rfc7518(const unsigned char *in, unsigned long inlen,
+ unsigned char *out, unsigned long *outlen,
+ prng_state *prng, int wprng, ecc_key *key);
+
+int ecc_sign_hash(const unsigned char *in, unsigned long inlen,
+ unsigned char *out, unsigned long *outlen,
+ prng_state *prng, int wprng, ecc_key *key);
+
+int ecc_verify_hash_rfc7518(const unsigned char *sig, unsigned long siglen,
+ const unsigned char *hash, unsigned long hashlen,
+ int *stat, ecc_key *key);
+
+int ecc_verify_hash(const unsigned char *sig, unsigned long siglen,
+ const unsigned char *hash, unsigned long hashlen,
+ int *stat, ecc_key *key);
+
+/* low level functions */
+ecc_point *ltc_ecc_new_point(void);
+void ltc_ecc_del_point(ecc_point *p);
+int ltc_ecc_is_valid_idx(int n);
+
+/* point ops (mp == montgomery digit) */
+#if !defined(LTC_MECC_ACCEL) || defined(LTM_DESC) || defined(GMP_DESC)
+/* R = 2P */
+int ltc_ecc_projective_dbl_point(ecc_point *P, ecc_point *R, void *modulus, void *mp);
+
+/* R = P + Q */
+int ltc_ecc_projective_add_point(ecc_point *P, ecc_point *Q, ecc_point *R, void *modulus, void *mp);
+#endif
+
+#if defined(LTC_MECC_FP)
+/* optimized point multiplication using fixed point cache (HAC algorithm 14.117) */
+int ltc_ecc_fp_mulmod(void *k, ecc_point *G, ecc_point *R, void *modulus, int map);
+
+/* functions for saving/loading/freeing/adding to fixed point cache */
+int ltc_ecc_fp_save_state(unsigned char **out, unsigned long *outlen);
+int ltc_ecc_fp_restore_state(unsigned char *in, unsigned long inlen);
+void ltc_ecc_fp_free(void);
+int ltc_ecc_fp_add_point(ecc_point *g, void *modulus, int lock);
+
+/* lock/unlock all points currently in fixed point cache */
+void ltc_ecc_fp_tablelock(int lock);
+#endif
+
+/* R = kG */
+int ltc_ecc_mulmod(void *k, ecc_point *G, ecc_point *R, void *modulus, int map);
+
+#ifdef LTC_ECC_SHAMIR
+/* kA*A + kB*B = C */
+int ltc_ecc_mul2add(ecc_point *A, void *kA,
+ ecc_point *B, void *kB,
+ ecc_point *C,
+ void *modulus);
+
+#ifdef LTC_MECC_FP
+/* Shamir's trick with optimized point multiplication using fixed point cache */
+int ltc_ecc_fp_mul2add(ecc_point *A, void *kA,
+ ecc_point *B, void *kB,
+ ecc_point *C, void *modulus);
+#endif
+
+#endif
+
+
+/* map P to affine from projective */
+int ltc_ecc_map(ecc_point *P, void *modulus, void *mp);
+
+#endif
+
+#ifdef LTC_MDSA
+
+/* Max diff between group and modulus size in bytes */
+#define LTC_MDSA_DELTA 512
+
+/* Max DSA group size in bytes (default allows 4k-bit groups) */
+#define LTC_MDSA_MAX_GROUP 512
+
+/** DSA key structure */
+typedef struct {
+ /** The key type, PK_PRIVATE or PK_PUBLIC */
+ int type;
+
+ /** The order of the sub-group used in octets */
+ int qord;
+
+ /** The generator */
+ void *g;
+
+ /** The prime used to generate the sub-group */
+ void *q;
+
+ /** The large prime that generats the field the contains the sub-group */
+ void *p;
+
+ /** The private key */
+ void *x;
+
+ /** The public key */
+ void *y;
+} dsa_key;
+
+int dsa_make_key(prng_state *prng, int wprng, int group_size, int modulus_size, dsa_key *key);
+
+int dsa_set_pqg(const unsigned char *p, unsigned long plen,
+ const unsigned char *q, unsigned long qlen,
+ const unsigned char *g, unsigned long glen,
+ dsa_key *key);
+int dsa_set_pqg_dsaparam(const unsigned char *dsaparam, unsigned long dsaparamlen, dsa_key *key);
+int dsa_generate_pqg(prng_state *prng, int wprng, int group_size, int modulus_size, dsa_key *key);
+
+int dsa_set_key(const unsigned char *in, unsigned long inlen, int type, dsa_key *key);
+int dsa_generate_key(prng_state *prng, int wprng, dsa_key *key);
+
+void dsa_free(dsa_key *key);
+
+int dsa_sign_hash_raw(const unsigned char *in, unsigned long inlen,
+ void *r, void *s,
+ prng_state *prng, int wprng, dsa_key *key);
+
+int dsa_sign_hash(const unsigned char *in, unsigned long inlen,
+ unsigned char *out, unsigned long *outlen,
+ prng_state *prng, int wprng, dsa_key *key);
+
+int dsa_verify_hash_raw( void *r, void *s,
+ const unsigned char *hash, unsigned long hashlen,
+ int *stat, dsa_key *key);
+
+int dsa_verify_hash(const unsigned char *sig, unsigned long siglen,
+ const unsigned char *hash, unsigned long hashlen,
+ int *stat, dsa_key *key);
+
+int dsa_encrypt_key(const unsigned char *in, unsigned long inlen,
+ unsigned char *out, unsigned long *outlen,
+ prng_state *prng, int wprng, int hash,
+ dsa_key *key);
+
+int dsa_decrypt_key(const unsigned char *in, unsigned long inlen,
+ unsigned char *out, unsigned long *outlen,
+ dsa_key *key);
+
+int dsa_import(const unsigned char *in, unsigned long inlen, dsa_key *key);
+int dsa_export(unsigned char *out, unsigned long *outlen, int type, dsa_key *key);
+int dsa_verify_key(dsa_key *key, int *stat);
+
+int dsa_shared_secret(void *private_key, void *base,
+ dsa_key *public_key,
+ unsigned char *out, unsigned long *outlen);
+#endif
+
+#ifdef LTC_DER
+/* DER handling */
+
+typedef enum ltc_asn1_type_ {
+ /* 0 */
+ LTC_ASN1_EOL,
+ LTC_ASN1_BOOLEAN,
+ LTC_ASN1_INTEGER,
+ LTC_ASN1_SHORT_INTEGER,
+ LTC_ASN1_BIT_STRING,
+ /* 5 */
+ LTC_ASN1_OCTET_STRING,
+ LTC_ASN1_NULL,
+ LTC_ASN1_OBJECT_IDENTIFIER,
+ LTC_ASN1_IA5_STRING,
+ LTC_ASN1_PRINTABLE_STRING,
+ /* 10 */
+ LTC_ASN1_UTF8_STRING,
+ LTC_ASN1_UTCTIME,
+ LTC_ASN1_CHOICE,
+ LTC_ASN1_SEQUENCE,
+ LTC_ASN1_SET,
+ /* 15 */
+ LTC_ASN1_SETOF,
+ LTC_ASN1_RAW_BIT_STRING,
+ LTC_ASN1_TELETEX_STRING,
+ LTC_ASN1_CONSTRUCTED,
+ LTC_ASN1_CONTEXT_SPECIFIC,
+ /* 20 */
+ LTC_ASN1_GENERALIZEDTIME,
+} ltc_asn1_type;
+
+/** A LTC ASN.1 list type */
+typedef struct ltc_asn1_list_ {
+ /** The LTC ASN.1 enumerated type identifier */
+ ltc_asn1_type type;
+ /** The data to encode or place for decoding */
+ void *data;
+ /** The size of the input or resulting output */
+ unsigned long size;
+ /** The used flag, this is used by the CHOICE ASN.1 type to indicate which choice was made */
+ int used;
+ /** prev/next entry in the list */
+ struct ltc_asn1_list_ *prev, *next, *child, *parent;
+} ltc_asn1_list;
+
+#define LTC_SET_ASN1(list, index, Type, Data, Size) \
+ do { \
+ int LTC_MACRO_temp = (index); \
+ ltc_asn1_list *LTC_MACRO_list = (list); \
+ LTC_MACRO_list[LTC_MACRO_temp].type = (Type); \
+ LTC_MACRO_list[LTC_MACRO_temp].data = (void*)(Data); \
+ LTC_MACRO_list[LTC_MACRO_temp].size = (Size); \
+ LTC_MACRO_list[LTC_MACRO_temp].used = 0; \
+ } while (0)
+
+/* SEQUENCE */
+int der_encode_sequence_ex(ltc_asn1_list *list, unsigned long inlen,
+ unsigned char *out, unsigned long *outlen, int type_of);
+
+#define der_encode_sequence(list, inlen, out, outlen) der_encode_sequence_ex(list, inlen, out, outlen, LTC_ASN1_SEQUENCE)
+
+int der_decode_sequence_ex(const unsigned char *in, unsigned long inlen,
+ ltc_asn1_list *list, unsigned long outlen, int ordered);
+
+#define der_decode_sequence(in, inlen, list, outlen) der_decode_sequence_ex(in, inlen, list, outlen, 1)
+
+int der_length_sequence(ltc_asn1_list *list, unsigned long inlen,
+ unsigned long *outlen);
+
+
+#ifdef LTC_SOURCE
+/* internal helper functions */
+int der_length_sequence_ex(ltc_asn1_list *list, unsigned long inlen,
+ unsigned long *outlen, unsigned long *payloadlen);
+/* SUBJECT PUBLIC KEY INFO */
+int der_encode_subject_public_key_info(unsigned char *out, unsigned long *outlen,
+ unsigned int algorithm, void* public_key, unsigned long public_key_len,
+ unsigned long parameters_type, void* parameters, unsigned long parameters_len);
+
+int der_decode_subject_public_key_info(const unsigned char *in, unsigned long inlen,
+ unsigned int algorithm, void* public_key, unsigned long* public_key_len,
+ unsigned long parameters_type, ltc_asn1_list* parameters, unsigned long parameters_len);
+#endif /* LTC_SOURCE */
+
+/* SET */
+#define der_decode_set(in, inlen, list, outlen) der_decode_sequence_ex(in, inlen, list, outlen, 0)
+#define der_length_set der_length_sequence
+int der_encode_set(ltc_asn1_list *list, unsigned long inlen,
+ unsigned char *out, unsigned long *outlen);
+
+int der_encode_setof(ltc_asn1_list *list, unsigned long inlen,
+ unsigned char *out, unsigned long *outlen);
+
+/* VA list handy helpers with triplets of <type, size, data> */
+int der_encode_sequence_multi(unsigned char *out, unsigned long *outlen, ...);
+int der_decode_sequence_multi(const unsigned char *in, unsigned long inlen, ...);
+
+/* FLEXI DECODER handle unknown list decoder */
+int der_decode_sequence_flexi(const unsigned char *in, unsigned long *inlen, ltc_asn1_list **out);
+#define der_free_sequence_flexi der_sequence_free
+void der_sequence_free(ltc_asn1_list *in);
+void der_sequence_shrink(ltc_asn1_list *in);
+
+/* BOOLEAN */
+int der_length_boolean(unsigned long *outlen);
+int der_encode_boolean(int in,
+ unsigned char *out, unsigned long *outlen);
+int der_decode_boolean(const unsigned char *in, unsigned long inlen,
+ int *out);
+/* INTEGER */
+int der_encode_integer(void *num, unsigned char *out, unsigned long *outlen);
+int der_decode_integer(const unsigned char *in, unsigned long inlen, void *num);
+int der_length_integer(void *num, unsigned long *len);
+
+/* INTEGER -- handy for 0..2^32-1 values */
+int der_decode_short_integer(const unsigned char *in, unsigned long inlen, unsigned long *num);
+int der_encode_short_integer(unsigned long num, unsigned char *out, unsigned long *outlen);
+int der_length_short_integer(unsigned long num, unsigned long *outlen);
+
+/* BIT STRING */
+int der_encode_bit_string(const unsigned char *in, unsigned long inlen,
+ unsigned char *out, unsigned long *outlen);
+int der_decode_bit_string(const unsigned char *in, unsigned long inlen,
+ unsigned char *out, unsigned long *outlen);
+int der_encode_raw_bit_string(const unsigned char *in, unsigned long inlen,
+ unsigned char *out, unsigned long *outlen);
+int der_decode_raw_bit_string(const unsigned char *in, unsigned long inlen,
+ unsigned char *out, unsigned long *outlen);
+int der_length_bit_string(unsigned long nbits, unsigned long *outlen);
+
+/* OCTET STRING */
+int der_encode_octet_string(const unsigned char *in, unsigned long inlen,
+ unsigned char *out, unsigned long *outlen);
+int der_decode_octet_string(const unsigned char *in, unsigned long inlen,
+ unsigned char *out, unsigned long *outlen);
+int der_length_octet_string(unsigned long noctets, unsigned long *outlen);
+
+/* OBJECT IDENTIFIER */
+int der_encode_object_identifier(unsigned long *words, unsigned long nwords,
+ unsigned char *out, unsigned long *outlen);
+int der_decode_object_identifier(const unsigned char *in, unsigned long inlen,
+ unsigned long *words, unsigned long *outlen);
+int der_length_object_identifier(unsigned long *words, unsigned long nwords, unsigned long *outlen);
+unsigned long der_object_identifier_bits(unsigned long x);
+
+/* IA5 STRING */
+int der_encode_ia5_string(const unsigned char *in, unsigned long inlen,
+ unsigned char *out, unsigned long *outlen);
+int der_decode_ia5_string(const unsigned char *in, unsigned long inlen,
+ unsigned char *out, unsigned long *outlen);
+int der_length_ia5_string(const unsigned char *octets, unsigned long noctets, unsigned long *outlen);
+
+int der_ia5_char_encode(int c);
+int der_ia5_value_decode(int v);
+
+/* TELETEX STRING */
+int der_decode_teletex_string(const unsigned char *in, unsigned long inlen,
+ unsigned char *out, unsigned long *outlen);
+int der_length_teletex_string(const unsigned char *octets, unsigned long noctets, unsigned long *outlen);
+
+#ifdef LTC_SOURCE
+/* internal helper functions */
+int der_teletex_char_encode(int c);
+int der_teletex_value_decode(int v);
+#endif /* LTC_SOURCE */
+
+
+/* PRINTABLE STRING */
+int der_encode_printable_string(const unsigned char *in, unsigned long inlen,
+ unsigned char *out, unsigned long *outlen);
+int der_decode_printable_string(const unsigned char *in, unsigned long inlen,
+ unsigned char *out, unsigned long *outlen);
+int der_length_printable_string(const unsigned char *octets, unsigned long noctets, unsigned long *outlen);
+
+int der_printable_char_encode(int c);
+int der_printable_value_decode(int v);
+
+/* UTF-8 */
+#if (defined(SIZE_MAX) || __STDC_VERSION__ >= 199901L || defined(WCHAR_MAX) || defined(__WCHAR_MAX__) || defined(_WCHAR_T) || defined(_WCHAR_T_DEFINED) || defined (__WCHAR_TYPE__)) && !defined(LTC_NO_WCHAR)
+#include <wchar.h>
+#if defined(__WCHAR_MAX__)
+#define LTC_WCHAR_MAX __WCHAR_MAX__
+#elif defined(WCHAR_MAX)
+#define LTC_WCHAR_MAX WCHAR_MAX
+#endif
+/* please note that it might happen that LTC_WCHAR_MAX is undefined */
+#else
+typedef ulong32 wchar_t;
+#define LTC_WCHAR_MAX 0xFFFFFFFF
+#endif
+
+int der_encode_utf8_string(const wchar_t *in, unsigned long inlen,
+ unsigned char *out, unsigned long *outlen);
+
+int der_decode_utf8_string(const unsigned char *in, unsigned long inlen,
+ wchar_t *out, unsigned long *outlen);
+unsigned long der_utf8_charsize(const wchar_t c);
+#ifdef LTC_SOURCE
+/* internal helper functions */
+int der_utf8_valid_char(const wchar_t c);
+#endif /* LTC_SOURCE */
+int der_length_utf8_string(const wchar_t *in, unsigned long noctets, unsigned long *outlen);
+
+
+/* CHOICE */
+int der_decode_choice(const unsigned char *in, unsigned long *inlen,
+ ltc_asn1_list *list, unsigned long outlen);
+
+/* UTCTime */
+typedef struct {
+ unsigned YY, /* year */
+ MM, /* month */
+ DD, /* day */
+ hh, /* hour */
+ mm, /* minute */
+ ss, /* second */
+ off_dir, /* timezone offset direction 0 == +, 1 == - */
+ off_hh, /* timezone offset hours */
+ off_mm; /* timezone offset minutes */
+} ltc_utctime;
+
+int der_encode_utctime(ltc_utctime *utctime,
+ unsigned char *out, unsigned long *outlen);
+
+int der_decode_utctime(const unsigned char *in, unsigned long *inlen,
+ ltc_utctime *out);
+
+int der_length_utctime(ltc_utctime *utctime, unsigned long *outlen);
+
+/* GeneralizedTime */
+typedef struct {
+ unsigned YYYY, /* year */
+ MM, /* month */
+ DD, /* day */
+ hh, /* hour */
+ mm, /* minute */
+ ss, /* second */
+ fs, /* fractional seconds */
+ off_dir, /* timezone offset direction 0 == +, 1 == - */
+ off_hh, /* timezone offset hours */
+ off_mm; /* timezone offset minutes */
+} ltc_generalizedtime;
+
+int der_encode_generalizedtime(ltc_generalizedtime *gtime,
+ unsigned char *out, unsigned long *outlen);
+
+int der_decode_generalizedtime(const unsigned char *in, unsigned long *inlen,
+ ltc_generalizedtime *out);
+
+int der_length_generalizedtime(ltc_generalizedtime *gtime, unsigned long *outlen);
+
+
+#endif
+
+/* ref: HEAD -> release/1.18.0, tag: v1.18.0-rc2 */
+/* git commit: aa0f396c0c8828ce39456129507fc72ef0208bd0 */
+/* commit time: 2017-07-13 14:58:01 +0200 */
diff --git a/src/third_party/tomcrypt-1.18.0-rc2/src/headers/tomcrypt_pkcs.h b/src/third_party/tomcrypt-1.18.0-rc2/src/headers/tomcrypt_pkcs.h
new file mode 100644
index 00000000000..f24cb03e7de
--- /dev/null
+++ b/src/third_party/tomcrypt-1.18.0-rc2/src/headers/tomcrypt_pkcs.h
@@ -0,0 +1,108 @@
+/* LibTomCrypt, modular cryptographic library -- Tom St Denis
+ *
+ * LibTomCrypt is a library that provides various cryptographic
+ * algorithms in a highly modular and flexible manner.
+ *
+ * The library is free for all purposes without any express
+ * guarantee it works.
+ */
+
+/* PKCS Header Info */
+
+/* ===> PKCS #1 -- RSA Cryptography <=== */
+#ifdef LTC_PKCS_1
+
+enum ltc_pkcs_1_v1_5_blocks
+{
+ LTC_PKCS_1_EMSA = 1, /* Block type 1 (PKCS #1 v1.5 signature padding) */
+ LTC_PKCS_1_EME = 2 /* Block type 2 (PKCS #1 v1.5 encryption padding) */
+};
+
+enum ltc_pkcs_1_paddings
+{
+ LTC_PKCS_1_V1_5 = 1, /* PKCS #1 v1.5 padding (\sa ltc_pkcs_1_v1_5_blocks) */
+ LTC_PKCS_1_OAEP = 2, /* PKCS #1 v2.0 encryption padding */
+ LTC_PKCS_1_PSS = 3, /* PKCS #1 v2.1 signature padding */
+ LTC_PKCS_1_V1_5_NA1 = 4 /* PKCS #1 v1.5 padding - No ASN.1 (\sa ltc_pkcs_1_v1_5_blocks) */
+};
+
+int pkcs_1_mgf1( int hash_idx,
+ const unsigned char *seed, unsigned long seedlen,
+ unsigned char *mask, unsigned long masklen);
+
+int pkcs_1_i2osp(void *n, unsigned long modulus_len, unsigned char *out);
+int pkcs_1_os2ip(void *n, unsigned char *in, unsigned long inlen);
+
+/* *** v1.5 padding */
+int pkcs_1_v1_5_encode(const unsigned char *msg,
+ unsigned long msglen,
+ int block_type,
+ unsigned long modulus_bitlen,
+ prng_state *prng,
+ int prng_idx,
+ unsigned char *out,
+ unsigned long *outlen);
+
+int pkcs_1_v1_5_decode(const unsigned char *msg,
+ unsigned long msglen,
+ int block_type,
+ unsigned long modulus_bitlen,
+ unsigned char *out,
+ unsigned long *outlen,
+ int *is_valid);
+
+/* *** v2.1 padding */
+int pkcs_1_oaep_encode(const unsigned char *msg, unsigned long msglen,
+ const unsigned char *lparam, unsigned long lparamlen,
+ unsigned long modulus_bitlen, prng_state *prng,
+ int prng_idx, int hash_idx,
+ unsigned char *out, unsigned long *outlen);
+
+int pkcs_1_oaep_decode(const unsigned char *msg, unsigned long msglen,
+ const unsigned char *lparam, unsigned long lparamlen,
+ unsigned long modulus_bitlen, int hash_idx,
+ unsigned char *out, unsigned long *outlen,
+ int *res);
+
+int pkcs_1_pss_encode(const unsigned char *msghash, unsigned long msghashlen,
+ unsigned long saltlen, prng_state *prng,
+ int prng_idx, int hash_idx,
+ unsigned long modulus_bitlen,
+ unsigned char *out, unsigned long *outlen);
+
+int pkcs_1_pss_decode(const unsigned char *msghash, unsigned long msghashlen,
+ const unsigned char *sig, unsigned long siglen,
+ unsigned long saltlen, int hash_idx,
+ unsigned long modulus_bitlen, int *res);
+
+#endif /* LTC_PKCS_1 */
+
+/* ===> PKCS #5 -- Password Based Cryptography <=== */
+#ifdef LTC_PKCS_5
+
+/* Algorithm #1 (old) */
+int pkcs_5_alg1(const unsigned char *password, unsigned long password_len,
+ const unsigned char *salt,
+ int iteration_count, int hash_idx,
+ unsigned char *out, unsigned long *outlen);
+
+/* Algorithm #1 - OpenSSL-compatible variant for arbitrarily-long keys.
+ Compatible with EVP_BytesToKey() */
+int pkcs_5_alg1_openssl(const unsigned char *password,
+ unsigned long password_len,
+ const unsigned char *salt,
+ int iteration_count, int hash_idx,
+ unsigned char *out, unsigned long *outlen);
+
+/* Algorithm #2 (new) */
+int pkcs_5_alg2(const unsigned char *password, unsigned long password_len,
+ const unsigned char *salt, unsigned long salt_len,
+ int iteration_count, int hash_idx,
+ unsigned char *out, unsigned long *outlen);
+
+int pkcs_5_test (void);
+#endif /* LTC_PKCS_5 */
+
+/* ref: HEAD -> release/1.18.0, tag: v1.18.0-rc2 */
+/* git commit: aa0f396c0c8828ce39456129507fc72ef0208bd0 */
+/* commit time: 2017-07-13 14:58:01 +0200 */
diff --git a/src/third_party/tomcrypt-1.18.0-rc2/src/headers/tomcrypt_prng.h b/src/third_party/tomcrypt-1.18.0-rc2/src/headers/tomcrypt_prng.h
new file mode 100644
index 00000000000..737f42461c6
--- /dev/null
+++ b/src/third_party/tomcrypt-1.18.0-rc2/src/headers/tomcrypt_prng.h
@@ -0,0 +1,232 @@
+/* LibTomCrypt, modular cryptographic library -- Tom St Denis
+ *
+ * LibTomCrypt is a library that provides various cryptographic
+ * algorithms in a highly modular and flexible manner.
+ *
+ * The library is free for all purposes without any express
+ * guarantee it works.
+ */
+
+/* ---- PRNG Stuff ---- */
+#ifdef LTC_YARROW
+struct yarrow_prng {
+ int cipher, hash;
+ unsigned char pool[MAXBLOCKSIZE];
+ symmetric_CTR ctr;
+};
+#endif
+
+#ifdef LTC_RC4
+struct rc4_prng {
+ rc4_state s;
+};
+#endif
+
+#ifdef LTC_CHACHA20_PRNG
+struct chacha20_prng {
+ chacha_state s; /* chacha state */
+ unsigned char ent[40]; /* entropy buffer */
+ unsigned long idx; /* entropy counter */
+};
+#endif
+
+#ifdef LTC_FORTUNA
+struct fortuna_prng {
+ hash_state pool[LTC_FORTUNA_POOLS]; /* the pools */
+
+ symmetric_key skey;
+
+ unsigned char K[32], /* the current key */
+ IV[16]; /* IV for CTR mode */
+
+ unsigned long pool_idx, /* current pool we will add to */
+ pool0_len, /* length of 0'th pool */
+ wd;
+
+ ulong64 reset_cnt; /* number of times we have reset */
+};
+#endif
+
+#ifdef LTC_SOBER128
+struct sober128_prng {
+ sober128_state s; /* sober128 state */
+ unsigned char ent[40]; /* entropy buffer */
+ unsigned long idx; /* entropy counter */
+};
+#endif
+
+typedef struct {
+ union {
+ char dummy[1];
+#ifdef LTC_YARROW
+ struct yarrow_prng yarrow;
+#endif
+#ifdef LTC_RC4
+ struct rc4_prng rc4;
+#endif
+#ifdef LTC_CHACHA20_PRNG
+ struct chacha20_prng chacha;
+#endif
+#ifdef LTC_FORTUNA
+ struct fortuna_prng fortuna;
+#endif
+#ifdef LTC_SOBER128
+ struct sober128_prng sober128;
+#endif
+ };
+ short ready; /* ready flag 0-1 */
+ LTC_MUTEX_TYPE(lock) /* lock */
+} prng_state;
+
+/** PRNG descriptor */
+extern struct ltc_prng_descriptor {
+ /** Name of the PRNG */
+ char *name;
+ /** size in bytes of exported state */
+ int export_size;
+ /** Start a PRNG state
+ @param prng [out] The state to initialize
+ @return CRYPT_OK if successful
+ */
+ int (*start)(prng_state *prng);
+ /** Add entropy to the PRNG
+ @param in The entropy
+ @param inlen Length of the entropy (octets)\
+ @param prng The PRNG state
+ @return CRYPT_OK if successful
+ */
+ int (*add_entropy)(const unsigned char *in, unsigned long inlen, prng_state *prng);
+ /** Ready a PRNG state to read from
+ @param prng The PRNG state to ready
+ @return CRYPT_OK if successful
+ */
+ int (*ready)(prng_state *prng);
+ /** Read from the PRNG
+ @param out [out] Where to store the data
+ @param outlen Length of data desired (octets)
+ @param prng The PRNG state to read from
+ @return Number of octets read
+ */
+ unsigned long (*read)(unsigned char *out, unsigned long outlen, prng_state *prng);
+ /** Terminate a PRNG state
+ @param prng The PRNG state to terminate
+ @return CRYPT_OK if successful
+ */
+ int (*done)(prng_state *prng);
+ /** Export a PRNG state
+ @param out [out] The destination for the state
+ @param outlen [in/out] The max size and resulting size of the PRNG state
+ @param prng The PRNG to export
+ @return CRYPT_OK if successful
+ */
+ int (*pexport)(unsigned char *out, unsigned long *outlen, prng_state *prng);
+ /** Import a PRNG state
+ @param in The data to import
+ @param inlen The length of the data to import (octets)
+ @param prng The PRNG to initialize/import
+ @return CRYPT_OK if successful
+ */
+ int (*pimport)(const unsigned char *in, unsigned long inlen, prng_state *prng);
+ /** Self-test the PRNG
+ @return CRYPT_OK if successful, CRYPT_NOP if self-testing has been disabled
+ */
+ int (*test)(void);
+} prng_descriptor[];
+
+#ifdef LTC_YARROW
+int yarrow_start(prng_state *prng);
+int yarrow_add_entropy(const unsigned char *in, unsigned long inlen, prng_state *prng);
+int yarrow_ready(prng_state *prng);
+unsigned long yarrow_read(unsigned char *out, unsigned long outlen, prng_state *prng);
+int yarrow_done(prng_state *prng);
+int yarrow_export(unsigned char *out, unsigned long *outlen, prng_state *prng);
+int yarrow_import(const unsigned char *in, unsigned long inlen, prng_state *prng);
+int yarrow_test(void);
+extern const struct ltc_prng_descriptor yarrow_desc;
+#endif
+
+#ifdef LTC_FORTUNA
+int fortuna_start(prng_state *prng);
+int fortuna_add_entropy(const unsigned char *in, unsigned long inlen, prng_state *prng);
+int fortuna_ready(prng_state *prng);
+unsigned long fortuna_read(unsigned char *out, unsigned long outlen, prng_state *prng);
+int fortuna_done(prng_state *prng);
+int fortuna_export(unsigned char *out, unsigned long *outlen, prng_state *prng);
+int fortuna_import(const unsigned char *in, unsigned long inlen, prng_state *prng);
+int fortuna_test(void);
+extern const struct ltc_prng_descriptor fortuna_desc;
+#endif
+
+#ifdef LTC_RC4
+int rc4_start(prng_state *prng);
+int rc4_add_entropy(const unsigned char *in, unsigned long inlen, prng_state *prng);
+int rc4_ready(prng_state *prng);
+unsigned long rc4_read(unsigned char *out, unsigned long outlen, prng_state *prng);
+int rc4_done(prng_state *prng);
+int rc4_export(unsigned char *out, unsigned long *outlen, prng_state *prng);
+int rc4_import(const unsigned char *in, unsigned long inlen, prng_state *prng);
+int rc4_test(void);
+extern const struct ltc_prng_descriptor rc4_desc;
+#endif
+
+#ifdef LTC_CHACHA20_PRNG
+int chacha20_prng_start(prng_state *prng);
+int chacha20_prng_add_entropy(const unsigned char *in, unsigned long inlen, prng_state *prng);
+int chacha20_prng_ready(prng_state *prng);
+unsigned long chacha20_prng_read(unsigned char *out, unsigned long outlen, prng_state *prng);
+int chacha20_prng_done(prng_state *prng);
+int chacha20_prng_export(unsigned char *out, unsigned long *outlen, prng_state *prng);
+int chacha20_prng_import(const unsigned char *in, unsigned long inlen, prng_state *prng);
+int chacha20_prng_test(void);
+extern const struct ltc_prng_descriptor chacha20_prng_desc;
+#endif
+
+#ifdef LTC_SPRNG
+int sprng_start(prng_state *prng);
+int sprng_add_entropy(const unsigned char *in, unsigned long inlen, prng_state *prng);
+int sprng_ready(prng_state *prng);
+unsigned long sprng_read(unsigned char *out, unsigned long outlen, prng_state *prng);
+int sprng_done(prng_state *prng);
+int sprng_export(unsigned char *out, unsigned long *outlen, prng_state *prng);
+int sprng_import(const unsigned char *in, unsigned long inlen, prng_state *prng);
+int sprng_test(void);
+extern const struct ltc_prng_descriptor sprng_desc;
+#endif
+
+#ifdef LTC_SOBER128
+int sober128_start(prng_state *prng);
+int sober128_add_entropy(const unsigned char *in, unsigned long inlen, prng_state *prng);
+int sober128_ready(prng_state *prng);
+unsigned long sober128_read(unsigned char *out, unsigned long outlen, prng_state *prng);
+int sober128_done(prng_state *prng);
+int sober128_export(unsigned char *out, unsigned long *outlen, prng_state *prng);
+int sober128_import(const unsigned char *in, unsigned long inlen, prng_state *prng);
+int sober128_test(void);
+extern const struct ltc_prng_descriptor sober128_desc;
+#endif
+
+int find_prng(const char *name);
+int register_prng(const struct ltc_prng_descriptor *prng);
+int unregister_prng(const struct ltc_prng_descriptor *prng);
+int register_all_prngs(void);
+int prng_is_valid(int idx);
+LTC_MUTEX_PROTO(ltc_prng_mutex)
+
+/* Slow RNG you **might** be able to use to seed a PRNG with. Be careful as this
+ * might not work on all platforms as planned
+ */
+unsigned long rng_get_bytes(unsigned char *out,
+ unsigned long outlen,
+ void (*callback)(void));
+
+int rng_make_prng(int bits, int wprng, prng_state *prng, void (*callback)(void));
+
+#ifdef LTC_PRNG_ENABLE_LTC_RNG
+extern unsigned long (*ltc_rng)(unsigned char *out, unsigned long outlen,
+ void (*callback)(void));
+#endif
+
+
+/* ref: HEAD -> release/1.18.0, tag: v1.18.0-rc2 */
+/* git commit: aa0f396c0c8828ce39456129507fc72ef0208bd0 */
+/* commit time: 2017-07-13 14:58:01 +0200 */
diff --git a/src/mongo/crypto/tom/hmac_done.c b/src/third_party/tomcrypt-1.18.0-rc2/src/mac/hmac/hmac_done.c
index d50a6f7a2ce..a836b89a893 100644
--- a/src/mongo/crypto/tom/hmac_done.c
+++ b/src/third_party/tomcrypt-1.18.0-rc2/src/mac/hmac/hmac_done.c
@@ -5,14 +5,12 @@
*
* The library is free for all purposes without any express
* guarantee it works.
- *
- * Tom St Denis, tomstdenis@gmail.com, http://libtom.org
*/
#include "tomcrypt.h"
/**
@file hmac_done.c
- LTC_HMAC support, terminate stream, Tom St Denis/Dobes Vandermeer
+ HMAC support, terminate stream, Tom St Denis/Dobes Vandermeer
*/
#ifdef LTC_HMAC
@@ -20,10 +18,10 @@
#define LTC_HMAC_BLOCKSIZE hash_descriptor[hash].blocksize
/**
- Terminate an LTC_HMAC session
- @param hmac The LTC_HMAC state
- @param out [out] The destination of the LTC_HMAC authentication tag
- @param outlen [in/out] The max size and resulting size of the LTC_HMAC authentication tag
+ Terminate an HMAC session
+ @param hmac The HMAC state
+ @param out [out] The destination of the HMAC authentication tag
+ @param outlen [in/out] The max size and resulting size of the HMAC authentication tag
@return CRYPT_OK if successful
*/
int hmac_done(hmac_state *hmac, unsigned char *out, unsigned long *outlen)
@@ -45,8 +43,8 @@ int hmac_done(hmac_state *hmac, unsigned char *out, unsigned long *outlen)
hashsize = hash_descriptor[hash].hashsize;
/* allocate buffers */
- buf = (unsigned char*)XMALLOC(LTC_HMAC_BLOCKSIZE);
- isha = (unsigned char*)XMALLOC(hashsize);
+ buf = XMALLOC(LTC_HMAC_BLOCKSIZE);
+ isha = XMALLOC(hashsize);
if (buf == NULL || isha == NULL) {
if (buf != NULL) {
XFREE(buf);
@@ -57,12 +55,12 @@ int hmac_done(hmac_state *hmac, unsigned char *out, unsigned long *outlen)
return CRYPT_MEM;
}
- /* Get the hash of the first LTC_HMAC vector plus the data */
+ /* Get the hash of the first HMAC vector plus the data */
if ((err = hash_descriptor[hash].done(&hmac->md, isha)) != CRYPT_OK) {
goto LBL_ERR;
}
- /* Create the second LTC_HMAC vector vector for step (3) */
+ /* Create the second HMAC vector vector for step (3) */
for(i=0; i < LTC_HMAC_BLOCKSIZE; i++) {
buf[i] = hmac->key[i] ^ 0x5C;
}
@@ -104,6 +102,6 @@ LBL_ERR:
#endif
-/* $Source: /cvs/libtom/libtomcrypt/src/mac/hmac/hmac_done.c,v $ */
-/* $Revision: 1.7 $ */
-/* $Date: 2007/05/12 14:37:41 $ */
+/* ref: HEAD -> release/1.18.0, tag: v1.18.0-rc2 */
+/* git commit: aa0f396c0c8828ce39456129507fc72ef0208bd0 */
+/* commit time: 2017-07-13 14:58:01 +0200 */
diff --git a/src/mongo/crypto/tom/hmac_init.c b/src/third_party/tomcrypt-1.18.0-rc2/src/mac/hmac/hmac_init.c
index aa211864681..699ae18e038 100644
--- a/src/mongo/crypto/tom/hmac_init.c
+++ b/src/third_party/tomcrypt-1.18.0-rc2/src/mac/hmac/hmac_init.c
@@ -5,14 +5,12 @@
*
* The library is free for all purposes without any express
* guarantee it works.
- *
- * Tom St Denis, tomstdenis@gmail.com, http://libtom.org
*/
#include "tomcrypt.h"
/**
@file hmac_init.c
- LTC_HMAC support, initialize state, Tom St Denis/Dobes Vandermeer
+ HMAC support, initialize state, Tom St Denis/Dobes Vandermeer
*/
#ifdef LTC_HMAC
@@ -20,8 +18,8 @@
#define LTC_HMAC_BLOCKSIZE hash_descriptor[hash].blocksize
/**
- Initialize an LTC_HMAC context.
- @param hmac The LTC_HMAC state
+ Initialize an HMAC context.
+ @param hmac The HMAC state
@param hash The index of the hash you want to use
@param key The secret key
@param keylen The length of the secret key (octets)
@@ -50,13 +48,13 @@ int hmac_init(hmac_state *hmac, int hash, const unsigned char *key, unsigned lon
}
/* allocate ram for buf */
- buf = (unsigned char*)XMALLOC(LTC_HMAC_BLOCKSIZE);
+ buf = XMALLOC(LTC_HMAC_BLOCKSIZE);
if (buf == NULL) {
return CRYPT_MEM;
}
/* allocate memory for key */
- hmac->key = (unsigned char*)XMALLOC(LTC_HMAC_BLOCKSIZE);
+ hmac->key = XMALLOC(LTC_HMAC_BLOCKSIZE);
if (hmac->key == NULL) {
XFREE(buf);
return CRYPT_MEM;
@@ -68,15 +66,13 @@ int hmac_init(hmac_state *hmac, int hash, const unsigned char *key, unsigned lon
if ((err = hash_memory(hash, key, keylen, hmac->key, &z)) != CRYPT_OK) {
goto LBL_ERR;
}
- if(hashsize < LTC_HMAC_BLOCKSIZE) {
- zeromem((hmac->key) + hashsize, (size_t)(LTC_HMAC_BLOCKSIZE - hashsize));
- }
keylen = hashsize;
} else {
XMEMCPY(hmac->key, key, (size_t)keylen);
- if(keylen < LTC_HMAC_BLOCKSIZE) {
- zeromem((hmac->key) + keylen, (size_t)(LTC_HMAC_BLOCKSIZE - keylen));
- }
+ }
+
+ if(keylen < LTC_HMAC_BLOCKSIZE) {
+ zeromem((hmac->key) + keylen, (size_t)(LTC_HMAC_BLOCKSIZE - keylen));
}
/* Create the initial vector for step (3) */
@@ -107,6 +103,6 @@ done:
#endif
-/* $Source: /cvs/libtom/libtomcrypt/src/mac/hmac/hmac_init.c,v $ */
-/* $Revision: 1.7 $ */
-/* $Date: 2007/05/12 14:37:41 $ */
+/* ref: HEAD -> release/1.18.0, tag: v1.18.0-rc2 */
+/* git commit: aa0f396c0c8828ce39456129507fc72ef0208bd0 */
+/* commit time: 2017-07-13 14:58:01 +0200 */
diff --git a/src/mongo/crypto/tom/hmac_memory.c b/src/third_party/tomcrypt-1.18.0-rc2/src/mac/hmac/hmac_memory.c
index 3f7545137f4..8b080e18587 100644
--- a/src/mongo/crypto/tom/hmac_memory.c
+++ b/src/third_party/tomcrypt-1.18.0-rc2/src/mac/hmac/hmac_memory.c
@@ -5,25 +5,23 @@
*
* The library is free for all purposes without any express
* guarantee it works.
- *
- * Tom St Denis, tomstdenis@gmail.com, http://libtom.org
*/
#include "tomcrypt.h"
/**
@file hmac_memory.c
- LTC_HMAC support, process a block of memory, Tom St Denis/Dobes Vandermeer
+ HMAC support, process a block of memory, Tom St Denis/Dobes Vandermeer
*/
#ifdef LTC_HMAC
/**
- LTC_HMAC a block of memory to produce the authentication tag
+ HMAC a block of memory to produce the authentication tag
@param hash The index of the hash to use
@param key The secret key
@param keylen The length of the secret key (octets)
- @param in The data to LTC_HMAC
- @param inlen The length of the data to LTC_HMAC (octets)
+ @param in The data to HMAC
+ @param inlen The length of the data to HMAC (octets)
@param out [out] Destination of the authentication tag
@param outlen [in/out] Max size and resulting size of authentication tag
@return CRYPT_OK if successful
@@ -53,7 +51,7 @@ int hmac_memory(int hash,
/* nope, so call the hmac functions */
/* allocate ram for hmac state */
- hmac = (hmac_state*)XMALLOC(sizeof(hmac_state));
+ hmac = XMALLOC(sizeof(hmac_state));
if (hmac == NULL) {
return CRYPT_MEM;
}
@@ -83,6 +81,6 @@ LBL_ERR:
#endif
-/* $Source: /cvs/libtom/libtomcrypt/src/mac/hmac/hmac_memory.c,v $ */
-/* $Revision: 1.8 $ */
-/* $Date: 2007/05/12 14:37:41 $ */
+/* ref: HEAD -> release/1.18.0, tag: v1.18.0-rc2 */
+/* git commit: aa0f396c0c8828ce39456129507fc72ef0208bd0 */
+/* commit time: 2017-07-13 14:58:01 +0200 */
diff --git a/src/mongo/crypto/tom/hmac_process.c b/src/third_party/tomcrypt-1.18.0-rc2/src/mac/hmac/hmac_process.c
index 68cd98b310a..0260fd7bf1b 100644
--- a/src/mongo/crypto/tom/hmac_process.c
+++ b/src/third_party/tomcrypt-1.18.0-rc2/src/mac/hmac/hmac_process.c
@@ -5,23 +5,21 @@
*
* The library is free for all purposes without any express
* guarantee it works.
- *
- * Tom St Denis, tomstdenis@gmail.com, http://libtom.org
*/
#include "tomcrypt.h"
/**
@file hmac_process.c
- LTC_HMAC support, process data, Tom St Denis/Dobes Vandermeer
+ HMAC support, process data, Tom St Denis/Dobes Vandermeer
*/
#ifdef LTC_HMAC
/**
- Process data through LTC_HMAC
+ Process data through HMAC
@param hmac The hmac state
- @param in The data to send through LTC_HMAC
- @param inlen The length of the data to LTC_HMAC (octets)
+ @param in The data to send through HMAC
+ @param inlen The length of the data to HMAC (octets)
@return CRYPT_OK if successful
*/
int hmac_process(hmac_state *hmac, const unsigned char *in, unsigned long inlen)
@@ -38,6 +36,6 @@ int hmac_process(hmac_state *hmac, const unsigned char *in, unsigned long inlen)
#endif
-/* $Source: /cvs/libtom/libtomcrypt/src/mac/hmac/hmac_process.c,v $ */
-/* $Revision: 1.7 $ */
-/* $Date: 2007/05/12 14:37:41 $ */
+/* ref: HEAD -> release/1.18.0, tag: v1.18.0-rc2 */
+/* git commit: aa0f396c0c8828ce39456129507fc72ef0208bd0 */
+/* commit time: 2017-07-13 14:58:01 +0200 */
diff --git a/src/third_party/tomcrypt-1.18.0-rc2/src/misc/compare_testvector.c b/src/third_party/tomcrypt-1.18.0-rc2/src/misc/compare_testvector.c
new file mode 100644
index 00000000000..112afb29ce0
--- /dev/null
+++ b/src/third_party/tomcrypt-1.18.0-rc2/src/misc/compare_testvector.c
@@ -0,0 +1,87 @@
+/* LibTomCrypt, modular cryptographic library -- Tom St Denis
+ *
+ * LibTomCrypt is a library that provides various cryptographic
+ * algorithms in a highly modular and flexible manner.
+ *
+ * The library is free for all purposes without any express
+ * guarantee it works.
+ */
+
+#include "tomcrypt.h"
+
+/**
+ @file compare_testvector.c
+ Function to compare two testvectors and print a (detailed) error-message if required, Steffen Jaeckel
+*/
+
+#if defined(LTC_TEST) && defined(LTC_TEST_DBG)
+static void _print_hex(const char* what, const void* v, const unsigned long l)
+{
+ const unsigned char* p = v;
+ unsigned long x, y = 0, z;
+ fprintf(stderr, "%s contents: \n", what);
+ for (x = 0; x < l; ) {
+ fprintf(stderr, "%02X ", p[x]);
+ if (!(++x % 16) || x == l) {
+ if((x % 16) != 0) {
+ z = 16 - (x % 16);
+ if(z >= 8)
+ fprintf(stderr, " ");
+ for (; z != 0; --z) {
+ fprintf(stderr, " ");
+ }
+ }
+ fprintf(stderr, " | ");
+ for(; y < x; y++) {
+ if((y % 8) == 0)
+ fprintf(stderr, " ");
+ if(isgraph(p[y]))
+ fprintf(stderr, "%c", p[y]);
+ else
+ fprintf(stderr, ".");
+ }
+ fprintf(stderr, "\n");
+ }
+ else if((x % 8) == 0) {
+ fprintf(stderr, " ");
+ }
+ }
+}
+#endif
+
+/**
+ Compare two test-vectors
+
+ @param is The data as it is
+ @param is_len The length of is
+ @param should The data as it should
+ @param should_len The length of should
+ @param what The type of the data
+ @param which The iteration count
+ @return 0 on equality, -1 or 1 on difference
+*/
+int compare_testvector(const void* is, const unsigned long is_len, const void* should, const unsigned long should_len, const char* what, int which)
+{
+ int res = 0;
+ if(is_len != should_len)
+ res = is_len > should_len ? -1 : 1;
+ else
+ res = XMEMCMP(is, should, is_len);
+
+#if defined(LTC_TEST) && defined(LTC_TEST_DBG)
+ if (res != 0) {
+ fprintf(stderr, "Testvector #%i of %s failed:\n", which, what);
+ _print_hex("SHOULD", should, should_len);
+ _print_hex("IS ", is, is_len);
+ }
+#else
+ LTC_UNUSED_PARAM(which);
+ LTC_UNUSED_PARAM(what);
+#endif
+
+ return res;
+}
+
+/* ref: HEAD -> release/1.18.0, tag: v1.18.0-rc2 */
+/* git commit: aa0f396c0c8828ce39456129507fc72ef0208bd0 */
+/* commit time: 2017-07-13 14:58:01 +0200 */
diff --git a/src/mongo/crypto/tom/crypt_argchk.c b/src/third_party/tomcrypt-1.18.0-rc2/src/misc/crypt/crypt_argchk.c
index b3a08d4fd1e..2a821e468c9 100644
--- a/src/mongo/crypto/tom/crypt_argchk.c
+++ b/src/third_party/tomcrypt-1.18.0-rc2/src/misc/crypt/crypt_argchk.c
@@ -5,11 +5,8 @@
*
* The library is free for all purposes without any express
* guarantee it works.
- *
- * Tom St Denis, tomstdenis@gmail.com, http://libtom.org
*/
#include "tomcrypt.h"
-#include <signal.h>
/**
@file crypt_argchk.c
@@ -17,14 +14,14 @@
*/
#if (ARGTYPE == 0)
-void crypt_argchk(const char *v, const char *s, int d)
+void crypt_argchk(char *v, char *s, int d)
{
fprintf(stderr, "LTC_ARGCHK '%s' failure on line %d of file %s\n",
v, d, s);
- (void)raise(SIGABRT);
+ abort();
}
#endif
-/* $Source: /cvs/libtom/libtomcrypt/src/misc/crypt/crypt_argchk.c,v $ */
-/* $Revision: 1.5 $ */
-/* $Date: 2006/12/28 01:27:24 $ */
+/* ref: HEAD -> release/1.18.0, tag: v1.18.0-rc2 */
+/* git commit: aa0f396c0c8828ce39456129507fc72ef0208bd0 */
+/* commit time: 2017-07-13 14:58:01 +0200 */
diff --git a/src/mongo/crypto/tom/crypt_find_hash.c b/src/third_party/tomcrypt-1.18.0-rc2/src/misc/crypt/crypt_find_hash.c
index d0056d103d6..edc22d8779c 100644
--- a/src/mongo/crypto/tom/crypt_find_hash.c
+++ b/src/third_party/tomcrypt-1.18.0-rc2/src/misc/crypt/crypt_find_hash.c
@@ -5,8 +5,6 @@
*
* The library is free for all purposes without any express
* guarantee it works.
- *
- * Tom St Denis, tomstdenis@gmail.com, http://libtom.org
*/
#include "tomcrypt.h"
@@ -18,7 +16,7 @@
/**
Find a registered hash by name
@param name The name of the hash to look for
- @return >= 0 if found, -1 if not presen
+ @return >= 0 if found, -1 if not present
*/
int find_hash(const char *name)
{
@@ -35,6 +33,6 @@ int find_hash(const char *name)
return -1;
}
-/* $Source: /cvs/libtom/libtomcrypt/src/misc/crypt/crypt_find_hash.c,v $ */
-/* $Revision: 1.7 $ */
-/* $Date: 2006/12/28 01:27:24 $ */
+/* ref: HEAD -> release/1.18.0, tag: v1.18.0-rc2 */
+/* git commit: aa0f396c0c8828ce39456129507fc72ef0208bd0 */
+/* commit time: 2017-07-13 14:58:01 +0200 */
diff --git a/src/mongo/crypto/tom/crypt_hash_descriptor.c b/src/third_party/tomcrypt-1.18.0-rc2/src/misc/crypt/crypt_hash_descriptor.c
index eef281340f1..ab69e9a6e4b 100644
--- a/src/mongo/crypto/tom/crypt_hash_descriptor.c
+++ b/src/third_party/tomcrypt-1.18.0-rc2/src/misc/crypt/crypt_hash_descriptor.c
@@ -5,8 +5,6 @@
*
* The library is free for all purposes without any express
* guarantee it works.
- *
- * Tom St Denis, tomstdenis@gmail.com, http://libtom.org
*/
#include "tomcrypt.h"
@@ -22,6 +20,6 @@ struct ltc_hash_descriptor hash_descriptor[TAB_SIZE] = {
LTC_MUTEX_GLOBAL(ltc_hash_mutex)
-/* $Source: /cvs/libtom/libtomcrypt/src/misc/crypt/crypt_hash_descriptor.c,v $ */
-/* $Revision: 1.10 $ */
-/* $Date: 2006/12/28 01:27:24 $ */
+/* ref: HEAD -> release/1.18.0, tag: v1.18.0-rc2 */
+/* git commit: aa0f396c0c8828ce39456129507fc72ef0208bd0 */
+/* commit time: 2017-07-13 14:58:01 +0200 */
diff --git a/src/mongo/crypto/tom/crypt_hash_is_valid.c b/src/third_party/tomcrypt-1.18.0-rc2/src/misc/crypt/crypt_hash_is_valid.c
index 107f70f35cc..1696537e50d 100644
--- a/src/mongo/crypto/tom/crypt_hash_is_valid.c
+++ b/src/third_party/tomcrypt-1.18.0-rc2/src/misc/crypt/crypt_hash_is_valid.c
@@ -5,8 +5,6 @@
*
* The library is free for all purposes without any express
* guarantee it works.
- *
- * Tom St Denis, tomstdenis@gmail.com, http://libtom.org
*/
#include "tomcrypt.h"
@@ -31,6 +29,6 @@ int hash_is_valid(int idx)
return CRYPT_OK;
}
-/* $Source: /cvs/libtom/libtomcrypt/src/misc/crypt/crypt_hash_is_valid.c,v $ */
-/* $Revision: 1.6 $ */
-/* $Date: 2006/12/28 01:27:24 $ */
+/* ref: HEAD -> release/1.18.0, tag: v1.18.0-rc2 */
+/* git commit: aa0f396c0c8828ce39456129507fc72ef0208bd0 */
+/* commit time: 2017-07-13 14:58:01 +0200 */
diff --git a/src/mongo/crypto/tom/crypt_register_hash.c b/src/third_party/tomcrypt-1.18.0-rc2/src/misc/crypt/crypt_register_hash.c
index ffdba10f263..0892a8d892f 100644
--- a/src/mongo/crypto/tom/crypt_register_hash.c
+++ b/src/third_party/tomcrypt-1.18.0-rc2/src/misc/crypt/crypt_register_hash.c
@@ -5,8 +5,6 @@
*
* The library is free for all purposes without any express
* guarantee it works.
- *
- * Tom St Denis, tomstdenis@gmail.com, http://libtom.org
*/
#include "tomcrypt.h"
@@ -49,6 +47,6 @@ int register_hash(const struct ltc_hash_descriptor *hash)
return -1;
}
-/* $Source: /cvs/libtom/libtomcrypt/src/misc/crypt/crypt_register_hash.c,v $ */
-/* $Revision: 1.7 $ */
-/* $Date: 2006/12/28 01:27:24 $ */
+/* ref: HEAD -> release/1.18.0, tag: v1.18.0-rc2 */
+/* git commit: aa0f396c0c8828ce39456129507fc72ef0208bd0 */
+/* commit time: 2017-07-13 14:58:01 +0200 */
diff --git a/src/mongo/crypto/tom/zeromem.c b/src/third_party/tomcrypt-1.18.0-rc2/src/misc/zeromem.c
index 46e499e66ce..a421309d786 100644
--- a/src/mongo/crypto/tom/zeromem.c
+++ b/src/third_party/tomcrypt-1.18.0-rc2/src/misc/zeromem.c
@@ -5,8 +5,6 @@
*
* The library is free for all purposes without any express
* guarantee it works.
- *
- * Tom St Denis, tomstdenis@gmail.com, http://libtom.org
*/
#include "tomcrypt.h"
@@ -20,15 +18,15 @@
@param out The destination of the area to zero
@param outlen The length of the area to zero (octets)
*/
-void zeromem(void *out, size_t outlen)
+void zeromem(volatile void *out, size_t outlen)
{
- unsigned char *mem = (unsigned char*)out;
+ volatile char *mem = out;
LTC_ARGCHKVD(out != NULL);
while (outlen-- > 0) {
- *mem++ = 0;
+ *mem++ = '\0';
}
}
-/* $Source: /cvs/libtom/libtomcrypt/src/misc/zeromem.c,v $ */
-/* $Revision: 1.7 $ */
-/* $Date: 2006/12/28 01:27:24 $ */
+/* ref: HEAD -> release/1.18.0, tag: v1.18.0-rc2 */
+/* git commit: aa0f396c0c8828ce39456129507fc72ef0208bd0 */
+/* commit time: 2017-07-13 14:58:01 +0200 */