Replace the vanetza-idf port with his current station firmware (microbu-firmware e4e4bd8: main/, components/microbu_v2x, components/vanetza, external/vanetza) and re-apply the MicrOBU additions, each marked "MicrOBU:" in the sources: - V2X_RX (opcode 0x90): every received ITS message, signed or not, through gn_unwrap.c, because the stack drops unsigned and foreign-root traffic. 0x85 is the spec's RAW_FRAME and 0x86 is reserved. The body is built in v2x_rx.hpp. - Unsigned transmission (geonet.c) for security profile 1 or station security 0, from a random MAC per configuration. - Radio polled from STATION_CONFIGURE on; manual country policy, TX power read-back, static RX buffer, short frames ignored. - Battery guard (battery_policy.hpp): deep sleep below 3.30 V with no phone on USB. - No USB writes without a host; last_frame_ms() for the guard. - 80 KB NVS with the app at 0x20000, 8 MB flash, XIAO ESP32-C5 board, 20 dBm. Remove the old C firmware, the bench listener and the old vanetza-idf copy. Add docs/station-link.tex as revision 2 of the interface reference (V2X_RX, unsigned sending, battery rule, revision history). Add 39 host unit tests (make firmware in test/host) next to the existing chain, replay, fuzz and battery tests, and point tools/station_status_watch.py at the new CONFIRM-based status.
126 lines
5.3 KiB
C++
126 lines
5.3 KiB
C++
#pragma once
|
|
#include <vanetza/common/byte_buffer.hpp>
|
|
#include <vanetza/security/backend.hpp>
|
|
#include <vanetza/security/key_type.hpp>
|
|
#include <vanetza/security/private_key.hpp>
|
|
#include <vanetza/security/public_key.hpp>
|
|
#include <vanetza/security/signature.hpp>
|
|
#include <cstdint>
|
|
#include <map>
|
|
#include <optional>
|
|
|
|
/** The station's security module: the only place that holds private keys.
|
|
*
|
|
* Everything that the micrOBU hardware puts into its secure element (SE051 on the
|
|
* prototype mainboard) or a later V2X HSM is behind SecurityModule: generation
|
|
* and import of key pairs, keeping them across resets, and the ECDSA signature
|
|
* over a digest (IEEE Std 1609.2 clause 5.3.1). Callers hold a KeyId, never a
|
|
* private key value; the operations follow the key management and asymmetric
|
|
* signature functions of the PSA Certified Crypto API (psa_generate_key,
|
|
* psa_import_key, psa_export_public_key, psa_sign_hash, psa_destroy_key), which
|
|
* a secure element integrates as an opaque key location.
|
|
*
|
|
* Operations on public data stay with vanetza::security::Backend on the
|
|
* application processor: hashing, verification and point decompression.
|
|
* Ephemeral ECIES keys and the AES-CCM session key of a TS 102 941 request
|
|
* (pki.hpp) are short-lived and stay there too.
|
|
*/
|
|
namespace microbu_v2x::security {
|
|
|
|
using vanetza::ByteBuffer;
|
|
using vanetza::security::KeyType;
|
|
using vanetza::security::PrivateKey;
|
|
using vanetza::security::PublicKey;
|
|
using vanetza::security::Signature;
|
|
|
|
/// A key held by a SecurityModule. A stored KeyId stays valid across resets until the key is destroyed.
|
|
struct KeyId {
|
|
std::uint32_t value = 0; // 0 is no key
|
|
explicit operator bool() const { return value != 0; }
|
|
friend bool operator==(KeyId a, KeyId b) { return a.value == b.value; }
|
|
friend bool operator!=(KeyId a, KeyId b) { return a.value != b.value; }
|
|
friend bool operator<(KeyId a, KeyId b) { return a.value < b.value; }
|
|
};
|
|
|
|
/// A key pair outside a security module: an authority's key in the issuing tools, an ephemeral ECIES key, or a
|
|
/// laboratory ticket key on its way into the module. Public key uncompressed.
|
|
struct KeyPair {
|
|
PrivateKey priv;
|
|
PublicKey pub;
|
|
};
|
|
|
|
class SecurityModule {
|
|
public:
|
|
virtual ~SecurityModule() = default;
|
|
/// New key pair on the curve, kept in the module; throws std::runtime_error when the module cannot create it.
|
|
virtual KeyId generate_key(KeyType) = 0;
|
|
/// Laboratory provisioning of a key pair created outside the station (VCR1 bundle, issuing tools). None when
|
|
/// the two halves do not belong together; throws std::runtime_error when the module cannot store the key.
|
|
virtual std::optional<KeyId> import_key(const KeyPair&) = 0;
|
|
/// The public half (uncompressed), none for a KeyId the module does not hold.
|
|
virtual std::optional<PublicKey> public_key(KeyId) const = 0;
|
|
/// ECDSA with the curve of the key over a digest of the curve's length; throws std::runtime_error for a KeyId
|
|
/// the module does not hold or a failed operation.
|
|
virtual Signature sign_digest(KeyId, const ByteBuffer& digest) = 0;
|
|
/// Removes the key for good; false when the module holds no such key.
|
|
virtual bool destroy_key(KeyId) = 0;
|
|
};
|
|
|
|
/// True when both keys are the same point on the same curve, in compressed or uncompressed form.
|
|
bool same_public_key(const PublicKey&, const PublicKey&);
|
|
|
|
/// Creates key pairs for SoftwareSecurityModule (the platform's random generator).
|
|
class KeyGenerator {
|
|
public:
|
|
virtual ~KeyGenerator() = default;
|
|
virtual KeyPair generate_key(KeyType) = 0;
|
|
};
|
|
|
|
/// Where SoftwareSecurityModule keeps its keys between resets: one blob, written as a whole.
|
|
class KeyStorage {
|
|
public:
|
|
virtual ~KeyStorage() = default;
|
|
/// false when nothing is stored
|
|
virtual bool load(ByteBuffer&) = 0;
|
|
virtual bool save(const ByteBuffer&) = 0;
|
|
};
|
|
|
|
class MemoryKeyStorage final : public KeyStorage {
|
|
public:
|
|
bool load(ByteBuffer& out) override;
|
|
bool save(const ByteBuffer& blob) override;
|
|
private:
|
|
ByteBuffer blob_;
|
|
};
|
|
|
|
/** SecurityModule in software on the application processor: the stand-in for the SE051 until its adapter
|
|
* exists. Signs through a Backend and keeps the keys in a KeyStorage, whose protection at rest is the
|
|
* platform's (NVS encryption on the ESP32-C5). Storage format: "MSM1", next KeyId (4 octets), then per key
|
|
* [KeyId: 4][curve: 1][private scalar][public x][public y], all big-endian. */
|
|
class SoftwareSecurityModule final : public SecurityModule {
|
|
public:
|
|
/// Loads the stored keys; throws std::runtime_error when the stored blob does not decode.
|
|
SoftwareSecurityModule(vanetza::security::Backend&, KeyGenerator&, KeyStorage&);
|
|
~SoftwareSecurityModule() override;
|
|
SoftwareSecurityModule(const SoftwareSecurityModule&) = delete;
|
|
SoftwareSecurityModule& operator=(const SoftwareSecurityModule&) = delete;
|
|
|
|
KeyId generate_key(KeyType) override;
|
|
std::optional<KeyId> import_key(const KeyPair&) override;
|
|
std::optional<PublicKey> public_key(KeyId) const override;
|
|
Signature sign_digest(KeyId, const ByteBuffer& digest) override;
|
|
bool destroy_key(KeyId) override;
|
|
|
|
private:
|
|
KeyId keep(KeyPair);
|
|
void persist();
|
|
|
|
vanetza::security::Backend& backend_;
|
|
KeyGenerator& generator_;
|
|
KeyStorage& storage_;
|
|
std::map<KeyId, KeyPair> keys_;
|
|
std::uint32_t next_ = 1;
|
|
};
|
|
|
|
} // namespace microbu_v2x::security
|