Key derivation functions are used in cryptography to lengthen and strengthen keys. For example, blockchain private keys are often stored in encrypted files (keystore files) built using key derivation functions. Ethereum Classic (ETC) uses a key derivation function for node communications which I will discuss.

Basics

The ETC key derivation function expands secrets by concatenating several hashes of them combined with various counter values. It is based on NIST (National Institute of Standards and Technology) Special Publications 800–56.

Implementation

A Python ETC key derivation function implementation is provided below. Note that twice the SHA256 hash length is referred to as the SHA256 block size (64 bytes):

#!/usr/bin/env python3 import hashlib BLOCK_SIZE = 64

COUNT_SIZE = 4 def etc_kdf(secret, length):

"""

key derivation function

""" key = b""

for i in range(1, length // BLOCK_SIZE + 2):

count = (i).to_bytes(COUNT_SIZE, "big")

key += hashlib.sha256(count + secret).digest()

key = key[:length] return key

Here is an example calculation in a Python shell:

>>> secret = b"This is the secret." >>> length = 32 >>> etc_kdf(secret, length)

b't\x11F\xe0\xec\xa0\xdd\x98.\xb7\x9c\xd5^\xd4\x83Y\xfe\x9d\xd2X\xebqn(5B\xcaO.R\x888' >>> etc_kdf(secret, length).hex()

'741146e0eca0dd982eb79cd55ed48359fe9dd258eb716e283542ca4f2e528838'

Feedback

Feel free to leave any comments or questions below. You can also contact me by email at cs@etcplanet.org or by clicking any of these icons:

Acknowledgements

I would like to thank IOHK (Input Output Hong Kong) for funding this effort.

License

This work is licensed under the Creative Commons Attribution ShareAlike 4.0 International License.