TL; DR —» Where exactly in the Mission Planner source code would I insert encryption/decryption functions from the libsodium-net crypto library?

(With help from a friend) I've already integrated encryption & decryption functions from NaCl into the APM 2.5 source code, so now I need to do the same in the ground control station to be able to communicate with it... my problem: I'm unfamiliar with C#. I'm familiar with enough programming languages to be able to read & understand it, but not enough to know how/where to include the "SecretBox" functions from libsodium-net in the Mission Planner source code to accomplish this.

Here's where I think I should include the encryption & decryption functions (I hope a developer will correct me if I'm wrong):

[For encryption] From the "MAVLink.cs" file in the MissionPlanner-master\Mavlink\ directory, the generatePacket() function [my speculation is in red]

lock (objlock)
{
    byte[] data;
    byte[] key = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f }; //ADDED THIS!
    byte[] nonce = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; //ADDED THIS!
    byte[] ct; //ADDED THIS!

    data = MavlinkUtil.StructureToByteArray(indata);
    ct = SecretBox.Create(data, nonce, key); //ADDED THIS!

//Console.WriteLine(DateTime.Now + " PC Doing req "+ messageType + " " + this.BytesToRead);
    byte[] packet = new byte[data.Length + 6 + 2];

    packet[0] = 254;
    packet[1] = (byte)ct.Length;
    packet[2] = (byte)packetcount;

    packetcount++;

    packet[3] = 255; // this is always 255 - MYGCS
    packet[4] = (byte)MAV_COMPONENT.MAV_COMP_ID_MISSIONPLANNER;
    packet[5] = messageType;

    int i = 6;
    foreach (byte b in ct)
    {
        packet[i] = b;
        i++;
    }

    ushort checksum = MavlinkCRC.crc_calculate(packet, packet[1] + 6); 

    checksum = MavlinkCRC.crc_accumulate(MAVLINK_MESSAGE_CRCS[messageType], checksum);

    byte ck_a = (byte)(checksum & 0xFF); ///< High byte
    byte ck_b = (byte)(checksum >> 8); ///< Low byte

    packet[i] = ck_a;
    i += 1;
    packet[i] = ck_b;
    i += 1;

[For decryption] From the "MavlinkParse.cs" file in the MissionPlanner-master\ExtLibs\Mavlink\ directory, the GenerateMAVLinkPacket() function [my speculation is in red]

public byte[] GenerateMAVLinkPacket(MAVLINK_MSG_ID messageType, object indata)
{
    byte[] data;

    byte[] key = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f }; //ADDED THIS!
    byte[] nonce = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; //ADDED THIS!
    byte[] pt; //ADDED THIS!

    data = MavlinkUtil.StructureToByteArray(indata);

    pt = SecretBox.Open(data, nonce, key); //ADDED THIS!

    byte[] packet = new byte[pt.Length + 6 + 2];

    packet[0] = 254;
    packet[1] = (byte)pt.Length;
    packet[2] = (byte)packetcount;

    packetcount++;

    packet[3] = 255; // this is always 255 - MYGCS
    packet[4] = (byte)MAV_COMPONENT.MAV_COMP_ID_MISSIONPLANNER;
    packet[5] = (byte)messageType;


    int i = 6;
    foreach (byte b in pt)
    {
        packet[i] = b;
        i++;
    }

    ushort checksum = MavlinkCRC.crc_calculate(packet, packet[1] + 6);

    checksum =     MavlinkCRC.crc_accumulate(MAVLINK_MESSAGE_CRCS[(byte)messageType],     checksum);

    byte ck_a = (byte)(checksum & 0xFF); ///< High byte
    byte ck_b = (byte)(checksum >> 8); ///< Low byte

    packet[i] = ck_a;
    i += 1;
    packet[i] = ck_b;
    i += 1;

    return packet;
    }

}

Cany any Mission Planner developers, or anyone familiar with C# please help me?

[Note: I'm aware the MAVLink developers are in the process of developing sMAVLink, but their completion deadline isn't as soon as mine.]

You need to be a member of diydrones to add comments!

Join diydrones

Email me when people reply –

Replies

  • Developer

    the encryption part looks like its in the correct place.

    the decription however is not.

    look in the MP/mavlink/mavlink.cs in function readPacket

This reply was deleted.

Activity