Coverage Report

Created: 2022-11-30 06:20

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