File: cpmask.c

package info (click to toggle)
fcrackzip 1.0-4
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 632 kB
  • sloc: ansic: 2,723; sh: 743; makefile: 121; perl: 81
file content (395 lines) | stat: -rw-r--r-- 8,838 bytes parent folder | download | duplicates (6)
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

/* CP mask code */

/* The original CP code was written by Hirotsuna Mizuno and is
 * Copyright (c) 1998 Hirotsuna Mizuno <s1041150@u-aizu.ac.jp>
 *
 * I heavily modified his code.
 * as for speed, this algorithm could be improved without end,
 * and alternative algorithms might exist that might be several orders
 * of magnitude faster.
 */

/* this is faster */
typedef unsigned int UI;

static UI image_width;
static UI image_height;
static u8 *image_data;
static UI transform_width;
static UI transform_height;
static u8 *transform_data;

typedef struct
  {
    UI dst_x, dst_y;
    int mirror;
  }
cp_cell;

static UI cp_cells;
static cp_cell *cp_trans;
static int *cp_table;
static UI cp_width;
static UI cp_height;


#define CP_CODE_MAX_LENGTH	16
#define CP_CELL_SIZE		8
#define CP_BPP			3
#define OFFSET_X		0
#define OFFSET_Y		0

/* the maximum size of the transformed image in cells */
#define MAX_SIZE		4

#define MAX_CP_WIDTH		2048

#define src_pixel(x,y,c) image_data     [((y)*image_width    +(x)) * CP_BPP + (c)]
#define dst_pixel(x,y,c) transform_data [((y)*transform_width+(x)) * CP_BPP + (c)]

static void
load_img (const char *name)
{
  FILE *img;
  UI image_depth;

  if (image_data)
    fprintf (stderr, "cannot load more than one image\n");
  else
    {
      img = fopen (name, "rb");
      if (img)
	{
	  if (fscanf (img, "P6 ") == EOF)
	    perror ("no BINARY PPM header detected");
	  else
	    {
	      fscanf (img, "#%*[^\012\015] ");	/* comment */
	      if (fscanf (img, "%u ", &image_width) != 1)
		fprintf (stderr, "unable to read image width\n");
	      else
		{
		  fscanf (img, "#%*[^\012\015] ");	/* comment */
		  if (fscanf (img, "%u ", &image_height) != 1)
		    fprintf (stderr, "unable to read image height\n");
		  else
		    {
		      fscanf (img, "#%*[^\012\015] ");	/* comment */
		      if (fscanf (img, "%u%*[ \012\015] ", &image_depth) != 1)
			fprintf (stderr, "unable to read image depth\n");
		      else
			{
			  if (image_depth != 255)
			    fprintf (stderr, "pixel maxval %d (!= 255) is not supported\n", image_depth);
			  else
			    {
			      image_data = (u8 *) malloc (image_width * image_height * CP_BPP);
			      if (!image_data)
				fprintf (stderr, "unable to allocate memory for image\n");
			      else
				{
				  if (fread (image_data, image_width * image_height * CP_BPP, 1, img) != 1)
				    fprintf (stderr, "unable to read image data\n");
				  else
				    {
				      /*fprintf (stderr, "read image %dx%d, %d\n", image_width, image_height, image_depth); */
				      file_path[file_count++] = name;
				    }
				}
			    }
			}
		    }
		}
	      fclose (img);
	    }
	}
    }
}

/* if you are interested, the following code can detect hidden passwords
 * stored in CP-masked-JPEG files.
 */

/*
 * sorry, the detect code was removed ;)
 */

/*----------------------------------------------------------------------------*/

static u8 cp_key[] =
{
0x10, 0x17, 0x13, 0x15, 0x09, 0x08, 0x0a, 0x14, 0x06, 0x05, 0x16, 0x02, 0x0d,
0x03, 0x01, 0x04, 0x19, 0x0c, 0x0f, 0x0e, 0x12, 0x07, 0x0b, 0x18, 0x11, 0x1a
};

