Skip to content

Commit

Permalink
MiniZipAEWrite enhanced
Browse files Browse the repository at this point in the history
- plain text inversion moved out of MiniZipAEWrite
- switches to Store method if compressor does not work or emits more bytes than source
  • Loading branch information
maxpat78 committed Mar 8, 2023
1 parent 989d684 commit 8c3e31b
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 32 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,4 @@ Network Trash Folder
Temporary Items
.apdisk
BUGS-TODO.TXT
CryptoPad/build.bat
27 changes: 25 additions & 2 deletions CryptoPad/CryptoPad.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@

extern void AskPassword(BOOL bForceOpen);

void memrev(unsigned char* m, unsigned int l) {
unsigned char* t = m;
unsigned char* b = m + l - 1;
while (b > t) {
unsigned char c = *t;
*t = *b; *b = c;
t++; b--;
}
}

#define APP_TITLE _T("CryptoPad")

enum Encodings {
Expand Down Expand Up @@ -288,7 +298,10 @@ int LoadFile()
}

dwRead = MiniZipAERead(lpBuffer, dwInSize, &dst, (unsigned long*)&dwOutSize, document_password);


if (*(lpBuffer+dwInSize-1) == 0x52) // if V2 doc format
memrev(dst, dwOutSize);

if (dwRead == MZAE_ERR_SUCCESS)
{
Free(lpBuffer);
Expand Down Expand Up @@ -478,15 +491,24 @@ BOOL SaveFile(int size)
if (document_password && document_password[0])
{
DWORD dwOutSize = 0;

memrev(p, size); // reverses source buffer (V2 document format)
// calc and alloca reqd buf size
err = MiniZipAEWrite(p, size, &dst, (unsigned long*)&dwOutSize, document_password);

if (err == MZAE_ERR_SUCCESS)
{
dst = Malloc(dwOutSize);
if (!dst)
{
memrev(p, size);
return FALSE;
}
}
else goto aeerr;

err = MiniZipAEWrite(p, size, &dst, (unsigned long*)&dwOutSize, document_password);
memrev(p, size);
if (err != MZAE_ERR_SUCCESS)
{
Free(dst);
Expand Down Expand Up @@ -713,7 +735,8 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
case EN_UPDATE:
{
// Signals modified buffer premitting '*' in window title
if (szWindowTitle[0] != _T('*')) {
if (szWindowTitle[0] != _T('*'))
{
int i;
GetWindowText(hwnd, szWindowTitle, sizeof(szWindowTitle));
for (i=lstrlen(szWindowTitle); i >= 0; i--) // move string forward in-place
Expand Down
52 changes: 24 additions & 28 deletions CryptoPad/MZAE_minizip.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
My simplified document format imposes:
1) a fixed filename length of 4 bytes ("data");
2) a single extra field (the AES header);
3) Deflate compression always;
3) Deflate compression (but Store if bigger);
4) 256-bit key strength (but can decrypt with smaller keys);
5) text encoded in UTF-8 with BOM, CR-LF ended;
6) text reversed before compression (V2) and thus
Expand Down Expand Up @@ -58,7 +58,7 @@
version 2 bytes (1 or 2)
ZIP vendor 2 bytes (actually, AE)
strength 1 byte (AES key bits: 1=128, 2=192, 3=256)
actual compression 2 byte (becomes 0x63 in LENT & CENT)
actual compression 2 byte (0=Stored, 8=Deflated; becomes 0x63 in LENT & CENT)
content data, as follows:
random salt (8, 12 or 16 byte depending on key size)
Expand Down Expand Up @@ -119,28 +119,17 @@
#define BS32(x) (x & 0xFF000000) >> 24 | ((x & 0xFF0000) >> 16) << 8 | ((x & 0xFF00) >> 8) << 16 | (x & 0xFF) << 24
#endif

#define memrev(m, l) { \
char *t = m; \
char *b = m+l-1; \
while (b > t) { \
char c = *t; \
*t=*b; *b=c; \
t++; b--; \
} \
}

