diff options
Diffstat (limited to 'tdewallet/backend')
-rw-r--r-- | tdewallet/backend/CMakeLists.txt | 41 | ||||
-rw-r--r-- | tdewallet/backend/Makefile.am | 25 | ||||
-rw-r--r-- | tdewallet/backend/blockcipher.cc | 37 | ||||
-rw-r--r-- | tdewallet/backend/blockcipher.h | 76 | ||||
-rw-r--r-- | tdewallet/backend/blowfish.cc | 259 | ||||
-rw-r--r-- | tdewallet/backend/blowfish.h | 73 | ||||
-rw-r--r-- | tdewallet/backend/blowfishtables.h | 214 | ||||
-rw-r--r-- | tdewallet/backend/cbc.cc | 149 | ||||
-rw-r--r-- | tdewallet/backend/cbc.h | 62 | ||||
-rw-r--r-- | tdewallet/backend/sha1.cc | 343 | ||||
-rw-r--r-- | tdewallet/backend/sha1.h | 71 | ||||
-rw-r--r-- | tdewallet/backend/tdewalletbackend.cc | 838 | ||||
-rw-r--r-- | tdewallet/backend/tdewalletbackend.h | 161 | ||||
-rw-r--r-- | tdewallet/backend/tdewalletentry.cc | 89 | ||||
-rw-r--r-- | tdewallet/backend/tdewalletentry.h | 62 | ||||
-rw-r--r-- | tdewallet/backend/tests/Makefile.am | 14 | ||||
-rw-r--r-- | tdewallet/backend/tests/backendtest.cpp | 45 | ||||
-rw-r--r-- | tdewallet/backend/tests/testbf.cpp | 67 | ||||
-rw-r--r-- | tdewallet/backend/tests/testsha.cpp | 43 |
19 files changed, 2669 insertions, 0 deletions
diff --git a/tdewallet/backend/CMakeLists.txt b/tdewallet/backend/CMakeLists.txt new file mode 100644 index 000000000..59c0bf942 --- /dev/null +++ b/tdewallet/backend/CMakeLists.txt @@ -0,0 +1,41 @@ +################################################# +# +# (C) 2010 Serghei Amelian +# serghei (DOT) amelian (AT) gmail.com +# +# Improvements and feedback are welcome +# +# This file is released under GPL >= 2 +# +################################################# + +include_directories( + ${TQT_INCLUDE_DIRS} + ${CMAKE_BINARY_DIR} + ${CMAKE_BINARY_DIR}/tdecore + ${CMAKE_SOURCE_DIR}/tdewallet/client + ${CMAKE_SOURCE_DIR}/dcop + ${CMAKE_SOURCE_DIR}/tdecore +) + +link_directories( + ${TQT_LIBRARY_DIRS} +) + + +##### libtdewalletbackend ######################### + +set( target tdewalletbackend ) + +set( ${target}_SRCS + blockcipher.cc blowfish.cc + cbc.cc sha1.cc tdewalletentry.cc + tdewalletbackend.cc +) + +tde_add_library( ${target} SHARED + SOURCES ${${target}_SRCS} + VERSION 1.0.0 + LINK tdecore-shared + DESTINATION ${LIB_INSTALL_DIR} +) diff --git a/tdewallet/backend/Makefile.am b/tdewallet/backend/Makefile.am new file mode 100644 index 000000000..79bfc0820 --- /dev/null +++ b/tdewallet/backend/Makefile.am @@ -0,0 +1,25 @@ + +INCLUDES = -I$(srcdir)/../client $(all_includes) + +lib_LTLIBRARIES = libtdewalletbackend.la + +libtdewalletbackend_la_LDFLAGS = $(KDE_RPATH) $(all_libraries) -version-info 1:0 -no-undefined +libtdewalletbackend_la_LIBADD = $(LIB_QT) ../../tdecore/libtdecore.la +libtdewalletbackend_la_SOURCES = blockcipher.cc \ + blowfish.cc \ + cbc.cc \ + sha1.cc \ + tdewalletentry.cc \ + tdewalletbackend.cc + + +libtdewalletbackend_la_METASOURCES = AUTO + +noinst_HEADERS = blowfishtables.h \ + cbc.h \ + sha1.h \ + blockcipher.h \ + tdewalletentry.h \ + tdewalletbackend.h \ + blowfish.h + diff --git a/tdewallet/backend/blockcipher.cc b/tdewallet/backend/blockcipher.cc new file mode 100644 index 000000000..9d770098e --- /dev/null +++ b/tdewallet/backend/blockcipher.cc @@ -0,0 +1,37 @@ +/* This file is part of the KDE project + Copyright (C) 2001 George Staikos <staikos@kde.org> + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#include "blockcipher.h" + + + +BlockCipher::BlockCipher() { + _blksz = -1; +} + + +BlockCipher::~BlockCipher() { +} + + +int BlockCipher::blockSize() const { + return _blksz; +} + + diff --git a/tdewallet/backend/blockcipher.h b/tdewallet/backend/blockcipher.h new file mode 100644 index 000000000..aadff6e70 --- /dev/null +++ b/tdewallet/backend/blockcipher.h @@ -0,0 +1,76 @@ +/* This file is part of the KDE project + Copyright (C) 2001 George Staikos <staikos@kde.org> + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + + +#ifndef __bcipher__ko__h +#define __bcipher__ko__h + + +/* @internal + */ +class BlockCipher { + public: + BlockCipher(); + virtual ~BlockCipher(); + + /* + * Return the current blocksize in bytes. + */ + int blockSize() const; + + /* + * Set the encryption key to key. Return true on success. + */ + virtual bool setKey(void *key, int bitlength) = 0; + + /* + * Get the required (or if it's variable, then the maximum) key + * length for this cipher in bits. + */ + virtual int keyLen() const = 0; + + /* + * True if the key is of a variable length. In this case, + * getKeyLen() will return the maximum length. + */ + virtual bool variableKeyLen() const = 0; + + /* + * True if all settings are good and we are ready to encrypt. + */ + virtual bool readyToGo() const = 0; + + /* + * Encrypt the block. Returns the number of bytes successfully + * encrypted. Can return -1 on error. + */ + virtual int encrypt(void *block, int len) = 0; + + /* + * Decrypt the block. Returns as does encrypt(); + */ + virtual int decrypt(void *block, int len) = 0; + + protected: + int _blksz; + int _keylen; // in bits +}; + + +#endif diff --git a/tdewallet/backend/blowfish.cc b/tdewallet/backend/blowfish.cc new file mode 100644 index 000000000..c708935de --- /dev/null +++ b/tdewallet/backend/blowfish.cc @@ -0,0 +1,259 @@ +/* This file is part of the KDE project + Copyright (C) 2001 George Staikos <staikos@kde.org> + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + + +// FIXME: should we unroll some loops? Optimization can be done here. + + +/* Implementation of 16 rounds blowfish as described in: + * _Applied_Cryptography_ (c) Bruce Schneier, 1996. + */ + +#include <config.h> + +#include <stdlib.h> +#include <stdio.h> +#include <string.h> + +#include "blowfish.h" +#include "blowfishtables.h" + + +BlowFish::BlowFish() { + _blksz = 8; + _key = 0L; + _init = false; +} + + + +bool BlowFish::init() { + // Initialize the sboxes + for (int i = 0; i < 256; i++) { + _S[0][i] = ks0[i]; + _S[1][i] = ks1[i]; + _S[2][i] = ks2[i]; + _S[3][i] = ks3[i]; + } + + uint32_t datal = 0; + uint32_t datar = 0; + uint32_t data = 0; + int j = 0; + + + // Update the sboxes and pbox. + for (int i = 0; i < 18; i++) { + data = 0; + for (int k = 0; k < 4; ++k) { + data = (data << 8) | ((unsigned char *)_key)[j++]; + if (j >= _keylen / 8) { + j = 0; + } + } + _P[i] = P[i] ^ data; + } + + for (int i = 0; i < 18; i += 2) { + encipher(&datal, &datar); + _P[i] = datal; + _P[i+1] = datar; + } + + for (int j = 0; j < 4; j++) { + for (int i = 0; i < 256; i += 2) { + encipher(&datal, &datar); + _S[j][i] = datal; + _S[j][i+1] = datar; + } + } + + // Nice code from gpg's implementation... + // check to see if the key is weak and return error if so + for (int i = 0; i < 255; i++) { + for (int j = i + 1; j < 256; j++) { + if ((_S[0][i] == _S[0][j]) || (_S[1][i] == _S[1][j]) || + (_S[2][i] == _S[2][j]) || (_S[3][i] == _S[3][j])) { + return false; + } + } + } + + _init = true; + + return true; +} + + +BlowFish::~BlowFish() { + delete[] (unsigned char *)_key; + _key = 0L; +} + + +int BlowFish::keyLen() const { + return 448; +} + + +bool BlowFish::variableKeyLen() const { + return true; +} + + +bool BlowFish::readyToGo() const { + return _init; +} + + +bool BlowFish::setKey(void *key, int bitlength) { + if (bitlength <= 0 || bitlength > 448 || bitlength % 8 != 0) { + return false; + } + + delete[] (unsigned char *)_key; + + _key = new unsigned char[bitlength / 8]; + memcpy(_key, key, bitlength / 8); + _keylen = bitlength; + + return init(); +} + + +#ifdef WORDS_BIGENDIAN +#define shuffle(x) do { \ + uint32_t r = x; \ + x = (r & 0xff000000) >> 24; \ + x |= (r & 0x00ff0000) >> 8; \ + x |= (r & 0x0000ff00) << 8; \ + x |= (r & 0x000000ff) << 24; \ + } while (0) +#endif + +int BlowFish::encrypt(void *block, int len) { + uint32_t *d = (uint32_t *)block; + + if (!_init || len % _blksz != 0) { + return -1; + } + + for (int i = 0; i < len / _blksz; i++) { +#ifdef WORDS_BIGENDIAN + shuffle(*d); + shuffle(*(d + 1)); +#endif + encipher(d, d + 1); +#ifdef WORDS_BIGENDIAN + shuffle(*d); + shuffle(*(d + 1)); +#endif + d += 2; + } + + return len; +} + + +int BlowFish::decrypt(void *block, int len) { + uint32_t *d = (uint32_t *)block; + + if (!_init || len % _blksz != 0) { + return -1; + } + + for (int i = 0; i < len / _blksz; i++) { +#ifdef WORDS_BIGENDIAN + shuffle(*d); + shuffle(*(d + 1)); +#endif + decipher(d, d + 1); +#ifdef WORDS_BIGENDIAN + shuffle(*d); + shuffle(*(d + 1)); +#endif + d += 2; + } + + return len; +} + + +uint32_t BlowFish::F(uint32_t x) { + unsigned short a, b, c, d; + uint32_t y; + + d = x & 0x000000ff; + x >>= 8; + c = x & 0x000000ff; + x >>= 8; + b = x & 0x000000ff; + x >>= 8; + a = x & 0x000000ff; + + y = _S[0][a] + _S[1][b]; + y ^= _S[2][c]; + y += _S[3][d]; + + return y; +} + + +void BlowFish::encipher(uint32_t *xl, uint32_t *xr) { + uint32_t Xl = *xl, Xr = *xr, temp; + + for (int i = 0; i < 16; ++i) { + Xl ^= _P[i]; + Xr ^= F(Xl); + // Exchange + temp = Xl; Xl = Xr; Xr = temp; + } + + // Exchange + temp = Xl; Xl = Xr; Xr = temp; + + Xr ^= _P[16]; + Xl ^= _P[17]; + + *xl = Xl; + *xr = Xr; +} + + +void BlowFish::decipher(uint32_t *xl, uint32_t *xr) { + uint32_t Xl = *xl, Xr = *xr, temp; + + for (int i = 17; i > 1; --i) { + Xl ^= _P[i]; + Xr ^= F(Xl); + // Exchange + temp = Xl; Xl = Xr; Xr = temp; + } + + // Exchange + temp = Xl; Xl = Xr; Xr = temp; + + Xr ^= _P[1]; + Xl ^= _P[0]; + + *xl = Xl; + *xr = Xr; +} + + diff --git a/tdewallet/backend/blowfish.h b/tdewallet/backend/blowfish.h new file mode 100644 index 000000000..581ac31c0 --- /dev/null +++ b/tdewallet/backend/blowfish.h @@ -0,0 +1,73 @@ +/* This file is part of the KDE project + Copyright (C) 2001 George Staikos <staikos@kde.org> + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#ifndef _BLOWFISH_H +#define _BLOWFISH_H + +#include <config.h> + +#ifdef HAVE_STDINT_H +#include <stdint.h> +#endif +#ifdef HAVE_SYS_TYPES_H +#include <sys/types.h> +#endif +#ifdef HAVE_SYS_BITYPES_H +#include <sys/bitypes.h> /* For uintXX_t on Tru64 */ +#endif + +#include "blockcipher.h" + + +/* @internal + */ +class BlowFish : public BlockCipher { + public: + BlowFish(); + virtual ~BlowFish(); + + virtual bool setKey(void *key, int bitlength); + + virtual int keyLen() const; + + virtual bool variableKeyLen() const; + + virtual bool readyToGo() const; + + virtual int encrypt(void *block, int len); + + virtual int decrypt(void *block, int len); + + private: + uint32_t _S[4][256]; + uint32_t _P[18]; + + void *_key; + int _keylen; // in bits + + bool _init; + + bool init(); + uint32_t F(uint32_t x); + void encipher(uint32_t *xl, uint32_t *xr); + void decipher(uint32_t *xl, uint32_t *xr); +}; + +#endif + diff --git a/tdewallet/backend/blowfishtables.h b/tdewallet/backend/blowfishtables.h new file mode 100644 index 000000000..dae448f96 --- /dev/null +++ b/tdewallet/backend/blowfishtables.h @@ -0,0 +1,214 @@ +/* This file is part of the KDE project + Copyright (C) 2001 George Staikos <staikos@kde.org> + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +// This is also from _Applied_Cryptography_. See blowfish.cc for more details. + +#ifndef __bfdefs_h +#define __bfdefs_h + + +const unsigned long ks0[256] = { + 0xd1310ba6, 0x98dfb5ac, 0x2ffd72db, 0xd01adfb7, 0xb8e1afed, 0x6a267e96, + 0xba7c9045, 0xf12c7f99, 0x24a19947, 0xb3916cf7, 0x0801f2e2, 0x858efc16, + 0x636920d8, 0x71574e69, 0xa458fea3, 0xf4933d7e, 0x0d95748f, 0x728eb658, + 0x718bcd58, 0x82154aee, 0x7b54a41d, 0xc25a59b5, 0x9c30d539, 0x2af26013, + 0xc5d1b023, 0x286085f0, 0xca417918, 0xb8db38ef, 0x8e79dcb0, 0x603a180e, + 0x6c9e0e8b, 0xb01e8a3e, 0xd71577c1, 0xbd314b27, 0x78af2fda, 0x55605c60, + 0xe65525f3, 0xaa55ab94, 0x57489862, 0x63e81440, 0x55ca396a, 0x2aab10b6, + 0xb4cc5c34, 0x1141e8ce, 0xa15486af, 0x7c72e993, 0xb3ee1411, 0x636fbc2a, + 0x2ba9c55d, 0x741831f6, 0xce5c3e16, 0x9b87931e, 0xafd6ba33, 0x6c24cf5c, + 0x7a325381, 0x28958677, 0x3b8f4898, 0x6b4bb9af, 0xc4bfe81b, 0x66282193, + 0x61d809cc, 0xfb21a991, 0x487cac60, 0x5dec8032, 0xef845d5d, 0xe98575b1, + 0xdc262302, 0xeb651b88, 0x23893e81, 0xd396acc5, 0x0f6d6ff3, 0x83f44239, + 0x2e0b4482, 0xa4842004, 0x69c8f04a, 0x9e1f9b5e, 0x21c66842, 0xf6e96c9a, + 0x670c9c61, 0xabd388f0, 0x6a51a0d2, 0xd8542f68, 0x960fa728, 0xab5133a3, + 0x6eef0b6c, 0x137a3be4, 0xba3bf050, 0x7efb2a98, 0xa1f1651d, 0x39af0176, + 0x66ca593e, 0x82430e88, 0x8cee8619, 0x456f9fb4, 0x7d84a5c3, 0x3b8b5ebe, + 0xe06f75d8, 0x85c12073, 0x401a449f, 0x56c16aa6, 0x4ed3aa62, 0x363f7706, + 0x1bfedf72, 0x429b023d, 0x37d0d724, 0xd00a1248, 0xdb0fead3, 0x49f1c09b, + 0x075372c9, 0x80991b7b, 0x25d479d8, 0xf6e8def7, 0xe3fe501a, 0xb6794c3b, + 0x976ce0bd, 0x04c006ba, 0xc1a94fb6, 0x409f60c4, 0x5e5c9ec2, 0x196a2463, + 0x68fb6faf, 0x3e6c53b5, 0x1339b2eb, 0x3b52ec6f, 0x6dfc511f, 0x9b30952c, + 0xcc814544, 0xaf5ebd09, 0xbee3d004, 0xde334afd, 0x660f2807, 0x192e4bb3, + 0xc0cba857, 0x45c8740f, 0xd20b5f39, 0xb9d3fbdb, 0x5579c0bd, 0x1a60320a, + 0xd6a100c6, 0x402c7279, 0x679f25fe, 0xfb1fa3cc, 0x8ea5e9f8, 0xdb3222f8, + 0x3c7516df, 0xfd616b15, 0x2f501ec8, 0xad0552ab, 0x323db5fa, 0xfd238760, + 0x53317b48, 0x3e00df82, 0x9e5c57bb, 0xca6f8ca0, 0x1a87562e, 0xdf1769db, + 0xd542a8f6, 0x287effc3, 0xac6732c6, 0x8c4f5573, 0x695b27b0, 0xbbca58c8, + 0xe1ffa35d, 0xb8f011a0, 0x10fa3d98, 0xfd2183b8, 0x4afcb56c, 0x2dd1d35b, + 0x9a53e479, 0xb6f84565, 0xd28e49bc, 0x4bfb9790, 0xe1ddf2da, 0xa4cb7e33, + 0x62fb1341, 0xcee4c6e8, 0xef20cada, 0x36774c01, 0xd07e9efe, 0x2bf11fb4, + 0x95dbda4d, 0xae909198, 0xeaad8e71, 0x6b93d5a0, 0xd08ed1d0, 0xafc725e0, + 0x8e3c5b2f, 0x8e7594b7, 0x8ff6e2fb, 0xf2122b64, 0x8888b812, 0x900df01c, + 0x4fad5ea0, 0x688fc31c, 0xd1cff191, 0xb3a8c1ad, 0x2f2f2218, 0xbe0e1777, + 0xea752dfe, 0x8b021fa1, 0xe5a0cc0f, 0xb56f74e8, 0x18acf3d6, 0xce89e299, + 0xb4a84fe0, 0xfd13e0b7, 0x7cc43b81, 0xd2ada8d9, 0x165fa266, 0x80957705, + 0x93cc7314, 0x211a1477, 0xe6ad2065, 0x77b5fa86, 0xc75442f5, 0xfb9d35cf, + 0xebcdaf0c, 0x7b3e89a0, 0xd6411bd3, 0xae1e7e49, 0x00250e2d, 0x2071b35e, + 0x226800bb, 0x57b8e0af, 0x2464369b, 0xf009b91e, 0x5563911d, 0x59dfa6aa, + 0x78c14389, 0xd95a537f, 0x207d5ba2, 0x02e5b9c5, 0x83260376, 0x6295cfa9, + 0x11c81968, 0x4e734a41, 0xb3472dca, 0x7b14a94a, 0x1b510052, 0x9a532915, + 0xd60f573f, 0xbc9bc6e4, 0x2b60a476, 0x81e67400, 0x08ba6fb5, 0x571be91f, + 0xf296ec6b, 0x2a0dd915, 0xb6636521, 0xe7b9f9b6, 0xff34052e, 0xc5855664, + 0x53b02d5d, 0xa99f8fa1, 0x08ba4799, 0x6e85076a }; + +const unsigned long ks1[256] = { + 0x4b7a70e9, 0xb5b32944, 0xdb75092e, 0xc4192623, 0xad6ea6b0, 0x49a7df7d, + 0x9cee60b8, 0x8fedb266, 0xecaa8c71, 0x699a17ff, 0x5664526c, 0xc2b19ee1, + 0x193602a5, 0x75094c29, 0xa0591340, 0xe4183a3e, 0x3f54989a, 0x5b429d65, + 0x6b8fe4d6, 0x99f73fd6, 0xa1d29c07, 0xefe830f5, 0x4d2d38e6, 0xf0255dc1, + 0x4cdd2086, 0x8470eb26, 0x6382e9c6, 0x021ecc5e, 0x09686b3f, 0x3ebaefc9, + 0x3c971814, 0x6b6a70a1, 0x687f3584, 0x52a0e286, 0xb79c5305, 0xaa500737, + 0x3e07841c, 0x7fdeae5c, 0x8e7d44ec, 0x5716f2b8, 0xb03ada37, 0xf0500c0d, + 0xf01c1f04, 0x0200b3ff, 0xae0cf51a, 0x3cb574b2, 0x25837a58, 0xdc0921bd, + 0xd19113f9, 0x7ca92ff6, 0x94324773, 0x22f54701, 0x3ae5e581, 0x37c2dadc, + 0xc8b57634, 0x9af3dda7, 0xa9446146, 0x0fd0030e, 0xecc8c73e, 0xa4751e41, + 0xe238cd99, 0x3bea0e2f, 0x3280bba1, 0x183eb331, 0x4e548b38, 0x4f6db908, + 0x6f420d03, 0xf60a04bf, 0x2cb81290, 0x24977c79, 0x5679b072, 0xbcaf89af, + 0xde9a771f, 0xd9930810, 0xb38bae12, 0xdccf3f2e, 0x5512721f, 0x2e6b7124, + 0x501adde6, 0x9f84cd87, 0x7a584718, 0x7408da17, 0xbc9f9abc, 0xe94b7d8c, + 0xec7aec3a, 0xdb851dfa, 0x63094366, 0xc464c3d2, 0xef1c1847, 0x3215d908, + 0xdd433b37, 0x24c2ba16, 0x12a14d43, 0x2a65c451, 0x50940002, 0x133ae4dd, + 0x71dff89e, 0x10314e55, 0x81ac77d6, 0x5f11199b, 0x043556f1, 0xd7a3c76b, + 0x3c11183b, 0x5924a509, 0xf28fe6ed, 0x97f1fbfa, 0x9ebabf2c, 0x1e153c6e, + 0x86e34570, 0xeae96fb1, 0x860e5e0a, 0x5a3e2ab3, 0x771fe71c, 0x4e3d06fa, + 0x2965dcb9, 0x99e71d0f, 0x803e89d6, 0x5266c825, 0x2e4cc978, 0x9c10b36a, + 0xc6150eba, 0x94e2ea78, 0xa5fc3c53, 0x1e0a2df4, 0xf2f74ea7, 0x361d2b3d, + 0x1939260f, 0x19c27960, 0x5223a708, 0xf71312b6, 0xebadfe6e, 0xeac31f66, + 0xe3bc4595, 0xa67bc883, 0xb17f37d1, 0x018cff28, 0xc332ddef, 0xbe6c5aa5, + 0x65582185, 0x68ab9802, 0xeecea50f, 0xdb2f953b, 0x2aef7dad, 0x5b6e2f84, + 0x1521b628, 0x29076170, 0xecdd4775, 0x619f1510, 0x13cca830, 0xeb61bd96, + 0x0334fe1e, 0xaa0363cf, 0xb5735c90, 0x4c70a239, 0xd59e9e0b, 0xcbaade14, + 0xeecc86bc, 0x60622ca7, 0x9cab5cab, 0xb2f3846e, 0x648b1eaf, 0x19bdf0ca, + 0xa02369b9, 0x655abb50, 0x40685a32, 0x3c2ab4b3, 0x319ee9d5, 0xc021b8f7, + 0x9b540b19, 0x875fa099, 0x95f7997e, 0x623d7da8, 0xf837889a, 0x97e32d77, + 0x11ed935f, 0x16681281, 0x0e358829, 0xc7e61fd6, 0x96dedfa1, 0x7858ba99, + 0x57f584a5, 0x1b227263, 0x9b83c3ff, 0x1ac24696, 0xcdb30aeb, 0x532e3054, + 0x8fd948e4, 0x6dbc3128, 0x58ebf2ef, 0x34c6ffea, 0xfe28ed61, 0xee7c3c73, + 0x5d4a14d9, 0xe864b7e3, 0x42105d14, 0x203e13e0, 0x45eee2b6, 0xa3aaabea, + 0xdb6c4f15, 0xfacb4fd0, 0xc742f442, 0xef6abbb5, 0x654f3b1d, 0x41cd2105, + 0xd81e799e, 0x86854dc7, 0xe44b476a, 0x3d816250, 0xcf62a1f2, 0x5b8d2646, + 0xfc8883a0, 0xc1c7b6a3, 0x7f1524c3, 0x69cb7492, 0x47848a0b, 0x5692b285, + 0x095bbf00, 0xad19489d, 0x1462b174, 0x23820e00, 0x58428d2a, 0x0c55f5ea, + 0x1dadf43e, 0x233f7061, 0x3372f092, 0x8d937e41, 0xd65fecf1, 0x6c223bdb, + 0x7cde3759, 0xcbee7460, 0x4085f2a7, 0xce77326e, 0xa6078084, 0x19f8509e, + 0xe8efd855, 0x61d99735, 0xa969a7aa, 0xc50c06c2, 0x5a04abfc, 0x800bcadc, + 0x9e447a2e, 0xc3453484, 0xfdd56705, 0x0e1e9ec9, 0xdb73dbd3, 0x105588cd, + 0x675fda79, 0xe3674340, 0xc5c43465, 0x713e38d8, 0x3d28f89e, 0xf16dff20, + 0x153e21e7, 0x8fb03d4a, 0xe6e39f2b, 0xdb83adf7 }; + +const unsigned long ks2[256] = { + 0xe93d5a68, 0x948140f7, 0xf64c261c, 0x94692934, 0x411520f7, 0x7602d4f7, + 0xbcf46b2e, 0xd4a20068, 0xd4082471, 0x3320f46a, 0x43b7d4b7, 0x500061af, + 0x1e39f62e, 0x97244546, 0x14214f74, 0xbf8b8840, 0x4d95fc1d, 0x96b591af, + 0x70f4ddd3, 0x66a02f45, 0xbfbc09ec, 0x03bd9785, 0x7fac6dd0, 0x31cb8504, + 0x96eb27b3, 0x55fd3941, 0xda2547e6, 0xabca0a9a, 0x28507825, 0x530429f4, + 0x0a2c86da, 0xe9b66dfb, 0x68dc1462, 0xd7486900, 0x680ec0a4, 0x27a18dee, + 0x4f3ffea2, 0xe887ad8c, 0xb58ce006, 0x7af4d6b6, 0xaace1e7c, 0xd3375fec, + 0xce78a399, 0x406b2a42, 0x20fe9e35, 0xd9f385b9, 0xee39d7ab, 0x3b124e8b, + 0x1dc9faf7, 0x4b6d1856, 0x26a36631, 0xeae397b2, 0x3a6efa74, 0xdd5b4332, + 0x6841e7f7, 0xca7820fb, 0xfb0af54e, 0xd8feb397, 0x454056ac, 0xba489527, + 0x55533a3a, 0x20838d87, 0xfe6ba9b7, 0xd096954b, 0x55a867bc, 0xa1159a58, + 0xcca92963, 0x99e1db33, 0xa62a4a56, 0x3f3125f9, 0x5ef47e1c, 0x9029317c, + 0xfdf8e802, 0x04272f70, 0x80bb155c, 0x05282ce3, 0x95c11548, 0xe4c66d22, + 0x48c1133f, 0xc70f86dc, 0x07f9c9ee, 0x41041f0f, 0x404779a4, 0x5d886e17, + 0x325f51eb, 0xd59bc0d1, 0xf2bcc18f, 0x41113564, 0x257b7834, 0x602a9c60, + 0xdff8e8a3, 0x1f636c1b, 0x0e12b4c2, 0x02e1329e, 0xaf664fd1, 0xcad18115, + 0x6b2395e0, 0x333e92e1, 0x3b240b62, 0xeebeb922, 0x85b2a20e, 0xe6ba0d99, + 0xde720c8c, 0x2da2f728, 0xd0127845, 0x95b794fd, 0x647d0862, 0xe7ccf5f0, + 0x5449a36f, 0x877d48fa, 0xc39dfd27, 0xf33e8d1e, 0x0a476341, 0x992eff74, + 0x3a6f6eab, 0xf4f8fd37, 0xa812dc60, 0xa1ebddf8, 0x991be14c, 0xdb6e6b0d, + 0xc67b5510, 0x6d672c37, 0x2765d43b, 0xdcd0e804, 0xf1290dc7, 0xcc00ffa3, + 0xb5390f92, 0x690fed0b, 0x667b9ffb, 0xcedb7d9c, 0xa091cf0b, 0xd9155ea3, + 0xbb132f88, 0x515bad24, 0x7b9479bf, 0x763bd6eb, 0x37392eb3, 0xcc115979, + 0x8026e297, 0xf42e312d, 0x6842ada7, 0xc66a2b3b, 0x12754ccc, 0x782ef11c, + 0x6a124237, 0xb79251e7, 0x06a1bbe6, 0x4bfb6350, 0x1a6b1018, 0x11caedfa, + 0x3d25bdd8, 0xe2e1c3c9, 0x44421659, 0x0a121386, 0xd90cec6e, 0xd5abea2a, + 0x64af674e, 0xda86a85f, 0xbebfe988, 0x64e4c3fe, 0x9dbc8057, 0xf0f7c086, + 0x60787bf8, 0x6003604d, 0xd1fd8346, 0xf6381fb0, 0x7745ae04, 0xd736fccc, + 0x83426b33, 0xf01eab71, 0xb0804187, 0x3c005e5f, 0x77a057be, 0xbde8ae24, + 0x55464299, 0xbf582e61, 0x4e58f48f, 0xf2ddfda2, 0xf474ef38, 0x8789bdc2, + 0x5366f9c3, 0xc8b38e74, 0xb475f255, 0x46fcd9b9, 0x7aeb2661, 0x8b1ddf84, + 0x846a0e79, 0x915f95e2, 0x466e598e, 0x20b45770, 0x8cd55591, 0xc902de4c, + 0xb90bace1, 0xbb8205d0, 0x11a86248, 0x7574a99e, 0xb77f19b6, 0xe0a9dc09, + 0x662d09a1, 0xc4324633, 0xe85a1f02, 0x09f0be8c, 0x4a99a025, 0x1d6efe10, + 0x1ab93d1d, 0x0ba5a4df, 0xa186f20f, 0x2868f169, 0xdcb7da83, 0x573906fe, + 0xa1e2ce9b, 0x4fcd7f52, 0x50115e01, 0xa70683fa, 0xa002b5c4, 0x0de6d027, + 0x9af88c27, 0x773f8641, 0xc3604c06, 0x61a806b5, 0xf0177a28, 0xc0f586e0, + 0x006058aa, 0x30dc7d62, 0x11e69ed7, 0x2338ea63, 0x53c2dd94, 0xc2c21634, + 0xbbcbee56, 0x90bcb6de, 0xebfc7da1, 0xce591d76, 0x6f05e409, 0x4b7c0188, + 0x39720a3d, 0x7c927c24, 0x86e3725f, 0x724d9db9, 0x1ac15bb4, 0xd39eb8fc, + 0xed545578, 0x08fca5b5, 0xd83d7cd3, 0x4dad0fc4, 0x1e50ef5e, 0xb161e6f8, + 0xa28514d9, 0x6c51133c, 0x6fd5c7e7, 0x56e14ec4, 0x362abfce, 0xddc6c837, + 0xd79a3234, 0x92638212, 0x670efa8e, 0x406000e0 }; + +const unsigned long ks3[256] = { + 0x3a39ce37, 0xd3faf5cf, 0xabc27737, 0x5ac52d1b, 0x5cb0679e, 0x4fa33742, + 0xd3822740, 0x99bc9bbe, 0xd5118e9d, 0xbf0f7315, 0xd62d1c7e, 0xc700c47b, + 0xb78c1b6b, 0x21a19045, 0xb26eb1be, 0x6a366eb4, 0x5748ab2f, 0xbc946e79, + 0xc6a376d2, 0x6549c2c8, 0x530ff8ee, 0x468dde7d, 0xd5730a1d, 0x4cd04dc6, + 0x2939bbdb, 0xa9ba4650, 0xac9526e8, 0xbe5ee304, 0xa1fad5f0, 0x6a2d519a, + 0x63ef8ce2, 0x9a86ee22, 0xc089c2b8, 0x43242ef6, 0xa51e03aa, 0x9cf2d0a4, + 0x83c061ba, 0x9be96a4d, 0x8fe51550, 0xba645bd6, 0x2826a2f9, 0xa73a3ae1, + 0x4ba99586, 0xef5562e9, 0xc72fefd3, 0xf752f7da, 0x3f046f69, 0x77fa0a59, + 0x80e4a915, 0x87b08601, 0x9b09e6ad, 0x3b3ee593, 0xe990fd5a, 0x9e34d797, + 0x2cf0b7d9, 0x022b8b51, 0x96d5ac3a, 0x017da67d, 0xd1cf3ed6, 0x7c7d2d28, + 0x1f9f25cf, 0xadf2b89b, 0x5ad6b472, 0x5a88f54c, 0xe029ac71, 0xe019a5e6, + 0x47b0acfd, 0xed93fa9b, 0xe8d3c48d, 0x283b57cc, 0xf8d56629, 0x79132e28, + 0x785f0191, 0xed756055, 0xf7960e44, 0xe3d35e8c, 0x15056dd4, 0x88f46dba, + 0x03a16125, 0x0564f0bd, 0xc3eb9e15, 0x3c9057a2, 0x97271aec, 0xa93a072a, + 0x1b3f6d9b, 0x1e6321f5, 0xf59c66fb, 0x26dcf319, 0x7533d928, 0xb155fdf5, + 0x03563482, 0x8aba3cbb, 0x28517711, 0xc20ad9f8, 0xabcc5167, 0xccad925f, + 0x4de81751, 0x3830dc8e, 0x379d5862, 0x9320f991, 0xea7a90c2, 0xfb3e7bce, + 0x5121ce64, 0x774fbe32, 0xa8b6e37e, 0xc3293d46, 0x48de5369, 0x6413e680, + 0xa2ae0810, 0xdd6db224, 0x69852dfd, 0x09072166, 0xb39a460a, 0x6445c0dd, + 0x586cdecf, 0x1c20c8ae, 0x5bbef7dd, 0x1b588d40, 0xccd2017f, 0x6bb4e3bb, + 0xdda26a7e, 0x3a59ff45, 0x3e350a44, 0xbcb4cdd5, 0x72eacea8, 0xfa6484bb, + 0x8d6612ae, 0xbf3c6f47, 0xd29be463, 0x542f5d9e, 0xaec2771b, 0xf64e6370, + 0x740e0d8d, 0xe75b1357, 0xf8721671, 0xaf537d5d, 0x4040cb08, 0x4eb4e2cc, + 0x34d2466a, 0x0115af84, 0xe1b00428, 0x95983a1d, 0x06b89fb4, 0xce6ea048, + 0x6f3f3b82, 0x3520ab82, 0x011a1d4b, 0x277227f8, 0x611560b1, 0xe7933fdc, + 0xbb3a792b, 0x344525bd, 0xa08839e1, 0x51ce794b, 0x2f32c9b7, 0xa01fbac9, + 0xe01cc87e, 0xbcc7d1f6, 0xcf0111c3, 0xa1e8aac7, 0x1a908749, 0xd44fbd9a, + 0xd0dadecb, 0xd50ada38, 0x0339c32a, 0xc6913667, 0x8df9317c, 0xe0b12b4f, + 0xf79e59b7, 0x43f5bb3a, 0xf2d519ff, 0x27d9459c, 0xbf97222c, 0x15e6fc2a, + 0x0f91fc71, 0x9b941525, 0xfae59361, 0xceb69ceb, 0xc2a86459, 0x12baa8d1, + 0xb6c1075e, 0xe3056a0c, 0x10d25065, 0xcb03a442, 0xe0ec6e0e, 0x1698db3b, + 0x4c98a0be, 0x3278e964, 0x9f1f9532, 0xe0d392df, 0xd3a0342b, 0x8971f21e, + 0x1b0a7441, 0x4ba3348c, 0xc5be7120, 0xc37632d8, 0xdf359f8d, 0x9b992f2e, + 0xe60b6f47, 0x0fe3f11d, 0xe54cda54, 0x1edad891, 0xce6279cf, 0xcd3e7e6f, + 0x1618b166, 0xfd2c1d05, 0x848fd2c5, 0xf6fb2299, 0xf523f357, 0xa6327623, + 0x93a83531, 0x56cccd02, 0xacf08162, 0x5a75ebb5, 0x6e163697, 0x88d273cc, + 0xde966292, 0x81b949d0, 0x4c50901b, 0x71c65614, 0xe6c6c7bd, 0x327a140a, + 0x45e1d006, 0xc3f27b9a, 0xc9aa53fd, 0x62a80f00, 0xbb25bfe2, 0x35bdd2f6, + 0x71126905, 0xb2040222, 0xb6cbcf7c, 0xcd769c2b, 0x53113ec0, 0x1640e3d3, + 0x38abbd60, 0x2547adf0, 0xba38209c, 0xf746ce76, 0x77afa1c5, 0x20756060, + 0x85cbfe4e, 0x8ae88dd8, 0x7aaaf9b0, 0x4cf9aa7e, 0x1948c25c, 0x02fb8a8c, + 0x01c36ae4, 0xd6ebe1f9, 0x90d4f869, 0xa65cdea0, 0x3f09252d, 0xc208e69f, + 0xb74e6132, 0xce77e25b, 0x578fdfe3, 0x3ac372e6 }; + +const unsigned long P[18] = { + 0x243f6a88, 0x85a308d3, 0x13198a2e, 0x03707344, 0xa4093822, 0x299f31d0, + 0x082efa98, 0xec4e6c89, 0x452821e6, 0x38d01377, 0xbe5466cf, 0x34e90c6c, + 0xc0ac29b7, 0xc97c50dd, 0x3f84d5b5, 0xb5470917, 0x9216d5d9, 0x8979fb1b }; + + +#endif + + diff --git a/tdewallet/backend/cbc.cc b/tdewallet/backend/cbc.cc new file mode 100644 index 000000000..7bc5f3891 --- /dev/null +++ b/tdewallet/backend/cbc.cc @@ -0,0 +1,149 @@ +/* This file is part of the KDE project + Copyright (C) 2001 George Staikos <staikos@kde.org> + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + + +#include "cbc.h" +#include <string.h> + + + +CipherBlockChain::CipherBlockChain(BlockCipher *cipher) : _cipher(cipher) { + _next = 0L; + _register = 0L; + _len = -1; + _reader = _writer = 0L; + if (cipher) { + _blksz = cipher->blockSize(); + } +} + + +CipherBlockChain::~CipherBlockChain() { + delete[] (char *)_register; + _register = 0L; + delete[] (char *)_next; + _next = 0L; +} + + +bool CipherBlockChain::setKey(void *key, int bitlength) { + if (_cipher) { + return _cipher->setKey(key, bitlength); + } + return false; +} + + +int CipherBlockChain::keyLen() const { + if (_cipher) { + return _cipher->keyLen(); + } + return -1; +} + + +bool CipherBlockChain::variableKeyLen() const { + if (_cipher) { + return _cipher->variableKeyLen(); + } + return false; +} + + +bool CipherBlockChain::readyToGo() const { + if (_cipher) { + return _cipher->readyToGo(); + } + return false; +} + + +int CipherBlockChain::encrypt(void *block, int len) { + if (_cipher && !_reader) { + int rc; + + _writer |= 1; + + if (!_register) { + _register = new unsigned char[len]; + _len = len; + memset(_register, 0, len); + } else if (len > _len) { + return -1; + } + + // This might be optimizable + char *tb = (char *)block; + for (int i = 0; i < len; i++) { + tb[i] ^= ((char *)_register)[i]; + } + + rc = _cipher->encrypt(block, len); + + if (rc != -1) { + memcpy(_register, block, len); + } + + return rc; + } + return -1; +} + + +int CipherBlockChain::decrypt(void *block, int len) { + if (_cipher && !_writer) { + int rc; + + _reader |= 1; + + if (!_register) { + _register = new unsigned char[len]; + _len = len; + memset(_register, 0, len); + } else if (len > _len) { + return -1; + } + + if (!_next) + _next = new unsigned char[_len]; + memcpy(_next, block, _len); + + rc = _cipher->decrypt(block, len); + + if (rc != -1) { + // This might be optimizable + char *tb = (char *)block; + for (int i = 0; i < len; i++) { + tb[i] ^= ((char *)_register)[i]; + } + } + + void *temp; + temp = _next; + _next = _register; + _register = temp; + + return rc; + } + return -1; +} + + + + diff --git a/tdewallet/backend/cbc.h b/tdewallet/backend/cbc.h new file mode 100644 index 000000000..1ce971ab3 --- /dev/null +++ b/tdewallet/backend/cbc.h @@ -0,0 +1,62 @@ +/* This file is part of the KDE project + Copyright (C) 2001 George Staikos <staikos@kde.org> + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + + + +#ifndef __CBC__KO__H +#define __CBC__KO__H + +#include "blockcipher.h" + +/* @internal + * Initialize this class with a pointer to a valid, uninitialized BlockCipher + * and it will apply that cipher using CBC. You may want to make the + * initial block a full block of random data. Do not change the block size + * at any time!! You must pad it yourself. Also, you can only encrypt or + * decrypt. You can't do both with a given instance. After you call one, + * calls to the other will fail in this instance. + */ + +class CipherBlockChain : public BlockCipher { + public: + CipherBlockChain(BlockCipher *cipher); + virtual ~CipherBlockChain(); + + virtual bool setKey(void *key, int bitlength); + + virtual int keyLen() const; + + virtual bool variableKeyLen() const; + + virtual bool readyToGo() const; + + virtual int encrypt(void *block, int len); + + virtual int decrypt(void *block, int len); + + private: + BlockCipher *_cipher; + void *_register; + void *_next; + int _len; + int _reader, _writer; + +}; + +#endif diff --git a/tdewallet/backend/sha1.cc b/tdewallet/backend/sha1.cc new file mode 100644 index 000000000..7420b5f16 --- /dev/null +++ b/tdewallet/backend/sha1.cc @@ -0,0 +1,343 @@ +/* This file is part of the KDE project + Copyright (C) 2001 George Staikos <staikos@kde.org> + Based heavily on SHA1 code from GPG 1.0.3 (C) 1998 FSF + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#include <config.h> + +#ifdef HAVE_SYS_TYPES_H +#include <sys/types.h> +#endif +#ifdef HAVE_STDINT_H +#include <stdint.h> /* For uintXX_t on OSX */ +#endif +#ifdef HAVE_SYS_BITYPES_H +#include <sys/bitypes.h> /* For uintXX_t on Tru64 */ +#endif +#ifdef HAVE_STDINT_H +#include <stdint.h> +#endif + +#include "sha1.h" +#include <string.h> + +// FIXME: this can be optimized to one instruction on most cpus. +#define rol(x,y) ((x << y) | (x >> (32-y))) + + +#define K1 0x5a827999L +#define K2 0x6ed9eba1L +#define K3 0x8f1bbcdcL +#define K4 0xca62c1d6L +#define F1(x,y,z) ( z ^ ( x & ( y ^ z ) ) ) +#define F2(x,y,z) ( x ^ y ^ z ) +#define F3(x,y,z) ( ( x & y ) | ( z & ( x | y ) ) ) +#define F4(x,y,z) ( x ^ y ^ z ) + +#define M(i) ( tm = x[i&0x0f] ^ x[(i-14)&0x0f] \ + ^ x[(i-8)&0x0f] ^ x[(i-3)&0x0f] \ + , (x[i&0x0f] = (tm << 1) | (tm >> 31))) + +#define R(a,b,c,d,e,f,k,m) do { e += rol(a, 5) \ + + f(b, c, d) \ + + k \ + + m; \ + b = rol(b, 30); \ + } while(0) + + +SHA1::SHA1() { + _hashlen = 160; + _init = false; + reset(); +} + + +int SHA1::reset() { + _h0 = 0x67452301; + _h1 = 0xefcdab89; + _h2 = 0x98badcfe; + _h3 = 0x10325476; + _h4 = 0xc3d2e1f0; + _nblocks = 0; + _count = 0; + memset(_buf, 0, 56); // clear the buffer + + _init = true; + return 0; +} + + +int SHA1::size() const { + return _hashlen; +} + + +SHA1::~SHA1() { + +} + + +void SHA1::transform(void *data) { + unsigned int a, b, c, d, e, tm; + unsigned int x[16]; + unsigned char *_data = (unsigned char *)data; + + a = _h0; + b = _h1; + c = _h2; + d = _h3; + e = _h4; + +#ifdef WORDS_BIGENDIAN + memcpy(x, _data, 64); +#else + int i; + unsigned char *p2; + for (i = 0, p2 = (unsigned char *)x; + i < 16; i++, p2 += 4) { + p2[3] = *_data++; + p2[2] = *_data++; + p2[1] = *_data++; + p2[0] = *_data++; + } +#endif + + R(a, b, c, d, e, F1, K1, x[ 0]); + R(e, a, b, c, d, F1, K1, x[ 1]); + R(d, e, a, b, c, F1, K1, x[ 2]); + R(c, d, e, a, b, F1, K1, x[ 3]); + R(b, c, d, e, a, F1, K1, x[ 4]); + R(a, b, c, d, e, F1, K1, x[ 5]); + R(e, a, b, c, d, F1, K1, x[ 6]); + R(d, e, a, b, c, F1, K1, x[ 7]); + R(c, d, e, a, b, F1, K1, x[ 8]); + R(b, c, d, e, a, F1, K1, x[ 9]); + R(a, b, c, d, e, F1, K1, x[10]); + R(e, a, b, c, d, F1, K1, x[11]); + R(d, e, a, b, c, F1, K1, x[12]); + R(c, d, e, a, b, F1, K1, x[13]); + R(b, c, d, e, a, F1, K1, x[14]); + R(a, b, c, d, e, F1, K1, x[15]); + R(e, a, b, c, d, F1, K1, M(16)); + R(d, e, a, b, c, F1, K1, M(17)); + R(c, d, e, a, b, F1, K1, M(18)); + R(b, c, d, e, a, F1, K1, M(19)); + R(a, b, c, d, e, F2, K2, M(20)); + R(e, a, b, c, d, F2, K2, M(21)); + R(d, e, a, b, c, F2, K2, M(22)); + R(c, d, e, a, b, F2, K2, M(23)); + R(b, c, d, e, a, F2, K2, M(24)); + R(a, b, c, d, e, F2, K2, M(25)); + R(e, a, b, c, d, F2, K2, M(26)); + R(d, e, a, b, c, F2, K2, M(27)); + R(c, d, e, a, b, F2, K2, M(28)); + R(b, c, d, e, a, F2, K2, M(29)); + R(a, b, c, d, e, F2, K2, M(30)); + R(e, a, b, c, d, F2, K2, M(31)); + R(d, e, a, b, c, F2, K2, M(32)); + R(c, d, e, a, b, F2, K2, M(33)); + R(b, c, d, e, a, F2, K2, M(34)); + R(a, b, c, d, e, F2, K2, M(35)); + R(e, a, b, c, d, F2, K2, M(36)); + R(d, e, a, b, c, F2, K2, M(37)); + R(c, d, e, a, b, F2, K2, M(38)); + R(b, c, d, e, a, F2, K2, M(39)); + R(a, b, c, d, e, F3, K3, M(40)); + R(e, a, b, c, d, F3, K3, M(41)); + R(d, e, a, b, c, F3, K3, M(42)); + R(c, d, e, a, b, F3, K3, M(43)); + R(b, c, d, e, a, F3, K3, M(44)); + R(a, b, c, d, e, F3, K3, M(45)); + R(e, a, b, c, d, F3, K3, M(46)); + R(d, e, a, b, c, F3, K3, M(47)); + R(c, d, e, a, b, F3, K3, M(48)); + R(b, c, d, e, a, F3, K3, M(49)); + R(a, b, c, d, e, F3, K3, M(50)); + R(e, a, b, c, d, F3, K3, M(51)); + R(d, e, a, b, c, F3, K3, M(52)); + R(c, d, e, a, b, F3, K3, M(53)); + R(b, c, d, e, a, F3, K3, M(54)); + R(a, b, c, d, e, F3, K3, M(55)); + R(e, a, b, c, d, F3, K3, M(56)); + R(d, e, a, b, c, F3, K3, M(57)); + R(c, d, e, a, b, F3, K3, M(58)); + R(b, c, d, e, a, F3, K3, M(59)); + R(a, b, c, d, e, F4, K4, M(60)); + R(e, a, b, c, d, F4, K4, M(61)); + R(d, e, a, b, c, F4, K4, M(62)); + R(c, d, e, a, b, F4, K4, M(63)); + R(b, c, d, e, a, F4, K4, M(64)); + R(a, b, c, d, e, F4, K4, M(65)); + R(e, a, b, c, d, F4, K4, M(66)); + R(d, e, a, b, c, F4, K4, M(67)); + R(c, d, e, a, b, F4, K4, M(68)); + R(b, c, d, e, a, F4, K4, M(69)); + R(a, b, c, d, e, F4, K4, M(70)); + R(e, a, b, c, d, F4, K4, M(71)); + R(d, e, a, b, c, F4, K4, M(72)); + R(c, d, e, a, b, F4, K4, M(73)); + R(b, c, d, e, a, F4, K4, M(74)); + R(a, b, c, d, e, F4, K4, M(75)); + R(e, a, b, c, d, F4, K4, M(76)); + R(d, e, a, b, c, F4, K4, M(77)); + R(c, d, e, a, b, F4, K4, M(78)); + R(b, c, d, e, a, F4, K4, M(79)); + + _h0 += a; + _h1 += b; + _h2 += c; + _h3 += d; + _h4 += e; + +} + + +bool SHA1::readyToGo() const { + return _init; +} + + +int SHA1::process(const void *block, int len) { + if (!_init) { + return -1; + } + + unsigned char *_block = (unsigned char *)block; + + int cnt = 0; + // Flush the buffer before proceeding + if (_count == 64) { + transform(_buf); + _count = 0; + _nblocks++; + } + + if (!_block) { + return 0; + } + + if (_count) { + for (; len && _count < 64; len--, cnt++) { + _buf[_count++] = *_block++; + } + process(0, 0); // flush the buffer if necessary + if (!len) { + return cnt; + } + } + + while (len >= 64) { + transform(_block); + _count = 0; + _nblocks++; + len -= 64; + cnt += 64; + _block += 64; + } + + for (; len && _count < 64; len--, cnt++) { + _buf[_count++] = *_block++; + } + + return cnt; +} + + +const unsigned char *SHA1::hash() { + unsigned int t, msb, lsb; + unsigned char *p; + + + if (!_init) { + return (unsigned char *)_buf; + } + + process(0, 0); + + msb = 0; + t = _nblocks; + + if ((lsb = t << 6) < t) { + msb++; + } + + msb += t >> 26; + t = lsb; + + if ((lsb = t + _count) < t) { + msb++; + } + + t = lsb; + + if ((lsb = t << 3) < t) { + msb++; + } + + msb += t >> 29; + + _buf[_count++] = 0x80; + + if (_count < 56) { + while (_count < 56) { + _buf[_count++] = 0; + } + } else { + while (_count < 64) { + _buf[_count++] = 0; + } + process(0, 0); + memset(_buf, 0, 56); + } + + _buf[56] = msb >> 24; + _buf[57] = msb >> 16; + _buf[58] = msb >> 8; + _buf[59] = msb; + _buf[60] = lsb >> 24; + _buf[61] = lsb >> 16; + _buf[62] = lsb >> 8; + _buf[63] = lsb; + + transform(_buf); + + p = _buf; + +#ifdef WORDS_BIGENDIAN +#define X( a ) do { *( uint32_t * )p = _h##a; p += 4; } while ( 0 ) +#else +#define X(a) do { *p++ = _h##a >> 24; *p++ = _h##a >> 16; \ + *p++ = _h##a >> 8; *p++ = _h##a; } while (0) +#endif + + X(0); + X(1); + X(2); + X(3); + X(4); + +#undef X + + _init = false; + + return (unsigned char *)_buf; +} + + + diff --git a/tdewallet/backend/sha1.h b/tdewallet/backend/sha1.h new file mode 100644 index 000000000..de40cc692 --- /dev/null +++ b/tdewallet/backend/sha1.h @@ -0,0 +1,71 @@ +/* This file is part of the KDE project + Copyright (C) 2001 George Staikos <staikos@kde.org> + Based heavily on SHA1 code from GPG 1.0.3 (C) 1998 FSF + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + + +#ifndef __sha1__ko__h +#define __sha1__ko__h + + +/* @internal + */ +class SHA1 { + public: + SHA1(); + virtual ~SHA1(); + + /* + * The number of bits in the hash generated. + */ + virtual int size() const; + + /* + * True if all settings are good and we are ready to hash. + */ + virtual bool readyToGo() const; + + /* + * Process a block of data for the hash function. + */ + virtual int process(const void *block, int len); + + /* + * Return the digest as a 20 byte array reference. + * Calling this makes readyToGo() == false. + */ + virtual const unsigned char *hash(); + + /* + * Reset the digest so a new one can be calculated. + */ + virtual int reset(); + + protected: + int _hashlen; + bool _init; + + long _h0, _h1, _h2, _h3, _h4; + long _nblocks; + int _count; + unsigned char _buf[64]; + void transform(void *data); +}; + + +#endif diff --git a/tdewallet/backend/tdewalletbackend.cc b/tdewallet/backend/tdewalletbackend.cc new file mode 100644 index 000000000..9871b9d7b --- /dev/null +++ b/tdewallet/backend/tdewalletbackend.cc @@ -0,0 +1,838 @@ +/* This file is part of the KDE project + * + * Copyright (C) 2001-2004 George Staikos <staikos@kde.org> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#include "tdewalletbackend.h" + +#include <stdlib.h> + +#include <kdebug.h> +#include <kglobal.h> +#include <klocale.h> +#include <kmdcodec.h> +#include <ksavefile.h> +#include <kstandarddirs.h> + +#include <tqfile.h> +#include <tqfileinfo.h> +#include <tqregexp.h> + +#include "blowfish.h" +#include "sha1.h" +#include "cbc.h" + +#include <assert.h> + +#define KWALLET_VERSION_MAJOR 0 +#define KWALLET_VERSION_MINOR 0 + +#define KWALLET_CIPHER_BLOWFISH_CBC 0 +#define KWALLET_CIPHER_3DES_CBC 1 // unsupported + +#define KWALLET_HASH_SHA1 0 +#define KWALLET_HASH_MD5 1 // unsupported + + +using namespace KWallet; + +#define KWMAGIC "KWALLET\n\r\0\r\n" +#define KWMAGIC_LEN 12 + +static void initKWalletDir() +{ + TDEGlobal::dirs()->addResourceType("tdewallet", "share/apps/tdewallet"); +} + +Backend::Backend(const TQString& name, bool isPath) : _name(name), _ref(0) { + initKWalletDir(); + if (isPath) { + _path = name; + } else { + _path = TDEGlobal::dirs()->saveLocation("tdewallet") + "/" + _name + ".kwl"; + } + + _open = false; +} + + +Backend::~Backend() { + if (_open) { + close(); + } +} + + +int Backend::close() { + for (FolderMap::ConstIterator i = _entries.begin(); i != _entries.end(); ++i) { + for (EntryMap::ConstIterator j = i.data().begin(); j != i.data().end(); ++j) { + delete j.data(); + } + } + _entries.clear(); + +return 0; +} + + +static int getRandomBlock(TQByteArray& randBlock) { + // First try /dev/urandom + if (TQFile::exists("/dev/urandom")) { + TQFile devrand("/dev/urandom"); + if (devrand.open(IO_ReadOnly)) { + unsigned int rc = devrand.readBlock(randBlock.data(), randBlock.size()); + + if (rc != randBlock.size()) { + return -3; // not enough data read + } + + return 0; + } + } + + // If that failed, try /dev/random + // FIXME: open in noblocking mode! + if (TQFile::exists("/dev/random")) { + TQFile devrand("/dev/random"); + if (devrand.open(IO_ReadOnly)) { + unsigned int rc = 0; + unsigned int cnt = 0; + + do { + int rc2 = devrand.readBlock(randBlock.data() + rc, randBlock.size()); + + if (rc2 < 0) { + return -3; // read error + } + + rc += rc2; + cnt++; + if (cnt > randBlock.size()) { + return -4; // reading forever?! + } + } while(rc < randBlock.size()); + + return 0; + } + } + + // EGD method + char *randFilename; + if ((randFilename = getenv("RANDFILE"))) { + if (TQFile::exists(randFilename)) { + TQFile devrand(randFilename); + if (devrand.open(IO_ReadOnly)) { + unsigned int rc = devrand.readBlock(randBlock.data(), randBlock.size()); + if (rc != randBlock.size()) { + return -3; // not enough data read + } + return 0; + } + } + } + + // Couldn't get any random data!! + + return -1; +} + + +// this should be SHA-512 for release probably +static int password2hash(const TQByteArray& password, TQByteArray& hash) { + SHA1 sha; + int shasz = sha.size() / 8; + + assert(shasz >= 20); + + TQByteArray block1(shasz); + + sha.process(password.data(), QMIN(password.size(), 16)); + + // To make brute force take longer + for (int i = 0; i < 2000; i++) { + memcpy(block1.data(), sha.hash(), shasz); + sha.reset(); + sha.process(block1.data(), shasz); + } + + sha.reset(); + + if (password.size() > 16) { + sha.process(password.data() + 16, QMIN(password.size() - 16, 16)); + TQByteArray block2(shasz); + // To make brute force take longer + for (int i = 0; i < 2000; i++) { + memcpy(block2.data(), sha.hash(), shasz); + sha.reset(); + sha.process(block2.data(), shasz); + } + + sha.reset(); + + if (password.size() > 32) { + sha.process(password.data() + 32, QMIN(password.size() - 32, 16)); + + TQByteArray block3(shasz); + // To make brute force take longer + for (int i = 0; i < 2000; i++) { + memcpy(block3.data(), sha.hash(), shasz); + sha.reset(); + sha.process(block3.data(), shasz); + } + + sha.reset(); + + if (password.size() > 48) { + sha.process(password.data() + 48, password.size() - 48); + + TQByteArray block4(shasz); + // To make brute force take longer + for (int i = 0; i < 2000; i++) { + memcpy(block4.data(), sha.hash(), shasz); + sha.reset(); + sha.process(block4.data(), shasz); + } + + sha.reset(); + // split 14/14/14/14 + hash.resize(56); + memcpy(hash.data(), block1.data(), 14); + memcpy(hash.data() + 14, block2.data(), 14); + memcpy(hash.data() + 28, block3.data(), 14); + memcpy(hash.data() + 42, block4.data(), 14); + block4.fill(0); + } else { + // split 20/20/16 + hash.resize(56); + memcpy(hash.data(), block1.data(), 20); + memcpy(hash.data() + 20, block2.data(), 20); + memcpy(hash.data() + 40, block3.data(), 16); + } + block3.fill(0); + } else { + // split 20/20 + hash.resize(40); + memcpy(hash.data(), block1.data(), 20); + memcpy(hash.data() + 20, block2.data(), 20); + } + block2.fill(0); + } else { + // entirely block1 + hash.resize(20); + memcpy(hash.data(), block1.data(), 20); + } + + block1.fill(0); + + return 0; +} + + +bool Backend::exists(const TQString& wallet) { + initKWalletDir(); + TQString path = TDEGlobal::dirs()->saveLocation("tdewallet") + "/" + wallet + ".kwl"; + // Note: 60 bytes is presently the minimum size of a wallet file. + // Anything smaller is junk. +return TQFile::exists(path) && TQFileInfo(path).size() >= 60; +} + + +TQString Backend::openRCToString(int rc) { + switch (rc) { + case -255: + return i18n("Already open."); + case -2: + return i18n("Error opening file."); + case -3: + return i18n("Not a wallet file."); + case -4: + return i18n("Unsupported file format revision."); + case -42: + return i18n("Unknown encryption scheme."); + case -43: + return i18n("Corrupt file?"); + case -8: + return i18n("Error validating wallet integrity. Possibly corrupted."); + case -5: + case -7: + case -9: + return i18n("Read error - possibly incorrect password."); + case -6: + return i18n("Decryption error."); + default: + return TQString::null; + } +} + + +int Backend::open(const TQByteArray& password) { + + if (_open) { + return -255; // already open + } + + TQByteArray passhash; + + // No wallet existed. Let's create it. + // Note: 60 bytes is presently the minimum size of a wallet file. + // Anything smaller is junk and should be deleted. + if (!TQFile::exists(_path) || TQFileInfo(_path).size() < 60) { + TQFile newfile(_path); + if (!newfile.open(IO_ReadWrite)) { + return -2; // error opening file + } + newfile.close(); + _open = true; + sync(password); + return 1; // new file opened, but OK + } + + TQFile db(_path); + + if (!db.open(IO_ReadOnly)) { + return -2; // error opening file + } + + char magicBuf[KWMAGIC_LEN]; + db.readBlock(magicBuf, KWMAGIC_LEN); + if (memcmp(magicBuf, KWMAGIC, KWMAGIC_LEN) != 0) { + return -3; // bad magic + } + + db.readBlock(magicBuf, 4); + + // First byte is major version, second byte is minor version + if (magicBuf[0] != KWALLET_VERSION_MAJOR) { + return -4; // unknown version + } + + if (magicBuf[1] != KWALLET_VERSION_MINOR) { + return -4; // unknown version + } + + if (magicBuf[2] != KWALLET_CIPHER_BLOWFISH_CBC) { + return -42; // unknown cipher + } + + if (magicBuf[3] != KWALLET_HASH_SHA1) { + return -42; // unknown hash + } + + _hashes.clear(); + // Read in the hashes + TQDataStream hds(&db); + TQ_UINT32 n; + hds >> n; + if (n > 0xffff) { // sanity check + return -43; + } + + for (size_t i = 0; i < n; ++i) { + KMD5::Digest d, d2; // judgment day + MD5Digest ba; + TQMap<MD5Digest,TQValueList<MD5Digest> >::iterator it; + TQ_UINT32 fsz; + if (hds.atEnd()) return -43; + hds.readRawBytes(reinterpret_cast<char *>(d), 16); + hds >> fsz; + ba.duplicate(reinterpret_cast<char *>(d), 16); + it = _hashes.insert(ba, TQValueList<MD5Digest>()); + for (size_t j = 0; j < fsz; ++j) { + hds.readRawBytes(reinterpret_cast<char *>(d2), 16); + ba.duplicate(reinterpret_cast<char *>(d2), 16); + (*it).append(ba); + } + } + + // Read in the rest of the file. + TQByteArray encrypted = db.readAll(); + assert(encrypted.size() < db.size()); + + BlowFish _bf; + CipherBlockChain bf(&_bf); + int blksz = bf.blockSize(); + if ((encrypted.size() % blksz) != 0) { + return -5; // invalid file structure + } + + // Decrypt the encrypted data + passhash.resize(bf.keyLen()/8); + password2hash(password, passhash); + + bf.setKey((void *)passhash.data(), passhash.size()*8); + + if (!encrypted.data()) { + passhash.fill(0); + encrypted.fill(0); + return -7; // file structure error + } + + int rc = bf.decrypt(encrypted.data(), encrypted.size()); + if (rc < 0) { + passhash.fill(0); + encrypted.fill(0); + return -6; // decrypt error + } + + passhash.fill(0); // passhash is UNUSABLE NOW + + const char *t = encrypted.data(); + + // strip the leading data + t += blksz; // one block of random data + + // strip the file size off + long fsize = 0; + + fsize |= (long(*t) << 24) & 0xff000000; + t++; + fsize |= (long(*t) << 16) & 0x00ff0000; + t++; + fsize |= (long(*t) << 8) & 0x0000ff00; + t++; + fsize |= long(*t) & 0x000000ff; + t++; + + if (fsize < 0 || fsize > long(encrypted.size()) - blksz - 4) { + //kdDebug() << "fsize: " << fsize << " encrypted.size(): " << encrypted.size() << " blksz: " << blksz << endl; + encrypted.fill(0); + return -9; // file structure error. + } + + // compute the hash ourself + SHA1 sha; + sha.process(t, fsize); + const char *testhash = (const char *)sha.hash(); + + // compare hashes + int sz = encrypted.size(); + for (int i = 0; i < 20; i++) { + if (testhash[i] != static_cast<const char>(encrypted.at(sz - 20 + i))) { + encrypted.fill(0); + sha.reset(); + return -8; // hash error. + } + } + + sha.reset(); + + // chop off the leading blksz+4 bytes + TQByteArray tmpenc; + tmpenc.duplicate(encrypted.data()+blksz+4, fsize); + encrypted.fill(0); + encrypted.duplicate(tmpenc.data(), tmpenc.size()); + tmpenc.fill(0); + + // Load the data structures up + TQDataStream eStream(encrypted, IO_ReadOnly); + + while (!eStream.atEnd()) { + TQString folder; + TQ_UINT32 n; + + eStream >> folder; + eStream >> n; + + // Force initialisation + _entries[folder].clear(); + + for (size_t i = 0; i < n; i++) { + TQString key; + KWallet::Wallet::EntryType et = KWallet::Wallet::Unknown; + Entry *e = new Entry; + eStream >> key; + TQ_INT32 x = 0; // necessary to read properly + eStream >> x; + et = static_cast<KWallet::Wallet::EntryType>(x); + + switch (et) { + case KWallet::Wallet::Password: + case KWallet::Wallet::Stream: + case KWallet::Wallet::Map: + break; + default: // Unknown entry + delete e; + continue; + } + + TQByteArray a; + eStream >> a; + e->setValue(a); + e->setType(et); + e->setKey(key); + _entries[folder][key] = e; + } + } + + _open = true; + return 0; +} + + +int Backend::sync(const TQByteArray& password) { + if (!_open) { + return -255; // not open yet + } + + KSaveFile sf(_path, 0600); + TQFile *qf = sf.file(); + + if (!qf) { + sf.abort(); + return -1; // error opening file + } + + qf->writeBlock(KWMAGIC, KWMAGIC_LEN); + + // Write the version number + TQByteArray version(4); + version[0] = KWALLET_VERSION_MAJOR; + version[1] = KWALLET_VERSION_MINOR; + version[2] = KWALLET_CIPHER_BLOWFISH_CBC; + version[3] = KWALLET_HASH_SHA1; + qf->writeBlock(version, 4); + + // Holds the hashes we write out + TQByteArray hashes; + TQDataStream hashStream(hashes, IO_WriteOnly); + KMD5 md5; + hashStream << static_cast<TQ_UINT32>(_entries.count()); + + // Holds decrypted data prior to encryption + TQByteArray decrypted; + + // FIXME: we should estimate the amount of data we will write in each + // buffer and resize them approximately in order to avoid extra + // resizes. + + // populate decrypted + TQDataStream dStream(decrypted, IO_WriteOnly); + for (FolderMap::ConstIterator i = _entries.begin(); i != _entries.end(); ++i) { + dStream << i.key(); + dStream << static_cast<TQ_UINT32>(i.data().count()); + + md5.reset(); + md5.update(i.key().utf8()); + hashStream.writeRawBytes(reinterpret_cast<const char*>(&(md5.rawDigest()[0])), 16); + hashStream << static_cast<TQ_UINT32>(i.data().count()); + + for (EntryMap::ConstIterator j = i.data().begin(); j != i.data().end(); ++j) { + dStream << j.key(); + dStream << static_cast<TQ_INT32>(j.data()->type()); + dStream << j.data()->value(); + + md5.reset(); + md5.update(j.key().utf8()); + hashStream.writeRawBytes(reinterpret_cast<const char*>(&(md5.rawDigest()[0])), 16); + } + } + + qf->writeBlock(hashes, hashes.size()); + + // calculate the hash of the file + SHA1 sha; + BlowFish _bf; + CipherBlockChain bf(&_bf); + + sha.process(decrypted.data(), decrypted.size()); + + // prepend and append the random data + TQByteArray wholeFile; + long blksz = bf.blockSize(); + long newsize = decrypted.size() + + blksz + // encrypted block + 4 + // file size + 20; // size of the SHA hash + + int delta = (blksz - (newsize % blksz)); + newsize += delta; + wholeFile.resize(newsize); + + TQByteArray randBlock; + randBlock.resize(blksz+delta); + if (getRandomBlock(randBlock) < 0) { + sha.reset(); + decrypted.fill(0); + sf.abort(); + return -3; // Fatal error: can't get random + } + + for (int i = 0; i < blksz; i++) { + wholeFile[i] = randBlock[i]; + } + + for (int i = 0; i < 4; i++) { + wholeFile[(int)(i+blksz)] = (decrypted.size() >> 8*(3-i))&0xff; + } + + for (unsigned int i = 0; i < decrypted.size(); i++) { + wholeFile[(int)(i+blksz+4)] = decrypted[i]; + } + + for (int i = 0; i < delta; i++) { + wholeFile[(int)(i+blksz+4+decrypted.size())] = randBlock[(int)(i+blksz)]; + } + + const char *hash = (const char *)sha.hash(); + for (int i = 0; i < 20; i++) { + wholeFile[(int)(newsize - 20 + i)] = hash[i]; + } + + sha.reset(); + decrypted.fill(0); + + // hash the passphrase + TQByteArray passhash; + password2hash(password, passhash); + + // encrypt the data + if (!bf.setKey(passhash.data(), passhash.size() * 8)) { + passhash.fill(0); + wholeFile.fill(0); + sf.abort(); + return -2; + } + + int rc = bf.encrypt(wholeFile.data(), wholeFile.size()); + if (rc < 0) { + passhash.fill(0); + wholeFile.fill(0); + sf.abort(); + return -2; // encrypt error + } + + passhash.fill(0); // passhash is UNUSABLE NOW + + // write the file + qf->writeBlock(wholeFile, wholeFile.size()); + if (!sf.close()) { + wholeFile.fill(0); + sf.abort(); + return -4; // write error + } + + wholeFile.fill(0); + +return 0; +} + + +int Backend::close(const TQByteArray& password) { + int rc = sync(password); + _open = false; + if (rc != 0) { + return rc; + } + return close(); +} + + +const TQString& Backend::walletName() const { + return _name; +} + + +bool Backend::isOpen() const { + return _open; +} + + +TQStringList Backend::folderList() const { + return _entries.keys(); +} + + +TQStringList Backend::entryList() const { + return _entries[_folder].keys(); +} + + +Entry *Backend::readEntry(const TQString& key) { +Entry *rc = 0L; + + if (_open && hasEntry(key)) { + rc = _entries[_folder][key]; + } + +return rc; +} + + +TQPtrList<Entry> Backend::readEntryList(const TQString& key) { + TQPtrList<Entry> rc; + + if (!_open) { + return rc; + } + + TQRegExp re(key, true, true); + + const EntryMap& map = _entries[_folder]; + for (EntryMap::ConstIterator i = map.begin(); i != map.end(); ++i) { + if (re.exactMatch(i.key())) { + rc.append(i.data()); + } + } + return rc; +} + + +bool Backend::createFolder(const TQString& f) { + if (_entries.contains(f)) { + return false; + } + + _entries.insert(f, EntryMap()); + + KMD5 folderMd5; + folderMd5.update(f.utf8()); + _hashes.insert(MD5Digest(folderMd5.rawDigest()), TQValueList<MD5Digest>()); + +return true; +} + + +int Backend::renameEntry(const TQString& oldName, const TQString& newName) { +EntryMap& emap = _entries[_folder]; +EntryMap::Iterator oi = emap.find(oldName); +EntryMap::Iterator ni = emap.find(newName); + + if (oi != emap.end() && ni == emap.end()) { + Entry *e = oi.data(); + emap.remove(oi); + emap[newName] = e; + + KMD5 folderMd5; + folderMd5.update(_folder.utf8()); + + HashMap::iterator i = _hashes.find(MD5Digest(folderMd5.rawDigest())); + if (i != _hashes.end()) { + KMD5 oldMd5, newMd5; + oldMd5.update(oldName.utf8()); + newMd5.update(newName.utf8()); + i.data().remove(MD5Digest(oldMd5.rawDigest())); + i.data().append(MD5Digest(newMd5.rawDigest())); + } + return 0; + } + +return -1; +} + + +void Backend::writeEntry(Entry *e) { + if (!_open) + return; + + if (!hasEntry(e->key())) { + _entries[_folder][e->key()] = new Entry; + } + _entries[_folder][e->key()]->copy(e); + + KMD5 folderMd5; + folderMd5.update(_folder.utf8()); + + HashMap::iterator i = _hashes.find(MD5Digest(folderMd5.rawDigest())); + if (i != _hashes.end()) { + KMD5 md5; + md5.update(e->key().utf8()); + i.data().append(MD5Digest(md5.rawDigest())); + } +} + + +bool Backend::hasEntry(const TQString& key) const { + return _entries.contains(_folder) && _entries[_folder].contains(key); +} + + +bool Backend::removeEntry(const TQString& key) { + if (!_open) { + return false; + } + + FolderMap::Iterator fi = _entries.find(_folder); + EntryMap::Iterator ei = fi.data().find(key); + + if (fi != _entries.end() && ei != fi.data().end()) { + delete ei.data(); + fi.data().remove(ei); + KMD5 folderMd5; + folderMd5.update(_folder.utf8()); + + HashMap::iterator i = _hashes.find(MD5Digest(folderMd5.rawDigest())); + if (i != _hashes.end()) { + KMD5 md5; + md5.update(key.utf8()); + i.data().remove(MD5Digest(md5.rawDigest())); + } + return true; + } + +return false; +} + + +bool Backend::removeFolder(const TQString& f) { + if (!_open) { + return false; + } + + FolderMap::Iterator fi = _entries.find(f); + + if (fi != _entries.end()) { + if (_folder == f) { + _folder = TQString::null; + } + + for (EntryMap::Iterator ei = fi.data().begin(); ei != fi.data().end(); ++ei) { + delete ei.data(); + } + + _entries.remove(fi); + + KMD5 folderMd5; + folderMd5.update(f.utf8()); + _hashes.erase(MD5Digest(folderMd5.rawDigest())); + return true; + } + +return false; +} + + +bool Backend::folderDoesNotExist(const TQString& folder) const { + KMD5 md5; + md5.update(folder.utf8()); + return !_hashes.contains(MD5Digest(md5.rawDigest())); +} + + +bool Backend::entryDoesNotExist(const TQString& folder, const TQString& entry) const { + KMD5 md5; + md5.update(folder.utf8()); + HashMap::const_iterator i = _hashes.find(MD5Digest(md5.rawDigest())); + if (i != _hashes.end()) { + md5.reset(); + md5.update(entry.utf8()); + return !i.data().contains(MD5Digest(md5.rawDigest())); + } + return true; +} + + diff --git a/tdewallet/backend/tdewalletbackend.h b/tdewallet/backend/tdewalletbackend.h new file mode 100644 index 000000000..105d421db --- /dev/null +++ b/tdewallet/backend/tdewalletbackend.h @@ -0,0 +1,161 @@ +/* This file is part of the KDE project + * + * Copyright (C) 2001-2004 George Staikos <staikos@kde.org> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + + +#ifndef _KWALLETBACKEND_H +#define _KWALLETBACKEND_H + +#include <kmdcodec.h> + +#include <tqstring.h> +#include <tqstringlist.h> +#include <tqmap.h> +#include "tdewalletentry.h" + + +namespace KWallet { + +class MD5Digest; + +/* @internal + */ +class KDE_EXPORT Backend { + public: + Backend(const TQString& name = "kdewallet", bool isPath = false); + ~Backend(); + + // Open and unlock the wallet. + int open(const TQByteArray& password); + + // Close and lock the wallet (saving changes). + int close(const TQByteArray& password); + + // Close the wallet, losing any changes. + int close(); + + // Write the wallet to disk + int sync(const TQByteArray& password); + + // Returns true if the current wallet is open. + bool isOpen() const; + + // Returns the current wallet name. + const TQString& walletName() const; + + // The list of folders. + TQStringList folderList() const; + + // Force creation of a folder. + bool createFolder(const TQString& f); + + // Change the folder. + void setFolder(const TQString& f) { _folder = f; } + + // Current folder. If empty, it's the global folder. + const TQString& folder() const { return _folder; } + + // Does it have this folder? + bool hasFolder(const TQString& f) const { return _entries.contains(f); } + + // Look up an entry. Returns null if it doesn't exist. + Entry *readEntry(const TQString& key); + + // Look up a list of entries. Supports wildcards. + // You delete the list. + TQPtrList<Entry> readEntryList(const TQString& key); + + // Store an entry. + void writeEntry(Entry *e); + + // Does this folder contain this entry? + bool hasEntry(const TQString& key) const; + + // Returns true if the entry was removed + bool removeEntry(const TQString& key); + + // Returns true if the folder was removed + bool removeFolder(const TQString& f); + + // The list of entries in this folder. + TQStringList entryList() const; + + // Rename an entry in this folder. + int renameEntry(const TQString& oldName, const TQString& newName); + + int ref() { return ++_ref; } + + int deref() { return --_ref; } + + int refCount() const { return _ref; } + + static bool exists(const TQString& wallet); + + bool folderDoesNotExist(const TQString& folder) const; + + bool entryDoesNotExist(const TQString& folder, const TQString& entry) const; + + static TQString openRCToString(int rc); + + private: + class BackendPrivate; + BackendPrivate *d; + TQString _name; + TQString _path; + bool _open; + TQString _folder; + int _ref; + // Map Folder->Entries + typedef TQMap< TQString, Entry* > EntryMap; + typedef TQMap< TQString, EntryMap > FolderMap; + FolderMap _entries; + typedef TQMap<MD5Digest, TQValueList<MD5Digest> > HashMap; + HashMap _hashes; +}; + +/** + * @internal + */ +class MD5Digest : public TQByteArray { + public: + MD5Digest() : TQByteArray(16) {} + MD5Digest(const KMD5::Digest d) : TQByteArray() { duplicate(reinterpret_cast<const char *>(d), 16); } + virtual ~MD5Digest() {} + + int operator<(const MD5Digest& r) const { + int i = 0; + char x, y; + for (; i < 16; ++i) { + x = at(i); + y = const_cast<MD5Digest&>(r).at(i); + if (x != y) { + break; + } + } + if (i < 16 && x < y) { + return 1; + } + return 0; + } +}; + +} + +#endif + diff --git a/tdewallet/backend/tdewalletentry.cc b/tdewallet/backend/tdewalletentry.cc new file mode 100644 index 000000000..a7e8a0425 --- /dev/null +++ b/tdewallet/backend/tdewalletentry.cc @@ -0,0 +1,89 @@ +/* This file is part of the KDE project + * + * Copyright (C) 2001-2003 George Staikos <staikos@kde.org> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#include "tdewalletentry.h" + + +using namespace KWallet; + + +Entry::Entry() { +} + +Entry::~Entry() { + _value.fill(0); +} + +const TQString& Entry::key() const { + return _key; +} + + +const TQByteArray& Entry::value() const { + return _value; +} + + +TQString Entry::password() const { +TQString x; + TQDataStream qds(_value, IO_ReadOnly); + qds >> x; + return x; +} + + +void Entry::setValue(const TQByteArray& val) { + // do a direct copy from one into the other without + // temporary variables + _value.fill(0); + _value.duplicate(val); +} + + +void Entry::setValue(const TQString& val) { + _value.fill(0); + TQDataStream qds(_value, IO_WriteOnly); + qds << val; +} + + +void Entry::setKey(const TQString& key) { + _key = key; +} + + +Wallet::EntryType Entry::type() const { + return _type; +} + + +void Entry::setType(Wallet::EntryType type) { + _type = type; +} + + +void Entry::copy(const Entry* x) { + _type = x->_type; + _key = x->_key; + _value.fill(0); + _value.duplicate(x->_value); +} + + diff --git a/tdewallet/backend/tdewalletentry.h b/tdewallet/backend/tdewalletentry.h new file mode 100644 index 000000000..bd39e97d1 --- /dev/null +++ b/tdewallet/backend/tdewalletentry.h @@ -0,0 +1,62 @@ +/* This file is part of the KDE project + * + * Copyright (C) 2001-2003 George Staikos <staikos@kde.org> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + + +#ifndef _KWALLETENTRY_H +#define _KWALLETENTRY_H + +#include <tqstring.h> +#include <tqdatastream.h> + +#include "tdewallet.h" + +namespace KWallet { + +/* @internal + */ +class KDE_EXPORT Entry { + public: + Entry(); + ~Entry(); + + const TQString& key() const; + const TQByteArray& value() const; + TQString password() const; + const TQByteArray& map() const { return value(); } + + void setValue(const TQByteArray& val); + void setValue(const TQString& val); + void setKey(const TQString& key); + + Wallet::EntryType type() const; + void setType(Wallet::EntryType type); + + void copy(const Entry* x); + + private: + TQString _key; + TQByteArray _value; + Wallet::EntryType _type; +}; + +} + +#endif + diff --git a/tdewallet/backend/tests/Makefile.am b/tdewallet/backend/tests/Makefile.am new file mode 100644 index 000000000..5f5da74d9 --- /dev/null +++ b/tdewallet/backend/tests/Makefile.am @@ -0,0 +1,14 @@ +INCLUDES = -I$(top_srcdir)/tdewallet/backend -I$(top_srcdir)/tdewallet/client $(all_includes) + +AM_LDFLAGS = $(QT_LDFLAGS) $(X_LDFLAGS) $(KDE_RPATH) + +check_PROGRAMS = backendtest testbf testsha + +METASOURCES = AUTO + +LDADD = ../libtdewalletbackend.la ../../client/libtdewalletclient.la +backendtest_SOURCES = backendtest.cpp + +testbf_SOURCES = testbf.cpp + +testsha_SOURCES = testsha.cpp diff --git a/tdewallet/backend/tests/backendtest.cpp b/tdewallet/backend/tests/backendtest.cpp new file mode 100644 index 000000000..4e19ce43f --- /dev/null +++ b/tdewallet/backend/tests/backendtest.cpp @@ -0,0 +1,45 @@ +#include <stdlib.h> +#include <stdio.h> + +#include <kapplication.h> +#include <tqstring.h> + +#include "tdewalletbackend.h" + +int main(int argc, char **argv) { + TDEApplication a(argc, argv, "tdewalletbackendtest"); + + KWallet::Backend be("ktestwallet"); + printf("KWalletBackend constructed\n"); + + TQByteArray apass, bpass, cpass; + + apass.duplicate("apassword", 9); + bpass.duplicate("bpassword", 9); + cpass.duplicate("cpassword", 9); + + printf("Passwords initialised.\n"); + int rc = be.close(apass); + + printf("be.close(apass) returned %d (should be -255)\n", rc); + + rc = be.open(bpass); + + printf("be.open(bpass) returned %d (should be 0 or 1)\n", rc); + + rc = be.close(bpass); + + printf("be.close(bpass) returned %d (should be 0)\n", rc); + + rc = be.open(apass); + + printf("be.open(apass) returned %d (should be negative)\n", rc); + + rc = be.open(bpass); + + printf("be.open(bpass) returned %d (should be 0)\n", rc); + + return 0; +} + + diff --git a/tdewallet/backend/tests/testbf.cpp b/tdewallet/backend/tests/testbf.cpp new file mode 100644 index 000000000..12dc74630 --- /dev/null +++ b/tdewallet/backend/tests/testbf.cpp @@ -0,0 +1,67 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include "blowfish.h" +#include "cbc.h" + + +int main() { +BlockCipher *bf; +char data[] = "This is a test."; +char expect[] = "\x22\x30\x7e\x2f\x42\x28\x44\x01\xda\xdf\x5a\x81\xd7\xe5\x7c\xd0"; +char key[] = "testkey"; +unsigned long et[] = {0x11223344}; + + printf("%d: 0x11 == %d and 0x44 == %d\n", ((unsigned char *)et)[0], + 0x11, 0x44); + bf = new BlowFish(); +// bf = new CipherBlockChain(new BlowFish()); + + bf->setKey((void *)key, 7*8); + + if (!bf->readyToGo()) { + printf("Error: not ready to go!\n"); + return -1; + } + + printf("About to encrypt...\n"); fflush(stdout); + if (-1 == bf->encrypt((void *)data, 8)) { + printf("Error: encrypt failed!\n"); + return -1; + } + printf("About to encrypt part 2...\n"); fflush(stdout); + bf->encrypt((void *)(data+8), 8); + + printf("Encryption done. data[] is now: "); + for (int i = 0; i < 16; i++) { + printf("0x%x ", data[i]&0xff); + if ((data[i]&0xff) != (expect[i]&0xff)) { + printf("Error. This byte failed the comparison. It should have been 0x%x.\n", expect[i]&0xff); + return -1; + } + } + printf("\n"); + + delete bf; + bf = new BlowFish(); +// bf = new CipherBlockChain(new BlowFish()); + bf->setKey((void *)key, 7*8); + + printf("About to decrypt...\n"); fflush(stdout); + if (-1 == bf->decrypt((void *)data, 16)) { + printf("Error: decrypt failed!\n"); + return -1; + } + //bf->decrypt((void *)(data+8), 8); + + printf("All done! Result... data[] = \"%s\"\n", data); + if (strcmp(data, "This is a test.")) { + printf("ERROR. Decryption failed.\n"); + return -1; + } + + delete bf; +} + + + diff --git a/tdewallet/backend/tests/testsha.cpp b/tdewallet/backend/tests/testsha.cpp new file mode 100644 index 000000000..70879f015 --- /dev/null +++ b/tdewallet/backend/tests/testsha.cpp @@ -0,0 +1,43 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include "sha1.h" + + +int main() { +SHA1 *sha1; +unsigned char data[] = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"; +unsigned long et[] = {0x11223344}; +int rc; + + printf("%d: 0x11 == %d and 0x44 == %d\n", ((unsigned char *)et)[0], + 0x11, 0x44); + sha1 = new SHA1(); + + if (!sha1->readyToGo()) { + printf("Error: not ready to go!\n"); + return -1; + } + + printf("About to process [%s]\n", data); + rc = sha1->process(data, strlen((char *)data)); + + if (rc != strlen((char *)data)) { + printf("Error processing the data. rc=%d\n", rc); + } else printf("Done.\n"); + +const unsigned char *res = sha1->getHash(); + + if (res) { + for (int i = 0; i < 20; i++) { + printf("%.2X", *res++); + if (i>0 && (i-1)%2 == 0) printf(" "); + } + printf("\n"); + } else printf("Error - getHash() returned NULL!\n"); + + delete sha1; +} + + + |