~ubuntu-branches/ubuntu/dapper/gnutls12/dapper

« back to all changes in this revision

Viewing changes to nettle/sha1.c

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Urlichs
  • Date: 2005-11-15 19:26:02 UTC
  • Revision ID: james.westby@ubuntu.com-20051115192602-milf548iripaq3jh
Tags: 1.2.9-2
* Install /usr/lib/pkgconfig/*.pc files.
* Depend on texinfo (>= 4.8, for the @euro{} sign).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* sha1.c
 
2
 *
 
3
 * The sha1 hash function.
 
4
 */
 
5
 
 
6
/* nettle, low-level cryptographics library
 
7
 *
 
8
 * Copyright (C) 2001 Peter Gutmann, Andrew Kuchling, Niels M�ller
 
9
 *  
 
10
 * The nettle library is free software; you can redistribute it and/or modify
 
11
 * it under the terms of the GNU Lesser General Public License as published by
 
12
 * the Free Software Foundation; either version 2.1 of the License, or (at your
 
13
 * option) any later version.
 
14
 * 
 
15
 * The nettle library is distributed in the hope that it will be useful, but
 
16
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 
17
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
 
18
 * License for more details.
 
19
 * 
 
20
 * You should have received a copy of the GNU Lesser General Public License
 
21
 * along with the nettle library; see the file COPYING.LIB.  If not, write to
 
22
 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
 
23
 * MA 02111-1307, USA.
 
24
 */
 
25
 
 
26
/* Here's the first paragraph of Peter Gutmann's posting,
 
27
 * <30ajo5$oe8@ccu2.auckland.ac.nz>: 
 
28
 *
 
29
 * The following is my SHA (FIPS 180) code updated to allow use of the "fixed"
 
30
 * SHA, thanks to Jim Gillogly and an anonymous contributor for the information on
 
31
 * what's changed in the new version.  The fix is a simple change which involves
 
32
 * adding a single rotate in the initial expansion function.  It is unknown
 
33
 * whether this is an optimal solution to the problem which was discovered in the
 
34
 * SHA or whether it's simply a bandaid which fixes the problem with a minimum of
 
35
 * effort (for example the reengineering of a great many Capstone chips).
 
36
 */
 
37
 
 
38
#if HAVE_CONFIG_H
 
39
# include "config.h"
 
40
#endif
 
41
 
 
42
#include <assert.h>
 
43
#include <stdlib.h>
 
44
#include <string.h>
 
45
 
 
46
#include "sha.h"
 
47
 
 
48
#include "macros.h"
 
49
 
 
50
/* A block, treated as a sequence of 32-bit words. */
 
51
#define SHA1_DATA_LENGTH 16
 
52
 
 
53
/* SHA initial values */
 
54
 
 
55
#define h0init  0x67452301L
 
56
#define h1init  0xEFCDAB89L
 
57
#define h2init  0x98BADCFEL
 
58
#define h3init  0x10325476L
 
59
#define h4init  0xC3D2E1F0L
 
60
 
 
61
/* Initialize the SHA values */
 
62
 
 
63
void
 
64
sha1_init(struct sha1_ctx *ctx)
 
65
{
 
66
  /* Set the h-vars to their initial values */
 
67
  ctx->digest[ 0 ] = h0init;
 
68
  ctx->digest[ 1 ] = h1init;
 
69
  ctx->digest[ 2 ] = h2init;
 
70
  ctx->digest[ 3 ] = h3init;
 
71
  ctx->digest[ 4 ] = h4init;
 
72
 
 
73
  /* Initialize bit count */
 
74
  ctx->count_low = ctx->count_high = 0;
 
75
  
 
76
  /* Initialize buffer */
 
77
  ctx->index = 0;
 
78
}
 
79
 
 
80
/* Compression function, written in assembler on some systems.
 
81
   Note that it destroys the data array. */
 
82
#define sha1_compress _nettle_sha1_compress
 
83
 
 
84
static void
 
85
sha1_block(struct sha1_ctx *ctx, const uint8_t *block)
 
86
{
 
87
  uint32_t data[SHA1_DATA_LENGTH];
 
88
  int i;
 
89
 
 
90
  /* Update block count */
 
91
  if (!++ctx->count_low)
 
92
    ++ctx->count_high;
 
93
 
 
94
  /* Endian independent conversion */
 
95
  for (i = 0; i<SHA1_DATA_LENGTH; i++, block += 4)
 
96
    data[i] = READ_UINT32(block);
 
97
 
 
98
  sha1_compress(ctx->digest, data);
 
99
}
 
100
 
 
101
void
 
102
sha1_update(struct sha1_ctx *ctx,
 
103
            unsigned length, const uint8_t *buffer)
 
