crypto_curve25519.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /* Copyright (c) 2012-2013, 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 "container.h"
  10. #include "crypto.h"
  11. #include "crypto_curve25519.h"
  12. #include "util.h"
  13. #include "torlog.h"
  14. /* ==============================
  15. Part 1: wrap a suitable curve25519 implementation as curve25519_impl
  16. ============================== */
  17. #ifdef USE_CURVE25519_DONNA
  18. int curve25519_donna(uint8_t *mypublic,
  19. const uint8_t *secret, const uint8_t *basepoint);
  20. #endif
  21. #ifdef USE_CURVE25519_NACL
  22. #ifdef HAVE_CRYPTO_SCALARMULT_CURVE25519_H
  23. #include <crypto_scalarmult_curve25519.h>
  24. #elif defined(HAVE_NACL_CRYPTO_SCALARMULT_CURVE25519_H)
  25. #include <nacl/crypto_scalarmult_curve25519.h>
  26. #endif
  27. #endif
  28. STATIC int
  29. curve25519_impl(uint8_t *output, const uint8_t *secret,
  30. const uint8_t *basepoint)
  31. {
  32. uint8_t bp[CURVE25519_PUBKEY_LEN];
  33. int r;
  34. memcpy(bp, basepoint, CURVE25519_PUBKEY_LEN);
  35. /* Clear the high bit, in case our backend foolishly looks at it. */
  36. bp[31] &= 0x7f;
  37. #ifdef USE_CURVE25519_DONNA
  38. r = curve25519_donna(output, secret, bp);
  39. #elif defined(USE_CURVE25519_NACL)
  40. r = crypto_scalarmult_curve25519(output, secret, bp);
  41. #else
  42. #error "No implementation of curve25519 is available."
  43. #endif
  44. memwipe(bp, 0, sizeof(bp));
  45. return r;
  46. }
  47. /* ==============================
  48. Part 2: Wrap curve25519_impl with some convenience types and functions.
  49. ============================== */
  50. /**
  51. * Return true iff a curve25519_public_key_t seems valid. (It's not necessary
  52. * to see if the point is on the curve, since the twist is also secure, but we
  53. * do need to make sure that it isn't the point at infinity.) */
  54. int
  55. curve25519_public_key_is_ok(const curve25519_public_key_t *key)
  56. {
  57. return !safe_mem_is_zero(key->public_key, CURVE25519_PUBKEY_LEN);
  58. }
  59. /**
  60. * Generate CURVE25519_SECKEY_LEN random bytes in <b>out</b>. If
  61. * <b>extra_strong</b> is true, this key is possibly going to get used more
  62. * than once, so use a better-than-usual RNG. Return 0 on success, -1 on
  63. * failure.
  64. *
  65. * This function does not adjust the output of the RNG at all; the will caller
  66. * will need to clear or set the appropriate bits to make curve25519 work.
  67. */
  68. int
  69. curve25519_rand_seckey_bytes(uint8_t *out, int extra_strong)
  70. {
  71. uint8_t k_tmp[CURVE25519_SECKEY_LEN];
  72. if (crypto_rand((char*)out, CURVE25519_SECKEY_LEN) < 0)
  73. return -1;
  74. if (extra_strong && !crypto_strongest_rand(k_tmp, CURVE25519_SECKEY_LEN)) {
  75. /* If they asked for extra-strong entropy and we have some, use it as an
  76. * HMAC key to improve not-so-good entropy rather than using it directly,
  77. * just in case the extra-strong entropy is less amazing than we hoped. */
  78. crypto_hmac_sha256((char*) out,
  79. (const char *)k_tmp, sizeof(k_tmp),
  80. (const char *)out, CURVE25519_SECKEY_LEN);
  81. }
  82. memwipe(k_tmp, 0, sizeof(k_tmp));
  83. return 0;
  84. }
  85. /** Generate a new keypair and return the secret key. If <b>extra_strong</b>
  86. * is true, this key is possibly going to get used more than once, so
  87. * use a better-than-usual RNG. Return 0 on success, -1 on failure. */
  88. int
  89. curve25519_secret_key_generate(curve25519_secret_key_t *key_out,
  90. int extra_strong)
  91. {
  92. if (curve25519_rand_seckey_bytes(key_out->secret_key, extra_strong) < 0)
  93. return -1;
  94. key_out->secret_key[0] &= 248;
  95. key_out->secret_key[31] &= 127;
  96. key_out->secret_key[31] |= 64;
  97. return 0;
  98. }
  99. void
  100. curve25519_public_key_generate(curve25519_public_key_t *key_out,
  101. const curve25519_secret_key_t *seckey)
  102. {
  103. static const uint8_t basepoint[32] = {9};
  104. curve25519_impl(key_out->public_key, seckey->secret_key, basepoint);
  105. }
  106. int
  107. curve25519_keypair_generate(curve25519_keypair_t *keypair_out,
  108. int extra_strong)
  109. {
  110. if (curve25519_secret_key_generate(&keypair_out->seckey, extra_strong) < 0)
  111. return -1;
  112. curve25519_public_key_generate(&keypair_out->pubkey, &keypair_out->seckey);
  113. return 0;
  114. }
  115. /** DOCDOC */
  116. int
  117. crypto_write_tagged_contents_to_file(const char *fname,
  118. const char *typestring,
  119. const char *tag,
  120. const uint8_t *data,
  121. size_t datalen)
  122. {
  123. char header[32];
  124. smartlist_t *chunks = smartlist_new();
  125. sized_chunk_t ch0, ch1;
  126. int r = -1;
  127. memset(header, 0, sizeof(header));
  128. if (tor_snprintf(header, sizeof(header),
  129. "== %s: %s ==", typestring, tag) < 0)
  130. goto end;
  131. ch0.bytes = header;
  132. ch0.len = 32;
  133. ch1.bytes = (const char*) data;
  134. ch1.len = datalen;
  135. smartlist_add(chunks, &ch0);
  136. smartlist_add(chunks, &ch1);
  137. r = write_chunks_to_file(fname, chunks, 1, 0);
  138. end:
  139. smartlist_free(chunks);
  140. return r;
  141. }
  142. /** DOCDOC */
  143. ssize_t
  144. crypto_read_tagged_contents_from_file(const char *fname,
  145. const char *typestring,
  146. char **tag_out,
  147. uint8_t *data_out,
  148. ssize_t data_out_len)
  149. {
  150. char prefix[33];
  151. char *content = NULL;
  152. struct stat st;
  153. ssize_t r = -1;
  154. *tag_out = NULL;
  155. st.st_size = 0;
  156. content = read_file_to_str(fname, RFTS_BIN|RFTS_IGNORE_MISSING, &st);
  157. if (! content)
  158. goto end;
  159. if (st.st_size < 32 || st.st_size > 32 + data_out_len)
  160. goto end;
  161. memcpy(prefix, content, 32);
  162. prefix[32] = 0;
  163. /* Check type, extract tag. */
  164. if (strcmpstart(prefix, "== ") || strcmpend(prefix, " ==") ||
  165. ! tor_mem_is_zero(prefix+strlen(prefix), 32-strlen(prefix)))
  166. goto end;
  167. if (strcmpstart(prefix+3, typestring) ||
  168. 3+strlen(typestring) >= 32 ||
  169. strcmpstart(prefix+3+strlen(typestring), ": "))
  170. goto end;
  171. *tag_out = tor_strndup(prefix+5+strlen(typestring),
  172. strlen(prefix)-8-strlen(typestring));
  173. memcpy(data_out, content+32, st.st_size-32);
  174. r = st.st_size - 32;
  175. end:
  176. if (content)
  177. memwipe(content, 0, st.st_size);
  178. tor_free(content);
  179. return r;
  180. }
  181. /** DOCDOC */
  182. int
  183. curve25519_keypair_write_to_file(const curve25519_keypair_t *keypair,
  184. const char *fname,
  185. const char *tag)
  186. {
  187. uint8_t contents[CURVE25519_SECKEY_LEN + CURVE25519_PUBKEY_LEN];
  188. int r;
  189. memcpy(contents, keypair->seckey.secret_key, CURVE25519_SECKEY_LEN);
  190. memcpy(contents+CURVE25519_SECKEY_LEN,
  191. keypair->pubkey.public_key, CURVE25519_PUBKEY_LEN);
  192. r = crypto_write_tagged_contents_to_file(fname,
  193. "c25519v1",
  194. tag,
  195. contents,
  196. sizeof(contents));
  197. memwipe(contents, 0, sizeof(contents));
  198. return r;
  199. }
  200. /** DOCDOC */
  201. int
  202. curve25519_keypair_read_from_file(curve25519_keypair_t *keypair_out,
  203. char **tag_out,
  204. const char *fname)
  205. {
  206. uint8_t content[CURVE25519_SECKEY_LEN + CURVE25519_PUBKEY_LEN];
  207. ssize_t len;
  208. int r = -1;
  209. len = crypto_read_tagged_contents_from_file(fname, "c25519v1", tag_out,
  210. content, sizeof(content));
  211. if (len != sizeof(content))
  212. goto end;
  213. memcpy(keypair_out->seckey.secret_key, content, CURVE25519_SECKEY_LEN);
  214. curve25519_public_key_generate(&keypair_out->pubkey, &keypair_out->seckey);
  215. if (tor_memneq(keypair_out->pubkey.public_key,
  216. content + CURVE25519_SECKEY_LEN,
  217. CURVE25519_PUBKEY_LEN))
  218. goto end;
  219. r = 0;
  220. end:
  221. memwipe(content, 0, sizeof(content));
  222. if (r != 0) {
  223. memset(keypair_out, 0, sizeof(*keypair_out));
  224. tor_free(*tag_out);
  225. }
  226. return r;
  227. }
  228. /** Perform the curve25519 ECDH handshake with <b>skey</b> and <b>pkey</b>,
  229. * writing CURVE25519_OUTPUT_LEN bytes of output into <b>output</b>. */
  230. void
  231. curve25519_handshake(uint8_t *output,
  232. const curve25519_secret_key_t *skey,
  233. const curve25519_public_key_t *pkey)
  234. {
  235. curve25519_impl(output, skey->secret_key, pkey->public_key);
  236. }