gen_flash_file.c
3.34 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
#include <stdlib.h>
#include <stdio.h>
#include "pi_util.h"
#include "atb.h"
#include "ecc/pi_ecc.h"
/*
* Generate 64MB file to initialize flash with randoms
*
* Format has 512B 16B 512B 16B ...
*
*/
/* 64MB data file */
#define FLASH_SIZE 0x4000000
//#define FLASH_SIZE 0x800
void flashInit(u8 *key, u8 *iv, u8 *data, int num_kbytes, FILE *fdata);
int main(int argc,char *argv[])
{
int i,j;
FILE *fdata;
u8 expkey[44*4],*indata=NULL;
u8 iv[32];
unsigned char key[] = {
0x10,0x11,0x12,0x13,
0x14,0x15,0x16,0x17,
0x18,0x19,0x1a,0x1b,
0x1c,0x1d,0x1e,0x1f
};
unsigned char first_iv[] = {
0x10,0x11,0x12,0x13,
0x14,0x15,0x16,0x17,
0x18,0x19,0x1a,0x1b,
0x1c,0x1d,0x1e,0x1f
};
if(argc!=2){
printf("Need one argument for output filename\n");
exit(1);
}
if ((fdata=fopen(argv[1],"w"))==0) {
printf("cannot open %s\n",argv[1]);
exit(1);
}
aesKeyExpand(key,expkey);
/*
* setup PI_FLASH_CTRL for ATB DMA
* (assumes PI_FLASH_CONF already initialized)
*/
/*
* setup AES for ATB DMA
*/
/* write encrypted data to buffer */
indata = (u8 *)malloc(sizeof(u8) *64*1024*1024);
for(i=0;i<64*1024*1024;i+=4){
// *((u32 *)(indata+i))= (u32) i;
*((u32 *)(indata+i))= (u32) rand();
}
/*initialise flash according to atb, 16K repeating pattern, 64MB flash
(input is number of kilo bytes) */
for(i=0;i<16;i++){
iv[i] = first_iv[i];
}
flashInit(key, iv, indata, 64*1024, fdata);
/*
for(i=0;i<FLASH_SIZE/512;i++){
fprintf(fdata,"// page %d\n",i);
for(j=0;j<528;j++){
fprintf(fdata,"%02x ",(unsigned char)rand());
if((j&0xf)==0xf)
fprintf(fdata,"\n");
}
fprintf(fdata,"\n");
}
*/
fclose(fdata);
free(indata);
return 0;
}
/* initialisation of flash contents with encrypted known content.
key is allocated and expanded and passed to the function below to create
the encrypted data . Last is size of flash to fill, data_size is in bytes*/
void flashInit(u8 *key, u8 *iv, u8 *data, int num_kbytes, FILE *fdata){
u8 encdata[512];
u8 writedata[512];
u8 indata[512];
u8 ecc[16];
u32 num_pages_in_data_block;
u32 num_AES_blocks_in_page;
int i,j,k;
num_pages_in_data_block = num_kbytes*1024/512;
num_AES_blocks_in_page = 512/16;
for(i=0;i<num_pages_in_data_block; i++){
/*load page data */
for(j=0; j<512;j++){
indata[j] = data[512*i + j];
}
/*encrypt a page*/
aesEncrypt(key, iv, indata, 512, encdata);
/*last 128 bits of data is IV */
for(k=0;k<16; k++){
iv[k] = encdata[512-16+k];
}
for(k=0; k< 512; k++){
writedata[k] = encdata[k];
}
/* this is to make it work with later tests: write page number */
fprintf(fdata,"// page %d\n",i);
/* save writedata in file */
for(j=0;j<512;j++){
fprintf(fdata,"%02x ",(unsigned char)writedata[j]);
if((j&0xf)==0xf)
fprintf(fdata,"\n");
}
/* calculate and save ecc */
calculate_ecc(encdata,ecc+13);
calculate_ecc(encdata+256,ecc+8);
for(j=0;j<16;j++){
fprintf(fdata, "%02x ",(unsigned char)ecc[j]);
}
fprintf(fdata, "\n");
fflush(fdata);
}
}