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