1/*
2 * Copyright (c) 2011-12 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24/*
25 * Copyright (c) 2006 Kungliga Tekniska Högskolan
26 * (Royal Institute of Technology, Stockholm, Sweden).
27 * All rights reserved.
28 *
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
31 * are met:
32 *
33 * 1. Redistributions of source code must retain the above copyright
34 *    notice, this list of conditions and the following disclaimer.
35 *
36 * 2. Redistributions in binary form must reproduce the above copyright
37 *    notice, this list of conditions and the following disclaimer in the
38 *    documentation and/or other materials provided with the distribution.
39 *
40 * 3. Neither the name of the Institute nor the names of its contributors
41 *    may be used to endorse or promote products derived from this software
42 *    without specific prior written permission.
43 *
44 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
45 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47 * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
48 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54 * SUCH DAMAGE.
55 */
56
57#ifndef _OSSL_RSA_H_
58#define _OSSL_RSA_H_			1
59
60/* symbol renaming */
61#define RSA_null_method			ossl_RSA_null_method
62#define RSA_imath_method		ossl_RSA_imath_method
63#define RSA_cdsa_method			ossl_RSA_cdsa_method
64#define RSA_tfm_method			ossl_RSA_tfm_method
65#define RSA_ltm_method			ossl_RSA_ltm_method
66#define RSA_gmp_method			ossl_RSA_gmp_method
67#define RSA_tfm_method			ossl_RSA_tfm_method
68#define RSA_new				ossl_RSA_new
69#define RSA_new_method			ossl_RSA_new_method
70#define RSA_free			ossl_RSA_free
71#define RSA_up_ref			ossl_RSA_up_ref
72#define RSA_set_default_method		ossl_RSA_set_default_method
73#define RSA_get_default_method		ossl_RSA_get_default_method
74#define RSA_set_method			ossl_RSA_set_method
75#define RSA_get_method			ossl_RSA_get_method
76#define RSA_set_app_data		ossl_RSA_set_app_data
77#define RSA_get_app_data		ossl_RSA_get_app_data
78#define RSA_check_key			ossl_RSA_check_key
79#define RSA_size			ossl_RSA_size
80#define RSA_public_encrypt		ossl_RSA_public_encrypt
81#define RSA_public_decrypt		ossl_RSA_public_decrypt
82#define RSA_private_encrypt		ossl_RSA_private_encrypt
83#define RSA_private_decrypt		ossl_RSA_private_decrypt
84#define RSA_sign			ossl_RSA_sign
85#define RSA_verify			ossl_RSA_verify
86#define RSA_generate_key_ex		ossl_RSA_generate_key_ex
87#define d2i_RSAPrivateKey		ossl_d2i_RSAPrivateKey
88#define i2d_RSAPrivateKey		ossl_i2d_RSAPrivateKey
89#define i2d_RSAPublicKey		ossl_i2d_RSAPublicKey
90#define d2i_RSAPublicKey		ossl_d2i_RSAPublicKey
91#define RSAPublicKey_dup		ossl_RSAPublicKey_dup
92#define RSAPrivateKey_dup		ossl_RSAPrivateKey_dup
93#define RSA_blinding_on			ossl_RSA_blinding_on
94
95/*
96 *
97 */
98
99typedef struct RSA		RSA;
100typedef struct RSA_METHOD	RSA_METHOD;
101
102#include "ossl-bn.h"
103#include "ossl-engine.h"
104
105struct RSA_METHOD {
106	const char *	name;
107	int		(*rsa_pub_enc)(int, const unsigned char *, unsigned char *, RSA *, int);
108	int		(*rsa_pub_dec)(int, const unsigned char *, unsigned char *, RSA *, int);
109	int		(*rsa_priv_enc)(int, const unsigned char *, unsigned char *, RSA *, int);
110	int		(*rsa_priv_dec)(int, const unsigned char *, unsigned char *, RSA *, int);
111	int		(*rsa_mod_exp)(BIGNUM *, const BIGNUM *, RSA *, BN_CTX *);
112	int		(*bn_mod_exp)(BIGNUM *, const BIGNUM *, const BIGNUM *, const BIGNUM *, BN_CTX *, BN_MONT_CTX *);
113	int		(*init)(RSA *rsa);
114	int		(*finish)(RSA *rsa);
115	int		flags;
116	char *		app_data;
117	int		(*rsa_sign)(int, const unsigned char *, unsigned int,
118	    unsigned char *, unsigned int *, const RSA *);
119	int		(*rsa_verify)(int, const unsigned char *, unsigned int,
120	    unsigned char *, unsigned int, const RSA *);
121	int		(*rsa_keygen)(RSA *, int, BIGNUM *, BN_GENCB *);
122};
123
124struct RSA {
125	int			pad;
126	long			version;
127	const RSA_METHOD *	meth;
128	void *			engine;
129	BIGNUM *		n;
130	BIGNUM *		e;
131	BIGNUM *		d;
132	BIGNUM *		p;
133	BIGNUM *		q;
134	BIGNUM *		dmp1;
135	BIGNUM *		dmq1;
136	BIGNUM *		iqmp;
137	struct rsa_CRYPTO_EX_DATA {
138		void *	sk;
139		int	dummy;
140	}
141	ex_data;
142	int			references;
143	int			flags;
144
145	BN_MONT_CTX *		_method_mod_n;
146	BN_MONT_CTX *		_method_mod_p;
147	BN_MONT_CTX *		_method_mod_q;
148
149	char *			bignum_data;
150	void *			blinding;
151	void *			mt_blinding;
152};
153
154#define RSA_FLAG_NO_BLINDING		0x0080
155#define RSA_FLAG_EXT_PKEY		0x0020
156#define RSA_FLAG_NO_CONSTTIME		0x0100
157
158#define RSA_PKCS1_PADDING		1
159#define RSA_PKCS1_OAEP_PADDING		4
160#define RSA_PKCS1_PADDING_SIZE		11
161
162#define RSA_F4				0x10001
163
164/*
165 *
166 */
167
168const RSA_METHOD *RSA_null_method(void);
169const RSA_METHOD *RSA_gmp_method(void);
170const RSA_METHOD *RSA_cdsa_method(void);
171const RSA_METHOD *RSA_tfm_method(void);
172const RSA_METHOD *RSA_ltm_method(void);
173
174/*
175 *
176 */
177
178RSA *RSA_new(void);
179RSA *RSA_new_method(ENGINE *);
180void RSA_free(RSA *);
181int RSA_up_ref(RSA *);
182
183void RSA_set_default_method(const RSA_METHOD *);
184const RSA_METHOD *RSA_get_default_method(void);
185
186const RSA_METHOD *RSA_get_method(const RSA *);
187int RSA_set_method(RSA *, const RSA_METHOD *);
188
189int RSA_set_app_data(RSA *, void *arg);
190void *RSA_get_app_data(const RSA *);
191
192int RSA_check_key(const RSA *);
193int RSA_size(const RSA *);
194
195int RSA_public_encrypt(int, const unsigned char *, unsigned char *, RSA *, int);
196int RSA_private_encrypt(int, const unsigned char *, unsigned char *, RSA *, int);
197int RSA_public_decrypt(int, const unsigned char *, unsigned char *, RSA *, int);
198int RSA_private_decrypt(int, const unsigned char *, unsigned char *, RSA *, int);
199
200int RSA_sign(int, const unsigned char *, unsigned int,
201		    unsigned char *, unsigned int *, RSA *);
202int RSA_verify(int, const unsigned char *, unsigned int,
203		    unsigned char *, unsigned int, RSA *);
204
205int RSA_generate_key_ex(RSA *, int, BIGNUM *, BN_GENCB *);
206
207RSA *d2i_RSAPrivateKey(RSA **, const unsigned char **, long);
208int i2d_RSAPrivateKey(const RSA *, unsigned char **);
209
210int i2d_RSAPublicKey(const RSA *, unsigned char **);
211RSA *d2i_RSAPublicKey(RSA **, const unsigned char **, long);
212RSA *RSAPublicKey_dup(RSA *rsa);
213RSA *RSAPrivateKey_dup(RSA *rsa);
214
215int RSA_blinding_on(RSA *rsa, BN_CTX *ctx);
216
217#endif /* _OSSL_RSA_H_ */
218