1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #ifndef INCLUDED_FILTER_MSFILTER_MSCODEC_HXX
21 #define INCLUDED_FILTER_MSFILTER_MSCODEC_HXX
23 #include <com/sun/star/uno/Sequence.hxx>
24 #include <com/sun/star/beans/NamedValue.hpp>
26 #include <rtl/cipher.h>
27 #include <rtl/digest.h>
28 #include <filter/msfilter/msfilterdllapi.h>
34 /** Encodes and decodes data from protected MSO 95- documents.
36 class MSFILTER_DLLPUBLIC MSCodec_Xor95
39 explicit MSCodec_Xor95(int nRotateDistance
);
40 virtual ~MSCodec_Xor95();
42 /** Initializes the algorithm with the specified password.
45 Character array containing the password. Must be zero terminated,
46 which results in a maximum length of 15 characters.
48 void InitKey( const sal_uInt8 pnPassData
[ 16 ] );
50 /** Initializes the algorithm with the encryption data.
53 The sequence contains the necessary data to initialize
56 bool InitCodec( const ::com::sun::star::uno::Sequence
< ::com::sun::star::beans::NamedValue
>& aData
);
58 /** Retrieves the encryption data
61 The sequence contains the necessary data to initialize
64 ::com::sun::star::uno::Sequence
< ::com::sun::star::beans::NamedValue
> GetEncryptionData();
67 /** Verifies the validity of the password using the passed key and hash.
70 The codec must be initialized with InitKey() before this function
74 Password key value read from the file.
76 Password hash value read from the file.
79 true = Test was successful.
81 bool VerifyKey( sal_uInt16 nKey
, sal_uInt16 nHash
) const;
83 /** Reinitializes the codec to start a new memory block.
85 Resets the internal key offset to 0.
88 The codec must be initialized with InitKey() before this function
93 /** Decodes a block of memory inplace.
96 The codec must be initialized with InitKey() before this function
100 Encrypted data block. Will contain the decrypted data afterwards.
102 Size of the passed data block.
104 virtual void Decode( sal_uInt8
* pnData
, sal_Size nBytes
)=0;
106 /** Lets the cipher skip a specific amount of bytes.
108 This function sets the cipher to the same state as if the specified
109 amount of data has been decoded with one or more calls of Decode().
112 The codec must be initialized with InitKey() before this function
116 Number of bytes to be skipped (cipher "seeks" forward).
118 void Skip( sal_Size nBytes
);
121 sal_uInt8 mpnKey
[ 16 ]; /// Encryption key.
122 sal_Size mnOffset
; /// Key offset.
125 MSFILTER_DLLPRIVATE
MSCodec_Xor95( const MSCodec_Xor95
& );
126 MSFILTER_DLLPRIVATE MSCodec_Xor95
& operator=( const MSCodec_Xor95
& );
128 sal_uInt16 mnKey
; /// Base key from password.
129 sal_uInt16 mnHash
; /// Hash value from password.
130 int mnRotateDistance
;
133 /** Encodes and decodes data from protected MSO XLS 95- documents.
135 class MSFILTER_DLLPUBLIC MSCodec_XorXLS95
: public MSCodec_Xor95
138 explicit MSCodec_XorXLS95() : MSCodec_Xor95(2) {}
140 /** Decodes a block of memory inplace.
143 The codec must be initialized with InitKey() before this function
147 Encrypted data block. Will contain the decrypted data afterwards.
149 Size of the passed data block.
151 virtual void Decode( sal_uInt8
* pnData
, sal_Size nBytes
) SAL_OVERRIDE
;
154 /** Encodes and decodes data from protected MSO Word 95- documents.
156 class MSFILTER_DLLPUBLIC MSCodec_XorWord95
: public MSCodec_Xor95
159 explicit MSCodec_XorWord95() : MSCodec_Xor95(7) {}
161 /** Decodes a block of memory inplace.
164 The codec must be initialized with InitKey() before this function
168 Encrypted data block. Will contain the decrypted data afterwards.
170 Size of the passed data block.
172 virtual void Decode( sal_uInt8
* pnData
, sal_Size nBytes
) SAL_OVERRIDE
;
178 /** Encodes and decodes data from protected MSO 97+ documents.
180 This is a wrapper class around low level cryptographic functions from RTL.
181 Implementation is based on the wvDecrypt package by Caolan McNamara:
182 http://www.csn.ul.ie/~caolan/docs/wvDecrypt.html
184 class MSFILTER_DLLPUBLIC MSCodec_Std97
187 explicit MSCodec_Std97();
190 /** Initializes the algorithm with the encryption data.
193 The sequence contains the necessary data to initialize
196 bool InitCodec( const ::com::sun::star::uno::Sequence
< ::com::sun::star::beans::NamedValue
>& aData
);
198 /** Retrieves the encryption data
201 The sequence contains the necessary data to initialize
204 ::com::sun::star::uno::Sequence
< ::com::sun::star::beans::NamedValue
> GetEncryptionData();
207 /** Initializes the algorithm with the specified password and document ID.
210 Wide character array containing the password. Must be zero
211 terminated, which results in a maximum length of 15 characters.
213 Unique document identifier read from or written to the file.
216 const sal_uInt16 pPassData
[ 16 ],
217 const sal_uInt8 pDocId
[ 16 ] );
219 /** Verifies the validity of the password using the passed salt data.
222 The codec must be initialized with InitKey() before this function
226 Salt data block read from the file.
228 Salt digest read from the file.
231 true = Test was successful.
234 const sal_uInt8 pSaltData
[ 16 ],
235 const sal_uInt8 pSaltDigest
[ 16 ] );
237 /** Rekeys the codec using the specified counter.
239 After reading a specific amount of data the cipher algorithm needs to
240 be rekeyed using a counter that counts the data blocks.
242 The block size is for example 512 Bytes for Word files and 1024 Bytes
246 The codec must be initialized with InitKey() before this function
250 Block counter used to rekey the cipher.
252 bool InitCipher( sal_uInt32 nCounter
);
254 /** Creates an MD5 digest of salt digest. */
255 bool CreateSaltDigest(
256 const sal_uInt8 nSaltData
[16], sal_uInt8 nSaltDigest
[16] );
258 /** Encodes a block of memory.
260 @see rtl_cipher_encode()
263 The codec must be initialized with InitKey() before this function
264 can be used. The destination buffer must be able to take all
265 unencoded data from the source buffer (usually this means it must be
266 as long as or longer than the source buffer).
269 Unencrypted source data block.
271 Size of the passed source data block.
273 Destination buffer for the encrypted data.
275 Size of the destination buffer.
278 true = Encoding was successful (no error occurred).
281 const void* pData
, sal_Size nDatLen
,
282 sal_uInt8
* pBuffer
, sal_Size nBufLen
);
284 /** Decodes a block of memory.
286 @see rtl_cipher_decode()
289 The codec must be initialized with InitKey() before this function
290 can be used. The destination buffer must be able to take all
291 encoded data from the source buffer (usually this means it must be
292 as long as or longer than the source buffer).
295 Encrypted source data block.
297 Size of the passed source data block.
299 Destination buffer for the decrypted data.
301 Size of the destination buffer.
304 true = Decoding was successful (no error occurred).
307 const void* pData
, sal_Size nDatLen
,
308 sal_uInt8
* pBuffer
, sal_Size nBufLen
);
310 /** Lets the cipher skip a specific amount of bytes.
312 This function sets the cipher to the same state as if the specified
313 amount of data has been decoded with one or more calls of Decode().
316 The codec must be initialized with InitKey() before this function
320 Number of bytes to be skipped (cipher "seeks" forward).
322 bool Skip( sal_Size nDatLen
);
324 /** Gets salt data and salt digest.
327 The codec must be initialized with InitKey() before this function
331 Salt, a random number.
333 Salt data block generated from the salt.
335 Salt digest generated from the salt.
338 const sal_uInt8 pSalt
[16],
339 sal_uInt8 pSaltData
[16],
340 sal_uInt8 pSaltDigest
[16]);
342 /* allows to get the unique document id from the codec
344 void GetDocId( sal_uInt8 pDocId
[16] );
346 void GetDigestFromSalt( const sal_uInt8 pSaltData
[16], sal_uInt8 pDigest
[16] );
350 const sal_uInt8 pKeyData
[64],
351 const sal_uInt8 pDocId
[16] );
355 MSFILTER_DLLPRIVATE
MSCodec_Std97( const MSCodec_Std97
& );
356 MSFILTER_DLLPRIVATE MSCodec_Std97
& operator=( const MSCodec_Std97
& );
360 sal_uInt8 m_pDigestValue
[ RTL_DIGEST_LENGTH_MD5
];
361 sal_uInt8 m_pDocId
[16];
366 } // namespace msfilter
370 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */