curvecpmakekey.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #include <sys/types.h>
  2. #include <sys/stat.h>
  3. #include <unistd.h>
  4. #include "die.h"
  5. #include "e.h"
  6. #include "savesync.h"
  7. #include "randombytes.h"
  8. #include "crypto_box.h"
  9. void die_usage(void)
  10. {
  11. die_1(111,"curvecpmakekey: usage: curvecpmakekey keydir\n");
  12. }
  13. void die_fatal(const char *trouble,const char *d,const char *fn)
  14. {
  15. if (fn) die_9(111,"curvecpmakekey: fatal: ",trouble," ",d,"/",fn,": ",e_str(errno),"\n");
  16. die_7(111,"curvecpmakekey: fatal: ",trouble," ",d,": ",e_str(errno),"\n");
  17. }
  18. unsigned char pk[crypto_box_PUBLICKEYBYTES];
  19. unsigned char sk[crypto_box_SECRETKEYBYTES];
  20. unsigned char lock[1];
  21. unsigned char noncekey[32];
  22. unsigned char noncecounter[8];
  23. void create(const char *d,const char *fn,const unsigned char *x,long long xlen)
  24. {
  25. if (savesync(fn,x,xlen) == -1) die_fatal("unable to create",d,fn);
  26. }
  27. int main(int argc,char **argv)
  28. {
  29. char *d;
  30. if (!argv[0]) die_usage();
  31. if (!argv[1]) die_usage();
  32. d = argv[1];
  33. umask(022);
  34. if (mkdir(d,0755) == -1) die_fatal("unable to create directory",d,0);
  35. if (chdir(d) == -1) die_fatal("unable to chdir to directory",d,0);
  36. if (mkdir(".expertsonly",0700) == -1) die_fatal("unable to create directory",d,".expertsonly");
  37. crypto_box_keypair(pk,sk);
  38. create(d,"publickey",pk,sizeof pk);
  39. randombytes(noncekey,sizeof noncekey);
  40. umask(077);
  41. create(d,".expertsonly/secretkey",sk,sizeof sk);
  42. create(d,".expertsonly/lock",lock,sizeof lock);
  43. create(d,".expertsonly/noncekey",noncekey,sizeof noncekey);
  44. create(d,".expertsonly/noncecounter",noncecounter,sizeof noncecounter);
  45. return 0;
  46. }