diff options
author | George Hazan <george.hazan@gmail.com> | 2016-04-08 08:17:43 +0000 |
---|---|---|
committer | George Hazan <george.hazan@gmail.com> | 2016-04-08 08:17:43 +0000 |
commit | f4239191646ae0e49d4984a083d31110e9bc42c1 (patch) | |
tree | 7b7d21d416ec595cadd6432b9e59814644c7dc2d /src/mir_core | |
parent | 699805f92b46fe6e3282672e89e693e20125497a (diff) |
mir_hmac_sha256 added
git-svn-id: http://svn.miranda-ng.org/main/trunk@16609 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c
Diffstat (limited to 'src/mir_core')
-rw-r--r-- | src/mir_core/src/mir_core.def | 1 | ||||
-rw-r--r-- | src/mir_core/src/mir_core64.def | 1 | ||||
-rw-r--r-- | src/mir_core/src/sha1.cpp | 35 | ||||
-rw-r--r-- | src/mir_core/src/sha256.cpp | 31 |
4 files changed, 46 insertions, 22 deletions
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);
+}
|