-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrypto_tool.cpp
188 lines (156 loc) · 6.75 KB
/
crypto_tool.cpp
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
/*
* Cipherator - A command-line tool for encrypting and decrypting files using AES-256 in GCM mode.
*
* Copyright (C) 2023 chatgptdev
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the MIT License as published by
* the Open Source Initiative, either version of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* MIT License for more details.
*
* You should have received a copy of the MIT License
* along with this program. If not, see <https://opensource.org/licenses/MIT>.
*/
#include <iostream>
#include <fstream>
#include "crypto_tool.h"
#ifdef _WIN32
#include <bcrypt.h>
#elif defined(__APPLE__)
#include <Security/Security.h>
#else
#include <openssl/rand.h>
#endif
constexpr size_t SALT_SIZE = 32;
constexpr size_t CHUNK_SIZE = 64 * 1024;
extern bool quietMode;
secure_vector<unsigned char> CryptoTool::generateRandom(size_t count) {
secure_vector<unsigned char> rnd(count);
#ifdef _WIN32
BCRYPT_ALG_HANDLE hAlg = NULL;
NTSTATUS status = BCryptOpenAlgorithmProvider(&hAlg, BCRYPT_RNG_ALGORITHM, NULL, 0);
if (status != ERROR_SUCCESS) {
throw std::runtime_error("Failed to open RNG algorithm provider.");
}
status = BCryptGenRandom(hAlg, rnd.data(), count, 0);
if (status != ERROR_SUCCESS) {
BCryptCloseAlgorithmProvider(hAlg, 0);
throw std::runtime_error("Failed to generate random salt.");
}
BCryptCloseAlgorithmProvider(hAlg, 0);
#elif defined(__APPLE__)
if (SecRandomCopyBytes(kSecRandomDefault, count, rnd.data()) != 0) {
throw std::runtime_error("Failed to generate random salt.");
}
#else
// use OpenSSL random generator
if (RAND_bytes(rnd.data(), count) != 1) {
throw std::runtime_error("Failed to generate random salt.");
}
#endif
return rnd;
}
bool CryptoTool::encrypt(const std::string& inputFile, const std::string& outputFile, const secure_vector<char>& password, const std::string& keyFile) {
try {
std::ifstream input(inputFile, std::ios::binary);
if (!input) {
throw std::runtime_error("Error opening input file");
}
std::ofstream output(outputFile, std::ios::binary);
if (!output) {
throw std::runtime_error("Error opening output file");
}
secure_vector<unsigned char> salt = generateRandom(SALT_SIZE);
secure_vector<unsigned char> key = key_management.deriveKey(password, keyFile, salt);
secure_vector<unsigned char> nonce = generateRandom(encryption.getNonceSize());
// Write salt and nonce to the output file
if (!output.write(reinterpret_cast<const char*>(salt.data()), salt.size())) {
throw std::runtime_error("Error writing salt to output file");
}
if (!output.write(reinterpret_cast<const char*>(nonce.data()), nonce.size())) {
throw std::runtime_error("Error writing nonce to output file");
}
secure_vector<unsigned char> buffer(CHUNK_SIZE);
secure_vector<unsigned char> encrypted_buffer(CHUNK_SIZE + encryption.getTagSize());
uint64_t counter = 0;
while (input) {
input.read(reinterpret_cast<char*>(buffer.data()), CHUNK_SIZE);
std::streamsize bytesRead = input.gcount();
if (bytesRead > 0) {
secure_vector<unsigned char> iv = key_management.deriveIVFromNonce(nonce, counter);
encryption.encrypt(buffer.data(), bytesRead, key, iv, encrypted_buffer);
// Write encrypted chunk and tag to the output file
if (!output.write(reinterpret_cast<const char*>(encrypted_buffer.data()), encrypted_buffer.size())) {
throw std::runtime_error("Error writing encrypted data to output file");
}
counter++;
}
}
input.close();
output.close();
return true;
}
catch (const std::exception& e) {
if (!quietMode) {
std::cerr << "Encryption failed: " << e.what() << std::endl;
}
return false;
}
}
bool CryptoTool::decrypt(const std::string& inputFile, const std::string& outputFile, const secure_vector<char>& password, const std::string& keyFile) {
try {
std::ifstream input(inputFile, std::ios::binary | std::ios::ate);
if (!input) {
throw std::runtime_error("Error opening input file");
}
std::streamsize fileSize = input.tellg();
input.seekg(0, std::ios::beg);
if (fileSize < static_cast<std::streamsize>(SALT_SIZE + encryption.getNonceSize() + encryption.getTagSize())) {
throw std::runtime_error("Input file is too small to contain valid encrypted data");
}
secure_vector<unsigned char> salt(SALT_SIZE);
secure_vector<unsigned char> nonce(encryption.getNonceSize());
// Read salt and nonce from the input file
if (!input.read(reinterpret_cast<char*>(salt.data()), salt.size())) {
throw std::runtime_error("Error reading salt from input file");
}
if (!input.read(reinterpret_cast<char*>(nonce.data()), nonce.size())) {
throw std::runtime_error("Error reading nonce from input file");
}
secure_vector<unsigned char> key = key_management.deriveKey(password, keyFile, salt);
std::ofstream output(outputFile, std::ios::binary);
if (!output) {
throw std::runtime_error("Error opening output file");
}
secure_vector<unsigned char> buffer(CHUNK_SIZE + encryption.getTagSize());
secure_vector<unsigned char> decrypted_buffer(CHUNK_SIZE);
uint64_t counter = 0;
while (input) {
input.read(reinterpret_cast<char*>(buffer.data()), buffer.size());
std::streamsize bytesRead = input.gcount();
if (bytesRead > 0) {
secure_vector<unsigned char> iv = key_management.deriveIVFromNonce(nonce, counter);
encryption.decrypt(buffer.data(), bytesRead, key, iv, decrypted_buffer);
// Write decrypted chunk to the output file
if (!output.write(reinterpret_cast<const char*>(decrypted_buffer.data()), decrypted_buffer.size())) {
throw std::runtime_error("Error writing decrypted data to output file");
}
counter++;
}
}
input.close();
output.close();
return true;
}
catch (const std::exception& e) {
if (!quietMode) {
std::cerr << "Decryption failed: " << e.what() << std::endl;
}
return false;
}
}