OpenCores
URL https://opencores.org/ocsvn/apbtoaes128/apbtoaes128/trunk

Subversion Repositories apbtoaes128

[/] [apbtoaes128/] [trunk/] [pli/] [bfm_cbc/] [aes_bfm_key_generation_ccfie_cbc.h] - Blame information for rev 18

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 5 redbear
//////////////////////////////////////////////////////////////////
2
////
3
////
4
////    AES CORE BLOCK
5
////
6
////
7
////
8
//// This file is part of the APB to AES128 project
9
////
10
//// http://www.opencores.org/cores/apbtoaes128/
11
////
12
////
13
////
14
//// Description
15
////
16
//// Implementation of APB IP core according to
17
////
18
//// aes128_spec IP core specification document.
19
////
20
////
21
////
22
//// To Do: Things are right here but always all block can suffer changes
23
////
24
////
25
////
26
////
27
////
28
//// Author(s): - Felipe Fernandes Da Costa, fefe2560@gmail.com
29
////
30
///////////////////////////////////////////////////////////////// 
31
////
32
////
33
//// Copyright (C) 2009 Authors and OPENCORES.ORG
34
////
35
////
36
////
37
//// This source file may be used and distributed without
38
////
39
//// restriction provided that this copyright statement is not
40
////
41
//// removed from the file and that any derivative work contains
42
//// the original copyright notice and the associated disclaimer.
43
////
44
////
45
//// This source file is free software; you can redistribute it
46
////
47
//// and/or modify it under the terms of the GNU Lesser General
48
////
49
//// Public License as published by the Free Software Foundation;
50
//// either version 2.1 of the License, or (at your option) any
51
////
52
//// later version.
53
////
54
////
55
////
56
//// This source is distributed in the hope that it will be
57
////
58
//// useful, but WITHOUT ANY WARRANTY; without even the implied
59
////
60
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
61
////
62
//// PURPOSE. See the GNU Lesser General Public License for more
63
//// details.
64
////
65
////
66
////
67
//// You should have received a copy of the GNU Lesser General
68
////
69
//// Public License along with this source; if not, download it
70
////
71
//// from http://www.opencores.org/lgpl.shtml
72
////
73
////
74
///////////////////////////////////////////////////////////////////
75
static int aes_bfm_key_generation_ccfie_cbc_calltf(char*user_data)
76
{
77
 
78
        vpiHandle PRESETn = vpi_handle_by_name("AES_GLADIC_tb.PRESETn", NULL);
79
        vpiHandle PWDATA = vpi_handle_by_name("AES_GLADIC_tb.PWDATA", NULL);
80
        vpiHandle PENABLE = vpi_handle_by_name("AES_GLADIC_tb.PENABLE", NULL);
81
        vpiHandle PSEL = vpi_handle_by_name("AES_GLADIC_tb.PSEL", NULL);
82
        vpiHandle PWRITE = vpi_handle_by_name("AES_GLADIC_tb.PWRITE", NULL);
83
        vpiHandle PADDR = vpi_handle_by_name("AES_GLADIC_tb.PADDR", NULL);
84
        vpiHandle PRDATA = vpi_handle_by_name("AES_GLADIC_tb.PRDATA", NULL);
85
        vpiHandle PREADY = vpi_handle_by_name("AES_GLADIC_tb.PREADY", NULL);
86
        vpiHandle PSLVERR = vpi_handle_by_name("AES_GLADIC_tb.PSLVERR", NULL);
87
        vpiHandle int_ccf = vpi_handle_by_name("AES_GLADIC_tb.int_ccf", NULL);
88
        vpiHandle int_err = vpi_handle_by_name("AES_GLADIC_tb.int_err", NULL);
89
        vpiHandle dma_req_wr = vpi_handle_by_name("AES_GLADIC_tb.dma_req_wr", NULL);
90
        vpiHandle dma_req_rd = vpi_handle_by_name("AES_GLADIC_tb.dma_req_rd", NULL);
91
 
92
        std::random_device rd;
93
        std::uniform_int_distribution<long int> data_in(0,4294967295);
94
 
95
        v_ecb.format=vpiIntVal;
96
 
97
        vpi_get_value(PRESETn, &v_ecb);
98
 
99
 
100
        //printf("%i\n",STATE);
101
 
102
 
103
        if(type_bfm == CBC_KEY_GEN_CCFIE && v_ecb.value.integer == 1)
104
        {
105
 
106
                switch(STATE)
107
                {
108
 
109
                  case IDLE:
110
 
111
                                if(PACKETS_GENERATED >= MAX_ITERATIONS)
112
                                {
113
 
114
                                        STATE = IDLE;
115
                                        type_bfm = 0;
116
 
117
                                }else
118
                                {
119
                                        STATE = WRITE;
120
 
121
                                        counter = 0;
122
 
123
 
124
                                        v_ecb.value.integer = 0;
125
                                        vpi_put_value(PENABLE, &v_ecb, NULL, vpiNoDelay);
126
 
127
                                        v_ecb.value.integer = vector_address[0];
128
                                        vpi_put_value(PADDR, &v_ecb, NULL, vpiNoDelay);
129
 
130
                                        v_ecb.value.integer = 0;
131
                                        vpi_put_value(PWDATA, &v_ecb, NULL, vpiNoDelay);
132
 
133
                                        v_ecb.value.integer = 1;
134
                                        vpi_put_value(PWRITE, &v_ecb, NULL, vpiNoDelay);
135
 
136
                                        v_ecb.value.integer = 1;
137
                                        vpi_put_value(PSEL, &v_ecb, NULL, vpiNoDelay);
138
                                }
139
 
140
                  break;
141
 
142
                  case WRITE:
143
                                if(counter == 0)
144
                                {
145
 
146
 
147
                                        counter_write++;
148
                                        counter++;
149
 
150
                                        v_ecb.value.integer = 1;
151
                                        vpi_put_value(PENABLE, &v_ecb, NULL, vpiNoDelay);
152
 
153
 
154
                                }else if(counter == 1)
155
                                {
156
 
157
                                        v_ecb.value.integer = 0;
158
                                        vpi_put_value(PENABLE, &v_ecb, NULL, vpiNoDelay);
159
 
160
 
161
 
162
                                        if(counter_write < 9)
163
                                        {
164
 
165
                                                v_ecb.value.integer = vector_address[counter_write];
166
                                                vpi_put_value(PADDR, &v_ecb, NULL, vpiNoDelay);
167
 
168
                                                t_ecb.type = vpiScaledRealTime;
169
                                                t_ecb.real = 0;
170
                                                v_ecb.format=vpiIntVal;
171
 
172
                                                if(FIPS_ENABLE == FIPS)
173
                                                {
174
 
175
                                                        if(vector_address[counter_write] == ADDR_AES_KEYR3)
176
                                                        {
177
                                                                a = a | KEY_FIPS_CBC_NOT_DERIVATED[0];
178
                                                                a = a << 8;
179
                                                                a = a | KEY_FIPS_CBC_NOT_DERIVATED[1];
180
                                                                a = a << 8;
181
                                                                a = a | KEY_FIPS_CBC_NOT_DERIVATED[2];
182
                                                                a = a << 8;
183
                                                                a = a | KEY_FIPS_CBC_NOT_DERIVATED[3];
184
                                                                v_ecb.value.integer = a;
185
                                                        }
186
 
187
 
188
                                                        if(vector_address[counter_write] == ADDR_AES_KEYR2)
189
                                                        {
190
                                                                b = b | KEY_FIPS_CBC_NOT_DERIVATED[4];
191
                                                                b = b << 8;
192
                                                                b = b | KEY_FIPS_CBC_NOT_DERIVATED[5];
193
                                                                b = b << 8;
194
                                                                b = b | KEY_FIPS_CBC_NOT_DERIVATED[6];
195
                                                                b = b << 8;
196
                                                                b = b | KEY_FIPS_CBC_NOT_DERIVATED[7];
197
                                                                v_ecb.value.integer = b;
198
                                                        }
199
 
200
                                                        if(vector_address[counter_write] == ADDR_AES_KEYR1)
201
                                                        {
202
 
203
                                                                c = c | KEY_FIPS_CBC_NOT_DERIVATED[8];
204
                                                                c = c << 8;
205
                                                                c = c | KEY_FIPS_CBC_NOT_DERIVATED[9];
206
                                                                c = c << 8;
207
                                                                c = c | KEY_FIPS_CBC_NOT_DERIVATED[10];
208
                                                                c = c << 8;
209
                                                                c = c | KEY_FIPS_CBC_NOT_DERIVATED[11];
210
                                                                v_ecb.value.integer = c;
211
 
212
                                                        }
213
 
214
                                                        if(vector_address[counter_write] == ADDR_AES_KEYR0)
215
                                                        {
216
                                                                d = d | KEY_FIPS_CBC_NOT_DERIVATED[12];
217
                                                                d = d << 8;
218
                                                                d = d | KEY_FIPS_CBC_NOT_DERIVATED[13];
219
                                                                d = d << 8;
220
                                                                d = d | KEY_FIPS_CBC_NOT_DERIVATED[14];
221
                                                                d = d << 8;
222
                                                                d = d | KEY_FIPS_CBC_NOT_DERIVATED[15];
223
                                                                v_ecb.value.integer = d;
224
                                                        }
225
 
226
 
227
                                                        if(vector_address[counter_write] == ADDR_AES_IVR3)
228
                                                        {
229
                                                                a = a | IV_FIPS_CBC_NOT_DERIVATED[0];
230
                                                                a = a << 8;
231
                                                                a = a | IV_FIPS_CBC_NOT_DERIVATED[1];
232
                                                                a = a << 8;
233
                                                                a = a | IV_FIPS_CBC_NOT_DERIVATED[2];
234
                                                                a = a << 8;
235
                                                                a = a | IV_FIPS_CBC_NOT_DERIVATED[3];
236
                                                                v_ecb.value.integer = a;
237
                                                        }
238
 
239
 
240
                                                        if(vector_address[counter_write] == ADDR_AES_IVR2)
241
                                                        {
242
                                                                b = b | IV_FIPS_CBC_NOT_DERIVATED[4];
243
                                                                b = b << 8;
244
                                                                b = b | IV_FIPS_CBC_NOT_DERIVATED[5];
245
                                                                b = b << 8;
246
                                                                b = b | IV_FIPS_CBC_NOT_DERIVATED[6];
247
                                                                b = b << 8;
248
                                                                b = b | IV_FIPS_CBC_NOT_DERIVATED[7];
249
                                                                v_ecb.value.integer = b;
250
                                                        }
251
 
252
                                                        if(vector_address[counter_write] == ADDR_AES_IVR1)
253
                                                        {
254
 
255
                                                                c = c | IV_FIPS_CBC_NOT_DERIVATED[8];
256
                                                                c = c << 8;
257
                                                                c = c | IV_FIPS_CBC_NOT_DERIVATED[9];
258
                                                                c = c << 8;
259
                                                                c = c | IV_FIPS_CBC_NOT_DERIVATED[10];
260
                                                                c = c << 8;
261
                                                                c = c | IV_FIPS_CBC_NOT_DERIVATED[11];
262
                                                                v_ecb.value.integer = c;
263
 
264
                                                        }
265
 
266
                                                        if(vector_address[counter_write] == ADDR_AES_IVR0)
267
                                                        {
268
                                                                d = d | IV_FIPS_CBC_NOT_DERIVATED[12];
269
                                                                d = d << 8;
270
                                                                d = d | IV_FIPS_CBC_NOT_DERIVATED[13];
271
                                                                d = d << 8;
272
                                                                d = d | IV_FIPS_CBC_NOT_DERIVATED[14];
273
                                                                d = d << 8;
274
                                                                d = d | IV_FIPS_CBC_NOT_DERIVATED[15];
275
                                                                v_ecb.value.integer = d;
276
                                                        }
277
 
278
 
279
 
280
                                                }else if(FIPS_ENABLE == RANDOM_DATA)
281
                                                {
282
                                                        v_ecb.value.integer = data_in(rd);
283
                                                }
284
 
285
                                                //vpi_put_value(PWDATA, &v_ecb, NULL, vpiNoDelay);                                              
286
                                                vpi_put_value(PWDATA, &v_ecb, &t_ecb, vpiTransportDelay);
287
 
288
 
289
                                                a = 0;
290
                                                b = 0;
291
                                                c = 0;
292
                                                d = 0;
293 15 redbear
                                        }else if(counter_write == 9)
294 5 redbear
                                        {
295
 
296 9 redbear
                                                v_ecb.value.integer = ADDR_AES_CR;
297 5 redbear
                                                vpi_put_value(PADDR, &v_ecb, NULL, vpiNoDelay);
298
 
299
                                                t_ecb.type = vpiScaledRealTime;
300
                                                t_ecb.real = 0;
301
                                                v_ecb.format=vpiIntVal;
302 12 redbear
                                                v_ecb.value.integer = 553;
303 5 redbear
                                                vpi_put_value(PWDATA, &v_ecb, &t_ecb, vpiTransportDelay);
304
 
305
                                        }
306
 
307
 
308
                                        v_ecb.value.integer = 1;
309
                                        vpi_put_value(PSEL, &v_ecb, NULL, vpiNoDelay);
310
 
311
                                        counter=0;
312
                                }
313
 
314
                                if(counter_write == 10)
315
                                {
316
                                        counter_write = 0;
317
                                        counter_read  = 0;
318
 
319
                                        STATE =WAIT_SR;
320
 
321
 
322
                                }
323
 
324
                  break;
325
 
326
                  case WAIT_SR:
327
 
328
                                v_ecb.value.integer = ADDR_AES_SR;
329
                                vpi_put_value(PADDR, &v_ecb, NULL, vpiNoDelay);
330
 
331
                                v_ecb.value.integer = 0;
332
                                vpi_put_value(PWRITE, &v_ecb, NULL, vpiNoDelay);
333
 
334
                                if(counter == 0)
335
                                {
336
 
337
                                        counter++;
338
 
339
                                        v_ecb.value.integer = 1;
340
                                        vpi_put_value(PENABLE, &v_ecb, NULL, vpiNoDelay);
341
 
342
 
343
                                }else if(counter == 1)
344
                                {
345
                                        v_ecb.value.integer = 0;
346
                                        vpi_put_value(PENABLE, &v_ecb, NULL, vpiNoDelay);
347
 
348
                                        counter=0;
349
                                }
350
 
351
                                v_ecb.value.integer = 0;
352
                                vpi_get_value(PRDATA,&v_ecb);
353
 
354
                                if(v_ecb.value.integer == 1)
355
                                {
356
                                        STATE = READ_KEY_GEN;
357
 
358 9 redbear
                                        //t_ecb.type = vpiScaledRealTime;
359
                                        //t_ecb.real = 10;
360
                                        //v_ecb.format=vpiIntVal;
361
                                        //v_ecb.value.integer = ADDR_AES_KEYR3;
362
                                        //vpi_put_value(PADDR, &v_ecb, &t_ecb, vpiTransportDelay);
363 5 redbear
                                }
364
 
365
 
366
                  break;
367
 
368
 
369
                 case READ_KEY_GEN:
370
 
371
 
372
 
373
                                if(counter == 0)
374
                                {
375
 
376
                                        v_ecb.value.integer = 1;
377
                                        vpi_put_value(PENABLE, &v_ecb, NULL, vpiNoDelay);
378
 
379
                                        counter_read++;
380
                                        counter++;
381
 
382
 
383
 
384
                                }else if(counter == 1)
385
                                {
386
 
387 9 redbear
                                        v_ecb.value.integer = 0;
388
                                        vpi_put_value(PENABLE, &v_ecb, NULL, vpiNoDelay);
389
 
390
                                        if(counter_read < 4)
391 5 redbear
                                        {
392 9 redbear
                                                v_ecb.value.integer = ADDR_AES_DOUTR;
393
                                                vpi_put_value(PADDR, &v_ecb, NULL, vpiNoDelay);
394
                                        }
395
 
396
                                        if(counter_read == 4)
397
                                        {
398 5 redbear
                                                v_ecb.value.integer = ADDR_AES_KEYR3;
399 9 redbear
                                                vpi_put_value(PADDR, &v_ecb, NULL, vpiNoDelay);
400 5 redbear
                                        }
401
 
402 9 redbear
                                        if(counter_read == 5)
403 5 redbear
                                        {
404
                                                v_ecb.value.integer = ADDR_AES_KEYR2;
405 9 redbear
                                                vpi_put_value(PADDR, &v_ecb, NULL, vpiNoDelay);
406 5 redbear
                                        }
407
 
408
 
409 9 redbear
                                        if(counter_read == 6)
410 5 redbear
                                        {
411
                                                v_ecb.value.integer = ADDR_AES_KEYR1;
412 9 redbear
                                                vpi_put_value(PADDR, &v_ecb, NULL, vpiNoDelay);
413 5 redbear
                                        }
414
 
415 9 redbear
                                        if(counter_read == 7)
416 5 redbear
                                        {
417
                                                v_ecb.value.integer = ADDR_AES_KEYR0;
418 9 redbear
                                                vpi_put_value(PADDR, &v_ecb, NULL, vpiNoDelay);
419 5 redbear
                                        }
420 9 redbear
 
421
                                        if(counter_read == 8)
422 5 redbear
                                        {
423
                                                v_ecb.value.integer = ADDR_AES_IVR3;
424 9 redbear
                                                vpi_put_value(PADDR, &v_ecb, NULL, vpiNoDelay);
425 5 redbear
                                        }
426
 
427 9 redbear
                                        if(counter_read == 9)
428 5 redbear
                                        {
429
                                                v_ecb.value.integer = ADDR_AES_IVR2;
430 9 redbear
                                                vpi_put_value(PADDR, &v_ecb, NULL, vpiNoDelay);
431 5 redbear
                                        }
432
 
433 9 redbear
                                        if(counter_read == 10)
434 5 redbear
                                        {
435
                                                v_ecb.value.integer = ADDR_AES_IVR1;
436 9 redbear
                                                vpi_put_value(PADDR, &v_ecb, NULL, vpiNoDelay);
437 5 redbear
                                        }
438
 
439 9 redbear
                                        if(counter_read == 11)
440 5 redbear
                                        {
441
                                                v_ecb.value.integer = ADDR_AES_IVR0;
442 9 redbear
                                                vpi_put_value(PADDR, &v_ecb, NULL, vpiNoDelay);
443 5 redbear
                                        }
444
 
445
                                        counter = 0;
446
                                }
447
 
448 9 redbear
                                if(counter_read == 12)
449 5 redbear
                                {
450
                                        STATE = RESET_SR;
451
                                        counter_write = 0;
452
                                        counter_read  = 0;
453
                                }
454
 
455
                 break;
456
 
457
                case RESET_SR:
458
 
459
                                v_ecb.value.integer = 1;
460
                                vpi_put_value(PWRITE, &v_ecb, NULL, vpiNoDelay);
461
 
462
                                v_ecb.value.integer = 1;
463
                                vpi_put_value(PSEL, &v_ecb, NULL, vpiNoDelay);
464
 
465
                                v_ecb.value.integer = 0;
466
                                vpi_put_value(PADDR, &v_ecb, NULL, vpiNoDelay);
467
 
468
                                v_ecb.value.integer = 128;
469
                                vpi_put_value(PWDATA, &v_ecb, NULL, vpiNoDelay);
470
 
471
 
472
                                if(counter == 0)
473
                                {
474
 
475
                                        counter_write++;
476
                                        counter++;
477
 
478
                                        v_ecb.value.integer = 1;
479
                                        vpi_put_value(PENABLE, &v_ecb, NULL, vpiNoDelay);
480
 
481
                                }else if(counter == 1)
482
                                {
483
 
484
                                        v_ecb.value.integer = 0;
485
                                        vpi_put_value(PENABLE, &v_ecb, NULL, vpiNoDelay);
486
                                        counter=0;
487
 
488
                                }
489
 
490
                                if(counter_write == 1)
491
                                {
492
                                        STATE =IDLE;
493
                                        counter_write = 0;
494
                                        counter=0;
495
                                        PACKETS_GENERATED = PACKETS_GENERATED + 1;
496
                                }
497
 
498
 
499
 
500
                break;
501
 
502
                }
503
 
504
        }
505
 
506
        return 0;
507
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.