2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2003 Hiroyuki Yamamoto and the Sylpheed-Claws Team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 #include <sys/types.h>
31 #include "passcrypt.h"
33 void crypt_cfb_buf(const char key[8], unsigned char *buf, unsigned len,
34 unsigned chunksize, int decrypt);
35 static void crypt_cfb_shift(unsigned char *to,
36 const unsigned char *from, unsigned len);
37 static void crypt_cfb_xor(unsigned char *to, const unsigned char *from,
39 static void crypt_unpack(unsigned char *a);
41 void passcrypt_encrypt(gchar *password, guint len)
43 crypt_cfb_buf(PASSCRYPT_KEY, password, len, 1, 0 );
46 void passcrypt_decrypt(gchar *password, guint len)
48 crypt_cfb_buf(PASSCRYPT_KEY, password, len, 1, 1 );
52 * crypt_cfb_iv is the intermediate vector used for cypher feedback encryption
54 unsigned char crypt_cfb_iv[64];
55 int crypt_cfb_blocksize = 8; /* 8 for DES */
58 crypt_cfb_buf(const char key[8], unsigned char *buf, unsigned len,
59 unsigned chunksize, int decrypt)
61 unsigned char temp[64];
65 setkey((const char *) temp);
66 memset(temp, 0, sizeof(temp));
68 memset(crypt_cfb_iv, 0, sizeof(crypt_cfb_iv));
70 if (chunksize > crypt_cfb_blocksize)
71 chunksize = crypt_cfb_blocksize;
74 memcpy(temp, crypt_cfb_iv, sizeof(temp));
75 encrypt((char *) temp, 0);
79 crypt_cfb_shift(crypt_cfb_iv, buf, chunksize);
80 crypt_cfb_xor((unsigned char *) buf, temp, chunksize);
82 crypt_cfb_shift(crypt_cfb_iv, buf, chunksize);
89 * Shift len bytes from end of to buffer to beginning, then put len
90 * bytes from from at the end. Caution: the to buffer is unpacked,
91 * but the from buffer is not.
94 crypt_cfb_shift(unsigned char *to, const unsigned char *from, unsigned len)
100 if (len < crypt_cfb_blocksize) {
102 j = crypt_cfb_blocksize * 8;
103 for (k = i; k < j; k++) {
109 for (i = 0; i < len; i++) {
111 for (k = 0x80; k; k >>= 1)
112 *to++ = ((j & k) != 0);
117 * XOR len bytes from from into the data at to. Caution: the from buffer
118 * is unpacked, but the to buffer is not.
121 crypt_cfb_xor(unsigned char *to, const unsigned char *from, unsigned len)
127 for (i = 0; i < len; i++) {
129 for (j = 0; j < 8; j++)
130 c = (c << 1) | *from++;
136 * Take the 8-byte array at *a (must be able to hold 64 bytes!) and unpack
137 * each bit into its own byte.
139 static void crypt_unpack(unsigned char *a)
143 for (i = 7; i >= 0; --i)
144 for (j = 7; j >= 0; --j)
145 a[(i << 3) + j] = (a[i] & (0x80 >> j)) != 0;