コード例 #1
0
ファイル: sha512_sha384.c プロジェクト: Distrotech/mhash
void
sha512_sha384_update(struct sha512_sha384_ctx *ctx, __const mutils_word8 * buffer, 
		     mutils_word32 length)
{
	mutils_word32 left;

	if (ctx->index)
	{	/* Try to fill partial block */
		left = SHA512_SHA384_DATA_SIZE - ctx->index;
		if (length < left)
		{
			mutils_memcpy(ctx->block + ctx->index, buffer, length);
			ctx->index += length;
			return;	/* Finished */
		} else {
			mutils_memcpy(ctx->block + ctx->index, buffer, left);
			sha512_sha384_block(ctx, ctx->block);
			buffer += left;
			length -= left;
		}
	}
	while (length >= SHA512_SHA384_DATA_SIZE)
	{
		sha512_sha384_block(ctx, buffer);
		buffer += SHA512_SHA384_DATA_SIZE;
		length -= SHA512_SHA384_DATA_SIZE;
	}
/* Buffer leftovers */
/* NOTE: The corresponding sha1 code checks for the special case length == 0.
 * That seems supoptimal, as I suspect it increases the number of branches. */

	mutils_memcpy(ctx->block, buffer, length);
	ctx->index = length;
}
コード例 #2
0
ファイル: keygen_hex.c プロジェクト: Distrotech/mhash
mutils_error _mhash_gen_key_hex(void *keyword, mutils_word32 key_size, 
				mutils_word8 *password, mutils_word32 plen)
{
	mutils_word8 *chain = password;
	mutils_word8 *pkeyword = keyword;
	mutils_word8 tmp[3];
	mutils_word32 i;

	mutils_bzero(keyword, key_size);

	/* The chain should have 2*n characters 
	 */

	if (plen % 2 != 0 || plen > key_size*2)
		return(-MUTILS_INVALID_SIZE);

	if (check_hex(chain, plen) == MUTILS_FALSE)
		return(-MUTILS_INVALID_FORMAT);

	mutils_bzero( keyword, key_size);

	for (i = 0; i < plen; i += 2) {
		mutils_memcpy(tmp, &chain[i], 2);
		tmp[2] = '\0';
		pkeyword[i / 2] = mutils_strtol(tmp, (mutils_word8 **) NULL, 16);
	}

	return(MUTILS_OK);
}
コード例 #3
0
ファイル: mhash.c プロジェクト: muhkuh-sys/muhkuh_old
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;
}
コード例 #4
0
ファイル: sha512_sha384.c プロジェクト: Distrotech/mhash
void sha384_init(struct sha512_sha384_ctx *ctx)
{
  /* Initial values */
	static __const mutils_word64 H0[_SHA512_SHA384_STATE_LENGTH] = {
	  0xcbbb9d5dc1059ed8ULL, 0x629a292a367cd507ULL, 0x9159015a3070dd17ULL,
	  0x152fecd8f70e5939ULL, 0x67332667ffc00b31ULL, 0x8eb44a8768581511ULL,
	  0xdb0c2e0d64f98fa7ULL, 0x47b5481dbefa4fa4ULL,
	};

	mutils_memcpy(ctx->state, H0, sizeof(H0));

	/* Initialize bit count */
	ctx->bitcount_low = ctx->bitcount_high = 0;

	/* Initialize buffer */
	ctx->index = 0;
}
コード例 #5
0
ファイル: sha512_sha384.c プロジェクト: Distrotech/mhash
void sha512_init(struct sha512_sha384_ctx *ctx)
{
  /* Initial values */
	static __const mutils_word64 H0[_SHA512_SHA384_STATE_LENGTH] = {
	  0x6a09e667f3bcc908ULL, 0xbb67ae8584caa73bULL, 0x3c6ef372fe94f82bULL, 
	  0xa54ff53a5f1d36f1ULL, 0x510e527fade682d1ULL, 0x9b05688c2b3e6c1fULL, 
	  0x1f83d9abfb41bd6bULL, 0x5be0cd19137e2179ULL
	};

	mutils_memcpy(ctx->state, H0, sizeof(H0));

	/* Initialize bit count */
	ctx->bitcount_low = ctx->bitcount_high = 0;

	/* Initialize buffer */
	ctx->index = 0;
}
コード例 #6
0
ファイル: mhash.c プロジェクト: muhkuh-sys/muhkuh_old
/*
  Saves the state of a hashing algorithm such that it can be
  restored at some later point in time using
  mhash_restore_state().

  mem_size should contain the size of the given _mem pointer.
  Otherwise the required size will be copied there.

  Original version and idea by Blake Stephen <*****@*****.**>
*/
WIN32DLL_DEFINE mutils_error mhash_save_state_mem(MHASH td, void *_mem, mutils_word32 *mem_size )
{
    mutils_word32 tot_size;
    mutils_word32 pos;
    mutils_word8 *mem = _mem;

    tot_size = sizeof(td->algorithm_given) + sizeof(td->hmac_key_size)
               + sizeof(td->hmac_block) + td->hmac_key_size +
               + sizeof(td->state_size) + td->state_size;

    if ( *mem_size < tot_size)
    {
        *mem_size = tot_size;
        return(MUTILS_INVALID_INPUT_BUFFER);
    }

    if ( mem != NULL)
    {
        pos = 0;
        mutils_memcpy( mem, &td->algorithm_given, sizeof(td->algorithm_given));
        pos = sizeof( td->algorithm_given);

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

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

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

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

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

    }
    return(MUTILS_OK);
}
コード例 #7
0
ファイル: hmac_test.c プロジェクト: Distrotech/mhash
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);
}
コード例 #8
0
ファイル: mhash.c プロジェクト: muhkuh-sys/muhkuh_old
/*
  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);
}
コード例 #9
0
ファイル: mhash.c プロジェクト: muhkuh-sys/muhkuh_old
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);
}