ccRTP 2.1.2
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
skein_port.h
Go to the documentation of this file.
1 #ifndef _SKEIN_PORT_H_
2 #define _SKEIN_PORT_H_
3 /*******************************************************************
4 **
5 ** Platform-specific definitions for Skein hash function.
6 **
7 ** Source code author: Doug Whiting, 2008.
8 **
9 ** This algorithm and source code is released to the public domain.
10 **
11 ** Many thanks to Brian Gladman for his portable header files.
12 **
13 ** To port Skein to an "unsupported" platform, change the definitions
14 ** in this file appropriately.
15 **
16 ********************************************************************/
17 
18 #include <crypto/brg_types.h> /* get integer type definitions */
19 
20 /*r3gis3r : android already has that defined in types */
21 #ifndef ANDROID
22 typedef unsigned int uint_t; /* native unsigned integer */
23 #endif
24 typedef uint_8t u08b_t; /* 8-bit unsigned integer */
25 typedef uint_64t u64b_t; /* 64-bit unsigned integer */
26 
27 #ifndef RotL_64
28 #define RotL_64(x,N) (((x) << (N)) | ((x) >> (64-(N))))
29 #endif
30 
31 /*
32  * Skein is "natively" little-endian (unlike SHA-xxx), for optimal
33  * performance on x86 CPUs. The Skein code requires the following
34  * definitions for dealing with endianness:
35  *
36  * SKEIN_NEED_SWAP: 0 for little-endian, 1 for big-endian
37  * Skein_Put64_LSB_First
38  * Skein_Get64_LSB_First
39  * Skein_Swap64
40  *
41  * If SKEIN_NEED_SWAP is defined at compile time, it is used here
42  * along with the portable versions of Put64/Get64/Swap64, which
43  * are slow in general.
44  *
45  * Otherwise, an "auto-detect" of endianness is attempted below.
46  * If the default handling doesn't work well, the user may insert
47  * platform-specific code instead (e.g., for big-endian CPUs).
48  *
49  */
50 #ifndef SKEIN_NEED_SWAP /* compile-time "override" for endianness? */
51 
52 #include <crypto/brg_endian.h> /* get endianness selection */
53 #if PLATFORM_BYTE_ORDER == IS_BIG_ENDIAN
54  /* here for big-endian CPUs */
55 #define SKEIN_NEED_SWAP (1)
56 #elif PLATFORM_BYTE_ORDER == IS_LITTLE_ENDIAN
57  /* here for x86 and x86-64 CPUs (and other detected little-endian CPUs) */
58 #define SKEIN_NEED_SWAP (0)
59 #if PLATFORM_MUST_ALIGN == 0 /* ok to use "fast" versions? */
60 #define Skein_Put64_LSB_First(dst08,src64,bCnt) memcpy(dst08,src64,bCnt)
61 #define Skein_Get64_LSB_First(dst64,src08,wCnt) memcpy(dst64,src08,8*(wCnt))
62 #endif
63 #else
64 #error "Skein needs endianness setting!"
65 #endif
66 
67 #endif /* ifndef SKEIN_NEED_SWAP */
68 
69 /*
70  ******************************************************************
71  * Provide any definitions still needed.
72  ******************************************************************
73  */
74 #ifndef Skein_Swap64 /* swap for big-endian, nop for little-endian */
75 #if SKEIN_NEED_SWAP
76 #define Skein_Swap64(w64) \
77  ( (( ((u64b_t)(w64)) & 0xFF) << 56) | \
78  (((((u64b_t)(w64)) >> 8) & 0xFF) << 48) | \
79  (((((u64b_t)(w64)) >>16) & 0xFF) << 40) | \
80  (((((u64b_t)(w64)) >>24) & 0xFF) << 32) | \
81  (((((u64b_t)(w64)) >>32) & 0xFF) << 24) | \
82  (((((u64b_t)(w64)) >>40) & 0xFF) << 16) | \
83  (((((u64b_t)(w64)) >>48) & 0xFF) << 8) | \
84  (((((u64b_t)(w64)) >>56) & 0xFF) ) )
85 #else
86 #define Skein_Swap64(w64) (w64)
87 #endif
88 #endif /* ifndef Skein_Swap64 */
89 
90 
91 #ifndef Skein_Put64_LSB_First
92 void Skein_Put64_LSB_First(u08b_t *dst,const u64b_t *src,size_t bCnt)
93 #ifdef SKEIN_PORT_CODE /* instantiate the function code here? */
94  { /* this version is fully portable (big-endian or little-endian), but slow */
95  size_t n;
96 
97  for (n=0;n<bCnt;n++)
98  dst[n] = (u08b_t) (src[n>>3] >> (8*(n&7)));
99  }
100 #else
101  ; /* output only the function prototype */
102 #endif
103 #endif /* ifndef Skein_Put64_LSB_First */
104 
105 
106 #ifndef Skein_Get64_LSB_First
107 void Skein_Get64_LSB_First(u64b_t *dst,const u08b_t *src,size_t wCnt)
108 #ifdef SKEIN_PORT_CODE /* instantiate the function code here? */
109  { /* this version is fully portable (big-endian or little-endian), but slow */
110  size_t n;
111 
112  for (n=0;n<8*wCnt;n+=8)
113  dst[n/8] = (((u64b_t) src[n ]) ) +
114  (((u64b_t) src[n+1]) << 8) +
115  (((u64b_t) src[n+2]) << 16) +
116  (((u64b_t) src[n+3]) << 24) +
117  (((u64b_t) src[n+4]) << 32) +
118  (((u64b_t) src[n+5]) << 40) +
119  (((u64b_t) src[n+6]) << 48) +
120  (((u64b_t) src[n+7]) << 56) ;
121  }
122 #else
123  ; /* output only the function prototype */
124 #endif
125 #endif /* ifndef Skein_Get64_LSB_First */
126 
127 #endif /* ifndef _SKEIN_PORT_H_ */
unsigned int uint_t
Definition: skein_port.h:22
uint_64t u64b_t
Definition: skein_port.h:25
void Skein_Put64_LSB_First(u08b_t *dst, const u64b_t *src, size_t bCnt)
uint_8t u08b_t
Definition: skein_port.h:24
void Skein_Get64_LSB_First(u64b_t *dst, const u08b_t *src, size_t wCnt)