2 * SHA1 hash algorithm. Used in SSH2 as a MAC, and the transform is
3 * also used as a `stirring' function for the PuTTY random number
4 * pool. Implemented directly from the specification by Simon
10 /* ----------------------------------------------------------------------
11 * Core SHA algorithm: processes 16-word blocks into a message digest.
14 #define rol(x,y) ( ((x) << (y)) | (((uint32)x) >> (32-y)) )
16 void SHA_Core_Init(uint32 h
[5]) {
24 void SHATransform(word32
*digest
, word32
*block
) {
29 for (t
= 0; t
< 16; t
++)
32 for (t
= 16; t
< 80; t
++) {
33 word32 tmp
= w
[t
-3] ^ w
[t
-8] ^ w
[t
-14] ^ w
[t
-16];
43 for (t
= 0; t
< 20; t
++) {
44 word32 tmp
= rol(a
, 5) + ( (b
&c
) | (d
&~b
) ) + e
+ w
[t
] + 0x5a827999;
45 e
= d
; d
= c
; c
= rol(b
, 30); b
= a
; a
= tmp
;
47 for (t
= 20; t
< 40; t
++) {
48 word32 tmp
= rol(a
, 5) + (b
^c
^d
) + e
+ w
[t
] + 0x6ed9eba1;
49 e
= d
; d
= c
; c
= rol(b
, 30); b
= a
; a
= tmp
;
51 for (t
= 40; t
< 60; t
++) {
52 word32 tmp
= rol(a
, 5) + ( (b
&c
) | (b
&d
) | (c
&d
) ) + e
+ w
[t
] + 0x8f1bbcdc;
53 e
= d
; d
= c
; c
= rol(b
, 30); b
= a
; a
= tmp
;
55 for (t
= 60; t
< 80; t
++) {
56 word32 tmp
= rol(a
, 5) + (b
^c
^d
) + e
+ w
[t
] + 0xca62c1d6;
57 e
= d
; d
= c
; c
= rol(b
, 30); b
= a
; a
= tmp
;
67 /* ----------------------------------------------------------------------
68 * Outer SHA algorithm: take an arbitrary length byte string,
69 * convert it into 16-word blocks with the prescribed padding at
70 * the end, and pass those blocks to the core SHA algorithm.
73 void SHA_Init(SHA_State
*s
) {
76 s
->lenhi
= s
->lenlo
= 0;
79 void SHA_Bytes(SHA_State
*s
, void *p
, int len
) {
80 unsigned char *q
= (unsigned char *)p
;
86 * Update the length field.
89 s
->lenhi
+= (s
->lenlo
< lenw
);
91 if (s
->blkused
&& s
->blkused
+len
< 64) {
93 * Trivial case: just add to the block.
95 memcpy(s
->block
+ s
->blkused
, q
, len
);
99 * We must complete and process at least one block.
101 while (s
->blkused
+ len
>= 64) {
102 memcpy(s
->block
+ s
->blkused
, q
, 64 - s
->blkused
);
103 q
+= 64 - s
->blkused
;
104 len
-= 64 - s
->blkused
;
105 /* Now process the block. Gather bytes big-endian into words */
106 for (i
= 0; i
< 16; i
++) {
108 ( ((uint32
)s
->block
[i
*4+0]) << 24 ) |
109 ( ((uint32
)s
->block
[i
*4+1]) << 16 ) |
110 ( ((uint32
)s
->block
[i
*4+2]) << 8 ) |
111 ( ((uint32
)s
->block
[i
*4+3]) << 0 );
113 SHATransform(s
->h
, wordblock
);
116 memcpy(s
->block
, q
, len
);
121 void SHA_Final(SHA_State
*s
, unsigned char *output
) {
127 if (s
->blkused
>= 56)
128 pad
= 56 + 64 - s
->blkused
;
130 pad
= 56 - s
->blkused
;
132 lenhi
= (s
->lenhi
<< 3) | (s
->lenlo
>> (32-3));
133 lenlo
= (s
->lenlo
<< 3);
137 SHA_Bytes(s
, &c
, pad
);
139 c
[0] = (lenhi
>> 24) & 0xFF;
140 c
[1] = (lenhi
>> 16) & 0xFF;
141 c
[2] = (lenhi
>> 8) & 0xFF;
142 c
[3] = (lenhi
>> 0) & 0xFF;
143 c
[4] = (lenlo
>> 24) & 0xFF;
144 c
[5] = (lenlo
>> 16) & 0xFF;
145 c
[6] = (lenlo
>> 8) & 0xFF;
146 c
[7] = (lenlo
>> 0) & 0xFF;
150 for (i
= 0; i
< 5; i
++) {
151 output
[i
*4 ] = (s
->h
[i
] >> 24) & 0xFF;
152 output
[i
*4+1] = (s
->h
[i
] >> 16) & 0xFF;
153 output
[i
*4+2] = (s
->h
[i
] >> 8) & 0xFF;
154 output
[i
*4+3] = (s
->h
[i
] ) & 0xFF;
158 void SHA_Simple(void *p
, int len
, unsigned char *output
) {
162 SHA_Bytes(&s
, p
, len
);
163 SHA_Final(&s
, output
);
166 /* ----------------------------------------------------------------------
167 * The above is the SHA-1 algorithm itself. Now we implement the
168 * HMAC wrapper on it.
171 static SHA_State sha1_cs_mac_s1
, sha1_cs_mac_s2
;
172 static SHA_State sha1_sc_mac_s1
, sha1_sc_mac_s2
;
174 static void sha1_key(SHA_State
*s1
, SHA_State
*s2
,
175 unsigned char *key
, int len
) {
176 unsigned char foo
[64];
179 memset(foo
, 0x36, 64);
180 for (i
= 0; i
< len
&& i
< 64; i
++)
183 SHA_Bytes(s1
, foo
, 64);
185 memset(foo
, 0x5C, 64);
186 for (i
= 0; i
< len
&& i
< 64; i
++)
189 SHA_Bytes(s2
, foo
, 64);
191 memset(foo
, 0, 64); /* burn the evidence */
194 static void sha1_cskey(unsigned char *key
) {
195 sha1_key(&sha1_cs_mac_s1
, &sha1_cs_mac_s2
, key
, 20);
198 static void sha1_sckey(unsigned char *key
) {
199 sha1_key(&sha1_sc_mac_s1
, &sha1_sc_mac_s2
, key
, 20);
202 static void sha1_cskey_buggy(unsigned char *key
) {
203 sha1_key(&sha1_cs_mac_s1
, &sha1_cs_mac_s2
, key
, 16);
206 static void sha1_sckey_buggy(unsigned char *key
) {
207 sha1_key(&sha1_sc_mac_s1
, &sha1_sc_mac_s2
, key
, 16);
210 static void sha1_do_hmac(SHA_State
*s1
, SHA_State
*s2
,
211 unsigned char *blk
, int len
, unsigned long seq
,
212 unsigned char *hmac
) {
214 unsigned char intermediate
[20];
216 intermediate
[0] = (unsigned char)((seq
>> 24) & 0xFF);
217 intermediate
[1] = (unsigned char)((seq
>> 16) & 0xFF);
218 intermediate
[2] = (unsigned char)((seq
>> 8) & 0xFF);
219 intermediate
[3] = (unsigned char)((seq
) & 0xFF);
221 s
= *s1
; /* structure copy */
222 SHA_Bytes(&s
, intermediate
, 4);
223 SHA_Bytes(&s
, blk
, len
);
224 SHA_Final(&s
, intermediate
);
225 s
= *s2
; /* structure copy */
226 SHA_Bytes(&s
, intermediate
, 20);
230 static void sha1_generate(unsigned char *blk
, int len
, unsigned long seq
) {
231 sha1_do_hmac(&sha1_cs_mac_s1
, &sha1_cs_mac_s2
, blk
, len
, seq
, blk
+len
);
234 static int sha1_verify(unsigned char *blk
, int len
, unsigned long seq
) {
235 unsigned char correct
[20];
236 sha1_do_hmac(&sha1_sc_mac_s1
, &sha1_sc_mac_s2
, blk
, len
, seq
, correct
);
237 return !memcmp(correct
, blk
+len
, 20);
240 struct ssh_mac ssh_sha1
= {
241 sha1_cskey
, sha1_sckey
,
248 struct ssh_mac ssh_sha1_buggy
= {
249 sha1_cskey_buggy
, sha1_sckey_buggy
,