add test
[openssl.git] / demos / engines / rsaref / rsaref.c
index bc68a711e8fc7cf61cd2c4d1b4cd850d1b4cf131..872811b8f72fbadfff5cf606e578e05261d7cef0 100644 (file)
@@ -3,19 +3,34 @@
    be found a little here and there. */
 
 #include <stdio.h>
-#if 0
+#include <string.h>
 #include "./source/global.h"
 #include "./source/rsaref.h"
-#endif
-#include "rsaref.h"
+#include "./source/rsa.h"
+#include "./source/des.h"
 #include <openssl/err.h>
+#define OPENSSL_NO_MD2
+#define OPENSSL_NO_MD5
+#include <openssl/evp.h>
 #include <openssl/bn.h>
 #include <openssl/engine.h>
 
-/* Constants used when creating the ENGINE */
+#define RSAREF_LIB_NAME "rsaref engine"
+#include "rsaref_err.c"
+
+/*****************************************************************************
+ *** Function declarations and global variable definitions                 ***
+ *****************************************************************************/
+
+/*****************************************************************************
+ * Constants used when creating the ENGINE
+ **/
 static const char *engine_rsaref_id = "rsaref";
 static const char *engine_rsaref_name = "RSAref engine support";
 
+/*****************************************************************************
+ * Functions to handle the engine
+ **/
 static int rsaref_destroy(ENGINE *e);
 static int rsaref_init(ENGINE *e);
 static int rsaref_finish(ENGINE *e);
@@ -23,6 +38,16 @@ static int rsaref_finish(ENGINE *e);
 static int rsaref_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)()); 
 #endif
 
+/*****************************************************************************
+ * Engine commands
+ **/
+static const ENGINE_CMD_DEFN rsaref_cmd_defns[] = {
+       {0, NULL, NULL, 0}
+       };
+
+/*****************************************************************************
+ * RSA functions
+ **/
 static int rsaref_private_decrypt(int len, const unsigned char *from,
        unsigned char *to, RSA *rsa, int padding);
 static int rsaref_private_encrypt(int len, const unsigned char *from,
@@ -35,10 +60,9 @@ static int bnref_mod_exp(BIGNUM *r,const BIGNUM *a,const BIGNUM *p,const BIGNUM
                          BN_CTX *ctx, BN_MONT_CTX *m_ctx);
 static int rsaref_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa);
 
-static const ENGINE_CMD_DEFN rsaref_cmd_defns[] = {
-       {0, NULL, NULL, 0}
-       };
-
+/*****************************************************************************
+ * Our RSA method
+ **/
 static RSA_METHOD rsaref_rsa =
 {
   "RSAref PKCS#1 RSA",
@@ -56,96 +80,140 @@ static RSA_METHOD rsaref_rsa =
   NULL
 };
 
