 |
OpenZWave Library 1.2
|
Go to the documentation of this file.
87#if !defined( _AESOPT_H )
90#if defined( __cplusplus )
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
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( __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
186#if 0 && defined( _WIN64 ) && defined( _MSC_VER )
187# define INTEL_AES_POSSIBLE
190#if defined( INTEL_AES_POSSIBLE ) && !defined( USE_INTEL_AES_IF_PRESENT )
191# define USE_INTEL_AES_IF_PRESENT
211#if 0 && defined( VIA_ACE_POSSIBLE ) && !defined( USE_VIA_ACE_IF_PRESENT )
212# define USE_VIA_ACE_IF_PRESENT
215#if 0 && defined( VIA_ACE_POSSIBLE ) && !defined( ASSUME_VIA_ACE_PRESENT )
216# define ASSUME_VIA_ACE_PRESENT
238#if 0 && !defined( ASM_X86_V1C )
240#elif 0 && !defined( ASM_X86_V2 )
242#elif 0 && !defined( ASM_X86_V2C )
244#elif 0 && !defined( ASM_AMD64_C )
248#if (defined ( ASM_X86_V1C ) || defined( ASM_X86_V2 ) || defined( ASM_X86_V2C )) \
249 && !defined( _M_IX86 ) || defined( ASM_AMD64_C ) && !defined( _M_X64 )
250# error Assembler code is only available for x86 and AMD64 systems
269#if 1 && !defined( _MSC_VER )
285# define ENC_UNROLL FULL
287# define ENC_UNROLL PARTIAL
289# define ENC_UNROLL NONE
293# define DEC_UNROLL FULL
295# define DEC_UNROLL PARTIAL
297# define DEC_UNROLL NONE
301# define ENC_KS_UNROLL
305# define DEC_KS_UNROLL
334#if 1 && !(defined( _MSC_VER ) && ( _MSC_VER <= 800 ))
344# define to_byte(x) ((uint8_t)(x))
346# define to_byte(x) ((x) & 0xff)
358#if 1 && defined( _MSC_VER ) && ( _MSC_VER >= 1300 )
359# define TABLE_ALIGN 32
368#if 0 && (defined( ASM_X86_V2 ) || defined( ASM_X86_V2C ))
369# define REDUCE_CODE_SIZE
393# define ENC_ROUND FOUR_TABLES
395# define ENC_ROUND ONE_TABLE
397# define ENC_ROUND NO_TABLES
401# define LAST_ENC_ROUND FOUR_TABLES
403# define LAST_ENC_ROUND ONE_TABLE
405# define LAST_ENC_ROUND NO_TABLES
409# define DEC_ROUND FOUR_TABLES
411# define DEC_ROUND ONE_TABLE
413# define DEC_ROUND NO_TABLES
417# define LAST_DEC_ROUND FOUR_TABLES
419# define LAST_DEC_ROUND ONE_TABLE
421# define LAST_DEC_ROUND NO_TABLES
429# define KEY_SCHED FOUR_TABLES
431# define KEY_SCHED ONE_TABLE
433# define KEY_SCHED NO_TABLES
440#if !defined( _MSC_VER ) && !defined( __GNUC__ )
441# if defined( ASSUME_VIA_ACE_PRESENT )
442# undef ASSUME_VIA_ACE_PRESENT
444# if defined( USE_VIA_ACE_IF_PRESENT )
445# undef USE_VIA_ACE_IF_PRESENT
449#if defined( ASSUME_VIA_ACE_PRESENT ) && !defined( USE_VIA_ACE_IF_PRESENT )
450# define USE_VIA_ACE_IF_PRESENT
454#if 1 || defined( USE_VIA_ACE_IF_PRESENT ) && !defined ( AES_REV_DKS )
459#if defined( USE_INTEL_AES_IF_PRESENT ) && defined ( AES_REV_DKS )
465#if ( defined( ASM_X86_V1C ) || defined( ASM_X86_V2C ) || defined( ASM_AMD64_C ) ) \
466 && (ALGORITHM_BYTE_ORDER != PLATFORM_BYTE_ORDER)
467# undef ALGORITHM_BYTE_ORDER
468# define ALGORITHM_BYTE_ORDER PLATFORM_BYTE_ORDER
494#if !defined( AES_ENCRYPT )
495# define EFUNCS_IN_C 0
496#elif defined( ASSUME_VIA_ACE_PRESENT ) || defined( ASM_X86_V1C ) \
497 || defined( ASM_X86_V2C ) || defined( ASM_AMD64_C )
498# define EFUNCS_IN_C ENC_KEYING_IN_C
499#elif !defined( ASM_X86_V2 )
500# define EFUNCS_IN_C ( ENCRYPTION_IN_C | ENC_KEYING_IN_C )
502# define EFUNCS_IN_C 0
505#if !defined( AES_DECRYPT )
506# define DFUNCS_IN_C 0
507#elif defined( ASSUME_VIA_ACE_PRESENT ) || defined( ASM_X86_V1C ) \
508 || defined( ASM_X86_V2C ) || defined( ASM_AMD64_C )
509# define DFUNCS_IN_C DEC_KEYING_IN_C
510#elif !defined( ASM_X86_V2 )
511# define DFUNCS_IN_C ( DECRYPTION_IN_C | DEC_KEYING_IN_C )
513# define DFUNCS_IN_C 0
516#define FUNCS_IN_C ( EFUNCS_IN_C | DFUNCS_IN_C )
520#define RC_LENGTH (5 * (AES_BLOCK_SIZE / 4 - 2))
524#if ENC_ROUND == NO_TABLES && LAST_ENC_ROUND != NO_TABLES
525# undef LAST_ENC_ROUND
526# define LAST_ENC_ROUND NO_TABLES
527#elif ENC_ROUND == ONE_TABLE && LAST_ENC_ROUND == FOUR_TABLES
528# undef LAST_ENC_ROUND
529# define LAST_ENC_ROUND ONE_TABLE
532#if ENC_ROUND == NO_TABLES && ENC_UNROLL != NONE
534# define ENC_UNROLL NONE
537#if DEC_ROUND == NO_TABLES && LAST_DEC_ROUND != NO_TABLES
538# undef LAST_DEC_ROUND
539# define LAST_DEC_ROUND NO_TABLES
540#elif DEC_ROUND == ONE_TABLE && LAST_DEC_ROUND == FOUR_TABLES
541# undef LAST_DEC_ROUND
542# define LAST_DEC_ROUND ONE_TABLE
545#if DEC_ROUND == NO_TABLES && DEC_UNROLL != NONE
547# define DEC_UNROLL NONE
550#if defined( bswap32 )
551# define aes_sw32 bswap32
552#elif defined( bswap_32 )
553# define aes_sw32 bswap_32
555# define brot(x,n) (((uint32_t)(x) << n) | ((uint32_t)(x) >> (32 - n)))
556# define aes_sw32(x) ((brot((x),8) & 0x00ff00ff) | (brot((x),24) & 0xff00ff00))
570#if ( ALGORITHM_BYTE_ORDER == IS_LITTLE_ENDIAN )
571# define upr(x,n) (((uint32_t)(x) << (8 * (n))) | ((uint32_t)(x) >> (32 - 8 * (n))))
572# define ups(x,n) ((uint32_t) (x) << (8 * (n)))
573# define bval(x,n) to_byte((x) >> (8 * (n)))
574# define bytes2word(b0, b1, b2, b3) \
575 (((uint32_t)(b3) << 24) | ((uint32_t)(b2) << 16) | ((uint32_t)(b1) << 8) | (b0))
578#if ( ALGORITHM_BYTE_ORDER == IS_BIG_ENDIAN )
579# define upr(x,n) (((uint32_t)(x) >> (8 * (n))) | ((uint32_t)(x) << (32 - 8 * (n))))
580# define ups(x,n) ((uint32_t) (x) >> (8 * (n)))
581# define bval(x,n) to_byte((x) >> (24 - 8 * (n)))
582# define bytes2word(b0, b1, b2, b3) \
583 (((uint32_t)(b0) << 24) | ((uint32_t)(b1) << 16) | ((uint32_t)(b2) << 8) | (b3))
586#if defined( SAFE_IO )
587# define word_in(x,c) bytes2word(((const uint8_t*)(x)+4*c)[0], ((const uint8_t*)(x)+4*c)[1], \
588 ((const uint8_t*)(x)+4*c)[2], ((const uint8_t*)(x)+4*c)[3])
589# define word_out(x,c,v) { ((uint8_t*)(x)+4*c)[0] = bval(v,0); ((uint8_t*)(x)+4*c)[1] = bval(v,1); \
590 ((uint8_t*)(x)+4*c)[2] = bval(v,2); ((uint8_t*)(x)+4*c)[3] = bval(v,3); }
591#elif ( ALGORITHM_BYTE_ORDER == PLATFORM_BYTE_ORDER )
592# define word_in(x,c) (*((uint32_t*)(x)+(c)))
593# define word_out(x,c,v) (*((uint32_t*)(x)+(c)) = (v))
595# define word_in(x,c) aes_sw32(*((uint32_t*)(x)+(c)))
596# define word_out(x,c,v) (*((uint32_t*)(x)+(c)) = aes_sw32(v))
606#define gf_c1 0x80808080
607#define gf_c2 0x7f7f7f7f
608#define gf_mulx(x) ((((x) & gf_c2) << 1) ^ ((((x) & gf_c1) >> 7) * BPOLY))
621#if defined( ASM_X86_V1C )
622# if defined( ENC_ROUND )
625# define ENC_ROUND FOUR_TABLES
626# if defined( LAST_ENC_ROUND )
627# undef LAST_ENC_ROUND
629# define LAST_ENC_ROUND FOUR_TABLES
630# if defined( DEC_ROUND )
633# define DEC_ROUND FOUR_TABLES
634# if defined( LAST_DEC_ROUND )
635# undef LAST_DEC_ROUND
637# define LAST_DEC_ROUND FOUR_TABLES
638# if defined( KEY_SCHED )
640# define KEY_SCHED FOUR_TABLES
644#if ( FUNCS_IN_C & ENCRYPTION_IN_C ) || defined( ASM_X86_V1C )
645# if ENC_ROUND == ONE_TABLE
647# elif ENC_ROUND == FOUR_TABLES
652# if LAST_ENC_ROUND == ONE_TABLE
654# elif LAST_ENC_ROUND == FOUR_TABLES
656# elif !defined( SBX_SET )
661#if ( FUNCS_IN_C & DECRYPTION_IN_C ) || defined( ASM_X86_V1C )
662# if DEC_ROUND == ONE_TABLE
664# elif DEC_ROUND == FOUR_TABLES
669# if LAST_DEC_ROUND == ONE_TABLE
671# elif LAST_DEC_ROUND == FOUR_TABLES
673# elif !defined(ISB_SET)
678#if !(defined( REDUCE_CODE_SIZE ) && (defined( ASM_X86_V2 ) || defined( ASM_X86_V2C )))
679# if ((FUNCS_IN_C & ENC_KEYING_IN_C) || (FUNCS_IN_C & DEC_KEYING_IN_C))
680# if KEY_SCHED == ONE_TABLE
681# if !defined( FL1_SET ) && !defined( FL4_SET )
684# elif KEY_SCHED == FOUR_TABLES
685# if !defined( FL4_SET )
688# elif !defined( SBX_SET )
692# if (FUNCS_IN_C & DEC_KEYING_IN_C)
693# if KEY_SCHED == ONE_TABLE
695# elif KEY_SCHED == FOUR_TABLES
697# 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( REDUCE_CODE_SIZE ) && (defined( ASM_X86_V2 ) || defined( ASM_X86_V2C )))
732#if defined( FM4_SET )
733# define fwd_mcol(x) four_tables(x,t_use(f,m),vf1,rf1,0)
734#elif defined( FM1_SET )
735# define fwd_mcol(x) one_table(x,upr,t_use(f,m),vf1,rf1,0)
737# define dec_fmvars uint32_t g2
738# define fwd_mcol(x) (g2 = gf_mulx(x), g2 ^ upr((x) ^ g2, 3) ^ upr((x), 2) ^ upr((x), 1))
741#if defined( IM4_SET )
742# define inv_mcol(x) four_tables(x,t_use(i,m),vf1,rf1,0)
743#elif defined( IM1_SET )
744# define inv_mcol(x) one_table(x,upr,t_use(i,m),vf1,rf1,0)
746# define dec_imvars uint32_t g2, g4, g9
747# define inv_mcol(x) (g2 = gf_mulx(x), g4 = gf_mulx(g2), g9 = (x) ^ gf_mulx(g4), g4 ^= g9, \
748 (x) ^ g2 ^ g4 ^ upr(g2 ^ g9, 3) ^ upr(g4, 2) ^ upr(g9, 1))
751#if defined( FL4_SET )
752# define ls_box(x,c) four_tables(x,t_use(f,l),vf1,rf2,c)
753#elif defined( LS4_SET )
754# define ls_box(x,c) four_tables(x,t_use(l,s),vf1,rf2,c)
755#elif defined( FL1_SET )
756# define ls_box(x,c) one_table(x,upr,t_use(f,l),vf1,rf2,c)
757#elif defined( LS1_SET )
758# define ls_box(x,c) one_table(x,upr,t_use(l,s),vf1,rf2,c)
760# define ls_box(x,c) no_table(x,t_use(s,box),vf1,rf2,c)
765#if defined( ASM_X86_V1C ) && defined( AES_DECRYPT ) && !defined( ISB_SET )