crypto_ed25519.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /* Copyright (c) 2013, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /* Wrapper code for an ed25519 implementation. */
  4. #include "orconfig.h"
  5. #ifdef HAVE_SYS_STAT_H
  6. #include <sys/stat.h>
  7. #endif
  8. #include "crypto.h"
  9. #include "crypto_curve25519.h"
  10. #include "crypto_ed25519.h"
  11. #include "torlog.h"
  12. #include "util.h"
  13. #include "ed25519/ref10/ed25519_ref10.h"
  14. int
  15. ed25519_secret_key_generate(ed25519_secret_key_t *seckey_out,
  16. int extra_strong)
  17. {
  18. int r;
  19. uint8_t seed[32];
  20. if (! extra_strong || crypto_strongest_rand(seed, sizeof(seed)) < 0)
  21. crypto_rand((char*)seed, sizeof(seed));
  22. r = ed25519_ref10_seckey_expand(seckey_out->seckey, seed);
  23. memwipe(seed, 0, sizeof(seed));
  24. return r < 0 ? -1 : 0;
  25. }
  26. int
  27. ed25519_secret_key_from_seed(ed25519_secret_key_t *seckey_out,
  28. const uint8_t *seed)
  29. {
  30. if (ed25519_ref10_seckey_expand(seckey_out->seckey, seed) < 0)
  31. return -1;
  32. return 0;
  33. }
  34. int
  35. ed25519_public_key_generate(ed25519_public_key_t *pubkey_out,
  36. const ed25519_secret_key_t *seckey)
  37. {
  38. if (ed25519_ref10_pubkey(pubkey_out->pubkey, seckey->seckey) < 0)
  39. return -1;
  40. return 0;
  41. }
  42. /** Generate a new ed25519 keypair in <b>keypair_out</b>. If
  43. * <b>extra_strong</b> is set, try to mix some system entropy into the key
  44. * generation process. Return 0 on success, -1 on failure. */
  45. int
  46. ed25519_keypair_generate(ed25519_keypair_t *keypair_out, int extra_strong)
  47. {
  48. if (ed25519_secret_key_generate(&keypair_out->seckey, extra_strong) < 0)
  49. return -1;
  50. if (ed25519_public_key_generate(&keypair_out->pubkey,
  51. &keypair_out->seckey)<0)
  52. return -1;
  53. return 0;
  54. }
  55. /**
  56. * Set <b>signature_out</b> to a signature of the <b>len</b>-byte message
  57. * <b>msg</b>, using the secret and public key in <b>keypair</b>.
  58. */
  59. int
  60. ed25519_sign(ed25519_signature_t *signature_out,
  61. const uint8_t *msg, size_t len,
  62. const ed25519_keypair_t *keypair)
  63. {
  64. if (ed25519_ref10_sign(signature_out->sig, msg, len,
  65. keypair->seckey.seckey,
  66. keypair->pubkey.pubkey) < 0) {
  67. return -1;
  68. }
  69. return 0;
  70. }
  71. /**
  72. * Check whether if <b>signature</b> is a valid signature for the
  73. * <b>len</b>-byte message in <b>msg</b> made with the key <b>pubkey</b>.
  74. *
  75. * Return 0 if the signature is valid; -1 if it isn't.
  76. */
  77. int
  78. ed25519_checksig(const ed25519_signature_t *signature,
  79. const uint8_t *msg, size_t len,
  80. const ed25519_public_key_t *pubkey)
  81. {
  82. return
  83. ed25519_ref10_open(signature->sig, msg, len, pubkey->pubkey) < 0 ? -1 : 0;
  84. }
  85. /** Validate every signature among those in <b>checkable</b>, which contains
  86. * exactly <b>n_checkable</b> elements. If <b>okay_out</b> is non-NULL, set
  87. * the i'th element of <b>okay_out</b> to 1 if the i'th element of
  88. * <b>checkable</b> is valid, and to 0 otherwise. Return 0 if every signature
  89. * was valid. Otherwise return -N, where N is the number of invalid
  90. * signatures.
  91. */
  92. int
  93. ed25519_checksig_batch(int *okay_out,
  94. const ed25519_checkable_t *checkable,
  95. int n_checkable)
  96. {
  97. int res, i;
  98. res = 0;
  99. for (i = 0; i < n_checkable; ++i) {
  100. const ed25519_checkable_t *ch = &checkable[i];
  101. int r = ed25519_checksig(&ch->signature, ch->msg, ch->len, ch->pubkey);
  102. if (r < 0)
  103. --res;
  104. if (okay_out)
  105. okay_out[i] = (r == 0);
  106. }
  107. #if 0
  108. const uint8_t **ms;
  109. size_t *lens;
  110. const uint8_t **pks;
  111. const uint8_t **sigs;
  112. int *oks;
  113. ms = tor_malloc(sizeof(uint8_t*)*n_checkable);
  114. lens = tor_malloc(sizeof(size_t)*n_checkable);
  115. pks = tor_malloc(sizeof(uint8_t*)*n_checkable);
  116. sigs = tor_malloc(sizeof(uint8_t*)*n_checkable);
  117. oks = okay_out ? okay_out : tor_malloc(sizeof(int)*n_checkable);
  118. for (i = 0; i < n_checkable; ++i) {
  119. ms[i] = checkable[i].msg;
  120. lens[i] = checkable[i].len;
  121. pks[i] = checkable[i].pubkey->pubkey;
  122. sigs[i] = checkable[i].signature.sig;
  123. oks[i] = 0;
  124. }
  125. ed25519_sign_open_batch_donna_fb(ms, lens, pks, sigs, n_checkable, oks);
  126. res = 0;
  127. for (i = 0; i < n_checkable; ++i) {
  128. if (!oks[i])
  129. --res;
  130. }
  131. tor_free(ms);
  132. tor_free(lens);
  133. tor_free(pks);
  134. if (! okay_out)
  135. tor_free(oks);
  136. #endif
  137. return res;
  138. }