zrtp_base.h 6.1 KB

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