summaryrefslogtreecommitdiff
path: root/src/encryption.h
blob: 5c6a08cbf9fd5f22662311538a6bc1092a8d416c (plain)
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
/**
 * @file encryption.h
 * @author syxhe (https://t.me/syxhe)
 * @brief A collection of all encryption related functions
 * @version 0.1
 * @date 2025-06-09
 * 
 * @copyright Copyright (c) 2025
 * 
 */

#ifndef __VXGG_REWRITE___ENCRYPTION_H___1481879318188___
#define __VXGG_REWRITE___ENCRYPTION_H___1481879318188___

#include <sodium.h>

// Determines whether any function that calls libsodium functions also checks to make sure libsodium is actually initialized. May
// cause unexpected issues with early exiting due to libsodium failing to initialize properly. It's recommended that you just
// manually run `sodium_init()` in some main or init function of your own so that you can deal with a potential error yourself
#define ___VXGG___ALWAYS_CHECK_LIBSODIUM___ 1

// Grants access to the `vxgg_setsodiumfailcb` function, which can be used to set a custom callback for what to do when libsodium
// fails upon initialization 
#define ___VXGG___USE_CLS_CALLBACK___ 1


#if ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0
// Checks if sodium is initialized. Initializes it if not. If `___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0`, it's possible to set an error callback to avoid exiting the entire program. Otherwise calls `error()` if libsodium can't initialize
void checksodium(void);

#if ___VXGG___USE_CLS_CALLBACK___ > 0
// Definition for the callback function that fires when a call to checksodium fails
typedef void (*vxgg_naclfailcb)(void*);

// Sets the error callback for when libsodium fails. Runs `cb(data)` if `(sodium_init() < 0)`
void vxgg_setsodiumfailcb(const vxgg_naclfailcb cb, void *data);
#endif
#endif

// Chunk size for en/de-cryption. I originally wanted to use st_blksize from stat(), but given that those chunks may be of different
// sizes between computers / filesystems / architectures / files, it's easier to just have this be a consistent macro
#define CHUNKSIZE (1 << 9)

