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