add support for Ayatana indicator to Notification plugin
[claws.git] / src / common / passcrypt.c
blob5236fa5a94c3cb5aaab127edeef28bd1af25386a
1 /*
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/>.
20 #include "config.h"
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 #include <nettle/des.h>
30 #if defined (__FreeBSD__)
31 #include <rpc/des_crypt.h>
32 #endif
34 #include <glib.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__)
58 static void
59 crypt_cfb_buf(const char key[8], unsigned char *buf, unsigned len,
60 unsigned chunksize, int decrypt)
62 char des_key[8];
64 memcpy(des_key, PASSCRYPT_KEY, 8);
65 des_setparity(des_key);
66 if (decrypt)
67 ecb_crypt(des_key, buf, len, DES_DECRYPT);
68 else
69 ecb_crypt(des_key, buf, len, DES_ENCRYPT);
71 #else
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,
75 unsigned len);
76 static void crypt_unpack(unsigned char *a);
78 static void
79 crypt_cfb_buf(const char key[8], unsigned char *buf, unsigned len,
80 unsigned chunksize, int decrypt)
82 struct des_ctx ctx;
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;
93 while (len) {
94 memcpy(temp, crypt_cfb_iv, sizeof(temp));
95 /* simulate encrypt() via Nettle */
96 char temp2[8];
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));
101 crypt_unpack(temp);
102 /* */
103 if (chunksize > len)
104 chunksize = len;
105 if (decrypt)
106 crypt_cfb_shift(crypt_cfb_iv, buf, chunksize);
107 crypt_cfb_xor((unsigned char *) buf, temp, chunksize);
108 if (!decrypt)
109 crypt_cfb_shift(crypt_cfb_iv, buf, chunksize);
110 len -= chunksize;
111 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.
120 static void
121 crypt_cfb_shift(unsigned char *to, const unsigned char *from, unsigned len)
123 unsigned i;
124 unsigned j;
125 unsigned k;
127 if (len < crypt_cfb_blocksize) {
128 i = len * 8;
129 j = crypt_cfb_blocksize * 8;
130 for (k = i; k < j; k++) {
131 to[0] = to[i];
132 ++to;
136 for (i = 0; i < len; i++) {
137 j = *from++;
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.
147 static void
148 crypt_cfb_xor(unsigned char *to, const unsigned char *from, unsigned len)
150 unsigned i;
151 unsigned j;
152 unsigned char c;
154 for (i = 0; i < len; i++) {
155 c = 0;
156 for (j = 0; j < 8; j++)
157 c = (c << 1) | *from++;
158 *to++ ^= c;
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)
168 int i, j;
170 for (i = 7; i >= 0; --i)
171 for (j = 7; j >= 0; --j)
172 a[(i << 3) + j] = (a[i] & (0x80 >> j)) != 0;
174 #endif