PolarSSL v1.2.8
test_suite_error.c
Go to the documentation of this file.
1 #include "fct.h"
2 #include <polarssl/config.h>
3 
4 #include <polarssl/error.h>
5 
6 #ifdef _MSC_VER
7 #include <basetsd.h>
8 typedef UINT32 uint32_t;
9 #else
10 #include <inttypes.h>
11 #endif
12 
13 /*
14  * 32-bit integer manipulation macros (big endian)
15  */
16 #ifndef GET_UINT32_BE
17 #define GET_UINT32_BE(n,b,i) \
18 { \
19  (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
20  | ( (uint32_t) (b)[(i) + 1] << 16 ) \
21  | ( (uint32_t) (b)[(i) + 2] << 8 ) \
22  | ( (uint32_t) (b)[(i) + 3] ); \
23 }
24 #endif
25 
26 #ifndef PUT_UINT32_BE
27 #define PUT_UINT32_BE(n,b,i) \
28 { \
29  (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
30  (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
31  (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
32  (b)[(i) + 3] = (unsigned char) ( (n) ); \
33 }
34 #endif
35 
36 int unhexify(unsigned char *obuf, const char *ibuf)
37 {
38  unsigned char c, c2;
39  int len = strlen(ibuf) / 2;
40  assert(!(strlen(ibuf) %1)); // must be even number of bytes
41 
42  while (*ibuf != 0)
43  {
44  c = *ibuf++;
45  if( c >= '0' && c <= '9' )
46  c -= '0';
47  else if( c >= 'a' && c <= 'f' )
48  c -= 'a' - 10;
49  else if( c >= 'A' && c <= 'F' )
50  c -= 'A' - 10;
51  else
52  assert( 0 );
53 
54  c2 = *ibuf++;
55  if( c2 >= '0' && c2 <= '9' )
56  c2 -= '0';
57  else if( c2 >= 'a' && c2 <= 'f' )
58  c2 -= 'a' - 10;
59  else if( c2 >= 'A' && c2 <= 'F' )
60  c2 -= 'A' - 10;
61  else
62  assert( 0 );
63 
64  *obuf++ = ( c << 4 ) | c2;
65  }
66 
67  return len;
68 }
69 
70 void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
71 {
72  unsigned char l, h;
73 
74  while (len != 0)
75  {
76  h = (*ibuf) / 16;
77  l = (*ibuf) % 16;
78 
79  if( h < 10 )
80  *obuf++ = '0' + h;
81  else
82  *obuf++ = 'a' + h - 10;
83 
84  if( l < 10 )
85  *obuf++ = '0' + l;
86  else
87  *obuf++ = 'a' + l - 10;
88 
89  ++ibuf;
90  len--;
91  }
92 }
93 
103 static int rnd_std_rand( void *rng_state, unsigned char *output, size_t len )
104 {
105  size_t i;
106 
107  if( rng_state != NULL )
108  rng_state = NULL;
109 
110  for( i = 0; i < len; ++i )
111  output[i] = rand();
112 
113  return( 0 );
114 }
115 
121 static int rnd_zero_rand( void *rng_state, unsigned char *output, size_t len )
122 {
123  if( rng_state != NULL )
124  rng_state = NULL;
125 
126  memset( output, 0, len );
127 
128  return( 0 );
129 }
130 
131 typedef struct
132 {
133  unsigned char *buf;
134  size_t length;
135 } rnd_buf_info;
136 
148 static int rnd_buffer_rand( void *rng_state, unsigned char *output, size_t len )
149 {
150  rnd_buf_info *info = (rnd_buf_info *) rng_state;
151  size_t use_len;
152 
153  if( rng_state == NULL )
154  return( rnd_std_rand( NULL, output, len ) );
155 
156  use_len = len;
157  if( len > info->length )
158  use_len = info->length;
159 
160  if( use_len )
161  {
162  memcpy( output, info->buf, use_len );
163  info->buf += use_len;
164  info->length -= use_len;
165  }
166 
167  if( len - use_len > 0 )
168  return( rnd_std_rand( NULL, output + use_len, len - use_len ) );
169 
170  return( 0 );
171 }
172 
180 typedef struct
181 {
182  uint32_t key[16];
183  uint32_t v0, v1;
185 
194 static int rnd_pseudo_rand( void *rng_state, unsigned char *output, size_t len )
195 {
196  rnd_pseudo_info *info = (rnd_pseudo_info *) rng_state;
197  uint32_t i, *k, sum, delta=0x9E3779B9;
198  unsigned char result[4];
199 
200  if( rng_state == NULL )
201  return( rnd_std_rand( NULL, output, len ) );
202 
203  k = info->key;
204 
205  while( len > 0 )
206  {
207  size_t use_len = ( len > 4 ) ? 4 : len;
208  sum = 0;
209 
210  for( i = 0; i < 32; i++ )
211  {
212  info->v0 += (((info->v1 << 4) ^ (info->v1 >> 5)) + info->v1) ^ (sum + k[sum & 3]);
213  sum += delta;
214  info->v1 += (((info->v0 << 4) ^ (info->v0 >> 5)) + info->v0) ^ (sum + k[(sum>>11) & 3]);
215  }
216 
217  PUT_UINT32_BE( info->v0, result, 0 );
218  memcpy( output, result, use_len );
219  len -= use_len;
220  }
221 
222  return( 0 );
223 }
224 
225 
227 {
228 #ifdef POLARSSL_ERROR_C
229 
230 
231  FCT_SUITE_BGN(test_suite_error)
232  {
233 #ifdef POLARSSL_AES_C
234 
235  FCT_TEST_BGN(single_low_error)
236  {
237  char buf[500];
238 
239  error_strerror( -0x0020, buf, 500 );
240 
241  fct_chk( strcmp( buf, "AES - Invalid key length" ) == 0 );
242  }
243  FCT_TEST_END();
244 #endif /* POLARSSL_AES_C */
245 
246 #ifdef POLARSSL_RSA_C
247 
248  FCT_TEST_BGN(single_high_error)
249  {
250  char buf[500];
251 
252  error_strerror( -0x4080, buf, 500 );
253 
254  fct_chk( strcmp( buf, "RSA - Bad input parameters to function" ) == 0 );
255  }
256  FCT_TEST_END();
257 #endif /* POLARSSL_RSA_C */
258 
259 #ifdef POLARSSL_AES_C
260 #ifdef POLARSSL_RSA_C
261 
262  FCT_TEST_BGN(low_and_high_error)
263  {
264  char buf[500];
265 
266  error_strerror( -0x40A0, buf, 500 );
267 
268  fct_chk( strcmp( buf, "RSA - Bad input parameters to function : AES - Invalid key length" ) == 0 );
269  }
270  FCT_TEST_END();
271 #endif /* POLARSSL_AES_C */
272 #endif /* POLARSSL_RSA_C */
273 
274 
275  FCT_TEST_BGN(non_existing_high_error)
276  {
277  char buf[500];
278 
279  error_strerror( -0x8880, buf, 500 );
280 
281  fct_chk( strcmp( buf, "UNKNOWN ERROR CODE (8880)" ) == 0 );
282  }
283  FCT_TEST_END();
284 
285 
286  FCT_TEST_BGN(non_existing_low_error)
287  {
288  char buf[500];
289 
290  error_strerror( -0x0001, buf, 500 );
291 
292  fct_chk( strcmp( buf, "UNKNOWN ERROR CODE (0001)" ) == 0 );
293  }
294  FCT_TEST_END();
295 
296 
297  FCT_TEST_BGN(non_existing_low_and_high_error)
298  {
299  char buf[500];
300 
301  error_strerror( -0x8881, buf, 500 );
302 
303  fct_chk( strcmp( buf, "UNKNOWN ERROR CODE (8880) : UNKNOWN ERROR CODE (0001)" ) == 0 );
304  }
305  FCT_TEST_END();
306 
307  }
308  FCT_SUITE_END();
309 
310 #endif /* POLARSSL_ERROR_C */
311 
312 }
313 FCT_END();
314