Commit 48d77e4d authored by Stefan Hackenberg's avatar Stefan Hackenberg
Browse files

Initial commit

parents
Pipeline #874 failed with stages
in 32 seconds
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "gcc - Aktive Datei erstellen und debuggen",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Automatische Strukturierung und Einrückung für \"gdb\" aktivieren",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: gcc Aktive Datei kompilieren",
"miDebuggerPath": "/usr/bin/gdb"
}
]
}
\ No newline at end of file
{
"files.associations": {
"random.h": "c",
"rsa.h": "c"
}
}
\ No newline at end of file
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: gcc Aktive Datei kompilieren",
"command": "/usr/bin/gcc",
"args": [
"-g",
"-Og",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}",
"-lmbedcrypto"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Vom Debugger generierte Aufgabe."
}
],
"version": "2.0.0"
}
\ No newline at end of file
import hashlib
from Crypto.Cipher import AES, PKCS1_v1_5
from Crypto.PublicKey import RSA
def hex2int(string):
return int.from_bytes(bytes.fromhex(string), 'big')
def sha_empty():
print('sha_empty output:', hashlib.sha256(b'').hexdigest())
def sha_continue():
sha = hashlib.sha256()
sha.update(16 * b'\x00')
sha.update(16 * b'\xff')
print('sha_continue output:', sha.hexdigest())
def aes():
aes = AES.new(16 * b'\x00', AES.MODE_ECB)
print('aes output:', aes.encrypt(16 * b'\x00').hex())
def rsa_raw(n=None):
if n:
rsakey = RSA.construct((n, 0x10001))
else:
rsakey = RSA.generate(2048)
print('rsa_raw output:', pow(
int.from_bytes(16 * b'\x0f', 'big'),
rsakey.e,
rsakey.n,
).to_bytes(256, 'big').hex())
def rsa_pkcs1_v15(n=None, d=None, msg=None):
if n:
rsakey = RSA.construct((n, 0x10001, d))
else:
rsakey = RSA.generate(2048)
cipher = PKCS1_v1_5.new(rsakey)
if msg:
print('rsa_pkcs1_v15 output:', cipher.decrypt(msg, None).hex())
else:
print('rsa_pkcs1_v15 output:', cipher.encrypt(b'\x00').hex())
if __name__ == '__main__':
sha_empty()
sha_continue()
aes()
rsa_raw()
rsa_pkcs1_v15(
n=hex2int('97 b5 89 63 44 9a 35 da 0c 51 b7 7e c4 29 61 41'),
d=hex2int('0a 09 75 c6 da 66 b0 f6 0b 56 3b 0f 1f 0a b5 09'),
msg=bytes.fromhex('84 7c c7 a2 1f af e6 35 b8 1c bc 56 a6 40 56 58'),
)
\ No newline at end of file
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mbedtls/aes.h>
#include <mbedtls/sha256.h>
#include <mbedtls/rsa.h>
#include <sys/random.h>
#define assert(x) if (!(x)) {printf("FAIL %d\n", __LINE__); exit(-1);}
void printfhex(const char *msg, const uint8_t *buffer, size_t length)
{
printf("%s: ", msg);
for (size_t i = 0; i < length; i++)
{
printf("%02x ", buffer[i]);
}
printf("\n");
}
void printbignum(const char *msg, const mbedtls_mpi *num)
{
printf("%s: ", msg);
for (int i = num->n * sizeof(unsigned long) - 1; i >= 0; i--)
{
printf("%02x ", ((uint8_t *)num->p)[i]);
}
printf("\n");
}
void sha_empty(void)
{
uint8_t output[32];
assert(mbedtls_sha256_ret(NULL, 0, output, 0) == 0);
printfhex("sha_empty output", output, 32);
}
void sha_continue(void)
{
mbedtls_sha256_context ctx;
mbedtls_sha256_init(&ctx);
assert(mbedtls_sha256_starts_ret(&ctx, 0) == 0);
uint8_t data[16];
memset(data, 0, sizeof(data));
assert(mbedtls_sha256_update_ret(&ctx, data, 16) == 0);
memset(data, 0xff, sizeof(data));
assert(mbedtls_sha256_update_ret(&ctx, data, 16) == 0);
uint8_t output[32];
assert(mbedtls_sha256_finish_ret(&ctx, output) == 0);
printfhex("sha_continue output", output, 32);
mbedtls_sha256_free(&ctx);
}
void aes(void)
{
mbedtls_aes_context ctx;
mbedtls_aes_init(&ctx);
uint8_t key[16];
memset(key, 0, sizeof(key));
assert(mbedtls_aes_setkey_enc(&ctx, key, 128) == 0);
uint8_t data[16];
memset(data, 0, sizeof(data));
uint8_t output[16];
assert(mbedtls_aes_crypt_ecb(&ctx, MBEDTLS_AES_ENCRYPT, data, output) == 0);
printfhex("aes output", output, 16);
mbedtls_aes_free(&ctx);
}
void rsa_easy(void)
{
int p = 11;
int q = 13;
int n = p * q;
printf("rsa_easy output: %d\n", (5 * 5 * 5) % n);
}
int mbedrandom(void *rngctx, uint8_t *buffer, size_t length)
{
getrandom(buffer, length, GRND_RANDOM);
return 0;
}
void rsa_raw(void)
{
int keysize = 2048;
mbedtls_rsa_context ctx;
mbedtls_rsa_init(&ctx, MBEDTLS_RSA_PKCS_V15, 0);
assert(mbedtls_rsa_gen_key(&ctx, mbedrandom, NULL, keysize, 0x10001) == 0);
printbignum("rsa_raw P", &ctx.P);
printbignum("rsa_raw Q", &ctx.Q);
printbignum("rsa_raw N", &ctx.N);
uint8_t buffer[keysize / 8];
memset(buffer, 0x0f, sizeof(buffer));
assert(mbedtls_rsa_public(&ctx, buffer, buffer) == 0);
printfhex("rsa_raw output", buffer, sizeof(buffer));
}
void bignum(void)
{
mbedtls_mpi number;
mbedtls_mpi_init(&number);
mbedtls_mpi_lset(&number, 1);
mbedtls_mpi_shift_l(&number, 64);
printbignum("bignum number", &number);
}
void rsa_pkcs1_v15(void)
{
mbedtls_rsa_context ctx;
mbedtls_rsa_init(&ctx, MBEDTLS_RSA_PKCS_V15, 0);
assert(mbedtls_rsa_gen_key(&ctx, mbedrandom, NULL, 128, 0x10001) == 0);
printbignum("rsa_pkcs1_v15 P", &ctx.P);
printbignum("rsa_pkcs1_v15 Q", &ctx.Q);
printbignum("rsa_pkcs1_v15 N", &ctx.N);
printbignum("rsa_pkcs1_v15 D", &ctx.D);
uint8_t buffer[16];
memset(buffer, 0x00, sizeof(buffer));
uint8_t output[16];
assert(mbedtls_rsa_pkcs1_encrypt(
&ctx,
mbedrandom,
NULL,
MBEDTLS_RSA_PUBLIC,
1,
buffer,
output) == 0);
printfhex("rsa_pkcs1_v15 output", output, sizeof(output));
}
int main(void)
{
sha_empty();
sha_continue();
aes();
rsa_easy();
rsa_raw();
bignum();
rsa_pkcs1_v15();
return 0;
}
\ No newline at end of file
pycryptodome
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment