Leaked source code of windows server 2003
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.

154 lines
5.8 KiB

  1. //+----------------------------------------------------------------------------
  2. //
  3. // File: cryptfnc.h
  4. //
  5. // Module: CMSECURE.LIB
  6. //
  7. // Synopsis: Definition for the cryptfnc class that provides
  8. // an easy to use interface to the CryptoAPI.
  9. //
  10. // Copyright (c) 1996-1999 Microsoft Corporation
  11. //
  12. // Author: AshishS Created 12/03/96
  13. // henryt modified for CM 5/21/97
  14. //
  15. //+----------------------------------------------------------------------------
  16. #ifndef _CRYPTFNC_INC_
  17. #define _CRYPTFNC_INC_
  18. #include <stdio.h>
  19. #include <stdarg.h>
  20. #include <windows.h>
  21. #include <wincrypt.h>
  22. #include "cmuufns.h"
  23. #include "cmsecure.h"
  24. #include "cmdebug.h"
  25. //#include "cmutil.h"
  26. //************************************************************************
  27. // define's
  28. //************************************************************************
  29. #define CRYPT_FNC_NO_ERROR 0
  30. #define CRYPT_FNC_INIT_NOT_CALLED 1
  31. #define CRYPT_FNC_INTERNAL_ERROR 2
  32. #define CRYPT_FNC_BAD_KEY 3
  33. #define CRYPT_FNC_INSUFFICIENT_BUFFER 4
  34. #define CRYPT_FNC_OUT_OF_MEMORY 5
  35. #define DEFAULT_CRYPTO_EXTRA_BUFFER_SIZE 256
  36. #define CM_CRYPTO_CONTAINER TEXT("CM Crypto Container")
  37. //************************************************************************
  38. // Typedefs for Advapi Linkage
  39. //************************************************************************
  40. typedef BOOL (WINAPI* pfnCryptAcquireContextSpec)(HCRYPTPROV *, LPCTSTR, LPCTSTR, DWORD, DWORD);
  41. typedef BOOL (WINAPI* pfnCryptCreateHashSpec)(HCRYPTPROV, ALG_ID, HCRYPTKEY, DWORD, HCRYPTHASH *);
  42. typedef BOOL (WINAPI* pfnCryptDecryptSpec)(HCRYPTKEY, HCRYPTHASH, BOOL, DWORD, BYTE *, DWORD *);
  43. typedef BOOL (WINAPI* pfnCryptDeriveKeySpec)(HCRYPTPROV, ALG_ID, HCRYPTHASH, DWORD, HCRYPTKEY *);
  44. typedef BOOL (WINAPI* pfnCryptDestroyHashSpec)(HCRYPTHASH);
  45. typedef BOOL (WINAPI* pfnCryptDestroyKeySpec)(HCRYPTKEY);
  46. typedef BOOL (WINAPI* pfnCryptEncryptSpec)(HCRYPTKEY, HCRYPTHASH, BOOL, DWORD, BYTE *, DWORD *, DWORD);
  47. typedef BOOL (WINAPI* pfnCryptHashDataSpec)(HCRYPTHASH, CONST BYTE *, DWORD, DWORD);
  48. typedef BOOL (WINAPI* pfnCryptReleaseContextSpec)(HCRYPTPROV , ULONG_PTR);
  49. typedef BOOL (WINAPI* pfnCryptGenRandomSpec)(HCRYPTPROV, DWORD, BYTE*);
  50. typedef struct _Advapi32LinkageStruct {
  51. HINSTANCE hInstAdvApi32;
  52. union {
  53. struct {
  54. pfnCryptAcquireContextSpec pfnCryptAcquireContext;
  55. pfnCryptCreateHashSpec pfnCryptCreateHash;
  56. pfnCryptDecryptSpec pfnCryptDecrypt;
  57. pfnCryptDeriveKeySpec pfnCryptDeriveKey;
  58. pfnCryptDestroyHashSpec pfnCryptDestroyHash;
  59. pfnCryptDestroyKeySpec pfnCryptDestroyKey;
  60. pfnCryptEncryptSpec pfnCryptEncrypt;
  61. pfnCryptHashDataSpec pfnCryptHashData;
  62. pfnCryptReleaseContextSpec pfnCryptReleaseContext;
  63. pfnCryptGenRandomSpec pfnCryptGenRandom;
  64. };
  65. void *apvPfn[11]; // The size of apvPfn[] should always be 1 size bigger than
  66. // the number of functions.
  67. };
  68. } Advapi32LinkageStruct;
  69. //************************************************************************
  70. // function prototypes
  71. //************************************************************************
  72. class CCryptFunctions
  73. {
  74. protected:
  75. HCRYPTPROV m_hProv;
  76. Advapi32LinkageStruct m_AdvApiLink;
  77. BOOL m_fnCryptAcquireContext(HCRYPTPROV *phProv, LPCSTR pszContainer, LPCSTR pszProvider,
  78. DWORD dwProvType, DWORD dwFlags);
  79. BOOL m_fnCryptCreateHash(HCRYPTPROV hProv, ALG_ID Algid, HCRYPTKEY hKey,
  80. DWORD dwFlags, HCRYPTHASH *phHash);
  81. BOOL m_fnCryptDecrypt(HCRYPTKEY hKey, HCRYPTHASH hHash, BOOL Final, DWORD dwFlags,
  82. BYTE *pbData, DWORD *pdwDataLen);
  83. BOOL m_fnCryptDeriveKey(HCRYPTPROV hProv, ALG_ID Algid, HCRYPTHASH hBaseData,
  84. DWORD dwFlags, HCRYPTKEY *phKey);
  85. BOOL m_fnCryptDestroyHash(HCRYPTHASH hHash);
  86. BOOL m_fnCryptDestroyKey(HCRYPTKEY hKey);
  87. BOOL m_fnCryptEncrypt(HCRYPTKEY hKey, HCRYPTHASH hHash, BOOL Final, DWORD dwFlags,
  88. BYTE *pbData, DWORD *pdwDataLen, DWORD dwBufLen);
  89. BOOL m_fnCryptHashData(HCRYPTHASH hHash, CONST BYTE *pbData, DWORD dwDataLen, DWORD dwFlags);
  90. BOOL m_fnCryptReleaseContext(HCRYPTPROV hProv, ULONG_PTR dwFlags);
  91. BOOL m_pfnCryptGenRandom(HCRYPTPROV hProv, DWORD dwLen, BYTE* pbBuffer);
  92. public:
  93. CCryptFunctions();
  94. ~CCryptFunctions();
  95. BOOL GenerateSessionKeyFromPassword(
  96. HCRYPTKEY *phKey, // location to store the session key
  97. LPTSTR pszPassword, // password to generate the session key from
  98. DWORD dwEncKeyLen); // how many bits of encryption
  99. BOOL InitCrypt();
  100. BOOL EncryptDataWithKey(
  101. LPTSTR pszKey,
  102. PBYTE pbData,
  103. DWORD dwDataLength,
  104. PBYTE *ppbEncryptedData,
  105. DWORD *pdwEncryptedBufferLen,
  106. PFN_CMSECUREALLOC pfnAlloc,
  107. PFN_CMSECUREFREE pfnFree,
  108. DWORD dwEncKeyLen);
  109. DWORD DecryptDataWithKey(
  110. LPTSTR pszKey,
  111. PBYTE pbEncryptedData,
  112. DWORD dwEncrytedDataLen,
  113. PBYTE *ppbData,
  114. DWORD *pdwDataBufferLength,
  115. PFN_CMSECUREALLOC pfnAlloc,
  116. PFN_CMSECUREFREE pfnFree,
  117. DWORD dwEncKeyLen);
  118. BOOL GenerateRandomKey(PBYTE pbData, DWORD cbData);
  119. };
  120. #endif // _CRYPTFNC_INC_