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>
50 #define ENCR_GENERATOR "0xd"
51 #define ENCR_PRIME "0x7fffffffffffffffffffffffffffffff" /* Mersenne :) */
54 char *my_public_key_base36;
55 int key_inited = 0, encryption_keylen;
56 mpz_t my_private_key, my_public_key, generator, shared_prime;
57 int my_key_expiry = (time_t)(-1);
59 static char* mypassphrase;
60 static int mypassphraselen;
62 int char_hex_to_bin(int c)
67 return tolower(c) - 'a' + 10;
70 int str_hex_to_bin(unsigned char *bin, unsigned char *hex)
72 int i = 0, j = 0, l = strlen(hex);
77 bin[0] = char_hex_to_bin(hex[0]);
79 for(; i < l; i+=2, j++)
80 bin[j] = (char_hex_to_bin(hex[i]) << 4) + char_hex_to_bin(hex[i+1]);
85 int read_passphrase(char *which, char **out)
91 extern char *confbase;
94 if((cfg = get_config_val(passphrasesdir)) == NULL)
96 filename = xmalloc(strlen(confbase)+13+strlen(which));
97 sprintf(filename, "%spassphrases/%s", confbase, which);
101 filename = xmalloc(strlen(cfg->data.ptr)+2+strlen(which));
102 sprintf(filename, "%s/%s", (char*)cfg->data.ptr, which);
105 if((f = fopen(filename, "rb")) == NULL)
107 syslog(LOG_ERR, "Could not open %s: %m", filename);
111 fscanf(f, "%d ", &size);
112 if(size < 1 || size > (1<<15))
114 syslog(LOG_ERR, "Illegal passphrase in %s; size would be %d", filename, size);
117 size >>= 2; /* bits->nibbles */
118 pp = xmalloc(size+2);
119 fgets(pp, size+1, f);
122 *out = xmalloc(size);
124 return str_hex_to_bin(*out, pp);
127 int read_my_passphrase(void)
130 if((mypassphraselen = read_passphrase("local", &mypassphrase)) < 0)
136 int generate_private_key(void)
143 if((cfg = get_config_val(keyexpire)) == NULL)
144 my_key_expiry = (time_t)(time(NULL) + 3600);
146 my_key_expiry = (time_t)(time(NULL) + cfg->data.val);
148 syslog(LOG_NOTICE, "Generating %d bits keys.", PRIVATE_KEY_BITS);
150 if((f = fopen("/dev/urandom", "r")) == NULL)
152 syslog(LOG_ERR, "Opening /dev/urandom failed: %m");
156 s = xmalloc((2 * PRIVATE_KEY_LENGTH) + 1);
158 for(i = 0; i < PRIVATE_KEY_LENGTH; i++)
159 sprintf(&s[i << 1], "%02x", fgetc(f));
161 s[2 * PRIVATE_KEY_LENGTH] = '\0';
163 mpz_set_str(my_private_key, s, 16);
168 void calculate_public_key(void)
171 mpz_powm(my_public_key, generator, my_private_key, shared_prime);
172 my_public_key_base36 = mpz_get_str(NULL, 36, my_public_key);
176 unsigned char static_key[] = { 0x9c, 0xbf, 0x36, 0xa9, 0xce, 0x20, 0x1b, 0x8b, 0x67, 0x56, 0x21, 0x5d, 0x27, 0x1b, 0xd8, 0x7a };
178 int security_init(void)
181 mpz_init(my_private_key);
182 mpz_init(my_public_key);
183 mpz_init_set_str(shared_prime, ENCR_PRIME, 0);
184 mpz_init_set_str(generator, ENCR_GENERATOR, 0);
186 if(read_my_passphrase() < 0)
188 if(generate_private_key() < 0)
191 if(cipher_init(CIPHER_BLOWFISH) < 0)
194 calculate_public_key();
199 void set_shared_key(char *almost_key)
203 mpz_t ak, our_shared_key;
205 mpz_init_set_str(ak, almost_key, 36);
206 mpz_init(our_shared_key);
207 mpz_powm(our_shared_key, ak, my_private_key, shared_prime);
209 tmp = mpz_get_str(NULL, 16, our_shared_key);
210 len = str_hex_to_bin(text_key, tmp);
212 cipher_set_key(&encryption_key, len, text_key);
214 encryption_keylen = len;
217 syslog(LOG_INFO, "Encryption key set to %s", tmp);
221 mpz_clear(our_shared_key);
226 void encrypt_passphrase(passphrase_t *pp)
230 unsigned char phrase[1000];
236 mpz_get_str(tmp, 16, my_public_key);
237 keylen = str_hex_to_bin(key, tmp);
239 cipher_set_key(&bf_key, keylen, key);
241 low_crypt_key(mypassphrase, phrase, &bf_key, mypassphraselen, BF_ENCRYPT);
242 pp->len = ((mypassphraselen - 1) | 7) + 1;
243 pp->phrase = xmalloc((pp->len << 1) + 1);
245 for(i = 0; i < pp->len; i++)
246 snprintf(&(pp->phrase)[i << 1], 3, "%02x", (int)phrase[i]);
248 pp->phrase[(pp->len << 1) + 1] = '\0';
251 cipher_set_key(&encryption_key, encryption_keylen, text_key);
255 int verify_passphrase(conn_list_t *cl, unsigned char *his_pubkey)
259 unsigned char phrase[1000];
264 char which[sizeof("123.123.123.123")+1];
267 mpz_init_set_str(pk, his_pubkey, 36);
268 tmp = mpz_get_str(NULL, 16, pk);
269 keylen = str_hex_to_bin(key, tmp);
270 out = xmalloc((cl->pp->len >> 1) + 3);
271 pplen = str_hex_to_bin(phrase, cl->pp->phrase);
273 cipher_set_key(&bf_key, keylen, key);
274 low_crypt_key(phrase, out, &bf_key, pplen, BF_DECRYPT);
276 cipher_set_key(&encryption_key, encryption_keylen, text_key);
278 sprintf(which, IP_ADDR_S, IP_ADDR_V(cl->vpn_ip));
279 if((pplen = read_passphrase(which, &meuk)) < 0)
282 if(memcmp(meuk, out, pplen))
288 char *make_shared_key(char *pk)
293 mpz_init_set_str(tmp, pk, 36);
295 mpz_powm(res, tmp, my_private_key, shared_prime);
297 r = mpz_get_str(NULL, 36, res);
306 free a key after overwriting it
308 void free_key(enc_key_t *k)
315 memset(k->key, (char)(-1), k->length);
322 void recalculate_encryption_keys(void)
327 for(p = conn_list; p != NULL; p = p->next)
329 if(!p->public_key || !p->public_key->key)
330 /* We haven't received a key from this host (yet). */
332 ek = make_shared_key(p->public_key->key);
334 p->key = xmalloc(sizeof(*p->key));
335 p->key->length = strlen(ek);
336 p->key->expiry = p->public_key->expiry;
337 p->key->key = xmalloc(strlen(ek) + 1);
338 strcpy(p->key->key, ek);
343 void regenerate_keys(void)
346 generate_private_key();
347 calculate_public_key();
348 send_key_changed_all();
349 recalculate_encryption_keys();