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

« back to all changes in this revision

Viewing changes to md5.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:
5
5
 
6
6
/* nettle, low-level cryptographics library
7
7
 *
8
 
 * Copyright (C) 2001 Niels M�ller
 
8
 * Copyright (C) 2001 Niels Möller
9
9
 *  
10
10
 * The nettle library is free software; you can redistribute it and/or modify
11
11
 * it under the terms of the GNU Lesser General Public License as published by
24
24
 */
25
25
 
26
26
/* Based on public domain code hacked by Colin Plumb, Andrew Kuchling, and
27
 
 * Niels M�ller. */
 
27
 * Niels Möller. */
28
28
 
29
29
#if HAVE_CONFIG_H
30
30
# include "config.h"
36
36
#include "md5.h"
37
37
 
38
38
#include "macros.h"
39
 
 
40
 
static void
41
 
md5_final(struct md5_ctx *ctx);
 
39
#include "nettle-write.h"
42
40
 
43
41
void
44
42
md5_init(struct md5_ctx *ctx)
45
43
{
46
 
  ctx->digest[0] = 0x67452301;
47
 
  ctx->digest[1] = 0xefcdab89;
48
 
  ctx->digest[2] = 0x98badcfe;
49
 
  ctx->digest[3] = 0x10325476;
50
 
  
51
 
  ctx->count_l = ctx->count_h = 0;
 
44
  const uint32_t iv[_MD5_DIGEST_LENGTH] =
 
45
    {
 
46
      0x67452301,
 
47
      0xefcdab89,
 
48
      0x98badcfe,
 
49
      0x10325476,
 
50
    };
 
51
  memcpy(ctx->state, iv, sizeof(ctx->state));
 
52
  ctx->count_low = ctx->count_high = 0;
52
53
  ctx->index = 0;
53
54
}
54
55
 
55
 
#define MD5_INCR(ctx) ((ctx)->count_h += !++(ctx)->count_l)
 
56
#define COMPRESS(ctx, data) (_nettle_md5_compress((ctx)->state, (data)))
56
57
 
57
58
void
58
59
md5_update(struct md5_ctx *ctx,
59
60
           unsigned length,
60
61
           const uint8_t *data)
61
62
{
62
 
  if (ctx->index)
63
 
    {
64
 
      /* Try to fill partial block */
65
 
      unsigned left = MD5_DATA_SIZE - ctx->index;
66
 
      if (length < left)
67
 
        {
68
 
          memcpy(ctx->block + ctx->index, data, length);
69
 
          ctx->index += length;
70
 
          return; /* Finished */
71
 
        }
72
 
      else
73
 
        {
74
 
          memcpy(ctx->block + ctx->index, data, left);
75
 
 
76
 
          _nettle_md5_compress(ctx->digest, ctx->block);
77
 
          MD5_INCR(ctx);
78
 
          
79
 
          data += left;
80
 
          length -= left;
81
 
        }
82
 
    }
83
 
  while (length >= MD5_DATA_SIZE)
84
 
    {
85
 
      _nettle_md5_compress(ctx->digest, data);
86
 
      MD5_INCR(ctx);
87
 
 
88
 
      data += MD5_DATA_SIZE;
89
 
      length -= MD5_DATA_SIZE;
90
 
    }
91
 
  if ((ctx->index = length))     /* This assignment is intended */
92
 
    /* Buffer leftovers */
93
 
    memcpy(ctx->block, data, length);
 
63
  MD_UPDATE(ctx, length, data, COMPRESS, MD_INCR(ctx));
94
64
}
95
65
 
96
66
void
98
68
           unsigned length,
99
69
           uint8_t *digest)
100
70
{
101
 
  unsigned i;
102
 
  unsigned words;
103
 
  unsigned leftover;
 
71
  uint32_t high, low;
104
72
  
105
73
  assert(length <= MD5_DIGEST_SIZE);
106
74
 
107
 
  md5_final(ctx);
108
 
  
109
 
  words = length / 4;
110
 
  leftover = length % 4;
111
 
  
112
 
  /* Little endian order */
113
 
  for (i = 0; i < words; i++, digest += 4)
114
 
    LE_WRITE_UINT32(digest, ctx->digest[i]);
115
 
 
116
 
  if (leftover)
117
 
    {
118
 
      uint32_t word;
119
 
      unsigned j;
120
 
 
121
 
      assert(i < _MD5_DIGEST_LENGTH);
122
 
      
123
 
      /* Still least significant byte first. */
124
 
      for (word = ctx->digest[i], j = 0; j < leftover;
125
 
           j++, word >>= 8)
126
 
        digest[j] = word & 0xff;
127
 
    }
 
75
  MD_PAD(ctx, 8, COMPRESS);
 
76
 
 
77
  /* There are 512 = 2^9 bits in one block */  
 
78
  high = (ctx->count_high << 9) | (ctx->count_low >> 23);
 
79
  low = (ctx->count_low << 9) | (ctx->index << 3);
 
80
 
 
81
  LE_WRITE_UINT32(ctx->block + (MD5_DATA_SIZE - 8), low);
 
82
  LE_WRITE_UINT32(ctx->block + (MD5_DATA_SIZE - 4), high);
 
83
  _nettle_md5_compress(ctx->state, ctx->block);
 
84
 
 
85
  _nettle_write_le32(length, digest, ctx->state);
128
86
  md5_init(ctx);
129
87
}
130
 
 
131
 
/* Final wrapup - pad to MD5_DATA_SIZE-byte boundary with the bit
132
 
 * pattern 1 0* (64-bit count of bits processed, LSB-first) */
133
 
 
134
 
static void
135
 
md5_final(struct md5_ctx *ctx)
136
 
{
137
 
  uint32_t bitcount_high;
138
 
  uint32_t bitcount_low;
139
 
  unsigned i;
140
 
  
141
 
  i = ctx->index;
142
 
 
143
 
  /* Set the first char of padding to 0x80. This is safe since there
144
 
   * is always at least one byte free */
145
 
  assert(i < MD5_DATA_SIZE);
146
 
  ctx->block[i++] = 0x80;
147
 
 
148
 
  if (i > (MD5_DATA_SIZE - 8))
149
 
    {
150
 
      /* No room for length in this block. Process it and
151
 
         pad with another one */
152
 
      memset(ctx->block + i, 0, MD5_DATA_SIZE - i);
153
 
      
154
 
      _nettle_md5_compress(ctx->digest, ctx->block);
155
 
      i = 0;
156
 
    }
157
 
  if (i < (MD5_DATA_SIZE - 8))
158
 
    memset(ctx->block + i, 0, (MD5_DATA_SIZE - 8) - i);
159
 
    
160
 
  /* There are 512 = 2^9 bits in one block 
161
 
   * Little-endian order => Least significant word first */
162
 
 
163
 
  bitcount_low = (ctx->count_l << 9) | (ctx->index << 3);
164
 
  bitcount_high = (ctx->count_h << 9) | (ctx->count_l >> 23);
165
 
  LE_WRITE_UINT32(ctx->block + (MD5_DATA_SIZE - 8), bitcount_low);
166
 
  LE_WRITE_UINT32(ctx->block + (MD5_DATA_SIZE - 4), bitcount_high);
167
 
  
168
 
  _nettle_md5_compress(ctx->digest, ctx->block);
169
 
}