00001
00067 #include "AES_driver.h"
00068
00069
00081 void AES_interrupt_driver_init(AES_interrupt_driver_t * interrupt_driver,
00082 uint8_t * input_ptr,
00083 uint8_t * output_ptr,
00084 uint8_t * AES_key,
00085 uint8_t * AES_CBC_init,
00086 uint8_t block_count,
00087 bool decrypt)
00088 {
00089
00090 interrupt_driver->block_count = block_count;
00091 interrupt_driver->blocks_left = block_count;
00092 interrupt_driver->output_ptr = output_ptr;
00093 interrupt_driver->input_ptr = input_ptr;
00094 interrupt_driver->key_ptr = AES_key;
00095 interrupt_driver->init_ptr = AES_CBC_init;
00096 interrupt_driver->decrypt = decrypt;
00097 }
00098
00099
00100
00112 bool AES_interrupt_driver_start(AES_interrupt_driver_t * interrupt_driver,
00113 AES_INTLVL_t int_lvl)
00114 {
00115 bool start_ok;
00116
00117
00118 AES.STATUS = (AES_ERROR_bm | AES_SRIF_bm);
00119
00120
00121
00122 AES.INTCTRL = int_lvl;
00123
00124
00125 if(interrupt_driver->decrypt){
00126 AES.CTRL |= AES_DECRYPT_bm;
00127 }else{
00128 AES.CTRL = AES.CTRL & (~AES_DECRYPT_bm);
00129 }
00130
00131
00132
00133
00134 if((interrupt_driver->block_count > 1) && !(interrupt_driver->decrypt)){
00135
00136
00137 uint8_t * temp_key_ptr = interrupt_driver->key_ptr;
00138 for(uint8_t i = 0; i < AES_BLOCK_LENGTH; i++){
00139 AES.KEY = *(temp_key_ptr++);
00140 }
00141
00142
00143 uint8_t * temp_input_ptr = interrupt_driver->input_ptr;
00144 for(uint8_t i = 0; i < AES_BLOCK_LENGTH; i++){
00145 AES.STATE = *(temp_input_ptr++);
00146 }
00147 interrupt_driver->input_ptr = temp_input_ptr;
00148
00149
00150 AES.CTRL = AES.CTRL | AES_XOR_bm | AES_AUTO_bm;
00151
00152
00153
00154
00155 uint8_t * temp_init_ptr = interrupt_driver->init_ptr;
00156 for(uint8_t i = 0; i < AES_BLOCK_LENGTH; i++){
00157 AES.STATE = *(temp_init_ptr++);
00158 }
00159
00160
00161 start_ok = !(AES_error_flag_check());
00162 }
00163
00164
00165 else{
00166
00167
00168 volatile uint8_t * temp_key_ptr = interrupt_driver->key_ptr;
00169 for(uint8_t i = 0; i < AES_BLOCK_LENGTH; i++){
00170 AES.KEY = *(temp_key_ptr++);
00171 }
00172
00173
00174 AES.CTRL |= AES_AUTO_bm;
00175
00176
00177 uint8_t * temp_input_ptr = interrupt_driver->input_ptr;
00178 for(uint8_t i = 0; i < AES_BLOCK_LENGTH; i++){
00179 AES.STATE = *(temp_input_ptr++);
00180 }
00181
00182
00183 interrupt_driver->input_ptr = temp_input_ptr;
00184
00185
00186 start_ok = !(AES_error_flag_check());
00187 }
00188
00189 return start_ok;
00190 }
00191
00192
00193
00200 void AES_interrupt_handler(AES_interrupt_driver_t * interrupt_driver)
00201 {
00202
00203
00204 if(!(interrupt_driver->decrypt)){
00205
00206
00207 uint8_t * temp_output_ptr = interrupt_driver->output_ptr;
00208 for(uint8_t i = 0; i < AES_BLOCK_LENGTH; i++){
00209 *(temp_output_ptr++) = AES.STATE;
00210 }
00211
00212
00213
00214 interrupt_driver->output_ptr = temp_output_ptr;
00215 interrupt_driver->blocks_left -= 1;
00216
00217
00218 if(interrupt_driver->blocks_left > 0){
00219
00220
00221 uint8_t * temp_key_ptr = interrupt_driver->key_ptr;
00222 for(uint8_t i = 0; i < AES_BLOCK_LENGTH; i++){
00223 AES.KEY = *(temp_key_ptr++);
00224 }
00225
00226
00227
00228
00229 uint8_t * temp_input_ptr = interrupt_driver->input_ptr;
00230 for(uint8_t i = 0; i < AES_BLOCK_LENGTH; i++){
00231 AES.STATE = *(temp_input_ptr++);
00232 }
00233
00234
00235 interrupt_driver->input_ptr = temp_input_ptr;
00236 }
00237 }
00238
00239
00240
00241
00242 else{
00243
00244
00245
00246 if(interrupt_driver->block_count == 1){
00247
00248
00249 uint8_t * temp_output_pointer = interrupt_driver->output_ptr;
00250 for(uint8_t i = 0; i < AES_BLOCK_LENGTH; i++){
00251 *(temp_output_pointer + i) = AES.STATE;
00252 }
00253 }
00254
00255 else{
00256
00257
00258 AES.CTRL = (AES.CTRL & (~AES_AUTO_bm)) | AES_XOR_bm;
00259
00260
00261
00262
00263 uint8_t * temp_ptr;
00264 uint8_t temp_blocks_left = interrupt_driver->blocks_left;
00265 if(interrupt_driver->block_count == temp_blocks_left){
00266 temp_ptr = interrupt_driver->init_ptr;
00267 }
00268
00269
00270 else{
00271 temp_ptr = interrupt_driver->input_ptr -(AES_BLOCK_LENGTH*2);
00272 }
00273
00274
00275
00276 for(uint8_t i = 0; i < AES_BLOCK_LENGTH; i++){
00277 AES.STATE = *(temp_ptr++);
00278 }
00279
00280
00281 uint8_t * temp_output_ptr = interrupt_driver->output_ptr;
00282 for(uint8_t i = 0; i < AES_BLOCK_LENGTH; i++){
00283 *(temp_output_ptr++) = AES.STATE;
00284 }
00285
00286
00287
00288 interrupt_driver->output_ptr = temp_output_ptr;
00289 interrupt_driver->blocks_left -= 1;
00290
00291
00292 if(interrupt_driver->blocks_left > 0){
00293
00294
00295 AES.CTRL = (AES.CTRL & (~AES_XOR_bm)) | AES_AUTO_bm;
00296
00297
00298 uint8_t * temp_key_ptr = interrupt_driver->key_ptr;
00299 for(uint8_t i = 0; i < AES_BLOCK_LENGTH; i++){
00300 AES.KEY = *(temp_key_ptr++);
00301 }
00302
00303
00304
00305 uint8_t * temp_input_ptr = interrupt_driver->input_ptr;
00306 for(uint8_t i = 0; i < AES_BLOCK_LENGTH; i++){
00307 AES.STATE = *(temp_input_ptr++);
00308 }
00309
00310
00311 interrupt_driver->input_ptr = temp_input_ptr;
00312 }
00313 }
00314 }
00315 }
00316
00317
00318
00326 bool AES_interrupt_driver_finished(AES_interrupt_driver_t * interrupt_driver)
00327 {
00328 bool finished = (interrupt_driver->blocks_left == 0);
00329 return finished;
00330 }
00331
00332
00333
00345 bool AES_encrypt(uint8_t * plaintext, uint8_t * ciphertext, uint8_t * key)
00346 {
00347 bool encrypt_ok;
00348
00349
00350 uint8_t * temp_key = key;
00351 for(uint8_t i = 0; i < AES_BLOCK_LENGTH; i++){
00352 AES.KEY = *(temp_key++);
00353 }
00354
00355
00356 uint8_t * temp_plaintext = plaintext;
00357 for(uint8_t i = 0; i < AES_BLOCK_LENGTH; i++){
00358 AES.STATE = *(temp_plaintext++);
00359 }
00360
00361
00362 AES.CTRL = (AES.CTRL & (~AES_DECRYPT_bm)) | AES_START_bm;
00363
00364 do{
00365
00366 }while((AES.STATUS & (AES_SRIF_bm|AES_ERROR_bm) ) == 0);
00367
00368
00369 if((AES.STATUS & AES_ERROR_bm) == 0){
00370
00371 uint8_t * temp_ciphertext = ciphertext;
00372 for(uint8_t i = 0; i < AES_BLOCK_LENGTH; i++){
00373 *(temp_ciphertext++) = AES.STATE;
00374 }
00375 encrypt_ok = true;
00376 }else{
00377 encrypt_ok = false;
00378
00379 }
00380 return encrypt_ok;
00381 }
00382
00383
00384
00396 bool AES_decrypt(uint8_t * ciphertext, uint8_t * plaintext,
00397 uint8_t * key)
00398 {
00399 bool decrypt_ok;
00400
00401
00402 uint8_t * temp_key = key;
00403 for(uint8_t i = 0; i < AES_BLOCK_LENGTH; i++){
00404 AES.KEY = *(temp_key++);
00405 }
00406
00407
00408 uint8_t * temp_ciphertext = ciphertext;
00409 for(uint8_t i = 0; i < AES_BLOCK_LENGTH; i++){
00410 AES.STATE = *(temp_ciphertext++);
00411 }
00412
00413
00414 AES.CTRL |= (AES_START_bm | AES_DECRYPT_bm);
00415
00416 do{
00417
00418 }while((AES.STATUS & (AES_SRIF_bm|AES_ERROR_bm) ) == 0);
00419
00420
00421 if((AES.STATUS & AES_ERROR_bm) == 0){
00422
00423 uint8_t * temp_plaintext = plaintext;
00424 for(uint8_t i = 0; i < AES_BLOCK_LENGTH; i++){
00425 *(temp_plaintext++) = AES.STATE;
00426 }
00427 decrypt_ok = true;
00428 }else{
00429 decrypt_ok = false;
00430
00431 }
00432 return decrypt_ok;
00433 }
00434
00435
00436
00437
00450 bool AES_lastsubkey_generate(uint8_t * key, uint8_t * last_sub_key)
00451 {
00452 bool keygen_ok;
00453 AES_software_reset();
00454
00455
00456 uint8_t * temp_key = key;
00457 for(uint8_t i = 0; i < AES_BLOCK_LENGTH; i++){
00458 AES.KEY = *(temp_key++);
00459 }
00460
00461
00462 for(uint8_t i = 0; i < AES_BLOCK_LENGTH; i++){
00463 AES.STATE = 0x00;
00464 }
00465
00466
00467 AES.CTRL = (AES.CTRL & (~AES_DECRYPT_bm)) | AES_START_bm;
00468
00469
00470 do{
00471
00472 }while((AES.STATUS & (AES_SRIF_bm|AES_ERROR_bm) ) == 0);
00473
00474
00475 if((AES.STATUS & AES_ERROR_bm) == 0){
00476
00477 uint8_t * temp_last_sub_key = last_sub_key;
00478 for(uint8_t i = 0; i < AES_BLOCK_LENGTH; i++){
00479 *(temp_last_sub_key++) = AES.KEY;
00480 }
00481 AES.STATUS = AES_SRIF_bm;
00482 keygen_ok = true;
00483 }else{
00484 AES.STATUS = AES_ERROR_bm;
00485 keygen_ok = false;
00486
00487 }
00488 return keygen_ok;
00489 }
00490
00491
00492
00507 bool AES_CBC_encrypt(uint8_t * plaintext, uint8_t * ciphertext,
00508 uint8_t * key, uint8_t * init, uint16_t block_count)
00509 {
00510 bool CBC_ok = true;
00511
00512
00513 uint8_t * temp_init = init;
00514 for(uint8_t i = 0; i < AES_BLOCK_LENGTH; i++){
00515 AES.STATE = *(temp_init++);
00516 }
00517
00518
00519
00520 AES.CTRL = (AES.CTRL & (~AES_DECRYPT_bm))| AES_XOR_bm |AES_AUTO_bm;
00521
00522
00523 uint8_t * temp_plaintext = plaintext;
00524 uint8_t * temp_ciphertext = ciphertext;
00525
00526 for(uint8_t blocks_left = block_count; blocks_left > 0; blocks_left--){
00527
00528
00529 uint8_t * temp_key = key;
00530 for(uint8_t i = 0; i < AES_BLOCK_LENGTH; i++){
00531 AES.KEY = *(temp_key++);
00532 }
00533
00534
00535 for(uint8_t i = 0; i < AES_BLOCK_LENGTH; i++){
00536 AES.STATE = *(temp_plaintext++);
00537 }
00538
00539
00540 do{
00541
00542 }while((AES.STATUS & (AES_SRIF_bm|AES_ERROR_bm) ) == 0);
00543
00544
00545 if((AES.STATUS & AES_ERROR_bm) == 0){
00546
00547
00548 uint8_t * temp = temp_ciphertext;
00549 for(uint8_t i = 0; i < AES_BLOCK_LENGTH; i++){
00550 *(temp++) = AES.STATE;
00551 }
00552 temp_ciphertext = temp;
00553 }else{
00554 CBC_ok = false;
00555 }
00556 }
00557
00558
00559 AES.CTRL = (AES.CTRL & ~( AES_XOR_bm |AES_AUTO_bm));
00560
00561 return CBC_ok;
00562 }
00563
00564
00565
00580 bool AES_CBC_decrypt(uint8_t * ciphertext, uint8_t * plaintext,
00581 uint8_t * key, uint8_t * init, uint16_t block_count)
00582 {
00583 bool CBC_ok = true;
00584
00585
00586 uint8_t * temp_plaintext = plaintext;
00587 uint8_t * temp_ciphertext = ciphertext;
00588
00589 for(uint8_t blocks_left = block_count; blocks_left > 0; blocks_left--){
00590
00591
00592 uint8_t * temp_key = key;
00593 for(uint8_t i = 0; i < AES_BLOCK_LENGTH; i++){
00594 AES.KEY = *(temp_key++);
00595 }
00596
00597
00598 uint8_t * temp = temp_ciphertext;
00599 for(uint8_t i = 0; i < AES_BLOCK_LENGTH; i++){
00600 AES.STATE = *(temp++);
00601 }
00602
00603 temp_ciphertext = temp;
00604
00605
00606 AES.CTRL |= (AES_DECRYPT_bm | AES_XOR_bm | AES_START_bm);
00607
00608 do{
00609
00610 }while((AES.STATUS & (AES_SRIF_bm|AES_ERROR_bm) ) == 0);
00611
00612
00613 if((AES.STATUS & AES_ERROR_bm) == 0){
00614
00615
00616 if(blocks_left == block_count){
00617
00618 uint8_t * temp_init = init;
00619 for(uint8_t i = 0; i < AES_BLOCK_LENGTH; i++){
00620 AES.STATE = *(temp_init++);
00621 }
00622 }
00623
00624 else{
00625
00626 uint8_t * last_ciphertext = temp_ciphertext - (AES_BLOCK_LENGTH*2);
00627 for(uint8_t i = 0; i < AES_BLOCK_LENGTH; i++){
00628 AES.STATE = *(last_ciphertext++);
00629 }
00630 }
00631
00632
00633 AES.CTRL = AES.CTRL & (~AES_XOR_bm);
00634
00635
00636 uint8_t * temp = temp_plaintext;
00637 for(uint8_t i = 0; i < AES_BLOCK_LENGTH; i++){
00638 *(temp++) = AES.STATE;
00639 }
00640 temp_plaintext = temp;
00641 }else{
00642 CBC_ok = false;
00643 }
00644
00645 }
00646 return CBC_ok;
00647 }
00648
00649
00650
00655 void AES_interruptlevel_set(AES_INTLVL_t int_lvl)
00656 {
00657 AES.INTCTRL = int_lvl;
00658 }
00659
00660
00661
00662
00678 bool AES_encrypt_backtoback(uint8_t * plaintext, uint8_t * ciphertext)
00679 {
00680 bool encrypt_ok;
00681
00682
00683 uint8_t * temp_plaintext = plaintext;
00684 for(uint8_t i = 0; i < AES_BLOCK_LENGTH; i++){
00685 AES.STATE = *(temp_plaintext++);
00686 }
00687
00688
00689 AES.CTRL = (AES.CTRL & (~AES_DECRYPT_bm)) | AES_START_bm;
00690
00691
00692 do{
00693
00694 }while((AES.STATUS & (AES_SRIF_bm|AES_ERROR_bm) ) == 0);
00695
00696
00697 if((AES.STATUS & AES_ERROR_bm) == 0){
00698
00699 uint8_t * temp_ciphertext = ciphertext;
00700 for(uint8_t i = 0; i < AES_BLOCK_LENGTH; i++){
00701 *(temp_ciphertext++) = AES.STATE;
00702 }
00703 encrypt_ok = true;
00704 }else{
00705 encrypt_ok = false;
00706
00707 }
00708 return encrypt_ok;
00709 }
00710
00711
00712
00728 bool AES_decrypt_backtoback(uint8_t * ciphertext, uint8_t * plaintext)
00729 {
00730 bool decrypt_ok;
00731
00732
00733 uint8_t * temp_ciphertext = ciphertext;
00734 for(uint8_t i = 0; i < AES_BLOCK_LENGTH; i++){
00735 AES.STATE = *(temp_ciphertext++);
00736 }
00737
00738
00739 AES.CTRL |= (AES_START_bm | AES_DECRYPT_bm);
00740
00741
00742 do{
00743
00744 }while((AES.STATUS & (AES_SRIF_bm|AES_ERROR_bm) ) == 0);
00745
00746
00747 if((AES.STATUS & AES_ERROR_bm) == 0){
00748
00749 uint8_t * temp_plaintext = plaintext;
00750 for(uint8_t i = 0; i < AES_BLOCK_LENGTH; i++){
00751 *(temp_plaintext++) = AES.STATE;
00752 }
00753 decrypt_ok = true;
00754 }else{
00755 decrypt_ok = false;
00756
00757 }
00758 return decrypt_ok;
00759 }