Coverage Report

Created: 2025-04-11 06:34

/src/botan/build/include/public/botan/ecdsa.h
Line
Count
Source (jump to first uncovered line)
1
/*
2
* ECDSA
3
* (C) 2007 Falko Strenzke, FlexSecure GmbH
4
*          Manuel Hartl, FlexSecure GmbH
5
* (C) 2008-2010 Jack Lloyd
6
*
7
* Botan is released under the Simplified BSD License (see license.txt)
8
*/
9
10
#ifndef BOTAN_ECDSA_KEY_H_
11
#define BOTAN_ECDSA_KEY_H_
12
13
#include <botan/ecc_key.h>
14
15
namespace Botan {
16
17
/**
18
* This class represents ECDSA Public Keys.
19
*/
20
class BOTAN_PUBLIC_API(2, 0) ECDSA_PublicKey : public virtual EC_PublicKey {
21
   public:
22
      /**
23
      * Create a public key from a given public point.
24
      * @param group the domain parameters associated with this key
25
      * @param public_key the public point defining this key
26
      */
27
0
      ECDSA_PublicKey(const EC_Group& group, const EC_AffinePoint& public_key) : EC_PublicKey(group, public_key) {}
28
29
#if defined(BOTAN_HAS_LEGACY_EC_POINT)
30
      /**
31
      * Create a public key from a given public point.
32
      * @param group the domain parameters associated with this key
33
      * @param public_point the public point defining this key
34
      */
35
0
      ECDSA_PublicKey(const EC_Group& group, const EC_Point& public_point) : EC_PublicKey(group, public_point) {}
36
#endif
37
38
      /**
39
      * Load a public key.
40
      * @param alg_id the X.509 algorithm identifier
41
      * @param key_bits DER encoded public key bits
42
      */
43
      ECDSA_PublicKey(const AlgorithmIdentifier& alg_id, std::span<const uint8_t> key_bits) :
44
1.95k
            EC_PublicKey(alg_id, key_bits) {}
45
46
      /**
47
      * Recover a public key from a signature/msg pair
48
      * See SEC section 4.6.1
49
      * @param group the elliptic curve group
50
      * @param msg the message
51
      * @param r the r paramter of the signature
52
      * @param s the s paramter of the signature
53
      * @param v the recovery ID
54
      */
55
      ECDSA_PublicKey(
56
         const EC_Group& group, const std::vector<uint8_t>& msg, const BigInt& r, const BigInt& s, uint8_t v);
57
58
      /**
59
      * Get this keys algorithm name.
60
      * @result this keys algorithm name ("ECDSA")
61
      */
62
0
      std::string algo_name() const override { return "ECDSA"; }
63
64
1.26k
      std::optional<size_t> _signature_element_size_for_DER_encoding() const override {
65
1.26k
         return domain().get_order_bytes();
66
1.26k
      }
67
68
0
      bool supports_operation(PublicKeyOperation op) const override { return (op == PublicKeyOperation::Signature); }
69
70
      std::unique_ptr<Private_Key> generate_another(RandomNumberGenerator& rng) const override;
71
72
      uint8_t recovery_param(const std::vector<uint8_t>& msg, const BigInt& r, const BigInt& s) const;
73
74
      std::unique_ptr<PK_Ops::Verification> create_verification_op(std::string_view params,
75
                                                                   std::string_view provider) const override;
76
77
      std::unique_ptr<PK_Ops::Verification> create_x509_verification_op(const AlgorithmIdentifier& signature_algorithm,
78
                                                                        std::string_view provider) const override;
79
80
   protected:
81
7.63k
      ECDSA_PublicKey() = default;
82
};
83
84
/**
85
* This class represents ECDSA Private Keys
86
*/
87
88
BOTAN_DIAGNOSTIC_PUSH
89
BOTAN_DIAGNOSTIC_IGNORE_INHERITED_VIA_DOMINANCE
90
91
class BOTAN_PUBLIC_API(2, 0) ECDSA_PrivateKey final : public ECDSA_PublicKey,
92
                                                      public EC_PrivateKey {
93
   public:
94
      /**
95
      * Load a private key
96
      * @param alg_id the X.509 algorithm identifier
97
      * @param key_bits ECPrivateKey bits
98
      */
99
      ECDSA_PrivateKey(const AlgorithmIdentifier& alg_id, std::span<const uint8_t> key_bits) :
100
7.63k
            EC_PrivateKey(alg_id, key_bits) {}
101
102
      /**
103
      * Create a private key from a given secret @p x
104
      * @param group curve parameters to bu used for this key
105
      * @param x      the private key
106
      */
107
0
      ECDSA_PrivateKey(EC_Group group, EC_Scalar x) : EC_PrivateKey(std::move(group), std::move(x)) {}
108
109
      /**
110
      * Create a new private key
111
      * @param rng a random number generator
112
      * @param group parameters to used for this key
113
      */
114
0
      ECDSA_PrivateKey(RandomNumberGenerator& rng, EC_Group group) : EC_PrivateKey(rng, std::move(group)) {}
115
116
      /**
117
      * Create a private key.
118
      * @param rng a random number generator
119
      * @param group parameters to used for this key
120
      * @param x the private key (if zero, generate a new random key)
121
      */
122
      BOTAN_DEPRECATED("Use one of the other constructors")
123
      ECDSA_PrivateKey(RandomNumberGenerator& rng, const EC_Group& group, const BigInt& x) :
124
0
            EC_PrivateKey(rng, group, x) {}
125
126
      bool check_key(RandomNumberGenerator& rng, bool) const override;
127
128
      std::unique_ptr<Public_Key> public_key() const override;
129
130
      std::unique_ptr<PK_Ops::Signature> create_signature_op(RandomNumberGenerator& rng,
131
                                                             std::string_view params,
132
                                                             std::string_view provider) const override;
133
};
134
135
BOTAN_DIAGNOSTIC_POP
136
137
}  // namespace Botan
138
139
#endif