Files
MicrOBU/obu-firmware/components/microbu_v2x/include/microbu_v2x/security_module.hpp
T

126 lines
5.3 KiB
C++
Raw Normal View History

#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