1 /* Abiword
2 * Copyright (C) 2002 Christian Biesinger <cbiesinger@web.de>
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301 USA.
18 */
19
20 /** @file
21 * implementation of staroffice decryption routines, very much inspired by
22 * openoffice's sw/source/core/sw3io/sw3imp.cxx#L2721 and sw/source/core/sw3io/crypter.cxx#L77 */
23
24 #include "sdw_cryptor.h"
25 #include "ut_debugmsg.h"
26 #include "ut_string_class.h"
27
28 // random values for encrypting the password
29 static const UT_uint8 gEncode[] =
30 { 0xab, 0x9e, 0x43, 0x05, 0x38, 0x12, 0x4d, 0x44,
31 0xd5, 0x7e, 0xe3, 0x84, 0x98, 0x23, 0x3f, 0xba };
32
SDWCryptor(UT_uint32 aDate,UT_uint32 aTime,const UT_uint8 * aFilePass)33 SDWCryptor::SDWCryptor(UT_uint32 aDate, UT_uint32 aTime, const UT_uint8* aFilePass)
34 : mDate(aDate), mTime(aTime) {
35 if (aFilePass)
36 memcpy(mFilePass, aFilePass, maxPWLen);
37 else
38 memset(mFilePass, 0, maxPWLen);
39 }
40
~SDWCryptor()41 SDWCryptor::~SDWCryptor() {
42 }
43
SetPassword(const char * aPassword)44 bool SDWCryptor::SetPassword(const char* aPassword) {
45 // Set the new password
46 char pw[maxPWLen];
47 strncpy(pw, aPassword, maxPWLen);
48 size_t len = strlen(aPassword);
49 // fill with spaces. only executed if len < maxPWLen
50 for (int i = len; i < maxPWLen; i++)
51 pw[i] = ' ';
52
53 // the password needs to be encrypted
54 memcpy(mPassword, gEncode, maxPWLen);
55 Encrypt(pw, mPassword, maxPWLen);
56
57 // Check password if we have valid date and/or time
58 if (mDate || mTime) {
59 char testString[17];
60 UT_String needle = UT_String_sprintf("%08x%08x", mDate, mTime);
61 Encrypt(needle.c_str(), testString, 16);
62 if (memcmp(testString, mFilePass, 16) != 0) {
63 return false; // wrong password
64 }
65 }
66 return true;
67 }
68
69 // almost literally taken from openoffice code (crypter.cxx)
Decrypt(const char * aEncrypted,char * aBuffer,UT_uint32 aLen) const70 void SDWCryptor::Decrypt(const char* aEncrypted, char* aBuffer, UT_uint32 aLen) const {
71 size_t nCryptPtr = 0;
72 UT_uint8 cBuf[maxPWLen];
73 memcpy(cBuf, mPassword, maxPWLen);
74 UT_uint8* p = cBuf;
75
76 if (!aLen)
77 aLen = strlen(aEncrypted);
78
79 while (aLen--) {
80 *aBuffer++ = *aEncrypted++ ^ ( *p ^ static_cast<UT_uint8> ( cBuf[ 0 ] * nCryptPtr ) );
81 *p += ( nCryptPtr < (maxPWLen-1) ) ? *(p+1) : cBuf[ 0 ];
82 if( !*p ) *p += 1;
83 p++;
84 if( ++nCryptPtr >= maxPWLen ) {
85 nCryptPtr = 0;
86 p = cBuf;
87 }
88 }
89 }
90