PolarSSL
polarssl/rsa.h
Go to the documentation of this file.
00001 
00025 #ifndef POLARSSL_RSA_H
00026 #define POLARSSL_RSA_H
00027 
00028 #include "polarssl/bignum.h"
00029 
00030 /*
00031  * RSA Error codes
00032  */
00033 #define POLARSSL_ERR_RSA_BAD_INPUT_DATA                    -0x0400
00034 #define POLARSSL_ERR_RSA_INVALID_PADDING                   -0x0410
00035 #define POLARSSL_ERR_RSA_KEY_GEN_FAILED                    -0x0420
00036 #define POLARSSL_ERR_RSA_KEY_CHECK_FAILED                  -0x0430
00037 #define POLARSSL_ERR_RSA_PUBLIC_FAILED                     -0x0440
00038 #define POLARSSL_ERR_RSA_PRIVATE_FAILED                    -0x0450
00039 #define POLARSSL_ERR_RSA_VERIFY_FAILED                     -0x0460
00040 #define POLARSSL_ERR_RSA_OUTPUT_TOO_LARGE                  -0x0470
00041 #define POLARSSL_ERR_RSA_RNG_FAILED                        -0x0480
00042 
00043 /*
00044  * PKCS#1 constants
00045  */
00046 #define SIG_RSA_RAW     0
00047 #define SIG_RSA_MD2     2
00048 #define SIG_RSA_MD4     3
00049 #define SIG_RSA_MD5     4
00050 #define SIG_RSA_SHA1    5
00051 #define SIG_RSA_SHA224  14
00052 #define SIG_RSA_SHA256  11
00053 #define SIG_RSA_SHA384  12
00054 #define SIG_RSA_SHA512  13
00055 
00056 #define RSA_PUBLIC      0
00057 #define RSA_PRIVATE     1
00058 
00059 #define RSA_PKCS_V15    0
00060 #define RSA_PKCS_V21    1
00061 
00062 #define RSA_SIGN        1
00063 #define RSA_CRYPT       2
00064 
00065 #define ASN1_STR_CONSTRUCTED_SEQUENCE   "\x30"
00066 #define ASN1_STR_NULL                           "\x05"
00067 #define ASN1_STR_OID                            "\x06"
00068 #define ASN1_STR_OCTET_STRING               "\x04"
00069 
00070 #define OID_DIGEST_ALG_MDX              "\x2A\x86\x48\x86\xF7\x0D\x02\x00"
00071 #define OID_HASH_ALG_SHA1               "\x2b\x0e\x03\x02\x1a"
00072 #define OID_HASH_ALG_SHA2X              "\x60\x86\x48\x01\x65\x03\x04\x02\x00"
00073 
00074 #define OID_ISO_MEMBER_BODIES       "\x2a"
00075 #define OID_ISO_IDENTIFIED_ORG      "\x2b"
00076 
00077 /*
00078  * ISO Member bodies OID parts
00079  */
00080 #define OID_COUNTRY_US                  "\x86\x48"
00081 #define OID_RSA_DATA_SECURITY       "\x86\xf7\x0d"
00082 
00083 /*
00084  * ISO Identified organization OID parts
00085  */
00086 #define OID_OIW_SECSIG_SHA1             "\x0e\x03\x02\x1a"
00087 
00088 /*
00089  * DigestInfo ::= SEQUENCE {
00090  *   digestAlgorithm DigestAlgorithmIdentifier,
00091  *   digest Digest }
00092  *
00093  * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
00094  *
00095  * Digest ::= OCTET STRING
00096  */
00097 #define ASN1_HASH_MDX                                           \
00098 (                                                                           \
00099     ASN1_STR_CONSTRUCTED_SEQUENCE "\x20"                \
00100       ASN1_STR_CONSTRUCTED_SEQUENCE "\x0C"              \
00101         ASN1_STR_OID "\x08"                                     \
00102           OID_DIGEST_ALG_MDX                                    \
00103         ASN1_STR_NULL "\x00"                                    \
00104       ASN1_STR_OCTET_STRING "\x10"                          \
00105 )
00106 
00107 #define ASN1_HASH_SHA1                                          \
00108     ASN1_STR_CONSTRUCTED_SEQUENCE "\x21"                \
00109       ASN1_STR_CONSTRUCTED_SEQUENCE "\x09"              \
00110         ASN1_STR_OID "\x05"                                     \
00111           OID_HASH_ALG_SHA1                                         \
00112         ASN1_STR_NULL "\x00"                                \
00113       ASN1_STR_OCTET_STRING "\x14"
00114 
00115 #define ASN1_HASH_SHA2X                                         \
00116     ASN1_STR_CONSTRUCTED_SEQUENCE "\x11"                \
00117       ASN1_STR_CONSTRUCTED_SEQUENCE "\x0d"              \
00118         ASN1_STR_OID "\x09"                                     \
00119           OID_HASH_ALG_SHA2X                                    \
00120         ASN1_STR_NULL "\x00"                                \
00121       ASN1_STR_OCTET_STRING "\x00"
00122 
00126 typedef struct
00127 {
00128     int ver;                    
00129     int len;                    
00131     mpi N;                      
00132     mpi E;                      
00134     mpi D;                      
00135     mpi P;                      
00136     mpi Q;                      
00137     mpi DP;                     
00138     mpi DQ;                     
00139     mpi QP;                     
00141     mpi RN;                     
00142     mpi RP;                     
00143     mpi RQ;                     
00145     int padding;                
00146     int hash_id;                
00147 }
00148 rsa_context;
00149 
00150 #ifdef __cplusplus
00151 extern "C" {
00152 #endif
00153 
00167 void rsa_init( rsa_context *ctx,
00168                int padding,
00169                int hash_id);
00170 
00185 int rsa_gen_key( rsa_context *ctx,
00186                  int (*f_rng)(void *),
00187                  void *p_rng,
00188                  int nbits, int exponent );
00189 
00197 int rsa_check_pubkey( const rsa_context *ctx );
00198 
00206 int rsa_check_privkey( const rsa_context *ctx );
00207 
00224 int rsa_public( rsa_context *ctx,
00225                 const unsigned char *input,
00226                 unsigned char *output );
00227 
00240 int rsa_private( rsa_context *ctx,
00241                  const unsigned char *input,
00242                  unsigned char *output );
00243 
00260 int rsa_pkcs1_encrypt( rsa_context *ctx,
00261                        int (*f_rng)(void *),
00262                        void *p_rng,
00263                        int mode, int  ilen,
00264                        const unsigned char *input,
00265                        unsigned char *output );
00266 
00283 int rsa_pkcs1_decrypt( rsa_context *ctx,
00284                        int mode, int *olen,
00285                        const unsigned char *input,
00286                        unsigned char *output,
00287                                int output_max_len );
00288 
00305 int rsa_pkcs1_sign( rsa_context *ctx,
00306                     int mode,
00307                     int hash_id,
00308                     int hashlen,
00309                     const unsigned char *hash,
00310                     unsigned char *sig );
00311 
00328 int rsa_pkcs1_verify( rsa_context *ctx,
00329                       int mode,
00330                       int hash_id,
00331                       int hashlen,
00332                       const unsigned char *hash,
00333                       unsigned char *sig );
00334 
00340 void rsa_free( rsa_context *ctx );
00341 
00347 int rsa_self_test( int verbose );
00348 
00349 #ifdef __cplusplus
00350 }
00351 #endif
00352 
00353 #endif /* rsa.h */
 All Classes Files Functions Variables Defines