static cp_table_lu1[MAX_CP_WIDTH];
static cp_table_lu2[MAX_CP_WIDTH];

/* this is a bottleneck */
static void
cp_set_pw (u8 * pw, u8 * pw_end)
{
  u8 *cursor;
  int i, j, x, y, len;
  int *table;
  cp_cell *trans;

  len = pw_end - pw;

  for (i = x = y = 0, table = cp_table, trans = cp_trans;
       i < cp_cells;
       i++)
    {
      *table++ = -1;
      trans->dst_x = x;
      trans->dst_y = y;
      trans->mirror = 0;
      trans++;

      if (++x == cp_width)
	{
	  x = 0;
	  y++;
	}
    }

  x = cp_cells - 1;
  y = len + cp_cells % len;

  for (i = 0, cursor = pw; i < cp_cells; i++)
    {
      x = cp_key[*cursor - 'A'] + x + y;

      if (++cursor == pw_end)
	cursor = pw;

      if (x >= cp_cells)
	x -= cp_cells;

      if (x >= cp_cells)
	x -= cp_cells;

      while (cp_table[x] != -1)
	{
	  ++x;
	  if (x >= cp_cells)
	    x = 0;
	}

      cp_table[x] = i;
      y++;

      if (++i >= cp_cells)
	break;

      x = cp_key[*cursor - 'A'] + x + y;

      if (++cursor == pw_end)
	cursor = pw;

      if (x >= cp_cells)
	x -= cp_cells;

      if (x >= cp_cells)
	x -= cp_cells;

      while (cp_table[x] != -1)
	{
	  if (x == 0)
	    x = cp_cells;

	  x--;
	}

      cp_table[x] = i;
      y++;
    }

  for (i = 0, j = cp_cells - 1; i < j; i++, j--)
    {
      cp_trans[cp_table[i]].dst_x = cp_table_lu1[j];
      cp_trans[cp_table[i]].dst_y = cp_table_lu2[j];
      cp_trans[cp_table[j]].dst_x = cp_table_lu1[i];
      cp_trans[cp_table[j]].dst_y = cp_table_lu2[i];

      if ((cp_table[i] ^ cp_table[j]) & 1)
	{
	  cp_trans[cp_table[i]].mirror = 1;
	  cp_trans[cp_table[j]].mirror = 1;
	}
    }
}

/*----------------------------------------------------------------------------*/

static void
cp_do_mask (void)
{
  UI x, y, src_x, src_y;
  UI u, v, dst_x, dst_y;
  UI xx;
  int rot;
  cp_cell *cell = cp_trans;

  for (y = 0; y < cp_height; ++y)
    {
      for (x = 0; x < cp_width; ++x)
	{
	  {
	    u = cell->dst_x;
	    v = cell->dst_y;
	    rot = cell->mirror;
	    cell++;
	    dst_x = x * CP_CELL_SIZE + OFFSET_X;
	    dst_y = y * CP_CELL_SIZE + OFFSET_Y;
	    src_x = u * CP_CELL_SIZE + OFFSET_X;
	    src_y = v * CP_CELL_SIZE + OFFSET_Y;
	  }

#define   COPY(sx,sy,dx,dy) do { \
            u8 *s = &src_pixel (src_x + (sx), src_y + (sy), 0);	\
            u8 *d = &dst_pixel (dst_x + (dx), dst_y + (dy), 0);	\
            *d++ = *s++;	\
            *d++ = *s++;	\
            *d++ = *s++;	\
	  } while(0)

	  if (dst_x + CP_CELL_SIZE <= transform_width &&
	      dst_y + CP_CELL_SIZE <= transform_height)
	    {
	      if (rot)
		{
		  /* this is shit */
		  for (xx = CP_CELL_SIZE - 1; xx--;)
		    {
		      COPY (xx + 1, 0, 0, xx + 1);
		      COPY (0, xx, xx, 0);
		      COPY (xx, CP_CELL_SIZE - 1, CP_CELL_SIZE - 1, xx);
		      COPY (CP_CELL_SIZE - 1, xx + 1, xx + 1, CP_CELL_SIZE - 1);
		    }
		}
	      else
		{
		  for (xx = CP_CELL_SIZE; xx--;)
		    {
		      COPY (xx + 1, 0, xx + 1, 0);
		      COPY (0, xx, 0, xx);
		      COPY (xx, CP_CELL_SIZE - 1, xx, CP_CELL_SIZE - 1);
		      COPY (CP_CELL_SIZE - 1, xx + 1, CP_CELL_SIZE - 1, xx + 1);
		    }
		}
	    }

#undef COPY
	}
    }