-#ifndef OPENSSL_NO_ERR
-/* Error function codes for use in rsaref operation */
-#define RSAREF_F_BNREF_MOD_EXP                          100
-#define RSAREF_F_RSAREF_BN2BIN                          101
-#define RSAREF_F_RSA_BN2BIN                             102
-#define RSAREF_F_RSA_PRIVATE_DECRYPT                    103
-#define RSAREF_F_RSA_PRIVATE_ENCRYPT                    104
-#define RSAREF_F_RSA_PUBLIC_DECRYPT                     105
-#define RSAREF_F_RSA_PUBLIC_ENCRYPT                     106
-#define RSAREF_F_RSAREF_MOD_EXP                                 108
-#define RSAREF_F_RSAREF_PRIVATE_DECRYPT                         109
-#define RSAREF_F_RSAREF_PRIVATE_ENCRYPT                         110
-#define RSAREF_F_RSAREF_PUBLIC_DECRYPT                  111
-#define RSAREF_F_RSAREF_PUBLIC_ENCRYPT                  112
-/* Error reason codes */
-#define RSAREF_R_CONTENT_ENCODING                       0x0400
-#define RSAREF_R_DATA                                   0x0401
-#define RSAREF_R_DIGEST_ALGORITHM                       0x0402
-#define RSAREF_R_ENCODING                               0x0403
-#define RSAREF_R_ENCRYPTION_ALGORITHM                   0x040d
-#define RSAREF_R_KEY                                    0x0404
-#define RSAREF_R_KEY_ENCODING                           0x0405
-#define RSAREF_R_LEN                                    0x0406
-#define RSAREF_R_MODULUS_LEN                            0x0407
-#define RSAREF_R_NEED_RANDOM                            0x0408
-#define RSAREF_R_PRIVATE_KEY                            0x0409
-#define RSAREF_R_PUBLIC_KEY                             0x040a
-#define RSAREF_R_SIGNATURE                              0x040b
-#define RSAREF_R_SIGNATURE_ENCODING                     0x040c
-
-static ERR_STRING_DATA rsaref_str_functs[] =
-       {
-       /* This first element is changed to match the dynamic 'lib' number */
-{ERR_PACK(0,0,0),                              "rsaref engine code"},
-{ERR_PACK(0,RSAREF_F_BNREF_MOD_EXP,0), "BN_REF_MOD_EXP"},
-{ERR_PACK(0,RSAREF_F_RSAREF_BN2BIN,0), "RSAREF_BN2BIN"},
-{ERR_PACK(0,RSAREF_F_RSA_BN2BIN,0),    "RSA_BN2BIN"},
-{ERR_PACK(0,RSAREF_F_RSA_PRIVATE_DECRYPT,0),   "RSA_private_decrypt"},
-{ERR_PACK(0,RSAREF_F_RSA_PRIVATE_ENCRYPT,0),   "RSA_private_encrypt"},
-{ERR_PACK(0,RSAREF_F_RSA_PUBLIC_DECRYPT,0),    "RSA_public_decrypt"},
-{ERR_PACK(0,RSAREF_F_RSA_PUBLIC_ENCRYPT,0),    "RSA_public_encrypt"},
-{ERR_PACK(0,RSAREF_F_RSAREF_MOD_EXP,0),        "RSA_REF_MOD_EXP"},
-{ERR_PACK(0,RSAREF_F_RSAREF_PRIVATE_DECRYPT,0),        "RSA_REF_PRIVATE_DECRYPT"},
-{ERR_PACK(0,RSAREF_F_RSAREF_PRIVATE_ENCRYPT,0),        "RSA_REF_PRIVATE_ENCRYPT"},
-{ERR_PACK(0,RSAREF_F_RSAREF_PUBLIC_DECRYPT,0), "RSA_REF_PUBLIC_DECRYPT"},
-{ERR_PACK(0,RSAREF_F_RSAREF_PUBLIC_ENCRYPT,0), "RSA_REF_PUBLIC_ENCRYPT"},
-{RSAREF_R_CONTENT_ENCODING               ,"content encoding"},
-{RSAREF_R_DATA                           ,"data"},
-{RSAREF_R_DIGEST_ALGORITHM               ,"digest algorithm"},
-{RSAREF_R_ENCODING                       ,"encoding"},
-{RSAREF_R_ENCRYPTION_ALGORITHM           ,"encryption algorithm"},
-{RSAREF_R_KEY                            ,"key"},
-{RSAREF_R_KEY_ENCODING                   ,"key encoding"},
-{RSAREF_R_LEN                            ,"len"},
-{RSAREF_R_MODULUS_LEN                    ,"modulus len"},
-{RSAREF_R_NEED_RANDOM                    ,"need random"},
-{RSAREF_R_PRIVATE_KEY                    ,"private key"},
-{RSAREF_R_PUBLIC_KEY                     ,"public key"},
-{RSAREF_R_SIGNATURE                      ,"signature"},
-{RSAREF_R_SIGNATURE_ENCODING             ,"signature encoding"},
-{0,NULL}
+/*****************************************************************************
+ * Symetric cipher and digest function registrars
+ **/
+static int rsaref_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
+       const int **nids, int nid);
+static int rsaref_digests(ENGINE *e, const EVP_MD **digest,
+       const int **nids, int nid);
+
+static int rsaref_cipher_nids[] =
+       { NID_des_cbc, NID_des_ede3_cbc, NID_desx_cbc, 0 };
+static int rsaref_digest_nids[] =
+       { NID_md2, NID_md5, 0 };
+
+/*****************************************************************************
+ * DES functions
+ **/
+static int cipher_des_cbc_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
+       const unsigned char *iv, int enc);
+static int cipher_des_cbc_code(EVP_CIPHER_CTX *ctx, unsigned char *out,
+       const unsigned char *in, unsigned int inl);
+static int cipher_des_cbc_clean(EVP_CIPHER_CTX *);
+static int cipher_des_ede3_cbc_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
+       const unsigned char *iv, int enc);
+static int cipher_des_ede3_cbc_code(EVP_CIPHER_CTX *ctx, unsigned char *out,
+       const unsigned char *in, unsigned int inl);
+static int cipher_des_ede3_cbc_clean(EVP_CIPHER_CTX *);
+static int cipher_desx_cbc_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
+       const unsigned char *iv, int enc);
+static int cipher_desx_cbc_code(EVP_CIPHER_CTX *ctx, unsigned char *out,
+       const unsigned char *in, unsigned int inl);
+static int cipher_desx_cbc_clean(EVP_CIPHER_CTX *);
+
+/*****************************************************************************
+ * Our DES ciphers
+ **/
+static const EVP_CIPHER cipher_des_cbc =
+       {
+       NID_des_cbc,
+       8, 8, 8,
+       0 | EVP_CIPH_CBC_MODE,
+       cipher_des_cbc_init,
+       cipher_des_cbc_code,
+       cipher_des_cbc_clean,
+       sizeof(DES_CBC_CTX),
+       NULL,
+       NULL,
+       NULL,
+       NULL
        };
