1160814Ssimon/* crypto/engine/hw_atalla.c */ 2280304Sjkim/* 3280304Sjkim * Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL project 4280304Sjkim * 2000. 5160814Ssimon */ 6160814Ssimon/* ==================================================================== 7160814Ssimon * Copyright (c) 1999-2001 The OpenSSL Project. All rights reserved. 8160814Ssimon * 9160814Ssimon * Redistribution and use in source and binary forms, with or without 10160814Ssimon * modification, are permitted provided that the following conditions 11160814Ssimon * are met: 12160814Ssimon * 13160814Ssimon * 1. Redistributions of source code must retain the above copyright 14280304Sjkim * notice, this list of conditions and the following disclaimer. 15160814Ssimon * 16160814Ssimon * 2. Redistributions in binary form must reproduce the above copyright 17160814Ssimon * notice, this list of conditions and the following disclaimer in 18160814Ssimon * the documentation and/or other materials provided with the 19160814Ssimon * distribution. 20160814Ssimon * 21160814Ssimon * 3. All advertising materials mentioning features or use of this 22160814Ssimon * software must display the following acknowledgment: 23160814Ssimon * "This product includes software developed by the OpenSSL Project 24160814Ssimon * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" 25160814Ssimon * 26160814Ssimon * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 27160814Ssimon * endorse or promote products derived from this software without 28160814Ssimon * prior written permission. For written permission, please contact 29160814Ssimon * licensing@OpenSSL.org. 30160814Ssimon * 31160814Ssimon * 5. Products derived from this software may not be called "OpenSSL" 32160814Ssimon * nor may "OpenSSL" appear in their names without prior written 33160814Ssimon * permission of the OpenSSL Project. 34160814Ssimon * 35160814Ssimon * 6. Redistributions of any form whatsoever must retain the following 36160814Ssimon * acknowledgment: 37160814Ssimon * "This product includes software developed by the OpenSSL Project 38160814Ssimon * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" 39160814Ssimon * 40160814Ssimon * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 41160814Ssimon * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 42160814Ssimon * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 43160814Ssimon * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 44160814Ssimon * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 45160814Ssimon * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 46160814Ssimon * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 47160814Ssimon * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 48160814Ssimon * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 49160814Ssimon * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 50160814Ssimon * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 51160814Ssimon * OF THE POSSIBILITY OF SUCH DAMAGE. 52160814Ssimon * ==================================================================== 53160814Ssimon * 54160814Ssimon * This product includes cryptographic software written by Eric Young 55160814Ssimon * (eay@cryptsoft.com). This product includes software written by Tim 56160814Ssimon * Hudson (tjh@cryptsoft.com). 57160814Ssimon * 58160814Ssimon */ 59160814Ssimon 60160814Ssimon#include <stdio.h> 61160814Ssimon#include <string.h> 62160814Ssimon#include <openssl/crypto.h> 63160814Ssimon#include <openssl/buffer.h> 64160814Ssimon#include <openssl/dso.h> 65160814Ssimon#include <openssl/engine.h> 66160814Ssimon#ifndef OPENSSL_NO_RSA 67280304Sjkim# include <openssl/rsa.h> 68160814Ssimon#endif 69160814Ssimon#ifndef OPENSSL_NO_DSA 70280304Sjkim# include <openssl/dsa.h> 71160814Ssimon#endif 72160814Ssimon#ifndef OPENSSL_NO_DH 73280304Sjkim# include <openssl/dh.h> 74160814Ssimon#endif 75160814Ssimon#include <openssl/bn.h> 76160814Ssimon 77160814Ssimon#ifndef OPENSSL_NO_HW 78280304Sjkim# ifndef OPENSSL_NO_HW_ATALLA 79160814Ssimon 80280304Sjkim# ifdef FLAT_INC 81280304Sjkim# include "atalla.h" 82280304Sjkim# else 83280304Sjkim# include "vendor_defns/atalla.h" 84280304Sjkim# endif 85160814Ssimon 86280304Sjkim# define ATALLA_LIB_NAME "atalla engine" 87280304Sjkim# include "e_atalla_err.c" 88160814Ssimon 89160814Ssimonstatic int atalla_destroy(ENGINE *e); 90160814Ssimonstatic int atalla_init(ENGINE *e); 91160814Ssimonstatic int atalla_finish(ENGINE *e); 92280304Sjkimstatic int atalla_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f) (void)); 93160814Ssimon 94160814Ssimon/* BIGNUM stuff */ 95160814Ssimonstatic int atalla_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, 96280304Sjkim const BIGNUM *m, BN_CTX *ctx); 97160814Ssimon 98280304Sjkim# ifndef OPENSSL_NO_RSA 99160814Ssimon/* RSA stuff */ 100280304Sjkimstatic int atalla_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, 101280304Sjkim BN_CTX *ctx); 102160814Ssimon/* This function is aliased to mod_exp (with the mont stuff dropped). */ 103160814Ssimonstatic int atalla_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, 104280304Sjkim const BIGNUM *m, BN_CTX *ctx, 105280304Sjkim BN_MONT_CTX *m_ctx); 106280304Sjkim# endif 107160814Ssimon 108280304Sjkim# ifndef OPENSSL_NO_DSA 109160814Ssimon/* DSA stuff */ 110160814Ssimonstatic int atalla_dsa_mod_exp(DSA *dsa, BIGNUM *rr, BIGNUM *a1, 111280304Sjkim BIGNUM *p1, BIGNUM *a2, BIGNUM *p2, BIGNUM *m, 112280304Sjkim BN_CTX *ctx, BN_MONT_CTX *in_mont); 113160814Ssimonstatic int atalla_mod_exp_dsa(DSA *dsa, BIGNUM *r, BIGNUM *a, 114280304Sjkim const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx, 115280304Sjkim BN_MONT_CTX *m_ctx); 116280304Sjkim# endif 117160814Ssimon 118280304Sjkim# ifndef OPENSSL_NO_DH 119160814Ssimon/* DH stuff */ 120160814Ssimon/* This function is alised to mod_exp (with the DH and mont dropped). */ 121160814Ssimonstatic int atalla_mod_exp_dh(const DH *dh, BIGNUM *r, 122280304Sjkim const BIGNUM *a, const BIGNUM *p, 123280304Sjkim const BIGNUM *m, BN_CTX *ctx, 124280304Sjkim BN_MONT_CTX *m_ctx); 125280304Sjkim# endif 126160814Ssimon 127160814Ssimon/* The definitions for control commands specific to this engine */ 128280304Sjkim# define ATALLA_CMD_SO_PATH ENGINE_CMD_BASE 129160814Ssimonstatic const ENGINE_CMD_DEFN atalla_cmd_defns[] = { 130280304Sjkim {ATALLA_CMD_SO_PATH, 131280304Sjkim "SO_PATH", 132280304Sjkim "Specifies the path to the 'atasi' shared library", 133280304Sjkim ENGINE_CMD_FLAG_STRING}, 134280304Sjkim {0, NULL, NULL, 0} 135280304Sjkim}; 136160814Ssimon 137280304Sjkim# ifndef OPENSSL_NO_RSA 138160814Ssimon/* Our internal RSA_METHOD that we provide pointers to */ 139280304Sjkimstatic RSA_METHOD atalla_rsa = { 140280304Sjkim "Atalla RSA method", 141280304Sjkim NULL, 142280304Sjkim NULL, 143280304Sjkim NULL, 144280304Sjkim NULL, 145280304Sjkim atalla_rsa_mod_exp, 146280304Sjkim atalla_mod_exp_mont, 147280304Sjkim NULL, 148280304Sjkim NULL, 149280304Sjkim 0, 150280304Sjkim NULL, 151280304Sjkim NULL, 152280304Sjkim NULL, 153280304Sjkim NULL 154280304Sjkim}; 155280304Sjkim# endif 156160814Ssimon 157280304Sjkim# ifndef OPENSSL_NO_DSA 158160814Ssimon/* Our internal DSA_METHOD that we provide pointers to */ 159280304Sjkimstatic DSA_METHOD atalla_dsa = { 160280304Sjkim "Atalla DSA method", 161280304Sjkim NULL, /* dsa_do_sign */ 162280304Sjkim NULL, /* dsa_sign_setup */ 163280304Sjkim NULL, /* dsa_do_verify */ 164280304Sjkim atalla_dsa_mod_exp, /* dsa_mod_exp */ 165280304Sjkim atalla_mod_exp_dsa, /* bn_mod_exp */ 166280304Sjkim NULL, /* init */ 167280304Sjkim NULL, /* finish */ 168280304Sjkim 0, /* flags */ 169280304Sjkim NULL, /* app_data */ 170280304Sjkim NULL, /* dsa_paramgen */ 171280304Sjkim NULL /* dsa_keygen */ 172280304Sjkim}; 173280304Sjkim# endif 174160814Ssimon 175280304Sjkim# ifndef OPENSSL_NO_DH 176160814Ssimon/* Our internal DH_METHOD that we provide pointers to */ 177280304Sjkimstatic DH_METHOD atalla_dh = { 178280304Sjkim "Atalla DH method", 179280304Sjkim NULL, 180280304Sjkim NULL, 181280304Sjkim atalla_mod_exp_dh, 182280304Sjkim NULL, 183280304Sjkim NULL, 184280304Sjkim 0, 185280304Sjkim NULL, 186280304Sjkim NULL 187280304Sjkim}; 188280304Sjkim# endif 189160814Ssimon 190160814Ssimon/* Constants used when creating the ENGINE */ 191160814Ssimonstatic const char *engine_atalla_id = "atalla"; 192160814Ssimonstatic const char *engine_atalla_name = "Atalla hardware engine support"; 193160814Ssimon 194280304Sjkim/* 195280304Sjkim * This internal function is used by ENGINE_atalla() and possibly by the 196280304Sjkim * "dynamic" ENGINE support too 197280304Sjkim */ 198160814Ssimonstatic int bind_helper(ENGINE *e) 199280304Sjkim{ 200280304Sjkim# ifndef OPENSSL_NO_RSA 201280304Sjkim const RSA_METHOD *meth1; 202280304Sjkim# endif 203280304Sjkim# ifndef OPENSSL_NO_DSA 204280304Sjkim const DSA_METHOD *meth2; 205280304Sjkim# endif 206280304Sjkim# ifndef OPENSSL_NO_DH 207280304Sjkim const DH_METHOD *meth3; 208280304Sjkim# endif 209280304Sjkim if (!ENGINE_set_id(e, engine_atalla_id) || 210280304Sjkim !ENGINE_set_name(e, engine_atalla_name) || 211280304Sjkim# ifndef OPENSSL_NO_RSA 212280304Sjkim !ENGINE_set_RSA(e, &atalla_rsa) || 213280304Sjkim# endif 214280304Sjkim# ifndef OPENSSL_NO_DSA 215280304Sjkim !ENGINE_set_DSA(e, &atalla_dsa) || 216280304Sjkim# endif 217280304Sjkim# ifndef OPENSSL_NO_DH 218280304Sjkim !ENGINE_set_DH(e, &atalla_dh) || 219280304Sjkim# endif 220280304Sjkim !ENGINE_set_destroy_function(e, atalla_destroy) || 221280304Sjkim !ENGINE_set_init_function(e, atalla_init) || 222280304Sjkim !ENGINE_set_finish_function(e, atalla_finish) || 223280304Sjkim !ENGINE_set_ctrl_function(e, atalla_ctrl) || 224280304Sjkim !ENGINE_set_cmd_defns(e, atalla_cmd_defns)) 225280304Sjkim return 0; 226160814Ssimon 227280304Sjkim# ifndef OPENSSL_NO_RSA 228280304Sjkim /* 229280304Sjkim * We know that the "PKCS1_SSLeay()" functions hook properly to the 230280304Sjkim * atalla-specific mod_exp and mod_exp_crt so we use those functions. NB: 231280304Sjkim * We don't use ENGINE_openssl() or anything "more generic" because 232280304Sjkim * something like the RSAref code may not hook properly, and if you own 233280304Sjkim * one of these cards then you have the right to do RSA operations on it 234280304Sjkim * anyway! 235280304Sjkim */ 236280304Sjkim meth1 = RSA_PKCS1_SSLeay(); 237280304Sjkim atalla_rsa.rsa_pub_enc = meth1->rsa_pub_enc; 238280304Sjkim atalla_rsa.rsa_pub_dec = meth1->rsa_pub_dec; 239280304Sjkim atalla_rsa.rsa_priv_enc = meth1->rsa_priv_enc; 240280304Sjkim atalla_rsa.rsa_priv_dec = meth1->rsa_priv_dec; 241280304Sjkim# endif 242160814Ssimon 243280304Sjkim# ifndef OPENSSL_NO_DSA 244280304Sjkim /* 245280304Sjkim * Use the DSA_OpenSSL() method and just hook the mod_exp-ish bits. 246280304Sjkim */ 247280304Sjkim meth2 = DSA_OpenSSL(); 248280304Sjkim atalla_dsa.dsa_do_sign = meth2->dsa_do_sign; 249280304Sjkim atalla_dsa.dsa_sign_setup = meth2->dsa_sign_setup; 250280304Sjkim atalla_dsa.dsa_do_verify = meth2->dsa_do_verify; 251280304Sjkim# endif 252160814Ssimon 253280304Sjkim# ifndef OPENSSL_NO_DH 254280304Sjkim /* Much the same for Diffie-Hellman */ 255280304Sjkim meth3 = DH_OpenSSL(); 256280304Sjkim atalla_dh.generate_key = meth3->generate_key; 257280304Sjkim atalla_dh.compute_key = meth3->compute_key; 258280304Sjkim# endif 259160814Ssimon 260280304Sjkim /* Ensure the atalla error handling is set up */ 261280304Sjkim ERR_load_ATALLA_strings(); 262280304Sjkim return 1; 263280304Sjkim} 264160814Ssimon 265280304Sjkim# ifdef OPENSSL_NO_DYNAMIC_ENGINE 266160814Ssimonstatic ENGINE *engine_atalla(void) 267280304Sjkim{ 268280304Sjkim ENGINE *ret = ENGINE_new(); 269280304Sjkim if (!ret) 270280304Sjkim return NULL; 271280304Sjkim if (!bind_helper(ret)) { 272280304Sjkim ENGINE_free(ret); 273280304Sjkim return NULL; 274280304Sjkim } 275280304Sjkim return ret; 276280304Sjkim} 277160814Ssimon 278160814Ssimonvoid ENGINE_load_atalla(void) 279280304Sjkim{ 280280304Sjkim /* Copied from eng_[openssl|dyn].c */ 281280304Sjkim ENGINE *toadd = engine_atalla(); 282280304Sjkim if (!toadd) 283280304Sjkim return; 284280304Sjkim ENGINE_add(toadd); 285280304Sjkim ENGINE_free(toadd); 286280304Sjkim ERR_clear_error(); 287280304Sjkim} 288280304Sjkim# endif 289160814Ssimon 290280304Sjkim/* 291280304Sjkim * This is a process-global DSO handle used for loading and unloading the 292280304Sjkim * Atalla library. NB: This is only set (or unset) during an init() or 293280304Sjkim * finish() call (reference counts permitting) and they're operating with 294280304Sjkim * global locks, so this should be thread-safe implicitly. 295280304Sjkim */ 296160814Ssimonstatic DSO *atalla_dso = NULL; 297160814Ssimon 298280304Sjkim/* 299280304Sjkim * These are the function pointers that are (un)set when the library has 300280304Sjkim * successfully (un)loaded. 301280304Sjkim */ 302160814Ssimonstatic tfnASI_GetHardwareConfig *p_Atalla_GetHardwareConfig = NULL; 303160814Ssimonstatic tfnASI_RSAPrivateKeyOpFn *p_Atalla_RSAPrivateKeyOpFn = NULL; 304280304Sjkimstatic tfnASI_GetPerformanceStatistics *p_Atalla_GetPerformanceStatistics = 305280304Sjkim NULL; 306160814Ssimon 307280304Sjkim/* 308280304Sjkim * These are the static string constants for the DSO file name and the 309280304Sjkim * function symbol names to bind to. Regrettably, the DSO name on *nix 310280304Sjkim * appears to be "atasi.so" rather than something more consistent like 311280304Sjkim * "libatasi.so". At the time of writing, I'm not sure what the file name on 312280304Sjkim * win32 is but clearly native name translation is not possible (eg 313280304Sjkim * libatasi.so on *nix, and atasi.dll on win32). For the purposes of testing, 314280304Sjkim * I have created a symbollic link called "libatasi.so" so that we can use 315280304Sjkim * native name-translation - a better solution will be needed. 316280304Sjkim */ 317160814Ssimonstatic const char *ATALLA_LIBNAME = NULL; 318160814Ssimonstatic const char *get_ATALLA_LIBNAME(void) 319280304Sjkim{ 320280304Sjkim if (ATALLA_LIBNAME) 321280304Sjkim return ATALLA_LIBNAME; 322280304Sjkim return "atasi"; 323280304Sjkim} 324280304Sjkim 325160814Ssimonstatic void free_ATALLA_LIBNAME(void) 326280304Sjkim{ 327280304Sjkim if (ATALLA_LIBNAME) 328280304Sjkim OPENSSL_free((void *)ATALLA_LIBNAME); 329280304Sjkim ATALLA_LIBNAME = NULL; 330280304Sjkim} 331280304Sjkim 332160814Ssimonstatic long set_ATALLA_LIBNAME(const char *name) 333280304Sjkim{ 334280304Sjkim free_ATALLA_LIBNAME(); 335280304Sjkim return (((ATALLA_LIBNAME = BUF_strdup(name)) != NULL) ? 1 : 0); 336280304Sjkim} 337280304Sjkim 338160814Ssimonstatic const char *ATALLA_F1 = "ASI_GetHardwareConfig"; 339160814Ssimonstatic const char *ATALLA_F2 = "ASI_RSAPrivateKeyOpFn"; 340160814Ssimonstatic const char *ATALLA_F3 = "ASI_GetPerformanceStatistics"; 341160814Ssimon 342160814Ssimon/* Destructor (complements the "ENGINE_atalla()" constructor) */ 343160814Ssimonstatic int atalla_destroy(ENGINE *e) 344280304Sjkim{ 345280304Sjkim free_ATALLA_LIBNAME(); 346280304Sjkim /* 347280304Sjkim * Unload the atalla error strings so any error state including our 348280304Sjkim * functs or reasons won't lead to a segfault (they simply get displayed 349280304Sjkim * without corresponding string data because none will be found). 350280304Sjkim */ 351280304Sjkim ERR_unload_ATALLA_strings(); 352280304Sjkim return 1; 353280304Sjkim} 354160814Ssimon 355160814Ssimon/* (de)initialisation functions. */ 356160814Ssimonstatic int atalla_init(ENGINE *e) 357280304Sjkim{ 358280304Sjkim tfnASI_GetHardwareConfig *p1; 359280304Sjkim tfnASI_RSAPrivateKeyOpFn *p2; 360280304Sjkim tfnASI_GetPerformanceStatistics *p3; 361280304Sjkim /* 362280304Sjkim * Not sure of the origin of this magic value, but Ben's code had it and 363280304Sjkim * it seemed to have been working for a few people. :-) 364280304Sjkim */ 365280304Sjkim unsigned int config_buf[1024]; 366160814Ssimon 367280304Sjkim if (atalla_dso != NULL) { 368280304Sjkim ATALLAerr(ATALLA_F_ATALLA_INIT, ATALLA_R_ALREADY_LOADED); 369280304Sjkim goto err; 370280304Sjkim } 371280304Sjkim /* 372280304Sjkim * Attempt to load libatasi.so/atasi.dll/whatever. Needs to be changed 373280304Sjkim * unfortunately because the Atalla drivers don't have standard library 374280304Sjkim * names that can be platform-translated well. 375280304Sjkim */ 376280304Sjkim /* 377280304Sjkim * TODO: Work out how to actually map to the names the Atalla drivers 378280304Sjkim * really use - for now a symbollic link needs to be created on the host 379280304Sjkim * system from libatasi.so to atasi.so on unix variants. 380280304Sjkim */ 381280304Sjkim atalla_dso = DSO_load(NULL, get_ATALLA_LIBNAME(), NULL, 0); 382280304Sjkim if (atalla_dso == NULL) { 383280304Sjkim ATALLAerr(ATALLA_F_ATALLA_INIT, ATALLA_R_NOT_LOADED); 384280304Sjkim goto err; 385280304Sjkim } 386280304Sjkim if (! 387280304Sjkim (p1 = 388280304Sjkim (tfnASI_GetHardwareConfig *) DSO_bind_func(atalla_dso, ATALLA_F1)) 389280304Sjkim|| !(p2 = (tfnASI_RSAPrivateKeyOpFn *) DSO_bind_func(atalla_dso, ATALLA_F2)) 390280304Sjkim|| !(p3 = 391280304Sjkim (tfnASI_GetPerformanceStatistics *) DSO_bind_func(atalla_dso, 392280304Sjkim ATALLA_F3))) { 393280304Sjkim ATALLAerr(ATALLA_F_ATALLA_INIT, ATALLA_R_NOT_LOADED); 394280304Sjkim goto err; 395280304Sjkim } 396280304Sjkim /* Copy the pointers */ 397280304Sjkim p_Atalla_GetHardwareConfig = p1; 398280304Sjkim p_Atalla_RSAPrivateKeyOpFn = p2; 399280304Sjkim p_Atalla_GetPerformanceStatistics = p3; 400280304Sjkim /* 401280304Sjkim * Perform a basic test to see if there's actually any unit running. 402280304Sjkim */ 403280304Sjkim if (p1(0L, config_buf) != 0) { 404280304Sjkim ATALLAerr(ATALLA_F_ATALLA_INIT, ATALLA_R_UNIT_FAILURE); 405280304Sjkim goto err; 406280304Sjkim } 407280304Sjkim /* Everything's fine. */ 408280304Sjkim return 1; 409280304Sjkim err: 410280304Sjkim if (atalla_dso) 411280304Sjkim DSO_free(atalla_dso); 412280304Sjkim atalla_dso = NULL; 413280304Sjkim p_Atalla_GetHardwareConfig = NULL; 414280304Sjkim p_Atalla_RSAPrivateKeyOpFn = NULL; 415280304Sjkim p_Atalla_GetPerformanceStatistics = NULL; 416280304Sjkim return 0; 417280304Sjkim} 418160814Ssimon 419160814Ssimonstatic int atalla_finish(ENGINE *e) 420280304Sjkim{ 421280304Sjkim free_ATALLA_LIBNAME(); 422280304Sjkim if (atalla_dso == NULL) { 423280304Sjkim ATALLAerr(ATALLA_F_ATALLA_FINISH, ATALLA_R_NOT_LOADED); 424280304Sjkim return 0; 425280304Sjkim } 426280304Sjkim if (!DSO_free(atalla_dso)) { 427280304Sjkim ATALLAerr(ATALLA_F_ATALLA_FINISH, ATALLA_R_UNIT_FAILURE); 428280304Sjkim return 0; 429280304Sjkim } 430280304Sjkim atalla_dso = NULL; 431280304Sjkim p_Atalla_GetHardwareConfig = NULL; 432280304Sjkim p_Atalla_RSAPrivateKeyOpFn = NULL; 433280304Sjkim p_Atalla_GetPerformanceStatistics = NULL; 434280304Sjkim return 1; 435280304Sjkim} 436160814Ssimon 437280304Sjkimstatic int atalla_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f) (void)) 438280304Sjkim{ 439280304Sjkim int initialised = ((atalla_dso == NULL) ? 0 : 1); 440280304Sjkim switch (cmd) { 441280304Sjkim case ATALLA_CMD_SO_PATH: 442280304Sjkim if (p == NULL) { 443280304Sjkim ATALLAerr(ATALLA_F_ATALLA_CTRL, ERR_R_PASSED_NULL_PARAMETER); 444280304Sjkim return 0; 445280304Sjkim } 446280304Sjkim if (initialised) { 447280304Sjkim ATALLAerr(ATALLA_F_ATALLA_CTRL, ATALLA_R_ALREADY_LOADED); 448280304Sjkim return 0; 449280304Sjkim } 450280304Sjkim return set_ATALLA_LIBNAME((const char *)p); 451280304Sjkim default: 452280304Sjkim break; 453280304Sjkim } 454280304Sjkim ATALLAerr(ATALLA_F_ATALLA_CTRL, ATALLA_R_CTRL_COMMAND_NOT_IMPLEMENTED); 455280304Sjkim return 0; 456280304Sjkim} 457160814Ssimon 458160814Ssimonstatic int atalla_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, 459280304Sjkim const BIGNUM *m, BN_CTX *ctx) 460280304Sjkim{ 461280304Sjkim /* 462280304Sjkim * I need somewhere to store temporary serialised values for use with the 463280304Sjkim * Atalla API calls. A neat cheat - I'll use BIGNUMs from the BN_CTX but 464280304Sjkim * access their arrays directly as byte arrays <grin>. This way I don't 465280304Sjkim * have to clean anything up. 466280304Sjkim */ 467280304Sjkim BIGNUM *modulus; 468280304Sjkim BIGNUM *exponent; 469280304Sjkim BIGNUM *argument; 470280304Sjkim BIGNUM *result; 471280304Sjkim RSAPrivateKey keydata; 472280304Sjkim int to_return, numbytes; 473160814Ssimon 474280304Sjkim modulus = exponent = argument = result = NULL; 475280304Sjkim to_return = 0; /* expect failure */ 476160814Ssimon 477280304Sjkim if (!atalla_dso) { 478280304Sjkim ATALLAerr(ATALLA_F_ATALLA_MOD_EXP, ATALLA_R_NOT_LOADED); 479280304Sjkim goto err; 480280304Sjkim } 481280304Sjkim /* Prepare the params */ 482280304Sjkim BN_CTX_start(ctx); 483280304Sjkim modulus = BN_CTX_get(ctx); 484280304Sjkim exponent = BN_CTX_get(ctx); 485280304Sjkim argument = BN_CTX_get(ctx); 486280304Sjkim result = BN_CTX_get(ctx); 487280304Sjkim if (!result) { 488280304Sjkim ATALLAerr(ATALLA_F_ATALLA_MOD_EXP, ATALLA_R_BN_CTX_FULL); 489280304Sjkim goto err; 490280304Sjkim } 491280304Sjkim if (!bn_wexpand(modulus, m->top) || !bn_wexpand(exponent, m->top) || 492280304Sjkim !bn_wexpand(argument, m->top) || !bn_wexpand(result, m->top)) { 493280304Sjkim ATALLAerr(ATALLA_F_ATALLA_MOD_EXP, ATALLA_R_BN_EXPAND_FAIL); 494280304Sjkim goto err; 495280304Sjkim } 496280304Sjkim /* Prepare the key-data */ 497280304Sjkim memset(&keydata, 0, sizeof keydata); 498280304Sjkim numbytes = BN_num_bytes(m); 499280304Sjkim memset(exponent->d, 0, numbytes); 500280304Sjkim memset(modulus->d, 0, numbytes); 501280304Sjkim BN_bn2bin(p, (unsigned char *)exponent->d + numbytes - BN_num_bytes(p)); 502280304Sjkim BN_bn2bin(m, (unsigned char *)modulus->d + numbytes - BN_num_bytes(m)); 503280304Sjkim keydata.privateExponent.data = (unsigned char *)exponent->d; 504280304Sjkim keydata.privateExponent.len = numbytes; 505280304Sjkim keydata.modulus.data = (unsigned char *)modulus->d; 506280304Sjkim keydata.modulus.len = numbytes; 507280304Sjkim /* Prepare the argument */ 508280304Sjkim memset(argument->d, 0, numbytes); 509280304Sjkim memset(result->d, 0, numbytes); 510280304Sjkim BN_bn2bin(a, (unsigned char *)argument->d + numbytes - BN_num_bytes(a)); 511280304Sjkim /* Perform the operation */ 512280304Sjkim if (p_Atalla_RSAPrivateKeyOpFn(&keydata, (unsigned char *)result->d, 513280304Sjkim (unsigned char *)argument->d, 514280304Sjkim keydata.modulus.len) != 0) { 515280304Sjkim ATALLAerr(ATALLA_F_ATALLA_MOD_EXP, ATALLA_R_REQUEST_FAILED); 516280304Sjkim goto err; 517280304Sjkim } 518280304Sjkim /* Convert the response */ 519280304Sjkim BN_bin2bn((unsigned char *)result->d, numbytes, r); 520280304Sjkim to_return = 1; 521280304Sjkim err: 522280304Sjkim BN_CTX_end(ctx); 523280304Sjkim return to_return; 524280304Sjkim} 525160814Ssimon 526280304Sjkim# ifndef OPENSSL_NO_RSA 527280304Sjkimstatic int atalla_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, 528280304Sjkim BN_CTX *ctx) 529280304Sjkim{ 530280304Sjkim int to_return = 0; 531160814Ssimon 532280304Sjkim if (!atalla_dso) { 533280304Sjkim ATALLAerr(ATALLA_F_ATALLA_RSA_MOD_EXP, ATALLA_R_NOT_LOADED); 534280304Sjkim goto err; 535280304Sjkim } 536280304Sjkim if (!rsa->d || !rsa->n) { 537280304Sjkim ATALLAerr(ATALLA_F_ATALLA_RSA_MOD_EXP, 538280304Sjkim ATALLA_R_MISSING_KEY_COMPONENTS); 539280304Sjkim goto err; 540280304Sjkim } 541280304Sjkim to_return = atalla_mod_exp(r0, I, rsa->d, rsa->n, ctx); 542280304Sjkim err: 543280304Sjkim return to_return; 544280304Sjkim} 545280304Sjkim# endif 546160814Ssimon 547280304Sjkim# ifndef OPENSSL_NO_DSA 548280304Sjkim/* 549280304Sjkim * This code was liberated and adapted from the commented-out code in 550280304Sjkim * dsa_ossl.c. Because of the unoptimised form of the Atalla acceleration (it 551280304Sjkim * doesn't have a CRT form for RSA), this function means that an Atalla 552280304Sjkim * system running with a DSA server certificate can handshake around 5 or 6 553280304Sjkim * times faster/more than an equivalent system running with RSA. Just check 554280304Sjkim * out the "signs" statistics from the RSA and DSA parts of "openssl speed 555280304Sjkim * -engine atalla dsa1024 rsa1024". 556280304Sjkim */ 557160814Ssimonstatic int atalla_dsa_mod_exp(DSA *dsa, BIGNUM *rr, BIGNUM *a1, 558280304Sjkim BIGNUM *p1, BIGNUM *a2, BIGNUM *p2, BIGNUM *m, 559280304Sjkim BN_CTX *ctx, BN_MONT_CTX *in_mont) 560280304Sjkim{ 561280304Sjkim BIGNUM t; 562280304Sjkim int to_return = 0; 563160814Ssimon 564280304Sjkim BN_init(&t); 565280304Sjkim /* let rr = a1 ^ p1 mod m */ 566280304Sjkim if (!atalla_mod_exp(rr, a1, p1, m, ctx)) 567280304Sjkim goto end; 568280304Sjkim /* let t = a2 ^ p2 mod m */ 569280304Sjkim if (!atalla_mod_exp(&t, a2, p2, m, ctx)) 570280304Sjkim goto end; 571280304Sjkim /* let rr = rr * t mod m */ 572280304Sjkim if (!BN_mod_mul(rr, rr, &t, m, ctx)) 573280304Sjkim goto end; 574280304Sjkim to_return = 1; 575280304Sjkim end: 576280304Sjkim BN_free(&t); 577280304Sjkim return to_return; 578280304Sjkim} 579280304Sjkim 580160814Ssimonstatic int atalla_mod_exp_dsa(DSA *dsa, BIGNUM *r, BIGNUM *a, 581280304Sjkim const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx, 582280304Sjkim BN_MONT_CTX *m_ctx) 583280304Sjkim{ 584280304Sjkim return atalla_mod_exp(r, a, p, m, ctx); 585280304Sjkim} 586280304Sjkim# endif 587160814Ssimon 588280304Sjkim# ifndef OPENSSL_NO_RSA 589160814Ssimon/* This function is aliased to mod_exp (with the mont stuff dropped). */ 590160814Ssimonstatic int atalla_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, 591280304Sjkim const BIGNUM *m, BN_CTX *ctx, 592280304Sjkim BN_MONT_CTX *m_ctx) 593280304Sjkim{ 594280304Sjkim return atalla_mod_exp(r, a, p, m, ctx); 595280304Sjkim} 596280304Sjkim# endif 597160814Ssimon 598280304Sjkim# ifndef OPENSSL_NO_DH 599160814Ssimon/* This function is aliased to mod_exp (with the dh and mont dropped). */ 600160814Ssimonstatic int atalla_mod_exp_dh(const DH *dh, BIGNUM *r, 601280304Sjkim const BIGNUM *a, const BIGNUM *p, 602280304Sjkim const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx) 603280304Sjkim{ 604280304Sjkim return atalla_mod_exp(r, a, p, m, ctx); 605280304Sjkim} 606280304Sjkim# endif 607160814Ssimon 608280304Sjkim/* 609280304Sjkim * This stuff is needed if this ENGINE is being compiled into a 610280304Sjkim * self-contained shared-library. 611280304Sjkim */ 612280304Sjkim# ifndef OPENSSL_NO_DYNAMIC_ENGINE 613160814Ssimonstatic int bind_fn(ENGINE *e, const char *id) 614280304Sjkim{ 615280304Sjkim if (id && (strcmp(id, engine_atalla_id) != 0)) 616280304Sjkim return 0; 617280304Sjkim if (!bind_helper(e)) 618280304Sjkim return 0; 619280304Sjkim return 1; 620280304Sjkim} 621280304Sjkim 622160814SsimonIMPLEMENT_DYNAMIC_CHECK_FN() 623280304Sjkim IMPLEMENT_DYNAMIC_BIND_FN(bind_fn) 624280304Sjkim# endif /* OPENSSL_NO_DYNAMIC_ENGINE */ 625280304Sjkim# endif /* !OPENSSL_NO_HW_ATALLA */ 626280304Sjkim#endif /* !OPENSSL_NO_HW */ 627