summaryrefslogtreecommitdiff
path: root/toxencryptsave/toxencryptsave.h
diff options
context:
space:
mode:
Diffstat (limited to 'toxencryptsave/toxencryptsave.h')
-rw-r--r--toxencryptsave/toxencryptsave.h220
1 files changed, 87 insertions, 133 deletions
diff --git a/toxencryptsave/toxencryptsave.h b/toxencryptsave/toxencryptsave.h
index db272592..ef1dfb5e 100644
--- a/toxencryptsave/toxencryptsave.h
+++ b/toxencryptsave/toxencryptsave.h
@@ -30,6 +30,7 @@ extern "C" {
30 30
31#include <stdint.h> 31#include <stdint.h>
32#include <stddef.h> 32#include <stddef.h>
33#include <stdbool.h>
33 34
34#ifndef TOX_DEFINED 35#ifndef TOX_DEFINED
35#define TOX_DEFINED 36#define TOX_DEFINED
@@ -37,21 +38,11 @@ typedef struct Tox Tox;
37struct Tox_Options; 38struct Tox_Options;
38#endif 39#endif
39 40
40// these functions provide access to these defines in toxencryptsave.c, which 41#define TOX_PASS_SALT_LENGTH 32
41// otherwise aren't actually available in clients... 42#define TOX_PASS_KEY_LENGTH 64
42int tox_pass_encryption_extra_length(); 43#define TOX_PASS_ENCRYPTION_EXTRA_LENGTH 80
43 44
44int tox_pass_key_length(); 45/* This module is conceptually organized into two parts. The first part are the functions
45
46int tox_pass_salt_length();
47
48/* return size of the messenger data (for encrypted Messenger saving). */
49uint32_t tox_encrypted_size(const Tox *tox);
50
51/* This "module" provides functions analogous to tox_load and tox_save in toxcore,
52 * as well as functions for encryption of arbitrary client data (e.g. chat logs).
53 *
54 * It is conceptually organized into two parts. The first part are the functions
55 * with "key" in the name. To use these functions, first derive an encryption key 46 * with "key" in the name. To use these functions, first derive an encryption key
56 * from a password with tox_derive_key_from_pass, and use the returned key to 47 * from a password with tox_derive_key_from_pass, and use the returned key to
57 * encrypt the data. The second part takes the password itself instead of the key, 48 * encrypt the data. The second part takes the password itself instead of the key,
@@ -61,114 +52,100 @@ uint32_t tox_encrypted_size(const Tox *tox);
61 * favor using the first part intead of the second part. 52 * favor using the first part intead of the second part.
62 * 53 *
63 * The encrypted data is prepended with a magic number, to aid validity checking 54 * The encrypted data is prepended with a magic number, to aid validity checking
64 * (no guarantees are made of course). 55 * (no guarantees are made of course). Any data to be decrypted must start with
56 * the magic number.
65 * 57 *
66 * Clients should consider alerting their users that, unlike plain data, if even one bit 58 * Clients should consider alerting their users that, unlike plain data, if even one bit
67 * becomes corrupted, the data will be entirely unrecoverable. 59 * becomes corrupted, the data will be entirely unrecoverable.
68 * Ditto if they forget their password, there is no way to recover the data. 60 * Ditto if they forget their password, there is no way to recover the data.
69 */ 61 */
70 62
71 63typedef enum TOX_ERR_KEY_DERIVATION {
72/******************************* BEGIN PART 2 ******************************* 64 TOX_ERR_KEY_DERIVATION_OK,
73 * For simplicty, the second part of the module is presented first. The API for
74 * the first part is analgous, with some extra functions for key handling. If
75 * your code spends too much time using these functions, consider using the part
76 * 1 functions instead.
77 */
78
79/* Encrypts the given data with the given passphrase. The output array must be
80 * at least data_len + tox_pass_encryption_extra_length() bytes long. This delegates
81 * to tox_derive_key_from_pass and tox_pass_key_encrypt.
82 *
83 * tox_encrypted_save() is a good example of how to use this function.
84 *
85 * returns 0 on success
86 * returns -1 on failure
87 */
88int tox_pass_encrypt(const uint8_t *data, uint32_t data_len, uint8_t *passphrase, uint32_t pplength, uint8_t *out);
89
90/* Save the messenger data encrypted with the given password.
91 * data must be at least tox_encrypted_size().
92 *
93 * NOTE: Unlike tox_save(), this function may fail. Be sure to check its return
94 * value.
95 *
96 * returns 0 on success
97 * returns -1 on failure
98 */
99int tox_encrypted_save(const Tox *tox, uint8_t *data, uint8_t *passphrase, uint32_t pplength);
100
101/* Decrypts the given data with the given passphrase. The output array must be
102 * at least data_len - tox_pass_encryption_extra_length() bytes long. This delegates
103 * to tox_pass_key_decrypt.
104 *
105 * tox_encrypted_load() is a good example of how to use this function.
106 *
107 * returns the length of the output data (== data_len - tox_pass_encryption_extra_length()) on success
108 * returns -1 on failure
109 */
110int tox_pass_decrypt(const uint8_t *data, uint32_t length, uint8_t *passphrase, uint32_t pplength, uint8_t *out);
111
112typedef enum TOX_ERR_ENCRYPTED_NEW {
113 TOX_ERR_ENCRYPTED_NEW_OK,
114 TOX_ERR_ENCRYPTED_NEW_NULL,
115 /** 65 /**
116 * The function was unable to allocate enough memory to store the internal 66 * Some input data, or maybe the output pointer, was null.
117 * structures for the Tox object.
118 */ 67 */
119 TOX_ERR_ENCRYPTED_NEW_MALLOC, 68 TOX_ERR_KEY_DERIVATION_NULL,
120 /** 69 /**
121 * The function was unable to bind to a port. This may mean that all ports 70 * The crypto lib was unable to derive a key from the given passphrase,
122 * have already been bound, e.g. by other Tox instances, or it may mean 71 * which is usually a lack of memory issue. The functions accepting keys
123 * a permission error. You may be able to gather more information from errno. 72 * do not produce this error.
124 */ 73 */
125 TOX_ERR_ENCRYPTED_NEW_PORT_ALLOC, 74 TOX_ERR_KEY_DERIVATION_FAILED
75} TOX_ERR_KEY_DERIVATION;
76
77typedef enum TOX_ERR_ENCRYPTION {
78 TOX_ERR_ENCRYPTION_OK,
126 /** 79 /**
127 * proxy_type was invalid. 80 * Some input data, or maybe the output pointer, was null.
128 */ 81 */
129 TOX_ERR_ENCRYPTED_NEW_PROXY_BAD_TYPE, 82 TOX_ERR_ENCRYPTION_NULL,
130 /** 83 /**
131 * proxy_type was valid but the proxy_host passed had an invalid format 84 * The crypto lib was unable to derive a key from the given passphrase,
132 * or was NULL. 85 * which is usually a lack of memory issue. The functions accepting keys
86 * do not produce this error.
133 */ 87 */
134 TOX_ERR_ENCRYPTED_NEW_PROXY_BAD_HOST, 88 TOX_ERR_ENCRYPTION_KEY_DERIVATION_FAILED,
135 /** 89 /**
136 * proxy_type was valid, but the proxy_port was invalid. 90 * The encryption itself failed.
137 */ 91 */
138 TOX_ERR_ENCRYPTED_NEW_PROXY_BAD_PORT, 92 TOX_ERR_ENCRYPTION_FAILED
93} TOX_ERR_ENCRYPTION;
94
95typedef enum TOX_ERR_DECRYPTION {
96 TOX_ERR_DECRYPTION_OK,
97 /**
98 * Some input data, or maybe the output pointer, was null.
99 */
100 TOX_ERR_DECRYPTION_NULL,
139 /** 101 /**
140 * The proxy host passed could not be resolved. 102 * The input data was shorter than TOX_PASS_ENCRYPTION_EXTRA_LENGTH bytes
141 */ 103 */
142 TOX_ERR_ENCRYPTED_NEW_PROXY_NOT_FOUND, 104 TOX_ERR_DECRYPTION_INVALID_LENGTH,
143 /** 105 /**
144 * The byte array to be loaded contained an encrypted save. 106 * The input data is missing the magic number (i.e. wasn't created by this
107 * module, or is corrupted)
145 */ 108 */
146 TOX_ERR_ENCRYPTED_NEW_LOAD_ENCRYPTED, 109 TOX_ERR_DECRYPTION_BAD_FORMAT,
147 /** 110 /**
148 * The data format was invalid. This can happen when loading data that was 111 * The crypto lib was unable to derive a key from the given passphrase,
149 * saved by an older version of Tox, or when the data has been corrupted. 112 * which is usually a lack of memory issue. The functions accepting keys
150 * When loading from badly formatted data, some data may have been loaded, 113 * do not produce this error.
151 * and the rest is discarded. Passing an invalid length parameter also
152 * causes this error.
153 */ 114 */
154 TOX_ERR_ENCRYPTED_NEW_LOAD_BAD_FORMAT, 115 TOX_ERR_DECRYPTION_KEY_DERIVATION_FAILED,
155 /** 116 /**
156 * The encrypted byte array could not be decrypted. Either the data was 117 * The encrypted byte array could not be decrypted. Either the data was
157 * corrupt or the password/key was incorrect. 118 * corrupt or the password/key was incorrect.
158 *
159 * NOTE: This error code is only set by tox_encrypted_new() and
160 * tox_encrypted_key_new(), in the toxencryptsave module.
161 */ 119 */
162 TOX_ERR_ENCRYPTED_NEW_LOAD_DECRYPTION_FAILED 120 TOX_ERR_DECRYPTION_FAILED
163} TOX_ERR_ENCRYPTED_NEW; 121} TOX_ERR_DECRYPTION;
122
123
124/******************************* BEGIN PART 2 *******************************
125 * For simplicty, the second part of the module is presented first. The API for
126 * the first part is analgous, with some extra functions for key handling. If
127 * your code spends too much time using these functions, consider using the part
128 * 1 functions instead.
129 */
130
131/* Encrypts the given data with the given passphrase. The output array must be
132 * at least data_len + TOX_PASS_ENCRYPTION_EXTRA_LENGTH bytes long. This delegates
133 * to tox_derive_key_from_pass and tox_pass_key_encrypt.
134 *
135 * returns true on success
136 */
137bool tox_pass_encrypt(const uint8_t *data, size_t data_len, uint8_t *passphrase, size_t pplength, uint8_t *out, TOX_ERR_ENCRYPTION *error);
164 138
165/* Load the new messenger from encrypted data of size length. 139
166 * All other arguments are like toxcore/tox_new(). 140/* Decrypts the given data with the given passphrase. The output array must be
141 * at least data_len - TOX_PASS_ENCRYPTION_EXTRA_LENGTH bytes long. This delegates
142 * to tox_pass_key_decrypt.
143 *
144 * the output data has size data_length - TOX_PASS_ENCRYPTION_EXTRA_LENGTH
167 * 145 *
168 * returns NULL on failure; see the documentation in toxcore/tox.h. 146 * returns true on success
169 */ 147 */
170Tox *tox_encrypted_new(const struct Tox_Options *options, const uint8_t *data, size_t length, uint8_t *passphrase, 148bool tox_pass_decrypt(const uint8_t *data, size_t length, uint8_t *passphrase, size_t pplength, uint8_t *out, TOX_ERR_DECRYPTION *error);
171 size_t pplength, TOX_ERR_ENCRYPTED_NEW *error);
172 149
173 150
174/******************************* BEGIN PART 1 ******************************* 151/******************************* BEGIN PART 1 *******************************
@@ -177,7 +154,7 @@ Tox *tox_encrypted_new(const struct Tox_Options *options, const uint8_t *data, s
177 */ 154 */
178 155
179/* Generates a secret symmetric key from the given passphrase. out_key must be at least 156/* Generates a secret symmetric key from the given passphrase. out_key must be at least
180 * tox_pass_key_length() bytes long. 157 * TOX_PASS_KEY_LENGTH bytes long.
181 * Be sure to not compromise the key! Only keep it in memory, do not write to disk. 158 * Be sure to not compromise the key! Only keep it in memory, do not write to disk.
182 * The password is zeroed after key derivation. 159 * The password is zeroed after key derivation.
183 * The key should only be used with the other functions in this module, as it 160 * The key should only be used with the other functions in this module, as it
@@ -185,72 +162,49 @@ Tox *tox_encrypted_new(const struct Tox_Options *options, const uint8_t *data, s
185 * Note that this function is not deterministic; to derive the same key from a 162 * Note that this function is not deterministic; to derive the same key from a
186 * password, you also must know the random salt that was used. See below. 163 * password, you also must know the random salt that was used. See below.
187 * 164 *
188 * returns 0 on success 165 * returns true on success
189 * returns -1 on failure
190 */ 166 */
191int tox_derive_key_from_pass(uint8_t *passphrase, uint32_t pplength, uint8_t *out_key); 167bool tox_derive_key_from_pass(uint8_t *passphrase, size_t pplength, uint8_t *out_key, TOX_ERR_KEY_DERIVATION *error);
192 168
193/* Same as above, except with use the given salt for deterministic key derivation. 169/* Same as above, except with use the given salt for deterministic key derivation.
194 * The salt must be tox_salt_length() bytes in length. 170 * The salt must be tox_salt_length() bytes in length.
195 */ 171 */
196int tox_derive_key_with_salt(uint8_t *passphrase, uint32_t pplength, uint8_t *salt, uint8_t *out_key); 172bool tox_derive_key_with_salt(uint8_t *passphrase, size_t pplength, uint8_t *salt, uint8_t *out_key, TOX_ERR_KEY_DERIVATION *error);
197 173
198/* This retrieves the salt used to encrypt the given data, which can then be passed to 174/* This retrieves the salt used to encrypt the given data, which can then be passed to
199 * derive_key_with_salt to produce the same key as was previously used. Any encrpyted 175 * derive_key_with_salt to produce the same key as was previously used. Any encrpyted
200 * data with this module can be used as input. 176 * data with this module can be used as input.
201 * 177 *
202 * returns -1 if the magic number is wrong 178 * returns true if magic number matches
203 * returns 0 otherwise (no guarantee about validity of data) 179 * success does not say anything about the validity of the data, only that data of
180 * the appropriate size was copied
204 */ 181 */
205int tox_get_salt(uint8_t *data, uint8_t *salt); 182bool tox_get_salt(const uint8_t *data, uint8_t *salt);
206 183
207/* Now come the functions that are analogous to the part 2 functions. */ 184/* Now come the functions that are analogous to the part 2 functions. */
208 185
209/* Encrypt arbitrary with a key produced by tox_derive_key_. The output 186/* Encrypt arbitrary with a key produced by tox_derive_key_*. The output
210 * array must be at least data_len + tox_pass_encryption_extra_length() bytes long. 187 * array must be at least data_len + TOX_PASS_ENCRYPTION_EXTRA_LENGTH bytes long.
211 * key must be tox_pass_key_length() bytes. 188 * key must be TOX_PASS_KEY_LENGTH bytes.
212 * If you already have a symmetric key from somewhere besides this module, simply 189 * If you already have a symmetric key from somewhere besides this module, simply
213 * call encrypt_data_symmetric in toxcore/crypto_core directly. 190 * call encrypt_data_symmetric in toxcore/crypto_core directly.
214 * 191 *
215 * returns 0 on success 192 * returns true on success
216 * returns -1 on failure
217 */
218int tox_pass_key_encrypt(const uint8_t *data, uint32_t data_len, const uint8_t *key, uint8_t *out);
219
220/* Save the messenger data encrypted with the given key from tox_derive_key.
221 * data must be at least tox_encrypted_size().
222 *
223 * NOTE: Unlike tox_save(), this function may fail. Be sure to check its return
224 * value.
225 *
226 * returns 0 on success
227 * returns -1 on failure
228 */ 193 */
229int tox_encrypted_key_save(const Tox *tox, uint8_t *data, uint8_t *key); 194bool tox_pass_key_encrypt(const uint8_t *data, size_t data_len, const uint8_t *key, uint8_t *out, TOX_ERR_ENCRYPTION *error);
230 195
231/* This is the inverse of tox_pass_key_encrypt, also using only keys produced by 196/* This is the inverse of tox_pass_key_encrypt, also using only keys produced by
232 * tox_derive_key_from_pass. 197 * tox_derive_key_from_pass.
233 * 198 *
234 * returns the length of the output data (== data_len - tox_pass_encryption_extra_length()) on success 199 * the output data has size data_length - TOX_PASS_ENCRYPTION_EXTRA_LENGTH
235 * returns -1 on failure
236 */
237int tox_pass_key_decrypt(const uint8_t *data, uint32_t length, const uint8_t *key, uint8_t *out);
238
239/* Load the messenger from encrypted data of size length, with key from tox_derive_key.
240 * All other arguments are like toxcore/tox_new().
241 * 200 *
242 * returns NULL on failure; see the documentation in toxcore/tox.h. 201 * returns true on success
243 */ 202 */
244Tox *tox_encrypted_key_new(const struct Tox_Options *options, const uint8_t *data, size_t length, uint8_t *key, 203bool tox_pass_key_decrypt(const uint8_t *data, size_t length, const uint8_t *key, uint8_t *out, TOX_ERR_DECRYPTION *error);
245 TOX_ERR_ENCRYPTED_NEW *error);
246
247 204
248/* Determines whether or not the given data is encrypted (by checking the magic number) 205/* Determines whether or not the given data is encrypted (by checking the magic number)
249 *
250 * returns 1 if it is encrypted
251 * returns 0 otherwise
252 */ 206 */
253int tox_is_data_encrypted(const uint8_t *data); 207bool tox_is_data_encrypted(const uint8_t *data);
254 208
255#ifdef __cplusplus 209#ifdef __cplusplus
256} 210}