Skip to content

Commit

Permalink
fix: add length of the payload ciphertext.
Browse files Browse the repository at this point in the history
  • Loading branch information
PromiseFru committed Oct 22, 2024
1 parent ef38d1e commit a32c7e4
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 17 deletions.
22 changes: 19 additions & 3 deletions content_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,36 @@ def decode_content(content: str) -> tuple:
if content_switch == "0":
len_public_key = struct.unpack("<i", payload[1:5])[0]
result = {"public_key": payload[5 : 5 + len_public_key]}

elif content_switch == "1":
len_auth_code = struct.unpack("<i", payload[1:5])[0]
auth_code = payload[5 : 5 + len_auth_code].decode("utf-8")
bridge_letter = chr(payload[5 + len_auth_code])

len_ciphertext = struct.unpack(
"<i", payload[6 + len_auth_code : 10 + len_auth_code]
)[0]
ciphertext_start = 10 + len_auth_code
ciphertext_end = ciphertext_start + len_ciphertext
content_ciphertext = payload[ciphertext_start:ciphertext_end]

result = {
"auth_code": auth_code,
"bridge_letter": bridge_letter,
"content_ciphertext": payload[6 + len_auth_code :],
"content_ciphertext": content_ciphertext,
}

elif content_switch == "2":
bridge_letter = chr(payload[1])

len_ciphertext = struct.unpack("<i", payload[2:6])[0]
content_ciphertext = payload[6 : 6 + len_ciphertext]

result = {
"bridge_letter": chr(payload[1]),
"content_ciphertext": payload[2:],
"bridge_letter": bridge_letter,
"content_ciphertext": content_ciphertext,
}

else:
raise ValueError(
f"Invalid starting byte value: {content_switch}. Expected one of '0' (public key), "
Expand Down
53 changes: 39 additions & 14 deletions docs/specifications.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,27 +51,40 @@ The authentication code payload is structured as follows:
- **Length of Authentication Code**: A 4-byte integer representing the length of the authentication code.
- **Authentication Code**: The authentication code data.
- **Bridge Letter**: A single byte representing the bridge letter.
- **Length of Ciphertext**: A 4-byte integer representing the length of the ciphertext.
- **Ciphertext**: The encrypted content (variable length).

### Example Layout

```
+----------------+-------------------------------+---------------------+-----------------+-----------------+
| Content Switch | Length of Authentication Code | Authentication Code | Bridge Letter | Ciphertext |
| (1 byte) | (4 bytes, integer) | (variable size) | (1 byte) | (variable size) |
+----------------+-------------------------------+---------------------+-----------------+-----------------+
+----------------+-------------------------------+---------------------+-----------------+----------------------------+----------------------------+
| Content Switch | Length of Authentication Code | Authentication Code | Bridge Letter | Length of Ciphertext | Ciphertext |
| (1 byte) | (4 bytes, integer) | (variable size) | (1 byte) | (4 bytes, integer) | (variable size) |
+----------------+-------------------------------+---------------------+-----------------+----------------------------+----------------------------+
```

#### Example Encoding

```python
import struct
import base64

content_switch = b"1"
auth_code = b"123456" # Example authentication code
bl = b"e" # Example bridge letter
bl = b"e" # Example bridge letter
enc_content = b"Hello world!" # Example content to encrypt

auth_code_data = content_switch + struct.pack("<i", len(auth_code)) + auth_code + bl + enc_content
auth_code_data = (
content_switch +
struct.pack("<i", len(auth_code)) + # Length of authentication code
auth_code +
bl +
struct.pack("<i", len(enc_content)) + # Length of ciphertext
enc_content
)

auth_code_payload = base64.b64encode(auth_code_data).decode("utf-8")
print(auth_code_payload)
```

### 1.3 Encrypted Content Only Payload (No Auth Code)
Expand All @@ -81,23 +94,35 @@ The encrypted content only payload is structured as follows:
- **Content Switch**: A single byte indicating the type of payload.
- Value: `2` (indicates that the payload contains only ciphertext)
- **Bridge Letter**: A single byte representing the bridge letter.
- **Length of Ciphertext**: A 4-byte integer representing the length of the ciphertext.
- **Ciphertext**: The encrypted content (variable length).

### Example Layout

```
+------------------+-----------------+-----------------------------------+
| Content Switch | Bridge Letter | Ciphertext (variable size) |
| (1 byte) | (1 byte) | |
+------------------+-----------------+-----------------------------------+
+------------------+-----------------+----------------------------+----------------------------+
| Content Switch | Bridge Letter | Length of Ciphertext | Ciphertext |
| (1 byte) | (1 byte) | (4 bytes, integer) | (variable size) |
+------------------+-----------------+----------------------------+----------------------------+
```

#### Example Encoding

```python
content_switch = b"2"
bl = b"e" # Example bridge letter
enc_content = b"Hello world!" # Example encrypted content
content_data = content_switch + bl + enc_content
import struct
import base64

content_switch = b"2" # Content Switch indicating ciphertext only
bl = b"e" # Example bridge letter
enc_content = b"Hello world!" # Example encrypted content

content_data = (
content_switch +
bl +
struct.pack("<i", len(enc_content)) + # Length of ciphertext (4 bytes, integer)
enc_content
)

enc_content_only_payload = base64.b64encode(content_data).decode("utf-8")
print(enc_content_only_payload)
```

0 comments on commit a32c7e4

Please sign in to comment.