7481e1a6f73d12de1de3b903962fd065edf9f4b8
[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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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], void *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], void *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,
80                                         (unsigned char *) buf, chunksize);
81                 crypt_cfb_xor((unsigned char *) buf, temp, chunksize);
82                 if (!decrypt)
83                         crypt_cfb_shift(crypt_cfb_iv,
84                                         (unsigned char *) buf, chunksize);
85                 len -= chunksize;
86                 buf += chunksize;
87         }
88 }
89
90 /*
91 * Shift len bytes from end of to buffer to beginning, then put len
92 * bytes from from at the end.  Caution: the to buffer is unpacked,
93 * but the from buffer is not.
94 */
95 static void
96 crypt_cfb_shift(unsigned char *to, const unsigned char *from, unsigned len)
97 {
98         unsigned i;
99         unsigned j;
100         unsigned k;
101
102         if (len < crypt_cfb_blocksize) {
103                 i = len * 8;
104                 j = crypt_cfb_blocksize * 8;
105                 for (k = i; k < j; k++) {
106                         to[0] = to[i];
107                         ++to;
108                 }
109         }
110
111         for (i = 0; i < len; i++) {
112                 j = *from++;
113                 for (k = 0x80; k; k >>= 1)
114                         *to++ = ((j & k) != 0);
115         }
116 }
117
118 /*
119 * XOR len bytes from from into the data at to.  Caution: the from buffer
120 * is unpacked, but the to buffer is not.
121 */
122 static void
123 crypt_cfb_xor(unsigned char *to, const unsigned char *from, unsigned len)
124 {
125         unsigned i;
126         unsigned j;
127         unsigned char c;
128
129         for (i = 0; i < len; i++) {
130                 c = 0;
131                 for (j = 0; j < 8; j++)
132                         c = (c << 1) | *from++;
133                 *to++ ^= c;
134         }
135 }
136
137 /*
138 * Take the 8-byte array at *a (must be able to hold 64 bytes!) and unpack
139 * each bit into its own byte.
140 */
141 static void crypt_unpack(unsigned char *a)
142 {
143         int i, j;
144
145         for (i = 7; i >= 0; --i)
146                 for (j = 7; j >= 0; --j)
147                         a[(i << 3) + j] = (a[i] & (0x80 >> j)) != 0;
148 }