PolarSSL v1.2.5
test_suite_pbkdf2.c
Go to the documentation of this file.
1 #include "fct.h"
2 
3 #include <polarssl/pbkdf2.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_PBKDF2_C
230 
231 
232  FCT_SUITE_BGN(test_suite_pbkdf2)
233  {
234 
235  FCT_TEST_BGN(pbkdf2_rfc_6070_test_vector_1_sha1)
236  {
237  unsigned char pw_str[100];
238  unsigned char salt_str[100];
239  unsigned char dst_str[100];
240 
241  md_context_t ctx;
242  const md_info_t *info;
243 
244  int pw_len, salt_len;
245  unsigned char key[100];
246 
247  memset(pw_str, 0x00, 100);
248  memset(salt_str, 0x00, 100);
249  memset(dst_str, 0x00, 100);
250 
251  pw_len = unhexify( pw_str, "70617373776f7264" );
252  salt_len = unhexify( salt_str, "73616c74" );
253 
254 
256  fct_chk( info != NULL );
257  fct_chk( md_init_ctx( &ctx, info ) == 0 );
258  fct_chk( pbkdf2_hmac( &ctx, pw_str, pw_len, salt_str, salt_len,
259  1, 20, key ) == 0 );
260 
261  hexify( dst_str, key, 20 );
262  fct_chk( strcmp( (char *) dst_str, "0c60c80f961f0e71f3a9b524af6012062fe037a6" ) == 0 );
263  }
264  FCT_TEST_END();
265 
266 
267  FCT_TEST_BGN(pbkdf2_rfc_6070_test_vector_2_sha1)
268  {
269  unsigned char pw_str[100];
270  unsigned char salt_str[100];
271  unsigned char dst_str[100];
272 
273  md_context_t ctx;
274  const md_info_t *info;
275 
276  int pw_len, salt_len;
277  unsigned char key[100];
278 
279  memset(pw_str, 0x00, 100);
280  memset(salt_str, 0x00, 100);
281  memset(dst_str, 0x00, 100);
282 
283  pw_len = unhexify( pw_str, "70617373776f7264" );
284  salt_len = unhexify( salt_str, "73616c74" );
285 
286 
288  fct_chk( info != NULL );
289  fct_chk( md_init_ctx( &ctx, info ) == 0 );
290  fct_chk( pbkdf2_hmac( &ctx, pw_str, pw_len, salt_str, salt_len,
291  2, 20, key ) == 0 );
292 
293  hexify( dst_str, key, 20 );
294  fct_chk( strcmp( (char *) dst_str, "ea6c014dc72d6f8ccd1ed92ace1d41f0d8de8957" ) == 0 );
295  }
296  FCT_TEST_END();
297 
298 
299  FCT_TEST_BGN(pbkdf2_rfc_6070_test_vector_3_sha1)
300  {
301  unsigned char pw_str[100];
302  unsigned char salt_str[100];
303  unsigned char dst_str[100];
304 
305  md_context_t ctx;
306  const md_info_t *info;
307 
308  int pw_len, salt_len;
309  unsigned char key[100];
310 
311  memset(pw_str, 0x00, 100);
312  memset(salt_str, 0x00, 100);
313  memset(dst_str, 0x00, 100);
314 
315  pw_len = unhexify( pw_str, "70617373776f7264" );
316  salt_len = unhexify( salt_str, "73616c74" );
317 
318 
320  fct_chk( info != NULL );
321  fct_chk( md_init_ctx( &ctx, info ) == 0 );
322  fct_chk( pbkdf2_hmac( &ctx, pw_str, pw_len, salt_str, salt_len,
323  4096, 20, key ) == 0 );
324 
325  hexify( dst_str, key, 20 );
326  fct_chk( strcmp( (char *) dst_str, "4b007901b765489abead49d926f721d065a429c1" ) == 0 );
327  }
328  FCT_TEST_END();
329 
330 
331  FCT_TEST_BGN(pbkdf2_rfc_6070_test_vector_5_sha1)
332  {
333  unsigned char pw_str[100];
334  unsigned char salt_str[100];
335  unsigned char dst_str[100];
336 
337  md_context_t ctx;
338  const md_info_t *info;
339 
340  int pw_len, salt_len;
341  unsigned char key[100];
342 
343  memset(pw_str, 0x00, 100);
344  memset(salt_str, 0x00, 100);
345  memset(dst_str, 0x00, 100);
346 
347  pw_len = unhexify( pw_str, "70617373776f726450415353574f524470617373776f7264" );
348  salt_len = unhexify( salt_str, "73616c7453414c5473616c7453414c5473616c7453414c5473616c7453414c5473616c74" );
349 
350 
352  fct_chk( info != NULL );
353  fct_chk( md_init_ctx( &ctx, info ) == 0 );
354  fct_chk( pbkdf2_hmac( &ctx, pw_str, pw_len, salt_str, salt_len,
355  4096, 25, key ) == 0 );
356 
357  hexify( dst_str, key, 25 );
358  fct_chk( strcmp( (char *) dst_str, "3d2eec4fe41c849b80c8d83662c0e44a8b291a964cf2f07038" ) == 0 );
359  }
360  FCT_TEST_END();
361 
362 
363  FCT_TEST_BGN(pbkdf2_rfc_6070_test_vector_6_sha1)
364  {
365  unsigned char pw_str[100];
366  unsigned char salt_str[100];
367  unsigned char dst_str[100];
368 
369  md_context_t ctx;
370  const md_info_t *info;
371 
372  int pw_len, salt_len;
373  unsigned char key[100];
374 
375  memset(pw_str, 0x00, 100);
376  memset(salt_str, 0x00, 100);
377  memset(dst_str, 0x00, 100);
378 
379  pw_len = unhexify( pw_str, "7061737300776f7264" );
380  salt_len = unhexify( salt_str, "7361006c74" );
381 
382 
384  fct_chk( info != NULL );
385  fct_chk( md_init_ctx( &ctx, info ) == 0 );
386  fct_chk( pbkdf2_hmac( &ctx, pw_str, pw_len, salt_str, salt_len,
387  4096, 16, key ) == 0 );
388 
389  hexify( dst_str, key, 16 );
390  fct_chk( strcmp( (char *) dst_str, "56fa6aa75548099dcc37d7f03425e0c3" ) == 0 );
391  }
392  FCT_TEST_END();
393 
394  }
395  FCT_SUITE_END();
396 
397 #endif /* POLARSSL_PBKDF2_C */
398 
399 }
400 FCT_END();
401