michael@0: /* michael@0: * math.c michael@0: * michael@0: * crypto math operations and data types michael@0: * michael@0: * David A. McGrew michael@0: * Cisco Systems, Inc. michael@0: */ michael@0: /* michael@0: * michael@0: * Copyright (c) 2001-2006 Cisco Systems, Inc. michael@0: * All rights reserved. michael@0: * michael@0: * Redistribution and use in source and binary forms, with or without michael@0: * modification, are permitted provided that the following conditions michael@0: * are met: michael@0: * michael@0: * Redistributions of source code must retain the above copyright michael@0: * notice, this list of conditions and the following disclaimer. michael@0: * michael@0: * Redistributions in binary form must reproduce the above michael@0: * copyright notice, this list of conditions and the following michael@0: * disclaimer in the documentation and/or other materials provided michael@0: * with the distribution. michael@0: * michael@0: * Neither the name of the Cisco Systems, Inc. nor the names of its michael@0: * contributors may be used to endorse or promote products derived michael@0: * from this software without specific prior written permission. michael@0: * michael@0: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@0: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT michael@0: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS michael@0: * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE michael@0: * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, michael@0: * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES michael@0: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR michael@0: * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) michael@0: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, michael@0: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) michael@0: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED michael@0: * OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: * michael@0: */ michael@0: michael@0: #include "crypto_math.h" michael@0: michael@0: int michael@0: octet_weight[256] = { michael@0: 0, 1, 1, 2, 1, 2, 2, 3, michael@0: 1, 2, 2, 3, 2, 3, 3, 4, michael@0: 1, 2, 2, 3, 2, 3, 3, 4, michael@0: 2, 3, 3, 4, 3, 4, 4, 5, michael@0: 1, 2, 2, 3, 2, 3, 3, 4, michael@0: 2, 3, 3, 4, 3, 4, 4, 5, michael@0: 2, 3, 3, 4, 3, 4, 4, 5, michael@0: 3, 4, 4, 5, 4, 5, 5, 6, michael@0: 1, 2, 2, 3, 2, 3, 3, 4, michael@0: 2, 3, 3, 4, 3, 4, 4, 5, michael@0: 2, 3, 3, 4, 3, 4, 4, 5, michael@0: 3, 4, 4, 5, 4, 5, 5, 6, michael@0: 2, 3, 3, 4, 3, 4, 4, 5, michael@0: 3, 4, 4, 5, 4, 5, 5, 6, michael@0: 3, 4, 4, 5, 4, 5, 5, 6, michael@0: 4, 5, 5, 6, 5, 6, 6, 7, michael@0: 1, 2, 2, 3, 2, 3, 3, 4, michael@0: 2, 3, 3, 4, 3, 4, 4, 5, michael@0: 2, 3, 3, 4, 3, 4, 4, 5, michael@0: 3, 4, 4, 5, 4, 5, 5, 6, michael@0: 2, 3, 3, 4, 3, 4, 4, 5, michael@0: 3, 4, 4, 5, 4, 5, 5, 6, michael@0: 3, 4, 4, 5, 4, 5, 5, 6, michael@0: 4, 5, 5, 6, 5, 6, 6, 7, michael@0: 2, 3, 3, 4, 3, 4, 4, 5, michael@0: 3, 4, 4, 5, 4, 5, 5, 6, michael@0: 3, 4, 4, 5, 4, 5, 5, 6, michael@0: 4, 5, 5, 6, 5, 6, 6, 7, michael@0: 3, 4, 4, 5, 4, 5, 5, 6, michael@0: 4, 5, 5, 6, 5, 6, 6, 7, michael@0: 4, 5, 5, 6, 5, 6, 6, 7, michael@0: 5, 6, 6, 7, 6, 7, 7, 8 michael@0: }; michael@0: michael@0: int michael@0: low_bit[256] = { michael@0: -1, 0, 1, 0, 2, 0, 1, 0, michael@0: 3, 0, 1, 0, 2, 0, 1, 0, michael@0: 4, 0, 1, 0, 2, 0, 1, 0, michael@0: 3, 0, 1, 0, 2, 0, 1, 0, michael@0: 5, 0, 1, 0, 2, 0, 1, 0, michael@0: 3, 0, 1, 0, 2, 0, 1, 0, michael@0: 4, 0, 1, 0, 2, 0, 1, 0, michael@0: 3, 0, 1, 0, 2, 0, 1, 0, michael@0: 6, 0, 1, 0, 2, 0, 1, 0, michael@0: 3, 0, 1, 0, 2, 0, 1, 0, michael@0: 4, 0, 1, 0, 2, 0, 1, 0, michael@0: 3, 0, 1, 0, 2, 0, 1, 0, michael@0: 5, 0, 1, 0, 2, 0, 1, 0, michael@0: 3, 0, 1, 0, 2, 0, 1, 0, michael@0: 4, 0, 1, 0, 2, 0, 1, 0, michael@0: 3, 0, 1, 0, 2, 0, 1, 0, michael@0: 7, 0, 1, 0, 2, 0, 1, 0, michael@0: 3, 0, 1, 0, 2, 0, 1, 0, michael@0: 4, 0, 1, 0, 2, 0, 1, 0, michael@0: 3, 0, 1, 0, 2, 0, 1, 0, michael@0: 5, 0, 1, 0, 2, 0, 1, 0, michael@0: 3, 0, 1, 0, 2, 0, 1, 0, michael@0: 4, 0, 1, 0, 2, 0, 1, 0, michael@0: 3, 0, 1, 0, 2, 0, 1, 0, michael@0: 6, 0, 1, 0, 2, 0, 1, 0, michael@0: 3, 0, 1, 0, 2, 0, 1, 0, michael@0: 4, 0, 1, 0, 2, 0, 1, 0, michael@0: 3, 0, 1, 0, 2, 0, 1, 0, michael@0: 5, 0, 1, 0, 2, 0, 1, 0, michael@0: 3, 0, 1, 0, 2, 0, 1, 0, michael@0: 4, 0, 1, 0, 2, 0, 1, 0, michael@0: 3, 0, 1, 0, 2, 0, 1, 0 michael@0: }; michael@0: michael@0: michael@0: int michael@0: high_bit[256] = { michael@0: -1, 0, 1, 1, 2, 2, 2, 2, michael@0: 3, 3, 3, 3, 3, 3, 3, 3, michael@0: 4, 4, 4, 4, 4, 4, 4, 4, michael@0: 4, 4, 4, 4, 4, 4, 4, 4, michael@0: 5, 5, 5, 5, 5, 5, 5, 5, michael@0: 5, 5, 5, 5, 5, 5, 5, 5, michael@0: 5, 5, 5, 5, 5, 5, 5, 5, michael@0: 5, 5, 5, 5, 5, 5, 5, 5, michael@0: 6, 6, 6, 6, 6, 6, 6, 6, michael@0: 6, 6, 6, 6, 6, 6, 6, 6, michael@0: 6, 6, 6, 6, 6, 6, 6, 6, michael@0: 6, 6, 6, 6, 6, 6, 6, 6, michael@0: 6, 6, 6, 6, 6, 6, 6, 6, michael@0: 6, 6, 6, 6, 6, 6, 6, 6, michael@0: 6, 6, 6, 6, 6, 6, 6, 6, michael@0: 6, 6, 6, 6, 6, 6, 6, 6, michael@0: 7, 7, 7, 7, 7, 7, 7, 7, michael@0: 7, 7, 7, 7, 7, 7, 7, 7, michael@0: 7, 7, 7, 7, 7, 7, 7, 7, michael@0: 7, 7, 7, 7, 7, 7, 7, 7, michael@0: 7, 7, 7, 7, 7, 7, 7, 7, michael@0: 7, 7, 7, 7, 7, 7, 7, 7, michael@0: 7, 7, 7, 7, 7, 7, 7, 7, michael@0: 7, 7, 7, 7, 7, 7, 7, 7, michael@0: 7, 7, 7, 7, 7, 7, 7, 7, michael@0: 7, 7, 7, 7, 7, 7, 7, 7, michael@0: 7, 7, 7, 7, 7, 7, 7, 7, michael@0: 7, 7, 7, 7, 7, 7, 7, 7, michael@0: 7, 7, 7, 7, 7, 7, 7, 7, michael@0: 7, 7, 7, 7, 7, 7, 7, 7, michael@0: 7, 7, 7, 7, 7, 7, 7, 7, michael@0: 7, 7, 7, 7, 7, 7, 7, 7 michael@0: }; michael@0: michael@0: int michael@0: octet_get_weight(uint8_t octet) { michael@0: extern int octet_weight[256]; michael@0: michael@0: return octet_weight[octet]; michael@0: } michael@0: michael@0: unsigned char michael@0: v32_weight(v32_t a) { michael@0: unsigned int wt = 0; michael@0: michael@0: wt += octet_weight[a.v8[0]]; /* note: endian-ness makes no difference */ michael@0: wt += octet_weight[a.v8[1]]; michael@0: wt += octet_weight[a.v8[2]]; michael@0: wt += octet_weight[a.v8[3]]; michael@0: michael@0: return wt; michael@0: } michael@0: michael@0: unsigned char michael@0: v32_distance(v32_t x, v32_t y) { michael@0: x.value ^= y.value; michael@0: return v32_weight(x); michael@0: } michael@0: michael@0: unsigned int michael@0: v32_dot_product(v32_t a, v32_t b) { michael@0: a.value &= b.value; michael@0: return v32_weight(a) & 1; michael@0: } michael@0: michael@0: /* michael@0: * _bit_string returns a NULL-terminated character string suitable for michael@0: * printing michael@0: */ michael@0: michael@0: #define MAX_STRING_LENGTH 1024 michael@0: michael@0: char bit_string[MAX_STRING_LENGTH]; michael@0: michael@0: char * michael@0: octet_bit_string(uint8_t x) { michael@0: int mask, index; michael@0: michael@0: for (mask = 1, index = 0; mask < 256; mask <<= 1) michael@0: if ((x & mask) == 0) michael@0: bit_string[index++] = '0'; michael@0: else michael@0: bit_string[index++] = '1'; michael@0: michael@0: bit_string[index++] = 0; /* NULL terminate string */ michael@0: michael@0: return bit_string; michael@0: } michael@0: michael@0: char * michael@0: v16_bit_string(v16_t x) { michael@0: int i, mask, index; michael@0: michael@0: for (i = index = 0; i < 2; i++) { michael@0: for (mask = 1; mask < 256; mask <<= 1) michael@0: if ((x.v8[i] & mask) == 0) michael@0: bit_string[index++] = '0'; michael@0: else michael@0: bit_string[index++] = '1'; michael@0: } michael@0: bit_string[index++] = 0; /* NULL terminate string */ michael@0: return bit_string; michael@0: } michael@0: michael@0: char * michael@0: v32_bit_string(v32_t x) { michael@0: int i, mask, index; michael@0: michael@0: for (i = index = 0; i < 4; i++) { michael@0: for (mask = 128; mask > 0; mask >>= 1) michael@0: if ((x.v8[i] & mask) == 0) michael@0: bit_string[index++] = '0'; michael@0: else michael@0: bit_string[index++] = '1'; michael@0: } michael@0: bit_string[index++] = 0; /* NULL terminate string */ michael@0: return bit_string; michael@0: } michael@0: michael@0: char * michael@0: v64_bit_string(const v64_t *x) { michael@0: int i, mask, index; michael@0: michael@0: for (i = index = 0; i < 8; i++) { michael@0: for (mask = 1; mask < 256; mask <<= 1) michael@0: if ((x->v8[i] & mask) == 0) michael@0: bit_string[index++] = '0'; michael@0: else michael@0: bit_string[index++] = '1'; michael@0: } michael@0: bit_string[index++] = 0; /* NULL terminate string */ michael@0: return bit_string; michael@0: } michael@0: michael@0: char * michael@0: v128_bit_string(v128_t *x) { michael@0: int j, index; michael@0: uint32_t mask; michael@0: michael@0: for (j=index=0; j < 4; j++) { michael@0: for (mask=0x80000000; mask > 0; mask >>= 1) { michael@0: if (x->v32[j] & mask) michael@0: bit_string[index] = '1'; michael@0: else michael@0: bit_string[index] = '0'; michael@0: ++index; michael@0: } michael@0: } michael@0: bit_string[128] = 0; /* null terminate string */ michael@0: michael@0: return bit_string; michael@0: } michael@0: michael@0: uint8_t michael@0: nibble_to_hex_char(uint8_t nibble) { michael@0: char buf[16] = {'0', '1', '2', '3', '4', '5', '6', '7', michael@0: '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; michael@0: return buf[nibble & 0xF]; michael@0: } michael@0: michael@0: char * michael@0: octet_hex_string(uint8_t x) { michael@0: michael@0: bit_string[0] = nibble_to_hex_char(x >> 4); michael@0: bit_string[1] = nibble_to_hex_char(x & 0xF); michael@0: michael@0: bit_string[2] = 0; /* null terminate string */ michael@0: return bit_string; michael@0: } michael@0: michael@0: char * michael@0: octet_string_hex_string(const void *str, int length) { michael@0: const uint8_t *s = str; michael@0: int i; michael@0: michael@0: /* double length, since one octet takes two hex characters */ michael@0: length *= 2; michael@0: michael@0: /* truncate string if it would be too long */ michael@0: if (length > MAX_STRING_LENGTH) michael@0: length = MAX_STRING_LENGTH-1; michael@0: michael@0: for (i=0; i < length; i+=2) { michael@0: bit_string[i] = nibble_to_hex_char(*s >> 4); michael@0: bit_string[i+1] = nibble_to_hex_char(*s++ & 0xF); michael@0: } michael@0: bit_string[i] = 0; /* null terminate string */ michael@0: return bit_string; michael@0: } michael@0: michael@0: char * michael@0: v16_hex_string(v16_t x) { michael@0: int i, j; michael@0: michael@0: for (i=j=0; i < 2; i++) { michael@0: bit_string[j++] = nibble_to_hex_char(x.v8[i] >> 4); michael@0: bit_string[j++] = nibble_to_hex_char(x.v8[i] & 0xF); michael@0: } michael@0: michael@0: bit_string[j] = 0; /* null terminate string */ michael@0: return bit_string; michael@0: } michael@0: michael@0: char * michael@0: v32_hex_string(v32_t x) { michael@0: int i, j; michael@0: michael@0: for (i=j=0; i < 4; i++) { michael@0: bit_string[j++] = nibble_to_hex_char(x.v8[i] >> 4); michael@0: bit_string[j++] = nibble_to_hex_char(x.v8[i] & 0xF); michael@0: } michael@0: michael@0: bit_string[j] = 0; /* null terminate string */ michael@0: return bit_string; michael@0: } michael@0: michael@0: char * michael@0: v64_hex_string(const v64_t *x) { michael@0: int i, j; michael@0: michael@0: for (i=j=0; i < 8; i++) { michael@0: bit_string[j++] = nibble_to_hex_char(x->v8[i] >> 4); michael@0: bit_string[j++] = nibble_to_hex_char(x->v8[i] & 0xF); michael@0: } michael@0: michael@0: bit_string[j] = 0; /* null terminate string */ michael@0: return bit_string; michael@0: } michael@0: michael@0: char * michael@0: v128_hex_string(v128_t *x) { michael@0: int i, j; michael@0: michael@0: for (i=j=0; i < 16; i++) { michael@0: bit_string[j++] = nibble_to_hex_char(x->v8[i] >> 4); michael@0: bit_string[j++] = nibble_to_hex_char(x->v8[i] & 0xF); michael@0: } michael@0: michael@0: bit_string[j] = 0; /* null terminate string */ michael@0: return bit_string; michael@0: } michael@0: michael@0: char * michael@0: char_to_hex_string(char *x, int num_char) { michael@0: int i, j; michael@0: michael@0: if (num_char >= 16) michael@0: num_char = 16; michael@0: for (i=j=0; i < num_char; i++) { michael@0: bit_string[j++] = nibble_to_hex_char(x[i] >> 4); michael@0: bit_string[j++] = nibble_to_hex_char(x[i] & 0xF); michael@0: } michael@0: michael@0: bit_string[j] = 0; /* null terminate string */ michael@0: return bit_string; michael@0: } michael@0: michael@0: int michael@0: hex_char_to_nibble(uint8_t c) { michael@0: switch(c) { michael@0: case ('0'): return 0x0; michael@0: case ('1'): return 0x1; michael@0: case ('2'): return 0x2; michael@0: case ('3'): return 0x3; michael@0: case ('4'): return 0x4; michael@0: case ('5'): return 0x5; michael@0: case ('6'): return 0x6; michael@0: case ('7'): return 0x7; michael@0: case ('8'): return 0x8; michael@0: case ('9'): return 0x9; michael@0: case ('a'): return 0xa; michael@0: case ('A'): return 0xa; michael@0: case ('b'): return 0xb; michael@0: case ('B'): return 0xb; michael@0: case ('c'): return 0xc; michael@0: case ('C'): return 0xc; michael@0: case ('d'): return 0xd; michael@0: case ('D'): return 0xd; michael@0: case ('e'): return 0xe; michael@0: case ('E'): return 0xe; michael@0: case ('f'): return 0xf; michael@0: case ('F'): return 0xf; michael@0: default: return -1; /* this flags an error */ michael@0: } michael@0: /* NOTREACHED */ michael@0: return -1; /* this keeps compilers from complaining */ michael@0: } michael@0: michael@0: int michael@0: is_hex_string(char *s) { michael@0: while(*s != 0) michael@0: if (hex_char_to_nibble(*s++) == -1) michael@0: return 0; michael@0: return 1; michael@0: } michael@0: michael@0: uint8_t michael@0: hex_string_to_octet(char *s) { michael@0: uint8_t x; michael@0: michael@0: x = (hex_char_to_nibble(s[0]) << 4) michael@0: | hex_char_to_nibble(s[1] & 0xFF); michael@0: michael@0: return x; michael@0: } michael@0: michael@0: /* michael@0: * hex_string_to_octet_string converts a hexadecimal string michael@0: * of length 2 * len to a raw octet string of length len michael@0: */ michael@0: michael@0: int michael@0: hex_string_to_octet_string(char *raw, char *hex, int len) { michael@0: uint8_t x; michael@0: int tmp; michael@0: int hex_len; michael@0: michael@0: hex_len = 0; michael@0: while (hex_len < len) { michael@0: tmp = hex_char_to_nibble(hex[0]); michael@0: if (tmp == -1) michael@0: return hex_len; michael@0: x = (tmp << 4); michael@0: hex_len++; michael@0: tmp = hex_char_to_nibble(hex[1]); michael@0: if (tmp == -1) michael@0: return hex_len; michael@0: x |= (tmp & 0xff); michael@0: hex_len++; michael@0: *raw++ = x; michael@0: hex += 2; michael@0: } michael@0: return hex_len; michael@0: } michael@0: michael@0: v16_t michael@0: hex_string_to_v16(char *s) { michael@0: v16_t x; michael@0: int i, j; michael@0: michael@0: for (i=j=0; i < 4; i += 2, j++) { michael@0: x.v8[j] = (hex_char_to_nibble(s[i]) << 4) michael@0: | hex_char_to_nibble(s[i+1] & 0xFF); michael@0: } michael@0: return x; michael@0: } michael@0: michael@0: v32_t michael@0: hex_string_to_v32(char *s) { michael@0: v32_t x; michael@0: int i, j; michael@0: michael@0: for (i=j=0; i < 8; i += 2, j++) { michael@0: x.v8[j] = (hex_char_to_nibble(s[i]) << 4) michael@0: | hex_char_to_nibble(s[i+1] & 0xFF); michael@0: } michael@0: return x; michael@0: } michael@0: michael@0: v64_t michael@0: hex_string_to_v64(char *s) { michael@0: v64_t x; michael@0: int i, j; michael@0: michael@0: for (i=j=0; i < 16; i += 2, j++) { michael@0: x.v8[j] = (hex_char_to_nibble(s[i]) << 4) michael@0: | hex_char_to_nibble(s[i+1] & 0xFF); michael@0: } michael@0: return x; michael@0: } michael@0: michael@0: v128_t michael@0: hex_string_to_v128(char *s) { michael@0: v128_t x; michael@0: int i, j; michael@0: michael@0: for (i=j=0; i < 32; i += 2, j++) { michael@0: x.v8[j] = (hex_char_to_nibble(s[i]) << 4) michael@0: | hex_char_to_nibble(s[i+1] & 0xFF); michael@0: } michael@0: return x; michael@0: } michael@0: michael@0: michael@0: michael@0: /* michael@0: * the matrix A[] is stored in column format, i.e., A[i] is the ith michael@0: * column of the matrix michael@0: */ michael@0: michael@0: uint8_t michael@0: A_times_x_plus_b(uint8_t A[8], uint8_t x, uint8_t b) { michael@0: int index = 0; michael@0: unsigned mask; michael@0: michael@0: for (mask=1; mask < 256; mask *= 2) { michael@0: if (x & mask) michael@0: b^= A[index]; michael@0: ++index; michael@0: } michael@0: michael@0: return b; michael@0: } michael@0: michael@0: void michael@0: v16_copy_octet_string(v16_t *x, const uint8_t s[2]) { michael@0: x->v8[0] = s[0]; michael@0: x->v8[1] = s[1]; michael@0: } michael@0: michael@0: void michael@0: v32_copy_octet_string(v32_t *x, const uint8_t s[4]) { michael@0: x->v8[0] = s[0]; michael@0: x->v8[1] = s[1]; michael@0: x->v8[2] = s[2]; michael@0: x->v8[3] = s[3]; michael@0: } michael@0: michael@0: void michael@0: v64_copy_octet_string(v64_t *x, const uint8_t s[8]) { michael@0: x->v8[0] = s[0]; michael@0: x->v8[1] = s[1]; michael@0: x->v8[2] = s[2]; michael@0: x->v8[3] = s[3]; michael@0: x->v8[4] = s[4]; michael@0: x->v8[5] = s[5]; michael@0: x->v8[6] = s[6]; michael@0: x->v8[7] = s[7]; michael@0: } michael@0: michael@0: void michael@0: v128_copy_octet_string(v128_t *x, const uint8_t s[16]) { michael@0: x->v8[0] = s[0]; michael@0: x->v8[1] = s[1]; michael@0: x->v8[2] = s[2]; michael@0: x->v8[3] = s[3]; michael@0: x->v8[4] = s[4]; michael@0: x->v8[5] = s[5]; michael@0: x->v8[6] = s[6]; michael@0: x->v8[7] = s[7]; michael@0: x->v8[8] = s[8]; michael@0: x->v8[9] = s[9]; michael@0: x->v8[10] = s[10]; michael@0: x->v8[11] = s[11]; michael@0: x->v8[12] = s[12]; michael@0: x->v8[13] = s[13]; michael@0: x->v8[14] = s[14]; michael@0: x->v8[15] = s[15]; michael@0: michael@0: } michael@0: michael@0: #ifndef DATATYPES_USE_MACROS /* little functions are not macros */ michael@0: michael@0: void michael@0: v128_set_to_zero(v128_t *x) { michael@0: _v128_set_to_zero(x); michael@0: } michael@0: michael@0: void michael@0: v128_copy(v128_t *x, const v128_t *y) { michael@0: _v128_copy(x, y); michael@0: } michael@0: michael@0: void michael@0: v128_xor(v128_t *z, v128_t *x, v128_t *y) { michael@0: _v128_xor(z, x, y); michael@0: } michael@0: michael@0: void michael@0: v128_and(v128_t *z, v128_t *x, v128_t *y) { michael@0: _v128_and(z, x, y); michael@0: } michael@0: michael@0: void michael@0: v128_or(v128_t *z, v128_t *x, v128_t *y) { michael@0: _v128_or(z, x, y); michael@0: } michael@0: michael@0: void michael@0: v128_complement(v128_t *x) { michael@0: _v128_complement(x); michael@0: } michael@0: michael@0: int michael@0: v128_is_eq(const v128_t *x, const v128_t *y) { michael@0: return _v128_is_eq(x, y); michael@0: } michael@0: michael@0: int michael@0: v128_get_bit(const v128_t *x, int i) { michael@0: return _v128_get_bit(x, i); michael@0: } michael@0: michael@0: void michael@0: v128_set_bit(v128_t *x, int i) { michael@0: _v128_set_bit(x, i); michael@0: } michael@0: michael@0: void michael@0: v128_clear_bit(v128_t *x, int i){ michael@0: _v128_clear_bit(x, i); michael@0: } michael@0: michael@0: void michael@0: v128_set_bit_to(v128_t *x, int i, int y){ michael@0: _v128_set_bit_to(x, i, y); michael@0: } michael@0: michael@0: michael@0: #endif /* DATATYPES_USE_MACROS */ michael@0: michael@0: michael@0: static inline void michael@0: v128_left_shift2(v128_t *x, int num_bits) { michael@0: int i; michael@0: int word_shift = num_bits >> 5; michael@0: int bit_shift = num_bits & 31; michael@0: michael@0: for (i=0; i < (4-word_shift); i++) { michael@0: x->v32[i] = x->v32[i+word_shift] << bit_shift; michael@0: } michael@0: michael@0: for ( ; i < word_shift; i++) { michael@0: x->v32[i] = 0; michael@0: } michael@0: michael@0: } michael@0: michael@0: void michael@0: v128_right_shift(v128_t *x, int index) { michael@0: const int base_index = index >> 5; michael@0: const int bit_index = index & 31; michael@0: int i, from; michael@0: uint32_t b; michael@0: michael@0: if (index > 127) { michael@0: v128_set_to_zero(x); michael@0: return; michael@0: } michael@0: michael@0: if (bit_index == 0) { michael@0: michael@0: /* copy each word from left size to right side */ michael@0: x->v32[4-1] = x->v32[4-1-base_index]; michael@0: for (i=4-1; i > base_index; i--) michael@0: x->v32[i-1] = x->v32[i-1-base_index]; michael@0: michael@0: } else { michael@0: michael@0: /* set each word to the "or" of the two bit-shifted words */ michael@0: for (i = 4; i > base_index; i--) { michael@0: from = i-1 - base_index; michael@0: b = x->v32[from] << bit_index; michael@0: if (from > 0) michael@0: b |= x->v32[from-1] >> (32-bit_index); michael@0: x->v32[i-1] = b; michael@0: } michael@0: michael@0: } michael@0: michael@0: /* now wrap up the final portion */ michael@0: for (i=0; i < base_index; i++) michael@0: x->v32[i] = 0; michael@0: michael@0: } michael@0: michael@0: void michael@0: v128_left_shift(v128_t *x, int index) { michael@0: int i; michael@0: const int base_index = index >> 5; michael@0: const int bit_index = index & 31; michael@0: michael@0: if (index > 127) { michael@0: v128_set_to_zero(x); michael@0: return; michael@0: } michael@0: michael@0: if (bit_index == 0) { michael@0: for (i=0; i < 4 - base_index; i++) michael@0: x->v32[i] = x->v32[i+base_index]; michael@0: } else { michael@0: for (i=0; i < 4 - base_index - 1; i++) michael@0: x->v32[i] = (x->v32[i+base_index] << bit_index) ^ michael@0: (x->v32[i+base_index+1] >> (32 - bit_index)); michael@0: x->v32[4 - base_index-1] = x->v32[4-1] << bit_index; michael@0: } michael@0: michael@0: /* now wrap up the final portion */ michael@0: for (i = 4 - base_index; i < 4; i++) michael@0: x->v32[i] = 0; michael@0: michael@0: } michael@0: michael@0: michael@0: #if 0 michael@0: void michael@0: v128_add(v128_t *z, v128_t *x, v128_t *y) { michael@0: /* integer addition modulo 2^128 */ michael@0: michael@0: #ifdef WORDS_BIGENDIAN michael@0: uint64_t tmp; michael@0: michael@0: tmp = x->v32[3] + y->v32[3]; michael@0: z->v32[3] = (uint32_t) tmp; michael@0: michael@0: tmp = x->v32[2] + y->v32[2] + (tmp >> 32); michael@0: z->v32[2] = (uint32_t) tmp; michael@0: michael@0: tmp = x->v32[1] + y->v32[1] + (tmp >> 32); michael@0: z->v32[1] = (uint32_t) tmp; michael@0: michael@0: tmp = x->v32[0] + y->v32[0] + (tmp >> 32); michael@0: z->v32[0] = (uint32_t) tmp; michael@0: michael@0: #else /* assume little endian architecture */ michael@0: uint64_t tmp; michael@0: michael@0: tmp = htonl(x->v32[3]) + htonl(y->v32[3]); michael@0: z->v32[3] = ntohl((uint32_t) tmp); michael@0: michael@0: tmp = htonl(x->v32[2]) + htonl(y->v32[2]) + htonl(tmp >> 32); michael@0: z->v32[2] = ntohl((uint32_t) tmp); michael@0: michael@0: tmp = htonl(x->v32[1]) + htonl(y->v32[1]) + htonl(tmp >> 32); michael@0: z->v32[1] = ntohl((uint32_t) tmp); michael@0: michael@0: tmp = htonl(x->v32[0]) + htonl(y->v32[0]) + htonl(tmp >> 32); michael@0: z->v32[0] = ntohl((uint32_t) tmp); michael@0: michael@0: #endif /* WORDS_BIGENDIAN */ michael@0: michael@0: } michael@0: #endif michael@0: michael@0: int michael@0: octet_string_is_eq(uint8_t *a, uint8_t *b, int len) { michael@0: uint8_t *end = b + len; michael@0: while (b < end) michael@0: if (*a++ != *b++) michael@0: return 1; michael@0: return 0; michael@0: } michael@0: michael@0: void michael@0: octet_string_set_to_zero(uint8_t *s, int len) { michael@0: uint8_t *end = s + len; michael@0: michael@0: do { michael@0: *s = 0; michael@0: } while (++s < end); michael@0: michael@0: } michael@0: michael@0: michael@0: /* functions below not yet tested! */ michael@0: michael@0: int michael@0: v32_low_bit(v32_t *w) { michael@0: int value; michael@0: michael@0: value = low_bit[w->v8[0]]; michael@0: if (value != -1) michael@0: return value; michael@0: value = low_bit[w->v8[1]]; michael@0: if (value != -1) michael@0: return value + 8; michael@0: value = low_bit[w->v8[2]]; michael@0: if (value != -1) michael@0: return value + 16; michael@0: value = low_bit[w->v8[3]]; michael@0: if (value == -1) michael@0: return -1; michael@0: return value + 24; michael@0: } michael@0: michael@0: /* high_bit not done yet */ michael@0: michael@0: michael@0: michael@0: michael@0: