]>
git.saurik.com Git - apple/security.git/blob - SecurityTests/cspxutils/hashTest/hashTest.c
1 /* Copyright (c) 1998,2003-2006,2008 Apple Inc.
3 * hashTest.c - test CDSA digest functions.
7 * 4 May 2000 Doug Mitchell
9 * 12 May 1998 Doug Mitchell at Apple
13 * text size = {random, from 100 bytes to 100k, in
14 * geometrical steps, i.e. the number of
15 * bytes would be 10^r, where r is random out of
16 * {2,3,4,5,6}, plus a random integer in {0,..99}};
19 * text contents = {random data, random size as specified above};
20 * generate digest in one shot;
21 * generate digest with multiple random-sized updates;
22 * verify digests compare;
23 * for various bytes of text {
25 * generate digest in one shot;
26 * veridy digest is different;
27 * restore corrupted byte;
36 #include <Security/cssm.h>
38 #include <Security/cssm.h>
39 #include <Security/cssmapple.h>
47 #define MIN_EXP 2 /* for data size 10**exp */
48 #define DEFAULT_MAX_EXP 3
50 #define INCR_DEFAULT 0 /* munge every incr bytes - zero means
51 * "adjust per ptext size" */
62 #define ALG_FIRST ALG_MD2
63 #define ALG_LAST ALG_SHA512
64 #define MAX_DATA_SIZE (100000 + 100) /* bytes */
65 #define LOOP_NOTIFY 20
67 static void usage(char **argv
)
69 printf("usage: %s [options]\n", argv
[0]);
70 printf(" Options:\n");
71 printf(" a=algorithm (s=SHA1; m=MD5; M=MD2; 4=SHA224; 2=SHA256; 3=SHA384; 5=SHA512; "
73 printf(" l=loops (default=%d; 0=forever)\n", LOOPS_DEF
);
74 printf(" n=minExp (default=%d)\n", MIN_EXP
);
75 printf(" x=maxExp (default=%d, max=%d)\n", DEFAULT_MAX_EXP
, MAX_EXP
);
76 printf(" i=increment (default=%d)\n", INCR_DEFAULT
);
77 printf(" p=pauseInterval (default=0, no pause)\n");
78 printf(" z (zero data)\n");
79 printf(" I (incrementing data)\n");
80 printf(" g (good digest only)\n");
81 printf(" D (CSP/DL; default = bare CSP)\n");
82 printf(" v(erbose)\n");
90 static int doTest(CSSM_CSP_HANDLE cspHand
,
95 CSSM_BOOL mallocDigest
,
99 CSSM_DATA refDigest
= {0, NULL
};
100 CSSM_DATA testDigest
= {0, NULL
};
104 unsigned char origData
;
111 * generate digest in one shot;
112 * generate digest with multiple random-sized updates;
113 * verify digests compare;
114 * for various bytes of ptext {
115 * corrupt ptext byte;
116 * generate digest in one shot;
117 * verify digest is different;
118 * restore corrupted byte;
121 crtn
= cspDigest(cspHand
,
127 rtn
= testError(quiet
);
130 crtn
= cspStagedDigest(cspHand
,
133 CSSM_TRUE
, // multi updates
137 rtn
= testError(quiet
);
140 if(refDigest
.Length
!= testDigest
.Length
) {
141 printf("Digest length mismatch (1)\n");
142 rtn
= testError(quiet
);
145 if(memcmp(refDigest
.Data
, testDigest
.Data
, refDigest
.Length
)) {
146 printf("Digest miscompare (1)\n");
147 rtn
= testError(quiet
);
156 appFreeCssmData(&testDigest
, CSSM_FALSE
);
157 testDigest
.Length
= 0;
158 data
= (unsigned char *)ptext
->Data
;
159 length
= ptext
->Length
;
160 for(byte
=0; byte
<length
; byte
+= incr
) {
161 if(verbose
&& ((loop
++ % LOG_FREQ
) == 0)) {
162 printf("....byte %d\n", byte
);
164 origData
= data
[byte
];
166 * Generate random non-zero byte
169 bits
= genRand(1, 0xff) & 0xff;
172 crtn
= cspDigest(cspHand
,
178 rtn
= testError(quiet
);
181 if(!memcmp(refDigest
.Data
, testDigest
.Data
, refDigest
.Length
)) {
182 printf("Unexpected digest compare\n");
183 rtn
= testError(quiet
);
186 appFreeCssmData(&testDigest
, CSSM_FALSE
);
187 testDigest
.Length
= 0;
188 data
[byte
] = origData
;
192 if(refDigest
.Length
) {
193 appFreeCssmData(&refDigest
, CSSM_FALSE
);
195 if(testDigest
.Length
) {
196 appFreeCssmData(&testDigest
, CSSM_FALSE
);
201 int main(int argc
, char **argv
)
207 CSSM_CSP_HANDLE cspHand
;
208 CSSM_BOOL mallocDigest
;
210 uint32 alg
; // ALG_MD5, etc.
211 uint32 cssmAlg
; // CSSM_ALGID_MD5, etc.
218 unsigned loops
= LOOPS_DEF
;
219 CSSM_BOOL verbose
= CSSM_FALSE
;
220 unsigned minExp
= MIN_EXP
;
221 unsigned maxExp
= DEFAULT_MAX_EXP
;
222 CSSM_BOOL quiet
= CSSM_FALSE
;
223 unsigned incr
= INCR_DEFAULT
;
224 unsigned minAlg
= ALG_FIRST
;
225 unsigned maxAlg
= ALG_LAST
;
226 unsigned pauseInterval
= 0;
228 CSSM_BOOL goodOnly
= CSSM_FALSE
;
229 CSSM_BOOL bareCsp
= CSSM_TRUE
;
231 for(arg
=1; arg
<argc
; arg
++) {
240 minAlg
= maxAlg
= ALG_SHA1
;
243 minAlg
= maxAlg
= ALG_MD5
;
246 minAlg
= maxAlg
= ALG_MD2
;
249 minAlg
= maxAlg
= ALG_SHA224
;
252 minAlg
= maxAlg
= ALG_SHA256
;
255 minAlg
= maxAlg
= ALG_SHA384
;
258 minAlg
= maxAlg
= ALG_SHA512
;
265 loops
= atoi(&argp
[2]);
268 minExp
= atoi(&argp
[2]);
271 maxExp
= atoi(&argp
[2]);
272 if(maxExp
> MAX_EXP
) {
277 incr
= atoi(&argp
[2]);
280 pauseInterval
= atoi(&argp
[2]);;
289 bareCsp
= CSSM_FALSE
;
298 goodOnly
= CSSM_TRUE
;
305 ptext
.Data
= (uint8
*)CSSM_MALLOC(MAX_DATA_SIZE
);
306 /* length set in test loop */
307 if(ptext
.Data
== NULL
) {
308 printf("Insufficient heap\n");
312 printf("Starting hashTest; args: ");
313 for(i
=1; i
<argc
; i
++) {
314 printf("%s ", argv
[i
]);
317 cspHand
= cspDlDbStartup(bareCsp
, NULL
);
322 for(alg
=minAlg
; alg
<=maxAlg
; alg
++) {
326 cssmAlg
= CSSM_ALGID_MD5
;
330 cssmAlg
= CSSM_ALGID_MD2
;
334 cssmAlg
= CSSM_ALGID_SHA1
;
338 cssmAlg
= CSSM_ALGID_SHA224
;
342 cssmAlg
= CSSM_ALGID_SHA256
;
346 cssmAlg
= CSSM_ALGID_SHA384
;
350 cssmAlg
= CSSM_ALGID_SHA512
;
354 printf("Testing alg %s\n", algStr
);
356 for(loop
=1; ; loop
++) {
357 ptext
.Length
= genData(ptext
.Data
, minExp
, maxExp
, dt
);
359 /* adjust increment as appropriate */
360 actualIncr
= (ptext
.Length
/ 50) + 1;
365 /* mix up mallocing */
366 mallocDigest
= (loop
& 1) ? CSSM_TRUE
: CSSM_FALSE
;
368 if(verbose
|| ((loop
% LOOP_NOTIFY
) == 0)) {
369 printf("..loop %d text size %lu mallocDigest %d\n",
370 loop
, (unsigned long)ptext
.Length
, (int)mallocDigest
);
383 if(loops
&& (loop
== loops
)) {
386 if(pauseInterval
&& ((loop
% pauseInterval
) == 0)) {
388 printf("Hit CR to proceed: ");
394 printf("%s test complete\n", argv
[0]);