2 * Claws Mail -- a GTK based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2012 Hiroyuki Yamamoto and the Claws Mail Team
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.
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.
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/>.
22 #include <sys/types.h>
28 #include <nettle/des.h>
30 #if defined (__FreeBSD__)
31 #include <rpc/des_crypt.h>
36 #include "passcrypt.h"
38 static void crypt_cfb_buf(const char key
[8], unsigned char *buf
, unsigned len
,
39 unsigned chunksize
, int decrypt
);
41 void passcrypt_encrypt(gchar
*password
, guint len
)
43 crypt_cfb_buf(PASSCRYPT_KEY
, password
, len
, 1, 0 );
46 void passcrypt_decrypt(gchar
*password
, guint len
)
48 crypt_cfb_buf(PASSCRYPT_KEY
, password
, len
, 1, 1 );
52 * crypt_cfb_iv is the intermediate vector used for cypher feedback encryption
54 unsigned char crypt_cfb_iv
[64];
55 int crypt_cfb_blocksize
= 8; /* 8 for DES */
57 #if defined (__FreeBSD__)
59 crypt_cfb_buf(const char key
[8], unsigned char *buf
, unsigned len
,
60 unsigned chunksize
, int decrypt
)
64 memcpy(des_key
, PASSCRYPT_KEY
, 8);
65 des_setparity(des_key
);
67 ecb_crypt(des_key
, buf
, len
, DES_DECRYPT
);
69 ecb_crypt(des_key
, buf
, len
, DES_ENCRYPT
);
72 static void crypt_cfb_shift(unsigned char *to
,
73 const unsigned char *from
, unsigned len
);
74 static void crypt_cfb_xor(unsigned char *to
, const unsigned char *from
,
76 static void crypt_unpack(unsigned char *a
);
79 crypt_cfb_buf(const char key
[8], unsigned char *buf
, unsigned len
,
80 unsigned chunksize
, int decrypt
)
83 unsigned char temp
[64];
85 des_set_key(&ctx
,(const uint8_t*) key
);
86 memset(temp
, 0, sizeof(temp
));
88 memset(crypt_cfb_iv
, 0, sizeof(crypt_cfb_iv
));
90 if (chunksize
> crypt_cfb_blocksize
)
91 chunksize
= crypt_cfb_blocksize
;
94 memcpy(temp
, crypt_cfb_iv
, sizeof(temp
));
95 /* simulate encrypt() via Nettle */
97 memset(temp2
,0,sizeof(temp2
));
98 crypt_cfb_xor(temp2
,temp
,sizeof(temp
)/sizeof(temp2
));
99 des_encrypt(&ctx
,sizeof(temp2
),(uint8_t*)temp2
,(uint8_t*)temp2
);
100 memcpy(temp
,temp2
,sizeof(temp2
));
106 crypt_cfb_shift(crypt_cfb_iv
, buf
, chunksize
);
107 crypt_cfb_xor((unsigned char *) buf
, temp
, chunksize
);
109 crypt_cfb_shift(crypt_cfb_iv
, buf
, chunksize
);
116 * Shift len bytes from end of to buffer to beginning, then put len
117 * bytes from from at the end. Caution: the to buffer is unpacked,
118 * but the from buffer is not.
121 crypt_cfb_shift(unsigned char *to
, const unsigned char *from
, unsigned len
)
127 if (len
< crypt_cfb_blocksize
) {
129 j
= crypt_cfb_blocksize
* 8;
130 for (k
= i
; k
< j
; k
++) {
136 for (i
= 0; i
< len
; i
++) {
138 for (k
= 0x80; k
; k
>>= 1)
139 *to
++ = ((j
& k
) != 0);
144 * XOR len bytes from from into the data at to. Caution: the from buffer
145 * is unpacked, but the to buffer is not.
148 crypt_cfb_xor(unsigned char *to
, const unsigned char *from
, unsigned len
)
154 for (i
= 0; i
< len
; i
++) {
156 for (j
= 0; j
< 8; j
++)
157 c
= (c
<< 1) | *from
++;
163 * Take the 8-byte array at *a (must be able to hold 64 bytes!) and unpack
164 * each bit into its own byte.
166 static void crypt_unpack(unsigned char *a
)
170 for (i
= 7; i
>= 0; --i
)
171 for (j
= 7; j
>= 0; --j
)
172 a
[(i
<< 3) + j
] = (a
[i
] & (0x80 >> j
)) != 0;