summaryrefslogtreecommitdiff
path: root/src/encryption.h
diff options
context:
space:
mode:
author@syxhe <https://t.me/syxhe>2025-10-20 15:51:49 -0500
committer@syxhe <https://t.me/syxhe>2025-10-20 15:51:49 -0500
commit33647dd2c4f4af7c7cb7d99c1a6dac7f1d059a67 (patch)
tree12925795f7ecc39e566ff78f18c3bfab5539e32f /src/encryption.h
parentd5de3e243a8481ad879dd8effc5759d406dc90b7 (diff)
Switch to unity build
Diffstat (limited to 'src/encryption.h')
-rw-r--r--src/encryption.h185
1 files changed, 0 insertions, 185 deletions
diff --git a/src/encryption.h b/src/encryption.h
deleted file mode 100644
index a23cbdf..0000000
--- a/src/encryption.h
+++ /dev/null
@@ -1,185 +0,0 @@
1/**
2 * @file encryption.h
3 * @author syxhe (https://t.me/syxhe)
4 * @brief A collection of all encryption related functions
5 * @version 0.1
6 * @date 2025-06-09
7 *
8 * @copyright Copyright (c) 2025
9 *
10 */
11
12#ifndef __VXGG_REWRITE___ENCRYPTION_H___1481879318188___
13#define __VXGG_REWRITE___ENCRYPTION_H___1481879318188___
14
15#include <sodium.h>
16#include "shared.h"
17
18/// Determines whether any function that calls libsodium functions also checks to make sure libsodium is actually initialized. May
19/// cause unexpected issues with early exiting due to libsodium failing to initialize properly. It's recommended that you just
20/// manually run `sodium_init()` in some main or init function of your own so that you can deal with a potential error yourself
21#define ___VXGG___ALWAYS_CHECK_LIBSODIUM___ 1
22
23/// Grants access to the `vxgg_setsodiumfailcb` function, which can be used to set a custom callback for what to do when libsodium
24/// fails upon initialization
25#define ___VXGG___USE_CLS_CALLBACK___ 1
26
27
28#if ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0
29#if ___VXGG___USE_CLS_CALLBACK___ > 0
30//! Definition for the callback function that fires when a call to checksodium fails
31typedef void (*vxgg_naclfailcb)(void*);
32
33/**
34 * @brief Sets a callback and data pair to be ran if/when sodium fails to initialize
35 *
36 * @param cb The new callback to set. Must be non-null
37 * @param data The data to be fed to the callback. May be null
38 */
39void vxgg_setsodiumfailcb(const vxgg_naclfailcb cb, void *data);
40#endif
41#endif
42
43/// Chunk size for en/decryption. I originally wanted to use st_blksize from stat(), but given that those chunks may be of different
44/// sizes between computers / filesystems / architectures / files, it's easier to just have this be a consistent macro
45#define CHUNKSIZE (1 << 9)
46
47const static char * test = "this is a test";
48
49// 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
50//! A list of possible words for password creation
51#define PASSWORD_WORDS (\
52 (const char * const []){\
53 "the", "of", "to", "and", "for", "our", "their", "has", "in", "he", "a", "them", "that", "these", "by", "have", "we", \
54 "us", "people", "which", "all", "is", "with", "laws", "be", "are", "his", "states", "on", "they", "right", "it", "from", \
55 "government", "such", "among", "powers", "most", "an", "time", "should", "new", "as", "been", "colonies", "assent", \
56 "large", "at", "independent", "free", "united", "when", "mankind", "hold", "rights", "governments", "consent", "its", \
57 "long", "themselves", "abolishing", "usurpations", "absolute", "repeated", "this", "world", "refused", "pass", "other", \
58 "others", "without", "justice", "peace", "power", "seas", "war", "do", "declaration", "america", "becomes", "necessary", \
59 "political", "equal", "declare", "causes", "separation", "men", "happiness", "any", "form", "alter", "or", "will", \
60 "forms", "same", "object", "off", "necessity", "history", "great", "britain", "tyranny", "over", "public", "good", \
61 "unless", "suspended", "so", "would", "legislature", "only", "legislative", "bodies", "purpose", "into", "dissolved", \
62 "state", "endeavoured", "refusing", "hither", "conditions", "establishing", "offices", "out", "armies", "legislatures", \
63 "render", "jurisdiction", "foreign", "acts", "pretended", "trial", "inhabitants", "cases", "transporting", "rule", \
64 "declaring", "here", "protection", "against", "lives", "circumstances", "ages", "totally", "friends", "brethren", "whose", \
65 "every", "may", "therefore", "ought", "unanimous", "thirteen", "course", "human", "events", "one", "dissolve", "bands", \
66 "connected", "another", "assume", "earth", "separate", "station", "nature", "natures", "god", "entitle", "decent", \
67 "respect", "opinions", "requires", "impel", "truths", "self", "evident", "created", "endowed", "creator", "certain", \
68 "unalienable", "life", "liberty", "pursuit", "secure", "instituted", "deriving", "just", "governed", "whenever", \
69 "destructive", "ends", "abolish", "institute", "laying", "foundation", "principles", "organizing", "shall", "seem", \
70 "likely", "effect", "safety", "prudence", "indeed", "dictate", "established", "not", "changed", "light", "transient", \
71 "accordingly", "experience", "hath", "shewn", "more", "disposed", "suffer", "while", "evils", "sufferable", "than", \
72 "accustomed", "but", "train", "abuses", "pursuing", "invariably", "evinces", "design", "reduce", "under", "despotism", \
73 "duty", "throw", "provide", "guards", "future", "security", "patient", "sufferance", "now", "constrains", "former", \
74 "systems", "present", "king", "injuries", "having", "direct", "establishment", "prove", "let", "facts", "submitted", \
75 "candid", "wholesome", "forbidden", "governors", "immediate", "pressing", "importance", "operation", "till", "obtained", \
76 "utterly", "neglected", "attend", "accommodation", "districts", "those", "relinquish", "representation", "inestimable", \
77 "formidable", "tyrants", "called", "together", "places", "unusual", "uncomfortable", "distant", "depository", "records", \
78 "sole", "fatiguing", "compliance", "measures", "representative", "houses", "repeatedly", "opposing", "manly", "firmness", \
79 "invasions", "after", "dissolutions", "cause", "elected", "whereby", "incapable", "annihilation", "returned", "exercise", \
80 "remaining", "mean", "exposed", "dangers", "invasion", "convulsions", "within", "prevent", "population", "obstructing", \
81 "naturalization", "foreigners", "encourage", "migrations", "raising", "appropriations", "lands", "obstructed", \
82 "administration", "judiciary", "made", "judges", "dependent", "alone", "tenure", "amount", "payment", "salaries", \
83 "erected", "multitude", "sent", "swarms", "officers", "harrass", "eat", "substance", "kept", "times", "standing", \
84 "affected", "military", "superior", "civil", "combined", "subject", "constitution", "unacknowledged", "giving", \
85 "legislation", "quartering", "armed", "troops", "protecting", "mock", "punishment", "murders", "commit", "cutting", \
86 "trade", "parts", "imposing", "taxes", "depriving", "many", "benefits", "jury", "beyond", "tried", "offences", "system", \
87 "english", "neighbouring", "province", "therein", "arbitrary", "enlarging", "boundaries", "once", "example", "fit", \
88 "instrument", "introducing", "taking", "away", "charters", "valuable", "altering", "fundamentally", "suspending", "own", \
89 "invested", "legislate", "whatsoever", "abdicated", "waging", "plundered", "ravaged", "coasts", "burnt", "towns", \
90 "destroyed", "mercenaries", "compleat", "works", "death", "desolation", "already", "begun", "cruelty", "perfidy", \
91 "scarcely", "paralleled", "barbarous", "unworthy", "head", "civilized", "nation", "constrained", "fellow", "citizens", \
92 "taken", "captive", "high", "bear", "arms", "country", "become", "executioners", "fall", "hands", "excited", "domestic", \
93 "insurrections", "amongst", "bring", "frontiers", "merciless", "indian", "savages", "known", "warfare", "undistinguished", \
94 "destruction", "sexes", "stage", "oppressions", "petitioned", "redress", "humble", "terms", "petitions", "answered", \
95 "injury", "prince", "character", "thus", "marked", "act", "define", "tyrant", "unfit", "ruler", "nor", "wanting", \
96 "attentions", "brittish", "warned", "attempts", "extend", "unwarrantable", "reminded", "emigration", "settlement", \
97 "appealed", "native", "magnanimity", "conjured", "ties", "common", "kindred", "disavow", "inevitably", "interrupt", \
98 "connections", "correspondence", "too", "deaf", "voice", "consanguinity", "must", "acquiesce", "denounces", "rest", \
99 "enemies", "representatives", "general", "congress", "assembled", "appealing", "supreme", "judge", "rectitude", \
100 "intentions", "name", "authority", "solemnly", "publish", "absolved", "allegiance", "british", "crown", "connection", \
101 "between", "full", "levy", "conclude", "contract", "alliances", "establish", "commerce", "things", "support", "firm", \
102 "reliance", "divine", "providence", "mutually", "pledge", "each", "fortunes", "sacred", "honor"\
103 }\
104)
105//! Short macro for getting the `PASSWORD_WORDS` array size
106#define PASSWORD_WORDS_LEN (STATIC_ARRAY_LEN(PASSWORD_WORDS))
107
108/**
109 * @brief open() with the flags O_TMPFILE, O_WRONLY, O_CLOEXEC, and O_SYNC. Opened with mode S_IRUSR, S_IWUSR
110 *
111 * @param dest The filename the new descriptor should have. Must be non-null
112 * @retval (int)[-1,int] A new file descriptor. -1 on error
113 */
114int maketmp(const char * const dest);
115
116/**
117 * @brief Encrypt src to dst using libsodium's xchacha encryption suite
118 *
119 * @param src File to encrypt
120 * @param dst Destination to write encrypted file
121 * @param key Key for encryption
122 * @retval (int)[-1, 0] Returns 0 on success, sets errno and returns -1 on error
123 */
124int encrypttofile(FILE *src, FILE *dst, const unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]);
125
126/**
127 * @brief Decrypt src to dst using libsodium's xchacha encryption suite
128 *
129 * @param src File to decrypt
130 * @param dst Destination to write decrypted file
131 * @param key Key used to encrypt
132 * @retval (int)[-1, 0] Returns 0 on success, sets errno and returns -1 on error
133 */
134int decrypttofile(FILE *src, FILE *dst, const unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]);
135
136/**
137 * @brief Encrypt file at `target` to `output` using Linux's named temp file system to do it in the background
138 *
139 * @param target
140 * @param output
141 * @param key
142 * @retval (int)[,]
143 */
144int encryptviatmp(const char * const target, const char * const output, const unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]);
145
146/**
147 * @brief Decrypt the file at `encrypted` to `target`
148 *
149 * @param encrypted
150 * @param target
151 * @param key
152 * @retval (int)[,]
153 */
154int decryptto(const char * const encrypted, const char * const target, const unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]);
155
156/**
157 * @brief Link a file descriptor into the filesystem
158 *
159 * @param target New filename the descriptor should have
160 * @param tgfd The file descriptor to link
161 * @retval (int)[-1, 0] 0 on success, -1 on error
162 */
163int linkto(const char * const target, int tgfd);
164
165/**
166 * @brief Generate a password viable for use in the derivation of a key
167 *
168 * @param str Pointer to a string. This will be filled by a malloc'ed string of words (the password). Must be non-null
169 * @param words The number of words to include in the password. A password of at least 20 words and probably not more than 40 is recommended
170 * @retval (int)[-1, words] On success, returns the number of words requested. On error, returns -1 and sets errno
171 */
172int genpassword(char **str, unsigned int words);
173
174/**
175 * @brief sodium_malloc wrapper.
176 *
177 * Calls `error()` or `abort()` depnding on the value of `___VXGG___XALLOC_EXIT_ON_ERROR___`. Will make sure libsodium is initialized if `___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0`
178 *
179 * @param size
180 * @retval (void*) A pointer to some data allocated via `sodium_malloc()`
181 */
182void* xsodium_malloc(size_t size);
183
184
185#endif \ No newline at end of file