-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathByteCrypt.hpp
1930 lines (1787 loc) · 105 KB
/
ByteCrypt.hpp
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#pragma once
/**
* ============================================================================
* ByteCrypt Class - A C++ Data Encryption Utility Module
* ============================================================================
*
* MIT License
*
* Copyright (c) 2024 Somorpher
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
* ============================================================================
*
* Written by [Somorpher], [2024].
*/
/**
* Cryptographic Utility library for most common cryptograpic operations, algorithms, modes, etc...
*
* Symmetric Cryptography:
* Available Modes: CBC((16+-)algos), GCM((4+-)algos), EAX((15+-)algos), CFB((6+-)algos), OFB((7+-)algos), CTR((6+-)algos)
* Hashing: SHA1, SHA224, SHA256, SHA384, SHA512, Tiger, Whirlpool, MD5, Blake2, Ripemd160
* Encoding: base64, hex
*
* Asymmetric Cryptography:
* RSA
* Message Signing, message authentication, Signature generation/verification, RSA key generation(DER, PEM) with
* different key size available(512, 1024, 2048, 3072, 4096), etc...
*
*/
#if defined(__linux__) || defined(__APPLE__) || defined(_WIN32) || defined(_WIN64) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) || \
defined(__sun) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__MINGW64__) || defined(__ANDROID__) || defined(__unix__) || defined(__HAIKU__)
#if defined(__x86_64__) || defined(__amd64__) || defined(__aarch64__) || defined(__mips64__) || defined(__s390x__) || defined(__riscv64__)
#if defined(__clang__) || defined(__GNUC__) || defined(__GNUG__) || defined(_MSC_VER)
#if defined(_WIN32) || defined(_WIN64)
#define PATH_SEPARATOR "\\"
#include <windows.h>
#else
#define PATH_SEPARATOR "/"
#endif
#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <bitset>
#include <chrono>
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <optional>
#include <sstream>
#include <stdexcept>
#include <string>
#include <type_traits>
#include <typeinfo>
#include <utility>
// Encryption Libraries
#include <crypto++/aes.h>
#include <crypto++/aria.h>
#include <crypto++/base64.h>
#include <crypto++/blake2.h>
#include <crypto++/blowfish.h>
#include <crypto++/cast.h>
#include <crypto++/chacha.h>
#include <crypto++/eax.h>
#include <crypto++/filters.h>
#include <crypto++/gcm.h>
#include <crypto++/gost.h>
#include <crypto++/hex.h>
#include <crypto++/hight.h>
#include <crypto++/idea.h>
#include <crypto++/lea.h>
#include <crypto++/mars.h>
#include <crypto++/modes.h>
#include <crypto++/osrng.h>
#include <crypto++/pwdbased.h>
#include <crypto++/rc2.h>
#include <crypto++/rc5.h>
#include <crypto++/rc6.h>
#include <crypto++/rijndael.h>
#include <crypto++/ripemd.h>
#include <crypto++/rsa.h>
#include <crypto++/seal.h>
#include <crypto++/secblock.h>
#include <crypto++/seed.h>
#include <crypto++/serpent.h>
#include <crypto++/sha.h>
#include <crypto++/simon.h>
#include <crypto++/speck.h>
#include <crypto++/tiger.h>
#include <crypto++/twofish.h>
#include <crypto++/whrlpool.h>
#include <cryptopp/cryptlib.h>
#include <cryptopp/pssr.h>
#define CRYPTOPP_ENABLE_NAMESPACE_WEAK 1
#include <crypto++/md5.h>
#ifndef __MODULE_BYTE_CRYPT__
#define __MODULE_BYTE_CRYPT__
namespace ByteCryptModule
{
/* Macros *\
\*+++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
#define RSA_PUBLIC_KEY_HEADER "-----BEGIN PUBLIC KEY-----\n"
#define RSA_PRIVATE_KEY_HEADER "-----BEGIN RSA PRIVATE KEY-----\n"
#define RSA_PUBLIC_KEY_FOOTER "-----END PUBLIC KEY-----\n"
#define RSA_PRIVATE_KEY_FOOTER "-----END RSA PRIVATE KEY-----\n"
#define RSA_ENCRYPTED_PRIVATE_KEY_HEADER "-----BEGIN ENCRYPTED PRIVATE KEY-----\n"
#define RSA_ENCRYPTED_PRIVATE_KEY_FOOTER "-----END ENCRYPTED PRIVATE KEY-----\n"
#define DEFAULT_CIPHER_ITERATION_COUNTER 10000
#define DEFAULT_SEC_BLOCK_KEY_SIZE CryptoPP::AES::DEFAULT_KEYLENGTH
#define DEFAULT_SEC_BLOCK_IV_SIZE CryptoPP::AES::BLOCKSIZE
/* Attribution *\
\*+++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
#if defined(__GNUC__) || defined(__GNUG__) || defined(__clang__)
#define __hint_set_iter_counter__ __attribute__((cold, nothrow, noipa, no_stack_protector))
#define __hint_set_def_key_size__ __attribute__((cold, nothrow, noipa, no_stack_protector))
#define __hint_set_def_iv_size__ __attribute__((cold, nothrow, noipa, no_stack_protector))
#define __hint_hash__ __attribute__((stack_protect, zero_call_used_regs("used"), warn_unused_result, access(read_only, 1), optimize(3)))
#define __hint_base64_encode__ __attribute__((warn_unused_result, stack_protect, access(read_only, 1), optimize("3")))
#define __hint_base64_decode__ __attribute__((warn_unused_result, stack_protect, access(read_only, 1), optimize("3")))
#define __hint_hex_encode__ __attribute__((warn_unused_result, no_stack_protector, access(read_only, 1), optimize("3")))
#define __hint_hex_decode__ __attribute__((warn_unused_result, no_stack_protector, access(read_only, 1), optimize("3")))
#define __hint_generate_rsa_key_der_pair__ __attribute__((cold, warn_unused_result, stack_protect, access(read_only, 1), zero_call_used_regs("used"), optimize("3")))
#define __hint_generate_rsa_key_pem_pair__ __attribute__((cold, warn_unused_result, stack_protect, access(read_only, 1), zero_call_used_regs("used"), optimize("3")))
#define __hint_sign_message__ __attribute__((warn_unused_result, stack_protect, access(read_only, 1), zero_call_used_regs("used"), optimize("3")))
#define __hint_verify_signature__ __attribute__((warn_unused_result, access(read_only, 1), access(read_only, 2), access(read_only, 3), stack_protect, zero_call_used_regs("used"), optimize("3")))
#define __hint_save_rsa_key__ __attribute__((stack_protect, zero_call_used_regs("used"), tainted_args, access(read_only, 1), access(read_only, 2), optimize("3")))
#define __hint_load_rsa_key__ __attribute__((warn_unused_result, cold, stack_protect, zero_call_used_regs("used"), access(read_only, 1), optimize("3")))
#define __hint_rsa_key_pair_verify__ __attribute__((warn_unused_result, stack_protect, zero_call_used_regs("used"), access(read_only, 1), optimize("3")))
#define __hint_rsa_key_pem_set_header__ __attribute__((nothrow, always_inline, stack_protect, zero_call_used_regs("used"), access(read_only, 1), optimize("3")))
#define __hint_rsa_key_pem_set_footer__ __attribute__((always_inline, nothrow, stack_protect, zero_call_used_regs("used"), access(read_only, 1), optimize("3")))
#define __hint_is_rsa_key_size_valid__ __attribute__((nothrow, warn_unused_result, always_inline, const, no_stack_protector, access(read_only, 1), optimize("1")))
#define __hint_is_rsa_key_pem__ __attribute__((warn_unused_result, nothrow, stack_protect, zero_call_used_regs("used"), access(read_only, 1), optimize("1")))
#define __hint_is_rsa_encrypted_key__ __attribute__((nothrow, warn_unused_result, const, always_inline, stack_protect, zero_call_used_regs("used"), access(read_only, 1), optimize("0")))
#define __hint_rsa_key_meta_wipe__ __attribute__((const, zero_call_used_regs("used"), warn_unused_result, access(read_only, 1), optimize("2")))
#define __hint_generate_random_bytes__ __attribute__((warn_unused_result, stack_protect, zero_call_used_regs("used"), optimize("3")))
#define __hint_store_secret__ __attribute__((cold, stack_protect, optimize("3"), zero_call_used_regs("used"), access(read_only, 1)))
#define __hint_load_secret_from_file__ __attribute__((cold, warn_unused_result, stack_protect, optimize("3"), zero_call_used_regs("used"), access(read_only, 1)))
#define __hint_prepare_secure_keys__ __attribute__((stack_protect, zero_call_used_regs("used"), optimize("3")))
#define __hint_cipher_transformation__ __attribute__((warn_unused_result, stack_protect, zero_call_used_regs("used"), noinline, access(read_only, 1), access(read_only, 2), optimize("3")))
#else
#define __hint_set_iter_counter__ [[nothrow]]
#define __hint_set_def_key_size__ [[nothrow]]
#define __hint_set_def_iv_size__ [[nothrow]]
#define __hint_hash__ [[nodiscard]]
#define __hint_base64_encode__ [[nodiscard]]
#define __hint_base64_decode__ [[nodiscard]]
#define __hint_hex_encode__ [[nodiscard]]
#define __hint_hex_decode__ [[nodiscard]]
#define __hint_generate_rsa_key_der_pair__ [[nodiscard]]
#define __hint_generate_rsa_key_pem_pair__ [[nodiscard]]
#define __hint_sign_message__ [[nodiscard]]
#define __hint_verify_signature__ [[nodiscard]]
#define __hint_save_rsa_key__ [[nodiscard]]
#define __hint_load_rsa_key__ [[nodiscard]]
#define __hint_rsa_key_pair_verify__ [[nodiscard]]
#define __hint_rsa_key_pem_set_header__ [[nothrow]]
#define __hint_rsa_key_pem_set_footer__ [[nothrow]]
#define __hint_is_rsa_key_size_valid__ [[nothrow, nodiscard]]
#define __hint_is_rsa_key_pem__ [[nothrow, nodiscard]]
#define __hint_is_rsa_encrypted_key__ [[nothrow, nodiscard]]
#define __hint_rsa_key_meta_wipe__ [[nodiscard]]
#define __hint_generate_random_bytes__ [[warn_unused_result]]
#define __hint_store_secret__ [[nodiscard]]
#define __hint_load_secret_from_file__ [[nodiscard]]
#define __hint_prepare_secure_keys__ [[]]
#define __hint_cipher_transformation__ [[]]
#endif
#define __temp_cipher_exec__ template <typename cipher_mode>
enum class e_hash_algo_option
{
SHA1 = 0,
SHA224,
SHA256,
SHA384,
SHA512,
MD5,
RIPEMD160,
WHIRLPOOL,
BLAKE2,
TIGER,
};
enum class e_rsa_key_pem_version
{
PUBLIC = 0,
PRIVATE
};
enum class e_operation_mode
{
CBC = 0,
GCM,
EAX,
CFB,
OFB,
CTR
};
enum class e_cbc_algorithm
{
AES = 0,
BLOWFISH,
CAST128,
CAST256,
IDEA,
RC2,
RC5,
RC6,
MARS,
SERPENT,
GOST,
SPECK128,
SIMON128,
HIGHT,
ARIA,
SEED,
__COUNT
};
enum class e_gcm_algorithm
{
AES = 0,
TWOFISH,
RC6,
MARS,
__COUNT,
};
enum class e_eax_algorithm
{
AES = 0,
BLOWFISH,
CAST128,
CAST256,
IDEA,
RC5,
RC6,
MARS,
SERPENT,
GOST,
LEA,
SPECK128,
SEED,
SIMON128,
HIGHT,
__COUNT
};
enum class e_cfb_algorithm
{
AES = 0,
BLOWFISH,
CAST128,
CAST256,
IDEA,
RC2,
RC5,
__COUNT
};
enum class e_ofb_algorithm
{
AES = 0,
BLOWFISH,
CAST128,
CAST256,
IDEA,
RC2,
RC5,
__COUNT
};
enum class e_ctr_algorithm
{
AES = 0,
BLOWFISH,
CAST128,
CAST256,
IDEA,
RC2,
RC5,
__COUNT
};
struct e_key_block_size
{
static const std::uint16_t
AES = CryptoPP::AES::DEFAULT_KEYLENGTH,
BLOWFISH = CryptoPP::Blowfish::DEFAULT_KEYLENGTH,
TWOFISH = CryptoPP::Twofish::DEFAULT_KEYLENGTH,
CAST128 = CryptoPP::CAST128::DEFAULT_KEYLENGTH,
CAST256 = CryptoPP::CAST256::DEFAULT_KEYLENGTH,
IDEA = CryptoPP::IDEA::DEFAULT_KEYLENGTH,
RC2 = CryptoPP::RC2::DEFAULT_KEYLENGTH,
RC5 = CryptoPP::RC5::DEFAULT_KEYLENGTH,
RC6 = CryptoPP::RC6::DEFAULT_KEYLENGTH,
MARS = CryptoPP::MARS::DEFAULT_KEYLENGTH,
SERPENT = CryptoPP::Serpent::DEFAULT_KEYLENGTH,
GOST = CryptoPP::GOST::DEFAULT_KEYLENGTH,
ARIA = CryptoPP::ARIA::BLOCKSIZE,
HIGHT = CryptoPP::HIGHT::BLOCKSIZE * 2,
LEA = CryptoPP::LEA::DEFAULT_KEYLENGTH,
SEED = CryptoPP::SEED::DEFAULT_KEYLENGTH,
SPECK128 = CryptoPP::SPECK128::DEFAULT_KEYLENGTH,
SIMON128 = CryptoPP::SIMON128::DEFAULT_KEYLENGTH;
};
struct e_iv_block_size
{
static const std::uint16_t
AES = CryptoPP::AES::BLOCKSIZE,
BLOWFISH = CryptoPP::Blowfish::BLOCKSIZE,
TWOFISH = CryptoPP::Twofish::BLOCKSIZE,
CAST128 = CryptoPP::CAST128::BLOCKSIZE,
CAST256 = CryptoPP::CAST256::DEFAULT_KEYLENGTH,
IDEA = CryptoPP::IDEA::BLOCKSIZE,
RC2 = CryptoPP::RC2::BLOCKSIZE,
RC5 = CryptoPP::RC5::BLOCKSIZE,
RC6 = CryptoPP::RC6::BLOCKSIZE,
MARS = CryptoPP::MARS::BLOCKSIZE,
SERPENT = CryptoPP::Serpent::BLOCKSIZE,
GOST = CryptoPP::GOST::BLOCKSIZE * 2,
ARIA = CryptoPP::ARIA::BLOCKSIZE,
HIGHT = CryptoPP::HIGHT::BLOCKSIZE * 2,
LEA = CryptoPP::LEA::BLOCKSIZE,
SEED = CryptoPP::SEED::BLOCKSIZE,
SIMON128 = CryptoPP::SIMON128::BLOCKSIZE,
SPECK128 = CryptoPP::SPECK128::BLOCKSIZE;
};
/* Type Alias *\
\*+++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
using byte = CryptoPP::byte;
using string_t = std::basic_string<char>;
using string_view_t = std::basic_string_view<char>;
using cbc_cipher_t = CryptoPP::Rijndael;
using transformer_filter_t = CryptoPP::StreamTransformationFilter;
using rsa_public_key_t = CryptoPP::RSA::PublicKey;
using rsa_private_key_t = CryptoPP::RSA::PrivateKey;
using entropy_seed_t = CryptoPP::AutoSeededRandomPool;
using invertible_rsa_t = CryptoPP::InvertibleRSAFunction;
using string_sink_t = CryptoPP::StringSink;
using string_source_t = CryptoPP::StringSource;
using hex_encoder_t = CryptoPP::HexEncoder;
using hex_decoder_t = CryptoPP::HexDecoder;
using base64_encoder_t = CryptoPP::Base64Encoder;
using base64_decoder_t = CryptoPP::Base64Decoder;
using rsa_signature_t = CryptoPP::RSASS<CryptoPP::PKCS1v15, CryptoPP::SHA256>::Signer;
using rsa_signature_filter_t = CryptoPP::SignerFilter;
using rsa_signature_verify_t = CryptoPP::RSASS<CryptoPP::PKCS1v15, CryptoPP::SHA256>::Verifier;
using sha256_hmac_t = CryptoPP::PKCS5_PBKDF2_HMAC<CryptoPP::SHA256>;
using auth_decryption_filter_t = CryptoPP::AuthenticatedDecryptionFilter;
using auth_encryption_filter_t = CryptoPP::AuthenticatedEncryptionFilter;
using sec_byte_block_t = CryptoPP::SecByteBlock;
using sha1_t = CryptoPP::SHA1;
using sha224_t = CryptoPP::SHA224;
using sha256_t = CryptoPP::SHA256;
using sha384_t = CryptoPP::SHA384;
using sha512_t = CryptoPP::SHA512;
using md5_t = CryptoPP::Weak1::MD5;
using ripemd160_t = CryptoPP::RIPEMD160;
using whirlpool_t = CryptoPP::Whirlpool;
using blake2_t = CryptoPP::BLAKE2b;
using tiger_t = CryptoPP::Tiger;
using gost_t = CryptoPP::GOST;
using hash_transformer_t = CryptoPP::HashTransformation;
using hash_filter_t = CryptoPP::HashFilter;
using cbc_aes_encryption_t = CryptoPP::CBC_Mode<cbc_cipher_t>::Encryption;
using cbc_blowfish_encryption_t = CryptoPP::CBC_Mode<CryptoPP::Blowfish>::Encryption;
using cbc_cast128_encryption_t = CryptoPP::CBC_Mode<CryptoPP::CAST128>::Encryption;
using cbc_cast256_encryption_t = CryptoPP::CBC_Mode<CryptoPP::CAST256>::Encryption;
using cbc_idea_encryption_t = CryptoPP::CBC_Mode<CryptoPP::IDEA>::Encryption;
using cbc_rc2_encryption_t = CryptoPP::CBC_Mode<CryptoPP::RC2>::Encryption;
using cbc_rc5_encryption_t = CryptoPP::CBC_Mode<CryptoPP::RC5>::Encryption;
using cbc_rc6_encryption_t = CryptoPP::CBC_Mode<CryptoPP::RC6>::Encryption;
using cbc_mars_encryption_t = CryptoPP::CBC_Mode<CryptoPP::MARS>::Encryption;
using cbc_serpent_encryption_t = CryptoPP::CBC_Mode<CryptoPP::Serpent>::Encryption;
using cbc_gost_encryption_t = CryptoPP::CBC_Mode<CryptoPP::GOST>::Encryption;
using cbc_aria_encryption_t = CryptoPP::CBC_Mode<CryptoPP::ARIA>::Encryption;
using cbc_simon128_encryption_t = CryptoPP::CBC_Mode<CryptoPP::SIMON128>::Encryption;
using cbc_speck128_encryption_t = CryptoPP::CBC_Mode<CryptoPP::SPECK128>::Encryption;
using cbc_hight_encryption_t = CryptoPP::CBC_Mode<CryptoPP::HIGHT>::Encryption;
using cbc_seed_encryption_t = CryptoPP::CBC_Mode<CryptoPP::SEED>::Encryption;
using cbc_aes_decryption_t = CryptoPP::CBC_Mode<cbc_cipher_t>::Decryption;
using cbc_blowfish_decryption_t = CryptoPP::CBC_Mode<CryptoPP::Blowfish>::Decryption;
using cbc_cast128_decryption_t = CryptoPP::CBC_Mode<CryptoPP::CAST128>::Decryption;
using cbc_cast256_decryption_t = CryptoPP::CBC_Mode<CryptoPP::CAST256>::Decryption;
using cbc_idea_decryption_t = CryptoPP::CBC_Mode<CryptoPP::IDEA>::Decryption;
using cbc_rc2_decryption_t = CryptoPP::CBC_Mode<CryptoPP::RC2>::Decryption;
using cbc_rc5_decryption_t = CryptoPP::CBC_Mode<CryptoPP::RC5>::Decryption;
using cbc_rc6_decryption_t = CryptoPP::CBC_Mode<CryptoPP::RC6>::Decryption;
using cbc_mars_decryption_t = CryptoPP::CBC_Mode<CryptoPP::MARS>::Decryption;
using cbc_serpent_decryption_t = CryptoPP::CBC_Mode<CryptoPP::Serpent>::Decryption;
using cbc_gost_decryption_t = CryptoPP::CBC_Mode<CryptoPP::GOST>::Decryption;
using cbc_aria_decryption_t = CryptoPP::CBC_Mode<CryptoPP::ARIA>::Decryption;
using cbc_simon128_decryption_t = CryptoPP::CBC_Mode<CryptoPP::SIMON128>::Decryption;
using cbc_speck128_decryption_t = CryptoPP::CBC_Mode<CryptoPP::SPECK128>::Decryption;
using cbc_hight_decryption_t = CryptoPP::CBC_Mode<CryptoPP::HIGHT>::Decryption;
using cbc_seed_decryption_t = CryptoPP::CBC_Mode<CryptoPP::SEED>::Decryption;
using gcm_aes_encryption_t = CryptoPP::GCM<cbc_cipher_t>::Encryption;
using gcm_twofish_encryption_t = CryptoPP::GCM<CryptoPP::Twofish>::Encryption;
using gcm_rc6_encryption_t = CryptoPP::GCM<CryptoPP::RC6>::Encryption;
using gcm_mars_encryption_t = CryptoPP::GCM<CryptoPP::MARS>::Encryption;
using gcm_aes_decryption_t = CryptoPP::GCM<cbc_cipher_t>::Decryption;
using gcm_twofish_decryption_t = CryptoPP::GCM<CryptoPP::Twofish>::Decryption;
using gcm_rc6_decryption_t = CryptoPP::GCM<CryptoPP::RC6>::Decryption;
using gcm_mars_decryption_t = CryptoPP::GCM<CryptoPP::MARS>::Decryption;
using eax_aes_encryption_t = CryptoPP::EAX<CryptoPP::AES>::Encryption;
using eax_blowfish_encryption_t = CryptoPP::EAX<CryptoPP::Blowfish>::Encryption;
using eax_serpent_encryption_t = CryptoPP::EAX<CryptoPP::Serpent>::Encryption;
using eax_cast128_encryption_t = CryptoPP::EAX<CryptoPP::CAST128>::Encryption;
using eax_cast256_encryption_t = CryptoPP::EAX<CryptoPP::CAST256>::Encryption;
using eax_idea_encryption_t = CryptoPP::EAX<CryptoPP::IDEA>::Encryption;
using eax_rc5_encryption_t = CryptoPP::EAX<CryptoPP::RC5>::Encryption;
using eax_rc6_encryption_t = CryptoPP::EAX<CryptoPP::RC6>::Encryption;
using eax_gost_encryption_t = CryptoPP::EAX<CryptoPP::GOST>::Encryption;
using eax_mars_encryption_t = CryptoPP::EAX<CryptoPP::MARS>::Encryption;
using eax_seed_encryption_t = CryptoPP::EAX<CryptoPP::SEED>::Encryption;
using eax_speck128_encryption_t = CryptoPP::EAX<CryptoPP::SPECK128>::Encryption;
using eax_lea_encryption_t = CryptoPP::EAX<CryptoPP::LEA>::Encryption;
using eax_simon128_encryption_t = CryptoPP::EAX<CryptoPP::SIMON128>::Encryption;
using eax_hight_encryption_t = CryptoPP::EAX<CryptoPP::HIGHT>::Encryption;
using eax_aes_decryption_t = CryptoPP::EAX<CryptoPP::AES>::Decryption;
using eax_blowfish_decryption_t = CryptoPP::EAX<CryptoPP::Blowfish>::Decryption;
using eax_serpent_decryption_t = CryptoPP::EAX<CryptoPP::Serpent>::Decryption;
using eax_cast128_decryption_t = CryptoPP::EAX<CryptoPP::CAST128>::Decryption;
using eax_cast256_decryption_t = CryptoPP::EAX<CryptoPP::CAST256>::Decryption;
using eax_idea_decryption_t = CryptoPP::EAX<CryptoPP::IDEA>::Decryption;
using eax_rc5_decryption_t = CryptoPP::EAX<CryptoPP::RC5>::Decryption;
using eax_rc6_decryption_t = CryptoPP::EAX<CryptoPP::RC6>::Decryption;
using eax_gost_decryption_t = CryptoPP::EAX<CryptoPP::GOST>::Decryption;
using eax_mars_decryption_t = CryptoPP::EAX<CryptoPP::MARS>::Decryption;
using eax_seed_decryption_t = CryptoPP::EAX<CryptoPP::SEED>::Decryption;
using eax_speck128_decryption_t = CryptoPP::EAX<CryptoPP::SPECK128>::Decryption;
using eax_lea_decryption_t = CryptoPP::EAX<CryptoPP::LEA>::Decryption;
using eax_simon128_decryption_t = CryptoPP::EAX<CryptoPP::SIMON128>::Decryption;
using eax_hight_decryption_t = CryptoPP::EAX<CryptoPP::HIGHT>::Decryption;
using cfb_aes_encryption_t = CryptoPP::CFB_Mode<CryptoPP::AES>::Encryption;
using cfb_blowfish_encryption_t = CryptoPP::CFB_Mode<CryptoPP::Blowfish>::Encryption;
using cfb_cast128_encryption_t = CryptoPP::CFB_Mode<CryptoPP::CAST128>::Encryption;
using cfb_cast256_encryption_t = CryptoPP::CFB_Mode<CryptoPP::CAST256>::Encryption;
using cfb_idea_encryption_t = CryptoPP::CFB_Mode<CryptoPP::IDEA>::Encryption;
using cfb_rc2_encryption_t = CryptoPP::CFB_Mode<CryptoPP::RC2>::Encryption;
using cfb_rc5_encryption_t = CryptoPP::CFB_Mode<CryptoPP::RC5>::Encryption;
using cfb_aes_decryption_t = CryptoPP::CFB_Mode<CryptoPP::AES>::Decryption;
using cfb_blowfish_decryption_t = CryptoPP::CFB_Mode<CryptoPP::Blowfish>::Decryption;
using cfb_cast128_decryption_t = CryptoPP::CFB_Mode<CryptoPP::CAST128>::Decryption;
using cfb_cast256_decryption_t = CryptoPP::CFB_Mode<CryptoPP::CAST256>::Decryption;
using cfb_idea_decryption_t = CryptoPP::CFB_Mode<CryptoPP::IDEA>::Decryption;
using cfb_rc2_decryption_t = CryptoPP::CFB_Mode<CryptoPP::RC2>::Decryption;
using cfb_rc5_decryption_t = CryptoPP::CFB_Mode<CryptoPP::RC5>::Decryption;
using ofb_aes_encryption_t = CryptoPP::OFB_Mode<CryptoPP::AES>::Encryption;
using ofb_blowfish_encryption_t = CryptoPP::OFB_Mode<CryptoPP::Blowfish>::Encryption;
using ofb_cast128_encryption_t = CryptoPP::OFB_Mode<CryptoPP::CAST128>::Encryption;
using ofb_cast256_encryption_t = CryptoPP::OFB_Mode<CryptoPP::CAST256>::Encryption;
using ofb_idea_encryption_t = CryptoPP::OFB_Mode<CryptoPP::IDEA>::Encryption;
using ofb_rc2_encryption_t = CryptoPP::OFB_Mode<CryptoPP::RC2>::Encryption;
using ofb_rc5_encryption_t = CryptoPP::OFB_Mode<CryptoPP::RC5>::Encryption;
using ofb_aes_decryption_t = CryptoPP::OFB_Mode<CryptoPP::AES>::Decryption;
using ofb_blowfish_decryption_t = CryptoPP::OFB_Mode<CryptoPP::Blowfish>::Decryption;
using ofb_cast128_decryption_t = CryptoPP::OFB_Mode<CryptoPP::CAST128>::Decryption;
using ofb_cast256_decryption_t = CryptoPP::OFB_Mode<CryptoPP::CAST256>::Decryption;
using ofb_idea_decryption_t = CryptoPP::OFB_Mode<CryptoPP::IDEA>::Decryption;
using ofb_rc2_decryption_t = CryptoPP::OFB_Mode<CryptoPP::RC2>::Decryption;
using ofb_rc5_decryption_t = CryptoPP::OFB_Mode<CryptoPP::RC5>::Decryption;
using ctr_aes_encryption_t = CryptoPP::CTR_Mode<CryptoPP::AES>::Encryption;
using ctr_blowfish_encryption_t = CryptoPP::CTR_Mode<CryptoPP::Blowfish>::Encryption;
using ctr_cast128_encryption_t = CryptoPP::CTR_Mode<CryptoPP::CAST128>::Encryption;
using ctr_cast256_encryption_t = CryptoPP::CTR_Mode<CryptoPP::CAST256>::Encryption;
using ctr_idea_encryption_t = CryptoPP::CTR_Mode<CryptoPP::IDEA>::Encryption;
using ctr_rc2_encryption_t = CryptoPP::CTR_Mode<CryptoPP::RC2>::Encryption;
using ctr_rc5_encryption_t = CryptoPP::CTR_Mode<CryptoPP::RC5>::Encryption;
using ctr_aes_decryption_t = CryptoPP::CTR_Mode<CryptoPP::AES>::Decryption;
using ctr_blowfish_decryption_t = CryptoPP::CTR_Mode<CryptoPP::Blowfish>::Decryption;
using ctr_cast128_decryption_t = CryptoPP::CTR_Mode<CryptoPP::CAST128>::Decryption;
using ctr_cast256_decryption_t = CryptoPP::CTR_Mode<CryptoPP::CAST256>::Decryption;
using ctr_idea_decryption_t = CryptoPP::CTR_Mode<CryptoPP::IDEA>::Decryption;
using ctr_rc2_decryption_t = CryptoPP::CTR_Mode<CryptoPP::RC2>::Decryption;
using ctr_rc5_decryption_t = CryptoPP::CTR_Mode<CryptoPP::RC5>::Decryption;
/* Structure *\
\*+++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
typedef struct alignas(void *)
{
std::optional<string_t> public_key{std::nullopt};
std::optional<string_t> private_key{std::nullopt};
bool state{false};
} rsa_key_pair_struct;
typedef struct alignas(void *)
{
string_t key{};
string_t error{};
bool status{false};
} rsa_key_block_load;
typedef struct alignas(void *)
{
string_t error_msg{""};
bool has_error{false};
} error_frame;
template <typename return_t> struct alignas(void *) op_frame
{
return_t result{};
error_frame error{};
};
typedef struct alignas(void *)
{
sec_byte_block_t key;
sec_byte_block_t iv;
} secure_byte_pair;
typedef op_frame<string_t> encryption_result;
typedef op_frame<string_t> decryption_result;
typedef struct alignas(void *)
{
std::uint16_t secure_key{};
std::uint16_t secure_ivector{};
} mode_of_operation_map;
typedef struct alignas(void *)
{
std::unordered_map<e_cbc_algorithm, mode_of_operation_map> cbc{};
std::unordered_map<e_gcm_algorithm, mode_of_operation_map> gcm{};
std::unordered_map<e_eax_algorithm, mode_of_operation_map> eax{};
std::unordered_map<e_cfb_algorithm, mode_of_operation_map> cfb{};
std::unordered_map<e_ofb_algorithm, mode_of_operation_map> ofb{};
std::unordered_map<e_ctr_algorithm, mode_of_operation_map> ctr{};
} operation_mode;
std::unordered_map<e_cbc_algorithm, mode_of_operation_map> cbc_map_block{
{e_cbc_algorithm::AES, mode_of_operation_map{.secure_key{e_key_block_size::AES}, .secure_ivector{e_iv_block_size::AES}}},
{e_cbc_algorithm::ARIA, mode_of_operation_map{.secure_key{e_key_block_size::ARIA}, .secure_ivector{e_iv_block_size::ARIA}}},
{e_cbc_algorithm::BLOWFISH, mode_of_operation_map{.secure_key{e_key_block_size::BLOWFISH}, .secure_ivector{e_iv_block_size::BLOWFISH}}},
{e_cbc_algorithm::CAST128, mode_of_operation_map{.secure_key{e_key_block_size::CAST128}, .secure_ivector{e_iv_block_size::CAST128}}},
{e_cbc_algorithm::CAST256, mode_of_operation_map{.secure_key{e_key_block_size::CAST256}, .secure_ivector{e_iv_block_size::CAST256}}},
{e_cbc_algorithm::GOST, mode_of_operation_map{.secure_key{e_key_block_size::GOST}, .secure_ivector{e_iv_block_size::GOST}}},
{e_cbc_algorithm::HIGHT, mode_of_operation_map{.secure_key{e_key_block_size::HIGHT}, .secure_ivector{e_iv_block_size::HIGHT}}},
{e_cbc_algorithm::IDEA, mode_of_operation_map{.secure_key{e_key_block_size::IDEA}, .secure_ivector{e_iv_block_size::IDEA}}},
{e_cbc_algorithm::MARS, mode_of_operation_map{.secure_key{e_key_block_size::MARS}, .secure_ivector{e_iv_block_size::MARS}}},
{e_cbc_algorithm::RC2, mode_of_operation_map{.secure_key{e_key_block_size::RC2}, .secure_ivector{e_iv_block_size::RC2}}},
{e_cbc_algorithm::RC5, mode_of_operation_map{.secure_key{e_key_block_size::RC5}, .secure_ivector{e_iv_block_size::RC5}}},
{e_cbc_algorithm::RC6, mode_of_operation_map{.secure_key{e_key_block_size::RC6}, .secure_ivector{e_iv_block_size::RC6}}},
{e_cbc_algorithm::SEED, mode_of_operation_map{.secure_key{e_key_block_size::SEED}, .secure_ivector{e_iv_block_size::SEED}}},
{e_cbc_algorithm::SERPENT, mode_of_operation_map{.secure_key{e_key_block_size::SERPENT}, .secure_ivector{e_iv_block_size::SERPENT}}},
{e_cbc_algorithm::SIMON128, mode_of_operation_map{.secure_key{e_key_block_size::SIMON128}, .secure_ivector{e_iv_block_size::SIMON128}}},
{e_cbc_algorithm::SPECK128, mode_of_operation_map{.secure_key{e_key_block_size::SPECK128}, .secure_ivector{e_iv_block_size::SPECK128}}},
};
std::unordered_map<e_gcm_algorithm, mode_of_operation_map> gcm_map_block{
{e_gcm_algorithm::AES, mode_of_operation_map{.secure_key{e_key_block_size::AES}, .secure_ivector{e_iv_block_size::AES}}},
{e_gcm_algorithm::MARS, mode_of_operation_map{.secure_key{e_key_block_size::MARS}, .secure_ivector{e_iv_block_size::MARS}}},
{e_gcm_algorithm::RC6, mode_of_operation_map{.secure_key{e_key_block_size::RC6}, .secure_ivector{e_iv_block_size::RC6}}},
{e_gcm_algorithm::TWOFISH, mode_of_operation_map{.secure_key{e_key_block_size::TWOFISH}, .secure_ivector{e_iv_block_size::TWOFISH}}}};
/**
* EAX Mode of Operation Secure Key/Initialization-Vector block size aggregation.
*/
std::unordered_map<e_eax_algorithm, mode_of_operation_map> eax_map_block{
{e_eax_algorithm::AES, mode_of_operation_map{.secure_key{e_key_block_size::AES}, .secure_ivector{e_iv_block_size::AES}}},
{e_eax_algorithm::BLOWFISH, mode_of_operation_map{.secure_key{e_key_block_size::BLOWFISH}, .secure_ivector{e_iv_block_size::BLOWFISH}}},
{e_eax_algorithm::CAST128, mode_of_operation_map{.secure_key{e_key_block_size::CAST128}, .secure_ivector{e_iv_block_size::CAST128}}},
{e_eax_algorithm::CAST256, mode_of_operation_map{.secure_key{e_key_block_size::CAST256}, .secure_ivector{e_iv_block_size::CAST256}}},
{e_eax_algorithm::GOST, mode_of_operation_map{.secure_key{e_key_block_size::GOST}, .secure_ivector{e_iv_block_size::GOST}}},
{e_eax_algorithm::HIGHT, mode_of_operation_map{.secure_key{e_key_block_size::HIGHT}, .secure_ivector{e_iv_block_size::HIGHT}}},
{e_eax_algorithm::IDEA, mode_of_operation_map{.secure_key{e_key_block_size::IDEA}, .secure_ivector{e_iv_block_size::IDEA}}},
{e_eax_algorithm::LEA, mode_of_operation_map{.secure_key{e_key_block_size::LEA}, .secure_ivector{e_iv_block_size::LEA}}},
{e_eax_algorithm::MARS, mode_of_operation_map{.secure_key{e_key_block_size::MARS}, .secure_ivector{e_iv_block_size::MARS}}},
{e_eax_algorithm::RC5, mode_of_operation_map{.secure_key{e_key_block_size::RC5}, .secure_ivector{e_iv_block_size::RC5}}},
{e_eax_algorithm::RC6, mode_of_operation_map{.secure_key{e_key_block_size::RC6}, .secure_ivector{e_iv_block_size::RC6}}},
{e_eax_algorithm::SEED, mode_of_operation_map{.secure_key{e_key_block_size::SEED}, .secure_ivector{e_iv_block_size::SEED}}},
{e_eax_algorithm::SERPENT, mode_of_operation_map{.secure_key{e_key_block_size::SERPENT}, .secure_ivector{e_iv_block_size::SERPENT}}},
{e_eax_algorithm::SIMON128, mode_of_operation_map{.secure_key{e_key_block_size::SIMON128}, .secure_ivector{e_iv_block_size::SIMON128}}},
{e_eax_algorithm::SPECK128, mode_of_operation_map{.secure_key{e_key_block_size::SPECK128}, .secure_ivector{e_iv_block_size::SPECK128}}},
};
std::unordered_map<e_cfb_algorithm, mode_of_operation_map> cfb_map_block{
{e_cfb_algorithm::AES, mode_of_operation_map{.secure_key{e_key_block_size::AES}, .secure_ivector{e_iv_block_size::AES}}},
{e_cfb_algorithm::BLOWFISH, mode_of_operation_map{.secure_key{e_key_block_size::BLOWFISH}, .secure_ivector{e_iv_block_size::BLOWFISH}}},
{e_cfb_algorithm::CAST128, mode_of_operation_map{.secure_key{e_key_block_size::CAST128}, .secure_ivector{e_iv_block_size::CAST128}}},
{e_cfb_algorithm::CAST256, mode_of_operation_map{.secure_key{e_key_block_size::CAST256}, .secure_ivector{e_iv_block_size::CAST256}}},
{e_cfb_algorithm::IDEA, mode_of_operation_map{.secure_key{e_key_block_size::IDEA}, .secure_ivector{e_iv_block_size::IDEA}}},
{e_cfb_algorithm::RC2, mode_of_operation_map{.secure_key{e_key_block_size::RC2}, .secure_ivector{e_iv_block_size::RC2}}},
{e_cfb_algorithm::RC5, mode_of_operation_map{.secure_key{e_key_block_size::RC5}, .secure_ivector{e_iv_block_size::RC5}}},
};
std::unordered_map<e_ofb_algorithm, mode_of_operation_map> ofb_map_block{
{e_ofb_algorithm::AES, mode_of_operation_map{.secure_key{e_key_block_size::AES}, .secure_ivector{e_iv_block_size::AES}}},
{e_ofb_algorithm::BLOWFISH, mode_of_operation_map{.secure_key{e_key_block_size::BLOWFISH}, .secure_ivector{e_iv_block_size::BLOWFISH}}},
{e_ofb_algorithm::CAST128, mode_of_operation_map{.secure_key{e_key_block_size::CAST128}, .secure_ivector{e_iv_block_size::CAST128}}},
{e_ofb_algorithm::CAST256, mode_of_operation_map{.secure_key{e_key_block_size::CAST256}, .secure_ivector{e_iv_block_size::CAST256}}},
{e_ofb_algorithm::IDEA, mode_of_operation_map{.secure_key{e_key_block_size::IDEA}, .secure_ivector{e_iv_block_size::IDEA}}},
{e_ofb_algorithm::RC2, mode_of_operation_map{.secure_key{e_key_block_size::RC2}, .secure_ivector{e_iv_block_size::RC2}}},
{e_ofb_algorithm::RC5, mode_of_operation_map{.secure_key{e_key_block_size::RC5}, .secure_ivector{e_iv_block_size::RC5}}},
};
std::unordered_map<e_ctr_algorithm, mode_of_operation_map> ctr_map_block{
{e_ctr_algorithm::AES, mode_of_operation_map{.secure_key{e_key_block_size::AES}, .secure_ivector{e_iv_block_size::AES}}},
{e_ctr_algorithm::BLOWFISH, mode_of_operation_map{.secure_key{e_key_block_size::BLOWFISH}, .secure_ivector{e_iv_block_size::BLOWFISH}}},
{e_ctr_algorithm::CAST128, mode_of_operation_map{.secure_key{e_key_block_size::CAST128}, .secure_ivector{e_iv_block_size::CAST128}}},
{e_ctr_algorithm::CAST256, mode_of_operation_map{.secure_key{e_key_block_size::CAST256}, .secure_ivector{e_iv_block_size::CAST256}}},
{e_ctr_algorithm::IDEA, mode_of_operation_map{.secure_key{e_key_block_size::IDEA}, .secure_ivector{e_iv_block_size::IDEA}}},
{e_ctr_algorithm::RC2, mode_of_operation_map{.secure_key{e_key_block_size::RC2}, .secure_ivector{e_iv_block_size::RC2}}},
{e_ctr_algorithm::RC5, mode_of_operation_map{.secure_key{e_key_block_size::RC5}, .secure_ivector{e_iv_block_size::RC5}}},
};
/* Class *\
\*+++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
class ByteCrypt
{
std::unique_ptr<string_t> secret_key = std::make_unique<string_t>("");
std::uint16_t cipher_iteration_count = DEFAULT_CIPHER_ITERATION_COUNTER;
std::uint16_t default_sec_key_size = DEFAULT_SEC_BLOCK_KEY_SIZE;
std::uint16_t default_sec_iv_size = DEFAULT_SEC_BLOCK_IV_SIZE;
static constexpr std::array<std::uint16_t, 5> rsa_key_size_options{512u, 1024u, 2048u, 3072u, 4096u};
operation_mode op_mode{
.cbc{cbc_map_block}, // CBC mode of operation supported algorithms and key/initialization vector block size
.gcm{gcm_map_block}, // GCM mode of operation supported algorithms and key/initialization vector block size
.eax{eax_map_block}, // EAX mode of operation supported algorithms and key/initialization vector block size
.cfb{cfb_map_block}, // CFB mode of operation supported algorithms and key/initialization vector block size
.ofb{ofb_map_block}, // OFB mode of operation supported algorithms and key/initialization vector block size
.ctr{ctr_map_block}, // CTR mode of operation supported algorithms and key/initialization vector block size
};
public:
inline ByteCrypt() noexcept = default;
inline ByteCrypt(const ByteCrypt &o_instance) noexcept
{
this->__constructor_copy_handler(o_instance);
};
inline ByteCrypt &operator=(const ByteCrypt &o_instance) noexcept
{
this->__constructor_copy_handler(o_instance);
return *this;
};
inline ByteCrypt(ByteCrypt &&o_instance) noexcept
{
this->__constructor_copy_handler(o_instance);
};
inline ByteCrypt &operator=(ByteCrypt &&o_instance) noexcept
{
this->__constructor_copy_handler(o_instance);
return *this;
};
inline ByteCrypt(const string_view_t initial_secret_key) noexcept
{
*this->secret_key = initial_secret_key;
};
inline ByteCrypt(const string_view_t &initial_secret_key, const byte key[], const byte iv[], const std::uint16_t key_size, const std::uint16_t iv_size) noexcept
{
*this->secret_key = (string_t)initial_secret_key;
};
inline const bool operator==(const ByteCrypt &o_instance) const noexcept
{
return this->secret_key.get()->compare(o_instance.secret_key.get()->c_str()) == 0;
};
inline const bool operator!=(const ByteCrypt &o_instance) const noexcept
{
return this->secret_key.get()->compare(o_instance.secret_key.get()->c_str()) != 0;
};
/**
* Hash buffer with specified algorithm and return digest.
* @param string_view_t buffer to hash
* @param e_hash_algo_option hash algorithm
* @returns op_frame<string_t> structure containing error and result.
*/
__hint_hash__ const op_frame<string_t> hash(const string_view_t buffer, const e_hash_algo_option sha = e_hash_algo_option::SHA256) const
{
op_frame<string_t> return_block{.result{}, .error{.error_msg{}, .has_error{false}}};
std::unique_ptr<hash_transformer_t> algo;
try
{
switch ((std::uint16_t)sha)
{
case (std::uint16_t)e_hash_algo_option::SHA1: algo = std::make_unique<sha1_t>(); break;
case (std::uint16_t)e_hash_algo_option::SHA224: algo = std::make_unique<sha224_t>(); break;
case (std::uint16_t)e_hash_algo_option::SHA256: algo = std::make_unique<sha256_t>(); break;
case (std::uint16_t)e_hash_algo_option::SHA384: algo = std::make_unique<sha384_t>(); break;
case (std::uint16_t)e_hash_algo_option::SHA512: algo = std::make_unique<sha512_t>(); break;
case (std::uint16_t)e_hash_algo_option::BLAKE2: algo = std::make_unique<blake2_t>(); break;
case (std::uint16_t)e_hash_algo_option::MD5: algo = std::make_unique<md5_t>(); break;
case (std::uint16_t)e_hash_algo_option::RIPEMD160: algo = std::make_unique<ripemd160_t>(); break;
case (std::uint16_t)e_hash_algo_option::TIGER: algo = std::make_unique<tiger_t>(); break;
case (std::uint16_t)e_hash_algo_option::WHIRLPOOL: algo = std::make_unique<whirlpool_t>(); break;
}
string_source_t(buffer.data(), true, new hash_filter_t(*algo, new hex_encoder_t(new string_sink_t(return_block.result))));
}
catch (const std::exception &e)
{
return_block.error.has_error = true;
return_block.error.error_msg = e.what();
}
return return_block;
};
/**
* generate a pair of DER RSA keys with rsa_key_size size, size defaults to 2048 bits.
* @param std::uint16_t rsa key size
* @returns op_frame<rsa_key_pair_struct> structure with public and private key association, and error info if any!
*/
__hint_generate_rsa_key_der_pair__ const op_frame<rsa_key_pair_struct> generate_rsa_key_der_pair(const std::uint16_t rsa_key_size = 2048U)
{
op_frame<rsa_key_pair_struct> return_block{.result{.public_key{std::nullopt}, .private_key{std::nullopt}, .state{false}}, .error{.error_msg{""}, .has_error{false}}};
std::function<void(const string_view_t &msg)> reset_block_state([&return_block](const string_view_t &msg) -> void {
return_block.error.has_error = true;
return_block.error.error_msg = msg.data();
return_block.result.state = false;
if (!return_block.result.private_key->empty())
return_block.result.private_key->clear();
if (!return_block.result.public_key->empty())
return_block.result.public_key->clear();
});
if (!this->__is_rsa_key_size_valid(rsa_key_size)) [[unlikely]]
return return_block;
try
{
entropy_seed_t entropy;
invertible_rsa_t private_key;
private_key.Initialize(entropy, rsa_key_size);
rsa_public_key_t public_key(private_key);
string_t private_key_result, public_key_result, private_key_result_encoded, public_key_result_encoded;
string_sink_t private_key_sink(private_key_result);
private_key.DEREncode(private_key_sink);
private_key_sink.MessageEnd();
string_source_t(private_key_result, true, new base64_encoder_t(new string_sink_t(private_key_result_encoded)));
if (!private_key_result_encoded.empty()) [[likely]]
{
try
{
string_t public_key_result;
string_sink_t public_key_sink(public_key_result);
public_key.DEREncode(public_key_sink);
public_key_sink.MessageEnd();
string_source_t(public_key_result, true, new base64_encoder_t(new string_sink_t(public_key_result_encoded)));
}
catch (const std::exception &e)
{
throw;
}
}
if (!private_key_result_encoded.empty() && !public_key_result_encoded.empty()) [[likely]]
{
return_block.result.private_key = std::move(private_key_result_encoded);
return_block.result.public_key = std::move(public_key_result_encoded);
if (this->__rsa_key_pair_verify(return_block.result)) [[likely]]
{
return_block.result.state = true;
return_block.error.has_error = false;
}
}
}
catch (const std::exception &e)
{
std::cerr << "Exception: " << e.what() << "\n";
reset_block_state(e.what());
}
return return_block;
};
/**
* generate a pair of RSA PEM key pair with rsa_key_size size.
* @param std::uint16_t rsa key size
* @returns op_frame<rsa_key_pair_struct> structure with the rsa generated PEM keys.
*/
__hint_generate_rsa_key_pem_pair__ const op_frame<rsa_key_pair_struct> generate_rsa_key_pem_pair(const std::uint16_t rsa_key_size = 2048U)
{
op_frame<rsa_key_pair_struct> rsa_keys = this->generate_rsa_key_der_pair(rsa_key_size);
if (!this->__is_rsa_key_size_valid(rsa_key_size)) [[unlikely]]
return rsa_keys;
try
{
string_t private_decoded, public_decoded;
this->__rsa_key_pem_set_header(private_decoded, false);
this->__rsa_key_pem_set_header(public_decoded, true);
private_decoded += rsa_keys.result.private_key.value_or("");
public_decoded += rsa_keys.result.public_key.value_or("");
this->__rsa_key_pem_set_footer(private_decoded, false);
this->__rsa_key_pem_set_footer(public_decoded, true);
if (private_decoded.empty() || public_decoded.empty()) [[unlikely]]
throw std::runtime_error("decoded private/public pem key error!");
rsa_keys.result.private_key = std::move(private_decoded);
rsa_keys.result.public_key = std::move(public_decoded);
}
catch (const std::exception &e)
{
rsa_keys.error.has_error = true;
rsa_keys.error.error_msg = e.what();
rsa_keys.result.state = false;
if (rsa_keys.result.private_key.has_value())
rsa_keys.result.private_key->clear();
if (rsa_keys.result.public_key.has_value())
rsa_keys.result.public_key->clear();
}
return rsa_keys;
};
/**
* sign message with rsa_key private key, function generates signature and returns it.
* @param string_view_t message to sign
* @param string_t& rsa private key for signature generation
* @returns op_frame<string_t> frame block containing signature as result and error(if any) as state meta-information
*/
__hint_sign_message__ const op_frame<string_t> sign_message(const string_view_t message, const string_t &rsa_key)
{
op_frame<string_t> return_block{.result{}, .error{.error_msg{}, .has_error{true}}};
if (!this->__is_rsa_key_pem(rsa_key, e_rsa_key_pem_version::PRIVATE)) [[unlikely]]
{
return_block.error.error_msg = "rsa key size is invalid!";
return return_block;
}
else if (message.empty() || message.length() >= UINT32_MAX) [[unlikely]]
{
return_block.error.error_msg = "message too long, cannot be > 4294967295U!";
return return_block;
}
string_t clean_key(this->__rsa_key_meta_wipe(const_cast<string_t &&>(rsa_key)));
try
{
if (clean_key.empty()) [[unlikely]]
{
return_block.error.error_msg = "cannot remove rsa key header/footer!\n";
return return_block;
}
string_t private_key_decoded, signature;
string_source_t(clean_key, true, new base64_decoder_t(new string_sink_t(private_key_decoded)));
rsa_private_key_t private_key;
string_source_t private_key_source(private_key_decoded, true);
private_key.BERDecode(private_key_source);
rsa_signature_t signer(private_key);
entropy_seed_t entropy;
string_source_t(message.data(), true, new rsa_signature_filter_t(entropy, signer, new string_sink_t(signature)));
string_t encoded_signature;
string_source_t(signature, true, new base64_encoder_t(new string_sink_t(encoded_signature)));
return_block.result = encoded_signature;
return_block.error.has_error = false;
}
catch (const std::exception &e)
{
return_block.error.error_msg = e.what();
return_block.error.has_error = true;
if (return_block.result.empty() == false)
return_block.result.clear();
}
return return_block;
};
/**
* verify message rsa private key signature with signature_str signature, and rsa_key public key
* @param string_view_t& message to verify signature from
* @param string_view_t& signature to use
* @param string_t& RSA public key
* @returns bool true if verification succeded
*/
__hint_verify_signature__ const bool verify_signature(const string_view_t &message, const string_view_t &signature_str, const string_t &rsa_key)
{
bool verification_result;
if (!this->__is_rsa_key_pem(rsa_key, e_rsa_key_pem_version::PUBLIC)) [[unlikely]] return false;
try
{
string_t public_key_decoded;
string_t pure_key(this->__rsa_key_meta_wipe(const_cast<string_t &&>(rsa_key)));
string_source_t(pure_key, true, new base64_decoder_t(new string_sink_t(public_key_decoded)));
rsa_public_key_t public_key;
string_source_t public_key_source(public_key_decoded, true);
public_key.BERDecode(public_key_source);
string_t signature_decoded;
string_source_t(signature_str.data(), true, new base64_decoder_t(new string_sink_t(signature_decoded)));
rsa_signature_verify_t verifier(public_key);
verification_result = verifier.VerifyMessage((const byte *)message.data(), message.length(), (const byte *)signature_decoded.data(), signature_decoded.size());
}
catch (const std::exception &e)
{
verification_result = false;
}
return verification_result;
};
/**
* save rsa_key into path(file name).
* @param string_t& path to key
* @param string_view_t& rsa key(public/private)
* @returns op_frame<bool> true if key saved
*/
__hint_save_rsa_key__ const op_frame<bool> save_rsa_key(const string_view_t &path, const string_t &rsa_key)
{
op_frame<bool> return_block{.result{false}, .error{.error_msg{}, .has_error{true}}};
try
{
if (path.empty()) [[unlikely]] throw std::invalid_argument("path to store rsa key not invalid!");
if (rsa_key.empty()) [[unlikely]] throw std::invalid_argument("rsa key value invalid!");
std::ofstream file_handler(path.data(), std::ios::binary | std::ios::out);
if (!file_handler.is_open()) [[unlikely]] throw std::ofstream::failure::runtime_error("file stream for writing rsa key not open!");
file_handler << rsa_key;
file_handler.close();
return_block.error.has_error = false;
return_block.result = true;
}
catch (const std::exception &e)
{
return_block.error.has_error = true;
return_block.error.error_msg = e.what();
}
return return_block;
};
/**
* load rsa key from file_name.
* @param string_view_t& address of file where key stored.
* @returns rsa_key_block_load structure containing loaded rsa key
*/
__hint_load_rsa_key__ const rsa_key_block_load load_rsa_key(const string_view_t &file_name)
{
rsa_key_block_load rsa_loader;
try
{
if (file_name.empty()) [[unlikely]] throw std::invalid_argument("path to read rsa key not invalid!");
std::ifstream file_handler(file_name.data(), std::ios::binary | std::ios::in);
if (!file_handler.is_open()) [[unlikely]] throw std::ifstream::failure::runtime_error("file stream for reading rsa key not open!");
string_t read_key;
rsa_loader.key.clear();
do rsa_loader.key += read_key += "\n";
while (std::getline(file_handler, read_key));
file_handler.close();
if (!rsa_loader.key.empty()) [[likely]] rsa_loader.status = true;
}
catch (const std::exception &e)
{
rsa_loader.error = e.what();
rsa_loader.status = false;
if (!rsa_loader.key.empty()) rsa_loader.key.clear();
}
return rsa_loader;
};
/**
*
* Generate Random Bytes using secure system entropy generator with 16 bytes output block size.
* @returns op_frame<string_t> the random generated string block frame
*