MCPcopy Create free account
hub / github.com/ElementsProject/lightning / test_hsmtool_secret_decryption

Function test_hsmtool_secret_decryption

tests/test_wallet.py:1311–1358  ·  view source on GitHub ↗

Test that we can encrypt and decrypt hsm_secret using hsmtool

(node_factory)

Source from the content-addressed store, hash-verified

1309
1310@unittest.skipIf(VALGRIND, "It does not play well with prompt and key derivation.")
1311def test_hsmtool_secret_decryption(node_factory):
1312 """Test that we can encrypt and decrypt hsm_secret using hsmtool"""
1313 l1 = node_factory.get_node(start=False) # Don't start the node
1314 password = "test_password\n"
1315 hsm_path = os.path.join(l1.daemon.lightning_dir, TEST_NETWORK, "hsm_secret")
1316
1317 # Write a known 32-byte key to hsm_secret
1318 known_secret = b'\x01' * 32 # 32 bytes of 0x01
1319 with open(hsm_path, 'wb') as f:
1320 f.write(known_secret)
1321
1322 # Read the hsm_secret to verify it's what we expect
1323 with open(hsm_path, 'rb') as f:
1324 content = f.read()
1325 assert content == known_secret, f"Expected {known_secret}, got {content}"
1326 assert len(content) == 32, f"Expected 32 bytes, got {len(content)}"
1327
1328 # Encrypt it using hsmtool
1329 master_fd, slave_fd = os.openpty()
1330 hsmtool = HsmTool(node_factory.directory, "encrypt", hsm_path)
1331 hsmtool.start(stdin=slave_fd)
1332 hsmtool.wait_for_log(r"Enter hsm_secret password:")
1333 write_all(master_fd, password.encode("utf-8"))
1334 hsmtool.wait_for_log(r"Confirm hsm_secret password:")
1335 write_all(master_fd, password.encode("utf-8"))
1336 assert hsmtool.proc.wait(WAIT_TIMEOUT) == 0
1337 hsmtool.is_in_log(r"Successfully encrypted")
1338
1339 # Read the hsm_secret again - it should now be encrypted (73 bytes)
1340 with open(hsm_path, 'rb') as f:
1341 encrypted_content = f.read()
1342 assert len(encrypted_content) == 73, f"Expected 73 bytes after encryption, got {len(encrypted_content)}"
1343 assert encrypted_content != known_secret, "File should be encrypted and different from original"
1344
1345 # Decrypt it using hsmtool
1346 master_fd, slave_fd = os.openpty()
1347 hsmtool = HsmTool(node_factory.directory, "decrypt", hsm_path)
1348 hsmtool.start(stdin=slave_fd)
1349 hsmtool.wait_for_log(r"Enter hsm_secret password:")
1350 write_all(master_fd, password.encode("utf-8"))
1351 assert hsmtool.proc.wait(WAIT_TIMEOUT) == 0
1352 hsmtool.is_in_log(r"Successfully decrypted")
1353
1354 # Read the hsm_secret again - it should now be back to the original 32 bytes
1355 with open(hsm_path, 'rb') as f:
1356 decrypted_content = f.read()
1357 assert decrypted_content == known_secret, f"Expected {known_secret}, got {decrypted_content}"
1358 assert len(decrypted_content) == 32, f"Expected 32 bytes after decryption, got {len(decrypted_content)}"
1359
1360
1361@unittest.skipIf(TEST_NETWORK == 'liquid-regtest', '')

Callers

nothing calls this directly

Calls 11

HsmToolClass · 0.85
joinMethod · 0.80
wait_for_logMethod · 0.80
is_in_logMethod · 0.80
write_allFunction · 0.70
get_nodeMethod · 0.45
writeMethod · 0.45
readMethod · 0.45
startMethod · 0.45
encodeMethod · 0.45
waitMethod · 0.45

Tested by

no test coverage detected