i2pd/hmac.h

62 lines
1.4 KiB
C
Raw Normal View History

2014-01-23 18:03:37 +00:00
#ifndef HMAC_H__
#define HMAC_H__
#include <inttypes.h>
#include <string.h>
#define CRYPTOPP_ENABLE_NAMESPACE_WEAK 1
#include <cryptopp/md5.h>
2014-11-01 18:56:13 +00:00
#include "Identity.h"
2014-01-23 18:03:37 +00:00
namespace i2p
{
namespace crypto
{
const uint64_t IPAD = 0x3636363636363636;
const uint64_t OPAD = 0x5C5C5C5C5C5C5C5C;
2014-11-01 18:56:13 +00:00
typedef i2p::data::Tag<32> MACKey;
inline void HMACMD5Digest (uint8_t * msg, size_t len, const MACKey& key, uint8_t * digest)
2014-01-23 18:03:37 +00:00
// key is 32 bytes
// digest is 16 bytes
// block size is 64 bytes
{
2014-05-07 23:44:43 +00:00
uint64_t buf[256];
2014-01-23 18:03:37 +00:00
// ikeypad
2014-11-01 18:56:13 +00:00
buf[0] = key.GetLL ()[0] ^ IPAD;
buf[1] = key.GetLL ()[1] ^ IPAD;
buf[2] = key.GetLL ()[2] ^ IPAD;
buf[3] = key.GetLL ()[3] ^ IPAD;
2014-05-07 23:44:43 +00:00
buf[4] = IPAD;
buf[5] = IPAD;
buf[6] = IPAD;
buf[7] = IPAD;
2014-01-23 18:03:37 +00:00
// concatenate with msg
2014-05-07 23:44:43 +00:00
memcpy (buf + 8, msg, len);
2014-01-23 18:03:37 +00:00
// calculate first hash
uint8_t hash[16]; // MD5
2014-05-07 23:44:43 +00:00
CryptoPP::Weak1::MD5().CalculateDigest (hash, (uint8_t *)buf, len + 64);
2014-01-23 18:03:37 +00:00
// okeypad
2014-11-01 18:56:13 +00:00
buf[0] = key.GetLL ()[0] ^ OPAD;
buf[1] = key.GetLL ()[1] ^ OPAD;
buf[2] = key.GetLL ()[2] ^ OPAD;
buf[3] = key.GetLL ()[3] ^ OPAD;
2014-05-07 23:44:43 +00:00
buf[4] = OPAD;
buf[5] = OPAD;
buf[6] = OPAD;
buf[7] = OPAD;
2014-01-23 18:03:37 +00:00
// copy first hash after okeypad
2014-05-07 23:44:43 +00:00
memcpy (buf + 8, hash, 16);
2014-01-23 18:03:37 +00:00
// fill next 16 bytes with zeros (first hash size assumed 32 bytes in I2P)
2014-05-07 23:44:43 +00:00
memset (buf + 10, 0, 16);
2014-01-23 18:03:37 +00:00
// calculate digest
2014-05-07 23:44:43 +00:00
CryptoPP::Weak1::MD5().CalculateDigest (digest, (uint8_t *)buf, 96);
2014-01-23 18:03:37 +00:00
}
}
}
#endif