Add DES functions.
[openssl.git] / demos / engines / rsaref / rsaref.c
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. */
4
5 #include <stdio.h>
6 #include "./source/global.h"
7 #include "./source/rsaref.h"
8 #include "./source/rsa.h"
9 #include "./source/des.h"
10 #include <openssl/err.h>
11 #include <openssl/evp.h>
12 #include <openssl/bn.h>
13 #include <openssl/engine.h>
14
15 #define RSAREF_LIB_NAME "rsaref engine"
16 #include "rsaref_err.c"
17
18 /*****************************************************************************
19  *** Function declarations and global variable definitions                 ***
20  *****************************************************************************/
21
22 /*****************************************************************************
23  * Constants used when creating the ENGINE
24  **/
25 static const char *engine_rsaref_id = "rsaref";
26 static const char *engine_rsaref_name = "RSAref engine support";
27
28 /*****************************************************************************
29  * Functions to handle the engine
30  **/
31 static int rsaref_destroy(ENGINE *e);
32 static int rsaref_init(ENGINE *e);
33 static int rsaref_finish(ENGINE *e);
34 #if 0
35 static int rsaref_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)()); 
36 #endif
37
38 /*****************************************************************************
39  * Engine commands
40  **/
41 static const ENGINE_CMD_DEFN rsaref_cmd_defns[] = {
42         {0, NULL, NULL, 0}
43         };
44
45 /*****************************************************************************
46  * RSA functions
47  **/
48 static int rsaref_private_decrypt(int len, const unsigned char *from,
49         unsigned char *to, RSA *rsa, int padding);
50 static int rsaref_private_encrypt(int len, const unsigned char *from,
51         unsigned char *to, RSA *rsa, int padding);
52 static int rsaref_public_encrypt(int len, const unsigned char *from,
53         unsigned char *to, RSA *rsa, int padding);
54 static int rsaref_public_decrypt(int len, const unsigned char *from,
55         unsigned char *to, RSA *rsa, int padding);
56 static int bnref_mod_exp(BIGNUM *r,const BIGNUM *a,const BIGNUM *p,const BIGNUM *m,
57                           BN_CTX *ctx, BN_MONT_CTX *m_ctx);
58 static int rsaref_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa);
59
60 /*****************************************************************************
61  * Our RSA method
62  **/
63 static RSA_METHOD rsaref_rsa =
64 {
65   "RSAref PKCS#1 RSA",
66   rsaref_public_encrypt,
67   rsaref_public_decrypt,
68   rsaref_private_encrypt,
69   rsaref_private_decrypt,
70   rsaref_mod_exp,
71   bnref_mod_exp,
72   NULL,
73   NULL,
74   0,
75   NULL,
76   NULL,
77   NULL
78 };
79
80 /*****************************************************************************
81  * Symetric cipher and digest function registrars
82  **/
83 static int rsaref_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
84         const int **nids, int nid);
85 static int rsaref_digests(ENGINE *e, const EVP_MD **digest,
86         const int **nids, int nid);
87
88 static int rsaref_cipher_nids[] =
89         { NID_des_cbc, NID_des_ede3_cbc, NID_desx_cbc, 0 };
90 static int rsaref_digest_nids[] =
91         { 0 };
92
93 /*****************************************************************************
94  * DES functions
95  **/
96 int cipher_des_cbc_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
97         const unsigned char *iv, int enc);
98 int cipher_des_cbc_code(EVP_CIPHER_CTX *ctx, unsigned char *out,
99         const unsigned char *in, unsigned int inl);
100 int cipher_des_cbc_clean(EVP_CIPHER_CTX *);
101 int cipher_des_ede3_cbc_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
102         const unsigned char *iv, int enc);
103 int cipher_des_ede3_cbc_code(EVP_CIPHER_CTX *ctx, unsigned char *out,
104         const unsigned char *in, unsigned int inl);
105 int cipher_des_ede3_cbc_clean(EVP_CIPHER_CTX *);
106 int cipher_desx_cbc_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
107         const unsigned char *iv, int enc);
108 int cipher_desx_cbc_code(EVP_CIPHER_CTX *ctx, unsigned char *out,
109         const unsigned char *in, unsigned int inl);
110 int cipher_desx_cbc_clean(EVP_CIPHER_CTX *);
111
112 /*****************************************************************************
113  * Our DES ciphers
114  **/
115 static EVP_CIPHER cipher_des_cbc =
116         {
117         NID_des_cbc,
118         8, 8, 8,
119         0,
120         cipher_des_cbc_init,
121         cipher_des_cbc_code,
122         cipher_des_cbc_clean,
123         sizeof(DES_CBC_CTX),
124         NULL,
125         NULL,
126         NULL,
127         NULL
128         };
129
130 static EVP_CIPHER cipher_des_ede3_cbc =
131         {
132         NID_des_ede3_cbc,
133         8, 24, 8,
134         0,
135         cipher_des_ede3_cbc_init,
136         cipher_des_ede3_cbc_code,
137         cipher_des_ede3_cbc_clean,
138         sizeof(DES3_CBC_CTX),
139         NULL,
140         NULL,
141         NULL,
142         NULL
143         };
144
145 static EVP_CIPHER cipher_desx_cbc =
146         {
147         NID_desx_cbc,
148         8, 24, 8,
149         0,
150         cipher_desx_cbc_init,
151         cipher_desx_cbc_code,
152         cipher_desx_cbc_clean,
153         sizeof(DESX_CBC_CTX),
154         NULL,
155         NULL,
156         NULL,
157         NULL
158         };
159
160 /*****************************************************************************
161  *** Function definitions                                                  ***
162  *****************************************************************************/
163
164 /*****************************************************************************
165  * Functions to handle the engine
166  **/
167
168 static int bind_rsaref(ENGINE *e)
169         {
170         const RSA_METHOD *meth1;
171         if(!ENGINE_set_id(e, engine_rsaref_id)
172                 || !ENGINE_set_name(e, engine_rsaref_name)
173                 || !ENGINE_set_RSA(e, &rsaref_rsa)
174                 || !ENGINE_set_ciphers(e, rsaref_ciphers)
175                 || !ENGINE_set_digests(e, rsaref_digests)
176                 || !ENGINE_set_destroy_function(e, rsaref_destroy)
177                 || !ENGINE_set_init_function(e, rsaref_init)
178                 || !ENGINE_set_finish_function(e, rsaref_finish)
179                 /* || !ENGINE_set_ctrl_function(e, rsaref_ctrl) */
180                 /* || !ENGINE_set_cmd_defns(e, rsaref_cmd_defns) */)
181                 return 0;
182
183         /* Ensure the rsaref error handling is set up */
184         ERR_load_RSAREF_strings();
185         return 1;
186         }
187
188 #ifdef ENGINE_DYNAMIC_SUPPORT
189 static int bind_helper(ENGINE *e, const char *id)
190         {
191         if(id && (strcmp(id, engine_rsaref_id) != 0))
192                 return 0;
193         if(!bind_rsaref(e))
194                 return 0;
195         return 1;
196         }       
197 IMPLEMENT_DYNAMIC_CHECK_FN()
198 IMPLEMENT_DYNAMIC_BIND_FN(bind_helper)
199 #else
200 static ENGINE *engine_rsaref(void)
201         {
202         ENGINE *ret = ENGINE_new();
203         if(!ret)
204                 return NULL;
205         if(!bind_rsaref(ret))
206                 {
207                 ENGINE_free(ret);
208                 return NULL;
209                 }
210         return ret;
211         }
212
213 void ENGINE_load_rsaref(void)
214         {
215         /* Copied from eng_[openssl|dyn].c */
216         ENGINE *toadd = engine_rsaref();
217         if(!toadd) return;
218         ENGINE_add(toadd);
219         ENGINE_free(toadd);
220         ERR_clear_error();
221         }
222 #endif
223
224 /* Initiator which is only present to make sure this engine looks available */
225 static int rsaref_init(ENGINE *e)
226         {
227         return 1;
228         }
229
230 /* Finisher which is only present to make sure this engine looks available */
231 static int rsaref_finish(ENGINE *e)
232         {
233         return 1;
234         }
235
236 /* Destructor (complements the "ENGINE_ncipher()" constructor) */
237 static int rsaref_destroy(ENGINE *e)
238         {
239         ERR_unload_RSAREF_strings();
240         return 1;
241         }
242
243 /*****************************************************************************
244  * RSA functions
245  **/
246
247 static int rsaref_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa)
248         {
249         RSAREFerr(RSAREF_F_RSAREF_MOD_EXP,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
250         return(0);
251         }
252
253 static int bnref_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
254                           const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
255         {
256         RSAREFerr(RSAREF_F_BNREF_MOD_EXP,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
257         return(0);
258         }
259
260 /* unsigned char *to:  [max]    */
261 static int RSAref_bn2bin(BIGNUM *from, unsigned char *to, int max)
262         {
263         int i;
264
265         i=BN_num_bytes(from);
266         if (i > max)
267                 {
268                 RSAREFerr(RSAREF_F_RSAREF_BN2BIN,RSAREF_R_LEN);
269                 return(0);
270                 }
271
272         memset(to,0,(unsigned int)max);
273         if (!BN_bn2bin(from,&(to[max-i])))
274                 return(0);
275         return(1);
276         }
277
278 #ifdef undef
279 /* unsigned char *from:  [max]    */
280 static BIGNUM *RSAref_bin2bn(unsigned char *from, BIGNUM *to, int max)
281         {
282         int i;
283         BIGNUM *ret;
284
285         for (i=0; i<max; i++)
286                 if (from[i]) break;
287
288         ret=BN_bin2bn(&(from[i]),max-i,to);
289         return(ret);
290         }
291
292 static int RSAref_Public_ref2eay(RSArefPublicKey *from, RSA *to)
293         {
294         to->n=RSAref_bin2bn(from->m,NULL,RSAref_MAX_LEN);
295         to->e=RSAref_bin2bn(from->e,NULL,RSAref_MAX_LEN);
296         if ((to->n == NULL) || (to->e == NULL)) return(0);
297         return(1);
298         }
299 #endif
300
301 static int RSAref_Public_eay2ref(RSA *from, R_RSA_PUBLIC_KEY *to)
302         {
303         to->bits=BN_num_bits(from->n);
304         if (!RSAref_bn2bin(from->n,to->modulus,MAX_RSA_MODULUS_LEN)) return(0);
305         if (!RSAref_bn2bin(from->e,to->exponent,MAX_RSA_MODULUS_LEN)) return(0);
306         return(1);
307         }
308
309 #ifdef undef
310 static int RSAref_Private_ref2eay(RSArefPrivateKey *from, RSA *to)
311         {
312         if ((to->n=RSAref_bin2bn(from->m,NULL,RSAref_MAX_LEN)) == NULL)
313                 return(0);
314         if ((to->e=RSAref_bin2bn(from->e,NULL,RSAref_MAX_LEN)) == NULL)
315                 return(0);
316         if ((to->d=RSAref_bin2bn(from->d,NULL,RSAref_MAX_LEN)) == NULL)
317                 return(0);
318         if ((to->p=RSAref_bin2bn(from->prime[0],NULL,RSAref_MAX_PLEN)) == NULL)
319                 return(0);
320         if ((to->q=RSAref_bin2bn(from->prime[1],NULL,RSAref_MAX_PLEN)) == NULL)
321                 return(0);
322         if ((to->dmp1=RSAref_bin2bn(from->pexp[0],NULL,RSAref_MAX_PLEN))
323                 == NULL)
324                 return(0);
325         if ((to->dmq1=RSAref_bin2bn(from->pexp[1],NULL,RSAref_MAX_PLEN))
326                 == NULL)
327                 return(0);
328         if ((to->iqmp=RSAref_bin2bn(from->coef,NULL,RSAref_MAX_PLEN)) == NULL)
329                 return(0);
330         return(1);
331         }
332 #endif
333
334 static int RSAref_Private_eay2ref(RSA *from, R_RSA_PRIVATE_KEY *to)
335         {
336         to->bits=BN_num_bits(from->n);
337         if (!RSAref_bn2bin(from->n,to->modulus,MAX_RSA_MODULUS_LEN)) return(0);
338         if (!RSAref_bn2bin(from->e,to->publicExponent,MAX_RSA_MODULUS_LEN)) return(0);
339         if (!RSAref_bn2bin(from->d,to->exponent,MAX_RSA_MODULUS_LEN)) return(0);
340         if (!RSAref_bn2bin(from->p,to->prime[0],MAX_RSA_PRIME_LEN)) return(0);
341         if (!RSAref_bn2bin(from->q,to->prime[1],MAX_RSA_PRIME_LEN)) return(0);
342         if (!RSAref_bn2bin(from->dmp1,to->primeExponent[0],MAX_RSA_PRIME_LEN)) return(0);
343         if (!RSAref_bn2bin(from->dmq1,to->primeExponent[1],MAX_RSA_PRIME_LEN)) return(0);
344         if (!RSAref_bn2bin(from->iqmp,to->coefficient,MAX_RSA_PRIME_LEN)) return(0);
345         return(1);
346         }
347
348 static int rsaref_private_decrypt(int len, const unsigned char *from, unsigned char *to,
349              RSA *rsa, int padding)
350         {
351         int i,outlen= -1;
352         R_RSA_PRIVATE_KEY RSAkey;
353
354         if (!RSAref_Private_eay2ref(rsa,&RSAkey))
355                 goto err;
356         if ((i=RSAPrivateDecrypt(to,&outlen,(unsigned char *)from,len,&RSAkey)) != 0)
357                 {
358                 RSAREFerr(RSAREF_F_RSAREF_PRIVATE_DECRYPT,i);
359                 outlen= -1;
360                 }
361 err:
362         memset(&RSAkey,0,sizeof(RSAkey));
363         return(outlen);
364         }
365
366 static int rsaref_private_encrypt(int len, const unsigned char *from, unsigned char *to,
367              RSA *rsa, int padding)
368         {
369         int i,outlen= -1;
370         R_RSA_PRIVATE_KEY RSAkey;
371
372         if (padding != RSA_PKCS1_PADDING)
373                 {
374                 RSAREFerr(RSAREF_F_RSAREF_PRIVATE_ENCRYPT, RSA_R_UNKNOWN_PADDING_TYPE);
375                 goto err;
376         }
377         if (!RSAref_Private_eay2ref(rsa,&RSAkey))
378                 goto err;
379         if ((i=RSAPrivateEncrypt(to,&outlen,(unsigned char *)from,len,&RSAkey)) != 0)
380                 {
381                 RSAREFerr(RSAREF_F_RSAREF_PRIVATE_ENCRYPT,i);
382                 outlen= -1;
383                 }
384 err:
385         memset(&RSAkey,0,sizeof(RSAkey));
386         return(outlen);
387         }
388
389 static int rsaref_public_decrypt(int len, const unsigned char *from, unsigned char *to,
390              RSA *rsa, int padding)
391         {
392         int i,outlen= -1;
393         R_RSA_PUBLIC_KEY RSAkey;
394
395         if (!RSAref_Public_eay2ref(rsa,&RSAkey))
396                 goto err;
397         if ((i=RSAPublicDecrypt(to,&outlen,(unsigned char *)from,len,&RSAkey)) != 0)
398                 {
399                 RSAREFerr(RSAREF_F_RSAREF_PUBLIC_DECRYPT,i);
400                 outlen= -1;
401                 }
402 err:
403         memset(&RSAkey,0,sizeof(RSAkey));
404         return(outlen);
405         }
406
407 static int rsaref_public_encrypt(int len, const unsigned char *from, unsigned char *to,
408              RSA *rsa, int padding)
409         {
410         int outlen= -1;
411         int i;
412         R_RSA_PUBLIC_KEY RSAkey;
413         R_RANDOM_STRUCT rnd;
414         unsigned char buf[16];
415
416         if (padding != RSA_PKCS1_PADDING && padding != RSA_SSLV23_PADDING) 
417                 {
418                 RSAREFerr(RSAREF_F_RSAREF_PUBLIC_ENCRYPT, RSA_R_UNKNOWN_PADDING_TYPE);
419                 goto err;
420                 }
421         
422         R_RandomInit(&rnd);
423         R_GetRandomBytesNeeded((unsigned int *)&i,&rnd);
424         while (i > 0)
425                 {
426                 if (RAND_bytes(buf,16) <= 0)
427                         goto err;
428                 R_RandomUpdate(&rnd,buf,(unsigned int)((i>16)?16:i));
429                 i-=16;
430                 }
431
432         if (!RSAref_Public_eay2ref(rsa,&RSAkey))
433                 goto err;
434         if ((i=RSAPublicEncrypt(to,&outlen,(unsigned char *)from,len,&RSAkey,&rnd)) != 0)
435                 {
436                 RSAREFerr(RSAREF_F_RSAREF_PUBLIC_ENCRYPT,i);
437                 outlen= -1;
438                 goto err;
439                 }
440 err:
441         memset(&RSAkey,0,sizeof(RSAkey));
442         R_RandomFinal(&rnd);
443         memset(&rnd,0,sizeof(rnd));
444         return(outlen);
445         }
446
447 /*****************************************************************************
448  * Symetric cipher and digest function registrars
449  **/
450 static int rsaref_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
451         const int **nids, int nid)
452         {
453         int ok = 1;
454         if(!cipher)
455                 {
456                 /* We are returning a list of supported nids */
457                 *nids = rsaref_cipher_nids;
458                 return (sizeof(rsaref_cipher_nids)-1)/sizeof(rsaref_cipher_nids[0]);
459                 }
460         /* We are being asked for a specific cipher */
461         switch (nid)
462                 {
463         case NID_des_cbc:
464                 *cipher = &cipher_des_cbc; break;
465         case NID_des_ede3_cbc:
466                 *cipher = &cipher_des_ede3_cbc; break;
467         case NID_desx_cbc:
468                 *cipher = &cipher_desx_cbc; break;
469         default:
470                 ok = 0;
471                 *cipher = NULL;
472                 break;
473                 }
474         return ok;
475         }
476 static int rsaref_digests(ENGINE *e, const EVP_MD **digest,
477         const int **nids, int nid)
478         {
479         int ok = 1;
480         if(!digest)
481                 {
482                 /* We are returning a list of supported nids */
483                 *nids = rsaref_digest_nids;
484                 return (sizeof(rsaref_digest_nids)-1)/sizeof(rsaref_digest_nids[0]);
485                 }
486         /* We are being asked for a specific digest */
487         switch (nid)
488                 {
489         default:
490                 ok = 0;
491                 *digest = NULL;
492                 break;
493                 }
494         return ok;
495         }
496
497 /*****************************************************************************
498  * DES functions
499  **/
500 #undef data
501 #define data(ctx) ((DES_CBC_CTX *)(ctx)->cipher_data)
502 int cipher_des_cbc_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
503         const unsigned char *iv, int enc)
504         {
505         DES_CBCInit(data(ctx), (unsigned char *)key, (unsigned char *)iv, enc);
506         }
507 int cipher_des_cbc_code(EVP_CIPHER_CTX *ctx, unsigned char *out,
508         const unsigned char *in, unsigned int inl)
509         {
510         DES_CBCUpdate(data(ctx), out, (unsigned char *)in, inl);
511         }
512 int cipher_des_cbc_clean(EVP_CIPHER_CTX *ctx)
513         {
514         memset(data(ctx), 0, ctx->cipher->ctx_size);
515         }
516
517 #undef data
518 #define data(ctx) ((DES3_CBC_CTX *)(ctx)->cipher_data)
519 int cipher_des_ede3_cbc_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
520         const unsigned char *iv, int enc)
521         {
522         DES3_CBCInit(data(ctx), (unsigned char *)key, (unsigned char *)iv,
523                 enc);
524         }
525 int cipher_des_ede3_cbc_code(EVP_CIPHER_CTX *ctx, unsigned char *out,
526         const unsigned char *in, unsigned int inl)
527         {
528         DES3_CBCUpdate(data(ctx), out, (unsigned char *)in, inl);
529         }
530 int cipher_des_ede3_cbc_clean(EVP_CIPHER_CTX *ctx)
531         {
532         memset(data(ctx), 0, ctx->cipher->ctx_size);
533         }
534
535 #undef data
536 #define data(ctx) ((DESX_CBC_CTX *)(ctx)->cipher_data)
537 int cipher_desx_cbc_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
538         const unsigned char *iv, int enc)
539         {
540         DESX_CBCInit(data(ctx), (unsigned char *)key, (unsigned char *)iv,
541                 enc);
542         }
543 int cipher_desx_cbc_code(EVP_CIPHER_CTX *ctx, unsigned char *out,
544         const unsigned char *in, unsigned int inl)
545         {
546         DESX_CBCUpdate(data(ctx), out, (unsigned char *)in, inl);
547         }
548 int cipher_desx_cbc_clean(EVP_CIPHER_CTX *ctx)
549         {
550         memset(data(ctx), 0, ctx->cipher->ctx_size);
551         }