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.

1006 lines
30 KiB

  1. /*
  2. CSPTestSuite.h
  3. This file contains typedef's and function prototypes for the CSP Test Suite.
  4. 6/16/00 dangriff created
  5. */
  6. #ifndef __CSPTESTSUITE__H__
  7. #define __CSPTESTSUITE__H__
  8. #include <windows.h>
  9. #include <wincrypt.h>
  10. #include "logging.h"
  11. #include "cspstruc.h"
  12. #include "utils.h"
  13. #define TESTVER_MAJOR_VERSION 5
  14. #define TESTVER_MINOR_VERSION 0
  15. #define TEST_APP_NAME L"CSPTestSuite"
  16. #define MINIMUM_ARGC 3
  17. #define CSP_TYPE_INVALID 0
  18. #define CSP_TYPE_RSA 1
  19. #define CSP_TYPE_DSS 2
  20. #define CSP_TYPE_ELIPTIC_CURVE 3
  21. #define CSP_TYPE_AES 4
  22. #define CLASS_INVALID 0x0
  23. #define CLASS_SIG_ONLY 0x00000001
  24. #define CLASS_SIG_KEYX 0x00000002
  25. #define CLASS_FULL 0x00000004
  26. #define CLASS_SCHANNEL 0x00000008
  27. #define CLASS_OPTIONAL 0x00000010
  28. static DWORD g_rgCspClasses [] = {
  29. CLASS_SIG_ONLY, CLASS_SIG_KEYX, CLASS_FULL, CLASS_OPTIONAL
  30. };
  31. #define TEST_LEVEL_CSP 0x1
  32. #define TEST_LEVEL_PROV 0x2
  33. #define TEST_LEVEL_HASH 0x4
  34. #define TEST_LEVEL_KEY 0x8
  35. #define TEST_LEVEL_CONTAINER 0x10
  36. static DWORD g_rgTestLevels [] = {
  37. TEST_LEVEL_CSP, TEST_LEVEL_PROV, TEST_LEVEL_HASH, TEST_LEVEL_KEY, TEST_LEVEL_CONTAINER
  38. };
  39. //
  40. // Parameters used by the test cases
  41. //
  42. #define BUFFER_SIZE 2048
  43. #define TEST_MAX_RSA_KEYSIZE 1024
  44. #define TEST_INVALID_FLAG -1
  45. #define TEST_INVALID_HANDLE -1
  46. #define TEST_INVALID_POINTER -1
  47. #define GENRANDOM_BYTE_COUNT 1000
  48. #define TEST_HASH_DATA L"Test hash data"
  49. #define TEST_DECRYPT_DATA L"Test decrypt data"
  50. #define HASH_LENGTH_SHA1 20
  51. #define HASH_LENGTH_MD5 16
  52. #define TEST_CIPHER_LENGTH_RC4 256
  53. #define TEST_RC2_DATA_LEN 8
  54. #define TEST_RC2_BUFFER_LEN 16
  55. #define TEST_CRYPTACQUIRECONTEXT_CONTAINER L"TestingCAC"
  56. #define TEST_CONTAINER L"TestSuiteContainer"
  57. #define TEST_CONTAINER_2 L"TestSuiteContainer2"
  58. #define TEST_SALT_BYTE 0xcd
  59. #define TEST_IV_BYTE 0xcd
  60. #define TEST_SALT_LEN 20
  61. #define TEST_EFFECTIVE_KEYLEN 56
  62. #define TEST_MODE_BITS 16
  63. //
  64. // ------------------------
  65. // Known Error Lookup Table
  66. // ------------------------
  67. //
  68. static KNOWN_ERROR_INFO g_KnownErrorTable[] =
  69. {
  70. //
  71. // Known errors in Positive test cases
  72. //
  73. /*
  74. KnownErrorID Maj Min SP SP Actual Error Level
  75. Rev Rev Maj Min
  76. */
  77. { KNOWN_CRYPTGETPROVPARAM_MAC, 5, 0, 0, 0, CSP_ERROR_WARNING },
  78. { KNOWN_CRYPTGETPROVPARAM_PPNAME, 5, 1, 0, 0, CSP_ERROR_WARNING },
  79. { KNOWN_CRYPTSETKEYPARAM_EXPORT, 5, 1, 0, 0, CSP_ERROR_WARNING },
  80. { KNOWN_CRYPTGETKEYPARAM_SALT, 5, 0, 0, 0, CSP_ERROR_WARNING },
  81. { KNOWN_CRYPTGENKEY_SALTDES, 5, 1, 0, 0, CSP_ERROR_WARNING },
  82. //
  83. // Known errors in Negative test cases
  84. //
  85. { KNOWN_CRYPTACQUIRECONTEXT_NULLPHPROV, 5, 1, 0, 0, CSP_ERROR_WARNING },
  86. { KNOWN_CRYPTACQUIRECONTEXT_BADFLAGS, 5, 1, 0, 0, CSP_ERROR_WARNING },
  87. { KNOWN_ERRORINVALIDHANDLE, 5, 1, 0, 0, CSP_ERROR_WARNING },
  88. { KNOWN_CRYPTGETPROVPARAM_MOREDATA, 5, 1, 0, 0, CSP_ERROR_WARNING },
  89. { KNOWN_CRYPTSETPROVPARAM_RNG, 5, 1, 0, 0, CSP_ERROR_WARNING },
  90. { KNOWN_CRYPTSETPROVPARAM_BADFLAGS, 5, 1, 0, 0, CSP_ERROR_WARNING },
  91. { KNOWN_CRYPTCREATEHASH_BADKEY, 5, 1, 0, 0, CSP_ERROR_WARNING },
  92. { KNOWN_CRYPTIMPORTKEY_BADFLAGS, 5, 1, 0, 0, CSP_ERROR_WARNING },
  93. { KNOWN_CRYPTIMPORTKEY_BADALGID, 5, 1, 0, 0, CSP_ERROR_WARNING },
  94. { KNOWN_NTEBADKEY, 5, 1, 0, 0, CSP_ERROR_WARNING },
  95. { KNOWN_NTEBADHASH, 5, 1, 0, 0, CSP_ERROR_WARNING },
  96. { KNOWN_CRYPTGENKEY_SILENTCONTEXT, 5, 0, 0, 0, CSP_ERROR_WARNING },
  97. //
  98. // Known errors in Scenario test cases
  99. //
  100. { KNOWN_TESTDECRYPTPROC_3DES112, 5, 0, 0, 0, CSP_ERROR_WARNING }
  101. };
  102. //
  103. // Function: GetKnownErrorValue
  104. // Purpose: Lookup a known error in the g_KnownErrorTable. The return value
  105. // is the actual error value that should be applied to the current error.
  106. // 1) If the error is not in the table, return the dwCurrentErrorLevel param
  107. // 2) If the error is found in the table, get the error level that should be
  108. // applied to that error from the table.
  109. //
  110. // If the version information for
  111. // the known error matches that of this NT system, return the error level as
  112. // is. Otherwise, increment the error level, since it is expected to have
  113. // been fixed by now.
  114. //
  115. DWORD GetKnownErrorValue(
  116. IN KNOWN_ERROR_ID KnownErrorID,
  117. IN DWORD dwCurrentErrorLevel);
  118. //
  119. // Struct: CSPTYPEMAP
  120. // Purpose: This struct is used to map external provider types (a subset of those
  121. // defined in wincrypt.h) to internal provider classes.
  122. //
  123. typedef struct _CSPTYPEMAP
  124. {
  125. DWORD dwExternalProvType;
  126. DWORD dwProvType;
  127. DWORD dwCSPClass;
  128. LPWSTR pwszExternalProvType;
  129. } CSPTYPEMAP, *PCSPTYPEMAP;
  130. static CSPTYPEMAP g_CspTypeTable [] =
  131. {
  132. /*
  133. ExternalProvType InternalProvType InternalCSPClass External Type Description
  134. */
  135. { PROV_RSA_FULL, CSP_TYPE_RSA, CLASS_FULL, L"PROV_RSA_FULL" },
  136. { PROV_RSA_SIG, CSP_TYPE_RSA, CLASS_SIG_ONLY, L"PROV_RSA_SIG" },
  137. { PROV_DSS, CSP_TYPE_DSS, CLASS_SIG_ONLY, L"PROV_DSS" },
  138. { PROV_DSS_DH, CSP_TYPE_DSS, CLASS_FULL, L"PROV_DSS_DH" },
  139. { PROV_RSA_AES, CSP_TYPE_AES, CLASS_FULL, L"PROV_RSA_AES" }
  140. };
  141. typedef struct _ALGID_TABLE
  142. {
  143. DWORD dwCSPClass;
  144. ALG_ID ai;
  145. } ALGID_TABLE, *PALGID_TABLE;
  146. static ALGID_TABLE g_RequiredAlgs_RSA[] =
  147. {
  148. { CLASS_SIG_ONLY | CLASS_SIG_KEYX | CLASS_FULL, CALG_RSA_SIGN },
  149. { CLASS_SIG_ONLY | CLASS_SIG_KEYX | CLASS_FULL, CALG_SHA1 },
  150. { CLASS_SIG_ONLY | CLASS_SIG_KEYX | CLASS_FULL, CALG_MD5 },
  151. { CLASS_SIG_KEYX | CLASS_FULL, CALG_RSA_KEYX },
  152. { CLASS_FULL, CALG_RC2 },
  153. { CLASS_FULL, CALG_RC4 }
  154. };
  155. static ALGID_TABLE g_OtherKnownAlgs_RSA[] =
  156. {
  157. { CLASS_SIG_ONLY | CLASS_SIG_KEYX | CLASS_FULL, CALG_MD2 },
  158. { CLASS_SIG_ONLY | CLASS_SIG_KEYX | CLASS_FULL, CALG_MD4 },
  159. { CLASS_FULL, CALG_MAC },
  160. { CLASS_FULL, CALG_HMAC },
  161. { CLASS_FULL, CALG_DES },
  162. { CLASS_FULL, CALG_3DES_112 },
  163. { CLASS_FULL, CALG_3DES }
  164. };
  165. static ALGID_TABLE g_RequiredAlgs_AES[] =
  166. {
  167. { CLASS_SIG_ONLY | CLASS_SIG_KEYX | CLASS_FULL, CALG_RSA_SIGN },
  168. { CLASS_SIG_ONLY | CLASS_SIG_KEYX | CLASS_FULL, CALG_SHA1 },
  169. { CLASS_SIG_ONLY | CLASS_SIG_KEYX | CLASS_FULL, CALG_MD5 },
  170. { CLASS_SIG_KEYX | CLASS_FULL, CALG_RSA_KEYX },
  171. { CLASS_FULL, CALG_RC2 },
  172. { CLASS_FULL, CALG_RC4 },
  173. { CLASS_FULL, CALG_AES_128 },
  174. { CLASS_FULL, CALG_AES_192 },
  175. { CLASS_FULL, CALG_AES_256 }
  176. };
  177. static ALGID_TABLE g_OtherKnownAlgs_AES[] =
  178. {
  179. { CLASS_SIG_ONLY | CLASS_SIG_KEYX | CLASS_FULL, CALG_MD2 },
  180. { CLASS_SIG_ONLY | CLASS_SIG_KEYX | CLASS_FULL, CALG_MD4 },
  181. { CLASS_FULL, CALG_MAC },
  182. { CLASS_FULL, CALG_HMAC },
  183. { CLASS_FULL, CALG_DES },
  184. { CLASS_FULL, CALG_3DES_112 },
  185. { CLASS_FULL, CALG_3DES }
  186. };
  187. //
  188. // Function: IsRequiredAlg
  189. // Purpose: Search the RequiredAlgs array for the specified algorithm, ai.
  190. // If ai is found, return TRUE, otherwise return FALSE.
  191. //
  192. BOOL IsRequiredAlg(ALG_ID ai, DWORD dwInternalProvType);
  193. //
  194. // Function: IsKnownAlg
  195. // Purpose: Search the OtherKnownAlgs array for the specified algorithm, ai.
  196. // If ai is found, return TRUE, otherwise return FALSE.
  197. //
  198. BOOL IsKnownAlg(ALG_ID ai, DWORD dwInternalProvType);
  199. //
  200. // ------------------
  201. // CSP Test Functions
  202. // ------------------
  203. //
  204. BOOL TestBuildAlgList(PCSPINFO pCSPInfo);
  205. BOOL PositiveAcquireContextTests(PCSPINFO pCSPInfo);
  206. BOOL NegativeAcquireContextTests(PCSPINFO pCSPInfo);
  207. BOOL PositiveGetProvParamTests(PCSPINFO pCSPInfo);
  208. BOOL NegativeGetProvParamTests(PCSPINFO pCSPInfo);
  209. BOOL PositiveSetProvParamTests(PCSPINFO pCSPInfo);
  210. BOOL NegativeSetProvParamTests(PCSPINFO pCSPInfo);
  211. BOOL PositiveReleaseContextTests(PCSPINFO pCSPInfo);
  212. BOOL NegativeReleaseContextTests(PCSPINFO pCSPInfo);
  213. BOOL PositiveGenRandomTests(PCSPINFO pCSPInfo);
  214. BOOL NegativeGenRandomTests(PCSPINFO pCSPInfo);
  215. BOOL PositiveCreateHashTests(PCSPINFO pCSPInfo);
  216. BOOL NegativeCreateHashTests(PCSPINFO pCSPInfo);
  217. BOOL PositiveDestroyHashTests(PCSPINFO pCSPInfo);
  218. BOOL NegativeDestroyHashTests(PCSPINFO pCSPInfo);
  219. BOOL PositiveGetHashParamTests(PCSPINFO pCSPInfo);
  220. BOOL NegativeGetHashParamTests(PCSPINFO pCSPInfo);
  221. BOOL PositiveHashDataTests(PCSPINFO pCSPInfo);
  222. BOOL NegativeHashDataTests(PCSPINFO pCSPInfo);
  223. BOOL PositiveSetHashParamTests(PCSPINFO pCSPInfo);
  224. BOOL NegativeSetHashParamTests(PCSPINFO pCSPInfo);
  225. BOOL PositiveDestroyKeyTests(PCSPINFO pCSPInfo);
  226. BOOL NegativeDestroyKeyTests(PCSPINFO pCSPInfo);
  227. BOOL PositiveGetKeyParamTests(PCSPINFO pCSPInfo);
  228. BOOL NegativeGetKeyParamTests(PCSPINFO pCSPInfo);
  229. BOOL PositiveSetKeyParamTests(PCSPINFO pCSPInfo);
  230. BOOL NegativeSetKeyParamTests(PCSPINFO pCSPInfo);
  231. BOOL PositiveDecryptTests(PCSPINFO pCSPInfo);
  232. BOOL NegativeDecryptTests(PCSPINFO pCSPInfo);
  233. BOOL PositiveEncryptTests(PCSPINFO pCSPInfo);
  234. BOOL NegativeEncryptTests(PCSPINFO pCSPInfo);
  235. BOOL PositiveGenKeyTests(PCSPINFO pCSPInfo);
  236. BOOL NegativeGenKeyTests(PCSPINFO pCSPInfo);
  237. BOOL PositiveDeriveKeyTests(PCSPINFO pCSPInfo);
  238. BOOL NegativeDeriveKeyTests(PCSPINFO pCSPInfo);
  239. BOOL PositiveHashSessionKeyTests(PCSPINFO pCSPInfo);
  240. BOOL NegativeHashSessionKeyTests(PCSPINFO pCSPInfo);
  241. BOOL PositiveExportKeyTests(PCSPINFO pCSPInfo);
  242. BOOL NegativeExportKeyTests(PCSPINFO pCSPInfo);
  243. BOOL PositiveGetUserKeyTests(PCSPINFO pCSPInfo);
  244. BOOL NegativeGetUserKeyTests(PCSPINFO pCSPInfo);
  245. BOOL PositiveImportKeyTests(PCSPINFO pCSPInfo);
  246. BOOL NegativeImportKeyTests(PCSPINFO pCSPInfo);
  247. BOOL PositiveSignHashTests(PCSPINFO pCSPInfo);
  248. BOOL NegativeSignHashTests(PCSPINFO pCSPInfo);
  249. BOOL PositiveVerifySignatureTests(PCSPINFO pCSPInfo);
  250. BOOL NegativeVerifySignatureTests(PCSPINFO pCSPInfo);
  251. BOOL ScenarioDecryptTests(PCSPINFO pCSPInfo);
  252. BOOL ScenarioImportKeyTests(PCSPINFO pCSPInfo);
  253. BOOL ScenarioVerifySignatureTests(PCSPINFO pCSPInfo);
  254. BOOL ScenarioKeyExchangeTests(PCSPINFO pCSPInfo);
  255. BOOL InteropHashDataTests(PCSPINFO pCSPInfo);
  256. BOOL InteropDecryptTests(PCSPINFO pCSPInfo);
  257. BOOL InteropDeriveKeyTests(PCSPINFO pCSPInfo);
  258. BOOL InteropHashSessionKeyTests(PCSPINFO pCSPInfo);
  259. BOOL InteropKeyExchangeTests(PCSPINFO pCSPInfo);
  260. //
  261. // --------------------------
  262. // CSP Test Function Matrices
  263. // --------------------------
  264. //
  265. //
  266. // Struct: TESTTABLEENTRY
  267. // Purpose: This struct will be used to store the static table below, which
  268. // will map test functions that should be called for each relevant
  269. // [ Test Class , Test Level ] combination.
  270. //
  271. typedef struct _TESTTABLEENTRY
  272. {
  273. PFN_CSPTEST pTestFunc;
  274. DWORD dwClassSigOnly;
  275. DWORD dwClassSigKeyX;
  276. DWORD dwClassFull;
  277. DWORD dwClassOptional;
  278. API_NAME ApiName;
  279. DWORD dwTestType;
  280. } TESTTABLEENTRY, *PTESTTABLEENTRY;
  281. //
  282. // A table of test functions and the
  283. // CSP Classes and Test Levels to which each function applies.
  284. //
  285. static TESTTABLEENTRY g_TestFunctionMappings[] =
  286. {
  287. /*
  288. Function- CLASS_SIG_ONLY CLASS_SIG_KEYX CLASS_FULL CLASS_OPTIONAL dwAPI dwTestType
  289. name
  290. */
  291. // TEST_LEVEL_CSP major group
  292. { PositiveAcquireContextTests, TEST_LEVEL_CSP | TEST_LEVEL_CONTAINER, 0, 0, 0, API_CRYPTACQUIRECONTEXT, TEST_CASES_POSITIVE },
  293. { NegativeAcquireContextTests, TEST_LEVEL_CSP | TEST_LEVEL_CONTAINER, 0, 0, 0, API_CRYPTACQUIRECONTEXT, TEST_CASES_NEGATIVE },
  294. { PositiveGetProvParamTests, TEST_LEVEL_CSP, 0, 0, 0, API_CRYPTGETPROVPARAM, TEST_CASES_POSITIVE },
  295. { NegativeGetProvParamTests, TEST_LEVEL_CSP, 0, 0, 0, API_CRYPTGETPROVPARAM, TEST_CASES_NEGATIVE },
  296. { PositiveSetProvParamTests, TEST_LEVEL_CSP, 0, 0, 0, API_CRYPTSETPROVPARAM, TEST_CASES_POSITIVE },
  297. { NegativeSetProvParamTests, TEST_LEVEL_CSP, 0, 0, 0, API_CRYPTSETPROVPARAM, TEST_CASES_NEGATIVE },
  298. { PositiveReleaseContextTests, TEST_LEVEL_CSP, 0, 0, 0, API_CRYPTRELEASECONTEXT, TEST_CASES_POSITIVE },
  299. { NegativeReleaseContextTests, TEST_LEVEL_CSP, 0, 0, 0, API_CRYPTRELEASECONTEXT, TEST_CASES_NEGATIVE },
  300. // TEST_LEVEL_PROV major group
  301. { PositiveGenRandomTests, 0, 0, 0, TEST_LEVEL_PROV, API_CRYPTGENRANDOM, TEST_CASES_POSITIVE },
  302. { NegativeGenRandomTests, 0, 0, 0, TEST_LEVEL_PROV, API_CRYPTGENRANDOM, TEST_CASES_NEGATIVE },
  303. // TEST_LEVEL_HASH major group
  304. { PositiveCreateHashTests, TEST_LEVEL_HASH, 0, 0, TEST_LEVEL_HASH, API_CRYPTCREATEHASH, TEST_CASES_POSITIVE },
  305. { NegativeCreateHashTests, TEST_LEVEL_HASH, 0, 0, TEST_LEVEL_HASH, API_CRYPTCREATEHASH, TEST_CASES_NEGATIVE },
  306. { PositiveDestroyHashTests, TEST_LEVEL_HASH, 0, 0, 0, API_CRYPTDESTROYHASH, TEST_CASES_POSITIVE },
  307. { NegativeDestroyHashTests, TEST_LEVEL_HASH, 0, 0, 0, API_CRYPTDESTROYHASH, TEST_CASES_NEGATIVE },
  308. { PositiveGetHashParamTests, TEST_LEVEL_HASH, 0, 0, 0, API_CRYPTGETHASHPARAM, TEST_CASES_POSITIVE },
  309. { NegativeGetHashParamTests, TEST_LEVEL_HASH, 0, 0, 0, API_CRYPTGETHASHPARAM, TEST_CASES_NEGATIVE },
  310. { PositiveHashDataTests, TEST_LEVEL_HASH, 0, 0, 0, API_CRYPTHASHDATA, TEST_CASES_POSITIVE },
  311. { NegativeHashDataTests, TEST_LEVEL_HASH, 0, 0, 0, API_CRYPTHASHDATA, TEST_CASES_NEGATIVE },
  312. { PositiveSetHashParamTests, TEST_LEVEL_HASH, 0, 0, 0, API_CRYPTSETHASHPARAM, TEST_CASES_POSITIVE },
  313. { NegativeSetHashParamTests, TEST_LEVEL_HASH, 0, 0, 0, API_CRYPTSETHASHPARAM, TEST_CASES_NEGATIVE },
  314. // TEST_LEVEL_KEY major group
  315. { PositiveDestroyKeyTests, TEST_LEVEL_KEY, 0, 0, 0, API_CRYPTDESTROYKEY, TEST_CASES_POSITIVE },
  316. { NegativeDestroyKeyTests, TEST_LEVEL_KEY, 0, 0, 0, API_CRYPTDESTROYKEY, TEST_CASES_NEGATIVE },
  317. { PositiveGetKeyParamTests, TEST_LEVEL_KEY, 0, 0, 0, API_CRYPTGETKEYPARAM, TEST_CASES_POSITIVE },
  318. { NegativeGetKeyParamTests, TEST_LEVEL_KEY, 0, 0, 0, API_CRYPTGETKEYPARAM, TEST_CASES_NEGATIVE },
  319. { PositiveSetKeyParamTests, TEST_LEVEL_KEY, 0, 0, 0, API_CRYPTSETKEYPARAM, TEST_CASES_POSITIVE },
  320. { NegativeSetKeyParamTests, TEST_LEVEL_KEY, 0, 0, 0, API_CRYPTSETKEYPARAM, TEST_CASES_NEGATIVE },
  321. { PositiveDecryptTests, 0, 0, TEST_LEVEL_KEY, 0, API_CRYPTDECRYPT, TEST_CASES_POSITIVE },
  322. { NegativeDecryptTests, 0, 0, TEST_LEVEL_KEY, 0, API_CRYPTDECRYPT, TEST_CASES_NEGATIVE },
  323. { PositiveEncryptTests, 0, 0, TEST_LEVEL_KEY, 0, API_CRYPTENCRYPT, TEST_CASES_POSITIVE },
  324. { NegativeEncryptTests, 0, 0, TEST_LEVEL_KEY, 0, API_CRYPTENCRYPT, TEST_CASES_NEGATIVE },
  325. { PositiveGenKeyTests, TEST_LEVEL_CONTAINER, TEST_LEVEL_CONTAINER, TEST_LEVEL_KEY, TEST_LEVEL_KEY | TEST_LEVEL_CONTAINER, API_CRYPTGENKEY, TEST_CASES_POSITIVE },
  326. { NegativeGenKeyTests, TEST_LEVEL_CONTAINER, 0, TEST_LEVEL_KEY, 0, API_CRYPTGENKEY, TEST_CASES_NEGATIVE },
  327. { PositiveDeriveKeyTests, 0, 0, 0, TEST_LEVEL_KEY | TEST_LEVEL_CONTAINER, API_CRYPTDERIVEKEY, TEST_CASES_POSITIVE },
  328. { NegativeDeriveKeyTests, 0, 0, 0, TEST_LEVEL_KEY, API_CRYPTDERIVEKEY, TEST_CASES_NEGATIVE },
  329. { PositiveHashSessionKeyTests, 0, 0, 0, TEST_LEVEL_KEY, API_CRYPTHASHSESSIONKEY, TEST_CASES_POSITIVE },
  330. { NegativeHashSessionKeyTests, 0, 0, 0, TEST_LEVEL_KEY, API_CRYPTHASHSESSIONKEY, TEST_CASES_NEGATIVE },
  331. // TEST_LEVEL_CONTAINER major group
  332. { PositiveExportKeyTests, TEST_LEVEL_CONTAINER, TEST_LEVEL_CONTAINER, 0, 0, API_CRYPTEXPORTKEY, TEST_CASES_POSITIVE },
  333. { NegativeExportKeyTests, TEST_LEVEL_CONTAINER, TEST_LEVEL_CONTAINER, 0, 0, API_CRYPTEXPORTKEY, TEST_CASES_NEGATIVE },
  334. { PositiveGetUserKeyTests, TEST_LEVEL_CONTAINER, TEST_LEVEL_CONTAINER, 0, 0, API_CRYPTGETUSERKEY, TEST_CASES_POSITIVE },
  335. { NegativeGetUserKeyTests, TEST_LEVEL_CONTAINER, TEST_LEVEL_CONTAINER, 0, 0, API_CRYPTGETUSERKEY, TEST_CASES_NEGATIVE },
  336. { PositiveImportKeyTests, TEST_LEVEL_CONTAINER, TEST_LEVEL_CONTAINER, 0, 0, API_CRYPTIMPORTKEY, TEST_CASES_POSITIVE },
  337. { NegativeImportKeyTests, TEST_LEVEL_CONTAINER, TEST_LEVEL_CONTAINER, 0, 0, API_CRYPTIMPORTKEY, TEST_CASES_NEGATIVE },
  338. { PositiveSignHashTests, TEST_LEVEL_CONTAINER, TEST_LEVEL_CONTAINER, 0, 0, API_CRYPTSIGNHASH, TEST_CASES_POSITIVE },
  339. { NegativeSignHashTests, TEST_LEVEL_CONTAINER, 0, 0, 0, API_CRYPTSIGNHASH, TEST_CASES_NEGATIVE },
  340. { PositiveVerifySignatureTests, TEST_LEVEL_CONTAINER, TEST_LEVEL_CONTAINER, 0, 0, API_CRYPTVERIFYSIGNATURE, TEST_CASES_POSITIVE },
  341. { NegativeVerifySignatureTests, TEST_LEVEL_CONTAINER, 0, 0, 0, API_CRYPTVERIFYSIGNATURE, TEST_CASES_NEGATIVE }
  342. };
  343. typedef struct _BASIC_TEST_TABLE
  344. {
  345. PFN_CSPTEST pTestFunc;
  346. LPWSTR pwszDescription;
  347. } BASIC_TEST_TABLE, *PBASIC_TEST_TABLE;
  348. //
  349. // A table of Scenario Test functions
  350. //
  351. static BASIC_TEST_TABLE g_ScenarioTestTable [] =
  352. {
  353. { TestBuildAlgList, L"Build a list a supported algorithms" },
  354. { ScenarioDecryptTests, L"Decryption/encryption test scenario" },
  355. { ScenarioImportKeyTests, L"Key import/export test scenario" },
  356. { ScenarioVerifySignatureTests, L"Data signature/verification test scenario" },
  357. { ScenarioKeyExchangeTests, L"RSA Key exchange test scenario" }
  358. };
  359. //
  360. // A table of Interop Test functions
  361. //
  362. static BASIC_TEST_TABLE g_InteropTestTable [] =
  363. {
  364. { TestBuildAlgList, L"Build a list a supported algorithms" },
  365. { InteropHashDataTests, L"Hashed data interop test" },
  366. { InteropDecryptTests, L"Decryption/encryption interop test" },
  367. { InteropDeriveKeyTests, L"Key derivation interop test" },
  368. { InteropHashSessionKeyTests, L"Hashed session key interop test" },
  369. { InteropKeyExchangeTests, L"RSA Key exchange interop test" }
  370. };
  371. //
  372. // ----------------
  373. // General Routines
  374. // ----------------
  375. //
  376. //
  377. // Function: GetNextRegisteredCSP
  378. // Purpose: In successive calls, enumerates all of the registered CSP's
  379. // on the system, if the ENUMERATE_REGISTERED_CSP flag is specified in
  380. // dwRequestedIndex. Otherwise, the provider at index dwRequestedIndex
  381. // is returned.
  382. //
  383. // Note: Do not mix enumerating all CSP's with a call using the
  384. // ENUMERATE_REGISTERED_CSP flag, or some of the CSP's may not
  385. // be enumerated.
  386. //
  387. #define ENUMERATE_REGISTERED_CSP -1
  388. DWORD GetNextRegisteredCSP(
  389. OUT LPWSTR pwszCsp,
  390. IN OUT PDWORD cbCsp,
  391. OUT PDWORD pdwProvType,
  392. IN DWORD dwRequestedIndex);
  393. //
  394. // -------------------------------
  395. // API Test Helpers
  396. // Struct's and Callback functions
  397. // -------------------------------
  398. //
  399. //
  400. // Function: TAcquire
  401. // Purpose: Call CryptAcquireContext with the supplied parameters and pass
  402. // the result to the logging routine.
  403. //
  404. BOOL TAcquire(
  405. HCRYPTPROV *phProv,
  406. LPWSTR pszContainer,
  407. LPWSTR pszProvider,
  408. DWORD dwProvType,
  409. DWORD dwFlags,
  410. PTESTCASE ptc);
  411. //
  412. // Function: TGetProv
  413. // Purpose: Call CryptGetProvParam with the supplied parameters and pass
  414. // the result to the logging routine.
  415. //
  416. BOOL TGetProv(
  417. HCRYPTPROV hProv,
  418. DWORD dwParam,
  419. BYTE *pbData,
  420. DWORD *pdwDataLen,
  421. DWORD dwFlags,
  422. PTESTCASE ptc);
  423. //
  424. // Function: TSetProv
  425. // Purpose: Call CryptSetProvParam with the supplied parameters and pass
  426. // the result to the logging routine.
  427. //
  428. BOOL TSetProv(
  429. HCRYPTPROV hProv,
  430. DWORD dwParam,
  431. BYTE *pbData,
  432. DWORD dwFlags,
  433. PTESTCASE ptc);
  434. //
  435. // Function: TRelease
  436. // Purpose: Call CryptReleaseContext with the supplied parameters and pass
  437. // the result to the logging routine.
  438. //
  439. BOOL TRelease(
  440. HCRYPTPROV hProv,
  441. DWORD dwFlags,
  442. PTESTCASE ptc);
  443. //
  444. // Function: TGenRand
  445. // Purpose: Call CryptGenRandom with the supplied parameters and pass
  446. // the result to the logging routine.
  447. //
  448. BOOL TGenRand(
  449. HCRYPTPROV hProv,
  450. DWORD dwLen,
  451. BYTE *pbBuffer,
  452. PTESTCASE ptc);
  453. //
  454. // Function: TCreateHash
  455. // Purpose: Call CryptCreateHash with the supplied parameters and pass
  456. // the result to the logging routine.
  457. //
  458. BOOL TCreateHash(
  459. HCRYPTPROV hProv,
  460. ALG_ID Algid,
  461. HCRYPTKEY hKey,
  462. DWORD dwFlags,
  463. HCRYPTHASH *phHash,
  464. PTESTCASE ptc);
  465. //
  466. // Function: TDestroyHash
  467. // Purpose: Call CryptDestroyHash with the supplied parameters and pass
  468. // the result to the logging routine.
  469. //
  470. BOOL TDestroyHash(
  471. HCRYPTHASH hHash,
  472. PTESTCASE ptc);
  473. //
  474. // Function: TDuplicateHash
  475. // Purpose: Call CryptDuplicateHash with the supplied parameters and pass
  476. // the result to the logging routine.
  477. //
  478. BOOL TDuplicateHash(
  479. HCRYPTHASH hHash,
  480. DWORD *pdwReserved,
  481. DWORD dwFlags,
  482. HCRYPTHASH *phHash,
  483. PTESTCASE ptc);
  484. //
  485. // Function: TGetHash
  486. // Purpose: Call CryptGetHashParam with the supplied parameters and pass
  487. // the result to the logging routine.
  488. //
  489. BOOL TGetHash(
  490. HCRYPTHASH hHash,
  491. DWORD dwParam,
  492. BYTE *pbData,
  493. DWORD *pdwDataLen,
  494. DWORD dwFlags,
  495. PTESTCASE ptc);
  496. //
  497. // Function: THashData
  498. // Purpose: Call CryptHashData with the supplied parameters and pass
  499. // the result to the logging routine.
  500. //
  501. BOOL THashData(
  502. HCRYPTHASH hHash,
  503. BYTE *pbData,
  504. DWORD dwDataLen,
  505. DWORD dwFlags,
  506. PTESTCASE ptc);
  507. //
  508. // Function: TSetHash
  509. // Purpose: Call CryptSetHashParam with the supplied parameters and pass
  510. // the result to the logging routine.
  511. //
  512. BOOL TSetHash(
  513. HCRYPTHASH hHash,
  514. DWORD dwParam,
  515. BYTE *pbData,
  516. DWORD dwFlags,
  517. PTESTCASE ptc);
  518. //
  519. // Function: TDecrypt
  520. // Purpose: Call CryptDecrypt with the supplied parameters and pass
  521. // the result to the logging routine.
  522. //
  523. BOOL TDecrypt(
  524. HCRYPTKEY hKey,
  525. HCRYPTHASH hHash,
  526. BOOL Final,
  527. DWORD dwFlags,
  528. BYTE *pbData,
  529. DWORD *pdwDataLen,
  530. PTESTCASE ptc);
  531. //
  532. // Function: TDeriveKey
  533. // Purpose: Call CryptDeriveKey with the supplied parameters and pass
  534. // the result to the logging routine.
  535. //
  536. BOOL TDeriveKey(
  537. HCRYPTPROV hProv,
  538. ALG_ID Algid,
  539. HCRYPTHASH hBaseData,
  540. DWORD dwFlags,
  541. HCRYPTKEY *phKey,
  542. PTESTCASE ptc);
  543. //
  544. // Function: TDestroyKey
  545. // Purpose: Call CryptDestroyKey with the supplied parameters and pass
  546. // the result to the logging routine.
  547. //
  548. BOOL TDestroyKey(
  549. HCRYPTKEY hKey,
  550. PTESTCASE ptc);
  551. //
  552. // Function: TEncrypt
  553. // Purpose: Call CryptEncrypt with the supplied parameters and pass
  554. // the result to the logging routine.
  555. //
  556. BOOL TEncrypt(
  557. HCRYPTKEY hKey,
  558. HCRYPTHASH hHash,
  559. BOOL Final,
  560. DWORD dwFlags,
  561. BYTE *pbData,
  562. DWORD *pdwDataLen,
  563. DWORD dwBufLen,
  564. PTESTCASE ptc);
  565. //
  566. // Function: TGenKey
  567. // Purpose: Call CryptGenKey with the supplied parameters and pass
  568. // the result to the logging routine.
  569. //
  570. BOOL TGenKey(
  571. HCRYPTPROV hProv,
  572. ALG_ID Algid,
  573. DWORD dwFlags,
  574. HCRYPTKEY *phKey,
  575. PTESTCASE ptc);
  576. //
  577. // Function: TGetKey
  578. // Purpose: Call CryptGetKeyParam with the supplied parameters and pass
  579. // the result to the logging routine.
  580. //
  581. BOOL TGetKey(
  582. HCRYPTKEY hKey,
  583. DWORD dwParam,
  584. BYTE *pbData,
  585. DWORD *pdwDataLen,
  586. DWORD dwFlags,
  587. PTESTCASE ptc);
  588. //
  589. // Function: THashSession
  590. // Purpose: Call CryptHashSessionKey with the supplied parameters and pass
  591. // the result to the logging routine.
  592. //
  593. BOOL THashSession(
  594. HCRYPTHASH hHash,
  595. HCRYPTKEY hKey,
  596. DWORD dwFlags,
  597. PTESTCASE ptc);
  598. //
  599. // Function: TSetKey
  600. // Purpose: Call CryptSetKeyParam with the supplied parameters and pass
  601. // the result to the logging routine.
  602. //
  603. BOOL TSetKey(
  604. HCRYPTKEY hKey,
  605. DWORD dwParam,
  606. BYTE *pbData,
  607. DWORD dwFlags,
  608. PTESTCASE ptc);
  609. //
  610. // Function: TExportKey
  611. // Purpose: Call CryptExportKey with the supplied parameters and pass
  612. // the result to the logging routine.
  613. //
  614. BOOL TExportKey(
  615. HCRYPTKEY hKey,
  616. HCRYPTKEY hExpKey,
  617. DWORD dwBlobType,
  618. DWORD dwFlags,
  619. BYTE *pbData,
  620. DWORD *pdwDataLen,
  621. PTESTCASE ptc);
  622. //
  623. // Function: TGetUser
  624. // Purpose: Call CryptGetUserKey with the supplied parameters and pass
  625. // the result to the logging routine.
  626. //
  627. BOOL TGetUser(
  628. HCRYPTPROV hProv,
  629. DWORD dwKeySpec,
  630. HCRYPTKEY *phUserKey,
  631. PTESTCASE ptc);
  632. //
  633. // Function: TImportKey
  634. // Purpose: Call CryptImportKey with the supplied parameters and pass
  635. // the result to the logging routine.
  636. //
  637. BOOL TImportKey(
  638. HCRYPTPROV hProv,
  639. BYTE *pbData,
  640. DWORD dwDataLen,
  641. HCRYPTKEY hPubKey,
  642. DWORD dwFlags,
  643. HCRYPTKEY *phKey,
  644. PTESTCASE ptc);
  645. //
  646. // Function: TSignHash
  647. // Purpose: Call CryptSignHash with the supplied parameters and pass
  648. // the result to the logging routine.
  649. //
  650. BOOL TSignHash(
  651. HCRYPTHASH hHash,
  652. DWORD dwKeySpec,
  653. LPWSTR sDescription,
  654. DWORD dwFlags,
  655. BYTE *pbSignature,
  656. DWORD *pdwSigLen,
  657. PTESTCASE ptc);
  658. //
  659. // Function: TVerifySign
  660. // Purpose: Call CryptVerifySign with the supplied parameters and pass
  661. // the result to the logging routine.
  662. //
  663. BOOL TVerifySign(
  664. HCRYPTHASH hHash,
  665. BYTE *pbSignature,
  666. DWORD dwSigLen,
  667. HCRYPTKEY hPubKey,
  668. LPWSTR sDescription,
  669. DWORD dwFlags,
  670. PTESTCASE ptc);
  671. //
  672. // Struct: TEST_CREATE_HASH_INFO
  673. // Purpose: Store data for calls to TestCreateHashProc
  674. //
  675. typedef struct _TEST_CREATE_HASH_INFO
  676. {
  677. HCRYPTPROV hProv;
  678. HCRYPTKEY hBlockCipherKey;
  679. } TEST_CREATE_HASH_INFO, *PTEST_CREATE_HASH_INFO;
  680. //
  681. // Struct: TEST_HASH_DATA_INFO
  682. // Purpose: Store data that will be common to all calls to TestHashDataProc
  683. //
  684. typedef struct _TEST_HASH_DATA_INFO
  685. {
  686. HCRYPTPROV hProv;
  687. HCRYPTPROV hInteropProv;
  688. DATA_BLOB dbBaseData;
  689. //
  690. // These parameters are only used when this structure
  691. // is passed to TestMacDataProc
  692. //
  693. PALGNODE pAlgList;
  694. HMAC_INFO HmacInfo;
  695. ALG_ID aiMac;
  696. } TEST_HASH_DATA_INFO, *PTEST_HASH_DATA_INFO;
  697. //
  698. // Function: TestHashDataProc
  699. // Purpose: Create a hash context of the algorithm supplied in the
  700. // pAlgNode parameter. Hash some test data. Verify that the hash result
  701. // is the same as what is reported by the interop CSP
  702. //
  703. BOOL TestHashDataProc(
  704. PALGNODE pAlgNode,
  705. PTESTCASE ptc,
  706. PVOID pvTestHashDataInfo);
  707. //
  708. // Struct: TEST_DECRYPT_INFO
  709. // Purpose: Store data to be passed to TestDecryptProc via
  710. // AlgListIterate.
  711. //
  712. typedef struct _TEST_DECRYPT_INFO
  713. {
  714. HCRYPTPROV hProv;
  715. HCRYPTPROV hInteropProv;
  716. //
  717. // fDecrypt
  718. // If False, test CryptEncrypt
  719. // If True, test CryptDecrypt
  720. //
  721. BOOL fDecrypt;
  722. }
  723. TEST_DECRYPT_INFO, *PTEST_DECRYPT_INFO;
  724. //
  725. // Function: TestDecryptProc
  726. // Purpose: A callback function for AlgListIterate. For each
  727. // encryption algorithm supported by the CSP under test, this
  728. // function will be called to test decryption functionality.
  729. //
  730. BOOL TestDecryptProc(
  731. PALGNODE pAlgNode,
  732. PTESTCASE ptc,
  733. PVOID pvTestDecryptInfo);
  734. //
  735. // Struct: TEST_DERIVE_KEY_INFO
  736. // Purpose: Store data to be passed to TestDeriveKeyProc via
  737. // AlgListIterate.
  738. //
  739. typedef struct _TEST_DERIVE_KEY_INFO
  740. {
  741. HCRYPTPROV hProv;
  742. HCRYPTPROV hInteropProv;
  743. } TEST_DERIVE_KEY_INFO, *PTEST_DERIVE_KEY_INFO;
  744. //
  745. // Function: TestDeriveKeyProc
  746. // Purpose: Callback function for testing CryptDeriveKey using
  747. // AlgListIterate. For each session key algorithm supported by the
  748. // target CSP, this function will be called to test CryptDeriveKey
  749. // functionality.
  750. //
  751. BOOL TestDeriveKeyProc(
  752. PALGNODE pAlgNode,
  753. PTESTCASE ptc,
  754. PVOID pvTestDeriveKeyInfo);
  755. //
  756. // Struct: TEST_GEN_KEY_INFO
  757. // Purpose: Data that must be provided to the TestGenKeyProc function.
  758. //
  759. typedef struct _TEST_GEN_KEY_INFO
  760. {
  761. HCRYPTPROV hProv;
  762. HCRYPTPROV hNotSilentProv;
  763. PCSPINFO pCSPInfo;
  764. } TEST_GEN_KEY_INFO, *PTEST_GEN_KEY_INFO;
  765. //
  766. // Function: TestGenKeyProc
  767. // Purpose: Callback function for testing CryptGenKey using
  768. // AlgListIterate. For each key algorithm supported by the target
  769. // CSP, this function will be called.
  770. //
  771. BOOL TestGenKeyProc(
  772. PALGNODE pAlgNode,
  773. PTESTCASE ptc,
  774. PVOID pvhProv);
  775. //
  776. // Struct: TEST_HASH_SESSION_KEY
  777. // Purpose: Data that must be provided to the TestHashSessionKeyProc function.
  778. //
  779. typedef struct _TEST_HASH_SESSION_KEY
  780. {
  781. HCRYPTPROV hProv;
  782. HCRYPTPROV hInteropProv;
  783. ALG_ID aiHash;
  784. } TEST_HASH_SESSION_KEY, *PTEST_HASH_SESSION_KEY;
  785. //
  786. // Function: TestHashSessionKeyProc
  787. // Purpose: Callback function for testing CryptHashSessionKey
  788. // using AlgListIterate. For each session key algorithm
  789. // supported by the target CSP, this function will be called.
  790. //
  791. BOOL TestHashSessionKeyProc(
  792. PALGNODE pAlgNode,
  793. PTESTCASE ptc,
  794. PVOID pvTestHashSessionKey);
  795. //
  796. // -----------------------------------------
  797. // Defines for testing key Import and Export
  798. // -----------------------------------------
  799. //
  800. typedef struct _KEY_EXPORT_INFO
  801. {
  802. HCRYPTPROV hProv;
  803. ALG_ID aiKey;
  804. DWORD dwKeySize;
  805. DWORD dwExportFlags;
  806. PALGNODE pAlgList;
  807. //
  808. // For PRIVATEKEYBLOB scenarios, the use of an encryption key
  809. // is optional, since exporting unencrypted PRIVATEKEYBLOB's is
  810. // supported.
  811. //
  812. BOOL fUseEncryptKey;
  813. ALG_ID aiEncryptKey;
  814. DWORD dwEncryptKeySize;
  815. } KEY_EXPORT_INFO, *PKEY_EXPORT_INFO;
  816. //
  817. // Function: TestPrivateKeyBlobProc
  818. // Purpose: Create a public/private key pair. Export the public key as a PRIVATEKEYBLOB
  819. // structure. Verify that the key blob can be successfully re-imported.
  820. //
  821. BOOL TestPrivateKeyBlobProc(
  822. PALGNODE pAlgNode,
  823. PTESTCASE ptc,
  824. PVOID pvKeyExportInfo);
  825. //
  826. // Function: TestSymmetricWrapKeyBlobProc
  827. // Purpose: Create a session key. Export the session key encrypted
  828. // with another session key. Verify that the first session key can
  829. // be re-imported.
  830. //
  831. BOOL TestSymmetricWrapKeyBlobProc(
  832. PALGNODE pAlgNode,
  833. PTESTCASE ptc,
  834. PVOID pvKeyExportInfo);
  835. //
  836. // -----------------------------------------
  837. // Defines for testing key Import and Export
  838. // -----------------------------------------
  839. //
  840. //
  841. // Struct: SIGN_HASH_INFO
  842. // Purpose: Test case data for creating and signing a hash
  843. //
  844. typedef struct _SIGN_HASH_INFO
  845. {
  846. HCRYPTPROV hProv;
  847. HCRYPTKEY hSigKey;
  848. HCRYPTKEY hExchKey;
  849. DATA_BLOB dbBaseData;
  850. } SIGN_HASH_INFO, *PSIGN_HASH_INFO;
  851. //
  852. // Function: SignAndVerifySignatureProc
  853. // Purpose: Create a hash of the requested type and hash some test data.
  854. // Sign and verify the signature of the hash with both the Signature and
  855. // Key Exchange key pairs.
  856. //
  857. BOOL SignAndVerifySignatureProc(
  858. PALGNODE pAlgNode,
  859. PTESTCASE ptc,
  860. PVOID pvSignHashInfo);
  861. //
  862. // -------------------------------------------
  863. // Defines for testing a Key Exchange scenario
  864. // -------------------------------------------
  865. //
  866. typedef struct _EXCHANGE_PROC_INFO
  867. {
  868. HCRYPTPROV hProv;
  869. HCRYPTPROV hInteropProv;
  870. ALG_ID aiHashAlg;
  871. DWORD dwPublicKeySize;
  872. DATA_BLOB dbPlainText;
  873. } EXCHANGE_PROC_INFO, *PEXCHANGE_PROC_INFO;
  874. //
  875. // Function: TestKeyExchangeProc
  876. // Purpose: Simulate a key/data exchange scenario for the specified
  877. // encryption alg.
  878. //
  879. BOOL TestKeyExchangeProc(
  880. PALGNODE pAlgNode,
  881. PTESTCASE ptc,
  882. PVOID pvExchangeProcInfo);
  883. //
  884. // ------------------
  885. // Known Hash Vectors
  886. // ------------------
  887. //
  888. // 8 bytes of test data
  889. #define KNOWN_HASH_DATA "HashThis"
  890. #define KNOWN_HASH_DATALEN 8
  891. // known result of an MD5 hash on the above buffer
  892. static BYTE g_rgbKnownMD5[] =
  893. {
  894. 0xb8, 0x2f, 0x6b, 0x11, 0x31, 0xc8, 0xec, 0xf4,
  895. 0xfe, 0x0b, 0xf0, 0x6d, 0x2a, 0xda, 0x3f, 0xc3
  896. };
  897. // known result of an SHA-1 hash on the above buffer
  898. static BYTE g_rgbKnownSHA1[] =
  899. {
  900. 0xe8, 0x96, 0x82, 0x85, 0xeb, 0xae, 0x01, 0x14,
  901. 0x73, 0xf9, 0x08, 0x45, 0xc0, 0x6a, 0x6d, 0x3e,
  902. 0x69, 0x80, 0x6a, 0x0c
  903. };
  904. #endif