|
DeterministicESPAsyncWebServer v6.27.1
Zero-allocation, bounded-execution async HTTP server for ESP32
|
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(). | |
RSA-SHA2-256/512 host-key signing (stack-only private key, PKCS#1 v1.5).
Definition in file ssh_rsa.cpp.
| 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.
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().
| 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().
| [in] | msg | Message to sign (typically the exchange hash H, 32 bytes). |
| [in] | msg_len | Length of msg. |
| [in] | hash | Signature hash: SHA256 (rsa-sha2-256) or SHA512 (rsa-sha2-512). |
| [out] | sig | Output buffer, must be SSH_RSA_SIG_BYTES (256) bytes. |
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.
| 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.
| [in] | n_be | RSA modulus n, big-endian, 256 bytes. |
| [in] | e_be4 | RSA public exponent e, big-endian, 4 bytes. |
| [in] | msg | Signed message. |
| [in] | msg_len | Length of msg. |
| [in] | sig | Signature bytes (256 for RSA-2048). |
| [in] | sig_len | Length of sig. |
| [in] | hash | Signature hash: SHA256 (rsa-sha2-256) or SHA512 (rsa-sha2-512). |
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.
| 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.
| [out] | out | Destination buffer. |
| [out] | out_len | Number of bytes written. |
| [in] | out_cap | Capacity of out. |
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.
| const uint8_t ssh_pkcs1_sha256_digestinfo[SSH_PKCS1_DIGESTINFO_LEN] |
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.
| const uint8_t ssh_pkcs1_sha512_digestinfo[SSH_PKCS1_SHA512_DIGESTINFO_LEN] |
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.
| 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().