listcheck.c
4.79 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
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
#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>
#include <aes.h>
/*
#define DEBUG
*/
typedef struct {
BbEccSig signature; /* over all data including numFields and below */
u32 numFields;
} BbReEncryptionHead;
typedef struct {
BbContentId cid;
BbAesKey key;
u32 status;
u32 padding[2]; /* Each field must be a multiple of 16 bytes, since
they are encrypted with AES-128 */
} BbReEncryptionField;
static void
getFieldFromList(BbReEncryptionField *field, BbReEncryptionHead *pKeyList, int num, u32 bbid, BbAesKey key){
BbAesIv iv;
int i;
/* hard code iv to be bbid +i */
for(i =0 ; i < sizeof(BbAesIv)/sizeof(u32); i++){
iv[i] = htonl(bbid + i);
}
aes_SwDecrypt((u8 *) key, (u8 *)&iv,
((u8 *) pKeyList) + sizeof(BbReEncryptionHead) +
num *sizeof(BbReEncryptionField),
sizeof(BbReEncryptionField),
(u8 *) field);
}
#define GET_LIST_SIG_SIZE(__num_fields) \
(sizeof(BbReEncryptionHead) - sizeof(BbEccSig) + \
htonl(__num_fields) * sizeof(BbReEncryptionField))
int verifyEccSig(u8 *data, u32 datasize, BbEccPublicKey public_key, BbEccSig sign, u32 identity){
boolean res;
int i;
for(i=0; i< sizeof(BbEccSig)/sizeof(u32); i++){
sign[i] = htonl(sign[i]);
}
bsl_verify_ecc_sig(data, datasize, public_key, sign, &res, identity);
if(res != BSL_TRUE){
return 1;
}
return 0;
}
int
verifyList(BbReEncryptionHead *pList, BbEccPublicKey pubkey)
{
int res;
int size = GET_LIST_SIG_SIZE(pList->numFields);
#define SK_IDENTITY (0x06091968)
res = verifyEccSig(((u8 *)(pList))+sizeof(BbEccSig),
size, pubkey, pList->signature, SK_IDENTITY);
if(res != 0){
return 1;
}
return 0;
}
int asciiToBin(char *str,int bytes,u8 *buf)
{
int i,j;
char t;
for(i=0;i<bytes;i++){
t = str[2*i];
if ((t >= '0') && (t <= '9'))
j = (t - '0') << 4;
else if ((t >= 'a') && (t <= 'f'))
j = (t - 'a' + 10) << 4;
else if ((t >= 'A') && (t <= 'F'))
j = (t - 'A' + 10) << 4;
else return -1;
t = str[2*i+1];
if ((t >= '0') && (t <= '9'))
j ^= (t - '0');
else if ((t >= 'a') && (t <= 'f'))
j ^= (t - 'a' + 10);
else if ((t >= 'A') && (t <= 'F'))
j ^= (t - 'A' + 10);
else return -1;
buf[i]=j;
}
return 0;
}
int main(int argc, char **argv){
int i;
FILE *listptr;
struct stat statbuf;
int file_size;
BbEccCert bbcert;
FILE *bbcertptr;
BbEccPublicKey publickey;
u8 *list;
BbAesKey key;
u32 bbid;
BbReEncryptionField field;
BbReEncryptionHead *head;
if(argc != 4){
fprintf(stderr,"usage: %s listfile bbcertfile re-enc-key\n", argv[0]);
exit(1);
}
/* pull list data from file */
listptr = fopen(argv[1], "r");
if(listptr ==0){
fprintf(stderr,"couldnt find your list data in local directory\n");
exit(1);
}
stat(argv[1], &statbuf);
file_size = (long) statbuf.st_size;
list= (u8 *)malloc(file_size);
fread((void *)list, file_size, 1, listptr);
/* verify a sign from a BB: assumed BBs cert is ecccert.bin */
if((bbcertptr = fopen(argv[2], "r")) == 0){
fprintf(stderr,"cant open bb cert file \n");
exit(1);
}
fread((void *) &bbcert, sizeof(BbEccCert), 1, bbcertptr);
for(i=0; i< sizeof(BbEccPublicKey)/4; i++){
publickey[i] = ntohl(bbcert.publicKey[i]);
}
fclose(bbcertptr);
/* check signature on key list */
if(verifyList((BbReEncryptionHead *) list, publickey) ==0){
printf("List verified!\n");
}
else{
printf("List not verified\n");
}
/* get key */
asciiToBin(argv[3], 16, (u8 *)key);
asciiToBin(bbcert.certId.name.bbid +2, 4, (u8 *)&bbid);
printf("bbid read = %08x\n", htonl(bbid));
/* decrypt each field */
head = (BbReEncryptionHead *) list;
for(i=0; i < htonl(head->numFields); i++){
getFieldFromList(&field, head, i, htonl(bbid), key);
printf("entry %d cid = %08x key = %08x%08x%08x%08x\n",
i,
(unsigned int) htonl(field.cid),
(unsigned int) htonl(field.key[0]),
(unsigned int) htonl(field.key[1]),
(unsigned int) htonl(field.key[2]),
(unsigned int) htonl(field.key[3]));
if(htonl(field.status) ==1){
printf(" status = RECRYPT NOT REQUIRED\n");
}
else if(htonl(field.status) == 2){
printf(" status = RECRYPT COMPLETE\n");
}
else if(htonl(field.status) == 3){
printf(" status = RECRYPT INCOMPLETE\n");
}
else if(htonl(field.status) == 4){
printf(" status = RECRYPT NEW\n");
}
}
return 0;
}