/src/libreoffice/package/source/zipapi/blowfishcontext.cxx
Line | Count | Source |
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 | | |
28 | | using namespace ::com::sun::star; |
29 | | |
30 | | // static |
31 | | uno::Reference< xml::crypto::XCipherContext > BlowfishCFB8CipherContext::Create( const uno::Sequence< sal_Int8 >& aDerivedKey, const uno::Sequence< sal_Int8 >& aInitVector, bool bEncrypt ) |
32 | 0 | { |
33 | 0 | ::rtl::Reference< BlowfishCFB8CipherContext > xResult = new BlowfishCFB8CipherContext(); |
34 | 0 | xResult->m_pCipher = rtl_cipher_create( rtl_Cipher_AlgorithmBF, rtl_Cipher_ModeStream ); |
35 | 0 | if ( !xResult->m_pCipher ) |
36 | 0 | throw uno::RuntimeException(u"Can not create cipher!"_ustr ); |
37 | | |
38 | 0 | if ( rtl_Cipher_E_None != rtl_cipher_init( |
39 | 0 | xResult->m_pCipher, |
40 | 0 | bEncrypt ? rtl_Cipher_DirectionEncode : rtl_Cipher_DirectionDecode, |
41 | 0 | reinterpret_cast< const sal_uInt8* >( aDerivedKey.getConstArray() ), |
42 | 0 | aDerivedKey.getLength(), |
43 | 0 | reinterpret_cast< const sal_uInt8* >( aInitVector.getConstArray() ), |
44 | 0 | aInitVector.getLength() ) ) |
45 | 0 | { |
46 | 0 | throw uno::RuntimeException(u"Can not initialize cipher!"_ustr ); |
47 | 0 | } |
48 | | |
49 | 0 | xResult->m_bEncrypt = bEncrypt; |
50 | |
|
51 | 0 | return xResult; |
52 | 0 | } |
53 | | |
54 | | BlowfishCFB8CipherContext::~BlowfishCFB8CipherContext() |
55 | 0 | { |
56 | 0 | if ( m_pCipher ) |
57 | 0 | { |
58 | 0 | rtl_cipher_destroy ( m_pCipher ); |
59 | 0 | m_pCipher = nullptr; |
60 | 0 | } |
61 | 0 | } |
62 | | |
63 | | uno::Sequence< sal_Int8 > SAL_CALL BlowfishCFB8CipherContext::convertWithCipherContext( const uno::Sequence< ::sal_Int8 >& aData ) |
64 | 0 | { |
65 | 0 | std::scoped_lock aGuard( m_aMutex ); |
66 | 0 | if ( !m_pCipher ) |
67 | 0 | throw lang::DisposedException(); |
68 | | |
69 | 0 | uno::Sequence< sal_Int8 > aResult( aData.getLength() ); |
70 | 0 | rtlCipherError nError = rtl_Cipher_E_None; |
71 | |
|
72 | 0 | if ( m_bEncrypt ) |
73 | 0 | { |
74 | 0 | nError = rtl_cipher_encode( m_pCipher, |
75 | 0 | aData.getConstArray(), |
76 | 0 | aData.getLength(), |
77 | 0 | reinterpret_cast< sal_uInt8* >( aResult.getArray() ), |
78 | 0 | aResult.getLength() ); |
79 | 0 | } |
80 | 0 | else |
81 | 0 | { |
82 | 0 | nError = rtl_cipher_decode( m_pCipher, |
83 | 0 | aData.getConstArray(), |
84 | 0 | aData.getLength(), |
85 | 0 | reinterpret_cast< sal_uInt8* >( aResult.getArray() ), |
86 | 0 | aResult.getLength() ); |
87 | 0 | } |
88 | |
|
89 | 0 | if ( rtl_Cipher_E_None != nError ) |
90 | 0 | { |
91 | 0 | throw uno::RuntimeException(u"Can not decrypt/encrypt with cipher!"_ustr ); |
92 | 0 | } |
93 | | |
94 | 0 | return aResult; |
95 | 0 | } |
96 | | |
97 | | uno::Sequence< ::sal_Int8 > SAL_CALL BlowfishCFB8CipherContext::finalizeCipherContextAndDispose() |
98 | 0 | { |
99 | 0 | std::scoped_lock aGuard( m_aMutex ); |
100 | 0 | if ( !m_pCipher ) |
101 | 0 | throw lang::DisposedException(); |
102 | | |
103 | 0 | rtl_cipher_destroy ( m_pCipher ); |
104 | 0 | m_pCipher = nullptr; |
105 | |
|
106 | 0 | return uno::Sequence< sal_Int8 >(); |
107 | 0 | } |
108 | | |
109 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |