Formatting changes.
[libradsec.git] / radsecproxy-hash.c
1 /* Copyright (c) 2006-2010, UNINETT AS
2  * Copyright (c) 2010-2012, NORDUnet A/S */
3 /* See LICENSE for licensing information. */
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <unistd.h>
8
9 #include "radsecproxy.h"
10 #include "fticks_hashmac.h"
11
12 void
13 usage()
14 {
15   fprintf(stderr,
16           "usage: radsecproxy-hash [-h] [-k key] [-t type]\n"
17 #if defined (READ_CONFIG)
18           "   -c configfile\tuse configuration from CONFIGFILE\n"
19 #endif
20           "   -h\t\t\tdisplay this help and exit\n"
21           "   -k key\t\tuse KEY for HMAC\n"
22           "   -t type\t\tprint digest of type TYPE [mac|hmac]\n");
23   exit(1);
24 }
25
26 #define MYNAME "radsecproxy-hash"
27
28 int
29 main(int argc, char *argv[])
30 {
31   int opt;
32 #if defined(READ_CONFIG)
33   char *config = NULL;
34 #endif
35   uint8_t buf[256];
36   char mac[80+1];
37   uint8_t *key = NULL;
38   enum { TYPE_HASH, TYPE_HMAC } type = TYPE_HASH;
39
40   while ((opt = getopt(argc, argv, "hk:t:")) != -1) {
41     switch (opt) {
42 #if defined(READ_CONFIG)
43     case 'c':
44       config = optarg;
45       break;
46 #endif
47     case 'h':
48       usage();
49     case 'k':
50       key = (uint8_t *) optarg;
51       break;
52     case 't':
53       if (strcmp(optarg, "hash") == 0)
54         type = TYPE_HASH;
55       else if (strcmp(optarg, "hmac") == 0)
56         type = TYPE_HMAC;
57       else
58         usage();
59       break;
60     default:
61       usage();
62     }
63   }
64
65   while (fgets(mac, sizeof(mac), stdin) != NULL) {
66     if (type == TYPE_HASH) {
67       if (fticks_hashmac((uint8_t *) mac, NULL, sizeof(buf), buf) != 0) {
68         fprintf(stderr, "%s: out of memory\n", MYNAME);
69         return 3;
70       }
71     }
72     else if (type == TYPE_HMAC) {
73       if (key == NULL) {
74         fprintf(stderr, "%s: generating HMAC requires a key, use `-k'\n",
75                 MYNAME);
76         return 2;
77       }
78       if (fticks_hashmac((uint8_t *) mac, key, sizeof(buf), buf) != 0) {
79         fprintf(stderr, "%s: out of memory\n", MYNAME);
80         return 3;
81       }
82     }
83     puts((const char *) buf);
84   }
85
86   return 0;
87 }