Commit 5331afd0 authored by yids's avatar yids
Browse files

basic functionality, it calculates a shared secret, encrypts and decrypts a...

basic functionality, it calculates a shared secret, encrypts and decrypts a string with it and print it out to serial.
parent 54d2af80
Pipeline #3 skipped
......@@ -14,13 +14,13 @@
RH_ASK driver;
//my pub-priv keypair
uint8_t private_0[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 public_0 = {
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}};
// other pub key
EccPoint public_1 = {
EccPoint remotePubkey = {
{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}};
......@@ -34,24 +34,41 @@ void p(char *fmt, ... ){
}
void dump(char *text, uint8_t *d) {
int i;
int i ;
p("%-20s", text);
for (i = 0; i < NUM_ECC_DIGITS; ++i)
p("%02x ", d[NUM_ECC_DIGITS-i-1]);
Serial.print("\n");
}
void decryptAES(uint8_t secret_0, uint8_t* data)
char* encryptAES(uint8_t* p_secret, char* p_data)
{
// aes128_dec_single(secret_0, data);
Serial.print("\n");
Serial.println("encoding data...");
Serial.println(p_data);
aes128_enc_single(p_secret, p_data);
Serial.println("done");
return p_data;
}
char* decryptAES(uint8_t* p_secret, char* p_data)
{
Serial.print("\n");
Serial.println("decoding data...");
aes128_dec_single(p_secret, p_data);
Serial.println("done");
return p_data;
}
void shareSecret()
// calculate shared secret using remote public key and local private key
// returns the shared secret
uint8_t calcSharedSecret(EccPoint *p_pubkey, uint8_t p_privkey[NUM_ECC_DIGITS])
{
uint8_t secret_0[NUM_ECC_DIGITS] = { 0, };
ecdh_shared_secret(secret_0, &public_1, private_0, NULL);
dump("secret", secret_0);
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;
}
void setup()
......@@ -64,21 +81,30 @@ void setup()
void loop()
{
shareSecret();
uint8_t buf[RH_ASK_MAX_MESSAGE_LEN];
uint8_t buflen = sizeof(buf);
// Crypto //
uint8_t sharedSecret = calcSharedSecret(&remotePubkey, privkey);
uint8_t key[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
//char data[16];
char data[] = "0123456789012345";
char* encodedData;
char* decodedData;
encodedData = encryptAES(&sharedSecret, data);
Serial.print("encoded data:");
Serial.println(encodedData);
decodedData = decryptAES(&sharedSecret, encodedData);
Serial.print("decoded data:");
Serial.println(decodedData);
// Radio //
uint8_t buf[RH_ASK_MAX_MESSAGE_LEN];
uint8_t buflen = sizeof(buf);
if (driver.recv(buf, &buflen)) // Non-blocking
{
int i;
// Message with a good checksum received, dump it.
driver.printBuffer("Got encrypted:", buf, buflen);
}
}
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