本文整理汇总了C++中HMAC_Init_ex函数的典型用法代码示例。如果您正苦于以下问题:C++ HMAC_Init_ex函数的具体用法?C++ HMAC_Init_ex怎么用?C++ HMAC_Init_ex使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了HMAC_Init_ex函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
int main(int argc, char *argv[]) {
if(argc != 2) {
printf("Usage: ./test_app <file_name>\n");
return -1;
}
ENGINE *e = load_engine(ENGINE_MODULE, "test_engine");
if(e == 0) {
printf("Unable to load engine\n");
return -1;
}
ENGINE_ctrl_cmd_string(e, "username", "user", 0);
ENGINE_ctrl_cmd_string(e, "password", "password", 0);
ENGINE_init(e);
HMAC_CTX ctx;
HMAC_CTX_init(&ctx);
HMAC_Init_ex(&ctx, "test_key\0", 9, EVP_sha1(), e);
FILE *f = fopen(argv[1], "r");
char buf[BUF_SIZE];
while(!feof(f)) {
size_t ln = fread(buf, sizeof(char), BUF_SIZE, f);
if(ln == 0) continue;
HMAC_Update(&ctx, buf, ln);
}
fclose(f);
unsigned int siglen;
unsigned char md[20];
HMAC_Final(&ctx, md, &siglen);
ENGINE_finish(e);
printf("HMAC-SHA1: ");
for(size_t i = 0; i < siglen; i++) printf("%02x", md[i]);
printf("\n");
return 0;
}
示例2: init
void init(RC4_KEY* rc4, const unsigned char* session_key, const unsigned char* peer_key)
{
HMAC_CTX hmac_ctx;
HMAC_CTX_init(&hmac_ctx);
HMAC_Init_ex(&hmac_ctx, (void*)peer_key, peer_key_size, EVP_sha1(), NULL);
HMAC_Update(&hmac_ctx, session_key, session_key_size);
unsigned char rc4_keystring[SHA_DIGEST_LENGTH];
unsigned int rc4_keystring_size = SHA_DIGEST_LENGTH;
HMAC_Final(&hmac_ctx, rc4_keystring, &rc4_keystring_size);
HMAC_CTX_cleanup(&hmac_ctx);
RC4_set_key(rc4, rc4_keystring_size, rc4_keystring);
const unsigned char rc4_key_init[rc4_init_size] = { 0x00 };
unsigned char rc4_key_init_output[rc4_init_size];
RC4(rc4, sizeof(rc4_key_init), rc4_key_init, rc4_key_init_output);
}
示例3: dsa_init
int
dsa_init(struct iked_dsa *dsa, const void *buf, size_t len)
{
int ret;
if (dsa->dsa_hmac) {
if (!HMAC_Init_ex(dsa->dsa_ctx, ibuf_data(dsa->dsa_keydata),
ibuf_length(dsa->dsa_keydata), dsa->dsa_priv, NULL))
return (-1);
return (0);
}
if (dsa->dsa_sign)
ret = EVP_SignInit_ex(dsa->dsa_ctx, dsa->dsa_priv, NULL);
else {
if ((ret = _dsa_verify_init(dsa, buf, len)) != 0)
return (ret);
ret = EVP_VerifyInit_ex(dsa->dsa_ctx, dsa->dsa_priv, NULL);
}
return (ret ? 0 : -1);
}
示例4: cmeHMACInit
int cmeHMACInit (HMAC_CTX **ctx, ENGINE *engine, EVP_MD *digest, const char *key, int keyLen)
{
int result;
*ctx=(HMAC_CTX *)malloc(sizeof(HMAC_CTX));
HMAC_CTX_init(*ctx); //Initialize HMAC context.
result= HMAC_Init_ex(*ctx,key,keyLen,digest,engine);
if (result==0) //1= success, 0=failure
{
#ifdef ERROR_LOG
fprintf(stderr,"CaumeDSE Error: cmeHMACInit(), HMAC_Init_ex() failure!\n");
#endif
return (1);
}
else
{
#ifdef DEBUG
fprintf(stdout,"CaumeDSE Debug: cmeHMACInit(), HMAC_Init_ex() success.\n");
#endif
return (0);
}
示例5: aead_tls_init
static int aead_tls_init(EVP_AEAD_CTX *ctx, const uint8_t *key, size_t key_len,
size_t tag_len, enum evp_aead_direction_t dir,
const EVP_CIPHER *cipher, const EVP_MD *md,
char implicit_iv) {
if (tag_len != EVP_AEAD_DEFAULT_TAG_LENGTH &&
tag_len != EVP_MD_size(md)) {
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_UNSUPPORTED_TAG_SIZE);
return 0;
}
if (key_len != EVP_AEAD_key_length(ctx->aead)) {
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_KEY_LENGTH);
return 0;
}
size_t mac_key_len = EVP_MD_size(md);
size_t enc_key_len = EVP_CIPHER_key_length(cipher);
assert(mac_key_len + enc_key_len +
(implicit_iv ? EVP_CIPHER_iv_length(cipher) : 0) == key_len);
AEAD_TLS_CTX *tls_ctx = (AEAD_TLS_CTX *)&ctx->state;
EVP_CIPHER_CTX_init(&tls_ctx->cipher_ctx);
HMAC_CTX_init(&tls_ctx->hmac_ctx);
assert(mac_key_len <= EVP_MAX_MD_SIZE);
OPENSSL_memcpy(tls_ctx->mac_key, key, mac_key_len);
tls_ctx->mac_key_len = (uint8_t)mac_key_len;
tls_ctx->implicit_iv = implicit_iv;
if (!EVP_CipherInit_ex(&tls_ctx->cipher_ctx, cipher, NULL, &key[mac_key_len],
implicit_iv ? &key[mac_key_len + enc_key_len] : NULL,
dir == evp_aead_seal) ||
!HMAC_Init_ex(&tls_ctx->hmac_ctx, key, mac_key_len, md, NULL)) {
aead_tls_cleanup(ctx);
return 0;
}
EVP_CIPHER_CTX_set_padding(&tls_ctx->cipher_ctx, 0);
return 1;
}
示例6: hmac_fdigest
static int hmac_fdigest(lua_State *L)
{
const char *t = luaL_checkstring(L, 1);
const EVP_MD *type = EVP_get_digestbyname(t);
const char *s;
const char *k;
unsigned char digest[EVP_MAX_MD_SIZE];
unsigned int written = 0;
unsigned int i;
char *hex;
HMAC_CTX c;
if (type == NULL) {
luaL_argerror(L, 1, "invalid digest type");
return 0;
}
s = luaL_checkstring(L, 2);
k = luaL_checkstring(L, 3);
HMAC_CTX_init(&c);
HMAC_Init_ex(&c, k, (int)lua_strlen(L, 3), type, NULL);
HMAC_Update(&c, (unsigned char *)s, lua_strlen(L, 2));
HMAC_Final(&c, digest, &written);
HMAC_CTX_cleanup(&c);
if (lua_toboolean(L, 4))
lua_pushlstring(L, (char *)digest, written);
else {
hex = (char*)calloc(sizeof(char), written*2 + 1);
for (i = 0; i < written; i++)
sprintf(hex + 2*i, "%02x", digest[i]);
lua_pushlstring(L, hex, written*2);
free(hex);
}
return 1;
}
示例7: calculate_auth_data
static int calculate_auth_data(const struct iovec *iov, int iovlen,
const struct in6_addr *coa,
const struct in6_addr *cn,
const uint8_t *key, uint8_t *digest)
{
uint8_t buf[HMAC_SHA1_HASH_LEN];
int i;
#ifdef HAVE_LIBCRYPTO
unsigned int len = HMAC_SHA1_HASH_LEN;
HMAC_CTX ctx;
const EVP_MD *evp_md = EVP_sha1();
HMAC_CTX_init(&ctx);
HMAC_Init_ex(&ctx, key, HMAC_SHA1_KEY_SIZE, evp_md, NULL);
HMAC_Update(&ctx, (uint8_t *)coa, sizeof(*coa));
HMAC_Update(&ctx, (uint8_t *)cn, sizeof(*coa));
for (i = 0; i < iovlen; i++) {
HMAC_Update(&ctx, (uint8_t *)iov[i].iov_base, iov[i].iov_len);
}
HMAC_Final(&ctx, buf, &len);
HMAC_CTX_cleanup(&ctx);
#else
HMAC_SHA1_CTX ctx;
HMAC_SHA1_init(&ctx, key, HMAC_SHA1_KEY_SIZE);
HMAC_SHA1_update(&ctx, (uint8_t *)coa, sizeof(*coa));
HMAC_SHA1_update(&ctx, (uint8_t *)cn, sizeof(*coa));
for (i = 0; i < iovlen; i++) {
HMAC_SHA1_update(&ctx, (uint8_t *)iov[i].iov_base,
iov[i].iov_len);
}
HMAC_SHA1_final(&ctx, buf);
#endif
memcpy(digest, buf, MIPV6_DIGEST_LEN);
return 0;
}
示例8: PKI_HMAC_init
/*
* \brief Initializes the passed hmac to use the passed key and digest algorithm
*/
int PKI_HMAC_init(PKI_HMAC *hmac, unsigned char *key, size_t key_size, PKI_DIGEST_ALG *digest, HSM *hsm)
{
if (!hmac) return PKI_ERROR(PKI_ERR_PARAM_NULL, NULL);
// Free the memory if another key was used
if (hmac->key) PKI_MEM_free(hmac->key);
hmac->key = NULL;
if (hmac->value) PKI_MEM_free(hmac->value);
hmac->value = NULL;
// Generate the new PKI_MEM to hold the key data
hmac->key = PKI_MEM_new_data(key_size, key);
if (!hmac->key || !hmac->key->data || hmac->key->size <= 0)
{
hmac->initialized = 0;
return PKI_ERROR(PKI_ERR_MEMORY_ALLOC, NULL);
}
// Sets the algoritm
hmac->digestAlg = digest ? digest : PKI_DIGEST_ALG_SHA1;
// Checks if HSM implementation was asked by the developer
if (hsm)
{
PKI_ERROR(PKI_ERR_GENERAL, "Code to support HMAC on HSMs not implemented, yet.");
hmac->initialized = 0;
return PKI_ERR;
}
// Initializes the Context
HMAC_Init_ex(&hmac->ctx, (const void *) key, (int) key_size, hmac->digestAlg, NULL);
// Sets the initialization flag
hmac->initialized = 1;
return PKI_OK;
}
示例9: HmacMd5Init
/**
Initializes user-supplied memory pointed by HmacMd5Context as HMAC-MD5 context for
subsequent use.
If HmacMd5Context is NULL, then return FALSE.
@param[out] HmacMd5Context Pointer to HMAC-MD5 context being initialized.
@param[in] Key Pointer to the user-supplied key.
@param[in] KeySize Key size in bytes.
@retval TRUE HMAC-MD5 context initialization succeeded.
@retval FALSE HMAC-MD5 context initialization failed.
**/
BOOLEAN
EFIAPI
HmacMd5Init (
OUT VOID *HmacMd5Context,
IN CONST UINT8 *Key,
IN UINTN KeySize
)
{
//
// Check input parameters.
//
if (HmacMd5Context == NULL || KeySize > INT_MAX) {
return FALSE;
}
//
// OpenSSL HMAC-MD5 Context Initialization
//
HMAC_CTX_init (HmacMd5Context);
HMAC_Init_ex (HmacMd5Context, Key, (UINT32) KeySize, EVP_md5(), NULL);
return TRUE;
}
示例10: hmac_fnew
static int hmac_fnew(lua_State *L)
{
HANDLER_HMAC *c = hmac_pnew(L);
const char *s = luaL_checkstring(L, 1);
size_t k_len;
const char *k = luaL_checklstring(L, 2, &k_len);
DIGEST_TYPE type = DIGEST_BY_NAME(s);
if (IS_DIGEST_INVALID(type)) {
luaL_argerror(L, 1, "invalid digest type");
return 0;
}
#if CRYPTO_OPENSSL
HMAC_CTX_init(c);
HMAC_Init_ex(c, k, k_len, type, NULL);
#elif CRYPTO_GCRYPT
gcry_md_open(c, type, GCRY_MD_FLAG_HMAC);
gcry_md_setkey(*c, k, k_len);
#endif
return 1;
}
示例11: fr_hmac_md5
/** Calculate HMAC using OpenSSL's MD5 implementation
*
* @param digest Caller digest to be filled in.
* @param in Pointer to data stream.
* @param inlen length of data stream.
* @param key Pointer to authentication key.
* @param key_len Length of authentication key.
*
*/
void fr_hmac_md5(uint8_t digest[MD5_DIGEST_LENGTH], uint8_t const *in, size_t inlen,
uint8_t const *key, size_t key_len)
{
HMAC_CTX *ctx;
if (unlikely(!md5_hmac_ctx)) {
ctx = HMAC_CTX_new();
if (unlikely(!ctx)) return;
fr_thread_local_set_destructor(md5_hmac_ctx, _hmac_md5_ctx_free_on_exit, ctx);
} else {
ctx = md5_hmac_ctx;
}
#ifdef EVP_MD_CTX_FLAG_NON_FIPS_ALLOW
/* Since MD5 is not allowed by FIPS, explicitly allow it. */
HMAC_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
#endif /* EVP_MD_CTX_FLAG_NON_FIPS_ALLOW */
HMAC_Init_ex(ctx, key, key_len, EVP_md5(), NULL);
HMAC_Update(ctx, in, inlen);
HMAC_Final(ctx, digest, NULL);
HMAC_CTX_reset(ctx);
}
示例12: malloc
char* CryptoHandler::hmac_sha512(char* datain, char* keyin, const bool& base64)
{
unsigned char* key = (unsigned char*) keyin;
unsigned char* data = (unsigned char*) datain;
unsigned char* result;
unsigned int result_len = 64;
HMAC_CTX ctx;
result = (unsigned char*) malloc(sizeof(char) * result_len);
ENGINE_load_builtin_engines();
ENGINE_register_all_complete();
HMAC_CTX_init(&ctx);
HMAC_Init_ex(&ctx, key, strlen(keyin), EVP_sha512(), NULL);
HMAC_Update(&ctx, data, strlen(datain));
HMAC_Final(&ctx, result, &result_len);
HMAC_CTX_cleanup(&ctx);
if(base64)
return base64encode(result,result_len);
return (char*)result;
}
示例13: HmacSha1Init
/**
Initializes user-supplied memory pointed by HmacSha1Context as HMAC-SHA1 context for
subsequent use.
If HmacSha1Context is NULL, then return FALSE.
@param[out] HmacSha1Context Pointer to HMAC-SHA1 context being initialized.
@param[in] Key Pointer to the user-supplied key.
@param[in] KeySize Key size in bytes.
@retval TRUE HMAC-SHA1 context initialization succeeded.
@retval FALSE HMAC-SHA1 context initialization failed.
**/
BOOLEAN
EFIAPI
HmacSha1Init (
OUT VOID *HmacSha1Context,
IN CONST UINT8 *Key,
IN UINTN KeySize
)
{
//
// Check input parameters.
//
if (HmacSha1Context == NULL) {
return FALSE;
}
//
// OpenSSL HMAC-SHA1 Context Initialization
//
HMAC_CTX_init (HmacSha1Context);
HMAC_Init_ex (HmacSha1Context, Key, (UINT32) KeySize, EVP_sha1(), NULL);
return TRUE;
}
示例14: mac_init
int
mac_init(Mac *mac)
{
if (mac->key == NULL)
fatal("mac_init: no key");
switch (mac->type) {
case SSH_EVP:
if (mac->evp_md == NULL)
return -1;
#ifdef HAVE_HMAC_CTX_INIT
HMAC_CTX_init(&mac->evp_ctx);
HMAC_Init_ex(&mac->evp_ctx, mac->key, mac->key_len, mac->evp_md, NULL);
#else
HMAC_Init(&mac->evp_ctx, mac->key, mac->key_len, mac->evp_md);
#endif
return 0;
case SSH_UMAC:
mac->umac_ctx = umac_new(mac->key);
return 0;
default:
return -1;
}
}
示例15:
unsigned char *HMAC(const EVP_MD *evp_md, const void *key, int key_len,
const unsigned char *d, size_t n, unsigned char *md,
unsigned int *md_len)
{
HMAC_CTX *c = NULL;
static unsigned char m[EVP_MAX_MD_SIZE];
if (md == NULL)
md = m;
if ((c = HMAC_CTX_new()) == NULL)
goto err;
if (!HMAC_Init_ex(c, key, key_len, evp_md, NULL))
goto err;
if (!HMAC_Update(c, d, n))
goto err;
if (!HMAC_Final(c, md, md_len))
goto err;
HMAC_CTX_free(c);
return md;
err:
HMAC_CTX_free(c);
return NULL;
}