crypto_pwbox.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /* Copyright (c) 2014-2018, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file crypto_pwbox.c
  5. *
  6. * \brief Code for encrypting secrets in a password-protected form and saving
  7. * them to disk.
  8. */
  9. #include "lib/crypt_ops/crypto.h"
  10. #include "lib/crypt_ops/crypto_digest.h"
  11. #include "lib/crypt_ops/crypto_pwbox.h"
  12. #include "lib/crypt_ops/crypto_rand.h"
  13. #include "lib/crypt_ops/crypto_s2k.h"
  14. #include "lib/crypt_ops/crypto_util.h"
  15. #include "lib/ctime/di_ops.h"
  16. #include "lib/intmath/muldiv.h"
  17. #include "common/util.h"
  18. #include "trunnel/pwbox.h"
  19. /* 8 bytes "TORBOX00"
  20. 1 byte: header len (H)
  21. H bytes: header, denoting secret key algorithm.
  22. 16 bytes: IV
  23. Round up to multiple of 128 bytes, then encrypt:
  24. 4 bytes: data len
  25. data
  26. zeros
  27. 32 bytes: HMAC-SHA256 of all previous bytes.
  28. */
  29. #define MAX_OVERHEAD (S2K_MAXLEN + 8 + 1 + 32 + CIPHER_IV_LEN)
  30. /**
  31. * Make an authenticated passphrase-encrypted blob to encode the
  32. * <b>input_len</b> bytes in <b>input</b> using the passphrase
  33. * <b>secret</b> of <b>secret_len</b> bytes. Allocate a new chunk of memory
  34. * to hold the encrypted data, and store a pointer to that memory in
  35. * *<b>out</b>, and its size in <b>outlen_out</b>. Use <b>s2k_flags</b> as an
  36. * argument to the passphrase-hashing function.
  37. */
  38. int
  39. crypto_pwbox(uint8_t **out, size_t *outlen_out,
  40. const uint8_t *input, size_t input_len,
  41. const char *secret, size_t secret_len,
  42. unsigned s2k_flags)
  43. {
  44. uint8_t *result = NULL, *encrypted_portion;
  45. size_t encrypted_len = 128 * CEIL_DIV(input_len+4, 128);
  46. ssize_t result_len;
  47. int spec_len;
  48. uint8_t keys[CIPHER_KEY_LEN + DIGEST256_LEN];
  49. pwbox_encoded_t *enc = NULL;
  50. ssize_t enc_len;
  51. crypto_cipher_t *cipher;
  52. int rv;
  53. enc = pwbox_encoded_new();
  54. pwbox_encoded_setlen_skey_header(enc, S2K_MAXLEN);
  55. spec_len = secret_to_key_make_specifier(
  56. pwbox_encoded_getarray_skey_header(enc),
  57. S2K_MAXLEN,
  58. s2k_flags);
  59. if (BUG(spec_len < 0 || spec_len > S2K_MAXLEN))
  60. goto err;
  61. pwbox_encoded_setlen_skey_header(enc, spec_len);
  62. enc->header_len = spec_len;
  63. crypto_rand((char*)enc->iv, sizeof(enc->iv));
  64. pwbox_encoded_setlen_data(enc, encrypted_len);
  65. encrypted_portion = pwbox_encoded_getarray_data(enc);
  66. set_uint32(encrypted_portion, htonl((uint32_t)input_len));
  67. memcpy(encrypted_portion+4, input, input_len);
  68. /* Now that all the data is in position, derive some keys, encrypt, and
  69. * digest */
  70. const int s2k_rv = secret_to_key_derivekey(keys, sizeof(keys),
  71. pwbox_encoded_getarray_skey_header(enc),
  72. spec_len,
  73. secret, secret_len);
  74. if (BUG(s2k_rv < 0))
  75. goto err;
  76. cipher = crypto_cipher_new_with_iv((char*)keys, (char*)enc->iv);
  77. crypto_cipher_crypt_inplace(cipher, (char*)encrypted_portion, encrypted_len);
  78. crypto_cipher_free(cipher);
  79. result_len = pwbox_encoded_encoded_len(enc);
  80. if (BUG(result_len < 0))
  81. goto err;
  82. result = tor_malloc(result_len);
  83. enc_len = pwbox_encoded_encode(result, result_len, enc);
  84. if (BUG(enc_len < 0))
  85. goto err;
  86. tor_assert(enc_len == result_len);
  87. crypto_hmac_sha256((char*) result + result_len - 32,
  88. (const char*)keys + CIPHER_KEY_LEN,
  89. DIGEST256_LEN,
  90. (const char*)result,
  91. result_len - 32);
  92. *out = result;
  93. *outlen_out = result_len;
  94. rv = 0;
  95. goto out;
  96. /* LCOV_EXCL_START
  97. This error case is often unreachable if we're correctly coded, unless
  98. somebody adds a new error case somewhere, or unless you're building
  99. without scrypto support.
  100. - make_specifier can't fail, unless S2K_MAX_LEN is too short.
  101. - secret_to_key_derivekey can't really fail unless we're missing
  102. scrypt, or the underlying function fails, or we pass it a bogus
  103. algorithm or parameters.
  104. - pwbox_encoded_encoded_len can't fail unless we're using trunnel
  105. incorrectly.
  106. - pwbox_encoded_encode can't fail unless we're using trunnel wrong,
  107. or it's buggy.
  108. */
  109. err:
  110. tor_free(result);
  111. rv = -1;
  112. /* LCOV_EXCL_STOP */
  113. out:
  114. pwbox_encoded_free(enc);
  115. memwipe(keys, 0, sizeof(keys));
  116. return rv;
  117. }
  118. /**
  119. * Try to decrypt the passphrase-encrypted blob of <b>input_len</b> bytes in
  120. * <b>input</b> using the passphrase <b>secret</b> of <b>secret_len</b> bytes.
  121. * On success, return 0 and allocate a new chunk of memory to hold the
  122. * decrypted data, and store a pointer to that memory in *<b>out</b>, and its
  123. * size in <b>outlen_out</b>. On failure, return UNPWBOX_BAD_SECRET if
  124. * the passphrase might have been wrong, and UNPWBOX_CORRUPT if the object is
  125. * definitely corrupt.
  126. */
  127. int
  128. crypto_unpwbox(uint8_t **out, size_t *outlen_out,
  129. const uint8_t *inp, size_t input_len,
  130. const char *secret, size_t secret_len)
  131. {
  132. uint8_t *result = NULL;
  133. const uint8_t *encrypted;
  134. uint8_t keys[CIPHER_KEY_LEN + DIGEST256_LEN];
  135. uint8_t hmac[DIGEST256_LEN];
  136. uint32_t result_len;
  137. size_t encrypted_len;
  138. crypto_cipher_t *cipher = NULL;
  139. int rv = UNPWBOX_CORRUPTED;
  140. ssize_t got_len;
  141. pwbox_encoded_t *enc = NULL;
  142. got_len = pwbox_encoded_parse(&enc, inp, input_len);
  143. if (got_len < 0 || (size_t)got_len != input_len)
  144. goto err;
  145. /* Now derive the keys and check the hmac. */
  146. if (secret_to_key_derivekey(keys, sizeof(keys),
  147. pwbox_encoded_getarray_skey_header(enc),
  148. pwbox_encoded_getlen_skey_header(enc),
  149. secret, secret_len) < 0)
  150. goto err;
  151. crypto_hmac_sha256((char *)hmac,
  152. (const char*)keys + CIPHER_KEY_LEN, DIGEST256_LEN,
  153. (const char*)inp, input_len - DIGEST256_LEN);
  154. if (tor_memneq(hmac, enc->hmac, DIGEST256_LEN)) {
  155. rv = UNPWBOX_BAD_SECRET;
  156. goto err;
  157. }
  158. /* How long is the plaintext? */
  159. encrypted = pwbox_encoded_getarray_data(enc);
  160. encrypted_len = pwbox_encoded_getlen_data(enc);
  161. if (encrypted_len < 4)
  162. goto err;
  163. cipher = crypto_cipher_new_with_iv((char*)keys, (char*)enc->iv);
  164. crypto_cipher_decrypt(cipher, (char*)&result_len, (char*)encrypted, 4);
  165. result_len = ntohl(result_len);
  166. if (encrypted_len < result_len + 4)
  167. goto err;
  168. /* Allocate a buffer and decrypt */
  169. result = tor_malloc_zero(result_len);
  170. crypto_cipher_decrypt(cipher, (char*)result, (char*)encrypted+4, result_len);
  171. *out = result;
  172. *outlen_out = result_len;
  173. rv = UNPWBOX_OKAY;
  174. goto out;
  175. err:
  176. tor_free(result);
  177. out:
  178. crypto_cipher_free(cipher);
  179. pwbox_encoded_free(enc);
  180. memwipe(keys, 0, sizeof(keys));
  181. return rv;
  182. }