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>
23 const size_t part_size = bits.size() / 2;
26 for(
size_t i = 0; i != part_size / 2; ++i) {
27 std::swap(bits[i], bits[part_size - 1 - i]);
28 std::swap(bits[part_size + i], bits[2 * part_size - 1 - i]);
31 std::vector<uint8_t> output;
39 if(p_bits == 256 || p_bits == 512) {
40 return fmt(
"GOST-34.10-2012-{}", p_bits);
42 throw Encoding_Error(
"GOST-34.10-2012 is not defined for parameters of this size");
47 std::vector<uint8_t> params;
65 const size_t p_bits = group.get_p_bits();
66 if(p_bits != 256 && p_bits != 512) {
67 throw Decoding_Error(
fmt(
"GOST-34.10-2012 is not defined for parameters of size {}", p_bits));
70 std::vector<uint8_t> bits;
73 if(bits.size() != 2 * (p_bits / 8)) {
74 throw Decoding_Error(
"GOST-34.10-2012 invalid encoding of public key");
77 const size_t part_size = bits.size() / 2;
80 std::vector<uint8_t> encoding;
81 encoding.reserve(bits.size() + 1);
83 encoding.insert(encoding.end(), bits.rbegin() + part_size, bits.rend());
84 encoding.insert(encoding.end(), bits.rbegin(), bits.rend() - part_size);
86 m_public_key = std::make_shared<EC_PublicKey_Data>(std::move(group), encoding);
92 if(p_bits != 256 && p_bits != 512) {
93 throw Decoding_Error(
fmt(
"GOST-34.10-2012 is not defined for parameters of size {}", p_bits));
103EC_Scalar gost_msg_to_scalar(
const EC_Group& group, std::span<const uint8_t> msg) {
104 std::vector<uint8_t> rev_bytes(msg.rbegin(), msg.rend());
117class GOST_3410_Signature_Operation
final :
public PK_Ops::Signature_with_Hash {
119 GOST_3410_Signature_Operation(
const GOST_3410_PrivateKey& gost_3410, std::string_view emsa) :
120 PK_Ops::Signature_with_Hash(emsa), m_group(gost_3410.domain()), m_x(gost_3410._private_key()) {}
122 size_t signature_length()
const override {
return 2 * m_group.get_order_bytes(); }
124 AlgorithmIdentifier algorithm_identifier()
const override;
126 std::vector<uint8_t> raw_sign(std::span<const uint8_t> msg, RandomNumberGenerator& rng)
override;
129 const EC_Group m_group;
131 std::vector<BigInt> m_ws;
134AlgorithmIdentifier GOST_3410_Signature_Operation::algorithm_identifier()
const {
135 const std::string hash_fn = hash_function();
139 std::string oid_name;
140 if(hash_fn ==
"GOST-R-34.11-94") {
141 oid_name =
"GOST-34.10/GOST-R-34.11-94";
142 }
else if(hash_fn ==
"Streebog-256" && p_bits == 256) {
143 oid_name =
"GOST-34.10-2012-256/Streebog-256";
144 }
else if(hash_fn ==
"Streebog-512" && p_bits == 512) {
145 oid_name =
"GOST-34.10-2012-512/Streebog-512";
146 }
else if(hash_fn ==
"SHA-256" && p_bits == 256) {
147 oid_name =
"GOST-34.10-2012-256/SHA-256";
150 if(oid_name.empty()) {
151 throw Not_Implemented(
"No encoding defined for GOST with " + hash_fn);
154 return AlgorithmIdentifier(oid_name, AlgorithmIdentifier::USE_EMPTY_PARAM);
157std::vector<uint8_t> GOST_3410_Signature_Operation::raw_sign(std::span<const uint8_t> msg, RandomNumberGenerator& rng) {
158 const auto e = gost_msg_to_scalar(m_group, msg);
160 const auto k = EC_Scalar::random(m_group, rng);
161 const auto r = EC_Scalar::gk_x_mod_order(k, rng, m_ws);
162 const auto s = (r * m_x) + (k * e);
164 if(r.is_zero() || s.is_zero()) {
165 throw Internal_Error(
"GOST 34.10 signature generation failed, r/s equal to zero");
168 return EC_Scalar::serialize_pair(s, r);
171std::string gost_hash_from_algid(
const AlgorithmIdentifier& alg_id) {
172 if(!alg_id.parameters_are_empty()) {
173 throw Decoding_Error(
"Unexpected non-empty AlgorithmIdentifier parameters for GOST 34.10 signature");
176 const std::string oid_str = alg_id.oid().to_formatted_string();
177 if(oid_str ==
"GOST-34.10/GOST-R-34.11-94") {
178 return "GOST-R-34.11-94";
180 if(oid_str ==
"GOST-34.10-2012-256/Streebog-256") {
181 return "Streebog-256";
183 if(oid_str ==
"GOST-34.10-2012-512/Streebog-512") {
184 return "Streebog-512";
186 if(oid_str ==
"GOST-34.10-2012-256/SHA-256") {
190 throw Decoding_Error(
fmt(
"Unknown OID ({}) for GOST 34.10 signatures", alg_id.oid()));
196class GOST_3410_Verification_Operation
final :
public PK_Ops::Verification_with_Hash {
198 GOST_3410_Verification_Operation(
const GOST_3410_PublicKey& gost, std::string_view padding) :
199 PK_Ops::Verification_with_Hash(padding), m_group(gost.domain()), m_gy_mul(gost._public_key()) {}
201 GOST_3410_Verification_Operation(
const GOST_3410_PublicKey& gost,
const AlgorithmIdentifier& alg_id) :
202 PK_Ops::Verification_with_Hash(gost_hash_from_algid(alg_id)),
203 m_group(gost.domain()),
204 m_gy_mul(gost._public_key()) {}
206 bool verify(std::span<const uint8_t> msg, std::span<const uint8_t> sig)
override;
209 const EC_Group m_group;
210 const EC_Group::Mul2Table m_gy_mul;
213bool GOST_3410_Verification_Operation::verify(std::span<const uint8_t> msg, std::span<const uint8_t> sig) {
214 if(
auto sr = EC_Scalar::deserialize_pair(m_group, sig)) {
215 const auto& [s, r] = sr.value();
217 if(r.is_nonzero() && s.is_nonzero()) {
218 const auto e = gost_msg_to_scalar(m_group, msg);
220 const auto v = e.invert();
233 return std::make_unique<GOST_3410_PrivateKey>(rng, domain());
237 std::string_view provider)
const {
238 if(provider ==
"base" || provider.empty()) {
239 return std::make_unique<GOST_3410_Verification_Operation>(*
this, params);
246 if(provider ==
"base" || provider.empty()) {
247 return std::make_unique<GOST_3410_Verification_Operation>(*
this, signature_algorithm);
254 std::string_view params,
255 std::string_view provider)
const {
256 if(provider ==
"base" || provider.empty()) {
257 return std::make_unique<GOST_3410_Signature_Operation>(*
this, params);
const std::vector< uint8_t > & parameters() const
virtual OID object_identifier() const
void push_back(const BER_Object &obj)
BER_Decoder & decode(bool &out)
BER_Decoder start_sequence()
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
secure_vector< uint8_t > xy_bytes() const
const EC_Group & domain() const
std::shared_ptr< const EC_PublicKey_Data > m_public_key
const EC_Point & public_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
int(* final)(unsigned char *, CTX *)
std::string fmt(std::string_view format, const T &... args)