10#include <botan/gost_3410.h>
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>
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));
30bool is_gost_3410_key_oid(
const OID& oid) {
36 if(!is_gost_3410_key_oid(alg_id.oid())) {
38 fmt(
"Unexpected AlgorithmIdentifier OID {} in association with GOST 34.10 key", alg_id.oid()));
48 auto params = outer.start_sequence();
49 params.decode(ecc_param_id);
51 if(params.more_items()) {
53 params.decode(digest_param_id);
57 throw Decoding_Error(
"Unexpected digest parameters for GOST-34.10-2012-256 public key");
62 throw Decoding_Error(
"Unexpected digest parameters for GOST-34.10-2012-512 public key");
66 if(params.more_items()) {
68 throw Decoding_Error(
"Unexpected extra parameters for GOST-34.10-2012 public key");
71 OID encryption_param_id;
72 params.decode(encryption_param_id);
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");
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");
92 assert_gost_algorithm_identifier(alg_id);
97 decoder.decode(ecc_param_id).verify_end();
99 ecc_param_id = decode_gost_key_parameters(alg_id);
103 check_gost_key_oid_matches_group(alg_id.oid(), group);
117 const size_t part_size = bits.size() / 2;
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]);
125 std::vector<uint8_t> output;
133 if(p_bits == 256 || p_bits == 512) {
134 return fmt(
"GOST-34.10-2012-{}", p_bits);
136 throw Encoding_Error(
"GOST-34.10-2012 is not defined for parameters of this size");
141 std::vector<uint8_t> params;
152 assert_gost_algorithm_identifier(alg_id);
154 const OID ecc_param_id = decode_gost_key_parameters(alg_id);
157 check_gost_key_oid_matches_group(alg_id.
oid(), group);
159 std::vector<uint8_t> bits;
162 if(bits.size() != 2 * (group.get_p_bits() / 8)) {
163 throw Decoding_Error(
"GOST-34.10-2012 invalid encoding of public key");
166 const size_t part_size = bits.size() / 2;
169 std::vector<uint8_t> encoding;
170 encoding.reserve(bits.size() + 1);
172 encoding.insert(encoding.end(), bits.rbegin() + part_size, bits.rend());
173 encoding.insert(encoding.end(), bits.rbegin(), bits.rend() - part_size);
175 m_public_key = std::make_shared<EC_PublicKey_Data>(std::move(group), encoding);
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());
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()) {}
215 size_t signature_length()
const override {
return 2 * m_group.get_order_bytes(); }
217 AlgorithmIdentifier algorithm_identifier()
const override;
219 std::vector<uint8_t> raw_sign(std::span<const uint8_t> msg, RandomNumberGenerator& rng)
override;
222 const EC_Group m_group;
227 const std::string hash_fn = hash_function();
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";
242 if(oid_name.empty()) {
243 throw Not_Implemented(
"No encoding defined for GOST with " + hash_fn);
246 return AlgorithmIdentifier(oid_name, AlgorithmIdentifier::USE_EMPTY_PARAM);
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);
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);
256 if(r.is_zero() || s.is_zero()) {
257 throw Internal_Error(
"GOST 34.10 signature generation failed, r/s equal to zero");
260 return EC_Scalar::serialize_pair(s, r);
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");
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") {
278 throw Decoding_Error(
fmt(
"Unknown OID ({}, {}) for GOST 34.10 signatures", alg_id.oid(), *name));
281 throw Decoding_Error(
fmt(
"Unknown OID ({}) for GOST 34.10 signatures", alg_id.oid()));
288class GOST_3410_Verification_Operation final :
public PK_Ops::Verification_with_Hash {
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()) {}
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()) {}
298 bool verify(std::span<const uint8_t> msg, std::span<const uint8_t> sig)
override;
301 const EC_Group m_group;
302 const EC_Group::Mul2Table m_gy_mul;
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();
309 if(r.is_nonzero() && s.is_nonzero()) {
310 const auto e = gost_msg_to_scalar(m_group, msg);
312 const auto v = e.invert_vartime();
325 return std::make_unique<GOST_3410_PrivateKey>(rng,
domain());
329 std::string_view provider)
const {
330 if(provider ==
"base" || provider.empty()) {
331 return std::make_unique<GOST_3410_Verification_Operation>(*
this, params);
338 if(provider ==
"base" || provider.empty()) {
339 return std::make_unique<GOST_3410_Verification_Operation>(*
this, signature_algorithm);
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);
virtual OID object_identifier() const
void push_back(const BER_Object &obj)
BER_Decoder & decode(bool &out)
BER_Decoder & verify_end()
DER_Encoder & start_sequence()
DER_Encoder & encode(bool b)
bool mul2_vartime_x_mod_order_eq(const EC_Scalar &v, const EC_Scalar &x, const EC_Scalar &y) const
size_t get_p_bits() const
static EC_Group from_OID(const OID &oid)
const OID & get_curve_oid() const
size_t get_order_bytes() const
EC_PrivateKey(const EC_PrivateKey &other)=default
const EC_Group & domain() const
std::shared_ptr< const EC_PublicKey_Data > m_public_key
const EC_AffinePoint & _public_ec_point() const
static EC_Scalar one(const EC_Group &group)
static EC_Scalar from_bytes_mod_order(const EC_Group &group, std::span< const uint8_t > bytes)
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
GOST_3410_PublicKey()=default
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)
std::string fmt(std::string_view format, const T &... args)