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