本文整理汇总了C++中EVP_CipherInit函数的典型用法代码示例。如果您正苦于以下问题:C++ EVP_CipherInit函数的具体用法?C++ EVP_CipherInit怎么用?C++ EVP_CipherInit使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了EVP_CipherInit函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: cipher_init
void
cipher_init(CipherContext *cc, Cipher *cipher,
const u_char *key, u_int keylen, const u_char *iv, u_int ivlen,
int encrypt)
{
static int dowarn = 1;
#ifdef SSH_OLD_EVP
EVP_CIPHER *type;
#else
const EVP_CIPHER *type;
#endif
int klen;
if (cipher->number == SSH_CIPHER_DES) {
if (dowarn) {
error("Warning: use of DES is strongly discouraged "
"due to cryptographic weaknesses");
dowarn = 0;
}
if (keylen > 8)
keylen = 8;
}
cc->plaintext = (cipher->number == SSH_CIPHER_NONE);
if (keylen < cipher->key_len)
fatal("cipher_init: key length %d is insufficient for %s.",
keylen, cipher->name);
if (iv != NULL && ivlen < cipher->block_size)
fatal("cipher_init: iv length %d is insufficient for %s.",
ivlen, cipher->name);
cc->cipher = cipher;
type = (*cipher->evptype)();
EVP_CIPHER_CTX_init(&cc->evp);
#ifdef SSH_OLD_EVP
if (type->key_len > 0 && type->key_len != keylen) {
debug("cipher_init: set keylen (%d -> %d)",
type->key_len, keylen);
type->key_len = keylen;
}
EVP_CipherInit(&cc->evp, type, (u_char *)key, (u_char *)iv,
(encrypt == CIPHER_ENCRYPT));
#else
if (EVP_CipherInit(&cc->evp, type, NULL, (u_char *)iv,
(encrypt == CIPHER_ENCRYPT)) == 0)
fatal("cipher_init: EVP_CipherInit failed for %s",
cipher->name);
klen = EVP_CIPHER_CTX_key_length(&cc->evp);
if (klen > 0 && keylen != klen) {
debug("cipher_init: set keylen (%d -> %d)", klen, keylen);
if (EVP_CIPHER_CTX_set_key_length(&cc->evp, keylen) == 0)
fatal("cipher_init: set keylen failed (%d -> %d)",
klen, keylen);
}
if (EVP_CipherInit(&cc->evp, NULL, (u_char *)key, NULL, -1) == 0)
fatal("cipher_init: EVP_CipherInit: set key failed for %s",
cipher->name);
#endif
}
示例2: cipher_init
void
cipher_init(CipherContext *cc, Cipher *cipher,
const u_char *key, u_int keylen, const u_char *iv, u_int ivlen,
int do_encrypt)
{
static int dowarn = 1;
const EVP_CIPHER *type;
int klen;
u_char *junk, *discard;
if (cipher->number == SSH_CIPHER_DES) {
if (dowarn) {
error("Warning: use of DES is strongly discouraged "
"due to cryptographic weaknesses");
dowarn = 0;
}
if (keylen > 8)
keylen = 8;
}
cc->plaintext = (cipher->number == SSH_CIPHER_NONE);
if (keylen < cipher->key_len)
fatal("cipher_init: key length %d is insufficient for %s.",
keylen, cipher->name);
if (iv != NULL && ivlen < cipher->block_size)
fatal("cipher_init: iv length %d is insufficient for %s.",
ivlen, cipher->name);
cc->cipher = cipher;
type = (*cipher->evptype)();
EVP_CIPHER_CTX_init(&cc->evp);
if (EVP_CipherInit(&cc->evp, type, NULL, __UNCONST(iv),
(do_encrypt == CIPHER_ENCRYPT)) == 0)
fatal("cipher_init: EVP_CipherInit failed for %s",
cipher->name);
klen = EVP_CIPHER_CTX_key_length(&cc->evp);
if (klen > 0 && keylen != (u_int)klen) {
debug2("cipher_init: set keylen (%d -> %d)", klen, keylen);
if (EVP_CIPHER_CTX_set_key_length(&cc->evp, keylen) == 0)
fatal("cipher_init: set keylen failed (%d -> %d)",
klen, keylen);
}
if (EVP_CipherInit(&cc->evp, NULL, __UNCONST(key), NULL, -1) == 0)
fatal("cipher_init: EVP_CipherInit: set key failed for %s",
cipher->name);
if (cipher->discard_len > 0) {
junk = xmalloc(cipher->discard_len);
discard = xmalloc(cipher->discard_len);
if (EVP_Cipher(&cc->evp, discard, junk,
cipher->discard_len) == 0)
fatal("evp_crypt: EVP_Cipher failed during discard");
memset(discard, 0, cipher->discard_len);
xfree(junk);
xfree(discard);
}
}
示例3: _libssh2_cipher_init
int
_libssh2_cipher_init(_libssh2_cipher_ctx * h,
_libssh2_cipher_type(algo),
unsigned char *iv, unsigned char *secret, int encrypt)
{
#ifdef HAVE_OPAQUE_STRUCTS
*h = EVP_CIPHER_CTX_new();
return !EVP_CipherInit(*h, algo(), secret, iv, encrypt);
#else
EVP_CIPHER_CTX_init(h);
return !EVP_CipherInit(h, algo(), secret, iv, encrypt);
#endif
}
示例4: tls_process_record_data
int
tls_process_record_data(struct SSLConnection *conn, const opaque *fragment,
const int len, uint8_t **out, uint32_t *outl)
{
EVP_CIPHER_CTX *evp;
uint8_t pad;
size_t flen = len;
tls_debug_print_hex("Ciphertext", fragment, len);
if (conn->direction == 0) {
evp = &conn->client_cipher_ctx;
} else {
evp = &conn->server_cipher_ctx;
}
// TLS 1.1 and later extract explicit IV
if (conn->version >= 2 && len > 16) {
if (conn->direction == 0) {
EVP_CipherInit(evp, conn->ciph,
conn->key_material.client_write_key,
fragment, 0);
} else {
EVP_CipherInit(evp, conn->ciph,
conn->key_material.server_write_key,
fragment, 0);
}
flen -= 16;
fragment += 16;
}
size_t dlen = len;
uint8_t *decoded = sng_malloc(dlen);
EVP_Cipher(evp, decoded, (unsigned char *) fragment, flen);
tls_debug_print_hex("Plaintext", decoded, flen);
// Get padding counter and remove from data
pad = decoded[flen - 1];
dlen = flen - (pad + 1);
tls_debug_print_hex("Mac", decoded + (dlen - 20), 20);
if ((int32_t)dlen > 0 && dlen <= *outl) {
memcpy(*out, decoded, dlen);
*outl = dlen - 20 /* Trailing MAC */;
}
// Clenaup decoded memory
sng_free(decoded);
return *outl;
}
示例5: 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;
}
示例6: ssh_EVP_CipherInit
int
ssh_EVP_CipherInit(EVP_CIPHER_CTX *evp, const EVP_CIPHER *type,
unsigned char *key, unsigned char *iv, int enc)
{
EVP_CipherInit(evp, type, key, iv, enc);
return 1;
}
示例7: sqlcipher_openssl_cipher
static int sqlcipher_openssl_cipher(void *ctx, int mode, unsigned char *key, int key_sz, unsigned char *iv, unsigned char *in, int in_sz, unsigned char *out) {
EVP_CIPHER_CTX ectx;
int tmp_csz, csz;
EVP_CipherInit(&ectx, ((openssl_ctx *)ctx)->evp_cipher, NULL, NULL, mode);
EVP_CIPHER_CTX_set_padding(&ectx, 0); // no padding
EVP_CipherInit(&ectx, NULL, key, iv, mode);
EVP_CipherUpdate(&ectx, out, &tmp_csz, in, in_sz);
csz = tmp_csz;
out += tmp_csz;
EVP_CipherFinal(&ectx, out, &tmp_csz);
csz += tmp_csz;
EVP_CIPHER_CTX_cleanup(&ectx);
assert(in_sz == csz);
return SQLITE_OK;
}
示例8: cipher_ctx_init
void
cipher_ctx_init(EVP_CIPHER_CTX *ctx, const uint8_t *key, int key_len,
const EVP_CIPHER *kt, int enc)
{
ASSERT(NULL != kt && NULL != ctx);
EVP_CIPHER_CTX_init(ctx);
if (!EVP_CipherInit(ctx, kt, NULL, NULL, enc))
{
crypto_msg(M_FATAL, "EVP cipher init #1");
}
#ifdef HAVE_EVP_CIPHER_CTX_SET_KEY_LENGTH
if (!EVP_CIPHER_CTX_set_key_length(ctx, key_len))
{
crypto_msg(M_FATAL, "EVP set key size");
}
#endif
if (!EVP_CipherInit_ex(ctx, NULL, NULL, key, NULL, enc))
{
crypto_msg(M_FATAL, "EVP cipher init #2");
}
/* make sure we used a big enough key */
ASSERT(EVP_CIPHER_CTX_key_length(ctx) <= key_len);
}
示例9: encryptfile
void encryptfile(FILE * fpin,FILE* fpout,unsigned char* key, unsigned char* iv)
{
//Using openssl EVP to encrypt a file
const unsigned bufsize = 4096;
unsigned char* read_buf = malloc(bufsize);
unsigned char* cipher_buf ;
unsigned blocksize;
int out_len;
EVP_CIPHER_CTX ctx;
EVP_CipherInit(&ctx,EVP_aes_256_cbc(),key,iv,1);
blocksize = EVP_CIPHER_CTX_block_size(&ctx);
cipher_buf = malloc(bufsize+blocksize);
// read file and write encrypted file until eof
while(1)
{
int bytes_read = fread(read_buf,sizeof(unsigned char),bufsize,fpin);
EVP_CipherUpdate(&ctx,cipher_buf,&out_len,read_buf, bytes_read);
fwrite(cipher_buf,sizeof(unsigned char),out_len,fpout);
if(bytes_read < bufsize)
{
break;//EOF
}
}
EVP_CipherFinal(&ctx,cipher_buf,&out_len);
fwrite(cipher_buf,sizeof(unsigned char),out_len,fpout);
free(cipher_buf);
free(read_buf);
}
示例10: _libssh2_cipher_init
int
_libssh2_cipher_init(_libssh2_cipher_ctx * h,
_libssh2_cipher_type(algo),
unsigned char *iv, unsigned char *secret, int encrypt)
{
EVP_CIPHER_CTX_init(h);
return !EVP_CipherInit(h, algo(), secret, iv, encrypt);
}
示例11: csf_read_page
static size_t csf_read_page(CSF_CTX *ctx, int pgno, void *data) {
off_t start_offset = HDR_SZ + (pgno * ctx->page_sz);
off_t cur_offset = lseek(*ctx->fh, 0L, SEEK_CUR);
int to_read = ctx->page_sz;
size_t read_sz = 0;
CSF_PAGE_HEADER header;
if(cur_offset != start_offset) { /* if not in proper position for page, seek there */
cur_offset = lseek(*ctx->fh, start_offset, SEEK_SET);
}
/* FIXME - error handling */
for(;read_sz < to_read;) {
size_t bytes_read = read(*ctx->fh, ctx->page_buffer + read_sz, to_read - read_sz);
read_sz += bytes_read;
if(bytes_read < 0) {
return 0;
}
}
if(ctx->encrypted) {
EVP_CIPHER_CTX ectx;
void *out_ptr = ctx->scratch_buffer;
int out_sz, cipher_sz = 0;
EVP_CipherInit(&ectx, CIPHER, NULL, NULL, 0);
EVP_CIPHER_CTX_set_padding(&ectx, 0);
EVP_CipherInit(&ectx, NULL, ctx->key_data, ctx->page_buffer, 0);
EVP_CipherUpdate(&ectx, out_ptr + cipher_sz, &out_sz, ctx->page_buffer + ctx->iv_sz, 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->scratch_buffer, ctx->page_buffer + ctx->iv_sz, ctx->page_header_sz + ctx->data_sz);
}
memcpy(&header, ctx->scratch_buffer, sizeof(header));
memcpy(data, ctx->scratch_buffer + ctx->page_header_sz, header.data_sz);
TRACE6("csf_read_page(%d,%d,x), cur_offset=%d, read_sz=%d, return=%d\n", *ctx->fh, pgno, cur_offset, read_sz, data_sz);
return header.data_sz;
}
示例12: codec_cipher
/*
* ctx - codec context
* pgno - page number in database
* size - size in bytes of input and output buffers
* mode - 1 to encrypt, 0 to decrypt
* in - pointer to input bytes
* out - pouter to output bytes
*/
static int codec_cipher(codec_ctx *ctx, Pgno pgno, int mode, int size, void *in, void *out) {
EVP_CIPHER_CTX ectx;
void *iv;
int tmp_csz, csz;
/* when this is an encryption operation and rekey is not null, we will actually encrypt
** data with the new rekey data */
void *key = ((mode == CIPHER_ENCRYPT && ctx->rekey != NULL) ? ctx->rekey : ctx->key);
/* just copy raw data from in to out whenever
** 1. key is NULL; or
** 2. this is a decrypt operation and rekey_plaintext is true
*/
if(key == NULL || (mode==CIPHER_DECRYPT && ctx->rekey_plaintext)) {
memcpy(out, in, size);
return SQLITE_OK;
}
size = size - ctx->iv_sz; /* adjust size to useable size and memset reserve at end of page */
iv = out + size;
if(mode == CIPHER_ENCRYPT) {
RAND_pseudo_bytes(iv, ctx->iv_sz);
} else {
memcpy(iv, in+size, ctx->iv_sz);
}
EVP_CipherInit(&ectx, CIPHER, NULL, NULL, mode);
EVP_CIPHER_CTX_set_padding(&ectx, 0);
EVP_CipherInit(&ectx, NULL, key, iv, mode);
EVP_CipherUpdate(&ectx, out, &tmp_csz, in, size);
csz = tmp_csz;
out += tmp_csz;
EVP_CipherFinal(&ectx, out, &tmp_csz);
csz += tmp_csz;
EVP_CIPHER_CTX_cleanup(&ectx);
assert(size == csz);
return SQLITE_OK;
}
示例13: ssh1_3des_init
static int
ssh1_3des_init(EVP_CIPHER_CTX *ctx, const u_char *key, const u_char *iv,
int enc)
{
struct ssh1_3des_ctx *c;
u_char *k1, *k2, *k3;
if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) == NULL) {
if ((c = calloc(1, sizeof(*c))) == NULL)
return 0;
EVP_CIPHER_CTX_set_app_data(ctx, c);
}
if (key == NULL)
return 1;
if (enc == -1)
enc = ctx->encrypt;
k1 = k2 = k3 = __UNCONST(key);
k2 += 8;
if (EVP_CIPHER_CTX_key_length(ctx) >= 16+8) {
if (enc)
k3 += 16;
else
k1 += 16;
}
EVP_CIPHER_CTX_init(&c->k1);
EVP_CIPHER_CTX_init(&c->k2);
EVP_CIPHER_CTX_init(&c->k3);
if (EVP_CipherInit(&c->k1, EVP_des_cbc(), k1, NULL, enc) == 0 ||
EVP_CipherInit(&c->k2, EVP_des_cbc(), k2, NULL, !enc) == 0 ||
EVP_CipherInit(&c->k3, EVP_des_cbc(), k3, NULL, enc) == 0) {
explicit_bzero(c, sizeof(*c));
free(c);
EVP_CIPHER_CTX_set_app_data(ctx, NULL);
return 0;
}
return 1;
}
示例14: ssh1_3des_init
static int
ssh1_3des_init(EVP_CIPHER_CTX *ctx, const u_char *key, const u_char *iv,
int enc)
{
struct ssh1_3des_ctx *c;
u_char *k1, *k2, *k3;
if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) == NULL) {
c = xmalloc(sizeof(*c));
EVP_CIPHER_CTX_set_app_data(ctx, c);
}
if (key == NULL)
return (1);
if (enc == -1)
enc = ctx->encrypt;
k1 = k2 = k3 = (u_char *) key;
k2 += 8;
if (EVP_CIPHER_CTX_key_length(ctx) >= 16+8) {
if (enc)
k3 += 16;
else
k1 += 16;
}
EVP_CIPHER_CTX_init(&c->k1);
EVP_CIPHER_CTX_init(&c->k2);
EVP_CIPHER_CTX_init(&c->k3);
if (EVP_CipherInit(&c->k1, EVP_des_cbc(), k1, NULL, enc) == 0 ||
EVP_CipherInit(&c->k2, EVP_des_cbc(), k2, NULL, !enc) == 0 ||
EVP_CipherInit(&c->k3, EVP_des_cbc(), k3, NULL, enc) == 0) {
memset(c, 0, sizeof(*c));
xfree(c);
EVP_CIPHER_CTX_set_app_data(ctx, NULL);
return (0);
}
return (1);
}
示例15: openssl_evp_comcrypt
void openssl_evp_comcrypt()
{
EVP_CIPHER_CTX ctx;
int i, len1 = 0, len2 = 0, len3 = 0;
unsigned char outs[MAX1_LEN], des[MAX1_LEN];
unsigned char msg[MAX1_LEN] = "openssl common encrypt test";
unsigned char iv[EVP_MAX_KEY_LENGTH], key[EVP_MAX_KEY_LENGTH];
for (i = 0; i < 24; i++)
key[i] = i;
for (i = 0; i < 8; i++)
iv[i] = i;
memset(des, 0, sizeof(des));
memset(outs, 0, sizeof(outs));
EVP_CIPHER_CTX_init(&ctx);
EVP_CipherInit(&ctx, EVP_des_ede3_cbc(), key, iv, 1);
EVP_CipherUpdate(&ctx, outs, &len1, msg, strlen((char *)msg));
EVP_CipherFinal(&ctx, outs + len1, &len3);
len1 += len3;
printf("\nEVP_COMEncry (%s) = ", msg);
for (i = 0; i < len1; i++)
printf("0x%.02x ", outs[i]);
EVP_CIPHER_CTX_cleanup(&ctx);
EVP_CIPHER_CTX_init(&ctx);
EVP_CipherInit(&ctx, EVP_des_ede3_cbc(), key, iv, 0);
EVP_CipherUpdate(&ctx, des, &len2, outs, len1);
EVP_CipherFinal(&ctx, des + len2, &len3);
len2 += len3;
printf("\nEVP_COMDecry (");
for (i = 0; i < len1; i++)
printf("0x%.02x ", outs[i]);
printf(") = %s\n", des);
EVP_CIPHER_CTX_cleanup(&ctx);
}