openssl之RSA相关函数介绍

主要介绍了openssl之RSA相关函数,这个对学习和实现RSA算法比较有帮助。

RSA基本结构

struct



  {



    int pad;



    long version;



    const RSA_METHOD *meth;



    ENGINE *engine;



    BIGNUM *n;       n=p*q



    BIGNUM *e;       公开的加密指数,经常为65537(ox10001)



    BIGNUM *d;       私钥



    BIGNUM *p;       大素数p



    BIGNUM *q;       大素数q



    BIGNUM *dmp1;     d mod (p-1)



    BIGNUM *dmq1;     d mod (q-1)



    BIGNUM *iqmp;     (inverse of q) mod p



    int references;



    int flags;



    // ...



    }RSA;



2.初始化函数

RSA * RSA_new(void);初始化一个RSA结构



void RSA_free(RSA *rsa);释放一个RSA结构



3.RSA私钥产生函数

RSA *RSA_generate_key(int num, unsigned long e,void (*callback)(int,int,void *), void *cb_arg);产生一个模为num位的密钥对,e为公开的加密指数,一般为65537(ox10001),假如后两个参数不为NULL,将有些调用。在产生密钥对之前,一般需要指定随机数种子



4.判断位数函数

int RSA_size(const RSA *rsa);返回RSA模的位数,他用来判断需要给加密值分配空间的大小



int RSA_check_key(RSA *rsa);他测试p,q是否为素数,n=p*q,d*e = 1 mod (p-1*q-1), dmp1, dmq1, iqmp是否均设置正确了。



5.RSA的RSA_METHOD函数

了解RSA的运算那就必须了解RSA_METHOD,下面我们先看看RSA_METHOD结构



typedef struct rsa_meth_st



    {



    const char *name;



    int (*rsa_pub_enc)(int flen,const unsigned char *from,



        unsigned char *to,RSA *rsa,int padding);



    int (*rsa_pub_dec)(int flen,const unsigned char *from,



        unsigned char *to,RSA *rsa,int padding);



    int (*rsa_priv_enc)(int flen,const unsigned char *from,



          unsigned char *to, RSA *rsa,int padding);



    int (*rsa_priv_dec)(int flen,const unsigned char *from,



          unsigned char *to,RSA *rsa,int padding);



    int (*rsa_mod_exp)(BIGNUM *r0,const BIGNUM *I,RSA *rsa);     int (*bn_mod_exp)(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,



          const BIGNUM *m, BN_CTX *ctx,BN_MONT_CTX *m_ctx);



    int (*init)(RSA *rsa);       /* called at new */



    int (*finish)(RSA *rsa);     /* called at free */



    int flags;         /* RSA_METHOD_FLAG_* things */



    char *app_data;             /* may be needed! */



    int (*rsa_sign)(int type,const unsigned char *m, unsigned int m_length,unsigned char *sigret, unsigned int *siglen, const RSA *rsa);



    int (*rsa_verify)(int dtype,const unsigned char *m, unsigned int m_length,unsigned char *sigbuf, unsigned int siglen, const RSA *rsa);



    } RSA_METHOD;



const RSA_METHOD *RSA_PKCS1_SSLeay(void);



const RSA_METHOD *RSA_null_method(void);



主要有上面两个函数。第二个函数是定义了RSA_null才会调用,其实要调用这个函数以后几乎什么都不能干,只是输出错误信息。第一个是常用的METHOD,下面我们看看它的定义



const RSA_METHOD *RSA_PKCS1_SSLeay(void)



    {



    return(&rsa_pkcs1_eay_meth);



    }



static RSA_METHOD rsa_pkcs1_eay_meth={



    "Eric Young's PKCS#1 RSA",



    RSA_eay_public_encrypt,



    RSA_eay_public_decrypt, /* signature verification */



    RSA_eay_private_encrypt, /* signing */



    RSA_eay_private_decrypt,



    RSA_eay_mod_exp,



    BN_mod_exp_mont,



    RSA_eay_init,



    RSA_eay_finish,



    0, /* flags */



    NULL,



    0, /* rsa_sign */



    0 /* rsa_verify */



    };



由此可以看出,一般rsa->meth-> rsa_pub_enc对应于RSA_eay_public_encrypt,刚开始看openssl的时候最难得就是这个指向函数的指针,根本不知道rsa->meth-> rsa_pub_enc对应于哪里。在openssl里面这种指针很多,到以后也能够看到。下面是设置meth的一些函数应该都很容易理解



