jabberd2  2.6.1
sha1.c
Go to the documentation of this file.
1 #ifdef HAVE_CONFIG_H
2 # include <config.h>
3 #endif
4 #ifndef HAVE_SSL
5 #warning OpenSSL functions for sha1 not available
6 /*
7  * The contents of this file are subject to the Mozilla Public
8  * License Version 1.1 (the "License"); you may not use this file
9  * except in compliance with the License. You may obtain a copy of
10  * the License at http://www.mozilla.org/MPL/
11  *
12  * Software distributed under the License is distributed on an "AS
13  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
14  * implied. See the License for the specific language governing
15  * rights and limitations under the License.
16  *
17  * The Original Code is SHA 180-1 Reference Implementation (Compact version)
18  *
19  * The Initial Developer of the Original Code is Paul Kocher of
20  * Cryptography Research. Portions created by Paul Kocher are
21  * Copyright (C) 1995-9 by Cryptography Research, Inc. All
22  * Rights Reserved.
23  *
24  */
25 
26 /* modified for j2 by Robert Norris */
27 
28 #include "sha1.h"
29 #include <string.h>
30 
31 static void sha1_hashblock(sha1_state_t *ctx);
32 
33 void sha1_init(sha1_state_t *ctx) {
34  int i;
35 
36  ctx->lenW = 0;
37  ctx->sizeHi = ctx->sizeLo = 0;
38 
39  /* Initialize H with the magic constants (see FIPS180 for constants)
40  */
41  ctx->H[0] = 0x67452301L;
42  ctx->H[1] = 0xefcdab89L;
43  ctx->H[2] = 0x98badcfeL;
44  ctx->H[3] = 0x10325476L;
45  ctx->H[4] = 0xc3d2e1f0L;
46 
47  for (i = 0; i < 80; i++)
48  ctx->W[i] = 0;
49 }
50 
51 
52 void sha1_append(sha1_state_t *ctx, const unsigned char *dataIn, int len) {
53  int i;
54 
55  /* Read the data into W and process blocks as they get full
56  */
57  for (i = 0; i < len; i++) {
58  ctx->W[ctx->lenW / 4] <<= 8;
59  ctx->W[ctx->lenW / 4] |= (uint32_t)dataIn[i];
60  if ((++ctx->lenW) % 64 == 0) {
61  sha1_hashblock(ctx);
62  ctx->lenW = 0;
63  }
64  ctx->sizeLo += 8;
65  ctx->sizeHi += (ctx->sizeLo < 8);
66  }
67 }
68 
69 
70 void sha1_finish(sha1_state_t *ctx, unsigned char hashout[20]) {
71  unsigned char pad0x80 = 0x80;
72  unsigned char pad0x00 = 0x00;
73  unsigned char padlen[8];
74  int i;
75 
76  /* Pad with a binary 1 (e.g. 0x80), then zeroes, then length
77  */
78  padlen[0] = (unsigned char)((ctx->sizeHi >> 24) & 255);
79  padlen[1] = (unsigned char)((ctx->sizeHi >> 16) & 255);
80  padlen[2] = (unsigned char)((ctx->sizeHi >> 8) & 255);
81  padlen[3] = (unsigned char)((ctx->sizeHi >> 0) & 255);
82  padlen[4] = (unsigned char)((ctx->sizeLo >> 24) & 255);
83  padlen[5] = (unsigned char)((ctx->sizeLo >> 16) & 255);
84  padlen[6] = (unsigned char)((ctx->sizeLo >> 8) & 255);
85  padlen[7] = (unsigned char)((ctx->sizeLo >> 0) & 255);
86  sha1_append(ctx, &pad0x80, 1);
87  while (ctx->lenW != 56)
88  sha1_append(ctx, &pad0x00, 1);
89  sha1_append(ctx, padlen, 8);
90 
91  /* Output hash
92  */
93  for (i = 0; i < 20; i++) {
94  hashout[i] = (unsigned char)(ctx->H[i / 4] >> 24);
95  ctx->H[i / 4] <<= 8;
96  }
97 
98  /*
99  * Re-initialize the context (also zeroizes contents)
100  */
101  sha1_init(ctx);
102 }
103 
104 
105 void sha1_hash(const unsigned char *dataIn, int len, unsigned char hashout[20]) {
106  sha1_state_t ctx;
107 
108  sha1_init(&ctx);
109  sha1_append(&ctx, dataIn, len);
110  sha1_finish(&ctx, hashout);
111 }
112 
113 
114 #define SHA_ROTL(X,n) ((((X) << (n)) | ((X) >> (32-(n)))) & 0xffffffffL)
115 
116 static void sha1_hashblock(sha1_state_t *ctx) {
117  int t;
118  uint32_t A,B,C,D,E,TEMP;
119 
120  for (t = 16; t <= 79; t++)
121  ctx->W[t] =
122  SHA_ROTL(ctx->W[t-3] ^ ctx->W[t-8] ^ ctx->W[t-14] ^ ctx->W[t-16], 1);
123 
124  A = ctx->H[0];
125  B = ctx->H[1];
126  C = ctx->H[2];
127  D = ctx->H[3];
128  E = ctx->H[4];
129 
130  for (t = 0; t <= 19; t++) {
131  TEMP = (SHA_ROTL(A,5) + (((C^D)&B)^D) + E + ctx->W[t] + 0x5a827999L) & 0xffffffffL;
132  E = D; D = C; C = SHA_ROTL(B, 30); B = A; A = TEMP;
133  }
134  for (t = 20; t <= 39; t++) {
135  TEMP = (SHA_ROTL(A,5) + (B^C^D) + E + ctx->W[t] + 0x6ed9eba1L) & 0xffffffffL;
136  E = D; D = C; C = SHA_ROTL(B, 30); B = A; A = TEMP;
137  }
138  for (t = 40; t <= 59; t++) {
139  TEMP = (SHA_ROTL(A,5) + ((B&C)|(D&(B|C))) + E + ctx->W[t] + 0x8f1bbcdcL) & 0xffffffffL;
140  E = D; D = C; C = SHA_ROTL(B, 30); B = A; A = TEMP;
141  }
142  for (t = 60; t <= 79; t++) {
143  TEMP = (SHA_ROTL(A,5) + (B^C^D) + E + ctx->W[t] + 0xca62c1d6L) & 0xffffffffL;
144  E = D; D = C; C = SHA_ROTL(B, 30); B = A; A = TEMP;
145  }
146 
147  ctx->H[0] += A;
148  ctx->H[1] += B;
149  ctx->H[2] += C;
150  ctx->H[3] += D;
151  ctx->H[4] += E;
152 }
153 #endif /* HAVE_SSL */
uint32_t sizeHi
Definition: sha1.h:57
uint32_t H[5]
Definition: sha1.h:54
void sha1_init(sha1_state_t *ctx)
Definition: sha1.c:33
void sha1_finish(sha1_state_t *ctx, unsigned char hashout[20])
Definition: sha1.c:70
#define SHA_ROTL(X, n)
Definition: sha1.c:114
int lenW
Definition: sha1.h:56
void sha1_hash(const unsigned char *dataIn, int len, unsigned char hashout[20])
Definition: sha1.c:105
void sha1_append(sha1_state_t *ctx, const unsigned char *dataIn, int len)
Definition: sha1.c:52
uint32_t sizeLo
Definition: sha1.h:57
static void sha1_hashblock(sha1_state_t *ctx)
Definition: sha1.c:116
uint32_t W[80]
Definition: sha1.h:55