private void LoadProgramButton_Click(object sender, RoutedEventArgs e) { var dialog = new OpenFileDialog(); if (dialog.ShowDialog() == true) { try { this.firmware = new MspFirmware(dialog.FileName); this.UpdateFirmwareSource(this.firmware); this.bytesToSend = MspBoot.EncodeProgram(this.firmware); this.loadedFileName.Text = $"{dialog.FileName} ({this.bytesToSend.Length} bytes)"; Console.WriteLine($"Sending: {this.bytesToSend.ToByteString("0x", ",")} Length = {this.bytesToSend.Length}"); this.sendDataAllButton.IsEnabled = (this.bytesToSend != null && this.isConnected); this.sendDataAllButton100.IsEnabled = (this.bytesToSend != null && this.isConnected); this.UpdateEncryptedDataTextBox(); this.UpdateEnryptedKey(); } catch (Exception ex) { BasicMessageBox.Show($"{ex.Message}\n{ex.StackTrace}", "Failed to load program"); } } }
/// Helper function for generating session data for a specific tag private byte[] GetSessionData(Tag tag, bool isObserver) { var payload = MspBoot.EncodeProgram(this.config.Firmware); var signature = GenerateSignature(this.config.SessionKey, payload, tag.Version, this.config.TargetVersion); var observerFlag = isObserver ? (byte)0x01 : (byte)0x00; return(Native.TinyCrypt.AesEncrypt_DefaultIV(tag.DeviceKey, this.config.SessionKey) .Concat(signature) .Concat(new byte[] { this.config.TargetVersion, 0, 0, observerFlag }) .ToArray()); }
/// Attempts to send the encrypted firmware encoded as MSPBoot commands to the target tag private async Task <bool> SendEncryptedFirmware(ushort tag) { var bytes = MspBoot.EncodeProgram(this.config.Firmware); this.progress.Info($"Sending payload to {tag:X04}"); var response = await SendData(this.reader, tag, bytes, this.config.SessionKey, (byte)this.config.BlockSize, this.progress); if (!response.IsSuccess()) { this.progress.Error($"Failed to send data: {response.status}.", tag); return(false); } return(true); }