-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcripter.h
executable file
·310 lines (201 loc) · 6.86 KB
/
cripter.h
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/*cripter.h*/
// DEPENDENCIES
/* // RSA
#if !defined(MBEDTLS_PK_WRITE_C) || !defined(MBEDTLS_PEM_WRITE_C) || \
!defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_ENTROPY_C) || \
!defined(MBEDTLS_CTR_DRBG_C) || !defined(MBEDTLS_BIGNUM_C)
#endif // RSA
*/
#ifndef CRIPTER_H
#define CRIPTER_H
#define MBEDTLS_ERROR_C
#define MBEDTLS_ERROR_BUFFER_LENGTH 255
#define GCM_TAG_SIZE 16
#define AES_GCM_BLOCK_SIZE 16
#define HASH_SIZE_SHA_256 32
#define EXPONENT 65537
#include "core/config/project_settings.h"
#include "core/object/ref_counted.h"
#include "core/core_bind.h"
#include "mbedtls/error.h"
#include <mbedtls/entropy.h>
#include <mbedtls/ctr_drbg.h>
#include <mbedtls/gcm.h>
#include <mbedtls/pk.h>
#include <mbedtls/pkcs5.h>
#include <mbedtls/md.h>
#include "mbedtls/rsa.h"
#include <mbedtls/ecp.h>
#include <iostream>
#include <stdio.h>
#include <fstream>
#include <vector>
#include <string>
#include <map>
class Cripter : public RefCounted{
GDCLASS(Cripter, RefCounted);
public:
enum CryptMode {
DECRYPT = 0,
ENCRYPT = 1
};
enum KeySize { // - Standard sizes for keys.
BITS_128 = 128,
BITS_192 = 192,
BITS_256 = 256,
BITS_512 = 512,
BITS_1024 = 1024,
BITS_2048 = 2048,
BITS_3072 = 3072,
BITS_4096 = 4096,
BITS_7680 = 7680,
BITS_8192 = 8192,
};
enum Algorithm {
EBC,
CBC,
CFB128,
CFB8,
OFB,
CTR
};
enum FileFormat {
PEM,
DER
};
using PK_TYPE = mbedtls_pk_type_t;
using CURVE_TYPE = mbedtls_ecp_curve_type;
using ECP_GROUP_ID = mbedtls_ecp_group_id;
private:
static constexpr int TYPE_RSA = 0;
static constexpr int TYPE_ECC = 1;
static size_t get_max_rsa_input_size(const mbedtls_pk_context *pk);
static String ensure_global_path(String p_path);
static std::vector<unsigned char> GDstring_to_STDvector(const String p_string);
static std::vector<unsigned char> byteArray_to_vector(const PackedByteArray &p_packed_array);
static String mbed_error_msn(int mbedtls_erro, const char* p_function);
static Error add_pkcs7_padding(const std::vector<unsigned char>& data, std::vector<unsigned char>& padded_data, const size_t block_size);
static PackedByteArray add_pkcs7_padding(const PackedByteArray data, const size_t block_size);
static Error remove_pkcs7_padding(const std::vector<unsigned char>& padded_data, std::vector<unsigned char>& data, const size_t block_size);
static PackedByteArray remove_pkcs7_padding(PackedByteArray padded_data, const size_t block_size);
static Variant _gcm_crypt(
std::vector<unsigned char> input,
std::vector<unsigned char> password,
std::vector<unsigned char> iv,
std::vector<unsigned char> aad,
std::vector<unsigned char> tag,
Cripter::KeySize keybits,
int mode
);
/*
static std::vector<unsigned char> _aes_crypt(
std::vector<unsigned char> input,
std::vector<unsigned char> password,
std::vector<unsigned char> iv,
Algorithm algorith,
Cripter::KeySize keybits,
int mode
);
*/
static PackedByteArray _aes_crypt(
PackedByteArray input,
String password,
PackedByteArray iv,
Algorithm algorith,
Cripter::KeySize keybits,
int mode
);
struct aes_streamer {
mbedtls_aes_xts_context aes;
};
struct gcm_streamer {
mbedtls_gcm_context gcm_ctx;
};
aes_streamer *aes_stream = nullptr;
gcm_streamer *gcm_stream = nullptr;
protected:
static void _bind_methods();
public:
// Streamming =======================
// Error aes_start_stream(const String password);
// Error aes_update_stream(const PackedByteArray data);
// Error aes_stop_stream();
Error gcm_start_stream(const String password, const PackedByteArray iv, const CryptMode mode, Cripter::KeySize keybits = BITS_256);
PackedByteArray gcm_update_stream(const PackedByteArray data, const bool in_chunk = false);
PackedByteArray gcm_stop_stream(PackedByteArray data); // return the tag
// Utilities ========================
static PackedByteArray generate_iv(const int iv_length, const String p_personalization);
static String derive_key_pbkdf2(const String p_password, const String p_salt, const int iterations = 500, const int key_length = 16);
static PackedStringArray get_available_curves();
//AES ========================
//TODO Finish XTS
static PackedByteArray aes_encrypt(
const PackedByteArray plaintext,
const String p_password, // A chave precisa ter um tamanho especifico. Use "derive_key_pbkdf2" para derivar a chave para 32 bytes / 256 bits.
PackedByteArray p_iv, // CBC=128 bits (16 bytes)
Algorithm algorith = CBC,
KeySize keybits = BITS_256
);
static PackedByteArray aes_decrypt(
const PackedByteArray ciphertext,
const String p_password,
PackedByteArray p_iv,
Algorithm algorith = CBC,
KeySize keybits = BITS_256
);
// GCM ========================
static Dictionary gcm_encrypt(
const PackedByteArray plaintext,
const String p_password,
const PackedByteArray p_iv,
String p_aad = "",
Cripter::KeySize keybits = BITS_256
);
static PackedByteArray gcm_decrypt(
const PackedByteArray ciphertext,
const String p_password,
const PackedByteArray p_iv,
const PackedByteArray p_tag,
const String p_aad,
Cripter::KeySize keybits = BITS_256
);
// TODO ===============
// Stream Start-Update-Stop
// PK ========================
static Dictionary pk_analyze_key(const String p_key_path);
static Error pk_generate_keys(
PK_TYPE algorithm_type, // RSA or ECC
KeySize key_size, // Key size in bits (for RSA)
const ECP_GROUP_ID curve, // Curve (for ECC)
FileFormat storage_format, // PEM or DER
const String password, // Password for encryption (optional)
const String p_private_key_filename, // Output private key filename
const String p_public_key_filename, // Output public key filename
const String personalization = "key_generation" // Personalization
);
static Variant pk_match_keys(const String p_private_key_path, const String p_public_key_path, const String password);
static PackedByteArray pk_encrypt(
const PackedByteArray plaintext, // The data to beencrypted.
const String p_public_key_path // The path to the key.
);
// Decrypt using RSA or EC.
static PackedByteArray pk_decrypt(
const PackedByteArray ciphertext, // Buffer to decrypt.
const String p_private_key_path, // The path to the key.
const String password = "" // The data to beencrypted.
);
static PackedByteArray pk_sign(const String private_key_path, const PackedByteArray data, const String password = "");
static Variant pk_verify_signature(const String public_key_path, const PackedByteArray data, const String password = "");
Cripter();
~Cripter();
};
// ENUMS CASTS ========================
VARIANT_ENUM_CAST(Cripter::ECP_GROUP_ID);
VARIANT_ENUM_CAST(Cripter::CURVE_TYPE);
VARIANT_ENUM_CAST(Cripter::FileFormat);
VARIANT_ENUM_CAST(Cripter::Algorithm);
VARIANT_ENUM_CAST(Cripter::CryptMode);
VARIANT_ENUM_CAST(Cripter::KeySize);
VARIANT_ENUM_CAST(Cripter::PK_TYPE);
#endif // CRIPTER_H
/*cripter.h*/