]>
git.saurik.com Git - apple/security.git/blob - libsecurity_cryptkit/ckutils/badsig/badsig.c
1 /* Copyright 1996-1997 Apple Computer, Inc.
3 * badsig.c - Verify bad signature detect
7 * 26 Aug 1996 Doug Mitchell at NeXT
12 * text size = {random, from 100 bytes to 1 megabyte, in
13 * geometrical steps, i.e. the number of
14 * bytes would be 10^r, where r is random out of
15 * {2,3,4,5,6}, plus a random integer in {0,..99}};
17 * password size = constant;
20 * text contents = {random data, random size as specified above};
21 * passsword data = random;
23 Alternate between ECDSA and ElGamal on sucessive loops:
24 * generate signature, validate;
25 * for each byte of signature {
27 * verify bad signature;
28 * restore corrupted byte;
36 #if !CRYPTKIT_HIGH_LEVEL_SIG
37 #error Can not build this program against a lib with !CRYPTKIT_HIGH_LEVEL_SIG.
43 static unsigned char *passwdPool
; /* all passwords come from here */
44 static unsigned char *dataPool
; /* plaintext comes from here */
46 #define MAX_DATA_SIZE ((1024 * 1024) + 100) /* bytes */
52 #define MIN_EXP 2 /* for data size 10**exp */
54 #define PWD_LENGTH 15 /* bytes */
55 #define DEPTH_DEFAULT FEE_DEPTH_DEFAULT
56 #define INCR_DEFAULT 1 /* munge every incr bytes */
58 ///#define DEPTH_DEFAULT FEE_DEPTH_5
60 static void usage(char **argv
)
62 printf("usage: %s [options]\n", argv
[0]);
63 printf(" Options:\n");
64 printf(" l=loops (default=%d; 0=forever)\n", LOOPS_DEF
);
65 printf(" n=minExp (default=%d)\n", MIN_EXP
);
66 printf(" x=maxExp (default=max=%d)\n", MAX_EXP
);
67 printf(" p=passwdLength (default=%d)\n", PWD_LENGTH
);
68 printf(" D=depth (default=%d)\n", DEPTH_DEFAULT
);
69 printf(" i=increment (default=%d)\n", INCR_DEFAULT
);
70 #if CRYPTKIT_ECDSA_ENABLE
71 printf(" e (ElGamal only, no ECDSA)\n");
74 printf(" v(erbose)\n");
81 * ...min <= return <= max
83 static int genRand(int min
, int max
)
86 /* note random() only yields a 31-bit number... */
88 if(max
== min
) /* avoid % 1 ! */
91 return(min
+ (random() % (max
-min
+1)));
94 static unsigned char *genPasswd(unsigned passwdLength
)
96 unsigned *ip
= (unsigned *)passwdPool
;
97 unsigned intCount
= passwdLength
/ 4;
100 unsigned residue
= passwdLength
& 0x3;
102 for (i
=0; i
<intCount
; i
++) {
105 cp
= (unsigned char *)ip
;
106 for(i
=0; i
<residue
; i
++) {
107 *cp
= (unsigned char)random();
113 * Calculate random data size, fill dataPool with that many random bytes.
122 #define MAX_OFFSET 99
124 #define MIN_ASCII ' '
125 #define MAX_ASCII '~'
127 static unsigned char *genData(unsigned minExp
,
130 unsigned *length
) // RETURNED
143 * Calculate "random" size : (10 ** (random exponent)) + random offset
145 exp
= genRand(minExp
, maxExp
);
146 offset
= genRand(MIN_OFFSET
, MAX_OFFSET
);
148 while(exp
--) { // size = 10 ** exp
155 bzero(dataPool
, size
);
160 for(i
=0; i
<size
; i
++) {
168 intCount
= size
>> 2;
169 ip
= (unsigned *)dataPool
;
170 for(i
=0; i
<intCount
; i
++) {
174 residue
= size
& 0x3;
175 cp
= (unsigned char *)ip
;
176 for(i
=0; i
<residue
; i
++) {
177 *cp
++ = (unsigned char)random();
186 static int sigError()
190 printf("Attach via debugger for more info.\n");
191 printf("a to abort, c to continue: ");
193 return (resp
[0] != 'c');
198 int doTest(unsigned char *ptext
,
200 unsigned char *passwd
,
207 int doECDSAVfy
) // ignored if doECDSASig == 0
213 unsigned char origData
;
217 pubKey
= feePubKeyAlloc();
218 frtn
= feePubKeyInitFromPrivDataDepth(pubKey
,
224 printf("feePubKeyInitFromPrivData returned %s\n",
225 feeReturnString(frtn
));
228 #if CRYPTKIT_ECDSA_ENABLE
230 frtn
= feePubKeyCreateECDSASignature(pubKey
,
236 printf("feePubKeyCreateECDSASignature returned %s\n",
237 feeReturnString(frtn
));
241 frtn
= feePubKeyVerifyECDSASignature(pubKey
,
248 frtn
= feePubKeyVerifySignature(pubKey
,
258 #endif /* CRYPTKIT_ECDSA_ENABLE */
259 frtn
= feePubKeyCreateSignature(pubKey
,
265 printf("feePubKeyCreateSignature returned %s\n",
266 feeReturnString(frtn
));
269 frtn
= feePubKeyVerifySignature(pubKey
,
276 printf("**Unexpected BAD signature\n");
279 for(byte
=0; byte
<ptextLen
; byte
+= incr
) {
280 if(!quiet
&& (verbose
|| ((byte
% LOG_FREQ
) == 0))) {
281 printf("....byte %d\n", byte
);
283 origData
= ptext
[byte
];
286 * Generate random non-zero byte
289 bits
= random() & 0xff;
293 #if CRYPTKIT_ECDSA_ENABLE
294 if(doECDSA
&& doECDSAVfy
) {
295 frtn
= feePubKeyVerifyECDSASignature(pubKey
,
304 #endif /* CRYPTKIT_ECDSA_ENABLE */
305 frtn
= feePubKeyVerifySignature(pubKey
,
311 if(frtn
== FR_Success
) {
312 printf("**Unexpected GOOD signature\n");
315 ptext
[byte
] = origData
;
317 feePubKeyFree(pubKey
);
321 int main(int argc
, char **argv
)
326 unsigned char *ptext
;
328 unsigned char *passwd
;
335 unsigned passwdLen
= PWD_LENGTH
;
336 unsigned loops
= LOOPS_DEF
;
340 unsigned minExp
= MIN_EXP
;
341 unsigned maxExp
= MAX_EXP
;
343 unsigned depth
= DEPTH_DEFAULT
;
344 unsigned incr
= INCR_DEFAULT
;
345 #if CRYPTKIT_ECDSA_ENABLE
351 for(arg
=1; arg
<argc
; arg
++) {
355 loops
= atoi(&argp
[2]);
358 minExp
= atoi(&argp
[2]);
361 maxExp
= atoi(&argp
[2]);
362 if(maxExp
> MAX_EXP
) {
367 depth
= atoi(&argp
[2]);
370 incr
= atoi(&argp
[2]);
373 seed
= atoi(&argp
[2]);
377 passwdLen
= atoi(&argp
[2]);
398 time((long *)(&seed
));
401 passwdPool
= malloc(passwdLen
);
402 dataPool
= malloc(MAX_DATA_SIZE
);
404 printf("Starting %s test: loops %d seed %d elGamalOnly %d depth %d\n",
405 argv
[0], loops
, seed
, elGamalOnly
, depth
);
411 printf("attach, then CR to continue: ");
416 for(loop
=1; ; loop
++) {
418 ptext
= genData(minExp
, maxExp
, DT_Random
, &ptextLen
);
419 passwd
= genPasswd(passwdLen
);
422 * Alternate between ECDSA and ElGamal
444 printf("..loop %d text size %d ECDSA %d ECDSAVfy %d\n",
445 loop
, ptextLen
, doECDSA
, doECDSAVfy
);
447 if(doTest(ptext
, ptextLen
, passwd
, passwdLen
,
448 verbose
, quiet
, depth
, incr
,
449 doECDSA
, doECDSAVfy
)) {
453 if(loops
&& (loop
== loops
)) {
458 printf("%s test complete\n", argv
[0]);