Commit c571edc4 authored by yids's avatar yids

bunch of stuff, in avr-src is a working avrnacl example

parent 9cba1452
......@@ -7,7 +7,7 @@ CFLAGS=-Os -ggdb3 -pedantic -std=gnu99 -DARDUINO -I../libs/avrnacl/randombytes -
OPTIMIZE = -O3
CC=avr-gcc
MCU=atmega2560
MCU=atmega328p
F_CPU=F_CPU=16000000UL
AVRDUDE_PROGRAMMERID=stk200 /* Programmer ids are listed in /etc/avrdude.conf */
......@@ -19,21 +19,26 @@ all: lib-test
lib-test: lib-test.c
$(CC) lib-test.c ../libs/pagerlib/pagerlib.c ../libs/pagerlib/things.c $(OBJS) $(OPTIMZE) -g -I AVRLIB/ -mmcu=$(MCU) -D$(F_CPU) $(CFLAGS) -o lib-test.elf
avr-objcopy -j .text -j .data -O ihex lib-test.elf lib-test.hex
avr-size --mcu=atmega328p --format=avr lib-test.elf
nacl-test: nacl-test.c
$(CC) nacl-test.c $(OBJS) $(OPTIMZE) -g -I AVRLIB/ -mmcu=$(MCU) -D$(F_CPU) $(CFLAGS) -o nacl-test.elf
avr-objcopy -j .text -j .data -O ihex nacl-test.elf nacl-test.hex
avr-size --mcu=atmega328p --format=avr nacl-test.elf
upload:
avrdude -carduino -p atmega328p -P $(AVRDUDE_PORT) -b57600 -U flash:w:lib-test.hex
simulavr:
simulavr -t -g -d $(MCU) --file lib-test.elf -W 0x20,- &
simulavr -t -g -d atmega328 --file nacl-test.elf -W 0x20,- &
sleep 1
avr-gdb -se lib-test.elf --eval-command="target remote localhost:1212" --eval-command="load"
avr-gdb -se nacl-test.elf --eval-command="target remote localhost:1212" --eval-command="load"
killall simulavr
simulavr-ddd:
simulavr -g -d $(MCU) --file lib-test.elf &
simulavr -g -d $(MCU) --file nacl-test.elf &
sleep 1
ddd --debugger 'avr-gdb -se lib-test.elf --eval-command="target remote localhost:1212" --eval-command="load"'
ddd --debugger 'avr-gdb -se nacl-test.elf --eval-command="target remote localhost:1212" --eval-command="load"'
killall simulavr
debugger: lib-test
......
......@@ -2,73 +2,97 @@
#define F_CPU 16000000UL
#define BAUD 9600
#define ARDUINO
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <avr/io.h>
#include "../libs/pagerlib/pagerlib.h"
#include "../libs/pagerlib/things.h"
#include <util/delay.h>
#include <util/setbaud.h>
#include </usr/local/include/simavr/avr/avr_mcu_section.h>
#define special_output_port (*((volatile char *)0x20))
char tx_priv_key[crypto_box_SECRETKEYBYTES] = {0x23, 0x4D, 0x77, 0xFFFFFFF7, 0x09, 0x70, 0x46, 0xFFFFFF82, 0xFFFFFFC7, 0x0B, 0x70, 0x39, 0xFFFFFFDA, 0x24, 0x4A, 0xFFFFFF9C, 0x28, 0x7A, 0xFFFFFFD8, 0xFFFFFFCE, 0x7B, 0xFFFFFFEF, 0xFFFFFFFB, 0x75, 0x7E, 0x34, 0x16, 0xFFFFFF99, 0xFFFFFFF6, 0x60, 0x08, 0x0A };
char tx_comp_p[crypto_box_PUBLICKEYBYTES] = {0xFFFFFFC1, 0xFFFFFFEF, 0xFFFFFF89, 0xFFFFFFA8, 0x66, 0x44, 0xFFFFFFD6, 0x72, 0x26, 0xFFFFFF84, 0xFFFFFFA9, 0xFFFFFFF5, 0xFFFFFF9A, 0xFFFFFFFC, 0xFFFFFF88, 0x38, 0xFFFFFFA3, 0x1D, 0x71, 0x05, 0xFFFFFFB9, 0x0A, 0x0F, 0xFFFFFFF3, 0xFFFFFF93, 0xFFFFFFEF, 0x48, 0xFFFFFFA1, 0xFFFFFFB7, 0x49, 0xFFFFFF90, 0x6D};
char rx_priv_key[crypto_box_SECRETKEYBYTES] = {0xFFFFFF84, 0x70, 0x10, 0x49, 0x01, 0xFFFFFFC3, 0x4F, 0xFFFFFFA2, 0x2A, 0x2A, 0xFFFFFFB9, 0x19, 0xFFFFFF8D, 0x1E, 0x44, 0x46, 0x26, 0x15, 0x3B, 0xFFFFFF92, 0x00, 0xFFFFFF91, 0x6A, 0x41, 0x36, 0xFFFFFFDF, 0x7E, 0xFFFFFF97, 0x6D, 0x3C, 0x6E, 0x01 };
char rx_comp_p[crypto_box_PUBLICKEYBYTES] = {0xFFFFFFA9, 0xFFFFFFB4, 0xFFFFFFA1, 0xFFFFFFF3, 0x56, 0x27, 0x70, 0xFFFFFF9E, 0xFFFFFFBD, 0x32, 0x27, 0x0A, 0xFFFFFFB8, 0x06, 0xFFFFFFE7, 0xFFFFFFE8, 0xFFFFFF98, 0xFFFFFFA4, 0x11, 0xFFFFFFD8, 0xFFFFFFB5, 0x64, 0xFFFFFF84, 0x64, 0xFFFFFFB6, 0xFFFFFFB6, 0x48, 0xFFFFFFEC, 0x0C, 0xFFFFFFA4, 0x1F, 0x49};
struct pl_keypair *sender, *receiver;
char clear_message[] = "dit is een test berichtje :) ";
struct pl_ctx * context;
/*
void uart_putchar(char c, FILE *stream) {
if (c == '\n') {
uart_putchar('\r', stream);
}
loop_until_bit_is_set(UCSR0A, UDRE0);
UDR0 = c;
}
FILE uart_output = FDEV_SETUP_STREAM(uart_putchar, NULL, _FDEV_SETUP_WRITE);
void debug_puts(const char *str)
char uart_getchar(FILE *stream) {
loop_until_bit_is_set(UCSR0A, RXC0);
return UDR0;
}
FILE uart_input = FDEV_SETUP_STREAM(NULL, uart_getchar, _FDEV_SETUP_READ);
*/
void uart_putchar(char c) {
loop_until_bit_is_set(UCSR0A, UDRE0); /* Wait until data register empty. */
UDR0 = c;
}
void uart_print(char *string){
for(int i = 0; string[i] != '\0'; i++)
uart_putchar(string[i]);
}
char uart_getchar(void) {
loop_until_bit_is_set(UCSR0A, RXC0); /* Wait until data exists. */
return UDR0;
}
/*
get_message(struct pl_pagermessage * message)
{
const char *c;
for(c = str; *c; c++)
special_output_port = *c;
for(int i=0; i<sizeof(struct pl_pagermessage); i++)
memset(&message[i], uart_getchar(), sizeof(char));
uart_print("msg received\n");
return 0;
}
*/
unsigned char rx_sk[crypto_box_SECRETKEYBYTES] = {0x84, 0x70, 0x10, 0x49, 0x01, 0xC3, 0x4F, 0xA2, 0x2A, 0x2A, 0xB9, 0x19, 0x8D, 0x1E, 0x44, 0x46, 0x26, 0x15, 0x3B, 0x92, 0x00, 0x91, 0x6A, 0x41, 0x36, 0xDF, 0x7E, 0x97, 0x6D, 0x3C, 0x6E, 0x01 };
unsigned char rx_pk[crypto_box_PUBLICKEYBYTES] = {0xA9, 0xB4, 0xA1, 0xF3, 0x56, 0x27, 0x70, 0x9E, 0xBD, 0x32, 0x27, 0x0A, 0xB8, 0x06, 0xE7, 0xE8, 0x98, 0xA4, 0x11, 0xD8, 0xB5, 0x64, 0x84, 0x64, 0xB6, 0xB6, 0x48, 0xEC, 0x0C, 0xA4, 0x1F, 0x49};
unsigned char tx_sk[crypto_box_SECRETKEYBYTES] = {0x7, 0x42, 0xc8, 0x29, 0xe6, 0x83, 0x11, 0x69, 0xb4, 0xa6, 0x46, 0x5, 0xa6, 0x7e, 0x5c, 0xe5, 0xe4, 0x2f, 0xb1, 0x74, 0x2, 0xc4, 0xdf, 0xe3, 0xec, 0x69, 0x59, 0xdc, 0x24, 0x60, 0xb0, 0xf2};
unsigned char tx_pk[crypto_box_PUBLICKEYBYTES] = {0xb0, 0xdd, 0xb4, 0x70, 0xad, 0x7a, 0x2d, 0x82, 0x8a, 0xfa, 0xa4, 0x7b, 0xee, 0xa8, 0xf5, 0xe5, 0xc8, 0xf8, 0x54, 0x69, 0xf3, 0xe5, 0xbc, 0x27, 0xde, 0xf4, 0xbf, 0x63, 0x3a, 0xad, 0x4c, 0x23};
int main(void)
int main()
{
// uart_init();
// uart_print("haaalloo");
uart_init();
debug_puts("start main\n");
uart_print("main\n\r");
struct pl_keypair *tx_kp;
struct pl_ctx *tx_ctx;
char clear_message[] = "Blaat blaat, dit is een test berichtje :), en nog meer en meer en meer 123456744555 blablablablablablabal jajajajaj hee blaat ";
char *rx_pubkey;
/* init */
tx_kp = NULL;
char clear_message[] = "Blaat blaat, dlaat ";
tx_ctx = pl_init();
memcpy(tx_ctx->kp->secret_key, tx_sk, crypto_box_SECRETKEYBYTES);
memcpy(tx_ctx->kp->public_key, tx_pk, crypto_box_PUBLICKEYBYTES);
pl_load_key_in_list(tx_ctx, tx_ctx->kp);
uart_print("loaded keypair \n\r");
pl_set_receiver(tx_ctx, rx_pk);
memcpy(tx_ctx->msg->msg, clear_message, MSG_SIZE);
uart_print("loaded message and set receiver \n\r");
//pl_send_message(tx_ctx);
//uart_print("encrypted the message \n\r");
/* load keypair */
memcpy(tx_ctx->kp->secret_key, tx_priv_key, crypto_box_SECRETKEYBYTES);
memcpy(tx_ctx->kp->public_key, tx_comp_p, crypto_box_PUBLICKEYBYTES);
/*load public key of receiver*/
memcpy(tx_ctx->receiver_public_key, rx_comp_p, crypto_box_PUBLICKEYBYTES);
memcpy(tx_ctx->msg->msg, clear_message, MSG_SIZE);
debug_puts("loaded msg\n");
pl_send_message(tx_ctx);
//debug_puts("processed msg\n");
//uart_print("set keys and proces msg\n");
//******** rx ***********/
/*
struct pl_ctx *rx_ctx;
rx_ctx = pl_init();
memcpy(rx_ctx->kp->secret_key, rx_priv_key, crypto_box_SECRETKEYBYTES);
memcpy(rx_ctx->kp->public_key, rx_comp_p, crypto_box_PUBLICKEYBYTES);
pl_load_key_in_list(rx_ctx, rx_ctx->kp);
struct list_kp * list;
for(list = rx_ctx->keypairs; list != NULL; list = list->next) {
uart_print("id ");
uart_print(list->id);
}
rx_ctx->msg = message;
pl_receive_message(rx_ctx) ;
*/
return 0;
}
......@@ -17,11 +17,11 @@ void hex_dump(struct pl_keypair *keypair)
printf("private key:\n");
printf("{");
for(int i=0; i < crypto_box_SECRETKEYBYTES; i++){
printf("0x%02X, ", keypair->secret_key[i]);
printf("0x%hhx, ", keypair->secret_key[i]);
}
printf("}\npublic key:\n{");
for(int i=0; i < crypto_box_PUBLICKEYBYTES; i++){
printf("0x%02X, ", keypair->public_key[i]);
printf("0x%hhx, ", keypair->public_key[i]);
}
printf("}\n\n");
}
......
......@@ -7,6 +7,8 @@
int main ()
{
char rx_priv_key[crypto_box_SECRETKEYBYTES] = {0x84, 0x70, 0x10, 0x49, 0x01, 0xC3, 0x4F, 0xA2, 0x2A, 0x2A, 0xB9, 0x19, 0x8D, 0x1E, 0x44, 0x46, 0x26, 0x15, 0x3B, 0x92, 0x00, 0x91, 0x6A, 0x41, 0x36, 0xDF, 0x7E, 0x97, 0x6D, 0x3C, 0x6E, 0x01 };
char rx_comp_p[crypto_box_PUBLICKEYBYTES] = {0xA9, 0xB4, 0xA1, 0xF3, 0x56, 0x27, 0x70, 0x9E, 0xBD, 0x32, 0x27, 0x0A, 0xB8, 0x06, 0xE7, 0xE8, 0x98, 0xA4, 0x11, 0xD8, 0xB5, 0x64, 0x84, 0x64, 0xB6, 0xB6, 0x48, 0xEC, 0x0C, 0xA4, 0x1F, 0x49};
struct pl_ctx *rx_ctx;
struct pl_keypair *rx_kp;
......@@ -16,7 +18,10 @@ int main ()
rx_ctx = pl_init();
/*load keypair */
pl_load_keypair_from_file(rx_ctx, "rx");
// pl_load_keypair_from_file(rx_ctx, "rx");
memcpy(rx_ctx->kp->public_key, rx_comp_p, crypto_box_PUBLICKEYBYTES);
memcpy(rx_ctx->kp->secret_key, rx_priv_key, crypto_box_SECRETKEYBYTES);
pl_load_key_in_list(rx_ctx, rx_ctx->kp);
pl_print_keylist(rx_ctx);
......
......@@ -16,11 +16,12 @@ void load_a_lot_of_keys(struct pl_ctx *ctx)
pl_load_public_key_in_list(ctx, pubkey, NULL);
}
}
int main()
{
// struct pl_keypair *tx_kp;
struct pl_ctx *tx_ctx;
char clear_message[] = "Blaat blaat, dit is een test berichtje :), en nog meer en meer en meer 123456744555 blablablablablablabal jajajajaj hee blaat ";
char clear_message[] = "Blaat blaat, dlaat ";
//char *rx_pubkey;
/* init */
......@@ -28,21 +29,23 @@ int main()
tx_ctx = pl_init();
/* load keypair */
pl_load_keypair_from_file(tx_ctx, "tx");
pl_load_keypair_from_file(tx_ctx, "tx");
/*load public key of receiver*/
char *pubkey;
pubkey = pl_load_public_key("rx.pub");
pl_load_public_key_in_list(tx_ctx, pubkey,"test");
// char *pubkey;
// pubkey = pl_load_public_key("rx.pub");
char pubkey[crypto_box_PUBLICKEYBYTES] = {0xA9, 0xB4, 0xA1, 0xF3, 0x56, 0x27, 0x70, 0x9E, 0xBD, 0x32, 0x27, 0x0A, 0xB8, 0x06, 0xE7, 0xE8, 0x98, 0xA4, 0x11, 0xD8, 0xB5, 0x64, 0x84, 0x64, 0xB6, 0xB6, 0x48, 0xEC, 0x0C, 0xA4, 0x1F, 0x49};
// pl_load_public_key_in_list(tx_ctx, pubkey,"test");
pl_print_public_keylist(tx_ctx);
// pl_print_public_keylist(tx_ctx);
pl_set_receiver(tx_ctx, pubkey);
memcpy(tx_ctx->msg->msg, clear_message, MSG_SIZE); // copy clear msg into context
switch(pl_send_message(tx_ctx)) { // send msg
case 0:
printf("SEND: OK! \n");
// printf("SEND: OK! \n");
break;
case -1:
printf("SEND: -1 \n");
......
......@@ -23,16 +23,16 @@ extern "C"
struct pl_keypair
{
char public_key[crypto_box_PUBLICKEYBYTES];
char secret_key[crypto_box_SECRETKEYBYTES];
unsigned char public_key[crypto_box_PUBLICKEYBYTES];
unsigned char secret_key[crypto_box_SECRETKEYBYTES];
};
struct pl_pagermessage
{
uint8_t address;
char sender_public_key[crypto_box_PUBLICKEYBYTES];
unsigned char sender_public_key[crypto_box_PUBLICKEYBYTES];
unsigned char nonce[crypto_box_NONCEBYTES];
char msg[MSG_SIZE];
unsigned char msg[MSG_SIZE];
};
......
......@@ -14,7 +14,6 @@
#include "things.h"
#include "pagerlib.h"
#ifndef ARDUINO
//#include "nacl/crypto_box.h"
#else
......@@ -64,7 +63,7 @@ int rng (uint8_t *dest, unsigned size)
* takes a compressed point and turns it into an address
* FIXME: should become a proper hash function
*/
inline uint8_t compressed_point_to_addr( uint8_t input[])
uint8_t compressed_point_to_addr( uint8_t input[])
{
uint8_t hashval;
int i = 0;
......@@ -95,7 +94,8 @@ inline struct pl_ctx * pl_init()
return ctx;
}
inline int pl_set_receiver(struct pl_ctx *ctx, char *public_key)
#ifndef RX // al these functions are not needed for receiving messages
inline int pl_set_receiver(struct pl_ctx *ctx, unsigned char *public_key)
{
memcpy(ctx->receiver_public_key, public_key, crypto_box_PUBLICKEYBYTES);
return 0;
......@@ -129,33 +129,44 @@ int pl_set_receiver_by_address(struct pl_ctx *ctx, uint32_t addr)
inline int pl_send_message(struct pl_ctx *ctx) {
// copy sender public key from keypair into pager message
memcpy(&ctx->msg->sender_public_key, &ctx->kp->public_key, sizeof(ctx->msg->sender_public_key));
memcpy(ctx->msg->sender_public_key, ctx->kp->public_key, crypto_box_PUBLICKEYBYTES);
// turn receiver public key into address
ctx->msg->address = compressed_point_to_addr(ctx->receiver_public_key);
// encrypt with NaCl
char temp_plain[PLAIN_MSG_SIZE];
char temp_encrypted[CRYPTED_MSG_SIZE];
unsigned char temp_plain[PLAIN_MSG_SIZE];
unsigned char temp_encrypted[CRYPTED_MSG_SIZE];
int rc;
// if(MSG_SIZE+crypto_box_ZEROBYTES >= MAX_MSG_SIZE) {
// return -2;
// }
memset(temp_plain, '\0', crypto_box_ZEROBYTES);
memcpy(temp_plain + crypto_box_ZEROBYTES, ctx->msg->msg, PLAIN_MSG_SIZE);
//char nonce[crypto_box_NONCEBYTES];
#ifdef ARDUINO
memset(&ctx->msg->nonce, 0, crypto_box_NONCEBYTES); //FIXME this is for testing purposes and should be changed
memset(ctx->msg->nonce, '\0', crypto_box_NONCEBYTES); //FIXME this is for testing purposes and should be changed
uart_write("nonce crap\n\r");
#else
randombytes(ctx->msg->nonce, crypto_box_NONCEBYTES);
// randombytes(ctx->msg->nonce, crypto_box_NONCEBYTES);
#endif
rc = crypto_box(temp_encrypted, temp_plain, crypto_box_ZEROBYTES + PLAIN_MSG_SIZE, ctx->msg->nonce, ctx->receiver_public_key, ctx->kp->secret_key);
if( rc != 0 )
return -1;
if( is_zero(temp_plain, crypto_box_BOXZEROBYTES) != 0 )
return -3;
// if( rc != 0 )
// return -1;
//if( is_zero(temp_plain, crypto_box_BOXZEROBYTES) != 0 )
// return -3;
memcpy(ctx->msg->msg, temp_encrypted, CRYPTED_MSG_SIZE);
// printf("temp_encrypted: \n %s \n ", temp_encrypted);
// return crypto_box_ZEROBYTES + length - crypto_box_BOXZEROBYTES;
return 0;
}
......@@ -174,18 +185,21 @@ int pl_send_message_fifo(struct pl_ctx *ctx, char *fifo)
return 0;
}
#endif
#endif //this is the endif for the ifndef RX
int pl_receive_message(struct pl_ctx * ctx)
{
// memset(&ctx->clear_message, 7, sizeof(ctx->clear_message));
DBM("msg->msg in pl_receive_message before crypt", sizeof(ctx->msg->msg), &ctx->msg->msg);
// DBM("msg->msg in pl_receive_message before crypt", sizeof(ctx->msg->msg), &ctx->msg->msg);
/* check if message is for us */
struct list_kp * list;
int found = 0;
for(list = ctx->keypairs; list != NULL; list = list->next) {
#ifndef ARDUINO
printf("\n address in msg: %u address in list: %u \n", ctx->msg->address, list->id );
#endif
if (ctx->msg->address == list->id){
// set keypair to use
ctx->kp = list->kp;
......@@ -194,11 +208,11 @@ int pl_receive_message(struct pl_ctx * ctx)
}
}
// exit (and trow away msg) when address not found
if (found == 0) {
/* if (found == 0) {
ctx->msg = (struct pl_pagermessage *) malloc(sizeof(struct pl_pagermessage));
free(ctx->msg);
return -4;
}
}*/
pl_inbox_append(ctx, ctx->msg);
// make pointer to current message null to prevent overwriting the stored message
ctx->msg = NULL;
......@@ -220,8 +234,8 @@ int pl_decrypt(struct pl_ctx * ctx, struct pl_pagermessage * msg, char plain[PL
// TODO: figure out why all the hassle with the zerobytes
#ifndef NOCRYPT
char temp_encrypted[CRYPTED_MSG_SIZE];
char temp_plain[PLAIN_MSG_SIZE];
unsigned char temp_encrypted[CRYPTED_MSG_SIZE];
unsigned char temp_plain[PLAIN_MSG_SIZE];
int rc;
memset(temp_encrypted, '\0', crypto_box_BOXZEROBYTES);
......@@ -231,14 +245,18 @@ int pl_decrypt(struct pl_ctx * ctx, struct pl_pagermessage * msg, char plain[PL
if( rc != 0 )
return -1;
if( is_zero(temp_plain, crypto_box_ZEROBYTES) != 0 )
return -3;
//if( is_zero(temp_plain, crypto_box_ZEROBYTES) != 0 )
// return -3;
memcpy(plain, temp_plain + crypto_box_ZEROBYTES, PLAIN_MSG_SIZE);
// for now store unencrypted in place of encrypted, but should be stored encrypted no?
memcpy(ctx->msg->msg + crypto_box_ZEROBYTES, temp_plain + crypto_box_ZEROBYTES, PLAIN_MSG_SIZE);
// memcpy(ctx->msg->msg + crypto_box_ZEROBYTES, temp_plain + crypto_box_ZEROBYTES, PLAIN_MSG_SIZE);
memcpy(ctx->msg->msg, plain, PLAIN_MSG_SIZE);
#ifndef ARDUINO
printf("new message: \n %s \n ", plain);
#endif
// return crypto_box_BOXZEROBYTES + MSG_SIZE - crypto_box_ZEROBYTES;
return 0;
#endif
......@@ -325,7 +343,7 @@ int pl_save_public_key(char *pubkey, char *filename)
char * pl_load_public_key(char *filename)
{
#ifdef ARDUINO
return 1;
return "1";
#else
char *public_key;
public_key = malloc(crypto_box_PUBLICKEYBYTES);
......@@ -415,7 +433,7 @@ int pl_print_keylist(struct pl_ctx *ctx)
}
}
int pl_load_public_key_in_list(struct pl_ctx *ctx, char *public_key, char *alias)
int pl_load_public_key_in_list(struct pl_ctx *ctx, unsigned char *public_key, char *alias)
{
struct list_public_key *list, *ni, *last, *listprev;
list = ctx->public_keys;
......
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