Files
MicrOBU/microbu-esp32c5/external/vanetza-idf/vanetza/security/ecc_point.cpp
T
Ashin Walpola 0e9525162d Keep the colleague's microbu-esp32c5 tree in this repository
obu-firmware builds against vanetza-idf from microbu-esp32c5/external, but
that tree was gitignored, so a clone of this repository could not build the
firmware it ships. It is now committed here as ordinary files in its own
folder, microbu-esp32c5/: the colleague's commit cf4b99f plus the V2X2MAP
bridge's signature verification (--trust) used on the bench. Nothing is
fetched from or pushed to the colleague's repository; this repository and
its remotes carry everything. The folder's own .gitignore keeps build output,
downloaded components and private key material out, as it did there; the
committed file set is identical to that repository's tracked files.

The ESP32-C5 is still flashed from obu-firmware/, which only takes
vanetza-idf from microbu-esp32c5/, so the two stay separate folders.
FLASHING.md says how to take a newer version of the colleague's tree (copy
it over the folder, rebuild, test, commit).
2026-09-23 17:46:40 +02:00

103 lines
2.7 KiB
C++

#include <vanetza/security/ecc_point.hpp>
#include <vanetza/security/ecdsa256.hpp>
#include <vanetza/security/public_key.hpp>
#include <boost/variant/apply_visitor.hpp>
#include <boost/variant/static_visitor.hpp>
#include <stdexcept>
namespace vanetza
{
namespace security
{
class EccPointVisitor : public boost::static_visitor<ByteBuffer>
{
public:
template<typename T>
ByteBuffer operator()(const T& point)
{
return point.x;
}
};
ByteBuffer convert_for_signing(const EccPoint& ecc_point)
{
EccPointVisitor visit;
return boost::apply_visitor(visit, ecc_point);
}
class EccPointLengthVisitor : public boost::static_visitor<std::size_t>
{
public:
std::size_t operator()(const X_Coordinate_Only& x_only) const
{
return x_only.x.size();
}
std::size_t operator()(const Compressed_Lsb_Y_0& y0) const
{
return y0.x.size();
}
std::size_t operator()(const Compressed_Lsb_Y_1& y1) const
{
return y1.x.size();
}
std::size_t operator()(const Uncompressed& unc) const
{
return unc.x.size() + unc.y.size();
}
};
std::size_t get_length(const EccPoint& point)
{
EccPointLengthVisitor visitor;
return boost::apply_visitor(visitor, point);
}
EccPoint compress_public_key(const PublicKey& public_key)
{
switch (public_key.compression)
{
case KeyCompression::NoCompression:
if (!public_key.y.empty() && public_key.y.back() & 0x01) {
return Compressed_Lsb_Y_1 {public_key.x };
} else {
return Compressed_Lsb_Y_0 {public_key.x };
}
case KeyCompression::Y0:
return Compressed_Lsb_Y_0 {public_key.x };
case KeyCompression::Y1:
return Compressed_Lsb_Y_1 {public_key.x };
default:
return Compressed_Lsb_Y_0 {};
}
}
EccPoint make_ecc_point(const PublicKey& public_key)
{
switch (public_key.compression)
{
case KeyCompression::NoCompression:
return Uncompressed { public_key.x, public_key.y };
case KeyCompression::Y0:
return Compressed_Lsb_Y_0 { public_key.x };
case KeyCompression::Y1:
return Compressed_Lsb_Y_1 { public_key.x };
}
throw std::invalid_argument("public key has an unknown point compression");
}
EccPoint compress_public_key(const ecdsa256::PublicKey& public_key)
{
if (!public_key.y.empty() && public_key.y.back() & 0x01) {
return Compressed_Lsb_Y_1 { ByteBuffer { public_key.x.begin(), public_key.x.end() } };
} else {
return Compressed_Lsb_Y_0 { ByteBuffer { public_key.x.begin(), public_key.x.end() } };
}
}
} // namespace security
} // namespace vanetza