• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C++ EVP_CIPHER_CTX_set_key_length函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了C++中EVP_CIPHER_CTX_set_key_length函数的典型用法代码示例。如果您正苦于以下问题:C++ EVP_CIPHER_CTX_set_key_length函数的具体用法?C++ EVP_CIPHER_CTX_set_key_length怎么用?C++ EVP_CIPHER_CTX_set_key_length使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了EVP_CIPHER_CTX_set_key_length函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C++代码示例。

示例1: tr_rc4_set_key

void
tr_rc4_set_key (tr_rc4_ctx_t    handle,
                const uint8_t * key,
                size_t          key_length)
{
  assert (handle != NULL);
  assert (key != NULL);

  if (!check_result (EVP_CIPHER_CTX_set_key_length (handle, key_length)))
    return;
  check_result (EVP_CipherInit_ex (handle, NULL, NULL, key, NULL, -1));
}
开发者ID:Prodito,项目名称:Torrentor,代码行数:12,代码来源:crypto-utils-openssl.c


示例2: cmd_start_crypt_in

void
cmd_start_crypt_in(struct ctrl_command *cmd)
{
#ifdef HAVE_LIBCRYPTO
	if(in_state.crypt)
		send_error("can't start decryption - already started!");

	if(!in_state.crypt_state.cipher)
		send_error("can't start decryption - no cipher set!");

	if(!in_state.crypt_state.key)
		send_error("can't start decryption - no key set!");

	in_state.crypt = 1;
	if(!EVP_DecryptInit(&in_state.crypt_state.ctx, in_state.crypt_state.cipher, NULL, NULL))
		send_error("can't start decryption - DecryptInit (1) failed: %s!",
			   ERR_error_string(ERR_get_error(), NULL));

	/*
	 * XXX - ugly hack to work around OpenSSL bug
	 *       if/when OpenSSL fix it, or give proper workaround
	 *       use that, and force minimum OpenSSL version
	 *
	 * Without this hack, BF/256 will fail.
	 */
	/* cast to avoid warning */
	*(unsigned int *) (&in_state.crypt_state.ctx.cipher->flags) |= EVP_CIPH_VARIABLE_LENGTH;

	if(!EVP_CIPHER_CTX_set_key_length(&in_state.crypt_state.ctx, in_state.crypt_state.keylen))
		send_error("can't start decryption - set_key_length failed: %s!",
			   ERR_error_string(ERR_get_error(), NULL));

	in_state.crypt_state.ivlen = EVP_CIPHER_CTX_iv_length(&in_state.crypt_state.ctx);

	if(in_state.crypt_state.ivlen)
		in_state.crypt_state.iv = calloc(in_state.crypt_state.ivlen, 1);

	if(in_state.crypt_state.rounds)
	{
		if(!EVP_CIPHER_CTX_ctrl(&in_state.crypt_state.ctx,
					EVP_CTRL_SET_RC5_ROUNDS, in_state.crypt_state.rounds, NULL))
			send_error("can't start decryption - SET_RC5_ROUNDS failed: %s!",
				   ERR_error_string(ERR_get_error(), NULL));
	}

	if(!EVP_DecryptInit(&in_state.crypt_state.ctx,
			    NULL, in_state.crypt_state.key, in_state.crypt_state.iv))
		send_error("can't start decryption - DecryptInit (2) failed: %s!",
			   ERR_error_string(ERR_get_error(), NULL));
#else
	send_error("can't start decryption - no OpenSSL support!");
#endif
}
开发者ID:Cloudxtreme,项目名称:ircd-ratbox,代码行数:53,代码来源:control.c


示例3: ossl_cipher_set_key_length

/*
 *  call-seq:
 *     cipher.key_length = integer -> integer
 *
 *  Sets the key length of the cipher.  If the cipher is a fixed length cipher then attempting to set the key
 *  length to any value other than the fixed value is an error.
 *
 *  Under normal circumstances you do not need to call this method (and probably shouldn't).
 *
 *  See EVP_CIPHER_CTX_set_key_length for further information.
 */
