crypto_pwbox.c 6.5 KB

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