Vanetza
Loading...
Searching...
No Matches
hash.cpp
1#include <vanetza/security/backend.hpp>
2#include <vanetza/security/v3/certificate.hpp>
3#include <vanetza/security/v3/hash.hpp>
4
5namespace vanetza
6{
7namespace security
8{
9namespace v3
10{
11
12ByteBuffer calculate_message_hash(Backend& backend, HashAlgorithm hash_algo, const ByteBuffer& payload, const CertificateView& signing_cert)
13{
14 ByteBuffer encoded_cert;
15 if (signing_cert.is_canonical()) {
16 encoded_cert = signing_cert.encode();
17 } else {
18 auto canonical_signing_cert = signing_cert.canonicalize();
19 if (canonical_signing_cert) {
20 encoded_cert = canonical_signing_cert->encode();
21 }
22 }
23
24 ByteBuffer data_hash = backend.calculate_hash(hash_algo, payload);
25 ByteBuffer cert_hash = backend.calculate_hash(hash_algo, encoded_cert);
26 ByteBuffer concat_hash;
27 concat_hash.reserve(data_hash.size() + cert_hash.size());
28 concat_hash.insert(concat_hash.end(), data_hash.begin(), data_hash.end());
29 concat_hash.insert(concat_hash.end(), cert_hash.begin(), cert_hash.end());
30 return backend.calculate_hash(hash_algo, concat_hash);
31}
32
33HashAlgorithm specified_hash_algorithm(KeyType key_type)
34{
35 switch (key_type) {
36 case KeyType::NistP256:
37 case KeyType::BrainpoolP256r1:
38 return HashAlgorithm::SHA256;
39 case KeyType::BrainpoolP384r1:
40 return HashAlgorithm::SHA384;
41 default:
42 return HashAlgorithm::Unspecified;
43 }
44}
45
46} // namespace v3
47} // namespace security
48} // namespace vanetza