add more spacing
[personal-kdebase.git] / runtime / kwalletd / backend / cbc.cc
blob7bc5f3891cfcbc0abb00c464e3274850b22750bb
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.
21 #include "cbc.h"
22 #include <string.h>
26 CipherBlockChain::CipherBlockChain(BlockCipher *cipher) : _cipher(cipher) {
27 _next = 0L;
28 _register = 0L;
29 _len = -1;
30 _reader = _writer = 0L;
31 if (cipher) {
32 _blksz = cipher->blockSize();
37 CipherBlockChain::~CipherBlockChain() {
38 delete[] (char *)_register;
39 _register = 0L;
40 delete[] (char *)_next;
41 _next = 0L;
45 bool CipherBlockChain::setKey(void *key, int bitlength) {
46 if (_cipher) {
47 return _cipher->setKey(key, bitlength);
49 return false;
53 int CipherBlockChain::keyLen() const {
54 if (_cipher) {
55 return _cipher->keyLen();
57 return -1;
61 bool CipherBlockChain::variableKeyLen() const {
62 if (_cipher) {
63 return _cipher->variableKeyLen();
65 return false;
69 bool CipherBlockChain::readyToGo() const {
70 if (_cipher) {
71 return _cipher->readyToGo();
73 return false;
77 int CipherBlockChain::encrypt(void *block, int len) {
78 if (_cipher && !_reader) {
79 int rc;
81 _writer |= 1;
83 if (!_register) {
84 _register = new unsigned char[len];
85 _len = len;
86 memset(_register, 0, len);
87 } else if (len > _len) {
88 return -1;
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);
99 if (rc != -1) {
100 memcpy(_register, block, len);
103 return rc;
105 return -1;
109 int CipherBlockChain::decrypt(void *block, int len) {
110 if (_cipher && !_writer) {
111 int rc;
113 _reader |= 1;
115 if (!_register) {
116 _register = new unsigned char[len];
117 _len = len;
118 memset(_register, 0, len);
119 } else if (len > _len) {
120 return -1;
123 if (!_next)
124 _next = new unsigned char[_len];
125 memcpy(_next, block, _len);
127 rc = _cipher->decrypt(block, len);
129 if (rc != -1) {
130 // This might be optimizable
131 char *tb = (char *)block;
132 for (int i = 0; i < len; i++) {
133 tb[i] ^= ((char *)_register)[i];
137 void *temp;
138 temp = _next;
139 _next = _register;
140 _register = temp;
142 return rc;
144 return -1;