Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
fc::private_key Class Reference

#include <pke.hpp>

Public Member Functions

 private_key ()
 
 private_key (const bytes &d)
 
 private_key (const private_key &k)
 
 private_key (private_key &&k)
 
 ~private_key ()
 
 operator bool () const
 
private_keyoperator= (const private_key &p)
 
private_keyoperator= (private_key &&p)
 
void sign (const sha1 &digest, array< char, 2048/8 > &sig) const
 
signature sign (const sha1 &digest) const
 
signature sign (const sha256 &digest) const
 
bytes decrypt (const char *bytes, size_t len) const
 
bytes decrypt (const bytes &) const
 
bytes encrypt (const bytes &) const
 
bytes serialize () const
 

Friends

void generate_key_pair (public_key &pub, private_key &priv)
 

Detailed Description

Definition at line 46 of file pke.hpp.

Constructor & Destructor Documentation

◆ private_key() [1/4]

fc::private_key::private_key ( )

Definition at line 165 of file pke.cpp.

166 {
167 }

◆ private_key() [2/4]

fc::private_key::private_key ( const bytes & d)
explicit

Definition at line 168 of file pke.cpp.

169 :my( std::make_shared<detail::pke_impl>() )
170 {
171
172 string pem = "-----BEGIN RSA PRIVATE KEY-----\n";
173 auto b64 = fc::base64_encode( (const unsigned char*)d.data(), d.size() );
174 for( size_t i = 0; i < b64.size(); i += 64 )
175 pem += b64.substr( i, 64 ) + "\n";
176 pem += "-----END RSA PRIVATE KEY-----\n";
177 // fc::cerr<<pem;
178
179 BIO* mem = (BIO*)BIO_new_mem_buf( (void*)pem.c_str(), pem.size() );
180 my->rsa = PEM_read_bio_RSAPrivateKey(mem, NULL, NULL, NULL );
181 BIO_free(mem);
182
183 FC_ASSERT( my->rsa, "read private key" );
184 }
#define FC_ASSERT(TEST,...)
Checks a condition and throws an assert_exception if the test is FALSE.
std::string base64_encode(unsigned char const *bytes_to_encode, unsigned int in_len)
Definition base64.cpp:92
CK_ULONG d
Here is the call graph for this function:

◆ private_key() [3/4]

fc::private_key::private_key ( const private_key & k)

Definition at line 186 of file pke.cpp.

187 :my(k.my)
188 {
189 }

◆ private_key() [4/4]

fc::private_key::private_key ( private_key && k)

Definition at line 190 of file pke.cpp.

191 :my(std::move(k.my) )
192 {
193 }

◆ ~private_key()

fc::private_key::~private_key ( )

Definition at line 194 of file pke.cpp.

194{ }

Member Function Documentation

◆ decrypt() [1/2]

bytes fc::private_key::decrypt ( const bytes & in) const

Definition at line 278 of file pke.cpp.

279 {
280 if( !my ) FC_THROW_EXCEPTION( assert_exception, "!null" );
281 bytes out;
282 out.resize( RSA_size(my->rsa) );
283 int rtn = RSA_private_decrypt( in.size(),
284 (unsigned char*)in.data(),
285 (unsigned char*)out.data(),
286 my->rsa, RSA_PKCS1_OAEP_PADDING );
287 if( rtn >= 0 ) {
288 out.resize(rtn);
289 return out;
290 }
291 FC_THROW_EXCEPTION( exception, "decrypt failed" );
292 }
#define FC_THROW_EXCEPTION(EXCEPTION, FORMAT,...)
std::vector< char > bytes
Definition alt_bn128.hpp:10

◆ decrypt() [2/2]

bytes fc::private_key::decrypt ( const char * bytes,
size_t len ) const

Definition at line 263 of file pke.cpp.

264 {
265 if( !my ) FC_THROW_EXCEPTION( assert_exception, "!null" );
266 bytes out;
267 out.resize( RSA_size(my->rsa) );
268 int rtn = RSA_private_decrypt( l,
269 (unsigned char*)in,
270 (unsigned char*)out.data(),
271 my->rsa, RSA_PKCS1_OAEP_PADDING );
272 if( rtn >= 0 ) {
273 out.resize(rtn);
274 return out;
275 }
276 FC_THROW_EXCEPTION( exception, "decrypt failed" );
277 }
int l

◆ encrypt()

bytes fc::private_key::encrypt ( const bytes & in) const

Definition at line 246 of file pke.cpp.

247 {
248 if( !my ) FC_THROW_EXCEPTION( assert_exception, "!null" );
249 bytes out;
250 out.resize( RSA_size(my->rsa) );
251 int rtn = RSA_private_encrypt( in.size(),
252 (unsigned char*)in.data(),
253 (unsigned char*)out.data(),
254 my->rsa, RSA_PKCS1_OAEP_PADDING );
255 if( rtn >= 0 ) {
256 out.resize(rtn);
257 return out;
258 }
259
260 FC_THROW_EXCEPTION( exception, "encrypt failed" );
261 }

◆ operator bool()

fc::private_key::operator bool ( ) const

Definition at line 30 of file pke.cpp.

30{ return !!my; }

◆ operator=() [1/2]

