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

◆ EVP_MAX_MD_SIZE

#define EVP_MAX_MD_SIZE   1

Definition at line 82 of file test_hmac.c.

Referenced by main(), and ota_pack_push_headers().

Function Documentation

◆ main()

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 #if OPENSSL_VERSION_NUMBER < 0x10100000L
113  HMAC_Init(ctx, octstr_get_cstr(key), octstr_len(key), EVP_sha1());
114 #else
115  ctx = HMAC_CTX_new();
116 #endif
117  p = HMAC(EVP_sha1(), octstr_get_cstr(key), octstr_len(key),
118  octstr_get_cstr(data), octstr_len(data),
119  macbuf, &mac_len);
120 #if OPENSSL_VERSION_NUMBER < 0x10100000L
121  HMAC_cleanup(ctx);
122 #else
123  HMAC_CTX_free(ctx);
124 #endif
125 #else
126  macbuf[0] = 0;
127  mac_len = 0;
128  p = macbuf;
129  warning(0, "No SSL support. Can't calculate HMAC value.");
130 #endif
131 
132  mac = octstr_create_from_data(p, mac_len);
133  octstr_binary_to_hex(mac, 0);
134 
135  debug("",0,"HMAC of file `%s' and key `%s' is:",
136  octstr_get_cstr(filename), octstr_get_cstr(key));
137  octstr_dump(mac, 0);
138 
139  octstr_destroy(data);
140  octstr_destroy(mac);
141  octstr_destroy(key);
142  gwlib_shutdown();
143  return 0;
144 }
#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:660
void octstr_destroy(Octstr *ostr)
Definition: octstr.c:322
char filename[FILENAME_MAX+1]
Definition: log.c:171
#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:726
#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.