|
DeterministicESPAsyncWebServer v6.27.1
Zero-allocation, bounded-execution async HTTP server for ESP32
|
2048-bit Montgomery modular exponentiation for DH-group14. More...
#include "network_drivers/presentation/ssh/crypto/ssh_bignum.h"#include "network_drivers/presentation/ssh/transport/ssh_keymat.h"#include <string.h>#include <mbedtls/bignum.h>Go to the source code of this file.
Functions | |
| void | bn_from_bytes (SshBigNum *out, const uint8_t *bytes, size_t len) |
Read a big-endian byte array of len bytes into a SshBigNum. | |
| void | bn_to_bytes (uint8_t bytes[256], const SshBigNum *in) |
| Write a SshBigNum as a 256-byte big-endian array. | |
| int | bn_cmp (const SshBigNum *a, const SshBigNum *b) |
| Compare two SshBigNum values. | |
| int | bn_is_zero (const SshBigNum *a) |
Return non-zero if a is zero (all limbs zero). | |
| int | bn_dh_validate (const SshBigNum *v) |
| Validate a received DH public value. | |
| void | bn_expmod_group14 (SshBigNum *out, const SshBigNum *base, const SshBigNum *exp) |
| Compute out = base^exp mod group14_p. | |
Variables | |
| uint8_t | crypto_work [SSH_CRYPTO_WORK_SIZE] |
| Global scratch buffer for big-number temporaries. | |
| const SshBigNum | group14_p |
| The RFC 3526 MODP group-14 prime (2048-bit). | |
| const SshBigNum | group14_g |
| Generator for group-14: g = 2. | |
2048-bit Montgomery modular exponentiation for DH-group14.
─── Montgomery parameter for group-14 ─────────────────────────────────── The group-14 prime p ends in ...FFFFFFFF FFFFFFFF (little-endian d[0]=d[1]= 0xFFFFFFFF). The Montgomery parameter:
p_inv = (-(p mod 2^32))^(-1) mod 2^32
p mod 2^32 = 0xFFFFFFFF -(0xFFFFFFFF) mod 2^32 = 0x00000001 0x00000001^(-1) mod 2^32 = 1
So p_inv = 1 for the group-14 prime. In the SOS reduction pass: m_i = t[i] * p_inv mod 2^32 = t[i].
─── R² mod p ──────────────────────────────────────────────────────────── R = 2^2048. R² mod p = 2^4096 mod p. It is computed once at startup by bn_init() via 4096 doublings mod p, and stored in the static s_g14.r2 constant. ─────────────────────────────────────────────────────────────────────────
Definition in file ssh_bignum.cpp.
| void bn_from_bytes | ( | SshBigNum * | out, |
| const uint8_t * | bytes, | ||
| size_t | len | ||
| ) |
Read a big-endian byte array of len bytes into a SshBigNum.
If len < 256 the most-significant limbs are zeroed. If len > 256 only the least-significant 256 bytes are read.
| out | Destination bignum. |
| bytes | Big-endian source bytes. |
| len | Number of source bytes (typically 256 for 2048-bit). |
Definition at line 210 of file ssh_bignum.cpp.
References SshBigNum::d.
Referenced by bn_expmod_group14(), and ssh_kexdh_handle().
| void bn_to_bytes | ( | uint8_t | bytes[256], |
| const SshBigNum * | in | ||
| ) |
Write a SshBigNum as a 256-byte big-endian array.
| bytes | Destination buffer (exactly 256 bytes). |
| in | Source bignum. |
Definition at line 222 of file ssh_bignum.cpp.
References SshBigNum::d, and SSH_BN_LIMBS.
Referenced by bn_expmod_group14(), and ssh_kexdh_handle().
Compare two SshBigNum values.
Definition at line 234 of file ssh_bignum.cpp.
References SshBigNum::d, and SSH_BN_LIMBS.
Referenced by bn_dh_validate().
| int bn_is_zero | ( | const SshBigNum * | a | ) |
Return non-zero if a is zero (all limbs zero).
Definition at line 239 of file ssh_bignum.cpp.
References SshBigNum::d, and SSH_BN_LIMBS.
| int bn_dh_validate | ( | const SshBigNum * | v | ) |
Validate a received DH public value.
RFC 4253 §8: the received value e (or f) must satisfy 1 < e < p-1. Returns 0 if the value is valid, -1 otherwise.
| v | Received public DH value. |
Definition at line 247 of file ssh_bignum.cpp.
References bn_cmp(), SshBigNum::d, group14_p, and SSH_BN_LIMBS.
Referenced by ssh_kexdh_handle().
Compute out = base^exp mod group14_p.
Uses Montgomery modular exponentiation with left-to-right binary scan. Uses crypto_work[] for all temporaries; zeros crypto_work[] on exit.
On Arduino the computation is delegated to mbedtls_mpi_exp_mod() which uses hardware multiplication and blinding.
| out | Result (base^exp mod p, 2048-bit). |
| base | Base value; must satisfy 1 < base < p-1. |
| exp | Exponent (e.g. the 2048-bit private DH scalar y). |
Definition at line 276 of file ssh_bignum.cpp.
References bn_from_bytes(), bn_to_bytes(), and group14_p.
Referenced by ssh_dh_generate(), and ssh_kexdh_handle().
| uint8_t crypto_work[SSH_CRYPTO_WORK_SIZE] |
Global scratch buffer for big-number temporaries.
Only one SSH KEX or RSA-sign operation runs at a time (single Arduino loop task, synchronous handshake). Zeroed after every use via ssh_wipe().
See the layout comment in the file header for the exact field map.
Definition at line 36 of file ssh_bignum.cpp.
| const SshBigNum group14_p |
The RFC 3526 MODP group-14 prime (2048-bit).
Definition at line 43 of file ssh_bignum.cpp.
Referenced by bn_dh_validate(), and bn_expmod_group14().
| const SshBigNum group14_g |
Generator for group-14: g = 2.
Definition at line 55 of file ssh_bignum.cpp.
Referenced by ssh_dh_generate().