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