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

Subversion Repositories apbtoaes128

[/] [apbtoaes128/] [trunk/] [pli/] [bfm_ctr/] [aes_bfm_encryption_ccfie_ctr.h] - Blame information for rev 9

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

powered by: WebSVN 2.1.0

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