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
|
/*
* Encryption routines for the SNOW steganography program.
* Uses the ICE encryption algorithm in 1-bit cipher-feedback (CFB) mode.
*
* Copyright (C) 1999 Matthew Kwan
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* For license text, see https://spdx.org/licenses/Apache-2.0>.
*/
#include <string.h>
#include "snow.h"
#include "ice.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);
}
/*
* 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
) {
ice_key_destroy (ice_key);
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
) {
ice_key_destroy (ice_key);
return (uncompress_flush (outf));
}
|