Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
7
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
yids
avr-pager
Commits
c1b17d4e
Commit
c1b17d4e
authored
Dec 06, 2015
by
hark
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'master' of
ssh://gitlab.net.020wireless.nl:2222/yids/avr-pager
parents
aa013437
6c691b4e
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
180 additions
and
132 deletions
+180
-132
arduino-rx/Makefile
arduino-rx/Makefile
+1
-1
arduino-rx/arduino-rx.ino
arduino-rx/arduino-rx.ino
+121
-72
arduino-tx/Makefile
arduino-tx/Makefile
+1
-1
arduino-tx/arduino-tx.ino
arduino-tx/arduino-tx.ino
+57
-58
No files found.
arduino-rx/Makefile
View file @
c1b17d4e
USER_LIB_PATH
=
../arduino-build/libraries/
BOARD_TAG
=
atmega328
ARDUINO_LIBS
=
RadioHead SPI AESLib Wire LCDI2C_LK162
ARDUINO_LIBS
=
RadioHead SPI AESLib Wire LCDI2C_LK162
MemoryFree micro-ecc
include
/usr/share/arduino/Arduino.mk
DEVICE_PATH
=
/dev/ttyUSB2
...
...
arduino-rx/arduino-rx.ino
View file @
c1b17d4e
...
...
@@ -5,7 +5,7 @@
// Implements a simplex (one-way) receiver with an Rx-B1 module
#include <stdarg.h>
#include <
ecc
.h>
#include <
uECC
.h>
#include <AESLib.h>
#include <RadioHead.h>
#include <RH_ASK.h>
...
...
@@ -14,9 +14,11 @@
#include <string.h>
#include <Wire.h>
#include <LCDI2C_LK162-12.h>
#include <MemoryFree.h>
#define BLOCK_SIZE 16
#define NUM_BLOCKS 4
#define NUM_ECC_DIGITS 24 //size of privkey, curvesize in bytes
LCDI2C
lcd
=
LCDI2C
(
2
,
16
,
0x50
,
0
);
...
...
@@ -26,33 +28,9 @@ aes_context ctx; // context for the cbc crypto stuff
RH_ASK
driver
(
5000
);
RHDatagram
manager
(
driver
,
ADDRESS
);
// pubkey in readable format
uint8_t
pubkey
[
2
][
NUM_ECC_DIGITS
];
//my pub-priv keypair
uint8_t
privkey
[
NUM_ECC_DIGITS
]
=
{
0x4D
,
0xDE
,
0x17
,
0xCE
,
0xAC
,
0x4F
,
0x81
,
0x25
,
0xA9
,
0xE5
,
0xC1
,
0x82
,
0x79
,
0x65
,
0x71
,
0x13
,
0x47
,
0xD6
,
0xF4
,
0x3B
,
0x5E
,
0x1F
,
0x27
,
0x53
};
EccPoint
localPubkey
=
{
{
0xF3
,
0xCD
,
0x7D
,
0x81
,
0x15
,
0x01
,
0xC0
,
0x3E
,
0xB5
,
0x56
,
0x5B
,
0xF1
,
0x1A
,
0x99
,
0xFB
,
0x0B
,
0x20
,
0x2B
,
0x18
,
0x82
,
0xD3
,
0x71
,
0x6F
,
0x1E
},
{
0x4B
,
0x84
,
0xC7
,
0xDD
,
0x67
,
0x6B
,
0xED
,
0x45
,
0x1A
,
0x4B
,
0x54
,
0x5A
,
0x19
,
0xCA
,
0x1B
,
0x6F
,
0x3B
,
0x93
,
0xBA
,
0xF4
,
0x2A
,
0x9B
,
0x65
,
0x69
}};
EccPoint
remotePubkey
;
void
p
(
char
*
fmt
,
...
){
char
tmp
[
128
];
// resulting string limited to 128 chars
va_list
args
;
va_start
(
args
,
fmt
);
vsnprintf
(
tmp
,
128
,
fmt
,
args
);
va_end
(
args
);
Serial
.
print
(
tmp
);
}
void
dump
(
char
*
text
,
uint8_t
*
d
)
{
int
i
;
p
(
"%-20s"
,
text
);
for
(
i
=
0
;
i
<
NUM_ECC_DIGITS
;
++
i
)
p
(
"%02x "
,
d
[
NUM_ECC_DIGITS
-
i
-
1
]);
Serial
.
print
(
"
\n
"
);
}
// uECC keys
uint8_t
privkey
[
25
]
=
{
0x50
,
0xEA
,
0x8F
,
0x57
,
0xFD
,
0xBE
,
0x75
,
0xAE
,
0x17
,
0x70
,
0xC6
,
0xF0
,
0x51
,
0x11
,
0x5C
,
0xA
,
0xF6
,
0xFE
,
0xCF
,
0x4
,
0xC9
,
0xBD
,
0xFC
,
0x7D
,
0xD6
};
uint8_t
pubkey
[
48
]
=
{
0x27
,
0x8C
,
0x41
,
0x2C
,
0x1F
,
0xF2
,
0xA9
,
0xCB
,
0x78
,
0xC4
,
0x1E
,
0xBB
,
0x2B
,
0x32
,
0x32
,
0x34
,
0xC3
,
0x5B
,
0xD1
,
0x87
,
0x52
,
0x6C
,
0xBD
,
0x7F
,
0x44
,
0x73
,
0xDC
,
0xF0
,
0xFC
,
0x93
,
0x97
,
0x99
,
0x46
,
0x16
,
0xE0
,
0x8F
,
0x65
,
0xA4
,
0xCB
,
0x65
,
0x59
,
0xA8
,
0xBF
,
0xFD
,
0xB4
,
0x61
,
0x23
,
0xA8
};
uint8_t
*
generateIV
()
{
...
...
@@ -87,34 +65,111 @@ char* decryptAES(uint8_t* p_secret, char* p_data, const aes_context ctx)
Serial
.
println
(
"done"
);
return
p_data
;
}
/*
// calculate shared secret using remote public key and local private key
// returns the shared secret
uint8_t
calcSharedSecret
(
EccPoin
t
*
p_pubkey
,
uint8_t
p_privkey
[
NUM_ECC_DIGITS
])
uint8_t calcSharedSecret(
uint8_
t *p_pubkey, uint8_t p_privkey[NUM_ECC_DIGITS]
, uECC_Curve curve
)
{
Serial.print("Calculating shared secret...");
uint8_t
secret
[
NUM_ECC_DIGITS
];
ecdh_shared_secret
(
secret
,
p_pubkey
,
p_privkey
,
NULL
);
//dump("secret", secret);
uint8_t secret[24];
unsigned long a = millis();
int r = uECC_shared_secret(p_pubkey, p_privkey, secret, curve);
unsigned long b = millis();
Serial.print("Shared secret 1 in "); Serial.println(b-a);
if (!r) {
Serial.print("shared_secret() failed (1)\n");
return 0;
}
return *secret;
}
*/
static
int
RNG
(
uint8_t
*
dest
,
unsigned
size
)
{
// Use the least-significant bits from the ADC for an unconnected pin (or connected to a source of
// random noise). This can take a long time to generate random data if the result of analogRead(0)
// doesn't change very frequently.
while
(
size
)
{
uint8_t
val
=
0
;
for
(
unsigned
i
=
0
;
i
<
8
;
++
i
)
{
int
init
=
analogRead
(
0
);
int
count
=
0
;
while
(
analogRead
(
0
)
==
init
)
{
++
count
;
}
if
(
count
==
0
)
{
val
=
(
val
<<
1
)
|
(
init
&
0x01
);
}
else
{
val
=
(
val
<<
1
)
|
(
count
&
0x01
);
}
}
*
dest
=
val
;
++
dest
;
--
size
;
}
// NOTE: it would be a good idea to hash the resulting random data using SHA-256 or similar.
return
1
;
}
void
generateKeys
()
{
const
struct
uECC_Curve_t
*
curve
=
uECC_secp192r1
();
uint8_t
private1
[
25
];
uint8_t
public1
[
48
];
uint8_t
secret1
[
24
];
unsigned
long
a
=
millis
();
uECC_make_key
(
public1
,
private1
,
curve
);
unsigned
long
b
=
millis
();
Serial
.
print
(
"Generated keypair in "
);
Serial
.
print
(
b
-
a
);
Serial
.
println
(
"Milliseconds"
);
Serial
.
println
(
"Public key:"
);
for
(
int
i
=
0
;
i
<
sizeof
(
public1
);
i
++
){
Serial
.
print
(
"0x"
);
Serial
.
print
(
public1
[
i
],
HEX
);
Serial
.
print
(
", "
);
}
Serial
.
println
(
""
);
Serial
.
println
(
"Private key:"
);
for
(
int
i
=
0
;
i
<
sizeof
(
private1
);
i
++
){
Serial
.
print
(
"0x"
);
Serial
.
print
(
private1
[
i
],
HEX
);
Serial
.
print
(
", "
);
}
Serial
.
println
(
""
);
}
void
setup
()
{
lcd
.
init
();
lcd
.
println
(
"dit is een pager"
);
Serial
.
begin
(
9600
);
// Debugging only
Serial
.
println
(
"
hoi
"
);
Serial
.
println
(
"
RX init
"
);
if
(
!
manager
.
init
())
Serial
.
println
(
"init failed"
);
randomSeed
(
analogRead
(
0
));
// randomSeed(analogRead(0));
Serial
.
print
(
"freeMemory()="
);
Serial
.
println
(
freeMemory
());
uECC_set_rng
(
&
RNG
);
}
void
hashSecret
(
uint8_t
*
p_secret
)
{
Serial
.
println
(
"Secret:"
);
for
(
int
i
=
0
;
i
<
NUM_ECC_DIGITS
;
i
++
){
Serial
.
print
(
p_secret
[
i
]);
Serial
.
print
(
" "
);
}
Serial
.
println
(
""
);
}
void
loop
()
{
// generateKeys(); function to generate keys, have to make a program for this to run on a computer
// crypto vars //
uint8_t
sharedSecret
;
uint8_t
remotePubkey
[
48
];
uint8_t
sharedSecret
[
NUM_ECC_DIGITS
];
const
struct
uECC_Curve_t
*
curve
=
uECC_secp192r1
();
// Radio vars //
uint8_t
*
receivedData
;
...
...
@@ -132,48 +187,42 @@ void loop()
Serial
.
print
(
id
);
Serial
.
println
(
" : "
);
if
(
id
==
50
){
// receiving a public key...
for
(
int
i
=
0
;
i
<
NUM_ECC_DIGITS
;
i
++
){
remotePubkey
.
x
[
i
]
=
buf
[
i
];
//Serial.println(remotePubkey.x[i]);
}
for
(
int
i
=
NUM_ECC_DIGITS
;
i
<
NUM_ECC_DIGITS
*
2
;
i
++
){
remotePubkey
.
y
[
i
-
NUM_ECC_DIGITS
]
=
buf
[
i
];
//Serial.println(remotePubkey.y[i-NUM_ECC_DIGITS]);
}
}
if
(
id
==
51
){
// receiving an encrypted message...
Serial
.
println
(
"Storing received message"
);
receivedData
=
buf
;
}
}
if
(
ecc_valid_public_key
(
&
remotePubkey
)
==
1
){
Serial
.
println
(
"Valid public key received"
);
sharedSecret
=
calcSharedSecret
(
&
remotePubkey
,
privkey
);
Serial
.
println
(
"Done!"
);
}
Serial
.
println
((
char
*
)
receivedData
);
/*
// Crypto //
char* data = "Hallo dit is een testbericht jwz";
if
(
id
==
50
){
// receiving a public key...
for
(
int
i
=
0
;
i
<
NUM_ECC_DIGITS
*
2
;
i
++
){
remotePubkey
[
i
]
=
buf
[
i
];
//Serial.println(remotePubkey.x[i]);
}
}
if
(
id
==
51
){
// receiving an encrypted message...
Serial
.
println
(
"Storing received message"
);
receivedData
=
buf
;
Serial
.
println
((
char
*
)
receivedData
);
}
}
if
(
uECC_valid_public_key
(
remotePubkey
,
curve
)
==
1
){
Serial
.
println
(
"Valid pubkey"
);
uECC_shared_secret
(
remotePubkey
,
privkey
,
sharedSecret
,
curve
);
hashSecret
(
sharedSecret
);
}
else
Serial
.
println
(
"Invalid pubkey"
);
uint8_t sharedSecret = calcSharedSecret(&remotePubkey, privkey);
char* encryptedData;
/* // CBC Crypto //
char* decryptedData;
// uint8_t* iv = generateIV();
// uint8_t* iv = generateIV();
uint8_t iv[16] = {4,9,4,9,4,9,4,9,4,9,4,9,4,9,4,9};
ctx = aes128_cbc_enc_start(&sharedSecret, iv);
encryptedData = encryptAES(&sharedSecret, data, ctx);
ctx = aes128_cbc_enc_start(&sharedSecret, iv);
decryptedData = decryptAES(&sharedSecret, encryptedData, ctx);
ctx = aes128_cbc_dec_start(&sharedSecret, iv);
decryptedData = decryptAES(&sharedSecret, ((char*)receivedData), ctx);
Serial.print("decrypted data:");
Serial.println(decryptedData);
Serial.println ("");
*/
/*
// single block crypto //
aes128_dec_single(&sharedSecret, receivedData);
Serial.println((char*)receivedData);
*/
}
arduino-tx/Makefile
View file @
c1b17d4e
BOARD_TAG
=
atmega328
USER_LIB_PATH
=
../libs/
ARDUINO_LIBS
=
RadioHead SPI AESLib
ARDUINO_LIBS
=
RadioHead SPI AESLib
micro-ecc
include
/usr/share/arduino/Arduino.mk
DEVICE_PATH
=
/dev/ttyUSB1
...
...
arduino-tx/arduino-tx.ino
View file @
c1b17d4e
...
...
@@ -5,7 +5,7 @@
// Implements a simplex (one-way) transmitter with an TX-C1 module
#include <
ecc
.h>
#include <
uECC
.h>
#include <AESLib.h>
#include <RadioHead.h>
#include <RH_ASK.h>
...
...
@@ -16,38 +16,16 @@
#define NUM_BLOCKS 4
#define ADDRESS 5
#define NUM_ECC_DIGITS 24 //size of privkey, curvesize in bytes
aes_context
ctx
;
RH_ASK
driver
(
5000
);
RHDatagram
manager
(
driver
,
ADDRESS
);
//my pub-priv keypair
uint8_t
privkey
[
NUM_ECC_DIGITS
]
=
{
0x14
,
0x14
,
0x92
,
0x2D
,
0x2E
,
0x00
,
0x87
,
0x16
,
0x2D
,
0x43
,
0x0E
,
0xC1
,
0x8A
,
0xD0
,
0x0A
,
0x1E
,
0xEE
,
0x89
,
0x4F
,
0x17
,
0x3F
,
0xB1
,
0x1B
,
0x5A
};
EccPoint
localPubkey
=
{
{
0x90
,
0x6F
,
0xFC
,
0xC8
,
0x07
,
0xA8
,
0x93
,
0x9C
,
0x3B
,
0xDC
,
0xAF
,
0xF1
,
0x8D
,
0xA9
,
0x96
,
0xF8
,
0xC2
,
0x3B
,
0x85
,
0x3E
,
0x5C
,
0x65
,
0xDE
,
0x32
},
{
0xE1
,
0xB1
,
0x28
,
0x7F
,
0xD3
,
0xB0
,
0x94
,
0x24
,
0x69
,
0x95
,
0xDE
,
0x76
,
0x40
,
0x15
,
0x6E
,
0xAE
,
0x74
,
0xE9
,
0x56
,
0x94
,
0xA7
,
0xE4
,
0x63
,
0xEF
}};
//other pubkey
EccPoint
remotePubkey
=
{
{
0xF3
,
0xCD
,
0x7D
,
0x81
,
0x15
,
0x01
,
0xC0
,
0x3E
,
0xB5
,
0x56
,
0x5B
,
0xF1
,
0x1A
,
0x99
,
0xFB
,
0x0B
,
0x20
,
0x2B
,
0x18
,
0x82
,
0xD3
,
0x71
,
0x6F
,
0x1E
},
{
0x4B
,
0x84
,
0xC7
,
0xDD
,
0x67
,
0x6B
,
0xED
,
0x45
,
0x1A
,
0x4B
,
0x54
,
0x5A
,
0x19
,
0xCA
,
0x1B
,
0x6F
,
0x3B
,
0x93
,
0xBA
,
0xF4
,
0x2A
,
0x9B
,
0x65
,
0x69
}};
void
p
(
char
*
fmt
,
...
){
char
tmp
[
128
];
// resulting string limited to 128 chars
va_list
args
;
va_start
(
args
,
fmt
);
vsnprintf
(
tmp
,
128
,
fmt
,
args
);
va_end
(
args
);
Serial
.
print
(
tmp
);
}
// uECC keys
uint8_t
privkey
[
25
]
=
{
0xED
,
0x3F
,
0xB6
,
0xB
,
0x20
,
0x3F
,
0x54
,
0xEE
,
0xCF
,
0xAF
,
0xFC
,
0xB6
,
0x1F
,
0x89
,
0x1
,
0x4
,
0x5B
,
0xFF
,
0x67
,
0xEF
,
0xDA
,
0xFF
,
0xD7
,
0xD2
,
0xD6
};
uint8_t
pubkey
[
48
]
=
{
0xC3
,
0xFB
,
0xBD
,
0xF3
,
0x31
,
0x6E
,
0x12
,
0x63
,
0x6A
,
0x5F
,
0x2
,
0xAB
,
0x18
,
0xF0
,
0xF3
,
0x43
,
0xB3
,
0x18
,
0xA2
,
0x6D
,
0x60
,
0x28
,
0xC4
,
0x5F
,
0x33
,
0x5E
,
0x4D
,
0xEC
,
0x99
,
0xA6
,
0xF
,
0x42
,
0xFE
,
0x55
,
0xD6
,
0x2C
,
0x78
,
0x8A
,
0x4C
,
0x22
,
0xCC
,
0x17
,
0xF2
,
0x6E
,
0x4B
,
0xC4
,
0xFD
,
0x22
};
void
dump
(
char
*
text
,
uint8_t
*
d
)
{
int
i
;
p
(
"%-20s"
,
text
);
for
(
i
=
0
;
i
<
NUM_ECC_DIGITS
;
++
i
)
p
(
"%02x "
,
d
[
NUM_ECC_DIGITS
-
i
-
1
]);
Serial
.
print
(
"
\n
"
);
}
uint8_t
remotePubkey
[
48
]
=
{
0x27
,
0x8C
,
0x41
,
0x2C
,
0x1F
,
0xF2
,
0xA9
,
0xCB
,
0x78
,
0xC4
,
0x1E
,
0xBB
,
0x2B
,
0x32
,
0x32
,
0x34
,
0xC3
,
0x5B
,
0xD1
,
0x87
,
0x52
,
0x6C
,
0xBD
,
0x7F
,
0x44
,
0x73
,
0xDC
,
0xF0
,
0xFC
,
0x93
,
0x97
,
0x99
,
0x46
,
0x16
,
0xE0
,
0x8F
,
0x65
,
0xA4
,
0xCB
,
0x65
,
0x59
,
0xA8
,
0xBF
,
0xFD
,
0xB4
,
0x61
,
0x23
,
0xA8
};
uint8_t
*
generateIV
()
{
...
...
@@ -82,67 +60,88 @@ char* decryptAES(uint8_t* p_secret, char* p_data, const aes_context ctx)
Serial
.
println
(
"done"
);
return
p_data
;
}
/*
// calculate shared secret using remote public key and local private key
// returns the shared secret
uint8_t
calcSharedSecret
(
EccPoin
t
*
p_pubkey
,
uint8_t
p_privkey
[
NUM_ECC_DIGITS
]
)
uint8_t
*
calcSharedSecret(
uint8_
t *p_pubkey, uint8_t
*
p_privkey
, uECC_Curve curve
)
{
Serial
.
println
(
"Calculating shared secret..."
);
uint8_t
secret
[
NUM_ECC_DIGITS
];
ecdh_shared_secret
(
secret
,
p_pubkey
,
p_privkey
,
NULL
);
//dump("secret", secret);
return
*
secret
;
Serial.print("Calculating shared secret...");
uint8_t secret[24];
unsigned long a = millis();
int r = uECC_shared_secret(p_pubkey, p_privkey, secret, curve);
unsigned long b = millis();
Serial.print("Shared secret 1 in "); Serial.println(b-a);
if (!r) {
Serial.print("shared_secret() failed (1)\n");
return 0;
}
return secret;
}
*/
void
setup
()
{
Serial
.
begin
(
9600
);
// Debugging only
Serial
.
println
(
"
het werk
t"
);
Serial
.
println
(
"
TX ini
t"
);
if
(
!
manager
.
init
())
Serial
.
println
(
"init failed"
);
}
void
sendPubkey
()
{
char
*
msg
=
"pubkey"
;
uint8_t
pubkey
[
NUM_ECC_DIGITS
*
2
];
for
(
uint8_t
i
=
0
;
i
<
NUM_ECC_DIGITS
;
i
++
){
pubkey
[
i
]
=
localPubkey
.
x
[
i
];
}
for
(
uint8_t
i
=
NUM_ECC_DIGITS
;
i
<
NUM_ECC_DIGITS
*
2
;
i
++
){
pubkey
[
i
]
=
localPubkey
.
y
[
i
-
NUM_ECC_DIGITS
];
}
manager
.
setHeaderId
(
50
);
manager
.
sendto
((
uint8_t
*
)
pubkey
,
48
,
2
);
manager
.
waitPacketSent
();
Serial
.
println
(
"sending public key..."
);
}
void
sendMessage
()
void
sendMessage
(
char
*
msg
)
{
Serial
.
println
(
"Sending message..."
);
char
*
msg
=
"hallo"
;
//
char* msg = "hallo";
manager
.
setHeaderId
(
51
);
manager
.
sendto
((
uint8_t
*
)
msg
,
48
,
2
);
//
manager.sendto((uint8_t *)msg, 48, 2);
manager
.
waitPacketSent
();
}
void
hashSecret
(
uint8_t
*
p_secret
)
{
Serial
.
println
(
"Secret:"
);
for
(
int
i
=
0
;
i
<
NUM_ECC_DIGITS
;
i
++
){
Serial
.
print
(
p_secret
[
i
]);
Serial
.
print
(
" "
);
}
Serial
.
println
(
""
);
}
void
loop
()
{
// crypto vars //
uint8_t
sharedSecret
;
char
*
data
=
"Hallo"
;
char
*
encryptedData
;
uint8_t
iv
[
16
]
=
{
4
,
9
,
4
,
9
,
4
,
9
,
4
,
9
,
4
,
9
,
4
,
9
,
4
,
9
,
4
,
9
};
uint8_t
sharedSecret
[
24
];
const
struct
uECC_Curve_t
*
curve
=
uECC_secp192r1
();
// char* data = "Hallo";
// char* encryptedData;
// uint8_t iv[16] = {4,9,4,9,4,9,4,9,4,9,4,9,4,9,4,9};
sendPubkey
();
sharedSecret
=
calcSharedSecret
(
&
remotePubkey
,
privkey
);
ctx
=
aes128_cbc_enc_start
(
&
sharedSecret
,
iv
);
if
(
uECC_valid_public_key
(
remotePubkey
,
curve
)
==
1
){
Serial
.
println
(
"Valid pubkey"
);
uECC_shared_secret
(
remotePubkey
,
privkey
,
sharedSecret
,
curve
);
hashSecret
(
sharedSecret
);
}
else
Serial
.
println
(
"Invalid pubkey"
);
// ctx = aes128_cbc_enc_start(&sharedSecret, iv);
// encryptedData = encryptAES(&sharedSecret, data, ctx);
delay
(
10000
);
sendMessage
();
delay
(
5000
);
//
// delay(10000);
// sendMessage(data);
// delay(5000);
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment