Add built-in (Mozilla) SHA1 routines Paul agreed to the GCC-exception license by email: | | From: Paul Kocher <paul@cryptography.com> | Date: Sun, 15 Mar 2009 11:37:23 -0700 | Subject: Re: Adding Mozilla SHA1 implementation to libgit2 | | Yes - that's fine. | | At 01:56 AM 3/5/2009, Andreas Ericsson wrote: | > Hi Paul. We spoke earlier about this, if you remember? | > We'd like to add the GCC-exception to the GPL license | > for these files. Signed-off-by: Paul Kocher <paul@cryptography.com> Signed-off-by: Ramsay Jones <ramsay@ramsay1.demon.co.uk> Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
diff --git a/Makefile b/Makefile
index 6586dbd..56933ff 100644
--- a/Makefile
+++ b/Makefile
@@ -2,6 +2,11 @@ all::
# Define NO_VISIBILITY if your compiler does not support symbol
# visibility in general (and the -fvisibility switch in particular).
+#
+# Define NO_OPENSSL if you do not have OpenSSL, or if you simply want
+# to use the bundled (Mozilla) SHA1 routines. (The bundled SHA1
+# routines are reported to be faster than OpenSSL on some platforms)
+#
DOXYGEN = doxygen
INSTALL = install
@@ -13,20 +18,19 @@ libdir=$(prefix)/lib
uname_S := $(shell sh -c 'uname -s 2>/dev/null || echo no')
CFLAGS = -g -O2 -Wall
+LIBS = -L. -lgit2 -lz
OS = unix
+EXTRA_LIBS =
+EXTRA_SRC =
+EXTRA_OBJ =
+
# Platform specific tweaks
ifneq (,$(findstring CYGWIN,$(uname_S)))
NO_VISIBILITY=YesPlease
endif
-BASIC_CFLAGS := -Isrc
-ifndef NO_VISIBILITY
-BASIC_CFLAGS += -fvisibility=hidden
-endif
-ALL_CFLAGS = $(CFLAGS) $(BASIC_CFLAGS)
-
SRC_C = $(wildcard src/*.c)
OBJS = $(patsubst %.c,%.o,$(SRC_C))
HDRS = $(wildcard src/*.h)
@@ -40,12 +44,32 @@ TEST_OBJ = $(patsubst %.c,%.o,\
TEST_EXE = $(patsubst %.o,%.exe,$(TEST_OBJ))
TEST_RUN = $(patsubst %.exe,%.run,$(TEST_EXE))
+ifndef NO_OPENSSL
+ SHA1_HEADER = <openssl/sha.h>
+ EXTRA_LIBS += -lcrypto
+else
+ SHA1_HEADER = "sha1/sha1.h"
+ EXTRA_SRC += src/sha1/sha1.c
+ EXTRA_OBJ += src/sha1/sha1.o
+endif
+
+BASIC_CFLAGS := -Isrc -DSHA1_HEADER='$(SHA1_HEADER)'
+ifndef NO_VISIBILITY
+BASIC_CFLAGS += -fvisibility=hidden
+endif
+
+ALL_CFLAGS = $(CFLAGS) $(BASIC_CFLAGS)
+SRC_C += $(EXTRA_SRC)
+OBJ += $(EXTRA_OBJ)
+LIBS += $(EXTRA_LIBS)
+
all:: $(GIT_LIB)
clean:
rm -f $(GIT_LIB)
rm -f libgit2.pc
rm -f src/*.o
+ rm -f src/sha1/*.o
rm -f tests/*.o tests/*.exe tests/*.toc
rm -rf trash-*.exe
rm -rf apidocs
@@ -86,7 +110,7 @@ uninstall:
$(OBJS): $(HDRS)
$(GIT_LIB): $(OBJS)
- rm -f $(LIB)
+ rm -f $(GIT_LIB)
$(AR) cr $(GIT_LIB) $(OBJS)
$(RANLIB) $(GIT_LIB)
@@ -116,7 +140,7 @@ $(TEST_EXE): tests/%.exe: tests/%.o tests/%_main.o
$(CC) -o $@ \
$(patsubst %.exe,%_main.o,$@) \
$(patsubst %.exe,%.o,$@) \
- $(T_LIB) -L. -lgit2 -lz -lcrypto
+ $(T_LIB) $(LIBS)
$(TEST_RUN): tests/%.run: tests/%.exe
@t=trash-$(<F) && \
diff --git a/src/hash.c b/src/hash.c
index 65912de..0051f09 100644
--- a/src/hash.c
+++ b/src/hash.c
@@ -25,7 +25,7 @@
#include "common.h"
#include "hash.h"
-#include <openssl/sha.h>
+#include SHA1_HEADER
struct git_hash_ctx {
SHA_CTX c;
diff --git a/src/sha1/sha1.c b/src/sha1/sha1.c
new file mode 100644
index 0000000..833ae91
--- /dev/null
+++ b/src/sha1/sha1.c
@@ -0,0 +1,170 @@
+/*
+ * The contents of this file are subject to the Mozilla Public
+ * License Version 1.1 (the "License"); you may not use this file
+ * except in compliance with the License. You may obtain a copy of
+ * the License at http://www.mozilla.org/MPL/
+ *
+ * Software distributed under the License is distributed on an "AS
+ * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
+ * implied. See the License for the specific language governing
+ * rights and limitations under the License.
+ *
+ * The Original Code is SHA 180-1 Reference Implementation (Compact version)
+ *
+ * The Initial Developer of the Original Code is Paul Kocher of
+ * Cryptography Research. Portions created by Paul Kocher are
+ * Copyright (C) 1995-9 by Cryptography Research, Inc. All
+ * Rights Reserved.
+ *
+ * Contributor(s):
+ *
+ * Paul Kocher
+ *
+ * Alternatively, the contents of this file may be used under the
+ * terms of the GNU General Public License, version 2, (the "GPL")
+ * as published by the Free Software Foundation.
+ *
+ * In addition to the permissions in the GNU General Public License,
+ * the authors give you unlimited permission to link the compiled
+ * version of this file into combinations with other programs,
+ * and to distribute those combinations without any restriction
+ * coming from the use of this file. (The General Public License
+ * restrictions do apply in other respects; for example, they cover
+ * modification of the file, and distribution when not linked into
+ * a combined executable.)
+ *
+ * This file is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; see the file COPYING. If not, write to
+ * the Free Software Foundation, 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+ *
+ * If you wish to allow use of your version of this file only under
+ * the terms of the GPL and not to allow others to use your version
+ * of this file under the MPL, indicate your decision by deleting
+ * the provisions above and replace them with the notice and other
+ * provisions required by the GPL. If you do not delete the
+ * provisions above, a recipient may use your version of this file
+ * under either the MPL or the GPL.
+ */
+
+#include "sha1.h"
+
+static void shaHashBlock(SHA_CTX *ctx);
+
+void SHA1_Init(SHA_CTX *ctx) {
+ int i;
+
+ ctx->lenW = 0;
+ ctx->sizeHi = ctx->sizeLo = 0;
+
+ /* Initialize H with the magic constants (see FIPS180 for constants)
+ */
+ ctx->H[0] = 0x67452301;
+ ctx->H[1] = 0xefcdab89;
+ ctx->H[2] = 0x98badcfe;
+ ctx->H[3] = 0x10325476;
+ ctx->H[4] = 0xc3d2e1f0;
+
+ for (i = 0; i < 80; i++)
+ ctx->W[i] = 0;
+}
+
+
+void SHA1_Update(SHA_CTX *ctx, const void *_dataIn, int len) {
+ const unsigned char *dataIn = _dataIn;
+ int i;
+
+ /* Read the data into W and process blocks as they get full
+ */
+ for (i = 0; i < len; i++) {
+ ctx->W[ctx->lenW / 4] <<= 8;
+ ctx->W[ctx->lenW / 4] |= (unsigned int)dataIn[i];
+ if ((++ctx->lenW) % 64 == 0) {
+ shaHashBlock(ctx);
+ ctx->lenW = 0;
+ }
+ ctx->sizeLo += 8;
+ ctx->sizeHi += (ctx->sizeLo < 8);
+ }
+}
+
+
+void SHA1_Final(unsigned char hashout[20], SHA_CTX *ctx) {
+ unsigned char pad0x80 = 0x80;
+ unsigned char pad0x00 = 0x00;
+ unsigned char padlen[8];
+ int i;
+
+ /* Pad with a binary 1 (e.g. 0x80), then zeroes, then length
+ */
+ padlen[0] = (unsigned char)((ctx->sizeHi >> 24) & 255);
+ padlen[1] = (unsigned char)((ctx->sizeHi >> 16) & 255);
+ padlen[2] = (unsigned char)((ctx->sizeHi >> 8) & 255);
+ padlen[3] = (unsigned char)((ctx->sizeHi >> 0) & 255);
+ padlen[4] = (unsigned char)((ctx->sizeLo >> 24) & 255);
+ padlen[5] = (unsigned char)((ctx->sizeLo >> 16) & 255);
+ padlen[6] = (unsigned char)((ctx->sizeLo >> 8) & 255);
+ padlen[7] = (unsigned char)((ctx->sizeLo >> 0) & 255);
+ SHA1_Update(ctx, &pad0x80, 1);
+ while (ctx->lenW != 56)
+ SHA1_Update(ctx, &pad0x00, 1);
+ SHA1_Update(ctx, padlen, 8);
+
+ /* Output hash
+ */
+ for (i = 0; i < 20; i++) {
+ hashout[i] = (unsigned char)(ctx->H[i / 4] >> 24);
+ ctx->H[i / 4] <<= 8;
+ }
+
+ /*
+ * Re-initialize the context (also zeroizes contents)
+ */
+ SHA1_Init(ctx);
+}
+
+
+#define SHA_ROT(X,n) (((X) << (n)) | ((X) >> (32-(n))))
+
+static void shaHashBlock(SHA_CTX *ctx) {
+ int t;
+ unsigned int A,B,C,D,E,TEMP;
+
+ for (t = 16; t <= 79; t++)
+ ctx->W[t] =
+ SHA_ROT(ctx->W[t-3] ^ ctx->W[t-8] ^ ctx->W[t-14] ^ ctx->W[t-16], 1);
+
+ A = ctx->H[0];
+ B = ctx->H[1];
+ C = ctx->H[2];
+ D = ctx->H[3];
+ E = ctx->H[4];
+
+ for (t = 0; t <= 19; t++) {
+ TEMP = SHA_ROT(A,5) + (((C^D)&B)^D) + E + ctx->W[t] + 0x5a827999;
+ E = D; D = C; C = SHA_ROT(B, 30); B = A; A = TEMP;
+ }
+ for (t = 20; t <= 39; t++) {
+ TEMP = SHA_ROT(A,5) + (B^C^D) + E + ctx->W[t] + 0x6ed9eba1;
+ E = D; D = C; C = SHA_ROT(B, 30); B = A; A = TEMP;
+ }
+ for (t = 40; t <= 59; t++) {
+ TEMP = SHA_ROT(A,5) + ((B&C)|(D&(B|C))) + E + ctx->W[t] + 0x8f1bbcdc;
+ E = D; D = C; C = SHA_ROT(B, 30); B = A; A = TEMP;
+ }
+ for (t = 60; t <= 79; t++) {
+ TEMP = SHA_ROT(A,5) + (B^C^D) + E + ctx->W[t] + 0xca62c1d6;
+ E = D; D = C; C = SHA_ROT(B, 30); B = A; A = TEMP;
+ }
+
+ ctx->H[0] += A;
+ ctx->H[1] += B;
+ ctx->H[2] += C;
+ ctx->H[3] += D;
+ ctx->H[4] += E;
+}
diff --git a/src/sha1/sha1.h b/src/sha1/sha1.h
new file mode 100644
index 0000000..cb5245e
--- /dev/null
+++ b/src/sha1/sha1.h
@@ -0,0 +1,64 @@
+/*
+ * The contents of this file are subject to the Mozilla Public
+ * License Version 1.1 (the "License"); you may not use this file
+ * except in compliance with the License. You may obtain a copy of
+ * the License at http://www.mozilla.org/MPL/
+ *
+ * Software distributed under the License is distributed on an "AS
+ * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
+ * implied. See the License for the specific language governing
+ * rights and limitations under the License.
+ *
+ * The Original Code is SHA 180-1 Header File
+ *
+ * The Initial Developer of the Original Code is Paul Kocher of
+ * Cryptography Research. Portions created by Paul Kocher are
+ * Copyright (C) 1995-9 by Cryptography Research, Inc. All
+ * Rights Reserved.
+ *
+ * Contributor(s):
+ *
+ * Paul Kocher
+ *
+ * Alternatively, the contents of this file may be used under the
+ * terms of the GNU General Public License, version 2, (the "GPL")
+ * as published by the Free Software Foundation.
+ *
+ * In addition to the permissions in the GNU General Public License,
+ * the authors give you unlimited permission to link the compiled
+ * version of this file into combinations with other programs,
+ * and to distribute those combinations without any restriction
+ * coming from the use of this file. (The General Public License
+ * restrictions do apply in other respects; for example, they cover
+ * modification of the file, and distribution when not linked into
+ * a combined executable.)
+ *
+ * This file is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; see the file COPYING. If not, write to
+ * the Free Software Foundation, 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+ *
+ * If you wish to allow use of your version of this file only under
+ * the terms of the GPL and not to allow others to use your version
+ * of this file under the MPL, indicate your decision by deleting
+ * the provisions above and replace them with the notice and other
+ * provisions required by the GPL. If you do not delete the
+ * provisions above, a recipient may use your version of this file
+ * under either the MPL or the GPL.
+ */
+
+typedef struct {
+ unsigned int H[5];
+ unsigned int W[80];
+ int lenW;
+ unsigned int sizeHi,sizeLo;
+} SHA_CTX;
+
+void SHA1_Init(SHA_CTX *ctx);
+void SHA1_Update(SHA_CTX *ctx, const void *dataIn, int len);
+void SHA1_Final(unsigned char hashout[20], SHA_CTX *ctx);