OpenSSL  1.0.1c
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros
rsaref.c
Go to the documentation of this file.
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 <string.h>
7 #include "./source/global.h"
8 #include "./source/rsaref.h"
9 #include "./source/rsa.h"
10 #include "./source/des.h"
11 #include <openssl/err.h>
12 #define OPENSSL_NO_MD2
13 #define OPENSSL_NO_MD5
14 #include <openssl/evp.h>
15 #include <openssl/bn.h>
16 #include <openssl/engine.h>
17 
18 #define RSAREF_LIB_NAME "rsaref engine"
19 #include "rsaref_err.c"
20 
21 /*****************************************************************************
22  *** Function declarations and global variable definitions ***
23  *****************************************************************************/
24 
25 /*****************************************************************************
26  * Constants used when creating the ENGINE
27  **/
28 static const char *engine_rsaref_id = "rsaref";
29 static const char *engine_rsaref_name = "RSAref engine support";
30 
31 /*****************************************************************************
32  * Functions to handle the engine
33  **/
34 static int rsaref_destroy(ENGINE *e);
35 static int rsaref_init(ENGINE *e);
36 static int rsaref_finish(ENGINE *e);
37 #if 0
38 static int rsaref_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)());
39 #endif
40 
41 /*****************************************************************************
42  * Engine commands
43  **/
44 static const ENGINE_CMD_DEFN rsaref_cmd_defns[] = {
45  {0, NULL, NULL, 0}
46  };
47 
48 /*****************************************************************************
49  * RSA functions
50  **/
51 static int rsaref_private_decrypt(int len, const unsigned char *from,
52  unsigned char *to, RSA *rsa, int padding);
53 static int rsaref_private_encrypt(int len, const unsigned char *from,
54  unsigned char *to, RSA *rsa, int padding);
55 static int rsaref_public_encrypt(int len, const unsigned char *from,
56  unsigned char *to, RSA *rsa, int padding);
57 static int rsaref_public_decrypt(int len, const unsigned char *from,
58  unsigned char *to, RSA *rsa, int padding);
59 static int bnref_mod_exp(BIGNUM *r,const BIGNUM *a,const BIGNUM *p,const BIGNUM *m,
60  BN_CTX *ctx, BN_MONT_CTX *m_ctx);
61 static int rsaref_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa);
62 
63 /*****************************************************************************
64  * Our RSA method
65  **/
66 static RSA_METHOD rsaref_rsa =
67 {
68  "RSAref PKCS#1 RSA",
69  rsaref_public_encrypt,
70  rsaref_public_decrypt,
71  rsaref_private_encrypt,
72  rsaref_private_decrypt,
73  rsaref_mod_exp,
74  bnref_mod_exp,
75  NULL,
76  NULL,
77  0,
78  NULL,
79  NULL,
80  NULL
81 };
82 
83 /*****************************************************************************
84  * Symetric cipher and digest function registrars
85  **/
86 static int rsaref_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
87  const int **nids, int nid);
88 static int rsaref_digests(ENGINE *e, const EVP_MD **digest,
89  const int **nids, int nid);
90 
91 static int rsaref_cipher_nids[] =
93 static int rsaref_digest_nids[] =
94  { 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, const unsigned char *key,
105  const unsigned char *iv, int enc);
106 static int cipher_des_ede3_cbc_code(EVP_CIPHER_CTX *ctx, unsigned char *out,
107  const unsigned char *in, unsigned int inl);
108 static int cipher_des_ede3_cbc_clean(EVP_CIPHER_CTX *);
109 static int cipher_desx_cbc_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
110  const unsigned char *iv, int enc);
111 static int cipher_desx_cbc_code(EVP_CIPHER_CTX *ctx, unsigned char *out,
112  const unsigned char *in, unsigned int inl);
113 static int cipher_desx_cbc_clean(EVP_CIPHER_CTX *);
114 
115 /*****************************************************************************
116  * Our DES ciphers
117  **/
118 static const EVP_CIPHER cipher_des_cbc =
119  {
120  NID_des_cbc,
121  8, 8, 8,
122  0 | EVP_CIPH_CBC_MODE,
123  cipher_des_cbc_init,
124  cipher_des_cbc_code,
125  cipher_des_cbc_clean,
126  sizeof(DES_CBC_CTX),
127  NULL,
128  NULL,
129  NULL,
130  NULL
131  };
132 
133 static const EVP_CIPHER cipher_des_ede3_cbc =
134  {
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  {
150  NID_desx_cbc,
151  8, 24, 8,
152  0 | EVP_CIPH_CBC_MODE,
153  cipher_desx_cbc_init,
154  cipher_desx_cbc_code,
155  cipher_desx_cbc_clean,
156  sizeof(DESX_CBC_CTX),
157  NULL,
158  NULL,
159  NULL,
160  NULL
161  };
162 
163 /*****************************************************************************
164  * MD functions
165  **/
166 static int digest_md2_init(EVP_MD_CTX *ctx);
167 static int digest_md2_update(EVP_MD_CTX *ctx,const void *data,
168  unsigned long count);
169 static int digest_md2_final(EVP_MD_CTX *ctx,unsigned char *md);
170 static int digest_md5_init(EVP_MD_CTX *ctx);
171 static int digest_md5_update(EVP_MD_CTX *ctx,const void *data,
172  unsigned long count);
173 static int digest_md5_final(EVP_MD_CTX *ctx,unsigned char *md);
174 
175 /*****************************************************************************
176  * Our MD digests
177  **/
178 static const EVP_MD digest_md2 =
179  {
180  NID_md2,
182  16,
183  0,
184  digest_md2_init,
185  digest_md2_update,
186  digest_md2_final,
187  NULL,
188  NULL,
190  16,
191  sizeof(MD2_CTX)
192  };
193 
194 static const EVP_MD digest_md5 =
195  {
196  NID_md5,
198  16,
199  0,
200  digest_md5_init,
201  digest_md5_update,
202  digest_md5_final,
203  NULL,
204  NULL,
206  64,
207  sizeof(MD5_CTX)
208  };
209 
210 /*****************************************************************************
211  *** Function definitions ***
212  *****************************************************************************/
213 
214 /*****************************************************************************
215  * Functions to handle the engine
216  **/
217 
218 static int bind_rsaref(ENGINE *e)
219  {
220  const RSA_METHOD *meth1;
221  if(!ENGINE_set_id(e, engine_rsaref_id)
222  || !ENGINE_set_name(e, engine_rsaref_name)
223  || !ENGINE_set_RSA(e, &rsaref_rsa)
224  || !ENGINE_set_ciphers(e, rsaref_ciphers)
225  || !ENGINE_set_digests(e, rsaref_digests)
226  || !ENGINE_set_destroy_function(e, rsaref_destroy)
227  || !ENGINE_set_init_function(e, rsaref_init)
228  || !ENGINE_set_finish_function(e, rsaref_finish)
229  /* || !ENGINE_set_ctrl_function(e, rsaref_ctrl) */
230  /* || !ENGINE_set_cmd_defns(e, rsaref_cmd_defns) */)
231  return 0;
232 
233  /* Ensure the rsaref error handling is set up */
234  ERR_load_RSAREF_strings();
235  return 1;
236  }
237 
238 #ifdef ENGINE_DYNAMIC_SUPPORT
239 static int bind_helper(ENGINE *e, const char *id)
240  {
241  if(id && (strcmp(id, engine_rsaref_id) != 0))
242  return 0;
243  if(!bind_rsaref(e))
244  return 0;
245  return 1;
246  }
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  {
257  ENGINE_free(ret);
258  return NULL;
259  }
260  return ret;
261  }
262 
264  {
265  /* Copied from eng_[openssl|dyn].c */
266  ENGINE *toadd = engine_rsaref();
267  if(!toadd) 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  {
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  {
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  {
319  return(0);
320  }
321 
322  memset(to,0,(unsigned int)max);
323  if (!BN_bn2bin(from,&(to[max-i])))
324  return(0);
325  return(1);
326  }
327 
328 #ifdef undef
329 /* unsigned char *from: [max] */
330 static BIGNUM *RSAref_bin2bn(unsigned char *from, BIGNUM *to, int max)
331  {
332  int i;
333  BIGNUM *ret;
334 
335  for (i=0; i<max; i++)
336  if (from[i]) break;
337 
338  ret=BN_bin2bn(&(from[i]),max-i,to);
339  return(ret);
340  }
341 
342 static int RSAref_Public_ref2eay(RSArefPublicKey *from, RSA *to)
343  {
344  to->n=RSAref_bin2bn(from->m,NULL,RSAref_MAX_LEN);
345  to->e=RSAref_bin2bn(from->e,NULL,RSAref_MAX_LEN);
346  if ((to->n == NULL) || (to->e == NULL)) return(0);
347  return(1);
348  }
349 #endif
350 
351 static int RSAref_Public_eay2ref(RSA *from, R_RSA_PUBLIC_KEY *to)
352  {
353  to->bits=BN_num_bits(from->n);
354  if (!RSAref_bn2bin(from->n,to->modulus,MAX_RSA_MODULUS_LEN)) return(0);
355  if (!RSAref_bn2bin(from->e,to->exponent,MAX_RSA_MODULUS_LEN)) return(0);
356  return(1);
357  }
358 
359 #ifdef undef
360 static int RSAref_Private_ref2eay(RSArefPrivateKey *from, RSA *to)
361  {
362  if ((to->n=RSAref_bin2bn(from->m,NULL,RSAref_MAX_LEN)) == NULL)
363  return(0);
364  if ((to->e=RSAref_bin2bn(from->e,NULL,RSAref_MAX_LEN)) == NULL)
365  return(0);
366  if ((to->d=RSAref_bin2bn(from->d,NULL,RSAref_MAX_LEN)) == NULL)
367  return(0);
368  if ((to->p=RSAref_bin2bn(from->prime[0],NULL,RSAref_MAX_PLEN)) == NULL)
369  return(0);
370  if ((to->q=RSAref_bin2bn(from->prime[1],NULL,RSAref_MAX_PLEN)) == NULL)
371  return(0);
372  if ((to->dmp1=RSAref_bin2bn(from->pexp[0],NULL,RSAref_MAX_PLEN))
373  == NULL)
374  return(0);
375  if ((to->dmq1=RSAref_bin2bn(from->pexp[1],NULL,RSAref_MAX_PLEN))
376  == NULL)
377  return(0);
378  if ((to->iqmp=RSAref_bin2bn(from->coef,NULL,RSAref_MAX_PLEN)) == NULL)
379  return(0);
380  return(1);
381  }
382 #endif
383 
384 static int RSAref_Private_eay2ref(RSA *from, R_RSA_PRIVATE_KEY *to)
385  {
386  to->bits=BN_num_bits(from->n);
387  if (!RSAref_bn2bin(from->n,to->modulus,MAX_RSA_MODULUS_LEN)) return(0);
388  if (!RSAref_bn2bin(from->e,to->publicExponent,MAX_RSA_MODULUS_LEN)) return(0);
389  if (!RSAref_bn2bin(from->d,to->exponent,MAX_RSA_MODULUS_LEN)) return(0);
390  if (!RSAref_bn2bin(from->p,to->prime[0],MAX_RSA_PRIME_LEN)) return(0);
391  if (!RSAref_bn2bin(from->q,to->prime[1],MAX_RSA_PRIME_LEN)) return(0);
392  if (!RSAref_bn2bin(from->dmp1,to->primeExponent[0],MAX_RSA_PRIME_LEN)) return(0);
393  if (!RSAref_bn2bin(from->dmq1,to->primeExponent[1],MAX_RSA_PRIME_LEN)) return(0);
394  if (!RSAref_bn2bin(from->iqmp,to->coefficient,MAX_RSA_PRIME_LEN)) return(0);
395  return(1);
396  }
397 
398 static int rsaref_private_decrypt(int len, const unsigned char *from, unsigned char *to,
399  RSA *rsa, int padding)
400  {
401  int i,outlen= -1;
402  R_RSA_PRIVATE_KEY RSAkey;
403 
404  if (!RSAref_Private_eay2ref(rsa,&RSAkey))
405  goto err;
406  if ((i=RSAPrivateDecrypt(to,(unsigned int *)&outlen,(unsigned char *)from,len,&RSAkey)) != 0)
407  {
409  outlen= -1;
410  }
411 err:
412  memset(&RSAkey,0,sizeof(RSAkey));
413  return(outlen);
414  }
415 
416 static int rsaref_private_encrypt(int len, const unsigned char *from, unsigned char *to,
417  RSA *rsa, int padding)
418  {
419  int i,outlen= -1;
420  R_RSA_PRIVATE_KEY RSAkey;
421 
422  if (padding != RSA_PKCS1_PADDING)
423  {
425  goto err;
426  }
427  if (!RSAref_Private_eay2ref(rsa,&RSAkey))
428  goto err;
429  if ((i=RSAPrivateEncrypt(to,(unsigned int *)&outlen,(unsigned char *)from,len,&RSAkey)) != 0)
430  {
432  outlen= -1;
433  }
434 err:
435  memset(&RSAkey,0,sizeof(RSAkey));
436  return(outlen);
437  }
438 
439 static int rsaref_public_decrypt(int len, const unsigned char *from, unsigned char *to,
440  RSA *rsa, int padding)
441  {
442  int i,outlen= -1;
443  R_RSA_PUBLIC_KEY RSAkey;
444 
445  if (!RSAref_Public_eay2ref(rsa,&RSAkey))
446  goto err;
447  if ((i=RSAPublicDecrypt(to,(unsigned int *)&outlen,(unsigned char *)from,len,&RSAkey)) != 0)
448  {
450  outlen= -1;
451  }
452 err:
453  memset(&RSAkey,0,sizeof(RSAkey));
454  return(outlen);
455  }
456 
457 static int rsaref_public_encrypt(int len, const unsigned char *from, unsigned char *to,
458  RSA *rsa, int padding)
459  {
460  int outlen= -1;
461  int i;
462  R_RSA_PUBLIC_KEY RSAkey;
463  R_RANDOM_STRUCT rnd;
464  unsigned char buf[16];
465 
466  if (padding != RSA_PKCS1_PADDING && padding != RSA_SSLV23_PADDING)
467  {
469  goto err;
470  }
471 
472  R_RandomInit(&rnd);
473  R_GetRandomBytesNeeded((unsigned int *)&i,&rnd);
474  while (i > 0)
475  {
476  if (RAND_bytes(buf,16) <= 0)
477  goto err;
478  R_RandomUpdate(&rnd,buf,(unsigned int)((i>16)?16:i));
479  i-=16;
480  }
481 
482  if (!RSAref_Public_eay2ref(rsa,&RSAkey))
483  goto err;
484  if ((i=RSAPublicEncrypt(to,(unsigned int *)&outlen,(unsigned char *)from,len,&RSAkey,&rnd)) != 0)
485  {
487  outlen= -1;
488  goto err;
489  }
490 err:
491  memset(&RSAkey,0,sizeof(RSAkey));
492  R_RandomFinal(&rnd);
493  memset(&rnd,0,sizeof(rnd));
494  return(outlen);
495  }
496 
497 /*****************************************************************************
498  * Symetric cipher and digest function registrars
499  **/
500 static int rsaref_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
501  const int **nids, int nid)
502  {
503  int ok = 1;
504  if(!cipher)
505  {
506  /* We are returning a list of supported nids */
507  *nids = rsaref_cipher_nids;
508  return (sizeof(rsaref_cipher_nids)-1)/sizeof(rsaref_cipher_nids[0]);
509  }
510  /* We are being asked for a specific cipher */
511  switch (nid)
512  {
513  case NID_des_cbc:
514  *cipher = &cipher_des_cbc; break;
515  case NID_des_ede3_cbc:
516  *cipher = &cipher_des_ede3_cbc; break;
517  case NID_desx_cbc:
518  *cipher = &cipher_desx_cbc; break;
519  default:
520  ok = 0;
521  *cipher = NULL;
522  break;
523  }
524  return ok;
525  }
526 static int rsaref_digests(ENGINE *e, const EVP_MD **digest,
527  const int **nids, int nid)
528  {
529  int ok = 1;
530  if(!digest)
531  {
532  /* We are returning a list of supported nids */
533  *nids = rsaref_digest_nids;
534  return (sizeof(rsaref_digest_nids)-1)/sizeof(rsaref_digest_nids[0]);
535  }
536  /* We are being asked for a specific digest */
537  switch (nid)
538  {
539  case NID_md2:
540  *digest = &digest_md2; break;
541  case NID_md5:
542  *digest = &digest_md5; break;
543  default:
544  ok = 0;
545  *digest = NULL;
546  break;
547  }
548  return ok;
549  }
550 
551 /*****************************************************************************
552  * DES functions
553  **/
554 #undef data
555 #define data(ctx) ((DES_CBC_CTX *)(ctx)->cipher_data)
556 static int cipher_des_cbc_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
557  const unsigned char *iv, int enc)
558  {
559  DES_CBCInit(data(ctx), (unsigned char *)key, (unsigned char *)iv, enc);
560  return 1;
561  }
562 static int cipher_des_cbc_code(EVP_CIPHER_CTX *ctx, unsigned char *out,
563  const unsigned char *in, unsigned int inl)
564  {
565  int ret = DES_CBCUpdate(data(ctx), out, (unsigned char *)in, inl);
566  switch (ret)
567  {
568  case RE_LEN:
570  break;
571  case 0:
572  break;
573  default:
575  }
576  return !ret;
577  }
578 static int cipher_des_cbc_clean(EVP_CIPHER_CTX *ctx)
579  {
580  memset(data(ctx), 0, ctx->cipher->ctx_size);
581  return 1;
582  }
583 
584 #undef data
585 #define data(ctx) ((DES3_CBC_CTX *)(ctx)->cipher_data)
586 static int cipher_des_ede3_cbc_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
587  const unsigned char *iv, int enc)
588  {
589  DES3_CBCInit(data(ctx), (unsigned char *)key, (unsigned char *)iv,
590  enc);
591  return 1;
592  }
593 static int cipher_des_ede3_cbc_code(EVP_CIPHER_CTX *ctx, unsigned char *out,
594  const unsigned char *in, unsigned int inl)
595  {
596  int ret = DES3_CBCUpdate(data(ctx), out, (unsigned char *)in, inl);
597  switch (ret)
598  {
599  case RE_LEN:
601  break;
602  case 0:
603  break;
604  default:
606  }
607  return !ret;
608  }
609 static int cipher_des_ede3_cbc_clean(EVP_CIPHER_CTX *ctx)
610  {
611  memset(data(ctx), 0, ctx->cipher->ctx_size);
612  return 1;
613  }
614 
615 #undef data
616 #define data(ctx) ((DESX_CBC_CTX *)(ctx)->cipher_data)
617 static int cipher_desx_cbc_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
618  const unsigned char *iv, int enc)
619  {
620  DESX_CBCInit(data(ctx), (unsigned char *)key, (unsigned char *)iv,
621  enc);
622  return 1;
623  }
624 static int cipher_desx_cbc_code(EVP_CIPHER_CTX *ctx, unsigned char *out,
625  const unsigned char *in, unsigned int inl)
626  {
627  int ret = DESX_CBCUpdate(data(ctx), out, (unsigned char *)in, inl);
628  switch (ret)
629  {
630  case RE_LEN:
632  break;
633  case 0:
634  break;
635  default:
637  }
638  return !ret;
639  }
640 static int cipher_desx_cbc_clean(EVP_CIPHER_CTX *ctx)
641  {
642  memset(data(ctx), 0, ctx->cipher->ctx_size);
643  return 1;
644  }
645 
646 /*****************************************************************************
647  * MD functions
648  **/
649 #undef data
650 #define data(ctx) ((MD2_CTX *)(ctx)->md_data)
651 static int digest_md2_init(EVP_MD_CTX *ctx)
652  {
653  MD2Init(data(ctx));
654  return 1;
655  }
656 static int digest_md2_update(EVP_MD_CTX *ctx,const void *data,
657  unsigned long count)
658  {
659  MD2Update(data(ctx), (unsigned char *)data, (unsigned int)count);
660  return 1;
661  }
662 static int digest_md2_final(EVP_MD_CTX *ctx,unsigned char *md)
663  {
664  MD2Final(md, data(ctx));
665  return 1;
666  }
667 
668 #undef data
669 #define data(ctx) ((MD5_CTX *)(ctx)->md_data)
670 static int digest_md5_init(EVP_MD_CTX *ctx)
671  {
672  MD5Init(data(ctx));
673  return 1;
674  }
675 static int digest_md5_update(EVP_MD_CTX *ctx,const void *data,
676  unsigned long count)
677  {
678  MD5Update(data(ctx), (unsigned char *)data, (unsigned int)count);
679  return 1;
680  }
681 static int digest_md5_final(EVP_MD_CTX *ctx,unsigned char *md)
682  {
683  MD5Final(md, data(ctx));
684  return 1;
685  }