diff options
Diffstat (limited to 'toxencryptsave/toxencryptsave.h')
-rw-r--r-- | toxencryptsave/toxencryptsave.h | 200 |
1 files changed, 120 insertions, 80 deletions
diff --git a/toxencryptsave/toxencryptsave.h b/toxencryptsave/toxencryptsave.h index 169f736c..c077d899 100644 --- a/toxencryptsave/toxencryptsave.h +++ b/toxencryptsave/toxencryptsave.h | |||
@@ -29,27 +29,20 @@ extern "C" { | |||
29 | #endif | 29 | #endif |
30 | 30 | ||
31 | #include <stdint.h> | 31 | #include <stdint.h> |
32 | #include <stddef.h> | ||
33 | #include <stdbool.h> | ||
32 | 34 | ||
33 | #ifndef __TOX_DEFINED__ | 35 | #ifndef TOX_DEFINED |
34 | #define __TOX_DEFINED__ | 36 | #define TOX_DEFINED |
35 | typedef struct Tox Tox; | 37 | typedef struct Tox Tox; |
38 | struct Tox_Options; | ||
36 | #endif | 39 | #endif |
37 | 40 | ||
38 | // these functions provide access to these defines in toxencryptsave.c, which | 41 | #define TOX_PASS_SALT_LENGTH 32 |
39 | // otherwise aren't actually available in clients... | 42 | #define TOX_PASS_KEY_LENGTH 32 |
40 | int tox_pass_encryption_extra_length(); | 43 | #define TOX_PASS_ENCRYPTION_EXTRA_LENGTH 80 |
41 | 44 | ||
42 | int tox_pass_key_length(); | 45 | /* This module is conceptually organized into two parts. The first part are the functions |
43 | |||
44 | int tox_pass_salt_length(); | ||
45 | |||
46 | /* return size of the messenger data (for encrypted Messenger saving). */ | ||
47 | uint32_t tox_encrypted_size(const Tox *tox); | ||
48 | |||
49 | /* This "module" provides functions analogous to tox_load and tox_save in toxcore, | ||
50 | * as well as functions for encryption of arbitrary client data (e.g. chat logs). | ||
51 | * | ||
52 | * It is conceptually organized into two parts. The first part are the functions | ||
53 | * 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 |
54 | * 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 |
55 | * 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, |
@@ -59,13 +52,83 @@ uint32_t tox_encrypted_size(const Tox *tox); | |||
59 | * favor using the first part intead of the second part. | 52 | * favor using the first part intead of the second part. |
60 | * | 53 | * |
61 | * 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 |
62 | * (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. | ||
63 | * | 57 | * |
64 | * 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 |
65 | * becomes corrupted, the data will be entirely unrecoverable. | 59 | * becomes corrupted, the data will be entirely unrecoverable. |
66 | * 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. |
67 | */ | 61 | */ |
68 | 62 | ||
63 | /* Since apparently no one actually bothered to learn about the module previously, | ||
64 | * the recently removed functions tox_encrypted_new and tox_get_encrypted_savedata | ||
65 | * may be trivially replaced by calls to tox_pass_decrypt -> tox_new or | ||
66 | * tox_get_savedata -> tox_pass_encrypt as appropriate. The removed functions | ||
67 | * were never more than 5 line wrappers of the other public API functions anyways. | ||
68 | * (As has always been, tox_pass_decrypt and tox_pass_encrypt are interchangeable | ||
69 | * with tox_pass_key_decrypt and tox_pass_key_encrypt, as the client program requires.) | ||
70 | */ | ||
71 | |||
72 | typedef enum TOX_ERR_KEY_DERIVATION { | ||
73 | TOX_ERR_KEY_DERIVATION_OK, | ||
74 | /** | ||
75 | * Some input data, or maybe the output pointer, was null. | ||
76 | */ | ||
77 | TOX_ERR_KEY_DERIVATION_NULL, | ||
78 | /** | ||
79 | * The crypto lib was unable to derive a key from the given passphrase, | ||
80 | * which is usually a lack of memory issue. The functions accepting keys | ||
81 | * do not produce this error. | ||
82 | */ | ||
83 | TOX_ERR_KEY_DERIVATION_FAILED | ||
84 | } TOX_ERR_KEY_DERIVATION; | ||
85 | |||
86 | typedef enum TOX_ERR_ENCRYPTION { | ||
87 | TOX_ERR_ENCRYPTION_OK, | ||
88 | /** | ||
89 | * Some input data, or maybe the output pointer, was null. | ||
90 | */ | ||
91 | TOX_ERR_ENCRYPTION_NULL, | ||
92 | /** | ||
93 | * The crypto lib was unable to derive a key from the given passphrase, | ||
94 | * which is usually a lack of memory issue. The functions accepting keys | ||
95 | * do not produce this error. | ||
96 | */ | ||
97 | TOX_ERR_ENCRYPTION_KEY_DERIVATION_FAILED, | ||
98 | /** | ||
99 | * The encryption itself failed. | ||
100 | */ | ||
101 | TOX_ERR_ENCRYPTION_FAILED | ||
102 | } TOX_ERR_ENCRYPTION; | ||
103 | |||
104 | typedef enum TOX_ERR_DECRYPTION { | ||
105 | TOX_ERR_DECRYPTION_OK, | ||
106 | /** | ||
107 | * Some input data, or maybe the output pointer, was null. | ||
108 | */ | ||
109 | TOX_ERR_DECRYPTION_NULL, | ||
110 | /** | ||
111 | * The input data was shorter than TOX_PASS_ENCRYPTION_EXTRA_LENGTH bytes | ||
112 | */ | ||
113 | TOX_ERR_DECRYPTION_INVALID_LENGTH, | ||
114 | /** | ||
115 | * The input data is missing the magic number (i.e. wasn't created by this | ||
116 | * module, or is corrupted) | ||
117 | */ | ||
118 | TOX_ERR_DECRYPTION_BAD_FORMAT, | ||
119 | /** | ||
120 | * The crypto lib was unable to derive a key from the given passphrase, | ||
121 | * which is usually a lack of memory issue. The functions accepting keys | ||
122 | * do not produce this error. | ||
123 | */ | ||
124 | TOX_ERR_DECRYPTION_KEY_DERIVATION_FAILED, | ||
125 | /** | ||
126 | * The encrypted byte array could not be decrypted. Either the data was | ||
127 | * corrupt or the password/key was incorrect. | ||
128 | */ | ||
129 | TOX_ERR_DECRYPTION_FAILED | ||
130 | } TOX_ERR_DECRYPTION; | ||
131 | |||
69 | 132 | ||
70 | /******************************* BEGIN PART 2 ******************************* | 133 | /******************************* BEGIN PART 2 ******************************* |
71 | * For simplicty, the second part of the module is presented first. The API for | 134 | * For simplicty, the second part of the module is presented first. The API for |
@@ -75,41 +138,25 @@ uint32_t tox_encrypted_size(const Tox *tox); | |||
75 | */ | 138 | */ |
76 | 139 | ||
77 | /* Encrypts the given data with the given passphrase. The output array must be | 140 | /* Encrypts the given data with the given passphrase. The output array must be |
78 | * at least data_len + tox_pass_encryption_extra_length() bytes long. This delegates | 141 | * at least data_len + TOX_PASS_ENCRYPTION_EXTRA_LENGTH bytes long. This delegates |
79 | * to tox_derive_key_from_pass and tox_pass_key_encrypt. | 142 | * to tox_derive_key_from_pass and tox_pass_key_encrypt. |
80 | * | 143 | * |
81 | * tox_encrypted_save() is a good example of how to use this function. | 144 | * returns true on success |
82 | * | ||
83 | * returns 0 on success | ||
84 | * returns -1 on failure | ||
85 | */ | 145 | */ |
86 | int tox_pass_encrypt(const uint8_t *data, uint32_t data_len, uint8_t *passphrase, uint32_t pplength, uint8_t *out); | 146 | bool tox_pass_encrypt(const uint8_t *data, size_t data_len, uint8_t *passphrase, size_t pplength, uint8_t *out, |
147 | TOX_ERR_ENCRYPTION *error); | ||
87 | 148 | ||
88 | /* Save the messenger data encrypted with the given password. | ||
89 | * data must be at least tox_encrypted_size(). | ||
90 | * | ||
91 | * returns 0 on success | ||
92 | * returns -1 on failure | ||
93 | */ | ||
94 | int tox_encrypted_save(const Tox *tox, uint8_t *data, uint8_t *passphrase, uint32_t pplength); | ||
95 | 149 | ||
96 | /* Decrypts the given data with the given passphrase. The output array must be | 150 | /* Decrypts the given data with the given passphrase. The output array must be |
97 | * at least data_len - tox_pass_encryption_extra_length() bytes long. This delegates | 151 | * at least data_len - TOX_PASS_ENCRYPTION_EXTRA_LENGTH bytes long. This delegates |
98 | * to tox_pass_key_decrypt. | 152 | * to tox_pass_key_decrypt. |
99 | * | 153 | * |
100 | * tox_encrypted_load() is a good example of how to use this function. | 154 | * the output data has size data_length - TOX_PASS_ENCRYPTION_EXTRA_LENGTH |
101 | * | 155 | * |
102 | * returns the length of the output data (== data_len - tox_pass_encryption_extra_length()) on success | 156 | * returns true on success |
103 | * returns -1 on failure | ||
104 | */ | 157 | */ |
105 | int tox_pass_decrypt(const uint8_t *data, uint32_t length, uint8_t *passphrase, uint32_t pplength, uint8_t *out); | 158 | bool tox_pass_decrypt(const uint8_t *data, size_t length, uint8_t *passphrase, size_t pplength, uint8_t *out, |
106 | 159 | TOX_ERR_DECRYPTION *error); | |
107 | /* Load the messenger from encrypted data of size length. | ||
108 | * | ||
109 | * returns 0 on success | ||
110 | * returns -1 on failure | ||
111 | */ | ||
112 | int tox_encrypted_load(Tox *tox, const uint8_t *data, uint32_t length, uint8_t *passphrase, uint32_t pplength); | ||
113 | 160 | ||
114 | 161 | ||
115 | /******************************* BEGIN PART 1 ******************************* | 162 | /******************************* BEGIN PART 1 ******************************* |
@@ -117,8 +164,16 @@ int tox_encrypted_load(Tox *tox, const uint8_t *data, uint32_t length, uint8_t * | |||
117 | * intensive than part one. The first 3 functions are for key handling. | 164 | * intensive than part one. The first 3 functions are for key handling. |
118 | */ | 165 | */ |
119 | 166 | ||
167 | /* This key structure's internals should not be used by any client program, even | ||
168 | * if they are straightforward here. | ||
169 | */ | ||
170 | typedef struct { | ||
171 | uint8_t salt[TOX_PASS_SALT_LENGTH]; | ||
172 | uint8_t key[TOX_PASS_KEY_LENGTH]; | ||
173 | } TOX_PASS_KEY; | ||
174 | |||
120 | /* Generates a secret symmetric key from the given passphrase. out_key must be at least | 175 | /* Generates a secret symmetric key from the given passphrase. out_key must be at least |
121 | * tox_pass_key_length() bytes long. | 176 | * TOX_PASS_KEY_LENGTH bytes long. |
122 | * Be sure to not compromise the key! Only keep it in memory, do not write to disk. | 177 | * Be sure to not compromise the key! Only keep it in memory, do not write to disk. |
123 | * The password is zeroed after key derivation. | 178 | * The password is zeroed after key derivation. |
124 | * The key should only be used with the other functions in this module, as it | 179 | * The key should only be used with the other functions in this module, as it |
@@ -126,68 +181,53 @@ int tox_encrypted_load(Tox *tox, const uint8_t *data, uint32_t length, uint8_t * | |||
126 | * Note that this function is not deterministic; to derive the same key from a | 181 | * Note that this function is not deterministic; to derive the same key from a |
127 | * password, you also must know the random salt that was used. See below. | 182 | * password, you also must know the random salt that was used. See below. |
128 | * | 183 | * |
129 | * returns 0 on success | 184 | * returns true on success |
130 | * returns -1 on failure | ||
131 | */ | 185 | */ |
132 | int tox_derive_key_from_pass(uint8_t *passphrase, uint32_t pplength, uint8_t *out_key); | 186 | bool tox_derive_key_from_pass(uint8_t *passphrase, size_t pplength, TOX_PASS_KEY *out_key, |
187 | TOX_ERR_KEY_DERIVATION *error); | ||
133 | 188 | ||
134 | /* Same as above, except with use the given salt for deterministic key derivation. | 189 | /* Same as above, except use the given salt for deterministic key derivation. |
135 | * The salt must be tox_salt_length() bytes in length. | 190 | * The salt must be TOX_PASS_SALT_LENGTH bytes in length. |
136 | */ | 191 | */ |
137 | int tox_derive_key_with_salt(uint8_t *passphrase, uint32_t pplength, uint8_t *salt, uint8_t *out_key); | 192 | bool tox_derive_key_with_salt(uint8_t *passphrase, size_t pplength, uint8_t *salt, TOX_PASS_KEY *out_key, |
193 | TOX_ERR_KEY_DERIVATION *error); | ||
138 | 194 | ||
139 | /* This retrieves the salt used to encrypt the given data, which can then be passed to | 195 | /* This retrieves the salt used to encrypt the given data, which can then be passed to |
140 | * derive_key_with_salt to produce the same key as was previously used. Any encrpyted | 196 | * derive_key_with_salt to produce the same key as was previously used. Any encrpyted |
141 | * data with this module can be used as input. | 197 | * data with this module can be used as input. |
142 | * | 198 | * |
143 | * returns -1 if the magic number is wrong | 199 | * returns true if magic number matches |
144 | * returns 0 otherwise (no guarantee about validity of data) | 200 | * success does not say anything about the validity of the data, only that data of |
201 | * the appropriate size was copied | ||
145 | */ | 202 | */ |
146 | int tox_get_salt(uint8_t *data, uint8_t *salt); | 203 | bool tox_get_salt(const uint8_t *data, uint8_t *salt); |
147 | 204 | ||
148 | /* Now come the functions that are analogous to the part 2 functions. */ | 205 | /* Now come the functions that are analogous to the part 2 functions. */ |
149 | 206 | ||
150 | /* Encrypt arbitrary with a key produced by tox_derive_key_. The output | 207 | /* Encrypt arbitrary with a key produced by tox_derive_key_*. The output |
151 | * array must be at least data_len + tox_pass_encryption_extra_length() bytes long. | 208 | * array must be at least data_len + TOX_PASS_ENCRYPTION_EXTRA_LENGTH bytes long. |
152 | * key must be tox_pass_key_length() bytes. | 209 | * key must be TOX_PASS_KEY_LENGTH bytes. |
153 | * If you already have a symmetric key from somewhere besides this module, simply | 210 | * If you already have a symmetric key from somewhere besides this module, simply |
154 | * call encrypt_data_symmetric in toxcore/crypto_core directly. | 211 | * call encrypt_data_symmetric in toxcore/crypto_core directly. |
155 | * | 212 | * |
156 | * returns 0 on success | 213 | * returns true on success |
157 | * returns -1 on failure | ||
158 | */ | 214 | */ |
159 | int tox_pass_key_encrypt(const uint8_t *data, uint32_t data_len, const uint8_t *key, uint8_t *out); | 215 | bool tox_pass_key_encrypt(const uint8_t *data, size_t data_len, const TOX_PASS_KEY *key, uint8_t *out, |
160 | 216 | TOX_ERR_ENCRYPTION *error); | |
161 | /* Save the messenger data encrypted with the given key from tox_derive_key. | ||
162 | * data must be at least tox_encrypted_size(). | ||
163 | * | ||
164 | * returns 0 on success | ||
165 | * returns -1 on failure | ||
166 | */ | ||
167 | int tox_encrypted_key_save(const Tox *tox, uint8_t *data, uint8_t *key); | ||
168 | 217 | ||
169 | /* This is the inverse of tox_pass_key_encrypt, also using only keys produced by | 218 | /* This is the inverse of tox_pass_key_encrypt, also using only keys produced by |
170 | * tox_derive_key_from_pass. | 219 | * tox_derive_key_from_pass. |
171 | * | 220 | * |
172 | * returns the length of the output data (== data_len - tox_pass_encryption_extra_length()) on success | 221 | * the output data has size data_length - TOX_PASS_ENCRYPTION_EXTRA_LENGTH |
173 | * returns -1 on failure | ||
174 | */ | ||
175 | int tox_pass_key_decrypt(const uint8_t *data, uint32_t length, const uint8_t *key, uint8_t *out); | ||
176 | |||
177 | /* Load the messenger from encrypted data of size length, with key from tox_derive_key. | ||
178 | * | 222 | * |
179 | * returns 0 on success | 223 | * returns true on success |
180 | * returns -1 on failure | ||
181 | */ | 224 | */ |
182 | int tox_encrypted_key_load(Tox *tox, const uint8_t *data, uint32_t length, uint8_t *key); | 225 | bool tox_pass_key_decrypt(const uint8_t *data, size_t length, const TOX_PASS_KEY *key, uint8_t *out, |
226 | TOX_ERR_DECRYPTION *error); | ||
183 | 227 | ||
184 | /* Determines whether or not the given data is encrypted (by checking the magic number) | 228 | /* Determines whether or not the given data is encrypted (by checking the magic number) |
185 | * | ||
186 | * returns 1 if it is encrypted | ||
187 | * returns 0 otherwise | ||
188 | */ | 229 | */ |
189 | int tox_is_data_encrypted(const uint8_t *data); | 230 | bool tox_is_data_encrypted(const uint8_t *data); |
190 | int tox_is_save_encrypted(const uint8_t *data); // poorly-named alias for backwards compat (oh irony...) | ||
191 | 231 | ||
192 | #ifdef __cplusplus | 232 | #ifdef __cplusplus |
193 | } | 233 | } |