Add tiger. It depends on duckhandy, so add that too.

This commit is contained in:
King_DuckZ 2020-08-14 00:46:36 +01:00
parent d1af39d2a7
commit 33fe2bc8cd
8 changed files with 1752 additions and 1 deletions

3
.gitmodules vendored
View file

@ -10,3 +10,6 @@
[submodule "src/gnulib"]
path = src/gnulib
url = git://git.savannah.gnu.org/gnulib.git
[submodule "subprojects/duckhandy"]
path = subprojects/duckhandy
url = http://alarmpi.no-ip.org/gitan/King_DuckZ/duckhandy.git

View file

@ -5,6 +5,7 @@ project('orotool', 'cpp', 'c',
license: 'GPL3+',
)
duckhandy_inc = include_directories('subprojects/duckhandy/include')
date_inc = include_directories('subprojects/date/include')
subdir('src')

View file

@ -57,9 +57,11 @@ executable(meson.project_name(),
'gnulib/lib/base64.c',
'base64.cpp',
'app_config.cpp',
'oro/tiger.c',
'oro/tiger.cpp',
project_config_file,
install: true,
dependencies: lib_deps,
include_directories: date_inc,
include_directories: [date_inc, duckhandy_inc],
cpp_args: ['-DEV_USE_STDEXCEPT'],
)

1347
src/oro/tiger.c Normal file

File diff suppressed because it is too large Load diff

219
src/oro/tiger.cpp Normal file
View file

