Jabber WindowGram Client (JWGC)

Introduction Screenshots Installation Downloads
Documentation Browse Source Resources Project Site

Stable Version
-none-

Latest Version
beta5



Main Page | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

JSha.c

Go to the documentation of this file.
00001 /*
00002  * The contents of this file are subject to the Mozilla Public License
00003  * Version 1.1 (the "License"); you may not use this file except in
00004  * compliance with the License. You may obtain a copy of the License at
00005  * http://www.mozilla.org/MPL/
00006  * 
00007  * Software distributed under the License is distributed on an "AS IS" basis,
00008  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00009  * for the specific language governing rights and limitations under the
00010  * 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 Cryptography
00015  * Research.  Portions created by Paul Kocher are Copyright (C) 1995-9 by
00016  * Cryptography Research, Inc.  All Rights Reserved.
00017  * 
00018  */
00019 
00020 /* $Id: JSha.c,v 1.2 2003/09/27 02:47:38 jadestorm Exp $ */
00021 
00022 #include "libjwgc.h"
00023 #include "libxode.h"
00024 
00025 static void j_shaHashBlock(j_SHA_CTX * ctx);
00026 
00027 void 
00028 j_shaInit(j_SHA_CTX * ctx)
00029 {
00030         int i;
00031 
00032         ctx->lenW = 0;
00033         ctx->sizeHi = ctx->sizeLo = 0;
00034 
00035         /*
00036          * Initialize H with the magic constants (see FIPS180 for constants)
00037          */
00038         ctx->H[0] = 0x67452301L;
00039         ctx->H[1] = 0xefcdab89L;
00040         ctx->H[2] = 0x98badcfeL;
00041         ctx->H[3] = 0x10325476L;
00042         ctx->H[4] = 0xc3d2e1f0L;
00043 
00044         for (i = 0; i < 80; i++)
00045                 ctx->W[i] = 0;
00046 }
00047 
00048 
00049 void 
00050 j_shaUpdate(j_SHA_CTX * ctx, unsigned char *dataIn, int len)
00051 {
00052         int i;
00053 
00054         /*
00055          * Read the data into W and process blocks as they get full
00056          */
00057         for (i = 0; i < len; i++) {
00058                 ctx->W[ctx->lenW / 4] <<= 8;
00059                 ctx->W[ctx->lenW / 4] |= (unsigned long) dataIn[i];
00060                 if ((++ctx->lenW) % 64 == 0) {
00061                         j_shaHashBlock(ctx);
00062                         ctx->lenW = 0;
00063                 }
00064                 ctx->sizeLo += 8;
00065                 ctx->sizeHi += (ctx->sizeLo < 8);
00066         }
00067 }
00068 
00069 
00070 void 
00071 j_shaFinal(j_SHA_CTX * ctx, unsigned char hashout[20])
00072 {
00073         unsigned char pad0x80 = 0x80;
00074         unsigned char pad0x00 = 0x00;
00075         unsigned char padlen[8];
00076         int i;
00077 
00078         /*
00079          * Pad with a binary 1 (e.g. 0x80), then zeroes, then length
00080          */
00081         padlen[0] = (unsigned char) ((ctx->sizeHi >> 24) & 255);
00082         padlen[1] = (unsigned char) ((ctx->sizeHi >> 16) & 255);
00083         padlen[2] = (unsigned char) ((ctx->sizeHi >> 8) & 255);
00084         padlen[3] = (unsigned char) ((ctx->sizeHi >> 0) & 255);
00085         padlen[4] = (unsigned char) ((ctx->sizeLo >> 24) & 255);
00086         padlen[5] = (unsigned char) ((ctx->sizeLo >> 16) & 255);
00087         padlen[6] = (unsigned char) ((ctx->sizeLo >> 8) & 255);
00088         padlen[7] = (unsigned char) ((ctx->sizeLo >> 0) & 255);
00089         j_shaUpdate(ctx, &pad0x80, 1);
00090         while (ctx->lenW != 56)
00091                 j_shaUpdate(ctx, &pad0x00, 1);
00092         j_shaUpdate(ctx, padlen, 8);
00093 
00094         /*
00095          * Output hash
00096          */
00097         for (i = 0; i < 20; i++) {
00098                 hashout[i] = (unsigned char) (ctx->H[i / 4] >> 24);
00099                 ctx->H[i / 4] <<= 8;
00100         }
00101 
00102         /*
00103          *  Re-initialize the context (also zeroizes contents)
00104          */
00105         j_shaInit(ctx);
00106 }
00107 
00108 
00109 void 
00110 j_shaBlock(unsigned char *dataIn, int len, unsigned char hashout[20])
00111 {
00112         j_SHA_CTX ctx;
00113 
00114         j_shaInit(&ctx);
00115         j_shaUpdate(&ctx, dataIn, len);
00116         j_shaFinal(&ctx, hashout);
00117 }
00118 
00119 
00120 #define SHA_ROTL(X,n) ((((X) << (n)) | ((X) >> (32-(n)))) & 0xffffffffL)
00121 
00122 static void 
00123 j_shaHashBlock(j_SHA_CTX * ctx)
00124 {
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) & 0xffffffffL;
00140                 E = D;
00141                 D = C;
00142                 C = SHA_ROTL(B, 30);
00143                 B = A;
00144                 A = TEMP;
00145         }
00146         for (t = 20; t <= 39; t++) {
00147                 TEMP = (SHA_ROTL(A, 5) + (B ^ C ^ D) + E + ctx->W[t] + 0x6ed9eba1L) & 0xffffffffL;
00148                 E = D;
00149                 D = C;
00150                 C = SHA_ROTL(B, 30);
00151                 B = A;
00152                 A = TEMP;
00153         }
00154         for (t = 40; t <= 59; t++) {
00155                 TEMP = (SHA_ROTL(A, 5) + ((B & C) | (D & (B | C))) + E + ctx->W[t] + 0x8f1bbcdcL) & 0xffffffffL;
00156                 E = D;
00157                 D = C;
00158                 C = SHA_ROTL(B, 30);
00159                 B = A;
00160                 A = TEMP;
00161         }
00162         for (t = 60; t <= 79; t++) {
00163                 TEMP = (SHA_ROTL(A, 5) + (B ^ C ^ D) + E + ctx->W[t] + 0xca62c1d6L) & 0xffffffffL;
00164                 E = D;
00165                 D = C;
00166                 C = SHA_ROTL(B, 30);
00167                 B = A;
00168                 A = TEMP;
00169         }
00170 
00171         ctx->H[0] += A;
00172         ctx->H[1] += B;
00173         ctx->H[2] += C;
00174         ctx->H[3] += D;
00175         ctx->H[4] += E;
00176 }
00177 
00178 /*----------------------------------------------------------------------------
00179  *
00180  * This code added by Thomas "temas" Muldowney for Jabber compatability
00181  *
00182  *---------------------------------------------------------------------------*/
00183 char *
00184 j_shahash(char *str)
00185 {
00186         static char final[41];
00187         char *pos;
00188         unsigned char hashval[20];
00189         int x;
00190 
00191         if (!str || strlen(str) == 0)
00192                 return NULL;
00193 
00194         j_shaBlock((unsigned char *) str, strlen(str), hashval);
00195 
00196         pos = final;
00197         for (x = 0; x < 20; x++) {
00198                 snprintf(pos, 3, "%02x", hashval[x]);
00199                 pos += 2;
00200         }
00201         return (char *) final;
00202 }
00203 
00204 void 
00205 j_shahash_r(const char *str, char hashbuf[41])
00206 {
00207         int x;
00208         char *pos;
00209         unsigned char hashval[20];
00210 
00211         if (!str || strlen(str) == 0)
00212                 return;
00213 
00214         j_shaBlock((unsigned char *) str, strlen(str), hashval);
00215 
00216         pos = hashbuf;
00217         for (x = 0; x < 20; x++) {
00218                 snprintf(pos, 3, "%02x", hashval[x]);
00219                 pos += 2;
00220         }
00221 
00222         return;
00223 }


Last updated at Tue Dec 18 21:07:42 PST 2007. This site and project hosted by...SourceForge.net Logo
Source Perspective by Fisheye