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.

271 lines
7.9 KiB

  1. /* context.h */
  2. #include <windows.h>
  3. #ifndef DSSCSP_CONTEXT_H
  4. #define DSSCSP_CONTEXT_H
  5. #ifdef CSP_USE_MD5
  6. #include "md5.h"
  7. #endif
  8. #ifdef CSP_USE_SHA1
  9. #include "sha.h"
  10. #endif
  11. // definition for disabling encryption in France
  12. #define CRYPT_DISABLE_CRYPT 0x1
  13. /*********************************/
  14. /* Definitions */
  15. /*********************************/
  16. #define KEY_MAGIC 0xBADF
  17. /* State definitions */
  18. #define KEY_INIT 0x0001
  19. #define MAX_BLOCKLEN 8
  20. // types of key storage
  21. #define PROTECTED_STORAGE_KEYS 1
  22. #define PROTECTION_API_KEYS 2
  23. #define HASH_MAGIC 0xBADE
  24. /* State Flags */
  25. #define HASH_INIT 0x0001
  26. #define HASH_DATA 0x0002
  27. #define HASH_FINISH 0x0004
  28. #define MAX_HASH_LEN 20
  29. #define CRYPT_BLKLEN 8
  30. #define HMAC_DEFAULT_STRING_LEN 64
  31. #define HMAC_STARTED 1
  32. #define HMAC_FINISHED 2
  33. /*********************************/
  34. /* Structure Definitions */
  35. /*********************************/
  36. typedef struct _Key_t_ {
  37. int magic; // Magic number
  38. void *pContext;
  39. int state; // State of object
  40. ALG_ID algId; // Algorithm Id
  41. DWORD flags; // General flags associated with key
  42. void *algParams; // Parameters for algorithm
  43. uchar IV[MAX_BLOCKLEN];
  44. uchar Temp_IV[MAX_BLOCKLEN];
  45. uchar *pbKey;
  46. DWORD cbKey;
  47. uchar *pbSalt;
  48. DWORD cbSalt;
  49. BYTE *pbData;
  50. DWORD cbData;
  51. DWORD cbEffectiveKeyLen;
  52. int mode;
  53. int pad;
  54. int mode_bits;
  55. BOOL InProgress; // if key is being used
  56. BOOL fUIOnKey; // flag to indicate if UI was to be set on the key
  57. } Key_t;
  58. // Packed version of Key_t. This is used when building opaque
  59. // blobs, and is necessary to properly support WOW64 operation.
  60. typedef struct _Packed_Key_t_ {
  61. // BLOBHEADER
  62. int magic; // Magic number
  63. int state; // State of object
  64. ALG_ID algId; // Algorithm Id
  65. DWORD flags; // General flags associated with key
  66. uchar IV[MAX_BLOCKLEN];
  67. uchar Temp_IV[MAX_BLOCKLEN];
  68. DWORD cbKey;
  69. DWORD cbData;
  70. DWORD cbEffectiveKeyLen;
  71. int mode;
  72. int pad;
  73. int mode_bits;
  74. BOOL InProgress; // if key is being used
  75. BOOL fUIOnKey; // flag to indicate if UI was to be set on the key
  76. // cbKey data bytes
  77. // cbData data bytes
  78. } Packed_Key_t;
  79. typedef struct {
  80. int magic; // Magic number
  81. void *pContext; // associated context
  82. int state; // State of hash object
  83. ALG_ID algId; // Algorithm Id
  84. DWORD size; // Size of hash
  85. void *pMAC; // pointer to mac state
  86. BYTE hashval[MAX_HASH_LEN];
  87. BYTE *pbData;
  88. DWORD cbData;
  89. Key_t *pKey;
  90. BOOL fInternalKey;
  91. ALG_ID HMACAlgid;
  92. DWORD HMACState;
  93. BYTE *pbHMACInner;
  94. DWORD cbHMACInner;
  95. BYTE *pbHMACOuter;
  96. DWORD cbHMACOuter;
  97. union {
  98. #ifdef CSP_USE_MD5
  99. MD5_CTX md5;
  100. #endif // CSP_USE_MD5
  101. #ifdef CSP_USE_SHA1
  102. A_SHA_CTX sha;
  103. #endif // CSP_USE_SHA1
  104. } algData;
  105. } Hash_t;
  106. /*********************************/
  107. /* Definitions */
  108. /*********************************/
  109. #define CONTEXT_MAGIC 0xDEADBEEF
  110. #define CONTEXT_RANDOM_LENGTH 20
  111. typedef struct _PStore_Info
  112. {
  113. HINSTANCE hInst;
  114. void *pProv;
  115. GUID SigType;
  116. GUID SigSubtype;
  117. GUID ExchType;
  118. GUID ExchSubtype;
  119. LPWSTR szPrompt;
  120. DWORD cbPrompt;
  121. } PSTORE_INFO;
  122. /*********************************/
  123. /* Structure Definitions */
  124. /*********************************/
  125. typedef struct {
  126. DWORD magic; // Magic number
  127. DWORD dwProvType; // Type of provider being called as
  128. LPSTR szProvName; // Name of provider being called as
  129. BOOL fMachineKeyset; // TRUE if keyset is for machine
  130. DWORD rights; // Privileges
  131. BOOL fIsLocalSystem; // check if running as local system
  132. KEY_CONTAINER_INFO ContInfo;
  133. Key_t *pSigKey; // pointer to the DSS sig key
  134. Key_t *pKExKey; // pointer to the DH key exchange key
  135. HKEY hKeys; // Handle to registry
  136. DWORD dwEnumalgs; // index for enumerating algorithms
  137. DWORD dwEnumalgsEx; // index for enumerating algorithms
  138. DWORD dwiSubKey; // index for enumerating containers
  139. DWORD dwMaxSubKey; // max number of containers
  140. void *contextData; // Context specific data
  141. CRITICAL_SECTION CritSec; // critical section for decrypting keys
  142. HWND hWnd; // handle to window for UI
  143. PSTORE_INFO *pPStore; // pointer to PStore information
  144. LPWSTR pwszPrompt; // UI prompt to be used
  145. DWORD dwOldKeyFlags; // flags to tell how keys should be migrated
  146. DWORD dwKeysetType; // type of storage used
  147. HANDLE hRNGDriver; // handle to hardware RNG driver
  148. EXPO_OFFLOAD_STRUCT *pOffloadInfo; // info for offloading modular expo
  149. DWORD dwPolicyId; // Index into policy keylengh arrays.
  150. } Context_t;
  151. /*********************************/
  152. /* Policy Definitions */
  153. /*********************************/
  154. extern PROV_ENUMALGS_EX *g_AlgTables[];
  155. // NOTE -- These definitions must match the order of entries in g_AlgTables.
  156. #define POLICY_DSS_BASE 0 // Policy for MS_DEF_DSS_PROV
  157. #define POLICY_DSSDH_BASE 1 // Policy for MS_DEF_DSS_DH_PROV
  158. #define POLICY_DSSDH_ENHANCED 2 // Policy for MS_ENH_DSS_DH_PROV
  159. #define POLICY_DSSDH_SCHANNEL 3 // Policy for MS_DEF_DH_SCHANNEL_PROV
  160. /*********************************/
  161. /* Function Definitions */
  162. /*********************************/
  163. extern void
  164. freeContext(
  165. Context_t *pContext);
  166. extern Context_t *
  167. checkContext(
  168. HCRYPTPROV hProv);
  169. extern Context_t *
  170. allocContext(
  171. void);
  172. // Initialize a context
  173. extern DWORD
  174. initContext(
  175. IN OUT Context_t *pContext,
  176. IN DWORD dwFlags,
  177. IN DWORD dwProvType,
  178. IN LPCSTR szProvName,
  179. IN DWORD dwPolicyId);
  180. extern HCRYPTPROV
  181. AddContext(
  182. Context_t *pContext);
  183. extern HCRYPTHASH
  184. addContextHash(
  185. Context_t *pContext,
  186. Hash_t *pHash);
  187. extern Hash_t *
  188. checkContextHash(
  189. Context_t *pContext,
  190. HCRYPTHASH hHash);
  191. // Add key to context
  192. extern HCRYPTKEY
  193. addContextKey(
  194. Context_t *pContext,
  195. Key_t *pKey);
  196. // Check if key exists in context
  197. extern Key_t *
  198. checkContextKey(
  199. IN Context_t *pContext,
  200. IN HCRYPTKEY hKey);
  201. // random number generation prototype
  202. extern DWORD
  203. FIPS186GenRandom(
  204. IN HANDLE hRNGDriver,
  205. IN BYTE **ppbContextSeed,
  206. IN DWORD *pcbContextSeed,
  207. IN OUT BYTE *pb,
  208. IN DWORD cb);
  209. // Scrub sensitive data from memory
  210. extern void
  211. memnuke(
  212. volatile BYTE *pData,
  213. DWORD dwLen);
  214. #include "dh_key.h"
  215. extern DWORD EncryptPrivateKeyInMemory(
  216. IN DHKey_t *pDH,
  217. IN BOOL fSigKey);
  218. extern DWORD CopyAndDecryptPrivateKey(
  219. IN DHKey_t *pDH,
  220. IN DHKey_t *pDH_copy,
  221. IN BOOL fSigKey);
  222. #endif