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