RSAREF.H
上传用户:fq1688
上传日期:2015-05-01
资源大小:101k
文件大小:8k
源码类别:

加密解密

开发平台:

C/C++

  1. /* RSAREF.H - header file for RSAREF cryptographic toolkit
  2.  */
  3. /* Copyright (C) RSA Laboratories, a division of RSA Data Security,
  4.      Inc., created 1991. All rights reserved.
  5.  */
  6. #ifndef _RSAREF_H_
  7. #define _RSAREF_H_ 1
  8. #include "md2.h"
  9. #include "md5.h"
  10. #include "des.h"
  11. #ifdef __cplusplus
  12. extern "C" {
  13. #endif
  14. /* Message-digest algorithms.
  15.  */
  16. #define DA_MD2 3
  17. #define DA_MD5 5
  18. /* Encryption algorithms to be ored with digest algorithm in Seal and Open.
  19.  */
  20. #define EA_DES_CBC 1
  21. #define EA_DES_EDE2_CBC 2
  22. #define EA_DES_EDE3_CBC 3
  23. #define EA_DESX_CBC 4
  24. /* RSA key lengths.
  25.  */
  26. #define MIN_RSA_MODULUS_BITS 508
  27. #define MAX_RSA_MODULUS_BITS 1024
  28. #define MAX_RSA_MODULUS_LEN ((MAX_RSA_MODULUS_BITS + 7) / 8)
  29. #define MAX_RSA_PRIME_BITS ((MAX_RSA_MODULUS_BITS + 1) / 2)
  30. #define MAX_RSA_PRIME_LEN ((MAX_RSA_PRIME_BITS + 7) / 8)
  31. /* Maximum lengths of encoded and encrypted content, as a function of
  32.    content length len. Also, inverse functions.
  33.  */
  34. #define ENCODED_CONTENT_LEN(len) (4*(len)/3 + 3)
  35. #define ENCRYPTED_CONTENT_LEN(len) ENCODED_CONTENT_LEN ((len)+8)
  36. #define DECODED_CONTENT_LEN(len) (3*(len)/4 + 1)
  37. #define DECRYPTED_CONTENT_LEN(len) (DECODED_CONTENT_LEN (len) - 1)
  38. /* Maximum lengths of signatures, encrypted keys, encrypted
  39.    signatures, and message digests.
  40.  */
  41. #define MAX_SIGNATURE_LEN MAX_RSA_MODULUS_LEN
  42. #define MAX_PEM_SIGNATURE_LEN ENCODED_CONTENT_LEN (MAX_SIGNATURE_LEN)
  43. #define MAX_ENCRYPTED_KEY_LEN MAX_RSA_MODULUS_LEN
  44. #define MAX_PEM_ENCRYPTED_KEY_LEN ENCODED_CONTENT_LEN (MAX_ENCRYPTED_KEY_LEN)
  45. #define MAX_PEM_ENCRYPTED_SIGNATURE_LEN 
  46.   ENCRYPTED_CONTENT_LEN (MAX_SIGNATURE_LEN)
  47. #define MAX_DIGEST_LEN 16
  48. /* Maximum length of Diffie-Hellman parameters.
  49.  */
  50. #define DH_PRIME_LEN(bits) (((bits) + 7) / 8)
  51. /* Error codes.
  52.  */
  53. #define RE_CONTENT_ENCODING 0x0400
  54. #define RE_DATA 0x0401
  55. #define RE_DIGEST_ALGORITHM 0x0402
  56. #define RE_ENCODING 0x0403
  57. #define RE_KEY 0x0404
  58. #define RE_KEY_ENCODING 0x0405
  59. #define RE_LEN 0x0406
  60. #define RE_MODULUS_LEN 0x0407
  61. #define RE_NEED_RANDOM 0x0408
  62. #define RE_PRIVATE_KEY 0x0409
  63. #define RE_PUBLIC_KEY 0x040a
  64. #define RE_SIGNATURE 0x040b
  65. #define RE_SIGNATURE_ENCODING 0x040c
  66. #define RE_ENCRYPTION_ALGORITHM 0x040d
  67. /* Random structure.
  68.  */
  69. typedef struct {
  70.   unsigned int bytesNeeded;
  71.   unsigned char state[16];
  72.   unsigned int outputAvailable;
  73.   unsigned char output[16];
  74. } R_RANDOM_STRUCT;
  75. /* RSA public and private key.
  76.  */
  77. typedef struct {
  78.   unsigned int bits;                           /* length in bits of modulus */
  79.   unsigned char modulus[MAX_RSA_MODULUS_LEN];                    /* modulus */
  80.   unsigned char exponent[MAX_RSA_MODULUS_LEN];           /* public exponent */
  81. } R_RSA_PUBLIC_KEY;
  82. typedef struct {
  83.   unsigned int bits;                           /* length in bits of modulus */
  84.   unsigned char modulus[MAX_RSA_MODULUS_LEN];                    /* modulus */
  85.   unsigned char publicExponent[MAX_RSA_MODULUS_LEN];     /* public exponent */
  86.   unsigned char exponent[MAX_RSA_MODULUS_LEN];          /* private exponent */
  87.   unsigned char prime[2][MAX_RSA_PRIME_LEN];               /* prime factors */
  88.   unsigned char primeExponent[2][MAX_RSA_PRIME_LEN];   /* exponents for CRT */
  89.   unsigned char coefficient[MAX_RSA_PRIME_LEN];          /* CRT coefficient */
  90. } R_RSA_PRIVATE_KEY;
  91. /* RSA prototype key.
  92.  */
  93. typedef struct {
  94.   unsigned int bits;                           /* length in bits of modulus */
  95.   int useFermat4;                        /* public exponent (1 = F4, 0 = 3) */
  96. } R_RSA_PROTO_KEY;
  97. /* Diffie-Hellman parameters.
  98.  */
  99. typedef struct {
  100.   unsigned char *prime;                                            /* prime */
  101.   unsigned int primeLen;                                 /* length of prime */
  102.   unsigned char *generator;                                    /* generator */
  103.   unsigned int generatorLen;                         /* length of generator */
  104. } R_DH_PARAMS;
  105. typedef struct {
  106.   int digestAlgorithm;
  107.   union {
  108.     MD2_CTX md2;
  109.     MD5_CTX md5;
  110.   } context;
  111. } R_DIGEST_CTX;
  112. typedef struct {
  113.   R_DIGEST_CTX digestContext;
  114. } R_SIGNATURE_CTX;
  115. typedef struct {
  116.   int encryptionAlgorithm;
  117.   union {
  118.     DES_CBC_CTX des;
  119.     DES3_CBC_CTX des3;
  120.     DESX_CBC_CTX desx;
  121.   } cipherContext;
  122.   
  123.   unsigned char buffer[8];
  124.   unsigned int bufferLen;
  125. } R_ENVELOPE_CTX;
  126. /* Random structures.
  127.  */
  128. int R_RandomInit PROTO_LIST ((R_RANDOM_STRUCT *));
  129. int R_RandomUpdate PROTO_LIST
  130.   ((R_RANDOM_STRUCT *, unsigned char *, unsigned int));
  131. int R_GetRandomBytesNeeded PROTO_LIST ((unsigned int *, R_RANDOM_STRUCT *));
  132. void R_RandomFinal PROTO_LIST ((R_RANDOM_STRUCT *));
  133. /* Cryptographic procedures "by parts"
  134.  */
  135. int R_DigestInit PROTO_LIST ((R_DIGEST_CTX *, int));
  136. int R_DigestUpdate PROTO_LIST
  137.   ((R_DIGEST_CTX *, unsigned char *, unsigned int));
  138. int R_DigestFinal PROTO_LIST
  139.   ((R_DIGEST_CTX *, unsigned char *, unsigned int *));
  140. int R_SignInit PROTO_LIST ((R_SIGNATURE_CTX *, int));
  141. int R_SignUpdate PROTO_LIST
  142.   ((R_SIGNATURE_CTX *, unsigned char *, unsigned int));
  143. int R_SignFinal PROTO_LIST
  144.   ((R_SIGNATURE_CTX *, unsigned char *, unsigned int *, R_RSA_PRIVATE_KEY *));
  145. int R_VerifyInit PROTO_LIST ((R_SIGNATURE_CTX *, int));
  146. int R_VerifyUpdate PROTO_LIST
  147.   ((R_SIGNATURE_CTX *, unsigned char *, unsigned int));
  148. int R_VerifyFinal PROTO_LIST
  149.   ((R_SIGNATURE_CTX *, unsigned char *, unsigned int, R_RSA_PUBLIC_KEY *));
  150. int R_SealInit PROTO_LIST
  151.   ((R_ENVELOPE_CTX *, unsigned char **, unsigned int *, unsigned char [8],
  152.     unsigned int, R_RSA_PUBLIC_KEY **, int, R_RANDOM_STRUCT *));
  153. int R_SealUpdate PROTO_LIST
  154.   ((R_ENVELOPE_CTX *, unsigned char *, unsigned int *, unsigned char *,
  155.     unsigned int));
  156. int R_SealFinal PROTO_LIST
  157.   ((R_ENVELOPE_CTX *, unsigned char *, unsigned int *));
  158. int R_OpenInit PROTO_LIST
  159.   ((R_ENVELOPE_CTX *, int, unsigned char *, unsigned int, unsigned char [8],
  160.     R_RSA_PRIVATE_KEY *));
  161. int R_OpenUpdate PROTO_LIST
  162.   ((R_ENVELOPE_CTX *, unsigned char *, unsigned int *, unsigned char *,
  163.     unsigned int));
  164. int R_OpenFinal PROTO_LIST
  165.   ((R_ENVELOPE_CTX *, unsigned char *, unsigned int *));
  166. /* Cryptographic enhancements by block.
  167.  */
  168. int R_SignPEMBlock PROTO_LIST
  169.   ((unsigned char *, unsigned int *, unsigned char *, unsigned int *,
  170.     unsigned char *, unsigned int, int, int, R_RSA_PRIVATE_KEY *));
  171. int R_SignBlock PROTO_LIST 
  172.   ((unsigned char *, unsigned int *, unsigned char *, unsigned int, int,
  173.     R_RSA_PRIVATE_KEY *));
  174. int R_VerifyPEMSignature PROTO_LIST
  175.   ((unsigned char *, unsigned int *, unsigned char *, unsigned int,
  176.     unsigned char *, unsigned int, int, int, R_RSA_PUBLIC_KEY *));
  177. int R_VerifyBlockSignature PROTO_LIST
  178.   ((unsigned char *, unsigned int, unsigned char *, unsigned int, int,
  179.     R_RSA_PUBLIC_KEY *));
  180. int R_SealPEMBlock PROTO_LIST
  181.   ((unsigned char *, unsigned int *, unsigned char *, unsigned int *,
  182.     unsigned char *, unsigned int *, unsigned char [8], unsigned char *,
  183.     unsigned int, int, R_RSA_PUBLIC_KEY *, R_RSA_PRIVATE_KEY *,
  184.     R_RANDOM_STRUCT *));
  185. int R_OpenPEMBlock PROTO_LIST 
  186.   ((unsigned char *, unsigned int *, unsigned char *, unsigned int,
  187.     unsigned char *, unsigned int, unsigned char *, unsigned int,
  188.     unsigned char [8], int, R_RSA_PRIVATE_KEY *, R_RSA_PUBLIC_KEY *));
  189. int R_DigestBlock PROTO_LIST 
  190.   ((unsigned char *, unsigned int *, unsigned char *, unsigned int, int));
  191. /* Printable ASCII encoding and decoding.
  192.  */
  193. int R_EncodePEMBlock PROTO_LIST
  194.   ((unsigned char *, unsigned int *, unsigned char *, unsigned int));
  195. int R_DecodePEMBlock PROTO_LIST
  196.   ((unsigned char *, unsigned int *, unsigned char *, unsigned int));
  197.   
  198. /* Key-pair generation.
  199.  */
  200. int R_GeneratePEMKeys PROTO_LIST
  201.   ((R_RSA_PUBLIC_KEY *, R_RSA_PRIVATE_KEY *, R_RSA_PROTO_KEY *,
  202.     R_RANDOM_STRUCT *));
  203. /* Diffie-Hellman key agreement.
  204.  */
  205. int R_GenerateDHParams PROTO_LIST
  206.   ((R_DH_PARAMS *, unsigned int, unsigned int, R_RANDOM_STRUCT *));
  207. int R_SetupDHAgreement PROTO_LIST
  208.   ((unsigned char *, unsigned char *, unsigned int, R_DH_PARAMS *,
  209.     R_RANDOM_STRUCT *));
  210. int R_ComputeDHAgreedKey PROTO_LIST
  211.   ((unsigned char *, unsigned char *, unsigned char *, unsigned int,
  212.     R_DH_PARAMS *));
  213. /* Routines supplied by the implementor.
  214.  */
  215. void R_memset PROTO_LIST ((POINTER, int, unsigned int));
  216. void R_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
  217. int R_memcmp PROTO_LIST ((POINTER, POINTER, unsigned int));
  218. #ifdef __cplusplus
  219. }
  220. #endif
  221. #endif