diff --git a/C/hashes/README.md b/C/hashes/README.md deleted file mode 100644 index 7d85680c..00000000 --- a/C/hashes/README.md +++ /dev/null @@ -1,4 +0,0 @@ - -Taken from lib/crypto/* of samba-4.5.8.tar.gz. - -/TR 2018-11-15 diff --git a/C/hashes/md2.c b/C/hashes/md2.c index 3bc1ab24..c125f4ee 100644 --- a/C/hashes/md2.c +++ b/C/hashes/md2.c @@ -1,5 +1,7 @@ /* + * Taken from lib/crypto/* of samba-4.5.8.tar.gz. + * * Copyright (c) 2006 Kungliga Tekniska Högskolan * (Royal Institute of Technology, Stockholm, Sweden). * All rights reserved. diff --git a/C/hashes/md2.h b/C/hashes/md2.h index 1985e98d..c1a415e5 100644 --- a/C/hashes/md2.h +++ b/C/hashes/md2.h @@ -1,5 +1,7 @@ /* + * Taken from lib/crypto/* of samba-4.5.8.tar.gz. + * * Copyright (c) 2006 Kungliga Tekniska Högskolan * (Royal Institute of Technology, Stockholm, Sweden). * All rights reserved. diff --git a/C/hashes/md4.c b/C/hashes/md4.c index e61ffb2b..469b41bf 100644 --- a/C/hashes/md4.c +++ b/C/hashes/md4.c @@ -1,5 +1,7 @@ /* + * Taken from lib/crypto/* of samba-4.5.8.tar.gz. + * * Copyright (c) 1995 - 2001 Kungliga Tekniska Högskolan * (Royal Institute of Technology, Stockholm, Sweden). * All rights reserved. diff --git a/C/hashes/md4.h b/C/hashes/md4.h index 30c6bfc4..af467fa7 100644 --- a/C/hashes/md4.h +++ b/C/hashes/md4.h @@ -1,5 +1,7 @@ /* + * Taken from lib/crypto/* of samba-4.5.8.tar.gz. + * * Copyright (c) 1995 - 2001 Kungliga Tekniska Högskolan * (Royal Institute of Technology, Stockholm, Sweden). * All rights reserved. diff --git a/C/hashes/md5.c b/C/hashes/md5.c index 82fe8a1b..b9613f57 100644 --- a/C/hashes/md5.c +++ b/C/hashes/md5.c @@ -1,5 +1,7 @@ /* + * Taken from lib/crypto/* of samba-4.5.8.tar.gz. + * * Copyright (c) 1995 - 2001 Kungliga Tekniska Högskolan * (Royal Institute of Technology, Stockholm, Sweden). * All rights reserved. diff --git a/C/hashes/md5.h b/C/hashes/md5.h index 3918edd5..2c83a317 100644 --- a/C/hashes/md5.h +++ b/C/hashes/md5.h @@ -1,5 +1,7 @@ /* + * Taken from lib/crypto/* of samba-4.5.8.tar.gz. + * * Copyright (c) 1995 - 2001 Kungliga Tekniska Högskolan * (Royal Institute of Technology, Stockholm, Sweden). * All rights reserved. diff --git a/C/hashes/sha3.c b/C/hashes/sha3.c new file mode 100644 index 00000000..4fada174 --- /dev/null +++ b/C/hashes/sha3.c @@ -0,0 +1,239 @@ + +/** + * Canonical implementation of Init/Update/Finalize for SHA-3 byte input. + * Based on code from https://github.com/brainhub/SHA3IUF/ + * + * This work is released into the public domain with CC0 1.0. + * + * Copyright (c) 2015. Andrey Jivsov + * Copyright (c) 2023 Tino Reichardt + */ + +#include +#include +#include + +#include "sha3.h" + +#define SHA3_ASSERT( x ) + +#if defined(_MSC_VER) +#define SHA3_CONST(x) x +#else +#define SHA3_CONST(x) x##L +#endif + +/* + * This flag is used to configure "pure" Keccak, as opposed to NIST SHA3. + */ +#define SHA3_USE_KECCAK_FLAG 0x80000000 +#define SHA3_CW(x) ((x) & (~SHA3_USE_KECCAK_FLAG)) + +#ifndef SHA3_ROTL64 +#define SHA3_ROTL64(x, y) (((x) << (y)) | ((x) >> (64 - (y)))) +#endif + +static const uint64_t keccakf_rndc[24] = { + SHA3_CONST(0x0000000000000001UL), SHA3_CONST(0x0000000000008082UL), + SHA3_CONST(0x800000000000808aUL), SHA3_CONST(0x8000000080008000UL), + SHA3_CONST(0x000000000000808bUL), SHA3_CONST(0x0000000080000001UL), + SHA3_CONST(0x8000000080008081UL), SHA3_CONST(0x8000000000008009UL), + SHA3_CONST(0x000000000000008aUL), SHA3_CONST(0x0000000000000088UL), + SHA3_CONST(0x0000000080008009UL), SHA3_CONST(0x000000008000000aUL), + SHA3_CONST(0x000000008000808bUL), SHA3_CONST(0x800000000000008bUL), + SHA3_CONST(0x8000000000008089UL), SHA3_CONST(0x8000000000008003UL), + SHA3_CONST(0x8000000000008002UL), SHA3_CONST(0x8000000000000080UL), + SHA3_CONST(0x000000000000800aUL), SHA3_CONST(0x800000008000000aUL), + SHA3_CONST(0x8000000080008081UL), SHA3_CONST(0x8000000000008080UL), + SHA3_CONST(0x0000000080000001UL), SHA3_CONST(0x8000000080008008UL) +}; + +static const unsigned keccakf_rotc[24] = { + 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 2, 14, 27, 41, 56, 8, 25, 43, 62, + 18, 39, 61, 20, 44 +}; + +static const unsigned keccakf_piln[24] = { + 10, 7, 11, 17, 18, 3, 5, 16, 8, 21, 24, 4, 15, 23, 19, 13, 12, 2, 20, + 14, 22, 9, 6, 1 +}; + +/* generally called after SHA3_KECCAK_SPONGE_WORDS-ctx->capacityWords words + * are XORed into the state s + */ +static void keccakf(uint64_t s[25]) +{ + int i, j, round; + uint64_t t, bc[5]; +#define KECCAK_ROUNDS 24 + + for (round = 0; round < KECCAK_ROUNDS; round++) { + + /* Theta */ + for (i = 0; i < 5; i++) + bc[i] = + s[i] ^ s[i + 5] ^ s[i + 10] ^ s[i + 15] ^ s[i + 20]; + + for (i = 0; i < 5; i++) { + t = bc[(i + 4) % 5] ^ SHA3_ROTL64(bc[(i + 1) % 5], 1); + for (j = 0; j < 25; j += 5) + s[j + i] ^= t; + } + + /* Rho Pi */ + t = s[1]; + for (i = 0; i < 24; i++) { + j = keccakf_piln[i]; + bc[0] = s[j]; + s[j] = SHA3_ROTL64(t, keccakf_rotc[i]); + t = bc[0]; + } + + /* Chi */ + for (j = 0; j < 25; j += 5) { + for (i = 0; i < 5; i++) + bc[i] = s[j + i]; + for (i = 0; i < 5; i++) + s[j + i] ^= + (~bc[(i + 1) % 5]) & bc[(i + 2) % 5]; + } + + /* Iota */ + s[0] ^= keccakf_rndc[round]; + } +} + +/* *************************** Public Inteface ************************ */ + +void SHA3_Init(SHA3_CTX * ctx, unsigned bitSize) +{ + memset(ctx, 0, sizeof(*ctx)); + ctx->digest_length = bitSize; + ctx->capacityWords = 2 * bitSize / (8 * sizeof(uint64_t)); +} + +void SHA3_Update(SHA3_CTX * ctx, void const *bufIn, size_t len) +{ + /* 0...7 -- how much is needed to have a word */ + unsigned old_tail = (8 - ctx->byteIndex) & 7; + + size_t words; + size_t tail; + size_t i; + + const uint8_t *buf = bufIn; + + SHA3_ASSERT(ctx->byteIndex < 8); + SHA3_ASSERT(ctx->wordIndex < sizeof(ctx->u.s) / sizeof(ctx->u.s[0])); + + if (len < old_tail) { /* have no complete word or haven't started + * the word yet */ + /* endian-independent code follows: */ + while (len--) + ctx->saved |= + (uint64_t) (*(buf++)) << ((ctx->byteIndex++) * 8); + SHA3_ASSERT(ctx->byteIndex < 8); + return; + } + + if (old_tail) { /* will have one word to process */ + /* endian-independent code follows: */ + len -= old_tail; + while (old_tail--) + ctx->saved |= + (uint64_t) (*(buf++)) << ((ctx->byteIndex++) * 8); + + /* now ready to add saved to the sponge */ + ctx->u.s[ctx->wordIndex] ^= ctx->saved; + SHA3_ASSERT(ctx->byteIndex == 8); + ctx->byteIndex = 0; + ctx->saved = 0; + if (++ctx->wordIndex == + (SHA3_KECCAK_SPONGE_WORDS - SHA3_CW(ctx->capacityWords))) { + keccakf(ctx->u.s); + ctx->wordIndex = 0; + } + } + + /* now work in full words directly from input */ + + SHA3_ASSERT(ctx->byteIndex == 0); + + words = len / sizeof(uint64_t); + tail = len - words * sizeof(uint64_t); + + for (i = 0; i < words; i++, buf += sizeof(uint64_t)) { + const uint64_t t = (uint64_t) (buf[0]) | + ((uint64_t) (buf[1]) << 8 * 1) | + ((uint64_t) (buf[2]) << 8 * 2) | + ((uint64_t) (buf[3]) << 8 * 3) | + ((uint64_t) (buf[4]) << 8 * 4) | + ((uint64_t) (buf[5]) << 8 * 5) | + ((uint64_t) (buf[6]) << 8 * 6) | + ((uint64_t) (buf[7]) << 8 * 7); +#if defined(__x86_64__ ) || defined(__i386__) + SHA3_ASSERT(memcmp(&t, buf, 8) == 0); +#endif + ctx->u.s[ctx->wordIndex] ^= t; + if (++ctx->wordIndex == + (SHA3_KECCAK_SPONGE_WORDS - SHA3_CW(ctx->capacityWords))) { + keccakf(ctx->u.s); + ctx->wordIndex = 0; + } + } + + /* finally, save the partial word */ + SHA3_ASSERT(ctx->byteIndex == 0 && tail < 8); + while (tail--) { + ctx->saved |= (uint64_t) (*(buf++)) << ((ctx->byteIndex++) * 8); + } + SHA3_ASSERT(ctx->byteIndex < 8); +} + +/* This is simply the 'update' with the padding block. + * The padding block is 0x01 || 0x00* || 0x80. First 0x01 and last 0x80 + * bytes are always present, but they can be the same byte. + */ +void SHA3_Final(void *res, SHA3_CTX * ctx) +{ + /* Append 2-bit suffix 01, per SHA-3 spec. Instead of 1 for padding we + * use 1<<2 below. The 0x02 below corresponds to the suffix 01. + * Overall, we feed 0, then 1, and finally 1 to start padding. Without + * M || 01, we would simply use 1 to start padding. */ + + uint64_t t; + + /* SHA3 version */ + t = (uint64_t) (((uint64_t) (0x02 | (1 << 2))) << + ((ctx->byteIndex) * 8)); + + ctx->u.s[ctx->wordIndex] ^= ctx->saved ^ t; + + ctx->u.s[SHA3_KECCAK_SPONGE_WORDS - SHA3_CW(ctx->capacityWords) - 1] ^= + SHA3_CONST(0x8000000000000000UL); + keccakf(ctx->u.s); + + /* Return first bytes of the ctx->s. This conversion is not needed for + * little-endian platforms e.g. wrap with #if !defined(__BYTE_ORDER__) + * || !defined(__ORDER_LITTLE_ENDIAN__) || __BYTE_ORDER__!=__ORDER_LITTLE_ENDIAN__ + * ... the conversion below ... + * #endif */ + { + unsigned i; + for (i = 0; i < SHA3_KECCAK_SPONGE_WORDS; i++) { + const unsigned t1 = (uint32_t) ctx->u.s[i]; + const unsigned t2 = + (uint32_t) ((ctx->u.s[i] >> 16) >> 16); + ctx->u.sb[i * 8 + 0] = (uint8_t) (t1); + ctx->u.sb[i * 8 + 1] = (uint8_t) (t1 >> 8); + ctx->u.sb[i * 8 + 2] = (uint8_t) (t1 >> 16); + ctx->u.sb[i * 8 + 3] = (uint8_t) (t1 >> 24); + ctx->u.sb[i * 8 + 4] = (uint8_t) (t2); + ctx->u.sb[i * 8 + 5] = (uint8_t) (t2 >> 8); + ctx->u.sb[i * 8 + 6] = (uint8_t) (t2 >> 16); + ctx->u.sb[i * 8 + 7] = (uint8_t) (t2 >> 24); + } + } + + memcpy(res, ctx->u.sb, ctx->digest_length/8); +} diff --git a/C/hashes/sha3.h b/C/hashes/sha3.h new file mode 100644 index 00000000..688716ab --- /dev/null +++ b/C/hashes/sha3.h @@ -0,0 +1,43 @@ + +/** + * Canonical implementation of Init/Update/Finalize for SHA-3 byte input. + * Based on code from https://github.com/brainhub/SHA3IUF/ + * + * This work is released into the public domain with CC0 1.0. + * + * Copyright (c) 2015. Andrey Jivsov + * Copyright (c) 2023 Tino Reichardt + */ + +#ifndef SHA3_H +#define SHA3_H + +#include + +#define SHA3_256_DIGEST_LENGTH 32 +#define SHA3_384_DIGEST_LENGTH 48 +#define SHA3_512_DIGEST_LENGTH 64 + +/* 'Words' here refers to uint64_t */ +#define SHA3_KECCAK_SPONGE_WORDS (200 / sizeof(uint64_t)) +typedef struct sha3_context_ { + /* the portion of the input message that we didn't consume yet */ + uint64_t saved; + uint64_t byteIndex; /* 0..7--the next byte after the set one */ + uint64_t wordIndex; /* 0..24--the next word to integrate input */ + uint64_t capacityWords; /* the double size of the hash output in words */ + + /* Keccak's state */ + union { + uint64_t s[SHA3_KECCAK_SPONGE_WORDS]; + uint8_t sb[SHA3_KECCAK_SPONGE_WORDS * 8]; + } u; + + unsigned digest_length; +} SHA3_CTX; + +void SHA3_Init(SHA3_CTX * ctx, unsigned bitSize); +void SHA3_Update(SHA3_CTX * ctx, void const *bufIn, size_t len); +void SHA3_Final(void *, SHA3_CTX * ctx); + +#endif diff --git a/C/hashes/sha512.c b/C/hashes/sha512.c index 23236dbc..f1382deb 100644 --- a/C/hashes/sha512.c +++ b/C/hashes/sha512.c @@ -1,5 +1,7 @@ /* + * Taken from lib/crypto/* of samba-4.5.8.tar.gz. + * * Copyright (c) 2006, 2010 Kungliga Tekniska Högskolan * (Royal Institute of Technology, Stockholm, Sweden). * All rights reserved. @@ -32,8 +34,7 @@ * SUCH DAMAGE. */ -#include "hash.h" -#include "sha.h" +#include "sha512.h" #define Ch(x,y,z) (((x) & (y)) ^ ((~(x)) & (z))) #define Maj(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z))) diff --git a/C/hashes/sha.h b/C/hashes/sha512.h similarity index 96% rename from C/hashes/sha.h rename to C/hashes/sha512.h index 537c2ba3..acdb8dcb 100644 --- a/C/hashes/sha.h +++ b/C/hashes/sha512.h @@ -1,5 +1,7 @@ /* + * Taken from lib/crypto/* of samba-4.5.8.tar.gz. + * * Copyright (c) 1995 - 2001 Kungliga Tekniska Högskolan * (Royal Institute of Technology, Stockholm, Sweden). * All rights reserved. @@ -57,6 +59,10 @@ void SHA512_Init (SHA512_CTX *); void SHA512_Update (SHA512_CTX *, const void *, size_t); void SHA512_Final (void *, SHA512_CTX *); +/* + * SHA-2 384 + */ + #define SHA384_DIGEST_LENGTH 48 typedef struct hc_sha512state SHA384_CTX; diff --git a/CPP/7zip/Bundles/Format7zF/Arc.mak b/CPP/7zip/Bundles/Format7zF/Arc.mak index d63e8652..f50966aa 100644 --- a/CPP/7zip/Bundles/Format7zF/Arc.mak +++ b/CPP/7zip/Bundles/Format7zF/Arc.mak @@ -17,6 +17,9 @@ COMMON_OBJS = \ $O\Sha256Reg.obj \ $O\Sha384Reg.obj \ $O\Sha512Reg.obj \ + $O\Sha3-256Reg.obj \ + $O\Sha3-384Reg.obj \ + $O\Sha3-512Reg.obj \ $O\StringConvert.obj \ $O\StringToInt.obj \ $O\UTFConvert.obj \ @@ -269,6 +272,7 @@ HASHES_OBJS = \ $O\md2.obj \ $O\md4.obj \ $O\md5.obj \ + $O\sha3.obj \ $O\sha512.obj \ C_OBJS = \ diff --git a/CPP/7zip/UI/Common/HashCalc.cpp b/CPP/7zip/UI/Common/HashCalc.cpp index 161ebe5a..5c8ccddc 100644 --- a/CPP/7zip/UI/Common/HashCalc.cpp +++ b/CPP/7zip/UI/Common/HashCalc.cpp @@ -807,18 +807,10 @@ static const char *SkipWhite(const char *s) static const char * const k_CsumMethodNames[] = { - "sha256" - , "sha224" -// , "sha512/224" -// , "sha512/256" - , "sha512" - , "sha384" - , "sha1" - , "md5" - , "blake2b" - , "crc64" - , "crc32" - , "cksum" + "sha3-512", "sha3-384", "sha3-256", + "sha512", "sha384", "sha256", "sha224", + "sha1", "md5", "blake2b", + "crc32", "crc64" , "cksum" }; static UString GetMethod_from_FileName(const UString &name) @@ -1421,7 +1413,8 @@ static bool CheckDigests(const Byte *a, const Byte *b, size_t size) static void AddDefaultMethod(UStringVector &methods, unsigned size) { const char *m = NULL; - if (size == 32) m = "sha256"; + if (size == 64) m = "sha512"; + else if (size == 32) m = "sha256"; else if (size == 20) m = "sha1"; else if (size == 16) m = "md5"; else if (size == 8) m = "crc64"; @@ -2098,11 +2091,11 @@ void Codecs_AddHashArcHandler(CCodecs *codecs) // ubuntu uses "SHA256SUMS" file item.AddExts(UString ( - "sha256 sha512 sha224 sha384 sha1 sha md5" - // "b2sum" - " crc32 crc64" - " asc" - " cksum" + "sha3-512 sha3-384 sha3-256 " + "sha512 sha384 sha256 sha224 " + "sha1 sha md5 " + "crc32 crc64 " + "asc cksum" ), UString()); diff --git a/CPP/7zip/UI/Explorer/ContextMenu.cpp b/CPP/7zip/UI/Explorer/ContextMenu.cpp index 153b2ebc..98d4bca5 100644 --- a/CPP/7zip/UI/Explorer/ContextMenu.cpp +++ b/CPP/7zip/UI/Explorer/ContextMenu.cpp @@ -242,13 +242,16 @@ static const CHashCommand g_HashCommands[] = { CZipContextMenu::kHash_MD4, "MD4", "MD4" }, { CZipContextMenu::kHash_MD5, "MD5", "MD5" }, { CZipContextMenu::kHash_SHA1, "SHA-1", "SHA1" }, - { CZipContextMenu::kHash_SHA256, "SHA-256", "SHA256" }, - { CZipContextMenu::kHash_SHA384, "SHA-384", "SHA384" }, - { CZipContextMenu::kHash_SHA512, "SHA-512", "SHA512" }, + { CZipContextMenu::kHash_SHA256, "SHA2-256", "SHA256" }, + { CZipContextMenu::kHash_SHA384, "SHA2-384", "SHA384" }, + { CZipContextMenu::kHash_SHA512, "SHA2-512", "SHA512" }, { CZipContextMenu::kHash_BLAKE2sp, "BLAKE2sp", "BLAKE2sp" }, { CZipContextMenu::kHash_BLAKE3, "BLAKE3", "BLAKE3" }, + { CZipContextMenu::kHash_SHA256, "SHA3-256", "SHA3-256" }, + { CZipContextMenu::kHash_SHA384, "SHA3-384", "SHA3-384" }, + { CZipContextMenu::kHash_SHA512, "SHA3-512", "SHA3-512" }, { CZipContextMenu::kHash_All, "*", "*" }, - { CZipContextMenu::kHash_Generate_SHA256, "SHA-256 -> file.sha256", "SHA256" }, + { CZipContextMenu::kHash_Generate_SHA256, "SHA2-256 -> file.sha256", "SHA256" }, { CZipContextMenu::kHash_TestArc, "Checksum : Test", "Hash" } }; @@ -966,7 +969,7 @@ STDMETHODIMP CZipContextMenu::QueryContextMenu(HMENU hMenu, UINT indexMenu, name += ".sha256"; cmi.Folder = fs2us(folderPrefix); cmi.ArcName = name; - s = "SHA-256 -> "; + s = "SHA2-256 -> "; s += name; } else if (hc.CommandInternalID == kHash_TestArc) @@ -1231,6 +1234,9 @@ HRESULT CZipContextMenu::InvokeCommandCommon(const CCommandMapItem &cmi) case kHash_SHA512: case kHash_BLAKE2sp: case kHash_BLAKE3: + case kHash_SHA3_256: + case kHash_SHA3_384: + case kHash_SHA3_512: case kHash_All: case kHash_Generate_SHA256: case kHash_TestArc: diff --git a/CPP/7zip/UI/Explorer/ContextMenu.h b/CPP/7zip/UI/Explorer/ContextMenu.h index 261ec72b..b45fcedf 100644 --- a/CPP/7zip/UI/Explorer/ContextMenu.h +++ b/CPP/7zip/UI/Explorer/ContextMenu.h @@ -59,6 +59,9 @@ class CZipContextMenu: kHash_SHA512, kHash_BLAKE2sp, kHash_BLAKE3, + kHash_SHA3_256, + kHash_SHA3_384, + kHash_SHA3_512, kHash_All, kHash_Generate_SHA256, kHash_TestArc diff --git a/CPP/Common/Blake3Reg.cpp b/CPP/Common/Blake3Reg.cpp index 755f0058..1a488d2c 100644 --- a/CPP/Common/Blake3Reg.cpp +++ b/CPP/Common/Blake3Reg.cpp @@ -40,4 +40,4 @@ STDMETHODIMP_(void) CBLAKE3Hasher::Final(Byte *digest) throw() { blake3_hasher_finalize(&_ctx, digest, BLAKE3_OUT_LEN); } -REGISTER_HASHER(CBLAKE3Hasher, 0x20a, "BLAKE3", BLAKE3_OUT_LEN) +REGISTER_HASHER(CBLAKE3Hasher, 0x204, "BLAKE3", BLAKE3_OUT_LEN) diff --git a/CPP/Common/CksumReg.cpp b/CPP/Common/CksumReg.cpp index 29d9f82e..4cd79a7a 100644 --- a/CPP/Common/CksumReg.cpp +++ b/CPP/Common/CksumReg.cpp @@ -57,4 +57,4 @@ STDMETHODIMP_(void) CCksumHasher::Final(Byte *digest) throw() SetUi32(digest, val); } -REGISTER_HASHER(CCksumHasher, 0x203, "CKSUM", 4) +REGISTER_HASHER(CCksumHasher, 0x202, "CKSUM", 4) diff --git a/CPP/Common/Sha3-256Reg.cpp b/CPP/Common/Sha3-256Reg.cpp new file mode 100644 index 00000000..07bffb31 --- /dev/null +++ b/CPP/Common/Sha3-256Reg.cpp @@ -0,0 +1,43 @@ +// Sha3-256Reg.cpp /TR 2023-06-18 + +#include "StdAfx.h" + +#include "../../C/CpuArch.h" + +EXTERN_C_BEGIN +#include "../../C/hashes/sha3.h" +EXTERN_C_END + +#include "../Common/MyCom.h" +#include "../7zip/Common/RegisterCodec.h" + +// SHA3-256 +class CSHA3_256Hasher: + public IHasher, + public CMyUnknownImp +{ + SHA3_CTX _ctx; + Byte mtDummy[1 << 7]; + +public: + CSHA3_256Hasher() { SHA3_Init(&_ctx, 256); } + + MY_UNKNOWN_IMP1(IHasher) + INTERFACE_IHasher(;) +}; + +STDMETHODIMP_(void) CSHA3_256Hasher::Init() throw() +{ + SHA3_Init(&_ctx, 256); +} + +STDMETHODIMP_(void) CSHA3_256Hasher::Update(const void *data, UInt32 size) throw() +{ + SHA3_Update(&_ctx, (const Byte *)data, size); +} + +STDMETHODIMP_(void) CSHA3_256Hasher::Final(Byte *digest) throw() +{ + SHA3_Final(digest, &_ctx); +} +REGISTER_HASHER(CSHA3_256Hasher, 0x20a, "SHA3-256", SHA3_256_DIGEST_LENGTH) diff --git a/CPP/Common/Sha3-384Reg.cpp b/CPP/Common/Sha3-384Reg.cpp new file mode 100644 index 00000000..3cdd3991 --- /dev/null +++ b/CPP/Common/Sha3-384Reg.cpp @@ -0,0 +1,43 @@ +// Sha3-384Reg.cpp /TR 2023-06-18 + +#include "StdAfx.h" + +#include "../../C/CpuArch.h" + +EXTERN_C_BEGIN +#include "../../C/hashes/sha3.h" +EXTERN_C_END + +#include "../Common/MyCom.h" +#include "../7zip/Common/RegisterCodec.h" + +// SHA3-384 +class CSHA3_384Hasher: + public IHasher, + public CMyUnknownImp +{ + SHA3_CTX _ctx; + Byte mtDummy[1 << 7]; + +public: + CSHA3_384Hasher() { SHA3_Init(&_ctx, 384); } + + MY_UNKNOWN_IMP1(IHasher) + INTERFACE_IHasher(;) +}; + +STDMETHODIMP_(void) CSHA3_384Hasher::Init() throw() +{ + SHA3_Init(&_ctx, 384); +} + +STDMETHODIMP_(void) CSHA3_384Hasher::Update(const void *data, UInt32 size) throw() +{ + SHA3_Update(&_ctx, (const Byte *)data, size); +} + +STDMETHODIMP_(void) CSHA3_384Hasher::Final(Byte *digest) throw() +{ + SHA3_Final(digest, &_ctx); +} +REGISTER_HASHER(CSHA3_384Hasher, 0x20b, "SHA3-384", SHA3_384_DIGEST_LENGTH) diff --git a/CPP/Common/Sha3-512Reg.cpp b/CPP/Common/Sha3-512Reg.cpp new file mode 100644 index 00000000..bc7894a8 --- /dev/null +++ b/CPP/Common/Sha3-512Reg.cpp @@ -0,0 +1,43 @@ +// Sha3-512Reg.cpp /TR 2023-06-18 + +#include "StdAfx.h" + +#include "../../C/CpuArch.h" + +EXTERN_C_BEGIN +#include "../../C/hashes/sha3.h" +EXTERN_C_END + +#include "../Common/MyCom.h" +#include "../7zip/Common/RegisterCodec.h" + +// SHA3-512 +class CSHA3_512Hasher: + public IHasher, + public CMyUnknownImp +{ + SHA3_CTX _ctx; + Byte mtDummy[1 << 7]; + +public: + CSHA3_512Hasher() { SHA3_Init(&_ctx, 512); } + + MY_UNKNOWN_IMP1(IHasher) + INTERFACE_IHasher(;) +}; + +STDMETHODIMP_(void) CSHA3_512Hasher::Init() throw() +{ + SHA3_Init(&_ctx, 512); +} + +STDMETHODIMP_(void) CSHA3_512Hasher::Update(const void *data, UInt32 size) throw() +{ + SHA3_Update(&_ctx, (const Byte *)data, size); +} + +STDMETHODIMP_(void) CSHA3_512Hasher::Final(Byte *digest) throw() +{ + SHA3_Final(digest, &_ctx); +} +REGISTER_HASHER(CSHA3_512Hasher, 0x20c, "SHA3-512", SHA3_512_DIGEST_LENGTH) diff --git a/CPP/Common/Sha384Reg.cpp b/CPP/Common/Sha384Reg.cpp index 82592baa..aa162cc6 100644 --- a/CPP/Common/Sha384Reg.cpp +++ b/CPP/Common/Sha384Reg.cpp @@ -5,7 +5,7 @@ #include "../../C/CpuArch.h" EXTERN_C_BEGIN -#include "../../C/hashes/sha.h" +#include "../../C/hashes/sha512.h" EXTERN_C_END #include "../Common/MyCom.h" diff --git a/CPP/Common/Sha512Reg.cpp b/CPP/Common/Sha512Reg.cpp index 5c6d71d7..a9e8d424 100644 --- a/CPP/Common/Sha512Reg.cpp +++ b/CPP/Common/Sha512Reg.cpp @@ -5,7 +5,7 @@ #include "../../C/CpuArch.h" EXTERN_C_BEGIN -#include "../../C/hashes/sha.h" +#include "../../C/hashes/sha512.h" EXTERN_C_END #include "../Common/MyCom.h" diff --git a/CPP/Common/XXH32Reg.cpp b/CPP/Common/XXH32Reg.cpp index 59a2de8e..8c3dd358 100644 --- a/CPP/Common/XXH32Reg.cpp +++ b/CPP/Common/XXH32Reg.cpp @@ -42,4 +42,4 @@ STDMETHODIMP_(void) CXXH32Hasher::Final(Byte *digest) throw() SetUi32(digest, val); } -REGISTER_HASHER(CXXH32Hasher, 0x203, "XXH32", 4) +REGISTER_HASHER(CXXH32Hasher, 0x20d, "XXH32", 4) diff --git a/CPP/Common/XXH64Reg.cpp b/CPP/Common/XXH64Reg.cpp index a0d7bf04..f8afa709 100644 --- a/CPP/Common/XXH64Reg.cpp +++ b/CPP/Common/XXH64Reg.cpp @@ -41,4 +41,4 @@ STDMETHODIMP_(void) CXXH64Hasher::Final(Byte *digest) throw() UInt64 val = XXH64_digest(_ctx); SetUi64(digest, val); } -REGISTER_HASHER(CXXH64Hasher, 0x204, "XXH64", 8) +REGISTER_HASHER(CXXH64Hasher, 0x20e, "XXH64", 8) diff --git a/DOC/Hashes.txt b/DOC/Hashes.txt new file mode 100644 index 00000000..36395952 --- /dev/null +++ b/DOC/Hashes.txt @@ -0,0 +1,29 @@ + +7-Zip Standard +REGISTER_HASHER(CCrcHasher, 0x1, "CRC32", 4) +REGISTER_HASHER(CXzCrc64Hasher, 0x4, "CRC64", 8) +REGISTER_HASHER(CSha256Hasher, 0xA, "SHA256", SHA256_DIGEST_SIZE) +REGISTER_HASHER(CSha1Hasher, 0x201, "SHA1", SHA1_DIGEST_SIZE) +REGISTER_HASHER(CBlake2spHasher, 0x202, "BLAKE2sp", BLAKE2S_DIGEST_SIZE) +REGISTER_HASHER(CCksumHasher, 0x203, "CKSUM", 4) + + +7-Zip ZS +REGISTER_HASHER(CCrcHasher, 0x1, "CRC32", 4) +REGISTER_HASHER(CXzCrc64Hasher, 0x4, "CRC64", 8) +REGISTER_HASHER(CSha256Hasher, 0xA, "SHA256", SHA256_DIGEST_SIZE) +REGISTER_HASHER(CSha1Hasher, 0x201, "SHA1", SHA1_DIGEST_SIZE) +REGISTER_HASHER(CBlake2spHasher, 0x202, "BLAKE2sp", BLAKE2S_DIGEST_SIZE) +REGISTER_HASHER(CCksumHasher, 0x203, "CKSUM", 4) + +REGISTER_HASHER(CBLAKE3Hasher, 0x204, "BLAKE3", BLAKE3_OUT_LEN) +REGISTER_HASHER(CMD2Hasher, 0x205, "MD2", MD2_DIGEST_LENGTH) +REGISTER_HASHER(CMD4Hasher, 0x206, "MD4", MD4_DIGEST_LENGTH) +REGISTER_HASHER(CMD5Hasher, 0x207, "MD5", MD5_DIGEST_LENGTH) +REGISTER_HASHER(CSHA384Hasher, 0x208, "SHA384", SHA384_DIGEST_LENGTH) +REGISTER_HASHER(CSHA512Hasher, 0x209, "SHA512", SHA512_DIGEST_LENGTH) +REGISTER_HASHER(CSHA3_256Hasher, 0x20a, "SHA3-256", SHA3_256_DIGEST_LENGTH) +REGISTER_HASHER(CSHA3_384Hasher, 0x20b, "SHA3-384", SHA3_384_DIGEST_LENGTH) +REGISTER_HASHER(CSHA3_512Hasher, 0x20c, "SHA3-512", SHA3_512_DIGEST_LENGTH) +REGISTER_HASHER(CXXH32Hasher, 0x20d, "XXH32", 4) +REGISTER_HASHER(CXXH64Hasher, 0x20e, "XXH64", 8)