Coverage Report

Created: 2025-04-11 06:34

/src/botan/build/include/public/botan/elgamal.h
Line
Count
Source (jump to first uncovered line)
1
/*
2
* ElGamal
3
* (C) 1999-2007,2023 Jack Lloyd
4
*
5
* Botan is released under the Simplified BSD License (see license.txt)
6
*/
7
8
#ifndef BOTAN_ELGAMAL_H_
9
#define BOTAN_ELGAMAL_H_
10
11
#include <botan/pk_keys.h>
12
#include <memory>
13
14
namespace Botan {
15
16
class BigInt;
17
class DL_Group;
18
class DL_PublicKey;
19
class DL_PrivateKey;
20
21
/**
22
* ElGamal Public Key
23
*/
24
class BOTAN_PUBLIC_API(2, 0) ElGamal_PublicKey : public virtual Public_Key {
25
   public:
26
0
      bool supports_operation(PublicKeyOperation op) const override { return (op == PublicKeyOperation::Encryption); }
27
28
      /**
29
      * Load a public key from the ASN.1 encoding
30
      * @param alg_id the X.509 algorithm identifier
31
      * @param key_bits DER encoded public key bits
32
      */
33
      ElGamal_PublicKey(const AlgorithmIdentifier& alg_id, std::span<const uint8_t> key_bits);
34
35
      /**
36
      * Create a public key.
37
      * @param group the underlying DL group
38
      * @param y the public value y = g^x mod p
39
      */
40
      ElGamal_PublicKey(const DL_Group& group, const BigInt& y);
41
42
      AlgorithmIdentifier algorithm_identifier() const override;
43
      std::vector<uint8_t> raw_public_key_bits() const override;
44
      std::vector<uint8_t> public_key_bits() const override;
45
46
      bool check_key(RandomNumberGenerator& rng, bool strong) const override;
47
48
      size_t estimated_strength() const override;
49
      size_t key_length() const override;
50
51
0
      std::string algo_name() const override { return "ElGamal"; }
52
53
      const BigInt& get_int_field(std::string_view field) const override;
54
55
      std::unique_ptr<Private_Key> generate_another(RandomNumberGenerator& rng) const final;
56
57
      std::unique_ptr<PK_Ops::Encryption> create_encryption_op(RandomNumberGenerator& rng,
58
                                                               std::string_view params,
59
                                                               std::string_view provider) const override;
60
61
   private:
62
      friend class ElGamal_PrivateKey;
63
64
0
      ElGamal_PublicKey() = default;
65
66
0
      ElGamal_PublicKey(std::shared_ptr<const DL_PublicKey> key) : m_public_key(std::move(key)) {}
67
68
      std::shared_ptr<const DL_PublicKey> m_public_key;
69
};
70
71
/**
72
* ElGamal Private Key
73
*/
74
75
BOTAN_DIAGNOSTIC_PUSH
76
BOTAN_DIAGNOSTIC_IGNORE_INHERITED_VIA_DOMINANCE
77
78
class BOTAN_PUBLIC_API(2, 0) ElGamal_PrivateKey final : public ElGamal_PublicKey,
79
                                                        public virtual Private_Key {
80
   public:
81
      /**
82
      * Load a private key from the ASN.1 encoding
83
      * @param alg_id the X.509 algorithm identifier
84
      * @param key_bits DER encoded key bits in ANSI X9.42 format
85
      */
86
      ElGamal_PrivateKey(const AlgorithmIdentifier& alg_id, std::span<const uint8_t> key_bits);
87
88
      /**
89
      * Create a new random private key.
90
      * @param rng random number generator to use
91
      * @param group the group to be used in the key
92
      */
93
      ElGamal_PrivateKey(RandomNumberGenerator& rng, const DL_Group& group);
94
95
      /**
96
      * Load a private key from the integer encoding
97
      * @param group the group to be used in the key
98
      * @param private_key the key's secret value
99
      */
100
      ElGamal_PrivateKey(const DL_Group& group, const BigInt& private_key);
101
102
      bool check_key(RandomNumberGenerator& rng, bool) const override;
103
104
      std::unique_ptr<Public_Key> public_key() const override;
105
106
      secure_vector<uint8_t> private_key_bits() const override;
107
108
      secure_vector<uint8_t> raw_private_key_bits() const override;
109
110
      const BigInt& get_int_field(std::string_view field) const override;
111
112
      std::unique_ptr<PK_Ops::Decryption> create_decryption_op(RandomNumberGenerator& rng,
113
                                                               std::string_view params,
114
                                                               std::string_view provider) const override;
115
116
   private:
117
      std::shared_ptr<const DL_PrivateKey> m_private_key;
118
};
119
120
BOTAN_DIAGNOSTIC_POP
121
122
}  // namespace Botan
123
124
#endif