crypto_pwbox.c 5.7 KB

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