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

Subversion Repositories apbtoaes128

[/] [apbtoaes128/] [trunk/] [pli/] [bfm_cbc/] [aes_bfm_decryption_ccfie_cbc.h] - Blame information for rev 11

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_decryption_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_DECRYPTION_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
                                                if(FIPS_ENABLE == FIPS)
172
                                                {
173
 
174
                                                        if(vector_address[counter_write] == ADDR_AES_KEYR3)
175
                                                                {
176 9 redbear
                                                                        a = a | KEY_FIPS_CBC_DERIVATED[0];
177 5 redbear
                                                                        a = a << 8;
178 9 redbear
                                                                        a = a | KEY_FIPS_CBC_DERIVATED[1];
179 5 redbear
                                                                        a = a << 8;
180 9 redbear
                                                                        a = a | KEY_FIPS_CBC_DERIVATED[2];
181 5 redbear
                                                                        a = a << 8;
182 9 redbear
                                                                        a = a | KEY_FIPS_CBC_DERIVATED[3];
183 5 redbear
                                                                        v_ecb.value.integer = a;
184
                                                                }
185
 
186
 
187
                                                                if(vector_address[counter_write] == ADDR_AES_KEYR2)
188
                                                                {
189 9 redbear
                                                                        b = b | KEY_FIPS_CBC_DERIVATED[4];
190 5 redbear
                                                                        b = b << 8;
191 9 redbear
                                                                        b = b | KEY_FIPS_CBC_DERIVATED[5];
192 5 redbear
                                                                        b = b << 8;
193 9 redbear
                                                                        b = b | KEY_FIPS_CBC_DERIVATED[6];
194 5 redbear
                                                                        b = b << 8;
195 9 redbear
                                                                        b = b | KEY_FIPS_CBC_DERIVATED[7];
196 5 redbear
                                                                        v_ecb.value.integer = b;
197
                                                                }
198
 
199
                                                                if(vector_address[counter_write] == ADDR_AES_KEYR1)
200
                                                                {
201
 
202 9 redbear
                                                                        c = c | KEY_FIPS_CBC_DERIVATED[8];
203 5 redbear
                                                                        c = c << 8;
204 9 redbear
                                                                        c = c | KEY_FIPS_CBC_DERIVATED[9];
205 5 redbear
                                                                        c = c << 8;
206 9 redbear
                                                                        c = c | KEY_FIPS_CBC_DERIVATED[10];
207 5 redbear
                                                                        c = c << 8;
208 9 redbear
                                                                        c = c | KEY_FIPS_CBC_DERIVATED[11];
209 5 redbear
                                                                        v_ecb.value.integer = c;
210
 
211
                                                                }
212
 
213
                                                                if(vector_address[counter_write] == ADDR_AES_KEYR0)
214
                                                                {
215 9 redbear
                                                                        d = d | KEY_FIPS_CBC_DERIVATED[12];
216 5 redbear
                                                                        d = d << 8;
217 9 redbear
                                                                        d = d | KEY_FIPS_CBC_DERIVATED[13];
218 5 redbear
                                                                        d = d << 8;
219 9 redbear
                                                                        d = d | KEY_FIPS_CBC_DERIVATED[14];
220 5 redbear
                                                                        d = d << 8;
221 9 redbear
                                                                        d = d | KEY_FIPS_CBC_DERIVATED[15];
222 5 redbear
                                                                        v_ecb.value.integer = d;
223
                                                                }
224
 
225
 
226
                                                                if(vector_address[counter_write] == ADDR_AES_IVR3)
227
                                                                {
228
                                                                        a = a | IV_FIPS_CBC_NOT_DERIVATED[0];
229
                                                                        a = a << 8;
230
                                                                        a = a | IV_FIPS_CBC_NOT_DERIVATED[1];
231
                                                                        a = a << 8;
232
                                                                        a = a | IV_FIPS_CBC_NOT_DERIVATED[2];
233
                                                                        a = a << 8;
234
                                                                        a = a | IV_FIPS_CBC_NOT_DERIVATED[3];
235
                                                                        v_ecb.value.integer = a;
236
                                                                }
237
 
238
 
239
                                                                if(vector_address[counter_write] == ADDR_AES_IVR2)
240
                                                                {
241
                                                                        b = b | IV_FIPS_CBC_NOT_DERIVATED[4];
242
                                                                        b = b << 8;
243
                                                                        b = b | IV_FIPS_CBC_NOT_DERIVATED[5];
244
                                                                        b = b << 8;
245
                                                                        b = b | IV_FIPS_CBC_NOT_DERIVATED[6];
246
                                                                        b = b << 8;
247
                                                                        b = b | IV_FIPS_CBC_NOT_DERIVATED[7];
248
                                                                        v_ecb.value.integer = b;
249
                                                                }
250
 
251
                                                                if(vector_address[counter_write] == ADDR_AES_IVR1)
252
                                                                {
253
 
254
                                                                        c = c | IV_FIPS_CBC_NOT_DERIVATED[8];
255
                                                                        c = c << 8;
256
                                                                        c = c | IV_FIPS_CBC_NOT_DERIVATED[9];
257
                                                                        c = c << 8;
258
                                                                        c = c | IV_FIPS_CBC_NOT_DERIVATED[10];
259
                                                                        c = c << 8;
260
                                                                        c = c | IV_FIPS_CBC_NOT_DERIVATED[11];
261
                                                                        v_ecb.value.integer = c;
262
 
263
                                                                }
264
 
265
                                                                if(vector_address[counter_write] == ADDR_AES_IVR0)
266
                                                                {
267
                                                                        d = d | IV_FIPS_CBC_NOT_DERIVATED[12];
268
                                                                        d = d << 8;
269
                                                                        d = d | IV_FIPS_CBC_NOT_DERIVATED[13];
270
                                                                        d = d << 8;
271
                                                                        d = d | IV_FIPS_CBC_NOT_DERIVATED[14];
272
                                                                        d = d << 8;
273
                                                                        d = d | IV_FIPS_CBC_NOT_DERIVATED[15];
274
                                                                        v_ecb.value.integer = d;
275
                                                                }
276
 
277
 
278
 
279
                                                }else if(FIPS_ENABLE == RANDOM_DATA)
280
                                                {
281
                                                        v_ecb.value.integer = data_in(rd);
282
                                                }
283
 
284
                                                //vpi_put_value(PWDATA, &v_ecb, NULL, vpiNoDelay);                                              
285
                                                vpi_put_value(PWDATA, &v_ecb, &t_ecb, vpiTransportDelay);
286
 
287
 
288
                                                a = 0;
289
                                                b = 0;
290
                                                c = 0;
291
                                                d = 0;
292
                                        }
293
 
294
 
295
                                        if(counter_write == 9)
296
                                        {
297
 
298
                                                v_ecb.value.integer = vector_address[counter_write];
299
                                                vpi_put_value(PADDR, &v_ecb, NULL, vpiNoDelay);
300
 
301
                                                t_ecb.type = vpiScaledRealTime;
302
                                                t_ecb.real = 0;
303
                                                v_ecb.format=vpiIntVal;
304
                                                v_ecb.value.integer = 561;
305
                                                vpi_put_value(PWDATA, &v_ecb, &t_ecb, vpiTransportDelay);
306
 
307
                                        }
308
 
309
 
310
                                        if(counter_write > 9  &&  counter_write < 14) //WRITE DINR
311
                                        {
312
 
313
                                                v_ecb.value.integer = ADDR_AES_DINR;
314
                                                vpi_put_value(PADDR, &v_ecb, NULL, vpiNoDelay);
315
 
316
 
317
                                                if(FIPS_ENABLE == FIPS)
318
                                                {
319
 
320
                                                                if(counter_write == 10)
321
                                                        {
322
                                                                a = a | TEXT_CBC_FIPS_DERIVATED[0];
323
                                                                a = a << 8;
324
                                                                a = a | TEXT_CBC_FIPS_DERIVATED[1];
325
                                                                a = a << 8;
326
                                                                a = a | TEXT_CBC_FIPS_DERIVATED[2];
327
                                                                a = a << 8;
328
                                                                a = a | TEXT_CBC_FIPS_DERIVATED[3];
329
                                                                v_ecb.value.integer = a;
330
 
331
                                                        }else if(counter_write == 11)
332
                                                        {
333
                                                                b = b | TEXT_CBC_FIPS_DERIVATED[4];
334
                                                                b = b << 8;
335
                                                                b = b | TEXT_CBC_FIPS_DERIVATED[5];
336
                                                                b = b << 8;
337
                                                                b = b | TEXT_CBC_FIPS_DERIVATED[6];
338
                                                                b = b << 8;
339
                                                                b = b | TEXT_CBC_FIPS_DERIVATED[7];
340
                                                                v_ecb.value.integer = b;
341
 
342
                                                        }else if(counter_write == 12 )
343
                                                        {
344
 
345
                                                                c = c | TEXT_CBC_FIPS_DERIVATED[8];
346
                                                                c = c << 8;
347
                                                                c = c | TEXT_CBC_FIPS_DERIVATED[9];
348
                                                                c = c << 8;
349
                                                                c = c | TEXT_CBC_FIPS_DERIVATED[10];
350
                                                                c = c << 8;
351
                                                                c = c | TEXT_CBC_FIPS_DERIVATED[11];
352
                                                                v_ecb.value.integer = c;
353
 
354
                                                        }else if(counter_write == 13 )
355
                                                        {
356
                                                                d = d | TEXT_CBC_FIPS_DERIVATED[12];
357
                                                                d = d << 8;
358
                                                                d = d | TEXT_CBC_FIPS_DERIVATED[13];
359
                                                                d = d << 8;
360
                                                                d = d | TEXT_CBC_FIPS_DERIVATED[14];
361
                                                                d = d << 8;
362
                                                                d = d | TEXT_CBC_FIPS_DERIVATED[15];
363
                                                                v_ecb.value.integer = d;
364
 
365
                                                        }
366
 
367
 
368
                                                }else if(FIPS_ENABLE == RANDOM_DATA)
369
                                                {
370
                                                        v_ecb.value.integer = data_in(rd);
371
                                                }
372
 
373
                                                //vpi_put_value(PWDATA, &v_ecb, NULL, vpiNoDelay);
374
                                                vpi_put_value(PWDATA, &v_ecb, &t_ecb, vpiTransportDelay);
375
 
376
                                                a = 0;
377
                                                b = 0;
378
                                                c = 0;
379
                                                d = 0;
380
 
381
 
382
                                        }
