Fix another concatenated filepath in a test for Digest::SHA.
[p5sagit/p5-mst-13.2.git] / ext / Digest / SHA / src / sha.h
CommitLineData
05128928 1/*
2 * sha.h: header file for SHA-1/224/256/384/512 routines
3 *
4 * Ref: NIST FIPS PUB 180-2 Secure Hash Standard
5 *
6 * Copyright (C) 2003-2005 Mark Shelor, All Rights Reserved
7 *
8 * Version: 5.32
9 * Fri Dec 2 02:32:20 MST 2005
10 *
11 */
12
13#ifndef _INCLUDE_SHA_H_
14#define _INCLUDE_SHA_H_
15
16#include <limits.h>
17
18#define SHA32_MAX 4294967295U
19#define SHA64_MAX 18446744073709551615U
20
21#define SHA32_SHR(x, n) ((x) >> (n))
22#define SHA32_SHL(x, n) ((x) << (n))
23
24#define SHA64_SHR(x, n) ((x) >> (n))
25#define SHA64_SHL(x, n) ((x) << (n))
26
27#define SHA32_ALIGNED
28#define SHA64_ALIGNED
29
30#define SHA_LO32(x) (x)
31
32#if USHRT_MAX == SHA32_MAX
33 #define SHA32 unsigned short
34 #define SHA32_CONST(c) c ## U
35#elif UINT_MAX == SHA32_MAX
36 #define SHA32 unsigned int
37 #define SHA32_CONST(c) c ## U
38#elif ULONG_MAX == SHA32_MAX
39 #define SHA32 unsigned long
40 #define SHA32_CONST(c) c ## UL
41#else
42 #undef SHA32_ALIGNED
43 #undef SHA_LO32
44 #define SHA_LO32(x) ((x) & SHA32_MAX)
45 #undef SHA32_SHR
46 #define SHA32_SHR(x, n) (SHA_LO32(x) >> (n))
47 #define SHA32 unsigned long
48 #define SHA32_CONST(c) c ## UL
49#endif
50
51#if defined(ULONG_LONG_MAX) || defined(ULLONG_MAX) || defined(HAS_LONG_LONG)
52 #define SHA_ULL_EXISTS
53#endif
54
55#if (((ULONG_MAX >> 16) >> 16) >> 16) >> 15 == 1UL
56 #define SHA64 unsigned long
57 #define SHA64_CONST(c) c ## UL
58#elif defined(SHA_ULL_EXISTS) && defined(LONGLONGSIZE) && LONGLONGSIZE == 8
59 #define SHA64 unsigned long long
60 #define SHA64_CONST(c) c ## ULL
61#elif defined(SHA_ULL_EXISTS)
62 #undef SHA64_ALIGNED
63 #undef SHA64_SHR
64 #define SHA64_SHR(x, n) (((x) & SHA64_MAX) >> (n))
65 #define SHA64 unsigned long long
66 #define SHA64_CONST(c) c ## ULL
67
68 /* The following cases detect compilers that
69 * support 64-bit types in a non-standard way */
70
71#elif defined(_MSC_VER) /* Microsoft C */
72 #define SHA64 unsigned __int64
73 #define SHA64_CONST(c) (SHA64) c
74#endif
75
76#if defined(SHA64) && !defined(NO_SHA_384_512)
77 #define SHA_384_512
78#endif
79
80#if defined(BYTEORDER) && (BYTEORDER & 0xffff) == 0x4321
81 #if defined(SHA32_ALIGNED)
82 #define SHA32_SCHED(W, b) memcpy(W, b, 64)
83 #endif
84 #if defined(SHA64) && defined(SHA64_ALIGNED)
85 #define SHA64_SCHED(W, b) memcpy(W, b, 128)
86 #endif
87#endif
88
89#if !defined(SHA32_SCHED)
90 #define SHA32_SCHED(W, b) { int t; SHA32 *q = W; \
91 for (t = 0; t < 16; t++, b += 4) *q++ = \
92 (SHA32) b[0] << 24 | (SHA32) b[1] << 16 | \
93 (SHA32) b[2] << 8 | (SHA32) b[3]; }
94#endif
95
96#if defined(SHA64) && !defined(SHA64_SCHED)
97 #define SHA64_SCHED(W, b) { int t; SHA64 *q = W; \
98 for (t = 0; t < 16; t++, b += 8) *q++ = \
99 (SHA64) b[0] << 56 | (SHA64) b[1] << 48 | \
100 (SHA64) b[2] << 40 | (SHA64) b[3] << 32 | \
101 (SHA64) b[4] << 24 | (SHA64) b[5] << 16 | \
102 (SHA64) b[6] << 8 | (SHA64) b[7]; }
103#endif
104
105/*
106 * SHA_STO_CLASS: default to auto storage class for message schedule
107 * arrays inside transform routines. Note that redefining this to
108 * static might improve performance on some platforms (e.g. Intel).
109 */
110
111#if !defined(SHA_STO_CLASS)
112 #define SHA_STO_CLASS auto
113#endif
114
115/* Override use of static arrays if compiling for thread-safety */
116#ifdef SHA_THREAD_SAFE
117 #undef SHA_STO_CLASS
118 #define SHA_STO_CLASS auto
119#endif
120
121/* Configure memory management and I/O for Perl or standalone C */
122#ifdef SHA_PERL_MODULE
123 #define SHA_new New
124 #define SHA_newz Newz
125 #define SHA_free Safefree
126 #define SHA_FILE PerlIO
127 #define SHA_stdin() PerlIO_stdin()
128 #define SHA_stdout() PerlIO_stdout()
129 #define SHA_open PerlIO_open
130 #define SHA_close PerlIO_close
131 #define SHA_fprintf PerlIO_printf
132 #define SHA_feof PerlIO_eof
133 #define SHA_getc PerlIO_getc
134#else
135 #define SHA_new(id, p, n, t) p = (t *) malloc(sizeof(t))
136 #define SHA_newz(id, p, n, t) p = (t *) calloc(n, sizeof(t))
137 #define SHA_free free
138 #define SHA_FILE FILE
139 #define SHA_stdin() stdin
140 #define SHA_stdout() stdout
141 #define SHA_open fopen
142 #define SHA_close fclose
143 #define SHA_fprintf fprintf
144 #define SHA_feof feof
145 #define SHA_getc fgetc
146#endif
147
148#define SHA1 1
149#define SHA224 224
150#define SHA256 256
151#define SHA384 384
152#define SHA512 512
153
154#define SHA1_BLOCK_BITS 512
155#define SHA224_BLOCK_BITS SHA1_BLOCK_BITS
156#define SHA256_BLOCK_BITS SHA1_BLOCK_BITS
157#define SHA384_BLOCK_BITS 1024
158#define SHA512_BLOCK_BITS SHA384_BLOCK_BITS
159
160#define SHA1_DIGEST_BITS 160
161#define SHA224_DIGEST_BITS 224
162#define SHA256_DIGEST_BITS 256
163#define SHA384_DIGEST_BITS 384
164#define SHA512_DIGEST_BITS 512
165
166#define SHA_MAX_BLOCK_BITS SHA512_BLOCK_BITS
167#define SHA_MAX_DIGEST_BITS SHA512_DIGEST_BITS
168#define SHA_MAX_HEX_LEN (SHA_MAX_DIGEST_BITS / 4)
169#define SHA_MAX_BASE64_LEN (1 + (SHA_MAX_DIGEST_BITS / 6))
170
171#if defined(SHA64)
172 #define SHA_H_SIZE sizeof(SHA64) * 8
173#else
174 #define SHA_H_SIZE sizeof(SHA32) * 8
175#endif
176
177typedef struct {
178 int alg;
179 void (*sha)();
180 unsigned char H[SHA_H_SIZE];
181 unsigned char block[SHA_MAX_BLOCK_BITS/8];
182 unsigned int blockcnt;
183 unsigned int blocksize;
184 SHA32 lenhh, lenhl, lenlh, lenll;
185 unsigned char digest[SHA_MAX_DIGEST_BITS/8];
186 int digestlen;
187 char hex[SHA_MAX_HEX_LEN+1];
188 char base64[SHA_MAX_BASE64_LEN+1];
189} SHA;
190
191#define SHA_FMT_RAW 1
192#define SHA_FMT_HEX 2
193#define SHA_FMT_BASE64 3
194
195#if defined(__STDC__) && __STDC__ != 0
196 #define _SHA_P(protos) protos
197#else
198 #define _SHA_P(protos) ()
199#endif
200
201#define _SHA_STATE SHA *s
202#define _SHA_ALG int alg
203#define _SHA_DATA unsigned char *bitstr, unsigned long bitcnt
204#define _SHA_FNAME char *filename
205
206SHA *shaopen _SHA_P((_SHA_ALG));
207unsigned long shawrite _SHA_P((_SHA_DATA, _SHA_STATE));
208void shafinish _SHA_P((_SHA_STATE));
209void sharewind _SHA_P((_SHA_STATE));
210unsigned char *shadigest _SHA_P((_SHA_STATE));
211char *shahex _SHA_P((_SHA_STATE));
212char *shabase64 _SHA_P((_SHA_STATE));
213int shadsize _SHA_P((_SHA_STATE));
214SHA *shadup _SHA_P((_SHA_STATE));
215int shadump _SHA_P((_SHA_FNAME, _SHA_STATE));
216SHA *shaload _SHA_P((_SHA_FNAME));
217int shaclose _SHA_P((_SHA_STATE));
218
219unsigned char *sha1digest _SHA_P((_SHA_DATA));
220char *sha1hex _SHA_P((_SHA_DATA));
221char *sha1base64 _SHA_P((_SHA_DATA));
222unsigned char *sha224digest _SHA_P((_SHA_DATA));
223char *sha224hex _SHA_P((_SHA_DATA));
224char *sha224base64 _SHA_P((_SHA_DATA));
225unsigned char *sha256digest _SHA_P((_SHA_DATA));
226char *sha256hex _SHA_P((_SHA_DATA));
227char *sha256base64 _SHA_P((_SHA_DATA));
228unsigned char *sha384digest _SHA_P((_SHA_DATA));
229char *sha384hex _SHA_P((_SHA_DATA));
230char *sha384base64 _SHA_P((_SHA_DATA));
231unsigned char *sha512digest _SHA_P((_SHA_DATA));
232char *sha512hex _SHA_P((_SHA_DATA));
233char *sha512base64 _SHA_P((_SHA_DATA));
234
235#endif /* _INCLUDE_SHA_H_ */