| 1 | /* |
| 2 | * Copyright (C) 1999 Descolada (dyn1-tnt9-237.chicago.il.ameritech.net) |
| 3 | * Copyright (C) 1999,2000 Ross Combs ([email protected]) |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU General Public License |
| 7 | * as published by the Free Software Foundation; either version 2 |
| 8 | * of the License, or (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 18 | */ |
| 19 | #include "common/setup_before.h" |
| 20 | #include "common/bnethash.h" |
| 21 | |
| 22 | #include <cstdint> |
| 23 | #include <cstdio> |
| 24 | #include <cstring> |
| 25 | |
| 26 | #include "common/bn_type.h" |
| 27 | #include "common/introtate.h" |
| 28 | #include "common/eventlog.h" |
| 29 | #include "common/setup_after.h" |
| 30 | |
| 31 | |
| 32 | namespace pvpgn |
| 33 | { |
| 34 | |
| 35 | typedef enum { |
| 36 | do_blizzard_hash, |
| 37 | do_sha1_hash |
| 38 | } t_hash_variant; |
| 39 | |
| 40 | static void hash_init(t_hash * hash); |
| 41 | static void do_hash(t_hash * hash, std::uint32_t * tmp); |
| 42 | static void hash_set_16(std::uint32_t * dst, unsigned char const * src, unsigned int count, t_hash_variant hash_variant); |
| 43 | |
| 44 | |
| 45 | static void hash_init(t_hash * hash) |
| 46 | { |
| 47 | (*hash)[0] = 0x67452301; |
| 48 | (*hash)[1] = 0xefcdab89; |
| 49 | (*hash)[2] = 0x98badcfe; |
| 50 | (*hash)[3] = 0x10325476; |
| 51 | (*hash)[4] = 0xc3d2e1f0; |
| 52 | } |
| 53 | |
| 54 | |
| 55 | static void do_hash(t_hash * hash, std::uint32_t * tmp, t_hash_variant hash_variant) |
| 56 | { |
| 57 | unsigned int i; |
| 58 | std::uint32_t a, b, c, d, e, g; |
| 59 | |
| 60 | for (i = 0; i < 64; i++) |
| 61 | if (hash_variant == do_blizzard_hash) |
| 62 | tmp[i + 16] = ROTL32(1, tmp[i] ^ tmp[i + 8] ^ tmp[i + 2] ^ tmp[i + 13]); |
| 63 | else |
| 64 | tmp[i + 16] = ROTL32(tmp[i] ^ tmp[i + 8] ^ tmp[i + 2] ^ tmp[i + 13], 1); |
| 65 | |
| 66 | a = (*hash)[0]; |
| 67 | b = (*hash)[1]; |
| 68 | c = (*hash)[2]; |
| 69 | d = (*hash)[3]; |
| 70 | e = (*hash)[4]; |
| 71 | |
| 72 | for (i = 0; i < 20 * 1; i++) |
| 73 | { |
| 74 | g = tmp[i] + ROTL32(a, 5) + e + ((b & c) | (~b & d)) + 0x5a827999; |
| 75 | e = d; |
| 76 | d = c; |
| 77 | c = ROTL32(b, 30); |
| 78 | b = a; |
| 79 | a = g; |
| 80 | } |
| 81 | |
| 82 | for (; i < 20 * 2; i++) |
| 83 | { |
| 84 | g = (d ^ c ^ b) + e + ROTL32(g, 5) + tmp[i] + 0x6ed9eba1; |
| 85 | e = d; |
| 86 | d = c; |
| 87 | c = ROTL32(b, 30); |
| 88 | b = a; |
| 89 | a = g; |
| 90 | } |
| 91 | |
| 92 | for (; i < 20 * 3; i++) |
| 93 | { |
| 94 | g = tmp[i] + ROTL32(g, 5) + e + ((c & b) | (d & c) | (d & b)) - 0x70e44324; |
| 95 | e = d; |
| 96 | d = c; |
| 97 | c = ROTL32(b, 30); |
| 98 | b = a; |
| 99 | a = g; |
| 100 | } |
| 101 | |
| 102 | for (; i < 20 * 4; i++) |
| 103 | { |
| 104 | g = (d ^ c ^ b) + e + ROTL32(g, 5) + tmp[i] - 0x359d3e2a; |
| 105 | e = d; |
| 106 | d = c; |
| 107 | c = ROTL32(b, 30); |
| 108 | b = a; |
| 109 | a = g; |
| 110 | } |
| 111 | |
| 112 | (*hash)[0] += g; |
| 113 | (*hash)[1] += b; |
| 114 | (*hash)[2] += c; |
| 115 | (*hash)[3] += d; |
| 116 | (*hash)[4] += e; |
| 117 | } |
| 118 | |
| 119 | |
| 120 | /* |
| 121 | * Fill 16 elements of the array of 32 bit values with the bytes from |
| 122 | * dst up to count in little endian order. Fill left over space with |
| 123 | * zeros. In case of SHA1 hash variant a binary 1 is appended after |
| 124 | * the actual data. |
| 125 | */ |
| 126 | static void hash_set_16(std::uint32_t * dst, unsigned char const * src, unsigned int count, |
| 127 | t_hash_variant hash_variant) |
| 128 | { |
| 129 | unsigned int i; |
| 130 | unsigned int pos; |
| 131 | |
| 132 | for (pos = 0, i = 0; i < 16; i++) |
| 133 | { |
| 134 | dst[i] = 0; |
| 135 | |
| 136 | if (hash_variant == do_blizzard_hash) { |
| 137 | if (pos < count) |
| 138 | dst[i] |= ((std::uint32_t)src[pos]); |
| 139 | } |
| 140 | else { |
| 141 | if (pos < count) |
| 142 | dst[i] |= ((std::uint32_t)src[pos]) << 24; |
| 143 | else if (pos == count) |
| 144 | dst[i] |= ((std::uint32_t)0x80000000); |
| 145 | } |
| 146 | pos++; |
| 147 | |
| 148 | if (hash_variant == do_blizzard_hash) { |
| 149 | if (pos < count) |
| 150 | dst[i] |= ((std::uint32_t)src[pos]) << 8; |
| 151 | } |
| 152 | else { |
| 153 | if (pos < count) |
| 154 | dst[i] |= ((std::uint32_t)src[pos]) << 16; |
| 155 | else if (pos == count) |
| 156 | dst[i] |= ((std::uint32_t)0x800000); |
| 157 | } |
| 158 | pos++; |
| 159 | |
| 160 | if (hash_variant == do_blizzard_hash) { |
| 161 | if (pos < count) |
| 162 | dst[i] |= ((std::uint32_t)src[pos]) << 16; |
| 163 | } |
| 164 | else { |
| 165 | if (pos < count) |
| 166 | dst[i] |= ((std::uint32_t)src[pos]) << 8; |
| 167 | else if (pos == count) |
| 168 | dst[i] |= ((std::uint32_t)0x8000); |
| 169 | } |
| 170 | pos++; |
| 171 | |
| 172 | if (hash_variant == do_blizzard_hash) { |
| 173 | if (pos < count) |
| 174 | dst[i] |= ((std::uint32_t)src[pos]) << 24; |
| 175 | } |
| 176 | else { |
| 177 | if (pos < count) |
| 178 | dst[i] |= ((std::uint32_t)src[pos]); |
| 179 | else if (pos == count) |
| 180 | dst[i] |= ((std::uint32_t)0x80); |
| 181 | } |
| 182 | pos++; |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | |
| 187 | extern int bnet_hash(t_hash * hashout, unsigned int size, void const * datain) |
| 188 | { |
| 189 | std::uint32_t tmp[64 + 16]; |
| 190 | const unsigned char* data; |
| 191 | unsigned int inc; |
| 192 | |
| 193 | if (!hashout) |
| 194 | { |
| 195 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL hashout"); |
| 196 | return -1; |
| 197 | } |
| 198 | if (size > 0 && !datain) |
| 199 | { |
| 200 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL datain with size={}", size); |
| 201 | return -1; |
| 202 | } |
| 203 | |
| 204 | hash_init(hashout); |
| 205 | |
| 206 | data = (const unsigned char*)datain; |
| 207 | while (size > 0) |
| 208 | { |
| 209 | if (size > 64) |
| 210 | inc = 64; |
| 211 | else |
| 212 | inc = size; |
| 213 | |
| 214 | hash_set_16(tmp, data, inc, do_blizzard_hash); |
| 215 | do_hash(hashout, tmp, do_blizzard_hash); |
| 216 | |
| 217 | data += inc; |
| 218 | size -= inc; |
| 219 | } |
| 220 | |
| 221 | return 0; |
| 222 | } |
| 223 | |
| 224 | static void hash_set_length(std::uint32_t * dst, unsigned int size){ |
| 225 | std::uint32_t size_high = 0; |
| 226 | std::uint32_t size_low = 0; |
| 227 | unsigned int counter; |
| 228 | for (counter = 0; counter < size; counter++){ |
| 229 | size_low += 8; |
| 230 | if (size_low == 0) |
| 231 | size_high++; |
| 232 | } |
| 233 | |
| 234 | dst[14] |= ((size_high >> 24) & 0xff) << 24; |
| 235 | dst[14] |= ((size_high >> 16) & 0xff) << 16; |
| 236 | dst[14] |= ((size_high >> 8) & 0xff) << 8; |
| 237 | dst[14] |= ((size_high)& 0xff); |
| 238 | |
| 239 | dst[15] |= ((size_low >> 24) & 0xff) << 24; |
| 240 | dst[15] |= ((size_low >> 16) & 0xff) << 16; |
| 241 | dst[15] |= ((size_low >> 8) & 0xff) << 8; |
| 242 | dst[15] |= ((size_low)& 0xff); |
| 243 | } |
| 244 | |
| 245 | extern int sha1_hash(t_hash * hashout, unsigned int size, void const * datain) |
| 246 | { |
| 247 | std::uint32_t tmp[64 + 16]; |
| 248 | unsigned char const * data; |
| 249 | unsigned int inc; |
| 250 | unsigned int orgSize; |
| 251 | |
| 252 | if (!hashout) |
| 253 | { |
| 254 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL hashout"); |
| 255 | return -1; |
| 256 | } |
| 257 | if (size > 0 && !datain) |
| 258 | { |
| 259 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL datain with size={}", size); |
| 260 | return -1; |
| 261 | } |
| 262 | |
| 263 | hash_init(hashout); |
| 264 | orgSize = size; |
| 265 | |
| 266 | data = (const unsigned char*)datain; |
| 267 | while (size > 0) |
| 268 | { |
| 269 | if (size >= 64) |
| 270 | inc = 64; |
| 271 | else |
| 272 | inc = size; |
| 273 | |
| 274 | if (size >= 64) |
| 275 | { |
| 276 | hash_set_16(tmp, data, inc, do_sha1_hash); |
| 277 | do_hash(hashout, tmp, do_sha1_hash); |
| 278 | } |
| 279 | else if (size > 55){ |
| 280 | |
| 281 | hash_set_16(tmp, data, inc, do_sha1_hash); |
| 282 | do_hash(hashout, tmp, do_sha1_hash); |
| 283 | |
| 284 | // now use blizz variant as we only wanna fill in zeros |
| 285 | hash_set_16(tmp, data, 0, do_blizzard_hash); |
| 286 | hash_set_length(tmp, orgSize); |
| 287 | do_hash(hashout, tmp, do_sha1_hash); |
| 288 | } |
| 289 | else{ |
| 290 | hash_set_16(tmp, data, inc, do_sha1_hash); |
| 291 | hash_set_length(tmp, orgSize); |
| 292 | do_hash(hashout, tmp, do_sha1_hash); |
| 293 | } |
| 294 | |
| 295 | data += inc; |
| 296 | size -= inc; |
| 297 | } |
| 298 | |
| 299 | return 0; |
| 300 | } |
| 301 | |
| 302 | extern int little_endian_sha1_hash(t_hash * hashout, unsigned int size, void const * datain) |
| 303 | { |
| 304 | bn_int value; |
| 305 | unsigned int i; |
| 306 | sha1_hash(hashout, size, datain); |
| 307 | for (i = 0; i < 5; i++) |
| 308 | { |
| 309 | bn_int_nset(&value, (*hashout)[i]); |
| 310 | (*hashout)[i] = bn_int_get(value); |
| 311 | } |
| 312 | return 0; |
| 313 | } |
| 314 | |
| 315 | extern int hash_eq(t_hash const h1, t_hash const h2) |
| 316 | { |
| 317 | unsigned int i; |
| 318 | |
| 319 | if (!h1 || !h2) |
| 320 | { |
| 321 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL hash"); |
| 322 | return -1; |
| 323 | } |
| 324 | |
| 325 | for (i = 0; i < 5; i++) |
| 326 | if (h1[i] != h2[i]) |
| 327 | return 0; |
| 328 | |
| 329 | return 1; |
| 330 | } |
| 331 | |
| 332 | |
| 333 | extern char const * hash_get_str(t_hash const hash) |
| 334 | { |
| 335 | static char temp[8 * 5 + 1]; /* each of 5 ints to 8 chars + null */ |
| 336 | unsigned int i; |
| 337 | |
| 338 | if (!hash) |
| 339 | { |
| 340 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL hash"); |
| 341 | return NULL; |
| 342 | } |
| 343 | |
| 344 | for (i = 0; i < 5; i++) |
| 345 | std::sprintf(&temp[i * 8], "%08x", hash[i]); |
| 346 | |
| 347 | return temp; |
| 348 | } |
| 349 | |
| 350 | extern char const * little_endian_hash_get_str(t_hash const hash) |
| 351 | { |
| 352 | bn_int value; |
| 353 | t_hash be_hash; |
| 354 | unsigned int i; |
| 355 | for (i = 0; i < 5; i++) |
| 356 | { |
| 357 | bn_int_nset(&value, hash[i]); |
| 358 | be_hash[i] = bn_int_get(value); |
| 359 | } |
| 360 | return hash_get_str(be_hash); |
| 361 | } |
| 362 | |
| 363 | |
| 364 | extern int hash_set_str(t_hash * hash, char const * str) |
| 365 | { |
| 366 | unsigned int i; |
| 367 | |
| 368 | if (!hash) |
| 369 | { |
| 370 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL hash pointer"); |
| 371 | return -1; |
| 372 | } |
| 373 | if (!*hash) |
| 374 | { |
| 375 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL hash"); |
| 376 | return -1; |
| 377 | } |
| 378 | if (!str) |
| 379 | { |
| 380 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL str"); |
| 381 | return -1; |
| 382 | } |
| 383 | if (std::strlen(str) != 5 * 8) |
| 384 | { |
| 385 | eventlog(eventlog_level_error, __FUNCTION__, "got string with length {} (should be {})", std::strlen(str), 5 * 8); |
| 386 | return -1; |
| 387 | } |
| 388 | |
| 389 | for (i = 0; i < 5; i++) |
| 390 | if (std::sscanf(&str[i * 8], "%8x", &(*hash)[i]) != 1) |
| 391 | { |
| 392 | eventlog(eventlog_level_error, __FUNCTION__, "got bad string"); |
| 393 | return -1; |
| 394 | } |
| 395 | |
| 396 | return 0; |
| 397 | } |
| 398 | |
| 399 | } |