Skip to content

Commit

Permalink
v7.2.2 cleaning compiler warnings and 2 patches.
Browse files Browse the repository at this point in the history
  • Loading branch information
circulosmeos committed Oct 5, 2015
1 parent 4b7c007 commit fc99336
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 32 deletions.
5 changes: 5 additions & 0 deletions Changes.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
v7.2.2

* patches: password was not correctly overwritten in memory after it wasn't needed. IVs weren't as random as intended. Both failures didn't compromise security of the encrypted file, AFAIK.
* code cleaning to eliminate most of the compiler warnings.

v7.2.1

* no fixes, improvements or additions: just little code compatibility changes with Android and others.
Expand Down
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ There's an [Android app available here](https://www.github.com/circulosmeos/trio

Features:

* Code can be compiled with any C99 compiler, no matter platform, endianness or word size (32-64 bits): it has been tested on Windows, linux, Solaris, HP-UX OS's and Intel/AMD, ARM and Itanium processors.
* Code can be compiled with any C99 compiler, no matter platform, endianness or word size (32-64 bits): it has been tested on Windows, linux, Solaris, HP-UX OS's and Intel/AMD, ARM, MIPS and Itanium processors.
* Same content produces different encrypted outputs every time. This is attained with a random initialization vector (IV) stored within the encrypted file.
* Files are (by default) encrypted/decrypted on-the-fly, so content is overwritten. This is interesting from a security point of view, as no clear content is left on disk.
* When decrypting, if password is not the one used for encrypting, the process is aborted, so the file cannot be rendered unusable. This behaviour is achieved thanks to a password hint stored within the encrypted file. (This hint can optionally be not stored: in this case the file could end up being decrypted with an incorrect password, so its contents would be irrecoverable.)
Expand All @@ -23,12 +23,11 @@ Features:
* Reduced program size: < 100 kiB on all platforms.
* [easily portable to Android](https://www.github.com/circulosmeos/triops.apk) as a JNI library. Check "ANDROID_LIBRARY" in the source code.
* Licensed as GPL v3.

Known limitations:

* Files higher than 4 GiB cannot be managed and will produce unexpected outputs: be warned!

Known limitations:

* Files higher than 4 GiB cannot be managed and will produce unexpected outputs in Windows: be warned!


Before compiling, check in triops.h that next values correctly adjust to your platform, modifying them as convenient:

Expand Down
2 changes: 1 addition & 1 deletion chacha20/include/crypto_stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,4 @@ int crypto_stream_xor(unsigned char *c, const unsigned char *m,
}
#endif

#endif
#endif
2 changes: 1 addition & 1 deletion chacha20/include/crypto_stream_xsalsa20.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,4 @@ int crypto_stream_xsalsa20_xor_ic(unsigned char *c, const unsigned char *m,
}
#endif

#endif
#endif
2 changes: 1 addition & 1 deletion chacha20/include/export.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@
# endif
#endif

#endif
#endif
2 changes: 1 addition & 1 deletion keccak/crypto_hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ const char *crypto_hash_primitive(void);
}
#endif

#endif
#endif
2 changes: 1 addition & 1 deletion keccak/export.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@
# endif
#endif

#endif
#endif
47 changes: 25 additions & 22 deletions triops.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@

#include <stdlib.h>
#include <stdio.h>
#include <time.h>

#ifdef ANDROID_LIBRARY
#include <jni.h>
Expand Down Expand Up @@ -74,7 +75,7 @@ int getch(void);



#define TRIOPS_VERSION "7.2.1"
#define TRIOPS_VERSION "7.2.2"
#define PROGRAM_NAME "triops"

#define BUFFERSIZE 16384 // for CHACHA20: multiple of 64 bytes to avoid bad implementation (http://goo.gl/DHCLz1)
Expand Down Expand Up @@ -142,18 +143,18 @@ union unionIV_v3



