本文整理汇总了C++中rsa_public函数的典型用法代码示例。如果您正苦于以下问题:C++ rsa_public函数的具体用法?C++ rsa_public怎么用?C++ rsa_public使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了rsa_public函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: rsa_pkcs1_verify
/*
* Perform a public RSA and check the message digest
*/
int rsa_pkcs1_verify( rsa_context *ctx, int alg_id,
uchar *hash, int hashlen,
uchar *sig, int siglen )
{
int len;
uchar *p, c, buf[512];
if( siglen != ctx->len || siglen < 48 || siglen > 512 )
return( ERR_RSA_VERIFY_FAILED );
if( rsa_public( ctx, sig, siglen, buf, siglen ) != 0 )
return( ERR_RSA_VERIFY_FAILED );
p = buf;
if( *p++ != 0 || *p++ != RSA_SIGN )
return( ERR_RSA_VERIFY_FAILED );
while( *p != 0 )
{
if( p >= buf + siglen - 1 || *p != 0xFF )
return( ERR_RSA_VERIFY_FAILED );
p++;
}
p++;
len = siglen - (int)( p - buf );
if( len == 34 )
{
c = p[13];
p[13] = 0;
if( memcmp( p, ASN1_HASH_MDX, 18 ) != 0 )
return( ERR_RSA_VERIFY_FAILED );
if( ( c == 2 && alg_id == RSA_MD2 ) ||
( c == 4 && alg_id == RSA_MD4 ) ||
( c == 5 && alg_id == RSA_MD5 ) )
{
if( memcmp( p + 18, hash, 16 ) == 0 )
return( 0 );
}
}
if( len == 35 && alg_id == RSA_SHA1 )
{
if( memcmp( p, ASN1_HASH_SHA1, 15 ) == 0 &&
memcmp( p + 15, hash, 20 ) == 0 )
return( 0 );
}
if( len == hashlen && alg_id == RSA_NONE )
{
if( memcmp( p, hash, hashlen ) == 0 )
return( 0 );
}
return( ERR_RSA_VERIFY_FAILED );
}
示例2: rsa_pkcs1_encrypt
/*
* Add the PKCS#1 v1.5 padding and do a public RSA
*/
int rsa_pkcs1_encrypt( rsa_context *ctx,
unsigned char *input, int ilen,
unsigned char *output, int *olen )
{
int nb_pad;
unsigned char *p = output;
if( *olen != ctx->len || *olen < ilen + 11 )
{
return( ERR_RSA_BAD_INPUT_DATA );
}
nb_pad = *olen - 3 - ilen;
*p++ = 0;
*p++ = RSA_CRYPT;
while( nb_pad-- > 0 )
{
do { *p = rand(); } while( *p == 0 );
p++;
}
*p++ = 0;
memcpy( p, input, ilen );
return( rsa_public( ctx, output, *olen, output, olen ) );
}
示例3: rsa_pkcs1_encrypt
/*
* Add the PKCS1 v1.5 padding and do a public RSA
*/
int rsa_pkcs1_encrypt( rsa_context *ctx,
uchar *input, int ilen,
uchar *output, int olen )
{
int nb_pad;
uchar *p = output;
if( olen != ctx->len || olen < ilen + 11 )
return( ERR_RSA_ENCRYPT_FAILED );
nb_pad = olen - 3 - ilen;
*p++ = 0;
*p++ = RSA_CRYPT;
while( nb_pad-- > 0 )
{
do { *p = (uchar) rand(); } while( *p == 0 );
p++;
}
*p++ = 0;
memcpy( p, input, ilen );
if( rsa_public( ctx, output, olen, output, olen ) != 0 )
return( ERR_RSA_ENCRYPT_FAILED );
return( 0 );
}
示例4: rsa_rsaes_pkcs1_v15_encrypt
/*
* Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
*/
int rsa_rsaes_pkcs1_v15_encrypt( rsa_context *ctx,
int (*f_rng)(void *, unsigned char *, size_t),
void *p_rng,
int mode, size_t ilen,
const unsigned char *input,
unsigned char *output )
{
size_t nb_pad, olen;
int ret;
unsigned char *p = output;
if( mode == RSA_PRIVATE && ctx->padding != RSA_PKCS_V15 )
return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
if( f_rng == NULL )
return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
olen = ctx->len;
if( olen < ilen + 11 )
return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
nb_pad = olen - 3 - ilen;
*p++ = 0;
if( mode == RSA_PUBLIC )
{
*p++ = RSA_CRYPT;
while( nb_pad-- > 0 )
{
int rng_dl = 100;
do {
ret = f_rng( p_rng, p, 1 );
} while( *p == 0 && --rng_dl && ret == 0 );
// Check if RNG failed to generate data
//
if( rng_dl == 0 || ret != 0 )
return( POLARSSL_ERR_RSA_RNG_FAILED + ret );
p++;
}
}
else
{
*p++ = RSA_SIGN;
while( nb_pad-- > 0 )
*p++ = 0xFF;
}
*p++ = 0;
memcpy( p, input, ilen );
return( ( mode == RSA_PUBLIC )
? rsa_public( ctx, output, output )
: rsa_private( ctx, f_rng, p_rng, output, output ) );
}
示例5: rsa_pkcs1_encrypt
/*
Add the message padding, then do an RSA operation
*/
int rsa_pkcs1_encrypt(rsa_context *ctx, int mode, int ilen, uchar *input, uchar *output)
{
int nb_pad, olen;
uchar *p = output;
olen = ctx->len;
switch (ctx->padding) {
case RSA_PKCS_V15:
if (ilen < 0 || olen < ilen + 11) {
return EST_ERR_RSA_BAD_INPUT_DATA;
}
nb_pad = olen - 3 - ilen;
*p++ = 0;
*p++ = RSA_CRYPT;
while (nb_pad-- > 0) {
do {
*p = (uchar)rand();
} while (*p == 0);
p++;
}
*p++ = 0;
memcpy(p, input, ilen);
break;
default:
return EST_ERR_RSA_INVALID_PADDING;
}
return (mode == RSA_PUBLIC) ? rsa_public(ctx, output, output) : rsa_private(ctx, output, output);
}
示例6: rsa_pkcs1_decrypt
/*
* Do an RSA operation, then remove the message padding
*/
int rsa_pkcs1_decrypt( rsa_context *ctx,
int mode, int *olen,
unsigned char *input,
unsigned char *output,
int output_max_len)
{
int ret, ilen;
unsigned char *p;
unsigned char buf[1024];
ilen = ctx->len;
if( ilen < 16 || ilen > (int) sizeof( buf ) )
return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
ret = ( mode == RSA_PUBLIC )
? rsa_public( ctx, input, buf )
: rsa_private( ctx, input, buf );
if( ret != 0 )
return( ret );
p = buf;
switch( ctx->padding )
{
case RSA_PKCS_V15:
if( *p++ != 0 || *p++ != RSA_CRYPT )
return( POLARSSL_ERR_RSA_INVALID_PADDING );
while( *p != 0 )
{
if( p >= buf + ilen - 1 )
return( POLARSSL_ERR_RSA_INVALID_PADDING );
p++;
}
p++;
break;
default:
return( POLARSSL_ERR_RSA_INVALID_PADDING );
}
if (ilen - (int)(p - buf) > output_max_len)
return( POLARSSL_ERR_RSA_OUTPUT_TOO_LARGE );
*olen = ilen - (int)(p - buf);
memcpy( output, p, *olen );
return( 0 );
}
示例7: rsa_rsaes_pkcs1_v15_decrypt
/*
* Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
*/
int rsa_rsaes_pkcs1_v15_decrypt( rsa_context *ctx,
int (*f_rng)(void *, unsigned char *, size_t),
void *p_rng,
int mode, size_t *olen,
const unsigned char *input,
unsigned char *output,
size_t output_max_len)
{
int ret;
size_t ilen, pad_count = 0, i;
unsigned char *p, bad, pad_done = 0;
unsigned char buf[POLARSSL_MPI_MAX_SIZE];
if( mode == RSA_PRIVATE && ctx->padding != RSA_PKCS_V15 )
return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
ilen = ctx->len;
if( ilen < 16 || ilen > sizeof( buf ) )
return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
ret = ( mode == RSA_PUBLIC )
? rsa_public( ctx, input, buf )
: rsa_private( ctx, f_rng, p_rng, input, buf );
if( ret != 0 )
return( ret );
p = buf;
bad = 0;
/*
* Check and get padding len in "constant-time"
*/
bad |= *p++; /* First byte must be 0 */
/* This test does not depend on secret data */
if( mode == RSA_PRIVATE )
{
bad |= *p++ ^ RSA_CRYPT;
/* Get padding len, but always read till end of buffer
* (minus one, for the 00 byte) */
for( i = 0; i < ilen - 3; i++ )
{
pad_done |= ((p[i] | (unsigned char)-p[i]) >> 7) ^ 1;
pad_count += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
}
p += pad_count;
bad |= *p++; /* Must be zero */
}
示例8: rsa_pkcs1_encrypt
/*
* Add the message padding, then do an RSA operation
*/
int rsa_pkcs1_encrypt( rsa_context *ctx,
int (*f_rng)(void *),
void *p_rng,
int mode, int ilen,
const unsigned char *input,
unsigned char *output )
{
int nb_pad, olen;
unsigned char *p = output;
olen = ctx->len;
switch( ctx->padding )
{
case RSA_PKCS_V15:
if( ilen < 0 || olen < ilen + 11 || f_rng == NULL )
return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
nb_pad = olen - 3 - ilen;
*p++ = 0;
*p++ = RSA_CRYPT;
while( nb_pad-- > 0 )
{
int rng_dl = 100;
do {
*p = (unsigned char) f_rng( p_rng );
} while( *p == 0 && --rng_dl );
// Check if RNG failed to generate data
//
if( rng_dl == 0 )
return POLARSSL_ERR_RSA_RNG_FAILED;
p++;
}
*p++ = 0;
memcpy( p, input, ilen );
break;
default:
return( POLARSSL_ERR_RSA_INVALID_PADDING );
}
return( ( mode == RSA_PUBLIC )
? rsa_public( ctx, output, output )
: rsa_private( ctx, output, output ) );
}
示例9: ctr_rsa_public
int ctr_rsa_public(const u8 signature[0x100], u8 output[0x100], rsakey2048* key)
{
ctr_rsa_context ctx;
u32 result;
ctr_rsa_init(&ctx, key);
result = rsa_public(&ctx.rsa, signature, output);
ctr_rsa_free(&ctx);
if (result == 0)
return 1;
else
return 0;
}
示例10: rsa_pkcs1_decrypt
/*
Do an RSA operation, then remove the message padding
*/
int rsa_pkcs1_decrypt(rsa_context *ctx, int mode, int *olen, uchar *input, uchar *output, int output_max_len)
{
int ret, ilen;
uchar *p;
uchar buf[512];
ilen = ctx->len;
if (ilen < 16 || ilen > (int)sizeof(buf)) {
return EST_ERR_RSA_BAD_INPUT_DATA;
}
ret = (mode == RSA_PUBLIC) ? rsa_public(ctx, input, buf) : rsa_private(ctx, input, buf);
if (ret != 0) {
return ret;
}
p = buf;
switch (ctx->padding) {
case RSA_PKCS_V15:
if (*p++ != 0 || *p++ != RSA_CRYPT) {
return EST_ERR_RSA_INVALID_PADDING;
}
while (*p != 0) {
if (p >= buf + ilen - 1) {
return EST_ERR_RSA_INVALID_PADDING;
}
p++;
}
p++;
break;
default:
return EST_ERR_RSA_INVALID_PADDING;
}
if (ilen - (int)(p - buf) > output_max_len) {
return EST_ERR_RSA_OUTPUT_TO_LARGE;
}
*olen = ilen - (int)(p - buf);
memcpy(output, p, *olen);
return 0;
}
示例11: crypto_rsa_encrypt
void
crypto_rsa_encrypt(int len, uint8 * in, uint8 * out, uint32 modulus_size, uint8 * modulus, uint8 * exponent)
{
rsa_context ctx;
rsa_init(&ctx, 0, 0);
ctx.len = modulus_size;
mpi_init(&ctx.N, &ctx.E, NULL);
mpi_read_binary(&ctx.N, modulus, modulus_size);
mpi_read_binary(&ctx.E, exponent, SEC_EXPONENT_SIZE);
ASSERT(!rsa_check_pubkey( &ctx ));
ASSERT(modulus_size <= SEC_MAX_MODULUS_SIZE);
uint8 in2[SEC_MAX_MODULUS_SIZE];
memset(in2, 0, modulus_size - len);
memcpy(in2 + modulus_size - len, in, len);
int err = rsa_public(&ctx, in2, out);
ASSERT(!err);
mpi_free(&ctx.N, &ctx.E, NULL);
rsa_free(&ctx);
}
示例12: main
//.........这里部分代码省略.........
tsc = hardclock();
for( j = 0; j < 1024; j++ )
des_cbc_encrypt( &des, tmp, buf, buf, BUFSIZE );
printf( "%9ld Kb/s, %9ld cycles/byte\n", i * BUFSIZE / 1024,
( hardclock() - tsc ) / ( j * BUFSIZE ) );
/*
* AES timings
*/
for( keysize = 128; keysize <= 256; keysize += 64 )
{
printf( " AES-%d : ", keysize );
fflush( stdout );
aes_set_key( &aes, tmp, keysize );
set_alarm( 1 );
for( i = 1; ! alarmed; i++ )
aes_cbc_encrypt( &aes, tmp, buf, buf, BUFSIZE );
tsc = hardclock();
for( j = 0; j < 1024; j++ )
aes_cbc_encrypt( &aes, tmp, buf, buf, BUFSIZE );
printf( "%9ld Kb/s, %9ld cycles/byte\n", i * BUFSIZE / 1024,
( hardclock() - tsc ) / ( j * BUFSIZE ) );
}
/*
* RSA-1024 timing
*/
printf( " RSA-1024 : " );
fflush( stdout );
rsa_gen_key( &rsa, 1024, 65537, myrand, NULL );
set_alarm( 4 );
for( i = 1; ! alarmed; i++ )
{
buf[0] = 0;
rsa_public( &rsa, buf, 128, buf, 128 );
}
printf( "%9ld public/s\n", i / 4 );
printf( " RSA-1024 : " );
fflush( stdout );
set_alarm( 4 );
for( i = 1; ! alarmed; i++ )
{
buf[0] = 0;
rsa_private( &rsa, buf, 128, buf, 128 );
}
printf( "%9ld private/s\n", i / 4 );
rsa_free( &rsa );
/*
* RSA-2048 timing
*/
printf( " RSA-2048 : " );
fflush( stdout );
rsa_gen_key( &rsa, 2048, 65537, myrand, NULL );
set_alarm( 4 );
for( i = 1; ! alarmed; i++ )
{
buf[0] = 0;
rsa_public( &rsa, buf, 256, buf, 256 );
}
printf( "%9ld public/s\n", i / 4 );
printf( " RSA-2048 : " );
fflush( stdout );
set_alarm( 4 );
for( i = 1; ! alarmed; i++ )
{
buf[0] = 0;
rsa_private( &rsa, buf, 256, buf, 256 );
}
printf( "%9ld private/s\n\n", i / 4 );
rsa_free( &rsa );
#ifdef WIN32
printf( " Press Enter to exit this program.\n" );
fflush( stdout ); getchar();
#endif
return( 0 );
}
示例13: rsa_pkcs1_sign
/*
* Do an RSA operation to sign the message digest
*/
int rsa_pkcs1_sign( rsa_context *ctx,
int mode,
int hash_id,
int hashlen,
unsigned char *hash,
unsigned char *sig )
{
int nb_pad, olen;
unsigned char *p = sig;
olen = ctx->len;
switch( ctx->padding )
{
case RSA_PKCS_V15:
switch( hash_id )
{
case RSA_RAW:
nb_pad = olen - 3 - hashlen;
break;
case RSA_MD2:
case RSA_MD4:
case RSA_MD5:
nb_pad = olen - 3 - 34;
break;
case RSA_SHA1:
nb_pad = olen - 3 - 35;
break;
default:
return( XYSSL_ERR_RSA_BAD_INPUT_DATA );
}
if( nb_pad < 8 )
return( XYSSL_ERR_RSA_BAD_INPUT_DATA );
*p++ = 0;
*p++ = RSA_SIGN;
memset( p, 0xFF, nb_pad );
p += nb_pad;
*p++ = 0;
break;
default:
return( XYSSL_ERR_RSA_INVALID_PADDING );
}
switch( hash_id )
{
case RSA_RAW:
memcpy( p, hash, hashlen );
break;
case RSA_MD2:
memcpy( p, ASN1_HASH_MDX, 18 );
memcpy( p + 18, hash, 16 );
p[13] = 2; break;
case RSA_MD4:
memcpy( p, ASN1_HASH_MDX, 18 );
memcpy( p + 18, hash, 16 );
p[13] = 4; break;
case RSA_MD5:
memcpy( p, ASN1_HASH_MDX, 18 );
memcpy( p + 18, hash, 16 );
p[13] = 5; break;
case RSA_SHA1:
memcpy( p, ASN1_HASH_SHA1, 15 );
memcpy( p + 15, hash, 20 );
break;
default:
return( XYSSL_ERR_RSA_BAD_INPUT_DATA );
}
return( ( mode == RSA_PUBLIC )
? rsa_public( ctx, sig, sig )
: rsa_private( ctx, sig, sig ) );
}
示例14: rsa_rsassa_pss_sign
//.........这里部分代码省略.........
int ret;
size_t msb;
const md_info_t *md_info;
md_context_t md_ctx;
if( ctx->padding != RSA_PKCS_V21 || f_rng == NULL )
return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
olen = ctx->len;
switch( hash_id )
{
case SIG_RSA_MD2:
case SIG_RSA_MD4:
case SIG_RSA_MD5:
hashlen = 16;
break;
case SIG_RSA_SHA1:
hashlen = 20;
break;
case SIG_RSA_SHA224:
hashlen = 28;
break;
case SIG_RSA_SHA256:
hashlen = 32;
break;
case SIG_RSA_SHA384:
hashlen = 48;
break;
case SIG_RSA_SHA512:
hashlen = 64;
break;
default:
return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
}
md_info = md_info_from_type( ctx->hash_id );
if( md_info == NULL )
return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
hlen = md_get_size( md_info );
slen = hlen;
if( olen < hlen + slen + 2 )
return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
memset( sig, 0, olen );
msb = mpi_msb( &ctx->N ) - 1;
// Generate salt of length slen
//
if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 )
return( POLARSSL_ERR_RSA_RNG_FAILED + ret );
// Note: EMSA-PSS encoding is over the length of N - 1 bits
//
msb = mpi_msb( &ctx->N ) - 1;
p += olen - hlen * 2 - 2;
*p++ = 0x01;
memcpy( p, salt, slen );
p += slen;
md_init_ctx( &md_ctx, md_info );
// Generate H = Hash( M' )
//
md_starts( &md_ctx );
md_update( &md_ctx, p, 8 );
md_update( &md_ctx, hash, hashlen );
md_update( &md_ctx, salt, slen );
md_finish( &md_ctx, p );
// Compensate for boundary condition when applying mask
//
if( msb % 8 == 0 )
offset = 1;
// maskedDB: Apply dbMask to DB
//
mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen, &md_ctx );
md_free_ctx( &md_ctx );
msb = mpi_msb( &ctx->N ) - 1;
sig[0] &= 0xFF >> ( olen * 8 - msb );
p += hlen;
*p++ = 0xBC;
return( ( mode == RSA_PUBLIC )
? rsa_public( ctx, sig, sig )
: rsa_private( ctx, sig, sig ) );
}
示例15: rsa_rsaes_pkcs1_v15_decrypt
/*
* Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
*/
int rsa_rsaes_pkcs1_v15_decrypt( rsa_context *ctx,
int mode, size_t *olen,
const unsigned char *input,
unsigned char *output,
size_t output_max_len)
{
int ret, correct = 1;
size_t ilen, pad_count = 0;
unsigned char *p, *q;
unsigned char bt;
unsigned char buf[POLARSSL_MPI_MAX_SIZE];
if( ctx->padding != RSA_PKCS_V15 )
return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
ilen = ctx->len;
if( ilen < 16 || ilen > sizeof( buf ) )
return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
ret = ( mode == RSA_PUBLIC )
? rsa_public( ctx, input, buf )
: rsa_private( ctx, input, buf );
if( ret != 0 )
return( ret );
p = buf;
if( *p++ != 0 )
correct = 0;
bt = *p++;
if( ( bt != RSA_CRYPT && mode == RSA_PRIVATE ) ||
( bt != RSA_SIGN && mode == RSA_PUBLIC ) )
{
correct = 0;
}
if( bt == RSA_CRYPT )
{
while( *p != 0 && p < buf + ilen - 1 )
pad_count += ( *p++ != 0 );
correct &= ( *p == 0 && p < buf + ilen - 1 );
q = p;
// Also pass over all other bytes to reduce timing differences
//
while ( q < buf + ilen - 1 )
pad_count += ( *q++ != 0 );
// Prevent compiler optimization of pad_count
//
correct |= pad_count & 0x100000; /* Always 0 unless 1M bit keys */
p++;
}
else
{
while( *p == 0xFF && p < buf + ilen - 1 )
pad_count += ( *p++ == 0xFF );
correct &= ( *p == 0 && p < buf + ilen - 1 );
q = p;
// Also pass over all other bytes to reduce timing differences
//
while ( q < buf + ilen - 1 )
pad_count += ( *q++ != 0 );
// Prevent compiler optimization of pad_count
//
correct |= pad_count & 0x100000; /* Always 0 unless 1M bit keys */
p++;
}
if( correct == 0 )
return( POLARSSL_ERR_RSA_INVALID_PADDING );
if (ilen - (p - buf) > output_max_len)
return( POLARSSL_ERR_RSA_OUTPUT_TOO_LARGE );
*olen = ilen - (p - buf);
memcpy( output, p, *olen );
return( 0 );
}