Kannel: Open Source WAP and SMS gateway  $Revision: 5037 $
test_hmac.c File Reference
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include "gwlib/gwlib.h"

Go to the source code of this file.

Macros

#define EVP_MAX_MD_SIZE   1
 

Functions

int main (int argc, char **argv)
 

Macro Definition Documentation

#define EVP_MAX_MD_SIZE   1

Definition at line 82 of file test_hmac.c.

Referenced by main(), ota_pack_push_headers(), and our_hash_func().

Function Documentation

int main ( int  argc,
char **  argv 
)

Definition at line 85 of file test_hmac.c.

References debug(), EVP_MAX_MD_SIZE, filename, get_and_set_debugs(), gwlib_init(), gwlib_shutdown(), octstr_binary_to_hex(), octstr_create, octstr_create_from_data, octstr_destroy(), octstr_dump, octstr_get_cstr, octstr_len(), octstr_read_file(), panic, and warning().

86 {
87  Octstr *data, *filename, *mac, *key;
88  unsigned char macbuf[EVP_MAX_MD_SIZE], *p;
89  int mac_len;
90 #ifdef HAVE_LIBSSL
91  HMAC_CTX ctx;
92 #endif
93 
94  gwlib_init();
95 
96  get_and_set_debugs(argc, argv, NULL);
97 
98  if (argc < 3)
99  panic(0, "Syntax: %s <key> <file>\n", argv[0]);
100 
101  key = octstr_create(argv[1]);
102  filename = octstr_create(argv[2]);
103  data = octstr_read_file(octstr_get_cstr(filename));
104 
105  if (data == NULL)
106  panic(0, "Cannot read file.");
107 
108  debug("",0,"Dumping file `%s':", octstr_get_cstr(filename));
109  octstr_dump(data, 0);
110 
111 #ifdef HAVE_LIBSSL
112  HMAC_Init(&ctx, octstr_get_cstr(key), octstr_len(key), EVP_sha1());
113  p = HMAC(EVP_sha1(), octstr_get_cstr(key), octstr_len(key),
114  octstr_get_cstr(data), octstr_len(data),
115  macbuf, &mac_len);
116  HMAC_cleanup(&ctx);
117 #else
118  macbuf[0] = 0;
119  mac_len = 0;
120  p = macbuf;
121  warning(0, "No SSL support. Can't calculate HMAC value.");
122 #endif
123 
124  mac = octstr_create_from_data(p, mac_len);
125  octstr_binary_to_hex(mac, 0);
126 
127  debug("",0,"HMAC of file `%s' and key `%s' is:",
128  octstr_get_cstr(filename), octstr_get_cstr(key));
129  octstr_dump(mac, 0);
130 
131  octstr_destroy(data);
132  octstr_destroy(mac);
133  octstr_destroy(key);
134  gwlib_shutdown();
135  return 0;
136 }
#define octstr_get_cstr(ostr)
Definition: octstr.h:233
void octstr_binary_to_hex(Octstr *ostr, int uppercase)
Definition: octstr.c:463
#define octstr_dump(ostr, level,...)
Definition: octstr.h:564
void warning(int err, const char *fmt,...)
Definition: log.c:624
void octstr_destroy(Octstr *ostr)
Definition: octstr.c:322
char filename[FILENAME_MAX+1]
Definition: log.c:135
#define octstr_create(cstr)
Definition: octstr.h:125
Octstr * octstr_read_file(const char *filename)
Definition: octstr.c:1546
long octstr_len(const Octstr *ostr)
Definition: octstr.c:340
Definition: octstr.c:118
void debug(const char *place, int err, const char *fmt,...)
Definition: log.c:690
#define panic
Definition: log.h:87
void gwlib_shutdown(void)
Definition: gwlib.c:94
#define EVP_MAX_MD_SIZE
Definition: test_hmac.c:82
void gwlib_init(void)
Definition: gwlib.c:78
int get_and_set_debugs(int argc, char **argv, int(*find_own)(int index, int argc, char **argv))
Definition: utils.c:626
#define octstr_create_from_data(data, len)
Definition: octstr.h:134
See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.