LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-12-2005, 02:09 PM   #1
jacques83
Member
 
Registered: Nov 2005
Posts: 34

Rep: Reputation: 15
HMAC function in OPENSSL


i am trying to run the hmac md5 message digest algo in my implementation of IPSEC. i am confused as to how to directly run the hmac function from the openssl library. can anyone tell me the parameters to be passed in this function to run the hmac()?


thanks
 
Old 11-12-2005, 11:23 PM   #2
primo
Member
 
Registered: Jun 2005
Posts: 542

Rep: Reputation: 34
Man, try not to ask the questions in so many threads because it backfires with so many fragmentation. I'll try my best to explain the OpenSSL HMAC API to you. I haven't used it because I don't like it and I have mine.

I'm taking as reference the manual as it appears in FreeBSD:

Quote:
#include <openssl/hmac.h>

unsigned char *HMAC(const EVP_MD *evp_md, const void *key,
int key_len, const unsigned char *d, int n, unsigned char *md, unsigned int *md_len);
This function does it all. The first argument should be either EVP_sha1() or EVP_md5(). There are more. You just choose it. Key and keylen are obvious. the next is the data (d) and the length of it. Then you have the message digest (md) and a value/result pointer to the length of md. (Remember that this length is of the binary data, not its ASCII hexadecimal representation.

Quote:
void HMAC_CTX_init(HMAC_CTX *ctx);
With this one you initialize a HMAC_CTX variable. Note that with the function above you don't need to declare one.

Quote:
void HMAC_Init(HMAC_CTX *ctx, const void *key, int key_len, const EVP_MD *md);
From the manpage:
Quote:
HMAC_Init() initializes a HMAC_CTX structure to use the hash function
evp_md and the key key which is key_len bytes long. It is deprecated
and only included for backward compatibility with OpenSSL 0.9.6b.
Quote:
void HMAC_Init_ex(HMAC_CTX *ctx, const void *key, int key_len, const EVP_MD *md);
void HMAC_Update(HMAC_CTX *ctx, const unsigned char *data, int len);
void HMAC_Final(HMAC_CTX *ctx, unsigned char *md, unsigned int *len);
These functions are very much like hash functions. The last argument in Init_ex is a EVP_xxx() as described above. Remember that HMAC() does it all by first declaring a HMAC_CTX variable, then initialize the context, then setup the key with _Init, Update() it, call _Final() and the a security cleanup of the context (because the key is there)

Quote:
void HMAC_CTX_cleanup(HMAC_CTX *ctx);
void HMAC_cleanup(HMAC_CTX *ctx);

I don't like OpenSSL because it's an horrible API. Sizes should be size_t and much of the book-keeping may be done internally. It may be fast because it has so much assembly, but it's ugly.

Last edited by primo; 11-12-2005 at 11:24 PM.
 
Old 11-13-2005, 08:57 AM   #3
jacques83
Member
 
Registered: Nov 2005
Posts: 34

Original Poster
Rep: Reputation: 15
thanks a lot man, i appreciate it
 
Old 09-30-2008, 10:24 AM   #4
virtualCoder
Member
 
Registered: Sep 2007
Distribution: Ubuntu
Posts: 33

Rep: Reputation: 15
I read that the key used and the quality of the hash function determine the strength of the hash. I would use EVP_sha1 as the function. I can make my own key from a single integer but that wouldnt make it a good key.

Is there a function that allows me to make a good key to use with hmac().

Quote:
This function does it all.
Also If I am understanding you correctly, no other function is needed for initialization if we use unsigned char *HMAC(...) . Is that correct?
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Hmac-md5 abdullahgee Programming 1 06-25-2004 05:27 PM
what are the Hexadecimal function and ASCII function in Perl Bassam Programming 1 06-03-2004 01:44 AM
A main can be changed by a function local without passing anything to the function? ananthbv Programming 10 05-04-2004 01:31 PM
Is the wait function is the same as the sleep function ? Linh Programming 3 04-28-2004 12:39 PM
Perl exec function in linux (and system-function) nazula Programming 1 04-19-2004 12:21 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:32 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration