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