1# This file is dual licensed under the terms of the Apache License, Version
2# 2.0, and the BSD License. See the LICENSE file in the root of this repository
3# for complete details.
4
5from __future__ import absolute_import, division, print_function
6
7INCLUDES = """
8#include <openssl/rsa.h>
9"""
10
11TYPES = """
12typedef ... RSA;
13typedef ... BN_GENCB;
14static const int RSA_PKCS1_PADDING;
15static const int RSA_NO_PADDING;
16static const int RSA_PKCS1_OAEP_PADDING;
17static const int RSA_PKCS1_PSS_PADDING;
18static const int RSA_F4;
19
20static const int Cryptography_HAS_PSS_PADDING;
21static const int Cryptography_HAS_RSA_OAEP_MD;
22static const int Cryptography_HAS_RSA_OAEP_LABEL;
23"""
24
25FUNCTIONS = """
26RSA *RSA_new(void);
27void RSA_free(RSA *);
28int RSA_size(const RSA *);
29int RSA_generate_key_ex(RSA *, int, BIGNUM *, BN_GENCB *);
30int RSA_check_key(const RSA *);
31RSA *RSAPublicKey_dup(RSA *);
32int RSA_blinding_on(RSA *, BN_CTX *);
33int RSA_public_encrypt(int, const unsigned char *, unsigned char *,
34                       RSA *, int);
35int RSA_private_encrypt(int, const unsigned char *, unsigned char *,
36                        RSA *, int);
37int RSA_public_decrypt(int, const unsigned char *, unsigned char *,
38                       RSA *, int);
39int RSA_private_decrypt(int, const unsigned char *, unsigned char *,
40                        RSA *, int);
41int RSA_print(BIO *, const RSA *, int);
42
43/* added in 1.1.0 when the RSA struct was opaqued */
44int RSA_set0_key(RSA *, BIGNUM *, BIGNUM *, BIGNUM *);
45int RSA_set0_factors(RSA *, BIGNUM *, BIGNUM *);
46int RSA_set0_crt_params(RSA *, BIGNUM *, BIGNUM *, BIGNUM *);
47void RSA_get0_key(const RSA *, const BIGNUM **, const BIGNUM **,
48                  const BIGNUM **);
49void RSA_get0_factors(const RSA *, const BIGNUM **, const BIGNUM **);
50void RSA_get0_crt_params(const RSA *, const BIGNUM **, const BIGNUM **,
51                         const BIGNUM **);
52int EVP_PKEY_CTX_set_rsa_padding(EVP_PKEY_CTX *, int);
53int EVP_PKEY_CTX_set_rsa_pss_saltlen(EVP_PKEY_CTX *, int);
54int EVP_PKEY_CTX_set_rsa_mgf1_md(EVP_PKEY_CTX *, EVP_MD *);
55int EVP_PKEY_CTX_set0_rsa_oaep_label(EVP_PKEY_CTX *, unsigned char *, int);
56
57int EVP_PKEY_CTX_set_rsa_oaep_md(EVP_PKEY_CTX *, EVP_MD *);
58"""
59
60CUSTOMIZATIONS = """
61static const long Cryptography_HAS_PSS_PADDING = 1;
62
63#if defined(EVP_PKEY_CTX_set_rsa_oaep_md)
64static const long Cryptography_HAS_RSA_OAEP_MD = 1;
65#else
66static const long Cryptography_HAS_RSA_OAEP_MD = 0;
67int (*EVP_PKEY_CTX_set_rsa_oaep_md)(EVP_PKEY_CTX *, EVP_MD *) = NULL;
68#endif
69
70#if defined(EVP_PKEY_CTX_set0_rsa_oaep_label)
71static const long Cryptography_HAS_RSA_OAEP_LABEL = 1;
72#else
73static const long Cryptography_HAS_RSA_OAEP_LABEL = 0;
74int (*EVP_PKEY_CTX_set0_rsa_oaep_label)(EVP_PKEY_CTX *, unsigned char *,
75                                        int) = NULL;
76#endif
77
78/* These functions were added in OpenSSL 1.1.0 */
79#if CRYPTOGRAPHY_OPENSSL_LESS_THAN_110 && !CRYPTOGRAPHY_LIBRESSL_27_OR_GREATER
80int RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d)
81{
82    /* If the fields n and e in r are NULL, the corresponding input
83     * parameters MUST be non-NULL for n and e.  d may be
84     * left NULL (in case only the public key is used).
85     */
86    if ((r->n == NULL && n == NULL)
87        || (r->e == NULL && e == NULL))
88        return 0;
89
90    if (n != NULL) {
91        BN_free(r->n);
92        r->n = n;
93    }
94    if (e != NULL) {
95        BN_free(r->e);
96        r->e = e;
97    }
98    if (d != NULL) {
99        BN_free(r->d);
100        r->d = d;
101    }
102
103    return 1;
104}
105
106int RSA_set0_factors(RSA *r, BIGNUM *p, BIGNUM *q)
107{
108    /* If the fields p and q in r are NULL, the corresponding input
109     * parameters MUST be non-NULL.
110     */
111    if ((r->p == NULL && p == NULL)
112        || (r->q == NULL && q == NULL))
113        return 0;
114
115    if (p != NULL) {
116        BN_free(r->p);
117        r->p = p;
118    }
119    if (q != NULL) {
120        BN_free(r->q);
121        r->q = q;
122    }
123
124    return 1;
125}
126
127int RSA_set0_crt_params(RSA *r, BIGNUM *dmp1, BIGNUM *dmq1, BIGNUM *iqmp)
128{
129    /* If the fields dmp1, dmq1 and iqmp in r are NULL, the corresponding input
130     * parameters MUST be non-NULL.
131     */
132    if ((r->dmp1 == NULL && dmp1 == NULL)
133        || (r->dmq1 == NULL && dmq1 == NULL)
134        || (r->iqmp == NULL && iqmp == NULL))
135        return 0;
136
137    if (dmp1 != NULL) {
138        BN_free(r->dmp1);
139        r->dmp1 = dmp1;
140    }
141    if (dmq1 != NULL) {
142        BN_free(r->dmq1);
143        r->dmq1 = dmq1;
144    }
145    if (iqmp != NULL) {
146        BN_free(r->iqmp);
147        r->iqmp = iqmp;
148    }
149
150    return 1;
151}
152
153void RSA_get0_key(const RSA *r,
154                  const BIGNUM **n, const BIGNUM **e, const BIGNUM **d)
155{
156    if (n != NULL)
157        *n = r->n;
158    if (e != NULL)
159        *e = r->e;
160    if (d != NULL)
161        *d = r->d;
162}
163
164void RSA_get0_factors(const RSA *r, const BIGNUM **p, const BIGNUM **q)
165{
166    if (p != NULL)
167        *p = r->p;
168    if (q != NULL)
169        *q = r->q;
170}
171
172void RSA_get0_crt_params(const RSA *r,
173                         const BIGNUM **dmp1, const BIGNUM **dmq1,
174                         const BIGNUM **iqmp)
175{
176    if (dmp1 != NULL)
177        *dmp1 = r->dmp1;
178    if (dmq1 != NULL)
179        *dmq1 = r->dmq1;
180    if (iqmp != NULL)
181        *iqmp = r->iqmp;
182}
183#endif
184"""
185