datatypes.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. /*
  2. * datatypes.h
  3. *
  4. * data types for bit vectors and finite fields
  5. *
  6. * David A. McGrew
  7. * Cisco Systems, Inc.
  8. */
  9. /*
  10. *
  11. * Copyright (c) 2001-2006, Cisco Systems, Inc.
  12. * All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or without
  15. * modification, are permitted provided that the following conditions
  16. * are met:
  17. *
  18. * Redistributions of source code must retain the above copyright
  19. * notice, this list of conditions and the following disclaimer.
  20. *
  21. * Redistributions in binary form must reproduce the above
  22. * copyright notice, this list of conditions and the following
  23. * disclaimer in the documentation and/or other materials provided
  24. * with the distribution.
  25. *
  26. * Neither the name of the Cisco Systems, Inc. nor the names of its
  27. * contributors may be used to endorse or promote products derived
  28. * from this software without specific prior written permission.
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  31. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  32. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  33. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  34. * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  35. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  36. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  37. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  38. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  41. * OF THE POSSIBILITY OF SUCH DAMAGE.
  42. *
  43. */
  44. #ifndef _DATATYPES_H
  45. #define _DATATYPES_H
  46. #include "integers.h" /* definitions of uint32_t, et cetera */
  47. #include "alloc.h"
  48. #include <stdarg.h>
  49. #ifndef SRTP_KERNEL
  50. # include <stdio.h>
  51. # include <string.h>
  52. # include <time.h>
  53. # ifdef HAVE_NETINET_IN_H
  54. # include <netinet/in.h>
  55. # elif defined HAVE_WINSOCK2_H
  56. # include <winsock2.h>
  57. # endif
  58. #endif
  59. /* if DATATYPES_USE_MACROS is defined, then little functions are macros */
  60. #define DATATYPES_USE_MACROS
  61. typedef union {
  62. uint8_t v8[2];
  63. uint16_t value;
  64. } v16_t;
  65. typedef union {
  66. uint8_t v8[4];
  67. uint16_t v16[2];
  68. uint32_t value;
  69. } v32_t;
  70. typedef union {
  71. uint8_t v8[8];
  72. uint16_t v16[4];
  73. uint32_t v32[2];
  74. uint64_t value;
  75. } v64_t;
  76. typedef union {
  77. uint8_t v8[16];
  78. uint16_t v16[8];
  79. uint32_t v32[4];
  80. uint64_t v64[2];
  81. } v128_t;
  82. typedef union {
  83. uint8_t v8[32];
  84. uint16_t v16[16];
  85. uint32_t v32[8];
  86. uint64_t v64[4];
  87. } v256_t;
  88. /* some useful and simple math functions */
  89. #define pow_2(X) ( (unsigned int)1 << (X) ) /* 2^X */
  90. #define pow_minus_one(X) ( (X) ? -1 : 1 ) /* (-1)^X */
  91. /*
  92. * octet_get_weight(x) returns the hamming weight (number of bits equal to
  93. * one) in the octet x
  94. */
  95. int
  96. octet_get_weight(uint8_t octet);
  97. char *
  98. octet_bit_string(uint8_t x);
  99. #define MAX_PRINT_STRING_LEN 1024
  100. char *
  101. octet_string_hex_string(const void *str, int length);
  102. char *
  103. v128_bit_string(v128_t *x);
  104. char *
  105. v128_hex_string(v128_t *x);
  106. uint8_t
  107. nibble_to_hex_char(uint8_t nibble);
  108. char *
  109. char_to_hex_string(char *x, int num_char);
  110. uint8_t
  111. hex_string_to_octet(char *s);
  112. /*
  113. * hex_string_to_octet_string(raw, hex, len) converts the hexadecimal
  114. * string at *hex (of length len octets) to the equivalent raw data
  115. * and writes it to *raw.
  116. *
  117. * if a character in the hex string that is not a hexadeciaml digit
  118. * (0123456789abcdefABCDEF) is encountered, the function stops writing
  119. * data to *raw
  120. *
  121. * the number of hex digits copied (which is two times the number of
  122. * octets in *raw) is returned
  123. */
  124. int
  125. hex_string_to_octet_string(char *raw, char *hex, int len);
  126. v128_t
  127. hex_string_to_v128(char *s);
  128. void
  129. v128_copy_octet_string(v128_t *x, const uint8_t s[16]);
  130. void
  131. v128_left_shift(v128_t *x, int shift_index);
  132. void
  133. v128_right_shift(v128_t *x, int shift_index);
  134. /*
  135. * the following macros define the data manipulation functions
  136. *
  137. * If DATATYPES_USE_MACROS is defined, then these macros are used
  138. * directly (and function call overhead is avoided). Otherwise,
  139. * the macros are used through the functions defined in datatypes.c
  140. * (and the compiler provides better warnings).
  141. */
  142. #define _v128_set_to_zero(x) \
  143. ( \
  144. (x)->v32[0] = 0, \
  145. (x)->v32[1] = 0, \
  146. (x)->v32[2] = 0, \
  147. (x)->v32[3] = 0 \
  148. )
  149. #define _v128_copy(x, y) \
  150. ( \
  151. (x)->v32[0] = (y)->v32[0], \
  152. (x)->v32[1] = (y)->v32[1], \
  153. (x)->v32[2] = (y)->v32[2], \
  154. (x)->v32[3] = (y)->v32[3] \
  155. )
  156. #define _v128_xor(z, x, y) \
  157. ( \
  158. (z)->v32[0] = (x)->v32[0] ^ (y)->v32[0], \
  159. (z)->v32[1] = (x)->v32[1] ^ (y)->v32[1], \
  160. (z)->v32[2] = (x)->v32[2] ^ (y)->v32[2], \
  161. (z)->v32[3] = (x)->v32[3] ^ (y)->v32[3] \
  162. )
  163. #define _v128_and(z, x, y) \
  164. ( \
  165. (z)->v32[0] = (x)->v32[0] & (y)->v32[0], \
  166. (z)->v32[1] = (x)->v32[1] & (y)->v32[1], \
  167. (z)->v32[2] = (x)->v32[2] & (y)->v32[2], \
  168. (z)->v32[3] = (x)->v32[3] & (y)->v32[3] \
  169. )
  170. #define _v128_or(z, x, y) \
  171. ( \
  172. (z)->v32[0] = (x)->v32[0] | (y)->v32[0], \
  173. (z)->v32[1] = (x)->v32[1] | (y)->v32[1], \
  174. (z)->v32[2] = (x)->v32[2] | (y)->v32[2], \
  175. (z)->v32[3] = (x)->v32[3] | (y)->v32[3] \
  176. )
  177. #define _v128_complement(x) \
  178. ( \
  179. (x)->v32[0] = ~(x)->v32[0], \
  180. (x)->v32[1] = ~(x)->v32[1], \
  181. (x)->v32[2] = ~(x)->v32[2], \
  182. (x)->v32[3] = ~(x)->v32[3] \
  183. )
  184. /* ok for NO_64BIT_MATH if it can compare uint64_t's (even as structures) */
  185. #define _v128_is_eq(x, y) \
  186. (((x)->v64[0] == (y)->v64[0]) && ((x)->v64[1] == (y)->v64[1]))
  187. #ifdef NO_64BIT_MATH
  188. #define _v128_xor_eq(z, x) \
  189. ( \
  190. (z)->v32[0] ^= (x)->v32[0], \
  191. (z)->v32[1] ^= (x)->v32[1], \
  192. (z)->v32[2] ^= (x)->v32[2], \
  193. (z)->v32[3] ^= (x)->v32[3] \
  194. )
  195. #else
  196. #define _v128_xor_eq(z, x) \
  197. ( \
  198. (z)->v64[0] ^= (x)->v64[0], \
  199. (z)->v64[1] ^= (x)->v64[1] \
  200. )
  201. #endif
  202. /* NOTE! This assumes an odd ordering! */
  203. /* This will not be compatible directly with math on some processors */
  204. /* bit 0 is first 32-bit word, low order bit. in little-endian, that's
  205. the first byte of the first 32-bit word. In big-endian, that's
  206. the 3rd byte of the first 32-bit word */
  207. /* The get/set bit code is used by the replay code ONLY, and it doesn't
  208. really care which bit is which. AES does care which bit is which, but
  209. doesn't use the 128-bit get/set or 128-bit shifts */
  210. #define _v128_get_bit(x, bit) \
  211. ( \
  212. ((((x)->v32[(bit) >> 5]) >> ((bit) & 31)) & 1) \
  213. )
  214. #define _v128_set_bit(x, bit) \
  215. ( \
  216. (((x)->v32[(bit) >> 5]) |= ((uint32_t)1 << ((bit) & 31))) \
  217. )
  218. #define _v128_clear_bit(x, bit) \
  219. ( \
  220. (((x)->v32[(bit) >> 5]) &= ~((uint32_t)1 << ((bit) & 31))) \
  221. )
  222. #define _v128_set_bit_to(x, bit, value) \
  223. ( \
  224. (value) ? _v128_set_bit(x, bit) : \
  225. _v128_clear_bit(x, bit) \
  226. )
  227. #if 0
  228. /* nothing uses this */
  229. #ifdef WORDS_BIGENDIAN
  230. #define _v128_add(z, x, y) { \
  231. uint64_t tmp; \
  232. \
  233. tmp = x->v32[3] + y->v32[3]; \
  234. z->v32[3] = (uint32_t) tmp; \
  235. \
  236. tmp = x->v32[2] + y->v32[2] + (tmp >> 32); \
  237. z->v32[2] = (uint32_t) tmp; \
  238. \
  239. tmp = x->v32[1] + y->v32[1] + (tmp >> 32); \
  240. z->v32[1] = (uint32_t) tmp; \
  241. \
  242. tmp = x->v32[0] + y->v32[0] + (tmp >> 32); \
  243. z->v32[0] = (uint32_t) tmp; \
  244. }
  245. #else /* assume little endian architecture */
  246. #define _v128_add(z, x, y) { \
  247. uint64_t tmp; \
  248. \
  249. tmp = htonl(x->v32[3]) + htonl(y->v32[3]); \
  250. z->v32[3] = ntohl((uint32_t) tmp); \
  251. \
  252. tmp = htonl(x->v32[2]) + htonl(y->v32[2]) \
  253. + htonl(tmp >> 32); \
  254. z->v32[2] = ntohl((uint32_t) tmp); \
  255. \
  256. tmp = htonl(x->v32[1]) + htonl(y->v32[1]) \
  257. + htonl(tmp >> 32); \
  258. z->v32[1] = ntohl((uint32_t) tmp); \
  259. \
  260. tmp = htonl(x->v32[0]) + htonl(y->v32[0]) \
  261. + htonl(tmp >> 32); \
  262. z->v32[0] = ntohl((uint32_t) tmp); \
  263. }
  264. #endif /* WORDS_BIGENDIAN */
  265. #endif /* 0 */
  266. #ifdef DATATYPES_USE_MACROS /* little functions are really macros */
  267. #define v128_set_to_zero(z) _v128_set_to_zero(z)
  268. #define v128_copy(z, x) _v128_copy(z, x)
  269. #define v128_xor(z, x, y) _v128_xor(z, x, y)
  270. #define v128_and(z, x, y) _v128_and(z, x, y)
  271. #define v128_or(z, x, y) _v128_or(z, x, y)
  272. #define v128_complement(x) _v128_complement(x)
  273. #define v128_is_eq(x, y) _v128_is_eq(x, y)
  274. #define v128_xor_eq(x, y) _v128_xor_eq(x, y)
  275. #define v128_get_bit(x, i) _v128_get_bit(x, i)
  276. #define v128_set_bit(x, i) _v128_set_bit(x, i)
  277. #define v128_clear_bit(x, i) _v128_clear_bit(x, i)
  278. #define v128_set_bit_to(x, i, y) _v128_set_bit_to(x, i, y)
  279. #else
  280. void
  281. v128_set_to_zero(v128_t *x);
  282. int
  283. v128_is_eq(const v128_t *x, const v128_t *y);
  284. void
  285. v128_copy(v128_t *x, const v128_t *y);
  286. void
  287. v128_xor(v128_t *z, v128_t *x, v128_t *y);
  288. void
  289. v128_and(v128_t *z, v128_t *x, v128_t *y);
  290. void
  291. v128_or(v128_t *z, v128_t *x, v128_t *y);
  292. void
  293. v128_complement(v128_t *x);
  294. int
  295. v128_get_bit(const v128_t *x, int i);
  296. void
  297. v128_set_bit(v128_t *x, int i) ;
  298. void
  299. v128_clear_bit(v128_t *x, int i);
  300. void
  301. v128_set_bit_to(v128_t *x, int i, int y);
  302. #endif /* DATATYPES_USE_MACROS */
  303. /*
  304. * octet_string_is_eq(a,b, len) returns 1 if the length len strings a
  305. * and b are not equal, returns 0 otherwise
  306. */
  307. int
  308. octet_string_is_eq(uint8_t *a, uint8_t *b, int len);
  309. void
  310. octet_string_set_to_zero(uint8_t *s, int len);
  311. #if !defined(SRTP_KERNEL_LINUX) && defined(HAVE_CONFIG_H)
  312. /*
  313. * Convert big endian integers to CPU byte order.
  314. */
  315. #ifdef WORDS_BIGENDIAN
  316. /* Nothing to do. */
  317. # define be32_to_cpu(x) (x)
  318. # define be64_to_cpu(x) (x)
  319. #elif defined(HAVE_BYTESWAP_H)
  320. /* We have (hopefully) optimized versions in byteswap.h */
  321. # include <byteswap.h>
  322. # define be32_to_cpu(x) bswap_32((x))
  323. # define be64_to_cpu(x) bswap_64((x))
  324. #else
  325. #if defined(__GNUC__) && defined(HAVE_X86)
  326. /* Fall back. */
  327. static inline uint32_t be32_to_cpu(uint32_t v)
  328. {
  329. /* optimized for x86. */
  330. asm("bswap %0" : "=r" (v) : "0" (v));
  331. return v;
  332. }
  333. # else /* HAVE_X86 */
  334. # ifdef HAVE_NETINET_IN_H
  335. # include <netinet/in.h>
  336. # elif defined HAVE_WINSOCK2_H
  337. # include <winsock2.h>
  338. # endif
  339. # define be32_to_cpu(x) ntohl((x))
  340. # endif /* HAVE_X86 */
  341. static inline uint64_t be64_to_cpu(uint64_t v)
  342. {
  343. # ifdef NO_64BIT_MATH
  344. /* use the make64 functions to do 64-bit math */
  345. v = make64(htonl(low32(v)),htonl(high32(v)));
  346. # else
  347. /* use the native 64-bit math */
  348. v= (uint64_t)((be32_to_cpu((uint32_t)(v >> 32))) | (((uint64_t)be32_to_cpu((uint32_t)v)) << 32));
  349. # endif
  350. return v;
  351. }
  352. #endif /* ! SRTP_KERNEL_LINUX */
  353. #endif /* WORDS_BIGENDIAN */
  354. /*
  355. * functions manipulating bitvector_t
  356. *
  357. * A bitvector_t consists of an array of words and an integer
  358. * representing the number of significant bits stored in the array.
  359. * The bits are packed as follows: the least significant bit is that
  360. * of word[0], while the most significant bit is the nth most
  361. * significant bit of word[m], where length = bits_per_word * m + n.
  362. *
  363. */
  364. #define bits_per_word 32
  365. #define bytes_per_word 4
  366. typedef struct {
  367. uint32_t length;
  368. uint32_t *word;
  369. } bitvector_t;
  370. #define _bitvector_get_bit(v, bit_index) \
  371. ( \
  372. ((((v)->word[((bit_index) >> 5)]) >> ((bit_index) & 31)) & 1) \
  373. )
  374. #define _bitvector_set_bit(v, bit_index) \
  375. ( \
  376. (((v)->word[((bit_index) >> 5)] |= ((uint32_t)1 << ((bit_index) & 31)))) \
  377. )
  378. #define _bitvector_clear_bit(v, bit_index) \
  379. ( \
  380. (((v)->word[((bit_index) >> 5)] &= ~((uint32_t)1 << ((bit_index) & 31)))) \
  381. )
  382. #define _bitvector_get_length(v) \
  383. ( \
  384. ((v)->length) \
  385. )
  386. #ifdef DATATYPES_USE_MACROS /* little functions are really macros */
  387. #define bitvector_get_bit(v, bit_index) _bitvector_get_bit(v, bit_index)
  388. #define bitvector_set_bit(v, bit_index) _bitvector_set_bit(v, bit_index)
  389. #define bitvector_clear_bit(v, bit_index) _bitvector_clear_bit(v, bit_index)
  390. #define bitvector_get_length(v) _bitvector_get_length(v)
  391. #else
  392. int
  393. bitvector_get_bit(const bitvector_t *v, int bit_index);
  394. void
  395. bitvector_set_bit(bitvector_t *v, int bit_index);
  396. void
  397. bitvector_clear_bit(bitvector_t *v, int bit_index);
  398. unsigned long
  399. bitvector_get_length(const bitvector_t *v);
  400. #endif
  401. int
  402. bitvector_alloc(bitvector_t *v, unsigned long length);
  403. void
  404. bitvector_dealloc(bitvector_t *v);
  405. void
  406. bitvector_set_to_zero(bitvector_t *x);
  407. void
  408. bitvector_left_shift(bitvector_t *x, int index);
  409. char *
  410. bitvector_bit_string(bitvector_t *x, char* buf, int len);
  411. #ifdef TESTAPP_SOURCE
  412. int base64_string_to_octet_string(char *raw, int *pad, char *base64, int len);
  413. #endif
  414. #endif /* _DATATYPES_H */