1
/* This file is part of the KDE project
2
Copyright (C) 2001 George Staikos <staikos@kde.org>
4
This library is free software; you can redistribute it and/or
5
modify it under the terms of the GNU Library General Public
6
License as published by the Free Software Foundation; either
7
version 2 of the License, or (at your option) any later version.
9
This library is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
Library General Public License for more details.
14
You should have received a copy of the GNU Library General Public License
15
along with this library; see the file COPYING.LIB. If not, write to
16
the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17
Boston, MA 02111-1307, USA.
26
CipherBlockChain::CipherBlockChain(BlockCipher *cipher) : _cipher(cipher) {
30
_reader = _writer = 0;
32
_blksz = cipher->blockSize();
36
CipherBlockChain::~CipherBlockChain() {
37
if (_register) delete[] (char *)_register;
41
bool CipherBlockChain::setKey(void *key, int bitlength) {
42
if (_cipher) return _cipher->setKey(key, bitlength);
47
int CipherBlockChain::getKeyLen() {
48
if (_cipher) return _cipher->getKeyLen();
53
bool CipherBlockChain::variableKeyLen() {
54
if (_cipher) return _cipher->variableKeyLen();
59
bool CipherBlockChain::readyToGo() {
60
if (_cipher) return _cipher->readyToGo();
65
int CipherBlockChain::encrypt(void *block, int len) {
66
if (_cipher && !_reader) {
72
_register = new unsigned char[len];
74
memset(_register, 0, len);
75
} else if (len > _len) return -1;
77
// This might be optimizable
78
char *tb = (char *)block;
79
for (int i = 0; i < len; i++) {
80
tb[i] ^= ((char *)_register)[i];
83
rc = _cipher->encrypt(block, len);
86
memcpy(_register, block, len);
95
int CipherBlockChain::decrypt(void *block, int len) {
96
if (_cipher && !_writer) {
102
_register = new unsigned char[len];
104
memset(_register, 0, len);
105
} else if (len > _len) {
110
_next = new unsigned char[_len];
111
memcpy(_next, block, _len);
113
rc = _cipher->decrypt(block, len);
116
// This might be optimizable
117
char *tb = (char *)block;
118
for (int i = 0; i < len; i++) {
119
tb[i] ^= ((char *)_register)[i];