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 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
|
/* Copyright (C) 2000-2004 Peter Selinger.
This file is part of ccrypt. It is free software and it is covered
by the GNU general public license. See the file COPYING for details. */
/* ccrypt.c: high-level functions for accessing ccryptlib */
/* $Id: ccrypt.c,v 1.10 2003/08/25 18:06:19 selinger Exp $ */
/* ccrypt implements a stream cipher based on the block cipher
Rijndael, the candidate for the AES standard. */
#define _FILE_OFFSET_BITS 64 /* turn off 2GB limit on file size */
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include "main.h"
#include "ccryptlib.h"
#include "unixcryptlib.h"
/* heuristically, the fastest inbufsize is 992 - this is slightly, but
significantly, faster than 1024 or very large buffer sizes. I'm not
sure why this is - maybe some strange interaction between the
filesystem blocksize and the page size? */
#define INBUFSIZE 992
#define MIDBUFSIZE 1024 /* for key change */
#define OUTBUFSIZE 1056
/* ---------------------------------------------------------------------- */
/* ccrypt error messages. These correspond to the error codes returned
by the ccrypt functions. Note: the error code -1 corresponds to a
system error, with errno set, and the error code -2 corresponds to
a ccrypt error, with ccrypt_errno set. */
const char *ccrypt_error(int st) {
if (st == -1) {
return strerror(errno);
} else if (st == -2) {
return ccrypt_errstr[ccrypt_errno];
} else {
return "unknown error";
}
}
/* ---------------------------------------------------------------------- */
/* keychange = compose decryption and encryption */
struct keychange_state_s {
ccrypt_stream_t b1;
ccrypt_stream_t b2;
int iv; /* count-down for IV bytes */
char buf[MIDBUFSIZE];
};
typedef struct keychange_state_s keychange_state_t;
static int keychange_init(ccrypt_stream_t *b, char *key1, char *key2) {
keychange_state_t *st;
int r;
st = malloc(sizeof(keychange_state_t));
if (st == NULL) {
return -1;
}
b->state = (void *)st;
r = ccdecrypt_init(&st->b1, key1, 0);
if (r) {
return r;
}
r = ccencrypt_init(&st->b2, key2);
if (r) {
return r;
}
st->b2.next_in = &st->buf[0];
st->b2.avail_in = 0;
st->iv = 32; /* count down IV bytes */
return 0;
}
static int keychange(ccrypt_stream_t *b) {
keychange_state_t *st = (keychange_state_t *)b->state;
int r;
/* note: we do not write anything until we have seen 32 bytes of
input. This way, we don't write the output IV until the input IV
has been verified. */
while (1) {
/* clear mid-buffer */
if (b->avail_out && !st->iv) {
st->b2.next_out = b->next_out;
st->b2.avail_out = b->avail_out;
r = ccencrypt(&st->b2);
if (r) {
return r;
}
b->next_out = st->b2.next_out;
b->avail_out = st->b2.avail_out;
}
/* if mid-buffer not empty, or no input available, stop */
if (st->b2.avail_in != 0 || b->avail_in == 0) {
break;
}
/* fill mid-buffer */
st->b1.next_out = &st->buf[0];
st->b1.avail_out = MIDBUFSIZE;
st->b1.next_in = b->next_in;
st->b1.avail_in = b->avail_in;
r = ccdecrypt(&st->b1);
if (r) {
return r;
}
if (st->iv) {
st->iv -= b->avail_in - st->b1.avail_in;
if (st->iv <= 0) {
st->iv = 0;
}
}
b->next_in = st->b1.next_in;
b->avail_in = st->b1.avail_in;
st->b2.next_in = &st->buf[0];
st->b2.avail_in = st->b1.next_out - st->b2.next_in;
}
return 0;
}
static int keychange_end(ccrypt_stream_t *b) {
keychange_state_t *st = (keychange_state_t *)b->state;
int r, cerr, err;
r = ccdecrypt_end(&st->b1);
if (r) {
cerr = ccrypt_errno;
err = errno;
ccencrypt_end(&st->b2);
free(b->state);
b->state = NULL;
ccrypt_errno = cerr;
errno = err;
return r;
}
r = ccencrypt_end(&st->b2);
if (r) {
cerr = ccrypt_errno;
err = errno;
free(b->state);
b->state = NULL;
ccrypt_errno = cerr;
errno = err;
return r;
}
free(b->state);
b->state = NULL;
return 0;
}
/* ---------------------------------------------------------------------- */
/* encryption/decryption of streams */
typedef int initfun(ccrypt_stream_t *b, char *key);
typedef int workfun(ccrypt_stream_t *b);
typedef int endfun(ccrypt_stream_t *b);
/* apply ccrypt_stream to pipe stuff from fin to fout. Assume the
ccrypt_stream has already been initialized. */
static int streamhandler(ccrypt_stream_t *b, workfun *work, endfun *end,
FILE *fin, FILE *fout) {
/* maybe should align buffers on page boundary */
char inbuf[INBUFSIZE], outbuf[OUTBUFSIZE];
int eof = 0;
int r;
int cerr, err;
b->avail_in = 0;
while (1) {
/* fill input buffer */
if (b->avail_in == 0 && !eof) {
r = fread(inbuf, 1, INBUFSIZE, fin);
b->next_in = &inbuf[0];
b->avail_in = r;
if (r<INBUFSIZE) {
eof = 1;
}
}
/* prepare output buffer */
b->next_out = &outbuf[0];
b->avail_out = OUTBUFSIZE;
/* do some work */
r = work(b);
if (r) {
cerr = ccrypt_errno;
err = errno;
end(b);
ccrypt_errno = cerr;
errno = err;
return r;
}
/* process output buffer */
if (b->avail_out < OUTBUFSIZE) {
fwrite(outbuf, 1, OUTBUFSIZE-b->avail_out, fout);
}
if (eof && b->avail_out != 0) {
break;
}
}
r = end(b);
if (r) {
return r;
}
return 0;
}
int ccencrypt_streams(FILE *fin, FILE *fout, char *key) {
ccrypt_stream_t ccs;
ccrypt_stream_t *b = &ccs;
int r;
r = ccencrypt_init(b, key);
if (r) {
return r;
}
return streamhandler(b, ccencrypt, ccencrypt_end, fin, fout);
}
int ccdecrypt_streams(FILE *fin, FILE *fout, char *key) {
ccrypt_stream_t ccs;
ccrypt_stream_t *b = &ccs;
int r;
int flags = 0;
if (cmd.mismatch) {
flags |= CCRYPT_MISMATCH;
}
r = ccdecrypt_init(b, key, flags);
if (r) {
return r;
}
return streamhandler(b, ccdecrypt, ccdecrypt_end, fin, fout);
}
int cckeychange_streams(FILE *fin, FILE *fout, char *key1, char *key2) {
ccrypt_stream_t ccs;
ccrypt_stream_t *b = &ccs;
int r;
r = keychange_init(b, key1, key2);
if (r) {
return r;
}
return streamhandler(b, keychange, keychange_end, fin, fout);
}
int unixcrypt_streams(FILE *fin, FILE *fout, char *key) {
ccrypt_stream_t ccs;
ccrypt_stream_t *b = &ccs;
int r;
r = unixcrypt_init(b, key);
if (r) {
return r;
}
return streamhandler(b, unixcrypt, unixcrypt_end, fin, fout);
}
/* ---------------------------------------------------------------------- */
/* destructive encryption/decryption of files */
/* A large value of FILEINBUFSIZE keeps the cost of "lseek" down. It
is okay for FILEINBUFSIZE to be much larger than MIDBUFSIZE.
FILEOUTBUFSIZE must be large enough to hold the encryption of
FILEINBUFSIZE in one piece, or otherwise there will be a buffer
overflow error. */
#define FILEINBUFSIZE 10240
#define FILEOUTBUFSIZE (FILEINBUFSIZE+32)
/* apply ccrypt_stream to destructively update (and resize) the given
fd, which must be opened in read/write mode and seekable.
Encryption will begin at the current file position (normally 0),
and extend until the end of the file. Note: this only works if the
stream encoder b/work/end expands its input by at most
FILEINBUFSIZE bytes; otherwise there will be a buffer overflow
error. */
static int filehandler(ccrypt_stream_t *b, workfun *work, endfun *end,
int fd) {
/* rp = reader's position, wp = writer's position, fp = file position */
int p; /* rp-wp */
char inbuf[FILEINBUFSIZE];
char outbuf[FILEINBUFSIZE+32];
int inbufsize, outbufsize;
off_t offs;
int r;
int i;
int eof=0;
int err, errc;
p = 0;
outbufsize = 0;
while (1) {
/* file is at position wp */
if (p != 0) {
r = lseek(fd, p, SEEK_CUR);
if (r == -1) {
goto error;
}
}
/* file is at position rp */
/* read block */
i = 0;
while (i<FILEINBUFSIZE && !eof) {
r = read(fd, inbuf+i, FILEINBUFSIZE-i);
if (r == -1) {
goto error;
} else if (r==0) {
eof = 1;
}
i += r;
}
p += i;
inbufsize = i;
/* file is at position rp */
if (p != 0) {
r = lseek(fd, -p, SEEK_CUR);
if (r == -1) {
goto error;
}
}
/* file is at position wp */
/* write previous block */
if (outbufsize > p && !eof) {
ccrypt_errno = CCRYPT_EBUFFER; /* buffer overflow; should never happen */
r = -2;
goto error;
}
if (outbufsize != 0) {
i = 0;
while (i<outbufsize) {
r = write(fd, outbuf+i, outbufsize-i);
if (r == -1) {
goto error;
}
i += r;
}
p -= outbufsize;
outbufsize = 0;
}
/* encrypt block */
b->next_in = inbuf;
b->avail_in = inbufsize;
b->next_out = outbuf;
b->avail_out = FILEINBUFSIZE+32;
r = work(b);
if (r) {
goto error;
}
if (b->avail_in != 0) {
ccrypt_errno = CCRYPT_EBUFFER; /* buffer overflow; should never happen */
r = -2;
goto error;
}
inbufsize = 0;
outbufsize = FILEINBUFSIZE+32-b->avail_out;
if (eof && outbufsize == 0) { /* done */
break;
}
}
/* file is at position wp */
/* close the stream (we need to do this before truncating, because
there might be an error!) */
r = end(b);
if (r) {
return r;
}
/* truncate the file to where it's been written */
r = offs = lseek(fd, 0, SEEK_CUR);
if (r == -1) {
return -1;
}
r = ftruncate(fd, offs);
if (r == -1) {
return -1;
}
return 0;
error:
err = errno;
errc = ccrypt_errno;
end(b);
errno = err;
ccrypt_errno = errc;
return r;
}
int ccencrypt_file(int fd, char *key) {
ccrypt_stream_t ccs;
ccrypt_stream_t *b = &ccs;
int r;
r = ccencrypt_init(b, key);
if (r) {
return r;
}
return filehandler(b, ccencrypt, ccencrypt_end, fd);
}
int ccdecrypt_file(int fd, char *key) {
ccrypt_stream_t ccs;
ccrypt_stream_t *b = &ccs;
int r;
r = ccdecrypt_init(b, key, 0);
if (r) {
return r;
}
return filehandler(b, ccdecrypt, ccdecrypt_end, fd);
}
int cckeychange_file(int fd, char *key1, char *key2) {
ccrypt_stream_t ccs;
ccrypt_stream_t *b = &ccs;
int r;
r = keychange_init(b, key1, key2);
if (r) {
return r;
}
return filehandler(b, keychange, keychange_end, fd);
}
int unixcrypt_file(int fd, char *key) {
ccrypt_stream_t ccs;
ccrypt_stream_t *b = &ccs;
int r;
r = unixcrypt_init(b, key);
if (r) {
return r;
}
return filehandler(b, unixcrypt, unixcrypt_end, fd);
}
|