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