383
 
384
                                        v_ecb.value.integer = 1;
385
                                        vpi_put_value(PSEL, &v_ecb, NULL, vpiNoDelay);
386
 
387
                                        counter=0;
388
                                }
389
 
390
                                if(counter_write == 14)
391
                                {
392
                                        counter_write = 0;
393
 
394
                                        STATE =WAIT_SR;
395
 
396
 
397
 
398
 
399
 
400
                                }
401
 
402
                  break;
403
 
404
                  case WAIT_SR:
405
 
406
                                v_ecb.value.integer = ADDR_AES_SR;
407
                                vpi_put_value(PADDR, &v_ecb, NULL, vpiNoDelay);
408
 
409
                                v_ecb.value.integer = 0;
410
                                vpi_put_value(PWRITE, &v_ecb, NULL, vpiNoDelay);
411 9 redbear
 
412
                                v_ecb.value.integer = 0;
413
                                vpi_put_value(PENABLE, &v_ecb, NULL, vpiNoDelay);
414
 
415
                                /*
416 5 redbear
                                if(counter == 0)
417
                                {
418
 
419
                                        counter++;
420
 
421
                                        v_ecb.value.integer = 1;
422
                                        vpi_put_value(PENABLE, &v_ecb, NULL, vpiNoDelay);
423
 
424
 
425
                                }else if(counter == 1)
426
                                {
427
                                        v_ecb.value.integer = 0;
428
                                        vpi_put_value(PENABLE, &v_ecb, NULL, vpiNoDelay);
429
 
430
                                        counter=0;
431
                                }
432 9 redbear
                                */
433 5 redbear
                                v_ecb.value.integer = 0;
434 9 redbear
                                vpi_get_value(int_ccf,&v_ecb);
435 5 redbear
 
436
                                if(v_ecb.value.integer == 1)
437
                                {
438
                                        STATE = READ_DOUTR;
439
 
440 9 redbear
                                        //t_ecb.type = vpiScaledRealTime;
441
                                        //t_ecb.real = 0;
442
                                        //v_ecb.format=vpiIntVal;
443
                                        //v_ecb.value.integer = ADDR_AES_DOUTR;
444
                                        //vpi_put_value(PADDR, &v_ecb, &t_ecb, vpiTransportDelay);
445 5 redbear
                                }
446
 
447
 
448
                  break;
449
 
450
 
451
                 case READ_DOUTR:
452
 
453
 
454
 
455
                                if(counter == 0)
456
                                {
457
 
458
                                        v_ecb.value.integer = 1;
459
                                        vpi_put_value(PENABLE, &v_ecb, NULL, vpiNoDelay);
460
 
461
                                        counter_read++;
462
                                        counter++;
463
 
464
 
465
 
466
                                }else if(counter == 1)
