Embedded systems coursework 2.
Fork of Crypto_light by
Embed:
(wiki syntax)
Show/hide line numbers
HMAC.cpp
00001 #include "HMAC.h" 00002 #include <string.h> 00003 00004 HMAC::HMAC(HashAlgorithm *hashAlgo, uint8_t *k, uint32_t kl): 00005 algo(hashAlgo), 00006 keyLength(kl) 00007 { 00008 memcpy(key, k, keyLength); 00009 uint8_t buffer[64]; 00010 memcpy(buffer, key, keyLength); 00011 memset(&buffer[keyLength], 0, 64-keyLength); 00012 00013 for(int i = 0; i < 64; ++i) 00014 buffer[i] ^= 0x36; 00015 00016 algo->update(buffer, 64); 00017 } 00018 00019 HMAC::~HMAC() 00020 { 00021 delete algo; 00022 } 00023 00024 void HMAC::update(uint8_t *data, uint32_t length) 00025 { 00026 algo->update(data, length); 00027 } 00028 00029 void HMAC::finalize(uint8_t *hash) 00030 { 00031 uint8_t buffer[64], buffer2[64]; 00032 algo->finalize(buffer); 00033 00034 memcpy(buffer2, key, keyLength); 00035 memset(&buffer2[keyLength], 0, 64-keyLength); 00036 for(int i = 0; i < 64; ++i) 00037 buffer2[i] ^= 0x5C; 00038 00039 algo->update(buffer2, 64); 00040 algo->update(buffer, algo->outputSize()); 00041 algo->finalize(hash); 00042 } 00043
Generated on Wed Jul 20 2022 22:55:43 by 1.7.2