void RSA_set_default_method(const RSA_METHOD *meth);



const RSA_METHOD *RSA_get_default_method(void);



int RSA_set_method(RSA *rsa, const RSA_METHOD *meth);



const RSA_METHOD *RSA_get_method(const RSA *rsa);



int RSA_flags(const RSA *rsa);



RSA *RSA_new_method(ENGINE *engine);



6.加解密函数

int RSA_public_encrypt(int flen, unsigned char *from,



  unsigned char *to, RSA *rsa, int padding);



int RSA_private_decrypt(int flen, unsigned char *from,



  unsigned char *to, RSA *rsa, int padding);



int RSA_private_encrypt(int flen, unsigned char *from,



  unsigned char *to, RSA *rsa,int padding);



int RSA_public_decrypt(int flen, unsigned char *from,



unsigned char *to, RSA *rsa,int padding);



有了第4节的基础,那理解这些加解密函数就容易了,假如



RSA_set_method(rsa, RSA_PKCS1_SSLeay())的话,那RSA_public_encrypt对应于RSA_eay_public_encrypt,这样我们就可以调试公钥加密的过程了。Flen为要加密信息的长度,from为需要加密的信息,to为加密后的信息,一般to至少要申请BN_num_bytes(rsa->n)大的空间。Padding是采取的加解密方案。PKCS#1中主要提供了两种加密方案,RSAEX-OAEP和PSAES-PKCS1-v1_5(反正就是两种加密过程了,有点复杂,它主要是先对先对需要加密的数据进行了编码,比如RSAES-OAEP采用EME-OAEP编码,再进行加密或解密)。Openssl中已经编好了编码的函数:



case RSA_PKCS1_PADDING:



  i=RSA_padding_add_PKCS1_type_2(buf,num,from,flen);



#ifndef OPENSSL_NO_SHA



case RSA_PKCS1_OAEP_PADDING:     i=RSA_padding_add_PKCS1_OAEP(buf,num,from,flen,NULL,0);



#endif



case RSA_SSLV23_PADDING:



  i=RSA_padding_add_SSLv23(buf,num,from,flen);



case RSA_NO_PADDING:



  i=RSA_padding_add_none(buf,num,from,flen);



等上面编好码后,就调用BN_mod_exp_mont来进行模幂了。最后得出值,这也就是具体的加密和解密过程。在这里还可以发现,加密时输入的rsa有两种方式,一是p,q,...为NULL,只有rsa->d,和rsa->n不为空,这样就直接用rsa->d和rsa->n进行模幂计算,假如p,q.....都不为空的话,他会调用中国剩余定理来进行加密。



7.签名函数

int RSA_sign(int type, unsigned char *m, unsigned int m_len,



  unsigned char *sigret, unsigned int *siglen, RSA *rsa);



int RSA_verify(int type, unsigned char *m, unsigned int m_len,



  unsigned char *sigbuf, unsigned int siglen, RSA *rsa);



其实签名其实和用私钥加密差不多是一回事,所以签名函数最终调用的就是私钥加密的函数,在openssl中这个签名函数很少单独拿出来用的,都是为了给EVP_SignFinal来调用的。所以假如是利用RSA进行签名的话,RSA_private_encrypt,BN_mod_exp_mont是最基本的,所有的都需要调用他,区别无非就在于在需要签名的信息上做了一下处理(一般将需要签名的信息求取摘要值得到m)



8.写入文件函数

int RSA_print(BIO *bp, RSA *x, int offset);



int RSA_print_fp(FILE *fp, RSA *x, int offset);offset是为了调整输出格式的,随意一个数都可以(例如2,12,16。。)



9.其他

int RSA_blinding_on(RSA *rsa, BN_CTX *ctx);



void RSA_blinding_off(RSA *rsa);



为了防止时间攻击,openssl还在签名的时候产生一个随机因子,附加在私钥上。



int RSA_sign_ASN1_OCTET_STRING(int dummy, unsigned char *m,unsigned int m_len, unsigned char *sigret, unsigned int *siglen,RSA *rsa);



int RSA_verify_ASN1_OCTET_STRING(int dummy, unsigned char *m,unsigned int m_len, unsigned char *sigbuf, unsigned int siglen,RSA *rsa);


用私钥对八元组串进行签名,原理同RSA_sign


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值