File: random.c

package info (click to toggle)
medussa 0.8-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,004 kB
  • ctags: 1,156
  • sloc: ansic: 9,049; sh: 3,319; makefile: 113
file content (164 lines) | stat: -rw-r--r-- 3,641 bytes parent folder | download | duplicates (3)
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
 * medussa - a distributed cracking system
 * Copyright (C) 1999 Kostas Evangelinos <kos@bastard.net>
 *
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * 
 */

/*
 * $Id: random.c,v 1.3 2003/02/05 04:38:37 kos Exp $
 *
 * Essentially a crap RNG based on MD5. It is not guaranteed to give
 * results that have a one to one mapping to the index, as with any RNG.
 * Use obfuscate for that.
 * 
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

#include "common.h"
#include "keyspace.h"
#include "binary.h"
#include "xmalloc.h"
#include "generator.h"
#include "random.h"

random_t *
random_init(char *params) {
  random_t *k;

  k = (random_t *)xcalloc(1, sizeof(random_t));
  param_get_str(params, "seed", k->seed, RNG_DEF_SEED);
  param_get_int(params, "len", &k->len, RNG_DEF_LENGTH);

  if(k->len > RNG_MAX_LENGTH ||
     k->len <= 0)
    return (random_t *)NULL;

  keyspace_init(k->minindex);
  keyspace_init(k->maxindex);
  keyspace_init(k->index);

  keyspace_fromint(0, k->minindex);  
  keyspace_fromint(2, k->maxindex);
  keyspace_pow(k->maxindex, k->len*8, k->maxindex);
  keyspace_set(k->index, k->minindex);
  return k;
}

key_index_t *
random_minindex(random_t *b) {
  return &b->minindex;
}

key_index_t *
random_maxindex(random_t *b) {
  return &b->maxindex;
}

key_index_t *
random_curindex(random_t *b) {
  return &b->index;
}

int
random_minlen(random_t *b) {
  return b->len;
}

int
random_maxlen(random_t *b) {
  return b->len;
}

int
random_destroy(random_t *k) {
  keyspace_destroy(k->minindex);
  keyspace_destroy(k->maxindex);
  keyspace_destroy(k->index);
  xfree(k);
  return 0;
}

int
random_fetch(random_t *k, kchar *buf, int userlen, int *len) {
  MD5_CTX ctx;
  unsigned char digest[MD5_DIGEST_LENGTH];
  int i;
  int j;

  if(userlen < k->len)
    return 1;

  MD5_Init(&ctx);
  MD5_Update(&ctx, k->x_n, RNG_MAX_LENGTH);
  MD5_Final(digest, &ctx);
  
  *len = k->len;
  memcpy(buf, digest, k->len);
  for(i=1; i<(RNG_MAX_LENGTH/k->len); i+=k->len) {
    for(j=0; j<k->len; j++)
      buf[j] += digest[i+j];
  }

  memcpy(k->x_n, digest, RNG_MAX_LENGTH);

  keyspace_inc(k->index);
  return 0;
}

int
random_set(random_t *k, key_index_t index) {
  unsigned char digest[RNG_MAX_LENGTH];
  MD5_CTX ctx;
  
  while(keyspace_cmp(k->index, index)) {
    MD5_Init(&ctx);
    MD5_Update(&ctx, k->x_n, RNG_MAX_LENGTH);
    MD5_Final(digest, &ctx);
  
    memcpy(k->x_n, digest, RNG_MAX_LENGTH);
    keyspace_inc(k->index);
  }
  return 0;
}

int 
random_done(random_t *k) {
  return (keyspace_cmp(k->index, k->maxindex) == 0);
}

generator_impl_t random_impl = {
  "random",
  (gen_init)random_init,
  (gen_set)random_set,
  (gen_fetch)random_fetch,
  (gen_done)random_done,
  (gen_destroy)random_destroy,
  (gen_minindex)random_minindex,
  (gen_maxindex)random_maxindex,
  (gen_curindex)random_curindex,
  (gen_minlen)random_minlen,
  (gen_maxlen)random_maxlen
};