DEV Community

Cover image for Understanding SSH Key Pairs: A Developer's Guide
Eugene Zimin
Eugene Zimin

Posted on

Understanding SSH Key Pairs: A Developer's Guide

In today's interconnected development world, secure authentication is not just a luxury—it's a necessity. Whether you're a seasoned DevOps engineer or a junior developer just starting your journey, understanding SSH key pairs is crucial for your daily workflow. They're the unsung heroes that keep our git pushes secure, our server access protected, and our deployments safe from prying eyes.

But let's be honest: SSH keys can be confusing. With terms like "public key infrastructure," "cryptographic algorithms," and "key fingerprints" floating around, it's easy to feel overwhelmed. This guide aims to demystify SSH key pairs, breaking down complex concepts into digestible pieces that will help you make informed decisions about your security setup.

What Are SSH Key Pairs?

SSH key pairs are cryptographic credentials consisting of two parts:

  • A private key that stays on your local machine (keep this secret!)
  • A public key that you can share freely

Think of them as a sophisticated lock and key system. The public key is like a special lock you can give to anyone, while the private key is the unique key that opens only your locks. When you connect to a remote system, it checks if your private key matches the public key it has stored—if they match, you're granted access. This eliminates the need for password-based authentication, which can be vulnerable to brute force attacks and keyloggers.

What makes SSH key pairs particularly powerful is their ability to provide secure authentication without transmitting sensitive information over the network. Your private key never leaves your machine, making it virtually impossible for attackers to intercept your credentials during the authentication process.

Types of SSH Key Pairs and Their Mathematical Foundations

The cryptographic algorithms behind SSH keys represent some of the most elegant applications of number theory and algebraic geometry in modern computing. Let's delve deep into their mathematical foundations and understand how they provide the security we rely on daily.

RSA: The Prime Numbers Guardian

RSA's brilliance lies in the elegant use of prime numbers and modular arithmetic. Let's walk through a simplified but illustrative example of how RSA actually works in practice.

Suppose we want to create a small (insecure, but educational) RSA key:

  1. First, choose two prime numbers:
    p = 61 and q = 53

  2. Calculate n (the modulus):
    n = p × q = 61 × 53 = 3,233

  3. Calculate the totient φ(n):
    φ(n) = (p-1) × (q-1) = 60 × 52 = 3,120

  4. Choose a public exponent e (commonly 65537):
    Let's use e = 17 for this example (must be coprime with φ(n))

  5. Calculate the private exponent d:
    d = e⁻¹ mod φ(n) = 2,753

This gives us:

  • Public key: (n=3,233, e=17)
  • Private key: (n=3,233, d=2,753)

Here's how the encryption works with these numbers:

Message (m) = 123
Encryption: c = m^e mod n
c = 123^17 mod 3,233 = 855

Decryption: m = c^d mod n
m = 855^2,753 mod 3,233 = 123
Enter fullscreen mode Exit fullscreen mode

In real RSA implementations, we use numbers that are typically 2048 or 4096 bits long. To put this in perspective, a 4096-bit number is roughly 1,234 decimal digits long. The security comes from the fact that factoring such large numbers is computationally infeasible with current technology.

Here's how you'd generate such a key in practice:

# Create a 4096-bit RSA key with custom settings
ssh-keygen -t rsa -b 4096 \
  -C "RSA-4096_$(date +%Y%m%d)" \
  -f ~/.ssh/id_rsa_4096 \
  -N "your_secure_passphrase"
Enter fullscreen mode Exit fullscreen mode

Ed25519: Elliptic Curve Elegance

Ed25519 operates on a specialized Edwards curve, defined by the equation:
-x² + y² = 1 - (121665/121666)x²y²

This curve was carefully chosen for several mathematical properties that make it both secure and efficient. Let's break down how a point multiplication works on this curve:

  1. The base point B has coordinates:
x = 15112221349535400772501151409588531511454012693041857206046113283949847762202
y = 46316835694926478169428394003475163141307993866256225615783033603165251855960
Enter fullscreen mode Exit fullscreen mode
  1. A private key is a 256-bit scalar k
  2. The public key is the point k·B (scalar multiplication)

Here's a simplified example of point addition on the curve:

Given two points P1(x₁,y₁) and P2(x₂,y₂):

x₃ = (x₁y₂ + y₁x₂)/(1 + dx₁x₂y₁y₂)
y₃ = (y₁y₂ - ax₁x₂)/(1 - dx₁x₂y₁y₂)

