crypto_curve25519.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /* Copyright (c) 2012-2015, 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. /** Write the <b>datalen</b> bytes from <b>data</b> to the file named
  116. * <b>fname</b> in the tagged-data format. This format contains a
  117. * 32-byte header, followed by the data itself. The header is the
  118. * NUL-padded string "== <b>typestring</b>: <b>tag</b> ==". The length
  119. * of <b>typestring</b> and <b>tag</b> must therefore be no more than
  120. * 24.
  121. **/
  122. int
  123. crypto_write_tagged_contents_to_file(const char *fname,
  124. const char *typestring,
  125. const char *tag,
  126. const uint8_t *data,
  127. size_t datalen)
  128. {
  129. char header[32];
  130. smartlist_t *chunks = smartlist_new();
  131. sized_chunk_t ch0, ch1;
  132. int r = -1;
  133. memset(header, 0, sizeof(header));
  134. if (tor_snprintf(header, sizeof(header),
  135. "== %s: %s ==", typestring, tag) < 0)
  136. goto end;
  137. ch0.bytes = header;
  138. ch0.len = 32;
  139. ch1.bytes = (const char*) data;
  140. ch1.len = datalen;
  141. smartlist_add(chunks, &ch0);
  142. smartlist_add(chunks, &ch1);
  143. r = write_chunks_to_file(fname, chunks, 1, 0);
  144. end:
  145. smartlist_free(chunks);
  146. return r;
  147. }
  148. /** Read a tagged-data file from <b>fname</b> into the
  149. * <b>data_out_len</b>-byte buffer in <b>data_out</b>. Check that the
  150. * typestring matches <b>typestring</b>; store the tag into a newly allocated
  151. * string in <b>tag_out</b>. Return -1 on failure, and the number of bytes of
  152. * data on success. */
  153. ssize_t
  154. crypto_read_tagged_contents_from_file(const char *fname,
  155. const char *typestring,
  156. char **tag_out,
  157. uint8_t *data_out,
  158. ssize_t data_out_len)
  159. {
  160. char prefix[33];
  161. char *content = NULL;
  162. struct stat st;
  163. ssize_t r = -1;
  164. size_t st_size = 0;
  165. *tag_out = NULL;
  166. st.st_size = 0;
  167. content = read_file_to_str(fname, RFTS_BIN|RFTS_IGNORE_MISSING, &st);
  168. if (! content)
  169. goto end;
  170. if (st.st_size < 32 || st.st_size > 32 + data_out_len)
  171. goto end;
  172. st_size = (size_t)st.st_size;
  173. memcpy(prefix, content, 32);
  174. prefix[32] = 0;
  175. /* Check type, extract tag. */
  176. if (strcmpstart(prefix, "== ") || strcmpend(prefix, " ==") ||
  177. ! tor_mem_is_zero(prefix+strlen(prefix), 32-strlen(prefix)))
  178. goto end;
  179. if (strcmpstart(prefix+3, typestring) ||
  180. 3+strlen(typestring) >= 32 ||
  181. strcmpstart(prefix+3+strlen(typestring), ": "))
  182. goto end;
  183. *tag_out = tor_strndup(prefix+5+strlen(typestring),
  184. strlen(prefix)-8-strlen(typestring));
  185. memcpy(data_out, content+32, st_size-32);
  186. r = st_size - 32;
  187. end:
  188. if (content)
  189. memwipe(content, 0, st_size);
  190. tor_free(content);
  191. return r;
  192. }
  193. /** DOCDOC */
  194. int
  195. curve25519_keypair_write_to_file(const curve25519_keypair_t *keypair,
  196. const char *fname,
  197. const char *tag)
  198. {
  199. uint8_t contents[CURVE25519_SECKEY_LEN + CURVE25519_PUBKEY_LEN];
  200. int r;
  201. memcpy(contents, keypair->seckey.secret_key, CURVE25519_SECKEY_LEN);
  202. memcpy(contents+CURVE25519_SECKEY_LEN,
  203. keypair->pubkey.public_key, CURVE25519_PUBKEY_LEN);
  204. r = crypto_write_tagged_contents_to_file(fname,
  205. "c25519v1",
  206. tag,
  207. contents,
  208. sizeof(contents));
  209. memwipe(contents, 0, sizeof(contents));
  210. return r;
  211. }
  212. /** DOCDOC */
  213. int
  214. curve25519_keypair_read_from_file(curve25519_keypair_t *keypair_out,
  215. char **tag_out,
  216. const char *fname)
  217. {
  218. uint8_t content[CURVE25519_SECKEY_LEN + CURVE25519_PUBKEY_LEN];
  219. ssize_t len;
  220. int r = -1;
  221. len = crypto_read_tagged_contents_from_file(fname, "c25519v1", tag_out,
  222. content, sizeof(content));
  223. if (len != sizeof(content))
  224. goto end;
  225. memcpy(keypair_out->seckey.secret_key, content, CURVE25519_SECKEY_LEN);
  226. curve25519_public_key_generate(&keypair_out->pubkey, &keypair_out->seckey);
  227. if (tor_memneq(keypair_out->pubkey.public_key,
  228. content + CURVE25519_SECKEY_LEN,
  229. CURVE25519_PUBKEY_LEN))
  230. goto end;
  231. r = 0;
  232. end:
  233. memwipe(content, 0, sizeof(content));
  234. if (r != 0) {
  235. memset(keypair_out, 0, sizeof(*keypair_out));
  236. tor_free(*tag_out);
  237. }
  238. return r;
  239. }
  240. /** Perform the curve25519 ECDH handshake with <b>skey</b> and <b>pkey</b>,
  241. * writing CURVE25519_OUTPUT_LEN bytes of output into <b>output</b>. */
  242. void
  243. curve25519_handshake(uint8_t *output,
  244. const curve25519_secret_key_t *skey,
  245. const curve25519_public_key_t *pkey)
  246. {
  247. curve25519_impl(output, skey->secret_key, pkey->public_key);
  248. }