Vanetza
Loading...
Searching...
No Matches
ecc_point.cpp
1#include <vanetza/security/ecc_point.hpp>
2#include <vanetza/security/ecdsa256.hpp>
3#include <vanetza/security/public_key.hpp>
4#include <boost/variant/apply_visitor.hpp>
5#include <boost/variant/static_visitor.hpp>
6#include <stdexcept>
7
8namespace vanetza
9{
10namespace security
11{
12
13class EccPointVisitor : public boost::static_visitor<ByteBuffer>
14{
15public:
16 template<typename T>
17 ByteBuffer operator()(const T& point)
18 {
19 return point.x;
20 }
21};
22
23ByteBuffer convert_for_signing(const EccPoint& ecc_point)
24{
25 EccPointVisitor visit;
26 return boost::apply_visitor(visit, ecc_point);
27}
28
29class EccPointLengthVisitor : public boost::static_visitor<std::size_t>
30{
31public:
32 std::size_t operator()(const X_Coordinate_Only& x_only) const
33 {
34 return x_only.x.size();
35 }
36
37 std::size_t operator()(const Compressed_Lsb_Y_0& y0) const
38 {
39 return y0.x.size();
40 }
41
42 std::size_t operator()(const Compressed_Lsb_Y_1& y1) const
43 {
44 return y1.x.size();
45 }
46
47 std::size_t operator()(const Uncompressed& unc) const
48 {
49 return unc.x.size() + unc.y.size();
50 }
51};
52
53std::size_t get_length(const EccPoint& point)
54{
55 EccPointLengthVisitor visitor;
56 return boost::apply_visitor(visitor, point);
57}
58
59EccPoint compress_public_key(const PublicKey& public_key)
60{
61 switch (public_key.compression)
62 {
63 case KeyCompression::NoCompression:
64 if (!public_key.y.empty() && public_key.y.back() & 0x01) {
65 return Compressed_Lsb_Y_1 {public_key.x };
66 } else {
67 return Compressed_Lsb_Y_0 {public_key.x };
68 }
69 case KeyCompression::Y0:
70 return Compressed_Lsb_Y_0 {public_key.x };
71 case KeyCompression::Y1:
72 return Compressed_Lsb_Y_1 {public_key.x };
73 default:
74 return Compressed_Lsb_Y_0 {};
75 }
76}
77
78EccPoint make_ecc_point(const PublicKey& public_key)
79{
80 switch (public_key.compression)
81 {
82 case KeyCompression::NoCompression:
83 return Uncompressed { public_key.x, public_key.y };
84 case KeyCompression::Y0:
85 return Compressed_Lsb_Y_0 { public_key.x };
86 case KeyCompression::Y1:
87 return Compressed_Lsb_Y_1 { public_key.x };
88 }
89 throw std::invalid_argument("public key has an unknown point compression");
90}
91
92EccPoint compress_public_key(const ecdsa256::PublicKey& public_key)
93{
94 if (!public_key.y.empty() && public_key.y.back() & 0x01) {
95 return Compressed_Lsb_Y_1 { ByteBuffer { public_key.x.begin(), public_key.x.end() } };
96 } else {
97 return Compressed_Lsb_Y_0 { ByteBuffer { public_key.x.begin(), public_key.x.end() } };
98 }
99}
100
101} // namespace security
102} // namespace vanetza
Compressed_Lsb_Y_0 specified in TS 103 097 v1.2.1 in section 4.2.5.
Definition ecc_point.hpp:24
Compressed_Lsb_Y_1 specified in TS 103 097 v1.2.1 in section 4.2.5.
Definition ecc_point.hpp:30
Uncompressed specified in TS 103 097 v1.2.1 in section 4.2.5.
Definition ecc_point.hpp:36
X_Coordinate_Only specified in TS 103 097 v1.2.1 in section 4.2.5.
Definition ecc_point.hpp:18