1/* 2 * Simple sign/verify test 3 */ 4#include "testParams.h" 5#include <Security/cssm.h> 6#include <stdlib.h> 7#include <stdio.h> 8#include <time.h> 9#include <utilLib/common.h> 10#include <utilLib/cspwrap.h> 11#include <strings.h> 12 13/* for memory leak debug only, with only one thread running */ 14#define DO_PAUSE 0 15 16#define SIG_ALG CSSM_ALGID_SHA1WithRSA 17#define KEY_GEN_ALG CSSM_ALGID_RSA 18#define KEY_SIZE CSP_RSA_KEY_SIZE_DEFAULT 19#define PTEXT_SIZE 1024 20#define USAGE_DEF "noUsage" 21 22/* per-thread info */ 23typedef struct { 24 CSSM_KEY privKey; 25 CSSM_KEY pubKey; 26 CSSM_DATA ptext; 27} TT_SignVfyParams; 28 29int signVerifyInit(TestParams *testParams) 30{ 31 CSSM_BOOL pubIsRef; 32 CSSM_BOOL privIsRef; 33 CSSM_RETURN crtn; 34 TT_SignVfyParams *svParams; 35 36 /* flip coin for ref/blob key forms */ 37 if(testParams->threadNum & 1) { 38 pubIsRef = CSSM_TRUE; 39 privIsRef = CSSM_FALSE; 40 } 41 else { 42 pubIsRef = CSSM_FALSE; 43 privIsRef = CSSM_TRUE; 44 } 45 svParams = (TT_SignVfyParams *)CSSM_MALLOC(sizeof(TT_SignVfyParams)); 46 crtn = cspGenKeyPair(testParams->cspHand, 47 KEY_GEN_ALG, 48 USAGE_DEF, 49 strlen(USAGE_DEF), 50 KEY_SIZE, 51 &svParams->pubKey, 52 pubIsRef, 53 CSSM_KEYUSE_VERIFY, 54 CSSM_KEYBLOB_RAW_FORMAT_NONE, 55 &svParams->privKey, 56 privIsRef, 57 CSSM_KEYUSE_SIGN, 58 CSSM_KEYBLOB_RAW_FORMAT_NONE, 59 CSSM_FALSE); 60 if(crtn) { 61 printf("***Error generating key pair; aborting\n"); 62 return 1; 63 } 64 svParams->ptext.Data = (uint8 *)CSSM_MALLOC(PTEXT_SIZE); 65 svParams->ptext.Length = PTEXT_SIZE; 66 67 testParams->perThread = svParams; 68 return 0; 69} 70 71int signVerify(TestParams *testParams) 72{ 73 TT_SignVfyParams *svParams = (TT_SignVfyParams *)testParams->perThread; 74 unsigned loop; 75 CSSM_RETURN crtn; 76 CSSM_DATA sig; 77 78 for(loop=0; loop<testParams->numLoops; loop++) { 79 if(testParams->verbose) { 80 printf("signVerify thread %d: loop %d\n", 81 testParams->threadNum, loop); 82 } 83 else if(!testParams->quiet) { 84 printChar(testParams->progressChar); 85 } 86 crtn = threadGetRandData(testParams, &svParams->ptext, PTEXT_SIZE); 87 if(crtn) { 88 return 1; 89 } 90 sig.Data = NULL; 91 sig.Length = 0; 92 crtn = cspSign(testParams->cspHand, 93 SIG_ALG, 94 &svParams->privKey, 95 &svParams->ptext, 96 &sig); 97 if(crtn) { 98 return 1; 99 } 100 crtn = cspSigVerify(testParams->cspHand, 101 SIG_ALG, 102 &svParams->pubKey, 103 &svParams->ptext, 104 &sig, 105 CSSM_OK); 106 if(crtn) { 107 return 1; 108 } 109 appFree(sig.Data, NULL); 110 #if DO_PAUSE 111 fpurge(stdin); 112 printf("Hit CR to proceed: "); 113 getchar(); 114 #endif 115 } 116 return 0; 117} 118 119