File: encrypt.c

package info (click to toggle)
gifshuffle 2.0-1
  • links: PTS
  • area: main
  • in suites: bookworm, bullseye, buster, forky, sid, stretch, trixie
  • size: 180 kB
  • ctags: 173
  • sloc: ansic: 2,113; makefile: 17
file content (235 lines) | stat: -rw-r--r-- 3,641 bytes parent folder | download | duplicates (2)
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
/*
 * Encryption routines for the gifshuffle steganography program.
 * Uses the ICE encryption algorithm in 1-bit cipher-feedback (CFB) mode.
 *
 * Written by Matthew Kwan - December 1996
 */

#include "gifshuf.h"
#include "ice.h"

#include <string.h>


/*
 * The key to use for encryption/decryption.
 */

static ICE_KEY		*ice_key = NULL;
static unsigned char	encrypt_iv_block[8];


/*
 * Build the ICE key from the supplied password.
 * Only uses the lower 7 bits from each character.
 */

void
password_set (
	const char	*passwd
) {
	int		i, level;
	unsigned char	buf[1024];

	level = (strlen (passwd) * 7 + 63) / 64;

	if (level == 0) {
	    if (!quiet_flag)
		fprintf (stderr, "Warning: an empty password is being used\n");
	    level = 1;
	} else if (level > 128) {
	    if (!quiet_flag)
		fprintf (stderr, "Warning: password truncated to 1170 chars\n");
	    level = 128;
	}

	if ((ice_key = ice_key_create (level)) == NULL) {
	    if (!quiet_flag)
		fprintf (stderr, "Warning: failed to set password\n");
	    return;
	}

	for (i=0; i<1024; i++)
	    buf[i] = 0;

	i = 0;
	while (*passwd != '\0') {
	    unsigned char	c = *passwd & 0x7f;
	    int			idx = i / 8;
	    int			bit = i & 7;

	    if (bit == 0) {
		buf[idx] = (c << 1);
	    } else if (bit == 1) {
		buf[idx] |= c;
	    } else {
		buf[idx] |= (c >> (bit - 1));
		buf[idx + 1] = (c << (9 - bit));
	    }

	    i += 7;
	    passwd++;

	    if (i > 8184)
		break;
	}

	ice_key_set (ice_key, buf);

		/* Set the initialization vector with the key
		 * with itself.
		 */
	ice_key_encrypt (ice_key, buf, encrypt_iv_block);
}


/*
 * Are we doing colourmap ordering encryption?
 */

BOOL
encrypting_colourmap (void)
{
	return (!v1_flag && ice_key != NULL);
}


/*
 * Encrypt a colour.
 */

void
encrypt_colour (
	unsigned char	r,
	unsigned char	g,
	unsigned char	b,
	unsigned char	*ctext
) {
	unsigned char	ptext[8];
	int		i;

	if (ice_key == NULL)
	    return;

	ptext[0] = r;
	ptext[1] = g;
	ptext[2] = b;

	for (i=3; i<8; i++)
	    ptext[i] = 0;

	ice_key_encrypt (ice_key, ptext, ctext);
}


/*
 * Initialize the encryption routines.
 */

void
encrypt_init (void)
{
	encode_init ();
}


/*
 * Encrypt a single bit.
 */

BOOL
encrypt_bit (
	int		bit,
	FILE		*inf,
	FILE		*outf
) {
	int		i;
	unsigned char	buf[8];

	if (ice_key == NULL)
	    return (encode_bit (bit, inf, outf));

	ice_key_encrypt (ice_key, encrypt_iv_block, buf);
	if ((buf[0] & 128) != 0)
	    bit = !bit;

			/* Rotate the IV block one bit left */
	for (i=0; i<8; i++) {
	    encrypt_iv_block[i] <<= 1;
	    if (i < 7 && (encrypt_iv_block[i+1] & 128) != 0)
		encrypt_iv_block[i] |= 1;
	}
	encrypt_iv_block[7] |= bit;

	return (encode_bit (bit, inf, outf));
}


/*
 * Flush the contents of the encryption routines.
 */

BOOL
encrypt_flush (
	FILE		*inf,
	FILE		*outf
) {
	return (encode_flush (inf, outf));
}


/*
 * Initialize the decryption routines.
 */

void
decrypt_init (void)
{
	uncompress_init ();
}


/*
 * Decrypt a single bit.
 */

BOOL
decrypt_bit (
	int		bit,
	FILE		*outf
) {
	int		i;
	int		nbit;
	unsigned char	buf[8];

	if (ice_key == NULL)
	    return (uncompress_bit (bit, outf));

	ice_key_encrypt (ice_key, encrypt_iv_block, buf);
	if ((buf[0] & 128) != 0)
	    nbit = !bit;
	else
	    nbit = bit;

			/* Rotate the IV block one bit left */
	for (i=0; i<8; i++) {
	    encrypt_iv_block[i] <<= 1;
	    if (i < 7 && (encrypt_iv_block[i+1] & 128) != 0)
		encrypt_iv_block[i] |= 1;
	}
	encrypt_iv_block[7] |= bit;

	return (uncompress_bit (nbit, outf));
}


/*
 * Flush the contents of the decryption routines.
 */

BOOL
decrypt_flush (
	FILE		*outf
) {
	return (uncompress_flush (outf));
}