crypto.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. #include "lib/crypt_ops/crypto.h"
  14. #include "lib/crypt_ops/crypto_rand.h"
  15. #include "lib/crypt_ops/crypto_util.h"
  16. #include "lib/log/log.h"
  17. #include "lib/log/util_bug.h"
  18. #include "lib/cc/torint.h"
  19. #include "lib/crypt_ops/aes.h"
  20. #include <string.h>
  21. /** Allocate and return a new symmetric cipher using the provided key and iv.
  22. * The key is <b>bits</b> bits long; the IV is CIPHER_IV_LEN bytes. Both
  23. * must be provided. Key length must be 128, 192, or 256 */
  24. crypto_cipher_t *
  25. crypto_cipher_new_with_iv_and_bits(const uint8_t *key,
  26. const uint8_t *iv,
  27. int bits)
  28. {
  29. tor_assert(key);
  30. tor_assert(iv);
  31. return aes_new_cipher((const uint8_t*)key, (const uint8_t*)iv, bits);
  32. }
  33. /** Allocate and return a new symmetric cipher using the provided key and iv.
  34. * The key is CIPHER_KEY_LEN bytes; the IV is CIPHER_IV_LEN bytes. Both
  35. * must be provided.
  36. */
  37. crypto_cipher_t *
  38. crypto_cipher_new_with_iv(const char *key, const char *iv)
  39. {
  40. return crypto_cipher_new_with_iv_and_bits((uint8_t*)key, (uint8_t*)iv,
  41. 128);
  42. }
  43. /** Return a new crypto_cipher_t with the provided <b>key</b> and an IV of all
  44. * zero bytes and key length <b>bits</b>. Key length must be 128, 192, or
  45. * 256. */
  46. crypto_cipher_t *
  47. crypto_cipher_new_with_bits(const char *key, int bits)
  48. {
  49. char zeroiv[CIPHER_IV_LEN];
  50. memset(zeroiv, 0, sizeof(zeroiv));
  51. return crypto_cipher_new_with_iv_and_bits((uint8_t*)key, (uint8_t*)zeroiv,
  52. bits);
  53. }
  54. /** Return a new crypto_cipher_t with the provided <b>key</b> (of
  55. * CIPHER_KEY_LEN bytes) and an IV of all zero bytes. */
  56. crypto_cipher_t *
  57. crypto_cipher_new(const char *key)
  58. {
  59. return crypto_cipher_new_with_bits(key, 128);
  60. }
  61. /** Free a symmetric cipher.
  62. */
  63. void
  64. crypto_cipher_free_(crypto_cipher_t *env)
  65. {
  66. if (!env)
  67. return;
  68. aes_cipher_free(env);
  69. }
  70. /* symmetric crypto */
  71. /** Encrypt <b>fromlen</b> bytes from <b>from</b> using the cipher
  72. * <b>env</b>; on success, store the result to <b>to</b> and return 0.
  73. * Does not check for failure.
  74. */
  75. int
  76. crypto_cipher_encrypt(crypto_cipher_t *env, char *to,
  77. const char *from, size_t fromlen)
  78. {
  79. tor_assert(env);
  80. tor_assert(env);
  81. tor_assert(from);
  82. tor_assert(fromlen);
  83. tor_assert(to);
  84. tor_assert(fromlen < SIZE_T_CEILING);
  85. memcpy(to, from, fromlen);
  86. aes_crypt_inplace(env, to, fromlen);
  87. return 0;
  88. }
  89. /** Decrypt <b>fromlen</b> bytes from <b>from</b> using the cipher
  90. * <b>env</b>; on success, store the result to <b>to</b> and return 0.
  91. * Does not check for failure.
  92. */
  93. int
  94. crypto_cipher_decrypt(crypto_cipher_t *env, char *to,
  95. const char *from, size_t fromlen)
  96. {
  97. tor_assert(env);
  98. tor_assert(from);
  99. tor_assert(to);
  100. tor_assert(fromlen < SIZE_T_CEILING);
  101. memcpy(to, from, fromlen);
  102. aes_crypt_inplace(env, to, fromlen);
  103. return 0;
  104. }
  105. /** Encrypt <b>len</b> bytes on <b>from</b> using the cipher in <b>env</b>;
  106. * on success. Does not check for failure.
  107. */
  108. void
  109. crypto_cipher_crypt_inplace(crypto_cipher_t *env, char *buf, size_t len)
  110. {
  111. tor_assert(len < SIZE_T_CEILING);
  112. aes_crypt_inplace(env, buf, len);
  113. }
  114. /** Encrypt <b>fromlen</b> bytes (at least 1) from <b>from</b> with the key in
  115. * <b>key</b> to the buffer in <b>to</b> of length
  116. * <b>tolen</b>. <b>tolen</b> must be at least <b>fromlen</b> plus
  117. * CIPHER_IV_LEN bytes for the initialization vector. On success, return the
  118. * number of bytes written, on failure, return -1.
  119. */
  120. int
  121. crypto_cipher_encrypt_with_iv(const char *key,
  122. char *to, size_t tolen,
  123. const char *from, size_t fromlen)
  124. {
  125. crypto_cipher_t *cipher;
  126. tor_assert(from);
  127. tor_assert(to);
  128. tor_assert(fromlen < INT_MAX);
  129. if (fromlen < 1)
  130. return -1;
  131. if (tolen < fromlen + CIPHER_IV_LEN)
  132. return -1;
  133. char iv[CIPHER_IV_LEN];
  134. crypto_rand(iv, sizeof(iv));
  135. cipher = crypto_cipher_new_with_iv(key, iv);
  136. memcpy(to, iv, CIPHER_IV_LEN);
  137. crypto_cipher_encrypt(cipher, to+CIPHER_IV_LEN, from, fromlen);
  138. crypto_cipher_free(cipher);
  139. memwipe(iv, 0, sizeof(iv));
  140. return (int)(fromlen + CIPHER_IV_LEN);
  141. }
  142. /** Decrypt <b>fromlen</b> bytes (at least 1+CIPHER_IV_LEN) from <b>from</b>
  143. * with the key in <b>key</b> to the buffer in <b>to</b> of length
  144. * <b>tolen</b>. <b>tolen</b> must be at least <b>fromlen</b> minus
  145. * CIPHER_IV_LEN bytes for the initialization vector. On success, return the
  146. * number of bytes written, on failure, return -1.
  147. */
  148. int
  149. crypto_cipher_decrypt_with_iv(const char *key,
  150. char *to, size_t tolen,
  151. const char *from, size_t fromlen)
  152. {
  153. crypto_cipher_t *cipher;
  154. tor_assert(key);
  155. tor_assert(from);
  156. tor_assert(to);
  157. tor_assert(fromlen < INT_MAX);
  158. if (fromlen <= CIPHER_IV_LEN)
  159. return -1;
  160. if (tolen < fromlen - CIPHER_IV_LEN)
  161. return -1;
  162. cipher = crypto_cipher_new_with_iv(key, from);
  163. crypto_cipher_encrypt(cipher, to, from+CIPHER_IV_LEN, fromlen-CIPHER_IV_LEN);
  164. crypto_cipher_free(cipher);
  165. return (int)(fromlen - CIPHER_IV_LEN);
  166. }