22 #include "trunnel/pwbox.h"
36 #define MAX_OVERHEAD (S2K_MAXLEN + 8 + 1 + 32 + CIPHER_IV_LEN)
48 const uint8_t *input,
size_t input_len,
49 const char *secret,
size_t secret_len,
52 uint8_t *result = NULL, *encrypted_portion;
53 size_t encrypted_len = 128 * CEIL_DIV(input_len+4, 128);
57 pwbox_encoded_t *enc = NULL;
60 crypto_cipher_t *cipher;
63 enc = pwbox_encoded_new();
66 pwbox_encoded_setlen_skey_header(enc,
S2K_MAXLEN);
69 pwbox_encoded_getarray_skey_header(enc),
72 if (BUG(spec_len < 0 || spec_len >
S2K_MAXLEN))
74 pwbox_encoded_setlen_skey_header(enc, spec_len);
75 enc->header_len = spec_len;
79 pwbox_encoded_setlen_data(enc, encrypted_len);
80 encrypted_portion = pwbox_encoded_getarray_data(enc);
83 memcpy(encrypted_portion+4, input, input_len);
88 pwbox_encoded_getarray_skey_header(enc),
96 crypto_cipher_free(cipher);
98 result_len = pwbox_encoded_encoded_len(enc);
99 if (BUG(result_len < 0))
101 result = tor_malloc(result_len);
102 enc_len = pwbox_encoded_encode(result, result_len, enc);
103 if (BUG(enc_len < 0))
114 *outlen_out = result_len;
138 pwbox_encoded_free(enc);
139 memwipe(keys, 0,
sizeof(keys));
154 const uint8_t *inp,
size_t input_len,
155 const char *secret,
size_t secret_len)
157 uint8_t *result = NULL;
158 const uint8_t *encrypted;
162 size_t encrypted_len;
163 crypto_cipher_t *cipher = NULL;
164 int rv = UNPWBOX_CORRUPTED;
167 pwbox_encoded_t *enc = NULL;
169 got_len = pwbox_encoded_parse(&enc, inp, input_len);
170 if (got_len < 0 || (
size_t)got_len != input_len)
175 pwbox_encoded_getarray_skey_header(enc),
176 pwbox_encoded_getlen_skey_header(enc),
177 secret, secret_len) < 0)
185 rv = UNPWBOX_BAD_SECRET;
190 encrypted = pwbox_encoded_getarray_data(enc);
191 encrypted_len = pwbox_encoded_getlen_data(enc);
192 if (encrypted_len < 4)
198 if (encrypted_len < result_len + 4)
202 result = tor_malloc_zero(result_len);
206 *outlen_out = result_len;
215 crypto_cipher_free(cipher);
216 pwbox_encoded_free(enc);
217 memwipe(keys, 0,
sizeof(keys));
Inline functions for reading and writing multibyte values from the middle of strings,...
static uint32_t tor_htonl(uint32_t a)
static uint32_t tor_ntohl(uint32_t a)
static void set_uint32(void *cp, uint32_t v)
void crypto_cipher_crypt_inplace(crypto_cipher_t *env, char *buf, size_t len)
int crypto_cipher_decrypt(crypto_cipher_t *env, char *to, const char *from, size_t fromlen)
crypto_cipher_t * crypto_cipher_new_with_iv(const char *key, const char *iv)
Headers for crypto_cipher.c.
Headers for crypto_digest.c.
void crypto_hmac_sha256(char *hmac_out, const char *key, size_t key_len, const char *msg, size_t msg_len)
int crypto_pwbox(uint8_t **out, size_t *outlen_out, const uint8_t *input, size_t input_len, const char *secret, size_t secret_len, unsigned s2k_flags)
int crypto_unpwbox(uint8_t **out, size_t *outlen_out, const uint8_t *inp, size_t input_len, const char *secret, size_t secret_len)
Header for crypto_pwbox.c.
void crypto_rand(char *to, size_t n)
Common functions for using (pseudo-)random number generators.
int secret_to_key_derivekey(uint8_t *key_out, size_t key_out_len, const uint8_t *spec, size_t spec_len, const char *secret, size_t secret_len)
int secret_to_key_make_specifier(uint8_t *buf, size_t buf_len, unsigned flags)
void memwipe(void *mem, uint8_t byte, size_t sz)
Common functions for cryptographic routines.
#define tor_memneq(a, b, sz)
Macros to manage assertions, fatal and non-fatal.