Commit dacd1a95 authored by yids's avatar yids

Merge branch 'master' of ssh://gitlab.irl.020/yids/avr-pager

Conflicts:
	base-tx/Makefile
parents 6658fae8 13bdc24f
Pipeline #18 skipped
......@@ -8,4 +8,14 @@ In the multicast slot pager messages can be send to a multicast group that is al
In that way receive only devices can receive messages.
"
Messages can be ―unicast‖ or ―multicast‖. Unicast messages are sent to a single end-device
and multicast messages are sent to multiple end-devices. All devices of a multicast group
must share the same multicast address and associated encryption keys. The LoRaWAN
Class B specification does not specify means to remotely setup such a multicast group or
securely distribute the required multicast key material. This must either be performed during
the node personalization or through the application layer.
"
http://thethingsnetwork.org/wiki/Hardware/OverviewNodes
......@@ -6,3 +6,8 @@ to try it out:
apt-get install arduino arduino-mk
- get the required libraries from https://gitlab.net.020wireless.nl/yids/arduino-build
some things on ECC:
http://www.infosecwriters.com/Papers/Anoopms_ECC.pdf
http://www.johannes-bauer.com/compsci/ecc/
USER_LIB_PATH=../arduino-build/libraries/
BOARD_TAG=atmega328
ARDUINO_LIBS=RadioHead SPI AESLib Wire LCDI2C_LK162 MemoryFree micro-ecc
include /usr/share/arduino/Arduino.mk
......
CC=gcc
CFLAGS=-I ../include
LIBS=../include/ecc.c
#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"
#LIBS=../include/ecc.c ../include/oaes_base64.c ../include/oaes_lib.c ../include/rand.c ../libs/micro-ecc/uECC.h
#LIBS=
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)
......@@ -4,10 +4,43 @@
struct pagermessage
{
// there is no space to store all pubkeys of possible senders, so need to be send
EccPoint senderPubkey;
//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];
//
// address is SHA1 hash of publickey
int address;
// maybe use 7bit ascii, or even smaller characters (only letters + numbers)
char msg[127];
}
/* subjectPublicKeyInfo field
* in X.509 certificates [PKI]
* https://www.ietf.org/rfc/rfc5480.txt
* https://tools.ietf.org/html/rfc5915
*/
/*
* gpg
* A V4 fingerprint is the 160-bit SHA-1 hash of the octet 0x99,
* followed by the two-octet packet length, followed by the entire
* Public-Key packet starting with the version field. The Key ID is the
* low-order 64 bits of the fingerprint. Here are the fields of the
* hash material, with the example of a DSA key:
*/
/* lorawan
Rate Max payload size
0 51
1 51
2 51
3 115
4 222
5 222
6 222
7 222
8:15 not defined
*/
/* Copyright 2014, Kenneth MacKay. Licensed under the BSD 2-clause license. */
#include "../libs/micro-ecc/uECC.h"
//#include "../include/oaes_lib.h"
//#include "mbedtls_config.h"
//#include "../libs/mbedtls-2.2.0/include/mbedtls/config.h"
//#include "../libs/mbedtls-2.2.0/include/mbedtls/aes.h"
//#include "../libs/mbedtls-2.2.0/library/aes.c"
#include "mbedtls/entropy.h"
#include "mbedtls/ctr_drbg.h"
#include "mbedtls/aes.h"
//#include <gnutls/gnutls.h>
#include <stdio.h>
#include <string.h>
#ifndef uECC_TEST_NUMBER_OF_ITERATIONS
#define uECC_TEST_NUMBER_OF_ITERATIONS 1
#endif
// mbedtls things
mbedtls_aes_context aes;
void
dump_buffer(unsigned n, const unsigned char* buf)
{
const unsigned char *p, *end;
unsigned i, j;
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");
}
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];
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 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;
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);
/* now we send the message....
* sender_compressed_key
* crypt_message
* iv??
* */
/* decompress the senders public key */
uECC_decompress(sender_compressed_point, sender_decompressed_point, curves[c]);
/*calculate shared secret on receiver*/
if (!uECC_shared_secret(sender_decompressed_point, receiver_private, receiver_shared_secret, curves[c])) {
printf("shared_secret() failed (2)\n");
return 1;
}
/* decrypt the message */
mbedtls_aes_setkey_dec( &aes, receiver_shared_secret, 256 );
mbedtls_aes_crypt_cbc( &aes, MBEDTLS_AES_DECRYPT, 128, siv, crypt_message, decrypted_message );
printf("decrypted_message: \n ");
dump_buffer(sizeof(decrypted_message), decrypted_message);
if (memcmp(sender_shared_secret, receiver_shared_secret, sizeof(sender_shared_secret)) != 0) {
printf(" \n shared secret is not equal!!! \n \n ");
}
if (memcmp(sender_public, sender_decompressed_point, sizeof(sender_public)) != 0) {
printf("Original and decompressed points are not identical!\n");
vli_print("Original point = ", sender_public, sizeof(sender_public));
vli_print("Compressed point = ", sender_compressed_point, sizeof(sender_compressed_point));
vli_print("Decompressed point = ", sender_decompressed_point, sizeof(sender_decompressed_point));
}
}
printf("\n");
}
return 0;
}
......@@ -3,7 +3,7 @@
#export ARDUINO_LIBS="RadioHead SPI AVRCryptoLib"
cd arduino-tx
make
make && make do_upload
cd ../arduino-rx
make
make && make do_upload
cd ..
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