1 /*
2           100% free public domain implementation of the SHA-1 algorithm
3           by Dominik Reichl <dominik.reichl@t-online.de>
4           Web: http://www.dominik-reichl.de/
5 
6           Version 1.6 - 2005-02-07 (thanks to Howard Kapustein for patches)
7           - You can set the endianness in your files, no need to modify the
8             header file of the CSHA1 class any more
9           - Aligned data support
10           - Made support/compilation of the utility functions (ReportHash
11             and HashFile) optional (useful, if bytes count, for example in
12             embedded environments)
13 
14           Version 1.5 - 2005-01-01
15           - 64-bit compiler compatibility added
16           - Made variable wiping optional (define SHA1_WIPE_VARIABLES)
17           - Removed unnecessary variable initializations
18           - ROL32 improvement for the Microsoft compiler (using _rotl)
19 
20           ======== Test Vectors (from FIPS PUB 180-1) ========
21 
22           SHA1("abc") =
23                     A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D
24 
25           SHA1("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq") =
26                     84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1
27 
28           SHA1(A million repetitions of "a") =
29                     34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F
30 */
31 
32 #ifndef ___SHA1_HDR___
33 #define ___SHA1_HDR___
34 
35 #if !defined(SHA1_UTILITY_FUNCTIONS) && !defined(SHA1_NO_UTILITY_FUNCTIONS)
36 #define SHA1_UTILITY_FUNCTIONS
37 #endif
38 
39 #include <memory.h> // Needed for memset and memcpy
40 #include "stdafx.h"
41 
42 #ifdef SHA1_UTILITY_FUNCTIONS
43 #include <stdio.h>  // Needed for file access and sprintf
44 #include <string.h> // Needed for strcat and strcpy
45 #endif
46 
47 #ifdef _MSC_VER
48 #include <stdlib.h>
49 #endif
50 
51 // You can define the endian mode in your files, without modifying the SHA1
52 // source files. Just #define SHA1_LITTLE_ENDIAN or #define SHA1_BIG_ENDIAN
53 // in your files, before including the SHA1.h header file. If you don't
54 // define anything, the class defaults to little endian.
55 
56 #if !defined(SHA1_LITTLE_ENDIAN) && !defined(SHA1_BIG_ENDIAN)
57 #define SHA1_LITTLE_ENDIAN
58 #endif
59 
60 // Same here. If you want variable wiping, #define SHA1_WIPE_VARIABLES, if
61 // not, #define SHA1_NO_WIPE_VARIABLES. If you don't define anything, it
62 // defaults to wiping.
63 
64 #if !defined(SHA1_WIPE_VARIABLES) && !defined(SHA1_NO_WIPE_VARIABLES)
65 #define SHA1_WIPE_VARIABLES
66 #endif
67 
68 /////////////////////////////////////////////////////////////////////////////
69 // Define 8- and 32-bit variables
70 
71 //#ifndef uint32_t
72 //
73 //#ifdef _MSC_VER
74 //
75 //#define uint8_t  unsigned __int8
76 //#define uint32_t unsigned __int32
77 //
78 //#else
79 //
80 //#define uint8_t unsigned char
81 //
82 //#if (ULONG_MAX == 0xFFFFFFFF)
83 //#define uint32_t unsigned long
84 //#else
85 //#define uint32_t unsigned int
86 //#endif
87 //
88 //#endif
89 //#endif
90 
91 /////////////////////////////////////////////////////////////////////////////
92 // Declare SHA1 workspace
93 
94 typedef union
95 {
96           uint8_t  c[64];
97           uint32_t l[16];
98 } SHA1_WORKSPACE_BLOCK;
99 
100 class CSHA1
101 {
102 public:
103 #ifdef SHA1_UTILITY_FUNCTIONS
104           // Two different formats for ReportHash(...)
105           enum
106           {
107                     REPORT_HEX = 0,
108                     REPORT_DIGIT = 1
109           };
110 #endif
111 
112           // Constructor and Destructor
113           CSHA1();
114           ~CSHA1();
115 
116           uint32_t m_state[5];
117           uint32_t m_count[2];
118           uint32_t __reserved1[1];
119           uint8_t  m_buffer[64];
120           uint8_t  m_digest[20];
121           uint32_t __reserved2[3];
122 
123           void Reset();
124 
125           // Update the hash value
126           void Update(const uint8_t *data, uint32_t len);
127 #ifdef SHA1_UTILITY_FUNCTIONS
128           bool HashFile(char *szFileName);
129 #endif
130 
131           // Finalize hash and report
132           void Final();
133 
134           // Report functions: as pre-formatted and raw data
135 #ifdef SHA1_UTILITY_FUNCTIONS
136           void ReportHash(char *szReport, unsigned char uReportType = REPORT_HEX);
137 #endif
138           void GetHash(uint8_t *puDest);
139 
140 private:
141           // Private SHA-1 transformation
142           void Transform(uint32_t *state, const uint8_t *buffer);
143 
144           // Member variables
145           uint8_t m_workspace[64];
146           SHA1_WORKSPACE_BLOCK *m_block; // SHA1 pointer to the byte array above
147 };
148 
149 #endif
150