Module: Eth::Devp2p::Crypto

Extended by:
Crypto
Included in:
Crypto
Defined in:
lib/ethruby/devp2p/crypto.rb

Defined Under Namespace

Classes: ECIESDecryptionError

Constant Summary collapse

ECIES_CIPHER_NAME =
'aes-128-ctr'

Instance Method Summary collapse

Instance Method Details

#ecies_decrypt(data, priv_key, shared_mac_data = '') ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/ethruby/devp2p/crypto.rb', line 40

def ecies_decrypt(data, priv_key, shared_mac_data = '')
  raise ECIESDecryptionError.new('invalid header') if data[0] != "\x04"

  # compute shared_secret
  ephem_raw_pubkey = data[1..65]
  ephem_pubkey = ec_pkey_from_raw(ephem_raw_pubkey)
  shared_secret = priv_key.dh_compute_key(ephem_pubkey.public_key)

  key = ecies_kdf(shared_secret, 32)
  key_enc, key_mac = key[0...16], key[16..-1]

  # verify data
  key_mac = Digest::SHA256.digest(key_mac)
  tag = data[-32..-1]
  # should use secure_compare?
  unless hmac_sha256(key_mac, data[66...-32] + shared_mac_data) == tag
    raise ECIESDecryptionError.new("Fail to verify data")
  end

  # decrypt data
  cipher = OpenSSL::Cipher.new(ECIES_CIPHER_NAME)

  iv_start = 66
  iv_end = iv_start + cipher.iv_len
  iv = data[iv_start...iv_end]
  ciphertext = data[iv_end...-32]

  cipher.decrypt
  cipher.key = key_enc
  cipher.iv = iv
  cipher.update(ciphertext) + cipher.final
end

#ecies_encrypt(message, raw_pubkey, shared_mac_data = '') ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/ethruby/devp2p/crypto.rb', line 17

def ecies_encrypt(message, raw_pubkey, shared_mac_data = '')
  pubkey = raw_pubkey.is_a?(OpenSSL::PKey::EC) ? raw_pubkey : ec_pkey_from_raw(raw_pubkey)

  # compute keys
  ephem_key = OpenSSL::PKey::EC.new('secp256k1')
  ephem_key.generate_key
  shared_secret = ephem_key.dh_compute_key(pubkey.public_key)
  key = ecies_kdf(shared_secret, 32)
  key_enc, key_mac = key[0...16], key[16..-1]

  key_mac = Digest::SHA256.digest(key_mac)
  ephem_raw_pubkey = ephem_key.public_key.to_bn.to_s(2)

  cipher = OpenSSL::Cipher.new(ECIES_CIPHER_NAME)
  cipher.encrypt
  iv = cipher.random_iv
  cipher.key = key_enc
  cipher_text = cipher.update(message) + cipher.final
  msg = "\x04" + ephem_raw_pubkey + iv + cipher_text
  tag = hmac_sha256(key_mac, msg[(1 + 65)..-1] + shared_mac_data)
  msg + tag
end