2 encr.c -- everything that deals with encryption
3 Copyright (C) 1998,1999,2000 Ivo Timmermans <zarq@iname.com>
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.
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, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include <sys/types.h>
29 #include <sys/socket.h>
35 # ifdef HAVE_GMP2_GMP_H
36 # include <gmp2/gmp.h>
52 #define ENCR_GENERATOR "0xd"
53 #define ENCR_PRIME "0x7fffffffffffffffffffffffffffffff" /* Mersenne :) */
56 char *my_public_key_base36;
57 int key_inited = 0, encryption_keylen;
58 mpz_t my_private_key, my_public_key, generator, shared_prime;
59 int my_key_expiry = (time_t)(-1);
61 static char* mypassphrase;
62 static int mypassphraselen;
64 int char_hex_to_bin(int c)
69 return tolower(c) - 'a' + 10;
72 int str_hex_to_bin(unsigned char *bin, unsigned char *hex)
74 int i = 0, j = 0, l = strlen(hex);
79 bin[0] = char_hex_to_bin(hex[0]);
81 for(; i < l; i+=2, j++)
82 bin[j] = (char_hex_to_bin(hex[i]) << 4) + char_hex_to_bin(hex[i+1]);
87 int read_passphrase(char *which, char **out)
93 extern char *confbase;
96 if((cfg = get_config_val(passphrasesdir)) == NULL)
98 filename = xmalloc(strlen(confbase)+13+strlen(which));
99 sprintf(filename, "%spassphrases/%s", confbase, which);
103 filename = xmalloc(strlen(cfg->data.ptr)+2+strlen(which));
104 sprintf(filename, "%s/%s", (char*)cfg->data.ptr, which);
107 if((f = fopen(filename, "rb")) == NULL)
109 syslog(LOG_ERR, _("Could not open %s: %m"), filename);
113 fscanf(f, "%d ", &size);
114 if(size < 1 || size > (1<<15))
116 syslog(LOG_ERR, _("Illegal passphrase in %s; size would be %d"), filename, size);
119 size >>= 2; /* bits->nibbles */
120 pp = xmalloc(size+2);
121 fgets(pp, size+1, f);
124 *out = xmalloc(size);
126 return str_hex_to_bin(*out, pp);
129 int read_my_passphrase(void)
132 if((mypassphraselen = read_passphrase("local", &mypassphrase)) < 0)
138 int generate_private_key(void)
145 if((cfg = get_config_val(keyexpire)) == NULL)
146 my_key_expiry = (time_t)(time(NULL) + 3600);
148 my_key_expiry = (time_t)(time(NULL) + cfg->data.val);
150 syslog(LOG_NOTICE, _("Generating %d bits keys."), PRIVATE_KEY_BITS);
152 if((f = fopen("/dev/urandom", "r")) == NULL)
154 syslog(LOG_ERR, _("Opening /dev/urandom failed: %m"));
158 s = xmalloc((2 * PRIVATE_KEY_LENGTH) + 1);
160 for(i = 0; i < PRIVATE_KEY_LENGTH; i++)
161 sprintf(&s[i << 1], "%02x", fgetc(f));
163 s[2 * PRIVATE_KEY_LENGTH] = '\0';
165 mpz_set_str(my_private_key, s, 16);
170 void calculate_public_key(void)
173 mpz_powm(my_public_key, generator, my_private_key, shared_prime);
174 my_public_key_base36 = mpz_get_str(NULL, 36, my_public_key);
178 unsigned char static_key[] = { 0x9c, 0xbf, 0x36, 0xa9, 0xce, 0x20, 0x1b, 0x8b, 0x67, 0x56, 0x21, 0x5d, 0x27, 0x1b, 0xd8, 0x7a };
180 int security_init(void)
183 mpz_init(my_private_key);
184 mpz_init(my_public_key);
185 mpz_init_set_str(shared_prime, ENCR_PRIME, 0);
186 mpz_init_set_str(generator, ENCR_GENERATOR, 0);
188 if(read_my_passphrase() < 0)
190 if(generate_private_key() < 0)
193 if(cipher_init(CIPHER_BLOWFISH) < 0)
196 calculate_public_key();
201 void set_shared_key(char *almost_key)
205 mpz_t ak, our_shared_key;
207 mpz_init_set_str(ak, almost_key, 36);
208 mpz_init(our_shared_key);
209 mpz_powm(our_shared_key, ak, my_private_key, shared_prime);
211 tmp = mpz_get_str(NULL, 16, our_shared_key);
212 len = str_hex_to_bin(text_key, tmp);
214 cipher_set_key(&encryption_key, len, text_key);
216 encryption_keylen = len;
219 syslog(LOG_INFO, _("Encryption key set to %s"), tmp);
223 mpz_clear(our_shared_key);
228 void encrypt_passphrase(passphrase_t *pp)
232 unsigned char phrase[1000];
238 mpz_get_str(tmp, 16, my_public_key);
239 keylen = str_hex_to_bin(key, tmp);
241 cipher_set_key(&bf_key, keylen, key);
243 low_crypt_key(mypassphrase, phrase, &bf_key, mypassphraselen, BF_ENCRYPT);
244 pp->len = ((mypassphraselen - 1) | 7) + 1;
245 pp->phrase = xmalloc((pp->len << 1) + 1);
247 for(i = 0; i < pp->len; i++)
248 snprintf(&(pp->phrase)[i << 1], 3, "%02x", (int)phrase[i]);
250 pp->phrase[(pp->len << 1) + 1] = '\0';
253 cipher_set_key(&encryption_key, encryption_keylen, text_key);
257 int verify_passphrase(conn_list_t *cl, unsigned char *his_pubkey)
261 unsigned char phrase[1000];
266 char which[sizeof("123.123.123.123")+1];
269 mpz_init_set_str(pk, his_pubkey, 36);
270 tmp = mpz_get_str(NULL, 16, pk);
271 keylen = str_hex_to_bin(key, tmp);
272 out = xmalloc((cl->pp->len >> 1) + 3);
273 pplen = str_hex_to_bin(phrase, cl->pp->phrase);
275 cipher_set_key(&bf_key, keylen, key);
276 low_crypt_key(phrase, out, &bf_key, pplen, BF_DECRYPT);
278 cipher_set_key(&encryption_key, encryption_keylen, text_key);
280 sprintf(which, IP_ADDR_S, IP_ADDR_V(cl->vpn_ip));
281 if((pplen = read_passphrase(which, &meuk)) < 0)
284 if(memcmp(meuk, out, pplen))
290 char *make_shared_key(char *pk)
295 mpz_init_set_str(tmp, pk, 36);
297 mpz_powm(res, tmp, my_private_key, shared_prime);
299 r = mpz_get_str(NULL, 36, res);
308 free a key after overwriting it
310 void free_key(enc_key_t *k)
317 memset(k->key, (char)(-1), k->length);
324 void recalculate_encryption_keys(void)
329 for(p = conn_list; p != NULL; p = p->next)
331 if(!p->public_key || !p->public_key->key)
332 /* We haven't received a key from this host (yet). */
334 ek = make_shared_key(p->public_key->key);
336 p->key = xmalloc(sizeof(*p->key));
337 p->key->length = strlen(ek);
338 p->key->expiry = p->public_key->expiry;
339 p->key->key = xmalloc(strlen(ek) + 1);
340 strcpy(p->key->key, ek);
345 void regenerate_keys(void)
348 generate_private_key();
349 calculate_public_key();
350 send_key_changed_all();
351 recalculate_encryption_keys();