pkcs5-test.c 1.47 KB
Newer Older
Niels Möller's avatar
Niels Möller committed
1
2
3
4
5
6
/* pkcs5-test.c
 *
 */

#include "crypto.h"

Niels Möller's avatar
Niels Möller committed
7
/* FIXME: In which include file can getopt be found? Solaris man page
8
 * says stdlib.h, linux's says unistd.h. */
Niels Möller's avatar
Niels Möller committed
9
#include <stdlib.h>
10
11
#include <unistd.h>

Niels Möller's avatar
Niels Möller committed
12
#include <stdio.h>
13
#include <string.h>
Niels Möller's avatar
Niels Möller committed
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78

static void
usage(void)
{
  fprintf(stderr, "pkcs5-test [-i iterations] [-s salt] [-l key length] password");
  exit(1);
}

static void
print_hex(unsigned length, const char *data)
{
  static const char digits[16] = "0123456789ABCDEF";
  unsigned i;

  for (i = 0; i<length; i++)
    {
      if (!(i%8))
	putchar(' ');

      putchar(digits[(data[i]/0x10) & 0xf]);
      putchar(digits[data[i] & 0xf]);
    }
}

int main(int argc, char **argv)
{
  int iterations = 10;
  char *salt = "pepper";
  int length = 32;
  char *password;
  int c;

  char *key;
  
  while ((c = getopt(argc, argv, "i:s:l:")) != -1)
    {
      switch (c)
	{
	case 'i':
	  iterations = atoi(optarg);
	  if ( (iterations < 1) || (iterations > 10000000))
	    usage();
	  break;
	case 's':
	  salt = optarg;
	  break;
	case 'l':
	  length = atoi(optarg);
	  if ( (length < 1) || (length > 5000) )
	    usage();
	  break;
	case '?':
	  usage();
	default:
	  abort();
	}
    }

  if (optind != (argc - 1))
    usage();

  password = argv[optind];
  
  key = alloca(length);
  
79
  pkcs5_derive_key(&crypto_hmac_sha1_algorithm,
Niels Möller's avatar
Niels Möller committed
80
81
82
83
84
85
86
87
88
89
90
91
		   strlen(password), password,
		   strlen(salt), salt,
		   iterations,
		   length, key);

  printf("Key:");
  print_hex(length, key);
  printf("\n");

  return 0;
}