Skip to content

Commit d10c966

Browse files
committed
feat: Add to_string functions for toxencryptsave errors.
Also added a tox save decryption tool.
1 parent 7bfd0dc commit d10c966

File tree

4 files changed

+147
-0
lines changed

4 files changed

+147
-0
lines changed

testing/BUILD.bazel

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,10 @@ cc_binary(
8989
"//c-toxcore/toxcore:mono_time",
9090
],
9191
)
92+
93+
cc_binary(
94+
name = "decrypt_save",
95+
testonly = 1,
96+
srcs = ["decrypt_save.c"],
97+
deps = ["//c-toxcore/toxencryptsave"],
98+
)

testing/decrypt_save.c

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/* SPDX-License-Identifier: GPL-3.0-or-later
2+
* Copyright © 2025 The TokTok team.
3+
*/
4+
#include "../toxencryptsave/toxencryptsave.h"
5+
6+
#include <stdio.h>
7+
#include <stdlib.h>
8+
#include <string.h>
9+
10+
// ./decrypt_save <password> <encrypted input> <decrypted output>
11+
int main(int argc, char *argv[])
12+
{
13+
if (argc != 4) {
14+
printf("Usage: %s <password> <encrypted input> <decrypted output>\n", argv[0]);
15+
return 1;
16+
}
17+
FILE *fp = fopen(argv[2], "rb");
18+
if (!fp) {
19+
printf("Could not open %s\n", argv[2]);
20+
return 1;
21+
}
22+
fseek(fp, 0, SEEK_END);
23+
size_t len = ftell(fp);
24+
fseek(fp, 0, SEEK_SET);
25+
uint8_t *data = (uint8_t *)malloc(len);
26+
if (!data) {
27+
printf("Could not allocate memory\n");
28+
fclose(fp);
29+
return 1;
30+
}
31+
32+
if (fread(data, 1, len, fp) != len) {
33+
printf("Could not read %s\n", argv[2]);
34+
free(data);
35+
fclose(fp);
36+
return 1;
37+
}
38+
fclose(fp);
39+
40+
uint8_t *plaintext = (uint8_t *)malloc(len);
41+
if (!plaintext) {
42+
printf("Could not allocate memory\n");
43+
free(data);
44+
return 1;
45+
}
46+
Tox_Err_Decryption error;
47+
if (!tox_pass_decrypt(data, len, (uint8_t *)argv[1], strlen(argv[1]), plaintext, &error)) {
48+
printf("Could not decrypt: %s\n", tox_err_decryption_to_string(error));
49+
free(data);
50+
free(plaintext);
51+
return 1;
52+
}
53+
fp = fopen(argv[3], "wb");
54+
if (!fp) {
55+
printf("Could not open %s\n", argv[3]);
56+
free(data);
57+
free(plaintext);
58+
return 1;
59+
}
60+
if (fwrite(plaintext, 1, len - TOX_PASS_ENCRYPTION_EXTRA_LENGTH, fp) != len - TOX_PASS_ENCRYPTION_EXTRA_LENGTH) {
61+
printf("Could not write %s\n", argv[3]);
62+
free(data);
63+
free(plaintext);
64+
fclose(fp);
65+
return 1;
66+
}
67+
free(data);
68+
free(plaintext);
69+
fclose(fp);
70+
71+
return 0;
72+
}

