Example #1
0
static int
crypto( RSA            *rsa,
        bool           decrypt,
        const bytes_t  &bytes,
        bytes_t        &crypt )
{
fprintf(stderr, "bytes: %d\n", static_cast<int>(bytes.size()));
fprintf(stderr, "RSA_size: %d\n", RSA_size(rsa));
	typedef int (*cryptop_t) (int, const unsigned char*, unsigned char*, RSA*, int);

	cryptop_t    cryptOp = ( decrypt ? RSA_private_decrypt : RSA_public_encrypt );
	const size_t len = bytes.size(),
	             rsaSz = RSA_size(rsa),
	             blen = rsaSz - ( decrypt ? 0 : 11 ); 
	size_t       tNum = 0;
	bytes_t      buf( rsaSz );

	while ( tNum < len )
	{
		int num = ( decrypt ? blen : (std::min)(blen, len-tNum) );
fprintf(stderr, "request: %d bytes\n", num);
		num = cryptOp(num, &bytes[tNum], &buf[0], rsa, RSA_PKCS1_PADDING);
fprintf(stderr, "crypted: %d bytes\n", num);
		if ( num < 0 )
			return ( -1 );
		else if ( num == 0 )
			return ( 0 );
		
		const size_t oldSZ = crypt.size(),
		             addSZ = ( decrypt ? num : rsaSz );
		crypt.resize(oldSZ + addSZ);
		::memcpy(&crypt[oldSZ], &buf[0], addSZ);
		tNum += blen; //( decrypt ? rsaSz : blen );
	}
fprintf(stderr, "Crypted: %d bytes, crypt size: %d\n", static_cast<int>(tNum), static_cast<int>(crypt.size()));
	return ( tNum == 0 );
}
Example #2
0
 explicit input_stream(bytes_t const& from)
         : begin_(from.data())
         , end_  (from.data() + from.size())
         , cur_  (from.data())
 {
 }
Example #3
0
 size_t size() const
 {
     return bytes.size();
 }
Example #4
0
 void append(uint8_t byte)
 {
     bytes.push_back(byte);
 }