2 encr.c -- everything that deals with encryption
3 Copyright (C) 1998,1999,2000 Ivo Timmermans <itimmermans@bigfoot.com>
4 2000 Guus Sliepen <guus@sliepen.warande.net>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 $Id: encr.c,v 1.13 2000/10/18 20:12:08 zarq Exp $
25 #include <sys/types.h>
32 #include <sys/socket.h>
38 # ifdef HAVE_GMP2_GMP_H
39 # include <gmp2/gmp.h>
55 #define ENCR_GENERATOR "0xd"
56 #define ENCR_PRIME "0x7fffffffffffffffffffffffffffffff" /* Mersenne :) */
59 char *my_public_key_base36;
60 int key_inited = 0, encryption_keylen;
61 mpz_t my_private_key, my_public_key, generator, shared_prime;
62 int my_key_expiry = (time_t)(-1);
67 int char_hex_to_bin(int c)
72 return tolower(c) - 'a' + 10;
75 int str_hex_to_bin(unsigned char *bin, unsigned char *hex)
77 int i = 0, j = 0, l = strlen(hex);
82 bin[0] = char_hex_to_bin(hex[0]);
84 for(; i < l; i+=2, j++)
85 bin[j] = (char_hex_to_bin(hex[i]) << 4) + char_hex_to_bin(hex[i+1]);
90 int read_passphrase(char *which, char **out)
96 extern char *confbase;
99 if((cfg = get_config_val(passphrasesdir)) == NULL)
101 asprintf(&filename, "%spassphrases/%s", confbase, which);
105 asprintf(&filename, "%s/%s", (char*)cfg->data.ptr, which);
108 if((f = fopen(filename, "rb")) == NULL)
111 syslog(LOG_ERR, _("Could not open %s: %m"), filename);
115 fscanf(f, "%d ", &size);
116 if(size < 1 || size > (1<<15))
118 syslog(LOG_ERR, _("Illegal passphrase in %s; size would be %d"), filename, size);
122 /* Hmz... hackish... strange +1 and +2 stuff... I really like more comments on those alignment thingies! */
124 pp = xmalloc(size/4 + 1); /* Allocate enough for fgets */
125 fgets(pp, size/4 + 1, f); /* Read passhrase and reserve one byte for end-of-string */
128 *out = xmalloc(size/8 + 2); /* Allocate enough bytes, +1 for rounding if bits%8 != 0, +1 for 2-byte alignment */
130 return str_hex_to_bin(*out, pp);
133 int read_my_passphrase(void)
136 if((mypassphraselen = read_passphrase("local", &mypassphrase)) < 0)
142 int generate_private_key(void)
149 if((cfg = get_config_val(keyexpire)) == NULL)
150 my_key_expiry = (time_t)(time(NULL) + 3600);
152 my_key_expiry = (time_t)(time(NULL) + cfg->data.val);
155 syslog(LOG_NOTICE, _("Generating %d bits keys"), PRIVATE_KEY_BITS);
157 if((f = fopen("/dev/urandom", "r")) == NULL)
159 syslog(LOG_ERR, _("Opening /dev/urandom failed: %m"));
163 s = xmalloc((2 * PRIVATE_KEY_LENGTH) + 1);
165 for(i = 0; i < PRIVATE_KEY_LENGTH; i++)
166 sprintf(&s[i << 1], "%02x", fgetc(f));
168 s[2 * PRIVATE_KEY_LENGTH] = '\0';
170 mpz_set_str(my_private_key, s, 16);
175 void calculate_public_key(void)
178 mpz_powm(my_public_key, generator, my_private_key, shared_prime);
179 my_public_key_base36 = mpz_get_str(NULL, 36, my_public_key);
183 unsigned char static_key[] = { 0x9c, 0xbf, 0x36, 0xa9, 0xce, 0x20, 0x1b, 0x8b, 0x67, 0x56, 0x21, 0x5d, 0x27, 0x1b, 0xd8, 0x7a };
185 int security_init(void)
188 mpz_init(my_private_key);
189 mpz_init(my_public_key);
190 mpz_init_set_str(shared_prime, ENCR_PRIME, 0);
191 mpz_init_set_str(generator, ENCR_GENERATOR, 0);
193 if(read_my_passphrase() < 0)
195 if(generate_private_key() < 0)
198 if(cipher_init(CIPHER_BLOWFISH) < 0)
201 calculate_public_key();
206 void set_shared_key(char *almost_key)
210 mpz_t ak, our_shared_key;
212 mpz_init_set_str(ak, almost_key, 36);
213 mpz_init(our_shared_key);
214 mpz_powm(our_shared_key, ak, my_private_key, shared_prime);
216 tmp = mpz_get_str(NULL, 16, our_shared_key);
217 len = str_hex_to_bin(text_key, tmp);
219 cipher_set_key(&encryption_key, len, text_key);
221 encryption_keylen = len;
224 syslog(LOG_INFO, _("Encryption key set to %s"), tmp);
228 mpz_clear(our_shared_key);
233 void encrypt_passphrase(passphrase_t *pp)
237 unsigned char phrase[1000];
243 mpz_get_str(tmp, 16, my_public_key);
244 keylen = str_hex_to_bin(key, tmp);
246 cipher_set_key(&bf_key, keylen, key);
248 low_crypt_key(mypassphrase, phrase, &bf_key, mypassphraselen, BF_ENCRYPT);
249 pp->len = ((mypassphraselen - 1) | 7) + 1;
250 pp->phrase = xmalloc((pp->len << 1) + 1);
252 for(i = 0; i < pp->len; i++)
253 snprintf(&(pp->phrase)[i << 1], 3, "%02x", (int)phrase[i]);
255 pp->phrase[(pp->len << 1) + 1] = '\0';
258 cipher_set_key(&encryption_key, encryption_keylen, text_key);
262 int verify_passphrase(conn_list_t *cl, unsigned char *his_pubkey)
266 unsigned char phrase[1000];
274 mpz_init_set_str(pk, his_pubkey, 36);
275 tmp = mpz_get_str(NULL, 16, pk);
276 keylen = str_hex_to_bin(key, tmp);
277 out = xmalloc((cl->pp->len >> 1) + 3);
278 pplen = str_hex_to_bin(phrase, cl->pp->phrase);
280 cipher_set_key(&bf_key, keylen, key);
281 low_crypt_key(phrase, out, &bf_key, pplen, BF_DECRYPT);
283 cipher_set_key(&encryption_key, encryption_keylen, text_key);
285 asprintf(&which, IP_ADDR_S, IP_ADDR_V(cl->vpn_ip));
286 if((pplen = read_passphrase(which, &meuk)) < 0)
289 if(memcmp(meuk, out, pplen))
295 char *make_shared_key(char *pk)
300 mpz_init_set_str(tmp, pk, 36);
302 mpz_powm(res, tmp, my_private_key, shared_prime);
304 r = mpz_get_str(NULL, 36, res);
313 free a key after overwriting it
315 void free_key(enc_key_t *k)
322 memset(k->key, (char)(-1), k->length);
329 void recalculate_encryption_keys(void)
334 for(p = conn_list; p != NULL; p = p->next)
336 if(!p->public_key || !p->public_key->key)
337 /* We haven't received a key from this host (yet). */
339 ek = make_shared_key(p->public_key->key);
340 free_key(p->datakey);
341 p->datakey = xmalloc(sizeof(*p->datakey));
342 p->datakey->length = strlen(ek);
343 p->datakey->expiry = p->public_key->expiry;
344 p->datakey->key = xmalloc(strlen(ek) + 1);
345 strcpy(p->datakey->key, ek);
350 void regenerate_keys(void)
353 generate_private_key();
354 calculate_public_key();
355 send_key_changed_all();
356 recalculate_encryption_keys();