michael@0: /* michael@0: * math.h 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: #ifndef MATH_H michael@0: #define MATH_H michael@0: michael@0: #include "datatypes.h" michael@0: michael@0: unsigned char michael@0: v32_weight(v32_t a); michael@0: michael@0: unsigned char michael@0: v32_distance(v32_t x, v32_t y); michael@0: michael@0: unsigned int michael@0: v32_dot_product(v32_t a, v32_t b); michael@0: michael@0: char * michael@0: v16_bit_string(v16_t x); michael@0: michael@0: char * michael@0: v32_bit_string(v32_t x); michael@0: michael@0: char * michael@0: v64_bit_string(const v64_t *x); michael@0: michael@0: char * michael@0: octet_hex_string(uint8_t x); michael@0: michael@0: char * michael@0: v16_hex_string(v16_t x); michael@0: michael@0: char * michael@0: v32_hex_string(v32_t x); michael@0: michael@0: char * michael@0: v64_hex_string(const v64_t *x); michael@0: michael@0: int michael@0: hex_char_to_nibble(uint8_t c); michael@0: michael@0: int michael@0: is_hex_string(char *s); michael@0: michael@0: v16_t michael@0: hex_string_to_v16(char *s); michael@0: michael@0: v32_t michael@0: hex_string_to_v32(char *s); michael@0: michael@0: v64_t michael@0: hex_string_to_v64(char *s); michael@0: michael@0: /* the matrix A[] is stored in column format, i.e., A[i] is michael@0: the ith column of the matrix */ 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: michael@0: void michael@0: v16_copy_octet_string(v16_t *x, const uint8_t s[2]); michael@0: michael@0: void michael@0: v32_copy_octet_string(v32_t *x, const uint8_t s[4]); michael@0: michael@0: void michael@0: v64_copy_octet_string(v64_t *x, const uint8_t s[8]); michael@0: michael@0: void michael@0: v128_add(v128_t *z, v128_t *x, v128_t *y); michael@0: michael@0: int michael@0: octet_string_is_eq(uint8_t *a, uint8_t *b, int len); michael@0: michael@0: void michael@0: octet_string_set_to_zero(uint8_t *s, int len); 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: uint8_t michael@0: A_times_x_plus_b(uint8_t A[8], uint8_t x, uint8_t b); michael@0: michael@0: michael@0: #if 0 michael@0: #if WORDS_BIGENDIAN michael@0: michael@0: #define _v128_add(z, x, y) { \ 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: michael@0: #else /* assume little endian architecture */ michael@0: michael@0: #define _v128_add(z, x, y) { \ 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]) \ michael@0: + 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]) \ michael@0: + 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]) \ michael@0: + htonl(tmp >> 32); \ michael@0: z->v32[0] = ntohl((uint32_t) tmp); \ michael@0: } michael@0: michael@0: #endif /* WORDS_BIGENDIAN */ michael@0: #endif michael@0: michael@0: #ifdef DATATYPES_USE_MACROS /* little functions are really macros */ michael@0: michael@0: #define v128_set_to_zero(z) _v128_set_to_zero(z) michael@0: #define v128_copy(z, x) _v128_copy(z, x) michael@0: #define v128_xor(z, x, y) _v128_xor(z, x, y) michael@0: #define v128_and(z, x, y) _v128_and(z, x, y) michael@0: #define v128_or(z, x, y) _v128_or(z, x, y) michael@0: #define v128_complement(x) _v128_complement(x) michael@0: #define v128_is_eq(x, y) _v128_is_eq(x, y) michael@0: #define v128_xor_eq(x, y) _v128_xor_eq(x, y) michael@0: #define v128_get_bit(x, i) _v128_get_bit(x, i) michael@0: #define v128_set_bit(x, i) _v128_set_bit(x, i) michael@0: #define v128_clear_bit(x, i) _v128_clear_bit(x, i) michael@0: #define v128_set_bit_to(x, i, y) _v128_set_bit_to(x, i, y) michael@0: michael@0: #else michael@0: michael@0: void michael@0: v128_set_to_zero(v128_t *x); michael@0: michael@0: int michael@0: v128_is_eq(const v128_t *x, const v128_t *y); michael@0: michael@0: void michael@0: v128_copy(v128_t *x, const v128_t *y); michael@0: michael@0: void michael@0: v128_xor(v128_t *z, v128_t *x, v128_t *y); michael@0: michael@0: void michael@0: v128_and(v128_t *z, v128_t *x, v128_t *y); michael@0: michael@0: void michael@0: v128_or(v128_t *z, v128_t *x, v128_t *y); michael@0: michael@0: void michael@0: v128_complement(v128_t *x); michael@0: michael@0: int michael@0: v128_get_bit(const v128_t *x, int i); michael@0: michael@0: void michael@0: v128_set_bit(v128_t *x, int i) ; michael@0: michael@0: void michael@0: v128_clear_bit(v128_t *x, int i); michael@0: michael@0: void michael@0: v128_set_bit_to(v128_t *x, int i, int y); michael@0: michael@0: #endif /* DATATYPES_USE_MACROS */ michael@0: michael@0: /* michael@0: * octet_string_is_eq(a,b, len) returns 1 if the length len strings a michael@0: * and b are not equal, returns 0 otherwise michael@0: */ michael@0: michael@0: int michael@0: octet_string_is_eq(uint8_t *a, uint8_t *b, int len); michael@0: michael@0: void michael@0: octet_string_set_to_zero(uint8_t *s, int len); michael@0: michael@0: michael@0: #endif /* MATH_H */ michael@0: michael@0: michael@0: