diff options
Diffstat (limited to 'third_party/srtp/crypto/math')
-rw-r--r-- | third_party/srtp/crypto/math/datatypes.c | 372 | ||||
-rw-r--r-- | third_party/srtp/crypto/math/gf2_8.c | 6 | ||||
-rw-r--r-- | third_party/srtp/crypto/math/math.c | 174 | ||||
-rw-r--r-- | third_party/srtp/crypto/math/stat.c | 40 |
4 files changed, 225 insertions, 367 deletions
diff --git a/third_party/srtp/crypto/math/datatypes.c b/third_party/srtp/crypto/math/datatypes.c index c8f44a52..a30873ee 100644 --- a/third_party/srtp/crypto/math/datatypes.c +++ b/third_party/srtp/crypto/math/datatypes.c @@ -43,6 +43,10 @@ * */ +#ifdef HAVE_CONFIG_H + #include <config.h> +#endif + #include "datatypes.h" int @@ -113,8 +117,8 @@ octet_string_hex_string(const void *s, int length) { length *= 2; /* truncate string if it would be too long */ - if (length >= MAX_PRINT_STRING_LEN-1) - length = MAX_PRINT_STRING_LEN-2; + if (length > MAX_PRINT_STRING_LEN) + length = MAX_PRINT_STRING_LEN-1; for (i=0; i < length; i+=2) { bit_string[i] = nibble_to_hex_char(*str >> 4); @@ -149,9 +153,10 @@ hex_char_to_nibble(uint8_t c) { case ('E'): return 0xe; case ('f'): return 0xf; case ('F'): return 0xf; - default: break; /* this flags an error */ + default: return -1; /* this flags an error */ } - return -1; + /* NOTREACHED */ + return -1; /* this keeps compilers from complaining */ } int @@ -206,16 +211,16 @@ v128_hex_string(v128_t *x) { char * v128_bit_string(v128_t *x) { - int j, index; + int j, i; uint32_t mask; - for (j=index=0; j < 4; j++) { + for (j=i=0; j < 4; j++) { for (mask=0x80000000; mask > 0; mask >>= 1) { if (x->v32[j] & mask) - bit_string[index] = '1'; + bit_string[i] = '1'; else - bit_string[index] = '0'; - ++index; + bit_string[i] = '0'; + ++i; } } bit_string[128] = 0; /* null terminate string */ @@ -322,13 +327,13 @@ v128_set_bit_to(v128_t *x, int i, int y){ #endif /* DATATYPES_USE_MACROS */ void -v128_right_shift(v128_t *x, int index) { - const int base_index = index >> 5; - const int bit_index = index & 31; +v128_right_shift(v128_t *x, int shift) { + const int base_index = shift >> 5; + const int bit_index = shift & 31; int i, from; uint32_t b; - if (index > 127) { + if (shift > 127) { v128_set_to_zero(x); return; } @@ -360,12 +365,12 @@ v128_right_shift(v128_t *x, int index) { } void -v128_left_shift(v128_t *x, int index) { +v128_left_shift(v128_t *x, int shift) { int i; - const int base_index = index >> 5; - const int bit_index = index & 31; + const int base_index = shift >> 5; + const int bit_index = shift & 31; - if (index > 127) { + if (shift > 127) { v128_set_to_zero(x); return; } @@ -386,6 +391,124 @@ v128_left_shift(v128_t *x, int index) { } +/* functions manipulating bitvector_t */ + +#ifndef DATATYPES_USE_MACROS /* little functions are not macros */ + +int +bitvector_get_bit(const bitvector_t *v, int bit_index) +{ + return _bitvector_get_bit(v, bit_index); +} + +void +bitvector_set_bit(bitvector_t *v, int bit_index) +{ + _bitvector_set_bit(v, bit_index); +} + +void +bitvector_clear_bit(bitvector_t *v, int bit_index) +{ + _bitvector_clear_bit(v, bit_index); +} + + +#endif /* DATATYPES_USE_MACROS */ + +int +bitvector_alloc(bitvector_t *v, unsigned long length) { + unsigned long l; + + /* Round length up to a multiple of bits_per_word */ + length = (length + bits_per_word - 1) & ~(unsigned long)((bits_per_word - 1)); + + l = length / bits_per_word * bytes_per_word; + + /* allocate memory, then set parameters */ + if (l == 0) + v->word = NULL; + else { + v->word = (uint32_t*)crypto_alloc(l); + if (v->word == NULL) { + v->word = NULL; + v->length = 0; + return -1; + } + } + v->length = length; + + /* initialize bitvector to zero */ + bitvector_set_to_zero(v); + + return 0; +} + + +void +bitvector_dealloc(bitvector_t *v) { + if (v->word != NULL) + crypto_free(v->word); + v->word = NULL; + v->length = 0; +} + +void +bitvector_set_to_zero(bitvector_t *x) +{ + /* C99 guarantees that memset(0) will set the value 0 for uint32_t */ + memset(x->word, 0, x->length >> 3); +} + +char * +bitvector_bit_string(bitvector_t *x, char* buf, int len) { + int j, i; + uint32_t mask; + + for (j=i=0; j < (int)(x->length>>5) && i < len-1; j++) { + for (mask=0x80000000; mask > 0; mask >>= 1) { + if (x->word[j] & mask) + buf[i] = '1'; + else + buf[i] = '0'; + ++i; + if (i >= len-1) + break; + } + } + buf[i] = 0; /* null terminate string */ + + return buf; +} + +void +bitvector_left_shift(bitvector_t *x, int shift) { + int i; + const int base_index = shift >> 5; + const int bit_index = shift & 31; + const int word_length = x->length >> 5; + + if (shift >= (int)x->length) { + bitvector_set_to_zero(x); + return; + } + + if (bit_index == 0) { + for (i=0; i < word_length - base_index; i++) + x->word[i] = x->word[i+base_index]; + } else { + for (i=0; i < word_length - base_index - 1; i++) + x->word[i] = (x->word[i+base_index] >> bit_index) ^ + (x->word[i+base_index+1] << (32 - bit_index)); + x->word[word_length - base_index-1] = x->word[word_length-1] >> bit_index; + } + + /* now wrap up the final portion */ + for (i = word_length - base_index; i < word_length; i++) + x->word[i] = 0; + +} + int octet_string_is_eq(uint8_t *a, uint8_t *b, int len) { @@ -406,194 +529,41 @@ octet_string_set_to_zero(uint8_t *s, int len) { } +#ifdef TESTAPP_SOURCE -/* - * From RFC 1521: The Base64 Alphabet - * - * Value Encoding Value Encoding Value Encoding Value Encoding - * 0 A 17 R 34 i 51 z - * 1 B 18 S 35 j 52 0 - * 2 C 19 T 36 k 53 1 - * 3 D 20 U 37 l 54 2 - * 4 E 21 V 38 m 55 3 - * 5 F 22 W 39 n 56 4 - * 6 G 23 X 40 o 57 5 - * 7 H 24 Y 41 p 58 6 - * 8 I 25 Z 42 q 59 7 - * 9 J 26 a 43 r 60 8 - * 10 K 27 b 44 s 61 9 - * 11 L 28 c 45 t 62 + - * 12 M 29 d 46 u 63 / - * 13 N 30 e 47 v - * 14 O 31 f 48 w (pad) = - * 15 P 32 g 49 x - * 16 Q 33 h 50 y - */ +static const char b64chars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz0123456789+/"; -int -base64_char_to_sextet(uint8_t c) { - switch(c) { - case 'A': - return 0; - case 'B': - return 1; - case 'C': - return 2; - case 'D': - return 3; - case 'E': - return 4; - case 'F': - return 5; - case 'G': - return 6; - case 'H': - return 7; - case 'I': - return 8; - case 'J': - return 9; - case 'K': - return 10; - case 'L': - return 11; - case 'M': - return 12; - case 'N': - return 13; - case 'O': - return 14; - case 'P': - return 15; - case 'Q': - return 16; - case 'R': - return 17; - case 'S': - return 18; - case 'T': - return 19; - case 'U': - return 20; - case 'V': - return 21; - case 'W': - return 22; - case 'X': - return 23; - case 'Y': - return 24; - case 'Z': - return 25; - case 'a': - return 26; - case 'b': - return 27; - case 'c': - return 28; - case 'd': - return 29; - case 'e': - return 30; - case 'f': - return 31; - case 'g': - return 32; - case 'h': - return 33; - case 'i': - return 34; - case 'j': - return 35; - case 'k': - return 36; - case 'l': - return 37; - case 'm': - return 38; - case 'n': - return 39; - case 'o': - return 40; - case 'p': - return 41; - case 'q': - return 42; - case 'r': - return 43; - case 's': - return 44; - case 't': - return 45; - case 'u': - return 46; - case 'v': - return 47; - case 'w': - return 48; - case 'x': - return 49; - case 'y': - return 50; - case 'z': - return 51; - case '0': - return 52; - case '1': - return 53; - case '2': - return 54; - case '3': - return 55; - case '4': - return 56; - case '5': - return 57; - case '6': - return 58; - case '7': - return 59; - case '8': - return 60; - case '9': - return 61; - case '+': - return 62; - case '/': - return 63; - case '=': - return 64; - default: - break; - } - return -1; -} +static int base64_block_to_octet_triple(char *out, char *in) { + unsigned char sextets[4] = {0}; + int j = 0; + int i; -/* - * base64_string_to_octet_string converts a hexadecimal string - * of length 2 * len to a raw octet string of length len - */ + for (i = 0; i < 4; i++) { + char *p = strchr(b64chars, in[i]); + if (p != NULL) sextets[i] = p - b64chars; + else j++; + } -int -base64_string_to_octet_string(char *raw, char *base64, int len) { - uint8_t x; - int tmp; - int base64_len; + out[0] = (sextets[0]<<2)|(sextets[1]>>4); + if (j < 2) out[1] = (sextets[1]<<4)|(sextets[2]>>2); + if (j < 1) out[2] = (sextets[2]<<6)|sextets[3]; + return j; +} - base64_len = 0; - while (base64_len < len) { - tmp = base64_char_to_sextet(base64[0]); - if (tmp == -1) - return base64_len; - x = (tmp << 6); - base64_len++; - tmp = base64_char_to_sextet(base64[1]); - if (tmp == -1) - return base64_len; - x |= (tmp & 0xffff); - base64_len++; - *raw++ = x; - base64 += 2; +int base64_string_to_octet_string(char *out, int *pad, char *in, int len) { + int k = 0; + int i = 0; + int j = 0; + if (len % 4 != 0) return 0; + + while (i < len && j == 0) { + j = base64_block_to_octet_triple(out + k, in + i); + k += 3; + i += 4; } - return base64_len; + *pad = j; + return i; } + +#endif diff --git a/third_party/srtp/crypto/math/gf2_8.c b/third_party/srtp/crypto/math/gf2_8.c index 8a112ba7..c57f8d23 100644 --- a/third_party/srtp/crypto/math/gf2_8.c +++ b/third_party/srtp/crypto/math/gf2_8.c @@ -45,12 +45,16 @@ */ +#ifdef HAVE_CONFIG_H + #include <config.h> +#endif + #include "datatypes.h" #include "gf2_8.h" /* gf2_8_shift() moved to gf2_8.h as an inline function */ -inline gf2_8 +gf2_8 gf2_8_multiply(gf2_8 x, gf2_8 y) { gf2_8 z = 0; diff --git a/third_party/srtp/crypto/math/math.c b/third_party/srtp/crypto/math/math.c index 3e619979..7f0bcd2b 100644 --- a/third_party/srtp/crypto/math/math.c +++ b/third_party/srtp/crypto/math/math.c @@ -42,8 +42,11 @@ * */ +#ifdef HAVE_CONFIG_H + #include <config.h> +#endif + #include "crypto_math.h" -#include <stdlib.h> /* malloc() used in bitvector_alloc */ int octet_weight[256] = { @@ -173,7 +176,7 @@ v32_weight(v32_t a) { return wt; } -inline unsigned char +unsigned char v32_distance(v32_t x, v32_t y) { x.value ^= y.value; return v32_weight(x); @@ -524,13 +527,13 @@ A_times_x_plus_b(uint8_t A[8], uint8_t x, uint8_t b) { return b; } -inline void +void v16_copy_octet_string(v16_t *x, const uint8_t s[2]) { x->v8[0] = s[0]; x->v8[1] = s[1]; } -inline void +void v32_copy_octet_string(v32_t *x, const uint8_t s[4]) { x->v8[0] = s[0]; x->v8[1] = s[1]; @@ -538,7 +541,7 @@ v32_copy_octet_string(v32_t *x, const uint8_t s[4]) { x->v8[3] = s[3]; } -inline void +void v64_copy_octet_string(v64_t *x, const uint8_t s[8]) { x->v8[0] = s[0]; x->v8[1] = s[1]; @@ -632,7 +635,7 @@ v128_set_bit_to(v128_t *x, int i, int y){ #endif /* DATATYPES_USE_MACROS */ -inline void +static inline void v128_left_shift2(v128_t *x, int num_bits) { int i; int word_shift = num_bits >> 5; @@ -773,165 +776,6 @@ octet_string_set_to_zero(uint8_t *s, int len) { } -/* functions manipulating bit_vector_t */ - -#define BITVECTOR_MAX_WORDS 5 - -int -bitvector_alloc(bitvector_t *v, unsigned long length) { - unsigned long l = (length + bytes_per_word - 1) / bytes_per_word; - int i; - - /* allocate memory, then set parameters */ - if (l > BITVECTOR_MAX_WORDS) - return -1; - else - l = BITVECTOR_MAX_WORDS; - v->word = malloc(l); - if (v->word == NULL) - return -1; - v->length = length; - - /* initialize bitvector to zero */ - for (i=0; i < (length >> 5); i++) { - v->word = 0; - } - - return 0; -} - -void -bitvector_set_bit(bitvector_t *v, int bit_index) { - - v->word[(bit_index >> 5)] |= (1 << (bit_index & 31)); - -} - -int -bitvector_get_bit(const bitvector_t *v, int bit_index) { - - return ((v->word[(bit_index >> 5)]) >> (bit_index & 31)) & 1; - -} - -#include <stdio.h> - -int -bitvector_print_hex(const bitvector_t *v, FILE *stream) { - int i; - int m = v->length >> 5; - int n = v->length & 31; - char string[9]; - uint32_t tmp; - - /* if length isn't a multiple of four, we can't hex_print */ - if (n & 3) - return -1; - - /* if the length is zero, do nothing */ - if (v->length == 0) - return 0; - - /* - * loop over words from most significant to least significant - - */ - - for (i=m; i > 0; i++) { - char *str = string + 7; - tmp = v->word[i]; - - /* null terminate string */ - string[8] = 0; - - /* loop over nibbles */ - *str-- = nibble_to_hex_char(tmp & 0xf); tmp >>= 4; - *str-- = nibble_to_hex_char(tmp & 0xf); tmp >>= 4; - *str-- = nibble_to_hex_char(tmp & 0xf); tmp >>= 4; - *str-- = nibble_to_hex_char(tmp & 0xf); tmp >>= 4; - *str-- = nibble_to_hex_char(tmp & 0xf); tmp >>= 4; - *str-- = nibble_to_hex_char(tmp & 0xf); tmp >>= 4; - *str-- = nibble_to_hex_char(tmp & 0xf); tmp >>= 4; - *str-- = nibble_to_hex_char(tmp & 0xf); - - /* now print stream */ - fprintf(stream, string); - } - - return 0; - -} - - -int -hex_string_length(char *s) { - int count = 0; - - /* ignore leading zeros */ - while ((*s != 0) && *s == '0') - s++; - - /* count remaining characters */ - while (*s != 0) { - if (hex_char_to_nibble(*s++) == -1) - return -1; - count++; - } - - return count; -} - -int -bitvector_set_from_hex(bitvector_t *v, char *string) { - int num_hex_chars, m, n, i, j; - uint32_t tmp; - - num_hex_chars = hex_string_length(string); - if (num_hex_chars == -1) - return -1; - - /* set length */ - v->length = num_hex_chars * 4; - /* - * at this point, we should subtract away a bit if the high - * bit of the first character is zero, but we ignore that - * for now and assume that we're four-bit aligned - DAM - */ - - - m = num_hex_chars / 8; /* number of words */ - n = num_hex_chars % 8; /* number of nibbles in last word */ - - /* if the length is greater than the bitvector, return an error */ - if (m > BITVECTOR_MAX_WORDS) - return -1; - - /* - * loop over words from most significant - first word is a special - * case - */ - - if (n) { - tmp = 0; - for (i=0; i < n; i++) { - tmp = hex_char_to_nibble(*string++); - tmp <<= 4; - } - v->word[m] = tmp; - } - - /* now loop over the rest of the words */ - for (i=m-1; i >= 0; i--) { - tmp = 0; - for (j=0; j < 8; j++) { - tmp = hex_char_to_nibble(*string++); - tmp <<= 4; - } - v->word[i] = tmp; - } - - return 0; -} - /* functions below not yet tested! */ diff --git a/third_party/srtp/crypto/math/stat.c b/third_party/srtp/crypto/math/stat.c index 5e46c209..aaff3c4f 100644 --- a/third_party/srtp/crypto/math/stat.c +++ b/third_party/srtp/crypto/math/stat.c @@ -7,6 +7,46 @@ * Cisco Systems, Inc. */ +/* + * + * Copyright (c) 2001-2006, Cisco Systems, Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * + * Neither the name of the Cisco Systems, Inc. nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifdef HAVE_CONFIG_H + #include <config.h> +#endif + #include "stat.h" debug_module_t mod_stat = { |