rsa.h revision 100931
156083Skris/* crypto/rsa/rsa.h */ 257511Speter/* $FreeBSD: head/crypto/openssl/crypto/rsa/rsa.h 100931 2002-07-30 12:46:49Z nectar $ */ 356083Skris/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 456083Skris * All rights reserved. 556083Skris * 656083Skris * This package is an SSL implementation written 756083Skris * by Eric Young (eay@cryptsoft.com). 856083Skris * The implementation was written so as to conform with Netscapes SSL. 956083Skris * 1056083Skris * This library is free for commercial and non-commercial use as long as 1156083Skris * the following conditions are aheared to. The following conditions 1256083Skris * apply to all code found in this distribution, be it the RC4, RSA, 1356083Skris * lhash, DES, etc., code; not just the SSL code. The SSL documentation 1456083Skris * included with this distribution is covered by the same copyright terms 1556083Skris * except that the holder is Tim Hudson (tjh@cryptsoft.com). 1656083Skris * 1756083Skris * Copyright remains Eric Young's, and as such any Copyright notices in 1856083Skris * the code are not to be removed. 1956083Skris * If this package is used in a product, Eric Young should be given attribution 2056083Skris * as the author of the parts of the library used. 2156083Skris * This can be in the form of a textual message at program startup or 2256083Skris * in documentation (online or textual) provided with the package. 2356083Skris * 2456083Skris * Redistribution and use in source and binary forms, with or without 2556083Skris * modification, are permitted provided that the following conditions 2656083Skris * are met: 2756083Skris * 1. Redistributions of source code must retain the copyright 2856083Skris * notice, this list of conditions and the following disclaimer. 2956083Skris * 2. Redistributions in binary form must reproduce the above copyright 3056083Skris * notice, this list of conditions and the following disclaimer in the 3156083Skris * documentation and/or other materials provided with the distribution. 3256083Skris * 3. All advertising materials mentioning features or use of this software 3356083Skris * must display the following acknowledgement: 3456083Skris * "This product includes cryptographic software written by 3556083Skris * Eric Young (eay@cryptsoft.com)" 3656083Skris * The word 'cryptographic' can be left out if the rouines from the library 3756083Skris * being used are not cryptographic related :-). 3856083Skris * 4. If you include any Windows specific code (or a derivative thereof) from 3956083Skris * the apps directory (application code) you must include an acknowledgement: 4056083Skris * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 4156083Skris * 4256083Skris * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 4356083Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 4456083Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 4556083Skris * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 4656083Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 4756083Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 4856083Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 4956083Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 5056083Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 5156083Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 5256083Skris * SUCH DAMAGE. 5356083Skris * 5456083Skris * The licence and distribution terms for any publically available version or 5556083Skris * derivative of this code cannot be changed. i.e. this code cannot simply be 5656083Skris * copied and put under another distribution licence 5756083Skris * [including the GNU Public Licence.] 5856083Skris */ 5956083Skris 6056083Skris#ifndef HEADER_RSA_H 6156083Skris#define HEADER_RSA_H 6256083Skris 6368654Skris#ifndef NO_BIO 6468654Skris#include <openssl/bio.h> 6556083Skris#endif 6656083Skris#include <openssl/bn.h> 6756083Skris#include <openssl/crypto.h> 6856083Skris 6956083Skris#ifdef NO_RSA 7056083Skris#error RSA is disabled. 7156083Skris#endif 7256083Skris 7368654Skris#ifdef __cplusplus 7468654Skrisextern "C" { 7568654Skris#endif 7668654Skris 7756083Skristypedef struct rsa_st RSA; 7856083Skris 7956083Skristypedef struct rsa_meth_st 8056083Skris { 8156083Skris const char *name; 8256083Skris int (*rsa_pub_enc)(int flen,unsigned char *from,unsigned char *to, 8356083Skris RSA *rsa,int padding); 8456083Skris int (*rsa_pub_dec)(int flen,unsigned char *from,unsigned char *to, 8556083Skris RSA *rsa,int padding); 8656083Skris int (*rsa_priv_enc)(int flen,unsigned char *from,unsigned char *to, 8756083Skris RSA *rsa,int padding); 8856083Skris int (*rsa_priv_dec)(int flen,unsigned char *from,unsigned char *to, 8956083Skris RSA *rsa,int padding); 9056083Skris int (*rsa_mod_exp)(BIGNUM *r0,BIGNUM *I,RSA *rsa); /* Can be null */ 9156083Skris int (*bn_mod_exp)(BIGNUM *r, BIGNUM *a, const BIGNUM *p, 9256083Skris const BIGNUM *m, BN_CTX *ctx, 9356083Skris BN_MONT_CTX *m_ctx); /* Can be null */ 9456083Skris int (*init)(RSA *rsa); /* called at new */ 9556083Skris int (*finish)(RSA *rsa); /* called at free */ 9656083Skris int flags; /* RSA_METHOD_FLAG_* things */ 9756083Skris char *app_data; /* may be needed! */ 9859194Skris/* New sign and verify functions: some libraries don't allow arbitrary data 9959194Skris * to be signed/verified: this allows them to be used. Note: for this to work 10059194Skris * the RSA_public_decrypt() and RSA_private_encrypt() should *NOT* be used 10159194Skris * RSA_sign(), RSA_verify() should be used instead. Note: for backwards 10259194Skris * compatibility this functionality is only enabled if the RSA_FLAG_SIGN_VER 10359194Skris * option is set in 'flags'. 10459194Skris */ 105100931Snectar 106100931Snectar/* changed m_len to m_length to avoid a conflict with a #define in 107100931Snectar vxworks for m_len for the mbuf code. This only shows up in apps 108100931Snectar that have USE_SOCKETS defined */ 109100931Snectar 110100931Snectar int (*rsa_sign)(int type, unsigned char *m, unsigned int m_length, 11159194Skris unsigned char *sigret, unsigned int *siglen, RSA *rsa); 112100931Snectar int (*rsa_verify)(int dtype, unsigned char *m, unsigned int m_length, 11359194Skris unsigned char *sigbuf, unsigned int siglen, RSA *rsa); 11459194Skris 11556083Skris } RSA_METHOD; 11656083Skris 11756083Skrisstruct rsa_st 11856083Skris { 11956083Skris /* The first parameter is used to pickup errors where 12056083Skris * this is passed instead of aEVP_PKEY, it is set to 0 */ 12156083Skris int pad; 12256083Skris int version; 12356083Skris RSA_METHOD *meth; 12456083Skris BIGNUM *n; 12556083Skris BIGNUM *e; 12656083Skris BIGNUM *d; 12756083Skris BIGNUM *p; 12856083Skris BIGNUM *q; 12956083Skris BIGNUM *dmp1; 13056083Skris BIGNUM *dmq1; 13156083Skris BIGNUM *iqmp; 13256083Skris /* be careful using this if the RSA structure is shared */ 13356083Skris CRYPTO_EX_DATA ex_data; 13456083Skris int references; 13556083Skris int flags; 13656083Skris 13756083Skris /* Used to cache montgomery values */ 13856083Skris BN_MONT_CTX *_method_mod_n; 13956083Skris BN_MONT_CTX *_method_mod_p; 14056083Skris BN_MONT_CTX *_method_mod_q; 14156083Skris 14256083Skris /* all BIGNUM values are actually in the following data, if it is not 14356083Skris * NULL */ 14456083Skris char *bignum_data; 14556083Skris BN_BLINDING *blinding; 14656083Skris }; 14756083Skris 14856083Skris#define RSA_3 0x3L 14956083Skris#define RSA_F4 0x10001L 15056083Skris 15156083Skris#define RSA_METHOD_FLAG_NO_CHECK 0x01 /* don't check pub/private match */ 15256083Skris 15356083Skris#define RSA_FLAG_CACHE_PUBLIC 0x02 15456083Skris#define RSA_FLAG_CACHE_PRIVATE 0x04 15556083Skris#define RSA_FLAG_BLINDING 0x08 15656083Skris#define RSA_FLAG_THREAD_SAFE 0x10 15756083Skris/* This flag means the private key operations will be handled by rsa_mod_exp 15856083Skris * and that they do not depend on the private key components being present: 15956083Skris * for example a key stored in external hardware. Without this flag bn_mod_exp 16056083Skris * gets called when private key components are absent. 16156083Skris */ 16256083Skris#define RSA_FLAG_EXT_PKEY 0x20 16356083Skris 16459194Skris/* This flag in the RSA_METHOD enables the new rsa_sign, rsa_verify functions. 16559194Skris */ 16659194Skris#define RSA_FLAG_SIGN_VER 0x40 16759194Skris 16856083Skris#define RSA_PKCS1_PADDING 1 16956083Skris#define RSA_SSLV23_PADDING 2 17056083Skris#define RSA_NO_PADDING 3 17156083Skris#define RSA_PKCS1_OAEP_PADDING 4 17256083Skris 17359194Skris#define RSA_set_app_data(s,arg) RSA_set_ex_data(s,0,arg) 17456083Skris#define RSA_get_app_data(s) RSA_get_ex_data(s,0) 17556083Skris 17656083SkrisRSA * RSA_new(void); 17756083SkrisRSA * RSA_new_method(RSA_METHOD *method); 17856083Skrisint RSA_size(RSA *); 17956083SkrisRSA * RSA_generate_key(int bits, unsigned long e,void 18056083Skris (*callback)(int,int,void *),void *cb_arg); 18156083Skrisint RSA_check_key(RSA *); 18256083Skris /* next 4 return -1 on error */ 18356083Skrisint RSA_public_encrypt(int flen, unsigned char *from, 18456083Skris unsigned char *to, RSA *rsa,int padding); 18556083Skrisint RSA_private_encrypt(int flen, unsigned char *from, 18656083Skris unsigned char *to, RSA *rsa,int padding); 18756083Skrisint RSA_public_decrypt(int flen, unsigned char *from, 18856083Skris unsigned char *to, RSA *rsa,int padding); 18956083Skrisint RSA_private_decrypt(int flen, unsigned char *from, 19056083Skris unsigned char *to, RSA *rsa,int padding); 19156083Skrisvoid RSA_free (RSA *r); 19256083Skris 19356083Skrisint RSA_flags(RSA *r); 19456083Skris 19556083Skrisvoid RSA_set_default_method(RSA_METHOD *meth); 19656083SkrisRSA_METHOD *RSA_get_default_method(void); 19756083SkrisRSA_METHOD *RSA_get_method(RSA *rsa); 19856083SkrisRSA_METHOD *RSA_set_method(RSA *rsa, RSA_METHOD *meth); 19956083Skris 20056083Skris/* This function needs the memory locking malloc callbacks to be installed */ 20156083Skrisint RSA_memory_lock(RSA *r); 20256083Skris 20368654Skris/* If you have RSAref compiled in. */ 20468654SkrisRSA_METHOD *RSA_PKCS1_RSAref(void); 20568654Skris 20656083Skris/* these are the actual SSLeay RSA functions */ 20768654SkrisRSA_METHOD *RSA_PKCS1_SSLeay(void); 20856083Skris 20959194SkrisRSA_METHOD *RSA_null_method(void); 21059194Skris 21156083SkrisRSA * d2i_RSAPublicKey(RSA **a, unsigned char **pp, long length); 21256083Skrisint i2d_RSAPublicKey(RSA *a, unsigned char **pp); 21356083SkrisRSA * d2i_RSAPrivateKey(RSA **a, unsigned char **pp, long length); 21456083Skrisint i2d_RSAPrivateKey(RSA *a, unsigned char **pp); 21556083Skris#ifndef NO_FP_API 21656083Skrisint RSA_print_fp(FILE *fp, RSA *r,int offset); 21756083Skris#endif 21856083Skris 21968654Skris#ifndef NO_BIO 22056083Skrisint RSA_print(BIO *bp, RSA *r,int offset); 22156083Skris#endif 22256083Skris 22368654Skrisint i2d_RSA_NET(RSA *a, unsigned char **pp, int (*cb)(), int sgckey); 22468654SkrisRSA *d2i_RSA_NET(RSA **a, unsigned char **pp, long length, int (*cb)(), int sgckey); 22568654SkrisRSA *d2i_RSA_NET_2(RSA **a, unsigned char **pp, long length, int (*cb)(), int sgckey); 22668654Skris 22756083Skrisint i2d_Netscape_RSA(RSA *a, unsigned char **pp, int (*cb)()); 22856083SkrisRSA *d2i_Netscape_RSA(RSA **a, unsigned char **pp, long length, int (*cb)()); 22956083Skris/* Naughty internal function required elsewhere, to handle a MS structure 23056083Skris * that is the same as the netscape one :-) */ 23156083SkrisRSA *d2i_Netscape_RSA_2(RSA **a, unsigned char **pp, long length, int (*cb)()); 23256083Skris 23356083Skris/* The following 2 functions sign and verify a X509_SIG ASN1 object 23456083Skris * inside PKCS#1 padded RSA encryption */ 235100931Snectarint RSA_sign(int type, unsigned char *m, unsigned int m_length, 23656083Skris unsigned char *sigret, unsigned int *siglen, RSA *rsa); 237100931Snectarint RSA_verify(int type, unsigned char *m, unsigned int m_length, 23856083Skris unsigned char *sigbuf, unsigned int siglen, RSA *rsa); 23956083Skris 24056083Skris/* The following 2 function sign and verify a ASN1_OCTET_STRING 24156083Skris * object inside PKCS#1 padded RSA encryption */ 242100931Snectarint RSA_sign_ASN1_OCTET_STRING(int type, unsigned char *m, unsigned int m_length, 24356083Skris unsigned char *sigret, unsigned int *siglen, RSA *rsa); 244100931Snectarint RSA_verify_ASN1_OCTET_STRING(int type, unsigned char *m, unsigned int m_length, 24556083Skris unsigned char *sigbuf, unsigned int siglen, RSA *rsa); 24656083Skris 24756083Skrisint RSA_blinding_on(RSA *rsa, BN_CTX *ctx); 24856083Skrisvoid RSA_blinding_off(RSA *rsa); 24956083Skris 25056083Skrisint RSA_padding_add_PKCS1_type_1(unsigned char *to,int tlen, 25156083Skris unsigned char *f,int fl); 25256083Skrisint RSA_padding_check_PKCS1_type_1(unsigned char *to,int tlen, 25356083Skris unsigned char *f,int fl,int rsa_len); 25456083Skrisint RSA_padding_add_PKCS1_type_2(unsigned char *to,int tlen, 25556083Skris unsigned char *f,int fl); 25656083Skrisint RSA_padding_check_PKCS1_type_2(unsigned char *to,int tlen, 25756083Skris unsigned char *f,int fl,int rsa_len); 25856083Skrisint RSA_padding_add_PKCS1_OAEP(unsigned char *to,int tlen, 25956083Skris unsigned char *f,int fl,unsigned char *p, 26056083Skris int pl); 26156083Skrisint RSA_padding_check_PKCS1_OAEP(unsigned char *to,int tlen, 26256083Skris unsigned char *f,int fl,int rsa_len, 26356083Skris unsigned char *p,int pl); 26456083Skrisint RSA_padding_add_SSLv23(unsigned char *to,int tlen, 26556083Skris unsigned char *f,int fl); 26656083Skrisint RSA_padding_check_SSLv23(unsigned char *to,int tlen, 26756083Skris unsigned char *f,int fl,int rsa_len); 26856083Skrisint RSA_padding_add_none(unsigned char *to,int tlen, 26956083Skris unsigned char *f,int fl); 27056083Skrisint RSA_padding_check_none(unsigned char *to,int tlen, 27156083Skris unsigned char *f,int fl,int rsa_len); 27256083Skris 27359194Skrisint RSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func, 27459194Skris CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func); 27559194Skrisint RSA_set_ex_data(RSA *r,int idx,void *arg); 27659194Skrisvoid *RSA_get_ex_data(RSA *r, int idx); 27757971Skris 27856083Skris/* BEGIN ERROR CODES */ 27956083Skris/* The following lines are auto generated by the script mkerr.pl. Any changes 28056083Skris * made after this point may be overwritten when the script is next run. 28156083Skris */ 28289840Skrisvoid ERR_load_RSA_strings(void); 28356083Skris 28456083Skris/* Error codes for the RSA functions. */ 28556083Skris 28656083Skris/* Function codes. */ 28756083Skris#define RSA_F_MEMORY_LOCK 100 28856083Skris#define RSA_F_RSA_CHECK_KEY 123 28956083Skris#define RSA_F_RSA_EAY_PRIVATE_DECRYPT 101 29056083Skris#define RSA_F_RSA_EAY_PRIVATE_ENCRYPT 102 29156083Skris#define RSA_F_RSA_EAY_PUBLIC_DECRYPT 103 29256083Skris#define RSA_F_RSA_EAY_PUBLIC_ENCRYPT 104 29356083Skris#define RSA_F_RSA_GENERATE_KEY 105 29456083Skris#define RSA_F_RSA_NEW_METHOD 106 29559194Skris#define RSA_F_RSA_NULL 124 29656083Skris#define RSA_F_RSA_PADDING_ADD_NONE 107 29756083Skris#define RSA_F_RSA_PADDING_ADD_PKCS1_OAEP 121 29856083Skris#define RSA_F_RSA_PADDING_ADD_PKCS1_TYPE_1 108 29956083Skris#define RSA_F_RSA_PADDING_ADD_PKCS1_TYPE_2 109 30056083Skris#define RSA_F_RSA_PADDING_ADD_SSLV23 110 30156083Skris#define RSA_F_RSA_PADDING_CHECK_NONE 111 30256083Skris#define RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP 122 30356083Skris#define RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1 112 30456083Skris#define RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_2 113 30556083Skris#define RSA_F_RSA_PADDING_CHECK_SSLV23 114 30656083Skris#define RSA_F_RSA_PRINT 115 30756083Skris#define RSA_F_RSA_PRINT_FP 116 30856083Skris#define RSA_F_RSA_SIGN 117 30956083Skris#define RSA_F_RSA_SIGN_ASN1_OCTET_STRING 118 31056083Skris#define RSA_F_RSA_VERIFY 119 31156083Skris#define RSA_F_RSA_VERIFY_ASN1_OCTET_STRING 120 31256083Skris 31356083Skris/* Reason codes. */ 31456083Skris#define RSA_R_ALGORITHM_MISMATCH 100 31556083Skris#define RSA_R_BAD_E_VALUE 101 31656083Skris#define RSA_R_BAD_FIXED_HEADER_DECRYPT 102 31756083Skris#define RSA_R_BAD_PAD_BYTE_COUNT 103 31856083Skris#define RSA_R_BAD_SIGNATURE 104 31956083Skris#define RSA_R_BLOCK_TYPE_IS_NOT_01 106 32056083Skris#define RSA_R_BLOCK_TYPE_IS_NOT_02 107 32156083Skris#define RSA_R_DATA_GREATER_THAN_MOD_LEN 108 32256083Skris#define RSA_R_DATA_TOO_LARGE 109 32356083Skris#define RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE 110 32489840Skris#define RSA_R_DATA_TOO_LARGE_FOR_MODULUS 132 32556083Skris#define RSA_R_DATA_TOO_SMALL 111 32656083Skris#define RSA_R_DATA_TOO_SMALL_FOR_KEY_SIZE 122 32756083Skris#define RSA_R_DIGEST_TOO_BIG_FOR_RSA_KEY 112 32856083Skris#define RSA_R_DMP1_NOT_CONGRUENT_TO_D 124 32956083Skris#define RSA_R_DMQ1_NOT_CONGRUENT_TO_D 125 33059194Skris#define RSA_R_D_E_NOT_CONGRUENT_TO_1 123 33159194Skris#define RSA_R_INVALID_MESSAGE_LENGTH 131 33256083Skris#define RSA_R_IQMP_NOT_INVERSE_OF_Q 126 33356083Skris#define RSA_R_KEY_SIZE_TOO_SMALL 120 33456083Skris#define RSA_R_NULL_BEFORE_BLOCK_MISSING 113 33556083Skris#define RSA_R_N_DOES_NOT_EQUAL_P_Q 127 33656083Skris#define RSA_R_OAEP_DECODING_ERROR 121 33756083Skris#define RSA_R_PADDING_CHECK_FAILED 114 33856083Skris#define RSA_R_P_NOT_PRIME 128 33956083Skris#define RSA_R_Q_NOT_PRIME 129 34059194Skris#define RSA_R_RSA_OPERATIONS_NOT_SUPPORTED 130 34156083Skris#define RSA_R_SSLV3_ROLLBACK_ATTACK 115 34256083Skris#define RSA_R_THE_ASN1_OBJECT_IDENTIFIER_IS_NOT_KNOWN_FOR_THIS_MD 116 34356083Skris#define RSA_R_UNKNOWN_ALGORITHM_TYPE 117 34456083Skris#define RSA_R_UNKNOWN_PADDING_TYPE 118 34556083Skris#define RSA_R_WRONG_SIGNATURE_LENGTH 119 34656083Skris 34756083Skris#ifdef __cplusplus 34856083Skris} 34956083Skris#endif 35056083Skris#endif 351