RSA - 基于内存代码实现

基于: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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值