summaryrefslogtreecommitdiff
path: root/src/modules/teamwork/sha1.c
blob: 7a53fa712ab46c75bcc5a2a0054dbcf500494cd3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
 * The contents of this file are subject to the Mozilla Public
 * License Version 1.1 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a copy of
 * the License at http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS
 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * rights and limitations under the License.
 *
 * The Original Code is SHA 180-1 Reference Implementation (Compact version)
 *
 * The Initial Developer of the Original Code is Paul Kocher of
 * Cryptography Research.  Portions created by Paul Kocher are
 * Copyright (C) 1995-9 by Cryptography Research, Inc.  All
 * Rights Reserved.
 *
 * Contributor(s):
 *
 *     Paul Kocher
 *
 */

#include "sha1.h"

static void shaHashBlock(SHA_CTX2 *ctx);

void
SHA1_Init2(SHA_CTX2 *ctx) {
   int i;

   ctx->lenW = 0;
   ctx->sizeHi = ctx->sizeLo = 0;

   /* Initialize H with the magic constants (see FIPS180 for constants)
    */
   ctx->H[0] = 0x67452301;
   ctx->H[1] = 0xefcdab89;
   ctx->H[2] = 0x98badcfe;
   ctx->H[3] = 0x10325476;
   ctx->H[4] = 0xc3d2e1f0;

   for (i = 0; i < 80; i++)
     ctx->W[i] = 0;
}

void
SHA1_Update2(SHA_CTX2 *ctx,
             void     *_dataIn,
             int       len) {
   unsigned char *dataIn = _dataIn;
   int i;

   /* Read the data into W and process blocks as they get full
    */
   for (i = 0; i < len; i++) {
        ctx->W[ctx->lenW / 4] <<= 8;
        ctx->W[ctx->lenW / 4] |= (unsigned int)dataIn[i];
        if ((++ctx->lenW) % 64 == 0)
          {
             shaHashBlock(ctx);
             ctx->lenW = 0;
          }
        ctx->sizeLo += 8;
        ctx->sizeHi += (ctx->sizeLo < 8);
     }
}

void
SHA1_Final2(unsigned char hashout[20],
            SHA_CTX2     *ctx) {
   unsigned char pad0x80 = 0x80;
   unsigned char pad0x00 = 0x00;
   unsigned char padlen[8];
   int i;

   /* Pad with a binary 1 (e.g. 0x80), then zeroes, then length
    */
   padlen[0] = (unsigned char)((ctx->sizeHi >> 24) & 255);
   padlen[1] = (unsigned char)((ctx->sizeHi >> 16) & 255);
   padlen[2] = (unsigned char)((ctx->sizeHi >> 8) & 255);
   padlen[3] = (unsigned char)((ctx->sizeHi >> 0) & 255);
   padlen[4] = (unsigned char)((ctx->sizeLo >> 24) & 255);
   padlen[5] = (unsigned char)((ctx->sizeLo >> 16) & 255);
   padlen[6] = (unsigned char)((ctx->sizeLo >> 8) & 255);
   padlen[7] = (unsigned char)((ctx->sizeLo >> 0) & 255);
   SHA1_Update2(ctx, &pad0x80, 1);
   while (ctx->lenW != 56)
     SHA1_Update2(ctx, &pad0x00, 1);
   SHA1_Update2(ctx, padlen, 8);

   /* Output hash
    */
   for (i = 0; i < 20; i++) {
        hashout[i] = (unsigned char)(ctx->H[i / 4] >> 24);
        ctx->H[i / 4] <<= 8;
     }

   /*
    *  Re-initialize the context (also zeroizes contents)
    */
   SHA1_Init2(ctx);
}

#define SHA_ROT(X, n) (((X) << (n)) | ((X) >> (32 - (n))))

static void
shaHashBlock(SHA_CTX2 *ctx) {
   int t;
   unsigned int A, B, C, D, E, TEMP;

   for (t = 16; t <= 79; t++)
     ctx->W[t] =
       SHA_ROT(ctx->W[t - 3] ^ ctx->W[t - 8] ^ ctx->W[t - 14] ^ ctx->W[t - 16], 1);

   A = ctx->H[0];
   B = ctx->H[1];
   C = ctx->H[2];
   D = ctx->H[3];
   E = ctx->H[4];

   for (t = 0; t <= 19; t++) {
        TEMP = SHA_ROT(A, 5) + (((C ^ D) & B) ^ D) + E + ctx->W[t] + 0x5a827999;
        E = D; D = C; C = SHA_ROT(B, 30); B = A; A = TEMP;
     }
   for (t = 20; t <= 39; t++) {
        TEMP = SHA_ROT(A, 5) + (B ^ C ^ D) + E + ctx->W[t] + 0x6ed9eba1;
        E = D; D = C; C = SHA_ROT(B, 30); B = A; A = TEMP;
     }
   for (t = 40; t <= 59; t++) {
        TEMP = SHA_ROT(A, 5) + ((B & C) | (D & (B | C))) + E + ctx->W[t] + 0x8f1bbcdc;
        E = D; D = C; C = SHA_ROT(B, 30); B = A; A = TEMP;
     }
   for (t = 60; t <= 79; t++) {
        TEMP = SHA_ROT(A, 5) + (B ^ C ^ D) + E + ctx->W[t] + 0xca62c1d6;
        E = D; D = C; C = SHA_ROT(B, 30); B = A; A = TEMP;
     }

   ctx->H[0] += A;
   ctx->H[1] += B;
   ctx->H[2] += C;
   ctx->H[3] += D;
   ctx->H[4] += E;
}