## 3 Secure FSO Link in GNU Radio Simulation
### Transmitter  
Once the initialisation stages are complete, the transmitter can send secured messages. There are six main stages that the transmitter goes through to prepare the message to be sent through free space, as depicted in Figure 4.  
The first stage is for the user to enter a message into a UI entry box. Next, the message is passed to the custom message handler and the UI entry box is cleared; this message handler is coded specifically for this system. The message handler prepares the message format for encryption, clears the UI entry box, prints to the terminal, and stores the message in a log.  
Therefore, GNU Radio passes asynchronous messages using streams. When the data represent a message type (shown by blocks with grey inputs/outputs), the transferred data will be a polymorphic type (PMT). PMTs are commonly used for asynchronous communications due to their flexibility. The incoming message is a PMT and it is converted to a PMT vector of two elements. The first element is the tag'msg_clear'; this is needed  
Figure 3: Secure FSO Initialisation blocks in GNU Radio.  
as the encryption block searches for PMT vectors with the tag'msg_clear' so that it can encrypt the second element of the vector, which contains the u8 vector data of the message and is coupled with the metadata of its length. The message must be in this format for the encryption blocks to recognise the data and perform accurate encryption. The message is also stored in a log file and written to the terminal as part of the UI to debug the data flow.  
Now the message is ready to be encrypted. The libsodium library provides a high-level application programming interface (API) for encryption that will produce an authenticated and encrypted message in one function. The function used by gr-nacl public encryption is crypto_box_easy, which takes a message, a random nonce, the sender's secret key and the recipient's public key to produce an encrypted message bundled with an authentication tag which ensures the message has not been tampered during transmission. The type of encryption used is called Xsalsa20 and the authentication used is Poly1305 [7].  
The message is passed through another custom formatter block to change the message type to a PMT construct, containing the nonce and packet in u8 vector form. Simultaneously the message is saved to the log and printed to the terminal by the formatter and subsequent simulation log block. Next, a 32-bit CRC is calculated and appended to the end of the message, which further ensures the integrity of the message at the receiver. One more custom formatter block applies a custom packet format that wraps the payload with four bytes of pre-amble, a four-byte access code, and finally four header bytes. For this work, the preamble is equal to 0 \(\times\) 55 0 \(\times\) 55 0 \(\times\) 55 0 \(\times\) 55 in hex or 85 85 85 85 in decimal and the access code is 0 \(\times\) E1 0 \(\times\) 5A 0 \(\times\) E8 0 \(\times\) 93 in hex or 225 90 232 147 in decimal. The final message configuration is ready to be sent to the transmitter for modulation, so once again it is saved to the log and written to the terminal. (Note that a message strobe block is also added here to keep the simulation active and push messages through certain buffers).  
This message is passed to the frequency shift keying (FSK) packet transmit a hierarchical block of the gr-control out-of-tree (OOT) [26] module but has been modified to suit the proposed system. A complex voltage control oscillator (VCO) is used and a fractional resampler has been added to control the speed of the simulation by changing the resampling radio.  
Figure 4: Secure FSO link Transmitter blocks in GNU Radio.  
Now that the data have been modulated, they can be transmitted through the channel. Constellation, frequency, and time sinks are added after modulation as GUI elements to help visualise the signal and measure signal strength.