87 #if !defined( _AESOPT_H ) 90 #if defined( __cplusplus ) 98 #include "brg_endian.h" 109 #define ENCRYPTION_IN_C 1 110 #define DECRYPTION_IN_C 2 111 #define ENC_KEYING_IN_C 4 112 #define DEC_KEYING_IN_C 8 116 #define FOUR_TABLES 4 155 # define ALGORITHM_BYTE_ORDER PLATFORM_BYTE_ORDER 157 # define ALGORITHM_BYTE_ORDER IS_LITTLE_ENDIAN 159 # define ALGORITHM_BYTE_ORDER IS_BIG_ENDIAN 161 # error The algorithm byte order is not defined 166 #if !defined(__APPLE__) && defined( __GNUC__ ) && defined( __i386__ ) \ 167 || defined( _WIN32 ) && defined( _M_IX86 ) \ 168 && !(defined( _WIN64 ) || defined( _WIN32_WCE ) || defined( _MSC_VER ) && ( _MSC_VER <= 800 )) 169 # define VIA_ACE_POSSIBLE 187 #if 1 && defined( VIA_ACE_POSSIBLE ) && !defined( USE_VIA_ACE_IF_PRESENT ) 188 # define USE_VIA_ACE_IF_PRESENT 191 #if 0 && defined( VIA_ACE_POSSIBLE ) && !defined( ASSUME_VIA_ACE_PRESENT ) 192 # define ASSUME_VIA_ACE_PRESENT 214 #if 0 && !defined( ASM_X86_V1C ) 216 #elif 0 && !defined( ASM_X86_V2 ) 218 #elif 0 && !defined( ASM_X86_V2C ) 220 #elif 0 && !defined( ASM_AMD64_C ) 224 #if (defined ( ASM_X86_V1C ) || defined( ASM_X86_V2 ) || defined( ASM_X86_V2C )) \ 225 && !defined( _M_IX86 ) || defined( ASM_AMD64_C ) && !defined( _M_X64 ) 226 # error Assembler code is only available for x86 and AMD64 systems 245 #if 1 && !defined( _MSC_VER ) 261 # define ENC_UNROLL FULL 263 # define ENC_UNROLL PARTIAL 265 # define ENC_UNROLL NONE 269 # define DEC_UNROLL FULL 271 # define DEC_UNROLL PARTIAL 273 # define DEC_UNROLL NONE 277 # define ENC_KS_UNROLL 281 # define DEC_KS_UNROLL 310 #if 1 && !(defined( _MSC_VER ) && ( _MSC_VER <= 800 )) 311 # define FIXED_TABLES 320 # define to_byte(x) ((uint_8t)(x)) 322 # define to_byte(x) ((x) & 0xff) 334 #if 1 && defined( _MSC_VER ) && ( _MSC_VER >= 1300 ) 335 # define TABLE_ALIGN 32 344 #if 1 && (defined( ASM_X86_V2 ) || defined( ASM_X86_V2C )) 345 # define REDUCE_CODE_SIZE 369 # define ENC_ROUND FOUR_TABLES 371 # define ENC_ROUND ONE_TABLE 373 # define ENC_ROUND NO_TABLES 377 # define LAST_ENC_ROUND FOUR_TABLES 379 # define LAST_ENC_ROUND ONE_TABLE 381 # define LAST_ENC_ROUND NO_TABLES 385 # define DEC_ROUND FOUR_TABLES 387 # define DEC_ROUND ONE_TABLE 389 # define DEC_ROUND NO_TABLES 393 # define LAST_DEC_ROUND FOUR_TABLES 395 # define LAST_DEC_ROUND ONE_TABLE 397 # define LAST_DEC_ROUND NO_TABLES 405 # define KEY_SCHED FOUR_TABLES 407 # define KEY_SCHED ONE_TABLE 409 # define KEY_SCHED NO_TABLES 416 #if !defined( _MSC_VER ) && !defined( __GNUC__ ) 417 # if defined( ASSUME_VIA_ACE_PRESENT ) 418 # undef ASSUME_VIA_ACE_PRESENT 420 # if defined( USE_VIA_ACE_IF_PRESENT ) 421 # undef USE_VIA_ACE_IF_PRESENT 425 #if defined( ASSUME_VIA_ACE_PRESENT ) && !defined( USE_VIA_ACE_IF_PRESENT ) 426 # define USE_VIA_ACE_IF_PRESENT 429 #if defined( USE_VIA_ACE_IF_PRESENT ) && !defined ( AES_REV_DKS ) 435 #if ( defined( ASM_X86_V1C ) || defined( ASM_X86_V2C ) || defined( ASM_AMD64_C ) ) \ 436 && (ALGORITHM_BYTE_ORDER != PLATFORM_BYTE_ORDER) 437 # undef ALGORITHM_BYTE_ORDER 438 # define ALGORITHM_BYTE_ORDER PLATFORM_BYTE_ORDER 452 #if defined( ARRAYS ) 464 #if !defined( AES_ENCRYPT ) 465 # define EFUNCS_IN_C 0 466 #elif defined( ASSUME_VIA_ACE_PRESENT ) || defined( ASM_X86_V1C ) \ 467 || defined( ASM_X86_V2C ) || defined( ASM_AMD64_C ) 468 # define EFUNCS_IN_C ENC_KEYING_IN_C 469 #elif !defined( ASM_X86_V2 ) 470 # define EFUNCS_IN_C ( ENCRYPTION_IN_C | ENC_KEYING_IN_C ) 472 # define EFUNCS_IN_C 0 475 #if !defined( AES_DECRYPT ) 476 # define DFUNCS_IN_C 0 477 #elif defined( ASSUME_VIA_ACE_PRESENT ) || defined( ASM_X86_V1C ) \ 478 || defined( ASM_X86_V2C ) || defined( ASM_AMD64_C ) 479 # define DFUNCS_IN_C DEC_KEYING_IN_C 480 #elif !defined( ASM_X86_V2 ) 481 # define DFUNCS_IN_C ( DECRYPTION_IN_C | DEC_KEYING_IN_C ) 483 # define DFUNCS_IN_C 0 486 #define FUNCS_IN_C ( EFUNCS_IN_C | DFUNCS_IN_C ) 490 #define RC_LENGTH (5 * (AES_BLOCK_SIZE / 4 - 2)) 494 #if ENC_ROUND == NO_TABLES && LAST_ENC_ROUND != NO_TABLES 495 # undef LAST_ENC_ROUND 496 # define LAST_ENC_ROUND NO_TABLES 497 #elif ENC_ROUND == ONE_TABLE && LAST_ENC_ROUND == FOUR_TABLES 498 # undef LAST_ENC_ROUND 499 # define LAST_ENC_ROUND ONE_TABLE 502 #if ENC_ROUND == NO_TABLES && ENC_UNROLL != NONE 504 # define ENC_UNROLL NONE 507 #if DEC_ROUND == NO_TABLES && LAST_DEC_ROUND != NO_TABLES 508 # undef LAST_DEC_ROUND 509 # define LAST_DEC_ROUND NO_TABLES 510 #elif DEC_ROUND == ONE_TABLE && LAST_DEC_ROUND == FOUR_TABLES 511 # undef LAST_DEC_ROUND 512 # define LAST_DEC_ROUND ONE_TABLE 515 #if DEC_ROUND == NO_TABLES && DEC_UNROLL != NONE 517 # define DEC_UNROLL NONE 520 #if defined( bswap32 ) 521 # define aes_sw32 bswap32 522 #elif defined( bswap_32 ) 523 # define aes_sw32 bswap_32 525 # define brot(x,n) (((uint_32t)(x) << n) | ((uint_32t)(x) >> (32 - n))) 526 # define aes_sw32(x) ((brot((x),8) & 0x00ff00ff) | (brot((x),24) & 0xff00ff00)) 540 #if ( ALGORITHM_BYTE_ORDER == IS_LITTLE_ENDIAN ) 541 # define upr(x,n) (((uint_32t)(x) << (8 * (n))) | ((uint_32t)(x) >> (32 - 8 * (n)))) 542 # define ups(x,n) ((uint_32t) (x) << (8 * (n))) 543 # define bval(x,n) to_byte((x) >> (8 * (n))) 544 # define bytes2word(b0, b1, b2, b3) \ 545 (((uint_32t)(b3) << 24) | ((uint_32t)(b2) << 16) | ((uint_32t)(b1) << 8) | (b0)) 548 #if ( ALGORITHM_BYTE_ORDER == IS_BIG_ENDIAN ) 549 # define upr(x,n) (((uint_32t)(x) >> (8 * (n))) | ((uint_32t)(x) << (32 - 8 * (n)))) 550 # define ups(x,n) ((uint_32t) (x) >> (8 * (n))) 551 # define bval(x,n) to_byte((x) >> (24 - 8 * (n))) 552 # define bytes2word(b0, b1, b2, b3) \ 553 (((uint_32t)(b0) << 24) | ((uint_32t)(b1) << 16) | ((uint_32t)(b2) << 8) | (b3)) 556 #if defined( SAFE_IO ) 557 # define word_in(x,c) bytes2word(((const uint_8t*)(x)+4*c)[0], ((const uint_8t*)(x)+4*c)[1], \ 558 ((const uint_8t*)(x)+4*c)[2], ((const uint_8t*)(x)+4*c)[3]) 559 # define word_out(x,c,v) { ((uint_8t*)(x)+4*c)[0] = bval(v,0); ((uint_8t*)(x)+4*c)[1] = bval(v,1); \ 560 ((uint_8t*)(x)+4*c)[2] = bval(v,2); ((uint_8t*)(x)+4*c)[3] = bval(v,3); } 561 #elif ( ALGORITHM_BYTE_ORDER == PLATFORM_BYTE_ORDER ) 562 # define word_in(x,c) (*((uint_32t*)(x)+(c))) 563 # define word_out(x,c,v) (*((uint_32t*)(x)+(c)) = (v)) 565 # define word_in(x,c) aes_sw32(*((uint_32t*)(x)+(c))) 566 # define word_out(x,c,v) (*((uint_32t*)(x)+(c)) = aes_sw32(v)) 576 #define gf_c1 0x80808080 577 #define gf_c2 0x7f7f7f7f 578 #define gf_mulx(x) ((((x) & gf_c2) << 1) ^ ((((x) & gf_c1) >> 7) * BPOLY)) 591 #if defined( ASM_X86_V1C ) 592 # if defined( ENC_ROUND ) 595 # define ENC_ROUND FOUR_TABLES 596 # if defined( LAST_ENC_ROUND ) 597 # undef LAST_ENC_ROUND 599 # define LAST_ENC_ROUND FOUR_TABLES 600 # if defined( DEC_ROUND ) 603 # define DEC_ROUND FOUR_TABLES 604 # if defined( LAST_DEC_ROUND ) 605 # undef LAST_DEC_ROUND 607 # define LAST_DEC_ROUND FOUR_TABLES 608 # if defined( KEY_SCHED ) 610 # define KEY_SCHED FOUR_TABLES 614 #if ( FUNCS_IN_C & ENCRYPTION_IN_C ) || defined( ASM_X86_V1C ) 615 # if ENC_ROUND == ONE_TABLE 617 # elif ENC_ROUND == FOUR_TABLES 622 # if LAST_ENC_ROUND == ONE_TABLE 624 # elif LAST_ENC_ROUND == FOUR_TABLES 626 # elif !defined( SBX_SET ) 631 #if ( FUNCS_IN_C & DECRYPTION_IN_C ) || defined( ASM_X86_V1C ) 632 # if DEC_ROUND == ONE_TABLE 634 # elif DEC_ROUND == FOUR_TABLES 639 # if LAST_DEC_ROUND == ONE_TABLE 641 # elif LAST_DEC_ROUND == FOUR_TABLES 643 # elif !defined(ISB_SET) 648 #if !(defined( REDUCE_CODE_SIZE ) && (defined( ASM_X86_V2 ) || defined( ASM_X86_V2C ))) 649 # if ((FUNCS_IN_C & ENC_KEYING_IN_C) || (FUNCS_IN_C & DEC_KEYING_IN_C)) 650 # if KEY_SCHED == ONE_TABLE 651 # if !defined( FL1_SET ) && !defined( FL4_SET ) 654 # elif KEY_SCHED == FOUR_TABLES 655 # if !defined( FL4_SET ) 658 # elif !defined( SBX_SET ) 662 # if (FUNCS_IN_C & DEC_KEYING_IN_C) 663 # if KEY_SCHED == ONE_TABLE 665 # elif KEY_SCHED == FOUR_TABLES 667 # elif !defined( SBX_SET ) 675 #define no_table(x,box,vf,rf,c) bytes2word( \ 676 box[bval(vf(x,0,c),rf(0,c))], \ 677 box[bval(vf(x,1,c),rf(1,c))], \ 678 box[bval(vf(x,2,c),rf(2,c))], \ 679 box[bval(vf(x,3,c),rf(3,c))]) 681 #define one_table(x,op,tab,vf,rf,c) \ 682 ( tab[bval(vf(x,0,c),rf(0,c))] \ 683 ^ op(tab[bval(vf(x,1,c),rf(1,c))],1) \ 684 ^ op(tab[bval(vf(x,2,c),rf(2,c))],2) \ 685 ^ op(tab[bval(vf(x,3,c),rf(3,c))],3)) 687 #define four_tables(x,tab,vf,rf,c) \ 688 ( tab[0][bval(vf(x,0,c),rf(0,c))] \ 689 ^ tab[1][bval(vf(x,1,c),rf(1,c))] \ 690 ^ tab[2][bval(vf(x,2,c),rf(2,c))] \ 691 ^ tab[3][bval(vf(x,3,c),rf(3,c))]) 693 #define vf1(x,r,c) (x) 695 #define rf2(r,c) ((8+r-c)&3) 700 #if !(defined( REDUCE_CODE_SIZE ) && (defined( ASM_X86_V2 ) || defined( ASM_X86_V2C ))) 702 #if defined( FM4_SET ) 703 # define fwd_mcol(x) four_tables(x,t_use(f,m),vf1,rf1,0) 704 #elif defined( FM1_SET ) 705 # define fwd_mcol(x) one_table(x,upr,t_use(f,m),vf1,rf1,0) 707 # define dec_fmvars uint_32t g2 708 # define fwd_mcol(x) (g2 = gf_mulx(x), g2 ^ upr((x) ^ g2, 3) ^ upr((x), 2) ^ upr((x), 1)) 711 #if defined( IM4_SET ) 712 # define inv_mcol(x) four_tables(x,t_use(i,m),vf1,rf1,0) 713 #elif defined( IM1_SET ) 714 # define inv_mcol(x) one_table(x,upr,t_use(i,m),vf1,rf1,0) 716 # define dec_imvars uint_32t g2, g4, g9 717 # define inv_mcol(x) (g2 = gf_mulx(x), g4 = gf_mulx(g2), g9 = (x) ^ gf_mulx(g4), g4 ^= g9, \ 718 (x) ^ g2 ^ g4 ^ upr(g2 ^ g9, 3) ^ upr(g4, 2) ^ upr(g9, 1)) 721 #if defined( FL4_SET ) 722 # define ls_box(x,c) four_tables(x,t_use(f,l),vf1,rf2,c) 723 #elif defined( LS4_SET ) 724 # define ls_box(x,c) four_tables(x,t_use(l,s),vf1,rf2,c) 725 #elif defined( FL1_SET ) 726 # define ls_box(x,c) one_table(x,upr,t_use(f,l),vf1,rf2,c) 727 #elif defined( LS1_SET ) 728 # define ls_box(x,c) one_table(x,upr,t_use(l,s),vf1,rf2,c) 730 # define ls_box(x,c) no_table(x,t_use(s,box),vf1,rf2,c) 735 #if defined( ASM_X86_V1C ) && defined( AES_DECRYPT ) && !defined( ISB_SET )