sha1.c
author Kristian H?gsberg <krh@redhat.com>
Thu Oct 25 13:21:18 2007 -0400 (2007-10-25)
changeset 61 b0e1638a9f96
permissions -rw-r--r--
Use immediate links for property package lists too.
krh@0
     1
/*
krh@0
     2
 * The contents of this file are subject to the Mozilla Public
krh@0
     3
 * License Version 1.1 (the "License"); you may not use this file
krh@0
     4
 * except in compliance with the License. You may obtain a copy of
krh@0
     5
 * the License at http://www.mozilla.org/MPL/
krh@0
     6
 *
krh@0
     7
 * Software distributed under the License is distributed on an "AS
krh@0
     8
 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
krh@0
     9
 * implied. See the License for the specific language governing
krh@0
    10
 * rights and limitations under the License.
krh@0
    11
 *
krh@0
    12
 * The Original Code is SHA 180-1 Reference Implementation (Compact version)
krh@0
    13
 *
krh@0
    14
 * The Initial Developer of the Original Code is Paul Kocher of
krh@0
    15
 * Cryptography Research.  Portions created by Paul Kocher are
krh@0
    16
 * Copyright (C) 1995-9 by Cryptography Research, Inc.  All
krh@0
    17
 * Rights Reserved.
krh@0
    18
 *
krh@0
    19
 * Contributor(s):
krh@0
    20
 *
krh@0
    21
 *     Paul Kocher
krh@0
    22
 *
krh@0
    23
 * Alternatively, the contents of this file may be used under the
krh@0
    24
 * terms of the GNU General Public License Version 2 or later (the
krh@0
    25
 * "GPL"), in which case the provisions of the GPL are applicable
krh@0
    26
 * instead of those above.  If you wish to allow use of your
krh@0
    27
 * version of this file only under the terms of the GPL and not to
krh@0
    28
 * allow others to use your version of this file under the MPL,
krh@0
    29
 * indicate your decision by deleting the provisions above and
krh@0
    30
 * replace them with the notice and other provisions required by
krh@0
    31
 * the GPL.  If you do not delete the provisions above, a recipient
krh@0
    32
 * may use your version of this file under either the MPL or the
krh@0
    33
 * GPL.
krh@0
    34
 */
krh@0
    35
krh@0
    36
#include "sha1.h"
krh@0
    37
krh@0
    38
static void shaHashBlock(SHA_CTX *ctx);
krh@0
    39
krh@0
    40
void SHA1_Init(SHA_CTX *ctx) {
krh@0
    41
  int i;
krh@0
    42
krh@0
    43
  ctx->lenW = 0;
krh@0
    44
  ctx->sizeHi = ctx->sizeLo = 0;
krh@0
    45
krh@0
    46
  /* Initialize H with the magic constants (see FIPS180 for constants)
krh@0
    47
   */
krh@0
    48
  ctx->H[0] = 0x67452301;
krh@0
    49
  ctx->H[1] = 0xefcdab89;
krh@0
    50
  ctx->H[2] = 0x98badcfe;
krh@0
    51
  ctx->H[3] = 0x10325476;
krh@0
    52
  ctx->H[4] = 0xc3d2e1f0;
krh@0
    53
krh@0
    54
  for (i = 0; i < 80; i++)
krh@0
    55
    ctx->W[i] = 0;
krh@0
    56
}
krh@0
    57
krh@0
    58
krh@0
    59
void SHA1_Update(SHA_CTX *ctx, const void *_dataIn, int len) {
krh@0
    60
  const unsigned char *dataIn = _dataIn;
krh@0
    61
  int i;
krh@0
    62
krh@0
    63
  /* Read the data into W and process blocks as they get full
krh@0
    64
   */
krh@0
    65
  for (i = 0; i < len; i++) {
krh@0
    66
    ctx->W[ctx->lenW / 4] <<= 8;
krh@0
    67
    ctx->W[ctx->lenW / 4] |= (unsigned int)dataIn[i];
krh@0
    68
    if ((++ctx->lenW) % 64 == 0) {
krh@0
    69
      shaHashBlock(ctx);
krh@0
    70
      ctx->lenW = 0;
krh@0
    71
    }
krh@0
    72
    ctx->sizeLo += 8;
krh@0
    73
    ctx->sizeHi += (ctx->sizeLo < 8);
krh@0
    74
  }
krh@0
    75
}
krh@0
    76
krh@0
    77
krh@0
    78
void SHA1_Final(unsigned char hashout[20], SHA_CTX *ctx) {
krh@0
    79
  unsigned char pad0x80 = 0x80;
krh@0
    80
  unsigned char pad0x00 = 0x00;
krh@0
    81
  unsigned char padlen[8];
krh@0
    82
  int i;
krh@0
    83
krh@0
    84
  /* Pad with a binary 1 (e.g. 0x80), then zeroes, then length
krh@0
    85
   */
krh@0
    86
  padlen[0] = (unsigned char)((ctx->sizeHi >> 24) & 255);
krh@0
    87
  padlen[1] = (unsigned char)((ctx->sizeHi >> 16) & 255);
krh@0
    88
  padlen[2] = (unsigned char)((ctx->sizeHi >> 8) & 255);
krh@0
    89
  padlen[3] = (unsigned char)((ctx->sizeHi >> 0) & 255);
krh@0
    90
  padlen[4] = (unsigned char)((ctx->sizeLo >> 24) & 255);
krh@0
    91
  padlen[5] = (unsigned char)((ctx->sizeLo >> 16) & 255);
krh@0
    92
  padlen[6] = (unsigned char)((ctx->sizeLo >> 8) & 255);
krh@0
    93
  padlen[7] = (unsigned char)((ctx->sizeLo >> 0) & 255);
krh@0
    94
  SHA1_Update(ctx, &pad0x80, 1);
krh@0
    95
  while (ctx->lenW != 56)
krh@0
    96
    SHA1_Update(ctx, &pad0x00, 1);
krh@0
    97
  SHA1_Update(ctx, padlen, 8);
krh@0
    98
krh@0
    99
  /* Output hash
krh@0
   100
   */
krh@0
   101
  for (i = 0; i < 20; i++) {
krh@0
   102
    hashout[i] = (unsigned char)(ctx->H[i / 4] >> 24);
krh@0
   103
    ctx->H[i / 4] <<= 8;
krh@0
   104
  }
krh@0
   105
krh@0
   106
  /*
krh@0
   107
   *  Re-initialize the context (also zeroizes contents)
krh@0
   108
   */
krh@0
   109
  SHA1_Init(ctx);
krh@0
   110
}
krh@0
   111
krh@0
   112
krh@0
   113
#define SHA_ROT(X,n) (((X) << (n)) | ((X) >> (32-(n))))
krh@0
   114
krh@0
   115
static void shaHashBlock(SHA_CTX *ctx) {
krh@0
   116
  int t;
krh@0
   117
  unsigned int A,B,C,D,E,TEMP;
krh@0
   118
krh@0
   119
  for (t = 16; t <= 79; t++)
krh@0
   120
    ctx->W[t] =
krh@0
   121
      SHA_ROT(ctx->W[t-3] ^ ctx->W[t-8] ^ ctx->W[t-14] ^ ctx->W[t-16], 1);
krh@0
   122
krh@0
   123
  A = ctx->H[0];
krh@0
   124
  B = ctx->H[1];
krh@0
   125
  C = ctx->H[2];
krh@0
   126
  D = ctx->H[3];
krh@0
   127
  E = ctx->H[4];
krh@0
   128
krh@0
   129
  for (t = 0; t <= 19; t++) {
krh@0
   130
    TEMP = SHA_ROT(A,5) + (((C^D)&B)^D)     + E + ctx->W[t] + 0x5a827999;
krh@0
   131
    E = D; D = C; C = SHA_ROT(B, 30); B = A; A = TEMP;
krh@0
   132
  }
krh@0
   133
  for (t = 20; t <= 39; t++) {
krh@0
   134
    TEMP = SHA_ROT(A,5) + (B^C^D)           + E + ctx->W[t] + 0x6ed9eba1;
krh@0
   135
    E = D; D = C; C = SHA_ROT(B, 30); B = A; A = TEMP;
krh@0
   136
  }
krh@0
   137
  for (t = 40; t <= 59; t++) {
krh@0
   138
    TEMP = SHA_ROT(A,5) + ((B&C)|(D&(B|C))) + E + ctx->W[t] + 0x8f1bbcdc;
krh@0
   139
    E = D; D = C; C = SHA_ROT(B, 30); B = A; A = TEMP;
krh@0
   140
  }
krh@0
   141
  for (t = 60; t <= 79; t++) {
krh@0
   142
    TEMP = SHA_ROT(A,5) + (B^C^D)           + E + ctx->W[t] + 0xca62c1d6;
krh@0
   143
    E = D; D = C; C = SHA_ROT(B, 30); B = A; A = TEMP;
krh@0
   144
  }
krh@0
   145
krh@0
   146
  ctx->H[0] += A;
krh@0
   147
  ctx->H[1] += B;
krh@0
   148
  ctx->H[2] += C;
krh@0
   149
  ctx->H[3] += D;
krh@0
   150
  ctx->H[4] += E;
krh@0
   151
}