v2Create.c
4.86 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
/*
* example and test program for calling v2 data creation API in libcrypto
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <getopt.h>
#include <PR/bcp.h>
#include <PR/bbvirage.h>
#include <PR/bbnand.h>
#include <bbtoolsapi.h>
#include <binary_field.h>
/*
* -c <cert_out_file>
* -e <ecc_cert_out_file>
* -b <bbid>
*/
#define OPTARG "c:e:b:p:r:"
void usage()
{
fprintf(stderr,"usage: \n "
" v2Create [-b <bbid>] [-p private_key] -c <cert_out_file> -e <ecc_cert_out_file>"
"inputdatafile outputviragefile \n");
}
int main(int argc, char ** argv){
int i;
FILE *infileptr;
FILE *outfileptr;
char *infilename;
char *outfilename;
u32 *inblob;
u32 inblobsize =0;
u32 *pvt;
u32 bbid=1; /* default to 1 */
u32 randoms[12];
u32 outblob[64];
FILE *certfileptr=NULL;
FILE *ecccertfileptr=NULL;
char* privkey = 0;
u32 certificateType, sigType, timestamp, exponent;
BbServerSuffix subjectname;
BbServerName issuername;
BbEccPublicKey public;
u32 publicKey[64];
u32 rsacert[SIZE_RSA_CERTBLOB_WORDS];
u32 ecccert[SIZE_BB_CERTBLOB_WORDS];
char* recrypt = 0;
/* for getopt usage */
extern int optind, opterr, optopt;
extern char *optarg;
char c;
optind = 1;
while ((c = getopt(argc, argv, OPTARG)) != EOF) {
switch (c) {
case 'c':
certfileptr = fopen(optarg, "wb");
break;
case 'e':
ecccertfileptr = fopen(optarg, "wb");
break;
case 'b':
if(optarg[0]=='0' && optarg[1]=='x')
sscanf(((char *)optarg)+2,"%08lx",&bbid);
else
bbid = atol(optarg);
break;
case 'p':
privkey = optarg;
break;
case 'r':
recrypt = optarg;
break;
case 'h': /* Help */
default:
usage();
return 1;
}
} /* while */
if( (argc-optind)!=2){
printf("incorrect number of args!\n\n");
usage();
return 1;
}
else{
infilename = argv[optind];
outfilename = argv[optind+1];
}
infileptr = fopen(infilename, "rb");
if(infileptr == NULL){
fprintf(stderr,"first create input file example, virage.in\n");
exit(1);
}
outfileptr = fopen(outfilename, "wb");
if(infileptr == NULL){
fprintf(stderr,"first create input file example, virage.in\n");
exit(1);
}
/* load content of file into blob: max size 2K bits */
inblob = (u32 *) malloc(4 * 256);
inblobsize = fread(inblob, 4, 256, infileptr);
#ifdef DEBUG
printf("size of inblob = %d\n", inblobsize);
#endif
pvt = (u32 *) malloc(4 * 8);
/* get random numbers from RNG*/
/*
for(i = 0; i < 8; i++){
*((u32 *) (pvt + i)) = rand();
}
*/
if (privkey) {
for(i = 0; i < sizeof(BbEccPrivateKey); i++)
sscanf(privkey+8*i, "%08lx", pvt+i);
} else {
/*XXXblythe endian-dependent*/
pvt[0] = (0xa8190276);
pvt[1] = (0x7e25db17);
pvt[2] = (0x0f3449c5);
pvt[3] = (0xd94b162f);
pvt[4] = (0xa8190276);
pvt[5] = (0x7e25db17);
pvt[6] = (0x0f3449c5);
pvt[7] = (0xd94b162f);
}
/*XXX Set the public key to zero, since this is what Mfr PC does */
memset(public, 0, sizeof(public));
for(i = 0; i < 12; i++){
randoms[i] = 0x00000000;
}
if (recrypt) {
for(i = 0; i < 4; i++)
sscanf(recrypt+8*i, "%08lx", randoms+i);
}
/* first 4 are boot app key */
generateVirage2Data(inblob, inblobsize * 4, bbid, pvt, public, randoms, outblob);
/*
fwrite(&virage, sizeof(virage), 1, outfileptr);
*/
fwrite(outblob, 4, 64, outfileptr);
fclose(infileptr);
fclose(outfileptr);
/* test for RSA cert */
certificateType = BB_CERT_TYPE_SERVER;
sigType = BB_SIG_TYPE_RSA4096;
timestamp = 0x09062003;
memset(issuername, 0, sizeof issuername);
strcpy(issuername, "Root");
memset(subjectname, 0, sizeof subjectname);
strcpy(subjectname, "MSCA-00010203");
for(i =0; i< 64; i++){
publicKey[i] = rand();
}
exponent = 3;
generateUnsignedRSACert(certificateType, sigType, timestamp, subjectname, issuername, publicKey, exponent, rsacert, SIZE_RSA_CERTBLOB_WORDS);
if(certfileptr)
fwrite(rsacert, 216, 1, certfileptr);
/* ecc cert example
*/
certificateType = BB_CERT_TYPE_BB;
sigType = BB_SIG_TYPE_RSA2048;
timestamp = 0x09062003;
memset(issuername, 0, sizeof issuername);
strcpy(issuername, "Root-MSCA00010203-MS0a0b0c0d");
memset(subjectname, 0, sizeof subjectname);
sprintf(subjectname, "BB%08lx", bbid);
generateUnsignedBbCert(certificateType, sigType, timestamp, subjectname, issuername, pvt, ecccert, SIZE_BB_CERTBLOB_WORDS);
if(ecccertfileptr)
fwrite(ecccert, SIZE_BB_CERTBLOB_WORDS, 4, ecccertfileptr);
return 0;
}