Initial revision
[LeanCalc.git] / calc / calc / hash.h
1 /*
2  * hash - one-way hash routines
3  *
4  * Copyright (C) 1999  Landon Curt Noll
5  *
6  * Calc is open software; you can redistribute it and/or modify it under
7  * the terms of the version 2.1 of the GNU Lesser General Public License
8  * as published by the Free Software Foundation.
9  *
10  * Calc is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General
13  * Public License for more details.
14  *
15  * A copy of version 2.1 of the GNU Lesser General Public License is
16  * distributed with calc under the filename COPYING-LGPL.  You should have
17  * received a copy with calc; if not, write to Free Software Foundation, Inc.
18  * 59 Temple Place, Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * @(#) $Revision$
21  * @(#) $Id$
22  * @(#) $Source$
23  *
24  * Under source code control:   1995/11/14 23:57:45
25  * File existed as early as:    1995
26  *
27  * chongo <was here> /\oo/\     http://www.isthe.com/chongo/
28  * Share and enjoy!  :-)        http://www.isthe.com/chongo/tech/comp/calc/
29  */
30
31
32 #if !defined(__HASH_H__)
33 #define __HASH_H__
34
35
36 #if defined(CALC_SRC)   /* if we are building from the calc source tree */
37 # include "calc/shs.h"
38 # include "calc/shs1.h"
39 # include "calc/md5.h"
40 # include "calc/zmath.h"
41 #else
42 # include <calc/shs.h>
43 # include <calc/shs1.h>
44 # include <calc/md5.h>
45 # include <calc/zmath.h>
46 #endif
47
48
49 /* MAX_CHUNKSIZE is the largest chunksize of any hash */
50 #define MAX_CHUNKSIZE (SHS1_CHUNKSIZE)
51
52 /* max size of debugging strings in xyz_print() functions */
53 #define DEBUG_SIZE 127
54
55
56 /*
57  * hashstate - state of a hash system
58  *
59  * Hashing some types of values requires a checkpoint (chkpt function call)
60  * to be performed, which pads buffered data with 0's and performs an
61  * update.  The checkpoint thus causes the value to start on a new hash
62  * block boundary with no buffered data.
63  *
64  * Some data types (strings, BLOCKs and OCTETs) do not require a
65  * checkpoint as long as the previous value hashed was a string,
66  * BLOCK or OCTET.
67  */
68 typedef struct hashstate HASH;
69 struct hashstate {
70         int hashtype;           /* XYZ_HASH_TYPE debug value */
71         BOOL bytes;             /* TRUE => reading bytes rather than words */
72         void (*update)(HASH*, USB8*, USB32);    /* update arbitrary length */
73         void (*chkpt)(HASH*);                   /* checkpoint a state */
74         void (*note)(int, HASH*);               /* note a special value */
75         void (*type)(int, HASH*);               /* note a VALUE type */
76         ZVALUE (*final)(HASH*);         /* complete hash state */
77         int (*cmp)(HASH*,HASH*);        /* compare to states, TRUE => a!=b */
78         void (*print)(HASH*);           /* print the value of a hash */
79         int base;                       /* XYZ_BASE special hash value */
80         int chunksize;                  /* XYZ_CHUNKSIZE input chunk size */
81         int unionsize;                  /* h_union element size */
82         union {                         /* hash dependent states */
83                 USB8 data[1];           /* used by hash_value to hash below */
84                 SHS_INFO h_shs;         /* old SHS/SHA internal state */
85                 SHS1_INFO h_shs1;       /* new SHS-1/SHA-1 internal state */
86                 MD5_CTX h_md5;          /* MD5 internal state */
87         } h_union;
88 };
89
90
91 /*
92  * what to xor to digest value when hashing special values
93  *
94  * IMPORTANT: To avoid overlap due to the HASH_XYZ macros below, the
95  *            XYZ_BASE values should be unique random hex values
96  *            that end in 00 (i.e., 0 mod 256).
97  */
98 #define SHS_BASE 0x12face00             /* old SHS / SHA */
99 #define SHS1_BASE 0x23cafe00            /* new SHS-1 / SHA-1 */
100 #define MD5_BASE 0x34feed00             /* MD5 */
101
102
103 /*
104  * XYZ_HASH_TYPE - hash types
105  *
106  * we support these hash types
107  */
108 #define SHS_HASH_TYPE 1
109 #define SHS1_HASH_TYPE 2
110 #define MD5_HASH_TYPE 3
111
112
113 /*
114  * Note a special value given the base value
115  */
116 #define HASH_NEG(base) (1+base)         /* note a negative value */
117 #define HASH_COMPLEX(base) (2+base)     /* note a complex value */
118 #define HASH_DIV(base) (4+base)         /* note a division by a value */
119 #define HASH_ZERO(base) (8+base)        /* note a zero numeric value */
120 #define HASH_ZVALUE(base) (16+base)     /* note a ZVALUE */
121
122
123 /*
124  * external functions
125  */
126 extern HASH* hash_init(int, HASH*);
127 extern void hash_free(HASH*);
128 extern HASH* hash_copy(HASH*);
129 extern int hash_cmp(HASH*, HASH*);
130 extern void hash_print(HASH*);
131 extern ZVALUE hash_final(HASH*);
132 extern HASH* hash_long(int, long, HASH*);
133 extern HASH* hash_zvalue(int, ZVALUE, HASH*);
134 extern HASH* hash_number(int, void*, HASH*);
135 extern HASH* hash_complex(int, void*, HASH*);
136 extern HASH* hash_str(int, char*, HASH*);
137 extern HASH* hash_usb8(int, USB8*, int, HASH*);
138 extern HASH* hash_value(int, void*, HASH*);
139
140
141 #endif /* !__HASH_H__ */