当前位置: 首页>>代码示例>>C++>>正文


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怎么用?C++ EVP_CIPHER_CTX_set_key_length使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


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


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