2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2007 Hiroyuki Yamamoto and the Claws Mail 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 #include <sys/types.h>
29 #if defined (__FreeBSD__)
30 #include <rpc/des_crypt.h>
35 #include "passcrypt.h"
37 void crypt_cfb_buf(const char key[8], unsigned char *buf, unsigned len,
38 unsigned chunksize, int decrypt);
40 void passcrypt_encrypt(gchar *password, guint len)
42 crypt_cfb_buf(PASSCRYPT_KEY, password, len, 1, 0 );
45 void passcrypt_decrypt(gchar *password, guint len)
47 crypt_cfb_buf(PASSCRYPT_KEY, password, len, 1, 1 );
51 * crypt_cfb_iv is the intermediate vector used for cypher feedback encryption
53 unsigned char crypt_cfb_iv[64];
54 int crypt_cfb_blocksize = 8; /* 8 for DES */
56 #if defined (__FreeBSD__)
58 crypt_cfb_buf(const char key[8], unsigned char *buf, unsigned len,
59 unsigned chunksize, int decrypt)
63 strncpy(des_key, PASSCRYPT_KEY, 8);
64 des_setparity(des_key);
66 ecb_crypt(des_key, buf, len, DES_DECRYPT);
68 ecb_crypt(des_key, buf, len, DES_ENCRYPT);
71 static void crypt_cfb_shift(unsigned char *to,
72 const unsigned char *from, unsigned len);
73 static void crypt_cfb_xor(unsigned char *to, const unsigned char *from,
75 static void crypt_unpack(unsigned char *a);
78 crypt_cfb_buf(const char key[8], unsigned char *buf, unsigned len,
79 unsigned chunksize, int decrypt)
81 unsigned char temp[64];
85 setkey((const char *) temp);
86 memset(temp, 0, sizeof(temp));
88 memset(crypt_cfb_iv, 0, sizeof(crypt_cfb_iv));
90 if (chunksize > crypt_cfb_blocksize)
91 chunksize = crypt_cfb_blocksize;
94 memcpy(temp, crypt_cfb_iv, sizeof(temp));
95 encrypt((char *) temp, 0);
99 crypt_cfb_shift(crypt_cfb_iv, buf, chunksize);
100 crypt_cfb_xor((unsigned char *) buf, temp, chunksize);
102 crypt_cfb_shift(crypt_cfb_iv, buf, chunksize);
109 * Shift len bytes from end of to buffer to beginning, then put len
110 * bytes from from at the end. Caution: the to buffer is unpacked,
111 * but the from buffer is not.
114 crypt_cfb_shift(unsigned char *to, const unsigned char *from, unsigned len)
120 if (len < crypt_cfb_blocksize) {
122 j = crypt_cfb_blocksize * 8;
123 for (k = i; k < j; k++) {
129 for (i = 0; i < len; i++) {
131 for (k = 0x80; k; k >>= 1)
132 *to++ = ((j & k) != 0);
137 * XOR len bytes from from into the data at to. Caution: the from buffer
138 * is unpacked, but the to buffer is not.
141 crypt_cfb_xor(unsigned char *to, const unsigned char *from, unsigned len)
147 for (i = 0; i < len; i++) {
149 for (j = 0; j < 8; j++)
150 c = (c << 1) | *from++;
156 * Take the 8-byte array at *a (must be able to hold 64 bytes!) and unpack
157 * each bit into its own byte.
159 static void crypt_unpack(unsigned char *a)
163 for (i = 7; i >= 0; --i)
164 for (j = 7; j >= 0; --j)
165 a[(i << 3) + j] = (a[i] & (0x80 >> j)) != 0;