~ubuntu-branches/ubuntu/utopic/dropbear/utopic-proposed

« back to all changes in this revision

Viewing changes to libtomcrypt/sha1.c

  • Committer: Bazaar Package Importer
  • Author(s): Matt Johnston
  • Date: 2005-12-08 19:20:21 UTC
  • mfrom: (1.2.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20051208192021-nyp9rwnt77nsg6ty
Tags: 0.47-1
* New upstream release.
* SECURITY: Fix incorrect buffer sizing.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
2
 
 *
3
 
 * LibTomCrypt is a library that provides various cryptographic
4
 
 * algorithms in a highly modular and flexible manner.
5
 
 *
6
 
 * The library is free for all purposes without any express
7
 
 * guarantee it works.
8
 
 *
9
 
 * Tom St Denis, tomstdenis@iahu.ca, http://libtomcrypt.org
10
 
 */
11
 
 
12
 
/* SHA1 code by Tom St Denis */
13
 
#include "mycrypt.h"
14
 
 
15
 
#ifdef SHA1
16
 
 
17
 
const struct _hash_descriptor sha1_desc =
18
 
{
19
 
    "sha1",
20
 
    2,
21
 
    20,
22
 
    64,
23
 
 
24
 
#if 0
25
 
        /* matt */
26
 
    /* DER identifier */
27
 
    { 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2B, 0x0E, 
28
 
      0x03, 0x02, 0x1A, 0x05, 0x00, 0x04, 0x14 },
29
 
    15,
30
 
#endif
31
 
 
32
 
    &sha1_init,
33
 
    &sha1_process,
34
 
    &sha1_done,
35
 
    &sha1_test
36
 
};
37
 
 
38
 
#define F0(x,y,z)  (z ^ (x & (y ^ z)))
39
 
#define F1(x,y,z)  (x ^ y ^ z)
40
 
#define F2(x,y,z)  ((x & y) | (z & (x | y)))
41
 
#define F3(x,y,z)  (x ^ y ^ z)
42
 
 
43
 
#ifdef CLEAN_STACK
44
 
static int _sha1_compress(hash_state *md, unsigned char *buf)
45
 
#else
46
 
static int  sha1_compress(hash_state *md, unsigned char *buf)
47
 
#endif
48
 
{
49
 
    ulong32 a,b,c,d,e,W[80],i;
50
 
#ifdef SMALL_CODE
51
 
    ulong32 t;
52
 
#endif
53
 
 
54
 
    /* copy the state into 512-bits into W[0..15] */
55
 
    for (i = 0; i < 16; i++) {
56
 
        LOAD32H(W[i], buf + (4*i));
57
 
    }
58
 
 
59
 
    /* copy state */
60
 
    a = md->sha1.state[0];
61
 
    b = md->sha1.state[1];
62
 
    c = md->sha1.state[2];
63
 
    d = md->sha1.state[3];
64
 
    e = md->sha1.state[4];
65
 
 
66
 
    /* expand it */
67
 
    for (i = 16; i < 80; i++) {
68
 
        W[i] = ROL(W[i-3] ^ W[i-8] ^ W[i-14] ^ W[i-16], 1); 
69
 
    }
70
 
 
71
 
    /* compress */
72
 
    /* round one */
73
 
    #define FF0(a,b,c,d,e,i) e = (ROL(a, 5) + F0(b,c,d) + e + W[i] + 0x5a827999UL); b = ROL(b, 30);
74
 
    #define FF1(a,b,c,d,e,i) e = (ROL(a, 5) + F1(b,c,d) + e + W[i] + 0x6ed9eba1UL); b = ROL(b, 30);
75
 
    #define FF2(a,b,c,d,e,i) e = (ROL(a, 5) + F2(b,c,d) + e + W[i] + 0x8f1bbcdcUL); b = ROL(b, 30);
76
 
    #define FF3(a,b,c,d,e,i) e = (ROL(a, 5) + F3(b,c,d) + e + W[i] + 0xca62c1d6UL); b = ROL(b, 30);
77
 
 
78
 
#ifdef SMALL_CODE
79
 
 
80
 
    for (i = 0; i < 20; ) {
81
 
       FF0(a,b,c,d,e,i++); t = e; e = d; d = c; c = b; b = a; a = t;
82
 
    }
83
 
 
84
 
    for (; i < 40; ) {
85
 
       FF1(a,b,c,d,e,i++); t = e; e = d; d = c; c = b; b = a; a = t;
86
 
    }
87
 
 
88
 
    for (; i < 60; ) {
89
 
       FF2(a,b,c,d,e,i++); t = e; e = d; d = c; c = b; b = a; a = t;
90
 
    }
91
 
 
92
 
    for (; i < 80; ) {
93
 
       FF3(a,b,c,d,e,i++); t = e; e = d; d = c; c = b; b = a; a = t;
94
 
    }
95
 
 
96
 
#else
97
 
 
98
 
    for (i = 0; i < 20; ) {
99
 
       FF0(a,b,c,d,e,i++);
100
 
       FF0(e,a,b,c,d,i++);
101
 
       FF0(d,e,a,b,c,i++);
102
 
       FF0(c,d,e,a,b,i++);
103
 
       FF0(b,c,d,e,a,i++);
104
 
    }
105
 
 
106
 
    /* round two */
107
 
    for (; i < 40; )  { 
108
 
       FF1(a,b,c,d,e,i++);
109
 
       FF1(e,a,b,c,d,i++);
110
 
       FF1(d,e,a,b,c,i++);
111
 
       FF1(c,d,e,a,b,i++);
112
 
       FF1(b,c,d,e,a,i++);
113
 
    }
114
 
 
115
 
    /* round three */
116
 
    for (; i < 60; )  { 
117
 
       FF2(a,b,c,d,e,i++);
118
 
       FF2(e,a,b,c,d,i++);
119
 
       FF2(d,e,a,b,c,i++);
120
 
       FF2(c,d,e,a,b,i++);
121
 
       FF2(b,c,d,e,a,i++);
122
 
    }
123
 
 
124
 
    /* round four */
125
 
    for (; i < 80; )  { 
126
 
       FF3(a,b,c,d,e,i++);
127
 
       FF3(e,a,b,c,d,i++);
128
 
       FF3(d,e,a,b,c,i++);
129
 
       FF3(c,d,e,a,b,i++);
130
 
       FF3(b,c,d,e,a,i++);
131
 
    }
132
 
#endif
133
 
 
134
 
    #undef FF0
135
 
    #undef FF1
136
 
    #undef FF2
137
 
    #undef FF3
138
 
 
139
 
    /* store */
140
 
    md->sha1.state[0] = md->sha1.state[0] + a;
141
 
    md->sha1.state[1] = md->sha1.state[1] + b;
142
 
    md->sha1.state[2] = md->sha1.state[2] + c;
143
 
    md->sha1.state[3] = md->sha1.state[3] + d;
144
 
    md->sha1.state[4] = md->sha1.state[4] + e;
145
 
 
146
 
    return CRYPT_OK;
147
 
}
148
 
 
149
 
#ifdef CLEAN_STACK
150
 
static int sha1_compress(hash_state *md, unsigned char *buf)
151
 
{
152
 
   int err;
153
 
   err = _sha1_compress(md, buf);
154
 
   burn_stack(sizeof(ulong32) * 87);
155
 
   return err;
156
 
}
157
 
#endif
158
 
 
159
 
int sha1_init(hash_state * md)
160
 
{
161
 
   _ARGCHK(md != NULL);
162
 
   md->sha1.state[0] = 0x67452301UL;
163
 
   md->sha1.state[1] = 0xefcdab89UL;
164
 
   md->sha1.state[2] = 0x98badcfeUL;
165
 
   md->sha1.state[3] = 0x10325476UL;
166
 
   md->sha1.state[4] = 0xc3d2e1f0UL;
167
 
   md->sha1.curlen = 0;
168
 
   md->sha1.length = 0;
169
 
   return CRYPT_OK;
170
 
}
171
 
 
172
 
HASH_PROCESS(sha1_process, sha1_compress, sha1, 64)
173
 
 
174
 
int sha1_done(hash_state * md, unsigned char *hash)
175
 
{
176
 
    int i;
177
 
 
178
 
    _ARGCHK(md != NULL);
179
 
    _ARGCHK(hash != NULL);
180
 
 
181
 
    if (md->sha1.curlen >= sizeof(md->sha1.buf)) {
182
 
       return CRYPT_INVALID_ARG;
183
 
    }
184
 
 
185
 
    /* increase the length of the message */
186
 
    md->sha1.length += md->sha1.curlen * 8;
187
 
 
188
 
    /* append the '1' bit */
189
 
    md->sha1.buf[md->sha1.curlen++] = (unsigned char)0x80;
190
 
 
191
 
    /* if the length is currently above 56 bytes we append zeros
192
 
     * then compress.  Then we can fall back to padding zeros and length
193
 
     * encoding like normal.
194
 
     */
195
 
    if (md->sha1.curlen > 56) {
196
 
        while (md->sha1.curlen < 64) {
197
 
            md->sha1.buf[md->sha1.curlen++] = (unsigned char)0;
198
 
        }
199
 
        sha1_compress(md, md->sha1.buf);
200
 
        md->sha1.curlen = 0;
201
 
    }
202
 
 
203
 
    /* pad upto 56 bytes of zeroes */
204
 
    while (md->sha1.curlen < 56) {
205
 
        md->sha1.buf[md->sha1.curlen++] = (unsigned char)0;
206
 
    }
207
 
 
208
 
    /* store length */
209
 
    STORE64H(md->sha1.length, md->sha1.buf+56);
210
 
    sha1_compress(md, md->sha1.buf);
211
 
 
212
 
    /* copy output */
213
 
    for (i = 0; i < 5; i++) {
214
 
        STORE32H(md->sha1.state[i], hash+(4*i));
215
 
    }
216
 
#ifdef CLEAN_STACK
217
 
    zeromem(md, sizeof(hash_state));
218
 
#endif
219
 
    return CRYPT_OK;
220
 
}
221
 
 
222
 
int  sha1_test(void)
223
 
{
224
 
 #ifndef LTC_TEST
225
 
    return CRYPT_NOP;
226
 
 #else    
227
 
  static const struct {
228
 
      char *msg;
229
 
      unsigned char hash[20];
230
 
  } tests[] = {
231
 
    { "abc",
232
 
      { 0xa9, 0x99, 0x3e, 0x36, 0x47, 0x06, 0x81, 0x6a,
233
 
        0xba, 0x3e, 0x25, 0x71, 0x78, 0x50, 0xc2, 0x6c,
234
 
        0x9c, 0xd0, 0xd8, 0x9d }
235
 
    },
236
 
    { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
237
 
      { 0x84, 0x98, 0x3E, 0x44, 0x1C, 0x3B, 0xD2, 0x6E,
238
 
        0xBA, 0xAE, 0x4A, 0xA1, 0xF9, 0x51, 0x29, 0xE5,
239
 
        0xE5, 0x46, 0x70, 0xF1 }
240
 
    }
241
 
  };
242
 
 
243
 
  int i;
244
 
  unsigned char tmp[20];
245
 
  hash_state md;
246
 
 
247
 
  for (i = 0; i < (int)(sizeof(tests) / sizeof(tests[0]));  i++) {
248
 
      sha1_init(&md);
249
 
      sha1_process(&md, (unsigned char*)tests[i].msg, (unsigned long)strlen(tests[i].msg));
250
 
      sha1_done(&md, tmp);
251
 
      if (memcmp(tmp, tests[i].hash, 20) != 0) {
252
 
         return CRYPT_FAIL_TESTVECTOR;
253
 
      }
254
 
  }
255
 
  return CRYPT_OK;
256
 
  #endif
257
 
}
258
 
 
259
 
#endif
260
 
 
261