Vanetza
Loading...
Searching...
No Matches
ecc_point.cpp
1#include <vanetza/security/exception.hpp>
2#include <vanetza/security/v2/ecc_point.hpp>
3#include <vanetza/security/v2/public_key.hpp>
4#include <boost/variant/apply_visitor.hpp>
5#include <boost/variant/static_visitor.hpp>
6#include <cassert>
7
8namespace vanetza
9{
10namespace security
11{
12namespace v2
13{
14
15size_t get_size(const EccPoint& point)
16{
17 size_t size = sizeof(EccPointType);
18 struct ecc_point_visitor : public boost::static_visitor<size_t>
19 {
20 size_t operator()(X_Coordinate_Only coord)
21 {
22 return coord.x.size();
23 }
24
25 size_t operator()(Compressed_Lsb_Y_0 coord)
26 {
27 return coord.x.size();
28 }
29
30 size_t operator()(Compressed_Lsb_Y_1 coord)
31 {
32 return coord.x.size();
33 }
34
35 size_t operator()(Uncompressed coord)
36 {
37 return coord.x.size() + coord.y.size();
38 }
39 };
40
41 ecc_point_visitor visit;
42 boost::apply_visitor(visit, point);
43
44 size += boost::apply_visitor(visit, point);
45 return size;
46}
47
48EccPointType get_type(const EccPoint& point)
49{
50 struct ecc_point_visitor : public boost::static_visitor<EccPointType>
51 {
52 EccPointType operator()(const X_Coordinate_Only&)
53 {
54 return EccPointType::X_Coordinate_Only;
55 }
56
57 EccPointType operator()(const Compressed_Lsb_Y_0&)
58 {
59 return EccPointType::Compressed_Lsb_Y_0;
60 }
61
62 EccPointType operator()(const Compressed_Lsb_Y_1&)
63 {
64 return EccPointType::Compressed_Lsb_Y_1;
65 }
66
67 EccPointType operator()(const Uncompressed&)
68 {
69 return EccPointType::Uncompressed;
70 }
71 };
72
73 ecc_point_visitor visit;
74 return boost::apply_visitor(visit, point);
75}
76
77void serialize(OutputArchive& ar, const EccPoint& point, PublicKeyAlgorithm algo)
78{
79 struct ecc_point_visitor : public boost::static_visitor<>
80 {
81 ecc_point_visitor(OutputArchive& ar, PublicKeyAlgorithm algo) :
82 m_archive(ar), m_algo(algo)
83 {
84 }
85
86 void operator()(X_Coordinate_Only coord)
87 {
88 assert(coord.x.size() == field_size(m_algo));
89 for (auto byte : coord.x) {
90 m_archive << byte;
91 }
92 }
93
94 void operator()(Compressed_Lsb_Y_0 coord)
95 {
96 assert(coord.x.size() == field_size(m_algo));
97 for (auto byte : coord.x) {
98 m_archive << byte;
99 }
100 }
101
102 void operator()(Compressed_Lsb_Y_1 coord)
103 {
104 assert(coord.x.size() == field_size(m_algo));
105 for (auto byte : coord.x) {
106 m_archive << byte;
107 }
108 }
109
110 void operator()(Uncompressed coord)
111 {
112 assert(coord.x.size() == field_size(m_algo));
113 assert(coord.y.size() == field_size(m_algo));
114 for (auto byte : coord.x) {
115 m_archive << byte;
116 }
117 for (auto byte : coord.y) {
118 m_archive << byte;
119 }
120 }
121
122 OutputArchive& m_archive;
123 PublicKeyAlgorithm m_algo;
124 };
125
126 EccPointType type = get_type(point);
127 serialize(ar, type);
128 ecc_point_visitor visit(ar, algo);
129 boost::apply_visitor(visit, point);
130}
131
132void deserialize(InputArchive& ar, EccPoint& point, PublicKeyAlgorithm algo)
133{
134 size_t size = field_size(algo);
135 uint8_t elem;
136 EccPointType type;
137 deserialize(ar, type);
138 switch (type) {
139 case EccPointType::X_Coordinate_Only: {
140 X_Coordinate_Only coord;
141 for (size_t c = 0; c < size; c++) {
142 ar >> elem;
143 coord.x.push_back(elem);
144 }
145 point = coord;
146 break;
147 }
148 case EccPointType::Compressed_Lsb_Y_0: {
149 Compressed_Lsb_Y_0 coord;
150 for (size_t c = 0; c < size; c++) {
151 ar >> elem;
152 coord.x.push_back(elem);
153 }
154 point = coord;
155 break;
156 }
157 case EccPointType::Compressed_Lsb_Y_1: {
158 Compressed_Lsb_Y_1 coord;
159 for (size_t c = 0; c < size; c++) {
160 ar >> elem;
161 coord.x.push_back(elem);
162 }
163 point = coord;
164 break;
165 }
166 case EccPointType::Uncompressed: {
167 Uncompressed coord;
168 for (size_t c = 0; c < size; c++) {
169 ar >> elem;
170 coord.x.push_back(elem);
171 }
172 for (size_t c = 0; c < size; c++) {
173 ar >> elem;
174 coord.y.push_back(elem);
175 }
176 point = coord;
177 break;
178 }
179 default:
180 throw deserialization_error("Unknown EccPointType");
181 }
182}
183
184} // namespace v2
185} // namespace security
186} // namespace vanetza
thrown when a deserialization error occurred
Definition exception.hpp:20
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