Main Page | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

CPipeHMAC_SHA1.cpp

Go to the documentation of this file.
00001 
00032 #include "stdafx.h"
00033 #include "CPipeHMAC_SHA1.h"
00034 
00035 // Make for some more convenient notation below
00036 // using AxPipe::Stock::CPipeHMAC_SHA1;
00037 // using AxPipe::Stock::TBits;
00038 namespace AxPipe {
00039     namespace Stock {
00040         // Instantiate explicitly to generate code for supported bit-lengths.
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;          // Number of bytes to skip before starting to HMAC
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             // K xor ipad
00101             XorPad(0x36);
00102 
00103             // Hash(iPad xor Key)
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             // Save the inner hash
00116             TBits<160> innerHash;
00117             DWORD dwHashLen = sizeof innerHash;
00118             ASSAPI(CryptGetHashParam(m_hHash, HP_HASHVAL, (unsigned char *)&innerHash, &dwHashLen, 0));
00119 
00120             // Re-initialize our hash-object.
00121             CryptDestroyHash(m_hHash); 
00122             ASSAPI(CryptCreateHash(m_hCryptProv, CALG_SHA1, 0, 0, &m_hHash));
00123 
00124             // K xor opad
00125             XorPad(0x5c);
00126 
00127             // Hash(oPad xor Key)
00128             ASSAPI(CryptHashData(m_hHash, (unsigned char *)&m_HMAC, sizeof m_HMAC, 0));
00129 
00130             // Hash(InnerHash)
00131             ASSAPI(CryptHashData(m_hHash, (unsigned char *)&innerHash, sizeof innerHash, 0));
00132 
00133             // Prepare the final hash as output.
00134             return CPipeSHA1::OutClose();
00135         }
00136     }
00137 }

Generated on Mon Feb 2 13:19:18 2004 for AxPipe by doxygen 1.3.5