Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Lukas Holderried
science-meets-industry-2021-students-lukash
Commits
388a3f59
Commit
388a3f59
authored
Nov 30, 2021
by
Lukas Holderried
Browse files
final hopefully
parent
8e21a0a2
Changes
3
Hide whitespace changes
Inline
Side-by-side
platformio.ini
View file @
388a3f59
...
...
@@ -17,8 +17,13 @@ build_type = release
lib_deps
=
https://gitlab.elektrotechnik.hs-augsburg.de/hackenbs/mbed-http.git
https://gitlab.elektrotechnik.hs-augsburg.de/hackenbs/mbed-mfrc522.git
https://gitlab.elektrotechnik.hs-augsburg.de/hackenbs/mbed-logger.git
debug_load_mode
=
manual
monitor_speed
=
115200
monitor_flags
=
--raw
build_flags
=
-DLOG_LEVEL_INFO
[env:smi]
\ No newline at end of file
src/crypto.cpp
View file @
388a3f59
...
...
@@ -28,7 +28,7 @@ void getRandom(uint8_t *buffer, size_t outlen) //gives a Random number of size "
while
(
size
<
outlen
)
{
size_t
tmplen
=
0
;
trng_get_bytes
(
&
rngctx
,
&
buffer
[
size
],
outlen
-
size
,
&
tmplen
);
MBED_ASSERT
(
trng_get_bytes
(
&
rngctx
,
&
buffer
[
size
],
outlen
-
size
,
&
tmplen
)
==
0
)
;
size
+=
tmplen
;
}
...
...
@@ -43,17 +43,15 @@ int crypto_trng_generate(void *rngctx, uint8_t *buffer, size_t length) //functio
void
generateKey
()
//generates a rsa key pair and shares it with the server
{
int
returncheck
;
uint8_t
buffer
[
256
];
//buffer to send public key
trngInit
();
returncheck
=
mbedtls_rsa_gen_key
(
&
ctx
,
crypto_trng_generate
,
&
rngctx
,
2048
,
0x10001
);
printf
(
"Result of RNG: %d
\n
"
,
returncheck
);
MBED_ASSERT
(
mbedtls_rsa_gen_key
(
&
ctx
,
crypto_trng_generate
,
&
rngctx
,
2048
,
0x10001
)
==
0
);
trng_free
(
&
rngctx
);
mbedtls_mpi_write_binary
(
&
ctx
.
N
,
buffer
,
sizeof
(
buffer
));
MBED_ASSERT
(
mbedtls_mpi_write_binary
(
&
ctx
.
N
,
buffer
,
sizeof
(
buffer
))
==
0
)
;
HttpRequest
request
(
wifi2
,
HTTP_GET
,
"http://smi-server.stefan-hackenberg.de/crypto/rsa/client_modulus"
);
HttpResponse
*
response
=
request
.
send
(
buffer
,
sizeof
(
buffer
));
...
...
@@ -64,20 +62,20 @@ void readServerKey() //function that reads servers rsa key and saves it in a rsa
const
uint8_t
public_exponent
[
3
]
=
{
0x01
,
0x00
,
0x01
};
HttpRequest
request
(
wifi2
,
HTTP_GET
,
"http://smi-server.stefan-hackenberg.de/crypto/rsa/server_modulus"
);
HttpResponse
*
response
=
request
.
send
(
NULL
,
0
);
mbedtls_rsa_import_raw
(
&
ctxserver
,
(
uint8_t
*
)
response
->
get_body
(),
response
->
get_body_length
(),
NULL
,
0
,
NULL
,
0
,
NULL
,
0
,
public_exponent
,
sizeof
(
public_exponent
)
MBED_ASSERT
(
mbedtls_rsa_import_raw
(
&
ctxserver
,
(
uint8_t
*
)
response
->
get_body
(),
response
->
get_body_length
(),
NULL
,
0
,
NULL
,
0
,
NULL
,
0
,
public_exponent
,
sizeof
(
public_exponent
)
);
)
==
0
);
printHex
((
uint8_t
*
)
response
->
get_body
(),
response
->
get_body_length
());
}
...
...
@@ -100,16 +98,16 @@ void keyExchange(uint8_t *key)
trng_free
(
&
rngctx
);
//encrypt key part with server context and send it
trngInit
();
mbedtls_rsa_rsaes_oaep_encrypt
(
&
ctxserver
,
crypto_trng_generate
,
&
rngctx
,
MBEDTLS_RSA_PUBLIC
,
NULL
,
0
,
sizeof
(
random16
),
random16
,
encrypted
);
MBED_ASSERT
(
mbedtls_rsa_rsaes_oaep_encrypt
(
&
ctxserver
,
crypto_trng_generate
,
&
rngctx
,
MBEDTLS_RSA_PUBLIC
,
NULL
,
0
,
sizeof
(
random16
),
random16
,
encrypted
)
==
0
)
;
trng_free
(
&
rngctx
);
HttpRequest
request
(
wifi2
,
HTTP_GET
,
"http://smi-server.stefan-hackenberg.de/crypto/rsa/client_aes"
);
HttpResponse
*
response
=
request
.
send
(
encrypted
,
sizeof
(
encrypted
));
//decrypt server answer and create xor of both key parts for full key
trng_t
rng2
;
trng_init
(
&
rng2
);
mbedtls_rsa_rsaes_oaep_decrypt
(
&
ctx
,
crypto_trng_generate
,
&
rng2
,
MBEDTLS_RSA_PRIVATE
,
NULL
,
0
,
&
len
,
(
uint8_t
*
)
response
->
get_body
(),
server
,
sizeof
(
server
));
MBED_ASSERT
(
mbedtls_rsa_rsaes_oaep_decrypt
(
&
ctx
,
crypto_trng_generate
,
&
rng2
,
MBEDTLS_RSA_PRIVATE
,
NULL
,
0
,
&
len
,
(
uint8_t
*
)
response
->
get_body
(),
server
,
sizeof
(
server
))
==
0
)
;
printf
(
"Server answer decrypted
\n
"
);
trng_free
(
&
rng2
);
printHex2
(
server
,
sizeof
(
server
));
...
...
src/main.cpp
View file @
388a3f59
...
...
@@ -74,7 +74,7 @@ void solveCrypto(void) //Solve puzzle with encrypted connection
{
mbedtls_aes_init
(
&
aesctx
);
keyExchange
(
key
);
mbedtls_aes_setkey_enc
(
&
aesctx
,
key
,
128
);
MBED_ASSERT
(
mbedtls_aes_setkey_enc
(
&
aesctx
,
key
,
128
)
==
0
)
;
uint8_t
hash
[
32
];
//array for hashes created from the snippets
uint8_t
solution
[
71
];
//array for solution to send to the server
int
ctr
;
//defined here since it's used outside of the loop aswell
...
...
@@ -88,10 +88,10 @@ void solveCrypto(void) //Solve puzzle with encrypted connection
HttpRequest
request
(
wifi
,
HTTP_GET
,
"http://smi-server.stefan-hackenberg.de/crypto/aes_ctr/getsnippet"
);
uid
[
7
]
=
i
;
//include reqeust Number in the uid for requesting different snippets
mbedtls_aes_crypt_ctr
(
&
aesctx
,
sizeof
(
uid
),
ptr
,
nonce
,
block
,
uid
,
out
);
//encryption of request
MBED_ASSERT
(
mbedtls_aes_crypt_ctr
(
&
aesctx
,
sizeof
(
uid
),
ptr
,
nonce
,
block
,
uid
,
out
)
==
0
)
;
//encryption of request
HttpResponse
*
response
=
request
.
send
(
out
,
sizeof
(
out
));
//send request to server
uint8_t
snippet
[
response
->
get_body_length
()];
//create an array to save the decrypted snippet in
mbedtls_aes_crypt_ctr
(
&
aesctx
,
response
->
get_body_length
(),
ptr
,
nonce
,
block
,
(
uint8_t
*
)
response
->
get_body
(),
snippet
);
MBED_ASSERT
(
mbedtls_aes_crypt_ctr
(
&
aesctx
,
response
->
get_body_length
(),
ptr
,
nonce
,
block
,
(
uint8_t
*
)
response
->
get_body
(),
snippet
)
==
0
)
;
mbedtls_sha256
(
snippet
,
response
->
get_body_length
(),
hash
,
0
);
//hashing the snippet
printHex
(
hash
,
11
);
for
(
ctr
=
0
;
ctr
<
64
;
ctr
++
)
//loop to compare the first 11 bytes of the hash to the data on the Tag
...
...
@@ -114,7 +114,7 @@ void solveCrypto(void) //Solve puzzle with encrypted connection
solution
[
u
]
=
uid
[
u
];
}
printHex
(
solution
,
71
);
mbedtls_aes_crypt_ctr
(
&
aesctx
,
sizeof
(
solution
),
ptr
,
nonce
,
block
,
solution
,
solutionCrypt
);
MBED_ASSERT
(
mbedtls_aes_crypt_ctr
(
&
aesctx
,
sizeof
(
solution
),
ptr
,
nonce
,
block
,
solution
,
solutionCrypt
)
==
0
)
;
HttpRequest
request2
(
wifi
,
HTTP_GET
,
"http://smi-server.stefan-hackenberg.de/crypto/aes_ctr/solve"
);
HttpResponse
*
response2
=
request2
.
send
(
solutionCrypt
,
sizeof
(
solutionCrypt
));
printf
(
"Status Code: %d
\n
"
,
response2
->
get_status_code
());
//check status code of result
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment