4
* data types for bit vectors and finite fields
12
* Copyright (c) 2001-2006, Cisco Systems, Inc.
13
* All rights reserved.
15
* Redistribution and use in source and binary forms, with or without
16
* modification, are permitted provided that the following conditions
19
* Redistributions of source code must retain the above copyright
20
* notice, this list of conditions and the following disclaimer.
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.
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.
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.
50
#include "integers.h" /* definitions of uint32_t, et cetera */
59
# ifdef HAVE_NETINET_IN_H
60
# include <netinet/in.h>
61
# elif defined HAVE_WINSOCK2_H
62
# include <winsock2.h>
67
/* if DATATYPES_USE_MACROS is defined, then little functions are macros */
68
#define DATATYPES_USE_MACROS
97
/* some useful and simple math functions */
99
#define pow_2(X) ( (unsigned int)1 << (X) ) /* 2^X */
101
#define pow_minus_one(X) ( (X) ? -1 : 1 ) /* (-1)^X */
105
* octet_get_weight(x) returns the hamming weight (number of bits equal to
106
* one) in the octet x
110
octet_get_weight(uint8_t octet);
113
octet_bit_string(uint8_t x);
115
#define MAX_PRINT_STRING_LEN 1024
118
octet_string_hex_string(const void *str, int length);
121
v128_bit_string(v128_t *x);
124
v128_hex_string(v128_t *x);
127
nibble_to_hex_char(uint8_t nibble);
130
char_to_hex_string(char *x, int num_char);
133
hex_string_to_octet(char *s);
136
* hex_string_to_octet_string(raw, hex, len) converts the hexadecimal
137
* string at *hex (of length len octets) to the equivalent raw data
138
* and writes it to *raw.
140
* if a character in the hex string that is not a hexadeciaml digit
141
* (0123456789abcdefABCDEF) is encountered, the function stops writing
144
* the number of hex digits copied (which is two times the number of
145
* octets in *raw) is returned
149
hex_string_to_octet_string(char *raw, char *hex, int len);
152
hex_string_to_v128(char *s);
155
v128_copy_octet_string(v128_t *x, const uint8_t s[16]);
158
v128_left_shift(v128_t *x, int index);
161
v128_right_shift(v128_t *x, int index);
164
* the following macros define the data manipulation functions
166
* If DATATYPES_USE_MACROS is defined, then these macros are used
167
* directly (and function call overhead is avoided). Otherwise,
168
* the macros are used through the functions defined in datatypes.c
169
* (and the compiler provides better warnings).
172
#define _v128_set_to_zero(x) \
180
#define _v128_copy(x, y) \
182
(x)->v32[0] = (y)->v32[0], \
183
(x)->v32[1] = (y)->v32[1], \
184
(x)->v32[2] = (y)->v32[2], \
185
(x)->v32[3] = (y)->v32[3] \
188
#define _v128_xor(z, x, y) \
190
(z)->v32[0] = (x)->v32[0] ^ (y)->v32[0], \
191
(z)->v32[1] = (x)->v32[1] ^ (y)->v32[1], \
192
(z)->v32[2] = (x)->v32[2] ^ (y)->v32[2], \
193
(z)->v32[3] = (x)->v32[3] ^ (y)->v32[3] \
196
#define _v128_and(z, x, y) \
198
(z)->v32[0] = (x)->v32[0] & (y)->v32[0], \
199
(z)->v32[1] = (x)->v32[1] & (y)->v32[1], \
200
(z)->v32[2] = (x)->v32[2] & (y)->v32[2], \
201
(z)->v32[3] = (x)->v32[3] & (y)->v32[3] \
204
#define _v128_or(z, x, y) \
206
(z)->v32[0] = (x)->v32[0] | (y)->v32[0], \
207
(z)->v32[1] = (x)->v32[1] | (y)->v32[1], \
208
(z)->v32[2] = (x)->v32[2] | (y)->v32[2], \
209
(z)->v32[3] = (x)->v32[3] | (y)->v32[3] \
212
#define _v128_complement(x) \
214
(x)->v32[0] = ~(x)->v32[0], \
215
(x)->v32[1] = ~(x)->v32[1], \
216
(x)->v32[2] = ~(x)->v32[2], \
217
(x)->v32[3] = ~(x)->v32[3] \
220
/* ok for NO_64BIT_MATH if it can compare uint64_t's (even as structures) */
221
#define _v128_is_eq(x, y) \
222
(((x)->v64[0] == (y)->v64[0]) && ((x)->v64[1] == (y)->v64[1]))
226
#define _v128_xor_eq(z, x) \
228
(z)->v32[0] ^= (x)->v32[0], \
229
(z)->v32[1] ^= (x)->v32[1], \
230
(z)->v32[2] ^= (x)->v32[2], \
231
(z)->v32[3] ^= (x)->v32[3] \
234
#define _v128_xor_eq(z, x) \
236
(z)->v64[0] ^= (x)->v64[0], \
237
(z)->v64[1] ^= (x)->v64[1] \
241
/* NOTE! This assumes an odd ordering! */
242
/* This will not be compatible directly with math on some processors */
243
/* bit 0 is first 32-bit word, low order bit. in little-endian, that's
244
the first byte of the first 32-bit word. In big-endian, that's
245
the 3rd byte of the first 32-bit word */
246
/* The get/set bit code is used by the replay code ONLY, and it doesn't
247
really care which bit is which. AES does care which bit is which, but
248
doesn't use the 128-bit get/set or 128-bit shifts */
250
#define _v128_get_bit(x, bit) \
252
((((x)->v32[(bit) >> 5]) >> ((bit) & 31)) & 1) \
255
#define _v128_set_bit(x, bit) \
257
(((x)->v32[(bit) >> 5]) |= ((uint32_t)1 << ((bit) & 31))) \
260
#define _v128_clear_bit(x, bit) \
262
(((x)->v32[(bit) >> 5]) &= ~((uint32_t)1 << ((bit) & 31))) \
265
#define _v128_set_bit_to(x, bit, value) \
267
(value) ? _v128_set_bit(x, bit) : \
268
_v128_clear_bit(x, bit) \
273
/* nothing uses this */
274
#ifdef WORDS_BIGENDIAN
276
#define _v128_add(z, x, y) { \
279
tmp = x->v32[3] + y->v32[3]; \
280
z->v32[3] = (uint32_t) tmp; \
282
tmp = x->v32[2] + y->v32[2] + (tmp >> 32); \
283
z->v32[2] = (uint32_t) tmp; \
285
tmp = x->v32[1] + y->v32[1] + (tmp >> 32); \
286
z->v32[1] = (uint32_t) tmp; \
288
tmp = x->v32[0] + y->v32[0] + (tmp >> 32); \
289
z->v32[0] = (uint32_t) tmp; \
292
#else /* assume little endian architecture */
294
#define _v128_add(z, x, y) { \
297
tmp = htonl(x->v32[3]) + htonl(y->v32[3]); \
298
z->v32[3] = ntohl((uint32_t) tmp); \
300
tmp = htonl(x->v32[2]) + htonl(y->v32[2]) \
301
+ htonl(tmp >> 32); \
302
z->v32[2] = ntohl((uint32_t) tmp); \
304
tmp = htonl(x->v32[1]) + htonl(y->v32[1]) \
305
+ htonl(tmp >> 32); \
306
z->v32[1] = ntohl((uint32_t) tmp); \
308
tmp = htonl(x->v32[0]) + htonl(y->v32[0]) \
309
+ htonl(tmp >> 32); \
310
z->v32[0] = ntohl((uint32_t) tmp); \
312
#endif /* WORDS_BIGENDIAN */
316
#ifdef DATATYPES_USE_MACROS /* little functions are really macros */
318
#define v128_set_to_zero(z) _v128_set_to_zero(z)
319
#define v128_copy(z, x) _v128_copy(z, x)
320
#define v128_xor(z, x, y) _v128_xor(z, x, y)
321
#define v128_and(z, x, y) _v128_and(z, x, y)
322
#define v128_or(z, x, y) _v128_or(z, x, y)
323
#define v128_complement(x) _v128_complement(x)
324
#define v128_is_eq(x, y) _v128_is_eq(x, y)
325
#define v128_xor_eq(x, y) _v128_xor_eq(x, y)
326
#define v128_get_bit(x, i) _v128_get_bit(x, i)
327
#define v128_set_bit(x, i) _v128_set_bit(x, i)
328
#define v128_clear_bit(x, i) _v128_clear_bit(x, i)
329
#define v128_set_bit_to(x, i, y) _v128_set_bit_to(x, i, y)
334
v128_set_to_zero(v128_t *x);
337
v128_is_eq(const v128_t *x, const v128_t *y);
340
v128_copy(v128_t *x, const v128_t *y);
343
v128_xor(v128_t *z, v128_t *x, v128_t *y);
346
v128_and(v128_t *z, v128_t *x, v128_t *y);
349
v128_or(v128_t *z, v128_t *x, v128_t *y);
352
v128_complement(v128_t *x);
355
v128_get_bit(const v128_t *x, int i);
358
v128_set_bit(v128_t *x, int i) ;
361
v128_clear_bit(v128_t *x, int i);
364
v128_set_bit_to(v128_t *x, int i, int y);
366
#endif /* DATATYPES_USE_MACROS */
369
* octet_string_is_eq(a,b, len) returns 1 if the length len strings a
370
* and b are not equal, returns 0 otherwise
374
octet_string_is_eq(uint8_t *a, uint8_t *b, int len);
377
octet_string_set_to_zero(uint8_t *s, int len);
380
#ifndef SRTP_KERNEL_LINUX
383
* Convert big endian integers to CPU byte order.
385
#ifdef WORDS_BIGENDIAN
387
# define be32_to_cpu(x) (x)
388
# define be64_to_cpu(x) (x)
389
#elif defined(HAVE_BYTESWAP_H)
390
/* We have (hopefully) optimized versions in byteswap.h */
391
# include <byteswap.h>
392
# define be32_to_cpu(x) bswap_32((x))
393
# define be64_to_cpu(x) bswap_64((x))
396
#if defined(__GNUC__) && defined(HAVE_X86)
398
static inline uint32_t be32_to_cpu(uint32_t v) {
399
/* optimized for x86. */
400
asm("bswap %0" : "=r" (v) : "0" (v));
403
# else /* HAVE_X86 */
404
# ifdef HAVE_NETINET_IN_H
405
# include <netinet/in.h>
406
# elif defined HAVE_WINSOCK2_H
407
# include <winsock2.h>
409
# define be32_to_cpu(x) ntohl((x))
410
# endif /* HAVE_X86 */
412
static inline uint64_t be64_to_cpu(uint64_t v) {
413
# ifdef NO_64BIT_MATH
414
/* use the make64 functions to do 64-bit math */
415
v = make64(htonl(low32(v)),htonl(high32(v)));
417
/* use the native 64-bit math */
418
v= (uint64_t)((be32_to_cpu((uint32_t)(v >> 32))) | (((uint64_t)be32_to_cpu((uint32_t)v)) << 32));
423
#endif /* ! SRTP_KERNEL_LINUX */
425
#endif /* WORDS_BIGENDIAN */
427
#endif /* _DATATYPES_H */