We've just finished building/testing an engine for openssl for the LunaXL, an SSL accelerator box made by Chrysalis ITS (which is just being released). We would like to release this code to the openssl project for inclusion in the openssl-engine codebase. This patch was generated against openssl-engine-0.9.6a, however works with the new openssl-egine-0.9.6b as well. Thanks, Behan Webster [EMAIL PROTECTED] diff -Naur openssl-engine-0.9.6a.orig/crypto/engine/Makefile openssl-engine-0.9.6a/crypto/engine/Makefile --- openssl-engine-0.9.6a.orig/crypto/engine/Makefile Thu Sep 7 10:57:29 2000 +++ openssl-engine-0.9.6a/crypto/engine/Makefile Tue Jun 19 20:11:48 2001 @@ -23,9 +23,9 @@ LIB=$(TOP)/libcrypto.a LIBSRC= engine_err.c engine_lib.c engine_list.c engine_openssl.c \ - hw_atalla.c hw_cswift.c hw_ncipher.c + hw_atalla.c hw_cswift.c hw_ncipher.c hw_lunaxl.c LIBOBJ= engine_err.o engine_lib.o engine_list.o engine_openssl.o \ - hw_atalla.o hw_cswift.o hw_ncipher.o + hw_atalla.o hw_cswift.o hw_ncipher.o hw_lunaxl.o SRC= $(LIBSRC) diff -Naur openssl-engine-0.9.6a.orig/crypto/engine/Makefile.ssl openssl-engine-0.9.6a/crypto/engine/Makefile.ssl --- openssl-engine-0.9.6a.orig/crypto/engine/Makefile.ssl Thu Sep 7 10:57:29 2000 +++ openssl-engine-0.9.6a/crypto/engine/Makefile.ssl Tue Jun 19 20:11:48 2001 @@ -23,9 +23,9 @@ LIB=$(TOP)/libcrypto.a LIBSRC= engine_err.c engine_lib.c engine_list.c engine_openssl.c \ - hw_atalla.c hw_cswift.c hw_ncipher.c + hw_atalla.c hw_cswift.c hw_ncipher.c hw_lunaxl.c LIBOBJ= engine_err.o engine_lib.o engine_list.o engine_openssl.o \ - hw_atalla.o hw_cswift.o hw_ncipher.o + hw_atalla.o hw_cswift.o hw_ncipher.o hw_lunaxl.o SRC= $(LIBSRC) diff -Naur openssl-engine-0.9.6a.orig/crypto/engine/engine_int.h openssl-engine-0.9.6a/crypto/engine/engine_int.h --- openssl-engine-0.9.6a.orig/crypto/engine/engine_int.h Thu Jul 6 18:39:27 2000 +++ openssl-engine-0.9.6a/crypto/engine/engine_int.h Tue Jun 19 20:11:48 2001 @@ -151,6 +151,11 @@ ENGINE *ENGINE_atalla(); #endif /* !NO_HW_ATALLA */ +#ifndef NO_HW_LUNA_XL +/* Returns a structure of LunaXL methods. */ +ENGINE *ENGINE_lunaxl(); +#endif /* !NO_HW_LUNA_XL */ + #endif /* !NO_HW */ #ifdef __cplusplus diff -Naur openssl-engine-0.9.6a.orig/crypto/engine/engine_list.c openssl-engine-0.9.6a/crypto/engine/engine_list.c --- openssl-engine-0.9.6a.orig/crypto/engine/engine_list.c Fri Jun 30 11:01:56 2000 +++ openssl-engine-0.9.6a/crypto/engine/engine_list.c Tue Jun 19 20:11:48 2001 @@ -198,6 +198,11 @@ if(!engine_list_add(ENGINE_atalla())) return 0; #endif /* !NO_HW_ATALLA */ +#ifndef NO_HW_LUNA_XL + if(!engine_list_add(ENGINE_lunaxl())) + return 0; +#endif /* !NO_HW_LUNA_XL */ + #endif /* !NO_HW */ engine_list_flag = 1; return 1; diff -Naur openssl-engine-0.9.6a.orig/crypto/engine/hw_lunaxl.c openssl-engine-0.9.6a/crypto/engine/hw_lunaxl.c --- openssl-engine-0.9.6a.orig/crypto/engine/hw_lunaxl.c Thu Jan 1 00:00:00 1970 +++ openssl-engine-0.9.6a/crypto/engine/hw_lunaxl.c Thu Jun 21 16:52:32 2001 @@ -0,0 +1,354 @@ +/* Written by Stevan Milinkovic <[EMAIL PROTECTED]>, + * Rouslan Beletski <[EMAIL PROTECTED]>, and * Behan Webster + * <[EMAIL PROTECTED]> for the OpenSSL project 2000. + */ +/* ==================================================================== + * Copyright (c) 1999 The OpenSSL Project. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. All advertising materials mentioning features or use of this + * software must display the following acknowledgment: + * "This product includes software developed by the OpenSSL Project + * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" + * + * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to + * endorse or promote products derived from this software without + * prior written permission. For written permission, please contact + * [EMAIL PROTECTED] + * + * 5. Products derived from this software may not be called "OpenSSL" + * nor may "OpenSSL" appear in their names without prior written + * permission of the OpenSSL Project. + * + * 6. Redistributions of any form whatsoever must retain the following + * acknowledgment: + * "This product includes software developed by the OpenSSL Project + * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" + * + * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY + * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + * ==================================================================== + * + * This product includes cryptographic software written by Eric Young + * ([EMAIL PROTECTED]). This product includes software written by Tim + * Hudson ([EMAIL PROTECTED]). + * + */ + + +#include <stdio.h> +#include <openssl/crypto.h> +#include "cryptlib.h" +#include <openssl/dso.h> +#include "engine_int.h" +#include <openssl/engine.h> + +#define MAX_SLOTS 32 +#ifndef NO_HW +#ifndef NO_HW_LUNA_XL + +#ifdef FLAT_INC +#include "lunaxl.h" +#else +#include "vendor_defns/lunaxl.h" +#endif + + +static DSO *LunaXL_dso = NULL; +static int LunaXL_init(void); +static int LunaXL_finish(void); +static int LunaXL_rsa_priv_enc(int flen, unsigned char *from, unsigned char *to, RSA *rsa, int padding); +static int LunaXL_rsa_priv_dec(int flen, unsigned char *from, unsigned char *to, RSA *rsa, int padding); + +static CK_SESSION_HANDLE hSession[MAX_SLOTS]; +static CK_FLAGS flags = CKF_SERIAL_SESSION | CKF_RW_SESSION; +static CK_MECHANISM mech = {CKM_RSA_PKCS, NULL_PTR, 0}; +static unsigned long ulSlotCount = 1; +int i, assigned_slot = 0; + +static RSA_METHOD LunaXL_rsa = + { + "PKCS#11 RSA method", + NULL, + NULL, + LunaXL_rsa_priv_enc, + LunaXL_rsa_priv_dec, + NULL, + NULL, + NULL, + NULL, + 0, + NULL, + NULL, + NULL + }; + +static ENGINE engine_LunaXL = + { + "LunaXL", + "Chrysalis-ITS Luna XL engine support", + &LunaXL_rsa, + NULL, + NULL, + NULL, + NULL, + NULL, + LunaXL_init, + LunaXL_finish, + NULL, + NULL, + NULL, + 0, + 0, 0, + NULL, NULL + }; + + +ENGINE *ENGINE_lunaxl() { + RSA_METHOD *method; + method = RSA_PKCS1_SSLeay(); + LunaXL_rsa.rsa_pub_enc = method->rsa_pub_enc; + LunaXL_rsa.rsa_pub_dec = method->rsa_pub_dec; + LunaXL_rsa.rsa_mod_exp = method->rsa_mod_exp; + LunaXL_rsa.bn_mod_exp = method->bn_mod_exp; + return &engine_LunaXL; +} + + +/* The following function is for ephemeral RSA key exchange */ +static int LunaXL_E_private_decrypt(int flen, unsigned char *from, unsigned char *to, RSA *rsa, int padding) { + const RSA_METHOD *meth; + BIGNUM f,ret; + int j,num=0,r= -1; + unsigned char *p; + unsigned char *buf=NULL; + BN_CTX *ctx=NULL; + + meth = ENGINE_get_RSA(rsa->engine); + BN_init(&f); + BN_init(&ret); + ctx=BN_CTX_new(); + + if (ctx == NULL) goto err; + num=BN_num_bytes(rsa->n); + if ((buf=(unsigned char *)OPENSSL_malloc(num)) == NULL){ + RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,ERR_R_MALLOC_FAILURE); + goto err; + } + if (flen > num) { + RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,RSA_R_DATA_GREATER_THAN_MOD_LEN); + goto err; + } + +/* convert data into a big number */ + if (BN_bin2bn(from,(int)flen,&f) == NULL) goto err; + if ((rsa->flags & RSA_FLAG_BLINDING) && (rsa->blinding == NULL)) + RSA_blinding_on(rsa,ctx); + if (rsa->flags & RSA_FLAG_BLINDING) + if (!BN_BLINDING_convert(&f,rsa->blinding,ctx)) goto err; + +/* do the decrypt */ + if ( (rsa->flags & RSA_FLAG_EXT_PKEY) || + ((rsa->p != NULL) && + (rsa->q != NULL) && + (rsa->dmp1 != NULL) && + (rsa->dmq1 != NULL) && + (rsa->iqmp != NULL)) ) + { if (!meth->rsa_mod_exp(&ret,&f,rsa)) goto err; } + else { + if (!meth->bn_mod_exp(&ret,&f,rsa->d,rsa->n,ctx,NULL)) + goto err; + } + + if (rsa->flags & RSA_FLAG_BLINDING) + if (!BN_BLINDING_invert(&ret,rsa->blinding,ctx)) goto err; + + p=buf; + j=BN_bn2bin(&ret,p); + + switch (padding) { + case RSA_PKCS1_PADDING: + r=RSA_padding_check_PKCS1_type_2(to,num,buf,j,num); + break; +#ifndef NO_SHA + case RSA_PKCS1_OAEP_PADDING: + r=RSA_padding_check_PKCS1_OAEP(to,num,buf,j,num,NULL,0); + break; +#endif + case RSA_SSLV23_PADDING: + r=RSA_padding_check_SSLv23(to,num,buf,j,num); + break; + case RSA_NO_PADDING: + r=RSA_padding_check_none(to,num,buf,j,num); + break; + default: + RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,RSA_R_UNKNOWN_PADDING_TYPE); + goto err; + } + if (r < 0) + RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,RSA_R_PADDING_CHECK_FAILED); +err: + if (ctx != NULL) BN_CTX_free(ctx); + BN_clear_free(&f); + BN_clear_free(&ret); + if (buf != NULL) { + memset(buf,0,num); + OPENSSL_free(buf); + } + return(r); +} + + +/* Chrystoki API hooks */ +static int LunaXL_init() { + + CK_RV retCode = CKR_OK; + +/* Load Chrystoki libarary */ + if(LunaXL_dso != NULL) { + fprintf(stderr, " Luna XL -> Chrystoki library already loaded.\n"); + return 0; + } + + LunaXL_dso = DSO_load(NULL,"/usr/lib/libcrystoki2.so" , NULL, DSO_FLAG_NAME_TRANSLATION); + if(LunaXL_dso == NULL) { + fprintf(stderr, " Luna XL -> Error loading Chrystoki library.\n"); + return 0; + } + + C_Initialize = (CP_Initialize)DSO_bind_func(LunaXL_dso, "C_Initialize"); + C_Finalize = (CP_Initialize)DSO_bind_func(LunaXL_dso, "C_Finalize"); + C_OpenSession = (CP_OpenSession)DSO_bind_func(LunaXL_dso, "C_OpenSession"); + C_CloseSession = (CP_CloseSession)DSO_bind_func(LunaXL_dso, "C_CloseSession"); + CA_SinglePartDecrypt = (CP_SinglePartDecrypt)DSO_bind_func(LunaXL_dso, "CA_SinglePartDecrypt"); + CA_SinglePartSign = (CP_SinglePartDecrypt)DSO_bind_func(LunaXL_dso, "CA_SinglePartSign"); + CA_GetNumberOfSSLSlots = (CP_GetNumberOfSSLSlots)DSO_bind_func(LunaXL_dso, "CA_GetNumberOfSSLSlots"); + + retCode=C_Initialize(NULL_PTR); + if(retCode !=CKR_OK) { + fprintf(stderr, " Luna XL -> Crystoki Initialize Error: 0x%x.\n", (int)retCode); + goto err; + } + + retCode=CA_GetNumberOfSSLSlots(&ulSlotCount); + if(retCode !=CKR_OK) { + fprintf(stderr, " Luna XL -> Unable to get number of slots: 0x%x.\n", (int)retCode); + goto err; + } + + if(ulSlotCount > MAX_SLOTS) { + fprintf(stderr, " Luna XL -> Maximum number of 32 slots exceeded.\n"); + goto err; + } + +/* Open sessions on all available slots */ + for(i=0; i<(int)ulSlotCount; i++) { + retCode = C_OpenSession((CK_SLOT_ID) (i+1),flags,"Application",0,&hSession[i]); + if(retCode !=CKR_OK) { + fprintf(stderr, " Luna XL -> Crystoki Open Session Error: Slot number %d.\n",i+1); + goto err; + } + } + return 1; + +err: + if(LunaXL_dso) + DSO_free(LunaXL_dso); + return 0; +} + + +static int LunaXL_finish() { + + CK_RV retCode = CKR_OK; + + retCode=C_Finalize(NULL_PTR); + if(retCode !=CKR_OK) { + fprintf(stderr, " Luna XL -> Crystoki Finalize Error: 0x%x.\n", (int)retCode); + } + + if(LunaXL_dso == NULL) { + fprintf(stderr, " Luna XL -> Chrystoki library no longer loaded.\n"); + return 0; + } + + if(!DSO_free(LunaXL_dso)) { + fprintf(stderr, " Luna XL -> Can't unload Chrystoki library.\n"); + return 0; + } + + LunaXL_dso = NULL; + return 1; +} + + +static int LunaXL_rsa_priv_enc(int flen, unsigned char *from, unsigned char *to, RSA *rsa, int padding) { + + CK_RV retCode = CKR_OK; + CK_OBJECT_HANDLE hKey = 0; + CK_ULONG usOutLength=128; + + assigned_slot = (((unsigned int) getpid()) % (unsigned int) ulSlotCount); + hKey= BN_get_word(rsa->d); + + retCode = CA_SinglePartSign(hSession[assigned_slot], &mech, hKey, (CK_BYTE_PTR)from, (CK_USHORT)flen, (CK_BYTE_PTR)to, &usOutLength); + if(retCode !=CKR_OK) { + fprintf(stderr, " Luna XL-> Crystoki Single Part Sign Error: 0x%x. Slot Number: %d\n", (int)retCode, (int)(assigned_slot+1)); + goto err; + } + + return usOutLength; +err: + return -1; + +} + +static int LunaXL_rsa_priv_dec(int flen, unsigned char *from, unsigned char *to, RSA *rsa, int padding) { + + CK_RV retCode = CKR_OK; + CK_OBJECT_HANDLE hKey = 0; + CK_ULONG usOutLength=128; + assigned_slot = (((unsigned int) getpid()) % (unsigned int) ulSlotCount); + +/* Check for temporary key */ + if(BN_num_bytes(rsa->d)>2) + usOutLength= LunaXL_E_private_decrypt(flen, from, to, rsa, padding); + else { +/* OK, it is our key */ + hKey= BN_get_word(rsa->d); + retCode = CA_SinglePartDecrypt((CK_SESSION_HANDLE)hSession[assigned_slot], &mech, hKey, (CK_BYTE_PTR)from, (CK_USHORT)flen, (CK_BYTE_PTR)to, &usOutLength); + if(retCode !=CKR_OK) { + fprintf(stderr, " Luna XL -> Crystoki Single Part Decrypt Error: 0x%x. Slot Number: %d\n", (int)retCode, (int)(assigned_slot+1)); + goto err; + } + + } + return usOutLength; +err: + return -1; + +} + +#endif // !NO_HW_LUNA_XL +#endif // !NO_HW diff -Naur openssl-engine-0.9.6a.orig/crypto/engine/vendor_defns/lunaxl.h openssl-engine-0.9.6a/crypto/engine/vendor_defns/lunaxl.h --- openssl-engine-0.9.6a.orig/crypto/engine/vendor_defns/lunaxl.h Thu Jan 1 00:00:00 1970 +++ openssl-engine-0.9.6a/crypto/engine/vendor_defns/lunaxl.h Tue Jun 19 20:14:03 2001 @@ -0,0 +1,105 @@ +/* Written by Stevan Milinkovic <[EMAIL PROTECTED]>, + * Rouslan Beletski <[EMAIL PROTECTED]>, and * Behan Webster + * <[EMAIL PROTECTED]> for the OpenSSL project 2000. + */ +/* ==================================================================== + * Copyright (c) 1999 The OpenSSL Project. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. All advertising materials mentioning features or use of this + * software must display the following acknowledgment: + * "This product includes software developed by the OpenSSL Project + * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" + * + * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to + * endorse or promote products derived from this software without + * prior written permission. For written permission, please contact + * [EMAIL PROTECTED] + * + * 5. Products derived from this software may not be called "OpenSSL" + * nor may "OpenSSL" appear in their names without prior written + * permission of the OpenSSL Project. + * + * 6. Redistributions of any form whatsoever must retain the following + * acknowledgment: + * "This product includes software developed by the OpenSSL Project + * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" + * + * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY + * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + * ==================================================================== + * + * This product includes cryptographic software written by Eric Young + * ([EMAIL PROTECTED]). This product includes software written by Tim + * Hudson ([EMAIL PROTECTED]). + * + */ + +#ifndef __LUNAXL_H__ +#define __LUNAXL_H__ + +#define CK_PTR * +#define NULL_PTR 0 +#define CK_POINTER CK_PTR +#define CKR_OK 0x0000 +#define CKM_RSA_PKCS 0x00000001 +#define CKF_RW_SESSION 0x0002 +#define CKF_SERIAL_SESSION 0x0004 + +typedef unsigned char CK_BYTE; +typedef unsigned long int CK_USHORT; +typedef unsigned long int CK_ULONG; +typedef void CK_POINTER CK_VOID_PTR; +typedef CK_ULONG CK_FLAGS; +typedef CK_ULONG CK_SLOT_ID; +typedef CK_BYTE CK_POINTER CK_BYTE_PTR; +typedef CK_USHORT CK_POINTER CK_USHORT_PTR; +typedef CK_ULONG CK_POINTER CK_ULONG_PTR; +typedef CK_ULONG CK_SESSION_HANDLE; +typedef CK_SESSION_HANDLE CK_POINTER CK_SESSION_HANDLE_PTR; +typedef CK_ULONG CK_OBJECT_HANDLE; +typedef CK_USHORT CK_MECHANISM_TYPE; + +typedef struct CK_MECHANISM { + CK_MECHANISM_TYPE mechanism; + CK_VOID_PTR pParameter; + CK_ULONG ulParameterLen; + }CK_MECHANISM; + +typedef CK_MECHANISM CK_POINTER CK_MECHANISM_PTR; +typedef CK_USHORT CK_RV; + +typedef CK_RV (*CP_Initialize)(CK_VOID_PTR); +typedef CK_RV (*CP_GetNumberOfSSLSlots)(CK_ULONG_PTR); +typedef CK_RV (*CP_CloseSession)(CK_SESSION_HANDLE); +typedef CK_RV (*CP_OpenSession)(CK_SLOT_ID,CK_FLAGS,CK_VOID_PTR,CK_RV,CK_SESSION_HANDLE_PTR); +typedef CK_RV (*CP_SinglePartDecrypt)(CK_SESSION_HANDLE,CK_MECHANISM_PTR,CK_OBJECT_HANDLE,CK_BYTE_PTR,CK_ULONG,CK_BYTE_PTR,CK_ULONG_PTR); + +CP_Initialize C_Initialize,C_Finalize; +CP_OpenSession C_OpenSession; +CP_CloseSession C_CloseSession; +CP_GetNumberOfSSLSlots CA_GetNumberOfSSLSlots; +CP_SinglePartDecrypt CA_SinglePartDecrypt,CA_SinglePartSign; + +#endif __LUNAXL_H__ ______________________________________________________________________ OpenSSL Project http://www.openssl.org Development Mailing List [EMAIL PROTECTED] Automated List Manager [EMAIL PROTECTED]
