00001
00032 #include "stdafx.h"
00033 #include "CPipeHMAC_SHA1.h"
00034
00035
00036
00037
00038 namespace AxPipe {
00039 namespace Stock {
00040
00041 template CPipeHMAC_SHA1<128>;
00042 template CPipeHMAC_SHA1<160>;
00043
00049 template<int iBits> void
00050 CPipeHMAC_SHA1<iBits>::XorPad(unsigned char oPad) {
00051 for (int i=0; i < sizeof m_HMAC; i++) {
00052 ((unsigned char *)&m_HMAC)[i] = oPad;
00053 if (i < sizeof m_Key) {
00054 ((unsigned char *)&m_HMAC)[i] ^= ((unsigned char *)&m_Key)[i];
00055 }
00056 }
00057 }
00058
00064 template<int iBits> CPipeHMAC_SHA1<iBits> *
00065 CPipeHMAC_SHA1<iBits>::Init(TBits<iBits> *pKey, size_t cbOffset) {
00066 CopyMemory(&m_Key, pKey, sizeof m_Key);
00067 m_cbOffset = cbOffset;
00068 return this;
00069 }
00070
00075 template<int iBits> void
00076 CPipeHMAC_SHA1<iBits>::Out(AxPipe::CSeg *pSeg) {
00077 if (pSeg->Len() <= m_cbOffset) {
00078 m_cbOffset -= pSeg->Len();
00079 Pump(pSeg);
00080 } else {
00081 if (m_cbOffset) {
00082 CSeg *pPartialSeg = pSeg->Clone();
00083 pPartialSeg->Len(m_cbOffset);
00084 Pump(pPartialSeg);
00085
00086 pSeg->Drop(m_cbOffset);
00087 m_cbOffset = 0;
00088 }
00089 CPipeSHA1::Out(pSeg);
00090 }
00091 }
00092
00096 template<int iBits> bool
00097 CPipeHMAC_SHA1<iBits>::OutOpen() {
00098 bool fReturn = CPipeSHA1::OutOpen();
00099
00100
00101 XorPad(0x36);
00102
00103
00104 ASSAPI(CryptHashData(m_hHash, (unsigned char *)&m_HMAC, sizeof m_HMAC, 0));
00105
00106 return fReturn;
00107 }
00108
00113 template<int iBits> bool
00114 CPipeHMAC_SHA1<iBits>::OutClose() {
00115
00116 TBits<160> innerHash;
00117 DWORD dwHashLen = sizeof innerHash;
00118 ASSAPI(CryptGetHashParam(m_hHash, HP_HASHVAL, (unsigned char *)&innerHash, &dwHashLen, 0));
00119
00120
00121 CryptDestroyHash(m_hHash);
00122 ASSAPI(CryptCreateHash(m_hCryptProv, CALG_SHA1, 0, 0, &m_hHash));
00123
00124
00125 XorPad(0x5c);
00126
00127
00128 ASSAPI(CryptHashData(m_hHash, (unsigned char *)&m_HMAC, sizeof m_HMAC, 0));
00129
00130
00131 ASSAPI(CryptHashData(m_hHash, (unsigned char *)&innerHash, sizeof innerHash, 0));
00132
00133
00134 return CPipeSHA1::OutClose();
00135 }
00136 }
00137 }