1*ebfedea0SLionel Sambuc /* crypto/engine/hw_cswift.c */ 2*ebfedea0SLionel Sambuc /* Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL 3*ebfedea0SLionel Sambuc * project 2000. 4*ebfedea0SLionel Sambuc */ 5*ebfedea0SLionel Sambuc /* ==================================================================== 6*ebfedea0SLionel Sambuc * Copyright (c) 1999-2001 The OpenSSL Project. All rights reserved. 7*ebfedea0SLionel Sambuc * 8*ebfedea0SLionel Sambuc * Redistribution and use in source and binary forms, with or without 9*ebfedea0SLionel Sambuc * modification, are permitted provided that the following conditions 10*ebfedea0SLionel Sambuc * are met: 11*ebfedea0SLionel Sambuc * 12*ebfedea0SLionel Sambuc * 1. Redistributions of source code must retain the above copyright 13*ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer. 14*ebfedea0SLionel Sambuc * 15*ebfedea0SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright 16*ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer in 17*ebfedea0SLionel Sambuc * the documentation and/or other materials provided with the 18*ebfedea0SLionel Sambuc * distribution. 19*ebfedea0SLionel Sambuc * 20*ebfedea0SLionel Sambuc * 3. All advertising materials mentioning features or use of this 21*ebfedea0SLionel Sambuc * software must display the following acknowledgment: 22*ebfedea0SLionel Sambuc * "This product includes software developed by the OpenSSL Project 23*ebfedea0SLionel Sambuc * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" 24*ebfedea0SLionel Sambuc * 25*ebfedea0SLionel Sambuc * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 26*ebfedea0SLionel Sambuc * endorse or promote products derived from this software without 27*ebfedea0SLionel Sambuc * prior written permission. For written permission, please contact 28*ebfedea0SLionel Sambuc * licensing@OpenSSL.org. 29*ebfedea0SLionel Sambuc * 30*ebfedea0SLionel Sambuc * 5. Products derived from this software may not be called "OpenSSL" 31*ebfedea0SLionel Sambuc * nor may "OpenSSL" appear in their names without prior written 32*ebfedea0SLionel Sambuc * permission of the OpenSSL Project. 33*ebfedea0SLionel Sambuc * 34*ebfedea0SLionel Sambuc * 6. Redistributions of any form whatsoever must retain the following 35*ebfedea0SLionel Sambuc * acknowledgment: 36*ebfedea0SLionel Sambuc * "This product includes software developed by the OpenSSL Project 37*ebfedea0SLionel Sambuc * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" 38*ebfedea0SLionel Sambuc * 39*ebfedea0SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 40*ebfedea0SLionel Sambuc * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 41*ebfedea0SLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 42*ebfedea0SLionel Sambuc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 43*ebfedea0SLionel Sambuc * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 44*ebfedea0SLionel Sambuc * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 45*ebfedea0SLionel Sambuc * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 46*ebfedea0SLionel Sambuc * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 47*ebfedea0SLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 48*ebfedea0SLionel Sambuc * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 49*ebfedea0SLionel Sambuc * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 50*ebfedea0SLionel Sambuc * OF THE POSSIBILITY OF SUCH DAMAGE. 51*ebfedea0SLionel Sambuc * ==================================================================== 52*ebfedea0SLionel Sambuc * 53*ebfedea0SLionel Sambuc * This product includes cryptographic software written by Eric Young 54*ebfedea0SLionel Sambuc * (eay@cryptsoft.com). This product includes software written by Tim 55*ebfedea0SLionel Sambuc * Hudson (tjh@cryptsoft.com). 56*ebfedea0SLionel Sambuc * 57*ebfedea0SLionel Sambuc */ 58*ebfedea0SLionel Sambuc 59*ebfedea0SLionel Sambuc #include <stdio.h> 60*ebfedea0SLionel Sambuc #include <string.h> 61*ebfedea0SLionel Sambuc #include <openssl/crypto.h> 62*ebfedea0SLionel Sambuc #include <openssl/buffer.h> 63*ebfedea0SLionel Sambuc #include <openssl/dso.h> 64*ebfedea0SLionel Sambuc #include <openssl/engine.h> 65*ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_RSA 66*ebfedea0SLionel Sambuc #include <openssl/rsa.h> 67*ebfedea0SLionel Sambuc #endif 68*ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_DSA 69*ebfedea0SLionel Sambuc #include <openssl/dsa.h> 70*ebfedea0SLionel Sambuc #endif 71*ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_DH 72*ebfedea0SLionel Sambuc #include <openssl/dh.h> 73*ebfedea0SLionel Sambuc #endif 74*ebfedea0SLionel Sambuc #include <openssl/rand.h> 75*ebfedea0SLionel Sambuc #include <openssl/bn.h> 76*ebfedea0SLionel Sambuc 77*ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_HW 78*ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_HW_CSWIFT 79*ebfedea0SLionel Sambuc 80*ebfedea0SLionel Sambuc /* Attribution notice: Rainbow have generously allowed me to reproduce 81*ebfedea0SLionel Sambuc * the necessary definitions here from their API. This means the support 82*ebfedea0SLionel Sambuc * can build independently of whether application builders have the 83*ebfedea0SLionel Sambuc * API or hardware. This will allow developers to easily produce software 84*ebfedea0SLionel Sambuc * that has latent hardware support for any users that have accelerators 85*ebfedea0SLionel Sambuc * installed, without the developers themselves needing anything extra. 86*ebfedea0SLionel Sambuc * 87*ebfedea0SLionel Sambuc * I have only clipped the parts from the CryptoSwift header files that 88*ebfedea0SLionel Sambuc * are (or seem) relevant to the CryptoSwift support code. This is 89*ebfedea0SLionel Sambuc * simply to keep the file sizes reasonable. 90*ebfedea0SLionel Sambuc * [Geoff] 91*ebfedea0SLionel Sambuc */ 92*ebfedea0SLionel Sambuc #ifdef FLAT_INC 93*ebfedea0SLionel Sambuc #include "cswift.h" 94*ebfedea0SLionel Sambuc #else 95*ebfedea0SLionel Sambuc #include "vendor_defns/cswift.h" 96*ebfedea0SLionel Sambuc #endif 97*ebfedea0SLionel Sambuc 98*ebfedea0SLionel Sambuc #define CSWIFT_LIB_NAME "cswift engine" 99*ebfedea0SLionel Sambuc #include "e_cswift_err.c" 100*ebfedea0SLionel Sambuc 101*ebfedea0SLionel Sambuc #define DECIMAL_SIZE(type) ((sizeof(type)*8+2)/3+1) 102*ebfedea0SLionel Sambuc 103*ebfedea0SLionel Sambuc static int cswift_destroy(ENGINE *e); 104*ebfedea0SLionel Sambuc static int cswift_init(ENGINE *e); 105*ebfedea0SLionel Sambuc static int cswift_finish(ENGINE *e); 106*ebfedea0SLionel Sambuc static int cswift_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)(void)); 107*ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_RSA 108*ebfedea0SLionel Sambuc static int cswift_bn_32copy(SW_LARGENUMBER * out, const BIGNUM * in); 109*ebfedea0SLionel Sambuc #endif 110*ebfedea0SLionel Sambuc 111*ebfedea0SLionel Sambuc /* BIGNUM stuff */ 112*ebfedea0SLionel Sambuc static int cswift_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, 113*ebfedea0SLionel Sambuc const BIGNUM *m, BN_CTX *ctx); 114*ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_RSA 115*ebfedea0SLionel Sambuc static int cswift_mod_exp_crt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, 116*ebfedea0SLionel Sambuc const BIGNUM *q, const BIGNUM *dmp1, const BIGNUM *dmq1, 117*ebfedea0SLionel Sambuc const BIGNUM *iqmp, BN_CTX *ctx); 118*ebfedea0SLionel Sambuc #endif 119*ebfedea0SLionel Sambuc 120*ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_RSA 121*ebfedea0SLionel Sambuc /* RSA stuff */ 122*ebfedea0SLionel Sambuc static int cswift_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx); 123*ebfedea0SLionel Sambuc /* This function is aliased to mod_exp (with the mont stuff dropped). */ 124*ebfedea0SLionel Sambuc static int cswift_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, 125*ebfedea0SLionel Sambuc const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx); 126*ebfedea0SLionel Sambuc #endif 127*ebfedea0SLionel Sambuc 128*ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_DSA 129*ebfedea0SLionel Sambuc /* DSA stuff */ 130*ebfedea0SLionel Sambuc static DSA_SIG *cswift_dsa_sign(const unsigned char *dgst, int dlen, DSA *dsa); 131*ebfedea0SLionel Sambuc static int cswift_dsa_verify(const unsigned char *dgst, int dgst_len, 132*ebfedea0SLionel Sambuc DSA_SIG *sig, DSA *dsa); 133*ebfedea0SLionel Sambuc #endif 134*ebfedea0SLionel Sambuc 135*ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_DH 136*ebfedea0SLionel Sambuc /* DH stuff */ 137*ebfedea0SLionel Sambuc /* This function is alised to mod_exp (with the DH and mont dropped). */ 138*ebfedea0SLionel Sambuc static int cswift_mod_exp_dh(const DH *dh, BIGNUM *r, 139*ebfedea0SLionel Sambuc const BIGNUM *a, const BIGNUM *p, 140*ebfedea0SLionel Sambuc const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx); 141*ebfedea0SLionel Sambuc #endif 142*ebfedea0SLionel Sambuc 143*ebfedea0SLionel Sambuc /* RAND stuff */ 144*ebfedea0SLionel Sambuc static int cswift_rand_bytes(unsigned char *buf, int num); 145*ebfedea0SLionel Sambuc static int cswift_rand_status(void); 146*ebfedea0SLionel Sambuc 147*ebfedea0SLionel Sambuc /* The definitions for control commands specific to this engine */ 148*ebfedea0SLionel Sambuc #define CSWIFT_CMD_SO_PATH ENGINE_CMD_BASE 149*ebfedea0SLionel Sambuc static const ENGINE_CMD_DEFN cswift_cmd_defns[] = { 150*ebfedea0SLionel Sambuc {CSWIFT_CMD_SO_PATH, 151*ebfedea0SLionel Sambuc "SO_PATH", 152*ebfedea0SLionel Sambuc "Specifies the path to the 'cswift' shared library", 153*ebfedea0SLionel Sambuc ENGINE_CMD_FLAG_STRING}, 154*ebfedea0SLionel Sambuc {0, NULL, NULL, 0} 155*ebfedea0SLionel Sambuc }; 156*ebfedea0SLionel Sambuc 157*ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_RSA 158*ebfedea0SLionel Sambuc /* Our internal RSA_METHOD that we provide pointers to */ 159*ebfedea0SLionel Sambuc static RSA_METHOD cswift_rsa = 160*ebfedea0SLionel Sambuc { 161*ebfedea0SLionel Sambuc "CryptoSwift RSA method", 162*ebfedea0SLionel Sambuc NULL, 163*ebfedea0SLionel Sambuc NULL, 164*ebfedea0SLionel Sambuc NULL, 165*ebfedea0SLionel Sambuc NULL, 166*ebfedea0SLionel Sambuc cswift_rsa_mod_exp, 167*ebfedea0SLionel Sambuc cswift_mod_exp_mont, 168*ebfedea0SLionel Sambuc NULL, 169*ebfedea0SLionel Sambuc NULL, 170*ebfedea0SLionel Sambuc 0, 171*ebfedea0SLionel Sambuc NULL, 172*ebfedea0SLionel Sambuc NULL, 173*ebfedea0SLionel Sambuc NULL, 174*ebfedea0SLionel Sambuc NULL 175*ebfedea0SLionel Sambuc }; 176*ebfedea0SLionel Sambuc #endif 177*ebfedea0SLionel Sambuc 178*ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_DSA 179*ebfedea0SLionel Sambuc /* Our internal DSA_METHOD that we provide pointers to */ 180*ebfedea0SLionel Sambuc static DSA_METHOD cswift_dsa = 181*ebfedea0SLionel Sambuc { 182*ebfedea0SLionel Sambuc "CryptoSwift DSA method", 183*ebfedea0SLionel Sambuc cswift_dsa_sign, 184*ebfedea0SLionel Sambuc NULL, /* dsa_sign_setup */ 185*ebfedea0SLionel Sambuc cswift_dsa_verify, 186*ebfedea0SLionel Sambuc NULL, /* dsa_mod_exp */ 187*ebfedea0SLionel Sambuc NULL, /* bn_mod_exp */ 188*ebfedea0SLionel Sambuc NULL, /* init */ 189*ebfedea0SLionel Sambuc NULL, /* finish */ 190*ebfedea0SLionel Sambuc 0, /* flags */ 191*ebfedea0SLionel Sambuc NULL, /* app_data */ 192*ebfedea0SLionel Sambuc NULL, /* dsa_paramgen */ 193*ebfedea0SLionel Sambuc NULL /* dsa_keygen */ 194*ebfedea0SLionel Sambuc }; 195*ebfedea0SLionel Sambuc #endif 196*ebfedea0SLionel Sambuc 197*ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_DH 198*ebfedea0SLionel Sambuc /* Our internal DH_METHOD that we provide pointers to */ 199*ebfedea0SLionel Sambuc static DH_METHOD cswift_dh = 200*ebfedea0SLionel Sambuc { 201*ebfedea0SLionel Sambuc "CryptoSwift DH method", 202*ebfedea0SLionel Sambuc NULL, 203*ebfedea0SLionel Sambuc NULL, 204*ebfedea0SLionel Sambuc cswift_mod_exp_dh, 205*ebfedea0SLionel Sambuc NULL, 206*ebfedea0SLionel Sambuc NULL, 207*ebfedea0SLionel Sambuc 0, 208*ebfedea0SLionel Sambuc NULL, 209*ebfedea0SLionel Sambuc NULL 210*ebfedea0SLionel Sambuc }; 211*ebfedea0SLionel Sambuc #endif 212*ebfedea0SLionel Sambuc 213*ebfedea0SLionel Sambuc static RAND_METHOD cswift_random = 214*ebfedea0SLionel Sambuc { 215*ebfedea0SLionel Sambuc /* "CryptoSwift RAND method", */ 216*ebfedea0SLionel Sambuc NULL, 217*ebfedea0SLionel Sambuc cswift_rand_bytes, 218*ebfedea0SLionel Sambuc NULL, 219*ebfedea0SLionel Sambuc NULL, 220*ebfedea0SLionel Sambuc cswift_rand_bytes, 221*ebfedea0SLionel Sambuc cswift_rand_status, 222*ebfedea0SLionel Sambuc }; 223*ebfedea0SLionel Sambuc 224*ebfedea0SLionel Sambuc 225*ebfedea0SLionel Sambuc /* Constants used when creating the ENGINE */ 226*ebfedea0SLionel Sambuc static const char *engine_cswift_id = "cswift"; 227*ebfedea0SLionel Sambuc static const char *engine_cswift_name = "CryptoSwift hardware engine support"; 228*ebfedea0SLionel Sambuc 229*ebfedea0SLionel Sambuc /* This internal function is used by ENGINE_cswift() and possibly by the 230*ebfedea0SLionel Sambuc * "dynamic" ENGINE support too */ 231*ebfedea0SLionel Sambuc static int bind_helper(ENGINE *e) 232*ebfedea0SLionel Sambuc { 233*ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_RSA 234*ebfedea0SLionel Sambuc const RSA_METHOD *meth1; 235*ebfedea0SLionel Sambuc #endif 236*ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_DH 237*ebfedea0SLionel Sambuc const DH_METHOD *meth2; 238*ebfedea0SLionel Sambuc #endif 239*ebfedea0SLionel Sambuc if(!ENGINE_set_id(e, engine_cswift_id) || 240*ebfedea0SLionel Sambuc !ENGINE_set_name(e, engine_cswift_name) || 241*ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_RSA 242*ebfedea0SLionel Sambuc !ENGINE_set_RSA(e, &cswift_rsa) || 243*ebfedea0SLionel Sambuc #endif 244*ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_DSA 245*ebfedea0SLionel Sambuc !ENGINE_set_DSA(e, &cswift_dsa) || 246*ebfedea0SLionel Sambuc #endif 247*ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_DH 248*ebfedea0SLionel Sambuc !ENGINE_set_DH(e, &cswift_dh) || 249*ebfedea0SLionel Sambuc #endif 250*ebfedea0SLionel Sambuc !ENGINE_set_RAND(e, &cswift_random) || 251*ebfedea0SLionel Sambuc !ENGINE_set_destroy_function(e, cswift_destroy) || 252*ebfedea0SLionel Sambuc !ENGINE_set_init_function(e, cswift_init) || 253*ebfedea0SLionel Sambuc !ENGINE_set_finish_function(e, cswift_finish) || 254*ebfedea0SLionel Sambuc !ENGINE_set_ctrl_function(e, cswift_ctrl) || 255*ebfedea0SLionel Sambuc !ENGINE_set_cmd_defns(e, cswift_cmd_defns)) 256*ebfedea0SLionel Sambuc return 0; 257*ebfedea0SLionel Sambuc 258*ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_RSA 259*ebfedea0SLionel Sambuc /* We know that the "PKCS1_SSLeay()" functions hook properly 260*ebfedea0SLionel Sambuc * to the cswift-specific mod_exp and mod_exp_crt so we use 261*ebfedea0SLionel Sambuc * those functions. NB: We don't use ENGINE_openssl() or 262*ebfedea0SLionel Sambuc * anything "more generic" because something like the RSAref 263*ebfedea0SLionel Sambuc * code may not hook properly, and if you own one of these 264*ebfedea0SLionel Sambuc * cards then you have the right to do RSA operations on it 265*ebfedea0SLionel Sambuc * anyway! */ 266*ebfedea0SLionel Sambuc meth1 = RSA_PKCS1_SSLeay(); 267*ebfedea0SLionel Sambuc cswift_rsa.rsa_pub_enc = meth1->rsa_pub_enc; 268*ebfedea0SLionel Sambuc cswift_rsa.rsa_pub_dec = meth1->rsa_pub_dec; 269*ebfedea0SLionel Sambuc cswift_rsa.rsa_priv_enc = meth1->rsa_priv_enc; 270*ebfedea0SLionel Sambuc cswift_rsa.rsa_priv_dec = meth1->rsa_priv_dec; 271*ebfedea0SLionel Sambuc #endif 272*ebfedea0SLionel Sambuc 273*ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_DH 274*ebfedea0SLionel Sambuc /* Much the same for Diffie-Hellman */ 275*ebfedea0SLionel Sambuc meth2 = DH_OpenSSL(); 276*ebfedea0SLionel Sambuc cswift_dh.generate_key = meth2->generate_key; 277*ebfedea0SLionel Sambuc cswift_dh.compute_key = meth2->compute_key; 278*ebfedea0SLionel Sambuc #endif 279*ebfedea0SLionel Sambuc 280*ebfedea0SLionel Sambuc /* Ensure the cswift error handling is set up */ 281*ebfedea0SLionel Sambuc ERR_load_CSWIFT_strings(); 282*ebfedea0SLionel Sambuc return 1; 283*ebfedea0SLionel Sambuc } 284*ebfedea0SLionel Sambuc 285*ebfedea0SLionel Sambuc #ifdef OPENSSL_NO_DYNAMIC_ENGINE 286*ebfedea0SLionel Sambuc static ENGINE *engine_cswift(void) 287*ebfedea0SLionel Sambuc { 288*ebfedea0SLionel Sambuc ENGINE *ret = ENGINE_new(); 289*ebfedea0SLionel Sambuc if(!ret) 290*ebfedea0SLionel Sambuc return NULL; 291*ebfedea0SLionel Sambuc if(!bind_helper(ret)) 292*ebfedea0SLionel Sambuc { 293*ebfedea0SLionel Sambuc ENGINE_free(ret); 294*ebfedea0SLionel Sambuc return NULL; 295*ebfedea0SLionel Sambuc } 296*ebfedea0SLionel Sambuc return ret; 297*ebfedea0SLionel Sambuc } 298*ebfedea0SLionel Sambuc 299*ebfedea0SLionel Sambuc void ENGINE_load_cswift(void) 300*ebfedea0SLionel Sambuc { 301*ebfedea0SLionel Sambuc /* Copied from eng_[openssl|dyn].c */ 302*ebfedea0SLionel Sambuc ENGINE *toadd = engine_cswift(); 303*ebfedea0SLionel Sambuc if(!toadd) return; 304*ebfedea0SLionel Sambuc ENGINE_add(toadd); 305*ebfedea0SLionel Sambuc ENGINE_free(toadd); 306*ebfedea0SLionel Sambuc ERR_clear_error(); 307*ebfedea0SLionel Sambuc } 308*ebfedea0SLionel Sambuc #endif 309*ebfedea0SLionel Sambuc 310*ebfedea0SLionel Sambuc /* This is a process-global DSO handle used for loading and unloading 311*ebfedea0SLionel Sambuc * the CryptoSwift library. NB: This is only set (or unset) during an 312*ebfedea0SLionel Sambuc * init() or finish() call (reference counts permitting) and they're 313*ebfedea0SLionel Sambuc * operating with global locks, so this should be thread-safe 314*ebfedea0SLionel Sambuc * implicitly. */ 315*ebfedea0SLionel Sambuc static DSO *cswift_dso = NULL; 316*ebfedea0SLionel Sambuc 317*ebfedea0SLionel Sambuc /* These are the function pointers that are (un)set when the library has 318*ebfedea0SLionel Sambuc * successfully (un)loaded. */ 319*ebfedea0SLionel Sambuc t_swAcquireAccContext *p_CSwift_AcquireAccContext = NULL; 320*ebfedea0SLionel Sambuc t_swAttachKeyParam *p_CSwift_AttachKeyParam = NULL; 321*ebfedea0SLionel Sambuc t_swSimpleRequest *p_CSwift_SimpleRequest = NULL; 322*ebfedea0SLionel Sambuc t_swReleaseAccContext *p_CSwift_ReleaseAccContext = NULL; 323*ebfedea0SLionel Sambuc 324*ebfedea0SLionel Sambuc /* Used in the DSO operations. */ 325*ebfedea0SLionel Sambuc static const char *CSWIFT_LIBNAME = NULL; 326*ebfedea0SLionel Sambuc static const char *get_CSWIFT_LIBNAME(void) 327*ebfedea0SLionel Sambuc { 328*ebfedea0SLionel Sambuc if(CSWIFT_LIBNAME) 329*ebfedea0SLionel Sambuc return CSWIFT_LIBNAME; 330*ebfedea0SLionel Sambuc return "swift"; 331*ebfedea0SLionel Sambuc } 332*ebfedea0SLionel Sambuc static void free_CSWIFT_LIBNAME(void) 333*ebfedea0SLionel Sambuc { 334*ebfedea0SLionel Sambuc if(CSWIFT_LIBNAME) 335*ebfedea0SLionel Sambuc OPENSSL_free((void*)CSWIFT_LIBNAME); 336*ebfedea0SLionel Sambuc CSWIFT_LIBNAME = NULL; 337*ebfedea0SLionel Sambuc } 338*ebfedea0SLionel Sambuc static long set_CSWIFT_LIBNAME(const char *name) 339*ebfedea0SLionel Sambuc { 340*ebfedea0SLionel Sambuc free_CSWIFT_LIBNAME(); 341*ebfedea0SLionel Sambuc return (((CSWIFT_LIBNAME = BUF_strdup(name)) != NULL) ? 1 : 0); 342*ebfedea0SLionel Sambuc } 343*ebfedea0SLionel Sambuc static const char *CSWIFT_F1 = "swAcquireAccContext"; 344*ebfedea0SLionel Sambuc static const char *CSWIFT_F2 = "swAttachKeyParam"; 345*ebfedea0SLionel Sambuc static const char *CSWIFT_F3 = "swSimpleRequest"; 346*ebfedea0SLionel Sambuc static const char *CSWIFT_F4 = "swReleaseAccContext"; 347*ebfedea0SLionel Sambuc 348*ebfedea0SLionel Sambuc 349*ebfedea0SLionel Sambuc /* CryptoSwift library functions and mechanics - these are used by the 350*ebfedea0SLionel Sambuc * higher-level functions further down. NB: As and where there's no 351*ebfedea0SLionel Sambuc * error checking, take a look lower down where these functions are 352*ebfedea0SLionel Sambuc * called, the checking and error handling is probably down there. */ 353*ebfedea0SLionel Sambuc 354*ebfedea0SLionel Sambuc /* utility function to obtain a context */ 355*ebfedea0SLionel Sambuc static int get_context(SW_CONTEXT_HANDLE *hac) 356*ebfedea0SLionel Sambuc { 357*ebfedea0SLionel Sambuc SW_STATUS status; 358*ebfedea0SLionel Sambuc 359*ebfedea0SLionel Sambuc status = p_CSwift_AcquireAccContext(hac); 360*ebfedea0SLionel Sambuc if(status != SW_OK) 361*ebfedea0SLionel Sambuc return 0; 362*ebfedea0SLionel Sambuc return 1; 363*ebfedea0SLionel Sambuc } 364*ebfedea0SLionel Sambuc 365*ebfedea0SLionel Sambuc /* similarly to release one. */ 366*ebfedea0SLionel Sambuc static void release_context(SW_CONTEXT_HANDLE hac) 367*ebfedea0SLionel Sambuc { 368*ebfedea0SLionel Sambuc p_CSwift_ReleaseAccContext(hac); 369*ebfedea0SLionel Sambuc } 370*ebfedea0SLionel Sambuc 371*ebfedea0SLionel Sambuc /* Destructor (complements the "ENGINE_cswift()" constructor) */ 372*ebfedea0SLionel Sambuc static int cswift_destroy(ENGINE *e) 373*ebfedea0SLionel Sambuc { 374*ebfedea0SLionel Sambuc free_CSWIFT_LIBNAME(); 375*ebfedea0SLionel Sambuc ERR_unload_CSWIFT_strings(); 376*ebfedea0SLionel Sambuc return 1; 377*ebfedea0SLionel Sambuc } 378*ebfedea0SLionel Sambuc 379*ebfedea0SLionel Sambuc /* (de)initialisation functions. */ 380*ebfedea0SLionel Sambuc static int cswift_init(ENGINE *e) 381*ebfedea0SLionel Sambuc { 382*ebfedea0SLionel Sambuc SW_CONTEXT_HANDLE hac; 383*ebfedea0SLionel Sambuc t_swAcquireAccContext *p1; 384*ebfedea0SLionel Sambuc t_swAttachKeyParam *p2; 385*ebfedea0SLionel Sambuc t_swSimpleRequest *p3; 386*ebfedea0SLionel Sambuc t_swReleaseAccContext *p4; 387*ebfedea0SLionel Sambuc 388*ebfedea0SLionel Sambuc if(cswift_dso != NULL) 389*ebfedea0SLionel Sambuc { 390*ebfedea0SLionel Sambuc CSWIFTerr(CSWIFT_F_CSWIFT_INIT,CSWIFT_R_ALREADY_LOADED); 391*ebfedea0SLionel Sambuc goto err; 392*ebfedea0SLionel Sambuc } 393*ebfedea0SLionel Sambuc /* Attempt to load libswift.so/swift.dll/whatever. */ 394*ebfedea0SLionel Sambuc cswift_dso = DSO_load(NULL, get_CSWIFT_LIBNAME(), NULL, 0); 395*ebfedea0SLionel Sambuc if(cswift_dso == NULL) 396*ebfedea0SLionel Sambuc { 397*ebfedea0SLionel Sambuc CSWIFTerr(CSWIFT_F_CSWIFT_INIT,CSWIFT_R_NOT_LOADED); 398*ebfedea0SLionel Sambuc goto err; 399*ebfedea0SLionel Sambuc } 400*ebfedea0SLionel Sambuc if(!(p1 = (t_swAcquireAccContext *) 401*ebfedea0SLionel Sambuc DSO_bind_func(cswift_dso, CSWIFT_F1)) || 402*ebfedea0SLionel Sambuc !(p2 = (t_swAttachKeyParam *) 403*ebfedea0SLionel Sambuc DSO_bind_func(cswift_dso, CSWIFT_F2)) || 404*ebfedea0SLionel Sambuc !(p3 = (t_swSimpleRequest *) 405*ebfedea0SLionel Sambuc DSO_bind_func(cswift_dso, CSWIFT_F3)) || 406*ebfedea0SLionel Sambuc !(p4 = (t_swReleaseAccContext *) 407*ebfedea0SLionel Sambuc DSO_bind_func(cswift_dso, CSWIFT_F4))) 408*ebfedea0SLionel Sambuc { 409*ebfedea0SLionel Sambuc CSWIFTerr(CSWIFT_F_CSWIFT_INIT,CSWIFT_R_NOT_LOADED); 410*ebfedea0SLionel Sambuc goto err; 411*ebfedea0SLionel Sambuc } 412*ebfedea0SLionel Sambuc /* Copy the pointers */ 413*ebfedea0SLionel Sambuc p_CSwift_AcquireAccContext = p1; 414*ebfedea0SLionel Sambuc p_CSwift_AttachKeyParam = p2; 415*ebfedea0SLionel Sambuc p_CSwift_SimpleRequest = p3; 416*ebfedea0SLionel Sambuc p_CSwift_ReleaseAccContext = p4; 417*ebfedea0SLionel Sambuc /* Try and get a context - if not, we may have a DSO but no 418*ebfedea0SLionel Sambuc * accelerator! */ 419*ebfedea0SLionel Sambuc if(!get_context(&hac)) 420*ebfedea0SLionel Sambuc { 421*ebfedea0SLionel Sambuc CSWIFTerr(CSWIFT_F_CSWIFT_INIT,CSWIFT_R_UNIT_FAILURE); 422*ebfedea0SLionel Sambuc goto err; 423*ebfedea0SLionel Sambuc } 424*ebfedea0SLionel Sambuc release_context(hac); 425*ebfedea0SLionel Sambuc /* Everything's fine. */ 426*ebfedea0SLionel Sambuc return 1; 427*ebfedea0SLionel Sambuc err: 428*ebfedea0SLionel Sambuc if(cswift_dso) 429*ebfedea0SLionel Sambuc { 430*ebfedea0SLionel Sambuc DSO_free(cswift_dso); 431*ebfedea0SLionel Sambuc cswift_dso = NULL; 432*ebfedea0SLionel Sambuc } 433*ebfedea0SLionel Sambuc p_CSwift_AcquireAccContext = NULL; 434*ebfedea0SLionel Sambuc p_CSwift_AttachKeyParam = NULL; 435*ebfedea0SLionel Sambuc p_CSwift_SimpleRequest = NULL; 436*ebfedea0SLionel Sambuc p_CSwift_ReleaseAccContext = NULL; 437*ebfedea0SLionel Sambuc return 0; 438*ebfedea0SLionel Sambuc } 439*ebfedea0SLionel Sambuc 440*ebfedea0SLionel Sambuc static int cswift_finish(ENGINE *e) 441*ebfedea0SLionel Sambuc { 442*ebfedea0SLionel Sambuc free_CSWIFT_LIBNAME(); 443*ebfedea0SLionel Sambuc if(cswift_dso == NULL) 444*ebfedea0SLionel Sambuc { 445*ebfedea0SLionel Sambuc CSWIFTerr(CSWIFT_F_CSWIFT_FINISH,CSWIFT_R_NOT_LOADED); 446*ebfedea0SLionel Sambuc return 0; 447*ebfedea0SLionel Sambuc } 448*ebfedea0SLionel Sambuc if(!DSO_free(cswift_dso)) 449*ebfedea0SLionel Sambuc { 450*ebfedea0SLionel Sambuc CSWIFTerr(CSWIFT_F_CSWIFT_FINISH,CSWIFT_R_UNIT_FAILURE); 451*ebfedea0SLionel Sambuc return 0; 452*ebfedea0SLionel Sambuc } 453*ebfedea0SLionel Sambuc cswift_dso = NULL; 454*ebfedea0SLionel Sambuc p_CSwift_AcquireAccContext = NULL; 455*ebfedea0SLionel Sambuc p_CSwift_AttachKeyParam = NULL; 456*ebfedea0SLionel Sambuc p_CSwift_SimpleRequest = NULL; 457*ebfedea0SLionel Sambuc p_CSwift_ReleaseAccContext = NULL; 458*ebfedea0SLionel Sambuc return 1; 459*ebfedea0SLionel Sambuc } 460*ebfedea0SLionel Sambuc 461*ebfedea0SLionel Sambuc static int cswift_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)(void)) 462*ebfedea0SLionel Sambuc { 463*ebfedea0SLionel Sambuc int initialised = ((cswift_dso == NULL) ? 0 : 1); 464*ebfedea0SLionel Sambuc switch(cmd) 465*ebfedea0SLionel Sambuc { 466*ebfedea0SLionel Sambuc case CSWIFT_CMD_SO_PATH: 467*ebfedea0SLionel Sambuc if(p == NULL) 468*ebfedea0SLionel Sambuc { 469*ebfedea0SLionel Sambuc CSWIFTerr(CSWIFT_F_CSWIFT_CTRL,ERR_R_PASSED_NULL_PARAMETER); 470*ebfedea0SLionel Sambuc return 0; 471*ebfedea0SLionel Sambuc } 472*ebfedea0SLionel Sambuc if(initialised) 473*ebfedea0SLionel Sambuc { 474*ebfedea0SLionel Sambuc CSWIFTerr(CSWIFT_F_CSWIFT_CTRL,CSWIFT_R_ALREADY_LOADED); 475*ebfedea0SLionel Sambuc return 0; 476*ebfedea0SLionel Sambuc } 477*ebfedea0SLionel Sambuc return set_CSWIFT_LIBNAME((const char *)p); 478*ebfedea0SLionel Sambuc default: 479*ebfedea0SLionel Sambuc break; 480*ebfedea0SLionel Sambuc } 481*ebfedea0SLionel Sambuc CSWIFTerr(CSWIFT_F_CSWIFT_CTRL,CSWIFT_R_CTRL_COMMAND_NOT_IMPLEMENTED); 482*ebfedea0SLionel Sambuc return 0; 483*ebfedea0SLionel Sambuc } 484*ebfedea0SLionel Sambuc 485*ebfedea0SLionel Sambuc /* Un petit mod_exp */ 486*ebfedea0SLionel Sambuc static int cswift_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, 487*ebfedea0SLionel Sambuc const BIGNUM *m, BN_CTX *ctx) 488*ebfedea0SLionel Sambuc { 489*ebfedea0SLionel Sambuc /* I need somewhere to store temporary serialised values for 490*ebfedea0SLionel Sambuc * use with the CryptoSwift API calls. A neat cheat - I'll use 491*ebfedea0SLionel Sambuc * BIGNUMs from the BN_CTX but access their arrays directly as 492*ebfedea0SLionel Sambuc * byte arrays <grin>. This way I don't have to clean anything 493*ebfedea0SLionel Sambuc * up. */ 494*ebfedea0SLionel Sambuc BIGNUM *modulus; 495*ebfedea0SLionel Sambuc BIGNUM *exponent; 496*ebfedea0SLionel Sambuc BIGNUM *argument; 497*ebfedea0SLionel Sambuc BIGNUM *result; 498*ebfedea0SLionel Sambuc SW_STATUS sw_status; 499*ebfedea0SLionel Sambuc SW_LARGENUMBER arg, res; 500*ebfedea0SLionel Sambuc SW_PARAM sw_param; 501*ebfedea0SLionel Sambuc SW_CONTEXT_HANDLE hac; 502*ebfedea0SLionel Sambuc int to_return, acquired; 503*ebfedea0SLionel Sambuc 504*ebfedea0SLionel Sambuc modulus = exponent = argument = result = NULL; 505*ebfedea0SLionel Sambuc to_return = 0; /* expect failure */ 506*ebfedea0SLionel Sambuc acquired = 0; 507*ebfedea0SLionel Sambuc 508*ebfedea0SLionel Sambuc if(!get_context(&hac)) 509*ebfedea0SLionel Sambuc { 510*ebfedea0SLionel Sambuc CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP,CSWIFT_R_UNIT_FAILURE); 511*ebfedea0SLionel Sambuc goto err; 512*ebfedea0SLionel Sambuc } 513*ebfedea0SLionel Sambuc acquired = 1; 514*ebfedea0SLionel Sambuc /* Prepare the params */ 515*ebfedea0SLionel Sambuc BN_CTX_start(ctx); 516*ebfedea0SLionel Sambuc modulus = BN_CTX_get(ctx); 517*ebfedea0SLionel Sambuc exponent = BN_CTX_get(ctx); 518*ebfedea0SLionel Sambuc argument = BN_CTX_get(ctx); 519*ebfedea0SLionel Sambuc result = BN_CTX_get(ctx); 520*ebfedea0SLionel Sambuc if(!result) 521*ebfedea0SLionel Sambuc { 522*ebfedea0SLionel Sambuc CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP,CSWIFT_R_BN_CTX_FULL); 523*ebfedea0SLionel Sambuc goto err; 524*ebfedea0SLionel Sambuc } 525*ebfedea0SLionel Sambuc if(!bn_wexpand(modulus, m->top) || !bn_wexpand(exponent, p->top) || 526*ebfedea0SLionel Sambuc !bn_wexpand(argument, a->top) || !bn_wexpand(result, m->top)) 527*ebfedea0SLionel Sambuc { 528*ebfedea0SLionel Sambuc CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP,CSWIFT_R_BN_EXPAND_FAIL); 529*ebfedea0SLionel Sambuc goto err; 530*ebfedea0SLionel Sambuc } 531*ebfedea0SLionel Sambuc sw_param.type = SW_ALG_EXP; 532*ebfedea0SLionel Sambuc sw_param.up.exp.modulus.nbytes = BN_bn2bin(m, 533*ebfedea0SLionel Sambuc (unsigned char *)modulus->d); 534*ebfedea0SLionel Sambuc sw_param.up.exp.modulus.value = (unsigned char *)modulus->d; 535*ebfedea0SLionel Sambuc sw_param.up.exp.exponent.nbytes = BN_bn2bin(p, 536*ebfedea0SLionel Sambuc (unsigned char *)exponent->d); 537*ebfedea0SLionel Sambuc sw_param.up.exp.exponent.value = (unsigned char *)exponent->d; 538*ebfedea0SLionel Sambuc /* Attach the key params */ 539*ebfedea0SLionel Sambuc sw_status = p_CSwift_AttachKeyParam(hac, &sw_param); 540*ebfedea0SLionel Sambuc switch(sw_status) 541*ebfedea0SLionel Sambuc { 542*ebfedea0SLionel Sambuc case SW_OK: 543*ebfedea0SLionel Sambuc break; 544*ebfedea0SLionel Sambuc case SW_ERR_INPUT_SIZE: 545*ebfedea0SLionel Sambuc CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP,CSWIFT_R_BAD_KEY_SIZE); 546*ebfedea0SLionel Sambuc goto err; 547*ebfedea0SLionel Sambuc default: 548*ebfedea0SLionel Sambuc { 549*ebfedea0SLionel Sambuc char tmpbuf[DECIMAL_SIZE(sw_status)+1]; 550*ebfedea0SLionel Sambuc CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP,CSWIFT_R_REQUEST_FAILED); 551*ebfedea0SLionel Sambuc sprintf(tmpbuf, "%ld", sw_status); 552*ebfedea0SLionel Sambuc ERR_add_error_data(2, "CryptoSwift error number is ",tmpbuf); 553*ebfedea0SLionel Sambuc } 554*ebfedea0SLionel Sambuc goto err; 555*ebfedea0SLionel Sambuc } 556*ebfedea0SLionel Sambuc /* Prepare the argument and response */ 557*ebfedea0SLionel Sambuc arg.nbytes = BN_bn2bin(a, (unsigned char *)argument->d); 558*ebfedea0SLionel Sambuc arg.value = (unsigned char *)argument->d; 559*ebfedea0SLionel Sambuc res.nbytes = BN_num_bytes(m); 560*ebfedea0SLionel Sambuc memset(result->d, 0, res.nbytes); 561*ebfedea0SLionel Sambuc res.value = (unsigned char *)result->d; 562*ebfedea0SLionel Sambuc /* Perform the operation */ 563*ebfedea0SLionel Sambuc if((sw_status = p_CSwift_SimpleRequest(hac, SW_CMD_MODEXP, &arg, 1, 564*ebfedea0SLionel Sambuc &res, 1)) != SW_OK) 565*ebfedea0SLionel Sambuc { 566*ebfedea0SLionel Sambuc char tmpbuf[DECIMAL_SIZE(sw_status)+1]; 567*ebfedea0SLionel Sambuc CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP,CSWIFT_R_REQUEST_FAILED); 568*ebfedea0SLionel Sambuc sprintf(tmpbuf, "%ld", sw_status); 569*ebfedea0SLionel Sambuc ERR_add_error_data(2, "CryptoSwift error number is ",tmpbuf); 570*ebfedea0SLionel Sambuc goto err; 571*ebfedea0SLionel Sambuc } 572*ebfedea0SLionel Sambuc /* Convert the response */ 573*ebfedea0SLionel Sambuc BN_bin2bn((unsigned char *)result->d, res.nbytes, r); 574*ebfedea0SLionel Sambuc to_return = 1; 575*ebfedea0SLionel Sambuc err: 576*ebfedea0SLionel Sambuc if(acquired) 577*ebfedea0SLionel Sambuc release_context(hac); 578*ebfedea0SLionel Sambuc BN_CTX_end(ctx); 579*ebfedea0SLionel Sambuc return to_return; 580*ebfedea0SLionel Sambuc } 581*ebfedea0SLionel Sambuc 582*ebfedea0SLionel Sambuc 583*ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_RSA 584*ebfedea0SLionel Sambuc int cswift_bn_32copy(SW_LARGENUMBER * out, const BIGNUM * in) 585*ebfedea0SLionel Sambuc { 586*ebfedea0SLionel Sambuc int mod; 587*ebfedea0SLionel Sambuc int numbytes = BN_num_bytes(in); 588*ebfedea0SLionel Sambuc 589*ebfedea0SLionel Sambuc mod = 0; 590*ebfedea0SLionel Sambuc while( ((out->nbytes = (numbytes+mod)) % 32) ) 591*ebfedea0SLionel Sambuc { 592*ebfedea0SLionel Sambuc mod++; 593*ebfedea0SLionel Sambuc } 594*ebfedea0SLionel Sambuc out->value = (unsigned char*)OPENSSL_malloc(out->nbytes); 595*ebfedea0SLionel Sambuc if(!out->value) 596*ebfedea0SLionel Sambuc { 597*ebfedea0SLionel Sambuc return 0; 598*ebfedea0SLionel Sambuc } 599*ebfedea0SLionel Sambuc BN_bn2bin(in, &out->value[mod]); 600*ebfedea0SLionel Sambuc if(mod) 601*ebfedea0SLionel Sambuc memset(out->value, 0, mod); 602*ebfedea0SLionel Sambuc 603*ebfedea0SLionel Sambuc return 1; 604*ebfedea0SLionel Sambuc } 605*ebfedea0SLionel Sambuc #endif 606*ebfedea0SLionel Sambuc 607*ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_RSA 608*ebfedea0SLionel Sambuc /* Un petit mod_exp chinois */ 609*ebfedea0SLionel Sambuc static int cswift_mod_exp_crt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, 610*ebfedea0SLionel Sambuc const BIGNUM *q, const BIGNUM *dmp1, 611*ebfedea0SLionel Sambuc const BIGNUM *dmq1, const BIGNUM *iqmp, BN_CTX *ctx) 612*ebfedea0SLionel Sambuc { 613*ebfedea0SLionel Sambuc SW_STATUS sw_status; 614*ebfedea0SLionel Sambuc SW_LARGENUMBER arg, res; 615*ebfedea0SLionel Sambuc SW_PARAM sw_param; 616*ebfedea0SLionel Sambuc SW_CONTEXT_HANDLE hac; 617*ebfedea0SLionel Sambuc BIGNUM *result = NULL; 618*ebfedea0SLionel Sambuc BIGNUM *argument = NULL; 619*ebfedea0SLionel Sambuc int to_return = 0; /* expect failure */ 620*ebfedea0SLionel Sambuc int acquired = 0; 621*ebfedea0SLionel Sambuc 622*ebfedea0SLionel Sambuc sw_param.up.crt.p.value = NULL; 623*ebfedea0SLionel Sambuc sw_param.up.crt.q.value = NULL; 624*ebfedea0SLionel Sambuc sw_param.up.crt.dmp1.value = NULL; 625*ebfedea0SLionel Sambuc sw_param.up.crt.dmq1.value = NULL; 626*ebfedea0SLionel Sambuc sw_param.up.crt.iqmp.value = NULL; 627*ebfedea0SLionel Sambuc 628*ebfedea0SLionel Sambuc if(!get_context(&hac)) 629*ebfedea0SLionel Sambuc { 630*ebfedea0SLionel Sambuc CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT,CSWIFT_R_UNIT_FAILURE); 631*ebfedea0SLionel Sambuc goto err; 632*ebfedea0SLionel Sambuc } 633*ebfedea0SLionel Sambuc acquired = 1; 634*ebfedea0SLionel Sambuc 635*ebfedea0SLionel Sambuc /* Prepare the params */ 636*ebfedea0SLionel Sambuc argument = BN_new(); 637*ebfedea0SLionel Sambuc result = BN_new(); 638*ebfedea0SLionel Sambuc if(!result || !argument) 639*ebfedea0SLionel Sambuc { 640*ebfedea0SLionel Sambuc CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT,CSWIFT_R_BN_CTX_FULL); 641*ebfedea0SLionel Sambuc goto err; 642*ebfedea0SLionel Sambuc } 643*ebfedea0SLionel Sambuc 644*ebfedea0SLionel Sambuc 645*ebfedea0SLionel Sambuc sw_param.type = SW_ALG_CRT; 646*ebfedea0SLionel Sambuc /************************************************************************/ 647*ebfedea0SLionel Sambuc /* 04/02/2003 */ 648*ebfedea0SLionel Sambuc /* Modified by Frederic Giudicelli (deny-all.com) to overcome the */ 649*ebfedea0SLionel Sambuc /* limitation of cswift with values not a multiple of 32 */ 650*ebfedea0SLionel Sambuc /************************************************************************/ 651*ebfedea0SLionel Sambuc if(!cswift_bn_32copy(&sw_param.up.crt.p, p)) 652*ebfedea0SLionel Sambuc { 653*ebfedea0SLionel Sambuc CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT,CSWIFT_R_BN_EXPAND_FAIL); 654*ebfedea0SLionel Sambuc goto err; 655*ebfedea0SLionel Sambuc } 656*ebfedea0SLionel Sambuc if(!cswift_bn_32copy(&sw_param.up.crt.q, q)) 657*ebfedea0SLionel Sambuc { 658*ebfedea0SLionel Sambuc CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT,CSWIFT_R_BN_EXPAND_FAIL); 659*ebfedea0SLionel Sambuc goto err; 660*ebfedea0SLionel Sambuc } 661*ebfedea0SLionel Sambuc if(!cswift_bn_32copy(&sw_param.up.crt.dmp1, dmp1)) 662*ebfedea0SLionel Sambuc { 663*ebfedea0SLionel Sambuc CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT,CSWIFT_R_BN_EXPAND_FAIL); 664*ebfedea0SLionel Sambuc goto err; 665*ebfedea0SLionel Sambuc } 666*ebfedea0SLionel Sambuc if(!cswift_bn_32copy(&sw_param.up.crt.dmq1, dmq1)) 667*ebfedea0SLionel Sambuc { 668*ebfedea0SLionel Sambuc CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT,CSWIFT_R_BN_EXPAND_FAIL); 669*ebfedea0SLionel Sambuc goto err; 670*ebfedea0SLionel Sambuc } 671*ebfedea0SLionel Sambuc if(!cswift_bn_32copy(&sw_param.up.crt.iqmp, iqmp)) 672*ebfedea0SLionel Sambuc { 673*ebfedea0SLionel Sambuc CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT,CSWIFT_R_BN_EXPAND_FAIL); 674*ebfedea0SLionel Sambuc goto err; 675*ebfedea0SLionel Sambuc } 676*ebfedea0SLionel Sambuc if( !bn_wexpand(argument, a->top) || 677*ebfedea0SLionel Sambuc !bn_wexpand(result, p->top + q->top)) 678*ebfedea0SLionel Sambuc { 679*ebfedea0SLionel Sambuc CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT,CSWIFT_R_BN_EXPAND_FAIL); 680*ebfedea0SLionel Sambuc goto err; 681*ebfedea0SLionel Sambuc } 682*ebfedea0SLionel Sambuc 683*ebfedea0SLionel Sambuc /* Attach the key params */ 684*ebfedea0SLionel Sambuc sw_status = p_CSwift_AttachKeyParam(hac, &sw_param); 685*ebfedea0SLionel Sambuc switch(sw_status) 686*ebfedea0SLionel Sambuc { 687*ebfedea0SLionel Sambuc case SW_OK: 688*ebfedea0SLionel Sambuc break; 689*ebfedea0SLionel Sambuc case SW_ERR_INPUT_SIZE: 690*ebfedea0SLionel Sambuc CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT,CSWIFT_R_BAD_KEY_SIZE); 691*ebfedea0SLionel Sambuc goto err; 692*ebfedea0SLionel Sambuc default: 693*ebfedea0SLionel Sambuc { 694*ebfedea0SLionel Sambuc char tmpbuf[DECIMAL_SIZE(sw_status)+1]; 695*ebfedea0SLionel Sambuc CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT,CSWIFT_R_REQUEST_FAILED); 696*ebfedea0SLionel Sambuc sprintf(tmpbuf, "%ld", sw_status); 697*ebfedea0SLionel Sambuc ERR_add_error_data(2, "CryptoSwift error number is ",tmpbuf); 698*ebfedea0SLionel Sambuc } 699*ebfedea0SLionel Sambuc goto err; 700*ebfedea0SLionel Sambuc } 701*ebfedea0SLionel Sambuc /* Prepare the argument and response */ 702*ebfedea0SLionel Sambuc arg.nbytes = BN_bn2bin(a, (unsigned char *)argument->d); 703*ebfedea0SLionel Sambuc arg.value = (unsigned char *)argument->d; 704*ebfedea0SLionel Sambuc res.nbytes = 2 * BN_num_bytes(p); 705*ebfedea0SLionel Sambuc memset(result->d, 0, res.nbytes); 706*ebfedea0SLionel Sambuc res.value = (unsigned char *)result->d; 707*ebfedea0SLionel Sambuc /* Perform the operation */ 708*ebfedea0SLionel Sambuc if((sw_status = p_CSwift_SimpleRequest(hac, SW_CMD_MODEXP_CRT, &arg, 1, 709*ebfedea0SLionel Sambuc &res, 1)) != SW_OK) 710*ebfedea0SLionel Sambuc { 711*ebfedea0SLionel Sambuc char tmpbuf[DECIMAL_SIZE(sw_status)+1]; 712*ebfedea0SLionel Sambuc CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT,CSWIFT_R_REQUEST_FAILED); 713*ebfedea0SLionel Sambuc sprintf(tmpbuf, "%ld", sw_status); 714*ebfedea0SLionel Sambuc ERR_add_error_data(2, "CryptoSwift error number is ",tmpbuf); 715*ebfedea0SLionel Sambuc goto err; 716*ebfedea0SLionel Sambuc } 717*ebfedea0SLionel Sambuc /* Convert the response */ 718*ebfedea0SLionel Sambuc BN_bin2bn((unsigned char *)result->d, res.nbytes, r); 719*ebfedea0SLionel Sambuc to_return = 1; 720*ebfedea0SLionel Sambuc err: 721*ebfedea0SLionel Sambuc if(sw_param.up.crt.p.value) 722*ebfedea0SLionel Sambuc OPENSSL_free(sw_param.up.crt.p.value); 723*ebfedea0SLionel Sambuc if(sw_param.up.crt.q.value) 724*ebfedea0SLionel Sambuc OPENSSL_free(sw_param.up.crt.q.value); 725*ebfedea0SLionel Sambuc if(sw_param.up.crt.dmp1.value) 726*ebfedea0SLionel Sambuc OPENSSL_free(sw_param.up.crt.dmp1.value); 727*ebfedea0SLionel Sambuc if(sw_param.up.crt.dmq1.value) 728*ebfedea0SLionel Sambuc OPENSSL_free(sw_param.up.crt.dmq1.value); 729*ebfedea0SLionel Sambuc if(sw_param.up.crt.iqmp.value) 730*ebfedea0SLionel Sambuc OPENSSL_free(sw_param.up.crt.iqmp.value); 731*ebfedea0SLionel Sambuc if(result) 732*ebfedea0SLionel Sambuc BN_free(result); 733*ebfedea0SLionel Sambuc if(argument) 734*ebfedea0SLionel Sambuc BN_free(argument); 735*ebfedea0SLionel Sambuc if(acquired) 736*ebfedea0SLionel Sambuc release_context(hac); 737*ebfedea0SLionel Sambuc return to_return; 738*ebfedea0SLionel Sambuc } 739*ebfedea0SLionel Sambuc #endif 740*ebfedea0SLionel Sambuc 741*ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_RSA 742*ebfedea0SLionel Sambuc static int cswift_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx) 743*ebfedea0SLionel Sambuc { 744*ebfedea0SLionel Sambuc int to_return = 0; 745*ebfedea0SLionel Sambuc const RSA_METHOD * def_rsa_method; 746*ebfedea0SLionel Sambuc 747*ebfedea0SLionel Sambuc if(!rsa->p || !rsa->q || !rsa->dmp1 || !rsa->dmq1 || !rsa->iqmp) 748*ebfedea0SLionel Sambuc { 749*ebfedea0SLionel Sambuc CSWIFTerr(CSWIFT_F_CSWIFT_RSA_MOD_EXP,CSWIFT_R_MISSING_KEY_COMPONENTS); 750*ebfedea0SLionel Sambuc goto err; 751*ebfedea0SLionel Sambuc } 752*ebfedea0SLionel Sambuc 753*ebfedea0SLionel Sambuc /* Try the limits of RSA (2048 bits) */ 754*ebfedea0SLionel Sambuc if(BN_num_bytes(rsa->p) > 128 || 755*ebfedea0SLionel Sambuc BN_num_bytes(rsa->q) > 128 || 756*ebfedea0SLionel Sambuc BN_num_bytes(rsa->dmp1) > 128 || 757*ebfedea0SLionel Sambuc BN_num_bytes(rsa->dmq1) > 128 || 758*ebfedea0SLionel Sambuc BN_num_bytes(rsa->iqmp) > 128) 759*ebfedea0SLionel Sambuc { 760*ebfedea0SLionel Sambuc #ifdef RSA_NULL 761*ebfedea0SLionel Sambuc def_rsa_method=RSA_null_method(); 762*ebfedea0SLionel Sambuc #else 763*ebfedea0SLionel Sambuc #if 0 764*ebfedea0SLionel Sambuc def_rsa_method=RSA_PKCS1_RSAref(); 765*ebfedea0SLionel Sambuc #else 766*ebfedea0SLionel Sambuc def_rsa_method=RSA_PKCS1_SSLeay(); 767*ebfedea0SLionel Sambuc #endif 768*ebfedea0SLionel Sambuc #endif 769*ebfedea0SLionel Sambuc if(def_rsa_method) 770*ebfedea0SLionel Sambuc return def_rsa_method->rsa_mod_exp(r0, I, rsa, ctx); 771*ebfedea0SLionel Sambuc } 772*ebfedea0SLionel Sambuc 773*ebfedea0SLionel Sambuc to_return = cswift_mod_exp_crt(r0, I, rsa->p, rsa->q, rsa->dmp1, 774*ebfedea0SLionel Sambuc rsa->dmq1, rsa->iqmp, ctx); 775*ebfedea0SLionel Sambuc err: 776*ebfedea0SLionel Sambuc return to_return; 777*ebfedea0SLionel Sambuc } 778*ebfedea0SLionel Sambuc 779*ebfedea0SLionel Sambuc /* This function is aliased to mod_exp (with the mont stuff dropped). */ 780*ebfedea0SLionel Sambuc static int cswift_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, 781*ebfedea0SLionel Sambuc const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx) 782*ebfedea0SLionel Sambuc { 783*ebfedea0SLionel Sambuc const RSA_METHOD * def_rsa_method; 784*ebfedea0SLionel Sambuc 785*ebfedea0SLionel Sambuc /* Try the limits of RSA (2048 bits) */ 786*ebfedea0SLionel Sambuc if(BN_num_bytes(r) > 256 || 787*ebfedea0SLionel Sambuc BN_num_bytes(a) > 256 || 788*ebfedea0SLionel Sambuc BN_num_bytes(m) > 256) 789*ebfedea0SLionel Sambuc { 790*ebfedea0SLionel Sambuc #ifdef RSA_NULL 791*ebfedea0SLionel Sambuc def_rsa_method=RSA_null_method(); 792*ebfedea0SLionel Sambuc #else 793*ebfedea0SLionel Sambuc #if 0 794*ebfedea0SLionel Sambuc def_rsa_method=RSA_PKCS1_RSAref(); 795*ebfedea0SLionel Sambuc #else 796*ebfedea0SLionel Sambuc def_rsa_method=RSA_PKCS1_SSLeay(); 797*ebfedea0SLionel Sambuc #endif 798*ebfedea0SLionel Sambuc #endif 799*ebfedea0SLionel Sambuc if(def_rsa_method) 800*ebfedea0SLionel Sambuc return def_rsa_method->bn_mod_exp(r, a, p, m, ctx, m_ctx); 801*ebfedea0SLionel Sambuc } 802*ebfedea0SLionel Sambuc 803*ebfedea0SLionel Sambuc return cswift_mod_exp(r, a, p, m, ctx); 804*ebfedea0SLionel Sambuc } 805*ebfedea0SLionel Sambuc #endif /* OPENSSL_NO_RSA */ 806*ebfedea0SLionel Sambuc 807*ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_DSA 808*ebfedea0SLionel Sambuc static DSA_SIG *cswift_dsa_sign(const unsigned char *dgst, int dlen, DSA *dsa) 809*ebfedea0SLionel Sambuc { 810*ebfedea0SLionel Sambuc SW_CONTEXT_HANDLE hac; 811*ebfedea0SLionel Sambuc SW_PARAM sw_param; 812*ebfedea0SLionel Sambuc SW_STATUS sw_status; 813*ebfedea0SLionel Sambuc SW_LARGENUMBER arg, res; 814*ebfedea0SLionel Sambuc BN_CTX *ctx; 815*ebfedea0SLionel Sambuc BIGNUM *dsa_p = NULL; 816*ebfedea0SLionel Sambuc BIGNUM *dsa_q = NULL; 817*ebfedea0SLionel Sambuc BIGNUM *dsa_g = NULL; 818*ebfedea0SLionel Sambuc BIGNUM *dsa_key = NULL; 819*ebfedea0SLionel Sambuc BIGNUM *result = NULL; 820*ebfedea0SLionel Sambuc DSA_SIG *to_return = NULL; 821*ebfedea0SLionel Sambuc int acquired = 0; 822*ebfedea0SLionel Sambuc 823*ebfedea0SLionel Sambuc if((ctx = BN_CTX_new()) == NULL) 824*ebfedea0SLionel Sambuc goto err; 825*ebfedea0SLionel Sambuc if(!get_context(&hac)) 826*ebfedea0SLionel Sambuc { 827*ebfedea0SLionel Sambuc CSWIFTerr(CSWIFT_F_CSWIFT_DSA_SIGN,CSWIFT_R_UNIT_FAILURE); 828*ebfedea0SLionel Sambuc goto err; 829*ebfedea0SLionel Sambuc } 830*ebfedea0SLionel Sambuc acquired = 1; 831*ebfedea0SLionel Sambuc /* Prepare the params */ 832*ebfedea0SLionel Sambuc BN_CTX_start(ctx); 833*ebfedea0SLionel Sambuc dsa_p = BN_CTX_get(ctx); 834*ebfedea0SLionel Sambuc dsa_q = BN_CTX_get(ctx); 835*ebfedea0SLionel Sambuc dsa_g = BN_CTX_get(ctx); 836*ebfedea0SLionel Sambuc dsa_key = BN_CTX_get(ctx); 837*ebfedea0SLionel Sambuc result = BN_CTX_get(ctx); 838*ebfedea0SLionel Sambuc if(!result) 839*ebfedea0SLionel Sambuc { 840*ebfedea0SLionel Sambuc CSWIFTerr(CSWIFT_F_CSWIFT_DSA_SIGN,CSWIFT_R_BN_CTX_FULL); 841*ebfedea0SLionel Sambuc goto err; 842*ebfedea0SLionel Sambuc } 843*ebfedea0SLionel Sambuc if(!bn_wexpand(dsa_p, dsa->p->top) || 844*ebfedea0SLionel Sambuc !bn_wexpand(dsa_q, dsa->q->top) || 845*ebfedea0SLionel Sambuc !bn_wexpand(dsa_g, dsa->g->top) || 846*ebfedea0SLionel Sambuc !bn_wexpand(dsa_key, dsa->priv_key->top) || 847*ebfedea0SLionel Sambuc !bn_wexpand(result, dsa->p->top)) 848*ebfedea0SLionel Sambuc { 849*ebfedea0SLionel Sambuc CSWIFTerr(CSWIFT_F_CSWIFT_DSA_SIGN,CSWIFT_R_BN_EXPAND_FAIL); 850*ebfedea0SLionel Sambuc goto err; 851*ebfedea0SLionel Sambuc } 852*ebfedea0SLionel Sambuc sw_param.type = SW_ALG_DSA; 853*ebfedea0SLionel Sambuc sw_param.up.dsa.p.nbytes = BN_bn2bin(dsa->p, 854*ebfedea0SLionel Sambuc (unsigned char *)dsa_p->d); 855*ebfedea0SLionel Sambuc sw_param.up.dsa.p.value = (unsigned char *)dsa_p->d; 856*ebfedea0SLionel Sambuc sw_param.up.dsa.q.nbytes = BN_bn2bin(dsa->q, 857*ebfedea0SLionel Sambuc (unsigned char *)dsa_q->d); 858*ebfedea0SLionel Sambuc sw_param.up.dsa.q.value = (unsigned char *)dsa_q->d; 859*ebfedea0SLionel Sambuc sw_param.up.dsa.g.nbytes = BN_bn2bin(dsa->g, 860*ebfedea0SLionel Sambuc (unsigned char *)dsa_g->d); 861*ebfedea0SLionel Sambuc sw_param.up.dsa.g.value = (unsigned char *)dsa_g->d; 862*ebfedea0SLionel Sambuc sw_param.up.dsa.key.nbytes = BN_bn2bin(dsa->priv_key, 863*ebfedea0SLionel Sambuc (unsigned char *)dsa_key->d); 864*ebfedea0SLionel Sambuc sw_param.up.dsa.key.value = (unsigned char *)dsa_key->d; 865*ebfedea0SLionel Sambuc /* Attach the key params */ 866*ebfedea0SLionel Sambuc sw_status = p_CSwift_AttachKeyParam(hac, &sw_param); 867*ebfedea0SLionel Sambuc switch(sw_status) 868*ebfedea0SLionel Sambuc { 869*ebfedea0SLionel Sambuc case SW_OK: 870*ebfedea0SLionel Sambuc break; 871*ebfedea0SLionel Sambuc case SW_ERR_INPUT_SIZE: 872*ebfedea0SLionel Sambuc CSWIFTerr(CSWIFT_F_CSWIFT_DSA_SIGN,CSWIFT_R_BAD_KEY_SIZE); 873*ebfedea0SLionel Sambuc goto err; 874*ebfedea0SLionel Sambuc default: 875*ebfedea0SLionel Sambuc { 876*ebfedea0SLionel Sambuc char tmpbuf[DECIMAL_SIZE(sw_status)+1]; 877*ebfedea0SLionel Sambuc CSWIFTerr(CSWIFT_F_CSWIFT_DSA_SIGN,CSWIFT_R_REQUEST_FAILED); 878*ebfedea0SLionel Sambuc sprintf(tmpbuf, "%ld", sw_status); 879*ebfedea0SLionel Sambuc ERR_add_error_data(2, "CryptoSwift error number is ",tmpbuf); 880*ebfedea0SLionel Sambuc } 881*ebfedea0SLionel Sambuc goto err; 882*ebfedea0SLionel Sambuc } 883*ebfedea0SLionel Sambuc /* Prepare the argument and response */ 884*ebfedea0SLionel Sambuc arg.nbytes = dlen; 885*ebfedea0SLionel Sambuc arg.value = (unsigned char *)dgst; 886*ebfedea0SLionel Sambuc res.nbytes = BN_num_bytes(dsa->p); 887*ebfedea0SLionel Sambuc memset(result->d, 0, res.nbytes); 888*ebfedea0SLionel Sambuc res.value = (unsigned char *)result->d; 889*ebfedea0SLionel Sambuc /* Perform the operation */ 890*ebfedea0SLionel Sambuc sw_status = p_CSwift_SimpleRequest(hac, SW_CMD_DSS_SIGN, &arg, 1, 891*ebfedea0SLionel Sambuc &res, 1); 892*ebfedea0SLionel Sambuc if(sw_status != SW_OK) 893*ebfedea0SLionel Sambuc { 894*ebfedea0SLionel Sambuc char tmpbuf[DECIMAL_SIZE(sw_status)+1]; 895*ebfedea0SLionel Sambuc CSWIFTerr(CSWIFT_F_CSWIFT_DSA_SIGN,CSWIFT_R_REQUEST_FAILED); 896*ebfedea0SLionel Sambuc sprintf(tmpbuf, "%ld", sw_status); 897*ebfedea0SLionel Sambuc ERR_add_error_data(2, "CryptoSwift error number is ",tmpbuf); 898*ebfedea0SLionel Sambuc goto err; 899*ebfedea0SLionel Sambuc } 900*ebfedea0SLionel Sambuc /* Convert the response */ 901*ebfedea0SLionel Sambuc if((to_return = DSA_SIG_new()) == NULL) 902*ebfedea0SLionel Sambuc goto err; 903*ebfedea0SLionel Sambuc to_return->r = BN_bin2bn((unsigned char *)result->d, 20, NULL); 904*ebfedea0SLionel Sambuc to_return->s = BN_bin2bn((unsigned char *)result->d + 20, 20, NULL); 905*ebfedea0SLionel Sambuc 906*ebfedea0SLionel Sambuc err: 907*ebfedea0SLionel Sambuc if(acquired) 908*ebfedea0SLionel Sambuc release_context(hac); 909*ebfedea0SLionel Sambuc if(ctx) 910*ebfedea0SLionel Sambuc { 911*ebfedea0SLionel Sambuc BN_CTX_end(ctx); 912*ebfedea0SLionel Sambuc BN_CTX_free(ctx); 913*ebfedea0SLionel Sambuc } 914*ebfedea0SLionel Sambuc return to_return; 915*ebfedea0SLionel Sambuc } 916*ebfedea0SLionel Sambuc 917*ebfedea0SLionel Sambuc static int cswift_dsa_verify(const unsigned char *dgst, int dgst_len, 918*ebfedea0SLionel Sambuc DSA_SIG *sig, DSA *dsa) 919*ebfedea0SLionel Sambuc { 920*ebfedea0SLionel Sambuc SW_CONTEXT_HANDLE hac; 921*ebfedea0SLionel Sambuc SW_PARAM sw_param; 922*ebfedea0SLionel Sambuc SW_STATUS sw_status; 923*ebfedea0SLionel Sambuc SW_LARGENUMBER arg[2], res; 924*ebfedea0SLionel Sambuc unsigned long sig_result; 925*ebfedea0SLionel Sambuc BN_CTX *ctx; 926*ebfedea0SLionel Sambuc BIGNUM *dsa_p = NULL; 927*ebfedea0SLionel Sambuc BIGNUM *dsa_q = NULL; 928*ebfedea0SLionel Sambuc BIGNUM *dsa_g = NULL; 929*ebfedea0SLionel Sambuc BIGNUM *dsa_key = NULL; 930*ebfedea0SLionel Sambuc BIGNUM *argument = NULL; 931*ebfedea0SLionel Sambuc int to_return = -1; 932*ebfedea0SLionel Sambuc int acquired = 0; 933*ebfedea0SLionel Sambuc 934*ebfedea0SLionel Sambuc if((ctx = BN_CTX_new()) == NULL) 935*ebfedea0SLionel Sambuc goto err; 936*ebfedea0SLionel Sambuc if(!get_context(&hac)) 937*ebfedea0SLionel Sambuc { 938*ebfedea0SLionel Sambuc CSWIFTerr(CSWIFT_F_CSWIFT_DSA_VERIFY,CSWIFT_R_UNIT_FAILURE); 939*ebfedea0SLionel Sambuc goto err; 940*ebfedea0SLionel Sambuc } 941*ebfedea0SLionel Sambuc acquired = 1; 942*ebfedea0SLionel Sambuc /* Prepare the params */ 943*ebfedea0SLionel Sambuc BN_CTX_start(ctx); 944*ebfedea0SLionel Sambuc dsa_p = BN_CTX_get(ctx); 945*ebfedea0SLionel Sambuc dsa_q = BN_CTX_get(ctx); 946*ebfedea0SLionel Sambuc dsa_g = BN_CTX_get(ctx); 947*ebfedea0SLionel Sambuc dsa_key = BN_CTX_get(ctx); 948*ebfedea0SLionel Sambuc argument = BN_CTX_get(ctx); 949*ebfedea0SLionel Sambuc if(!argument) 950*ebfedea0SLionel Sambuc { 951*ebfedea0SLionel Sambuc CSWIFTerr(CSWIFT_F_CSWIFT_DSA_VERIFY,CSWIFT_R_BN_CTX_FULL); 952*ebfedea0SLionel Sambuc goto err; 953*ebfedea0SLionel Sambuc } 954*ebfedea0SLionel Sambuc if(!bn_wexpand(dsa_p, dsa->p->top) || 955*ebfedea0SLionel Sambuc !bn_wexpand(dsa_q, dsa->q->top) || 956*ebfedea0SLionel Sambuc !bn_wexpand(dsa_g, dsa->g->top) || 957*ebfedea0SLionel Sambuc !bn_wexpand(dsa_key, dsa->pub_key->top) || 958*ebfedea0SLionel Sambuc !bn_wexpand(argument, 40)) 959*ebfedea0SLionel Sambuc { 960*ebfedea0SLionel Sambuc CSWIFTerr(CSWIFT_F_CSWIFT_DSA_VERIFY,CSWIFT_R_BN_EXPAND_FAIL); 961*ebfedea0SLionel Sambuc goto err; 962*ebfedea0SLionel Sambuc } 963*ebfedea0SLionel Sambuc sw_param.type = SW_ALG_DSA; 964*ebfedea0SLionel Sambuc sw_param.up.dsa.p.nbytes = BN_bn2bin(dsa->p, 965*ebfedea0SLionel Sambuc (unsigned char *)dsa_p->d); 966*ebfedea0SLionel Sambuc sw_param.up.dsa.p.value = (unsigned char *)dsa_p->d; 967*ebfedea0SLionel Sambuc sw_param.up.dsa.q.nbytes = BN_bn2bin(dsa->q, 968*ebfedea0SLionel Sambuc (unsigned char *)dsa_q->d); 969*ebfedea0SLionel Sambuc sw_param.up.dsa.q.value = (unsigned char *)dsa_q->d; 970*ebfedea0SLionel Sambuc sw_param.up.dsa.g.nbytes = BN_bn2bin(dsa->g, 971*ebfedea0SLionel Sambuc (unsigned char *)dsa_g->d); 972*ebfedea0SLionel Sambuc sw_param.up.dsa.g.value = (unsigned char *)dsa_g->d; 973*ebfedea0SLionel Sambuc sw_param.up.dsa.key.nbytes = BN_bn2bin(dsa->pub_key, 974*ebfedea0SLionel Sambuc (unsigned char *)dsa_key->d); 975*ebfedea0SLionel Sambuc sw_param.up.dsa.key.value = (unsigned char *)dsa_key->d; 976*ebfedea0SLionel Sambuc /* Attach the key params */ 977*ebfedea0SLionel Sambuc sw_status = p_CSwift_AttachKeyParam(hac, &sw_param); 978*ebfedea0SLionel Sambuc switch(sw_status) 979*ebfedea0SLionel Sambuc { 980*ebfedea0SLionel Sambuc case SW_OK: 981*ebfedea0SLionel Sambuc break; 982*ebfedea0SLionel Sambuc case SW_ERR_INPUT_SIZE: 983*ebfedea0SLionel Sambuc CSWIFTerr(CSWIFT_F_CSWIFT_DSA_VERIFY,CSWIFT_R_BAD_KEY_SIZE); 984*ebfedea0SLionel Sambuc goto err; 985*ebfedea0SLionel Sambuc default: 986*ebfedea0SLionel Sambuc { 987*ebfedea0SLionel Sambuc char tmpbuf[DECIMAL_SIZE(sw_status)+1]; 988*ebfedea0SLionel Sambuc CSWIFTerr(CSWIFT_F_CSWIFT_DSA_VERIFY,CSWIFT_R_REQUEST_FAILED); 989*ebfedea0SLionel Sambuc sprintf(tmpbuf, "%ld", sw_status); 990*ebfedea0SLionel Sambuc ERR_add_error_data(2, "CryptoSwift error number is ",tmpbuf); 991*ebfedea0SLionel Sambuc } 992*ebfedea0SLionel Sambuc goto err; 993*ebfedea0SLionel Sambuc } 994*ebfedea0SLionel Sambuc /* Prepare the argument and response */ 995*ebfedea0SLionel Sambuc arg[0].nbytes = dgst_len; 996*ebfedea0SLionel Sambuc arg[0].value = (unsigned char *)dgst; 997*ebfedea0SLionel Sambuc arg[1].nbytes = 40; 998*ebfedea0SLionel Sambuc arg[1].value = (unsigned char *)argument->d; 999*ebfedea0SLionel Sambuc memset(arg[1].value, 0, 40); 1000*ebfedea0SLionel Sambuc BN_bn2bin(sig->r, arg[1].value + 20 - BN_num_bytes(sig->r)); 1001*ebfedea0SLionel Sambuc BN_bn2bin(sig->s, arg[1].value + 40 - BN_num_bytes(sig->s)); 1002*ebfedea0SLionel Sambuc res.nbytes = 4; /* unsigned long */ 1003*ebfedea0SLionel Sambuc res.value = (unsigned char *)(&sig_result); 1004*ebfedea0SLionel Sambuc /* Perform the operation */ 1005*ebfedea0SLionel Sambuc sw_status = p_CSwift_SimpleRequest(hac, SW_CMD_DSS_VERIFY, arg, 2, 1006*ebfedea0SLionel Sambuc &res, 1); 1007*ebfedea0SLionel Sambuc if(sw_status != SW_OK) 1008*ebfedea0SLionel Sambuc { 1009*ebfedea0SLionel Sambuc char tmpbuf[DECIMAL_SIZE(sw_status)+1]; 1010*ebfedea0SLionel Sambuc CSWIFTerr(CSWIFT_F_CSWIFT_DSA_VERIFY,CSWIFT_R_REQUEST_FAILED); 1011*ebfedea0SLionel Sambuc sprintf(tmpbuf, "%ld", sw_status); 1012*ebfedea0SLionel Sambuc ERR_add_error_data(2, "CryptoSwift error number is ",tmpbuf); 1013*ebfedea0SLionel Sambuc goto err; 1014*ebfedea0SLionel Sambuc } 1015*ebfedea0SLionel Sambuc /* Convert the response */ 1016*ebfedea0SLionel Sambuc to_return = ((sig_result == 0) ? 0 : 1); 1017*ebfedea0SLionel Sambuc 1018*ebfedea0SLionel Sambuc err: 1019*ebfedea0SLionel Sambuc if(acquired) 1020*ebfedea0SLionel Sambuc release_context(hac); 1021*ebfedea0SLionel Sambuc if(ctx) 1022*ebfedea0SLionel Sambuc { 1023*ebfedea0SLionel Sambuc BN_CTX_end(ctx); 1024*ebfedea0SLionel Sambuc BN_CTX_free(ctx); 1025*ebfedea0SLionel Sambuc } 1026*ebfedea0SLionel Sambuc return to_return; 1027*ebfedea0SLionel Sambuc } 1028*ebfedea0SLionel Sambuc #endif 1029*ebfedea0SLionel Sambuc 1030*ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_DH 1031*ebfedea0SLionel Sambuc /* This function is aliased to mod_exp (with the dh and mont dropped). */ 1032*ebfedea0SLionel Sambuc static int cswift_mod_exp_dh(const DH *dh, BIGNUM *r, 1033*ebfedea0SLionel Sambuc const BIGNUM *a, const BIGNUM *p, 1034*ebfedea0SLionel Sambuc const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx) 1035*ebfedea0SLionel Sambuc { 1036*ebfedea0SLionel Sambuc return cswift_mod_exp(r, a, p, m, ctx); 1037*ebfedea0SLionel Sambuc } 1038*ebfedea0SLionel Sambuc #endif 1039*ebfedea0SLionel Sambuc 1040*ebfedea0SLionel Sambuc /* Random bytes are good */ 1041*ebfedea0SLionel Sambuc static int cswift_rand_bytes(unsigned char *buf, int num) 1042*ebfedea0SLionel Sambuc { 1043*ebfedea0SLionel Sambuc SW_CONTEXT_HANDLE hac; 1044*ebfedea0SLionel Sambuc SW_STATUS swrc; 1045*ebfedea0SLionel Sambuc SW_LARGENUMBER largenum; 1046*ebfedea0SLionel Sambuc int acquired = 0; 1047*ebfedea0SLionel Sambuc int to_return = 0; /* assume failure */ 1048*ebfedea0SLionel Sambuc unsigned char buf32[1024]; 1049*ebfedea0SLionel Sambuc 1050*ebfedea0SLionel Sambuc 1051*ebfedea0SLionel Sambuc if (!get_context(&hac)) 1052*ebfedea0SLionel Sambuc { 1053*ebfedea0SLionel Sambuc CSWIFTerr(CSWIFT_F_CSWIFT_RAND_BYTES, CSWIFT_R_UNIT_FAILURE); 1054*ebfedea0SLionel Sambuc goto err; 1055*ebfedea0SLionel Sambuc } 1056*ebfedea0SLionel Sambuc acquired = 1; 1057*ebfedea0SLionel Sambuc 1058*ebfedea0SLionel Sambuc /************************************************************************/ 1059*ebfedea0SLionel Sambuc /* 04/02/2003 */ 1060*ebfedea0SLionel Sambuc /* Modified by Frederic Giudicelli (deny-all.com) to overcome the */ 1061*ebfedea0SLionel Sambuc /* limitation of cswift with values not a multiple of 32 */ 1062*ebfedea0SLionel Sambuc /************************************************************************/ 1063*ebfedea0SLionel Sambuc 1064*ebfedea0SLionel Sambuc while(num >= (int)sizeof(buf32)) 1065*ebfedea0SLionel Sambuc { 1066*ebfedea0SLionel Sambuc largenum.value = buf; 1067*ebfedea0SLionel Sambuc largenum.nbytes = sizeof(buf32); 1068*ebfedea0SLionel Sambuc /* tell CryptoSwift how many bytes we want and where we want it. 1069*ebfedea0SLionel Sambuc * Note: - CryptoSwift cannot do more than 4096 bytes at a time. 1070*ebfedea0SLionel Sambuc * - CryptoSwift can only do multiple of 32-bits. */ 1071*ebfedea0SLionel Sambuc swrc = p_CSwift_SimpleRequest(hac, SW_CMD_RAND, NULL, 0, &largenum, 1); 1072*ebfedea0SLionel Sambuc if (swrc != SW_OK) 1073*ebfedea0SLionel Sambuc { 1074*ebfedea0SLionel Sambuc char tmpbuf[20]; 1075*ebfedea0SLionel Sambuc CSWIFTerr(CSWIFT_F_CSWIFT_RAND_BYTES, CSWIFT_R_REQUEST_FAILED); 1076*ebfedea0SLionel Sambuc sprintf(tmpbuf, "%ld", swrc); 1077*ebfedea0SLionel Sambuc ERR_add_error_data(2, "CryptoSwift error number is ", tmpbuf); 1078*ebfedea0SLionel Sambuc goto err; 1079*ebfedea0SLionel Sambuc } 1080*ebfedea0SLionel Sambuc buf += sizeof(buf32); 1081*ebfedea0SLionel Sambuc num -= sizeof(buf32); 1082*ebfedea0SLionel Sambuc } 1083*ebfedea0SLionel Sambuc if(num) 1084*ebfedea0SLionel Sambuc { 1085*ebfedea0SLionel Sambuc largenum.nbytes = sizeof(buf32); 1086*ebfedea0SLionel Sambuc largenum.value = buf32; 1087*ebfedea0SLionel Sambuc swrc = p_CSwift_SimpleRequest(hac, SW_CMD_RAND, NULL, 0, &largenum, 1); 1088*ebfedea0SLionel Sambuc if (swrc != SW_OK) 1089*ebfedea0SLionel Sambuc { 1090*ebfedea0SLionel Sambuc char tmpbuf[20]; 1091*ebfedea0SLionel Sambuc CSWIFTerr(CSWIFT_F_CSWIFT_RAND_BYTES, CSWIFT_R_REQUEST_FAILED); 1092*ebfedea0SLionel Sambuc sprintf(tmpbuf, "%ld", swrc); 1093*ebfedea0SLionel Sambuc ERR_add_error_data(2, "CryptoSwift error number is ", tmpbuf); 1094*ebfedea0SLionel Sambuc goto err; 1095*ebfedea0SLionel Sambuc } 1096*ebfedea0SLionel Sambuc memcpy(buf, largenum.value, num); 1097*ebfedea0SLionel Sambuc } 1098*ebfedea0SLionel Sambuc 1099*ebfedea0SLionel Sambuc to_return = 1; /* success */ 1100*ebfedea0SLionel Sambuc err: 1101*ebfedea0SLionel Sambuc if (acquired) 1102*ebfedea0SLionel Sambuc release_context(hac); 1103*ebfedea0SLionel Sambuc 1104*ebfedea0SLionel Sambuc return to_return; 1105*ebfedea0SLionel Sambuc } 1106*ebfedea0SLionel Sambuc 1107*ebfedea0SLionel Sambuc static int cswift_rand_status(void) 1108*ebfedea0SLionel Sambuc { 1109*ebfedea0SLionel Sambuc return 1; 1110*ebfedea0SLionel Sambuc } 1111*ebfedea0SLionel Sambuc 1112*ebfedea0SLionel Sambuc 1113*ebfedea0SLionel Sambuc /* This stuff is needed if this ENGINE is being compiled into a self-contained 1114*ebfedea0SLionel Sambuc * shared-library. */ 1115*ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_DYNAMIC_ENGINE 1116*ebfedea0SLionel Sambuc static int bind_fn(ENGINE *e, const char *id) 1117*ebfedea0SLionel Sambuc { 1118*ebfedea0SLionel Sambuc if(id && (strcmp(id, engine_cswift_id) != 0)) 1119*ebfedea0SLionel Sambuc return 0; 1120*ebfedea0SLionel Sambuc if(!bind_helper(e)) 1121*ebfedea0SLionel Sambuc return 0; 1122*ebfedea0SLionel Sambuc return 1; 1123*ebfedea0SLionel Sambuc } 1124*ebfedea0SLionel Sambuc IMPLEMENT_DYNAMIC_CHECK_FN() 1125*ebfedea0SLionel Sambuc IMPLEMENT_DYNAMIC_BIND_FN(bind_fn) 1126*ebfedea0SLionel Sambuc #endif /* OPENSSL_NO_DYNAMIC_ENGINE */ 1127*ebfedea0SLionel Sambuc 1128*ebfedea0SLionel Sambuc #endif /* !OPENSSL_NO_HW_CSWIFT */ 1129*ebfedea0SLionel Sambuc #endif /* !OPENSSL_NO_HW */ 1130