DeterministicESPAsyncWebServer v6.27.1
Zero-allocation, bounded-execution async HTTP server for ESP32
Loading...
Searching...
No Matches
ssh_rsa.cpp File Reference

RSA-SHA2-256/512 host-key signing (stack-only private key, PKCS#1 v1.5). More...

#include "network_drivers/presentation/ssh/crypto/ssh_rsa.h"
#include "network_drivers/presentation/ssh/transport/ssh_keymat.h"
#include <string.h>
#include <Preferences.h>
#include <esp_random.h>
#include <freertos/FreeRTOS.h>
#include <freertos/semphr.h>
#include <mbedtls/md.h>
#include <mbedtls/pk.h>
#include <mbedtls/rsa.h>

Go to the source code of this file.

Classes

struct  SshRsaCtx
 

Functions

int ssh_rsa_load_pubkey (void)
 Load the public portion of the RSA host key into ssh_host_pubkey.
 
int ssh_rsa_sign (const uint8_t *msg, size_t msg_len, SshRsaHash hash, uint8_t sig[SSH_RSA_SIG_BYTES])
 Sign msg using the RSA host key (PKCS#1 v1.5, rsa-sha2-256/512).
 
int ssh_rsa_verify (const uint8_t n_be[SSH_RSA_KEY_BYTES], const uint8_t e_be4[4], const uint8_t *msg, size_t msg_len, const uint8_t *sig, size_t sig_len, SshRsaHash hash)
 Verify an RSA PKCS#1 v1.5 signature (rsa-sha2-256/512) with a public key.
 
int ssh_rsa_encode_pubkey (uint8_t *out, size_t *out_len, size_t out_cap)
 Serialize the RSA public host key into an SSH "ssh-rsa" key blob.
 

Variables

const uint8_t ssh_pkcs1_sha256_digestinfo [SSH_PKCS1_DIGESTINFO_LEN]
 The DER-encoded DigestInfo wrapper for SHA-256.
 
const uint8_t ssh_pkcs1_sha512_digestinfo [SSH_PKCS1_SHA512_DIGESTINFO_LEN]
 The DER-encoded DigestInfo wrapper for SHA-512.
 
SshRsaPubKey ssh_host_pubkey
 Static host public key (BSS). Set by ssh_rsa_load_pubkey().
 

Detailed Description

RSA-SHA2-256/512 host-key signing (stack-only private key, PKCS#1 v1.5).

Definition in file ssh_rsa.cpp.

Function Documentation

◆ ssh_rsa_load_pubkey()

int ssh_rsa_load_pubkey ( void  )

Load the public portion of the RSA host key into ssh_host_pubkey.

Arduino: reads the DER blob from NVS ("ssh_host_key"/"priv_der"), parses n and e via mbedtls, stores them in ssh_host_pubkey.

Native: reads n and e from the test fixture arrays (see ssh_rsa.cpp).

Call once at startup (or after begin()). Must succeed before any SSH connections are accepted.

Returns
0 on success, -1 if the key is absent or malformed.

Definition at line 82 of file ssh_rsa.cpp.

References SshRsaPubKey::e_bytes, SshRsaPubKey::loaded, SshRsaCtx::lock, SshRsaPubKey::n, SshRsaCtx::pk, SshRsaCtx::ready, ssh_host_pubkey, SSH_RSA_KEY_BYTES, and SSH_RSA_KEY_DER_MAX.

Referenced by ssh_rsa_sign().

◆ ssh_rsa_sign()

int ssh_rsa_sign ( const uint8_t *  msg,
size_t  msg_len,
SshRsaHash  hash,
uint8_t  sig[SSH_RSA_SIG_BYTES] 
)

Sign msg using the RSA host key (PKCS#1 v1.5, rsa-sha2-256/512).

The private key is loaded from NVS directly into a stack-local SshRsaPrivKey struct, used, then zeroed before this function returns. The key NEVER touches static or global memory.

On Arduino: delegates to mbedtls_pk_sign() (hardware-accelerated). On native: software SHA-256/512 + hand-built PKCS#1 pad + bn_expmod_group14().

Parameters
[in]msgMessage to sign (typically the exchange hash H, 32 bytes).
[in]msg_lenLength of msg.
[in]hashSignature hash: SHA256 (rsa-sha2-256) or SHA512 (rsa-sha2-512).
[out]sigOutput buffer, must be SSH_RSA_SIG_BYTES (256) bytes.
Returns
0 on success, -1 on failure (NVS read error, RSA error).

Definition at line 147 of file ssh_rsa.cpp.

References SshRsaCtx::lock, SshRsaCtx::pk, SshRsaCtx::ready, SHA512, ssh_rsa_load_pubkey(), SSH_RSA_SIG_BYTES, ssh_sha256(), SSH_SHA256_DIGEST_LEN, ssh_sha512(), and SSH_SHA512_DIGEST_LEN.

◆ ssh_rsa_verify()

int ssh_rsa_verify ( const uint8_t  n_be[SSH_RSA_KEY_BYTES],
const uint8_t  e_be4[4],
const uint8_t *  msg,
size_t  msg_len,
const uint8_t *  sig,
size_t  sig_len,
SshRsaHash  hash 
)

Verify an RSA PKCS#1 v1.5 signature (rsa-sha2-256/512) with a public key.

Used for client publickey authentication (RFC 4252 §7): n_be / e_be4 come from the client-supplied key blob, not the host key. The public exponent is small (typically 65537), so the modular exponentiation s^e mod n is cheap and is performed for real on both platforms (native: schoolbook bignum; Arduino: mbedTLS). The hash is selected by the client's signature algorithm name (rsa-sha2-256 -> SHA256, rsa-sha2-512 -> SHA512), not by the key blob.

Parameters
[in]n_beRSA modulus n, big-endian, 256 bytes.
[in]e_be4RSA public exponent e, big-endian, 4 bytes.
[in]msgSigned message.
[in]msg_lenLength of msg.
[in]sigSignature bytes (256 for RSA-2048).
[in]sig_lenLength of sig.
[in]hashSignature hash: SHA256 (rsa-sha2-256) or SHA512 (rsa-sha2-512).
Returns
0 if the signature is valid, -1 otherwise.

Definition at line 181 of file ssh_rsa.cpp.

References SHA512, SSH_RSA_KEY_BYTES, ssh_sha256(), SSH_SHA256_DIGEST_LEN, ssh_sha512(), and SSH_SHA512_DIGEST_LEN.

◆ ssh_rsa_encode_pubkey()

int ssh_rsa_encode_pubkey ( uint8_t *  out,
size_t *  out_len,
size_t  out_cap 
)

Serialize the RSA public host key into an SSH "ssh-rsa" key blob.

Format (RFC 4253 §6.6, RFC 8332 §3): uint32 len("ssh-rsa") = 7 byte[7] "ssh-rsa" mpint e mpint n

Writes into out; sets *out_len to the number of bytes written. out must be at least SSH_RSA_PUBKEY_BLOB_MAX bytes.

Parameters
[out]outDestination buffer.
[out]out_lenNumber of bytes written.
[in]out_capCapacity of out.
Returns
0 on success, -1 if pubkey not loaded or buffer too small.

Definition at line 576 of file ssh_rsa.cpp.

References SshRsaPubKey::e_bytes, SshRsaPubKey::loaded, SshRsaPubKey::n, ssh_host_pubkey, SSH_RSA_KEY_BYTES, SSH_RSA_PUBKEY_ALG, SSH_RSA_PUBKEY_ALG_LEN, and SSH_RSA_PUBKEY_BLOB_MAX.

Variable Documentation

◆ ssh_pkcs1_sha256_digestinfo

const uint8_t ssh_pkcs1_sha256_digestinfo[SSH_PKCS1_DIGESTINFO_LEN]
Initial value:
= {
0x30, 0x31,
0x30, 0x0d,
0x06, 0x09,
0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01,
0x05, 0x00,
0x04, 0x20
}

The DER-encoded DigestInfo wrapper for SHA-256.

Prepend this to the 32-byte SHA-256 digest to form the 51-byte DigestInfo structure for PKCS#1 v1.5.

Definition at line 17 of file ssh_rsa.cpp.

◆ ssh_pkcs1_sha512_digestinfo

const uint8_t ssh_pkcs1_sha512_digestinfo[SSH_PKCS1_SHA512_DIGESTINFO_LEN]
Initial value:
= {
0x30, 0x51,
0x30, 0x0d,
0x06, 0x09,
0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03,
0x05, 0x00,
0x04, 0x40
}

The DER-encoded DigestInfo wrapper for SHA-512.

Prepend this to the 64-byte SHA-512 digest to form the 83-byte DigestInfo structure for PKCS#1 v1.5.

Definition at line 26 of file ssh_rsa.cpp.

◆ ssh_host_pubkey

SshRsaPubKey ssh_host_pubkey

Static host public key (BSS). Set by ssh_rsa_load_pubkey().

Definition at line 39 of file ssh_rsa.cpp.

Referenced by ssh_rsa_encode_pubkey(), and ssh_rsa_load_pubkey().