Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

generate_ed25519.cc 2.1 KiB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 "../crypto/test/scoped_types.h"
  19. #include "internal.h"
  20. static const struct argument kArguments[] = {
  21. {
  22. "-out-public", kRequiredArgument, "The file to write the public key to",
  23. },
  24. {
  25. "-out-private", kRequiredArgument,
  26. "The file to write the private key to",
  27. },
  28. {
  29. "", kOptionalArgument, "",
  30. },
  31. };
  32. static bool WriteToFile(const std::string &path, const uint8_t *in,
  33. size_t in_len) {
  34. ScopedFILE file(fopen(path.c_str(), "wb"));
  35. if (!file) {
  36. fprintf(stderr, "Failed to open '%s': %s\n", path.c_str(), strerror(errno));
  37. return false;
  38. }
  39. if (fwrite(in, in_len, 1, file.get()) != 1) {
  40. fprintf(stderr, "Failed to write to '%s': %s\n", path.c_str(),
  41. strerror(errno));
  42. return false;
  43. }
  44. return true;
  45. }
  46. bool GenerateEd25519Key(const std::vector<std::string> &args) {
  47. std::map<std::string, std::string> args_map;
  48. if (!ParseKeyValueArguments(&args_map, args, kArguments)) {
  49. PrintUsage(kArguments);
  50. return false;
  51. }
  52. uint8_t public_key[32], private_key[64];
  53. ED25519_keypair(public_key, private_key);
  54. return WriteToFile(args_map["-out-public"], public_key, sizeof(public_key)) &&
  55. WriteToFile(args_map["-out-private"], private_key,
  56. sizeof(private_key));
  57. }