Import 3.0 beta 3 tarball
[mozilla-nss.git] / security / nss / lib / freebl / sha.c
blobf7031d3d96bb80c7226452354551948593a0f675
1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
12 * License.
14 * The Original Code is SHA 180-1 Reference Implementation (Compact version).
16 * The Initial Developer of the Original Code is
17 * Paul Kocher of Cryptography Research.
18 * Portions created by the Initial Developer are Copyright (C) 1995-9
19 * the Initial Developer. All Rights Reserved.
21 * Contributor(s):
23 * Alternatively, the contents of this file may be used under the terms of
24 * either the GNU General Public License Version 2 or later (the "GPL"), or
25 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26 * in which case the provisions of the GPL or the LGPL are applicable instead
27 * of those above. If you wish to allow use of your version of this file only
28 * under the terms of either the GPL or the LGPL, and not to allow others to
29 * use your version of this file under the terms of the MPL, indicate your
30 * decision by deleting the provisions above and replace them with the notice
31 * and other provisions required by the GPL or the LGPL. If you do not delete
32 * the provisions above, a recipient may use your version of this file under
33 * the terms of any one of the MPL, the GPL or the LGPL.
35 * ***** END LICENSE BLOCK ***** */
37 #include "sha.h"
39 static void shaHashBlock(SHA_CTX *ctx);
41 void shaInit(SHA_CTX *ctx) {
42 int i;
44 ctx->lenW = 0;
45 ctx->sizeHi = ctx->sizeLo = 0;
47 /* Initialize H with the magic constants (see FIPS180 for constants)
49 ctx->H[0] = 0x67452301L;
50 ctx->H[1] = 0xefcdab89L;
51 ctx->H[2] = 0x98badcfeL;
52 ctx->H[3] = 0x10325476L;
53 ctx->H[4] = 0xc3d2e1f0L;
55 for (i = 0; i < 80; i++)
56 ctx->W[i] = 0;
60 void shaUpdate(SHA_CTX *ctx, unsigned char *dataIn, int len) {
61 int i;
63 /* Read the data into W and process blocks as they get full
65 for (i = 0; i < len; i++) {
66 ctx->W[ctx->lenW / 4] <<= 8;
67 ctx->W[ctx->lenW / 4] |= (unsigned long)dataIn[i];
68 if ((++ctx->lenW) % 64 == 0) {
69 shaHashBlock(ctx);
70 ctx->lenW = 0;
72 ctx->sizeLo += 8;
73 ctx->sizeHi += (ctx->sizeLo < 8);
78 void shaFinal(SHA_CTX *ctx, unsigned char hashout[20]) {
79 unsigned char pad0x80 = 0x80;
80 unsigned char pad0x00 = 0x00;
81 unsigned char padlen[8];
82 int i;
84 /* Pad with a binary 1 (e.g. 0x80), then zeroes, then length
86 padlen[0] = (unsigned char)((ctx->sizeHi >> 24) & 255);
87 padlen[1] = (unsigned char)((ctx->sizeHi >> 16) & 255);
88 padlen[2] = (unsigned char)((ctx->sizeHi >> 8) & 255);
89 padlen[3] = (unsigned char)((ctx->sizeHi >> 0) & 255);
90 padlen[4] = (unsigned char)((ctx->sizeLo >> 24) & 255);
91 padlen[5] = (unsigned char)((ctx->sizeLo >> 16) & 255);
92 padlen[6] = (unsigned char)((ctx->sizeLo >> 8) & 255);
93 padlen[7] = (unsigned char)((ctx->sizeLo >> 0) & 255);
94 shaUpdate(ctx, &pad0x80, 1);
95 while (ctx->lenW != 56)
96 shaUpdate(ctx, &pad0x00, 1);
97 shaUpdate(ctx, padlen, 8);
99 /* Output hash
101 for (i = 0; i < 20; i++) {
102 hashout[i] = (unsigned char)(ctx->H[i / 4] >> 24);
103 ctx->H[i / 4] <<= 8;
107 * Re-initialize the context (also zeroizes contents)
109 shaInit(ctx);
113 void shaBlock(unsigned char *dataIn, int len, unsigned char hashout[20]) {
114 SHA_CTX ctx;
116 shaInit(&ctx);
117 shaUpdate(&ctx, dataIn, len);
118 shaFinal(&ctx, hashout);
122 #define SHA_ROTL(X,n) (((X) << (n)) | ((X) >> (32-(n))))
124 static void shaHashBlock(SHA_CTX *ctx) {
125 int t;
126 unsigned long A,B,C,D,E,TEMP;
128 for (t = 16; t <= 79; t++)
129 ctx->W[t] =
130 SHA_ROTL(ctx->W[t-3] ^ ctx->W[t-8] ^ ctx->W[t-14] ^ ctx->W[t-16], 1);
132 A = ctx->H[0];
133 B = ctx->H[1];
134 C = ctx->H[2];
135 D = ctx->H[3];
136 E = ctx->H[4];
138 for (t = 0; t <= 19; t++) {
139 TEMP = SHA_ROTL(A,5) + (((C^D)&B)^D) + E + ctx->W[t] + 0x5a827999L;
140 E = D; D = C; C = SHA_ROTL(B, 30); B = A; A = TEMP;
142 for (t = 20; t <= 39; t++) {
143 TEMP = SHA_ROTL(A,5) + (B^C^D) + E + ctx->W[t] + 0x6ed9eba1L;
144 E = D; D = C; C = SHA_ROTL(B, 30); B = A; A = TEMP;
146 for (t = 40; t <= 59; t++) {
147 TEMP = SHA_ROTL(A,5) + ((B&C)|(D&(B|C))) + E + ctx->W[t] + 0x8f1bbcdcL;
148 E = D; D = C; C = SHA_ROTL(B, 30); B = A; A = TEMP;
150 for (t = 60; t <= 79; t++) {
151 TEMP = SHA_ROTL(A,5) + (B^C^D) + E + ctx->W[t] + 0xca62c1d6L;
152 E = D; D = C; C = SHA_ROTL(B, 30); B = A; A = TEMP;
155 ctx->H[0] += A;
156 ctx->H[1] += B;
157 ctx->H[2] += C;
158 ctx->H[3] += D;
159 ctx->H[4] += E;