From f4239191646ae0e49d4984a083d31110e9bc42c1 Mon Sep 17 00:00:00 2001 From: George Hazan Date: Fri, 8 Apr 2016 08:17:43 +0000 Subject: mir_hmac_sha256 added git-svn-id: http://svn.miranda-ng.org/main/trunk@16609 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- bin10/lib/mir_core.lib | Bin 301152 -> 301378 bytes bin10/lib/mir_core64.lib | Bin 302190 -> 302392 bytes bin12/lib/mir_core.lib | Bin 301152 -> 301378 bytes bin12/lib/mir_core64.lib | Bin 302190 -> 302392 bytes bin14/lib/mir_core.lib | Bin 301152 -> 301378 bytes bin14/lib/mir_core64.lib | Bin 302190 -> 302392 bytes include/m_core.h | 9 ++++++--- src/mir_core/src/mir_core.def | 1 + src/mir_core/src/mir_core64.def | 1 + src/mir_core/src/sha1.cpp | 35 +++++++++++++---------------------- src/mir_core/src/sha256.cpp | 31 +++++++++++++++++++++++++++++++ 11 files changed, 52 insertions(+), 25 deletions(-) diff --git a/bin10/lib/mir_core.lib b/bin10/lib/mir_core.lib index 8e8b44cd43..58c24648c3 100644 Binary files a/bin10/lib/mir_core.lib and b/bin10/lib/mir_core.lib differ diff --git a/bin10/lib/mir_core64.lib b/bin10/lib/mir_core64.lib index 654763f59c..bd91bc8bb9 100644 Binary files a/bin10/lib/mir_core64.lib and b/bin10/lib/mir_core64.lib differ diff --git a/bin12/lib/mir_core.lib b/bin12/lib/mir_core.lib index 8e8b44cd43..58c24648c3 100644 Binary files a/bin12/lib/mir_core.lib and b/bin12/lib/mir_core.lib differ diff --git a/bin12/lib/mir_core64.lib b/bin12/lib/mir_core64.lib index 654763f59c..bd91bc8bb9 100644 Binary files a/bin12/lib/mir_core64.lib and b/bin12/lib/mir_core64.lib differ diff --git a/bin14/lib/mir_core.lib b/bin14/lib/mir_core.lib index 8e8b44cd43..58c24648c3 100644 Binary files a/bin14/lib/mir_core.lib and b/bin14/lib/mir_core.lib differ diff --git a/bin14/lib/mir_core64.lib b/bin14/lib/mir_core64.lib index 654763f59c..bd91bc8bb9 100644 Binary files a/bin14/lib/mir_core64.lib and b/bin14/lib/mir_core64.lib differ diff --git a/include/m_core.h b/include/m_core.h index e70187306c..c1d031f564 100644 --- a/include/m_core.h +++ b/include/m_core.h @@ -341,6 +341,7 @@ MIR_APP_DLL(int) ProtoGetAvatarFileFormat(const TCHAR *ptszFileName); // sha1 functions #define MIR_SHA1_HASH_SIZE 20 +#define MIR_SHA_BLOCKSIZE 64 struct mir_sha1_ctx { @@ -351,9 +352,9 @@ struct mir_sha1_ctx }; MIR_CORE_DLL(void) mir_sha1_init(mir_sha1_ctx *ctx); -MIR_CORE_DLL(void) mir_sha1_append(mir_sha1_ctx *ctx, const BYTE *dataIn, int len); +MIR_CORE_DLL(void) mir_sha1_append(mir_sha1_ctx *ctx, const BYTE *dataIn, size_t len); MIR_CORE_DLL(void) mir_sha1_finish(mir_sha1_ctx *ctx, BYTE hashout[MIR_SHA1_HASH_SIZE]); -MIR_CORE_DLL(void) mir_sha1_hash(BYTE *dataIn, int len, BYTE hashout[MIR_SHA1_HASH_SIZE]); +MIR_CORE_DLL(void) mir_sha1_hash(BYTE *dataIn, size_t len, BYTE hashout[MIR_SHA1_HASH_SIZE]); MIR_CORE_DLL(void) mir_hmac_sha1(BYTE hashout[MIR_SHA1_HASH_SIZE], const BYTE *key, size_t keylen, const BYTE *text, size_t textlen); @@ -366,7 +367,7 @@ struct SHA256_CONTEXT { UINT32 h0, h1, h2, h3, h4, h5, h6, h7; UINT32 nblocks; - BYTE buf[64]; + BYTE buf[MIR_SHA_BLOCKSIZE]; int count; }; @@ -375,6 +376,8 @@ MIR_CORE_DLL(void) mir_sha256_write(SHA256_CONTEXT *ctx, const void *dataIn, siz MIR_CORE_DLL(void) mir_sha256_final(SHA256_CONTEXT *ctx, BYTE hashout[MIR_SHA256_HASH_SIZE]); MIR_CORE_DLL(void) mir_sha256_hash(const void *dataIn, size_t len, BYTE hashout[MIR_SHA256_HASH_SIZE]); +MIR_CORE_DLL(void) mir_hmac_sha256(BYTE hashout[MIR_SHA256_HASH_SIZE], const BYTE *key, size_t keylen, const BYTE *text, size_t textlen); + /////////////////////////////////////////////////////////////////////////////// // strings diff --git a/src/mir_core/src/mir_core.def b/src/mir_core/src/mir_core.def index 3263fd854b..0ad6544d27 100644 --- a/src/mir_core/src/mir_core.def +++ b/src/mir_core/src/mir_core.def @@ -990,3 +990,4 @@ mir_sha256_write @1147 mir_forkthreadowner @1148 hex2bin @1149 hex2binW @1150 +mir_hmac_sha256 @1151 diff --git a/src/mir_core/src/mir_core64.def b/src/mir_core/src/mir_core64.def index 5fa704f8d9..6cd4c84264 100644 --- a/src/mir_core/src/mir_core64.def +++ b/src/mir_core/src/mir_core64.def @@ -990,3 +990,4 @@ mir_sha256_write @1147 mir_forkthreadowner @1148 hex2bin @1149 hex2binW @1150 +mir_hmac_sha256 @1151 diff --git a/src/mir_core/src/sha1.cpp b/src/mir_core/src/sha1.cpp index 33a85f8a54..bb6955023d 100644 --- a/src/mir_core/src/sha1.cpp +++ b/src/mir_core/src/sha1.cpp @@ -94,7 +94,7 @@ MIR_CORE_DLL(void) mir_sha1_init(mir_sha1_ctx *ctx) ctx->W[i] = 0; } -MIR_CORE_DLL(void) mir_sha1_append(mir_sha1_ctx *ctx, const BYTE *dataIn, int len) +MIR_CORE_DLL(void) mir_sha1_append(mir_sha1_ctx *ctx, const BYTE *dataIn, size_t len) { /* Read the data into W and process blocks as they get full */ @@ -145,7 +145,7 @@ MIR_CORE_DLL(void) mir_sha1_finish(mir_sha1_ctx *ctx, BYTE hashout[20]) mir_sha1_init(ctx); } -MIR_CORE_DLL(void) mir_sha1_hash(BYTE *dataIn, int len, BYTE hashout[20]) +MIR_CORE_DLL(void) mir_sha1_hash(BYTE *dataIn, size_t len, BYTE hashout[20]) { mir_sha1_ctx ctx; @@ -158,38 +158,29 @@ MIR_CORE_DLL(void) mir_sha1_hash(BYTE *dataIn, int len, BYTE hashout[20]) MIR_CORE_DLL(void) mir_hmac_sha1(BYTE hashout[MIR_SHA1_HASH_SIZE], const BYTE *key, size_t keylen, const BYTE *text, size_t textlen) { - const unsigned SHA_BLOCKSIZE = 64; - - BYTE mdkey[MIR_SHA1_HASH_SIZE], k_ipad[SHA_BLOCKSIZE], k_opad[SHA_BLOCKSIZE]; mir_sha1_ctx ctx; + BYTE usedKey[MIR_SHA_BLOCKSIZE] = { 0 }; - if (keylen > SHA_BLOCKSIZE) { + if (keylen > MIR_SHA_BLOCKSIZE) { mir_sha1_init(&ctx); mir_sha1_append(&ctx, key, (int)keylen); - mir_sha1_finish(&ctx, mdkey); - keylen = 20; - key = mdkey; + mir_sha1_finish(&ctx, usedKey); } + else memcpy(usedKey, key, keylen); - memcpy(k_ipad, key, keylen); - memcpy(k_opad, key, keylen); - if (keylen < SHA_BLOCKSIZE) { - memset(k_ipad + keylen, 0x36, SHA_BLOCKSIZE - keylen); - memset(k_opad + keylen, 0x5c, SHA_BLOCKSIZE - keylen); - } - - for (unsigned i = 0; i < keylen; i++) { - k_ipad[i] ^= 0x36; - k_opad[i] ^= 0x5c; - } + for (size_t i = 0; i < MIR_SHA_BLOCKSIZE; i++) + usedKey[i] ^= 0x36; mir_sha1_init(&ctx); - mir_sha1_append(&ctx, k_ipad, SHA_BLOCKSIZE); + mir_sha1_append(&ctx, usedKey, MIR_SHA_BLOCKSIZE); mir_sha1_append(&ctx, text, (int)textlen); mir_sha1_finish(&ctx, hashout); + for (size_t i = 0; i < MIR_SHA_BLOCKSIZE; i++) + usedKey[i] ^= 0x5C ^ 0x36; + mir_sha1_init(&ctx); - mir_sha1_append(&ctx, k_opad, SHA_BLOCKSIZE); + mir_sha1_append(&ctx, usedKey, MIR_SHA_BLOCKSIZE); mir_sha1_append(&ctx, hashout, MIR_SHA1_HASH_SIZE); mir_sha1_finish(&ctx, hashout); } diff --git a/src/mir_core/src/sha256.cpp b/src/mir_core/src/sha256.cpp index c6641479dd..70f1de0af6 100644 --- a/src/mir_core/src/sha256.cpp +++ b/src/mir_core/src/sha256.cpp @@ -287,3 +287,34 @@ MIR_CORE_DLL(void) mir_sha256_hash(const void *dataIn, size_t len, BYTE hashout[ mir_sha256_write(&tmp, dataIn, len); mir_sha256_final(&tmp, hashout); } + +///////////////////////////////////////////////////////////////////////////////////////// + +MIR_CORE_DLL(void) mir_hmac_sha256(BYTE hashout[MIR_SHA256_HASH_SIZE], const BYTE *key, size_t keylen, const BYTE *text, size_t textlen) +{ + SHA256_CONTEXT ctx; + BYTE usedKey[MIR_SHA_BLOCKSIZE] = { 0 }; + + if (keylen > MIR_SHA_BLOCKSIZE) { + mir_sha256_init(&ctx); + mir_sha256_write(&ctx, key, (int)keylen); + mir_sha256_final(&ctx, usedKey); + } + else memcpy(usedKey, key, keylen); + + for (size_t i = 0; i < MIR_SHA_BLOCKSIZE; i++) + usedKey[i] ^= 0x36; + + mir_sha256_init(&ctx); + mir_sha256_write(&ctx, usedKey, MIR_SHA_BLOCKSIZE); + mir_sha256_write(&ctx, text, textlen); + mir_sha256_final(&ctx, hashout); + + for (size_t i = 0; i < MIR_SHA_BLOCKSIZE; i++) + usedKey[i] ^= 0x5C ^ 0x36; + + mir_sha256_init(&ctx); + mir_sha256_write(&ctx, usedKey, MIR_SHA_BLOCKSIZE); + mir_sha256_write(&ctx, hashout, MIR_SHA1_HASH_SIZE); + mir_sha256_final(&ctx, hashout); +} -- cgit v1.2.3