Blame doc/curve25519-sha256@libssh.org.txt

Packit 6c0a39
curve25519-sha256@libssh.org.txt        Aris Adamantiadis <aris@badcode.be>
Packit 6c0a39
                                                                  21/9/2013
Packit 6c0a39
Packit 6c0a39
1. Introduction
Packit 6c0a39
Packit 6c0a39
This document describes the key exchange methode curve25519-sha256@libssh.org
Packit 6c0a39
for SSH version 2 protocol. It is provided as an alternative to the existing
Packit 6c0a39
key exchange mechanisms based on either Diffie-Hellman or Elliptic Curve Diffie-
Packit 6c0a39
Hellman [RFC5656].
Packit 6c0a39
The reason is the following : During summer of 2013, revelations from ex-
Packit 6c0a39
consultant at NSA Edward Snowden gave proof that NSA willingly inserts backdoors
Packit 6c0a39
into softwares, hardware components and published standards. While it is still
Packit 6c0a39
believed that the mathematics behind ECC cryptography are still sound and solid,
Packit 6c0a39
some people (including Bruce Schneier [SCHNEIER]), showed their lack of confidence
Packit 6c0a39
in NIST-published curves such as nistp256, nistp384, nistp521, for which constant
Packit 6c0a39
parameters (including the generator point) are defined without explanation. It
Packit 6c0a39
is also believed that NSA had a word to say in their definition. These curves
Packit 6c0a39
are not the most secure or fastest possible for their key sizes [DJB], and
Packit 6c0a39
researchers think it is possible that NSA have ways of cracking NIST curves.
Packit 6c0a39
It is also interesting to note that SSH belongs to the list of protocols the NSA
Packit 6c0a39
claims to be able to eavesdrop. Having a secure replacement would make passive
Packit 6c0a39
attacks much harder if such a backdoor exists.
Packit 6c0a39
Packit 6c0a39
However an alternative exists in the form of Curve25519. This algorithm has been
Packit 6c0a39
proposed in 2006 by DJB [Curve25519]. Its main strengths are its speed, its
Packit 6c0a39
constant-time run time (and resistance against side-channel attacks), and its
Packit 6c0a39
lack of nebulous hard-coded constants.
Packit 6c0a39
Packit 6c0a39
The reference version being used in this document is the one described in
Packit 6c0a39
[Curve25519] as implemented in the library NaCl [NaCl].
Packit 6c0a39
This document does not attempt to provide alternatives to the ecdsa-sha1-*
Packit 6c0a39
authentication keys.
Packit 6c0a39
Packit 6c0a39
2. Key exchange
Packit 6c0a39
Packit 6c0a39
The key exchange procedure is very similar to the one described chapter 4 of
Packit 6c0a39
[RFC5656]. Public ephemeral keys are transmitted over SSH encapsulated into
Packit 6c0a39
standard SSH strings.
Packit 6c0a39
Packit 6c0a39
The following is an overview of the key exchange process:
Packit 6c0a39
Packit 6c0a39
Client                                                            Server
Packit 6c0a39
------                                                            ------
Packit 6c0a39
Generate ephemeral key pair.
Packit 6c0a39
SSH_MSG_KEX_ECDH_INIT          -------->                      
Packit 6c0a39
                                            Verify that client public key 
Packit 6c0a39
                                            length is 32 bytes.
Packit 6c0a39
                                             Generate ephemeral key pair.
Packit 6c0a39
                                                   Compute shared secret.
Packit 6c0a39
                                         Generate and sign exchange hash.
Packit 6c0a39
                               <--------           SSH_MSG_KEX_ECDH_REPLY
