本文整理汇总了C++中RAND_pseudo_bytes函数的典型用法代码示例。如果您正苦于以下问题:C++ RAND_pseudo_bytes函数的具体用法?C++ RAND_pseudo_bytes怎么用?C++ RAND_pseudo_bytes使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了RAND_pseudo_bytes函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: Q_UNUSED
qint32 CryptoUtils::padAESEncrypt (const char *from, qint32 fromLen, char *to, qint32 size) {
qint32 paddedSize = (fromLen + 15) & -16;
Q_UNUSED(size);
Q_ASSERT(fromLen > 0 && paddedSize <= size);
if (fromLen < paddedSize) {
qint32 isRandSupported = RAND_pseudo_bytes ((uchar *) from + fromLen, paddedSize - fromLen);
Q_UNUSED(isRandSupported);
Q_ASSERT(isRandSupported >= 0);
}
AES_ige_encrypt ((uchar *) from, (uchar *) to, paddedSize, &aes_key, aes_iv, AES_ENCRYPT);
// delete from;
// delete to;
return paddedSize;
}
示例2: rand_pseudo_bytes
void rand_pseudo_bytes(char* buf, int count)
{
static int init = init_openssl();
(void)init;
// RAND_pseudo_bytes is deprecated in favor of RAND_bytes as of OpenSSL 1.1.0
#if OPENSSL_VERSION_NUMBER < 0x10100000L
int result = RAND_pseudo_bytes((unsigned char*)buf, count);
if (result == -1)
FC_THROW("Error calling OpenSSL's RAND_pseudo_bytes(): ${code}", ("code", (uint32_t)ERR_get_error()));
#else
rand_bytes(buf, count);
#endif
}
示例3: px_get_pseudo_random_bytes
int
px_get_pseudo_random_bytes(uint8 *dst, unsigned count)
{
int res;
if (!openssl_random_init)
init_openssl_rand();
res = RAND_pseudo_bytes(dst, count);
if (res == 0 || res == 1)
return count;
return PXE_OSSL_RAND_ERROR;
}
示例4: ossl_rand_pseudo_bytes
/*
* call-seq:
* pseudo_bytes(length) -> aString
*
*/
static VALUE
ossl_rand_pseudo_bytes(VALUE self, SEL sel, VALUE len)
{
VALUE str;
int n = NUM2INT(len);
str = rb_bstr_new();
rb_bstr_resize(str, n);
if (!RAND_pseudo_bytes((unsigned char *)rb_bstr_bytes(str), n)) {
ossl_raise(eRandomError, NULL);
}
return str;
}
示例5: mech_step_accname
static int mech_step_accname(sasl_session_t *p, char *message, size_t len, char **out, size_t *out_len)
{
ecdsa_session_t *s = p->mechdata;
myuser_t *mu;
char *end;
unsigned char pubkey_raw[BUFSIZE];
const unsigned char *pubkey_raw_p;
metadata_t *md;
int ret;
memset(pubkey_raw, '\0', sizeof pubkey_raw);
end = memchr(message, '\0', len);
if (end == NULL)
p->username = sstrndup(message, len);
else
{
p->username = sstrndup(message, end-message);
p->authzid = sstrndup(end+1, len-1-(end-message));
}
mu = myuser_find_by_nick(p->username);
if (mu == NULL)
return ASASL_FAIL;
md = metadata_find(mu, "pubkey");
if (md == NULL)
return ASASL_FAIL;
ret = base64_decode(md->value, (char *)pubkey_raw, BUFSIZE);
if (ret == -1)
return ASASL_FAIL;
pubkey_raw_p = pubkey_raw;
o2i_ECPublicKey(&s->pubkey, &pubkey_raw_p, ret);
#ifndef DEBUG_STATIC_CHALLENGE_VECTOR
RAND_pseudo_bytes(s->challenge, CHALLENGE_LENGTH);
#else
memset(s->challenge, 'A', CHALLENGE_LENGTH);
#endif
*out = malloc(400);
memcpy(*out, s->challenge, CHALLENGE_LENGTH);
*out_len = CHALLENGE_LENGTH;
s->step = ECDSA_ST_RESPONSE;
return ASASL_MORE;
}
示例6: packetTestsuiteMsg
static int packetTestsuiteMsg(const int random_msg) {
unsigned char plbuf[packetTestsuite_PLBUF_SIZE];
unsigned char plbufdec[packetTestsuite_PLBUF_SIZE];
struct s_packet_data testdata = { .pl_buf_size = packetTestsuite_PLBUF_SIZE, .pl_buf = plbuf };
struct s_packet_data testdatadec = { .pl_buf_size = packetTestsuite_PLBUF_SIZE, .pl_buf = plbufdec };
unsigned char pkbuf[packetTestsuite_PKBUF_SIZE];
struct s_crypto ctx[2];
unsigned char secret[64];
unsigned char nonce[16];
struct s_seq_state seqstate;
char str[4096];
int len;
memset(secret, 23, 64);
memset(nonce, 5, 16);
cryptoCreate(ctx, 2);
if(!cryptoSetKeys(&ctx[0], 1, secret, 64, nonce, 16)) return 0;
if(!cryptoSetKeys(&ctx[1], 1, secret, 64, nonce, 16)) return 0;
seqInit(&seqstate, 0);
memset(plbuf, 0, packetTestsuite_PLBUF_SIZE);
if(random_msg) RAND_pseudo_bytes(plbuf, packetTestsuite_PLBUF_SIZE);
else strcpy((char *)plbuf, "moo");
len = packetTestsuite_PLBUF_SIZE;
testdata.pl_length = len;
testdata.pl_type = 0;
testdata.pl_options = 0;
testdata.peerid = plbuf[0];
testdata.seq = 1;
utilByteArrayToHexstring(str, 4096, plbuf, len);
printf("%s (len=%d, peerid=%d) -> ", str, len, testdata.peerid);
len = packetEncode(pkbuf, packetTestsuite_PKBUF_SIZE, &testdata, &ctx[0]);
if(!(len > 0)) return 0;
utilByteArrayToHexstring(str, 4096, pkbuf, len);
printf("%s (%d) -> ", str, len);
if(!(packetDecode(&testdatadec, pkbuf, len, &ctx[1], &seqstate))) return 0;
if(!(testdatadec.pl_length > 0)) return 0;
if(!(testdatadec.peerid == plbuf[0])) return 0;
if(!memcmp(testdatadec.pl_buf, testdata.pl_buf, packetTestsuite_PLBUF_SIZE) == 0) return 0;
utilByteArrayToHexstring(str, 4096, testdatadec.pl_buf, testdatadec.pl_length);
printf("%s (len=%d, peerid=%d)\n", str, testdatadec.pl_length, testdatadec.peerid);
cryptoDestroy(ctx, 2);
return 1;
}
示例7: get_serial
/* Generates a new serial number for a certificate. */
long get_serial()
{
long serial = read_serial();
if (serial == INVALID_SERIAL) { /* Read failed */
/* Generate a new serial */
RAND_pseudo_bytes((unsigned char*)&serial, sizeof(serial));
serial &= 0x0FFFFFFF; /* Fix sign and allow loads of serials before an overflow occurs */
RAND_cleanup();
write_serial(serial);
} else
write_serial(++serial); /* Update serial file */
return serial;
}
示例8: test_aes
void test_aes()
{
const int nAESKeyLen = 16;
unsigned char aesKey[nAESKeyLen + 1] = { 0 };
RAND_pseudo_bytes((unsigned char*)aesKey, nAESKeyLen);
std::string strKey((char*)aesKey, 16);
std::cout << "key: " << aesKey << std::endl << std::endl;
std::string strAesKey((char*)aesKey, nAESKeyLen);
std::string strNaked = "I am cswuyg.....";
std::cout << "nake: " << strNaked << std::endl;
std::string strEncode = Encryption::EncodeAES(strAesKey, strNaked);
std::cout << "encode: " << strEncode << std::endl;
std::string strDecode = Encryption::DecodeAES(strAesKey, strEncode);
std::cout << "decode " << strDecode << std::endl << std::endl;
}
示例9: __bro_openssl_rand_bytes
int
__bro_openssl_rand_bytes(u_char *buf, int num)
{
if (! buf || num <= 0)
return FALSE;
/* Make sure PRNG is initialized; has effect only once. */
prng_init();
if (RAND_bytes(buf, num) > 0)
return TRUE;
RAND_pseudo_bytes(buf, num);
return TRUE;
}
示例10: get_randomness
int get_randomness( unsigned char * buf, int length )
{
/* Seed OpenSSL PRNG with EGD enthropy pool -kre */
if (ConfigFileEntry.use_egd &&
(ConfigFileEntry.egdpool_path != NULL))
{
if (RAND_egd(ConfigFileEntry.egdpool_path) == -1)
return -1;
}
if ( RAND_status() )
return RAND_bytes( buf, length );
else /* XXX - abort? */
return RAND_pseudo_bytes( buf, length );
}
示例11: PKCS5_pbe_set0_algor
int
PKCS5_pbe_set0_algor(X509_ALGOR *algor, int alg, int iter,
const unsigned char *salt, int saltlen)
{
PBEPARAM *pbe = NULL;
ASN1_STRING *pbe_str = NULL;
unsigned char *sstr;
pbe = PBEPARAM_new();
if (!pbe) {
ASN1err(ASN1_F_PKCS5_PBE_SET0_ALGOR, ERR_R_MALLOC_FAILURE);
goto err;
}
if (iter <= 0)
iter = PKCS5_DEFAULT_ITER;
if (!ASN1_INTEGER_set(pbe->iter, iter)) {
ASN1err(ASN1_F_PKCS5_PBE_SET0_ALGOR, ERR_R_MALLOC_FAILURE);
goto err;
}
if (!saltlen)
saltlen = PKCS5_SALT_LEN;
if (!ASN1_STRING_set(pbe->salt, NULL, saltlen)) {
ASN1err(ASN1_F_PKCS5_PBE_SET0_ALGOR, ERR_R_MALLOC_FAILURE);
goto err;
}
sstr = ASN1_STRING_data(pbe->salt);
if (salt)
memcpy(sstr, salt, saltlen);
else if (RAND_pseudo_bytes(sstr, saltlen) < 0)
goto err;
if (!ASN1_item_pack(pbe, ASN1_ITEM_rptr(PBEPARAM), &pbe_str)) {
ASN1err(ASN1_F_PKCS5_PBE_SET0_ALGOR, ERR_R_MALLOC_FAILURE);
goto err;
}
PBEPARAM_free(pbe);
pbe = NULL;
if (X509_ALGOR_set0(algor, OBJ_nid2obj(alg), V_ASN1_SEQUENCE, pbe_str))
return 1;
err:
if (pbe != NULL)
PBEPARAM_free(pbe);
ASN1_STRING_free(pbe_str);
return 0;
}
示例12: encrypt_esp_packet
int encrypt_esp_packet(struct openconnect_info *vpninfo, struct pkt *pkt)
{
int i, padlen;
const int blksize = 16;
unsigned int hmac_len = 20;
int crypt_len;
HMAC_CTX hmac_ctx;
/* This gets much more fun if the IV is variable-length */
pkt->esp.spi = vpninfo->esp_out.spi;
pkt->esp.seq = htonl(vpninfo->esp_out.seq++);
if (!RAND_pseudo_bytes((void *)&pkt->esp.iv, sizeof(pkt->esp.iv))) {
vpn_progress(vpninfo, PRG_ERR,
_("Failed to generate random IV for ESP packet:\n"));
openconnect_report_ssl_errors(vpninfo);
return -EIO;
}
padlen = blksize - 1 - ((pkt->len + 1) % blksize);
for (i=0; i<padlen; i++)
pkt->data[pkt->len + i] = i + 1;
pkt->data[pkt->len + padlen] = padlen;
pkt->data[pkt->len + padlen + 1] = 0x04; /* Legacy IP */
if (!EVP_EncryptInit_ex(&vpninfo->esp_out.cipher, NULL, NULL, NULL,
pkt->esp.iv)) {
vpn_progress(vpninfo, PRG_ERR,
_("Failed to set up encryption context for ESP packet:\n"));
openconnect_report_ssl_errors(vpninfo);
return -EINVAL;
}
crypt_len = pkt->len + padlen + 2;
if (!EVP_EncryptUpdate(&vpninfo->esp_out.cipher, pkt->data, &crypt_len,
pkt->data, crypt_len)) {
vpn_progress(vpninfo, PRG_ERR,
_("Failed to encrypt ESP packet:\n"));
openconnect_report_ssl_errors(vpninfo);
return -EINVAL;
}
HMAC_CTX_copy(&hmac_ctx, &vpninfo->esp_out.hmac);
HMAC_Update(&hmac_ctx, (void *)&pkt->esp, sizeof(pkt->esp) + crypt_len);
HMAC_Final(&hmac_ctx, pkt->data + crypt_len, &hmac_len);
HMAC_CTX_cleanup(&hmac_ctx);
return sizeof(pkt->esp) + crypt_len + 12;
}
示例13: csf_write_page
static size_t csf_write_page(CSF_CTX *ctx, int pgno, void *data, size_t data_sz) {
off_t start_offset = HDR_SZ + (pgno * ctx->page_sz);
off_t cur_offset = lseek(*ctx->fh, 0L, SEEK_CUR);
int to_write = ctx->page_sz;
size_t write_sz = 0;
CSF_PAGE_HEADER header;
assert(data_sz <= ctx->data_sz);
header.data_sz = data_sz;
if(cur_offset != start_offset) { /* if not in proper position for page, seek there */
cur_offset = lseek(*ctx->fh, start_offset, SEEK_SET);
}
RAND_pseudo_bytes(ctx->page_buffer, ctx->iv_sz);
memcpy(ctx->scratch_buffer, &header, sizeof(header));
memcpy(ctx->scratch_buffer + ctx->page_header_sz, data, data_sz);
/* normally this would encrypt here */
if(ctx->encrypted) {
EVP_CIPHER_CTX ectx;
void *out_ptr = ctx->page_buffer + ctx->iv_sz;
int out_sz, cipher_sz = 0;
EVP_CipherInit(&ectx, CIPHER, NULL, NULL, 1);
EVP_CIPHER_CTX_set_padding(&ectx, 0);
EVP_CipherInit(&ectx, NULL, ctx->key_data, ctx->page_buffer, 1);
EVP_CipherUpdate(&ectx, out_ptr + cipher_sz, &out_sz, ctx->scratch_buffer, ctx->page_header_sz + ctx->data_sz);
cipher_sz += out_sz;
EVP_CipherFinal(&ectx, out_ptr + cipher_sz, &out_sz);
cipher_sz += out_sz;
EVP_CIPHER_CTX_cleanup(&ectx);
assert(cipher_sz == (ctx->page_header_sz + ctx->data_sz));
} else {
memcpy(ctx->page_buffer + ctx->iv_sz, ctx->scratch_buffer, ctx->page_header_sz + ctx->data_sz);
}
for(;write_sz < to_write;) { /* FIXME - error handling */
size_t bytes_write = write(*ctx->fh, ctx->page_buffer + write_sz, to_write - write_sz);
write_sz += bytes_write;
}
TRACE6("csf_write_page(%d,%d,x,%d), cur_offset=%d, write_sz= %d\n", *ctx->fh, pgno, data_sz, cur_offset, write_sz);
return data_sz;
}
示例14: g_dir_make_tmp
// create max_files and fill with random data
// return list of {file name, content md5}
static GList *populate_file_list (gint max_files, GList *l_files, gchar *in_dir)
{
gint i;
gchar *out_dir;
GError *error = NULL;
FileData *fdata;
gchar *name;
FILE *f;
out_dir = g_dir_make_tmp (NULL, &error);
g_assert (out_dir);
LOG_debug (POOL_TEST, "In dir: %s Out dir: %s", in_dir, out_dir);
for (i = 0; i < max_files; i++) {
char *bytes;
size_t bytes_len;
fdata = g_new0 (FileData, 1);
fdata->checked = FALSE;
bytes_len = g_random_int_range (100000, 1000000);
bytes = g_malloc (bytes_len + 1);
RAND_pseudo_bytes ((unsigned char *)bytes, bytes_len);
*(bytes + bytes_len) = '\0';
name = get_random_string (15, TRUE);
fdata->in_name = g_strdup_printf ("%s/%s", in_dir, name);
f = fopen (fdata->in_name, "w");
fwrite (bytes, 1, bytes_len + 1, f);
fclose (f);
fdata->out_name = g_strdup_printf ("%s/%s", out_dir, name);
get_md5_sum (bytes, bytes_len + 1, &fdata->md5, NULL);
fdata->fout = fopen (fdata->out_name, "w");
g_assert (fdata->fout);
fdata->url = g_strdup_printf ("http://127.0.0.1:8011/%s", name);
g_assert (fdata->url);
LOG_debug (POOL_TEST, "%s -> %s, size: %u", fdata->in_name, fdata->md5, bytes_len);
l_files = g_list_append (l_files, fdata);
}
return l_files;
}
示例15: send_challenge_user
static void send_challenge_user(CcnetProcessor *processor, CcnetUser *user)
{
CcnetKeepaliveProcPriv *priv = GET_PRIV (processor);
unsigned char *buf;
int len;
ccnet_debug ("[Keepalive] Send user challenge to %.8s\n",
processor->peer->id);
RAND_pseudo_bytes (priv->random_buf, 40);
buf = public_key_encrypt (user->pubkey, priv->random_buf, 40, &len);
ccnet_processor_send_update (processor, "321", NULL, (char *)buf, len);
g_free(buf);
processor->state = WAIT_CHALLENGE_USER;
reset_timeout (processor);
}