2005-09-21 [paul] 1.9.14cvs47
[claws.git] / src / common / passcrypt.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2003 Hiroyuki Yamamoto and the Sylpheed-Claws 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 #include <glib.h>
30
31 #include "passcrypt.h"
32
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,
38                           unsigned len);
39 static void crypt_unpack(unsigned char *a);
40
41 void passcrypt_encrypt(gchar *password, guint len)
42 {
43         crypt_cfb_buf(PASSCRYPT_KEY, password, len, 1, 0 );
44 }
45
46 void passcrypt_decrypt(gchar *password, guint len)
47 {
48         crypt_cfb_buf(PASSCRYPT_KEY, password, len, 1, 1 );
49 }
50
51 /*
52 * crypt_cfb_iv is the intermediate vector used for cypher feedback encryption
53 */
54 unsigned char crypt_cfb_iv[64];
55 int crypt_cfb_blocksize = 8;    /* 8 for DES */
56
57 void
58 crypt_cfb_buf(const char key[8], unsigned char *buf, unsigned len,
59               unsigned chunksize, int decrypt)
60 {
61         unsigned char temp[64];
62
63         memcpy(temp, key, 8);
64         crypt_unpack(temp);
65         setkey((const char *) temp);
66         memset(temp, 0, sizeof(temp));
67
68         memset(crypt_cfb_iv, 0, sizeof(crypt_cfb_iv));
69
70         if (chunksize > crypt_cfb_blocksize)
71                 chunksize = crypt_cfb_blocksize;
72
73         while (len) {
74                 memcpy(temp, crypt_cfb_iv, sizeof(temp));
75                 encrypt((char *) temp, 0);
76                 if (chunksize > len)
77                         chunksize = len;
78                 if (decrypt)
79                         crypt_cfb_shift(crypt_cfb_iv, buf, chunksize);
80                 crypt_cfb_xor((unsigned char *) buf, temp, chunksize);
81                 if (!decrypt)
82                         crypt_cfb_shift(crypt_cfb_iv, buf, chunksize);
83                 len -= chunksize;
84                 buf += chunksize;
85         }
86 }
87
88 /*
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.
92 */
93 static void
94 crypt_cfb_shift(unsigned char *to, const unsigned char *from, unsigned len)
95 {
96         unsigned i;
97         unsigned j;
98         unsigned k;
99
100         if (len < crypt_cfb_blocksize) {
101                 i = len * 8;
102                 j = crypt_cfb_blocksize * 8;
103                 for (k = i; k < j; k++) {
104                         to[0] = to[i];
105                         ++to;
106                 }
107         }
108
109         for (i = 0; i < len; i++) {
110                 j = *from++;
111                 for (k = 0x80; k; k >>= 1)
112                         *to++ = ((j & k) != 0);
113         }
114 }
115
116 /*
117 * XOR len bytes from from into the data at to.  Caution: the from buffer
118 * is unpacked, but the to buffer is not.
119 */
120 static void
121 crypt_cfb_xor(unsigned char *to, const unsigned char *from, unsigned len)
122 {
123         unsigned i;
124         unsigned j;
125         unsigned char c;
126
127         for (i = 0; i < len; i++) {
128                 c = 0;
129                 for (j = 0; j < 8; j++)
130                         c = (c << 1) | *from++;
131                 *to++ ^= c;
132         }
133 }
134
135 /*
136 * Take the 8-byte array at *a (must be able to hold 64 bytes!) and unpack
137 * each bit into its own byte.
138 */
139 static void crypt_unpack(unsigned char *a)
140 {
141         int i, j;
142
143         for (i = 7; i >= 0; --i)
144                 for (j = 7; j >= 0; --j)
145                         a[(i << 3) + j] = (a[i] & (0x80 >> j)) != 0;
146 }