1
0

migration

This commit is contained in:
2025-04-29 22:08:00 +03:00
commit ad28507008
232 changed files with 12299 additions and 0 deletions

View File

@ -0,0 +1,24 @@
# CompileFlags:
# Add:
# - -I/usr/x86_64-w64-mingw32/include
# - -I/usr/x86_64-w64-mingw32/include/w32api
# - -DUNICODE
# - -D_UNICODE
CompileFlags:
Add: [-Wall, -Wextra, -std=c++17, -DBUILD_SHARED, --target=x86_64-w64-mingw32]
CompilationDatabase: build/
Diagnostics:
UnusedIncludes: Strict
InlayHints:
Enabled: Yes
ParameterNames: Yes
DeducedTypes: Yes
Index:
Background: Build
Hover:
ShowAKA: Yes

View File

@ -0,0 +1,28 @@
cc := "x86_64-w64-mingw32-clang++"
flags := "-Wall -Wextra -std=c++17"
@clean:
rm -rf build
@makedir-build: clean
mkdir -p build
@build-lib-shared: makedir-build
{{cc}} {{flags}} -D BUILD_SHARED --shared -Wl,--out-implib,build/rsa.lib library/rsa.cpp -o build/rsa.dll
@build-app-shared: build-lib-shared
{{cc}} {{flags}} application/main.cpp -L build -l rsa -o build/main.exe
@build-lib-static: makedir-build
{{cc}} {{flags}} -c -D BUILD_SHARED library/rsa.cpp -o build/rsa.o
ar rcs build/rsa.lib build/rsa.o
@build-app-static: build-lib-static
{{cc}} {{flags}} -D BUILD_STATIC application/main.cpp -L build -l rsa -o build/main.exe
@run:
wine cmd /c build/main.exe
run-shared: build-app-shared run
run-static: build-app-static run

View File

@ -0,0 +1,74 @@
#include <stdint.h>
#include <stdio.h>
#include "../library/rsa.h"
char *int128_to_str(__int128_t n) {
static char str[41] = {0};
char *s = str + sizeof(str) - 1;
bool neg = n < 0;
if (neg)
n = -n;
do {
*--s = "0123456789"[n % 10];
n /= 10;
} while (n);
if (neg)
*--s = '-';
return s;
}
int main() {
// uint64_t p0 = 8589934609, q0 = 2147483693, e0 = 65537;
// uint64_t p1 = 2147483659, q1 = 8589934621, e1 = 65537;
uint32_t p0 = 2147483659, q0 = 2147483693, e0 = 65537; // pair_0
uint32_t p1 = 2147483659, q1 = 2147483693, e1 = 65537; // pair_1
rsa_Pair pair_0 = rsa_gen_pair(p0, q0, e0);
rsa_Pair pair_1 = rsa_gen_pair(p1, q1, e1);
printf("pair_0:\n");
printf("- e:\t%s\n", int128_to_str(pair_0.e));
printf("- d:\t%s\n", int128_to_str(pair_0.d));
printf("- n:\t%s\n", int128_to_str(pair_0.n));
printf("pair_1:\n");
printf("- e:\t%s\n", int128_to_str(pair_1.e));
printf("- d:\t%s\n", int128_to_str(pair_1.d));
printf("- n:\t%s\n", int128_to_str(pair_1.n));
uint64_t t[] = {4096, 17, 8, 65500, 0x9fffffff};
for (size_t i = 0; i < 5; i++) {
printf("- - - - - - - - - - - - - - - - - -\n");
printf("t[%zu]:\t\t%lld\n", i, t[i]);
__int128_t e1t = rsa_encrypt(t[i], pair_0.e, pair_0.n);
printf("e1t:\t\t%s\n", int128_to_str(e1t));
__int128_t d1e1t = rsa_decrypt(e1t, pair_0.d, pair_0.n);
printf("d1e1t\t\t%s\n", int128_to_str(d1e1t));
if (d1e1t != t[i]) {
fprintf(stderr, "Error: decrypt[i]on failed - expected: %llu; got: %s\n",
t[i], int128_to_str(d1e1t));
return 1;
}
__int128_t e0d1e1t = rsa_encrypt(t[i], pair_1.e, pair_1.n);
printf("e0d1e1t:\t%s\n", int128_to_str(e0d1e1t));
__int128_t d0e0d1e1t = rsa_decrypt(e0d1e1t, pair_1.d, pair_1.n);
printf("d0e0d1e1t:\t%s\n", int128_to_str(d0e0d1e1t));
if (d0e0d1e1t != t[i]) {
fprintf(stderr, "Error: decrypt[i]on failed - expected: %llu; got: %s\n",
t[i], int128_to_str(d0e0d1e1t));
return 1;
}
}
return 0;
}

View File

@ -0,0 +1,78 @@
#include "rsa.h"
__int128_t mul_mod(__int128_t a, __int128_t b, __int128_t mod) {
__int128_t res = 0;
a %= mod;
while (b > 0) {
if (b & 1)
res = (res + a) % mod;
a = (a << 1) % mod;
b >>= 1;
}
return res;
}
__int128_t pow_mod(__int128_t base, __int128_t exp, __int128_t mod) {
__int128_t res = 1;
base %= mod;
while (exp > 0) {
if (exp & 1)
res = mul_mod(res, base, mod);
base = mul_mod(base, base, mod);
exp >>= 1;
}
return res;
}
__int128_t mod_inverse(__int128_t e, __int128_t phi) {
__int128_t a = e, b = phi;
__int128_t x = 1, y = 0;
__int128_t x1 = 0, y1 = 1;
__int128_t q, temp;
while (b != 0) {
q = a / b;
temp = a % b;
a = b;
b = temp;
temp = x - q * x1;
x = x1;
x1 = temp;
temp = y - q * y1;
y = y1;
y1 = temp;
}
if (x < 0)
x += phi;
return x;
}
rsa_Pair rsa_gen_pair(__int128_t p, __int128_t q, __int128_t e) {
rsa_Pair pair;
pair.e = e;
pair.n = p * q;
__int128_t phi = (p - 1) * (q - 1);
pair.d = mod_inverse(e, phi);
return pair;
}
__int128_t rsa_encrypt(__int128_t data, __int128_t e, __int128_t n) {
return pow_mod(data, e, n);
}
__int128_t rsa_decrypt(__int128_t data, __int128_t d, __int128_t n) {
return pow_mod(data, d, n);
}

View File

@ -0,0 +1,35 @@
#ifndef RSA_LIB
#define RSA_LIB
#ifdef __cplusplus
extern "C" {
#endif
#ifdef _WIN32
#ifdef BUILD_SHARED
#define BUILD_SPEC __declspec(dllexport)
#elif BUILD_STATIC
#define BUILD_SPEC
#else
#define BUILD_SPEC __declspec(dllimport)
#endif
#else
#define BUILD_SPEC
#endif
typedef struct {
__int128_t d;
__int128_t e;
__int128_t n;
} rsa_Pair;
BUILD_SPEC rsa_Pair rsa_gen_pair(__int128_t p, __int128_t q, __int128_t e);
BUILD_SPEC __int128_t rsa_encrypt(__int128_t data, __int128_t e, __int128_t n);
BUILD_SPEC __int128_t rsa_decrypt(__int128_t data, __int128_t d, __int128_t n);
#ifdef __cplusplus
}
#endif
#endif