@ -0,0 +1,219 @@
/* Copyright 2015, 2016, Michele Santullo
* This file is part of "dindexer".
*
* "dindexer" is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* "dindexer" 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with "dindexer". If not, see <http://www.gnu.org/licenses/>.
*/
#include "tiger.hpp"
#include "duckhandy/int_conv.hpp"
#include <fstream>
#include <cstdint>
#include <memory>
#include <cassert>
#include <algorithm>
#include <utility>
#if !defined(NDEBUG)
# include <sstream>
#endif
#include <iomanip>
#include <string_view>
extern "C" void tiger ( const char* parStr, uint64_t parLength, uint64_t parHash[3], char parPadding );
#if defined(__SSE2__)
# define USE_TIGER_SSE2
#endif
#if defined(USE_TIGER_SSE2)
extern "C" void tiger_sse2_chunk ( const char* parStr1, const char* parStr2, uint64_t parLength, uint64_t parRes1[3], uint64_t parRes2[3] );
extern "C" void tiger_sse2_last_chunk ( const char* parStr1, const char* parStr2, uint64_t parLength, uint64_t parRealLength1, uint64_t parRealLength2, uint64_t parRes1[3], uint64_t parRes2[3], char parPadding );
#else
extern "C" void tiger_2_chunk ( const char* parStr1, const char* parStr2, uint64_t parLength, uint64_t parRes1[3], uint64_t parRes2[3] );
extern "C" void tiger_2_last_chunk ( const char* parStr1, const char* parStr2, uint64_t parLength, uint64_t parRealLength1, uint64_t parRealLength2, uint64_t parRes1[3], uint64_t parRes2[3], char parPadding );
#endif
namespace mchlib {
namespace {
const uint32_t g_buff_size = 1024 * 8;
const char g_tiger_padding = 0x80; //0x01 for V1
uint64_t swap_long (uint64_t parNum) {
parNum = (parNum & 0x00000000FFFFFFFF) << 32 | (parNum & 0xFFFFFFFF00000000) >> 32;
parNum = (parNum & 0x0000FFFF0000FFFF) << 16 | (parNum & 0xFFFF0000FFFF0000) >> 16;
parNum = (parNum & 0x00FF00FF00FF00FF) << 8 | (parNum & 0xFF00FF00FF00FF00) >> 8;
return parNum;
}
uint64_t to_hash_part (std::string_view str) {
return swap_long(
dhandy::ary_to_int<uint64_t, char, 16>(
str.data(),
str.data() + str.size()
)
);
}
template <bool Upcase>
std::string tiger_to_string_impl (const TigerHash& parHash) {
typedef dhandy::ASCIITranslator<char, '0', (Upcase ? 'A' : 'a')> Ascii;
std::string retval(sizeof(uint64_t) * 2 * 3, '0');
{
auto str_ary_a = dhandy::int_to_ary<uint64_t, 16, Ascii>(swap_long(parHash.part_a));
std::copy(str_ary_a.begin(), str_ary_a.end(), retval.begin() + sizeof(uint64_t) * 2 * 1 - str_ary_a.size() + 1);
}
{
auto str_ary_b = dhandy::int_to_ary<uint64_t, 16, Ascii>(swap_long(parHash.part_b));
std::copy(str_ary_b.begin(), str_ary_b.end(), retval.begin() + sizeof(uint64_t) * 2 * 2 - str_ary_b.size() + 1);
}
{
auto str_ary_c = dhandy::int_to_ary<uint64_t, 16, Ascii>(swap_long(parHash.part_c));
std::copy(str_ary_c.begin(), str_ary_c.end(), retval.begin() + sizeof(uint64_t) * 2 * 3 - str_ary_c.size() + 1);
}
#if !defined(NDEBUG)
std::ostringstream oss;
if constexpr (Upcase) {
oss << std::uppercase;
}
oss << std::hex << std::setfill('0') << std::setw(2 * sizeof(uint64_t))
<< swap_long(parHash.part_a)
<< std::hex << std::setfill('0') << std::setw(2 * sizeof(uint64_t))
<< swap_long(parHash.part_b)
<< std::hex << std::setfill('0') << std::setw(2 * sizeof(uint64_t))
<< swap_long(parHash.part_c)
;
std::string expected(oss.str());
assert(expected == retval);
#endif
return retval;
}
} //unnamed namespace
void tiger_init_hash (TigerHash& parHash) {
parHash.part_a = 0x0123456789ABCDEFULL;
parHash.part_b = 0xFEDCBA9876543210ULL;
parHash.part_c = 0xF096A5B4C3B2E187ULL;
}
void tiger_file (const std::string& parPath, TigerHash& parHashFile, TigerHash& parHashDir, uint64_t& parSizeOut) {
typedef decltype(std::declval<std::ifstream>().tellg()) FileSizeType;
tiger_init_hash(parHashFile);
std::ifstream src(parPath, std::ios::binary);
src.exceptions(src.badbit); //Throw on read error
src.seekg(0, std::ios_base::end);
const auto file_size = src.tellg();
src.seekg(0, std::ios_base::beg);
const FileSizeType hash_size = (sizeof(TigerHash) + 63) & -64;
const uint32_t buffsize = static_cast<uint32_t>(std::max(hash_size, std::min<FileSizeType>(file_size, g_buff_size)));
std::unique_ptr<char[]> buff(new char[63 + buffsize]);
char* const buff_ptr = reinterpret_cast<char*>((reinterpret_cast<std::intptr_t>(buff.get()) + 63) & (-64));
assert(buff_ptr >= buff.get() and buff_ptr + buffsize <= buff.get() + 63 + buffsize);
//Take a copy of parHashDir and work on it - if hashing fails at some
//point, we need to leave the dir's hash untouched.
auto hash_dir = parHashDir;
//Use the initial value of the dir's hash as if it was part of the data to hash and start
//by processing that value. Hash is reset to the initial value before the call to tiger.
{
std::copy(hash_dir.byte_data, hash_dir.byte_data + sizeof(hash_dir), buff_ptr);
assert(hash_size >= static_cast<FileSizeType>(sizeof(hash_dir)));
std::fill(buff_ptr + sizeof(hash_dir), buff_ptr + hash_size, 0);
TigerHash dummy {};
tiger_init_hash(hash_dir);
#if defined(USE_TIGER_SSE2)
tiger_sse2_chunk(buff_ptr, buff_ptr, hash_size, dummy.data, hash_dir.data);
#else
tiger_2_chunk(buff_ptr, buff_ptr, hash_size, dummy.data, hash_dir.data);
#endif
}
auto remaining = file_size;
while (remaining > buffsize) {
assert(buffsize >= sizeof(uint64_t) * 3);
assert(buffsize == (buffsize & -64));
assert(buffsize % 64 == 0);
remaining -= buffsize;
src.read(buff_ptr, buffsize);
#if defined(USE_TIGER_SSE2)
tiger_sse2_chunk(buff_ptr, buff_ptr, buffsize, parHashFile.data, hash_dir.data);
#else
tiger_2_chunk(buff_ptr, buff_ptr, buffsize, parHashFile.data, hash_dir.data);
#endif
}
{
assert(remaining <= buffsize);
src.read(buff_ptr, remaining);
const auto aligned_size = remaining & -64;
assert(aligned_size <= remaining);
assert(aligned_size <= buffsize);
const char* read_from_buff = buff_ptr;
if (aligned_size) {
#if defined(USE_TIGER_SSE2)
tiger_sse2_chunk(buff_ptr, buff_ptr, aligned_size, parHashFile.data, hash_dir.data);
#else
tiger_2_chunk(buff_ptr, buff_ptr, aligned_size, parHashFile.data, hash_dir.data);
#endif
assert((remaining & 63) == remaining - aligned_size);
remaining -= aligned_size;
read_from_buff += aligned_size;
}
//Remember to pass the augmented data size for the second reallength value: we passed the initial
//dir's hash value (64 bytes) as if they were part of the data.
#if defined(USE_TIGER_SSE2)
tiger_sse2_last_chunk(read_from_buff, read_from_buff, remaining, file_size, file_size + hash_size, parHashFile.data, hash_dir.data, g_tiger_padding);
#else
tiger_2_last_chunk(read_from_buff, read_from_buff, remaining, file_size, file_size + hash_size, parHashFile.data, hash_dir.data, g_tiger_padding);
#endif
}
parSizeOut = static_cast<uint64_t>(file_size);
parHashDir = hash_dir;
}
std::string tiger_to_string (const TigerHash& parHash, bool parUpcase) {
if (parUpcase)
return tiger_to_string_impl<true>(parHash);
else
return tiger_to_string_impl<false>(parHash);
}
TigerHash string_to_tiger (const std::string& parString) {
assert(parString.size() == sizeof(TigerHash) * 2);
TigerHash retval;
const std::string_view inp(parString);
retval.part_a = to_hash_part(inp.substr(0, sizeof(uint64_t) * 2));
retval.part_b = to_hash_part(inp.substr(sizeof(uint64_t) * 2, sizeof(uint64_t) * 2));
retval.part_c = to_hash_part(inp.substr(sizeof(uint64_t) * 4, sizeof(uint64_t) * 2));
return retval;
}
void tiger_data (const std::string& parData, TigerHash& parHash) {
tiger (parData.data(), parData.size(), parHash.data, g_tiger_padding);
}
void tiger_data (const std::vector<char>& parData, TigerHash& parHash) {
tiger (parData.data(), parData.size(), parHash.data, g_tiger_padding);
}
} //namespace mchlib
#if defined(USE_TIGER_SSE2)
# undef USE_TIGER_SSE2
#endif

