2 * The contents of this file are subject to the Mozilla Public
3 * License Version 1.1 (the "License"); you may not use this file
4 * except in compliance with the License. You may obtain a copy of
5 * the License at http://www.mozilla.org/MPL/
7 * Software distributed under the License is distributed on an "AS
8 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
9 * implied. See the License for the specific language governing
10 * rights and limitations under the License.
12 * The Original Code is SHA 180-1 Reference Implementation (Compact version)
14 * The Initial Developer of the Original Code is Paul Kocher of
15 * Cryptography Research. Portions created by Paul Kocher are
16 * Copyright (C) 1995-9 by Cryptography Research, Inc. All
25 static void shaHashBlock(A_SHA_CTX
*ctx
);
27 void A_SHAInit(A_SHA_CTX
*ctx
)
32 ctx
->sizeHi
= ctx
->sizeLo
= 0;
34 /* Initialize H with the magic constants (see FIPS180 for constants)
36 ctx
->H
[0] = 0x67452301L
;
37 ctx
->H
[1] = 0xefcdab89L
;
38 ctx
->H
[2] = 0x98badcfeL
;
39 ctx
->H
[3] = 0x10325476L
;
40 ctx
->H
[4] = 0xc3d2e1f0L
;
42 for (i
= 0; i
< 80; i
++)
47 void A_SHAUpdate(A_SHA_CTX
*ctx
, unsigned char *dataIn
, int len
)
51 /* Read the data into W and process blocks as they get full */
52 for (i
= 0; i
< len
; i
++)
54 ctx
->W
[ctx
->lenW
/ 4] <<= 8;
55 ctx
->W
[ctx
->lenW
/ 4] |= (unsigned long)dataIn
[i
];
56 if ((++ctx
->lenW
) % 64 == 0)
62 ctx
->sizeHi
+= (ctx
->sizeLo
< 8);
67 void A_SHAFinal(A_SHA_CTX
*ctx
, unsigned char hashout
[20])
69 unsigned char pad0x80
= 0x80;
70 unsigned char pad0x00
= 0x00;
71 unsigned char padlen
[8];
74 /* Pad with a binary 1 (e.g. 0x80), then zeroes, then length */
75 padlen
[0] = (unsigned char)((ctx
->sizeHi
>> 24) & 255);
76 padlen
[1] = (unsigned char)((ctx
->sizeHi
>> 16) & 255);
77 padlen
[2] = (unsigned char)((ctx
->sizeHi
>> 8) & 255);
78 padlen
[3] = (unsigned char)((ctx
->sizeHi
>> 0) & 255);
79 padlen
[4] = (unsigned char)((ctx
->sizeLo
>> 24) & 255);
80 padlen
[5] = (unsigned char)((ctx
->sizeLo
>> 16) & 255);
81 padlen
[6] = (unsigned char)((ctx
->sizeLo
>> 8) & 255);
82 padlen
[7] = (unsigned char)((ctx
->sizeLo
>> 0) & 255);
83 A_SHAUpdate(ctx
, &pad0x80
, 1);
84 while (ctx
->lenW
!= 56)
85 A_SHAUpdate(ctx
, &pad0x00
, 1);
86 A_SHAUpdate(ctx
, padlen
, 8);
90 for (i
= 0; i
< 20; i
++)
92 hashout
[i
] = (unsigned char)(ctx
->H
[i
/ 4] >> 24);
97 * Re-initialize the context (also zeroizes contents)
102 #define SHA_ROTL(X,n) ((((X) << (n)) | ((X) >> (32-(n)))) & 0xffffffffL)
104 static void shaHashBlock(A_SHA_CTX
*ctx
)
107 unsigned long A
,B
,C
,D
,E
,TEMP
;
109 for (t
= 16; t
<= 79; t
++)
110 ctx
->W
[t
] = SHA_ROTL(ctx
->W
[t
-3] ^ ctx
->W
[t
-8] ^ ctx
->W
[t
-14] ^ ctx
->W
[t
-16], 1);
118 for (t
= 0; t
<= 19; t
++)
120 TEMP
= (SHA_ROTL(A
,5) + (((C
^D
)&B
)^D
) + E
+ ctx
->W
[t
] + 0x5a827999L
) & 0xffffffffL
;
121 E
= D
; D
= C
; C
= SHA_ROTL(B
, 30); B
= A
; A
= TEMP
;
123 for (t
= 20; t
<= 39; t
++)
125 TEMP
= (SHA_ROTL(A
,5) + (B
^C
^D
) + E
+ ctx
->W
[t
] + 0x6ed9eba1L
) & 0xffffffffL
;
126 E
= D
; D
= C
; C
= SHA_ROTL(B
, 30); B
= A
; A
= TEMP
;
128 for (t
= 40; t
<= 59; t
++)
130 TEMP
= (SHA_ROTL(A
,5) + ((B
&C
)|(D
&(B
|C
))) + E
+ ctx
->W
[t
] + 0x8f1bbcdcL
) & 0xffffffffL
;
131 E
= D
; D
= C
; C
= SHA_ROTL(B
, 30); B
= A
; A
= TEMP
;
133 for (t
= 60; t
<= 79; t
++)
135 TEMP
= (SHA_ROTL(A
,5) + (B
^C
^D
) + E
+ ctx
->W
[t
] + 0xca62c1d6L
) & 0xffffffffL
;
136 E
= D
; D
= C
; C
= SHA_ROTL(B
, 30); B
= A
; A
= TEMP
;