Coverage Report

Created: 2021-05-04 09:02

/src/botan/src/lib/pubkey/pk_ops.cpp
Line
Count
Source (jump to first uncovered line)
1
/*
2
* PK Operation Types
3
* (C) 2010,2015 Jack Lloyd
4
*
5
* Botan is released under the Simplified BSD License (see license.txt)
6
*/
7
8
#include <botan/internal/pk_ops_impl.h>
9
#include <botan/internal/bit_ops.h>
10
#include <botan/rng.h>
11
12
namespace Botan {
13
14
PK_Ops::Encryption_with_EME::Encryption_with_EME(const std::string& eme) :
15
   m_eme(EME::create(eme))
16
0
   {
17
0
   }
18
19
size_t PK_Ops::Encryption_with_EME::max_input_bits() const
20
0
   {
21
0
   return 8 * m_eme->maximum_input_size(max_raw_input_bits());
22
0
   }
23
24
secure_vector<uint8_t> PK_Ops::Encryption_with_EME::encrypt(const uint8_t msg[], size_t msg_len,
25
                                                         RandomNumberGenerator& rng)
26
0
   {
27
0
   const size_t max_raw = max_raw_input_bits();
28
0
   const std::vector<uint8_t> encoded = unlock(m_eme->encode(msg, msg_len, max_raw, rng));
29
0
   return raw_encrypt(encoded.data(), encoded.size(), rng);
30
0
   }
31
32
PK_Ops::Decryption_with_EME::Decryption_with_EME(const std::string& eme) :
33
   m_eme(EME::create(eme))
34
0
   {
35
0
   }
36
37
secure_vector<uint8_t>
38
PK_Ops::Decryption_with_EME::decrypt(uint8_t& valid_mask,
39
                                     const uint8_t ciphertext[],
40
                                     size_t ciphertext_len)
41
0
   {
42
0
   const secure_vector<uint8_t> raw = raw_decrypt(ciphertext, ciphertext_len);
43
0
   return m_eme->unpad(valid_mask, raw.data(), raw.size());
44
0
   }
45
46
PK_Ops::Key_Agreement_with_KDF::Key_Agreement_with_KDF(const std::string& kdf)
47
10.4k
   {
48
10.4k
   if(kdf != "Raw")
49
0
      m_kdf = KDF::create_or_throw(kdf);
50
10.4k
   }
51
52
secure_vector<uint8_t> PK_Ops::Key_Agreement_with_KDF::agree(size_t key_len,
53
                                                          const uint8_t w[], size_t w_len,
54
                                                          const uint8_t salt[], size_t salt_len)
55
10.4k
   {
56
10.4k
   const secure_vector<uint8_t> z = raw_agree(w, w_len);
57
10.4k
   if(m_kdf)
58
0
      return m_kdf->derive_key(key_len, z, salt, salt_len);
59
10.4k
   return z;
60
10.4k
  }
61
62
PK_Ops::Signature_with_EMSA::Signature_with_EMSA(const std::string& emsa) :
63
   Signature(),
64
   m_emsa(EMSA::create_or_throw(emsa)),
65
   m_hash(hash_for_emsa(emsa)),
66
   m_prefix_used(false)
67
0
   {
68
0
   }
69
70
void PK_Ops::Signature_with_EMSA::update(const uint8_t msg[], size_t msg_len)
71
0
   {
72
0
   if(has_prefix() && !m_prefix_used)
73
0
      {
74
0
      m_prefix_used = true;
75
0
      secure_vector<uint8_t> prefix = message_prefix();
76
0
      m_emsa->update(prefix.data(), prefix.size());
77
0
      }
78
0
   m_emsa->update(msg, msg_len);
79
0
   }
80
81
secure_vector<uint8_t> PK_Ops::Signature_with_EMSA::sign(RandomNumberGenerator& rng)
82
0
   {
83
0
   m_prefix_used = false;
84
0
   const secure_vector<uint8_t> msg = m_emsa->raw_data();
85
0
   const auto padded = m_emsa->encoding_of(msg, this->max_input_bits(), rng);
86
0
   return raw_sign(padded.data(), padded.size(), rng);
87
0
   }
88
89
PK_Ops::Verification_with_EMSA::Verification_with_EMSA(const std::string& emsa) :
90
   Verification(),
91
   m_emsa(EMSA::create_or_throw(emsa)),
92
   m_hash(hash_for_emsa(emsa)),
93
   m_prefix_used(false)
94
7.03k
   {
95
7.03k
   }
96
97
void PK_Ops::Verification_with_EMSA::update(const uint8_t msg[], size_t msg_len)
98
7.03k
   {
99
7.03k
   if(has_prefix() && !m_prefix_used)
100
0
      {
101
0
      m_prefix_used = true;
102
0
      secure_vector<uint8_t> prefix = message_prefix();
103
0
      m_emsa->update(prefix.data(), prefix.size());
104
0
      }
105
7.03k
   m_emsa->update(msg, msg_len);
106
7.03k
   }
107
108
bool PK_Ops::Verification_with_EMSA::is_valid_signature(const uint8_t sig[], size_t sig_len)
109
6.93k
   {
110
6.93k
   m_prefix_used = false;
111
6.93k
   const secure_vector<uint8_t> msg = m_emsa->raw_data();
112
113
6.93k
   if(with_recovery())
114
6.63k
      {
115
6.63k
      secure_vector<uint8_t> output_of_key = verify_mr(sig, sig_len);
116
6.63k
      return m_emsa->verify(output_of_key, msg, max_input_bits());
117
6.63k
      }
118
301
   else
119
301
      {
120
301
      Null_RNG rng;
121
301
      secure_vector<uint8_t> encoded = m_emsa->encoding_of(msg, max_input_bits(), rng);
122
301
      return verify(encoded.data(), encoded.size(), sig, sig_len);
123
301
      }
124
6.93k
   }
125
126
void PK_Ops::KEM_Encryption_with_KDF::kem_encrypt(secure_vector<uint8_t>& out_encapsulated_key,
127
                                                  secure_vector<uint8_t>& out_shared_key,
128
                                                  size_t desired_shared_key_len,
129
                                                  Botan::RandomNumberGenerator& rng,
130
                                                  const uint8_t salt[],
131
                                                  size_t salt_len)
132
0
   {
133
0
   secure_vector<uint8_t> raw_shared;
134
0
   this->raw_kem_encrypt(out_encapsulated_key, raw_shared, rng);
135
136
0
   out_shared_key = m_kdf->derive_key(desired_shared_key_len,
137
0
                                      raw_shared.data(), raw_shared.size(),
138
0
                                      salt, salt_len);
139
0
   }
140
141
PK_Ops::KEM_Encryption_with_KDF::KEM_Encryption_with_KDF(const std::string& kdf)
142
0
   {
143
0
   m_kdf = KDF::create_or_throw(kdf);
144
0
   }
145
146
secure_vector<uint8_t>
147
PK_Ops::KEM_Decryption_with_KDF::kem_decrypt(const uint8_t encap_key[],
148
                                             size_t len,
149
                                             size_t desired_shared_key_len,
150
                                             const uint8_t salt[],
151
                                             size_t salt_len)
152
0
   {
153
0
   secure_vector<uint8_t> raw_shared = this->raw_kem_decrypt(encap_key, len);
154
155
0
   return m_kdf->derive_key(desired_shared_key_len,
156
0
                            raw_shared.data(), raw_shared.size(),
157
0
                            salt, salt_len);
158
0
   }
159
160
PK_Ops::KEM_Decryption_with_KDF::KEM_Decryption_with_KDF(const std::string& kdf)
161
0
   {
162
0
   m_kdf = KDF::create_or_throw(kdf);
163
0
   }
164
165
}