crypto_ed25519.c 4.0 KB

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