OLD | NEW |
1 /* | 1 /* |
2 * datatypes.h | 2 * datatypes.h |
3 * | 3 * |
4 * data types for bit vectors and finite fields | 4 * data types for bit vectors and finite fields |
5 * | 5 * |
6 * David A. McGrew | 6 * David A. McGrew |
7 * Cisco Systems, Inc. | 7 * Cisco Systems, Inc. |
8 */ | 8 */ |
9 | 9 |
10 /* | 10 /* |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
45 | 45 |
46 | 46 |
47 #ifndef _DATATYPES_H | 47 #ifndef _DATATYPES_H |
48 #define _DATATYPES_H | 48 #define _DATATYPES_H |
49 | 49 |
50 #include "integers.h" /* definitions of uint32_t, et cetera */ | 50 #include "integers.h" /* definitions of uint32_t, et cetera */ |
51 #include "alloc.h" | 51 #include "alloc.h" |
52 | 52 |
53 #include <stdarg.h> | 53 #include <stdarg.h> |
54 | 54 |
55 #ifndef SRTP_KERNEL | 55 #include <stdio.h> |
56 # include <stdio.h> | 56 #include <string.h> |
57 # include <string.h> | 57 #include <time.h> |
58 # include <time.h> | 58 #ifdef HAVE_NETINET_IN_H |
59 # ifdef HAVE_NETINET_IN_H | 59 # include <netinet/in.h> |
60 # include <netinet/in.h> | 60 #elif defined HAVE_WINSOCK2_H |
61 # elif defined HAVE_WINSOCK2_H | 61 # include <winsock2.h> |
62 # include <winsock2.h> | 62 #endif |
63 # elif defined HAVE_BYTESWAP_METHODS_H | 63 |
64 # include <stdlib.h> | 64 #ifdef __cplusplus |
65 # define ntohl(x) _byteswap_ulong (x) | 65 extern "C" { |
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 | 66 #endif |
71 | 67 |
72 | 68 |
73 /* if DATATYPES_USE_MACROS is defined, then little functions are macros */ | 69 /* if DATATYPES_USE_MACROS is defined, then little functions are macros */ |
74 #define DATATYPES_USE_MACROS | 70 #define DATATYPES_USE_MACROS |
75 | 71 |
76 typedef union { | 72 typedef union { |
77 uint8_t v8[2]; | 73 uint8_t v8[2]; |
78 uint16_t value; | 74 uint16_t value; |
79 } v16_t; | 75 } v16_t; |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
114 | 110 |
115 | 111 |
116 /* | 112 /* |
117 * octet_get_weight(x) returns the hamming weight (number of bits equal to | 113 * octet_get_weight(x) returns the hamming weight (number of bits equal to |
118 * one) in the octet x | 114 * one) in the octet x |
119 */ | 115 */ |
120 | 116 |
121 int | 117 int |
122 octet_get_weight(uint8_t octet); | 118 octet_get_weight(uint8_t octet); |
123 | 119 |
124 char * | |
125 octet_bit_string(uint8_t x); | |
126 | |
127 #define MAX_PRINT_STRING_LEN 1024 | 120 #define MAX_PRINT_STRING_LEN 1024 |
128 | 121 |
129 char * | 122 char * |
130 octet_string_hex_string(const void *str, int length); | 123 srtp_octet_string_hex_string(const void *str, int length); |
131 | 124 |
132 char * | 125 char * |
133 v128_bit_string(v128_t *x); | 126 v128_bit_string(v128_t *x); |
134 | 127 |
135 char * | 128 char * |
136 v128_hex_string(v128_t *x); | 129 v128_hex_string(v128_t *x); |
137 | 130 |
138 uint8_t | |
139 nibble_to_hex_char(uint8_t nibble); | |
140 | |
141 char * | |
142 char_to_hex_string(char *x, int num_char); | |
143 | |
144 uint8_t | |
145 hex_string_to_octet(char *s); | |
146 | |
147 /* | |
148 * hex_string_to_octet_string(raw, hex, len) converts the hexadecimal | |
149 * string at *hex (of length len octets) to the equivalent raw data | |
150 * and writes it to *raw. | |
151 * | |
152 * if a character in the hex string that is not a hexadeciaml digit | |
153 * (0123456789abcdefABCDEF) is encountered, the function stops writing | |
154 * data to *raw | |
155 * | |
156 * the number of hex digits copied (which is two times the number of | |
157 * octets in *raw) is returned | |
158 */ | |
159 | |
160 int | |
161 hex_string_to_octet_string(char *raw, char *hex, int len); | |
162 | |
163 v128_t | |
164 hex_string_to_v128(char *s); | |
165 | |
166 void | 131 void |
167 v128_copy_octet_string(v128_t *x, const uint8_t s[16]); | 132 v128_copy_octet_string(v128_t *x, const uint8_t s[16]); |
168 | 133 |
169 void | 134 void |
170 v128_left_shift(v128_t *x, int shift_index); | 135 v128_left_shift(v128_t *x, int shift_index); |
171 | 136 |
172 void | 137 void |
173 v128_right_shift(v128_t *x, int shift_index); | 138 v128_right_shift(v128_t *x, int shift_index); |
174 | 139 |
175 /* | 140 /* |
(...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
371 | 336 |
372 void | 337 void |
373 v128_clear_bit(v128_t *x, int i); | 338 v128_clear_bit(v128_t *x, int i); |
374 | 339 |
375 void | 340 void |
376 v128_set_bit_to(v128_t *x, int i, int y); | 341 v128_set_bit_to(v128_t *x, int i, int y); |
377 | 342 |
378 #endif /* DATATYPES_USE_MACROS */ | 343 #endif /* DATATYPES_USE_MACROS */ |
379 | 344 |
380 /* | 345 /* |
381 * octet_string_is_eq(a,b, len) returns 1 if the length len strings a | 346 * octet_string_is_eq(a, b, len) returns 1 if the length len strings a |
382 * and b are not equal, returns 0 otherwise | 347 * and b are not equal. It returns 0 otherwise. The running time of the |
| 348 * comparison depends only on len, making this safe to use for (e.g.) |
| 349 * verifying authentication tags. |
383 */ | 350 */ |
384 | 351 |
385 int | 352 int |
386 octet_string_is_eq(uint8_t *a, uint8_t *b, int len); | 353 octet_string_is_eq(uint8_t *a, uint8_t *b, int len); |
387 | 354 |
388 void | 355 void |
389 octet_string_set_to_zero(uint8_t *s, int len); | 356 octet_string_set_to_zero(uint8_t *s, int len); |
390 | 357 |
391 | 358 |
392 #if !defined(SRTP_KERNEL_LINUX) && defined(HAVE_CONFIG_H) | 359 #if defined(HAVE_CONFIG_H) |
393 | 360 |
394 /* | 361 /* |
395 * Convert big endian integers to CPU byte order. | 362 * Convert big endian integers to CPU byte order. |
396 */ | 363 */ |
397 #ifdef WORDS_BIGENDIAN | 364 #ifdef WORDS_BIGENDIAN |
398 /* Nothing to do. */ | 365 /* Nothing to do. */ |
399 # define be32_to_cpu(x) (x) | 366 # define be32_to_cpu(x) (x) |
400 # define be64_to_cpu(x) (x) | 367 # define be64_to_cpu(x) (x) |
401 #elif defined(HAVE_BYTESWAP_H) | 368 #elif defined(HAVE_BYTESWAP_H) |
402 /* We have (hopefully) optimized versions in byteswap.h */ | 369 /* We have (hopefully) optimized versions in byteswap.h */ |
(...skipping 22 matching lines...) Expand all Loading... |
425 # ifdef NO_64BIT_MATH | 392 # ifdef NO_64BIT_MATH |
426 /* use the make64 functions to do 64-bit math */ | 393 /* use the make64 functions to do 64-bit math */ |
427 v = make64(htonl(low32(v)),htonl(high32(v))); | 394 v = make64(htonl(low32(v)),htonl(high32(v))); |
428 # else | 395 # else |
429 /* use the native 64-bit math */ | 396 /* use the native 64-bit math */ |
430 v= (uint64_t)((be32_to_cpu((uint32_t)(v >> 32))) | (((uint64_t)be32_to_cpu((u
int32_t)v)) << 32)); | 397 v= (uint64_t)((be32_to_cpu((uint32_t)(v >> 32))) | (((uint64_t)be32_to_cpu((u
int32_t)v)) << 32)); |
431 # endif | 398 # endif |
432 return v; | 399 return v; |
433 } | 400 } |
434 | 401 |
435 #endif /* ! SRTP_KERNEL_LINUX */ | 402 #endif |
436 | 403 |
437 #endif /* WORDS_BIGENDIAN */ | 404 #endif /* WORDS_BIGENDIAN */ |
438 | 405 |
439 /* | 406 /* |
440 * functions manipulating bitvector_t | 407 * functions manipulating bitvector_t |
441 * | 408 * |
442 * A bitvector_t consists of an array of words and an integer | 409 * A bitvector_t consists of an array of words and an integer |
443 * representing the number of significant bits stored in the array. | 410 * representing the number of significant bits stored in the array. |
444 * The bits are packed as follows: the least significant bit is that | 411 * The bits are packed as follows: the least significant bit is that |
445 * of word[0], while the most significant bit is the nth most | 412 * of word[0], while the most significant bit is the nth most |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
508 | 475 |
509 void | 476 void |
510 bitvector_set_to_zero(bitvector_t *x); | 477 bitvector_set_to_zero(bitvector_t *x); |
511 | 478 |
512 void | 479 void |
513 bitvector_left_shift(bitvector_t *x, int index); | 480 bitvector_left_shift(bitvector_t *x, int index); |
514 | 481 |
515 char * | 482 char * |
516 bitvector_bit_string(bitvector_t *x, char* buf, int len); | 483 bitvector_bit_string(bitvector_t *x, char* buf, int len); |
517 | 484 |
518 #ifdef TESTAPP_SOURCE | 485 #ifdef __cplusplus |
519 int base64_string_to_octet_string(char *raw, int *pad, char *base64, int len); | 486 } |
520 #endif | 487 #endif |
521 | 488 |
522 #endif /* _DATATYPES_H */ | 489 #endif /* _DATATYPES_H */ |
OLD | NEW |