]>
git.saurik.com Git - apple/security.git/blob - Security/libsecurity_cryptkit/ckutils/badsig/badsig.c
2 * Copyright (c) 1996-1997,2011,2014 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
26 * text size = {random, from 100 bytes to 1 megabyte, in
27 * geometrical steps, i.e. the number of
28 * bytes would be 10^r, where r is random out of
29 * {2,3,4,5,6}, plus a random integer in {0,..99}};
31 * password size = constant;
34 * text contents = {random data, random size as specified above};
35 * passsword data = random;
37 Alternate between ECDSA and ElGamal on sucessive loops:
38 * generate signature, validate;
39 * for each byte of signature {
41 * verify bad signature;
42 * restore corrupted byte;
50 #if !CRYPTKIT_HIGH_LEVEL_SIG
51 #error Can not build this program against a lib with !CRYPTKIT_HIGH_LEVEL_SIG.
57 static unsigned char *passwdPool
; /* all passwords come from here */
58 static unsigned char *dataPool
; /* plaintext comes from here */
60 #define MAX_DATA_SIZE ((1024 * 1024) + 100) /* bytes */
66 #define MIN_EXP 2 /* for data size 10**exp */
68 #define PWD_LENGTH 15 /* bytes */
69 #define DEPTH_DEFAULT FEE_DEPTH_DEFAULT
70 #define INCR_DEFAULT 1 /* munge every incr bytes */
72 ///#define DEPTH_DEFAULT FEE_DEPTH_5
74 static void usage(char **argv
)
76 printf("usage: %s [options]\n", argv
[0]);
77 printf(" Options:\n");
78 printf(" l=loops (default=%d; 0=forever)\n", LOOPS_DEF
);
79 printf(" n=minExp (default=%d)\n", MIN_EXP
);
80 printf(" x=maxExp (default=max=%d)\n", MAX_EXP
);
81 printf(" p=passwdLength (default=%d)\n", PWD_LENGTH
);
82 printf(" D=depth (default=%d)\n", DEPTH_DEFAULT
);
83 printf(" i=increment (default=%d)\n", INCR_DEFAULT
);
84 #if CRYPTKIT_ECDSA_ENABLE
85 printf(" e (ElGamal only, no ECDSA)\n");
88 printf(" v(erbose)\n");
95 * ...min <= return <= max
97 static int genRand(int min
, int max
)
100 /* note random() only yields a 31-bit number... */
102 if(max
== min
) /* avoid % 1 ! */
105 return(min
+ (random() % (max
-min
+1)));
108 static unsigned char *genPasswd(unsigned passwdLength
)
110 unsigned *ip
= (unsigned *)passwdPool
;
111 unsigned intCount
= passwdLength
/ 4;
114 unsigned residue
= passwdLength
& 0x3;
116 for (i
=0; i
<intCount
; i
++) {
119 cp
= (unsigned char *)ip
;
120 for(i
=0; i
<residue
; i
++) {
121 *cp
= (unsigned char)random();
127 * Calculate random data size, fill dataPool with that many random bytes.
136 #define MAX_OFFSET 99
138 #define MIN_ASCII ' '
139 #define MAX_ASCII '~'
141 static unsigned char *genData(unsigned minExp
,
144 unsigned *length
) // RETURNED
157 * Calculate "random" size : (10 ** (random exponent)) + random offset
159 exp
= genRand(minExp
, maxExp
);
160 offset
= genRand(MIN_OFFSET
, MAX_OFFSET
);
162 while(exp
--) { // size = 10 ** exp
169 bzero(dataPool
, size
);
174 for(i
=0; i
<size
; i
++) {
182 intCount
= size
>> 2;
183 ip
= (unsigned *)dataPool
;
184 for(i
=0; i
<intCount
; i
++) {
188 residue
= size
& 0x3;
189 cp
= (unsigned char *)ip
;
190 for(i
=0; i
<residue
; i
++) {
191 *cp
++ = (unsigned char)random();
200 static int sigError()
204 printf("Attach via debugger for more info.\n");
205 printf("a to abort, c to continue: ");
207 return (resp
[0] != 'c');
212 int doTest(unsigned char *ptext
,
214 unsigned char *passwd
,
221 int doECDSAVfy
) // ignored if doECDSASig == 0
227 unsigned char origData
;
231 pubKey
= feePubKeyAlloc();
232 frtn
= feePubKeyInitFromPrivDataDepth(pubKey
,
238 printf("feePubKeyInitFromPrivData returned %s\n",
239 feeReturnString(frtn
));
242 #if CRYPTKIT_ECDSA_ENABLE
244 frtn
= feePubKeyCreateECDSASignature(pubKey
,
250 printf("feePubKeyCreateECDSASignature returned %s\n",
251 feeReturnString(frtn
));
255 frtn
= feePubKeyVerifyECDSASignature(pubKey
,
262 frtn
= feePubKeyVerifySignature(pubKey
,
272 #endif /* CRYPTKIT_ECDSA_ENABLE */
273 frtn
= feePubKeyCreateSignature(pubKey
,
279 printf("feePubKeyCreateSignature returned %s\n",
280 feeReturnString(frtn
));
283 frtn
= feePubKeyVerifySignature(pubKey
,
290 printf("**Unexpected BAD signature\n");
293 for(byte
=0; byte
<ptextLen
; byte
+= incr
) {
294 if(!quiet
&& (verbose
|| ((byte
% LOG_FREQ
) == 0))) {
295 printf("....byte %d\n", byte
);
297 origData
= ptext
[byte
];
300 * Generate random non-zero byte
303 bits
= random() & 0xff;
307 #if CRYPTKIT_ECDSA_ENABLE
308 if(doECDSA
&& doECDSAVfy
) {
309 frtn
= feePubKeyVerifyECDSASignature(pubKey
,
318 #endif /* CRYPTKIT_ECDSA_ENABLE */
319 frtn
= feePubKeyVerifySignature(pubKey
,
325 if(frtn
== FR_Success
) {
326 printf("**Unexpected GOOD signature\n");
329 ptext
[byte
] = origData
;
331 feePubKeyFree(pubKey
);
335 int main(int argc
, char **argv
)
340 unsigned char *ptext
;
342 unsigned char *passwd
;
349 unsigned passwdLen
= PWD_LENGTH
;
350 unsigned loops
= LOOPS_DEF
;
354 unsigned minExp
= MIN_EXP
;
355 unsigned maxExp
= MAX_EXP
;
357 unsigned depth
= DEPTH_DEFAULT
;
358 unsigned incr
= INCR_DEFAULT
;
359 #if CRYPTKIT_ECDSA_ENABLE
365 for(arg
=1; arg
<argc
; arg
++) {
369 loops
= atoi(&argp
[2]);
372 minExp
= atoi(&argp
[2]);
375 maxExp
= atoi(&argp
[2]);
376 if(maxExp
> MAX_EXP
) {
381 depth
= atoi(&argp
[2]);
384 incr
= atoi(&argp
[2]);
387 seed
= atoi(&argp
[2]);
391 passwdLen
= atoi(&argp
[2]);
412 time((long *)(&seed
));
415 passwdPool
= malloc(passwdLen
);
416 dataPool
= malloc(MAX_DATA_SIZE
);
418 printf("Starting %s test: loops %d seed %d elGamalOnly %d depth %d\n",
419 argv
[0], loops
, seed
, elGamalOnly
, depth
);
425 printf("attach, then CR to continue: ");
430 for(loop
=1; ; loop
++) {
432 ptext
= genData(minExp
, maxExp
, DT_Random
, &ptextLen
);
433 passwd
= genPasswd(passwdLen
);
436 * Alternate between ECDSA and ElGamal
458 printf("..loop %d text size %d ECDSA %d ECDSAVfy %d\n",
459 loop
, ptextLen
, doECDSA
, doECDSAVfy
);
461 if(doTest(ptext
, ptextLen
, passwd
, passwdLen
,
462 verbose
, quiet
, depth
, incr
,
463 doECDSA
, doECDSAVfy
)) {
467 if(loops
&& (loop
== loops
)) {
472 printf("%s test complete\n", argv
[0]);