crypto.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /* Copyright (c) 2001, Matej Pfajfar.
  2. * Copyright (c) 2001-2004, Roger Dingledine.
  3. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  4. * Copyright (c) 2007-2018, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. /**
  7. * \file crypto.c
  8. * \brief Wrapper functions to present a consistent interface to
  9. * public-key and symmetric cryptography operations from OpenSSL and
  10. * other places.
  11. **/
  12. #include "orconfig.h"
  13. #ifdef _WIN32
  14. #include <winsock2.h>
  15. #include <windows.h>
  16. #include <wincrypt.h>
  17. /* Windows defines this; so does OpenSSL 0.9.8h and later. We don't actually
  18. * use either definition. */
  19. #undef OCSP_RESPONSE
  20. #endif /* defined(_WIN32) */
  21. #define CRYPTO_PRIVATE
  22. #include "lib/crypt_ops/compat_openssl.h"
  23. #include "lib/crypt_ops/crypto.h"
  24. #include "lib/crypt_ops/crypto_curve25519.h"
  25. #include "lib/crypt_ops/crypto_digest.h"
  26. #include "lib/crypt_ops/crypto_dh.h"
  27. #include "lib/crypt_ops/crypto_ed25519.h"
  28. #include "lib/crypt_ops/crypto_format.h"
  29. #include "lib/crypt_ops/crypto_rand.h"
  30. #include "lib/crypt_ops/crypto_rsa.h"
  31. #include "lib/crypt_ops/crypto_util.h"
  32. DISABLE_GCC_WARNING(redundant-decls)
  33. #include <openssl/err.h>
  34. #include <openssl/evp.h>
  35. #include <openssl/bn.h>
  36. #include <openssl/dh.h>
  37. #include <openssl/conf.h>
  38. #include <openssl/hmac.h>
  39. #include <openssl/ssl.h>
  40. ENABLE_GCC_WARNING(redundant-decls)
  41. #if __GNUC__ && GCC_VERSION >= 402
  42. #if GCC_VERSION >= 406
  43. #pragma GCC diagnostic pop
  44. #else
  45. #pragma GCC diagnostic warning "-Wredundant-decls"
  46. #endif
  47. #endif /* __GNUC__ && GCC_VERSION >= 402 */
  48. #ifdef HAVE_CTYPE_H
  49. #include <ctype.h>
  50. #endif
  51. #ifdef HAVE_UNISTD_H
  52. #include <unistd.h>
  53. #endif
  54. #include "lib/log/log.h"
  55. #include "lib/log/util_bug.h"
  56. #include "lib/cc/torint.h"
  57. #include "lib/crypt_ops/aes.h"
  58. #include "lib/encoding/binascii.h"
  59. #include "keccak-tiny/keccak-tiny.h"
  60. #include "siphash.h"
  61. #include <string.h>
  62. /** Allocate and return a new symmetric cipher using the provided key and iv.
  63. * The key is <b>bits</b> bits long; the IV is CIPHER_IV_LEN bytes. Both
  64. * must be provided. Key length must be 128, 192, or 256 */
  65. crypto_cipher_t *
  66. crypto_cipher_new_with_iv_and_bits(const uint8_t *key,
  67. const uint8_t *iv,
  68. int bits)
  69. {
  70. tor_assert(key);
  71. tor_assert(iv);
  72. return aes_new_cipher((const uint8_t*)key, (const uint8_t*)iv, bits);
  73. }
  74. /** Allocate and return a new symmetric cipher using the provided key and iv.
  75. * The key is CIPHER_KEY_LEN bytes; the IV is CIPHER_IV_LEN bytes. Both
  76. * must be provided.
  77. */
  78. crypto_cipher_t *
  79. crypto_cipher_new_with_iv(const char *key, const char *iv)
  80. {
  81. return crypto_cipher_new_with_iv_and_bits((uint8_t*)key, (uint8_t*)iv,
  82. 128);
  83. }
  84. /** Return a new crypto_cipher_t with the provided <b>key</b> and an IV of all
  85. * zero bytes and key length <b>bits</b>. Key length must be 128, 192, or
  86. * 256. */
  87. crypto_cipher_t *
  88. crypto_cipher_new_with_bits(const char *key, int bits)
  89. {
  90. char zeroiv[CIPHER_IV_LEN];
  91. memset(zeroiv, 0, sizeof(zeroiv));
  92. return crypto_cipher_new_with_iv_and_bits((uint8_t*)key, (uint8_t*)zeroiv,
  93. bits);
  94. }
  95. /** Return a new crypto_cipher_t with the provided <b>key</b> (of
  96. * CIPHER_KEY_LEN bytes) and an IV of all zero bytes. */
  97. crypto_cipher_t *
  98. crypto_cipher_new(const char *key)
  99. {
  100. return crypto_cipher_new_with_bits(key, 128);
  101. }
  102. /** Free a symmetric cipher.
  103. */
  104. void
  105. crypto_cipher_free_(crypto_cipher_t *env)
  106. {
  107. if (!env)
  108. return;
  109. aes_cipher_free(env);
  110. }
  111. /** Copy <b>in</b> to the <b>outlen</b>-byte buffer <b>out</b>, adding spaces
  112. * every four characters. */
  113. void
  114. crypto_add_spaces_to_fp(char *out, size_t outlen, const char *in)
  115. {
  116. int n = 0;
  117. char *end = out+outlen;
  118. tor_assert(outlen < SIZE_T_CEILING);
  119. while (*in && out<end) {
  120. *out++ = *in++;
  121. if (++n == 4 && *in && out<end) {
  122. n = 0;
  123. *out++ = ' ';
  124. }
  125. }
  126. tor_assert(out<end);
  127. *out = '\0';
  128. }
  129. /* symmetric crypto */
  130. /** Encrypt <b>fromlen</b> bytes from <b>from</b> using the cipher
  131. * <b>env</b>; on success, store the result to <b>to</b> and return 0.
  132. * Does not check for failure.
  133. */
  134. int
  135. crypto_cipher_encrypt(crypto_cipher_t *env, char *to,
  136. const char *from, size_t fromlen)
  137. {
  138. tor_assert(env);
  139. tor_assert(env);
  140. tor_assert(from);
  141. tor_assert(fromlen);
  142. tor_assert(to);
  143. tor_assert(fromlen < SIZE_T_CEILING);
  144. memcpy(to, from, fromlen);
  145. aes_crypt_inplace(env, to, fromlen);
  146. return 0;
  147. }
  148. /** Decrypt <b>fromlen</b> bytes from <b>from</b> using the cipher
  149. * <b>env</b>; on success, store the result to <b>to</b> and return 0.
  150. * Does not check for failure.
  151. */
  152. int
  153. crypto_cipher_decrypt(crypto_cipher_t *env, char *to,
  154. const char *from, size_t fromlen)
  155. {
  156. tor_assert(env);
  157. tor_assert(from);
  158. tor_assert(to);
  159. tor_assert(fromlen < SIZE_T_CEILING);
  160. memcpy(to, from, fromlen);
  161. aes_crypt_inplace(env, to, fromlen);
  162. return 0;
  163. }
  164. /** Encrypt <b>len</b> bytes on <b>from</b> using the cipher in <b>env</b>;
  165. * on success. Does not check for failure.
  166. */
  167. void
  168. crypto_cipher_crypt_inplace(crypto_cipher_t *env, char *buf, size_t len)
  169. {
  170. tor_assert(len < SIZE_T_CEILING);
  171. aes_crypt_inplace(env, buf, len);
  172. }
  173. /** Encrypt <b>fromlen</b> bytes (at least 1) from <b>from</b> with the key in
  174. * <b>key</b> to the buffer in <b>to</b> of length
  175. * <b>tolen</b>. <b>tolen</b> must be at least <b>fromlen</b> plus
  176. * CIPHER_IV_LEN bytes for the initialization vector. On success, return the
  177. * number of bytes written, on failure, return -1.
  178. */
  179. int
  180. crypto_cipher_encrypt_with_iv(const char *key,
  181. char *to, size_t tolen,
  182. const char *from, size_t fromlen)
  183. {
  184. crypto_cipher_t *cipher;
  185. tor_assert(from);
  186. tor_assert(to);
  187. tor_assert(fromlen < INT_MAX);
  188. if (fromlen < 1)
  189. return -1;
  190. if (tolen < fromlen + CIPHER_IV_LEN)
  191. return -1;
  192. char iv[CIPHER_IV_LEN];
  193. crypto_rand(iv, sizeof(iv));
  194. cipher = crypto_cipher_new_with_iv(key, iv);
  195. memcpy(to, iv, CIPHER_IV_LEN);
  196. crypto_cipher_encrypt(cipher, to+CIPHER_IV_LEN, from, fromlen);
  197. crypto_cipher_free(cipher);
  198. memwipe(iv, 0, sizeof(iv));
  199. return (int)(fromlen + CIPHER_IV_LEN);
  200. }
  201. /** Decrypt <b>fromlen</b> bytes (at least 1+CIPHER_IV_LEN) from <b>from</b>
  202. * with the key in <b>key</b> to the buffer in <b>to</b> of length
  203. * <b>tolen</b>. <b>tolen</b> must be at least <b>fromlen</b> minus
  204. * CIPHER_IV_LEN bytes for the initialization vector. On success, return the
  205. * number of bytes written, on failure, return -1.
  206. */
  207. int
  208. crypto_cipher_decrypt_with_iv(const char *key,
  209. char *to, size_t tolen,
  210. const char *from, size_t fromlen)
  211. {
  212. crypto_cipher_t *cipher;
  213. tor_assert(key);
  214. tor_assert(from);
  215. tor_assert(to);
  216. tor_assert(fromlen < INT_MAX);
  217. if (fromlen <= CIPHER_IV_LEN)
  218. return -1;
  219. if (tolen < fromlen - CIPHER_IV_LEN)
  220. return -1;
  221. cipher = crypto_cipher_new_with_iv(key, from);
  222. crypto_cipher_encrypt(cipher, to, from+CIPHER_IV_LEN, fromlen-CIPHER_IV_LEN);
  223. crypto_cipher_free(cipher);
  224. return (int)(fromlen - CIPHER_IV_LEN);
  225. }