Commit 0abad409 authored by Jonas Hundseder's avatar Jonas Hundseder
Browse files

Stand ohne Code Style, jedoch mit Funktion (ausser solve puzzel aes ctr)

parent e10d43d2
#ifndef MAIN_H
#define MAIN_H
#define MBEDTLS_CIPHER_MODE_CTR
//#define MBEDTLS_CIPHER_MODE_CTR
#endif
\ No newline at end of file
No preview for this file type
......@@ -20,5 +20,4 @@ lib_deps =
debug_load_mode = manual
monitor_speed = 115200
monitor_flags = --raw
[env:smi]
\ No newline at end of file
......@@ -4,6 +4,7 @@
#include <sha256.h>
#include <hal/trng_api.h>
#include <rsa.h>
#include <aes.h>
#include "main.h"
#define SMI_SHORT_HASH_LENGTH (3*4-1)
......@@ -11,6 +12,24 @@
#define SMI_TAGIN_LENGTH 1
#define TEST_WIFI_CONNECTION(value) (if((value) != NSAPI_ERROR_OK) exit())
uint8_t block [64][SMI_SHORT_HASH_LENGTH];
uint8_t bufferID[SMI_TAGID_LENGTH + 1];
HttpResponse *response;
uint8_t hash_snippet[32] = {0};
uint8_t hash_equal_index[64] = {0};
uint8_t buffer[256] = {0};
uint8_t public_key_server[256] = {0};
uint8_t *index_hash = (uint8_t *) malloc(64);
uint8_t *sn = (uint8_t*) malloc(4500);
uint8_t bufferCM[256] = {0};
uint8_t our_half_aes_key_enc[256] = {0};
uint8_t server_half_aes_key_enc[256] = {0};
mbedtls_rsa_context rsactx_client;
mbedtls_rsa_context rsactx_server;
uint8_t our_half_aes_key_buffer[256] = {0};
size_t nc_off = 0;
uint8_t nonce_counter[16] = {0};
uint8_t stream_block[16] = {0};
void wait_for_tag(MFRC522* nfc)
{
......@@ -103,7 +122,7 @@ HttpRequest create_request_getsnippet(void)
HttpRequest create_request_solve(void)
{
WiFiInterface *wifi = WiFiInterface::get_default_instance();
printf("IP Adresse: ");
//printf("IP Adresse: ");
printfhex((uint8_t *)wifi->get_ip_address(),sizeof((uint8_t*)wifi->get_ip_address()));
HttpRequest request(wifi, HTTP_GET, "http://smi-server.stefan-hackenberg.de/solve");
......@@ -111,6 +130,28 @@ HttpRequest create_request_solve(void)
return request;
}
HttpRequest create_request_aesctr_solve(void)
{
WiFiInterface *wifi = WiFiInterface::get_default_instance();
//printf("IP Adresse: ");
printfhex((uint8_t *)wifi->get_ip_address(),sizeof((uint8_t*)wifi->get_ip_address()));
HttpRequest request(wifi, HTTP_GET, "http://smi-server.stefan-hackenberg.de/crypto/aes_ctr/solve");
return request;
}
HttpRequest create_request_aesctr_getsnippet(void)
{
WiFiInterface *wifi = WiFiInterface::get_default_instance();
//printf("IP Adresse: ");
printfhex((uint8_t *)wifi->get_ip_address(),sizeof((uint8_t*)wifi->get_ip_address()));
HttpRequest request(wifi, HTTP_GET, "http://smi-server.stefan-hackenberg.de/crypto/aes_ctr/getsnippet");
return request;
}
void send_request_solve(HttpRequest *request, uint8_t *uid, uint8_t *hash_equal_index)
{
uint8_t *data = (uint8*) malloc(64+7);
......@@ -121,6 +162,20 @@ void send_request_solve(HttpRequest *request, uint8_t *uid, uint8_t *hash_equal_
}
void send_request_aesctr_solve(HttpRequest *request, uint8_t *uid, uint8_t *hash_equal_index, mbedtls_aes_context *aesctx)
{
uint8_t *data = (uint8*) malloc(64+7);
uint8_t *data_enc = (uint8*) malloc(64+7);
memcpy(data,uid,7);
memcpy(data+7,hash_equal_index,64);
mbedtls_aes_crypt_ctr(aesctx, sizeof(data), &nc_off, nonce_counter, stream_block,data,data_enc);
HttpResponse *response = request->send(data_enc,64+7);
free(data);
free(data_enc);
return;
}
HttpResponse * send_request_snippet(HttpRequest request, uint8_t *bufferId, uint8_t *bufferIn)
{
uint8_t *data = (uint8_t*) malloc((SMI_TAGID_LENGTH+SMI_TAGIN_LENGTH)*sizeof(uint8_t));
......@@ -134,6 +189,24 @@ HttpResponse * send_request_snippet(HttpRequest request, uint8_t *bufferId, uint
}
HttpResponse * send_request_aesctr_snippet(HttpRequest request, uint8_t *bufferId, uint8_t *bufferIn, mbedtls_aes_context *aesctx)
{
uint8_t *data = (uint8_t*) malloc((SMI_TAGID_LENGTH+SMI_TAGIN_LENGTH)*sizeof(uint8_t));
uint8_t *data_enc = (uint8_t*) malloc((SMI_TAGID_LENGTH+SMI_TAGIN_LENGTH)*sizeof(uint8_t));
memcpy(data, bufferId,(SMI_TAGID_LENGTH+1)*sizeof(uint8_t));
memcpy(data+SMI_TAGID_LENGTH,bufferIn,(SMI_TAGIN_LENGTH));
mbedtls_aes_crypt_ctr(aesctx, 8, &nc_off, nonce_counter, stream_block,data,data_enc);
printf("snippets data: ");
printfhex(data,8);
printf("snippets data_enc: ");
printfhex(data_enc,8);
HttpResponse *response = request.send(data_enc,8);
free(data);
free(data_enc);
return response;
}
uint8_t is_hash_equal(uint8_t *hash_table, uint8_t *hash_snippet)
{
uint8_t n = memcmp(hash_table, hash_snippet, sizeof(hash_table));
......@@ -225,7 +298,7 @@ HttpRequest create_request_test_exKey(void)
{
WiFiInterface *wifi = WiFiInterface::get_default_instance();
HttpRequest request(wifi, HTTP_GET, "http://smi-server.stefan-hackenberg.de/aes_ctr/echo");
HttpRequest request(wifi, HTTP_GET, "http://smi-server.stefan-hackenberg.de/crypto/aes_ctr/echo");
return request;
......@@ -292,25 +365,13 @@ mbedtls_rsa_context create_and_send_client_modulus(uint8_t buffer[256])
return ctx;
}
/*
void dec_snippet_data(uint8_t snippet_enc[8], uint8_t snippet_dec[8],mbedtls_aes_context *aesctx)
{
mbedtls_aes_crypt_ctr(aesctx, 8, &nc_off, nonce_counter, stream_block,snippet_enc,snippet_dec);
}
*/
uint8_t block [64][SMI_SHORT_HASH_LENGTH];
uint8_t bufferID[SMI_TAGID_LENGTH + 1];
HttpResponse *response;
uint8_t hash_snippet[32] = {0};
uint8_t hash_equal_index[64] = {0};
uint8_t buffer[256] = {0};
uint8_t public_key_server[256] = {0};
uint8_t *index_hash = (uint8_t *) malloc(64);
uint8_t *sn = (uint8_t*) malloc(4500);
uint8_t bufferCM[256] = {0};
uint8_t our_half_aes_key_enc[256] = {0};
uint8_t server_half_aes_key_enc[256] = {0};
mbedtls_rsa_context rsactx_client;
mbedtls_rsa_context rsactx_server;
uint8_t our_half_aes_key_buffer[256] = {0};
int main(void)
{
extern uint8_t block[64][SMI_SHORT_HASH_LENGTH];
......@@ -327,7 +388,10 @@ int main(void)
extern mbedtls_rsa_context rsactx_client;
extern mbedtls_rsa_context rsactx_server;
extern uint8_t our_half_aes_key_buffer[256];
extern size_t nc_off;
extern uint8_t nonce_counter[16];
extern uint8_t stream_block[16];
MFRC522 nfc(P9_0, P9_1, P9_2, P9_3, P9_4);
uint8_t j = 0;
uint8_t bufferIN;
......@@ -348,9 +412,7 @@ int main(void)
uint8_t aes_test_enc[32] = {0};
uint8_t aes_test_final[32] = {0};
size_t *nc_off = NULL;
uint8_t nonce_counter[16] = {0};
uint8_t stream_block[16] = {0};
uint8_t snippet_dec[8] = {0};
trng_t rngctx_server;
......@@ -451,25 +513,70 @@ for (int i = 0; i < 16; ++i) {
aes_key[i] = server_half_aes_key[i] ^ our_half_aes_key[i];
}
printf("AES Key final: ");
printfhex(aes_key,16);
HttpRequest test_exKey = create_request_test_exKey();
mbedtls_aes_init(&aesctx);
nc_off = 0;
memset(nonce_counter,0,sizeof(nonce_counter));
memset(stream_block,0,sizeof(nonce_counter));
mbedtls_aes_setkey_enc(&aesctx,aes_key,128);
mbedtls_aes_crypt_ctr(&aesctx,32, nc_off,nonce_counter,stream_block,aes_test,aes_test_enc);
mbedtls_aes_crypt_ctr(&aesctx, 32, &nc_off,nonce_counter,stream_block,aes_test,aes_test_enc);
send_request_test_exKey(&test_exKey, aes_test_enc, aes_test_server_enc);
mbedtls_aes_crypt_ctr(&aesctx, 32,nc_off,nonce_counter,stream_block,aes_test_server_enc,aes_test_final);
printf("test server enc data: ");
printfhex(aes_test_server_enc, sizeof(aes_test_server_enc));
mbedtls_aes_crypt_ctr(&aesctx, 32,&nc_off,nonce_counter,stream_block,aes_test_server_enc,aes_test_final);
printf("final result aes test:");
printfhex(aes_test_final, sizeof(aes_test_final));
mbedtls_aes_free(&aesctx);
//mbedtls_aes_free(&aesctx);
//mbedtls_aes_init(&aesctx);
//mbedtls_aes_setkey_enc(&aesctx,aes_key,128);
//nc_off = 0;
//memset(nonce_counter,0,sizeof(nonce_counter));
//memset(stream_block,0,sizeof(stream_block));
//HttpRequest aes_ctr_solve = create_request_aesctr_getsnippet();
for(uint8_t snp = 0; snp < 64; ++snp)
{
response = send_request_aesctr_snippet(create_request_aesctr_getsnippet(),bufferID,&snp,&aesctx);
mbedtls_aes_crypt_ctr(&aesctx, 8, &nc_off, nonce_counter, stream_block,(unsigned char *)response->get_body(),snippet_dec);
mbedtls_sha256_ret(snippet_dec, sizeof(snippet_dec),hash_snippet, 0);
printf("Hash-Wert snippet: ");
printfhex(hash_snippet,32);
for(int ind = 0; ind<64; ++ind)
{
if(is_hash_equal(block[ind], hash_snippet)) {
hash_equal_index[index_hash[ind]] = snp;
printf("snippet_%d mit Index:%d\n",snp,*(index_hash+ind));
break;
}
}
}
HttpRequest aesctr_solve = create_request_aesctr_solve();
send_request_aesctr_solve(&aesctr_solve,nfc.uid.uidByte,hash_equal_index, &aesctx);
free(sn);
free(index_hash);
mbedtls_aes_free(&aesctx);
return 0;
}
\ No newline at end of file
......@@ -225,6 +225,65 @@ void test_aes16(void)
}
void get_aes_key(uint8_t key[16])
{
}
void send_request_test_exKey(HttpRequest *request, uint8_t data[32], uint8_t respon_data[32])
{
HttpResponse *response = request->send(data,32);
memcpy(respon_data,response->get_body(),32);
}
void test_aesctr(void)
{
mbedtls_aes_context aesctx;
size_t nc_off;
uint8_t nonce_counter[16];
uint8_t stream_block[16];
uint8_t aes_key[16];
uint8_t aes_test[32] = {0x62, 0xe9, 0x4b, 0xd4, 0xef, 0x2a, 0x2c, 0x3b,
0x81, 0x4c, 0xfa, 0x59, 0xca, 0xc4, 0x2b, 0x2e,
0x55, 0xe2, 0xac, 0xde, 0xfa, 0x7e, 0x30, 0x61,
0xe3, 0x7f, 0x1d, 0x57, 0xab, 0xe7, 0x45, 0x5a};
uint8_t aes_test_enc[32];
uint8_t aes_test_server_enc[32];
uint8_t aes_test_final[32];
WiFiInterface *wifi = WiFiInterface::get_default_instance();
HttpRequest request(wifi, HTTP_GET, "http://smi-server.stefan-hackenberg.de/crypto/aes_ctr/echo");
get_aes_key(aes_key);
mbedtls_aes_init(&aesctx);
nc_off = 0;
memset(nonce_counter,0,sizeof(nonce_counter));
memset(stream_block,0,sizeof(nonce_counter));
mbedtls_aes_setkey_enc(&aesctx,aes_key,128);
mbedtls_aes_crypt_ctr(&aesctx, 32, &nc_off,nonce_counter,stream_block,aes_test,aes_test_enc);
send_request_test_exKey(&request, aes_test_enc, aes_test_server_enc);
printf("test server enc data: ");
printfhex(aes_test_server_enc, sizeof(aes_test_server_enc));
mbedtls_aes_crypt_ctr(&aesctx, 32,&nc_off,nonce_counter,stream_block,aes_test_server_enc,aes_test_final);
printf("final result aes test:");
printfhex(aes_test_final, sizeof(aes_test_final));
TEST_ASSERT_EQUAL_UINT8_ARRAY(aes_test,aes_test_final,32);
mbedtls_aes_free(&aesctx);
}
void test_AES32(void)
{
uint8_t input[32] = {0};
......@@ -284,8 +343,9 @@ int main (void)
RUN_TEST(test_sha256);
RUN_TEST(test_rsa);
RUN_TEST(test_aes16);
// RUN_TEST(test_AES32);
RUN_TEST(test_AES32);
RUN_TEST(test_mac);
RUN_TEST(test_aesctr);
UNITY_END();
}
\ 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