1ebfedea0SLionel Sambuc /* crypto/engine/hw_ibmca.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 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 /* (C) COPYRIGHT International Business Machines Corp. 2001 */
61ebfedea0SLionel Sambuc
62ebfedea0SLionel Sambuc #include <stdio.h>
63ebfedea0SLionel Sambuc #include <openssl/crypto.h>
64ebfedea0SLionel Sambuc #include <openssl/dso.h>
65ebfedea0SLionel Sambuc #include <openssl/engine.h>
66ebfedea0SLionel Sambuc
67ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_HW
68ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_HW_IBMCA
69ebfedea0SLionel Sambuc
70ebfedea0SLionel Sambuc # ifdef FLAT_INC
71ebfedea0SLionel Sambuc # include "ica_openssl_api.h"
72ebfedea0SLionel Sambuc # else
73ebfedea0SLionel Sambuc # include "vendor_defns/ica_openssl_api.h"
74ebfedea0SLionel Sambuc # endif
75ebfedea0SLionel Sambuc
76ebfedea0SLionel Sambuc # define IBMCA_LIB_NAME "ibmca engine"
77ebfedea0SLionel Sambuc # include "hw_ibmca_err.c"
78ebfedea0SLionel Sambuc
79ebfedea0SLionel Sambuc static int ibmca_destroy(ENGINE *e);
80ebfedea0SLionel Sambuc static int ibmca_init(ENGINE *e);
81ebfedea0SLionel Sambuc static int ibmca_finish(ENGINE *e);
82ebfedea0SLionel Sambuc static int ibmca_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f) ());
83ebfedea0SLionel Sambuc
84ebfedea0SLionel Sambuc static const char *IBMCA_F1 = "icaOpenAdapter";
85ebfedea0SLionel Sambuc static const char *IBMCA_F2 = "icaCloseAdapter";
86ebfedea0SLionel Sambuc static const char *IBMCA_F3 = "icaRsaModExpo";
87ebfedea0SLionel Sambuc static const char *IBMCA_F4 = "icaRandomNumberGenerate";
88ebfedea0SLionel Sambuc static const char *IBMCA_F5 = "icaRsaCrt";
89ebfedea0SLionel Sambuc
90ebfedea0SLionel Sambuc ICA_ADAPTER_HANDLE handle = 0;
91ebfedea0SLionel Sambuc
92ebfedea0SLionel Sambuc /* BIGNUM stuff */
93ebfedea0SLionel Sambuc static int ibmca_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
94ebfedea0SLionel Sambuc const BIGNUM *m, BN_CTX *ctx);
95ebfedea0SLionel Sambuc
96ebfedea0SLionel Sambuc static int ibmca_mod_exp_crt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
97*0a6a1f1dSLionel Sambuc const BIGNUM *q, const BIGNUM *dmp1,
98*0a6a1f1dSLionel Sambuc const BIGNUM *dmq1, const BIGNUM *iqmp,
99*0a6a1f1dSLionel Sambuc BN_CTX *ctx);
100ebfedea0SLionel Sambuc
101ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_RSA
102ebfedea0SLionel Sambuc /* RSA stuff */
103ebfedea0SLionel Sambuc static int ibmca_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa);
104ebfedea0SLionel Sambuc # endif
105ebfedea0SLionel Sambuc
106ebfedea0SLionel Sambuc /* This function is aliased to mod_exp (with the mont stuff dropped). */
107ebfedea0SLionel Sambuc static int ibmca_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
108*0a6a1f1dSLionel Sambuc const BIGNUM *m, BN_CTX *ctx,
109*0a6a1f1dSLionel Sambuc BN_MONT_CTX *m_ctx);
110ebfedea0SLionel Sambuc
111ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_DSA
112ebfedea0SLionel Sambuc /* DSA stuff */
113ebfedea0SLionel Sambuc static int ibmca_dsa_mod_exp(DSA *dsa, BIGNUM *rr, BIGNUM *a1,
114ebfedea0SLionel Sambuc BIGNUM *p1, BIGNUM *a2, BIGNUM *p2, BIGNUM *m,
115ebfedea0SLionel Sambuc BN_CTX *ctx, BN_MONT_CTX *in_mont);
116ebfedea0SLionel Sambuc static int ibmca_mod_exp_dsa(DSA *dsa, BIGNUM *r, BIGNUM *a,
117ebfedea0SLionel Sambuc const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
118ebfedea0SLionel Sambuc BN_MONT_CTX *m_ctx);
119ebfedea0SLionel Sambuc # endif
120ebfedea0SLionel Sambuc
121ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_DH
122ebfedea0SLionel Sambuc /* DH stuff */
123ebfedea0SLionel Sambuc /* This function is alised to mod_exp (with the DH and mont dropped). */
124ebfedea0SLionel Sambuc static int ibmca_mod_exp_dh(const DH *dh, BIGNUM *r,
125ebfedea0SLionel Sambuc const BIGNUM *a, const BIGNUM *p,
126ebfedea0SLionel Sambuc const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
127ebfedea0SLionel Sambuc # endif
128ebfedea0SLionel Sambuc
129ebfedea0SLionel Sambuc /* RAND stuff */
130ebfedea0SLionel Sambuc static int ibmca_rand_bytes(unsigned char *buf, int num);
131ebfedea0SLionel Sambuc static int ibmca_rand_status(void);
132ebfedea0SLionel Sambuc
133ebfedea0SLionel Sambuc /* WJH - check for more commands, like in nuron */
134ebfedea0SLionel Sambuc
135ebfedea0SLionel Sambuc /* The definitions for control commands specific to this engine */
136ebfedea0SLionel Sambuc # define IBMCA_CMD_SO_PATH ENGINE_CMD_BASE
137ebfedea0SLionel Sambuc static const ENGINE_CMD_DEFN ibmca_cmd_defns[] = {
138ebfedea0SLionel Sambuc {IBMCA_CMD_SO_PATH,
139ebfedea0SLionel Sambuc "SO_PATH",
140ebfedea0SLionel Sambuc "Specifies the path to the 'atasi' shared library",
141ebfedea0SLionel Sambuc ENGINE_CMD_FLAG_STRING},
142ebfedea0SLionel Sambuc {0, NULL, NULL, 0}
143ebfedea0SLionel Sambuc };
144ebfedea0SLionel Sambuc
145ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_RSA
146ebfedea0SLionel Sambuc /* Our internal RSA_METHOD that we provide pointers to */
147*0a6a1f1dSLionel Sambuc static RSA_METHOD ibmca_rsa = {
148ebfedea0SLionel Sambuc "Ibmca RSA method",
149ebfedea0SLionel Sambuc NULL,
150ebfedea0SLionel Sambuc NULL,
151ebfedea0SLionel Sambuc NULL,
152ebfedea0SLionel Sambuc NULL,
153ebfedea0SLionel Sambuc ibmca_rsa_mod_exp,
154ebfedea0SLionel Sambuc ibmca_mod_exp_mont,
155ebfedea0SLionel Sambuc NULL,
156ebfedea0SLionel Sambuc NULL,
157ebfedea0SLionel Sambuc 0,
158ebfedea0SLionel Sambuc NULL,
159ebfedea0SLionel Sambuc NULL,
160ebfedea0SLionel Sambuc NULL
161ebfedea0SLionel Sambuc };
162ebfedea0SLionel Sambuc # endif
163ebfedea0SLionel Sambuc
164ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_DSA
165ebfedea0SLionel Sambuc /* Our internal DSA_METHOD that we provide pointers to */
166*0a6a1f1dSLionel Sambuc static DSA_METHOD ibmca_dsa = {
167ebfedea0SLionel Sambuc "Ibmca DSA method",
168ebfedea0SLionel Sambuc NULL, /* dsa_do_sign */
169ebfedea0SLionel Sambuc NULL, /* dsa_sign_setup */
170ebfedea0SLionel Sambuc NULL, /* dsa_do_verify */
171ebfedea0SLionel Sambuc ibmca_dsa_mod_exp, /* dsa_mod_exp */
172ebfedea0SLionel Sambuc ibmca_mod_exp_dsa, /* bn_mod_exp */
173ebfedea0SLionel Sambuc NULL, /* init */
174ebfedea0SLionel Sambuc NULL, /* finish */
175ebfedea0SLionel Sambuc 0, /* flags */
176ebfedea0SLionel Sambuc NULL /* app_data */
177ebfedea0SLionel Sambuc };
178ebfedea0SLionel Sambuc # endif
179ebfedea0SLionel Sambuc
180ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_DH
181ebfedea0SLionel Sambuc /* Our internal DH_METHOD that we provide pointers to */
182*0a6a1f1dSLionel Sambuc static DH_METHOD ibmca_dh = {
183ebfedea0SLionel Sambuc "Ibmca DH method",
184ebfedea0SLionel Sambuc NULL,
185ebfedea0SLionel Sambuc NULL,
186ebfedea0SLionel Sambuc ibmca_mod_exp_dh,
187ebfedea0SLionel Sambuc NULL,
188ebfedea0SLionel Sambuc NULL,
189ebfedea0SLionel Sambuc 0,
190ebfedea0SLionel Sambuc NULL
191ebfedea0SLionel Sambuc };
192ebfedea0SLionel Sambuc # endif
193ebfedea0SLionel Sambuc
194*0a6a1f1dSLionel Sambuc static RAND_METHOD ibmca_rand = {
195ebfedea0SLionel Sambuc /* "IBMCA RAND method", */
196ebfedea0SLionel Sambuc NULL,
197ebfedea0SLionel Sambuc ibmca_rand_bytes,
198ebfedea0SLionel Sambuc NULL,
199ebfedea0SLionel Sambuc NULL,
200ebfedea0SLionel Sambuc ibmca_rand_bytes,
201ebfedea0SLionel Sambuc ibmca_rand_status,
202ebfedea0SLionel Sambuc };
203ebfedea0SLionel Sambuc
204ebfedea0SLionel Sambuc /* Constants used when creating the ENGINE */
205ebfedea0SLionel Sambuc static const char *engine_ibmca_id = "ibmca";
206ebfedea0SLionel Sambuc static const char *engine_ibmca_name = "Ibmca hardware engine support";
207ebfedea0SLionel Sambuc
208*0a6a1f1dSLionel Sambuc /*
209*0a6a1f1dSLionel Sambuc * This internal function is used by ENGINE_ibmca() and possibly by the
210*0a6a1f1dSLionel Sambuc * "dynamic" ENGINE support too
211*0a6a1f1dSLionel Sambuc */
bind_helper(ENGINE * e)212ebfedea0SLionel Sambuc static int bind_helper(ENGINE *e)
213ebfedea0SLionel Sambuc {
214ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_RSA
215ebfedea0SLionel Sambuc const RSA_METHOD *meth1;
216ebfedea0SLionel Sambuc # endif
217ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_DSA
218ebfedea0SLionel Sambuc const DSA_METHOD *meth2;
219ebfedea0SLionel Sambuc # endif
220ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_DH
221ebfedea0SLionel Sambuc const DH_METHOD *meth3;
222ebfedea0SLionel Sambuc # endif
223ebfedea0SLionel Sambuc if (!ENGINE_set_id(e, engine_ibmca_id) ||
224ebfedea0SLionel Sambuc !ENGINE_set_name(e, engine_ibmca_name) ||
225ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_RSA
226ebfedea0SLionel Sambuc !ENGINE_set_RSA(e, &ibmca_rsa) ||
227ebfedea0SLionel Sambuc # endif
228ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_DSA
229ebfedea0SLionel Sambuc !ENGINE_set_DSA(e, &ibmca_dsa) ||
230ebfedea0SLionel Sambuc # endif
231ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_DH
232ebfedea0SLionel Sambuc !ENGINE_set_DH(e, &ibmca_dh) ||
233ebfedea0SLionel Sambuc # endif
234ebfedea0SLionel Sambuc !ENGINE_set_RAND(e, &ibmca_rand) ||
235ebfedea0SLionel Sambuc !ENGINE_set_destroy_function(e, ibmca_destroy) ||
236ebfedea0SLionel Sambuc !ENGINE_set_init_function(e, ibmca_init) ||
237ebfedea0SLionel Sambuc !ENGINE_set_finish_function(e, ibmca_finish) ||
238ebfedea0SLionel Sambuc !ENGINE_set_ctrl_function(e, ibmca_ctrl) ||
239ebfedea0SLionel Sambuc !ENGINE_set_cmd_defns(e, ibmca_cmd_defns))
240ebfedea0SLionel Sambuc return 0;
241ebfedea0SLionel Sambuc
242ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_RSA
243*0a6a1f1dSLionel Sambuc /*
244*0a6a1f1dSLionel Sambuc * We know that the "PKCS1_SSLeay()" functions hook properly to the
245*0a6a1f1dSLionel Sambuc * ibmca-specific mod_exp and mod_exp_crt so we use those functions. NB:
246*0a6a1f1dSLionel Sambuc * We don't use ENGINE_openssl() or anything "more generic" because
247*0a6a1f1dSLionel Sambuc * something like the RSAref code may not hook properly, and if you own
248*0a6a1f1dSLionel Sambuc * one of these cards then you have the right to do RSA operations on it
249*0a6a1f1dSLionel Sambuc * anyway!
250*0a6a1f1dSLionel Sambuc */
251ebfedea0SLionel Sambuc meth1 = RSA_PKCS1_SSLeay();
252ebfedea0SLionel Sambuc ibmca_rsa.rsa_pub_enc = meth1->rsa_pub_enc;
253ebfedea0SLionel Sambuc ibmca_rsa.rsa_pub_dec = meth1->rsa_pub_dec;
254ebfedea0SLionel Sambuc ibmca_rsa.rsa_priv_enc = meth1->rsa_priv_enc;
255ebfedea0SLionel Sambuc ibmca_rsa.rsa_priv_dec = meth1->rsa_priv_dec;
256ebfedea0SLionel Sambuc # endif
257ebfedea0SLionel Sambuc
258ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_DSA
259*0a6a1f1dSLionel Sambuc /*
260*0a6a1f1dSLionel Sambuc * Use the DSA_OpenSSL() method and just hook the mod_exp-ish bits.
261*0a6a1f1dSLionel Sambuc */
262ebfedea0SLionel Sambuc meth2 = DSA_OpenSSL();
263ebfedea0SLionel Sambuc ibmca_dsa.dsa_do_sign = meth2->dsa_do_sign;
264ebfedea0SLionel Sambuc ibmca_dsa.dsa_sign_setup = meth2->dsa_sign_setup;
265ebfedea0SLionel Sambuc ibmca_dsa.dsa_do_verify = meth2->dsa_do_verify;
266ebfedea0SLionel Sambuc # endif
267ebfedea0SLionel Sambuc
268ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_DH
269ebfedea0SLionel Sambuc /* Much the same for Diffie-Hellman */
270ebfedea0SLionel Sambuc meth3 = DH_OpenSSL();
271ebfedea0SLionel Sambuc ibmca_dh.generate_key = meth3->generate_key;
272ebfedea0SLionel Sambuc ibmca_dh.compute_key = meth3->compute_key;
273ebfedea0SLionel Sambuc # endif
274ebfedea0SLionel Sambuc
275ebfedea0SLionel Sambuc /* Ensure the ibmca error handling is set up */
276ebfedea0SLionel Sambuc ERR_load_IBMCA_strings();
277ebfedea0SLionel Sambuc return 1;
278ebfedea0SLionel Sambuc }
279ebfedea0SLionel Sambuc
engine_ibmca(void)280ebfedea0SLionel Sambuc static ENGINE *engine_ibmca(void)
281ebfedea0SLionel Sambuc {
282ebfedea0SLionel Sambuc ENGINE *ret = ENGINE_new();
283ebfedea0SLionel Sambuc if (!ret)
284ebfedea0SLionel Sambuc return NULL;
285*0a6a1f1dSLionel Sambuc if (!bind_helper(ret)) {
286ebfedea0SLionel Sambuc ENGINE_free(ret);
287ebfedea0SLionel Sambuc return NULL;
288ebfedea0SLionel Sambuc }
289ebfedea0SLionel Sambuc return ret;
290ebfedea0SLionel Sambuc }
291ebfedea0SLionel Sambuc
292ebfedea0SLionel Sambuc # ifdef ENGINE_DYNAMIC_SUPPORT
293ebfedea0SLionel Sambuc static
294ebfedea0SLionel Sambuc # endif
ENGINE_load_ibmca(void)295ebfedea0SLionel Sambuc void ENGINE_load_ibmca(void)
296ebfedea0SLionel Sambuc {
297ebfedea0SLionel Sambuc /* Copied from eng_[openssl|dyn].c */
298ebfedea0SLionel Sambuc ENGINE *toadd = engine_ibmca();
299*0a6a1f1dSLionel Sambuc if (!toadd)
300*0a6a1f1dSLionel Sambuc return;
301ebfedea0SLionel Sambuc ENGINE_add(toadd);
302ebfedea0SLionel Sambuc ENGINE_free(toadd);
303ebfedea0SLionel Sambuc ERR_clear_error();
304ebfedea0SLionel Sambuc }
305ebfedea0SLionel Sambuc
306ebfedea0SLionel Sambuc /* Destructor (complements the "ENGINE_ibmca()" constructor) */
ibmca_destroy(ENGINE * e)307ebfedea0SLionel Sambuc static int ibmca_destroy(ENGINE *e)
308ebfedea0SLionel Sambuc {
309*0a6a1f1dSLionel Sambuc /*
310*0a6a1f1dSLionel Sambuc * Unload the ibmca error strings so any error state including our functs
311*0a6a1f1dSLionel Sambuc * or reasons won't lead to a segfault (they simply get displayed without
312*0a6a1f1dSLionel Sambuc * corresponding string data because none will be found).
313*0a6a1f1dSLionel Sambuc */
314ebfedea0SLionel Sambuc ERR_unload_IBMCA_strings();
315ebfedea0SLionel Sambuc return 1;
316ebfedea0SLionel Sambuc }
317ebfedea0SLionel Sambuc
318*0a6a1f1dSLionel Sambuc /*
319*0a6a1f1dSLionel Sambuc * This is a process-global DSO handle used for loading and unloading the
320*0a6a1f1dSLionel Sambuc * Ibmca library. NB: This is only set (or unset) during an init() or
321*0a6a1f1dSLionel Sambuc * finish() call (reference counts permitting) and they're operating with
322*0a6a1f1dSLionel Sambuc * global locks, so this should be thread-safe implicitly.
323*0a6a1f1dSLionel Sambuc */
324ebfedea0SLionel Sambuc
325ebfedea0SLionel Sambuc static DSO *ibmca_dso = NULL;
326ebfedea0SLionel Sambuc
327*0a6a1f1dSLionel Sambuc /*
328*0a6a1f1dSLionel Sambuc * These are the function pointers that are (un)set when the library has
329*0a6a1f1dSLionel Sambuc * successfully (un)loaded.
330*0a6a1f1dSLionel Sambuc */
331ebfedea0SLionel Sambuc
332ebfedea0SLionel Sambuc static unsigned int (ICA_CALL * p_icaOpenAdapter) ();
333ebfedea0SLionel Sambuc static unsigned int (ICA_CALL * p_icaCloseAdapter) ();
334ebfedea0SLionel Sambuc static unsigned int (ICA_CALL * p_icaRsaModExpo) ();
335ebfedea0SLionel Sambuc static unsigned int (ICA_CALL * p_icaRandomNumberGenerate) ();
336ebfedea0SLionel Sambuc static unsigned int (ICA_CALL * p_icaRsaCrt) ();
337ebfedea0SLionel Sambuc
338ebfedea0SLionel Sambuc /* utility function to obtain a context */
get_context(ICA_ADAPTER_HANDLE * p_handle)339ebfedea0SLionel Sambuc static int get_context(ICA_ADAPTER_HANDLE * p_handle)
340ebfedea0SLionel Sambuc {
341ebfedea0SLionel Sambuc unsigned int status = 0;
342ebfedea0SLionel Sambuc
343ebfedea0SLionel Sambuc status = p_icaOpenAdapter(0, p_handle);
344ebfedea0SLionel Sambuc if (status != 0)
345ebfedea0SLionel Sambuc return 0;
346ebfedea0SLionel Sambuc return 1;
347ebfedea0SLionel Sambuc }
348ebfedea0SLionel Sambuc
349ebfedea0SLionel Sambuc /* similarly to release one. */
release_context(ICA_ADAPTER_HANDLE handle)350ebfedea0SLionel Sambuc static void release_context(ICA_ADAPTER_HANDLE handle)
351ebfedea0SLionel Sambuc {
352ebfedea0SLionel Sambuc p_icaCloseAdapter(handle);
353ebfedea0SLionel Sambuc }
354ebfedea0SLionel Sambuc
355ebfedea0SLionel Sambuc /* (de)initialisation functions. */
ibmca_init(ENGINE * e)356ebfedea0SLionel Sambuc static int ibmca_init(ENGINE *e)
357ebfedea0SLionel Sambuc {
358ebfedea0SLionel Sambuc
359ebfedea0SLionel Sambuc void (*p1) ();
360ebfedea0SLionel Sambuc void (*p2) ();
361ebfedea0SLionel Sambuc void (*p3) ();
362ebfedea0SLionel Sambuc void (*p4) ();
363ebfedea0SLionel Sambuc void (*p5) ();
364ebfedea0SLionel Sambuc
365*0a6a1f1dSLionel Sambuc if (ibmca_dso != NULL) {
366ebfedea0SLionel Sambuc IBMCAerr(IBMCA_F_IBMCA_INIT, IBMCA_R_ALREADY_LOADED);
367ebfedea0SLionel Sambuc goto err;
368ebfedea0SLionel Sambuc }
369*0a6a1f1dSLionel Sambuc /*
370*0a6a1f1dSLionel Sambuc * Attempt to load libatasi.so/atasi.dll/whatever. Needs to be changed
371*0a6a1f1dSLionel Sambuc * unfortunately because the Ibmca drivers don't have standard library
372*0a6a1f1dSLionel Sambuc * names that can be platform-translated well.
373*0a6a1f1dSLionel Sambuc */
374*0a6a1f1dSLionel Sambuc /*
375*0a6a1f1dSLionel Sambuc * TODO: Work out how to actually map to the names the Ibmca drivers
376*0a6a1f1dSLionel Sambuc * really use - for now a symbollic link needs to be created on the host
377*0a6a1f1dSLionel Sambuc * system from libatasi.so to atasi.so on unix variants.
378*0a6a1f1dSLionel Sambuc */
379ebfedea0SLionel Sambuc
380ebfedea0SLionel Sambuc /* WJH XXX check name translation */
381ebfedea0SLionel Sambuc
382ebfedea0SLionel Sambuc ibmca_dso = DSO_load(NULL, IBMCA_LIBNAME, NULL,
383*0a6a1f1dSLionel Sambuc /*
384*0a6a1f1dSLionel Sambuc * DSO_FLAG_NAME_TRANSLATION
385*0a6a1f1dSLionel Sambuc */ 0);
386*0a6a1f1dSLionel Sambuc if (ibmca_dso == NULL) {
387ebfedea0SLionel Sambuc IBMCAerr(IBMCA_F_IBMCA_INIT, IBMCA_R_DSO_FAILURE);
388ebfedea0SLionel Sambuc goto err;
389ebfedea0SLionel Sambuc }
390ebfedea0SLionel Sambuc
391*0a6a1f1dSLionel Sambuc if (!(p1 = DSO_bind_func(ibmca_dso, IBMCA_F1)) ||
392*0a6a1f1dSLionel Sambuc !(p2 = DSO_bind_func(ibmca_dso, IBMCA_F2)) ||
393*0a6a1f1dSLionel Sambuc !(p3 = DSO_bind_func(ibmca_dso, IBMCA_F3)) ||
394*0a6a1f1dSLionel Sambuc !(p4 = DSO_bind_func(ibmca_dso, IBMCA_F4)) ||
395*0a6a1f1dSLionel Sambuc !(p5 = DSO_bind_func(ibmca_dso, IBMCA_F5))) {
396ebfedea0SLionel Sambuc IBMCAerr(IBMCA_F_IBMCA_INIT, IBMCA_R_DSO_FAILURE);
397ebfedea0SLionel Sambuc goto err;
398ebfedea0SLionel Sambuc }
399ebfedea0SLionel Sambuc
400ebfedea0SLionel Sambuc /* Copy the pointers */
401ebfedea0SLionel Sambuc
402ebfedea0SLionel Sambuc p_icaOpenAdapter = (unsigned int (ICA_CALL *) ())p1;
403ebfedea0SLionel Sambuc p_icaCloseAdapter = (unsigned int (ICA_CALL *) ())p2;
404ebfedea0SLionel Sambuc p_icaRsaModExpo = (unsigned int (ICA_CALL *) ())p3;
405ebfedea0SLionel Sambuc p_icaRandomNumberGenerate = (unsigned int (ICA_CALL *) ())p4;
406ebfedea0SLionel Sambuc p_icaRsaCrt = (unsigned int (ICA_CALL *) ())p5;
407ebfedea0SLionel Sambuc
408*0a6a1f1dSLionel Sambuc if (!get_context(&handle)) {
409ebfedea0SLionel Sambuc IBMCAerr(IBMCA_F_IBMCA_INIT, IBMCA_R_UNIT_FAILURE);
410ebfedea0SLionel Sambuc goto err;
411ebfedea0SLionel Sambuc }
412ebfedea0SLionel Sambuc
413ebfedea0SLionel Sambuc return 1;
414ebfedea0SLionel Sambuc err:
415ebfedea0SLionel Sambuc if (ibmca_dso)
416ebfedea0SLionel Sambuc DSO_free(ibmca_dso);
417ebfedea0SLionel Sambuc
418ebfedea0SLionel Sambuc p_icaOpenAdapter = NULL;
419ebfedea0SLionel Sambuc p_icaCloseAdapter = NULL;
420ebfedea0SLionel Sambuc p_icaRsaModExpo = NULL;
421ebfedea0SLionel Sambuc p_icaRandomNumberGenerate = NULL;
422ebfedea0SLionel Sambuc
423ebfedea0SLionel Sambuc return 0;
424ebfedea0SLionel Sambuc }
425ebfedea0SLionel Sambuc
ibmca_finish(ENGINE * e)426ebfedea0SLionel Sambuc static int ibmca_finish(ENGINE *e)
427ebfedea0SLionel Sambuc {
428*0a6a1f1dSLionel Sambuc if (ibmca_dso == NULL) {
429ebfedea0SLionel Sambuc IBMCAerr(IBMCA_F_IBMCA_FINISH, IBMCA_R_NOT_LOADED);
430ebfedea0SLionel Sambuc return 0;
431ebfedea0SLionel Sambuc }
432ebfedea0SLionel Sambuc release_context(handle);
433*0a6a1f1dSLionel Sambuc if (!DSO_free(ibmca_dso)) {
434ebfedea0SLionel Sambuc IBMCAerr(IBMCA_F_IBMCA_FINISH, IBMCA_R_DSO_FAILURE);
435ebfedea0SLionel Sambuc return 0;
436ebfedea0SLionel Sambuc }
437ebfedea0SLionel Sambuc ibmca_dso = NULL;
438ebfedea0SLionel Sambuc
439ebfedea0SLionel Sambuc return 1;
440ebfedea0SLionel Sambuc }
441ebfedea0SLionel Sambuc
ibmca_ctrl(ENGINE * e,int cmd,long i,void * p,void (* f)())442ebfedea0SLionel Sambuc static int ibmca_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f) ())
443ebfedea0SLionel Sambuc {
444ebfedea0SLionel Sambuc int initialised = ((ibmca_dso == NULL) ? 0 : 1);
445*0a6a1f1dSLionel Sambuc switch (cmd) {
446ebfedea0SLionel Sambuc case IBMCA_CMD_SO_PATH:
447*0a6a1f1dSLionel Sambuc if (p == NULL) {
448ebfedea0SLionel Sambuc IBMCAerr(IBMCA_F_IBMCA_CTRL, ERR_R_PASSED_NULL_PARAMETER);
449ebfedea0SLionel Sambuc return 0;
450ebfedea0SLionel Sambuc }
451*0a6a1f1dSLionel Sambuc if (initialised) {
452ebfedea0SLionel Sambuc IBMCAerr(IBMCA_F_IBMCA_CTRL, IBMCA_R_ALREADY_LOADED);
453ebfedea0SLionel Sambuc return 0;
454ebfedea0SLionel Sambuc }
455ebfedea0SLionel Sambuc IBMCA_LIBNAME = (const char *)p;
456ebfedea0SLionel Sambuc return 1;
457ebfedea0SLionel Sambuc default:
458ebfedea0SLionel Sambuc break;
459ebfedea0SLionel Sambuc }
460ebfedea0SLionel Sambuc IBMCAerr(IBMCA_F_IBMCA_CTRL, IBMCA_R_CTRL_COMMAND_NOT_IMPLEMENTED);
461ebfedea0SLionel Sambuc return 0;
462ebfedea0SLionel Sambuc }
463ebfedea0SLionel Sambuc
ibmca_mod_exp(BIGNUM * r,const BIGNUM * a,const BIGNUM * p,const BIGNUM * m,BN_CTX * ctx)464ebfedea0SLionel Sambuc static int ibmca_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
465ebfedea0SLionel Sambuc const BIGNUM *m, BN_CTX *ctx)
466ebfedea0SLionel Sambuc {
467*0a6a1f1dSLionel Sambuc /*
468*0a6a1f1dSLionel Sambuc * I need somewhere to store temporary serialised values for use with the
469*0a6a1f1dSLionel Sambuc * Ibmca API calls. A neat cheat - I'll use BIGNUMs from the BN_CTX but
470*0a6a1f1dSLionel Sambuc * access their arrays directly as byte arrays <grin>. This way I don't
471*0a6a1f1dSLionel Sambuc * have to clean anything up.
472*0a6a1f1dSLionel Sambuc */
473ebfedea0SLionel Sambuc
474ebfedea0SLionel Sambuc BIGNUM *argument = NULL;
475ebfedea0SLionel Sambuc BIGNUM *result = NULL;
476ebfedea0SLionel Sambuc BIGNUM *key = NULL;
477ebfedea0SLionel Sambuc int to_return;
478ebfedea0SLionel Sambuc int inLen, outLen, tmpLen;
479ebfedea0SLionel Sambuc
480ebfedea0SLionel Sambuc ICA_KEY_RSA_MODEXPO *publKey = NULL;
481ebfedea0SLionel Sambuc unsigned int rc;
482ebfedea0SLionel Sambuc
483ebfedea0SLionel Sambuc to_return = 0; /* expect failure */
484ebfedea0SLionel Sambuc
485*0a6a1f1dSLionel Sambuc if (!ibmca_dso) {
486ebfedea0SLionel Sambuc IBMCAerr(IBMCA_F_IBMCA_MOD_EXP, IBMCA_R_NOT_LOADED);
487ebfedea0SLionel Sambuc goto err;
488ebfedea0SLionel Sambuc }
489ebfedea0SLionel Sambuc /* Prepare the params */
490ebfedea0SLionel Sambuc BN_CTX_start(ctx);
491ebfedea0SLionel Sambuc argument = BN_CTX_get(ctx);
492ebfedea0SLionel Sambuc result = BN_CTX_get(ctx);
493ebfedea0SLionel Sambuc key = BN_CTX_get(ctx);
494ebfedea0SLionel Sambuc
495*0a6a1f1dSLionel Sambuc if (!argument || !result || !key) {
496ebfedea0SLionel Sambuc IBMCAerr(IBMCA_F_IBMCA_MOD_EXP, IBMCA_R_BN_CTX_FULL);
497ebfedea0SLionel Sambuc goto err;
498ebfedea0SLionel Sambuc }
499ebfedea0SLionel Sambuc
500ebfedea0SLionel Sambuc if (!bn_wexpand(argument, m->top) || !bn_wexpand(result, m->top) ||
501*0a6a1f1dSLionel Sambuc !bn_wexpand(key, sizeof(*publKey) / BN_BYTES)) {
502ebfedea0SLionel Sambuc IBMCAerr(IBMCA_F_IBMCA_MOD_EXP, IBMCA_R_BN_EXPAND_FAIL);
503ebfedea0SLionel Sambuc goto err;
504ebfedea0SLionel Sambuc }
505ebfedea0SLionel Sambuc
506ebfedea0SLionel Sambuc publKey = (ICA_KEY_RSA_MODEXPO *)key->d;
507ebfedea0SLionel Sambuc
508*0a6a1f1dSLionel Sambuc if (publKey == NULL) {
509ebfedea0SLionel Sambuc goto err;
510ebfedea0SLionel Sambuc }
511ebfedea0SLionel Sambuc memset(publKey, 0, sizeof(ICA_KEY_RSA_MODEXPO));
512ebfedea0SLionel Sambuc
513ebfedea0SLionel Sambuc publKey->keyType = CORRECT_ENDIANNESS(ME_KEY_TYPE);
514ebfedea0SLionel Sambuc publKey->keyLength = CORRECT_ENDIANNESS(sizeof(ICA_KEY_RSA_MODEXPO));
515ebfedea0SLionel Sambuc publKey->expOffset = (char *)publKey->keyRecord - (char *)publKey;
516ebfedea0SLionel Sambuc
517*0a6a1f1dSLionel Sambuc /*
518*0a6a1f1dSLionel Sambuc * A quirk of the card: the exponent length has to be the same as the
519*0a6a1f1dSLionel Sambuc * modulus (key) length
520*0a6a1f1dSLionel Sambuc */
521ebfedea0SLionel Sambuc
522ebfedea0SLionel Sambuc outLen = BN_num_bytes(m);
523ebfedea0SLionel Sambuc
524ebfedea0SLionel Sambuc /* check for modulus length SAB*/
525ebfedea0SLionel Sambuc if (outLen > 256) {
526ebfedea0SLionel Sambuc IBMCAerr(IBMCA_F_IBMCA_MOD_EXP, IBMCA_R_MEXP_LENGTH_TO_LARGE);
527ebfedea0SLionel Sambuc goto err;
528ebfedea0SLionel Sambuc }
529ebfedea0SLionel Sambuc /* check for modulus length SAB*/
530ebfedea0SLionel Sambuc
531ebfedea0SLionel Sambuc publKey->expLength = publKey->nLength = outLen;
532*0a6a1f1dSLionel Sambuc /*
533*0a6a1f1dSLionel Sambuc * SAB Check for underflow condition the size of the exponent is less
534*0a6a1f1dSLionel Sambuc * than the size of the parameter then we have a big problem and will
535*0a6a1f1dSLionel Sambuc * underflow the keyRecord buffer. Bad stuff could happen then
536ebfedea0SLionel Sambuc */
537ebfedea0SLionel Sambuc if (outLen < BN_num_bytes(p)) {
538ebfedea0SLionel Sambuc IBMCAerr(IBMCA_F_IBMCA_MOD_EXP, IBMCA_R_UNDERFLOW_KEYRECORD);
539ebfedea0SLionel Sambuc goto err;
540ebfedea0SLionel Sambuc }
541ebfedea0SLionel Sambuc /* SAB End check for underflow */
542ebfedea0SLionel Sambuc
543*0a6a1f1dSLionel Sambuc BN_bn2bin(p, &publKey->keyRecord[publKey->expLength - BN_num_bytes(p)]);
544ebfedea0SLionel Sambuc BN_bn2bin(m, &publKey->keyRecord[publKey->expLength]);
545ebfedea0SLionel Sambuc
546ebfedea0SLionel Sambuc publKey->modulusBitLength = CORRECT_ENDIANNESS(publKey->nLength * 8);
547ebfedea0SLionel Sambuc publKey->nOffset = CORRECT_ENDIANNESS(publKey->expOffset +
548ebfedea0SLionel Sambuc publKey->expLength);
549ebfedea0SLionel Sambuc
550ebfedea0SLionel Sambuc publKey->expOffset = CORRECT_ENDIANNESS((char *)publKey->keyRecord -
551ebfedea0SLionel Sambuc (char *)publKey);
552ebfedea0SLionel Sambuc
553ebfedea0SLionel Sambuc tmpLen = outLen;
554ebfedea0SLionel Sambuc publKey->expLength = publKey->nLength = CORRECT_ENDIANNESS(tmpLen);
555ebfedea0SLionel Sambuc
556ebfedea0SLionel Sambuc /* Prepare the argument */
557ebfedea0SLionel Sambuc
558ebfedea0SLionel Sambuc memset(argument->d, 0, outLen);
559*0a6a1f1dSLionel Sambuc BN_bn2bin(a, (unsigned char *)argument->d + outLen - BN_num_bytes(a));
560ebfedea0SLionel Sambuc
561ebfedea0SLionel Sambuc inLen = outLen;
562ebfedea0SLionel Sambuc
563ebfedea0SLionel Sambuc /* Perform the operation */
564ebfedea0SLionel Sambuc
565ebfedea0SLionel Sambuc if ((rc = p_icaRsaModExpo(handle, inLen, (unsigned char *)argument->d,
566ebfedea0SLionel Sambuc publKey, &outLen, (unsigned char *)result->d))
567*0a6a1f1dSLionel Sambuc != 0) {
568ebfedea0SLionel Sambuc printf("rc = %d\n", rc);
569ebfedea0SLionel Sambuc IBMCAerr(IBMCA_F_IBMCA_MOD_EXP, IBMCA_R_REQUEST_FAILED);
570ebfedea0SLionel Sambuc goto err;
571ebfedea0SLionel Sambuc }
572ebfedea0SLionel Sambuc
573ebfedea0SLionel Sambuc /* Convert the response */
574ebfedea0SLionel Sambuc BN_bin2bn((unsigned char *)result->d, outLen, r);
575ebfedea0SLionel Sambuc to_return = 1;
576ebfedea0SLionel Sambuc err:
577ebfedea0SLionel Sambuc BN_CTX_end(ctx);
578ebfedea0SLionel Sambuc return to_return;
579ebfedea0SLionel Sambuc }
580ebfedea0SLionel Sambuc
581ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_RSA
ibmca_rsa_mod_exp(BIGNUM * r0,const BIGNUM * I,RSA * rsa)582ebfedea0SLionel Sambuc static int ibmca_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa)
583ebfedea0SLionel Sambuc {
584ebfedea0SLionel Sambuc BN_CTX *ctx;
585ebfedea0SLionel Sambuc int to_return = 0;
586ebfedea0SLionel Sambuc
587ebfedea0SLionel Sambuc if ((ctx = BN_CTX_new()) == NULL)
588ebfedea0SLionel Sambuc goto err;
589*0a6a1f1dSLionel Sambuc if (!rsa->p || !rsa->q || !rsa->dmp1 || !rsa->dmq1 || !rsa->iqmp) {
590*0a6a1f1dSLionel Sambuc if (!rsa->d || !rsa->n) {
591ebfedea0SLionel Sambuc IBMCAerr(IBMCA_F_IBMCA_RSA_MOD_EXP,
592ebfedea0SLionel Sambuc IBMCA_R_MISSING_KEY_COMPONENTS);
593ebfedea0SLionel Sambuc goto err;
594ebfedea0SLionel Sambuc }
595ebfedea0SLionel Sambuc to_return = ibmca_mod_exp(r0, I, rsa->d, rsa->n, ctx);
596*0a6a1f1dSLionel Sambuc } else {
597ebfedea0SLionel Sambuc to_return = ibmca_mod_exp_crt(r0, I, rsa->p, rsa->q, rsa->dmp1,
598ebfedea0SLionel Sambuc rsa->dmq1, rsa->iqmp, ctx);
599ebfedea0SLionel Sambuc }
600ebfedea0SLionel Sambuc err:
601ebfedea0SLionel Sambuc if (ctx)
602ebfedea0SLionel Sambuc BN_CTX_free(ctx);
603ebfedea0SLionel Sambuc return to_return;
604ebfedea0SLionel Sambuc }
605ebfedea0SLionel Sambuc # endif
606ebfedea0SLionel Sambuc
607ebfedea0SLionel Sambuc /* Ein kleines chinesisches "Restessen" */
ibmca_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)608ebfedea0SLionel Sambuc static int ibmca_mod_exp_crt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
609ebfedea0SLionel Sambuc const BIGNUM *q, const BIGNUM *dmp1,
610*0a6a1f1dSLionel Sambuc const BIGNUM *dmq1, const BIGNUM *iqmp,
611*0a6a1f1dSLionel Sambuc BN_CTX *ctx)
612ebfedea0SLionel Sambuc {
613ebfedea0SLionel Sambuc
614ebfedea0SLionel Sambuc BIGNUM *argument = NULL;
615ebfedea0SLionel Sambuc BIGNUM *result = NULL;
616ebfedea0SLionel Sambuc BIGNUM *key = NULL;
617ebfedea0SLionel Sambuc
618ebfedea0SLionel Sambuc int to_return = 0; /* expect failure */
619ebfedea0SLionel Sambuc
620ebfedea0SLionel Sambuc char *pkey = NULL;
621ebfedea0SLionel Sambuc ICA_KEY_RSA_CRT *privKey = NULL;
622ebfedea0SLionel Sambuc int inLen, outLen;
623ebfedea0SLionel Sambuc
624ebfedea0SLionel Sambuc int rc;
625ebfedea0SLionel Sambuc unsigned int offset, pSize, qSize;
626ebfedea0SLionel Sambuc /* SAB New variables */
627ebfedea0SLionel Sambuc unsigned int keyRecordSize;
628ebfedea0SLionel Sambuc unsigned int pbytes = BN_num_bytes(p);
629ebfedea0SLionel Sambuc unsigned int qbytes = BN_num_bytes(q);
630ebfedea0SLionel Sambuc unsigned int dmp1bytes = BN_num_bytes(dmp1);
631ebfedea0SLionel Sambuc unsigned int dmq1bytes = BN_num_bytes(dmq1);
632ebfedea0SLionel Sambuc unsigned int iqmpbytes = BN_num_bytes(iqmp);
633ebfedea0SLionel Sambuc
634ebfedea0SLionel Sambuc /* Prepare the params */
635ebfedea0SLionel Sambuc
636ebfedea0SLionel Sambuc BN_CTX_start(ctx);
637ebfedea0SLionel Sambuc argument = BN_CTX_get(ctx);
638ebfedea0SLionel Sambuc result = BN_CTX_get(ctx);
639ebfedea0SLionel Sambuc key = BN_CTX_get(ctx);
640ebfedea0SLionel Sambuc
641*0a6a1f1dSLionel Sambuc if (!argument || !result || !key) {
642ebfedea0SLionel Sambuc IBMCAerr(IBMCA_F_IBMCA_MOD_EXP_CRT, IBMCA_R_BN_CTX_FULL);
643ebfedea0SLionel Sambuc goto err;
644ebfedea0SLionel Sambuc }
645ebfedea0SLionel Sambuc
646ebfedea0SLionel Sambuc if (!bn_wexpand(argument, p->top + q->top) ||
647ebfedea0SLionel Sambuc !bn_wexpand(result, p->top + q->top) ||
648*0a6a1f1dSLionel Sambuc !bn_wexpand(key, sizeof(*privKey) / BN_BYTES)) {
649ebfedea0SLionel Sambuc IBMCAerr(IBMCA_F_IBMCA_MOD_EXP_CRT, IBMCA_R_BN_EXPAND_FAIL);
650ebfedea0SLionel Sambuc goto err;
651ebfedea0SLionel Sambuc }
652ebfedea0SLionel Sambuc
653ebfedea0SLionel Sambuc privKey = (ICA_KEY_RSA_CRT *)key->d;
654*0a6a1f1dSLionel Sambuc /*
655*0a6a1f1dSLionel Sambuc * SAB Add check for total size in bytes of the parms does not exceed the
656*0a6a1f1dSLionel Sambuc * buffer space we have do this first
657ebfedea0SLionel Sambuc */
658ebfedea0SLionel Sambuc keyRecordSize = pbytes + qbytes + dmp1bytes + dmq1bytes + iqmpbytes;
659ebfedea0SLionel Sambuc if (keyRecordSize > sizeof(privKey->keyRecord)) {
660ebfedea0SLionel Sambuc IBMCAerr(IBMCA_F_IBMCA_MOD_EXP_CRT, IBMCA_R_OPERANDS_TO_LARGE);
661ebfedea0SLionel Sambuc goto err;
662ebfedea0SLionel Sambuc }
663ebfedea0SLionel Sambuc
664ebfedea0SLionel Sambuc if ((qbytes + dmq1bytes) > 256) {
665ebfedea0SLionel Sambuc IBMCAerr(IBMCA_F_IBMCA_MOD_EXP_CRT, IBMCA_R_OPERANDS_TO_LARGE);
666ebfedea0SLionel Sambuc goto err;
667ebfedea0SLionel Sambuc }
668ebfedea0SLionel Sambuc
669ebfedea0SLionel Sambuc if (pbytes + dmp1bytes > 256) {
670ebfedea0SLionel Sambuc IBMCAerr(IBMCA_F_IBMCA_MOD_EXP_CRT, IBMCA_R_OPERANDS_TO_LARGE);
671ebfedea0SLionel Sambuc goto err;
672ebfedea0SLionel Sambuc }
673ebfedea0SLionel Sambuc
674ebfedea0SLionel Sambuc /* end SAB additions */
675ebfedea0SLionel Sambuc
676ebfedea0SLionel Sambuc memset(privKey, 0, sizeof(ICA_KEY_RSA_CRT));
677ebfedea0SLionel Sambuc privKey->keyType = CORRECT_ENDIANNESS(CRT_KEY_TYPE);
678ebfedea0SLionel Sambuc privKey->keyLength = CORRECT_ENDIANNESS(sizeof(ICA_KEY_RSA_CRT));
679*0a6a1f1dSLionel Sambuc privKey->modulusBitLength = CORRECT_ENDIANNESS(BN_num_bytes(q) * 2 * 8);
680ebfedea0SLionel Sambuc
681ebfedea0SLionel Sambuc /*
682ebfedea0SLionel Sambuc * p,dp & qInv are 1 QWORD Larger
683ebfedea0SLionel Sambuc */
684ebfedea0SLionel Sambuc privKey->pLength = CORRECT_ENDIANNESS(BN_num_bytes(p) + 8);
685ebfedea0SLionel Sambuc privKey->qLength = CORRECT_ENDIANNESS(BN_num_bytes(q));
686ebfedea0SLionel Sambuc privKey->dpLength = CORRECT_ENDIANNESS(BN_num_bytes(dmp1) + 8);
687ebfedea0SLionel Sambuc privKey->dqLength = CORRECT_ENDIANNESS(BN_num_bytes(dmq1));
688ebfedea0SLionel Sambuc privKey->qInvLength = CORRECT_ENDIANNESS(BN_num_bytes(iqmp) + 8);
689ebfedea0SLionel Sambuc
690*0a6a1f1dSLionel Sambuc offset = (char *)privKey->keyRecord - (char *)privKey;
691ebfedea0SLionel Sambuc
692ebfedea0SLionel Sambuc qSize = BN_num_bytes(q);
693ebfedea0SLionel Sambuc pSize = qSize + 8; /* 1 QWORD larger */
694ebfedea0SLionel Sambuc
695*0a6a1f1dSLionel Sambuc /*
696*0a6a1f1dSLionel Sambuc * SAB probably aittle redundant, but we'll verify that each of the
697*0a6a1f1dSLionel Sambuc * components which make up a key record sent ot the card does not exceed
698*0a6a1f1dSLionel Sambuc * the space that is allocated for it. this handles the case where even
699*0a6a1f1dSLionel Sambuc * if the total length does not exceed keyrecord zied, if the operands are
700*0a6a1f1dSLionel Sambuc * funny sized they could cause potential side affects on either the card
701*0a6a1f1dSLionel Sambuc * or the result
702*0a6a1f1dSLionel Sambuc */
703ebfedea0SLionel Sambuc
704ebfedea0SLionel Sambuc if ((pbytes > pSize) || (dmp1bytes > pSize) ||
705*0a6a1f1dSLionel Sambuc (iqmpbytes > pSize) || (qbytes > qSize) || (dmq1bytes > qSize)) {
706ebfedea0SLionel Sambuc IBMCAerr(IBMCA_F_IBMCA_MOD_EXP_CRT, IBMCA_R_OPERANDS_TO_LARGE);
707ebfedea0SLionel Sambuc goto err;
708ebfedea0SLionel Sambuc
709ebfedea0SLionel Sambuc }
710ebfedea0SLionel Sambuc
711ebfedea0SLionel Sambuc privKey->dpOffset = CORRECT_ENDIANNESS(offset);
712ebfedea0SLionel Sambuc
713ebfedea0SLionel Sambuc offset += pSize;
714ebfedea0SLionel Sambuc privKey->dqOffset = CORRECT_ENDIANNESS(offset);
715ebfedea0SLionel Sambuc
716ebfedea0SLionel Sambuc offset += qSize;
717ebfedea0SLionel Sambuc privKey->pOffset = CORRECT_ENDIANNESS(offset);
718ebfedea0SLionel Sambuc
719ebfedea0SLionel Sambuc offset += pSize;
720ebfedea0SLionel Sambuc privKey->qOffset = CORRECT_ENDIANNESS(offset);
721ebfedea0SLionel Sambuc
722ebfedea0SLionel Sambuc offset += qSize;
723ebfedea0SLionel Sambuc privKey->qInvOffset = CORRECT_ENDIANNESS(offset);
724ebfedea0SLionel Sambuc
725ebfedea0SLionel Sambuc pkey = (char *)privKey->keyRecord;
726ebfedea0SLionel Sambuc
727ebfedea0SLionel Sambuc /* SAB first check that we don;t under flow the buffer */
728ebfedea0SLionel Sambuc if (pSize < pbytes) {
729ebfedea0SLionel Sambuc IBMCAerr(IBMCA_F_IBMCA_MOD_EXP_CRT, IBMCA_R_UNDERFLOW_CONDITION);
730ebfedea0SLionel Sambuc goto err;
731ebfedea0SLionel Sambuc }
732ebfedea0SLionel Sambuc
733ebfedea0SLionel Sambuc /* pkey += pSize - BN_num_bytes(p); WROING this should be dmp1) */
734ebfedea0SLionel Sambuc pkey += pSize - BN_num_bytes(dmp1);
735ebfedea0SLionel Sambuc BN_bn2bin(dmp1, pkey);
736ebfedea0SLionel Sambuc pkey += BN_num_bytes(dmp1); /* move the pointer */
737ebfedea0SLionel Sambuc
738ebfedea0SLionel Sambuc BN_bn2bin(dmq1, pkey); /* Copy over dmq1 */
739ebfedea0SLionel Sambuc
740ebfedea0SLionel Sambuc pkey += qSize; /* move pointer */
741*0a6a1f1dSLionel Sambuc /* set up for zero padding of next field */
742*0a6a1f1dSLionel Sambuc pkey += pSize - BN_num_bytes(p);
743ebfedea0SLionel Sambuc
744ebfedea0SLionel Sambuc BN_bn2bin(p, pkey);
745*0a6a1f1dSLionel Sambuc /* increment pointer by number of bytes moved */
746*0a6a1f1dSLionel Sambuc pkey += BN_num_bytes(p);
747ebfedea0SLionel Sambuc
748ebfedea0SLionel Sambuc BN_bn2bin(q, pkey);
749ebfedea0SLionel Sambuc pkey += qSize; /* move the pointer */
750ebfedea0SLionel Sambuc pkey += pSize - BN_num_bytes(iqmp); /* Adjust for padding */
751ebfedea0SLionel Sambuc BN_bn2bin(iqmp, pkey);
752ebfedea0SLionel Sambuc
753ebfedea0SLionel Sambuc /* Prepare the argument and response */
754ebfedea0SLionel Sambuc
755*0a6a1f1dSLionel Sambuc /*
756*0a6a1f1dSLionel Sambuc * Correct endianess is used because the fields were converted above
757*0a6a1f1dSLionel Sambuc */
758*0a6a1f1dSLionel Sambuc outLen = CORRECT_ENDIANNESS(privKey->qLength) * 2;
759ebfedea0SLionel Sambuc
760ebfedea0SLionel Sambuc if (outLen > 256) {
761ebfedea0SLionel Sambuc IBMCAerr(IBMCA_F_IBMCA_MOD_EXP_CRT, IBMCA_R_OUTLEN_TO_LARGE);
762ebfedea0SLionel Sambuc goto err;
763ebfedea0SLionel Sambuc }
764ebfedea0SLionel Sambuc
765ebfedea0SLionel Sambuc /* SAB check for underflow here on the argeument */
766ebfedea0SLionel Sambuc if (outLen < BN_num_bytes(a)) {
767ebfedea0SLionel Sambuc IBMCAerr(IBMCA_F_IBMCA_MOD_EXP_CRT, IBMCA_R_UNDERFLOW_CONDITION);
768ebfedea0SLionel Sambuc goto err;
769ebfedea0SLionel Sambuc }
770ebfedea0SLionel Sambuc
771*0a6a1f1dSLionel Sambuc BN_bn2bin(a, (unsigned char *)argument->d + outLen - BN_num_bytes(a));
772ebfedea0SLionel Sambuc inLen = outLen;
773ebfedea0SLionel Sambuc
774ebfedea0SLionel Sambuc memset(result->d, 0, outLen);
775ebfedea0SLionel Sambuc
776ebfedea0SLionel Sambuc /* Perform the operation */
777ebfedea0SLionel Sambuc
778ebfedea0SLionel Sambuc if ((rc = p_icaRsaCrt(handle, inLen, (unsigned char *)argument->d,
779ebfedea0SLionel Sambuc privKey, &outLen, (unsigned char *)result->d)) != 0)
780ebfedea0SLionel Sambuc {
781ebfedea0SLionel Sambuc printf("rc = %d\n", rc);
782ebfedea0SLionel Sambuc IBMCAerr(IBMCA_F_IBMCA_MOD_EXP_CRT, IBMCA_R_REQUEST_FAILED);
783ebfedea0SLionel Sambuc goto err;
784ebfedea0SLionel Sambuc }
785ebfedea0SLionel Sambuc
786ebfedea0SLionel Sambuc /* Convert the response */
787ebfedea0SLionel Sambuc
788ebfedea0SLionel Sambuc BN_bin2bn((unsigned char *)result->d, outLen, r);
789ebfedea0SLionel Sambuc to_return = 1;
790ebfedea0SLionel Sambuc
791ebfedea0SLionel Sambuc err:
792ebfedea0SLionel Sambuc BN_CTX_end(ctx);
793ebfedea0SLionel Sambuc return to_return;
794ebfedea0SLionel Sambuc
795ebfedea0SLionel Sambuc }
796ebfedea0SLionel Sambuc
797ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_DSA
798*0a6a1f1dSLionel Sambuc /*
799*0a6a1f1dSLionel Sambuc * This code was liberated and adapted from the commented-out code in
800*0a6a1f1dSLionel Sambuc * dsa_ossl.c. Because of the unoptimised form of the Ibmca acceleration (it
801*0a6a1f1dSLionel Sambuc * doesn't have a CRT form for RSA), this function means that an Ibmca system
802*0a6a1f1dSLionel Sambuc * running with a DSA server certificate can handshake around 5 or 6 times
803*0a6a1f1dSLionel Sambuc * faster/more than an equivalent system running with RSA. Just check out the
804*0a6a1f1dSLionel Sambuc * "signs" statistics from the RSA and DSA parts of "openssl speed -engine
805*0a6a1f1dSLionel Sambuc * ibmca dsa1024 rsa1024".
806*0a6a1f1dSLionel Sambuc */
ibmca_dsa_mod_exp(DSA * dsa,BIGNUM * rr,BIGNUM * a1,BIGNUM * p1,BIGNUM * a2,BIGNUM * p2,BIGNUM * m,BN_CTX * ctx,BN_MONT_CTX * in_mont)807ebfedea0SLionel Sambuc static int ibmca_dsa_mod_exp(DSA *dsa, BIGNUM *rr, BIGNUM *a1,
808ebfedea0SLionel Sambuc BIGNUM *p1, BIGNUM *a2, BIGNUM *p2, BIGNUM *m,
809ebfedea0SLionel Sambuc BN_CTX *ctx, BN_MONT_CTX *in_mont)
810ebfedea0SLionel Sambuc {
811ebfedea0SLionel Sambuc BIGNUM t;
812ebfedea0SLionel Sambuc int to_return = 0;
813ebfedea0SLionel Sambuc
814ebfedea0SLionel Sambuc BN_init(&t);
815ebfedea0SLionel Sambuc /* let rr = a1 ^ p1 mod m */
816*0a6a1f1dSLionel Sambuc if (!ibmca_mod_exp(rr, a1, p1, m, ctx))
817*0a6a1f1dSLionel Sambuc goto end;
818ebfedea0SLionel Sambuc /* let t = a2 ^ p2 mod m */
819*0a6a1f1dSLionel Sambuc if (!ibmca_mod_exp(&t, a2, p2, m, ctx))
820*0a6a1f1dSLionel Sambuc goto end;
821ebfedea0SLionel Sambuc /* let rr = rr * t mod m */
822*0a6a1f1dSLionel Sambuc if (!BN_mod_mul(rr, rr, &t, m, ctx))
823*0a6a1f1dSLionel Sambuc goto end;
824ebfedea0SLionel Sambuc to_return = 1;
825ebfedea0SLionel Sambuc end:
826ebfedea0SLionel Sambuc BN_free(&t);
827ebfedea0SLionel Sambuc return to_return;
828ebfedea0SLionel Sambuc }
829ebfedea0SLionel Sambuc
ibmca_mod_exp_dsa(DSA * dsa,BIGNUM * r,BIGNUM * a,const BIGNUM * p,const BIGNUM * m,BN_CTX * ctx,BN_MONT_CTX * m_ctx)830ebfedea0SLionel Sambuc static int ibmca_mod_exp_dsa(DSA *dsa, BIGNUM *r, BIGNUM *a,
831ebfedea0SLionel Sambuc const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
832ebfedea0SLionel Sambuc BN_MONT_CTX *m_ctx)
833ebfedea0SLionel Sambuc {
834ebfedea0SLionel Sambuc return ibmca_mod_exp(r, a, p, m, ctx);
835ebfedea0SLionel Sambuc }
836ebfedea0SLionel Sambuc # endif
837ebfedea0SLionel Sambuc
838ebfedea0SLionel Sambuc /* This function is aliased to mod_exp (with the mont stuff dropped). */
ibmca_mod_exp_mont(BIGNUM * r,const BIGNUM * a,const BIGNUM * p,const BIGNUM * m,BN_CTX * ctx,BN_MONT_CTX * m_ctx)839ebfedea0SLionel Sambuc static int ibmca_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
840*0a6a1f1dSLionel Sambuc const BIGNUM *m, BN_CTX *ctx,
841*0a6a1f1dSLionel Sambuc BN_MONT_CTX *m_ctx)
842ebfedea0SLionel Sambuc {
843ebfedea0SLionel Sambuc return ibmca_mod_exp(r, a, p, m, ctx);
844ebfedea0SLionel Sambuc }
845ebfedea0SLionel Sambuc
846ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_DH
847ebfedea0SLionel Sambuc /* This function is aliased to mod_exp (with the dh and mont dropped). */
ibmca_mod_exp_dh(DH const * dh,BIGNUM * r,const BIGNUM * a,const BIGNUM * p,const BIGNUM * m,BN_CTX * ctx,BN_MONT_CTX * m_ctx)848ebfedea0SLionel Sambuc static int ibmca_mod_exp_dh(DH const *dh, BIGNUM *r,
849ebfedea0SLionel Sambuc const BIGNUM *a, const BIGNUM *p,
850ebfedea0SLionel Sambuc const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
851ebfedea0SLionel Sambuc {
852ebfedea0SLionel Sambuc return ibmca_mod_exp(r, a, p, m, ctx);
853ebfedea0SLionel Sambuc }
854ebfedea0SLionel Sambuc # endif
855ebfedea0SLionel Sambuc
856ebfedea0SLionel Sambuc /* Random bytes are good */
ibmca_rand_bytes(unsigned char * buf,int num)857ebfedea0SLionel Sambuc static int ibmca_rand_bytes(unsigned char *buf, int num)
858ebfedea0SLionel Sambuc {
859ebfedea0SLionel Sambuc int to_return = 0; /* assume failure */
860ebfedea0SLionel Sambuc unsigned int ret;
861ebfedea0SLionel Sambuc
862*0a6a1f1dSLionel Sambuc if (handle == 0) {
863ebfedea0SLionel Sambuc IBMCAerr(IBMCA_F_IBMCA_RAND_BYTES, IBMCA_R_NOT_INITIALISED);
864ebfedea0SLionel Sambuc goto err;
865ebfedea0SLionel Sambuc }
866ebfedea0SLionel Sambuc
867ebfedea0SLionel Sambuc ret = p_icaRandomNumberGenerate(handle, num, buf);
868*0a6a1f1dSLionel Sambuc if (ret < 0) {
869ebfedea0SLionel Sambuc IBMCAerr(IBMCA_F_IBMCA_RAND_BYTES, IBMCA_R_REQUEST_FAILED);
870ebfedea0SLionel Sambuc goto err;
871ebfedea0SLionel Sambuc }
872ebfedea0SLionel Sambuc to_return = 1;
873ebfedea0SLionel Sambuc err:
874ebfedea0SLionel Sambuc return to_return;
875ebfedea0SLionel Sambuc }
876ebfedea0SLionel Sambuc
ibmca_rand_status(void)877ebfedea0SLionel Sambuc static int ibmca_rand_status(void)
878ebfedea0SLionel Sambuc {
879ebfedea0SLionel Sambuc return 1;
880ebfedea0SLionel Sambuc }
881ebfedea0SLionel Sambuc
882*0a6a1f1dSLionel Sambuc /*
883*0a6a1f1dSLionel Sambuc * This stuff is needed if this ENGINE is being compiled into a
884*0a6a1f1dSLionel Sambuc * self-contained shared-library.
885*0a6a1f1dSLionel Sambuc */
886ebfedea0SLionel Sambuc # ifdef ENGINE_DYNAMIC_SUPPORT
bind_fn(ENGINE * e,const char * id)887ebfedea0SLionel Sambuc static int bind_fn(ENGINE *e, const char *id)
888ebfedea0SLionel Sambuc {
889ebfedea0SLionel Sambuc if (id && (strcmp(id, engine_ibmca_id) != 0)) /* WJH XXX */
890ebfedea0SLionel Sambuc return 0;
891ebfedea0SLionel Sambuc if (!bind_helper(e))
892ebfedea0SLionel Sambuc return 0;
893ebfedea0SLionel Sambuc return 1;
894ebfedea0SLionel Sambuc }
895*0a6a1f1dSLionel Sambuc
896ebfedea0SLionel Sambuc IMPLEMENT_DYNAMIC_CHECK_FN()
897ebfedea0SLionel Sambuc IMPLEMENT_DYNAMIC_BIND_FN(bind_fn)
898ebfedea0SLionel Sambuc # endif /* ENGINE_DYNAMIC_SUPPORT */
899ebfedea0SLionel Sambuc # endif /* !OPENSSL_NO_HW_IBMCA */
900ebfedea0SLionel Sambuc #endif /* !OPENSSL_NO_HW */
901