104
{
 
105
  if (ctx->index)
 
106
    { /* Try to fill partial block */
 
107
      unsigned left = SHA1_DATA_SIZE - ctx->index;
 
108
      if (length < left)
 
109
        {
 
110
          memcpy(ctx->block + ctx->index, buffer, length);
 
111
          ctx->index += length;
 
112
          return; /* Finished */
 
113
        }
 
114
      else
 
115
        {
 
116
          memcpy(ctx->block + ctx->index, buffer, left);
 
117
          sha1_block(ctx, ctx->block);
 
118
          buffer += left;
 
119
          length -= left;
 
120
        }
 
121
    }
 
122
  while (length >= SHA1_DATA_SIZE)
 
123
    {
 
124
      sha1_block(ctx, buffer);
 
125
      buffer += SHA1_DATA_SIZE;
 
126
      length -= SHA1_DATA_SIZE;
 
127
    }
 
128
  if ((ctx->index = length))     /* This assignment is intended */
 
129
    /* Buffer leftovers */
 
130
    memcpy(ctx->block, buffer, length);
 
131
}
 
132
          
 
133
/* Final wrapup - pad to SHA1_DATA_SIZE-byte boundary with the bit pattern
 
134
   1 0* (64-bit count of bits processed, MSB-first) */
 
135
 
 
136
static void
 
137
sha1_final(struct sha1_ctx *ctx)
 
138
{
 
139
  uint32_t data[SHA1_DATA_LENGTH];
 
140
  int i;
 
141
  int words;
 
142
 
 
143
  i = ctx->index;
 
144
  
 
145
  /* Set the first char of padding to 0x80.  This is safe since there is
 
146
     always at least one byte free */
 
147
 
 
148
  assert(i < SHA1_DATA_SIZE);
 
149
  ctx->block[i++] = 0x80;
 
150
 
 
151
  /* Fill rest of word */
 
152
  for( ; i & 3; i++)
 
153
    ctx->block[i] = 0;
 
154
 
 
155
  /* i is now a multiple of the word size 4 */
 
156
  words = i >> 2;
 
157
  for (i = 0; i < words; i++)
 
158
    data[i] = READ_UINT32(ctx->block + 4*i);
 
159
  
 
160
  if (words > (SHA1_DATA_LENGTH-2))
 
161
    { /* No room for length in this block. Process it and
 
162
       * pad with another one */
 
163
      for (i = words ; i < SHA1_DATA_LENGTH; i++)
 
164
        data[i] = 0;
 
165
      sha1_compress(ctx->digest, data);
 
166
      for (i = 0; i < (SHA1_DATA_LENGTH-2); i++)
 
167
        data[i] = 0;
 
168
    }
 
169
  else
 
170
    for (i = words ; i < SHA1_DATA_LENGTH - 2; i++)
 
171
      data[i] = 0;
 
172
 
 
173
  /* There are 512 = 2^9 bits in one block */
 
174
  data[SHA1_DATA_LENGTH-2] = (ctx->count_high << 9) | (ctx->count_low >> 23);
 
175
  data[SHA1_DATA_LENGTH-1] = (ctx->count_low << 9) | (ctx->index << 3);
 
176
  sha1_compress(ctx->digest, data);
 
177
}
 
178
 
 
179
void
 
180
sha1_digest(struct sha1_ctx *ctx,
 
181
            unsigned length,
 
182
            uint8_t *digest)
 
183
{
 
184
  unsigned i;
 
185
  unsigned words;
 
186
  unsigned leftover;
 
187
  
 
188
  assert(length <= SHA1_DIGEST_SIZE);
 
189
 
 
190
  sha1_final(ctx);
 
191
  
 
192
  words = length / 4;
 
193
  leftover = length % 4;
 
194
 
 
195
  for (i = 0; i < words; i++, digest += 4)
 
196
    WRITE_UINT32(digest, ctx->digest[i]);
 
197
 
 
198
  if (leftover)
 
199
    {
 
200
      uint32_t word;
 
201
      unsigned j = leftover;
 
202
      
 
203
      assert(i < _SHA1_DIGEST_LENGTH);
 
204
      
 
205
      word = ctx->digest[i];
 
206
      
 
207
      switch (leftover)
 
208
        {
 
209
        default:
 
210
          abort();
 
211
        case 3:
 
212
          digest[--j] = (word >> 8) & 0xff;
 
213
          /* Fall through */
 
214
        case 2:
 
215
          digest[--j] = (word >> 16) & 0xff;
 
216
          /* Fall through */
 
217
        case 1:
 
218
          digest[--j] = (word >> 24) & 0xff;
 
219
        }
 
220
    }
 
221
  sha1_init(ctx);
 
222
}