1 /*
2  * libZRTP SDK library, implements the ZRTP secure VoIP protocol.
3  * Copyright (c) 2006-2009 Philip R. Zimmermann.  All rights reserved.
4  * Contact: http://philzimmermann.com
5  * For licensing and other legal details, see the file zrtp_legal.c.
6  *
7  * Viktor Krykun <v.krikun at zfoneproject.com>
8  */
9 
10 #ifndef __ZRTP_BASE_H__
11 #define __ZRTP_BASE_H__
12 
13 #include "zrtp_config.h"
14 
15 typedef double uint64_t_;
16 
17 typedef uint8_t						zrtp_uchar4_t[4];
18 typedef uint8_t						zrtp_uchar8_t[8];
19 typedef uint8_t						zrtp_uchar12_t[12];
20 typedef uint8_t						zrtp_uchar16_t[16];
21 typedef uint8_t						zrtp_uchar32_t[32];
22 typedef uint8_t						zrtp_uchar64_t[64];
23 typedef uint8_t						zrtp_uchar128_t[128];
24 typedef uint8_t						zrtp_uchar256_t[256];
25 typedef uint8_t						zrtp_uchar1024_t[1024];
26 
27 typedef uint32_t					zrtp_id_t;
28 
29 typedef struct zrtp_profile_t		zrtp_profile_t;
30 typedef struct zrtp_stream_t		zrtp_stream_t;
31 typedef struct zrtp_session_t		zrtp_session_t;
32 typedef struct zrtp_global_t		zrtp_global_t;
33 
34 typedef struct zrtp_protocol_t		zrtp_protocol_t;
35 typedef struct zrtp_srtp_ctx_t		zrtp_srtp_ctx_t;
36 typedef struct zrtp_shared_secret_t	zrtp_shared_secret_t;
37 typedef struct zrtp_retry_task_t	zrtp_retry_task_t;
38 
39 typedef struct zrtp_hash_t			zrtp_hash_t;
40 typedef struct zrtp_cipher_t		zrtp_cipher_t;
41 typedef struct zrtp_auth_tag_length_t zrtp_auth_tag_length_t;
42 typedef struct zrtp_pk_scheme_t		zrtp_pk_scheme_t;
43 typedef struct zrtp_sas_scheme_t	zrtp_sas_scheme_t;
44 typedef struct zrtp_sig_scheme_t	zrtp_sig_scheme_t;
45 
46 typedef struct zrtp_mutex_t			zrtp_mutex_t;
47 typedef struct zrtp_sem_t			zrtp_sem_t;
48 
49 typedef struct zrtp_stream_info_t	zrtp_stream_info_t;
50 typedef struct zrtp_session_info_t	zrtp_session_info_t;
51 
52 #include "sha2.h"
53 #define MD_CTX						sha512_ctx
54 #define MD_Update(a,b,c)			sha512_hash((const unsigned char *)(b),c,a)
55 
56 
57 /**
58  * \brief Function computing minimum value
59  *
60  * This macro returns the lesser of two values. If the numbers are equal, either of them is returned.
61  *
62  * \param left - first value for comparison;
63  * \param right - second value for comparison.
64  * \return
65  *  - lesser of compared numbers.
66  */
67 #define ZRTP_MIN(left, right) ((left < right) ? left : right)
68 
69 
70 /*!
71  * \brief zrtp_htonXX,  zrtp_ntohXX - convert values between host and network
72  * byte order
73  *
74  * To avoid ambiguities and difficulties with compilation on various platforms,
75  * we designed our own swap functions. Byte order detection is based on zrtp_system.h.
76  *
77  * On the i80x86 the host byte order is little-endian (least significant byte
78  * first), whereas the network byte order, as used on the Internet, is
79  * big-endian (most significant byte first).
80  */
81 
82 uint16_t zrtp_swap16(uint16_t x);
83 uint32_t zrtp_swap32(uint32_t x);
84 uint64_t zrtp_swap64(uint64_t x);
85 
86 #if ZRTP_BYTE_ORDER == ZBO_BIG_ENDIAN
87 /*! Converts 16 bit unsigned integer to network byte order */
88 #define zrtp_hton16(x)    (x)
89 /*! Converts 32 bit unsigned integer to network byte order */
90 #define zrtp_hton32(x)    (x)
91 /*! Converts 64 bit unsigned integer to network byte order */
92 #define zrtp_hton64(x)    (x)
93 
94 /*! Converts 16 bit unsigned integer to host byte order */
95 #define zrtp_ntoh16(x)    (x)
96 /*! Converts 32 bit unsigned integer to host byte order */
97 #define zrtp_ntoh32(x)    (x)
98 /*! Converts 64 bit unsigned integer to host byte order */
99 #define zrtp_ntoh64(x)    (x)
100 #else /* ZBO_BIG_ENDIAN    */
101 /*! Converts 16 bit unsigned integer to network byte order */
102 #define zrtp_hton16(x)    (zrtp_swap16(x))
103 /*! Converts 32 bit unsigned integer to network byte order */
104 #define zrtp_hton32(x)    (zrtp_swap32(x))
105 /*! Converts 64 bit unsigned integer to network byte order */
106 #define zrtp_hton64(x)    (zrtp_swap64(x))
107 
108 /*! Converts 16 bit unsigned integer to host byte order */
109 #define zrtp_ntoh16(x)    (zrtp_swap16(x))
110 /*! Converts 32 bit unsigned integer to host byte order */
111 #define zrtp_ntoh32(x)    (zrtp_swap32(x))
112 /*! Converts 64 bit unsigned integer to host byte order */
113 #define zrtp_ntoh64(x)    (zrtp_swap64(x))
114 #endif
115 
116 
117 /*
118  * 128 and 256-bit structures used in Ciphers and SRTP module
119  */
120 typedef union
121 	{
122 		uint8_t  v8[16];
123 		uint16_t v16[8];
124 		uint32_t v32[4];
125 		uint64_t v64[2];
126 	} zrtp_v128_t;
127 
128 typedef union
129 	{
130 		uint8_t  v8[32];
131 		uint16_t v16[16];
132 		uint32_t v32[8];
133 		uint64_t v64[4];
134 	} zrtp_v256_t;
135 
136 /*
137  * The following macros define the data manipulation functions.
138  *
139  * If DATATYPES_USE_MACROS is defined, then these macros are used directly (and
140  * function-call overhead is avoided).  Otherwise, the macros are used through
141  * the functions defined in datatypes.c (and the compiler provides better
142  * warnings).
143  */
144 
145 #define _zrtp_v128_xor(z, x, y)                        \
146 (                                                      \
147 (z)->v32[0] = (x)->v32[0] ^ (y)->v32[0],               \
148 (z)->v32[1] = (x)->v32[1] ^ (y)->v32[1],               \
149 (z)->v32[2] = (x)->v32[2] ^ (y)->v32[2],               \
150 (z)->v32[3] = (x)->v32[3] ^ (y)->v32[3]                \
151 )
152 
153 #define _zrtp_v128_get_bit(x, bit)                     \
154 (                                                      \
155 ( (((x)->v32[(bit) >> 5]) >> ((bit) & 31)) & 1)        \
156 )
157 
158 #define zrtp_bitmap_get_bit(x, bit)                    \
159 (                                                      \
160 ( (((x)[(bit) >> 3]) >> ((bit) & 7) ) & 1)             \
161 )
162 
163 #define zrtp_bitmap_set_bit(x, bit)                     \
164 (                                                       \
165 ( (((x)[(bit) >> 3])) |= ((uint8_t)1 << ((bit) & 7)) )  \
166 )
167 
168 #define zrtp_bitmap_clear_bit(x, bit)                   \
169 (                                                       \
170 ( (((x)[(bit) >> 3])) &= ~((uint8_t)1 << ((bit) & 7)) ) \
171 )
172 
173 void zrtp_bitmap_left_shift(uint8_t *x, int width_bytes, int index);
174 
175 void zrtp_v128_xor(zrtp_v128_t *z, zrtp_v128_t *x, zrtp_v128_t *y);
176 
177 
178 
179 //WIN64 {
180 #if (ZRTP_PLATFORM == ZP_WIN32_KERNEL)
181 
182 #ifdef WIN64 // For 64-bit apps
183 
184 unsigned __int64 __rdtsc(void);
185 #pragma intrinsic(__rdtsc)
186 #define _RDTSC __rdtsc
187 
188 #else // For 32-bit apps
189 
190 #define _RDTSC_STACK(ts) \
191 __asm rdtsc \
192 __asm mov DWORD PTR [ts], eax \
193 __asm mov DWORD PTR [ts+4], edx
194 
_inl_rdtsc32()195 __inline unsigned __int64 _inl_rdtsc32() {
196 	unsigned __int64 t;
197 	_RDTSC_STACK(t);
198 	return t;
199 }
200 #define _RDTSC _inl_rdtsc32
201 
202 #endif
203 
204 #endif
205 //WIN64 }
206 
207 
208 #endif /*__ZRTP_BASE_H__*/
209