Where would I insert encryption & decryption functions in the MP code?

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.]

Views: 743

Reply to This

Replies to This Discussion

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

Excellent! Thank you!

So, I think I'm making the correct code modifications... before I jump to using libsodium-net I thought I should try a simple XOR "encryption" to make sure I'm doing the right thing in the right place...

Could you verify that I am using the correct syntax in the correct location to accomplish this?

—In the "MAVLink.cs" at around line 546(ish) I added a for loop in the generatePacket() function to XOR every byte in the payload, where "secret = 0xA5" (defined as a global variable at the top, just below

"internal string plaintxtline = ""; 

string buildplaintxtline = ""; ")  :

— then at around line 1759(ish) I added another for loop in the GetPacket() function to XOR every byte in the payload again with the globally defined "secret = 0xA5":

If these are in the correct place (and syntax) to accomplish an XOR of the payloads before transmission & upon receipt, then my problem may be in the APM code (for which I hope Tridge will help! but in case you can, I've included those changes as well):

In the "_mav_finalize_message_chan_send()" function of the "mavlink_helpers.h" file, I inserted a for loop to XOR each byte of the payload with 0xA5:

— and in the "else" portion of the switch:case found in "mavlink_parse_char()" function, I inserted a for loop to XOR each byte of the payload with 0xA5:

Again, the XORing of the payload is just a trivial "encryption" to verify that I am doing the right thing in the right place... if this succeeds, then I SHOULD be able to use the crypto_secretbox() function from NaCl to perform the acutal encryption. As of now, these implementations are not working, so I think I'm misunderstanding the code in some way (probably the datatype of payload64).

So, Am I doing this the right way in the right place?

I would do the decryption in readpacket() otherwise you will need to modify multiple places in code.

look over readpacket in mavink.cs you will need to decrypt it before all the packet checks are done. around line 2306.

I took your advice and inserted the for loop to XOR the payload right where you suggested... I'm pretty sure it SHOULD work; I think the reason the Mission Planner (with these changes) won't connect to the APM has to do with my failure to implement the same changes in the APM code (for which I'm awaiting feedback from Tridge to see what I'm doing wrong there).

Would you mind taking a quick look at my version of MAVLink.cs to verify that my changes should result in the message payload being XORd "in flight"? The encryption is at line 544, and the decryption is at line 2313. I think these changes should achieve the goal (it compiles), but so far I've had no luck establishing a connection with the APM.

Thanks!!!

in generate packet, the crc is done on the encrypted packet. I don't know is this is how you want it.

in readpacket, to follow on with above, you need to move your decrpt packet to after the checksum check. so move it to line 2392 ish

Thank you! That's progress... as you can see in the following screenshot, the heartbeat is detected because it's trying to download the parameters.

but it just hangs there for a minute until the "Connect Failed" dialog box pops up. When I click on "details" it provides the following explanation:

In case you can't read it in the image, it says:

Timeout on read - getParamList

828 Packets where received, but no paramater packets where received

at MissionPlanner.MAVLinkInterface.getParamListBG() in c:\...\MissionPlanner\Mavlink\MAVLink.cs:line 863

at MissionPlanner.MAVLinkInterface.OpenBg(Object PRsender, Boolean getparams, ProgressWorkerEventArgs progressWorkerEventArgs) in c:\...\MissionPlanner\Mavlink\MAVLink.cs:line 472

at MissionPlanner.MAVLinkInterface.FrmProgressReportsDoWorkAndParams(Object sender, ProgressWorkerEventArgs e, Object passdata) in c:\...\MissionPlanner\Mavlink\MAVLink.cs:line 298

at MissionPlanner.Controls.ProgressReporterDialogue.RunBackgroundOperation(Object o) in c:\...\MissionPlanner\ExtLibs\Controls\ProgressReporterDialogue.cs:line 87

Again, the problem is probably in my erroneous XOR implementation in the APM code, and probably not something I've done in the Mission Planner code... but if you notice something, or have any other suggestions, I'd greatly appreciate your feedback!!

Reply to Discussion

RSS

© 2019   Created by Chris Anderson.   Powered by

Badges  |  Report an Issue  |  Terms of Service