]> git.saurik.com Git - apple/security.git/blob - SecurityTests/testclient/csptests.cpp
Security-57031.1.35.tar.gz
[apple/security.git] / SecurityTests / testclient / csptests.cpp
1 #include "csptests.h"
2
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>
8
9 using namespace CssmClient;
10
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);
16
17
18 void csptests()
19 {
20 testCrypt(gGuidAppleCSP);
21 testCrypt(gGuidAppleCSPDL);
22 testDigests(gGuidAppleCSP);
23 testRandom(gGuidAppleCSP);
24 testRandom(gGuidAppleCSPDL);
25 testMac(gGuidAppleCSP);
26 testMac(gGuidAppleCSPDL);
27 }
28
29 void testmac()
30 {
31 testMac(gGuidAppleCSP);
32 }
33
34 void testwrap()
35 {
36 testWrap(gGuidAppleCSP);
37 }
38
39 static void testCrypt(const Guid &cspGuid)
40 {
41 printf("\n* performing encrypt/decrypt test...\n");
42
43 CSP csp(cspGuid);
44
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));
48 printf("done\n");
49
50 // Gnerate IV
51 printf("Generating iv\n");
52 //CssmData iv = Random(csp, CSSM_ALGID_SHARandom)(8);
53 CssmPolyData iv("12345678");
54
55 CssmPolyData in("Om mani padme hum");
56 printf("input=");
57 dump(in);
58
59 // Encrypt
60 printf("Encrypting\n");
61
62 Encrypt encrypt(csp, CSSM_ALGID_DES);
63 encrypt.mode(CSSM_ALGMODE_CBCPadIV8);
64 encrypt.padding(CSSM_PADDING_PKCS1);
65 encrypt.initVector(iv);
66 encrypt.key(key);
67 CssmData cipher;
68 CssmData remcipher;
69 encrypt.encrypt(&in, 1, &cipher, 1);
70 encrypt.final(remcipher);
71 printf("ciphertext=");
72 dump(cipher);
73 printf("remainder=");
74 dump(remcipher);
75
76 // Decrypt
77 printf("Decrypting\n");
78
79 Decrypt decrypt(csp, CSSM_ALGID_DES);
80 decrypt.key(key);
81 decrypt.mode(CSSM_ALGMODE_CBCPadIV8);
82 decrypt.padding(CSSM_PADDING_PKCS1);
83 decrypt.initVector(iv);
84 CssmData plain;
85 CssmData remplain;
86 CssmData inp[] = { cipher, remcipher };
87 decrypt.decrypt(inp, 2, &plain, 1);
88 decrypt.final(remplain);
89 printf("plaintext=");
90 dump(plain);
91 printf("remainder=");
92 dump(remplain);
93
94 printf("end encrypt/decrypt test\n");
95 }
96
97 static void testDigests(const Guid &cspGuid)
98 {
99 printf("\n* performing digest test...\n");
100 CSP csp(cspGuid);
101 Digest md5(csp, CSSM_ALGID_MD5);
102 StringData data("Once in a blue moon");
103 DataBuffer<20> digest;
104 md5.digest(data, digest);
105 printf("digest=");
106 dump(digest);
107 }
108
109
110 static void testRandom(const Guid &cspGuid)
111 {
112 printf("\n* performing random test...\n");
113 CSP csp(cspGuid);
114 CssmData result = Random(csp, CSSM_ALGID_APPLE_YARROW)(16);
115 assert(result.length() == 16);
116 printf("result=");
117 dump(result);
118 free(result.data());
119 }
120
121
122 void dump(const CssmData &data)
123 {
124 unsigned char *p = data;
125 for (uint32 n = 0; n < data.length(); n++)
126 printf("%2.2x", p[n]);
127 printf("\n");
128 }
129
130 static void testMac(const Guid &cspGuid)
131 {
132 printf("\n* performing mac test...\n");
133
134 CssmData keyData;
135 keyData.Length = 8;
136 keyData.Data = (uint8 *)"1234567";
137
138 CSP csp(cspGuid);
139
140 Key key(csp, keyData);
141
142 printf("Generating key\n");
143 GenerateKey genKey(csp, CSSM_ALGID_DES, 64);
144 key = genKey(KeySpec(CSSM_KEYUSE_ANY, CSSM_KEYATTR_RETURN_DEFAULT));
145 printf("done\n");
146
147 GenerateMac mac(csp, CSSM_ALGID_SHA1HMAC);
148 mac.key(key);
149 StringData data("Om mani padme hum");
150 DataBuffer<20> signature;
151 mac.sign(data, signature);
152 printf("signature=");
153 dump(signature);
154
155 VerifyMac vmac(csp, CSSM_ALGID_SHA1HMAC);
156 vmac.key(key);
157 vmac.verify(data, signature);
158 printf("testing mac verify\n");
159
160 bool failed = false;
161 try
162 {
163 printf("testing mac verify with bad data\n");
164 StringData baddata("not even close to the original");
165 vmac.verify(baddata, signature);
166 }
167 catch(const CssmError &e)
168 {
169 printf("caught verify error\n");
170 failed = true;
171 if (e.osStatus() != CSSMERR_CSP_VERIFY_FAILED)
172 throw;
173 }
174 if (!failed) throw Error(CSSMERR_CSP_VERIFY_FAILED);
175
176 printf("end mac test\n");
177 }
178
179 static void testWrap(const Guid &cspGuid)
180 {
181 printf("\n* performing wrap test...\n");
182
183 CssmData keyData;
184 keyData.Length = 8;
185 keyData.Data = (uint8 *)"1234567";
186
187 CSP csp(cspGuid);
188
189 Key key(csp, keyData);
190
191 Key wrappedKey;
192 GenerateKey genKey(csp, CSSM_ALGID_RC4, 128);
193 key = genKey(KeySpec(CSSM_KEYUSE_ANY, CSSM_KEYATTR_RETURN_DEFAULT));
194
195 WrapKey wrapKey(csp, CSSM_ALGID_RC2);
196 wrapKey.key(key);
197
198 AccessCredentials(cred);
199 wrapKey.cred(&cred);
200 wrapKey.mode(CSSM_ALGMODE_CBC_IV8);
201 CssmData initVec;
202 initVec.Length = 8;
203 initVec.Data = (uint8 *)"12345678";
204 wrapKey.initVector(initVec);
205
206 wrappedKey=wrapKey(key);
207
208
209 printf("end wrap test\n");
210 }