1 /* 2 * GOST 34.10-2001 3 * (C) 2007 Falko Strenzke, FlexSecure GmbH 4 * Manuel Hartl, FlexSecure GmbH 5 * (C) 2008-2010 Jack Lloyd 6 * 7 * Distributed under the terms of the Botan license 8 */ 9 10 #ifndef BOTAN_GOST_3410_KEY_H__ 11 #define BOTAN_GOST_3410_KEY_H__ 12 13 #include <botan/ecc_key.h> 14 #include <botan/pk_ops.h> 15 16 namespace Botan { 17 18 /** 19 * GOST-34.10 Public Key 20 */ 21 class BOTAN_DLL GOST_3410_PublicKey : public virtual EC_PublicKey 22 { 23 public: 24 25 /** 26 * Construct a public key from a given public point. 27 * @param dom_par the domain parameters associated with this key 28 * @param public_point the public point defining this key 29 */ GOST_3410_PublicKey(const EC_Group & dom_par,const PointGFp & public_point)30 GOST_3410_PublicKey(const EC_Group& dom_par, 31 const PointGFp& public_point) : 32 EC_PublicKey(dom_par, public_point) {} 33 34 /** 35 * Construct from X.509 algorithm id and subject public key bits 36 */ 37 GOST_3410_PublicKey(const AlgorithmIdentifier& alg_id, 38 const MemoryRegion<byte>& key_bits); 39 40 /** 41 * Get this keys algorithm name. 42 * @result this keys algorithm name 43 */ algo_name()44 std::string algo_name() const { return "GOST-34.10"; } 45 46 AlgorithmIdentifier algorithm_identifier() const; 47 48 MemoryVector<byte> x509_subject_public_key() const; 49 50 /** 51 * Get the maximum number of bits allowed to be fed to this key. 52 * This is the bitlength of the order of the base point. 53 54 * @result the maximum number of input bits 55 */ max_input_bits()56 size_t max_input_bits() const { return domain().get_order().bits(); } 57 message_parts()58 size_t message_parts() const { return 2; } 59 message_part_size()60 size_t message_part_size() const 61 { return domain().get_order().bytes(); } 62 63 protected: GOST_3410_PublicKey()64 GOST_3410_PublicKey() {} 65 }; 66 67 /** 68 * GOST-34.10 Private Key 69 */ 70 class BOTAN_DLL GOST_3410_PrivateKey : public GOST_3410_PublicKey, 71 public EC_PrivateKey 72 { 73 public: 74 GOST_3410_PrivateKey(const AlgorithmIdentifier & alg_id,const MemoryRegion<byte> & key_bits)75 GOST_3410_PrivateKey(const AlgorithmIdentifier& alg_id, 76 const MemoryRegion<byte>& key_bits) : 77 EC_PrivateKey(alg_id, key_bits) {} 78 79 /** 80 * Generate a new private key 81 * @param rng a random number generator 82 * @param domain parameters to used for this key 83 * @param x the private key; if zero, a new random key is generated 84 */ 85 GOST_3410_PrivateKey(RandomNumberGenerator& rng, 86 const EC_Group& domain, 87 const BigInt& x = 0) : EC_PrivateKey(rng,domain,x)88 EC_PrivateKey(rng, domain, x) {} 89 pkcs8_algorithm_identifier()90 AlgorithmIdentifier pkcs8_algorithm_identifier() const 91 { return EC_PublicKey::algorithm_identifier(); } 92 }; 93 94 /** 95 * GOST-34.10 signature operation 96 */ 97 class BOTAN_DLL GOST_3410_Signature_Operation : public PK_Ops::Signature 98 { 99 public: 100 GOST_3410_Signature_Operation(const GOST_3410_PrivateKey& gost_3410); 101 message_parts()102 size_t message_parts() const { return 2; } message_part_size()103 size_t message_part_size() const { return order.bytes(); } max_input_bits()104 size_t max_input_bits() const { return order.bits(); } 105 106 SecureVector<byte> sign(const byte msg[], size_t msg_len, 107 RandomNumberGenerator& rng); 108 109 private: 110 const PointGFp& base_point; 111 const BigInt& order; 112 const BigInt& x; 113 }; 114 115 /** 116 * GOST-34.10 verification operation 117 */ 118 class BOTAN_DLL GOST_3410_Verification_Operation : public PK_Ops::Verification 119 { 120 public: 121 GOST_3410_Verification_Operation(const GOST_3410_PublicKey& gost); 122 message_parts()123 size_t message_parts() const { return 2; } message_part_size()124 size_t message_part_size() const { return order.bytes(); } max_input_bits()125 size_t max_input_bits() const { return order.bits(); } 126 with_recovery()127 bool with_recovery() const { return false; } 128 129 bool verify(const byte msg[], size_t msg_len, 130 const byte sig[], size_t sig_len); 131 private: 132 const PointGFp& base_point; 133 const PointGFp& public_point; 134 const BigInt& order; 135 }; 136 137 } 138 139 #endif 140