/* * Copyright (C) 2010-2011 Mamadou Diop. * * Contact: Mamadou Diop * * This file is part of Open Source Doubango Framework. * * DOUBANGO is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * DOUBANGO is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with DOUBANGO. * */ /**@file tsk_hmac.c * @brief HMAC: Keyed-Hashing for Message Authentication (RFC 2104) / FIPS-198-1. * * @author Mamadou Diop * */ #include "tsk_hmac.h" #include "tsk_string.h" #include "tsk_buffer.h" #include /**@defgroup tsk_hmac_group Keyed-Hashing for Message Authentication (RFC 2104/ FIPS-198-1). */ /**@ingroup tsk_hmac_group */ typedef enum tsk_hash_type_e { md5, sha1 } tsk_hash_type_t; int tsk_hmac_xxxcompute(const uint8_t* input, tsk_size_t input_size, const char* key, tsk_size_t key_size, tsk_hash_type_t type, uint8_t* digest) { #define TSK_MAX_BLOCK_SIZE TSK_SHA1_BLOCK_SIZE tsk_size_t i, newkey_size; tsk_size_t block_size = type == md5 ? TSK_MD5_BLOCK_SIZE : TSK_SHA1_BLOCK_SIZE; // Only SHA-1 and MD5 are supported for now tsk_size_t digest_size = type == md5 ? TSK_MD5_DIGEST_SIZE : TSK_SHA1_DIGEST_SIZE; char hkey [TSK_MAX_BLOCK_SIZE]; uint8_t ipad [TSK_MAX_BLOCK_SIZE]; uint8_t opad [TSK_MAX_BLOCK_SIZE]; memset(ipad, 0, sizeof(ipad)); memset(opad, 0, sizeof(ipad)); /* * H(K XOR opad, H(K XOR ipad, input)) */ // Check key len if (key_size > block_size) { if(type == md5) { TSK_MD5_DIGEST_CALC(key, key_size, (uint8_t*)hkey); } else if(type == sha1) { TSK_SHA1_DIGEST_CALC((uint8_t*)key, (unsigned int)key_size, (uint8_t*)hkey); } else { return -3; } newkey_size = digest_size; } else { memcpy(hkey, key, key_size); newkey_size = key_size; } memcpy(ipad, hkey, newkey_size); memcpy(opad, hkey, newkey_size); /* [K XOR ipad] and [K XOR opad]*/ for (i=0; i