| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
#include <stdio.h> |
| 33 |
#include <stdlib.h> |
| 34 |
#include <stdint.h> |
| 35 |
#include <pthread.h> |
| 36 |
#include <semaphore.h> |
| 37 |
#include <math.h> |
| 38 |
#include <string.h> |
| 39 |
#include <unistd.h> |
| 40 |
#include <assert.h> |
| 41 |
#include <openssl/des.h> |
| 42 |
|
| 43 |
#include "lmtable.h" |
| 44 |
#include "lmflash.h" |
| 45 |
#include "hash.h" |
| 46 |
#include "ophel.h" |
| 47 |
|
| 48 |
int lmtable_setup(void *tbl_) { |
| 49 |
table_t *tbl = tbl_; |
| 50 |
|
| 51 |
switch (tbl->kind) { |
| 52 |
case lmalphanum10k: |
| 53 |
tbl->ncols = 10000; |
| 54 |
tbl->offset = 10000; |
| 55 |
tbl->sizes = lmalphanum10k_sizes; |
| 56 |
break; |
| 57 |
|
| 58 |
case lmalphanum5k: |
| 59 |
tbl->ncols = 5000; |
| 60 |
tbl->offset = 10000; |
| 61 |
tbl->sizes = lmalphanum5k_sizes; |
| 62 |
break; |
| 63 |
|
| 64 |
case lmextended: |
| 65 |
tbl->ncols = 20479; |
| 66 |
tbl->offset = 71538; |
| 67 |
tbl->sizes = lmextended_sizes; |
| 68 |
break; |
| 69 |
|
| 70 |
default: |
| 71 |
break; |
| 72 |
} |
| 73 |
|
| 74 |
tbl->find = lmtable_find; |
| 75 |
tbl->check = lmtable_check; |
| 76 |
tbl->isvalid = lmtable_isvalid; |
| 77 |
|
| 78 |
tbl->lookup_idx = lmtable_lookup_idx; |
| 79 |
tbl->lookup_end = lmtable_lookup_end; |
| 80 |
tbl->lookup_srt = lmtable_lookup_srt; |
| 81 |
|
| 82 |
return 1; |
| 83 |
} |
| 84 |
|
| 85 |
void lmtable_find(void *hsh_, void *tbl_, void *el_) { |
| 86 |
hash_t *hsh = hsh_; |
| 87 |
table_t *tbl = tbl_; |
| 88 |
ophel_t *el = el_; |
| 89 |
uchar_t *hash = hsh->hash;; |
| 90 |
uchar_t *pwd = (uchar_t*)el->pwd; |
| 91 |
ophstat_t *stat = el->stat; |
| 92 |
|
| 93 |
int c; |
| 94 |
int col = el->col; |
| 95 |
int ncols = tbl->ncols; |
| 96 |
int n_redux = col + tbl->idx * tbl->offset; |
| 97 |
|
| 98 |
uchar_t tmp[8]; |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
lmtable_mkredux(tbl, hash, pwd, n_redux); |
| 103 |
|
| 104 |
for (c=1; c<ncols-col; ++c) { |
| 105 |
++n_redux; |
| 106 |
|
| 107 |
lmtable_mkhash(pwd, tmp); |
| 108 |
lmtable_mkredux(tbl, tmp, pwd, n_redux); |
| 109 |
++stat->hredux; |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
char *charset = 0; |
| 115 |
int chars_size = 0; |
| 116 |
|
| 117 |
switch (tbl->kind) { |
| 118 |
case lmalphanum5k: |
| 119 |
case lmalphanum10k: |
| 120 |
charset = (char*)alphanum_chars; |
| 121 |
chars_size = sizeof(alphanum_chars); |
| 122 |
break; |
| 123 |
|
| 124 |
case lmextended: |
| 125 |
charset = (char*)extended_chars; |
| 126 |
chars_size = sizeof(extended_chars); |
| 127 |
break; |
| 128 |
|
| 129 |
default: |
| 130 |
break; |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
int i; |
| 136 |
int len0 = strlen((char*)pwd); |
| 137 |
int len = len0 < 4 ? len0 : 4; |
| 138 |
|
| 139 |
uint32_t prefix = 0; |
| 140 |
uint64_t offset = chars_size * chars_size * chars_size * chars_size; |
| 141 |
|
| 142 |
char *p; |
| 143 |
|
| 144 |
for (i=0; i<4; ++i) { |
| 145 |
p = strchr(charset, pwd[i]); |
| 146 |
|
| 147 |
if (i < len) |
| 148 |
prefix = prefix * chars_size + (p - charset); |
| 149 |
else { |
| 150 |
prefix += offset; |
| 151 |
offset /= chars_size; |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
el->prefix = prefix; |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
uint16_t postfix = 0; |
| 160 |
|
| 161 |
len = (len0 > 4 ? len0-4 : 0); |
| 162 |
offset = chars_size * chars_size * chars_size; |
| 163 |
|
| 164 |
for (i=0; i<3; ++i) { |
| 165 |
p = strchr(charset, pwd[i+4]); |
| 166 |
|
| 167 |
if (i < len) |
| 168 |
postfix = postfix * chars_size + (p - charset); |
| 169 |
else { |
| 170 |
postfix += offset; |
| 171 |
offset /= chars_size; |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
el->postfix = postfix; |
| 176 |
} |
| 177 |
|
| 178 |
int lmtable_lookup_idx(void *hsh_, void *tbl_, void *el_) { |
| 179 |
table_t *tbl = tbl_; |
| 180 |
ophel_t *el = el_; |
| 181 |
|
| 182 |
uint32_t prefix = el->prefix; |
| 183 |
|
| 184 |
ophstat_t *stat = el->stat; |
| 185 |
++stat->prefix; |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
if ((prefix+1)*4 == tbl->idxsize) prefix--; |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
if (tbl->idxmem) { |
| 194 |
uint32_t *mem = (uint32_t*)(tbl->idxmem + 4*prefix); |
| 195 |
|
| 196 |
el->low = mem[0]; |
| 197 |
el->high = mem[1]; |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
else { |
| 203 |
FILE *idxfile = tbl->idxfile; |
| 204 |
|
| 205 |
el->low = 0; |
| 206 |
el->high = 0; |
| 207 |
fseeko(idxfile, 4*prefix, SEEK_SET); |
| 208 |
fread(&el->low, 4, 1, idxfile); |
| 209 |
fread(&el->high, 4, 1, idxfile); |
| 210 |
++stat->fseek_idx; |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
if ((prefix+1)*4 == tbl->idxsize) el->high = tbl->endsize / 2; |
| 216 |
if (prefix == 0) el->low = 0; |
| 217 |
if (el->high == 0 && el->low != 0) el->high = tbl->endsize / 2; |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
el->low = ftohl(el->low); |
| 222 |
el->high = ftohl(el->high); |
| 223 |
|
| 224 |
assert(el->high >= el->low); |
| 225 |
|
| 226 |
return 1; |
| 227 |
} |
| 228 |
|
| 229 |
int lmtable_lookup_end(void *hsh_, void *tbl_, void *el_) { |
| 230 |
table_t *tbl = tbl_; |
| 231 |
ophel_t *el = el_; |
| 232 |
ophstat_t *stat = el->stat; |
| 233 |
|
| 234 |
uint16_t postfix = el->postfix; |
| 235 |
uint64_t low = el->low; |
| 236 |
uint64_t high = el->high; |
| 237 |
uint64_t n = high - low; |
| 238 |
uint64_t i; |
| 239 |
uint64_t offset = 2*low; |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
if (tbl->endmem) { |
| 244 |
uint16_t *mem = (uint16_t*)(tbl->endmem + offset); |
| 245 |
|
| 246 |
for (i=0; i<n; ++i) |
| 247 |
if (ftohs(mem[i]) == postfix) break; |
| 248 |
|
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
else { |
| 254 |
uint16_t pfix[512]; |
| 255 |
FILE *endfile = tbl->endfile; |
| 256 |
|
| 257 |
fseeko(endfile, offset, SEEK_SET); |
| 258 |
fread(pfix, sizeof(uint16_t), n, endfile); |
| 259 |
++stat->fseek_end; |
| 260 |
|
| 261 |
for (i=0; i<n; ++i) |
| 262 |
if (ftohs(pfix[i]) == postfix) break; |
| 263 |
|
| 264 |
} |
| 265 |
|
| 266 |
el->offset = 4 * (low+i); |
| 267 |
|
| 268 |
|
| 269 |
if (i < n) ++stat->postfix; |
| 270 |
|
| 271 |
return i == n ? 0 : 1; |
| 272 |
} |
| 273 |
|
| 274 |
int lmtable_lookup_srt(void *hsh_, void *tbl_, void *el_) { |
| 275 |
table_t *tbl = tbl_; |
| 276 |
ophel_t *el = el_; |
| 277 |
|
| 278 |
uint64_t offset = el->offset; |
| 279 |
uint32_t start; |
| 280 |
|
| 281 |
ophstat_t *stat = el->stat; |
| 282 |
++stat->start; |
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
if (tbl->srtmem) { |
| 287 |
uint32_t *mem = (uint32_t*)(tbl->srtmem + offset); |
| 288 |
start = mem[0]; |
| 289 |
} |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
else { |
| 294 |
FILE *srtfile = tbl->srtfile; |
| 295 |
|
| 296 |
fseeko(srtfile, offset, SEEK_SET); |
| 297 |
fread(&start, sizeof(start), 1, srtfile); |
| 298 |
++stat->fseek_srt; |
| 299 |
} |
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
start = ftohl(start); |
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
int i; |
| 308 |
uchar_t *pwd = (uchar_t*)el->pwd; |
| 309 |
uchar_t *charset = 0; |
| 310 |
|
| 311 |
switch (tbl->kind) { |
| 312 |
case lmalphanum5k: |
| 313 |
case lmalphanum10k: |
| 314 |
charset = (uchar_t*)alphanum_chars; |
| 315 |
break; |
| 316 |
|
| 317 |
case lmextended: |
| 318 |
charset = (uchar_t*)extended_chars; |
| 319 |
break; |
| 320 |
|
| 321 |
default: |
| 322 |
break; |
| 323 |
} |
| 324 |
|
| 325 |
start &= 0x3FFFFFFF; |
| 326 |
|
| 327 |
pwd[6] = 0; |
| 328 |
pwd[7] = 0; |
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
for (i=5; i>=0; --i) { |
| 334 |
pwd[i] = charset[start % 36]; |
| 335 |
start /= 36; |
| 336 |
} |
| 337 |
|
| 338 |
return 1; |
| 339 |
} |
| 340 |
|
| 341 |
int lmtable_check(void *hsh_, void *tbl_, void *el_) { |
| 342 |
hash_t *hsh = hsh_; |
| 343 |
table_t *tbl = tbl_; |
| 344 |
ophel_t *el = el_; |
| 345 |
uchar_t *hash = hsh->hash; |
| 346 |
uchar_t *pwd = (uchar_t*)el->pwd; |
| 347 |
ophstat_t *stat = el->stat; |
| 348 |
|
| 349 |
int c; |
| 350 |
int col = el->col; |
| 351 |
int ncols = tbl->ncols; |
| 352 |
int n_redux = tbl->idx * tbl->offset; |
| 353 |
|
| 354 |
uchar_t tmp[8]; |
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
stat->falarm_hredux = 0; |
| 359 |
|
| 360 |
for (c=ncols-col; c<ncols; ++c) { |
| 361 |
lmtable_mkhash(pwd, tmp); |
| 362 |
lmtable_mkredux(tbl, tmp, pwd, n_redux); |
| 363 |
|
| 364 |
++n_redux; |
| 365 |
++stat->hredux; |
| 366 |
++stat->falarm_hredux; |
| 367 |
} |
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
lmtable_mkhash(pwd, tmp); |
| 372 |
|
| 373 |
if (memcmp(tmp, hash, 8) == 0) { |
| 374 |
++stat->match_table; |
| 375 |
stat->falarm_hredux = 0; |
| 376 |
return 1; |
| 377 |
} |
| 378 |
|
| 379 |
else { |
| 380 |
++stat->falarm; |
| 381 |
return 0; |
| 382 |
} |
| 383 |
} |
| 384 |
|
| 385 |
int lmtable_isvalid(void *hsh_, void *tbl_) { |
| 386 |
hash_t *hsh = hsh_; |
| 387 |
|
| 388 |
return (hsh->kind == lm1 || hsh->kind == lm2) ? 1 : 0; |
| 389 |
} |
| 390 |
|
| 391 |
void lmtable_mkredux(table_t *tbl, uchar_t *hash, uchar_t *pwd, |
| 392 |
int n_redux) { |
| 393 |
int i, n; |
| 394 |
uint32_t *max = 0; |
| 395 |
uchar_t *charset = 0; |
| 396 |
int chars_size = 0; |
| 397 |
|
| 398 |
switch (tbl->kind) { |
| 399 |
case lmalphanum5k: |
| 400 |
case lmalphanum10k: |
| 401 |
max = (uint32_t*)alphanum_max; |
| 402 |
charset = (uchar_t*)alphanum_chars; |
| 403 |
chars_size = sizeof(alphanum_chars); |
| 404 |
break; |
| 405 |
|
| 406 |
case lmextended: |
| 407 |
max = (uint32_t*)extended_max; |
| 408 |
charset = (uchar_t*)extended_chars; |
| 409 |
chars_size = sizeof(extended_chars); |
| 410 |
break; |
| 411 |
|
| 412 |
case lmflash: |
| 413 |
max = (uint32_t*)lmflash_max; |
| 414 |
charset = (uchar_t*)extended_chars; |
| 415 |
chars_size = sizeof(extended_chars); |
| 416 |
break; |
| 417 |
|
| 418 |
default: |
| 419 |
break; |
| 420 |
} |
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
uint32_t idx[2]; |
| 425 |
uint32_t int_hash[2]; |
| 426 |
|
| 427 |
int_hash[0] = ftohl(*(uint32_t*)hash); |
| 428 |
int_hash[1] = ftohl(*(uint32_t*)(hash+4)); |
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
idx[0] = int_hash[0] ^ n_redux; |
| 433 |
idx[1] = int_hash[1]; |
| 434 |
|
| 435 |
n = idx[1]; |
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
for (i=0; i<4; i++) { |
| 440 |
pwd[i] = charset[idx[0] % chars_size]; |
| 441 |
idx[0] /= chars_size; |
| 442 |
} |
| 443 |
|
| 444 |
for (i=4; i<7; i++) { |
| 445 |
pwd[i] = charset[idx[1] % chars_size]; |
| 446 |
idx[1] /= chars_size; |
| 447 |
} |
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
if (n<max[7]) { |
| 452 |
pwd[6]=0; |
| 453 |
if (n<max[6]) { |
| 454 |
pwd[5]=0; |
| 455 |
if (n<max[5]) { |
| 456 |
pwd[4]=0; |
| 457 |
if (n<max[4]) { |
| 458 |
pwd[3]=0; |
| 459 |
if (n<max[3]) { |
| 460 |
pwd[2]=0; |
| 461 |
if (n<max[2]) { |
| 462 |
pwd[1]=0; |
| 463 |
} |
| 464 |
} |
| 465 |
} |
| 466 |
} |
| 467 |
} |
| 468 |
} |
| 469 |
} |
| 470 |
|
| 471 |
void lmtable_mkhash(uchar_t *pwd, uchar_t *hash) { |
| 472 |
uchar_t key[8]; |
| 473 |
DES_key_schedule ks; |
| 474 |
DES_cblock *magic = (DES_cblock*)lmmagic; |
| 475 |
|
| 476 |
key[0] = pwd[0]; |
| 477 |
key[1] = (pwd[0] << 7) | (pwd[1] >> 1); |
| 478 |
key[2] = (pwd[1] << 6) | (pwd[2] >> 2); |
| 479 |
key[3] = (pwd[2] << 5) | (pwd[3] >> 3); |
| 480 |
key[4] = (pwd[3] << 4) | (pwd[4] >> 4); |
| 481 |
key[5] = (pwd[4] << 3) | (pwd[5] >> 5); |
| 482 |
key[6] = (pwd[5] << 2) | (pwd[6] >> 6); |
| 483 |
key[7] = (pwd[6] << 1) ; |
| 484 |
|
| 485 |
DES_set_odd_parity(&key); |
| 486 |
DES_set_key(&key, &ks); |
| 487 |
DES_ecb_encrypt(magic, (DES_cblock*)hash, &ks, DES_ENCRYPT); |
| 488 |
} |