2 encr.c -- everything that deals with encryption
3 Copyright (C) 1998,99 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.
27 #include <sys/socket.h>
33 # ifdef HAVE_GMP2_GMP_H
34 # include <gmp2/gmp.h>
48 #define ENCR_GENERATOR "0xd"
49 #define ENCR_PRIME "0x7fffffffffffffffffffffffffffffff" /* Mersenne :) */
52 char *my_public_key_base36;
53 int key_inited = 0, encryption_keylen;
54 mpz_t my_private_key, my_public_key, generator, shared_prime;
55 int my_key_expiry = (time_t)(-1);
57 static char* mypassphrase;
58 static int mypassphraselen;
60 int char_hex_to_bin(int c)
65 return tolower(c) - 'a' + 10;
68 int str_hex_to_bin(unsigned char *bin, unsigned char *hex)
70 int i = 0, j = 0, l = strlen(hex);
75 bin[0] = char_hex_to_bin(hex[0]);
77 for(; i < l; i+=2, j++)
78 bin[j] = (char_hex_to_bin(hex[i]) << 4) + char_hex_to_bin(hex[i+1]);
83 int read_passphrase(char *which, char **out)
89 extern char *confbase;
92 if((cfg = get_config_val(passphrasesdir)) == NULL)
94 filename = xmalloc(strlen(confbase)+13+strlen(which));
95 sprintf(filename, "%spassphrases/%s", confbase, which);
99 filename = xmalloc(strlen(cfg->data.ptr)+2+strlen(which));
100 sprintf(filename, "%s/%s", (char*)cfg->data.ptr, which);
103 if((f = fopen(filename, "rb")) == NULL)
105 syslog(LOG_ERR, "Could not open %s: %m", filename);
109 fscanf(f, "%d ", &size);
110 if(size < 1 || size > (1<<15))
112 syslog(LOG_ERR, "Illegal passphrase in %s; size would be %d", filename, size);
115 size >>= 2; /* bits->nibbles */
116 pp = xmalloc(size+2);
117 fgets(pp, size+1, f);
120 *out = xmalloc(size);
122 return str_hex_to_bin(*out, pp);
125 int read_my_passphrase(void)
128 if((mypassphraselen = read_passphrase("local", &mypassphrase)) < 0)
134 int generate_private_key(void)
141 if((cfg = get_config_val(keyexpire)) == NULL)
142 my_key_expiry = (time_t)(time(NULL) + 3600);
144 my_key_expiry = (time_t)(time(NULL) + cfg->data.val);
146 syslog(LOG_NOTICE, "Generating %d bits keys.", PRIVATE_KEY_BITS);
148 if((f = fopen("/dev/urandom", "r")) == NULL)
150 syslog(LOG_ERR, "Opening /dev/urandom failed: %m");
154 s = xmalloc((2 * PRIVATE_KEY_LENGTH) + 1);
156 for(i = 0; i < PRIVATE_KEY_LENGTH; i++)
157 sprintf(&s[i << 1], "%02x", fgetc(f));
159 s[2 * PRIVATE_KEY_LENGTH] = '\0';
161 mpz_set_str(my_private_key, s, 16);
166 void calculate_public_key(void)
169 mpz_powm(my_public_key, generator, my_private_key, shared_prime);
170 my_public_key_base36 = mpz_get_str(NULL, 36, my_public_key);
174 unsigned char static_key[] = { 0x9c, 0xbf, 0x36, 0xa9, 0xce, 0x20, 0x1b, 0x8b, 0x67, 0x56, 0x21, 0x5d, 0x27, 0x1b, 0xd8, 0x7a };
176 int security_init(void)
179 mpz_init(my_private_key);
180 mpz_init(my_public_key);
181 mpz_init_set_str(shared_prime, ENCR_PRIME, 0);
182 mpz_init_set_str(generator, ENCR_GENERATOR, 0);
184 if(read_my_passphrase() < 0)
186 if(generate_private_key() < 0)
189 if(cipher_init(CIPHER_BLOWFISH) < 0)
192 calculate_public_key();
197 void set_shared_key(char *almost_key)
201 mpz_t ak, our_shared_key;
203 mpz_init_set_str(ak, almost_key, 36);
204 mpz_init(our_shared_key);
205 mpz_powm(our_shared_key, ak, my_private_key, shared_prime);
207 tmp = mpz_get_str(NULL, 16, our_shared_key);
208 len = str_hex_to_bin(text_key, tmp);
210 cipher_set_key(&encryption_key, len, &text_key[0]);
212 encryption_keylen = len;
215 syslog(LOG_INFO, "Encryption key set to %s", tmp);
219 mpz_clear(our_shared_key);
224 void encrypt_passphrase(passphrase_t *pp)
231 mpz_get_str(&tmp[0], 16, my_public_key);
232 len = str_hex_to_bin(key, tmp);
234 cipher_set_key(&bf_key, len, &key[0]);
236 low_crypt_key(mypassphrase, pp->phrase, &bf_key, mypassphraselen, BF_ENCRYPT);
237 pp->len = ((mypassphraselen - 1) | 7) + 5;
240 cipher_set_key(&encryption_key, encryption_keylen, &text_key[0]);
244 int verify_passphrase(conn_list_t *cl, unsigned char *his_pubkey)
252 char which[sizeof("123.123.123.123")+1];
255 mpz_init_set_str(pk, his_pubkey, 36);
256 mpz_get_str(&tmp[0], 16, pk);
257 len = str_hex_to_bin(key, tmp);
258 out = xmalloc(cl->pp->len+3);
260 cipher_set_key(&bf_key, len, &key[0]);
261 low_crypt_key(cl->pp->phrase, out, &bf_key, cl->pp->len, BF_DECRYPT);
263 cipher_set_key(&encryption_key, encryption_keylen, &text_key[0]);
265 sprintf(&which[0], IP_ADDR_S, IP_ADDR_V(cl->vpn_ip));
266 if((len = read_passphrase(which, &meuk)) < 0)
269 if(memcmp(meuk, out, len))
275 char *make_shared_key(char *pk)
280 mpz_init_set_str(tmp, pk, 36);
282 mpz_powm(res, tmp, my_private_key, shared_prime);
284 r = mpz_get_str(NULL, 36, res);
293 free a key after overwriting it
295 void free_key(enc_key_t *k)
302 memset(k->key, (char)(-1), k->length);
309 void recalculate_encryption_keys(void)
314 for(p = conn_list; p != NULL; p = p->next)
316 if(!p->public_key || !p->public_key->key)
317 /* We haven't received a key from this host (yet). */
319 ek = make_shared_key(p->public_key->key);
321 p->key = xmalloc(sizeof(enc_key_t));
322 p->key->length = strlen(ek);
323 p->key->expiry = p->public_key->expiry;
324 p->key->key = xmalloc(strlen(ek) + 1);
325 strcpy(p->key->key, ek);
330 void regenerate_keys(void)
333 generate_private_key();
334 calculate_public_key();
336 recalculate_encryption_keys();