KWallet

cbc.master.cc
1/*
2 This file is part of the KDE project
3 SPDX-FileCopyrightText: 2001 George Staikos <staikos@kde.org>
4
5 SPDX-License-Identifier: LGPL-2.0-or-later
6*/
7
8#include "cbc.h"
9#include <string.h>
10
11CipherBlockChain::CipherBlockChain(BlockCipher *cipher) : _cipher(cipher)
12{
13 _next = 0L;
14 _register = 0L;
15 _len = -1;
16 _reader = _writer = 0L;
17 if (cipher) {
18 _blksz = cipher->blockSize();
19 }
20}
21
22CipherBlockChain::~CipherBlockChain()
23{
24 delete[](char *)_register;
25 _register = 0L;
26 delete[](char *)_next;
27 _next = 0L;
28}
29
30bool CipherBlockChain::setKey(void *key, int bitlength)
31{
32 if (_cipher) {
33 return _cipher->setKey(key, bitlength);
34 }
35 return false;
36}
37
38int CipherBlockChain::keyLen() const
39{
40 if (_cipher) {
41 return _cipher->keyLen();
42 }
43 return -1;
44}
45
46bool CipherBlockChain::variableKeyLen() const
47{
48 if (_cipher) {
49 return _cipher->variableKeyLen();
50 }
51 return false;
52}
53
54bool CipherBlockChain::readyToGo() const
55{
56 if (_cipher) {
57 return _cipher->readyToGo();
58 }
59 return false;
60}
61
62int CipherBlockChain::encrypt(void *block, int len)
63{
64 if (_cipher && !_reader) {
65 int rc;
66
67 _writer |= 1;
68
69 if (!_register) {
70 _register = new unsigned char[len];
71 _len = len;
72 memset(_register, 0, len);
73 } else if (len > _len) {
74 return -1;
75 }
76
77 // This might be optimizable
78 char *tb = (char *)block;
79 for (int i = 0; i < len; i++) {
80 tb[i] ^= ((char *)_register)[i];
81 }
82
83 rc = _cipher->encrypt(block, len);
84
85 if (rc != -1) {
86 memcpy(_register, block, len);
87 }
88
89 return rc;
90 }
91 return -1;
92}
93
94int CipherBlockChain::decrypt(void *block, int len)
95{
96 if (_cipher && !_writer) {
97 int rc;
98
99 _reader |= 1;
100
101 if (!_register) {
102 _register = new unsigned char[len];
103 _len = len;
104 memset(_register, 0, len);
105 } else if (len > _len) {
106 return -1;
107 }
108
109 if (!_next) {
110 _next = new unsigned char[_len];
111 }
112 memcpy(_next, block, _len);
113
114 rc = _cipher->decrypt(block, len);
115
116 if (rc != -1) {
117 // This might be optimizable
118 char *tb = (char *)block;
119 for (int i = 0; i < len; i++) {
120 tb[i] ^= ((char *)_register)[i];
121 }
122 }
123
124 void *temp;
125 temp = _next;
126 _next = _register;
127 _register = temp;
128
129 return rc;
130 }
131 return -1;
132}
133
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:16:05 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.