crypto_ed25519.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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_public_key_generate(ed25519_public_key_t *pubkey_out,
  25. const ed25519_secret_key_t *seckey)
  26. {
  27. if (ed25519_ref10_pubkey(pubkey_out->pubkey, seckey->seckey) < 0)
  28. return -1;
  29. return 0;
  30. }
  31. /** Generate a new ed25519 keypair in <b>keypair_out</b>. If
  32. * <b>extra_strong</b> is set, try to mix some system entropy into the key
  33. * generation process. Return 0 on success, -1 on failure. */
  34. int
  35. ed25519_keypair_generate(ed25519_keypair_t *keypair_out, int extra_strong)
  36. {
  37. (void) extra_strong;
  38. if (ed25519_ref10_keygen(keypair_out->pubkey.pubkey,
  39. keypair_out->seckey.seckey)<0)
  40. return -1;
  41. return 0;
  42. }
  43. /**
  44. * Set <b>signature_out</b> to a signature of the <b>len</b>-byte message
  45. * <b>msg</b>, using the secret and public key in <b>keypair</b>.
  46. */
  47. int
  48. ed25519_sign(ed25519_signature_t *signature_out,
  49. const uint8_t *msg, size_t len,
  50. const ed25519_keypair_t *keypair)
  51. {
  52. uint8_t keys[64];
  53. uint8_t *tmp;
  54. uint64_t tmplen;
  55. /* XXXX Make crypto_sign in ref10 friendlier so we don't need this stupid
  56. * copying. */
  57. tor_assert(len < SIZE_T_CEILING - 64);
  58. tmplen = ((uint64_t)len) + 64;
  59. tmp = tor_malloc(tmplen);
  60. memcpy(keys, keypair->seckey.seckey, 32);
  61. memcpy(keys+32, keypair->pubkey.pubkey, 32);
  62. if (ed25519_ref10_sign(tmp, &tmplen, msg, len, keys) < 0) {
  63. tor_free(tmp);
  64. return -1;
  65. }
  66. memcpy(signature_out->sig, tmp, 64);
  67. memwipe(keys, 0, sizeof(keys));
  68. tor_free(tmp);
  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. uint8_t *smtmp;
  83. uint8_t *tmp;
  84. uint64_t tmplen;
  85. int r;
  86. tor_assert(len < SIZE_T_CEILING - 64);
  87. tmplen = len + 64;
  88. tmp = tor_malloc(tmplen);
  89. smtmp = tor_malloc(tmplen);
  90. memcpy(smtmp, signature->sig, 64);
  91. memcpy(smtmp+64, msg, len);
  92. r = ed25519_ref10_open(tmp, &tmplen, smtmp, tmplen, pubkey->pubkey);
  93. tor_free(tmp);
  94. tor_free(smtmp);
  95. return r;
  96. }
  97. /** Validate every signature among those in <b>checkable</b>, which contains
  98. * exactly <b>n_checkable</b> elements. If <b>okay_out</b> is non-NULL, set
  99. * the i'th element of <b>okay_out</b> to 1 if the i'th element of
  100. * <b>checkable</b> is valid, and to 0 otherwise. Return 0 if every signature
  101. * was valid. Otherwise return -N, where N is the number of invalid
  102. * signatures.
  103. */
  104. int
  105. ed25519_checksig_batch(int *okay_out,
  106. const ed25519_checkable_t *checkable,
  107. int n_checkable)
  108. {
  109. int res, i;
  110. res = 0;
  111. for (i = 0; i < n_checkable; ++i) {
  112. const ed25519_checkable_t *ch = &checkable[i];
  113. int r = ed25519_checksig(&ch->signature, ch->msg, ch->len, ch->pubkey);
  114. if (r < 0)
  115. --res;
  116. if (okay_out)
  117. okay_out[i] = (r == 0);
  118. }
  119. #if 0
  120. const uint8_t **ms;
  121. size_t *lens;
  122. const uint8_t **pks;
  123. const uint8_t **sigs;
  124. int *oks;
  125. ms = tor_malloc(sizeof(uint8_t*)*n_checkable);
  126. lens = tor_malloc(sizeof(size_t)*n_checkable);
  127. pks = tor_malloc(sizeof(uint8_t*)*n_checkable);
  128. sigs = tor_malloc(sizeof(uint8_t*)*n_checkable);
  129. oks = okay_out ? okay_out : tor_malloc(sizeof(int)*n_checkable);
  130. for (i = 0; i < n_checkable; ++i) {
  131. ms[i] = checkable[i].msg;
  132. lens[i] = checkable[i].len;
  133. pks[i] = checkable[i].pubkey->pubkey;
  134. sigs[i] = checkable[i].signature.sig;
  135. oks[i] = 0;
  136. }
  137. ed25519_sign_open_batch_donna_fb(ms, lens, pks, sigs, n_checkable, oks);
  138. res = 0;
  139. for (i = 0; i < n_checkable; ++i) {
  140. if (!oks[i])
  141. --res;
  142. }
  143. tor_free(ms);
  144. tor_free(lens);
  145. tor_free(pks);
  146. if (! okay_out)
  147. tor_free(oks);
  148. #endif
  149. return res;
  150. }