125
src/oro/tiger.h Normal file
View file

@ -0,0 +1,125 @@
/**
* Copyright (c) 2012 Francisco Blas Izquierdo Riera (klondike)
* The Tiger algorithm was written by Eli Biham and Ross Anderson and is
* available on the official Tiger algorithm page.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* the algorithm authorsip notice, this list of conditions and the following
* disclaimer.
* 2. 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.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
* 4. If this license is not appropriate for you please write me at
* klondike ( a t ) klondike ( d o t ) es to negotiate another license.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 THE AUTHOR 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.
**/
/**
* These are some implementations of tiger made without looking at the original
* reference code to ensure the resulting code can be published under a free
* license. The paper was looked though to know how did tiger work.
*/
/** Implementation details:
* * Here we assume char and unsigned char have size 1. If thats not the case in
* your compiler you may want to replace them by a type that does
*/
#ifndef TIGER_H
#define TIGER_H 1
#if !defined(_MSC_VER) || (_MSC_VER >= 1600)
#include <stdint.h>
#else
typedef __int32 int32_t;
typedef unsigned __int32 uint32_t;
typedef __int64 int64_t;
typedef unsigned __int64 uint64_t;
#endif
#if _M_IX86_FP >= 2
#define __SSE2__
#endif
#ifdef __linux
#include <endian.h>
#if __BYTE_ORDER == __LITTLE_ENDIAN
#define IS_LITTLE_ENDIAN
#elif __BYTE_ORDER == __BIG_ENDIAN
#define USE_BIG_ENDIAN
#elif __BYTE_ORDER == __PDP_ENDIAN
#error "If you feel like writting code for PDP endianess go ahead, I'm not doing that"
#else
#error "Unknown endianess"
#endif
#else
//Assume little endian if you know how to detect endianism well on other compilers state it.
#define IS_LITTLE_ENDIAN
#endif
#if defined(_WIN64) || defined(__x86_64__) || defined(__amd64__)
#define HASX64
#endif
/** A word in the tiger hash, 64 bits **/
typedef uint64_t t_word;
/** This one is provided as a commodity for people wanting an easy way to declare result variables **/
typedef t_word t_res[3];
/** Partial calculation as used by tigerp1 and tigerp2 **/
typedef struct {
t_res h; // Hash status
char r[128]; // SALT
t_word n; // Number of characters of r used
t_word hs; // Amount of total data hashed
} t_pres;
/** This one is provided as a commodity for people wanting an easy way to declare block variables **/
typedef t_word t_block[8];
/** Standard tiger calculation, put your string in str and the string length on length and get the result on res **/
void tiger(const char *str, t_word length, t_res res, char pad);
/** Similar to tiger but interleaving accesses to both equally sized strings to reduce overhead and pipeline stalls you get the result of str1 on res1 and the one of str2 on res2 **/
void tiger_2(const char *str1, const char *str2, t_word length, t_res res1, t_res res2, char pad);
#ifdef __SSE2__
/** This is equivalent to tiger_2 but uses SSE2 for the key schduling making it faster **/
void tiger_sse2(const char *str1, const char *str2, t_word length, t_res res1, t_res res2, char pad);
#endif
/** This function is optimized for use on TTHs just send the two concatenated hashes and you will get back the hash with a prepended 0x01 **/
void tiger_49(const char *str, t_res res);
/** This function is optimized for use on TTHs just send the 1024 sized block and you will get back the hash with a prepended 0x00 **/
void tiger_1025(const char *str, t_res res);
/** Interleaved version of tiger_49 you insert two hashes and get back two results **/
void tiger_2_49(const char *str1, const char *str2, t_res res1, t_res res2);
/** Interleaved version of tiger_1025 you insert two hashes and get back two results **/
void tiger_2_1025(const char *str1, const char *str2, t_res res1, t_res res2);
#ifdef __SSE2__
/** SSE2 version of tiger_49 you insert two hashes and get back two results **/
void tiger_sse2_49(const char *str1, const char *str2, t_res res1, t_res res2);
/** SSE2 version of tiger_1025 you insert two hashes and get back two results **/
void tiger_sse2_1025(const char *str1, const char *str2, t_res res1, t_res res2);
#endif
/** First stage of partial tiger calculation to improve password security during storage **/
void tigerp1(const char *password, t_word length, const char *salt, t_pres *pres);
/** Second stage of partial tiger calculation **/
void tigerp2(const t_pres *pres, const char *salt, t_word length, t_res res);
#endif

