More reformatting.
[ahxm.git] / sha1.c
blob2d4cdb9fc5c338c7a225bc1626e4d4b6e8666ff4
1 /*
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/
6 *
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
17 * Rights Reserved.
19 * Contributor(s):
21 * Paul Kocher
23 * Alternatively, the contents of this file may be used under the
24 * terms of the GNU General Public License Version 2 or later (the
25 * "GPL"), in which case the provisions of the GPL are applicable
26 * instead of those above. If you wish to allow use of your
27 * version of this file only under the terms of the GPL and not to
28 * allow others to use your version of this file under the MPL,
29 * indicate your decision by deleting the provisions above and
30 * replace them with the notice and other provisions required by
31 * the GPL. If you do not delete the provisions above, a recipient
32 * may use your version of this file under either the MPL or the
33 * GPL.
36 #include "sha1.h"
38 static void shaHashBlock(SHA_CTX * ctx);
40 void SHA1_Init(SHA_CTX * ctx)
42 int i;
44 ctx->lenW = 0;
45 ctx->sizeHi = ctx->sizeLo = 0;
47 /* Initialize H with the magic constants (see FIPS180 for constants)
49 ctx->H[0] = 0x67452301;
50 ctx->H[1] = 0xefcdab89;
51 ctx->H[2] = 0x98badcfe;
52 ctx->H[3] = 0x10325476;
53 ctx->H[4] = 0xc3d2e1f0;
55 for (i = 0; i < 80; i++)
56 ctx->W[i] = 0;
60 void SHA1_Update(SHA_CTX * ctx, void *_dataIn, int len)
62 unsigned char *dataIn = _dataIn;
63 int i;
65 /* Read the data into W and process blocks as they get full
67 for (i = 0; i < len; i++) {
68 ctx->W[ctx->lenW / 4] <<= 8;
69 ctx->W[ctx->lenW / 4] |= (unsigned int) dataIn[i];
70 if ((++ctx->lenW) % 64 == 0) {
71 shaHashBlock(ctx);
72 ctx->lenW = 0;
74 ctx->sizeLo += 8;
75 ctx->sizeHi += (ctx->sizeLo < 8);
80 void SHA1_Final(unsigned char hashout[20], SHA_CTX * ctx)
82 unsigned char pad0x80 = 0x80;
83 unsigned char pad0x00 = 0x00;
84 unsigned char padlen[8];
85 int i;
87 /* Pad with a binary 1 (e.g. 0x80), then zeroes, then length
89 padlen[0] = (unsigned char) ((ctx->sizeHi >> 24) & 255);
90 padlen[1] = (unsigned char) ((ctx->sizeHi >> 16) & 255);
91 padlen[2] = (unsigned char) ((ctx->sizeHi >> 8) & 255);
92 padlen[3] = (unsigned char) ((ctx->sizeHi >> 0) & 255);
93 padlen[4] = (unsigned char) ((ctx->sizeLo >> 24) & 255);
94 padlen[5] = (unsigned char) ((ctx->sizeLo >> 16) & 255);
95 padlen[6] = (unsigned char) ((ctx->sizeLo >> 8) & 255);
96 padlen[7] = (unsigned char) ((ctx->sizeLo >> 0) & 255);
97 SHA1_Update(ctx, &pad0x80, 1);
98 while (ctx->lenW != 56)
99 SHA1_Update(ctx, &pad0x00, 1);
100 SHA1_Update(ctx, padlen, 8);
102 /* Output hash
104 for (i = 0; i < 20; i++) {
105 hashout[i] = (unsigned char) (ctx->H[i / 4] >> 24);
106 ctx->H[i / 4] <<= 8;
110 * Re-initialize the context (also zeroizes contents)
112 SHA1_Init(ctx);
116 #define SHA_ROT(X,n) (((X) << (n)) | ((X) >> (32-(n))))
118 static void shaHashBlock(SHA_CTX * ctx)
120 int t;
121 unsigned int A, B, C, D, E, TEMP;
123 for (t = 16; t <= 79; t++)
124 ctx->W[t] =
125 SHA_ROT(ctx->W[t - 3] ^ ctx->W[t - 8] ^ ctx->
126 W[t - 14] ^ ctx->W[t - 16], 1);
128 A = ctx->H[0];
129 B = ctx->H[1];
130 C = ctx->H[2];
131 D = ctx->H[3];
132 E = ctx->H[4];
134 for (t = 0; t <= 19; t++) {
135 TEMP =
136 SHA_ROT(A,
137 5) + (((C ^ D) & B) ^ D) + E + ctx->W[t] + 0x5a827999;
138 E = D;
139 D = C;
140 C = SHA_ROT(B, 30);
141 B = A;
142 A = TEMP;
144 for (t = 20; t <= 39; t++) {
145 TEMP = SHA_ROT(A, 5) + (B ^ C ^ D) + E + ctx->W[t] + 0x6ed9eba1;
146 E = D;
147 D = C;
148 C = SHA_ROT(B, 30);
149 B = A;
150 A = TEMP;
152 for (t = 40; t <= 59; t++) {
153 TEMP =
154 SHA_ROT(A,
155 5) + ((B & C) | (D & (B | C))) + E + ctx->W[t] +
156 0x8f1bbcdc;
157 E = D;
158 D = C;
159 C = SHA_ROT(B, 30);
160 B = A;
161 A = TEMP;
163 for (t = 60; t <= 79; t++) {
164 TEMP = SHA_ROT(A, 5) + (B ^ C ^ D) + E + ctx->W[t] + 0xca62c1d6;
165 E = D;
166 D = C;
167 C = SHA_ROT(B, 30);
168 B = A;
169 A = TEMP;
172 ctx->H[0] += A;
173 ctx->H[1] += B;
174 ctx->H[2] += C;
175 ctx->H[3] += D;
176 ctx->H[4] += E;