123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- #include <string.h>
- #if defined(USE_GNUTLS)
- #include <gnutls/gnutls.h>
- #include <gnutls/crypto.h>
- #elif defined(__APPLE__)
- #define USE_CC_CRYPT 1
- #include <CommonCrypto/CommonDigest.h>
- #elif defined(_WIN32)
- #define USE_WIN_CRYPT 1
- #include <windows.h>
- #include <wincrypt.h>
- #else
- #include <openssl/sha.h>
- #endif
- #include "hmac.h"
- #ifdef USE_CC_CRYPT
- #define SHA256_CTX CC_SHA256_CTX
- #define SHA256_Init CC_SHA256_Init
- #define SHA256_Update CC_SHA256_Update
- #define SHA256_Final CC_SHA256_Final
- #endif
- typedef struct
- {
- #ifdef _WIN32
- HCRYPTPROV prov;
- HCRYPTHASH sha;
- #elif defined(USE_GNUTLS)
- gnutls_hmac_hd_t hmac;
- #else
- SHA256_CTX sha;
- #endif
- unsigned char keybuf[64];
- } hmac_sha256_t;
- static void HMAC_SHA256_Init(hmac_sha256_t *hmac, const void *key, size_t length)
- {
- #ifdef USE_WIN_CRYPT
- int i;
- CryptAcquireContext(&hmac->prov,NULL,NULL,PROV_RSA_AES,CRYPT_VERIFYCONTEXT);
- if(length > 64) {
- unsigned char digest[32];
- HCRYPTHASH sha;
- DWORD bufLength = 32;
- CryptCreateHash(hmac->prov,CALG_SHA_256,0,0,&sha);
- CryptHashData(sha,(PBYTE)key,(DWORD)length,0);
- CryptGetHashParam(sha,HP_HASHVAL,hmac->keybuf,&bufLength,0);
- CryptDestroyHash(sha);
- }
- else memcpy(hmac->keybuf,key,length);
- for(i=length;i<64;i++) hmac->keybuf[i] = 0;
- for(i=0;i<64;i++) hmac->keybuf[i] ^= 0x36;
- CryptCreateHash(hmac->prov,CALG_SHA_256,0,0,&hmac->sha);
- CryptHashData(hmac->sha,hmac->keybuf,64,0);
- #elif defined(USE_GNUTLS)
- gnutls_hmac_init(&hmac->hmac, GNUTLS_MAC_SHA256, key, length);
- #else
- size_t i;
- if(length > 64) {
- unsigned char digest[32];
- SHA256_CTX sha;
- SHA256_Init(&sha);
- SHA256_Update(&sha,key,length);
- SHA256_Final(digest,&sha);
- memcpy(hmac->keybuf,digest,32);
- length = 32;
- }
- else memcpy(hmac->keybuf,key,length);
- for(i=length;i<64;i++) hmac->keybuf[i] = 0;
- for(i=0;i<64;i++) hmac->keybuf[i] ^= 0x36;
- SHA256_Init(&hmac->sha);
- SHA256_Update(&hmac->sha,hmac->keybuf,64);
- #endif
- }
- static void HMAC_SHA256_Update(hmac_sha256_t *hmac, const void *data, size_t length)
- {
- #ifdef USE_WIN_CRYPT
- CryptHashData(hmac->sha,(PBYTE)data,(DWORD)length,0);
- #elif defined(USE_GNUTLS)
- gnutls_hmac(hmac->hmac, data, length);
- #else
- SHA256_Update(&hmac->sha,data,length);
- #endif
- }
- static void HMAC_SHA256_Final(hmac_sha256_t *hmac, unsigned char *md)
- {
- #ifdef USE_WIN_CRYPT
- DWORD i, bufLength = 32;
- CryptGetHashParam(hmac->sha,HP_HASHVAL,md,&bufLength,0);
- CryptDestroyHash(hmac->sha);
- HCRYPTHASH sha;
- CryptCreateHash(hmac->prov,CALG_SHA_256,0,0,&sha);
- for(i=0;i<64;i++) hmac->keybuf[i] ^= 0x36 ^ 0x5c;
- CryptHashData(sha,hmac->keybuf,64,0);
- CryptHashData(sha,md,32,0);
- CryptGetHashParam(sha,HP_HASHVAL,md,&bufLength,0);
- CryptDestroyHash(sha);
- CryptReleaseContext(hmac->prov, 0);
- #elif defined(USE_GNUTLS)
- gnutls_hmac_deinit(hmac->hmac, md);
- #else
- int i;
- SHA256_Final(md,&hmac->sha);
- SHA256_CTX sha;
- SHA256_Init(&sha);
- for(i=0;i<64;i++) hmac->keybuf[i] ^= 0x36 ^ 0x5c;
- SHA256_Update(&sha,hmac->keybuf,64);
- SHA256_Update(&sha,md,32);
- SHA256_Final(md,&sha);
- #endif
- }
- void proxy2ch_HMAC_SHA256(const void *key, size_t keyLength, const void *data, size_t dataLength, void *macOut)
- {
- hmac_sha256_t hmac;
- HMAC_SHA256_Init(&hmac, key, keyLength);
- HMAC_SHA256_Update(&hmac, data, dataLength);
- HMAC_SHA256_Final(&hmac, macOut);
- }
|