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 5

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
                                if(counter == 0)
401
                                {
402
 
403
                                        counter++;
404
 
405
                                        v_ecb.value.integer = 1;
406
                                        vpi_put_value(PENABLE, &v_ecb, NULL, vpiNoDelay);
407
 
408
 
409
                                }else if(counter == 1)
410
                                {
411
                                        v_ecb.value.integer = 0;
412
                                        vpi_put_value(PENABLE, &v_ecb, NULL, vpiNoDelay);
413
 
414
                                        counter=0;
415
                                }
416
 
417
                                v_ecb.value.integer = 0;
418
                                vpi_get_value(PRDATA,&v_ecb);
419
 
420
                                if(v_ecb.value.integer == 1)
421
                                {
422
                                        STATE = READ_DOUTR;
423
 
424
                                        t_ecb.type = vpiScaledRealTime;
425
                                        t_ecb.real = 10;
426
                                        v_ecb.format=vpiIntVal;
427
                                        v_ecb.value.integer = ADDR_AES_DOUTR;
428
                                        vpi_put_value(PADDR, &v_ecb, &t_ecb, vpiTransportDelay);
429
                                }
430
 
431
 
432
                  break;
433
 
434
 
435
                 case READ_DOUTR:
436
 
437
 
438
 
439
                                if(counter == 0)
440
                                {
441
 
442
                                        v_ecb.value.integer = 1;
443
                                        vpi_put_value(PENABLE, &v_ecb, NULL, vpiNoDelay);
444
 
445
                                        counter_read++;
446
                                        counter++;
447
 
448
 
449
 
450
                                }else if(counter == 1)
451
                                {
452
                                        v_ecb.value.integer = 0;
453
                                        vpi_put_value(PENABLE, &v_ecb, NULL, vpiNoDelay);
454
 
455
 
456
                                        if(counter_read < 4)
457
                                        {
458
 
459
                                                v_ecb.value.integer = ADDR_AES_DOUTR;
460
                                                vpi_put_value(PADDR, &v_ecb, NULL, vpiNoDelay);
461
 
462
                                        }
463
 
464
 
465
                                        if(counter_read == 4)
466
                                        {
467
 
468
                                                v_ecb.value.integer = ADDR_AES_KEYR3;
469
                                                vpi_put_value(PADDR, &v_ecb, NULL, vpiNoDelay);
470
 
471
                                        }
472
 
473
 
474
                                        if(counter_read == 5)
475
                                        {
476
 
477
                                                v_ecb.value.integer = ADDR_AES_KEYR2;
478
                                                vpi_put_value(PADDR, &v_ecb, NULL, vpiNoDelay);
479
 
480
                                        }
481
 
482
 
483
 
484
                                        if(counter_read == 6)
485
                                        {
486
 
487
                                                v_ecb.value.integer = ADDR_AES_KEYR1;
488
                                                vpi_put_value(PADDR, &v_ecb, NULL, vpiNoDelay);
489
 
490
                                        }
491
 
492
 
493
                                        if(counter_read == 7)
494
                                        {
495
 
496
                                                v_ecb.value.integer = ADDR_AES_KEYR0;
497
                                                vpi_put_value(PADDR, &v_ecb, NULL, vpiNoDelay);
498
 
499
                                        }
500
 
501
 
502
                                        if(counter_read == 8)
503
                                        {
504
 
505
                                                v_ecb.value.integer = ADDR_AES_IVR3;
506
                                                vpi_put_value(PADDR, &v_ecb, NULL, vpiNoDelay);
507
 
508
                                        }
509
 
510
 
511
                                        if(counter_read == 9)
512
                                        {
513
 
514
                                                v_ecb.value.integer = ADDR_AES_IVR2;
515
                                                vpi_put_value(PADDR, &v_ecb, NULL, vpiNoDelay);
516
 
517
                                        }
518
 
519
 
520
 
521
                                        if(counter_read == 10)
522
                                        {
523
 
524
                                                v_ecb.value.integer = ADDR_AES_IVR1;
525
                                                vpi_put_value(PADDR, &v_ecb, NULL, vpiNoDelay);
526
 
527
                                        }
528
 
529
 
530
                                        if(counter_read == 11)
531
                                        {
532
 
533
                                                v_ecb.value.integer = ADDR_AES_IVR0;
534
                                                vpi_put_value(PADDR, &v_ecb, NULL, vpiNoDelay);
535
 
536
                                        }
537
 
538
 
539
                                        counter = 0;
540
                                }
541
 
542
                                if(counter_read == 12)
543
                                {
544
                                        STATE = RESET_SR;
545
                                        counter_read = 0;
546
                                }
547
 
548
                 break;
549
 
550
                case RESET_SR:
551
 
552
                                v_ecb.value.integer = 1;
553
                                vpi_put_value(PWRITE, &v_ecb, NULL, vpiNoDelay);
554
 
555
                                v_ecb.value.integer = 1;
556
                                vpi_put_value(PSEL, &v_ecb, NULL, vpiNoDelay);
557
 
558
                                v_ecb.value.integer = 0;
559
                                vpi_put_value(PADDR, &v_ecb, NULL, vpiNoDelay);
560
 
561
                                v_ecb.value.integer = 128;
562
                                vpi_put_value(PWDATA, &v_ecb, NULL, vpiNoDelay);
563
 
564
 
565
                                if(counter == 0)
566
                                {
567
 
568
                                        counter_write++;
569
                                        counter++;
570
 
571
                                        v_ecb.value.integer = 1;
572
                                        vpi_put_value(PENABLE, &v_ecb, NULL, vpiNoDelay);
573
 
574
                                }else if(counter == 1)
575
                                {
576
 
577
                                        v_ecb.value.integer = 0;
578
                                        vpi_put_value(PENABLE, &v_ecb, NULL, vpiNoDelay);
579
                                        counter=0;
580
 
581
                                }
582
 
583
                                if(counter_write == 1)
584
                                {
585
                                        STATE =IDLE;
586
                                        counter_write = 0;
587
                                        counter=0;
588
 
589
                                        PACKETS_GENERATED = PACKETS_GENERATED + 1;
590
                                }
591
 
592
 
593
 
594
                break;
595
 
596
                }
597
 
598
        }
599
 
600
        return 0;
601
}

powered by: WebSVN 2.1.0

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