⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 haval.cpp

📁 A console-based hah calculators
💻 CPP
📖 第 1 页 / 共 3 页
字号:
/* Modified version of the HAVAL hash algorithm implementation
 * by Calyptix Security Corporation.
 * Use freely.
 *
 * Changes to the original version:
 * - Renamed some function and constants
 * - Added new include file to the haval.h header file
 * - Removed config.h from include directives
 */

/*  $Id: haval.c,v 1.8 2003/01/20 05:44:48 lteo Exp $ */

/*
 *  haval.c:  specifies the routines in the HAVAL (V.1) hashing library.
 *
 *  Copyright (c) 2003 Calyptix Security Corporation
 *  All rights reserved.
 *
 *  This code is derived from software contributed to Calyptix Security
 *  Corporation by Yuliang Zheng.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions
 *  are met:
 *  1. Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *  2. Redistributions in binary form must reproduce the above
 *     copyright notice, this list of conditions and the following
 *     disclaimer in the documentation and/or other materials provided
 *     with the distribution.
 *  3. Neither the name of Calyptix Security Corporation nor the
 *     names of its contributors may be used to endorse or promote
 *     products derived from this software without specific prior
 *     written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 * -------------------------------------------------------------------
 *
 *      HAVAL is a one-way hashing algorithm with the following
 *      collision-resistant property:
 *             It is computationally infeasible to find two or more
 *             messages that are hashed into the same fingerprint.
 *      
 *  Reference:
 *       Y. Zheng, J. Pieprzyk and J. Seberry:
 *       ``HAVAL --- a one-way hashing algorithm with variable
 *       length of output'', Advances in Cryptology --- AUSCRYPT'92,
 *       Lecture Notes in Computer Science,  Vol.718, pp.83-104, 
 *       Springer-Verlag, 1993.
 *
 *  Descriptions:
 *      -  haval_string:      hash a string
 *      -  haval_file:        hash a file
 *      -  haval_stdin:       filter -- hash input from the stdin device
 *      -  haval_hash:        hash a string of specified length
 *                            (Haval_hash is used in conjunction with
 *                             haval_start & haval_end.)
 *      -  haval_hash_block:  hash a 32-word block
 *      -  haval_start:       initialization
 *      -  haval_end:         finalization
 *
 *  Authors:    Yuliang Zheng and Lawrence Teo
 *              Calyptix Security Corporation
 *              P.O. Box 561508, Charlotte, NC 28213, USA
 *              Email: info@calyptix.com
 *              URL:   http://www.calyptix.com/
 *              Voice: +1 704 806 8635
 *
 *  For a list of changes, see the ChangeLog file.
 */

static char rcsid[] = "$Id: haval.c,v 1.8 2003/01/20 05:44:48 lteo Exp $";

#include <stdio.h>
#include <string.h>
/* #include "havalapp.h" */
/* #include "config.h" */
#include "haval.h"
#include "../rhsyscfg.h"

#define HAVAL_VERSION    1                   /* current version number */

void haval_string (char *, unsigned char *); /* hash a string */
int  haval_file (char *, unsigned char *);   /* hash a file */
void haval_stdin (void);                     /* hash input from stdin */
void haval_start (haval_state *);            /* initialization */
void haval_hash (haval_state *,
        unsigned char *, unsigned int);      /* updating routine */
void haval_end (haval_state *, unsigned char *); /* finalization */
void haval_hash_block (haval_state *);       /* hash a 32-word block */
static void haval_tailor (haval_state *);    /* folding the last output */

static unsigned char haval_padding[128] = {        /* constants for haval_padding */
0x01, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};

#define haval_f_1(x6, x5, x4, x3, x2, x1, x0)          \
           ((x1) & ((x0) ^ (x4)) ^ (x2) & (x5) ^ \
            (x3) & (x6) ^ (x0))

#define haval_f_2(x6, x5, x4, x3, x2, x1, x0)                         \
           ((x2) & ((x1) & ~(x3) ^ (x4) & (x5) ^ (x6) ^ (x0)) ^ \
            (x4) & ((x1) ^ (x5)) ^ (x3) & (x5) ^ (x0)) 

#define haval_f_3(x6, x5, x4, x3, x2, x1, x0)          \
           ((x3) & ((x1) & (x2) ^ (x6) ^ (x0)) ^ \
            (x1) & (x4) ^ (x2) & (x5) ^ (x0))

#define haval_f_4(x6, x5, x4, x3, x2, x1, x0)                                 \
           ((x4) & ((x5) & ~(x2) ^ (x3) & ~(x6) ^ (x1) ^ (x6) ^ (x0)) ^ \
            (x3) & ((x1) & (x2) ^ (x5) ^ (x6)) ^                        \
            (x2) & (x6) ^ (x0))

#define haval_f_5(x6, x5, x4, x3, x2, x1, x0)             \
           ((x0) & ((x1) & (x2) & (x3) ^ ~(x5)) ^   \
            (x1) & (x4) ^ (x2) & (x5) ^ (x3) & (x6))

/*
 * Permutations phi_{i,j}, i=3,4,5, j=1,...,i.
 *
 * HAVAL_PASS = 3:
 *               6 5 4 3 2 1 0
 *               | | | | | | | (replaced by)
 *  phi_{3,1}:   1 0 3 5 6 2 4
 *  phi_{3,2}:   4 2 1 0 5 3 6
 *  phi_{3,3}:   6 1 2 3 4 5 0
 *
 * HAVAL_PASS = 4:
 *               6 5 4 3 2 1 0
 *               | | | | | | | (replaced by)
 *  phi_{4,1}:   2 6 1 4 5 3 0
 *  phi_{4,2}:   3 5 2 0 1 6 4
 *  phi_{4,3}:   1 4 3 6 0 2 5
 *  phi_{4,4}:   6 4 0 5 2 1 3
 *
 * HAVAL_PASS = 5:
 *               6 5 4 3 2 1 0
 *               | | | | | | | (replaced by)
 *  phi_{5,1}:   3 4 1 0 5 2 6
 *  phi_{5,2}:   6 2 1 0 3 4 5
 *  phi_{5,3}:   2 6 0 4 3 1 5
 *  phi_{5,4}:   1 5 3 2 0 4 6
 *  phi_{5,5}:   2 5 0 6 4 3 1
 */

#if   HAVAL_PASS == 3
#define haval_Fphi_1(x6, x5, x4, x3, x2, x1, x0) \
           haval_f_1(x1, x0, x3, x5, x6, x2, x4)
#elif HAVAL_PASS == 4
#define haval_Fphi_1(x6, x5, x4, x3, x2, x1, x0) \
           haval_f_1(x2, x6, x1, x4, x5, x3, x0)
#else 
#define haval_Fphi_1(x6, x5, x4, x3, x2, x1, x0) \
           haval_f_1(x3, x4, x1, x0, x5, x2, x6)
#endif

#if   HAVAL_PASS == 3
#define haval_Fphi_2(x6, x5, x4, x3, x2, x1, x0) \
           haval_f_2(x4, x2, x1, x0, x5, x3, x6)
#elif HAVAL_PASS == 4
#define haval_Fphi_2(x6, x5, x4, x3, x2, x1, x0) \
           haval_f_2(x3, x5, x2, x0, x1, x6, x4)
#else 
#define haval_Fphi_2(x6, x5, x4, x3, x2, x1, x0) \
           haval_f_2(x6, x2, x1, x0, x3, x4, x5)
#endif

#if   HAVAL_PASS == 3
#define haval_Fphi_3(x6, x5, x4, x3, x2, x1, x0) \
           haval_f_3(x6, x1, x2, x3, x4, x5, x0)
#elif HAVAL_PASS == 4
#define haval_Fphi_3(x6, x5, x4, x3, x2, x1, x0) \
           haval_f_3(x1, x4, x3, x6, x0, x2, x5)
#else 
#define haval_Fphi_3(x6, x5, x4, x3, x2, x1, x0) \
           haval_f_3(x2, x6, x0, x4, x3, x1, x5)
#endif

#if   HAVAL_PASS == 4
#define haval_Fphi_4(x6, x5, x4, x3, x2, x1, x0) \
           haval_f_4(x6, x4, x0, x5, x2, x1, x3)
#else 
#define haval_Fphi_4(x6, x5, x4, x3, x2, x1, x0) \
            haval_f_4(x1, x5, x3, x2, x0, x4, x6)
#endif

#define haval_Fphi_5(x6, x5, x4, x3, x2, x1, x0) \
           haval_f_5(x2, x5, x0, x6, x4, x3, x1)

#define haval_rotate_right(x, n) (((x) >> (n)) | ((x) << (32-(n))))

#define haval_FF_1(x7, x6, x5, x4, x3, x2, x1, x0, w) {                        \
      register haval_word temp = haval_Fphi_1(x6, x5, x4, x3, x2, x1, x0);     \
      (x7) = haval_rotate_right(temp, 7) + haval_rotate_right((x7), 11) + (w);       \
      }

#define haval_FF_2(x7, x6, x5, x4, x3, x2, x1, x0, w, c) {                      \
      register haval_word temp = haval_Fphi_2(x6, x5, x4, x3, x2, x1, x0);      \
      (x7) = haval_rotate_right(temp, 7) + haval_rotate_right((x7), 11) + (w) + (c);  \
      }

#define haval_FF_3(x7, x6, x5, x4, x3, x2, x1, x0, w, c) {                      \
      register haval_word temp = haval_Fphi_3(x6, x5, x4, x3, x2, x1, x0);      \
      (x7) = haval_rotate_right(temp, 7) + haval_rotate_right((x7), 11) + (w) + (c);  \
      }

#define haval_FF_4(x7, x6, x5, x4, x3, x2, x1, x0, w, c) {                      \
      register haval_word temp = haval_Fphi_4(x6, x5, x4, x3, x2, x1, x0);      \
      (x7) = haval_rotate_right(temp, 7) + haval_rotate_right((x7), 11) + (w) + (c);  \
      }

#define haval_FF_5(x7, x6, x5, x4, x3, x2, x1, x0, w, c) {                      \
      register haval_word temp = haval_Fphi_5(x6, x5, x4, x3, x2, x1, x0);      \
      (x7) = haval_rotate_right(temp, 7) + haval_rotate_right((x7), 11) + (w) + (c);  \
      }

/*
 * translate every four characters into a word.
 * assume the number of characters is a multiple of four.
 */
#define haval_ch2uint(string, word, slen) {      \
  unsigned char *sp = string;              \
  haval_word    *wp = word;                \
  while (sp < (string) + (slen)) {         \
    *wp++ =  (haval_word)*sp            |  \
            ((haval_word)*(sp+1) <<  8) |  \

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -