Main Page   Alphabetical List   Data Structures   File List   Data Fields   Globals   Examples  

sha.c

Go to the documentation of this file.
00001 /*! \file sha.c
00002     This file implements the Secure Hash Algorithm.
00003  */
00004 
00005 /* 
00006  * The contents of this file are subject to the Mozilla Public
00007  * License Version 1.1 (the "License"); you may not use this file
00008  * except in compliance with the License. You may obtain a copy of
00009  * the License at http://www.mozilla.org/MPL/
00010  * 
00011  * Software distributed under the License is distributed on an "AS
00012  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
00013  * implied. See the License for the specific language governing
00014  * rights and limitations under the License.
00015  * 
00016  * The Original Code is SHA 180-1 Reference Implementation (Compact version)
00017  * 
00018  * The Initial Developer of the Original Code is Paul Kocher of
00019  * Cryptography Research.  Portions created by Paul Kocher are 
00020  * Copyright (C) 1995-9 by Cryptography Research, Inc.  All
00021  * Rights Reserved.
00022  * 
00023  * Contributor(s):
00024  *
00025  *     Paul Kocher
00026  * 
00027  * Alternatively, the contents of this file may be used under the
00028  * terms of the GNU General Public License Version 2 or later (the
00029  * "GPL"), in which case the provisions of the GPL are applicable 
00030  * instead of those above.  If you wish to allow use of your 
00031  * version of this file only under the terms of the GPL and not to
00032  * allow others to use your version of this file under the MPL,
00033  * indicate your decision by deleting the provisions above and
00034  * replace them with the notice and other provisions required by
00035  * the GPL.  If you do not delete the provisions above, a recipient
00036  * may use your version of this file under either the MPL or the
00037  * GPL.
00038  */
00039 
00040 #include <string.h>
00041 #include <stdio.h>
00042 #include "sha.h"
00043 
00044 static void shaHashBlock(SHA_CTX *ctx);
00045 
00046 void shaInit(SHA_CTX *ctx) {
00047   int i;
00048 
00049   ctx->lenW = 0;
00050   ctx->sizeHi = ctx->sizeLo = 0;
00051 
00052   /* Initialize H with the magic constants (see FIPS180 for constants)
00053    */
00054   ctx->H[0] = 0x67452301L;
00055   ctx->H[1] = 0xefcdab89L;
00056   ctx->H[2] = 0x98badcfeL;
00057   ctx->H[3] = 0x10325476L;
00058   ctx->H[4] = 0xc3d2e1f0L;
00059 
00060   for (i = 0; i < 80; i++)
00061     ctx->W[i] = 0;
00062 }
00063 
00064 
00065 void shaUpdate(SHA_CTX *ctx, unsigned char *dataIn, int len) {
00066   int i;
00067 
00068   /* Read the data into W and process blocks as they get full
00069    */
00070   for (i = 0; i < len; i++) {
00071     ctx->W[ctx->lenW / 4] <<= 8;
00072     ctx->W[ctx->lenW / 4] |= (unsigned long)dataIn[i];
00073     if ((++ctx->lenW) % 64 == 0) {
00074       shaHashBlock(ctx);
00075       ctx->lenW = 0;
00076     }
00077     ctx->sizeLo += 8;
00078     ctx->sizeHi += (ctx->sizeLo < 8);
00079   }
00080 }
00081 
00082 
00083 void shaFinal(SHA_CTX *ctx, unsigned char hashout[20]) {
00084   unsigned char pad0x80 = 0x80;
00085   unsigned char pad0x00 = 0x00;
00086   unsigned char padlen[8];
00087   int i;
00088 
00089   /* Pad with a binary 1 (e.g. 0x80), then zeroes, then length
00090    */
00091   padlen[0] = (unsigned char)((ctx->sizeHi >> 24) & 255);
00092   padlen[1] = (unsigned char)((ctx->sizeHi >> 16) & 255);
00093   padlen[2] = (unsigned char)((ctx->sizeHi >> 8) & 255);
00094   padlen[3] = (unsigned char)((ctx->sizeHi >> 0) & 255);
00095   padlen[4] = (unsigned char)((ctx->sizeLo >> 24) & 255);
00096   padlen[5] = (unsigned char)((ctx->sizeLo >> 16) & 255);
00097   padlen[6] = (unsigned char)((ctx->sizeLo >> 8) & 255);
00098   padlen[7] = (unsigned char)((ctx->sizeLo >> 0) & 255);
00099   shaUpdate(ctx, &pad0x80, 1);
00100   while (ctx->lenW != 56)
00101     shaUpdate(ctx, &pad0x00, 1);
00102   shaUpdate(ctx, padlen, 8);
00103 
00104   /* Output hash
00105    */
00106   for (i = 0; i < 20; i++) {
00107     hashout[i] = (unsigned char)(ctx->H[i / 4] >> 24);
00108     ctx->H[i / 4] <<= 8;
00109   }
00110 
00111   /*
00112    *  Re-initialize the context (also zeroizes contents)
00113    */
00114   shaInit(ctx);
00115 }
00116 
00117 
00118 void shaBlock(unsigned char *dataIn, int len, unsigned char hashout[20]) {
00119   SHA_CTX ctx;
00120 
00121   shaInit(&ctx);
00122   shaUpdate(&ctx, dataIn, len);
00123   shaFinal(&ctx, hashout);
00124 }
00125 
00126 
00127 #define SHA_ROTL(X,n) (((X) << (n)) | ((X) >> (32-(n))))
00128 
00129 static void shaHashBlock(SHA_CTX *ctx) {
00130   int t;
00131   unsigned long A,B,C,D,E,TEMP;
00132 
00133   for (t = 16; t <= 79; t++)
00134     ctx->W[t] =
00135       SHA_ROTL(ctx->W[t-3] ^ ctx->W[t-8] ^ ctx->W[t-14] ^ ctx->W[t-16], 1);
00136 
00137   A = ctx->H[0];
00138   B = ctx->H[1];
00139   C = ctx->H[2];
00140   D = ctx->H[3];
00141   E = ctx->H[4];
00142 
00143   for (t = 0; t <= 19; t++) {
00144     TEMP = SHA_ROTL(A,5) + (((C^D)&B)^D)     + E + ctx->W[t] + 0x5a827999L;
00145     E = D; D = C; C = SHA_ROTL(B, 30); B = A; A = TEMP;
00146   }
00147   for (t = 20; t <= 39; t++) {
00148     TEMP = SHA_ROTL(A,5) + (B^C^D)           + E + ctx->W[t] + 0x6ed9eba1L;
00149     E = D; D = C; C = SHA_ROTL(B, 30); B = A; A = TEMP;
00150   }
00151   for (t = 40; t <= 59; t++) {
00152     TEMP = SHA_ROTL(A,5) + ((B&C)|(D&(B|C))) + E + ctx->W[t] + 0x8f1bbcdcL;
00153     E = D; D = C; C = SHA_ROTL(B, 30); B = A; A = TEMP;
00154   }
00155   for (t = 60; t <= 79; t++) {
00156     TEMP = SHA_ROTL(A,5) + (B^C^D)           + E + ctx->W[t] + 0xca62c1d6L;
00157     E = D; D = C; C = SHA_ROTL(B, 30); B = A; A = TEMP;
00158   }
00159 
00160   ctx->H[0] += A;
00161   ctx->H[1] += B;
00162   ctx->H[2] += C;
00163   ctx->H[3] += D;
00164   ctx->H[4] += E;
00165 }
00166 
00167 /*----------------------------------------------------------------------------
00168  *
00169  * This code added by Thomas "temas" Muldowney for Jabber compatability
00170  *
00171  *---------------------------------------------------------------------------*/
00172 char *shahash(char *str)
00173 {
00174     static char final[41];
00175     char *pos;
00176     unsigned char hashval[20];
00177     int x;
00178 
00179     if(!str || strlen(str) == 0)
00180         return NULL;
00181 
00182     shaBlock((unsigned char *)str, strlen(str), hashval);
00183 
00184     pos = final;
00185     for(x=0;x<20;x++)
00186     {
00187         snprintf(pos, 3, "%02x", hashval[x]);
00188         pos += 2;
00189     }
00190     return (char *)final;
00191 }
00192 
00193 
00194 /*! \brief Calculate the SHA of a string.
00195     \param str Pointer to a NULL terminated string of characters that you want to calculate the SHA.
00196     \param hashbuf Pointer to a memory locattion that will receive the SHA calculated from 'str'. Please note that 
00197                    'hashbuf' should be at least 41 bytes long (40 bytes for the SHA result and 1 byte for the final 0).
00198  */
00199 
00200 void shahash_r(const char* str, char hashbuf[41])
00201 {
00202     int x;
00203     char *pos;
00204     unsigned char hashval[20];
00205     
00206     if(!str || strlen(str) == 0)
00207         return;
00208 
00209     shaBlock((unsigned char *)str, strlen(str), hashval);
00210 
00211     pos = hashbuf;
00212     for(x=0;x<20;x++)
00213     {
00214         snprintf(pos, 3, "%02x", hashval[x]);
00215         pos += 2;
00216     }
00217 
00218     return;
00219 }
00220 
00221 /*! \example test_sha.c
00222     This file shows how to use the function shahash_r().
00223  */

Generated on Thu Apr 3 16:23:43 2003 for Common_C_libraries by doxygen1.3-rc1