# sha1.cc

    00001 /*
    00002  * The contents of this file are subject to the Mozilla Public
    00003  * License Version 1.1 (the "License"); you may not use this file
    00004  * except in compliance with the License. You may obtain a copy of
    00005  * the License at http://www.mozilla.org/MPL/
    00006  *
    00007  * Software distributed under the License is distributed on an "AS
    00008  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
    00009  * implied. See the License for the specific language governing
    00010  * rights and limitations under the License.
    00011  *
    00012  * The Original Code is SHA 180-1 Reference Implementation (Compact version)
    00013  *
    00014  * The Initial Developer of the Original Code is Paul Kocher of
    00015  * Cryptography Research.  Portions created by Paul Kocher are
    00016  * Copyright (C) 1995-9 by Cryptography Research, Inc.  All
    00017  * Rights Reserved.
    00018  *
    00019  * Contributor(s):
    00020  *
    00021  *     Paul Kocher
    00022  *
    00023  * Alternatively, the contents of this file may be used under the
    00024  * terms of the GNU General Public License Version 2 or later (the
    00025  * "GPL"), in which case the provisions of the GPL are applicable
    00026  * instead of those above.  If you wish to allow use of your
    00027  * version of this file only under the terms of the GPL and not to
    00028  * allow others to use your version of this file under the MPL,
    00029  * indicate your decision by deleting the provisions above and
    00030  * replace them with the notice and other provisions required by
    00031  * the GPL.  If you do not delete the provisions above, a recipient
    00032  * may use your version of this file under either the MPL or the
    00033  * GPL.
    00034  */
    00035
    00036 /*
    00037 Copied from the git sources, with the following revision history:
    00038
    00039 commit 77ab8798d3f8df39877235be17bb6e70077aaba2
    00040 Author: Junio C Hamano <junkio@cox.net>
    00041 Date:   Tue Nov 1 10:56:03 2005 -0800
    00042
    00043     Fix constness of input in mozilla-sha1/sha1.c::SHA1_Update().
    00044
    00045     Among the three of our own implementations, only this one lacked
    00046     "const" from the second argument.
    00047
    00048     Signed-off-by: Junio C Hamano <junkio@cox.net>
    00049
    00050 commit cef661fc799a3a13ffdea4a3f69f1acd295de53d
    00051 Author: Linus Torvalds <torvalds@ppc970.osdl.org>
    00052 Date:   Thu Apr 21 12:33:22 2005 -0700
    00053
    00054     Add support for alternate SHA1 library implementations.
    00055
    00056     This one includes the Mozilla SHA1 implementation sent in by Edgar Toernig.
    00057     It's dual-licenced under MPL-1.1 or GPL, so in the context of git, we
    00058     obviously use the GPL version.
    00059
    00060     Side note: the Mozilla SHA1 implementation is about twice as fast as the
    00061     default openssl one on my G5, but the default openssl one has optimized
    00062     x86 assembly language on x86. So choose wisely.
    00063
    00064 */
    00065
    00066 #include "sha1.h"
    00067
    00068 namespace Barry {
    00069
    00070 static void shaHashBlock(SHA_CTX *ctx);
    00071
    00072 void SHA1(const void *dataIn, int len, unsigned char *hashout)
    00073 {
    00074         SHA_CTX ctx;
    00075         SHA1_Init(&ctx);
    00076         SHA1_Update(&ctx, dataIn, len);
    00077         SHA1_Final(hashout, &ctx);
    00078 }
    00079
    00080 void SHA1_Init(SHA_CTX *ctx) {
    00081   int i;
    00082
    00083   ctx->lenW = 0;
    00084   ctx->sizeHi = ctx->sizeLo = 0;
    00085
    00086   /* Initialize H with the magic constants (see FIPS180 for constants)
    00087    */
    00088   ctx->H[0] = 0x67452301;
    00089   ctx->H[1] = 0xefcdab89;
    00090   ctx->H[2] = 0x98badcfe;
    00091   ctx->H[3] = 0x10325476;
    00092   ctx->H[4] = 0xc3d2e1f0;
    00093
    00094   for (i = 0; i < 80; i++)
    00095     ctx->W[i] = 0;
    00096 }
    00097
    00098
    00099 void SHA1_Update(SHA_CTX *ctx, const void *_dataIn, int len) {
    00100   const unsigned char *dataIn = (const unsigned char *) _dataIn;
    00101   int i;
    00102
    00103   /* Read the data into W and process blocks as they get full
    00104    */
    00105   for (i = 0; i < len; i++) {
    00106     ctx->W[ctx->lenW / 4] <<= 8;
    00107     ctx->W[ctx->lenW / 4] |= (unsigned int)dataIn[i];
    00108     if ((++ctx->lenW) % 64 == 0) {
    00109       shaHashBlock(ctx);
    00110       ctx->lenW = 0;
    00111     }
    00112     ctx->sizeLo += 8;
    00113     ctx->sizeHi += (ctx->sizeLo < 8);
    00114   }
    00115 }
    00116
    00117
    00118 void SHA1_Final(unsigned char hashout[20], SHA_CTX *ctx) {
    00119   unsigned char pad0x80 = 0x80;
    00120   unsigned char pad0x00 = 0x00;
    00121   unsigned char padlen[8];
    00122   int i;
    00123
    00124   /* Pad with a binary 1 (e.g. 0x80), then zeroes, then length
    00125    */
    00126   padlen[0] = (unsigned char)((ctx->sizeHi >> 24) & 255);
    00127   padlen[1] = (unsigned char)((ctx->sizeHi >> 16) & 255);
    00128   padlen[2] = (unsigned char)((ctx->sizeHi >> 8) & 255);
    00129   padlen[3] = (unsigned char)((ctx->sizeHi >> 0) & 255);
    00130   padlen[4] = (unsigned char)((ctx->sizeLo >> 24) & 255);
    00131   padlen[5] = (unsigned char)((ctx->sizeLo >> 16) & 255);
    00132   padlen[6] = (unsigned char)((ctx->sizeLo >> 8) & 255);
    00133   padlen[7] = (unsigned char)((ctx->sizeLo >> 0) & 255);
    00134   SHA1_Update(ctx, &pad0x80, 1);
    00135   while (ctx->lenW != 56)
    00136     SHA1_Update(ctx, &pad0x00, 1);
    00137   SHA1_Update(ctx, padlen, 8);
    00138
    00139   /* Output hash
    00140    */
    00141   for (i = 0; i < 20; i++) {
    00142     hashout[i] = (unsigned char)(ctx->H[i / 4] >> 24);
    00143     ctx->H[i / 4] <<= 8;
    00144   }
    00145
    00146   /*
    00147    *  Re-initialize the context (also zeroizes contents)
    00148    */
    00149   SHA1_Init(ctx);
    00150 }
    00151
    00152
    00153 #define SHA_ROT(X,n) (((X) << (n)) | ((X) >> (32-(n))))
    00154
    00155 static void shaHashBlock(SHA_CTX *ctx) {
    00156   int t;
    00157   unsigned int A,B,C,D,E,TEMP;
    00158
    00159   for (t = 16; t <= 79; t++)
    00160     ctx->W[t] =
    00161       SHA_ROT(ctx->W[t-3] ^ ctx->W[t-8] ^ ctx->W[t-14] ^ ctx->W[t-16], 1);
    00162
    00163   A = ctx->H[0];
    00164   B = ctx->H[1];
    00165   C = ctx->H[2];
    00166   D = ctx->H[3];
    00167   E = ctx->H[4];
    00168
    00169   for (t = 0; t <= 19; t++) {
    00170     TEMP = SHA_ROT(A,5) + (((C^D)&B)^D)     + E + ctx->W[t] + 0x5a827999;
    00171     E = D; D = C; C = SHA_ROT(B, 30); B = A; A = TEMP;
    00172   }
    00173   for (t = 20; t <= 39; t++) {
    00174     TEMP = SHA_ROT(A,5) + (B^C^D)           + E + ctx->W[t] + 0x6ed9eba1;
    00175     E = D; D = C; C = SHA_ROT(B, 30); B = A; A = TEMP;
    00176   }
    00177   for (t = 40; t <= 59; t++) {
    00178     TEMP = SHA_ROT(A,5) + ((B&C)|(D&(B|C))) + E + ctx->W[t] + 0x8f1bbcdc;
    00179     E = D; D = C; C = SHA_ROT(B, 30); B = A; A = TEMP;
    00180   }
    00181   for (t = 60; t <= 79; t++) {
    00182     TEMP = SHA_ROT(A,5) + (B^C^D)           + E + ctx->W[t] + 0xca62c1d6;
    00183     E = D; D = C; C = SHA_ROT(B, 30); B = A; A = TEMP;
    00184   }
    00185
    00186   ctx->H[0] += A;
    00187   ctx->H[1] += B;
    00188   ctx->H[2] += C;
    00189   ctx->H[3] += D;
    00190   ctx->H[4] += E;
    00191 }
    00192
    00193 }
    00194