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_ex
作者在 2006-08-16 18:16:00 发布以下内容
关键词: openss RSA