static VALUE
ossl_cipher_set_key_length(VALUE self, VALUE key_length)
{
    int len = NUM2INT(key_length);
    EVP_CIPHER_CTX *ctx;
 
    GetCipher(self, ctx);
    if (EVP_CIPHER_CTX_set_key_length(ctx, len) != 1)
        ossl_raise(eCipherError, NULL);

    return key_length;
}
开发者ID:mamute,项目名称:rubyenterpriseedition187-248,代码行数:23,代码来源:ossl_cipher.c


示例4: cmd_start_crypt_in

void
cmd_start_crypt_in(struct ctrl_command *cmd)
{
#ifdef HAVE_LIBCRYPTO                                                           
  if (in_state.crypt)
    send_error("can't start decryption - already started!");

  if (!in_state.crypt_state.cipher)
    send_error("can't start decryption - no cipher set!");

  if (!in_state.crypt_state.key)
    send_error("can't start decryption - no key set!");

  in_state.crypt = 1;
  if (!EVP_DecryptInit(&in_state.crypt_state.ctx,
                       in_state.crypt_state.cipher, NULL, NULL))
    send_error("can't start decryption - DecryptInit (1) failed: %s!",
               ERR_error_string(ERR_get_error(), NULL));

  if (!EVP_CIPHER_CTX_set_key_length(&in_state.crypt_state.ctx,
                                     in_state.crypt_state.keylen))
    send_error("can't start decryption - set_key_length failed: %s!",
               ERR_error_string(ERR_get_error(), NULL));

  in_state.crypt_state.ivlen =
    EVP_CIPHER_CTX_iv_length(&in_state.crypt_state.ctx);

  if (in_state.crypt_state.ivlen)
    in_state.crypt_state.iv = calloc(in_state.crypt_state.ivlen, 1);

  if (in_state.crypt_state.rounds)
  {
    if (!EVP_CIPHER_CTX_ctrl(&in_state.crypt_state.ctx,
                             EVP_CTRL_SET_RC5_ROUNDS,
                             in_state.crypt_state.rounds,
                             NULL))
      send_error("can't start decryption - SET_RC5_ROUNDS failed: %s!",
                 ERR_error_string(ERR_get_error(), NULL));
  }

  if (!EVP_DecryptInit(&in_state.crypt_state.ctx,
                       NULL,
                       in_state.crypt_state.key,
                       in_state.crypt_state.iv))
    send_error("can't start decryption - DecryptInit (2) failed: %s!",
               ERR_error_string(ERR_get_error(), NULL));
#else
  send_error("can't start decryption - no OpenSSL support!");
#endif
}
开发者ID:Adam-,项目名称:oftc-hybrid,代码行数:50,代码来源:control.c


示例5: enc_ctx_init

void enc_ctx_init(EVP_CIPHER_CTX *ctx, const char *pass, int enc) {
    unsigned char key[EVP_MAX_KEY_LENGTH];
    unsigned char iv[EVP_MAX_IV_LENGTH];
    int key_len = EVP_BytesToKey(EVP_rc4(), EVP_md5(), NULL, (unsigned char*) pass, 
            strlen(pass), 1, key, iv);
    EVP_CIPHER_CTX_init(ctx);
    EVP_CipherInit_ex(ctx, EVP_rc4(), NULL, NULL, NULL, enc);
    if (!EVP_CIPHER_CTX_set_key_length(ctx, key_len)) {
        LOGE("Invalid key length: %d", key_len);
        EVP_CIPHER_CTX_cleanup(ctx);
        exit(EXIT_FAILURE);
    }
    EVP_CipherInit_ex(ctx, NULL, NULL, key, iv, enc);
}
开发者ID:AmVPN,项目名称:proxydroid,代码行数:14,代码来源:encrypt.c


示例6: EVP_OpenInit

