blob: e62069ef02120d6ca626c4088636ca870c944ea8 [file] [log] [blame]
Emeric Vigier2f625822012-08-06 11:09:52 -04001#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 <ccrtp/crypto/brg_types.h> /* get integer type definitions */
19
20typedef unsigned int uint_t; /* native unsigned integer */
21typedef uint_8t u08b_t; /* 8-bit unsigned integer */
22typedef uint_64t u64b_t; /* 64-bit unsigned integer */
23
24#ifndef RotL_64
25#define RotL_64(x,N) (((x) << (N)) | ((x) >> (64-(N))))
26#endif
27
28/*
29 * Skein is "natively" little-endian (unlike SHA-xxx), for optimal
30 * performance on x86 CPUs. The Skein code requires the following
31 * definitions for dealing with endianness:
32 *
33 * SKEIN_NEED_SWAP: 0 for little-endian, 1 for big-endian
34 * Skein_Put64_LSB_First
35 * Skein_Get64_LSB_First
36 * Skein_Swap64
37 *
38 * If SKEIN_NEED_SWAP is defined at compile time, it is used here
39 * along with the portable versions of Put64/Get64/Swap64, which
40 * are slow in general.
41 *
42 * Otherwise, an "auto-detect" of endianness is attempted below.
43 * If the default handling doesn't work well, the user may insert
44 * platform-specific code instead (e.g., for big-endian CPUs).
45 *
46 */
47#ifndef SKEIN_NEED_SWAP /* compile-time "override" for endianness? */
48
49#include <ccrtp/crypto/brg_endian.h> /* get endianness selection */
50#if PLATFORM_BYTE_ORDER == IS_BIG_ENDIAN
51 /* here for big-endian CPUs */
52#define SKEIN_NEED_SWAP (1)
53#elif PLATFORM_BYTE_ORDER == IS_LITTLE_ENDIAN
54 /* here for x86 and x86-64 CPUs (and other detected little-endian CPUs) */
55#define SKEIN_NEED_SWAP (0)
56#if PLATFORM_MUST_ALIGN == 0 /* ok to use "fast" versions? */
57#define Skein_Put64_LSB_First(dst08,src64,bCnt) memcpy(dst08,src64,bCnt)
58#define Skein_Get64_LSB_First(dst64,src08,wCnt) memcpy(dst64,src08,8*(wCnt))
59#endif
60#else
61#error "Skein needs endianness setting!"
62#endif
63
64#endif /* ifndef SKEIN_NEED_SWAP */
65
66/*
67 ******************************************************************
68 * Provide any definitions still needed.
69 ******************************************************************
70 */
71#ifndef Skein_Swap64 /* swap for big-endian, nop for little-endian */
72#if SKEIN_NEED_SWAP
73#define Skein_Swap64(w64) \
74 ( (( ((u64b_t)(w64)) & 0xFF) << 56) | \
75 (((((u64b_t)(w64)) >> 8) & 0xFF) << 48) | \
76 (((((u64b_t)(w64)) >>16) & 0xFF) << 40) | \
77 (((((u64b_t)(w64)) >>24) & 0xFF) << 32) | \
78 (((((u64b_t)(w64)) >>32) & 0xFF) << 24) | \
79 (((((u64b_t)(w64)) >>40) & 0xFF) << 16) | \
80 (((((u64b_t)(w64)) >>48) & 0xFF) << 8) | \
81 (((((u64b_t)(w64)) >>56) & 0xFF) ) )
82#else
83#define Skein_Swap64(w64) (w64)
84#endif
85#endif /* ifndef Skein_Swap64 */
86
87
88#ifndef Skein_Put64_LSB_First
89void Skein_Put64_LSB_First(u08b_t *dst,const u64b_t *src,size_t bCnt)
90#ifdef SKEIN_PORT_CODE /* instantiate the function code here? */
91 { /* this version is fully portable (big-endian or little-endian), but slow */
92 size_t n;
93
94 for (n=0;n<bCnt;n++)
95 dst[n] = (u08b_t) (src[n>>3] >> (8*(n&7)));
96 }
97#else
98 ; /* output only the function prototype */
99#endif
100#endif /* ifndef Skein_Put64_LSB_First */
101
102
103#ifndef Skein_Get64_LSB_First
104void Skein_Get64_LSB_First(u64b_t *dst,const u08b_t *src,size_t wCnt)
105#ifdef SKEIN_PORT_CODE /* instantiate the function code here? */
106 { /* this version is fully portable (big-endian or little-endian), but slow */
107 size_t n;
108
109 for (n=0;n<8*wCnt;n+=8)
110 dst[n/8] = (((u64b_t) src[n ]) ) +
111 (((u64b_t) src[n+1]) << 8) +
112 (((u64b_t) src[n+2]) << 16) +
113 (((u64b_t) src[n+3]) << 24) +
114 (((u64b_t) src[n+4]) << 32) +
115 (((u64b_t) src[n+5]) << 40) +
116 (((u64b_t) src[n+6]) << 48) +
117 (((u64b_t) src[n+7]) << 56) ;
118 }
119#else
120 ; /* output only the function prototype */
121#endif
122#endif /* ifndef Skein_Get64_LSB_First */
123
124#endif /* ifndef _SKEIN_PORT_H_ */