00001
00089 #include "board.h"
00090 #include "print_funcs.h"
00091 #include "gpio.h"
00092 #include "pm.h"
00093 #include "intc.h"
00094 #include "usart.h"
00095 #include "aes.h"
00096
00097
00100
00101 #if BOARD == EVK1104
00102 # define AES_EXAMPLE_USART (&AVR32_USART1)
00103 # define AES_EXAMPLE_USART_RX_PIN AVR32_USART1_RXD_0_0_PIN
00104 # define AES_EXAMPLE_USART_RX_FUNCTION AVR32_USART1_RXD_0_0_FUNCTION
00105 # define AES_EXAMPLE_USART_TX_PIN AVR32_USART1_TXD_0_0_PIN
00106 # define AES_EXAMPLE_USART_TX_FUNCTION AVR32_USART1_TXD_0_0_FUNCTION
00107 # define AES_EXAMPLE_USART_BAUDRATE 57600
00108 # define AES_EXAMPLE_LED1 LED0_GPIO
00109 # define AES_EXAMPLE_LED2 LED1_GPIO
00110 # define AES_EXAMPLE_LED3 LED2_GPIO
00111 # define AES_EXAMPLE_LED4 LED3_GPIO
00112 #endif
00113
00114 #if !defined(AES_EXAMPLE_USART) || \
00115 !defined(AES_EXAMPLE_USART_RX_PIN) || \
00116 !defined(AES_EXAMPLE_USART_RX_FUNCTION) || \
00117 !defined(AES_EXAMPLE_USART_TX_PIN) || \
00118 !defined(AES_EXAMPLE_USART_TX_FUNCTION) || \
00119 !defined(AES_EXAMPLE_USART_BAUDRATE) || \
00120 !defined(AES_EXAMPLE_LED1) || \
00121 !defined(AES_EXAMPLE_LED2) || \
00122 !defined(AES_EXAMPLE_LED3) || \
00123 !defined(AES_EXAMPLE_LED4)
00124 # error The USART and LEDs configuration to use for debug on your board is missing
00125 #endif
00127
00128
00130
00131 #define AES_EXAMPLE_STATE_1 0
00132 #define AES_EXAMPLE_STATE_2 1
00133 #define AES_EXAMPLE_STATE_3 2
00134 #define AES_EXAMPLE_STATE_4 3
00135 #define AES_EXAMPLE_STATE_5 4
00136 #define AES_EXAMPLE_STATE_6 5
00137 #define AES_EXAMPLE_STATE_7 6
00138 #define AES_EXAMPLE_STATE_8 7
00140
00141 #define AES_EXAMPLE_REFBUF_SIZE 16
00142
00143
00144 const unsigned int CBCRefInputData[AES_EXAMPLE_REFBUF_SIZE] = {
00145 0x6bc1bee2,
00146 0x2e409f96,
00147 0xe93d7e11,
00148 0x7393172a,
00149 0xae2d8a57,
00150 0x1e03ac9c,
00151 0x9eb76fac,
00152 0x45af8e51,
00153 0x30c81c46,
00154 0xa35ce411,
00155 0xe5fbc119,
00156 0x1a0a52ef,
00157 0xf69f2445,
00158 0xdf4f9b17,
00159 0xad2b417b,
00160 0xe66c3710 };
00161
00162
00163 const unsigned int CBCRefOutputData[AES_EXAMPLE_REFBUF_SIZE] = {
00164 0xf58c4c04,
00165 0xd6e5f1ba,
00166 0x779eabfb,
00167 0x5f7bfbd6,
00168 0x9cfc4e96,
00169 0x7edb808d,
00170 0x679f777b,
00171 0xc6702c7d,
00172 0x39f23369,
00173 0xa9d9bacf,
00174 0xa530e263,
00175 0x04231461,
00176 0xb2eb05e2,
00177 0xc39be9fc,
00178 0xda6c1907,
00179 0x8c6a9d1b };
00180
00181
00182
00183 const unsigned int CipherKey256[8] = {
00184 0x603deb10,
00185 0x15ca71be,
00186 0x2b73aef0,
00187 0x857d7781,
00188 0x1f352c07,
00189 0x3b6108d7,
00190 0x2d9810a3,
00191 0x0914dff4
00192 };
00193
00194
00195
00196 const unsigned int InitVectorCBC[4] = {
00197 0x00010203,
00198 0x04050607,
00199 0x08090a0b,
00200 0x0c0d0e0f
00201 };
00202
00203 volatile unsigned int OutputData[AES_EXAMPLE_REFBUF_SIZE];
00204
00205 volatile unsigned char state = 0;
00206
00207 volatile unsigned int ccountt0, ccountt1;
00208
00209 volatile unsigned long Tempo;
00210
00211 volatile unsigned char urad_evt = FALSE;
00212
00213 volatile unsigned char ContinueCBCProcess = TRUE;
00214
00215 static const gpio_map_t USART_GPIO_MAP =
00216 {
00217 {AES_EXAMPLE_USART_RX_PIN, AES_EXAMPLE_USART_RX_FUNCTION},
00218 {AES_EXAMPLE_USART_TX_PIN, AES_EXAMPLE_USART_TX_FUNCTION}
00219 };
00220
00221 static const usart_options_t USART_OPTIONS =
00222 {
00223 .baudrate = AES_EXAMPLE_USART_BAUDRATE,
00224 .charlength = 8,
00225 .paritytype = USART_NO_PARITY,
00226 .stopbits = USART_1_STOPBIT,
00227 .channelmode = USART_NORMAL_CHMODE
00228 };
00229
00230 pm_freq_param_t pm_freq_param =
00231 {
00232 .cpu_f = AES_EXAMPLE_CPU_FREQ,
00233 .pba_f = AES_EXAMPLE_CPU_FREQ,
00234 .osc0_f = FOSC0,
00235 .osc0_startup = OSC0_STARTUP
00236 };
00237
00244 #if defined(__GNUC__)
00245 __attribute__((__interrupt__))
00246 #elif defined(__ICCAVR32__)
00247 __interrupt
00248 #endif
00249 static void aes_int_handler(void)
00250 {
00251 ccountt1 = Get_system_register(AVR32_COUNT);
00252 if(AES_EXAMPLE_STATE_1 == state)
00253 {
00254 gpio_clr_gpio_pin(AES_EXAMPLE_LED1);
00255
00256 aes_read_outputdata(&AVR32_AES, (unsigned int *)OutputData);
00257 state++;
00258 }
00259 else if(AES_EXAMPLE_STATE_2 == state)
00260 {
00261 gpio_clr_gpio_pin(AES_EXAMPLE_LED2);
00262
00263 aes_read_outputdata(&AVR32_AES, (unsigned int *)OutputData);
00264 state++;
00265 }
00266 else if(AES_EXAMPLE_STATE_3 == state)
00267 {
00268 Tempo = aes_get_status(&AVR32_AES);
00269 if(Tempo & AVR32_AES_ISR_URAD_MASK)
00270 {
00271 urad_evt = TRUE;
00272 state++;
00273 if( AES_URAT_READ_WRITEONLY == ((Tempo & AVR32_AES_ISR_URAT_MASK)>>AVR32_AES_ISR_URAT_OFFSET ))
00274 gpio_clr_gpio_pin(AES_EXAMPLE_LED3);
00275
00276
00277 aes_swreset(&AVR32_AES);
00278 }
00279 }
00280 else if(AES_EXAMPLE_STATE_4 == state)
00281 {
00282 gpio_clr_gpio_pin(AES_EXAMPLE_LED4);
00283
00284 aes_read_outputdata(&AVR32_AES, (unsigned int *)OutputData);
00285 state++;
00286 }
00287 else if((AES_EXAMPLE_STATE_5 <= state) && (AES_EXAMPLE_STATE_8 >= state))
00288 {
00289
00290 aes_read_outputdata(&AVR32_AES, (unsigned int *)&OutputData[(state - AES_EXAMPLE_STATE_5)*4]);
00291 ContinueCBCProcess = TRUE;
00292 state++;
00293 }
00294 else
00295 while(1);
00296 }
00297
00298
00301 static void init_hmatrix(void)
00302 {
00303
00304
00305 avr32_hmatrix_scfg_t scfg;
00306
00307 scfg = AVR32_HMATRIX.SCFG[AVR32_HMATRIX_SLAVE_FLASH];
00308 scfg.defmstr_type = AVR32_HMATRIX_DEFMSTR_TYPE_LAST_DEFAULT;
00309 AVR32_HMATRIX.SCFG[AVR32_HMATRIX_SLAVE_FLASH] = scfg;
00310 }
00311
00312
00320 int main(void)
00321 {
00322 aes_isrconfig_t AesIsrConf;
00323 aes_config_t AesConf;
00324 unsigned int CipherKey[8];
00325 unsigned int InputData[4];
00326 unsigned char TestResult = TRUE;
00327 unsigned int i;
00328
00329
00330
00331 #if BOARD == EVK1104
00332 if( PM_FREQ_STATUS_FAIL==pm_configure_clocks(&pm_freq_param) )
00333 while(1);
00334 #endif
00335
00336 init_hmatrix();
00337
00338
00339 gpio_enable_module(USART_GPIO_MAP,
00340 sizeof(USART_GPIO_MAP) / sizeof(USART_GPIO_MAP[0]));
00341
00342
00343 usart_init_rs232(AES_EXAMPLE_USART, &USART_OPTIONS, AES_EXAMPLE_CPU_FREQ);
00344 print(AES_EXAMPLE_USART, "\x1B[2J\x1B[H.: Using the AES :.\n\n");
00345
00346
00347 Disable_global_interrupt();
00348
00349
00350 INTC_init_interrupts();
00351
00352
00353
00354
00355
00356
00357
00358 INTC_register_interrupt(&aes_int_handler, AVR32_AES_IRQ, AVR32_INTC_INT0);
00359
00360
00361 AesIsrConf.datrdy = ENABLE;
00362 AesIsrConf.urad = ENABLE;
00363 aes_isr_configure(&AVR32_AES, &AesIsrConf);
00364
00365
00366 Enable_global_interrupt();
00367
00368
00369
00370
00371
00372
00373
00374
00375 print(AES_EXAMPLE_USART, "\n-----------------------------------\n");
00376 print(AES_EXAMPLE_USART, "------ Cipher in Manual Mode ------\n");
00377 print(AES_EXAMPLE_USART, " - 128bit cryptographic key\n");
00378 print(AES_EXAMPLE_USART, " - ECB cipher mode\n");
00379 print(AES_EXAMPLE_USART, " - No counter measures\n");
00380 print(AES_EXAMPLE_USART, " - input of 4 32bit words\n");
00381 print(AES_EXAMPLE_USART, "-----------------------------------\n");
00382
00383
00384 AesConf.ProcessingMode = AES_PMODE_CIPHER;
00385 AesConf.ProcessingDelay = 0;
00386 AesConf.StartMode = AES_START_MODE_MANUAL;
00387 AesConf.KeySize = AES_KEY_SIZE_128;
00388 AesConf.OpMode = AES_ECB_MODE;
00389 AesConf.LodMode = 0;
00390
00391
00392 AesConf.CFBSize = 0;
00393 AesConf.CounterMeasureMask = 0;
00394 aes_configure(&AVR32_AES, &AesConf);
00395
00396
00397
00398 CipherKey[0] = 0x00010203;
00399 CipherKey[1] = 0x04050607;
00400 CipherKey[2] = 0x08090A0B;
00401 CipherKey[3] = 0x0C0D0E0F;
00402 aes_set_key(&AVR32_AES, CipherKey);
00403
00404
00405
00406
00407
00408 InputData[0] = 0x00112233;
00409 InputData[1] = 0x44556677;
00410 InputData[2] = 0x8899AABB;
00411 InputData[3] = 0xCCDDEEFF;
00412 aes_write_inputdata(&AVR32_AES, InputData);
00413
00414 ccountt0 = Get_system_register(AVR32_COUNT);
00415
00416 aes_start(&AVR32_AES);
00417
00418
00419 while(AES_EXAMPLE_STATE_1 == state);
00420
00421
00422 if(( 0x69c4e0d8 != OutputData[0] ) || ( 0x6a7b0430 != OutputData[1] )
00423 || ( 0xd8cdb780 != OutputData[2] ) || ( 0x70b4c55a != OutputData[3] ))
00424 print(AES_EXAMPLE_USART, "\nKO!!!\n");
00425 else
00426 {
00427 print(AES_EXAMPLE_USART, "\nOK!!! Nb cycles: ");
00428 print_ulong(AES_EXAMPLE_USART, ccountt1 - ccountt0);
00429 }
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439 print(AES_EXAMPLE_USART, "\n-----------------------------------\n");
00440 print(AES_EXAMPLE_USART, "------ Cipher in Manual Mode ------\n");
00441 print(AES_EXAMPLE_USART, " - 128bit cryptographic key\n");
00442 print(AES_EXAMPLE_USART, " - ECB cipher mode\n");
00443 print(AES_EXAMPLE_USART, " - all counter measures\n");
00444 print(AES_EXAMPLE_USART, " - input of 4 32bit words\n");
00445 print(AES_EXAMPLE_USART, "-----------------------------------\n");
00446
00447
00448 AesConf.ProcessingMode = AES_PMODE_CIPHER;
00449 AesConf.ProcessingDelay = 0;
00450 AesConf.StartMode = AES_START_MODE_MANUAL;
00451 AesConf.KeySize = AES_KEY_SIZE_128;
00452 AesConf.OpMode = AES_ECB_MODE;
00453 AesConf.LodMode = 0;
00454
00455
00456 AesConf.CFBSize = 0;
00457 AesConf.CounterMeasureMask = 31;
00458 aes_configure(&AVR32_AES, &AesConf);
00459
00460
00461
00462 CipherKey[0] = 0x00010203;
00463 CipherKey[1] = 0x04050607;
00464 CipherKey[2] = 0x08090A0B;
00465 CipherKey[3] = 0x0C0D0E0F;
00466 aes_set_key(&AVR32_AES, CipherKey);
00467
00468
00469
00470
00471
00472 InputData[0] = 0x00112233;
00473 InputData[1] = 0x44556677;
00474 InputData[2] = 0x8899AABB;
00475 InputData[3] = 0xCCDDEEFF;
00476 aes_write_inputdata(&AVR32_AES, InputData);
00477
00478 ccountt0 = Get_system_register(AVR32_COUNT);
00479
00480 aes_start(&AVR32_AES);
00481
00482
00483 while(AES_EXAMPLE_STATE_2 == state);
00484
00485
00486 if(( 0x69c4e0d8 != OutputData[0] ) || ( 0x6a7b0430 != OutputData[1] )
00487 || ( 0xd8cdb780 != OutputData[2] ) || ( 0x70b4c55a != OutputData[3] ))
00488 print(AES_EXAMPLE_USART, "\nKO!!!\n");
00489 else
00490 {
00491 print(AES_EXAMPLE_USART, "\nOK!!! Nb cycles: ");
00492 print_ulong(AES_EXAMPLE_USART, ccountt1 - ccountt0);
00493 }
00494
00495
00496
00497
00498 print(AES_EXAMPLE_USART, "\n-----------------------------------\n");
00499 print(AES_EXAMPLE_USART, "------ Checking the URAD ------\n");
00500 print(AES_EXAMPLE_USART, "-----------------------------------\n");
00501
00502 Tempo = AVR32_AES.keyw1r;
00503 while(FALSE == urad_evt);
00504 print(AES_EXAMPLE_USART, "\nOK!!!\n");
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514 print(AES_EXAMPLE_USART, "\n-----------------------------------\n");
00515 print(AES_EXAMPLE_USART, "------ Decipher in Auto Mode ------\n");
00516 print(AES_EXAMPLE_USART, " - 128bit cryptographic key\n");
00517 print(AES_EXAMPLE_USART, " - ECB cipher mode\n");
00518 print(AES_EXAMPLE_USART, " - No counter measures\n");
00519 print(AES_EXAMPLE_USART, " - input of 4 32bit words\n");
00520 print(AES_EXAMPLE_USART, "-----------------------------------\n");
00521
00522
00523 AesConf.ProcessingMode = AES_PMODE_DECIPHER;
00524 AesConf.ProcessingDelay = 0;
00525 AesConf.StartMode = AES_START_MODE_AUTO;
00526 AesConf.KeySize = AES_KEY_SIZE_128;
00527 AesConf.OpMode = AES_ECB_MODE;
00528 AesConf.LodMode = 0;
00529
00530
00531 AesConf.CFBSize = 0;
00532 AesConf.CounterMeasureMask = 0;
00533 aes_configure(&AVR32_AES, &AesConf);
00534
00535
00536
00537 aes_isr_configure(&AVR32_AES, &AesIsrConf);
00538
00539
00540
00541 CipherKey[0] = 0x00010203;
00542 CipherKey[1] = 0x04050607;
00543 CipherKey[2] = 0x08090A0B;
00544 CipherKey[3] = 0x0C0D0E0F;
00545 aes_set_key(&AVR32_AES, CipherKey);
00546
00547
00548
00549
00550
00551 AVR32_AES.idata1r = OutputData[0];
00552 AVR32_AES.idata2r = OutputData[1];
00553 AVR32_AES.idata3r = OutputData[2];
00554 ccountt0 = Get_system_register(AVR32_COUNT);
00555 AVR32_AES.idata4r = OutputData[3];
00556
00557
00558
00559
00560
00561
00562
00563 while(AES_EXAMPLE_STATE_4 == state);
00564
00565
00566 if(( 0x00112233 != OutputData[0] ) || ( 0x44556677 != OutputData[1] )
00567 || ( 0x8899AABB != OutputData[2] ) || ( 0xCCDDEEFF != OutputData[3] ))
00568 print(AES_EXAMPLE_USART, "\nKO!!!\n");
00569 else
00570 {
00571 print(AES_EXAMPLE_USART, "\nOK!!! Nb cycles: ");
00572 print_ulong(AES_EXAMPLE_USART, ccountt1 - ccountt0);
00573 }
00574
00575
00576
00577
00578
00579
00580
00581
00582
00583
00584 print(AES_EXAMPLE_USART, "\n-----------------------------------\n");
00585 print(AES_EXAMPLE_USART, "------ Cipher in Auto Mode ------\n");
00586 print(AES_EXAMPLE_USART, " - 256bit cryptographic key\n");
00587 print(AES_EXAMPLE_USART, " - CBC cipher mode\n");
00588 print(AES_EXAMPLE_USART, " - No counter measures\n");
00589 print(AES_EXAMPLE_USART, " - input of 16 32bit words\n");
00590 print(AES_EXAMPLE_USART, " - output of 16 32bit words\n");
00591 print(AES_EXAMPLE_USART, "-----------------------------------\n");
00592
00593
00594 AesConf.ProcessingMode = AES_PMODE_CIPHER;
00595 AesConf.ProcessingDelay = 0;
00596 AesConf.StartMode = AES_START_MODE_AUTO;
00597 AesConf.KeySize = AES_KEY_SIZE_256;
00598 AesConf.OpMode = AES_CBC_MODE;
00599 AesConf.LodMode = 0;
00600
00601
00602 AesConf.CFBSize = 0;
00603 AesConf.CounterMeasureMask = 0;
00604 aes_configure(&AVR32_AES, &AesConf);
00605
00606
00607 aes_set_key(&AVR32_AES, CipherKey256);
00608
00609
00610 aes_set_initvector(&AVR32_AES, InitVectorCBC);
00611
00612 ccountt0 = Get_system_register(AVR32_COUNT);
00613
00614 for(i=0; i<AES_EXAMPLE_REFBUF_SIZE; i+=4)
00615 {
00616 while(FALSE == ContinueCBCProcess);
00617 ContinueCBCProcess = FALSE;
00618 AVR32_AES.idata1r = CBCRefInputData[i];
00619 AVR32_AES.idata2r = CBCRefInputData[i+1];
00620 AVR32_AES.idata3r = CBCRefInputData[i+2];
00621 AVR32_AES.idata4r = CBCRefInputData[i+3];
00622
00623
00624
00625 }
00626
00627
00628 while(AES_EXAMPLE_STATE_8 >= state);
00629
00630
00631 for(i=0; i<AES_EXAMPLE_REFBUF_SIZE; i++)
00632 {
00633 if(OutputData[i] != CBCRefOutputData[i])
00634 {
00635 TestResult = FALSE;
00636 break;
00637 }
00638 }
00639 if(FALSE == TestResult)
00640 print(AES_EXAMPLE_USART, "\nKO!!!\n");
00641 else
00642 {
00643 print(AES_EXAMPLE_USART, "\nOK!!! Nb cycles: ");
00644 print_ulong(AES_EXAMPLE_USART, ccountt1 - ccountt0);
00645 }
00646
00647 SLEEP(AVR32_PM_SMODE_STATIC);
00648 while (TRUE);
00649 }