PKIFXSECCryptoHash.cpp
Go to the documentation of this file.00001
00010 #include "PKIFXSECCryptoHash.h"
00011 #include "PKIFXSECCryptoKeyHMAC.h"
00012
00013 #include <xsec/enc/XSCrypt/XSCryptCryptoBase64.hpp>
00014 #include <xsec/enc/XSECCryptoException.hpp>
00015 #include <xsec/enc/XSECCryptoUtils.hpp>
00016 #include <xsec/framework/XSECError.hpp>
00017
00018
00020 struct PKIFXSECCryptoHashImpl
00021 {
00022 PKIFXSECCryptoHashImpl();
00023 IPKIFMediatorPtr m_med;
00024 XSECCryptoHash::HashType m_alg;
00025 CPKIFKeyMaterialPtr m_key;
00026 IPKIFRawCryptContext * m_hmacCtx;
00027 IPKIFHashContext * m_hashCtx;
00028 IPKIFCryptoRaw * m_raw;
00029 IPKIFCryptoMisc * m_misc;
00030 };
00031
00040 PKIFXSECCryptoHashImpl::PKIFXSECCryptoHashImpl()
00041 {
00042 m_hmacCtx = 0;
00043 m_hashCtx = 0;
00044 m_raw = 0;
00045 m_misc = 0;
00046 }
00048
00055 PKIFCRYPTO::HASH_ALG pkifHashAlgForHT(
00057 XSECCryptoHash::HashType ht)
00058 {
00059 switch(ht) {
00060 case HASH_SHA1:
00061 return PKIFCRYPTO::SHA1;
00062 break;
00063 case HASH_MD5:
00064 return PKIFCRYPTO::MD5;
00065 break;
00066 case HASH_SHA224:
00067 return PKIFCRYPTO::SHA224;
00068 break;
00069 case HASH_SHA256:
00070 return PKIFCRYPTO::SHA256;
00071 break;
00072 case HASH_SHA384:
00073 return PKIFCRYPTO::SHA384;
00074 break;
00075 case HASH_SHA512:
00076 return PKIFCRYPTO::SHA512;
00077 break;
00078 }
00079
00080 return PKIFCRYPTO::SHA1;
00081 }
00082
00090 PKIFXSECCryptoHash::PKIFXSECCryptoHash()
00091 :m_impl(new PKIFXSECCryptoHashImpl)
00092 {
00093 }
00094
00102 PKIFXSECCryptoHash::PKIFXSECCryptoHash(
00104 IPKIFMediatorPtr & med,
00106 HashType alg)
00107 :m_impl(new PKIFXSECCryptoHashImpl)
00108 {
00109 m_impl->m_med = med;
00110 m_impl->m_alg = alg;
00111
00112 }
00113
00121 PKIFXSECCryptoHash::~PKIFXSECCryptoHash()
00122 {
00123 if(m_impl) {
00124 if(m_impl->m_hmacCtx) {
00125 delete m_impl->m_hmacCtx;
00126 m_impl->m_hmacCtx = 0;
00127 }
00128 if(m_impl->m_hashCtx) {
00129 delete m_impl->m_hashCtx;
00130 m_impl->m_hashCtx = 0;
00131 }
00132 delete m_impl;
00133 m_impl = 0;
00134 }
00135 }
00136
00144 void PKIFXSECCryptoHash::reset(void)
00145 {
00146 if(m_impl->m_hmacCtx) {
00147 delete m_impl->m_hmacCtx;
00148 m_impl->m_hmacCtx = 0;
00149 }
00150 if(m_impl->m_hashCtx) {
00151 delete m_impl->m_hashCtx;
00152 m_impl->m_hashCtx = 0;
00153 }
00154 }
00155
00163 void PKIFXSECCryptoHash::hash(
00165 unsigned char * data,
00167 unsigned int length)
00168 {
00169 if(!m_impl->m_med) {
00170 throw XSECCryptoException(XSECCryptoException::MDError,
00171 "PKIFXSECCryptoHash - No mediator is configured");
00172 }
00173 try {
00174
00175 if(m_impl->m_key) {
00176
00177 if(!m_impl->m_raw) {
00178 m_impl->m_raw = m_impl->m_med->GetMediator<IPKIFCryptoRaw>();
00179 if(!m_impl->m_raw) {
00180 throw XSECCryptoException(XSECCryptoException::MDError,
00181 "PKIFXSECCryptoHash - No mediator is configured for HMAC support");
00182 }
00183 }
00184
00185 if(!m_impl->m_hmacCtx) {
00186 m_impl->m_hmacCtx = m_impl->m_raw->HMACInit(*(m_impl->m_key),pkifHashAlgForHT(m_impl->m_alg));
00187 }
00188 m_impl->m_raw->HMACUpdate(m_impl->m_hmacCtx,data,length);
00189 } else {
00190
00191 if(!m_impl->m_misc) {
00192 m_impl->m_misc = m_impl->m_med->GetMediator<IPKIFCryptoMisc>();
00193 if(!m_impl->m_misc) {
00194 throw XSECCryptoException(XSECCryptoException::MDError,
00195 "PKIFXSECCryptoHash - No mediator is configured for hash support");
00196 }
00197 }
00198
00199 if(!m_impl->m_hashCtx) {
00200 m_impl->m_hashCtx = m_impl->m_misc->HashInit(pkifHashAlgForHT(m_impl->m_alg));
00201 }
00202 m_impl->m_misc->HashUpdate(m_impl->m_hashCtx,data,length);
00203 }
00204 }catch(CPKIFException & e) {
00205 throw XSECCryptoException(XSECCryptoException::MDError,e.GetDescription());
00206 }
00207 }
00208
00216 unsigned int PKIFXSECCryptoHash::finish(
00218 unsigned char * hash,
00220 unsigned int maxLength)
00221 {
00222 int outLen = maxLength;
00223 try {
00224
00225 if(m_impl->m_hmacCtx) {
00226 m_impl->m_raw->HMACFinal(m_impl->m_hmacCtx,hash,&outLen);
00227 } else if(m_impl->m_hashCtx){
00228 m_impl->m_misc->HashFinal(m_impl->m_hashCtx,hash,&outLen);
00229 }
00230 }catch(CPKIFException & e) {
00231 throw XSECCryptoException(XSECCryptoException::MDError,e.GetDescription());
00232 }
00233 return outLen;
00234 }
00235
00243 XSECCryptoHash::HashType PKIFXSECCryptoHash::getHashType() const
00244 {
00245 return m_impl->m_alg;
00246 }
00247
00255 void PKIFXSECCryptoHash::setKey(
00257 XSECCryptoKey * key)
00258 {
00259 if(!key){
00260 CPKIFKeyMaterialPtr empty;
00261 m_impl->m_key = empty;
00262 return;
00263 }
00264 PKIFXSECCryptoKeyHMAC * hmacKey = dynamic_cast<PKIFXSECCryptoKeyHMAC *>(key);
00265 if(!hmacKey) {
00266 throw XSECCryptoException(XSECCryptoException::MDError,
00267 "PKIFXSECCryptoHash - Invalid key");
00268 }
00269
00270 m_impl->m_key = hmacKey->getPKIFKeyMaterial();
00271 }
00272