]>
git.saurik.com Git - apple/security.git/blob - SecurityTests/cspxutils/hashCompat/hashCompat.c
2 * hashCompat.c - test compatibilty of two different implementations of a
3 * various digest algorithms - one in the standard AppleCSP, one in BSAFE.
5 * Written by Doug Mitchell.
11 #include <Security/cssm.h>
12 #include <Security/cssmapple.h>
15 #include "bsafeUtils.h"
22 #define MIN_EXP 2 /* for data size 10**exp */
23 #define DEFAULT_MAX_EXP 4
26 #define MAX_DATA_SIZE (100000 + 100) /* bytes */
27 #define LOOP_NOTIFY 20
30 * Enumerate algs our own way to allow iteration.
38 #define ALG_FIRST ALG_SHA1
39 #define ALG_LAST ALG_MD2
41 static void usage(char **argv
)
43 printf("usage: %s [options]\n", argv
[0]);
44 printf(" Options:\n");
45 printf(" a=algorithm (s=SHA1; 5=MD5; 2=MD2; default=all\n");
46 printf(" l=loops (default=%d; 0=forever)\n", LOOPS_DEF
);
47 printf(" n=minExp (default=%d)\n", MIN_EXP
);
48 printf(" x=maxExp (default=%d, max=%d)\n", DEFAULT_MAX_EXP
, MAX_EXP
);
49 printf(" p=pauseInterval (default=0, no pause)\n");
50 printf(" D (CSP/DL; default = bare CSP)\n");
51 printf(" v(erbose)\n");
58 * generate digest using reference BSAFE.
60 static CSSM_RETURN
genDigestBSAFE(
61 CSSM_ALGORITHMS hashAlg
,
62 const CSSM_DATA
*inText
,
63 CSSM_DATA_PTR outText
) // mallocd and returned
67 crtn
= buGenDigest(hashAlg
,
74 * Generate digest using CSP.
76 static CSSM_RETURN
genDigestCSSM(
77 CSSM_CSP_HANDLE cspHand
,
78 CSSM_ALGORITHMS hashAlg
,
79 const CSSM_DATA
*inText
,
80 CSSM_DATA_PTR outText
) // mallocd and returned if doGen
85 return cspStagedDigest(cspHand
,
87 CSSM_TRUE
, // mallocDigest
88 CSSM_TRUE
, // multiUpdates
95 static int doTest(CSSM_CSP_HANDLE cspHand
,
96 const CSSM_DATA
*ptext
,
100 CSSM_DATA hashRef
= {0, NULL
}; // digest, BSAFE reference
101 CSSM_DATA hashTest
= {0, NULL
}; // digest, CSP test
106 * generate with each method;
107 * verify digests compare;
109 crtn
= genDigestBSAFE(hashAlg
,
113 return testError(quiet
);
115 crtn
= genDigestCSSM(cspHand
,
120 return testError(quiet
);
123 /* ensure both methods resulted in same hash */
124 if(hashRef
.Length
!= hashTest
.Length
) {
125 printf("hash length mismatch (1)\n");
126 rtn
= testError(quiet
);
131 if(memcmp(hashRef
.Data
, hashTest
.Data
, hashTest
.Length
)) {
132 printf("hash miscompare\n");
133 rtn
= testError(quiet
);
139 if(hashTest
.Length
) {
140 CSSM_FREE(hashTest
.Data
);
143 CSSM_FREE(hashRef
.Data
);
149 int main(int argc
, char **argv
)
155 CSSM_CSP_HANDLE cspHand
;
157 uint32 hashAlg
; // CSSM_ALGID_xxx
159 unsigned currAlg
; // ALG_xxx
165 unsigned minAlg
= ALG_FIRST
;
166 unsigned maxAlg
= ALG_LAST
;
167 unsigned loops
= LOOPS_DEF
;
168 CSSM_BOOL verbose
= CSSM_FALSE
;
169 unsigned minExp
= MIN_EXP
;
170 unsigned maxExp
= DEFAULT_MAX_EXP
;
171 CSSM_BOOL quiet
= CSSM_FALSE
;
172 unsigned pauseInterval
= 0;
173 CSSM_BOOL bareCsp
= CSSM_TRUE
;
176 for(arg
=1; arg
<argc
; arg
++) {
185 minAlg
= maxAlg
= ALG_SHA1
;
188 minAlg
= maxAlg
= ALG_MD5
;
191 minAlg
= maxAlg
= ALG_MD2
;
198 loops
= atoi(&argp
[2]);
201 minExp
= atoi(&argp
[2]);
204 maxExp
= atoi(&argp
[2]);
205 if(maxExp
> MAX_EXP
) {
213 bareCsp
= CSSM_FALSE
;
219 pauseInterval
= atoi(&argp
[2]);;
226 if(minExp
> maxExp
) {
227 printf("***minExp must be <= maxExp\n");
230 ptext
.Data
= (uint8
*)CSSM_MALLOC(MAX_DATA_SIZE
);
231 if(ptext
.Data
== NULL
) {
232 printf("Insufficient heap space\n");
235 /* ptext length set in test loop */
237 printf("Starting hashCompat; args: ");
238 for(i
=1; i
<argc
; i
++) {
239 printf("%s ", argv
[i
]);
242 cspHand
= cspDlDbStartup(bareCsp
, NULL
);
248 printf("Top of test; hit CR to proceed: ");
251 for(currAlg
=minAlg
; currAlg
<=maxAlg
; currAlg
++) {
254 hashAlg
= CSSM_ALGID_SHA1
;
258 hashAlg
= CSSM_ALGID_MD5
;
262 hashAlg
= CSSM_ALGID_MD2
;
266 printf("***Brrzap. Bad alg.\n");
270 if(!quiet
|| verbose
) {
271 printf("Testing alg %s\n", algStr
);
273 for(loop
=1; ; loop
++) {
274 /* random ptext length and data */
275 ptext
.Length
= genData(ptext
.Data
, minExp
, maxExp
, DT_Random
);
277 if(verbose
|| ((loop
% LOOP_NOTIFY
) == 0)) {
278 printf("..loop %d text size %lu \n", loop
, ptext
.Length
);
289 if(pauseInterval
&& ((loop
% pauseInterval
) == 0)) {
292 printf("Hit CR to proceed, q to abort: ");
298 if(loops
&& (loop
== loops
)) {
308 cspShutdown(cspHand
, bareCsp
);
311 printf("ModuleDetach/Unload complete; hit CR to exit: ");
314 if((rtn
== 0) && !quiet
) {
315 printf("%s complete\n", argv
[0]);
317 CSSM_FREE(ptext
.Data
);