Commit 3541b2f4 authored by Jonas Hundseder's avatar Jonas Hundseder
Browse files

1. commit, 25.10.21

parent d51aec52
#include <mbed.h>
#include <MFRC522.h>
#include <http_request.h>
#include <sha256.h>
#include <hal/trng_api.h>
#include <rsa.h>
#define SMI_SHORT_HASH_LENGTH (3*4-1)
#define SMI_TAGID_LENGTH 7
#define SMI_TAGIN_LENGTH 1
#define TEST_WIFI_CONNECTION(value) (if((value) != NSAPI_ERROR_OK) exit())
int main(void)
void wait_for_tag(MFRC522* nfc)
{
bool result = false;
printf("Wait for Tag\n");
while(result != true)
{
result = nfc->PICC_IsNewCardPresent();
}
printf("Get new Tag\n");
}
void printfhex(uint8_t Byte[], size_t size)
{
for (size_t i = 0; i < size; ++i)
{
printf("%02x ", Byte[i]);
}
printf("\n");
}
void get_UID(MFRC522* nfc)
{
printf("Read Card Serial\n");
bool result = nfc->PICC_ReadCardSerial();
if(result == true){
printf("Tagid: ");
printfhex(nfc->uid.uidByte, nfc->uid.size);
} else{
printf("Error\n");
}
}
void nfc_read_index_hash_set(MFRC522 *nfc, uint8_t block, uint8_t in[],uint8_t nr, uint8_t hash[SMI_SHORT_HASH_LENGTH])
{
uint8_t buffer[18];
uint8_t buffer_size = sizeof(buffer);
uint8_t status = nfc->MIFARE_Read(block,buffer,&buffer_size);
uint8_t ind = 3;
*(in+nr) = buffer[0];
for (int i = 1; i < 4; ++i){
hash[i-1] = buffer[i];
}
for (int i = 1; i < 3; ++i) {
status = nfc->MIFARE_Read(block+i,buffer,&buffer_size);
for (int j = 0; j < 4; ++j) {
hash[ind] = buffer[j];
++ind;
}
}
}
nsapi_error_t connect_wifi(void)
{
WiFiInterface *wifi = WiFiInterface::get_default_instance();
const char WIFI_SSID[] = "FRITZ!Box 6490 Cable";
const char WIFI_PASSWORD[] = "09729577961903800041";
nsapi_error_t status = wifi->connect(WIFI_SSID,WIFI_PASSWORD,NSAPI_SECURITY_WPA_WPA2);
return status;
}
HttpRequest create_request_getsnippet(void)
{
WiFiInterface *wifi = WiFiInterface::get_default_instance();
HttpRequest request(wifi, HTTP_GET, "http://smi-server.stefan-hackenberg.de/getsnippet");
return request;
}
HttpRequest create_request_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/solve");
return request;
}
void send_request_solve(HttpRequest *request, uint8_t *uid, uint8_t *hash_equal_index)
{
uint8_t *data = (uint8*) malloc(64+7);
memcpy(data,uid,7);
memcpy(data+7,hash_equal_index,64);
HttpResponse *response = request->send(data,64+7);
free(data);
}
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));
memcpy(data, bufferId,(SMI_TAGID_LENGTH+1)*sizeof(uint8_t));
memcpy(data+SMI_TAGID_LENGTH,bufferIn,(SMI_TAGIN_LENGTH));
printf("snippets data: ");
printfhex(data,8);
HttpResponse *response = request.send(data,8);
free(data);
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));
if(n == 0) {
return 1;
}else{
return 0;
}
}
void random_numbers(uint8_t number[16])
{
trng_t rngctx;
trng_init(&rngctx);
uint8_t buffer[16];
size_t outlen;
trng_get_bytes(&rngctx, buffer, sizeof(buffer), &outlen);
trng_free(&rngctx);
printf("Random number: ");
printfhex(buffer,16);
}
int crypto_trng_generate(void *rngctx, uint8_t *buffer, size_t length)
{
size_t outlen;
return trng_get_bytes((trng_t *)rngctx, buffer, length, &outlen);
}
void test_rsa(void)
{
trng_t rngctx;
trng_init(&rngctx);
uint8_t buffer[256];
int nbits = 128;
int exp = 0x1011001;
mbedtls_rsa_context ctx;
mbedtls_rsa_init(&ctx, MBEDTLS_RSA_PKCS_V21, MBEDTLS_MD_SHA256);
mbedtls_rsa_gen_key(&ctx,crypto_trng_generate,&rngctx,nbits,exp);
trng_free(&rngctx);
mbedtls_mpi_write_binary(&ctx.P, buffer, sizeof(buffer));
printf("P: ");
printfhex(buffer, sizeof(buffer));
mbedtls_mpi_write_binary(&ctx.Q, buffer, sizeof(buffer));
printf("Q: ");
printfhex(buffer, sizeof(buffer));
mbedtls_mpi_write_binary(&ctx.N, buffer, sizeof(buffer));
printf("N: ");
printfhex(buffer, sizeof(buffer));
mbedtls_mpi_write_binary(&ctx.E, buffer, sizeof(buffer));
printf("E: ");
printfhex(buffer, sizeof(buffer));
mbedtls_mpi_write_binary(&ctx.D, buffer, sizeof(buffer));
printf("D: ");
printfhex(buffer, sizeof(buffer));
}
HttpRequest create_request_public_key(void)
{
printf("Hello World!\n");
WiFiInterface *wifi = WiFiInterface::get_default_instance();
HttpRequest request(wifi, HTTP_GET, "http://smi-server.stefan-hackenberg.de/crypto/rsa/server_modulus");
return request;
}
void send_request_public_key(HttpRequest *request, uint8_t public_key[256])
{
HttpResponse *response = request->send(NULL,0);
memcpy(public_key,response->get_body(),256);
}
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[256] = {0};
uint8_t *index_hash = (uint8_t *) malloc(64);
uint8_t *sn = (uint8_t*) malloc(4500);
int main(void)
{
extern uint8_t block[64][SMI_SHORT_HASH_LENGTH];
extern uint8_t public_key[256];
extern uint8_t bufferID[SMI_TAGID_LENGTH + 1];
extern uint8_t hash_snippet[32];
extern uint8_t hash_equal_index[64];
extern uint8_t buffer[256];
extern uint8_t *index_hash;
extern uint8_t *sn;
MFRC522 nfc(P9_0, P9_1, P9_2, P9_3, P9_4);
uint8_t j = 0;
uint8_t bufferIN;
nsapi_error_t error;
uint8_t indexnr = 0;
size_t size_snippet = 0;
mbedtls_rsa_context ctx;
mbedtls_rsa_context rsactx;
const uint8_t public_exponent[3] = {0x01, 0x00, 0x01};
uint8_t our_half_aes_key[16];
nfc.PCD_Init();
wait_for_tag(&nfc);
get_UID(&nfc);
error = connect_wifi();
for(int i = 4; i < 194; i=i+3)
{
nfc_read_index_hash_set(&nfc, i, index_hash ,indexnr ,block[j]);
printf("Index%d: ",indexnr);
printf("%02x\n", index_hash[indexnr]);
printf("Hash%d:", indexnr);
printfhex(block[j], SMI_SHORT_HASH_LENGTH);
++indexnr;
++j;
}
memcpy(bufferID, nfc.uid.uidByte, SMI_TAGID_LENGTH);
for(uint8_t snp = 0; snp < 64; ++snp)
{
response = send_request_snippet(create_request_getsnippet(),bufferID,&snp);
mbedtls_sha256_ret((uint8_t*)response->get_body(), response->get_body_length(),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 request_solve = create_request_solve();
send_request_solve(&request_solve, nfc.uid.uidByte, hash_equal_index);
random_numbers();
//test_rsa();
HttpRequest request_public_key = create_request_public_key();
mbedtls_rsa_init(&rsactx, MBEDTLS_RSA_PKCS_V15, 0);
send_request_public_key(&request_public_key, public_key);
printf("public Modulus from Server: ");
printfhex(public_key,256);
mbedtls_rsa_import_raw(&rsactx,public_key, 256,NULL,0,NULL,0,NULL,0,public_exponent,sizeof(public_exponent));
random_numbers(our_half_aes_key);
mbedtls_rsa_rsaes_oaep_encrypt(&rsactx,NULL,NULL,MBEDTLS_RSA_PUBLIC,NULL,0,)
free(sn);
free(index_hash);
return 0;
return 0;
}
\ No newline at end of file
#include <mbed.h>
#include <unity.h>
#include <MFRC522.h>
#include <http_request.h>
#include <sha256.h>
#include <hal/trng_api.h>
#include <aes.h>
#include <cmac.h>
#define FIRM_VER 0x92
void printfhex(uint8_t Byte[], uint8_t size)
{
for (int i = 0; i < size; ++i)
{
printf("%02x ", Byte[i]);
}
printf("\n");
}
void test_a_well_known_truth(void)
{
TEST_ASSERT_EQUAL(1,1);
}
nsapi_error_t connect_wifi(void)
{
WiFiInterface *wifi = WiFiInterface::get_default_instance();
const char WIFI_SSID[] = "FRITZ!Box 6490 Cable";
const char WIFI_PASSWORD[] = "09729577961903800041";
nsapi_error_t status = wifi->connect(WIFI_SSID,WIFI_PASSWORD,NSAPI_SECURITY_WPA_WPA2);
return status;
}
void test_wifi(void)
{
nsapi_error_t status = connect_wifi();
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, status);
}
void init_RC522(void)
{
MFRC522 nfc(P9_0, P9_1, P9_2, P9_3, P9_4);
nfc.PCD_Init();
}
void test_RC522(void)
{
MFRC522 nfc(P9_0, P9_1, P9_2, P9_3, P9_4);
nfc.PCD_Init();
uint8_t version = nfc.PCD_ReadRegister(MFRC522::VersionReg);
TEST_ASSERT_EQUAL(FIRM_VER, version);
}
HttpRequest create_request_hello(void)
{
WiFiInterface *wifi = WiFiInterface::get_default_instance();
HttpRequest request(wifi, HTTP_GET, "http://smi-server.stefan-hackenberg.de/hello");
return request;
}
HttpRequest create_request_echo(void)
{
WiFiInterface *wifi = WiFiInterface::get_default_instance();
HttpRequest request(wifi, HTTP_GET, "http://smi-server.stefan-hackenberg.de/echo");
return request;
}
HttpResponse* send_empty_request(HttpRequest *request)
{
HttpResponse *response = request->send(NULL,0);
return response;
}
HttpResponse* send_request(HttpRequest *request)
{
const char data[] = "EchoEcho";
HttpResponse *response = request->send(data,64);
return response;
}
void test_hello(void)
{
HttpRequest request = create_request_hello();
HttpResponse* response = send_empty_request(&request);
int status = response->get_status_code();
TEST_ASSERT_EQUAL(200,status);
TEST_ASSERT_EQUAL_STRING("hello", response->get_body_as_string().c_str());
}
void test_echo(void)
{
HttpRequest request = create_request_echo();
HttpResponse* response = send_request(&request);
int status = response->get_status_code();
TEST_ASSERT_EQUAL(200,status);
TEST_ASSERT_EQUAL_STRING("EchoEcho", response->get_body_as_string().c_str());
}
void test_sha256()
{
unsigned char input[16] = {0};
size_t ilen = sizeof(input);
unsigned char output[32] = {0};
mbedtls_sha256_ret(input, ilen, output, 0);
uint8_t result[32] = {0x37, 0x47, 0x08, 0xff, 0xf7, 0x71, 0x9d, 0xd5,
0x97, 0x9e, 0xc8, 0x75, 0xd5, 0x6c, 0xd2, 0x28,
0x6f, 0x6d, 0x3c, 0xf7, 0xec, 0x31, 0x7a, 0x3b,
0x25, 0x63, 0x2a, 0xab, 0x28, 0xec, 0x37, 0xbb};
for(int i = 0; i<32;++i)
{
TEST_ASSERT_EQUAL(result[i],(uint8_t) output[i]);
}
}
uint8_t random_numbers(void)
{
trng_t rngctx;
trng_init(&rngctx);
uint8_t buffer[16];
size_t outlen;
trng_get_bytes(&rngctx, buffer, sizeof(buffer), &outlen);
trng_free(&rngctx);
printf("Random number: ");
printfhex(buffer,16);
}
int crypto_trng_generate(void *rngctx, uint8_t *buffer, size_t length)
{
size_t outlen;
return trng_get_bytes((trng_t *)rngctx, buffer, length, &outlen);
}
mbedtls_rsa_context generate_rsa_key(trng_t *rngc)
{
int nbits = 2048;
int exp = 0x10001;
mbedtls_rsa_context ctx;
mbedtls_rsa_init(&ctx, MBEDTLS_RSA_PKCS_V21, MBEDTLS_MD_SHA256);
mbedtls_rsa_gen_key(&ctx,crypto_trng_generate,rngc,nbits,exp);
return ctx;
}
void test_rsa(void)
{
trng_t rngctx;
trng_init(&rngctx);
uint8_t buffer[256];
size_t olength = 5;
uint8_t output[5];
size_t output_max_length = 5;
mbedtls_rsa_context ctx = generate_rsa_key(&rngctx);
WiFiInterface *wifi = WiFiInterface::get_default_instance();
HttpRequest request(wifi, HTTP_GET, "http://smi-server.stefan-hackenberg.de/crypto/rsa/client_modulus");
mbedtls_mpi_write_binary(&ctx.N, buffer, sizeof(buffer));
printfhex(buffer, sizeof(buffer));
HttpResponse *respon = request.send(buffer,256);
mbedtls_rsa_rsaes_oaep_decrypt(&ctx,crypto_trng_generate,&rngctx,MBEDTLS_RSA_PRIVATE,NULL,0,&olength,(unsigned char*) respon->get_body(),output,output_max_length);
trng_free(&rngctx);
TEST_ASSERT_EQUAL_STRING("hello",output);
}
void test_aes16(void)
{
uint8_t input[16] = {0};
uint8_t output[16] = {0};
mbedtls_aes_context ctx;
uint8_t key[16] = {0};
uint8_t val[16] = {0x66, 0xe9, 0x4b, 0xd4, 0xef, 0x8a, 0x2c, 0x3b,
0x88, 0x4c, 0xfa, 0x59, 0xca, 0x34, 0x2b, 0x2e};
mbedtls_aes_init(&ctx);
mbedtls_aes_setkey_enc(&ctx,key,128);
mbedtls_aes_crypt_ecb(&ctx, MBEDTLS_AES_ENCRYPT,input,output);
mbedtls_aes_free(&ctx);
for(int i = 0; i < 16; ++i){
TEST_ASSERT_EQUAL(val[i], output[i]);
}
}
void test_AES32(void)
{
uint8_t input[32] = {0};
uint8_t output[32] = {0};
mbedtls_aes_context ctx;
uint8_t key[16] = {0};
unsigned char nonce_counter[16] = {0};
unsigned char stream_block[16] = {0};
size_t nc_off = 0;
uint8_t val[32] = {0x66, 0xe9, 0x4b, 0xd4, 0xef, 0x8a, 0x2c, 0x3b,
0x88, 0x4c, 0xfa, 0x59, 0xca, 0x34, 0x2b, 0x2e,
0x58, 0xe2, 0xfc, 0xce, 0xfa, 0x7e, 0x30, 0x61,
0x36, 0x7f, 0x1d, 0x57, 0xa4, 0xe7, 0x45, 0x5a};
mbedtls_aes_init(&ctx);
mbedtls_aes_setkey_enc(&ctx,key,128);
mbedtls_aes_crypt_ctr(&ctx,32, &nc_off,nonce_counter,stream_block,input,output);
mbedtls_aes_free(&ctx);
for(int i = 0; i < 32; ++i){
TEST_ASSERT_EQUAL(val[i], output[i]);
}
}
void test_mac(void)
{ uint8_t key[16] = {0};
uint8_t input[16] = {0};
uint8_t output[16] = {0};
uint8_t val[16] = {0x76, 0x3c, 0xbc, 0xde, 0x81, 0xdf, 0x91, 0x31,
0xbf, 0x89, 0x77, 0x12, 0xc0, 0x88, 0xed, 0xad};
mbedtls_cipher_context_t ctx;
int status = mbedtls_cipher_setup(&ctx, mbedtls_cipher_info_from_type(MBEDTLS_CIPHER_AES_128_ECB));
status = mbedtls_cipher_cmac_starts(&ctx, key, 128);
status = mbedtls_cipher_cmac_update(&ctx, input, 16);
status = mbedtls_cipher_cmac_finish(&ctx, output);
for(int i = 0; i < 16; ++i){
TEST_ASSERT_EQUAL(val[i], output[i]);
}
}
int main (void)
{
UNITY_BEGIN();
RUN_TEST(test_a_well_known_truth);
RUN_TEST(test_wifi);
RUN_TEST(test_RC522);
RUN_TEST(test_hello);
RUN_TEST(test_echo);
RUN_TEST(test_sha256);
// RUN_TEST(test_rsa);
RUN_TEST(test_aes16);
RUN_TEST(test_AES32);
RUN_TEST(test_mac);
UNITY_END();
}
\ No newline at end of file