Botan 3.12.0
Crypto and TLS for C&
gost_3410.cpp
Go to the documentation of this file.
1/*
2* GOST 34.10-2012
3* (C) 2007 Falko Strenzke, FlexSecure GmbH
4* Manuel Hartl, FlexSecure GmbH
5* (C) 2008-2010,2015,2018,2024 Jack Lloyd
6*
7* Botan is released under the Simplified BSD License (see license.txt)
8*/
9
10#include <botan/gost_3410.h>
11
12#include <botan/ber_dec.h>
13#include <botan/der_enc.h>
14#include <botan/internal/ec_key_data.h>
15#include <botan/internal/fmt.h>
16#include <botan/internal/pk_ops_impl.h>
17
18namespace Botan {
19
20namespace {
21
22EC_Group check_domain(EC_Group domain) {
23 const size_t p_bits = domain.get_p_bits();
24 if(p_bits != 256 && p_bits != 512) {
25 throw Decoding_Error(fmt("GOST-34.10-2012 is not defined for parameters of size {}", p_bits));
26 }
27 return domain;
28}
29
30} // namespace
31
35
36std::vector<uint8_t> GOST_3410_PublicKey::public_key_bits() const {
37 auto bits = _public_ec_point().xy_bytes();
38
39 const size_t part_size = bits.size() / 2;
40
41 // GOST keys are stored in little endian format (WTF)
42 for(size_t i = 0; i != part_size / 2; ++i) {
43 std::swap(bits[i], bits[part_size - 1 - i]);
44 std::swap(bits[part_size + i], bits[2 * part_size - 1 - i]);
45 }
46
47 std::vector<uint8_t> output;
49 return output;
50}
51
52std::string GOST_3410_PublicKey::algo_name() const {
53 const size_t p_bits = domain().get_p_bits();
54
55 if(p_bits == 256 || p_bits == 512) {
56 return fmt("GOST-34.10-2012-{}", p_bits);
57 } else {
58 throw Encoding_Error("GOST-34.10-2012 is not defined for parameters of this size");
59 }
60}
61
63 std::vector<uint8_t> params;
64
65 const OID gost_oid = object_identifier();
66 const OID domain_oid = domain().get_curve_oid();
67
68 DER_Encoder(params).start_sequence().encode(domain_oid).end_cons();
69
70 return AlgorithmIdentifier(gost_oid, params);
71}
72
73GOST_3410_PublicKey::GOST_3410_PublicKey(const AlgorithmIdentifier& alg_id, std::span<const uint8_t> key_bits) {
74 OID ecc_param_id;
75
76 // The parameters also includes hash and cipher OIDs
79 .decode(ecc_param_id)
81 .end_cons()
82 .verify_end();
83
84 auto group = check_domain(EC_Group::from_OID(ecc_param_id));
85
86 std::vector<uint8_t> bits;
88
89 if(bits.size() != 2 * (group.get_p_bits() / 8)) {
90 throw Decoding_Error("GOST-34.10-2012 invalid encoding of public key");
91 }
92
93 const size_t part_size = bits.size() / 2;
94
95 // Keys are stored in little endian format (WTF)
96 std::vector<uint8_t> encoding;
97 encoding.reserve(bits.size() + 1);
98 encoding.push_back(0x04);
99 encoding.insert(encoding.end(), bits.rbegin() + part_size, bits.rend());
100 encoding.insert(encoding.end(), bits.rbegin(), bits.rend() - part_size);
101
102 m_public_key = std::make_shared<EC_PublicKey_Data>(std::move(group), encoding);
103}
104
106 EC_PrivateKey(check_domain(domain), EC_Scalar::from_bigint(domain, x)) {}
107
110
113
114std::unique_ptr<Public_Key> GOST_3410_PrivateKey::public_key() const {
115 return std::make_unique<GOST_3410_PublicKey>(domain(), _public_ec_point());
116}
117
118namespace {
119
120EC_Scalar gost_msg_to_scalar(const EC_Group& group, std::span<const uint8_t> msg) {
121 std::vector<uint8_t> rev_bytes(msg.rbegin(), msg.rend());
122
123 auto ie = EC_Scalar::from_bytes_mod_order(group, rev_bytes);
124 if(ie.is_zero()) {
125 return EC_Scalar::one(group);
126 } else {
127 return ie;
128 }
129}
130
131/**
132* GOST-34.10 signature operation
133*/
134class GOST_3410_Signature_Operation final : public PK_Ops::Signature_with_Hash {
135 public:
136 GOST_3410_Signature_Operation(const GOST_3410_PrivateKey& gost_3410, std::string_view hash_fn) :
137 PK_Ops::Signature_with_Hash(hash_fn), m_group(gost_3410.domain()), m_x(gost_3410._private_key()) {}
138
139 size_t signature_length() const override { return 2 * m_group.get_order_bytes(); }
140
141 AlgorithmIdentifier algorithm_identifier() const override;
142
143 std::vector<uint8_t> raw_sign(std::span<const uint8_t> msg, RandomNumberGenerator& rng) override;
144
145 private:
146 const EC_Group m_group;
147 const EC_Scalar m_x;
148};
149
150AlgorithmIdentifier GOST_3410_Signature_Operation::algorithm_identifier() const {
151 const std::string hash_fn = hash_function();
152
153 const size_t p_bits = m_group.get_p_bits();
154
155 std::string oid_name;
156 if(hash_fn == "GOST-R-34.11-94") {
157 oid_name = "GOST-34.10/GOST-R-34.11-94";
158 } else if(hash_fn == "Streebog-256" && p_bits == 256) {
159 oid_name = "GOST-34.10-2012-256/Streebog-256";
160 } else if(hash_fn == "Streebog-512" && p_bits == 512) {
161 oid_name = "GOST-34.10-2012-512/Streebog-512";
162 } else if(hash_fn == "SHA-256" && p_bits == 256) {
163 oid_name = "GOST-34.10-2012-256/SHA-256";
164 }
165
166 if(oid_name.empty()) {
167 throw Not_Implemented("No encoding defined for GOST with " + hash_fn);
168 }
169
170 return AlgorithmIdentifier(oid_name, AlgorithmIdentifier::USE_EMPTY_PARAM);
171}
172
173std::vector<uint8_t> GOST_3410_Signature_Operation::raw_sign(std::span<const uint8_t> msg, RandomNumberGenerator& rng) {
174 const auto e = gost_msg_to_scalar(m_group, msg);
175
176 const auto k = EC_Scalar::random(m_group, rng);
177 const auto r = EC_Scalar::gk_x_mod_order(k, rng);
178 const auto s = (r * m_x) + (k * e);
179
180 if(r.is_zero() || s.is_zero()) {
181 throw Internal_Error("GOST 34.10 signature generation failed, r/s equal to zero");
182 }
183
184 return EC_Scalar::serialize_pair(s, r);
185}
186
187std::string gost_hash_from_algid(const AlgorithmIdentifier& alg_id) {
188 if(!alg_id.parameters_are_empty()) {
189 throw Decoding_Error("Unexpected non-empty AlgorithmIdentifier parameters for GOST 34.10 signature");
190 }
191
192 const std::string oid_str = alg_id.oid().to_formatted_string();
193 if(oid_str == "GOST-34.10/GOST-R-34.11-94") {
194 return "GOST-R-34.11-94";
195 }
196 if(oid_str == "GOST-34.10-2012-256/Streebog-256") {
197 return "Streebog-256";
198 }
199 if(oid_str == "GOST-34.10-2012-512/Streebog-512") {
200 return "Streebog-512";
201 }
202 if(oid_str == "GOST-34.10-2012-256/SHA-256") {
203 return "SHA-256";
204 }
205
206 throw Decoding_Error(fmt("Unknown OID ({}) for GOST 34.10 signatures", alg_id.oid()));
207}
208
209/**
210* GOST-34.10 verification operation
211*/
212class GOST_3410_Verification_Operation final : public PK_Ops::Verification_with_Hash {
213 public:
214 GOST_3410_Verification_Operation(const GOST_3410_PublicKey& gost, std::string_view padding) :
215 PK_Ops::Verification_with_Hash(padding), m_group(gost.domain()), m_gy_mul(gost._public_ec_point()) {}
216
217 GOST_3410_Verification_Operation(const GOST_3410_PublicKey& gost, const AlgorithmIdentifier& alg_id) :
218 PK_Ops::Verification_with_Hash(gost_hash_from_algid(alg_id)),
219 m_group(gost.domain()),
220 m_gy_mul(gost._public_ec_point()) {}
221
222 bool verify(std::span<const uint8_t> msg, std::span<const uint8_t> sig) override;
223
224 private:
225 const EC_Group m_group;
226 const EC_Group::Mul2Table m_gy_mul;
227};
228
229bool GOST_3410_Verification_Operation::verify(std::span<const uint8_t> msg, std::span<const uint8_t> sig) {
230 if(auto sr = EC_Scalar::deserialize_pair(m_group, sig)) {
231 const auto& [s, r] = sr.value();
232
233 if(r.is_nonzero() && s.is_nonzero()) {
234 const auto e = gost_msg_to_scalar(m_group, msg);
235
236 const auto v = e.invert_vartime();
237
238 // Check if r == x_coord(g*v*s - y*v*r) % n
239 return m_gy_mul.mul2_vartime_x_mod_order_eq(r, v, s, r.negate());
240 }
241 }
242
243 return false;
244}
245
246} // namespace
247
248std::unique_ptr<Private_Key> GOST_3410_PublicKey::generate_another(RandomNumberGenerator& rng) const {
249 return std::make_unique<GOST_3410_PrivateKey>(rng, domain());
250}
251
252std::unique_ptr<PK_Ops::Verification> GOST_3410_PublicKey::create_verification_op(std::string_view params,
253 std::string_view provider) const {
254 if(provider == "base" || provider.empty()) {
255 return std::make_unique<GOST_3410_Verification_Operation>(*this, params);
256 }
257 throw Provider_Not_Found(algo_name(), provider);
258}
259
260std::unique_ptr<PK_Ops::Verification> GOST_3410_PublicKey::create_x509_verification_op(
261 const AlgorithmIdentifier& signature_algorithm, std::string_view provider) const {
262 if(provider == "base" || provider.empty()) {
263 return std::make_unique<GOST_3410_Verification_Operation>(*this, signature_algorithm);
264 }
265
266 throw Provider_Not_Found(algo_name(), provider);
267}
268
269std::unique_ptr<PK_Ops::Signature> GOST_3410_PrivateKey::create_signature_op(RandomNumberGenerator& /*rng*/,
270 std::string_view params,
271 std::string_view provider) const {
272 if(provider == "base" || provider.empty()) {
273 return std::make_unique<GOST_3410_Signature_Operation>(*this, params);
274 }
275 throw Provider_Not_Found(algo_name(), provider);
276}
277
278} // namespace Botan
const std::vector< uint8_t > & parameters() const
Definition asn1_obj.h:409
virtual OID object_identifier() const
Definition pk_keys.cpp:22
static Limits DER()
Definition ber_dec.h:35
void push_back(const BER_Object &obj)
Definition ber_dec.cpp:500
BER_Decoder & decode(bool &out)
Definition ber_dec.h:220
BER_Decoder & verify_end()
Definition ber_dec.cpp:381
BER_Decoder & end_cons()
Definition ber_dec.cpp:524
BER_Decoder & discard_remaining()
Definition ber_dec.cpp:398
BER_Decoder start_sequence()
Definition ber_dec.h:160
DER_Encoder & start_sequence()
Definition der_enc.h:67
DER_Encoder & end_cons()
Definition der_enc.cpp:173
DER_Encoder & encode(bool b)
Definition der_enc.cpp:245
bool mul2_vartime_x_mod_order_eq(const EC_Scalar &v, const EC_Scalar &x, const EC_Scalar &y) const
Definition ec_group.cpp:873
size_t get_p_bits() const
Definition ec_group.cpp:623
static EC_Group from_OID(const OID &oid)
Definition ec_group.cpp:457
const OID & get_curve_oid() const
Definition ec_group.cpp:707
size_t get_order_bytes() const
Definition ec_group.cpp:635
EC_PrivateKey(const EC_PrivateKey &other)=default
const EC_Group & domain() const
Definition ecc_key.cpp:64
std::shared_ptr< const EC_PublicKey_Data > m_public_key
Definition ecc_key.h:139
const EC_AffinePoint & _public_ec_point() const
Definition ecc_key.cpp:76
static EC_Scalar one(const EC_Group &group)
Definition ec_scalar.cpp:65
static EC_Scalar from_bytes_mod_order(const EC_Group &group, std::span< const uint8_t > bytes)
Definition ec_scalar.cpp:53
std::unique_ptr< PK_Ops::Signature > create_signature_op(RandomNumberGenerator &rng, std::string_view params, std::string_view provider) const override
std::unique_ptr< Public_Key > public_key() const override
GOST_3410_PrivateKey(const AlgorithmIdentifier &alg_id, std::span< const uint8_t > key_bits)
Definition gost_3410.h:90
AlgorithmIdentifier algorithm_identifier() const override
Definition gost_3410.cpp:62
std::string algo_name() const override
Definition gost_3410.cpp:52
std::unique_ptr< PK_Ops::Verification > create_x509_verification_op(const AlgorithmIdentifier &signature_algorithm, std::string_view provider) const override
std::unique_ptr< PK_Ops::Verification > create_verification_op(std::string_view params, std::string_view provider) const override
std::unique_ptr< Private_Key > generate_another(RandomNumberGenerator &rng) const final
std::vector< uint8_t > public_key_bits() const override
Definition gost_3410.cpp:36
std::optional< size_t > _signature_element_size_for_DER_encoding() const override
Definition gost_3410.cpp:32
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53