Vanetza
 
Loading...
Searching...
No Matches
public_key.cpp
1#include <vanetza/security/exception.hpp>
2#include <vanetza/security/v2/public_key.hpp>
3#include <boost/variant/apply_visitor.hpp>
4#include <boost/variant/static_visitor.hpp>
5
6namespace vanetza
7{
8namespace security
9{
10namespace v2
11{
12
13PublicKeyAlgorithm get_type(const PublicKey& key)
14{
15 struct public_key_visitor : public boost::static_visitor<PublicKeyAlgorithm>
16 {
17 PublicKeyAlgorithm operator()(const ecdsa_nistp256_with_sha256& ecdsa)
18 {
19 return PublicKeyAlgorithm::ECDSA_NISTP256_With_SHA256;
20 }
21 PublicKeyAlgorithm operator()(const ecies_nistp256& ecies)
22 {
23 return PublicKeyAlgorithm::ECIES_NISTP256;
24 }
25 };
26
27 public_key_visitor visit;
28 return boost::apply_visitor(visit, key);
29}
30
31void serialize(OutputArchive& ar, const PublicKey& key)
32{
33 struct public_key_visitor : public boost::static_visitor<>
34 {
35 public_key_visitor(OutputArchive& ar, PublicKeyAlgorithm algo) :
36 m_archive(ar), m_algo(algo)
37 {
38 }
39 void operator()(const ecdsa_nistp256_with_sha256& ecdsa)
40 {
41 serialize(m_archive, ecdsa.public_key, m_algo);
42 }
43 void operator()(const ecies_nistp256& ecies)
44 {
45 serialize(m_archive, ecies.supported_symm_alg);
46 serialize(m_archive, ecies.public_key, m_algo);
47 }
48 OutputArchive& m_archive;
49 PublicKeyAlgorithm m_algo;
50 };
51
52 PublicKeyAlgorithm type = get_type(key);
53 serialize(ar, type);
54 public_key_visitor visit(ar, type);
55 boost::apply_visitor(visit, key);
56}
57
58std::size_t field_size(PublicKeyAlgorithm algo)
59{
60 size_t size = 0;
61 switch (algo) {
62 case PublicKeyAlgorithm::ECDSA_NISTP256_With_SHA256:
63 size = 32;
64 break;
65 case PublicKeyAlgorithm::ECIES_NISTP256:
66 size = 32;
67 break;
68 }
69 return size;
70}
71
72std::size_t field_size(SymmetricAlgorithm algo)
73{
74 size_t size = 0;
75 switch (algo) {
76 case SymmetricAlgorithm::AES128_CCM:
77 size = 16;
78 break;
79 default:
80 throw deserialization_error("Unknown SymmetricAlgorithm");
81 break;
82 }
83 return size;
84}
85
86size_t deserialize(InputArchive& ar, PublicKey& key)
87{
88 PublicKeyAlgorithm type;
89 deserialize(ar, type);
90 switch (type) {
91 case PublicKeyAlgorithm::ECDSA_NISTP256_With_SHA256: {
92 ecdsa_nistp256_with_sha256 ecdsa;
93 deserialize(ar, ecdsa.public_key, PublicKeyAlgorithm::ECDSA_NISTP256_With_SHA256);
94 key = ecdsa;
95 break;
96 }
97 case PublicKeyAlgorithm::ECIES_NISTP256: {
98 ecies_nistp256 ecies;
99 deserialize(ar, ecies.supported_symm_alg);
100 deserialize(ar, ecies.public_key, PublicKeyAlgorithm::ECIES_NISTP256);
101 key = ecies;
102 break;
103 }
104 default:
105 throw deserialization_error("Unknown PublicKeyAlgorithm");
106 break;
107 }
108 return get_size(key);
109}
110
111size_t get_size(const PublicKey& key)
112{
113 size_t size = sizeof(PublicKeyAlgorithm);
114 struct publicKey_visitor : public boost::static_visitor<size_t>
115 {
116 size_t operator()(ecdsa_nistp256_with_sha256 key)
117 {
118 return get_size(key.public_key);
119 }
120 size_t operator()(ecies_nistp256 key)
121 {
122 return get_size(key.public_key) + sizeof(key.supported_symm_alg);
123 }
124 };
125 publicKey_visitor visit;
126 size += boost::apply_visitor(visit, key);
127 return size;
128}
129
130} // namespace v2
131} // namespace security
132} // namespace vanetza