diff options
author | 2020-04-13 06:16:03 +1000 | |
---|---|---|
committer | 2020-04-13 06:16:03 +1000 | |
commit | af82c0eca86eab376474f5771c8aa66c5a68c1d8 (patch) | |
tree | 1eb4c67a12dff4db27508cc3a105f8924ba447c8 | |
parent | c8cbf8abb2883aa5b4f87dd65bfef36a2154728a (diff) |
util: re-implement SHA-1 and get rid of the two third-party implementations (nw)
-rw-r--r-- | scripts/src/lib.lua | 3 | ||||
-rw-r--r-- | src/lib/util/crypto.hpp | 35 | ||||
-rw-r--r-- | src/lib/util/hashing.cpp | 183 | ||||
-rw-r--r-- | src/lib/util/hashing.h | 23 | ||||
-rw-r--r-- | src/lib/util/server_ws_impl.hpp | 1 | ||||
-rw-r--r-- | src/lib/util/sha1.cpp | 443 | ||||
-rw-r--r-- | src/lib/util/sha1.h | 63 | ||||
-rw-r--r-- | src/lib/util/sha1.hpp | 181 |
8 files changed, 198 insertions, 734 deletions
diff --git a/scripts/src/lib.lua b/scripts/src/lib.lua index b66b41d2313..a5ce6180857 100644 --- a/scripts/src/lib.lua +++ b/scripts/src/lib.lua @@ -93,9 +93,6 @@ project "utils" MAME_DIR .. "src/lib/util/server_https.hpp", MAME_DIR .. "src/lib/util/server_ws.hpp", MAME_DIR .. "src/lib/util/server_wss.hpp", - MAME_DIR .. "src/lib/util/sha1.cpp", - MAME_DIR .. "src/lib/util/sha1.h", - MAME_DIR .. "src/lib/util/sha1.hpp", MAME_DIR .. "src/lib/util/strformat.cpp", MAME_DIR .. "src/lib/util/strformat.h", MAME_DIR .. "src/lib/util/timeconv.cpp", diff --git a/src/lib/util/crypto.hpp b/src/lib/util/crypto.hpp index 705aad6a375..e28f5b1f9f5 100644 --- a/src/lib/util/crypto.hpp +++ b/src/lib/util/crypto.hpp @@ -1,18 +1,31 @@ -// license:MIT -// copyright-holders:Miodrag Milanovic -#ifndef CRYPTO_HPP -#define CRYPTO_HPP +// license:BSD-3-Clause +// copyright-holders:Vas Crabb +#ifndef MAME_UTIL_CRYPTO_HPP +#define MAME_UTIL_CRYPTO_HPP + +#pragma once + +#include "hashing.h" + +#include <cstdint> +#include <limits> +#include <string> -#include "base64.hpp" -#include "sha1.hpp" inline std::string sha1_encode(const std::string& input) { - char message_digest[20]; - sha1::calc(input.c_str(),input.length(),reinterpret_cast<unsigned char*>(message_digest)); - - return std::string(message_digest, sizeof(message_digest)); + util::sha1_creator digester; + const char *ptr = input.c_str(); + std::string::size_type remain = input.length(); + while (remain > std::numeric_limits<uint32_t>::max()) + { + digester.append(ptr, std::numeric_limits<uint32_t>::max()); + ptr += std::numeric_limits<uint32_t>::max(); + remain -= std::numeric_limits<uint32_t>::max(); + } + digester.append(ptr, uint32_t(remain)); + return std::string(reinterpret_cast<const char *>(digester.finish().m_raw), 20); } -#endif /* CRYPTO_HPP */ +#endif // MAME_UTIL_CRYPTO_HPP diff --git a/src/lib/util/hashing.cpp b/src/lib/util/hashing.cpp index a5946f1ace8..ebfc84fd33f 100644 --- a/src/lib/util/hashing.cpp +++ b/src/lib/util/hashing.cpp @@ -1,5 +1,5 @@ // license:BSD-3-Clause -// copyright-holders:Aaron Giles +// copyright-holders:Aaron Giles, Vas Crabb /*************************************************************************** hashing.c @@ -9,43 +9,114 @@ ***************************************************************************/ #include "hashing.h" + #include <zlib.h> + +#include <algorithm> #include <iomanip> #include <sstream> namespace util { -//************************************************************************** -// CONSTANTS -//************************************************************************** - -const crc16_t crc16_t::null = { 0 }; -const crc32_t crc32_t::null = { 0 }; -const md5_t md5_t::null = { { 0 } }; -const sha1_t sha1_t::null = { { 0 } }; - - //************************************************************************** // INLINE FUNCTIONS //************************************************************************** +namespace { + //------------------------------------------------- // char_to_hex - return the hex value of a // character //------------------------------------------------- -inline int char_to_hex(char c) +constexpr int char_to_hex(char c) +{ + return + (c >= '0' && c <= '9') ? (c - '0') : + (c >= 'a' && c <= 'f') ? (10 + c - 'a') : + (c >= 'A' && c <= 'F') ? (10 + c - 'A') : + -1; +} + + +constexpr uint32_t sha1_rol(uint32_t x, unsigned n) +{ + return (x << n) | (x >> (32 - n)); +} + +uint32_t sha1_b(uint32_t *data, unsigned i) +{ + uint32_t r = data[(i + 13) & 15U]; + r ^= data[(i + 8) & 15U]; + r ^= data[(i + 2) & 15U]; + r ^= data[i & 15U]; + r = sha1_rol(r, 1); + data[i & 15U] = r; + return r; +} + +inline void sha1_r0(const uint32_t *data, std::array<uint32_t, 5> &d, unsigned i) +{ + d[i % 5] = d[i % 5] + ((d[(i + 3) % 5] & (d[(i + 2) % 5] ^ d[(i + 1) % 5])) ^ d[(i + 1) % 5]) + data[i] + 0x5a827999U + sha1_rol(d[(i + 4) % 5], 5); + d[(i + 3) % 5] = sha1_rol(d[(i + 3) % 5], 30); +} + +inline void sha1_r1(uint32_t *data, std::array<uint32_t, 5> &d, unsigned i) +{ + d[i % 5] = d[i % 5] + ((d[(i + 3) % 5] & (d[(i + 2) % 5] ^ d[(i + 1) % 5])) ^ d[(i + 1) % 5])+ sha1_b(data, i) + 0x5a827999U + sha1_rol(d[(i + 4) % 5], 5); + d[(i + 3) % 5] = sha1_rol(d[(i + 3) % 5], 30); +} + +inline void sha1_r2(uint32_t *data, std::array<uint32_t, 5> &d, unsigned i) +{ + d[i % 5] = d[i % 5] + (d[(i + 3) % 5] ^ d[(i + 2) % 5] ^ d[(i + 1) % 5]) + sha1_b(data, i) + 0x6ed9eba1U + sha1_rol(d[(i + 4) % 5], 5); + d[(i + 3) % 5] = sha1_rol(d[(i + 3) % 5], 30); +} + +inline void sha1_r3(uint32_t *data, std::array<uint32_t, 5> &d, unsigned i) +{ + d[i % 5] = d[i % 5] + (((d[(i + 3) % 5] | d[(i + 2) % 5]) & d[(i + 1) % 5]) | (d[(i + 3) % 5] & d[(i + 2) % 5])) + sha1_b(data, i) + 0x8f1bbcdcU + sha1_rol(d[(i + 4) % 5], 5); + d[(i + 3) % 5] = sha1_rol(d[(i + 3) % 5], 30); +} + +inline void sha1_r4(uint32_t *data, std::array<uint32_t, 5> &d, unsigned i) +{ + d[i % 5] = d[i % 5] + (d[(i + 3) % 5] ^ d[(i + 2) % 5] ^ d[(i + 1) % 5]) + sha1_b(data, i) + 0xca62c1d6U + sha1_rol(d[(i + 4) % 5], 5); + d[(i + 3) % 5] = sha1_rol(d[(i + 3) % 5], 30); +} + +inline void sha1_process(std::array<uint32_t, 5> &st, uint32_t *data) { - if (c >= '0' && c <= '9') - return c - '0'; - if (c >= 'a' && c <= 'f') - return 10 + c - 'a'; - if (c >= 'A' && c <= 'F') - return 10 + c - 'A'; - return -1; + std::array<uint32_t, 5> d = st; + unsigned i = 0U; + while (i < 16U) + sha1_r0(data, d, i++); + while (i < 20U) + sha1_r1(data, d, i++); + while (i < 40U) + sha1_r2(data, d, i++); + while (i < 60U) + sha1_r3(data, d, i++); + while (i < 80U) + sha1_r4(data, d, i++); + for (i = 0U; i < 5U; i++) + st[i] += d[i]; } +} // anonymous namespace + + + +//************************************************************************** +// CONSTANTS +//************************************************************************** + +const crc16_t crc16_t::null = { 0 }; +const crc32_t crc32_t::null = { 0 }; +const md5_t md5_t::null = { { 0 } }; +const sha1_t sha1_t::null = { { 0 } }; + //************************************************************************** @@ -93,6 +164,80 @@ std::string sha1_t::as_string() const } +//------------------------------------------------- +// reset - prepare to digest a block of data +//------------------------------------------------- + +void sha1_creator::reset() +{ + m_cnt = 0U; + m_st[0] = 0xc3d2e1f0U; + m_st[1] = 0x10325476U; + m_st[2] = 0x98badcfeU; + m_st[3] = 0xefcdab89U; + m_st[4] = 0x67452301U; +} + + +//------------------------------------------------- +// append - digest a block of data +//------------------------------------------------- + +void sha1_creator::append(const void *data, uint32_t length) +{ +#ifdef LSB_FIRST + constexpr unsigned swizzle = 3U; +#else + constexpr unsigned swizzle = 0U; +#endif + uint32_t residual = (uint32_t(m_cnt) >> 3) & 63U; + m_cnt += uint64_t(length) << 3; + uint32_t offset = 0U; + if (length >= (64U - residual)) + { + if (residual) + { + for (offset = 0U; (offset + residual) < 64U; offset++) + reinterpret_cast<uint8_t *>(m_buf)[(offset + residual) ^ swizzle] = reinterpret_cast<const uint8_t *>(data)[offset]; + sha1_process(m_st, m_buf); + } + while ((length - offset) >= 64U) + { + for (residual = 0U; residual < 64U; residual++, offset++) + reinterpret_cast<uint8_t *>(m_buf)[residual ^ swizzle] = reinterpret_cast<const uint8_t *>(data)[offset]; + sha1_process(m_st, m_buf); + } + residual = 0U; + } + for ( ; offset < length; residual++, offset++) + reinterpret_cast<uint8_t *>(m_buf)[residual ^ swizzle] = reinterpret_cast<const uint8_t *>(data)[offset]; +} + + +//------------------------------------------------- +// finish - compute final hash +//------------------------------------------------- + +sha1_t sha1_creator::finish() +{ + const unsigned padlen = 64U - (63U & ((unsigned(m_cnt) >> 3) + 8U)); + uint8_t padbuf[64]; + padbuf[0] = 0x80; + for (unsigned i = 1U; i < padlen; i++) + padbuf[i] = 0x00; + uint8_t lenbuf[8]; + for (unsigned i = 0U; i < 8U; i++) + lenbuf[i] = uint8_t(m_cnt >> ((7U - i) << 3)); + append(padbuf, padlen); + append(lenbuf, sizeof(lenbuf)); + sha1_t result; + for (unsigned i = 0U; i < 20U; i++) + result.m_raw[i] = uint8_t(m_st[4U - (i >> 2)] >> ((3U - (i & 3)) << 3)); + return result; +} + + + //************************************************************************** // MD-5 HELPERS //************************************************************************** diff --git a/src/lib/util/hashing.h b/src/lib/util/hashing.h index b1505c654a4..11bf7fd655e 100644 --- a/src/lib/util/hashing.h +++ b/src/lib/util/hashing.h @@ -1,5 +1,5 @@ // license:BSD-3-Clause -// copyright-holders:Aaron Giles +// copyright-holders:Aaron Giles, Vas Crabb /*************************************************************************** hashing.h @@ -7,7 +7,6 @@ Hashing helper classes. ***************************************************************************/ - #ifndef MAME_UTIL_HASHING_H #define MAME_UTIL_HASHING_H @@ -16,13 +15,14 @@ #include "osdcore.h" #include "corestr.h" #include "md5.h" -#include "sha1.h" +#include <array> #include <functional> #include <string> namespace util { + //************************************************************************** // TYPE DEFINITIONS //************************************************************************** @@ -50,19 +50,13 @@ public: sha1_creator() { reset(); } // reset - void reset() { sha1_init(&m_context); } + void reset(); // append data - void append(const void *data, uint32_t length) { sha1_update(&m_context, length, reinterpret_cast<const uint8_t *>(data)); } + void append(const void *data, uint32_t length); // finalize and compute the final digest - sha1_t finish() - { - sha1_t result; - sha1_final(&m_context); - sha1_digest(&m_context, sizeof(result.m_raw), result.m_raw); - return result; - } + sha1_t finish(); // static wrapper to just get the digest from a block static sha1_t simple(const void *data, uint32_t length) @@ -73,8 +67,9 @@ public: } protected: - // internal state - struct sha1_ctx m_context; // internal context + uint64_t m_cnt; + std::array<uint32_t, 5> m_st; + uint32_t m_buf[16]; }; diff --git a/src/lib/util/server_ws_impl.hpp b/src/lib/util/server_ws_impl.hpp index 9fc5876cab2..dab73b63a67 100644 --- a/src/lib/util/server_ws_impl.hpp +++ b/src/lib/util/server_ws_impl.hpp @@ -3,6 +3,7 @@ #ifndef MAME_LIB_UTIL_SERVER_WS_IMPL_HPP #define MAME_LIB_UTIL_SERVER_WS_IMPL_HPP #include "path_to_regex.hpp" +#include "base64.hpp" #include "crypto.hpp" #include "asio.h" diff --git a/src/lib/util/sha1.cpp b/src/lib/util/sha1.cpp deleted file mode 100644 index abc073cb6e8..00000000000 --- a/src/lib/util/sha1.cpp +++ /dev/null @@ -1,443 +0,0 @@ -// license:LGPL-2.1+ -// copyright-holders:Peter Gutmann, Andrew Kuchling, Niels Moeller -/* sha1.h - * - * The sha1 hash function. - */ - -/* nettle, low-level cryptographics library - * - * Copyright 2001 Peter Gutmann, Andrew Kuchling, Niels Moeller - * - * The nettle library is free software; you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation; either version 2.1 of the License, or (at your - * option) any later version. - * - * The nettle library 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 Lesser General Public - * License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with the nettle library; see the file COPYING.LIB. If not, write to - * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, - * MA 02111-1307, USA. - */ - -#include "sha1.h" - -#include <cassert> -#include <cstdlib> -#include <cstring> - -static unsigned int READ_UINT32(const uint8_t* data) -{ - return ((uint32_t)data[0] << 24) | - ((uint32_t)data[1] << 16) | - ((uint32_t)data[2] << 8) | - ((uint32_t)data[3]); -} - -static void WRITE_UINT32(unsigned char* data, uint32_t val) -{ - data[0] = (val >> 24) & 0xFF; - data[1] = (val >> 16) & 0xFF; - data[2] = (val >> 8) & 0xFF; - data[3] = (val >> 0) & 0xFF; -} - - -/* A block, treated as a sequence of 32-bit words. */ -#define SHA1_DATA_LENGTH 16 - -/* The SHA f()-functions. The f1 and f3 functions can be optimized to - save one boolean operation each - thanks to Rich Schroeppel, - rcs@cs.arizona.edu for discovering this */ - -/* #define f1(x,y,z) ( ( x & y ) | ( ~x & z ) ) Rounds 0-19 */ -#define f1(x,y,z) ( z ^ ( x & ( y ^ z ) ) ) /* Rounds 0-19 */ -#define f2(x,y,z) ( x ^ y ^ z ) /* Rounds 20-39 */ -/* #define f3(x,y,z) ( ( x & y ) | ( x & z ) | ( y & z ) ) Rounds 40-59 */ -#define f3(x,y,z) ( ( x & y ) | ( z & ( x | y ) ) ) /* Rounds 40-59 */ -#define f4(x,y,z) ( x ^ y ^ z ) /* Rounds 60-79 */ - -/* The SHA Mysterious Constants */ - -#define K1 0x5A827999L /* Rounds 0-19 */ -#define K2 0x6ED9EBA1L /* Rounds 20-39 */ -#define K3 0x8F1BBCDCL /* Rounds 40-59 */ -#define K4 0xCA62C1D6L /* Rounds 60-79 */ - -/* SHA initial values */ - -#define h0init 0x67452301L -#define h1init 0xEFCDAB89L -#define h2init 0x98BADCFEL -#define h3init 0x10325476L -#define h4init 0xC3D2E1F0L - -/* 32-bit rotate left - kludged with shifts */ -#ifdef _MSC_VER -#define ROTL(n,X) _lrotl(X, n) -#else -#define ROTL(n,X) ( ( (X) << (n) ) | ( (X) >> ( 32 - (n) ) ) ) -#endif - -/* The initial expanding function. The hash function is defined over an - 80-word expanded input array W, where the first 16 are copies of the input - data, and the remaining 64 are defined by - - W[ i ] = W[ i - 16 ] ^ W[ i - 14 ] ^ W[ i - 8 ] ^ W[ i - 3 ] - - This implementation generates these values on the fly in a circular - buffer - thanks to Colin Plumb, colin@nyx10.cs.du.edu for this - optimization. - - The updated SHA changes the expanding function by adding a rotate of 1 - bit. Thanks to Jim Gillogly, jim@rand.org, and an anonymous contributor - for this information */ - -#define expand(W,i) ( W[ i & 15 ] = \ - ROTL( 1, ( W[ i & 15 ] ^ W[ (i - 14) & 15 ] ^ \ - W[ (i - 8) & 15 ] ^ W[ (i - 3) & 15 ] ) ) ) - - -/* The prototype SHA sub-round. The fundamental sub-round is: - - a' = e + ROTL( 5, a ) + f( b, c, d ) + k + data; - b' = a; - c' = ROTL( 30, b ); - d' = c; - e' = d; - - but this is implemented by unrolling the loop 5 times and renaming the - variables ( e, a, b, c, d ) = ( a', b', c', d', e' ) each iteration. - This code is then replicated 20 times for each of the 4 functions, using - the next 20 values from the W[] array each time */ - -#define subRound(a, b, c, d, e, f, k, data) \ - ( e += ROTL( 5, a ) + f( b, c, d ) + k + data, b = ROTL( 30, b ) ) - -/* Initialize the SHA values */ - -/** - * @fn void sha1_init(struct sha1_ctx *ctx) - * - * @brief Sha 1 initialise. - * - * @param [in,out] ctx If non-null, the context. - */ - -void -sha1_init(struct sha1_ctx *ctx) -{ - /* Set the h-vars to their initial values */ - ctx->digest[ 0 ] = h0init; - ctx->digest[ 1 ] = h1init; - ctx->digest[ 2 ] = h2init; - ctx->digest[ 3 ] = h3init; - ctx->digest[ 4 ] = h4init; - - /* Initialize bit count */ - ctx->count_low = ctx->count_high = 0; - - /* Initialize buffer */ - ctx->index = 0; -} - -/* Perform the SHA transformation. Note that this code, like MD5, seems to - break some optimizing compilers due to the complexity of the expressions - and the size of the basic block. It may be necessary to split it into - sections, e.g. based on the four subrounds - - Note that this function destroys the data area */ - -/** - * @fn static void sha1_transform(uint32_t *state, uint32_t *data) - * - * @brief Sha 1 transform. - * - * @param [in,out] state If non-null, the state. - * @param [in,out] data If non-null, the data. - */ - -static void -sha1_transform(uint32_t *state, uint32_t *data) -{ - uint32_t A, B, C, D, E; /* Local vars */ - - /* Set up first buffer and local data buffer */ - A = state[0]; - B = state[1]; - C = state[2]; - D = state[3]; - E = state[4]; - - /* Heavy mangling, in 4 sub-rounds of 20 iterations each. */ - subRound( A, B, C, D, E, f1, K1, data[ 0] ); - subRound( E, A, B, C, D, f1, K1, data[ 1] ); - subRound( D, E, A, B, C, f1, K1, data[ 2] ); - subRound( C, D, E, A, B, f1, K1, data[ 3] ); - subRound( B, C, D, E, A, f1, K1, data[ 4] ); - subRound( A, B, C, D, E, f1, K1, data[ 5] ); - subRound( E, A, B, C, D, f1, K1, data[ 6] ); - subRound( D, E, A, B, C, f1, K1, data[ 7] ); - subRound( C, D, E, A, B, f1, K1, data[ 8] ); - subRound( B, C, D, E, A, f1, K1, data[ 9] ); - subRound( A, B, C, D, E, f1, K1, data[10] ); - subRound( E, A, B, C, D, f1, K1, data[11] ); - subRound( D, E, A, B, C, f1, K1, data[12] ); - subRound( C, D, E, A, B, f1, K1, data[13] ); - subRound( B, C, D, E, A, f1, K1, data[14] ); - subRound( A, B, C, D, E, f1, K1, data[15] ); - subRound( E, A, B, C, D, f1, K1, expand( data, 16 ) ); - subRound( D, E, A, B, C, f1, K1, expand( data, 17 ) ); - subRound( C, D, E, A, B, f1, K1, expand( data, 18 ) ); - subRound( B, C, D, E, A, f1, K1, expand( data, 19 ) ); - - subRound( A, B, C, D, E, f2, K2, expand( data, 20 ) ); - subRound( E, A, B, C, D, f2, K2, expand( data, 21 ) ); - subRound( D, E, A, B, C, f2, K2, expand( data, 22 ) ); - subRound( C, D, E, A, B, f2, K2, expand( data, 23 ) ); - subRound( B, C, D, E, A, f2, K2, expand( data, 24 ) ); - subRound( A, B, C, D, E, f2, K2, expand( data, 25 ) ); - subRound( E, A, B, C, D, f2, K2, expand( data, 26 ) ); - subRound( D, E, A, B, C, f2, K2, expand( data, 27 ) ); - subRound( C, D, E, A, B, f2, K2, expand( data, 28 ) ); - subRound( B, C, D, E, A, f2, K2, expand( data, 29 ) ); - subRound( A, B, C, D, E, f2, K2, expand( data, 30 ) ); - subRound( E, A, B, C, D, f2, K2, expand( data, 31 ) ); - subRound( D, E, A, B, C, f2, K2, expand( data, 32 ) ); - subRound( C, D, E, A, B, f2, K2, expand( data, 33 ) ); - subRound( B, C, D, E, A, f2, K2, expand( data, 34 ) ); - subRound( A, B, C, D, E, f2, K2, expand( data, 35 ) ); - subRound( E, A, B, C, D, f2, K2, expand( data, 36 ) ); - subRound( D, E, A, B, C, f2, K2, expand( data, 37 ) ); - subRound( C, D, E, A, B, f2, K2, expand( data, 38 ) ); - subRound( B, C, D, E, A, f2, K2, expand( data, 39 ) ); - - subRound( A, B, C, D, E, f3, K3, expand( data, 40 ) ); - subRound( E, A, B, C, D, f3, K3, expand( data, 41 ) ); - subRound( D, E, A, B, C, f3, K3, expand( data, 42 ) ); - subRound( C, D, E, A, B, f3, K3, expand( data, 43 ) ); - subRound( B, C, D, E, A, f3, K3, expand( data, 44 ) ); - subRound( A, B, C, D, E, f3, K3, expand( data, 45 ) ); - subRound( E, A, B, C, D, f3, K3, expand( data, 46 ) ); - subRound( D, E, A, B, C, f3, K3, expand( data, 47 ) ); - subRound( C, D, E, A, B, f3, K3, expand( data, 48 ) ); - subRound( B, C, D, E, A, f3, K3, expand( data, 49 ) ); - subRound( A, B, C, D, E, f3, K3, expand( data, 50 ) ); - subRound( E, A, B, C, D, f3, K3, expand( data, 51 ) ); - subRound( D, E, A, B, C, f3, K3, expand( data, 52 ) ); - subRound( C, D, E, A, B, f3, K3, expand( data, 53 ) ); - subRound( B, C, D, E, A, f3, K3, expand( data, 54 ) ); - subRound( A, B, C, D, E, f3, K3, expand( data, 55 ) ); - subRound( E, A, B, C, D, f3, K3, expand( data, 56 ) ); - subRound( D, E, A, B, C, f3, K3, expand( data, 57 ) ); - subRound( C, D, E, A, B, f3, K3, expand( data, 58 ) ); - subRound( B, C, D, E, A, f3, K3, expand( data, 59 ) ); - - subRound( A, B, C, D, E, f4, K4, expand( data, 60 ) ); - subRound( E, A, B, C, D, f4, K4, expand( data, 61 ) ); - subRound( D, E, A, B, C, f4, K4, expand( data, 62 ) ); - subRound( C, D, E, A, B, f4, K4, expand( data, 63 ) ); - subRound( B, C, D, E, A, f4, K4, expand( data, 64 ) ); - subRound( A, B, C, D, E, f4, K4, expand( data, 65 ) ); - subRound( E, A, B, C, D, f4, K4, expand( data, 66 ) ); - subRound( D, E, A, B, C, f4, K4, expand( data, 67 ) ); - subRound( C, D, E, A, B, f4, K4, expand( data, 68 ) ); - subRound( B, C, D, E, A, f4, K4, expand( data, 69 ) ); - subRound( A, B, C, D, E, f4, K4, expand( data, 70 ) ); - subRound( E, A, B, C, D, f4, K4, expand( data, 71 ) ); - subRound( D, E, A, B, C, f4, K4, expand( data, 72 ) ); - subRound( C, D, E, A, B, f4, K4, expand( data, 73 ) ); - subRound( B, C, D, E, A, f4, K4, expand( data, 74 ) ); - subRound( A, B, C, D, E, f4, K4, expand( data, 75 ) ); - subRound( E, A, B, C, D, f4, K4, expand( data, 76 ) ); - subRound( D, E, A, B, C, f4, K4, expand( data, 77 ) ); - subRound( C, D, E, A, B, f4, K4, expand( data, 78 ) ); - subRound( B, C, D, E, A, f4, K4, expand( data, 79 ) ); - - /* Build message digest */ - state[0] += A; - state[1] += B; - state[2] += C; - state[3] += D; - state[4] += E; -} - -/** - * @fn static void sha1_block(struct sha1_ctx *ctx, const uint8_t *block) - * - * @brief Sha 1 block. - * - * @param [in,out] ctx If non-null, the context. - * @param block The block. - */ - -static void -sha1_block(struct sha1_ctx *ctx, const uint8_t *block) -{ - uint32_t data[SHA1_DATA_LENGTH]; - int i; - - /* Update block count */ - if (!++ctx->count_low) - ++ctx->count_high; - - /* Endian independent conversion */ - for (i = 0; i<SHA1_DATA_LENGTH; i++, block += 4) - data[i] = READ_UINT32(block); - - sha1_transform(ctx->digest, data); -} - -/** - * @fn void sha1_update(struct sha1_ctx *ctx, unsigned length, const uint8_t *buffer) - * - * @brief Sha 1 update. - * - * @param [in,out] ctx If non-null, the context. - * @param length The length. - * @param buffer The buffer. - */ - -void -sha1_update(struct sha1_ctx *ctx, - unsigned length, const uint8_t *buffer) -{ - if (ctx->index) - { /* Try to fill partial block */ - unsigned left = SHA1_DATA_SIZE - ctx->index; - if (length < left) - { - memcpy(ctx->block + ctx->index, buffer, length); - ctx->index += length; - return; /* Finished */ - } - else - { - memcpy(ctx->block + ctx->index, buffer, left); - sha1_block(ctx, ctx->block); - buffer += left; - length -= left; - } - } - while (length >= SHA1_DATA_SIZE) - { - sha1_block(ctx, buffer); - buffer += SHA1_DATA_SIZE; - length -= SHA1_DATA_SIZE; - } - ctx->index = length; - if (length) - /* Buffer leftovers */ - memcpy(ctx->block, buffer, length); -} - -/* Final wrapup - pad to SHA1_DATA_SIZE-byte boundary with the bit pattern - 1 0* (64-bit count of bits processed, MSB-first) */ - -/** - * @fn void sha1_final(struct sha1_ctx *ctx) - * - * @brief Sha 1 final. - * - * @param [in,out] ctx If non-null, the context. - */ - -void -sha1_final(struct sha1_ctx *ctx) -{ - uint32_t data[SHA1_DATA_LENGTH]; - int i; - int words; - - i = ctx->index; - - /* Set the first char of padding to 0x80. This is safe since there is - always at least one byte free */ - - assert(i < SHA1_DATA_SIZE); - ctx->block[i++] = 0x80; - - /* Fill rest of word */ - for( ; i & 3; i++) - ctx->block[i] = 0; - - /* i is now a multiple of the word size 4 */ - words = i >> 2; - for (i = 0; i < words; i++) - data[i] = READ_UINT32(ctx->block + 4*i); - - if (words > (SHA1_DATA_LENGTH-2)) - { /* No room for length in this block. Process it and - * pad with another one */ - for (i = words ; i < SHA1_DATA_LENGTH; i++) - data[i] = 0; - sha1_transform(ctx->digest, data); - for (i = 0; i < (SHA1_DATA_LENGTH-2); i++) - data[i] = 0; - } - else - for (i = words ; i < SHA1_DATA_LENGTH - 2; i++) - data[i] = 0; - - /* There are 512 = 2^9 bits in one block */ - data[SHA1_DATA_LENGTH-2] = (ctx->count_high << 9) | (ctx->count_low >> 23); - data[SHA1_DATA_LENGTH-1] = (ctx->count_low << 9) | (ctx->index << 3); - sha1_transform(ctx->digest, data); -} - -/** - * @fn void sha1_digest(const struct sha1_ctx *ctx, unsigned length, uint8_t *digest) - * - * @brief Sha 1 digest. - * - * @param ctx The context. - * @param length The length. - * @param [in,out] digest If non-null, the digest. - */ - -void -sha1_digest(const struct sha1_ctx *ctx, - unsigned length, - uint8_t *digest) -{ - unsigned i; - unsigned words; - unsigned leftover; - - assert(length <= SHA1_DIGEST_SIZE); - - words = length / 4; - leftover = length % 4; - - for (i = 0; i < words; i++, digest += 4) - WRITE_UINT32(digest, ctx->digest[i]); - - if (leftover) - { - uint32_t word; - unsigned j = leftover; - - assert(i < _SHA1_DIGEST_LENGTH); - - word = ctx->digest[i]; - - switch (leftover) - { - default: - /* this is just here to keep the compiler happy; it can never happen */ - case 3: - digest[--j] = (word >> 8) & 0xff; - /* Fall through */ - case 2: - digest[--j] = (word >> 16) & 0xff; - /* Fall through */ - case 1: - digest[--j] = (word >> 24) & 0xff; - } - } -} diff --git a/src/lib/util/sha1.h b/src/lib/util/sha1.h deleted file mode 100644 index 177caf9f6dc..00000000000 --- a/src/lib/util/sha1.h +++ /dev/null @@ -1,63 +0,0 @@ -// license:LGPL-2.1+ -// copyright-holders:Peter Gutmann, Andrew Kuchling, Niels Moeller -/* sha1.h - * - * The sha1 hash function. - */ - -/* nettle, low-level cryptographics library - * - * Copyright 2001 Niels Moeller - * - * The nettle library is free software; you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation; either version 2.1 of the License, or (at your - * option) any later version. - * - * The nettle library 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 Lesser General Public - * License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with the nettle library; see the file COPYING.LIB. If not, write to - * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, - * MA 02111-1307, USA. - */ - -#ifndef NETTLE_SHA1_H_INCLUDED -#define NETTLE_SHA1_H_INCLUDED - -#include "osdcore.h" - -#define SHA1_DIGEST_SIZE 20 -#define SHA1_DATA_SIZE 64 - -/* Digest is kept internally as 4 32-bit words. */ -#define _SHA1_DIGEST_LENGTH 5 - -struct sha1_ctx -{ - uint32_t digest[_SHA1_DIGEST_LENGTH]; /* Message digest */ - uint32_t count_low, count_high; /* 64-bit block count */ - uint8_t block[SHA1_DATA_SIZE]; /* SHA1 data buffer */ - unsigned int index; /* index into buffer */ -}; - -void -sha1_init(struct sha1_ctx *ctx); - -void -sha1_update(struct sha1_ctx *ctx, - unsigned length, - const uint8_t *data); - -void -sha1_final(struct sha1_ctx *ctx); - -void -sha1_digest(const struct sha1_ctx *ctx, - unsigned length, - uint8_t *digest); - -#endif /* NETTLE_SHA1_H_INCLUDED */ diff --git a/src/lib/util/sha1.hpp b/src/lib/util/sha1.hpp deleted file mode 100644 index a293bebb7cc..00000000000 --- a/src/lib/util/sha1.hpp +++ /dev/null @@ -1,181 +0,0 @@ -// license:BSD-3-Clause -// copyright-holders:Micael Hildenborg -/* - Copyright (c) 2011, Micael Hildenborg - All rights reserved. - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are met: - * Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - * Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in the - documentation and/or other materials provided with the distribution. - * Neither the name of Micael Hildenborg nor the - names of its contributors may be used to endorse or promote products - derived from this software without specific prior written permission. - - THIS SOFTWARE IS PROVIDED BY Micael Hildenborg ''AS IS'' AND ANY - EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - DISCLAIMED. IN NO EVENT SHALL Micael Hildenborg BE LIABLE FOR ANY - DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND - ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#ifndef SHA1_DEFINED -#define SHA1_DEFINED - -namespace sha1 { - -namespace { // local - -// Rotate an integer value to left. -inline unsigned int rol(unsigned int value, unsigned int steps) { - return ((value << steps) | (value >> (32 - steps))); -} - -// Sets the first 16 integers in the buffert to zero. -// Used for clearing the W buffert. -inline void clearWBuffert(unsigned int * buffert) -{ - for (int pos = 16; --pos >= 0;) - { - buffert[pos] = 0; - } -} - -inline void innerHash(unsigned int * result, unsigned int * w) -{ - unsigned int a = result[0]; - unsigned int b = result[1]; - unsigned int c = result[2]; - unsigned int d = result[3]; - unsigned int e = result[4]; - - int round = 0; - - #define sha1macro(func,val) \ - { \ - const unsigned int t = rol(a, 5) + (func) + e + val + w[round]; \ - e = d; \ - d = c; \ - c = rol(b, 30); \ - b = a; \ - a = t; \ - } - - while (round < 16) - { - sha1macro((b & c) | (~b & d), 0x5a827999) - ++round; - } - while (round < 20) - { - w[round] = rol((w[round - 3] ^ w[round - 8] ^ w[round - 14] ^ w[round - 16]), 1); - sha1macro((b & c) | (~b & d), 0x5a827999) - ++round; - } - while (round < 40) - { - w[round] = rol((w[round - 3] ^ w[round - 8] ^ w[round - 14] ^ w[round - 16]), 1); - sha1macro(b ^ c ^ d, 0x6ed9eba1) - ++round; - } - while (round < 60) - { - w[round] = rol((w[round - 3] ^ w[round - 8] ^ w[round - 14] ^ w[round - 16]), 1); - sha1macro((b & c) | (b & d) | (c & d), 0x8f1bbcdc) - ++round; - } - while (round < 80) - { - w[round] = rol((w[round - 3] ^ w[round - 8] ^ w[round - 14] ^ w[round - 16]), 1); - sha1macro(b ^ c ^ d, 0xca62c1d6) - ++round; - } - - #undef sha1macro - - result[0] += a; - result[1] += b; - result[2] += c; - result[3] += d; - result[4] += e; -} - -} // namespace - -/// Calculate a SHA1 hash -/** - * @param src points to any kind of data to be hashed. - * @param bytelength the number of bytes to hash from the src pointer. - * @param hash should point to a buffer of at least 20 bytes of size for storing - * the sha1 result in. - */ -inline void calc(void const * src, size_t bytelength, unsigned char * hash) { - // Init the result array. - unsigned int result[5] = { 0x67452301, 0xefcdab89, 0x98badcfe, - 0x10325476, 0xc3d2e1f0 }; - - // Cast the void src pointer to be the byte array we can work with. - unsigned char const * sarray = static_cast<unsigned char const *>(src); - - // The reusable round buffer - unsigned int w[80]; - - // Loop through all complete 64byte blocks. - - size_t endCurrentBlock; - size_t currentBlock = 0; - - if (bytelength >= 64) { - size_t const endOfFullBlocks = bytelength - 64; - - while (currentBlock <= endOfFullBlocks) { - endCurrentBlock = currentBlock + 64; - - // Init the round buffer with the 64 byte block data. - for (int roundPos = 0; currentBlock < endCurrentBlock; currentBlock += 4) - { - // This line will swap endian on big endian and keep endian on - // little endian. - w[roundPos++] = static_cast<unsigned int>(sarray[currentBlock + 3]) - | (static_cast<unsigned int>(sarray[currentBlock + 2]) << 8) - | (static_cast<unsigned int>(sarray[currentBlock + 1]) << 16) - | (static_cast<unsigned int>(sarray[currentBlock]) << 24); - } - innerHash(result, w); - } - } - - // Handle the last and not full 64 byte block if existing. - endCurrentBlock = bytelength - currentBlock; - clearWBuffert(w); - size_t lastBlockBytes = 0; - for (;lastBlockBytes < endCurrentBlock; ++lastBlockBytes) { - w[lastBlockBytes >> 2] |= static_cast<unsigned int>(sarray[lastBlockBytes + currentBlock]) << ((3 - (lastBlockBytes & 3)) << 3); - } - - w[lastBlockBytes >> 2] |= 0x80 << ((3 - (lastBlockBytes & 3)) << 3); - if (endCurrentBlock >= 56) { - innerHash(result, w); - clearWBuffert(w); - } - w[15] = bytelength << 3; - innerHash(result, w); - - // Store hash in result pointer, and make sure we get in in the correct - // order on both endian models. - for (int hashByte = 20; --hashByte >= 0;) { - hash[hashByte] = (result[hashByte >> 2] >> (((3 - hashByte) & 0x3) << 3)) & 0xff; - } -} - -} // namespace sha1 - -#endif // SHA1_DEFINED |