Back to index

lightning-sunbird  0.9+nobinonly
sha.c
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is SHA 180-1 Reference Implementation (Compact version).
00015  *
00016  * The Initial Developer of the Original Code is
00017  * Paul Kocher of Cryptography Research.
00018  * Portions created by the Initial Developer are Copyright (C) 1995-9
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *
00023  * Alternatively, the contents of this file may be used under the terms of
00024  * either the GNU General Public License Version 2 or later (the "GPL"), or
00025  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00026  * in which case the provisions of the GPL or the LGPL are applicable instead
00027  * of those above. If you wish to allow use of your version of this file only
00028  * under the terms of either the GPL or the LGPL, and not to allow others to
00029  * use your version of this file under the terms of the MPL, indicate your
00030  * decision by deleting the provisions above and replace them with the notice
00031  * and other provisions required by the GPL or the LGPL. If you do not delete
00032  * the provisions above, a recipient may use your version of this file under
00033  * the terms of any one of the MPL, the GPL or the LGPL.
00034  *
00035  * ***** END LICENSE BLOCK ***** */
00036 
00037 #include "sha.h"
00038 
00039 static void shaHashBlock(SHA_CTX *ctx);
00040 
00041 void shaInit(SHA_CTX *ctx) {
00042   int i;
00043 
00044   ctx->lenW = 0;
00045   ctx->sizeHi = ctx->sizeLo = 0;
00046 
00047   /* Initialize H with the magic constants (see FIPS180 for constants)
00048    */
00049   ctx->H[0] = 0x67452301L;
00050   ctx->H[1] = 0xefcdab89L;
00051   ctx->H[2] = 0x98badcfeL;
00052   ctx->H[3] = 0x10325476L;
00053   ctx->H[4] = 0xc3d2e1f0L;
00054 
00055   for (i = 0; i < 80; i++)
00056     ctx->W[i] = 0;
00057 }
00058 
00059 
00060 void shaUpdate(SHA_CTX *ctx, unsigned char *dataIn, int len) {
00061   int i;
00062 
00063   /* Read the data into W and process blocks as they get full
00064    */
00065   for (i = 0; i < len; i++) {
00066     ctx->W[ctx->lenW / 4] <<= 8;
00067     ctx->W[ctx->lenW / 4] |= (unsigned long)dataIn[i];
00068     if ((++ctx->lenW) % 64 == 0) {
00069       shaHashBlock(ctx);
00070       ctx->lenW = 0;
00071     }
00072     ctx->sizeLo += 8;
00073     ctx->sizeHi += (ctx->sizeLo < 8);
00074   }
00075 }
00076 
00077 
00078 void shaFinal(SHA_CTX *ctx, unsigned char hashout[20]) {
00079   unsigned char pad0x80 = 0x80;
00080   unsigned char pad0x00 = 0x00;
00081   unsigned char padlen[8];
00082   int i;
00083 
00084   /* Pad with a binary 1 (e.g. 0x80), then zeroes, then length
00085    */
00086   padlen[0] = (unsigned char)((ctx->sizeHi >> 24) & 255);
00087   padlen[1] = (unsigned char)((ctx->sizeHi >> 16) & 255);
00088   padlen[2] = (unsigned char)((ctx->sizeHi >> 8) & 255);
00089   padlen[3] = (unsigned char)((ctx->sizeHi >> 0) & 255);
00090   padlen[4] = (unsigned char)((ctx->sizeLo >> 24) & 255);
00091   padlen[5] = (unsigned char)((ctx->sizeLo >> 16) & 255);
00092   padlen[6] = (unsigned char)((ctx->sizeLo >> 8) & 255);
00093   padlen[7] = (unsigned char)((ctx->sizeLo >> 0) & 255);
00094   shaUpdate(ctx, &pad0x80, 1);
00095   while (ctx->lenW != 56)
00096     shaUpdate(ctx, &pad0x00, 1);
00097   shaUpdate(ctx, padlen, 8);
00098 
00099   /* Output hash
00100    */
00101   for (i = 0; i < 20; i++) {
00102     hashout[i] = (unsigned char)(ctx->H[i / 4] >> 24);
00103     ctx->H[i / 4] <<= 8;
00104   }
00105 
00106   /*
00107    *  Re-initialize the context (also zeroizes contents)
00108    */
00109   shaInit(ctx);
00110 }
00111 
00112 
00113 void shaBlock(unsigned char *dataIn, int len, unsigned char hashout[20]) {
00114   SHA_CTX ctx;
00115 
00116   shaInit(&ctx);
00117   shaUpdate(&ctx, dataIn, len);
00118   shaFinal(&ctx, hashout);
00119 }
00120 
00121 
00122 #define SHA_ROTL(X,n) (((X) << (n)) | ((X) >> (32-(n))))
00123 
00124 static void shaHashBlock(SHA_CTX *ctx) {
00125   int t;
00126   unsigned long A,B,C,D,E,TEMP;
00127 
00128   for (t = 16; t <= 79; t++)
00129     ctx->W[t] =
00130       SHA_ROTL(ctx->W[t-3] ^ ctx->W[t-8] ^ ctx->W[t-14] ^ ctx->W[t-16], 1);
00131 
00132   A = ctx->H[0];
00133   B = ctx->H[1];
00134   C = ctx->H[2];
00135   D = ctx->H[3];
00136   E = ctx->H[4];
00137 
00138   for (t = 0; t <= 19; t++) {
00139     TEMP = SHA_ROTL(A,5) + (((C^D)&B)^D)     + E + ctx->W[t] + 0x5a827999L;
00140     E = D; D = C; C = SHA_ROTL(B, 30); B = A; A = TEMP;
00141   }
00142   for (t = 20; t <= 39; t++) {
00143     TEMP = SHA_ROTL(A,5) + (B^C^D)           + E + ctx->W[t] + 0x6ed9eba1L;
00144     E = D; D = C; C = SHA_ROTL(B, 30); B = A; A = TEMP;
00145   }
00146   for (t = 40; t <= 59; t++) {
00147     TEMP = SHA_ROTL(A,5) + ((B&C)|(D&(B|C))) + E + ctx->W[t] + 0x8f1bbcdcL;
00148     E = D; D = C; C = SHA_ROTL(B, 30); B = A; A = TEMP;
00149   }
00150   for (t = 60; t <= 79; t++) {
00151     TEMP = SHA_ROTL(A,5) + (B^C^D)           + E + ctx->W[t] + 0xca62c1d6L;
00152     E = D; D = C; C = SHA_ROTL(B, 30); B = A; A = TEMP;
00153   }
00154 
00155   ctx->H[0] += A;
00156   ctx->H[1] += B;
00157   ctx->H[2] += C;
00158   ctx->H[3] += D;
00159   ctx->H[4] += E;
00160 }
00161