-
Notifications
You must be signed in to change notification settings - Fork 3
/
7800sign.c
818 lines (649 loc) · 19.5 KB
/
7800sign.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
// Provided under the LGPL v2 license. See the included LICENSE.txt for details.
// sign7800.c
// Encrypts a digital signature for Atari 7800 cartridges.
// 2004-06-20 by Bruce Tomlin
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#define version "1.0 of 2004-06-20"
unsigned char acc[256];
unsigned char cart[65536];
const char *progname; // pointer to argv[0]
int testonly;
int writeback;
char fname[256];
FILE *f;
int starta;
int offseta;
int offsetr;
int sizea;
int sizer0;
int sizer1;
int sizer5;
int sizer7;
unsigned char *modmod;
unsigned char *addmod;
int adacmod0;
unsigned char *cmpmod;
int cpacmod0;
unsigned char *submod;
int sbacmod0;
unsigned char *mdmmod0;
int mdmmod1;
unsigned char acc[256];
unsigned char reg0[128];
unsigned char reg1[128];
unsigned char reg2[128];
unsigned char reg3[128];
unsigned char reg4[128];
unsigned char reg5[128];
unsigned char reg6[128];
unsigned char reg7[128];
unsigned char reg8[128];
unsigned char reg9[128];
unsigned char reg10[128];
unsigned char reg11[128];
unsigned char reg12[128];
unsigned char reg13[128];
unsigned char reg14[128];
unsigned char *evenregs[8] = { reg0, reg2, reg4, reg6, reg8, reg10, reg12, reg14 };
unsigned char s[] = { 0xC7, 0x65, 0xAB, 0xCA, 0xEE, 0xF7, 0x83, 0x09, // note: s and t overlap
//unsigned char t[] =
0xE1, 0xD0, 0x92, 0x67, 0x62, 0xB6, 0x72, 0x55, 0x8E, 0x91, 0xDC, 0xC5,
0x81, 0xBE, 0x78, 0x20,
0x59, 0xB7, 0xE6, 0x3D, 0x06, 0x45, 0xAF, 0xC8, 0x08, 0x31, 0x38, 0xD1,
0xFB, 0x73, 0x84, 0xA9,
0x17, 0xFC, 0x34, 0x87, 0xA3, 0x94, 0xFA, 0x90, 0xB8, 0xED, 0xCE, 0x3B,
0x5B, 0x0A, 0x43, 0xD9,
0xF3, 0x53, 0x82, 0xB3, 0x0D, 0x6D, 0x5A, 0x60, 0x9D, 0x51, 0xA7, 0xB9,
0x11, 0x10, 0xBC, 0xE4,
0x7F, 0x80, 0x41, 0xE7, 0xE3, 0xF6, 0x56, 0x26, 0x35, 0xEC, 0xD6, 0xDF,
0x0C, 0x7F, 0xF4, 0x9E,
0xAC, 0x52, 0x46, 0xEF, 0xCF, 0xBF, 0xA2, 0x3F, 0xA4, 0x13, 0x15, 0x97,
0x4A, 0x1C, 0xB0, 0x42,
0x8C, 0xB1, 0x05, 0x58, 0x80, 0x18, 0x77, 0x2B, 0x02, 0x3E, 0xA8, 0x49,
0x1A, 0x6A, 0xCB, 0x6E,
0x0B, 0x8A, 0xEB, 0xF1, 0x4F, 0x14, 0x79, 0x8B, 0xD8, 0x9F, 0x9B, 0x57,
0x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
0xD0, 0x0D, 0xBD, 0xF1,
0xB3, 0x23, 0x71, 0x32, 0x04, 0x02, 0xFD, 0x53
};
void dump (int n, unsigned char *data)
{
int i;
for (i = 0; i < n; i++)
{
if ((i & 15) == 8)
printf (" ");
else if (i & 15)
printf (" ");
printf ("%.2x", data[i]);
if ((i & 15) == 15)
printf ("\n");
}
printf ("\n");
if (n & 15)
printf ("\n");
}
//======================================================
void setregs (void) // puts shifted copies of reg0 into even regs reg2..reg14
{
int i, j, n, carry;
unsigned char *r1, *r2;
offsetr = sizer0 + 1;
reg0[0] = 0;
for (i = 0; i < 7; i++)
{
r1 = evenregs[i];
r2 = evenregs[i + 1];
carry = 0;
for (j = offsetr; j >= 0; j--)
{
n = r1[j] * 2 + carry;
r2[j] = n & 0xFF;
carry = (n >> 8) & 1;
}
}
}
void multadd (void) // acc = acc + addmod
{
int i, n, carry;
carry = 0;
for (i = offsetr; i >= 0; i--)
{
n = acc[i + adacmod0] + addmod[i] + carry;
acc[i + adacmod0] = n & 0xFF;
carry = (n >> 8) & 1;
}
while (carry)
{
n = acc[i + adacmod0] + carry;
acc[i + adacmod0] = n & 0xFF;
carry = (n >> 8) & 1;
i--;
}
}
void multiply (void) // acc = reg0 * reg1
{
int i, newoffseta;
setregs ();
offseta = sizer1 + 1;
newoffseta = offseta + offsetr;
for (i = 0; i <= newoffseta; i++)
acc[i] = 0;
adacmod0 = offseta + 1;
while (offseta--)
{
adacmod0--;
for (i = 0; i <= 7; i++)
{
if (reg1[offseta] & 1 << i)
{
addmod = evenregs[i];
multadd ();
}
}
}
offseta = newoffseta;
starta = 1;
}
void divsub (void) // acc = acc - submod
{
int i, n, borrow;
borrow = 0;
for (i = offsetr; i >= 0; i--)
{
n = acc[i + sbacmod0] - submod[i] - borrow;
acc[i + sbacmod0] = n & 0xFF;
borrow = (n >> 8) & 1;
}
while (borrow)
{
n = acc[i + sbacmod0] - borrow;
acc[i + sbacmod0] = n;
borrow = (n >> 8) & 1;
i--;
}
}
int divcmp (void) // returns 1 if acc > cmpmod
{
int i;
for (i = 0; i < offsetr; i++)
if (acc[i + cpacmod0] != cmpmod[i])
return acc[i + cpacmod0] > cmpmod[i];
return 0;
}
// this may do division, but it's only used for modulus
// support for saving the quotient has been added as comments
void divide (void) // acc = acc mod reg0
{
int i;
setregs ();
starta = offseta = sizea - sizer0;
cpacmod0 = sbacmod0 = -1;
acc[0] = 0;
while (offseta--)
{
cpacmod0++;
sbacmod0++;
for (i = 7; i >= 0; i--)
{
cmpmod = submod = evenregs[i];
if (divcmp ())
{
divsub ();
// quotient[offseta+1] |= (1 << i);
}
}
}
offseta = sizea;
}
void simplmod (void) // if acc > modmod, acc = acc - modmod
{
cmpmod = submod = modmod;
cpacmod0 = sbacmod0 = 1;
if (acc[0] == 0)
if (!divcmp ())
return;
divsub ();
}
void hseroff (void) // reg0 * reg1 mod mdmmod0
{
multiply ();
sizer0 = mdmmod1;
memcpy (reg0 + 1, mdmmod0, sizer0 + 1);
sizea = offseta;
divide ();
}
void square (void) // reg3 * reg3 mod mdmmod0
{
sizer0 = sizer7;
memcpy (reg0 + 1, reg3, sizer7 + 1);
sizer1 = sizer7;
memcpy (reg1, reg3, sizer7 + 1);
hseroff ();
memcpy (reg3, acc + starta, sizer7 + 1);
}
void power (void) // inputs reg3, reg5, reg7; output reg9
{
int byt, bit;
mdmmod0 = reg7;
mdmmod1 = sizer7;
reg9[sizer7] = 1;
memset (reg9, 0, sizer7);
byt = sizer5 + 1;
while (byt--)
{
for (bit = 0; bit <= 7; bit++)
{
if (reg5[byt] & (1 << bit))
{
sizer0 = sizer1 = sizer7;
memcpy (reg0 + 1, reg3, sizer7 + 1);
memcpy (reg1, reg9, sizer7 + 1);
hseroff ();
memcpy (reg9, acc + starta, sizer7 + 1);
}
square ();
}
}
offsetr = sizer7;
}
void hashpower (void) // reg9 = reg11 ^ reg5 mod reg7
{
memcpy (acc + 1, reg11, sizeof (n));
acc[0] = 0;
sizer0 = sizer7;
memcpy (reg0 + 1, reg7, sizer7 + 1);
sizea = sizeof (n);
divide ();
memcpy (reg3, acc + starta, sizer7 + 1);
power ();
}
void dectest (void) // decrypt hash for validation; acc = reg0 * reg1 mod n
{
sizer0 = sizeof (n) - 1;
sizer1 = sizeof (n) - 1;
multiply ();
memcpy (reg0 + 1, n, sizeof (n));
sizea = offseta;
divide ();
}
// input: cartridge hash in reg11
// output: returns 1 if success and cartridge signature in reg1
int encrypt_hash ()
{
int i, tries;
printf ("Encrypting...");
for (tries = 0; tries < 256; tries++)
{
printf (" %.2X", reg11[4]);
sizer7 = sizeof (p) - 1;
memcpy (reg7, p, sizeof (p)); // reg7 = p
sizer5 = sizeof (pexp) - 1;
memcpy (reg5, pexp, sizeof (pexp)); // reg5 = pexp
hashpower (); // reg9 = reg11 ^ reg5 mod reg7
memcpy (reg13, reg9, sizeof (p)); // reg13 = reg9
sizer7 = sizeof (q) - 1;
memcpy (reg7, q, sizeof (q)); // reg7 = q
sizer5 = sizeof (qexp) - 1;
memcpy (reg5, qexp, sizeof (qexp)); // reg5 = qexp
hashpower (); // reg9 = reg11 & reg5 mod reg7
sizer1 = sizeof (q) - 1;
memcpy (reg1, reg9, sizeof (q)); // reg1 = reg9
sizer0 = sizeof (aq) - 1;
memcpy (reg0 + 1, aq, sizeof (aq)); // reg0[1] = aq
multiply (); // acc = reg0 * reg1
memcpy (reg0 + 1, n, sizeof (n)); // reg0[1] = n
sizea = offseta;
divide (); // acc = acc mod reg0
memcpy (reg3, acc + starta, sizeof (n)); // reg3 = acc[starta]
sizer1 = sizeof (p) - 1;
memcpy (reg1, reg13, sizeof (p)); // reg1 = reg13
sizer0 = sizeof (ap) - 1;
memcpy (reg0 + 1, ap, sizeof (ap)); // reg0[1] = ap
multiply (); // acc = reg0 * reg1
memcpy (reg0 + 1, n, sizeof (n)); // reg0[1] = n
sizea = offseta;
divide (); // acc = acc mod reg0
offsetr = sizeof (n) - 1;
memmove (acc + 1, acc + starta, sizeof (n)); // acc[1] = acc[starta]
addmod = reg3;
adacmod0 = 1;
multadd (); // acc = acc + reg3
modmod = n;
simplmod (); // if acc > n, acc = acc - n
memcpy (reg0 + 1, acc + 1, sizeof (n)); // reg0[1] = acc[1]
memcpy (reg1, acc + 1, sizeof (n)); // reg1 = acc[1]
dectest (); // try decrypting this signature
// compare decrypted signature
for (i = 0; i < sizeof (n) && acc[i + starta] == reg11[i]; i++);
if (i == sizeof (n)) // if decrypted properly...
{
printf (" success!\n");
return 1; //return success
}
reg11[4]++; // try encrypting a different number
}
printf (" failed!\n");
return 0; // we've tried all possible numbers, so give up
}
void decrypt_sig (void)
{
memcpy (reg0 + 1, cart + 0xFF80, sizeof (n));
memcpy (reg1, cart + 0xFF80, sizeof (n));
dectest ();
memcpy (reg1, acc + starta, sizeof (reg1));
reg1[0] = reg1[0] & 0x07; // zero out the don't-care bits
reg1[4] = 0;
}
//======================================================
void cscheck (int page, unsigned char *perm, int *a, int *carry)
{
int i, n;
int addr;
addr = page << 8;
for (i = 0; i < 256; i++)
{
n = *a + acc[i] + *carry;
*carry = (n >> 8) & 1;
n = (n & 0xFF) + cart[addr + i] + *carry;
*a = perm[n & 0xFF];
acc[i] = *a & 0xFF;
*carry = (n >> 8) & 1;
}
}
void csrotate (int *carry)
{
int i, n;
for (i = 0; i < 256; i++)
{
n = (acc[i] << 1) + *carry;
acc[i] = n & 255;
*carry = (n >> 8) & 1;
}
}
void hashcart (void)
{
int i, a, carry, startpage;
startpage = cart[0xFFF9] & 0xF0;
memcpy (acc, cart + 0xFF00, 256); // copy FFxx page
memset (acc + 0x80, 0, 0x78); // zero out signature zone
a = 0;
carry = 1;
for (i = startpage; i <= 0xFE; i++)
{
cscheck (i, s, &a, &carry); // do s permutation
carry = 0;
}
carry = 1;
csrotate (&carry);
csrotate (&carry);
for (i = 0xFE; i >= startpage; i--)
{
cscheck (i, s + 8, &a, &carry); // do t permutation
carry = 1;
}
for (i = 0; i <= 0x77; i++) // xor the whole mess together
reg11[i] = acc[i] ^ acc[i + 0x50] ^ acc[i + 0x88];
reg11[0] = reg11[0] & 0x07; // zero out the don't-care bits
reg11[4] = 0;
}
//======================================================
void usage (void)
{
fprintf (stderr, "Atari 7800 digital signature generator - version %s\n", version);
fprintf (stderr, "by Bruce Tomlin");
fprintf (stderr, "\n");
fprintf (stderr, "Usage:\n");
fprintf (stderr, " %s [options] image\n", progname);
fprintf (stderr, "\n");
fprintf (stderr, "Options:\n");
fprintf (stderr, " -t test existing cartridge signature only\n");
fprintf (stderr, " -w write back update signature to cartridge\n");
exit (1);
}
void getopts (int argc, char *const argv[])
{
int ch;
while ((ch = getopt (argc, argv, "tw?")) != -1)
{
switch (ch)
{
case 't':
testonly = 1;
break;
case 'w':
writeback = 1;
break;
case '?':
default:
usage ();
}
}
argc -= optind;
argv += optind;
// now argc is the number of remaining arguments
// and argv[0] is the first remaining argument
if (argc != 1)
usage ();
strncpy (fname, argv[0], 255);
// check for conflicting options
if (testonly && writeback)
usage ();
// note: this won't work if there's a single-char filename in the current directory!
if (fname[0] == '?' && fname[1] == 0)
usage ();
}
int main (int argc, char *const argv[])
{
int i, valid;
long fsize;
setvbuf (stdout, NULL, _IONBF, 0); // turn off C library file buffering
progname = argv[0];
testonly = 0;
writeback = 0;
memset (fname, 0, sizeof (fname));
getopts (argc, argv);
f = fopen (fname, "rb");
if (!f)
{
printf ("Can't open '%s'!\n", fname);
exit (1);
}
// get size of file
fseek (f, 0, SEEK_END);
fsize = ftell (f);
fseek (f, 0, SEEK_SET);
if (((fsize & 0xFFF) != 0) && ((fsize & 0x0FFF) != 128))
{
printf ("Cartridge size not a multiple of 4K bytes!\n");
exit (1);
}
if (fsize < 0x1000)
{
printf ("Cartridge data file must be at least 4K!\n");
exit (1);
}
// skip to last 48K of file
if (fsize > 0xC000)
{
fseek (f, fsize - 0xC000, SEEK_SET);
fsize = 0xC000;
}
// read cartridge data
i = fread (cart + 0x10000 - fsize, 1, fsize, f);
fclose (f);
printf ("Read $%.4X bytes of cartridge data.\n", i);
// perform simple validaitions
if ((cart[0xFFF8] & 0xF1) != 0xF1)
{
printf ("Invalid byte at $FFF8, should be $FF!\n");
exit (1);
}
if (((cart[0xFFF9] & 0xF0) << 8) < 0x10000 - fsize)
{
printf ("Invalid byte at $FFF9, hash space larger than cartridge image!\n");
exit (1);
}
if ((cart[0xFFF9] & 0x0B) != 3)
{
printf ("Invalid byte at $FFF9, low nibble should be 3 or 7!\n");
exit (1);
}
if (cart[0xFFFD] < (cart[0xFFF9] & 0xF0))
{
printf ("Cartridge reset vector points outside hashed area!\n");
exit (1);
}
printf ("Cartridge hash area is from $%.4X to $FFFF.\n", (cart[0xFFF9] & 0xF0) << 8);
decrypt_sig ();
hashcart ();
for (i = 0; i <= 0x77 && reg1[i] == reg11[i]; i++);
valid = (i == 0x78);
if (valid)
printf ("Cartridge signature for '%s' is valid!\n", fname);
else
{
for (i = 0; i < 0x78 && ((cart[0xFF80 + i] == 0) || (cart[0xFF80 + i] == 0xff)); i++);
if (i == 0x78)
printf ("Cartridge signature for '%s' appears to be empty.\n", fname);
else
printf ("Cartridge signature for '%s' is not valid.\n", fname);
}
if (!testonly)
{
if (encrypt_hash ())
{
printf ("\nA valid cartridge signature is:\n");
dump (120, reg1);
if (writeback)
{
if (valid)
{
printf ("Cartridge already has a valid signature, not re-writing file.");
}
else
{
f = fopen (fname, "rb+");
if (!f)
{
printf ("Can't open '%s' for writing!\n", fname);
exit (1);
}
// get size of file
fseek (f, 0, SEEK_END);
fsize = ftell (f);
fseek (f, 0, SEEK_SET);
fseek (f, fsize - 128, SEEK_SET); // seek to start of signature area
i = fwrite (reg1, 1, 120, f); // write signature
printf ("Wrote back %d bytes to '%s'.\n", i, fname);
fclose (f);
}
}
}
}
else
exit (1);
return 0;
}