void truncateFile (LPBYTE);
BOOL obtainPassword (LPBYTE, LPBYTE);
void truncateFile (char *);
BOOL obtainPassword (char *szPassFile, char *szPass);
unsigned long long FileSize(char *);
void EliminatePasswords (LPBYTE szPassFile, LPBYTE szPass);
void EliminatePasswords (char *szPassFile, char *szPass);
#ifndef WINDOWS_PLATFORM
time_t obtainTimestampUnix (char *szFile, int iMarcaDeTiempo);
#else
void obtainTimestampWin (char *szFile, LPFILETIME lpLastWriteTime);
void writeTimestampWin (char *szFile, LPFILETIME lpLastWriteTime);
#endif

void truncateFileBySize ( LPBYTE, unsigned long long );
void truncateFileBySize ( char *, unsigned long long );

void LoadIVandHash_v3 (FILE *, LPBYTE, LPBYTE, char *);
int CheckKeyIsValid_v3 (LPSTR, LPBYTE, LPBYTE, LPDWORD, BOOL);
Expand Down Expand Up @@ -233,7 +234,7 @@ int local_triops (int argc, char* const argv[static 4])
#endif
{
unsigned long long nBytesSoFar;
unsigned long long nBytesRead, nBytesWritten;
unsigned long long nBytesRead;
FILE * hFile;
FILE * hFileOut;
FILE * hFileTail;
Expand Down Expand Up @@ -317,13 +318,14 @@ int local_triops (int argc, char* const argv[static 4])
}
} else {
// decrypting: so triops Version can be deduced from extension:
if (strlen(szFile)>=4)
if (strlen(szFile)>=4) {
if (strcmp( szFile+(strlen(szFile)-4), TRIOPS_V3_EXTENSION ) == 0 )
triopsVersion=TRIOPS_V3;
else {
printf ("\nDecrypting, but format could not be deduced from file extension.\nProcess aborted.\n");
return 1;
}
}
}
//.................................................
// (5)
Expand Down Expand Up @@ -444,7 +446,7 @@ int local_triops (int argc, char* const argv[static 4])
}
}

