Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*
3 : : * This file is part of the LibreOffice project.
4 : : *
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/.
8 : : *
9 : : * This file incorporates work covered by the following license notice:
10 : : *
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 .
18 : : */
19 : :
20 : :
21 : : #include <rtl/cipher.h>
22 : : #include <rtl/ref.hxx>
23 : :
24 : : #include "blowfishcontext.hxx"
25 : :
26 : : using namespace ::com::sun::star;
27 : :
28 : : // static
29 : 24 : uno::Reference< xml::crypto::XCipherContext > BlowfishCFB8CipherContext::Create( const uno::Sequence< sal_Int8 >& aDerivedKey, const uno::Sequence< sal_Int8 >& aInitVector, bool bEncrypt )
30 : : {
31 [ + - ]: 24 : ::rtl::Reference< BlowfishCFB8CipherContext > xResult = new BlowfishCFB8CipherContext();
32 : 24 : xResult->m_pCipher = rtl_cipher_create( rtl_Cipher_AlgorithmBF, rtl_Cipher_ModeStream );
33 [ - + ]: 24 : if ( !xResult->m_pCipher )
34 : : throw uno::RuntimeException( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Can not create cipher!")),
35 [ # # ][ # # ]: 0 : uno::Reference< XInterface >() );
36 : :
37 [ - + ]: 24 : if ( rtl_Cipher_E_None != rtl_cipher_init(
38 : 24 : xResult->m_pCipher,
39 : : bEncrypt ? rtl_Cipher_DirectionEncode : rtl_Cipher_DirectionDecode,
40 : 24 : reinterpret_cast< const sal_uInt8* >( aDerivedKey.getConstArray() ),
41 : 24 : aDerivedKey.getLength(),
42 : 24 : reinterpret_cast< const sal_uInt8* >( aInitVector.getConstArray() ),
43 [ - + ]: 72 : aInitVector.getLength() ) )
44 : : {
45 : : throw uno::RuntimeException( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Can not initialize cipher!") ),
46 [ # # ][ # # ]: 0 : uno::Reference< XInterface >() );
47 : : }
48 : :
49 : 24 : xResult->m_bEncrypt = bEncrypt;
50 : :
51 [ + - ][ + - ]: 24 : return uno::Reference< xml::crypto::XCipherContext >( xResult.get() );
52 : : }
53 : :
54 [ + - ]: 24 : BlowfishCFB8CipherContext::~BlowfishCFB8CipherContext()
55 : : {
56 [ - + ]: 24 : if ( m_pCipher )
57 : : {
58 : 0 : rtl_cipher_destroy ( m_pCipher );
59 : 0 : m_pCipher = NULL;
60 : : }
61 [ - + ]: 48 : }
62 : :
63 : 24 : uno::Sequence< sal_Int8 > SAL_CALL BlowfishCFB8CipherContext::convertWithCipherContext( const uno::Sequence< ::sal_Int8 >& aData )
64 : : throw( lang::IllegalArgumentException, lang::DisposedException, uno::RuntimeException )
65 : : {
66 [ + - ]: 24 : ::osl::MutexGuard aGuard( m_aMutex );
67 [ - + ]: 24 : if ( !m_pCipher )
68 [ # # ]: 0 : throw lang::DisposedException();
69 : :
70 [ + - ]: 24 : uno::Sequence< sal_Int8 > aResult( aData.getLength() );
71 : 24 : rtlCipherError nError = rtl_Cipher_E_None;
72 : :
73 [ - + ]: 24 : if ( m_bEncrypt )
74 : : {
75 : : rtl_cipher_encode( m_pCipher,
76 : 0 : aData.getConstArray(),
77 : 0 : aData.getLength(),
78 [ # # ]: 0 : reinterpret_cast< sal_uInt8* >( aResult.getArray() ),
79 : 0 : aResult.getLength() );
80 : : }
81 : : else
82 : : {
83 : : rtl_cipher_decode( m_pCipher,
84 : 24 : aData.getConstArray(),
85 : 24 : aData.getLength(),
86 [ + - ]: 24 : reinterpret_cast< sal_uInt8* >( aResult.getArray() ),
87 : 48 : aResult.getLength() );
88 : : }
89 : :
90 [ - + ]: 24 : if ( rtl_Cipher_E_None != nError )
91 : : {
92 : : throw uno::RuntimeException( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Can not decrypt/encrypt with cipher!") ),
93 [ # # ][ # # ]: 0 : uno::Reference< uno::XInterface >() );
94 : : }
95 : :
96 [ + - ]: 24 : return aResult;
97 : : }
98 : :
99 : 24 : uno::Sequence< ::sal_Int8 > SAL_CALL BlowfishCFB8CipherContext::finalizeCipherContextAndDispose()
100 : : throw( lang::DisposedException, uno::RuntimeException )
101 : : {
102 [ + - ]: 24 : ::osl::MutexGuard aGuard( m_aMutex );
103 [ - + ]: 24 : if ( !m_pCipher )
104 [ # # ]: 0 : throw lang::DisposedException();
105 : :
106 : 24 : rtl_cipher_destroy ( m_pCipher );
107 : 24 : m_pCipher = NULL;
108 : :
109 [ + - ][ + - ]: 24 : return uno::Sequence< sal_Int8 >();
110 : : }
111 : :
112 : :
113 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|