1 /*************************************************************************** 2 PLATFORM.H -- Platform-specific defines for TWOFISH code 3 4 Submitters: 5 Bruce Schneier, Counterpane Systems 6 Doug Whiting, Hi/fn 7 John Kelsey, Counterpane Systems 8 Chris Hall, Counterpane Systems 9 David Wagner, UC Berkeley 10 11 Code Author: Doug Whiting, Hi/fn 12 13 Version 1.00 April 1998 14 15 Copyright 1998, Hi/fn and Counterpane Systems. All rights reserved. 16 17 Notes: 18 * Tab size is set to 4 characters in this file 19 20 ***************************************************************************/ 21 22 /* use intrinsic rotate if possible */ 23 #define ROL(x,n) (((x) << ((n) & 0x1F)) | ((x) >> (32-((n) & 0x1F)))) 24 #define ROR(x,n) (((x) >> ((n) & 0x1F)) | ((x) << (32-((n) & 0x1F)))) 25 26 #if (0) && defined(__BORLANDC__) && (__BORLANDC__ >= 0x462) 27 #error "!!!This does not work for some reason!!!" 28 #include <stdlib.h> /* get prototype for _lrotl() , _lrotr() */ 29 #pragma inline __lrotl__ 30 #pragma inline __lrotr__ 31 #undef ROL /* get rid of inefficient definitions */ 32 #undef ROR 33 #define ROL(x,n) __lrotl__(x,n) /* use compiler intrinsic rotations */ 34 #define ROR(x,n) __lrotr__(x,n) 35 #endif 36 37 #ifdef _MSC_VER 38 #include <stdlib.h> /* get prototypes for rotation functions */ 39 #undef ROL 40 #undef ROR 41 #pragma intrinsic(_lrotl,_lrotr) /* use intrinsic compiler rotations */ 42 #define ROL(x,n) _lrotl(x,n) 43 #define ROR(x,n) _lrotr(x,n) 44 #endif 45 46 #ifndef _M_IX86 47 #ifdef __BORLANDC__ 48 #define _M_IX86 300 /* make sure this is defined for Intel CPUs */ 49 #endif 50 #endif 51 52 #if WORDS_BIGENDIAN 53 #define LittleEndian 0 54 #define ALIGN32 1 55 #else 56 #define LittleEndian 1 /* e.g., 1 for Pentium, 0 for 68K */ 57 #define ALIGN32 0 /* need dword alignment? (no for Pentium) */ 58 #endif 59 60 #if LittleEndian 61 #define Bswap(x) (x) /* NOP for little-endian machines */ 62 #define ADDR_XOR 0 /* NOP for little-endian machines */ 63 #else 64 #define Bswap(x) ((ROR(x,8) & 0xFF00FF00) | (ROL(x,8) & 0x00FF00FF)) 65 #define ADDR_XOR 3 /* convert byte address in dword */ 66 #endif 67 68 /* Macros for extracting bytes from dwords (correct for endianness) */ 69 #define _b(x,N) (((BYTE *)&x)[((N) & 3) ^ ADDR_XOR]) /* pick bytes out of a dword */ 70 71 #define b0(x) _b(x,0) /* extract LSB of DWORD */ 72 #define b1(x) _b(x,1) 73 #define b2(x) _b(x,2) 74 #define b3(x) _b(x,3) /* extract MSB of DWORD */ 75 76