summaryrefslogtreecommitdiffabout
path: root/pwmanager/pwmanager/blowfish.h
Unidiff
Diffstat (limited to 'pwmanager/pwmanager/blowfish.h') (more/less context) (ignore whitespace changes)
-rw-r--r--pwmanager/pwmanager/blowfish.h120
1 files changed, 120 insertions, 0 deletions
diff --git a/pwmanager/pwmanager/blowfish.h b/pwmanager/pwmanager/blowfish.h
new file mode 100644
index 0000000..c05de77
--- a/dev/null
+++ b/pwmanager/pwmanager/blowfish.h
@@ -0,0 +1,120 @@
1/***************************************************************************
2 * *
3 * copyright (C) 2003, 2004 by Michael Buesch *
4 * email: mbuesch@freenet.de *
5 * *
6 * blowfish.c - Blowfish encryption *
7 * Copyright (C) 1998, 2001, 2002 Free Software Foundation, Inc. *
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License version 2 *
11 * as published by the Free Software Foundation. *
12 * *
13 ***************************************************************************/
14
15/***************************************************************************
16 * copyright (C) 2004 by Ulf Schenk
17 * This file is originaly based on version 1.0.1 of pwmanager
18 * and was modified to run on embedded devices that run microkde
19 *
20 * $Id$
21 **************************************************************************/
22
23#ifndef BLOWFISH_H
24#define BLOWFISH_H
25
26#include "pwmexception.h"
27
28#include <stdint.h>
29#include <string>
30using std::string;
31
32 #define BLOWFISH_BLOCKSIZE8
33 #define BLOWFISH_ROUNDS 16
34 #define CIPHER_ALGO_BLOWFISH 4/* blowfish 128 bit key */
35
36 typedef uint8_t byte;
37
38/** blowfish encryption algorithm.
39 * Derived from libgcrypt-1.1.12
40 */
41class Blowfish
42{
43 struct BLOWFISH_context
44 {
45 uint32_t s0[256];
46 uint32_t s1[256];
47 uint32_t s2[256];
48 uint32_t s3[256];
49 uint32_t p[BLOWFISH_ROUNDS+2];
50 };
51
52public:
53 Blowfish() {}
54 static bool selfTest();
55
56 /** set key to encrypt. if return == 1, it is a weak key. */
57 int bf_setkey( byte *key, unsigned int keylen );
58 /** encrypt inbuf and return it in outbuf.
59 * inbuf and outbuf have to be: buf % 8 == 0
60 * You may check this with getPaddedLen() and pad with NULL.
61 */
62 int bf_encrypt( byte *outbuf, byte *inbuf, unsigned int inbuf_len );
63 /** decrypt inbuf and return it in outbuf.
64 * inbuf and outbuf have to be: buf % 8 == 0
65 * You may check this with getPaddedLen() and pad with NULL.
66 */
67 int bf_decrypt( byte *outbuf, byte *inbuf, unsigned int inbuf_len );
68 /** returns the length, the sting has to be padded to */
69 static unsigned int getPaddedLen(unsigned int inLen)
70 { return ((8 - (inLen % 8)) + inLen); }
71 /** pad up to 8 bytes. */
72 static void padNull(string *buf);
73 /** remove padded data */
74 static bool unpadNull(string *buf);
75
76protected:
77#if BLOWFISH_ROUNDS != 16
78 uint32_t function_F( uint32_t x)
79 {
80 uint16_t a, b, c, d;
81 #ifdef BIG_ENDIAN_HOST
82 a = ((byte *) & x)[0];
83 b = ((byte *) & x)[1];
84 c = ((byte *) & x)[2];
85 d = ((byte *) & x)[3];
86 #else
87 a = ((byte *) & x)[3];
88 b = ((byte *) & x)[2];
89 c = ((byte *) & x)[1];
90 d = ((byte *) & x)[0];
91 #endif
92 return ((bc.s0[a] + bc.s1[b]) ^ bc.s2[c]) + bc.s3[d];
93 }
94#endif
95 void R(uint32_t &l, uint32_t &r, uint32_t i, uint32_t *p,
96 uint32_t *s0, uint32_t *s1, uint32_t *s2, uint32_t *s3)
97 {
98 l ^= p[i];
99 #ifdef BIG_ENDIAN_HOST
100 r ^= (( s0[((byte*)&l)[0]] + s1[((byte*)&l)[1]])
101 ^ s2[((byte*)&l)[2]]) + s3[((byte*)&l)[3]];
102 #else
103 r ^= (( s0[((byte*)&l)[3]] + s1[((byte*)&l)[2]])
104 ^ s2[((byte*)&l)[1]]) + s3[((byte*)&l)[0]];
105 #endif
106 }
107 void encrypt_block(byte *outbuf, byte *inbuf);
108 void decrypt_block(byte *outbuf, byte *inbuf);
109 void burn_stack(int bytes);
110 void do_encrypt(uint32_t *ret_xl, uint32_t *ret_xr);
111 void do_decrypt(uint32_t *ret_xl, uint32_t *ret_xr);
112 void do_encrypt_block(byte *outbuf, byte *inbuf);
113 void do_decrypt_block(byte *outbuf, byte *inbuf);
114 int do_bf_setkey(byte *key, unsigned int keylen);
115
116protected:
117 struct BLOWFISH_context bc;
118};
119
120#endif