8ba6e8c2ee4c50010771cef31cd5661ff1e91c89
[openssl.git] / crypto / rsa / rsa_depr.c
1 /*
2  * Copyright 2002-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 /*
11  * NB: This file contains deprecated functions (compatibility wrappers to the
12  * "new" versions).
13  */
14
15 /*
16  * RSA low level APIs are deprecated for public use, but still ok for
17  * internal use.
18  */
19 #include "internal/deprecated.h"
20
21 #include <openssl/opensslconf.h>
22 #ifdef OPENSSL_NO_DEPRECATED_0_9_8
23 NON_EMPTY_TRANSLATION_UNIT
24
25 #else
26
27 # include <stdio.h>
28 # include <time.h>
29 # include "internal/cryptlib.h"
30 # include <openssl/bn.h>
31 # include <openssl/rsa.h>
32
33 RSA *RSA_generate_key(int bits, unsigned long e_value,
34                       void (*callback) (int, int, void *), void *cb_arg)
35 {
36     int i;
37     BN_GENCB *cb = BN_GENCB_new();
38     RSA *rsa = RSA_new();
39     BIGNUM *e = BN_new();
40
41     if (cb == NULL || rsa == NULL || e == NULL)
42         goto err;
43
44     /*
45      * The problem is when building with 8, 16, or 32 BN_ULONG, unsigned long
46      * can be larger
47      */
48     for (i = 0; i < (int)sizeof(unsigned long) * 8; i++) {
49         if (e_value & (1UL << i))
50             if (BN_set_bit(e, i) == 0)
51                 goto err;
52     }
53
54     BN_GENCB_set_old(cb, callback, cb_arg);
55
56     if (RSA_generate_key_ex(rsa, bits, e, cb)) {
57         BN_free(e);
58         BN_GENCB_free(cb);
59         return rsa;
60     }
61  err:
62     BN_free(e);
63     RSA_free(rsa);
64     BN_GENCB_free(cb);
65     return 0;
66 }
67 #endif