Go to the documentation of this file.
94 #if !defined( _AESOPT_H )
104 #if defined( _MSC_VER ) && ( _MSC_VER > 800 ) && \
105 defined( _M_IX86 ) && \
106 !( defined( _WIN32_WCE ) || defined( NO_ASM ) )
107 #ifndef USE_VIA_ACE_IF_PRESENT
108 #define USE_VIA_ACE_IF_PRESENT
112 #if defined( _MSC_VER ) && ( _MSC_VER > 800 ) && \
113 !( defined( _WIN32_WCE ) || defined( NO_ASM ) )
116 #if defined( _M_X64 )
120 #elif defined( _M_IX86 )
129 #if defined( INC_ALL )
137 #if defined( INC_ALL )
152 #define ENCRYPTION_IN_C 1
153 #define DECRYPTION_IN_C 2
154 #define ENC_KEYING_IN_C 4
155 #define DEC_KEYING_IN_C 8
159 #define FOUR_TABLES 4
198 #define ALGORITHM_BYTE_ORDER PLATFORM_BYTE_ORDER
200 #define ALGORITHM_BYTE_ORDER IS_LITTLE_ENDIAN
202 #define ALGORITHM_BYTE_ORDER IS_BIG_ENDIAN
204 #error The algorithm byte order is not defined
224 #if 0 && !defined( USE_VIA_ACE_IF_PRESENT )
225 # define USE_VIA_ACE_IF_PRESENT
228 #if 0 && !defined( ASSUME_VIA_ACE_PRESENT )
229 # define ASSUME_VIA_ACE_PRESENT
232 #if defined ( _WIN64 ) || defined( _WIN32_WCE ) || \
233 defined( _MSC_VER ) && ( _MSC_VER <= 800 )
234 # if defined( USE_VIA_ACE_IF_PRESENT )
235 # undef USE_VIA_ACE_IF_PRESENT
237 # if defined( ASSUME_VIA_ACE_PRESENT )
238 # undef ASSUME_VIA_ACE_PRESENT
261 #if 0 && !defined( ASM_X86_V1C )
263 #elif 0 && !defined( ASM_X86_V2 )
265 #elif 0 && !defined( ASM_X86_V2C )
267 #elif 0 && !defined( ASM_AMD64_C )
271 #if (defined ( ASM_X86_V1C ) || defined( ASM_X86_V2 ) || defined( ASM_X86_V2C )) \
272 && !defined( _M_IX86 ) || defined( ASM_AMD64_C ) && !defined( _M_X64 )
273 # error Assembler code is only available for x86 and AMD64 systems
292 #if 1 && !defined( _MSC_VER )
308 #define ENC_UNROLL FULL
310 #define ENC_UNROLL PARTIAL
312 #define ENC_UNROLL NONE
316 #define DEC_UNROLL FULL
318 #define DEC_UNROLL PARTIAL
320 #define DEC_UNROLL NONE
349 #if 1 && !(defined( _MSC_VER ) && ( _MSC_VER <= 800 ))
359 # define to_byte(x) ((uint_8t)(x))
361 # define to_byte(x) ((x) & 0xff)
373 #if 1 && defined( _MSC_VER ) && ( _MSC_VER >= 1300 )
374 #define TABLE_ALIGN 32
398 #define ENC_ROUND FOUR_TABLES
400 #define ENC_ROUND ONE_TABLE
402 #define ENC_ROUND NO_TABLES
406 #define LAST_ENC_ROUND FOUR_TABLES
408 #define LAST_ENC_ROUND ONE_TABLE
410 #define LAST_ENC_ROUND NO_TABLES
414 #define DEC_ROUND FOUR_TABLES
416 #define DEC_ROUND ONE_TABLE
418 #define DEC_ROUND NO_TABLES
422 #define LAST_DEC_ROUND FOUR_TABLES
424 #define LAST_DEC_ROUND ONE_TABLE
426 #define LAST_DEC_ROUND NO_TABLES
434 #define KEY_SCHED FOUR_TABLES
436 #define KEY_SCHED ONE_TABLE
438 #define KEY_SCHED NO_TABLES
445 #if !defined( _MSC_VER ) && !defined( __GNUC__ )
446 # if defined( ASSUME_VIA_ACE_PRESENT )
447 # undef ASSUME_VIA_ACE_PRESENT
449 # if defined( USE_VIA_ACE_IF_PRESENT )
450 # undef USE_VIA_ACE_IF_PRESENT
454 #if defined( ASSUME_VIA_ACE_PRESENT ) && !defined( USE_VIA_ACE_IF_PRESENT )
455 #define USE_VIA_ACE_IF_PRESENT
458 #if defined( USE_VIA_ACE_IF_PRESENT ) && !defined ( AES_REV_DKS )
464 #if ( defined( ASM_X86_V1C ) || defined( ASM_X86_V2C ) || defined( ASM_AMD64_C ) ) \
465 && (ALGORITHM_BYTE_ORDER != PLATFORM_BYTE_ORDER)
466 #undef ALGORITHM_BYTE_ORDER
467 #define ALGORITHM_BYTE_ORDER PLATFORM_BYTE_ORDER
481 #if defined( ARRAYS )
493 #if !defined( AES_ENCRYPT )
494 # define EFUNCS_IN_C 0
495 #elif defined( ASSUME_VIA_ACE_PRESENT ) || defined( ASM_X86_V1C ) \
496 || defined( ASM_X86_V2C ) || defined( ASM_AMD64_C )
497 # define EFUNCS_IN_C ENC_KEYING_IN_C
498 #elif !defined( ASM_X86_V2 )
499 # define EFUNCS_IN_C ( ENCRYPTION_IN_C | ENC_KEYING_IN_C )
501 # define EFUNCS_IN_C 0
504 #if !defined( AES_DECRYPT )
505 # define DFUNCS_IN_C 0
506 #elif defined( ASSUME_VIA_ACE_PRESENT ) || defined( ASM_X86_V1C ) \
507 || defined( ASM_X86_V2C ) || defined( ASM_AMD64_C )
508 # define DFUNCS_IN_C DEC_KEYING_IN_C
509 #elif !defined( ASM_X86_V2 )
510 # define DFUNCS_IN_C ( DECRYPTION_IN_C | DEC_KEYING_IN_C )
512 # define DFUNCS_IN_C 0
515 #define FUNCS_IN_C ( EFUNCS_IN_C | DFUNCS_IN_C )
519 #define RC_LENGTH (5 * (AES_BLOCK_SIZE / 4 - 2))
523 #if ENC_ROUND == NO_TABLES && LAST_ENC_ROUND != NO_TABLES
524 #undef LAST_ENC_ROUND
525 #define LAST_ENC_ROUND NO_TABLES
526 #elif ENC_ROUND == ONE_TABLE && LAST_ENC_ROUND == FOUR_TABLES
527 #undef LAST_ENC_ROUND
528 #define LAST_ENC_ROUND ONE_TABLE
531 #if ENC_ROUND == NO_TABLES && ENC_UNROLL != NONE
533 #define ENC_UNROLL NONE
536 #if DEC_ROUND == NO_TABLES && LAST_DEC_ROUND != NO_TABLES
537 #undef LAST_DEC_ROUND
538 #define LAST_DEC_ROUND NO_TABLES
539 #elif DEC_ROUND == ONE_TABLE && LAST_DEC_ROUND == FOUR_TABLES
540 #undef LAST_DEC_ROUND
541 #define LAST_DEC_ROUND ONE_TABLE
544 #if DEC_ROUND == NO_TABLES && DEC_UNROLL != NONE
546 #define DEC_UNROLL NONE
549 #if defined( bswap32 )
550 #define aes_sw32 bswap32
551 #elif defined( bswap_32 )
552 #define aes_sw32 bswap_32
554 #define brot(x,n) (((uint_32t)(x) << n) | ((uint_32t)(x) >> (32 - n)))
555 #define aes_sw32(x) ((brot((x),8) & 0x00ff00ff) | (brot((x),24) & 0xff00ff00))
569 #if ( ALGORITHM_BYTE_ORDER == IS_LITTLE_ENDIAN )
570 #define upr(x,n) (((uint_32t)(x) << (8 * (n))) | ((uint_32t)(x) >> (32 - 8 * (n))))
571 #define ups(x,n) ((uint_32t) (x) << (8 * (n)))
572 #define bval(x,n) to_byte((x) >> (8 * (n)))
573 #define bytes2word(b0, b1, b2, b3) \
574 (((uint_32t)(b3) << 24) | ((uint_32t)(b2) << 16) | ((uint_32t)(b1) << 8) | (b0))
577 #if ( ALGORITHM_BYTE_ORDER == IS_BIG_ENDIAN )
578 #define upr(x,n) (((uint_32t)(x) >> (8 * (n))) | ((uint_32t)(x) << (32 - 8 * (n))))
579 #define ups(x,n) ((uint_32t) (x) >> (8 * (n)))
580 #define bval(x,n) to_byte((x) >> (24 - 8 * (n)))
581 #define bytes2word(b0, b1, b2, b3) \
582 (((uint_32t)(b0) << 24) | ((uint_32t)(b1) << 16) | ((uint_32t)(b2) << 8) | (b3))
585 #if defined( SAFE_IO )
587 #define word_in(x,c) bytes2word(((const uint_8t*)(x)+4*c)[0], ((const uint_8t*)(x)+4*c)[1], \
588 ((const uint_8t*)(x)+4*c)[2], ((const uint_8t*)(x)+4*c)[3])
589 #define word_out(x,c,v) { ((uint_8t*)(x)+4*c)[0] = bval(v,0); ((uint_8t*)(x)+4*c)[1] = bval(v,1); \
590 ((uint_8t*)(x)+4*c)[2] = bval(v,2); ((uint_8t*)(x)+4*c)[3] = bval(v,3); }
592 #elif ( ALGORITHM_BYTE_ORDER == PLATFORM_BYTE_ORDER )
594 #define word_in(x,c) (*((uint_32t*)(x)+(c)))
595 #define word_out(x,c,v) (*((uint_32t*)(x)+(c)) = (v))
599 #define word_in(x,c) aes_sw32(*((uint_32t*)(x)+(c)))
600 #define word_out(x,c,v) (*((uint_32t*)(x)+(c)) = aes_sw32(v))
611 #define m1 0x80808080
612 #define m2 0x7f7f7f7f
613 #define gf_mulx(x) ((((x) & m2) << 1) ^ ((((x) & m1) >> 7) * BPOLY))
626 #if defined( ASM_X86_V1C )
627 #if defined( ENC_ROUND )
630 #define ENC_ROUND FOUR_TABLES
631 #if defined( LAST_ENC_ROUND )
632 #undef LAST_ENC_ROUND
634 #define LAST_ENC_ROUND FOUR_TABLES
635 #if defined( DEC_ROUND )
638 #define DEC_ROUND FOUR_TABLES
639 #if defined( LAST_DEC_ROUND )
640 #undef LAST_DEC_ROUND
642 #define LAST_DEC_ROUND FOUR_TABLES
643 #if defined( KEY_SCHED )
645 #define KEY_SCHED FOUR_TABLES
649 #if ( FUNCS_IN_C & ENCRYPTION_IN_C ) || defined( ASM_X86_V1C )
650 #if ENC_ROUND == ONE_TABLE
652 #elif ENC_ROUND == FOUR_TABLES
657 #if LAST_ENC_ROUND == ONE_TABLE
659 #elif LAST_ENC_ROUND == FOUR_TABLES
661 #elif !defined( SBX_SET )
666 #if ( FUNCS_IN_C & DECRYPTION_IN_C ) || defined( ASM_X86_V1C )
667 #if DEC_ROUND == ONE_TABLE
669 #elif DEC_ROUND == FOUR_TABLES
674 #if LAST_DEC_ROUND == ONE_TABLE
676 #elif LAST_DEC_ROUND == FOUR_TABLES
678 #elif !defined(ISB_SET)
683 #if (FUNCS_IN_C & ENC_KEYING_IN_C) || (FUNCS_IN_C & DEC_KEYING_IN_C)
684 #if KEY_SCHED == ONE_TABLE
686 #elif KEY_SCHED == FOUR_TABLES
688 #elif !defined( SBX_SET )
693 #if (FUNCS_IN_C & DEC_KEYING_IN_C)
694 #if KEY_SCHED == ONE_TABLE
696 #elif KEY_SCHED == FOUR_TABLES
698 #elif !defined( SBX_SET )
705 #define no_table(x,box,vf,rf,c) bytes2word( \
706 box[bval(vf(x,0,c),rf(0,c))], \
707 box[bval(vf(x,1,c),rf(1,c))], \
708 box[bval(vf(x,2,c),rf(2,c))], \
709 box[bval(vf(x,3,c),rf(3,c))])
711 #define one_table(x,op,tab,vf,rf,c) \
712 ( tab[bval(vf(x,0,c),rf(0,c))] \
713 ^ op(tab[bval(vf(x,1,c),rf(1,c))],1) \
714 ^ op(tab[bval(vf(x,2,c),rf(2,c))],2) \
715 ^ op(tab[bval(vf(x,3,c),rf(3,c))],3))
717 #define four_tables(x,tab,vf,rf,c) \
718 ( tab[0][bval(vf(x,0,c),rf(0,c))] \
719 ^ tab[1][bval(vf(x,1,c),rf(1,c))] \
720 ^ tab[2][bval(vf(x,2,c),rf(2,c))] \
721 ^ tab[3][bval(vf(x,3,c),rf(3,c))])
723 #define vf1(x,r,c) (x)
725 #define rf2(r,c) ((8+r-c)&3)
730 #if defined( FM4_SET )
731 #define fwd_mcol(x) four_tables(x,t_use(f,m),vf1,rf1,0)
732 #elif defined( FM1_SET )
733 #define fwd_mcol(x) one_table(x,upr,t_use(f,m),vf1,rf1,0)
735 #define dec_fmvars uint_32t g2
736 #define fwd_mcol(x) (g2 = gf_mulx(x), g2 ^ upr((x) ^ g2, 3) ^ upr((x), 2) ^ upr((x), 1))
739 #if defined( IM4_SET )
740 #define inv_mcol(x) four_tables(x,t_use(i,m),vf1,rf1,0)
741 #elif defined( IM1_SET )
742 #define inv_mcol(x) one_table(x,upr,t_use(i,m),vf1,rf1,0)
744 #define dec_imvars uint_32t g2, g4, g9
745 #define inv_mcol(x) (g2 = gf_mulx(x), g4 = gf_mulx(g2), g9 = (x) ^ gf_mulx(g4), g4 ^= g9, \
746 (x) ^ g2 ^ g4 ^ upr(g2 ^ g9, 3) ^ upr(g4, 2) ^ upr(g9, 1))
749 #if defined( FL4_SET )
750 #define ls_box(x,c) four_tables(x,t_use(f,l),vf1,rf2,c)
751 #elif defined( LS4_SET )
752 #define ls_box(x,c) four_tables(x,t_use(l,s),vf1,rf2,c)
753 #elif defined( FL1_SET )
754 #define ls_box(x,c) one_table(x,upr,t_use(f,l),vf1,rf2,c)
755 #elif defined( LS1_SET )
756 #define ls_box(x,c) one_table(x,upr,t_use(l,s),vf1,rf2,c)
758 #define ls_box(x,c) no_table(x,t_use(s,box),vf1,rf2,c)
761 #if defined( ASM_X86_V1C ) && defined( AES_DECRYPT ) && !defined( ISB_SET )