debian: bumped opensync 0.4x plugin to compat 6 (with similar fixes)
[barry/progweb.git] / src / sha1.cc
blob0fce89722db856d4598510666d48846e6d2208a4
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/
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.
37 Copied from the git sources, with the following revision history:
39 commit 77ab8798d3f8df39877235be17bb6e70077aaba2
40 Author: Junio C Hamano <junkio@cox.net>
41 Date: Tue Nov 1 10:56:03 2005 -0800
43 Fix constness of input in mozilla-sha1/sha1.c::SHA1_Update().
45 Among the three of our own implementations, only this one lacked
46 "const" from the second argument.
48 Signed-off-by: Junio C Hamano <junkio@cox.net>
50 commit cef661fc799a3a13ffdea4a3f69f1acd295de53d
51 Author: Linus Torvalds <torvalds@ppc970.osdl.org>
52 Date: Thu Apr 21 12:33:22 2005 -0700
54 Add support for alternate SHA1 library implementations.
56 This one includes the Mozilla SHA1 implementation sent in by Edgar Toernig.
57 It's dual-licenced under MPL-1.1 or GPL, so in the context of git, we
58 obviously use the GPL version.
60 Side note: the Mozilla SHA1 implementation is about twice as fast as the
61 default openssl one on my G5, but the default openssl one has optimized
62 x86 assembly language on x86. So choose wisely.
66 #include "sha1.h"
68 namespace Barry {
70 static void shaHashBlock(SHA_CTX *ctx);
72 void SHA1(const void *dataIn, int len, unsigned char *hashout)
74 SHA_CTX ctx;
75 SHA1_Init(&ctx);
76 SHA1_Update(&ctx, dataIn, len);
77 SHA1_Final(hashout, &ctx);
80 void SHA1_Init(SHA_CTX *ctx) {
81 int i;
83 ctx->lenW = 0;
84 ctx->sizeHi = ctx->sizeLo = 0;
86 /* Initialize H with the magic constants (see FIPS180 for constants)
88 ctx->H[0] = 0x67452301;
89 ctx->H[1] = 0xefcdab89;
90 ctx->H[2] = 0x98badcfe;
91 ctx->H[3] = 0x10325476;
92 ctx->H[4] = 0xc3d2e1f0;
94 for (i = 0; i < 80; i++)
95 ctx->W[i] = 0;
99 void SHA1_Update(SHA_CTX *ctx, const void *_dataIn, int len) {
100 const unsigned char *dataIn = (const unsigned char *) _dataIn;
101 int i;
103 /* Read the data into W and process blocks as they get full
105 for (i = 0; i < len; i++) {
106 ctx->W[ctx->lenW / 4] <<= 8;
107 ctx->W[ctx->lenW / 4] |= (unsigned int)dataIn[i];
108 if ((++ctx->lenW) % 64 == 0) {
109 shaHashBlock(ctx);
110 ctx->lenW = 0;
112 ctx->sizeLo += 8;
113 ctx->sizeHi += (ctx->sizeLo < 8);
118 void SHA1_Final(unsigned char hashout[20], SHA_CTX *ctx) {
119 unsigned char pad0x80 = 0x80;
120 unsigned char pad0x00 = 0x00;
121 unsigned char padlen[8];
122 int i;
124 /* Pad with a binary 1 (e.g. 0x80), then zeroes, then length
126 padlen[0] = (unsigned char)((ctx->sizeHi >> 24) & 255);
127 padlen[1] = (unsigned char)((ctx->sizeHi >> 16) & 255);
128 padlen[2] = (unsigned char)((ctx->sizeHi >> 8) & 255);
129 padlen[3] = (unsigned char)((ctx->sizeHi >> 0) & 255);
130 padlen[4] = (unsigned char)((ctx->sizeLo >> 24) & 255);
131 padlen[5] = (unsigned char)((ctx->sizeLo >> 16) & 255);
132 padlen[6] = (unsigned char)((ctx->sizeLo >> 8) & 255);
133 padlen[7] = (unsigned char)((ctx->sizeLo >> 0) & 255);
134 SHA1_Update(ctx, &pad0x80, 1);
135 while (ctx->lenW != 56)
136 SHA1_Update(ctx, &pad0x00, 1);
137 SHA1_Update(ctx, padlen, 8);
139 /* Output hash
141 for (i = 0; i < 20; i++) {
142 hashout[i] = (unsigned char)(ctx->H[i / 4] >> 24);
143 ctx->H[i / 4] <<= 8;
147 * Re-initialize the context (also zeroizes contents)
149 SHA1_Init(ctx);
153 #define SHA_ROT(X,n) (((X) << (n)) | ((X) >> (32-(n))))
155 static void shaHashBlock(SHA_CTX *ctx) {
156 int t;
157 unsigned int A,B,C,D,E,TEMP;
159 for (t = 16; t <= 79; t++)
160 ctx->W[t] =
161 SHA_ROT(ctx->W[t-3] ^ ctx->W[t-8] ^ ctx->W[t-14] ^ ctx->W[t-16], 1);
163 A = ctx->H[0];
164 B = ctx->H[1];
165 C = ctx->H[2];
166 D = ctx->H[3];
167 E = ctx->H[4];
169 for (t = 0; t <= 19; t++) {
170 TEMP = SHA_ROT(A,5) + (((C^D)&B)^D) + E + ctx->W[t] + 0x5a827999;
171 E = D; D = C; C = SHA_ROT(B, 30); B = A; A = TEMP;
173 for (t = 20; t <= 39; t++) {
174 TEMP = SHA_ROT(A,5) + (B^C^D) + E + ctx->W[t] + 0x6ed9eba1;
175 E = D; D = C; C = SHA_ROT(B, 30); B = A; A = TEMP;
177 for (t = 40; t <= 59; t++) {
178 TEMP = SHA_ROT(A,5) + ((B&C)|(D&(B|C))) + E + ctx->W[t] + 0x8f1bbcdc;
179 E = D; D = C; C = SHA_ROT(B, 30); B = A; A = TEMP;
181 for (t = 60; t <= 79; t++) {
182 TEMP = SHA_ROT(A,5) + (B^C^D) + E + ctx->W[t] + 0xca62c1d6;
183 E = D; D = C; C = SHA_ROT(B, 30); B = A; A = TEMP;
186 ctx->H[0] += A;
187 ctx->H[1] += B;
188 ctx->H[2] += C;
189 ctx->H[3] += D;
190 ctx->H[4] += E;