cryptlib  3.4.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros
consts.h
Go to the documentation of this file.
1 /****************************************************************************
2 * *
3 * cryptlib Data Size and Crypto-related Constants *
4 * Copyright Peter Gutmann 1992-2010 *
5 * *
6 ****************************************************************************/
7 
8 #ifndef _CONSTS_DEFINED
9 
10 #define _CONSTS_DEFINED
11 
12 /* The maximum length that can be safely handled using an integer. We don't
13  quite allow the maximum possible length since most data/message formats
14  impose some extra overhead themselves.
15 
16  In addition to the maximum-possible length we also define a shorter
17  length defined as a generally sensible upper bound for values that
18  shouldn't require arbitrary-length data quantities */
19 
20 #ifdef SYSTEM_16BIT
21  #define MAX_INTLENGTH_DELTA 8192
22 #else
23  #define MAX_INTLENGTH_DELTA 1048576
24 #endif /* 16- vs. 32/64-bit systems */
25 #define MAX_INTLENGTH ( INT_MAX - MAX_INTLENGTH_DELTA )
26 #define MAX_INTLENGTH_SHORT 16384
27 
28 /* The size of a cryptlib key ID, an SHA-1 hash of the SubjectPublicKeyInfo,
29  and the PGP key ID */
30 
31 #define KEYID_SIZE 20
32 #define PGP_KEYID_SIZE 8
33 
34 /* The minimum and maximum private key data size. This is used when
35  buffering the encrypted private key from a keyset during decryption and
36  is equal to the overall size of the total number of possible PKC
37  parameters in an encryption context, plus a little extra for encoding and
38  encryption */
39 
40 #define MIN_PRIVATE_KEYSIZE 18 /* For DLP keys */
41 #define MAX_PRIVATE_KEYSIZE ( ( CRYPT_MAX_PKCSIZE * 8 ) + 256 )
42 
43 /* The minimum and maximum working conventional key size in bits. In order
44  to avoid problems with space inside PKC-encrypted blocks when MIN_PKCSIZE
45  is less than 1024 bits, we limit the total keysize to 256 bits, which is
46  adequate for all purposes - the limiting factor is AES-256.
47  Unfortunately when loading a default-length key into a context we can't
48  tell what the user is going to do with the generated key (for example
49  whether they'll export it using a very short public key) so we have to
50  take the approach of using a practical working key length that will work
51  even with a short public key. This means that for Blowfish, RC2, RC4,
52  and RC5 the working keylength is shorter than strictly necessary
53  (actually for RC2 we have to limit the keysize to 128 bits for CMS/SMIME
54  compatibility) */
55 
56 #define MIN_KEYSIZE bitsToBytes( 64 )
57 #define MAX_WORKING_KEYSIZE bitsToBytes( 256 )
58 
59 /* The minimum public key size (c.f. CRYPT_MAX_PKCSIZE). This is a bit less
60  than the actual size because keygen specifics can lead to keys that are
61  slightly shorter than the nominal size. In addition we have to have a
62  special value for ECC keys, for which key sizes work differently that
63  conventional PKCs */
64 
65 #define MIN_PKCSIZE ( bitsToBytes( 1024 ) - 1 )
66 #define MIN_PKCSIZE_ECC ( bitsToBytes( 192 ) - 1 )
67 
68 /* When we read a public key, a value that's too short to be even vaguely
69  sensible is reported as CRYPT_ERROR_BADDATA, but it's it's at least
70  vaguely sensible but too short to be secure it's reported as
71  CRYPT_ERROR_NOSECURE. The following value defines the cutoff point
72  between "obviously invalid" and "theoretically valid but not secure",
73  so that 0...MIN_PKCSIZE_THRESHOLD - 1 is rejected with
74  CRYPT_ERROR_BADDATA, MIN_PKCSIZE_THRESHOLD... MIN_PKCSIZE - 1 is rejected
75  with CRYPT_ERROR_NOSECURE, and MIN_PKCSIZE...CRYPT_MAX_PKCSIZE is
76  accepted */
77 
78 #define MIN_PKCSIZE_THRESHOLD ( bitsToBytes( 504 ) )
79 #define MIN_PKCSIZE_ECC_THRESHOLD ( bitsToBytes( 120 ) )
80 
81 /* ECC points present special problems of their own since they're encoded
82  by stuffing them into byte strings with a type indicator at the start
83  which leads to a length that bears no relation to the actual key size */
84 
85 #define MIN_PKCSIZE_ECCPOINT ( 1 + ( MIN_PKCSIZE_ECC * 2 ) )
86 #define MIN_PKCSIZE_ECCPOINT_THRESHOLD \
87  ( 1 + ( MIN_PKCSIZE_ECC_THRESHOLD * 2 ) )
88 #define MAX_PKCSIZE_ECCPOINT ( 1 + ( CRYPT_MAX_PKCSIZE_ECC * 2 ) )
89 
90 /* The size of the largest public-key wrapped value, corresponding to an
91  ASN.1-encoded Elgamal-encrypted key. If we're not using Elgamal it's
92  the same as CRYPT_MAX_PKCSIZE */
93 
94 #ifdef USE_ELGAMAL
95  #define MAX_PKCENCRYPTED_SIZE ( 16 + ( CRYPT_MAX_PKCSIZE * 2 ) )
96 #else
97  #define MAX_PKCENCRYPTED_SIZE CRYPT_MAX_PKCSIZE
98 #endif /* USE_ELGAMAL */
99 
100 /* The maximum public-key object size. This is used to allocate temporary
101  buffers when working with signatures and PKC-encrypted keys. The size
102  estimate is somewhat crude and involves a fair safety margin, it usually
103  contains a single PKC object (signature or encrypted key) along with
104  algorithm and key ID information */
105 
106 #define MAX_PKC_OBJECTSIZE ( CRYPT_MAX_PKCSIZE * 2 )
107 
108 /* The minimum size of an encoded signature or exported key object. This is
109  used by the pointer-check macros (for the OSes that support this) to
110  check that the pointers to objects that are passed to functions point to
111  the minimal amount of valid memory required for an object, and also to
112  zero the buffer for the object to ensure that the caller gets invalid
113  data if the function fails */
114 
115 #define MIN_CRYPT_OBJECTSIZE 64
116 
117 /* The minimum size of a certificate. This is used by the pointer-check
118  macros (for the OSes that support this) to check that the pointers being
119  passed to these functions point to the minimal amount of valid memory
120  required for an object */
121 
122 #define MIN_CERTSIZE 256
123 
124 /* The maximum size of an object attribute. In theory this can be any size,
125  but in practice we limit it to the following maximum to stop people
126  creating things like certs containing MPEGs of themselves playing with
127  their cat */
128 
129 #define MAX_ATTRIBUTE_SIZE 1024
130 
131 /* Some objects contain internal buffers used to process data whose size can
132  be specified by the user, the following is the minimum size allowed for
133  these buffers */
134 
135 #define MIN_BUFFER_SIZE 8192
136 
137 /* The minimum allowed length for object names (keysets, devices, users,
138  etc). In theory this could be a single character, but by default we
139  make it 2 chars to make things more resistant to off-by-one errors in
140  lengths, particularly since it applies to external objects outside
141  cryptlib's control */
142 
143 #ifdef UNICODE_CHARS
144  #define MIN_NAME_LENGTH ( 2 * sizeof( wchar_t ) )
145 #else
146  #define MIN_NAME_LENGTH 2
147 #endif /* Unicode vs. ASCII environments */
148 
149 /* The minimum time value that's regarded as being a valid time (we have to
150  allow dates slightly before the current time because of things like
151  backdated cert revocations, as a rule of thumb we allow a date up to two
152  years in the past), an approximation of the current time (with the
153  constraint that it's not after the current date), and a somewhat more
154  relaxed minimum time value used when reading stored data, which can
155  contain keys that have been hanging around for years */
156 
157 #define MIN_TIME_VALUE ( ( 2007 - 1970 ) * 365 * 86400L )
158 #define CURRENT_TIME_VALUE ( ( 2009 - 1970 ) * 365 * 86400L )
159 #define MIN_STORED_TIME_VALUE ( ( 1995 - 1970 ) * 365 * 86400L )
160 
161 /* The minimum and maximum network port numbers. Note that we allow ports
162  down to 21 (= FTP) rather than the more obvious 22 (= SSH) provided by
163  cryptlib sessions because the URL-handling code is also used for general-
164  purpose URI parsing for which the lowest-numbered one that we'd normally
165  run into is FTP. We set the upper bound at the end of the non-ephemeral
166  port range, 49152-65535 is for ephemeral ports that are only valid for
167  the duration of a TCP session */
168 
169 #define MIN_PORT_NUMBER 21
170 #define MAX_PORT_NUMBER 49151L
171 
172 /* Some object types interact with exteral services that can return detailed
173  error messages when problems occur, the following is the maximum length
174  error string that we store. Anything beyond this size is truncated */
175 
176 #define MAX_ERRMSG_SIZE 512
177 
178 /* The maximum number of iterations that we allow for an iterated key setup
179  such as a hashed password. This is used to prevent DoS attacks from data
180  containing excessive iteration counts */
181 
182 #define MAX_KEYSETUP_ITERATIONS 20000
183 
184 /* The maximum certificate compliance level */
185 
186 #if defined( USE_CERTLEVEL_PKIX_FULL )
187  #define MAX_COMPLIANCE_LEVEL CRYPT_COMPLIANCELEVEL_PKIX_FULL
188 #elif defined( USE_CERTLEVEL_PKIX_PARTIAL )
189  #define MAX_COMPLIANCE_LEVEL CRYPT_COMPLIANCELEVEL_PKIX_PARTIAL
190 #else
191  #define MAX_COMPLIANCE_LEVEL CRYPT_COMPLIANCELEVEL_STANDARD
192 #endif /* Maximum compliance level */
193 
194 /* All non-constant loops contain a guard to prevent excessive looping.
195  This is a generalisation of the programming practice that when looping on
196  externally-supplied data, we should perform a sanity check on loop
197  iterations to prevent DoS attacks due to malformed data. The exception
198  to the guard-condition requirement is pointer-chasing loops, for which
199  (if the pointers become corrupted so the loop doesn't terminate as it
200  should) we'd segfault long before the guard would ever be reached.
201 
202  Apart from the data safety checking, this checking catches two things:
203 
204  1. Running off the end of a lookup table.
205 
206  2. A loop that uses a function as its terminating condition and doesn't
207  exit at the expected point:
208 
209  do {
210  term = foo();
211  }
212  while( !term );
213 
214  for( ptr = listHead; ptr != NULL; ptr = getNextValue( ptr ) );
215 
216  The following bounds on loop iterations apply:
217 
218  FAILSAFE_SMALL: Expect 1 but can have a few more.
219  FAILSAFE_MED: Expect 10-20 but can have a few more.
220  FAILSAFE_LARGE: Expect many, but not too many.
221 
222  In addition to these values there's a special value FAILSAFE_MAX which
223  is equivalent to the ASN.1 (1...MAX) construct in setting an upper bound
224  on loop iterations without necessarily setting any specific limit:
225 
226  FAILSAFE_MAX: A value that's unlikely to be reached during normal
227  operation, but that also won't result in an excessive
228  stall if it's exceeded */
229 
230 #define FAILSAFE_ITERATIONS_SMALL 10
231 #define FAILSAFE_ITERATIONS_MED 50
232 #define FAILSAFE_ITERATIONS_LARGE 1000
233 #ifdef SYSTEM_16BIT
234  #define FAILSAFE_ITERATIONS_MAX 10000
235 #else
236  #define FAILSAFE_ITERATIONS_MAX 100000
237 #endif /* 16-bit vs 32/64-bit systems */
238 
239 /* Pseudo-constants used for array bounds-checking. These provide a more
240  precise limit than the FAILSAFE_ITERATIONS_xxx values above. We subtract
241  one from the total count because static arrays are always overallocated
242  with two extra dummy elements at the end */
243 
244 #define FAILSAFE_ARRAYSIZE( array, elementType ) \
245  ( ( sizeof( array ) / sizeof( elementType ) ) - 1 )
246 
247 /* The minimum and maximum size of various Internet-related values, used for
248  range checking */
249 
250 #define MIN_DNS_SIZE 4 /* x.com */
251 #define MAX_DNS_SIZE 255 /* Max hostname size */
252 #define MIN_RFC822_SIZE 7 /* [email protected] */
253 #define MAX_RFC822_SIZE 255
254 #define MIN_URL_SIZE 12 /* http://x.com */
255 #define MAX_URL_SIZE MAX_DNS_SIZE
256 
257 /* The HMAC input and output padding values. These are defined here rather
258  than in context.h because they're needed by some routines that perform
259  HMAC operations using raw SHA-1 contexts, since some devices provide SHA-1
260  but not HMAC-SHA1 so we have to build it ourselves where it's needed for
261  things like key hashing */
262 
263 #define HMAC_IPAD 0x36
264 #define HMAC_OPAD 0x5C
265 
266 /* Generic error return code/invalid value code */
267 
268 #define CRYPT_ERROR -1
269 
270 /* Sometimes compilers get confused about whether a variable has been
271  initialised or not and report a used-before-initialised error when there
272  isn't one. This happens most frequently when the variable is initialised
273  as part of a conditional expression where the developer knows the control
274  flow will result in an initialisation but the compiler doesn't. To get
275  around this we perform a dummy initialisation of the variable with a
276  symbolic value to get rid of the false positive */
277 
278 #define DUMMY_INIT 0
279 #define DUMMY_INIT_PTR NULL
280 #define DUMMY_INIT_STRUCT { 0 }
281 
282 /* A special return code to indicate that everything went OK but there's
283  some special action to perform. This is generally used when a lower-level
284  routine wants to return a CRYPT_OK with some condition attached, typically
285  that the calling routine not update state information since it's already
286  been done by the returning routine or because the returning routine has
287  more work to do on a later call. The parentheses are to catch potential
288  erroneous use in an expression */
289 
290 #define OK_SPECIAL ( -4321 )
291 
292 /* When parameters get passed in messages, their mapping to parameters passed
293  to the calling function gets lost. The following error codes are used to
294  denote errors in message parameters that are mapped to function parameter
295  error codes by the caller. For a message call:
296 
297  krnlSendMessage( object, {args}, MESSAGE_TYPE, value );
298 
299  we have the following possible error codes. The parentheses are to catch
300  potential erroneous use in an expression */
301 
302 #define CRYPT_ARGERROR_OBJECT ( -1000 ) /* Error in object being sent msg.*/
303 #define CRYPT_ARGERROR_VALUE ( -1001 ) /* Error in message value */
304 #define CRYPT_ARGERROR_STR1 ( -1002 ) /* Error in first string arg */
305 #define CRYPT_ARGERROR_STR2 ( -1003 ) /* Error in second string arg */
306 #define CRYPT_ARGERROR_NUM1 ( -1004 ) /* Error in first numeric arg */
307 #define CRYPT_ARGERROR_NUM2 ( -1005 ) /* Error in second numeric arg */
308 
309 #define cryptArgError( status ) \
310  ( ( status ) >= CRYPT_ARGERROR_NUM2 && ( status ) <= CRYPT_ARGERROR_OBJECT )
311 #define cryptStandardError( status ) \
312  ( ( status ) >= CRYPT_ENVELOPE_RESOURCE && ( status ) <= CRYPT_OK )
313 
314 /* The data formats for reading/writing public keys */
315 
316 typedef enum {
317  KEYFORMAT_NONE, /* No key format */
318  KEYFORMAT_CERT, /* X.509 SubjectPublicKeyInfo */
319 /* KEYFORMAT_PUBLIC, // PKCS #15 public key - currently unused */
320  KEYFORMAT_SSH, /* SSHv2 public key */
321  KEYFORMAT_SSH1, /* SSHv1 public key */
322  KEYFORMAT_SSL, /* SSL public key */
323  KEYFORMAT_PGP, /* PGP public key */
324  KEYFORMAT_PRIVATE, /* Private key */
325  KEYFORMAT_PRIVATE_OLD, /* Older format for backwards-compatibility */
326  KEYFORMAT_LAST /* Last possible key format type */
327  } KEYFORMAT_TYPE;
328 
329 /* The different types of actions that can be signalled to the management
330  function for each object class. This instructs the management function
331  to initialise or shut down any object-class-specific information that it
332  may maintain */
333 
334 typedef enum {
335  MANAGEMENT_ACTION_NONE, /* No management action */
336  MANAGEMENT_ACTION_PRE_INIT, /* Pre-initialisation */
337  MANAGEMENT_ACTION_INIT, /* Initialisation */
338  MANAGEMENT_ACTION_PRE_SHUTDOWN, /* Pre-shutdown */
340  MANAGEMENT_ACTION_LAST /* Last possible management action */
342 
343 /* Certificate key usage types. SIGN is for data signing and CA is for
344  certificate signing. we don't include CRYPT_KEYUSAGE_DATAENCIPHERMENT in
345  KEYUSAGE_CRYPT since this is more or less never what's actually meant */
346 
347 #define KEYUSAGE_SIGN ( CRYPT_KEYUSAGE_DIGITALSIGNATURE | \
348  CRYPT_KEYUSAGE_NONREPUDIATION )
349 #define KEYUSAGE_CA ( CRYPT_KEYUSAGE_KEYCERTSIGN | \
350  CRYPT_KEYUSAGE_CRLSIGN )
351 #define KEYUSAGE_CRYPT ( CRYPT_KEYUSAGE_KEYENCIPHERMENT )
352 #define KEYUSAGE_KEYAGREEMENT ( CRYPT_KEYUSAGE_KEYAGREEMENT | \
353  CRYPT_KEYUSAGE_ENCIPHERONLY | \
354  CRYPT_KEYUSAGE_DECIPHERONLY )
355 
356 #endif /* _CONSTS_DEFINED */