Save the Tristate
Quantum Key Distribution (QKD)

# Description

So it was just another day in Danville when Phineas and Ferb were making a new device to communicate with Meep as he travels across the galaxy. To make a device suitable for galatic communication and secure enough to be safe from alien hackers, they decide to protect their device with QKD! Unfortunately, due to Phineas & Co singing their usual musical numbers about their inventions, Doofenshmirtz has caught wind of this technology and wants to use it to take over the Tristate area, using his brand new Qubit-Disrupt-inator. Naturally I, Major Monogram, have to send you, Perry the Platypus, on a mission to stop Doofenshmirtz from disrupting Phineas and Ferb's qubits with his diabolical inator. So grab your tiny fedora and doo-bee-doo-bee-doo-ba-doo your way over to stop Doofenshmirtz! Mission:
• Receive # of qubits that translate to the flag
• Measure qubits in your own basis
• Monogram tells you how many qubits were measured correctly, but not which ones
• Go back and fix it
• Get it right
nc misc.chal.csaw.io 5001

# Solution

## BB84

To solve this challenge, one has to understand the BB84 protocol.
Alice and Bob want to share a secret key over a potentially insecure channel.
1. 1.
Alice generates N bits and N bases (either `+` or `x`). She encodes the generated bits as qubits in the bases she has chosen.
2. 2.
Bob also randomly chooses N bases. He reads the qubits sent by Alice and measures them using the bases he generated. Note that Bob has a 50% chance of generating the same basis as Alice for each bit - otherwise, Bob will measure the bits wrongly.
3. 3.
Alice and Bob share their basis with each other and get rid of every bit that was measured by different bases.
Note that at this point, Bob and Alice would share a common secret, which is the sequence of bits that were measured correctly with the same basis. They can then verify part of the shared bits with each other.
This method is secure, because if an eavesdropper, Eve, had interfered with the quantum channel, then Alice and Bob will disagree on the "verification bits" and Eve would be unveiled.

## Bruteforcing the Basis

This challenge is a little different, though. The server tells us how many basis we got right, but not which ones. We can simply bruteforce this, though, by making sure that the number of errors remains at 0.
1
from pwn import *
2
from Crypto.Util.number import *
3
β
4
conn = remote("misc.chal.csaw.io", 5001)
5
β
6
length = 1
7
curr = ''
8
prev_checked = '+'
9
β
10
print(conn.recvuntil(b"? \r\n").decode())
11
β
12
while True:
13
conn.sendline((str(length)).encode())
14
print(conn.recvuntil(b": \r\n").decode())
15
β
16
if prev_checked == '+':
17
to_check = 'x'
18
else:
19
to_check = '+'
20
21
prev_checked = to_check
22
β
23
conn.sendline((curr + to_check).encode())
24
β
25
if length != 256:
26
27
else:
28
29
30
31
β
32
33
curr += to_check
34
length += 1
35
β
36
print("Current:", curr)
37
β
38
if length > 256:
39
break
Copied!

## Getting the Key

After we get all 256 basis right, we are given the measured qubits. Note that at this point, we share the same basis as the server, so all of the bits will be used as part of the key. We simply have to decode the bit values, based on the basis of each bit, to get the key.
Adding on to the script above:
1
2
data += conn.recvuntil("What is the key?").decode()
3
print(data)
4
β
5
key = 0
6
for line in data.splitlines():
7
8
# 0 + 1i
9
if line.startswith('0.0'):
10
key *= 2
11
key += 1
12
13
# 1 + 0i
14
elif line.startswith('1.0'):
15
key *= 2
16
key += 0
17
18
# 0.707 + 0.707i
19
elif line.startswith('0.707'):
20
key *= 2
21
key += 0
22
23
# -0.707 + 0.707i
24
elif line.startswith('-0.707'):
25
key *= 2
26
key += 1
27
β
28
elif line.startswith("What is the key?"):
29
break
30
β
31
else:
32
pass
33
β
34
print(bin(key))
35
print(str(key))
36
β
37
conn.interactive()
Copied!
It turns out the key is a string. Submit the key to get the flag!
1
>>> long_to_bytes(43931681853956549478184553086187289692451455473923533011002686733443258084897)
2
b'a semi-aquatic mammal of action!'
Copied!
The flag is `flag{MO0O0O0O0M PH1NE4S & F3RB R T4LK1NG 2 AL1ENS 0V3R QKD!!!}`