Commit 61c7d5a0 authored by hark's avatar hark

ported pagerlib to arduino

parent abc4a904
Pipeline #32 skipped
USER_LIB_PATH=../arduino-build/libraries/
ARDUINO_DIR=
USER_LIB_PATH=../libs/
BOARD_TAG=atmega328
ARDUINO_LIBS=RadioHead SPI AESLib Wire LCDI2C_LK162 MemoryFree micro-ecc
include /usr/share/arduino/Arduino.mk
......
BOARD_TAG=atmega328
ARDUINO_DIR=../arduino-build/
include /usr/share/arduino/Arduino.mk
ARDUINO_LIB_PATH=../arduino-build/libraries
USER_LIB_PATH=../libs/
ARDUINO_LIBS=RadioHead SPI AESLib micro-ecc MemoryFree
include /usr/share/arduino/Arduino.mk
ARDUINO_LIBS=pagerlib RadioHead SPI AESLib micro-ecc MemoryFree
DEVICE_PATH = /dev/ttyUSB1
TARGET = arduino-tx
#include <uECC.h>
#include <micro-ecc.h>
#include <AESLib.h>
#include <RadioHead.h>
#include <RH_ASK.h>
......@@ -6,6 +6,8 @@
#include <SPI.h> // Not actually used but needed to compile
#include <MemoryFree.h>
#include <pagerlib.h>
#define ADDRESS 5
#define NUM_ECC_DIGITS 24 //size of privkey, curvesize in bytes
#define CURVE uECC_secp192r1()
......
#ifdef __cplusplus
extern "C"
{
#endif
// pager.h
/*
......@@ -8,8 +14,8 @@
struct pl_keypair
{
uint8_t public[64];
uint8_t private[32];
uint8_t public_key[64];
uint8_t private_key[32];
uint8_t compressed_point[33];
};
......@@ -53,4 +59,8 @@ Rate Max payload size
8:15 not defined
*/
#ifdef __cplusplus
} /* end of extern "C" */
#endif
......@@ -3,9 +3,6 @@
#include "../micro-ecc/uECC.h"
#include "packets.h"
#include <stdlib.h>
#include "mbedtls/entropy.h"
#include "mbedtls/ctr_drbg.h"
#include "mbedtls/aes.h"
#include "things.h"
const struct uECC_Curve_t * curve;
......@@ -15,11 +12,18 @@ const struct uECC_Curve_t * curve;
#endif
#ifdef ARDUINO
#include <AESLib.h>
#define DBG
#define DB
#else
#define DBG(f_, ...) printf((f_), ##__VA_ARGS__);
#define DB(f_, ...) dump_buffer((f_), ##__VA_ARGS__);
#include "mbedtls/entropy.h"
#include "mbedtls/ctr_drbg.h"
#include "mbedtls/aes.h"
#endif
......@@ -72,7 +76,7 @@ struct pl_pagermessage * pl_send_message(struct pl_keypair *to, struct pl_keyp
memcpy(msg->iv, iv, sizeof(msg->iv));
/*calculate shared secret on sender*/
if (!uECC_shared_secret(to->public, from->private, shared_secret, curve)) {
if (!uECC_shared_secret(to->public_key, from->private_key, shared_secret, curve)) {
DBG("shared_secret() failed (1)\n");
return 1;
}
......@@ -85,7 +89,7 @@ struct pl_pagermessage * pl_send_message(struct pl_keypair *to, struct pl_keyp
memcpy(msg->msg, tm, MSG_SIZE);
aes_ctx = aes192_cbc_enc_start(shared_secret, iv);
aes192_cbc_enc_continue(enc_aes, msg->msg, MSG_SIZE);
aes192_cbc_enc_continue(aes_ctx, msg->msg, MSG_SIZE);
aes192_cbc_enc_finish(aes_ctx);
......@@ -123,7 +127,7 @@ void pl_receive_message(struct pl_keypair *me, struct pl_pagermessage *msg)
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)) {
if (!uECC_shared_secret(sender_decompressed_point, me->private_key, shared_secret, curve)) {
DBG("shared_secret() failed (receive)\n");
}
DB(SHARED_SECRET_SIZE, shared_secret);
......@@ -158,16 +162,16 @@ struct pl_keypair * pl_create_keypair() {
struct pl_keypair *keypair;
keypair = malloc(sizeof(struct pl_keypair));
memset(keypair->public, 3, sizeof(keypair->public));
memset(keypair->private, 4, sizeof(keypair->private));
memset(keypair->public_key, 3, sizeof(keypair->public_key));
memset(keypair->private_key, 4, sizeof(keypair->private_key));
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)) {
if (!uECC_make_key(keypair->public_key, keypair->private_key, curve)) {
DBG("uECC_make_key() failed\n");
}
uECC_compress(keypair->public, keypair->compressed_point, curve);
uECC_compress(keypair->public_key, keypair->compressed_point, curve);
DBG(" \n comp point \n ");
DB(sizeof(keypair->compressed_point), keypair->compressed_point);
......@@ -206,3 +210,5 @@ pl_load_key(struct pl_keypair *key, char * filename) {
}
#ifdef __cplusplus
extern "C"
{
#endif
#include "packets.h"
......@@ -15,6 +24,10 @@ void pl_receive_message(struct keypair *, struct pagermessage *);
void pl_save_key(struct keypair *key, char * filename);
void pl_load_key(struct keypair *key, char * filename);
#ifdef __cplusplus
} /* end of extern "C" */
#endif
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "mbedtls/entropy.h"
#include "mbedtls/ctr_drbg.h"
#include "mbedtls/aes.h"
#if defined(__AVR_ATmega328P__)
#define ARDUINO
......@@ -13,26 +10,32 @@
#include <avr/pgmspace.h>
const char PROGMEM b64_alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/"
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/";
#else
const char b64_alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/";
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/";
#include "mbedtls/entropy.h"
#include "mbedtls/ctr_drbg.h"
#include "mbedtls/aes.h"
#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);
*/
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);
*/
#ifdef ARDUINO
void dump_buffer(unsigned int n, const unsigned char* buf)
{
// not implemented
// not implemented
return;
}
#else
void
......@@ -63,143 +66,150 @@ dump_buffer(unsigned int n, const unsigned char* buf)
BREAKOUT:
return;
}
#endif
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");
for(unsigned i=0; i<size; ++i) {
printf("%02X ", (unsigned)vli[i]);
}
printf("\n");
}
#endif
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);
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];
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;
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];
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);
while(inputLen--) {
a3[i++] = *(input++);
if(i == 3) {
a3_to_a4(a4, a3);
for(i = 0; i < 4; i++) {
for(i = 0; i < 4; i++) {
#ifdef ARDUINO
output[encLen++] = pgm_read_byte(&b64_alphabet[a4[i]]);
output[encLen++] = pgm_read_byte(&b64_alphabet[a4[i]]);
#else
output[encLen++] = b64_alphabet[a4[i]];
#endif
}
i = 0;
}
}
i = 0;
}
}
if(i) {
for(j = i; j < 3; j++) {
a3[j] = '\0';
}
if(i) {
for(j = i; j < 3; j++) {
a3[j] = '\0';
}
a3_to_a4(a4, a3);
a3_to_a4(a4, a3);
for(j = 0; j < i + 1; j++) {
for(j = 0; j < i + 1; j++) {
#ifdef ARDUINO
output[encLen++] = pgm_read_byte(&b64_alphabet[a4[j]]);
output[encLen++] = pgm_read_byte(&b64_alphabet[a4[j]]);
#else
output[encLen++] = b64_alphabet[a4[j]];
output[encLen++] = b64_alphabet[a4[j]];
#endif
}
while((i++ < 3)) {
output[encLen++] = '=';
}
}
output[encLen] = '\0';
return encLen;
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 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 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++;
}
int i = 0;
int numEq = 0;
for(i = inputLen - 1; input[i] == '='; i--) {
numEq++;
}
return ((6 * inputLen) / 8) - numEq;
return ((6 * inputLen) / 8) - numEq;
}
////////////////////////////////////////////////
......
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