diff --git a/Versions/build.txt b/Versions/build.txt index 476b093c4..aa886adb3 100644 --- a/Versions/build.txt +++ b/Versions/build.txt @@ -1 +1 @@ -2228 +2244 diff --git a/crypto/crypto.c b/crypto/crypto.c index e6161a5b5..5a3883ce9 100644 --- a/crypto/crypto.c +++ b/crypto/crypto.c @@ -36,7 +36,6 @@ see ecryption-doc.txt for details #define WKEY_LEN 256 #define KEY_LEN 512 -#define PAD_SLOP 16 bool useFileKey = false; // file should be encrypted char fileKey[KEY_LEN] = { 0 }; // ascii passphrase for the file key @@ -476,7 +475,7 @@ bool EncryptAndWriteFile(HWND hwnd, HANDLE hFile, BYTE *data, DWORD size, DWORD static int sequence = 1; // sequence counter so each time is unique srand(sequence++ ^ (unsigned int)time(NULL)); { - int i; for (i = 0; i < AES_MAX_IV_SIZE; i++) { + for (int i = 0; i < AES_MAX_IV_SIZE; i++) { precodedata[PREAMBLE_SIZE + i] = 0;//rand(); } } @@ -513,7 +512,7 @@ bool EncryptAndWriteFile(HWND hwnd, HANDLE hFile, BYTE *data, DWORD size, DWORD AES_bin_setup(&masterencode, AES_DIR_ENCRYPT, KEY_BYTES * 8, binMasterKey); {// generate another IV for the master key - int i; for (i = 0; i < sizeof(masterFileIV); i++) { masterFileIV[i] = (BYTE)(rand() & BYTE_MAX); } + for (int i = 0; i < sizeof(masterFileIV); i++) { masterFileIV[i] = (BYTE)(rand() & BYTE_MAX); } } AES_bin_cipherInit(&mastercypher, AES_MODE_CBC, masterFileIV); @@ -542,10 +541,11 @@ bool EncryptAndWriteFile(HWND hwnd, HANDLE hFile, BYTE *data, DWORD size, DWORD DWORD enclen = 0; bool bWriteRes = false; - BYTE* encdata = (BYTE*)HeapAlloc(GetProcessHeap(), HEAP_GENERATE_EXCEPTIONS, size + PAD_SLOP); // add slop to the end for padding - if (!encdata) - return bWriteRes; + BYTE* encdata = (BYTE*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size + PAD_SLOP); // add slop to the end for padding + if (!encdata) { + return bWriteRes; + } if (size > PAD_SLOP) { enclen += AES_blockEncrypt(&fileCypher, &fileEncode, data, size - PAD_SLOP, encdata); } enclen += AES_padEncrypt(&fileCypher, &fileEncode, data + enclen, size - enclen, encdata + enclen); diff --git a/crypto/crypto.h b/crypto/crypto.h index 6766aa6da..f53673161 100644 --- a/crypto/crypto.h +++ b/crypto/crypto.h @@ -3,13 +3,19 @@ #define __CRYPTO_H__ #include +#ifdef _DEBUG #define BUG1(a,b) { perror("a"); } #define BUG(a) { perror("a"); } +#else +#define BUG1(a,b) ((void)0); +#define BUG(a) ((void)0); +#endif -#define PREAMBLE_SIZE 8 // 4 byte signature + 4 byte subfile type -#define KEY_BYTES 32 // 32 bytes = 256 bits of key +#define PREAMBLE_SIZE 8 // 4 byte signature + 4 byte subfile type +#define PAD_SLOP 16 // padding for block chain +#define KEY_BYTES 32 // 32 bytes = 256 bits of key #define PREAMBLE 0x01020304 // first 4 bytes of the file -#define FILEKEY_FORMAT 1 // next 4 bytes determine version/format +#define FILEKEY_FORMAT 1 // next 4 bytes determine version/format #define MASTERKEY_FORMAT 2 // format with master key #define MASTER_KEY_OFFSET (PREAMBLE_SIZE+AES_MAX_IV_SIZE) #define UNUSED(expr) (void)(expr) diff --git a/crypto/rijndael-api-fst.c b/crypto/rijndael-api-fst.c index acc9df84b..96be327e1 100644 --- a/crypto/rijndael-api-fst.c +++ b/crypto/rijndael-api-fst.c @@ -96,7 +96,6 @@ int AES_setup char *keyMat; u8 cipherKey[MAXKB]; - if (TheKey != NULL) { //strncpy(key->TheKey, TheKey, keyLen/4); memcpy_s(key->TheKey, AES_MAX_KEY_SIZE, TheKey, keyLen / 4); @@ -245,8 +244,8 @@ int AES_blockEncrypt int inputLen, // @parm the size of the input data BYTE *outBuffer) //@parm a buffer to receive the encrypted data { - int i, k, t, numBlocks; - u8 block[16], *iv; + u8 block[16]; + u8* iv; if (cipher == NULL || key == NULL || @@ -257,11 +256,11 @@ int AES_blockEncrypt return 0; /* nothing to do */ } - numBlocks = inputLen / 16; + unsigned int const numBlocks = inputLen / 16; switch (cipher->mode) { case AES_MODE_ECB: - for (i = numBlocks; i > 0; i--) { + for (unsigned int i = numBlocks; i > 0; i--) { rijndaelEncrypt(key->rk, key->Nr, input, outBuffer); input += 16; outBuffer += 16; @@ -270,7 +269,7 @@ int AES_blockEncrypt case AES_MODE_CBC: iv = cipher->IV; - for (i = numBlocks; i > 0; i--) { + for (unsigned int i = numBlocks; i > 0; i--) { ((u32*)block)[0] = ((u32*)input)[0] ^ ((u32*)iv)[0]; ((u32*)block)[1] = ((u32*)input)[1] ^ ((u32*)iv)[1]; ((u32*)block)[2] = ((u32*)input)[2] ^ ((u32*)iv)[2]; @@ -287,12 +286,12 @@ int AES_blockEncrypt case AES_MODE_CFB1: iv = cipher->IV; - for (i = numBlocks; i > 0; i--) { + for (unsigned int i = numBlocks; i > 0; i--) { memcpy(outBuffer, input, 16); - for (k = 0; k < 128; k++) { + for (unsigned int k = 0; k < 128; k++) { rijndaelEncrypt(key->ek, key->Nr, iv, block); outBuffer[k >> 3] ^= (block[0] & 0x80U) >> (k & 7); - for (t = 0; t < 15; t++) { + for (unsigned int t = 0; t < 15; t++) { iv[t] = (iv[t] << 1) | (iv[t + 1] >> 7); } iv[15] = (iv[15] << 1) | ((outBuffer[k >> 3] >> (7 - (k & 7))) & 1); @@ -330,8 +329,8 @@ int AES_padEncrypt int inputOctets, // @parm the size of the input data BYTE *outBuffer) //@parm a buffer to receive the encrypted data { - int i, numBlocks, padLen; - u8 block[16], *iv; + u8 block[16]; + u8* iv; if (cipher == NULL || key == NULL || @@ -342,50 +341,57 @@ int AES_padEncrypt return 0; /* nothing to do */ } - numBlocks = inputOctets / 16; + unsigned int const numBlocks = inputOctets / 16; switch (cipher->mode) { case AES_MODE_ECB: - for (i = numBlocks; i > 0; i--) { - rijndaelEncrypt(key->rk, key->Nr, input, outBuffer); - input += 16; - outBuffer += 16; - } - padLen = 16 - (inputOctets - 16 * numBlocks); - if ((padLen <= 0) || (padLen > 16)) { - BUG1("Padding must be 1-16, is %d", padLen); - } - memcpy(block, input, 16 - padLen); - memset(block + 16 - padLen, padLen, padLen); - rijndaelEncrypt(key->rk, key->Nr, block, outBuffer); - break; + { + for (unsigned int i = numBlocks; i > 0; i--) { + rijndaelEncrypt(key->rk, key->Nr, input, outBuffer); + input += 16; + outBuffer += 16; + } + unsigned int const padLen = 16 - (inputOctets - 16 * numBlocks); + if ((padLen <= 0) || (padLen > 16)) { + BUG1("Padding must be 1-16, is %d", padLen); + return 16 * numBlocks; + } + memcpy(block, input, 16 - padLen); + memset(block + 16 - padLen, padLen, padLen); + rijndaelEncrypt(key->rk, key->Nr, block, outBuffer); + } + break; case AES_MODE_CBC: - iv = cipher->IV; - for (i = numBlocks; i > 0; i--) { - ((u32*)block)[0] = ((u32*)input)[0] ^ ((u32*)iv)[0]; - ((u32*)block)[1] = ((u32*)input)[1] ^ ((u32*)iv)[1]; - ((u32*)block)[2] = ((u32*)input)[2] ^ ((u32*)iv)[2]; - ((u32*)block)[3] = ((u32*)input)[3] ^ ((u32*)iv)[3]; - rijndaelEncrypt(key->rk, key->Nr, block, outBuffer); - iv = outBuffer; - input += 16; - outBuffer += 16; - } - padLen = 16 - (inputOctets - 16 * numBlocks); - if ((padLen <= 0) || (padLen > 16)) { - BUG1("Padding must be 1-16, is %d", padLen); - } - for (i = 0; i < 16 - padLen; i++) { - block[i] = input[i] ^ iv[i]; - } - for (i = 16 - padLen; (0 <= i) && (i < 16); i++) { - block[i] = (BYTE)padLen ^ iv[i]; - } + { + iv = cipher->IV; + for (unsigned int i = numBlocks; i > 0; i--) { + ((u32*)block)[0] = ((u32*)input)[0] ^ ((u32*)iv)[0]; + ((u32*)block)[1] = ((u32*)input)[1] ^ ((u32*)iv)[1]; + ((u32*)block)[2] = ((u32*)input)[2] ^ ((u32*)iv)[2]; + ((u32*)block)[3] = ((u32*)input)[3] ^ ((u32*)iv)[3]; rijndaelEncrypt(key->rk, key->Nr, block, outBuffer); - // set for chaining to the next block, even though there will normally not be one - memcpy(cipher->IV, outBuffer, AES_MAX_IV_SIZE); - break; + iv = outBuffer; + input += 16; + outBuffer += 16; + } + unsigned int const padLen = 16 - (inputOctets - 16 * numBlocks); + if ((padLen <= 0) || (padLen > 16)) { + BUG1("Padding must be 1-16, is %d", padLen); + return 16 * numBlocks; + } + for (unsigned int i = 0; i < 16 - padLen; i++) { + block[i] = input[i] ^ iv[i]; + } + BYTE const plen = (BYTE)(padLen & 0xFF); + for (unsigned int i = 16 - padLen; (i < 16); i++) { + block[i] = plen ^ iv[i]; + } + rijndaelEncrypt(key->rk, key->Nr, block, outBuffer); + // set for chaining to the next block, even though there will normally not be one + memcpy(cipher->IV, outBuffer, AES_MAX_IV_SIZE); + } + break; default: return BAD_CIPHER_STATE; @@ -409,8 +415,8 @@ int AES_blockDecrypt BYTE *outBuffer) //@parm a buffer to receive the decrypted buffer { //int lim = 32; - int i, k, t, numBlocks; - u8 block[16], *iv; + u8 block[16]; + u8* iv; if (cipher == NULL || key == NULL || @@ -421,11 +427,11 @@ int AES_blockDecrypt return 0; /* nothing to do */ } - numBlocks = inputLen / 16; + unsigned int const numBlocks = inputLen / 16; switch (cipher->mode) { case AES_MODE_ECB: - for (i = numBlocks; i > 0; i--) { + for (unsigned int i = numBlocks; i > 0; i--) { rijndaelDecrypt(key->rk, key->Nr, input, outBuffer); input += 16; outBuffer += 16; @@ -434,7 +440,7 @@ int AES_blockDecrypt case AES_MODE_CBC: iv = cipher->IV; - for (i = numBlocks; i > 0; i--) { + for (unsigned int i = numBlocks; i > 0; i--) { rijndaelDecrypt(key->rk, key->Nr, input, block); ((u32*)block)[0] ^= ((u32*)iv)[0]; ((u32*)block)[1] ^= ((u32*)iv)[1]; @@ -449,11 +455,11 @@ int AES_blockDecrypt case AES_MODE_CFB1: iv = cipher->IV; - for (i = numBlocks; i > 0; i--) { + for (unsigned int i = numBlocks; i > 0; i--) { memcpy(outBuffer, input, 16); - for (k = 0; k < 128; k++) { + for (unsigned int k = 0; k < 128; k++) { rijndaelEncrypt(key->ek, key->Nr, iv, block); - for (t = 0; t < 15; t++) { + for (unsigned int t = 0; t < 15; t++) { iv[t] = (iv[t] << 1) | (iv[t + 1] >> 7); } iv[15] = (iv[15] << 1) | ((input[k >> 3] >> (7 - (k & 7))) & 1); @@ -487,8 +493,8 @@ int AES_padDecrypt int inputOctets, //@parm the size of the input BYTE *outBuffer) //@parm a buffer to receive the decrypted buffer { - int i, numBlocks, padLen; u8 block[16]; + unsigned int padLen; if (cipher == NULL || key == NULL || @@ -502,12 +508,12 @@ int AES_padDecrypt return BAD_DATA; } - numBlocks = inputOctets / 16; + unsigned int const numBlocks = inputOctets / 16; switch (cipher->mode) { case AES_MODE_ECB: /* all blocks but last */ - for (i = numBlocks - 1; i > 0; i--) { + for (unsigned int i = numBlocks - 1; i > 0; i--) { rijndaelDecrypt(key->rk, key->Nr, input, outBuffer); input += 16; outBuffer += 16; @@ -518,7 +524,7 @@ int AES_padDecrypt if (padLen >= 16) { return BAD_DATA; } - for (i = 16 - padLen; i < 16; i++) { + for (unsigned int i = 16 - padLen; i < 16; i++) { if (block[i] != padLen) { return BAD_DATA; } @@ -528,7 +534,7 @@ int AES_padDecrypt case AES_MODE_CBC: /* all blocks but last */ - for (i = numBlocks - 1; i > 0; i--) { + for (unsigned int i = numBlocks - 1; i > 0; i--) { rijndaelDecrypt(key->rk, key->Nr, input, block); ((u32*)block)[0] ^= ((u32*)cipher->IV)[0]; ((u32*)block)[1] ^= ((u32*)cipher->IV)[1]; @@ -550,7 +556,7 @@ int AES_padDecrypt if (padLen <= 0 || padLen > 16) { return BAD_DATA; } - for (i = 16 - padLen; i < 16; i++) { + for (unsigned int i = 16 - padLen; i < 16; i++) { if (block[i] != padLen) { return BAD_DATA; } diff --git a/res/Notepad3.exe.manifest.conf b/res/Notepad3.exe.manifest.conf index 54f3a40cf..86270fbf1 100644 --- a/res/Notepad3.exe.manifest.conf +++ b/res/Notepad3.exe.manifest.conf @@ -3,7 +3,7 @@ Notepad3 RC2 diff --git a/src/Edit.c b/src/Edit.c index 2824bc9cd..896806798 100644 --- a/src/Edit.c +++ b/src/Edit.c @@ -1316,8 +1316,6 @@ bool EditSaveFile( // get text cbData = (DWORD)SciCall_GetTextLength(); - lpData = AllocMem(cbData + 4, HEAP_ZERO_MEMORY); //fix: +bom - SciCall_GetText((DocPos)SizeOfMem(lpData), lpData); if (cbData == 0) { bWriteSuccess = SetEndOfFile(hFile); @@ -1325,7 +1323,10 @@ bool EditSaveFile( } else { - // FIXME: move checks in front of disk file access + lpData = AllocMem(cbData + 4 + PAD_SLOP, HEAP_ZERO_MEMORY); //fix: +bom + SciCall_GetText((DocPos)cbData+1, lpData); + + // FIXME: move checks in front of disk file access // Msg if file tag encoding does not correspond to BOM /*if ((g_Encodings[iEncoding].uFlags & NCP_UNICODE) == 0 && (g_Encodings[iEncoding].uFlags & NCP_UTF8_SIGN) == 0) { bool bEncodingMismatch = true; @@ -1352,7 +1353,7 @@ bool EditSaveFile( { SetEndOfFile(hFile); - LPWSTR lpDataWide = AllocMem(cbData * 2 + 16, HEAP_ZERO_MEMORY); + LPWSTR lpDataWide = AllocMem(cbData * 2 + PAD_SLOP, HEAP_ZERO_MEMORY); int bomoffset = 0; if (Encoding_IsUNICODE_BOM(status->iEncoding)) { const char* bom = "\xFF\xFE"; @@ -1378,7 +1379,7 @@ bool EditSaveFile( if (Encoding_IsUTF8_SIGN(status->iEncoding)) { const char* bom = "\xEF\xBB\xBF"; - DWORD bomoffset = 3; + DWORD const bomoffset = 3; MoveMemory(&lpData[bomoffset], lpData, cbData); CopyMemory(lpData, bom, bomoffset); cbData += bomoffset; @@ -1395,7 +1396,7 @@ bool EditSaveFile( BOOL bCancelDataLoss = FALSE; UINT uCodePage = Encoding_GetCodePage(status->iEncoding); - LPWSTR lpDataWide = AllocMem(cbData * 2 + 16, HEAP_ZERO_MEMORY); + LPWSTR lpDataWide = AllocMem(cbData * 2 + PAD_SLOP, HEAP_ZERO_MEMORY); int cbDataWide = MultiByteToWideChar(Encoding_SciCP,0,lpData,cbData, lpDataWide,(MBWC_DocPos_Cast)(SizeOfMem(lpDataWide)/sizeof(WCHAR))); diff --git a/src/VersionEx.h b/src/VersionEx.h index ed6ae2c4e..0f22b0367 100644 --- a/src/VersionEx.h +++ b/src/VersionEx.h @@ -7,8 +7,8 @@ #define SAPPNAME "Notepad3" #define VERSION_MAJOR 5 #define VERSION_MINOR 19 -#define VERSION_REV 529 -#define VERSION_BUILD 2228 +#define VERSION_REV 605 +#define VERSION_BUILD 2244 #define SCINTILLA_VER 415+ #define ONIGMO_REGEX_VER 6.2.0 #define VERSION_PATCH RC2