int EVP_OpenInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type,
                 const unsigned char *ek, int ekl, const unsigned char *iv,
                 EVP_PKEY *priv)
{
    unsigned char *key = NULL;
    int i, size = 0, ret = 0;

    if (type) {
        EVP_CIPHER_CTX_init(ctx);
        if (!EVP_DecryptInit_ex(ctx, type, NULL, NULL, NULL))
            return 0;
    }

    if (!priv)
        return 1;

    if ((EVP_PKEY_base_id(priv) != EVP_PKEY_RSA) &&
        (EVP_PKEY_base_id(priv) != EVP_PKEY_EC)) {
        EVPerr(EVP_F_EVP_OPENINIT, EVP_R_PUBLIC_KEY_NOT_RSA);
        goto err;
    }

    size = EVP_PKEY_size(priv);
    key = (unsigned char *)OPENSSL_malloc(size + 2);
    if (key == NULL) {
        /* ERROR */
        EVPerr(EVP_F_EVP_OPENINIT, ERR_R_MALLOC_FAILURE);
        goto err;
    }

    i = EVP_PKEY_decrypt_old(key, ek, ekl, priv);
    if ((i <= 0) || !EVP_CIPHER_CTX_set_key_length(ctx, i)) {
        /* ERROR */
        goto err;
    }
    if (!EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv))
        goto err;

    ret = 1;
 err:
    if (key != NULL)
        OPENSSL_cleanse(key, size);
    OPENSSL_free(key);
    return (ret);
}
开发者ID:LiTianjue,项目名称:GmSSL,代码行数:45,代码来源:p_open.c


示例7: cipher_ctx_init

/*
 * Our hc_EVP_CIPHER init() method; wraps around OpenSSL
 * EVP_CipherInit_ex().
 *
 * This is very similar to the init() function pointer in an OpenSSL
 * EVP_CIPHER, but a) we can't access them in 1.1, and b) the method
 * invocation protocols in hcrypto and OpenSSL are similar but not the
 * same, thus we must have this wrapper.
 */
static int
cipher_ctx_init(hc_EVP_CIPHER_CTX *ctx, const unsigned char *key,
                const unsigned char *iv, int enc)
{
    struct ossl_cipher_ctx *ossl_ctx = ctx->cipher_data; /* EVP_CIPHER_CTX wrapper */
    const EVP_CIPHER *c;

    assert(ossl_ctx != NULL);
    assert(ctx->cipher != NULL);
    assert(ctx->cipher->app_data != NULL);

    /*
     * Here be dragons.
     *
     * We need to make sure that the OpenSSL EVP_CipherInit_ex() is
     * called with cipher!=NULL just once per EVP_CIPHER_CTX, otherwise
     * state in the OpenSSL EVP_CIPHER_CTX will get cleaned up and then
     * we'll segfault.
     *
     * hcrypto applications can re-initialize an (hc_)EVP_CIPHER_CTX as
     * usual by calling (hc)EVP_CipherInit_ex() with a non-NULL cipher
     * argument, and that will cause cipher_cleanup() (below) to be
     * called.
     */
    c = ossl_ctx->ossl_cipher = ctx->cipher->app_data; /* OpenSSL's EVP_CIPHER * */
    if (!ossl_ctx->initialized) {
        ossl_ctx->ossl_cipher_ctx = EVP_CIPHER_CTX_new();
        if (ossl_ctx->ossl_cipher_ctx == NULL)
            return 0;
        /*
         * So we always call EVP_CipherInit_ex() with c!=NULL, but other
         * things NULL...
         */
        if (!EVP_CipherInit_ex(ossl_ctx->ossl_cipher_ctx, c, NULL, NULL, NULL, enc))
            return 0;
        ossl_ctx->initialized = 1;
    }

    /* ...and from here on always call EVP_CipherInit_ex() with c=NULL */
    if ((ctx->cipher->flags & hc_EVP_CIPH_VARIABLE_LENGTH) &&
        ctx->key_len > 0)
        EVP_CIPHER_CTX_set_key_length(ossl_ctx->ossl_cipher_ctx, ctx->key_len);

    return EVP_CipherInit_ex(ossl_ctx->ossl_cipher_ctx, NULL, NULL, key, iv, enc);
}
开发者ID:InvLim,项目名称:heimdal,代码行数:54,代码来源:evp-openssl.c


