本文整理汇总了C++中EVP_DigestFinal函数的典型用法代码示例。如果您正苦于以下问题:C++ EVP_DigestFinal函数的具体用法?C++ EVP_DigestFinal怎么用?C++ EVP_DigestFinal使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了EVP_DigestFinal函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: snmp_passwd_to_keys
/* [RFC 3414] - A.2. Password to Key Algorithm */
enum snmp_code
snmp_passwd_to_keys(struct snmp_user *user, char *passwd)
{
int err, loop, i, pwdlen;
uint32_t keylen, olen;
const EVP_MD *dtype;
EVP_MD_CTX ctx;
uint8_t authbuf[SNMP_AUTH_BUF_SIZE];
if (passwd == NULL || user == NULL)
return (SNMP_CODE_FAILED);
err = snmp_digest_init(user, &ctx, &dtype, &keylen);
if (err < 0)
return (SNMP_CODE_BADDIGEST);
else if (err == 0)
return (SNMP_CODE_OK);
memset(user->auth_key, 0, sizeof(user->auth_key));
pwdlen = strlen(passwd);
for (loop = 0; loop < SNMP_AUTH_KEY_LOOPCNT; loop += i) {
for (i = 0; i < SNMP_EXTENDED_KEY_SIZ; i++)
authbuf[i] = passwd[(loop + i) % pwdlen];
if (EVP_DigestUpdate(&ctx, authbuf, SNMP_EXTENDED_KEY_SIZ) != 1)
goto failed;
}
if (EVP_DigestFinal(&ctx, user->auth_key, &olen) != 1)
goto failed;
EVP_MD_CTX_cleanup(&ctx);
return (SNMP_CODE_OK);
failed:
EVP_MD_CTX_cleanup(&ctx);
return (SNMP_CODE_BADDIGEST);
}
示例2: snmp_get_local_keys
/* [RFC 3414] - 2.6. Key Localization Algorithm */
enum snmp_code
snmp_get_local_keys(struct snmp_user *user, uint8_t *eid, uint32_t elen)
{
int err;
uint32_t keylen, olen;
const EVP_MD *dtype;
EVP_MD_CTX ctx;
uint8_t authbuf[SNMP_AUTH_BUF_SIZE];
if (user == NULL || eid == NULL || elen > SNMP_ENGINE_ID_SIZ)
return (SNMP_CODE_FAILED);
memset(user->priv_key, 0, sizeof(user->priv_key));
memset(authbuf, 0, sizeof(authbuf));
err = snmp_digest_init(user, &ctx, &dtype, &keylen);
if (err < 0)
return (SNMP_CODE_BADDIGEST);
else if (err == 0)
return (SNMP_CODE_OK);
memcpy(authbuf, user->auth_key, keylen);
memcpy(authbuf + keylen, eid, elen);
memcpy(authbuf + keylen + elen, user->auth_key, keylen);
if (EVP_DigestUpdate(&ctx, authbuf, 2 * keylen + elen) != 1 ||
EVP_DigestFinal(&ctx, user->auth_key, &olen) != 1) {
EVP_MD_CTX_cleanup(&ctx);
return (SNMP_CODE_BADDIGEST);
}
EVP_MD_CTX_cleanup(&ctx);
if (user->priv_proto != SNMP_PRIV_NOPRIV)
memcpy(user->priv_key, user->auth_key, sizeof(user->priv_key));
return (SNMP_CODE_OK);
}
示例3: return
//Copied from aegis-crypto0 bin/accli.c
int PackageUtils::computeDigest(int ih, unsigned char* digest, ssize_t maxdigestlen)
{
EVP_MD_CTX mdctx;
unsigned int mdlen;
unsigned char data[512];
int rc;
ssize_t len;
if (maxdigestlen < DIGESTLEN)
return(-EINVAL);
rc = EVP_DigestInit(&mdctx, DIGESTTYP());
if (EVPOK != rc) {
qDebug()<<"EVP_DigestInit returns "<< rc;
return 0;
}
while (0 < (len = read(ih, data, sizeof(data)))) {
rc = EVP_DigestUpdate(&mdctx, data, len);
if (EVPOK != rc) {
qDebug()<<"EVP_DigestUpdate returns:"<<rc<<Q_FUNC_INFO;
return 0;
}
if (len < sizeof(data))
break;
}
rc = EVP_DigestFinal(&mdctx, digest, &mdlen);
if (rc != EVPOK) {
qDebug()<<"EVP_DigestUpdate returns:"<<rc<<Q_FUNC_INFO;
return(0);
}
EVP_MD_CTX_cleanup(&mdctx);
return mdlen;
}
示例4: gfs_digest_calculate_local
/*
* Not really public interface,
* but common routine called from both client and server.
*/
int
gfs_digest_calculate_local(int fd, char *buffer, size_t buffer_size,
const EVP_MD *md_type, EVP_MD_CTX *md_ctx,
size_t *md_lenp, unsigned char *md_value,
file_offset_t *filesizep)
{
int size;
file_offset_t off = 0;
unsigned int len;
if (lseek(fd, (off_t)0, 0) == -1)
return (errno);
EVP_DigestInit(md_ctx, md_type);
while ((size = read(fd, buffer, buffer_size)) > 0) {
EVP_DigestUpdate(md_ctx, buffer, size);
off += size;
}
EVP_DigestFinal(md_ctx, md_value, &len);
*md_lenp = len;
*filesizep = off;
return (size == -1 ? errno : 0);
}
示例5: EVP_hexdigest
static PyObject *
EVP_hexdigest(EVPobject *self, PyObject *unused)
{
unsigned char digest[EVP_MAX_MD_SIZE];
EVP_MD_CTX *temp_ctx;
unsigned int digest_size;
temp_ctx = EVP_MD_CTX_new();
if (temp_ctx == NULL) {
PyErr_NoMemory();
return NULL;
}
/* Get the raw (binary) digest value */
if (!locked_EVP_MD_CTX_copy(temp_ctx, self)) {
return _setException(PyExc_ValueError);
}
digest_size = EVP_MD_CTX_size(temp_ctx);
EVP_DigestFinal(temp_ctx, digest, NULL);
EVP_MD_CTX_free(temp_ctx);
return _Py_strhex((const char *)digest, digest_size);
}
示例6: gfs_digest_calculate_local
/*
* Not really public interface,
* but common routine called from both client and server.
*/
int
gfs_digest_calculate_local(int fd, char *buffer, size_t buffer_size,
const EVP_MD *md_type, EVP_MD_CTX *md_ctx,
size_t *md_lenp, unsigned char *md_value,
gfarm_off_t *filesizep)
{
int size, save_errno;
gfarm_off_t off = 0;
unsigned int len;
if (lseek(fd, (off_t)0, 0) == -1) {
save_errno = errno;
gflog_debug(GFARM_MSG_1001020, "lseek() failed: %s",
strerror(save_errno));
return (save_errno);
}
EVP_DigestInit(md_ctx, md_type);
while ((size = read(fd, buffer, buffer_size)) > 0) {
EVP_DigestUpdate(md_ctx, buffer, size);
off += size;
}
EVP_DigestFinal(md_ctx, md_value, &len);
*md_lenp = len;
*filesizep = off;
if (size == -1) {
save_errno = errno;
gflog_debug(GFARM_MSG_1001021, "read() failed: %s",
strerror(save_errno));
return (save_errno);
}
return (0);
}
示例7: EVP_digest
static PyObject *
EVP_digest(EVPobject *self, PyObject *unused)
{
unsigned char digest[EVP_MAX_MD_SIZE];
EVP_MD_CTX *temp_ctx;
PyObject *retval;
unsigned int digest_size;
temp_ctx = EVP_MD_CTX_new();
if (temp_ctx == NULL) {
PyErr_NoMemory();
return NULL;
}
if (!locked_EVP_MD_CTX_copy(temp_ctx, self)) {
return _setException(PyExc_ValueError);
}
digest_size = EVP_MD_CTX_size(temp_ctx);
EVP_DigestFinal(temp_ctx, digest, NULL);
retval = PyBytes_FromStringAndSize((const char *)digest, digest_size);
EVP_MD_CTX_free(temp_ctx);
return retval;
}
示例8: sl_digest
static void sl_digest (void){
SLang_BString_Type* data; /* we will give a slang string */
unsigned char output[EVP_MAX_MD_SIZE];
const EVP_MD *md;
EVP_MD_CTX ctx;
char* dtype;
int dlen, hashlen;
SLang_BString_Type *out;
if (SLang_Num_Function_Args != 2 ||
SLang_pop_slstring(&dtype) == -1 ){
return;}
md = EVP_get_digestbyname(dtype);
if (!md){
SLang_verror(SL_UNDEFINED_NAME,"could not find digest %s",dtype);
SLang_free_slstring(dtype);
return;
}
if (SLang_pop_bstring(&data) == -1 ){
return;
}
unsigned char* idata = SLbstring_get_pointer (data,&dlen);
EVP_MD_CTX_init(&ctx);
EVP_DigestInit_ex(&ctx, md, NULL);
EVP_DigestUpdate(&ctx, idata, dlen);
EVP_DigestFinal(&ctx, output, &hashlen);
out = SLbstring_create (output, hashlen);
SLang_push_bstring(out);
SLbstring_free(data);
SLbstring_free(out);
}
示例9: ssh_dss_verify
int ssh_dss_verify(DSA *key,
u_char *signature, u_int signaturelen,
u_char *data, u_int datalen)
{
DSA_SIG *sig;
const EVP_MD *evp_md = EVP_sha1();
EVP_MD_CTX md;
unsigned char digest[EVP_MAX_MD_SIZE], *sigblob;
unsigned int len, dlen;
int ret;
char *ptr;
OpenSSL_add_all_digests();
if (key == NULL) {
return -2;
}
ptr = signature;
// step1
if (signaturelen == 0x28) {
// workaround for SSH-2.0-2.0* and SSH-2.0-2.1* (2006.11.18 maya)
ptr -= 4;
}
else {
len = get_uint32_MSBfirst(ptr);
ptr += 4;
if (strncmp("ssh-dss", ptr, len) != 0) {
return -3;
}
ptr += len;
}
// step2
len = get_uint32_MSBfirst(ptr);
ptr += 4;
sigblob = ptr;
ptr += len;
if (len != SIGBLOB_LEN) {
return -4;
}
/* parse signature */
if ((sig = DSA_SIG_new()) == NULL)
return -5;
if ((sig->r = BN_new()) == NULL)
return -6;
if ((sig->s = BN_new()) == NULL)
return -7;
BN_bin2bn(sigblob, INTBLOB_LEN, sig->r);
BN_bin2bn(sigblob+ INTBLOB_LEN, INTBLOB_LEN, sig->s);
/* sha1 the data */
EVP_DigestInit(&md, evp_md);
EVP_DigestUpdate(&md, data, datalen);
EVP_DigestFinal(&md, digest, &dlen);
ret = DSA_do_verify(digest, dlen, sig, key);
memset(digest, 'd', sizeof(digest));
DSA_SIG_free(sig);
return ret;
}
示例10: ssh_ecdsa_verify
int ssh_ecdsa_verify(EC_KEY *key, ssh_keytype keytype,
u_char *signature, u_int signaturelen,
u_char *data, u_int datalen)
{
ECDSA_SIG *sig;
const EVP_MD *evp_md;
EVP_MD_CTX md;
unsigned char digest[EVP_MAX_MD_SIZE], *sigblob;
unsigned int len, dlen;
int ret, nid = NID_undef;
char *ptr;
OpenSSL_add_all_digests();
if (key == NULL) {
return -2;
}
ptr = signature;
len = get_uint32_MSBfirst(ptr);
ptr += 4;
if (strncmp(get_ssh_keytype_name(keytype), ptr, len) != 0) {
return -3;
}
ptr += len;
len = get_uint32_MSBfirst(ptr);
ptr += 4;
sigblob = ptr;
ptr += len;
/* parse signature */
if ((sig = ECDSA_SIG_new()) == NULL)
return -4;
if ((sig->r = BN_new()) == NULL)
return -5;
if ((sig->s = BN_new()) == NULL)
return -6;
buffer_get_bignum2(&sigblob, sig->r);
buffer_get_bignum2(&sigblob, sig->s);
if (sigblob != ptr) {
return -7;
}
/* hash the data */
nid = keytype_to_hash_nid(keytype);
if ((evp_md = EVP_get_digestbynid(nid)) == NULL) {
return -8;
}
EVP_DigestInit(&md, evp_md);
EVP_DigestUpdate(&md, data, datalen);
EVP_DigestFinal(&md, digest, &dlen);
ret = ECDSA_do_verify(digest, dlen, sig, key);
memset(digest, 'd', sizeof(digest));
ECDSA_SIG_free(sig);
return ret;
}
示例11: ssh_rsa_verify
int ssh_rsa_verify(RSA *key,
u_char *signature, u_int signaturelen,
u_char *data, u_int datalen)
{
const EVP_MD *evp_md;
EVP_MD_CTX md;
// char *ktype;
u_char digest[EVP_MAX_MD_SIZE], *sigblob;
u_int len, dlen, modlen;
// int rlen, ret, nid;
int ret, nid;
char *ptr;
OpenSSL_add_all_digests();
if (key == NULL) {
return -2;
}
if (BN_num_bits(key->n) < SSH_RSA_MINIMUM_MODULUS_SIZE) {
return -3;
}
//debug_print(41, signature, signaturelen);
ptr = signature;
// step1
len = get_uint32_MSBfirst(ptr);
ptr += 4;
if (strncmp("ssh-rsa", ptr, len) != 0) {
return -4;
}
ptr += len;
// step2
len = get_uint32_MSBfirst(ptr);
ptr += 4;
sigblob = ptr;
ptr += len;
#if 0
rlen = get_uint32_MSBfirst(ptr);
if (rlen != 0) {
return -1;
}
#endif
/* RSA_verify expects a signature of RSA_size */
modlen = RSA_size(key);
if (len > modlen) {
return -5;
} else if (len < modlen) {
u_int diff = modlen - len;
sigblob = realloc(sigblob, modlen);
memmove(sigblob + diff, sigblob, len);
memset(sigblob, 0, diff);
len = modlen;
}
/* sha1 the data */
// nid = (datafellows & SSH_BUG_RSASIGMD5) ? NID_md5 : NID_sha1;
nid = NID_sha1;
if ((evp_md = EVP_get_digestbynid(nid)) == NULL) {
//error("ssh_rsa_verify: EVP_get_digestbynid %d failed", nid);
return -6;
}
EVP_DigestInit(&md, evp_md);
EVP_DigestUpdate(&md, data, datalen);
EVP_DigestFinal(&md, digest, &dlen);
ret = openssh_RSA_verify(nid, digest, dlen, sigblob, len, key);
memset(digest, 'd', sizeof(digest));
memset(sigblob, 's', len);
//free(sigblob);
//debug("ssh_rsa_verify: signature %scorrect", (ret==0) ? "in" : "");
return ret;
}
示例12: generate_SSH2_keysign
BOOL generate_SSH2_keysign(Key *keypair, char **sigptr, int *siglen, char *data, int datalen)
{
buffer_t *msg = NULL;
char *s;
int ret;
msg = buffer_init();
if (msg == NULL) {
// TODO: error check
return FALSE;
}
switch (keypair->type) {
case KEY_RSA: // RSA
{
const EVP_MD *evp_md = EVP_sha1();
EVP_MD_CTX md;
u_char digest[EVP_MAX_MD_SIZE], *sig;
u_int slen, dlen, len;
int ok, nid = NID_sha1;
// ダイジェスト値の計算
EVP_DigestInit(&md, evp_md);
EVP_DigestUpdate(&md, data, datalen);
EVP_DigestFinal(&md, digest, &dlen);
slen = RSA_size(keypair->rsa);
sig = malloc(slen);
if (sig == NULL)
goto error;
// 電子署名を計算
ok = RSA_sign(nid, digest, dlen, sig, &len, keypair->rsa);
memset(digest, 'd', sizeof(digest));
if (ok != 1) { // error
free(sig);
goto error;
}
// 署名のサイズがバッファより小さい場合、後ろへずらす。先頭はゼロで埋める。
if (len < slen) {
u_int diff = slen - len;
memmove(sig + diff, sig, len);
memset(sig, 0, diff);
} else if (len > slen) {
free(sig);
goto error;
} else {
// do nothing
}
s = get_sshname_from_key(keypair);
buffer_put_string(msg, s, strlen(s));
buffer_append_length(msg, sig, slen);
len = buffer_len(msg);
// setting
*siglen = len;
*sigptr = malloc(len);
if (*sigptr == NULL) {
free(sig);
goto error;
}
memcpy(*sigptr, buffer_ptr(msg), len);
free(sig);
break;
}
case KEY_DSA: // DSA
{
DSA_SIG *sig;
const EVP_MD *evp_md = EVP_sha1();
EVP_MD_CTX md;
u_char digest[EVP_MAX_MD_SIZE], sigblob[SIGBLOB_LEN];
u_int rlen, slen, len, dlen;
// ダイジェストの計算
EVP_DigestInit(&md, evp_md);
EVP_DigestUpdate(&md, data, datalen);
EVP_DigestFinal(&md, digest, &dlen);
// DSA電子署名を計算
sig = DSA_do_sign(digest, dlen, keypair->dsa);
memset(digest, 'd', sizeof(digest));
if (sig == NULL) {
goto error;
}
// BIGNUMからバイナリ値への変換
rlen = BN_num_bytes(sig->r);
slen = BN_num_bytes(sig->s);
if (rlen > INTBLOB_LEN || slen > INTBLOB_LEN) {
DSA_SIG_free(sig);
goto error;
}
memset(sigblob, 0, SIGBLOB_LEN);
BN_bn2bin(sig->r, sigblob+ SIGBLOB_LEN - INTBLOB_LEN - rlen);
BN_bn2bin(sig->s, sigblob+ SIGBLOB_LEN - slen);
//.........这里部分代码省略.........
示例13: EVP_DigestFinal_ex
static int
EVP_DigestFinal_ex(EVP_MD_CTX *ctx, unsigned char *res, unsigned int *len)
{
EVP_DigestFinal(ctx, res, len);
return 1;
}
示例14: RSA_padding_add_PKCS1_PSS
int RSA_padding_add_PKCS1_PSS(RSA *rsa, unsigned char *EM,
const unsigned char *mHash,
const EVP_MD *Hash, int sLen)
{
int i;
int ret = 0;
int hLen, maskedDBLen, MSBits, emLen;
unsigned char *H, *salt = NULL, *p;
EVP_MD_CTX ctx;
hLen = EVP_MD_size(Hash);
/*
* Negative sLen has special meanings:
* -1 sLen == hLen
* -2 salt length is maximized
* -N reserved
*/
if (sLen == -1) sLen = hLen;
else if (sLen == -2) sLen = -2;
else if (sLen < -2)
{
RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_PSS, RSA_R_SLEN_CHECK_FAILED);
goto err;
}
MSBits = (BN_num_bits(rsa->n) - 1) & 0x7;
emLen = RSA_size(rsa);
if (MSBits == 0)
{
*EM++ = 0;
emLen--;
}
if (sLen == -2)
{
sLen = emLen - hLen - 2;
}
else if (emLen < (hLen + sLen + 2))
{
RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_PSS,
RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
goto err;
}
if (sLen > 0)
{
salt = OPENSSL_malloc(sLen);
if (!salt)
{
RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_PSS,
ERR_R_MALLOC_FAILURE);
goto err;
}
if (!RAND_bytes(salt, sLen))
goto err;
}
maskedDBLen = emLen - hLen - 1;
H = EM + maskedDBLen;
EVP_MD_CTX_init(&ctx);
EVP_DigestInit_ex(&ctx, Hash, NULL);
EVP_DigestUpdate(&ctx, zeroes, sizeof zeroes);
EVP_DigestUpdate(&ctx, mHash, hLen);
if (sLen)
EVP_DigestUpdate(&ctx, salt, sLen);
EVP_DigestFinal(&ctx, H, NULL);
EVP_MD_CTX_cleanup(&ctx);
/* Generate dbMask in place then perform XOR on it */
PKCS1_MGF1(EM, maskedDBLen, H, hLen, Hash);
p = EM;
/* Initial PS XORs with all zeroes which is a NOP so just update
* pointer. Note from a test above this value is guaranteed to
* be non-negative.
*/
p += emLen - sLen - hLen - 2;
*p++ ^= 0x1;
if (sLen > 0)
{
for (i = 0; i < sLen; i++)
*p++ ^= salt[i];
}
if (MSBits)
EM[0] &= 0xFF >> (8 - MSBits);
/* H is already in place so just set final 0xbc */
EM[emLen - 1] = 0xbc;
ret = 1;
err:
if (salt)
OPENSSL_free(salt);
return ret;
}
示例15: sc_hash
_SCAPI_NOT_CONFIGURED
#endif /* */
/* sc_hash(): a generic wrapper around whatever hashing package we are using.
IN:
hashtype - oid pointer to a hash type
hashtypelen - length of oid pointer
buf - u_char buffer to be hashed
buf_len - integer length of buf data
MAC_len - length of the passed MAC buffer size.
OUT:
MAC - pre-malloced space to store hash output.
MAC_len - length of MAC output to the MAC buffer.
Returns:
SNMPERR_SUCCESS Success.
SNMP_SC_GENERAL_FAILURE Any error.
*/
int
sc_hash(oid *hashtype, size_t hashtypelen, u_char *buf, size_t buf_len,
u_char *MAC, size_t *MAC_len)
#if defined(USE_INTERNAL_MD5) || defined(USE_OPENSSL)
{
int rval = SNMPERR_SUCCESS;
#ifdef USE_OPENSSL
EVP_MD *hash(void);
HMAC_CTX *c = NULL;
#endif
DEBUGTRACE;
if (hashtype == NULL || hashtypelen < 0 || buf == NULL ||
buf_len < 0 || MAC == NULL || MAC_len == NULL ||
(int)(*MAC_len) < sc_get_properlength(hashtype, hashtypelen))
return (SNMPERR_GENERR);
#ifdef USE_OPENSSL
/*
* Determine transform type.
*/
c = malloc(sizeof(HMAC_CTX));
if (c == NULL)
return (SNMPERR_GENERR);
if (ISTRANSFORM(hashtype, HMACMD5Auth)) {
EVP_DigestInit(&c->md_ctx, (const EVP_MD *) EVP_md5());
}
else if (ISTRANSFORM(hashtype, HMACSHA1Auth)) {
EVP_DigestInit(&c->md_ctx, (const EVP_MD *) EVP_sha1());
}
else {
return(SNMPERR_GENERR);
}
EVP_DigestUpdate(&c->md_ctx, buf, buf_len);
EVP_DigestFinal(&(c->md_ctx), MAC, MAC_len);
free(c);
return (rval);
#else /* USE_INTERNAL_MD5 */
if (MDchecksum(buf, buf_len, MAC, *MAC_len)) {
return SNMPERR_GENERR;
}
if (*MAC_len > 16)
*MAC_len = 16;
return SNMPERR_SUCCESS;
#endif /* USE_OPENSSL */
}