private_key & fc::private_key::operator= ( const private_key & p)

Definition at line 196 of file pke.cpp.

197 {
198 my = p.my; return *this;
199 }
const mie::Vuint & p
Definition bn.cpp:27

◆ operator=() [2/2]

private_key & fc::private_key::operator= ( private_key && p)

Definition at line 200 of file pke.cpp.

201 {
202 my = std::move(p.my); return *this;
203 }

◆ serialize()

bytes fc::private_key::serialize ( ) const

Definition at line 294 of file pke.cpp.

295 {
296 bytes ba;
297 if( !my ) { return ba; }
298
299 BIO *mem = BIO_new(BIO_s_mem());
300 int e = PEM_write_bio_RSAPrivateKey( mem, my->rsa, NULL, NULL, 0, NULL, NULL );
301 if( e != 1 )
302 {
303 BIO_free(mem);
304 FC_THROW_EXCEPTION( exception, "Error writing private key, ${message}", ("message",fc::string(ERR_error_string( ERR_get_error(),NULL))) );
305 }
306 char* dat;
307 uint32_t l = BIO_get_mem_data( mem, &dat );
308 // return bytes( dat, dat + l );
309
310 stringstream ss( string( dat, l ) );
311 stringstream key;
312 string tmp;
313 fc::getline( ss, tmp );
314 fc::getline( ss, tmp );
315
316 while( tmp.size() && tmp[0] != '-' )
317 {
318 key << tmp;
319 fc::getline( ss, tmp );
320 }
321 auto str = key.str();
322 str = fc::base64_decode( str );
323 ba = bytes( str.begin(), str.end() );
324 // ba = bytes( dat, dat + l );
325 BIO_free(mem);
326 return ba;
327 }
return str
Definition CLI11.hpp:1359
static const Segment ss(Segment::ss)
std::string string
Definition string.hpp:10
std::string base64_decode(const std::string &encoded_string)
Definition base64.cpp:152
unsigned int uint32_t
Definition stdint.h:126
uint8_t key[16]
Definition yubico_otp.c:41
Here is the call graph for this function:
Here is the caller graph for this function:

◆ sign() [1/3]

signature fc::private_key::sign ( const sha1 & digest) const

Definition at line 216 of file pke.cpp.

217 {
218 if( !my ) FC_THROW_EXCEPTION( assert_exception, "!null" );
219 signature sig;
220 sig.resize( RSA_size(my->rsa) );
221
222 uint32_t slen = 0;
223 if( 1 != RSA_sign( NID_sha1, (uint8_t*)digest.data(),
224 20, (unsigned char*)sig.data(), &slen, my->rsa ) )
225 {
226 FC_THROW_EXCEPTION( exception, "rsa sign failed with ${message}", ("message",fc::string(ERR_error_string( ERR_get_error(),NULL))) );
227 }
228 return sig;
229 }
const char * data() const
Definition sha256.cpp:31
fc::sha256 digest(const T &value)
Definition digest.hpp:9
bytes signature
Definition pke.hpp:17
unsigned char uint8_t
Definition stdint.h:124
Here is the call graph for this function:

◆ sign() [2/3]

void fc::private_key::sign ( const sha1 & digest,
array< char, 2048/8 > & sig ) const

Definition at line 205 of file pke.cpp.

206 {
207 FC_ASSERT( (size_t(RSA_size(my->rsa)) <= sizeof(sig)), "Invalid RSA size" );
208 uint32_t slen = 0;
209 if( 1 != RSA_sign( NID_sha1, (uint8_t*)&digest,
210 20, (unsigned char*)&sig, &slen, my->rsa ) )
211 {
212 FC_THROW_EXCEPTION( exception, "rsa sign failed with ${message}", ("message",fc::string(ERR_error_string( ERR_get_error(),NULL))) );
213 }
214 }
Here is the call graph for this function:

◆ sign() [3/3]

signature fc::private_key::sign ( const sha256 & digest) const

Definition at line 230 of file pke.cpp.

231 {
232 if( !my ) FC_THROW_EXCEPTION( assert_exception, "!null" );
233 signature sig;
234 sig.resize( RSA_size(my->rsa) );
235
236 uint32_t slen = 0;
237 if( 1 != RSA_sign( NID_sha256, (uint8_t*)digest.data(),
238 32, (unsigned char*)sig.data(), &slen, my->rsa ) )
239 {
240 FC_THROW_EXCEPTION( exception, "rsa sign failed with ${message}", ("message",fc::string(ERR_error_string( ERR_get_error(),NULL))) );
241 }
242 return sig;
243 }
Here is the call graph for this function:

Friends And Related Symbol Documentation

◆ generate_key_pair

void generate_key_pair ( public_key & pub,
private_key & priv )
friend

Definition at line 329 of file pke.cpp.

330 {
331 static bool init = true;
332 if( init ) { ERR_load_crypto_strings(); init = false; }
333
334 pub.my = std::make_shared<detail::pke_impl>();
335 priv.my = pub.my;
336 pub.my->rsa = RSA_generate_key( 2048, 65537, NULL, NULL );
337 }
void init()
Definition lib_test.cpp:3
bool pub

The documentation for this class was generated from the following files: