hmac.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #include <string.h>
  2. #if defined(USE_GNUTLS)
  3. #include <gnutls/gnutls.h>
  4. #include <gnutls/crypto.h>
  5. #elif defined(__APPLE__)
  6. #define USE_CC_CRYPT 1
  7. #include <CommonCrypto/CommonDigest.h>
  8. #elif defined(_WIN32)
  9. #define USE_WIN_CRYPT 1
  10. #include <windows.h>
  11. #include <wincrypt.h>
  12. #else
  13. #include <openssl/opensslv.h>
  14. #if OPENSSL_VERSION_NUMBER >= 0x30000000L
  15. #define OPENSSL_3_AND_LATER 1
  16. #include <openssl/evp.h>
  17. #else
  18. #include <openssl/sha.h>
  19. #endif
  20. #endif
  21. #include "hmac.h"
  22. #ifdef USE_CC_CRYPT
  23. #define SHA256_CTX CC_SHA256_CTX
  24. #define SHA256_Init CC_SHA256_Init
  25. #define SHA256_Update CC_SHA256_Update
  26. #define SHA256_Final CC_SHA256_Final
  27. #endif
  28. typedef struct
  29. {
  30. #ifdef USE_WIN_CRYPT
  31. HCRYPTPROV prov;
  32. HCRYPTHASH sha;
  33. #elif defined(USE_GNUTLS)
  34. gnutls_hmac_hd_t hmac;
  35. #elif defined(OPENSSL_3_AND_LATER)
  36. EVP_MD_CTX *basectx;
  37. EVP_MD_CTX *mdctx;
  38. #else
  39. SHA256_CTX sha;
  40. #endif
  41. unsigned char keybuf[64];
  42. } hmac_sha256_t;
  43. static void HMAC_SHA256_Init(hmac_sha256_t *hmac, const void *key, size_t length)
  44. {
  45. #ifdef USE_WIN_CRYPT
  46. int i;
  47. CryptAcquireContext(&hmac->prov,NULL,NULL,PROV_RSA_AES,CRYPT_VERIFYCONTEXT);
  48. if(length > 64) {
  49. unsigned char digest[32];
  50. HCRYPTHASH sha;
  51. DWORD bufLength = 32;
  52. CryptCreateHash(hmac->prov,CALG_SHA_256,0,0,&sha);
  53. CryptHashData(sha,(PBYTE)key,(DWORD)length,0);
  54. CryptGetHashParam(sha,HP_HASHVAL,hmac->keybuf,&bufLength,0);
  55. CryptDestroyHash(sha);
  56. }
  57. else memcpy(hmac->keybuf,key,length);
  58. for(i=length;i<64;i++) hmac->keybuf[i] = 0;
  59. for(i=0;i<64;i++) hmac->keybuf[i] ^= 0x36;
  60. CryptCreateHash(hmac->prov,CALG_SHA_256,0,0,&hmac->sha);
  61. CryptHashData(hmac->sha,hmac->keybuf,64,0);
  62. #elif defined(USE_GNUTLS)
  63. gnutls_hmac_init(&hmac->hmac, GNUTLS_MAC_SHA256, key, length);
  64. #elif defined(OPENSSL_3_AND_LATER)
  65. size_t i;
  66. hmac->basectx = EVP_MD_CTX_new();
  67. hmac->mdctx = EVP_MD_CTX_new();
  68. EVP_DigestInit_ex(hmac->basectx, EVP_sha256(), NULL);
  69. if (length > 64) {
  70. EVP_MD_CTX_copy_ex(hmac->mdctx, hmac->basectx);
  71. EVP_DigestUpdate(hmac->mdctx, key, length);
  72. EVP_DigestFinal_ex(hmac->mdctx, hmac->keybuf, NULL);
  73. length = 32;
  74. }
  75. else memcpy(hmac->keybuf, key, length);
  76. for (i=length; i<64; i++) hmac->keybuf[i] = 0;
  77. for (i=0; i<64; i++) hmac->keybuf[i] ^= 0x36;
  78. EVP_MD_CTX_copy_ex(hmac->mdctx, hmac->basectx);
  79. EVP_DigestUpdate(hmac->mdctx, hmac->keybuf, 64);
  80. #else
  81. size_t i;
  82. if(length > 64) {
  83. unsigned char digest[32];
  84. SHA256_CTX sha;
  85. SHA256_Init(&sha);
  86. SHA256_Update(&sha,key,length);
  87. SHA256_Final(digest,&sha);
  88. memcpy(hmac->keybuf,digest,32);
  89. length = 32;
  90. }
  91. else memcpy(hmac->keybuf,key,length);
  92. for(i=length;i<64;i++) hmac->keybuf[i] = 0;
  93. for(i=0;i<64;i++) hmac->keybuf[i] ^= 0x36;
  94. SHA256_Init(&hmac->sha);
  95. SHA256_Update(&hmac->sha,hmac->keybuf,64);
  96. #endif
  97. }
  98. static void HMAC_SHA256_Update(hmac_sha256_t *hmac, const void *data, size_t length)
  99. {
  100. #ifdef USE_WIN_CRYPT
  101. CryptHashData(hmac->sha,(PBYTE)data,(DWORD)length,0);
  102. #elif defined(USE_GNUTLS)
  103. gnutls_hmac(hmac->hmac, data, length);
  104. #elif defined(OPENSSL_3_AND_LATER)
  105. EVP_DigestUpdate(hmac->mdctx, data, length);
  106. #else
  107. SHA256_Update(&hmac->sha,data,length);
  108. #endif
  109. }
  110. static void HMAC_SHA256_Final(hmac_sha256_t *hmac, unsigned char *md)
  111. {
  112. #ifdef USE_WIN_CRYPT
  113. DWORD i, bufLength = 32;
  114. CryptGetHashParam(hmac->sha,HP_HASHVAL,md,&bufLength,0);
  115. CryptDestroyHash(hmac->sha);
  116. HCRYPTHASH sha;
  117. CryptCreateHash(hmac->prov,CALG_SHA_256,0,0,&sha);
  118. for(i=0;i<64;i++) hmac->keybuf[i] ^= 0x36 ^ 0x5c;
  119. CryptHashData(sha,hmac->keybuf,64,0);
  120. CryptHashData(sha,md,32,0);
  121. CryptGetHashParam(sha,HP_HASHVAL,md,&bufLength,0);
  122. CryptDestroyHash(sha);
  123. CryptReleaseContext(hmac->prov, 0);
  124. #elif defined(USE_GNUTLS)
  125. gnutls_hmac_deinit(hmac->hmac, md);
  126. #elif defined(OPENSSL_3_AND_LATER)
  127. int i;
  128. EVP_DigestFinal_ex(hmac->mdctx, md, NULL);
  129. EVP_MD_CTX_copy_ex(hmac->mdctx, hmac->basectx);
  130. for (i=0; i<64; i++) hmac->keybuf[i] ^= 0x36 ^ 0x5c;
  131. EVP_DigestUpdate(hmac->mdctx, hmac->keybuf, 64);
  132. EVP_DigestUpdate(hmac->mdctx, md, 32);
  133. EVP_DigestFinal_ex(hmac->mdctx, md, NULL);
  134. EVP_MD_CTX_free(hmac->mdctx);
  135. EVP_MD_CTX_free(hmac->basectx);
  136. #else
  137. int i;
  138. SHA256_Final(md,&hmac->sha);
  139. SHA256_CTX sha;
  140. SHA256_Init(&sha);
  141. for(i=0;i<64;i++) hmac->keybuf[i] ^= 0x36 ^ 0x5c;
  142. SHA256_Update(&sha,hmac->keybuf,64);
  143. SHA256_Update(&sha,md,32);
  144. SHA256_Final(md,&sha);
  145. #endif
  146. }
  147. void proxy2ch_HMAC_SHA256(const void *key, size_t keyLength, const void *data, size_t dataLength, void *macOut)
  148. {
  149. hmac_sha256_t hmac;
  150. HMAC_SHA256_Init(&hmac, key, keyLength);
  151. HMAC_SHA256_Update(&hmac, data, dataLength);
  152. HMAC_SHA256_Final(&hmac, macOut);
  153. }