1 /* $NetBSD: rsa.h,v 1.1.1.2 2011/04/14 14:08:33 elric Exp $ */ 2 3 /* 4 * Copyright (c) 2006 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 /* 73 * 74 */ 75 76 typedef struct RSA RSA; 77 typedef struct RSA_METHOD RSA_METHOD; 78 79 #include <hcrypto/bn.h> 80 #include <hcrypto/engine.h> 81 82 struct RSA_METHOD { 83 const char *name; 84 int (*rsa_pub_enc)(int,const unsigned char *, unsigned char *, RSA *,int); 85 int (*rsa_pub_dec)(int,const unsigned char *, unsigned char *, RSA *,int); 86 int (*rsa_priv_enc)(int,const unsigned char *, unsigned char *, RSA *,int); 87 int (*rsa_priv_dec)(int,const unsigned char *, unsigned char *, RSA *,int); 88 void *rsa_mod_exp; 89 void *bn_mod_exp; 90 int (*init)(RSA *rsa); 91 int (*finish)(RSA *rsa); 92 int flags; 93 char *app_data; 94 int (*rsa_sign)(int, const unsigned char *, unsigned int, 95 unsigned char *, unsigned int *, const RSA *); 96 int (*rsa_verify)(int, const unsigned char *, unsigned int, 97 unsigned char *, unsigned int, const RSA *); 98 int (*rsa_keygen)(RSA *, int, BIGNUM *, BN_GENCB *); 99 }; 100 101 struct RSA { 102 int pad; 103 long version; 104 const RSA_METHOD *meth; 105 void *engine; 106 BIGNUM *n; 107 BIGNUM *e; 108 BIGNUM *d; 109 BIGNUM *p; 110 BIGNUM *q; 111 BIGNUM *dmp1; 112 BIGNUM *dmq1; 113 BIGNUM *iqmp; 114 struct rsa_CRYPTO_EX_DATA { 115 void *sk; 116 int dummy; 117 } ex_data; 118 int references; 119 int flags; 120 void *_method_mod_n; 121 void *_method_mod_p; 122 void *_method_mod_q; 123 124 char *bignum_data; 125 void *blinding; 126 void *mt_blinding; 127 }; 128 129 #define RSA_FLAG_NO_BLINDING 0x0080 130 131 #define RSA_PKCS1_PADDING 1 132 #define RSA_PKCS1_OAEP_PADDING 4 133 #define RSA_PKCS1_PADDING_SIZE 11 134 135 /* 136 * 137 */ 138 139 const RSA_METHOD *RSA_null_method(void); 140 const RSA_METHOD *RSA_gmp_method(void); 141 const RSA_METHOD *RSA_tfm_method(void); 142 const RSA_METHOD *RSA_ltm_method(void); 143 144 /* 145 * 146 */ 147 148 RSA * RSA_new(void); 149 RSA * RSA_new_method(ENGINE *); 150 void RSA_free(RSA *); 151 int RSA_up_ref(RSA *); 152 153 void RSA_set_default_method(const RSA_METHOD *); 154 const RSA_METHOD * RSA_get_default_method(void); 155 156 const RSA_METHOD * RSA_get_method(const RSA *); 157 int RSA_set_method(RSA *, const RSA_METHOD *); 158 159 int RSA_set_app_data(RSA *, void *arg); 160 void * RSA_get_app_data(const RSA *); 161 162 int RSA_check_key(const RSA *); 163 int RSA_size(const RSA *); 164 165 int RSA_public_encrypt(int,const unsigned char*,unsigned char*,RSA *,int); 166 int RSA_private_encrypt(int,const unsigned char*,unsigned char*,RSA *,int); 167 int RSA_public_decrypt(int,const unsigned char*,unsigned char*,RSA *,int); 168 int RSA_private_decrypt(int,const unsigned char*,unsigned char*,RSA *,int); 169 170 int RSA_sign(int, const unsigned char *, unsigned int, 171 unsigned char *, unsigned int *, RSA *); 172 int RSA_verify(int, const unsigned char *, unsigned int, 173 unsigned char *, unsigned int, RSA *); 174 175 int RSA_generate_key_ex(RSA *, int, BIGNUM *, BN_GENCB *); 176 177 RSA * d2i_RSAPrivateKey(RSA *, const unsigned char **, size_t); 178 int i2d_RSAPrivateKey(RSA *, unsigned char **); 179 180 int i2d_RSAPublicKey(RSA *, unsigned char **); 181 RSA * d2i_RSAPublicKey(RSA *, const unsigned char **, size_t); 182 183 #endif /* _HEIM_RSA_H */ 184