1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/netwerk/srtp/src/crypto/include/crypto_math.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,239 @@ 1.4 +/* 1.5 + * math.h 1.6 + * 1.7 + * crypto math operations and data types 1.8 + * 1.9 + * David A. McGrew 1.10 + * Cisco Systems, Inc. 1.11 + */ 1.12 +/* 1.13 + * 1.14 + * Copyright (c) 2001-2006 Cisco Systems, Inc. 1.15 + * All rights reserved. 1.16 + * 1.17 + * Redistribution and use in source and binary forms, with or without 1.18 + * modification, are permitted provided that the following conditions 1.19 + * are met: 1.20 + * 1.21 + * Redistributions of source code must retain the above copyright 1.22 + * notice, this list of conditions and the following disclaimer. 1.23 + * 1.24 + * Redistributions in binary form must reproduce the above 1.25 + * copyright notice, this list of conditions and the following 1.26 + * disclaimer in the documentation and/or other materials provided 1.27 + * with the distribution. 1.28 + * 1.29 + * Neither the name of the Cisco Systems, Inc. nor the names of its 1.30 + * contributors may be used to endorse or promote products derived 1.31 + * from this software without specific prior written permission. 1.32 + * 1.33 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1.34 + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1.35 + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 1.36 + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 1.37 + * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 1.38 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 1.39 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 1.40 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 1.41 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 1.42 + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 1.43 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 1.44 + * OF THE POSSIBILITY OF SUCH DAMAGE. 1.45 + * 1.46 + */ 1.47 + 1.48 +#ifndef MATH_H 1.49 +#define MATH_H 1.50 + 1.51 +#include "datatypes.h" 1.52 + 1.53 +unsigned char 1.54 +v32_weight(v32_t a); 1.55 + 1.56 +unsigned char 1.57 +v32_distance(v32_t x, v32_t y); 1.58 + 1.59 +unsigned int 1.60 +v32_dot_product(v32_t a, v32_t b); 1.61 + 1.62 +char * 1.63 +v16_bit_string(v16_t x); 1.64 + 1.65 +char * 1.66 +v32_bit_string(v32_t x); 1.67 + 1.68 +char * 1.69 +v64_bit_string(const v64_t *x); 1.70 + 1.71 +char * 1.72 +octet_hex_string(uint8_t x); 1.73 + 1.74 +char * 1.75 +v16_hex_string(v16_t x); 1.76 + 1.77 +char * 1.78 +v32_hex_string(v32_t x); 1.79 + 1.80 +char * 1.81 +v64_hex_string(const v64_t *x); 1.82 + 1.83 +int 1.84 +hex_char_to_nibble(uint8_t c); 1.85 + 1.86 +int 1.87 +is_hex_string(char *s); 1.88 + 1.89 +v16_t 1.90 +hex_string_to_v16(char *s); 1.91 + 1.92 +v32_t 1.93 +hex_string_to_v32(char *s); 1.94 + 1.95 +v64_t 1.96 +hex_string_to_v64(char *s); 1.97 + 1.98 +/* the matrix A[] is stored in column format, i.e., A[i] is 1.99 + the ith column of the matrix */ 1.100 + 1.101 +uint8_t 1.102 +A_times_x_plus_b(uint8_t A[8], uint8_t x, uint8_t b); 1.103 + 1.104 +void 1.105 +v16_copy_octet_string(v16_t *x, const uint8_t s[2]); 1.106 + 1.107 +void 1.108 +v32_copy_octet_string(v32_t *x, const uint8_t s[4]); 1.109 + 1.110 +void 1.111 +v64_copy_octet_string(v64_t *x, const uint8_t s[8]); 1.112 + 1.113 +void 1.114 +v128_add(v128_t *z, v128_t *x, v128_t *y); 1.115 + 1.116 +int 1.117 +octet_string_is_eq(uint8_t *a, uint8_t *b, int len); 1.118 + 1.119 +void 1.120 +octet_string_set_to_zero(uint8_t *s, int len); 1.121 + 1.122 + 1.123 + 1.124 +/* 1.125 + * the matrix A[] is stored in column format, i.e., A[i] is the ith 1.126 + * column of the matrix 1.127 +*/ 1.128 +uint8_t 1.129 +A_times_x_plus_b(uint8_t A[8], uint8_t x, uint8_t b); 1.130 + 1.131 + 1.132 +#if 0 1.133 +#if WORDS_BIGENDIAN 1.134 + 1.135 +#define _v128_add(z, x, y) { \ 1.136 + uint64_t tmp; \ 1.137 + \ 1.138 + tmp = x->v32[3] + y->v32[3]; \ 1.139 + z->v32[3] = (uint32_t) tmp; \ 1.140 + \ 1.141 + tmp = x->v32[2] + y->v32[2] + (tmp >> 32); \ 1.142 + z->v32[2] = (uint32_t) tmp; \ 1.143 + \ 1.144 + tmp = x->v32[1] + y->v32[1] + (tmp >> 32); \ 1.145 + z->v32[1] = (uint32_t) tmp; \ 1.146 + \ 1.147 + tmp = x->v32[0] + y->v32[0] + (tmp >> 32); \ 1.148 + z->v32[0] = (uint32_t) tmp; \ 1.149 +} 1.150 + 1.151 +#else /* assume little endian architecture */ 1.152 + 1.153 +#define _v128_add(z, x, y) { \ 1.154 + uint64_t tmp; \ 1.155 + \ 1.156 + tmp = htonl(x->v32[3]) + htonl(y->v32[3]); \ 1.157 + z->v32[3] = ntohl((uint32_t) tmp); \ 1.158 + \ 1.159 + tmp = htonl(x->v32[2]) + htonl(y->v32[2]) \ 1.160 + + htonl(tmp >> 32); \ 1.161 + z->v32[2] = ntohl((uint32_t) tmp); \ 1.162 + \ 1.163 + tmp = htonl(x->v32[1]) + htonl(y->v32[1]) \ 1.164 + + htonl(tmp >> 32); \ 1.165 + z->v32[1] = ntohl((uint32_t) tmp); \ 1.166 + \ 1.167 + tmp = htonl(x->v32[0]) + htonl(y->v32[0]) \ 1.168 + + htonl(tmp >> 32); \ 1.169 + z->v32[0] = ntohl((uint32_t) tmp); \ 1.170 +} 1.171 + 1.172 +#endif /* WORDS_BIGENDIAN */ 1.173 +#endif 1.174 + 1.175 +#ifdef DATATYPES_USE_MACROS /* little functions are really macros */ 1.176 + 1.177 +#define v128_set_to_zero(z) _v128_set_to_zero(z) 1.178 +#define v128_copy(z, x) _v128_copy(z, x) 1.179 +#define v128_xor(z, x, y) _v128_xor(z, x, y) 1.180 +#define v128_and(z, x, y) _v128_and(z, x, y) 1.181 +#define v128_or(z, x, y) _v128_or(z, x, y) 1.182 +#define v128_complement(x) _v128_complement(x) 1.183 +#define v128_is_eq(x, y) _v128_is_eq(x, y) 1.184 +#define v128_xor_eq(x, y) _v128_xor_eq(x, y) 1.185 +#define v128_get_bit(x, i) _v128_get_bit(x, i) 1.186 +#define v128_set_bit(x, i) _v128_set_bit(x, i) 1.187 +#define v128_clear_bit(x, i) _v128_clear_bit(x, i) 1.188 +#define v128_set_bit_to(x, i, y) _v128_set_bit_to(x, i, y) 1.189 + 1.190 +#else 1.191 + 1.192 +void 1.193 +v128_set_to_zero(v128_t *x); 1.194 + 1.195 +int 1.196 +v128_is_eq(const v128_t *x, const v128_t *y); 1.197 + 1.198 +void 1.199 +v128_copy(v128_t *x, const v128_t *y); 1.200 + 1.201 +void 1.202 +v128_xor(v128_t *z, v128_t *x, v128_t *y); 1.203 + 1.204 +void 1.205 +v128_and(v128_t *z, v128_t *x, v128_t *y); 1.206 + 1.207 +void 1.208 +v128_or(v128_t *z, v128_t *x, v128_t *y); 1.209 + 1.210 +void 1.211 +v128_complement(v128_t *x); 1.212 + 1.213 +int 1.214 +v128_get_bit(const v128_t *x, int i); 1.215 + 1.216 +void 1.217 +v128_set_bit(v128_t *x, int i) ; 1.218 + 1.219 +void 1.220 +v128_clear_bit(v128_t *x, int i); 1.221 + 1.222 +void 1.223 +v128_set_bit_to(v128_t *x, int i, int y); 1.224 + 1.225 +#endif /* DATATYPES_USE_MACROS */ 1.226 + 1.227 +/* 1.228 + * octet_string_is_eq(a,b, len) returns 1 if the length len strings a 1.229 + * and b are not equal, returns 0 otherwise 1.230 + */ 1.231 + 1.232 +int 1.233 +octet_string_is_eq(uint8_t *a, uint8_t *b, int len); 1.234 + 1.235 +void 1.236 +octet_string_set_to_zero(uint8_t *s, int len); 1.237 + 1.238 + 1.239 +#endif /* MATH_H */ 1.240 + 1.241 + 1.242 +