|
| 1 | +import base64 |
1 | 2 | import datetime |
2 | 3 |
|
3 | 4 | import pytest as pytest |
|
18 | 19 | PersistentVolume, |
19 | 20 | ) |
20 | 21 |
|
21 | | -from aleph.sdk.utils import enum_as_str, get_message_type_value, parse_volume |
| 22 | +from aleph.sdk.types import SEVInfo |
| 23 | +from aleph.sdk.utils import ( |
| 24 | + calculate_firmware_hash, |
| 25 | + compute_confidential_measure, |
| 26 | + enum_as_str, |
| 27 | + get_message_type_value, |
| 28 | + parse_volume, |
| 29 | +) |
22 | 30 |
|
23 | 31 |
|
24 | 32 | def test_get_message_type_value(): |
@@ -174,3 +182,56 @@ def test_parse_persistent_volume(): |
174 | 182 | volume = parse_volume(volume) |
175 | 183 | assert volume |
176 | 184 | assert isinstance(volume, PersistentVolume) |
| 185 | + |
| 186 | + |
| 187 | +def test_calculate_firmware_hash(mocker): |
| 188 | + mock_path = mocker.Mock( |
| 189 | + read_bytes=mocker.Mock(return_value=b"abc"), |
| 190 | + ) |
| 191 | + |
| 192 | + assert ( |
| 193 | + calculate_firmware_hash(mock_path) |
| 194 | + == "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad" |
| 195 | + ) |
| 196 | + |
| 197 | + |
| 198 | +def test_compute_confidential_measure(): |
| 199 | + """Verify that we properly calculate the measurement we use agains the server |
| 200 | +
|
| 201 | + Validated against the sevctl command: |
| 202 | + $ RUST_LOG=trace sevctl measurement build --api-major 01 --api-minor 55 --build-id 24 --policy 1 |
| 203 | + --tik ~/pycharm-aleph-sdk-python/decadecadecadecadecadecadecadecadecadecadecadecadecadecadecadeca_tik.bin |
| 204 | + --firmware /usr/share/ovmf/OVMF.fd --nonce URQNqJAqh/2ep4drjx/XvA |
| 205 | +
|
| 206 | + [2024-07-05T11:19:06Z DEBUG sevctl::measurement] firmware + table len=4194304 sha256: d06471f485c0a61aba5a431ec136b947be56907acf6ed96afb11788ae4525aeb |
| 207 | + [2024-07-05T11:19:06Z DEBUG sevctl::measurement] --tik base64: npOTEc4mtRGfXfB+G6EBdw== |
| 208 | + [2024-07-05T11:19:06Z DEBUG sevctl::measurement] --nonce base64: URQNqJAqh/2ep4drjx/XvA== |
| 209 | + [2024-07-05T11:19:06Z DEBUG sevctl::measurement] Raw measurement: BAE3GAEAAADQZHH0hcCmGrpaQx7BNrlHvlaQes9u2Wr7EXiK5FJa61EUDaiQKof9nqeHa48f17w= |
| 210 | + [2024-07-05T11:19:06Z DEBUG sevctl::measurement] Signed measurement: ls2jv10V3HVShVI/RHCo/a43WO0soLZf0huU9ZZstIw= |
| 211 | + [2024-07-05T11:19:06Z DEBUG sevctl::measurement] Measurement + nonce: ls2jv10V3HVShVI/RHCo/a43WO0soLZf0huU9ZZstIxRFA2okCqH/Z6nh2uPH9e8 |
| 212 | + """ |
| 213 | + |
| 214 | + tik = bytes.fromhex("9e939311ce26b5119f5df07e1ba10177") |
| 215 | + assert base64.b64encode(tik) == b"npOTEc4mtRGfXfB+G6EBdw==" |
| 216 | + expected_hash = "d06471f485c0a61aba5a431ec136b947be56907acf6ed96afb11788ae4525aeb" |
| 217 | + nonce = base64.b64decode("URQNqJAqh/2ep4drjx/XvA==") |
| 218 | + sev_info = SEVInfo.parse_obj( |
| 219 | + { |
| 220 | + "enabled": True, |
| 221 | + "api_major": 1, |
| 222 | + "api_minor": 55, |
| 223 | + "build_id": 24, |
| 224 | + "policy": 1, |
| 225 | + "state": "running", |
| 226 | + "handle": 1, |
| 227 | + } |
| 228 | + ) |
| 229 | + |
| 230 | + assert ( |
| 231 | + base64.b64encode( |
| 232 | + compute_confidential_measure( |
| 233 | + sev_info, tik, expected_hash, nonce=nonce |
| 234 | + ).digest() |
| 235 | + ) |
| 236 | + == b"ls2jv10V3HVShVI/RHCo/a43WO0soLZf0huU9ZZstIw=" |
| 237 | + ) |
0 commit comments