Packit 6c0a39
Verify that server public key length is 32 bytes.
Packit 6c0a39
* Verify host keys belong to server.
Packit 6c0a39
Compute shared secret.
Packit 6c0a39
Generate exchange hash.
Packit 6c0a39
Verify server's signature.
Packit 6c0a39
Packit 6c0a39
*   Optional but strongly recommanded as this protects against MITM attacks.
Packit 6c0a39
Packit 6c0a39
This is implemented using the same messages as described in RFC5656 chapter 4
Packit 6c0a39
Packit 6c0a39
3. Method Name
Packit 6c0a39
Packit 6c0a39
The name of this key exchange method is "curve25519-sha256@libssh.org".
Packit 6c0a39
Packit 6c0a39
4. Implementation considerations
Packit 6c0a39
Packit 6c0a39
The whole method is based on the curve25519 scalar multiplication. In this
Packit 6c0a39
method, a private key is a scalar of 256 bits, and a public key is a point
Packit 6c0a39
of 256 bits.
Packit 6c0a39
Packit 6c0a39
4.1. Private key generation
Packit 6c0a39
Packit 6c0a39
A 32 bytes private key should be generated for each new connection,
Packit 6c0a39
 using a secure PRNG. The following actions must be done on the private key:
Packit 6c0a39
     mysecret[0] &= 248;
Packit 6c0a39
     mysecret[31] &= 127;
Packit 6c0a39
     mysecret[31] |= 64;
Packit 6c0a39
In order to keep the key valid. However, many cryptographic libraries will do
Packit 6c0a39
this automatically.
Packit 6c0a39
It should be noted that, in opposition to NIST curves, no special validation
Packit 6c0a39
should be done to ensure the result is a valid and secure private key.
Packit 6c0a39
Packit 6c0a39
4.2 Public key generation
Packit 6c0a39
Packit 6c0a39
The 32 bytes public key of either a client or a server must be generated using
Packit 6c0a39
the 32 bytes private key and a common generator base. This base is defined as 9
Packit 6c0a39
followed by all zeroes:
Packit 6c0a39
     const unsigned char basepoint[32] = {9};
Packit 6c0a39
Packit 6c0a39
The public key is calculated using the cryptographic scalar multiplication:
Packit 6c0a39
     const unsigned char privkey[32];
Packit 6c0a39
     unsigned char pubkey[32];
Packit 6c0a39
     crypto_scalarmult (pubkey, privkey, basepoint);
Packit 6c0a39
However some cryptographic libraries may provide a combined function:
Packit 6c0a39
     crypto_scalarmult_base (pubkey, privkey);
Packit 6c0a39
Packit 6c0a39
It should be noted that, in opposition to NIST curves, no special validation
Packit 6c0a39
should be done to ensure the received public keys are valid curves point. The
Packit 6c0a39
Curve25519 algorithm ensure that every possible public key maps to a valid
Packit 6c0a39
ECC Point.
Packit 6c0a39
Packit 6c0a39
4.3 Shared secret generation
Packit 6c0a39
Packit 6c0a39
The shared secret, k, is defined in SSH specifications to be a big integer.
Packit 6c0a39
This number is calculated using the following procedure:
Packit 6c0a39
Packit 6c0a39
     X is the 32 bytes point obtained by the scalar multiplication of the other
Packit 6c0a39
     side's public key and the local private key scalar.
Packit 6c0a39
Packit 6c0a39
     The whole 32 bytes of the number X are then converted into a big integer k.
Packit 6c0a39
     This conversion follows the network byte order. This step differs from 
Packit 6c0a39
     RFC5656.
Packit 6c0a39
Packit Service fcc0d2
[RFC5656]    https://tools.ietf.org/html/rfc5656
Packit 6c0a39
[SCHNEIER]   https://www.schneier.com/blog/archives/2013/09/the_nsa_is_brea.html#c1675929
Packit Service fcc0d2
[DJB]        https://cr.yp.to/talks/2013.05.31/slides-dan+tanja-20130531-4x3.pdf
Packit 6c0a39
[Curve25519] "Curve25519: new Diffie-Hellman speed records."
Packit Service fcc0d2
             https://cr.yp.to/ecdh/curve25519-20060209.pdf