2 * keyHash.c - simple test of CSSM_APPLECSP_KEYDIGEST passthrough
9 #include <Security/cssm.h>
12 #include "cspdlTesting.h"
14 #define USAGE_NAME "noUsage"
15 #define USAGE_NAME_LEN (strlen(USAGE_NAME))
18 static void usage(char **argv
)
20 printf("usage: %s [options]\n", argv
[0]);
22 printf(" l=loops (default=%d; 0=forever)\n", LOOPS_DEF
);
23 printf(" D (CSP/DL; default = bare CSP)\n");
24 printf(" p(ause on each loop)\n");
26 printf(" v(erbose))\n");
30 static void dumpBuf(uint8
*buf
,
36 for(i
=0; i
<len
; i
++) {
37 printf("%02X ", buf
[i
]);
47 * NULL wrap to data key;
48 * obtain hash of both keys;
49 * ensure hashes are equal;
52 CSSM_CSP_HANDLE cspHand
, // raw or CSPDL
53 CSSM_CSP_HANDLE rawCspHand
, // raw, may be same as cspHand
60 CSSM_DATA_PTR refHash
;
61 CSSM_DATA_PTR rawHash
;
63 if(refKey
->KeyHeader
.BlobType
!= CSSM_KEYBLOB_REFERENCE
) {
64 printf("Hey! this only works on ref keys!!\n");
69 crtn
= cspWrapKey(cspHand
,
74 CSSM_KEYBLOB_WRAPPED_FORMAT_NONE
,
80 return testError(quiet
);
83 /* hash of both keys */
84 crtn
= cspKeyHash(cspHand
, refKey
, &refHash
);
86 return testError(quiet
);
90 printf(" ...Ref key hash:\n ");
91 dumpBuf(refHash
->Data
, refHash
->Length
);
94 crtn
= cspKeyHash(rawCspHand
, &rawKey
, &rawHash
);
96 return testError(quiet
);
100 printf(" ...Raw key hash:\n ");
101 dumpBuf(rawHash
->Data
, rawHash
->Length
);
104 if(!appCompareCssmData(refHash
, rawHash
)) {
105 printf("***Key Hash Miscompare!\n");
106 return testError(quiet
);
108 appFreeCssmData(refHash
, CSSM_TRUE
);
109 appFreeCssmData(rawHash
, CSSM_TRUE
);
110 cspFreeKey(cspHand
, &rawKey
);
114 int main(int argc
, char **argv
)
119 CSSM_CSP_HANDLE cspHand
;
120 CSSM_CSP_HANDLE rawCspHand
;
130 unsigned loops
= LOOPS_DEF
;
131 CSSM_BOOL verbose
= CSSM_FALSE
;
132 CSSM_BOOL quiet
= CSSM_FALSE
;
133 CSSM_BOOL bareCsp
= CSSM_TRUE
;
134 CSSM_BOOL doPause
= CSSM_FALSE
;
136 for(arg
=1; arg
<argc
; arg
++) {
140 loops
= atoi(&argp
[2]);
143 bareCsp
= CSSM_FALSE
;
160 printf("Starting keyHash; args: ");
161 for(i
=1; i
<argc
; i
++) {
162 printf("%s ", argv
[i
]);
165 cspHand
= cspDlDbStartup(bareCsp
, NULL
);
170 rawCspHand
= cspHand
;
173 rawCspHand
= cspDlDbStartup(CSSM_TRUE
, NULL
);
174 if(rawCspHand
== 0) {
178 for(loop
=1; ; loop
++) {
180 printf("...loop %d\n", loop
);
183 /* first with symmetric key */
185 printf(" ...testing DES key\n");
187 symKey
= cspGenSymKey(cspHand
,
191 CSSM_KEYUSE_ENCRYPT
| CSSM_KEYUSE_DECRYPT
,
193 CSSM_TRUE
); // refKey
195 if(testError(quiet
)) {
199 if(doTest(cspHand
, rawCspHand
, symKey
, verbose
, quiet
)) {
202 CSSM_FreeKey(cspHand
, NULL
, symKey
, CSSM_TRUE
);
205 /* cook up an RSA key pair */
206 rtn
= cspGenKeyPair(cspHand
,
210 CSP_RSA_KEY_SIZE_DEFAULT
,
212 CSSM_TRUE
, // pubIsRef
214 CSSM_KEYBLOB_RAW_FORMAT_NONE
,
216 CSSM_TRUE
, // privIsRef
218 CSSM_KEYBLOB_RAW_FORMAT_NONE
,
219 CSSM_FALSE
); // genSeed
221 if(testError(quiet
)) {
226 printf(" ...testing RSA public key\n");
228 if(doTest(cspHand
, rawCspHand
, &pubKey
, verbose
, quiet
)) {
232 printf(" ...testing RSA private key\n");
234 if(doTest(cspHand
, rawCspHand
, &privKey
, verbose
, quiet
)) {
237 CSSM_FreeKey(cspHand
, NULL
, &pubKey
, CSSM_TRUE
);
238 CSSM_FreeKey(cspHand
, NULL
, &privKey
, CSSM_TRUE
);
239 if(loops
&& (loop
== loops
)) {
244 printf("Hit CR to proceed: ");