sha1.c 2.45 KB
Newer Older
Niels Möller's avatar
Niels Möller committed
1
/* sha1.c
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33

   The sha1 hash function.
   Defined by http://www.itl.nist.gov/fipspubs/fip180-1.htm.

   Copyright (C) 2001, 2013 Niels Möller

   This file is part of GNU Nettle.

   GNU Nettle is free software: you can redistribute it and/or
   modify it under the terms of either:

     * the GNU Lesser General Public License as published by the Free
       Software Foundation; either version 3 of the License, or (at your
       option) any later version.

   or

     * the GNU General Public License as published by the Free
       Software Foundation; either version 2 of the License, or (at your
       option) any later version.

   or both in parallel, as here.

   GNU Nettle is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received copies of the GNU General Public License and
   the GNU Lesser General Public License along with this program.  If
   not, see http://www.gnu.org/licenses/.
*/
Niels Möller's avatar
Niels Möller committed
34

35 36 37
#if HAVE_CONFIG_H
# include "config.h"
#endif
Niels Möller's avatar
Niels Möller committed
38 39

#include <assert.h>
Niels Möller's avatar
Niels Möller committed
40
#include <stdlib.h>
Niels Möller's avatar
Niels Möller committed
41 42
#include <string.h>

43
#include "sha1.h"
44 45

#include "macros.h"
46
#include "nettle-write.h"
47

Niels Möller's avatar
Niels Möller committed
48 49 50 51
/* Initialize the SHA values */
void
sha1_init(struct sha1_ctx *ctx)
{
52 53 54 55
  /* FIXME: Put the buffer last in the struct, and arrange so that we
     can initialize with a single memcpy. */
  static const uint32_t iv[_SHA1_DIGEST_LENGTH] = 
    {
56
      /* SHA initial values, first 4 identical to md5's. */
57 58 59 60 61 62 63 64
      0x67452301L,
      0xEFCDAB89L,
      0x98BADCFEL,
      0x10325476L,
      0xC3D2E1F0L,
    };

  memcpy(ctx->state, iv, sizeof(ctx->state));
65
  ctx->count = 0;
Niels Möller's avatar
Niels Möller committed
66 67 68 69 70
  
  /* Initialize buffer */
  ctx->index = 0;
}

71 72
#define COMPRESS(ctx, data) (_nettle_sha1_compress((ctx)->state, data))

Niels Möller's avatar
Niels Möller committed
73 74
void
sha1_update(struct sha1_ctx *ctx,
75
	    size_t length, const uint8_t *data)
Niels Möller's avatar
Niels Möller committed
76
{
77
  MD_UPDATE (ctx, length, data, COMPRESS, ctx->count++);
Niels Möller's avatar
Niels Möller committed
78 79 80
}
	  
void
81
sha1_digest(struct sha1_ctx *ctx,
82
	    size_t length,
Niels Möller's avatar
Niels Möller committed
83 84
	    uint8_t *digest)
{
85
  uint64_t bit_count;
86

Niels Möller's avatar
Niels Möller committed
87 88
  assert(length <= SHA1_DIGEST_SIZE);

89 90
  MD_PAD(ctx, 8, COMPRESS);

91 92
  /* There are 512 = 2^9 bits in one block */
  bit_count = (ctx->count << 9) | (ctx->index << 3);
93 94

  /* append the 64 bit count */
95
  WRITE_UINT64(ctx->block + (SHA1_BLOCK_SIZE - 8), bit_count);
96 97
  _nettle_sha1_compress(ctx->state, ctx->block);

98
  _nettle_write_be32(length, digest, ctx->state);
99
  sha1_init(ctx);
Niels Möller's avatar
Niels Möller committed
100
}