-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkey_management.cpp
113 lines (100 loc) · 4.72 KB
/
key_management.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
/*
* 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 "key_management.h"
#include <fstream>
KeyManagement::KeyManagement(size_t iterations) : iterationsCount(iterations) {
#ifdef _WIN32
NTSTATUS status = BCryptOpenAlgorithmProvider(&hSha256Algorithm, BCRYPT_SHA256_ALGORITHM, nullptr, 0);
if (!BCRYPT_SUCCESS(status)) {
throw std::runtime_error("Error opening hash algorithm provider");
}
status = BCryptOpenAlgorithmProvider(&hHMACSha256Algorithm, BCRYPT_SHA256_ALGORITHM, nullptr, BCRYPT_ALG_HANDLE_HMAC_FLAG);
if (!BCRYPT_SUCCESS(status)) {
BCryptCloseAlgorithmProvider(hSha256Algorithm, 0);
throw std::runtime_error("Error opening HMAC algorithm provider");
}
#endif
}
KeyManagement::~KeyManagement() {
#ifdef _WIN32
BCryptCloseAlgorithmProvider(hSha256Algorithm, 0);
BCryptCloseAlgorithmProvider(hHMACSha256Algorithm, 0);
#endif
}
secure_vector<unsigned char> KeyManagement::deriveKey(const secure_vector<char>& password, const std::string& keyFile, const secure_vector<unsigned char>& salt) {
secure_vector<unsigned char> finalPassword(password.begin(), password.end());
if (!keyFile.empty()) {
std::ifstream file(keyFile, std::ios::binary);
if (file) {
secure_vector<char> buffer(1 << 20); // Read up to 1MiB
file.read(buffer.data(), buffer.size());
std::streamsize bytesRead = file.gcount();
buffer.resize(bytesRead);
finalPassword.insert(finalPassword.end(), buffer.begin(), buffer.end());
secure_vector<unsigned char> hash(32);
#ifdef _WIN32
NTSTATUS status = BCryptHash(hSha256Algorithm, nullptr, 0, (PUCHAR)finalPassword.data(), finalPassword.size(), hash.data(), hash.size());
if (!BCRYPT_SUCCESS(status)) {
throw std::runtime_error("Error hashing password and key file content");
}
#elif defined(__APPLE__)
CC_SHA256_CTX sha256Context;
CC_SHA256_Init(&sha256Context);
CC_SHA256_Update(&sha256Context, finalPassword.data(), finalPassword.size());
CC_SHA256_Final(hash.data(), &sha256Context);
#else
unsigned int length = 0;
if (!EVP_Digest(finalPassword.data(), finalPassword.size(), hash.data(), &length, EVP_sha256(), nullptr)) {
throw std::runtime_error("Error hashing password and key file content");
}
#endif
finalPassword = hash;
}
else {
throw std::runtime_error("Error opening key file");
}
}
const size_t iterations = getIterationsCount();
size_t cbDerivedKey = 32;
secure_vector<unsigned char> derivedKey(cbDerivedKey);
#ifdef _WIN32
NTSTATUS status = BCryptDeriveKeyPBKDF2(hHMACSha256Algorithm, (PUCHAR)finalPassword.data(), finalPassword.size(), (PUCHAR)salt.data(), salt.size(), (DWORD) iterations, derivedKey.data(), (DWORD) cbDerivedKey, 0);
if (!BCRYPT_SUCCESS(status)) {
throw std::runtime_error("Error deriving key from password and key file");
}
#elif defined(__APPLE__)
CCKeyDerivationPBKDF(kCCPBKDF2, finalPassword.data(), finalPassword.size(), salt.data(), salt.size(), kCCPRFHmacAlgSHA256, iterations, derivedKey.data(), derivedKey.size());
#else
if (!PKCS5_PBKDF2_HMAC((const char*)finalPassword.data(), finalPassword.size(), salt.data(), (int) salt.size(), (int) iterations, EVP_sha256(), (int) cbDerivedKey, derivedKey.data())) {
throw std::runtime_error("Error deriving key from password and key file");
}
#endif
return derivedKey;
}
secure_vector<unsigned char> KeyManagement::deriveIVFromNonce(const secure_vector<unsigned char>& nonce, uint64_t counter) {
secure_vector<unsigned char> derivedIV(nonce.begin(), nonce.end());
if (nonce.size() < sizeof(counter)) {
throw std::runtime_error("Error deriving IV from nonce: nonce is too small");
}
// XOR nonce with the big-endian counter
for (size_t i = 0; i < sizeof(counter); ++i) {
derivedIV[derivedIV.size() - 1 - i] ^= (counter >> (8 * i)) & 0xFF;
}
return derivedIV;
}