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