You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

75 lines
2.2 KiB

  1. /* Copyright (c) 2015, Google Inc.
  2. *
  3. * Permission to use, copy, modify, and/or distribute this software for any
  4. * purpose with or without fee is hereby granted, provided that the above
  5. * copyright notice and this permission notice appear in all copies.
  6. *
  7. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  10. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  12. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
  14. #include <openssl/curve25519.h>
  15. #include <errno.h>
  16. #include <stdio.h>
  17. #include <string.h>
  18. #include "internal.h"
  19. struct FileCloser {
  20. void operator()(FILE *file) {
  21. fclose(file);
  22. }
  23. };
  24. using ScopedFILE = std::unique_ptr<FILE, FileCloser>;
  25. static const struct argument kArguments[] = {
  26. {
  27. "-out-public", kRequiredArgument, "The file to write the public key to",
  28. },
  29. {
  30. "-out-private", kRequiredArgument,
  31. "The file to write the private key to",
  32. },
  33. {
  34. "", kOptionalArgument, "",
  35. },
  36. };
  37. static bool WriteToFile(const std::string &path, const uint8_t *in,
  38. size_t in_len) {
  39. ScopedFILE file(fopen(path.c_str(), "wb"));
  40. if (!file) {
  41. fprintf(stderr, "Failed to open '%s': %s\n", path.c_str(), strerror(errno));
  42. return false;
  43. }
  44. if (fwrite(in, in_len, 1, file.get()) != 1) {
  45. fprintf(stderr, "Failed to write to '%s': %s\n", path.c_str(),
  46. strerror(errno));
  47. return false;
  48. }
  49. return true;
  50. }
  51. bool GenerateEd25519Key(const std::vector<std::string> &args) {
  52. std::map<std::string, std::string> args_map;
  53. if (!ParseKeyValueArguments(&args_map, args, kArguments)) {
  54. PrintUsage(kArguments);
  55. return false;
  56. }
  57. uint8_t public_key[32], private_key[64];
  58. ED25519_keypair(public_key, private_key);
  59. return WriteToFile(args_map["-out-public"], public_key, sizeof(public_key)) &&
  60. WriteToFile(args_map["-out-private"], private_key,
  61. sizeof(private_key));
  62. }