Make input data and source file names const in memacpy and stracpy.
[elinks/elinks-j605.git] / src / util / sha1.c
blob517e5c19908481720fe66c52943e96b1cb604198
1 /* SHA1 implementation */
3 /* The contents of this file are subject to the Mozilla Public
4 * License Version 1.1 (the "License"); you may not use this file
5 * except in compliance with the License. You may obtain a copy of
6 * the License at http://www.mozilla.org/MPL/
8 * Software distributed under the License is distributed on an "AS
9 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
10 * implied. See the License for the specific language governing
11 * rights and limitations under the License.
13 * The Original Code is SHA 180-1 Reference Implementation (Compact version)
15 * The Initial Developer of the Original Code is Paul Kocher of
16 * Cryptography Research. Portions created by Paul Kocher are
17 * Copyright (C) 1995-9 by Cryptography Research, Inc. All
18 * Rights Reserved.
20 * Contributor(s):
22 * Paul Kocher
24 * Alternatively, the contents of this file may be used under the
25 * terms of the GNU General Public License Version 2 or later (the
26 * "GPL"), in which case the provisions of the GPL are applicable
27 * instead of those above. If you wish to allow use of your
28 * version of this file only under the terms of the GPL and not to
29 * allow others to use your version of this file under the MPL,
30 * indicate your decision by deleting the provisions above and
31 * replace them with the notice and other provisions required by
32 * the GPL. If you do not delete the provisions above, a recipient
33 * may use your version of this file under either the MPL or the
34 * GPL. */
36 #include "util/sha1.h"
38 static void sha_hash_block(struct sha1_context *ctx);
40 void
41 init_sha1(struct sha1_context *ctx)
43 int i;
45 ctx->lenW = 0;
46 ctx->sizeHi = ctx->sizeLo = 0;
48 /* Initialize H with the magic constants (see FIPS180 for constants). */
49 ctx->H[0] = 0x67452301;
50 ctx->H[1] = 0xefcdab89;
51 ctx->H[2] = 0x98badcfe;
52 ctx->H[3] = 0x10325476;
53 ctx->H[4] = 0xc3d2e1f0;
55 for (i = 0; i < 80; i++)
56 ctx->W[i] = 0;
60 void
61 update_sha1(struct sha1_context *ctx, const unsigned char *dataIn,
62 unsigned long len)
64 int i;
66 /* Read the data into W and process blocks as they get full. */
67 for (i = 0; i < len; i++) {
68 ctx->W[ctx->lenW / 4] <<= 8;
69 ctx->W[ctx->lenW / 4] |= (unsigned int) dataIn[i];
71 if ((++ctx->lenW) % 64 == 0) {
72 sha_hash_block(ctx);
73 ctx->lenW = 0;
76 ctx->sizeLo += 8;
77 ctx->sizeHi += (ctx->sizeLo < 8);
82 void
83 done_sha1(struct sha1_context *ctx, sha1_digest_bin_T digest)
85 unsigned char pad0x80 = 0x80;
86 unsigned char pad0x00 = 0x00;
87 unsigned char padlen[8];
88 int i;
90 /* Pad with a binary 1 (e.g. 0x80), then zeroes, then length. */
91 padlen[0] = (unsigned char) ((ctx->sizeHi >> 24) & 255);
92 padlen[1] = (unsigned char) ((ctx->sizeHi >> 16) & 255);
93 padlen[2] = (unsigned char) ((ctx->sizeHi >> 8) & 255);
94 padlen[3] = (unsigned char) ((ctx->sizeHi >> 0) & 255);
95 padlen[4] = (unsigned char) ((ctx->sizeLo >> 24) & 255);
96 padlen[5] = (unsigned char) ((ctx->sizeLo >> 16) & 255);
97 padlen[6] = (unsigned char) ((ctx->sizeLo >> 8) & 255);
98 padlen[7] = (unsigned char) ((ctx->sizeLo >> 0) & 255);
100 update_sha1(ctx, &pad0x80, 1);
101 while (ctx->lenW != 56)
102 update_sha1(ctx, &pad0x00, 1);
103 update_sha1(ctx, padlen, 8);
105 /* Output hash. */
106 for (i = 0; i < 20; i++) {
107 digest[i] = (unsigned char) (ctx->H[i / 4] >> 24);
108 ctx->H[i / 4] <<= 8;
111 /* Re-initialize the context (also zeroizes contents). */
112 init_sha1(ctx);
115 unsigned char *
116 digest_sha1(const unsigned char *data, unsigned long length,
117 sha1_digest_bin_T digest)
119 struct sha1_context ctx;
121 init_sha1(&ctx);
123 if (length != 0)
124 update_sha1(&ctx, data, length);
126 done_sha1(&ctx, digest);
128 return digest;
132 #define SHA_ROT(X,n) (((X) << (n)) | ((X) >> (32 - (n))))
134 static void
135 sha_hash_block(struct sha1_context *ctx)
137 int t;
138 unsigned int A, B, C, D, E, TEMP;
140 for (t = 16; t <= 79; t++)
141 ctx->W[t] = SHA_ROT(ctx->W[t-3] ^ ctx->W[t-8] ^ ctx->W[t-14]
142 ^ ctx->W[t-16], 1);
144 A = ctx->H[0];
145 B = ctx->H[1];
146 C = ctx->H[2];
147 D = ctx->H[3];
148 E = ctx->H[4];
150 for (t = 0; t <= 19; t++) {
151 TEMP = SHA_ROT(A,5) + (((C^D)&B)^D) + E + ctx->W[t] + 0x5a827999;
152 E = D; D = C; C = SHA_ROT(B, 30); B = A; A = TEMP;
155 for (t = 20; t <= 39; t++) {
156 TEMP = SHA_ROT(A,5) + (B^C^D) + E + ctx->W[t] + 0x6ed9eba1;
157 E = D; D = C; C = SHA_ROT(B, 30); B = A; A = TEMP;
160 for (t = 40; t <= 59; t++) {
161 TEMP = SHA_ROT(A,5) + ((B&C)|(D&(B|C))) + E + ctx->W[t] + 0x8f1bbcdc;
162 E = D; D = C; C = SHA_ROT(B, 30); B = A; A = TEMP;
165 for (t = 60; t <= 79; t++) {
166 TEMP = SHA_ROT(A,5) + (B^C^D) + E + ctx->W[t] + 0xca62c1d6;
167 E = D; D = C; C = SHA_ROT(B, 30); B = A; A = TEMP;
170 ctx->H[0] += A;
171 ctx->H[1] += B;
172 ctx->H[2] += C;
173 ctx->H[3] += D;
174 ctx->H[4] += E;