Files
MicrOBU/microbu-esp32c5/external/vanetza-idf/vanetza/security/v2/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

187 lines
4.8 KiB
C++

#include <vanetza/security/exception.hpp>
#include <vanetza/security/v2/ecc_point.hpp>
#include <vanetza/security/v2/public_key.hpp>
#include <boost/variant/apply_visitor.hpp>
#include <boost/variant/static_visitor.hpp>
#include <cassert>
namespace vanetza
{
namespace security
{
namespace v2
{
size_t get_size(const EccPoint& point)
{
size_t size = sizeof(EccPointType);
struct ecc_point_visitor : public boost::static_visitor<size_t>
{
size_t operator()(X_Coordinate_Only coord)
{
return coord.x.size();
}
size_t operator()(Compressed_Lsb_Y_0 coord)
{
return coord.x.size();
}
size_t operator()(Compressed_Lsb_Y_1 coord)
{
return coord.x.size();
}
size_t operator()(Uncompressed coord)
{
return coord.x.size() + coord.y.size();
}
};
ecc_point_visitor visit;
boost::apply_visitor(visit, point);
size += boost::apply_visitor(visit, point);
return size;
}
EccPointType get_type(const EccPoint& point)
{
struct ecc_point_visitor : public boost::static_visitor<EccPointType>
{
EccPointType operator()(const X_Coordinate_Only&)
{
return EccPointType::X_Coordinate_Only;
}
EccPointType operator()(const Compressed_Lsb_Y_0&)
{
return EccPointType::Compressed_Lsb_Y_0;
}
EccPointType operator()(const Compressed_Lsb_Y_1&)
{
return EccPointType::Compressed_Lsb_Y_1;
}
EccPointType operator()(const Uncompressed&)
{
return EccPointType::Uncompressed;
}
};
ecc_point_visitor visit;
return boost::apply_visitor(visit, point);
}
void serialize(OutputArchive& ar, const EccPoint& point, PublicKeyAlgorithm algo)
{
struct ecc_point_visitor : public boost::static_visitor<>
{
ecc_point_visitor(OutputArchive& ar, PublicKeyAlgorithm algo) :
m_archive(ar), m_algo(algo)
{
}
void operator()(X_Coordinate_Only coord)
{
assert(coord.x.size() == field_size(m_algo));
for (auto byte : coord.x) {
m_archive << byte;
}
}
void operator()(Compressed_Lsb_Y_0 coord)
{
assert(coord.x.size() == field_size(m_algo));
for (auto byte : coord.x) {
m_archive << byte;
}
}
void operator()(Compressed_Lsb_Y_1 coord)
{
assert(coord.x.size() == field_size(m_algo));
for (auto byte : coord.x) {
m_archive << byte;
}
}
void operator()(Uncompressed coord)
{
assert(coord.x.size() == field_size(m_algo));
assert(coord.y.size() == field_size(m_algo));
for (auto byte : coord.x) {
m_archive << byte;
}
for (auto byte : coord.y) {
m_archive << byte;
}
}
OutputArchive& m_archive;
PublicKeyAlgorithm m_algo;
};
EccPointType type = get_type(point);
serialize(ar, type);
ecc_point_visitor visit(ar, algo);
boost::apply_visitor(visit, point);
}
void deserialize(InputArchive& ar, EccPoint& point, PublicKeyAlgorithm algo)
{
size_t size = field_size(algo);
uint8_t elem;
EccPointType type;
deserialize(ar, type);
switch (type) {
case EccPointType::X_Coordinate_Only: {
X_Coordinate_Only coord;
for (size_t c = 0; c < size; c++) {
ar >> elem;
coord.x.push_back(elem);
}
point = coord;
break;
}
case EccPointType::Compressed_Lsb_Y_0: {
Compressed_Lsb_Y_0 coord;
for (size_t c = 0; c < size; c++) {
ar >> elem;
coord.x.push_back(elem);
}
point = coord;
break;
}
case EccPointType::Compressed_Lsb_Y_1: {
Compressed_Lsb_Y_1 coord;
for (size_t c = 0; c < size; c++) {
ar >> elem;
coord.x.push_back(elem);
}
point = coord;
break;
}
case EccPointType::Uncompressed: {
Uncompressed coord;
for (size_t c = 0; c < size; c++) {
ar >> elem;
coord.x.push_back(elem);
}
for (size_t c = 0; c < size; c++) {
ar >> elem;
coord.y.push_back(elem);
}
point = coord;
break;
}
default:
throw deserialization_error("Unknown EccPointType");
}
}
} // namespace v2
} // namespace security
} // namespace vanetza