]> git.saurik.com Git - apple/security.git/blob - SecurityTests/clxutils/threadTest/rsaSign.cpp
Security-57336.1.9.tar.gz
[apple/security.git] / SecurityTests / clxutils / threadTest / rsaSign.cpp
1 /*
2 * Simple RSA sign/verify threadTest module
3 */
4 /*
5 * Simple sign/verify test
6 */
7 #include "testParams.h"
8 #include <Security/cssmtype.h>
9 #include <stdlib.h>
10 #include <stdio.h>
11 #include <time.h>
12 #include <cspxutils/cspwrap.h>
13 #include <cspxutils/common.h>
14 #include <BSafe/bsafe.h>
15
16 /* for memory leak debug only, with only one thread running */
17 #define DO_PAUSE 0
18
19 #define SAFE_RAND_DATA 0
20
21 #define KEY_SIZE CSP_RSA_KEY_SIZE_DEFAULT /* key size, bits */
22 #define MAX_SIG_SIZE ((KEY_SIZE / 8) * 2) /* max signature size, bytes */
23 #define PTEXT_SIZE 1024
24 #define NUM_SEED_BYTES 32
25
26 static B_ALGORITHM_METHOD *BSAFE_ALGORITHM_CHOOSER[] = {
27 &AM_MD5_RANDOM,
28 &AM_MD,
29 &AM_MD5,
30 &AM_MAC,
31 &AM_SHA,
32 &AM_RSA_CRT_DECRYPT,
33 &AM_RSA_CRT_ENCRYPT,
34 &AM_RSA_DECRYPT,
35 &AM_RSA_ENCRYPT,
36 &AM_RSA_KEY_GEN,
37 (B_ALGORITHM_METHOD *)NULL_PTR
38 };
39
40 /* generate RSA key pair */
41 static int RsaGenKeyPair(
42 const TestParams *testParams,
43 unsigned keySize,
44 B_KEY_OBJ *pubKey,
45 B_KEY_OBJ *privKey)
46 {
47 int brtn;
48 B_ALGORITHM_OBJ keypairGenerator = (B_ALGORITHM_OBJ)NULL_PTR;
49 static unsigned char f4Data[3] = {0x01, 0x00, 0x01};
50 B_ALGORITHM_OBJ randomAlgorithm = (B_ALGORITHM_OBJ)NULL_PTR;
51 uint8 seedBytes[NUM_SEED_BYTES];
52 CSSM_DATA seedData = {NUM_SEED_BYTES, seedBytes};
53 A_RSA_KEY_GEN_PARAMS keygenParams;
54
55 /* boilerplate RSA key pair generate */
56 /* first the random algorithm object */
57 brtn = B_CreateAlgorithmObject(&randomAlgorithm);
58 if(brtn) {
59 printf("***B_CreateAlgorithmObject error (%d)\n", brtn);
60 return 1;
61 }
62 brtn = B_SetAlgorithmInfo(randomAlgorithm,
63 AI_MD5Random,
64 NULL_PTR);
65 if(brtn) {
66 printf("***B_SetAlgorithmInfo error (%d)\n", brtn);
67 return 1;
68 }
69 brtn = B_RandomInit(randomAlgorithm,
70 BSAFE_ALGORITHM_CHOOSER,
71 (A_SURRENDER_CTX *)NULL_PTR);
72 if(brtn) {
73 printf("***B_RandomInit error (%d)\n", brtn);
74 return 1;
75 }
76 #if SAFE_RAND_DATA
77 threadGetRandData(testParams, &seedData, NUM_SEED_BYTES);
78 #else
79 simpleGenData(&seedData, NUM_SEED_BYTES,NUM_SEED_BYTES);
80 #endif
81
82 brtn = B_RandomUpdate(randomAlgorithm, seedBytes, NUM_SEED_BYTES,
83 (A_SURRENDER_CTX *)NULL_PTR);
84 if(brtn) {
85 printf("***B_RandomUpdate error (%d)\n", brtn);
86 return 1;
87 }
88
89 /* create a keypair generator */
90 brtn = B_CreateAlgorithmObject(&keypairGenerator);
91 if(brtn) {
92 printf("***B_CreateAlgorithmObject error (%d)\n", brtn);
93 return 1;
94 }
95 keygenParams.modulusBits = keySize;
96 keygenParams.publicExponent.data = f4Data;
97 keygenParams.publicExponent.len = 3;
98
99 brtn = B_SetAlgorithmInfo(keypairGenerator,
100 AI_RSAKeyGen,
101 (POINTER)&keygenParams);
102 if(brtn) {
103 printf("***B_SetAlgorithmInfo error (%d)\n", brtn);
104 return 1;
105 }
106
107 /* go for it */
108 brtn = B_GenerateInit(keypairGenerator,
109 BSAFE_ALGORITHM_CHOOSER,
110 (A_SURRENDER_CTX *)NULL_PTR);
111 if(brtn) {
112 printf("***B_GenerateInit error (%d)\n", brtn);
113 return 1;
114 }
115 brtn = B_CreateKeyObject(pubKey);
116 if(brtn) {
117 printf("***B_CreateKeyObject error (%d)\n", brtn);
118 return 1;
119 }
120 brtn = B_CreateKeyObject(privKey);
121 if(brtn) {
122 printf("***B_CreateKeyObject error (%d)\n", brtn);
123 return 1;
124 }
125
126 brtn = B_GenerateKeypair(keypairGenerator,
127 *pubKey,
128 *privKey,
129 randomAlgorithm,
130 (A_SURRENDER_CTX *)NULL_PTR);
131 if(brtn) {
132 printf("***B_GenerateKeypair error (%d)\n", brtn);
133 return 1;
134 }
135
136 B_DestroyAlgorithmObject (&keypairGenerator);
137 B_DestroyAlgorithmObject (&randomAlgorithm);
138 return 0;
139 }
140
141 static int rsaSign(
142 const TestParams *testParams,
143 B_KEY_OBJ privKey,
144 const CSSM_DATA *ptext,
145 uint8 *sigBytes,
146 unsigned maxSigSize,
147 unsigned *actSigSize) // RETURNED
148 {
149 int brtn;
150 B_ALGORITHM_OBJ signer = (B_ALGORITHM_OBJ)NULL_PTR;
151
152 brtn = B_CreateAlgorithmObject(&signer);
153 if(brtn) {
154 printf("***B_CreateAlgorithmObject error (%d)\n", brtn);
155 return 1;
156 }
157
158 /* we happen to know that no info is needed for any signing algs */
159 brtn = B_SetAlgorithmInfo(signer,
160 AI_MD5WithRSAEncryption,
161 NULL);
162 if(brtn) {
163 printf("***B_SetAlgorithmInfo error (%d)\n", brtn);
164 return 1;
165 }
166 brtn = B_SignInit(signer,
167 privKey,
168 BSAFE_ALGORITHM_CHOOSER,
169 (A_SURRENDER_CTX *)NULL_PTR);
170 if(brtn) {
171 printf("***B_SignInit error (%d)\n", brtn);
172 return 1;
173 }
174 brtn = B_SignUpdate(signer,
175 ptext->Data,
176 ptext->Length,
177 NULL);
178 if(brtn) {
179 printf("***B_SignUpdate error (%d)\n", brtn);
180 return 1;
181 }
182 brtn = B_SignFinal(signer,
183 sigBytes,
184 actSigSize,
185 maxSigSize,
186 NULL, // randAlg
187 NULL);
188 if(brtn) {
189 printf("***B_SignFinal error (%d)\n", brtn);
190 }
191 B_DestroyAlgorithmObject(&signer);
192 return brtn;
193 }
194
195 static int rsaSigVerify(
196 const TestParams *testParams,
197 B_KEY_OBJ pubKey,
198 const CSSM_DATA *ptext,
199 uint8 *sigBytes,
200 unsigned sigSize) // RETURNED
201 {
202 int brtn;
203 B_ALGORITHM_OBJ verifier = (B_ALGORITHM_OBJ)NULL_PTR;
204
205 brtn = B_CreateAlgorithmObject(&verifier);
206 if(brtn) {
207 printf("***B_CreateAlgorithmObject error (%d)\n", brtn);
208 return 1;
209 }
210
211 /* we happen to know that no info is needed for any verifying algs */
212 brtn = B_SetAlgorithmInfo(verifier,
213 AI_MD5WithRSAEncryption,
214 NULL);
215 if(brtn) {
216 printf("***B_SetAlgorithmInfo error (%d)\n", brtn);
217 return 1;
218 }
219 brtn = B_VerifyInit(verifier,
220 pubKey,
221 BSAFE_ALGORITHM_CHOOSER,
222 (A_SURRENDER_CTX *)NULL_PTR);
223 if(brtn) {
224 printf("***B_VerifyInit error (%d)\n", brtn);
225 return 1;
226 }
227 brtn = B_VerifyUpdate(verifier,
228 ptext->Data,
229 ptext->Length,
230 NULL);
231 if(brtn) {
232 printf("***B_VerifyUpdate error (%d)\n", brtn);
233 return 1;
234 }
235 brtn = B_VerifyFinal(verifier,
236 sigBytes,
237 sigSize,
238 NULL, // randAlg
239 NULL);
240 if(brtn) {
241 printf("***B_VerifyFinal error (%d)\n", brtn);
242 }
243 B_DestroyAlgorithmObject(&verifier);
244 return brtn;
245 }
246
247 /* per-thread info */
248 typedef struct {
249 B_KEY_OBJ privKey;
250 B_KEY_OBJ pubKey;
251 CSSM_DATA ptext;
252 } TT_RsaSignParams;
253
254 int rsaSignInit(TestParams *testParams)
255 {
256 int rtn;
257 TT_RsaSignParams *svParams;
258
259 svParams = (TT_RsaSignParams *)CSSM_MALLOC(sizeof(TT_RsaSignParams));
260 rtn = RsaGenKeyPair(testParams,
261 KEY_SIZE,
262 &svParams->pubKey,
263 &svParams->privKey);
264 if(rtn) {
265 printf("***Error generating key pair; aborting\n");
266 return 1;
267 }
268 svParams->ptext.Data = (uint8 *)CSSM_MALLOC(PTEXT_SIZE);
269 svParams->ptext.Length = PTEXT_SIZE;
270
271 testParams->perThread = svParams;
272 return 0;
273 }
274
275 int rsaSignTest(TestParams *testParams)
276 {
277 TT_RsaSignParams *svParams = (TT_RsaSignParams *)testParams->perThread;
278 unsigned loop;
279 int rtn;
280 uint8 sigBytes[MAX_SIG_SIZE];
281 unsigned actSigSize;
282
283 for(loop=0; loop<testParams->numLoops; loop++) {
284 if(testParams->verbose) {
285 printf("signVerify thread %d: loop %d\n",
286 testParams->threadNum, loop);
287 }
288 else if(!testParams->quiet) {
289 printChar(testParams->progressChar);
290 }
291 #if SAFE_RAND_DATA
292 CSSM_RETURN crtn = threadGetRandData(testParams, &svParams->ptext, PTEXT_SIZE);
293 if(crtn) {
294 return 1;
295 }
296 #else
297 simpleGenData(&svParams->ptext, PTEXT_SIZE,PTEXT_SIZE);
298 #endif
299 rtn = rsaSign(testParams,
300 svParams->privKey,
301 &svParams->ptext,
302 sigBytes,
303 MAX_SIG_SIZE,
304 &actSigSize);
305 if(rtn) {
306 return 1;
307 }
308 rtn = rsaSigVerify(testParams,
309 svParams->pubKey,
310 &svParams->ptext,
311 sigBytes,
312 actSigSize);
313 if(rtn) {
314 return 1;
315 }
316
317 #if DO_PAUSE
318 fpurge(stdin);
319 printf("Hit CR to proceed: ");
320 getchar();
321 #endif
322 }
323 return 0;
324 }
325