1*d0626817SAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
3*d0626817SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4*d0626817SAndrew Rist * or more contributor license agreements. See the NOTICE file
5*d0626817SAndrew Rist * distributed with this work for additional information
6*d0626817SAndrew Rist * regarding copyright ownership. The ASF licenses this file
7*d0626817SAndrew Rist * to you under the Apache License, Version 2.0 (the
8*d0626817SAndrew Rist * "License"); you may not use this file except in compliance
9*d0626817SAndrew Rist * with the License. You may obtain a copy of the License at
10cdf0e10cSrcweir *
11*d0626817SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir *
13*d0626817SAndrew Rist * Unless required by applicable law or agreed to in writing,
14*d0626817SAndrew Rist * software distributed under the License is distributed on an
15*d0626817SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*d0626817SAndrew Rist * KIND, either express or implied. See the License for the
17*d0626817SAndrew Rist * specific language governing permissions and limitations
18*d0626817SAndrew Rist * under the License.
19cdf0e10cSrcweir *
20*d0626817SAndrew Rist *************************************************************/
21*d0626817SAndrew Rist
22*d0626817SAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_package.hxx"
26cdf0e10cSrcweir
27cdf0e10cSrcweir #include <rtl/cipher.h>
28cdf0e10cSrcweir #include <rtl/ref.hxx>
29cdf0e10cSrcweir
30cdf0e10cSrcweir #include "blowfishcontext.hxx"
31cdf0e10cSrcweir
32cdf0e10cSrcweir using namespace ::com::sun::star;
33cdf0e10cSrcweir
34cdf0e10cSrcweir // static
Create(const uno::Sequence<sal_Int8> & aDerivedKey,const uno::Sequence<sal_Int8> & aInitVector,bool bEncrypt)35cdf0e10cSrcweir uno::Reference< xml::crypto::XCipherContext > BlowfishCFB8CipherContext::Create( const uno::Sequence< sal_Int8 >& aDerivedKey, const uno::Sequence< sal_Int8 >& aInitVector, bool bEncrypt )
36cdf0e10cSrcweir {
37cdf0e10cSrcweir ::rtl::Reference< BlowfishCFB8CipherContext > xResult = new BlowfishCFB8CipherContext();
38cdf0e10cSrcweir xResult->m_pCipher = rtl_cipher_create( rtl_Cipher_AlgorithmBF, rtl_Cipher_ModeStream );
39cdf0e10cSrcweir if ( !xResult->m_pCipher )
40cdf0e10cSrcweir throw uno::RuntimeException( ::rtl::OUString::createFromAscii( "Can not create cipher!\n" ),
41cdf0e10cSrcweir uno::Reference< XInterface >() );
42cdf0e10cSrcweir
43cdf0e10cSrcweir if ( rtl_Cipher_E_None != rtl_cipher_init(
44cdf0e10cSrcweir xResult->m_pCipher,
45cdf0e10cSrcweir bEncrypt ? rtl_Cipher_DirectionEncode : rtl_Cipher_DirectionDecode,
46cdf0e10cSrcweir reinterpret_cast< const sal_uInt8* >( aDerivedKey.getConstArray() ),
47cdf0e10cSrcweir aDerivedKey.getLength(),
48cdf0e10cSrcweir reinterpret_cast< const sal_uInt8* >( aInitVector.getConstArray() ),
49cdf0e10cSrcweir aInitVector.getLength() ) )
50cdf0e10cSrcweir {
51cdf0e10cSrcweir throw uno::RuntimeException( ::rtl::OUString::createFromAscii( "Can not initialize cipher!\n" ),
52cdf0e10cSrcweir uno::Reference< XInterface >() );
53cdf0e10cSrcweir }
54cdf0e10cSrcweir
55cdf0e10cSrcweir xResult->m_bEncrypt = bEncrypt;
56cdf0e10cSrcweir
57cdf0e10cSrcweir return uno::Reference< xml::crypto::XCipherContext >( xResult.get() );
58cdf0e10cSrcweir }
59cdf0e10cSrcweir
~BlowfishCFB8CipherContext()60cdf0e10cSrcweir BlowfishCFB8CipherContext::~BlowfishCFB8CipherContext()
61cdf0e10cSrcweir {
62cdf0e10cSrcweir if ( m_pCipher )
63cdf0e10cSrcweir {
64cdf0e10cSrcweir rtl_cipher_destroy ( m_pCipher );
65cdf0e10cSrcweir m_pCipher = NULL;
66cdf0e10cSrcweir }
67cdf0e10cSrcweir }
68cdf0e10cSrcweir
convertWithCipherContext(const uno::Sequence<::sal_Int8> & aData)69cdf0e10cSrcweir uno::Sequence< sal_Int8 > SAL_CALL BlowfishCFB8CipherContext::convertWithCipherContext( const uno::Sequence< ::sal_Int8 >& aData )
70cdf0e10cSrcweir throw( lang::IllegalArgumentException, lang::DisposedException, uno::RuntimeException )
71cdf0e10cSrcweir {
72cdf0e10cSrcweir ::osl::MutexGuard aGuard( m_aMutex );
73cdf0e10cSrcweir if ( !m_pCipher )
74cdf0e10cSrcweir throw lang::DisposedException();
75cdf0e10cSrcweir
76cdf0e10cSrcweir uno::Sequence< sal_Int8 > aResult( aData.getLength() );
77cdf0e10cSrcweir rtlCipherError nError = rtl_Cipher_E_None;
78cdf0e10cSrcweir
79cdf0e10cSrcweir if ( m_bEncrypt )
80cdf0e10cSrcweir {
81cdf0e10cSrcweir rtl_cipher_encode( m_pCipher,
82cdf0e10cSrcweir aData.getConstArray(),
83cdf0e10cSrcweir aData.getLength(),
84cdf0e10cSrcweir reinterpret_cast< sal_uInt8* >( aResult.getArray() ),
85cdf0e10cSrcweir aResult.getLength() );
86cdf0e10cSrcweir }
87cdf0e10cSrcweir else
88cdf0e10cSrcweir {
89cdf0e10cSrcweir rtl_cipher_decode( m_pCipher,
90cdf0e10cSrcweir aData.getConstArray(),
91cdf0e10cSrcweir aData.getLength(),
92cdf0e10cSrcweir reinterpret_cast< sal_uInt8* >( aResult.getArray() ),
93cdf0e10cSrcweir aResult.getLength() );
94cdf0e10cSrcweir }
95cdf0e10cSrcweir
96cdf0e10cSrcweir if ( rtl_Cipher_E_None != nError )
97cdf0e10cSrcweir {
98cdf0e10cSrcweir throw uno::RuntimeException( ::rtl::OUString::createFromAscii( "Can not decrypt/encrypt with cipher!\n" ),
99cdf0e10cSrcweir uno::Reference< uno::XInterface >() );
100cdf0e10cSrcweir }
101cdf0e10cSrcweir
102cdf0e10cSrcweir return aResult;
103cdf0e10cSrcweir }
104cdf0e10cSrcweir
finalizeCipherContextAndDispose()105cdf0e10cSrcweir uno::Sequence< ::sal_Int8 > SAL_CALL BlowfishCFB8CipherContext::finalizeCipherContextAndDispose()
106cdf0e10cSrcweir throw( lang::DisposedException, uno::RuntimeException )
107cdf0e10cSrcweir {
108cdf0e10cSrcweir ::osl::MutexGuard aGuard( m_aMutex );
109cdf0e10cSrcweir if ( !m_pCipher )
110cdf0e10cSrcweir throw lang::DisposedException();
111cdf0e10cSrcweir
112cdf0e10cSrcweir rtl_cipher_destroy ( m_pCipher );
113cdf0e10cSrcweir m_pCipher = NULL;
114cdf0e10cSrcweir
115cdf0e10cSrcweir return uno::Sequence< sal_Int8 >();
116cdf0e10cSrcweir }
117cdf0e10cSrcweir
118cdf0e10cSrcweir
119