2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2011 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 3 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, see <http://www.gnu.org/licenses/>.
20 #if !defined (__FreeBSD__)
21 #define _XOPEN_SOURCE 600
26 #include <sys/types.h>
33 #if defined (__FreeBSD__)
34 #include <rpc/des_crypt.h>
39 #include "passcrypt.h"
41 static void crypt_cfb_buf(const char key[8], unsigned char *buf, unsigned len,
42 unsigned chunksize, int decrypt);
44 void passcrypt_encrypt(gchar *password, guint len)
46 crypt_cfb_buf(PASSCRYPT_KEY, password, len, 1, 0 );
49 void passcrypt_decrypt(gchar *password, guint len)
51 crypt_cfb_buf(PASSCRYPT_KEY, password, len, 1, 1 );
55 * crypt_cfb_iv is the intermediate vector used for cypher feedback encryption
57 unsigned char crypt_cfb_iv[64];
58 int crypt_cfb_blocksize = 8; /* 8 for DES */
60 #if defined (__FreeBSD__)
62 crypt_cfb_buf(const char key[8], unsigned char *buf, unsigned len,
63 unsigned chunksize, int decrypt)
67 strncpy(des_key, PASSCRYPT_KEY, 8);
68 des_setparity(des_key);
70 ecb_crypt(des_key, buf, len, DES_DECRYPT);
72 ecb_crypt(des_key, buf, len, DES_ENCRYPT);
75 static void crypt_cfb_shift(unsigned char *to,
76 const unsigned char *from, unsigned len);
77 static void crypt_cfb_xor(unsigned char *to, const unsigned char *from,
79 static void crypt_unpack(unsigned char *a);
82 crypt_cfb_buf(const char key[8], unsigned char *buf, unsigned len,
83 unsigned chunksize, int decrypt)
85 unsigned char temp[64];
89 setkey((const char *) temp);
90 memset(temp, 0, sizeof(temp));
92 memset(crypt_cfb_iv, 0, sizeof(crypt_cfb_iv));
94 if (chunksize > crypt_cfb_blocksize)
95 chunksize = crypt_cfb_blocksize;
98 memcpy(temp, crypt_cfb_iv, sizeof(temp));
99 encrypt((char *) temp, 0);
103 crypt_cfb_shift(crypt_cfb_iv, buf, chunksize);
104 crypt_cfb_xor((unsigned char *) buf, temp, chunksize);
106 crypt_cfb_shift(crypt_cfb_iv, buf, chunksize);
113 * Shift len bytes from end of to buffer to beginning, then put len
114 * bytes from from at the end. Caution: the to buffer is unpacked,
115 * but the from buffer is not.
118 crypt_cfb_shift(unsigned char *to, const unsigned char *from, unsigned len)
124 if (len < crypt_cfb_blocksize) {
126 j = crypt_cfb_blocksize * 8;
127 for (k = i; k < j; k++) {
133 for (i = 0; i < len; i++) {
135 for (k = 0x80; k; k >>= 1)
136 *to++ = ((j & k) != 0);
141 * XOR len bytes from from into the data at to. Caution: the from buffer
142 * is unpacked, but the to buffer is not.
145 crypt_cfb_xor(unsigned char *to, const unsigned char *from, unsigned len)
151 for (i = 0; i < len; i++) {
153 for (j = 0; j < 8; j++)
154 c = (c << 1) | *from++;
160 * Take the 8-byte array at *a (must be able to hold 64 bytes!) and unpack
161 * each bit into its own byte.
163 static void crypt_unpack(unsigned char *a)
167 for (i = 7; i >= 0; --i)
168 for (j = 7; j >= 0; --j)
169 a[(i << 3) + j] = (a[i] & (0x80 >> j)) != 0;