本文整理汇总了C++中RSA_sign函数的典型用法代码示例。如果您正苦于以下问题:C++ RSA_sign函数的具体用法?C++ RSA_sign怎么用?C++ RSA_sign使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了RSA_sign函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: osPrintf
int UtlCryptoKeyRsa::sign(const unsigned char* pSrc,
int srcLen,
unsigned char* pDest,
int* pDestLen) const
{
if (!pSrc || !srcLen || !pDest || !pDestLen ||
*pDestLen < getMaxSignatureSize(srcLen))
{
if (pDestLen)
*pDestLen = 0;
return 0;
}
*pDestLen = 0;
// First, compute the Message Digest (MD) of the source data
int mdLen = EVP_MAX_MD_SIZE;
unsigned char md[EVP_MAX_MD_SIZE];
if (computeDigest(pSrc, srcLen, &md[0], &mdLen) == 0)
return 0;
// Next, sign the Message Digest & return that
if (!RSA_sign(getDigestAlgType(), &md[0], mdLen,
pDest, (unsigned*)pDestLen, mpRsa))
{
osPrintf("*****RSA_sign failed");
return 0;
}
setLastError(0);
return *pDestLen;
}
示例2: main
int main(int argc, char *argv[])
{
unsigned char* signature;
unsigned int slen;
RSA *private_key = NULL;
BIO *priv_bio;
unsigned char buffer[SIG_LEN];
if (argc < 3)
{
printf("Usage: %s image_file signed_file\n", argv[0]);
exit(-1);
}
raw_file = fopen(argv[1], "r");
new_file = fopen(argv[2], "w");
if (!raw_file || !new_file)
{
fprintf(stderr, "Open file error\n");
exit(-1);
}
priv_bio = BIO_new_mem_buf(priv_key, -1);
if(priv_bio == NULL)
{
fprintf(stderr, "priv_bio is null\n");
exit(-1);
}
private_key = PEM_read_bio_RSAPrivateKey(priv_bio, NULL, NULL, NULL);
if(private_key == NULL)
{
fprintf(stderr, "private_key is null\n");
exit(-1);
}
signature = (unsigned char*) malloc(RSA_size(private_key));
if (calc_sha256() != 0)
{
fprintf(stderr, "calc_sha256 error\n");
exit(-1);
}
if(RSA_sign(NID_sha256, (unsigned char*) calc_hash, strlen(calc_hash),
signature, &slen, private_key) != 1)
{
fprintf(stderr, "RSA_sign error\n");
}
fwrite(signature, SIG_LEN, 1, new_file);
int bytesRead = 0;
while((bytesRead = fread(buffer, 1, SIG_LEN, raw_file)))
{
fwrite(buffer, bytesRead, 1, new_file);
}
/* free resouces */
return 0;
}
示例3: rsa_sign
int
rsa_sign(char *path, struct rsa_key *rsa, unsigned char **sigret, unsigned int *siglen)
{
char errbuf[1024];
int max_len = 0, ret;
char sha256[SHA256_DIGEST_LENGTH * 2 +1];
if (access(rsa->path, R_OK) == -1) {
pkg_emit_errno("access", rsa->path);
return (EPKG_FATAL);
}
if (rsa->key == NULL && _load_rsa_private_key(rsa) != EPKG_OK) {
pkg_emit_error("can't load key from %s", rsa->path);
return (EPKG_FATAL);
}
max_len = RSA_size(rsa->key);
*sigret = calloc(1, max_len + 1);
sha256_file(path, sha256);
ret = RSA_sign(NID_sha1, sha256, sizeof(sha256), *sigret, siglen, rsa->key);
if (ret == 0) {
/* XXX pass back RSA errors correctly */
pkg_emit_error("%s: %s", rsa->path,
ERR_error_string(ERR_get_error(), errbuf));
return (EPKG_FATAL);
}
return (EPKG_OK);
}
示例4: RSA_do_sign
/*
* Maybe the missing function from libcrypto
*
* I think now, maybe it's a bad idea to name it has it should have be
* named in libcrypto
*/
ssh_string RSA_do_sign(const unsigned char *payload, int len, RSA *privkey) {
ssh_string sign = NULL;
unsigned char *buffer = NULL;
unsigned int size;
buffer = malloc(RSA_size(privkey));
if (buffer == NULL) {
return NULL;
}
if (RSA_sign(NID_sha1, payload, len, buffer, &size, privkey) == 0) {
SAFE_FREE(buffer);
return NULL;
}
sign = ssh_string_new(size);
if (sign == NULL) {
SAFE_FREE(buffer);
return NULL;
}
ssh_string_fill(sign, buffer, size);
SAFE_FREE(buffer);
return sign;
}
示例5: CreateRSASignature
bool CreateRSASignature(std::string privateKey, void* data, size_t dataSize, std::string& signature)
{
// privateKey has to be reformatted with -----RSA PRIVATE KEY----- header/footer and newlines after every 64 chars
// before calling this function
BIO* privKeyBuff = BIO_new_mem_buf((void*)privateKey.c_str(), privateKey.length());
if (!privKeyBuff)
return false;
RSA* rsa = PEM_read_bio_RSAPrivateKey(privKeyBuff, 0, 0, 0);
if (!rsa)
return false;
unsigned char hash[SHA256_DIGEST_LENGTH];
SHA256_CTX sha;
SHA256_Init(&sha);
SHA256_Update(&sha, data, dataSize);
SHA256_Final(hash, &sha);
void* sigret = malloc(RSA_size(rsa));
unsigned int siglen = 0;
int retVal = RSA_sign(NID_sha256, (unsigned char*)hash, SHA256_DIGEST_LENGTH, (unsigned char*)sigret, &siglen, rsa);
RSA_free(rsa);
BIO_free_all(privKeyBuff);
if (retVal != 1)
return false;
signature = Utils::String::Base64Encode((unsigned char*)sigret, siglen);
return true;
}
示例6: pkey_rsa_sign
static int
pkey_rsa_sign(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
const unsigned char *tbs, size_t tbslen)
{
int ret;
RSA_PKEY_CTX *rctx = ctx->data;
RSA *rsa = ctx->pkey->pkey.rsa;
if (rctx->md) {
if (tbslen != (size_t)EVP_MD_size(rctx->md)) {
RSAerr(RSA_F_PKEY_RSA_SIGN,
RSA_R_INVALID_DIGEST_LENGTH);
return -1;
}
if (EVP_MD_type(rctx->md) == NID_mdc2) {
unsigned int sltmp;
if (rctx->pad_mode != RSA_PKCS1_PADDING)
return -1;
ret = RSA_sign_ASN1_OCTET_STRING(NID_mdc2, tbs, tbslen,
sig, &sltmp, rsa);
if (ret <= 0)
return ret;
ret = sltmp;
} else if (rctx->pad_mode == RSA_X931_PADDING) {
if (!setup_tbuf(rctx, ctx))
return -1;
memcpy(rctx->tbuf, tbs, tbslen);
rctx->tbuf[tbslen] =
RSA_X931_hash_id(EVP_MD_type(rctx->md));
ret = RSA_private_encrypt(tbslen + 1, rctx->tbuf, sig,
rsa, RSA_X931_PADDING);
} else if (rctx->pad_mode == RSA_PKCS1_PADDING) {
unsigned int sltmp;
ret = RSA_sign(EVP_MD_type(rctx->md), tbs, tbslen, sig,
&sltmp, rsa);
if (ret <= 0)
return ret;
ret = sltmp;
} else if (rctx->pad_mode == RSA_PKCS1_PSS_PADDING) {
if (!setup_tbuf(rctx, ctx))
return -1;
if (!RSA_padding_add_PKCS1_PSS_mgf1(rsa, rctx->tbuf,
tbs, rctx->md, rctx->mgf1md, rctx->saltlen))
return -1;
ret = RSA_private_encrypt(RSA_size(rsa), rctx->tbuf,
sig, rsa, RSA_NO_PADDING);
} else
return -1;
} else
ret = RSA_private_encrypt(tbslen, tbs, sig, ctx->pkey->pkey.rsa,
rctx->pad_mode);
if (ret < 0)
return ret;
*siglen = ret;
return 1;
}
示例7: ldns_sign_public_rsasha1
ldns_rdf *
ldns_sign_public_rsasha1(ldns_buffer *to_sign, RSA *key)
{
unsigned char *sha1_hash;
unsigned int siglen;
ldns_rdf *sigdata_rdf;
ldns_buffer *b64sig;
int result;
siglen = 0;
b64sig = ldns_buffer_new(LDNS_MAX_PACKETLEN);
if (!b64sig) {
return NULL;
}
sha1_hash = SHA1((unsigned char*)ldns_buffer_begin(to_sign),
ldns_buffer_position(to_sign), NULL);
if (!sha1_hash) {
ldns_buffer_free(b64sig);
return NULL;
}
result = RSA_sign(NID_sha1, sha1_hash, SHA_DIGEST_LENGTH,
(unsigned char*)ldns_buffer_begin(b64sig),
&siglen, key);
if (result != 1) {
ldns_buffer_free(b64sig);
return NULL;
}
sigdata_rdf = ldns_rdf_new_frm_data(LDNS_RDF_TYPE_B64, siglen,
ldns_buffer_begin(b64sig));
ldns_buffer_free(b64sig); /* can't free this buffer ?? */
return sigdata_rdf;
}
示例8: sign
/**
* @brief Sign a message with provided RSA key.
*
* Hash msg (SHA1) and encrypt the resulting digest. The buffer supplied to hold
* the signature must be at least RSA_size(keypair) bytes long.
*
* @param msg Data to sign.
* @param msg_len Size of data to sign.
* @param sigbuf Buffer to hold created signature.
* @param sigbuflen Space available for signature.
*
* @return Size of signature on success, 0 on error.
*/
uint32_t sign(RSA *keypair, char *msg, size_t msg_len, char *sigbuf, int sigbuflen) {
if (sigbuflen < RSA_size(keypair)) {
ERROR("ERROR: Could not sign message because sigbuf is too small");
return 0;
}
/* first hash msg */
unsigned char *digest = hash(msg, msg_len);
if (digest == NULL) {
ERROR("ERROR: Unable to hash message");
return 0;
}
/* now sign the hash */
uint32_t siglen;
if (RSA_sign(NID_sha1, digest, SHA_DIGEST_LENGTH, (unsigned char*)sigbuf, &siglen, keypair) != 1) {
char *err = (char *)malloc(130); //FIXME?
ERR_load_crypto_strings();
ERR_error_string(ERR_get_error(), err);
ERRORF("Error signing message: %s", err);
free(err);
return 0;
}
free(digest);
return siglen;
}
示例9: sign
int
sign(char* private_key_path, unsigned char *msg, int msg_len,
unsigned char *sig, unsigned int *sig_len)
{
//Load private key
FILE *fp = fopen(private_key_path, "r");
if(!fp) {
DEBUGMSG(ERROR, "Could not find private key\n");
return 0;
}
RSA *rsa = (RSA *) PEM_read_RSAPrivateKey(fp,NULL,NULL,NULL);
fclose(fp);
if(!rsa) return 0;
unsigned char md[SHA256_DIGEST_LENGTH];
sha(msg, msg_len, md);
//Compute signatur
int err = RSA_sign(NID_sha256, md, SHA256_DIGEST_LENGTH, (unsigned char*)sig, (unsigned int*)sig_len, rsa);
if(!err){
printf("Error: %ul\n", (unsigned int)ERR_get_error());
}
RSA_free(rsa);
return err;
}
示例10: RSA_size
bool SecurityHelper::signDataObject(DataObjectRef& dObj, RSA *key)
{
unsigned char *signature;
if (!key || !dObj)
return false;
unsigned int siglen = RSA_size(key);
signature = (unsigned char *)malloc(siglen);
if (!signature)
return false;
printf("signing data object, siglen=%u\n", siglen);
memset(signature, 0, siglen);
if (RSA_sign(NID_sha1, dObj->getId(), sizeof(DataObjectId_t), signature, &siglen, key) != 1) {
free(signature);
return false;
}
dObj->setSignature(getManager()->getKernel()->getThisNode()->getIdStr(), signature, siglen);
// Assume that our own signature is valid
dObj->setSignatureStatus(DataObject::SIGNATURE_VALID);
// Do not free the allocated signature as it is now owned by the data object...
return true;
}
示例11: ldns_sign_public_rsamd5
ldns_rdf *
ldns_sign_public_rsamd5(ldns_buffer *to_sign, RSA *key)
{
unsigned char *md5_hash;
unsigned int siglen;
ldns_rdf *sigdata_rdf;
ldns_buffer *b64sig;
b64sig = ldns_buffer_new(LDNS_MAX_PACKETLEN);
if (!b64sig) {
return NULL;
}
md5_hash = MD5((unsigned char*)ldns_buffer_begin(to_sign),
ldns_buffer_position(to_sign), NULL);
if (!md5_hash) {
ldns_buffer_free(b64sig);
return NULL;
}
RSA_sign(NID_md5, md5_hash, MD5_DIGEST_LENGTH,
(unsigned char*)ldns_buffer_begin(b64sig),
&siglen, key);
sigdata_rdf = ldns_rdf_new_frm_data(LDNS_RDF_TYPE_B64, siglen,
ldns_buffer_begin(b64sig));
ldns_buffer_free(b64sig);
return sigdata_rdf;
}
示例12: __ast_sign_bin
static int __ast_sign_bin(struct ast_key *key, const char *msg, int msglen, unsigned char *dsig)
{
unsigned char digest[20];
unsigned int siglen = 128;
int res;
if (key->ktype != AST_KEY_PRIVATE) {
ast_log(LOG_WARNING, "Cannot sign with a public key\n");
return -1;
}
/* Calculate digest of message */
SHA1((unsigned char *)msg, msglen, digest);
/* Verify signature */
res = RSA_sign(NID_sha1, digest, sizeof(digest), dsig, &siglen, key->rsa);
if (!res) {
ast_log(LOG_WARNING, "RSA Signature (key %s) failed\n", key->name);
return -1;
}
if (siglen != 128) {
ast_log(LOG_WARNING, "Unexpected signature length %d, expecting %d\n", (int)siglen, (int)128);
return -1;
}
return 0;
}
示例13: private_key_operation
// private_key_operation: perform a private key operation
kssl_error_code private_key_operation(pk_list list, // Private key array from new_pk_list
int key_id, // ID of key in pk_list from find_private_key
int opcode, // Opcode from a KSSL message indicating the operation
int length, // Length of data in message
BYTE *message, // Bytes to perform operation on
BYTE *out, // Buffer into which operation output is written
unsigned int *size) { // Size of returned data written here
int rc = KSSL_ERROR_NONE;
// Currently, we only support decrypt or sign here
if (opcode == KSSL_OP_RSA_DECRYPT) {
int s = RSA_private_decrypt(length, message, out, list->privates[key_id].key,
RSA_PKCS1_PADDING);
if (s != -1) {
*size = (unsigned int)s;
} else {
rc = KSSL_ERROR_CRYPTO_FAILED;
ERR_clear_error();
}
} else {
if (RSA_sign(opcode_to_digest_nid(opcode), message, length, out, size,
list->privates[key_id].key) != 1) {
rc = KSSL_ERROR_CRYPTO_FAILED;
}
}
return rc;
}
示例14: s2n_rsa_sign
int s2n_rsa_sign(struct s2n_rsa_private_key *key, struct s2n_hash_state *digest, struct s2n_blob *signature)
{
uint8_t digest_out[MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH];
int type, digest_length;
if (digest->alg == S2N_HASH_MD5_SHA1) {
type = NID_md5_sha1;
digest_length = MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH;
} else if (digest->alg == S2N_HASH_SHA1) {
type = NID_sha1;
digest_length = SHA_DIGEST_LENGTH;
} else {
S2N_ERROR(S2N_ERR_HASH_INVALID_ALGORITHM);
}
GUARD(s2n_hash_digest(digest, digest_out, digest_length));
unsigned int signature_size = signature->size;
if (RSA_sign(type, digest_out, digest_length, signature->data, &signature_size, key->rsa) == 0) {
S2N_ERROR(S2N_ERR_SIGN);
}
if (signature_size > signature->size) {
S2N_ERROR(S2N_ERR_SIZE_MISMATCH);
}
signature->size = signature_size;
return 0;
}
示例15: _libssh2_rsa_sha1_sign
int
_libssh2_rsa_sha1_sign(LIBSSH2_SESSION * session,
libssh2_rsa_ctx * rsactx,
const unsigned char *hash,
unsigned long hash_len,
unsigned char **signature, unsigned long *signature_len)
{
int ret;
unsigned char *sig;
unsigned int sig_len;
sig_len = RSA_size(rsactx);
sig = LIBSSH2_ALLOC(session, sig_len);
if (!sig) {
return -1;
}
ret = RSA_sign(NID_sha1, hash, hash_len, sig, &sig_len, rsactx);
if (!ret) {
LIBSSH2_FREE(session, sig);
return -1;
}
*signature = sig;
*signature_len = sig_len;
return 0;
}