//  {FILE *f=fopen("x.ppm","wb"); fprintf (f,"P6 %d %d %d ",transform_width,transform_height,255); fwrite(transform_data,transform_width*transform_height*CP_BPP,1,f); fclose(f); }
}

static void
cp_cleanup (void)
{
  if (cp_table)
    free (cp_table);

  if (cp_trans)
    free (cp_trans);
}

static void
init_cpmask (void)
{
  UI i;

  assert (image_data);
  cp_width = image_width / CP_CELL_SIZE;
  cp_height = image_height / CP_CELL_SIZE;
  cp_cells = cp_width * cp_height;
  cp_table = (int *) malloc (sizeof (int) * cp_cells);
  cp_trans = (cp_cell *) malloc (sizeof (cp_cell) * cp_cells);

  if (cp_width > MAX_CP_WIDTH)
    {
      printf ("maximum image width in this version is %d\n", MAX_CP_WIDTH * CP_CELL_SIZE);
      exit (1);
    }

  for (i = 0; i < MAX_CP_WIDTH; i++)
    {
      cp_table_lu1[i] = i % cp_width;
      cp_table_lu2[i] = i / cp_width;
    }

  transform_width = image_width < CP_CELL_SIZE * MAX_SIZE ? image_width : CP_CELL_SIZE * MAX_SIZE;
  transform_height = image_height < CP_CELL_SIZE * MAX_SIZE ? image_height : CP_CELL_SIZE * MAX_SIZE;

  transform_data = (u8 *) malloc (transform_width * transform_height * CP_BPP);
}

static int
crack_cpmask (gen_func genfunc, callback_func cbfunc)
{
  unsigned long minimum = 1 << 31;
  unsigned long current;
  int changed = -1;
  int x, y;

  do
    {
      if (changed >= 4 && verbosity)
	printf ("checking pw %s\r", pw), fflush (stdout);

      if (changed < 0)
	pw_end = pw + strlen (pw);

      cp_set_pw (pw, pw_end);
      cp_do_mask ();

#define P(x,y,c) (UI)dst_pixel((x),(y),(c))
      current = 0;

      for (x = CP_CELL_SIZE; x < transform_width; x += CP_CELL_SIZE)
	for (y = transform_height; y--;)
	  {
	    current += abs (P (x - 1, y, 0) - P (x, y, 0));
	    current += abs (P (x - 1, y, 1) - P (x, y, 1));
	    current += abs (P (x - 1, y, 2) - P (x, y, 2));

            if (current > minimum)
              goto overflow;
	  }

      for (y = CP_CELL_SIZE; y < transform_height && current < minimum; y += CP_CELL_SIZE)
	for (x = transform_width; x-- && current < minimum;)
	  {
	    current += abs (P (x, y - 1, 0) - P (x, y, 0));
	    current += abs (P (x, y - 1, 1) - P (x, y, 1));
	    current += abs (P (x, y - 1, 2) - P (x, y, 2));

            if (current > minimum)
              goto overflow;
	  }
#undef P

overflow:
      if (current < minimum)
	{
	  char info[80];

	  minimum = current + 99;

	  sprintf (info, "badness %ld", current);

	  if ((changed = cbfunc (pw, info)))
	    return changed;
	}

    }
  while ((changed = genfunc ()));

  return 0;
}