ProtoCore v0.0.1
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
ssh_rsa.h File Reference

SSH RSA host-key layer: NVS-backed host key, host-key signing, and "ssh-rsa" blob encoding. More...

#include "crypto/asymmetric/rsa.h"
#include <stddef.h>
#include <stdint.h>

Go to the source code of this file.

Classes

struct  SshRsaPubKey
 RSA-2048 public host key parameters. Allocated in BSS; contains only n and e. More...
 

Macros

#define SSH_RSA_KEY_DER_MAX   1700
 Maximum DER size for a PKCS#1 RSAPrivateKey with 2048-bit fields.
 
#define SSH_RSA_PUBKEY_ALG   "ssh-rsa"
 Key-blob type string for an RSA host key.
 
#define SSH_RSA_PUBKEY_ALG_LEN   7
 Length of SSH_RSA_PUBKEY_ALG ("ssh-rsa" = 7 bytes).
 
#define SSH_RSA_PUBKEY_BLOB_MAX   (4 + 7 + 4 + 1 + 4 + 4 + 1 + 256)
 Upper bound on the encoded "ssh-rsa" public-key blob (len+alg + mpint e + mpint n).
 

Functions

int pc_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, pc_rsa_hash hash, uint8_t sig[PC_RSA_SIG_BYTES])
 Sign msg with the RSA host key (PKCS#1 v1.5, rsa-sha2-256/512).
 
int ssh_rsa_encode_pubkey (uint8_t *out, size_t *out_len, size_t out_cap)
 Encode ssh_host_pubkey as the RFC 4253 §6.6 "ssh-rsa" public-key blob.
 

Variables

SshRsaPubKey ssh_host_pubkey
 Static host public key (BSS). Set by pc_ssh_rsa_load_pubkey().
 

Detailed Description

SSH RSA host-key layer: NVS-backed host key, host-key signing, and "ssh-rsa" blob encoding.

This is the SSH-specific wrapper around the shared RSA primitive (crypto/rsa): it owns the device's RSA-2048 host key (loaded from NVS on Arduino, a test fixture on native), signs handshake data with it, and serializes the public key into the RFC 4253 / RFC 8332 "ssh-rsa" blob. The protocol-agnostic RSASSA-PKCS1-v1.5 math (verify, software sign, PKCS#1 encoding, modexp) lives in crypto/rsa (pc_rsa_verify / pc_rsa_sign_sw); peers' signatures are verified via that primitive.

═══════════════════════════════════════════════════════════════════════════ SECURITY MODEL - PRIVATE KEY LIFETIME ═══════════════════════════════════════════════════════════════════════════ The RSA-2048 private key MUST NEVER live in static or global memory. On Arduino the parsed key is held in a private mbedtls context for the server lifetime (as an SSH host key normally is) and every sign runs under a mutex; on native the test fixture injects n/d/e and the software sign (pc_rsa_sign_sw) wipes its own temporaries. The signature scheme is PKCS#1 v1.5 (RFC 8017 §8.2), "rsa-sha2-256" / "rsa-sha2-512" (RFC 8332) - only the hash and its DigestInfo OID differ.

NVS: on Arduino the private key is a DER PKCS#1 RSAPrivateKey in namespace "ssh_host_key" / key "priv_der", parsed by mbedtls. On native, the test fixture injects n, e, d as raw 256-byte arrays.

Author
Douglas Quigg (dstroy0)
Date
2026

Definition in file ssh_rsa.h.

Macro Definition Documentation

◆ SSH_RSA_KEY_DER_MAX

#define SSH_RSA_KEY_DER_MAX   1700

Maximum DER size for a PKCS#1 RSAPrivateKey with 2048-bit fields.

Definition at line 38 of file ssh_rsa.h.

◆ SSH_RSA_PUBKEY_ALG

#define SSH_RSA_PUBKEY_ALG   "ssh-rsa"

Key-blob type string for an RSA host key.

Per RFC 8332 §3, the RSA public-key blob always carries the type string "ssh-rsa" - even when the negotiated signature algorithm is "rsa-sha2-256" / "rsa-sha2-512". Only the signature and authentication algorithm-name fields use "rsa-sha2-256"; the key blob format is unchanged from RFC 4253 §6.6.

Definition at line 48 of file ssh_rsa.h.

◆ SSH_RSA_PUBKEY_ALG_LEN

#define SSH_RSA_PUBKEY_ALG_LEN   7

Length of SSH_RSA_PUBKEY_ALG ("ssh-rsa" = 7 bytes).

Definition at line 51 of file ssh_rsa.h.

◆ SSH_RSA_PUBKEY_BLOB_MAX

#define SSH_RSA_PUBKEY_BLOB_MAX   (4 + 7 + 4 + 1 + 4 + 4 + 1 + 256)

Upper bound on the encoded "ssh-rsa" public-key blob (len+alg + mpint e + mpint n).

Definition at line 77 of file ssh_rsa.h.

Function Documentation

◆ pc_ssh_rsa_load_pubkey()

int pc_ssh_rsa_load_pubkey ( void  )

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

Arduino: reads + parses the DER blob from NVS ("ssh_host_key"/"priv_der") and caches the signer context. Native: reads n/e from the test fixture. Call once at startup (single-threaded).

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

Definition at line 58 of file ssh_rsa.cpp.

References SshRsaPubKey::e_bytes, SshRsaPubKey::loaded, SshRsaCtx::lock, SshRsaPubKey::n, PC_RSA_KEY_BYTES, SshRsaCtx::pk, SshRsaCtx::ready, ssh_host_pubkey, 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,
pc_rsa_hash  hash,
uint8_t  sig[PC_RSA_SIG_BYTES] 
)

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

Returns
0 on success, -1 on failure. sig receives PC_RSA_SIG_BYTES big-endian.

Definition at line 126 of file ssh_rsa.cpp.

References SshRsaCtx::lock, PC_RSA_SIG_BYTES, pc_sha256(), PC_SHA256_DIGEST_LEN, pc_sha512(), PC_SHA512_DIGEST_LEN, pc_ssh_rsa_load_pubkey(), SshRsaCtx::pk, SshRsaCtx::ready, and SHA512.

◆ ssh_rsa_encode_pubkey()

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

Encode ssh_host_pubkey as the RFC 4253 §6.6 "ssh-rsa" public-key blob.

Returns
0 on success (writing out_len), -1 if the key is not loaded or out_cap is too small.

Definition at line 230 of file ssh_rsa.cpp.

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

Variable Documentation

◆ ssh_host_pubkey

SshRsaPubKey ssh_host_pubkey
extern

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

Definition at line 29 of file ssh_rsa.cpp.

Referenced by pc_ssh_rsa_load_pubkey(), and ssh_rsa_encode_pubkey().