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 #include <nettle/des.h>
33
34 #if defined (__FreeBSD__)
35 #include <rpc/des_crypt.h>
36 #endif
37
38 #include <glib.h>
39
40 #include "passcrypt.h"
41
42 static void crypt_cfb_buf(const char key[8], unsigned char *buf, unsigned len,
43 unsigned chunksize, int decrypt);
44
passcrypt_encrypt(gchar * password,guint len)45 void passcrypt_encrypt(gchar *password, guint len)
46 {
47 crypt_cfb_buf(PASSCRYPT_KEY, password, len, 1, 0 );
48 }
49
passcrypt_decrypt(gchar * password,guint len)50 void passcrypt_decrypt(gchar *password, guint len)
51 {
52 crypt_cfb_buf(PASSCRYPT_KEY, password, len, 1, 1 );
53 }
54
55 /*
56 * crypt_cfb_iv is the intermediate vector used for cypher feedback encryption
57 */
58 unsigned char crypt_cfb_iv[64];
59 int crypt_cfb_blocksize = 8; /* 8 for DES */
60
61 #if defined (__FreeBSD__)
62 static void
crypt_cfb_buf(const char key[8],unsigned char * buf,unsigned len,unsigned chunksize,int decrypt)63 crypt_cfb_buf(const char key[8], unsigned char *buf, unsigned len,
64 unsigned chunksize, int decrypt)
65 {
66 char des_key[8];
67
68 strncpy(des_key, PASSCRYPT_KEY, 8);
69 des_setparity(des_key);
70 if (decrypt)
71 ecb_crypt(des_key, buf, len, DES_DECRYPT);
72 else
73 ecb_crypt(des_key, buf, len, DES_ENCRYPT);
74 }
75 #else
76 static void crypt_cfb_shift(unsigned char *to,
77 const unsigned char *from, unsigned len);
78 static void crypt_cfb_xor(unsigned char *to, const unsigned char *from,
79 unsigned len);
80 static void crypt_unpack(unsigned char *a);
81
82 static void
crypt_cfb_buf(const char key[8],unsigned char * buf,unsigned len,unsigned chunksize,int decrypt)83 crypt_cfb_buf(const char key[8], unsigned char *buf, unsigned len,
84 unsigned chunksize, int decrypt)
85 {
86 struct des_ctx ctx;
87 unsigned char temp[64];
88
89 des_set_key(&ctx,(const uint8_t*) key);
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 /* simulate encrypt() via Nettle */
100 char temp2[8];
101 memset(temp2,0,sizeof(temp2));
102 crypt_cfb_xor(temp2,temp,sizeof(temp)/sizeof(temp2));
103 des_encrypt(&ctx,sizeof(temp2),(uint8_t*)temp2,(uint8_t*)temp2);
104 memcpy(temp,temp2,sizeof(temp2));
105 crypt_unpack(temp);
106 /* */
107 if (chunksize > len)
108 chunksize = len;
109 if (decrypt)
110 crypt_cfb_shift(crypt_cfb_iv, buf, chunksize);
111 crypt_cfb_xor((unsigned char *) buf, temp, chunksize);
112 if (!decrypt)
113 crypt_cfb_shift(crypt_cfb_iv, buf, chunksize);
114 len -= chunksize;
115 buf += chunksize;
116 }
117 }
118
119 /*
120 * Shift len bytes from end of to buffer to beginning, then put len
121 * bytes from from at the end. Caution: the to buffer is unpacked,
122 * but the from buffer is not.
123 */
124 static void
crypt_cfb_shift(unsigned char * to,const unsigned char * from,unsigned len)125 crypt_cfb_shift(unsigned char *to, const unsigned char *from, unsigned len)
126 {
127 unsigned i;
128 unsigned j;
129 unsigned k;
130
131 if (len < crypt_cfb_blocksize) {
132 i = len * 8;
133 j = crypt_cfb_blocksize * 8;
134 for (k = i; k < j; k++) {
135 to[0] = to[i];
136 ++to;
137 }
138 }
139
140 for (i = 0; i < len; i++) {
141 j = *from++;
142 for (k = 0x80; k; k >>= 1)
143 *to++ = ((j & k) != 0);
144 }
145 }
146
147 /*
148 * XOR len bytes from from into the data at to. Caution: the from buffer
149 * is unpacked, but the to buffer is not.
150 */
151 static void
crypt_cfb_xor(unsigned char * to,const unsigned char * from,unsigned len)152 crypt_cfb_xor(unsigned char *to, const unsigned char *from, unsigned len)
153 {
154 unsigned i;
155 unsigned j;
156 unsigned char c;
157
158 for (i = 0; i < len; i++) {
159 c = 0;
160 for (j = 0; j < 8; j++)
161 c = (c << 1) | *from++;
162 *to++ ^= c;
163 }
164 }
165
166 /*
167 * Take the 8-byte array at *a (must be able to hold 64 bytes!) and unpack
168 * each bit into its own byte.
169 */
crypt_unpack(unsigned char * a)170 static void crypt_unpack(unsigned char *a)
171 {
172 int i, j;
173
174 for (i = 7; i >= 0; --i)
175 for (j = 7; j >= 0; --j)
176 a[(i << 3) + j] = (a[i] & (0x80 >> j)) != 0;
177 }
178 #endif
179