示例8: CMAC_Init

int
CMAC_Init(CMAC_CTX *ctx, const void *key, size_t keylen,
    const EVP_CIPHER *cipher, ENGINE *impl)
{
	static unsigned char zero_iv[EVP_MAX_BLOCK_LENGTH];

	/* All zeros means restart */
	if (!key && !cipher && !impl && keylen == 0) {
		/* Not initialised */
		if (ctx->nlast_block == -1)
			return 0;
		if (!EVP_EncryptInit_ex(&ctx->cctx, NULL, NULL, NULL, zero_iv))
			return 0;
		memset(ctx->tbl, 0, EVP_CIPHER_CTX_block_size(&ctx->cctx));
		ctx->nlast_block = 0;
		return 1;
	}
	/* Initialiase context */
	if (cipher && !EVP_EncryptInit_ex(&ctx->cctx, cipher, impl, NULL, NULL))
		return 0;
	/* Non-NULL key means initialisation complete */
	if (key) {
		int bl;

		if (!EVP_CIPHER_CTX_cipher(&ctx->cctx))
			return 0;
		if (!EVP_CIPHER_CTX_set_key_length(&ctx->cctx, keylen))
			return 0;
		if (!EVP_EncryptInit_ex(&ctx->cctx, NULL, NULL, key, zero_iv))
			return 0;
		bl = EVP_CIPHER_CTX_block_size(&ctx->cctx);
		if (!EVP_Cipher(&ctx->cctx, ctx->tbl, zero_iv, bl))
			return 0;
		make_kn(ctx->k1, ctx->tbl, bl);
		make_kn(ctx->k2, ctx->k1, bl);
		OPENSSL_cleanse(ctx->tbl, bl);
		/* Reset context again ready for first data block */
		if (!EVP_EncryptInit_ex(&ctx->cctx, NULL, NULL, NULL, zero_iv))
			return 0;
		/* Zero tbl so resume works */
		memset(ctx->tbl, 0, bl);
		ctx->nlast_block = 0;
	}
	return 1;
}
开发者ID:SylvestreG,项目名称:bitrig,代码行数:45,代码来源:cmac.c


示例9: init_cipher

void init_cipher(struct encryption_ctx *ctx, const unsigned char *iv, int iv_len, int is_cipher) {
    ctx->status = STATUS_INIT;
    if (_method != EncryptionTable) {
        EVP_CIPHER_CTX_init(ctx->ctx);
        EVP_CipherInit_ex(ctx->ctx, _cipher, NULL, NULL, NULL, is_cipher);
        if (!EVP_CIPHER_CTX_set_key_length(ctx->ctx, _key_len)) {
            cleanup_encryption(ctx);
//            NSLog(@"Invalid key length");
//            assert(0);
            // TODO free memory and report error
            return;
        }
        EVP_CIPHER_CTX_set_padding(ctx->ctx, 1);

        EVP_CipherInit_ex(ctx->ctx, NULL, NULL, _key, iv, is_cipher);

    }
}
开发者ID:hynnet,项目名称:ShadowWeb,代码行数:18,代码来源:encrypt.c


示例10: EVP_CIPHER_CTX_key_length

STDMETHODIMP CBCipher::GenerateKey(short iSize)
{
	if(!m_ctx.cipher)return SetErrorInfo(s_strAlgoError);

	if(iSize < 0)
		m_iKeySize = EVP_CIPHER_CTX_key_length(&m_ctx);

	if(iSize > EVP_MAX_KEY_LENGTH)
		return E_INVALIDARG;

	if(!EVP_CIPHER_CTX_set_key_length(&m_ctx, iSize))
		return E_INVALIDARG;

	m_pKey.Allocate(m_iKeySize = iSize);

	RAND_bytes(m_pKey, m_iKeySize);

	return S_OK;
}
开发者ID:2Quico,项目名称:netbox,代码行数:19,代码来源:BCipher.cpp


示例11: decipher_evp

