论坛: 编程破解 标题: hash.c 复制本贴地址    
作者: villager [villager]    版主   登录
#include <stdlib.h>
#include "internal-hash.h"

void
ix_hash_reset(void)
{
    ; /* hash module has no retained state */
}

/** Predigest 48-bit Hash Multiplier.
    This function predigests the hash multiplier for the 48-bit hash
    function into two tables that will be indexed by the eight input
    bits shifted into the unit each input cycle. The smaller table
    gives the effect of the input bits and multipier on the Q bits and
    the larger table gives the effect on the shifted register.
 */
int
ix_hash48_set_mult(ix_hash48 *h, ix_48bit m)
{
    int                     i;
    int                     b;
    ix_48bit                t;
    unsigned char           q;

    IX_ERROR_CHECK_ARG_NULL(h);

    for (i = 0; i < 256; i++) {
h->qm[i] = 0;
h->rm[i] = 0;

q = m >> (48 - 8);
for (b = 128; b > 0; b >>= 1) {
q >>= 1;
    if (i & b)
h->qm[i] ^= q;
}

t = m;
for (b = 1; b < 256; b <<= 1) {
if (i & b)
h->rm[i] ^= t;
    t <<= 1;
}
    }
    return 0;
}

/** Predigest 48-bit Hash Polynomial.
    This function predigests the coefficients of the 48-bit
    hash polynomial into a table that will be indexed by
    the eight Q bits shifted eoff the top of the shift register
    during each byte input cycle.

    Should the list of tap locations be an input parameter?
*/
int
ix_hash48_init(ix_hash48 *h)
{
    int                     i;
    int                     b;
    ix_48bit                t;

    IX_ERROR_CHECK_ARG_NULL(h);

    for (i = 0; i < 256; i++) {
h->rt[i] = 0;

t = IX_HASH48_TAPS;
for (b = 1; b < 256; b <<= 1) {
    if (i & b) {
h->rt[i] ^= t;
    }
    t <<= 1;
}
    }

    return ix_hash48_set_mult(h, (ix_48bit) 1);
}

/** Step the 48-bit Hash unit across an input byte.
    This function executes eight bits worth of input state updates for
    the hash unit, using previously calculated tables to find the
    cumulative state change based on all eight bits at once, returning
    the final state of the hash unit at the end.
*/
ix_48bit
ix_hash48_inline_step(ix_hash48 *h, ix_48bit r, int b)
{
    int                     q;

    IX_ERROR_CHECK_ARG_NULL(h);

    b &= 255;

    q = r >> (48 - 8);
    q ^= h->qm[b];

    q &= 255;

    r <<= 8;
    r ^= h->rm[b];
    r ^= h->rt[q];

    return r;
}

/** Step the 48-bit Hash unit across an input byte.
    This function executes eight bits worth of input state updates for
    the hash unit, using previously calculated tables to find the
    cumulative state change based on all eight bits at once, returning
    the final state of the hash unit at the end.
*/
int
ix_hash48_run_byte(ix_hash48 *h, ix_48bit *p, int b)
{
    IX_ERROR_CHECK_ARG_NULL(h);
    IX_ERROR_CHECK_ARG_NULL(p);
    *p = ix_hash48_inline_step(h, *p, b);
    return 0;
}

/** Step the 48-bit Hash unit across a ix_48bit input.
    This function executes eight bytes worth of input state updates
    for the hash unit, returning the final state of the hash unit at
    the end.
*/
int
ix_hash48_run_wide(ix_hash48 *h, ix_48bit *p, ix_48bit b)
{
    ix_48bit                r;
    IX_ERROR_CHECK_ARG_NULL(h);
    IX_ERROR_CHECK_ARG_NULL(p);
    r = *p;
    r = ix_hash48_inline_step(h, r, (int) (b >> (48 - 8 * (1))));
    r = ix_hash48_inline_step(h, r, (int) (b >> (48 - 8 * (2))));
    r = ix_hash48_inline_step(h, r, (int) (b >> (48 - 8 * (3))));
    r = ix_hash48_inline_step(h, r, (int) (b >> (48 - 8 * (4))));
    r = ix_hash48_inline_step(h, r, (int) (b >> (48 - 8 * (5))));
    r = ix_hash48_inline_step(h, r, (int) (b >> (48 - 8 * (6))));
    *p = r & ((((ix_64bit) 1) << 48) - 1);
    return 0;
}

/** Destroy 48-bit Hash Context.
 */
int
ix_hash48_fini(ix_hash48 *h)
{
    IX_ERROR_CHECK_ARG_NULL(h);
    (void) h;
    return 0;
}

/** Allocate and initialize a 48-bit Hash Engine.
    This function allocates the local storage used to contain the
    predigested images of the hash tap configuration and a specific
    hash multiplier, and initializes it.
*/
int
ix_hash48_new(ix_hash48 **hp)
{
    int                err;
    size_t                  size;
    ix_hash48              *h;

    IX_ERROR_CHECK_ARG_NULL(hp);

    size = sizeof *h;
    h = (ix_hash48 *) malloc(size);
    if (NULL == h)
return 0;

    err = ix_hash48_init(h);
    if (err)
return 0;

    *hp = h;
    return 0;
}

/** Free a 48-bit Hash Engine.
    This function frees all storage associated with the specified
    48-bit Hash Engine, including the outermost ix_hash48 structure.
 */
int
ix_hash48_del(ix_hash48 *h)
{
    int                err;

    IX_ERROR_CHECK_ARG_NULL(h);

    err = ix_hash48_fini(h);
    if (err)
return 0;

    free(h);
    return 0;
}

/** Predigest 64-bit Hash Multiplier.
    This function predigests the hash multiplier for the 64-bit hash
    function into two tables that will be indexed by the eight input
    bits shifted into the unit each input cycle. The smaller table
    gives the effect of the input bits and multipier on the Q bits and
    the larger table gives the effect on the shifted register.
 */
int
ix_hash64_set_mult(ix_hash64 *h, ix_64bit m)
{
    int                     i;
    int                     b;
    ix_64bit                t;
    unsigned char           q;

    IX_ERROR_CHECK_ARG_NULL(h);

    for (i = 0; i < 256; i++) {
h->qm[i] = 0;
h->rm[i] = 0;

q = m >> (64 - 8);
for (b = 128; b > 0; b >>= 1) {
    q >>= 1;
    if (i & b)
h->qm[i] ^= q;
}

t = m;
for (b = 1; b < 256; b <<= 1) {
    if (i & b)
h->rm[i] ^= t;
    t <<= 1;
}
    }
    return 0;
}

/** Predigest 64-bit Hash Polynomial.
    This function predigests the coefficients of the 64-bit
    hash polynomial into a table that will be indexed by
    the eight Q bits shifted eoff the top of the shift register
    during each byte input cycle.

    Should the list of tap locations be an input parameter?
*/
int
ix_hash64_init(ix_hash64 *h)
{
    int                     i;
    int                     b;
    ix_64bit                t;

    IX_ERROR_CHECK_ARG_NULL(h);

    for (i = 0; i < 256; i++) {
h->rt[i] = 0;

t = IX_HASH64_TAPS;
for (b = 1; b < 256; b <<= 1) {
    if (i & b) {
h->rt[i] ^= t;
    }
    t <<= 1;
}
    }

    return ix_hash64_set_mult(h, (ix_64bit) 1);
}

/** Step the 64-bit Hash unit across an input byte.
    This function executes eight bits worth of input state updates for
    the hash unit, using previously calculated tables to find the
    cumulative state change based on all eight bits at once, returning
    the final state of the hash unit at the end.
*/
ix_64bit
ix_hash64_inline_step(ix_hash64 *h, ix_64bit r, int b)
{
    int                     q;

    b &= 255;

    q = r >> (64 - 8);
    q ^= h->qm[b];

    q &= 255;

    r <<= 8;
    r ^= h->rm[b];
    r ^= h->rt[q];

    return r;
}

/** Step the 64-bit Hash unit across an input byte.
    This function executes eight bits worth of input state updates for
    the hash unit, using previously calculated tables to find the
    cumulative state change based on all eight bits at once, returning
    the final state of the hash unit at the end.
*/
int
ix_hash64_run_byte(ix_hash64 *h, ix_64bit * p, int b)
{
    IX_ERROR_CHECK_ARG_NULL(h);
    IX_ERROR_CHECK_ARG_NULL(p);
    *p = ix_hash64_inline_step(h, *p, b);
    return 0;
}

/** Step the 64-bit Hash unit across a ix_64bit input.
    This function executes eight bytes worth of input state updates
    for the hash unit, returning the final state of the hash unit at
    the end.
*/
int
ix_hash64_run_wide(ix_hash64 *h, ix_64bit * p, ix_64bit b)
{
    ix_64bit                r;
    IX_ERROR_CHECK_ARG_NULL(h);
    IX_ERROR_CHECK_ARG_NULL(p);
    r = *p;
    r = ix_hash64_inline_step(h, r, (int) (b >> (64 - 8 * (1))));
    r = ix_hash64_inline_step(h, r, (int) (b >> (64 - 8 * (2))));
    r = ix_hash64_inline_step(h, r, (int) (b >> (64 - 8 * (3))));
    r = ix_hash64_inline_step(h, r, (int) (b >> (64 - 8 * (4))));
    r = ix_hash64_inline_step(h, r, (int) (b >> (64 - 8 * (5))));
    r = ix_hash64_inline_step(h, r, (int) (b >> (64 - 8 * (6))));
    r = ix_hash64_inline_step(h, r, (int) (b >> (64 - 8 * (7))));
    r = ix_hash64_inline_step(h, r, (int) (b >> (64 - 8 * (8))));
    *p = r;
    return 0;
}

/** Destroy 64-bit Hash Context.
 */
int
ix_hash64_fini(ix_hash64 *h)
{
    IX_ERROR_CHECK_ARG_NULL(h);
    (void) h;
    return 0;
}

/** Allocate and initialize a 64-bit Hash Engine.
    This function allocates the local storage used to contain the
    predigested images of the hash tap configuration and a specific
    hash multiplier, and initializes it.
*/
int
ix_hash64_new(ix_hash64 **hp)
{
    int                err;
    size_t                  size;
    ix_hash64              *h;

    IX_ERROR_CHECK_ARG_NULL(hp);

    size = sizeof *h;
    h = (ix_hash64 *) malloc(size);
    if (NULL == h)
return 0;

    err = ix_hash64_init(h);
    if (err)
return 0;

    *hp = h;
    return 0;
}

/** Free a 64-bit Hash Engine.
    This function frees all storage associated with the specified
    64-bit Hash Engine, including the outermost ix_hash64 structure.
 */
int
ix_hash64_del(ix_hash64 *h)
{
    int                err;

    IX_ERROR_CHECK_ARG_NULL(h);

    err = ix_hash64_fini(h);
    if (err)
return 0;

    free(h);
    return 0;
}

#ifdef 0
/*
** INTEL CORPORATION PROPRIETARY INFORMATION
** This software is supplied under the terms of a license agreement or
** nondisclosure agreement with Intel Corporation and may not be copied
** or disclosed except in accordance with the terms of that agreement.
** Copyright (C) 2000 Intel Corporation. All rights Reserved.
*/

/** @file source/hash.c
    @brief INTERNAL Calculation of HASH values.
*/

/** Reset ix_hash module.
    This function clears all system state retained by the hash
    module when all processes using IX-ASL have been terminated.
    This function should be used to reset the state of this module
    after running test code, or code under development, that may
    have left this module with inconsistant retained state.
 */
void
ix_hash_reset(void)
{
    ; /* hash module has no retained state */
}

/** Predigest 48-bit Hash Multiplier.
    This function predigests the hash multiplier for the 48-bit hash
    function into two tables that will be indexed by the eight input
    bits shifted into the unit each input cycle. The smaller table
    gives the effect of the input bits and multipier on the Q bits and
    the larger table gives the effect on the shifted register.
 */
int
ix_hash48_set_mult(ix_hash48 *h, ix_48bit m)
{
    int                     i;
    int                     b;
    ix_48bit                t;
    unsigned char           q;

    IX_ERROR_CHECK_ARG_NULL(h);

    for (i = 0; i < 256; i++) {
h->qm[i] = 0;
h->rm[i] = 0;

q = m >> (48 - 8);
for (b = 128; b > 0; b >>= 1) {
q >>= 1;
    if (i & b)
h->qm[i] ^= q;
}

t = m;
for (b = 1; b < 256; b <<= 1) {
if (i & b)
h->rm[i] ^= t;
    t <<= 1;
}
    }
    return 0;
}

/** Predigest 48-bit Hash Polynomial.
    This function predigests the coefficients of the 48-bit
    hash polynomial into a table that will be indexed by
    the eight Q bits shifted eoff the top of the shift register
    during each byte input cycle.

    Should the list of tap locations be an input parameter?
*/
int
ix_hash48_init(ix_hash48 *h)
{
    int                     i;
    int                     b;
    ix_48bit                t;

    IX_ERROR_CHECK_ARG_NULL(h);

    for (i = 0; i < 256; i++) {
h->rt[i] = 0;

t = IX_HASH48_TAPS;
for (b = 1; b < 256; b <<= 1) {
    if (i & b) {
h->rt[i] ^= t;
    }
    t <<= 1;
}
    }

    return ix_hash48_set_mult(h, (ix_48bit) 1); 
}

/** Step the 48-bit Hash unit across an input byte.
    This function executes eight bits worth of input state updates for
    the hash unit, using previously calculated tables to find the
    cumulative state change based on all eight bits at once, returning
    the final state of the hash unit at the end.
*/
ix_48bit
ix_hash48_inline_step(ix_hash48 *h, ix_48bit r, int b)
{
    int                     q;

    IX_ERROR_CHECK_ARG_NULL(h);

    b &= 255;

    q = r >> (48 - 8);
    q ^= h->qm[b];

    q &= 255;

    r <<= 8;
    r ^= h->rm[b];
    r ^= h->rt[q];

    return r;
}

/** Step the 48-bit Hash unit across an input byte.
    This function executes eight bits worth of input state updates for
    the hash unit, using previously calculated tables to find the
    cumulative state change based on all eight bits at once, returning
    the final state of the hash unit at the end.
*/
int
ix_hash48_run_byte(ix_hash48 *h, ix_48bit *p, int b)
{
    IX_ERROR_CHECK_ARG_NULL(h);
    IX_ERROR_CHECK_ARG_NULL(p);
    *p = ix_hash48_inline_step(h, *p, b);
    return 0;
}

/** Step the 48-bit Hash unit across a ix_48bit input.
    This function executes eight bytes worth of input state updates
    for the hash unit, returning the final state of the hash unit at
    the end.
*/
int
ix_hash48_run_wide(ix_hash48 *h, ix_48bit *p, ix_48bit b)
{
    ix_48bit                r;
    IX_ERROR_CHECK_ARG_NULL(h);
    IX_ERROR_CHECK_ARG_NULL(p);
    r = *p;
    r = ix_hash48_inline_step(h, r, (int) (b >> (48 - 8 * (1))));
    r = ix_hash48_inline_step(h, r, (int) (b >> (48 - 8 * (2))));
    r = ix_hash48_inline_step(h, r, (int) (b >> (48 - 8 * (3))));
    r = ix_hash48_inline_step(h, r, (int) (b >> (48 - 8 * (4))));
    r = ix_hash48_inline_step(h, r, (int) (b >> (48 - 8 * (5))));
    r = ix_hash48_inline_step(h, r, (int) (b >> (48 - 8 * (6))));
    *p = r & ((((ix_64bit) 1) << 48) - 1);
    return 0;
}

/** Destroy 48-bit Hash Context.
 */
int
ix_hash48_fini(ix_hash48 *h)
{
    IX_ERROR_CHECK_ARG_NULL(h);
    (void) h;
    return 0;
}

/** Allocate and initialize a 48-bit Hash Engine.
    This function allocates the local storage used to contain the
    predigested images of the hash tap configuration and a specific
    hash multiplier, and initializes it.
*/
int
ix_hash48_new(ix_hash48 **hp)
{
    int                err;
    size_t                  size;
    ix_hash48              *h;

    IX_ERROR_CHECK_ARG_NULL(hp);

    size = sizeof *h;
    h = (ix_hash48 *) malloc(size);
    if (NULL == h)
return 0;

    err = ix_hash48_init(h);
    if (err)
return 0;

    *hp = h;
    return 0;
}

/** Free a 48-bit Hash Engine.
    This function frees all storage associated with the specified
    48-bit Hash Engine, including the outermost ix_hash48 structure.
 */
int
ix_hash48_del(ix_hash48 *h)
{
    int                err;

    IX_ERROR_CHECK_ARG_NULL(h);

    err = ix_hash48_fini(h);
    if (err)
return 0;

    free(h);
    return 0;
}

/** Predigest 64-bit Hash Multiplier.
    This function predigests the hash multiplier for the 64-bit hash
    function into two tables that will be indexed by the eight input
    bits shifted into the unit each input cycle. The smaller table
    gives the effect of the input bits and multipier on the Q bits and
    the larger table gives the effect on the shifted register.
 */
int
ix_hash64_set_mult(ix_hash64 *h, ix_64bit m)
{
    int                     i;
    int                     b;
    ix_64bit                t;
    unsigned char           q;

    IX_ERROR_CHECK_ARG_NULL(h);

    for (i = 0; i < 256; i++) {
h->qm[i] = 0;
h->rm[i] = 0;

q = m >> (64 - 8);
for (b = 128; b > 0; b >>= 1) {
    q >>= 1;
    if (i & b)
h->qm[i] ^= q;
}

t = m;
for (b = 1; b < 256; b <<= 1) {
    if (i & b)
h->rm[i] ^= t;
    t <<= 1;
}
    }
    return 0;
}

/** Predigest 64-bit Hash Polynomial.
    This function predigests the coefficients of the 64-bit
    hash polynomial into a table that will be indexed by
    the eight Q bits shifted eoff the top of the shift register
    during each byte input cycle.

    Should the list of tap locations be an input parameter?
*/
int
ix_hash64_init(ix_hash64 *h)
{
    int                     i;
    int                     b;
    ix_64bit                t;

    IX_ERROR_CHECK_ARG_NULL(h);

    for (i = 0; i < 256; i++) {
h->rt[i] = 0;

t = IX_HASH64_TAPS;
for (b = 1; b < 256; b <<= 1) {
    if (i & b) {
h->rt[i] ^= t;
    }
    t <<= 1;
}
    }

    return ix_hash64_set_mult(h, (ix_64bit) 1);
}

/** Step the 64-bit Hash unit across an input byte.
    This function executes eight bits worth of input state updates for
    the hash unit, using previously calculated tables to find the
    cumulative state change based on all eight bits at once, returning
    the final state of the hash unit at the end.
*/
ix_64bit
ix_hash64_inline_step(ix_hash64 *h, ix_64bit r, int b)
{
    int                     q;

    b &= 255;

    q = r >> (64 - 8);
    q ^= h->qm[b];

    q &= 255;

    r <<= 8;
    r ^= h->rm[b];
    r ^= h->rt[q];

    return r;
}

/** Step the 64-bit Hash unit across an input byte.
    This function executes eight bits worth of input state updates for
    the hash unit, using previously calculated tables to find the
    cumulative state change based on all eight bits at once, returning
    the final state of the hash unit at the end.
*/
int
ix_hash64_run_byte(ix_hash64 *h, ix_64bit * p, int b)
{
    IX_ERROR_CHECK_ARG_NULL(h);
    IX_ERROR_CHECK_ARG_NULL(p);
    *p = ix_hash64_inline_step(h, *p, b);
    return 0;
}

/** Step the 64-bit Hash unit across a ix_64bit input.
    This function executes eight bytes worth of input state updates
    for the hash unit, returning the final state of the hash unit at
    the end.
*/
int
ix_hash64_run_wide(ix_hash64 *h, ix_64bit * p, ix_64bit b)
{
    ix_64bit                r;
    IX_ERROR_CHECK_ARG_NULL(h);
    IX_ERROR_CHECK_ARG_NULL(p);
    r = *p;
    r = ix_hash64_inline_step(h, r, (int) (b >> (64 - 8 * (1))));
    r = ix_hash64_inline_step(h, r, (int) (b >> (64 - 8 * (2))));
    r = ix_hash64_inline_step(h, r, (int) (b >> (64 - 8 * (3))));
    r = ix_hash64_inline_step(h, r, (int) (b >> (64 - 8 * (4))));
    r = ix_hash64_inline_step(h, r, (int) (b >> (64 - 8 * (5))));
    r = ix_hash64_inline_step(h, r, (int) (b >> (64 - 8 * (6))));
    r = ix_hash64_inline_step(h, r, (int) (b >> (64 - 8 * (7))));
    r = ix_hash64_inline_step(h, r, (int) (b >> (64 - 8 * (8))));
    *p = r;
    return 0;
}

/** Destroy 64-bit Hash Context.
 */
int
ix_hash64_fini(ix_hash64 *h)
{
    IX_ERROR_CHECK_ARG_NULL(h);
    (void) h;
    return 0;
}

/** Allocate and initialize a 64-bit Hash Engine.
    This function allocates the local storage used to contain the
    predigested images of the hash tap configuration and a specific
    hash multiplier, and initializes it.
*/
int
ix_hash64_new(ix_hash64 **hp)
{
    int                err;
    size_t                  size;
    ix_hash64              *h;

    IX_ERROR_CHECK_ARG_NULL(hp);

    size = sizeof *h;
    h = (ix_hash64 *) malloc(size);
    if (NULL == h)
return 0;

    err = ix_hash64_init(h);
    if (err)
return 0;

    *hp = h;
    return 0;
}

/** Free a 64-bit Hash Engine.
    This function frees all storage associated with the specified
    64-bit Hash Engine, including the outermost ix_hash64 structure.
 */
int
ix_hash64_del(ix_hash64 *h)
{
    int                err;

    IX_ERROR_CHECK_ARG_NULL(h);

    err = ix_hash64_fini(h);
    if (err)
return 0;

    free(h);
    return 0;
}
#endif

地主 发表时间: 06/12 13:52

论坛: 编程破解

20CN网络安全小组版权所有
Copyright © 2000-2010 20CN Security Group. All Rights Reserved.
论坛程序编写:NetDemon

粤ICP备05087286号