本文整理汇总了C++中EVP_CipherUpdate函数的典型用法代码示例。如果您正苦于以下问题:C++ EVP_CipherUpdate函数的具体用法?C++ EVP_CipherUpdate怎么用?C++ EVP_CipherUpdate使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了EVP_CipherUpdate函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: XCBC_Init
int
XCBC_Init(XCBC_CTX *xctx,
const uint8_t const *k)
{
int bl, outl;
EVP_CIPHER_CTX *ctx;
uint8_t k1[XCBC_MAX_BLOCK_LENGTH];
ctx = xctx->ctx;
EVP_CIPHER_CTX_init(ctx);
OPENSSL_try(EVP_CipherInit_ex(ctx, EVP_aes_128_ecb(), NULL, k, NULL, 1),
"cipher init error", return0);
bl = EVP_CIPHER_CTX_block_size((const EVP_CIPHER_CTX *)ctx);
OPENSSL_try(EVP_CipherUpdate(ctx, k1, &outl, ks1, bl),
"cipher update error (k1)", return0);
OPENSSL_try(EVP_CipherUpdate(ctx, xctx->k2, &outl, ks2, bl),
"cipher update error (k2)", return0);
OPENSSL_try(EVP_CipherUpdate(ctx, xctx->k3, &outl, ks3, bl),
"cipher update error (k3)", return0);
OPENSSL_try(EVP_CipherInit_ex(ctx, NULL, NULL, k1, NULL, -1),
"cipher reset error", return0);
memset(xctx->e, 0, bl);
xctx->size = 0;
return 1;
return0:
return 0;
}
示例2: encrypt_buf
void encrypt_buf(struct encryption_ctx *ctx, char *buf, int *len) {
if (_method == EncryptionTable) {
table_encrypt(buf, *len);
} else {
if (ctx->status == STATUS_EMPTY) {
int iv_len = encryption_iv_len[_method];
unsigned char iv[EVP_MAX_IV_LENGTH];
memset(iv, 0, iv_len);
RAND_bytes(iv, iv_len);
init_cipher(ctx, iv, iv_len, 1);
int out_len = *len + EVP_CIPHER_CTX_block_size(ctx->ctx);
unsigned char *cipher_text = malloc(out_len);
EVP_CipherUpdate(ctx->ctx, cipher_text, &out_len, buf, *len);
memcpy(buf, iv, iv_len);
memcpy(buf + iv_len, cipher_text, out_len);
*len = iv_len + out_len;
free(cipher_text);
} else {
int out_len = *len + EVP_CIPHER_CTX_block_size(ctx->ctx);
unsigned char *cipher_text = malloc(out_len);
EVP_CipherUpdate(ctx->ctx, cipher_text, &out_len, buf, *len);
memcpy(buf, cipher_text, out_len);
*len = out_len;
free(cipher_text);
}
}
}
示例3: ossl_cipher_update_long
static int
ossl_cipher_update_long(EVP_CIPHER_CTX *ctx, unsigned char *out, long *out_len_ptr,
const unsigned char *in, long in_len)
{
int out_part_len;
long out_len = 0;
#define UPDATE_LENGTH_LIMIT INT_MAX
#if SIZEOF_LONG > UPDATE_LENGTH_LIMIT
if (in_len > UPDATE_LENGTH_LIMIT) {
const int in_part_len = (UPDATE_LENGTH_LIMIT / 2 + 1) & ~1;
do {
if (!EVP_CipherUpdate(ctx, out ? (out + out_len) : 0,
&out_part_len, in, in_part_len))
return 0;
out_len += out_part_len;
in += in_part_len;
} while ((in_len -= in_part_len) > UPDATE_LENGTH_LIMIT);
}
#endif
if (!EVP_CipherUpdate(ctx, out ? (out + out_len) : 0,
&out_part_len, in, (int)in_len))
return 0;
if (out_len_ptr) *out_len_ptr = out_len += out_part_len;
return 1;
}
示例4: main
int main(int N, char ** S)
{
unsigned char key[]= "helloworld" ;
unsigned char iv[]= "12345678" ;
char * destp ;
int iuse, iuse2 ;
EVP_CIPHER_CTX ctx;
OpenSSL_add_all_ciphers() ;
printf("test: (%s) len %zd.\n", test, strlen(test)) ;
EVP_CIPHER_CTX_init(&ctx) ;
// encode
EVP_CipherInit_ex(&ctx, EVP_bf_cbc(), NULL, NULL, NULL, 1);
EVP_CIPHER_CTX_set_key_length(&ctx, strlen(key));
EVP_CipherInit_ex(&ctx, NULL, NULL, key, iv, -1);
destp= buffer ;
EVP_CipherUpdate(&ctx, destp, &iuse, test, strlen(test) +1) ;
destp += iuse ;
EVP_CipherFinal_ex(&ctx, destp, &iuse) ;
destp += iuse ;
iuse= ( destp - buffer ) ;
EVP_CIPHER_CTX_cleanup(&ctx);
// decode
EVP_CipherInit_ex(&ctx, EVP_bf_cbc(), NULL, NULL, NULL, 0);
EVP_CIPHER_CTX_set_key_length(&ctx, strlen(key));
EVP_CipherInit_ex(&ctx, NULL, NULL, key, iv, -1);
destp= buffer2 ;
EVP_CipherUpdate(&ctx, destp, &iuse2, buffer, iuse ) ;
destp += iuse2 ;
EVP_CipherFinal_ex(&ctx, destp, &iuse2) ;
destp += iuse2 ;
iuse2= ( destp - buffer2 ) ;
EVP_CIPHER_CTX_cleanup(&ctx);
EVP_cleanup() ;
encode_asc85(printbuf, sizeof(printbuf), test, strlen(test) +1) ;
printf("SRC: %s\n", printbuf) ;
encode_asc85(printbuf, sizeof(printbuf), buffer, iuse) ;
printf("ENC: %s\n", printbuf) ;
encode_asc85(printbuf, sizeof(printbuf), buffer2, iuse2) ;
printf("DEC: %s\nout: %s\n", printbuf, buffer2) ;
}
示例5: XCBC_Final
int
XCBC_Final(XCBC_CTX *xctx,
uint8_t *out)
{
int bl, n, outl;
bl = EVP_CIPHER_CTX_block_size(xctx->ctx);
/* xctx->r = M[n] */
if (xctx->size == bl) {
for (n = 0; n < bl; n++)
xctx->m[n] = xctx->r[n] ^ xctx->e[n] ^ xctx->k2[n];
} else {
for (n = xctx->size; n < bl; n++)
xctx->r[n] = (n == xctx->size) ? 0x80 : 0x00;
for (n = 0; n < bl; n++) {
xctx->m[n] = xctx->r[n] ^ xctx->e[n] ^ xctx->k3[n];
}
}
OPENSSL_try(EVP_CipherUpdate(xctx->ctx,
xctx->e, &outl,
xctx->m, bl),
"cipher update error (finalizing)", return0);
memcpy(out, xctx->e, bl);
return 1;
return0:
return 0;
}
示例6: writeSize
/*
the size of outBuf must be larger than inBufSize + blockSize
@retval positive or 0 : writeSize(+blockSize)
@retval -1 : error
*/
int update(char *outBuf, const char *inBuf, int inBufSize)
{
int outLen = 0;
int ret = EVP_CipherUpdate(&ctx_, cybozu::cast<uint8_t*>(outBuf), &outLen, cybozu::cast<const uint8_t*>(inBuf), inBufSize);
if (ret != 1) return -1;
return outLen;
}
示例7: 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);
}
示例8: cipher_context_update
static int cipher_context_update(cipher_ctx_t *ctx, uint8_t *output, size_t *olen,
const uint8_t *input, size_t ilen)
{
#ifdef USE_CRYPTO_APPLECC
cipher_cc_t *cc = &ctx->cc;
if (cc->valid == kCCContextValid) {
CCCryptorStatus ret;
ret = CCCryptorUpdate(cc->cryptor, input, ilen, output,
ilen, olen);
return (ret == kCCSuccess) ? 1 : 0;
}
#endif
cipher_evp_t *evp = &ctx->evp;
#if defined(USE_CRYPTO_OPENSSL)
int err = 0, tlen = *olen;
err = EVP_CipherUpdate(evp, (uint8_t *)output, &tlen,
(const uint8_t *)input, ilen);
*olen = tlen;
return err;
#elif defined(USE_CRYPTO_POLARSSL)
return !cipher_update(evp, (const uint8_t *)input, ilen,
(uint8_t *)output, olen);
#elif defined(USE_CRYPTO_MBEDTLS)
return !mbedtls_cipher_update(evp, (const uint8_t *)input, ilen,
(uint8_t *)output, olen);
#endif
}
示例9: do_crypt
/********************do the cryption part*************************/
int do_crypt(unsigned char *key, unsigned char *iv, char *msg, int *l, int crypt) {
unsigned char outbuf[BUFSIZE + EVP_MAX_BLOCK_LENGTH];
int len = *l, outlen, tmplen, i;
unsigned char input[BUFSIZE];
memcpy(input, msg, len);
if (DEBUG) {
printf("\nbefore crypted payload: ");
for(i = 0; i < len; i++) printf("%02x", *(input+i));
putchar(10);
}
EVP_CIPHER_CTX ctx;
EVP_CIPHER_CTX_init(&ctx);
EVP_CipherInit_ex(&ctx, EVP_aes_128_cbc(), NULL, key, iv, crypt);
if(!EVP_CipherUpdate(&ctx, outbuf, &outlen, input, len)) {
EVP_CIPHER_CTX_cleanup(&ctx);
return 0;
}
if(!EVP_CipherFinal_ex(&ctx, outbuf + outlen, &tmplen)) {
EVP_CIPHER_CTX_cleanup(&ctx);
return 0;
}
outlen += tmplen;
if (DEBUG) {
printf("\ncrypted payload: ");
for(i = 0; i < outlen; i++) printf("%02x", *(outbuf+i));
printf("\n");
}
memcpy(msg, outbuf, outlen);
*l = outlen;
EVP_CIPHER_CTX_cleanup(&ctx);
return 1;
}
示例10: run
void run(const char *name, unsigned char *buf, int len) {
int pass = TOTAL_LEN / len;
unsigned char key[32] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
EVP_CIPHER_CTX ctx;
EVP_CIPHER_CTX_init(&ctx);
int outlen = 0;
struct timeval start, end;
double total_time, throughput;
EVP_CIPHER *cipher = (EVP_CIPHER *) EVP_get_cipherbyname("aes-256-ecb");
EVP_CipherInit_ex(&ctx, cipher, NULL, key, NULL, 1);
gettimeofday(&start, NULL);
for (int i = 0; i < pass; i++) {
fprintf(stderr, "%s: Pass %d/%d\n", name, i + 1, pass);
// note we cannot run multiple pass on the same buffer, the CPU will cache it! (smart bastards at Intel)
EVP_CipherUpdate(&ctx, buf + i * len, &outlen, buf, len);
if (len != outlen) {
fprintf(stderr, "Fatal error: incorrect output size.\n");
}
}
gettimeofday(&end, NULL);
total_time = end.tv_sec - start.tv_sec + (end.tv_usec - start.tv_usec) / 1000000.0;
throughput = TOTAL_LEN * 8 / total_time / 1000000;
printf("%s: %u-byte blocks, %lubytes in %f s, Throughput: %fMbps\n", name, len, TOTAL_LEN, total_time, throughput);
EVP_CIPHER_CTX_cleanup(&ctx);
}
示例11: crypt
int crypt(unsigned char *inbuf, int inlen, unsigned char *outbuf, unsigned char key[],unsigned char iv[], int do_encrypt)
{
int outlen, mlen;
EVP_CIPHER_CTX ctx;
EVP_CIPHER_CTX_init(&ctx);
EVP_CipherInit_ex(&ctx, EVP_aes_128_cbc(), NULL, key, iv, do_encrypt);
outlen = 0;
if(inlen <= 0) return 0;
if(!EVP_CipherUpdate(&ctx, outbuf + outlen, &mlen, inbuf, inlen))
{
/* Error */
EVP_CIPHER_CTX_cleanup(&ctx);
return 0;
}
outlen += mlen;
if(!EVP_CipherFinal_ex(&ctx, outbuf + outlen, &mlen))
{
/* Error */
EVP_CIPHER_CTX_cleanup(&ctx);
return 0;
}
outlen += mlen;
EVP_CIPHER_CTX_cleanup(&ctx);
return outlen;
}
示例12: Java_com_facebook_crypto_cipher_NativeGCMCipher_nativeUpdate
JNIEXPORT int JNICALL Java_com_facebook_crypto_cipher_NativeGCMCipher_nativeUpdate(
JNIEnv* env,
jobject obj,
jbyteArray data,
jint offset,
jint dataLength,
jbyteArray output) {
int bytesWritten = 0;
EVP_CIPHER_CTX* ctx = Get_Cipher_CTX(env, obj);
if (!ctx) {
return CRYPTO_NO_BYTES_WRITTEN;
}
jbyte* outputBytes = (*env)->GetByteArrayElements(env, output, NULL);
if (!outputBytes) {
return CRYPTO_NO_BYTES_WRITTEN;
}
jbyte* dataBytes = (*env)->GetByteArrayElements(env, data, NULL);
if (!dataBytes) {
(*env)->ReleaseByteArrayElements(env, output, outputBytes, 0);
return CRYPTO_NO_BYTES_WRITTEN;
}
if (!EVP_CipherUpdate(ctx, outputBytes, &bytesWritten, dataBytes + offset, dataLength)) {
bytesWritten = CRYPTO_NO_BYTES_WRITTEN;
}
(*env)->ReleaseByteArrayElements(env, data, dataBytes, JNI_ABORT);
(*env)->ReleaseByteArrayElements(env, output, outputBytes, 0);
return bytesWritten;
}
示例13: ossl_cipher_update
/*
* call-seq:
* cipher.update(data [, buffer]) -> string or buffer
*
* === Parameters
* +data+ is a nonempty string.
* +buffer+ is an optional string to store the result.
*/
static VALUE
ossl_cipher_update(int argc, VALUE *argv, VALUE self)
{
EVP_CIPHER_CTX *ctx;
char *in;
int in_len, out_len;
VALUE data, str;
rb_scan_args(argc, argv, "11", &data, &str);
StringValue(data);
in = RSTRING_PTR(data);
if ((in_len = RSTRING_LEN(data)) == 0)
rb_raise(rb_eArgError, "data must not be empty");
GetCipher(self, ctx);
out_len = in_len+EVP_CIPHER_CTX_block_size(ctx);
if (NIL_P(str)) {
str = rb_str_new(0, out_len);
} else {
StringValue(str);
rb_str_resize(str, out_len);
}
if (!EVP_CipherUpdate(ctx, RSTRING_PTR(str), &out_len, in, in_len))
ossl_raise(eCipherError, NULL);
assert(out_len < RSTRING_LEN(str));
rb_str_set_len(str, out_len);
return str;
}
示例14: sb_aes_crypt
static int sb_aes_crypt(struct sb_image_ctx *ictx, uint8_t *in_data,
uint8_t *out_data, int in_len)
{
EVP_CIPHER_CTX *ctx = &ictx->cipher_ctx;
int ret, outlen;
uint8_t *outbuf;
outbuf = malloc(in_len);
if (!outbuf)
return -ENOMEM;
memset(outbuf, 0, sizeof(in_len));
ret = EVP_CipherUpdate(ctx, outbuf, &outlen, in_data, in_len);
if (!ret) {
ret = -EINVAL;
goto err;
}
if (out_data)
memcpy(out_data, outbuf, outlen);
err:
free(outbuf);
return ret;
}
示例15: EVP_CIPHER_CTX_init
bool CryptoBuffer::doCrypt(const char *in, int inlen, bool isEncrypt, QByteArray &out)
{
const int OUTBUF_SIZE = 8*1024;
unsigned char outbuf[OUTBUF_SIZE + EVP_MAX_BLOCK_LENGTH];
EVP_CIPHER_CTX ctx;
EVP_CIPHER_CTX_init(&ctx);
auto ctxGuard = makeGuard([&ctx] { EVP_CIPHER_CTX_cleanup(&ctx); });
if (!EVP_CipherInit_ex(&ctx, d->cipher, NULL, d->key, d->iv, isEncrypt))
return DEBUGRET(false, "EVP_CipherInit_Ex failed");
const unsigned char *ptr = reinterpret_cast<const unsigned char*>(in);
int restlen = inlen;
int outlen;
while (restlen > 0)
{
int readlen = std::min(restlen, OUTBUF_SIZE);
if (!EVP_CipherUpdate(&ctx, outbuf, &outlen, ptr, readlen))
return DEBUGRET(false, "EVP_CipherUpdate failed");
out.append(reinterpret_cast<const char*>(outbuf), outlen);
ptr += readlen;
restlen -= readlen;
}
if (!EVP_CipherFinal_ex(&ctx, outbuf, &outlen))
return DEBUGRET(false, "EVP_CipherFinal_ex failed");
out.append(reinterpret_cast<const char*>(outbuf), outlen);
return true;
}