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