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., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
26 CipherBlockChain::CipherBlockChain(BlockCipher
*cipher
) : _cipher(cipher
) {
30 _reader
= _writer
= 0L;
32 _blksz
= cipher
->blockSize();
37 CipherBlockChain::~CipherBlockChain() {
38 delete[] (char *)_register
;
40 delete[] (char *)_next
;
45 bool CipherBlockChain::setKey(void *key
, int bitlength
) {
47 return _cipher
->setKey(key
, bitlength
);
53 int CipherBlockChain::keyLen() const {
55 return _cipher
->keyLen();
61 bool CipherBlockChain::variableKeyLen() const {
63 return _cipher
->variableKeyLen();
69 bool CipherBlockChain::readyToGo() const {
71 return _cipher
->readyToGo();
77 int CipherBlockChain::encrypt(void *block
, int len
) {
78 if (_cipher
&& !_reader
) {
84 _register
= new unsigned char[len
];
86 memset(_register
, 0, len
);
87 } else if (len
> _len
) {
91 // This might be optimizable
92 char *tb
= (char *)block
;
93 for (int i
= 0; i
< len
; i
++) {
94 tb
[i
] ^= ((char *)_register
)[i
];
97 rc
= _cipher
->encrypt(block
, len
);
100 memcpy(_register
, block
, len
);
109 int CipherBlockChain::decrypt(void *block
, int len
) {
110 if (_cipher
&& !_writer
) {
116 _register
= new unsigned char[len
];
118 memset(_register
, 0, len
);
119 } else if (len
> _len
) {
124 _next
= new unsigned char[_len
];
125 memcpy(_next
, block
, _len
);
127 rc
= _cipher
->decrypt(block
, len
);
130 // This might be optimizable
131 char *tb
= (char *)block
;
132 for (int i
= 0; i
< len
; i
++) {
133 tb
[i
] ^= ((char *)_register
)[i
];