toxencryptsave/toxencryptsave.c

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,3 +395,63 @@ bool tox_is_data_encrypted(const uint8_t data[TOX_PASS_ENCRYPTION_EXTRA_LENGTH])
395395
{
396396
return memcmp(data, TOX_ENC_SAVE_MAGIC_NUMBER, TOX_ENC_SAVE_MAGIC_LENGTH) == 0;
397397
}
398+
399+
const char *tox_err_key_derivation_to_string(Tox_Err_Key_Derivation error)
400+
{
401+
switch (error) {
402+
case TOX_ERR_KEY_DERIVATION_OK:
403+
return "TOX_ERR_KEY_DERIVATION_OK";
404+
case TOX_ERR_KEY_DERIVATION_NULL:
405+
return "TOX_ERR_KEY_DERIVATION_NULL";
406+
case TOX_ERR_KEY_DERIVATION_FAILED:
407+
return "TOX_ERR_KEY_DERIVATION_FAILED";
408+
}
409+
return "<invalid Tox_Err_Key_Derivation>";
410+
}
411+
412+
const char *tox_err_encryption_to_string(Tox_Err_Encryption error)
413+
{
414+
switch (error) {
415+
case TOX_ERR_ENCRYPTION_OK:
416+
return "TOX_ERR_ENCRYPTION_OK";
417+
case TOX_ERR_ENCRYPTION_NULL:
418+
return "TOX_ERR_ENCRYPTION_NULL";
419+
case TOX_ERR_ENCRYPTION_KEY_DERIVATION_FAILED:
420+
return "TOX_ERR_ENCRYPTION_KEY_DERIVATION_FAILED";
421+
case TOX_ERR_ENCRYPTION_FAILED:
422+
return "TOX_ERR_ENCRYPTION_FAILED";
423+
}
424+
return "<invalid Tox_Err_Encryption>";
425+
}
426+
427+
const char *tox_err_decryption_to_string(Tox_Err_Decryption error)
428+
{
429+
switch (error) {
430+
case TOX_ERR_DECRYPTION_OK:
431+
return "TOX_ERR_DECRYPTION_OK";
432+
case TOX_ERR_DECRYPTION_NULL:
433+
return "TOX_ERR_DECRYPTION_NULL";
434+
case TOX_ERR_DECRYPTION_INVALID_LENGTH:
435+
return "TOX_ERR_DECRYPTION_INVALID_LENGTH";
436+
case TOX_ERR_DECRYPTION_BAD_FORMAT:
437+
return "TOX_ERR_DECRYPTION_BAD_FORMAT";
438+
case TOX_ERR_DECRYPTION_KEY_DERIVATION_FAILED:
439+
return "TOX_ERR_DECRYPTION_KEY_DERIVATION_FAILED";
440+
case TOX_ERR_DECRYPTION_FAILED:
441+
return "TOX_ERR_DECRYPTION_FAILED";
442+
}
443+
return "<invalid Tox_Err_Decryption>";
444+
}
445+
446+
const char *tox_err_get_salt_to_string(Tox_Err_Get_Salt error)
447+
{
448+
switch (error) {
449+
case TOX_ERR_GET_SALT_OK:
450+
return "TOX_ERR_GET_SALT_OK";
451+
case TOX_ERR_GET_SALT_NULL:
452+
return "TOX_ERR_GET_SALT_NULL";
453+
case TOX_ERR_GET_SALT_BAD_FORMAT:
454+
return "TOX_ERR_GET_SALT_BAD_FORMAT";
455+
}
456+
return "<invalid Tox_Err_Get_Salt>";
457+
}

toxencryptsave/toxencryptsave.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ typedef enum Tox_Err_Key_Derivation {
8888

8989
} Tox_Err_Key_Derivation;
9090

91+
const char *tox_err_key_derivation_to_string(Tox_Err_Key_Derivation error);
92+
9193
typedef enum Tox_Err_Encryption {
9294

9395
/**
@@ -114,6 +116,8 @@ typedef enum Tox_Err_Encryption {
114116

115117
} Tox_Err_Encryption;
116118

119+
const char *tox_err_encryption_to_string(Tox_Err_Encryption error);
120+
117121
typedef enum Tox_Err_Decryption {
118122

119123
/**
@@ -152,6 +156,8 @@ typedef enum Tox_Err_Decryption {
152156

153157
} Tox_Err_Decryption;
154158

159+
const char *tox_err_decryption_to_string(Tox_Err_Decryption error);
160+
155161
/*******************************************************************************
156162
*
157163
* BEGIN PART 1
@@ -313,6 +319,8 @@ typedef enum Tox_Err_Get_Salt {
313319

314320
} Tox_Err_Get_Salt;
315321

322+
const char *tox_err_get_salt_to_string(Tox_Err_Get_Salt error);
323+
316324
/**
317325
* Retrieves the salt used to encrypt the given data.
318326
*

0 commit comments

Comments
 (0)