crypto_pwbox.c 5.1 KB

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