annotate Resources/sha1/sha1.cpp @ 2192:61ab5fa36754 Orthanc-0.5.1

hotfix in release 0.5.1 to properly download dependencies
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 02 Dec 2016 13:04:38 +0100
parents 81b6f3013738
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
177
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 /*
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2 * sha1.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
3 *
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
4 * Copyright (C) 1998, 2009
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
5 * Paul E. Jones <paulej@packetizer.com>
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
6 * All Rights Reserved.
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 *
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 *****************************************************************************
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9 * $Id: sha1.cpp 12 2009-06-22 19:34:25Z paulej $
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
10 *****************************************************************************
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 *
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 * Description:
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 * This class implements the Secure Hashing Standard as defined
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 * in FIPS PUB 180-1 published April 17, 1995.
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 *
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 * The Secure Hashing Standard, which uses the Secure Hashing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 * Algorithm (SHA), produces a 160-bit message digest for a
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18 * given data stream. In theory, it is highly improbable that
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
19 * two messages will produce the same message digest. Therefore,
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20 * this algorithm can serve as a means of providing a "fingerprint"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21 * for a message.
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22 *
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23 * Portability Issues:
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24 * SHA-1 is defined in terms of 32-bit "words". This code was
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25 * written with the expectation that the processor has at least
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26 * a 32-bit machine word size. If the machine word size is larger,
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27 * the code should still function properly. One caveat to that
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28 * is that the input functions taking characters and character arrays
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29 * assume that only 8 bits of information are stored in each character.
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30 *
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31 * Caveats:
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32 * SHA-1 is designed to work with messages less than 2^64 bits long.
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33 * Although SHA-1 allows a message digest to be generated for
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34 * messages of any number of bits less than 2^64, this implementation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35 * only works with messages with a length that is a multiple of 8
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36 * bits.
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
37 *
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
38 */
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
39
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41 #include "sha1.h"
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43 /*
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44 * SHA1
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
45 *
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46 * Description:
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47 * This is the constructor for the sha1 class.
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48 *
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49 * Parameters:
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50 * None.
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51 *
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52 * Returns:
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
53 * Nothing.
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
54 *
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
55 * Comments:
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56 *
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
57 */
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
58 SHA1::SHA1()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
59 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
60 Reset();
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
61 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
62
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63 /*
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
64 * ~SHA1
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65 *
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66 * Description:
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67 * This is the destructor for the sha1 class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
68 *
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
69 * Parameters:
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
70 * None.
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
71 *
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
72 * Returns:
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
73 * Nothing.
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
74 *
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
75 * Comments:
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
76 *
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
77 */
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
78 SHA1::~SHA1()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
79 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
80 // The destructor does nothing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
81 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
82
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
83 /*
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
84 * Reset
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
85 *
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
86 * Description:
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
87 * This function will initialize the sha1 class member variables
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
88 * in preparation for computing a new message digest.
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
89 *
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
90 * Parameters:
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
91 * None.
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
92 *
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
93 * Returns:
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
94 * Nothing.
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
95 *
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
96 * Comments:
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
97 *
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
98 */
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
99 void SHA1::Reset()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
100 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
101 Length_Low = 0;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
102 Length_High = 0;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
103 Message_Block_Index = 0;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
104
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
105 H[0] = 0x67452301;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
106 H[1] = 0xEFCDAB89;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
107 H[2] = 0x98BADCFE;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
108 H[3] = 0x10325476;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
109 H[4] = 0xC3D2E1F0;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
110
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
111 Computed = false;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
112 Corrupted = false;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
113 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
114
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
115 /*
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
116 * Result
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
117 *
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
118 * Description:
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
119 * This function will return the 160-bit message digest into the
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
120 * array provided.
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
121 *
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
122 * Parameters:
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
123 * message_digest_array: [out]
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
124 * This is an array of five unsigned integers which will be filled
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
125 * with the message digest that has been computed.
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
126 *
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
127 * Returns:
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
128 * True if successful, false if it failed.
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
129 *
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
130 * Comments:
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
131 *
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
132 */
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
133 bool SHA1::Result(unsigned *message_digest_array)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
134 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
135 int i; // Counter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
136
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
137 if (Corrupted)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
138 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
139 return false;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
140 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
141
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
142 if (!Computed)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
143 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
144 PadMessage();
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
145 Computed = true;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
146 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
147
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
148 for(i = 0; i < 5; i++)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
149 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
150 message_digest_array[i] = H[i];
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
151 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
152
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
153 return true;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
154 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
155
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
156 /*
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
157 * Input
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
158 *
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
159 * Description:
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
160 * This function accepts an array of octets as the next portion of
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
161 * the message.
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
162 *
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
163 * Parameters:
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
164 * message_array: [in]
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
165 * An array of characters representing the next portion of the
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
166 * message.
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
167 *
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
168 * Returns:
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
169 * Nothing.
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
170 *
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
171 * Comments:
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
172 *
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
173 */
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
174 void SHA1::Input( const unsigned char *message_array,
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
175 unsigned length)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
176 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
177 if (!length)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
178 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
179 return;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
180 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
181
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
182 if (Computed || Corrupted)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
183 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
184 Corrupted = true;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
185 return;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
186 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
187
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
188 while(length-- && !Corrupted)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
189 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
190 Message_Block[Message_Block_Index++] = (*message_array & 0xFF);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
191
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
192 Length_Low += 8;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
193 Length_Low &= 0xFFFFFFFF; // Force it to 32 bits
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
194 if (Length_Low == 0)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
195 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
196 Length_High++;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
197 Length_High &= 0xFFFFFFFF; // Force it to 32 bits
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
198 if (Length_High == 0)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
199 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
200 Corrupted = true; // Message is too long
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
201 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
202 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
203
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
204 if (Message_Block_Index == 64)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
205 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
206 ProcessMessageBlock();
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
207 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
208
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
209 message_array++;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
210 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
211 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
212
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
213 /*
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
214 * Input
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
215 *
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
216 * Description:
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
217 * This function accepts an array of octets as the next portion of
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
218 * the message.
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
219 *
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
220 * Parameters:
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
221 * message_array: [in]
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
222 * An array of characters representing the next portion of the
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
223 * message.
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
224 * length: [in]
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
225 * The length of the message_array
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
226 *
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
227 * Returns:
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
228 * Nothing.
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
229 *
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
230 * Comments:
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
231 *
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
232 */
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
233 void SHA1::Input( const char *message_array,
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
234 unsigned length)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
235 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
236 Input((unsigned char *) message_array, length);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
237 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
238
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
239 /*
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
240 * Input
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
241 *
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
242 * Description:
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
243 * This function accepts a single octets as the next message element.
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
244 *
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
245 * Parameters:
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
246 * message_element: [in]
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
247 * The next octet in the message.
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
248 *
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
249 * Returns:
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
250 * Nothing.
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
251 *
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
252 * Comments:
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
253 *
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
254 */
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
255 void SHA1::Input(unsigned char message_element)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
256 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
257 Input(&message_element, 1);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
258 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
259
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
260 /*
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
261 * Input
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
262 *
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
263 * Description:
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
264 * This function accepts a single octet as the next message element.
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
265 *
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
266 * Parameters:
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
267 * message_element: [in]
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
268 * The next octet in the message.
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
269 *
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
270 * Returns:
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
271 * Nothing.
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
272 *
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
273 * Comments:
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
274 *
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
275 */
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
276 void SHA1::Input(char message_element)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
277 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
278 Input((unsigned char *) &message_element, 1);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
279 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
280
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
281 /*
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
282 * operator<<
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
283 *
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
284 * Description:
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
285 * This operator makes it convenient to provide character strings to
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
286 * the SHA1 object for processing.
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
287 *
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
288 * Parameters:
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
289 * message_array: [in]
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
290 * The character array to take as input.
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
291 *
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
292 * Returns:
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
293 * A reference to the SHA1 object.
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
294 *
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
295 * Comments:
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
296 * Each character is assumed to hold 8 bits of information.
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
297 *
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
298 */
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
299 SHA1& SHA1::operator<<(const char *message_array)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
300 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
301 const char *p = message_array;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
302
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
303 while(*p)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
304 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
305 Input(*p);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
306 p++;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
307 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
308
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
309 return *this;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
310 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
311
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
312 /*
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
313 * operator<<
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
314 *
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
315 * Description:
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
316 * This operator makes it convenient to provide character strings to
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
317 * the SHA1 object for processing.
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
318 *
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
319 * Parameters:
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
320 * message_array: [in]
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
321 * The character array to take as input.
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
322 *
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
323 * Returns:
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
324 * A reference to the SHA1 object.
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
325 *
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
326 * Comments:
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
327 * Each character is assumed to hold 8 bits of information.
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
328 *
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
329 */
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
330 SHA1& SHA1::operator<<(const unsigned char *message_array)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
331 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
332 const unsigned char *p = message_array;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
333
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
334 while(*p)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
335 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
336 Input(*p);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
337 p++;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
338 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
339
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
340 return *this;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
341 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
342
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
343 /*
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
344 * operator<<
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
345 *
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
346 * Description:
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
347 * This function provides the next octet in the message.
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
348 *
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
349 * Parameters:
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
350 * message_element: [in]
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
351 * The next octet in the message
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
352 *
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
353 * Returns:
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
354 * A reference to the SHA1 object.
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
355 *
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
356 * Comments:
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
357 * The character is assumed to hold 8 bits of information.
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
358 *
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
359 */
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
360 SHA1& SHA1::operator<<(const char message_element)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
361 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
362 Input((unsigned char *) &message_element, 1);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
363
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
364 return *this;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
365 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
366
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
367 /*
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
368 * operator<<
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
369 *
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
370 * Description:
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
371 * This function provides the next octet in the message.
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
372 *
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
373 * Parameters:
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
374 * message_element: [in]
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
375 * The next octet in the message
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
376 *
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
377 * Returns:
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
378 * A reference to the SHA1 object.
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
379 *
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
380 * Comments:
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
381 * The character is assumed to hold 8 bits of information.
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
382 *
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
383 */
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
384 SHA1& SHA1::operator<<(const unsigned char message_element)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
385 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
386 Input(&message_element, 1);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
387
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
388 return *this;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
389 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
390
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
391 /*
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
392 * ProcessMessageBlock
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
393 *
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
394 * Description:
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
395 * This function will process the next 512 bits of the message
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
396 * stored in the Message_Block array.
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
397 *
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
398 * Parameters:
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
399 * None.
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
400 *
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
401 * Returns:
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
402 * Nothing.
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
403 *
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
404 * Comments:
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
405 * Many of the variable names in this function, especially the single
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
406 * character names, were used because those were the names used
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
407 * in the publication.
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
408 *
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
409 */
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
410 void SHA1::ProcessMessageBlock()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
411 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
412 const unsigned K[] = { // Constants defined for SHA-1
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
413 0x5A827999,
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
414 0x6ED9EBA1,
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
415 0x8F1BBCDC,
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
416 0xCA62C1D6
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
417 };
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
418 int t; // Loop counter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
419 unsigned temp; // Temporary word value
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
420 unsigned W[80]; // Word sequence
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
421 unsigned A, B, C, D, E; // Word buffers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
422
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
423 /*
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
424 * Initialize the first 16 words in the array W
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
425 */
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
426 for(t = 0; t < 16; t++)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
427 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
428 W[t] = ((unsigned) Message_Block[t * 4]) << 24;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
429 W[t] |= ((unsigned) Message_Block[t * 4 + 1]) << 16;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
430 W[t] |= ((unsigned) Message_Block[t * 4 + 2]) << 8;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
431 W[t] |= ((unsigned) Message_Block[t * 4 + 3]);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
432 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
433
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
434 for(t = 16; t < 80; t++)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
435 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
436 W[t] = CircularShift(1,W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16]);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
437 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
438
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
439 A = H[0];
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
440 B = H[1];
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
441 C = H[2];
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
442 D = H[3];
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
443 E = H[4];
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
444
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
445 for(t = 0; t < 20; t++)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
446 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
447 temp = CircularShift(5,A) + ((B & C) | ((~B) & D)) + E + W[t] + K[0];
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
448 temp &= 0xFFFFFFFF;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
449 E = D;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
450 D = C;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
451 C = CircularShift(30,B);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
452 B = A;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
453 A = temp;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
454 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
455
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
456 for(t = 20; t < 40; t++)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
457 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
458 temp = CircularShift(5,A) + (B ^ C ^ D) + E + W[t] + K[1];
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
459 temp &= 0xFFFFFFFF;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
460 E = D;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
461 D = C;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
462 C = CircularShift(30,B);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
463 B = A;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
464 A = temp;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
465 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
466
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
467 for(t = 40; t < 60; t++)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
468 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
469 temp = CircularShift(5,A) +
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
470 ((B & C) | (B & D) | (C & D)) + E + W[t] + K[2];
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
471 temp &= 0xFFFFFFFF;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
472 E = D;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
473 D = C;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
474 C = CircularShift(30,B);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
475 B = A;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
476 A = temp;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
477 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
478
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
479 for(t = 60; t < 80; t++)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
480 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
481 temp = CircularShift(5,A) + (B ^ C ^ D) + E + W[t] + K[3];
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
482 temp &= 0xFFFFFFFF;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
483 E = D;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
484 D = C;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
485 C = CircularShift(30,B);
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
486 B = A;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
487 A = temp;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
488 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
489
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
490 H[0] = (H[0] + A) & 0xFFFFFFFF;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
491 H[1] = (H[1] + B) & 0xFFFFFFFF;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
492 H[2] = (H[2] + C) & 0xFFFFFFFF;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
493 H[3] = (H[3] + D) & 0xFFFFFFFF;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
494 H[4] = (H[4] + E) & 0xFFFFFFFF;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
495
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
496 Message_Block_Index = 0;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
497 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
498
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
499 /*
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
500 * PadMessage
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
501 *
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
502 * Description:
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
503 * According to the standard, the message must be padded to an even
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
504 * 512 bits. The first padding bit must be a '1'. The last 64 bits
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
505 * represent the length of the original message. All bits in between
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
506 * should be 0. This function will pad the message according to those
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
507 * rules by filling the message_block array accordingly. It will also
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
508 * call ProcessMessageBlock() appropriately. When it returns, it
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
509 * can be assumed that the message digest has been computed.
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
510 *
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
511 * Parameters:
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
512 * None.
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
513 *
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
514 * Returns:
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
515 * Nothing.
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
516 *
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
517 * Comments:
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
518 *
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
519 */
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
520 void SHA1::PadMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
521 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
522 /*
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
523 * Check to see if the current message block is too small to hold
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
524 * the initial padding bits and length. If so, we will pad the
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
525 * block, process it, and then continue padding into a second block.
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
526 */
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
527 if (Message_Block_Index > 55)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
528 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
529 Message_Block[Message_Block_Index++] = 0x80;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
530 while(Message_Block_Index < 64)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
531 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
532 Message_Block[Message_Block_Index++] = 0;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
533 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
534
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
535 ProcessMessageBlock();
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
536
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
537 while(Message_Block_Index < 56)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
538 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
539 Message_Block[Message_Block_Index++] = 0;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
540 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
541 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
542 else
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
543 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
544 Message_Block[Message_Block_Index++] = 0x80;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
545 while(Message_Block_Index < 56)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
546 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
547 Message_Block[Message_Block_Index++] = 0;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
548 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
549
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
550 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
551
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
552 /*
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
553 * Store the message length as the last 8 octets
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
554 */
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
555 Message_Block[56] = (Length_High >> 24) & 0xFF;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
556 Message_Block[57] = (Length_High >> 16) & 0xFF;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
557 Message_Block[58] = (Length_High >> 8) & 0xFF;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
558 Message_Block[59] = (Length_High) & 0xFF;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
559 Message_Block[60] = (Length_Low >> 24) & 0xFF;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
560 Message_Block[61] = (Length_Low >> 16) & 0xFF;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
561 Message_Block[62] = (Length_Low >> 8) & 0xFF;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
562 Message_Block[63] = (Length_Low) & 0xFF;
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
563
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
564 ProcessMessageBlock();
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
565 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
566
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
567
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
568 /*
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
569 * CircularShift
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
570 *
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
571 * Description:
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
572 * This member function will perform a circular shifting operation.
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
573 *
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
574 * Parameters:
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
575 * bits: [in]
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
576 * The number of bits to shift (1-31)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
577 * word: [in]
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
578 * The value to shift (assumes a 32-bit integer)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
579 *
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
580 * Returns:
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
581 * The shifted value.
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
582 *
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
583 * Comments:
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
584 *
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
585 */
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
586 unsigned SHA1::CircularShift(int bits, unsigned word)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
587 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
588 return ((word << bits) & 0xFFFFFFFF) | ((word & 0xFFFFFFFF) >> (32-bits));
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
589 }