Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* |
michael@0 | 2 | * math.h |
michael@0 | 3 | * |
michael@0 | 4 | * crypto math operations and data types |
michael@0 | 5 | * |
michael@0 | 6 | * David A. McGrew |
michael@0 | 7 | * Cisco Systems, Inc. |
michael@0 | 8 | */ |
michael@0 | 9 | /* |
michael@0 | 10 | * |
michael@0 | 11 | * Copyright (c) 2001-2006 Cisco Systems, Inc. |
michael@0 | 12 | * All rights reserved. |
michael@0 | 13 | * |
michael@0 | 14 | * Redistribution and use in source and binary forms, with or without |
michael@0 | 15 | * modification, are permitted provided that the following conditions |
michael@0 | 16 | * are met: |
michael@0 | 17 | * |
michael@0 | 18 | * Redistributions of source code must retain the above copyright |
michael@0 | 19 | * notice, this list of conditions and the following disclaimer. |
michael@0 | 20 | * |
michael@0 | 21 | * Redistributions in binary form must reproduce the above |
michael@0 | 22 | * copyright notice, this list of conditions and the following |
michael@0 | 23 | * disclaimer in the documentation and/or other materials provided |
michael@0 | 24 | * with the distribution. |
michael@0 | 25 | * |
michael@0 | 26 | * Neither the name of the Cisco Systems, Inc. nor the names of its |
michael@0 | 27 | * contributors may be used to endorse or promote products derived |
michael@0 | 28 | * from this software without specific prior written permission. |
michael@0 | 29 | * |
michael@0 | 30 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
michael@0 | 31 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
michael@0 | 32 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
michael@0 | 33 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
michael@0 | 34 | * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
michael@0 | 35 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
michael@0 | 36 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
michael@0 | 37 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
michael@0 | 38 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
michael@0 | 39 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
michael@0 | 40 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
michael@0 | 41 | * OF THE POSSIBILITY OF SUCH DAMAGE. |
michael@0 | 42 | * |
michael@0 | 43 | */ |
michael@0 | 44 | |
michael@0 | 45 | #ifndef MATH_H |
michael@0 | 46 | #define MATH_H |
michael@0 | 47 | |
michael@0 | 48 | #include "datatypes.h" |
michael@0 | 49 | |
michael@0 | 50 | unsigned char |
michael@0 | 51 | v32_weight(v32_t a); |
michael@0 | 52 | |
michael@0 | 53 | unsigned char |
michael@0 | 54 | v32_distance(v32_t x, v32_t y); |
michael@0 | 55 | |
michael@0 | 56 | unsigned int |
michael@0 | 57 | v32_dot_product(v32_t a, v32_t b); |
michael@0 | 58 | |
michael@0 | 59 | char * |
michael@0 | 60 | v16_bit_string(v16_t x); |
michael@0 | 61 | |
michael@0 | 62 | char * |
michael@0 | 63 | v32_bit_string(v32_t x); |
michael@0 | 64 | |
michael@0 | 65 | char * |
michael@0 | 66 | v64_bit_string(const v64_t *x); |
michael@0 | 67 | |
michael@0 | 68 | char * |
michael@0 | 69 | octet_hex_string(uint8_t x); |
michael@0 | 70 | |
michael@0 | 71 | char * |
michael@0 | 72 | v16_hex_string(v16_t x); |
michael@0 | 73 | |
michael@0 | 74 | char * |
michael@0 | 75 | v32_hex_string(v32_t x); |
michael@0 | 76 | |
michael@0 | 77 | char * |
michael@0 | 78 | v64_hex_string(const v64_t *x); |
michael@0 | 79 | |
michael@0 | 80 | int |
michael@0 | 81 | hex_char_to_nibble(uint8_t c); |
michael@0 | 82 | |
michael@0 | 83 | int |
michael@0 | 84 | is_hex_string(char *s); |
michael@0 | 85 | |
michael@0 | 86 | v16_t |
michael@0 | 87 | hex_string_to_v16(char *s); |
michael@0 | 88 | |
michael@0 | 89 | v32_t |
michael@0 | 90 | hex_string_to_v32(char *s); |
michael@0 | 91 | |
michael@0 | 92 | v64_t |
michael@0 | 93 | hex_string_to_v64(char *s); |
michael@0 | 94 | |
michael@0 | 95 | /* the matrix A[] is stored in column format, i.e., A[i] is |
michael@0 | 96 | the ith column of the matrix */ |
michael@0 | 97 | |
michael@0 | 98 | uint8_t |
michael@0 | 99 | A_times_x_plus_b(uint8_t A[8], uint8_t x, uint8_t b); |
michael@0 | 100 | |
michael@0 | 101 | void |
michael@0 | 102 | v16_copy_octet_string(v16_t *x, const uint8_t s[2]); |
michael@0 | 103 | |
michael@0 | 104 | void |
michael@0 | 105 | v32_copy_octet_string(v32_t *x, const uint8_t s[4]); |
michael@0 | 106 | |
michael@0 | 107 | void |
michael@0 | 108 | v64_copy_octet_string(v64_t *x, const uint8_t s[8]); |
michael@0 | 109 | |
michael@0 | 110 | void |
michael@0 | 111 | v128_add(v128_t *z, v128_t *x, v128_t *y); |
michael@0 | 112 | |
michael@0 | 113 | int |
michael@0 | 114 | octet_string_is_eq(uint8_t *a, uint8_t *b, int len); |
michael@0 | 115 | |
michael@0 | 116 | void |
michael@0 | 117 | octet_string_set_to_zero(uint8_t *s, int len); |
michael@0 | 118 | |
michael@0 | 119 | |
michael@0 | 120 | |
michael@0 | 121 | /* |
michael@0 | 122 | * the matrix A[] is stored in column format, i.e., A[i] is the ith |
michael@0 | 123 | * column of the matrix |
michael@0 | 124 | */ |
michael@0 | 125 | uint8_t |
michael@0 | 126 | A_times_x_plus_b(uint8_t A[8], uint8_t x, uint8_t b); |
michael@0 | 127 | |
michael@0 | 128 | |
michael@0 | 129 | #if 0 |
michael@0 | 130 | #if WORDS_BIGENDIAN |
michael@0 | 131 | |
michael@0 | 132 | #define _v128_add(z, x, y) { \ |
michael@0 | 133 | uint64_t tmp; \ |
michael@0 | 134 | \ |
michael@0 | 135 | tmp = x->v32[3] + y->v32[3]; \ |
michael@0 | 136 | z->v32[3] = (uint32_t) tmp; \ |
michael@0 | 137 | \ |
michael@0 | 138 | tmp = x->v32[2] + y->v32[2] + (tmp >> 32); \ |
michael@0 | 139 | z->v32[2] = (uint32_t) tmp; \ |
michael@0 | 140 | \ |
michael@0 | 141 | tmp = x->v32[1] + y->v32[1] + (tmp >> 32); \ |
michael@0 | 142 | z->v32[1] = (uint32_t) tmp; \ |
michael@0 | 143 | \ |
michael@0 | 144 | tmp = x->v32[0] + y->v32[0] + (tmp >> 32); \ |
michael@0 | 145 | z->v32[0] = (uint32_t) tmp; \ |
michael@0 | 146 | } |
michael@0 | 147 | |
michael@0 | 148 | #else /* assume little endian architecture */ |
michael@0 | 149 | |
michael@0 | 150 | #define _v128_add(z, x, y) { \ |
michael@0 | 151 | uint64_t tmp; \ |
michael@0 | 152 | \ |
michael@0 | 153 | tmp = htonl(x->v32[3]) + htonl(y->v32[3]); \ |
michael@0 | 154 | z->v32[3] = ntohl((uint32_t) tmp); \ |
michael@0 | 155 | \ |
michael@0 | 156 | tmp = htonl(x->v32[2]) + htonl(y->v32[2]) \ |
michael@0 | 157 | + htonl(tmp >> 32); \ |
michael@0 | 158 | z->v32[2] = ntohl((uint32_t) tmp); \ |
michael@0 | 159 | \ |
michael@0 | 160 | tmp = htonl(x->v32[1]) + htonl(y->v32[1]) \ |
michael@0 | 161 | + htonl(tmp >> 32); \ |
michael@0 | 162 | z->v32[1] = ntohl((uint32_t) tmp); \ |
michael@0 | 163 | \ |
michael@0 | 164 | tmp = htonl(x->v32[0]) + htonl(y->v32[0]) \ |
michael@0 | 165 | + htonl(tmp >> 32); \ |
michael@0 | 166 | z->v32[0] = ntohl((uint32_t) tmp); \ |
michael@0 | 167 | } |
michael@0 | 168 | |
michael@0 | 169 | #endif /* WORDS_BIGENDIAN */ |
michael@0 | 170 | #endif |
michael@0 | 171 | |
michael@0 | 172 | #ifdef DATATYPES_USE_MACROS /* little functions are really macros */ |
michael@0 | 173 | |
michael@0 | 174 | #define v128_set_to_zero(z) _v128_set_to_zero(z) |
michael@0 | 175 | #define v128_copy(z, x) _v128_copy(z, x) |
michael@0 | 176 | #define v128_xor(z, x, y) _v128_xor(z, x, y) |
michael@0 | 177 | #define v128_and(z, x, y) _v128_and(z, x, y) |
michael@0 | 178 | #define v128_or(z, x, y) _v128_or(z, x, y) |
michael@0 | 179 | #define v128_complement(x) _v128_complement(x) |
michael@0 | 180 | #define v128_is_eq(x, y) _v128_is_eq(x, y) |
michael@0 | 181 | #define v128_xor_eq(x, y) _v128_xor_eq(x, y) |
michael@0 | 182 | #define v128_get_bit(x, i) _v128_get_bit(x, i) |
michael@0 | 183 | #define v128_set_bit(x, i) _v128_set_bit(x, i) |
michael@0 | 184 | #define v128_clear_bit(x, i) _v128_clear_bit(x, i) |
michael@0 | 185 | #define v128_set_bit_to(x, i, y) _v128_set_bit_to(x, i, y) |
michael@0 | 186 | |
michael@0 | 187 | #else |
michael@0 | 188 | |
michael@0 | 189 | void |
michael@0 | 190 | v128_set_to_zero(v128_t *x); |
michael@0 | 191 | |
michael@0 | 192 | int |
michael@0 | 193 | v128_is_eq(const v128_t *x, const v128_t *y); |
michael@0 | 194 | |
michael@0 | 195 | void |
michael@0 | 196 | v128_copy(v128_t *x, const v128_t *y); |
michael@0 | 197 | |
michael@0 | 198 | void |
michael@0 | 199 | v128_xor(v128_t *z, v128_t *x, v128_t *y); |
michael@0 | 200 | |
michael@0 | 201 | void |
michael@0 | 202 | v128_and(v128_t *z, v128_t *x, v128_t *y); |
michael@0 | 203 | |
michael@0 | 204 | void |
michael@0 | 205 | v128_or(v128_t *z, v128_t *x, v128_t *y); |
michael@0 | 206 | |
michael@0 | 207 | void |
michael@0 | 208 | v128_complement(v128_t *x); |
michael@0 | 209 | |
michael@0 | 210 | int |
michael@0 | 211 | v128_get_bit(const v128_t *x, int i); |
michael@0 | 212 | |
michael@0 | 213 | void |
michael@0 | 214 | v128_set_bit(v128_t *x, int i) ; |
michael@0 | 215 | |
michael@0 | 216 | void |
michael@0 | 217 | v128_clear_bit(v128_t *x, int i); |
michael@0 | 218 | |
michael@0 | 219 | void |
michael@0 | 220 | v128_set_bit_to(v128_t *x, int i, int y); |
michael@0 | 221 | |
michael@0 | 222 | #endif /* DATATYPES_USE_MACROS */ |
michael@0 | 223 | |
michael@0 | 224 | /* |
michael@0 | 225 | * octet_string_is_eq(a,b, len) returns 1 if the length len strings a |
michael@0 | 226 | * and b are not equal, returns 0 otherwise |
michael@0 | 227 | */ |
michael@0 | 228 | |
michael@0 | 229 | int |
michael@0 | 230 | octet_string_is_eq(uint8_t *a, uint8_t *b, int len); |
michael@0 | 231 | |
michael@0 | 232 | void |
michael@0 | 233 | octet_string_set_to_zero(uint8_t *s, int len); |
michael@0 | 234 | |
michael@0 | 235 | |
michael@0 | 236 | #endif /* MATH_H */ |
michael@0 | 237 | |
michael@0 | 238 | |
michael@0 | 239 |