static char *	decipher_evp (const unsigned char *key, int keylen, const unsigned char *ciphertext, int cipherlen, const EVP_CIPHER *type, int *outlen, int ivsize)
{
        unsigned char *outbuf;
	unsigned char	*iv = NULL;
	unsigned long errcode;
	int	outlen2;
        EVP_CIPHER_CTX a;
        EVP_CIPHER_CTX_init(&a);
	EVP_CIPHER_CTX_set_padding(&a, 0);

	if (ivsize > 0)
		iv = new_malloc(ivsize);
	outbuf = new_malloc(cipherlen + 1024);
	if (ivsize > 0)
		memcpy(iv, ciphertext, ivsize);

        EVP_DecryptInit_ex(&a, type, NULL, NULL, iv);
	EVP_CIPHER_CTX_set_key_length(&a, keylen);
	EVP_CIPHER_CTX_set_padding(&a, 0);
        EVP_DecryptInit_ex(&a, NULL, NULL, key, NULL);

        if (EVP_DecryptUpdate(&a, outbuf, outlen, ciphertext, cipherlen) != 1)
		yell("EVP_DecryptUpdate died.");
	if (EVP_DecryptFinal_ex(&a, outbuf + (*outlen), &outlen2) != 1)
		yell("EVP_DecryptFinal_Ex died.");
	*outlen += outlen2;

        EVP_CIPHER_CTX_cleanup(&a);

	ERR_load_crypto_strings();
	while ((errcode = ERR_get_error()))
	{
	    char r[256];
	    ERR_error_string_n(errcode, r, 256);
	    yell("ERROR: %s", r);
	}

	if (ivsize > 0)
		new_free(&iv);
	return outbuf;
}
开发者ID:Cloudxtreme,项目名称:epic5,代码行数:41,代码来源:crypto.c


示例12: put_Key

STDMETHODIMP CBCipher::put_Key(VARIANT Val)
{
	if(!m_ctx.cipher)return SetErrorInfo(s_strAlgoError);

	HRESULT hr;
	CBVarPtr varPtr;

	hr = varPtr.Attach(Val);
	if(FAILED(hr))return hr;

	if(varPtr.m_nSize > EVP_MAX_KEY_LENGTH)
		return E_INVALIDARG;

	if(!EVP_CIPHER_CTX_set_key_length(&m_ctx, m_iKeySize))
		return E_INVALIDARG;

	m_pKey.Allocate(m_iKeySize = varPtr.m_nSize);
	CopyMemory(m_pKey, varPtr.m_pData, m_iKeySize);

	return S_OK;
}
开发者ID:2Quico,项目名称:netbox,代码行数:21,代码来源:BCipher.cpp


示例13: cipher_ctx_init

void
cipher_ctx_init (EVP_CIPHER_CTX *ctx, uint8_t *key, int key_len,
    const EVP_CIPHER *kt, int enc)
{
  ASSERT(NULL != kt && NULL != ctx);

  CLEAR (*ctx);

  EVP_CIPHER_CTX_init (ctx);
  if (!EVP_CipherInit (ctx, kt, NULL, NULL, enc))
    msg (M_SSLERR, "EVP cipher init #1");
#ifdef HAVE_EVP_CIPHER_CTX_SET_KEY_LENGTH
  if (!EVP_CIPHER_CTX_set_key_length (ctx, key_len))
    msg (M_SSLERR, "EVP set key size");
#endif
  if (!EVP_CipherInit (ctx, NULL, key, NULL, enc))
    msg (M_SSLERR, "EVP cipher init #2");

  /* make sure we used a big enough key */
  ASSERT (EVP_CIPHER_CTX_key_length (ctx) <= key_len);
}
开发者ID:AllardJ,项目名称:Tomato,代码行数:21,代码来源:crypto_openssl.c


示例14: logp

