/* * Copyright (C) (2004) (Mario Juric) * * 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., 675 Mass Ave, Cambridge, MA 02139, USA. */ #ifndef seekcrypt_internal #define seekcrypt_internal #include "encio.h" #if HAVE_CONFIG_H #include #endif #include #include #include #ifndef TRUE #define TRUE 1 #endif #ifndef FALSE #define FALSE 0 #endif /* ENCFILE file modes */ #define WRITABLE 0x01 #define READABLE 0x02 typedef unsigned char byte; /* extra.c */ int identify_algorihtm(char* keymode); void * passphrase_to_key( /* convert a passphrase to key, using hashing algorithm */ const char *passphrase, unsigned int *len, /* passphrase, passphrase length (on output, generated key length) */ char* keymode, /* hashing method */ int keysize, /* key length to be generated */ void *salt, int salt_size); int read_header( /* read MCRYPT file header from file FSTREAM */ FILE * fstream, /* [in] input stream */ char *cipher, /* [out] encryption cipher */ char *mode, /* [out] encryption mode (cfb, ccb, ...) */ char *keymode, int *keysize, /* [out] key hash algorithm, key size */ void *salt, int *salt_size, /* [out] salt, salt size */ int *noiv, /* [out] is IV present in the file */ int *hash_algorithm /* [out] checksum hash algorithm */ ); int write_header( FILE * filedes, char *cipher, char *mode, char *keymode, int *keysize, void *salt, int salt_size ); void *read_iv(FILE* fstream, int ivsize); /* bit manipulation functions/macros */ unsigned int m_setbit(unsigned int which, unsigned int fullnum, unsigned int what); #define m_getbit(n, v) (((unsigned int)(v) >> (unsigned)(n)) & 1) #define i_setbit(n, v) ((unsigned int)(v) | (1U << (unsigned)(n))) #define i_unsetbit(n, v) ((unsigned int)(v) & ~(1U << (unsigned)(n))) /* aux functions */ void hexdump(char *text, void *data, int len); void *memdup(void *src, int length); void set_error(const char *errtext, ...); /* ufc_crypt.c */ int gen_crypt(void *keyword, int key_size, unsigned char *password, int plen); /* main.c */ #define DEFAULT_ALGORITHM "rijndael-128" /* The AES winner */ #define DEFAULT_MODE "cfb" /* I changed this compared to Mcrypt */ #define DEFAULT_PGP_MODE "ncfb" #define DEFAULT_PGP_ALGO "cast-128" #define DEFAULT_KEYMODE "mcrypt-sha1" #define DEFAULT_PGP_KEYMODE "s2k-isalted-sha1" #define SALT_SIZE 20 extern char *enc_cipher; /* encryption cipher */ extern char *enc_mode; /* encryption mode (cfb, ccb, ...) */ extern char *enc_keymode; /* hash algorithm for passphrase->key conversion */ extern int enc_checksum_algo; /* checksum hash algorithm */ /* Crypt-file & index structures */ typedef struct ENCINDEX_t /* Encrypted file index structure and support functions. */ { char *indexfile; // name of the file where the index is stored char *index; // should be thought of as 'indexblock index[len]' array int len; // length of the index array (in index blocks) int blocksize; // byte length of a serialized index block int bookmark_stride; // indexes are set every bytes in file } ENCINDEX; ENCINDEX *encindex_alloc(); void encindex_free (ENCINDEX *this); void encindex_unserialize (ENCINDEX *this, MCRYPT td, int idx); int encindex_serialize (ENCINDEX *this, MCRYPT td, int idx); struct ENCFILE_t /* Encrypted file structure and support functions. */ { FILE *fp; int rwmode; /* readable/writable flags */ int ptr; /* file pointer for ftell */ MCRYPT td; int blocksize; /* size of the encryption block (i.e. 256-bit encryption == 32) */ int enc_data_offset; /* offset to start of encrypted data */ int enc_data_length; /* length of encrypted data */ MHASH tm; /* hash object, for writable files */ int hash_size; /* length of hashed data */ int checksum_algo; /* hash algorithm */ /* for writable files, I'm not sure if this is even needed to be preserved after the initialization of mcrypt enc/dec engines */ byte *salt; /* salt used to salt the encryption key */ int salt_size; byte *key; /* encryption key, generated from passphrase */ int key_size; byte *IV; /* initialization vector, used to salt the encryption data */ int IV_size; ENCINDEX *idx; /* fseek index, if it exists */ }; ENCFILE *encfile_alloc(); void encfile_free(ENCFILE *this); /* main */ ENCINDEX *enc_iopen(ENCFILE *ef, const char *indexfile, const char *passphrase, int index_mode); ENCINDEX *enc_icreate(ENCFILE *ef, int buffer_size); int enc_fseek_index(ENCFILE *ef, long at, int dir, ENCINDEX *idx); ENCFILE *enc_fopen_readable(ENCFILE *ef, const char *passphrase); ENCFILE *enc_fopen_writable(ENCFILE *ef, const char *passphrase); void init_random(void); void deinit_random( void); #endif