本文整理汇总了C++中EVP_DigestSignInit函数的典型用法代码示例。如果您正苦于以下问题:C++ EVP_DigestSignInit函数的具体用法?C++ EVP_DigestSignInit怎么用?C++ EVP_DigestSignInit使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了EVP_DigestSignInit函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: ssl_sign_ecdsa
static int ssl_sign_ecdsa(SSL *ssl, uint8_t *out, size_t *out_len,
size_t max_out, int curve, const EVP_MD *md,
const uint8_t *in, size_t in_len) {
EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(ssl->cert->privatekey);
if (ec_key == NULL) {
OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
return 0;
}
/* In TLS 1.3, the curve is also specified by the signature algorithm. */
if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION &&
(curve == NID_undef ||
EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key)) != curve)) {
OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
return 0;
}
EVP_MD_CTX ctx;
EVP_MD_CTX_init(&ctx);
*out_len = max_out;
int ret = EVP_DigestSignInit(&ctx, NULL, md, NULL, ssl->cert->privatekey) &&
EVP_DigestSignUpdate(&ctx, in, in_len) &&
EVP_DigestSignFinal(&ctx, out, out_len);
EVP_MD_CTX_cleanup(&ctx);
return ret;
}
示例2: PKCS7_SIGNER_INFO_sign_0
static int PKCS7_SIGNER_INFO_sign_0(PKCS7_SIGNER_INFO *si)
{
EVP_MD_CTX mctx;
EVP_PKEY_CTX *pctx;
unsigned char *abuf = NULL;
int alen;
size_t siglen;
const EVP_MD *md = NULL;
md = EVP_get_digestbyobj(si->digest_alg->algorithm);
if (md == NULL)
return 0;
EVP_MD_CTX_init(&mctx);
if (EVP_DigestSignInit(&mctx, &pctx, md, NULL, si->pkey) <= 0)
goto err;
if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_SIGN,
EVP_PKEY_CTRL_PKCS7_SIGN, 0, si) <= 0)
{
PKCS7err(PKCS7_F_PKCS7_SIGNER_INFO_SIGN, PKCS7_R_CTRL_ERROR);
goto err;
}
alen = ASN1_item_i2d((ASN1_VALUE *) si->auth_attr, &abuf,
ASN1_ITEM_rptr(PKCS7_ATTR_SIGN));
if (!abuf)
goto err;
if (EVP_DigestSignUpdate(&mctx, abuf, alen) <= 0)
goto err;
OPENSSL_free(abuf);
abuf = NULL;
if (EVP_DigestSignFinal(&mctx, NULL, &siglen) <= 0)
goto err;
abuf = OPENSSL_malloc(siglen);
if (!abuf)
goto err;
if (EVP_DigestSignFinal(&mctx, abuf, &siglen) <= 0)
goto err;
if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_SIGN,
EVP_PKEY_CTRL_PKCS7_SIGN, 1, si) <= 0)
{
PKCS7err(PKCS7_F_PKCS7_SIGNER_INFO_SIGN, PKCS7_R_CTRL_ERROR);
goto err;
}
EVP_MD_CTX_cleanup(&mctx);
ASN1_STRING_set0(si->enc_digest, abuf, siglen);
return 1;
err:
if (abuf)
OPENSSL_free(abuf);
EVP_MD_CTX_cleanup(&mctx);
return 0;
}
示例3: RSA_PKCS1_sign
Handle<std::string> RSA_PKCS1_sign(Handle<ScopedEVP_PKEY> hKey, const EVP_MD *md, Handle<std::string> hData) {
LOG_FUNC();
ScopedEVP_MD_CTX ctx(EVP_MD_CTX_create());
EVP_PKEY_CTX* pctx = nullptr;
size_t siglen = 0;
if (ctx.isEmpty() ||
!EVP_DigestSignInit(ctx.Get(), &pctx, md, nullptr, hKey->Get())) {
THROW_OPENSSL("EVP_DigestSignInit");
}
byte* data = (byte*)hData->c_str();
size_t datalen = hData->length();
if (1 != EVP_DigestSignUpdate(ctx.Get(), data, datalen)) {
THROW_OPENSSL("EVP_DigestSignUpdate");
}
if (1 != EVP_DigestSignFinal(ctx.Get(), nullptr, &siglen)) {
THROW_OPENSSL("EVP_DigestSignFinal");
}
Handle<std::string> hOutput(new std::string());
hOutput->resize(siglen);
byte *output = (byte*)hOutput->c_str();
if (!EVP_DigestSignFinal(ctx.Get(), output, &siglen))
THROW_OPENSSL("EVP_DigestSignFinal");
return hOutput;
}
示例4: digiSign
//Create base 64 encoded digital signature of given data
bool digiSign(StringBuffer &b64Signature, size32_t dataSz, const void *data, const CLoadedKey &signingKey)
{
OwnedEVPMdCtx signingCtx(EVP_MD_CTX_create());
//initialize context for SHA-256 hashing function
int rc = EVP_DigestSignInit(signingCtx, nullptr, EVP_sha256(), nullptr, signingKey);
if (rc <= 0)
throwEVPException(-1, "digiSign:EVP_DigestSignInit");
//add string to the context
if (EVP_DigestSignUpdate(signingCtx, data, dataSz) <= 0)
throwEVPException(-1, "digiSign:EVP_DigestSignUpdate");
//compute length of signature
size_t encMsgLen;
if (EVP_DigestSignFinal(signingCtx, nullptr, &encMsgLen) <= 0)
throwEVPException(-1, "digiSign:EVP_DigestSignFinal1");
if (encMsgLen == 0)
throwEVPException(-1, "digiSign:EVP_DigestSignFinal length returned 0");
//compute signature (signed digest)
OwnedEVPMemory encMsg = OPENSSL_malloc(encMsgLen);
if (encMsg == nullptr)
throw MakeStringException(-1, "digiSign:OPENSSL_malloc(%u) returned NULL", (unsigned)encMsgLen);
if (EVP_DigestSignFinal(signingCtx, (unsigned char *)encMsg.get(), &encMsgLen) <= 0)
throwEVPException(-1, "digiSign:EVP_DigestSignFinal2");
//convert to base64
JBASE64_Encode(encMsg, encMsgLen, b64Signature, false);
return true;
}
示例5: tls13_final_finish_mac
/*
* Generates the mac for the Finished message. Returns the length of the MAC or
* 0 on error.
*/
size_t tls13_final_finish_mac(SSL *s, const char *str, size_t slen,
unsigned char *out)
{
const EVP_MD *md = ssl_handshake_md(s);
unsigned char hash[EVP_MAX_MD_SIZE];
size_t hashlen, ret = 0;
EVP_PKEY *key = NULL;
EVP_MD_CTX *ctx = EVP_MD_CTX_new();
if (!ssl_handshake_hash(s, hash, sizeof(hash), &hashlen))
goto err;
if (str == s->method->ssl3_enc->server_finished_label)
key = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL,
s->server_finished_secret, hashlen);
else
key = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL,
s->client_finished_secret, hashlen);
if (key == NULL
|| ctx == NULL
|| EVP_DigestSignInit(ctx, NULL, md, NULL, key) <= 0
|| EVP_DigestSignUpdate(ctx, hash, hashlen) <= 0
|| EVP_DigestSignFinal(ctx, out, &hashlen) <= 0)
goto err;
ret = hashlen;
err:
EVP_PKEY_free(key);
EVP_MD_CTX_free(ctx);
return ret;
}
示例6: sign
static int sign(void *ctx,char *file,void *in,int ilen,void *out,int *olen)
{
int r=NOCARD;
size_t slen=*olen;
ENGINE *e=(ENGINE *)ctx;
EVP_PKEY *key;
EVP_MD_CTX *mdc;
resume_engine(e,engbits);
if(!(key=ENGINE_load_private_key(e,file,NULL,NULL)))goto err1;
r=CRYPTOFAIL;
if(!(mdc=EVP_MD_CTX_create()))goto err2;
if(EVP_DigestInit_ex(mdc,EVP_sha256(),NULL)!=1)goto err3;
if(EVP_DigestSignInit(mdc,NULL,EVP_sha256(),NULL,key)!=1)goto err3;
if(EVP_DigestSignUpdate(mdc,in,ilen)!=1)goto err3;
if(EVP_DigestSignFinal(mdc,out,&slen)!=1)goto err3;
*olen=slen;
r=OK;
err3: EVP_MD_CTX_destroy(mdc);
err2: EVP_PKEY_free(key);
err1: suspend_engine(e,&engbits);
return r;
}
示例7: test_EVP_DigestSignAlgorithm
static int test_EVP_DigestSignAlgorithm(void) {
int ret = 0;
EVP_PKEY *pkey = NULL;
EVP_MD_CTX md_ctx;
EVP_PKEY_CTX *pkey_ctx;
EVP_MD_CTX_init(&md_ctx);
pkey = load_example_rsa_key();
if (pkey == NULL) {
goto out;
}
/* Test a simple AlgorithmIdentifier. */
if (!EVP_DigestSignInit(&md_ctx, &pkey_ctx, EVP_sha256(), NULL, pkey) ||
!test_algorithm_roundtrip(&md_ctx, pkey)) {
fprintf(stderr, "RSA with SHA-256 failed\n");
goto out;
}
EVP_MD_CTX_cleanup(&md_ctx);
EVP_MD_CTX_init(&md_ctx);
/* Test RSA-PSS with custom parameters. */
if (!EVP_DigestSignInit(&md_ctx, &pkey_ctx, EVP_sha256(), NULL, pkey) ||
!EVP_PKEY_CTX_set_rsa_padding(pkey_ctx, RSA_PKCS1_PSS_PADDING) ||
!EVP_PKEY_CTX_set_rsa_mgf1_md(pkey_ctx, EVP_sha512()) ||
!test_algorithm_roundtrip(&md_ctx, pkey)) {
fprintf(stderr, "RSA-PSS failed\n");
goto out;
}
ret = 1;
out:
if (!ret) {
BIO_print_errors_fp(stderr);
}
EVP_MD_CTX_cleanup(&md_ctx);
if (pkey) {
EVP_PKEY_free(pkey);
}
return ret;
}
示例8: signMsgRSA
int signMsgRSA(EVP_PKEY* key, const unsigned char* msg,
unsigned char** sig, size_t* slen, size_t msglen){
EVP_MD_CTX* ctx = NULL;
const EVP_MD* md = NULL;
if(msg == NULL || sig == NULL || slen == NULL)
return 0;
ctx = EVP_MD_CTX_create();
md = EVP_get_digestbyname(hn);
if(md == NULL){
printf("ERR EVP_get_digestbyname\n");
return 0;
}
if(ctx == NULL){
printf("ERR EVP_MD_CTX_create\n");
return 0;
}
if(1 != EVP_DigestInit_ex(ctx, md, NULL)){
printf("ERR EVP_DigestInit_ex\n");
return 0;
}
if(1 != EVP_DigestSignInit(ctx, NULL, md, NULL, key)){
printf("ERR EVP_DigestSignInit\n");
return 0;
}
/*SE FIRMA EL MENSAJE*/
if(1 != EVP_DigestSignUpdate(ctx, msg, msglen)){
printf("ERR EVP_DigestSignUpdate\n");
return 0;
}
if(1 != EVP_DigestSignFinal(ctx, NULL, slen)){
printf("ERR EVP_DigestSignFinal\n");
return 0;
}
*sig = OPENSSL_malloc(*slen);
if(*sig == NULL){
printf("ERR OPENSSL_malloc\n");
}
if(1 != EVP_DigestSignFinal(ctx, *sig, slen)){
printf("ERR EVP_DigestSignFinal\n");
return 0;
}
EVP_MD_CTX_destroy(ctx);
ctx = NULL;
return 1;
}
示例9: test_EVP_DigestSignInit
static int test_EVP_DigestSignInit(void) {
int ret = 0;
EVP_PKEY *pkey = NULL;
uint8_t *sig = NULL;
size_t sig_len = 0;
EVP_MD_CTX md_ctx, md_ctx_verify;
EVP_MD_CTX_init(&md_ctx);
EVP_MD_CTX_init(&md_ctx_verify);
pkey = load_example_rsa_key();
if (pkey == NULL ||
!EVP_DigestSignInit(&md_ctx, NULL, EVP_sha256(), NULL, pkey) ||
!EVP_DigestSignUpdate(&md_ctx, kMsg, sizeof(kMsg))) {
goto out;
}
/* Determine the size of the signature. */
if (!EVP_DigestSignFinal(&md_ctx, NULL, &sig_len)) {
goto out;
}
/* Sanity check for testing. */
if (sig_len != EVP_PKEY_size(pkey)) {
fprintf(stderr, "sig_len mismatch\n");
goto out;
}
sig = malloc(sig_len);
if (sig == NULL || !EVP_DigestSignFinal(&md_ctx, sig, &sig_len)) {
goto out;
}
/* Ensure that the signature round-trips. */
if (!EVP_DigestVerifyInit(&md_ctx_verify, NULL, EVP_sha256(), NULL, pkey) ||
!EVP_DigestVerifyUpdate(&md_ctx_verify, kMsg, sizeof(kMsg)) ||
!EVP_DigestVerifyFinal(&md_ctx_verify, sig, sig_len)) {
goto out;
}
ret = 1;
out:
if (!ret) {
BIO_print_errors_fp(stderr);
}
EVP_MD_CTX_cleanup(&md_ctx);
EVP_MD_CTX_cleanup(&md_ctx_verify);
if (pkey) {
EVP_PKEY_free(pkey);
}
if (sig) {
free(sig);
}
return ret;
}
示例10: test_EVP_DigestSignInit
static int test_EVP_DigestSignInit(void)
{
int ret = 0;
EVP_PKEY *pkey = NULL;
unsigned char *sig = NULL;
size_t sig_len = 0;
EVP_MD_CTX *md_ctx, *md_ctx_verify;
md_ctx = EVP_MD_CTX_new();
md_ctx_verify = EVP_MD_CTX_new();
if (md_ctx == NULL || md_ctx_verify == NULL)
goto out;
pkey = load_example_rsa_key();
if (pkey == NULL ||
!EVP_DigestSignInit(md_ctx, NULL, EVP_sha256(), NULL, pkey) ||
!EVP_DigestSignUpdate(md_ctx, kMsg, sizeof(kMsg))) {
goto out;
}
/* Determine the size of the signature. */
if (!EVP_DigestSignFinal(md_ctx, NULL, &sig_len)) {
goto out;
}
/* Sanity check for testing. */
if (sig_len != (size_t)EVP_PKEY_size(pkey)) {
fprintf(stderr, "sig_len mismatch\n");
goto out;
}
sig = OPENSSL_malloc(sig_len);
if (sig == NULL || !EVP_DigestSignFinal(md_ctx, sig, &sig_len)) {
goto out;
}
/* Ensure that the signature round-trips. */
if (!EVP_DigestVerifyInit(md_ctx_verify, NULL, EVP_sha256(), NULL, pkey)
|| !EVP_DigestVerifyUpdate(md_ctx_verify, kMsg, sizeof(kMsg))
|| !EVP_DigestVerifyFinal(md_ctx_verify, sig, sig_len)) {
goto out;
}
ret = 1;
out:
if (!ret) {
ERR_print_errors_fp(stderr);
}
EVP_MD_CTX_free(md_ctx);
EVP_MD_CTX_free(md_ctx_verify);
EVP_PKEY_free(pkey);
OPENSSL_free(sig);
return ret;
}
示例11: ssl_sign_rsa_pkcs1
static int ssl_sign_rsa_pkcs1(SSL *ssl, uint8_t *out, size_t *out_len,
size_t max_out, const EVP_MD *md,
const uint8_t *in, size_t in_len) {
EVP_MD_CTX ctx;
EVP_MD_CTX_init(&ctx);
*out_len = max_out;
int ret = EVP_DigestSignInit(&ctx, NULL, md, NULL, ssl->cert->privatekey) &&
EVP_DigestSignUpdate(&ctx, in, in_len) &&
EVP_DigestSignFinal(&ctx, out, out_len);
EVP_MD_CTX_cleanup(&ctx);
return ret;
}
示例12: ASN1_item_sign
int ASN1_item_sign(const ASN1_ITEM *it, X509_ALGOR *algor1,
X509_ALGOR *algor2, ASN1_BIT_STRING *signature, void *asn,
EVP_PKEY *pkey, const EVP_MD *type)
{
EVP_MD_CTX ctx;
EVP_MD_CTX_init(&ctx);
if (!EVP_DigestSignInit(&ctx, NULL, type, NULL, pkey)) {
EVP_MD_CTX_cleanup(&ctx);
return 0;
}
return ASN1_item_sign_ctx(it, algor1, algor2, signature, asn, &ctx);
}
示例13: spp_init_mac_st
SPP_MAC* spp_init_mac_st(SSL* s, SPP_MAC* mac, unsigned char* key, int which) {
int mac_type;
EVP_PKEY *mac_key;
EVP_MD_CTX md;
const EVP_MD *m;
mac_type = s->s3->tmp.new_mac_pkey_type;
m=s->s3->tmp.new_hash;
if (mac == NULL) {
if ((mac=OPENSSL_malloc(sizeof(SPP_MAC))) == NULL) {
return NULL;
}
}
if (which & SSL3_CC_READ) {
mac->read_hash = EVP_MD_CTX_create();
//ssl_replace_hash(&(mac->read_hash),NULL);
memset(&(mac->read_sequence[0]),0,8);
mac->read_mac_secret_size = s->s3->tmp.new_mac_secret_size;
OPENSSL_assert(mac->read_mac_secret_size <= EVP_MAX_MD_SIZE);
memcpy(&(mac->read_mac_secret[0]), key, mac->read_mac_secret_size);
mac_key = EVP_PKEY_new_mac_key(mac_type, NULL,&(mac->read_mac_secret[0]),mac->read_mac_secret_size);
EVP_DigestSignInit(mac->read_hash,NULL,m,NULL,mac_key);
EVP_PKEY_free(mac_key);
} else {
mac->write_hash = EVP_MD_CTX_create();
//ssl_replace_hash(&(mac->write_hash),NULL);
memset(&(mac->write_sequence[0]),0,8);
mac->write_mac_secret_size = s->s3->tmp.new_mac_secret_size;
OPENSSL_assert(mac->write_mac_secret_size <= EVP_MAX_MD_SIZE);
memcpy(&(mac->write_mac_secret[0]), key, mac->write_mac_secret_size);
mac_key = EVP_PKEY_new_mac_key(mac_type, NULL,&(mac->write_mac_secret[0]),mac->write_mac_secret_size);
EVP_DigestSignInit(mac->write_hash,NULL,m,NULL,mac_key);
EVP_PKEY_free(mac_key);
}
return mac;
}
示例14: tls13_final_finish_mac
/*
* Generates the mac for the Finished message. Returns the length of the MAC or
* 0 on error.
*/
size_t tls13_final_finish_mac(SSL *s, const char *str, size_t slen,
unsigned char *out)
{
const EVP_MD *md = ssl_handshake_md(s);
unsigned char hash[EVP_MAX_MD_SIZE];
size_t hashlen, ret = 0;
EVP_PKEY *key = NULL;
EVP_MD_CTX *ctx = EVP_MD_CTX_new();
if (!ssl_handshake_hash(s, hash, sizeof(hash), &hashlen)) {
/* SSLfatal() already called */
goto err;
}
if (str == s->method->ssl3_enc->server_finished_label) {
key = EVP_PKEY_new_raw_private_key(EVP_PKEY_HMAC, NULL,
s->server_finished_secret, hashlen);
} else if (SSL_IS_FIRST_HANDSHAKE(s)) {
key = EVP_PKEY_new_raw_private_key(EVP_PKEY_HMAC, NULL,
s->client_finished_secret, hashlen);
} else {
unsigned char finsecret[EVP_MAX_MD_SIZE];
if (!tls13_derive_finishedkey(s, ssl_handshake_md(s),
s->client_app_traffic_secret,
finsecret, hashlen))
goto err;
key = EVP_PKEY_new_raw_private_key(EVP_PKEY_HMAC, NULL, finsecret,
hashlen);
OPENSSL_cleanse(finsecret, sizeof(finsecret));
}
if (key == NULL
|| ctx == NULL
|| EVP_DigestSignInit(ctx, NULL, md, NULL, key) <= 0
|| EVP_DigestSignUpdate(ctx, hash, hashlen) <= 0
|| EVP_DigestSignFinal(ctx, out, &hashlen) <= 0) {
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_FINAL_FINISH_MAC,
ERR_R_INTERNAL_ERROR);
goto err;
}
ret = hashlen;
err:
EVP_PKEY_free(key);
EVP_MD_CTX_free(ctx);
return ret;
}
示例15: autoca_signcert
static int autoca_signcert(X509 *cert, EVP_PKEY *pkey)
{
EVP_MD_CTX *ctx = EVP_MD_CTX_create();
EVP_PKEY_CTX *pkctx = NULL;
int rc = -1;
if ( ctx == NULL )
return -1;
if (EVP_DigestSignInit(ctx, &pkctx, NULL, NULL, pkey))
{
rc = X509_sign_ctx(cert, ctx);
}
EVP_MD_CTX_destroy(ctx);
return rc;
}