1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: mscodec.hxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #ifndef SVX_MSCODEC_HXX
32 #define SVX_MSCODEC_HXX
34 #include "rtl/cipher.h"
35 #include "rtl/digest.h"
36 #include "svx/svxdllapi.h"
40 // ============================================================================
42 /** Encodes and decodes data from protected MSO 95- documents.
44 class SVX_DLLPUBLIC MSCodec_Xor95
47 explicit MSCodec_Xor95(int nRotateDistance
);
48 virtual ~MSCodec_Xor95();
50 /** Initializes the algorithm with the specified password.
53 Character array containing the password. Must be zero terminated,
54 which results in a maximum length of 15 characters.
56 void InitKey( const sal_uInt8 pnPassData
[ 16 ] );
58 /** Verifies the validity of the password using the passed key and hash.
61 The codec must be initialized with InitKey() before this function
65 Password key value read from the file.
67 Password hash value read from the file.
70 true = Test was successful.
72 bool VerifyKey( sal_uInt16 nKey
, sal_uInt16 nHash
) const;
74 /** Reinitializes the codec to start a new memory block.
76 Resets the internal key offset to 0.
79 The codec must be initialized with InitKey() before this function
84 /** Decodes a block of memory inplace.
87 The codec must be initialized with InitKey() before this function
91 Encrypted data block. Will contain the decrypted data afterwards.
93 Size of the passed data block.
95 virtual void Decode( sal_uInt8
* pnData
, sal_Size nBytes
)=0;
97 /** Lets the cipher skip a specific amount of bytes.
99 This function sets the cipher to the same state as if the specified
100 amount of data has been decoded with one or more calls of Decode().
103 The codec must be initialized with InitKey() before this function
107 Number of bytes to be skipped (cipher "seeks" forward).
109 void Skip( sal_Size nBytes
);
111 // static -----------------------------------------------------------------
113 /** Calculates the 16-bit hash value for the given password.
115 The password data may be longer than 16 bytes. The array does not need
116 to be terminated with a NULL byte (but it can without invalidating the
119 static sal_uInt16
GetHash( const sal_uInt8
* pnPassData
, sal_Size nSize
);
122 sal_uInt8 mpnKey
[ 16 ]; /// Encryption key.
123 sal_Size mnOffset
; /// Key offset.
126 SVX_DLLPRIVATE
MSCodec_Xor95( const MSCodec_Xor95
& );
127 SVX_DLLPRIVATE MSCodec_Xor95
& operator=( const MSCodec_Xor95
& );
129 sal_uInt16 mnKey
; /// Base key from password.
130 sal_uInt16 mnHash
; /// Hash value from password.
131 int mnRotateDistance
;
134 /** Encodes and decodes data from protected MSO XLS 95- documents.
136 class SVX_DLLPUBLIC MSCodec_XorXLS95
: public MSCodec_Xor95
139 explicit MSCodec_XorXLS95() : MSCodec_Xor95(2) {}
141 /** Decodes a block of memory inplace.
144 The codec must be initialized with InitKey() before this function
148 Encrypted data block. Will contain the decrypted data afterwards.
150 Size of the passed data block.
152 virtual void Decode( sal_uInt8
* pnData
, sal_Size nBytes
);
155 /** Encodes and decodes data from protected MSO Word 95- documents.
157 class SVX_DLLPUBLIC MSCodec_XorWord95
: public MSCodec_Xor95
160 explicit MSCodec_XorWord95() : MSCodec_Xor95(7) {}
162 /** Decodes a block of memory inplace.
165 The codec must be initialized with InitKey() before this function
169 Encrypted data block. Will contain the decrypted data afterwards.
171 Size of the passed data block.
173 virtual void Decode( sal_uInt8
* pnData
, sal_Size nBytes
);
177 // ============================================================================
179 /** Encodes and decodes data from protected MSO 97+ documents.
181 This is a wrapper class around low level cryptographic functions from RTL.
182 Implementation is based on the wvDecrypt package by Caolan McNamara:
183 http://www.csn.ul.ie/~caolan/docs/wvDecrypt.html
185 class SVX_DLLPUBLIC MSCodec_Std97
188 explicit MSCodec_Std97();
191 /** Initializes the algorithm with the specified password and document ID.
194 Wide character array containing the password. Must be zero
195 terminated, which results in a maximum length of 15 characters.
197 Unique document identifier read from or written to the file.
200 const sal_uInt16 pPassData
[ 16 ],
201 const sal_uInt8 pUnique
[ 16 ] );
203 /** Verifies the validity of the password using the passed salt data.
206 The codec must be initialized with InitKey() before this function
210 Salt data block read from the file.
212 Salt digest read from the file.
215 true = Test was successful.
218 const sal_uInt8 pSaltData
[ 16 ],
219 const sal_uInt8 pSaltDigest
[ 16 ] );
221 /** Rekeys the codec using the specified counter.
223 After reading a specific amount of data the cipher algorithm needs to
224 be rekeyed using a counter that counts the data blocks.
226 The block size is for example 512 Bytes for Word files and 1024 Bytes
230 The codec must be initialized with InitKey() before this function
234 Block counter used to rekey the cipher.
236 bool InitCipher( sal_uInt32 nCounter
);
238 /** Creates an MD5 digest of salt digest. */
239 bool CreateSaltDigest(
240 const sal_uInt8 nSaltData
[16], sal_uInt8 nSaltDigest
[16] );
242 /** Encodes a block of memory.
244 @see rtl_cipher_encode()
247 The codec must be initialized with InitKey() before this function
248 can be used. The destination buffer must be able to take all
249 unencoded data from the source buffer (usually this means it must be
250 as long as or longer than the source buffer).
253 Unencrypted source data block.
255 Size of the passed source data block.
257 Destination buffer for the encrypted data.
259 Size of the destination buffer.
262 true = Encoding was successful (no error occured).
265 const void* pData
, sal_Size nDatLen
,
266 sal_uInt8
* pBuffer
, sal_Size nBufLen
);
268 /** Decodes a block of memory.
270 @see rtl_cipher_decode()
273 The codec must be initialized with InitKey() before this function
274 can be used. The destination buffer must be able to take all
275 encoded data from the source buffer (usually this means it must be
276 as long as or longer than the source buffer).
279 Encrypted source data block.
281 Size of the passed source data block.
283 Destination buffer for the decrypted data.
285 Size of the destination buffer.
288 true = Decoding was successful (no error occured).
291 const void* pData
, sal_Size nDatLen
,
292 sal_uInt8
* pBuffer
, sal_Size nBufLen
);
294 /** Lets the cipher skip a specific amount of bytes.
296 This function sets the cipher to the same state as if the specified
297 amount of data has been decoded with one or more calls of Decode().
300 The codec must be initialized with InitKey() before this function
304 Number of bytes to be skipped (cipher "seeks" forward).
306 bool Skip( sal_Size nDatLen
);
308 /** Gets salt data and salt digest.
311 The codec must be initialized with InitKey() before this function
315 Salt, a random number.
317 Salt data block generated from the salt.
319 Salt digest generated from the salt.
322 const sal_uInt8 pSalt
[16],
323 sal_uInt8 pSaltData
[16],
324 sal_uInt8 pSaltDigest
[16]);
327 void GetDigestFromSalt( const sal_uInt8 pSaltData
[16], sal_uInt8 pDigest
[16] );
330 SVX_DLLPRIVATE
MSCodec_Std97( const MSCodec_Std97
& );
331 SVX_DLLPRIVATE MSCodec_Std97
& operator=( const MSCodec_Std97
& );
335 sal_uInt8 m_pDigestValue
[ RTL_DIGEST_LENGTH_MD5
];
338 // ============================================================================