LibreOffice Module package (master) 1
blowfishcontext.cxx
Go to the documentation of this file.
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#include <sal/config.h>
21
22#include <com/sun/star/lang/DisposedException.hpp>
23#include <rtl/cipher.h>
24#include <rtl/ref.hxx>
25
26#include "blowfishcontext.hxx"
27
28using namespace ::com::sun::star;
29
30// static
31uno::Reference< xml::crypto::XCipherContext > BlowfishCFB8CipherContext::Create( const uno::Sequence< sal_Int8 >& aDerivedKey, const uno::Sequence< sal_Int8 >& aInitVector, bool bEncrypt )
32{
34 xResult->m_pCipher = rtl_cipher_create( rtl_Cipher_AlgorithmBF, rtl_Cipher_ModeStream );
35 if ( !xResult->m_pCipher )
36 throw uno::RuntimeException("Can not create cipher!" );
37
38 if ( rtl_Cipher_E_None != rtl_cipher_init(
39 xResult->m_pCipher,
40 bEncrypt ? rtl_Cipher_DirectionEncode : rtl_Cipher_DirectionDecode,
41 reinterpret_cast< const sal_uInt8* >( aDerivedKey.getConstArray() ),
42 aDerivedKey.getLength(),
43 reinterpret_cast< const sal_uInt8* >( aInitVector.getConstArray() ),
44 aInitVector.getLength() ) )
45 {
46 throw uno::RuntimeException("Can not initialize cipher!" );
47 }
48
49 xResult->m_bEncrypt = bEncrypt;
50
51 return xResult;
52}
53
55{
56 if ( m_pCipher )
57 {
58 rtl_cipher_destroy ( m_pCipher );
59 m_pCipher = nullptr;
60 }
61}
62
63uno::Sequence< sal_Int8 > SAL_CALL BlowfishCFB8CipherContext::convertWithCipherContext( const uno::Sequence< ::sal_Int8 >& aData )
64{
65 std::scoped_lock aGuard( m_aMutex );
66 if ( !m_pCipher )
67 throw lang::DisposedException();
68
69 uno::Sequence< sal_Int8 > aResult( aData.getLength() );
70 rtlCipherError nError = rtl_Cipher_E_None;
71
72 if ( m_bEncrypt )
73 {
74 nError = rtl_cipher_encode( m_pCipher,
75 aData.getConstArray(),
76 aData.getLength(),
77 reinterpret_cast< sal_uInt8* >( aResult.getArray() ),
78 aResult.getLength() );
79 }
80 else
81 {
82 nError = rtl_cipher_decode( m_pCipher,
83 aData.getConstArray(),
84 aData.getLength(),
85 reinterpret_cast< sal_uInt8* >( aResult.getArray() ),
86 aResult.getLength() );
87 }
88
89 if ( rtl_Cipher_E_None != nError )
90 {
91 throw uno::RuntimeException("Can not decrypt/encrypt with cipher!" );
92 }
93
94 return aResult;
95}
96
98{
99 std::scoped_lock aGuard( m_aMutex );
100 if ( !m_pCipher )
101 throw lang::DisposedException();
102
103 rtl_cipher_destroy ( m_pCipher );
104 m_pCipher = nullptr;
105
106 return uno::Sequence< sal_Int8 >();
107}
108
109/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static css::uno::Reference< css::xml::crypto::XCipherContext > Create(const css::uno::Sequence< sal_Int8 > &aDerivedKey, const css::uno::Sequence< sal_Int8 > &aInitVector, bool bEncrypt)
virtual ~BlowfishCFB8CipherContext() override
virtual css::uno::Sequence< ::sal_Int8 > SAL_CALL convertWithCipherContext(const css::uno::Sequence< ::sal_Int8 > &aData) override
virtual css::uno::Sequence< ::sal_Int8 > SAL_CALL finalizeCipherContextAndDispose() override
constexpr OUStringLiteral aData
unsigned char sal_uInt8