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

« back to all changes in this revision

Viewing changes to sha256.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:
7
7
 
8
8
/* nettle, low-level cryptographics library
9
9
 *
10
 
 * Copyright (C) 2001 Niels M�ller
 
10
 * Copyright (C) 2001 Niels Möller
11
11
 *  
12
12
 * The nettle library is free software; you can redistribute it and/or modify
13
13
 * it under the terms of the GNU Lesser General Public License as published by
62
62
  0x90befffaUL, 0xa4506cebUL, 0xbef9a3f7UL, 0xc67178f2UL, 
63
63
};
64
64
 
 
65
#define COMPRESS(ctx, data) (_nettle_sha256_compress((ctx)->state, (data), K))
 
66
 
65
67
/* Initialize the SHA values */
66
68
 
67
69
void
83
85
  ctx->index = 0;
84
86
}
85
87
 
86
 
#define SHA256_INCR(ctx) ((ctx)->count_high += !++(ctx)->count_low)
87
 
 
88
88
void
89
89
sha256_update(struct sha256_ctx *ctx,
90
 
              unsigned length, const uint8_t *buffer)
 
90
              unsigned length, const uint8_t *data)
91
91
{
92
 
  if (ctx->index)
93
 
    { /* Try to fill partial block */
94
 
      unsigned left = SHA256_DATA_SIZE - ctx->index;
95
 
      if (length < left)
96
 
        {
97
 
          memcpy(ctx->block + ctx->index, buffer, length);
98
 
          ctx->index += length;
99
 
          return; /* Finished */
100
 
        }
101
 
      else
102
 
        {
103
 
          memcpy(ctx->block + ctx->index, buffer, left);
104
 
 
105
 
          _nettle_sha256_compress(ctx->state, ctx->block, K);
106
 
          SHA256_INCR(ctx);
107
 
          
108
 
          buffer += left;
109
 
          length -= left;
110
 
        }
111
 
    }
112
 
  while (length >= SHA256_DATA_SIZE)
113
 
    {
114
 
      _nettle_sha256_compress(ctx->state, buffer, K);
115
 
      SHA256_INCR(ctx);
116
 
 
117
 
      buffer += SHA256_DATA_SIZE;
118
 
      length -= SHA256_DATA_SIZE;
119
 
    }
120
 
  /* Buffer leftovers */
121
 
  /* NOTE: The corresponding sha1 code checks for the special case length == 0.
122
 
   * That seems supoptimal, as I suspect it increases the number of branches. */
123
 
  
124
 
  memcpy(ctx->block, buffer, length);
125
 
  ctx->index = length;
 
92
  MD_UPDATE (ctx, length, data, COMPRESS, MD_INCR(ctx));
126
93
}
127
94
 
128
 
/* Final wrapup - pad to SHA1_DATA_SIZE-byte boundary with the bit pattern
129
 
   1 0* (64-bit count of bits processed, MSB-first) */
130
 
 
131
95
static void
132
 
sha256_final(struct sha256_ctx *ctx)
 
96
sha256_write_digest(struct sha256_ctx *ctx,
 
97
                    unsigned length,
 
98
                    uint8_t *digest)
133
99
{
134
 
  uint32_t bitcount_high;
135
 
  uint32_t bitcount_low;
136
 
  int i;
137
 
 
138
 
  i = ctx->index;
139
 
  
140
 
  /* Set the first char of padding to 0x80.  This is safe since there is
141
 
     always at least one byte free */
142
 
 
143
 
  assert(i < SHA256_DATA_SIZE);
144
 
  ctx->block[i++] = 0x80;
145
 
 
146
 
  if (i > (SHA1_DATA_SIZE - 8))
147
 
    { /* No room for length in this block. Process it and
148
 
       * pad with another one */
149
 
      memset(ctx->block + i, 0, SHA256_DATA_SIZE - i);
150
 
      _nettle_sha256_compress(ctx->state, ctx->block, K);
151
 
 
152
 
      i = 0;
153
 
    }
154
 
 
155
 
  if (i < (SHA256_DATA_SIZE - 8))
156
 
    memset(ctx->block + i, 0, (SHA256_DATA_SIZE - 8) - i);
157
 
 
158
 
  /* There are 512 = 2^9 bits in one block */
159
 
  bitcount_high = (ctx->count_high << 9) | (ctx->count_low >> 23);
160
 
  bitcount_low = (ctx->count_low << 9) | (ctx->index << 3);
 
100
  uint32_t high, low;
 
101
 
 
102
  assert(length <= SHA256_DIGEST_SIZE);
 
103
 
 
104
  MD_PAD(ctx, 8, COMPRESS);
 
105
 
 
106
  /* There are 512 = 2^9 bits in one block */  
 
107
  high = (ctx->count_high << 9) | (ctx->count_low >> 23);
 
108
  low = (ctx->count_low << 9) | (ctx->index << 3);
161
109
 
162
110
  /* This is slightly inefficient, as the numbers are converted to
163
111
     big-endian format, and will be converted back by the compression
164
112
     function. It's probably not worth the effort to fix this. */
165
 
  WRITE_UINT32(ctx->block + (SHA256_DATA_SIZE - 8), bitcount_high);
166
 
  WRITE_UINT32(ctx->block + (SHA256_DATA_SIZE - 4), bitcount_low);
 
113
  WRITE_UINT32(ctx->block + (SHA256_DATA_SIZE - 8), high);
 
114
  WRITE_UINT32(ctx->block + (SHA256_DATA_SIZE - 4), low);
 
115
  COMPRESS(ctx, ctx->block);
167
116
 
168
 
  _nettle_sha256_compress(ctx->state, ctx->block, K);
 
117
  _nettle_write_be32(length, digest, ctx->state);
169
118
}
170
119
 
171
120
void
173
122
              unsigned length,
174
123
              uint8_t *digest)
175
124
{
176
 
  assert(length <= SHA256_DIGEST_SIZE);
177
 
 
178
 
  sha256_final(ctx);
179
 
  _nettle_write_be32(length, digest, ctx->state);
 
125
  sha256_write_digest(ctx, length, digest);
180
126
  sha256_init(ctx);
181
127
}
182
128
 
206
152
              unsigned length,
207
153
              uint8_t *digest)
208
154
{
209
 
  assert(length <= SHA224_DIGEST_SIZE);
210
 
 
211
 
  sha256_final(ctx);
212
 
  _nettle_write_be32(length, digest, ctx->state);
 
155
  sha256_write_digest(ctx, length, digest);
213
156
  sha224_init(ctx);
214
157
}