1 // This is a part of the Active Template Library.
2 // Copyright (C) Microsoft Corporation
3 // All rights reserved.
5 // This source code is only intended as a supplement to the
6 // Active Template Library Reference and related
7 // electronic documentation provided with the library.
8 // See these sources for detailed information regarding the
9 // Active Template Library product.
11 #ifndef __ATLCRYPT_H__
12 #define __ATLCRYPT_H__
16 #include <atlchecked.h>
20 #pragma pack(push,_ATL_PACKING)
33 CCryptProv( const CCryptProv
& prov
) throw();
34 explicit CCryptProv( HCRYPTPROV hProv
, BOOL bTakeOwnership
= FALSE
) throw();
35 ~CCryptProv() throw();
37 CCryptProv
& operator=( const CCryptProv
& prov
) throw();
39 HRESULT
AddRef() throw();
40 void Attach( HCRYPTPROV hProv
, BOOL bTakeOwnership
= FALSE
) throw();
41 HCRYPTPROV
Detach() throw();
42 HRESULT
Release() throw();
45 HRESULT
Initialize(DWORD dwProviderType
= PROV_RSA_FULL
,
46 LPCTSTR szContainer
= NULL
, LPCTSTR szProvider
= MS_DEF_PROV
,
47 DWORD dwFlags
= 0) throw();
48 HRESULT
InitVerifyContext(DWORD dwProviderType
= PROV_RSA_FULL
,
49 LPCTSTR szProvider
= MS_DEF_PROV
, DWORD dwFlags
= 0) throw();
50 HRESULT
InitCreateKeySet(DWORD dwProviderType
= PROV_RSA_FULL
,
51 LPCTSTR szContainer
= NULL
, LPCTSTR szProvider
= MS_DEF_PROV
,
52 DWORD dwFlags
= 0) throw();
54 HRESULT
DeleteKeySet(DWORD dwProviderType
= PROV_RSA_FULL
,
55 LPCTSTR szContainer
= NULL
, LPCTSTR szProvider
= MS_DEF_PROV
,
56 DWORD dwFlags
= 0) throw();
58 HRESULT
Uninitialize();
60 HRESULT
GetParam(DWORD dwParam
, BYTE
* pbData
, DWORD
* pdwDataLen
, DWORD dwFlags
= 0) throw();
61 HRESULT
SetParam( DWORD dwParam
, BYTE
* pbData
, DWORD dwFlags
= 0) throw();
62 HRESULT
GetName(__out_ecount_part_z(*pdwLength
, *pdwLength
) LPSTR szBuf
, __inout DWORD
* pdwLength
) throw();
63 HRESULT
GetContainer(__out_ecount_part_z(*pdwLength
, *pdwLength
) LPSTR szBuf
, __inout DWORD
* pdwLength
) throw();
64 HRESULT
GetImpType(DWORD
* pdwImpType
) throw();
65 HRESULT
GetVersion(DWORD
* pdwVersion
) throw();
66 HRESULT
GetProvType(DWORD
* pdwType
) throw();
67 HRESULT
GetSecurityDesc(SECURITY_INFORMATION
* pSecInfo
) throw();
68 HRESULT
SetSecurityDesc(SECURITY_INFORMATION SecInfo
) throw();
70 HRESULT
GenRandom(ULONG nLength
, BYTE
* pbBuffer
) throw();
72 inline HCRYPTPROV
GetHandle() throw()
76 }; // class CCryptProv
80 // Provides base functionality of hashes.
88 CCryptHash( const CCryptHash
& hash
) throw();
89 explicit CCryptHash( HCRYPTHASH hHash
, BOOL bTakeOwnership
= FALSE
) throw();
90 ~CCryptHash() throw();
92 void Attach( HCRYPTHASH hHash
, BOOL bTakeOwnership
= FALSE
) throw();
93 void Destroy() throw();
94 HCRYPTHASH
Detach() throw();
95 HCRYPTHASH
Duplicate() const throw();
97 HRESULT
Uninitialize() throw();
98 HRESULT
Detach(HCRYPTHASH
* phHash
) throw();
99 HRESULT
AddData(const BYTE
* pbData
, DWORD dwDataLen
, DWORD dwFlags
= 0) throw();
100 HRESULT
AddString(LPCTSTR szData
, DWORD dwFlags
= 0) throw();
101 HRESULT
GetParam(DWORD dwParam
, BYTE
* pbData
, DWORD
* pdwDataLen
, DWORD dwFlags
= 0) throw();
102 HRESULT
SetParam(DWORD dwParam
, BYTE
* pbData
, DWORD dwFlags
= 0) throw();
103 HRESULT
GetAlgId(ALG_ID
* pAlgId
) throw();
104 HRESULT
GetSize(DWORD
* pdwSize
) throw();
105 HRESULT
GetValue(BYTE
* pBuf
, DWORD
* pdwSize
) throw();
106 HRESULT
SetValue(BYTE
* pBuf
) throw();
111 DWORD dwKeySpec
= AT_SIGNATURE
) throw();
112 HRESULT
VerifySignature(
113 const BYTE
* pbSignature
,
116 DWORD dwFlags
= 0) throw();
118 inline HCRYPTHASH
GetHandle()
122 static CCryptHash EmptyHash
;
124 }; // class CCryptHash
127 // Provides the functionality for cryptographic keys, i.e. encrypting, decrypting.
135 CCryptKey( const CCryptKey
& key
) throw();
136 explicit CCryptKey( HCRYPTKEY hKey
, BOOL bTakeOwnership
= FALSE
) throw();
137 ~CCryptKey() throw();
139 void Attach( HCRYPTKEY hKey
, BOOL bTakeOwnership
= FALSE
) throw();
140 void Destroy() throw();
141 HCRYPTKEY
Detach() throw();
142 HCRYPTKEY
Duplicate() const throw();
144 HRESULT
Uninitialize() throw();
150 CCryptHash
&Hash
= CCryptHash::EmptyHash
) throw();
153 const BYTE
* pbPlainText
,
154 DWORD dwPlainTextLen
,
156 DWORD
* pdwCipherTextLen
,
157 CCryptHash
&Hash
= CCryptHash::EmptyHash
) throw();
163 CCryptHash
&Hash
= CCryptHash::EmptyHash
) throw();
165 const BYTE
* pbCipherText
,
166 DWORD dwCipherTextLen
,
168 DWORD
* pdwPlainTextLen
,
169 CCryptHash
&Hash
= CCryptHash::EmptyHash
) throw();
170 HRESULT
EncryptString(
173 DWORD
* pdwCipherTextLen
,
174 CCryptHash
&Hash
= CCryptHash::EmptyHash
) throw();
175 HRESULT
ExportSimpleBlob(
179 DWORD
* pdwDataLen
) throw();
180 HRESULT
ExportPublicKeyBlob(
184 DWORD
* pdwDataLen
) throw();
185 HRESULT
ExportPrivateKeyBlob(
189 DWORD
* pdwDataLen
) throw();
190 HRESULT
GetParam(DWORD dwParam
, BYTE
* pbData
, DWORD
* pdwDataLen
, DWORD dwFlags
= 0) throw();
191 HRESULT
SetParam(DWORD dwParam
, BYTE
* pbData
, DWORD dwFlags
= 0) throw();
192 HRESULT
GetAlgId(ALG_ID
* pAlgId
) throw();
193 HRESULT
SetAlgId(ALG_ID AlgId
, DWORD dwFlags
) throw();
194 HRESULT
GetBlockLength(DWORD
* pdwBlockLen
) throw();
195 HRESULT
GetKeyLength(DWORD
* pdwKeyLen
) throw();
196 HRESULT
GetSalt(BYTE
* pbSalt
, DWORD
* pdwLength
) throw();
197 HRESULT
SetSalt(BYTE
* pbSalt
) throw();
198 HRESULT
SetSaltEx(_CRYPTOAPI_BLOB
* pBlobSalt
) throw();
199 HRESULT
GetPermissions(DWORD
* pdwPerms
) throw();
200 HRESULT
SetPermissions(DWORD dwPerms
) throw();
201 HRESULT
GetP(BYTE
* pbP
, DWORD
* pdwLength
) throw();
202 HRESULT
SetP(_CRYPTOAPI_BLOB
* pBlobP
) throw();
203 HRESULT
SetP(BYTE
* pbP
, DWORD dwLength
) throw();
204 HRESULT
GetQ(BYTE
* pbQ
, DWORD
* pdwLength
) throw();
205 HRESULT
SetQ(_CRYPTOAPI_BLOB
* pBlobQ
) throw();
206 HRESULT
SetQ(BYTE
* pbQ
, DWORD dwLength
) throw();
207 HRESULT
GetG(BYTE
* pbG
, DWORD
* pdwLength
) throw();
208 HRESULT
SetG(_CRYPTOAPI_BLOB
* pBlobG
) throw();
209 HRESULT
SetG(BYTE
* pbG
, DWORD dwLength
) throw();
210 HRESULT
SetX() throw();
211 HRESULT
GetEffKeyLen(DWORD
* pdwEffKeyLen
) throw();
212 HRESULT
SetEffKeyLen(DWORD dwEffKeyLen
) throw();
213 HRESULT
GetPadding(DWORD
* pdwPadding
) throw();
214 HRESULT
SetPadding(DWORD dwPadding
) throw();
215 HRESULT
GetIV(BYTE
* pbIV
, DWORD
* pdwLength
) throw();
216 HRESULT
SetIV(BYTE
* pbIV
) throw();
217 HRESULT
GetMode(DWORD
* pdwMode
) throw();
218 HRESULT
SetMode(DWORD dwMode
) throw();
219 HRESULT
GetModeBits(DWORD
* pdwModeBits
) throw();
220 HRESULT
SetModeBits(DWORD dwModeBits
) throw();
222 inline HCRYPTKEY
GetHandle() throw()
227 static CCryptKey EmptyKey
;
228 }; // class CCryptKey
232 // Specific instances of Keys and Hashes
234 // class CCryptDerivedKey
235 // A key that is derived from a hashed password. Two keys derived
236 // from the same password will be identical.
237 class CCryptDerivedKey
: public CCryptKey
243 ALG_ID algid
= CALG_RC4
,
244 DWORD dwFlags
= CRYPT_EXPORTABLE
) throw();
245 }; // class CCryptDerivedKey
247 // class CCryptRandomKey
248 // A randomly generated key. Can be used internally by a program
249 // to protect data during execution, or can be exported with Crypt.Export
251 // Currently it is possible to pass in AT_KEYEXCHANGE or AT_SIGNATURE
252 // for algid, but these two will generate keys for the current key set, and
253 // the resulting handle can only be used for exporting and importing keys or
255 class CCryptRandomKey
: public CCryptKey
260 ALG_ID algid
= CALG_RC4
,
261 DWORD dwFlags
= CRYPT_EXPORTABLE
) throw();
262 }; // class CCryptRandomKey
264 // class CCryptUserExKey
265 // Obtains the user's key exchange key pair.
266 class CCryptUserExKey
: public CCryptKey
269 HRESULT
Initialize(CCryptProv
&Prov
) throw();
270 HRESULT
Create(CCryptProv
&Prov
) throw();
271 }; // class CCryptUserExKey
273 // class CCryptUserSigKey
274 // Obtains the user's signature key pair
275 class CCryptUserSigKey
: public CCryptKey
278 HRESULT
Initialize(CCryptProv
&Prov
) throw();
279 HRESULT
Create(CCryptProv
&Prov
) throw();
280 }; // class CCryptUserSigKey
282 // class CCryptImportKey
283 // Forms a key from an imported key blob
284 class CCryptImportKey
: public CCryptKey
292 DWORD dwFlags
) throw();
293 }; // class CCryptImportKey
297 // A generic hash that may or may not take a key.
298 class CCryptKeyedHash
: public CCryptHash
302 HRESULT
Initialize(CCryptProv
&Prov
, ALG_ID Algid
, CCryptKey
&Key
, DWORD dwFlags
) throw();
303 }; // class CCryptKeyedHash
305 // class CCryptMD5Hash
306 // RSA's MD5 hash (RSA's most recent hash as of 9/7/99);
307 class CCryptMD5Hash
: public CCryptHash
311 HRESULT
Initialize(CCryptProv
&Prov
, LPCTSTR szText
= NULL
) throw();
312 }; // class CCryptMD5Hash
314 // class CCryptMD4Hash
316 class CCryptMD4Hash
: public CCryptHash
320 HRESULT
Initialize(CCryptProv
&Prov
, LPCTSTR szText
= NULL
) throw();
321 }; // class CCryptMD4Hash
324 // class CCryptMD2Hash
326 class CCryptMD2Hash
: public CCryptHash
330 HRESULT
Initialize(CCryptProv
&Prov
, LPCTSTR szText
= NULL
) throw();
331 }; // class CCryptMD2Hash
334 // class CCryptSHAHash
335 // The Secure Hash Algorithm hash, from NIST and NSA. Technically, SHA-1.
336 class CCryptSHAHash
: public CCryptHash
340 HRESULT
Initialize(CCryptProv
&Prov
, LPCTSTR szText
= NULL
) throw();
341 }; // class CCryptSHAHash
343 // The Secure Hash Algorithm, from NIST and NSA. Identical to CCryptSHA
344 typedef CCryptSHAHash CCryptSHA1Hash
;
347 // class CCryptHMACHash
348 // Hash-base Message Authentication Code keyed hash
349 class CCryptHMACHash
: public CCryptHash
352 HRESULT
Initialize(CCryptProv
&Prov
, CCryptKey
&Key
, LPCTSTR szText
= NULL
) throw();
353 }; // class CCryptHMACHash
355 // class CCryptMACHash
356 // Message Authentication Code keyed hash. Believed to be less secure than HMAC
357 class CCryptMACHash
: public CCryptHash
360 HRESULT
Initialize(CCryptProv
&Prov
, CCryptKey
&Key
, LPCTSTR szText
= NULL
) throw();
361 }; // class CCryptMACHash
363 // class CCryptSSL3SHAMD5Hash
364 // Hash algorithm used by Secure Socket Layer
365 class CCryptSSL3SHAMD5Hash
: public CCryptHash
368 HRESULT
Initialize(CCryptProv
&Prov
, CCryptKey
&Key
, LPCTSTR szText
= NULL
) throw();
369 }; // class CCryptSSl3SHAMD5Hash
374 #include <atlcrypt.inl>
376 #endif // __ATLCRYPT_H__