bbtoolsapi.c
13.7 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
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
#include <stdlib.h>
#include <string.h>
#ifndef WIN32
#include <netinet/in.h>
#endif
#include "binary_field.h"
#include "bbtoolsapi.h"
#include "algorithms.h"
#include "bb_nn.h"
#include "aes.h"
#include "../bbboot/boot.h"
#include <PR/bbtypes.h>
#include <PR/bbcert.h>
#include <PR/bbcrl.h>
#include <PR/bbticket.h>
#include <PR/bcp.h>
#include <PR/bbvirage.h>
#include <PR/bbmetadata.h>
#include <PR/bbnand.h>
#include "../../lib/bbboot/boot.h"
/*
* to get virage2 data blob send input from the constant file, its size and
* data from RNG and bbid from database
* returns zero on success
*/
int
generateVirage2Data(u32 *constantsFile, /* big-endian */
u32 constantsFileLen,
u32 bbid,
BbEccPrivateKey privateKey, /* host-endian */
BbEccPublicKey publicKey, /* host-endian */
u32 *rand, /* host-endian */
void *viragePtr){
BbVirage2 virage;
u32 *blob = (u32 *)viragePtr;
int i, blobindex, index;
u32 temp = 0x0;
blobindex = 0;
if(constantsFile == NULL){
fprintf(stderr, "get input from constants file\n");
return -1;
}
/*load hash from blob and write in */
for(i = 0; i < 5; i++){
virage.skHash[i] = constantsFile[blobindex];
temp += ntohl(virage.skHash[i]);
blobindex++;
}
for(i = 0; i < 16; i ++){
virage.romPatch[i] = constantsFile[blobindex];
blobindex++;
temp += ntohl(virage.romPatch[i]);
}
virage.bbId = htonl(bbid);
temp += bbid;
/* write private key : assumes already 233 bits*/
for(i = 0; i < 8; i++){
virage.privateKey[i] = htonl(privateKey[i]);
temp += privateKey[i];
}
/* remove public key later */
for(i = 0; i < 16; i ++){
virage.publicKey[i] = htonl(*(publicKey + i));
temp += publicKey[i];
}
for(i = 0; i < 4; i++){
virage.bootAppKey[i] = constantsFile[blobindex];
blobindex++;
temp += ntohl(virage.bootAppKey[i]);
}
/* assign the random numbers to the AES keys */
for(i = 0; i < 4; i++){
virage.recryptListKey[i] = htonl(rand[i]);
temp += rand[i];
}
for(i = 0; i < 4; i++){
virage.appStateKey[i] = htonl(rand[i+4]);
temp += rand[i+4];
}
for(i = 0; i < 4; i++){
virage.selfMsgKey[i] = htonl(rand[i+8]);
temp += rand[i+8];
}
virage.jtagEnable = constantsFile[blobindex];
temp += ntohl(virage.jtagEnable);
/* compute checksum*/
virage.csumAdjust = htonl(VIRAGE2_CHECKSUM - ((int)temp));
/* serialise the structure
*/
index =0;
for( i =0; i < 5; i++){
blob[index] = virage.skHash[i];
index++;
}
for(i = 0; i < 16; i++){
blob[index] = virage.romPatch[i];
index++;
}
for(i = 0; i < sizeof(BbEccPublicKey)/4; i++){
blob[index] = virage.publicKey[i];
index++;
}
blob[index] = virage.bbId;
index++;
for(i =0; i < sizeof(BbEccPrivateKey)/4; i++){
blob[index] = virage.privateKey[i];
index++;
}
for(i =0; i < sizeof(BbAesKey)/4; i++){
blob[index] = virage.bootAppKey[i];
index++;
}
for(i =0; i < sizeof(BbAesKey)/4; i++){
blob[index] = virage.recryptListKey[i];
index++;
}
for(i =0; i < sizeof(BbAesKey)/4; i++){
blob[index] = virage.appStateKey[i];
index++;
}
for(i =0; i < sizeof(BbAesKey)/4; i++){
blob[index] = virage.selfMsgKey[i];
index++;
}
blob[index] = virage.csumAdjust;
index++;
blob[index] = virage.jtagEnable;
index++;
return 0;
}
/* utility to get words from byte string of 4 bytes
*/
static u32 get_word(u8 *input){
return (*(u32 *)input);
}
/* allocate for all inputs based on sizes in bbtypes.h and size of certificate
* blob
*/
int generateUnsignedRSACert(u32 certificateType, u32 sigType, u32 timestamp, u8 *subjectname, u8 * issuername, u32 *publicKey, u32 exponent, u32 *rsacert, int sizeofblob){
BbRsaCert cert;
int i, count;
if(sizeofblob < SIZE_RSA_CERTBLOB_WORDS){
return -1;
}
cert.certId.certType = htonl(certificateType);
cert.certId.sigType = htonl(sigType);
cert.certId.date = htonl(timestamp);
memcpy(cert.certId.issuer, issuername, 64);
memcpy(cert.certId.name.server, subjectname, 16);
memcpy(cert.publicKey, publicKey, 256);
cert.exponent = htonl(exponent);
for(i = 0; i < 128; i++){
cert.signature.rsa4096[i] = 0;
}
/* write to output byte array: see file format defn */
rsacert[0] = cert.certId.certType;
rsacert[1] = cert.certId.sigType;
rsacert[2] = cert.certId.date;
count = 3;
for(i = 0; i < 64; i= i + 4){
rsacert[count] = get_word(cert.certId.issuer +i);
count++;
}
/* changed to 64 */
for(i = 0; i < 16; i = i + 4){
rsacert[count] = get_word(cert.certId.name.server + i);
count++;
}
for(i = 16; i < 64; i = i+4){
rsacert[count] = 0;
count++;
}
/*
count = 23;
*/
for(i = 0; i < 64; i++){
rsacert[count] = htonl(cert.publicKey[i]);
count++;
}
/*
rsacert[87] = cert.exponent;
*/
rsacert[count] = cert.exponent;
count++;
/* overwrite this or entire space below with signature */
/*
for(i = 88; i< 88+ 64; i++){
*/
for(i = 0; i < 64; i ++){
rsacert[count] =0;
count++;
}
/* this portion remains zeros for 2048 bit signature */
/*
for(i = 152; i < 152 + 64; i++){
*/
for(i = 0; i < 64; i++){
rsacert[count] = 0;
count++;
}
return 0;
}
/* takes private key and other ID data and return cert blob
*/
int generateUnsignedBbCert(u32 certificateType,
u32 sigType,
u32 timestamp,
u8 * subjectname,
u8 * issuername,
BbEccPrivateKey privatekey,
u32 *ecccert,
int sizeofblob){
BbEccCert cert;
int i;
BSL_error error;
BbEccPublicKey publickey;
if(sizeofblob < SIZE_BB_CERTBLOB_WORDS){
return -1;
}
cert.certId.certType = htonl(certificateType);
cert.certId.sigType = htonl(sigType);
cert.certId.date = htonl(timestamp);
memset(cert.certId.issuer, 0, sizeof(BbServerName));
memset(cert.certId.name.bbid, 0, sizeof(BbName));
strncpy(cert.certId.issuer, issuername, sizeof(BbServerName));
strncpy(cert.certId.name.bbid, subjectname, sizeof(BbName));
/* get private key and create public key
*/
error = eccGenPublicKey(publickey, privatekey);
if(error != BSL_OK){
return -1;
}
for(i = 0; i < 16; i++){
cert.publicKey[i] = htonl(publickey[i]);
}
memset(&cert.signature, 0, sizeof cert.signature);
memcpy(ecccert, &cert, sizeof cert);
return 0;
}
/*
* create unsigned meta data blob
*/
/*
* create unsigned meta data blob: called by content publishing
*/
int generateUnsignedContentMetaDataHead(
u32 caCrlVersion,
u32 cpCrlVersion,
BbContentId contentId,
u32 contentSize,
u32 descFlags, /* is it common encryption
use BB_CMD_DESC_COMMON_KEY*/
u8 * commonIv, /* if we want
common encryption */
u32 *constantsFile, /* this is to get
the virage
constants file
and get common key
*/
u8 * contentKey,
u8 * contentHash,
u8 * contentIv,
/* make this opaque to the server:7 words */
u32 * contentProperties,
/*
u32 execFlags,
u32 hwAccessRights,
u32 secureKernelRights,
*/
u32 bbid, /* optional to attach to one BB*/
BbContentMetaDataHead *head){
BbAesKey commonkey;
u8 encryptResult[BB_CMD_ENCRYPTED_CHUNK_BYTES];
int err;
if((contentKey == 0)||
(contentHash == 0)||
(contentIv == 0)||
(contentProperties == 0)||
(head == 0)){
return -1;
}
/* the structure is straightforward, so fill in and return u8 *
*/
head->unusedPadding = 0;
head->caCrlVersion = htonl(caCrlVersion);
head->cpCrlVersion = htonl(cpCrlVersion);
head->id = htonl(contentId);
head->size = htonl(contentSize);
head->descFlags = htonl(descFlags);
memcpy(head->key, contentKey, sizeof head->key);
memcpy(head->hash, contentHash, sizeof head->hash);
memcpy(head->iv, contentIv, sizeof head->iv);
head->execFlags = htonl(contentProperties[0]);
head->hwAccessRights = htonl(contentProperties[1]);
head->secureKernelRights = htonl(contentProperties[2]);
head->bbid = htonl(bbid);
/*
memcpy(head->issuer, issuername, 64);
*/
/* if desc flags says common encryption (example, sysapp) do
software aes encrypt using commonIv and commonKey here.
*/
if((commonIv ==0) ||(constantsFile ==0)){
return -1;
}
memcpy(head->commonCmdIv, commonIv, sizeof head->commonCmdIv);
memcpy(commonkey, &constantsFile[BB_VIRAGE2_BOOTAPPKEY_LOC], sizeof commonkey);
err = aes_SwEncrypt((u8 *)commonkey, (u8 *)head->commonCmdIv, (u8 *) &(head->key), BB_CMD_ENCRYPTED_CHUNK_BYTES, encryptResult);
memcpy((u8 *) &(head->key), encryptResult, BB_CMD_ENCRYPTED_CHUNK_BYTES);
if(err < 0) return err;
memset(head->contentMetaDataSign, 0, sizeof head->contentMetaDataSign);
return 0;
}
/* from signed content meta data
*/
int generateUnsignedEncryptedTicket( BbContentMetaData *metadata,
u32 bbid,
BbTicketId tid,
u16 limitCode,
u16 limit,
u32 tsCrlVersion,
BbAesIv cmdIv,
BbEccPrivateKey serverprivateKey,
BbEccPublicKey serverpublicKey,
BbEccPublicKey bbpublicKey,
BbTicket *ticket){
int i;
BSL_error err;
BbAesKey sharedKey;
int orred = 0;
u8 encrypted[BB_CMD_ENCRYPTED_CHUNK_BYTES];
memcpy((u8 *)&(ticket->cmd),metadata,sizeof(BbContentMetaData));
ticket->head.tsCrlVersion = htonl(tsCrlVersion);
ticket->head.bbId = htonl(bbid);
ticket->head.tid = htons(tid);
ticket->head.code = htons(limitCode);
ticket->head.limit = htons(limit);
ticket->head.reserved = htons(0);
for( i = 0; i < 4; i++){
ticket->head.cmdIv[i] = htonl(cmdIv[i]);
}
for( i = 0; i < 16; i++){
ticket->head.serverKey[i] = htonl(serverpublicKey[i]);
}
for(i = 0; i < 64; i++){
ticket->head.ticketSign[i] = 0;
}
/* from unsigned unencrypted ticket
* to unsigned encrypted ticket
* the input private key belongs to the server (ephemeral) and
* the public key belongs to the requesting BB
*/
err = eccGenAesKey(bbpublicKey, serverprivateKey, sharedKey);
if(err != BSL_OK){
return -1;
}
/* encrypt part of Ticket and overwrite in ticketdata
*/
/* encrypt portions that need it. */
for(i= 0; i < 4; i++){
sharedKey[i] = htonl(sharedKey[i]);
/*
printf("aes key = %08x\n", sharedKey[i]);
*/
}
for(i=0; i< 4; i++){
orred |= sharedKey[i];
}
if (orred == 0x00){
/* computed key is all zero, check input keys */
return -1;
}
if(aes_SwEncrypt((u8 *)sharedKey, (u8 *)ticket->head.cmdIv, (u8 *)ticket->cmd.head.key, BB_CMD_ENCRYPTED_CHUNK_BYTES, encrypted) <0){
return -1;
}
memcpy((u8 *)&(ticket->cmd.head.key), encrypted, BB_CMD_ENCRYPTED_CHUNK_BYTES);
return 0;
}
/* return crl header
* Add the array of u32 serial numbers.
* the signature is the first entry in the header.
* to be added after making this call.
* Signature starts from version number, does not include signature
* field, includes array of serial numbers.
* if RSA2048, the sig comes first, zero
* append to fill.
*/
int generateCrlHeader(u32 versionNumber, u32 type, u32 unusedArg, u32 date, BbServerName issuer, u32 numberRevoked, u32 sigType, u32 *crlHeaderBlob, u32 *lengthBlob){
BbCrlHead *crlhead = (BbCrlHead *)crlHeaderBlob;
crlhead->versionNumber = htonl(versionNumber);
crlhead->type = htonl(type);
crlhead->date = htonl(date);
crlhead->unusedPadding = 0;
memcpy(&crlhead->issuer, issuer, sizeof(BbServerName));
crlhead->numberRevoked = htonl(numberRevoked);
crlhead->sigType = htonl(sigType);
*lengthBlob = sizeof(BbCrlHead);
return 0;
}