cryptlib  3.4.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros
misc_rw.h
Go to the documentation of this file.
1 /****************************************************************************
2 * *
3 * Miscellaneous (Non-ASN.1) Routines Header File *
4 * Copyright Peter Gutmann 1992-2004 *
5 * *
6 ****************************************************************************/
7 
8 #ifndef _MISCRW_DEFINED
9 
10 #define _MISCRW_DEFINED
11 
12 #include <time.h>
13 #if defined( INC_ALL )
14  #include "stream.h"
15 #else
16  #include "io/stream.h"
17 #endif /* Compiler-specific includes */
18 
19 /****************************************************************************
20 * *
21 * Constants and Macros *
22 * *
23 ****************************************************************************/
24 
25 /* Sizes of encoded integer values */
26 
27 #define UINT16_SIZE 2
28 #define UINT32_SIZE 4
29 #define UINT64_SIZE 8
30 
31 /****************************************************************************
32 * *
33 * Function Prototypes *
34 * *
35 ****************************************************************************/
36 
37 /* Read and write 16-, 32-, and 64-bit integer values */
38 
39 RETVAL_RANGE( MAX_ERROR, 0xFFFF ) STDC_NONNULL_ARG( ( 1 ) ) \
40 int readUint16( INOUT STREAM *stream );
41 RETVAL STDC_NONNULL_ARG( ( 1 ) ) \
42 int writeUint16( INOUT STREAM *stream, IN_RANGE( 0, 0xFFFF ) const int value );
44 int readUint32( INOUT STREAM *stream );
45 RETVAL STDC_NONNULL_ARG( ( 1 ) ) \
46 int writeUint32( INOUT STREAM *stream, IN_INT_Z const long value );
47 RETVAL STDC_NONNULL_ARG( ( 1, 2 ) ) \
48 int readUint64( INOUT STREAM *stream, OUT_INT_Z long *value );
49 RETVAL STDC_NONNULL_ARG( ( 1 ) ) \
50 int writeUint64( INOUT STREAM *stream, IN_INT_Z const long value );
51 
52 /* Read and write 32- and 64-bit time values */
53 
54 RETVAL STDC_NONNULL_ARG( ( 1, 2 ) ) \
55 int readUint32Time( INOUT STREAM *stream, OUT time_t *timeVal );
56 RETVAL STDC_NONNULL_ARG( ( 1 ) ) \
57 int writeUint32Time( INOUT STREAM *stream, const time_t timeVal );
58 RETVAL STDC_NONNULL_ARG( ( 1, 2 ) ) \
59 int readUint64Time( INOUT STREAM *stream, OUT time_t *timeVal );
60 RETVAL STDC_NONNULL_ARG( ( 1 ) ) \
61 int writeUint64Time( INOUT STREAM *stream, const time_t timeVal );
62 
63 /* Read and write strings preceded by 32-bit lengths */
64 
65 #define sizeofString32( string, stringLength ) \
66  ( ( stringLength > 0 ) ? ( UINT32_SIZE + stringLength ) : \
67  ( UINT32_SIZE + strlen( string ) ) )
68 
69 RETVAL STDC_NONNULL_ARG( ( 1, 2, 4 ) ) \
70 int readString32( INOUT STREAM *stream,
72  void *string, IN_LENGTH_SHORT const int stringMaxLength,
74 RETVAL STDC_NONNULL_ARG( ( 1, 2 ) ) \
75 int writeString32( INOUT STREAM *stream,
77  const void *string,
78  IN_LENGTH_SHORT const int stringLength );
79 
80 /* Read a raw object preceded by a 32-bit length */
81 
82 CHECK_RETVAL STDC_NONNULL_ARG( ( 1, 2, 4 ) ) \
83 int readRawObject32( INOUT STREAM *stream,
84  OUT_BUFFER( bufferMaxLength, *bufferLength ) \
85  void *buffer,
87  const int bufferMaxLength,
89 
90 /* Read a universal type and discard it (used to skip unknown or unwanted
91  types) */
92 
93 RETVAL STDC_NONNULL_ARG( ( 1 ) ) \
94 int readUniversal16( INOUT STREAM *stream );
95 RETVAL STDC_NONNULL_ARG( ( 1 ) ) \
96 int readUniversal32( INOUT STREAM *stream );
97 
98 /* Read and write unsigned (large) integers preceded by 16- and 32-bit
99  lengths, lengths in bits */
100 
101 #define sizeofInteger16U( integerLength ) ( UINT16_SIZE + integerLength )
102 #define sizeofInteger32( integer, integerLength ) \
103  ( UINT32_SIZE + ( ( ( ( BYTE * ) integer )[ 0 ] & 0x80 ) ? 1 : 0 ) + \
104  integerLength )
105 
106 RETVAL STDC_NONNULL_ARG( ( 1, 3 ) ) \
107 int readInteger16U( INOUT STREAM *stream,
110  IN_LENGTH_PKC const int minLength,
111  IN_LENGTH_PKC const int maxLength );
112 RETVAL STDC_NONNULL_ARG( ( 1, 3 ) ) \
113 int readInteger16Ubits( INOUT STREAM *stream,
116  IN_LENGTH_PKC const int minLength,
117  IN_LENGTH_PKC const int maxLength );
118 RETVAL STDC_NONNULL_ARG( ( 1, 3 ) ) \
119 int readInteger32( INOUT STREAM *stream,
122  IN_LENGTH_PKC const int minLength,
123  IN_LENGTH_PKC const int maxLength );
124 RETVAL STDC_NONNULL_ARG( ( 1, 2 ) ) \
125 int writeInteger16U( INOUT STREAM *stream,
127  const void *integer,
128  IN_LENGTH_PKC const int integerLength );
129 RETVAL STDC_NONNULL_ARG( ( 1, 2 ) ) \
130 int writeInteger16Ubits( INOUT STREAM *stream,
132  const void *integer,
133  IN_LENGTH_PKC const int integerLength );
134 RETVAL STDC_NONNULL_ARG( ( 1, 2 ) ) \
135 int writeInteger32( INOUT STREAM *stream,
137  const void *integer,
138  IN_LENGTH_PKC const int integerLength );
139 
140 /* Special-case large integer read routines that explicitly check for a too-
141  short key and return CRYPT_ERROR_NOSECURE rather than the
142  CRYPT_ERROR_BADDATA that'd otherwise be returned */
143 
144 CHECK_RETVAL STDC_NONNULL_ARG( ( 1, 3 ) ) \
145 int readInteger16UChecked( INOUT STREAM *stream,
148  IN_LENGTH_PKC const int minLength,
149  IN_LENGTH_PKC const int maxLength );
150 CHECK_RETVAL STDC_NONNULL_ARG( ( 1, 3 ) ) \
151 int readInteger32Checked( INOUT STREAM *stream,
154  IN_LENGTH_PKC const int minLength,
155  IN_LENGTH_PKC const int maxLength );
156 
157 #ifdef USE_PKC
158 
159 /* Read and write bignum integers */
160 
161 RETVAL STDC_NONNULL_ARG( ( 1, 2 ) ) \
162 int readBignumInteger16U( INOUT STREAM *stream,
163  INOUT TYPECAST( BIGNUM ) void *bignum,
164  IN_LENGTH_PKC const int minLength,
165  IN_LENGTH_PKC const int maxLength,
166  IN_OPT TYPECAST( BIGNUM ) const void *maxRange );
167 RETVAL STDC_NONNULL_ARG( ( 1, 2 ) ) \
168 int writeBignumInteger16U( INOUT STREAM *stream,
169  TYPECAST( BIGNUM ) const void *bignum );
170 RETVAL STDC_NONNULL_ARG( ( 1, 2 ) ) \
171 int readBignumInteger16Ubits( INOUT STREAM *stream,
172  INOUT TYPECAST( BIGNUM ) void *bignum,
173  IN_LENGTH_PKC const int minBits,
174  IN_LENGTH_PKC const int maxBits,
175  IN_OPT TYPECAST( BIGNUM ) const void *maxRange );
176 RETVAL STDC_NONNULL_ARG( ( 1, 2 ) ) \
177 int writeBignumInteger16Ubits( INOUT STREAM *stream,
178  TYPECAST( BIGNUM ) const void *bignum );
179 CHECK_RETVAL_RANGE( MAX_ERROR, MAX_INTLENGTH_SHORT ) STDC_NONNULL_ARG( ( 1 ) ) \
180 int sizeofBignumInteger32( const void *bignum );
181 RETVAL STDC_NONNULL_ARG( ( 1, 2 ) ) \
182 int readBignumInteger32( INOUT STREAM *stream,
183  INOUT TYPECAST( BIGNUM ) void *bignum,
184  IN_LENGTH_PKC const int minLength,
185  IN_LENGTH_PKC const int maxLength,
186  IN_OPT TYPECAST( BIGNUM ) const void *maxRange );
187 RETVAL STDC_NONNULL_ARG( ( 1 ) ) \
188 int writeBignumInteger32( INOUT STREAM *stream,
189  TYPECAST( BIGNUM ) const void *bignum );
190 
191 /* Special-case bignum read routines that explicitly check for a too-short
192  key and return CRYPT_ERROR_NOSECURE rather than the CRYPT_ERROR_BADDATA
193  that'd otherwise be returned */
194 
195 CHECK_RETVAL STDC_NONNULL_ARG( ( 1, 2 ) ) \
196 int readBignumInteger16UChecked( INOUT STREAM *stream,
197  INOUT TYPECAST( BIGNUM * ) void *bignum,
198  IN_LENGTH_PKC const int minLength,
199  IN_LENGTH_PKC const int maxLength );
200 CHECK_RETVAL STDC_NONNULL_ARG( ( 1, 2 ) ) \
201 int readBignumInteger16UbitsChecked( INOUT STREAM *stream,
202  INOUT TYPECAST( BIGNUM * ) void *bignum,
203  IN_LENGTH_PKC const int minBits,
204  IN_LENGTH_PKC const int maxBits );
205 CHECK_RETVAL STDC_NONNULL_ARG( ( 1, 2 ) ) \
206 int readBignumInteger32Checked( INOUT STREAM *stream,
207  INOUT TYPECAST( BIGNUM * ) void *bignum,
208  IN_LENGTH_PKC const int minLength,
209  IN_LENGTH_PKC const int maxLength );
210 #endif /* USE_PKC */
211 
212 #ifdef _PGP_DEFINED
213 
214 /* PGP-specific read/write routines. The difference between
215  pgpReadPacketHeader() and pgpReadPacketHeaderI() is that the latter
216  allows indefinite-length encoding for partial lengths. Once we've
217  read an indefinite length, we have to use pgpReadPartialLengh() to
218  read subsequence partial-length values */
219 
221 int pgpReadShortLength( INOUT STREAM *stream, OUT_LENGTH int *length,
222  IN_BYTE const int ctb );
223 RETVAL STDC_NONNULL_ARG( ( 1 ) ) \
224 int pgpWriteLength( INOUT STREAM *stream, IN_LENGTH const long length );
226 int pgpReadPacketHeader( INOUT STREAM *stream, OUT_OPT_BYTE int *ctb,
227  OUT_OPT_LENGTH_Z long *length,
228  IN_LENGTH_SHORT const int minLength );
230 int pgpReadPacketHeaderI( INOUT STREAM *stream, OUT_OPT_BYTE int *ctb,
231  OUT_OPT_LENGTH_Z long *length,
232  IN_LENGTH_SHORT const int minLength );
234 int pgpReadPartialLength( INOUT STREAM *stream,
235  OUT_OPT_LENGTH_Z long *length );
236 RETVAL STDC_NONNULL_ARG( ( 1 ) ) \
237 int pgpWritePacketHeader( INOUT STREAM *stream,
238  IN_ENUM( PGP_PACKET ) \
239  const PGP_PACKET_TYPE packetType,
240  IN_LENGTH const long length );
241 #define pgpSizeofLength( length ) \
242  ( ( length < 0 ) ? length : \
243  ( length <= 191 ) ? 1 : \
244  ( length <= 8383 ) ? 2 : 4 )
245 #endif /* _PGP_DEFINED */
246 
247 #endif /* !_MISCRW_DEFINED */