PolarSSL v1.1.4
rsa.h
Go to the documentation of this file.
00001 
00027 #ifndef POLARSSL_RSA_H
00028 #define POLARSSL_RSA_H
00029 
00030 #include "bignum.h"
00031 
00032 /*
00033  * RSA Error codes
00034  */
00035 #define POLARSSL_ERR_RSA_BAD_INPUT_DATA                    -0x4080  
00036 #define POLARSSL_ERR_RSA_INVALID_PADDING                   -0x4100  
00037 #define POLARSSL_ERR_RSA_KEY_GEN_FAILED                    -0x4180  
00038 #define POLARSSL_ERR_RSA_KEY_CHECK_FAILED                  -0x4200  
00039 #define POLARSSL_ERR_RSA_PUBLIC_FAILED                     -0x4280  
00040 #define POLARSSL_ERR_RSA_PRIVATE_FAILED                    -0x4300  
00041 #define POLARSSL_ERR_RSA_VERIFY_FAILED                     -0x4380  
00042 #define POLARSSL_ERR_RSA_OUTPUT_TOO_LARGE                  -0x4400  
00043 #define POLARSSL_ERR_RSA_RNG_FAILED                        -0x4480  
00045 /*
00046  * PKCS#1 constants
00047  */
00048 #define SIG_RSA_RAW     0
00049 #define SIG_RSA_MD2     2
00050 #define SIG_RSA_MD4     3
00051 #define SIG_RSA_MD5     4
00052 #define SIG_RSA_SHA1    5
00053 #define SIG_RSA_SHA224 14
00054 #define SIG_RSA_SHA256 11
00055 #define SIG_RSA_SHA384 12
00056 #define SIG_RSA_SHA512 13
00057 
00058 #define RSA_PUBLIC      0
00059 #define RSA_PRIVATE     1
00060 
00061 #define RSA_PKCS_V15    0
00062 #define RSA_PKCS_V21    1
00063 
00064 #define RSA_SIGN        1
00065 #define RSA_CRYPT       2
00066 
00067 #define ASN1_STR_CONSTRUCTED_SEQUENCE   "\x30"
00068 #define ASN1_STR_NULL                   "\x05"
00069 #define ASN1_STR_OID                    "\x06"
00070 #define ASN1_STR_OCTET_STRING           "\x04"
00071 
00072 #define OID_DIGEST_ALG_MDX              "\x2A\x86\x48\x86\xF7\x0D\x02\x00"
00073 #define OID_HASH_ALG_SHA1               "\x2b\x0e\x03\x02\x1a"
00074 #define OID_HASH_ALG_SHA2X              "\x60\x86\x48\x01\x65\x03\x04\x02\x00"
00075 
00076 #define OID_ISO_MEMBER_BODIES           "\x2a"
00077 #define OID_ISO_IDENTIFIED_ORG          "\x2b"
00078 
00079 /*
00080  * ISO Member bodies OID parts
00081  */
00082 #define OID_COUNTRY_US                  "\x86\x48"
00083 #define OID_RSA_DATA_SECURITY           "\x86\xf7\x0d"
00084 
00085 /*
00086  * ISO Identified organization OID parts
00087  */
00088 #define OID_OIW_SECSIG_SHA1             "\x0e\x03\x02\x1a"
00089 
00090 /*
00091  * DigestInfo ::= SEQUENCE {
00092  *   digestAlgorithm DigestAlgorithmIdentifier,
00093  *   digest Digest }
00094  *
00095  * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
00096  *
00097  * Digest ::= OCTET STRING
00098  */
00099 #define ASN1_HASH_MDX                           \
00100 (                                               \
00101     ASN1_STR_CONSTRUCTED_SEQUENCE "\x20"        \
00102       ASN1_STR_CONSTRUCTED_SEQUENCE "\x0C"      \
00103         ASN1_STR_OID "\x08"                     \
00104       OID_DIGEST_ALG_MDX                        \
00105     ASN1_STR_NULL "\x00"                        \
00106       ASN1_STR_OCTET_STRING "\x10"              \
00107 )
00108 
00109 #define ASN1_HASH_SHA1                          \
00110     ASN1_STR_CONSTRUCTED_SEQUENCE "\x21"        \
00111       ASN1_STR_CONSTRUCTED_SEQUENCE "\x09"      \
00112         ASN1_STR_OID "\x05"                     \
00113       OID_HASH_ALG_SHA1                         \
00114         ASN1_STR_NULL "\x00"                    \
00115       ASN1_STR_OCTET_STRING "\x14"
00116 
00117 #define ASN1_HASH_SHA2X                         \
00118     ASN1_STR_CONSTRUCTED_SEQUENCE "\x11"        \
00119       ASN1_STR_CONSTRUCTED_SEQUENCE "\x0d"      \
00120         ASN1_STR_OID "\x09"                     \
00121       OID_HASH_ALG_SHA2X                        \
00122         ASN1_STR_NULL "\x00"                    \
00123       ASN1_STR_OCTET_STRING "\x00"
00124 
00128 typedef struct
00129 {
00130     int ver;                    
00131     size_t len;                 
00133     mpi N;                      
00134     mpi E;                      
00136     mpi D;                      
00137     mpi P;                      
00138     mpi Q;                      
00139     mpi DP;                     
00140     mpi DQ;                     
00141     mpi QP;                     
00143     mpi RN;                     
00144     mpi RP;                     
00145     mpi RQ;                     
00147     int padding;                
00149     int hash_id;                
00153 }
00154 rsa_context;
00155 
00156 #ifdef __cplusplus
00157 extern "C" {
00158 #endif
00159 
00170 void rsa_init( rsa_context *ctx,
00171                int padding,
00172                int hash_id);
00173 
00188 int rsa_gen_key( rsa_context *ctx,
00189                  int (*f_rng)(void *, unsigned char *, size_t),
00190                  void *p_rng,
00191                  unsigned int nbits, int exponent );
00192 
00200 int rsa_check_pubkey( const rsa_context *ctx );
00201 
00209 int rsa_check_privkey( const rsa_context *ctx );
00210 
00227 int rsa_public( rsa_context *ctx,
00228                 const unsigned char *input,
00229                 unsigned char *output );
00230 
00243 int rsa_private( rsa_context *ctx,
00244                  const unsigned char *input,
00245                  unsigned char *output );
00246 
00263 int rsa_pkcs1_encrypt( rsa_context *ctx,
00264                        int (*f_rng)(void *, unsigned char *, size_t),
00265                        void *p_rng,
00266                        int mode, size_t ilen,
00267                        const unsigned char *input,
00268                        unsigned char *output );
00269 
00286 int rsa_pkcs1_decrypt( rsa_context *ctx,
00287                        int mode, size_t *olen,
00288                        const unsigned char *input,
00289                        unsigned char *output,
00290                        size_t output_max_len );
00291 
00316 int rsa_pkcs1_sign( rsa_context *ctx,
00317                     int (*f_rng)(void *, unsigned char *, size_t),
00318                     void *p_rng,
00319                     int mode,
00320                     int hash_id,
00321                     unsigned int hashlen,
00322                     const unsigned char *hash,
00323                     unsigned char *sig );
00324 
00347 int rsa_pkcs1_verify( rsa_context *ctx,
00348                       int mode,
00349                       int hash_id,
00350                       unsigned int hashlen,
00351                       const unsigned char *hash,
00352                       unsigned char *sig );
00353 
00359 void rsa_free( rsa_context *ctx );
00360 
00366 int rsa_self_test( int verbose );
00367 
00368 #ifdef __cplusplus
00369 }
00370 #endif
00371 
00372 #endif /* rsa.h */