-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrypto.cpp
More file actions
200 lines (170 loc) · 8.83 KB
/
Copy pathcrypto.cpp
File metadata and controls
200 lines (170 loc) · 8.83 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include "crypto.hpp"
#include "fileops.hpp"
#include "logger.hpp"
#include <openssl/evp.h>
#include <openssl/rand.h>
#include <openssl/sha.h>
#include <cstdio>
#include <stdexcept>
#include <vector>
#include <cstring>
#include <filesystem>
namespace sflk {
static void ensure(bool cond, const char* msg){ if(!cond) throw std::runtime_error(msg); }
std::vector<uint8_t> derive_key(const std::string& password,
const std::vector<uint8_t>& salt,
uint32_t iterations) {
std::vector<uint8_t> key(32);
ensure(PKCS5_PBKDF2_HMAC(password.c_str(), password.size(),
salt.data(), salt.size(),
iterations, EVP_sha256(), key.size(), key.data())==1,
"PBKDF2 failed");
return key;
}
static void write_header(FILE* out, const Meta& m) {
const char magic[4] = {'S','F','L','K'};
uint8_t version = 1;
uint32_t iters = m.iterations;
uint8_t saltLen = (uint8_t)m.salt.size();
uint8_t ivLen = (uint8_t)m.iv.size();
uint16_t nameLen= (uint16_t)m.origName.size();
uint64_t sizeLE = m.origSize;
fwrite(magic,1,4,out);
fwrite(&version,1,1,out);
fwrite(&iters,4,1,out);
fwrite(&saltLen,1,1,out);
fwrite(&ivLen,1,1,out);
fwrite(&nameLen,2,1,out);
fwrite(&sizeLE,8,1,out);
fwrite(m.sha256.data(),1,32,out);
if (!m.salt.empty()) fwrite(m.salt.data(),1,m.salt.size(),out);
if (!m.iv.empty()) fwrite(m.iv.data(),1,m.iv.size(),out);
if (!m.origName.empty()) fwrite(m.origName.data(),1,m.origName.size(),out);
}
static Meta read_header_int(FILE* in) {
Meta m; char magic[4]; ensure(fread(magic,1,4,in)==4, "EOF hdr");
ensure(std::memcmp(magic,"SFLK",4)==0, "Bad magic");
uint8_t version; ensure(fread(&version,1,1,in)==1, "EOF ver"); ensure(version==1, "Bad ver");
uint32_t iters; ensure(fread(&iters,4,1,in)==1, "EOF iters"); m.iterations = iters;
uint8_t saltLen, ivLen; ensure(fread(&saltLen,1,1,in)==1, "EOF saltLen"); ensure(fread(&ivLen,1,1,in)==1, "EOF ivLen");
uint16_t nameLen; ensure(fread(&nameLen,2,1,in)==1, "EOF nameLen");
uint64_t origSize; ensure(fread(&origSize,8,1,in)==1, "EOF size"); m.origSize = origSize;
ensure(fread(m.sha256.data(),1,32,in)==32, "EOF sha");
if (saltLen) { m.salt.resize(saltLen); ensure(fread(m.salt.data(),1,saltLen,in)==saltLen, "EOF salt"); }
if (ivLen) { m.iv.resize(ivLen); ensure(fread(m.iv.data(),1,ivLen,in)==ivLen, "EOF iv"); }
if (nameLen) { m.origName.resize(nameLen); ensure(fread(&m.origName[0],1,nameLen,in)==nameLen, "EOF name"); }
return m;
}
void encrypt_file(const std::string& inPath,
const std::string& outPath,
const std::string& password,
uint32_t iterations) {
FILE* in = fopen(inPath.c_str(), "rb"); ensure(in,"open in");
FILE* out= fopen(outPath.c_str(), "wb"); ensure(out,"open out");
Meta m; m.iterations = iterations; m.origName = std::filesystem::path(inPath).filename().string();
m.origSize = file_size(inPath);
m.salt.resize(16); ensure(RAND_bytes(m.salt.data(), m.salt.size())==1, "salt rng");
m.iv.resize(12); ensure(RAND_bytes(m.iv.data(), m.iv.size())==1, "iv rng");
SHA256_CTX sha; SHA256_Init(&sha);
auto key = derive_key(password, m.salt, m.iterations);
EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new(); ensure(ctx, "ctx");
ensure(EVP_EncryptInit_ex(ctx, EVP_aes_256_gcm(), nullptr, nullptr, nullptr)==1, "enc init");
ensure(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, (int)m.iv.size(), nullptr)==1, "ivlen");
ensure(EVP_EncryptInit_ex(ctx, nullptr, nullptr, key.data(), m.iv.data())==1, "enc key/iv");
write_header(out, m);
const size_t BUF=1<<16; std::vector<uint8_t> buf(BUF), cbuf(BUF+16);
int outlen=0; size_t r;
while ((r=fread(buf.data(),1,BUF,in))>0) {
SHA256_Update(&sha, buf.data(), r);
ensure(EVP_EncryptUpdate(ctx, cbuf.data(), &outlen, buf.data(), (int)r)==1, "enc upd");
if(outlen) fwrite(cbuf.data(),1,outlen,out);
}
SHA256_Final(m.sha256.data(), &sha);
ensure(EVP_EncryptFinal_ex(ctx, cbuf.data(), &outlen)==1, "enc fin");
if(outlen) fwrite(cbuf.data(),1,outlen,out);
uint8_t tag[16]; ensure(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_GET_TAG, 16, tag)==1, "get tag");
fwrite(tag,1,16,out);
EVP_CIPHER_CTX_free(ctx);
fclose(in); fclose(out);
log_info(std::string("encrypted: ")+inPath+" -> "+outPath);
}
Meta read_header(const std::string& inPath) {
FILE* f=fopen(inPath.c_str(),"rb"); if(!f) throw std::runtime_error("open fail");
Meta m = read_header_int(f); fclose(f); return m;
}
std::string decrypt_file(const std::string& inPath,
const std::string& outPath,
const std::string& password) {
FILE* in = fopen(inPath.c_str(), "rb"); ensure(in,"open in");
Meta m = read_header_int(in);
auto key = derive_key(password, m.salt, m.iterations);
std::string restore = outPath.empty()? m.origName : outPath;
FILE* out = fopen(restore.c_str(), "wb"); ensure(out,"open out");
EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new(); ensure(ctx, "ctx");
ensure(EVP_DecryptInit_ex(ctx, EVP_aes_256_gcm(), nullptr, nullptr, nullptr)==1, "dec init");
ensure(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, (int)m.iv.size(), nullptr)==1, "ivlen");
ensure(EVP_DecryptInit_ex(ctx, nullptr, nullptr, key.data(), m.iv.data())==1, "dec key/iv");
const size_t BUF=1<<16; std::vector<uint8_t> buf(BUF), pbuf(BUF);
int outlen=0; size_t r; uint64_t produced=0;
uint64_t total = file_size(inPath);
long pos = ftell(in);
uint64_t headerBytes = (uint64_t)pos;
uint64_t encBytes = total - headerBytes - 16;
uint64_t left = encBytes;
SHA256_CTX sha; SHA256_Init(&sha);
while (left) {
size_t chunk = left>BUF?BUF:left;
r = fread(buf.data(),1,chunk,in); ensure(r==chunk, "EOF cipher");
ensure(EVP_DecryptUpdate(ctx, pbuf.data(), &outlen, buf.data(), (int)r)==1, "dec upd");
if (outlen) { fwrite(pbuf.data(),1,outlen,out); SHA256_Update(&sha, pbuf.data(), outlen); produced += outlen; }
left -= r;
}
uint8_t tag[16]; ensure(fread(tag,1,16,in)==16, "EOF tag");
ensure(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_TAG, 16, tag)==1, "set tag");
int finok = EVP_DecryptFinal_ex(ctx, pbuf.data(), &outlen);
if (finok<=0) { fclose(out); std::remove(restore.c_str()); EVP_CIPHER_CTX_free(ctx); throw std::runtime_error("Authentication failed (bad password or corrupted file)"); }
if (outlen) { fwrite(pbuf.data(),1,outlen,out); SHA256_Update(&sha, pbuf.data(), outlen); produced += outlen; }
std::array<uint8_t,32> shaOut{}; SHA256_Final(shaOut.data(), &sha);
if (produced != m.origSize || shaOut != m.sha256) {
fclose(out); EVP_CIPHER_CTX_free(ctx);
throw std::runtime_error("Checksum/size mismatch after decrypt – file may be corrupted");
}
fclose(out); EVP_CIPHER_CTX_free(ctx);
log_info(std::string("decrypted: ")+inPath+" -> "+restore);
return restore;
}
void verify_file(const std::string& inPath, const std::string& password) {
FILE* in = fopen(inPath.c_str(), "rb"); ensure(in,"open in");
Meta m = read_header_int(in);
auto key = derive_key(password, m.salt, m.iterations);
EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new(); ensure(ctx, "ctx");
ensure(EVP_DecryptInit_ex(ctx, EVP_aes_256_gcm(), nullptr, nullptr, nullptr)==1, "dec init");
ensure(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, (int)m.iv.size(), nullptr)==1, "ivlen");
ensure(EVP_DecryptInit_ex(ctx, nullptr, nullptr, key.data(), m.iv.data())==1, "dec key/iv");
const size_t BUF=1<<16; std::vector<uint8_t> buf(BUF), pbuf(BUF);
int outlen=0; size_t r;
uint64_t total = file_size(inPath);
uint64_t headerBytes = ftell(in);
uint64_t encBytes = total - headerBytes - 16;
uint64_t left = encBytes; uint64_t produced=0;
SHA256_CTX sha; SHA256_Init(&sha);
while (left) {
size_t chunk = left>BUF?BUF:left;
r = fread(buf.data(),1,chunk,in); ensure(r==chunk, "EOF cipher");
ensure(EVP_DecryptUpdate(ctx, pbuf.data(), &outlen, buf.data(), (int)r)==1, "dec upd");
if (outlen) { SHA256_Update(&sha, pbuf.data(), outlen); produced += outlen; }
left -= r;
}
uint8_t tag[16]; ensure(fread(tag,1,16,in)==16, "EOF tag");
ensure(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_TAG, 16, tag)==1, "set tag");
int finok = EVP_DecryptFinal_ex(ctx, pbuf.data(), &outlen);
ensure(finok>0, "Auth failed");
if (outlen) { SHA256_Update(&sha, pbuf.data(), outlen); produced += outlen; }
std::array<uint8_t,32> shaOut{}; SHA256_Final(shaOut.data(), &sha);
ensure(produced==m.origSize, "Size mismatch");
ensure(shaOut==m.sha256, "SHA256 mismatch");
EVP_CIPHER_CTX_free(ctx);
fclose(in);
log_info(std::string("verified: ")+inPath);
}
}