summaryrefslogtreecommitdiff
path: root/toxencryptsave
diff options
context:
space:
mode:
authordubslow <bunslow@gmail.com>2014-10-12 02:29:34 -0500
committerdubslow <bunslow@gmail.com>2014-10-12 02:29:34 -0500
commit458a09f6ab289afa1e475f388988d88e07dcb8b0 (patch)
treed85f2cb7780a5d36a4f43d3af028cdcaa947d5fe /toxencryptsave
parentccfd777e35812a914dd0025487f219d5f503a1bd (diff)
parent0444ca18dff821943aac9bc3a59207b2d0dea33f (diff)
Merge branch 'master' of https://github.com/irungentoo/toxcore
Diffstat (limited to 'toxencryptsave')
-rw-r--r--toxencryptsave/toxencryptsave.c23
-rw-r--r--toxencryptsave/toxencryptsave.h8
2 files changed, 17 insertions, 14 deletions
diff --git a/toxencryptsave/toxencryptsave.c b/toxencryptsave/toxencryptsave.c
index 953ee802..7efba089 100644
--- a/toxencryptsave/toxencryptsave.c
+++ b/toxencryptsave/toxencryptsave.c
@@ -59,7 +59,7 @@ uint32_t tox_encrypted_size(const Tox *tox)
59 * returns 0 on success 59 * returns 0 on success
60 * returns -1 on failure 60 * returns -1 on failure
61 */ 61 */
62int tox_derive_key_from_pass(uint8_t* passphrase, uint32_t pplength, uint8_t* out_key) 62int tox_derive_key_from_pass(uint8_t *passphrase, uint32_t pplength, uint8_t *out_key)
63{ 63{
64 if (pplength == 0) 64 if (pplength == 0)
65 return -1; 65 return -1;
@@ -98,7 +98,7 @@ int tox_derive_key_from_pass(uint8_t* passphrase, uint32_t pplength, uint8_t* ou
98 * returns 0 on success 98 * returns 0 on success
99 * returns -1 on failure 99 * returns -1 on failure
100 */ 100 */
101int tox_pass_key_encrypt(uint8_t* data, uint32_t data_len, const uint8_t* key, uint8_t* out) 101int tox_pass_key_encrypt(uint8_t *data, uint32_t data_len, const uint8_t *key, uint8_t *out)
102{ 102{
103 /* the output data consists of, in order: 103 /* the output data consists of, in order:
104 * salt, nonce, mac, enc_data 104 * salt, nonce, mac, enc_data
@@ -110,7 +110,7 @@ int tox_pass_key_encrypt(uint8_t* data, uint32_t data_len, const uint8_t* key, u
110 110
111 /* first add the prefix */ 111 /* first add the prefix */
112 uint8_t nonce[crypto_box_NONCEBYTES]; 112 uint8_t nonce[crypto_box_NONCEBYTES];
113 random_nonce(nonce); 113 random_nonce(nonce);
114 114
115 memcpy(out, key, crypto_pwhash_scryptsalsa208sha256_SALTBYTES); 115 memcpy(out, key, crypto_pwhash_scryptsalsa208sha256_SALTBYTES);
116 key += crypto_pwhash_scryptsalsa208sha256_SALTBYTES; 116 key += crypto_pwhash_scryptsalsa208sha256_SALTBYTES;
@@ -134,9 +134,10 @@ int tox_pass_key_encrypt(uint8_t* data, uint32_t data_len, const uint8_t* key, u
134 * returns 0 on success 134 * returns 0 on success
135 * returns -1 on failure 135 * returns -1 on failure
136 */ 136 */
137int tox_pass_encrypt(uint8_t* data, uint32_t data_len, uint8_t* passphrase, uint32_t pplength, uint8_t* out) 137int tox_pass_encrypt(uint8_t *data, uint32_t data_len, uint8_t *passphrase, uint32_t pplength, uint8_t *out)
138{ 138{
139 uint8_t key[TOX_PASS_KEY_LENGTH]; 139 uint8_t key[TOX_PASS_KEY_LENGTH];
140
140 if (tox_derive_key_from_pass(passphrase, pplength, key) == -1) 141 if (tox_derive_key_from_pass(passphrase, pplength, key) == -1)
141 return -1; 142 return -1;
142 143
@@ -171,7 +172,7 @@ int tox_encrypted_save(const Tox *tox, uint8_t *data, uint8_t *passphrase, uint3
171 * returns the length of the output data (== data_len - TOX_PASS_ENCRYPTION_EXTRA_LENGTH) on success 172 * returns the length of the output data (== data_len - TOX_PASS_ENCRYPTION_EXTRA_LENGTH) on success
172 * returns -1 on failure 173 * returns -1 on failure
173 */ 174 */
174int tox_pass_key_decrypt(const uint8_t* data, uint32_t length, const uint8_t* key, uint8_t* out) 175int tox_pass_key_decrypt(const uint8_t *data, uint32_t length, const uint8_t *key, uint8_t *out)
175{ 176{
176 if (length <= TOX_PASS_ENCRYPTION_EXTRA_LENGTH) 177 if (length <= TOX_PASS_ENCRYPTION_EXTRA_LENGTH)
177 return -1; 178 return -1;
@@ -201,14 +202,14 @@ int tox_pass_key_decrypt(const uint8_t* data, uint32_t length, const uint8_t* ke
201 * returns the length of the output data (== data_len - TOX_PASS_ENCRYPTION_EXTRA_LENGTH) on success 202 * returns the length of the output data (== data_len - TOX_PASS_ENCRYPTION_EXTRA_LENGTH) on success
202 * returns -1 on failure 203 * returns -1 on failure
203 */ 204 */
204int tox_pass_decrypt(const uint8_t* data, uint32_t length, uint8_t* passphrase, uint32_t pplength, uint8_t* out) 205int tox_pass_decrypt(const uint8_t *data, uint32_t length, uint8_t *passphrase, uint32_t pplength, uint8_t *out)
205{ 206{
206 207
207 uint8_t passkey[crypto_hash_sha256_BYTES]; 208 uint8_t passkey[crypto_hash_sha256_BYTES];
208 crypto_hash_sha256(passkey, passphrase, pplength); 209 crypto_hash_sha256(passkey, passphrase, pplength);
209 210
210 uint8_t salt[crypto_pwhash_scryptsalsa208sha256_SALTBYTES]; 211 uint8_t salt[crypto_pwhash_scryptsalsa208sha256_SALTBYTES];
211 memcpy(salt, data, crypto_pwhash_scryptsalsa208sha256_SALTBYTES); 212 memcpy(salt, data, crypto_pwhash_scryptsalsa208sha256_SALTBYTES);
212 213
213 /* derive the key */ 214 /* derive the key */
214 uint8_t key[crypto_box_KEYBYTES + crypto_pwhash_scryptsalsa208sha256_SALTBYTES]; 215 uint8_t key[crypto_box_KEYBYTES + crypto_pwhash_scryptsalsa208sha256_SALTBYTES];
@@ -236,13 +237,15 @@ int tox_encrypted_load(Tox *tox, const uint8_t *data, uint32_t length, uint8_t *
236{ 237{
237 if (memcmp(data, TOX_ENC_SAVE_MAGIC_NUMBER, TOX_ENC_SAVE_MAGIC_LENGTH) != 0) 238 if (memcmp(data, TOX_ENC_SAVE_MAGIC_NUMBER, TOX_ENC_SAVE_MAGIC_LENGTH) != 0)
238 return -1; 239 return -1;
239 data += TOX_ENC_SAVE_MAGIC_LENGTH; length -= TOX_ENC_SAVE_MAGIC_LENGTH; 240
241 data += TOX_ENC_SAVE_MAGIC_LENGTH;
242 length -= TOX_ENC_SAVE_MAGIC_LENGTH;
240 243
241 uint32_t decrypt_length = length - TOX_PASS_ENCRYPTION_EXTRA_LENGTH; 244 uint32_t decrypt_length = length - TOX_PASS_ENCRYPTION_EXTRA_LENGTH;
242 uint8_t temp_data[decrypt_length]; 245 uint8_t temp_data[decrypt_length];
243 246
244 if (tox_pass_decrypt(data, length, passphrase, pplength, temp_data) 247 if (tox_pass_decrypt(data, length, passphrase, pplength, temp_data)
245 != decrypt_length) 248 != decrypt_length)
246 return -1; 249 return -1;
247 250
248 return tox_load(tox, temp_data, decrypt_length); 251 return tox_load(tox, temp_data, decrypt_length);
diff --git a/toxencryptsave/toxencryptsave.h b/toxencryptsave/toxencryptsave.h
index 6abcca42..2fb6a8c5 100644
--- a/toxencryptsave/toxencryptsave.h
+++ b/toxencryptsave/toxencryptsave.h
@@ -61,7 +61,7 @@ uint32_t tox_encrypted_size(const Tox *tox);
61 * returns 0 on success 61 * returns 0 on success
62 * returns -1 on failure 62 * returns -1 on failure
63 */ 63 */
64int tox_derive_key_from_pass(uint8_t* passphrase, uint32_t pplength, uint8_t* out_key); 64int tox_derive_key_from_pass(uint8_t *passphrase, uint32_t pplength, uint8_t *out_key);
65 65
66/* Encrypt arbitrary with a key produced by tox_derive_key_from_pass. The output 66/* Encrypt arbitrary with a key produced by tox_derive_key_from_pass. The output
67 * array must be at least data_len + TOX_PASS_ENCRYPTION_EXTRA_LENGTH bytes long. 67 * array must be at least data_len + TOX_PASS_ENCRYPTION_EXTRA_LENGTH bytes long.
@@ -73,7 +73,7 @@ int tox_derive_key_from_pass(uint8_t* passphrase, uint32_t pplength, uint8_t* ou
73 * returns 0 on success 73 * returns 0 on success
74 * returns -1 on failure 74 * returns -1 on failure
75 */ 75 */
76int tox_pass_key_encrypt(uint8_t* data, uint32_t data_len, const uint8_t* key, uint8_t* out); 76int tox_pass_key_encrypt(uint8_t *data, uint32_t data_len, const uint8_t *key, uint8_t *out);
77 77
78/* Encrypts the given data with the given passphrase. The output array must be 78/* Encrypts the given data with the given passphrase. The output array must be
79 * at least data_len + TOX_PASS_ENCRYPTION_EXTRA_LENGTH bytes long. This delegates 79 * at least data_len + TOX_PASS_ENCRYPTION_EXTRA_LENGTH bytes long. This delegates
@@ -84,7 +84,7 @@ int tox_pass_key_encrypt(uint8_t* data, uint32_t data_len, const uint8_t* key, u
84 * returns 0 on success 84 * returns 0 on success
85 * returns -1 on failure 85 * returns -1 on failure
86 */ 86 */
87int tox_pass_encrypt(uint8_t* data, uint32_t data_len, uint8_t* passphrase, uint32_t pplength, uint8_t* out); 87int tox_pass_encrypt(uint8_t *data, uint32_t data_len, uint8_t *passphrase, uint32_t pplength, uint8_t *out);
88 88
89/* Save the messenger data encrypted with the given password. 89/* Save the messenger data encrypted with the given password.
90 * data must be at least tox_encrypted_size(). 90 * data must be at least tox_encrypted_size().
@@ -111,7 +111,7 @@ int tox_pass_key_decrypt(const uint8_t* data, uint32_t length, const uint8_t* ke
111 * returns the length of the output data (== data_len - TOX_PASS_ENCRYPTION_EXTRA_LENGTH) on success 111 * returns the length of the output data (== data_len - TOX_PASS_ENCRYPTION_EXTRA_LENGTH) on success
112 * returns -1 on failure 112 * returns -1 on failure
113 */ 113 */
114int tox_pass_decrypt(const uint8_t* data, uint32_t length, uint8_t* passphrase, uint32_t pplength, uint8_t* out); 114int tox_pass_decrypt(const uint8_t *data, uint32_t length, uint8_t *passphrase, uint32_t pplength, uint8_t *out);
115 115
116/* Load the messenger from encrypted data of size length. 116/* Load the messenger from encrypted data of size length.
117 * 117 *