Fork of François Berder Crypto, fixed AES CBC and small rework

Dependents:   AES_example shaun_larada Smartage

Fork of Crypto by Francois Berder

hash/HMAC.cpp

Committer:
Geremia
Date:
2015-01-28
Revision:
16:4399e2e6260b
Parent:
11:96d87a5394ee

File content as of revision 16:4399e2e6260b:

#include "HMAC.h"
#include <string.h>

HMAC::HMAC(HashAlgorithm *hashAlgo, uint8_t *k, uint32_t kl):
algo(hashAlgo),
keyLength(kl)
{
    memcpy(key, k, keyLength);
    uint8_t buffer[64];
    memcpy(buffer, key, keyLength);
    memset(&buffer[keyLength], 0, 64-keyLength);  
    
    for(int i = 0; i < 64; ++i)
        buffer[i] ^= 0x36;
        
    algo->update(buffer, 64);
}

HMAC::~HMAC()
{
    delete algo;
}

void HMAC::update(uint8_t *data, uint32_t length)
{
    algo->update(data, length);
}

void HMAC::finalize(uint8_t *hash)
{
    uint8_t buffer[64], buffer2[64];
    algo->finalize(buffer);
    
    memcpy(buffer2, key, keyLength);
    memset(&buffer2[keyLength], 0, 64-keyLength);
    for(int i = 0; i < 64; ++i)
        buffer2[i] ^= 0x5C;
        
    algo->update(buffer2, 64);
    algo->update(buffer, algo->outputSize());
    algo->finalize(hash);
}