1 // This is a part of the Active Template Library.
\r
2 // Copyright (C) Microsoft Corporation
\r
3 // All rights reserved.
\r
5 // This source code is only intended as a supplement to the
\r
6 // Active Template Library Reference and related
\r
7 // electronic documentation provided with the library.
\r
8 // See these sources for detailed information regarding the
\r
9 // Active Template Library product.
\r
12 #ifndef __ATLCRYPT_INL__
\r
13 #define __ATLCRYPT_INL__
\r
17 #ifndef __ATLCRYPT_H__
\r
18 #error atlcrypt.inl requires atlcrypt.h to be included first
\r
25 inline CCryptProv::CCryptProv( const CCryptProv& prov ) throw()
\r
27 m_hProv = prov.m_hProv;
\r
32 inline CCryptProv::CCryptProv( HCRYPTPROV hProv, BOOL bTakeOwnership ) throw()
\r
35 if (m_hProv && !bTakeOwnership)
\r
39 inline CCryptProv::~CCryptProv() throw()
\r
44 inline CCryptProv& CCryptProv::operator=( const CCryptProv& prov ) throw()
\r
50 m_hProv = prov.m_hProv;
\r
51 if( m_hProv != NULL )
\r
59 inline HRESULT CCryptProv::AddRef() throw()
\r
61 ATLASSUME( m_hProv != NULL );
\r
63 if (!CryptContextAddRef( m_hProv, NULL, 0))
\r
65 return AtlHresultFromLastError();
\r
70 inline void CCryptProv::Attach( HCRYPTPROV hProv, BOOL bTakeOwnership ) throw()
\r
72 ATLASSUME( m_hProv == NULL );
\r
75 if (m_hProv && !bTakeOwnership)
\r
79 inline HCRYPTPROV CCryptProv::Detach() throw()
\r
90 inline CCryptProv::CCryptProv() throw() :
\r
95 inline HRESULT CCryptProv::Release() throw()
\r
97 if( m_hProv != NULL )
\r
99 if (!CryptReleaseContext( m_hProv, 0 ))
\r
101 return AtlHresultFromLastError();
\r
108 inline HRESULT CCryptProv::Initialize(
\r
109 DWORD dwProviderType,
\r
110 LPCTSTR szContainer,
\r
111 LPCTSTR szProvider,
\r
112 DWORD dwFlags) throw()
\r
114 ATLASSUME(m_hProv == NULL);
\r
116 if (!CryptAcquireContext(&m_hProv, szContainer, szProvider, dwProviderType, dwFlags))
\r
118 return AtlHresultFromLastError();
\r
123 inline HRESULT CCryptProv::InitVerifyContext(
\r
124 DWORD dwProviderType,
\r
125 LPCTSTR szProvider,
\r
126 DWORD dwFlags) throw()
\r
128 ATLASSUME(m_hProv == NULL);
\r
130 if (!CryptAcquireContext(&m_hProv, NULL, szProvider, dwProviderType, CRYPT_VERIFYCONTEXT | dwFlags))
\r
132 return AtlHresultFromLastError();
\r
137 inline HRESULT CCryptProv::InitCreateKeySet(
\r
138 DWORD dwProviderType,
\r
139 LPCTSTR szContainer,
\r
140 LPCTSTR szProvider,
\r
141 DWORD dwFlags) throw()
\r
143 ATLASSUME(m_hProv == NULL);
\r
145 if (!CryptAcquireContext(&m_hProv, szContainer, szProvider, dwProviderType, CRYPT_NEWKEYSET | dwFlags))
\r
147 return AtlHresultFromLastError();
\r
152 inline HRESULT CCryptProv::DeleteKeySet(
\r
153 DWORD dwProviderType,
\r
154 LPCTSTR szContainer,
\r
155 LPCTSTR szProvider,
\r
156 DWORD dwFlags) throw()
\r
158 HCRYPTPROV hProv = NULL;
\r
159 if (!CryptAcquireContext(&hProv, szContainer, szProvider, dwProviderType, CRYPT_DELETEKEYSET | dwFlags))
\r
161 return AtlHresultFromLastError();
\r
167 inline HRESULT CCryptProv::Uninitialize() throw()
\r
169 ATLASSUME(m_hProv != NULL);
\r
171 if (!CryptReleaseContext(m_hProv, 0))
\r
173 return AtlHresultFromLastError();
\r
182 inline HRESULT CCryptProv::GetParam(DWORD dwParam, BYTE * pbData, DWORD * pdwDataLen, DWORD dwFlags) throw()
\r
184 ATLASSUME(m_hProv != NULL);
\r
186 if (!CryptGetProvParam(m_hProv, dwParam, pbData, pdwDataLen, dwFlags))
\r
188 return AtlHresultFromLastError();
\r
193 inline HRESULT CCryptProv::SetParam( DWORD dwParam, BYTE* pbData, DWORD dwFlags) throw()
\r
195 ATLASSUME(m_hProv != NULL);
\r
197 if (!CryptSetProvParam(m_hProv, dwParam, pbData, dwFlags ))
\r
199 return AtlHresultFromLastError();
\r
204 inline HRESULT CCryptProv::GetName(__out_bcount_part(*pdwLength, *pdwLength) LPSTR szBuf, __inout DWORD * pdwLength) throw()
\r
206 return GetParam(PP_NAME, (BYTE *)szBuf, pdwLength);
\r
209 inline HRESULT CCryptProv::GetContainer(__out_bcount_part(*pdwLength, *pdwLength) LPSTR szBuf, __inout DWORD * pdwLength) throw()
\r
211 return GetParam(PP_CONTAINER, (BYTE *)szBuf, pdwLength);
\r
214 inline HRESULT CCryptProv::GetImpType(DWORD * pdwImpType) throw()
\r
216 DWORD dwLength = sizeof(DWORD);
\r
217 return GetParam(PP_IMPTYPE, (BYTE *)pdwImpType, &dwLength);
\r
220 inline HRESULT CCryptProv::GetVersion(DWORD * pdwVersion) throw()
\r
222 DWORD dwLength = sizeof(DWORD);
\r
223 return GetParam(PP_VERSION, (BYTE *)pdwVersion, &dwLength);
\r
226 inline HRESULT CCryptProv::GetProvType(DWORD * pdwType) throw()
\r
228 DWORD dwLength = sizeof(DWORD);
\r
229 return GetParam(PP_PROVTYPE, (BYTE * )pdwType, &dwLength);
\r
232 inline HRESULT CCryptProv::GetSecurityDesc(SECURITY_INFORMATION * pSecInfo) throw()
\r
234 DWORD dwSize = sizeof(SECURITY_INFORMATION);
\r
235 return GetParam(PP_KEYSET_SEC_DESCR, (BYTE *)pSecInfo, &dwSize);
\r
238 inline HRESULT CCryptProv::SetSecurityDesc(SECURITY_INFORMATION SecInfo) throw()
\r
240 return SetParam(PP_KEYSET_SEC_DESCR, (BYTE *)&SecInfo);
\r
243 inline HRESULT CCryptProv::GenRandom(ULONG nLength, BYTE* pbBuffer ) throw()
\r
245 ATLASSUME(m_hProv != NULL);
\r
247 if (!CryptGenRandom( m_hProv, nLength, pbBuffer ))
\r
249 return AtlHresultFromLastError();
\r
255 inline CCryptHash::CCryptHash() throw() :
\r
260 inline CCryptHash::CCryptHash( const CCryptHash& hash ) throw()
\r
262 m_hHash = hash.Duplicate();
\r
265 inline CCryptHash::CCryptHash( HCRYPTHASH hHash, BOOL bTakeOwnership ) throw()
\r
267 if (bTakeOwnership)
\r
272 BOOL bRet = ::CryptDuplicateHash( hHash, NULL, 0, &m_hHash );
\r
278 inline CCryptHash::~CCryptHash() throw()
\r
283 inline void CCryptHash::Attach( HCRYPTHASH hHash, BOOL bTakeOwnership ) throw()
\r
285 ATLASSUME( m_hHash == NULL );
\r
287 if (bTakeOwnership)
\r
292 BOOL bRet = ::CryptDuplicateHash( hHash, NULL, 0, &m_hHash );
\r
298 inline void CCryptHash::Destroy() throw()
\r
300 if( m_hHash != NULL )
\r
304 bSuccess = ::CryptDestroyHash( m_hHash );
\r
306 // can fail if the cryptographic service provider
\r
307 // (managed by CCryptProv) has already been destroyed
\r
308 ATLASSERT( bSuccess );
\r
313 inline HCRYPTHASH CCryptHash::Detach() throw()
\r
323 inline HCRYPTHASH CCryptHash::Duplicate() const throw()
\r
328 ATLASSUME( m_hHash != NULL );
\r
331 bSuccess = ::CryptDuplicateHash( m_hHash, NULL, 0, &hHash );
\r
340 inline HRESULT CCryptHash::Uninitialize() throw()
\r
342 ATLASSUME(m_hHash != NULL);
\r
344 if (!CryptDestroyHash(m_hHash))
\r
346 return AtlHresultFromLastError();
\r
355 inline HRESULT CCryptHash::Detach(HCRYPTHASH * phHash) throw()
\r
359 return E_INVALIDARG;
\r
367 inline HRESULT CCryptHash::AddData(const BYTE * pbData, DWORD dwDataLen, DWORD dwFlags) throw()
\r
369 ATLASSUME(m_hHash != NULL);
\r
371 if (!CryptHashData(m_hHash, pbData, dwDataLen, dwFlags))
\r
373 return AtlHresultFromLastError();
\r
379 inline HRESULT CCryptHash::AddString(LPCTSTR szData, DWORD dwFlags) throw()
\r
381 return AddData((BYTE *)szData, (DWORD)_tcslen(szData) * sizeof(TCHAR), dwFlags);
\r
384 inline HRESULT CCryptHash::GetParam(DWORD dwParam, BYTE * pbData, DWORD * pdwDataLen, DWORD dwFlags) throw()
\r
386 ATLASSUME(m_hHash != NULL);
\r
388 if (!CryptGetHashParam(m_hHash, dwParam, pbData, pdwDataLen, dwFlags))
\r
390 return AtlHresultFromLastError();
\r
395 inline HRESULT CCryptHash::SetParam(DWORD dwParam, BYTE * pbData, DWORD dwFlags) throw()
\r
397 ATLASSUME(m_hHash != NULL);
\r
399 if (!CryptSetHashParam(m_hHash, dwParam, pbData, dwFlags))
\r
401 return AtlHresultFromLastError();
\r
406 inline HRESULT CCryptHash::GetAlgId(ALG_ID * pAlgId) throw()
\r
408 DWORD dwSize = sizeof(ALG_ID);
\r
409 return GetParam(HP_ALGID, (BYTE *)pAlgId, &dwSize);
\r
412 inline HRESULT CCryptHash::GetSize(DWORD * pdwSize) throw()
\r
414 DWORD dwLength = sizeof(DWORD);
\r
415 return GetParam(HP_HASHSIZE, (BYTE *)pdwSize, &dwLength);
\r
418 inline HRESULT CCryptHash::GetValue(BYTE * pBuf, DWORD * pdwSize) throw()
\r
420 return GetParam(HP_HASHVAL, pBuf, pdwSize);
\r
423 inline HRESULT CCryptHash::SetValue(BYTE * pBuf) throw()
\r
425 return SetParam(HP_HASHVAL, pBuf);
\r
428 inline HRESULT CCryptHash::Sign(
\r
429 BYTE * pbSignature,
\r
432 DWORD dwKeySpec) throw()
\r
434 ATLASSUME(m_hHash != NULL);
\r
436 if (!CryptSignHash(m_hHash, dwKeySpec, NULL, dwFlags, pbSignature, pdwSigLen))
\r
438 return AtlHresultFromLastError();
\r
443 inline HRESULT CCryptHash::VerifySignature(
\r
444 const BYTE * pbSignature,
\r
447 DWORD dwFlags) throw()
\r
449 ATLASSUME(m_hHash != NULL);
\r
451 if (!CryptVerifySignature(m_hHash, pbSignature, dwSigLen, PubKey.GetHandle(), NULL, dwFlags))
\r
453 return AtlHresultFromLastError();
\r
458 __declspec(selectany) CCryptHash CCryptHash::EmptyHash = CCryptHash();
\r
459 __declspec(selectany) CCryptKey CCryptKey::EmptyKey = CCryptKey();
\r
460 inline CCryptKey::CCryptKey() throw() :
\r
465 inline CCryptKey::CCryptKey( const CCryptKey& key ) throw()
\r
467 m_hKey = key.Duplicate();
\r
470 inline CCryptKey::CCryptKey( HCRYPTKEY hKey, BOOL bTakeOwnership ) throw()
\r
472 if (bTakeOwnership)
\r
476 BOOL bSuccess = ::CryptDuplicateKey( hKey, NULL, 0, &m_hKey );
\r
482 inline CCryptKey::~CCryptKey() throw()
\r
487 inline void CCryptKey::Attach( HCRYPTKEY hKey, BOOL bTakeOwnership ) throw()
\r
489 ATLASSUME( m_hKey == NULL );
\r
490 if (bTakeOwnership)
\r
494 BOOL bSuccess = ::CryptDuplicateKey( hKey, NULL, 0, &m_hKey );
\r
500 inline void CCryptKey::Destroy() throw()
\r
502 if( m_hKey != NULL )
\r
506 bSuccess = ::CryptDestroyKey( m_hKey );
\r
508 // can fail if the cryptographic service provider
\r
509 // (managed by CCryptProv) has already been destroyed
\r
510 ATLASSERT( bSuccess );
\r
515 inline HCRYPTKEY CCryptKey::Detach() throw()
\r
525 inline HCRYPTKEY CCryptKey::Duplicate() const throw()
\r
529 ATLASSUME( m_hKey != NULL );
\r
531 HCRYPTKEY hKey = NULL;
\r
532 bSuccess = ::CryptDuplicateKey( m_hKey, NULL, 0, &hKey );
\r
539 inline HRESULT CCryptKey::Uninitialize() throw()
\r
541 ATLASSUME(m_hKey != NULL);
\r
543 if (!CryptDestroyKey(m_hKey))
\r
545 return AtlHresultFromLastError();
\r
554 inline HRESULT CCryptKey::Encrypt(
\r
557 DWORD * pdwDataLen,
\r
559 CCryptHash &Hash) throw()
\r
561 ATLASSUME(m_hKey != NULL);
\r
563 if (!::CryptEncrypt(m_hKey, Hash.GetHandle(), final, 0, pbData, pdwDataLen, dwBufLen))
\r
565 return AtlHresultFromLastError();
\r
571 inline HRESULT CCryptKey::Decrypt(BOOL final, BYTE * pbData, DWORD * pdwDataLen, CCryptHash &Hash) throw()
\r
573 ATLASSUME(m_hKey != NULL);
\r
575 if (!::CryptDecrypt(m_hKey, Hash.GetHandle(), final, 0, pbData, pdwDataLen))
\r
577 return AtlHresultFromLastError();
\r
583 inline HRESULT CCryptKey::Encrypt(
\r
584 const BYTE * pbPlainText,
\r
585 DWORD dwPlainTextLen,
\r
586 BYTE * pbCipherText,
\r
587 DWORD * pdwCipherTextLen,
\r
588 CCryptHash &Hash) throw()
\r
590 ATLASSUME(m_hKey != NULL);
\r
592 if (*pdwCipherTextLen < dwPlainTextLen)
\r
593 return ERROR_MORE_DATA;
\r
595 Checked::memcpy_s(pbCipherText, dwPlainTextLen, pbPlainText, dwPlainTextLen);
\r
596 DWORD dwSize = dwPlainTextLen;
\r
597 if (!::CryptEncrypt(m_hKey, Hash.GetHandle(), TRUE, 0, pbCipherText, &dwSize, *pdwCipherTextLen))
\r
599 return AtlHresultFromLastError();
\r
602 *pdwCipherTextLen = dwSize;
\r
607 inline HRESULT CCryptKey::Decrypt(
\r
608 const BYTE * pbCipherText,
\r
609 DWORD dwCipherTextLen,
\r
610 BYTE * pbPlainText,
\r
611 DWORD * pdwPlainTextLen,
\r
612 CCryptHash &Hash) throw()
\r
614 ATLASSUME(m_hKey != NULL);
\r
616 if (*pdwPlainTextLen < dwCipherTextLen)
\r
617 return ERROR_MORE_DATA;
\r
619 Checked::memcpy_s(pbPlainText, dwCipherTextLen, pbCipherText, dwCipherTextLen);
\r
620 DWORD dwSize = dwCipherTextLen;
\r
621 if (!::CryptDecrypt(m_hKey, Hash.GetHandle(), TRUE, 0, pbPlainText, &dwSize))
\r
623 return AtlHresultFromLastError();
\r
626 *pdwPlainTextLen = dwSize;
\r
630 inline HRESULT CCryptKey::EncryptString(
\r
631 LPCTSTR szPlainText,
\r
632 BYTE * pbCipherText,
\r
633 DWORD * pdwCipherTextLen,
\r
634 CCryptHash &Hash) throw()
\r
636 DWORD dwSize = ((DWORD)_tcslen(szPlainText) + 1) * sizeof(TCHAR);
\r
637 return Encrypt((BYTE *)szPlainText, dwSize, pbCipherText, pdwCipherTextLen, Hash);
\r
640 inline HRESULT CCryptKey::ExportSimpleBlob(
\r
644 DWORD * pdwDataLen) throw()
\r
646 ATLASSUME(m_hKey != NULL);
\r
648 if (!CryptExportKey(m_hKey, ExpKey.GetHandle(), SIMPLEBLOB, dwFlags, pbData, pdwDataLen))
\r
650 return AtlHresultFromLastError();
\r
655 inline HRESULT CCryptKey::ExportPublicKeyBlob(
\r
659 DWORD * pdwDataLen) throw()
\r
661 ATLASSUME(m_hKey != NULL);
\r
663 if (!CryptExportKey(m_hKey, ExpKey.GetHandle(), PUBLICKEYBLOB, dwFlags, pbData, pdwDataLen))
\r
665 return AtlHresultFromLastError();
\r
670 inline HRESULT CCryptKey::ExportPrivateKeyBlob(
\r
674 DWORD * pdwDataLen) throw()
\r
676 ATLASSUME(m_hKey != NULL);
\r
678 if (!CryptExportKey(m_hKey, ExpKey.GetHandle(), PRIVATEKEYBLOB, dwFlags, pbData, pdwDataLen))
\r
680 return AtlHresultFromLastError();
\r
685 inline HRESULT CCryptKey::GetParam(DWORD dwParam, BYTE * pbData, DWORD * pdwDataLen, DWORD dwFlags) throw()
\r
687 ATLASSUME(m_hKey != NULL);
\r
689 if (!CryptGetKeyParam(m_hKey, dwParam, pbData, pdwDataLen, dwFlags))
\r
691 return AtlHresultFromLastError();
\r
696 inline HRESULT CCryptKey::SetParam(DWORD dwParam, BYTE * pbData, DWORD dwFlags) throw()
\r
698 ATLASSUME(m_hKey != NULL);
\r
700 if (!CryptSetKeyParam(m_hKey, dwParam, pbData, dwFlags))
\r
702 return AtlHresultFromLastError();
\r
707 inline HRESULT CCryptKey::GetAlgId(ALG_ID * pAlgId) throw()
\r
709 DWORD dwSize = sizeof(DWORD);
\r
710 return GetParam(KP_ALGID, (BYTE *)pAlgId, &dwSize);
\r
713 inline HRESULT CCryptKey::SetAlgId(ALG_ID AlgId, DWORD dwFlags) throw()
\r
715 return SetParam(KP_ALGID, (BYTE *)&AlgId, dwFlags);
\r
718 inline HRESULT CCryptKey::GetBlockLength(DWORD * pdwBlockLen) throw()
\r
720 DWORD dwSize = sizeof(DWORD);
\r
721 return GetParam(KP_BLOCKLEN, (BYTE *)pdwBlockLen, &dwSize);
\r
724 inline HRESULT CCryptKey::GetKeyLength(DWORD * pdwKeyLen) throw()
\r
726 DWORD dwSize = sizeof(DWORD);
\r
727 return GetParam(KP_KEYLEN, (BYTE *)pdwKeyLen, &dwSize);
\r
730 inline HRESULT CCryptKey::GetSalt(BYTE * pbSalt, DWORD * pdwLength) throw()
\r
732 return GetParam(KP_SALT, pbSalt, pdwLength);
\r
735 inline HRESULT CCryptKey::SetSalt(BYTE * pbSalt) throw()
\r
737 return SetParam(KP_SALT, pbSalt);
\r
740 inline HRESULT CCryptKey::SetSaltEx(_CRYPTOAPI_BLOB * pBlobSalt) throw()
\r
742 return SetParam(KP_SALT_EX, (BYTE *)pBlobSalt);
\r
745 inline HRESULT CCryptKey::GetPermissions(DWORD * pdwPerms) throw()
\r
747 DWORD dwSize = sizeof(DWORD);
\r
748 return GetParam(KP_PERMISSIONS, (BYTE *)pdwPerms, &dwSize);
\r
751 inline HRESULT CCryptKey::SetPermissions(DWORD dwPerms) throw()
\r
753 return SetParam(KP_PERMISSIONS, (BYTE *)&dwPerms);
\r
756 inline HRESULT CCryptKey::GetP(BYTE * pbP, DWORD * pdwLength) throw()
\r
758 return GetParam(KP_P, (BYTE *)pbP, pdwLength);
\r
761 inline HRESULT CCryptKey::SetP(_CRYPTOAPI_BLOB * pBlobP) throw()
\r
763 return SetParam(KP_P, (BYTE *)pBlobP);
\r
766 inline HRESULT CCryptKey::SetP(BYTE * pbP, DWORD dwLength) throw()
\r
768 _CRYPTOAPI_BLOB blob = { dwLength, pbP };
\r
769 return SetParam(KP_P, (BYTE *)&blob);
\r
772 inline HRESULT CCryptKey::GetQ(BYTE * pbQ, DWORD * pdwLength) throw()
\r
774 return GetParam(KP_Q, (BYTE *)pbQ, pdwLength);
\r
777 inline HRESULT CCryptKey::SetQ(_CRYPTOAPI_BLOB * pBlobQ) throw()
\r
779 return SetParam(KP_Q, (BYTE *)pBlobQ);
\r
782 inline HRESULT CCryptKey::SetQ(BYTE * pbQ, DWORD dwLength) throw()
\r
784 _CRYPTOAPI_BLOB blob = { dwLength, pbQ };
\r
785 return SetParam(KP_Q, (BYTE *)&blob);
\r
788 inline HRESULT CCryptKey::GetG(BYTE * pbG, DWORD * pdwLength) throw()
\r
790 return GetParam(KP_G, (BYTE *)pbG, pdwLength);
\r
793 inline HRESULT CCryptKey::SetG(_CRYPTOAPI_BLOB * pBlobG) throw()
\r
795 return SetParam(KP_G, (BYTE *)pBlobG);
\r
798 inline HRESULT CCryptKey::SetG(BYTE * pbG, DWORD dwLength) throw()
\r
800 _CRYPTOAPI_BLOB blob = { dwLength, pbG };
\r
801 return SetParam(KP_G, (BYTE *)&blob);
\r
804 inline HRESULT CCryptKey::SetX() throw()
\r
806 return SetParam(KP_X, NULL);
\r
809 inline HRESULT CCryptKey::GetEffKeyLen(DWORD * pdwEffKeyLen) throw()
\r
811 DWORD dwSize = sizeof(DWORD);
\r
812 return GetParam(KP_EFFECTIVE_KEYLEN, (BYTE *)pdwEffKeyLen, &dwSize);
\r
815 inline HRESULT CCryptKey::SetEffKeyLen(DWORD dwEffKeyLen) throw()
\r
817 return SetParam(KP_EFFECTIVE_KEYLEN, (BYTE *)&dwEffKeyLen);
\r
820 inline HRESULT CCryptKey::GetPadding(DWORD * pdwPadding) throw()
\r
822 DWORD dwSize = sizeof(DWORD);
\r
823 return GetParam(KP_PADDING, (BYTE *)pdwPadding, &dwSize);
\r
826 inline HRESULT CCryptKey::SetPadding(DWORD dwPadding) throw()
\r
828 return SetParam(KP_PADDING, (BYTE *)&dwPadding);
\r
831 inline HRESULT CCryptKey::GetIV(BYTE * pbIV, DWORD * pdwLength) throw()
\r
833 return GetParam(KP_IV, pbIV, pdwLength);
\r
836 inline HRESULT CCryptKey::SetIV(BYTE * pbIV) throw()
\r
838 return SetParam(KP_IV, pbIV);
\r
841 inline HRESULT CCryptKey::GetMode(DWORD * pdwMode) throw()
\r
843 DWORD dwSize = sizeof(DWORD);
\r
844 return GetParam(KP_MODE, (BYTE *)pdwMode, &dwSize);
\r
847 inline HRESULT CCryptKey::SetMode(DWORD dwMode) throw()
\r
849 return SetParam(KP_MODE, (BYTE *)&dwMode);
\r
852 inline HRESULT CCryptKey::GetModeBits(DWORD * pdwModeBits) throw()
\r
854 DWORD dwSize = sizeof(DWORD);
\r
855 return GetParam(KP_MODE_BITS, (BYTE *)pdwModeBits, &dwSize);
\r
858 inline HRESULT CCryptKey::SetModeBits(DWORD dwModeBits) throw()
\r
860 return SetParam(KP_MODE_BITS, (BYTE *)&dwModeBits);
\r
863 inline HRESULT CCryptDerivedKey::Initialize(
\r
867 DWORD dwFlags) throw()
\r
869 ATLASSUME(m_hKey == NULL);
\r
871 if (!CryptDeriveKey(Prov.GetHandle(), algid, Hash.GetHandle(), dwFlags, &m_hKey))
\r
873 return AtlHresultFromLastError();
\r
878 inline HRESULT CCryptRandomKey::Initialize(CCryptProv &Prov, ALG_ID algid, DWORD dwFlags) throw()
\r
880 ATLASSUME(m_hKey == NULL);
\r
882 if (!CryptGenKey(Prov.GetHandle(), algid, dwFlags, &m_hKey))
\r
884 return AtlHresultFromLastError();
\r
890 inline HRESULT CCryptUserExKey::Initialize(CCryptProv &Prov) throw()
\r
892 ATLASSUME(m_hKey == NULL);
\r
894 if (!CryptGetUserKey(Prov.GetHandle(), AT_KEYEXCHANGE, &m_hKey))
\r
896 return AtlHresultFromLastError();
\r
901 inline HRESULT CCryptUserExKey::Create(CCryptProv &Prov) throw()
\r
903 ATLASSUME(m_hKey == NULL);
\r
905 if (!CryptGenKey(Prov.GetHandle(), AT_KEYEXCHANGE, 0, &m_hKey))
\r
907 return AtlHresultFromLastError();
\r
912 inline HRESULT CCryptUserSigKey::Initialize(CCryptProv &Prov) throw()
\r
914 ATLASSUME(m_hKey == NULL);
\r
916 if (!CryptGetUserKey(Prov.GetHandle(), AT_SIGNATURE, &m_hKey))
\r
918 return AtlHresultFromLastError();
\r
923 inline HRESULT CCryptUserSigKey::Create(CCryptProv &Prov) throw()
\r
925 ATLASSUME(m_hKey == NULL);
\r
927 if (!CryptGenKey(Prov.GetHandle(), AT_SIGNATURE, 0, &m_hKey))
\r
929 return AtlHresultFromLastError();
\r
934 inline HRESULT CCryptImportKey::Initialize(
\r
939 DWORD dwFlags) throw()
\r
941 ATLASSUME(m_hKey == NULL);
\r
943 if (!CryptImportKey(Prov.GetHandle(), pbData, dwDataLen, PubKey.GetHandle(), dwFlags, &m_hKey))
\r
945 return AtlHresultFromLastError();
\r
950 inline HRESULT CCryptKeyedHash::Initialize(
\r
954 DWORD dwFlags) throw()
\r
956 ATLASSUME(m_hHash == NULL);
\r
958 if (!CryptCreateHash(Prov.GetHandle(), Algid, Key.GetHandle(), dwFlags, &m_hHash))
\r
960 return AtlHresultFromLastError();
\r
965 inline HRESULT CCryptMD5Hash::Initialize(CCryptProv &Prov, LPCTSTR szText) throw()
\r
967 ATLASSUME(m_hHash == NULL);
\r
969 if (!CryptCreateHash(Prov.GetHandle(), CALG_MD5, 0, 0, &m_hHash))
\r
971 return AtlHresultFromLastError();
\r
975 return AddString(szText);
\r
979 inline HRESULT CCryptMD4Hash::Initialize(CCryptProv &Prov, LPCTSTR szText) throw()
\r
981 ATLASSUME(m_hHash == NULL);
\r
983 if (!CryptCreateHash(Prov.GetHandle(), CALG_MD4, 0, 0, &m_hHash))
\r
985 return AtlHresultFromLastError();
\r
988 return AddString(szText);
\r
992 inline HRESULT CCryptMD2Hash::Initialize(CCryptProv &Prov, LPCTSTR szText) throw()
\r
994 ATLASSUME(m_hHash == NULL);
\r
996 if (!CryptCreateHash(Prov.GetHandle(), CALG_MD2, 0, 0, &m_hHash))
\r
998 return AtlHresultFromLastError();
\r
1001 return AddString(szText);
\r
1005 inline HRESULT CCryptSHAHash::Initialize(CCryptProv &Prov, LPCTSTR szText) throw()
\r
1007 ATLASSUME(m_hHash == NULL);
\r
1009 if (!CryptCreateHash(Prov.GetHandle(), CALG_SHA, 0, 0, &m_hHash))
\r
1011 return AtlHresultFromLastError();
\r
1014 return AddString(szText);
\r
1018 inline HRESULT CCryptHMACHash::Initialize(CCryptProv &Prov, CCryptKey &Key, LPCTSTR szText) throw()
\r
1020 ATLASSUME(m_hHash == NULL);
\r
1022 if (!CryptCreateHash(Prov.GetHandle(), CALG_HMAC, Key.GetHandle(), 0, &m_hHash))
\r
1024 return AtlHresultFromLastError();
\r
1027 return AddString(szText);
\r
1032 inline HRESULT CCryptMACHash::Initialize(CCryptProv &Prov, CCryptKey &Key, LPCTSTR szText) throw()
\r
1034 ATLASSUME(m_hHash == NULL);
\r
1036 if (!CryptCreateHash(Prov.GetHandle(), CALG_MAC, Key.GetHandle(), 0, &m_hHash))
\r
1038 return AtlHresultFromLastError();
\r
1041 return AddString(szText);
\r
1046 inline HRESULT CCryptSSL3SHAMD5Hash::Initialize(CCryptProv &Prov, CCryptKey &Key, LPCTSTR szText) throw()
\r
1048 ATLASSUME(m_hHash == NULL);
\r
1050 if (!CryptCreateHash(Prov.GetHandle(), CALG_SSL3_SHAMD5, Key.GetHandle(), 0, &m_hHash))
\r
1052 return AtlHresultFromLastError();
\r
1055 return AddString(szText);
\r
1060 }; // namespace ATL
\r
1062 #endif //__ATLCRYPT_INL__
\r