EVP_CIPHER_CTX *enc_setup(int encrypt, const char *encryption_password)
{
	EVP_CIPHER_CTX *ctx=NULL;
	// Declare enc_iv with individual characters so that the weird last
	// character can be specified as a hex number in order to prevent
	// compilation warnings on Macs.
	uint8_t enc_iv[]={'[', 'l', 'k', 'd', '.', '$', 'G', 0xa3, '\0'};

	if(!encryption_password)
	{
		logp("No encryption password in %s()\n", __func__);
		goto error;
	}

	if(!(ctx=(EVP_CIPHER_CTX *)
		calloc_w(1, sizeof(EVP_CIPHER_CTX), __func__)))
			goto error;

	// Don't set key or IV because we will modify the parameters.
	EVP_CIPHER_CTX_init(ctx);
	if(!(EVP_CipherInit_ex(ctx, EVP_bf_cbc(), NULL, NULL, NULL, encrypt)))
	{
		logp("EVP_CipherInit_ex failed\n");
		goto error;
	}
	EVP_CIPHER_CTX_set_key_length(ctx, strlen(encryption_password));
	// We finished modifying parameters so now we can set key and IV

	if(!EVP_CipherInit_ex(ctx, NULL, NULL,
		(uint8_t *)encryption_password,
		enc_iv, encrypt))
	{
		logp("Second EVP_CipherInit_ex failed\n");
		goto error;
	}
	return ctx;
error:
	free_v((void **)&ctx);
	return NULL;
}
开发者ID:ZungBang,项目名称:burp,代码行数:40,代码来源:handy.c


示例15: ssl_des3_encrypt

size_t ssl_des3_encrypt(const unsigned char *key, size_t key_len, const unsigned char *input, size_t input_len,
                        const unsigned char *iv, unsigned char **res)
{
    int output_length = 0;
    EVP_CIPHER_CTX ctx;

    *res = g_new0(unsigned char, 72);

    /* Don't set key or IV because we will modify the parameters */
    EVP_CIPHER_CTX_init(&ctx);
    EVP_CipherInit_ex(&ctx, EVP_des_ede3_cbc(), NULL, NULL, NULL, 1);
    EVP_CIPHER_CTX_set_key_length(&ctx, key_len);
    EVP_CIPHER_CTX_set_padding(&ctx, 0);
    /* We finished modifying parameters so now we can set key and IV */
    EVP_CipherInit_ex(&ctx, NULL, NULL, key, iv, 1);
    EVP_CipherUpdate(&ctx, *res, &output_length, input, input_len);
    EVP_CipherFinal_ex(&ctx, *res, &output_length);
    EVP_CIPHER_CTX_cleanup(&ctx);
    //EVP_cleanup();

    return output_length;
}
开发者ID:Voltara,项目名称:bitlbee,代码行数:22,代码来源:ssl_openssl.c


示例16: rc2_get_asn1_type_and_iv

static int rc2_get_asn1_type_and_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
{
    long num=0;
    int i=0,l;
    int key_bits;
    unsigned char iv[EVP_MAX_IV_LENGTH];

    if (type != NULL)
    {
        l=EVP_CIPHER_CTX_iv_length(c);
        i=ASN1_TYPE_get_int_octetstring(type,&num,iv,l);
        if (i != l)
            return(-1);
        key_bits =rc2_magic_to_meth((int)num);
        if (!key_bits)
            return(-1);
        if(i > 0) EVP_CipherInit(c, NULL, NULL, iv, -1);
        EVP_CIPHER_CTX_ctrl(c, EVP_CTRL_SET_RC2_KEY_BITS, key_bits, NULL);
        EVP_CIPHER_CTX_set_key_length(c, key_bits / 8);
    }
    return(i);
}
开发者ID:houzhenggang,项目名称:mt7688_mips_ecos,代码行数:22,代码来源:e_rc2.c


示例17: cipher_context_init

