source: lib/sha.c @ a3a3778

Last change on this file since a3a3778 was df1694b, checked in by Wilmer van der Gaast <wilmer@…>, at 2006-06-25T12:15:42Z

Moving all generic files to lib/ instead of having some in / and some in
protocols/, and adding RC4 code.

  • Property mode set to 100644
File size: 4.5 KB
RevLine 
[b7d3cc34]1/*
2 * The contents of this file are subject to the Mozilla Public
3 * License Version 1.1 (the "License"); you may not use this file
4 * except in compliance with the License. You may obtain a copy of
5 * the License at http://www.mozilla.org/MPL/
6 *
7 * Software distributed under the License is distributed on an "AS
8 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
9 * implied. See the License for the specific language governing
10 * rights and limitations under the License.
11 *
12 * The Original Code is SHA 180-1 Reference Implementation (Compact version)
13 *
14 * The Initial Developer of the Original Code is Paul Kocher of
15 * Cryptography Research.  Portions created by Paul Kocher are
16 * Copyright (C) 1995-9 by Cryptography Research, Inc.  All
17 * Rights Reserved.
18 *
19 * Contributor(s):
20 *
21 */
22
23#define BITLBEE_CORE
24#include "nogaim.h"
25
26static void shaHashBlock(SHA_CTX *ctx);
27
28void shaInit(SHA_CTX *ctx) {
29  int i;
30
31  ctx->lenW = 0;
32  ctx->sizeHi = ctx->sizeLo = 0;
33
34  /* Initialize H with the magic constants (see FIPS180 for constants)
35   */
36  ctx->H[0] = 0x67452301L;
37  ctx->H[1] = 0xefcdab89L;
38  ctx->H[2] = 0x98badcfeL;
39  ctx->H[3] = 0x10325476L;
40  ctx->H[4] = 0xc3d2e1f0L;
41
42  for (i = 0; i < 80; i++)
43    ctx->W[i] = 0;
44}
45
46
47void shaUpdate(SHA_CTX *ctx, unsigned char *dataIn, int len) {
48  int i;
49
50  /* Read the data into W and process blocks as they get full
51   */
52  for (i = 0; i < len; i++) {
53    ctx->W[ctx->lenW / 4] <<= 8;
54    ctx->W[ctx->lenW / 4] |= (guint32)dataIn[i];
55    if ((++ctx->lenW) % 64 == 0) {
56      shaHashBlock(ctx);
57      ctx->lenW = 0;
58    }
59    ctx->sizeLo += 8;
60    ctx->sizeHi += (ctx->sizeLo < 8);
61  }
62}
63
64
65void shaFinal(SHA_CTX *ctx, unsigned char hashout[20]) {
66  unsigned char pad0x80 = 0x80;
67  unsigned char pad0x00 = 0x00;
68  unsigned char padlen[8];
69  int i;
70
71  /* Pad with a binary 1 (e.g. 0x80), then zeroes, then length
72   */
73  padlen[0] = (unsigned char)((ctx->sizeHi >> 24) & 255);
74  padlen[1] = (unsigned char)((ctx->sizeHi >> 16) & 255);
75  padlen[2] = (unsigned char)((ctx->sizeHi >> 8) & 255);
76  padlen[3] = (unsigned char)((ctx->sizeHi >> 0) & 255);
77  padlen[4] = (unsigned char)((ctx->sizeLo >> 24) & 255);
78  padlen[5] = (unsigned char)((ctx->sizeLo >> 16) & 255);
79  padlen[6] = (unsigned char)((ctx->sizeLo >> 8) & 255);
80  padlen[7] = (unsigned char)((ctx->sizeLo >> 0) & 255);
81  shaUpdate(ctx, &pad0x80, 1);
82  while (ctx->lenW != 56)
83    shaUpdate(ctx, &pad0x00, 1);
84  shaUpdate(ctx, padlen, 8);
85
86  /* Output hash
87   */
88  for (i = 0; i < 20; i++) {
89    hashout[i] = (unsigned char)(ctx->H[i / 4] >> 24);
90    ctx->H[i / 4] <<= 8;
91  }
92
93  /*
94   *  Re-initialize the context (also zeroizes contents)
95   */
96  shaInit(ctx); 
97}
98
99
100void shaBlock(unsigned char *dataIn, int len, unsigned char hashout[20]) {
101  SHA_CTX ctx;
102
103  shaInit(&ctx);
104  shaUpdate(&ctx, dataIn, len);
105  shaFinal(&ctx, hashout);
106}
107
108
109#define SHA_ROTL(X,n) ((((X) << (n)) | ((X) >> (32-(n)))) & 0xffffffffL)
110
111static void shaHashBlock(SHA_CTX *ctx) {
112  int t;
113  guint32 A,B,C,D,E,TEMP;
114
115  for (t = 16; t <= 79; t++)
116    ctx->W[t] =
117      SHA_ROTL(ctx->W[t-3] ^ ctx->W[t-8] ^ ctx->W[t-14] ^ ctx->W[t-16], 1);
118
119  A = ctx->H[0];
120  B = ctx->H[1];
121  C = ctx->H[2];
122  D = ctx->H[3];
123  E = ctx->H[4];
124
125  for (t = 0; t <= 19; t++) {
126    TEMP = (SHA_ROTL(A,5) + (((C^D)&B)^D)     + E + ctx->W[t] + 0x5a827999L) & 0xffffffffL;
127    E = D; D = C; C = SHA_ROTL(B, 30); B = A; A = TEMP;
128  }
129  for (t = 20; t <= 39; t++) {
130    TEMP = (SHA_ROTL(A,5) + (B^C^D)           + E + ctx->W[t] + 0x6ed9eba1L) & 0xffffffffL;
131    E = D; D = C; C = SHA_ROTL(B, 30); B = A; A = TEMP;
132  }
133  for (t = 40; t <= 59; t++) {
134    TEMP = (SHA_ROTL(A,5) + ((B&C)|(D&(B|C))) + E + ctx->W[t] + 0x8f1bbcdcL) & 0xffffffffL;
135    E = D; D = C; C = SHA_ROTL(B, 30); B = A; A = TEMP;
136  }
137  for (t = 60; t <= 79; t++) {
138    TEMP = (SHA_ROTL(A,5) + (B^C^D)           + E + ctx->W[t] + 0xca62c1d6L) & 0xffffffffL;
139    E = D; D = C; C = SHA_ROTL(B, 30); B = A; A = TEMP;
140  }
141
142  ctx->H[0] += A;
143  ctx->H[1] += B;
144  ctx->H[2] += C;
145  ctx->H[3] += D;
146  ctx->H[4] += E;
147}
148
149/*----------------------------------------------------------------------------
150 *
151 * This code added by Thomas "temas" Muldowney for Jabber compatability
152 *
153 *---------------------------------------------------------------------------*/
154char *shahash(char *str)
155{
156    static char final[41];
157    char *pos;
158    unsigned char hashval[20];
159    int x;
160
161    if(!str || strlen(str) == 0)
162        return NULL;
163
164    shaBlock((unsigned char *)str, strlen(str), hashval);
165
166    pos = final;
167    for(x=0;x<20;x++)
168    {
169        g_snprintf(pos, 3, "%02x", hashval[x]);
170        pos += 2;
171    }
172    return (char *)final;
173}
Note: See TracBrowser for help on using the repository browser.