Coverage Report

Created: 2025-10-12 06:36

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/rnp/src/lib/fingerprint.hpp
Line
Count
Source
1
/*
2
 * Copyright (c) 2017-2025 [Ribose Inc](https://www.ribose.com).
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without modification,
6
 * are permitted provided that the following conditions are met:
7
 *
8
 * 1.  Redistributions of source code must retain the above copyright notice,
9
 *     this list of conditions and the following disclaimer.
10
 *
11
 * 2.  Redistributions in binary form must reproduce the above copyright notice,
12
 *     this list of conditions and the following disclaimer in the documentation
13
 *     and/or other materials provided with the distribution.
14
 *
15
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
19
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25
 */
26
27
#ifndef RNP_FINGERPRINT_HPP_
28
#define RNP_FINGERPRINT_HPP_
29
30
#include <array>
31
#include <vector>
32
#include <cstring>
33
#include "config.h"
34
#include <repgp/repgp_def.h>
35
36
typedef struct pgp_key_pkt_t pgp_key_pkt_t;
37
38
/* Size of the keyid */
39
0
#define PGP_KEY_ID_SIZE 8
40
41
/* Size of the fingerprint */
42
0
#define PGP_FINGERPRINT_V3_SIZE 16
43
0
#define PGP_FINGERPRINT_V4_SIZE 20
44
0
#define PGP_FINGERPRINT_V5_SIZE 32
45
0
#define PGP_MAX_FINGERPRINT_SIZE 32
46
#define PGP_FINGERPRINT_HEX_SIZE (PGP_MAX_FINGERPRINT_SIZE * 2) + 1
47
48
static_assert(PGP_MAX_FINGERPRINT_SIZE >= PGP_FINGERPRINT_V4_SIZE, "FP size mismatch.");
49
static_assert(PGP_MAX_FINGERPRINT_SIZE >= PGP_FINGERPRINT_V5_SIZE, "FP size mismatch.");
50
#if defined(ENABLE_CRYPTO_REFRESH)
51
0
#define PGP_FINGERPRINT_V6_SIZE 32
52
static_assert(PGP_MAX_FINGERPRINT_SIZE >= PGP_FINGERPRINT_V6_SIZE, "FP size mismatch.");
53
static_assert(PGP_FINGERPRINT_V5_SIZE == PGP_FINGERPRINT_V6_SIZE, "FP size mismatch.");
54
#endif
55
56
namespace pgp {
57
58
using KeyID = std::array<uint8_t, PGP_KEY_ID_SIZE>;
59
using KeyGrip = std::array<uint8_t, PGP_KEY_GRIP_SIZE>;
60
61
class Fingerprint {
62
    std::vector<uint8_t> fp_;
63
    KeyID                keyid_;
64
65
  public:
66
    Fingerprint();
67
    Fingerprint(const uint8_t *data, size_t size);
68
    Fingerprint(const pgp_key_pkt_t &src);
69
70
    bool operator==(const Fingerprint &src) const;
71
    bool operator!=(const Fingerprint &src) const;
72
73
    static bool                 size_valid(size_t size) noexcept;
74
    const KeyID &               keyid() const noexcept;
75
    const std::vector<uint8_t> &vec() const noexcept;
76
    const uint8_t *             data() const noexcept;
77
    size_t                      size() const noexcept;
78
};
79
80
using Fingerprints = std::vector<Fingerprint>;
81
82
} // namespace pgp
83
84
namespace std {
85
template <> struct hash<pgp::Fingerprint> {
86
    std::size_t
87
    operator()(pgp::Fingerprint const &fp) const noexcept
88
108
    {
89
        /* since fingerprint value is hash itself, we may use its low bytes */
90
108
        size_t res = 0;
91
108
        size_t cpy = std::min(sizeof(res), fp.size());
92
108
        std::memcpy(&res, fp.data(), cpy);
93
108
        return res;
94
108
    }
95
};
96
} // namespace std
97
98
#endif