dev: Integrate `promisc' module into `dev' module
[netsniff-ng.git] / keypair.c
blob5c099ad66cbc13e44fb8e21ad802e3048b86adcd
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 "curve.h"
12 #include "ioops.h"
13 #include "config.h"
14 #include "keypair.h"
16 void generate_keypair(void)
18 struct passwd *pw = getpwuid(getuid());
19 unsigned char publickey[crypto_box_pub_key_size];
20 unsigned char secretkey[crypto_box_sec_key_size];
21 char file[128];
23 xmemset(publickey, 0, sizeof(publickey));
24 xmemset(secretkey, 0, sizeof(secretkey));
26 curve25519_selftest();
28 printf("Reading from %s (this may take a while) ...\n",
29 HIG_ENTROPY_SOURCE);
31 gen_key_bytes(secretkey, sizeof(secretkey));
32 crypto_scalarmult_curve25519_base(publickey, secretkey);
34 slprintf(file, sizeof(file), "%s/%s", pw->pw_dir, FILE_PUBKEY);
35 write_blob_or_die(file, publickey, sizeof(publickey));
36 printf("Public key written to %s!\n", file);
38 slprintf(file, sizeof(file), "%s/%s", pw->pw_dir, FILE_PRIVKEY);
39 write_blob_or_die(file, secretkey, sizeof(secretkey));
40 printf("Secret key written to %s!\n", file);
42 xmemset(publickey, 0, sizeof(publickey));
43 xmemset(secretkey, 0, sizeof(secretkey));
46 void verify_keypair(void)
48 int result;
49 struct passwd *pw = getpwuid(getuid());
50 unsigned char publickey[crypto_box_pub_key_size];
51 unsigned char publicres[crypto_box_pub_key_size];
52 unsigned char secretkey[crypto_box_sec_key_size];
53 char file[128];
55 curve25519_selftest();
57 xmemset(publickey, 0, sizeof(publickey));
58 xmemset(publicres, 0, sizeof(publicres));
59 xmemset(secretkey, 0, sizeof(secretkey));
61 slprintf(file, sizeof(file), "%s/%s", pw->pw_dir, FILE_PUBKEY);
62 read_blob_or_die(file, publickey, sizeof(publickey));
64 slprintf(file, sizeof(file), "%s/%s", pw->pw_dir, FILE_PRIVKEY);
65 read_blob_or_die(file, secretkey, sizeof(secretkey));
67 crypto_scalarmult_curve25519_base(publicres, secretkey);
68 result = crypto_verify_32(publicres, publickey);
70 xmemset(publickey, 0, sizeof(publickey));
71 xmemset(publicres, 0, sizeof(publicres));
72 xmemset(secretkey, 0, sizeof(secretkey));
74 if (result)
75 panic("Keypair is corrupt! You need to regenerate!\n");