ethereum / eth-keyfile

Tools for handling the encrypted keyfile format used to store private keys.
MIT License
79 stars 49 forks source link

Add zeros to iv #28

Closed valar999 closed 1 year ago

valar999 commented 4 years ago

What was wrong?

Parity does not accept iv with hex length != 32. It throws this error:

2020-02-19 17:49:37 UTC Invalid key file: "/opt/parity/keys/ethereum/keyfile" (Error("Invalid cipher params", line: 1, column: 149))
Consensus signer account not found for the current chain. You can create an account via RPC, UI or `parity account new --chain chain.json --keys-path /opt/parity/keys`.

How was it fixed?

hex_iv.zfill(32) to make iv length always correct

Cute Animal Picture

![Cute animal picture]()

valar999 commented 4 years ago

May be this way would be better?

--- a/eth_keyfile/keyfile.py
+++ b/eth_keyfile/keyfile.py
@@ -128,9 +128,9 @@ def _create_v3_keyfile_json(private_key, password, kdf,
     else:
         raise NotImplementedError("KDF not implemented: {0}".format(kdf))

-    iv = big_endian_to_int(Random.get_random_bytes(16))
+    iv = Random.get_random_bytes(16)
     encrypt_key = derived_key[:16]
-    ciphertext = encrypt_aes_ctr(private_key, encrypt_key, iv)
+    ciphertext = encrypt_aes_ctr(private_key, encrypt_key, big_endian_to_int(iv))
     mac = keccak(derived_key[16:32] + ciphertext)

     address = keys.PrivateKey(private_key).public_key.to_address()
@@ -140,7 +140,7 @@ def _create_v3_keyfile_json(private_key, password, kdf,
         'crypto': {
             'cipher': 'aes-128-ctr',
             'cipherparams': {
-                'iv': encode_hex_no_prefix(int_to_big_endian(iv)).zfill(32),
+                'iv': encode_hex_no_prefix(iv),
             },
             'ciphertext': encode_hex_no_prefix(ciphertext),
             'kdf': kdf,