# How Does SSH Work

The SSH protocol is a method to secure the transmission of information over an insecure network.

The following sections briefly describe the different stages of an SSH session and the various 
encryption technologies being used at each stage. There are many sources available for more 
elaborated descriptions, such as [Understanding the SSH Encryption and Connection Process][1].

## SSH Session Stages and Encryption Used

There are three main stages involved in an SSH session and each stage uses a different encryption 
technology as follows:

- Session Encryption Negotiation Stage
- User Authentication Stage
- Session Communication Stage

### Session Encryption Negotiation Stage

At this initial stage, the two parties agree on the exact encryption protocol and version to be used.
Then the client verifies the identity of the host by checking the public host key provided by the 
server. Both parties then negotiate a **session key** that will be used with **symmetric encryption** 
of the information transferred during the next stages of the session.

The negotiation of the session key is based on temporary public and private key pairs with an 
**asymmetric encryption** method. These keys are generated by both parties during this stage as part 
of the algorithm for session key generation. These keys are **not** the public and private key pairs 
used for the User Authentication stage that is described in the next section. 

The Diffie-Hellman (or a related) algorithm is used to generate the session key. The two parties 
exchange their generated temporary public keys. The receiving entities uses their own temporary 
private key, the other party's temporary public key, and a shared prime number to **independently** 
compute a session key. The session key generation algorithm guaranties that the resulting session 
keys are the same. The session key is also called **shared secret key**, although it is never 
transferred between the parties. Note that the resultant key is never disclosed to any third party.

The established session key is specific to each SSH session, and is generated prior to client 
authentication. Once the key has been generated, all packets moving between the two machines must be 
encrypted by the session key. This includes the password typed into the console by the user, so 
credentials are always protected from network packet sniffers.

### User Authentication Stage

Depending on the server, the client authenticates using his credentials by transferring them to the 
server using the negotiated session key (the one that was generated on the first stage).

Alternatively, the client authenticates using public and private SSH key pair with an asymmetric 
encryption method. The public key is used to encrypt data that can only be decrypted with the 
private key. The public key can be freely shared, because, although it can encrypt for the private 
key, there is no method of deriving the private key from the public key.

The authentication process that is based on SSH key pairs relies on the fact that the public key of
the client has already been registered by the server. During the authentication, the client sends to
the server an ID for the key pair it would like to authenticate with. The server uses this ID to 
fetch the respective public key from its `authorized_keys` file.

The server generates a random number, encrypts it with the public key of the client, and sends it to 
the client as a challenge. The client decrypts the message with the private key to reveal the number 
sent by the server. The client then sends back to the server the hash of the combination of the 
revealed number with the session key. The server independently calculates a hash of the number sent 
to the client combined with the agreed session key. The server then compares the result of its own 
calculation to the hash that the client sent back.

### Session Communication Stage

This stage uses the agreed session key with a symmetric encryption method. Symmetrical encryption is 
a type of encryption where one key can be used to encrypt messages to the opposite party, and also 
to decrypt the messages received from the other participant. This means that anyone who holds the 
key can encrypt and decrypt messages to anyone else holding the key.

Note that the asymmetric encryption method with the permanent public/private key pair is used only
for authentication, not for the encrypting the information exchange.

This type of encryption technology is often called "shared secret" encryption, or "secret key" 
encryption.

## Summary

The below table summarizes the various encryption methods used during the different stages of an SSH 
session.

Stage | Stage Purpose          | Encryption Method Used
:----:|:-----------------------|:-------------------------------------------------------------------
   1  | Encryption Negotiation | asymmetric with temporary public and private key pairs
   2  | User Authentication    | asymmetric with a permanent key pair, symmetric with session key, and hash
   3  | Session Communication  | symmetric with session key

---

[1]: https://www.digitalocean.com/community/tutorials/understanding-the-ssh-encryption-and-connection-process