void cipher_context_init(cipher_ctx_t *evp, int method, int enc)
{
    if (method <= TABLE || method >= CIPHER_NUM) {
        LOGE("cipher_context_init(): Illegal method");
        return;
    }

    const char *ciphername = supported_ciphers[method];
    const cipher_kt_t *cipher = get_cipher_type(method);
#if defined(USE_CRYPTO_OPENSSL)
    if (cipher == NULL) {
        LOGE("Cipher %s not found in OpenSSL library", ciphername);
        FATAL("Cannot initialize cipher");
    }
    EVP_CIPHER_CTX_init(evp);
    if (!EVP_CipherInit_ex(evp, cipher, NULL, NULL, NULL, enc)) {
        LOGE("Cannot initialize cipher %s", ciphername);
        exit(EXIT_FAILURE);
    }
    if (!EVP_CIPHER_CTX_set_key_length(evp, enc_key_len)) {
        EVP_CIPHER_CTX_cleanup(evp);
        LOGE("Invalid key length: %d", enc_key_len);
        exit(EXIT_FAILURE);
    }
    if (method > RC4) {
        EVP_CIPHER_CTX_set_padding(evp, 1);
    }
#elif defined(USE_CRYPTO_POLARSSL)
    if (cipher == NULL) {
        LOGE("Cipher %s not found in PolarSSL library", ciphername);
        FATAL("Cannot initialize PolarSSL cipher");
    }
    if (cipher_init_ctx(evp, cipher) != 0) {
        FATAL("Cannot initialize PolarSSL cipher context");
    }
#endif
}
开发者ID:764664,项目名称:shadowsocks-libev,代码行数:37,代码来源:encrypt.c


示例18: gen_ossl_encrypt

static int
gen_ossl_encrypt(PX_Cipher *c, const uint8 *data, unsigned dlen,
				 uint8 *res)
{
	ossldata   *od = c->ptr;
	int			outlen;

	if (!od->init)
	{
		EVP_CIPHER_CTX_init(&od->evp_ctx);
		if (!EVP_EncryptInit_ex(&od->evp_ctx, od->evp_ciph, NULL, NULL, NULL))
			return PXE_CIPHER_INIT;
		if (!EVP_CIPHER_CTX_set_key_length(&od->evp_ctx, od->klen))
			return PXE_CIPHER_INIT;
		if (!EVP_EncryptInit_ex(&od->evp_ctx, NULL, NULL, od->key, od->iv))
			return PXE_CIPHER_INIT;
		od->init = true;
	}

	if (!EVP_EncryptUpdate(&od->evp_ctx, res, &outlen, data, dlen))
		return PXE_ERR_GENERIC;

	return 0;
}
开发者ID:cconvey,项目名称:postgres,代码行数:24,代码来源:openssl.c


示例19: BIO_new

