]>
git.saurik.com Git - apple/security.git/blob - SecurityTests/cspxutils/hashClone/hashClone.c
2 * hashClone.c - test CSSM_DigestDataClone function
8 #include <Security/cssm.h>
9 #include <Security/cssmapple.h>
17 #define MAX_PTEXT (8 * 1024)
19 #define LOOP_NOTIFY 20
24 typedef unsigned privAlg
;
35 #define ALG_FIRST ALG_MD5
36 #define ALG_LAST ALG_SHA512
38 static void usage(char **argv
)
40 printf("usage: %s [options]\n", argv
[0]);
41 printf(" Options:\n");
42 printf(" a=algorithm (s=SHA1; m=MD5; M=MD2; 4=SHA224; 2=SHA256; 3=SHA384; 5=SHA512; "
44 printf(" l=loops (default=%d; 0=forever)\n", LOOPS_DEF
);
45 printf(" p=pauseInterval (default=0, no pause)\n");
46 printf(" D (CSP/DL; default = bare CSP)\n");
47 printf(" v(erbose)\n");
53 static int doTest(CSSM_CSP_HANDLE cspHand
,
60 CSSM_CC_HANDLE digHand1
= 0; // reference
61 CSSM_CC_HANDLE digHand2
= 0; // to be cloned
62 CSSM_CC_HANDLE digHand3
= 0; // cloned from digHand2
63 CSSM_DATA dig1
= {0, NULL
};
64 CSSM_DATA dig2
= {0, NULL
};
65 CSSM_DATA dig3
= {0, NULL
};
67 unsigned thisMove
; // this update
68 unsigned toMove
; // total to go
69 unsigned totalRequest
; // originally requested
70 CSSM_DATA thisText
; // actually passed to update
72 /* cook up two digest contexts */
73 crtn
= CSSM_CSP_CreateDigestContext(cspHand
,
77 printError("CSSM_CSP_CreateDigestContext (1)", crtn
);
78 return testError(quiet
);
80 crtn
= CSSM_CSP_CreateDigestContext(cspHand
,
84 printError("CSSM_CSP_CreateDigestContext (2)", crtn
);
85 return testError(quiet
);
87 crtn
= CSSM_DigestDataInit(digHand1
);
89 printError("CSSM_DigestDataInit (1)", crtn
);
90 return testError(quiet
);
92 crtn
= CSSM_DigestDataInit(digHand2
);
94 printError("CSSM_DigestDataInit (2)", crtn
);
95 return testError(quiet
);
98 /* do some random updates to first two digests, until we've digested
99 * at least half of the requested data */
100 totalRequest
= ptext
->Length
;
101 toMove
= ptext
->Length
;
102 thisText
.Data
= ptext
->Data
;
103 while(toMove
> (totalRequest
/ 2)) {
104 thisMove
= genRand((MIN_PTEXT
/ 2), toMove
);
105 thisText
.Length
= thisMove
;
107 printf(" ..updating digest1, digest2 with %d bytes\n", thisMove
);
109 crtn
= CSSM_DigestDataUpdate(digHand1
, &thisText
, 1);
111 printError("CSSM_DigestDataUpdate (1)", crtn
);
112 return testError(quiet
);
114 crtn
= CSSM_DigestDataUpdate(digHand2
, &thisText
, 1);
116 printError("CSSM_DigestDataUpdate (2)", crtn
);
117 return testError(quiet
);
119 thisText
.Data
+= thisMove
;
123 /* digest3 := clone(digest2) */
124 crtn
= CSSM_DigestDataClone(digHand2
, &digHand3
);
126 printError("CSSM_DigestDataClone", crtn
);
127 return testError(quiet
);
130 /* finish off remaining ptext, updating all 3 digests identically */
132 thisMove
= genRand(1, toMove
);
133 thisText
.Length
= thisMove
;
135 printf(" ..updating all three digests with %d bytes\n", thisMove
);
137 crtn
= CSSM_DigestDataUpdate(digHand1
, &thisText
, 1);
139 printError("CSSM_DigestDataUpdate (3)", crtn
);
140 return testError(quiet
);
142 crtn
= CSSM_DigestDataUpdate(digHand2
, &thisText
, 1);
144 printError("CSSM_DigestDataUpdate (4)", crtn
);
145 return testError(quiet
);
147 crtn
= CSSM_DigestDataUpdate(digHand3
, &thisText
, 1);
149 printError("CSSM_DigestDataUpdate (5)", crtn
);
150 return testError(quiet
);
152 thisText
.Data
+= thisMove
;
156 /* obtain all three digests */
157 crtn
= CSSM_DigestDataFinal(digHand1
, &dig1
);
159 printError("CSSM_DigestDataFinal (1)", crtn
);
160 return testError(quiet
);
162 crtn
= CSSM_DigestDataFinal(digHand2
, &dig2
);
164 printError("CSSM_DigestDataFinal (2)", crtn
);
165 return testError(quiet
);
167 crtn
= CSSM_DigestDataFinal(digHand3
, &dig3
);
169 printError("CSSM_DigestDataFinal (3)", crtn
);
170 return testError(quiet
);
173 /* ensure all three digests identical */
174 if(!appCompareCssmData(&dig1
, &dig2
)) {
175 printf("***Digest miscompare(dig1, dig2)***\n");
176 if(testError(quiet
)) {
180 if(!appCompareCssmData(&dig2
, &dig3
)) {
181 printf("***Digest miscompare(dig2, dig3)***\n");
182 if(testError(quiet
)) {
188 appFreeCssmData(&dig1
, CSSM_FALSE
);
189 appFreeCssmData(&dig2
, CSSM_FALSE
);
190 appFreeCssmData(&dig3
, CSSM_FALSE
);
191 CSSM_DeleteContext(digHand1
);
192 CSSM_DeleteContext(digHand2
);
193 CSSM_DeleteContext(digHand3
);
197 int main(int argc
, char **argv
)
203 CSSM_CSP_HANDLE cspHand
;
205 privAlg alg
; // ALG_MD5, etc.
206 CSSM_ALGORITHMS cssmAlg
; // CSSM_ALGID_MD5, etc.
212 unsigned loops
= LOOPS_DEF
;
213 CSSM_BOOL verbose
= CSSM_FALSE
;
214 CSSM_BOOL quiet
= CSSM_FALSE
;
215 unsigned minAlg
= ALG_FIRST
;
216 unsigned maxAlg
= ALG_LAST
;
217 unsigned pauseInterval
= 0;
218 CSSM_BOOL bareCsp
= CSSM_TRUE
;
220 for(arg
=1; arg
<argc
; arg
++) {
229 minAlg
= maxAlg
= ALG_SHA1
;
232 minAlg
= maxAlg
= ALG_MD5
;
235 minAlg
= maxAlg
= ALG_MD2
;
238 minAlg
= maxAlg
= ALG_SHA224
;
241 minAlg
= maxAlg
= ALG_SHA256
;
244 minAlg
= maxAlg
= ALG_SHA384
;
247 minAlg
= maxAlg
= ALG_SHA512
;
254 loops
= atoi(&argp
[2]);
257 pauseInterval
= atoi(&argp
[2]);;
266 bareCsp
= CSSM_FALSE
;
273 ptext
.Data
= (uint8
*)CSSM_MALLOC(MAX_PTEXT
);
274 /* length set in test loop */
275 if(ptext
.Data
== NULL
) {
276 printf("Insufficient heap\n");
280 printf("Starting hashClone; args: ");
281 for(j
=1; j
<argc
; j
++) {
282 printf("%s ", argv
[j
]);
285 cspHand
= cspDlDbStartup(bareCsp
, NULL
);
290 for(alg
=minAlg
; alg
<=maxAlg
; alg
++) {
294 cssmAlg
= CSSM_ALGID_MD5
;
298 cssmAlg
= CSSM_ALGID_MD2
;
302 cssmAlg
= CSSM_ALGID_SHA1
;
306 cssmAlg
= CSSM_ALGID_SHA224
;
310 cssmAlg
= CSSM_ALGID_SHA256
;
314 cssmAlg
= CSSM_ALGID_SHA384
;
318 cssmAlg
= CSSM_ALGID_SHA512
;
322 printf("Testing alg %s\n", algStr
);
324 for(loop
=1; ; loop
++) {
325 simpleGenData(&ptext
, MIN_PTEXT
, MAX_PTEXT
);
327 if(verbose
|| ((loop
% LOOP_NOTIFY
) == 0)) {
328 printf("..loop %d text size %lu\n", loop
, ptext
.Length
);
339 if(loops
&& (loop
== loops
)) {
342 if(pauseInterval
&& ((loop
% pauseInterval
) == 0)) {
344 printf("Hit CR to proceed: ");
349 cspShutdown(cspHand
, bareCsp
);
351 printf("%s test complete\n", argv
[0]);