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