LCOV - code coverage report
Current view: top level - package/source/zipapi - blowfishcontext.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 33 44 75.0 %
Date: 2014-04-11 Functions: 5 5 100.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10