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 size_t operator()(Compressed_Lsb_Y_0 coord)
25 {
26 return coord.x.size();
27 }
28 size_t operator()(Compressed_Lsb_Y_1 coord)
29 {
30 return coord.x.size();
31 }
32 size_t operator()(Uncompressed coord)
33 {
34 return coord.x.size() + coord.y.size();
35 }
36 };
37
38 ecc_point_visitor visit;
39 boost::apply_visitor(visit, point);
40
41 size += boost::apply_visitor(visit, point);
42 return size;
43}
44
45EccPointType get_type(const EccPoint& point)
46{
47 struct ecc_point_visitor : public boost::static_visitor<EccPointType>
48 {
49 EccPointType operator()(X_Coordinate_Only coord)
50 {
51 return EccPointType::X_Coordinate_Only;
52 }
53 EccPointType operator()(Compressed_Lsb_Y_0 coord)
54 {
55 return EccPointType::Compressed_Lsb_Y_0;
56 }
57 EccPointType operator()(Compressed_Lsb_Y_1 coord)
58 {
59 return EccPointType::Compressed_Lsb_Y_1;
60 }
61 EccPointType operator()(Uncompressed coord)
62 {
63 return EccPointType::Uncompressed;
64 }
65 };
66
67 ecc_point_visitor visit;
68 return boost::apply_visitor(visit, point);
69}
70
71void serialize(OutputArchive& ar, const EccPoint& point, PublicKeyAlgorithm algo)
72{
73 struct ecc_point_visitor : public boost::static_visitor<>
74 {
75 ecc_point_visitor(OutputArchive& ar, PublicKeyAlgorithm algo) :
76 m_archive(ar), m_algo(algo)
77 {
78 }
79 void operator()(X_Coordinate_Only coord)
80 {
81 assert(coord.x.size() == field_size(m_algo));
82 for (auto byte : coord.x) {
83 m_archive << byte;
84 }
85 }
86 void operator()(Compressed_Lsb_Y_0 coord)
87 {
88 assert(coord.x.size() == field_size(m_algo));
89 for (auto byte : coord.x) {
90 m_archive << byte;
91 }
92 }
93 void operator()(Compressed_Lsb_Y_1 coord)
94 {
95 assert(coord.x.size() == field_size(m_algo));
96 for (auto byte : coord.x) {
97 m_archive << byte;
98 }
99 }
100 void operator()(Uncompressed coord)
101 {
102 assert(coord.x.size() == field_size(m_algo));
103 assert(coord.y.size() == field_size(m_algo));
104 for (auto byte : coord.x) {
105 m_archive << byte;
106 }
107 for (auto byte : coord.y) {
108 m_archive << byte;
109 }
110 }
111 OutputArchive& m_archive;
112 PublicKeyAlgorithm m_algo;
113 };
114
115 EccPointType type = get_type(point);
116 serialize(ar, type);
117 ecc_point_visitor visit(ar, algo);
118 boost::apply_visitor(visit, point);
119}
120
121void deserialize(InputArchive& ar, EccPoint& point, PublicKeyAlgorithm algo)
122{
123 size_t size = field_size(algo);
124 uint8_t elem;
125 EccPointType type;
126 deserialize(ar, type);
127 switch (type) {
128 case EccPointType::X_Coordinate_Only: {
129 X_Coordinate_Only coord;
130 for (size_t c = 0; c < size; c++) {
131 ar >> elem;
132 coord.x.push_back(elem);
133 }
134 point = coord;
135 break;
136 }
137 case EccPointType::Compressed_Lsb_Y_0: {
138 Compressed_Lsb_Y_0 coord;
139 for (size_t c = 0; c < size; c++) {
140 ar >> elem;
141 coord.x.push_back(elem);
142 }
143 point = coord;
144 break;
145 }
146 case EccPointType::Compressed_Lsb_Y_1: {
147 Compressed_Lsb_Y_1 coord;
148 for (size_t c = 0; c < size; c++) {
149 ar >> elem;
150 coord.x.push_back(elem);
151 }
152 point = coord;
153 break;
154 }
155 case EccPointType::Uncompressed: {
156 Uncompressed coord;
157 for (size_t c = 0; c < size; c++) {
158 ar >> elem;
159 coord.x.push_back(elem);
160 }
161 for (size_t c = 0; c < size; c++) {
162 ar >> elem;
163 coord.y.push_back(elem);
164 }
165 point = coord;
166 break;
167 }
168 default:
169 throw deserialization_error("Unknown EccPointType");
170 }
171}
172
173} // namespace v2
174} // namespace security
175} // namespace vanetza