2 * Simple RSA sign/verify threadTest module
5 * Simple sign/verify test
7 #include "testParams.h"
8 #include <Security/cssmtype.h>
12 #include <cspxutils/cspwrap.h>
13 #include <cspxutils/common.h>
14 #include <BSafe/bsafe.h>
16 /* for memory leak debug only, with only one thread running */
19 #define SAFE_RAND_DATA 0
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
26 static B_ALGORITHM_METHOD
*BSAFE_ALGORITHM_CHOOSER
[] = {
37 (B_ALGORITHM_METHOD
*)NULL_PTR
40 /* generate RSA key pair */
41 static int RsaGenKeyPair(
42 const TestParams
*testParams
,
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
;
55 /* boilerplate RSA key pair generate */
56 /* first the random algorithm object */
57 brtn
= B_CreateAlgorithmObject(&randomAlgorithm
);
59 printf("***B_CreateAlgorithmObject error (%d)\n", brtn
);
62 brtn
= B_SetAlgorithmInfo(randomAlgorithm
,
66 printf("***B_SetAlgorithmInfo error (%d)\n", brtn
);
69 brtn
= B_RandomInit(randomAlgorithm
,
70 BSAFE_ALGORITHM_CHOOSER
,
71 (A_SURRENDER_CTX
*)NULL_PTR
);
73 printf("***B_RandomInit error (%d)\n", brtn
);
77 threadGetRandData(testParams
, &seedData
, NUM_SEED_BYTES
);
79 simpleGenData(&seedData
, NUM_SEED_BYTES
,NUM_SEED_BYTES
);
82 brtn
= B_RandomUpdate(randomAlgorithm
, seedBytes
, NUM_SEED_BYTES
,
83 (A_SURRENDER_CTX
*)NULL_PTR
);
85 printf("***B_RandomUpdate error (%d)\n", brtn
);
89 /* create a keypair generator */
90 brtn
= B_CreateAlgorithmObject(&keypairGenerator
);
92 printf("***B_CreateAlgorithmObject error (%d)\n", brtn
);
95 keygenParams
.modulusBits
= keySize
;
96 keygenParams
.publicExponent
.data
= f4Data
;
97 keygenParams
.publicExponent
.len
= 3;
99 brtn
= B_SetAlgorithmInfo(keypairGenerator
,
101 (POINTER
)&keygenParams
);
103 printf("***B_SetAlgorithmInfo error (%d)\n", brtn
);
108 brtn
= B_GenerateInit(keypairGenerator
,
109 BSAFE_ALGORITHM_CHOOSER
,
110 (A_SURRENDER_CTX
*)NULL_PTR
);
112 printf("***B_GenerateInit error (%d)\n", brtn
);
115 brtn
= B_CreateKeyObject(pubKey
);
117 printf("***B_CreateKeyObject error (%d)\n", brtn
);
120 brtn
= B_CreateKeyObject(privKey
);
122 printf("***B_CreateKeyObject error (%d)\n", brtn
);
126 brtn
= B_GenerateKeypair(keypairGenerator
,
130 (A_SURRENDER_CTX
*)NULL_PTR
);
132 printf("***B_GenerateKeypair error (%d)\n", brtn
);
136 B_DestroyAlgorithmObject (&keypairGenerator
);
137 B_DestroyAlgorithmObject (&randomAlgorithm
);
142 const TestParams
*testParams
,
144 const CSSM_DATA
*ptext
,
147 unsigned *actSigSize
) // RETURNED
150 B_ALGORITHM_OBJ signer
= (B_ALGORITHM_OBJ
)NULL_PTR
;
152 brtn
= B_CreateAlgorithmObject(&signer
);
154 printf("***B_CreateAlgorithmObject error (%d)\n", brtn
);
158 /* we happen to know that no info is needed for any signing algs */
159 brtn
= B_SetAlgorithmInfo(signer
,
160 AI_MD5WithRSAEncryption
,
163 printf("***B_SetAlgorithmInfo error (%d)\n", brtn
);
166 brtn
= B_SignInit(signer
,
168 BSAFE_ALGORITHM_CHOOSER
,
169 (A_SURRENDER_CTX
*)NULL_PTR
);
171 printf("***B_SignInit error (%d)\n", brtn
);
174 brtn
= B_SignUpdate(signer
,
179 printf("***B_SignUpdate error (%d)\n", brtn
);
182 brtn
= B_SignFinal(signer
,
189 printf("***B_SignFinal error (%d)\n", brtn
);
191 B_DestroyAlgorithmObject(&signer
);
195 static int rsaSigVerify(
196 const TestParams
*testParams
,
198 const CSSM_DATA
*ptext
,
200 unsigned sigSize
) // RETURNED
203 B_ALGORITHM_OBJ verifier
= (B_ALGORITHM_OBJ
)NULL_PTR
;
205 brtn
= B_CreateAlgorithmObject(&verifier
);
207 printf("***B_CreateAlgorithmObject error (%d)\n", brtn
);
211 /* we happen to know that no info is needed for any verifying algs */
212 brtn
= B_SetAlgorithmInfo(verifier
,
213 AI_MD5WithRSAEncryption
,
216 printf("***B_SetAlgorithmInfo error (%d)\n", brtn
);
219 brtn
= B_VerifyInit(verifier
,
221 BSAFE_ALGORITHM_CHOOSER
,
222 (A_SURRENDER_CTX
*)NULL_PTR
);
224 printf("***B_VerifyInit error (%d)\n", brtn
);
227 brtn
= B_VerifyUpdate(verifier
,
232 printf("***B_VerifyUpdate error (%d)\n", brtn
);
235 brtn
= B_VerifyFinal(verifier
,
241 printf("***B_VerifyFinal error (%d)\n", brtn
);
243 B_DestroyAlgorithmObject(&verifier
);
247 /* per-thread info */
254 int rsaSignInit(TestParams
*testParams
)
257 TT_RsaSignParams
*svParams
;
259 svParams
= (TT_RsaSignParams
*)CSSM_MALLOC(sizeof(TT_RsaSignParams
));
260 rtn
= RsaGenKeyPair(testParams
,
265 printf("***Error generating key pair; aborting\n");
268 svParams
->ptext
.Data
= (uint8
*)CSSM_MALLOC(PTEXT_SIZE
);
269 svParams
->ptext
.Length
= PTEXT_SIZE
;
271 testParams
->perThread
= svParams
;
275 int rsaSignTest(TestParams
*testParams
)
277 TT_RsaSignParams
*svParams
= (TT_RsaSignParams
*)testParams
->perThread
;
280 uint8 sigBytes
[MAX_SIG_SIZE
];
283 for(loop
=0; loop
<testParams
->numLoops
; loop
++) {
284 if(testParams
->verbose
) {
285 printf("signVerify thread %d: loop %d\n",
286 testParams
->threadNum
, loop
);
288 else if(!testParams
->quiet
) {
289 printChar(testParams
->progressChar
);
292 CSSM_RETURN crtn
= threadGetRandData(testParams
, &svParams
->ptext
, PTEXT_SIZE
);
297 simpleGenData(&svParams
->ptext
, PTEXT_SIZE
,PTEXT_SIZE
);
299 rtn
= rsaSign(testParams
,
308 rtn
= rsaSigVerify(testParams
,
319 printf("Hit CR to proceed: ");