]> git.saurik.com Git - apple/security.git/blob - SecurityTests/clxutils/threadTest/symTestThr.cpp
Security-57031.10.10.tar.gz
[apple/security.git] / SecurityTests / clxutils / threadTest / symTestThr.cpp
1 /*
2 * Simple symmetric encrypt/decrypt test
3 */
4 #include "testParams.h"
5 #include <Security/cssm.h>
6 #include <stdlib.h>
7 #include <stdio.h>
8 #include <time.h>
9 #include <security_cdsa_utils/cuFileIo.h>
10 #include <utilLib/common.h>
11 #include <utilLib/cspwrap.h>
12 #include <strings.h>
13
14 /* for memory leak debug only, with only one thread running */
15 #define DO_PAUSE 0
16
17 #define PTEXT_SIZE 1024
18 #define USAGE_DEF "noUsage"
19
20 int symTestInit(TestParams *testParams)
21 {
22 /* nothing for now */
23 return 0;
24 }
25
26 int symTest(TestParams *testParams)
27 {
28 unsigned loop;
29 CSSM_RETURN crtn;
30 CSSM_DATA ptext = {0, NULL};
31 CSSM_DATA ctext = {0, NULL};
32 CSSM_DATA rptext = {0, NULL};
33 CSSM_KEY_PTR symKey;
34 CSSM_PADDING padding;
35 CSSM_ALGORITHMS keyAlg;
36 CSSM_ALGORITHMS encrAlg;
37 CSSM_ENCRYPT_MODE encrMode;
38 CSSM_BOOL keyIsRef;
39 CSSM_DATA initVector;
40
41 initVector.Data = (uint8 *)"someStrangeInitVect";
42 ptext.Data = (uint8 *)CSSM_MALLOC(PTEXT_SIZE);
43 ptext.Length = PTEXT_SIZE;
44
45 for(loop=0; loop<testParams->numLoops; loop++) {
46 if(testParams->verbose) {
47 printf("symTest thread %d: loop %d\n",
48 testParams->threadNum, loop);
49 }
50 else if(!testParams->quiet) {
51 printChar(testParams->progressChar);
52 }
53
54 /* random plaintext */
55 crtn = threadGetRandData(testParams, &ptext, PTEXT_SIZE);
56 if(crtn) {
57 return 1;
58 }
59
60 /* pick algorithm and params */
61 if(loop & 1) {
62 keyAlg = CSSM_ALGID_DES;
63 encrAlg = CSSM_ALGID_DES;
64 encrMode = CSSM_ALGMODE_CBCPadIV8;
65 padding = CSSM_PADDING_PKCS1;
66 initVector.Length = 8;
67 }
68 else {
69 keyAlg = CSSM_ALGID_AES;
70 encrAlg = CSSM_ALGID_AES;
71 encrMode = CSSM_ALGMODE_CBCPadIV8;
72 padding = CSSM_PADDING_PKCS5;
73 initVector.Length = 16;
74 }
75 if(loop & 2) {
76 keyIsRef = CSSM_TRUE;
77 }
78 else {
79 keyIsRef = CSSM_FALSE;
80 }
81
82 /* cook up a key */
83 symKey = cspGenSymKey(testParams->cspHand,
84 keyAlg,
85 USAGE_DEF,
86 strlen(USAGE_DEF),
87 CSSM_KEYUSE_ENCRYPT | CSSM_KEYUSE_DECRYPT,
88 CSP_KEY_SIZE_DEFAULT,
89 keyIsRef);
90 if(symKey == NULL) {
91 return 1;
92 }
93
94 /* encrypt, decrypt */
95 crtn = cspEncrypt(testParams->cspHand,
96 encrAlg,
97 encrMode,
98 padding,
99 symKey,
100 NULL, // second key unused
101 0, // efectiveKeySizeInBits,
102 0, // rounds
103 &initVector,
104 &ptext,
105 &ctext,
106 CSSM_TRUE); // mallocCtext
107 if(crtn) {
108 return 1;
109 }
110 crtn = cspDecrypt(testParams->cspHand,
111 encrAlg,
112 encrMode,
113 padding,
114 symKey,
115 NULL, // second key unused
116 0, // efectiveKeySizeInBits
117 0, // rounds
118 &initVector,
119 &ctext,
120 &rptext,
121 CSSM_TRUE); // mallocCtext
122 if(crtn) {
123 return 1;
124 }
125
126 /* compare */
127 if(ptext.Length != rptext.Length) {
128 printf("Ptext length mismatch: expect %lu, got %lu\n",
129 ptext.Length, rptext.Length);
130 return 1;
131 }
132 if(memcmp(ptext.Data, rptext.Data, ptext.Length)) {
133 printf("***data miscompare\n");
134 return 1;
135 }
136
137 /* free everything */
138 appFreeCssmData(&ctext, CSSM_FALSE);
139 appFreeCssmData(&rptext, CSSM_FALSE);
140 cspFreeKey(testParams->cspHand, symKey);
141 CSSM_FREE(symKey);
142 #if DO_PAUSE
143 fpurge(stdin);
144 printf("Hit CR to proceed: ");
145 getchar();
146 #endif
147 }
148 return 0;
149 }
150