本文整理汇总了C++中HMAC_Init函数的典型用法代码示例。如果您正苦于以下问题:C++ HMAC_Init函数的具体用法?C++ HMAC_Init怎么用?C++ HMAC_Init使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了HMAC_Init函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: hmac_init
HMACCTX hmac_init(const void *key, int len, int type) {
HMACCTX ctx = NULL;
ctx = malloc(sizeof(*ctx));
if (ctx == NULL) {
return NULL;
}
#ifndef OLD_CRYPTO
HMAC_CTX_init(ctx); // openssl 0.9.7 requires it.
#endif
switch(type) {
case HMAC_SHA1:
HMAC_Init(ctx, key, len, EVP_sha1());
break;
case HMAC_MD5:
HMAC_Init(ctx, key, len, EVP_md5());
break;
default:
SAFE_FREE(ctx);
ctx = NULL;
}
return ctx;
}
示例2: sftp_mac_set_read_key
int sftp_mac_set_read_key(pool *p, const EVP_MD *hash, const BIGNUM *k,
const char *h, uint32_t hlen) {
const unsigned char *id = NULL;
char *buf, *ptr;
uint32_t buflen, bufsz, id_len;
char letter;
size_t blocksz;
struct sftp_mac *mac;
HMAC_CTX *mac_ctx;
switch_read_mac();
mac = &(read_macs[read_mac_idx]);
mac_ctx = &(read_ctxs[read_mac_idx]);
bufsz = buflen = 1024;
ptr = buf = sftp_msg_getbuf(p, bufsz);
/* Need to use SSH2-style format of K for the key. */
sftp_msg_write_mpint(&buf, &buflen, k);
id_len = sftp_session_get_id(&id);
/* HASH(K || H || "E" || session_id) */
letter = 'E';
set_mac_key(mac, hash, ptr, (bufsz - buflen), h, hlen, &letter, id, id_len);
#if OPENSSL_VERSION_NUMBER > 0x000907000L
HMAC_CTX_init(mac_ctx);
# if OPENSSL_VERSION_NUMBER >= 0x10000001L
if (HMAC_Init_ex(mac_ctx, mac->key, mac->key_len, mac->digest, NULL) != 1) {
pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
"error initializing HMAC: %s", sftp_crypto_get_errors());
errno = EPERM;
return -1;
}
# else
HMAC_Init_ex(mac_ctx, mac->key, mac->key_len, mac->digest, NULL);
# endif /* OpenSSL-1.0.0 and later */
#else
/* Reset the HMAC context. */
HMAC_Init(mac_ctx, NULL, 0, NULL);
HMAC_Init(mac_ctx, mac->key, mac->key_len, mac->digest);
#endif
if (mac->mac_len == 0) {
blocksz = EVP_MD_size(mac->digest);
} else {
blocksz = mac->mac_len;
}
pr_memscrub(ptr, bufsz);
sftp_mac_set_block_size(blocksz);
return 0;
}
示例3: isc_hmacmd5_init
void
isc_hmacmd5_init(isc_hmacmd5_t *ctx, const unsigned char *key,
unsigned int len)
{
#ifdef HMAC_RETURN_INT
RUNTIME_CHECK(HMAC_Init(ctx, (const void *) key,
(int) len, EVP_md5()) == 1);
#else
HMAC_Init(ctx, (const void *) key, (int) len, EVP_md5());
#endif
}
示例4: HDW_generate_master_node
int HDW_generate_master_node(uint8_t *seed,
size_t seed_len,
HDW_XKEY_NET net,
HDW_xkey_t *key) {
int res;
HMAC_CTX hmac_ctx;
res = HMAC_Init(&hmac_ctx, BIP32_SPEC_DEFAULT_KEY, (int) strlen(BIP32_SPEC_DEFAULT_KEY), EVP_sha512());
if (!res) {
fprintf(stderr, "Could not HMAC_Init\n");
return res;
}
res = HMAC_Update(&hmac_ctx, seed, seed_len);
if (!res) {
fprintf(stderr, "Could not HMAC_Update\n");
goto cleanup_hmac_context;
}
uint8_t master_digest[SHA512_DIGEST_LENGTH];
uint32_t digest_len;
res = HMAC_Final(&hmac_ctx, master_digest, &digest_len);
if (digest_len != SHA512_DIGEST_LENGTH) {
// Oh ho, we might have smashed the stack :( . Abort everything!
// This should not happen at all.
fprintf(stderr, "Big problem at %s%d\n", __FILE__, __LINE__);
exit(-1);
}
if (!res) {
fprintf(stderr, "Could not HMAC_Final\n");
goto cleanup_hmac_context;
}
size_t half_hash_len = SHA512_DIGEST_LENGTH / 2;
memcpy(key->version, KEY_VERSIONS_VALUES[net | HDW_XKEY_TYPE_PRIVATE], sizeof(key->version));
// Copy the L and R part into the key.
key->key_data[0] = 0;
memcpy(key->key_data + 1, master_digest, half_hash_len);
memcpy(key->chain_code, master_digest + half_hash_len, half_hash_len);
// Set the rest of the data as master key.
key->depth = (uint8_t) 0;
memset(key->parent_fingerprint, 0, sizeof(key->parent_fingerprint));
memset(key->child_number, 0, sizeof(key->child_number));
cleanup_hmac_context:
HMAC_CTX_cleanup(&hmac_ctx);
return res;
}
示例5: mz_crypt_hmac_init
int32_t mz_crypt_hmac_init(void *handle, const void *key, int32_t key_length)
{
mz_crypt_hmac *hmac = (mz_crypt_hmac *)handle;
int32_t result = 0;
const EVP_MD *evp_md = NULL;
if (hmac == NULL || key == NULL)
return MZ_PARAM_ERROR;
mz_crypt_hmac_reset(handle);
hmac->ctx = HMAC_CTX_new();
if (hmac->algorithm == MZ_HASH_SHA1)
evp_md = EVP_sha1();
else
evp_md = EVP_sha256();
result = HMAC_Init(hmac->ctx, key, key_length, evp_md);
if (!result)
{
hmac->error = ERR_get_error();
return MZ_HASH_ERROR;
}
return MZ_OK;
}
示例6: insert_message_authenticator
static void
insert_message_authenticator(struct rad_handle *h, int resp)
{
#ifdef WITH_SSL
u_char md[EVP_MAX_MD_SIZE];
u_int md_len;
const struct rad_server *srvp;
HMAC_CTX ctx;
srvp = &h->servers[h->srv];
if (h->authentic_pos != 0) {
HMAC_CTX_init(&ctx);
HMAC_Init(&ctx, srvp->secret, strlen(srvp->secret), EVP_md5());
HMAC_Update(&ctx, &h->out[POS_CODE], POS_AUTH - POS_CODE);
if (resp)
HMAC_Update(&ctx, &h->in[POS_AUTH], LEN_AUTH);
else
HMAC_Update(&ctx, &h->out[POS_AUTH], LEN_AUTH);
HMAC_Update(&ctx, &h->out[POS_ATTRS],
h->out_len - POS_ATTRS);
HMAC_Final(&ctx, md, &md_len);
HMAC_CTX_cleanup(&ctx);
HMAC_cleanup(&ctx);
memcpy(&h->out[h->authentic_pos + 2], md, md_len);
}
#endif
}
示例7: __hmac_sha1_init
static int
__hmac_sha1_init(archive_hmac_sha1_ctx *ctx, const uint8_t *key, size_t key_len)
{
HMAC_CTX_init(ctx);
HMAC_Init(ctx, key, key_len, EVP_sha1());
return 0;
}
示例8: HMAC_CTX_init
// Signing functions
bool OSSLEVPMacAlgorithm::signInit(const SymmetricKey* key)
{
// Call the superclass initialiser
if (!MacAlgorithm::signInit(key))
{
return false;
}
// Initialize the context
HMAC_CTX_init(&curCTX);
// Initialize EVP signing
if (!HMAC_Init(&curCTX, key->getKeyBits().const_byte_str(), key->getKeyBits().size(), getEVPHash()))
{
ERROR_MSG("HMAC_Init failed");
HMAC_CTX_cleanup(&curCTX);
ByteString dummy;
MacAlgorithm::signFinal(dummy);
return false;
}
return true;
}
示例9: H_Init
/* The random function H(x) = HMAC-SHA256(0^32, x) */
void H_Init(HMAC_CTX *ctx)
{
u8 allzero[SHA256_DIGEST_LENGTH];
os_memset(allzero, 0, SHA256_DIGEST_LENGTH);
HMAC_Init(ctx, allzero, SHA256_DIGEST_LENGTH, EVP_sha256());
}
示例10: openssl_hmac_md5
void openssl_hmac_md5()
{
int fd, size;
unsigned int len;
HMAC_CTX hmac_ctx;
char file_name[COMM_LEN], inputs[COMM_LEN];
unsigned char tmps[LINE_LEN], outputs[EVP_MAX_MD_SIZE];
memset(tmps, 0, sizeof(tmps));
memset(inputs, 0, sizeof(inputs));
memset(outputs, 0, sizeof(outputs));
printf("\nPlease input a file name: ");
scanf("%s", file_name);
fd = open(file_name, O_RDONLY);
if (fd < 0)
return;
OpenSSL_add_all_digests();
strcpy(inputs, "hmac_md5");
HMAC_Init(&hmac_ctx, inputs, sizeof(inputs), EVP_get_digestbyname("md5"));
while ((size = read(fd, tmps, LINE_LEN)) > 0)
HMAC_Update(&hmac_ctx, tmps, size);
HMAC_Final(&hmac_ctx, outputs, &len);
HMAC_cleanup(&hmac_ctx);
close(fd);
printf("HMAC_MD5(%s, %s) = ", file_name, inputs);
for (size = 0; size < len; size++)
printf("%02x", outputs[size]);
printf("\n");
}
示例11: mac_compute
int
mac_compute(Mac *mac, u_int32_t seqno, u_char *data, int datalen,
u_char *digest, size_t dlen)
{
static u_char m[MAC_DIGEST_LEN_MAX];
u_char b[4], nonce[8];
if (mac->mac_len > sizeof(m))
return SSH_ERR_INTERNAL_ERROR;
switch (mac->type) {
case SSH_EVP:
POKE_U32(b, seqno);
/* reset HMAC context */
if (HMAC_Init(&mac->evp_ctx, NULL, 0, NULL) != 1 ||
HMAC_Update(&mac->evp_ctx, b, sizeof(b)) != 1 ||
HMAC_Update(&mac->evp_ctx, data, datalen) != 1 ||
HMAC_Final(&mac->evp_ctx, m, NULL) != 1)
return SSH_ERR_LIBCRYPTO_ERROR;
break;
case SSH_UMAC:
POKE_U64(nonce, seqno);
umac_update(mac->umac_ctx, data, datalen);
umac_final(mac->umac_ctx, m, nonce);
break;
default:
return SSH_ERR_INVALID_ARGUMENT;
}
if (digest != NULL) {
if (dlen > mac->mac_len)
dlen = mac->mac_len;
memcpy(digest, m, dlen);
}
return 0;
}
示例12: mac_compute
u_char *
mac_compute(Mac *mac, u_int32_t seqno, u_char *data, int datalen)
{
static u_char m[EVP_MAX_MD_SIZE];
u_char b[4];
if (mac->mac_len > sizeof(m))
fatal("mac_compute: mac too long %u %lu",
mac->mac_len, (u_long)sizeof(m));
switch (mac->type) {
case SSH_EVP:
put_u32(b, seqno);
/* reset HMAC context */
HMAC_Init(&mac->evp_ctx, NULL, 0, NULL);
HMAC_Update(&mac->evp_ctx, b, sizeof(b));
HMAC_Update(&mac->evp_ctx, data, datalen);
HMAC_Final(&mac->evp_ctx, m, NULL);
break;
default:
fatal("mac_compute: unknown MAC type");
}
return (m);
}
示例13: mac_init
int
mac_init(struct sshmac *mac)
{
if (mac->key == NULL)
return SSH_ERR_INVALID_ARGUMENT;
switch (mac->type) {
case SSH_EVP:
if (mac->evp_md == NULL)
return SSH_ERR_INVALID_ARGUMENT;
HMAC_CTX_init(&mac->evp_ctx);
if (HMAC_Init(&mac->evp_ctx, mac->key, mac->key_len,
mac->evp_md) != 1) {
HMAC_CTX_cleanup(&mac->evp_ctx);
return SSH_ERR_LIBCRYPTO_ERROR;
}
return 0;
case SSH_UMAC:
if ((mac->umac_ctx = umac_new(mac->key)) == NULL)
return SSH_ERR_ALLOC_FAIL;
return 0;
case SSH_UMAC128:
mac->umac_ctx = umac128_new(mac->key);
return 0;
default:
return SSH_ERR_INVALID_ARGUMENT;
}
}
示例14: soap_smd_init
/**
@fn int soap_smd_init(struct soap *soap, struct soap_smd_data *data, int alg, const void *key, int keylen)
@brief Initiates a (signed) digest computation.
@param soap context
@param[in,out] data smdevp engine context
@param[in] alg is algorithm to use
@param[in] key is key to use or NULL for digests
@param[in] keylen is length of HMAC key (when provided)
@return SOAP_OK or SOAP_SSL_ERROR
*/
int
soap_smd_init(struct soap *soap, struct soap_smd_data *data, int alg, const void *key, int keylen)
{ static int done = 0;
int err = 1;
#ifdef WITH_OPENSSL
/* OpenSSL: make sure we have the digest algorithms, need to call just once */
if (!done)
{ done = 1;
OpenSSL_add_all_digests();
OpenSSL_add_all_algorithms();
}
#endif
/* the algorithm to use */
data->alg = alg;
/* the key to use */
data->key = key;
/* allocate and init the OpenSSL HMAC or EVP_MD context */
if ((alg & (SOAP_SMD_PASSTHRU-1)) == SOAP_SMD_HMAC_SHA1)
{ data->ctx = (void*)SOAP_MALLOC(soap, sizeof(HMAC_CTX));
HMAC_CTX_init((HMAC_CTX*)data->ctx);
}
else
{ data->ctx = (void*)SOAP_MALLOC(soap, sizeof(EVP_MD_CTX));
EVP_MD_CTX_init((EVP_MD_CTX*)data->ctx);
}
DBGLOG(TEST, SOAP_MESSAGE(fdebug, "-- SMD Init alg=%d (%p) --\n", alg, data->ctx));
/* init the digest or signature computations */
switch (alg & (SOAP_SMD_PASSTHRU-1))
{ case SOAP_SMD_DGST_MD5:
EVP_DigestInit((EVP_MD_CTX*)data->ctx, EVP_md5());
break;
case SOAP_SMD_DGST_SHA1:
EVP_DigestInit((EVP_MD_CTX*)data->ctx, EVP_sha1());
break;
case SOAP_SMD_DGST_SHA256:
EVP_DigestInit((EVP_MD_CTX*)data->ctx, EVP_sha256());
break;
case SOAP_SMD_HMAC_SHA1:
HMAC_Init((HMAC_CTX*)data->ctx, key, keylen, EVP_sha1());
break;
case SOAP_SMD_SIGN_DSA_SHA1:
err = EVP_SignInit((EVP_MD_CTX*)data->ctx, EVP_dss1());
break;
case SOAP_SMD_SIGN_RSA_SHA1:
err = EVP_SignInit((EVP_MD_CTX*)data->ctx, EVP_sha1());
break;
case SOAP_SMD_SIGN_RSA_SHA256:
err = EVP_SignInit((EVP_MD_CTX*)data->ctx, EVP_sha256());
break;
case SOAP_SMD_VRFY_DSA_SHA1:
err = EVP_VerifyInit((EVP_MD_CTX*)data->ctx, EVP_dss1());
break;
case SOAP_SMD_VRFY_RSA_SHA1:
err = EVP_VerifyInit((EVP_MD_CTX*)data->ctx, EVP_sha1());
break;
}
/* check and return */
return soap_smd_check(soap, data, err, "soap_smd_init() failed");
}
示例15: hmac
QByteArray SSH2MAC::mac(const QByteArray & data)
{
QByteArray hmac(m_macLen, 0);
HMAC_Init(m_ctx, (const uint8_t*) m_key.data(), m_keyLen, m_evptype);
HMAC_Update(m_ctx, (const uint8_t *) data.data(), data.size());
HMAC_Final(m_ctx, (uint8_t *) hmac.data(), NULL);
return hmac;
}