How to use an HMAC?

Get help with using the PHP Secure Communications Library.

Moderator: Nuxius

Forum rules
The purpose of this forum is to provide support for phpseclib, a pure PHP SSH / SFTP / RSA library.

Posts by new users are held in a moderation queue and are not publicly visible until the post is approved.

How to use an HMAC?

Postby wise.man » Sun Jan 08, 2012 12:05 pm

i am using a cryptographic library in php (http://phpseclib.sourceforge.net/).

i am adding a new pair of methods to its AES class.
those methods implement HMAC generation/verification.
encryption method adds the hmac string to the ciphertext.
decryption method separates the hmac and verifies it.

i have several questions.

1) is this general formula correct for the encryption method:

Code: Select all
ciphertext=aes_encr(key, plaintext)
final_result=hmac(key, ciphertext)||ciphertext

note: || means concatenation.

2) it is better the hmac be appended or prepended to the cipher text and why (or maybe there is no deference?).

3) i can use a variety of hash algorithms for hmac:
md2, md5, md5-96, sha1, sha1-96, sha256, sha384, and sha512

but i dont want to degrade the performance and increase the output length unnecessarily. if it is relevant, i use AES 128 bits; i am not sure if there must be a correlation between the encryption key length and the hmac algorithm used.

i know that the md5 and sha1 hash algorithms have known weaknesses and should no longer be used, but wikipedia article about HMAC says:
HMACs are substantially less affected by collisions than their underlying hashing algorithms alone.[5] [6] .[7] Therefore, HMAC-MD5 does not suffer from the same weaknesses that have been found in MD5.

so can i use hmac-md5 safely?
wise.man
Traveler
 
Posts: 14
Joined: Tue Nov 15, 2011 5:54 pm

Re: How to use an HMAC?

Postby TerraFrost » Wed Jan 11, 2012 7:46 am

Sorry for the delay.

Anyway...

1) looks good and I'd say hmac-md5 is good as well.

but i dont want to degrade the performance and increase the output length unnecessarily.

It wouldn't be much of a performance hit and unless you're just doing one block it wouldn't increase the length a ton either.
TerraFrost
Legendary Guard
 
Posts: 12217
Joined: Wed Dec 04, 2002 6:37 am

Re: How to use an HMAC?

Postby wise.man » Sun Jan 15, 2012 8:15 am

Code: Select all
function IvEncrypt($plaintext) {
   $iv=random_bytes(16);
   $this->setIV($iv);
   return $iv.$this->encrypt($plaintext);
}

function IvDecrypt($ciphertext) {
   $this->setIV(substr($ciphertext, 0, 16));
   return $this->decrypt(substr($ciphertext, 16));
}

function IvEncryptHmac($plaintext) {
   $hash = new Crypt_Hash('sha1');
   $hash->setKey($this->key);
   $ciphertext=$this->IvEncrypt($plaintext);
   return $hash->hash($ciphertext).$ciphertext;
}

function IvDecryptHmac($ciphertext) {
   $hmac=substr($ciphertext, 0, 20);
   $ciphertext=substr($ciphertext, 20);
   $hash = new Crypt_Hash('sha1');
   $hash->setKey($this->key);
   if($hmac!==$hash->hash($ciphertext)) return false;
   return $this->IvDecrypt($ciphertext);
}
wise.man
Traveler
 
Posts: 14
Joined: Tue Nov 15, 2011 5:54 pm

Re: How to use an HMAC?

Postby TerraFrost » Thu Jan 19, 2012 1:00 am

Looks good to me!
TerraFrost
Legendary Guard
 
Posts: 12217
Joined: Wed Dec 04, 2002 6:37 am


Return to phpseclib support

Who is online

Users browsing this forum: No registered users and 0 guests

cron