stun: memset saddr before binding
[netsniff-ng.git] / keypair.c
blob724bf057eba307942b4ae32b7f4b78230715697a
1 #include <string.h>
2 #include <unistd.h>
3 #include <sys/types.h>
4 #include <pwd.h>
5 #include <stdio.h>
7 #include "rnd.h"
8 #include "die.h"
9 #include "str.h"
10 #include "crypto.h"
11 #include "ioops.h"
12 #include "config.h"
13 #include "keypair.h"
15 void generate_keypair(void)
17 struct passwd *pw = getpwuid(getuid());
18 unsigned char publickey[crypto_box_pub_key_size];
19 unsigned char secretkey[crypto_box_sec_key_size];
20 char file[128];
22 xmemset(publickey, 0, sizeof(publickey));
23 xmemset(secretkey, 0, sizeof(secretkey));
25 curve25519_selftest();
27 printf("Reading from %s (this may take a while) ...\n",
28 HIG_ENTROPY_SOURCE);
30 gen_key_bytes(secretkey, sizeof(secretkey));
31 crypto_scalarmult_curve25519_base(publickey, secretkey);
33 slprintf(file, sizeof(file), "%s/%s", pw->pw_dir, FILE_PUBKEY);
34 write_blob_or_die(file, publickey, sizeof(publickey));
35 printf("Public key written to %s!\n", file);
37 slprintf(file, sizeof(file), "%s/%s", pw->pw_dir, FILE_PRIVKEY);
38 write_blob_or_die(file, secretkey, sizeof(secretkey));
39 printf("Secret key written to %s!\n", file);
41 xmemset(publickey, 0, sizeof(publickey));
42 xmemset(secretkey, 0, sizeof(secretkey));
45 void verify_keypair(void)
47 int result;
48 struct passwd *pw = getpwuid(getuid());
49 unsigned char publickey[crypto_box_pub_key_size];
50 unsigned char publicres[crypto_box_pub_key_size];
51 unsigned char secretkey[crypto_box_sec_key_size];
52 char file[128];
54 curve25519_selftest();
56 xmemset(publickey, 0, sizeof(publickey));
57 xmemset(publicres, 0, sizeof(publicres));
58 xmemset(secretkey, 0, sizeof(secretkey));
60 slprintf(file, sizeof(file), "%s/%s", pw->pw_dir, FILE_PUBKEY);
61 read_blob_or_die(file, publickey, sizeof(publickey));
63 slprintf(file, sizeof(file), "%s/%s", pw->pw_dir, FILE_PRIVKEY);
64 read_blob_or_die(file, secretkey, sizeof(secretkey));
66 crypto_scalarmult_curve25519_base(publicres, secretkey);
67 result = crypto_verify_32(publicres, publickey);
69 xmemset(publickey, 0, sizeof(publickey));
70 xmemset(publicres, 0, sizeof(publicres));
71 xmemset(secretkey, 0, sizeof(secretkey));
73 if (result)
74 panic("Keypair is corrupt! You need to regenerate!\n");