e_cswift.c revision 296465
11573Srgrimes/* crypto/engine/hw_cswift.c */ 21573Srgrimes/* 31573Srgrimes * Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL project 41573Srgrimes * 2000. 51573Srgrimes */ 61573Srgrimes/* ==================================================================== 71573Srgrimes * Copyright (c) 1999-2001 The OpenSSL Project. All rights reserved. 81573Srgrimes * 91573Srgrimes * Redistribution and use in source and binary forms, with or without 101573Srgrimes * modification, are permitted provided that the following conditions 111573Srgrimes * are met: 121573Srgrimes * 131573Srgrimes * 1. Redistributions of source code must retain the above copyright 141573Srgrimes * notice, this list of conditions and the following disclaimer. 151573Srgrimes * 161573Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 171573Srgrimes * notice, this list of conditions and the following disclaimer in 181573Srgrimes * the documentation and/or other materials provided with the 191573Srgrimes * distribution. 201573Srgrimes * 211573Srgrimes * 3. All advertising materials mentioning features or use of this 221573Srgrimes * software must display the following acknowledgment: 231573Srgrimes * "This product includes software developed by the OpenSSL Project 241573Srgrimes * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" 251573Srgrimes * 261573Srgrimes * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 271573Srgrimes * endorse or promote products derived from this software without 281573Srgrimes * prior written permission. For written permission, please contact 291573Srgrimes * licensing@OpenSSL.org. 301573Srgrimes * 311573Srgrimes * 5. Products derived from this software may not be called "OpenSSL" 321573Srgrimes * nor may "OpenSSL" appear in their names without prior written 331573Srgrimes * permission of the OpenSSL Project. 341573Srgrimes * 351573Srgrimes * 6. Redistributions of any form whatsoever must retain the following 361573Srgrimes * acknowledgment: 371573Srgrimes * "This product includes software developed by the OpenSSL Project 381573Srgrimes * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" 391573Srgrimes * 401573Srgrimes * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 411573Srgrimes * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 428870Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 431573Srgrimes * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 441573Srgrimes * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 451573Srgrimes * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 461573Srgrimes * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 471573Srgrimes * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 481573Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 491573Srgrimes * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 501573Srgrimes * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 511573Srgrimes * OF THE POSSIBILITY OF SUCH DAMAGE. 521573Srgrimes * ==================================================================== 531573Srgrimes * 541573Srgrimes * This product includes cryptographic software written by Eric Young 551573Srgrimes * (eay@cryptsoft.com). This product includes software written by Tim 561573Srgrimes * Hudson (tjh@cryptsoft.com). 571573Srgrimes * 581573Srgrimes */ 591573Srgrimes 601573Srgrimes#include <stdio.h> 611573Srgrimes#include <string.h> 621573Srgrimes#include <openssl/crypto.h> 631573Srgrimes#include <openssl/buffer.h> 641573Srgrimes#include <openssl/dso.h> 651573Srgrimes#include <openssl/engine.h> 661573Srgrimes#ifndef OPENSSL_NO_RSA 671573Srgrimes# include <openssl/rsa.h> 681573Srgrimes#endif 691573Srgrimes#ifndef OPENSSL_NO_DSA 701573Srgrimes# include <openssl/dsa.h> 711573Srgrimes#endif 721573Srgrimes#ifndef OPENSSL_NO_DH 731573Srgrimes# include <openssl/dh.h> 741573Srgrimes#endif 751573Srgrimes#include <openssl/rand.h> 761573Srgrimes#include <openssl/bn.h> 771573Srgrimes 781573Srgrimes#ifndef OPENSSL_NO_HW 791573Srgrimes# ifndef OPENSSL_NO_HW_CSWIFT 801573Srgrimes 811573Srgrimes/* 821573Srgrimes * Attribution notice: Rainbow have generously allowed me to reproduce the 831573Srgrimes * necessary definitions here from their API. This means the support can 841573Srgrimes * build independently of whether application builders have the API or 851573Srgrimes * hardware. This will allow developers to easily produce software that has 861573Srgrimes * latent hardware support for any users that have accelerators installed, 871573Srgrimes * without the developers themselves needing anything extra. I have only 881573Srgrimes * clipped the parts from the CryptoSwift header files that are (or seem) 891573Srgrimes * relevant to the CryptoSwift support code. This is simply to keep the file 901573Srgrimes * sizes reasonable. [Geoff] 911573Srgrimes */ 921573Srgrimes# ifdef FLAT_INC 931573Srgrimes# include "cswift.h" 941573Srgrimes# else 951573Srgrimes# include "vendor_defns/cswift.h" 961573Srgrimes# endif 971573Srgrimes 981573Srgrimes# define CSWIFT_LIB_NAME "cswift engine" 991573Srgrimes# include "e_cswift_err.c" 1001573Srgrimes 1011573Srgrimes# define DECIMAL_SIZE(type) ((sizeof(type)*8+2)/3+1) 1021573Srgrimes 1031573Srgrimesstatic int cswift_destroy(ENGINE *e); 1041573Srgrimesstatic int cswift_init(ENGINE *e); 1051573Srgrimesstatic int cswift_finish(ENGINE *e); 1061573Srgrimesstatic int cswift_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f) (void)); 1071573Srgrimes# ifndef OPENSSL_NO_RSA 1081573Srgrimesstatic int cswift_bn_32copy(SW_LARGENUMBER *out, const BIGNUM *in); 1091573Srgrimes# endif 1101573Srgrimes 1111573Srgrimes/* BIGNUM stuff */ 1121573Srgrimesstatic int cswift_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, 1131573Srgrimes const BIGNUM *m, BN_CTX *ctx); 1141573Srgrimes# ifndef OPENSSL_NO_RSA 1151573Srgrimesstatic int cswift_mod_exp_crt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, 1161573Srgrimes const BIGNUM *q, const BIGNUM *dmp1, 1171573Srgrimes const BIGNUM *dmq1, const BIGNUM *iqmp, 1181573Srgrimes BN_CTX *ctx); 1191573Srgrimes# endif 1201573Srgrimes 1211573Srgrimes# ifndef OPENSSL_NO_RSA 1221573Srgrimes/* RSA stuff */ 1231573Srgrimesstatic int cswift_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, 1241573Srgrimes BN_CTX *ctx); 1251573Srgrimes/* This function is aliased to mod_exp (with the mont stuff dropped). */ 1261573Srgrimesstatic int cswift_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, 1271573Srgrimes const BIGNUM *m, BN_CTX *ctx, 1281573Srgrimes BN_MONT_CTX *m_ctx); 1291573Srgrimes# endif 1301573Srgrimes 1311573Srgrimes# ifndef OPENSSL_NO_DSA 1321573Srgrimes/* DSA stuff */ 1331573Srgrimesstatic DSA_SIG *cswift_dsa_sign(const unsigned char *dgst, int dlen, 1341573Srgrimes DSA *dsa); 1351573Srgrimesstatic int cswift_dsa_verify(const unsigned char *dgst, int dgst_len, 1361573Srgrimes DSA_SIG *sig, DSA *dsa); 1371573Srgrimes# endif 1381573Srgrimes 1391573Srgrimes# ifndef OPENSSL_NO_DH 1401573Srgrimes/* DH stuff */ 1411573Srgrimes/* This function is alised to mod_exp (with the DH and mont dropped). */ 1421573Srgrimesstatic int cswift_mod_exp_dh(const DH *dh, BIGNUM *r, 1431573Srgrimes const BIGNUM *a, const BIGNUM *p, 1441573Srgrimes const BIGNUM *m, BN_CTX *ctx, 1451573Srgrimes BN_MONT_CTX *m_ctx); 1461573Srgrimes# endif 1471573Srgrimes 1481573Srgrimes/* RAND stuff */ 1491573Srgrimesstatic int cswift_rand_bytes(unsigned char *buf, int num); 1501573Srgrimesstatic int cswift_rand_status(void); 1511573Srgrimes 1521573Srgrimes/* The definitions for control commands specific to this engine */ 1531573Srgrimes# define CSWIFT_CMD_SO_PATH ENGINE_CMD_BASE 1541573Srgrimesstatic const ENGINE_CMD_DEFN cswift_cmd_defns[] = { 1551573Srgrimes {CSWIFT_CMD_SO_PATH, 1561573Srgrimes "SO_PATH", 1571573Srgrimes "Specifies the path to the 'cswift' shared library", 1581573Srgrimes ENGINE_CMD_FLAG_STRING}, 1591573Srgrimes {0, NULL, NULL, 0} 1601573Srgrimes}; 1611573Srgrimes 1621573Srgrimes# ifndef OPENSSL_NO_RSA 1631573Srgrimes/* Our internal RSA_METHOD that we provide pointers to */ 1641573Srgrimesstatic RSA_METHOD cswift_rsa = { 1651573Srgrimes "CryptoSwift RSA method", 1661573Srgrimes NULL, 1671573Srgrimes NULL, 1681573Srgrimes NULL, 1691573Srgrimes NULL, 1701573Srgrimes cswift_rsa_mod_exp, 1711573Srgrimes cswift_mod_exp_mont, 1721573Srgrimes NULL, 1731573Srgrimes NULL, 1741573Srgrimes 0, 1751573Srgrimes NULL, 1761573Srgrimes NULL, 1771573Srgrimes NULL, 1781573Srgrimes NULL 1791573Srgrimes}; 1801573Srgrimes# endif 1811573Srgrimes 1821573Srgrimes# ifndef OPENSSL_NO_DSA 1831573Srgrimes/* Our internal DSA_METHOD that we provide pointers to */ 1841573Srgrimesstatic DSA_METHOD cswift_dsa = { 1851573Srgrimes "CryptoSwift DSA method", 1861573Srgrimes cswift_dsa_sign, 1871573Srgrimes NULL, /* dsa_sign_setup */ 1881573Srgrimes cswift_dsa_verify, 1891573Srgrimes NULL, /* dsa_mod_exp */ 1901573Srgrimes NULL, /* bn_mod_exp */ 1911573Srgrimes NULL, /* init */ 1921573Srgrimes NULL, /* finish */ 1931573Srgrimes 0, /* flags */ 1941573Srgrimes NULL, /* app_data */ 1951573Srgrimes NULL, /* dsa_paramgen */ 1961573Srgrimes NULL /* dsa_keygen */ 1971573Srgrimes}; 1981573Srgrimes# endif 1991573Srgrimes 2001573Srgrimes# ifndef OPENSSL_NO_DH 2011573Srgrimes/* Our internal DH_METHOD that we provide pointers to */ 2021573Srgrimesstatic DH_METHOD cswift_dh = { 2031573Srgrimes "CryptoSwift DH method", 2041573Srgrimes NULL, 2051573Srgrimes NULL, 2061573Srgrimes cswift_mod_exp_dh, 2071573Srgrimes NULL, 2081573Srgrimes NULL, 2091573Srgrimes 0, 2101573Srgrimes NULL, 2111573Srgrimes NULL 2121573Srgrimes}; 2131573Srgrimes# endif 2141573Srgrimes 2151573Srgrimesstatic RAND_METHOD cswift_random = { 2161573Srgrimes /* "CryptoSwift RAND method", */ 2171573Srgrimes NULL, 2181573Srgrimes cswift_rand_bytes, 2191573Srgrimes NULL, 2201573Srgrimes NULL, 2211573Srgrimes cswift_rand_bytes, 2221573Srgrimes cswift_rand_status, 2231573Srgrimes}; 2241573Srgrimes 2251573Srgrimes/* Constants used when creating the ENGINE */ 2261573Srgrimesstatic const char *engine_cswift_id = "cswift"; 2271573Srgrimesstatic const char *engine_cswift_name = "CryptoSwift hardware engine support"; 2281573Srgrimes 2291573Srgrimes/* 2301573Srgrimes * This internal function is used by ENGINE_cswift() and possibly by the 2311573Srgrimes * "dynamic" ENGINE support too 2321573Srgrimes */ 2331573Srgrimesstatic int bind_helper(ENGINE *e) 2341573Srgrimes{ 2351573Srgrimes# ifndef OPENSSL_NO_RSA 2361573Srgrimes const RSA_METHOD *meth1; 2371573Srgrimes# endif 2381573Srgrimes# ifndef OPENSSL_NO_DH 2391573Srgrimes const DH_METHOD *meth2; 2401573Srgrimes# endif 2411573Srgrimes if (!ENGINE_set_id(e, engine_cswift_id) || 2421573Srgrimes !ENGINE_set_name(e, engine_cswift_name) || 2431573Srgrimes# ifndef OPENSSL_NO_RSA 2441573Srgrimes !ENGINE_set_RSA(e, &cswift_rsa) || 2451573Srgrimes# endif 2461573Srgrimes# ifndef OPENSSL_NO_DSA 2471573Srgrimes !ENGINE_set_DSA(e, &cswift_dsa) || 2481573Srgrimes# endif 2491573Srgrimes# ifndef OPENSSL_NO_DH 2501573Srgrimes !ENGINE_set_DH(e, &cswift_dh) || 2511573Srgrimes# endif 2521573Srgrimes !ENGINE_set_RAND(e, &cswift_random) || 2531573Srgrimes !ENGINE_set_destroy_function(e, cswift_destroy) || 2541573Srgrimes !ENGINE_set_init_function(e, cswift_init) || 2551573Srgrimes !ENGINE_set_finish_function(e, cswift_finish) || 2561573Srgrimes !ENGINE_set_ctrl_function(e, cswift_ctrl) || 2571573Srgrimes !ENGINE_set_cmd_defns(e, cswift_cmd_defns)) 2581573Srgrimes return 0; 2591573Srgrimes 2601573Srgrimes# ifndef OPENSSL_NO_RSA 2611573Srgrimes /* 2621573Srgrimes * We know that the "PKCS1_SSLeay()" functions hook properly to the 2631573Srgrimes * cswift-specific mod_exp and mod_exp_crt so we use those functions. NB: 2641573Srgrimes * We don't use ENGINE_openssl() or anything "more generic" because 2651573Srgrimes * something like the RSAref code may not hook properly, and if you own 2661573Srgrimes * one of these cards then you have the right to do RSA operations on it 2671573Srgrimes * anyway! 2681573Srgrimes */ 2691573Srgrimes meth1 = RSA_PKCS1_SSLeay(); 2701573Srgrimes cswift_rsa.rsa_pub_enc = meth1->rsa_pub_enc; 2711573Srgrimes cswift_rsa.rsa_pub_dec = meth1->rsa_pub_dec; 2721573Srgrimes cswift_rsa.rsa_priv_enc = meth1->rsa_priv_enc; 2731573Srgrimes cswift_rsa.rsa_priv_dec = meth1->rsa_priv_dec; 2741573Srgrimes# endif 2751573Srgrimes 2761573Srgrimes# ifndef OPENSSL_NO_DH 2771573Srgrimes /* Much the same for Diffie-Hellman */ 2781573Srgrimes meth2 = DH_OpenSSL(); 2791573Srgrimes cswift_dh.generate_key = meth2->generate_key; 2801573Srgrimes cswift_dh.compute_key = meth2->compute_key; 2811573Srgrimes# endif 2821573Srgrimes 2831573Srgrimes /* Ensure the cswift error handling is set up */ 2841573Srgrimes ERR_load_CSWIFT_strings(); 2851573Srgrimes return 1; 2861573Srgrimes} 2871573Srgrimes 2881573Srgrimes# ifdef OPENSSL_NO_DYNAMIC_ENGINE 2891573Srgrimesstatic ENGINE *engine_cswift(void) 2901573Srgrimes{ 2911573Srgrimes ENGINE *ret = ENGINE_new(); 2921573Srgrimes if (!ret) 2931573Srgrimes return NULL; 2941573Srgrimes if (!bind_helper(ret)) { 2951573Srgrimes ENGINE_free(ret); 2961573Srgrimes return NULL; 2971573Srgrimes } 2981573Srgrimes return ret; 2991573Srgrimes} 3001573Srgrimes 3011573Srgrimesvoid ENGINE_load_cswift(void) 3021573Srgrimes{ 3031573Srgrimes /* Copied from eng_[openssl|dyn].c */ 3041573Srgrimes ENGINE *toadd = engine_cswift(); 3051573Srgrimes if (!toadd) 3061573Srgrimes return; 3071573Srgrimes ENGINE_add(toadd); 3081573Srgrimes ENGINE_free(toadd); 3091573Srgrimes ERR_clear_error(); 3101573Srgrimes} 3111573Srgrimes# endif 3121573Srgrimes 3131573Srgrimes/* 3141573Srgrimes * This is a process-global DSO handle used for loading and unloading the 3151573Srgrimes * CryptoSwift library. NB: This is only set (or unset) during an init() or 3161573Srgrimes * finish() call (reference counts permitting) and they're operating with 3171573Srgrimes * global locks, so this should be thread-safe implicitly. 3181573Srgrimes */ 3191573Srgrimesstatic DSO *cswift_dso = NULL; 3201573Srgrimes 3211573Srgrimes/* 3221573Srgrimes * These are the function pointers that are (un)set when the library has 3231573Srgrimes * successfully (un)loaded. 3241573Srgrimes */ 3251573Srgrimest_swAcquireAccContext *p_CSwift_AcquireAccContext = NULL; 3261573Srgrimest_swAttachKeyParam *p_CSwift_AttachKeyParam = NULL; 3271573Srgrimest_swSimpleRequest *p_CSwift_SimpleRequest = NULL; 3281573Srgrimest_swReleaseAccContext *p_CSwift_ReleaseAccContext = NULL; 3291573Srgrimes 3301573Srgrimes/* Used in the DSO operations. */ 3311573Srgrimesstatic const char *CSWIFT_LIBNAME = NULL; 3321573Srgrimesstatic const char *get_CSWIFT_LIBNAME(void) 3331573Srgrimes{ 3341573Srgrimes if (CSWIFT_LIBNAME) 3351573Srgrimes return CSWIFT_LIBNAME; 3361573Srgrimes return "swift"; 3371573Srgrimes} 3381573Srgrimes 3391573Srgrimesstatic void free_CSWIFT_LIBNAME(void) 3401573Srgrimes{ 3411573Srgrimes if (CSWIFT_LIBNAME) 3421573Srgrimes OPENSSL_free((void *)CSWIFT_LIBNAME); 3431573Srgrimes CSWIFT_LIBNAME = NULL; 3441573Srgrimes} 3451573Srgrimes 3461573Srgrimesstatic long set_CSWIFT_LIBNAME(const char *name) 3471573Srgrimes{ 3481573Srgrimes free_CSWIFT_LIBNAME(); 3491573Srgrimes return (((CSWIFT_LIBNAME = BUF_strdup(name)) != NULL) ? 1 : 0); 3501573Srgrimes} 3511573Srgrimes 3521573Srgrimesstatic const char *CSWIFT_F1 = "swAcquireAccContext"; 3531573Srgrimesstatic const char *CSWIFT_F2 = "swAttachKeyParam"; 3541573Srgrimesstatic const char *CSWIFT_F3 = "swSimpleRequest"; 3551573Srgrimesstatic const char *CSWIFT_F4 = "swReleaseAccContext"; 3561573Srgrimes 3571573Srgrimes/* 3581573Srgrimes * CryptoSwift library functions and mechanics - these are used by the 3591573Srgrimes * higher-level functions further down. NB: As and where there's no error 3601573Srgrimes * checking, take a look lower down where these functions are called, the 3611573Srgrimes * checking and error handling is probably down there. 3621573Srgrimes */ 3631573Srgrimes 3641573Srgrimes/* utility function to obtain a context */ 3651573Srgrimesstatic int get_context(SW_CONTEXT_HANDLE *hac) 3661573Srgrimes{ 3671573Srgrimes SW_STATUS status; 3681573Srgrimes 3691573Srgrimes status = p_CSwift_AcquireAccContext(hac); 3701573Srgrimes if (status != SW_OK) 3711573Srgrimes return 0; 3721573Srgrimes return 1; 3731573Srgrimes} 3741573Srgrimes 3751573Srgrimes/* similarly to release one. */ 3761573Srgrimesstatic void release_context(SW_CONTEXT_HANDLE hac) 3771573Srgrimes{ 3781573Srgrimes p_CSwift_ReleaseAccContext(hac); 3791573Srgrimes} 3801573Srgrimes 3811573Srgrimes/* Destructor (complements the "ENGINE_cswift()" constructor) */ 3821573Srgrimesstatic int cswift_destroy(ENGINE *e) 3831573Srgrimes{ 3841573Srgrimes free_CSWIFT_LIBNAME(); 3851573Srgrimes ERR_unload_CSWIFT_strings(); 3861573Srgrimes return 1; 3871573Srgrimes} 3881573Srgrimes 3891573Srgrimes/* (de)initialisation functions. */ 3901573Srgrimesstatic int cswift_init(ENGINE *e) 3911573Srgrimes{ 3921573Srgrimes SW_CONTEXT_HANDLE hac; 3931573Srgrimes t_swAcquireAccContext *p1; 3941573Srgrimes t_swAttachKeyParam *p2; 3951573Srgrimes t_swSimpleRequest *p3; 3961573Srgrimes t_swReleaseAccContext *p4; 3971573Srgrimes 3981573Srgrimes if (cswift_dso != NULL) { 3991573Srgrimes CSWIFTerr(CSWIFT_F_CSWIFT_INIT, CSWIFT_R_ALREADY_LOADED); 4001573Srgrimes goto err; 4011573Srgrimes } 4021573Srgrimes /* Attempt to load libswift.so/swift.dll/whatever. */ 4031573Srgrimes cswift_dso = DSO_load(NULL, get_CSWIFT_LIBNAME(), NULL, 0); 4041573Srgrimes if (cswift_dso == NULL) { 4051573Srgrimes CSWIFTerr(CSWIFT_F_CSWIFT_INIT, CSWIFT_R_NOT_LOADED); 4061573Srgrimes goto err; 4071573Srgrimes } 4081573Srgrimes if (!(p1 = (t_swAcquireAccContext *) 4091573Srgrimes DSO_bind_func(cswift_dso, CSWIFT_F1)) || 4101573Srgrimes !(p2 = (t_swAttachKeyParam *) 4111573Srgrimes DSO_bind_func(cswift_dso, CSWIFT_F2)) || 4121573Srgrimes !(p3 = (t_swSimpleRequest *) 4131573Srgrimes DSO_bind_func(cswift_dso, CSWIFT_F3)) || 4141573Srgrimes !(p4 = (t_swReleaseAccContext *) 4151573Srgrimes DSO_bind_func(cswift_dso, CSWIFT_F4))) { 4161573Srgrimes CSWIFTerr(CSWIFT_F_CSWIFT_INIT, CSWIFT_R_NOT_LOADED); 4171573Srgrimes goto err; 4181573Srgrimes } 4191573Srgrimes /* Copy the pointers */ 4201573Srgrimes p_CSwift_AcquireAccContext = p1; 4211573Srgrimes p_CSwift_AttachKeyParam = p2; 4221573Srgrimes p_CSwift_SimpleRequest = p3; 4231573Srgrimes p_CSwift_ReleaseAccContext = p4; 4241573Srgrimes /* 4251573Srgrimes * Try and get a context - if not, we may have a DSO but no accelerator! 4261573Srgrimes */ 4271573Srgrimes if (!get_context(&hac)) { 4281573Srgrimes CSWIFTerr(CSWIFT_F_CSWIFT_INIT, CSWIFT_R_UNIT_FAILURE); 4291573Srgrimes goto err; 4301573Srgrimes } 4311573Srgrimes release_context(hac); 4321573Srgrimes /* Everything's fine. */ 4331573Srgrimes return 1; 4341573Srgrimes err: 4351573Srgrimes if (cswift_dso) { 4361573Srgrimes DSO_free(cswift_dso); 4371573Srgrimes cswift_dso = NULL; 4381573Srgrimes } 4391573Srgrimes p_CSwift_AcquireAccContext = NULL; 4401573Srgrimes p_CSwift_AttachKeyParam = NULL; 4411573Srgrimes p_CSwift_SimpleRequest = NULL; 4421573Srgrimes p_CSwift_ReleaseAccContext = NULL; 4431573Srgrimes return 0; 4441573Srgrimes} 4451573Srgrimes 4461573Srgrimesstatic int cswift_finish(ENGINE *e) 4471573Srgrimes{ 4481573Srgrimes free_CSWIFT_LIBNAME(); 4491573Srgrimes if (cswift_dso == NULL) { 4501573Srgrimes CSWIFTerr(CSWIFT_F_CSWIFT_FINISH, CSWIFT_R_NOT_LOADED); 4511573Srgrimes return 0; 4521573Srgrimes } 4531573Srgrimes if (!DSO_free(cswift_dso)) { 4541573Srgrimes CSWIFTerr(CSWIFT_F_CSWIFT_FINISH, CSWIFT_R_UNIT_FAILURE); 4551573Srgrimes return 0; 4561573Srgrimes } 4571573Srgrimes cswift_dso = NULL; 4581573Srgrimes p_CSwift_AcquireAccContext = NULL; 4591573Srgrimes p_CSwift_AttachKeyParam = NULL; 4601573Srgrimes p_CSwift_SimpleRequest = NULL; 4611573Srgrimes p_CSwift_ReleaseAccContext = NULL; 4621573Srgrimes return 1; 4631573Srgrimes} 4641573Srgrimes 4651573Srgrimesstatic int cswift_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f) (void)) 4661573Srgrimes{ 4671573Srgrimes int initialised = ((cswift_dso == NULL) ? 0 : 1); 4681573Srgrimes switch (cmd) { 4691573Srgrimes case CSWIFT_CMD_SO_PATH: 4701573Srgrimes if (p == NULL) { 4711573Srgrimes CSWIFTerr(CSWIFT_F_CSWIFT_CTRL, ERR_R_PASSED_NULL_PARAMETER); 4721573Srgrimes return 0; 4731573Srgrimes } 4741573Srgrimes if (initialised) { 4751573Srgrimes CSWIFTerr(CSWIFT_F_CSWIFT_CTRL, CSWIFT_R_ALREADY_LOADED); 4761573Srgrimes return 0; 4771573Srgrimes } 4781573Srgrimes return set_CSWIFT_LIBNAME((const char *)p); 4791573Srgrimes default: 4801573Srgrimes break; 4811573Srgrimes } 4821573Srgrimes CSWIFTerr(CSWIFT_F_CSWIFT_CTRL, CSWIFT_R_CTRL_COMMAND_NOT_IMPLEMENTED); 4831573Srgrimes return 0; 4841573Srgrimes} 4851573Srgrimes 4861573Srgrimes/* Un petit mod_exp */ 4871573Srgrimesstatic int cswift_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, 4881573Srgrimes const BIGNUM *m, BN_CTX *ctx) 4891573Srgrimes{ 4901573Srgrimes /* 4911573Srgrimes * I need somewhere to store temporary serialised values for use with the 4921573Srgrimes * CryptoSwift API calls. A neat cheat - I'll use BIGNUMs from the BN_CTX 4931573Srgrimes * but access their arrays directly as byte arrays <grin>. This way I 4941573Srgrimes * don't have to clean anything up. 4951573Srgrimes */ 4961573Srgrimes BIGNUM *modulus; 4971573Srgrimes BIGNUM *exponent; 4981573Srgrimes BIGNUM *argument; 4991573Srgrimes BIGNUM *result; 5001573Srgrimes SW_STATUS sw_status; 5011573Srgrimes SW_LARGENUMBER arg, res; 5021573Srgrimes SW_PARAM sw_param; 5031573Srgrimes SW_CONTEXT_HANDLE hac; 5041573Srgrimes int to_return, acquired; 5051573Srgrimes 5061573Srgrimes modulus = exponent = argument = result = NULL; 5071573Srgrimes to_return = 0; /* expect failure */ 5081573Srgrimes acquired = 0; 5091573Srgrimes 5101573Srgrimes if (!get_context(&hac)) { 5111573Srgrimes CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP, CSWIFT_R_UNIT_FAILURE); 5121573Srgrimes goto err; 5131573Srgrimes } 5141573Srgrimes acquired = 1; 5151573Srgrimes /* Prepare the params */ 5161573Srgrimes BN_CTX_start(ctx); 5171573Srgrimes modulus = BN_CTX_get(ctx); 5181573Srgrimes exponent = BN_CTX_get(ctx); 5191573Srgrimes argument = BN_CTX_get(ctx); 5201573Srgrimes result = BN_CTX_get(ctx); 5211573Srgrimes if (!result) { 5221573Srgrimes CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP, CSWIFT_R_BN_CTX_FULL); 5231573Srgrimes goto err; 5241573Srgrimes } 5251573Srgrimes if (!bn_wexpand(modulus, m->top) || !bn_wexpand(exponent, p->top) || 5261573Srgrimes !bn_wexpand(argument, a->top) || !bn_wexpand(result, m->top)) { 5271573Srgrimes CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP, CSWIFT_R_BN_EXPAND_FAIL); 5281573Srgrimes goto err; 5291573Srgrimes } 5301573Srgrimes sw_param.type = SW_ALG_EXP; 5311573Srgrimes sw_param.up.exp.modulus.nbytes = BN_bn2bin(m, 5321573Srgrimes (unsigned char *)modulus->d); 5331573Srgrimes sw_param.up.exp.modulus.value = (unsigned char *)modulus->d; 5341573Srgrimes sw_param.up.exp.exponent.nbytes = BN_bn2bin(p, 5351573Srgrimes (unsigned char *)exponent->d); 5361573Srgrimes sw_param.up.exp.exponent.value = (unsigned char *)exponent->d; 5371573Srgrimes /* Attach the key params */ 5381573Srgrimes sw_status = p_CSwift_AttachKeyParam(hac, &sw_param); 5391573Srgrimes switch (sw_status) { 5401573Srgrimes case SW_OK: 5411573Srgrimes break; 5421573Srgrimes case SW_ERR_INPUT_SIZE: 5431573Srgrimes CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP, CSWIFT_R_BAD_KEY_SIZE); 5441573Srgrimes goto err; 5451573Srgrimes default: 5461573Srgrimes { 5471573Srgrimes char tmpbuf[DECIMAL_SIZE(sw_status) + 1]; 5481573Srgrimes CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP, CSWIFT_R_REQUEST_FAILED); 5491573Srgrimes sprintf(tmpbuf, "%ld", sw_status); 5501573Srgrimes ERR_add_error_data(2, "CryptoSwift error number is ", tmpbuf); 5511573Srgrimes } 5521573Srgrimes goto err; 5531573Srgrimes } 5541573Srgrimes /* Prepare the argument and response */ 5551573Srgrimes arg.nbytes = BN_bn2bin(a, (unsigned char *)argument->d); 5561573Srgrimes arg.value = (unsigned char *)argument->d; 5571573Srgrimes res.nbytes = BN_num_bytes(m); 5581573Srgrimes memset(result->d, 0, res.nbytes); 5591573Srgrimes res.value = (unsigned char *)result->d; 5601573Srgrimes /* Perform the operation */ 5611573Srgrimes if ((sw_status = p_CSwift_SimpleRequest(hac, SW_CMD_MODEXP, &arg, 1, 5621573Srgrimes &res, 1)) != SW_OK) { 5631573Srgrimes char tmpbuf[DECIMAL_SIZE(sw_status) + 1]; 5641573Srgrimes CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP, CSWIFT_R_REQUEST_FAILED); 5651573Srgrimes sprintf(tmpbuf, "%ld", sw_status); 5661573Srgrimes ERR_add_error_data(2, "CryptoSwift error number is ", tmpbuf); 5671573Srgrimes goto err; 5681573Srgrimes } 5691573Srgrimes /* Convert the response */ 5701573Srgrimes BN_bin2bn((unsigned char *)result->d, res.nbytes, r); 5711573Srgrimes to_return = 1; 5721573Srgrimes err: 5731573Srgrimes if (acquired) 5741573Srgrimes release_context(hac); 5751573Srgrimes BN_CTX_end(ctx); 5761573Srgrimes return to_return; 5771573Srgrimes} 5781573Srgrimes 5791573Srgrimes# ifndef OPENSSL_NO_RSA 5801573Srgrimesint cswift_bn_32copy(SW_LARGENUMBER *out, const BIGNUM *in) 5811573Srgrimes{ 5821573Srgrimes int mod; 5831573Srgrimes int numbytes = BN_num_bytes(in); 5841573Srgrimes 5851573Srgrimes mod = 0; 5861573Srgrimes while (((out->nbytes = (numbytes + mod)) % 32)) { 5871573Srgrimes mod++; 5881573Srgrimes } 5891573Srgrimes out->value = (unsigned char *)OPENSSL_malloc(out->nbytes); 5901573Srgrimes if (!out->value) { 5911573Srgrimes return 0; 5921573Srgrimes } 5931573Srgrimes BN_bn2bin(in, &out->value[mod]); 5941573Srgrimes if (mod) 5951573Srgrimes memset(out->value, 0, mod); 5961573Srgrimes 5971573Srgrimes return 1; 5981573Srgrimes} 5991573Srgrimes# endif 6001573Srgrimes 6011573Srgrimes# ifndef OPENSSL_NO_RSA 6021573Srgrimes/* Un petit mod_exp chinois */ 6031573Srgrimesstatic int cswift_mod_exp_crt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, 6041573Srgrimes const BIGNUM *q, const BIGNUM *dmp1, 6051573Srgrimes const BIGNUM *dmq1, const BIGNUM *iqmp, 6061573Srgrimes BN_CTX *ctx) 6071573Srgrimes{ 6081573Srgrimes SW_STATUS sw_status; 6091573Srgrimes SW_LARGENUMBER arg, res; 6101573Srgrimes SW_PARAM sw_param; 6111573Srgrimes SW_CONTEXT_HANDLE hac; 6121573Srgrimes BIGNUM *result = NULL; 6131573Srgrimes BIGNUM *argument = NULL; 6141573Srgrimes int to_return = 0; /* expect failure */ 6151573Srgrimes int acquired = 0; 6161573Srgrimes 6171573Srgrimes sw_param.up.crt.p.value = NULL; 6181573Srgrimes sw_param.up.crt.q.value = NULL; 6191573Srgrimes sw_param.up.crt.dmp1.value = NULL; 6201573Srgrimes sw_param.up.crt.dmq1.value = NULL; 6211573Srgrimes sw_param.up.crt.iqmp.value = NULL; 6221573Srgrimes 6231573Srgrimes if (!get_context(&hac)) { 6241573Srgrimes CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT, CSWIFT_R_UNIT_FAILURE); 6251573Srgrimes goto err; 6261573Srgrimes } 6271573Srgrimes acquired = 1; 6281573Srgrimes 6291573Srgrimes /* Prepare the params */ 6301573Srgrimes argument = BN_new(); 6311573Srgrimes result = BN_new(); 6321573Srgrimes if (!result || !argument) { 6331573Srgrimes CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT, CSWIFT_R_BN_CTX_FULL); 6341573Srgrimes goto err; 6351573Srgrimes } 6361573Srgrimes 6371573Srgrimes sw_param.type = SW_ALG_CRT; 6381573Srgrimes /************************************************************************/ 6391573Srgrimes /* 6401573Srgrimes * 04/02/2003 6411573Srgrimes */ 6421573Srgrimes /* 6431573Srgrimes * Modified by Frederic Giudicelli (deny-all.com) to overcome the 6441573Srgrimes */ 6451573Srgrimes /* 6461573Srgrimes * limitation of cswift with values not a multiple of 32 6471573Srgrimes */ 6481573Srgrimes /************************************************************************/ 6491573Srgrimes if (!cswift_bn_32copy(&sw_param.up.crt.p, p)) { 6501573Srgrimes CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT, CSWIFT_R_BN_EXPAND_FAIL); 6511573Srgrimes goto err; 6521573Srgrimes } 6531573Srgrimes if (!cswift_bn_32copy(&sw_param.up.crt.q, q)) { 6541573Srgrimes CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT, CSWIFT_R_BN_EXPAND_FAIL); 6551573Srgrimes goto err; 6561573Srgrimes } 6571573Srgrimes if (!cswift_bn_32copy(&sw_param.up.crt.dmp1, dmp1)) { 6581573Srgrimes CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT, CSWIFT_R_BN_EXPAND_FAIL); 6591573Srgrimes goto err; 6601573Srgrimes } 6611573Srgrimes if (!cswift_bn_32copy(&sw_param.up.crt.dmq1, dmq1)) { 6621573Srgrimes CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT, CSWIFT_R_BN_EXPAND_FAIL); 6638870Srgrimes goto err; 6648870Srgrimes } 6651573Srgrimes if (!cswift_bn_32copy(&sw_param.up.crt.iqmp, iqmp)) { 6661573Srgrimes CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT, CSWIFT_R_BN_EXPAND_FAIL); 6671573Srgrimes goto err; 6681573Srgrimes } 6691573Srgrimes if (!bn_wexpand(argument, a->top) || !bn_wexpand(result, p->top + q->top)) { 6701573Srgrimes CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT, CSWIFT_R_BN_EXPAND_FAIL); 6711573Srgrimes goto err; 6721573Srgrimes } 6731573Srgrimes 6741573Srgrimes /* Attach the key params */ 6751573Srgrimes sw_status = p_CSwift_AttachKeyParam(hac, &sw_param); 6761573Srgrimes switch (sw_status) { 6771573Srgrimes case SW_OK: 6781573Srgrimes break; 6791573Srgrimes case SW_ERR_INPUT_SIZE: 6801573Srgrimes CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT, CSWIFT_R_BAD_KEY_SIZE); 6811573Srgrimes goto err; 6821573Srgrimes default: 6831573Srgrimes { 6841573Srgrimes char tmpbuf[DECIMAL_SIZE(sw_status) + 1]; 6851573Srgrimes CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT, CSWIFT_R_REQUEST_FAILED); 6861573Srgrimes sprintf(tmpbuf, "%ld", sw_status); 6871573Srgrimes ERR_add_error_data(2, "CryptoSwift error number is ", tmpbuf); 6881573Srgrimes } 6891573Srgrimes goto err; 6901573Srgrimes } 6911573Srgrimes /* Prepare the argument and response */ 6921573Srgrimes arg.nbytes = BN_bn2bin(a, (unsigned char *)argument->d); 6931573Srgrimes arg.value = (unsigned char *)argument->d; 6941573Srgrimes res.nbytes = 2 * BN_num_bytes(p); 6951573Srgrimes memset(result->d, 0, res.nbytes); 6961573Srgrimes res.value = (unsigned char *)result->d; 6971573Srgrimes /* Perform the operation */ 6981573Srgrimes if ((sw_status = p_CSwift_SimpleRequest(hac, SW_CMD_MODEXP_CRT, &arg, 1, 6991573Srgrimes &res, 1)) != SW_OK) { 7001573Srgrimes char tmpbuf[DECIMAL_SIZE(sw_status) + 1]; 7011573Srgrimes CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT, CSWIFT_R_REQUEST_FAILED); 7021573Srgrimes sprintf(tmpbuf, "%ld", sw_status); 7031573Srgrimes ERR_add_error_data(2, "CryptoSwift error number is ", tmpbuf); 7041573Srgrimes goto err; 7051573Srgrimes } 7061573Srgrimes /* Convert the response */ 7071573Srgrimes BN_bin2bn((unsigned char *)result->d, res.nbytes, r); 7081573Srgrimes to_return = 1; 7091573Srgrimes err: 7101573Srgrimes if (sw_param.up.crt.p.value) 7111573Srgrimes OPENSSL_free(sw_param.up.crt.p.value); 7121573Srgrimes if (sw_param.up.crt.q.value) 7131573Srgrimes OPENSSL_free(sw_param.up.crt.q.value); 7141573Srgrimes if (sw_param.up.crt.dmp1.value) 7151573Srgrimes OPENSSL_free(sw_param.up.crt.dmp1.value); 7161573Srgrimes if (sw_param.up.crt.dmq1.value) 7171573Srgrimes OPENSSL_free(sw_param.up.crt.dmq1.value); 7181573Srgrimes if (sw_param.up.crt.iqmp.value) 7191573Srgrimes OPENSSL_free(sw_param.up.crt.iqmp.value); 7201573Srgrimes if (result) 7211573Srgrimes BN_free(result); 7221573Srgrimes if (argument) 7231573Srgrimes BN_free(argument); 7241573Srgrimes if (acquired) 7251573Srgrimes release_context(hac); 7261573Srgrimes return to_return; 7271573Srgrimes} 7281573Srgrimes# endif 7291573Srgrimes 7301573Srgrimes# ifndef OPENSSL_NO_RSA 7311573Srgrimesstatic int cswift_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, 7321573Srgrimes BN_CTX *ctx) 7331573Srgrimes{ 7341573Srgrimes int to_return = 0; 7351573Srgrimes const RSA_METHOD *def_rsa_method; 7361573Srgrimes 7371573Srgrimes if (!rsa->p || !rsa->q || !rsa->dmp1 || !rsa->dmq1 || !rsa->iqmp) { 7381573Srgrimes CSWIFTerr(CSWIFT_F_CSWIFT_RSA_MOD_EXP, 7391573Srgrimes CSWIFT_R_MISSING_KEY_COMPONENTS); 7401573Srgrimes goto err; 7411573Srgrimes } 7421573Srgrimes 7431573Srgrimes /* Try the limits of RSA (2048 bits) */ 7441573Srgrimes if (BN_num_bytes(rsa->p) > 128 || 7451573Srgrimes BN_num_bytes(rsa->q) > 128 || 7461573Srgrimes BN_num_bytes(rsa->dmp1) > 128 || 7471573Srgrimes BN_num_bytes(rsa->dmq1) > 128 || BN_num_bytes(rsa->iqmp) > 128) { 7481573Srgrimes# ifdef RSA_NULL 7491573Srgrimes def_rsa_method = RSA_null_method(); 7501573Srgrimes# else 7511573Srgrimes# if 0 7521573Srgrimes def_rsa_method = RSA_PKCS1_RSAref(); 7531573Srgrimes# else 7541573Srgrimes def_rsa_method = RSA_PKCS1_SSLeay(); 7551573Srgrimes# endif 7561573Srgrimes# endif 7571573Srgrimes if (def_rsa_method) 7581573Srgrimes return def_rsa_method->rsa_mod_exp(r0, I, rsa, ctx); 7591573Srgrimes } 7601573Srgrimes 7611573Srgrimes to_return = cswift_mod_exp_crt(r0, I, rsa->p, rsa->q, rsa->dmp1, 7621573Srgrimes rsa->dmq1, rsa->iqmp, ctx); 7631573Srgrimes err: 7641573Srgrimes return to_return; 7651573Srgrimes} 7661573Srgrimes 7671573Srgrimes/* This function is aliased to mod_exp (with the mont stuff dropped). */ 7681573Srgrimesstatic int cswift_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, 7691573Srgrimes const BIGNUM *m, BN_CTX *ctx, 7701573Srgrimes BN_MONT_CTX *m_ctx) 7711573Srgrimes{ 7721573Srgrimes const RSA_METHOD *def_rsa_method; 7731573Srgrimes 7741573Srgrimes /* Try the limits of RSA (2048 bits) */ 7751573Srgrimes if (BN_num_bytes(r) > 256 || 7761573Srgrimes BN_num_bytes(a) > 256 || BN_num_bytes(m) > 256) { 7771573Srgrimes# ifdef RSA_NULL 7781573Srgrimes def_rsa_method = RSA_null_method(); 7791573Srgrimes# else 7801573Srgrimes# if 0 7811573Srgrimes def_rsa_method = RSA_PKCS1_RSAref(); 7821573Srgrimes# else 7831573Srgrimes def_rsa_method = RSA_PKCS1_SSLeay(); 7841573Srgrimes# endif 7851573Srgrimes# endif 7861573Srgrimes if (def_rsa_method) 7871573Srgrimes return def_rsa_method->bn_mod_exp(r, a, p, m, ctx, m_ctx); 7881573Srgrimes } 7891573Srgrimes 7901573Srgrimes return cswift_mod_exp(r, a, p, m, ctx); 7911573Srgrimes} 7921573Srgrimes# endif /* OPENSSL_NO_RSA */ 7931573Srgrimes 7941573Srgrimes# ifndef OPENSSL_NO_DSA 7951573Srgrimesstatic DSA_SIG *cswift_dsa_sign(const unsigned char *dgst, int dlen, DSA *dsa) 7961573Srgrimes{ 7971573Srgrimes SW_CONTEXT_HANDLE hac; 7981573Srgrimes SW_PARAM sw_param; 7991573Srgrimes SW_STATUS sw_status; 8001573Srgrimes SW_LARGENUMBER arg, res; 8011573Srgrimes BN_CTX *ctx; 8021573Srgrimes BIGNUM *dsa_p = NULL; 8031573Srgrimes BIGNUM *dsa_q = NULL; 8041573Srgrimes BIGNUM *dsa_g = NULL; 8051573Srgrimes BIGNUM *dsa_key = NULL; 8061573Srgrimes BIGNUM *result = NULL; 8071573Srgrimes DSA_SIG *to_return = NULL; 8081573Srgrimes int acquired = 0; 8091573Srgrimes 8101573Srgrimes if ((ctx = BN_CTX_new()) == NULL) 8111573Srgrimes goto err; 8121573Srgrimes if (!get_context(&hac)) { 8131573Srgrimes CSWIFTerr(CSWIFT_F_CSWIFT_DSA_SIGN, CSWIFT_R_UNIT_FAILURE); 8141573Srgrimes goto err; 8151573Srgrimes } 8161573Srgrimes acquired = 1; 8171573Srgrimes /* Prepare the params */ 8181573Srgrimes BN_CTX_start(ctx); 8191573Srgrimes dsa_p = BN_CTX_get(ctx); 8201573Srgrimes dsa_q = BN_CTX_get(ctx); 8211573Srgrimes dsa_g = BN_CTX_get(ctx); 8221573Srgrimes dsa_key = BN_CTX_get(ctx); 8231573Srgrimes result = BN_CTX_get(ctx); 8241573Srgrimes if (!result) { 8251573Srgrimes CSWIFTerr(CSWIFT_F_CSWIFT_DSA_SIGN, CSWIFT_R_BN_CTX_FULL); 8261573Srgrimes goto err; 8271573Srgrimes } 8281573Srgrimes if (!bn_wexpand(dsa_p, dsa->p->top) || 8291573Srgrimes !bn_wexpand(dsa_q, dsa->q->top) || 8301573Srgrimes !bn_wexpand(dsa_g, dsa->g->top) || 8311573Srgrimes !bn_wexpand(dsa_key, dsa->priv_key->top) || 8321573Srgrimes !bn_wexpand(result, dsa->p->top)) { 8331573Srgrimes CSWIFTerr(CSWIFT_F_CSWIFT_DSA_SIGN, CSWIFT_R_BN_EXPAND_FAIL); 8341573Srgrimes goto err; 8351573Srgrimes } 8361573Srgrimes sw_param.type = SW_ALG_DSA; 8371573Srgrimes sw_param.up.dsa.p.nbytes = BN_bn2bin(dsa->p, (unsigned char *)dsa_p->d); 8381573Srgrimes sw_param.up.dsa.p.value = (unsigned char *)dsa_p->d; 8391573Srgrimes sw_param.up.dsa.q.nbytes = BN_bn2bin(dsa->q, (unsigned char *)dsa_q->d); 8401573Srgrimes sw_param.up.dsa.q.value = (unsigned char *)dsa_q->d; 8411573Srgrimes sw_param.up.dsa.g.nbytes = BN_bn2bin(dsa->g, (unsigned char *)dsa_g->d); 8421573Srgrimes sw_param.up.dsa.g.value = (unsigned char *)dsa_g->d; 8431573Srgrimes sw_param.up.dsa.key.nbytes = BN_bn2bin(dsa->priv_key, 8441573Srgrimes (unsigned char *)dsa_key->d); 8451573Srgrimes sw_param.up.dsa.key.value = (unsigned char *)dsa_key->d; 8461573Srgrimes /* Attach the key params */ 8471573Srgrimes sw_status = p_CSwift_AttachKeyParam(hac, &sw_param); 8481573Srgrimes switch (sw_status) { 8491573Srgrimes case SW_OK: 8501573Srgrimes break; 8511573Srgrimes case SW_ERR_INPUT_SIZE: 8521573Srgrimes CSWIFTerr(CSWIFT_F_CSWIFT_DSA_SIGN, CSWIFT_R_BAD_KEY_SIZE); 8531573Srgrimes goto err; 8541573Srgrimes default: 8551573Srgrimes { 8561573Srgrimes char tmpbuf[DECIMAL_SIZE(sw_status) + 1]; 8571573Srgrimes CSWIFTerr(CSWIFT_F_CSWIFT_DSA_SIGN, CSWIFT_R_REQUEST_FAILED); 8581573Srgrimes sprintf(tmpbuf, "%ld", sw_status); 8591573Srgrimes ERR_add_error_data(2, "CryptoSwift error number is ", tmpbuf); 8601573Srgrimes } 8611573Srgrimes goto err; 8621573Srgrimes } 8631573Srgrimes /* Prepare the argument and response */ 8641573Srgrimes arg.nbytes = dlen; 8651573Srgrimes arg.value = (unsigned char *)dgst; 8661573Srgrimes res.nbytes = BN_num_bytes(dsa->p); 8671573Srgrimes memset(result->d, 0, res.nbytes); 8681573Srgrimes res.value = (unsigned char *)result->d; 8691573Srgrimes /* Perform the operation */ 8701573Srgrimes sw_status = p_CSwift_SimpleRequest(hac, SW_CMD_DSS_SIGN, &arg, 1, 8711573Srgrimes &res, 1); 8721573Srgrimes if (sw_status != SW_OK) { 8731573Srgrimes char tmpbuf[DECIMAL_SIZE(sw_status) + 1]; 8741573Srgrimes CSWIFTerr(CSWIFT_F_CSWIFT_DSA_SIGN, CSWIFT_R_REQUEST_FAILED); 8751573Srgrimes sprintf(tmpbuf, "%ld", sw_status); 8761573Srgrimes ERR_add_error_data(2, "CryptoSwift error number is ", tmpbuf); 8771573Srgrimes goto err; 8781573Srgrimes } 8791573Srgrimes /* Convert the response */ 8801573Srgrimes if ((to_return = DSA_SIG_new()) == NULL) 8811573Srgrimes goto err; 8821573Srgrimes to_return->r = BN_bin2bn((unsigned char *)result->d, 20, NULL); 8831573Srgrimes to_return->s = BN_bin2bn((unsigned char *)result->d + 20, 20, NULL); 8841573Srgrimes 8858870Srgrimes err: 8861573Srgrimes if (acquired) 8871573Srgrimes release_context(hac); 8881573Srgrimes if (ctx) { 8891573Srgrimes BN_CTX_end(ctx); 8901573Srgrimes BN_CTX_free(ctx); 8911573Srgrimes } 8921573Srgrimes return to_return; 8931573Srgrimes} 8941573Srgrimes 8951573Srgrimesstatic int cswift_dsa_verify(const unsigned char *dgst, int dgst_len, 8961573Srgrimes DSA_SIG *sig, DSA *dsa) 8971573Srgrimes{ 8981573Srgrimes SW_CONTEXT_HANDLE hac; 8991573Srgrimes SW_PARAM sw_param; 9001573Srgrimes SW_STATUS sw_status; 9011573Srgrimes SW_LARGENUMBER arg[2], res; 9021573Srgrimes unsigned long sig_result; 9031573Srgrimes BN_CTX *ctx; 9041573Srgrimes BIGNUM *dsa_p = NULL; 9058870Srgrimes BIGNUM *dsa_q = NULL; 9061573Srgrimes BIGNUM *dsa_g = NULL; 9071573Srgrimes BIGNUM *dsa_key = NULL; 9081573Srgrimes BIGNUM *argument = NULL; 9091573Srgrimes int to_return = -1; 9101573Srgrimes int acquired = 0; 9111573Srgrimes 9121573Srgrimes if ((ctx = BN_CTX_new()) == NULL) 9131573Srgrimes goto err; 9141573Srgrimes if (!get_context(&hac)) { 9151573Srgrimes CSWIFTerr(CSWIFT_F_CSWIFT_DSA_VERIFY, CSWIFT_R_UNIT_FAILURE); 9161573Srgrimes goto err; 9171573Srgrimes } 9181573Srgrimes acquired = 1; 9191573Srgrimes /* Prepare the params */ 9201573Srgrimes BN_CTX_start(ctx); 9211573Srgrimes dsa_p = BN_CTX_get(ctx); 9221573Srgrimes dsa_q = BN_CTX_get(ctx); 9231573Srgrimes dsa_g = BN_CTX_get(ctx); 9241573Srgrimes dsa_key = BN_CTX_get(ctx); 9251573Srgrimes argument = BN_CTX_get(ctx); 9261573Srgrimes if (!argument) { 9271573Srgrimes CSWIFTerr(CSWIFT_F_CSWIFT_DSA_VERIFY, CSWIFT_R_BN_CTX_FULL); 9281573Srgrimes goto err; 9291573Srgrimes } 9301573Srgrimes if (!bn_wexpand(dsa_p, dsa->p->top) || 9311573Srgrimes !bn_wexpand(dsa_q, dsa->q->top) || 9321573Srgrimes !bn_wexpand(dsa_g, dsa->g->top) || 9331573Srgrimes !bn_wexpand(dsa_key, dsa->pub_key->top) || 9341573Srgrimes !bn_wexpand(argument, 40)) { 9351573Srgrimes CSWIFTerr(CSWIFT_F_CSWIFT_DSA_VERIFY, CSWIFT_R_BN_EXPAND_FAIL); 9361573Srgrimes goto err; 9371573Srgrimes } 9381573Srgrimes sw_param.type = SW_ALG_DSA; 9391573Srgrimes sw_param.up.dsa.p.nbytes = BN_bn2bin(dsa->p, (unsigned char *)dsa_p->d); 9401573Srgrimes sw_param.up.dsa.p.value = (unsigned char *)dsa_p->d; 9411573Srgrimes sw_param.up.dsa.q.nbytes = BN_bn2bin(dsa->q, (unsigned char *)dsa_q->d); 9421573Srgrimes sw_param.up.dsa.q.value = (unsigned char *)dsa_q->d; 9431573Srgrimes sw_param.up.dsa.g.nbytes = BN_bn2bin(dsa->g, (unsigned char *)dsa_g->d); 9441573Srgrimes sw_param.up.dsa.g.value = (unsigned char *)dsa_g->d; 9451573Srgrimes sw_param.up.dsa.key.nbytes = BN_bn2bin(dsa->pub_key, 9461573Srgrimes (unsigned char *)dsa_key->d); 9471573Srgrimes sw_param.up.dsa.key.value = (unsigned char *)dsa_key->d; 9481573Srgrimes /* Attach the key params */ 9491573Srgrimes sw_status = p_CSwift_AttachKeyParam(hac, &sw_param); 9501573Srgrimes switch (sw_status) { 9518870Srgrimes case SW_OK: 9528870Srgrimes break; 9531573Srgrimes case SW_ERR_INPUT_SIZE: 9541573Srgrimes CSWIFTerr(CSWIFT_F_CSWIFT_DSA_VERIFY, CSWIFT_R_BAD_KEY_SIZE); 9551573Srgrimes goto err; 9561573Srgrimes default: 9571573Srgrimes { 9581573Srgrimes char tmpbuf[DECIMAL_SIZE(sw_status) + 1]; 9591573Srgrimes CSWIFTerr(CSWIFT_F_CSWIFT_DSA_VERIFY, CSWIFT_R_REQUEST_FAILED); 9601573Srgrimes sprintf(tmpbuf, "%ld", sw_status); 9611573Srgrimes ERR_add_error_data(2, "CryptoSwift error number is ", tmpbuf); 9621573Srgrimes } 9631573Srgrimes goto err; 9641573Srgrimes } 9651573Srgrimes /* Prepare the argument and response */ 9661573Srgrimes arg[0].nbytes = dgst_len; 9671573Srgrimes arg[0].value = (unsigned char *)dgst; 9681573Srgrimes arg[1].nbytes = 40; 9691573Srgrimes arg[1].value = (unsigned char *)argument->d; 9701573Srgrimes memset(arg[1].value, 0, 40); 9711573Srgrimes BN_bn2bin(sig->r, arg[1].value + 20 - BN_num_bytes(sig->r)); 9721573Srgrimes BN_bn2bin(sig->s, arg[1].value + 40 - BN_num_bytes(sig->s)); 9731573Srgrimes res.nbytes = 4; /* unsigned long */ 9741573Srgrimes res.value = (unsigned char *)(&sig_result); 9751573Srgrimes /* Perform the operation */ 9761573Srgrimes sw_status = p_CSwift_SimpleRequest(hac, SW_CMD_DSS_VERIFY, arg, 2, 9771573Srgrimes &res, 1); 9781573Srgrimes if (sw_status != SW_OK) { 9791573Srgrimes char tmpbuf[DECIMAL_SIZE(sw_status) + 1]; 9808870Srgrimes CSWIFTerr(CSWIFT_F_CSWIFT_DSA_VERIFY, CSWIFT_R_REQUEST_FAILED); 9811573Srgrimes sprintf(tmpbuf, "%ld", sw_status); 9821573Srgrimes ERR_add_error_data(2, "CryptoSwift error number is ", tmpbuf); 9831573Srgrimes goto err; 9841573Srgrimes } 9858870Srgrimes /* Convert the response */ 9861573Srgrimes to_return = ((sig_result == 0) ? 0 : 1); 9871573Srgrimes 9881573Srgrimes err: 9891573Srgrimes if (acquired) 9901573Srgrimes release_context(hac); 9911573Srgrimes if (ctx) { 9921573Srgrimes BN_CTX_end(ctx); 9931573Srgrimes BN_CTX_free(ctx); 9941573Srgrimes } 9951573Srgrimes return to_return; 9961573Srgrimes} 9971573Srgrimes# endif 9981573Srgrimes 9991573Srgrimes# ifndef OPENSSL_NO_DH 10001573Srgrimes/* This function is aliased to mod_exp (with the dh and mont dropped). */ 10011573Srgrimesstatic int cswift_mod_exp_dh(const DH *dh, BIGNUM *r, 10021573Srgrimes const BIGNUM *a, const BIGNUM *p, 10031573Srgrimes const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx) 10041573Srgrimes{ 10051573Srgrimes return cswift_mod_exp(r, a, p, m, ctx); 10061573Srgrimes} 10071573Srgrimes# endif 10081573Srgrimes 10091573Srgrimes/* Random bytes are good */ 10101573Srgrimesstatic int cswift_rand_bytes(unsigned char *buf, int num) 10111573Srgrimes{ 10121573Srgrimes SW_CONTEXT_HANDLE hac; 10131573Srgrimes SW_STATUS swrc; 10141573Srgrimes SW_LARGENUMBER largenum; 10151573Srgrimes int acquired = 0; 10161573Srgrimes int to_return = 0; /* assume failure */ 10171573Srgrimes unsigned char buf32[1024]; 10181573Srgrimes 10191573Srgrimes if (!get_context(&hac)) { 10201573Srgrimes CSWIFTerr(CSWIFT_F_CSWIFT_RAND_BYTES, CSWIFT_R_UNIT_FAILURE); 10211573Srgrimes goto err; 10221573Srgrimes } 10231573Srgrimes acquired = 1; 10241573Srgrimes 10251573Srgrimes /************************************************************************/ 10261573Srgrimes /* 10271573Srgrimes * 04/02/2003 10281573Srgrimes */ 10291573Srgrimes /* 10301573Srgrimes * Modified by Frederic Giudicelli (deny-all.com) to overcome the 10311573Srgrimes */ 10321573Srgrimes /* 10331573Srgrimes * limitation of cswift with values not a multiple of 32 10341573Srgrimes */ 10351573Srgrimes /************************************************************************/ 10361573Srgrimes 10371573Srgrimes while (num >= (int)sizeof(buf32)) { 10381573Srgrimes largenum.value = buf; 10391573Srgrimes largenum.nbytes = sizeof(buf32); 10401573Srgrimes /*- 10411573Srgrimes * tell CryptoSwift how many bytes we want and where we want it. 10421573Srgrimes * Note: - CryptoSwift cannot do more than 4096 bytes at a time. 10431573Srgrimes * - CryptoSwift can only do multiple of 32-bits. 10441573Srgrimes */ 10451573Srgrimes swrc = 10461573Srgrimes p_CSwift_SimpleRequest(hac, SW_CMD_RAND, NULL, 0, &largenum, 1); 10471573Srgrimes if (swrc != SW_OK) { 10481573Srgrimes char tmpbuf[20]; 10491573Srgrimes CSWIFTerr(CSWIFT_F_CSWIFT_RAND_BYTES, CSWIFT_R_REQUEST_FAILED); 10501573Srgrimes sprintf(tmpbuf, "%ld", swrc); 10511573Srgrimes ERR_add_error_data(2, "CryptoSwift error number is ", tmpbuf); 10521573Srgrimes goto err; 10531573Srgrimes } 10541573Srgrimes buf += sizeof(buf32); 10551573Srgrimes num -= sizeof(buf32); 10561573Srgrimes } 10571573Srgrimes if (num) { 10581573Srgrimes largenum.nbytes = sizeof(buf32); 10591573Srgrimes largenum.value = buf32; 10601573Srgrimes swrc = 10611573Srgrimes p_CSwift_SimpleRequest(hac, SW_CMD_RAND, NULL, 0, &largenum, 1); 10621573Srgrimes if (swrc != SW_OK) { 10631573Srgrimes char tmpbuf[20]; 10641573Srgrimes CSWIFTerr(CSWIFT_F_CSWIFT_RAND_BYTES, CSWIFT_R_REQUEST_FAILED); 10651573Srgrimes sprintf(tmpbuf, "%ld", swrc); 10661573Srgrimes ERR_add_error_data(2, "CryptoSwift error number is ", tmpbuf); 10678870Srgrimes goto err; 10681573Srgrimes } 10691573Srgrimes memcpy(buf, largenum.value, num); 10701573Srgrimes } 10711573Srgrimes 10721573Srgrimes to_return = 1; /* success */ 10731573Srgrimes err: 10748870Srgrimes if (acquired) 10751573Srgrimes release_context(hac); 10761573Srgrimes 10771573Srgrimes return to_return; 10781573Srgrimes} 10791573Srgrimes 10801573Srgrimesstatic int cswift_rand_status(void) 10811573Srgrimes{ 10821573Srgrimes return 1; 10831573Srgrimes} 10841573Srgrimes 10851573Srgrimes/* 10861573Srgrimes * This stuff is needed if this ENGINE is being compiled into a 10871573Srgrimes * self-contained shared-library. 10881573Srgrimes */ 10891573Srgrimes# ifndef OPENSSL_NO_DYNAMIC_ENGINE 10901573Srgrimesstatic int bind_fn(ENGINE *e, const char *id) 10911573Srgrimes{ 10921573Srgrimes if (id && (strcmp(id, engine_cswift_id) != 0)) 10931573Srgrimes return 0; 10941573Srgrimes if (!bind_helper(e)) 10951573Srgrimes return 0; 10961573Srgrimes return 1; 10971573Srgrimes} 10981573Srgrimes 10991573SrgrimesIMPLEMENT_DYNAMIC_CHECK_FN() 11001573Srgrimes IMPLEMENT_DYNAMIC_BIND_FN(bind_fn) 11011573Srgrimes# endif /* OPENSSL_NO_DYNAMIC_ENGINE */ 11021573Srgrimes# endif /* !OPENSSL_NO_HW_CSWIFT */ 11031573Srgrimes#endif /* !OPENSSL_NO_HW */ 11041573Srgrimes