~ubuntu-branches/ubuntu/utopic/mdadm/utopic

« back to all changes in this revision

Viewing changes to sha1.c

  • Committer: Package Import Robot
  • Author(s): Dmitrijs Ledkovs
  • Date: 2012-06-15 15:31:59 UTC
  • mfrom: (1.1.30 sid)
  • Revision ID: package-import@ubuntu.com-20120615153159-9lp7jykbq7vua4bo
Tags: 3.2.5-1ubuntu1
* Merge from Debian testing. (LP: #920324)  Remaining changes:
  - Call checks in local-premount to avoid race condition with udev
    and opening a degraded array.
  - d/initramfs/mdadm-functions: Record in /run when boot-degraded 
    question has been asked so that it is only asked once
  - pass --test to mdadm to enable result codes for degraded arrays. 
  - debian/control: we need udev and util-linux in the right version. We
    also remove the build dependency from quilt and docbook-to-man as both
    are not used in Ubuntus mdadm.
  - debian/initramfs/hook: kept the Ubuntus version for handling the absence
    of active raid arrays in <initramfs>/etc/mdadm/mdadm.conf
  - debian/initramfs/script.local-top.DEBIAN, debian/mdadm-startall,
    debian/mdadm.raid.DEBIAN: removed. udev does its job now instead.
  - debian/mdadm-startall.sgml, debian/mdadm-startall.8: documentation of
    unused startall script
  - debian/mdadm.config, debian/mdadm.postinst - let udev do the handling
    instead. Resolved merge conflict by keeping Ubuntu's version.
  - debian/mdadm.postinst, debian/mdadm.config, initramfs/init-premount:
    boot-degraded enablement; maintain udev starting of RAID devices;
    init-premount hook script for the initramfs, to provide information at
    boot
  - debian/mkconf.in is the older mkconf. Kept the Ubuntu version.
  - debian/rules: Kept Ubuntus version for installing apport hooks, not
    installing un-used startall script.
  - debian/install-rc, check.d/_numbers, check.d/root_on_raid: Ubuntu partman
    installer changes
  - debian/presubj: Dropped this unused bug reporting file. Instead use
    source_mdadm.py act as an apport hook for bug handling.
  - d/p/debian-changes-3.1.4-1+8efb9d1ubuntu4: mdadm udev rule
    incrementally adds mdadm member when detected. Starting such an
    array in degraded mode is possible by mdadm -IRs. Using mdadm
    -ARs without stopping the array first does nothing when no
    mdarray-unassociated device is available. Using mdadm -IRs to
    start a previously partially assembled array through incremental
    mode. Keeping the mdadm -ARs for assembling arrays which were for
    some reason not assembled through incremental mode (i.e through
    mdadm's udev rule).

* Additional Ubuntu changes:
    - debian/initramfs/local-premount: add call wait_for_udev to wait a
    little longer for RAID devices to appear (LP: #942106)

* Dropped Ubuntu changes:
  - Build udeb with -O2 on ppc64, working around a link error. Builds
    fine without it on debian.
  - rename debian/mdadm.vol_id.udev to debian/mdadm.mdadm-blkid.udev so
    that the rules file ends up with a more reasonable name. debian/rules
    changes for adding ubuntu's udev rule corresponding to mdadm. As we
    are now using 'upstream' udev rules see 3.2.3-2ubuntu2.

* Changes to Ubuntu changes:
  - debian/source_mdadm.py: make apport hook python 2 and 3 compatible
    (LP: #1013171).

* New upstream release closes this bugs:
  - mdadm --detail --scan segfaults during update-initramfs (LP: #969384)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* sha1.c - Functions to compute SHA1 message digest of files or
2
2
   memory blocks according to the NIST specification FIPS-180-1.
3
3
 
4
 
   Copyright (C) 2000, 2001, 2003, 2004, 2005 Free Software Foundation, Inc.
 
4
   Copyright (C) 2000, 2001, 2003, 2004, 2005, 2006, 2008 Free Software
 
5
   Foundation, Inc.
5
6
 
6
7
   This program is free software; you can redistribute it and/or modify it
7
8
   under the terms of the GNU General Public License as published by the
22
23
      Robert Klep <robert@ilse.nl>  -- Expansion function fix
23
24
*/
24
25
 
25
 
#ifdef HAVE_CONFIG_H
26
 
# include <config.h>
27
 
#endif
 
26
//#include <config.h>
28
27
 
29
28
#include "sha1.h"
30
29
 
35
34
# include "unlocked-io.h"
36
35
#endif
37
36
 
38
 
/* SWAP does an endian swap on architectures that are little-endian,
39
 
   as SHA1 needs some data in a big-endian form.  */
40
 
 
41
37
#ifdef WORDS_BIGENDIAN
42
38
# define SWAP(n) (n)
43
39
#else
55
51
static const unsigned char fillbuf[64] = { 0x80, 0 /* , 0, 0, ...  */ };
56
52
 
57
53
 
58
 
/*
59
 
  Takes a pointer to a 160 bit block of data (five 32 bit ints) and
60
 
  intializes it to the start constants of the SHA1 algorithm.  This
61
 
  must be called before using hash in the call to sha1_hash.
62
 
*/
 
54
/* Take a pointer to a 160 bit block of data (five 32 bit ints) and
 
55
   initialize it to the start constants of the SHA1 algorithm.  This
 
56
   must be called before using hash in the call to sha1_hash.  */
63
57
void
64
58
sha1_init_ctx (struct sha1_ctx *ctx)
65
59
{
77
71
   must be in little endian byte order.
78
72
 
79
73
   IMPORTANT: On some systems it is required that RESBUF is correctly
80
 
   aligned for a 32 bits value.  */
 
74
   aligned for a 32-bit value.  */
81
75
void *
82
76
sha1_read_ctx (const struct sha1_ctx *ctx, void *resbuf)
83
77
{
84
 
  ((md5_uint32 *) resbuf)[0] = SWAP (ctx->A);
85
 
  ((md5_uint32 *) resbuf)[1] = SWAP (ctx->B);
86
 
  ((md5_uint32 *) resbuf)[2] = SWAP (ctx->C);
87
 
  ((md5_uint32 *) resbuf)[3] = SWAP (ctx->D);
88
 
  ((md5_uint32 *) resbuf)[4] = SWAP (ctx->E);
 
78
  ((sha1_uint32 *) resbuf)[0] = SWAP (ctx->A);
 
79
  ((sha1_uint32 *) resbuf)[1] = SWAP (ctx->B);
 
80
  ((sha1_uint32 *) resbuf)[2] = SWAP (ctx->C);
 
81
  ((sha1_uint32 *) resbuf)[3] = SWAP (ctx->D);
 
82
  ((sha1_uint32 *) resbuf)[4] = SWAP (ctx->E);
89
83
 
90
84
  return resbuf;
91
85
}
94
88
   prolog according to the standard and write the result to RESBUF.
95
89
 
96
90
   IMPORTANT: On some systems it is required that RESBUF is correctly
97
 
   aligned for a 32 bits value.  */
 
91
   aligned for a 32-bit value.  */
98
92
void *
99
93
sha1_finish_ctx (struct sha1_ctx *ctx, void *resbuf)
100
94
{
101
95
  /* Take yet unprocessed bytes into account.  */
102
 
  md5_uint32 bytes = ctx->buflen;
103
 
  size_t pad;
 
96
  sha1_uint32 bytes = ctx->buflen;
 
97
  size_t size = (bytes < 56) ? 64 / 4 : 64 * 2 / 4;
104
98
 
105
99
  /* Now count remaining bytes.  */
106
100
  ctx->total[0] += bytes;
107
101
  if (ctx->total[0] < bytes)
108
102
    ++ctx->total[1];
109
103
 
110
 
  pad = bytes >= 56 ? 64 + 56 - bytes : 56 - bytes;
111
 
  memcpy (&ctx->buffer[bytes], fillbuf, pad);
112
 
 
113
104
  /* Put the 64-bit file length in *bits* at the end of the buffer.  */
114
 
  *(md5_uint32 *) &ctx->buffer[bytes + pad + 4] = SWAP (ctx->total[0] << 3);
115
 
  *(md5_uint32 *) &ctx->buffer[bytes + pad] = SWAP ((ctx->total[1] << 3) |
116
 
                                                    (ctx->total[0] >> 29));
 
105
  ctx->buffer[size - 2] = SWAP ((ctx->total[1] << 3) | (ctx->total[0] >> 29));
 
106
  ctx->buffer[size - 1] = SWAP (ctx->total[0] << 3);
 
107
 
 
108
  memcpy (&((char *) ctx->buffer)[bytes], fillbuf, (size - 2) * 4 - bytes);
117
109
 
118
110
  /* Process last bytes.  */
119
 
  sha1_process_block (ctx->buffer, bytes + pad + 8, ctx);
 
111
  sha1_process_block (ctx->buffer, size * 4, ctx);
120
112
 
121
113
  return sha1_read_ctx (ctx, resbuf);
122
114
}
187
179
  return 0;
188
180
}
189
181
 
190
 
/* Compute MD5 message digest for LEN bytes beginning at BUFFER.  The
 
182
/* Compute SHA1 message digest for LEN bytes beginning at BUFFER.  The
191
183
   result is always in little endian byte order, so that a byte-wise
192
184
   output yields to the wanted ASCII representation of the message
193
185
   digest.  */
216
208
      size_t left_over = ctx->buflen;
217
209
      size_t add = 128 - left_over > len ? len : 128 - left_over;
218
210
 
219
 
      memcpy (&ctx->buffer[left_over], buffer, add);
 
211
      memcpy (&((char *) ctx->buffer)[left_over], buffer, add);
220
212
      ctx->buflen += add;
221
213
 
222
214
      if (ctx->buflen > 64)
225
217
 
226
218
          ctx->buflen &= 63;
227
219
          /* The regions in the following copy operation cannot overlap.  */
228
 
          memcpy (ctx->buffer, &ctx->buffer[(left_over + add) & ~63],
 
220
          memcpy (ctx->buffer,
 
221
                  &((char *) ctx->buffer)[(left_over + add) & ~63],
229
222
                  ctx->buflen);
230
223
        }
231
224
 
238
231
    {
239
232
#if !_STRING_ARCH_unaligned
240
233
# define alignof(type) offsetof (struct { char c; type x; }, x)
241
 
# define UNALIGNED_P(p) (((size_t) p) % alignof (md5_uint32) != 0)
 
234
# define UNALIGNED_P(p) (((size_t) p) % alignof (sha1_uint32) != 0)
242
235
      if (UNALIGNED_P (buffer))
243
236
        while (len > 64)
244
237
          {
260
253
    {
261
254
      size_t left_over = ctx->buflen;
262
255
 
263
 
      memcpy (&ctx->buffer[left_over], buffer, len);
 
256
      memcpy (&((char *) ctx->buffer)[left_over], buffer, len);
264
257
      left_over += len;
265
258
      if (left_over >= 64)
266
259
        {
267
260
          sha1_process_block (ctx->buffer, 64, ctx);
268
261
          left_over -= 64;
269
 
          memcpy (ctx->buffer, &ctx->buffer[64], left_over);
 
262
          memcpy (ctx->buffer, &ctx->buffer[16], left_over);
270
263
        }
271
264
      ctx->buflen = left_over;
272
265
    }
275
268
/* --- Code below is the primary difference between md5.c and sha1.c --- */
276
269
 
277
270
/* SHA1 round constants */
278
 
#define K1 0x5a827999L
279
 
#define K2 0x6ed9eba1L
280
 
#define K3 0x8f1bbcdcL
281
 
#define K4 0xca62c1d6L
 
271
#define K1 0x5a827999
 
272
#define K2 0x6ed9eba1
 
273
#define K3 0x8f1bbcdc
 
274
#define K4 0xca62c1d6
282
275
 
283
276
/* Round functions.  Note that F2 is the same as F4.  */
284
277
#define F1(B,C,D) ( D ^ ( B & ( C ^ D ) ) )
293
286
void
294
287
sha1_process_block (const void *buffer, size_t len, struct sha1_ctx *ctx)
295
288
{
296
 
  const md5_uint32 *words = buffer;
297
 
  size_t nwords = len / sizeof (md5_uint32);
298
 
  const md5_uint32 *endp = words + nwords;
299
 
  md5_uint32 x[16];
300
 
  md5_uint32 a = ctx->A;
301
 
  md5_uint32 b = ctx->B;
302
 
  md5_uint32 c = ctx->C;
303
 
  md5_uint32 d = ctx->D;
304
 
  md5_uint32 e = ctx->E;
 
289
  const sha1_uint32 *words = (const sha1_uint32*) buffer;
 
290
  size_t nwords = len / sizeof (sha1_uint32);
 
291
  const sha1_uint32 *endp = words + nwords;
 
292
  sha1_uint32 x[16];
 
293
  sha1_uint32 a = ctx->A;
 
294
  sha1_uint32 b = ctx->B;
 
295
  sha1_uint32 c = ctx->C;
 
296
  sha1_uint32 d = ctx->D;
 
297
  sha1_uint32 e = ctx->E;
305
298
 
306
299
  /* First increment the byte count.  RFC 1321 specifies the possible
307
300
     length of the file up to 2^64 bits.  Here we only compute the
310
303
  if (ctx->total[0] < len)
311
304
    ++ctx->total[1];
312
305
 
313
 
#define rol(x, n) (((x) << (n)) | ((x) >> (32 - (n))))
 
306
#define rol(x, n) (((x) << (n)) | ((sha1_uint32) (x) >> (32 - (n))))
314
307
 
315
308
#define M(I) ( tm =   x[I&0x0f] ^ x[(I-14)&0x0f] \
316
309
                    ^ x[(I-8)&0x0f] ^ x[(I-3)&0x0f] \
325
318
 
326
319
  while (words < endp)
327
320
    {
328
 
      md5_uint32 tm;
 
321
      sha1_uint32 tm;
329
322
      int t;
330
323
      for (t = 0; t < 16; t++)
331
324
        {