1 /* Demo of how to construct your own engine and using it. The basis of this
2 engine is RSAref, an old reference of the RSA algorithm which can still
3 be found a little here and there. */
6 #include "./source/global.h"
7 #include "./source/rsaref.h"
8 #include "./source/rsa.h"
9 #include <openssl/err.h>
10 #include <openssl/bn.h>
11 #include <openssl/engine.h>
13 #define RSAREF_LIB_NAME "rsaref engine"
14 #include "rsaref_err.c"
16 /* Constants used when creating the ENGINE */
17 static const char *engine_rsaref_id = "rsaref";
18 static const char *engine_rsaref_name = "RSAref engine support";
20 static int rsaref_destroy(ENGINE *e);
21 static int rsaref_init(ENGINE *e);
22 static int rsaref_finish(ENGINE *e);
24 static int rsaref_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)());
27 static int rsaref_private_decrypt(int len, const unsigned char *from,
28 unsigned char *to, RSA *rsa, int padding);
29 static int rsaref_private_encrypt(int len, const unsigned char *from,
30 unsigned char *to, RSA *rsa, int padding);
31 static int rsaref_public_encrypt(int len, const unsigned char *from,
32 unsigned char *to, RSA *rsa, int padding);
33 static int rsaref_public_decrypt(int len, const unsigned char *from,
34 unsigned char *to, RSA *rsa, int padding);
35 static int bnref_mod_exp(BIGNUM *r,const BIGNUM *a,const BIGNUM *p,const BIGNUM *m,
36 BN_CTX *ctx, BN_MONT_CTX *m_ctx);
37 static int rsaref_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa);
39 static const ENGINE_CMD_DEFN rsaref_cmd_defns[] = {
43 static RSA_METHOD rsaref_rsa =
46 rsaref_public_encrypt,
47 rsaref_public_decrypt,
48 rsaref_private_encrypt,
49 rsaref_private_decrypt,
60 /* Now, to our own code */
62 static int bind_rsaref(ENGINE *e)
64 const RSA_METHOD *meth1;
65 if(!ENGINE_set_id(e, engine_rsaref_id)
66 || !ENGINE_set_name(e, engine_rsaref_name)
67 || !ENGINE_set_RSA(e, &rsaref_rsa)
68 || !ENGINE_set_destroy_function(e, rsaref_destroy)
69 || !ENGINE_set_init_function(e, rsaref_init)
70 || !ENGINE_set_finish_function(e, rsaref_finish)
71 /* || !ENGINE_set_ctrl_function(e, rsaref_ctrl) */
72 /* || !ENGINE_set_cmd_defns(e, rsaref_cmd_defns) */)
75 /* Ensure the rsaref error handling is set up */
76 ERR_load_RSAREF_strings();
80 #ifdef ENGINE_DYNAMIC_SUPPORT
81 static int bind_helper(ENGINE *e, const char *id)
83 if(id && (strcmp(id, engine_rsaref_id) != 0))
89 IMPLEMENT_DYNAMIC_CHECK_FN()
90 IMPLEMENT_DYNAMIC_BIND_FN(bind_helper)
92 static ENGINE *engine_rsaref(void)
94 ENGINE *ret = ENGINE_new();
105 void ENGINE_load_rsaref(void)
107 /* Copied from eng_[openssl|dyn].c */
108 ENGINE *toadd = engine_rsaref();
116 /* Initiator which is only present to make sure this engine looks available */
117 static int rsaref_init(ENGINE *e)
122 /* Finisher which is only present to make sure this engine looks available */
123 static int rsaref_finish(ENGINE *e)
128 /* Destructor (complements the "ENGINE_ncipher()" constructor) */
129 static int rsaref_destroy(ENGINE *e)
131 ERR_unload_RSAREF_strings();
135 static int rsaref_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa)
137 RSAREFerr(RSAREF_F_RSAREF_MOD_EXP,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
141 static int bnref_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
142 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
144 RSAREFerr(RSAREF_F_BNREF_MOD_EXP,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
148 /* unsigned char *to: [max] */
149 static int RSAref_bn2bin(BIGNUM *from, unsigned char *to, int max)
153 i=BN_num_bytes(from);
156 RSAREFerr(RSAREF_F_RSAREF_BN2BIN,RSAREF_R_LEN);
160 memset(to,0,(unsigned int)max);
161 if (!BN_bn2bin(from,&(to[max-i])))
167 /* unsigned char *from: [max] */
168 static BIGNUM *RSAref_bin2bn(unsigned char *from, BIGNUM *to, int max)
173 for (i=0; i<max; i++)
176 ret=BN_bin2bn(&(from[i]),max-i,to);
180 static int RSAref_Public_ref2eay(RSArefPublicKey *from, RSA *to)
182 to->n=RSAref_bin2bn(from->m,NULL,RSAref_MAX_LEN);
183 to->e=RSAref_bin2bn(from->e,NULL,RSAref_MAX_LEN);
184 if ((to->n == NULL) || (to->e == NULL)) return(0);
189 static int RSAref_Public_eay2ref(RSA *from, R_RSA_PUBLIC_KEY *to)
191 to->bits=BN_num_bits(from->n);
192 if (!RSAref_bn2bin(from->n,to->modulus,MAX_RSA_MODULUS_LEN)) return(0);
193 if (!RSAref_bn2bin(from->e,to->exponent,MAX_RSA_MODULUS_LEN)) return(0);
198 static int RSAref_Private_ref2eay(RSArefPrivateKey *from, RSA *to)
200 if ((to->n=RSAref_bin2bn(from->m,NULL,RSAref_MAX_LEN)) == NULL)
202 if ((to->e=RSAref_bin2bn(from->e,NULL,RSAref_MAX_LEN)) == NULL)
204 if ((to->d=RSAref_bin2bn(from->d,NULL,RSAref_MAX_LEN)) == NULL)
206 if ((to->p=RSAref_bin2bn(from->prime[0],NULL,RSAref_MAX_PLEN)) == NULL)
208 if ((to->q=RSAref_bin2bn(from->prime[1],NULL,RSAref_MAX_PLEN)) == NULL)
210 if ((to->dmp1=RSAref_bin2bn(from->pexp[0],NULL,RSAref_MAX_PLEN))
213 if ((to->dmq1=RSAref_bin2bn(from->pexp[1],NULL,RSAref_MAX_PLEN))
216 if ((to->iqmp=RSAref_bin2bn(from->coef,NULL,RSAref_MAX_PLEN)) == NULL)
222 static int RSAref_Private_eay2ref(RSA *from, R_RSA_PRIVATE_KEY *to)
224 to->bits=BN_num_bits(from->n);
225 if (!RSAref_bn2bin(from->n,to->modulus,MAX_RSA_MODULUS_LEN)) return(0);
226 if (!RSAref_bn2bin(from->e,to->publicExponent,MAX_RSA_MODULUS_LEN)) return(0);
227 if (!RSAref_bn2bin(from->d,to->exponent,MAX_RSA_MODULUS_LEN)) return(0);
228 if (!RSAref_bn2bin(from->p,to->prime[0],MAX_RSA_PRIME_LEN)) return(0);
229 if (!RSAref_bn2bin(from->q,to->prime[1],MAX_RSA_PRIME_LEN)) return(0);
230 if (!RSAref_bn2bin(from->dmp1,to->primeExponent[0],MAX_RSA_PRIME_LEN)) return(0);
231 if (!RSAref_bn2bin(from->dmq1,to->primeExponent[1],MAX_RSA_PRIME_LEN)) return(0);
232 if (!RSAref_bn2bin(from->iqmp,to->coefficient,MAX_RSA_PRIME_LEN)) return(0);
236 static int rsaref_private_decrypt(int len, const unsigned char *from, unsigned char *to,
237 RSA *rsa, int padding)
240 R_RSA_PRIVATE_KEY RSAkey;
242 if (!RSAref_Private_eay2ref(rsa,&RSAkey))
244 if ((i=RSAPrivateDecrypt(to,&outlen,(unsigned char *)from,len,&RSAkey)) != 0)
246 RSAREFerr(RSAREF_F_RSAREF_PRIVATE_DECRYPT,i);
250 memset(&RSAkey,0,sizeof(RSAkey));
254 static int rsaref_private_encrypt(int len, const unsigned char *from, unsigned char *to,
255 RSA *rsa, int padding)
258 R_RSA_PRIVATE_KEY RSAkey;
260 if (padding != RSA_PKCS1_PADDING)
262 RSAREFerr(RSAREF_F_RSAREF_PRIVATE_ENCRYPT, RSA_R_UNKNOWN_PADDING_TYPE);
265 if (!RSAref_Private_eay2ref(rsa,&RSAkey))
267 if ((i=RSAPrivateEncrypt(to,&outlen,(unsigned char *)from,len,&RSAkey)) != 0)
269 RSAREFerr(RSAREF_F_RSAREF_PRIVATE_ENCRYPT,i);
273 memset(&RSAkey,0,sizeof(RSAkey));
277 static int rsaref_public_decrypt(int len, const unsigned char *from, unsigned char *to,
278 RSA *rsa, int padding)
281 R_RSA_PUBLIC_KEY RSAkey;
283 if (!RSAref_Public_eay2ref(rsa,&RSAkey))
285 if ((i=RSAPublicDecrypt(to,&outlen,(unsigned char *)from,len,&RSAkey)) != 0)
287 RSAREFerr(RSAREF_F_RSAREF_PUBLIC_DECRYPT,i);
291 memset(&RSAkey,0,sizeof(RSAkey));
295 static int rsaref_public_encrypt(int len, const unsigned char *from, unsigned char *to,
296 RSA *rsa, int padding)
300 R_RSA_PUBLIC_KEY RSAkey;
302 unsigned char buf[16];
304 if (padding != RSA_PKCS1_PADDING && padding != RSA_SSLV23_PADDING)
306 RSAREFerr(RSAREF_F_RSAREF_PUBLIC_ENCRYPT, RSA_R_UNKNOWN_PADDING_TYPE);
311 R_GetRandomBytesNeeded((unsigned int *)&i,&rnd);
314 if (RAND_bytes(buf,16) <= 0)
316 R_RandomUpdate(&rnd,buf,(unsigned int)((i>16)?16:i));
320 if (!RSAref_Public_eay2ref(rsa,&RSAkey))
322 if ((i=RSAPublicEncrypt(to,&outlen,(unsigned char *)from,len,&RSAkey,&rnd)) != 0)
324 RSAREFerr(RSAREF_F_RSAREF_PUBLIC_ENCRYPT,i);
329 memset(&RSAkey,0,sizeof(RSAkey));
331 memset(&rnd,0,sizeof(rnd));