您的位置:首页 > 编程语言

RSA - 基于内存代码实现

2017-07-26 21:40 148 查看
基于:openssl-1.0.1g

#define OPENSSL_AES_BITS_128 (128)
#define OPENSSL_AES_BITS_256 (256)
#define OPENSSL_AES_LEN_16 (16) // 16 = 128 / 8
#define OPENSSL_AES_LEN_32 (32) // 32 = 256 / 8
typedef enum
{
OPENSSL_RSA_PUBKEY = 1,
OPENSSL_RSA_PUBLICKEY,
OPENSSL_BULT
}OPENSSL_RSA_KEY_TYPE_E;
int openssl_rsa_generate_key(uint8_t *publicKey, uint8_t *privateKey, uint32_t keyLen, uint8_t key_type)
{
RSA *rsa = NULL;
BIGNUM *bne = NULL;

rsa = RSA_new();
bne = BN_new();
BN_set_word(bne, RSA_F4);

if (1 != RSA_generate_key_ex(rsa, keyLen, bne, NULL))
{
printf("RSA_generate_key err!\n");
return -1;
}

//start generate private key
BIO *bp = BIO_new(BIO_s_mem());
//BIO *bp = BIO_new_file("private.key", "w+");
if (NULL == bp)
{
printf("%s-%d:BIO_new_mem_buf failed!\n", __FUNCTION__, __LINE__);
return -1;
}

if (PEM_write_bio_RSAPrivateKey(bp, rsa, NULL, NULL, 0, NULL, NULL) != 1)
{
printf("PEM_write_bio_RSAPrivateKey err!\n");
return -1;
}

//printf("create private key ok!\n");
BIO_read(bp, privateKey, keyLen);
BIO_free_all(bp);
bp = NULL;

//start generate public key
bp = BIO_new(BIO_s_mem());
//bp = BIO_new_file("public.key", "w+");
if (NULL == bp)
{
printf("%s-%d:BIO_new_mem_buf failed!\n", __FUNCTION__, __LINE__);
return -1;
}

if (OPENSSL_RSA_PUBKEY == key_type)
{
if (PEM_write_bio_RSA_PUBKEY(bp, rsa) != 1)
{
printf("PEM_write_bio_RSAPublicKey err!\n");
return -1;
}
}
else if (OPENSSL_RSA_PUBLICKEY == key_type)
{
if (PEM_write_bio_RSAPublicKey(bp, rsa) != 1)
{
printf("PEM_write_bio_RSAPublicKey err!\n");
return -1;
}
}

//printf("create public key ok!\n");
BIO_read(bp, publicKey, keyLen);
BIO_free_all(bp);
bp = NULL;

RSA_free(rsa);
rsa = NULL;

return 0;
}

int openssl_rsa_public_key_encrypt(uint8_t *key, uint8_t key_type, uint8_t encrypt_mode, const uint8_t *in, uint32_t in_len, uint8_t *out, uint32_t *out_len)
{
BIO *bp = NULL;
RSA *rsa = NULL;

if (NULL == key)
{
printf("%s-%d:input key information error!\n", __FUNCTION__, __LINE__);
return -1;
}

if ((NULL == in) || (0 == in_len) || (NULL == out) || (NULL == out_len))
{
printf("%s-%d:input paramentes error!\n", __FUNCTION__, __LINE__);
return -1;
}

bp = BIO_new_mem_buf(key, -1);
if (NULL == bp)
{
printf("%s-%d:BIO_new_mem_buf failed!\n", __FUNCTION__, __LINE__);
return -1;
}

if (OPENSSL_RSA_PUBKEY == key_type)
{
if ((rsa = PEM_read_bio_RSA_PUBKEY(bp, &rsa, NULL, NULL)) == NULL)
{
printf("%s %d:PEM_read_bio_RSA_PUBKEY!\n", __FUNCTION__, __LINE__);
return -1;
}
}
else if (OPENSSL_RSA_PUBLICKEY == key_type)
{
if ((rsa = PEM_read_bio_RSAPublicKey(bp, &rsa, NULL, NULL)) == NULL)
{
printf("%s %d:PEM_read_bio_RSAPublicKey failure!\n", __FUNCTION__, __LINE__);
return -1;
}
}

if (RSA_PKCS1_PADDING == encrypt_mode)
{
if (in_len > (uint32_t)(RSA_size(rsa) - RSA_PKCS1_PADDING_SIZE))
{
RSAerr(RSA_F_RSA_SIGN, RSA_R_DIGEST_TOO_BIG_FOR_RSA_KEY);
return -1;
}
}

if ((*out_len = RSA_public_encrypt(in_len, in, out, rsa, encrypt_mode)) < 0)
{
printf("%s %d:RSA_public_encrypt err!\n", __FUNCTION__, __LINE__);
return -1;
}

RSA_free(rsa);
rsa = NULL;

BIO_free_all(bp);
bp = NULL;

return 0;
}

