示例#1
0
MHASH mhash_init_int(__const hashid type)
{
    MHASH ret;
    INIT_FUNC func;

    ret = (MHASH) mutils_malloc(sizeof(MHASH_INSTANCE));
    if (ret == NULL)
    {
        return MHASH_FAILED;
    }

    mutils_memset(ret, 0, sizeof(MHASH_INSTANCE));

    ret->algorithm_given = type;

    ret->state_size = _mhash_get_state_size(type);
    if (ret->state_size == 0)
    {
        mutils_free(ret);
        return(MHASH_FAILED);
    }

    if ( (ret->state = mutils_malloc(ret->state_size)) == NULL)
    {
        mutils_free(ret);
        return(MHASH_FAILED);
    }

    func = _mhash_get_init_func( type);

    if (func != NULL)
    {
        func(ret->state);
    }
    else
    {
        mutils_free(ret->state);
        mutils_free(ret);
        return(MHASH_FAILED);
    }

    ret->hash_func = _mhash_get_hash_func( type);
    ret->deinit_func = _mhash_get_deinit_func( type);
    ret->final_func = _mhash_get_final_func( type);

    return ret;

}
示例#2
0
MHASH mhash_cp(MHASH from) {
    MHASH ret;

    ret = (MHASH) mutils_malloc(sizeof(MHASH_INSTANCE));

    if (ret == NULL)
    {
        return(MHASH_FAILED);
    }

    mutils_memcpy(ret, from, sizeof(MHASH_INSTANCE));

    /* copy the internal state also */
    ret->state = (mutils_word8 *) mutils_malloc(ret->state_size);

    if (ret->state == NULL)
    {
        mutils_free(ret);
        return(MHASH_FAILED);
    }

    mutils_memcpy(ret->state, from->state, ret->state_size);

    /* copy the key in case of hmac*/
    if (ret->hmac_key_size != 0)
    {
        ret->hmac_key = (mutils_word8 *) mutils_malloc(ret->hmac_key_size);

        if (ret == NULL)
        {
            mutils_free(ret->state);
            mutils_free(ret);
            return(MHASH_FAILED);
        }

        mutils_memcpy(ret->hmac_key, from->hmac_key, ret->hmac_key_size);
    }
    return ret;
}
示例#3
0
WIN32DLL_DEFINE
void mhash_deinit(MHASH td, void *result)
{

    if (td->final_func != NULL)
    {
        td->final_func( td->state);
    }

    if (td->deinit_func != NULL)
    {
        td->deinit_func( td->state, result);
    }

    if (NULL != td->state)
    {
        mutils_free(td->state);
    }

    mutils_free(td);

    return;
}
示例#4
0
int main()
{

	mutils_word8 *tmp;
	mutils_word8 *password;
	mutils_word32 passlen;
	mutils_word8 *data;
	mutils_word32 datalen;
	MHASH td;
	mutils_word8 *mac;
	mutils_word32 j;
	int result;

	passlen=sizeof(KEY1) - 1;
	password = mutils_malloc(passlen + 1);
	mutils_memcpy(password, (mutils_word8 *) KEY1, passlen);

	datalen = mutils_strlen((mutils_word8 *) DATA1);
	data = mutils_malloc(datalen+1);
	mutils_strcpy(data, (mutils_word8 *) DATA1);

	td = mhash_hmac_init(MHASH_MD5, password, passlen,
			    mhash_get_hash_pblock(MHASH_MD5));

	mhash(td, data, datalen);
	mac = mhash_hmac_end(td);

	tmp = mutils_asciify(mac, mhash_get_block_size(MHASH_MD5));
	
	result = mutils_strcmp((mutils_word8 *) DIGEST1, tmp);

	mutils_free(password);
	mutils_free(data);

	if (result != 0) {
		fprintf(stderr, "HMAC-Test: Failed\n");
		fprintf(stderr, "Digest size: %d\n", mhash_get_block_size(MHASH_MD5));
		fprintf(stderr, "Expecting: 0x%s\n", DIGEST1);
		fprintf(stderr, "Got: 0x%s\n", tmp);
		return(MUTILS_INVALID_RESULT);
	}

	mutils_free(tmp);

	/* Test No 2 */	
	
	mutils_memset(tmp, 0, sizeof(tmp));
	
	passlen=sizeof(KEY2) - 1;
	password = (mutils_word8 *) mutils_malloc(passlen+1);
	mutils_memcpy(password, KEY2, passlen);
	
	datalen = mutils_strlen((mutils_word8 *) DATA2);
	data = (mutils_word8 *) mutils_malloc(datalen+1);
	mutils_strcpy(data, (mutils_word8 *) DATA2);

	td = mhash_hmac_init(MHASH_MD5, password, passlen,
			    mhash_get_hash_pblock(MHASH_MD5));

	mhash(td, data, datalen);
	mac = mhash_hmac_end(td);

	tmp = mutils_asciify(mac, mhash_get_block_size(MHASH_MD5));
	
	result = mutils_strcmp((mutils_word8 *) DIGEST2, tmp);

	mutils_free(password);
	mutils_free(data);

	if (result != 0)
	{
		fprintf(stderr, "HMAC-Test: Failed\n");
		fprintf(stderr, "Expecting: 0x%s\nGot: 0x%s\n", DIGEST2, tmp);
		return(MUTILS_INVALID_RESULT);
	}

	fprintf(stderr, "MD5 HMAC-Test: Ok\n");

	mutils_free(tmp);

	return(MUTILS_OK);
}
示例#5
0
/*
  Restores the state of a hashing algorithm that was saved
  using mhash_save_state(). Use like mhash_init.
*/
WIN32DLL_DEFINE MHASH mhash_restore_state_mem(void* _mem)
{
    mutils_word8 *mem = _mem;
    hashid algorithm_given;
    MHASH ret = MHASH_FAILED;
    mutils_word32 pos;

    if (mem==NULL)
    {
        return(ret);
    }

    mutils_memcpy( &algorithm_given, mem, sizeof(algorithm_given));

    if ((ret = mhash_init(algorithm_given)) == MHASH_FAILED)
    {
        return(ret);
    }

    ret->algorithm_given = algorithm_given;

    pos = sizeof(algorithm_given);

    mutils_memcpy( &ret->hmac_key_size, &mem[pos], sizeof(ret->hmac_key_size));
    pos += sizeof( ret->hmac_key_size);

    mutils_memcpy( &ret->hmac_block, &mem[pos], sizeof(ret->hmac_block));
    pos += sizeof(ret->hmac_block);

    if (ret->hmac_key_size != 0)
    {
        ret->hmac_key = mutils_malloc(ret->hmac_key_size);
        if (ret->hmac_key == NULL)
        {
            goto freeall;
        }

        mutils_memcpy( ret->hmac_key, &mem[pos], ret->hmac_key_size);
        pos += sizeof(ret->hmac_key_size);
    }

    mutils_memcpy( &ret->state_size, &mem[pos], sizeof(ret->state_size));
    pos += sizeof( ret->state_size);

    ret->state = mutils_malloc(ret->state_size);
    if (ret->state==NULL)
        goto freeall;

    mutils_memcpy( ret->state, &mem[pos], ret->state_size);
    pos += ret->state_size;

    ret->hash_func = _mhash_get_hash_func( algorithm_given);
    ret->deinit_func = _mhash_get_deinit_func( algorithm_given);
    ret->final_func = _mhash_get_final_func( algorithm_given);

    return(ret);

freeall:
    /* This uses too much internals
     */
    mutils_free(ret->state);
    mutils_free(ret->hmac_key);
    mutils_free(ret);
    return(MHASH_FAILED);
}
示例#6
0
WIN32DLL_DEFINE void mhash_free(void *ptr)
{
    mutils_free(ptr);
}
示例#7
0
WIN32DLL_DEFINE
MHASH mhash_hmac_init(__const hashid type,
                      void *key, mutils_word32 keysize,
                      mutils_word32 block)
{
    MHASH ret = MHASH_FAILED;
    MHASH tmptd;
    mutils_word8 *ipad;
    mutils_word8 _ipad[MAX_BLOCK_SIZE];
    mutils_word32 i;
    mutils_boolean ipad_alloc = MUTILS_FALSE;
    mutils_boolean res;

    if (block == 0)
    {
        block = 64;	/* the default for ripemd,md5,sha-1 */
    }

    ret = mhash_init_int(type);

    if (ret != MHASH_FAILED)
    {
        /* Initial hmac calculations */
        ret->hmac_block = block;

        if ( ret->hmac_block > MAX_BLOCK_SIZE)
        {
            ipad = mutils_malloc(ret->hmac_block);
            if (ipad == NULL)
            {
                return MHASH_FAILED;
            }
            ipad_alloc = MUTILS_TRUE;
        }
        else
        {
            ipad = _ipad;
        }

        if (keysize > ret->hmac_block)
        {
            tmptd = mhash_init(type);
            mhash(tmptd, key, keysize);
            ret->hmac_key_size = mhash_get_block_size(type);
            ret->hmac_key = mhash_end(tmptd);
        }
        else
        {
            ret->hmac_key = mutils_malloc(ret->hmac_block);
            mutils_bzero(ret->hmac_key, ret->hmac_block);
            mutils_memcpy(ret->hmac_key, key, keysize);
            ret->hmac_key_size = ret->hmac_block;
        }

        /* IPAD */

        for (i = 0; i < ret->hmac_key_size; i++)
        {
            ipad[i] = (0x36) ^ ret->hmac_key[i];
        }

        for (; i < ret->hmac_block; i++)
        {
            ipad[i] = (0x36);
        }

        res = mhash(ret, ipad, ret->hmac_block);

        if (ipad_alloc == MUTILS_TRUE)
        {
            mutils_free(ipad);
        }
    }

    return(ret);
}
示例#8
0
WIN32DLL_DEFINE
mutils_error mhash_hmac_deinit(MHASH td, void *result)
{
    mutils_word8 *opad;
    mutils_word8 _opad[MAX_BLOCK_SIZE];
    MHASH tmptd;
    mutils_word32 i;
    mutils_word32 opad_alloc = 0;

    if (td->hmac_block > MAX_BLOCK_SIZE)
    {
        opad = mutils_malloc(td->hmac_block);
        if (opad == NULL)
        {
            return(-MUTILS_SYSTEM_RESOURCE_ERROR);
        }
        opad_alloc = 1;
    }
    else
    {
        opad = _opad;
    }


    for (i = 0; i < td->hmac_key_size; i++)
    {
        opad[i] = (0x5C) ^ td->hmac_key[i];
    }

    for (; i < td->hmac_block; i++)
    {
        opad[i] = (0x5C);
    }

    tmptd = mhash_init(td->algorithm_given);
    mhash(tmptd, opad, td->hmac_block);

    if (td->final_func != NULL)
    {
        td->final_func(td->state);
    }

    if (td->deinit_func != NULL)
    {
        td->deinit_func(td->state, result);
    }

    if (result != NULL)
    {
        mhash(tmptd, result,
              mhash_get_block_size(td->algorithm_given));
    }

    mutils_free(td->state);

    if (opad_alloc!=0)
    {
        mutils_free(opad);
    }

    mutils_bzero(td->hmac_key, td->hmac_key_size);
    mutils_free(td->hmac_key);
    mutils_free(td);

    mhash_deinit(tmptd, result);

    return(MUTILS_OK);
}