aboutsummaryrefslogtreecommitdiff
path: root/src/hash.cpp
diff options
context:
space:
mode:
authorPieter Wuille <pieter.wuille@gmail.com>2013-06-22 22:23:22 +0200
committerPieter Wuille <pieter.wuille@gmail.com>2013-06-24 00:56:45 +0200
commit12dff9801f92d37c330ccf4776da36a3910abec4 (patch)
treee0c6123959759ec57ee6fff354a41f548bedb770 /src/hash.cpp
parentee4949794bb7f881a67266ad598edb5cf6019d31 (diff)
Add HMAC-SHA512 to hash
Diffstat (limited to 'src/hash.cpp')
-rw-r--r--src/hash.cpp41
1 files changed, 41 insertions, 0 deletions
diff --git a/src/hash.cpp b/src/hash.cpp
index bddd8abf38..7b054bd154 100644
--- a/src/hash.cpp
+++ b/src/hash.cpp
@@ -56,3 +56,44 @@ unsigned int MurmurHash3(unsigned int nHashSeed, const std::vector<unsigned char
return h1;
}
+
+int HMAC_SHA512_Init(HMAC_SHA512_CTX *pctx, const void *pkey, size_t len)
+{
+ unsigned char key[128];
+ if (len <= 128)
+ {
+ memcpy(key, pkey, len);
+ memset(key + len, 0, 128-len);
+ }
+ else
+ {
+ SHA512_CTX ctxKey;
+ SHA512_Init(&ctxKey);
+ SHA512_Update(&ctxKey, pkey, len);
+ SHA512_Final(key, &ctxKey);
+ memset(key + 64, 0, 64);
+ }
+
+ for (int n=0; n<128; n++)
+ key[n] ^= 0x5c;
+ SHA512_Init(&pctx->ctxOuter);
+ SHA512_Update(&pctx->ctxOuter, key, 128);
+
+ for (int n=0; n<128; n++)
+ key[n] ^= 0x5c ^ 0x36;
+ SHA512_Init(&pctx->ctxInner);
+ return SHA512_Update(&pctx->ctxInner, key, 128);
+}
+
+int HMAC_SHA512_Update(HMAC_SHA512_CTX *pctx, const void *pdata, size_t len)
+{
+ return SHA512_Update(&pctx->ctxInner, pdata, len);
+}
+
+int HMAC_SHA512_Final(unsigned char *pmd, HMAC_SHA512_CTX *pctx)
+{
+ unsigned char buf[64];
+ SHA512_Final(buf, &pctx->ctxInner);
+ SHA512_Update(&pctx->ctxOuter, buf, 64);
+ return SHA512_Final(pmd, &pctx->ctxOuter);
+}