int openssl_rsa_private_key_encrypt(uint8_t *key, uint8_t key_type, uint8_t encrypt_mode, const uint8_t *in, uint32_t in_len, uint8_t *out, uint32_t *out_len)
{
BIO *bp = NULL;
RSA *rsa = NULL;

if (NULL == key)
{
printf("%s-%d:input key information error!\n", __FUNCTION__, __LINE__);
return -1;
}

if ((NULL == in) || (0 == in_len) || (NULL == out) || (NULL == out_len))
{
printf("%s-%d:input paramentes error!\n", __FUNCTION__, __LINE__);
return -1;
}

bp = BIO_new_mem_buf(key, -1);
if (NULL == bp)
{
printf("%s-%d:BIO_new_mem_buf failed!\n", __FUNCTION__, __LINE__);
return -1;
}

if ((rsa = PEM_read_bio_RSAPrivateKey(bp, &rsa, NULL, NULL)) == NULL)
{
printf("%s %d:PEM_read_bio_RSAPrivateKey failure!\n", __FUNCTION__, __LINE__);
return -1;
}

if (RSA_PKCS1_PADDING == encrypt_mode)
{
if (in_len > (uint32_t)(RSA_size(rsa) - RSA_PKCS1_PADDING_SIZE))
{
RSAerr(RSA_F_RSA_SIGN, RSA_R_DIGEST_TOO_BIG_FOR_RSA_KEY);
return -1;
}
}

if ((*out_len = RSA_private_encrypt(in_len, in, out, rsa, encrypt_mode)) < 0)
{
printf("%s %d:RSA_private_encrypt err!\n", __FUNCTION__, __LINE__);
return -1;
}

RSA_free(rsa);
rsa = NULL;

BIO_free_all(bp);
bp = NULL;

return 0;
}

int openssl_rsa_private_key_decrypt(uint8_t *key, uint8_t key_type, uint8_t encrypt_mode, const uint8_t *in, uint32_t in_len, uint8_t *out, uint32_t *out_len)
{
BIO *bp = NULL;
RSA *rsa = NULL;

if (NULL == key)
{
printf("%s-%d:input key information error!\n", __FUNCTION__, __LINE__);
return -1;
}

if ((NULL == in) || (0 == in_len) || (NULL == out) || (NULL == out_len))
{
printf("%s-%d:input paramentes error!\n", __FUNCTION__, __LINE__);
return -1;
}

bp = BIO_new_mem_buf(key, -1);
if (NULL == bp)
{
printf("%s-%d:BIO_new_mem_buf failed!\n", __FUNCTION__, __LINE__);
return -1;
}

if ((rsa = PEM_read_bio_RSAPrivateKey(bp, &rsa, NULL, NULL)) == NULL)
{
printf("%s %d:PEM_read_bio_RSAPrivateKey failure!\n", __FUNCTION__, __LINE__);
return -1;
}

if ((*out_len = RSA_private_decrypt(in_len, in, out, rsa, encrypt_mode)) < 0)
{
printf("%s %d:RSA_private_decrypt err!\n", __FUNCTION__, __LINE__);
return -1;
}

RSA_free(rsa);
rsa = NULL;

BIO_free_all(bp);
bp = NULL;

return 0;
}

int openssl_rsa_public_key_decrypt(uint8_t *key, uint8_t key_type, uint8_t encrypt_mode, const uint8_t *in, uint32_t in_len, uint8_t *out, uint32_t *out_len)
{
BIO *bp = NULL;
RSA *rsa = NULL;

if (NULL == key)
{
printf("%s-%d:input key information error!\n", __FUNCTION__, __LINE__);
return -1;
}

if ((NULL == in) || (0 == in_len) || (NULL == out) || (NULL == out_len))
{
printf("%s-%d:input paramentes error!\n", __FUNCTION__, __LINE__);
return -1;
}

bp = BIO_new_mem_buf(key, -1);
if (NULL == bp)
{
printf("%s-%d:BIO_new_mem_buf failed!\n", __FUNCTION__, __LINE__);
return -1;
}

if (OPENSSL_RSA_PUBKEY == key_type)
{
if ((rsa = PEM_read_bio_RSA_PUBKEY(bp, &rsa, NULL, NULL)) == NULL)
{
printf("%s %d:PEM_read_bio_RSA_PUBKEY failure!\n", __FUNCTION__, __LINE__);
return -1;
}
}
else if (OPENSSL_RSA_PUBLICKEY == key_type)
{
if ((rsa = PEM_read_bio_RSAPublicKey(bp, &rsa, NULL, NULL)) == NULL)
{
printf("%s %d:PEM_read_bio_RSAPublicKey failure!\n", __FUNCTION__, __LINE__);
return -1;
}
}

if ((*out_len = RSA_public_decrypt(in_len, in, out, rsa, encrypt_mode)) < 0)
{
printf("%s %d:RSA_public_decrypt err!\n", __FUNCTION__, __LINE__);
return -1;
}

RSA_free(rsa);
rsa = NULL;

BIO_free_all(bp);
bp = NULL;

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: