CDC Badge OS
Firmware for the CDC Badge v1.0 hardware security key
Loading...
Searching...
No Matches
ecdh.cpp File Reference
#include "ecdh.h"
#include <mbedtls/ecdh.h>
#include <mbedtls/ecp.h>
#include <mbedtls/ctr_drbg.h>
#include <mbedtls/entropy.h>
#include <mbedtls/platform_util.h>
#include <esp_random.h>
#include <string.h>

Go to the source code of this file.

Macros

#define ECP_POINT_X(P)
 MbedTLS 3.x/4.x compatibility macros for ECP point field access.
#define ECP_POINT_Y(P)
#define ECP_POINT_Z(P)

Functions

static int hw_random (void *ctx, unsigned char *buf, size_t len)
 Hardware RNG callback used by MbedTLS.
void ecdh_secure_clear (void *ptr, size_t size)
 Securely clears sensitive memory using platform zeroize.
bool ecdh_p256_compute_shared_secret (uint8_t *privkey, const uint8_t *peer_pubkey, uint8_t *shared_out)
 Computes ECDH shared secret on P-256 using local private key and peer public key.
bool ecdh_p256_generate_keypair (uint8_t *privkey_out, uint8_t *pubkey_out)
bool ecdh_p256_derive_pubkey (const uint8_t *privkey, uint8_t *pubkey_out)

Macro Definition Documentation

◆ ECP_POINT_X

#define ECP_POINT_X ( P)
Value:
(P).X

MbedTLS 3.x/4.x compatibility macros for ECP point field access.

ECDH P-256 Implementation for OpenPGP PSO:DECIPHER

SECURITY NOTE: The TROPIC01 secure element does NOT support native ECDH operations. This module provides software-based ECDH using MbedTLS with the private key temporarily loaded from encrypted R-Memory storage.

See docs/GPG_ECDH_SECURITY.md for security analysis and trade-offs.

Definition at line 29 of file ecdh.cpp.

Referenced by ecdh_p256_compute_shared_secret(), ecdh_p256_derive_pubkey(), and ecdh_p256_generate_keypair().

◆ ECP_POINT_Y

#define ECP_POINT_Y ( P)
Value:
(P).Y

Definition at line 30 of file ecdh.cpp.

Referenced by ecdh_p256_compute_shared_secret(), ecdh_p256_derive_pubkey(), and ecdh_p256_generate_keypair().

◆ ECP_POINT_Z

#define ECP_POINT_Z ( P)
Value:
(P).Z

Definition at line 31 of file ecdh.cpp.

Referenced by ecdh_p256_compute_shared_secret().

Function Documentation

◆ ecdh_p256_compute_shared_secret()

bool ecdh_p256_compute_shared_secret ( uint8_t * privkey,
const uint8_t * peer_pubkey,
uint8_t * shared_out )

Computes ECDH shared secret on P-256 using local private key and peer public key.

Parameters
privkeyIn/out private key buffer (cleared on completion/failure).
peer_pubkeyPeer uncompressed public key (0x04 || X || Y).
shared_outOutput 32-byte shared secret (X coordinate).
Returns
true if shared secret derivation succeeded.

Definition at line 63 of file ecdh.cpp.

References ecdh_secure_clear(), ECP_POINT_X, ECP_POINT_Y, ECP_POINT_Z, and hw_random().

Referenced by cmd_pso_decipher().

◆ ecdh_p256_derive_pubkey()

bool ecdh_p256_derive_pubkey ( const uint8_t * privkey,
uint8_t * pubkey_out )

Derive public key from private key

Parameters
privkey32-byte private key
pubkey_out65-byte output for public key (04 || X || Y)
Returns
true on success

Definition at line 214 of file ecdh.cpp.

References ECP_POINT_X, ECP_POINT_Y, and hw_random().

Referenced by read_public_key().

◆ ecdh_p256_generate_keypair()

bool ecdh_p256_generate_keypair ( uint8_t * privkey_out,
uint8_t * pubkey_out )

Generate ephemeral P-256 key pair for ECDH

Parameters
privkey_out32-byte output for private key
pubkey_out65-byte output for public key (04 || X || Y)
Returns
true on success

Definition at line 153 of file ecdh.cpp.

References ecdh_secure_clear(), ECP_POINT_X, ECP_POINT_Y, and hw_random().

Referenced by generate_dec_key(), and gpg_generate_key().

◆ ecdh_secure_clear()

void ecdh_secure_clear ( void * ptr,
size_t size )

Securely clears sensitive memory using platform zeroize.

Parameters
ptrMemory region start.
sizeNumber of bytes to clear.

Definition at line 52 of file ecdh.cpp.

Referenced by ecdh_p256_compute_shared_secret(), and ecdh_p256_generate_keypair().

◆ hw_random()

int hw_random ( void * ctx,
unsigned char * buf,
size_t len )
static

Hardware RNG callback used by MbedTLS.

Parameters
ctxUnused callback context.
bufOutput buffer to fill with random bytes.
lenNumber of random bytes requested.
Returns
0 on success.

Definition at line 41 of file ecdh.cpp.

Referenced by ecdh_p256_compute_shared_secret(), ecdh_p256_derive_pubkey(), and ecdh_p256_generate_keypair().