int MiniZipAEWrite(char* src, unsigned long srcLen, char** dst, unsigned long *dstLen, char* password)
{
char *tmpbuf = NULL;
unsigned int buflen;
unsigned int buflen, ret, method=8;
long crc = 0;
char salt[16];
char* aes_key;
char* hmac_key;
char* vv;
char *ppbuf;
char *digest, *p;
char* revSrc;
unsigned char ucLocalHeader[45] = {
0x50, 0x4B, 0x03, 0x04, 0x33, 0x00, 0x01, 0x00,
0x63, 0x00, 0x00, 0x00, 0x21, 0x00, 0x00, 0x00,
Expand Down Expand Up @@ -172,18 +161,20 @@ int MiniZipAEWrite(char* src, unsigned long srcLen, char** dst, unsigned long *d
if (!srcLen)
return MZAE_ERR_PARAMS;

// Reverse source buffer copy
revSrc = (char*) malloc(srcLen);
memcpy(revSrc, src, srcLen);
memrev(revSrc, srcLen)

if (MZAE_deflate(revSrc, srcLen, &tmpbuf, &buflen))
return MZAE_ERR_CODEC;

ret = MZAE_deflate(src, srcLen, &tmpbuf, &buflen);

// Switch from Deflate to Store if error or disadvantage
if (ret || buflen >= srcLen)
{
method = 0;
buflen = srcLen;
}

if (! *dstLen)
{
*dstLen = buflen + 45 + 28 + 61 + 23; //(45+28)+61+23
free(tmpbuf);
if (tmpbuf)
free(tmpbuf);
return MZAE_ERR_SUCCESS;
}

Expand Down Expand Up @@ -212,6 +203,13 @@ int MiniZipAEWrite(char* src, unsigned long srcLen, char** dst, unsigned long *d
return MZAE_ERR_KDF;
}

if (method == 0)
{
free(tmpbuf);
tmpbuf = (char*) malloc(buflen);
memcpy(tmpbuf, src, srcLen);
}

if (MZAE_ctr_crypt(aes_key, 32, tmpbuf, buflen, &ppbuf))
{
free(tmpbuf);
Expand Down Expand Up @@ -241,7 +239,7 @@ int MiniZipAEWrite(char* src, unsigned long srcLen, char** dst, unsigned long *d
crc = 0;
}
else {
crc = MZAE_crc(0, revSrc, srcLen);
crc = MZAE_crc(0, src, srcLen);
}

// Builds the ZIP Local File Header
Expand All @@ -254,6 +252,7 @@ int MiniZipAEWrite(char* src, unsigned long srcLen, char** dst, unsigned long *d
PDW(14, crc);
PDW(18, buflen+28);
PDW(22, srcLen);
PW(43, method);

// Copies the raw contents: salt, check word, encrypted data and HMAC
memcpy(p + 45, salt, 16);
Expand All @@ -274,6 +273,7 @@ int MiniZipAEWrite(char* src, unsigned long srcLen, char** dst, unsigned long *d
PDW(24, srcLen);
if (srcLen < 20)
PW(54, 2); // AE-2
PW(59, method);

p += 61;
memcpy(p, ucEndHeader, sizeof(ucEndHeader));
Expand All @@ -283,7 +283,6 @@ int MiniZipAEWrite(char* src, unsigned long srcLen, char** dst, unsigned long *d

free(tmpbuf);
free(ppbuf);
free(revSrc);

return MZAE_ERR_SUCCESS;
}
Expand Down Expand Up @@ -376,9 +375,6 @@ int MiniZipAERead(char* src, unsigned long srcLen, char** dst, unsigned long *ds
return MZAE_ERR_BADCRC;
}

if (*(src+srcLen-1) == 0x52) // If V2 format
memrev(*dst, uncompSize)

free(pbuf);

return MZAE_ERR_SUCCESS;
Expand Down
4 changes: 2 additions & 2 deletions CryptoPad/resource.rc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
#include "resource.h"

#define MY_FILE_VERSION "1.0.23"
#define MY_FILE_VERSION "1.0.24"

#define APSTUDIO_READONLY_SYMBOLS
/////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -74,7 +74,7 @@ END
//

VS_VERSION_INFO VERSIONINFO
FILEVERSION 1,0,0,23
FILEVERSION 1,0,0,24
PRODUCTVERSION 1,0,0,0
FILEFLAGSMASK 0x3fL
#ifdef _DEBUG
Expand Down

0 comments on commit 8c3e31b

Please sign in to comment.