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&)
18 {
19 return PublicKeyAlgorithm::ECDSA_NISTP256_With_SHA256;
20 }
21
22 PublicKeyAlgorithm operator()(const ecies_nistp256&)
23 {
24 return PublicKeyAlgorithm::ECIES_NISTP256;
25 }
26 };
27
28 public_key_visitor visit;
29 return boost::apply_visitor(visit, key);
30}
31
32void serialize(OutputArchive& ar, const PublicKey& key)
33{
34 struct public_key_visitor : public boost::static_visitor<>
35 {
36 public_key_visitor(OutputArchive& ar, PublicKeyAlgorithm algo) :
37 m_archive(ar), m_algo(algo)
38 {
39 }
40
41 void operator()(const ecdsa_nistp256_with_sha256& ecdsa)
42 {
43 serialize(m_archive, ecdsa.public_key, m_algo);
44 }
45
46 void operator()(const ecies_nistp256& ecies)
47 {
48 serialize(m_archive, ecies.supported_symm_alg);
49 serialize(m_archive, ecies.public_key, m_algo);
50 }
51
52 OutputArchive& m_archive;
53 PublicKeyAlgorithm m_algo;
54 };
55
56 PublicKeyAlgorithm type = get_type(key);
57 serialize(ar, type);
58 public_key_visitor visit(ar, type);
59 boost::apply_visitor(visit, key);
60}
61
62std::size_t field_size(PublicKeyAlgorithm algo)
63{
64 size_t size = 0;
65 switch (algo) {
66 case PublicKeyAlgorithm::ECDSA_NISTP256_With_SHA256:
67 size = 32;
68 break;
69 case PublicKeyAlgorithm::ECIES_NISTP256:
70 size = 32;
71 break;
72 }
73 return size;
74}
75
76std::size_t field_size(SymmetricAlgorithm algo)
77{
78 size_t size = 0;
79 switch (algo) {
80 case SymmetricAlgorithm::AES128_CCM:
81 size = 16;
82 break;
83 default:
84 throw deserialization_error("Unknown SymmetricAlgorithm");
85 break;
86 }
87 return size;
88}
89
90size_t deserialize(InputArchive& ar, PublicKey& key)
91{
92 PublicKeyAlgorithm type;
93 deserialize(ar, type);
94 switch (type) {
95 case PublicKeyAlgorithm::ECDSA_NISTP256_With_SHA256: {
97 deserialize(ar, ecdsa.public_key, PublicKeyAlgorithm::ECDSA_NISTP256_With_SHA256);
98 key = ecdsa;
99 break;
100 }
101 case PublicKeyAlgorithm::ECIES_NISTP256: {
102 ecies_nistp256 ecies;
103 deserialize(ar, ecies.supported_symm_alg);
104 deserialize(ar, ecies.public_key, PublicKeyAlgorithm::ECIES_NISTP256);
105 key = ecies;
106 break;
107 }
108 default:
109 throw deserialization_error("Unknown PublicKeyAlgorithm");
110 break;
111 }
112 return get_size(key);
113}
114
115size_t get_size(const PublicKey& key)
116{
117 size_t size = sizeof(PublicKeyAlgorithm);
118 struct publicKey_visitor : public boost::static_visitor<size_t>
119 {
120 size_t operator()(ecdsa_nistp256_with_sha256 key)
121 {
122 return get_size(key.public_key);
123 }
124
125 size_t operator()(ecies_nistp256 key)
126 {
127 return get_size(key.public_key) + sizeof(key.supported_symm_alg);
128 }
129 };
130 publicKey_visitor visit;
131 size += boost::apply_visitor(visit, key);
132 return size;
133}
134
135} // namespace v2
136} // namespace security
137} // namespace vanetza
thrown when a deserialization error occurred
Definition exception.hpp:20
ecdsa_nistp256_with_sha256 specified in TS 103 097 v1.2.1, section 4.2.4
ecies_nistp256 specified in TS 103 097 v1.2.1, section 4.2.4