// use the iv to create a unique key for this file
// use the IV to create a unique key for this file
if (triopsVersion==TRIOPS_V3) {
CreateUniqueKey_v3 (uniqueKey_v3.keyW, key_v3.keyB, &(iv_v3.iv));
// it is not necessary to make a copy of the original IV, as CHACHA20 uses it as const *
Expand Down Expand Up @@ -654,7 +656,7 @@ int local_triops (int argc, char* const argv[static 4])
} else {
// the space destined to the hash is filled with all zeros value:
if (triopsVersion==TRIOPS_V3)
for (i=0; i++; i<HASHSIZE_v3) { matrix3[i]=0x0; }
for (i=0; i < HASHSIZE_v3; i++) { matrix3[i]=0x0; }
}
if (triopsVersion==TRIOPS_V3)
fwrite(matrix3, HASHSIZE_v3, 1, hFileTail );
Expand Down Expand Up @@ -728,7 +730,7 @@ unsigned long long FileSize( char *szFile )
// truncates the size of the file, deleting the encrypted file's data tail
// (or its equivalent size, which is the same).
void
truncateFile ( LPBYTE szFile )
truncateFile ( char *szFile )
{
long int bytesToTruncate;
if (triopsVersion==TRIOPS_V3)
Expand All @@ -740,7 +742,7 @@ truncateFile ( LPBYTE szFile )
// truncates the size of the file, deleting bytesToTruncate bytes from the encrypted file's tail
// (or its equivalent size, which is the same).
void
truncateFileBySize ( LPBYTE szFile, unsigned long long bytesToTruncate )
truncateFileBySize ( char *szFile, unsigned long long bytesToTruncate )
{
// this check is needed because the file to truncate can be smaller !
if ( FileSize(szFile) < bytesToTruncate ) {
Expand All @@ -765,6 +767,7 @@ truncateFileBySize ( LPBYTE szFile, unsigned long long bytesToTruncate )
}
if (chsize(iFile, filelength(iFile) - bytesToTruncate )) {
printf ("Error while modifying file. Hope nothing changed, but can't assure that.\n");
close (iFile);
exit (-3);
}
close (iFile);
Expand All @@ -780,10 +783,10 @@ truncateFileBySize ( LPBYTE szFile, unsigned long long bytesToTruncate )
// if the fs path passed starts and ends with '_' char,
// the enclosed string is the password itself.
BOOL
obtainPassword (LPBYTE szFile, LPBYTE szPass)
obtainPassword (char *szFile, char *szPass)
{
FILE * hFile;
DWORD nBytesRead;
unsigned long long nBytesRead;
BYTE lpFileBuffer [BUFFERSIZE];
int i, c;
unsigned long long lFileSize;
Expand Down Expand Up @@ -892,7 +895,7 @@ obtainPassword (LPBYTE szFile, LPBYTE szPass)
{
lBlockNumber++;
// size_t fread(void *ptr, size_t size, size_t n, FILE *stream);
nBytesRead=fread(lpFileBuffer, BUFFERSIZE, 1, hFile);
nBytesRead=(unsigned long long)fread(lpFileBuffer, BUFFERSIZE, 1, hFile);
if (nBytesRead || feof(hFile) )
{
if (feof(hFile)) {
Expand All @@ -902,7 +905,7 @@ obtainPassword (LPBYTE szFile, LPBYTE szPass)
nBytesRead = nBytesRead * (unsigned long long)BUFFERSIZE;
}
if (triopsVersion==TRIOPS_V3) {
sph_keccak512(&mc, lpFileBuffer, nBytesRead);
sph_keccak512(&mc, lpFileBuffer, (size_t)nBytesRead);
}

}
Expand All @@ -927,15 +930,15 @@ obtainPassword (LPBYTE szFile, LPBYTE szPass)

// password is not needed anymore: variables are filled not to reside in memory,
// as a paranoic security measure:
void EliminatePasswords(LPBYTE szPassFile, LPBYTE szPass)
void EliminatePasswords(char *szPassFile, char *szPass)
{
int i;

// both variables are filled: szPassFile isn't needed anymore anyway.
for (i=0; i++; i<MAX_PATH) {
for (i=0; i < MAX_PATH; i++) {
szPassFile[i]=0xff;
}
for (i=0; i++; i<MAX_PASSWORD_LENGTH) {
for (i=0; i < MAX_PASSWORD_LENGTH; i++) {
szPass[i]=0xff;
}

Expand Down Expand Up @@ -1006,7 +1009,7 @@ void writeTimestampWin(char *szFile, LPFILETIME lpLastWriteTime)
NULL); // no attr. template
if (hFile == INVALID_HANDLE_VALUE) {
printf("warning: can't write attributtes for file '%s' (error: %d)\n",
szFile, GetLastError());
szFile, (int)GetLastError());
} else {
if ( SetFileTime(
hFile, // identifies the file
Expand Down Expand Up @@ -1120,13 +1123,13 @@ CheckKeyIsValid_v3 (LPSTR szPass, LPBYTE lpKey, LPBYTE lpIV, LPDWORD lpHashedKey

// some hashes more
for (i=0; i<500; i++) {
crypto_hash(szTemp, szTemp, HASHSIZE_v3);
crypto_hash((unsigned char *)szTemp, (unsigned char *)szTemp, HASHSIZE_v3);
}

memcpy((LPBYTE)(szTemp+HASHSIZE_v3), lpIV, IVSIZE_v3);

// hash again in hashedKey:
crypto_hash(testKey.keyB, szTemp, HASHSIZE_v3 + IVSIZE_v3);
crypto_hash(testKey.keyB, (unsigned char *)szTemp, HASHSIZE_v3 + IVSIZE_v3);
// .................................................

// now verify against the stored hashed key
Expand Down Expand Up @@ -1193,7 +1196,7 @@ createIV_v3 ( LPIV_v3 iv, char *szFile )
crypto_hash(cTempHash, (unsigned char *)iv, IVSIZE_v3);

// as KECCAK-512 produces 512 bits, let's get just some bytes:
for (i=0;i++;i<8) {
for (i=0; i < 8; i++) {
((unsigned char*)iv)[i] = cTempHash[i*4];
}

Expand Down

0 comments on commit fc99336

Please sign in to comment.