where d = -121665/121666
Enter fullscreen mode Exit fullscreen mode

The actual Ed25519 implementation uses field arithmetic modulo the prime 2²⁵⁵ - 19, chosen for efficient computation on modern 64-bit processors. When you generate an Ed25519 key:

# Generate Ed25519 key with maximum entropy
ssh-keygen -t ed25519 \
  -C "Ed25519_$(hostname)_$(date +%Y%m%d)" \
  -f ~/.ssh/id_ed25519 \
  -N "$(head -c 32 /dev/urandom | base64)"
Enter fullscreen mode Exit fullscreen mode

The real power of Ed25519 comes from its resistance to various implementation attacks:

Signing equation:
R = rB
S = (r + H(R,A,M)a) mod l

where:
r = H(h_b,...,h_2b-1,M)
H = SHA-512
a = private key
A = public key
M = message
l = 2²⁵² + 27742317777372353535851937790883648493
Enter fullscreen mode Exit fullscreen mode

ECDSA: The NIST Curves

ECDSA uses the NIST P-curves, which are defined over prime fields. The P-256 curve is defined by:
y² = x³ - 3x + b

where b = 0x5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B

Let's examine a point multiplication on this curve:

  1. Start with a base point G:
Gx = 0x6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296
Gy = 0x4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5
Enter fullscreen mode Exit fullscreen mode
  1. For a private key k, the public key Q = kG is computed through repeated point doubling and addition:
Point Addition (P + Q):
s = (y₂ - y₁)/(x₂ - x₁) mod p
x₃ = s² - x₁ - x₂ mod p
y₃ = s(x₁ - x₃) - y₁ mod p

Point Doubling (P + P):
s = (3x₁² + a)/(2y₁) mod p
x₃ = s² - 2x₁ mod p
y₃ = s(x₁ - x₃) - y₁ mod p
Enter fullscreen mode Exit fullscreen mode

Generate an ECDSA key using the P-521 curve:

# Generate ECDSA key with P-521 curve
ssh-keygen -t ecdsa -b 521 \
  -C "ECDSA_P521_$(date +%Y%m%d)" \
  -f ~/.ssh/id_ecdsa_521 \
  -N "$(openssl rand -base64 32)"
Enter fullscreen mode Exit fullscreen mode

Extended Security Considerations

The security of these algorithms depends on different hard mathematical problems:

RSA: Integer Factorization Problem (IFP)

Given n = pq, find p and q
Time complexity: O(exp((log n)^(1/3) * (log log n)^(2/3)))
Enter fullscreen mode Exit fullscreen mode

Ed25519: Elliptic Curve Discrete Logarithm Problem (ECDLP)

Given P and Q = kP, find k
Time complexity: O(√n) using Pollard's rho algorithm
Enter fullscreen mode Exit fullscreen mode

ECDSA: Same as Ed25519, but with different curve parameters

Security level comparison:
RSA 3072-bit ≈ ECDSA/Ed25519 256-bit
RSA 15360-bit ≈ ECDSA/Ed25519 512-bit
Enter fullscreen mode Exit fullscreen mode

Quantum Computing Impact

The advent of quantum computers poses different threats to these algorithms. Using Shor's algorithm:

RSA factoring time complexity:
Classical: O(exp((log N)^(1/3) * (log log N)^(2/3)))
Quantum: O((log N)^3)

ECDLP solving time complexity:
Classical: O(√n)
Quantum: O((log n)^3)
Enter fullscreen mode Exit fullscreen mode

This is why post-quantum cryptography is becoming increasingly important, though it's not yet implemented in standard SSH keys.

Making an Informed Choice

The choice between these algorithms goes beyond mathematics—it's about balancing security, compatibility, and performance. Ed25519 represents the future: mathematically elegant, computationally efficient, and designed with modern threats in mind. Its implementation provides consistent security properties across different platforms, making it the ideal choice for new deployments.

For systems requiring broad compatibility, RSA with 4096 bits remains a solid choice. Its mathematical foundation has withstood decades of cryptanalysis, and while it may be computationally more intensive than modern elliptic curve approaches, its security margins are well understood.

When implementing any of these algorithms, the key is to ensure proper entropy during key generation. A strong random number generator is crucial for security, as even the most mathematically secure algorithm can be compromised by poor randomness. Modern systems use hardware random number generators and entropy pools to ensure strong key generation, but it's worth being aware of this critical foundation.

Top comments (0)