53
src/oro/tiger.hpp Normal file
View file

@ -0,0 +1,53 @@
/* Copyright 2015, 2016, Michele Santullo
* This file is part of "dindexer".
*
* "dindexer" is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* "dindexer" 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with "dindexer". If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef idBE93AF97FA4343ECA2BC8FB1FD3E5E60
#define idBE93AF97FA4343ECA2BC8FB1FD3E5E60
#include <cstdint>
#include <string>
#include <vector>
#include <ciso646>
namespace mchlib {
struct TigerHash {
TigerHash ( void ) = default;
union {
struct {
uint64_t part_a;
uint64_t part_b;
uint64_t part_c;
};
uint64_t data[3];
uint8_t byte_data[sizeof(uint64_t) * 3];
};
bool operator== (const TigerHash& parOther) const { return part_a == parOther.part_a and part_b == parOther.part_b and part_c == parOther.part_c; }
};
static_assert(sizeof(TigerHash) == 24, "Wrong struct size");
void tiger_file ( const std::string& parPath, TigerHash& parHashFile, TigerHash& parHashDir, uint64_t& parSizeOut );
void tiger_init_hash ( TigerHash& parHash );
std::string tiger_to_string ( const TigerHash& parHash, bool parUpcase=false );
TigerHash string_to_tiger ( const std::string& parString );
void tiger_data ( const std::string& parData, TigerHash& parHash );
void tiger_data ( const std::vector<char>& parData, TigerHash& parHash );
} //namespace mchlib
#endif

1
subprojects/duckhandy Submodule

@ -0,0 +1 @@
Subproject commit 1c6de14cd466ea067d5543de635a64ec0739b78e