bbarsa.h 3.88 KB
/*
 * bdtlsRsa.h
 * 代码源自轻量级RSA开源库:RSAEURO
 * 支持CER格式的RSA公钥和私钥
 *
 * Created on: 2019/4/26
 * Author: machao10
 */

#pragma once
/* PROTOTYPES should be set to one if and only if the compiler supports
         function argument prototyping.
     The following makes PROTOTYPES default to 1 if it has not already been
         defined as 0 with C compiler flags. */
#ifndef PROTOTYPES
#define PROTOTYPES 1
#endif

/* PROTO_LIST is defined depending on how PROTOTYPES is defined above.
     If using PROTOTYPES, then PROTO_LIST returns the list, otherwise it
     returns an empty list. */

#if PROTOTYPES
#define PROTO_LIST(list) list
#else
#define PROTO_LIST(list) ()
#endif

/* POINTER defines a generic pointer type */
typedef unsigned char *POINTER;

/* UINT2 defines a two byte word */
typedef unsigned short int UINT2;

/* UINT4 defines a four byte word */
typedef unsigned int UINT4;

typedef unsigned char BYTE;

/* BYTE defines a unsigned character */
// typedef unsigned char BYTE;

/* Internal Error Codes */

/* IDOK and IDERROR changed to ID_OK and ID_ERROR */

#define ID_OK    0
#define ID_ERROR 1

/* Internal defs. */

#define TRUE    1
#define FALSE   0

/* Error codes. */

#define RE_CONTENT_ENCODING 0x0400
#define RE_DATA 0x0401
#define RE_DIGEST_ALGORITHM 0x0402
#define RE_ENCODING 0x0403
#define RE_KEY 0x0404
#define RE_KEY_ENCODING 0x0405
#define RE_LEN 0x0406
#define RE_MODULUS_LEN 0x0407
#define RE_NEED_RANDOM 0x0408
#define RE_PRIVATE_KEY 0x0409
#define RE_PUBLIC_KEY 0x040a
#define RE_SIGNATURE 0x040b
#define RE_SIGNATURE_ENCODING 0x040c
#define RE_ENCRYPTION_ALGORITHM 0x040d
#define RE_FILE 0x040e

/*
     PGP 2.6.2 Now allows 2048-bit keys changing below will allow this.
     It does lengthen key generation slightly if the value is increased.
*/
#define MAX_RSA_MODULUS_BITS 1024
#define MAX_RSA_MODULUS_LEN ((MAX_RSA_MODULUS_BITS + 7) / 8)
#define MAX_RSA_PRIME_BITS ((MAX_RSA_MODULUS_BITS + 1) / 2)
#define MAX_RSA_PRIME_LEN ((MAX_RSA_PRIME_BITS + 7) / 8)

/* Random structure. */

typedef struct {
    unsigned int bytesNeeded;                    /* seed bytes required */
    unsigned char state[16];                     /* state of object */
    unsigned int outputAvailable;                /* number byte available */
    unsigned char output[16];                    /* output bytes */
} R_RANDOM_STRUCT;

/* RSA public and private key. */

typedef struct {
    unsigned short int bits;                     /* length in bits of modulus */
    unsigned char modulus[MAX_RSA_MODULUS_LEN];  /* modulus */
    unsigned char exponent[MAX_RSA_MODULUS_LEN]; /* public exponent */
} R_RSA_PUBLIC_KEY;

typedef struct {
    unsigned short int bits;                     /* length in bits of modulus */
    unsigned char modulus[MAX_RSA_MODULUS_LEN];  /* modulus */
    unsigned char publicExponent[MAX_RSA_MODULUS_LEN];     /* public exponent */
    unsigned char exponent[MAX_RSA_MODULUS_LEN]; /* private exponent */
    unsigned char prime[2][MAX_RSA_PRIME_LEN];   /* prime factors */
    unsigned char primeExponent[2][MAX_RSA_PRIME_LEN];     /* exponents for CRT */
    unsigned char coefficient[MAX_RSA_PRIME_LEN];          /* CRT coefficient */
} R_RSA_PRIVATE_KEY;


/* Printable ASCII encoding and decoding. */

int R_DecodePEMBlock PROTO_LIST ((unsigned char *, unsigned int *,
    unsigned char *, unsigned int));

/* Key-pair generation. */


int getRsaPublicKeyFromPem(unsigned char* pemPublicKey,
        R_RSA_PUBLIC_KEY *rsaPublicKey);

void R_RandomCreate(R_RANDOM_STRUCT *random);

int RSAPublicEncrypt PROTO_LIST ((unsigned char *, unsigned int *, unsigned char *, unsigned int,
    R_RSA_PUBLIC_KEY *, R_RANDOM_STRUCT *));
int RSAPrivateDecrypt PROTO_LIST ((unsigned char *, unsigned int *, unsigned char *, unsigned int,
    R_RSA_PRIVATE_KEY *));


#define R_memset(x, y, z) memset(x, y, z)
#define R_memcpy(x, y, z) memcpy(x, y, z)
#define R_memcmp(x, y, z) memcmp(x, y, z)