djbdnscurve6 45
djbdnscurve6
Loading...
Searching...
No Matches
curvedns-keygen.c
Go to the documentation of this file.
1#include <sys/stat.h>
2#include <unistd.h>
3#include "logmsg.h"
4#include "str.h"
5#include "case.h"
6#include "open.h"
7#include "close.h"
8#include "stralloc.h"
9#include "base32.h"
10#include "auto_home.h"
11#include "uint_t.h"
12#include "generic-conf.h"
13#include "curve.h"
14#include <stdio.h>
15#include "byte.h"
16
17#define WHO "curvedns-keygen"
18
19int main()
20{
21 struct stat st;
22 char hexpublic[65];
23 char hexprivate[65];
24 char dnsname[55];
25 uint8 public[32];
26 uint8 private[32];
27
28 /* check if already exists */
29
30 if (chdir("env") == -1)
31 logmsg(WHO,111,FATAL,"unable to switch to: ./env");
32
33 if (stat("CURVEDNS_PRIVATE_KEY",&st) == 0)
34 logmsg(WHO,100,ERROR,"A private key file already exists; remove that first.");
35
36 if (!crypto_random_init())
37 logmsg(WHO,100,FATAL,"unable to ensure randomness");
38
39 // Generate the actual keypair
40 if (crypto_box_keypair(public,private))
41 logmsg(WHO,100,FATAL,"unable to generate public/private key pair");
42
43 // The DNSCurve (base32)-encoding of the PUBLIC key
44 byte_copy(dnsname,3,"uz5");
45 if (base32_serverkey(dnsname + 3,public,32) != 52)
46 logmsg(WHO,100,INFO,"base32_encode of public key failed");
47
48 // The hex encoding of the PUBLIC key
49 if (!hex_encode(public,32,hexpublic,64))
50 logmsg(WHO,100,ERROR,"hex_encode of public key failed");
51
52 // The hex encoding of the PRIVATE key
53 if (!hex_encode(private,32,hexprivate,64))
54 logmsg(WHO,100,ERROR,"hex_encode of private key failed");
55
56 hexpublic[64] = '\0';
57 hexprivate[64] = '\0';
58 dnsname[54] = '\0';
59
60 start("CURVEDNS_PRIVATE_KEY");
61 out(private,32);
62 finish();
63 perm(0400);
64
65 start(dnsname);
66 outs(hexpublic);
67 outs("\n");
68 finish();
69 perm(0644);
70
71 /* Report */
72
73 logmsg(WHO,INFO,0,B("DNS public key: ",dnsname));
74 logmsg(WHO,INFO,0,B("Hex public key: ",hexpublic));
75 logmsg(WHO,INFO,0,B("Hex secret key: ",hexprivate));
76
77 return 0;
78}
unsigned int base32_serverkey(uint8 *out, const char *in, unsigned int len)
Definition: base32.c:127
#define crypto_box_keypair
Definition: curve.h:11
#define WHO
int main()
int crypto_random_init(void)
Definition: dns_random.c:90
void outs(const char *s)
Definition: generic-conf.c:49
void start(const char *s)
Definition: generic-conf.c:41
void perm(int mode)
Definition: generic-conf.c:71
void finish(void)
Definition: generic-conf.c:64
void out(const char *s, unsigned int len)
Definition: generic-conf.c:54
int hex_encode(const uint8 *src, int srclen, char *dst, int dstlen)
Definition: hex.c:42