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.

7134 lines
189 KiB

  1. // *****************************************************************************
  2. //
  3. // Purpose : Multithreaded stress test
  4. //
  5. // Created : arunm 03/20/96
  6. // Modified: dangriff 11/6/00
  7. //
  8. // *****************************************************************************
  9. #include <nt.h>
  10. #include <ntrtl.h>
  11. #include <nturtl.h>
  12. #include <windows.h>
  13. #include <stdlib.h>
  14. #include <stdio.h>
  15. #include <wincrypt.h>
  16. #include "strestst.h"
  17. #include <pincache.h>
  18. #include <sha2.h>
  19. // ===========================================================================
  20. int Usage(void)
  21. {
  22. printf("%s -c <CSP Index> [options]\n", APP_NAME) ;
  23. printf(" -?: This message\n") ;
  24. printf(" -n <N>: # of threads to create (Def: %d)\n", StressGetDefaultThreadCount());
  25. printf(" -t <N>: End test in N minutes (Def: never end)\n");
  26. printf(" -e: Ephemeral keys\n");
  27. printf(" -u: User-protected keys (cannot be used with -e)\n");
  28. printf(" -r: Run regression tests\n");
  29. printf(" -s: Skip PinCache attack test\n");
  30. printf(" -d: Delete all key containers\n");
  31. printf(" -a<Flags> <Container>: Call CryptAcquireContext with Flags\n");
  32. printf("\nCryptAcquireContext Flags:\n");
  33. printf(" v - CRYPT_VERIFYCONTEXT (don't specify <Container>)\n");
  34. printf(" n - CRYPT_NEWKEYSET\n");
  35. printf(" l - CRYPT_MACHINE_KEYSET\n");
  36. printf(" d - CRYPT_DELETEKEYSET\n");
  37. printf(" q - CRYPT_SILENT\n");
  38. printf(" x - create a Key Exchange keyset\n");
  39. printf(" s - create a Signature keyset\n");
  40. printf(" u - request keyset to be User Protected\n");
  41. printf(" e - request keyset to be Exportable\n");
  42. return 1;
  43. }
  44. //
  45. // Function: StressGetDefaultThreadCount
  46. //
  47. DWORD StressGetDefaultThreadCount(void)
  48. {
  49. SYSTEM_INFO SystemInfo;
  50. ZeroMemory(&SystemInfo, sizeof(SystemInfo));
  51. GetSystemInfo(&SystemInfo);
  52. return
  53. SystemInfo.dwNumberOfProcessors == 1 ?
  54. STRESS_DEFAULT_THREAD_COUNT :
  55. SystemInfo.dwNumberOfProcessors;
  56. }
  57. //
  58. // Function: MyAlloc
  59. //
  60. LPVOID MyAlloc(SIZE_T dwBytes)
  61. {
  62. return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwBytes);
  63. }
  64. //
  65. // Function: MyFree
  66. //
  67. BOOL MyFree(LPVOID lpMem)
  68. {
  69. return HeapFree(GetProcessHeap(), 0, lpMem);
  70. }
  71. //
  72. // Function: PrintBytes
  73. //
  74. #define CROW 8
  75. void PrintBytes(LPSTR pszHdr, BYTE *pb, DWORD cbSize)
  76. {
  77. ULONG cb, i;
  78. CHAR rgsz[1024];
  79. printf("\n %s, %d bytes ::\n", pszHdr, cbSize);
  80. while (cbSize > 0)
  81. {
  82. // Start every row with an extra space
  83. printf(" ");
  84. cb = min(CROW, cbSize);
  85. cbSize -= cb;
  86. for (i = 0; i < cb; i++)
  87. printf(" %02x", pb[i]);
  88. for (i = cb; i < CROW; i++)
  89. printf(" ");
  90. printf(" '");
  91. for (i = 0; i < cb; i++)
  92. {
  93. if (pb[i] >= 0x20 && pb[i] <= 0x7f)
  94. printf("%c", pb[i]);
  95. else
  96. printf(".");
  97. }
  98. printf("\n");
  99. pb += cb;
  100. }
  101. }
  102. typedef struct {
  103. DWORD dwSignFlags;
  104. DWORD dwKeySize;
  105. ALG_ID aiHash;
  106. BOOL fExpectSuccess;
  107. } SHA_SIGN_INFO, *PSHA_SIGN_INFO;
  108. DWORD DoShaSigning(PTHREAD_DATA pThreadData, PSHA_SIGN_INFO pShaSignInfo)
  109. {
  110. DWORD dwError = ERROR_SUCCESS;
  111. BOOL fSuccess = FALSE;
  112. HCRYPTKEY hKey = 0;
  113. HCRYPTHASH hHash = 0;
  114. CHAR rgHashData [] = "Hash This";
  115. DWORD cbHashData = sizeof(rgHashData);
  116. PBYTE pbSign = NULL;
  117. DWORD cbSign = 0;
  118. BOOL fPrint = pShaSignInfo->fExpectSuccess;
  119. #define TPrint(X) { if (fPrint) { printf(X); } }
  120. if (! CryptCreateHash(
  121. pThreadData->hVerifyCtx,
  122. pShaSignInfo->aiHash,
  123. 0, 0, &hHash))
  124. {
  125. TPrint("CryptCreateHash ");
  126. goto Ret;
  127. }
  128. if (! CryptHashData(
  129. hHash, (PBYTE) rgHashData, cbHashData, 0))
  130. {
  131. TPrint("CryptHashData ");
  132. goto Ret;
  133. }
  134. if (! CryptGenKey(
  135. pThreadData->hVerifyCtx, AT_SIGNATURE,
  136. pShaSignInfo->dwKeySize << 16, &hKey))
  137. {
  138. TPrint("CryptGenKey ");
  139. goto Ret;
  140. }
  141. if (! CryptSignHash(
  142. hHash, AT_SIGNATURE, NULL,
  143. pShaSignInfo->dwSignFlags, NULL, &cbSign))
  144. {
  145. TPrint("CryptSignHash size ");
  146. goto Ret;
  147. }
  148. if (NULL == (pbSign = (PBYTE) MyAlloc(cbSign)))
  149. {
  150. SetLastError(ERROR_NOT_ENOUGH_MEMORY);
  151. goto Ret;
  152. }
  153. if (! CryptSignHash(
  154. hHash, AT_SIGNATURE, NULL,
  155. pShaSignInfo->dwSignFlags, pbSign, &cbSign))
  156. {
  157. TPrint("CryptSignHash ");
  158. goto Ret;
  159. }
  160. //
  161. // No expected errors should occur from here on
  162. //
  163. pShaSignInfo->fExpectSuccess = TRUE;
  164. if (! CryptDestroyHash(hHash))
  165. {
  166. printf("CryptDestroyHash ");
  167. goto Ret;
  168. }
  169. hHash = 0;
  170. if (! CryptCreateHash(
  171. pThreadData->hVerifyCtx,
  172. pShaSignInfo->aiHash,
  173. 0, 0, &hHash))
  174. {
  175. printf("CryptCreateHash 2 ");
  176. goto Ret;
  177. }
  178. if (! CryptHashData(
  179. hHash, (PBYTE) rgHashData, cbHashData, 0))
  180. {
  181. printf("CryptHashData 2 ");
  182. goto Ret;
  183. }
  184. if (! CryptVerifySignature(
  185. hHash, pbSign, cbSign, hKey,
  186. NULL, pShaSignInfo->dwSignFlags))
  187. {
  188. printf("CryptVerifySignature ");
  189. goto Ret;
  190. }
  191. fSuccess = TRUE;
  192. Ret:
  193. if (! fSuccess)
  194. {
  195. dwError = GetLastError();
  196. if (0 == dwError)
  197. dwError = -1;
  198. }
  199. if (hHash)
  200. CryptDestroyHash(hHash);
  201. if (hKey)
  202. CryptDestroyKey(hKey);
  203. if (pbSign)
  204. MyFree(pbSign);
  205. return dwError;
  206. }
  207. #define cbSHA256_PKCS1_ENCODING 0x13
  208. #define cbSHA384_PKCS1_ENCODING 0x13
  209. #define cbSHA512_PKCS1_ENCODING 0x13
  210. #define cbPKCS1_PADDING 0x3
  211. #define cbSHA256_MIN_SIGNING_KEY_NOOID \
  212. SHA256_DIGEST_LEN + cbPKCS1_PADDING
  213. #define cbSHA256_MIN_SIGNING_KEY \
  214. SHA256_DIGEST_LEN + cbSHA256_PKCS1_ENCODING + cbPKCS1_PADDING
  215. #define cbSHA384_MIN_SIGNING_KEY_NOOID \
  216. SHA384_DIGEST_LEN + cbPKCS1_PADDING
  217. #define cbSHA384_MIN_SIGNING_KEY \
  218. SHA384_DIGEST_LEN + cbSHA384_PKCS1_ENCODING + cbPKCS1_PADDING
  219. #define cbSHA512_MIN_SIGNING_KEY_NOOID \
  220. SHA512_DIGEST_LEN + cbPKCS1_PADDING
  221. #define cbSHA512_MIN_SIGNING_KEY \
  222. SHA512_DIGEST_LEN + cbSHA512_PKCS1_ENCODING + cbPKCS1_PADDING
  223. //
  224. // Function: NewShaModesSigningRegression
  225. //
  226. DWORD NewShaModesSigningRegression(PTHREAD_DATA pThreadData)
  227. {
  228. DWORD dwError = ERROR_SUCCESS;
  229. BOOL fSuccess = FALSE;
  230. SHA_SIGN_INFO ShaSignInfo;
  231. DWORD dwMinSigKeySize = 0;
  232. PALGNODE pAlgNode = NULL;
  233. for ( pAlgNode = pThreadData->pAlgList;
  234. pAlgNode != NULL && CALG_RSA_KEYX != pAlgNode->EnumalgsEx.aiAlgid;
  235. pAlgNode = pAlgNode->pNext);
  236. if (NULL == pAlgNode)
  237. {
  238. printf("ERROR: Could not find CALG_RSA_KEYX alg info ");
  239. goto Ret;
  240. }
  241. dwMinSigKeySize = pAlgNode->EnumalgsEx.dwMinLen / 8;
  242. //
  243. // Test 1A:
  244. // SHA-256
  245. // Positive
  246. //
  247. ShaSignInfo.aiHash = CALG_SHA_256;
  248. ShaSignInfo.dwSignFlags = 0;
  249. ShaSignInfo.fExpectSuccess = TRUE;
  250. ShaSignInfo.dwKeySize =
  251. 8 * (max(dwMinSigKeySize, cbSHA256_MIN_SIGNING_KEY));
  252. dwError = ERROR_SUCCESS;
  253. dwError = DoShaSigning(pThreadData, &ShaSignInfo);
  254. if (ERROR_SUCCESS != dwError)
  255. {
  256. printf("ERROR: Test1A ");
  257. goto Ret;
  258. }
  259. //
  260. // Test 1B:
  261. // SHA-256
  262. // Negative
  263. //
  264. if (cbSHA256_MIN_SIGNING_KEY > dwMinSigKeySize)
  265. {
  266. ShaSignInfo.fExpectSuccess = FALSE;
  267. ShaSignInfo.dwKeySize = 8 * (cbSHA256_MIN_SIGNING_KEY - 1);
  268. dwError = ERROR_SUCCESS;
  269. dwError = DoShaSigning(pThreadData, &ShaSignInfo);
  270. if (NTE_BAD_LEN != dwError)
  271. {
  272. printf("ERROR: Test1B ");
  273. goto Ret;
  274. }
  275. }
  276. //
  277. // Test 1C:
  278. // SHA-256
  279. // NOHASHOID
  280. // Positive
  281. //
  282. ShaSignInfo.dwSignFlags = CRYPT_NOHASHOID;
  283. ShaSignInfo.fExpectSuccess = TRUE;
  284. ShaSignInfo.dwKeySize =
  285. 8 * (max(dwMinSigKeySize, cbSHA256_MIN_SIGNING_KEY_NOOID));
  286. dwError = ERROR_SUCCESS;
  287. dwError = DoShaSigning(pThreadData, &ShaSignInfo);
  288. if (ERROR_SUCCESS != dwError)
  289. {
  290. printf("ERROR: Test1C ");
  291. goto Ret;
  292. }
  293. //
  294. // Test 1D:
  295. // SHA-256
  296. // NOHASHOID
  297. // Negative
  298. //
  299. if (cbSHA256_MIN_SIGNING_KEY_NOOID > dwMinSigKeySize)
  300. {
  301. ShaSignInfo.fExpectSuccess = FALSE;
  302. ShaSignInfo.dwKeySize =
  303. 8 * (cbSHA256_MIN_SIGNING_KEY_NOOID - 1);
  304. dwError = ERROR_SUCCESS;
  305. dwError = DoShaSigning(pThreadData, &ShaSignInfo);
  306. if (NTE_BAD_LEN != dwError)
  307. {
  308. printf("ERROR: Test1D ");
  309. goto Ret;
  310. }
  311. }
  312. //
  313. // Test 2A:
  314. // SHA-384
  315. // Positive
  316. //
  317. ShaSignInfo.aiHash = CALG_SHA_384;
  318. ShaSignInfo.dwSignFlags = 0;
  319. ShaSignInfo.fExpectSuccess = TRUE;
  320. ShaSignInfo.dwKeySize =
  321. 8 * (max(dwMinSigKeySize, cbSHA384_MIN_SIGNING_KEY));
  322. dwError = ERROR_SUCCESS;
  323. dwError = DoShaSigning(pThreadData, &ShaSignInfo);
  324. if (ERROR_SUCCESS != dwError)
  325. {
  326. printf("ERROR: Test2A ");
  327. goto Ret;
  328. }
  329. //
  330. // Test 2B:
  331. // SHA-384
  332. // Negative
  333. //
  334. if (cbSHA384_MIN_SIGNING_KEY > dwMinSigKeySize)
  335. {
  336. ShaSignInfo.fExpectSuccess = FALSE;
  337. ShaSignInfo.dwKeySize = 8 * (cbSHA384_MIN_SIGNING_KEY - 1);
  338. dwError = ERROR_SUCCESS;
  339. dwError = DoShaSigning(pThreadData, &ShaSignInfo);
  340. if (NTE_BAD_LEN != dwError)
  341. {
  342. printf("ERROR: Test2B ");
  343. goto Ret;
  344. }
  345. }
  346. //
  347. // Test 2C:
  348. // SHA-384
  349. // NOHASHOID
  350. // Positive
  351. //
  352. ShaSignInfo.dwSignFlags = CRYPT_NOHASHOID;
  353. ShaSignInfo.fExpectSuccess = TRUE;
  354. ShaSignInfo.dwKeySize =
  355. 8 * (max(dwMinSigKeySize, cbSHA384_MIN_SIGNING_KEY_NOOID));
  356. dwError = ERROR_SUCCESS;
  357. dwError = DoShaSigning(pThreadData, &ShaSignInfo);
  358. if (ERROR_SUCCESS != dwError)
  359. {
  360. printf("ERROR: Test2C ");
  361. goto Ret;
  362. }
  363. //
  364. // Test 2D:
  365. // SHA-384
  366. // NOHASHOID
  367. // Negative
  368. //
  369. if (cbSHA384_MIN_SIGNING_KEY_NOOID > dwMinSigKeySize)
  370. {
  371. ShaSignInfo.fExpectSuccess = FALSE;
  372. ShaSignInfo.dwKeySize =
  373. 8 * (cbSHA384_MIN_SIGNING_KEY_NOOID - 1);
  374. dwError = ERROR_SUCCESS;
  375. dwError = DoShaSigning(pThreadData, &ShaSignInfo);
  376. if (NTE_BAD_LEN != dwError)
  377. {
  378. printf("ERROR: Test2D ");
  379. goto Ret;
  380. }
  381. }
  382. //
  383. // Test 3A:
  384. // SHA-512
  385. // Positive
  386. //
  387. ShaSignInfo.aiHash = CALG_SHA_512;
  388. ShaSignInfo.dwSignFlags = 0;
  389. ShaSignInfo.fExpectSuccess = TRUE;
  390. ShaSignInfo.dwKeySize =
  391. 8 * (max(dwMinSigKeySize, cbSHA512_MIN_SIGNING_KEY));
  392. dwError = ERROR_SUCCESS;
  393. dwError = DoShaSigning(pThreadData, &ShaSignInfo);
  394. if (ERROR_SUCCESS != dwError)
  395. {
  396. printf("ERROR: Test3A ");
  397. goto Ret;
  398. }
  399. //
  400. // Test 3B:
  401. // SHA-512
  402. // Negative
  403. //
  404. if (cbSHA512_MIN_SIGNING_KEY > dwMinSigKeySize)
  405. {
  406. ShaSignInfo.fExpectSuccess = FALSE;
  407. ShaSignInfo.dwKeySize = 8 * (cbSHA512_MIN_SIGNING_KEY - 1);
  408. dwError = ERROR_SUCCESS;
  409. dwError = DoShaSigning(pThreadData, &ShaSignInfo);
  410. if (NTE_BAD_LEN != dwError)
  411. {
  412. printf("ERROR: Test3B ");
  413. goto Ret;
  414. }
  415. }
  416. //
  417. // Test 3C:
  418. // SHA-512
  419. // NOHASHOID
  420. // Positive
  421. //
  422. ShaSignInfo.dwSignFlags = CRYPT_NOHASHOID;
  423. ShaSignInfo.fExpectSuccess = TRUE;
  424. ShaSignInfo.dwKeySize =
  425. 8 * (max(dwMinSigKeySize, cbSHA512_MIN_SIGNING_KEY_NOOID));
  426. dwError = ERROR_SUCCESS;
  427. dwError = DoShaSigning(pThreadData, &ShaSignInfo);
  428. if (ERROR_SUCCESS != dwError)
  429. {
  430. printf("ERROR: Test3C ");
  431. goto Ret;
  432. }
  433. //
  434. // Test 3D:
  435. // SHA-512
  436. // NOHASHOID
  437. // Negative
  438. //
  439. if (cbSHA512_MIN_SIGNING_KEY_NOOID > dwMinSigKeySize)
  440. {
  441. ShaSignInfo.fExpectSuccess = FALSE;
  442. ShaSignInfo.dwKeySize =
  443. 8 * (cbSHA512_MIN_SIGNING_KEY_NOOID - 1);
  444. dwError = ERROR_SUCCESS;
  445. dwError = DoShaSigning(pThreadData, &ShaSignInfo);
  446. if (NTE_BAD_LEN != dwError)
  447. {
  448. printf("ERROR: Test3D ");
  449. goto Ret;
  450. }
  451. }
  452. fSuccess = TRUE;
  453. dwError = ERROR_SUCCESS;
  454. Ret:
  455. if (! fSuccess)
  456. {
  457. if (0 == dwError)
  458. dwError = -1;
  459. }
  460. return dwError;
  461. }
  462. //
  463. // Function: NewShaModesHmacRegression
  464. //
  465. DWORD NewShaModesHmacRegression(PTHREAD_DATA pThreadData)
  466. {
  467. BYTE rgbHmacKeyShaNew [20]; // set bytes to 0x0b
  468. BYTE rgbHmacDataShaNew [] = {
  469. 0x48, 0x69, 0x20, 0x54,
  470. 0x68, 0x65, 0x72, 0x65
  471. };
  472. // Hmac SHA-256 Result
  473. BYTE rgbHmacSha256 [] = {
  474. 0xb0, 0x34, 0x4c, 0x61,
  475. 0xd8, 0xdb, 0x38, 0x53,
  476. 0x5c, 0xa8, 0xaf, 0xce,
  477. 0xaf, 0x0b, 0xf1, 0x2b,
  478. 0x88, 0x1d, 0xc2, 0x00,
  479. 0xc9, 0x83, 0x3d, 0xa7,
  480. 0x26, 0xe9, 0x37, 0x6c,
  481. 0x2e, 0x32, 0xcf, 0xf7
  482. };
  483. // Hmac SHA-384 Result
  484. BYTE rgbHmacSha384 [] = {
  485. 0xaf, 0xd0, 0x39, 0x44,
  486. 0xd8, 0x48, 0x95, 0x62,
  487. 0x6b, 0x08, 0x25, 0xf4,
  488. 0xab, 0x46, 0x90, 0x7f,
  489. 0x15, 0xf9, 0xda, 0xdb,
  490. 0xe4, 0x10, 0x1e, 0xc6,
  491. 0x82, 0xaa, 0x03, 0x4c,
  492. 0x7c, 0xeb, 0xc5, 0x9c,
  493. 0xfa, 0xea, 0x9e, 0xa9,
  494. 0x07, 0x6e, 0xde, 0x7f,
  495. 0x4a, 0xf1, 0x52, 0xe8,
  496. 0xb2, 0xfa, 0x9c, 0xb6
  497. };
  498. // Hmac SHA-512 Result
  499. BYTE rgbHmacSha512 [] = {
  500. 0x87, 0xaa, 0x7c, 0xde,
  501. 0xa5, 0xef, 0x61, 0x9d,
  502. 0x4f, 0xf0, 0xb4, 0x24,
  503. 0x1a, 0x1d, 0x6c, 0xb0,
  504. 0x23, 0x79, 0xf4, 0xe2,
  505. 0xce, 0x4e, 0xc2, 0x78,
  506. 0x7a, 0xd0, 0xb3, 0x05,
  507. 0x45, 0xe1, 0x7c, 0xde,
  508. 0xda, 0xa8, 0x33, 0xb7,
  509. 0xd6, 0xb8, 0xa7, 0x02,
  510. 0x03, 0x8b, 0x27, 0x4e,
  511. 0xae, 0xa3, 0xf4, 0xe4,
  512. 0xbe, 0x9d, 0x91, 0x4e,
  513. 0xeb, 0x61, 0xf1, 0x70,
  514. 0x2e, 0x69, 0x6c, 0x20,
  515. 0x3a, 0x12, 0x68, 0x54
  516. };
  517. DWORD dwError = ERROR_SUCCESS;
  518. BOOL fSuccess = FALSE;
  519. HCRYPTKEY hKey = 0;
  520. HCRYPTHASH hHash = 0;
  521. BYTE rgbKey[1000];
  522. BYTE rgbResult[64];
  523. DWORD cb = 0;
  524. BLOBHEADER *pHeader = NULL;
  525. PDWORD pdw = NULL;
  526. PBYTE pb = NULL;
  527. HMAC_INFO HmacInfo;
  528. memset(&HmacInfo, 0, sizeof(HmacInfo));
  529. pHeader = (BLOBHEADER *) rgbKey;
  530. pdw = (PDWORD)(rgbKey + sizeof(BLOBHEADER));
  531. pb = rgbKey + sizeof(BLOBHEADER) + sizeof(DWORD);
  532. pHeader->aiKeyAlg = CALG_RC2;
  533. pHeader->bType = PLAINTEXTKEYBLOB;
  534. pHeader->bVersion = CUR_BLOB_VERSION;
  535. pHeader->reserved = 0x0000;
  536. *pdw = sizeof(rgbHmacKeyShaNew);
  537. memset(rgbHmacKeyShaNew, 0x0b, sizeof(rgbHmacKeyShaNew));
  538. memcpy(pb, rgbHmacKeyShaNew, sizeof(rgbHmacKeyShaNew));
  539. cb = sizeof(BLOBHEADER) + sizeof(DWORD) + sizeof(rgbHmacKeyShaNew);
  540. if (! CryptImportKey(
  541. pThreadData->hVerifyCtx, rgbKey, cb, 0, CRYPT_IPSEC_HMAC_KEY, &hKey))
  542. {
  543. printf("CryptImportKey ");
  544. goto Ret;
  545. }
  546. //
  547. // Hmac SHA-256 Tests
  548. //
  549. HmacInfo.HashAlgid = CALG_SHA_256;
  550. if (! CryptCreateHash(
  551. pThreadData->hVerifyCtx, CALG_HMAC, hKey, 0, &hHash))
  552. {
  553. printf("CryptCreateHash ");
  554. goto Ret;
  555. }
  556. if (! CryptSetHashParam(
  557. hHash, HP_HMAC_INFO, (PBYTE) &HmacInfo, 0))
  558. {
  559. printf("CryptSetHashParam ");
  560. goto Ret;
  561. }
  562. if (! CryptHashData(
  563. hHash, rgbHmacDataShaNew, sizeof(rgbHmacDataShaNew), 0))
  564. {
  565. printf("CryptHashData ");
  566. goto Ret;
  567. }
  568. cb = sizeof(rgbResult);
  569. if (! CryptGetHashParam(
  570. hHash, HP_HASHVAL, rgbResult, &cb, 0))
  571. {
  572. printf("CryptGetHashParam ");
  573. goto Ret;
  574. }
  575. if (memcmp(rgbResult, rgbHmacSha256, sizeof(rgbHmacSha256)))
  576. {
  577. printf("ERROR: Incorrect Hmac result for SHA 256 ");
  578. goto Ret;
  579. }
  580. if (! CryptDestroyHash(hHash))
  581. {
  582. printf("CryptDestroyHash ");
  583. goto Ret;
  584. }
  585. hHash = 0;
  586. //
  587. // Hmac SHA-384 Tests
  588. //
  589. HmacInfo.HashAlgid = CALG_SHA_384;
  590. if (! CryptCreateHash(
  591. pThreadData->hVerifyCtx, CALG_HMAC, hKey, 0, &hHash))
  592. {
  593. printf("CryptCreateHash ");
  594. goto Ret;
  595. }
  596. if (! CryptSetHashParam(
  597. hHash, HP_HMAC_INFO, (PBYTE) &HmacInfo, 0))
  598. {
  599. printf("CryptSetHashParam ");
  600. goto Ret;
  601. }
  602. if (! CryptHashData(
  603. hHash, rgbHmacDataShaNew, sizeof(rgbHmacDataShaNew), 0))
  604. {
  605. printf("CryptHashData ");
  606. goto Ret;
  607. }
  608. cb = sizeof(rgbResult);
  609. if (! CryptGetHashParam(
  610. hHash, HP_HASHVAL, rgbResult, &cb, 0))
  611. {
  612. printf("CryptGetHashParam ");
  613. goto Ret;
  614. }
  615. if (memcmp(rgbResult, rgbHmacSha384, sizeof(rgbHmacSha384)))
  616. {
  617. printf("ERROR: Incorrect Hmac result for SHA 384 ");
  618. goto Ret;
  619. }
  620. if (! CryptDestroyHash(hHash))
  621. {
  622. printf("CryptDestroyHash ");
  623. goto Ret;
  624. }
  625. hHash = 0;
  626. //
  627. // Hmac SHA-512 Tests
  628. //
  629. HmacInfo.HashAlgid = CALG_SHA_512;
  630. if (! CryptCreateHash(
  631. pThreadData->hVerifyCtx, CALG_HMAC, hKey, 0, &hHash))
  632. {
  633. printf("CryptCreateHash ");
  634. goto Ret;
  635. }
  636. if (! CryptSetHashParam(
  637. hHash, HP_HMAC_INFO, (PBYTE) &HmacInfo, 0))
  638. {
  639. printf("CryptSetHashParam ");
  640. goto Ret;
  641. }
  642. if (! CryptHashData(
  643. hHash, rgbHmacDataShaNew, sizeof(rgbHmacDataShaNew), 0))
  644. {
  645. printf("CryptHashData ");
  646. goto Ret;
  647. }
  648. cb = sizeof(rgbResult);
  649. if (! CryptGetHashParam(
  650. hHash, HP_HASHVAL, rgbResult, &cb, 0))
  651. {
  652. printf("CryptGetHashParam ");
  653. goto Ret;
  654. }
  655. if (memcmp(rgbResult, rgbHmacSha512, sizeof(rgbHmacSha512)))
  656. {
  657. printf("ERROR: Incorrect Hmac result for SHA 512 ");
  658. goto Ret;
  659. }
  660. fSuccess = TRUE;
  661. Ret:
  662. if (! fSuccess)
  663. {
  664. dwError = GetLastError();
  665. if (0 == dwError)
  666. dwError = -1;
  667. }
  668. if (hHash)
  669. CryptDestroyHash(hHash);
  670. if (hKey)
  671. CryptDestroyKey(hKey);
  672. return dwError;
  673. }
  674. // SHA 256 Vectors
  675. CHAR l_rgSha256Test1[] = "abc";
  676. DWORD l_cbSha256Test1 = sizeof(l_rgSha256Test1) - 1;
  677. BYTE l_rgbSha256Result1[] = {
  678. 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea,
  679. 0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23,
  680. 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c,
  681. 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad
  682. };
  683. CHAR l_rgSha256Test2[] = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  684. DWORD l_cbSha256Test2 = sizeof(l_rgSha256Test2) - 1;
  685. BYTE l_rgbSha256Result2[] = {
  686. 0x24, 0x8d, 0x6a, 0x61, 0xd2, 0x06, 0x38, 0xb8,
  687. 0xe5, 0xc0, 0x26, 0x93, 0x0c, 0x3e, 0x60, 0x39,
  688. 0xa3, 0x3c, 0xe4, 0x59, 0x64, 0xff, 0x21, 0x67,
  689. 0xf6, 0xec, 0xed, 0xd4, 0x19, 0xdb, 0x06, 0xc1
  690. };
  691. // SHA 384 Vectors
  692. BYTE l_rgSha384Test1[] = "abc";
  693. DWORD l_cbSha384Test1 = sizeof(l_rgSha384Test1) - 1;
  694. BYTE l_rgbSha384Result1[] = {
  695. 0xcb, 0x00, 0x75, 0x3f, 0x45, 0xa3, 0x5e, 0x8b,
  696. 0xb5, 0xa0, 0x3d, 0x69, 0x9a, 0xc6, 0x50, 0x07,
  697. 0x27, 0x2c, 0x32, 0xab, 0x0e, 0xde, 0xd1, 0x63,
  698. 0x1a, 0x8b, 0x60, 0x5a, 0x43, 0xff, 0x5b, 0xed,
  699. 0x80, 0x86, 0x07, 0x2b, 0xa1, 0xe7, 0xcc, 0x23,
  700. 0x58, 0xba, 0xec, 0xa1, 0x34, 0xc8, 0x25, 0xa7
  701. };
  702. BYTE l_rgSha384Test2[] = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  703. DWORD l_cbSha384Test2 = sizeof(l_rgSha384Test2) - 1;
  704. BYTE l_rgbSha384Result2[] = {
  705. 0x09, 0x33, 0x0c, 0x33, 0xf7, 0x11, 0x47, 0xe8,
  706. 0x3d, 0x19, 0x2f, 0xc7, 0x82, 0xcd, 0x1b, 0x47,
  707. 0x53, 0x11, 0x1b, 0x17, 0x3b, 0x3b, 0x05, 0xd2,
  708. 0x2f, 0xa0, 0x80, 0x86, 0xe3, 0xb0, 0xf7, 0x12,
  709. 0xfc, 0xc7, 0xc7, 0x1a, 0x55, 0x7e, 0x2d, 0xb9,
  710. 0x66, 0xc3, 0xe9, 0xfa, 0x91, 0x74, 0x60, 0x39
  711. };
  712. // SHA 512 Vectors
  713. BYTE l_rgSha512Test1[] = "abc";
  714. DWORD l_cbSha512Test1 = sizeof(l_rgSha512Test1) - 1;
  715. BYTE l_rgbSha512Result1[] = {
  716. 0xdd, 0xaf, 0x35, 0xa1, 0x93, 0x61, 0x7a, 0xba,
  717. 0xcc, 0x41, 0x73, 0x49, 0xae, 0x20, 0x41, 0x31,
  718. 0x12, 0xe6, 0xfa, 0x4e, 0x89, 0xa9, 0x7e, 0xa2,
  719. 0x0a, 0x9e, 0xee, 0xe6, 0x4b, 0x55, 0xd3, 0x9a,
  720. 0x21, 0x92, 0x99, 0x2a, 0x27, 0x4f, 0xc1, 0xa8,
  721. 0x36, 0xba, 0x3c, 0x23, 0xa3, 0xfe, 0xeb, 0xbd,
  722. 0x45, 0x4d, 0x44, 0x23, 0x64, 0x3c, 0xe8, 0x0e,
  723. 0x2a, 0x9a, 0xc9, 0x4f, 0xa5, 0x4c, 0xa4, 0x9f
  724. };
  725. BYTE l_rgSha512Test2[] = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  726. DWORD l_cbSha512Test2 = sizeof(l_rgSha512Test2) - 1;
  727. BYTE l_rgbSha512Result2[] = {
  728. 0x8e, 0x95, 0x9b, 0x75, 0xda, 0xe3, 0x13, 0xda,
  729. 0x8c, 0xf4, 0xf7, 0x28, 0x14, 0xfc, 0x14, 0x3f,
  730. 0x8f, 0x77, 0x79, 0xc6, 0xeb, 0x9f, 0x7f, 0xa1,
  731. 0x72, 0x99, 0xae, 0xad, 0xb6, 0x88, 0x90, 0x18,
  732. 0x50, 0x1d, 0x28, 0x9e, 0x49, 0x00, 0xf7, 0xe4,
  733. 0x33, 0x1b, 0x99, 0xde, 0xc4, 0xb5, 0x43, 0x3a,
  734. 0xc7, 0xd3, 0x29, 0xee, 0xb6, 0xdd, 0x26, 0x54,
  735. 0x5e, 0x96, 0xe5, 0x5b, 0x87, 0x4b, 0xe9, 0x09
  736. };
  737. typedef struct {
  738. ALG_ID aiKey;
  739. ALG_ID aiHash;
  740. PBYTE pbBaseData;
  741. DWORD cbBaseData;
  742. PBYTE pbExpectedKeyData;
  743. } DERIVE_KEY_INFO, *PDERIVE_KEY_INFO;
  744. DWORD DoDeriveKey(
  745. PTHREAD_DATA pThreadData,
  746. PDERIVE_KEY_INFO pDeriveKeyInfo)
  747. {
  748. BOOL fSuccess = FALSE;
  749. DWORD dwError = ERROR_SUCCESS;
  750. HCRYPTKEY hKey = 0;
  751. HCRYPTHASH hHash = 0;
  752. BYTE rgbKey [1000];
  753. DWORD cbKey = 0;
  754. DWORD cb = 0;
  755. if (! CryptCreateHash(
  756. pThreadData->hVerifyCtx,
  757. pDeriveKeyInfo->aiHash,
  758. 0, 0, &hHash))
  759. {
  760. printf("CryptCreateHash ");
  761. goto Ret;
  762. }
  763. if (! CryptHashData(
  764. hHash,
  765. pDeriveKeyInfo->pbBaseData,
  766. pDeriveKeyInfo->cbBaseData,
  767. 0))
  768. {
  769. printf("CryptHashData ");
  770. goto Ret;
  771. }
  772. if (! CryptDeriveKey(
  773. pThreadData->hVerifyCtx,
  774. pDeriveKeyInfo->aiKey,
  775. hHash,
  776. CRYPT_EXPORTABLE,
  777. &hKey))
  778. {
  779. printf("CryptDeriveKey ");
  780. goto Ret;
  781. }
  782. cbKey = sizeof(rgbKey);
  783. if (! CryptExportKey(
  784. hKey, 0, PLAINTEXTKEYBLOB,
  785. 0, rgbKey, &cbKey))
  786. {
  787. printf("CryptExportKey ");
  788. goto Ret;
  789. }
  790. cb = sizeof(DWORD);
  791. if (! CryptGetKeyParam(
  792. hKey, KP_KEYLEN, (PBYTE) &cbKey,
  793. &cb, 0))
  794. {
  795. printf("CryptGetKeyParam ");
  796. goto Ret;
  797. }
  798. if (memcmp(
  799. pDeriveKeyInfo->pbExpectedKeyData,
  800. rgbKey + sizeof(BLOBHEADER) + sizeof(DWORD),
  801. cbKey / 8))
  802. {
  803. printf(
  804. "ERROR: Incorrect key data for key alg %x; hash alg %x ",
  805. pDeriveKeyInfo->aiKey,
  806. pDeriveKeyInfo->aiHash);
  807. goto Ret;
  808. }
  809. fSuccess = TRUE;
  810. Ret:
  811. if (! fSuccess)
  812. {
  813. dwError = GetLastError();
  814. if (0 == dwError)
  815. dwError = -1;
  816. }
  817. if (hKey)
  818. CryptDestroyKey(hKey);
  819. if (hHash)
  820. CryptDestroyHash(hHash);
  821. return dwError;
  822. }
  823. //
  824. // Function: NewShaModesDeriveKeyRegression
  825. //
  826. DWORD NewShaModesDeriveKeyRegression(PTHREAD_DATA pThreadData)
  827. {
  828. DERIVE_KEY_INFO DeriveKeyInfo;
  829. BOOL fSuccess = FALSE;
  830. DWORD dwError = ERROR_SUCCESS;
  831. ALG_ID rgKeyAlgs [] = {
  832. CALG_AES_128, CALG_AES_192, CALG_AES_256
  833. };
  834. DWORD dw;
  835. for (dw = 0; dw < (sizeof(rgKeyAlgs) / sizeof(ALG_ID)); dw++)
  836. {
  837. DeriveKeyInfo.aiKey = rgKeyAlgs[dw];
  838. //
  839. // SHA-256 Derive Key Test
  840. //
  841. DeriveKeyInfo.aiHash = CALG_SHA_256;
  842. DeriveKeyInfo.cbBaseData = l_cbSha256Test1;
  843. DeriveKeyInfo.pbBaseData = l_rgSha256Test1;
  844. DeriveKeyInfo.pbExpectedKeyData = l_rgbSha256Result1;
  845. dwError = DoDeriveKey(pThreadData, &DeriveKeyInfo);
  846. if (ERROR_SUCCESS != dwError)
  847. goto Ret;
  848. //
  849. // SHA-384 Derive Key Test
  850. //
  851. DeriveKeyInfo.aiHash = CALG_SHA_384;
  852. DeriveKeyInfo.cbBaseData = l_cbSha384Test1;
  853. DeriveKeyInfo.pbBaseData = l_rgSha384Test1;
  854. DeriveKeyInfo.pbExpectedKeyData = l_rgbSha384Result1;
  855. dwError = DoDeriveKey(pThreadData, &DeriveKeyInfo);
  856. if (ERROR_SUCCESS != dwError)
  857. goto Ret;
  858. //
  859. // SHA-512 Derive Key Test
  860. //
  861. DeriveKeyInfo.aiHash = CALG_SHA_512;
  862. DeriveKeyInfo.cbBaseData = l_cbSha512Test1;
  863. DeriveKeyInfo.pbBaseData = l_rgSha512Test1;
  864. DeriveKeyInfo.pbExpectedKeyData = l_rgbSha512Result1;
  865. dwError = DoDeriveKey(pThreadData, &DeriveKeyInfo);
  866. if (ERROR_SUCCESS != dwError)
  867. goto Ret;
  868. }
  869. fSuccess = TRUE;
  870. Ret:
  871. if (! fSuccess)
  872. {
  873. if (0 == dwError)
  874. dwError = -1;
  875. }
  876. return dwError;
  877. }
  878. typedef struct {
  879. ALG_ID aiHash;
  880. PBYTE pbKeyData;
  881. DWORD cbKeyData;
  882. PBYTE pbExpectedHash;
  883. } HASH_SESSION_KEY_INFO, *PHASH_SESSION_KEY_INFO;
  884. DWORD DoHashSessionKey(
  885. PTHREAD_DATA pThreadData,
  886. PHASH_SESSION_KEY_INFO pHashSessionKeyInfo)
  887. {
  888. DWORD dwError = ERROR_SUCCESS;
  889. BOOL fSuccess = FALSE;
  890. HCRYPTHASH hHash = 0;
  891. HCRYPTKEY hKey = 0;
  892. BYTE rgbHash [1000];
  893. DWORD cbHash = 0;
  894. BYTE rgbKey [1000];
  895. DWORD cbKey = 0;
  896. BLOBHEADER *pBlobHeader = (BLOBHEADER *) rgbKey;
  897. DWORD *pdw = (DWORD *)(rgbKey + sizeof(BLOBHEADER));
  898. pBlobHeader->aiKeyAlg = CALG_RC2;
  899. pBlobHeader->bType = PLAINTEXTKEYBLOB;
  900. pBlobHeader->bVersion = CUR_BLOB_VERSION;
  901. pBlobHeader->reserved = 0x0000;
  902. *pdw = pHashSessionKeyInfo->cbKeyData;
  903. memcpy(
  904. rgbKey + sizeof(BLOBHEADER) + sizeof(DWORD),
  905. pHashSessionKeyInfo->pbKeyData,
  906. pHashSessionKeyInfo->cbKeyData);
  907. if (! CryptImportKey(
  908. pThreadData->hVerifyCtx,
  909. rgbKey,
  910. sizeof(BLOBHEADER) + sizeof(DWORD) + pHashSessionKeyInfo->cbKeyData,
  911. 0, CRYPT_IPSEC_HMAC_KEY, &hKey))
  912. {
  913. printf("CryptImportKey ");
  914. goto Ret;
  915. }
  916. if (! CryptCreateHash(
  917. pThreadData->hVerifyCtx,
  918. pHashSessionKeyInfo->aiHash,
  919. 0, 0, &hHash))
  920. {
  921. printf("CryptCreateHash ");
  922. goto Ret;
  923. }
  924. if (! CryptHashSessionKey(
  925. hHash, hKey, CRYPT_LITTLE_ENDIAN))
  926. {
  927. printf("CryptHashSessionKey ");
  928. goto Ret;
  929. }
  930. cbHash = sizeof(rgbHash);
  931. if (! CryptGetHashParam(
  932. hHash, HP_HASHVAL,
  933. rgbHash, &cbHash, 0))
  934. {
  935. printf("CryptGetHashParam ");
  936. goto Ret;
  937. }
  938. if (0 == cbHash || memcmp(
  939. rgbHash,
  940. pHashSessionKeyInfo->pbExpectedHash,
  941. cbHash))
  942. {
  943. printf(
  944. "ERROR: Incorrect hash result for hash alg 0x%x ",
  945. pHashSessionKeyInfo->aiHash);
  946. goto Ret;
  947. }
  948. fSuccess = TRUE;
  949. Ret:
  950. if (! fSuccess)
  951. {
  952. dwError = GetLastError();
  953. if (0 == dwError)
  954. dwError = -1;
  955. }
  956. if (hHash)
  957. CryptDestroyHash(hHash);
  958. if (hKey)
  959. CryptDestroyKey(hKey);
  960. return dwError;
  961. }
  962. //
  963. // Function: NewShaModesHashSessionKeyRegression
  964. //
  965. DWORD NewShaModesHashSessionKeyRegression(PTHREAD_DATA pThreadData)
  966. {
  967. DWORD dwError = ERROR_SUCCESS;
  968. BOOL fSuccess = FALSE;
  969. HASH_SESSION_KEY_INFO HashSessionKeyInfo;
  970. //
  971. // SHA-256 CryptHashSessionKey Test
  972. //
  973. HashSessionKeyInfo.aiHash = CALG_SHA_256;
  974. HashSessionKeyInfo.pbKeyData = l_rgSha256Test2;
  975. HashSessionKeyInfo.cbKeyData = l_cbSha256Test2;
  976. HashSessionKeyInfo.pbExpectedHash = l_rgbSha256Result2;
  977. dwError = DoHashSessionKey(pThreadData, &HashSessionKeyInfo);
  978. if (ERROR_SUCCESS != dwError)
  979. goto Ret;
  980. //
  981. // SHA-384 CryptHashSessionKey Test
  982. //
  983. HashSessionKeyInfo.aiHash = CALG_SHA_384;
  984. HashSessionKeyInfo.pbKeyData = l_rgSha384Test2;
  985. HashSessionKeyInfo.cbKeyData = l_cbSha384Test2;
  986. HashSessionKeyInfo.pbExpectedHash = l_rgbSha384Result2;
  987. dwError = DoHashSessionKey(pThreadData, &HashSessionKeyInfo);
  988. if (ERROR_SUCCESS != dwError)
  989. goto Ret;
  990. //
  991. // SHA-512 CryptHashSessionKey Test
  992. //
  993. HashSessionKeyInfo.aiHash = CALG_SHA_512;
  994. HashSessionKeyInfo.pbKeyData = l_rgSha512Test2;
  995. HashSessionKeyInfo.cbKeyData = l_cbSha512Test2;
  996. HashSessionKeyInfo.pbExpectedHash = l_rgbSha512Result2;
  997. dwError = DoHashSessionKey(pThreadData, &HashSessionKeyInfo);
  998. if (ERROR_SUCCESS != dwError)
  999. goto Ret;
  1000. fSuccess = TRUE;
  1001. Ret:
  1002. if (! fSuccess)
  1003. {
  1004. if (0 == dwError)
  1005. dwError = -1;
  1006. }
  1007. return dwError;
  1008. }
  1009. //
  1010. // Function: NewShaModesBasicRegression
  1011. //
  1012. DWORD NewShaModesBasicRegression(PTHREAD_DATA pThreadData)
  1013. {
  1014. HCRYPTHASH hHash = 0;
  1015. BYTE rgbResult[64];
  1016. DWORD cbResult = 0;
  1017. BOOL fSuccess = FALSE;
  1018. DWORD dwError = ERROR_SUCCESS;
  1019. //
  1020. // SHA-256 Tests
  1021. //
  1022. // Test 1
  1023. if (! CryptCreateHash(
  1024. pThreadData->hVerifyCtx, CALG_SHA_256, 0, 0, &hHash))
  1025. {
  1026. printf("CryptCreateHash ");
  1027. goto Ret;
  1028. }
  1029. if (! CryptHashData(
  1030. hHash, l_rgSha256Test1, l_cbSha256Test1, 0))
  1031. {
  1032. printf("CryptHashData ");
  1033. goto Ret;
  1034. }
  1035. cbResult = sizeof(rgbResult);
  1036. if (! CryptGetHashParam(
  1037. hHash, HP_HASHVAL, rgbResult, &cbResult, 0))
  1038. {
  1039. printf("CryptGetHashParam ");
  1040. goto Ret;
  1041. }
  1042. if (memcmp(rgbResult, l_rgbSha256Result1, sizeof(l_rgbSha256Result1)))
  1043. {
  1044. printf("ERROR: Incorrect hash result for SHA-256 1 ");
  1045. goto Ret;
  1046. }
  1047. if (! CryptDestroyHash(hHash))
  1048. {
  1049. printf("CryptDestroyHash ");
  1050. goto Ret;
  1051. }
  1052. hHash = 0;
  1053. // Test 2
  1054. if (! CryptCreateHash(
  1055. pThreadData->hVerifyCtx, CALG_SHA_256, 0, 0, &hHash))
  1056. {
  1057. printf("CryptCreateHash ");
  1058. goto Ret;
  1059. }
  1060. if (! CryptHashData(
  1061. hHash, l_rgSha256Test2, l_cbSha256Test2, 0))
  1062. {
  1063. printf("CryptHashData ");
  1064. goto Ret;
  1065. }
  1066. cbResult = sizeof(rgbResult);
  1067. if (! CryptGetHashParam(
  1068. hHash, HP_HASHVAL, rgbResult, &cbResult, 0))
  1069. {
  1070. printf("CryptGetHashParam ");
  1071. goto Ret;
  1072. }
  1073. if (memcmp(rgbResult, l_rgbSha256Result2, sizeof(l_rgbSha256Result2)))
  1074. {
  1075. printf("ERROR: Incorrect hash result for SHA-256 2 ");
  1076. goto Ret;
  1077. }
  1078. if (! CryptDestroyHash(hHash))
  1079. {
  1080. printf("CryptDestroyHash ");
  1081. goto Ret;
  1082. }
  1083. hHash = 0;
  1084. //
  1085. // SHA-384 Tests
  1086. //
  1087. // Test 1
  1088. if (! CryptCreateHash(
  1089. pThreadData->hVerifyCtx, CALG_SHA_384, 0, 0, &hHash))
  1090. {
  1091. printf("CryptCreateHash ");
  1092. goto Ret;
  1093. }
  1094. if (! CryptHashData(
  1095. hHash, l_rgSha384Test1, l_cbSha384Test1, 0))
  1096. {
  1097. printf("CryptHashData ");
  1098. goto Ret;
  1099. }
  1100. cbResult = sizeof(rgbResult);
  1101. if (! CryptGetHashParam(
  1102. hHash, HP_HASHVAL, rgbResult, &cbResult, 0))
  1103. {
  1104. printf("CryptGetHashParam ");
  1105. goto Ret;
  1106. }
  1107. if (memcmp(rgbResult, l_rgbSha384Result1, sizeof(l_rgbSha384Result1)))
  1108. {
  1109. printf("ERROR: Incorrect hash result for SHA-384 1 ");
  1110. goto Ret;
  1111. }
  1112. if (! CryptDestroyHash(hHash))
  1113. {
  1114. printf("CryptDestroyHash ");
  1115. goto Ret;
  1116. }
  1117. hHash = 0;
  1118. // Test 2
  1119. if (! CryptCreateHash(
  1120. pThreadData->hVerifyCtx, CALG_SHA_384, 0, 0, &hHash))
  1121. {
  1122. printf("CryptCreateHash ");
  1123. goto Ret;
  1124. }
  1125. if (! CryptHashData(
  1126. hHash, l_rgSha384Test2, l_cbSha384Test2, 0))
  1127. {
  1128. printf("CryptHashData ");
  1129. goto Ret;
  1130. }
  1131. cbResult = sizeof(rgbResult);
  1132. if (! CryptGetHashParam(
  1133. hHash, HP_HASHVAL, rgbResult, &cbResult, 0))
  1134. {
  1135. printf("CryptGetHashParam ");
  1136. goto Ret;
  1137. }
  1138. if (memcmp(rgbResult, l_rgbSha384Result2, sizeof(l_rgbSha384Result2)))
  1139. {
  1140. printf("ERROR: Incorrect hash result for SHA-384 2 ");
  1141. goto Ret;
  1142. }
  1143. if (! CryptDestroyHash(hHash))
  1144. {
  1145. printf("CryptDestroyHash ");
  1146. goto Ret;
  1147. }
  1148. hHash = 0;
  1149. //
  1150. // SHA-512 Tests
  1151. //
  1152. // Test 1
  1153. if (! CryptCreateHash(
  1154. pThreadData->hVerifyCtx, CALG_SHA_512, 0, 0, &hHash))
  1155. {
  1156. printf("CryptCreateHash ");
  1157. goto Ret;
  1158. }
  1159. if (! CryptHashData(
  1160. hHash, l_rgSha512Test1, l_cbSha512Test1, 0))
  1161. {
  1162. printf("CryptHashData ");
  1163. goto Ret;
  1164. }
  1165. cbResult = sizeof(rgbResult);
  1166. if (! CryptGetHashParam(
  1167. hHash, HP_HASHVAL, rgbResult, &cbResult, 0))
  1168. {
  1169. printf("CryptGetHashParam ");
  1170. goto Ret;
  1171. }
  1172. if (memcmp(rgbResult, l_rgbSha512Result1, sizeof(l_rgbSha512Result1)))
  1173. {
  1174. printf("ERROR: Incorrect hash result for SHA-512 1 ");
  1175. goto Ret;
  1176. }
  1177. if (! CryptDestroyHash(hHash))
  1178. {
  1179. printf("CryptDestroyHash ");
  1180. goto Ret;
  1181. }
  1182. hHash = 0;
  1183. // Test 2
  1184. if (! CryptCreateHash(
  1185. pThreadData->hVerifyCtx, CALG_SHA_512, 0, 0, &hHash))
  1186. {
  1187. printf("CryptCreateHash ");
  1188. goto Ret;
  1189. }
  1190. if (! CryptHashData(
  1191. hHash, l_rgSha512Test2, l_cbSha512Test2, 0))
  1192. {
  1193. printf("CryptHashData ");
  1194. goto Ret;
  1195. }
  1196. cbResult = sizeof(rgbResult);
  1197. if (! CryptGetHashParam(
  1198. hHash, HP_HASHVAL, rgbResult, &cbResult, 0))
  1199. {
  1200. printf("CryptGetHashParam ");
  1201. goto Ret;
  1202. }
  1203. if (memcmp(rgbResult, l_rgbSha512Result2, sizeof(l_rgbSha512Result2)))
  1204. {
  1205. printf("ERROR: Incorrect hash result for SHA-512 2 ");
  1206. goto Ret;
  1207. }
  1208. fSuccess = TRUE;
  1209. Ret:
  1210. if (! fSuccess)
  1211. {
  1212. dwError = GetLastError();
  1213. if (0 == dwError)
  1214. dwError = -1;
  1215. }
  1216. if (hHash)
  1217. CryptDestroyHash(hHash);
  1218. return dwError;
  1219. }
  1220. typedef struct _DERIVE_KEY_DATA
  1221. {
  1222. ALG_ID aiKey;
  1223. ALG_ID aiHash;
  1224. PBYTE pbPassword;
  1225. DWORD cbPassword;
  1226. PBYTE pbPlaintext;
  1227. DWORD cbPlaintext;
  1228. } DERIVE_KEY_DATA, *PDERIVE_KEY_DATA;
  1229. DWORD DeriveAndCompareAesKey(
  1230. PTHREAD_DATA pThreadData,
  1231. PDERIVE_KEY_DATA pDeriveKeyData)
  1232. {
  1233. HCRYPTKEY hDeriveKey = 0;
  1234. HCRYPTKEY hCompareKey = 0;
  1235. HCRYPTHASH hDeriveHash = 0;
  1236. HCRYPTHASH hCompareHash = 0;
  1237. BYTE rgbStringA[64];
  1238. BYTE rgbStringB[64];
  1239. BYTE rgbKey[64];
  1240. BYTE rgbHashA[20];
  1241. BYTE rgbHashB[20];
  1242. PBYTE pbDeriveCiphertext = NULL;
  1243. DWORD cbDeriveCiphertext = 0;
  1244. PBYTE pbCompareCiphertext = NULL;
  1245. DWORD cbCompareCiphertext = 0;
  1246. DWORD cbHash = 0;
  1247. DWORD cb = 0;
  1248. DWORD cbKey = 0;
  1249. BOOL fSuccess = FALSE;
  1250. DWORD dwError = ERROR_SUCCESS;
  1251. unsigned int i;
  1252. memset(rgbStringA, 0, sizeof(rgbStringA));
  1253. memset(rgbStringB, 0, sizeof(rgbStringB));
  1254. memset(rgbKey, 0, sizeof(rgbKey));
  1255. memset(rgbHashA, 0, sizeof(rgbHashA));
  1256. memset(rgbHashB, 0, sizeof(rgbHashB));
  1257. //
  1258. // Derive a key from hashed password and encrypt.
  1259. //
  1260. if (! CryptCreateHash(
  1261. pThreadData->hVerifyCtx,
  1262. pDeriveKeyData->aiHash,
  1263. 0, 0, &hDeriveHash))
  1264. {
  1265. printf("CryptCreateHash ");
  1266. goto Ret;
  1267. }
  1268. if (! CryptHashData(
  1269. hDeriveHash,
  1270. pDeriveKeyData->pbPassword,
  1271. pDeriveKeyData->cbPassword,
  1272. 0))
  1273. {
  1274. printf("CryptHashData ");
  1275. goto Ret;
  1276. }
  1277. if (! CryptDeriveKey(
  1278. pThreadData->hVerifyCtx,
  1279. pDeriveKeyData->aiKey,
  1280. hDeriveHash,
  1281. 0, &hDeriveKey))
  1282. {
  1283. printf("CryptDeriveKey ");
  1284. goto Ret;
  1285. }
  1286. cbDeriveCiphertext = pDeriveKeyData->cbPlaintext;
  1287. if (! CryptEncrypt(
  1288. hDeriveKey,
  1289. 0, TRUE, 0, NULL,
  1290. &cbDeriveCiphertext, 0))
  1291. {
  1292. printf("CryptEncrypt size ");
  1293. goto Ret;
  1294. }
  1295. if (NULL == (pbDeriveCiphertext =
  1296. (PBYTE) MyAlloc(cbDeriveCiphertext)))
  1297. return ERROR_NOT_ENOUGH_MEMORY;
  1298. memcpy(
  1299. pbDeriveCiphertext,
  1300. pDeriveKeyData->pbPlaintext,
  1301. pDeriveKeyData->cbPlaintext);
  1302. cb = pDeriveKeyData->cbPlaintext;
  1303. if (! CryptEncrypt(
  1304. hDeriveKey,
  1305. 0, TRUE, 0,
  1306. pbDeriveCiphertext,
  1307. &cb,
  1308. cbDeriveCiphertext))
  1309. {
  1310. printf("CryptEncrypt ");
  1311. goto Ret;
  1312. }
  1313. //
  1314. // Now do the derive "manually" and compare
  1315. // the result.
  1316. //
  1317. if (! CryptCreateHash(
  1318. pThreadData->hVerifyCtx,
  1319. pDeriveKeyData->aiHash,
  1320. 0, 0, &hCompareHash))
  1321. {
  1322. printf("CryptCreateHash ");
  1323. goto Ret;
  1324. }
  1325. if (! CryptHashData(
  1326. hCompareHash,
  1327. pDeriveKeyData->pbPassword,
  1328. pDeriveKeyData->cbPassword,
  1329. 0))
  1330. {
  1331. printf("CryptHashData ");
  1332. goto Ret;
  1333. }
  1334. cbHash = sizeof(rgbHashA);
  1335. if (! CryptGetHashParam(
  1336. hCompareHash,
  1337. HP_HASHVAL,
  1338. rgbHashA,
  1339. &cbHash, 0))
  1340. {
  1341. printf("CryptGetHashParam ");
  1342. goto Ret;
  1343. }
  1344. if (! CryptDestroyHash(hCompareHash))
  1345. {
  1346. printf("CryptDestroyHash ");
  1347. goto Ret;
  1348. }
  1349. hCompareHash = 0;
  1350. memset(rgbStringA, 0x36, sizeof(rgbStringA));
  1351. memset(rgbStringB, 0x5c, sizeof(rgbStringB));
  1352. for (i = 0; i < cbHash; i++)
  1353. {
  1354. rgbStringA[i] ^= rgbHashA[i];
  1355. rgbStringB[i] ^= rgbHashA[i];
  1356. }
  1357. ((BLOBHEADER *) rgbKey)->aiKeyAlg = pDeriveKeyData->aiKey;
  1358. ((BLOBHEADER *) rgbKey)->bType = PLAINTEXTKEYBLOB;
  1359. ((BLOBHEADER *) rgbKey)->bVersion = CUR_BLOB_VERSION;
  1360. switch (pDeriveKeyData->aiKey)
  1361. {
  1362. case CALG_AES_128:
  1363. cbKey = 16;
  1364. break;
  1365. case CALG_AES_192:
  1366. cbKey = 24;
  1367. break;
  1368. case CALG_AES_256:
  1369. cbKey = 32;
  1370. break;
  1371. default:
  1372. printf("Bad Key Alg ");
  1373. goto Ret;
  1374. }
  1375. *(DWORD *)(rgbKey + sizeof(BLOBHEADER)) = cbKey;
  1376. if (! CryptCreateHash(
  1377. pThreadData->hVerifyCtx,
  1378. pDeriveKeyData->aiHash,
  1379. 0, 0, &hCompareHash))
  1380. {
  1381. printf("CryptCreateHash compare ");
  1382. goto Ret;
  1383. }
  1384. if (! CryptHashData(
  1385. hCompareHash, rgbStringA, sizeof(rgbStringA), 0))
  1386. {
  1387. printf("CryptHashData compare ");
  1388. goto Ret;
  1389. }
  1390. memset(rgbHashA, 0, sizeof(rgbHashA));
  1391. cbHash = sizeof(rgbHashA);
  1392. if (! CryptGetHashParam(
  1393. hCompareHash, HP_HASHVAL,
  1394. rgbHashA,
  1395. &cbHash, 0))
  1396. {
  1397. printf("CryptGetHashParam ");
  1398. goto Ret;
  1399. }
  1400. if (! CryptDestroyHash(hCompareHash))
  1401. {
  1402. printf("CryptDestroyHash ");
  1403. goto Ret;
  1404. }
  1405. hCompareHash = 0;
  1406. if (! CryptCreateHash(
  1407. pThreadData->hVerifyCtx,
  1408. pDeriveKeyData->aiHash,
  1409. 0, 0, &hCompareHash))
  1410. {
  1411. printf("CryptCreateHash compare 2 ");
  1412. goto Ret;
  1413. }
  1414. if (! CryptHashData(
  1415. hCompareHash, rgbStringB, sizeof(rgbStringB), 0))
  1416. {
  1417. printf("CryptHashData compare 2 ");
  1418. goto Ret;
  1419. }
  1420. cbHash = sizeof(rgbHashB);
  1421. if (! CryptGetHashParam(
  1422. hCompareHash, HP_HASHVAL,
  1423. rgbHashB,
  1424. &cbHash, 0))
  1425. {
  1426. printf("CryptGetHashParam 2 ");
  1427. goto Ret;
  1428. }
  1429. cb = sizeof(BLOBHEADER) + sizeof(DWORD);
  1430. memcpy(
  1431. rgbKey + cb,
  1432. rgbHashA,
  1433. min(cbKey, cbHash));
  1434. if (cbKey > cbHash)
  1435. {
  1436. cb += cbHash;
  1437. memcpy(
  1438. rgbKey + cb,
  1439. rgbHashB,
  1440. min(cbKey - cbHash, cbHash));
  1441. }
  1442. if (! CryptImportKey(
  1443. pThreadData->hVerifyCtx,
  1444. rgbKey,
  1445. sizeof(BLOBHEADER) + sizeof(DWORD) + cbKey,
  1446. 0, 0, &hCompareKey))
  1447. {
  1448. printf("CryptImportKey ");
  1449. goto Ret;
  1450. }
  1451. cbCompareCiphertext = pDeriveKeyData->cbPlaintext;
  1452. if (! CryptEncrypt(
  1453. hCompareKey,
  1454. 0, TRUE, 0, NULL,
  1455. &cbCompareCiphertext, 0))
  1456. {
  1457. printf("CryptEncrypt size compare ");
  1458. goto Ret;
  1459. }
  1460. if (NULL == (pbCompareCiphertext =
  1461. (PBYTE) MyAlloc(cbCompareCiphertext)))
  1462. return ERROR_NOT_ENOUGH_MEMORY;
  1463. memcpy(
  1464. pbCompareCiphertext,
  1465. pDeriveKeyData->pbPlaintext,
  1466. pDeriveKeyData->cbPlaintext);
  1467. cb = pDeriveKeyData->cbPlaintext;
  1468. if (! CryptEncrypt(
  1469. hCompareKey,
  1470. 0, TRUE, 0,
  1471. pbCompareCiphertext,
  1472. &cb,
  1473. cbCompareCiphertext))
  1474. {
  1475. printf("CryptEncrypt compare ");
  1476. goto Ret;
  1477. }
  1478. //
  1479. // Now compare cipher results for the two methods.
  1480. //
  1481. if (cbCompareCiphertext != cbDeriveCiphertext ||
  1482. 0 != memcmp(
  1483. pbCompareCiphertext, pbDeriveCiphertext, cbDeriveCiphertext))
  1484. {
  1485. printf(
  1486. "Cipher result mismatch for KeyAlg %x ; HashAlg %x",
  1487. pDeriveKeyData->aiKey,
  1488. pDeriveKeyData->aiHash);
  1489. PrintBytes(
  1490. "Derived ciphertext",
  1491. pbDeriveCiphertext,
  1492. cbDeriveCiphertext);
  1493. PrintBytes(
  1494. "Compare ciphertext",
  1495. pbCompareCiphertext,
  1496. cbCompareCiphertext);
  1497. }
  1498. //
  1499. // Now decrypt with both keys and compare result.
  1500. //
  1501. if (! CryptDecrypt(
  1502. hDeriveKey,
  1503. 0, TRUE, 0,
  1504. pbDeriveCiphertext,
  1505. &cbDeriveCiphertext))
  1506. {
  1507. printf("CryptDecrypt ");
  1508. goto Ret;
  1509. }
  1510. if (! CryptDecrypt(
  1511. hCompareKey,
  1512. 0, TRUE, 0,
  1513. pbCompareCiphertext,
  1514. &cbCompareCiphertext))
  1515. {
  1516. printf("CryptDecrypt compare ");
  1517. goto Ret;
  1518. }
  1519. if (cbCompareCiphertext != cbDeriveCiphertext ||
  1520. 0 != memcmp(
  1521. pbCompareCiphertext, pbDeriveCiphertext, cbDeriveCiphertext))
  1522. {
  1523. printf(
  1524. "Decrypt result mismatch for KeyAlg %x ; HashAlg %x",
  1525. pDeriveKeyData->aiKey,
  1526. pDeriveKeyData->aiHash);
  1527. PrintBytes(
  1528. "Derived plaintext",
  1529. pbDeriveCiphertext,
  1530. cbDeriveCiphertext);
  1531. PrintBytes(
  1532. "Compare plaintext",
  1533. pbCompareCiphertext,
  1534. cbCompareCiphertext);
  1535. }
  1536. fSuccess = TRUE;
  1537. Ret:
  1538. if (! fSuccess)
  1539. {
  1540. dwError = GetLastError();
  1541. if (0 == dwError)
  1542. dwError = -1;
  1543. }
  1544. if (hDeriveKey)
  1545. CryptDestroyKey(hDeriveKey);
  1546. if (hCompareKey)
  1547. CryptDestroyKey(hCompareKey);
  1548. if (hDeriveHash)
  1549. CryptDestroyHash(hDeriveHash);
  1550. if (hCompareHash)
  1551. CryptDestroyHash(hCompareHash);
  1552. if (pbDeriveCiphertext)
  1553. MyFree(pbDeriveCiphertext);
  1554. if (pbCompareCiphertext)
  1555. MyFree(pbCompareCiphertext);
  1556. return dwError;
  1557. }
  1558. //
  1559. // Function: AesDeriveKeyRegression
  1560. //
  1561. DWORD AesDeriveKeyRegression(PTHREAD_DATA pThreadData)
  1562. {
  1563. DWORD dwError = ERROR_SUCCESS;
  1564. BOOL fSuccess = FALSE;
  1565. DERIVE_KEY_DATA DeriveKeyData;
  1566. BYTE rgbPlaintext[99];
  1567. LPSTR pszPassword = "My Password";
  1568. ALG_ID Hashes[] = { CALG_SHA, CALG_MD5 };
  1569. DWORD cHashes = sizeof(Hashes) / sizeof(ALG_ID);
  1570. ALG_ID Keys[] = { CALG_AES_128, CALG_AES_192, CALG_AES_256 };
  1571. DWORD cKeys = sizeof(Keys) / sizeof(ALG_ID);
  1572. unsigned int i, iHash, iKey;
  1573. for (i = 0; i < sizeof(rgbPlaintext); i++)
  1574. rgbPlaintext[i] = (BYTE) i;
  1575. DeriveKeyData.pbPlaintext = rgbPlaintext;
  1576. DeriveKeyData.cbPlaintext = sizeof(rgbPlaintext);
  1577. DeriveKeyData.pbPassword = (PBYTE) pszPassword;
  1578. DeriveKeyData.cbPassword = strlen(pszPassword);
  1579. for (iHash = 0; iHash < cHashes; iHash++)
  1580. {
  1581. DeriveKeyData.aiHash = Hashes[iHash];
  1582. for (iKey = 0; iKey < cKeys; iKey++)
  1583. {
  1584. DeriveKeyData.aiKey = Keys[iKey];
  1585. if (ERROR_SUCCESS !=
  1586. (dwError = DeriveAndCompareAesKey(
  1587. pThreadData, &DeriveKeyData)))
  1588. {
  1589. printf("DeriveAndCompareAesKey ");
  1590. goto Ret;
  1591. }
  1592. }
  1593. }
  1594. fSuccess = TRUE;
  1595. Ret:
  1596. if (! fSuccess)
  1597. {
  1598. printf("- error 0x%x\n", dwError);
  1599. if (0 == dwError)
  1600. dwError = -1;
  1601. }
  1602. return dwError;
  1603. }
  1604. //
  1605. // Function: UnalignedImportExportRegression
  1606. //
  1607. DWORD UnalignedImportExportRegression(PTHREAD_DATA pThreadData)
  1608. {
  1609. DWORD dwError = ERROR_SUCCESS;
  1610. BYTE rgbKeyBuf[2000];
  1611. DWORD cbKeyBuf = sizeof(rgbKeyBuf);
  1612. PBYTE pbKeyBuf = NULL;
  1613. HCRYPTKEY hKey = 0;
  1614. HCRYPTKEY hWrapKey = 0;
  1615. BOOL fSuccess = FALSE;
  1616. if (! CryptGenKey(
  1617. pThreadData->hProv, CALG_RC2, 0, &hWrapKey))
  1618. {
  1619. printf("CryptGenKey CALG_RC2 ");
  1620. goto Ret;
  1621. }
  1622. //
  1623. // 1a) Exchange key pair PRIVATEKEYBLOB
  1624. //
  1625. // Unalign the output buffer
  1626. pbKeyBuf = rgbKeyBuf + 1;
  1627. cbKeyBuf = sizeof(rgbKeyBuf) - 1;
  1628. if (! CryptExportKey(
  1629. pThreadData->hExchangeKey, 0, PRIVATEKEYBLOB, 0, pbKeyBuf, &cbKeyBuf))
  1630. {
  1631. printf("CryptExportKey KeyEx PRIVATEKEYBLOB ");
  1632. goto Ret;
  1633. }
  1634. if (! CryptImportKey(
  1635. pThreadData->hProv, pbKeyBuf, cbKeyBuf, 0, CRYPT_EXPORTABLE, &hKey))
  1636. {
  1637. printf("CryptImportKey KeyEx PRIVATEKEYBLOB ");
  1638. goto Ret;
  1639. }
  1640. if (! CryptDestroyKey(hKey))
  1641. {
  1642. printf("CryptDestroyKey KeyEx PRIVATEKEYBLOB ");
  1643. goto Ret;
  1644. }
  1645. //
  1646. // 1b) Exchange key pair PRIVATEKEYBLOB, encrypted
  1647. //
  1648. // Unalign the output buffer
  1649. pbKeyBuf = rgbKeyBuf + 1;
  1650. cbKeyBuf = sizeof(rgbKeyBuf) - 1;
  1651. if (! CryptExportKey(
  1652. pThreadData->hExchangeKey,
  1653. hWrapKey,
  1654. PRIVATEKEYBLOB,
  1655. 0,
  1656. pbKeyBuf,
  1657. &cbKeyBuf))
  1658. {
  1659. printf("CryptExportKey KeyEx PRIVATEKEYBLOB b ");
  1660. goto Ret;
  1661. }
  1662. if (! CryptImportKey(
  1663. pThreadData->hProv,
  1664. pbKeyBuf,
  1665. cbKeyBuf,
  1666. hWrapKey,
  1667. CRYPT_EXPORTABLE,
  1668. &hKey))
  1669. {
  1670. printf("CryptImportKey KeyEx PRIVATEKEYBLOB b ");
  1671. goto Ret;
  1672. }
  1673. if (! CryptDestroyKey(hKey))
  1674. {
  1675. printf("CryptDestroyKey KeyEx PRIVATEKEYBLOB b ");
  1676. goto Ret;
  1677. }
  1678. //
  1679. // 2) Exchange key pair PUBLICKEYBLOB
  1680. //
  1681. // Unalign the output buffer
  1682. pbKeyBuf = rgbKeyBuf + 1;
  1683. cbKeyBuf = sizeof(rgbKeyBuf) - 1;
  1684. if (! CryptExportKey(
  1685. pThreadData->hExchangeKey, 0, PUBLICKEYBLOB, 0, pbKeyBuf, &cbKeyBuf))
  1686. {
  1687. printf("CryptExportKey KeyEx PUBLICKEYBLOB ");
  1688. goto Ret;
  1689. }
  1690. if (! CryptImportKey(
  1691. pThreadData->hProv,
  1692. pbKeyBuf,
  1693. cbKeyBuf,
  1694. pThreadData->dwProvType == PROV_DSS_DH ? pThreadData->hExchangeKey : 0,
  1695. 0,
  1696. &hKey))
  1697. {
  1698. printf("CryptImportKey KeyEx PUBLICKEYBLOB ");
  1699. goto Ret;
  1700. }
  1701. if (! CryptDestroyKey(hKey))
  1702. {
  1703. printf("CryptDestroyKey KeyEx PUBLICKEYBLOB ");
  1704. goto Ret;
  1705. }
  1706. //
  1707. // 3a) Signature key pair PRIVATEKEYBLOB
  1708. //
  1709. // Unalign the output buffer
  1710. pbKeyBuf = rgbKeyBuf + 1;
  1711. cbKeyBuf = sizeof(rgbKeyBuf) - 1;
  1712. if (! CryptExportKey(
  1713. pThreadData->hSignatureKey, 0, PRIVATEKEYBLOB, 0, pbKeyBuf, &cbKeyBuf))
  1714. {
  1715. printf("CryptExportKey Sig PRIVATEKEYBLOB ");
  1716. goto Ret;
  1717. }
  1718. if (! CryptImportKey(
  1719. pThreadData->hProv, pbKeyBuf, cbKeyBuf, 0, CRYPT_EXPORTABLE, &hKey))
  1720. {
  1721. printf("CryptImportKey Sig PRIVATEKEYBLOB ");
  1722. goto Ret;
  1723. }
  1724. if (! CryptDestroyKey(hKey))
  1725. {
  1726. printf("CryptDestroyKey Sig PRIVATEKEYBLOB ");
  1727. goto Ret;
  1728. }
  1729. //
  1730. // 3b) Signature key pair PRIVATEKEYBLOB, encrypted
  1731. //
  1732. // Unalign the output buffer
  1733. pbKeyBuf = rgbKeyBuf + 1;
  1734. cbKeyBuf = sizeof(rgbKeyBuf) - 1;
  1735. if (! CryptExportKey(
  1736. pThreadData->hSignatureKey,
  1737. hWrapKey,
  1738. PRIVATEKEYBLOB,
  1739. 0,
  1740. pbKeyBuf,
  1741. &cbKeyBuf))
  1742. {
  1743. printf("CryptExportKey Sig PRIVATEKEYBLOB b ");
  1744. goto Ret;
  1745. }
  1746. if (! CryptImportKey(
  1747. pThreadData->hProv,
  1748. pbKeyBuf,
  1749. cbKeyBuf,
  1750. hWrapKey,
  1751. CRYPT_EXPORTABLE,
  1752. &hKey))
  1753. {
  1754. printf("CryptImportKey Sig PRIVATEKEYBLOB b ");
  1755. goto Ret;
  1756. }
  1757. if (! CryptDestroyKey(hKey))
  1758. {
  1759. printf("CryptDestroyKey Sig PRIVATEKEYBLOB b ");
  1760. goto Ret;
  1761. }
  1762. if (! CryptDestroyKey(hWrapKey))
  1763. {
  1764. printf("CryptDestroyKey CALG_RC2 ");
  1765. goto Ret;
  1766. }
  1767. //
  1768. // 4) Signature key pair PUBLICKEYBLOB
  1769. //
  1770. // Unalign the output buffer
  1771. pbKeyBuf = rgbKeyBuf + 1;
  1772. cbKeyBuf = sizeof(rgbKeyBuf) - 1;
  1773. if (! CryptExportKey(
  1774. pThreadData->hSignatureKey, 0, PUBLICKEYBLOB, 0, pbKeyBuf, &cbKeyBuf))
  1775. {
  1776. printf("CryptExportKey Sig PUBLICKEYBLOB ");
  1777. goto Ret;
  1778. }
  1779. if (! CryptImportKey(
  1780. pThreadData->hProv, pbKeyBuf, cbKeyBuf, 0, 0, &hKey))
  1781. {
  1782. printf("CryptImportKey Sig PUBLICKEYBLOB ");
  1783. goto Ret;
  1784. }
  1785. if (! CryptDestroyKey(hKey))
  1786. {
  1787. printf("CryptDestroyKey Sig PUBLICKEYBLOB ");
  1788. goto Ret;
  1789. }
  1790. //
  1791. // 5) SIMPLEBLOB
  1792. //
  1793. if (pThreadData->dwProvType != PROV_DSS_DH &&
  1794. pThreadData->dwProvType != PROV_DSS)
  1795. {
  1796. //
  1797. // 5.1) Use PKCS2 padding.
  1798. //
  1799. if (! CryptGenKey(
  1800. pThreadData->hProv, CALG_RC2, CRYPT_EXPORTABLE, &hKey))
  1801. {
  1802. printf("CryptGenKey CALG_RC2 SIMPLEBLOB ");
  1803. goto Ret;
  1804. }
  1805. // Unalign the output buffer
  1806. pbKeyBuf = rgbKeyBuf + 1;
  1807. cbKeyBuf = sizeof(rgbKeyBuf) - 1;
  1808. if (! CryptExportKey(
  1809. hKey, pThreadData->hExchangeKey, SIMPLEBLOB, 0, pbKeyBuf, &cbKeyBuf))
  1810. {
  1811. printf("CryptExportKey SIMPLEBLOB ");
  1812. goto Ret;
  1813. }
  1814. if (! CryptDestroyKey(hKey))
  1815. {
  1816. printf("CryptDestroyKey CALG_RC2 A SIMPLEBLOB ");
  1817. goto Ret;
  1818. }
  1819. if (! CryptImportKey(
  1820. pThreadData->hProv, pbKeyBuf, cbKeyBuf, pThreadData->hExchangeKey, 0, &hKey))
  1821. {
  1822. printf("CryptImportKey SIMPLEBLOB ");
  1823. goto Ret;
  1824. }
  1825. if (! CryptDestroyKey(hKey))
  1826. {
  1827. printf("CryptDestroyKey CALG_RC2 B SIMPLEBLOB ");
  1828. goto Ret;
  1829. }
  1830. //
  1831. // 5.2) Use OAEP padding.
  1832. //
  1833. if (! CryptGenKey(
  1834. pThreadData->hProv, CALG_RC2, CRYPT_EXPORTABLE, &hKey))
  1835. {
  1836. printf("CryptGenKey CALG_RC2 SIMPLEBLOB OAEP ");
  1837. goto Ret;
  1838. }
  1839. // Unalign the output buffer
  1840. pbKeyBuf = rgbKeyBuf + 1;
  1841. cbKeyBuf = sizeof(rgbKeyBuf) - 1;
  1842. if (! CryptExportKey(
  1843. hKey, pThreadData->hExchangeKey, SIMPLEBLOB, CRYPT_OAEP, pbKeyBuf, &cbKeyBuf))
  1844. {
  1845. printf("CryptExportKey SIMPLEBLOB OAEP ");
  1846. goto Ret;
  1847. }
  1848. if (! CryptDestroyKey(hKey))
  1849. {
  1850. printf("CryptDestroyKey CALG_RC2 A SIMPLEBLOB OAEP ");
  1851. goto Ret;
  1852. }
  1853. if (! CryptImportKey(
  1854. pThreadData->hProv, pbKeyBuf, cbKeyBuf, pThreadData->hExchangeKey, CRYPT_OAEP, &hKey))
  1855. {
  1856. printf("CryptImportKey SIMPLEBLOB OAEP ");
  1857. goto Ret;
  1858. }
  1859. if (! CryptDestroyKey(hKey))
  1860. {
  1861. printf("CryptDestroyKey CALG_RC2 B SIMPLEBLOB OAEP ");
  1862. goto Ret;
  1863. }
  1864. }
  1865. //
  1866. // 6) SYMMETRICWRAPKEYBLOB
  1867. //
  1868. if (! CryptGenKey(
  1869. pThreadData->hProv, CALG_RC2, CRYPT_EXPORTABLE, &hKey))
  1870. {
  1871. printf("CryptGenKey CALG_RC2 SYMMETRICWRAPKEYBLOB A ");
  1872. goto Ret;
  1873. }
  1874. if (! CryptGenKey(
  1875. pThreadData->hProv, CALG_RC2, CRYPT_EXPORTABLE, &hWrapKey))
  1876. {
  1877. printf("CryptGenKey CALG_RC2 SYMMETRICWRAPKEYBLOB B ");
  1878. goto Ret;
  1879. }
  1880. // Unalign the output buffer
  1881. pbKeyBuf = rgbKeyBuf + 1;
  1882. cbKeyBuf = sizeof(rgbKeyBuf) - 1;
  1883. if (! CryptExportKey(
  1884. hKey, hWrapKey, SYMMETRICWRAPKEYBLOB, 0, pbKeyBuf, &cbKeyBuf))
  1885. {
  1886. printf("CryptExportKey SYMMETRICWRAPKEYBLOB ");
  1887. goto Ret;
  1888. }
  1889. if (! CryptDestroyKey(hKey))
  1890. {
  1891. printf("CryptDestroyKey CALG_RC2 A SYMMETRICWRAPKEYBLOB ");
  1892. goto Ret;
  1893. }
  1894. if (! CryptImportKey(
  1895. pThreadData->hProv, pbKeyBuf, cbKeyBuf, hWrapKey, 0, &hKey))
  1896. {
  1897. printf("CryptImportKey SYMMETRICWRAPKEYBLOB ");
  1898. goto Ret;
  1899. }
  1900. if (! CryptDestroyKey(hKey))
  1901. {
  1902. printf("CryptDestroyKey CALG_RC2 B SYMMETRICWRAPKEYBLOB ");
  1903. goto Ret;
  1904. }
  1905. if (! CryptDestroyKey(hWrapKey))
  1906. {
  1907. printf("CryptDestroyKey CALG_RC2 C SYMMETRICWRAPKEYBLOB ");
  1908. goto Ret;
  1909. }
  1910. //
  1911. // 7) PLAINTEXTKEYBLOB
  1912. //
  1913. if (! CryptGenKey(
  1914. pThreadData->hProv, CALG_RC2, CRYPT_EXPORTABLE, &hKey))
  1915. {
  1916. printf("CryptGenKey CALG_RC2 PLAINTEXTKEYBLOB ");
  1917. goto Ret;
  1918. }
  1919. // Unalign the output buffer
  1920. pbKeyBuf = rgbKeyBuf + 1;
  1921. cbKeyBuf = sizeof(rgbKeyBuf) - 1;
  1922. if (! CryptExportKey(
  1923. hKey, 0, PLAINTEXTKEYBLOB, 0, pbKeyBuf, &cbKeyBuf))
  1924. {
  1925. printf("CryptExportKey PLAINTEXTKEYBLOB ");
  1926. goto Ret;
  1927. }
  1928. if (! CryptDestroyKey(hKey))
  1929. {
  1930. printf("CryptDestroyKey CALG_RC2 A PLAINTEXTKEYBLOB ");
  1931. goto Ret;
  1932. }
  1933. if (! CryptImportKey(
  1934. pThreadData->hProv, pbKeyBuf, cbKeyBuf, 0, 0, &hKey))
  1935. {
  1936. printf("CryptImportKey PLAINTEXTKEYBLOB ");
  1937. goto Ret;
  1938. }
  1939. if (! CryptDestroyKey(hKey))
  1940. {
  1941. printf("CryptDestroyKey CALG_RC2 B PLAINTEXTKEYBLOB ");
  1942. goto Ret;
  1943. }
  1944. fSuccess = TRUE;
  1945. Ret:
  1946. if (! fSuccess)
  1947. printf("- error 0x%x\n", dwError = GetLastError());
  1948. return dwError;
  1949. }
  1950. static BYTE rgbPrivateKeyWithExponentOfOne[] =
  1951. {
  1952. 0x07, 0x02, 0x00, 0x00, 0x00, 0xA4, 0x00, 0x00,
  1953. 0x52, 0x53, 0x41, 0x32, 0x00, 0x02, 0x00, 0x00,
  1954. 0x01, 0x00, 0x00, 0x00, 0xAB, 0xEF, 0xFA, 0xC6,
  1955. 0x7D, 0xE8, 0xDE, 0xFB, 0x68, 0x38, 0x09, 0x92,
  1956. 0xD9, 0x42, 0x7E, 0x6B, 0x89, 0x9E, 0x21, 0xD7,
  1957. 0x52, 0x1C, 0x99, 0x3C, 0x17, 0x48, 0x4E, 0x3A,
  1958. 0x44, 0x02, 0xF2, 0xFA, 0x74, 0x57, 0xDA, 0xE4,
  1959. 0xD3, 0xC0, 0x35, 0x67, 0xFA, 0x6E, 0xDF, 0x78,
  1960. 0x4C, 0x75, 0x35, 0x1C, 0xA0, 0x74, 0x49, 0xE3,
  1961. 0x20, 0x13, 0x71, 0x35, 0x65, 0xDF, 0x12, 0x20,
  1962. 0xF5, 0xF5, 0xF5, 0xC1, 0xED, 0x5C, 0x91, 0x36,
  1963. 0x75, 0xB0, 0xA9, 0x9C, 0x04, 0xDB, 0x0C, 0x8C,
  1964. 0xBF, 0x99, 0x75, 0x13, 0x7E, 0x87, 0x80, 0x4B,
  1965. 0x71, 0x94, 0xB8, 0x00, 0xA0, 0x7D, 0xB7, 0x53,
  1966. 0xDD, 0x20, 0x63, 0xEE, 0xF7, 0x83, 0x41, 0xFE,
  1967. 0x16, 0xA7, 0x6E, 0xDF, 0x21, 0x7D, 0x76, 0xC0,
  1968. 0x85, 0xD5, 0x65, 0x7F, 0x00, 0x23, 0x57, 0x45,
  1969. 0x52, 0x02, 0x9D, 0xEA, 0x69, 0xAC, 0x1F, 0xFD,
  1970. 0x3F, 0x8C, 0x4A, 0xD0,
  1971. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1972. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1973. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1974. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1975. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1976. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1977. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1978. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1979. 0x64, 0xD5, 0xAA, 0xB1,
  1980. 0xA6, 0x03, 0x18, 0x92, 0x03, 0xAA, 0x31, 0x2E,
  1981. 0x48, 0x4B, 0x65, 0x20, 0x99, 0xCD, 0xC6, 0x0C,
  1982. 0x15, 0x0C, 0xBF, 0x3E, 0xFF, 0x78, 0x95, 0x67,
  1983. 0xB1, 0x74, 0x5B, 0x60,
  1984. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1985. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1986. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1987. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1988. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1989. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1990. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1991. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1992. };
  1993. //
  1994. // 64-bit and 56-bit DES SIMPLEBLOB's exported with
  1995. // above public key. These keys are functionally
  1996. // equivalent on Windows 2000 due to a buffer overrun
  1997. // bug.
  1998. //
  1999. static BYTE g_rgbDes56BitKeyBlob[] =
  2000. {
  2001. 0x01, 0x02, 0x00, 0x00, 0x01, 0x66, 0x00, 0x00,
  2002. 0x00, 0xa4, 0x00, 0x00, 0x29, 0x32, 0xc4, 0xd0,
  2003. 0x75, 0x25, 0xa4, 0x00, 0xa8, 0x6f, 0x02, 0x35,
  2004. 0x0e, 0x53, 0x75, 0xaa, 0xad, 0x8d, 0x21, 0x67,
  2005. 0xf6, 0x8a, 0x93, 0x78, 0x12, 0x27, 0x5c, 0xd1,
  2006. 0xa2, 0x13, 0x0f, 0xab, 0xe4, 0x68, 0x8e, 0x28,
  2007. 0xcc, 0x3e, 0xf1, 0xa5, 0x52, 0xe4, 0xf7, 0xa4,
  2008. 0x57, 0xaa, 0x86, 0x93, 0xc8, 0x73, 0xb1, 0x9f,
  2009. 0x77, 0xc8, 0x84, 0x97, 0xe4, 0xad, 0x63, 0xad,
  2010. 0x5d, 0x76, 0x02, 0x00
  2011. };
  2012. static BYTE g_rgbDes64BitKeyBlob[] =
  2013. {
  2014. 0x01, 0x02, 0x00, 0x00, 0x01, 0x66, 0x00, 0x00,
  2015. 0x00, 0xa4, 0x00, 0x00, 0x00, 0x29, 0x32, 0xc4,
  2016. 0xd0, 0x75, 0x25, 0xa4, 0x00, 0xa8, 0x6f, 0x02,
  2017. 0x0e, 0x53, 0x75, 0xaa, 0xad, 0x8d, 0x21, 0x67,
  2018. 0xf6, 0x8a, 0x93, 0x78, 0x12, 0x27, 0x5c, 0xd1,
  2019. 0xa2, 0x13, 0x0f, 0xab, 0xe4, 0x68, 0x8e, 0x28,
  2020. 0xcc, 0x3e, 0xf1, 0xa5, 0x52, 0xe4, 0xf7, 0xa4,
  2021. 0x57, 0xaa, 0x86, 0x93, 0xc8, 0x73, 0xb1, 0x9f,
  2022. 0x77, 0xc8, 0x84, 0x97, 0xe4, 0xad, 0x63, 0xad,
  2023. 0x5d, 0x76, 0x02, 0x00
  2024. };
  2025. static BYTE g_rgbDesPlainText[] =
  2026. {
  2027. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  2028. };
  2029. static BYTE g_rgbDesCipherText[] =
  2030. {
  2031. 0x3b, 0xd9, 0x09, 0xfb, 0xd6, 0xa7, 0x9c, 0x37,
  2032. 0xf6, 0x5d, 0xe1, 0x50, 0x6d, 0x39, 0xb0, 0x0c
  2033. };
  2034. //
  2035. // 112 and 128 bit "equivalent" 3Des-2Key blobs
  2036. //
  2037. static BYTE g_rgbDes112BitKeyBlob[] =
  2038. {
  2039. 0x01, 0x02, 0x00, 0x00, 0x09, 0x66, 0x00, 0x00,
  2040. 0x00, 0xa4, 0x00, 0x00, 0x29, 0x32, 0xc4, 0xd0,
  2041. 0x75, 0x25, 0xa4, 0x75, 0xa8, 0x6f, 0x02, 0x35,
  2042. 0x0e, 0x53, 0x00, 0xaa, 0xad, 0x8d, 0x21, 0x67,
  2043. 0xf6, 0x8a, 0x93, 0x78, 0x12, 0x27, 0x5c, 0xd1,
  2044. 0xa2, 0x13, 0x0f, 0xab, 0xe4, 0x68, 0x8e, 0x28,
  2045. 0xcc, 0x3e, 0xf1, 0xa5, 0x52, 0xe4, 0xf7, 0xa4,
  2046. 0x57, 0xaa, 0x86, 0x93, 0xc8, 0x73, 0xb1, 0x9f,
  2047. 0x77, 0xc8, 0x84, 0x97, 0xe4, 0xad, 0x63, 0xad,
  2048. 0x5d, 0x76, 0x02, 0x00
  2049. };
  2050. static BYTE g_rgbDes128BitKeyBlob[] =
  2051. {
  2052. 0x01, 0x02, 0x00, 0x00, 0x09, 0x66, 0x00, 0x00,
  2053. 0x00, 0xa4, 0x00, 0x00, 0x00, 0x00, 0x29, 0x32,
  2054. 0xc4, 0xd0, 0x75, 0x25, 0xa4, 0x75, 0xa8, 0x6f,
  2055. 0x02, 0x35, 0x0e, 0x53, 0x00, 0xaa, 0xad, 0x8d,
  2056. 0xf6, 0x8a, 0x93, 0x78, 0x12, 0x27, 0x5c, 0xd1,
  2057. 0xa2, 0x13, 0x0f, 0xab, 0xe4, 0x68, 0x8e, 0x28,
  2058. 0xcc, 0x3e, 0xf1, 0xa5, 0x52, 0xe4, 0xf7, 0xa4,
  2059. 0x57, 0xaa, 0x86, 0x93, 0xc8, 0x73, 0xb1, 0x9f,
  2060. 0x77, 0xc8, 0x84, 0x97, 0xe4, 0xad, 0x63, 0xad,
  2061. 0x5d, 0x76, 0x02, 0x00
  2062. };
  2063. static BYTE g_rgb2DesCipherText[] =
  2064. {
  2065. 0x56, 0x03, 0xdf, 0x55, 0xeb, 0xfb, 0x76, 0x1f,
  2066. 0x93, 0x38, 0xd7, 0xef, 0x8f, 0x38, 0x76, 0x49
  2067. };
  2068. //
  2069. // 168 and 192 bit "equivalent" 3Des blobs
  2070. //
  2071. static BYTE g_rgbDes168BitKeyBlob[] =
  2072. {
  2073. 0x01, 0x02, 0x00, 0x00, 0x03, 0x66, 0x00, 0x00,
  2074. 0x00, 0xa4, 0x00, 0x00, 0x29, 0x32, 0xc4, 0xd0,
  2075. 0x75, 0x25, 0xa4, 0x8a, 0xa8, 0x6f, 0x02, 0x35,
  2076. 0x0e, 0x53, 0x75, 0xaa, 0xad, 0x8d, 0x21, 0x67,
  2077. 0xf6, 0x00, 0x93, 0x78, 0x12, 0x27, 0x5c, 0xd1,
  2078. 0xa2, 0x13, 0x0f, 0xab, 0xe4, 0x68, 0x8e, 0x28,
  2079. 0xcc, 0x3e, 0xf1, 0xa5, 0x52, 0xe4, 0xf7, 0xa4,
  2080. 0x57, 0xaa, 0x86, 0x93, 0xc8, 0x73, 0xb1, 0x9f,
  2081. 0x77, 0xc8, 0x84, 0x97, 0xe4, 0xad, 0x63, 0xad,
  2082. 0x5d, 0x76, 0x02, 0x00
  2083. };
  2084. static BYTE g_rgbDes192BitKeyBlob[] =
  2085. {
  2086. 0x01, 0x02, 0x00, 0x00, 0x03, 0x66, 0x00, 0x00,
  2087. 0x00, 0xa4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x29,
  2088. 0x32, 0xc4, 0xd0, 0x75, 0x25, 0xa4, 0x8a, 0xa8,
  2089. 0x6f, 0x02, 0x35, 0x0e, 0x53, 0x75, 0xaa, 0xad,
  2090. 0x8d, 0x21, 0x67, 0xf6, 0x00, 0x93, 0x78, 0x12,
  2091. 0xa2, 0x13, 0x0f, 0xab, 0xe4, 0x68, 0x8e, 0x28,
  2092. 0xcc, 0x3e, 0xf1, 0xa5, 0x52, 0xe4, 0xf7, 0xa4,
  2093. 0x57, 0xaa, 0x86, 0x93, 0xc8, 0x73, 0xb1, 0x9f,
  2094. 0x77, 0xc8, 0x84, 0x97, 0xe4, 0xad, 0x63, 0xad,
  2095. 0x5d, 0x76, 0x02, 0x00
  2096. };
  2097. static BYTE g_rgb3DesCipherText[] =
  2098. {
  2099. 0x25, 0x25, 0x14, 0x94, 0x6b, 0xe0, 0x69, 0x21,
  2100. 0xea, 0x3d, 0xb5, 0xa6, 0x5b, 0xaa, 0x6c, 0x87
  2101. };
  2102. //
  2103. // Function: DesImportEquivalenceTest
  2104. // Purpose: Verify that the provided des key correctly
  2105. // encrypts the above rgbDesPlainText.
  2106. //
  2107. DWORD DesImportEquivalenceTest(
  2108. PTHREAD_DATA pThreadData,
  2109. PBYTE pbDesKey,
  2110. DWORD cbDesKey,
  2111. PBYTE pbDesShortKey,
  2112. DWORD cbDesShortKey,
  2113. PBYTE pbCipherText,
  2114. DWORD cbCipherText)
  2115. {
  2116. DWORD dwError = ERROR_SUCCESS;
  2117. BOOL fSuccess = FALSE;
  2118. HCRYPTKEY hPubKey = 0;
  2119. HCRYPTKEY hDesKey = 0;
  2120. PBYTE pb = NULL;
  2121. DWORD cb = 0;
  2122. BYTE rgbPlain[sizeof(g_rgbDesPlainText) * 2];
  2123. DWORD cbPlain = sizeof(rgbPlain);
  2124. BOOL fBlobError = FALSE;
  2125. PBYTE p1 = NULL, p2 = NULL, p3 = NULL;
  2126. if (! CryptImportKey(
  2127. pThreadData->hVerifyCtx, rgbPrivateKeyWithExponentOfOne,
  2128. sizeof(rgbPrivateKeyWithExponentOfOne), 0, 0, &hPubKey))
  2129. {
  2130. printf("CryptImportKey privatekeywithexponentofone ");
  2131. goto Ret;
  2132. }
  2133. // Try to import the short key; should fail
  2134. if (CryptImportKey(
  2135. pThreadData->hVerifyCtx, pbDesShortKey, cbDesShortKey,
  2136. hPubKey, 0, &hDesKey))
  2137. {
  2138. printf("CryptImportKey ShortDesKey should've failed ");
  2139. goto Ret;
  2140. }
  2141. PrintBytes("Testing this des key SIMPLEBLOB", pbDesKey, cbDesKey);
  2142. if (! CryptImportKey(
  2143. pThreadData->hVerifyCtx, pbDesKey, cbDesKey,
  2144. hPubKey, CRYPT_EXPORTABLE, &hDesKey))
  2145. {
  2146. printf("CryptImportKey deskeyblob ");
  2147. goto Ret;
  2148. }
  2149. cb = sizeof(g_rgbDesPlainText);
  2150. memcpy(rgbPlain, g_rgbDesPlainText, cb);
  2151. if (! CryptEncrypt(
  2152. hDesKey, 0, TRUE, 0, rgbPlain, &cb, cbPlain))
  2153. {
  2154. printf("CryptEncrypt ");
  2155. goto Ret;
  2156. }
  2157. if (0 != memcmp(rgbPlain, pbCipherText, cbCipherText))
  2158. {
  2159. printf("Cipher text doesn't match\n");
  2160. PrintBytes("Expected cipher text", pbCipherText, cbCipherText);
  2161. PrintBytes("Actual cipher text", rgbPlain, sizeof(rgbPlain));
  2162. fBlobError = TRUE;
  2163. }
  2164. if (! CryptExportKey(
  2165. hDesKey, hPubKey, SIMPLEBLOB, 0, NULL, &cb))
  2166. {
  2167. printf("CryptExportKey size ");
  2168. goto Ret;
  2169. }
  2170. if (NULL == (pb = (PBYTE) MyAlloc(cb)))
  2171. return ERROR_NOT_ENOUGH_MEMORY;
  2172. if (! CryptExportKey(
  2173. hDesKey, hPubKey, SIMPLEBLOB, 0, pb, &cb))
  2174. {
  2175. printf("CryptExportKey ");
  2176. goto Ret;
  2177. }
  2178. if (0 != memcmp(
  2179. pb, pbDesKey,
  2180. sizeof(SIMPLEBLOB) + sizeof(ALG_ID) + 8))
  2181. {
  2182. printf("Header + key portion of blob doesn't match\n");
  2183. PrintBytes("Expected key blob", pbDesKey, cbDesKey);
  2184. PrintBytes("Actual key blob", pb, cb);
  2185. fBlobError = TRUE;
  2186. }
  2187. fSuccess = TRUE;
  2188. Ret:
  2189. if (fBlobError)
  2190. dwError = -1;
  2191. if (! fSuccess)
  2192. printf("- error 0x%x\n", dwError = GetLastError());
  2193. if (hDesKey)
  2194. CryptDestroyKey(hDesKey);
  2195. if (hPubKey)
  2196. CryptDestroyKey(hPubKey);
  2197. if (pb)
  2198. MyFree(pb);
  2199. return dwError;
  2200. }
  2201. //
  2202. // Function: DesImportRegression
  2203. //
  2204. DWORD DesImportRegression(PTHREAD_DATA pThreadData)
  2205. {
  2206. DWORD dwSts;
  2207. DWORD dwError = ERROR_SUCCESS;
  2208. if (ERROR_SUCCESS != (dwSts =
  2209. DesImportEquivalenceTest(
  2210. pThreadData,
  2211. g_rgbDes64BitKeyBlob,
  2212. sizeof(g_rgbDes64BitKeyBlob),
  2213. g_rgbDes56BitKeyBlob,
  2214. sizeof(g_rgbDes56BitKeyBlob),
  2215. g_rgbDesCipherText,
  2216. sizeof(g_rgbDesCipherText))))
  2217. dwError = dwSts;
  2218. if (ERROR_SUCCESS != (dwSts =
  2219. DesImportEquivalenceTest(
  2220. pThreadData,
  2221. g_rgbDes128BitKeyBlob,
  2222. sizeof(g_rgbDes128BitKeyBlob),
  2223. g_rgbDes112BitKeyBlob,
  2224. sizeof(g_rgbDes112BitKeyBlob),
  2225. g_rgb2DesCipherText,
  2226. sizeof(g_rgb2DesCipherText))))
  2227. dwError = dwSts;
  2228. if (ERROR_SUCCESS != (dwSts =
  2229. DesImportEquivalenceTest(
  2230. pThreadData,
  2231. g_rgbDes192BitKeyBlob,
  2232. sizeof(g_rgbDes192BitKeyBlob),
  2233. g_rgbDes168BitKeyBlob,
  2234. sizeof(g_rgbDes168BitKeyBlob),
  2235. g_rgb3DesCipherText,
  2236. sizeof(g_rgb3DesCipherText))))
  2237. dwError = dwSts;
  2238. return dwError;
  2239. }
  2240. static BYTE rgbPlainText[] =
  2241. {
  2242. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  2243. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  2244. };
  2245. static BYTE rgbIV[] =
  2246. {
  2247. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  2248. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  2249. };
  2250. BYTE rgbRC2PlainTextKey [] = {
  2251. 0x08, 0x02, 0x00, 0x00, 0x02, 0x66, 0x00, 0x00,
  2252. 0x10, 0x00, 0x00, 0x00, 0x10, 0x62, 0x0a, 0x8a,
  2253. 0x6b, 0x0d, 0x60, 0xbe, 0xf3, 0x94, 0x99, 0x12,
  2254. 0xef, 0x39, 0xbf, 0x4f
  2255. };
  2256. BYTE rgbRC2CipherText [] = {
  2257. 0xfd, 0x25, 0x3e, 0x7a, 0xff, 0xb5, 0xc2, 0x6e,
  2258. 0x13, 0xcf, 0x52, 0xf1, 0xba, 0xa3, 0x9a, 0xef,
  2259. 0x1c, 0xfb, 0x91, 0x88, 0x9d, 0xf7, 0xe5, 0x12
  2260. };
  2261. BYTE rgbDESPlainTextKey [] = {
  2262. 0x08, 0x02, 0x00, 0x00, 0x01, 0x66, 0x00, 0x00,
  2263. 0x08, 0x00, 0x00, 0x00, 0xef, 0x8f, 0x10, 0xec,
  2264. 0xea, 0x7a, 0x2c, 0x01
  2265. };
  2266. BYTE rgbDESCipherText [] = {
  2267. 0x13, 0x68, 0x16, 0xc5, 0x15, 0x3d, 0x59, 0x1f,
  2268. 0x8e, 0x9c, 0x9c, 0x4f, 0x03, 0x7b, 0xb2, 0x12,
  2269. 0x24, 0xa7, 0x81, 0x5e, 0x68, 0xb1, 0x58, 0xaa
  2270. };
  2271. BYTE rgb3DES112PlainTextKey [] = {
  2272. 0x08, 0x02, 0x00, 0x00, 0x09, 0x66, 0x00, 0x00,
  2273. 0x10, 0x00, 0x00, 0x00, 0x6d, 0x07, 0xcd, 0xe9,
  2274. 0xa4, 0x23, 0xc7, 0x97, 0x4a, 0x4f, 0x5b, 0x2f,
  2275. 0x34, 0x92, 0xb5, 0x92
  2276. };
  2277. BYTE rgb3DES112CipherText [] = {
  2278. 0xf4, 0xfd, 0xde, 0x15, 0xfd, 0x50, 0xaa, 0x3c,
  2279. 0x02, 0xb1, 0x07, 0x3b, 0x0f, 0x0f, 0x93, 0x23,
  2280. 0xc2, 0x23, 0xda, 0x1f, 0x65, 0x81, 0x59, 0x24
  2281. };
  2282. BYTE rgb3DESCipherText [] = {
  2283. 0xa6, 0xae, 0xa2, 0x97, 0xc4, 0x85, 0xda, 0xa7,
  2284. 0x43, 0xc8, 0x5d, 0xf4, 0x97, 0xb4, 0xbc, 0x03,
  2285. 0x96, 0xf9, 0xa2, 0x66, 0x9e, 0x18, 0x91, 0x4a
  2286. };
  2287. BYTE rgb3DESPlainTextKey [] = {
  2288. 0x08, 0x02, 0x00, 0x00, 0x03, 0x66, 0x00, 0x00,
  2289. 0x18, 0x00, 0x00, 0x00, 0xdc, 0x0d, 0x20, 0xf2,
  2290. 0xcb, 0xa8, 0xb6, 0x15, 0x3e, 0x23, 0x38, 0xb6,
  2291. 0x31, 0x62, 0x4a, 0x16, 0xa4, 0x49, 0xe5, 0xe5,
  2292. 0x61, 0x76, 0x75, 0x23
  2293. };
  2294. BYTE rgbAES128PlainTextKey [] = {
  2295. 0x08, 0x02, 0x00, 0x00, 0x0e, 0x66, 0x00, 0x00,
  2296. 0x10, 0x00, 0x00, 0x00,
  2297. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  2298. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  2299. };
  2300. BYTE rgbAES128CipherText [] = {
  2301. 0xc6, 0xa1, 0x3b, 0x37, 0x87, 0x8f, 0x5b, 0x82,
  2302. 0x6f, 0x4f, 0x81, 0x62, 0xa1, 0xc8, 0xd8, 0x79,
  2303. 0xb1, 0xa2, 0x92, 0x73, 0xbe, 0x2c, 0x42, 0x07,
  2304. 0xa5, 0xac, 0xe3, 0x93, 0x39, 0x8c, 0xb6, 0xfb
  2305. };
  2306. BYTE rgbAES192PlainTextKey [] = {
  2307. 0x08, 0x02, 0x00, 0x00, 0x0f, 0x66, 0x00, 0x00,
  2308. 0x18, 0x00, 0x00, 0x00,
  2309. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  2310. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  2311. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  2312. };
  2313. BYTE rgbAES192CipherText [] = {
  2314. 0x91, 0x62, 0x51, 0x82, 0x1c, 0x73, 0xa5, 0x22,
  2315. 0xc3, 0x96, 0xd6, 0x27, 0x38, 0x01, 0x96, 0x07,
  2316. 0x5f, 0x9d, 0x65, 0x29, 0x74, 0x04, 0x30, 0x7e,
  2317. 0x94, 0x97, 0xf4, 0x56, 0x25, 0xd5, 0xfd, 0x48
  2318. };
  2319. BYTE rgbAES256PlainTextKey [] = {
  2320. 0x08, 0x02, 0x00, 0x00, 0x10, 0x66, 0x00, 0x00,
  2321. 0x20, 0x00, 0x00, 0x00,
  2322. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  2323. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  2324. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  2325. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F
  2326. };
  2327. BYTE rgbAES256CipherText [] = {
  2328. 0xf2, 0x90, 0x00, 0xb6, 0x2a, 0x49, 0x9f, 0xd0,
  2329. 0xa9, 0xf3, 0x9a, 0x6a, 0xdd, 0x2e, 0x77, 0x80,
  2330. 0x53, 0xc8, 0x74, 0x2d, 0x0e, 0xa2, 0x9b, 0x27,
  2331. 0x12, 0xf6, 0xc7, 0xaf, 0x40, 0x48, 0xf4, 0xb4
  2332. };
  2333. typedef struct _KnownBlockCipherResult {
  2334. ALG_ID ai;
  2335. BYTE *pbKey;
  2336. DWORD cbKey;
  2337. BYTE *pbCipherText;
  2338. DWORD cbCipherText;
  2339. } KnownBlockCipherResult, *pKnownBlockCipherResult;
  2340. KnownBlockCipherResult g_rgKnownBlockCipherResults [] = {
  2341. { CALG_RC2, rgbRC2PlainTextKey, sizeof(rgbRC2PlainTextKey),
  2342. rgbRC2CipherText, sizeof(rgbRC2CipherText) },
  2343. { CALG_DES, rgbDESPlainTextKey, sizeof(rgbDESPlainTextKey),
  2344. rgbDESCipherText, sizeof(rgbDESCipherText) },
  2345. { CALG_3DES_112, rgb3DES112PlainTextKey, sizeof(rgb3DES112PlainTextKey),
  2346. rgb3DES112CipherText, sizeof(rgb3DES112CipherText) },
  2347. { CALG_3DES, rgb3DESPlainTextKey, sizeof(rgb3DESPlainTextKey),
  2348. rgb3DESCipherText, sizeof(rgb3DESCipherText) },
  2349. { CALG_AES_128, rgbAES128PlainTextKey, sizeof(rgbAES128PlainTextKey),
  2350. rgbAES128CipherText, sizeof(rgbAES128CipherText) },
  2351. { CALG_AES_192, rgbAES192PlainTextKey, sizeof(rgbAES192PlainTextKey),
  2352. rgbAES192CipherText, sizeof(rgbAES192CipherText) },
  2353. { CALG_AES_256, rgbAES256PlainTextKey, sizeof(rgbAES256PlainTextKey),
  2354. rgbAES256CipherText, sizeof(rgbAES256CipherText) }
  2355. };
  2356. static const unsigned g_cKnownBlockCipherResults =
  2357. sizeof(g_rgKnownBlockCipherResults) / sizeof(KnownBlockCipherResult);
  2358. //
  2359. // Function: KnownSymKeyRegression
  2360. //
  2361. DWORD KnownBlockCipherKeyRegression(PTHREAD_DATA pThreadData)
  2362. {
  2363. DWORD dwError = ERROR_SUCCESS;
  2364. BOOL fSuccess = FALSE;
  2365. BOOL fBlobError = FALSE;
  2366. HCRYPTKEY hPubKey = 0;
  2367. HCRYPTKEY hSymKey = 0;
  2368. PALGNODE pAlgNode = NULL;
  2369. PBYTE pb = NULL;
  2370. DWORD cb = 0;
  2371. DWORD cbBuf = 0;
  2372. DWORD dw = 0;
  2373. CHAR rgsz[1024];
  2374. unsigned u;
  2375. for ( pAlgNode = pThreadData->pAlgList;
  2376. pAlgNode != NULL;
  2377. pAlgNode = pAlgNode->pNext)
  2378. {
  2379. if ((ALG_CLASS_DATA_ENCRYPT != GET_ALG_CLASS(pAlgNode->EnumalgsEx.aiAlgid))
  2380. || (ALG_TYPE_BLOCK != GET_ALG_TYPE(pAlgNode->EnumalgsEx.aiAlgid)))
  2381. continue;
  2382. for ( u = 0;
  2383. u < g_cKnownBlockCipherResults &&
  2384. pAlgNode->EnumalgsEx.aiAlgid != g_rgKnownBlockCipherResults[u].ai;
  2385. u++);
  2386. // CYLINK_MEK is not supported with PLAINTEXTKEYBLOB's
  2387. if (CALG_CYLINK_MEK == pAlgNode->EnumalgsEx.aiAlgid)
  2388. continue;
  2389. sprintf(
  2390. rgsz,
  2391. "Importing 0x%x blob for CSP alg %s (0x%x)",
  2392. g_rgKnownBlockCipherResults[u].ai,
  2393. pAlgNode->EnumalgsEx.szName,
  2394. pAlgNode->EnumalgsEx.aiAlgid);
  2395. PrintBytes(
  2396. rgsz,
  2397. g_rgKnownBlockCipherResults[u].pbKey,
  2398. g_rgKnownBlockCipherResults[u].cbKey);
  2399. if (! CryptImportKey(
  2400. pThreadData->hProv,
  2401. g_rgKnownBlockCipherResults[u].pbKey,
  2402. g_rgKnownBlockCipherResults[u].cbKey,
  2403. 0, 0, &hSymKey))
  2404. {
  2405. printf("CryptImportKey ");
  2406. goto Ret;
  2407. }
  2408. if (! CryptSetKeyParam(hSymKey, KP_IV, rgbIV, 0))
  2409. {
  2410. printf("CryptSetKeyParam ");
  2411. goto Ret;
  2412. }
  2413. dw = CRYPT_MODE_CBC;
  2414. if (! CryptSetKeyParam(hSymKey, KP_MODE, (PBYTE) &dw, 0))
  2415. {
  2416. printf("CryptSetKeyParam ");
  2417. goto Ret;
  2418. }
  2419. cb = sizeof(rgbPlainText);
  2420. if (! CryptEncrypt(hSymKey, 0, TRUE, 0, NULL, &cb, 0))
  2421. {
  2422. printf("CryptEncrypt ");
  2423. goto Ret;
  2424. }
  2425. if (NULL == (pb = (PBYTE) MyAlloc(cb)))
  2426. return ERROR_NOT_ENOUGH_MEMORY;
  2427. cbBuf = cb;
  2428. cb = sizeof(rgbPlainText);
  2429. memcpy(pb, rgbPlainText, cb);
  2430. if (! CryptEncrypt(hSymKey, 0, TRUE, 0, pb, &cb, cbBuf))
  2431. {
  2432. printf("CryptEncrypt ");
  2433. goto Ret;
  2434. }
  2435. if (0 != memcmp(pb, g_rgKnownBlockCipherResults[u].pbCipherText, cb)
  2436. || 0 == cb)
  2437. {
  2438. printf(
  2439. "\nCiphertext is wrong for alg %s\n",
  2440. pAlgNode->EnumalgsEx.szName);
  2441. PrintBytes(
  2442. "Expected ciphertext",
  2443. g_rgKnownBlockCipherResults[u].pbCipherText,
  2444. cb);
  2445. PrintBytes(
  2446. "Actual ciphertext",
  2447. pb, cb);
  2448. fBlobError = TRUE;
  2449. }
  2450. // Verify decryption as well
  2451. if (! CryptDecrypt(hSymKey, 0, TRUE, 0, pb, &cb))
  2452. {
  2453. printf("CryptDecrypt ");
  2454. goto Ret;
  2455. }
  2456. if (0 != memcmp(pb, rgbPlainText, sizeof(rgbPlainText)))
  2457. {
  2458. printf(
  2459. "\nPlaintext is wrong for alg %s\n",
  2460. pAlgNode->EnumalgsEx.szName);
  2461. PrintBytes(
  2462. "Expected plaintext",
  2463. rgbPlainText,
  2464. sizeof(rgbPlainText));
  2465. PrintBytes(
  2466. "Actual plaintext",
  2467. pb, cb);
  2468. fBlobError = TRUE;
  2469. }
  2470. MyFree(pb);
  2471. pb = NULL;
  2472. }
  2473. fSuccess = TRUE;
  2474. Ret:
  2475. if (fBlobError)
  2476. dwError = -1;
  2477. if ((! fSuccess) && (! fBlobError))
  2478. printf("- error 0x%x\n", dwError = GetLastError());
  2479. return dwError;
  2480. }
  2481. #define MY_AES_BLOCKSIZE 16
  2482. #define MY_HALF_AES_BLOCKSIZE 8
  2483. //
  2484. // Function: AESMonteCarloCBCRegression
  2485. //
  2486. DWORD AESMonteCarloCBCRegression(PTHREAD_DATA pThreadData)
  2487. {
  2488. DWORD dwError = ERROR_SUCCESS;
  2489. BOOL fSuccess = FALSE;
  2490. BYTE rgbAESPlainTextKey [] = {
  2491. 0x08, 0x02, 0x00, 0x00, 0x10, 0x66, 0x00, 0x00,
  2492. 0x20, 0x00, 0x00, 0x00,
  2493. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  2494. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  2495. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  2496. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  2497. };
  2498. BLOBHEADER *pBlobHeader = NULL;
  2499. DWORD cbKey = 0, cb = 0, dwParam = 0;
  2500. BYTE rgbData [MY_AES_BLOCKSIZE * 2];
  2501. unsigned il, ol;
  2502. HCRYPTKEY hKey = 0;
  2503. BOOL fError = FALSE;
  2504. PBYTE pbPT = NULL, pbCV = NULL, pbCT = NULL;
  2505. fpos_t fpos;
  2506. BYTE rgbCBC_E_128_Result [] = {
  2507. 0x2F, 0x84, 0x4C, 0xBF, 0x78, 0xEB, 0xA7, 0x0D,
  2508. 0xA7, 0xA4, 0x96, 0x01, 0x38, 0x8F, 0x1A, 0xB6
  2509. //0x8A, 0x05, 0xFC, 0x5E, 0x09, 0x5A, 0xF4, 0x84,
  2510. //0x8A, 0x08, 0xD3, 0x28, 0xD3, 0x68, 0x8E, 0x3D
  2511. };
  2512. BYTE rgbCBC_E_192_Result [] = {
  2513. //0xBA, 0x50, 0xC9, 0x44, 0x40, 0xC0, 0x4A, 0x8C,
  2514. //0x08, 0x99, 0xD4, 0x26, 0x58, 0xE2, 0x54, 0x37
  2515. 0x7B, 0xD9, 0x66, 0xD5, 0x3A, 0xD8, 0xC1, 0xBB,
  2516. 0x85, 0xD2, 0xAD, 0xFA, 0xE8, 0x7B, 0xB1, 0x04
  2517. };
  2518. BYTE rgbCBC_E_256_Result [] = {
  2519. //0xC0, 0xFE, 0xFF, 0xF0, 0x75, 0x06, 0xA0, 0xB4,
  2520. //0xCD, 0x7B, 0x8B, 0x0C, 0xF2, 0x5D, 0x36, 0x64
  2521. 0xFE, 0x3C, 0x53, 0x65, 0x3E, 0x2F, 0x45, 0xB5,
  2522. 0x6F, 0xCD, 0x88, 0xB2, 0xCC, 0x89, 0x8F, 0xF0
  2523. };
  2524. BYTE rgbCBC_D_128_Result [] = {
  2525. //0x9B, 0x8F, 0xB7, 0x1E, 0x03, 0x5C, 0xEF, 0xF9,
  2526. //0xCB, 0xFA, 0x13, 0x46, 0xE5, 0xAC, 0xEF, 0xE0
  2527. 0xFA, 0xCA, 0x37, 0xE0, 0xB0, 0xC8, 0x53, 0x73,
  2528. 0xDF, 0x70, 0x6E, 0x73, 0xF7, 0xC9, 0xAF, 0x86
  2529. };
  2530. BYTE rgbCBC_D_192_Result [] = {
  2531. //0x63, 0x42, 0xBF, 0xDD, 0xD2, 0xF6, 0x61, 0x03,
  2532. //0x50, 0x45, 0x8B, 0x66, 0x95, 0x46, 0x34, 0x84
  2533. 0x5D, 0xF6, 0x78, 0xDD, 0x17, 0xBA, 0x4E, 0x75,
  2534. 0xB6, 0x17, 0x68, 0xC6, 0xAD, 0xEF, 0x7C, 0x7B
  2535. };
  2536. BYTE rgbCBC_D_256_Result [] = {
  2537. //0xCD, 0x64, 0x29, 0xCF, 0x3F, 0x81, 0xF8, 0xB4,
  2538. //0xF8, 0x2B, 0xC6, 0x27, 0xA8, 0x28, 0x30, 0x96
  2539. 0x48, 0x04, 0xE1, 0x81, 0x8F, 0xE6, 0x29, 0x75,
  2540. 0x19, 0xA3, 0xE8, 0x8C, 0x57, 0x31, 0x04, 0x13
  2541. };
  2542. memset(rgbData, 0, sizeof(rgbData));
  2543. pBlobHeader = (BLOBHEADER *) rgbAESPlainTextKey;
  2544. pBlobHeader->aiKeyAlg = CALG_AES_128;
  2545. cbKey = 16;
  2546. *((PDWORD) (rgbAESPlainTextKey + sizeof(BLOBHEADER))) = cbKey;
  2547. pbCV = rgbData;
  2548. pbPT = rgbData;
  2549. pbCT = rgbData;
  2550. cb = MY_AES_BLOCKSIZE;
  2551. printf("\n");
  2552. for (ol = 0; ol < 400; ol++)
  2553. {
  2554. if (hKey)
  2555. {
  2556. if (! CryptDestroyKey(hKey))
  2557. {
  2558. printf("CryptDestroyKey ");
  2559. goto Ret;
  2560. }
  2561. hKey = 0;
  2562. }
  2563. ((PUINT64) (rgbAESPlainTextKey + sizeof(BLOBHEADER) + sizeof(DWORD)))[0] ^= ((PUINT64) pbCT)[0];
  2564. ((PUINT64) (rgbAESPlainTextKey + sizeof(BLOBHEADER) + sizeof(DWORD)))[1] ^= ((PUINT64) pbCT)[1];
  2565. if (! CryptImportKey(
  2566. pThreadData->hProv, rgbAESPlainTextKey,
  2567. sizeof(BLOBHEADER) + sizeof(DWORD) + cbKey, 0, 0, &hKey))
  2568. {
  2569. printf("CryptImportKey ");
  2570. goto Ret;
  2571. }
  2572. dwParam = CRYPT_MODE_CBC;
  2573. if (! CryptSetKeyParam(hKey, KP_MODE, (PBYTE) &dwParam, 0))
  2574. {
  2575. printf("CryptSetKeyParam ");
  2576. goto Ret;
  2577. }
  2578. for (il = 0; il < 10000; il++)
  2579. {
  2580. ((PUINT64) pbPT)[0] ^= ((PUINT64) pbCV)[0];
  2581. ((PUINT64) pbPT)[1] ^= ((PUINT64) pbCV)[1];
  2582. if (! CryptEncrypt(hKey, 0, FALSE, 0, pbPT, &cb, sizeof(rgbData)))
  2583. {
  2584. printf("CryptEncrypt ");
  2585. goto Ret;
  2586. }
  2587. pbCT = pbPT;
  2588. if (0 == il && 0 == ol)
  2589. pbPT += MY_AES_BLOCKSIZE;
  2590. else
  2591. {
  2592. if (rgbData + MY_AES_BLOCKSIZE == pbPT)
  2593. pbPT = rgbData;
  2594. else
  2595. pbPT += MY_AES_BLOCKSIZE;
  2596. if (rgbData + MY_AES_BLOCKSIZE == pbCV)
  2597. pbCV = rgbData;
  2598. else
  2599. pbCV += MY_AES_BLOCKSIZE;
  2600. }
  2601. }
  2602. }
  2603. if (0 != memcmp(pbCT, rgbCBC_E_128_Result, sizeof(rgbCBC_E_128_Result)))
  2604. {
  2605. printf("\nCiphertext is wrong for CBC E 128\n");
  2606. PrintBytes(
  2607. "Expected",
  2608. rgbCBC_E_128_Result,
  2609. sizeof(rgbCBC_E_128_Result));
  2610. PrintBytes(
  2611. "Actual",
  2612. pbCT, sizeof(rgbCBC_E_128_Result));
  2613. fError = TRUE;
  2614. }
  2615. fSuccess = TRUE;
  2616. Ret:
  2617. if (! fSuccess)
  2618. dwError = GetLastError();
  2619. if (fError && 0 == dwError)
  2620. dwError = -1;
  2621. if (hKey)
  2622. CryptDestroyKey(hKey);
  2623. return dwError;
  2624. }
  2625. //
  2626. // Function: CFBCipherModeRegression
  2627. //
  2628. DWORD CFBCipherModeRegression(PTHREAD_DATA pThreadData)
  2629. {
  2630. DWORD dwError = ERROR_SUCCESS;
  2631. BOOL fSuccess = FALSE;
  2632. BOOL fBlobError = FALSE;
  2633. HCRYPTKEY hSymKey = 0;
  2634. PALGNODE pAlgNode = NULL;
  2635. DWORD dw = 0;
  2636. DWORD cb = 0;
  2637. DWORD cbBuf = 0;
  2638. PBYTE pb = NULL;
  2639. CHAR rgsz[1024];
  2640. unsigned u;
  2641. BYTE rgbCFBPlainText[] = {
  2642. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  2643. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  2644. 0x10
  2645. };
  2646. for ( pAlgNode = pThreadData->pAlgList;
  2647. pAlgNode != NULL;
  2648. pAlgNode = pAlgNode->pNext)
  2649. {
  2650. if ((ALG_CLASS_DATA_ENCRYPT != GET_ALG_CLASS(pAlgNode->EnumalgsEx.aiAlgid))
  2651. || (ALG_TYPE_BLOCK != GET_ALG_TYPE(pAlgNode->EnumalgsEx.aiAlgid)))
  2652. continue;
  2653. for ( u = 0;
  2654. u < g_cKnownBlockCipherResults &&
  2655. pAlgNode->EnumalgsEx.aiAlgid != g_rgKnownBlockCipherResults[u].ai;
  2656. u++);
  2657. // CYLINK_MEK is not supported with PLAINTEXTKEYBLOB's
  2658. if (CALG_CYLINK_MEK == pAlgNode->EnumalgsEx.aiAlgid)
  2659. continue;
  2660. sprintf(
  2661. rgsz,
  2662. "Importing 0x%x blob for CSP alg %s (0x%x)",
  2663. g_rgKnownBlockCipherResults[u].ai,
  2664. pAlgNode->EnumalgsEx.szName,
  2665. pAlgNode->EnumalgsEx.aiAlgid);
  2666. PrintBytes(
  2667. rgsz,
  2668. g_rgKnownBlockCipherResults[u].pbKey,
  2669. g_rgKnownBlockCipherResults[u].cbKey);
  2670. if (! CryptImportKey(
  2671. pThreadData->hProv,
  2672. g_rgKnownBlockCipherResults[u].pbKey,
  2673. g_rgKnownBlockCipherResults[u].cbKey,
  2674. 0, 0, &hSymKey))
  2675. {
  2676. printf("CryptImportKey ");
  2677. goto Ret;
  2678. }
  2679. if (! CryptSetKeyParam(hSymKey, KP_IV, rgbIV, 0))
  2680. {
  2681. printf("CryptSetKeyParam ");
  2682. goto Ret;
  2683. }
  2684. dw = CRYPT_MODE_CFB;
  2685. if (! CryptSetKeyParam(hSymKey, KP_MODE, (PBYTE) &dw, 0))
  2686. {
  2687. printf("CryptSetKeyParam ");
  2688. goto Ret;
  2689. }
  2690. cb = sizeof(rgbPlainText);
  2691. if (! CryptEncrypt(hSymKey, 0, TRUE, 0, NULL, &cb, 0))
  2692. {
  2693. printf("CryptEncrypt ");
  2694. goto Ret;
  2695. }
  2696. if (NULL == (pb = (PBYTE) MyAlloc(cb)))
  2697. return ERROR_NOT_ENOUGH_MEMORY;
  2698. cbBuf = cb;
  2699. cb = sizeof(rgbCFBPlainText);
  2700. memcpy(pb, rgbCFBPlainText, cb);
  2701. if (! CryptEncrypt(hSymKey, 0, TRUE, 0, pb, &cb, cbBuf))
  2702. {
  2703. printf("CryptEncrypt ");
  2704. goto Ret;
  2705. }
  2706. if (0 == memcmp(pb, rgbCFBPlainText, sizeof(rgbCFBPlainText)))
  2707. {
  2708. printf("Ciphertext matches plaintext ");
  2709. goto Ret;
  2710. }
  2711. // Verify decryption
  2712. if (! CryptDecrypt(hSymKey, 0, TRUE, 0, pb, &cb))
  2713. {
  2714. printf("CryptDecrypt ");
  2715. goto Ret;
  2716. }
  2717. if (0 != memcmp(pb, rgbCFBPlainText, sizeof(rgbCFBPlainText)))
  2718. {
  2719. printf(
  2720. "Plaintext is wrong for alg %s\n",
  2721. pAlgNode->EnumalgsEx.szName);
  2722. PrintBytes(
  2723. "Expected plaintext",
  2724. rgbCFBPlainText,
  2725. sizeof(rgbCFBPlainText));
  2726. PrintBytes(
  2727. "Actual plaintext",
  2728. pb, cb);
  2729. fBlobError = TRUE;
  2730. }
  2731. MyFree(pb);
  2732. pb = NULL;
  2733. }
  2734. fSuccess = TRUE;
  2735. Ret:
  2736. if (fBlobError)
  2737. dwError = -1;
  2738. if ((! fSuccess) && (! fBlobError))
  2739. printf("- error 0x%x\n", dwError = GetLastError());
  2740. return dwError;
  2741. }
  2742. typedef struct _HMAC_TEST
  2743. {
  2744. PBYTE pbKey;
  2745. DWORD cbKey;
  2746. PBYTE pbData;
  2747. DWORD cbData;
  2748. PBYTE pbData2;
  2749. DWORD cbData2;
  2750. PBYTE pbHmac;
  2751. DWORD cbHmac;
  2752. ALG_ID aiHash;
  2753. } HMAC_TEST, *PHMAC_TEST;
  2754. //
  2755. // Function: DoHmacTestCase
  2756. //
  2757. DWORD DoHmacTestCase(
  2758. IN PTHREAD_DATA pThreadData,
  2759. IN PHMAC_TEST pHmac)
  2760. {
  2761. HCRYPTKEY hKey = 0;
  2762. HCRYPTHASH hHash = 0;
  2763. DWORD cb = 0;
  2764. BLOBHEADER *pHeader = NULL;
  2765. BOOL fSuccess = FALSE;
  2766. DWORD dwError = ERROR_SUCCESS;
  2767. BYTE rgBuf[1024];
  2768. HMAC_INFO HmacInfo;
  2769. ZeroMemory(rgBuf, sizeof(rgBuf));
  2770. ZeroMemory(&HmacInfo, sizeof(HmacInfo));
  2771. pHeader = (BLOBHEADER *) rgBuf;
  2772. pHeader->bType = PLAINTEXTKEYBLOB;
  2773. pHeader->bVersion = CUR_BLOB_VERSION;
  2774. pHeader->aiKeyAlg = CALG_RC2;
  2775. *(DWORD*)(rgBuf + sizeof(BLOBHEADER)) = pHmac->cbKey;
  2776. memcpy(
  2777. rgBuf + sizeof(BLOBHEADER) + sizeof(DWORD),
  2778. pHmac->pbKey, pHmac->cbKey);
  2779. if (! CryptImportKey(
  2780. pThreadData->hProv, rgBuf,
  2781. sizeof(BLOBHEADER) + sizeof(DWORD) + pHmac->cbKey,
  2782. 0, CRYPT_IPSEC_HMAC_KEY, &hKey))
  2783. {
  2784. printf("CryptImportKey");
  2785. goto Ret;
  2786. }
  2787. if (! CryptCreateHash(
  2788. pThreadData->hProv, CALG_HMAC, hKey, 0, &hHash))
  2789. {
  2790. printf("CryptCreateHash");
  2791. goto Ret;
  2792. }
  2793. HmacInfo.HashAlgid = pHmac->aiHash;
  2794. if (! CryptSetHashParam(
  2795. hHash, HP_HMAC_INFO, (PBYTE) &HmacInfo, 0))
  2796. {
  2797. printf("CryptSetHashParam");
  2798. goto Ret;
  2799. }
  2800. if (! CryptHashData(
  2801. hHash, pHmac->pbData, pHmac->cbData, 0))
  2802. {
  2803. printf("CryptHashData");
  2804. goto Ret;
  2805. }
  2806. if (pHmac->cbData2)
  2807. {
  2808. if (! CryptHashData(
  2809. hHash, pHmac->pbData2, pHmac->cbData2, 0))
  2810. {
  2811. printf("CryptHashData 2");
  2812. goto Ret;
  2813. }
  2814. }
  2815. cb = sizeof(rgBuf);
  2816. ZeroMemory(rgBuf, sizeof(rgBuf));
  2817. if (! CryptGetHashParam(
  2818. hHash, HP_HASHVAL, rgBuf, &cb, 0))
  2819. {
  2820. printf("CryptGetHashParam");
  2821. goto Ret;
  2822. }
  2823. PrintBytes("Expected Hmac", pHmac->pbHmac, pHmac->cbHmac);
  2824. PrintBytes("Actual Hmac", rgBuf, cb);
  2825. if ( 0 != memcmp(rgBuf, pHmac->pbHmac, cb)
  2826. || cb != pHmac->cbHmac)
  2827. goto Ret;
  2828. if (! CryptDestroyKey(hKey))
  2829. {
  2830. printf("CryptDestroyKey");
  2831. goto Ret;
  2832. }
  2833. hKey = 0;
  2834. if (! CryptDestroyHash(hHash))
  2835. {
  2836. printf("CryptDestroyHash");
  2837. goto Ret;
  2838. }
  2839. hHash = 0;
  2840. fSuccess = TRUE;
  2841. Ret:
  2842. if (! fSuccess)
  2843. {
  2844. dwError = GetLastError();
  2845. printf(" error - 0x%x\n", dwError);
  2846. if (0 == dwError)
  2847. dwError = -1;
  2848. }
  2849. if (hKey)
  2850. CryptDestroyKey(hKey);
  2851. if (hHash)
  2852. CryptDestroyHash(hHash);
  2853. return dwError;
  2854. }
  2855. //
  2856. // Function: HmacRegression
  2857. //
  2858. DWORD HmacRegression(PTHREAD_DATA pThreadData)
  2859. {
  2860. BOOL fSuccess = FALSE;
  2861. DWORD dwError = ERROR_SUCCESS;
  2862. HMAC_TEST Hmac;
  2863. // SHA Test case 1
  2864. BYTE rgKey1 [] = {
  2865. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  2866. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  2867. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  2868. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b
  2869. };
  2870. LPSTR pszData1 = "Hi There";
  2871. BYTE rgHmac1 [] = {
  2872. 0xb6, 0x17, 0x31, 0x86, 0x55,
  2873. 0x05, 0x72, 0x64, 0xe2, 0x8b,
  2874. 0xc0, 0xb6, 0xfb, 0x37, 0x8c,
  2875. 0x8e, 0xf1, 0x46, 0xbe, 0x00
  2876. };
  2877. // SHA Test case 2
  2878. BYTE rgKey2 [] = {
  2879. 0x01, 0x02, 0x03, 0x04, 0x05,
  2880. 0x06, 0x07, 0x08, 0x09, 0x0a,
  2881. 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  2882. 0x10, 0x11, 0x12, 0x13, 0x14,
  2883. 0x15, 0x16, 0x17, 0x18, 0x19
  2884. };
  2885. BYTE rgData2 [50];
  2886. BYTE rgHmac2 [] = {
  2887. 0x4c, 0x90, 0x07, 0xf4, 0x02,
  2888. 0x62, 0x50, 0xc6, 0xbc, 0x84,
  2889. 0x14, 0xf9, 0xbf, 0x50, 0xc8,
  2890. 0x6c, 0x2d, 0x72, 0x35, 0xda
  2891. };
  2892. // SHA Test case 3
  2893. BYTE rgKey3 [80];
  2894. LPSTR pszData3 = "Test Using Larger Than Block-Size Key - Hash Key First";
  2895. BYTE rgHmac3 [] = {
  2896. 0xaa, 0x4a, 0xe5, 0xe1, 0x52,
  2897. 0x72, 0xd0, 0x0e, 0x95, 0x70,
  2898. 0x56, 0x37, 0xce, 0x8a, 0x3b,
  2899. 0x55, 0xed, 0x40, 0x21, 0x12
  2900. };
  2901. // MD5 Test case 1
  2902. // use rgKey1 (16 bytes only) and pszData1
  2903. BYTE rgHmacMD1 [] = {
  2904. 0x92, 0x94, 0x72, 0x7a,
  2905. 0x36, 0x38, 0xbb, 0x1c,
  2906. 0x13, 0xf4, 0x8e, 0xf8,
  2907. 0x15, 0x8b, 0xfc, 0x9d
  2908. };
  2909. // MD5 Test case 2
  2910. // use rgKey3 (full length) and pszData3
  2911. BYTE rgHmacMD2 [] = {
  2912. 0x6b, 0x1a, 0xb7, 0xfe,
  2913. 0x4b, 0xd7, 0xbf, 0x8f,
  2914. 0x0b, 0x62, 0xe6, 0xce,
  2915. 0x61, 0xb9, 0xd0, 0xcd
  2916. };
  2917. // IPSec MD5 vectors
  2918. BYTE rgKeyIpsec [] = {
  2919. 0x66, 0x6f, 0x6f
  2920. };
  2921. BYTE rgDataIpsecA [] = {
  2922. 0x38, 0x8e, 0x5e, 0x8a,
  2923. 0xb0, 0x79, 0x16, 0x47,
  2924. 0x29, 0x1b, 0xf0, 0x02,
  2925. 0x78, 0x5e, 0x38, 0xe5,
  2926. 0x82, 0x3c, 0x17, 0x0d
  2927. };
  2928. BYTE rgDataIpsecB [] = {
  2929. 0x34, 0xdd, 0xdb, 0x22,
  2930. 0x9e, 0x21, 0x75, 0x28,
  2931. 0x5e, 0x4d, 0x7d, 0xdf,
  2932. 0xea, 0x35, 0xc5, 0xfc,
  2933. 0x44, 0xdb, 0x62, 0xad
  2934. };
  2935. BYTE rgHmacIpsec [] = {
  2936. 0x52, 0x07, 0x38, 0x15,
  2937. 0xef, 0xb0, 0x68, 0x43,
  2938. 0x89, 0x8e, 0x0b, 0xdc,
  2939. 0x58, 0xc0, 0x70, 0xc0
  2940. };
  2941. memset(rgData2, 0xcd, sizeof(rgData2));
  2942. memset(rgKey3, 0xaa, sizeof(rgKey3));
  2943. // SHA 1
  2944. ZeroMemory(&Hmac, sizeof(HMAC_TEST));
  2945. Hmac.cbData = strlen(pszData1) * sizeof(CHAR);
  2946. Hmac.pbData = (PBYTE) pszData1;
  2947. Hmac.cbHmac = sizeof(rgHmac1);
  2948. Hmac.pbHmac = rgHmac1;
  2949. Hmac.cbKey = sizeof(rgKey1);
  2950. Hmac.pbKey = rgKey1;
  2951. Hmac.aiHash = CALG_SHA1;
  2952. if (ERROR_SUCCESS != (dwError = DoHmacTestCase(pThreadData, &Hmac)))
  2953. {
  2954. printf("ERROR: DoHmacTestCase SHA 1\n");
  2955. return dwError;
  2956. }
  2957. // SHA 2
  2958. ZeroMemory(&Hmac, sizeof(HMAC_TEST));
  2959. Hmac.cbData = sizeof(rgData2);
  2960. Hmac.pbData = rgData2;
  2961. Hmac.cbHmac = sizeof(rgHmac2);
  2962. Hmac.pbHmac = rgHmac2;
  2963. Hmac.cbKey = sizeof(rgKey2);
  2964. Hmac.pbKey = rgKey2;
  2965. Hmac.aiHash = CALG_SHA1;
  2966. if (ERROR_SUCCESS != (dwError = DoHmacTestCase(pThreadData, &Hmac)))
  2967. {
  2968. printf("ERROR: DoHmacTestCase SHA 2\n");
  2969. return dwError;
  2970. }
  2971. // SHA 3
  2972. ZeroMemory(&Hmac, sizeof(HMAC_TEST));
  2973. Hmac.cbData = strlen(pszData3) * sizeof(CHAR);
  2974. Hmac.pbData = (PBYTE) pszData3;
  2975. Hmac.cbHmac = sizeof(rgHmac3);
  2976. Hmac.pbHmac = rgHmac3;
  2977. Hmac.cbKey = sizeof(rgKey3);
  2978. Hmac.pbKey = rgKey3;
  2979. Hmac.aiHash = CALG_SHA1;
  2980. if (ERROR_SUCCESS != (dwError = DoHmacTestCase(pThreadData, &Hmac)))
  2981. {
  2982. printf("ERROR: DoHmacTestCase SHA 3\n");
  2983. return dwError;
  2984. }
  2985. // MD5 1
  2986. ZeroMemory(&Hmac, sizeof(HMAC_TEST));
  2987. Hmac.cbData = strlen(pszData1) * sizeof(CHAR);
  2988. Hmac.pbData = (PBYTE) pszData1;
  2989. Hmac.cbHmac = sizeof(rgHmacMD1);
  2990. Hmac.pbHmac = rgHmacMD1;
  2991. Hmac.cbKey = 16; // only 16-byte key for this one
  2992. Hmac.pbKey = rgKey1;
  2993. Hmac.aiHash = CALG_MD5;
  2994. if (ERROR_SUCCESS != (dwError = DoHmacTestCase(pThreadData, &Hmac)))
  2995. {
  2996. printf("ERROR: DoHmacTestCase MD5 1\n");
  2997. return dwError;
  2998. }
  2999. // MD5 2
  3000. ZeroMemory(&Hmac, sizeof(HMAC_TEST));
  3001. Hmac.cbData = strlen(pszData3) * sizeof(CHAR);
  3002. Hmac.pbData = (PBYTE) pszData3;
  3003. Hmac.cbHmac = sizeof(rgHmacMD2);
  3004. Hmac.pbHmac = rgHmacMD2;
  3005. Hmac.cbKey = sizeof(rgKey3);
  3006. Hmac.pbKey = rgKey3;
  3007. Hmac.aiHash = CALG_MD5;
  3008. if (ERROR_SUCCESS != (dwError = DoHmacTestCase(pThreadData, &Hmac)))
  3009. {
  3010. printf("ERROR: DoHmacTestCase MD5 2\n");
  3011. return dwError;
  3012. }
  3013. // MD5 Ipsec vector
  3014. ZeroMemory(&Hmac, sizeof(HMAC_TEST));
  3015. Hmac.cbData = sizeof(rgDataIpsecA);
  3016. Hmac.pbData = rgDataIpsecA;
  3017. Hmac.cbData2 = sizeof(rgDataIpsecB);
  3018. Hmac.pbData2 = rgDataIpsecB;
  3019. Hmac.cbHmac = sizeof(rgHmacIpsec);
  3020. Hmac.pbHmac = rgHmacIpsec;
  3021. Hmac.cbKey = sizeof(rgKeyIpsec);
  3022. Hmac.pbKey = rgKeyIpsec;
  3023. Hmac.aiHash = CALG_MD5;
  3024. if (ERROR_SUCCESS != (dwError = DoHmacTestCase(pThreadData, &Hmac)))
  3025. {
  3026. printf("ERROR: DoHmacTestCase MD5 Ipsec\n");
  3027. return dwError;
  3028. }
  3029. return ERROR_SUCCESS;
  3030. }
  3031. //
  3032. // Function: KeyArchiveRegression
  3033. //
  3034. // Not thread safe
  3035. //
  3036. DWORD KeyArchiveRegression(PTHREAD_DATA pThreadData)
  3037. {
  3038. HCRYPTPROV hProv = 0;
  3039. HCRYPTKEY hKey = 0;
  3040. DWORD dwError = 0;
  3041. LPSTR pszContainer = "KeyArchiveRegression";
  3042. BYTE rgbKey[2048];
  3043. DWORD cbKey;
  3044. DWORD dwData;
  3045. DWORD cbData;
  3046. BOOL fSuccess = FALSE;
  3047. CryptAcquireContext(
  3048. &hProv,
  3049. pszContainer,
  3050. pThreadData->rgszProvName,
  3051. pThreadData->dwProvType,
  3052. CRYPT_DELETEKEYSET);
  3053. if (! CryptAcquireContext(
  3054. &hProv,
  3055. pszContainer,
  3056. pThreadData->rgszProvName,
  3057. pThreadData->dwProvType,
  3058. CRYPT_NEWKEYSET))
  3059. {
  3060. printf("CryptAcquireContext newkeyset ");
  3061. goto Ret;
  3062. }
  3063. if (! CryptGenKey(
  3064. hProv,
  3065. AT_SIGNATURE,
  3066. CRYPT_ARCHIVABLE,
  3067. &hKey))
  3068. {
  3069. printf("CryptGenKey archivable ");
  3070. goto Ret;
  3071. }
  3072. cbData = sizeof(dwData);
  3073. if (! CryptGetKeyParam(
  3074. hKey, KP_PERMISSIONS, (PBYTE) &dwData, &cbData, 0))
  3075. {
  3076. printf("CryptGetKeyParam ");
  3077. goto Ret;
  3078. }
  3079. if (! ((CRYPT_ARCHIVE & dwData) && (! (CRYPT_EXPORT & dwData))))
  3080. {
  3081. printf("incorrect KP_PERMISSIONS ");
  3082. goto Ret;
  3083. }
  3084. cbKey = sizeof(rgbKey);
  3085. if (! CryptExportKey(
  3086. hKey,
  3087. 0,
  3088. PRIVATEKEYBLOB,
  3089. 0,
  3090. rgbKey,
  3091. &cbKey))
  3092. {
  3093. printf("CryptExportKey privatekeyblob ");
  3094. goto Ret;
  3095. }
  3096. if (! CryptDestroyKey(hKey))
  3097. {
  3098. printf("CryptDestroyKey ");
  3099. goto Ret;
  3100. }
  3101. if (! CryptReleaseContext(hProv, 0))
  3102. {
  3103. printf("CryptReleaseContext ");
  3104. goto Ret;
  3105. }
  3106. if (! CryptAcquireContext(
  3107. &hProv,
  3108. pszContainer,
  3109. pThreadData->rgszProvName,
  3110. pThreadData->dwProvType,
  3111. 0))
  3112. {
  3113. printf("CryptAcquireContext ");
  3114. goto Ret;
  3115. }
  3116. if (! CryptGetUserKey(hProv, AT_SIGNATURE, &hKey))
  3117. {
  3118. printf("CryptGetUserKey ");
  3119. goto Ret;
  3120. }
  3121. // try to set the key export/archive perms; should fail
  3122. cbData = sizeof(dwData);
  3123. if (! CryptGetKeyParam(
  3124. hKey, KP_PERMISSIONS, (PBYTE) &dwData, &cbData, 0))
  3125. {
  3126. printf("CryptGetKeyParam ");
  3127. goto Ret;
  3128. }
  3129. dwData |= CRYPT_EXPORT | CRYPT_ARCHIVE;
  3130. // should fail
  3131. if (CryptSetKeyParam(hKey, KP_PERMISSIONS, (PBYTE) &dwData, 0))
  3132. {
  3133. printf("CryptSetKeyParam should have failed ");
  3134. goto Ret;
  3135. }
  3136. // should fail
  3137. cbKey = sizeof(rgbKey);
  3138. if (CryptExportKey(
  3139. hKey,
  3140. 0,
  3141. PRIVATEKEYBLOB,
  3142. 0,
  3143. rgbKey,
  3144. &cbKey))
  3145. {
  3146. printf("CryptExportKey should have failed ");
  3147. goto Ret;
  3148. }
  3149. fSuccess = TRUE;
  3150. Ret:
  3151. if (! fSuccess)
  3152. printf("- error 0x%x\n", dwError = GetLastError());
  3153. if (hKey)
  3154. CryptDestroyKey(hKey);
  3155. if (hProv)
  3156. CryptReleaseContext(hProv, 0);
  3157. return dwError;
  3158. }
  3159. //
  3160. // Function: PlaintextBlobRegression
  3161. //
  3162. DWORD PlaintextBlobRegression(PTHREAD_DATA pThreadData)
  3163. {
  3164. HCRYPTKEY hKey = 0;
  3165. DWORD cbKey = 0;
  3166. PBYTE pbKey = NULL;
  3167. PALGNODE pAlgNode = NULL;
  3168. BLOBHEADER *header = NULL;
  3169. PBYTE pbTemp = NULL;
  3170. DWORD cbData, cb;
  3171. BYTE rgbData[1024];
  3172. BOOL fSuccess = FALSE;
  3173. DWORD dwError = ERROR_SUCCESS;
  3174. DWORD cbKeySize = 0;
  3175. // try an invalid key type
  3176. if (CryptExportKey(
  3177. pThreadData->hSignatureKey,
  3178. 0,
  3179. PLAINTEXTKEYBLOB,
  3180. 0,
  3181. NULL,
  3182. &cbKey))
  3183. {
  3184. printf("CryptExportKey plaintextkeyblob should have failed ");
  3185. goto Ret;
  3186. }
  3187. // try all the valid key types
  3188. for (pAlgNode = pThreadData->pAlgList; pAlgNode != NULL; pAlgNode = pAlgNode->pNext)
  3189. {
  3190. if (ALG_CLASS_DATA_ENCRYPT != GET_ALG_CLASS(pAlgNode->EnumalgsEx.aiAlgid)
  3191. || (ALG_TYPE_BLOCK != GET_ALG_TYPE(pAlgNode->EnumalgsEx.aiAlgid)
  3192. && ALG_TYPE_STREAM != GET_ALG_TYPE(pAlgNode->EnumalgsEx.aiAlgid)))
  3193. continue;
  3194. // Plaintext import not supported for CYLINK_MEK
  3195. if (CALG_CYLINK_MEK == pAlgNode->EnumalgsEx.aiAlgid)
  3196. continue;
  3197. /*
  3198. if (PROV_DSS == pThreadData->dwProvType
  3199. || PROV_DSS_DH == pThreadData->dwProvType)
  3200. {
  3201. cbKeySize = pAlgNode->EnumalgsEx.dwMaxLen / 8;
  3202. }
  3203. else
  3204. {
  3205. */
  3206. switch (pAlgNode->EnumalgsEx.aiAlgid)
  3207. {
  3208. case CALG_DES:
  3209. cbKeySize = pAlgNode->EnumalgsEx.dwMaxLen / 8 + 1;
  3210. break;
  3211. case CALG_3DES_112:
  3212. cbKeySize = pAlgNode->EnumalgsEx.dwMaxLen / 8 + 2;
  3213. break;
  3214. case CALG_3DES:
  3215. cbKeySize = pAlgNode->EnumalgsEx.dwMaxLen / 8 + 3;
  3216. break;
  3217. default:
  3218. cbKeySize = pAlgNode->EnumalgsEx.dwMaxLen / 8;
  3219. }
  3220. /*
  3221. }
  3222. */
  3223. printf(
  3224. "Importing Alg: %xh (%s), Size: %d bits\n",
  3225. pAlgNode->EnumalgsEx.aiAlgid,
  3226. pAlgNode->EnumalgsEx.szName,
  3227. cbKeySize * 8);
  3228. cbKey = sizeof(BLOBHEADER) + sizeof(DWORD) + cbKeySize;
  3229. if (NULL == (pbKey = (PBYTE) MyAlloc(cbKey)))
  3230. return ERROR_NOT_ENOUGH_MEMORY;
  3231. header = (BLOBHEADER *) pbKey;
  3232. header->aiKeyAlg = pAlgNode->EnumalgsEx.aiAlgid;
  3233. header->bType = PLAINTEXTKEYBLOB;
  3234. header->bVersion = CUR_BLOB_VERSION;
  3235. header->reserved = 0;
  3236. pbTemp = pbKey + sizeof(BLOBHEADER);
  3237. *((DWORD*)pbTemp) = cbKeySize;
  3238. pbTemp += sizeof(DWORD);
  3239. // create some key data
  3240. if (! CryptGenRandom(
  3241. pThreadData->hProv,
  3242. cbKeySize,
  3243. pbTemp))
  3244. {
  3245. printf("CryptGenRandom ");
  3246. goto Ret;
  3247. }
  3248. if (! CryptImportKey(
  3249. pThreadData->hProv,
  3250. pbKey, cbKey, 0, CRYPT_EXPORTABLE, &hKey))
  3251. {
  3252. printf("CryptImportKey ");
  3253. goto Ret;
  3254. }
  3255. MyFree(pbKey);
  3256. // create some data to encrypt
  3257. if (! CryptGenRandom(
  3258. pThreadData->hProv,
  3259. sizeof(rgbData),
  3260. rgbData))
  3261. {
  3262. printf("CryptGenRandom ");
  3263. goto Ret;
  3264. }
  3265. cbData = sizeof(rgbData);
  3266. cb = cbData / 2;
  3267. if (! CryptEncrypt(
  3268. hKey, 0, TRUE, 0, rgbData, &cb, cbData))
  3269. {
  3270. printf("CryptEncrypt ");
  3271. goto Ret;
  3272. }
  3273. if (! CryptExportKey(
  3274. hKey, 0, PLAINTEXTKEYBLOB, 0, NULL, &cbKey))
  3275. {
  3276. printf("CryptExportKey size ");
  3277. goto Ret;
  3278. }
  3279. if (NULL == (pbKey = (PBYTE) MyAlloc(cbKey)))
  3280. return ERROR_NOT_ENOUGH_MEMORY;
  3281. if (! CryptExportKey(
  3282. hKey, 0, PLAINTEXTKEYBLOB, 0, pbKey, &cbKey))
  3283. {
  3284. printf("CryptExportKey ");
  3285. goto Ret;
  3286. }
  3287. // check the blob
  3288. header = (BLOBHEADER *) pbKey;
  3289. if (pAlgNode->EnumalgsEx.aiAlgid != header->aiKeyAlg)
  3290. {
  3291. printf("header->aiKeyAlg is wrong ");
  3292. goto Ret;
  3293. }
  3294. if (CUR_BLOB_VERSION != header->bVersion)
  3295. {
  3296. printf("header->bVersion is wrong ");
  3297. goto Ret;
  3298. }
  3299. if (0 != header->reserved)
  3300. {
  3301. printf("header->reserved is wrong ");
  3302. goto Ret;
  3303. }
  3304. if (PLAINTEXTKEYBLOB != header->bType)
  3305. {
  3306. printf("header->bType is wrong ");
  3307. goto Ret;
  3308. }
  3309. pbTemp = pbKey + sizeof(BLOBHEADER);
  3310. if (cbKeySize != *((DWORD*)pbTemp))
  3311. {
  3312. printf(
  3313. "blob key size is %d, but should be %d ",
  3314. *((DWORD*)pbTemp),
  3315. cbKeySize);
  3316. goto Ret;
  3317. }
  3318. MyFree(pbKey);
  3319. if (! CryptDestroyKey(hKey))
  3320. {
  3321. printf("CryptDestroyKey ");
  3322. hKey = 0;
  3323. goto Ret;
  3324. }
  3325. hKey = 0;
  3326. }
  3327. fSuccess = TRUE;
  3328. Ret:
  3329. if (! fSuccess)
  3330. printf("- error 0x%x\n", dwError = GetLastError());
  3331. if (hKey)
  3332. CryptDestroyKey(hKey);
  3333. return dwError;
  3334. }
  3335. //
  3336. // Function: LoadAesCspRegression
  3337. //
  3338. DWORD LoadAesCspRegression(PTHREAD_DATA pThreadData)
  3339. {
  3340. DWORD dwError = 0;
  3341. BOOL fSuccess = FALSE;
  3342. HMODULE hMod = NULL;
  3343. if (NULL == (hMod = LoadLibraryEx(RSA_AES_CSP, NULL, 0)))
  3344. {
  3345. printf("LoadLibraryEx %s ", RSA_AES_CSP);
  3346. goto Ret;
  3347. }
  3348. if (! FreeLibrary(hMod))
  3349. {
  3350. printf("FreeLibrary %s ", RSA_AES_CSP);
  3351. goto Ret;
  3352. }
  3353. fSuccess = TRUE;
  3354. Ret:
  3355. if (! fSuccess)
  3356. printf("- error 0x%x\n", dwError = GetLastError());
  3357. return dwError;
  3358. }
  3359. DWORD VerifyPinCallback(PPINCACHE_PINS pPins, PVOID pvData)
  3360. {
  3361. DWORD dwReturn = *(DWORD*)pvData;
  3362. PrintBytes(
  3363. "VerifyPinCallback current pin",
  3364. pPins->pbCurrentPin, pPins->cbCurrentPin);
  3365. PrintBytes(
  3366. "VerifyPinCallback new pin",
  3367. pPins->pbNewPin, pPins->cbNewPin);
  3368. return dwReturn;
  3369. }
  3370. static USHORT l_uTestLogonID;
  3371. void SetLogonID(USHORT uLogon)
  3372. {
  3373. l_uTestLogonID = uLogon;
  3374. }
  3375. void GetLogonID(LUID *pLuid)
  3376. {
  3377. memset(pLuid, l_uTestLogonID, sizeof(LUID));
  3378. }
  3379. BOOL MyGetTokenInformation(
  3380. HANDLE TokenHandle,
  3381. TOKEN_INFORMATION_CLASS TokenInformationClass,
  3382. LPVOID TokenInformation,
  3383. DWORD TokenInformationLength,
  3384. PDWORD ReturnLength)
  3385. {
  3386. NTSTATUS status = NtQueryInformationToken(
  3387. TokenHandle, TokenInformationClass,
  3388. TokenInformation, TokenInformationLength,
  3389. ReturnLength);
  3390. if (TokenStatistics == TokenInformationClass)
  3391. {
  3392. printf("MyGetTokenInformation: intercepted TokenStatistics call\n");
  3393. GetLogonID(&((TOKEN_STATISTICS*) TokenInformation)->AuthenticationId);
  3394. }
  3395. return (S_OK == status);
  3396. }
  3397. //
  3398. // Function: PinCacheRegression
  3399. //
  3400. DWORD PinCacheRegression(PTHREAD_DATA pThreadData)
  3401. {
  3402. DWORD dwError = 0;
  3403. BOOL fSuccess = FALSE;
  3404. PINCACHE_HANDLE hCache = NULL;
  3405. BYTE rgPin[] = { 1, 2, 3, 4 };
  3406. DWORD cbPin = sizeof(rgPin);
  3407. BYTE rgPin2[] = { 5, 6, 7, 8, 9 };
  3408. BYTE *pbPin = NULL;
  3409. PFN_VERIFYPIN_CALLBACK pfnVerifyPin = VerifyPinCallback;
  3410. DWORD dwCallbackReturn;
  3411. PINCACHE_PINS Pins;
  3412. int i;
  3413. ZeroMemory(&Pins, sizeof(PINCACHE_PINS));
  3414. Pins.cbCurrentPin = sizeof(rgPin);
  3415. Pins.pbCurrentPin = rgPin;
  3416. dwCallbackReturn = 0x7070;
  3417. if (0x7070 != (dwError =
  3418. PinCacheAdd(&hCache, &Pins,
  3419. pfnVerifyPin, (PVOID) &dwCallbackReturn)))
  3420. {
  3421. printf("PinCacheAdd with callback fail ");
  3422. goto Ret;
  3423. }
  3424. //
  3425. // (0)
  3426. // Cache uninitialized
  3427. //
  3428. SetLogonID(1);
  3429. dwCallbackReturn = ERROR_SUCCESS;
  3430. if (ERROR_SUCCESS != (dwError =
  3431. PinCacheAdd(&hCache, &Pins,
  3432. pfnVerifyPin, (PVOID) &dwCallbackReturn)))
  3433. {
  3434. printf("PinCacheAdd ");
  3435. goto Ret;
  3436. }
  3437. if (ERROR_SUCCESS != (dwError = PinCacheQuery(hCache, NULL, &cbPin)))
  3438. {
  3439. printf("PinCacheQuery NULL for size ");
  3440. goto Ret;
  3441. }
  3442. if (NULL == (pbPin = (PBYTE) MyAlloc(cbPin)))
  3443. return ERROR_NOT_ENOUGH_MEMORY;
  3444. cbPin--;
  3445. if (ERROR_SUCCESS == (dwError = PinCacheQuery(hCache, pbPin, &cbPin)))
  3446. {
  3447. printf("PinCacheQuery insufficient size succeeded ");
  3448. goto Ret;
  3449. }
  3450. if (ERROR_SUCCESS != (dwError = PinCacheQuery(hCache, pbPin, &cbPin)))
  3451. {
  3452. printf("PinCacheQuery ");
  3453. goto Ret;
  3454. }
  3455. if (sizeof(rgPin) != RtlCompareMemory(pbPin, rgPin, cbPin))
  3456. {
  3457. PrintBytes("Actual cache", pbPin, cbPin);
  3458. PrintBytes("Expected cache", rgPin, sizeof(rgPin));
  3459. printf("Cache is incorrect ");
  3460. goto Ret;
  3461. }
  3462. PinCacheFlush(&hCache);
  3463. if (NULL != hCache)
  3464. {
  3465. printf("PinCacheFlush should set hCache=NULL ");
  3466. goto Ret;
  3467. }
  3468. // Re-initialize to continue tests
  3469. if (ERROR_SUCCESS != (dwError =
  3470. PinCacheAdd(&hCache, &Pins,
  3471. pfnVerifyPin, (PVOID) &dwCallbackReturn)))
  3472. {
  3473. printf("PinCacheAdd ");
  3474. goto Ret;
  3475. }
  3476. //
  3477. // (1)
  3478. // Same LogonID, same Pin
  3479. //
  3480. if (ERROR_SUCCESS != (dwError =
  3481. PinCacheAdd(&hCache, &Pins,
  3482. pfnVerifyPin, (PVOID) &dwCallbackReturn)))
  3483. {
  3484. printf("PinCacheAdd 1 ");
  3485. goto Ret;
  3486. }
  3487. cbPin = sizeof(rgPin);
  3488. if (ERROR_SUCCESS != (dwError = PinCacheQuery(hCache, pbPin, &cbPin)))
  3489. {
  3490. printf("PinCacheQuery 1 ");
  3491. goto Ret;
  3492. }
  3493. if (sizeof(rgPin) != RtlCompareMemory(pbPin, rgPin, cbPin))
  3494. {
  3495. PrintBytes("Actual cache", pbPin, cbPin);
  3496. PrintBytes("Expected cache", rgPin, sizeof(rgPin));
  3497. printf("Cache is incorrect 1 ");
  3498. goto Ret;
  3499. }
  3500. // Try a pin change
  3501. Pins.cbNewPin = sizeof(rgPin2);
  3502. Pins.pbNewPin = rgPin2;
  3503. if (ERROR_SUCCESS != (dwError =
  3504. PinCacheAdd(&hCache, &Pins,
  3505. pfnVerifyPin, (PVOID) &dwCallbackReturn)))
  3506. {
  3507. printf("PinCacheAdd 1 change ");
  3508. goto Ret;
  3509. }
  3510. if (ERROR_SUCCESS != (dwError = PinCacheQuery(hCache, NULL, &cbPin)))
  3511. {
  3512. printf("PinCacheQuery 1 change query ");
  3513. goto Ret;
  3514. }
  3515. MyFree(pbPin);
  3516. pbPin = NULL;
  3517. if (NULL == (pbPin = (PBYTE) MyAlloc(cbPin)))
  3518. return ERROR_NOT_ENOUGH_MEMORY;
  3519. if (ERROR_SUCCESS != (dwError = PinCacheQuery(hCache, pbPin, &cbPin)))
  3520. {
  3521. printf("PinCacheQuery 1 change ");
  3522. goto Ret;
  3523. }
  3524. if (sizeof(rgPin2) != RtlCompareMemory(pbPin, rgPin2, cbPin))
  3525. {
  3526. PrintBytes("Actual cache", pbPin, cbPin);
  3527. PrintBytes("Expected cache", rgPin, sizeof(rgPin));
  3528. printf("Cache is incorrect 1 change ");
  3529. goto Ret;
  3530. }
  3531. // Try a failed pin change
  3532. Pins.cbCurrentPin = sizeof(rgPin2);
  3533. Pins.pbCurrentPin = rgPin2;
  3534. Pins.cbNewPin = sizeof(rgPin);
  3535. Pins.pbNewPin = rgPin;
  3536. dwCallbackReturn = -1;
  3537. if (-1 != (dwError =
  3538. PinCacheAdd(&hCache, &Pins,
  3539. pfnVerifyPin, (PVOID) &dwCallbackReturn)))
  3540. {
  3541. printf("PinCacheAdd 1 change-fail ");
  3542. goto Ret;
  3543. }
  3544. // Cache should have been preserved
  3545. cbPin = sizeof(rgPin2);
  3546. ZeroMemory(pbPin, sizeof(rgPin2));
  3547. if (ERROR_SUCCESS != (dwError = PinCacheQuery(hCache, pbPin, &cbPin)))
  3548. {
  3549. printf("PinCacheQuery 1 change-fail ");
  3550. goto Ret;
  3551. }
  3552. if (sizeof(rgPin2) != RtlCompareMemory(pbPin, rgPin2, cbPin))
  3553. {
  3554. PrintBytes("Actual cache", pbPin, cbPin);
  3555. PrintBytes("Expected cache", rgPin2, sizeof(rgPin2));
  3556. printf("Cache is incorrect 1 change-fail ");
  3557. goto Ret;
  3558. }
  3559. //
  3560. // (2)
  3561. // Different LogonID, different Pin
  3562. //
  3563. SetLogonID(2);
  3564. Pins.cbCurrentPin -= 1;
  3565. Pins.cbNewPin = 0;
  3566. Pins.pbNewPin = NULL;
  3567. dwCallbackReturn = ERROR_SUCCESS;
  3568. if (SCARD_W_WRONG_CHV != (dwError =
  3569. PinCacheAdd(&hCache, &Pins,
  3570. pfnVerifyPin, (PVOID) &dwCallbackReturn)))
  3571. {
  3572. printf("PinCacheAdd 2 ");
  3573. goto Ret;
  3574. }
  3575. SetLogonID(1);
  3576. cbPin = sizeof(rgPin2);
  3577. if (ERROR_SUCCESS != (dwError = PinCacheQuery(hCache, pbPin, &cbPin)))
  3578. {
  3579. printf("PinCacheQuery 2 ");
  3580. goto Ret;
  3581. }
  3582. if (sizeof(rgPin2) != RtlCompareMemory(pbPin, rgPin2, cbPin))
  3583. {
  3584. PrintBytes("Actual cache", pbPin, cbPin);
  3585. PrintBytes("Expected cache", rgPin2, sizeof(rgPin2));
  3586. printf("Cache is incorrect 2 ");
  3587. goto Ret;
  3588. }
  3589. //
  3590. // (3)
  3591. // Different LogonID, same Pin
  3592. //
  3593. SetLogonID(2);
  3594. Pins.cbCurrentPin = sizeof(rgPin2);
  3595. if (ERROR_SUCCESS != (dwError =
  3596. PinCacheAdd(&hCache, &Pins,
  3597. pfnVerifyPin, (PVOID) &dwCallbackReturn)))
  3598. {
  3599. printf("PinCacheAdd 3 ");
  3600. goto Ret;
  3601. }
  3602. cbPin = sizeof(rgPin2);
  3603. if (ERROR_SUCCESS != (dwError = PinCacheQuery(hCache, pbPin, &cbPin)))
  3604. {
  3605. printf("PinCacheQuery 3 ");
  3606. goto Ret;
  3607. }
  3608. if (sizeof(rgPin2) != RtlCompareMemory(pbPin, rgPin2, cbPin))
  3609. {
  3610. PrintBytes("Actual cache", pbPin, cbPin);
  3611. PrintBytes("Expected cache", rgPin2, sizeof(rgPin2));
  3612. printf("Cache is incorrect 3 ");
  3613. goto Ret;
  3614. }
  3615. SetLogonID(1);
  3616. if (ERROR_SUCCESS != (dwError = PinCacheQuery(hCache, pbPin, &cbPin)))
  3617. {
  3618. printf("PinCacheQuery 3,1 ");
  3619. goto Ret;
  3620. }
  3621. if (0 != cbPin)
  3622. {
  3623. printf("PinCacheQuery 3,1 should have returned NULL pin ");
  3624. goto Ret;
  3625. }
  3626. //
  3627. // (4)
  3628. // Same LogonID, different Pin
  3629. //
  3630. SetLogonID(2);
  3631. Pins.cbCurrentPin -= 1;
  3632. if (SCARD_W_WRONG_CHV != (dwError =
  3633. PinCacheAdd(&hCache, &Pins,
  3634. pfnVerifyPin, (PVOID) &dwCallbackReturn)))
  3635. {
  3636. printf("PinCacheAdd 4 ");
  3637. goto Ret;
  3638. }
  3639. // cache should have been left intact
  3640. cbPin = sizeof(rgPin2);
  3641. if (ERROR_SUCCESS != (dwError = PinCacheQuery(hCache, pbPin, &cbPin)))
  3642. {
  3643. printf("PinCacheQuery 4 ");
  3644. goto Ret;
  3645. }
  3646. if (sizeof(rgPin2) != RtlCompareMemory(pbPin, rgPin2, cbPin))
  3647. {
  3648. PrintBytes("Actual cache", pbPin, cbPin);
  3649. PrintBytes("Expected cache", rgPin2, sizeof(rgPin2));
  3650. printf("Cache is incorrect 4 ");
  3651. goto Ret;
  3652. }
  3653. if (! pThreadData->fSkipPinAttackTest)
  3654. {
  3655. //
  3656. // (5)
  3657. // Pin discovery attack
  3658. //
  3659. SetLogonID(2);
  3660. Pins.cbCurrentPin -= 1;
  3661. for (i = 0; i < 6; i++)
  3662. {
  3663. printf("PinCacheAdd should not delay ...\n");
  3664. if (SCARD_W_WRONG_CHV != (dwError =
  3665. PinCacheAdd(&hCache, &Pins,
  3666. pfnVerifyPin, (PVOID) &dwCallbackReturn)))
  3667. {
  3668. printf("PinCacheAdd 5 . %d ", i);
  3669. goto Ret;
  3670. }
  3671. }
  3672. SetLogonID(1);
  3673. for (i = 0; i < 6; i++)
  3674. {
  3675. printf("PinCacheAdd should delay ...\n");
  3676. if (SCARD_W_WRONG_CHV != (dwError =
  3677. PinCacheAdd(&hCache, &Pins,
  3678. pfnVerifyPin, (PVOID) &dwCallbackReturn)))
  3679. {
  3680. printf("PinCacheAdd 6 . %d ", i);
  3681. goto Ret;
  3682. }
  3683. }
  3684. // cache should have been left intact
  3685. SetLogonID(2);
  3686. Pins.cbCurrentPin += 1;
  3687. cbPin = sizeof(rgPin2);
  3688. if (ERROR_SUCCESS != (dwError = PinCacheQuery(hCache, pbPin, &cbPin)))
  3689. {
  3690. printf("PinCacheQuery 5 ");
  3691. goto Ret;
  3692. }
  3693. if (sizeof(rgPin2) != RtlCompareMemory(pbPin, rgPin2, cbPin))
  3694. {
  3695. PrintBytes("Actual cache", pbPin, cbPin);
  3696. PrintBytes("Expected cache", rgPin2, sizeof(rgPin2));
  3697. printf("Cache is incorrect 5 ");
  3698. goto Ret;
  3699. }
  3700. //
  3701. // Test PinCachePresentPin
  3702. //
  3703. dwCallbackReturn = 0x7070;
  3704. if (0x7070 != (dwError =
  3705. PinCachePresentPin(hCache, pfnVerifyPin, (PVOID) &dwCallbackReturn)))
  3706. {
  3707. printf("PinCachePresentPin fail ");
  3708. goto Ret;
  3709. }
  3710. SetLogonID(1);
  3711. if (SCARD_W_CARD_NOT_AUTHENTICATED != (dwError =
  3712. PinCachePresentPin(hCache, pfnVerifyPin, (PVOID) &dwCallbackReturn)))
  3713. {
  3714. printf("PinCachePresentPin wrong luid fail ");
  3715. goto Ret;
  3716. }
  3717. }
  3718. dwError = ERROR_SUCCESS;
  3719. fSuccess = TRUE;
  3720. Ret:
  3721. if (pbPin)
  3722. MyFree(pbPin);
  3723. if (hCache)
  3724. PinCacheFlush(&hCache);
  3725. if (! fSuccess)
  3726. {
  3727. printf("- error 0x%x\n", dwError);
  3728. if (0 == dwError)
  3729. return -1;
  3730. }
  3731. return dwError;
  3732. }
  3733. //
  3734. // Function: VerifyDesKeyParams
  3735. //
  3736. DWORD VerifyDesKeyParams(
  3737. IN HCRYPTKEY hKey,
  3738. IN DWORD dwExpectedKeyLen,
  3739. IN DWORD dwExpectedEffectiveKeyLen)
  3740. {
  3741. DWORD dwError = 0;
  3742. DWORD dwParam = 0;
  3743. DWORD cb = sizeof(dwParam);
  3744. BOOL fSuccess = FALSE;
  3745. if (! CryptGetKeyParam(hKey, KP_KEYLEN, (PBYTE) &dwParam, &cb, 0))
  3746. {
  3747. dwError = GetLastError();
  3748. printf("CryptGetKeyParam KP_KEYLEN ");
  3749. goto Ret;
  3750. }
  3751. if (dwExpectedKeyLen != dwParam)
  3752. {
  3753. printf(
  3754. "FAIL: VerifyDesKeyParams expected KP_KEYLEN=%d, actual=%d\n",
  3755. dwExpectedKeyLen, dwParam);
  3756. goto Ret;
  3757. }
  3758. if (! CryptGetKeyParam(hKey, KP_EFFECTIVE_KEYLEN, (PBYTE) &dwParam, &cb, 0))
  3759. {
  3760. dwError = GetLastError();
  3761. printf("CryptGetKeyParam KP_EFFECTIVE_KEYLEN ");
  3762. goto Ret;
  3763. }
  3764. if (dwExpectedEffectiveKeyLen != dwParam)
  3765. {
  3766. printf(
  3767. "FAIL: VerifyDesKeyParams expected KP_EFFECTIVE_KEYLEN=%d, actual=%d\n",
  3768. dwExpectedEffectiveKeyLen, dwParam);
  3769. goto Ret;
  3770. }
  3771. fSuccess = TRUE;
  3772. Ret:
  3773. if (! fSuccess)
  3774. {
  3775. printf(" - error 0x%x\n", dwError);
  3776. if (0 == dwError)
  3777. dwError = -1;
  3778. }
  3779. return dwError;
  3780. }
  3781. //
  3782. // Function: DesGetKeyParamRegression
  3783. //
  3784. DWORD DesGetKeyParamRegression(PTHREAD_DATA pThreadData)
  3785. {
  3786. DWORD dwError = ERROR_SUCCESS;
  3787. BOOL fSuccess = FALSE;
  3788. HCRYPTKEY hKey = 0;
  3789. if (! CryptGenKey(pThreadData->hProv, CALG_DES, 0, &hKey))
  3790. {
  3791. dwError = GetLastError();
  3792. printf("CryptGenKey des ");
  3793. goto Ret;
  3794. }
  3795. if (ERROR_SUCCESS != (dwError = VerifyDesKeyParams(hKey, 64, 56)))
  3796. {
  3797. printf("VerifyDesKeyParams des ");
  3798. goto Ret;
  3799. }
  3800. if (! CryptDestroyKey(hKey))
  3801. {
  3802. dwError = GetLastError();
  3803. printf("CryptDestroyKey des ");
  3804. goto Ret;
  3805. }
  3806. hKey = 0;
  3807. if (! CryptGenKey(pThreadData->hProv, CALG_3DES_112, 0, &hKey))
  3808. {
  3809. dwError = GetLastError();
  3810. printf("CryptGenKey 3des_112 ");
  3811. goto Ret;
  3812. }
  3813. if (ERROR_SUCCESS != (dwError = VerifyDesKeyParams(hKey, 128, 112)))
  3814. {
  3815. printf("VerifyDesKeyParams 3des_112 ");
  3816. goto Ret;
  3817. }
  3818. if (! CryptDestroyKey(hKey))
  3819. {
  3820. dwError = GetLastError();
  3821. printf("CryptDestroyKey 3des_112 ");
  3822. goto Ret;
  3823. }
  3824. hKey = 0;
  3825. if (! CryptGenKey(pThreadData->hProv, CALG_3DES, 0, &hKey))
  3826. {
  3827. dwError = GetLastError();
  3828. printf("CryptGenKey 3des ");
  3829. goto Ret;
  3830. }
  3831. if (ERROR_SUCCESS != (dwError = VerifyDesKeyParams(hKey, 192, 168)))
  3832. {
  3833. printf("VerifyDesKeyParams 3des ");
  3834. goto Ret;
  3835. }
  3836. if (! CryptDestroyKey(hKey))
  3837. {
  3838. dwError = GetLastError();
  3839. printf("CryptDestroyKey 3des ");
  3840. goto Ret;
  3841. }
  3842. fSuccess = TRUE;
  3843. Ret:
  3844. if (! fSuccess)
  3845. {
  3846. printf("- error 0x%x\n", dwError);
  3847. if (0 == dwError)
  3848. return -1;
  3849. }
  3850. return dwError;
  3851. }
  3852. //
  3853. // Function: MacEncryptRegression
  3854. //
  3855. DWORD MacEncryptRegression(
  3856. IN PTHREAD_DATA pThreadData)
  3857. {
  3858. DWORD dwError = ERROR_SUCCESS;
  3859. BOOL fSuccess = FALSE;
  3860. HCRYPTKEY hSymKey = 0;
  3861. HCRYPTKEY hMacKey = 0;
  3862. HCRYPTKEY hMacKey2 = 0;
  3863. HCRYPTHASH hMac = 0;
  3864. BYTE rgPlaintext[31];
  3865. DWORD cb = sizeof(rgPlaintext);
  3866. DWORD cbBuf = 0;
  3867. PBYTE pbMac = NULL;
  3868. DWORD cbMac = 0;
  3869. PBYTE pb = NULL;
  3870. DWORD cbAlign = 0;
  3871. #define GetNextAlignedValue(c, alignment) ((c + alignment) & ~(alignment - 1))
  3872. #define GetPtrAlignedSize(cb) (GetNextAlignedValue(cb, sizeof(ULONG_PTR)))
  3873. cbAlign = GetNextAlignedValue(60, 4);
  3874. cbAlign = GetPtrAlignedSize(60);
  3875. if (! CryptGenKey(pThreadData->hProv, CALG_RC2, 0, &hSymKey))
  3876. {
  3877. printf("CryptGenKey rc2");
  3878. goto Ret;
  3879. }
  3880. if (! CryptGenKey(pThreadData->hProv, CALG_RC2, 0, &hMacKey))
  3881. {
  3882. printf("CryptGenKey rc2 2");
  3883. goto Ret;
  3884. }
  3885. if (! CryptDuplicateKey(hMacKey, NULL, 0, &hMacKey2))
  3886. {
  3887. printf("CryptDuplicateKey");
  3888. goto Ret;
  3889. }
  3890. if (! CryptCreateHash(pThreadData->hProv, CALG_MAC, hMacKey, 0, &hMac))
  3891. {
  3892. printf("CryptCreateHash");
  3893. goto Ret;
  3894. }
  3895. while (cb--)
  3896. rgPlaintext[cb] = (BYTE) cb;
  3897. cb = sizeof(rgPlaintext);
  3898. if (! CryptEncrypt(hSymKey, 0, TRUE, 0, NULL, &cb, 0))
  3899. {
  3900. printf("CryptEncrypt size");
  3901. goto Ret;
  3902. }
  3903. if (NULL == (pb = (PBYTE) MyAlloc(cb)))
  3904. return ERROR_NOT_ENOUGH_MEMORY;
  3905. cbBuf = cb;
  3906. cb = sizeof(rgPlaintext);
  3907. if (! CryptEncrypt(hSymKey, hMac, TRUE, 0, pb, &cb, cbBuf))
  3908. {
  3909. printf("CryptEncrypt");
  3910. goto Ret;
  3911. }
  3912. if (! CryptGetHashParam(hMac, HP_HASHVAL, NULL, &cbMac, 0))
  3913. {
  3914. printf("CryptGetHashParam size");
  3915. goto Ret;
  3916. }
  3917. if (NULL == (pbMac = (PBYTE) MyAlloc(cbMac)))
  3918. return ERROR_NOT_ENOUGH_MEMORY;
  3919. if (! CryptGetHashParam(hMac, HP_HASHVAL, pbMac, &cbMac, 0))
  3920. {
  3921. printf("CryptGetHashParam");
  3922. goto Ret;
  3923. }
  3924. if (! CryptDestroyHash(hMac))
  3925. {
  3926. printf("CryptDestroyHash");
  3927. goto Ret;
  3928. }
  3929. hMac = 0;
  3930. if (! CryptCreateHash(pThreadData->hProv, CALG_MAC, hMacKey2, 0, &hMac))
  3931. {
  3932. printf("CryptCreateHash 2");
  3933. goto Ret;
  3934. }
  3935. if (! CryptDecrypt(hSymKey, hMac, TRUE, 0, pb, &cb))
  3936. {
  3937. printf("CryptDecrypt");
  3938. goto Ret;
  3939. }
  3940. MyFree(pb);
  3941. pb = NULL;
  3942. if (! CryptGetHashParam(hMac, HP_HASHVAL, NULL, &cb, 0))
  3943. {
  3944. printf("CryptGetHashParam size");
  3945. goto Ret;
  3946. }
  3947. if (NULL == (pb = (PBYTE) MyAlloc(cb)))
  3948. return ERROR_NOT_ENOUGH_MEMORY;
  3949. if (! CryptGetHashParam(hMac, HP_HASHVAL, pb, &cb, 0))
  3950. {
  3951. printf("CryptGetHashParam");
  3952. goto Ret;
  3953. }
  3954. PrintBytes("Expected Mac result", pbMac, cbMac);
  3955. PrintBytes("Actual Mac result", pb, cb);
  3956. if (0 != memcmp(pb, pbMac, cb)
  3957. || 0 == cb
  3958. || cb != cbMac)
  3959. {
  3960. goto Ret;
  3961. }
  3962. fSuccess = TRUE;
  3963. Ret:
  3964. if (! fSuccess)
  3965. {
  3966. dwError = GetLastError();
  3967. printf(" - error 0x%x\n", dwError);
  3968. if (0 == dwError)
  3969. dwError = -1;
  3970. }
  3971. if (pb)
  3972. MyFree(pb);
  3973. if (pbMac)
  3974. MyFree(pbMac);
  3975. if (hSymKey)
  3976. CryptDestroyKey(hSymKey);
  3977. if (hMacKey)
  3978. CryptDestroyKey(hMacKey);
  3979. if (hMacKey2)
  3980. CryptDestroyKey(hMacKey2);
  3981. if (hMac)
  3982. CryptDestroyHash(hMac);
  3983. return dwError;
  3984. }
  3985. //
  3986. // Function: StressEncryptionTest
  3987. //
  3988. DWORD StressEncryptionTest(
  3989. IN HCRYPTPROV hProv,
  3990. IN PENCRYPTION_TEST_DATA pTestData)
  3991. {
  3992. HCRYPTKEY hEncryptionKey = 0;
  3993. HCRYPTKEY hHashKey1 = 0;
  3994. HCRYPTKEY hHashKey2 = 0;
  3995. HCRYPTHASH hHash = 0;
  3996. PBYTE pbData = 0;
  3997. DWORD dwData = 0;
  3998. DWORD cbData = 0;
  3999. DWORD cbPlainText = 0;
  4000. DWORD cbCipherText = 0;
  4001. DWORD cbProcessed = 0;
  4002. DWORD dwKeyAlg = 0;
  4003. DWORD dwBlockLen = 0;
  4004. BOOL fFinal = FALSE;
  4005. DWORD dwError = 0;
  4006. BOOL fSuccess = FALSE;
  4007. BYTE rgbHashVal1[200];
  4008. BYTE rgbHashVal2[200];
  4009. ZeroMemory(rgbHashVal1, 200);
  4010. ZeroMemory(rgbHashVal2, 200);
  4011. if (! CryptGenKey(
  4012. hProv,
  4013. pTestData->aiEncryptionKey,
  4014. 0,
  4015. &hEncryptionKey))
  4016. {
  4017. goto Cleanup;
  4018. }
  4019. //
  4020. // Check for requested simultaneous encryption/hashing
  4021. //
  4022. if (pTestData->aiHash)
  4023. {
  4024. //
  4025. // Is this a keyed hash?
  4026. //
  4027. if (pTestData->aiHashKey)
  4028. {
  4029. if (! CryptGenKey(
  4030. hProv,
  4031. pTestData->aiHashKey,
  4032. 0,
  4033. &hHashKey1))
  4034. {
  4035. goto Cleanup;
  4036. }
  4037. //
  4038. // To verify the result of hashing the same data in two
  4039. // separate keyed hashes, the key must first be duplicated,
  4040. // since its state changes once it's used.
  4041. //
  4042. if (! CryptDuplicateKey(
  4043. hHashKey1,
  4044. NULL,
  4045. 0,
  4046. &hHashKey2))
  4047. {
  4048. goto Cleanup;
  4049. }
  4050. }
  4051. if (! CryptCreateHash(
  4052. hProv,
  4053. pTestData->aiHash,
  4054. hHashKey1,
  4055. 0,
  4056. &hHash))
  4057. {
  4058. goto Cleanup;
  4059. }
  4060. }
  4061. //
  4062. // Is this a block encryption alg?
  4063. //
  4064. if (ALG_TYPE_BLOCK & pTestData->aiEncryptionKey)
  4065. {
  4066. //
  4067. // Get the block size of this encryption alg
  4068. //
  4069. cbData = sizeof(dwBlockLen);
  4070. if (! CryptGetKeyParam(
  4071. hEncryptionKey,
  4072. KP_BLOCKLEN,
  4073. (PBYTE) &dwBlockLen,
  4074. &cbData,
  4075. 0))
  4076. {
  4077. goto Cleanup;
  4078. }
  4079. //
  4080. // Choose an "interesting" plaintext length, based on the block length
  4081. // of this alg.
  4082. //
  4083. cbPlainText = 2 * dwBlockLen + 1;
  4084. }
  4085. else
  4086. {
  4087. //
  4088. // Plaintext length for a stream encryption alg
  4089. //
  4090. cbPlainText = 500;
  4091. }
  4092. cbCipherText = cbPlainText;
  4093. //
  4094. // Determine size of ciphertext
  4095. //
  4096. if (! CryptEncrypt(
  4097. hEncryptionKey,
  4098. 0,
  4099. TRUE,
  4100. 0,
  4101. NULL,
  4102. &cbCipherText,
  4103. 0))
  4104. {
  4105. goto Cleanup;
  4106. }
  4107. if (NULL == (pbData = (PBYTE) MyAlloc(cbCipherText)))
  4108. {
  4109. goto Cleanup;
  4110. }
  4111. //
  4112. // Initialize the plaintext
  4113. //
  4114. memset(pbData, 0xDA, cbPlainText);
  4115. memset(pbData + cbPlainText, 0, cbCipherText - cbPlainText);
  4116. //
  4117. // Encrypt
  4118. //
  4119. cbProcessed = 0;
  4120. while (! fFinal)
  4121. {
  4122. if (0 == dwBlockLen)
  4123. {
  4124. cbData = cbPlainText;
  4125. fFinal = TRUE;
  4126. }
  4127. else
  4128. {
  4129. if (cbPlainText - cbProcessed > dwBlockLen)
  4130. {
  4131. cbData = dwBlockLen;
  4132. }
  4133. else
  4134. {
  4135. cbData = cbPlainText % dwBlockLen;
  4136. fFinal = TRUE;
  4137. }
  4138. }
  4139. if (! CryptEncrypt(
  4140. hEncryptionKey,
  4141. hHash,
  4142. fFinal,
  4143. 0,
  4144. pbData + cbProcessed,
  4145. &cbData,
  4146. cbCipherText))
  4147. {
  4148. goto Cleanup;
  4149. }
  4150. cbProcessed += cbData;
  4151. }
  4152. if (cbProcessed != cbCipherText)
  4153. {
  4154. goto Cleanup;
  4155. }
  4156. if (0 != hHash)
  4157. {
  4158. //
  4159. // Get hash result from encryption
  4160. //
  4161. cbData = sizeof(rgbHashVal1);
  4162. if (! CryptGetHashParam(
  4163. hHash,
  4164. HP_HASHVAL,
  4165. rgbHashVal1,
  4166. &cbData,
  4167. 0))
  4168. {
  4169. goto Cleanup;
  4170. }
  4171. if (! CryptDestroyHash(hHash))
  4172. {
  4173. goto Cleanup;
  4174. }
  4175. if (! CryptCreateHash(
  4176. hProv,
  4177. pTestData->aiHash,
  4178. hHashKey2,
  4179. 0,
  4180. &hHash))
  4181. {
  4182. goto Cleanup;
  4183. }
  4184. }
  4185. //
  4186. // Decrypt
  4187. //
  4188. cbProcessed = 0;
  4189. fFinal = FALSE;
  4190. while (! fFinal)
  4191. {
  4192. if (0 == dwBlockLen)
  4193. {
  4194. cbData = cbCipherText;
  4195. fFinal = TRUE;
  4196. }
  4197. else
  4198. {
  4199. if (cbCipherText - cbProcessed > dwBlockLen)
  4200. {
  4201. cbData = dwBlockLen;
  4202. }
  4203. else
  4204. {
  4205. cbData = cbCipherText - cbProcessed;
  4206. fFinal = TRUE;
  4207. }
  4208. }
  4209. if (! CryptDecrypt(
  4210. hEncryptionKey,
  4211. hHash,
  4212. fFinal,
  4213. 0,
  4214. pbData + cbProcessed,
  4215. &cbData))
  4216. {
  4217. goto Cleanup;
  4218. }
  4219. cbProcessed += cbData;
  4220. }
  4221. if (cbProcessed != cbPlainText)
  4222. {
  4223. goto Cleanup;
  4224. }
  4225. while (cbPlainText)
  4226. {
  4227. if (0xDA != pbData[cbPlainText - 1])
  4228. {
  4229. goto Cleanup;
  4230. }
  4231. cbPlainText--;
  4232. }
  4233. if (0 != hHash)
  4234. {
  4235. //
  4236. // Get hash result from decryption
  4237. //
  4238. cbData = sizeof(rgbHashVal2);
  4239. if (! CryptGetHashParam(
  4240. hHash,
  4241. HP_HASHVAL,
  4242. rgbHashVal2,
  4243. &cbData,
  4244. 0))
  4245. {
  4246. goto Cleanup;
  4247. }
  4248. if (0 != memcmp(rgbHashVal1, rgbHashVal2, cbData))
  4249. {
  4250. goto Cleanup;
  4251. }
  4252. }
  4253. fSuccess = TRUE;
  4254. Cleanup:
  4255. if (! fSuccess)
  4256. {
  4257. if (0 == (dwError = GetLastError()))
  4258. {
  4259. dwError = -1;
  4260. }
  4261. }
  4262. if (hEncryptionKey)
  4263. {
  4264. CryptDestroyKey(hEncryptionKey);
  4265. }
  4266. if (hHashKey1)
  4267. {
  4268. CryptDestroyKey(hHashKey1);
  4269. }
  4270. if (hHashKey2)
  4271. {
  4272. CryptDestroyKey(hHashKey2);
  4273. }
  4274. if (hHash)
  4275. {
  4276. CryptDestroyHash(hHash);
  4277. }
  4278. if (pbData)
  4279. {
  4280. MyFree(pbData);
  4281. }
  4282. return dwError;
  4283. }
  4284. //
  4285. // Function: StressTestAllEncryptionAlgs
  4286. //
  4287. DWORD StressTestAllEncryptionAlgs(
  4288. PTHREAD_DATA pThreadData,
  4289. BOOL fContinueOnMacError,
  4290. BOOL *pfMacErrorOccurred)
  4291. {
  4292. DWORD dwError = 0;
  4293. ENCRYPTION_TEST_DATA TestData;
  4294. PALGNODE pEncryptionAlg, pHashAlg, pBlockEncryptionAlg;
  4295. ZeroMemory(&TestData, sizeof(TestData));
  4296. for ( pEncryptionAlg = pThreadData->pAlgList;
  4297. NULL != pEncryptionAlg;
  4298. pEncryptionAlg = pEncryptionAlg->pNext)
  4299. {
  4300. if (! (pEncryptionAlg->EnumalgsEx.aiAlgid & ALG_CLASS_DATA_ENCRYPT))
  4301. continue;
  4302. TestData.aiEncryptionKey = pEncryptionAlg->EnumalgsEx.aiAlgid;
  4303. for ( pHashAlg = pThreadData->pAlgList;
  4304. NULL != pHashAlg;
  4305. pHashAlg = pHashAlg->pNext)
  4306. {
  4307. if ( (! (pHashAlg->EnumalgsEx.aiAlgid & ALG_CLASS_HASH &&
  4308. pHashAlg->EnumalgsEx.aiAlgid & ALG_TYPE_ANY)) ||
  4309. CALG_SSL3_SHAMD5 == pHashAlg->EnumalgsEx.aiAlgid ||
  4310. CALG_TLS1PRF == pHashAlg->EnumalgsEx.aiAlgid)
  4311. continue;
  4312. TestData.aiHash = pHashAlg->EnumalgsEx.aiAlgid;
  4313. if (CALG_MAC == pHashAlg->EnumalgsEx.aiAlgid)
  4314. {
  4315. for ( pBlockEncryptionAlg = pThreadData->pAlgList;
  4316. NULL != pBlockEncryptionAlg;
  4317. pBlockEncryptionAlg = pBlockEncryptionAlg->pNext)
  4318. {
  4319. if (! ( pBlockEncryptionAlg->EnumalgsEx.aiAlgid & ALG_CLASS_DATA_ENCRYPT &&
  4320. pBlockEncryptionAlg->EnumalgsEx.aiAlgid & ALG_TYPE_BLOCK))
  4321. continue;
  4322. TestData.aiHashKey = pBlockEncryptionAlg->EnumalgsEx.aiAlgid;
  4323. if (ERROR_SUCCESS != (dwError = StressEncryptionTest(pThreadData->hProv, &TestData)))
  4324. {
  4325. if (fContinueOnMacError)
  4326. *pfMacErrorOccurred = TRUE;
  4327. else
  4328. return dwError;
  4329. }
  4330. }
  4331. }
  4332. else
  4333. {
  4334. if (ERROR_SUCCESS != (dwError = StressEncryptionTest(pThreadData->hProv, &TestData)))
  4335. return dwError;
  4336. }
  4337. }
  4338. }
  4339. return dwError;
  4340. }
  4341. //+ ===========================================================================
  4342. //- ===========================================================================
  4343. void L_ErrorBox(LPSTR pszMsg, DWORD dwThreadNum)
  4344. {
  4345. char szErrorMsg[256] ;
  4346. sprintf(szErrorMsg, "Thread %d: %s in L_ErrorBox", dwThreadNum, pszMsg) ;
  4347. MessageBox(NULL, szErrorMsg, ERROR_CAPTION, MB_OK | MB_ICONERROR) ;
  4348. }
  4349. //+ ===========================================================================
  4350. //- ===========================================================================
  4351. void L_LastErrorBox(LPSTR pszMsg, DWORD dwThreadNum)
  4352. {
  4353. char szErrorMsg[256] ;
  4354. sprintf(szErrorMsg, "Thread %d: %s 0x%x in L_LastErrorBox", dwThreadNum, pszMsg, GetLastError()) ;
  4355. MessageBox(NULL, szErrorMsg, ERROR_CAPTION, MB_OK | MB_ICONERROR) ;
  4356. }
  4357. //+ =================================================================================
  4358. //
  4359. // L_GetKeyAlg
  4360. // Local function that given a key handle, returns the key Alg.
  4361. //
  4362. //- =================================================================================
  4363. DWORD L_GetKeyAlg(HCRYPTKEY hKey)
  4364. {
  4365. DWORD dwData = 0;
  4366. DWORD cbData=0 ;
  4367. char szErrorMsg[256];
  4368. cbData = sizeof(dwData) ;
  4369. if (!CryptGetKeyParam(
  4370. hKey,
  4371. KP_ALGID,
  4372. (PBYTE) &dwData,
  4373. &cbData,
  4374. 0))
  4375. {
  4376. GENERIC_FAIL(CryptGetKeyParam) ;
  4377. }
  4378. ErrorReturn:
  4379. return dwData;
  4380. }
  4381. //+ =================================================================================
  4382. //
  4383. // L_GetKeySize
  4384. // Local function that given a key handle, returns the key length.
  4385. //
  4386. //- =================================================================================
  4387. DWORD L_GetKeySize(HCRYPTKEY hKey)
  4388. {
  4389. DWORD dwData = 0;
  4390. DWORD cbData=0 ;
  4391. char szErrorMsg[256];
  4392. cbData = sizeof(dwData) ;
  4393. if (!CryptGetKeyParam(
  4394. hKey,
  4395. KP_KEYLEN,
  4396. (PBYTE) &dwData,
  4397. &cbData,
  4398. 0))
  4399. {
  4400. GENERIC_FAIL(CryptGetKeyParam) ;
  4401. }
  4402. ErrorReturn:
  4403. return dwData;
  4404. }
  4405. //+ ==============================================================================
  4406. //- ==============================================================================
  4407. DWORD Hlp_GetKeyAlgId(HCRYPTKEY hKey)
  4408. {
  4409. DWORD dwRetVal=0 ;
  4410. PBYTE pbData=NULL ;
  4411. DWORD cbData=sizeof(DWORD) ;
  4412. char szErrorMsg[256] ;
  4413. DWORD dwAlgId=0 ;
  4414. if (!CryptGetKeyParam(
  4415. hKey,
  4416. KP_ALGID,
  4417. (PBYTE)&dwAlgId,
  4418. &cbData,
  4419. 0))
  4420. GENERIC_FAIL(CryptGetKeyParam) ;
  4421. dwRetVal=dwAlgId ;
  4422. ErrorReturn :
  4423. return dwRetVal ;
  4424. }
  4425. //+ =================================================================================
  4426. //
  4427. // L_GetKeyParam
  4428. // Local function that given a key handle, retrieves the specified Key Param.
  4429. // The Key param is not of too much interest in this case. In a multithread scenario,
  4430. // we just care to see if the call succeeds.
  4431. //
  4432. //- =================================================================================
  4433. DWORD L_GetKeyParam(HCRYPTKEY hKey, DWORD dwParam)
  4434. {
  4435. DWORD dwRetVal=0 ;
  4436. PBYTE pbData=NULL ;
  4437. DWORD cbData=0 ;
  4438. char szErrorMsg[256] ;
  4439. DWORD dwAlgId=0 ;
  4440. DWORD dwError=0 ;
  4441. if (!CryptGetKeyParam( hKey,
  4442. dwParam,
  4443. NULL,
  4444. &cbData,
  4445. 0))
  4446. GENERIC_FAIL(CryptGetKeyParam) ;
  4447. if (NULL == (pbData = (PBYTE) MyAlloc(cbData)))
  4448. ALLOC_FAIL(pbData);
  4449. if (!CryptGetKeyParam( hKey,
  4450. dwParam,
  4451. pbData,
  4452. &cbData,
  4453. 0))
  4454. GENERIC_FAIL(CryptGetKeyParam) ;
  4455. dwRetVal=1 ;
  4456. ErrorReturn :
  4457. MyFree(pbData) ;
  4458. return dwRetVal ;
  4459. }
  4460. //+ ======================================================================================
  4461. // ProgramInit
  4462. // Acquire context
  4463. // Generate Keys that will be used by all the threads. (AT_SIGNATURE and AT_KEYEXCHANGE)
  4464. //- ======================================================================================
  4465. DWORD ProgramInit(PTHREAD_DATA pThreadData)
  4466. {
  4467. DWORD dwRetVal = 0;
  4468. char szErrorMsg[256]; // defined for GENERIC_FAIL
  4469. LPSTR pszContainer = NULL;
  4470. DWORD dwContextFlags = 0;
  4471. DWORD dwKeyFlags = CRYPT_EXPORTABLE;
  4472. if (pThreadData->fEphemeralKeys)
  4473. {
  4474. dwContextFlags = CRYPT_VERIFYCONTEXT;
  4475. }
  4476. else
  4477. {
  4478. pszContainer = KEY_CONTAINER_NAME;
  4479. dwContextFlags = CRYPT_NEWKEYSET;
  4480. CryptAcquireContext(
  4481. &pThreadData->hProv,
  4482. pszContainer,
  4483. pThreadData->rgszProvName,
  4484. pThreadData->dwProvType,
  4485. CRYPT_DELETEKEYSET);
  4486. // Create a Verify Context for some of the sub-tests to use
  4487. if (! CryptAcquireContext(
  4488. &pThreadData->hVerifyCtx,
  4489. NULL,
  4490. pThreadData->rgszProvName,
  4491. pThreadData->dwProvType,
  4492. CRYPT_VERIFYCONTEXT))
  4493. {
  4494. pThreadData->hVerifyCtx = 0;
  4495. GENERIC_FAIL(CryptAcquireContext_VERIFYCONTEXT);
  4496. }
  4497. }
  4498. if (pThreadData->fUserProtectedKeys)
  4499. dwKeyFlags |= CRYPT_USER_PROTECTED;
  4500. if (!CryptAcquireContext(
  4501. &pThreadData->hProv,
  4502. pszContainer,
  4503. pThreadData->rgszProvName,
  4504. pThreadData->dwProvType,
  4505. dwContextFlags))
  4506. {
  4507. pThreadData->hProv = 0;
  4508. GENERIC_FAIL(CryptAcquireContext_Init);
  4509. }
  4510. // Generate a sign and exchange key
  4511. if (!CryptGenKey(
  4512. pThreadData->hProv,
  4513. AT_SIGNATURE,
  4514. dwKeyFlags,
  4515. &pThreadData->hSignatureKey))
  4516. {
  4517. GENERIC_FAIL(CryptGenKey_AT_SIGNATURE);
  4518. CryptReleaseContext(pThreadData->hProv, 0);
  4519. pThreadData->hProv = 0;
  4520. pThreadData->hSignatureKey = 0;
  4521. }
  4522. if (PROV_RSA_FULL == pThreadData->dwProvType ||
  4523. PROV_RSA_AES == pThreadData->dwProvType ||
  4524. PROV_DSS_DH == pThreadData->dwProvType)
  4525. {
  4526. if (!CryptGenKey(
  4527. pThreadData->hProv,
  4528. AT_KEYEXCHANGE,
  4529. dwKeyFlags,
  4530. &pThreadData->hExchangeKey))
  4531. {
  4532. GENERIC_FAIL(CryptGenKey_AT_KEYEXCHANGE);
  4533. CryptReleaseContext(pThreadData->hProv, 0);
  4534. pThreadData->hProv = 0;
  4535. pThreadData->hExchangeKey = 0;
  4536. }
  4537. }
  4538. dwRetVal=1;
  4539. ErrorReturn:
  4540. return dwRetVal;
  4541. }
  4542. // ======================================================================================
  4543. // Terminates all the threads after the specified amount of time has elapsed (-t option)
  4544. // This thread sleeps for the specified amount of time and them then turns off the
  4545. // g_dwLoopSwitch.
  4546. // ======================================================================================
  4547. void WINAPI KillProgramTimer(LPVOID pvThreadData)
  4548. {
  4549. DWORD dwSleepTime=0;
  4550. PTHREAD_DATA pThreadData = (PTHREAD_DATA) pvThreadData;
  4551. if (pThreadData->dwProgramMins)
  4552. {
  4553. dwSleepTime = pThreadData->dwProgramMins * 60 * 1000;
  4554. SleepEx(dwSleepTime, FALSE);
  4555. if (! SetEvent(pThreadData->hEndTestEvent))
  4556. {
  4557. printf("SetEvent() failed, 0x%x\n", GetLastError());
  4558. exit(1);
  4559. }
  4560. printf("All threads should be shutting down now....\n");
  4561. }
  4562. }
  4563. // ======================================================================================
  4564. // Prints the status of all the threads
  4565. // The status is represented in iteration count in ThreadStatus[i][0]
  4566. // ======================================================================================
  4567. void WINAPI PrintThreadStatus(LPVOID pvThreadData)
  4568. {
  4569. DWORD thread;
  4570. PTHREAD_DATA pThreadData = (PTHREAD_DATA) pvThreadData;
  4571. char rgStatus[256] ;
  4572. printf("\n\n\n") ;
  4573. while (WAIT_TIMEOUT == WaitForSingleObject(pThreadData->hEndTestEvent, 0))
  4574. {
  4575. Sleep(10000);
  4576. ZeroMemory(rgStatus, sizeof(rgStatus));
  4577. for (thread = 0; thread < pThreadData->dwThreadCount; thread++)
  4578. {
  4579. sprintf(
  4580. rgStatus + strlen(rgStatus),
  4581. " %4x",
  4582. pThreadData->rgdwThreadStatus[thread]);
  4583. }
  4584. printf("%s\n", rgStatus);
  4585. }
  4586. }
  4587. //+ ========================================================================
  4588. //
  4589. // Function : L_GetAllKeyParams
  4590. // Purpose : Gets all the key params and does nothing with it
  4591. //
  4592. //- ========================================================================
  4593. DWORD L_GetAllKeyParams(HCRYPTKEY hKey, DWORD dwThreadNum)
  4594. {
  4595. char szErrorMsg[256] ;
  4596. ALG_ID AlgId=0 ;
  4597. DWORD dwRetVal=0 ;
  4598. AlgId = (ALG_ID)Hlp_GetKeyAlgId(hKey) ;
  4599. // Get Keys Length
  4600. if (!L_GetKeyParam(hKey, KP_KEYLEN))
  4601. {
  4602. sprintf(szErrorMsg, "Thread %d: L_GetKeyParam KP_KEYLEN error 0x%x",
  4603. dwThreadNum, GetLastError()) ;
  4604. MessageBox(NULL, szErrorMsg, ERROR_CAPTION, MB_OK | MB_ICONERROR) ;
  4605. goto ErrorReturn ;
  4606. }
  4607. // Get ALGID
  4608. if (!L_GetKeyParam(hKey, KP_ALGID))
  4609. {
  4610. sprintf(szErrorMsg, "Thread %d: L_GetKeyParam KP_ALGID error 0x%x",
  4611. dwThreadNum, GetLastError()) ;
  4612. MessageBox(NULL, szErrorMsg, ERROR_CAPTION, MB_OK | MB_ICONERROR) ;
  4613. goto ErrorReturn ;
  4614. }
  4615. // Get KP_BLOCKLEN
  4616. // Although this is meaningful only for block cipher keys, it will not fail
  4617. // for RSA Keys. It'll just return 0 as the block len (which we don't care
  4618. // about for multi tests.
  4619. if (!L_GetKeyParam(hKey, KP_BLOCKLEN))
  4620. {
  4621. sprintf(szErrorMsg, "Thread %d: L_GetKeyParam KP_BLOCKLEN error 0x%x",
  4622. dwThreadNum, GetLastError()) ;
  4623. MessageBox(NULL, szErrorMsg, ERROR_CAPTION, MB_OK | MB_ICONERROR) ;
  4624. goto ErrorReturn ;
  4625. }
  4626. /*
  4627. if (! (ALG_CLASS_SIGNATURE == GET_ALG_CLASS(AlgId)
  4628. || ALG_CLASS_KEY_EXCHANGE == GET_ALG_CLASS(AlgId)))
  4629. {
  4630. if (!L_GetKeyParam(hKey, KP_SALT))
  4631. {
  4632. sprintf(szErrorMsg, "Thread %d: L_GetKeyParam KP_SALT error 0x%x",
  4633. dwThreadNum, GetLastError()) ;
  4634. MessageBox(NULL, szErrorMsg, ERROR_CAPTION, MB_OK | MB_ICONERROR) ;
  4635. goto ErrorReturn ;
  4636. }
  4637. }
  4638. */
  4639. // Get KP_PERMISSIONS
  4640. if (!L_GetKeyParam(hKey, KP_PERMISSIONS))
  4641. {
  4642. sprintf(szErrorMsg, "Thread %d: L_GetKeyParam KP_PERMISSION error 0x%x",
  4643. dwThreadNum, GetLastError()) ;
  4644. MessageBox(NULL, szErrorMsg, ERROR_CAPTION, MB_OK | MB_ICONERROR) ;
  4645. goto ErrorReturn ;
  4646. }
  4647. // Effective KeyLen can be queried only for RC2 key
  4648. if (CALG_RC2 == AlgId)
  4649. {
  4650. // Get KP_EFFECTIVE_KEYLEN
  4651. if (!L_GetKeyParam(hKey, KP_EFFECTIVE_KEYLEN))
  4652. {
  4653. sprintf(szErrorMsg, "Thread %d: L_GetKeyParam KP_EFFECTIVE_KEYLEN error 0x%x",
  4654. dwThreadNum, GetLastError()) ;
  4655. MessageBox(NULL, szErrorMsg, ERROR_CAPTION, MB_OK | MB_ICONERROR) ;
  4656. goto ErrorReturn ;
  4657. }
  4658. }
  4659. // These Key Params are good only for Block Cipher Keys
  4660. if (ALG_TYPE_BLOCK == GET_ALG_TYPE(AlgId)
  4661. && ALG_CLASS_DATA_ENCRYPT == GET_ALG_CLASS(AlgId))
  4662. {
  4663. // Get KP_IV
  4664. if (!L_GetKeyParam(hKey, KP_IV))
  4665. {
  4666. sprintf(szErrorMsg, "Thread %d: L_GetKeyParam KP_IV error 0x%x",
  4667. dwThreadNum, GetLastError()) ;
  4668. MessageBox(NULL, szErrorMsg, ERROR_CAPTION, MB_OK | MB_ICONERROR) ;
  4669. goto ErrorReturn ;
  4670. }
  4671. // Get KP_PADDING
  4672. if (!L_GetKeyParam(hKey, KP_PADDING))
  4673. {
  4674. sprintf(szErrorMsg, "Thread %d: L_GetKeyParam KP_PADDING error 0x%x",
  4675. dwThreadNum, GetLastError()) ;
  4676. MessageBox(NULL, szErrorMsg, ERROR_CAPTION, MB_OK | MB_ICONERROR) ;
  4677. goto ErrorReturn ;
  4678. }
  4679. // Get KP_MODE
  4680. if (!L_GetKeyParam(hKey, KP_MODE))
  4681. {
  4682. sprintf(szErrorMsg, "Thread %d: L_GetKeyParam KP_MODE error 0x%x",
  4683. dwThreadNum, GetLastError()) ;
  4684. MessageBox(NULL, szErrorMsg, ERROR_CAPTION, MB_OK | MB_ICONERROR) ;
  4685. goto ErrorReturn ;
  4686. }
  4687. // Get KP_MODE_BITS
  4688. if (!L_GetKeyParam(hKey, KP_MODE_BITS))
  4689. {
  4690. sprintf(szErrorMsg, "Thread %d: L_GetKeyParam KP_MODE_BITS error 0x%x",
  4691. dwThreadNum, GetLastError()) ;
  4692. MessageBox(NULL, szErrorMsg, ERROR_CAPTION, MB_OK | MB_ICONERROR) ;
  4693. goto ErrorReturn ;
  4694. }
  4695. }
  4696. dwRetVal=1 ;
  4697. ErrorReturn :
  4698. return dwRetVal ;
  4699. }
  4700. /*
  4701. L_ProvParam2Text
  4702. dangriff -- Modifying this function so that caller must free the psz
  4703. return value.
  4704. */
  4705. char *L_ProvParam2Text(DWORD dwParam)
  4706. {
  4707. LPSTR pszProvParamText = NULL;
  4708. if (NULL == (pszProvParamText = (LPSTR) MyAlloc(PROV_PARAM_BUFFER_SIZE)))
  4709. {
  4710. SetLastError(ERROR_NOT_ENOUGH_MEMORY);
  4711. return NULL;
  4712. }
  4713. switch(dwParam)
  4714. {
  4715. case PP_ENUMALGS :
  4716. strcpy(pszProvParamText, "PP_ENUMALGS") ;
  4717. break ;
  4718. case PP_ENUMCONTAINERS :
  4719. strcpy(pszProvParamText, "PP_ENUMCONTAINERS") ;
  4720. break ;
  4721. case PP_IMPTYPE :
  4722. strcpy(pszProvParamText, "PP_IMPTYPE") ;
  4723. break ;
  4724. case PP_NAME :
  4725. strcpy(pszProvParamText, "PP_NAME") ;
  4726. break ;
  4727. case PP_VERSION :
  4728. strcpy(pszProvParamText, "PP_VERSION") ;
  4729. break ;
  4730. case PP_CONTAINER :
  4731. strcpy(pszProvParamText, "PP_CONTAINER") ;
  4732. break ;
  4733. case PP_CHANGE_PASSWORD :
  4734. strcpy(pszProvParamText, "PP_CHANGE_PASSWORD") ;
  4735. break ;
  4736. case PP_KEYSET_SEC_DESCR :
  4737. strcpy(pszProvParamText, "PP_KEYSET_SEC_DESCR") ;
  4738. break ;
  4739. case PP_CERTCHAIN :
  4740. strcpy(pszProvParamText, "PP_CERTCHAIN") ;
  4741. break ;
  4742. case PP_KEY_TYPE_SUBTYPE :
  4743. strcpy(pszProvParamText, "PP_KEY_TYPE_SUBTYPE") ;
  4744. break ;
  4745. case PP_PROVTYPE :
  4746. strcpy(pszProvParamText, "PP_PROVTYPE") ;
  4747. break ;
  4748. case PP_KEYSTORAGE :
  4749. strcpy(pszProvParamText, "PP_KEYSTORAGE") ;
  4750. break ;
  4751. case PP_APPLI_CERT :
  4752. strcpy(pszProvParamText, "PP_APPLI_CERT") ;
  4753. break ;
  4754. case PP_SYM_KEYSIZE :
  4755. strcpy(pszProvParamText, "PP_SYM_KEYSIZE") ;
  4756. break ;
  4757. case PP_SESSION_KEYSIZE :
  4758. strcpy(pszProvParamText, "PP_SESSION_KEYSIZE") ;
  4759. break ;
  4760. case PP_UI_PROMPT :
  4761. strcpy(pszProvParamText, "PP_UI_PROMPT") ;
  4762. break ;
  4763. case PP_ENUMALGS_EX :
  4764. strcpy(pszProvParamText, "PP_ENUMALGS_EX") ;
  4765. break ;
  4766. case PP_ENUMMANDROOTS :
  4767. strcpy(pszProvParamText, "PP_ENUMMANDROOTS") ;
  4768. break ;
  4769. case PP_ENUMELECTROOTS :
  4770. strcpy(pszProvParamText, "PP_ENUMELECTROOTS") ;
  4771. break ;
  4772. case PP_KEYSET_TYPE :
  4773. strcpy(pszProvParamText, "PP_KEYSET_TYPE") ;
  4774. break ;
  4775. case PP_ADMIN_PIN :
  4776. strcpy(pszProvParamText, "PP_ADMIN_PIN") ;
  4777. break ;
  4778. case PP_KEYEXCHANGE_PIN :
  4779. strcpy(pszProvParamText, "PP_KEYEXCHANGE_PIN") ;
  4780. break ;
  4781. case PP_SIGNATURE_PIN :
  4782. strcpy(pszProvParamText, "PP_SIGNATURE_PIN") ;
  4783. break ;
  4784. case PP_SIG_KEYSIZE_INC :
  4785. strcpy(pszProvParamText, "PP_SIG_KEYSIZE_INC") ;
  4786. break ;
  4787. case PP_KEYX_KEYSIZE_INC :
  4788. strcpy(pszProvParamText, "PP_KEYX_KEYSIZE_INC") ;
  4789. break ;
  4790. case PP_UNIQUE_CONTAINER :
  4791. strcpy(pszProvParamText, "PP_UNIQUE_CONTAINER") ;
  4792. break ;
  4793. case PP_SGC_INFO :
  4794. strcpy(pszProvParamText, "PP_SGC_INFO") ;
  4795. break ;
  4796. case PP_USE_HARDWARE_RNG :
  4797. strcpy(pszProvParamText, "PP_USE_HARDWARE_RNG") ;
  4798. break ;
  4799. case PP_KEYSPEC :
  4800. strcpy(pszProvParamText, "PP_KEYSPEC") ;
  4801. break ;
  4802. case PP_ENUMEX_SIGNING_PROT :
  4803. strcpy(pszProvParamText, "PP_ENUMEX_SIGNING_PROT") ;
  4804. break ;
  4805. }
  4806. return pszProvParamText ;
  4807. }
  4808. //+ ==================================================================
  4809. //
  4810. // Function : L_GetProvParam
  4811. // Purpose : Gets the requested Prov Param
  4812. // Does nothing with the ProvParam
  4813. // Has special logic for all enumeration params
  4814. //
  4815. //- ==================================================================
  4816. DWORD L_GetProvParam(HCRYPTPROV hProv, DWORD dwParam, DWORD dwThreadNum)
  4817. {
  4818. PBYTE pbProvData=NULL ;
  4819. DWORD cbProvData=0 ;
  4820. DWORD dwFlags=0 ;
  4821. DWORD dwEnumFlag=0 ;
  4822. char szErrorMsg[256] ;
  4823. DWORD dwRetVal=0 ;
  4824. LPSTR pszProvParamText = NULL;
  4825. DWORD dwError = 0;
  4826. if ((PP_ENUMALGS == dwParam) ||
  4827. (PP_ENUMALGS_EX == dwParam) ||
  4828. (PP_ENUMCONTAINERS == dwParam))
  4829. {
  4830. dwEnumFlag = 1 ;
  4831. dwFlags = CRYPT_FIRST ;
  4832. }
  4833. // dwFlags needs to be set in the case of PP_KEYSET_SECR_DECR
  4834. if (PP_KEYSET_SEC_DESCR == dwParam)
  4835. {
  4836. dwFlags = SACL_SECURITY_INFORMATION ;
  4837. }
  4838. if (!CryptGetProvParam( hProv,
  4839. dwParam,
  4840. NULL,
  4841. &cbProvData,
  4842. dwFlags))
  4843. {
  4844. if ((ERROR_PRIVILEGE_NOT_HELD == (dwError = GetLastError())) &&
  4845. (PP_KEYSET_SEC_DESCR == dwParam))
  4846. {
  4847. // At this point the test has done it's job. The call is an expected failure call
  4848. // so we aren't going to try and make any more.
  4849. // This call with fail with that expected LastError.
  4850. dwRetVal=1 ;
  4851. goto ErrorReturn ;
  4852. }
  4853. else
  4854. {
  4855. pszProvParamText = L_ProvParam2Text(dwParam);
  4856. sprintf(szErrorMsg, "Thread %d: CryptGetProvParam 1 %s error 0x%x",
  4857. dwThreadNum, pszProvParamText, dwError) ;
  4858. MessageBox(NULL, szErrorMsg, ERROR_CAPTION, MB_OK | MB_ICONERROR) ;
  4859. MyFree(pszProvParamText);
  4860. goto ErrorReturn ;
  4861. }
  4862. }
  4863. if (NULL == (pbProvData = (PBYTE) MyAlloc(cbProvData)))
  4864. {
  4865. SetLastError(ERROR_NOT_ENOUGH_MEMORY);
  4866. return 0;
  4867. }
  4868. // If this is an enumeration, keep calling the function until
  4869. // the enumeration reaches the end.
  4870. do
  4871. {
  4872. if (!CryptGetProvParam( hProv,
  4873. dwParam,
  4874. pbProvData,
  4875. &cbProvData,
  4876. dwFlags))
  4877. {
  4878. // Have we reached the end of the enumeration ? If yes, flag it.
  4879. if (ERROR_NO_MORE_ITEMS == (dwError = GetLastError()))
  4880. {
  4881. dwEnumFlag=0 ;
  4882. }
  4883. else
  4884. {
  4885. pszProvParamText = L_ProvParam2Text(dwParam);
  4886. sprintf(szErrorMsg, "Thread %d: CryptGetProvParam 2 %s error 0x%x",
  4887. dwThreadNum, pszProvParamText, dwError) ;
  4888. MessageBox(NULL, szErrorMsg, ERROR_CAPTION, MB_OK | MB_ICONERROR) ;
  4889. MyFree(pszProvParamText);
  4890. goto ErrorReturn ;
  4891. }
  4892. }
  4893. dwFlags=0 ;
  4894. } while (dwEnumFlag) ;
  4895. dwRetVal=1 ;
  4896. ErrorReturn :
  4897. MyFree(pbProvData) ;
  4898. return dwRetVal ;
  4899. }
  4900. //+ =================================================================================
  4901. //- =================================================================================
  4902. /*
  4903. DWORD L_GetAllProvParams(HCRYPTPROV hProv, DWORD dwThreadNum)
  4904. {
  4905. DWORD dwRetVal=0 ;
  4906. EnterCriticalSection(&g_CSEnumParam);
  4907. if (!L_GetProvParam(hProv, PP_ENUMALGS, dwThreadNum))
  4908. {
  4909. LeaveCriticalSection(&g_CSEnumParam);
  4910. goto ErrorReturn;
  4911. }
  4912. LeaveCriticalSection(&g_CSEnumParam);
  4913. if (!L_CheckHeap(dwThreadNum, "after L_GetProvParam PP_ENUMALGS"))
  4914. goto ErrorReturn ;
  4915. EnterCriticalSection(&g_CSEnumParam);
  4916. if (!L_GetProvParam(hProv, PP_ENUMALGS, dwThreadNum))
  4917. {
  4918. LeaveCriticalSection(&g_CSEnumParam);
  4919. goto ErrorReturn ;
  4920. }
  4921. LeaveCriticalSection(&g_CSEnumParam);
  4922. if (!L_CheckHeap(dwThreadNum, "after L_GetProvParam PP_ENUMALGS"))
  4923. goto ErrorReturn ;
  4924. EnterCriticalSection(&g_CSEnumParam);
  4925. if (!L_GetProvParam(hProv, PP_ENUMCONTAINERS, dwThreadNum))
  4926. {
  4927. LeaveCriticalSection(&g_CSEnumParam);
  4928. goto ErrorReturn ;
  4929. }
  4930. LeaveCriticalSection(&g_CSEnumParam);
  4931. if (!L_GetProvParam(hProv, PP_NAME, dwThreadNum))
  4932. goto ErrorReturn ;
  4933. if (!L_CheckHeap(dwThreadNum, "after L_GetProvParam PP_NAME"))
  4934. goto ErrorReturn ;
  4935. if (!L_GetProvParam(hProv, PP_CONTAINER, dwThreadNum))
  4936. goto ErrorReturn ;
  4937. if (!L_CheckHeap(dwThreadNum, "after L_GetProvParam PP_CONTAINER"))
  4938. goto ErrorReturn ;
  4939. if (!L_GetProvParam(hProv, PP_IMPTYPE, dwThreadNum))
  4940. goto ErrorReturn ;
  4941. if (!L_CheckHeap(dwThreadNum, "after L_GetProvParam PP_IMPTYPE"))
  4942. goto ErrorReturn ;
  4943. if (!L_GetProvParam(hProv, PP_VERSION, dwThreadNum))
  4944. goto ErrorReturn ;
  4945. if (!L_CheckHeap(dwThreadNum, "after L_GetProvParam PP_VERSION"))
  4946. goto ErrorReturn ;
  4947. if (!L_GetProvParam(hProv, PP_KEYSET_SEC_DESCR, dwThreadNum))
  4948. goto ErrorReturn ;
  4949. if (!L_CheckHeap(dwThreadNum, "after L_GetProvParam PP_KEYSET_SEC_DESCR"))
  4950. goto ErrorReturn ;
  4951. if (!L_GetProvParam(hProv, PP_UNIQUE_CONTAINER, dwThreadNum))
  4952. goto ErrorReturn ;
  4953. if (!L_CheckHeap(dwThreadNum, "after L_GetProvParam PP_UNIQUE_CONTAINER"))
  4954. goto ErrorReturn ;
  4955. if (!L_GetProvParam(hProv, PP_PROVTYPE, dwThreadNum))
  4956. goto ErrorReturn ;
  4957. if (!L_CheckHeap(dwThreadNum, "after L_GetProvParam PP_PROVTYPE"))
  4958. goto ErrorReturn ;
  4959. if (!L_GetProvParam(hProv, PP_SIG_KEYSIZE_INC, dwThreadNum))
  4960. goto ErrorReturn ;
  4961. if (!L_CheckHeap(dwThreadNum, "after L_GetProvParam PP_SIG_KEYSIZE_INC"))
  4962. goto ErrorReturn ;
  4963. if (!L_GetProvParam(hProv, PP_KEYX_KEYSIZE_INC, dwThreadNum))
  4964. goto ErrorReturn ;
  4965. dwRetVal=1 ;
  4966. ErrorReturn :
  4967. return dwRetVal ;
  4968. }
  4969. */
  4970. //+ ======================================================================
  4971. //- ======================================================================
  4972. DWORD L_ImportAndCheckSessionKeys( HCRYPTPROV hProv,
  4973. HCRYPTKEY hKeyExch,
  4974. PBYTE pbRCx_KeyBlob,
  4975. DWORD cbRCx_KeyBlob,
  4976. PBYTE pbRCx_CipherText,
  4977. DWORD cbRCx_CipherText,
  4978. PBYTE pbPlainText,
  4979. DWORD cbPlainText,
  4980. DWORD dwThreadNum)
  4981. {
  4982. DWORD dwRetVal=0 ;
  4983. HCRYPTKEY hRCxKey=0 ;
  4984. if (!CryptImportKey(hProv,
  4985. pbRCx_KeyBlob,
  4986. cbRCx_KeyBlob,
  4987. hKeyExch,
  4988. 0,
  4989. &hRCxKey))
  4990. L_LastErrorBox("Failed to import session Key", dwThreadNum) ;
  4991. if (!CryptDecrypt(hRCxKey,
  4992. 0,
  4993. TRUE,
  4994. 0,
  4995. pbRCx_CipherText,
  4996. &cbRCx_CipherText))
  4997. L_LastErrorBox("Failed CryptDecrypt", dwThreadNum) ;
  4998. if (memcmp(pbRCx_CipherText, pbPlainText, cbPlainText))
  4999. L_ErrorBox("Ciphertext does not match plaintext after decrypting", dwThreadNum) ;
  5000. //ErrorReturn :
  5001. if (!CryptDestroyKey(hRCxKey))
  5002. L_LastErrorBox("Failed CryptDestroyKey sessionKey", dwThreadNum) ;
  5003. dwRetVal=1 ;
  5004. return dwRetVal ;
  5005. }
  5006. //+ ======================================================================
  5007. //- ======================================================================
  5008. DWORD L_TestContextAddRef(HCRYPTPROV hProv, DWORD dwThreadNum)
  5009. {
  5010. DWORD dwRetVal=0 ;
  5011. DWORD i=0 ;
  5012. DWORD dwCount=50 ;
  5013. for (i=0; i<dwCount ; i++)
  5014. {
  5015. if (!CryptContextAddRef(hProv, NULL, 0))
  5016. {
  5017. L_LastErrorBox("Failed CryptContextAddRef", dwThreadNum) ;
  5018. goto ErrorReturn ;
  5019. }
  5020. }
  5021. for (i=0; i<dwCount ; i++)
  5022. {
  5023. if (!CryptReleaseContext(hProv, 0))
  5024. {
  5025. L_LastErrorBox("Failed CryptReleaseContext (AddRef Test)", dwThreadNum) ;
  5026. goto ErrorReturn ;
  5027. }
  5028. }
  5029. dwRetVal=1 ;
  5030. ErrorReturn :
  5031. return dwRetVal ;
  5032. }
  5033. //+ ===========================================================================
  5034. // L_ExportKey
  5035. //
  5036. // Exports a session key, given the exchange key
  5037. // Mem Allocated here needs to be freed by the calling funtion.
  5038. //- ===========================================================================
  5039. DWORD L_ExportKey( HCRYPTKEY hRC_Key,
  5040. HCRYPTKEY hKeyExch,
  5041. DWORD dwType,
  5042. DWORD dwMustBeZero,
  5043. PBYTE *ppbRC_KeyBlob,
  5044. DWORD *pcbRC_KeyBlob)
  5045. {
  5046. DWORD dwRetVal=0 ;
  5047. char szErrorMsg[256] ;
  5048. if (!CryptExportKey(hRC_Key,
  5049. hKeyExch,
  5050. dwType,
  5051. 0,
  5052. NULL,
  5053. pcbRC_KeyBlob))
  5054. GENERIC_FAIL(CryptExportKey_RC) ;
  5055. if (NULL == (*ppbRC_KeyBlob = (PBYTE) MyAlloc(*pcbRC_KeyBlob)))
  5056. return 0;
  5057. memset(*ppbRC_KeyBlob, 33, *pcbRC_KeyBlob) ;
  5058. if (!CryptExportKey(hRC_Key,
  5059. hKeyExch,
  5060. SIMPLEBLOB,
  5061. 0,
  5062. *ppbRC_KeyBlob,
  5063. pcbRC_KeyBlob))
  5064. GENERIC_FAIL(CryptExportKey_RC) ;
  5065. dwRetVal=1 ;
  5066. ErrorReturn :
  5067. return dwRetVal ;
  5068. }
  5069. //+ ==========================================================================
  5070. //
  5071. //
  5072. //- ==========================================================================
  5073. DWORD L_GetHashParam(HCRYPTHASH hHash, DWORD dwParam)
  5074. {
  5075. DWORD dwRetVal=0 ;
  5076. char szErrorMsg[256] ;
  5077. PBYTE pbData=NULL ;
  5078. DWORD cbData=0 ;
  5079. if (!CryptGetHashParam( hHash,
  5080. dwParam,
  5081. NULL,
  5082. &cbData,
  5083. 0))
  5084. GENERIC_FAIL(CryptGetHashParam) ;
  5085. if (NULL == (pbData = (PBYTE) MyAlloc(cbData)))
  5086. return 0;
  5087. if (!CryptGetHashParam( hHash,
  5088. dwParam,
  5089. pbData,
  5090. &cbData,
  5091. 0))
  5092. GENERIC_FAIL(CryptGetHashParam) ;
  5093. dwRetVal=1 ;
  5094. ErrorReturn :
  5095. MyFree(pbData) ;
  5096. return dwRetVal ;
  5097. }
  5098. //+ ==========================================================================
  5099. //
  5100. //
  5101. //- ==========================================================================
  5102. DWORD L_GetAllHashParams(HCRYPTHASH hHash, DWORD dwThreadNum)
  5103. {
  5104. DWORD dwRetVal=0 ;
  5105. char szErrorMsg[256] ;
  5106. if (!L_GetHashParam(hHash, HP_ALGID))
  5107. goto ErrorReturn ;
  5108. if (!L_GetHashParam(hHash, HP_HASHSIZE))
  5109. goto ErrorReturn ;
  5110. if (!L_GetHashParam(hHash, HP_HASHVAL))
  5111. goto ErrorReturn ;
  5112. dwRetVal=1 ;
  5113. ErrorReturn :
  5114. return dwRetVal ;
  5115. }
  5116. //
  5117. // Function: ThreadAcquireContextTest
  5118. //
  5119. DWORD ThreadAcquireContextTest(PTHREAD_DATA pThreadData, DWORD dwThreadNum)
  5120. {
  5121. HCRYPTPROV hProv = 0;
  5122. HCRYPTKEY hKey = 0;
  5123. DWORD dwError = 0;
  5124. CHAR rgsz[100];
  5125. ZeroMemory(rgsz, sizeof(rgsz));
  5126. sprintf(rgsz, "%s_%d", "ThreadContainer", dwThreadNum);
  5127. if (! CryptAcquireContext(
  5128. &hProv, rgsz, pThreadData->rgszProvName,
  5129. pThreadData->dwProvType, 0))
  5130. {
  5131. dwError = GetLastError();
  5132. printf("CryptAcquireContext: %s, %d, 0x%x\n", rgsz, dwThreadNum, dwError);
  5133. if (NTE_BAD_KEYSET == dwError)
  5134. {
  5135. if (! CryptAcquireContext(
  5136. &hProv, rgsz, pThreadData->rgszProvName,
  5137. pThreadData->dwProvType, CRYPT_NEWKEYSET))
  5138. {
  5139. dwError = GetLastError();
  5140. printf("CryptAcquireContext CRYPT_NEWKEYSET: %s, %d, 0x%x\n", rgsz, dwThreadNum, dwError);
  5141. goto Ret;
  5142. }
  5143. }
  5144. else
  5145. goto Ret;
  5146. }
  5147. if (! CryptGetUserKey(hProv, AT_SIGNATURE, &hKey))
  5148. {
  5149. if (NTE_NO_KEY == (dwError = GetLastError()))
  5150. {
  5151. if (! CryptGenKey(hProv, AT_SIGNATURE, 0, &hKey))
  5152. {
  5153. dwError = GetLastError();
  5154. goto Ret;
  5155. }
  5156. }
  5157. else
  5158. goto Ret;
  5159. }
  5160. if (! CryptDestroyKey(hKey))
  5161. {
  5162. dwError = GetLastError();
  5163. goto Ret;
  5164. }
  5165. if (! CryptReleaseContext(hProv, 0))
  5166. {
  5167. dwError = GetLastError();
  5168. goto Ret;
  5169. }
  5170. dwError = 0;
  5171. Ret:
  5172. return dwError;
  5173. }
  5174. //
  5175. // Function: ThreadHashingTest
  5176. //
  5177. DWORD ThreadHashingTest(PTHREAD_DATA pThreadData, PBYTE pbData, DWORD cbData)
  5178. {
  5179. HCRYPTHASH hHash = 0;
  5180. HCRYPTKEY hKey = 0;
  5181. BYTE rgHash[100];
  5182. DWORD cb = 0;
  5183. DWORD dwError = 0;
  5184. PALGNODE pHashAlg;
  5185. for ( pHashAlg = pThreadData->pAlgList;
  5186. NULL != pHashAlg;
  5187. pHashAlg = pHashAlg->pNext)
  5188. {
  5189. if (CALG_SHA1 != pHashAlg->EnumalgsEx.aiAlgid &&
  5190. CALG_MD5 != pHashAlg->EnumalgsEx.aiAlgid)
  5191. continue;
  5192. if (! CryptCreateHash(pThreadData->hProv, pHashAlg->EnumalgsEx.aiAlgid, 0, 0, &hHash))
  5193. {
  5194. dwError = GetLastError();
  5195. goto Ret;
  5196. }
  5197. if (! CryptHashData(hHash, pbData, cbData, 0))
  5198. {
  5199. dwError = GetLastError();
  5200. goto Ret;
  5201. }
  5202. if (! CryptHashData(hHash, pbData, cbData, 0))
  5203. {
  5204. dwError = GetLastError();
  5205. goto Ret;
  5206. }
  5207. cb = sizeof(rgHash);
  5208. if (! CryptGetHashParam(hHash, HP_HASHVAL, rgHash, &cb, 0))
  5209. {
  5210. dwError = GetLastError();
  5211. goto Ret;
  5212. }
  5213. if (! CryptDestroyHash(hHash))
  5214. {
  5215. dwError = GetLastError();
  5216. goto Ret;
  5217. }
  5218. hHash = 0;
  5219. }
  5220. //dwError = HmacRegression(pThreadData);
  5221. Ret:
  5222. return dwError;
  5223. }
  5224. //
  5225. // Function: ThreadSignatureTest
  5226. //
  5227. DWORD ThreadSignatureTest(PTHREAD_DATA pThreadData)
  5228. {
  5229. HCRYPTHASH hHash = 0;
  5230. PBYTE pbData = NULL;
  5231. DWORD cbData = 0;
  5232. PBYTE pbSignature = NULL;
  5233. DWORD cbSignature = 0;
  5234. DWORD dwError = 0;
  5235. PALGNODE pHashAlg = NULL;
  5236. //
  5237. // CALG_SHA1 Test
  5238. //
  5239. if (! CryptCreateHash(
  5240. pThreadData->hProv, CALG_SHA1, 0, 0, &hHash))
  5241. {
  5242. dwError = GetLastError();
  5243. goto Ret;
  5244. }
  5245. cbData = SIGN_DATA_SIZE;
  5246. if (NULL == (pbData = (PBYTE) MyAlloc(cbData)))
  5247. {
  5248. dwError = ERROR_NOT_ENOUGH_MEMORY;
  5249. goto Ret;
  5250. }
  5251. if (! CryptGenRandom(
  5252. pThreadData->hProv, cbData, pbData))
  5253. {
  5254. dwError = GetLastError();
  5255. goto Ret;
  5256. }
  5257. if (! CryptHashData(
  5258. hHash, pbData, cbData, 0))
  5259. {
  5260. dwError = GetLastError();
  5261. goto Ret;
  5262. }
  5263. if (! CryptSignHash(
  5264. hHash, AT_SIGNATURE, NULL, 0, NULL, &cbSignature))
  5265. {
  5266. dwError = GetLastError();
  5267. goto Ret;
  5268. }
  5269. if (NULL == (pbSignature = (PBYTE) MyAlloc(cbSignature)))
  5270. {
  5271. dwError = ERROR_NOT_ENOUGH_MEMORY;
  5272. goto Ret;
  5273. }
  5274. if (! CryptSignHash(
  5275. hHash, AT_SIGNATURE, NULL, 0, pbSignature, &cbSignature))
  5276. {
  5277. dwError = GetLastError();
  5278. goto Ret;
  5279. }
  5280. if (! CryptVerifySignature(
  5281. hHash, pbSignature, cbSignature,
  5282. pThreadData->hSignatureKey, NULL, 0))
  5283. {
  5284. dwError = GetLastError();
  5285. goto Ret;
  5286. }
  5287. if (! CryptDestroyHash(hHash))
  5288. {
  5289. dwError = GetLastError();
  5290. goto Ret;
  5291. }
  5292. MyFree(pbData);
  5293. pbData = NULL;
  5294. MyFree(pbSignature);
  5295. pbSignature = NULL;
  5296. //
  5297. // CALG_MD2 Test
  5298. //
  5299. if (! CryptCreateHash(
  5300. pThreadData->hProv, CALG_MD2, 0, 0, &hHash))
  5301. {
  5302. dwError = GetLastError();
  5303. goto Ret;
  5304. }
  5305. cbData = SIGN_DATA_SIZE;
  5306. if (NULL == (pbData = (PBYTE) MyAlloc(cbData)))
  5307. {
  5308. dwError = ERROR_NOT_ENOUGH_MEMORY;
  5309. goto Ret;
  5310. }
  5311. if (! CryptGenRandom(
  5312. pThreadData->hProv, cbData, pbData))
  5313. {
  5314. dwError = GetLastError();
  5315. goto Ret;
  5316. }
  5317. if (! CryptHashData(
  5318. hHash, pbData, cbData, 0))
  5319. {
  5320. dwError = GetLastError();
  5321. goto Ret;
  5322. }
  5323. if (! CryptSignHash(
  5324. hHash, AT_SIGNATURE, NULL, 0, NULL, &cbSignature))
  5325. {
  5326. dwError = GetLastError();
  5327. goto Ret;
  5328. }
  5329. if (NULL == (pbSignature = (PBYTE) MyAlloc(cbSignature)))
  5330. {
  5331. dwError = ERROR_NOT_ENOUGH_MEMORY;
  5332. goto Ret;
  5333. }
  5334. if (! CryptSignHash(
  5335. hHash, AT_SIGNATURE, NULL, 0, pbSignature, &cbSignature))
  5336. {
  5337. dwError = GetLastError();
  5338. goto Ret;
  5339. }
  5340. if (! CryptVerifySignature(
  5341. hHash, pbSignature, cbSignature,
  5342. pThreadData->hSignatureKey, NULL, 0))
  5343. {
  5344. dwError = GetLastError();
  5345. goto Ret;
  5346. }
  5347. if (! CryptDestroyHash(hHash))
  5348. {
  5349. dwError = GetLastError();
  5350. goto Ret;
  5351. }
  5352. MyFree(pbData);
  5353. pbData = NULL;
  5354. MyFree(pbSignature);
  5355. pbSignature = NULL;
  5356. //
  5357. // CALG_MD4 Test
  5358. //
  5359. if (! CryptCreateHash(
  5360. pThreadData->hProv, CALG_MD4, 0, 0, &hHash))
  5361. {
  5362. dwError = GetLastError();
  5363. goto Ret;
  5364. }
  5365. cbData = SIGN_DATA_SIZE;
  5366. if (NULL == (pbData = (PBYTE) MyAlloc(cbData)))
  5367. {
  5368. dwError = ERROR_NOT_ENOUGH_MEMORY;
  5369. goto Ret;
  5370. }
  5371. if (! CryptGenRandom(
  5372. pThreadData->hProv, cbData, pbData))
  5373. {
  5374. dwError = GetLastError();
  5375. goto Ret;
  5376. }
  5377. if (! CryptHashData(
  5378. hHash, pbData, cbData, 0))
  5379. {
  5380. dwError = GetLastError();
  5381. goto Ret;
  5382. }
  5383. if (! CryptSignHash(
  5384. hHash, AT_SIGNATURE, NULL, 0, NULL, &cbSignature))
  5385. {
  5386. dwError = GetLastError();
  5387. goto Ret;
  5388. }
  5389. if (NULL == (pbSignature = (PBYTE) MyAlloc(cbSignature)))
  5390. {
  5391. dwError = ERROR_NOT_ENOUGH_MEMORY;
  5392. goto Ret;
  5393. }
  5394. if (! CryptSignHash(
  5395. hHash, AT_SIGNATURE, NULL, 0, pbSignature, &cbSignature))
  5396. {
  5397. dwError = GetLastError();
  5398. goto Ret;
  5399. }
  5400. if (! CryptVerifySignature(
  5401. hHash, pbSignature, cbSignature,
  5402. pThreadData->hSignatureKey, NULL, 0))
  5403. {
  5404. dwError = GetLastError();
  5405. goto Ret;
  5406. }
  5407. if (! CryptDestroyHash(hHash))
  5408. {
  5409. dwError = GetLastError();
  5410. goto Ret;
  5411. }
  5412. MyFree(pbData);
  5413. pbData = NULL;
  5414. MyFree(pbSignature);
  5415. pbSignature = NULL;
  5416. Ret:
  5417. if (pbData)
  5418. MyFree(pbData);
  5419. if (pbSignature)
  5420. MyFree(pbSignature);
  5421. return dwError;
  5422. }
  5423. // ======================================================================================
  5424. // MULTITHREADED routine
  5425. // ======================================================================================
  5426. void WINAPI ThreadRoutine(LPVOID pvThreadData)
  5427. {
  5428. DWORD dwThreadNum = 0;
  5429. DWORD dwError = 0;
  5430. BOOL fMacErrorOccurred = FALSE;
  5431. PTHREAD_DATA pThreadData = (PTHREAD_DATA) pvThreadData;
  5432. CHAR szErrorMsg[256];
  5433. BYTE rgbData[HASH_DATA_SIZE];
  5434. // Get identifier for this thread
  5435. EnterCriticalSection(&pThreadData->CSThreadData);
  5436. dwThreadNum = pThreadData->dwThreadID;
  5437. pThreadData->dwThreadID++;
  5438. LeaveCriticalSection(&pThreadData->CSThreadData);
  5439. if (! CryptGenRandom(pThreadData->hProv, sizeof(rgbData), rgbData))
  5440. {
  5441. dwError = GetLastError();
  5442. goto ErrorReturn;
  5443. }
  5444. do
  5445. {
  5446. if (RUN_THREAD_SIGNATURE_TEST & pThreadData->dwTestsToRun)
  5447. {
  5448. if (ERROR_SUCCESS != (dwError = ThreadSignatureTest(pThreadData)))
  5449. {
  5450. sprintf(
  5451. szErrorMsg,
  5452. "Thread %d: ThreadSignatureTest error 0x%x",
  5453. dwThreadNum,
  5454. dwError);
  5455. MessageBox(NULL, szErrorMsg, ERROR_CAPTION, MB_OK | MB_ICONERROR);
  5456. goto ErrorReturn;
  5457. }
  5458. }
  5459. if (RUN_STRESS_TEST_ALL_ENCRYPTION_ALGS & pThreadData->dwTestsToRun)
  5460. {
  5461. //
  5462. // Call new shared encryption stress tests
  5463. //
  5464. if (ERROR_SUCCESS != (dwError =
  5465. StressTestAllEncryptionAlgs(pThreadData, TRUE, &fMacErrorOccurred)))
  5466. {
  5467. sprintf(
  5468. szErrorMsg,
  5469. "Thread %d: StressTestAllEncryptionAlgs error 0x%x",
  5470. dwThreadNum,
  5471. dwError);
  5472. MessageBox(NULL, szErrorMsg, ERROR_CAPTION, MB_OK | MB_ICONERROR);
  5473. goto ErrorReturn;
  5474. }
  5475. }
  5476. if (RUN_THREAD_HASHING_TEST & pThreadData->dwTestsToRun)
  5477. {
  5478. if (ERROR_SUCCESS != (dwError = ThreadHashingTest(pThreadData, rgbData, sizeof(rgbData))))
  5479. {
  5480. sprintf(
  5481. szErrorMsg,
  5482. "Thread %d: ThreadHashingTest error 0x%x",
  5483. dwThreadNum,
  5484. dwError);
  5485. MessageBox(NULL, szErrorMsg, ERROR_CAPTION, MB_OK | MB_ICONERROR);
  5486. goto ErrorReturn;
  5487. }
  5488. }
  5489. if (RUN_THREAD_ACQUIRE_CONTEXT_TEST & pThreadData->dwTestsToRun)
  5490. {
  5491. if (ERROR_SUCCESS != (dwError = ThreadAcquireContextTest(pThreadData, dwThreadNum)))
  5492. {
  5493. sprintf(
  5494. szErrorMsg,
  5495. "Thread %d: ThreadAcquireContextTest error 0x%x",
  5496. dwThreadNum,
  5497. dwError);
  5498. MessageBox(NULL, szErrorMsg, ERROR_CAPTION, MB_OK | MB_ICONERROR);
  5499. goto ErrorReturn;
  5500. }
  5501. }
  5502. pThreadData->rgdwThreadStatus[dwThreadNum]++;
  5503. }
  5504. while (WAIT_TIMEOUT == WaitForSingleObject(pThreadData->hEndTestEvent, 0));
  5505. ErrorReturn:
  5506. if (fMacErrorOccurred)
  5507. {
  5508. printf("ERROR: Mac bug 189368 is not fixed!\n");
  5509. }
  5510. return;
  5511. }
  5512. //
  5513. // Function: GetNextRegisteredCSP
  5514. //
  5515. DWORD GetNextRegisteredCSP(
  5516. LPSTR pszCsp,
  5517. PDWORD pcbCsp,
  5518. PDWORD pdwProvType,
  5519. DWORD dwRequestedIndex)
  5520. {
  5521. static DWORD dwNextEnumIndex = 0;
  5522. DWORD dwActualIndex = 0;
  5523. DWORD dwError = 0;
  5524. dwActualIndex =
  5525. (ENUMERATE_REGISTERED_CSP == dwRequestedIndex) ?
  5526. dwNextEnumIndex :
  5527. dwRequestedIndex;
  5528. if (! CryptEnumProviders(
  5529. dwActualIndex,
  5530. NULL,
  5531. 0,
  5532. pdwProvType,
  5533. pszCsp,
  5534. pcbCsp))
  5535. {
  5536. dwError = GetLastError();
  5537. switch (dwError)
  5538. {
  5539. case ERROR_NO_MORE_ITEMS:
  5540. dwNextEnumIndex = 0;
  5541. break;
  5542. }
  5543. }
  5544. else
  5545. {
  5546. if (ENUMERATE_REGISTERED_CSP == dwRequestedIndex)
  5547. {
  5548. dwNextEnumIndex++;
  5549. }
  5550. }
  5551. return dwError;
  5552. }
  5553. //
  5554. // Function: InitializeAlgList
  5555. // Purpose: Create a list of algorithms supported by this CSP
  5556. //
  5557. DWORD InitializeAlgList(PTHREAD_DATA pThreadData)
  5558. {
  5559. PALGNODE pAlgNode = NULL, pPrevNode = NULL;
  5560. DWORD dwError = 0;
  5561. DWORD cbData = sizeof(ALGNODE);
  5562. DWORD dwFlags = CRYPT_FIRST;
  5563. if (NULL == (pThreadData->pAlgList = (PALGNODE) MyAlloc(sizeof(ALGNODE))))
  5564. return ERROR_NOT_ENOUGH_MEMORY;
  5565. pAlgNode = pThreadData->pAlgList;
  5566. while (CryptGetProvParam(
  5567. pThreadData->hProv,
  5568. PP_ENUMALGS_EX,
  5569. (PBYTE) &pAlgNode->EnumalgsEx,
  5570. &cbData,
  5571. dwFlags))
  5572. {
  5573. dwFlags = 0;
  5574. if (NULL == (pAlgNode->pNext = (PALGNODE) MyAlloc(sizeof(ALGNODE))))
  5575. return ERROR_NOT_ENOUGH_MEMORY;
  5576. pPrevNode = pAlgNode;
  5577. pAlgNode = pAlgNode->pNext;
  5578. }
  5579. if (ERROR_NO_MORE_ITEMS != (dwError = GetLastError()))
  5580. return dwError;
  5581. MyFree(pAlgNode);
  5582. pPrevNode->pNext = NULL;
  5583. return ERROR_SUCCESS;
  5584. }
  5585. //
  5586. // Function: RunRegressionTests
  5587. //
  5588. BOOL RunRegressionTests(PTHREAD_DATA pThreadData)
  5589. {
  5590. DWORD dwError = ERROR_SUCCESS;
  5591. unsigned u;
  5592. BOOL fAllPassed = TRUE;
  5593. for (u = 0; u < g_cRegressTests; u++)
  5594. {
  5595. if (pThreadData->dwProvType & g_rgRegressTests[u].dwExclude)
  5596. {
  5597. printf(
  5598. "Skipping %s\n\n", g_rgRegressTests[u].pszDescription);
  5599. continue;
  5600. }
  5601. if (ERROR_SUCCESS !=
  5602. (dwError = (g_rgRegressTests[u].pfTest)(pThreadData)))
  5603. {
  5604. printf(
  5605. "FAIL: %s, 0x%x\n\n",
  5606. g_rgRegressTests[u].pszDescription,
  5607. dwError);
  5608. fAllPassed = FALSE;
  5609. }
  5610. else
  5611. printf("PASS: %s\n\n", g_rgRegressTests[u].pszDescription);
  5612. }
  5613. return fAllPassed;
  5614. }
  5615. //
  5616. // Function: CallCryptAcquireContext
  5617. //
  5618. BOOL CallCryptAcquireContext(
  5619. IN PTHREAD_DATA pThreadData,
  5620. IN LPSTR pszOptions,
  5621. IN LPSTR pszContainer)
  5622. {
  5623. HCRYPTPROV hProv = 0;
  5624. HCRYPTKEY hKey = 0;
  5625. HCRYPTKEY hSigKey = 0;
  5626. HCRYPTKEY hExchKey = 0;
  5627. DWORD dwFlags = 0;
  5628. unsigned uLen = strlen(pszOptions);
  5629. ALG_ID ai = 0;
  5630. DWORD dwKeyFlags = 0;
  5631. BOOL fSuccess = TRUE;
  5632. BYTE rgbKeyBlob[5000];
  5633. DWORD cbKeyBlob = sizeof(rgbKeyBlob);
  5634. DWORD dwErr = 0;
  5635. CHAR rgszCtxFlags[MAX_PATH];
  5636. CHAR rgszKeyFlags[MAX_PATH];
  5637. memset(rgszCtxFlags, 0, sizeof(rgszCtxFlags));
  5638. memset(rgszKeyFlags, 0, sizeof(rgszKeyFlags));
  5639. printf(" pszContainer = %s\n", pszContainer);
  5640. sprintf(rgszCtxFlags, " AcquireContext dwFlags =");
  5641. sprintf(rgszKeyFlags, " GenKey dwFlags =");
  5642. while (uLen)
  5643. {
  5644. switch (pszOptions[uLen - 1])
  5645. {
  5646. case 'l':
  5647. dwFlags |= CRYPT_MACHINE_KEYSET;
  5648. strcat(rgszCtxFlags, " CRYPT_MACHINE_KEYSET");
  5649. break;
  5650. case 'v':
  5651. dwFlags |= CRYPT_VERIFYCONTEXT;
  5652. strcat(rgszCtxFlags, " CRYPT_VERIFYCONTEXT");
  5653. break;
  5654. case 'n':
  5655. dwFlags |= CRYPT_NEWKEYSET;
  5656. strcat(rgszCtxFlags, " CRYPT_NEWKEYSET");
  5657. break;
  5658. case 'd':
  5659. dwFlags |= CRYPT_DELETEKEYSET;
  5660. strcat(rgszCtxFlags, " CRYPT_DELETEKEYSET");
  5661. break;
  5662. case 'q':
  5663. dwFlags |= CRYPT_SILENT;
  5664. strcat(rgszCtxFlags, " CRYPT_SILENT");
  5665. break;
  5666. case 'x':
  5667. ai = AT_KEYEXCHANGE;
  5668. break;
  5669. case 's':
  5670. ai = AT_SIGNATURE;
  5671. break;
  5672. case 'u':
  5673. dwKeyFlags |= CRYPT_USER_PROTECTED;
  5674. strcat(rgszKeyFlags, " CRYPT_USER_PROTECTED");
  5675. break;
  5676. case 'e':
  5677. dwKeyFlags |= CRYPT_EXPORTABLE;
  5678. strcat(rgszKeyFlags, " CRYPT_EXPORTABLE");
  5679. break;
  5680. default:
  5681. printf(" Invalid!\n");
  5682. return FALSE;
  5683. }
  5684. uLen--;
  5685. }
  5686. printf("%s\n", rgszCtxFlags);
  5687. if (CryptAcquireContext(
  5688. &hProv, pszContainer, pThreadData->rgszProvName,
  5689. pThreadData->dwProvType, dwFlags))
  5690. {
  5691. printf("Success\n");
  5692. // See if any keys are already in this container.
  5693. // If they exist, cause the private key to be accessed.
  5694. if (CryptGetUserKey(hProv, AT_SIGNATURE, &hSigKey))
  5695. {
  5696. printf(" Found AT_SIGNATURE key in this container\n");
  5697. cbKeyBlob = sizeof(rgbKeyBlob);
  5698. if (! CryptExportKey(
  5699. hSigKey, 0, PRIVATEKEYBLOB, 0, rgbKeyBlob, &cbKeyBlob))
  5700. {
  5701. printf(" Signature key export failed - 0x%x\n", GetLastError());
  5702. fSuccess = FALSE;
  5703. }
  5704. else
  5705. printf(" Successfully exported existing AT_SIGNATURE key\n");
  5706. if (! CryptDestroyKey(hSigKey))
  5707. {
  5708. printf("ERROR: CryptDestroyKey failed - 0x%x\n", GetLastError());
  5709. fSuccess = FALSE;
  5710. }
  5711. hSigKey = 0;
  5712. }
  5713. else
  5714. {
  5715. if (NTE_NO_KEY == (dwErr = GetLastError()))
  5716. printf(" No AT_SIGNATURE key found in this container\n");
  5717. else
  5718. {
  5719. printf("ERROR: CryptGetUserKey AT_SIGNATURE failed - 0x%x\n", dwErr);
  5720. fSuccess = FALSE;
  5721. }
  5722. }
  5723. if (CryptGetUserKey(hProv, AT_KEYEXCHANGE, &hExchKey))
  5724. {
  5725. printf(" Found AT_KEYEXCHANGE key in this container\n");
  5726. cbKeyBlob = sizeof(rgbKeyBlob);
  5727. if (! CryptExportKey(
  5728. hExchKey, 0, PRIVATEKEYBLOB, 0, rgbKeyBlob, &cbKeyBlob))
  5729. {
  5730. printf(" Key Exchange key export failed - 0x%x\n", GetLastError());
  5731. fSuccess = FALSE;
  5732. }
  5733. else
  5734. printf(" Successfully exported existing AT_KEYEXCHANGE key\n");
  5735. if (! CryptDestroyKey(hExchKey))
  5736. {
  5737. printf("ERROR: CryptDestroyKey failed - 0x%x\n", GetLastError());
  5738. fSuccess = FALSE;
  5739. }
  5740. hExchKey = 0;
  5741. }
  5742. else
  5743. {
  5744. if (NTE_NO_KEY == (dwErr = GetLastError()))
  5745. printf(" No AT_KEYEXCHANGE key found in this container\n");
  5746. else
  5747. {
  5748. printf("ERROR: CryptGetUserKey AT_KEYEXCHANGE failed - 0x%x\n", dwErr);
  5749. fSuccess = FALSE;
  5750. }
  5751. }
  5752. if (0 != ai)
  5753. {
  5754. printf("\nCalling CryptGenKey ...\n");
  5755. if (AT_KEYEXCHANGE == ai)
  5756. printf(" Algid = AT_KEYEXCHANGE\n");
  5757. else
  5758. printf(" Algid = AT_SIGNATURE\n");
  5759. printf("%s\n", rgszKeyFlags);
  5760. if (CryptGenKey(
  5761. hProv, ai, dwKeyFlags, &hKey))
  5762. {
  5763. printf("Success\n");
  5764. if (PROV_DSS == pThreadData->dwProvType ||
  5765. PROV_DSS_DH == pThreadData->dwProvType)
  5766. {
  5767. if (! CryptSetKeyParam(hKey, KP_X, NULL, 0))
  5768. {
  5769. printf("ERROR: CryptSetKeyParam KP_X failed - 0x%x\n", GetLastError());
  5770. fSuccess = FALSE;
  5771. }
  5772. }
  5773. if (dwKeyFlags & CRYPT_EXPORTABLE)
  5774. {
  5775. cbKeyBlob = sizeof(rgbKeyBlob);
  5776. if (! CryptExportKey(
  5777. hKey, 0, PRIVATEKEYBLOB, 0, rgbKeyBlob, &cbKeyBlob))
  5778. {
  5779. printf("ERROR: CryptExportKey PRIVATEKEYBLOB failed - 0x%x\n", GetLastError());
  5780. fSuccess = FALSE;
  5781. }
  5782. else
  5783. printf(" Successfully exported new key\n");
  5784. }
  5785. }
  5786. else
  5787. {
  5788. printf("ERROR: CryptGenKey failed - 0x%x\n", GetLastError());
  5789. fSuccess = FALSE;
  5790. }
  5791. }
  5792. }
  5793. else
  5794. {
  5795. printf("ERROR: CryptAcquireContext failed - 0x%x\n", GetLastError());
  5796. fSuccess = FALSE;
  5797. }
  5798. if (hKey)
  5799. {
  5800. if (! CryptDestroyKey(hKey))
  5801. {
  5802. printf("ERROR: CryptDestroyKey failed - 0x%x\n", GetLastError());
  5803. fSuccess = FALSE;
  5804. }
  5805. }
  5806. if (hProv)
  5807. {
  5808. if ((! dwFlags & CRYPT_DELETEKEYSET) &&
  5809. FALSE == CryptReleaseContext(hProv, 0))
  5810. {
  5811. printf("ERROR: CryptReleaseContext failed - 0x%x\n", GetLastError());
  5812. fSuccess = FALSE;
  5813. }
  5814. }
  5815. return fSuccess;
  5816. }
  5817. //
  5818. // Function: DeleteAllContainers
  5819. //
  5820. BOOL DeleteAllContainers(THREAD_DATA *pThreadData)
  5821. {
  5822. HCRYPTPROV hDefProv = 0;
  5823. HCRYPTPROV hProv = 0;
  5824. CHAR rgszContainer[MAX_PATH];
  5825. CHAR rgszDefCont[MAX_PATH];
  5826. DWORD cbContainer = MAX_PATH;
  5827. DWORD dwFlags = CRYPT_FIRST;
  5828. if (! CryptAcquireContext(
  5829. &hDefProv, NULL, pThreadData->rgszProvName,
  5830. pThreadData->dwProvType, 0))
  5831. {
  5832. printf("CryptAcquireContext default keyset failed - 0x%x\n", GetLastError());
  5833. return FALSE;
  5834. }
  5835. if (! CryptGetProvParam(
  5836. hDefProv, PP_CONTAINER, (PBYTE) rgszDefCont,
  5837. &cbContainer, 0))
  5838. {
  5839. printf("CryptGetProvParam PP_CONTAINER failed - 0x%x\n", GetLastError());
  5840. return FALSE;
  5841. }
  5842. cbContainer = MAX_PATH;
  5843. while (CryptGetProvParam(
  5844. hDefProv, PP_ENUMCONTAINERS, (PBYTE) rgszContainer,
  5845. &cbContainer, dwFlags))
  5846. {
  5847. if (dwFlags)
  5848. dwFlags = 0;
  5849. // If the enumerated container is the same as the default
  5850. // container, skip it for now
  5851. if (0 == strcmp(rgszContainer, rgszDefCont))
  5852. continue;
  5853. printf("\"%s\" - ", rgszContainer);
  5854. if (! CryptAcquireContext(
  5855. &hProv, rgszContainer, pThreadData->rgszProvName,
  5856. pThreadData->dwProvType, CRYPT_DELETEKEYSET))
  5857. printf("CryptAcquireContext CRYPT_DELETEKEYSET failed - 0x%x\n", GetLastError());
  5858. else
  5859. printf("Deleted\n");
  5860. cbContainer = MAX_PATH;
  5861. }
  5862. if (! CryptReleaseContext(hDefProv, 0))
  5863. {
  5864. printf("CryptReleaseContext failed - 0x%x\n", GetLastError());
  5865. return FALSE;
  5866. }
  5867. // Now try to delete default keyset
  5868. printf("\"%s\" - ", rgszDefCont);
  5869. if (! CryptAcquireContext(
  5870. &hProv, rgszDefCont, pThreadData->rgszProvName,
  5871. pThreadData->dwProvType, CRYPT_DELETEKEYSET))
  5872. printf("CryptAcquireContext CRYPT_DELETEKEYSET failed - 0x%x\n", GetLastError());
  5873. else
  5874. printf("Deleted\n");
  5875. return TRUE;
  5876. }
  5877. //*****************************************************
  5878. //
  5879. int _cdecl main(int argc, char * argv[])
  5880. {
  5881. HANDLE rghThread[MAX_THREADS];
  5882. DWORD rgdwThreadID[MAX_THREADS];
  5883. DWORD threadID=0;
  5884. DWORD thread_number=0;
  5885. DWORD dwErr=0;
  5886. DWORD dwArg = 0;
  5887. DWORD cbCspName = 0;
  5888. DWORD dwFreeHandle=0;
  5889. DWORD i = 0 ;
  5890. DWORD tick_StartTime=0;
  5891. char szErrorMsg[256] ;
  5892. THREAD_DATA ThreadData;
  5893. BOOL fInvalidArgs = FALSE;
  5894. PALGNODE pAlgNode = NULL;
  5895. BOOL fRunRegressions = FALSE;
  5896. BOOL fAcquireContext = FALSE;
  5897. LPSTR pszOptions = NULL;
  5898. LPSTR pszContainer = NULL;
  5899. BOOL fDeleteContainers = FALSE;
  5900. // Set high-order bit on dwSpinCount param so that the event used
  5901. // by EnterCriticalSection() will be pre-allocated by
  5902. // InitializeCriticalSectionAndSpinCount()
  5903. DWORD dwSpinCount = 0x8000;
  5904. ZeroMemory(&ThreadData, sizeof(ThreadData));
  5905. __try
  5906. {
  5907. InitializeCriticalSectionAndSpinCount(&ThreadData.CSThreadData, dwSpinCount);
  5908. }
  5909. __except (STATUS_NO_MEMORY == GetExceptionCode() ?
  5910. EXCEPTION_EXECUTE_HANDLER :
  5911. EXCEPTION_CONTINUE_SEARCH )
  5912. {
  5913. printf("InitializeCriticalSectionAndSpinCount failed: STATUS_NO_MEMORY exception\n");
  5914. exit(1);
  5915. }
  5916. // Setting all the defaults
  5917. ThreadData.dwThreadCount = StressGetDefaultThreadCount();
  5918. ThreadData.dwTestsToRun = RUN_ALL_TESTS;
  5919. while (--argc>0)
  5920. {
  5921. if (**++argv == '-')
  5922. {
  5923. switch(argv[0][1])
  5924. {
  5925. case 'n':
  5926. argv++;
  5927. argc--;
  5928. if (0 == argc || 0 == (dwArg = atoi(argv[0])))
  5929. {
  5930. fInvalidArgs = TRUE;
  5931. goto Ret;
  5932. }
  5933. ThreadData.dwThreadCount = dwArg;
  5934. break;
  5935. case 't':
  5936. argv++;
  5937. argc--;
  5938. if (0 == argc || 0 == (dwArg = atoi(argv[0])))
  5939. {
  5940. fInvalidArgs = TRUE;
  5941. goto Ret;
  5942. }
  5943. ThreadData.dwProgramMins = dwArg;
  5944. break;
  5945. case 'c':
  5946. argv++;
  5947. argc--;
  5948. if (0 == argc)
  5949. {
  5950. fInvalidArgs = TRUE;
  5951. goto Ret;
  5952. }
  5953. cbCspName = MAX_PATH;
  5954. dwErr = GetNextRegisteredCSP(
  5955. ThreadData.rgszProvName,
  5956. &cbCspName,
  5957. &ThreadData.dwProvType,
  5958. atoi(*argv));
  5959. if (ERROR_SUCCESS != dwErr)
  5960. {
  5961. fInvalidArgs = TRUE;
  5962. goto Ret;
  5963. }
  5964. break;
  5965. case 'e':
  5966. ThreadData.fEphemeralKeys = TRUE;
  5967. break;
  5968. case 'u':
  5969. ThreadData.fUserProtectedKeys = TRUE;
  5970. break;
  5971. case 'r':
  5972. fRunRegressions = TRUE;
  5973. break;
  5974. case 's':
  5975. ThreadData.fSkipPinAttackTest = TRUE;
  5976. break;
  5977. case '?':
  5978. fInvalidArgs = TRUE;
  5979. goto Ret;
  5980. case 'a':
  5981. fAcquireContext = TRUE;
  5982. pszOptions = argv[0] + 2;
  5983. if (NULL == strchr(pszOptions, 'v'))
  5984. {
  5985. argv++;
  5986. argc--;
  5987. pszContainer = *argv;
  5988. }
  5989. break;
  5990. case 'd':
  5991. fDeleteContainers = TRUE;
  5992. break;
  5993. case 'T':
  5994. argv++;
  5995. argc--;
  5996. if (0 == argc || 0 == (dwArg = atoi(argv[0])))
  5997. {
  5998. fInvalidArgs = TRUE;
  5999. goto Ret;
  6000. }
  6001. ThreadData.dwTestsToRun = dwArg;
  6002. break;
  6003. default:
  6004. fInvalidArgs = TRUE;
  6005. goto Ret;
  6006. }
  6007. }
  6008. }
  6009. //
  6010. // Check arg validity
  6011. //
  6012. if ( 0 != argc ||
  6013. 0 == ThreadData.dwProvType ||
  6014. (ThreadData.fEphemeralKeys && ThreadData.fUserProtectedKeys) ||
  6015. (fRunRegressions && fAcquireContext))
  6016. {
  6017. fInvalidArgs = TRUE;
  6018. goto Ret;
  6019. }
  6020. printf("Provider: %s, Type: %d\n\n", ThreadData.rgszProvName, ThreadData.dwProvType);
  6021. if (fDeleteContainers)
  6022. {
  6023. printf("Deleting all key containers ...\n");
  6024. if (! DeleteAllContainers(&ThreadData))
  6025. exit(1);
  6026. goto Ret;
  6027. }
  6028. if (fAcquireContext)
  6029. {
  6030. printf("Calling CryptAcquireContext ...\n");
  6031. if (! CallCryptAcquireContext(
  6032. &ThreadData, pszOptions, pszContainer))
  6033. exit(1);
  6034. goto Ret;
  6035. }
  6036. if (!ProgramInit(&ThreadData))
  6037. {
  6038. printf("ProgramInit() failed\n");
  6039. exit(1) ;
  6040. }
  6041. //
  6042. // Initialize list of supported algorithms
  6043. //
  6044. if (ERROR_SUCCESS != (dwErr = InitializeAlgList(&ThreadData)))
  6045. {
  6046. printf("InitializeAlgList failed, 0x%x\n", dwErr);
  6047. exit(1);
  6048. }
  6049. if (fRunRegressions)
  6050. {
  6051. printf("Running regression tests ...\n");
  6052. if (! RunRegressionTests(&ThreadData))
  6053. exit(1);
  6054. goto Ret;
  6055. }
  6056. //
  6057. // Summarize user options
  6058. //
  6059. printf("Number of threads: %d\n", ThreadData.dwThreadCount);
  6060. if (ThreadData.dwProgramMins)
  6061. printf(" - Timeout in %d minute(s)\n", ThreadData.dwProgramMins);
  6062. if (ThreadData.fEphemeralKeys)
  6063. printf(" - Using ephemeral keys\n");
  6064. if (ThreadData.fUserProtectedKeys)
  6065. printf(" - Using user-protected keys\n");
  6066. // Create event that can be used by the timer thread to stop
  6067. // the worker threads.
  6068. if ((ThreadData.hEndTestEvent = CreateEvent(NULL, TRUE, FALSE, NULL)) == NULL)
  6069. {
  6070. printf("CreateEvent() failed, 0x%x\n", GetLastError());
  6071. exit(1);
  6072. }
  6073. // Create the threads
  6074. tick_StartTime = GetTickCount() ;
  6075. for (thread_number = 0; thread_number < ThreadData.dwThreadCount; thread_number++)
  6076. {
  6077. if ((rghThread[thread_number] =
  6078. CreateThread(
  6079. NULL,
  6080. 0,
  6081. (LPTHREAD_START_ROUTINE) ThreadRoutine,
  6082. &ThreadData,
  6083. 0,
  6084. &threadID)) != NULL)
  6085. {
  6086. rgdwThreadID[thread_number] = threadID ;
  6087. }
  6088. else
  6089. {
  6090. sprintf(szErrorMsg, "\n\nERROR creating thread number 0x%x. Error 0x%x",
  6091. thread_number, GetLastError()) ;
  6092. MessageBox(NULL, szErrorMsg, ERROR_CAPTION, MB_OK | MB_ICONERROR) ;
  6093. exit(0) ;
  6094. }
  6095. }
  6096. // Spawn PrintThreadStatus
  6097. rghThread[thread_number++] = CreateThread(
  6098. NULL, 0,
  6099. (LPTHREAD_START_ROUTINE)PrintThreadStatus,
  6100. &ThreadData,
  6101. 0, &threadID);
  6102. // Spawn KillProgramTimer (This will shut down all the threads and kill the program)
  6103. rghThread[thread_number++] = CreateThread(
  6104. NULL, 0,
  6105. (LPTHREAD_START_ROUTINE)KillProgramTimer,
  6106. &ThreadData,
  6107. 0, &threadID);
  6108. // Done Creating all threads
  6109. // End multithreading
  6110. dwErr = WaitForMultipleObjects(thread_number, rghThread, TRUE, INFINITE) ;
  6111. if (dwErr == WAIT_FAILED)
  6112. printf("WaitForMultipleObjects() failed, 0x%x\n", GetLastError());
  6113. if (! CryptDestroyKey(ThreadData.hSignatureKey))
  6114. {
  6115. sprintf(szErrorMsg, "FAILED CryptDestroyKey SIG error 0x%x\n", GetLastError()) ;
  6116. MessageBox(NULL, szErrorMsg, ERROR_CAPTION, MB_OK | MB_ICONERROR) ;
  6117. }
  6118. if (ThreadData.hExchangeKey && (! CryptDestroyKey(ThreadData.hExchangeKey)))
  6119. {
  6120. sprintf(szErrorMsg, "FAILED CryptDestroyKey KEYX error 0x%x\n", GetLastError()) ;
  6121. MessageBox(NULL, szErrorMsg, ERROR_CAPTION, MB_OK | MB_ICONERROR) ;
  6122. }
  6123. if (! CryptReleaseContext(ThreadData.hVerifyCtx, 0))
  6124. {
  6125. sprintf(szErrorMsg, "FAILED CryptReleaseContext 1 error 0x%x\n", GetLastError());
  6126. MessageBox(NULL, szErrorMsg, ERROR_CAPTION, MB_OK | MB_ICONERROR);
  6127. }
  6128. if (! CryptReleaseContext(ThreadData.hProv, 0))
  6129. {
  6130. sprintf(szErrorMsg, "FAILED CryptReleaseContext 2 error 0x%x\n", GetLastError()) ;
  6131. MessageBox(NULL, szErrorMsg, ERROR_CAPTION, MB_OK | MB_ICONERROR) ;
  6132. }
  6133. DeleteCriticalSection(&ThreadData.CSThreadData);
  6134. CloseHandle(ThreadData.hEndTestEvent);
  6135. while (thread_number--)
  6136. CloseHandle(rghThread[thread_number]);
  6137. Ret:
  6138. while (ThreadData.pAlgList)
  6139. {
  6140. pAlgNode = ThreadData.pAlgList->pNext;
  6141. MyFree(ThreadData.pAlgList);
  6142. ThreadData.pAlgList = pAlgNode;
  6143. }
  6144. if (fInvalidArgs)
  6145. {
  6146. Usage();
  6147. printf("\nRegistered CSP's:\n");
  6148. cbCspName = MAX_PATH;
  6149. for ( i = 0;
  6150. ERROR_SUCCESS == GetNextRegisteredCSP(
  6151. ThreadData.rgszProvName,
  6152. &cbCspName,
  6153. &ThreadData.dwProvType,
  6154. ENUMERATE_REGISTERED_CSP);
  6155. i++, cbCspName = MAX_PATH)
  6156. {
  6157. printf(" %d: %s, Type %d\n", i, ThreadData.rgszProvName, ThreadData.dwProvType);
  6158. }
  6159. exit(1);
  6160. }
  6161. return 0 ;
  6162. }