3 #include <security_cdsa_client/keyclient.h>
4 #include <security_cdsa_client/cspclient.h>
5 #include <security_cdsa_client/macclient.h>
6 #include <security_cdsa_client/genkey.h>
7 #include <security_cdsa_client/wrapkey.h>
9 using namespace CssmClient
;
11 static void testCrypt(const Guid
&cspGuid
);
12 static void testDigests(const Guid
&cspGuid
);
13 static void testRandom(const Guid
&cspGuid
);
14 static void testMac(const Guid
&cspGuid
);
15 static void testWrap(const Guid
&cspGuid
);
20 testCrypt(gGuidAppleCSP
);
21 testCrypt(gGuidAppleCSPDL
);
22 testDigests(gGuidAppleCSP
);
23 testRandom(gGuidAppleCSP
);
24 testRandom(gGuidAppleCSPDL
);
25 testMac(gGuidAppleCSP
);
26 testMac(gGuidAppleCSPDL
);
31 testMac(gGuidAppleCSP
);
36 testWrap(gGuidAppleCSP
);
39 static void testCrypt(const Guid
&cspGuid
)
41 printf("\n* performing encrypt/decrypt test...\n");
45 printf("Generating key\n");
46 GenerateKey
genKey(csp
, CSSM_ALGID_DES
, 64);
47 Key key
= genKey(KeySpec(CSSM_KEYUSE_ANY
, CSSM_KEYATTR_RETURN_DEFAULT
));
51 printf("Generating iv\n");
52 //CssmData iv = Random(csp, CSSM_ALGID_SHARandom)(8);
53 CssmPolyData
iv("12345678");
55 CssmPolyData
in("Om mani padme hum");
60 printf("Encrypting\n");
62 Encrypt
encrypt(csp
, CSSM_ALGID_DES
);
63 encrypt
.mode(CSSM_ALGMODE_CBCPadIV8
);
64 encrypt
.padding(CSSM_PADDING_PKCS1
);
65 encrypt
.initVector(iv
);
69 encrypt
.encrypt(&in
, 1, &cipher
, 1);
70 encrypt
.final(remcipher
);
71 printf("ciphertext=");
77 printf("Decrypting\n");
79 Decrypt
decrypt(csp
, CSSM_ALGID_DES
);
81 decrypt
.mode(CSSM_ALGMODE_CBCPadIV8
);
82 decrypt
.padding(CSSM_PADDING_PKCS1
);
83 decrypt
.initVector(iv
);
86 CssmData inp
[] = { cipher
, remcipher
};
87 decrypt
.decrypt(inp
, 2, &plain
, 1);
88 decrypt
.final(remplain
);
94 printf("end encrypt/decrypt test\n");
97 static void testDigests(const Guid
&cspGuid
)
99 printf("\n* performing digest test...\n");
101 Digest
md5(csp
, CSSM_ALGID_MD5
);
102 StringData
data("Once in a blue moon");
103 DataBuffer
<20> digest
;
104 md5
.digest(data
, digest
);
110 static void testRandom(const Guid
&cspGuid
)
112 printf("\n* performing random test...\n");
114 CssmData result
= Random(csp
, CSSM_ALGID_APPLE_YARROW
)(16);
115 assert(result
.length() == 16);
122 void dump(const CssmData
&data
)
124 unsigned char *p
= data
;
125 for (uint32 n
= 0; n
< data
.length(); n
++)
126 printf("%2.2x", p
[n
]);
130 static void testMac(const Guid
&cspGuid
)
132 printf("\n* performing mac test...\n");
136 keyData
.Data
= (uint8
*)"1234567";
140 Key
key(csp
, keyData
);
142 printf("Generating key\n");
143 GenerateKey
genKey(csp
, CSSM_ALGID_DES
, 64);
144 key
= genKey(KeySpec(CSSM_KEYUSE_ANY
, CSSM_KEYATTR_RETURN_DEFAULT
));
147 GenerateMac
mac(csp
, CSSM_ALGID_SHA1HMAC
);
149 StringData
data("Om mani padme hum");
150 DataBuffer
<20> signature
;
151 mac
.sign(data
, signature
);
152 printf("signature=");
155 VerifyMac
vmac(csp
, CSSM_ALGID_SHA1HMAC
);
157 vmac
.verify(data
, signature
);
158 printf("testing mac verify\n");
163 printf("testing mac verify with bad data\n");
164 StringData
baddata("not even close to the original");
165 vmac
.verify(baddata
, signature
);
167 catch(const CssmError
&e
)
169 printf("caught verify error\n");
171 if (e
.osStatus() != CSSMERR_CSP_VERIFY_FAILED
)
174 if (!failed
) throw Error(CSSMERR_CSP_VERIFY_FAILED
);
176 printf("end mac test\n");
179 static void testWrap(const Guid
&cspGuid
)
181 printf("\n* performing wrap test...\n");
185 keyData
.Data
= (uint8
*)"1234567";
189 Key
key(csp
, keyData
);
192 GenerateKey
genKey(csp
, CSSM_ALGID_RC4
, 128);
193 key
= genKey(KeySpec(CSSM_KEYUSE_ANY
, CSSM_KEYATTR_RETURN_DEFAULT
));
195 WrapKey
wrapKey(csp
, CSSM_ALGID_RC2
);
198 AccessCredentials(cred
);
200 wrapKey
.mode(CSSM_ALGMODE_CBC_IV8
);
203 initVec
.Data
= (uint8
*)"12345678";
204 wrapKey
.initVector(initVec
);
206 wrappedKey
=wrapKey(key
);
209 printf("end wrap test\n");