-/* The library number we obtain dynamically from the ERR code */
-static int rsaref_err_lib = -1;
-#define RSAREFerr(f,r) ERR_PUT_error(rsaref_err_lib,(f),(r),__FILE__,__LINE__)
-static void rsaref_load_error_strings(void)
+
+static const EVP_CIPHER cipher_des_ede3_cbc =
        {
-       if(rsaref_err_lib < 0)
-               {
-               if((rsaref_err_lib = ERR_get_next_error_library()) <= 0)
-                       return;
-               rsaref_str_functs[0].error = ERR_PACK(rsaref_err_lib,0,0);
-               ERR_load_strings(rsaref_err_lib, rsaref_str_functs);
-               }
-       }
-static void rsaref_unload_error_strings(void)
+       NID_des_ede3_cbc,
+       8, 24, 8,
+       0 | EVP_CIPH_CBC_MODE,
+       cipher_des_ede3_cbc_init,
+       cipher_des_ede3_cbc_code,
+       cipher_des_ede3_cbc_clean,
+       sizeof(DES3_CBC_CTX),
+       NULL,
+       NULL,
+       NULL,
+       NULL
+       };
+
+static const EVP_CIPHER cipher_desx_cbc =
        {
-       if(rsaref_err_lib >= 0)
-               {
-               ERR_unload_strings(rsaref_err_lib, rsaref_str_functs);
-               rsaref_err_lib = -1;
-               }
-       }
-#else
-#define RSAREFerr(f,r)                                 /* NOP */
-static void rsaref_load_error_strings(void) { }                /* NOP */
-static void rsaref_unload_error_strings(void) { }      /* NOP */
-#endif
+       NID_desx_cbc,
+       8, 24, 8,
+       0 | EVP_CIPH_CBC_MODE,
+       cipher_desx_cbc_init,
+       cipher_desx_cbc_code,
+       cipher_desx_cbc_clean,
+       sizeof(DESX_CBC_CTX),
+       NULL,
+       NULL,
+       NULL,
+       NULL
+       };
 
