Files
MicrOBU/obu-firmware/components/microbu_v2x/src/security_module.cpp
T

206 lines
7.2 KiB
C++
Raw Normal View History

#include <microbu_v2x/security_module.hpp>
#include <microbu_v2x/ecc.hpp>
#include <vanetza/security/v3/hash.hpp>
#include <algorithm>
#include <stdexcept>
namespace microbu_v2x::security {
using vanetza::security::KeyCompression;
using vanetza::security::key_length;
namespace {
constexpr std::uint8_t magic[4] = {'M', 'S', 'M', '1'};
void wipe(ByteBuffer& secret) {
volatile std::uint8_t* target = secret.data();
for (std::size_t n = secret.size(); n--;) *target++ = 0;
secret.clear();
}
std::uint8_t curve_code(KeyType type) {
switch (type) {
case KeyType::NistP256: return 1;
case KeyType::BrainpoolP256r1: return 2;
case KeyType::BrainpoolP384r1: return 3;
default: return 0;
}
}
KeyType curve_type(std::uint8_t code) {
switch (code) {
case 1: return KeyType::NistP256;
case 2: return KeyType::BrainpoolP256r1;
case 3: return KeyType::BrainpoolP384r1;
default: return KeyType::Unspecified;
}
}
void put_u32(ByteBuffer& out, std::uint32_t value) {
for (int shift = 24; shift >= 0; shift -= 8) out.push_back(static_cast<std::uint8_t>(value >> shift));
}
std::uint32_t get_u32(const ByteBuffer& in, std::size_t at) {
return std::uint32_t(in[at]) << 24 | std::uint32_t(in[at + 1]) << 16 | std::uint32_t(in[at + 2]) << 8 | in[at + 3];
}
bool well_formed(const KeyPair& pair) {
const auto type = pair.priv.type;
const auto n = key_length(type);
return curve_code(type) != 0 && pair.pub.type == type && pair.priv.key.size() == n &&
pair.pub.compression == KeyCompression::NoCompression && pair.pub.x.size() == n && pair.pub.y.size() == n;
}
/// The two halves belong together when a signature with the private key verifies with the public key.
bool halves_match(vanetza::security::Backend& backend, const KeyPair& pair) {
const ByteBuffer probe = backend.calculate_hash(vanetza::security::v3::specified_hash_algorithm(pair.priv.type),
pair.pub.x);
return backend.verify_digest(pair.pub, probe, backend.sign_digest(pair.priv, probe));
}
ByteBuffer encode_keys(std::uint32_t next, const std::map<KeyId, KeyPair>& keys) {
ByteBuffer out(std::begin(magic), std::end(magic));
put_u32(out, next);
for (const auto& [id, pair] : keys) {
put_u32(out, id.value);
out.push_back(curve_code(pair.priv.type));
for (const auto* part : {&pair.priv.key, &pair.pub.x, &pair.pub.y}) out.insert(out.end(), part->begin(), part->end());
}
return out;
}
bool decode_keys(const ByteBuffer& in, std::uint32_t& next, std::map<KeyId, KeyPair>& keys) {
if (in.size() < 8 || !std::equal(std::begin(magic), std::end(magic), in.begin())) return false;
next = get_u32(in, 4);
for (std::size_t at = 8; at < in.size();) {
if (in.size() - at < 5) return false;
const KeyId id {get_u32(in, at)};
const KeyType type = curve_type(in[at + 4]);
at += 5;
const auto n = key_length(type);
if (!id || id.value >= next || type == KeyType::Unspecified || in.size() - at < 3 * n || keys.count(id)) return false;
KeyPair pair;
pair.priv.type = pair.pub.type = type;
pair.pub.compression = KeyCompression::NoCompression;
pair.priv.key.assign(in.begin() + at, in.begin() + at + n);
pair.pub.x.assign(in.begin() + at + n, in.begin() + at + 2 * n);
pair.pub.y.assign(in.begin() + at + 2 * n, in.begin() + at + 3 * n);
at += 3 * n;
keys.emplace(id, std::move(pair));
}
return true;
}
/// x and y of a public key in uncompressed form, none when a compressed point is not on its curve.
std::optional<PublicKey> uncompressed(const PublicKey& key) {
if (key.compression == KeyCompression::NoCompression) return key;
const bool odd = key.compression == KeyCompression::Y1;
if (!odd && key.compression != KeyCompression::Y0) return std::nullopt;
const auto point = ecc::decompress(key.type, key.x, odd);
if (!point) return std::nullopt;
PublicKey out;
out.type = key.type;
out.compression = KeyCompression::NoCompression;
out.x = point->x;
out.y = point->y;
return out;
}
} // namespace
bool same_public_key(const PublicKey& a, const PublicKey& b) {
if (a.type != b.type || a.type == KeyType::Unspecified) return false;
const auto ua = uncompressed(a), ub = uncompressed(b);
return ua && ub && ua->x == ub->x && ua->y == ub->y;
}
bool MemoryKeyStorage::load(ByteBuffer& out) {
if (blob_.empty()) return false;
out = blob_;
return true;
}
bool MemoryKeyStorage::save(const ByteBuffer& blob) {
wipe(blob_);
blob_ = blob;
return true;
}
SoftwareSecurityModule::SoftwareSecurityModule(vanetza::security::Backend& backend, KeyGenerator& generator,
KeyStorage& storage) :
backend_(backend), generator_(generator), storage_(storage) {
ByteBuffer stored;
if (!storage_.load(stored)) return;
const bool decoded = decode_keys(stored, next_, keys_);
wipe(stored);
if (!decoded) throw std::runtime_error("stored security module keys do not decode");
}
SoftwareSecurityModule::~SoftwareSecurityModule() {
for (auto& entry : keys_) wipe(entry.second.priv.key);
}
KeyId SoftwareSecurityModule::generate_key(KeyType type) {
KeyPair pair = generator_.generate_key(type);
if (!well_formed(pair)) throw std::runtime_error("key generation returned no usable key pair");
return keep(std::move(pair));
}
std::optional<KeyId> SoftwareSecurityModule::import_key(const KeyPair& pair) {
const auto pub = uncompressed(pair.pub);
if (!pub) return std::nullopt;
KeyPair copy {pair.priv, *pub};
try {
if (!well_formed(copy) || !halves_match(backend_, copy)) {
wipe(copy.priv.key);
return std::nullopt;
}
} catch (const std::exception&) {
wipe(copy.priv.key);
return std::nullopt;
}
return keep(std::move(copy));
}
std::optional<PublicKey> SoftwareSecurityModule::public_key(KeyId id) const {
const auto found = keys_.find(id);
if (found == keys_.end()) return std::nullopt;
return found->second.pub;
}
Signature SoftwareSecurityModule::sign_digest(KeyId id, const ByteBuffer& digest) {
const auto found = keys_.find(id);
if (found == keys_.end()) throw std::runtime_error("security module holds no such key");
return backend_.sign_digest(found->second.priv, digest);
}
bool SoftwareSecurityModule::destroy_key(KeyId id) {
const auto found = keys_.find(id);
if (found == keys_.end()) return false;
wipe(found->second.priv.key);
keys_.erase(found);
persist();
return true;
}
KeyId SoftwareSecurityModule::keep(KeyPair pair) {
if (next_ == 0) throw std::runtime_error("security module key identifiers exhausted");
const KeyId id {next_++};
keys_.emplace(id, std::move(pair));
try {
persist();
} catch (...) {
wipe(keys_[id].priv.key);
keys_.erase(id);
throw;
}
return id;
}
void SoftwareSecurityModule::persist() {
ByteBuffer blob = encode_keys(next_, keys_);
const bool saved = storage_.save(blob);
wipe(blob);
if (!saved) throw std::runtime_error("security module keys not stored");
}
} // namespace microbu_v2x::security