// Fuck reading from a file. Even if someone ran strings on the binary and got this they wouldn't be able to regenerate the key
#define PASSWORD_WORDS (\
    (const char * const []){\
        "the", "of", "to", "and", "for", "our", "their", "has", "in", "he", "a", "them", "that", "these", "by", "have", "we", \
        "us", "people", "which", "all", "is", "with", "laws", "be", "are", "his", "states", "on", "they", "right", "it", "from", \
        "government", "such", "among", "powers", "most", "an", "time", "should", "new", "as", "been", "colonies", "assent", \
        "large", "at", "independent", "free", "united", "when", "mankind", "hold", "rights", "governments", "consent", "its", \
        "long", "themselves", "abolishing", "usurpations", "absolute", "repeated", "this", "world", "refused", "pass", "other", \
        "others", "without", "justice", "peace", "power", "seas", "war", "do", "declaration", "america", "becomes", "necessary", \
        "political", "equal", "declare", "causes", "separation", "men", "happiness", "any", "form", "alter", "or", "will", \
        "forms", "same", "object", "off", "necessity", "history", "great", "britain", "tyranny", "over", "public", "good", \
        "unless", "suspended", "so", "would", "legislature", "only", "legislative", "bodies", "purpose", "into", "dissolved", \
        "state", "endeavoured", "refusing", "hither", "conditions", "establishing", "offices", "out", "armies", "legislatures", \
        "render", "jurisdiction", "foreign", "acts", "pretended", "trial", "inhabitants", "cases", "transporting", "rule", \
        "declaring", "here", "protection", "against", "lives", "circumstances", "ages", "totally", "friends", "brethren", "whose", \
        "every", "may", "therefore", "ought", "unanimous", "thirteen", "course", "human", "events", "one", "dissolve", "bands", \
        "connected", "another", "assume", "earth", "separate", "station", "nature", "natures", "god", "entitle", "decent", \
        "respect", "opinions", "requires", "impel", "truths", "self", "evident", "created", "endowed", "creator", "certain", \
        "unalienable", "life", "liberty", "pursuit", "secure", "instituted", "deriving", "just", "governed", "whenever", \
        "destructive", "ends", "abolish", "institute", "laying", "foundation", "principles", "organizing", "shall", "seem", \
        "likely", "effect", "safety", "prudence", "indeed", "dictate", "established", "not", "changed", "light", "transient", \
        "accordingly", "experience", "hath", "shewn", "more", "disposed", "suffer", "while", "evils", "sufferable", "than", \
        "accustomed", "but", "train", "abuses", "pursuing", "invariably", "evinces", "design", "reduce", "under", "despotism", \
        "duty", "throw", "provide", "guards", "future", "security", "patient", "sufferance", "now", "constrains", "former", \
        "systems", "present", "king", "injuries", "having", "direct", "establishment", "prove", "let", "facts", "submitted", \
        "candid", "wholesome", "forbidden", "governors", "immediate", "pressing", "importance", "operation", "till", "obtained", \
        "utterly", "neglected", "attend", "accommodation", "districts", "those", "relinquish", "representation", "inestimable", \
        "formidable", "tyrants", "called", "together", "places", "unusual", "uncomfortable", "distant", "depository", "records", \
        "sole", "fatiguing", "compliance", "measures", "representative", "houses", "repeatedly", "opposing", "manly", "firmness", \
        "invasions", "after", "dissolutions", "cause", "elected", "whereby", "incapable", "annihilation", "returned", "exercise", \
        "remaining", "mean", "exposed", "dangers", "invasion", "convulsions", "within", "prevent", "population", "obstructing", \
        "naturalization", "foreigners", "encourage", "migrations", "raising", "appropriations", "lands", "obstructed", \
        "administration", "judiciary", "made", "judges", "dependent", "alone", "tenure", "amount", "payment", "salaries", \
        "erected", "multitude", "sent", "swarms", "officers", "harrass", "eat", "substance", "kept", "times", "standing", \
        "affected", "military", "superior", "civil", "combined", "subject", "constitution", "unacknowledged", "giving", \
        "legislation", "quartering", "armed", "troops", "protecting", "mock", "punishment", "murders", "commit", "cutting", \
        "trade", "parts", "imposing", "taxes", "depriving", "many", "benefits", "jury", "beyond", "tried", "offences", "system", \
        "english", "neighbouring", "province", "therein", "arbitrary", "enlarging", "boundaries", "once", "example", "fit", \
        "instrument", "introducing", "taking", "away", "charters", "valuable", "altering", "fundamentally", "suspending", "own", \
        "invested", "legislate", "whatsoever", "abdicated", "waging", "plundered", "ravaged", "coasts", "burnt", "towns", \
        "destroyed", "mercenaries", "compleat", "works", "death", "desolation", "already", "begun", "cruelty", "perfidy", \
        "scarcely", "paralleled", "barbarous", "unworthy", "head", "civilized", "nation", "constrained", "fellow", "citizens", \
        "taken", "captive", "high", "bear", "arms", "country", "become", "executioners", "fall", "hands", "excited", "domestic", \
        "insurrections", "amongst", "bring", "frontiers", "merciless", "indian", "savages", "known", "warfare", "undistinguished", \
        "destruction", "sexes", "stage", "oppressions", "petitioned", "redress", "humble", "terms", "petitions", "answered", \
        "injury", "prince", "character", "thus", "marked", "act", "define", "tyrant", "unfit", "ruler", "nor", "wanting", \
        "attentions", "brittish", "warned", "attempts", "extend", "unwarrantable", "reminded", "emigration", "settlement", \
        "appealed", "native", "magnanimity", "conjured", "ties", "common", "kindred", "disavow", "inevitably", "interrupt", \
        "connections", "correspondence", "too", "deaf", "voice", "consanguinity", "must", "acquiesce", "denounces", "rest", \
        "enemies", "representatives", "general", "congress", "assembled", "appealing", "supreme", "judge", "rectitude", \
        "intentions", "name", "authority", "solemnly", "publish", "absolved", "allegiance", "british", "crown", "connection", \
        "between", "full", "levy", "conclude", "contract", "alliances", "establish", "commerce", "things", "support", "firm", \
        "reliance", "divine", "providence", "mutually", "pledge", "each", "fortunes", "sacred", "honor"\
    }\
)
#define PASSWORD_WORDS_LEN (STATIC_ARRAY_LEN(PASSWORD_WORDS))

// open() with the flags O_TMPFILE, O_WRONLY, O_CLOEXEC, and O_SYNC. Opened with mode S_IRUSR, S_IWUSR
int maketmp(const char * const dest);

// Encrypt src to dst using libsodium's xchacha encryption suite
int encrypttofile(FILE *src, FILE *dst, const unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]);

// Decrypt src to dst using libsodium's xchacha encryption suite
int decrypttofile(FILE *src, FILE *dst, const unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]);

// Encrypt file at `target` to `output` using Linux's named temp file system to do it in the background
int encryptviatmp(const char * const target, const char * const output, const unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]);

// Decrypt the file at `encrypted` to `target`
int decryptto(const char * const encrypted, const char * const target, const unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]);

//
int linkto(const char * const target, int tgfd);

//
int genpassword(char **str, unsigned int words);

#endif