Source

Magnum / source / libs / magnum_crypto / source / magnum / crypto / BlowfishCipher.cpp

/* 
  Magnum 
  Copyright (C) 2002-2011 Kaya Kupferschmidt. All Rights Reserved.

  This software is provided 'as-is', without any express or implied
  warranty.  In no event will the authors be held liable for any damages
  arising from the use of this software.

  Permission is granted to anyone to use this software for any purpose,
  including commercial applications, and to alter it and redistribute it
  freely, under the terms in LICENSE.TXT.

  Kaya Kupferschmidt  (k.kupferschmidt@dimajix.de)
*/

#include "magnum/openssl/OpenSSL.h"
#include "magnum/crypto/Exception.h"
#include "magnum/crypto/BlowfishKey.h"
#include "magnum/crypto/BlowfishCipher.h"


namespace magnum { namespace crypto {
using magnum::object::IllegalArgumentException;
using magnum::openssl::OpenSSL;


/*--------------------------------------------------------------------------*/
/**
 */
BlowfishCipher::BlowfishCipher()
: AbstractCipher("BLOWFISH")
, m_Encrypt(0)
, m_INum(0)
{
    OpenSSL::startup();
}


/*--------------------------------------------------------------------------*/
/**
 */
BlowfishCipher::~BlowfishCipher()
{
}


/*--------------------------------------------------------------------------*/
/**
 */
void BlowfishCipher::initEncryptImpl(const Key* key)
{
    const BlowfishKey* bfkey = dynamic_cast<const BlowfishKey*>(key);
    if (!key)
        throw IllegalKeyException();
        
    const ByteArray& bkey = bfkey->getKey();
    size_t bkeylen = bkey.getLength();
    if (!bkeylen)
        throw IllegalKeyException();
        
    BF_set_key(&m_Key, (int)bkeylen, (const unsigned char*)bkey.getData());
    m_INum = 0;
    memset(m_IVec, 0, sizeof(m_IVec));
    m_Encrypt = 1;
}


/*--------------------------------------------------------------------------*/
/**
 */
void BlowfishCipher::initDecryptImpl(const Key* key)
{
    const BlowfishKey* bfkey = dynamic_cast<const BlowfishKey*>(key);
    if (!key)
        throw IllegalKeyException();
        
    const ByteArray& bkey = bfkey->getKey();
    size_t bkeylen = bkey.getLength();
    if (!bkeylen)
        throw IllegalKeyException();
        
    BF_set_key(&m_Key, (int)bkeylen, (const unsigned char*)bkey.getData());
    m_INum = 0;
    memset(m_IVec, 0, sizeof(m_IVec));
    m_Encrypt = 0;
}


/*--------------------------------------------------------------------------*/
/**
 */
size_t BlowfishCipher::updateImpl(const void* src, size_t len, void* dst)
{
    const unsigned char* csrc = reinterpret_cast<const unsigned char*>(src);
    unsigned char* cdst = reinterpret_cast<unsigned char*>(dst);
    BF_cfb64_encrypt(csrc, cdst, (long)len, &m_Key, m_IVec, &m_INum, m_Encrypt);
    return len;
}


/*--------------------------------------------------------------------------*/
/**
 */
size_t BlowfishCipher::finishImpl(void* )
{
    return 0;
}


/*--------------------------------------------------------------------------*/
/**
 */
size_t BlowfishCipher::outputLenImpl(size_t len) const
{
    return len;
}

} }