-/* Now, to our own code */
+/*****************************************************************************
+ * MD functions
+ **/
+static int digest_md2_init(EVP_MD_CTX *ctx);
+static int digest_md2_update(EVP_MD_CTX *ctx,const void *data,
+       unsigned long count);
+static int digest_md2_final(EVP_MD_CTX *ctx,unsigned char *md);
+static int digest_md5_init(EVP_MD_CTX *ctx);
+static int digest_md5_update(EVP_MD_CTX *ctx,const void *data,
+       unsigned long count);
+static int digest_md5_final(EVP_MD_CTX *ctx,unsigned char *md);
+
+/*****************************************************************************
+ * Our MD digests
+ **/
+static const EVP_MD digest_md2 =
+       {
+       NID_md2,
+       NID_md2WithRSAEncryption,
+       16,
+       0,
+       digest_md2_init,
+       digest_md2_update,
+       digest_md2_final,
+       NULL,
+       NULL,
+       EVP_PKEY_RSA_method,
+       16,
+       sizeof(MD2_CTX)
+       };
+
+static const EVP_MD digest_md5 =
+       {
+       NID_md5,
+       NID_md5WithRSAEncryption,
+       16,
+       0,
+       digest_md5_init,
+       digest_md5_update,
+       digest_md5_final,
+       NULL,
+       NULL,
+       EVP_PKEY_RSA_method,
+       64,
+       sizeof(MD5_CTX)
+       };
+
+/*****************************************************************************
+ *** Function definitions                                                  ***
+ *****************************************************************************/
+
+/*****************************************************************************
+ * Functions to handle the engine
+ **/
 
 static int bind_rsaref(ENGINE *e)
        {
@@ -153,6 +221,8 @@ static int bind_rsaref(ENGINE *e)
        if(!ENGINE_set_id(e, engine_rsaref_id)
                || !ENGINE_set_name(e, engine_rsaref_name)
                || !ENGINE_set_RSA(e, &rsaref_rsa)
+               || !ENGINE_set_ciphers(e, rsaref_ciphers)
+               || !ENGINE_set_digests(e, rsaref_digests)
                || !ENGINE_set_destroy_function(e, rsaref_destroy)
                || !ENGINE_set_init_function(e, rsaref_init)
                || !ENGINE_set_finish_function(e, rsaref_finish)
@@ -161,7 +231,7 @@ static int bind_rsaref(ENGINE *e)
                return 0;
 
        /* Ensure the rsaref error handling is set up */
-       rsaref_load_error_strings();
+       ERR_load_RSAREF_strings();
        return 1;
        }
 
@@ -216,10 +286,14 @@ static int rsaref_finish(ENGINE *e)
 /* Destructor (complements the "ENGINE_ncipher()" constructor) */
 static int rsaref_destroy(ENGINE *e)
        {
-       rsaref_unload_error_strings();
+       ERR_unload_RSAREF_strings();
        return 1;
        }
 
+/*****************************************************************************
+ * RSA functions
+ **/
+
 static int rsaref_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa)
        {
        RSAREFerr(RSAREF_F_RSAREF_MOD_EXP,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
@@ -274,11 +348,11 @@ static int RSAref_Public_ref2eay(RSArefPublicKey *from, RSA *to)
        }
 #endif
 
-static int RSAref_Public_eay2ref(RSA *from, RSArefPublicKey *to)
+static int RSAref_Public_eay2ref(RSA *from, R_RSA_PUBLIC_KEY *to)
        {
        to->bits=BN_num_bits(from->n);
-       if (!RSAref_bn2bin(from->n,to->m,RSAref_MAX_LEN)) return(0);
-       if (!RSAref_bn2bin(from->e,to->e,RSAref_MAX_LEN)) return(0);
+       if (!RSAref_bn2bin(from->n,to->modulus,MAX_RSA_MODULUS_LEN)) return(0);
+       if (!RSAref_bn2bin(from->e,to->exponent,MAX_RSA_MODULUS_LEN)) return(0);
        return(1);
        }
 
@@ -307,17 +381,17 @@ static int RSAref_Private_ref2eay(RSArefPrivateKey *from, RSA *to)
        }
 #endif
 
-static int RSAref_Private_eay2ref(RSA *from, RSArefPrivateKey *to)
+static int RSAref_Private_eay2ref(RSA *from, R_RSA_PRIVATE_KEY *to)
        {
        to->bits=BN_num_bits(from->n);
-       if (!RSAref_bn2bin(from->n,to->m,RSAref_MAX_LEN)) return(0);
-       if (!RSAref_bn2bin(from->e,to->e,RSAref_MAX_LEN)) return(0);
-       if (!RSAref_bn2bin(from->d,to->d,RSAref_MAX_LEN)) return(0);
-       if (!RSAref_bn2bin(from->p,to->prime[0],RSAref_MAX_PLEN)) return(0);
-       if (!RSAref_bn2bin(from->q,to->prime[1],RSAref_MAX_PLEN)) return(0);
-       if (!RSAref_bn2bin(from->dmp1,to->pexp[0],RSAref_MAX_PLEN)) return(0);
-       if (!RSAref_bn2bin(from->dmq1,to->pexp[1],RSAref_MAX_PLEN)) return(0);
-       if (!RSAref_bn2bin(from->iqmp,to->coef,RSAref_MAX_PLEN)) return(0);
+       if (!RSAref_bn2bin(from->n,to->modulus,MAX_RSA_MODULUS_LEN)) return(0);
+       if (!RSAref_bn2bin(from->e,to->publicExponent,MAX_RSA_MODULUS_LEN)) return(0);
+       if (!RSAref_bn2bin(from->d,to->exponent,MAX_RSA_MODULUS_LEN)) return(0);
+       if (!RSAref_bn2bin(from->p,to->prime[0],MAX_RSA_PRIME_LEN)) return(0);
+       if (!RSAref_bn2bin(from->q,to->prime[1],MAX_RSA_PRIME_LEN)) return(0);
+       if (!RSAref_bn2bin(from->dmp1,to->primeExponent[0],MAX_RSA_PRIME_LEN)) return(0);
+       if (!RSAref_bn2bin(from->dmq1,to->primeExponent[1],MAX_RSA_PRIME_LEN)) return(0);
+       if (!RSAref_bn2bin(from->iqmp,to->coefficient,MAX_RSA_PRIME_LEN)) return(0);
        return(1);
        }
 
