Stupid casts...
[openssl.git] / demos / engines / rsaref / rsaref.c
index b044badc0582b172b16d1292f9c9b323d418f767..f97974fc497939f3734679abf72d9d320d01f705 100644 (file)
@@ -3,11 +3,14 @@
    be found a little here and there. */
 
 #include <stdio.h>
+#include <string.h>
 #include "./source/global.h"
 #include "./source/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>
@@ -88,35 +91,35 @@ static int rsaref_digests(ENGINE *e, const EVP_MD **digest,
 static int rsaref_cipher_nids[] =
        { NID_des_cbc, NID_des_ede3_cbc, NID_desx_cbc, 0 };
 static int rsaref_digest_nids[] =
-       { 0 };
+       { NID_md2, NID_md5, 0 };
 
 /*****************************************************************************
  * DES functions
  **/
-int cipher_des_cbc_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
+static int cipher_des_cbc_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
        const unsigned char *iv, int enc);
-int cipher_des_cbc_code(EVP_CIPHER_CTX *ctx, unsigned char *out,
+static int cipher_des_cbc_code(EVP_CIPHER_CTX *ctx, unsigned char *out,
        const unsigned char *in, unsigned int inl);
-int cipher_des_cbc_clean(EVP_CIPHER_CTX *);
-int cipher_des_ede3_cbc_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
+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);
-int cipher_des_ede3_cbc_code(EVP_CIPHER_CTX *ctx, unsigned char *out,
+static int cipher_des_ede3_cbc_code(EVP_CIPHER_CTX *ctx, unsigned char *out,
        const unsigned char *in, unsigned int inl);
-int cipher_des_ede3_cbc_clean(EVP_CIPHER_CTX *);
-int cipher_desx_cbc_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
+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);
-int cipher_desx_cbc_code(EVP_CIPHER_CTX *ctx, unsigned char *out,
+static int cipher_desx_cbc_code(EVP_CIPHER_CTX *ctx, unsigned char *out,
        const unsigned char *in, unsigned int inl);
-int cipher_desx_cbc_clean(EVP_CIPHER_CTX *);
+static int cipher_desx_cbc_clean(EVP_CIPHER_CTX *);
 
 /*****************************************************************************
  * Our DES ciphers
  **/
-static EVP_CIPHER cipher_des_cbc =
+static const EVP_CIPHER cipher_des_cbc =
        {
        NID_des_cbc,
        8, 8, 8,
-       0,
+       0 | EVP_CIPH_CBC_MODE,
        cipher_des_cbc_init,
        cipher_des_cbc_code,
        cipher_des_cbc_clean,
@@ -127,11 +130,11 @@ static EVP_CIPHER cipher_des_cbc =
        NULL
        };
 
-static EVP_CIPHER cipher_des_ede3_cbc =
+static const EVP_CIPHER cipher_des_ede3_cbc =
        {
        NID_des_ede3_cbc,
        8, 24, 8,
-       0,
+       0 | EVP_CIPH_CBC_MODE,
        cipher_des_ede3_cbc_init,
        cipher_des_ede3_cbc_code,
        cipher_des_ede3_cbc_clean,
@@ -142,11 +145,11 @@ static EVP_CIPHER cipher_des_ede3_cbc =
        NULL
        };
 
-static EVP_CIPHER cipher_desx_cbc =
+static const EVP_CIPHER cipher_desx_cbc =
        {
        NID_desx_cbc,
        8, 24, 8,
-       0,
+       0 | EVP_CIPH_CBC_MODE,
        cipher_desx_cbc_init,
        cipher_desx_cbc_code,
        cipher_desx_cbc_clean,
@@ -157,6 +160,53 @@ static EVP_CIPHER cipher_desx_cbc =
        NULL
        };
 
+/*****************************************************************************
+ * 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                                                  ***
  *****************************************************************************/
@@ -353,7 +403,7 @@ static int rsaref_private_decrypt(int len, const unsigned char *from, unsigned c
 
        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_RSAREF_PRIVATE_DECRYPT,i);
                outlen= -1;
@@ -376,7 +426,7 @@ static int rsaref_private_encrypt(int len, const unsigned char *from, unsigned c
        }
        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_RSAREF_PRIVATE_ENCRYPT,i);
                outlen= -1;
@@ -394,7 +444,7 @@ static int rsaref_public_decrypt(int len, const unsigned char *from, unsigned ch
 
        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_RSAREF_PUBLIC_DECRYPT,i);
                outlen= -1;
@@ -431,7 +481,7 @@ 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_RSAREF_PUBLIC_ENCRYPT,i);
                outlen= -1;
@@ -486,6 +536,10 @@ static int rsaref_digests(ENGINE *e, const EVP_MD **digest,
        /* 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;
@@ -499,53 +553,133 @@ static int rsaref_digests(ENGINE *e, const EVP_MD **digest,
  **/
 #undef data
 #define data(ctx) ((DES_CBC_CTX *)(ctx)->cipher_data)
-int cipher_des_cbc_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
+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;
        }
-int cipher_des_cbc_code(EVP_CIPHER_CTX *ctx, unsigned char *out,
+static int cipher_des_cbc_code(EVP_CIPHER_CTX *ctx, unsigned char *out,
        const unsigned char *in, unsigned int inl)
        {
-       DES_CBCUpdate(data(ctx), out, (unsigned char *)in, 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;
        }
-int cipher_des_cbc_clean(EVP_CIPHER_CTX *ctx)
+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)
-int cipher_des_ede3_cbc_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
+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;
        }
-int cipher_des_ede3_cbc_code(EVP_CIPHER_CTX *ctx, unsigned char *out,
+static int cipher_des_ede3_cbc_code(EVP_CIPHER_CTX *ctx, unsigned char *out,
        const unsigned char *in, unsigned int inl)
        {
-       DES3_CBCUpdate(data(ctx), out, (unsigned char *)in, 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;
        }
-int cipher_des_ede3_cbc_clean(EVP_CIPHER_CTX *ctx)
+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)
-int cipher_desx_cbc_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
+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;
        }
-int cipher_desx_cbc_code(EVP_CIPHER_CTX *ctx, unsigned char *out,
+static int cipher_desx_cbc_code(EVP_CIPHER_CTX *ctx, unsigned char *out,
        const unsigned char *in, unsigned int inl)
        {
-       DESX_CBCUpdate(data(ctx), out, (unsigned char *)in, 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;
        }
-int cipher_desx_cbc_clean(EVP_CIPHER_CTX *ctx)
+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;
        }