Update ooo320-m1
[ooovba.git] / svx / inc / mscodec.hxx
blobb80a778f1cd8174d4c6ac141a513c5fb5346c476
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: mscodec.hxx,v $
10 * $Revision: 1.5 $
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"
38 namespace svx {
40 // ============================================================================
42 /** Encodes and decodes data from protected MSO 95- documents.
44 class SVX_DLLPUBLIC MSCodec_Xor95
46 public:
47 explicit MSCodec_Xor95(int nRotateDistance);
48 virtual ~MSCodec_Xor95();
50 /** Initializes the algorithm with the specified password.
52 @param pPassData
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.
60 @precond
61 The codec must be initialized with InitKey() before this function
62 can be used.
64 @param nKey
65 Password key value read from the file.
66 @param nHash
67 Password hash value read from the file.
69 @return
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.
78 @precond
79 The codec must be initialized with InitKey() before this function
80 can be used.
82 void InitCipher();
84 /** Decodes a block of memory inplace.
86 @precond
87 The codec must be initialized with InitKey() before this function
88 can be used.
90 @param pnData
91 Encrypted data block. Will contain the decrypted data afterwards.
92 @param nBytes
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().
102 @precond
103 The codec must be initialized with InitKey() before this function
104 can be used.
106 @param nBytes
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
117 result).
119 static sal_uInt16 GetHash( const sal_uInt8* pnPassData, sal_Size nSize );
121 protected:
122 sal_uInt8 mpnKey[ 16 ]; /// Encryption key.
123 sal_Size mnOffset; /// Key offset.
125 private:
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
138 public:
139 explicit MSCodec_XorXLS95() : MSCodec_Xor95(2) {}
141 /** Decodes a block of memory inplace.
143 @precond
144 The codec must be initialized with InitKey() before this function
145 can be used.
147 @param pnData
148 Encrypted data block. Will contain the decrypted data afterwards.
149 @param nBytes
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
159 public:
160 explicit MSCodec_XorWord95() : MSCodec_Xor95(7) {}
162 /** Decodes a block of memory inplace.
164 @precond
165 The codec must be initialized with InitKey() before this function
166 can be used.
168 @param pnData
169 Encrypted data block. Will contain the decrypted data afterwards.
170 @param nBytes
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
187 public:
188 explicit MSCodec_Std97();
189 ~MSCodec_Std97();
191 /** Initializes the algorithm with the specified password and document ID.
193 @param pPassData
194 Wide character array containing the password. Must be zero
195 terminated, which results in a maximum length of 15 characters.
196 @param pUnique
197 Unique document identifier read from or written to the file.
199 void InitKey(
200 const sal_uInt16 pPassData[ 16 ],
201 const sal_uInt8 pUnique[ 16 ] );
203 /** Verifies the validity of the password using the passed salt data.
205 @precond
206 The codec must be initialized with InitKey() before this function
207 can be used.
209 @param pSaltData
210 Salt data block read from the file.
211 @param pSaltDigest
212 Salt digest read from the file.
214 @return
215 true = Test was successful.
217 bool VerifyKey(
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
227 for Excel files.
229 @precond
230 The codec must be initialized with InitKey() before this function
231 can be used.
233 @param nCounter
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()
246 @precond
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).
252 @param pData
253 Unencrypted source data block.
254 @param nDatLen
255 Size of the passed source data block.
256 @param pBuffer
257 Destination buffer for the encrypted data.
258 @param nBufLen
259 Size of the destination buffer.
261 @return
262 true = Encoding was successful (no error occured).
264 bool Encode(
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()
272 @precond
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).
278 @param pData
279 Encrypted source data block.
280 @param nDatLen
281 Size of the passed source data block.
282 @param pBuffer
283 Destination buffer for the decrypted data.
284 @param nBufLen
285 Size of the destination buffer.
287 @return
288 true = Decoding was successful (no error occured).
290 bool Decode(
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().
299 @precond
300 The codec must be initialized with InitKey() before this function
301 can be used.
303 @param nDatLen
304 Number of bytes to be skipped (cipher "seeks" forward).
306 bool Skip( sal_Size nDatLen );
308 /** Gets salt data and salt digest.
310 @precond
311 The codec must be initialized with InitKey() before this function
312 can be used.
314 @param pSalt
315 Salt, a random number.
316 @param pSaltData
317 Salt data block generated from the salt.
318 @param pSaltDigest
319 Salt digest generated from the salt.
321 void GetEncryptKey (
322 const sal_uInt8 pSalt[16],
323 sal_uInt8 pSaltData[16],
324 sal_uInt8 pSaltDigest[16]);
326 private:
327 void GetDigestFromSalt( const sal_uInt8 pSaltData[16], sal_uInt8 pDigest[16] );
329 private:
330 SVX_DLLPRIVATE MSCodec_Std97( const MSCodec_Std97& );
331 SVX_DLLPRIVATE MSCodec_Std97& operator=( const MSCodec_Std97& );
333 rtlCipher m_hCipher;
334 rtlDigest m_hDigest;
335 sal_uInt8 m_pDigestValue[ RTL_DIGEST_LENGTH_MD5 ];
338 // ============================================================================
340 } // namespace svx
342 #endif