2007-01-03 [paul] 2.6.1cvs96
[claws.git] / src / common / passcrypt.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2007 Hiroyuki Yamamoto and the Claws Mail Team
4  *
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.
9  *
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.
14  *
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.
18  */
19
20 #define _XOPEN_SOURCE
21
22 #include <sys/types.h>
23 #include <stdio.h>
24 #include <memory.h>
25 #include <ctype.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28
29 #if defined (__FreeBSD__)
30 #include <rpc/des_crypt.h>
31 #endif
32
33 #include <glib.h>
34
35 #include "passcrypt.h"
36
37 void crypt_cfb_buf(const char key[8], unsigned char *buf, unsigned len,
38                    unsigned chunksize, int decrypt);
39
40 void passcrypt_encrypt(gchar *password, guint len)
41 {
42         crypt_cfb_buf(PASSCRYPT_KEY, password, len, 1, 0 );
43 }
44
45 void passcrypt_decrypt(gchar *password, guint len)
46 {
47         crypt_cfb_buf(PASSCRYPT_KEY, password, len, 1, 1 );
48 }
49
50 /*
51 * crypt_cfb_iv is the intermediate vector used for cypher feedback encryption
52 */
53 unsigned char crypt_cfb_iv[64];
54 int crypt_cfb_blocksize = 8;    /* 8 for DES */
55
56 #if defined (__FreeBSD__)
57 void
58 crypt_cfb_buf(const char key[8], unsigned char *buf, unsigned len,
59               unsigned chunksize, int decrypt)
60 {
61         char des_key[8];
62         
63         strncpy(des_key, PASSCRYPT_KEY, 8);
64         des_setparity(des_key);
65         if (decrypt)
66                 ecb_crypt(des_key, buf, len, DES_DECRYPT);
67         else
68                 ecb_crypt(des_key, buf, len, DES_ENCRYPT);
69 }
70 #else
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,
74                           unsigned len);
75 static void crypt_unpack(unsigned char *a);
76
77 void
78 crypt_cfb_buf(const char key[8], unsigned char *buf, unsigned len,
79               unsigned chunksize, int decrypt)
80 {
81         unsigned char temp[64];
82
83         memcpy(temp, key, 8);
84         crypt_unpack(temp);
85         setkey((const char *) temp);
86         memset(temp, 0, sizeof(temp));
87
88         memset(crypt_cfb_iv, 0, sizeof(crypt_cfb_iv));
89
90         if (chunksize > crypt_cfb_blocksize)
91                 chunksize = crypt_cfb_blocksize;
92
93         while (len) {
94                 memcpy(temp, crypt_cfb_iv, sizeof(temp));
95                 encrypt((char *) temp, 0);
96                 if (chunksize > len)
97                         chunksize = len;
98                 if (decrypt)
99                         crypt_cfb_shift(crypt_cfb_iv, buf, chunksize);
100                 crypt_cfb_xor((unsigned char *) buf, temp, chunksize);
101                 if (!decrypt)
102                         crypt_cfb_shift(crypt_cfb_iv, buf, chunksize);
103                 len -= chunksize;
104                 buf += chunksize;
105         }
106 }
107
108 /*
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.
112 */
113 static void
114 crypt_cfb_shift(unsigned char *to, const unsigned char *from, unsigned len)
115 {
116         unsigned i;
117         unsigned j;
118         unsigned k;
119
120         if (len < crypt_cfb_blocksize) {
121                 i = len * 8;
122                 j = crypt_cfb_blocksize * 8;
123                 for (k = i; k < j; k++) {
124                         to[0] = to[i];
125                         ++to;
126                 }
127         }
128
129         for (i = 0; i < len; i++) {
130                 j = *from++;
131                 for (k = 0x80; k; k >>= 1)
132                         *to++ = ((j & k) != 0);
133         }
134 }
135
136 /*
137 * XOR len bytes from from into the data at to.  Caution: the from buffer
138 * is unpacked, but the to buffer is not.
139 */
140 static void
141 crypt_cfb_xor(unsigned char *to, const unsigned char *from, unsigned len)
142 {
143         unsigned i;
144         unsigned j;
145         unsigned char c;
146
147         for (i = 0; i < len; i++) {
148                 c = 0;
149                 for (j = 0; j < 8; j++)
150                         c = (c << 1) | *from++;
151                 *to++ ^= c;
152         }
153 }
154
155 /*
156 * Take the 8-byte array at *a (must be able to hold 64 bytes!) and unpack
157 * each bit into its own byte.
158 */
159 static void crypt_unpack(unsigned char *a)
160 {
161         int i, j;
162
163         for (i = 7; i >= 0; --i)
164                 for (j = 7; j >= 0; --j)
165                         a[(i << 3) + j] = (a[i] & (0x80 >> j)) != 0;
166 }
167 #endif