title: Shared key generation from EC with pycryptodome
body: <p>I'm curently working on a project were i need to compute an hkdf symetric key.
  To do that i need to generate a shared secret from the private key and an ephemeral public key.</p>
  <p>For the rest of my work i did use <a href="https://pycryptodome-master.readthedocs.io/en/latest/index.html" rel="nofollow noreferrer">pycryptodome</a> but i can't find in the doc if it allow generation of shared secret. I saw in the futur plan their intention to add <a href="https://pycryptodome.readthedocs.io/en/latest/src/future.html" rel="nofollow noreferrer">Elliptic Curves (ECIES, ECDH)</a> since ecdh is based on shared key it wouldn't be suprising if shared key generation is not implemented yet.</p>
  <p>I tried using the cryptography lib too but impossible to load my ephemeral key.</p>
  <pre class="lang-py prettyprint-override"><code>from cryptography.hazmat.primitives import serialization
  from cryptography.hazmat.primitives.asymmetric import ec
  
  def __compute_shared_secret(ephemeral_public_key: bytearray) -&gt; bytes:
      client_public_key = serialization.load_der_public_key(ephemeral_public_key)
      server_private_key = serialization.load_der_private_key(b&quot;my_private&quot;, password=None)
      shared_secret = server_private_key.exchange(ec.ECDH(), client_public_key)
      return shared_secret
  </code></pre>
  <pre class="lang-sh prettyprint-override"><code>Could not deserialize key data. The data may be in an incorrect format or it may be encrypted with an unsupported algorithm.
  </code></pre>
  <p>ephemeral_public_key is base64 encoded and given by the gpay api.</p>
  <p>i would like to know if i can do it with pycryptodome and if not if using the cryptography lib only for this part is a good idea.</p>