Commit cf633267 authored by hark's avatar hark

test messages

parent 13bdc24f
CC=gcc
CC=colorgcc
#CFLAGS=-I ../include mbedtls/ -iquote ../libs/mbedtls-2.2.0/include/ -DMBEDTLS_CONFIG_FILE='<mbedtls_config.h>'
#CFLAGS="-I ../include -iquote ../libs/mbedtls-2.2.0/include/mbedtls/ -DMBEDTLS_CONFIG_FILE='<mbedtls_config.h>'"
CFLAGS="-I ../include"
......@@ -9,5 +9,7 @@ CFLAGS="-I ../include"
base-tx: base-tx.c
$(CC) -o base-tx base-tx.c $(LIBS) $(CFLAGS)
test: test.c
$(CC) -std=c99 -o test test.c ../libs/micro-ecc/uECC.c -lmbedtls -lmbedx509 -lmbedcrypto $(LIBS) $(CFLAGS)
test: test.c common.c
$(CC) -std=c99 -g -o test test.c common.c ../libs/micro-ecc/uECC.c -lmbedtls -lmbedx509 -lmbedcrypto $(LIBS) $(CFLAGS)
test2: test2.c
$(CC) -std=c99 -o test2 test2.c ../libs/micro-ecc/uECC.c -lmbedtls -lmbedx509 -lmbedcrypto $(LIBS) $(CFLAGS)
#include <stdio.h>
#include <string.h>
#include "../libs/micro-ecc/uECC.h"
#include "packets.h"
#include <stdlib.h>
#ifdef AVR
#include <avr/pgmspace.h>
const char PROGMEM b64_alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/"
#else
const char b64_alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/";
#endif
/* 'Private' declarations */
/* why?
inline void a3_to_a4(unsigned char * a4, unsigned char * a3);
inline void a4_to_a3(unsigned char * a3, unsigned char * a4);
inline unsigned char b64_lookup(char c);
*/
void
dump_buffer(unsigned int n, const unsigned char* buf)
{
const unsigned char *p, *end;
unsigned i, j;
printf("\n Buffer: \n ");
end = buf + n;
for (i = 0; ; i += 16) {
p = buf + i;
for (j = 0; j < 16; j++) {
fprintf(stderr, "%02X ", p[j]);
if (p + j >= end)
goto BREAKOUT;
}
fprintf(stderr, " ");
p = buf + i;
for (j = 0; j < 16; j++) {
fprintf(stderr, "%c", isprint(p[j]) ? p[j] :
'.');
if (p + j >= end)
goto BREAKOUT;
}
fprintf(stderr, "\n");
}
BREAKOUT:
return;
}
void vli_print(char *str, uint8_t *vli, unsigned int size) {
// printf("%s ", str);
for(unsigned i=0; i<size; ++i) {
printf("%02X ", (unsigned)vli[i]);
}
printf("\n");
}
static inline void a3_to_a4(unsigned char * a4, unsigned char * a3) {
a4[0] = (a3[0] & 0xfc) >> 2;
a4[1] = ((a3[0] & 0x03) << 4) + ((a3[1] & 0xf0) >> 4);
a4[2] = ((a3[1] & 0x0f) << 2) + ((a3[2] & 0xc0) >> 6);
a4[3] = (a3[2] & 0x3f);
}
static inline void a4_to_a3(unsigned char * a3, unsigned char * a4) {
a3[0] = (a4[0] << 2) + ((a4[1] & 0x30) >> 4);
a3[1] = ((a4[1] & 0xf) << 4) + ((a4[2] & 0x3c) >> 2);
a3[2] = ((a4[2] & 0x3) << 6) + a4[3];
}
static inline unsigned char b64_lookup(char c) {
if(c >='A' && c <='Z') return c - 'A';
if(c >='a' && c <='z') return c - 71;
if(c >='0' && c <='9') return c + 4;
if(c == '+') return 62;
if(c == '/') return 63;
return -1;
}
int base64_encode(char *output, char *input, int inputLen) {
int i = 0, j = 0;
int encLen = 0;
unsigned char a3[3];
unsigned char a4[4];
while(inputLen--) {
a3[i++] = *(input++);
if(i == 3) {
a3_to_a4(a4, a3);
for(i = 0; i < 4; i++) {
#ifdef AVR
output[encLen++] = pgm_read_byte(&b64_alphabet[a4[i]]);
#else
output[encLen++] = b64_alphabet[a4[i]];
#endif
}
i = 0;
}
}
if(i) {
for(j = i; j < 3; j++) {
a3[j] = '\0';
}
a3_to_a4(a4, a3);
for(j = 0; j < i + 1; j++) {
#ifdef AVR
output[encLen++] = pgm_read_byte(&b64_alphabet[a4[j]]);
#else
output[encLen++] = b64_alphabet[a4[j]];
#endif
}
while((i++ < 3)) {
output[encLen++] = '=';
}
}
output[encLen] = '\0';
return encLen;
}
int base64_decode(char * output, char * input, int inputLen) {
int i = 0, j = 0;
int decLen = 0;
unsigned char a3[3];
unsigned char a4[4];
while (inputLen--) {
if(*input == '=') {
break;
}
a4[i++] = *(input++);
if (i == 4) {
for (i = 0; i <4; i++) {
a4[i] = b64_lookup(a4[i]);
}
a4_to_a3(a3,a4);
for (i = 0; i < 3; i++) {
output[decLen++] = a3[i];
}
i = 0;
}
}
if (i) {
for (j = i; j < 4; j++) {
a4[j] = '\0';
}
for (j = 0; j <4; j++) {
a4[j] = b64_lookup(a4[j]);
}
a4_to_a3(a3,a4);
for (j = 0; j < i - 1; j++) {
output[decLen++] = a3[j];
}
}
output[decLen] = '\0';
return decLen;
}
int base64_enc_len(int plainLen) {
int n = plainLen;
return (n + 2 - ((n + 2) % 3)) / 3 * 4;
}
int base64_dec_len(char * input, int inputLen) {
int i = 0;
int numEq = 0;
for(i = inputLen - 1; input[i] == '='; i--) {
numEq++;
}
return ((6 * inputLen) / 8) - numEq;
}
void dump_buffer(unsigned int , const unsigned char* );
void vli_print(char *, uint8_t *, unsigned int);
/* b64_alphabet:
* Description: Base64 alphabet table, a mapping between integers
* and base64 digits
* Notes: This is an extern here but is defined in Base64.c
*/
extern const char b64_alphabet[];
/* base64_encode:
* Description:
* Encode a string of characters as base64
* Parameters:
* output: the output buffer for the encoding, stores the encoded string
* input: the input buffer for the encoding, stores the binary to be encoded
* inputLen: the length of the input buffer, in bytes
* Return value:
* Returns the length of the encoded string
* Requirements:
* 1. output must not be null or empty
* 2. input must not be null
* 3. inputLen must be greater than or equal to 0
*/
int base64_encode(char *output, char *input, int inputLen);
/* base64_decode:
* Description:
* Decode a base64 encoded string into bytes
* Parameters:
* output: the output buffer for the decoding,
* stores the decoded binary
* input: the input buffer for the decoding,
* stores the base64 string to be decoded
* inputLen: the length of the input buffer, in bytes
* Return value:
* Returns the length of the decoded string
* Requirements:
* 1. output must not be null or empty
* 2. input must not be null
* 3. inputLen must be greater than or equal to 0
*/
int base64_decode(char *output, char *input, int inputLen);
/* base64_enc_len:
* Description:
* Returns the length of a base64 encoded string whose decoded
* form is inputLen bytes long
* Parameters:
* inputLen: the length of the decoded string
* Return value:
* The length of a base64 encoded string whose decoded form
* is inputLen bytes long
* Requirements:
* None
*/
int base64_enc_len(int inputLen);
/* base64_dec_len:
* Description:
* Returns the length of the decoded form of a
* base64 encoded string
* Parameters:
* input: the base64 encoded string to be measured
* inputLen: the length of the base64 encoded string
* Return value:
* Returns the length of the decoded form of a
* base64 encoded string
* Requirements:
* 1. input must not be null
* 2. input must be greater than or equal to zero
*/
int base64_dec_len(char *input, int inputLen);
......@@ -2,19 +2,30 @@
* pager packet format
*/
struct keypair
{
uint8_t public[64];
uint8_t private[32];
uint8_t compressed_point[33];
};
struct pagermessage
{
//Will be filled in with the compressed public key. Must be at least
// (curve size + 1) bytes long; for example, if the curve is secp256r1,
// compressed must be 33 bytes long.
uint8_t senderpubkey[33];
//
uint8_t sender_compressed_point[33];
// address is SHA1 hash of publickey
int address;
//int address;
// maybe use 7bit ascii, or even smaller characters (only letters + numbers)
char msg[127];
}
char msg[128];
//unsigned char key[32];
unsigned char iv[16];
};
/* subjectPublicKeyInfo field
* in X.509 certificates [PKI]
......
......@@ -12,196 +12,212 @@
//#include <gnutls/gnutls.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "packets.h"
#include "common.h"
#ifndef uECC_TEST_NUMBER_OF_ITERATIONS
#define uECC_TEST_NUMBER_OF_ITERATIONS 1
#endif
#define MSG_SIZE 128
// mbedtls things
mbedtls_aes_context aes;
mbedtls_aes_context enc_aes;
mbedtls_aes_context dec_aes;
const struct uECC_Curve_t * curve;
void init() {
curve = uECC_secp256k1();
}
void
dump_buffer(unsigned n, const unsigned char* buf)
{
const unsigned char *p, *end;
unsigned i, j;
unsigned char fakeiv[] = "123456789abcdefghijklmnop";
struct pagermessage * send_message(struct keypair *to, struct keypair *from, char * msg_text) {
struct pagermessage *msg;
char crypt_message[128];
char decrypted_message[128];
//unsigned char key[32];
unsigned char iv[16];
uint8_t shared_secret[32] = {0};
msg = malloc(sizeof(struct pagermessage));
memset(msg, 7, sizeof(struct pagermessage));
end = buf + n;
/* create a random iv */
// TODO (this is stupid)
memcpy(iv, fakeiv, sizeof(iv));
for (i = 0; ; i += 16) {
p = buf + i;
for (j = 0; j < 16; j++) {
fprintf(stderr, "%02X ", p[j]);
if (p + j >= end)
goto BREAKOUT;
// copy compressed point into pager message
memcpy(msg->sender_compressed_point, from->compressed_point, sizeof(msg->sender_compressed_point));
// copy iv for sending
memcpy(msg->iv, iv, sizeof(msg->iv));
/*calculate shared secret on sender*/
if (!uECC_shared_secret(to->public, from->private, shared_secret, curve)) {
printf("shared_secret() failed (1)\n");
return 1;
}
fprintf(stderr, " ");
p = buf + i;
for (j = 0; j < 16; j++) {
fprintf(stderr, "%c", isprint(p[j]) ? p[j] :
'.');
if (p + j >= end)
goto BREAKOUT;
// dump_buffer(32, shared_secret);
// dump_buffer(16, msg->iv);
mbedtls_aes_init( &enc_aes );
/* encrypt message with aes using shared secret as the key */
mbedtls_aes_setkey_enc( &enc_aes, shared_secret, 256 );
char tm[128] = "dit is een test berichtje :) ";
mbedtls_aes_crypt_cbc( &enc_aes, MBEDTLS_AES_ENCRYPT, MSG_SIZE, iv, tm, msg->msg );
printf("message to send: %s \n ", msg_text);
/*
printf("sender_shared_secrete: \n ");
dump_buffer(sizeof(sender_shared_secret), sender_shared_secret);
printf("clear_message: \n ");
dump_buffer(sizeof(clear_message), clear_message);
printf("crypt_message: \n ");
char bs_dec[128];
char bs_msg[128];
memset(bs_dec, 0, 128);
memset(bs_msg, 0, 128);
base64_encode(&bs_msg, &clear_message, 128 );
base64_decode(bs_dec, bs_msg, 128 );
printf(" \n base64: %128.128s \n dec: %s \n", bs_msg, bs_dec);
dump_buffer(sizeof(crypt_message), crypt_message);
*/
// dump_buffer(sizeof(struct pagermessage), msg);
return msg;
}
void receive_message(struct keypair *me, struct pagermessage *msg)
{
uint8_t sender_decompressed_point[64];
uint8_t shared_secret[32] = {0};
char crypt_message[128];
char decrypted_message[128];
/* decompress the senders public key */
uECC_decompress(msg->sender_compressed_point, sender_decompressed_point, curve);
/*calculate shared secret on receiver*/
if (!uECC_shared_secret(sender_decompressed_point, me->private, shared_secret, curve)) {
printf("shared_secret() failed (receive)\n");
}
fprintf(stderr, "\n");
dump_buffer(32, shared_secret);
dump_buffer(16, msg->iv);
/* decrypt the message */
mbedtls_aes_init( &dec_aes );
mbedtls_aes_setkey_dec( &dec_aes, shared_secret, 256 );
mbedtls_aes_crypt_cbc( &dec_aes, MBEDTLS_AES_DECRYPT, 128, msg->iv, msg->msg, decrypted_message );
printf("decrypted_message: %s \n ", decrypted_message);
dump_buffer(sizeof(decrypted_message), decrypted_message);
}
struct keypair * create_keypair() {
struct keypair *keypair;
keypair = malloc(sizeof(struct keypair));
memset(keypair->public, 3, sizeof(keypair->public));
memset(keypair->private, 4, sizeof(keypair->private));
memset(keypair->compressed_point, 5, sizeof(keypair->compressed_point));
/* Generate arbitrary EC point (public) on Curve */
if (!uECC_make_key(keypair->public, keypair->private, curve)) {
printf("uECC_make_key() failed\n");
}
BREAKOUT:
return;
uECC_compress(keypair->public, keypair->compressed_point, curve);
printf(" \n comp point \n ");
dump_buffer(32, keypair->compressed_point);
printf(" \n end comp point \n ");
return keypair;
}
void vli_print(char *str, uint8_t *vli, unsigned int size) {
// printf("%s ", str);
for(unsigned i=0; i<size; ++i) {
printf("%02X ", (unsigned)vli[i]);
}
printf("\n");
void pb(char buf) {
}
int main() {
uint8_t sender_public[64];
uint8_t sender_private[32];
uint8_t receiver_public[64];
uint8_t receiver_private[32];
uint8_t sender_compressed_point[33];
struct keypair *sender, *receiver;
uint8_t sender_decompressed_point[64];
uint8_t receiver_compressed_point[33];
uint8_t receiver_decompressed_point[64];
uint8_t sender_shared_secret[32] = {0};
uint8_t receiver_shared_secret[32] = {0};
char clear_message[128] = "dit is een test berichtje :) ";
char clear_message[] = "Blaat blaat, dit is een test berichtje :) ";
char crypt_message[128];
char decrypted_message[128];
//unsigned char key[32];
unsigned char iv[16];
unsigned char siv[16]; // for sending the iv
//size_t input_len = 40;
//size_t output_len = 0;
int i;
int c;
// initialise the curve
init();
struct pagermessage * sended_msg;
/* create keypairs */
sender = create_keypair();
receiver = create_keypair();
sended_msg = send_message(receiver, sender, clear_message);
printf("\n The message that was send: \n");
dump_buffer(sizeof(struct pagermessage), sended_msg);
printf("\n ----------------------------\n");
const struct uECC_Curve_t * curves[5];
int num_curves = 0;
#if uECC_SUPPORTS_secp160r1
curves[num_curves++] = uECC_secp160r1();
#endif
#if uECC_SUPPORTS_secp192r1
curves[num_curves++] = uECC_secp192r1();
#endif
#if uECC_SUPPORTS_secp224r1
curves[num_curves++] = uECC_secp224r1();
#endif
#if uECC_SUPPORTS_secp256r1
curves[num_curves++] = uECC_secp256r1();
#endif
#if uECC_SUPPORTS_secp256k1
curves[num_curves++] = uECC_secp256k1();
#endif
printf("Testing compression and decompression of %d random EC points\n",
uECC_TEST_NUMBER_OF_ITERATIONS);
for (c = 0; c < num_curves; ++c) {
for (i = 0; i < uECC_TEST_NUMBER_OF_ITERATIONS; ++i) {
printf("Testing: \n");
fflush(stdout);
memset(sender_public, 0, sizeof(sender_public));
memset(sender_decompressed_point, 0, sizeof(sender_decompressed_point));
memset(receiver_public, 0, sizeof(receiver_public));
memset(receiver_decompressed_point, 0, sizeof(receiver_decompressed_point));
/* Generate arbitrary EC point (public) on Curve */
if (!uECC_make_key(sender_public, sender_private, curves[c])) {
printf("uECC_make_key() failed\n");
continue;
}
if (!uECC_make_key(receiver_public, receiver_private, curves[c])) {
printf("uECC_make_key() failed\n");
continue;
}
/*calculate shared secret on sender*/
if (!uECC_shared_secret(sender_public, sender_private, sender_shared_secret, curves[c])) {
printf("shared_secret() failed (1)\n");
return 1;
}
/* compress sender public key */
uECC_compress(sender_public, sender_compressed_point, curves[c]);
printf(" \n Compressed public key: \n");
// dump_buffer(256, sender_compressed_point);
/* create a random iv */
// TODO
memcpy(iv, clear_message, sizeof(iv));
/* encrypt message with aes using shared secret as the key */
// copy iv for sending
memcpy(siv, iv, sizeof(siv));
mbedtls_aes_setkey_enc( &aes, sender_shared_secret, 256 );
mbedtls_aes_crypt_cbc( &aes, MBEDTLS_AES_ENCRYPT, 128, iv, clear_message, crypt_message );
printf("sender_shared_secrete: \n ");
dump_buffer(sizeof(sender_shared_secret), sender_shared_secret);
printf("clear_message: \n ");
dump_buffer(sizeof(clear_message), clear_message);
printf("crypt_message: \n ");
dump_buffer(sizeof(crypt_message), crypt_message);