467
                                {
468
                                        v_ecb.value.integer = 0;
469
                                        vpi_put_value(PENABLE, &v_ecb, NULL, vpiNoDelay);
470
 
471
 
472
                                        if(counter_read < 4)
473
                                        {
474
 
475
                                                v_ecb.value.integer = ADDR_AES_DOUTR;
476
                                                vpi_put_value(PADDR, &v_ecb, NULL, vpiNoDelay);
477
 
478
                                        }
479
 
480
 
481
                                        if(counter_read == 4)
482
                                        {
483
 
484
                                                v_ecb.value.integer = ADDR_AES_KEYR3;
485
                                                vpi_put_value(PADDR, &v_ecb, NULL, vpiNoDelay);
486
 
487
                                        }
488
 
489
 
490
                                        if(counter_read == 5)
491
                                        {
492
 
493
                                                v_ecb.value.integer = ADDR_AES_KEYR2;
494
                                                vpi_put_value(PADDR, &v_ecb, NULL, vpiNoDelay);
495
 
496
                                        }
497
 
498
 
499
 
500
                                        if(counter_read == 6)
501
                                        {
502
 
503
                                                v_ecb.value.integer = ADDR_AES_KEYR1;
504
                                                vpi_put_value(PADDR, &v_ecb, NULL, vpiNoDelay);
505
 
506
                                        }
507
 
508
 
509
                                        if(counter_read == 7)
510
                                        {
511
 
512
                                                v_ecb.value.integer = ADDR_AES_KEYR0;
513
                                                vpi_put_value(PADDR, &v_ecb, NULL, vpiNoDelay);
514
 
515
                                        }
516
 
517
 
518
                                        if(counter_read == 8)
519
                                        {
520
 
521
                                                v_ecb.value.integer = ADDR_AES_IVR3;
522
                                                vpi_put_value(PADDR, &v_ecb, NULL, vpiNoDelay);
523
 
524
                                        }
525
 
526
 
527
                                        if(counter_read == 9)
528
                                        {
529
 
530
                                                v_ecb.value.integer = ADDR_AES_IVR2;
531
                                                vpi_put_value(PADDR, &v_ecb, NULL, vpiNoDelay);
532
 
533
                                        }
534
 
535
 
536
 
537
                                        if(counter_read == 10)
538
                                        {
539
 
540
                                                v_ecb.value.integer = ADDR_AES_IVR1;
541
                                                vpi_put_value(PADDR, &v_ecb, NULL, vpiNoDelay);
542
 
543
                                        }
544
 
545
 
546
                                        if(counter_read == 11)
547
                                        {
548
 
549
                                                v_ecb.value.integer = ADDR_AES_IVR0;
550
                                                vpi_put_value(PADDR, &v_ecb, NULL, vpiNoDelay);
551
 
552
                                        }
553
 
554
 
555
                                        counter = 0;
556
                                }
557
 
558
                                if(counter_read == 12)
559
                                {
560
                                        STATE = RESET_SR;
561
                                        counter_read = 0;
562
                                }
563
 
564
                 break;
565
 
566
                case RESET_SR:
567
 
568
                                v_ecb.value.integer = 1;
569
                                vpi_put_value(PWRITE, &v_ecb, NULL, vpiNoDelay);
570
 
571
                                v_ecb.value.integer = 1;
572
                                vpi_put_value(PSEL, &v_ecb, NULL, vpiNoDelay);
573
 
574
                                v_ecb.value.integer = 0;
575
                                vpi_put_value(PADDR, &v_ecb, NULL, vpiNoDelay);
576
 
577
                                v_ecb.value.integer = 128;
578
                                vpi_put_value(PWDATA, &v_ecb, NULL, vpiNoDelay);
579
 
580
 
581
                                if(counter == 0)
582
                                {
583
 
584
                                        counter_write++;
585
                                        counter++;
586
 
587
                                        v_ecb.value.integer = 1;
588
                                        vpi_put_value(PENABLE, &v_ecb, NULL, vpiNoDelay);
589
 
590
                                }else if(counter == 1)
591
                                {
592
 
593
                                        v_ecb.value.integer = 0;
594
                                        vpi_put_value(PENABLE, &v_ecb, NULL, vpiNoDelay);
595
                                        counter=0;
596
 
597
                                }
598
 
599
                                if(counter_write == 1)
600
                                {
601
                                        STATE =IDLE;
602
                                        counter_write = 0;
603
                                        counter=0;
604
                                        PACKETS_GENERATED = PACKETS_GENERATED + 1;
605
                                }
606
 
607
 
608
 
609
                break;
610
 
611
                }
612
 
613
        }
614
 
615
        return 0;
616
}

powered by: WebSVN 2.1.0

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