@@ -325,13 +399,13 @@ static int rsaref_private_decrypt(int len, const unsigned char *from, unsigned c
             RSA *rsa, int padding)
        {
        int i,outlen= -1;
-       RSArefPrivateKey RSAkey;
+       R_RSA_PRIVATE_KEY RSAkey;
 
        if (!RSAref_Private_eay2ref(rsa,&RSAkey))
                goto err;
-       if ((i=RSAPrivateDecrypt(to,&outlen,(unsigned char *)from,len,&RSAkey)) != 0)
+       if ((i=RSAPrivateDecrypt(to,(unsigned int *)&outlen,(unsigned char *)from,len,&RSAkey)) != 0)
                {
-               RSAREFerr(RSAREF_F_RSA_REF_PRIVATE_DECRYPT,i);
+               RSAREFerr(RSAREF_F_RSAREF_PRIVATE_DECRYPT,i);
                outlen= -1;
                }
 err:
@@ -343,18 +417,18 @@ static int rsaref_private_encrypt(int len, const unsigned char *from, unsigned c
             RSA *rsa, int padding)
        {
        int i,outlen= -1;
-       RSArefPrivateKey RSAkey;
+       R_RSA_PRIVATE_KEY RSAkey;
 
        if (padding != RSA_PKCS1_PADDING)
                {
-               RSAREFerr(RSAREF_F_RSA_REF_PRIVATE_ENCRYPT, RSA_R_UNKNOWN_PADDING_TYPE);
+               RSAREFerr(RSAREF_F_RSAREF_PRIVATE_ENCRYPT, RSA_R_UNKNOWN_PADDING_TYPE);
                goto err;
        }
        if (!RSAref_Private_eay2ref(rsa,&RSAkey))
                goto err;
-       if ((i=RSAPrivateEncrypt(to,&outlen,(unsigned char *)from,len,&RSAkey)) != 0)
+       if ((i=RSAPrivateEncrypt(to,(unsigned int)&outlen,(unsigned char *)from,len,&RSAkey)) != 0)
                {
-               RSAREFerr(RSAREF_F_RSA_REF_PRIVATE_ENCRYPT,i);
+               RSAREFerr(RSAREF_F_RSAREF_PRIVATE_ENCRYPT,i);
                outlen= -1;
                }
 err:
@@ -366,13 +440,13 @@ static int rsaref_public_decrypt(int len, const unsigned char *from, unsigned ch
             RSA *rsa, int padding)
        {
        int i,outlen= -1;
-       RSArefPublicKey RSAkey;
+       R_RSA_PUBLIC_KEY RSAkey;
 
        if (!RSAref_Public_eay2ref(rsa,&RSAkey))
                goto err;
-       if ((i=RSAPublicDecrypt(to,&outlen,(unsigned char *)from,len,&RSAkey)) != 0)
+       if ((i=RSAPublicDecrypt(to,(unsigned int)&outlen,(unsigned char *)from,len,&RSAkey)) != 0)
                {
-               RSAREFerr(RSAREF_F_RSA_REF_PUBLIC_DECRYPT,i);
+               RSAREFerr(RSAREF_F_RSAREF_PUBLIC_DECRYPT,i);
                outlen= -1;
                }
 err:
@@ -385,13 +459,13 @@ static int rsaref_public_encrypt(int len, const unsigned char *from, unsigned ch
        {
        int outlen= -1;
        int i;
-       RSArefPublicKey RSAkey;
-       RSARandomState rnd;
+       R_RSA_PUBLIC_KEY RSAkey;
+       R_RANDOM_STRUCT rnd;
        unsigned char buf[16];
 
        if (padding != RSA_PKCS1_PADDING && padding != RSA_SSLV23_PADDING) 
                {
-               RSAREFerr(RSAREF_F_RSA_REF_PUBLIC_ENCRYPT, RSA_R_UNKNOWN_PADDING_TYPE);
+               RSAREFerr(RSAREF_F_RSAREF_PUBLIC_ENCRYPT, RSA_R_UNKNOWN_PADDING_TYPE);
                goto err;
                }
        
@@ -407,9 +481,9 @@ static int rsaref_public_encrypt(int len, const unsigned char *from, unsigned ch
 
        if (!RSAref_Public_eay2ref(rsa,&RSAkey))
                goto err;
-       if ((i=RSAPublicEncrypt(to,&outlen,(unsigned char *)from,len,&RSAkey,&rnd)) != 0)
+       if ((i=RSAPublicEncrypt(to,(unsigned int)&outlen,(unsigned char *)from,len,&RSAkey,&rnd)) != 0)
                {
-               RSAREFerr(RSAREF_F_RSA_REF_PUBLIC_ENCRYPT,i);
+               RSAREFerr(RSAREF_F_RSAREF_PUBLIC_ENCRYPT,i);
                outlen= -1;
                goto err;
                }
@@ -419,3 +493,193 @@ err:
        memset(&rnd,0,sizeof(rnd));
        return(outlen);
        }
+
+/*****************************************************************************
+ * Symetric cipher and digest function registrars
+ **/
+static int rsaref_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
+       const int **nids, int nid)
+       {
+       int ok = 1;
+       if(!cipher)
+               {
+               /* We are returning a list of supported nids */
+               *nids = rsaref_cipher_nids;
+               return (sizeof(rsaref_cipher_nids)-1)/sizeof(rsaref_cipher_nids[0]);
+               }
+       /* We are being asked for a specific cipher */
+       switch (nid)
+               {
+       case NID_des_cbc:
+               *cipher = &cipher_des_cbc; break;
+       case NID_des_ede3_cbc:
+               *cipher = &cipher_des_ede3_cbc; break;
+       case NID_desx_cbc:
+               *cipher = &cipher_desx_cbc; break;
+       default:
+               ok = 0;
+               *cipher = NULL;
+               break;
+               }
+       return ok;
+       }
+static int rsaref_digests(ENGINE *e, const EVP_MD **digest,
+       const int **nids, int nid)
+       {
+       int ok = 1;
+       if(!digest)
+               {
+               /* We are returning a list of supported nids */
+               *nids = rsaref_digest_nids;
+               return (sizeof(rsaref_digest_nids)-1)/sizeof(rsaref_digest_nids[0]);
+               }
+       /* We are being asked for a specific digest */
+       switch (nid)
+               {
+       case NID_md2:
+               *digest = &digest_md2; break;
+       case NID_md5:
+               *digest = &digest_md5; break;
+       default:
+               ok = 0;
+               *digest = NULL;
+               break;
+               }
+       return ok;
+       }
+
+/*****************************************************************************
+ * DES functions
+ **/
+#undef data
+#define data(ctx) ((DES_CBC_CTX *)(ctx)->cipher_data)
+static int cipher_des_cbc_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
+       const unsigned char *iv, int enc)
+       {
+       DES_CBCInit(data(ctx), (unsigned char *)key, (unsigned char *)iv, enc);
+       return 1;
+       }
+static int cipher_des_cbc_code(EVP_CIPHER_CTX *ctx, unsigned char *out,
+       const unsigned char *in, unsigned int inl)
+       {
+       int ret = DES_CBCUpdate(data(ctx), out, (unsigned char *)in, inl);
+       switch (ret)
+               {
+       case RE_LEN:
+               RSAREFerr(RSAREF_F_CIPHER_DES_CBC_CODE,RSAREF_R_LENGTH_NOT_BLOCK_ALIGNED);
+               break;
+       case 0:
+               break;
+       default:
+               RSAREFerr(RSAREF_F_CIPHER_DES_CBC_CODE,RSAREF_R_UNKNOWN_FAULT);
+               }
+       return !ret;
+       }
+static int cipher_des_cbc_clean(EVP_CIPHER_CTX *ctx)
+       {
+       memset(data(ctx), 0, ctx->cipher->ctx_size);
+       return 1;
+       }
+
+#undef data
+#define data(ctx) ((DES3_CBC_CTX *)(ctx)->cipher_data)
+static int cipher_des_ede3_cbc_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
+       const unsigned char *iv, int enc)
+       {
+       DES3_CBCInit(data(ctx), (unsigned char *)key, (unsigned char *)iv,
+               enc);
+       return 1;
+       }
+static int cipher_des_ede3_cbc_code(EVP_CIPHER_CTX *ctx, unsigned char *out,
+       const unsigned char *in, unsigned int inl)
+       {
+       int ret = DES3_CBCUpdate(data(ctx), out, (unsigned char *)in, inl);
+       switch (ret)
+               {
+       case RE_LEN:
+               RSAREFerr(RSAREF_F_CIPHER_DES_CBC_CODE,RSAREF_R_LENGTH_NOT_BLOCK_ALIGNED);
+               break;
+       case 0:
+               break;
+       default:
+               RSAREFerr(RSAREF_F_CIPHER_DES_CBC_CODE,RSAREF_R_UNKNOWN_FAULT);
+               }
+       return !ret;
+       }
+static int cipher_des_ede3_cbc_clean(EVP_CIPHER_CTX *ctx)
+       {
+       memset(data(ctx), 0, ctx->cipher->ctx_size);
+       return 1;
+       }
+
+#undef data
+#define data(ctx) ((DESX_CBC_CTX *)(ctx)->cipher_data)
+static int cipher_desx_cbc_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
+       const unsigned char *iv, int enc)
+       {
+       DESX_CBCInit(data(ctx), (unsigned char *)key, (unsigned char *)iv,
+               enc);
+       return 1;
+       }
+static int cipher_desx_cbc_code(EVP_CIPHER_CTX *ctx, unsigned char *out,
+       const unsigned char *in, unsigned int inl)
+       {
+       int ret = DESX_CBCUpdate(data(ctx), out, (unsigned char *)in, inl);
+       switch (ret)
+               {
+       case RE_LEN:
+               RSAREFerr(RSAREF_F_CIPHER_DES_CBC_CODE,RSAREF_R_LENGTH_NOT_BLOCK_ALIGNED);
+               break;
+       case 0:
+               break;
+       default:
+               RSAREFerr(RSAREF_F_CIPHER_DES_CBC_CODE,RSAREF_R_UNKNOWN_FAULT);
+               }
+       return !ret;
+       }
+static int cipher_desx_cbc_clean(EVP_CIPHER_CTX *ctx)
+       {
+       memset(data(ctx), 0, ctx->cipher->ctx_size);
+       return 1;
+       }
+
+/*****************************************************************************
+ * MD functions
+ **/
+#undef data
+#define data(ctx) ((MD2_CTX *)(ctx)->md_data)
+static int digest_md2_init(EVP_MD_CTX *ctx)
+       {
+       MD2Init(data(ctx));
+       return 1;
+       }
+static int digest_md2_update(EVP_MD_CTX *ctx,const void *data,
+       unsigned long count)
+       {
+       MD2Update(data(ctx), (unsigned char *)data, (unsigned int)count);
+       return 1;
+       }
+static int digest_md2_final(EVP_MD_CTX *ctx,unsigned char *md)
+       {
+       MD2Final(md, data(ctx));
+       return 1;
+       }
+
+#undef data
+#define data(ctx) ((MD5_CTX *)(ctx)->md_data)
+static int digest_md5_init(EVP_MD_CTX *ctx)
+       {
+       MD5Init(data(ctx));
+       return 1;
+       }
+static int digest_md5_update(EVP_MD_CTX *ctx,const void *data,
+       unsigned long count)
+       {
+       MD5Update(data(ctx), (unsigned char *)data, (unsigned int)count);
+       return 1;
+       }
+static int digest_md5_final(EVP_MD_CTX *ctx,unsigned char *md)
+       {
+       MD5Final(md, data(ctx));
+       return 1;
+       }