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.c |
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 | #include "crypto_math.h" |
michael@0 | 46 | |
michael@0 | 47 | int |
michael@0 | 48 | octet_weight[256] = { |
michael@0 | 49 | 0, 1, 1, 2, 1, 2, 2, 3, |
michael@0 | 50 | 1, 2, 2, 3, 2, 3, 3, 4, |
michael@0 | 51 | 1, 2, 2, 3, 2, 3, 3, 4, |
michael@0 | 52 | 2, 3, 3, 4, 3, 4, 4, 5, |
michael@0 | 53 | 1, 2, 2, 3, 2, 3, 3, 4, |
michael@0 | 54 | 2, 3, 3, 4, 3, 4, 4, 5, |
michael@0 | 55 | 2, 3, 3, 4, 3, 4, 4, 5, |
michael@0 | 56 | 3, 4, 4, 5, 4, 5, 5, 6, |
michael@0 | 57 | 1, 2, 2, 3, 2, 3, 3, 4, |
michael@0 | 58 | 2, 3, 3, 4, 3, 4, 4, 5, |
michael@0 | 59 | 2, 3, 3, 4, 3, 4, 4, 5, |
michael@0 | 60 | 3, 4, 4, 5, 4, 5, 5, 6, |
michael@0 | 61 | 2, 3, 3, 4, 3, 4, 4, 5, |
michael@0 | 62 | 3, 4, 4, 5, 4, 5, 5, 6, |
michael@0 | 63 | 3, 4, 4, 5, 4, 5, 5, 6, |
michael@0 | 64 | 4, 5, 5, 6, 5, 6, 6, 7, |
michael@0 | 65 | 1, 2, 2, 3, 2, 3, 3, 4, |
michael@0 | 66 | 2, 3, 3, 4, 3, 4, 4, 5, |
michael@0 | 67 | 2, 3, 3, 4, 3, 4, 4, 5, |
michael@0 | 68 | 3, 4, 4, 5, 4, 5, 5, 6, |
michael@0 | 69 | 2, 3, 3, 4, 3, 4, 4, 5, |
michael@0 | 70 | 3, 4, 4, 5, 4, 5, 5, 6, |
michael@0 | 71 | 3, 4, 4, 5, 4, 5, 5, 6, |
michael@0 | 72 | 4, 5, 5, 6, 5, 6, 6, 7, |
michael@0 | 73 | 2, 3, 3, 4, 3, 4, 4, 5, |
michael@0 | 74 | 3, 4, 4, 5, 4, 5, 5, 6, |
michael@0 | 75 | 3, 4, 4, 5, 4, 5, 5, 6, |
michael@0 | 76 | 4, 5, 5, 6, 5, 6, 6, 7, |
michael@0 | 77 | 3, 4, 4, 5, 4, 5, 5, 6, |
michael@0 | 78 | 4, 5, 5, 6, 5, 6, 6, 7, |
michael@0 | 79 | 4, 5, 5, 6, 5, 6, 6, 7, |
michael@0 | 80 | 5, 6, 6, 7, 6, 7, 7, 8 |
michael@0 | 81 | }; |
michael@0 | 82 | |
michael@0 | 83 | int |
michael@0 | 84 | low_bit[256] = { |
michael@0 | 85 | -1, 0, 1, 0, 2, 0, 1, 0, |
michael@0 | 86 | 3, 0, 1, 0, 2, 0, 1, 0, |
michael@0 | 87 | 4, 0, 1, 0, 2, 0, 1, 0, |
michael@0 | 88 | 3, 0, 1, 0, 2, 0, 1, 0, |
michael@0 | 89 | 5, 0, 1, 0, 2, 0, 1, 0, |
michael@0 | 90 | 3, 0, 1, 0, 2, 0, 1, 0, |
michael@0 | 91 | 4, 0, 1, 0, 2, 0, 1, 0, |
michael@0 | 92 | 3, 0, 1, 0, 2, 0, 1, 0, |
michael@0 | 93 | 6, 0, 1, 0, 2, 0, 1, 0, |
michael@0 | 94 | 3, 0, 1, 0, 2, 0, 1, 0, |
michael@0 | 95 | 4, 0, 1, 0, 2, 0, 1, 0, |
michael@0 | 96 | 3, 0, 1, 0, 2, 0, 1, 0, |
michael@0 | 97 | 5, 0, 1, 0, 2, 0, 1, 0, |
michael@0 | 98 | 3, 0, 1, 0, 2, 0, 1, 0, |
michael@0 | 99 | 4, 0, 1, 0, 2, 0, 1, 0, |
michael@0 | 100 | 3, 0, 1, 0, 2, 0, 1, 0, |
michael@0 | 101 | 7, 0, 1, 0, 2, 0, 1, 0, |
michael@0 | 102 | 3, 0, 1, 0, 2, 0, 1, 0, |
michael@0 | 103 | 4, 0, 1, 0, 2, 0, 1, 0, |
michael@0 | 104 | 3, 0, 1, 0, 2, 0, 1, 0, |
michael@0 | 105 | 5, 0, 1, 0, 2, 0, 1, 0, |
michael@0 | 106 | 3, 0, 1, 0, 2, 0, 1, 0, |
michael@0 | 107 | 4, 0, 1, 0, 2, 0, 1, 0, |
michael@0 | 108 | 3, 0, 1, 0, 2, 0, 1, 0, |
michael@0 | 109 | 6, 0, 1, 0, 2, 0, 1, 0, |
michael@0 | 110 | 3, 0, 1, 0, 2, 0, 1, 0, |
michael@0 | 111 | 4, 0, 1, 0, 2, 0, 1, 0, |
michael@0 | 112 | 3, 0, 1, 0, 2, 0, 1, 0, |
michael@0 | 113 | 5, 0, 1, 0, 2, 0, 1, 0, |
michael@0 | 114 | 3, 0, 1, 0, 2, 0, 1, 0, |
michael@0 | 115 | 4, 0, 1, 0, 2, 0, 1, 0, |
michael@0 | 116 | 3, 0, 1, 0, 2, 0, 1, 0 |
michael@0 | 117 | }; |
michael@0 | 118 | |
michael@0 | 119 | |
michael@0 | 120 | int |
michael@0 | 121 | high_bit[256] = { |
michael@0 | 122 | -1, 0, 1, 1, 2, 2, 2, 2, |
michael@0 | 123 | 3, 3, 3, 3, 3, 3, 3, 3, |
michael@0 | 124 | 4, 4, 4, 4, 4, 4, 4, 4, |
michael@0 | 125 | 4, 4, 4, 4, 4, 4, 4, 4, |
michael@0 | 126 | 5, 5, 5, 5, 5, 5, 5, 5, |
michael@0 | 127 | 5, 5, 5, 5, 5, 5, 5, 5, |
michael@0 | 128 | 5, 5, 5, 5, 5, 5, 5, 5, |
michael@0 | 129 | 5, 5, 5, 5, 5, 5, 5, 5, |
michael@0 | 130 | 6, 6, 6, 6, 6, 6, 6, 6, |
michael@0 | 131 | 6, 6, 6, 6, 6, 6, 6, 6, |
michael@0 | 132 | 6, 6, 6, 6, 6, 6, 6, 6, |
michael@0 | 133 | 6, 6, 6, 6, 6, 6, 6, 6, |
michael@0 | 134 | 6, 6, 6, 6, 6, 6, 6, 6, |
michael@0 | 135 | 6, 6, 6, 6, 6, 6, 6, 6, |
michael@0 | 136 | 6, 6, 6, 6, 6, 6, 6, 6, |
michael@0 | 137 | 6, 6, 6, 6, 6, 6, 6, 6, |
michael@0 | 138 | 7, 7, 7, 7, 7, 7, 7, 7, |
michael@0 | 139 | 7, 7, 7, 7, 7, 7, 7, 7, |
michael@0 | 140 | 7, 7, 7, 7, 7, 7, 7, 7, |
michael@0 | 141 | 7, 7, 7, 7, 7, 7, 7, 7, |
michael@0 | 142 | 7, 7, 7, 7, 7, 7, 7, 7, |
michael@0 | 143 | 7, 7, 7, 7, 7, 7, 7, 7, |
michael@0 | 144 | 7, 7, 7, 7, 7, 7, 7, 7, |
michael@0 | 145 | 7, 7, 7, 7, 7, 7, 7, 7, |
michael@0 | 146 | 7, 7, 7, 7, 7, 7, 7, 7, |
michael@0 | 147 | 7, 7, 7, 7, 7, 7, 7, 7, |
michael@0 | 148 | 7, 7, 7, 7, 7, 7, 7, 7, |
michael@0 | 149 | 7, 7, 7, 7, 7, 7, 7, 7, |
michael@0 | 150 | 7, 7, 7, 7, 7, 7, 7, 7, |
michael@0 | 151 | 7, 7, 7, 7, 7, 7, 7, 7, |
michael@0 | 152 | 7, 7, 7, 7, 7, 7, 7, 7, |
michael@0 | 153 | 7, 7, 7, 7, 7, 7, 7, 7 |
michael@0 | 154 | }; |
michael@0 | 155 | |
michael@0 | 156 | int |
michael@0 | 157 | octet_get_weight(uint8_t octet) { |
michael@0 | 158 | extern int octet_weight[256]; |
michael@0 | 159 | |
michael@0 | 160 | return octet_weight[octet]; |
michael@0 | 161 | } |
michael@0 | 162 | |
michael@0 | 163 | unsigned char |
michael@0 | 164 | v32_weight(v32_t a) { |
michael@0 | 165 | unsigned int wt = 0; |
michael@0 | 166 | |
michael@0 | 167 | wt += octet_weight[a.v8[0]]; /* note: endian-ness makes no difference */ |
michael@0 | 168 | wt += octet_weight[a.v8[1]]; |
michael@0 | 169 | wt += octet_weight[a.v8[2]]; |
michael@0 | 170 | wt += octet_weight[a.v8[3]]; |
michael@0 | 171 | |
michael@0 | 172 | return wt; |
michael@0 | 173 | } |
michael@0 | 174 | |
michael@0 | 175 | unsigned char |
michael@0 | 176 | v32_distance(v32_t x, v32_t y) { |
michael@0 | 177 | x.value ^= y.value; |
michael@0 | 178 | return v32_weight(x); |
michael@0 | 179 | } |
michael@0 | 180 | |
michael@0 | 181 | unsigned int |
michael@0 | 182 | v32_dot_product(v32_t a, v32_t b) { |
michael@0 | 183 | a.value &= b.value; |
michael@0 | 184 | return v32_weight(a) & 1; |
michael@0 | 185 | } |
michael@0 | 186 | |
michael@0 | 187 | /* |
michael@0 | 188 | * _bit_string returns a NULL-terminated character string suitable for |
michael@0 | 189 | * printing |
michael@0 | 190 | */ |
michael@0 | 191 | |
michael@0 | 192 | #define MAX_STRING_LENGTH 1024 |
michael@0 | 193 | |
michael@0 | 194 | char bit_string[MAX_STRING_LENGTH]; |
michael@0 | 195 | |
michael@0 | 196 | char * |
michael@0 | 197 | octet_bit_string(uint8_t x) { |
michael@0 | 198 | int mask, index; |
michael@0 | 199 | |
michael@0 | 200 | for (mask = 1, index = 0; mask < 256; mask <<= 1) |
michael@0 | 201 | if ((x & mask) == 0) |
michael@0 | 202 | bit_string[index++] = '0'; |
michael@0 | 203 | else |
michael@0 | 204 | bit_string[index++] = '1'; |
michael@0 | 205 | |
michael@0 | 206 | bit_string[index++] = 0; /* NULL terminate string */ |
michael@0 | 207 | |
michael@0 | 208 | return bit_string; |
michael@0 | 209 | } |
michael@0 | 210 | |
michael@0 | 211 | char * |
michael@0 | 212 | v16_bit_string(v16_t x) { |
michael@0 | 213 | int i, mask, index; |
michael@0 | 214 | |
michael@0 | 215 | for (i = index = 0; i < 2; i++) { |
michael@0 | 216 | for (mask = 1; mask < 256; mask <<= 1) |
michael@0 | 217 | if ((x.v8[i] & mask) == 0) |
michael@0 | 218 | bit_string[index++] = '0'; |
michael@0 | 219 | else |
michael@0 | 220 | bit_string[index++] = '1'; |
michael@0 | 221 | } |
michael@0 | 222 | bit_string[index++] = 0; /* NULL terminate string */ |
michael@0 | 223 | return bit_string; |
michael@0 | 224 | } |
michael@0 | 225 | |
michael@0 | 226 | char * |
michael@0 | 227 | v32_bit_string(v32_t x) { |
michael@0 | 228 | int i, mask, index; |
michael@0 | 229 | |
michael@0 | 230 | for (i = index = 0; i < 4; i++) { |
michael@0 | 231 | for (mask = 128; mask > 0; mask >>= 1) |
michael@0 | 232 | if ((x.v8[i] & mask) == 0) |
michael@0 | 233 | bit_string[index++] = '0'; |
michael@0 | 234 | else |
michael@0 | 235 | bit_string[index++] = '1'; |
michael@0 | 236 | } |
michael@0 | 237 | bit_string[index++] = 0; /* NULL terminate string */ |
michael@0 | 238 | return bit_string; |
michael@0 | 239 | } |
michael@0 | 240 | |
michael@0 | 241 | char * |
michael@0 | 242 | v64_bit_string(const v64_t *x) { |
michael@0 | 243 | int i, mask, index; |
michael@0 | 244 | |
michael@0 | 245 | for (i = index = 0; i < 8; i++) { |
michael@0 | 246 | for (mask = 1; mask < 256; mask <<= 1) |
michael@0 | 247 | if ((x->v8[i] & mask) == 0) |
michael@0 | 248 | bit_string[index++] = '0'; |
michael@0 | 249 | else |
michael@0 | 250 | bit_string[index++] = '1'; |
michael@0 | 251 | } |
michael@0 | 252 | bit_string[index++] = 0; /* NULL terminate string */ |
michael@0 | 253 | return bit_string; |
michael@0 | 254 | } |
michael@0 | 255 | |
michael@0 | 256 | char * |
michael@0 | 257 | v128_bit_string(v128_t *x) { |
michael@0 | 258 | int j, index; |
michael@0 | 259 | uint32_t mask; |
michael@0 | 260 | |
michael@0 | 261 | for (j=index=0; j < 4; j++) { |
michael@0 | 262 | for (mask=0x80000000; mask > 0; mask >>= 1) { |
michael@0 | 263 | if (x->v32[j] & mask) |
michael@0 | 264 | bit_string[index] = '1'; |
michael@0 | 265 | else |
michael@0 | 266 | bit_string[index] = '0'; |
michael@0 | 267 | ++index; |
michael@0 | 268 | } |
michael@0 | 269 | } |
michael@0 | 270 | bit_string[128] = 0; /* null terminate string */ |
michael@0 | 271 | |
michael@0 | 272 | return bit_string; |
michael@0 | 273 | } |
michael@0 | 274 | |
michael@0 | 275 | uint8_t |
michael@0 | 276 | nibble_to_hex_char(uint8_t nibble) { |
michael@0 | 277 | char buf[16] = {'0', '1', '2', '3', '4', '5', '6', '7', |
michael@0 | 278 | '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; |
michael@0 | 279 | return buf[nibble & 0xF]; |
michael@0 | 280 | } |
michael@0 | 281 | |
michael@0 | 282 | char * |
michael@0 | 283 | octet_hex_string(uint8_t x) { |
michael@0 | 284 | |
michael@0 | 285 | bit_string[0] = nibble_to_hex_char(x >> 4); |
michael@0 | 286 | bit_string[1] = nibble_to_hex_char(x & 0xF); |
michael@0 | 287 | |
michael@0 | 288 | bit_string[2] = 0; /* null terminate string */ |
michael@0 | 289 | return bit_string; |
michael@0 | 290 | } |
michael@0 | 291 | |
michael@0 | 292 | char * |
michael@0 | 293 | octet_string_hex_string(const void *str, int length) { |
michael@0 | 294 | const uint8_t *s = str; |
michael@0 | 295 | int i; |
michael@0 | 296 | |
michael@0 | 297 | /* double length, since one octet takes two hex characters */ |
michael@0 | 298 | length *= 2; |
michael@0 | 299 | |
michael@0 | 300 | /* truncate string if it would be too long */ |
michael@0 | 301 | if (length > MAX_STRING_LENGTH) |
michael@0 | 302 | length = MAX_STRING_LENGTH-1; |
michael@0 | 303 | |
michael@0 | 304 | for (i=0; i < length; i+=2) { |
michael@0 | 305 | bit_string[i] = nibble_to_hex_char(*s >> 4); |
michael@0 | 306 | bit_string[i+1] = nibble_to_hex_char(*s++ & 0xF); |
michael@0 | 307 | } |
michael@0 | 308 | bit_string[i] = 0; /* null terminate string */ |
michael@0 | 309 | return bit_string; |
michael@0 | 310 | } |
michael@0 | 311 | |
michael@0 | 312 | char * |
michael@0 | 313 | v16_hex_string(v16_t x) { |
michael@0 | 314 | int i, j; |
michael@0 | 315 | |
michael@0 | 316 | for (i=j=0; i < 2; i++) { |
michael@0 | 317 | bit_string[j++] = nibble_to_hex_char(x.v8[i] >> 4); |
michael@0 | 318 | bit_string[j++] = nibble_to_hex_char(x.v8[i] & 0xF); |
michael@0 | 319 | } |
michael@0 | 320 | |
michael@0 | 321 | bit_string[j] = 0; /* null terminate string */ |
michael@0 | 322 | return bit_string; |
michael@0 | 323 | } |
michael@0 | 324 | |
michael@0 | 325 | char * |
michael@0 | 326 | v32_hex_string(v32_t x) { |
michael@0 | 327 | int i, j; |
michael@0 | 328 | |
michael@0 | 329 | for (i=j=0; i < 4; i++) { |
michael@0 | 330 | bit_string[j++] = nibble_to_hex_char(x.v8[i] >> 4); |
michael@0 | 331 | bit_string[j++] = nibble_to_hex_char(x.v8[i] & 0xF); |
michael@0 | 332 | } |
michael@0 | 333 | |
michael@0 | 334 | bit_string[j] = 0; /* null terminate string */ |
michael@0 | 335 | return bit_string; |
michael@0 | 336 | } |
michael@0 | 337 | |
michael@0 | 338 | char * |
michael@0 | 339 | v64_hex_string(const v64_t *x) { |
michael@0 | 340 | int i, j; |
michael@0 | 341 | |
michael@0 | 342 | for (i=j=0; i < 8; i++) { |
michael@0 | 343 | bit_string[j++] = nibble_to_hex_char(x->v8[i] >> 4); |
michael@0 | 344 | bit_string[j++] = nibble_to_hex_char(x->v8[i] & 0xF); |
michael@0 | 345 | } |
michael@0 | 346 | |
michael@0 | 347 | bit_string[j] = 0; /* null terminate string */ |
michael@0 | 348 | return bit_string; |
michael@0 | 349 | } |
michael@0 | 350 | |
michael@0 | 351 | char * |
michael@0 | 352 | v128_hex_string(v128_t *x) { |
michael@0 | 353 | int i, j; |
michael@0 | 354 | |
michael@0 | 355 | for (i=j=0; i < 16; i++) { |
michael@0 | 356 | bit_string[j++] = nibble_to_hex_char(x->v8[i] >> 4); |
michael@0 | 357 | bit_string[j++] = nibble_to_hex_char(x->v8[i] & 0xF); |
michael@0 | 358 | } |
michael@0 | 359 | |
michael@0 | 360 | bit_string[j] = 0; /* null terminate string */ |
michael@0 | 361 | return bit_string; |
michael@0 | 362 | } |
michael@0 | 363 | |
michael@0 | 364 | char * |
michael@0 | 365 | char_to_hex_string(char *x, int num_char) { |
michael@0 | 366 | int i, j; |
michael@0 | 367 | |
michael@0 | 368 | if (num_char >= 16) |
michael@0 | 369 | num_char = 16; |
michael@0 | 370 | for (i=j=0; i < num_char; i++) { |
michael@0 | 371 | bit_string[j++] = nibble_to_hex_char(x[i] >> 4); |
michael@0 | 372 | bit_string[j++] = nibble_to_hex_char(x[i] & 0xF); |
michael@0 | 373 | } |
michael@0 | 374 | |
michael@0 | 375 | bit_string[j] = 0; /* null terminate string */ |
michael@0 | 376 | return bit_string; |
michael@0 | 377 | } |
michael@0 | 378 | |
michael@0 | 379 | int |
michael@0 | 380 | hex_char_to_nibble(uint8_t c) { |
michael@0 | 381 | switch(c) { |
michael@0 | 382 | case ('0'): return 0x0; |
michael@0 | 383 | case ('1'): return 0x1; |
michael@0 | 384 | case ('2'): return 0x2; |
michael@0 | 385 | case ('3'): return 0x3; |
michael@0 | 386 | case ('4'): return 0x4; |
michael@0 | 387 | case ('5'): return 0x5; |
michael@0 | 388 | case ('6'): return 0x6; |
michael@0 | 389 | case ('7'): return 0x7; |
michael@0 | 390 | case ('8'): return 0x8; |
michael@0 | 391 | case ('9'): return 0x9; |
michael@0 | 392 | case ('a'): return 0xa; |
michael@0 | 393 | case ('A'): return 0xa; |
michael@0 | 394 | case ('b'): return 0xb; |
michael@0 | 395 | case ('B'): return 0xb; |
michael@0 | 396 | case ('c'): return 0xc; |
michael@0 | 397 | case ('C'): return 0xc; |
michael@0 | 398 | case ('d'): return 0xd; |
michael@0 | 399 | case ('D'): return 0xd; |
michael@0 | 400 | case ('e'): return 0xe; |
michael@0 | 401 | case ('E'): return 0xe; |
michael@0 | 402 | case ('f'): return 0xf; |
michael@0 | 403 | case ('F'): return 0xf; |
michael@0 | 404 | default: return -1; /* this flags an error */ |
michael@0 | 405 | } |
michael@0 | 406 | /* NOTREACHED */ |
michael@0 | 407 | return -1; /* this keeps compilers from complaining */ |
michael@0 | 408 | } |
michael@0 | 409 | |
michael@0 | 410 | int |
michael@0 | 411 | is_hex_string(char *s) { |
michael@0 | 412 | while(*s != 0) |
michael@0 | 413 | if (hex_char_to_nibble(*s++) == -1) |
michael@0 | 414 | return 0; |
michael@0 | 415 | return 1; |
michael@0 | 416 | } |
michael@0 | 417 | |
michael@0 | 418 | uint8_t |
michael@0 | 419 | hex_string_to_octet(char *s) { |
michael@0 | 420 | uint8_t x; |
michael@0 | 421 | |
michael@0 | 422 | x = (hex_char_to_nibble(s[0]) << 4) |
michael@0 | 423 | | hex_char_to_nibble(s[1] & 0xFF); |
michael@0 | 424 | |
michael@0 | 425 | return x; |
michael@0 | 426 | } |
michael@0 | 427 | |
michael@0 | 428 | /* |
michael@0 | 429 | * hex_string_to_octet_string converts a hexadecimal string |
michael@0 | 430 | * of length 2 * len to a raw octet string of length len |
michael@0 | 431 | */ |
michael@0 | 432 | |
michael@0 | 433 | int |
michael@0 | 434 | hex_string_to_octet_string(char *raw, char *hex, int len) { |
michael@0 | 435 | uint8_t x; |
michael@0 | 436 | int tmp; |
michael@0 | 437 | int hex_len; |
michael@0 | 438 | |
michael@0 | 439 | hex_len = 0; |
michael@0 | 440 | while (hex_len < len) { |
michael@0 | 441 | tmp = hex_char_to_nibble(hex[0]); |
michael@0 | 442 | if (tmp == -1) |
michael@0 | 443 | return hex_len; |
michael@0 | 444 | x = (tmp << 4); |
michael@0 | 445 | hex_len++; |
michael@0 | 446 | tmp = hex_char_to_nibble(hex[1]); |
michael@0 | 447 | if (tmp == -1) |
michael@0 | 448 | return hex_len; |
michael@0 | 449 | x |= (tmp & 0xff); |
michael@0 | 450 | hex_len++; |
michael@0 | 451 | *raw++ = x; |
michael@0 | 452 | hex += 2; |
michael@0 | 453 | } |
michael@0 | 454 | return hex_len; |
michael@0 | 455 | } |
michael@0 | 456 | |
michael@0 | 457 | v16_t |
michael@0 | 458 | hex_string_to_v16(char *s) { |
michael@0 | 459 | v16_t x; |
michael@0 | 460 | int i, j; |
michael@0 | 461 | |
michael@0 | 462 | for (i=j=0; i < 4; i += 2, j++) { |
michael@0 | 463 | x.v8[j] = (hex_char_to_nibble(s[i]) << 4) |
michael@0 | 464 | | hex_char_to_nibble(s[i+1] & 0xFF); |
michael@0 | 465 | } |
michael@0 | 466 | return x; |
michael@0 | 467 | } |
michael@0 | 468 | |
michael@0 | 469 | v32_t |
michael@0 | 470 | hex_string_to_v32(char *s) { |
michael@0 | 471 | v32_t x; |
michael@0 | 472 | int i, j; |
michael@0 | 473 | |
michael@0 | 474 | for (i=j=0; i < 8; i += 2, j++) { |
michael@0 | 475 | x.v8[j] = (hex_char_to_nibble(s[i]) << 4) |
michael@0 | 476 | | hex_char_to_nibble(s[i+1] & 0xFF); |
michael@0 | 477 | } |
michael@0 | 478 | return x; |
michael@0 | 479 | } |
michael@0 | 480 | |
michael@0 | 481 | v64_t |
michael@0 | 482 | hex_string_to_v64(char *s) { |
michael@0 | 483 | v64_t x; |
michael@0 | 484 | int i, j; |
michael@0 | 485 | |
michael@0 | 486 | for (i=j=0; i < 16; i += 2, j++) { |
michael@0 | 487 | x.v8[j] = (hex_char_to_nibble(s[i]) << 4) |
michael@0 | 488 | | hex_char_to_nibble(s[i+1] & 0xFF); |
michael@0 | 489 | } |
michael@0 | 490 | return x; |
michael@0 | 491 | } |
michael@0 | 492 | |
michael@0 | 493 | v128_t |
michael@0 | 494 | hex_string_to_v128(char *s) { |
michael@0 | 495 | v128_t x; |
michael@0 | 496 | int i, j; |
michael@0 | 497 | |
michael@0 | 498 | for (i=j=0; i < 32; i += 2, j++) { |
michael@0 | 499 | x.v8[j] = (hex_char_to_nibble(s[i]) << 4) |
michael@0 | 500 | | hex_char_to_nibble(s[i+1] & 0xFF); |
michael@0 | 501 | } |
michael@0 | 502 | return x; |
michael@0 | 503 | } |
michael@0 | 504 | |
michael@0 | 505 | |
michael@0 | 506 | |
michael@0 | 507 | /* |
michael@0 | 508 | * the matrix A[] is stored in column format, i.e., A[i] is the ith |
michael@0 | 509 | * column of the matrix |
michael@0 | 510 | */ |
michael@0 | 511 | |
michael@0 | 512 | uint8_t |
michael@0 | 513 | A_times_x_plus_b(uint8_t A[8], uint8_t x, uint8_t b) { |
michael@0 | 514 | int index = 0; |
michael@0 | 515 | unsigned mask; |
michael@0 | 516 | |
michael@0 | 517 | for (mask=1; mask < 256; mask *= 2) { |
michael@0 | 518 | if (x & mask) |
michael@0 | 519 | b^= A[index]; |
michael@0 | 520 | ++index; |
michael@0 | 521 | } |
michael@0 | 522 | |
michael@0 | 523 | return b; |
michael@0 | 524 | } |
michael@0 | 525 | |
michael@0 | 526 | void |
michael@0 | 527 | v16_copy_octet_string(v16_t *x, const uint8_t s[2]) { |
michael@0 | 528 | x->v8[0] = s[0]; |
michael@0 | 529 | x->v8[1] = s[1]; |
michael@0 | 530 | } |
michael@0 | 531 | |
michael@0 | 532 | void |
michael@0 | 533 | v32_copy_octet_string(v32_t *x, const uint8_t s[4]) { |
michael@0 | 534 | x->v8[0] = s[0]; |
michael@0 | 535 | x->v8[1] = s[1]; |
michael@0 | 536 | x->v8[2] = s[2]; |
michael@0 | 537 | x->v8[3] = s[3]; |
michael@0 | 538 | } |
michael@0 | 539 | |
michael@0 | 540 | void |
michael@0 | 541 | v64_copy_octet_string(v64_t *x, const uint8_t s[8]) { |
michael@0 | 542 | x->v8[0] = s[0]; |
michael@0 | 543 | x->v8[1] = s[1]; |
michael@0 | 544 | x->v8[2] = s[2]; |
michael@0 | 545 | x->v8[3] = s[3]; |
michael@0 | 546 | x->v8[4] = s[4]; |
michael@0 | 547 | x->v8[5] = s[5]; |
michael@0 | 548 | x->v8[6] = s[6]; |
michael@0 | 549 | x->v8[7] = s[7]; |
michael@0 | 550 | } |
michael@0 | 551 | |
michael@0 | 552 | void |
michael@0 | 553 | v128_copy_octet_string(v128_t *x, const uint8_t s[16]) { |
michael@0 | 554 | x->v8[0] = s[0]; |
michael@0 | 555 | x->v8[1] = s[1]; |
michael@0 | 556 | x->v8[2] = s[2]; |
michael@0 | 557 | x->v8[3] = s[3]; |
michael@0 | 558 | x->v8[4] = s[4]; |
michael@0 | 559 | x->v8[5] = s[5]; |
michael@0 | 560 | x->v8[6] = s[6]; |
michael@0 | 561 | x->v8[7] = s[7]; |
michael@0 | 562 | x->v8[8] = s[8]; |
michael@0 | 563 | x->v8[9] = s[9]; |
michael@0 | 564 | x->v8[10] = s[10]; |
michael@0 | 565 | x->v8[11] = s[11]; |
michael@0 | 566 | x->v8[12] = s[12]; |
michael@0 | 567 | x->v8[13] = s[13]; |
michael@0 | 568 | x->v8[14] = s[14]; |
michael@0 | 569 | x->v8[15] = s[15]; |
michael@0 | 570 | |
michael@0 | 571 | } |
michael@0 | 572 | |
michael@0 | 573 | #ifndef DATATYPES_USE_MACROS /* little functions are not macros */ |
michael@0 | 574 | |
michael@0 | 575 | void |
michael@0 | 576 | v128_set_to_zero(v128_t *x) { |
michael@0 | 577 | _v128_set_to_zero(x); |
michael@0 | 578 | } |
michael@0 | 579 | |
michael@0 | 580 | void |
michael@0 | 581 | v128_copy(v128_t *x, const v128_t *y) { |
michael@0 | 582 | _v128_copy(x, y); |
michael@0 | 583 | } |
michael@0 | 584 | |
michael@0 | 585 | void |
michael@0 | 586 | v128_xor(v128_t *z, v128_t *x, v128_t *y) { |
michael@0 | 587 | _v128_xor(z, x, y); |
michael@0 | 588 | } |
michael@0 | 589 | |
michael@0 | 590 | void |
michael@0 | 591 | v128_and(v128_t *z, v128_t *x, v128_t *y) { |
michael@0 | 592 | _v128_and(z, x, y); |
michael@0 | 593 | } |
michael@0 | 594 | |
michael@0 | 595 | void |
michael@0 | 596 | v128_or(v128_t *z, v128_t *x, v128_t *y) { |
michael@0 | 597 | _v128_or(z, x, y); |
michael@0 | 598 | } |
michael@0 | 599 | |
michael@0 | 600 | void |
michael@0 | 601 | v128_complement(v128_t *x) { |
michael@0 | 602 | _v128_complement(x); |
michael@0 | 603 | } |
michael@0 | 604 | |
michael@0 | 605 | int |
michael@0 | 606 | v128_is_eq(const v128_t *x, const v128_t *y) { |
michael@0 | 607 | return _v128_is_eq(x, y); |
michael@0 | 608 | } |
michael@0 | 609 | |
michael@0 | 610 | int |
michael@0 | 611 | v128_get_bit(const v128_t *x, int i) { |
michael@0 | 612 | return _v128_get_bit(x, i); |
michael@0 | 613 | } |
michael@0 | 614 | |
michael@0 | 615 | void |
michael@0 | 616 | v128_set_bit(v128_t *x, int i) { |
michael@0 | 617 | _v128_set_bit(x, i); |
michael@0 | 618 | } |
michael@0 | 619 | |
michael@0 | 620 | void |
michael@0 | 621 | v128_clear_bit(v128_t *x, int i){ |
michael@0 | 622 | _v128_clear_bit(x, i); |
michael@0 | 623 | } |
michael@0 | 624 | |
michael@0 | 625 | void |
michael@0 | 626 | v128_set_bit_to(v128_t *x, int i, int y){ |
michael@0 | 627 | _v128_set_bit_to(x, i, y); |
michael@0 | 628 | } |
michael@0 | 629 | |
michael@0 | 630 | |
michael@0 | 631 | #endif /* DATATYPES_USE_MACROS */ |
michael@0 | 632 | |
michael@0 | 633 | |
michael@0 | 634 | static inline void |
michael@0 | 635 | v128_left_shift2(v128_t *x, int num_bits) { |
michael@0 | 636 | int i; |
michael@0 | 637 | int word_shift = num_bits >> 5; |
michael@0 | 638 | int bit_shift = num_bits & 31; |
michael@0 | 639 | |
michael@0 | 640 | for (i=0; i < (4-word_shift); i++) { |
michael@0 | 641 | x->v32[i] = x->v32[i+word_shift] << bit_shift; |
michael@0 | 642 | } |
michael@0 | 643 | |
michael@0 | 644 | for ( ; i < word_shift; i++) { |
michael@0 | 645 | x->v32[i] = 0; |
michael@0 | 646 | } |
michael@0 | 647 | |
michael@0 | 648 | } |
michael@0 | 649 | |
michael@0 | 650 | void |
michael@0 | 651 | v128_right_shift(v128_t *x, int index) { |
michael@0 | 652 | const int base_index = index >> 5; |
michael@0 | 653 | const int bit_index = index & 31; |
michael@0 | 654 | int i, from; |
michael@0 | 655 | uint32_t b; |
michael@0 | 656 | |
michael@0 | 657 | if (index > 127) { |
michael@0 | 658 | v128_set_to_zero(x); |
michael@0 | 659 | return; |
michael@0 | 660 | } |
michael@0 | 661 | |
michael@0 | 662 | if (bit_index == 0) { |
michael@0 | 663 | |
michael@0 | 664 | /* copy each word from left size to right side */ |
michael@0 | 665 | x->v32[4-1] = x->v32[4-1-base_index]; |
michael@0 | 666 | for (i=4-1; i > base_index; i--) |
michael@0 | 667 | x->v32[i-1] = x->v32[i-1-base_index]; |
michael@0 | 668 | |
michael@0 | 669 | } else { |
michael@0 | 670 | |
michael@0 | 671 | /* set each word to the "or" of the two bit-shifted words */ |
michael@0 | 672 | for (i = 4; i > base_index; i--) { |
michael@0 | 673 | from = i-1 - base_index; |
michael@0 | 674 | b = x->v32[from] << bit_index; |
michael@0 | 675 | if (from > 0) |
michael@0 | 676 | b |= x->v32[from-1] >> (32-bit_index); |
michael@0 | 677 | x->v32[i-1] = b; |
michael@0 | 678 | } |
michael@0 | 679 | |
michael@0 | 680 | } |
michael@0 | 681 | |
michael@0 | 682 | /* now wrap up the final portion */ |
michael@0 | 683 | for (i=0; i < base_index; i++) |
michael@0 | 684 | x->v32[i] = 0; |
michael@0 | 685 | |
michael@0 | 686 | } |
michael@0 | 687 | |
michael@0 | 688 | void |
michael@0 | 689 | v128_left_shift(v128_t *x, int index) { |
michael@0 | 690 | int i; |
michael@0 | 691 | const int base_index = index >> 5; |
michael@0 | 692 | const int bit_index = index & 31; |
michael@0 | 693 | |
michael@0 | 694 | if (index > 127) { |
michael@0 | 695 | v128_set_to_zero(x); |
michael@0 | 696 | return; |
michael@0 | 697 | } |
michael@0 | 698 | |
michael@0 | 699 | if (bit_index == 0) { |
michael@0 | 700 | for (i=0; i < 4 - base_index; i++) |
michael@0 | 701 | x->v32[i] = x->v32[i+base_index]; |
michael@0 | 702 | } else { |
michael@0 | 703 | for (i=0; i < 4 - base_index - 1; i++) |
michael@0 | 704 | x->v32[i] = (x->v32[i+base_index] << bit_index) ^ |
michael@0 | 705 | (x->v32[i+base_index+1] >> (32 - bit_index)); |
michael@0 | 706 | x->v32[4 - base_index-1] = x->v32[4-1] << bit_index; |
michael@0 | 707 | } |
michael@0 | 708 | |
michael@0 | 709 | /* now wrap up the final portion */ |
michael@0 | 710 | for (i = 4 - base_index; i < 4; i++) |
michael@0 | 711 | x->v32[i] = 0; |
michael@0 | 712 | |
michael@0 | 713 | } |
michael@0 | 714 | |
michael@0 | 715 | |
michael@0 | 716 | #if 0 |
michael@0 | 717 | void |
michael@0 | 718 | v128_add(v128_t *z, v128_t *x, v128_t *y) { |
michael@0 | 719 | /* integer addition modulo 2^128 */ |
michael@0 | 720 | |
michael@0 | 721 | #ifdef WORDS_BIGENDIAN |
michael@0 | 722 | uint64_t tmp; |
michael@0 | 723 | |
michael@0 | 724 | tmp = x->v32[3] + y->v32[3]; |
michael@0 | 725 | z->v32[3] = (uint32_t) tmp; |
michael@0 | 726 | |
michael@0 | 727 | tmp = x->v32[2] + y->v32[2] + (tmp >> 32); |
michael@0 | 728 | z->v32[2] = (uint32_t) tmp; |
michael@0 | 729 | |
michael@0 | 730 | tmp = x->v32[1] + y->v32[1] + (tmp >> 32); |
michael@0 | 731 | z->v32[1] = (uint32_t) tmp; |
michael@0 | 732 | |
michael@0 | 733 | tmp = x->v32[0] + y->v32[0] + (tmp >> 32); |
michael@0 | 734 | z->v32[0] = (uint32_t) tmp; |
michael@0 | 735 | |
michael@0 | 736 | #else /* assume little endian architecture */ |
michael@0 | 737 | uint64_t tmp; |
michael@0 | 738 | |
michael@0 | 739 | tmp = htonl(x->v32[3]) + htonl(y->v32[3]); |
michael@0 | 740 | z->v32[3] = ntohl((uint32_t) tmp); |
michael@0 | 741 | |
michael@0 | 742 | tmp = htonl(x->v32[2]) + htonl(y->v32[2]) + htonl(tmp >> 32); |
michael@0 | 743 | z->v32[2] = ntohl((uint32_t) tmp); |
michael@0 | 744 | |
michael@0 | 745 | tmp = htonl(x->v32[1]) + htonl(y->v32[1]) + htonl(tmp >> 32); |
michael@0 | 746 | z->v32[1] = ntohl((uint32_t) tmp); |
michael@0 | 747 | |
michael@0 | 748 | tmp = htonl(x->v32[0]) + htonl(y->v32[0]) + htonl(tmp >> 32); |
michael@0 | 749 | z->v32[0] = ntohl((uint32_t) tmp); |
michael@0 | 750 | |
michael@0 | 751 | #endif /* WORDS_BIGENDIAN */ |
michael@0 | 752 | |
michael@0 | 753 | } |
michael@0 | 754 | #endif |
michael@0 | 755 | |
michael@0 | 756 | int |
michael@0 | 757 | octet_string_is_eq(uint8_t *a, uint8_t *b, int len) { |
michael@0 | 758 | uint8_t *end = b + len; |
michael@0 | 759 | while (b < end) |
michael@0 | 760 | if (*a++ != *b++) |
michael@0 | 761 | return 1; |
michael@0 | 762 | return 0; |
michael@0 | 763 | } |
michael@0 | 764 | |
michael@0 | 765 | void |
michael@0 | 766 | octet_string_set_to_zero(uint8_t *s, int len) { |
michael@0 | 767 | uint8_t *end = s + len; |
michael@0 | 768 | |
michael@0 | 769 | do { |
michael@0 | 770 | *s = 0; |
michael@0 | 771 | } while (++s < end); |
michael@0 | 772 | |
michael@0 | 773 | } |
michael@0 | 774 | |
michael@0 | 775 | |
michael@0 | 776 | /* functions below not yet tested! */ |
michael@0 | 777 | |
michael@0 | 778 | int |
michael@0 | 779 | v32_low_bit(v32_t *w) { |
michael@0 | 780 | int value; |
michael@0 | 781 | |
michael@0 | 782 | value = low_bit[w->v8[0]]; |
michael@0 | 783 | if (value != -1) |
michael@0 | 784 | return value; |
michael@0 | 785 | value = low_bit[w->v8[1]]; |
michael@0 | 786 | if (value != -1) |
michael@0 | 787 | return value + 8; |
michael@0 | 788 | value = low_bit[w->v8[2]]; |
michael@0 | 789 | if (value != -1) |
michael@0 | 790 | return value + 16; |
michael@0 | 791 | value = low_bit[w->v8[3]]; |
michael@0 | 792 | if (value == -1) |
michael@0 | 793 | return -1; |
michael@0 | 794 | return value + 24; |
michael@0 | 795 | } |
michael@0 | 796 | |
michael@0 | 797 | /* high_bit not done yet */ |
michael@0 | 798 | |
michael@0 | 799 | |
michael@0 | 800 | |
michael@0 | 801 | |
michael@0 | 802 |