BitShares-Core  4.0.0
BitShares blockchain implementation and command-line interface software
dh.cpp
Go to the documentation of this file.
1 #include <fc/crypto/dh.hpp>
2 
3 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
4 #endif
5 
6 namespace fc {
7  static bool validate( const ssl_dh& dh, bool& valid ) {
8  int check;
9  DH_check(dh,&check);
10  return valid = !(check /*& DH_CHECK_P_NOT_SAFE_PRIME*/);
11  }
12 
13  bool diffie_hellman::generate_params( int s, uint8_t g )
14  {
15  ssl_dh dh(DH_new());
16  DH_generate_parameters_ex(dh.obj, s, g, NULL);
17 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
18  const BIGNUM* bn_p; // must not be free'd!
19  DH_get0_pqg(dh.obj, &bn_p, NULL, NULL);
20  p.resize( BN_num_bytes( bn_p ) );
21  if( p.size() )
22  BN_bn2bin( bn_p, (unsigned char*)&p.front() );
23 #else
24  p.resize( BN_num_bytes( dh->p ) );
25  if( p.size() )
26  BN_bn2bin( dh->p, (unsigned char*)&p.front() );
27 #endif
28  this->g = g;
29  return fc::validate( dh, valid );
30  }
31 
33  {
34  if( !p.size() )
35  return valid = false;
36  ssl_dh dh(DH_new());
37 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
38  const auto bn_p = BN_bin2bn( (unsigned char*)&p.front(), p.size(), NULL );
39  const auto bn_g = BN_bin2bn( (unsigned char*)&g, 1, NULL );
40  DH_set0_pqg(dh.obj, bn_p, NULL, bn_g);
41 #else
42  dh->p = BN_bin2bn( (unsigned char*)&p.front(), p.size(), NULL );
43  dh->g = BN_bin2bn( (unsigned char*)&g, 1, NULL );
44 #endif
45  return fc::validate( dh, valid );
46  }
47 
49  {
50  if( !p.size() )
51  return valid = false;
52  ssl_dh dh(DH_new());
53 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
54  const auto bn_p = BN_bin2bn( (unsigned char*)&p.front(), p.size(), NULL );
55  const auto bn_g = BN_bin2bn( (unsigned char*)&g, 1, NULL );
56  DH_set0_pqg(dh.obj, bn_p, NULL, bn_g);
57 #else
58  dh->p = BN_bin2bn( (unsigned char*)&p.front(), p.size(), NULL );
59  dh->g = BN_bin2bn( (unsigned char*)&g, 1, NULL );
60 #endif
61 
62  if( !fc::validate( dh, valid ) )
63  {
64  return false;
65  }
66  DH_generate_key(dh);
67 
68 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
69  const BIGNUM* bn_pub_key; // must not be free'd!
70  const BIGNUM* bn_priv_key; // must not be free'd!
71  DH_get0_key(dh.obj, &bn_pub_key, &bn_priv_key);
72  pub_key.resize( BN_num_bytes( bn_pub_key ) );
73  priv_key.resize( BN_num_bytes( bn_priv_key ) );
74  if( pub_key.size() )
75  BN_bn2bin( bn_pub_key, (unsigned char*)&pub_key.front() );
76  if( priv_key.size() )
77  BN_bn2bin( bn_priv_key, (unsigned char*)&priv_key.front() );
78 #else
79  pub_key.resize( BN_num_bytes( dh->pub_key ) );
80  priv_key.resize( BN_num_bytes( dh->priv_key ) );
81  if( pub_key.size() )
82  BN_bn2bin( dh->pub_key, (unsigned char*)&pub_key.front() );
83  if( priv_key.size() )
84  BN_bn2bin( dh->priv_key, (unsigned char*)&priv_key.front() );
85 #endif
86 
87  return true;
88  }
89  bool diffie_hellman::compute_shared_key( const char* buf, uint32_t s ) {
90  ssl_dh dh(DH_new());
91 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
92  auto bn_p = BN_bin2bn( (unsigned char*)&p.front(), p.size(), NULL );
93  auto bn_pub_key = BN_bin2bn( (unsigned char*)&pub_key.front(), pub_key.size(), NULL );
94  auto bn_priv_key = BN_bin2bn( (unsigned char*)&priv_key.front(), priv_key.size(), NULL );
95  auto bn_g = BN_bin2bn( (unsigned char*)&g, 1, NULL );
96  DH_set0_pqg(dh.obj, bn_p, NULL, bn_g);
97  DH_set0_key(dh.obj, bn_pub_key, bn_priv_key);
98 #else
99  dh->p = BN_bin2bn( (unsigned char*)&p.front(), p.size(), NULL );
100  dh->pub_key = BN_bin2bn( (unsigned char*)&pub_key.front(), pub_key.size(), NULL );
101  dh->priv_key = BN_bin2bn( (unsigned char*)&priv_key.front(), priv_key.size(), NULL );
102  dh->g = BN_bin2bn( (unsigned char*)&g, 1, NULL );
103 #endif
104 
105  int check;
106  DH_check(dh,&check);
107  if( !fc::validate( dh, valid ) )
108  {
109  return false;
110  }
111 
112  ssl_bignum pk;
113  BN_bin2bn( (unsigned char*)buf, s, pk );
114  int est_size = DH_size(dh);
115  shared_key.resize( est_size );
116  int actual_size = DH_compute_key( (unsigned char*)&shared_key.front(), pk, dh );
117  if ( actual_size < 0 ) return false;
118  if ( actual_size != est_size )
119  shared_key.resize( actual_size );
120 
121  return true;
122  }
123  bool diffie_hellman::compute_shared_key( const std::vector<char>& pubk ) {
124  return compute_shared_key( &pubk.front(), pubk.size() );
125  }
126 }
std::vector< char > priv_key
Definition: dh.hpp:18
uint8_t g
Definition: dh.hpp:21
bool generate_params(int s, uint8_t g)
Definition: dh.cpp:13
bool compute_shared_key(const char *buf, uint32_t s)
Definition: dh.cpp:89
bool validate()
Definition: dh.cpp:32
bool generate_pub_key()
Definition: dh.cpp:48
std::vector< char > shared_key
Definition: dh.hpp:19
std::vector< char > pub_key
Definition: dh.hpp:17
bignum_st BIGNUM
Definition: bigint.hpp:6
Definition: api.hpp:15
std::vector< char > p
Definition: dh.hpp:16