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

1218 lines
26 KiB

  1. #undef UNICODE // ## Not Yet
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <ctype.h>
  5. #include <windows.h>
  6. #include <wincrypt.h>
  7. #define NTAG_MULTIPHASE 0x80000000
  8. #define DES_TEST 0x00000008
  9. #define UTILITY_BUF_SIZE 1024
  10. #define UTILITY_BUF_SIZE_2 1050
  11. #define EXPORT_BUFFER_LEN 32768
  12. #define MAXKEYLEN 128
  13. HCRYPTPROV hMe;
  14. CHAR pszMyName[64];
  15. OFSTRUCT ImageInfoBuf;
  16. HFILE hFile;
  17. BYTE buf[UTILITY_BUF_SIZE];
  18. BYTE buf2[UTILITY_BUF_SIZE];
  19. DWORD BLen, BLen2, i;
  20. HCRYPTKEY hKey = 0;
  21. HCRYPTKEY hKey2 = 0;
  22. HCRYPTKEY hMyPubKey;
  23. HCRYPTKEY hClientKey;
  24. WORD wRandomSize;
  25. DWORD dRandom;
  26. BYTE *pTmp;
  27. DWORD count;
  28. BYTE *bRanbuf;
  29. BYTE *bcryptbuf;
  30. DWORD i;
  31. BYTE pData[8];
  32. BYTE pHashData1[50];
  33. DWORD BHashLen1;
  34. BYTE pHashData2[50];
  35. DWORD BHashLen2;
  36. BYTE pHashSignData1[MAXKEYLEN];
  37. DWORD BHashSignLen1;
  38. BYTE pHashSignData2[MAXKEYLEN];
  39. DWORD BHashSignLen2;
  40. HCRYPTHASH hHash = 0;
  41. BOOL Logon(int cArg);
  42. BOOL TEncrypt(HCRYPTKEY hTKey, HCRYPTHASH hTHash, BOOL FinalFlag,
  43. DWORD dwFlags, BYTE *Tbuf, DWORD *pBLen, DWORD pdwBufLen,
  44. int cArg, CHAR *szAlgid, CHAR *szmode);
  45. BOOL TDecrypt(HCRYPTKEY hTKey, HCRYPTHASH hTHash, BOOL FinalFlag,
  46. DWORD dwFlags, BYTE *Tbuf, DWORD *pBLen, int cArg,
  47. CHAR *szAlgid, CHAR *szmode);
  48. BOOL TestRC2(int cArg, CHAR *bbuf, DWORD bsize);
  49. BOOL TestRC4(int cArg, CHAR *bbuf, DWORD bsize);
  50. #ifdef TEST_VERSION
  51. BOOL TestDES(int cArg, CHAR *bbuf, DWORD bsize);
  52. #endif
  53. BOOL TestHash(int cArg, CHAR *bbuf, DWORD bsize);
  54. BOOL TestExchange(int cArg, CHAR *bbuf, DWORD bsize);
  55. BOOL Hash(int cArg, CHAR *bbuf, DWORD bsize, BYTE *pHashOut,
  56. DWORD *pHashLenOut, BYTE *pSigData, DWORD *pdwSigLen, DWORD Algid);
  57. int __cdecl main(int cArg, char *rgszArg[])
  58. {
  59. // Make sure keys don't exist to start
  60. strcpy(pszMyName, "stress");
  61. CryptAcquireContext(&hMe, pszMyName, MS_DEF_PROV, PROV_RSA_FULL,
  62. CRYPT_DELETEKEYSET);
  63. while (TRUE)
  64. {
  65. // Logon to provider
  66. if (Logon(cArg))
  67. exit(0);
  68. pTmp = (BYTE *) &wRandomSize;
  69. while (1)
  70. {
  71. if (RCRYPT_FAILED(CryptGenRandom(hMe, 2, pTmp)))
  72. {
  73. if (cArg > 1)
  74. printf("GenRandom failed = %x\n", GetLastError());
  75. else
  76. printf("FAIL\n");
  77. return(TRUE);
  78. }
  79. if (wRandomSize != 0)
  80. break;
  81. }
  82. dRandom = (DWORD) (wRandomSize + (wRandomSize % 8));
  83. if ((bRanbuf = VirtualAlloc(0, dRandom, MEM_COMMIT |
  84. MEM_RESERVE, PAGE_READWRITE)) == 0)
  85. {
  86. if (cArg > 1)
  87. {
  88. printf("malloc failed = %x\n", GetLastError());
  89. }
  90. else
  91. printf("malloc FAIL\n");
  92. return(TRUE);
  93. }
  94. if (RCRYPT_FAILED(CryptGenRandom(hMe, dRandom, bRanbuf)))
  95. {
  96. if (cArg > 1)
  97. printf("GenRandom failed = %x\n", GetLastError());
  98. else
  99. printf("FAIL\n");
  100. return(TRUE);
  101. }
  102. if ((bcryptbuf = VirtualAlloc(0, dRandom + 8, MEM_COMMIT |
  103. MEM_RESERVE, PAGE_READWRITE)) == 0)
  104. {
  105. if (cArg > 1)
  106. {
  107. printf("malloc failed = %x\n", GetLastError());
  108. }
  109. else
  110. printf("malloc FAIL\n");
  111. return(TRUE);
  112. }
  113. if (cArg > 1)
  114. {
  115. printf("bytes generated %x\n", wRandomSize);
  116. }
  117. memcpy(bcryptbuf, bRanbuf, dRandom);
  118. if (TestRC2(cArg, bcryptbuf, dRandom))
  119. exit(0);
  120. if (TestRC4(cArg, bcryptbuf, dRandom))
  121. exit(0);
  122. #ifdef TEST_VERSION
  123. if (TestDES(cArg, bcryptbuf, dRandom))
  124. exit(0);
  125. #endif
  126. if (TestHash(cArg, bcryptbuf, dRandom))
  127. exit(0);
  128. if (TestExchange(cArg, bcryptbuf, dRandom))
  129. exit(0);
  130. if (VirtualFree(bRanbuf, 0, MEM_RELEASE) != TRUE)
  131. {
  132. if (cArg > 1)
  133. printf("VirtulaFree failed: %x\n", GetLastError());
  134. }
  135. if (VirtualFree(bcryptbuf, 0, MEM_RELEASE) != TRUE)
  136. {
  137. if (cArg > 1)
  138. printf("VirtulaFree failed: %x\n", GetLastError());
  139. }
  140. if (cArg > 1)
  141. printf("CryptReleaseContext ");
  142. if (RCRYPT_FAILED(CryptReleaseContext(hMe, 0)))
  143. {
  144. printf("FAIL Error = %x\n", GetLastError());
  145. return(TRUE);
  146. }
  147. else
  148. if (cArg > 1)
  149. printf("SUCCEED\n");
  150. if (cArg > 1)
  151. printf("CryptAcquireContext Delete ");
  152. strcpy(pszMyName, "stress");
  153. if (RCRYPT_FAILED(CryptAcquireContext(&hMe, pszMyName,
  154. MS_DEF_PROV, PROV_RSA_FULL, CRYPT_DELETEKEYSET)))
  155. {
  156. if (cArg > 1)
  157. printf("FAIL Error = %x\n", GetLastError());
  158. return(FALSE);
  159. }
  160. else
  161. {
  162. if (cArg > 1)
  163. {
  164. printf("SUCCEED\n");
  165. printf("\n");
  166. }
  167. }
  168. }
  169. exit(0);
  170. }
  171. BOOL TestExchange(int cArg, CHAR *bbuf, DWORD bsize)
  172. {
  173. BYTE ExpBuf[EXPORT_BUFFER_LEN];
  174. BYTE SigBuf[UTILITY_BUF_SIZE];
  175. DWORD ExpBufLen, SigBufLen;
  176. HCRYPTKEY hKey2;
  177. HCRYPTHASH hHash;
  178. PUBLICKEYSTRUC *pPubKey;
  179. RSAPUBKEY *pRSAKey;
  180. //
  181. // Generate a RC4 key
  182. //
  183. if (RCRYPT_FAILED(CryptGenKey(hMe, CALG_RC4, CRYPT_EXPORTABLE, &hKey)))
  184. {
  185. if (cArg > 1)
  186. printf("failed = %x\n", GetLastError());
  187. else
  188. printf("CryptGenKey FAIL\n");
  189. return(TRUE);
  190. }
  191. //
  192. // Look for our own exchange key
  193. //
  194. if (RCRYPT_FAILED(CryptGetUserKey(hMe, AT_KEYEXCHANGE, &hKey2)))
  195. {
  196. if (cArg > 1)
  197. printf("cguk failed = %x", GetLastError());
  198. else
  199. printf("CryptGetUserKey FAIL\n");
  200. return (TRUE);
  201. }
  202. //
  203. // Export it in PUBLICKEYBLOB form
  204. //
  205. if (cArg > 1)
  206. printf("CryptExportKey (PKB) ");
  207. ExpBufLen = EXPORT_BUFFER_LEN;
  208. if (RCRYPT_FAILED(CryptExportKey(hKey2, 0, PUBLICKEYBLOB, 0,
  209. ExpBuf, &ExpBufLen)))
  210. {
  211. if (cArg > 1)
  212. printf("failed = %x", GetLastError());
  213. else
  214. printf("CryptExportKey FAIL\n");
  215. return (TRUE);
  216. }
  217. if (cArg > 1)
  218. printf("SUCCEED\n");
  219. CryptDestroyKey(hKey2);
  220. pPubKey = (PUBLICKEYSTRUC *)ExpBuf;
  221. pRSAKey = (RSAPUBKEY *)(ExpBuf + sizeof(PUBLICKEYSTRUC));
  222. if (pPubKey->aiKeyAlg != CALG_RSA_KEYX)
  223. {
  224. printf("Pub key fails check\n");
  225. return(TRUE);
  226. }
  227. if (pRSAKey->pubexp != 0x10001)
  228. {
  229. printf("RSA key fails check\n");
  230. return(TRUE);
  231. }
  232. //
  233. // Import it in PUBLICKEYBLOB form
  234. //
  235. if (cArg > 1)
  236. printf("CryptImportKey (PKB) ");
  237. if (RCRYPT_FAILED(CryptImportKey(hMe, ExpBuf, ExpBufLen, 0, 0, &hKey2)))
  238. {
  239. if (cArg > 1)
  240. printf("failed = %x", GetLastError());
  241. else
  242. printf("CryptImportKey FAIL\n");
  243. return (TRUE);
  244. }
  245. if (cArg > 1)
  246. printf("SUCCEED\n");
  247. //
  248. // Encrypt and Decrypt
  249. //
  250. BLen = bsize;
  251. BLen2 = bsize + 8;
  252. if (TEncrypt(hKey, 0, TRUE, 0, bbuf, &BLen, BLen2, cArg, "RC4", ""))
  253. {
  254. return(TRUE);
  255. }
  256. //
  257. // Export the key in SIMPLEBLOB form
  258. //
  259. if (cArg > 1)
  260. printf("CryptExportKey ");
  261. ExpBufLen = EXPORT_BUFFER_LEN;
  262. if (RCRYPT_FAILED(CryptExportKey(hKey, hKey2, SIMPLEBLOB, 0,
  263. ExpBuf, &ExpBufLen)))
  264. {
  265. if (cArg > 1)
  266. printf("failed = %x", GetLastError());
  267. else
  268. printf("CryptExportKey FAIL\n");
  269. return (TRUE);
  270. }
  271. if (cArg > 1)
  272. printf("SUCCEED\n");
  273. //
  274. // Nuke the old key
  275. //
  276. if (RCRYPT_FAILED(CryptDestroyKey(hKey)))
  277. {
  278. if (cArg > 1)
  279. printf("failed = %x\n", GetLastError());
  280. else
  281. printf("CryptDestroyKey FAIL\n");
  282. return(TRUE);
  283. }
  284. if (RCRYPT_FAILED(CryptDestroyKey(hKey2)))
  285. {
  286. if (cArg > 1)
  287. printf("failed = %x\n", GetLastError());
  288. else
  289. printf("CryptDestroyKey FAIL\n");
  290. return(TRUE);
  291. }
  292. //
  293. // Sign the blob with the key exchange key
  294. //
  295. if (cArg > 1)
  296. printf("CryptCreateHash ");
  297. if (RCRYPT_FAILED(CryptCreateHash(hMe, CALG_SHA, 0, 0, &hHash)))
  298. {
  299. if (cArg > 1)
  300. printf("failed = %x", GetLastError());
  301. else
  302. printf("CryptCreateHash FAIL\n");
  303. return (TRUE);
  304. }
  305. if (cArg > 1)
  306. printf("SUCCEED\n");
  307. if (cArg > 1)
  308. printf("CryptHashData ");
  309. if (RCRYPT_FAILED(CryptHashData(hHash, ExpBuf, ExpBufLen, 0)))
  310. {
  311. if (cArg > 1)
  312. printf("failed = %x", GetLastError());
  313. else
  314. printf("CryptHashData FAIL\n");
  315. return (TRUE);
  316. }
  317. if (cArg > 1)
  318. printf("SUCCEED\n");
  319. if (cArg > 1)
  320. printf("CryptSignHash (KEYX) ");
  321. SigBufLen = UTILITY_BUF_SIZE;
  322. if (RCRYPT_FAILED(CryptSignHash(hHash, AT_KEYEXCHANGE, NULL, 0,
  323. SigBuf, &SigBufLen)))
  324. {
  325. if (cArg > 1)
  326. printf("failed = %x", GetLastError());
  327. else
  328. printf("CryptSignHash FAIL\n");
  329. return (TRUE);
  330. }
  331. if (cArg > 1)
  332. printf("SUCCEED\n");
  333. if (RCRYPT_FAILED(CryptDestroyHash(hHash)))
  334. {
  335. if (cArg > 1)
  336. printf("failed = %x\n", GetLastError());
  337. else
  338. printf("CryptDestroyHash FAIL\n");
  339. return(TRUE);
  340. }
  341. if (cArg > 1)
  342. printf("CryptCreateHash ");
  343. if (RCRYPT_FAILED(CryptCreateHash(hMe, CALG_SHA, 0, 0, &hHash)))
  344. {
  345. if (cArg > 1)
  346. printf("failed = %x", GetLastError());
  347. else
  348. printf("CryptCreateHash FAIL\n");
  349. return (TRUE);
  350. }
  351. if (cArg > 1)
  352. printf("SUCCEED\n");
  353. if (cArg > 1)
  354. printf("CryptHashData ");
  355. if (RCRYPT_FAILED(CryptHashData(hHash, ExpBuf, ExpBufLen, 0)))
  356. {
  357. if (cArg > 1)
  358. printf("failed = %x", GetLastError());
  359. else
  360. printf("CryptHashData FAIL\n");
  361. return (TRUE);
  362. }
  363. if (cArg > 1)
  364. printf("SUCCEED\n");
  365. if (RCRYPT_FAILED(CryptGetUserKey(hMe, AT_KEYEXCHANGE, &hKey2)))
  366. {
  367. if (cArg > 1)
  368. printf("cguk failed = %x", GetLastError());
  369. else
  370. printf("CryptGetUserKey FAIL\n");
  371. return (TRUE);
  372. }
  373. if (cArg > 1)
  374. printf("CryptVerifySignature (KEYX) ");
  375. if (RCRYPT_FAILED(CryptVerifySignature(hHash, SigBuf, SigBufLen, hKey2,
  376. 0, 0)))
  377. {
  378. if (cArg > 1)
  379. printf("failed = %x", GetLastError());
  380. else
  381. printf("CryptVerifySignature FAIL\n");
  382. return (TRUE);
  383. }
  384. if (cArg > 1)
  385. printf("SUCCEED\n");
  386. if (RCRYPT_FAILED(CryptDestroyKey(hKey2)))
  387. {
  388. if (cArg > 1)
  389. printf("failed = %x\n", GetLastError());
  390. else
  391. printf("CryptDestroyKey FAIL\n");
  392. return(TRUE);
  393. }
  394. if (RCRYPT_FAILED(CryptDestroyHash(hHash)))
  395. {
  396. if (cArg > 1)
  397. printf("failed = %x\n", GetLastError());
  398. else
  399. printf("CryptDestroyHash FAIL\n");
  400. return(TRUE);
  401. }
  402. if (RCRYPT_FAILED(CryptGetUserKey(hMe, AT_KEYEXCHANGE, &hKey2)))
  403. {
  404. if (cArg > 1)
  405. printf("cguk failed = %x", GetLastError());
  406. else
  407. printf("CryptGetUserKey FAIL\n");
  408. return (TRUE);
  409. }
  410. if (cArg > 1)
  411. printf("CryptImportKey ");
  412. if (RCRYPT_FAILED(CryptImportKey(hMe, ExpBuf, ExpBufLen, 0, 0,
  413. &hKey)))
  414. {
  415. if (cArg > 1)
  416. printf("failed = %x", GetLastError());
  417. else
  418. printf("CryptImportKey FAIL\n");
  419. return (TRUE);
  420. }
  421. if (cArg > 1)
  422. printf("SUCCEED\n");
  423. if (RCRYPT_FAILED(CryptDestroyKey(hKey2)))
  424. {
  425. if (cArg > 1)
  426. printf("failed = %x\n", GetLastError());
  427. else
  428. printf("CryptDestroyKey FAIL\n");
  429. return(TRUE);
  430. }
  431. if (TDecrypt(hKey, 0, TRUE, 0, bbuf, &BLen, cArg, "RC4", ""))
  432. {
  433. return(TRUE);
  434. }
  435. if (cArg > 1)
  436. printf("Compare data ");
  437. if (memcmp(bbuf, bRanbuf, bsize) != 0)
  438. {
  439. printf("FAIL\n");
  440. return(TRUE);
  441. }
  442. if (cArg > 1)
  443. printf("SUCCEED\n");
  444. if (RCRYPT_FAILED(CryptDestroyKey(hKey)))
  445. {
  446. if (cArg > 1)
  447. printf("failed = %x\n", GetLastError());
  448. else
  449. printf("CryptDestroyKey FAIL\n");
  450. return(TRUE);
  451. }
  452. return(FALSE);
  453. }
  454. BOOL TestRC2(int cArg, CHAR *bbuf, DWORD bsize)
  455. {
  456. //
  457. // Generate a RC2 key
  458. //
  459. if (RCRYPT_FAILED(CryptGenKey(hMe, CALG_RC2, 0, &hKey)))
  460. {
  461. if (cArg > 1)
  462. printf("failed = %x\n", GetLastError());
  463. else
  464. printf("CryptGenKey FAIL\n");
  465. return(TRUE);
  466. }
  467. //
  468. // Encrypt and Decrypt using CBC default mode
  469. //
  470. BLen = bsize;
  471. BLen2 = bsize + 8;
  472. if (TEncrypt(hKey, 0, TRUE, 0, bbuf, &BLen, BLen2, cArg, "RC2", "CBC"))
  473. {
  474. return(TRUE);
  475. }
  476. if (TDecrypt(hKey, 0, TRUE, 0, bbuf, &BLen, cArg, "RC2", "CBC"))
  477. {
  478. return(TRUE);
  479. }
  480. if (cArg > 1)
  481. printf("Compare data ");
  482. if (memcmp(bbuf, bRanbuf, bsize) != 0)
  483. {
  484. printf("FAIL\n");
  485. return(TRUE);
  486. }
  487. if (cArg > 1)
  488. printf("SUCCEED\n");
  489. //
  490. // Change mode to ECB
  491. //
  492. *pData = CRYPT_MODE_ECB;
  493. if (RCRYPT_FAILED(CryptSetKeyParam(hKey, KP_MODE, pData, 0)))
  494. {
  495. if (cArg > 1)
  496. printf("failed = %x\n", GetLastError());
  497. else
  498. printf("CryptSetKeyParam FAIL\n");
  499. return(TRUE);
  500. }
  501. BLen = bsize;
  502. BLen2 = bsize + 8;
  503. if (TEncrypt(hKey, 0, TRUE, 0, bbuf, &BLen, BLen2, cArg, "RC2", "ECB"))
  504. {
  505. return(TRUE);
  506. }
  507. if (TDecrypt(hKey, 0, TRUE, 0, bbuf, &BLen, cArg, "RC2", "ECB"))
  508. {
  509. return(TRUE);
  510. }
  511. if (cArg > 1)
  512. printf("Compare data ");
  513. if (memcmp(bbuf, bRanbuf, bsize) != 0)
  514. {
  515. printf("FAIL\n");
  516. return(TRUE);
  517. }
  518. if (cArg > 1)
  519. printf("SUCCEED\n");
  520. //
  521. // Change mode to CFB
  522. //
  523. *pData = CRYPT_MODE_CFB;
  524. if (RCRYPT_FAILED(CryptSetKeyParam(hKey, KP_MODE, pData, 0)))
  525. {
  526. if (cArg > 1)
  527. printf("failed = %x\n", GetLastError());
  528. else
  529. printf("CryptSetKeyParam FAIL\n");
  530. return(TRUE);
  531. }
  532. BLen = bsize;
  533. BLen2 = bsize + 8;
  534. if (TEncrypt(hKey, 0, TRUE, 0, bbuf, &BLen, BLen2, cArg, "RC2", "CFB"))
  535. {
  536. return(TRUE);
  537. }
  538. if (TDecrypt(hKey, 0, TRUE, 0, bbuf, &BLen, cArg, "RC2", "CFB"))
  539. {
  540. return(TRUE);
  541. }
  542. if (cArg > 1)
  543. printf("Compare data ");
  544. if (memcmp(bbuf, bRanbuf, bsize) != 0)
  545. {
  546. printf("FAIL\n");
  547. return(TRUE);
  548. }
  549. if (cArg > 1)
  550. printf("SUCCEED\n");
  551. if (RCRYPT_FAILED(CryptDestroyKey(hKey)))
  552. {
  553. if (cArg > 1)
  554. printf("failed = %x\n", GetLastError());
  555. else
  556. printf("CryptDestroyKey FAIL\n");
  557. return(TRUE);
  558. }
  559. return(FALSE);
  560. }
  561. BOOL TestRC4(int cArg, CHAR *bbuf, DWORD bsize)
  562. {
  563. //
  564. // Generate a RC4 key
  565. //
  566. if (RCRYPT_FAILED(CryptGenKey(hMe, CALG_RC4, 0, &hKey)))
  567. {
  568. if (cArg > 1)
  569. printf("failed = %x\n", GetLastError());
  570. else
  571. printf("CryptGenKey FAIL\n");
  572. return(TRUE);
  573. }
  574. //
  575. // Encrypt and Decrypt
  576. //
  577. BLen = bsize;
  578. BLen2 = bsize + 8;
  579. if (TEncrypt(hKey, 0, TRUE, 0, bbuf, &BLen, BLen2, cArg, "RC4", ""))
  580. {
  581. return(TRUE);
  582. }
  583. if (TDecrypt(hKey, 0, TRUE, 0, bbuf, &BLen, cArg, "RC4", ""))
  584. {
  585. return(TRUE);
  586. }
  587. if (cArg > 1)
  588. printf("Compare data ");
  589. if (memcmp(bbuf, bRanbuf, bsize) != 0)
  590. {
  591. printf("FAIL\n");
  592. return(TRUE);
  593. }
  594. if (cArg > 1)
  595. printf("SUCCEED\n");
  596. if (RCRYPT_FAILED(CryptDestroyKey(hKey)))
  597. {
  598. if (cArg > 1)
  599. printf("failed = %x\n", GetLastError());
  600. else
  601. printf("CryptDestroyKey FAIL\n");
  602. return(TRUE);
  603. }
  604. return(FALSE);
  605. }
  606. #ifdef TEST_VERSION
  607. BOOL TestDES(int cArg, CHAR *bbuf, DWORD bsize)
  608. {
  609. //
  610. // Generate a DES key
  611. //
  612. if (RCRYPT_FAILED(CryptGenKey(hMe, CALG_DES, 0, &hKey)))
  613. {
  614. if (cArg > 1)
  615. printf("failed = %x\n", GetLastError());
  616. else
  617. printf("CryptGenKey FAIL\n");
  618. return(TRUE);
  619. }
  620. //
  621. // Encrypt and Decrypt using CBC default mode
  622. //
  623. BLen = bsize;
  624. BLen2 = bsize + 8;
  625. if (TEncrypt(hKey, 0, TRUE, 0, bbuf, &BLen, BLen2, cArg, "DES", "CBC"))
  626. {
  627. return(TRUE);
  628. }
  629. if (TDecrypt(hKey, 0, TRUE, 0, bbuf, &BLen, cArg, "DES", "CBC"))
  630. {
  631. return(TRUE);
  632. }
  633. if (cArg > 1)
  634. printf("Compare data ");
  635. if (memcmp(bbuf, bRanbuf, bsize) != 0)
  636. {
  637. printf("FAIL\n");
  638. return(TRUE);
  639. }
  640. if (cArg > 1)
  641. printf("SUCCEED\n");
  642. //
  643. // Change mode to ECB
  644. //
  645. *pData = CRYPT_MODE_ECB;
  646. if (RCRYPT_FAILED(CryptSetKeyParam(hKey, KP_MODE, pData, 0)))
  647. {
  648. if (cArg > 1)
  649. printf("failed = %x\n", GetLastError());
  650. else
  651. printf("CryptSetKeyParam FAIL\n");
  652. return(TRUE);
  653. }
  654. BLen = bsize;
  655. BLen2 = bsize + 8;
  656. if (TEncrypt(hKey, 0, TRUE, 0, bbuf, &BLen, BLen2, cArg, "DES", "ECB"))
  657. {
  658. return(TRUE);
  659. }
  660. if (TDecrypt(hKey, 0, TRUE, 0, bbuf, &BLen, cArg, "DES", "ECB"))
  661. {
  662. return(TRUE);
  663. }
  664. if (cArg > 1)
  665. printf("Compare data ");
  666. if (memcmp(bbuf, bRanbuf, bsize) != 0)
  667. {
  668. printf("FAIL\n");
  669. return(TRUE);
  670. }
  671. if (cArg > 1)
  672. printf("SUCCEED\n");
  673. //
  674. // Change mode to CFB
  675. //
  676. *pData = CRYPT_MODE_CFB;
  677. if (RCRYPT_FAILED(CryptSetKeyParam(hKey, KP_MODE, pData, 0)))
  678. {
  679. if (cArg > 1)
  680. printf("failed = %x\n", GetLastError());
  681. else
  682. printf("CryptSetKeyParam FAIL\n");
  683. return(TRUE);
  684. }
  685. BLen = bsize;
  686. BLen2 = bsize + 8;
  687. if (TEncrypt(hKey, 0, TRUE, 0, bbuf, &BLen, BLen2, cArg, "DES", "CFB"))
  688. {
  689. return(TRUE);
  690. }
  691. if (TDecrypt(hKey, 0, TRUE, 0, bbuf, &BLen, cArg, "DES", "CFB"))
  692. {
  693. return(TRUE);
  694. }
  695. if (cArg > 1)
  696. printf("Compare data ");
  697. if (memcmp(bbuf, bRanbuf, bsize) != 0)
  698. {
  699. printf("FAIL\n");
  700. return(TRUE);
  701. }
  702. if (cArg > 1)
  703. printf("SUCCEED\n");
  704. if (RCRYPT_FAILED(CryptDestroyKey(hKey)))
  705. {
  706. if (cArg > 1)
  707. printf("failed = %x\n", GetLastError());
  708. else
  709. printf("CryptDestroyKey FAIL\n");
  710. return(TRUE);
  711. }
  712. return(FALSE);
  713. }
  714. #endif
  715. BOOL TestHash(int cArg, CHAR *bbuf, DWORD bsize)
  716. {
  717. if (cArg > 1)
  718. printf("Hash, compare with MD4 & MD5 ");
  719. BHashLen1 = 50;
  720. BHashSignLen1 = MAXKEYLEN;
  721. if (Hash(cArg, bbuf, bsize, pHashData1, &BHashLen1, pHashSignData1,
  722. &BHashSignLen1, CALG_MD4))
  723. {
  724. return(TRUE);
  725. }
  726. if (memcmp(bbuf, bRanbuf, bsize) != 0)
  727. {
  728. printf("Data corrupted check 1 in Hash test\n");
  729. return(TRUE);
  730. }
  731. BHashLen2 = 50;
  732. BHashSignLen2 = MAXKEYLEN;
  733. if (Hash(cArg, bbuf, bsize, pHashData2, &BHashLen2, pHashSignData2,
  734. &BHashSignLen2, CALG_MD4))
  735. {
  736. return(TRUE);
  737. }
  738. if (memcmp(bbuf, bRanbuf, bsize) != 0)
  739. {
  740. printf("Data corrupted check 2 in Hash test\n");
  741. return(TRUE);
  742. }
  743. if (BHashLen1 != BHashLen2)
  744. {
  745. printf("Hash lengths don't match MD4 FAIL\n");
  746. return(TRUE);
  747. }
  748. if (memcmp(pHashData1, pHashData2, BHashLen1) != 0)
  749. {
  750. printf("Hash data doesn't compare MD4 FAIL\n");
  751. return(TRUE);
  752. }
  753. if (BHashSignLen1 != BHashSignLen2)
  754. {
  755. printf("Hash signatures lengths don't match MD4 FAIL\n");
  756. return(TRUE);
  757. }
  758. if (memcmp(pHashSignData1, pHashSignData2, BHashLen1) != 0)
  759. {
  760. printf("Hash signature data doesn't compare MD4 FAIL\n");
  761. return(TRUE);
  762. }
  763. BHashLen1 = 50;
  764. BHashSignLen1 = MAXKEYLEN;
  765. if (Hash(cArg, bbuf, bsize, pHashData1, &BHashLen1, pHashSignData1,
  766. &BHashSignLen1, CALG_MD5))
  767. {
  768. return(TRUE);
  769. }
  770. if (memcmp(bbuf, bRanbuf, bsize) != 0)
  771. {
  772. printf("Data corrupted check 3 in Hash test\n");
  773. return(TRUE);
  774. }
  775. BHashLen2 = 50;
  776. BHashSignLen2 = MAXKEYLEN;
  777. if (Hash(cArg, bbuf, bsize, pHashData2, &BHashLen2, pHashSignData2,
  778. &BHashSignLen2, CALG_MD5))
  779. {
  780. return(TRUE);
  781. }
  782. if (memcmp(bbuf, bRanbuf, bsize) != 0)
  783. {
  784. printf("Data corrupted check 4 in Hash test\n");
  785. return(TRUE);
  786. }
  787. if (BHashLen1 != BHashLen2)
  788. {
  789. printf("Hash lengths don't match MD5 FAIL\n");
  790. return(TRUE);
  791. }
  792. if (memcmp(pHashData1, pHashData2, BHashLen1) != 0)
  793. {
  794. printf("Hash data doesn't compare MD5 FAIL\n");
  795. return(TRUE);
  796. }
  797. if (BHashSignLen1 != BHashSignLen2)
  798. {
  799. printf("Hash signatures lengths don't match MD5 FAIL\n");
  800. return(TRUE);
  801. }
  802. if (memcmp(pHashSignData1, pHashSignData2, BHashSignLen1) != 0)
  803. {
  804. printf("Hash signature data doesn't compare MD5 FAIL\n");
  805. return(TRUE);
  806. }
  807. if (cArg > 1)
  808. printf("SUCCEED\n");
  809. return(FALSE);
  810. }
  811. BOOL Hash(int cArg, CHAR *bbuf, DWORD bsize, BYTE *pHashOut,
  812. DWORD *pHashLenOut, BYTE *pSigData, DWORD *pdwSigLen, DWORD Algid)
  813. {
  814. if (RCRYPT_FAILED(CryptCreateHash(hMe, Algid, 0, 0, &hHash)))
  815. {
  816. if (cArg > 1)
  817. printf("CryptCreateHash failed = %x\n", GetLastError());
  818. else
  819. printf("CryptCreateHash FAIL\n");
  820. return(TRUE);
  821. }
  822. if (RCRYPT_FAILED(CryptHashData(hHash, bbuf, bsize, 0)))
  823. {
  824. if (cArg > 1)
  825. printf("CryptUpDataHash failed = %x\n", GetLastError());
  826. else
  827. printf("CryptHashData FAIL\n");
  828. return(TRUE);
  829. }
  830. if (RCRYPT_FAILED(CryptSignHash(hHash, AT_SIGNATURE, 0, 0, pSigData,
  831. pdwSigLen)))
  832. {
  833. if (cArg > 1)
  834. printf("CryptSignHash failed = %x\n", GetLastError());
  835. else
  836. printf("CryptSignHash FAIL\n");
  837. return(TRUE);
  838. }
  839. if (RCRYPT_FAILED(CryptDestroyHash(hHash)))
  840. {
  841. if (cArg > 1)
  842. printf("CryptDestroyHash failed = %x\n", GetLastError());
  843. else
  844. printf("CryptDestroyHash FAIL\n");
  845. return(TRUE);
  846. }
  847. if (RCRYPT_FAILED(CryptCreateHash(hMe, Algid, 0, 0, &hHash)))
  848. {
  849. if (cArg > 1)
  850. printf("CryptCreateHash failed = %x\n", GetLastError());
  851. else
  852. printf("CryptCreateHash FAIL\n");
  853. return(TRUE);
  854. }
  855. if (RCRYPT_FAILED(CryptHashData(hHash, bbuf, bsize, 0)))
  856. {
  857. if (cArg > 1)
  858. printf("CryptUpDataHash failed = %x\n", GetLastError());
  859. else
  860. printf("CryptHashData FAIL\n");
  861. return(TRUE);
  862. }
  863. if (RCRYPT_FAILED(CryptGetUserKey(hMe, AT_SIGNATURE, &hKey2)))
  864. {
  865. if (cArg > 1)
  866. printf("cguk failed = %x", GetLastError());
  867. else
  868. printf("CryptGetUserKey FAIL\n");
  869. return (TRUE);
  870. }
  871. if (RCRYPT_FAILED(CryptVerifySignature(hHash, pSigData, *pdwSigLen,
  872. hKey2, 0, 0)))
  873. {
  874. if (cArg > 1)
  875. printf("CryptVerifySignature failed = %x\n", GetLastError());
  876. else
  877. printf("CryptVerifySignature FAIL\n");
  878. return(TRUE);
  879. }
  880. if (RCRYPT_FAILED(CryptDestroyKey(hKey2)))
  881. {
  882. if (cArg > 1)
  883. printf("CryptDestroyKey failed = %x\n", GetLastError());
  884. else
  885. printf("CryptDestroyKey FAIL\n");
  886. return(TRUE);
  887. }
  888. if (RCRYPT_FAILED(CryptDestroyHash(hHash)))
  889. {
  890. if (cArg > 1)
  891. printf("CryptDestroyHash failed = %x\n", GetLastError());
  892. else
  893. printf("CryptDestroyHash FAIL\n");
  894. return(TRUE);
  895. }
  896. if (RCRYPT_FAILED(CryptCreateHash(hMe, Algid, 0, 0, &hHash)))
  897. {
  898. if (cArg > 1)
  899. printf("CryptCreateHash failed = %x\n", GetLastError());
  900. else
  901. printf("CryptCreateHash FAIL\n");
  902. return(TRUE);
  903. }
  904. if (RCRYPT_FAILED(CryptHashData(hHash, bbuf, bsize, 0)))
  905. {
  906. if (cArg > 1)
  907. printf("CryptUpDataHash failed = %x\n", GetLastError());
  908. else
  909. printf("CryptHashData FAIL\n");
  910. return(TRUE);
  911. }
  912. if (RCRYPT_FAILED(CryptGetHashParam(hHash, HP_HASHVAL, pHashOut,
  913. pHashLenOut, 0)))
  914. {
  915. if (cArg > 1)
  916. printf("CryptGetHashParam failed = %x\n", GetLastError());
  917. else
  918. printf("CryptGetHashParam FAIL\n");
  919. return(TRUE);
  920. }
  921. if (RCRYPT_FAILED(CryptDestroyHash(hHash)))
  922. {
  923. if (cArg > 1)
  924. printf("CryptDestroyHash failed = %x\n", GetLastError());
  925. else
  926. printf("CryptDestroyHash FAIL\n");
  927. return(TRUE);
  928. }
  929. return(FALSE);
  930. }
  931. BOOL Logon(int cArg)
  932. {
  933. strcpy(pszMyName, "stress");
  934. if (cArg > 1)
  935. printf("CryptAcquireContext ");
  936. if (RCRYPT_FAILED(CryptAcquireContext(&hMe, pszMyName,
  937. MS_DEF_PROV, PROV_RSA_FULL, CRYPT_NEWKEYSET)))
  938. {
  939. if (cArg > 1)
  940. printf("FAIL Error = %x\n", GetLastError());
  941. return(TRUE);
  942. }
  943. else
  944. {
  945. if (cArg > 1)
  946. printf("SUCCEED\n");
  947. }
  948. if (cArg > 1)
  949. printf("CryptGenKeys ");
  950. if (RCRYPT_FAILED(CryptGenKey(hMe, CALG_RSA_SIGN,
  951. CRYPT_EXPORTABLE, &hClientKey)))
  952. {
  953. if (cArg > 1)
  954. printf("FAIL Error = %x\n", GetLastError());
  955. return(TRUE);
  956. }
  957. CryptDestroyKey(hClientKey);
  958. if (RCRYPT_FAILED(CryptGenKey(hMe, CALG_RSA_KEYX,
  959. CRYPT_EXPORTABLE, &hClientKey)))
  960. {
  961. if (cArg > 1)
  962. printf("FAIL Error = %x\n", GetLastError());
  963. return(TRUE);
  964. }
  965. if (cArg > 1)
  966. printf("SUCCEED\n");
  967. CryptDestroyKey(hClientKey);
  968. return(FALSE);
  969. }
  970. BOOL TEncrypt(HCRYPTKEY hTKey, HCRYPTHASH hTHash, BOOL FinalFlag,
  971. DWORD dwFlags, BYTE *Tbuf, DWORD *pBLen, DWORD pdwBufLen,
  972. int cArg, CHAR *szAlgid, CHAR *szmode)
  973. {
  974. if (cArg > 1)
  975. printf("CryptEncrypt %s-%s ", szAlgid, szmode);
  976. if (RCRYPT_FAILED(CryptEncrypt(hTKey, hTHash, FinalFlag, dwFlags,
  977. Tbuf, pBLen, pdwBufLen)))
  978. {
  979. if (cArg > 1)
  980. printf("failed = %x\n", GetLastError());
  981. else
  982. printf("CryptEncrypt FAIL\n");
  983. return(TRUE);
  984. }
  985. if (cArg > 1)
  986. printf("SUCCEED\n");
  987. return(FALSE);
  988. }
  989. BOOL TDecrypt(HCRYPTKEY hTKey, HCRYPTHASH hTHash, BOOL FinalFlag,
  990. DWORD dwFlags, BYTE *Tbuf, DWORD *pBLen, int cArg,
  991. CHAR *szAlgid, CHAR *szmode)
  992. {
  993. if (cArg > 1)
  994. printf("CryptDecrypt %s-%s ", szAlgid, szmode);
  995. if (RCRYPT_FAILED(CryptDecrypt(hTKey, hTHash, FinalFlag, dwFlags,
  996. Tbuf, pBLen)))
  997. {
  998. if (cArg > 1)
  999. printf("failed = %x\n", GetLastError());
  1000. else
  1001. printf("CryptDecrypt FAIL\n");
  1002. return(TRUE);
  1003. }
  1004. if (cArg > 1)
  1005. printf("SUCCEED\n");
  1006. return(FALSE);
  1007. }