Add MD digests.
[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         { NID_md2, NID_md5, 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 const 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 const 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 const 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  * MD functions
162  **/
163 static int digest_md2_init(EVP_MD_CTX *ctx);
164 static int digest_md2_update(EVP_MD_CTX *ctx,const void *data,
165         unsigned long count);
166 static int digest_md2_final(EVP_MD_CTX *ctx,unsigned char *md);
167 static int digest_md5_init(EVP_MD_CTX *ctx);
168 static int digest_md5_update(EVP_MD_CTX *ctx,const void *data,
169         unsigned long count);
170 static int digest_md5_final(EVP_MD_CTX *ctx,unsigned char *md);
171
172 /*****************************************************************************
173  * Our MD digests
174  **/
175 static const EVP_MD digest_md2 =
176         {
177         NID_md2,
178         NID_md2WithRSAEncryption,
179         16,
180         0,
181         digest_md2_init,
182         digest_md2_update,
183         digest_md2_final,
184         NULL,
185         NULL,
186         EVP_PKEY_RSA_method,
187         16,
188         sizeof(MD2_CTX)
189         };
190
191 static const EVP_MD digest_md5 =
192         {
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                 /* || !ENGINE_set_cmd_defns(e, rsaref_cmd_defns) */)
228                 return 0;
229
230         /* Ensure the rsaref error handling is set up */
231         ERR_load_RSAREF_strings();
232         return 1;
233         }
234
235 #ifdef ENGINE_DYNAMIC_SUPPORT
236 static int bind_helper(ENGINE *e, const char *id)
237         {
238         if(id && (strcmp(id, engine_rsaref_id) != 0))
239                 return 0;
240         if(!bind_rsaref(e))
241                 return 0;
242         return 1;
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                 {
254                 ENGINE_free(ret);
255                 return NULL;
256                 }
257         return ret;
258         }
259
260 void ENGINE_load_rsaref(void)
261         {
262         /* Copied from eng_[openssl|dyn].c */
263         ENGINE *toadd = engine_rsaref();
264         if(!toadd) 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                 {
315                 RSAREFerr(RSAREF_F_RSAREF_BN2BIN,RSAREF_R_LEN);
316                 return(0);
317                 }
318
319         memset(to,0,(unsigned int)max);
320         if (!BN_bn2bin(from,&(to[max-i])))
321                 return(0);
322         return(1);
323         }
324
325 #ifdef undef
326 /* unsigned char *from:  [max]    */
327 static BIGNUM *RSAref_bin2bn(unsigned char *from, BIGNUM *to, int max)
328         {
329         int i;
330         BIGNUM *ret;
331
332         for (i=0; i<max; i++)
333                 if (from[i]) break;
334
335         ret=BN_bin2bn(&(from[i]),max-i,to);
336         return(ret);
337         }
338
339 static int RSAref_Public_ref2eay(RSArefPublicKey *from, RSA *to)
340         {
341         to->n=RSAref_bin2bn(from->m,NULL,RSAref_MAX_LEN);
342         to->e=RSAref_bin2bn(from->e,NULL,RSAref_MAX_LEN);
343         if ((to->n == NULL) || (to->e == NULL)) return(0);
344         return(1);
345         }
346 #endif
347
348 static int RSAref_Public_eay2ref(RSA *from, R_RSA_PUBLIC_KEY *to)
349         {
350         to->bits=BN_num_bits(from->n);
351         if (!RSAref_bn2bin(from->n,to->modulus,MAX_RSA_MODULUS_LEN)) return(0);
352         if (!RSAref_bn2bin(from->e,to->exponent,MAX_RSA_MODULUS_LEN)) return(0);
353         return(1);
354         }
355
356 #ifdef undef
357 static int RSAref_Private_ref2eay(RSArefPrivateKey *from, RSA *to)
358         {
359         if ((to->n=RSAref_bin2bn(from->m,NULL,RSAref_MAX_LEN)) == NULL)
360                 return(0);
361         if ((to->e=RSAref_bin2bn(from->e,NULL,RSAref_MAX_LEN)) == NULL)
362                 return(0);
363         if ((to->d=RSAref_bin2bn(from->d,NULL,RSAref_MAX_LEN)) == NULL)
364                 return(0);
365         if ((to->p=RSAref_bin2bn(from->prime[0],NULL,RSAref_MAX_PLEN)) == NULL)
366                 return(0);
367         if ((to->q=RSAref_bin2bn(from->prime[1],NULL,RSAref_MAX_PLEN)) == NULL)
368                 return(0);
369         if ((to->dmp1=RSAref_bin2bn(from->pexp[0],NULL,RSAref_MAX_PLEN))
370                 == NULL)
371                 return(0);
372         if ((to->dmq1=RSAref_bin2bn(from->pexp[1],NULL,RSAref_MAX_PLEN))
373                 == NULL)
374                 return(0);
375         if ((to->iqmp=RSAref_bin2bn(from->coef,NULL,RSAref_MAX_PLEN)) == NULL)
376                 return(0);
377         return(1);
378         }
379 #endif
380
381 static int RSAref_Private_eay2ref(RSA *from, R_RSA_PRIVATE_KEY *to)
382         {
383         to->bits=BN_num_bits(from->n);
384         if (!RSAref_bn2bin(from->n,to->modulus,MAX_RSA_MODULUS_LEN)) return(0);
385         if (!RSAref_bn2bin(from->e,to->publicExponent,MAX_RSA_MODULUS_LEN)) return(0);
386         if (!RSAref_bn2bin(from->d,to->exponent,MAX_RSA_MODULUS_LEN)) return(0);
387         if (!RSAref_bn2bin(from->p,to->prime[0],MAX_RSA_PRIME_LEN)) return(0);
388         if (!RSAref_bn2bin(from->q,to->prime[1],MAX_RSA_PRIME_LEN)) return(0);
389         if (!RSAref_bn2bin(from->dmp1,to->primeExponent[0],MAX_RSA_PRIME_LEN)) return(0);
390         if (!RSAref_bn2bin(from->dmq1,to->primeExponent[1],MAX_RSA_PRIME_LEN)) return(0);
391         if (!RSAref_bn2bin(from->iqmp,to->coefficient,MAX_RSA_PRIME_LEN)) return(0);
392         return(1);
393         }
394
395 static int rsaref_private_decrypt(int len, const unsigned char *from, unsigned char *to,
396              RSA *rsa, int padding)
397         {
398         int i,outlen= -1;
399         R_RSA_PRIVATE_KEY RSAkey;
400
401         if (!RSAref_Private_eay2ref(rsa,&RSAkey))
402                 goto err;
403         if ((i=RSAPrivateDecrypt(to,&outlen,(unsigned char *)from,len,&RSAkey)) != 0)
404                 {
405                 RSAREFerr(RSAREF_F_RSAREF_PRIVATE_DECRYPT,i);
406                 outlen= -1;
407                 }
408 err:
409         memset(&RSAkey,0,sizeof(RSAkey));
410         return(outlen);
411         }
412
413 static int rsaref_private_encrypt(int len, const unsigned char *from, unsigned char *to,
414              RSA *rsa, int padding)
415         {
416         int i,outlen= -1;
417         R_RSA_PRIVATE_KEY RSAkey;
418
419         if (padding != RSA_PKCS1_PADDING)
420                 {
421                 RSAREFerr(RSAREF_F_RSAREF_PRIVATE_ENCRYPT, RSA_R_UNKNOWN_PADDING_TYPE);
422                 goto err;
423         }
424         if (!RSAref_Private_eay2ref(rsa,&RSAkey))
425                 goto err;
426         if ((i=RSAPrivateEncrypt(to,&outlen,(unsigned char *)from,len,&RSAkey)) != 0)
427                 {
428                 RSAREFerr(RSAREF_F_RSAREF_PRIVATE_ENCRYPT,i);
429                 outlen= -1;
430                 }
431 err:
432         memset(&RSAkey,0,sizeof(RSAkey));
433         return(outlen);
434         }
435
436 static int rsaref_public_decrypt(int len, const unsigned char *from, unsigned char *to,
437              RSA *rsa, int padding)
438         {
439         int i,outlen= -1;
440         R_RSA_PUBLIC_KEY RSAkey;
441
442         if (!RSAref_Public_eay2ref(rsa,&RSAkey))
443                 goto err;
444         if ((i=RSAPublicDecrypt(to,&outlen,(unsigned char *)from,len,&RSAkey)) != 0)
445                 {
446                 RSAREFerr(RSAREF_F_RSAREF_PUBLIC_DECRYPT,i);
447                 outlen= -1;
448                 }
449 err:
450         memset(&RSAkey,0,sizeof(RSAkey));
451         return(outlen);
452         }
453
454 static int rsaref_public_encrypt(int len, const unsigned char *from, unsigned char *to,
455              RSA *rsa, int padding)
456         {
457         int outlen= -1;
458         int i;
459         R_RSA_PUBLIC_KEY RSAkey;
460         R_RANDOM_STRUCT rnd;
461         unsigned char buf[16];
462
463         if (padding != RSA_PKCS1_PADDING && padding != RSA_SSLV23_PADDING) 
464                 {
465                 RSAREFerr(RSAREF_F_RSAREF_PUBLIC_ENCRYPT, RSA_R_UNKNOWN_PADDING_TYPE);
466                 goto err;
467                 }
468         
469         R_RandomInit(&rnd);
470         R_GetRandomBytesNeeded((unsigned int *)&i,&rnd);
471         while (i > 0)
472                 {
473                 if (RAND_bytes(buf,16) <= 0)
474                         goto err;
475                 R_RandomUpdate(&rnd,buf,(unsigned int)((i>16)?16:i));
476                 i-=16;
477                 }
478
479         if (!RSAref_Public_eay2ref(rsa,&RSAkey))
480                 goto err;
481         if ((i=RSAPublicEncrypt(to,&outlen,(unsigned char *)from,len,&RSAkey,&rnd)) != 0)
482                 {
483                 RSAREFerr(RSAREF_F_RSAREF_PUBLIC_ENCRYPT,i);
484                 outlen= -1;
485                 goto err;
486                 }
487 err:
488         memset(&RSAkey,0,sizeof(RSAkey));
489         R_RandomFinal(&rnd);
490         memset(&rnd,0,sizeof(rnd));
491         return(outlen);
492         }
493
494 /*****************************************************************************
495  * Symetric cipher and digest function registrars
496  **/
497 static int rsaref_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
498         const int **nids, int nid)
499         {
500         int ok = 1;
501         if(!cipher)
502                 {
503                 /* We are returning a list of supported nids */
504                 *nids = rsaref_cipher_nids;
505                 return (sizeof(rsaref_cipher_nids)-1)/sizeof(rsaref_cipher_nids[0]);
506                 }
507         /* We are being asked for a specific cipher */
508         switch (nid)
509                 {
510         case NID_des_cbc:
511                 *cipher = &cipher_des_cbc; break;
512         case NID_des_ede3_cbc:
513                 *cipher = &cipher_des_ede3_cbc; break;
514         case NID_desx_cbc:
515                 *cipher = &cipher_desx_cbc; break;
516         default:
517                 ok = 0;
518                 *cipher = NULL;
519                 break;
520                 }
521         return ok;
522         }
523 static int rsaref_digests(ENGINE *e, const EVP_MD **digest,
524         const int **nids, int nid)
525         {
526         int ok = 1;
527         if(!digest)
528                 {
529                 /* We are returning a list of supported nids */
530                 *nids = rsaref_digest_nids;
531                 return (sizeof(rsaref_digest_nids)-1)/sizeof(rsaref_digest_nids[0]);
532                 }
533         /* We are being asked for a specific digest */
534         switch (nid)
535                 {
536         case NID_md2:
537                 *digest = &digest_md2; break;
538         case NID_md5:
539                 *digest = &digest_md5; break;
540         default:
541                 ok = 0;
542                 *digest = NULL;
543                 break;
544                 }
545         return ok;
546         }
547
548 /*****************************************************************************
549  * DES functions
550  **/
551 #undef data
552 #define data(ctx) ((DES_CBC_CTX *)(ctx)->cipher_data)
553 int cipher_des_cbc_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
554         const unsigned char *iv, int enc)
555         {
556         DES_CBCInit(data(ctx), (unsigned char *)key, (unsigned char *)iv, enc);
557         return 1;
558         }
559 int cipher_des_cbc_code(EVP_CIPHER_CTX *ctx, unsigned char *out,
560         const unsigned char *in, unsigned int inl)
561         {
562         int ret = DES_CBCUpdate(data(ctx), out, (unsigned char *)in, inl);
563         switch (ret)
564                 {
565         case RE_LEN:
566                 RSAREFerr(RSAREF_F_CIPHER_DES_CBC_CODE,RSAREF_R_LENGTH_NOT_BLOCK_ALIGNED);
567                 break;
568         case 0:
569                 break;
570         default:
571                 RSAREFerr(RSAREF_F_CIPHER_DES_CBC_CODE,RSAREF_R_UNKNOWN_FAULT);
572                 }
573         return !ret;
574         }
575 int cipher_des_cbc_clean(EVP_CIPHER_CTX *ctx)
576         {
577         memset(data(ctx), 0, ctx->cipher->ctx_size);
578         return 1;
579         }
580
581 #undef data
582 #define data(ctx) ((DES3_CBC_CTX *)(ctx)->cipher_data)
583 int cipher_des_ede3_cbc_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
584         const unsigned char *iv, int enc)
585         {
586         DES3_CBCInit(data(ctx), (unsigned char *)key, (unsigned char *)iv,
587                 enc);
588         return 1;
589         }
590 int cipher_des_ede3_cbc_code(EVP_CIPHER_CTX *ctx, unsigned char *out,
591         const unsigned char *in, unsigned int inl)
592         {
593         int ret = DES3_CBCUpdate(data(ctx), out, (unsigned char *)in, inl);
594         switch (ret)
595                 {
596         case RE_LEN:
597                 RSAREFerr(RSAREF_F_CIPHER_DES_CBC_CODE,RSAREF_R_LENGTH_NOT_BLOCK_ALIGNED);
598                 break;
599         case 0:
600                 break;
601         default:
602                 RSAREFerr(RSAREF_F_CIPHER_DES_CBC_CODE,RSAREF_R_UNKNOWN_FAULT);
603                 }
604         return !ret;
605         }
606 int cipher_des_ede3_cbc_clean(EVP_CIPHER_CTX *ctx)
607         {
608         memset(data(ctx), 0, ctx->cipher->ctx_size);
609         return 1;
610         }
611
612 #undef data
613 #define data(ctx) ((DESX_CBC_CTX *)(ctx)->cipher_data)
614 int cipher_desx_cbc_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
615         const unsigned char *iv, int enc)
616         {
617         DESX_CBCInit(data(ctx), (unsigned char *)key, (unsigned char *)iv,
618                 enc);
619         return 1;
620         }
621 int cipher_desx_cbc_code(EVP_CIPHER_CTX *ctx, unsigned char *out,
622         const unsigned char *in, unsigned int inl)
623         {
624         int ret = DESX_CBCUpdate(data(ctx), out, (unsigned char *)in, inl);
625         switch (ret)
626                 {
627         case RE_LEN:
628                 RSAREFerr(RSAREF_F_CIPHER_DES_CBC_CODE,RSAREF_R_LENGTH_NOT_BLOCK_ALIGNED);
629                 break;
630         case 0:
631                 break;
632         default:
633                 RSAREFerr(RSAREF_F_CIPHER_DES_CBC_CODE,RSAREF_R_UNKNOWN_FAULT);
634                 }
635         return !ret;
636         }
637 int cipher_desx_cbc_clean(EVP_CIPHER_CTX *ctx)
638         {
639         memset(data(ctx), 0, ctx->cipher->ctx_size);
640         return 1;
641         }
642
643 /*****************************************************************************
644  * MD functions
645  **/
646 #undef data
647 #define data(ctx) ((MD2_CTX *)(ctx)->md_data)
648 static int digest_md2_init(EVP_MD_CTX *ctx)
649         {
650         MD2Init(data(ctx));
651         return 1;
652         }
653 static int digest_md2_update(EVP_MD_CTX *ctx,const void *data,
654         unsigned long count)
655         {
656         MD2Update(data(ctx), (unsigned char *)data, (unsigned int)count);
657         return 1;
658         }
659 static int digest_md2_final(EVP_MD_CTX *ctx,unsigned char *md)
660         {
661         MD2Final(md, data(ctx));
662         return 1;
663         }
664
665 #undef data
666 #define data(ctx) ((MD5_CTX *)(ctx)->md_data)
667 static int digest_md5_init(EVP_MD_CTX *ctx)
668         {
669         MD5Init(data(ctx));
670         return 1;
671         }
672 static int digest_md5_update(EVP_MD_CTX *ctx,const void *data,
673         unsigned long count)
674         {
675         MD5Update(data(ctx), (unsigned char *)data, (unsigned int)count);
676         return 1;
677         }
678 static int digest_md5_final(EVP_MD_CTX *ctx,unsigned char *md)
679         {
680         MD5Final(md, data(ctx));
681         return 1;
682         }