Botan 3.6.1
Crypto and TLS for C&
dl_scheme.cpp
Go to the documentation of this file.
1/*
2* (C) 2023 Jack Lloyd
3*
4* Botan is released under the Simplified BSD License (see license.txt)
5*/
6
7#include <botan/internal/dl_scheme.h>
8
9#include <botan/assert.h>
10#include <botan/ber_dec.h>
11#include <botan/der_enc.h>
12
13namespace Botan {
14
15namespace {
16
17BigInt decode_single_bigint(std::span<const uint8_t> key_bits) {
18 BigInt x;
19 BER_Decoder(key_bits).decode(x);
20 return x;
21}
22
23BigInt generate_private_dl_key(const DL_Group& group, RandomNumberGenerator& rng) {
24 if(group.has_q() && group.q_bits() >= 160 && group.q_bits() <= 384) {
25 return BigInt::random_integer(rng, 2, group.get_q());
26 } else {
27 return BigInt(rng, group.exponent_bits());
28 }
29}
30
31BigInt check_dl_private_key_input(const BigInt& x, const DL_Group& group) {
32 BOTAN_ARG_CHECK(group.verify_private_element(x), "Invalid discrete logarithm private key value");
33 return x;
34}
35
36} // namespace
37
38DL_PublicKey::DL_PublicKey(const DL_Group& group, const BigInt& public_key) :
39 m_group(group), m_public_key(public_key) {}
40
42 std::span<const uint8_t> key_bits,
43 DL_Group_Format format) :
44 m_group(alg_id.parameters(), format), m_public_key(decode_single_bigint(key_bits)) {}
45
46std::vector<uint8_t> DL_PublicKey::public_key_as_bytes() const {
47 return m_public_key.serialize(m_group.p_bytes());
48}
49
50std::vector<uint8_t> DL_PublicKey::DER_encode() const {
51 std::vector<uint8_t> output;
52 DER_Encoder(output).encode(m_public_key);
53 return output;
54}
55
56bool DL_PublicKey::check_key(RandomNumberGenerator& rng, bool strong) const {
57 return m_group.verify_group(rng, strong) && m_group.verify_public_element(m_public_key);
58}
59
61 return m_group.estimated_strength();
62}
63
64size_t DL_PublicKey::p_bits() const {
65 return m_group.p_bits();
66}
67
68DL_PrivateKey::DL_PrivateKey(const DL_Group& group, const BigInt& private_key) :
69 m_group(group),
70 m_private_key(check_dl_private_key_input(private_key, m_group)),
71 m_public_key(m_group.power_g_p(m_private_key, m_private_key.bits())) {}
72
74 m_group(group),
75 m_private_key(generate_private_dl_key(group, rng)),
76 m_public_key(m_group.power_g_p(m_private_key, m_private_key.bits())) {}
77
79 std::span<const uint8_t> key_bits,
80 DL_Group_Format format) :
81 m_group(alg_id.parameters(), format),
82 m_private_key(check_dl_private_key_input(decode_single_bigint(key_bits), m_group)),
83 m_public_key(m_group.power_g_p(m_private_key, m_group.p_bits())) {}
84
88
92
93bool DL_PrivateKey::check_key(RandomNumberGenerator& rng, bool strong) const {
94 return m_group.verify_group(rng, strong) && m_group.verify_private_element(m_private_key);
95}
96
97std::shared_ptr<DL_PublicKey> DL_PrivateKey::public_key() const {
98 return std::make_shared<DL_PublicKey>(m_group, m_public_key);
99}
100
101const BigInt& DL_PublicKey::get_int_field(std::string_view algo, std::string_view field) const {
102 if(field == "p") {
103 return m_group.get_p();
104 } else if(field == "q") {
105 return m_group.get_q();
106 } else if(field == "g") {
107 return m_group.get_g();
108 } else if(field == "y") {
109 return m_public_key;
110 } else {
111 throw Unknown_PK_Field_Name(algo, field);
112 }
113}
114
115const BigInt& DL_PrivateKey::get_int_field(std::string_view algo, std::string_view field) const {
116 if(field == "p") {
117 return m_group.get_p();
118 } else if(field == "q") {
119 return m_group.get_q();
120 } else if(field == "g") {
121 return m_group.get_g();
122 } else if(field == "x") {
123 return m_private_key;
124 } else if(field == "y") {
125 return m_public_key;
126 } else {
127 throw Unknown_PK_Field_Name(algo, field);
128 }
129}
130
131} // namespace Botan
#define BOTAN_ARG_CHECK(expr, msg)
Definition assert.h:29
static BigInt random_integer(RandomNumberGenerator &rng, const BigInt &min, const BigInt &max)
Definition big_rand.cpp:43
T serialize(size_t len) const
Definition bigint.h:712
secure_vector< uint8_t > get_contents()
Definition der_enc.cpp:132
DER_Encoder & encode(bool b)
Definition der_enc.cpp:250
bool verify_private_element(const BigInt &x) const
Definition dl_group.cpp:341
size_t p_bits() const
Definition dl_group.cpp:452
const BigInt & get_p() const
Definition dl_group.cpp:426
bool verify_public_element(const BigInt &y) const
Definition dl_group.cpp:324
size_t p_bytes() const
Definition dl_group.cpp:456
size_t estimated_strength() const
Definition dl_group.cpp:470
bool verify_group(RandomNumberGenerator &rng, bool strong=true) const
Definition dl_group.cpp:373
const BigInt & get_g() const
Definition dl_group.cpp:433
const BigInt & get_q() const
Definition dl_group.cpp:440
DL_PrivateKey(const DL_Group &group, const BigInt &private_key)
Definition dl_scheme.cpp:68
secure_vector< uint8_t > DER_encode() const
Definition dl_scheme.cpp:85
const BigInt & get_int_field(std::string_view algo_name, std::string_view field) const
secure_vector< uint8_t > raw_private_key_bits() const
Definition dl_scheme.cpp:89
std::shared_ptr< DL_PublicKey > public_key() const
Definition dl_scheme.cpp:97
bool check_key(RandomNumberGenerator &rng, bool strong) const
Definition dl_scheme.cpp:93
std::vector< uint8_t > public_key_as_bytes() const
Definition dl_scheme.cpp:46
const BigInt & get_int_field(std::string_view algo_name, std::string_view field) const
size_t p_bits() const
Definition dl_scheme.cpp:64
size_t estimated_strength() const
Definition dl_scheme.cpp:60
std::vector< uint8_t > DER_encode() const
Definition dl_scheme.cpp:50
bool check_key(RandomNumberGenerator &rng, bool strong) const
Definition dl_scheme.cpp:56
DL_PublicKey(const DL_Group &group, const BigInt &public_key)
Definition dl_scheme.cpp:38
DL_Group_Format
Definition dl_group.h:28
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:61