The answersDownload the PDF
Worksheet

A7.10 Project: a secure sensor packet

Data representation · A level · OCR H446 1.3.1, AQA 7517 4.5.4.4, Eduqas A500QS 2.3 · about 40 min

BugBotLab
NameClassDate

What this lesson is about

Pack a distance reading into fixed point bytes with flags and a checksum, encrypt it with a one-time pad and send it by radio.

Questions 6 marks in all

  1. [1 mark]A byte stores a distance in unsigned fixed point with 7 bits before the binary point and 1 bit after. Give the 8 bits for 36.5 cm.

  2. [1 mark]For that format (unsigned, 7 bits before the point and 1 bit after), what are the range and precision?

    1. A0 to 127.5, in steps of 0.5
    2. B0 to 255, in steps of 1
    3. C-64 to 63.5, in steps of 0.5
    4. D0 to 127, in steps of 0.1
  3. [1 mark]Which operation tests whether bit 0 of the flags byte is set?

    1. AAND with 00000001, then check the result is not zero
    2. BOR with 00000001
    3. CXOR with 00000001
    4. DShift left by 1
  4. [1 mark]What does this program print?

    packet = [0x44, 0x10, 0x81]
    check = sum(packet) % 256
    print(format(check, "02X"))
  5. [1 mark]What is the denary value of 01000100 XOR 00111010?

  6. [1 mark]The receiver decrypts the packet. Why should it check the checksum before using the reading?

    1. AInterference may have changed bits, and a wrong reading could make the robot act wrongly
    2. BDecryption always introduces errors
    3. CThe checksum contains the key
    4. DThe reading cannot be read until the checksum is removed

The task: a secure sensor packet

The robot faces a wall. PAD is a list of four whole numbers from 0 to 255. 1. Read the sensor once with distance(), a reading in cm (a float, from 0 to 127.5 on this mat). 2. Build the four packet bytes as whole numbers from 0 to 255: the type byte ord("D"); the reading byte, the reading doubled with its fraction dropped (int); the flags byte with bit 7 always set and bit 0 set only when the reading is under 30 cm, built with shifts and OR; and the checksum, the sum of the first three bytes modulo 256. 3. Print packet: followed by the four bytes as two uppercase hex digits each, separated by single spaces. 4. Encrypt the packet by XORing each byte with the PAD byte in the same position, and print encrypted: followed by the four encrypted bytes in the same format. 5. Send PKT followed by the encrypted bytes as eight hex digits with no spaces, built by your program. 6. Now be the receiver, using only the text you sent: turn each pair of hex digits back into a byte, XOR with PAD, and check that the checksum matches and bit 7 of the flags is set. If both are true, print received: <cm> cm, checksum ok, where <cm> is the reading byte divided by 2 (for example 51.0), and set the LED to green. Otherwise print received: checksum failed and set the LED to red. Build every byte from the reading; do not type any of the packet in.

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

PAD = [0x3A, 0x91, 0x5C, 0xE7]

def hex_bytes(data, sep):
    return sep.join(format(b, "02X") for b in data)

d = distance()

Plan your program here, then type it in and press Run.

QR code
Do it on the robot
www.bugbotlab.com/learn/a7-10-project-secure-sensor-packet/
The simulator checks it and tells you when it passes. Nothing to install, no account.

Challenges

  1. A reading of 140 cm does not fit. Clamp it to the largest value the byte can hold and set a new flag, bit 1, to say it was clamped.
  2. Change the format to 6 bits before the point and 2 after. What are the new range and precision, and which suits this mat better?
  3. Flip one bit of the encrypted bytes before the receiver reads them, and show that any single flipped bit is caught. Then find two flipped bits that the checksum misses.