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