Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

382 lines
11 KiB

  1. // This is a part of the Active Template Library.
  2. // Copyright (C) 1996-2001 Microsoft Corporation
  3. // All rights reserved.
  4. //
  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.
  10. #ifndef __ATLCRYPT_H__
  11. #define __ATLCRYPT_H__
  12. #pragma once
  13. #include <wincrypt.h>
  14. namespace ATL
  15. {
  16. class CCryptKey;
  17. class CCryptProv
  18. {
  19. protected:
  20. HCRYPTPROV m_hProv;
  21. public:
  22. CCryptProv() throw();
  23. CCryptProv( const CCryptProv& prov ) throw();
  24. explicit CCryptProv( HCRYPTPROV hProv, BOOL bTakeOwnership = FALSE ) throw();
  25. ~CCryptProv() throw();
  26. CCryptProv& operator=( const CCryptProv& prov ) throw();
  27. HRESULT AddRef() throw();
  28. void Attach( HCRYPTPROV hProv, BOOL bTakeOwnership = FALSE ) throw();
  29. HCRYPTPROV Detach() throw();
  30. HRESULT Release() throw();
  31. HRESULT Initialize(DWORD dwProviderType = PROV_RSA_FULL,
  32. LPCTSTR szContainer = NULL, LPCTSTR szProvider = MS_DEF_PROV,
  33. DWORD dwFlags = 0) throw();
  34. HRESULT InitVerifyContext(DWORD dwProviderType = PROV_RSA_FULL,
  35. LPCTSTR szProvider = MS_DEF_PROV, DWORD dwFlags = 0) throw();
  36. HRESULT InitCreateKeySet(DWORD dwProviderType = PROV_RSA_FULL,
  37. LPCTSTR szContainer = NULL, LPCTSTR szProvider = MS_DEF_PROV,
  38. DWORD dwFlags = 0) throw();
  39. HRESULT DeleteKeySet(DWORD dwProviderType = PROV_RSA_FULL,
  40. LPCTSTR szContainer = NULL, LPCTSTR szProvider = MS_DEF_PROV,
  41. DWORD dwFlags = 0) throw();
  42. HRESULT Uninitialize();
  43. HRESULT GetParam(DWORD dwParam, BYTE * pbData, DWORD * pdwDataLen, DWORD dwFlags = 0) throw();
  44. HRESULT SetParam( DWORD dwParam, BYTE* pbData, DWORD dwFlags = 0) throw();
  45. HRESULT GetName(LPSTR szBuf, DWORD * pdwLength) throw();
  46. HRESULT GetContainer(LPSTR szBuf, DWORD * pdwLength) throw();
  47. HRESULT GetImpType(DWORD * pdwImpType) throw();
  48. HRESULT GetVersion(DWORD * pdwVersion) throw();
  49. HRESULT GetProvType(DWORD * pdwType) throw();
  50. HRESULT GetSecurityDesc(SECURITY_INFORMATION * pSecInfo) throw();
  51. HRESULT SetSecurityDesc(SECURITY_INFORMATION SecInfo) throw();
  52. HRESULT GenRandom(ULONG nLength, BYTE* pbBuffer ) throw();
  53. /*
  54. * No longer exist:
  55. HRESULT GetUniqueContainer(LPSTR szBuf, DWORD * pdwLength)
  56. HRESULT GetSigKeySizeInc(DWORD * pdwInc);
  57. HRESULT GetKeyxKeySizeInc(DWORD * pdwInc);
  58. */
  59. inline HCRYPTPROV GetHandle() throw()
  60. {
  61. return m_hProv;
  62. }
  63. }; // class CCryptProv
  64. // class CCryptHash
  65. // Provides base functionality of hashes.
  66. class CCryptHash
  67. {
  68. protected:
  69. HCRYPTHASH m_hHash;
  70. public:
  71. CCryptHash() throw();
  72. CCryptHash( const CCryptHash& hash ) throw();
  73. explicit CCryptHash( HCRYPTHASH hHash, BOOL bTakeOwnership = FALSE ) throw();
  74. ~CCryptHash() throw();
  75. void Attach( HCRYPTHASH hHash, BOOL bTakeOwnership = FALSE ) throw();
  76. void Destroy() throw();
  77. HCRYPTHASH Detach() throw();
  78. HCRYPTHASH Duplicate() const throw();
  79. HRESULT Uninitialize() throw();
  80. HRESULT Detach(HCRYPTHASH * phHash) throw();
  81. HRESULT AddData(const BYTE * pbData, DWORD dwDataLen, DWORD dwFlags = 0) throw();
  82. HRESULT AddString(LPCTSTR szData, DWORD dwFlags = 0) throw();
  83. HRESULT GetParam(DWORD dwParam, BYTE * pbData, DWORD * pdwDataLen, DWORD dwFlags = 0) throw();
  84. HRESULT SetParam(DWORD dwParam, BYTE * pbData, DWORD dwFlags = 0) throw();
  85. HRESULT GetAlgId(ALG_ID * pAlgId) throw();
  86. HRESULT GetSize(DWORD * pdwSize) throw();
  87. HRESULT GetValue(BYTE * pBuf, DWORD * pdwSize) throw();
  88. HRESULT SetValue(BYTE * pBuf) throw();
  89. HRESULT Sign(
  90. BYTE * pbSignature,
  91. DWORD * pdwSigLen,
  92. DWORD dwFlags = 0,
  93. DWORD dwKeySpec = AT_SIGNATURE) throw();
  94. HRESULT VerifySignature(
  95. const BYTE * pbSignature,
  96. DWORD pdwSignLen,
  97. CCryptKey &PubKey,
  98. DWORD dwFlags = 0) throw();
  99. inline HCRYPTHASH GetHandle()
  100. {
  101. return m_hHash;
  102. }
  103. static CCryptHash EmptyHash;
  104. }; // class CCryptHash
  105. // class CCryptKey
  106. // Provides the functionality for cryptographic keys, i.e. encrypting, decrypting.
  107. class CCryptKey
  108. {
  109. protected:
  110. HCRYPTKEY m_hKey;
  111. public:
  112. CCryptKey() throw();
  113. CCryptKey( const CCryptKey& key ) throw();
  114. explicit CCryptKey( HCRYPTKEY hKey, BOOL bTakeOwnership = FALSE ) throw();
  115. ~CCryptKey() throw();
  116. void Attach( HCRYPTKEY hKey, BOOL bTakeOwnership = FALSE ) throw();
  117. void Destroy() throw();
  118. HCRYPTKEY Detach() throw();
  119. HCRYPTKEY Duplicate() const throw();
  120. HRESULT Uninitialize() throw();
  121. HRESULT Encrypt(
  122. BOOL final,
  123. BYTE * pbData,
  124. DWORD * pdwDataLen,
  125. DWORD dwBufLen,
  126. CCryptHash &Hash = CCryptHash::EmptyHash) throw();
  127. HRESULT Encrypt(
  128. const BYTE * pbPlainText,
  129. DWORD dwPlainTextLen,
  130. BYTE * pbCipherText,
  131. DWORD * pdwCipherTextLen,
  132. CCryptHash &Hash = CCryptHash::EmptyHash) throw();
  133. HRESULT Decrypt(
  134. BOOL final,
  135. BYTE * pbData,
  136. DWORD * pdwDataLen,
  137. CCryptHash &Hash = CCryptHash::EmptyHash) throw();
  138. HRESULT Decrypt(
  139. const BYTE * pbCipherText,
  140. DWORD dwCipherTextLen,
  141. BYTE * pbPlainText,
  142. DWORD * pdwPlainTextLen,
  143. CCryptHash &Hash = CCryptHash::EmptyHash) throw();
  144. HRESULT EncryptString(
  145. LPCTSTR szPlainText,
  146. BYTE * pbCipherText,
  147. DWORD * pdwCipherTextLen,
  148. CCryptHash &Hash = CCryptHash::EmptyHash) throw();
  149. HRESULT ExportSimpleBlob(
  150. CCryptKey &ExpKey,
  151. DWORD dwFlags,
  152. BYTE * pbData,
  153. DWORD * pdwDataLen) throw();
  154. HRESULT ExportPublicKeyBlob(
  155. CCryptKey &ExpKey,
  156. DWORD dwFlags,
  157. BYTE * pbData,
  158. DWORD * pdwDataLen) throw();
  159. HRESULT ExportPrivateKeyBlob(
  160. CCryptKey &ExpKey,
  161. DWORD dwFlags,
  162. BYTE * pbData,
  163. DWORD * pdwDataLen) throw();
  164. HRESULT GetParam(DWORD dwParam, BYTE * pbData, DWORD * pdwDataLen, DWORD dwFlags = 0) throw();
  165. HRESULT SetParam(DWORD dwParam, BYTE * pbData, DWORD dwFlags = 0) throw();
  166. HRESULT GetAlgId(ALG_ID * pAlgId) throw();
  167. HRESULT SetAlgId(ALG_ID AlgId, DWORD dwFlags) throw();
  168. HRESULT GetBlockLength(DWORD * pdwBlockLen) throw();
  169. HRESULT GetKeyLength(DWORD * pdwKeyLen) throw();
  170. HRESULT GetSalt(BYTE * pbSalt, DWORD * pdwLength) throw();
  171. HRESULT SetSalt(BYTE * pbSalt) throw();
  172. HRESULT SetSaltEx(_CRYPTOAPI_BLOB * pBlobSalt) throw();
  173. HRESULT GetPermissions(DWORD * pdwPerms) throw();
  174. HRESULT SetPermissions(DWORD dwPerms) throw();
  175. HRESULT GetP(BYTE * pbP, DWORD * pdwLength) throw();
  176. HRESULT SetP(_CRYPTOAPI_BLOB * pBlobP) throw();
  177. HRESULT SetP(BYTE * pbP, DWORD dwLength) throw();
  178. HRESULT GetQ(BYTE * pbQ, DWORD * pdwLength) throw();
  179. HRESULT SetQ(_CRYPTOAPI_BLOB * pBlobQ) throw();
  180. HRESULT SetQ(BYTE * pbQ, DWORD dwLength) throw();
  181. HRESULT GetG(BYTE * pbG, DWORD * pdwLength) throw();
  182. HRESULT SetG(_CRYPTOAPI_BLOB * pBlobG) throw();
  183. HRESULT SetG(BYTE * pbG, DWORD dwLength) throw();
  184. HRESULT SetX() throw();
  185. HRESULT GetEffKeyLen(DWORD * pdwEffKeyLen) throw();
  186. HRESULT SetEffKeyLen(DWORD dwEffKeyLen) throw();
  187. HRESULT GetPadding(DWORD * pdwPadding) throw();
  188. HRESULT SetPadding(DWORD dwPadding) throw();
  189. HRESULT GetIV(BYTE * pbIV, DWORD * pdwLength) throw();
  190. HRESULT SetIV(BYTE * pbIV) throw();
  191. HRESULT GetMode(DWORD * pdwMode) throw();
  192. HRESULT SetMode(DWORD dwMode) throw();
  193. HRESULT GetModeBits(DWORD * pdwModeBits) throw();
  194. HRESULT SetModeBits(DWORD dwModeBits) throw();
  195. inline HCRYPTKEY GetHandle() throw()
  196. {
  197. return m_hKey;
  198. }
  199. static CCryptKey EmptyKey;
  200. }; // class CCryptKey
  201. // Specific instances of Keys and Hashes
  202. // class CCryptDerivedKey
  203. // A key that is derived from a hashed password. Two keys derived
  204. // from the same password will be identical.
  205. class CCryptDerivedKey : public CCryptKey
  206. {
  207. public:
  208. HRESULT Initialize(
  209. CCryptProv &Prov,
  210. CCryptHash &Hash,
  211. ALG_ID algid = CALG_RC4,
  212. DWORD dwFlags = CRYPT_EXPORTABLE) throw();
  213. }; // class CCryptDerivedKey
  214. // class CCryptRandomKey
  215. // A randomly generated key. Can be used internally by a program
  216. // to protect data during execution, or can be exported with Crypt.Export
  217. // REVIEW: Currently it is possible to pass in AT_KEYEXCHANGE or AT_SIGNATURE
  218. // for algid, but these two will generate keys for the current key set, and
  219. // the resulting handle can only be used for exporting and importing keys or
  220. // signing hashes.
  221. class CCryptRandomKey : public CCryptKey
  222. {
  223. public:
  224. HRESULT Initialize(
  225. CCryptProv &Prov,
  226. ALG_ID algid = CALG_RC4,
  227. DWORD dwFlags = CRYPT_EXPORTABLE) throw();
  228. }; // class CCryptRandomKey
  229. // class CCryptUserExKey
  230. // Obtains the user's key exchange key pair.
  231. class CCryptUserExKey : public CCryptKey
  232. {
  233. public:
  234. HRESULT Initialize(CCryptProv &Prov) throw();
  235. HRESULT Create(CCryptProv &Prov) throw();
  236. }; // class CCryptUserExKey
  237. // class CCryptUserSigKey
  238. // Obtains the user's signature key pair
  239. class CCryptUserSigKey : public CCryptKey
  240. {
  241. public:
  242. HRESULT Initialize(CCryptProv &Prov) throw();
  243. HRESULT Create(CCryptProv &Prov) throw();
  244. }; // class CCryptUserSigKey
  245. // class CCryptImportKey
  246. // Forms a key from an imported key blob
  247. class CCryptImportKey : public CCryptKey
  248. {
  249. public:
  250. HRESULT Initialize(
  251. CCryptProv &Prov,
  252. BYTE * pbData,
  253. DWORD dwDataLen,
  254. CCryptKey &PubKey,
  255. DWORD dwFlags) throw();
  256. }; // class CCryptImportKey
  257. // class CCryptHash
  258. // A generic hash that may or may not take a key.
  259. // HMAC and MAC are examples or keyed hashes, while
  260. // MD5 and SHA are examples of unkeyed hashes. This
  261. // class is for additional hashes that may be added
  262. // by future Cryptographic Service Providers.
  263. class CCryptKeyedHash : public CCryptHash
  264. {
  265. public:
  266. HRESULT Initialize(CCryptProv &Prov, ALG_ID Algid, CCryptKey &Key, DWORD dwFlags) throw();
  267. }; // class CCryptKeyedHash
  268. // class CCryptMD5Hash
  269. // RSA's MD5 hash (RSA's most recent hash as of 9/7/99);
  270. class CCryptMD5Hash : public CCryptHash
  271. {
  272. public:
  273. HRESULT Initialize(CCryptProv &Prov, LPCTSTR szText = NULL) throw();
  274. }; // class CCryptMD5Hash
  275. // class CCryptMD4Hash
  276. // RSA's MD4 hash
  277. class CCryptMD4Hash : public CCryptHash
  278. {
  279. public:
  280. HRESULT Initialize(CCryptProv &Prov, LPCTSTR szText = NULL) throw();
  281. }; // class CCryptMD4Hash
  282. // class CCryptMD2Hash
  283. // RSA's MD2 hash
  284. class CCryptMD2Hash : public CCryptHash
  285. {
  286. public:
  287. HRESULT Initialize(CCryptProv &Prov, LPCTSTR szText = NULL) throw();
  288. }; // class CCryptMD2Hash
  289. // class CCryptSHAHash
  290. // The Secure Hash Algorithm hash, from NIST and NSA. Technically, SHA-1.
  291. class CCryptSHAHash : public CCryptHash
  292. {
  293. public:
  294. HRESULT Initialize(CCryptProv &Prov, LPCTSTR szText = NULL) throw();
  295. }; // class CCryptSHAHash
  296. // The Secure Hash Algorithm, from NIST and NSA. Identical to CCryptSHA
  297. typedef CCryptSHAHash CCryptSHA1Hash;
  298. // class CCryptHMACHash
  299. // Hash-base Message Authentication Code keyed hash
  300. class CCryptHMACHash : public CCryptHash
  301. {
  302. public:
  303. HRESULT Initialize(CCryptProv &Prov, CCryptKey &Key, LPCTSTR szText = NULL) throw();
  304. }; // class CCryptHMACHash
  305. // class CCryptMACHash
  306. // Message Authentication Code keyed hash. Believed to be less secure than HMAC
  307. class CCryptMACHash : public CCryptHash
  308. {
  309. public:
  310. HRESULT Initialize(CCryptProv &Prov, CCryptKey &Key, LPCTSTR szText = NULL) throw();
  311. }; // class CCryptMACHash
  312. // class CCryptSSL3SHAMD4Hash
  313. // Hash algorithm used by Secure Socket Layer
  314. class CCryptSSL3SHAMD4Hash : public CCryptHash
  315. {
  316. public:
  317. HRESULT Initialize(CCryptProv &Prov, CCryptKey &Key, LPCTSTR szText = NULL) throw();
  318. }; // class CCryptSSl3SHAMD4Hash
  319. }; // namespace ATL
  320. #include <atlcrypt.inl>
  321. #endif // __ATLCRYPT_H__