10139N/Adiff -urN -x /home/sbade/dontdirr openssl-engine-0.9.6e.base/crypto/engine/Makefile openssl-engine-0.9.6e.patch/crypto/engine/Makefile
10139N/A--- openssl-engine-0.9.6e.base/crypto/engine/Makefile Thu Feb 7 23:04:30 2002
10139N/A+++ openssl-engine-0.9.6e.patch/crypto/engine/Makefile Fri Aug 2 14:37:08 2002
10139N/A@@ -24,10 +24,10 @@
10139N/A LIB=$(TOP)/libcrypto.a
10139N/A LIBSRC= engine_err.c engine_lib.c engine_list.c engine_openssl.c \
10139N/A hw_atalla.c hw_cswift.c hw_ncipher.c hw_aep.c hw_sureware.c \
10139N/A- hw_ubsec.c hw_keyclient.c
10139N/A+ hw_ubsec.c hw_keyclient.c hw_ibmca.c
10139N/A LIBOBJ= engine_err.o engine_lib.o engine_list.o engine_openssl.o \
10139N/A hw_atalla.o hw_cswift.o hw_ncipher.o hw_aep.o hw_sureware.o \
10139N/A- hw_ubsec.o hw_keyclient.o
10139N/A+ hw_ubsec.o hw_keyclient.o hw_ibmca.o
10139N/A
10139N/A SRC= $(LIBSRC)
10139N/A
10139N/Adiff -urN -x /home/sbade/dontdirr openssl-engine-0.9.6e.base/crypto/engine/Makefile.ssl openssl-engine-0.9.6e.patch/crypto/engine/Makefile.ssl
10139N/A--- openssl-engine-0.9.6e.base/crypto/engine/Makefile.ssl Thu Feb 7 23:04:30 2002
10139N/A+++ openssl-engine-0.9.6e.patch/crypto/engine/Makefile.ssl Fri Aug 2 14:37:08 2002
10139N/A@@ -24,10 +24,10 @@
10139N/A LIB=$(TOP)/libcrypto.a
10139N/A LIBSRC= engine_err.c engine_lib.c engine_list.c engine_openssl.c \
10139N/A hw_atalla.c hw_cswift.c hw_ncipher.c hw_aep.c hw_sureware.c \
10139N/A- hw_ubsec.c hw_keyclient.c
10139N/A+ hw_ubsec.c hw_keyclient.c hw_ibmca.c
10139N/A LIBOBJ= engine_err.o engine_lib.o engine_list.o engine_openssl.o \
10139N/A hw_atalla.o hw_cswift.o hw_ncipher.o hw_aep.o hw_sureware.o \
10139N/A- hw_ubsec.o hw_keyclient.o
10139N/A+ hw_ubsec.o hw_keyclient.o hw_ibmca.o
10139N/A
10139N/A SRC= $(LIBSRC)
10139N/A
10139N/Adiff -urN -x /home/sbade/dontdirr openssl-engine-0.9.6e.base/crypto/engine/engine.h openssl-engine-0.9.6e.patch/crypto/engine/engine.h
10139N/A--- openssl-engine-0.9.6e.base/crypto/engine/engine.h Thu Feb 7 23:04:29 2002
10139N/A+++ openssl-engine-0.9.6e.patch/crypto/engine/engine.h Fri Aug 2 14:37:08 2002
10139N/A@@ -406,6 +406,13 @@
10139N/A #define ENGINE_F_UBSEC_RNG_BYTES 172
10139N/A #define ENGINE_F_UBSEC_RSA_MOD_EXP 168
10139N/A #define ENGINE_F_UBSEC_RSA_MOD_EXP_CRT 169
10139N/A+#define ENGINE_F_IBMCA_FINISH 180
10139N/A+#define ENGINE_F_IBMCA_INIT 181
10139N/A+#define ENGINE_F_IBMCA_MOD_EXP 182
10139N/A+#define ENGINE_F_IBMCA_MOD_EXP_CRT 183
10139N/A+#define ENGINE_F_IBMCA_RAND_BYTES 184
10139N/A+#define ENGINE_F_IBMCA_RSA_MOD_EXP 185
10139N/A+
10139N/A
10139N/A /* Reason codes. */
10139N/A #define ENGINE_R_AEP_INIT_FAILURE 132
10139N/Adiff -urN -x /home/sbade/dontdirr openssl-engine-0.9.6e.base/crypto/engine/engine_err.c openssl-engine-0.9.6e.patch/crypto/engine/engine_err.c
10139N/A--- openssl-engine-0.9.6e.base/crypto/engine/engine_err.c Thu Feb 7 23:04:27 2002
10139N/A+++ openssl-engine-0.9.6e.patch/crypto/engine/engine_err.c Fri Aug 2 14:37:08 2002
10139N/A@@ -177,6 +177,12 @@
10139N/A {ERR_PACK(0,ENGINE_F_UBSEC_RNG_BYTES,0), "UBSEC_RNG_BYTES"},
10139N/A {ERR_PACK(0,ENGINE_F_UBSEC_RSA_MOD_EXP,0), "UBSEC_RSA_MOD_EXP"},
10139N/A {ERR_PACK(0,ENGINE_F_UBSEC_RSA_MOD_EXP_CRT,0), "UBSEC_RSA_MOD_EXP_CRT"},
10139N/A+{ERR_PACK(0,ENGINE_F_IBMCA_FINISH,0), "IBMCA_FINISH"},
10139N/A+{ERR_PACK(0,ENGINE_F_IBMCA_INIT,0), "IBMCA_INIT"},
10139N/A+{ERR_PACK(0,ENGINE_F_IBMCA_MOD_EXP,0), "IBMCA_MOD_EXP"},
10139N/A+{ERR_PACK(0,ENGINE_F_IBMCA_MOD_EXP_CRT,0), "IBMCA_MOD_EXP_CRT"},
10139N/A+{ERR_PACK(0,ENGINE_F_IBMCA_RAND_BYTES,0), "IBMCA_RAND_BYTES"},
10139N/A+{ERR_PACK(0,ENGINE_F_IBMCA_RSA_MOD_EXP,0), "IBMCA_RSA_MOD_EXP"},
10139N/A {0,NULL}
10139N/A };
10139N/A
10139N/Adiff -urN -x /home/sbade/dontdirr openssl-engine-0.9.6e.base/crypto/engine/engine_int.h openssl-engine-0.9.6e.patch/crypto/engine/engine_int.h
10139N/A--- openssl-engine-0.9.6e.base/crypto/engine/engine_int.h Sat Nov 17 06:28:05 2001
10139N/A+++ openssl-engine-0.9.6e.patch/crypto/engine/engine_int.h Fri Aug 2 14:37:08 2002
10139N/A@@ -151,6 +151,11 @@
10139N/A ENGINE *ENGINE_atalla();
10139N/A #endif /* !NO_HW_ATALLA */
10139N/A
10139N/A+#ifndef NO_HW_IBMCA
10139N/A+/* Returns a structure of ibmca methods. */
10139N/A+ENGINE *ENGINE_ibmca();
10139N/A+#endif /* !NO_HW_IBMCA */
10139N/A+
10139N/A #ifndef NO_HW_AEP
10139N/A /* Returns a structure of AEP methods. */
10139N/A ENGINE *ENGINE_aep();
10139N/Adiff -urN -x /home/sbade/dontdirr openssl-engine-0.9.6e.base/crypto/engine/engine_list.c openssl-engine-0.9.6e.patch/crypto/engine/engine_list.c
10139N/A--- openssl-engine-0.9.6e.base/crypto/engine/engine_list.c Sat Nov 17 06:28:07 2001
10139N/A+++ openssl-engine-0.9.6e.patch/crypto/engine/engine_list.c Fri Aug 2 14:37:08 2002
10139N/A@@ -215,6 +215,10 @@
10139N/A return 0;
10139N/A #endif /* !NO_HW_KEYCLIENT */
10139N/A #endif /* !NO_HW */
10139N/A+#ifndef NO_HW_IBMCA
10139N/A+ if(!engine_list_add(ENGINE_ibmca()))
10139N/A+ return 0;
10139N/A+#endif /* !NO_HW_IBMCA */
10139N/A engine_list_flag = 1;
10139N/A return 1;
10139N/A }
10139N/Adiff -urN -x /home/sbade/dontdirr openssl-engine-0.9.6e.base/crypto/engine/hw_ibmca.c openssl-engine-0.9.6e.patch/crypto/engine/hw_ibmca.c
10139N/A--- openssl-engine-0.9.6e.base/crypto/engine/hw_ibmca.c Thu Jan 1 01:00:00 1970
10139N/A+++ openssl-engine-0.9.6e.patch/crypto/engine/hw_ibmca.c Fri Aug 2 14:38:02 2002
10139N/A@@ -0,0 +1,814 @@
10139N/A+/* crypto/engine/hw_ibmca.c */
10139N/A+/* Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL
10139N/A+ * project 2000.
10139N/A+ */
10139N/A+/* ====================================================================
10139N/A+ * Copyright (c) 1999 The OpenSSL Project. All rights reserved.
10139N/A+ *
10139N/A+ * Redistribution and use in source and binary forms, with or without
10139N/A+ * modification, are permitted provided that the following conditions
10139N/A+ * are met:
10139N/A+ *
10139N/A+ * 1. Redistributions of source code must retain the above copyright
10139N/A+ * notice, this list of conditions and the following disclaimer.
10139N/A+ *
10139N/A+ * 2. Redistributions in binary form must reproduce the above copyright
10139N/A+ * notice, this list of conditions and the following disclaimer in
10139N/A+ * the documentation and/or other materials provided with the
10139N/A+ * distribution.
10139N/A+ *
10139N/A+ * 3. All advertising materials mentioning features or use of this
10139N/A+ * software must display the following acknowledgment:
10139N/A+ * "This product includes software developed by the OpenSSL Project
10139N/A+ * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
10139N/A+ *
10139N/A+ * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
10139N/A+ * endorse or promote products derived from this software without
10139N/A+ * prior written permission. For written permission, please contact
10139N/A+ * licensing@OpenSSL.org.
10139N/A+ *
10139N/A+ * 5. Products derived from this software may not be called "OpenSSL"
10139N/A+ * nor may "OpenSSL" appear in their names without prior written
10139N/A+ * permission of the OpenSSL Project.
10139N/A+ *
10139N/A+ * 6. Redistributions of any form whatsoever must retain the following
10139N/A+ * acknowledgment:
10139N/A+ * "This product includes software developed by the OpenSSL Project
10139N/A+ * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
10139N/A+ *
10139N/A+ * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
10139N/A+ * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
10139N/A+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
10139N/A+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
10139N/A+ * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
10139N/A+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
10139N/A+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
10139N/A+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
10139N/A+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
10139N/A+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
10139N/A+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
10139N/A+ * OF THE POSSIBILITY OF SUCH DAMAGE.
10139N/A+ * ====================================================================
10139N/A+ *
10139N/A+ * This product includes cryptographic software written by Eric Young
10139N/A+ * (eay@cryptsoft.com). This product includes software written by Tim
10139N/A+ * Hudson (tjh@cryptsoft.com).
10139N/A+ *
10139N/A+ */
10139N/A+
10139N/A+/* (C) COPYRIGHT International Business Machines Corp. 2001 */
10139N/A+
10139N/A+#include <stdio.h>
10139N/A+#include <openssl/crypto.h>
10139N/A+#include "cryptlib.h"
10139N/A+#include <openssl/dso.h>
10139N/A+#include "engine_int.h"
10139N/A+#include <openssl/engine.h>
10139N/A+
10139N/A+#ifndef NO_HW
10139N/A+#ifndef NO_HW_IBMCA
10139N/A+
10139N/A+#ifdef FLAT_INC
10139N/A+#include "ica_openssl_api.h"
10139N/A+#else
10139N/A+#include "vendor_defns/ica_openssl_api.h"
10139N/A+#endif
10139N/A+
10139N/A+static const char *IBMCA_F1 = "icaOpenAdapter";
10139N/A+static const char *IBMCA_F2 = "icaCloseAdapter";
10139N/A+static const char *IBMCA_F3 = "icaRsaModExpo";
10139N/A+static const char *IBMCA_F4 = "icaRandomNumberGenerate";
10139N/A+static const char *IBMCA_F5 = "icaRsaCrt";
10139N/A+
10139N/A+ICA_ADAPTER_HANDLE handle=0;
10139N/A+
10139N/A+static int ibmca_init(void);
10139N/A+static int ibmca_finish(void);
10139N/A+
10139N/A+/* BIGNUM stuff */
10139N/A+static int ibmca_mod_exp(BIGNUM *r, BIGNUM *a, const BIGNUM *p,
10139N/A+ const BIGNUM *m, BN_CTX *ctx);
10139N/A+
10139N/A+static int ibmca_mod_exp_crt(BIGNUM *r, BIGNUM *a, const BIGNUM *p,
10139N/A+ const BIGNUM *q, const BIGNUM *dmp1, const BIGNUM *dmq1,
10139N/A+ const BIGNUM *iqmp, BN_CTX *ctx);
10139N/A+
10139N/A+/* RSA stuff */
10139N/A+static int ibmca_rsa_mod_exp(BIGNUM *r0, BIGNUM *I, RSA *rsa);
10139N/A+/* This function is aliased to mod_exp (with the mont stuff dropped). */
10139N/A+static int ibmca_mod_exp_mont(BIGNUM *r, BIGNUM *a, const BIGNUM *p,
10139N/A+ const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
10139N/A+
10139N/A+/* DSA stuff */
10139N/A+static int ibmca_dsa_mod_exp(DSA *dsa, BIGNUM *rr, BIGNUM *a1,
10139N/A+ BIGNUM *p1, BIGNUM *a2, BIGNUM *p2, BIGNUM *m,
10139N/A+ BN_CTX *ctx, BN_MONT_CTX *in_mont);
10139N/A+static int ibmca_mod_exp_dsa(DSA *dsa, BIGNUM *r, BIGNUM *a,
10139N/A+ const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
10139N/A+ BN_MONT_CTX *m_ctx);
10139N/A+
10139N/A+/* DH stuff */
10139N/A+/* This function is alised to mod_exp (with the DH and mont dropped). */
10139N/A+static int ibmca_mod_exp_dh(DH *dh, BIGNUM *r, BIGNUM *a, const BIGNUM *p,
10139N/A+ const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
10139N/A+
10139N/A+/* RAND stuff */
10139N/A+static int ibmca_rand_bytes(unsigned char *buf, int num);
10139N/A+static int ibmca_rand_status(void);
10139N/A+
10139N/A+/* Our internal RSA_METHOD that we provide pointers to */
10139N/A+static RSA_METHOD ibmca_rsa =
10139N/A+ {
10139N/A+ "Ibmca RSA method",
10139N/A+ NULL,
10139N/A+ NULL,
10139N/A+ NULL,
10139N/A+ NULL,
10139N/A+ ibmca_rsa_mod_exp,
10139N/A+ ibmca_mod_exp_mont,
10139N/A+ NULL,
10139N/A+ NULL,
10139N/A+ 0,
10139N/A+ NULL,
10139N/A+ NULL,
10139N/A+ NULL
10139N/A+ };
10139N/A+
10139N/A+/* Our internal DSA_METHOD that we provide pointers to */
10139N/A+static DSA_METHOD ibmca_dsa =
10139N/A+ {
10139N/A+ "Ibmca DSA method",
10139N/A+ NULL, /* dsa_do_sign */
10139N/A+ NULL, /* dsa_sign_setup */
10139N/A+ NULL, /* dsa_do_verify */
10139N/A+ ibmca_dsa_mod_exp, /* dsa_mod_exp */
10139N/A+ ibmca_mod_exp_dsa, /* bn_mod_exp */
10139N/A+ NULL, /* init */
10139N/A+ NULL, /* finish */
10139N/A+ 0, /* flags */
10139N/A+ NULL /* app_data */
10139N/A+ };
10139N/A+
10139N/A+/* Our internal DH_METHOD that we provide pointers to */
10139N/A+static DH_METHOD ibmca_dh =
10139N/A+ {
10139N/A+ "Ibmca DH method",
10139N/A+ NULL,
10139N/A+ NULL,
10139N/A+ ibmca_mod_exp_dh,
10139N/A+ NULL,
10139N/A+ NULL,
10139N/A+ 0,
10139N/A+ NULL
10139N/A+ };
10139N/A+
10139N/A+static RAND_METHOD ibmca_rand =
10139N/A+ {
10139N/A+ /* "IBMCA RAND method", */
10139N/A+ NULL,
10139N/A+ ibmca_rand_bytes,
10139N/A+ NULL,
10139N/A+ NULL,
10139N/A+ ibmca_rand_bytes,
10139N/A+ ibmca_rand_status,
10139N/A+ };
10139N/A+
10139N/A+/* Our ENGINE structure. */
10139N/A+static ENGINE engine_ibmca =
10139N/A+ {
10139N/A+ "ibmca",
10139N/A+ "Ibmca hardware engine support",
10139N/A+ &ibmca_rsa,
10139N/A+ &ibmca_dsa,
10139N/A+ &ibmca_dh,
10139N/A+ &ibmca_rand,
10139N/A+ ibmca_mod_exp,
10139N/A+ NULL,
10139N/A+ ibmca_init,
10139N/A+ ibmca_finish,
10139N/A+ NULL, /* no ctrl() */
10139N/A+ NULL, /* no load_privkey() */
10139N/A+ NULL, /* no load_pubkey() */
10139N/A+ 0, /* no flags */
10139N/A+ 0, 0, /* no references */
10139N/A+ NULL, NULL /* unlinked */
10139N/A+ };
10139N/A+
10139N/A+/* As this is only ever called once, there's no need for locking
10139N/A+ * (indeed - the lock will already be held by our caller!!!) */
10139N/A+ENGINE *ENGINE_ibmca()
10139N/A+ {
10139N/A+ RSA_METHOD *meth1;
10139N/A+ DSA_METHOD *meth2;
10139N/A+ DH_METHOD *meth3;
10139N/A+
10139N/A+ /* We know that the "PKCS1_SSLeay()" functions hook properly
10139N/A+ * to the ibmca-specific mod_exp and mod_exp_crt so we use
10139N/A+ * those functions. NB: We don't use ENGINE_openssl() or
10139N/A+ * anything "more generic" because something like the RSAref
10139N/A+ * code may not hook properly, and if you own one of these
10139N/A+ * cards then you have the right to do RSA operations on it
10139N/A+ * anyway! */
10139N/A+ meth1 = RSA_PKCS1_SSLeay();
10139N/A+ ibmca_rsa.rsa_pub_enc = meth1->rsa_pub_enc;
10139N/A+ ibmca_rsa.rsa_pub_dec = meth1->rsa_pub_dec;
10139N/A+ ibmca_rsa.rsa_priv_enc = meth1->rsa_priv_enc;
10139N/A+ ibmca_rsa.rsa_priv_dec = meth1->rsa_priv_dec;
10139N/A+
10139N/A+ /* Use the DSA_OpenSSL() method and just hook the mod_exp-ish
10139N/A+ * bits. */
10139N/A+ meth2 = DSA_OpenSSL();
10139N/A+ ibmca_dsa.dsa_do_sign = meth2->dsa_do_sign;
10139N/A+ ibmca_dsa.dsa_sign_setup = meth2->dsa_sign_setup;
10139N/A+ ibmca_dsa.dsa_do_verify = meth2->dsa_do_verify;
10139N/A+
10139N/A+ /* Much the same for Diffie-Hellman */
10139N/A+ meth3 = DH_OpenSSL();
10139N/A+ ibmca_dh.generate_key = meth3->generate_key;
10139N/A+ ibmca_dh.compute_key = meth3->compute_key;
10139N/A+ return &engine_ibmca;
10139N/A+ }
10139N/A+
10139N/A+/* This is a process-global DSO handle used for loading and unloading
10139N/A+ * the Ibmca library. NB: This is only set (or unset) during an
10139N/A+ * init() or finish() call (reference counts permitting) and they're
10139N/A+ * operating with global locks, so this should be thread-safe
10139N/A+ * implicitly. */
10139N/A+
10139N/A+static DSO *ibmca_dso = NULL;
10139N/A+
10139N/A+/* These are the function pointers that are (un)set when the library has
10139N/A+ * successfully (un)loaded. */
10139N/A+
10139N/A+static unsigned int (ICA_CALL *p_icaOpenAdapter)();
10139N/A+static unsigned int (ICA_CALL *p_icaCloseAdapter)();
10139N/A+static unsigned int (ICA_CALL *p_icaRsaModExpo)();
10139N/A+static unsigned int (ICA_CALL *p_icaRandomNumberGenerate)();
10139N/A+static unsigned int (ICA_CALL *p_icaRsaCrt)();
10139N/A+
10139N/A+/* utility function to obtain a context */
10139N/A+static int get_context(ICA_ADAPTER_HANDLE *p_handle)
10139N/A+ {
10139N/A+ unsigned int status=0;
10139N/A+
10139N/A+ status = p_icaOpenAdapter(0, p_handle);
10139N/A+ if(status != 0)
10139N/A+ return 0;
10139N/A+ return 1;
10139N/A+ }
10139N/A+
10139N/A+/* similarly to release one. */
10139N/A+static void release_context(ICA_ADAPTER_HANDLE handle)
10139N/A+ {
10139N/A+ p_icaCloseAdapter(handle);
10139N/A+ }
10139N/A+
10139N/A+/* (de)initialisation functions. */
10139N/A+static int ibmca_init()
10139N/A+ {
10139N/A+
10139N/A+ void (*p1)();
10139N/A+ void (*p2)();
10139N/A+ void (*p3)();
10139N/A+ void (*p4)();
10139N/A+ void (*p5)();
10139N/A+
10139N/A+ if(ibmca_dso != NULL)
10139N/A+ {
10139N/A+ ENGINEerr(ENGINE_F_IBMCA_INIT,ENGINE_R_ALREADY_LOADED);
10139N/A+ goto err;
10139N/A+ }
10139N/A+ /* Attempt to load libatasi.so/atasi.dll/whatever. Needs to be
10139N/A+ * changed unfortunately because the Ibmca drivers don't have
10139N/A+ * standard library names that can be platform-translated well. */
10139N/A+ /* TODO: Work out how to actually map to the names the Ibmca
10139N/A+ * drivers really use - for now a symbollic link needs to be
10139N/A+ * created on the host system from libatasi.so to atasi.so on
10139N/A+ * unix variants. */
10139N/A+
10139N/A+ ibmca_dso = DSO_load(NULL, IBMCA_LIBNAME, NULL,
10139N/A+ DSO_FLAG_NAME_TRANSLATION);
10139N/A+ if(ibmca_dso == NULL)
10139N/A+ {
10139N/A+ ENGINEerr(ENGINE_F_IBMCA_INIT,ENGINE_R_DSO_FAILURE);
10139N/A+ goto err;
10139N/A+ }
10139N/A+
10139N/A+ if(!(p1 = DSO_bind_func(
10139N/A+ ibmca_dso, IBMCA_F1)) ||
10139N/A+ !(p2 = DSO_bind_func(
10139N/A+ ibmca_dso, IBMCA_F2)) ||
10139N/A+ !(p3 = DSO_bind_func(
10139N/A+ ibmca_dso, IBMCA_F3)) ||
10139N/A+ !(p4 = DSO_bind_func(
10139N/A+ ibmca_dso, IBMCA_F4)) ||
10139N/A+ !(p5 = DSO_bind_func(
10139N/A+ ibmca_dso, IBMCA_F5)))
10139N/A+ {
10139N/A+ ENGINEerr(ENGINE_F_IBMCA_INIT,ENGINE_R_DSO_FAILURE);
10139N/A+ goto err;
10139N/A+ }
10139N/A+
10139N/A+ /* Copy the pointers */
10139N/A+
10139N/A+ p_icaOpenAdapter = (unsigned int (ICA_CALL *)())p1;
10139N/A+ p_icaCloseAdapter = (unsigned int (ICA_CALL *)())p2;
10139N/A+ p_icaRsaModExpo = (unsigned int (ICA_CALL *)())p3;
10139N/A+ p_icaRandomNumberGenerate = (unsigned int (ICA_CALL *)())p4;
10139N/A+ p_icaRsaCrt = (unsigned int (ICA_CALL *)())p5;
10139N/A+
10139N/A+ if(!get_context(&handle))
10139N/A+ {
10139N/A+ ENGINEerr(ENGINE_F_IBMCA_INIT,ENGINE_R_UNIT_FAILURE);
10139N/A+ goto err;
10139N/A+ }
10139N/A+
10139N/A+ return 1;
10139N/A+ err:
10139N/A+ if(ibmca_dso)
10139N/A+ DSO_free(ibmca_dso);
10139N/A+
10139N/A+ p_icaOpenAdapter = NULL;
10139N/A+ p_icaCloseAdapter = NULL;
10139N/A+ p_icaRsaModExpo = NULL;
10139N/A+ p_icaRandomNumberGenerate = NULL;
10139N/A+
10139N/A+ return 0;
10139N/A+ }
10139N/A+
10139N/A+static int ibmca_finish()
10139N/A+ {
10139N/A+ if(ibmca_dso == NULL)
10139N/A+ {
10139N/A+ ENGINEerr(ENGINE_F_IBMCA_FINISH,ENGINE_R_NOT_LOADED);
10139N/A+ return 0;
10139N/A+ }
10139N/A+ release_context(handle);
10139N/A+ if(!DSO_free(ibmca_dso))
10139N/A+ {
10139N/A+ ENGINEerr(ENGINE_F_IBMCA_FINISH,ENGINE_R_DSO_FAILURE);
10139N/A+ return 0;
10139N/A+ }
10139N/A+ ibmca_dso = NULL;
10139N/A+
10139N/A+ return 1;
10139N/A+ }
10139N/A+
10139N/A+static int ibmca_mod_exp(BIGNUM *r, BIGNUM *a, const BIGNUM *p,
10139N/A+ const BIGNUM *m, BN_CTX *ctx)
10139N/A+ {
10139N/A+ /* I need somewhere to store temporary serialised values for
10139N/A+ * use with the Ibmca API calls. A neat cheat - I'll use
10139N/A+ * BIGNUMs from the BN_CTX but access their arrays directly as
10139N/A+ * byte arrays <grin>. This way I don't have to clean anything
10139N/A+ * up. */
10139N/A+
10139N/A+ BIGNUM *argument=NULL;
10139N/A+ BIGNUM *result=NULL;
10139N/A+ BIGNUM *key=NULL;
10139N/A+ int to_return;
10139N/A+ int inLen, outLen, tmpLen;
10139N/A+
10139N/A+
10139N/A+ ICA_KEY_RSA_MODEXPO *publKey=NULL;
10139N/A+ unsigned int rc;
10139N/A+
10139N/A+ to_return = 0; /* expect failure */
10139N/A+
10139N/A+ if(!ibmca_dso)
10139N/A+ {
10139N/A+ ENGINEerr(ENGINE_F_IBMCA_MOD_EXP,ENGINE_R_NOT_LOADED);
10139N/A+ goto err;
10139N/A+ }
10139N/A+ /* Prepare the params */
10139N/A+ argument = BN_CTX_get(ctx);
10139N/A+ result = BN_CTX_get(ctx);
10139N/A+ key = BN_CTX_get(ctx);
10139N/A+
10139N/A+ if( !argument || !result || !key)
10139N/A+ {
10139N/A+ ENGINEerr(ENGINE_F_IBMCA_MOD_EXP,ENGINE_R_BN_CTX_FULL);
10139N/A+ goto err;
10139N/A+ }
10139N/A+
10139N/A+
10139N/A+ if(!bn_wexpand(argument, m->top) || !bn_wexpand(result, m->top) ||
10139N/A+ !bn_wexpand(key, sizeof(*publKey)/BN_BYTES))
10139N/A+
10139N/A+ {
10139N/A+ ENGINEerr(ENGINE_F_IBMCA_MOD_EXP,ENGINE_R_BN_EXPAND_FAIL);
10139N/A+ goto err;
10139N/A+ }
10139N/A+
10139N/A+ publKey = (ICA_KEY_RSA_MODEXPO *)key->d;
10139N/A+
10139N/A+ if (publKey == NULL)
10139N/A+ {
10139N/A+ goto err;
10139N/A+ }
10139N/A+ memset(publKey, 0, sizeof(ICA_KEY_RSA_MODEXPO));
10139N/A+
10139N/A+ publKey->keyType = CORRECT_ENDIANNESS(ME_KEY_TYPE);
10139N/A+ publKey->keyLength = CORRECT_ENDIANNESS(sizeof(ICA_KEY_RSA_MODEXPO));
10139N/A+ publKey->expOffset = (char *) publKey->keyRecord - (char *) publKey;
10139N/A+
10139N/A+ /* A quirk of the card: the exponent length has to be the same
10139N/A+ as the modulus (key) length */
10139N/A+
10139N/A+ outLen = BN_num_bytes(m);
10139N/A+
10139N/A+ /* Check for a modulus greater than 2048 bits (256 bytes) */
10139N/A+ if ( outLen > 256 ) {
10139N/A+ ENGINEerr(ENGINE_F_IBMCA_MOD_EXP,ENGINE_R_SIZE_TOO_LARGE_OR_TOO_SMALL);
10139N/A+ goto err;
10139N/A+ }
10139N/A+ /* SAB End check for modulus length */
10139N/A+
10139N/A+
10139N/A+ publKey->expLength = publKey->nLength = outLen;
10139N/A+
10139N/A+ /* SAB Check for underflow condition here */
10139N/A+ /* if the size of the exponent length is less than the size of the parameter p,
10139N/A+ * then we have a big problem and we will underlfow the keyRecord buffer
10139N/A+ * who knows what will happen then
10139N/A+ * */
10139N/A+ if ( outLen < BN_num_bytes(p) ) {
10139N/A+ ENGINEerr(ENGINE_F_IBMCA_MOD_EXP,ENGINE_R_SIZE_TOO_LARGE_OR_TOO_SMALL);
10139N/A+ goto err;
10139N/A+ }
10139N/A+ /* SAB End Check for underflow */
10139N/A+
10139N/A+
10139N/A+ BN_bn2bin(p, &publKey->keyRecord[publKey->expLength -
10139N/A+ BN_num_bytes(p)]);
10139N/A+ BN_bn2bin(m, &publKey->keyRecord[publKey->expLength]);
10139N/A+
10139N/A+
10139N/A+
10139N/A+ publKey->modulusBitLength = CORRECT_ENDIANNESS(publKey->nLength * 8);
10139N/A+ publKey->nOffset = CORRECT_ENDIANNESS(publKey->expOffset +
10139N/A+ publKey->expLength);
10139N/A+
10139N/A+ publKey->expOffset = CORRECT_ENDIANNESS((char *) publKey->keyRecord -
10139N/A+ (char *) publKey);
10139N/A+
10139N/A+
10139N/A+ tmpLen = outLen;
10139N/A+
10139N/A+
10139N/A+
10139N/A+ publKey->expLength = publKey->nLength = CORRECT_ENDIANNESS(tmpLen);
10139N/A+
10139N/A+ /* Prepare the argument */
10139N/A+
10139N/A+ memset(argument->d, 0, outLen);
10139N/A+ BN_bn2bin(a, (unsigned char *)argument->d + outLen -
10139N/A+ BN_num_bytes(a));
10139N/A+
10139N/A+ inLen = outLen;
10139N/A+
10139N/A+ /* Perform the operation */
10139N/A+
10139N/A+ if( (rc = p_icaRsaModExpo(handle, inLen,(unsigned char *)argument->d,
10139N/A+ publKey, &outLen, (unsigned char *)result->d))
10139N/A+ !=0 )
10139N/A+
10139N/A+ {
10139N/A+ ENGINEerr(ENGINE_F_IBMCA_MOD_EXP,ENGINE_R_REQUEST_FAILED);
10139N/A+ goto err;
10139N/A+ }
10139N/A+
10139N/A+
10139N/A+ /* Convert the response */
10139N/A+ BN_bin2bn((unsigned char *)result->d, outLen, r);
10139N/A+ to_return = 1;
10139N/A+ err:
10139N/A+ if(argument) ctx->tos--;
10139N/A+ if(result) ctx->tos--;
10139N/A+ if(key) ctx->tos--;
10139N/A+
10139N/A+ return to_return;
10139N/A+ }
10139N/A+
10139N/A+static int ibmca_rsa_mod_exp(BIGNUM *r0, BIGNUM *I, RSA *rsa)
10139N/A+ {
10139N/A+ BN_CTX *ctx;
10139N/A+ int to_return = 0;
10139N/A+
10139N/A+ if((ctx = BN_CTX_new()) == NULL)
10139N/A+ goto err;
10139N/A+ if(!rsa->p || !rsa->q || !rsa->dmp1 || !rsa->dmq1 || !rsa->iqmp)
10139N/A+ {
10139N/A+ if(!rsa->d || !rsa->n)
10139N/A+ {
10139N/A+ ENGINEerr(ENGINE_F_IBMCA_RSA_MOD_EXP,
10139N/A+ ENGINE_R_MISSING_KEY_COMPONENTS);
10139N/A+ goto err;
10139N/A+ }
10139N/A+ to_return = ibmca_mod_exp(r0, I, rsa->d, rsa->n, ctx);
10139N/A+ }
10139N/A+ else
10139N/A+ {
10139N/A+ to_return = ibmca_mod_exp_crt(r0, I, rsa->p, rsa->q, rsa->dmp1,
10139N/A+ rsa->dmq1, rsa->iqmp, ctx);
10139N/A+ }
10139N/A+ err:
10139N/A+ if(ctx)
10139N/A+ BN_CTX_free(ctx);
10139N/A+ return to_return;
10139N/A+ }
10139N/A+
10139N/A+/* Ein kleines chinesisches "Restessen" */
10139N/A+static int ibmca_mod_exp_crt(BIGNUM *r, BIGNUM *a, const BIGNUM *p,
10139N/A+ const BIGNUM *q, const BIGNUM *dmp1,
10139N/A+ const BIGNUM *dmq1, const BIGNUM *iqmp, BN_CTX *ctx)
10139N/A+ {
10139N/A+
10139N/A+ BIGNUM *argument = NULL;
10139N/A+ BIGNUM *result = NULL;
10139N/A+ BIGNUM *key = NULL;
10139N/A+
10139N/A+ unsigned int keyRecordSize;
10139N/A+
10139N/A+ int to_return = 0; /* expect failure */
10139N/A+
10139N/A+ char *pkey=NULL;
10139N/A+ ICA_KEY_RSA_CRT *privKey=NULL;
10139N/A+ int inLen, outLen;
10139N/A+
10139N/A+ int rc;
10139N/A+ unsigned int offset, pSize, qSize;
10139N/A+
10139N/A+ /* Prepare the params */
10139N/A+
10139N/A+ argument = BN_CTX_get(ctx);
10139N/A+ result = BN_CTX_get(ctx);
10139N/A+ key = BN_CTX_get(ctx);
10139N/A+
10139N/A+ if(!argument || !result || !key)
10139N/A+ {
10139N/A+ ENGINEerr(ENGINE_F_IBMCA_MOD_EXP_CRT,ENGINE_R_BN_CTX_FULL);
10139N/A+ goto err;
10139N/A+ }
10139N/A+
10139N/A+ if(!bn_wexpand(argument, p->top + q->top) ||
10139N/A+ !bn_wexpand(result, p->top + q->top) ||
10139N/A+ !bn_wexpand(key, sizeof(*privKey)/BN_BYTES ))
10139N/A+ {
10139N/A+ ENGINEerr(ENGINE_F_IBMCA_MOD_EXP_CRT,ENGINE_R_BN_EXPAND_FAIL);
10139N/A+ goto err;
10139N/A+ }
10139N/A+
10139N/A+ privKey = (ICA_KEY_RSA_CRT *)key->d;
10139N/A+ memset(privKey, 0, sizeof(ICA_KEY_RSA_CRT));
10139N/A+
10139N/A+ /* SAB Add, check that the total size in bytes of the parameters does not
10139N/A+ * exceede the buffer space we have
10139N/A+ * do this first because if it exceeds the
10139N/A+ */
10139N/A+ keyRecordSize = BN_num_bytes(p) + BN_num_bytes(q) + BN_num_bytes(dmp1) +
10139N/A+ BN_num_bytes(dmq1) + + BN_num_bytes(iqmp);
10139N/A+ // DELETE BN_num_bytes(dmq1) + BN_num_bytes(q) + BN_num_bytes(iqmp);
10139N/A+
10139N/A+ if (keyRecordSize > sizeof(privKey->keyRecord)) {
10139N/A+ ENGINEerr(ENGINE_F_IBMCA_MOD_EXP_CRT,ENGINE_R_SIZE_TOO_LARGE_OR_TOO_SMALL);
10139N/A+ goto err;
10139N/A+ }
10139N/A+
10139N/A+ /* SAB add, check that the size of the split of q are not greater than 2048 (256 bytes) */
10139N/A+ if ( (BN_num_bytes(q) + BN_num_bytes(dmq1) ) > 256 ) {
10139N/A+ ENGINEerr(ENGINE_F_IBMCA_MOD_EXP_CRT,ENGINE_R_SIZE_TOO_LARGE_OR_TOO_SMALL);
10139N/A+ goto err;
10139N/A+ }
10139N/A+
10139N/A+ /* SAB add, check that the size of the parts of P are not greater than 2048 (256 bytes) */
10139N/A+ if ( (BN_num_bytes(p) + BN_num_bytes(dmp1) ) > 256 ) {
10139N/A+ ENGINEerr(ENGINE_F_IBMCA_MOD_EXP_CRT,ENGINE_R_SIZE_TOO_LARGE_OR_TOO_SMALL);
10139N/A+ goto err;
10139N/A+ }
10139N/A+
10139N/A+
10139N/A+ privKey->keyType = CORRECT_ENDIANNESS(CRT_KEY_TYPE);
10139N/A+ privKey->keyLength = CORRECT_ENDIANNESS(sizeof(ICA_KEY_RSA_CRT));
10139N/A+ privKey->modulusBitLength = CORRECT_ENDIANNESS(BN_num_bytes(q) * 2 * 8);
10139N/A+
10139N/A+ /*
10139N/A+ * p,dp & qInv are 1 QWORD Larger
10139N/A+ */
10139N/A+ privKey->pLength = CORRECT_ENDIANNESS(BN_num_bytes(p)+8);
10139N/A+ privKey->qLength = CORRECT_ENDIANNESS(BN_num_bytes(q));
10139N/A+ privKey->dpLength = CORRECT_ENDIANNESS(BN_num_bytes(dmp1)+8);
10139N/A+ privKey->dqLength = CORRECT_ENDIANNESS(BN_num_bytes(dmq1));
10139N/A+ privKey->qInvLength = CORRECT_ENDIANNESS(BN_num_bytes(iqmp)+8);
10139N/A+
10139N/A+ offset = (char *) privKey->keyRecord
10139N/A+ - (char *) privKey;
10139N/A+
10139N/A+ qSize = BN_num_bytes(q);
10139N/A+ pSize = qSize + 8; /* 1 QWORD larger */
10139N/A+
10139N/A+/* SAB probably a little redundant, but we'll verify that each of the
10139N/A+ components which make up a keyrecord sent to the card does not
10139N/A+ exceed the space that is allocate for it. This handles the
10139N/A+ case where even if the total length does not exceed keyrecord size, if
10139N/A+ the operands are funny sized causing potential side affects to either
10139N/A+ the card or result */
10139N/A+
10139N/A+ if ( ((BN_num_bytes(p)) > pSize ) ||
10139N/A+ ((BN_num_bytes(dmp1)) > pSize ) ||
10139N/A+ ((BN_num_bytes(iqmp)) > pSize ) ||
10139N/A+ ((BN_num_bytes(q)) > qSize ) ||
10139N/A+ ((BN_num_bytes(dmq1)) > qSize ) ) {
10139N/A+
10139N/A+ ENGINEerr(ENGINE_F_IBMCA_MOD_EXP_CRT,ENGINE_R_SIZE_TOO_LARGE_OR_TOO_SMALL);
10139N/A+ goto err;
10139N/A+
10139N/A+ }
10139N/A+
10139N/A+
10139N/A+
10139N/A+
10139N/A+ /* these offset values are ignored on all platforms except Intel
10139N/A+ * in libica
10139N/A+ * */
10139N/A+ privKey->dpOffset = CORRECT_ENDIANNESS(offset);
10139N/A+
10139N/A+ offset += pSize;
10139N/A+ privKey->dqOffset = CORRECT_ENDIANNESS(offset);
10139N/A+
10139N/A+ offset += qSize;
10139N/A+ privKey->pOffset = CORRECT_ENDIANNESS(offset);
10139N/A+
10139N/A+ offset += pSize;
10139N/A+ privKey->qOffset = CORRECT_ENDIANNESS(offset);
10139N/A+
10139N/A+ offset += qSize;
10139N/A+ privKey->qInvOffset = CORRECT_ENDIANNESS(offset);
10139N/A+
10139N/A+ /* Start at the beginning of the buffer. */
10139N/A+ pkey = (char *) privKey->keyRecord;
10139N/A+
10139N/A+ /* SAB First check that we won't underflow the buffer... */
10139N/A+ /* p greater in size than pSize will cause us to underflow */
10139N/A+ /* should really check the other data sizes as well before doing the copies */
10139N/A+ /* no sense doing any copies if the sizes and offsets will create a bad buffer */
10139N/A+ /* although the check for total lengths not exceeding total space should help */
10139N/A+
10139N/A+ if ( pSize < BN_num_bytes(p)){
10139N/A+ ENGINEerr(ENGINE_F_IBMCA_MOD_EXP_CRT,ENGINE_R_SIZE_TOO_LARGE_OR_TOO_SMALL);
10139N/A+ goto err;
10139N/A+ }
10139N/A+
10139N/A+
10139N/A+ /* pkey += pSize - BN_num_bytes(p); this line is wrong SAB this is so that the field is padded with leading zeros */
10139N/A+ pkey += pSize - BN_num_bytes(dmp1); /* SAB this is so that the field is padded with leading zeros */
10139N/A+
10139N/A+ BN_bn2bin(dmp1, pkey);
10139N/A+ pkey += BN_num_bytes(dmp1); /* move the pointer */
10139N/A+
10139N/A+ BN_bn2bin(dmq1, pkey); /* Copy dmq1 */
10139N/A+ pkey += qSize ; /* dmq1 is qSize but somewhere we should really check this */
10139N/A+ pkey += pSize - BN_num_bytes(p); /* the next field is padded with leading zeros */
10139N/A+
10139N/A+ BN_bn2bin(p, pkey);
10139N/A+
10139N/A+ pkey += BN_num_bytes(p); /* move the pointer however many bytes we moved it */
10139N/A+
10139N/A+ BN_bn2bin(q, pkey);
10139N/A+
10139N/A+ pkey += qSize ; /* Move the pointer to the next offset in the buffer */
10139N/A+ pkey += pSize - BN_num_bytes(iqmp); /* Another zero padded field */
10139N/A+ BN_bn2bin(iqmp, pkey);
10139N/A+
10139N/A+ /* Prepare the argument and response */
10139N/A+
10139N/A+ outLen = CORRECT_ENDIANNESS(privKey->qLength) * 2; /* correct endianess is used here because the privKey structures
10139N/A+ fields were converted for endianess above This makes it into
10139N/A+ native */
10139N/A+
10139N/A+ if (outLen > 256 ) {
10139N/A+ ENGINEerr(ENGINE_F_IBMCA_MOD_EXP_CRT,ENGINE_R_SIZE_TOO_LARGE_OR_TOO_SMALL);
10139N/A+ goto err;
10139N/A+ }
10139N/A+
10139N/A+
10139N/A+ /* SAB Need to check for underflow as well as the case where outLen is < the argument which would clobber
10139N/A+ * the prior buffers */
10139N/A+ if ( outLen < BN_num_bytes(a)) {
10139N/A+ ENGINEerr(ENGINE_F_IBMCA_MOD_EXP_CRT,ENGINE_R_BN_EXPAND_FAIL);
10139N/A+ }
10139N/A+
10139N/A+ /* This will pad with leading zeros if the argument is less than the key length this shouldnot really
10139N/A+ * happen, but since the callers really are responsible for the padding, we need to make sure we have it
10139N/A+ * proper */
10139N/A+
10139N/A+ BN_bn2bin(a, (unsigned char *)argument->d + outLen -
10139N/A+ BN_num_bytes(a));
10139N/A+
10139N/A+
10139N/A+ inLen = outLen;
10139N/A+
10139N/A+ memset(result->d, 0, outLen);
10139N/A+
10139N/A+ /* Perform the operation */
10139N/A+
10139N/A+ if ( (rc = p_icaRsaCrt(handle, inLen, (unsigned char *)argument->d,
10139N/A+ privKey, &outLen, (unsigned char *)result->d)) != 0)
10139N/A+ {
10139N/A+ ENGINEerr(ENGINE_F_IBMCA_MOD_EXP_CRT,ENGINE_R_REQUEST_FAILED);
10139N/A+ goto err;
10139N/A+ }
10139N/A+
10139N/A+ /* Convert the response */
10139N/A+
10139N/A+ BN_bin2bn((unsigned char *)result->d, outLen, r);
10139N/A+ to_return = 1;
10139N/A+
10139N/A+ err:
10139N/A+ if(argument) ctx->tos--;
10139N/A+ if(result) ctx->tos--;
10139N/A+ if(key) ctx->tos--;
10139N/A+ return to_return;
10139N/A+
10139N/A+ }
10139N/A+
10139N/A+/* This code was liberated and adapted from the commented-out code in
10139N/A+ * dsa_ossl.c. Because of the unoptimised form of the Ibmca acceleration
10139N/A+ * (it doesn't have a CRT form for RSA), this function means that an
10139N/A+ * Ibmca system running with a DSA server certificate can handshake
10139N/A+ * around 5 or 6 times faster/more than an equivalent system running with
10139N/A+ * RSA. Just check out the "signs" statistics from the RSA and DSA parts
10139N/A+ * of "openssl speed -engine ibmca dsa1024 rsa1024". */
10139N/A+static int ibmca_dsa_mod_exp(DSA *dsa, BIGNUM *rr, BIGNUM *a1,
10139N/A+ BIGNUM *p1, BIGNUM *a2, BIGNUM *p2, BIGNUM *m,
10139N/A+ BN_CTX *ctx, BN_MONT_CTX *in_mont)
10139N/A+ {
10139N/A+ BIGNUM t;
10139N/A+ int to_return = 0;
10139N/A+
10139N/A+ BN_init(&t);
10139N/A+ /* let rr = a1 ^ p1 mod m */
10139N/A+ if (!ibmca_mod_exp(rr,a1,p1,m,ctx)) goto end;
10139N/A+ /* let t = a2 ^ p2 mod m */
10139N/A+ if (!ibmca_mod_exp(&t,a2,p2,m,ctx)) goto end;
10139N/A+ /* let rr = rr * t mod m */
10139N/A+ if (!BN_mod_mul(rr,rr,&t,m,ctx)) goto end;
10139N/A+ to_return = 1;
10139N/A+ end:
10139N/A+ BN_free(&t);
10139N/A+ return to_return;
10139N/A+ }
10139N/A+
10139N/A+
10139N/A+static int ibmca_mod_exp_dsa(DSA *dsa, BIGNUM *r, BIGNUM *a,
10139N/A+ const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
10139N/A+ BN_MONT_CTX *m_ctx)
10139N/A+ {
10139N/A+ return ibmca_mod_exp(r, a, p, m, ctx);
10139N/A+ }
10139N/A+
10139N/A+/* This function is aliased to mod_exp (with the mont stuff dropped). */
10139N/A+static int ibmca_mod_exp_mont(BIGNUM *r, BIGNUM *a, const BIGNUM *p,
10139N/A+ const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
10139N/A+ {
10139N/A+ return ibmca_mod_exp(r, a, p, m, ctx);
10139N/A+ }
10139N/A+
10139N/A+/* This function is aliased to mod_exp (with the dh and mont dropped). */
10139N/A+static int ibmca_mod_exp_dh(DH *dh, BIGNUM *r, BIGNUM *a, const BIGNUM *p,
10139N/A+ const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
10139N/A+ {
10139N/A+ return ibmca_mod_exp(r, a, p, m, ctx);
10139N/A+ }
10139N/A+
10139N/A+/* Random bytes are good */
10139N/A+static int ibmca_rand_bytes(unsigned char *buf, int num)
10139N/A+ {
10139N/A+ int to_return = 0; /* assume failure */
10139N/A+ unsigned int ret;
10139N/A+
10139N/A+
10139N/A+ if(handle == 0)
10139N/A+ {
10139N/A+ ENGINEerr(ENGINE_F_IBMCA_RAND_BYTES,ENGINE_R_NOT_INITIALISED);
10139N/A+ goto err;
10139N/A+ }
10139N/A+
10139N/A+ ret = p_icaRandomNumberGenerate(handle, num, buf);
10139N/A+ if (ret < 0)
10139N/A+ {
10139N/A+ ENGINEerr(ENGINE_F_IBMCA_RAND_BYTES,ENGINE_R_REQUEST_FAILED);
10139N/A+ goto err;
10139N/A+ }
10139N/A+ to_return = 1;
10139N/A+ err:
10139N/A+ return to_return;
10139N/A+ }
10139N/A+
10139N/A+static int ibmca_rand_status(void)
10139N/A+ {
10139N/A+ return 1;
10139N/A+ }
10139N/A+
10139N/A+#endif /* !NO_HW_IBMCA */
10139N/A+#endif /* !NO_HW */
10139N/Adiff -urN -x /home/sbade/dontdirr openssl-engine-0.9.6e.base/crypto/engine/vendor_defns/ica_openssl_api.h openssl-engine-0.9.6e.patch/crypto/engine/vendor_defns/ica_openssl_api.h
10139N/A--- openssl-engine-0.9.6e.base/crypto/engine/vendor_defns/ica_openssl_api.h Thu Jan 1 01:00:00 1970
10139N/A+++ openssl-engine-0.9.6e.patch/crypto/engine/vendor_defns/ica_openssl_api.h Fri Aug 2 14:37:08 2002
10139N/A@@ -0,0 +1,189 @@
10139N/A+
10139N/A+#ifndef __ICA_OPENSSL_API_H__
10139N/A+#define __ICA_OPENSSL_API_H__
10139N/A+
10139N/A+/**
10139N/A+ ** abstract data types for API
10139N/A+ **/
10139N/A+
10139N/A+#define ICA_ADAPTER_HANDLE int
10139N/A+
10139N/A+#if defined(linux) || defined (_AIX) || defined(sun)
10139N/A+#define ICA_CALL
10139N/A+#endif
10139N/A+
10139N/A+#if defined(WIN32) || defined(_WIN32)
10139N/A+#define ICA_CALL __stdcall
10139N/A+#endif
10139N/A+
10139N/A+/*------------------------------------------------*
10139N/A+ | RSA defines and typedefs |
10139N/A+ *------------------------------------------------*/
10139N/A+ /*
10139N/A+ * All data elements of the RSA key are in big-endian format
10139N/A+ * Modulus-Exponent form of key
10139N/A+ *
10139N/A+ */
10139N/A+ #define MAX_EXP_SIZE 256
10139N/A+ #define MAX_MODULUS_SIZE 256
10139N/A+ #define MAX_MODEXP_SIZE (MAX_EXP_SIZE + MAX_MODULUS_SIZE)
10139N/A+
10139N/A+ #define MAX_OPERAND_SIZE MAX_EXP_SIZE
10139N/A+
10139N/A+ typedef unsigned char ICA_KEY_RSA_MODEXPO_REC[MAX_MODEXP_SIZE];
10139N/A+ /*
10139N/A+ * All data elements of the RSA key are in big-endian format
10139N/A+ * Chinese Remainder Thereom(CRT) form of key
10139N/A+ * Used only for Decrypt, the encrypt form is typically Modulus-Exponent
10139N/A+ *
10139N/A+ */
10139N/A+ #define MAX_BP_SIZE 136
10139N/A+ #define MAX_BQ_SIZE 128
10139N/A+ #define MAX_NP_SIZE 136
10139N/A+ #define MAX_NQ_SIZE 128
10139N/A+ #define MAX_QINV_SIZE 136
10139N/A+ #define MAX_RSACRT_SIZE (MAX_BP_SIZE+MAX_BQ_SIZE+MAX_NP_SIZE+MAX_NQ_SIZE+MAX_QINV_SIZE)
10139N/A+
10139N/A+#define RSA_GEN_OPERAND_MAX 256 /* bytes */
10139N/A+
10139N/A+typedef unsigned char ICA_KEY_RSA_CRT_REC[MAX_RSACRT_SIZE];
10139N/A+/*------------------------------------------------*
10139N/A+ | RSA key token types |
10139N/A+ *------------------------------------------------*/
10139N/A+
10139N/A+#define RSA_PUBLIC_MODULUS_EXPONENT 3
10139N/A+#define RSA_PKCS_PRIVATE_CHINESE_REMAINDER 6
10139N/A+
10139N/A+#define KEYTYPE_MODEXPO 1
10139N/A+#define KEYTYPE_PKCSCRT 2
10139N/A+
10139N/A+
10139N/A+/*------------------------------------------------*
10139N/A+ | RSA Key Token format |
10139N/A+ *------------------------------------------------*/
10139N/A+
10139N/A+/*
10139N/A+ * NOTE: All the fields in the ICA_KEY_RSA_MODEXPO structure
10139N/A+ * (lengths, offsets, exponents, modulus, etc.) are
10139N/A+ * stored in big-endian format
10139N/A+ */
10139N/A+
10139N/A+typedef struct _ICA_KEY_RSA_MODEXPO
10139N/A+{ unsigned int keyType; /* RSA key type. */
10139N/A+ unsigned int keyLength; /* Total length of the token. */
10139N/A+ unsigned int modulusBitLength; /* Modulus n bit length. */
10139N/A+ /* -- Start of the data length.*/
10139N/A+ unsigned int nLength; /* Modulus n = p * q */
10139N/A+ unsigned int expLength; /* exponent (public or private)*/
10139N/A+ /* e = 1/d * mod(p-1)(q-1) */
10139N/A+ /* -- Start of the data offsets*/
10139N/A+ unsigned int nOffset; /* Modulus n . */
10139N/A+ unsigned int expOffset; /* exponent (public or private)*/
10139N/A+ unsigned char reserved[112]; /* reserved area */
10139N/A+ /* -- Start of the variable -- */
10139N/A+ /* -- length token data. -- */
10139N/A+ ICA_KEY_RSA_MODEXPO_REC keyRecord;
10139N/A+} ICA_KEY_RSA_MODEXPO;
10139N/A+#define SZ_HEADER_MODEXPO (sizeof(ICA_KEY_RSA_MODEXPO) - sizeof(ICA_KEY_RSA_MODEXPO_REC))
10139N/A+
10139N/A+/*
10139N/A+ * NOTE: All the fields in the ICA_KEY_RSA_CRT structure
10139N/A+ * (lengths, offsets, exponents, modulus, etc.) are
10139N/A+ * stored in big-endian format
10139N/A+ */
10139N/A+
10139N/A+typedef struct _ICA_KEY_RSA_CRT
10139N/A+{ unsigned int keyType; /* RSA key type. */
10139N/A+ unsigned int keyLength; /* Total length of the token. */
10139N/A+ unsigned int modulusBitLength; /* Modulus n bit length. */
10139N/A+ /* -- Start of the data length.*/
10139N/A+#if _AIX
10139N/A+ unsigned int nLength; /* Modulus n = p * q */
10139N/A+#endif
10139N/A+ unsigned int pLength; /* Prime number p . */
10139N/A+ unsigned int qLength; /* Prime number q . */
10139N/A+ unsigned int dpLength; /* dp = d * mod(p-1) . */
10139N/A+ unsigned int dqLength; /* dq = d * mod(q-1) . */
10139N/A+ unsigned int qInvLength; /* PKCS: qInv = Ap/q */
10139N/A+ /* -- Start of the data offsets*/
10139N/A+#if _AIX
10139N/A+ unsigned int nOffset; /* Modulus n . */
10139N/A+#endif
10139N/A+ unsigned int pOffset; /* Prime number p . */
10139N/A+ unsigned int qOffset; /* Prime number q . */
10139N/A+ unsigned int dpOffset; /* dp . */
10139N/A+ unsigned int dqOffset; /* dq . */
10139N/A+ unsigned int qInvOffset; /* qInv for PKCS */
10139N/A+#if _AIX
10139N/A+ unsigned char reserved[80]; /* reserved area */
10139N/A+#else
10139N/A+ unsigned char reserved[88]; /* reserved area */
10139N/A+#endif
10139N/A+ /* -- Start of the variable -- */
10139N/A+ /* -- length token data. -- */
10139N/A+ ICA_KEY_RSA_CRT_REC keyRecord;
10139N/A+} ICA_KEY_RSA_CRT;
10139N/A+#define SZ_HEADER_CRT (sizeof(ICA_KEY_RSA_CRT) - sizeof(ICA_KEY_RSA_CRT_REC))
10139N/A+
10139N/A+unsigned int
10139N/A+icaOpenAdapter( unsigned int adapterId,
10139N/A+ ICA_ADAPTER_HANDLE *pAdapterHandle );
10139N/A+
10139N/A+unsigned int
10139N/A+icaCloseAdapter( ICA_ADAPTER_HANDLE adapterHandle );
10139N/A+
10139N/A+unsigned int
10139N/A+icaRsaModExpo( ICA_ADAPTER_HANDLE hAdapterHandle,
10139N/A+ unsigned int inputDataLength,
10139N/A+ unsigned char *pInputData,
10139N/A+ ICA_KEY_RSA_MODEXPO *pKeyModExpo,
10139N/A+ unsigned int *pOutputDataLength,
10139N/A+ unsigned char *pOutputData );
10139N/A+
10139N/A+unsigned int
10139N/A+icaRsaCrt( ICA_ADAPTER_HANDLE hAdapterHandle,
10139N/A+ unsigned int inputDataLength,
10139N/A+ unsigned char *pInputData,
10139N/A+ ICA_KEY_RSA_CRT *pKeyCrt,
10139N/A+ unsigned int *pOutputDataLength,
10139N/A+ unsigned char *pOutputData );
10139N/A+
10139N/A+unsigned int
10139N/A+icaRandomNumberGenerate( ICA_ADAPTER_HANDLE hAdapterHandle,
10139N/A+ unsigned int outputDataLength,
10139N/A+ unsigned char *pOutputData );
10139N/A+
10139N/A+/* Specific macros and definitions to not have IFDEF;s all over the
10139N/A+ main code */
10139N/A+
10139N/A+#if (_AIX)
10139N/A+static const char *IBMCA_LIBNAME = "/lib/libica.a(shr.o)";
10139N/A+#elif (WIN32)
10139N/A+static const char *IBMCA_LIBNAME = "cryptica";
10139N/A+#else
10139N/A+static const char *IBMCA_LIBNAME = "ica";
10139N/A+#endif
10139N/A+
10139N/A+#if (WIN32)
10139N/A+/*
10139N/A+ The ICA_KEY_RSA_MODEXPO & ICA_KEY_RSA_CRT lengths and
10139N/A+ offsets must be in big-endian format.
10139N/A+
10139N/A+*/
10139N/A+#define CORRECT_ENDIANNESS(b) ( \
10139N/A+ (((unsigned long) (b) & 0x000000ff) << 24) | \
10139N/A+ (((unsigned long) (b) & 0x0000ff00) << 8) | \
10139N/A+ (((unsigned long) (b) & 0x00ff0000) >> 8) | \
10139N/A+ (((unsigned long) (b) & 0xff000000) >> 24) \
10139N/A+ )
10139N/A+#define CRT_KEY_TYPE RSA_PKCS_PRIVATE_CHINESE_REMAINDER
10139N/A+#define ME_KEY_TYPE RSA_PUBLIC_MODULUS_EXPONENT
10139N/A+#else
10139N/A+#define CORRECT_ENDIANNESS(b) (b)
10139N/A+#define CRT_KEY_TYPE KEYTYPE_PKCSCRT
10139N/A+#define ME_KEY_TYPE KEYTYPE_MODEXPO
10139N/A+#endif
10139N/A+
10139N/A+
10139N/A+
10139N/A+#endif /* __ICA_OPENSSL_API_H__ */