This repository has been archived by the owner on Apr 30, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtox_data.h
150 lines (125 loc) · 4.46 KB
/
tox_data.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#ifndef TOX_DATA_H
#define TOX_DATA_H
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <tox.h>
#include <sodium.h>
#ifdef __cplusplus
extern "C" {
#endif
#include <scrypt-jane.h>
/* "Profile" Save Format
* ==============
*
* bytes name type purpose
* ----------------------------------------
* -- block one [unencrypted] --
* 4 magic uint8 magic, 6c:69:62:65 "libe"
* 8 saved uint64 unix timestamp from when the profile was last used
* 2 namelen uint16 length of name
* varies name uint8 name of profile, UTF-8 preferably
* 12 scryptvars uint32 N,r,p variables for scrypt - in this order
* 24 salt uint8 the salt for scrypt
* 24 nonce uint8 the nonce for nacl
* 8 blocklen uint64 the length of the encrypted block
* -- block two [encrypted] --
* 32 0 uint8 crypto_secretbox_ZEROBYTES
* 4 magic uint8 magic, 72:74:61:73 "rtas"
* varies profile uint8 the messenger data - this goes to tox_load()
*/
typedef struct tox_data {
//the file path for the given profile
char *file_path;
//indicates whether the profile is locked
int locked;
uint8_t encrypted_key[crypto_secretbox_KEYBYTES], //Sodium encrypt key, 32 bytes.
nonce[crypto_secretbox_NONCEBYTES], //Sodium nonce, 24 bytes.
salt[24]; //Scrypt salt, 24 bytes.
/* The values used (N=12,r=8,p=1) were sourced from here: https://www.tarsnap.com/scrypt/scrypt-slides.pdf
* Percival recommends larger (N=14) values for interactive logins - I've lessened them slightly to make
* decryption acceptable on slower machines. One can always specify harder values.
*/
uint32_t scrypt_n,
scrypt_r,
scrypt_p;
//The profile's name and last save time.
uint8_t *name;
uint64_t time_saved;
//The encrypted block's file offset and length.
size_t _block_two_offset;
uint64_t _block_two_length;
//The unencrypted messenger data.
uint8_t *_data;
size_t _data_length;
} tox_data;
/* NOTE - all string function parameters must be nul-teminated (path, password, et cetera) */
/* Creates and returns a new tox_data with the given path, internal name, and password
* The returned tox_data is both unlocked and not yet saved to disk.
* The caller is responsible for calling data_close() on the given tox_data when done.
*
* returns tox_data on success, NULL otherwise
*/
tox_data* data_init_new(char *path, uint8_t *data_name, uint8_t *password);
/* Loads a tox_data from the given path
* The returned tox_data is locked until data_unlock() is called.
* The caller is responsible for calling data_close() on the given tox_data when done.
*
* returns tox_data on success, NULL otherwise
*/
tox_data* data_init_load(char *path);
/* Safely frees a given tox_data */
void data_close(tox_data *data);
/* Unlocks the given tox_data
*
* returns 0 if success
* -1 if the password is wrong
* -2 if the file is malformed
*/
int data_unlock(tox_data *data, uint8_t *password);
/* ------------- REQUIRES UNLOCKING ------------- */
/* Locks the given tox_data
*
* returns 0 if success
* -1 if the file is locked
*/
int data_lock(tox_data *data);
/* Changes the password for the given tox_data
*
* returns 0 if success
* -1 if the file is locked
* -2 if the old password is wrong
*/
int data_change_key(tox_data *data, uint8_t *old_password, uint8_t *new_password);
/* Writes the given messenger to the given tox_data
* This function also calls data_flush().
*
* returns 0 if success
* -1 if the file is locked
*/
int data_write_messenger(tox_data *data, uint8_t *buffer, size_t length);
/* Returns the size of the loaded messenger
*
* returns the size of the tox messenger if success
* -1 if the file is locked
*/
size_t data_messenger_size(tox_data *data);
/* Copies the messenger from the given tox_data to the given buffer
* The buffer must be preallocated to data_messenger_size() bytes.
*
* returns 0 if success
* -1 if the file is locked
*/
int data_read_messenger(tox_data *data, uint8_t *buffer);
/* Writes the given tox_data to the disk
*
* returns 0 if success
* -1 if the file is locked
* -2 if encryption error
* -3 if FILE error
*/
int data_flush(tox_data *data);
#ifdef __cplusplus
}
#endif
#endif