~ubuntu-branches/ubuntu/vivid/nettle/vivid

« back to all changes in this revision

Viewing changes to sha1.c

  • Committer: Package Import Robot
  • Author(s): Magnus Holmgren
  • Date: 2012-08-25 18:28:37 UTC
  • mfrom: (1.5.1) (8.1.5 sid)
  • mto: (8.1.7 sid)
  • mto: This revision was merged to the branch mainline in revision 14.
  • Revision ID: package-import@ubuntu.com-20120825182837-i7h4w03l7mxgvmqb
Tags: 2.5-1
* New upstream release (Closes: #685855).
  - All symbols from nettle-internal.c have been dropped from the built
    library, and pkcs1_signature_prefix renamed with a leading underscore,
    without SONAME change, as they were all for internal use only.
* debian/watch: Updated to handle -pre releases.

Show diffs side-by-side

added added

removed removed

Lines of Context:
6
6
 
7
7
/* nettle, low-level cryptographics library
8
8
 *
9
 
 * Copyright (C) 2001 Peter Gutmann, Andrew Kuchling, Niels M�ller
 
9
 * Copyright (C) 2001 Peter Gutmann, Andrew Kuchling, Niels Möller
10
10
 *  
11
11
 * The nettle library is free software; you can redistribute it and/or modify
12
12
 * it under the terms of the GNU Lesser General Public License as published by
49
49
#include "macros.h"
50
50
#include "nettle-write.h"
51
51
 
52
 
/* A block, treated as a sequence of 32-bit words. */
53
 
#define SHA1_DATA_LENGTH 16
54
 
 
55
 
/* SHA initial values */
56
 
 
57
 
#define h0init  0x67452301L
58
 
#define h1init  0xEFCDAB89L
59
 
#define h2init  0x98BADCFEL
60
 
#define h3init  0x10325476L
61
 
#define h4init  0xC3D2E1F0L
62
 
 
63
52
/* Initialize the SHA values */
64
 
 
65
53
void
66
54
sha1_init(struct sha1_ctx *ctx)
67
55
{
68
 
  /* Set the h-vars to their initial values */
69
 
  ctx->digest[ 0 ] = h0init;
70
 
  ctx->digest[ 1 ] = h1init;
71
 
  ctx->digest[ 2 ] = h2init;
72
 
  ctx->digest[ 3 ] = h3init;
73
 
  ctx->digest[ 4 ] = h4init;
 
56
  /* FIXME: Put the buffer last in the struct, and arrange so that we
 
57
     can initialize with a single memcpy. */
 
58
  static const uint32_t iv[_SHA1_DIGEST_LENGTH] = 
 
59
    {
 
60
      /* SHA initial values */
 
61
      0x67452301L,
 
62
      0xEFCDAB89L,
 
63
      0x98BADCFEL,
 
64
      0x10325476L,
 
65
      0xC3D2E1F0L,
 
66
    };
74
67
 
75
 
  /* Initialize bit count */
 
68
  memcpy(ctx->state, iv, sizeof(ctx->state));
76
69
  ctx->count_low = ctx->count_high = 0;
77
70
  
78
71
  /* Initialize buffer */
79
72
  ctx->index = 0;
80
73
}
81
74
 
82
 
#define SHA1_INCR(ctx) ((ctx)->count_high += !++(ctx)->count_low)
 
75
#define COMPRESS(ctx, data) (_nettle_sha1_compress((ctx)->state, data))
83
76
 
84
77
void
85
78
sha1_update(struct sha1_ctx *ctx,
86
 
            unsigned length, const uint8_t *buffer)
 
79
            unsigned length, const uint8_t *data)
87
80
{
88
 
  if (ctx->index)
89
 
    { /* Try to fill partial block */
90
 
      unsigned left = SHA1_DATA_SIZE - ctx->index;
91
 
      if (length < left)
92
 
        {
93
 
          memcpy(ctx->block + ctx->index, buffer, length);
94
 
          ctx->index += length;
95
 
          return; /* Finished */
96
 
        }
97
 
      else
98
 
        {
99
 
          memcpy(ctx->block + ctx->index, buffer, left);
100
 
 
101
 
          _nettle_sha1_compress(ctx->digest, ctx->block);
102
 
          SHA1_INCR(ctx);
103
 
 
104
 
          buffer += left;
105
 
          length -= left;
106
 
        }
107
 
    }
108
 
  while (length >= SHA1_DATA_SIZE)
109
 
    {
110
 
      _nettle_sha1_compress(ctx->digest, buffer);
111
 
      SHA1_INCR(ctx);
112
 
 
113
 
      buffer += SHA1_DATA_SIZE;
114
 
      length -= SHA1_DATA_SIZE;
115
 
    }
116
 
  if ((ctx->index = length))     /* This assignment is intended */
117
 
    /* Buffer leftovers */
118
 
    memcpy(ctx->block, buffer, length);
 
81
  MD_UPDATE (ctx, length, data, COMPRESS, MD_INCR(ctx));
119
82
}
120
83
          
121
 
/* Final wrapup - pad to SHA1_DATA_SIZE-byte boundary with the bit pattern
122
 
   1 0* (64-bit count of bits processed, MSB-first) */
123
 
 
124
 
static void
125
 
sha1_final(struct sha1_ctx *ctx)
126
 
{
127
 
  uint32_t bitcount_high;
128
 
  uint32_t bitcount_low;
129
 
  unsigned i;
130
 
  
131
 
  i = ctx->index;
132
 
  
133
 
  /* Set the first char of padding to 0x80.  This is safe since there is
134
 
     always at least one byte free */
135
 
 
136
 
  assert(i < SHA1_DATA_SIZE);
137
 
  ctx->block[i++] = 0x80;
138
 
 
139
 
  if (i > (SHA1_DATA_SIZE - 8))
140
 
    { /* No room for length in this block. Process it and
141
 
         pad with another one */
142
 
      memset(ctx->block + i, 0, SHA1_DATA_SIZE - i);
143
 
      
144
 
      _nettle_sha1_compress(ctx->digest, ctx->block);
145
 
      i = 0;
146
 
    }
147
 
  if (i < (SHA1_DATA_SIZE - 8))
148
 
    memset(ctx->block + i, 0, (SHA1_DATA_SIZE - 8) - i);
149
 
 
150
 
  /* There are 512 = 2^9 bits in one block */  
151
 
  bitcount_high = (ctx->count_high << 9) | (ctx->count_low >> 23);
152
 
  bitcount_low = (ctx->count_low << 9) | (ctx->index << 3);
153
 
 
154
 
  /* This is slightly inefficient, as the numbers are converted to
155
 
     big-endian format, and will be converted back by the compression
156
 
     function. It's probably not worth the effort to fix this. */
157
 
  WRITE_UINT32(ctx->block + (SHA1_DATA_SIZE - 8), bitcount_high);
158
 
  WRITE_UINT32(ctx->block + (SHA1_DATA_SIZE - 4), bitcount_low);
159
 
 
160
 
  _nettle_sha1_compress(ctx->digest, ctx->block);
161
 
}
162
 
 
163
84
void
164
85
sha1_digest(struct sha1_ctx *ctx,
165
86
            unsigned length,
166
87
            uint8_t *digest)
167
88
{
 
89
  uint32_t high, low;
 
90
 
168
91
  assert(length <= SHA1_DIGEST_SIZE);
169
92
 
170
 
  sha1_final(ctx);
171
 
  _nettle_write_be32(length, digest, ctx->digest);
 
93
  MD_PAD(ctx, 8, COMPRESS);
 
94
 
 
95
  /* There are 512 = 2^9 bits in one block */  
 
96
  high = (ctx->count_high << 9) | (ctx->count_low >> 23);
 
97
  low = (ctx->count_low << 9) | (ctx->index << 3);
 
98
 
 
99
  /* append the 64 bit count */
 
100
  WRITE_UINT32(ctx->block + (SHA1_DATA_SIZE - 8), high);
 
101
  WRITE_UINT32(ctx->block + (SHA1_DATA_SIZE - 4), low);
 
102
  _nettle_sha1_compress(ctx->state, ctx->block);
 
103
 
 
104
  _nettle_write_be32(length, digest, ctx->state);
172
105
  sha1_init(ctx);
173
106
}