本文整理汇总了C++中rsa_free函数的典型用法代码示例。如果您正苦于以下问题:C++ rsa_free函数的具体用法?C++ rsa_free怎么用?C++ rsa_free使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了rsa_free函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: rsa_genkey
uint8_t *
rsa_genkey (void)
{
int r;
uint8_t index = 0;
uint8_t *p_q_modulus = (uint8_t *)malloc (KEY_CONTENT_LEN*2);
uint8_t *p = p_q_modulus;
uint8_t *q = p_q_modulus + KEY_CONTENT_LEN/2;
uint8_t *modulus = p_q_modulus + KEY_CONTENT_LEN;
if (p_q_modulus == NULL)
return NULL;
rsa_init (&rsa_ctx, RSA_PKCS_V15, 0);
r = rsa_gen_key (&rsa_ctx, random_byte, &index,
KEY_CONTENT_LEN * 8, RSA_EXPONENT);
if (r < 0)
{
free (p_q_modulus);
rsa_free (&rsa_ctx);
return NULL;
}
mpi_write_binary (&rsa_ctx.P, p, KEY_CONTENT_LEN/2);
mpi_write_binary (&rsa_ctx.Q, q, KEY_CONTENT_LEN/2);
mpi_write_binary (&rsa_ctx.N, modulus, KEY_CONTENT_LEN);
rsa_free (&rsa_ctx);
return p_q_modulus;
}
示例2: _vcrypt_load_keys
int _vcrypt_load_keys(VCRYPT_CTX *ctx, const char *file, char *checksum)
{
FILE *f = fopen(file, "rb");
if (f == NULL ) {
ctx->has_valid_keys_locally = 0;
return -ERR_FILE_READ;
}
uint8_t keydata[4096];
int keylen = fread(keydata, 1, sizeof keydata, f);
if (keylen <= 0) {
fclose(f);
return -ERR_FILE_READ;
}
fclose(f);
ctx->has_valid_keys_locally = 0; // the next will invalidate them
rsa_free(&ctx->ssl_req.rsa);
int ret = x509parse_key(&ctx->ssl_req.rsa, keydata, keylen, NULL, 0);
if (ret == 0)
vcrypt_get_key_fingerprint_ctx(ctx, checksum);
ctx->has_valid_keys_locally = 1;
return ret == 0 ? 0 : -ERR_RSA_ERROR_LOADING_KEYS;
}
示例3: main
int main(int argc, char **argv) {
int ret;
rsa_t rsa;
uint32_t mc, vf;
datum_t em, m;
uint8_t EM[256];
mlockall(MCL_CURRENT|MCL_FUTURE);
rsa_init(&rsa);
mc = vf = 0;
em.data = (uint8_t *)EM;
em.size = (uint32_t)sizeof(EM);
/*
generate these with gentests.pl
NOTE: in some cases, the RSA signing operation will produce a signature which
is 1 or more bytes less in length than N. In these cases, it must be padded
on the left with zeros.
*/
#include "tests.c"
rsa_free(&rsa);
munlockall();
printf("\nTest run completed with %d miscompares and %d verification failures.\n\n", mc, vf);
return 0;
}
示例4: entropy_init
/*
rsa oaep encryption
*/
unsigned char *rsacrypt(pk_context *pkctx,const unsigned char *plaintext,const unsigned int plaintextsize){
entropy_context entropy = {0};
ctr_drbg_context ctr_drbg = {0};
rsa_context rsactx = {0};
int pkresult = 0;
unsigned char *encryptedoutput = NULL;
unsigned int encryptedoutputsize = 0;
char pers[33] = "3s:!2OXI(FX%#Q($[CEjiGRIk\\-)4e&?";
int ret = 0;
entropy_init( &entropy );
if((ret = ctr_drbg_init(&ctr_drbg, entropy_func, &entropy, (unsigned char *)&pers[0],strlen(pers))) != 0 ){
outputerror(DBG_ERROR,"%s\n","rsacrypt::failed to initialize random generator");
return NULL;
}
encryptedoutputsize = pk_get_len(pkctx);
outputerror(DBG_INFO,"%s %Iu\n","rsacrypt::buffer size for rsa encrypted output ",encryptedoutputsize);
encryptedoutput = (unsigned char *)malloc(encryptedoutputsize);
SecureZeroMemory(encryptedoutput,encryptedoutputsize);
rsa_copy(&rsactx,pkctx->pk_ctx);
rsactx.padding = RSA_PKCS_V21;
rsactx.hash_id = POLARSSL_MD_SHA1;
pkresult = 0;
pkresult = rsa_rsaes_oaep_encrypt(&rsactx,ctr_drbg_random,&ctr_drbg,RSA_PUBLIC,"cryptoshot",strlen("cryptoshot"),plaintextsize,plaintext,encryptedoutput);
if(pkresult != 0){
outputerror(DBG_ERROR,"%s %i\n","rsacrypt::failed to encrypt data",pkresult);
return NULL;
}
entropy_free(&entropy);
rsa_free(&rsactx);
return encryptedoutput;
}
示例5: d
/**
Import RSA key from raw numbers
@param N RSA's N
@param Nlen RSA's N's length
@param e RSA's e
@param elen RSA's e's length
@param d RSA's d (only private key, NULL for public key)
@param dlen RSA's d's length
@param key [out] the destination for the imported key
@return CRYPT_OK if successful
*/
int rsa_set_key(const unsigned char *N, unsigned long Nlen,
const unsigned char *e, unsigned long elen,
const unsigned char *d, unsigned long dlen,
rsa_key *key)
{
int err;
LTC_ARGCHK(key != NULL);
LTC_ARGCHK(N != NULL);
LTC_ARGCHK(e != NULL);
LTC_ARGCHK(ltc_mp.name != NULL);
err = mp_init_multi(&key->e, &key->d, &key->N, &key->dQ, &key->dP, &key->qP, &key->p, &key->q, NULL);
if (err != CRYPT_OK) return err;
if ((err = mp_read_unsigned_bin(key->N , (unsigned char *)N , Nlen)) != CRYPT_OK) { goto LBL_ERR; }
if ((err = mp_read_unsigned_bin(key->e , (unsigned char *)e , elen)) != CRYPT_OK) { goto LBL_ERR; }
if (d && dlen) {
if ((err = mp_read_unsigned_bin(key->d , (unsigned char *)d , dlen)) != CRYPT_OK) { goto LBL_ERR; }
key->type = PK_PRIVATE;
}
else {
key->type = PK_PUBLIC;
}
return CRYPT_OK;
LBL_ERR:
rsa_free(key);
return err;
}
示例6: rsa_verify
int
rsa_verify (const uint8_t *pubkey, const uint8_t *hash, const uint8_t *sig)
{
int r;
rsa_init (&rsa_ctx, RSA_PKCS_V15, 0);
rsa_ctx.len = KEY_CONTENT_LEN;
mpi_lset (&rsa_ctx.E, 0x10001);
mpi_read_binary (&rsa_ctx.N, pubkey, KEY_CONTENT_LEN);
DEBUG_INFO ("RSA verify...");
r = rsa_pkcs1_verify (&rsa_ctx, RSA_PUBLIC, SIG_RSA_SHA256, 32, hash, sig);
rsa_free (&rsa_ctx);
if (r < 0)
{
DEBUG_INFO ("fail:");
DEBUG_SHORT (r);
return r;
}
else
{
DEBUG_INFO ("verified.\r\n");
return 0;
}
}
示例7: rsa_test
bool
rsa_test(unsigned bits, mt64_context *rand_ctx)
{
mt64_init_u64(rand_ctx, 1234567890U);
rsa_ctx rsa;
if (!rsa_init_keygen(&rsa, bits, rand_ctx))
return false;
ASSERT(rsa.n != NULL);
ASSERT(rsa.phi != NULL);
ASSERT(rsa.e != NULL);
ASSERT(rsa.d != NULL);
mpi_t m = MPI_INITIALIZER;
mpi_mul(rsa.e, rsa.d, m);
mpi_mod(m, rsa.phi, m);
bool result = true;
if (!mpi_is_one(m)) {
printf("N: "), mpi_print_dec(rsa.n), printf("\n");
printf("E: "), mpi_print_dec(rsa.e), printf("\n");
printf("D: "), mpi_print_dec(rsa.d), printf("\n");
printf("Φ: "), mpi_print_dec(rsa.phi), printf("\n");
printf("E * D mod Φ: "), mpi_print_dec(m), printf("\n");
result = false;
}
mpi_free(m);
rsa_free(&rsa);
return result;
}
示例8: rsa_copy
/*
* Copy the components of an RSA key
*/
int rsa_copy( rsa_context *dst, const rsa_context *src )
{
int ret;
dst->ver = src->ver;
dst->len = src->len;
MPI_CHK( mpi_copy( &dst->N, &src->N ) );
MPI_CHK( mpi_copy( &dst->E, &src->E ) );
MPI_CHK( mpi_copy( &dst->D, &src->D ) );
MPI_CHK( mpi_copy( &dst->P, &src->P ) );
MPI_CHK( mpi_copy( &dst->Q, &src->Q ) );
MPI_CHK( mpi_copy( &dst->DP, &src->DP ) );
MPI_CHK( mpi_copy( &dst->DQ, &src->DQ ) );
MPI_CHK( mpi_copy( &dst->QP, &src->QP ) );
MPI_CHK( mpi_copy( &dst->RN, &src->RN ) );
MPI_CHK( mpi_copy( &dst->RP, &src->RP ) );
MPI_CHK( mpi_copy( &dst->RQ, &src->RQ ) );
#if !defined(POLARSSL_RSA_NO_CRT)
MPI_CHK( mpi_copy( &dst->Vi, &src->Vi ) );
MPI_CHK( mpi_copy( &dst->Vf, &src->Vf ) );
#endif
dst->padding = src->padding;
dst->hash_id = src->hash_id;
cleanup:
if( ret != 0 )
rsa_free( dst );
return( ret );
}
示例9: FinalizeAsymmetricCipher
ALWAYS_INLINE void
FinalizeAsymmetricCipher( JS::HandleObject obj, bool wipe ) {
AsymmetricCipherPrivate *pv = (AsymmetricCipherPrivate*)JL_GetPrivate(obj);
if ( pv ) {
if ( pv->hasKey ) {
switch ( pv->cipher ) {
case rsa:
rsa_free( &pv->key.rsaKey );
break;
case ecc:
ecc_free( &pv->key.eccKey );
break;
case dsa:
dsa_free( &pv->key.dsaKey );
break;
#ifdef MKAT
case katja:
katja_free( &pv->key.katjaKey );
break;
#endif
}
}
if ( wipe )
zeromem(pv, sizeof(AsymmetricCipherPrivate));
jl_free(pv);
}
}
示例10: VerifyWeakSignature
static DWORD VerifyWeakSignature(
TMPQArchive * ha,
PMPQ_SIGNATURE_INFO pSI)
{
BYTE RevSignature[MPQ_WEAK_SIGNATURE_SIZE];
BYTE Md5Digest[MD5_DIGEST_SIZE];
rsa_key key;
int hash_idx = find_hash("md5");
int result = 0;
// Calculate hash of the entire archive, skipping the (signature) file
if(!CalculateMpqHashMd5(ha, pSI, Md5Digest))
return ERROR_VERIFY_FAILED;
// Import the Blizzard key in OpenSSL format
if(!decode_base64_key(szBlizzardWeakPublicKey, &key))
return ERROR_VERIFY_FAILED;
// Verify the signature
memcpy(RevSignature, &pSI->Signature[8], MPQ_WEAK_SIGNATURE_SIZE);
memrev(RevSignature, MPQ_WEAK_SIGNATURE_SIZE);
rsa_verify_hash_ex(RevSignature, MPQ_WEAK_SIGNATURE_SIZE, Md5Digest, sizeof(Md5Digest), LTC_LTC_PKCS_1_V1_5, hash_idx, 0, &result, &key);
rsa_free(&key);
// Return the result
return result ? ERROR_WEAK_SIGNATURE_OK : ERROR_WEAK_SIGNATURE_ERROR;
}
示例11: rsa_genkey
static int rsa_genkey (lua_State *L) {
rsa_context rsa;
havege_state hs;
int ret=0;
rsa_init( &rsa, RSA_PKCS_V15, 0, havege_rand, &hs );
if( ( ret = rsa_gen_key( &rsa, KEY_SIZE, EXPONENT ) ) != 0 )
{
luaL_error(L, "Error generating key (%d)", ret);
}
/* Public Key */
if(ret = push_public_key(L, &rsa))
{
luaL_error(L, "failed to obtain public key: error %d", ret );
}
/* Private Key */
if(ret = push_private_key(L, &rsa))
{
luaL_error(L, "failed to obtain private key: error %d", ret );
}
rsa_free( &rsa );
return 2;
}
示例12: cleanup_crypt
void cleanup_crypt(void)
{
/* this never gets called because we never cleanly exit, but
here it is for completeness */
rsa_free(&key);
unregister_prng(&yarrow_desc);
}
示例13: main
int main(int argc, char **argv)
{
int rc = 0;
prng_state prng;
int prng_index, hash_index;
rsa_key key;
int i;
ltc_mp = tfm_desc;
prng_index = register_prng(&sprng_desc); /* (fortuna_desc is a good choice if your platform's PRNG sucks.) */
if (prng_index == -1) {
fail("Failed to register a RNG");
}
hash_index = register_hash(&sha256_desc);
if (hash_index == -1) {
fail("Failed to register sha256 hasher");
}
if ((rc = rng_make_prng(128, prng_index, &prng, NULL)) != CRYPT_OK) {
fail("rng_make_prng failed: %s", error_to_string(rc));
}
read_rsakey(&key, "privatekey.bin");
for (i = 1; i < argc; i++) {
sign_file(argv[i], &key, &prng, prng_index, hash_index);
}
rsa_free(&key);
return 0;
}
示例14: memcpy
int SparkProtocol::handshake(void)
{
memcpy(queue + 40, device_id, 12);
int err = blocking_receive(queue, 40);
if (0 > err) return err;
parse_device_pubkey_from_privkey(queue+52, core_private_key);
rsa_context rsa;
init_rsa_context_with_public_key(&rsa, server_public_key);
const int len = 52+MAX_DEVICE_PUBLIC_KEY_LENGTH;
err = rsa_pkcs1_encrypt(&rsa, RSA_PUBLIC, len, queue, queue + len);
rsa_free(&rsa);
if (err) return err;
blocking_send(queue + len, 256);
err = blocking_receive(queue, 384);
if (0 > err) return err;
err = set_key(queue);
if (err) return err;
queue[0] = 0x00;
queue[1] = 0x10;
hello(queue + 2, descriptor.was_ota_upgrade_successful());
err = blocking_send(queue, 18);
if (0 > err) return err;
if (!event_loop()) // read the hello message from the server
return -1;
return 0;
}
示例15: free_connection
void free_connection(connection_t *c) {
if(!c)
return;
cipher_close(c->incipher);
digest_close(c->indigest);
cipher_close(c->outcipher);
digest_close(c->outdigest);
sptps_stop(&c->sptps);
ecdsa_free(c->ecdsa);
rsa_free(c->rsa);
free(c->hischallenge);
buffer_clear(&c->inbuf);
buffer_clear(&c->outbuf);
io_del(&c->io);
if(c->socket > 0)
closesocket(c->socket);
free(c->name);
free(c->hostname);
if(c->config_tree)
exit_configuration(&c->config_tree);
free(c);
}