Path: blob/master/crypto/skein_port.h
1310 views
#ifndef _SKEIN_PORT_H_1#define _SKEIN_PORT_H_23#include <limits.h>4#include <stdint.h>56#ifndef RETURN_VALUES7# define RETURN_VALUES8# if defined( DLL_EXPORT )9# if defined( _MSC_VER ) || defined ( __INTEL_COMPILER )10# define VOID_RETURN __declspec( dllexport ) void __stdcall11# define INT_RETURN __declspec( dllexport ) int __stdcall12# elif defined( __GNUC__ )13# define VOID_RETURN __declspec( __dllexport__ ) void14# define INT_RETURN __declspec( __dllexport__ ) int15# else16# error Use of the DLL is only available on the Microsoft, Intel and GCC compilers17# endif18# elif defined( DLL_IMPORT )19# if defined( _MSC_VER ) || defined ( __INTEL_COMPILER )20# define VOID_RETURN __declspec( dllimport ) void __stdcall21# define INT_RETURN __declspec( dllimport ) int __stdcall22# elif defined( __GNUC__ )23# define VOID_RETURN __declspec( __dllimport__ ) void24# define INT_RETURN __declspec( __dllimport__ ) int25# else26# error Use of the DLL is only available on the Microsoft, Intel and GCC compilers27# endif28# elif defined( __WATCOMC__ )29# define VOID_RETURN void __cdecl30# define INT_RETURN int __cdecl31# else32# define VOID_RETURN void33# define INT_RETURN int34# endif35#endif3637/* These defines are used to declare buffers in a way that allows38faster operations on longer variables to be used. In all these39defines 'size' must be a power of 2 and >= 84041dec_unit_type(size,x) declares a variable 'x' of length42'size' bits4344dec_bufr_type(size,bsize,x) declares a buffer 'x' of length 'bsize'45bytes defined as an array of variables46each of 'size' bits (bsize must be a47multiple of size / 8)4849ptr_cast(x,size) casts a pointer to a pointer to a50varaiable of length 'size' bits51*/5253#define ui_type(size) uint##size##_t54#define dec_unit_type(size,x) typedef ui_type(size) x55#define dec_bufr_type(size,bsize,x) typedef ui_type(size) x[bsize / (size >> 3)]56#define ptr_cast(x,size) ((ui_type(size)*)(x))5758typedef unsigned int uint_t; /* native unsigned integer */59typedef uint8_t u08b_t; /* 8-bit unsigned integer */60typedef uint64_t u64b_t; /* 64-bit unsigned integer */6162#ifndef RotL_6463#define RotL_64(x,N) (((x) << (N)) | ((x) >> (64-(N))))64#endif6566/*67* Skein is "natively" little-endian (unlike SHA-xxx), for optimal68* performance on x86 CPUs. The Skein code requires the following69* definitions for dealing with endianness:70*71* SKEIN_NEED_SWAP: 0 for little-endian, 1 for big-endian72* Skein_Put64_LSB_First73* Skein_Get64_LSB_First74* Skein_Swap6475*76* If SKEIN_NEED_SWAP is defined at compile time, it is used here77* along with the portable versions of Put64/Get64/Swap64, which78* are slow in general.79*80* Otherwise, an "auto-detect" of endianness is attempted below.81* If the default handling doesn't work well, the user may insert82* platform-specific code instead (e.g., for big-endian CPUs).83*84*/85#ifndef SKEIN_NEED_SWAP /* compile-time "override" for endianness? */868788#include "int-util.h"8990#define IS_BIG_ENDIAN 4321 /* byte 0 is most significant (mc68k) */91#define IS_LITTLE_ENDIAN 1234 /* byte 0 is least significant (i386) */9293#if BYTE_ORDER == LITTLE_ENDIAN94# define PLATFORM_BYTE_ORDER IS_LITTLE_ENDIAN95#endif9697#if BYTE_ORDER == BIG_ENDIAN98# define PLATFORM_BYTE_ORDER IS_BIG_ENDIAN99#endif100101/* special handler for IA64, which may be either endianness (?) */102/* here we assume little-endian, but this may need to be changed */103#if defined(__ia64) || defined(__ia64__) || defined(_M_IA64)104# define PLATFORM_MUST_ALIGN (1)105#ifndef PLATFORM_BYTE_ORDER106# define PLATFORM_BYTE_ORDER IS_LITTLE_ENDIAN107#endif108#endif109110#ifndef PLATFORM_MUST_ALIGN111# define PLATFORM_MUST_ALIGN (0)112#endif113114115#if PLATFORM_BYTE_ORDER == IS_BIG_ENDIAN116/* here for big-endian CPUs */117#define SKEIN_NEED_SWAP (1)118#elif PLATFORM_BYTE_ORDER == IS_LITTLE_ENDIAN119/* here for x86 and x86-64 CPUs (and other detected little-endian CPUs) */120#define SKEIN_NEED_SWAP (0)121#if PLATFORM_MUST_ALIGN == 0 /* ok to use "fast" versions? */122#define Skein_Put64_LSB_First(dst08,src64,bCnt) memcpy(dst08,src64,bCnt)123#define Skein_Get64_LSB_First(dst64,src08,wCnt) memcpy(dst64,src08,8*(wCnt))124#endif125#else126#error "Skein needs endianness setting!"127#endif128129#endif /* ifndef SKEIN_NEED_SWAP */130131/*132******************************************************************133* Provide any definitions still needed.134******************************************************************135*/136#ifndef Skein_Swap64 /* swap for big-endian, nop for little-endian */137#if SKEIN_NEED_SWAP138#define Skein_Swap64(w64) \139( (( ((u64b_t)(w64)) & 0xFF) << 56) | \140(((((u64b_t)(w64)) >> 8) & 0xFF) << 48) | \141(((((u64b_t)(w64)) >>16) & 0xFF) << 40) | \142(((((u64b_t)(w64)) >>24) & 0xFF) << 32) | \143(((((u64b_t)(w64)) >>32) & 0xFF) << 24) | \144(((((u64b_t)(w64)) >>40) & 0xFF) << 16) | \145(((((u64b_t)(w64)) >>48) & 0xFF) << 8) | \146(((((u64b_t)(w64)) >>56) & 0xFF) ) )147#else148#define Skein_Swap64(w64) (w64)149#endif150#endif /* ifndef Skein_Swap64 */151152153#ifndef Skein_Put64_LSB_First154void Skein_Put64_LSB_First(u08b_t *dst,const u64b_t *src,size_t bCnt)155#ifdef SKEIN_PORT_CODE /* instantiate the function code here? */156{ /* this version is fully portable (big-endian or little-endian), but slow */157size_t n;158159for (n=0;n<bCnt;n++)160dst[n] = (u08b_t) (src[n>>3] >> (8*(n&7)));161}162#else163; /* output only the function prototype */164#endif165#endif /* ifndef Skein_Put64_LSB_First */166167168#ifndef Skein_Get64_LSB_First169void Skein_Get64_LSB_First(u64b_t *dst,const u08b_t *src,size_t wCnt)170#ifdef SKEIN_PORT_CODE /* instantiate the function code here? */171{ /* this version is fully portable (big-endian or little-endian), but slow */172size_t n;173174for (n=0;n<8*wCnt;n+=8)175dst[n/8] = (((u64b_t) src[n ]) ) +176(((u64b_t) src[n+1]) << 8) +177(((u64b_t) src[n+2]) << 16) +178(((u64b_t) src[n+3]) << 24) +179(((u64b_t) src[n+4]) << 32) +180(((u64b_t) src[n+5]) << 40) +181(((u64b_t) src[n+6]) << 48) +182(((u64b_t) src[n+7]) << 56) ;183}184#else185; /* output only the function prototype */186#endif187#endif /* ifndef Skein_Get64_LSB_First */188189#endif /* ifndef _SKEIN_PORT_H_ */190191192