sigadd.c
3.72 KB
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
#include <openssl/rsa.h>
#include <openssl/bn.h>
#include <stdlib.h>
#include <string.h>
#include <sha1.h>
#include <PR/bbmetadata.h>
#include <PR/bbcert.h>
#include <stdlib.h>
#include <bb_nn.h>
#include <PR/bbcrl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <netinet/in.h>
#include <algorithms.h>
/*
#define DEBUG
*/
#define FILE_SIZE (16*1024)
typedef struct {
char name[12];
BbEccSig sig;
} sig_entry_t;
/* return zero if state file name not found */
int lookupSig(FILE *sigptr, char *statename, BbEccSig sig, sig_entry_t *sigret){
int i;
sig_entry_t sigentry;
int num_compare;
char charac;
if(strchr(statename, 'p') != NULL){
/* Controller pak : .pak file */
num_compare = 1;
charac= 'p';
}
else{
/* default .sta file */
num_compare = 8;
charac= 's';
}
while(fread((void *) &sigentry, sizeof(sig_entry_t), 1, sigptr) ==1){
if((strncmp(sigentry.name, statename, num_compare) == 0)
&&(strchr(sigentry.name+8, charac) != NULL)){
printf("state found = %s\n", sigentry.name);
memcpy(sig, sigentry.sig, sizeof(BbEccSig));
memcpy(sigret, &sigentry, sizeof(sig_entry_t));
for(i=0; i< sizeof(BbEccSig)/4; i++){
sig[i] = ntohl(sig[i]);
}
return 1;
}
}
return 0;
}
int main(int argc, char **argv){
int i;
unsigned char hash_data[20];
SHA1Context sha;
u8 *stateBlob;
FILE *statedataptr;
FILE *sigptr;
sig_entry_t sigentry;
struct stat statbuf;
int state_size;
BbEccSig sig;
unsigned char zero;
FILE *testptr;
if(argc != 3){
fprintf(stderr,"usage: %s statefile signaturefile\n", argv[0]);
exit(1);
}
/* pull state data from file */
statedataptr = fopen(argv[1], "r");
if(statedataptr ==0){
fprintf(stderr,"couldnt find your state data in local directory\n");
exit(1);
}
/* read in state data */
stat(argv[1], &statbuf);
state_size = (long) statbuf.st_size;
stateBlob= malloc(state_size);
fread(stateBlob, state_size, 1, statedataptr);
/* compute hash */
SHA1Reset(&sha);
SHA1Input(&sha, ((u8 *)(stateBlob)),state_size);
SHA1Result(&sha, hash_data);
if(statedataptr) fclose(statedataptr);
if((sigptr = fopen(argv[2], "r")) == 0){
fprintf(stderr,"cant open bb sig file\n");
exit(1);
}
/* look up signature corresponding to the file */
if( lookupSig(sigptr, argv[1], sig, &sigentry) ==0){
fprintf(stderr, "Lookup failed: no signature\n");
exit(1);
}
/* write a whole sig.db with that sig */
testptr = fopen("sig.db.1", "w");
for(i =0; i < FILE_SIZE/sizeof(sig_entry_t); i++){
fwrite(&sigentry, sizeof(sig_entry_t), 1, testptr);
}
zero = 0x00;
for(i= (FILE_SIZE/sizeof(sig_entry_t)) *sizeof(sig_entry_t); i< FILE_SIZE; i++){
fwrite(&zero, 1, sizeof(unsigned char), testptr);
}
fclose(testptr);
/* write a sig.db with first entry + random state name */
testptr = fopen("sig.db.2", "w");
fwrite(&sigentry, sizeof(sig_entry_t), 1, testptr);
/* change the name */
sigentry.name[0] += 1;
for(i =0; i < FILE_SIZE/sizeof(sig_entry_t)-1; i++){
fwrite(&sigentry, sizeof(sig_entry_t), 1, testptr);
}
zero = 0x00;
for(i= (FILE_SIZE/sizeof(sig_entry_t)) *sizeof(sig_entry_t); i< FILE_SIZE; i++){
fwrite(&zero, 1, sizeof(unsigned char), testptr);
}
fclose(testptr);
/* write a whole sig.db with some random state name */
testptr = fopen("sig.db.3", "w");
sigentry.name[0] +=1;
for(i =0; i < FILE_SIZE/sizeof(sig_entry_t); i++){
fwrite(&sigentry, sizeof(sig_entry_t), 1, testptr);
}
zero = 0x00;
for(i= (FILE_SIZE/sizeof(sig_entry_t)) *sizeof(sig_entry_t); i< FILE_SIZE; i++){
fwrite(&zero, 1, sizeof(unsigned char), testptr);
}
fclose(testptr);
return 0;
}