crypto_curve25519.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /* Copyright (c) 2012, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /* Wrapper code for a curve25519 implementation. */
  4. #define CRYPTO_CURVE25519_PRIVATE
  5. #include "orconfig.h"
  6. #ifdef HAVE_SYS_STAT_H
  7. #include <sys/stat.h>
  8. #endif
  9. #include "crypto.h"
  10. #include "crypto_curve25519.h"
  11. #include "util.h"
  12. #include "torlog.h"
  13. /* ==============================
  14. Part 1: wrap a suitable curve25519 implementation as curve25519_impl
  15. ============================== */
  16. #ifdef USE_CURVE25519_DONNA
  17. int curve25519_donna(uint8_t *mypublic,
  18. const uint8_t *secret, const uint8_t *basepoint);
  19. #endif
  20. #ifdef USE_CURVE25519_NACL
  21. #include <crypto_scalarmult_curve25519.h>
  22. #endif
  23. int
  24. curve25519_impl(uint8_t *output, const uint8_t *secret,
  25. const uint8_t *basepoint)
  26. {
  27. #ifdef USE_CURVE25519_DONNA
  28. return curve25519_donna(output, secret, basepoint);
  29. #elif defined(USE_CURVE25519_NACL)
  30. return crypto_scalarmult_curve25519(output, secret, basepoint);
  31. #else
  32. #error "No implementation of curve25519 is available."
  33. #endif
  34. }
  35. /* ==============================
  36. Part 2: Wrap curve25519_impl with some convenience types and functions.
  37. ============================== */
  38. /**
  39. * Return true iff a curve25519_public_key_t seems valid. (It's not necessary
  40. * to see if the point is on the curve, since the twist is also secure, but we
  41. * do need to make sure that it isn't the point at infinity.) */
  42. int
  43. curve25519_public_key_is_ok(const curve25519_public_key_t *key)
  44. {
  45. return !safe_mem_is_zero(key->public_key, CURVE25519_PUBKEY_LEN);
  46. }
  47. /** Generate a new keypair and return the secret key. If <b>extra_strong</b>
  48. * is true, this key is possibly going to get used more than once, so
  49. * use a better-than-usual RNG. */
  50. void
  51. curve25519_secret_key_generate(curve25519_secret_key_t *key_out,
  52. int extra_strong)
  53. {
  54. uint8_t k_tmp[CURVE25519_SECKEY_LEN];
  55. crypto_rand((char*)key_out->secret_key, CURVE25519_SECKEY_LEN);
  56. if (extra_strong && !crypto_strongest_rand(k_tmp, CURVE25519_SECKEY_LEN)) {
  57. /* If they asked for extra-strong entropy and we have some, use it as an
  58. * HMAC key to improve not-so-good entopy rather than using it directly,
  59. * just in case the extra-strong entropy is less amazing than we hoped. */
  60. crypto_hmac_sha256((char *)key_out->secret_key,
  61. (const char *)k_tmp, sizeof(k_tmp),
  62. (const char *)key_out->secret_key, CURVE25519_SECKEY_LEN);
  63. }
  64. memwipe(k_tmp, 0, sizeof(k_tmp));
  65. key_out->secret_key[0] &= 248;
  66. key_out->secret_key[31] &= 127;
  67. key_out->secret_key[31] |= 64;
  68. }
  69. void
  70. curve25519_public_key_generate(curve25519_public_key_t *key_out,
  71. const curve25519_secret_key_t *seckey)
  72. {
  73. static const uint8_t basepoint[32] = {9};
  74. curve25519_impl(key_out->public_key, seckey->secret_key, basepoint);
  75. }
  76. void
  77. curve25519_keypair_generate(curve25519_keypair_t *keypair_out,
  78. int extra_strong)
  79. {
  80. curve25519_secret_key_generate(&keypair_out->seckey, extra_strong);
  81. curve25519_public_key_generate(&keypair_out->pubkey, &keypair_out->seckey);
  82. }
  83. int
  84. curve25519_keypair_write_to_file(const curve25519_keypair_t *keypair,
  85. const char *fname,
  86. const char *tag)
  87. {
  88. char contents[32 + CURVE25519_SECKEY_LEN + CURVE25519_PUBKEY_LEN];
  89. int r;
  90. memset(contents, 0, sizeof(contents));
  91. tor_snprintf(contents, sizeof(contents), "== c25519v1: %s ==", tag);
  92. tor_assert(strlen(contents) <= 32);
  93. memcpy(contents+32, keypair->seckey.secret_key, CURVE25519_SECKEY_LEN);
  94. memcpy(contents+32+CURVE25519_SECKEY_LEN,
  95. keypair->pubkey.public_key, CURVE25519_PUBKEY_LEN);
  96. r = write_bytes_to_file(fname, contents, sizeof(contents), 1);
  97. memwipe(contents, 0, sizeof(contents));
  98. return r;
  99. }
  100. int
  101. curve25519_keypair_read_from_file(curve25519_keypair_t *keypair_out,
  102. char **tag_out,
  103. const char *fname)
  104. {
  105. char prefix[33];
  106. char *content;
  107. struct stat st;
  108. int r = -1;
  109. *tag_out = NULL;
  110. st.st_size = 0;
  111. content = read_file_to_str(fname, RFTS_BIN|RFTS_IGNORE_MISSING, &st);
  112. if (! content)
  113. goto end;
  114. if (st.st_size != 32 + CURVE25519_SECKEY_LEN + CURVE25519_PUBKEY_LEN)
  115. goto end;
  116. memcpy(prefix, content, 32);
  117. prefix[32] = '\0';
  118. if (strcmpstart(prefix, "== c25519v1: ") ||
  119. strcmpend(prefix, " =="))
  120. goto end;
  121. *tag_out = tor_strndup(prefix+strlen("== c25519v1: "),
  122. strlen(prefix) - strlen("== c25519v1: =="));
  123. memcpy(keypair_out->seckey.secret_key, content+32, CURVE25519_SECKEY_LEN);
  124. curve25519_public_key_generate(&keypair_out->pubkey, &keypair_out->seckey);
  125. if (tor_memneq(keypair_out->pubkey.public_key,
  126. content + 32 + CURVE25519_SECKEY_LEN,
  127. CURVE25519_PUBKEY_LEN))
  128. goto end;
  129. r = 0;
  130. end:
  131. if (content) {
  132. memwipe(content, 0, st.st_size);
  133. tor_free(content);
  134. }
  135. if (r != 0) {
  136. memset(keypair_out, 0, sizeof(*keypair_out));
  137. tor_free(*tag_out);
  138. }
  139. return r;
  140. }
  141. /** Perform the curve25519 ECDH handshake with <b>skey</b> and <b>pkey</b>,
  142. * writing CURVE25519_OUTPUT_LEN bytes of output into <b>output</b>. */
  143. void
  144. curve25519_handshake(uint8_t *output,
  145. const curve25519_secret_key_t *skey,
  146. const curve25519_public_key_t *pkey)
  147. {
  148. curve25519_impl(output, skey->secret_key, pkey->public_key);
  149. }