Botan 3.13.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
30bool is_gost_3410_key_oid(const OID& oid) {
31 return oid == OID::from_string("GOST-34.10") || oid == OID::from_string("GOST-34.10-2012-256") ||
32 oid == OID::from_string("GOST-34.10-2012-512");
33}
34
35const AlgorithmIdentifier& assert_gost_algorithm_identifier(const AlgorithmIdentifier& alg_id) {
36 if(!is_gost_3410_key_oid(alg_id.oid())) {
37 throw Decoding_Error(
38 fmt("Unexpected AlgorithmIdentifier OID {} in association with GOST 34.10 key", alg_id.oid()));
39 }
40
41 return alg_id; // NOLINT(*-return-const-ref-from-parameter)
42}
43
44OID decode_gost_key_parameters(const AlgorithmIdentifier& alg_id) {
45 OID ecc_param_id;
46
47 auto outer = BER_Decoder(alg_id.parameters(), BER_Decoder::Limits::DER());
48 auto params = outer.start_sequence();
49 params.decode(ecc_param_id);
50
51 if(params.more_items()) {
52 OID digest_param_id;
53 params.decode(digest_param_id);
54
55 if(alg_id.oid() == OID::from_string("GOST-34.10-2012-256") &&
56 digest_param_id != OID::from_string("Streebog-256")) {
57 throw Decoding_Error("Unexpected digest parameters for GOST-34.10-2012-256 public key");
58 }
59
60 if(alg_id.oid() == OID::from_string("GOST-34.10-2012-512") &&
61 digest_param_id != OID::from_string("Streebog-512")) {
62 throw Decoding_Error("Unexpected digest parameters for GOST-34.10-2012-512 public key");
63 }
64 }
65
66 if(params.more_items()) {
67 if(alg_id.oid() != OID::from_string("GOST-34.10")) {
68 throw Decoding_Error("Unexpected extra parameters for GOST-34.10-2012 public key");
69 }
70
71 OID encryption_param_id;
72 params.decode(encryption_param_id);
73 }
74
75 params.verify_end();
76 outer.verify_end();
77
78 return ecc_param_id;
79}
80
81void check_gost_key_oid_matches_group(const OID& key_oid, const EC_Group& group) {
82 if(key_oid == OID::from_string("GOST-34.10-2012-256") && group.get_p_bits() != 256) {
83 throw Decoding_Error("GOST-34.10-2012-256 public key has unexpected parameters");
84 }
85
86 if(key_oid == OID::from_string("GOST-34.10-2012-512") && group.get_p_bits() != 512) {
87 throw Decoding_Error("GOST-34.10-2012-512 public key has unexpected parameters");
88 }
89}
90
91AlgorithmIdentifier gost_private_key_alg_id(const AlgorithmIdentifier& alg_id) {
92 assert_gost_algorithm_identifier(alg_id);
93
94 OID ecc_param_id;
95 BER_Decoder decoder(alg_id.parameters(), BER_Decoder::Limits::DER());
96 if(decoder.peek_next_object().type_tag() == ASN1_Type::ObjectId) {
97 decoder.decode(ecc_param_id).verify_end();
98 } else {
99 ecc_param_id = decode_gost_key_parameters(alg_id);
100 }
101
102 auto group = check_domain(EC_Group::from_OID(ecc_param_id));
103 check_gost_key_oid_matches_group(alg_id.oid(), group);
104
105 return AlgorithmIdentifier(alg_id.oid(), group.DER_encode());
106}
107
108} // namespace
109
113
114std::vector<uint8_t> GOST_3410_PublicKey::public_key_bits() const {
115 auto bits = _public_ec_point().xy_bytes();
116
117 const size_t part_size = bits.size() / 2;
118
119 // GOST keys are stored in little endian format (WTF)
120 for(size_t i = 0; i != part_size / 2; ++i) {
121 std::swap(bits[i], bits[part_size - 1 - i]);
122 std::swap(bits[part_size + i], bits[2 * part_size - 1 - i]);
123 }
124
125 std::vector<uint8_t> output;
127 return output;
128}
129
131 const size_t p_bits = domain().get_p_bits();
132
133 if(p_bits == 256 || p_bits == 512) {
134 return fmt("GOST-34.10-2012-{}", p_bits);
135 } else {
136 throw Encoding_Error("GOST-34.10-2012 is not defined for parameters of this size");
137 }
138}
139
141 std::vector<uint8_t> params;
142
143 const OID gost_oid = object_identifier();
144 const OID domain_oid = domain().get_curve_oid();
145
146 DER_Encoder(params).start_sequence().encode(domain_oid).end_cons();
147
148 return AlgorithmIdentifier(gost_oid, params);
149}
150
151GOST_3410_PublicKey::GOST_3410_PublicKey(const AlgorithmIdentifier& alg_id, std::span<const uint8_t> key_bits) {
152 assert_gost_algorithm_identifier(alg_id);
153
154 const OID ecc_param_id = decode_gost_key_parameters(alg_id);
155
156 auto group = check_domain(EC_Group::from_OID(ecc_param_id));
157 check_gost_key_oid_matches_group(alg_id.oid(), group);
158
159 std::vector<uint8_t> bits;
161
162 if(bits.size() != 2 * (group.get_p_bits() / 8)) {
163 throw Decoding_Error("GOST-34.10-2012 invalid encoding of public key");
164 }
165
166 const size_t part_size = bits.size() / 2;
167
168 // Keys are stored in little endian format (WTF)
169 std::vector<uint8_t> encoding;
170 encoding.reserve(bits.size() + 1);
171 encoding.push_back(0x04);
172 encoding.insert(encoding.end(), bits.rbegin() + part_size, bits.rend());
173 encoding.insert(encoding.end(), bits.rbegin(), bits.rend() - part_size);
174
175 m_public_key = std::make_shared<EC_PublicKey_Data>(std::move(group), encoding);
176}
177
179 EC_PrivateKey(check_domain(domain), EC_Scalar::from_bigint(domain, x)) {}
180
183
186
187GOST_3410_PrivateKey::GOST_3410_PrivateKey(const AlgorithmIdentifier& alg_id, std::span<const uint8_t> key_bits) :
188 EC_PrivateKey(gost_private_key_alg_id(alg_id), key_bits) {}
189
190std::unique_ptr<Public_Key> GOST_3410_PrivateKey::public_key() const {
191 return std::make_unique<GOST_3410_PublicKey>(domain(), _public_ec_point());
192}
193
194namespace {
195
196EC_Scalar gost_msg_to_scalar(const EC_Group& group, std::span<const uint8_t> msg) {
197 std::vector<uint8_t> rev_bytes(msg.rbegin(), msg.rend());
198
199 auto ie = EC_Scalar::from_bytes_mod_order(group, rev_bytes);
200 if(ie.is_zero()) {
201 return EC_Scalar::one(group);
202 } else {
203 return ie;
204 }
205}
206
207/**
208* GOST-34.10 signature operation
209*/
210class GOST_3410_Signature_Operation final : public PK_Ops::Signature_with_Hash {
211 public:
212 GOST_3410_Signature_Operation(const GOST_3410_PrivateKey& gost_3410, std::string_view hash_fn) :
213 PK_Ops::Signature_with_Hash(hash_fn), m_group(gost_3410.domain()), m_x(gost_3410._private_key()) {}
214
215 size_t signature_length() const override { return 2 * m_group.get_order_bytes(); }
216
217 AlgorithmIdentifier algorithm_identifier() const override;
218
219 std::vector<uint8_t> raw_sign(std::span<const uint8_t> msg, RandomNumberGenerator& rng) override;
220
221 private:
222 const EC_Group m_group;
223 const EC_Scalar m_x;
224};
225
226AlgorithmIdentifier GOST_3410_Signature_Operation::algorithm_identifier() const {
227 const std::string hash_fn = hash_function();
228
229 const size_t p_bits = m_group.get_p_bits();
230
231 std::string oid_name;
232 if(hash_fn == "GOST-R-34.11-94") {
233 oid_name = "GOST-34.10/GOST-R-34.11-94";
234 } else if(hash_fn == "Streebog-256" && p_bits == 256) {
235 oid_name = "GOST-34.10-2012-256/Streebog-256";
236 } else if(hash_fn == "Streebog-512" && p_bits == 512) {
237 oid_name = "GOST-34.10-2012-512/Streebog-512";
238 } else if(hash_fn == "SHA-256" && p_bits == 256) {
239 oid_name = "GOST-34.10-2012-256/SHA-256";
240 }
241
242 if(oid_name.empty()) {
243 throw Not_Implemented("No encoding defined for GOST with " + hash_fn);
244 }
245
246 return AlgorithmIdentifier(oid_name, AlgorithmIdentifier::USE_EMPTY_PARAM);
247}
248
249std::vector<uint8_t> GOST_3410_Signature_Operation::raw_sign(std::span<const uint8_t> msg, RandomNumberGenerator& rng) {
250 const auto e = gost_msg_to_scalar(m_group, msg);
251
252 const auto k = EC_Scalar::random(m_group, rng);
253 const auto r = EC_Scalar::gk_x_mod_order(k, rng);
254 const auto s = (r * m_x) + (k * e);
255
256 if(r.is_zero() || s.is_zero()) {
257 throw Internal_Error("GOST 34.10 signature generation failed, r/s equal to zero");
258 }
259
260 return EC_Scalar::serialize_pair(s, r);
261}
262
263std::string gost_hash_from_algid(const AlgorithmIdentifier& alg_id) {
264 if(!alg_id.parameters_are_empty()) {
265 throw Decoding_Error("Unexpected non-empty AlgorithmIdentifier parameters for GOST 34.10 signature");
266 }
267
268 if(const auto name = alg_id.oid().registered_name()) {
269 if(*name == "GOST-34.10/GOST-R-34.11-94") {
270 return "GOST-R-34.11-94";
271 } else if(*name == "GOST-34.10-2012-256/Streebog-256") {
272 return "Streebog-256";
273 } else if(*name == "GOST-34.10-2012-512/Streebog-512") {
274 return "Streebog-512";
275 } else if(*name == "GOST-34.10-2012-256/SHA-256") {
276 return "SHA-256";
277 } else {
278 throw Decoding_Error(fmt("Unknown OID ({}, {}) for GOST 34.10 signatures", alg_id.oid(), *name));
279 }
280 } else {
281 throw Decoding_Error(fmt("Unknown OID ({}) for GOST 34.10 signatures", alg_id.oid()));
282 }
283}
284
285/**
286* GOST-34.10 verification operation
287*/
288class GOST_3410_Verification_Operation final : public PK_Ops::Verification_with_Hash {
289 public:
290 GOST_3410_Verification_Operation(const GOST_3410_PublicKey& gost, std::string_view padding) :
291 PK_Ops::Verification_with_Hash(padding), m_group(gost.domain()), m_gy_mul(gost._public_ec_point()) {}
292
293 GOST_3410_Verification_Operation(const GOST_3410_PublicKey& gost, const AlgorithmIdentifier& alg_id) :
294 PK_Ops::Verification_with_Hash(gost_hash_from_algid(alg_id)),
295 m_group(gost.domain()),
296 m_gy_mul(gost._public_ec_point()) {}
297
298 bool verify(std::span<const uint8_t> msg, std::span<const uint8_t> sig) override;
299
300 private:
301 const EC_Group m_group;
302 const EC_Group::Mul2Table m_gy_mul;
303};
304
305bool GOST_3410_Verification_Operation::verify(std::span<const uint8_t> msg, std::span<const uint8_t> sig) {
306 if(auto sr = EC_Scalar::deserialize_pair(m_group, sig)) {
307 const auto& [s, r] = sr.value();
308
309 if(r.is_nonzero() && s.is_nonzero()) {
310 const auto e = gost_msg_to_scalar(m_group, msg);
311
312 const auto v = e.invert_vartime();
313
314 // Check if r == x_coord(g*v*s - y*v*r) % n
315 return m_gy_mul.mul2_vartime_x_mod_order_eq(r, v, s, r.negate());
316 }
317 }
318
319 return false;
320}
321
322} // namespace
323
324std::unique_ptr<Private_Key> GOST_3410_PublicKey::generate_another(RandomNumberGenerator& rng) const {
325 return std::make_unique<GOST_3410_PrivateKey>(rng, domain());
326}
327
328std::unique_ptr<PK_Ops::Verification> GOST_3410_PublicKey::create_verification_op(std::string_view params,
329 std::string_view provider) const {
330 if(provider == "base" || provider.empty()) {
331 return std::make_unique<GOST_3410_Verification_Operation>(*this, params);
332 }
333 throw Provider_Not_Found(algo_name(), provider);
334}
335
336std::unique_ptr<PK_Ops::Verification> GOST_3410_PublicKey::create_x509_verification_op(
337 const AlgorithmIdentifier& signature_algorithm, std::string_view provider) const {
338 if(provider == "base" || provider.empty()) {
339 return std::make_unique<GOST_3410_Verification_Operation>(*this, signature_algorithm);
340 }
341
342 throw Provider_Not_Found(algo_name(), provider);
343}
344
345std::unique_ptr<PK_Ops::Signature> GOST_3410_PrivateKey::create_signature_op(RandomNumberGenerator& /*rng*/,
346 std::string_view params,
347 std::string_view provider) const {
348 if(provider == "base" || provider.empty()) {
349 return std::make_unique<GOST_3410_Signature_Operation>(*this, params);
350 }
351 throw Provider_Not_Found(algo_name(), provider);
352}
353
354} // namespace Botan
const OID & oid() const
Definition asn1_obj.h:688
virtual OID object_identifier() const
Definition pk_keys.cpp:22
static Limits DER()
Definition ber_dec.h:42
void push_back(const BER_Object &obj)
Definition ber_dec.cpp:600
BER_Decoder & decode(bool &out)
Definition ber_dec.h:358
BER_Decoder & verify_end()
Definition ber_dec.cpp:471
DER_Encoder & start_sequence()
Definition der_enc.h:86
DER_Encoder & end_cons()
Definition der_enc.cpp:208
DER_Encoder & encode(bool b)
Definition der_enc.cpp:313
bool mul2_vartime_x_mod_order_eq(const EC_Scalar &v, const EC_Scalar &x, const EC_Scalar &y) const
Definition ec_group.cpp:924
size_t get_p_bits() const
Definition ec_group.cpp:654
static EC_Group from_OID(const OID &oid)
Definition ec_group.cpp:467
const OID & get_curve_oid() const
Definition ec_group.cpp:738
size_t get_order_bytes() const
Definition ec_group.cpp:666
EC_PrivateKey(const EC_PrivateKey &other)=default
const EC_Group & domain() const
Definition ecc_key.cpp:76
std::shared_ptr< const EC_PublicKey_Data > m_public_key
Definition ecc_key.h:142
const EC_AffinePoint & _public_ec_point() const
Definition ecc_key.cpp:88
static EC_Scalar one(const EC_Group &group)
Definition ec_scalar.cpp:68
static EC_Scalar from_bytes_mod_order(const EC_Group &group, std::span< const uint8_t > bytes)
Definition ec_scalar.cpp:56
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)
AlgorithmIdentifier algorithm_identifier() const override
std::string algo_name() const override
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
std::optional< size_t > _signature_element_size_for_DER_encoding() const override
static OID from_string(std::string_view str)
Definition asn1_oid.cpp:80
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53