exth_crypto v0.1.6 ExthCrypto.ECIES

Defines ECIES, as it pertains to Ethereum.

This is derived primarily from SEC 1: Elliptic Curve Cryptography

Link to this section Summary

Link to this section Functions

Link to this function decrypt(my_static_private_key, ecies_encoded_msg, shared_info_1 \\ <<>>, shared_info_2 \\ <<>>)
decrypt(ExthCrypto.Key.private_key(), binary(), binary(), binary()) ::
  {:ok, ExthCrypto.Cipher.plaintext()} | {:error, String.t()}

Decrypts a message according to ECIES specification.

ECIES Decrypt (performed by recipient):

  1) generate shared-secret = kdf( ecdhAgree(myPrivKey, msg[1:65]) )
  2) verify tag
  3) decrypt

  ecdhAgree(r, recipientPublic) == ecdhAgree(recipientPrivate, R)
  [where R = r*G, and recipientPublic = recipientPrivate*G]

Examples

iex> ecies_encoded_msg = "049871eb081567823267592abac8ec9e9fddfdece7901a15f233b53f304d7860686c21601ba1a7f56680e22d0ac03eccd08e496469514c25ae1d5e55f391c1956f0102030405060708090a0b0c0d0e0f10a6c88ba08a258e9e5b5124997ee1b502570f933d4fc0b48cef5a504749e4eac1a56f3211de" |> ExthCrypto.Math.hex_to_bin
iex> ExthCrypto.ECIES.decrypt(ExthCrypto.Test.private_key(:key_a), ecies_encoded_msg, "shared_info_1", "shared_info_2")
{:ok, "hello"}
Link to this function encrypt(her_static_public_key, message, shared_info_1 \\ <<>>, shared_info_2 \\ <<>>, my_ephemeral_key_pair \\ nil, init_vector \\ nil)

Encrypts a message according to ECIES specification.

ECIES Encrypt, where P = recipient public key is:

  1) generate r = random value
  2) generate shared-secret = kdf( ecdhAgree(r, P) )
  3) generate R = rG [same op as generating a public key]
  4) send 0x04 || R || AsymmetricEncrypt(shared-secret, plaintext) || tag

Examples

iex> {:ok, enc} = ExthCrypto.ECIES.encrypt(ExthCrypto.Test.public_key(:key_a), "hello", "shared_info_1", "shared_info_2", ExthCrypto.Test.key_pair(:key_b), ExthCrypto.Test.init_vector)
iex> enc |> ExthCrypto.Math.bin_to_hex
"049871eb081567823267592abac8ec9e9fddfdece7901a15f233b53f304d7860686c21601ba1a7f56680e22d0ac03eccd08e496469514c25ae1d5e55f391c1956f0102030405060708090a0b0c0d0e0f10a6c88ba08a258e9e5b5124997ee1b502570f933d4fc0b48cef5a504749e4eac1a56f3211de"

# Test overhead is exactly 113 bytes
iex> msg = "The quick brown fox jumped over the lazy dog."
iex> {:ok, enc} = ExthCrypto.ECIES.encrypt(ExthCrypto.Test.public_key(:key_a), msg, "shared_info_1", "shared_info_2", ExthCrypto.Test.key_pair(:key_b), ExthCrypto.Test.init_vector)
iex> byte_size(enc) - byte_size(msg)
113

# TODO: More tests