Botan 3.3.0
Crypto and TLS for C&
elgamal.h
Go to the documentation of this file.
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
14namespace Botan {
15
16class BigInt;
17class DL_Group;
18class DL_PublicKey;
19class DL_PrivateKey;
20
21/**
22* ElGamal Public Key
23*/
24class BOTAN_PUBLIC_API(2, 0) ElGamal_PublicKey : public virtual Public_Key {
25 public:
26 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> public_key_bits() const override;
44
45 bool check_key(RandomNumberGenerator& rng, bool strong) const override;
46
47 size_t estimated_strength() const override;
48 size_t key_length() const override;
49
50 std::string algo_name() const override { return "ElGamal"; }
51
52 const BigInt& get_int_field(std::string_view field) const override;
53
54 std::unique_ptr<Private_Key> generate_another(RandomNumberGenerator& rng) const final;
55
56 std::unique_ptr<PK_Ops::Encryption> create_encryption_op(RandomNumberGenerator& rng,
57 std::string_view params,
58 std::string_view provider) const override;
59
60 private:
61 friend class ElGamal_PrivateKey;
62
63 ElGamal_PublicKey() = default;
64
65 ElGamal_PublicKey(std::shared_ptr<const DL_PublicKey> key) : m_public_key(std::move(key)) {}
66
67 std::shared_ptr<const DL_PublicKey> m_public_key;
68};
69
70/**
71* ElGamal Private Key
72*/
73
76
78 public virtual Private_Key {
79 public:
80 /**
81 * Load a private key from the ASN.1 encoding
82 * @param alg_id the X.509 algorithm identifier
83 * @param key_bits DER encoded key bits in ANSI X9.42 format
84 */
85 ElGamal_PrivateKey(const AlgorithmIdentifier& alg_id, std::span<const uint8_t> key_bits);
86
87 /**
88 * Create a new random private key.
89 * @param rng random number generator to use
90 * @param group the group to be used in the key
91 */
93
94 /**
95 * Load a private key from the integer encoding
96 * @param group the group to be used in the key
97 * @param private_key the key's secret value
98 */
99 ElGamal_PrivateKey(const DL_Group& group, const BigInt& private_key);
100
101 bool check_key(RandomNumberGenerator& rng, bool) const override;
102
103 std::unique_ptr<Public_Key> public_key() const override;
104
105 secure_vector<uint8_t> private_key_bits() const override;
106
107 secure_vector<uint8_t> raw_private_key_bits() const override;
108
109 const BigInt& get_int_field(std::string_view field) const override;
110
111 std::unique_ptr<PK_Ops::Decryption> create_decryption_op(RandomNumberGenerator& rng,
112 std::string_view params,
113 std::string_view provider) const override;
114
115 private:
116 std::shared_ptr<const DL_PrivateKey> m_private_key;
117};
118
120
121} // namespace Botan
122
123#endif
std::string algo_name() const override
Definition elgamal.h:50
bool supports_operation(PublicKeyOperation op) const override
Definition elgamal.h:26
int(* final)(unsigned char *, CTX *)
#define BOTAN_DIAGNOSTIC_POP
Definition compiler.h:191
#define BOTAN_DIAGNOSTIC_PUSH
Definition compiler.h:188
#define BOTAN_DIAGNOSTIC_IGNORE_INHERITED_VIA_DOMINANCE
Definition compiler.h:190
#define BOTAN_PUBLIC_API(maj, min)
Definition compiler.h:31
PublicKeyOperation
Definition pk_keys.h:45
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:61