diff options
author | irungentoo <irungentoo@gmail.com> | 2013-12-21 22:35:27 -0500 |
---|---|---|
committer | irungentoo <irungentoo@gmail.com> | 2013-12-21 22:35:27 -0500 |
commit | c51b8a9eba2b7f50f34796f9606c4cb7322993fa (patch) | |
tree | 4c7e179679a5abbbd00ba94dd5fa3a38fa1fa71f /toxcore/Messenger.c | |
parent | 83cb946db0ae21cb2edb577093237b34ef99375f (diff) |
Added function to save an encrypted version of the messenger.
Also added some tests.
Diffstat (limited to 'toxcore/Messenger.c')
-rw-r--r-- | toxcore/Messenger.c | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index b3c8fb40..78c83519 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c | |||
@@ -2264,6 +2264,75 @@ int messenger_load(Messenger *m, uint8_t *data, uint32_t length) | |||
2264 | return -1; | 2264 | return -1; |
2265 | } | 2265 | } |
2266 | 2266 | ||
2267 | /* return the size of data to pass to messenger_save_encrypted(...) | ||
2268 | * | ||
2269 | */ | ||
2270 | uint32_t messenger_size_encrypted(Messenger *m) | ||
2271 | { | ||
2272 | return messenger_size(m) + crypto_secretbox_MACBYTES + crypto_secretbox_NONCEBYTES; | ||
2273 | } | ||
2274 | |||
2275 | /* Save the messenger, encrypting the data with key of length key_length | ||
2276 | * | ||
2277 | * return 0 on success. | ||
2278 | * return -1 on failure. | ||
2279 | */ | ||
2280 | int messenger_save_encrypted(Messenger *m, uint8_t *data, uint8_t *key, uint16_t key_length) | ||
2281 | { | ||
2282 | uint32_t m_size = messenger_size(m); | ||
2283 | uint8_t *plain_messenger = malloc(m_size); | ||
2284 | |||
2285 | if (plain_messenger == NULL) | ||
2286 | return -1; | ||
2287 | |||
2288 | messenger_save(m, plain_messenger); | ||
2289 | |||
2290 | /* Hash the key with SHA256 to get a 32byte key. */ | ||
2291 | uint8_t hash[crypto_hash_sha256_BYTES]; | ||
2292 | crypto_hash_sha256(hash, key, key_length); | ||
2293 | random_nonce(data); | ||
2294 | encrypt_data_symmetric(hash, data, plain_messenger, m_size, data + crypto_secretbox_NONCEBYTES); | ||
2295 | |||
2296 | memset(plain_messenger, 0, m_size); | ||
2297 | free(plain_messenger); | ||
2298 | memset(hash, 0, crypto_hash_sha256_BYTES); | ||
2299 | return 0; | ||
2300 | } | ||
2301 | |||
2302 | /* Load the messenger from data of size length encrypted with key of key_length. | ||
2303 | * | ||
2304 | * return 0 on success. | ||
2305 | * return -1 on failure. | ||
2306 | */ | ||
2307 | int messenger_load_encrypted(Messenger *m, uint8_t *data, uint32_t length, uint8_t *key, uint16_t key_length) | ||
2308 | { | ||
2309 | if (length <= crypto_secretbox_MACBYTES + crypto_secretbox_NONCEBYTES) | ||
2310 | return -1; | ||
2311 | |||
2312 | uint8_t *plain_messenger = malloc(length); | ||
2313 | |||
2314 | if (plain_messenger == NULL) | ||
2315 | return -1; | ||
2316 | |||
2317 | /* Hash the key with SHA256 to get a 32byte key. */ | ||
2318 | uint8_t hash[crypto_hash_sha256_BYTES]; | ||
2319 | crypto_hash_sha256(hash, key, key_length); | ||
2320 | int len = decrypt_data_symmetric(hash, data, data + crypto_secretbox_NONCEBYTES, length - crypto_secretbox_NONCEBYTES, | ||
2321 | plain_messenger); | ||
2322 | int ret; | ||
2323 | |||
2324 | if ((uint32_t)len == length - crypto_secretbox_NONCEBYTES - crypto_secretbox_MACBYTES) { | ||
2325 | ret = messenger_load(m, plain_messenger, length - crypto_secretbox_NONCEBYTES - crypto_secretbox_MACBYTES); | ||
2326 | } else { | ||
2327 | ret = -1; | ||
2328 | } | ||
2329 | |||
2330 | memset(plain_messenger, 0, length); | ||
2331 | free(plain_messenger); | ||
2332 | memset(hash, 0, crypto_hash_sha256_BYTES); | ||
2333 | return ret; | ||
2334 | } | ||
2335 | |||
2267 | /* Return the number of friends in the instance m. | 2336 | /* Return the number of friends in the instance m. |
2268 | * You should use this to determine how much memory to allocate | 2337 | * You should use this to determine how much memory to allocate |
2269 | * for copy_friendlist. */ | 2338 | * for copy_friendlist. */ |