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