1/*	$NetBSD: rsa.h,v 1.2 2017/01/28 21:31:47 christos Exp $	*/
2
3/*
4 * Copyright (c) 2006-2016 Kungliga Tekniska H��gskolan
5 * (Royal Institute of Technology, Stockholm, Sweden).
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 *
19 * 3. Neither the name of the Institute nor the names of its contributors
20 *    may be used to endorse or promote products derived from this software
21 *    without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36/*
37 * Id
38 */
39
40#ifndef _HEIM_RSA_H
41#define _HEIM_RSA_H 1
42
43/* symbol renaming */
44#define RSA_null_method hc_RSA_null_method
45#define RSA_ltm_method hc_RSA_ltm_method
46#define RSA_gmp_method hc_RSA_gmp_method
47#define RSA_tfm_method hc_RSA_tfm_method
48#define RSA_new hc_RSA_new
49#define RSA_new_method hc_RSA_new_method
50#define RSA_free hc_RSA_free
51#define RSA_up_ref hc_RSA_up_ref
52#define RSA_set_default_method hc_RSA_set_default_method
53#define RSA_get_default_method hc_RSA_get_default_method
54#define RSA_set_method hc_RSA_set_method
55#define RSA_get_method hc_RSA_get_method
56#define RSA_set_app_data hc_RSA_set_app_data
57#define RSA_get_app_data hc_RSA_get_app_data
58#define RSA_check_key hc_RSA_check_key
59#define RSA_size hc_RSA_size
60#define RSA_public_encrypt hc_RSA_public_encrypt
61#define RSA_public_decrypt hc_RSA_public_decrypt
62#define RSA_private_encrypt hc_RSA_private_encrypt
63#define RSA_private_decrypt hc_RSA_private_decrypt
64#define RSA_sign hc_RSA_sign
65#define RSA_verify hc_RSA_verify
66#define RSA_generate_key_ex hc_RSA_generate_key_ex
67#define d2i_RSAPrivateKey hc_d2i_RSAPrivateKey
68#define i2d_RSAPrivateKey hc_i2d_RSAPrivateKey
69#define i2d_RSAPublicKey hc_i2d_RSAPublicKey
70#define d2i_RSAPublicKey hc_d2i_RSAPublicKey
71
72#define RSA hc_RSA
73#define RSA_METHOD hc_RSA_METHOD
74
75/*
76 *
77 */
78
79typedef struct RSA RSA;
80typedef struct RSA_METHOD RSA_METHOD;
81
82#include <hcrypto/bn.h>
83#include <hcrypto/engine.h>
84
85struct RSA_METHOD {
86    const char *name;
87    int (*rsa_pub_enc)(int,const unsigned char *, unsigned char *, RSA *,int);
88    int (*rsa_pub_dec)(int,const unsigned char *, unsigned char *, RSA *,int);
89    int (*rsa_priv_enc)(int,const unsigned char *, unsigned char *, RSA *,int);
90    int (*rsa_priv_dec)(int,const unsigned char *, unsigned char *, RSA *,int);
91    void *rsa_mod_exp;
92    void *bn_mod_exp;
93    int (*init)(RSA *rsa);
94    int (*finish)(RSA *rsa);
95    int flags;
96    char *app_data;
97    int (*rsa_sign)(int, const unsigned char *, unsigned int,
98		    unsigned char *, unsigned int *, const RSA *);
99    int (*rsa_verify)(int, const unsigned char *, unsigned int,
100		      unsigned char *, unsigned int, const RSA *);
101    int (*rsa_keygen)(RSA *, int, BIGNUM *, BN_GENCB *);
102};
103
104struct RSA {
105    int pad;
106    long version;
107    const RSA_METHOD *meth;
108    void *engine;
109    BIGNUM *n;
110    BIGNUM *e;
111    BIGNUM *d;
112    BIGNUM *p;
113    BIGNUM *q;
114    BIGNUM *dmp1;
115    BIGNUM *dmq1;
116    BIGNUM *iqmp;
117    struct rsa_CRYPTO_EX_DATA {
118	void *sk;
119	int dummy;
120    } ex_data;
121    int references;
122    int flags;
123    void *_method_mod_n;
124    void *_method_mod_p;
125    void *_method_mod_q;
126
127    char *bignum_data;
128    void *blinding;
129    void *mt_blinding;
130};
131
132#define RSA_FLAG_NO_BLINDING		0x0080
133
134#define RSA_PKCS1_PADDING		1
135#define RSA_PKCS1_OAEP_PADDING		4
136#define RSA_PKCS1_PADDING_SIZE		11
137
138/*
139 *
140 */
141
142const RSA_METHOD *RSA_null_method(void);
143const RSA_METHOD *RSA_gmp_method(void);
144const RSA_METHOD *RSA_tfm_method(void);
145const RSA_METHOD *RSA_ltm_method(void);
146
147/*
148 *
149 */
150
151RSA *	RSA_new(void);
152RSA *	RSA_new_method(ENGINE *);
153void	RSA_free(RSA *);
154int	RSA_up_ref(RSA *);
155
156void	RSA_set_default_method(const RSA_METHOD *);
157const RSA_METHOD * RSA_get_default_method(void);
158
159const RSA_METHOD * RSA_get_method(const RSA *);
160int RSA_set_method(RSA *, const RSA_METHOD *);
161
162int	RSA_set_app_data(RSA *, void *arg);
163void *	RSA_get_app_data(const RSA *);
164
165int	RSA_check_key(const RSA *);
166int	RSA_size(const RSA *);
167
168int	RSA_public_encrypt(int,const unsigned char*,unsigned char*,RSA *,int);
169int	RSA_private_encrypt(int,const unsigned char*,unsigned char*,RSA *,int);
170int	RSA_public_decrypt(int,const unsigned char*,unsigned char*,RSA *,int);
171int	RSA_private_decrypt(int,const unsigned char*,unsigned char*,RSA *,int);
172
173int RSA_sign(int, const unsigned char *, unsigned int,
174	     unsigned char *, unsigned int *, RSA *);
175int RSA_verify(int, const unsigned char *, unsigned int,
176	       unsigned char *, unsigned int, RSA *);
177
178int	RSA_generate_key_ex(RSA *, int, BIGNUM *, BN_GENCB *);
179
180RSA *	d2i_RSAPrivateKey(RSA *, const unsigned char **, size_t);
181int	i2d_RSAPrivateKey(RSA *, unsigned char **);
182
183int	i2d_RSAPublicKey(RSA *, unsigned char **);
184RSA *	d2i_RSAPublicKey(RSA *, const unsigned char **, size_t);
185
186#endif /* _HEIM_RSA_H */
187