00001 
00004 #ifndef XYSSL_RSA_H
00005 #define XYSSL_RSA_H
00006 
00007 #include "bignum.h"
00008 
00009 #define XYSSL_ERR_RSA_BAD_INPUT_DATA                    -0x0400
00010 #define XYSSL_ERR_RSA_INVALID_PADDING                   -0x0410
00011 #define XYSSL_ERR_RSA_KEY_GEN_FAILED                    -0x0420
00012 #define XYSSL_ERR_RSA_KEY_CHECK_FAILED                  -0x0430
00013 #define XYSSL_ERR_RSA_PUBLIC_FAILED                     -0x0440
00014 #define XYSSL_ERR_RSA_PRIVATE_FAILED                    -0x0450
00015 #define XYSSL_ERR_RSA_VERIFY_FAILED                     -0x0460
00016 
00017 
00018 
00019 
00020 #define RSA_RAW         0
00021 #define RSA_MD2         2
00022 #define RSA_MD4         3
00023 #define RSA_MD5         4
00024 #define RSA_SHA1        5
00025 #define RSA_SHA256      6
00026 
00027 #define RSA_PUBLIC      0
00028 #define RSA_PRIVATE     1
00029 
00030 #define RSA_PKCS_V15    0
00031 #define RSA_PKCS_V21    1
00032 
00033 #define RSA_SIGN        1
00034 #define RSA_CRYPT       2
00035 
00036 
00037 
00038 
00039 
00040 
00041 
00042 
00043 
00044 
00045 #define ASN1_HASH_MDX                       \
00046     "\x30\x20\x30\x0C\x06\x08\x2A\x86\x48"  \
00047     "\x86\xF7\x0D\x02\x00\x05\x00\x04\x10"
00048 
00049 #define ASN1_HASH_SHA1                      \
00050     "\x30\x21\x30\x09\x06\x05\x2B\x0E\x03"  \
00051     "\x02\x1A\x05\x00\x04\x14"
00052 
00056 typedef struct
00057 {
00058     int ver;                    
00059     int len;                    
00061     mpi N;                      
00062     mpi E;                      
00064     mpi D;                      
00065     mpi P;                      
00066     mpi Q;                      
00067     mpi DP;                     
00068     mpi DQ;                     
00069     mpi QP;                     
00071     mpi RN;                     
00072     mpi RP;                     
00073     mpi RQ;                     
00075     int padding;                
00076     int hash_id;                
00077     int (*f_rng)(void *);       
00078     void *p_rng;                
00079 }
00080 rsa_context;
00081 
00082 #ifdef __cplusplus
00083 extern "C" {
00084 #endif
00085 
00101 void rsa_init( rsa_context *ctx,
00102                int padding,
00103                int hash_id,
00104                int (*f_rng)(void *),
00105                void *p_rng );
00106 
00119 int rsa_gen_key( rsa_context *ctx, int nbits, int exponent );
00120 
00128 int rsa_check_pubkey( rsa_context *ctx );
00129 
00137 int rsa_check_privkey( rsa_context *ctx );
00138 
00154 int rsa_public( rsa_context *ctx,
00155                 unsigned char *input,
00156                 unsigned char *output );
00157 
00170 int rsa_private( rsa_context *ctx,
00171                  unsigned char *input,
00172                  unsigned char *output );
00173 
00188 int rsa_pkcs1_encrypt( rsa_context *ctx,
00189                        int mode, int  ilen,
00190                        unsigned char *input,
00191                        unsigned char *output );
00192 
00207 int rsa_pkcs1_decrypt( rsa_context *ctx,
00208                        int mode, int *olen,
00209                        unsigned char *input,
00210                        unsigned char *output );
00211 
00228 int rsa_pkcs1_sign( rsa_context *ctx,
00229                     int mode,
00230                     int hash_id,
00231                     int hashlen,
00232                     unsigned char *hash,
00233                     unsigned char *sig );
00234 
00251 int rsa_pkcs1_verify( rsa_context *ctx,
00252                       int mode,
00253                       int hash_id,
00254                       int hashlen,
00255                       unsigned char *hash,
00256                       unsigned char *sig );
00257 
00261 void rsa_free( rsa_context *ctx );
00262 
00268 int rsa_self_test( int verbose );
00269 
00270 #ifdef __cplusplus
00271 }
00272 #endif
00273 
00274 #endif