BIO *cms_EncryptedContent_init_bio(CMS_EncryptedContentInfo *ec)
{
    BIO *b;
    EVP_CIPHER_CTX *ctx;
    const EVP_CIPHER *ciph;
    X509_ALGOR *calg = ec->contentEncryptionAlgorithm;
    unsigned char iv[EVP_MAX_IV_LENGTH], *piv = NULL;
    unsigned char *tkey = NULL;
    size_t tkeylen = 0;

    int ok = 0;

    int enc, keep_key = 0;

    enc = ec->cipher ? 1 : 0;

    b = BIO_new(BIO_f_cipher());
    if (!b) {
        CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO, ERR_R_MALLOC_FAILURE);
        return NULL;
    }

    BIO_get_cipher_ctx(b, &ctx);

    if (enc) {
        ciph = ec->cipher;
        /*
         * If not keeping key set cipher to NULL so subsequent calls decrypt.
         */
        if (ec->key)
            ec->cipher = NULL;
    } else {
        ciph = EVP_get_cipherbyobj(calg->algorithm);

        if (!ciph) {
            CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO, CMS_R_UNKNOWN_CIPHER);
            goto err;
        }
    }

    if (EVP_CipherInit_ex(ctx, ciph, NULL, NULL, NULL, enc) <= 0) {
        CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO,
               CMS_R_CIPHER_INITIALISATION_ERROR);
        goto err;
    }

    if (enc) {
        int ivlen;
        calg->algorithm = OBJ_nid2obj(EVP_CIPHER_CTX_type(ctx));
        /* Generate a random IV if we need one */
        ivlen = EVP_CIPHER_CTX_iv_length(ctx);
        if (ivlen > 0) {
            if (RAND_bytes(iv, ivlen) <= 0)
                goto err;
            piv = iv;
        }
    } else if (EVP_CIPHER_asn1_to_param(ctx, calg->parameter) <= 0) {
        CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO,
               CMS_R_CIPHER_PARAMETER_INITIALISATION_ERROR);
        goto err;
    }
    tkeylen = EVP_CIPHER_CTX_key_length(ctx);
    /* Generate random session key */
    if (!enc || !ec->key) {
        tkey = OPENSSL_malloc(tkeylen);
        if (!tkey) {
            CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO, ERR_R_MALLOC_FAILURE);
            goto err;
        }
        if (EVP_CIPHER_CTX_rand_key(ctx, tkey) <= 0)
            goto err;
    }

    if (!ec->key) {
        ec->key = tkey;
        ec->keylen = tkeylen;
        tkey = NULL;
        if (enc)
            keep_key = 1;
        else
            ERR_clear_error();

    }

    if (ec->keylen != tkeylen) {
        /* If necessary set key length */
        if (EVP_CIPHER_CTX_set_key_length(ctx, ec->keylen) <= 0) {
            /*
             * Only reveal failure if debugging so we don't leak information
             * which may be useful in MMA.
             */
            if (enc || ec->debug) {
                CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO,
                       CMS_R_INVALID_KEY_LENGTH);
                goto err;
            } else {
                /* Use random key */
                OPENSSL_clear_free(ec->key, ec->keylen);
                ec->key = tkey;
                ec->keylen = tkeylen;
//.........这里部分代码省略.........
开发者ID:375670450,项目名称:openssl,代码行数:101,代码来源:cms_enc.c


示例20: ciphers_valid


//.........这里部分代码省略.........
		    keylen, cipher->name);
	if (iv != NULL && ivlen < cipher_ivlen(cipher))
		fatal("cipher_init: iv length %d is insufficient for %s.",
		    ivlen, cipher->name);
	cc->cipher = cipher;

	if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) {
		chachapoly_init(&cc->cp_ctx, key, keylen);
		return;
	}
	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,
	    (do_encrypt == CIPHER_ENCRYPT));
#else
	if (EVP_CipherInit(&cc->evp, type, NULL, (u_char *)iv,
	    (do_encrypt == CIPHER_ENCRYPT)) == 0)
		fatal("cipher_init: EVP_CipherInit failed for %s",
		    cipher->name);
	if (cipher_authlen(cipher) &&
	    !EVP_CIPHER_CTX_ctrl(&cc->evp, EVP_CTRL_GCM_SET_IV_FIXED,
	    -1, (u_char *)iv))
		fatal("cipher_init: EVP_CTRL_GCM_SET_IV_FIXED 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, (u_char *)key, NULL, -1) == 0)
		fatal("cipher_init: EVP_CipherInit: set key failed for %s",
		    cipher->name);
#endif

	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");
		explicit_bzero(discard, cipher->discard_len);
		free(junk);
		free(discard);
	}
}

/*
 * cipher_crypt() operates as following:
 * Copy 'aadlen' bytes (without en/decryption) from 'src' to 'dest'.
 * Theses bytes are treated as additional authenticated data for
 * authenticated encryption modes.
 * En/Decrypt 'len' bytes at offset 'aadlen' from 'src' to 'dest'.
 * Use 'authlen' bytes at offset 'len'+'aadlen' as the authentication tag.
 * This tag is written on encryption and verified on decryption.
 * Both 'aadlen' and 'authlen' can be set to 0.
 * cipher_crypt() returns 0 on success and -1 if the decryption integrity
 * check fails.
 */
开发者ID:Alkzndr,项目名称:freebsd,代码行数:67,代码来源:cipher.c



注:本文中的EVP_CIPHER_CTX_set_key_length函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C++ EVP_CIPHER_CTX_set_padding函数代码示例发布时间:2022-05-30
下一篇:
C++ EVP_CIPHER_CTX_new函数代码示例发布时间:2022-05-30
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap