6

I noticed that the GO standard library has some really nice functions for performing AES encryption / decryption in various modes. However, I couldn't (yet) find anything for Diffie-Hellman key exchange.

I'm not sure if I should proceed by searching more carefully, switching to a different language with a more extensive crypto library, or trying to implement the key exchange protocol myself.

The protocol "seems" implementable:

  • Alice sends $g^a \mod p$ to Bob
  • Bob sends $g^b \mod p$ to Alice
  • We need to choose $g, a, b, p$ appropriately

But I'd bet that there are a lot of subtleties buried in that "straightforward protocol" (for example, we need some out-of-band mechanism to defend against man in the middle attacks).

Plus, one of the first lessons of crypto is don't implement cryptographic primitives yourself, and I don't know if this warrants an exception to that lesson.

Are developers generally expected to implement Diffie-Hellman key exchange themselves? If so, what are the primitives that I should be using, and where can I find a good reference for some of the subtleties that should be considered?

Mike Edward Moras
  • 18,161
  • 12
  • 87
  • 240
Elliott
  • 1,711
  • 3
  • 15
  • 9

2 Answers2

2

Look for a 'Key Agreement' functionality (see https://golang.org/src/crypto/tls/key_agreement.go), as many comments above suggested - if you're trying to establish a secure communication channel, the default TLS (SSL) would be good way to go.

gusto2
  • 1,194
  • 7
  • 14
1

To find a free implementation of a simple Diffie Hellman can be offered by HEAPS of libraries such as: OpenSSL, NaCl/LibSodium etc etc.

Also many languages offer it in the default apis for example Node.js: https://nodejs.org/api/crypto.html

If a language does not support it by default and its ecosystem does not offer a library from 3rd party you can always do some C/C++ bondage (in order words some bindings) with libraries such as OpenSSL, NaCl/LibSodium etc etc. Just wrap it nicely in your own exposed pass-through api wrapping the libraries functionalities. That approach is recomended for missing or low level functionalities such as group key agreements or a sustom authentication/agreement protocol.